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-2015 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>
13 * Permission to use, copy, modify, and distribute this software for any
14 * purpose with or without fee is hereby granted, provided that the above
15 * copyright notice and this permission notice appear in all copies.
17 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
18 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
20 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
21 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
22 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
23 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 * Much code and ideas taken from dwm under the following license:
27 * MIT/X Consortium License
29 * 2006-2008 Anselm R Garbe <garbeam at gmail dot com>
30 * 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
31 * 2006-2007 Jukka Salmi <jukka at salmi dot ch>
32 * 2007 Premysl Hruby <dfenze at gmail dot com>
33 * 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
34 * 2007 Christof Musik <christof at sendfax dot de>
35 * 2007-2008 Enno Gottox Boland <gottox at s01 dot de>
36 * 2007-2008 Peter Hartlich <sgkkr at hartlich dot com>
37 * 2008 Martin Hurton <martin dot hurton at gmail dot com>
39 * Permission is hereby granted, free of charge, to any person obtaining a
40 * copy of this software and associated documentation files (the "Software"),
41 * to deal in the Software without restriction, including without limitation
42 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
43 * and/or sell copies of the Software, and to permit persons to whom the
44 * Software is furnished to do so, subject to the following conditions:
46 * The above copyright notice and this permission notice shall be included in
47 * all copies or substantial portions of the Software.
49 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
50 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
51 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
52 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
53 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
54 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
55 * DEALINGS IN THE SOFTWARE.
59 #include <sys/types.h>
63 #include <sys/queue.h>
64 #include <sys/param.h>
65 #include <sys/select.h>
66 #if defined(__linux__)
68 #elif defined(__OpenBSD__)
70 #elif defined(__FreeBSD__)
94 #include <X11/cursorfont.h>
95 #include <X11/extensions/Xrandr.h>
96 #include <X11/Xcursor/Xcursor.h>
97 #include <X11/Xft/Xft.h>
98 #include <X11/Xlib-xcb.h>
99 #include <xcb/xcb_atom.h>
100 #include <xcb/xcb_aux.h>
101 #include <xcb/xcb_event.h>
102 #include <xcb/xcb_icccm.h>
103 #include <xcb/xcb_keysyms.h>
104 #include <xcb/xtest.h>
105 #include <xcb/randr.h>
113 #ifdef SPECTRWM_BUILDSTR
114 static const char *buildstr
= SPECTRWM_BUILDSTR
;
116 static const char *buildstr
= SPECTRWM_VERSION
;
119 #if !defined(__CYGWIN__) /* cygwin chokes on randr stuff */
121 # error RandR versions less than 1.0 are not supported
124 # if RANDR_MAJOR >= 1
125 # if RANDR_MINOR >= 2
126 # define SWM_XRR_HAS_CRTC
129 #endif /* __CYGWIN__ */
131 #ifndef XCB_ICCCM_NUM_WM_HINTS_ELEMENTS
132 #define XCB_ICCCM_SIZE_HINT_P_MIN_SIZE XCB_SIZE_HINT_P_MIN_SIZE
133 #define XCB_ICCCM_SIZE_HINT_P_MAX_SIZE XCB_SIZE_HINT_P_MAX_SIZE
134 #define XCB_ICCCM_SIZE_HINT_P_RESIZE_INC XCB_SIZE_HINT_P_RESIZE_INC
135 #define XCB_ICCCM_WM_HINT_INPUT XCB_WM_HINT_INPUT
136 #define XCB_ICCCM_WM_HINT_X_URGENCY XCB_WM_HINT_X_URGENCY
137 #define XCB_ICCCM_WM_STATE_ICONIC XCB_WM_STATE_ICONIC
138 #define XCB_ICCCM_WM_STATE_WITHDRAWN XCB_WM_STATE_WITHDRAWN
139 #define XCB_ICCCM_WM_STATE_NORMAL XCB_WM_STATE_NORMAL
140 #define xcb_icccm_get_text_property_reply_t xcb_get_text_property_reply_t
141 #define xcb_icccm_get_text_property_reply_wipe xcb_get_text_property_reply_wipe
142 #define xcb_icccm_get_wm_class xcb_get_wm_class
143 #define xcb_icccm_get_wm_class_reply xcb_get_wm_class_reply
144 #define xcb_icccm_get_wm_class_reply_t xcb_get_wm_class_reply_t
145 #define xcb_icccm_get_wm_class_reply_wipe xcb_get_wm_class_reply_wipe
146 #define xcb_icccm_get_wm_hints xcb_get_wm_hints
147 #define xcb_icccm_wm_hints_get_urgency xcb_wm_hints_get_urgency
148 #define xcb_icccm_get_wm_hints_reply xcb_get_wm_hints_reply
149 #define xcb_icccm_get_wm_name xcb_get_wm_name
150 #define xcb_icccm_get_wm_name_reply xcb_get_wm_name_reply
151 #define xcb_icccm_get_wm_normal_hints xcb_get_wm_normal_hints
152 #define xcb_icccm_get_wm_normal_hints_reply xcb_get_wm_normal_hints_reply
153 #define xcb_icccm_get_wm_protocols xcb_get_wm_protocols
154 #define xcb_icccm_get_wm_protocols_reply xcb_get_wm_protocols_reply
155 #define xcb_icccm_get_wm_protocols_reply_t xcb_get_wm_protocols_reply_t
156 #define xcb_icccm_get_wm_protocols_reply_wipe xcb_get_wm_protocols_reply_wipe
157 #define xcb_icccm_get_wm_transient_for xcb_get_wm_transient_for
158 #define xcb_icccm_get_wm_transient_for_reply xcb_get_wm_transient_for_reply
159 #define xcb_icccm_wm_hints_t xcb_wm_hints_t
162 /*#define SWM_DEBUG*/
164 #define DPRINTF(x...) do { \
166 fprintf(stderr, x); \
168 #define DNPRINTF(n,x...) do { \
169 if (swm_debug & n) { \
170 fprintf(stderr, "%ld ", (long)(time(NULL) - time_started)); \
171 fprintf(stderr, x); \
174 #define SWM_D_MISC 0x0001
175 #define SWM_D_EVENT 0x0002
176 #define SWM_D_WS 0x0004
177 #define SWM_D_FOCUS 0x0008
178 #define SWM_D_MOVE 0x0010
179 #define SWM_D_STACK 0x0020
180 #define SWM_D_MOUSE 0x0040
181 #define SWM_D_PROP 0x0080
182 #define SWM_D_CLASS 0x0100
183 #define SWM_D_KEY 0x0200
184 #define SWM_D_QUIRK 0x0400
185 #define SWM_D_SPAWN 0x0800
186 #define SWM_D_EVENTQ 0x1000
187 #define SWM_D_CONF 0x2000
188 #define SWM_D_BAR 0x4000
189 #define SWM_D_INIT 0x8000
191 uint32_t swm_debug
= 0
210 #define DPRINTF(x...)
211 #define DNPRINTF(n,x...)
214 #define SWM_EWMH_ACTION_COUNT_MAX (8)
215 #define EWMH_F_FULLSCREEN (0x001)
216 #define EWMH_F_ABOVE (0x002)
217 #define EWMH_F_HIDDEN (0x004)
218 #define EWMH_F_MAXIMIZED_VERT (0x008)
219 #define EWMH_F_MAXIMIZED_HORZ (0x010)
220 #define EWMH_F_SKIP_PAGER (0x020)
221 #define EWMH_F_SKIP_TASKBAR (0x040)
222 #define SWM_F_MANUAL (0x080)
224 #define EWMH_F_MAXIMIZED (EWMH_F_MAXIMIZED_VERT | EWMH_F_MAXIMIZED_HORZ)
226 /* convert 8-bit to 16-bit */
227 #define RGB_8_TO_16(col) (((col) << 8) + (col))
229 #define PIXEL_TO_XRENDERCOLOR(px, xrc) \
230 xrc.red = RGB_8_TO_16((px) >> 16 & 0xff); \
231 xrc.green = RGB_8_TO_16((px) >> 8 & 0xff); \
232 xrc.blue = RGB_8_TO_16((px) & 0xff); \
235 #define LENGTH(x) (int)(sizeof (x) / sizeof (x)[0])
236 #define MODKEY XCB_MOD_MASK_1
237 #define CLEANMASK(mask) ((mask) & ~(numlockmask | XCB_MOD_MASK_LOCK))
238 #define BUTTONMASK (XCB_EVENT_MASK_BUTTON_PRESS | \
239 XCB_EVENT_MASK_BUTTON_RELEASE)
240 #define MOUSEMASK (BUTTONMASK|XCB_EVENT_MASK_POINTER_MOTION)
241 #define SWM_PROPLEN (16)
242 #define SWM_FUNCNAME_LEN (32)
243 #define SWM_KEYS_LEN (255)
244 #define SWM_QUIRK_LEN (64)
245 #define X(r) ((r)->g.x)
246 #define Y(r) ((r)->g.y)
247 #define WIDTH(r) ((r)->g.w)
248 #define HEIGHT(r) ((r)->g.h)
249 #define BORDER(w) ((w)->bordered ? border_width : 0)
250 #define MAX_X(r) ((r)->g.x + (r)->g.w)
251 #define MAX_Y(r) ((r)->g.y + (r)->g.h)
252 #define SH_MIN(w) ((w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE)
253 #define SH_MIN_W(w) ((w)->sh.min_width)
254 #define SH_MIN_H(w) ((w)->sh.min_height)
255 #define SH_MAX(w) ((w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MAX_SIZE)
256 #define SH_MAX_W(w) ((w)->sh.max_width)
257 #define SH_MAX_H(w) ((w)->sh.max_height)
258 #define SH_INC(w) ((w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_RESIZE_INC)
259 #define SH_INC_W(w) ((w)->sh.width_inc)
260 #define SH_INC_H(w) ((w)->sh.height_inc)
261 #define SWM_MAX_FONT_STEPS (3)
262 #define WINID(w) ((w) ? (w)->id : XCB_WINDOW_NONE)
263 #define WS_FOCUSED(ws) ((ws)->r && (ws)->r->s->r_focus == (ws)->r)
264 #define YESNO(x) ((x) ? "yes" : "no")
265 #define ICONIC(w) ((w)->ewmh_flags & EWMH_F_HIDDEN)
266 #define ABOVE(w) ((w)->ewmh_flags & EWMH_F_ABOVE)
267 #define FULLSCREEN(w) ((w)->ewmh_flags & EWMH_F_FULLSCREEN)
268 #define MAXIMIZED_VERT(w) ((w)->ewmh_flags & EWMH_F_MAXIMIZED_VERT)
269 #define MAXIMIZED_HORZ(w) ((w)->ewmh_flags & EWMH_F_MAXIMIZED_HORZ)
270 #define MAXIMIZED(w) (MAXIMIZED_VERT(w) || MAXIMIZED_HORZ(w))
271 #define MANUAL(w) ((w)->ewmh_flags & SWM_F_MANUAL)
272 #define TRANS(w) ((w)->transient != XCB_WINDOW_NONE)
273 #define FLOATING(w) (ABOVE(w) || TRANS(w) || FULLSCREEN(w) || \
276 /* Constrain Window flags */
277 #define SWM_CW_RESIZABLE (0x01)
278 #define SWM_CW_SOFTBOUNDARY (0x02)
279 #define SWM_CW_HARDBOUNDARY (0x04)
280 #define SWM_CW_RIGHT (0x10)
281 #define SWM_CW_LEFT (0x20)
282 #define SWM_CW_BOTTOM (0x40)
283 #define SWM_CW_TOP (0x80)
284 #define SWM_CW_ALLSIDES (0xf0)
286 #define SWM_FOCUS_DEFAULT (0)
287 #define SWM_FOCUS_FOLLOW (1)
288 #define SWM_FOCUS_MANUAL (2)
290 #define SWM_CK_NONE (0)
291 #define SWM_CK_ALL (0xf)
292 #define SWM_CK_FOCUS (0x1)
293 #define SWM_CK_POINTER (0x2)
294 #define SWM_CK_FALLBACK (0x4)
295 #define SWM_CK_REGION (0x8)
297 #define SWM_G_ALL (0xf)
298 #define SWM_G_SIZE (0x1)
299 #define SWM_G_POS (0x2)
301 #define SWM_CONF_DEFAULT (0)
302 #define SWM_CONF_KEYMAPPING (1)
305 #define SWM_LIB "/usr/local/lib/libswmhack.so"
312 xcb_atom_t a_net_wm_check
;
313 xcb_atom_t a_net_supported
;
314 xcb_atom_t a_takefocus
;
315 xcb_atom_t a_utf8_string
;
317 volatile sig_atomic_t running
= 1;
318 volatile sig_atomic_t restart_wm
= 0;
319 xcb_timestamp_t last_event_time
= 0;
323 unsigned int numlockmask
= 0;
326 xcb_connection_t
*conn
;
327 xcb_key_symbols_t
*syms
;
329 int boundary_width
= 50;
330 bool cycle_empty
= false;
331 bool cycle_visible
= false;
333 int font_adjusted
= 0;
334 unsigned int mod_key
= MODKEY
;
335 bool warp_pointer
= false;
336 unsigned int mouse_button_move
= XCB_BUTTON_INDEX_1
;
337 unsigned int mouse_button_resize
= XCB_BUTTON_INDEX_3
;
340 struct swm_region
*search_r
;
341 int select_list_pipe
[2];
342 int select_resp_pipe
[2];
344 volatile sig_atomic_t search_resp
;
345 int search_resp_action
;
347 struct search_window
{
348 TAILQ_ENTRY(search_window
) entry
;
352 xcb_window_t indicator
;
354 TAILQ_HEAD(search_winlist
, search_window
);
355 struct search_winlist search_wl
;
360 SWM_SEARCH_UNICONIFY
,
361 SWM_SEARCH_NAME_WORKSPACE
,
362 SWM_SEARCH_SEARCH_WORKSPACE
,
363 SWM_SEARCH_SEARCH_WINDOW
366 #define SWM_STACK_TOP (0)
367 #define SWM_STACK_BOTTOM (1)
368 #define SWM_STACK_ABOVE (2)
369 #define SWM_STACK_BELOW (3)
372 double dialog_ratio
= 0.6;
374 #define SWM_BAR_MAX (256)
375 #define SWM_BAR_JUSTIFY_LEFT (0)
376 #define SWM_BAR_JUSTIFY_CENTER (1)
377 #define SWM_BAR_JUSTIFY_RIGHT (2)
378 #define SWM_BAR_OFFSET (4)
379 #define SWM_BAR_FONTS "-*-terminus-medium-*-*-*-12-*-*-*-*-*-*-*," \
380 "-*-profont-*-*-*-*-12-*-*-*-*-*-*-*," \
381 "-*-times-medium-r-*-*-12-*-*-*-*-*-*-*," \
382 "-misc-fixed-medium-r-*-*-12-*-*-*-*-*-*-*," \
383 "-*-*-*-r-*-*-*-*-*-*-*-*-*-*"
385 #ifdef X_HAVE_UTF8_STRING
386 #define DRAWSTRING(x...) Xutf8DrawString(x)
388 #define DRAWSTRING(x...) XmbDrawString(x)
391 char *bar_argv
[] = { NULL
, NULL
};
393 char bar_ext
[SWM_BAR_MAX
];
394 char bar_ext_buf
[SWM_BAR_MAX
];
395 char bar_vertext
[SWM_BAR_MAX
];
396 bool bar_version
= false;
397 bool bar_enabled
= true;
398 int bar_border_width
= 1;
399 bool bar_at_bottom
= false;
400 bool bar_extra
= false;
402 int bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
403 char *bar_format
= NULL
;
404 bool stack_enabled
= true;
405 bool clock_enabled
= true;
406 bool iconic_enabled
= false;
407 bool urgent_enabled
= false;
408 bool urgent_collapse
= false;
409 char *clock_format
= NULL
;
410 bool window_class_enabled
= false;
411 bool window_instance_enabled
= false;
412 bool window_name_enabled
= false;
413 int focus_mode
= SWM_FOCUS_DEFAULT
;
414 int focus_close
= SWM_STACK_BELOW
;
415 bool focus_close_wrap
= true;
416 int focus_default
= SWM_STACK_TOP
;
417 int spawn_position
= SWM_STACK_TOP
;
418 bool disable_border
= false;
419 int border_width
= 1;
420 int region_padding
= 0;
422 bool java_workaround
= true;
423 bool verbose_layout
= false;
429 XFontSetExtents
*bar_fs_extents
;
431 bool bar_font_legacy
= true;
433 XftColor bar_font_color
;
434 XftColor search_font_color
;
436 char *startup_exception
;
437 unsigned int nr_exceptions
= 0;
439 /* layout manager data */
440 struct swm_geometry
{
453 struct swm_geometry g
;
456 /* virtual "screens" */
458 TAILQ_ENTRY(swm_region
) entry
;
460 struct swm_geometry g
;
461 struct workspace
*ws
; /* current workspace on this region */
462 struct workspace
*ws_prior
; /* prior workspace on this region */
463 struct swm_screen
*s
; /* screen idx */
466 TAILQ_HEAD(swm_region_list
, swm_region
);
469 TAILQ_ENTRY(ws_win
) entry
;
470 TAILQ_ENTRY(ws_win
) stack_entry
;
472 xcb_window_t transient
;
473 struct ws_win
*focus_child
; /* focus on child transient */
474 struct swm_geometry g
; /* current geometry */
475 struct swm_geometry g_float
; /* region coordinates */
476 bool g_floatvalid
; /* g_float geometry validity */
480 int font_size_boundary
[SWM_MAX_FONT_STEPS
];
487 struct workspace
*ws
; /* always valid */
488 struct swm_screen
*s
; /* always valid, never changes */
490 xcb_icccm_get_wm_class_reply_t ch
;
491 xcb_icccm_wm_hints_t hints
;
493 TAILQ_HEAD(ws_win_list
, ws_win
);
494 TAILQ_HEAD(ws_win_stack
, ws_win
);
498 TAILQ_ENTRY(pid_e
) entry
;
502 TAILQ_HEAD(pid_list
, pid_e
);
503 struct pid_list pidlist
= TAILQ_HEAD_INITIALIZER(pidlist
);
505 /* layout handlers */
507 void vertical_config(struct workspace
*, int);
508 void vertical_stack(struct workspace
*, struct swm_geometry
*);
509 void horizontal_config(struct workspace
*, int);
510 void horizontal_stack(struct workspace
*, struct swm_geometry
*);
511 void max_stack(struct workspace
*, struct swm_geometry
*);
512 void plain_stacker(struct workspace
*);
513 void fancy_stacker(struct workspace
*);
516 void (*l_stack
)(struct workspace
*, struct swm_geometry
*);
517 void (*l_config
)(struct workspace
*, int);
519 #define SWM_L_FOCUSPREV (1<<0)
520 #define SWM_L_MAPONFOCUS (1<<1)
521 void (*l_string
)(struct workspace
*);
523 /* stack, configure */
524 { vertical_stack
, vertical_config
, 0, plain_stacker
},
525 { horizontal_stack
, horizontal_config
, 0, plain_stacker
},
527 SWM_L_MAPONFOCUS
| SWM_L_FOCUSPREV
, plain_stacker
},
528 { NULL
, NULL
, 0, NULL
},
531 /* position of max_stack mode in the layouts array, index into layouts! */
532 #define SWM_V_STACK (0)
533 #define SWM_H_STACK (1)
534 #define SWM_MAX_STACK (2)
536 #define SWM_H_SLICE (32)
537 #define SWM_V_SLICE (32)
539 /* define work spaces */
541 int idx
; /* workspace index */
542 char *name
; /* workspace name */
543 bool always_raise
; /* raise windows on focus */
544 bool bar_enabled
; /* bar visibility */
545 struct layout
*cur_layout
; /* current layout handlers */
546 struct ws_win
*focus
; /* may be NULL */
547 struct ws_win
*focus_prev
; /* may be NULL */
548 struct ws_win
*focus_pending
; /* may be NULL */
549 struct swm_region
*r
; /* may be NULL */
550 struct swm_region
*old_r
; /* may be NULL */
551 struct ws_win_list winlist
; /* list of windows in ws */
552 struct ws_win_list unmanagedlist
; /* list of dead windows in ws */
553 struct ws_win_stack stack
; /* stacking order */
554 int state
; /* mapping state */
555 char stacker
[10]; /* display stacker and layout */
559 int horizontal_msize
;
561 int horizontal_stacks
;
562 bool horizontal_flip
;
572 SWM_WS_STATE_MAPPING
,
578 SWM_S_COLOR_BAR_BORDER
,
579 SWM_S_COLOR_BAR_BORDER_UNFOCUS
,
580 SWM_S_COLOR_BAR_FONT
,
582 SWM_S_COLOR_FOCUS_MAXIMIZED
,
584 SWM_S_COLOR_UNFOCUS_MAXIMIZED
,
588 /* physical screen mapping */
589 #define SWM_WS_MAX (22) /* hard limit */
590 int workspace_limit
= 10; /* soft limit */
593 int idx
; /* screen index */
594 struct swm_region_list rl
; /* list of regions on this screen */
595 struct swm_region_list orl
; /* list of old regions */
597 struct workspace ws
[SWM_WS_MAX
];
598 struct swm_region
*r_focus
;
605 } c
[SWM_S_COLOR_MAX
];
607 xcb_gcontext_t bar_gc
;
610 struct swm_screen
*screens
;
612 /* args to functions */
615 #define SWM_ARG_ID_FOCUSNEXT (0)
616 #define SWM_ARG_ID_FOCUSPREV (1)
617 #define SWM_ARG_ID_FOCUSMAIN (2)
618 #define SWM_ARG_ID_FOCUSURGENT (3)
619 #define SWM_ARG_ID_SWAPNEXT (10)
620 #define SWM_ARG_ID_SWAPPREV (11)
621 #define SWM_ARG_ID_SWAPMAIN (12)
622 #define SWM_ARG_ID_MOVELAST (13)
623 #define SWM_ARG_ID_MASTERSHRINK (20)
624 #define SWM_ARG_ID_MASTERGROW (21)
625 #define SWM_ARG_ID_MASTERADD (22)
626 #define SWM_ARG_ID_MASTERDEL (23)
627 #define SWM_ARG_ID_FLIPLAYOUT (24)
628 #define SWM_ARG_ID_STACKRESET (30)
629 #define SWM_ARG_ID_STACKINIT (31)
630 #define SWM_ARG_ID_CYCLEWS_UP (40)
631 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
632 #define SWM_ARG_ID_CYCLERG_UP (42)
633 #define SWM_ARG_ID_CYCLERG_DOWN (43)
634 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
635 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
636 #define SWM_ARG_ID_CYCLEWS_MOVE_UP (46)
637 #define SWM_ARG_ID_CYCLEWS_MOVE_DOWN (47)
638 #define SWM_ARG_ID_STACKINC (50)
639 #define SWM_ARG_ID_STACKDEC (51)
640 #define SWM_ARG_ID_SS_ALL (60)
641 #define SWM_ARG_ID_SS_WINDOW (61)
642 #define SWM_ARG_ID_DONTCENTER (70)
643 #define SWM_ARG_ID_CENTER (71)
644 #define SWM_ARG_ID_KILLWINDOW (80)
645 #define SWM_ARG_ID_DELETEWINDOW (81)
646 #define SWM_ARG_ID_WIDTHGROW (90)
647 #define SWM_ARG_ID_WIDTHSHRINK (91)
648 #define SWM_ARG_ID_HEIGHTGROW (92)
649 #define SWM_ARG_ID_HEIGHTSHRINK (93)
650 #define SWM_ARG_ID_MOVEUP (100)
651 #define SWM_ARG_ID_MOVEDOWN (101)
652 #define SWM_ARG_ID_MOVELEFT (102)
653 #define SWM_ARG_ID_MOVERIGHT (103)
654 #define SWM_ARG_ID_RAISE (105)
655 #define SWM_ARG_ID_LOWER (106)
656 #define SWM_ARG_ID_BAR_TOGGLE (110)
657 #define SWM_ARG_ID_BAR_TOGGLE_WS (111)
663 TAILQ_ENTRY(quirk
) entry
;
664 char *class; /* WM_CLASS:class */
665 char *instance
; /* WM_CLASS:instance */
666 char *name
; /* WM_NAME */
668 regex_t regex_instance
;
671 int ws
; /* Initial workspace. */
672 #define SWM_Q_FLOAT (1<<0) /* float this window */
673 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
674 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
675 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
676 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
677 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
678 #define SWM_Q_NOFOCUSONMAP (1<<6) /* Don't focus on window when mapped. */
679 #define SWM_Q_FOCUSONMAP_SINGLE (1<<7) /* Only focus if single win of type. */
680 #define SWM_Q_OBEYAPPFOCUSREQ (1<<8) /* Focus when applications ask. */
681 #define SWM_Q_IGNOREPID (1<<9) /* Ignore PID when determining ws. */
682 #define SWM_Q_IGNORESPAWNWS (1<<10) /* Ignore _SWM_WS when managing win. */
684 TAILQ_HEAD(quirk_list
, quirk
);
685 struct quirk_list quirks
= TAILQ_HEAD_INITIALIZER(quirks
);
688 * Supported EWMH hints should be added to
689 * both the enum and the ewmh array
695 _NET_CURRENT_DESKTOP
,
696 _NET_DESKTOP_GEOMETRY
,
698 _NET_DESKTOP_VIEWPORT
,
699 _NET_MOVERESIZE_WINDOW
,
700 _NET_NUMBER_OF_DESKTOPS
,
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_RESTACK_WINDOW", XCB_ATOM_NONE
},
745 {"_NET_WM_ACTION_ABOVE", XCB_ATOM_NONE
},
746 {"_NET_WM_ACTION_CLOSE", XCB_ATOM_NONE
},
747 {"_NET_WM_ACTION_FULLSCREEN", XCB_ATOM_NONE
},
748 {"_NET_WM_ACTION_MOVE", XCB_ATOM_NONE
},
749 {"_NET_WM_ACTION_RESIZE", XCB_ATOM_NONE
},
750 {"_NET_WM_ALLOWED_ACTIONS", XCB_ATOM_NONE
},
751 {"_NET_WM_DESKTOP", XCB_ATOM_NONE
},
752 {"_NET_WM_FULL_PLACEMENT", XCB_ATOM_NONE
},
753 {"_NET_WM_NAME", XCB_ATOM_NONE
},
754 {"_NET_WM_STATE", XCB_ATOM_NONE
},
755 {"_NET_WM_STATE_ABOVE", XCB_ATOM_NONE
},
756 {"_NET_WM_STATE_FULLSCREEN", XCB_ATOM_NONE
},
757 {"_NET_WM_STATE_HIDDEN", XCB_ATOM_NONE
},
758 {"_NET_WM_STATE_MAXIMIZED_VERT", XCB_ATOM_NONE
},
759 {"_NET_WM_STATE_MAXIMIZED_HORZ", XCB_ATOM_NONE
},
760 {"_NET_WM_STATE_SKIP_PAGER", XCB_ATOM_NONE
},
761 {"_NET_WM_STATE_SKIP_TASKBAR", XCB_ATOM_NONE
},
762 {"_NET_WM_WINDOW_TYPE", XCB_ATOM_NONE
},
763 {"_NET_WM_WINDOW_TYPE_DIALOG", XCB_ATOM_NONE
},
764 {"_NET_WM_WINDOW_TYPE_DOCK", XCB_ATOM_NONE
},
765 {"_NET_WM_WINDOW_TYPE_NORMAL", XCB_ATOM_NONE
},
766 {"_NET_WM_WINDOW_TYPE_SPLASH", XCB_ATOM_NONE
},
767 {"_NET_WM_WINDOW_TYPE_TOOLBAR", XCB_ATOM_NONE
},
768 {"_NET_WM_WINDOW_TYPE_UTILITY", XCB_ATOM_NONE
},
769 {"_SWM_WM_STATE_MANUAL", XCB_ATOM_NONE
},
772 /* EWMH source type */
774 EWMH_SOURCE_TYPE_NONE
= 0,
775 EWMH_SOURCE_TYPE_NORMAL
= 1,
776 EWMH_SOURCE_TYPE_OTHER
= 2,
783 XC_BOTTOM_LEFT_CORNER
,
784 XC_BOTTOM_RIGHT_CORNER
,
792 char *name
; /* Name used by Xcursor .*/
793 uint8_t cf_char
; /* cursorfont index. */
795 } cursors
[XC_MAX
] = {
796 {"fleur", XC_fleur
, XCB_CURSOR_NONE
},
797 {"left_ptr", XC_left_ptr
, XCB_CURSOR_NONE
},
798 {"bottom_left_corner", XC_bottom_left_corner
, XCB_CURSOR_NONE
},
799 {"bottom_right_corner", XC_bottom_right_corner
, XCB_CURSOR_NONE
},
800 {"sizing", XC_sizing
, XCB_CURSOR_NONE
},
801 {"top_left_corner", XC_top_left_corner
, XCB_CURSOR_NONE
},
802 {"top_right_corner", XC_top_right_corner
, XCB_CURSOR_NONE
},
805 #define SWM_SPAWN_OPTIONAL 0x1
809 TAILQ_ENTRY(spawn_prog
) entry
;
815 TAILQ_HEAD(spawn_list
, spawn_prog
);
816 struct spawn_list spawns
= TAILQ_HEAD_INITIALIZER(spawns
);
818 /* user/key callable function IDs */
934 KF_DUMPWINS
, /* MUST BE LAST */
942 enum keyfuncid funcid
;
945 RB_HEAD(key_tree
, key
);
947 /* function prototypes */
948 void adjust_font(struct ws_win
*);
949 char *argsep(char **);
950 void bar_cleanup(struct swm_region
*);
951 void bar_extra_setup(void);
952 void bar_extra_stop(void);
953 int bar_extra_update(void);
954 void bar_fmt(const char *, char *, struct swm_region
*, size_t);
955 void bar_fmt_expand(char *, size_t);
957 void bar_print(struct swm_region
*, const char *);
958 void bar_print_legacy(struct swm_region
*, const char *);
959 void bar_replace(char *, char *, struct swm_region
*, size_t);
960 void bar_replace_pad(char *, int *, size_t);
961 char *bar_replace_seq(char *, char *, struct swm_region
*, size_t *, size_t);
962 void bar_setup(struct swm_region
*);
963 void bar_toggle(struct swm_region
*, union arg
*);
964 void bar_urgent(char *, size_t);
965 void bar_window_class(char *, size_t, struct swm_region
*);
966 void bar_window_class_instance(char *, size_t, struct swm_region
*);
967 void bar_window_float(char *, size_t, struct swm_region
*);
968 void bar_window_instance(char *, size_t, struct swm_region
*);
969 void bar_window_name(char *, size_t, struct swm_region
*);
970 void bar_window_state(char *, size_t, struct swm_region
*);
971 void bar_workspace_name(char *, size_t, struct swm_region
*);
972 void buttonpress(xcb_button_press_event_t
*);
973 void center_pointer(struct swm_region
*);
974 void check_conn(void);
975 void clear_keys(void);
976 int clear_maximized(struct workspace
*);
977 void clientmessage(xcb_client_message_event_t
*);
978 void client_msg(struct ws_win
*, xcb_atom_t
, xcb_timestamp_t
);
979 int conf_load(const char *, int);
980 void configurenotify(xcb_configure_notify_event_t
*);
981 void configurerequest(xcb_configure_request_event_t
*);
982 void config_win(struct ws_win
*, xcb_configure_request_event_t
*);
983 void constrain_window(struct ws_win
*, struct swm_geometry
*, int *);
984 int count_win(struct workspace
*, bool);
985 void cursors_cleanup(void);
986 void cursors_load(void);
987 void custom_region(const char *);
988 void cyclerg(struct swm_region
*, union arg
*);
989 void cyclews(struct swm_region
*, union arg
*);
990 void cycle_layout(struct swm_region
*, union arg
*);
991 void destroynotify(xcb_destroy_notify_event_t
*);
992 void dumpwins(struct swm_region
*, union arg
*);
994 void enternotify(xcb_enter_notify_event_t
*);
995 void event_drain(uint8_t);
996 void event_error(xcb_generic_error_t
*);
997 void event_handle(xcb_generic_event_t
*);
998 void ewmh_apply_flags(struct ws_win
*, uint32_t);
999 void ewmh_autoquirk(struct ws_win
*);
1000 void ewmh_get_desktop_names(void);
1001 void ewmh_get_wm_state(struct ws_win
*);
1002 void ewmh_update_actions(struct ws_win
*);
1003 void ewmh_update_client_list(void);
1004 void ewmh_update_current_desktop(void);
1005 void ewmh_update_desktop_names(void);
1006 void ewmh_update_desktops(void);
1007 void ewmh_change_wm_state(struct ws_win
*, xcb_atom_t
, long);
1008 void ewmh_update_wm_state(struct ws_win
*);
1009 char *expand_tilde(const char *);
1010 void expose(xcb_expose_event_t
*);
1011 void fake_keypress(struct ws_win
*, xcb_keysym_t
, uint16_t);
1012 struct pid_e
*find_pid(pid_t
);
1013 struct ws_win
*find_unmanaged_window(xcb_window_t
);
1014 struct ws_win
*find_window(xcb_window_t
);
1015 void floating_toggle(struct swm_region
*, union arg
*);
1016 void focus(struct swm_region
*, union arg
*);
1018 void focusin(xcb_focus_in_event_t
*);
1019 void focusout(xcb_focus_out_event_t
*);
1021 void focus_flush(void);
1022 void focus_region(struct swm_region
*);
1023 void focusrg(struct swm_region
*, union arg
*);
1024 void focus_win(struct ws_win
*);
1025 void fontset_init(void);
1026 void free_window(struct ws_win
*);
1027 xcb_atom_t
get_atom_from_string(const char *);
1029 char *get_atom_name(xcb_atom_t
);
1031 struct ws_win
*get_focus_magic(struct ws_win
*);
1032 struct ws_win
*get_focus_prev(struct ws_win
*);
1034 char *get_notify_detail_label(uint8_t);
1035 char *get_notify_mode_label(uint8_t);
1037 struct ws_win
*get_pointer_win(xcb_window_t
);
1038 struct ws_win
*get_region_focus(struct swm_region
*);
1039 int get_region_index(struct swm_region
*);
1040 xcb_screen_t
*get_screen(int);
1041 int get_screen_count(void);
1043 char *get_source_type_label(uint32_t);
1044 char *get_stack_mode_name(uint8_t);
1046 int32_t get_swm_ws(xcb_window_t
);
1047 bool get_urgent(struct ws_win
*);
1048 char *get_win_name(xcb_window_t
);
1049 uint8_t get_win_state(xcb_window_t
);
1050 void get_wm_protocols(struct ws_win
*);
1051 int get_ws_idx(struct ws_win
*);
1052 void grabbuttons(struct ws_win
*);
1053 void grabkeys(void);
1054 void grab_windows(void);
1055 void iconify(struct swm_region
*, union arg
*);
1056 bool isxlfd(char *);
1057 void keypress(xcb_key_press_event_t
*);
1058 int key_cmp(struct key
*, struct key
*);
1059 void key_insert(unsigned int, KeySym
, enum keyfuncid
, const char *);
1060 struct key
*key_lookup(unsigned int, KeySym
);
1061 void key_remove(struct key
*);
1062 void key_replace(struct key
*, unsigned int, KeySym
, enum keyfuncid
,
1064 void kill_bar_extra_atexit(void);
1065 void kill_refs(struct ws_win
*);
1067 void leavenotify(xcb_leave_notify_event_t
*);
1069 void load_float_geom(struct ws_win
*);
1070 void lower_window(struct ws_win
*);
1071 struct ws_win
*manage_window(xcb_window_t
, int, bool);
1072 void map_window(struct ws_win
*);
1073 void mapnotify(xcb_map_notify_event_t
*);
1074 void mappingnotify(xcb_mapping_notify_event_t
*);
1075 void maprequest(xcb_map_request_event_t
*);
1076 void maximize_toggle(struct swm_region
*, union arg
*);
1077 void motionnotify(xcb_motion_notify_event_t
*);
1078 void move(struct ws_win
*, union arg
*);
1079 void move_step(struct swm_region
*, union arg
*);
1080 uint32_t name_to_pixel(int, const char *);
1081 void name_workspace(struct swm_region
*, union arg
*);
1082 void new_region(struct swm_screen
*, int, int, int, int);
1083 int parsekeys(const char *, unsigned int, unsigned int *, KeySym
*);
1084 int parsequirks(const char *, uint32_t *, int *);
1085 int parse_rgb(const char *, uint16_t *, uint16_t *, uint16_t *);
1086 void pressbutton(struct swm_region
*, union arg
*);
1087 void priorws(struct swm_region
*, union arg
*);
1089 void print_win_geom(xcb_window_t
);
1091 void propertynotify(xcb_property_notify_event_t
*);
1092 void quirk_free(struct quirk
*);
1093 void quirk_insert(const char *, const char *, const char *, uint32_t, int);
1094 void quirk_remove(struct quirk
*);
1095 void quirk_replace(struct quirk
*, const char *, const char *, const char *,
1097 void quit(struct swm_region
*, union arg
*);
1098 void raise_toggle(struct swm_region
*, union arg
*);
1099 void raise_window(struct ws_win
*);
1100 void region_containment(struct ws_win
*, struct swm_region
*, int);
1101 struct swm_region
*region_under(struct swm_screen
*, int, int);
1102 void regionize(struct ws_win
*, int, int);
1103 void resize(struct ws_win
*, union arg
*);
1104 void resize_step(struct swm_region
*, union arg
*);
1105 void restart(struct swm_region
*, union arg
*);
1106 struct swm_region
*root_to_region(xcb_window_t
, int);
1107 void screenchange(xcb_randr_screen_change_notify_event_t
*);
1108 void scan_randr(int);
1109 void search_do_resp(void);
1110 void search_resp_name_workspace(const char *, size_t);
1111 void search_resp_search_window(const char *);
1112 void search_resp_search_workspace(const char *);
1113 void search_resp_uniconify(const char *, size_t);
1114 void search_win(struct swm_region
*, union arg
*);
1115 void search_win_cleanup(void);
1116 void search_workspace(struct swm_region
*, union arg
*);
1117 void send_to_rg(struct swm_region
*, union arg
*);
1118 void send_to_ws(struct swm_region
*, union arg
*);
1119 void set_region(struct swm_region
*);
1120 int setautorun(const char *, const char *, int);
1121 int setconfbinding(const char *, const char *, int);
1122 int setconfcolor(const char *, const char *, int);
1123 int setconfmodkey(const char *, const char *, int);
1124 int setconfmousebuttonmove(const char *, const char *, int);
1125 int setconfmousebuttonresize(const char *, const char *, int);
1126 int setconfquirk(const char *, const char *, int);
1127 int setconfregion(const char *, const char *, int);
1128 int setconfspawn(const char *, const char *, int);
1129 int setconfvalue(const char *, const char *, int);
1130 void setkeybinding(unsigned int, KeySym
, enum keyfuncid
, const char *);
1131 int setkeymapping(const char *, const char *, int);
1132 int setlayout(const char *, const char *, int);
1133 void setquirk(const char *, const char *, const char *, uint32_t, int);
1134 void setscreencolor(const char *, int, int);
1135 void setspawn(const char *, const char *, int);
1136 void setup_ewmh(void);
1137 void setup_globals(void);
1138 void setup_keys(void);
1139 void setup_quirks(void);
1140 void setup_screens(void);
1141 void setup_spawn(void);
1142 void set_child_transient(struct ws_win
*, xcb_window_t
*);
1143 void set_win_state(struct ws_win
*, uint8_t);
1144 void shutdown_cleanup(void);
1146 void socket_setnonblock(int);
1147 void sort_windows(struct ws_win_list
*);
1148 void spawn(int, union arg
*, bool);
1149 void spawn_custom(struct swm_region
*, union arg
*, const char *);
1150 int spawn_expand(struct swm_region
*, union arg
*, const char *, char ***);
1151 void spawn_insert(const char *, const char *, int);
1152 struct spawn_prog
*spawn_find(const char *);
1153 void spawn_remove(struct spawn_prog
*);
1154 void spawn_replace(struct spawn_prog
*, const char *, const char *, int);
1155 void spawn_select(struct swm_region
*, union arg
*, const char *, int *);
1156 void stack_config(struct swm_region
*, union arg
*);
1157 void stack_master(struct workspace
*, struct swm_geometry
*, int, bool);
1158 void store_float_geom(struct ws_win
*);
1159 char *strdupsafe(const char *);
1160 void swapwin(struct swm_region
*, union arg
*);
1161 void switchws(struct swm_region
*, union arg
*);
1162 void teardown_ewmh(void);
1163 void unescape_selector(char *);
1164 void unfocus_win(struct ws_win
*);
1165 void uniconify(struct swm_region
*, union arg
*);
1166 void unmanage_window(struct ws_win
*);
1167 void unmapnotify(xcb_unmap_notify_event_t
*);
1168 void unmap_all(void);
1169 void unmap_window(struct ws_win
*);
1170 void updatenumlockmask(void);
1171 void update_floater(struct ws_win
*);
1172 void update_modkey(unsigned int);
1173 unsigned char update_mousebutton(unsigned char, unsigned int);
1174 void update_win_stacking(struct ws_win
*);
1175 void update_window(struct ws_win
*);
1176 void update_window_color(struct ws_win
*);
1177 void update_wm_state(struct ws_win
*win
);
1178 void validate_spawns(void);
1179 int validate_win(struct ws_win
*);
1180 int validate_ws(struct workspace
*);
1181 void version(struct swm_region
*, union arg
*);
1182 void win_to_ws(struct ws_win
*, int, bool);
1183 pid_t
window_get_pid(xcb_window_t
);
1184 void wkill(struct swm_region
*, union arg
*);
1185 void update_ws_stack(struct workspace
*);
1186 void xft_init(struct swm_region
*);
1187 void _add_startup_exception(const char *, va_list);
1188 void add_startup_exception(const char *, ...);
1190 RB_PROTOTYPE(key_tree
, key
, entry
, key_cmp
);
1191 RB_GENERATE(key_tree
, key
, entry
, key_cmp
);
1192 struct key_tree keys
;
1197 xcb_font_t cf
= XCB_NONE
;
1200 for (i
= 0; i
< LENGTH(cursors
); ++i
) {
1201 /* try to load Xcursor first. */
1202 cursors
[i
].cid
= XcursorLibraryLoadCursor(display
,
1205 /* fallback to cursorfont. */
1206 if (cursors
[i
].cid
== XCB_CURSOR_NONE
) {
1207 if (cf
== XCB_NONE
) {
1208 cf
= xcb_generate_id(conn
);
1209 xcb_open_font(conn
, cf
, strlen("cursor"),
1213 cursors
[i
].cid
= xcb_generate_id(conn
);
1214 xcb_create_glyph_cursor(conn
, cursors
[i
].cid
, cf
, cf
,
1215 cursors
[i
].cf_char
, cursors
[i
].cf_char
+ 1, 0, 0, 0,
1216 0xffff, 0xffff, 0xffff);
1222 xcb_close_font(conn
, cf
);
1226 cursors_cleanup(void)
1229 for (i
= 0; i
< LENGTH(cursors
); ++i
)
1230 xcb_free_cursor(conn
, cursors
[i
].cid
);
1234 expand_tilde(const char *s
)
1236 struct passwd
*ppwd
;
1244 errx(1, "expand_tilde: NULL string.");
1247 result
= strdup(sc
);
1253 if ((max
= sysconf(_SC_LOGIN_NAME_MAX
)) == -1)
1254 errx(1, "expand_tilde: sysconf");
1256 if ((user
= calloc(1, max
+ 1)) == NULL
)
1257 errx(1, "expand_tilde: calloc");
1259 for (i
= 0; s
[i
] != '/' && s
[i
] != '\0'; ++i
)
1264 ppwd
= strlen(user
) == 0 ? getpwuid(getuid()) : getpwnam(user
);
1268 result
= strdup(sc
);
1270 if (asprintf(&result
, "%s%s", ppwd
->pw_dir
, s
) == -1)
1274 errx(1, "expand_tilde: failed to allocate memory.");
1280 parse_rgb(const char *rgb
, uint16_t *rr
, uint16_t *gg
, uint16_t *bb
)
1282 unsigned int tmpr
, tmpg
, tmpb
;
1284 if (sscanf(rgb
, "rgb:%x/%x/%x", &tmpr
, &tmpg
, &tmpb
) != 3)
1287 *rr
= RGB_8_TO_16(tmpr
);
1288 *gg
= RGB_8_TO_16(tmpg
);
1289 *bb
= RGB_8_TO_16(tmpb
);
1295 get_screen(int screen
)
1297 const xcb_setup_t
*r
;
1298 xcb_screen_iterator_t iter
;
1300 if ((r
= xcb_get_setup(conn
)) == NULL
) {
1301 DNPRINTF(SWM_D_MISC
, "get_screen: xcb_get_setup\n");
1305 iter
= xcb_setup_roots_iterator(r
);
1306 for (; iter
.rem
; --screen
, xcb_screen_next(&iter
))
1314 get_screen_count(void)
1316 const xcb_setup_t
*r
;
1318 if ((r
= xcb_get_setup(conn
)) == NULL
) {
1319 DNPRINTF(SWM_D_MISC
, "get_screen_count: xcb_get_setup\n");
1323 return xcb_setup_roots_length(r
);
1327 get_region_index(struct swm_region
*r
)
1329 struct swm_region
*rr
;
1335 TAILQ_FOREACH(rr
, &r
->s
->rl
, entry
) {
1350 if (focus_mode
== SWM_FOCUS_DEFAULT
)
1351 event_drain(XCB_ENTER_NOTIFY
);
1357 get_atom_from_string(const char *str
)
1359 xcb_intern_atom_cookie_t c
;
1360 xcb_intern_atom_reply_t
*r
;
1363 c
= xcb_intern_atom(conn
, 0, strlen(str
), str
);
1364 r
= xcb_intern_atom_reply(conn
, c
, NULL
);
1372 return (XCB_ATOM_NONE
);
1376 get_wm_protocols(struct ws_win
*win
) {
1378 xcb_icccm_get_wm_protocols_reply_t wpr
;
1380 if (xcb_icccm_get_wm_protocols_reply(conn
,
1381 xcb_icccm_get_wm_protocols(conn
, win
->id
, a_prot
),
1383 for (i
= 0; i
< (int)wpr
.atoms_len
; i
++) {
1384 if (wpr
.atoms
[i
] == a_takefocus
)
1385 win
->take_focus
= true;
1386 if (wpr
.atoms
[i
] == a_delete
)
1387 win
->can_delete
= true;
1389 xcb_icccm_get_wm_protocols_reply_wipe(&wpr
);
1396 xcb_window_t root
, win
;
1397 int i
, j
, num_screens
;
1399 for (i
= 0; i
< LENGTH(ewmh
); i
++)
1400 ewmh
[i
].atom
= get_atom_from_string(ewmh
[i
].name
);
1402 num_screens
= get_screen_count();
1403 for (i
= 0; i
< num_screens
; i
++) {
1404 root
= screens
[i
].root
;
1406 /* Set up _NET_SUPPORTING_WM_CHECK. */
1407 win
= xcb_generate_id(conn
);
1408 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, win
, root
,
1409 0, 0, 1, 1, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
1410 XCB_COPY_FROM_PARENT
, 0, NULL
);
1412 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, root
,
1413 a_net_wm_check
, XCB_ATOM_WINDOW
, 32, 1, &win
);
1414 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
1415 a_net_wm_check
, XCB_ATOM_WINDOW
, 32, 1, &win
);
1418 * Impersonate LG3D non-reparenting WM, written by Sun, to
1419 * workaround a Java GUI rendering issue.
1421 if (java_workaround
)
1422 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
1423 ewmh
[_NET_WM_NAME
].atom
, a_utf8_string
,
1424 8, strlen("LG3D"), "LG3D");
1426 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
1427 ewmh
[_NET_WM_NAME
].atom
, a_utf8_string
,
1428 8, strlen("spectrwm"), "spectrwm");
1430 /* Report supported atoms */
1431 xcb_delete_property(conn
, root
, a_net_supported
);
1432 for (j
= 0; j
< LENGTH(ewmh
); j
++)
1433 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, root
,
1434 a_net_supported
, XCB_ATOM_ATOM
, 32, 1,
1439 ewmh_update_desktops();
1440 ewmh_get_desktop_names();
1448 xcb_get_property_cookie_t pc
;
1449 xcb_get_property_reply_t
*pr
;
1451 num_screens
= get_screen_count();
1453 for (i
= 0; i
< num_screens
; i
++) {
1454 /* Get the support check window and destroy it */
1455 pc
= xcb_get_property(conn
, 0, screens
[i
].root
, a_net_wm_check
,
1456 XCB_ATOM_WINDOW
, 0, 1);
1457 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
1460 if (pr
->format
== a_net_wm_check
) {
1461 id
= *((xcb_window_t
*)xcb_get_property_value(pr
));
1463 xcb_destroy_window(conn
, id
);
1464 xcb_delete_property(conn
, screens
[i
].root
,
1466 xcb_delete_property(conn
, screens
[i
].root
,
1474 ewmh_autoquirk(struct ws_win
*win
)
1476 xcb_get_property_reply_t
*r
;
1477 xcb_get_property_cookie_t c
;
1481 c
= xcb_get_property(conn
, 0, win
->id
,
1482 ewmh
[_NET_WM_WINDOW_TYPE
].atom
, XCB_ATOM_ATOM
, 0, UINT32_MAX
);
1483 r
= xcb_get_property_reply(conn
, c
, NULL
);
1487 type
= xcb_get_property_value(r
);
1488 n
= xcb_get_property_value_length(r
) / sizeof(xcb_atom_t
);
1490 for (i
= 0; i
< n
; i
++) {
1491 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_NORMAL
].atom
)
1493 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_DOCK
].atom
||
1494 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_TOOLBAR
].atom
||
1495 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_UTILITY
].atom
) {
1496 win
->quirks
= SWM_Q_FLOAT
| SWM_Q_ANYWHERE
;
1499 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_SPLASH
].atom
||
1500 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_DIALOG
].atom
) {
1501 win
->quirks
= SWM_Q_FLOAT
;
1509 ewmh_update_actions(struct ws_win
*win
)
1511 xcb_atom_t actions
[SWM_EWMH_ACTION_COUNT_MAX
];
1517 actions
[n
++] = ewmh
[_NET_WM_ACTION_CLOSE
].atom
;
1520 actions
[n
++] = ewmh
[_NET_WM_ACTION_MOVE
].atom
;
1521 actions
[n
++] = ewmh
[_NET_WM_ACTION_RESIZE
].atom
;
1522 actions
[n
++] = ewmh
[_NET_WM_ACTION_ABOVE
].atom
;
1525 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
1526 ewmh
[_NET_WM_ALLOWED_ACTIONS
].atom
, XCB_ATOM_ATOM
, 32, 1, actions
);
1529 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
1530 #define _NET_WM_STATE_ADD 1 /* add/set property */
1531 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
1534 ewmh_change_wm_state(struct ws_win
*win
, xcb_atom_t state
, long action
)
1541 name
= get_atom_name(state
);
1542 DNPRINTF(SWM_D_PROP
, "ewmh_change_wm_state: win %#x, state: %s, "
1543 "action: %ld\n", WINID(win
), name
, action
);
1549 if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
)
1550 flag
= EWMH_F_FULLSCREEN
;
1551 else if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
)
1552 flag
= EWMH_F_ABOVE
;
1553 else if (state
== ewmh
[_NET_WM_STATE_HIDDEN
].atom
)
1554 flag
= EWMH_F_HIDDEN
;
1555 else if (state
== ewmh
[_NET_WM_STATE_MAXIMIZED_VERT
].atom
||
1556 state
== ewmh
[_NET_WM_STATE_MAXIMIZED_HORZ
].atom
)
1557 flag
= EWMH_F_MAXIMIZED
;
1558 else if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
)
1559 flag
= SWM_F_MANUAL
;
1560 else if (state
== ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
)
1561 flag
= EWMH_F_SKIP_PAGER
;
1562 else if (state
== ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
)
1563 flag
= EWMH_F_SKIP_TASKBAR
;
1565 /* Disallow unfloating transients. */
1566 if (TRANS(win
) && flag
== EWMH_F_ABOVE
)
1569 new_flags
= win
->ewmh_flags
;
1572 case _NET_WM_STATE_REMOVE
:
1575 case _NET_WM_STATE_ADD
:
1578 case _NET_WM_STATE_TOGGLE
:
1583 ewmh_apply_flags(win
, new_flags
);
1586 DNPRINTF(SWM_D_PROP
, "ewmh_change_wm_state: done.\n");
1590 ewmh_apply_flags(struct ws_win
*win
, uint32_t pending
)
1592 struct workspace
*ws
;
1595 changed
= win
->ewmh_flags
^ pending
;
1599 DNPRINTF(SWM_D_PROP
, "ewmh_apply_flags: pending: %d\n", pending
);
1601 win
->ewmh_flags
= pending
;
1604 if (changed
& EWMH_F_HIDDEN
) {
1606 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
1607 ws
->focus_pending
= get_focus_prev(win
);
1612 /* Reload floating geometry in case region changed. */
1614 load_float_geom(win
);
1616 /* The window is no longer iconic, prepare focus. */
1617 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
1618 ws
->focus_pending
= get_focus_magic(win
);
1623 if (changed
& EWMH_F_ABOVE
) {
1624 if (ws
->cur_layout
!= &layouts
[SWM_MAX_STACK
]) {
1626 load_float_geom(win
);
1627 else if (!MAXIMIZED(win
))
1628 store_float_geom(win
);
1630 win
->ewmh_flags
&= ~EWMH_F_MAXIMIZED
;
1631 changed
&= ~EWMH_F_MAXIMIZED
;
1635 win
->ewmh_flags
^= EWMH_F_ABOVE
& pending
;
1639 if (changed
& EWMH_F_MAXIMIZED
) {
1640 /* VERT and/or HORZ changed. */
1642 if (!MAXIMIZED(win
))
1643 load_float_geom(win
);
1645 store_float_geom(win
);
1648 if (MAXIMIZED(win
)) {
1649 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
1650 ws
->cur_layout
!= &layouts
[SWM_MAX_STACK
]) {
1654 ws
->focus_pending
= win
;
1658 update_window_color(win
);
1662 if (changed
& EWMH_F_FULLSCREEN
) {
1663 if (FULLSCREEN(win
)) {
1664 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
1668 ws
->focus_pending
= win
;
1671 load_float_geom(win
);
1674 win
->ewmh_flags
&= ~EWMH_F_MAXIMIZED
;
1678 DNPRINTF(SWM_D_PROP
, "ewmh_apply_flags: done.\n");
1682 ewmh_update_wm_state(struct ws_win
*win
) {
1683 xcb_atom_t vals
[SWM_EWMH_ACTION_COUNT_MAX
];
1687 vals
[n
++] = ewmh
[_NET_WM_STATE_HIDDEN
].atom
;
1688 if (FULLSCREEN(win
))
1689 vals
[n
++] = ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
;
1690 if (MAXIMIZED_VERT(win
))
1691 vals
[n
++] = ewmh
[_NET_WM_STATE_MAXIMIZED_VERT
].atom
;
1692 if (MAXIMIZED_HORZ(win
))
1693 vals
[n
++] = ewmh
[_NET_WM_STATE_MAXIMIZED_HORZ
].atom
;
1694 if (win
->ewmh_flags
& EWMH_F_SKIP_PAGER
)
1695 vals
[n
++] = ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
;
1696 if (win
->ewmh_flags
& EWMH_F_SKIP_TASKBAR
)
1697 vals
[n
++] = ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
;
1698 if (win
->ewmh_flags
& EWMH_F_ABOVE
)
1699 vals
[n
++] = ewmh
[_NET_WM_STATE_ABOVE
].atom
;
1700 if (win
->ewmh_flags
& SWM_F_MANUAL
)
1701 vals
[n
++] = ewmh
[_SWM_WM_STATE_MANUAL
].atom
;
1704 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
1705 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, n
, vals
);
1707 xcb_delete_property(conn
, win
->id
, ewmh
[_NET_WM_STATE
].atom
);
1711 ewmh_get_wm_state(struct ws_win
*win
)
1714 xcb_get_property_cookie_t c
;
1715 xcb_get_property_reply_t
*r
;
1721 win
->ewmh_flags
= 0;
1723 c
= xcb_get_property(conn
, 0, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
1724 XCB_ATOM_ATOM
, 0, UINT32_MAX
);
1725 r
= xcb_get_property_reply(conn
, c
, NULL
);
1729 states
= xcb_get_property_value(r
);
1730 n
= xcb_get_property_value_length(r
) / sizeof(xcb_atom_t
);
1732 for (i
= 0; i
< n
; i
++)
1733 ewmh_change_wm_state(win
, states
[i
], _NET_WM_STATE_ADD
);
1741 dumpwins(struct swm_region
*r
, union arg
*args
)
1745 xcb_get_window_attributes_cookie_t c
;
1746 xcb_get_window_attributes_reply_t
*wa
;
1748 /* suppress unused warning since var is needed */
1751 if (r
->ws
== NULL
) {
1752 DPRINTF("dumpwins: invalid workspace\n");
1756 DPRINTF("=== managed window list ws %02d ===\n", r
->ws
->idx
);
1757 TAILQ_FOREACH(w
, &r
->ws
->winlist
, entry
) {
1758 state
= get_win_state(w
->id
);
1759 c
= xcb_get_window_attributes(conn
, w
->id
);
1760 wa
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
1762 DPRINTF("win %#x, map_state: %d, state: %u, "
1763 "transient: %#x\n", w
->id
, wa
->map_state
,
1764 state
, w
->transient
);
1767 DPRINTF("win %#x, failed xcb_get_window_attributes\n",
1771 DPRINTF("=== stacking order (top down) === \n");
1772 TAILQ_FOREACH(w
, &r
->ws
->stack
, stack_entry
) {
1773 DPRINTF("win %#x, fs: %s, maximized: %s, above: %s, "
1774 "iconic: %s\n", w
->id
, YESNO(FULLSCREEN(w
)),
1775 YESNO(MAXIMIZED(w
)), YESNO(ABOVE(w
)), YESNO(ICONIC(w
)));
1778 DPRINTF("===== unmanaged window list =====\n");
1779 TAILQ_FOREACH(w
, &r
->ws
->unmanagedlist
, entry
) {
1780 state
= get_win_state(w
->id
);
1781 c
= xcb_get_window_attributes(conn
, w
->id
);
1782 wa
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
1784 DPRINTF("win %#x, map_state: %d, state: %u, "
1785 "transient: %#x\n", w
->id
, wa
->map_state
,
1786 state
, w
->transient
);
1789 DPRINTF("win %#x, failed xcb_get_window_attributes\n",
1793 DPRINTF("=================================\n");
1797 dumpwins(struct swm_region
*r
, union arg
*s
)
1802 #endif /* SWM_DEBUG */
1807 int saved_errno
, status
;
1810 saved_errno
= errno
;
1814 while ((pid
= waitpid(WAIT_ANY
, &status
, WNOHANG
)) != 0) {
1819 if (errno
!= ECHILD
)
1820 warn("sighdlr: waitpid");
1821 #endif /* SWM_DEBUG */
1824 if (pid
== searchpid
)
1828 if (WIFEXITED(status
)) {
1829 if (WEXITSTATUS(status
) != 0)
1830 warnx("sighdlr: child exit status: %d",
1831 WEXITSTATUS(status
));
1833 warnx("sighdlr: child is terminated "
1835 #endif /* SWM_DEBUG */
1849 errno
= saved_errno
;
1855 struct pid_e
*p
= NULL
;
1857 DNPRINTF(SWM_D_MISC
, "find_pid: %d\n", pid
);
1862 TAILQ_FOREACH(p
, &pidlist
, entry
) {
1871 name_to_pixel(int sidx
, const char *colorname
)
1873 uint32_t result
= 0;
1874 char cname
[32] = "#";
1875 xcb_screen_t
*screen
;
1876 xcb_colormap_t cmap
;
1877 xcb_alloc_color_reply_t
*cr
;
1878 xcb_alloc_named_color_reply_t
*nr
;
1879 uint16_t rr
, gg
, bb
;
1881 screen
= get_screen(sidx
);
1882 cmap
= screen
->default_colormap
;
1884 /* color is in format rgb://rr/gg/bb */
1885 if (strncmp(colorname
, "rgb:", 4) == 0) {
1886 if (parse_rgb(colorname
, &rr
, &gg
, &bb
) == -1)
1887 warnx("could not parse rgb %s", colorname
);
1889 cr
= xcb_alloc_color_reply(conn
,
1890 xcb_alloc_color(conn
, cmap
, rr
, gg
, bb
),
1896 warnx("color '%s' not found", colorname
);
1899 nr
= xcb_alloc_named_color_reply(conn
,
1900 xcb_alloc_named_color(conn
, cmap
, strlen(colorname
),
1903 strlcat(cname
, colorname
+ 2, sizeof cname
- 1);
1904 nr
= xcb_alloc_named_color_reply(conn
,
1905 xcb_alloc_named_color(conn
, cmap
, strlen(cname
),
1912 warnx("color '%s' not found", colorname
);
1919 setscreencolor(const char *val
, int i
, int c
)
1921 if (i
< 0 || i
>= get_screen_count())
1924 screens
[i
].c
[c
].pixel
= name_to_pixel(i
, val
);
1925 free(screens
[i
].c
[c
].name
);
1926 if ((screens
[i
].c
[c
].name
= strdup(val
)) == NULL
)
1931 fancy_stacker(struct workspace
*ws
)
1933 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1934 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1935 snprintf(ws
->stacker
, sizeof ws
->stacker
,
1936 ws
->l_state
.vertical_flip
? "[%d>%d]" : "[%d|%d]",
1937 ws
->l_state
.vertical_mwin
, ws
->l_state
.vertical_stacks
);
1938 else if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1939 snprintf(ws
->stacker
, sizeof ws
->stacker
,
1940 ws
->l_state
.horizontal_flip
? "[%dv%d]" : "[%d-%d]",
1941 ws
->l_state
.horizontal_mwin
, ws
->l_state
.horizontal_stacks
);
1945 plain_stacker(struct workspace
*ws
)
1947 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1948 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1949 strlcpy(ws
->stacker
, ws
->l_state
.vertical_flip
? "[>]" : "[|]",
1950 sizeof ws
->stacker
);
1951 else if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1952 strlcpy(ws
->stacker
, ws
->l_state
.horizontal_flip
? "[v]" : "[-]",
1953 sizeof ws
->stacker
);
1957 custom_region(const char *val
)
1959 unsigned int x
, y
, w
, h
;
1960 int sidx
, num_screens
;
1961 xcb_screen_t
*screen
;
1963 num_screens
= get_screen_count();
1964 if (sscanf(val
, "screen[%d]:%ux%u+%u+%u", &sidx
, &w
, &h
, &x
, &y
) != 5)
1965 errx(1, "invalid custom region, "
1966 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1967 if (sidx
< 1 || sidx
> num_screens
)
1968 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1972 if ((screen
= get_screen(sidx
)) == NULL
)
1973 errx(1, "ERROR: can't get screen %d.", sidx
);
1976 errx(1, "region %ux%u+%u+%u too small", w
, h
, x
, y
);
1978 if (x
> screen
->width_in_pixels
||
1979 y
> screen
->height_in_pixels
||
1980 w
+ x
> screen
->width_in_pixels
||
1981 h
+ y
> screen
->height_in_pixels
) {
1982 warnx("ignoring region %ux%u+%u+%u - not within screen "
1983 "boundaries (%ux%u)", w
, h
, x
, y
,
1984 screen
->width_in_pixels
, screen
->height_in_pixels
);
1988 new_region(&screens
[sidx
], x
, y
, w
, h
);
1992 socket_setnonblock(int fd
)
1996 if ((flags
= fcntl(fd
, F_GETFL
, 0)) == -1)
1997 err(1, "fcntl F_GETFL");
1998 flags
|= O_NONBLOCK
;
1999 if ((flags
= fcntl(fd
, F_SETFL
, flags
)) == -1)
2000 err(1, "fcntl F_SETFL");
2004 bar_print_legacy(struct swm_region
*r
, const char *s
)
2006 xcb_rectangle_t rect
;
2011 XRectangle ibox
, lbox
;
2015 XmbTextExtents(bar_fs
, s
, len
, &ibox
, &lbox
);
2017 switch (bar_justify
) {
2018 case SWM_BAR_JUSTIFY_LEFT
:
2021 case SWM_BAR_JUSTIFY_CENTER
:
2022 x
= (WIDTH(r
) - lbox
.width
) / 2;
2024 case SWM_BAR_JUSTIFY_RIGHT
:
2025 x
= WIDTH(r
) - lbox
.width
- SWM_BAR_OFFSET
;
2029 if (x
< SWM_BAR_OFFSET
)
2034 rect
.width
= WIDTH(r
->bar
);
2035 rect
.height
= HEIGHT(r
->bar
);
2037 /* clear back buffer */
2038 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
2039 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
2040 xcb_poly_fill_rectangle(conn
, r
->bar
->buffer
, r
->s
->bar_gc
, 1, &rect
);
2042 /* draw back buffer */
2043 gcvd
.graphics_exposures
= 0;
2044 draw
= XCreateGC(display
, r
->bar
->buffer
, GCGraphicsExposures
, &gcvd
);
2045 XSetForeground(display
, draw
, r
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
);
2046 DRAWSTRING(display
, r
->bar
->buffer
, bar_fs
, draw
,
2047 x
, (bar_fs_extents
->max_logical_extent
.height
- lbox
.height
) / 2 -
2049 XFreeGC(display
, draw
);
2052 xcb_copy_area(conn
, r
->bar
->buffer
, r
->bar
->id
, r
->s
->bar_gc
, 0, 0,
2053 0, 0, WIDTH(r
->bar
), HEIGHT(r
->bar
));
2057 bar_print(struct swm_region
*r
, const char *s
)
2060 xcb_rectangle_t rect
;
2068 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
, &info
);
2070 switch (bar_justify
) {
2071 case SWM_BAR_JUSTIFY_LEFT
:
2074 case SWM_BAR_JUSTIFY_CENTER
:
2075 x
= (WIDTH(r
) - info
.width
) / 2;
2077 case SWM_BAR_JUSTIFY_RIGHT
:
2078 x
= WIDTH(r
) - info
.width
- SWM_BAR_OFFSET
;
2082 if (x
< SWM_BAR_OFFSET
)
2087 rect
.width
= WIDTH(r
->bar
);
2088 rect
.height
= HEIGHT(r
->bar
);
2090 /* clear back buffer */
2091 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
2092 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
2093 xcb_poly_fill_rectangle(conn
, r
->bar
->buffer
, r
->s
->bar_gc
, 1, &rect
);
2095 /* draw back buffer */
2096 draw
= XftDrawCreate(display
, r
->bar
->buffer
,
2097 DefaultVisual(display
, r
->s
->idx
),
2098 DefaultColormap(display
, r
->s
->idx
));
2100 XftDrawStringUtf8(draw
, &bar_font_color
, bar_font
, x
,
2101 (HEIGHT(r
->bar
) + bar_font
->height
) / 2 - bar_font
->descent
,
2104 XftDrawDestroy(draw
);
2107 xcb_copy_area(conn
, r
->bar
->buffer
, r
->bar
->id
, r
->s
->bar_gc
, 0, 0,
2108 0, 0, WIDTH(r
->bar
), HEIGHT(r
->bar
));
2112 bar_extra_stop(void)
2116 bzero(bar_pipe
, sizeof bar_pipe
);
2119 kill(bar_pid
, SIGTERM
);
2122 strlcpy(bar_ext
, "", sizeof bar_ext
);
2127 bar_window_class(char *s
, size_t sz
, struct swm_region
*r
)
2129 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2131 if (r
->ws
->focus
->ch
.class_name
!= NULL
)
2132 strlcat(s
, r
->ws
->focus
->ch
.class_name
, sz
);
2136 bar_window_instance(char *s
, size_t sz
, struct swm_region
*r
)
2138 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2140 if (r
->ws
->focus
->ch
.instance_name
!= NULL
)
2141 strlcat(s
, r
->ws
->focus
->ch
.instance_name
, sz
);
2145 bar_window_class_instance(char *s
, size_t sz
, struct swm_region
*r
)
2147 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2150 bar_window_class(s
, sz
, r
);
2151 strlcat(s
, ":", sz
);
2152 bar_window_instance(s
, sz
, r
);
2156 bar_window_state(char *s
, size_t sz
, struct swm_region
*r
)
2158 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2160 if (MAXIMIZED(r
->ws
->focus
))
2161 strlcat(s
, "(m)", sz
);
2162 else if (ABOVE(r
->ws
->focus
))
2163 strlcat(s
, "(f)", sz
);
2167 bar_window_name(char *s
, size_t sz
, struct swm_region
*r
)
2171 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2174 title
= get_win_name(r
->ws
->focus
->id
);
2175 strlcat(s
, title
, sz
);
2180 get_urgent(struct ws_win
*win
)
2182 xcb_icccm_wm_hints_t hints
;
2183 xcb_get_property_cookie_t c
;
2184 bool urgent
= false;
2187 c
= xcb_icccm_get_wm_hints(conn
, win
->id
);
2188 if (xcb_icccm_get_wm_hints_reply(conn
, c
, &hints
, NULL
))
2189 urgent
= xcb_icccm_wm_hints_get_urgency(&hints
);
2196 bar_urgent(char *s
, size_t sz
)
2199 int i
, j
, num_screens
;
2200 bool urgent
[SWM_WS_MAX
];
2203 for (i
= 0; i
< workspace_limit
; i
++)
2206 num_screens
= get_screen_count();
2207 for (i
= 0; i
< num_screens
; i
++)
2208 for (j
= 0; j
< workspace_limit
; j
++)
2209 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2210 if (get_urgent(win
))
2213 for (i
= 0; i
< workspace_limit
; i
++) {
2215 snprintf(b
, sizeof b
, "%d ", i
+ 1);
2217 } else if (!urgent_collapse
) {
2218 strlcat(s
, "- ", sz
);
2224 bar_workspace_name(char *s
, size_t sz
, struct swm_region
*r
)
2226 if (r
== NULL
|| r
->ws
== NULL
)
2228 if (r
->ws
->name
!= NULL
)
2229 strlcat(s
, r
->ws
->name
, sz
);
2232 /* build the default bar format according to the defined enabled options */
2234 bar_fmt(const char *fmtexp
, char *fmtnew
, struct swm_region
*r
, size_t sz
)
2238 /* if format provided, just copy the buffers */
2239 if (bar_format
!= NULL
) {
2240 strlcpy(fmtnew
, fmtexp
, sz
);
2244 /* reset the output buffer */
2247 strlcat(fmtnew
, "+N:+I ", sz
);
2249 strlcat(fmtnew
, "+S", sz
);
2250 strlcat(fmtnew
, " ", sz
);
2252 /* only show the workspace name if there's actually one */
2253 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->name
!= NULL
)
2254 strlcat(fmtnew
, "<+D>", sz
);
2256 /* If enabled, only show the iconic count if there are iconic wins. */
2257 if (iconic_enabled
&& r
!= NULL
&& r
->ws
!= NULL
)
2258 TAILQ_FOREACH(w
, &r
->ws
->winlist
, entry
)
2260 strlcat(fmtnew
, "{+M}", sz
);
2264 strlcat(fmtnew
, "+3<", sz
);
2266 if (clock_enabled
) {
2267 strlcat(fmtnew
, fmtexp
, sz
);
2268 strlcat(fmtnew
, "+4<", sz
);
2271 /* bar_urgent already adds the space before the last asterisk */
2273 strlcat(fmtnew
, "* +U*+4<", sz
);
2275 if (window_class_enabled
) {
2276 strlcat(fmtnew
, "+C", sz
);
2277 if (!window_instance_enabled
)
2278 strlcat(fmtnew
, "+4<", sz
);
2281 /* checks needed by the colon and floating strlcat(3) calls below */
2282 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->focus
!= NULL
) {
2283 if (window_instance_enabled
) {
2284 if (window_class_enabled
)
2285 strlcat(fmtnew
, ":", sz
);
2286 strlcat(fmtnew
, "+T+4<", sz
);
2288 if (window_name_enabled
) {
2289 if (ABOVE(r
->ws
->focus
) || MAXIMIZED(r
->ws
->focus
))
2290 strlcat(fmtnew
, "+F ", sz
);
2291 strlcat(fmtnew
, "+64W ", sz
);
2295 /* finally add the action script output and the version */
2296 strlcat(fmtnew
, "+4<+A+4<+V", sz
);
2300 bar_replace_pad(char *tmp
, int *limit
, size_t sz
)
2302 /* special case; no limit given, pad one space, instead */
2303 if (*limit
== (int)sz
- 1)
2305 snprintf(tmp
, sz
, "%*s", *limit
, " ");
2308 /* replaces the bar format character sequences (like in tmux(1)) */
2310 bar_replace_seq(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t *offrep
,
2315 char tmp
[SWM_BAR_MAX
];
2316 int limit
, size
, count
;
2319 /* reset strlcat(3) buffer */
2322 /* get number, if any */
2325 if (sscanf(fmt
, "%d%n", &limit
, &size
) != 1)
2326 limit
= sizeof tmp
- 1;
2327 if (limit
<= 0 || limit
>= (int)sizeof tmp
)
2328 limit
= sizeof tmp
- 1;
2330 /* there is nothing to replace (ie EOL) */
2337 bar_replace_pad(tmp
, &limit
, sizeof tmp
);
2340 snprintf(tmp
, sizeof tmp
, "%s", bar_ext
);
2343 bar_window_class(tmp
, sizeof tmp
, r
);
2346 bar_workspace_name(tmp
, sizeof tmp
, r
);
2349 bar_window_state(tmp
, sizeof tmp
, r
);
2352 snprintf(tmp
, sizeof tmp
, "%d", r
->ws
->idx
+ 1);
2356 TAILQ_FOREACH(w
, &r
->ws
->winlist
, entry
)
2360 snprintf(tmp
, sizeof tmp
, "%d", count
);
2363 snprintf(tmp
, sizeof tmp
, "%d", r
->s
->idx
+ 1);
2366 bar_window_class_instance(tmp
, sizeof tmp
, r
);
2369 snprintf(tmp
, sizeof tmp
, "%s", r
->ws
->stacker
);
2372 bar_window_instance(tmp
, sizeof tmp
, r
);
2375 bar_urgent(tmp
, sizeof tmp
);
2378 snprintf(tmp
, sizeof tmp
, "%s", bar_vertext
);
2381 bar_window_name(tmp
, sizeof tmp
, r
);
2384 /* unknown character sequence; copy as-is */
2385 snprintf(tmp
, sizeof tmp
, "+%c", *fmt
);
2391 if ((int)len
< limit
)
2393 while (limit
-- > 0) {
2394 if (*offrep
>= sz
- 1)
2396 fmtrep
[(*offrep
)++] = *ptr
++;
2404 bar_replace(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t sz
)
2409 while (*fmt
!= '\0') {
2411 /* skip ordinary characters */
2414 fmtrep
[off
++] = *fmt
++;
2418 /* character sequence found; replace it */
2419 fmt
= bar_replace_seq(fmt
, fmtrep
, r
, &off
, sz
);
2428 bar_fmt_expand(char *fmtexp
, size_t sz
)
2435 /* start by grabbing the current time and date */
2437 localtime_r(&tmt
, &tm
);
2439 /* figure out what to expand */
2440 if (bar_format
!= NULL
)
2442 else if (bar_format
== NULL
&& clock_enabled
)
2444 /* if nothing to expand bail out */
2450 /* copy as-is, just in case the format shouldn't be expanded below */
2451 strlcpy(fmtexp
, fmt
, sz
);
2452 /* finally pass the string through strftime(3) */
2453 #ifndef SWM_DENY_CLOCK_FORMAT
2454 if ((len
= strftime(fmtexp
, sz
, fmt
, &tm
)) == 0)
2455 warnx("format too long");
2460 /* Redraws the bar; need to follow with xcb_flush() or focus_flush(). */
2464 char fmtexp
[SWM_BAR_MAX
], fmtnew
[SWM_BAR_MAX
];
2465 char fmtrep
[SWM_BAR_MAX
];
2467 struct swm_region
*r
;
2469 /* expand the format by first passing it through strftime(3) */
2470 bar_fmt_expand(fmtexp
, sizeof fmtexp
);
2472 num_screens
= get_screen_count();
2473 for (i
= 0; i
< num_screens
; i
++) {
2474 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
2478 if (bar_enabled
&& r
->ws
->bar_enabled
)
2479 xcb_map_window(conn
, r
->bar
->id
);
2481 xcb_unmap_window(conn
, r
->bar
->id
);
2485 if (startup_exception
)
2486 snprintf(fmtrep
, sizeof fmtrep
, "total "
2487 "exceptions: %d, first exception: %s",
2491 bar_fmt(fmtexp
, fmtnew
, r
, sizeof fmtnew
);
2492 bar_replace(fmtnew
, fmtrep
, r
, sizeof fmtrep
);
2494 if (bar_font_legacy
)
2495 bar_print_legacy(r
, fmtrep
);
2497 bar_print(r
, fmtrep
);
2503 * Reads external script output; call when stdin is readable.
2504 * Returns 1 if bar_ext was updated; otherwise 0.
2507 bar_extra_update(void)
2510 char b
[SWM_BAR_MAX
];
2511 bool changed
= false;
2516 while (fgets(b
, sizeof(b
), stdin
) != NULL
) {
2519 if (b
[len
- 1] == '\n') {
2520 /* Remove newline. */
2523 /* "Clear" bar_ext. */
2526 /* Flush buffered output. */
2527 strlcpy(bar_ext
, bar_ext_buf
, sizeof(bar_ext
));
2528 bar_ext_buf
[0] = '\0';
2530 /* Append new output to bar. */
2531 strlcat(bar_ext
, b
, sizeof(bar_ext
));
2535 /* Buffer output. */
2536 strlcat(bar_ext_buf
, b
, sizeof(bar_ext_buf
));
2541 if (errno
!= EAGAIN
) {
2542 warn("bar_action failed");
2551 bar_toggle(struct swm_region
*r
, union arg
*args
)
2553 struct swm_region
*tmpr
;
2556 /* suppress unused warnings since vars are needed */
2560 DNPRINTF(SWM_D_BAR
, "bar_toggle\n");
2563 case SWM_ARG_ID_BAR_TOGGLE_WS
:
2564 /* Only change if master switch is enabled. */
2566 r
->ws
->bar_enabled
= !r
->ws
->bar_enabled
;
2568 bar_enabled
= r
->ws
->bar_enabled
= true;
2570 case SWM_ARG_ID_BAR_TOGGLE
:
2571 bar_enabled
= !bar_enabled
;
2575 /* update bars as necessary */
2576 num_screens
= get_screen_count();
2577 for (i
= 0; i
< num_screens
; i
++)
2578 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
2580 if (bar_enabled
&& tmpr
->ws
->bar_enabled
)
2581 xcb_map_window(conn
, tmpr
->bar
->id
);
2583 xcb_unmap_window(conn
, tmpr
->bar
->id
);
2588 /* must be after stack */
2595 bar_extra_setup(void)
2597 /* do this here because the conf file is in memory */
2598 if (!bar_extra
&& bar_argv
[0]) {
2599 /* launch external status app */
2601 if (pipe(bar_pipe
) == -1)
2602 err(1, "pipe error");
2603 socket_setnonblock(bar_pipe
[0]);
2604 socket_setnonblock(bar_pipe
[1]); /* XXX hmmm, really? */
2606 /* Set stdin to read from the pipe. */
2607 if (dup2(bar_pipe
[0], STDIN_FILENO
) == -1)
2610 /* Set stdout to write to the pipe. */
2611 if (dup2(bar_pipe
[1], STDOUT_FILENO
) == -1)
2614 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
2615 err(1, "could not disable SIGPIPE");
2616 switch (bar_pid
= fork()) {
2618 err(1, "cannot fork");
2622 execvp(bar_argv
[0], bar_argv
);
2623 err(1, "%s external app failed", bar_argv
[0]);
2625 default: /* parent */
2630 atexit(kill_bar_extra_atexit
);
2635 kill_bar_extra_atexit(void)
2638 kill(bar_pid
, SIGTERM
);
2646 while ((s
= index(s
, '-'))) {
2651 return (count
== 14);
2657 char *default_string
;
2658 char **missing_charsets
;
2659 int num_missing_charsets
= 0;
2663 XFreeFontSet(display
, bar_fs
);
2667 DNPRINTF(SWM_D_INIT
, "fontset_init: loading bar_fonts: %s\n", bar_fonts
);
2669 bar_fs
= XCreateFontSet(display
, bar_fonts
, &missing_charsets
,
2670 &num_missing_charsets
, &default_string
);
2672 if (num_missing_charsets
> 0) {
2673 warnx("Unable to load charset(s):");
2675 for (i
= 0; i
< num_missing_charsets
; ++i
)
2676 warnx("%s", missing_charsets
[i
]);
2678 XFreeStringList(missing_charsets
);
2680 if (strcmp(default_string
, ""))
2681 warnx("Glyphs from those sets will be replaced "
2682 "by '%s'.", default_string
);
2684 warnx("Glyphs from those sets won't be drawn.");
2688 errx(1, "Error creating font set structure.");
2690 bar_fs_extents
= XExtentsOfFontSet(bar_fs
);
2692 bar_height
= bar_fs_extents
->max_logical_extent
.height
+
2693 2 * bar_border_width
;
2700 xft_init(struct swm_region
*r
)
2702 char *font
, *d
, *search
;
2705 if (bar_font
== NULL
) {
2706 if ((d
= strdup(bar_fonts
)) == NULL
)
2707 errx(1, "insufficient memory.");
2709 while ((font
= strsep(&search
, ",")) != NULL
) {
2713 DNPRINTF(SWM_D_INIT
, "xft_init: try font %s\n", font
);
2716 bar_font
= XftFontOpenXlfd(display
, r
->s
->idx
,
2719 bar_font
= XftFontOpenName(display
, r
->s
->idx
,
2723 if (bar_font
== NULL
) {
2724 warnx("unable to load font %s", font
);
2727 DNPRINTF(SWM_D_INIT
, "successfully opened "
2735 if (bar_font
== NULL
)
2736 errx(1, "unable to open a font");
2738 PIXEL_TO_XRENDERCOLOR(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
, color
);
2740 if (!XftColorAllocValue(display
, DefaultVisual(display
, r
->s
->idx
),
2741 DefaultColormap(display
, r
->s
->idx
), &color
, &bar_font_color
))
2742 warn("Xft error: unable to allocate color.");
2744 PIXEL_TO_XRENDERCOLOR(r
->s
->c
[SWM_S_COLOR_BAR
].pixel
, color
);
2746 if (!XftColorAllocValue(display
, DefaultVisual(display
, r
->s
->idx
),
2747 DefaultColormap(display
, r
->s
->idx
), &color
, &search_font_color
))
2748 warn("Xft error: unable to allocate color.");
2750 bar_height
= bar_font
->height
+ 2 * bar_border_width
;
2757 bar_setup(struct swm_region
*r
)
2759 xcb_screen_t
*screen
;
2762 DNPRINTF(SWM_D_BAR
, "bar_setup: screen %d.\n",
2765 if ((screen
= get_screen(r
->s
->idx
)) == NULL
)
2766 errx(1, "ERROR: can't get screen %d.", r
->s
->idx
);
2771 if ((r
->bar
= calloc(1, sizeof(struct swm_bar
))) == NULL
)
2772 err(1, "bar_setup: calloc: failed to allocate memory.");
2774 if (bar_font_legacy
)
2780 Y(r
->bar
) = bar_at_bottom
? (Y(r
) + HEIGHT(r
) - bar_height
) : Y(r
);
2781 WIDTH(r
->bar
) = WIDTH(r
) - 2 * bar_border_width
;
2782 HEIGHT(r
->bar
) = bar_height
- 2 * bar_border_width
;
2784 /* Assume region is unfocused when we create the bar. */
2785 r
->bar
->id
= xcb_generate_id(conn
);
2786 wa
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
2787 wa
[1] = r
->s
->c
[SWM_S_COLOR_BAR_BORDER_UNFOCUS
].pixel
;
2788 wa
[2] = XCB_EVENT_MASK_EXPOSURE
| XCB_EVENT_MASK_POINTER_MOTION
|
2789 XCB_EVENT_MASK_POINTER_MOTION_HINT
;
2791 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, r
->bar
->id
, r
->s
->root
,
2792 X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
), HEIGHT(r
->bar
),
2793 bar_border_width
, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
2794 XCB_COPY_FROM_PARENT
, XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
2795 | XCB_CW_EVENT_MASK
, wa
);
2797 /* Stack bar window above region window to start. */
2799 wa
[1] = XCB_STACK_MODE_ABOVE
;
2801 xcb_configure_window(conn
, r
->bar
->id
, XCB_CONFIG_WINDOW_SIBLING
|
2802 XCB_CONFIG_WINDOW_STACK_MODE
, wa
);
2804 r
->bar
->buffer
= xcb_generate_id(conn
);
2805 xcb_create_pixmap(conn
, screen
->root_depth
, r
->bar
->buffer
, r
->bar
->id
,
2806 WIDTH(r
->bar
), HEIGHT(r
->bar
));
2809 xcb_randr_select_input(conn
, r
->bar
->id
,
2810 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE
);
2813 xcb_map_window(conn
, r
->bar
->id
);
2815 DNPRINTF(SWM_D_BAR
, "bar_setup: win %#x, (x,y) w x h: (%d,%d) "
2816 "%d x %d\n", WINID(r
->bar
), X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
),
2823 bar_cleanup(struct swm_region
*r
)
2827 xcb_destroy_window(conn
, r
->bar
->id
);
2828 xcb_free_pixmap(conn
, r
->bar
->buffer
);
2834 set_win_state(struct ws_win
*win
, uint8_t state
)
2836 uint16_t data
[2] = { state
, XCB_ATOM_NONE
};
2838 DNPRINTF(SWM_D_EVENT
, "set_win_state: win %#x, state: %u\n",
2844 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
, a_state
,
2845 a_state
, 32, 2, data
);
2849 get_win_state(xcb_window_t w
)
2851 xcb_get_property_reply_t
*r
;
2852 xcb_get_property_cookie_t c
;
2853 uint32_t result
= 0;
2855 c
= xcb_get_property(conn
, 0, w
, a_state
, a_state
, 0L, 2L);
2856 r
= xcb_get_property_reply(conn
, c
, NULL
);
2858 if (r
->type
== a_state
&& r
->format
== 32 && r
->length
== 2)
2859 result
= *((uint32_t *)xcb_get_property_value(r
));
2863 DNPRINTF(SWM_D_MISC
, "get_win_state property: win %#x state %u\n", w
,
2869 version(struct swm_region
*r
, union arg
*args
)
2871 /* suppress unused warnings since vars are needed */
2875 bar_version
= !bar_version
;
2877 snprintf(bar_vertext
, sizeof bar_vertext
,
2878 "Version: %s Build: %s", SPECTRWM_VERSION
, buildstr
);
2880 strlcpy(bar_vertext
, "", sizeof bar_vertext
);
2887 client_msg(struct ws_win
*win
, xcb_atom_t a
, xcb_timestamp_t t
)
2889 xcb_client_message_event_t ev
;
2897 name
= get_atom_name(a
);
2898 DNPRINTF(SWM_D_EVENT
, "client_msg: win %#x, atom: %s(%u), "
2900 win
->id
, name
, a
, t
);
2904 bzero(&ev
, sizeof ev
);
2905 ev
.response_type
= XCB_CLIENT_MESSAGE
;
2906 ev
.window
= win
->id
;
2909 ev
.data
.data32
[0] = a
;
2910 ev
.data
.data32
[1] = t
;
2912 xcb_send_event(conn
, 0, win
->id
,
2913 XCB_EVENT_MASK_NO_EVENT
, (const char *)&ev
);
2916 /* synthetic response to a ConfigureRequest when not making a change */
2918 config_win(struct ws_win
*win
, xcb_configure_request_event_t
*ev
)
2920 xcb_configure_notify_event_t ce
;
2925 /* send notification of unchanged state. */
2926 bzero(&ce
, sizeof(ce
));
2927 ce
.response_type
= XCB_CONFIGURE_NOTIFY
;
2930 ce
.width
= WIDTH(win
);
2931 ce
.height
= HEIGHT(win
);
2932 ce
.override_redirect
= 0;
2937 ce
.window
= win
->id
;
2938 ce
.border_width
= BORDER(win
);
2939 ce
.above_sibling
= XCB_WINDOW_NONE
;
2942 ce
.event
= ev
->window
;
2943 ce
.window
= ev
->window
;
2945 /* make response appear more WM_SIZE_HINTS-compliant */
2946 if (win
->sh
.flags
) {
2947 DNPRINTF(SWM_D_MISC
, "config_win: hints: win %#x,"
2948 " sh.flags: %u, min: %d x %d, max: %d x %d, inc: "
2949 "%d x %d\n", win
->id
, win
->sh
.flags
, SH_MIN_W(win
),
2950 SH_MIN_H(win
), SH_MAX_W(win
), SH_MAX_H(win
),
2951 SH_INC_W(win
), SH_INC_H(win
));
2956 /* the hint may be set... to 0! */
2957 if (SH_MIN_W(win
) > 0 && ce
.width
< SH_MIN_W(win
))
2958 ce
.width
= SH_MIN_W(win
);
2959 if (SH_MIN_H(win
) > 0 && ce
.height
< SH_MIN_H(win
))
2960 ce
.height
= SH_MIN_H(win
);
2965 /* may also be advertized as 0 */
2966 if (SH_MAX_W(win
) > 0 && ce
.width
> SH_MAX_W(win
))
2967 ce
.width
= SH_MAX_W(win
);
2968 if (SH_MAX_H(win
) > 0 && ce
.height
> SH_MAX_H(win
))
2969 ce
.height
= SH_MAX_H(win
);
2972 /* resize increment. */
2974 if (SH_INC_W(win
) > 1 && ce
.width
> SH_INC_W(win
))
2975 ce
.width
-= (ce
.width
- SH_MIN_W(win
)) %
2977 if (SH_INC_H(win
) > 1 && ce
.height
> SH_INC_H(win
))
2978 ce
.height
-= (ce
.height
- SH_MIN_H(win
)) %
2982 /* adjust x and y for requested border_width. */
2983 ce
.x
+= BORDER(win
) - ev
->border_width
;
2984 ce
.y
+= BORDER(win
) - ev
->border_width
;
2985 ce
.border_width
= ev
->border_width
;
2986 ce
.above_sibling
= ev
->sibling
;
2989 DNPRINTF(SWM_D_MISC
, "config_win: ewmh: %s, win %#x, (x,y) w x h: "
2990 "(%d,%d) %d x %d, border: %d\n", YESNO(ev
== NULL
), win
->id
, ce
.x
,
2991 ce
.y
, ce
.width
, ce
.height
, ce
.border_width
);
2993 xcb_send_event(conn
, 0, win
->id
, XCB_EVENT_MASK_STRUCTURE_NOTIFY
,
2998 count_win(struct workspace
*ws
, bool count_transient
)
3003 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3004 if (!count_transient
&& FLOATING(win
))
3010 DNPRINTF(SWM_D_MISC
, "count_win: %d\n", count
);
3016 quit(struct swm_region
*r
, union arg
*args
)
3018 /* suppress unused warnings since vars are needed */
3022 DNPRINTF(SWM_D_MISC
, "quit\n");
3027 lower_window(struct ws_win
*win
)
3029 struct ws_win
*target
= NULL
;
3030 struct workspace
*ws
;
3037 DNPRINTF(SWM_D_EVENT
, "lower_window: win %#x\n", win
->id
);
3039 TAILQ_FOREACH(target
, &ws
->stack
, stack_entry
) {
3040 if (target
== win
|| ICONIC(target
))
3042 if (ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
3045 if (win
->transient
== target
->transient
)
3047 if (win
->transient
== target
->id
)
3050 if (FULLSCREEN(target
))
3052 if (FULLSCREEN(win
))
3054 if (MAXIMIZED(target
))
3058 if (ABOVE(target
) || TRANS(target
))
3060 if (ABOVE(win
) || TRANS(win
))
3064 /* Change stack position. */
3065 TAILQ_REMOVE(&ws
->stack
, win
, stack_entry
);
3067 TAILQ_INSERT_BEFORE(target
, win
, stack_entry
);
3069 TAILQ_INSERT_TAIL(&ws
->stack
, win
, stack_entry
);
3071 update_win_stacking(win
);
3074 if (swm_debug
& SWM_D_STACK
) {
3075 DPRINTF("=== stacking order (top down) === \n");
3076 TAILQ_FOREACH(target
, &ws
->stack
, stack_entry
) {
3077 DPRINTF("win %#x, fs: %s, maximized: %s, above: %s, "
3078 "iconic: %s\n", target
->id
, YESNO(FULLSCREEN(target
)),
3079 YESNO(MAXIMIZED(target
)), YESNO(ABOVE(target
)),
3080 YESNO(ICONIC(target
)));
3084 DNPRINTF(SWM_D_EVENT
, "lower_window: done\n");
3088 raise_window(struct ws_win
*win
)
3090 struct ws_win
*target
= NULL
;
3091 struct workspace
*ws
;
3097 DNPRINTF(SWM_D_EVENT
, "raise_window: win %#x\n", win
->id
);
3099 TAILQ_FOREACH(target
, &ws
->stack
, stack_entry
) {
3100 if (target
== win
|| ICONIC(target
))
3102 if (ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
3104 if (TRANS(win
) && (win
->transient
== target
->transient
||
3105 win
->transient
== target
->id
))
3107 if (FULLSCREEN(win
))
3109 if (FULLSCREEN(target
))
3113 if (MAXIMIZED(target
))
3115 if (ABOVE(win
) || TRANS(win
) ||
3116 (win
->ws
->focus
== win
&& ws
->always_raise
))
3118 if (!ABOVE(target
) && !TRANS(target
))
3122 TAILQ_REMOVE(&ws
->stack
, win
, stack_entry
);
3124 TAILQ_INSERT_BEFORE(target
, win
, stack_entry
);
3126 TAILQ_INSERT_TAIL(&ws
->stack
, win
, stack_entry
);
3128 update_win_stacking(win
);
3131 if (swm_debug
& SWM_D_STACK
) {
3132 DPRINTF("=== stacking order (top down) === \n");
3133 TAILQ_FOREACH(target
, &ws
->stack
, stack_entry
) {
3134 DPRINTF("win %#x, fs: %s, maximized: %s, above: %s, "
3135 "iconic: %s\n", target
->id
, YESNO(FULLSCREEN(target
)),
3136 YESNO(MAXIMIZED(target
)), YESNO(ABOVE(target
)),
3137 YESNO(ICONIC(target
)));
3141 DNPRINTF(SWM_D_EVENT
, "raise_window: done\n");
3145 update_win_stacking(struct ws_win
*win
)
3147 struct ws_win
*sibling
;
3148 struct swm_region
*r
;
3151 if (win
== NULL
|| (r
= win
->ws
->r
) == NULL
)
3154 sibling
= TAILQ_NEXT(win
, stack_entry
);
3155 if (sibling
!= NULL
&& (FLOATING(win
) == FLOATING(sibling
) ||
3156 (win
->ws
->always_raise
&& win
->ws
->focus
== win
)))
3157 val
[0] = sibling
->id
;
3158 else if (FLOATING(win
) || (win
->ws
->always_raise
&&
3159 win
->ws
->focus
== win
))
3160 val
[0] = r
->bar
->id
;
3164 DNPRINTF(SWM_D_EVENT
, "update_win_stacking: %#x, sibling %#x\n",
3167 val
[1] = XCB_STACK_MODE_ABOVE
;
3169 xcb_configure_window(conn
, win
->id
, XCB_CONFIG_WINDOW_SIBLING
|
3170 XCB_CONFIG_WINDOW_STACK_MODE
, val
);
3174 map_window(struct ws_win
*win
)
3179 DNPRINTF(SWM_D_EVENT
, "map_window: win %#x, mapped: %s\n",
3180 win
->id
, YESNO(win
->mapped
));
3185 xcb_map_window(conn
, win
->id
);
3186 set_win_state(win
, XCB_ICCCM_WM_STATE_NORMAL
);
3191 unmap_window(struct ws_win
*win
)
3196 DNPRINTF(SWM_D_EVENT
, "unmap_window: win %#x, mapped: %s\n", win
->id
,
3197 YESNO(win
->mapped
));
3202 xcb_unmap_window(conn
, win
->id
);
3203 set_win_state(win
, XCB_ICCCM_WM_STATE_ICONIC
);
3204 win
->mapped
= false;
3211 int i
, j
, num_screens
;
3213 num_screens
= get_screen_count();
3214 for (i
= 0; i
< num_screens
; i
++)
3215 for (j
= 0; j
< workspace_limit
; j
++)
3216 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
3221 fake_keypress(struct ws_win
*win
, xcb_keysym_t keysym
, uint16_t modifiers
)
3223 xcb_key_press_event_t event
;
3224 xcb_keycode_t
*keycode
;
3229 keycode
= xcb_key_symbols_get_keycode(syms
, keysym
);
3231 DNPRINTF(SWM_D_MISC
, "fake_keypress: win %#x, keycode %u\n",
3234 bzero(&event
, sizeof(event
));
3235 event
.event
= win
->id
;
3236 event
.root
= win
->s
->root
;
3237 event
.child
= XCB_WINDOW_NONE
;
3238 event
.time
= XCB_CURRENT_TIME
;
3239 event
.event_x
= X(win
);
3240 event
.event_y
= Y(win
);
3243 event
.same_screen
= 1;
3244 event
.detail
= *keycode
;
3245 event
.state
= modifiers
;
3247 event
.response_type
= XCB_KEY_PRESS
;
3248 xcb_send_event(conn
, 1, win
->id
,
3249 XCB_EVENT_MASK_KEY_PRESS
, (const char *)&event
);
3251 event
.response_type
= XCB_KEY_RELEASE
;
3252 xcb_send_event(conn
, 1, win
->id
,
3253 XCB_EVENT_MASK_KEY_RELEASE
, (const char *)&event
);
3259 restart(struct swm_region
*r
, union arg
*args
)
3261 /* suppress unused warning since var is needed */
3265 DNPRINTF(SWM_D_MISC
, "restart: %s\n", start_argv
[0]);
3269 execvp(start_argv
[0], start_argv
);
3270 warn("execvp failed");
3275 get_pointer_win(xcb_window_t root
)
3277 struct ws_win
*win
= NULL
;
3278 xcb_query_pointer_reply_t
*r
;
3280 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: root: %#x.\n", root
);
3282 r
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, root
), NULL
);
3284 win
= find_window(r
->child
);
3286 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: %#x.\n",
3289 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: none.\n");
3298 center_pointer(struct swm_region
*r
)
3302 if (!warp_pointer
|| r
== NULL
)
3307 DNPRINTF(SWM_D_EVENT
, "center_pointer: win %#x.\n", WINID(win
));
3309 if (win
&& win
->mapped
)
3310 xcb_warp_pointer(conn
, XCB_NONE
, win
->id
, 0, 0, 0, 0,
3311 WIDTH(win
) / 2, HEIGHT(win
) / 2);
3313 xcb_warp_pointer(conn
, XCB_NONE
, r
->id
, 0, 0, 0, 0,
3314 WIDTH(r
) / 2, HEIGHT(r
) / 2);
3318 root_to_region(xcb_window_t root
, int check
)
3321 struct swm_region
*r
= NULL
;
3323 xcb_query_pointer_reply_t
*qpr
;
3324 xcb_get_input_focus_reply_t
*gifr
;
3326 DNPRINTF(SWM_D_MISC
, "root_to_region: win %#x\n", root
);
3328 num_screens
= get_screen_count();
3329 for (i
= 0; i
< num_screens
; i
++)
3330 if (screens
[i
].root
== root
)
3333 if (check
& SWM_CK_REGION
)
3334 r
= screens
[i
].r_focus
;
3336 if (r
== NULL
&& check
& SWM_CK_FOCUS
) {
3337 /* Try to find an actively focused window */
3338 gifr
= xcb_get_input_focus_reply(conn
,
3339 xcb_get_input_focus(conn
), NULL
);
3341 cfw
= find_window(gifr
->focus
);
3342 if (cfw
&& cfw
->ws
->r
)
3349 if (r
== NULL
&& check
& SWM_CK_POINTER
) {
3350 /* No region with an active focus; try to use pointer. */
3351 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
,
3352 screens
[i
].root
), NULL
);
3354 DNPRINTF(SWM_D_MISC
, "root_to_region: pointer: "
3355 "(%d,%d)\n", qpr
->root_x
, qpr
->root_y
);
3356 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3357 if (X(r
) <= qpr
->root_x
&&
3358 qpr
->root_x
< MAX_X(r
) &&
3359 Y(r
) <= qpr
->root_y
&&
3360 qpr
->root_y
< MAX_Y(r
))
3367 if (r
== NULL
&& check
& SWM_CK_FALLBACK
)
3368 r
= TAILQ_FIRST(&screens
[i
].rl
);
3370 DNPRINTF(SWM_D_MISC
, "root_to_region: idx: %d\n", get_region_index(r
));
3376 find_unmanaged_window(xcb_window_t id
)
3379 int i
, j
, num_screens
;
3381 num_screens
= get_screen_count();
3382 for (i
= 0; i
< num_screens
; i
++)
3383 for (j
= 0; j
< workspace_limit
; j
++)
3384 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].unmanagedlist
,
3392 find_window(xcb_window_t id
)
3395 int i
, j
, num_screens
;
3396 xcb_query_tree_reply_t
*r
;
3398 num_screens
= get_screen_count();
3399 for (i
= 0; i
< num_screens
; i
++)
3400 for (j
= 0; j
< workspace_limit
; j
++)
3401 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
3405 r
= xcb_query_tree_reply(conn
, xcb_query_tree(conn
, id
), NULL
);
3409 /* if we were looking for the parent return that window instead */
3410 if (r
->parent
== 0 || r
->root
== r
->parent
) {
3415 /* look for parent */
3416 for (i
= 0; i
< num_screens
; i
++)
3417 for (j
= 0; j
< workspace_limit
; j
++)
3418 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
3419 if (r
->parent
== win
->id
) {
3429 spawn(int ws_idx
, union arg
*args
, bool close_fd
)
3434 DNPRINTF(SWM_D_MISC
, "spawn: %s\n", args
->argv
[0]);
3436 close(xcb_get_file_descriptor(conn
));
3438 setenv("LD_PRELOAD", SWM_LIB
, 1);
3440 if (asprintf(&ret
, "%d", ws_idx
) == -1) {
3441 warn("spawn: asprintf SWM_WS");
3444 setenv("_SWM_WS", ret
, 1);
3448 if (asprintf(&ret
, "%d", getpid()) == -1) {
3449 warn("spawn: asprintf _SWM_PID");
3452 setenv("_SWM_PID", ret
, 1);
3456 if (setsid() == -1) {
3457 warn("spawn: setsid");
3463 * close stdin and stdout to prevent interaction between apps
3464 * and the baraction script
3465 * leave stderr open to record errors
3467 if ((fd
= open(_PATH_DEVNULL
, O_RDWR
, 0)) == -1) {
3468 warn("spawn: open");
3471 dup2(fd
, STDIN_FILENO
);
3472 dup2(fd
, STDOUT_FILENO
);
3477 execvp(args
->argv
[0], args
->argv
);
3479 warn("spawn: execvp");
3484 kill_refs(struct ws_win
*win
)
3486 int i
, x
, num_screens
;
3487 struct swm_region
*r
;
3488 struct workspace
*ws
;
3493 num_screens
= get_screen_count();
3494 for (i
= 0; i
< num_screens
; i
++)
3495 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3496 for (x
= 0; x
< workspace_limit
; x
++) {
3498 if (win
== ws
->focus
)
3500 if (win
== ws
->focus_prev
)
3501 ws
->focus_prev
= NULL
;
3506 validate_win(struct ws_win
*testwin
)
3509 struct workspace
*ws
;
3510 struct swm_region
*r
;
3511 int i
, x
, num_screens
;
3513 if (testwin
== NULL
)
3516 num_screens
= get_screen_count();
3517 for (i
= 0; i
< num_screens
; i
++)
3518 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3519 for (x
= 0; x
< workspace_limit
; x
++) {
3521 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
3529 validate_ws(struct workspace
*testws
)
3531 struct swm_region
*r
;
3532 struct workspace
*ws
;
3533 int i
, x
, num_screens
;
3535 /* validate all ws */
3536 num_screens
= get_screen_count();
3537 for (i
= 0; i
< num_screens
; i
++)
3538 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3539 for (x
= 0; x
< workspace_limit
; x
++) {
3548 unfocus_win(struct ws_win
*win
)
3550 xcb_window_t none
= XCB_WINDOW_NONE
;
3552 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: win %#x\n", WINID(win
));
3557 if (win
->ws
== NULL
) {
3558 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: NULL ws.\n");
3562 if (validate_ws(win
->ws
)) {
3563 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: invalid ws.\n");
3567 if (win
->ws
->r
== NULL
) {
3568 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: NULL region.\n");
3572 if (validate_win(win
)) {
3573 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: invalid win.\n");
3578 if (win
->ws
->focus
== win
) {
3579 win
->ws
->focus
= NULL
;
3580 win
->ws
->focus_prev
= win
;
3583 if (validate_win(win
->ws
->focus
)) {
3584 kill_refs(win
->ws
->focus
);
3585 win
->ws
->focus
= NULL
;
3588 if (validate_win(win
->ws
->focus_prev
)) {
3589 kill_refs(win
->ws
->focus_prev
);
3590 win
->ws
->focus_prev
= NULL
;
3593 update_window_color(win
);
3595 /* Raise window to "top unfocused position." */
3596 if (win
->ws
->always_raise
)
3599 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
3600 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1, &none
);
3602 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: done.\n");
3606 focus_win(struct ws_win
*win
)
3608 struct ws_win
*cfw
= NULL
, *parent
= NULL
, *w
, *tmpw
;
3609 struct workspace
*ws
;
3610 xcb_get_input_focus_reply_t
*gifr
;
3612 DNPRINTF(SWM_D_FOCUS
, "focus_win: win %#x\n", WINID(win
));
3614 if (win
== NULL
|| win
->ws
== NULL
|| !win
->mapped
)
3619 if (validate_ws(ws
))
3622 if (validate_win(win
)) {
3627 gifr
= xcb_get_input_focus_reply(conn
, xcb_get_input_focus(conn
), NULL
);
3629 cfw
= find_window(gifr
->focus
);
3630 if (cfw
!= NULL
&& cfw
!= win
) {
3631 if (cfw
->ws
!= ws
&& cfw
->ws
->r
!= NULL
) {
3632 /* Change border to unfocused color. */
3633 xcb_change_window_attributes(conn
, cfw
->id
,
3634 XCB_CW_BORDER_PIXEL
,
3635 &cfw
->s
->c
[(MAXIMIZED(cfw
) ?
3636 SWM_S_COLOR_UNFOCUS_MAXIMIZED
:
3637 SWM_S_COLOR_UNFOCUS
)].pixel
);
3645 if (ws
->focus
!= win
) {
3646 if (ws
->focus
&& ws
->focus
!= cfw
)
3647 unfocus_win(ws
->focus
);
3651 /* If this window directs focus to a child window, then clear. */
3652 if (win
->focus_child
)
3653 win
->focus_child
= NULL
;
3655 /* If transient, adjust parent's focus child for focus_magic. */
3657 parent
= find_window(win
->transient
);
3658 if (parent
&& parent
->focus_child
!= win
)
3659 parent
->focus_child
= win
;
3662 if (cfw
!= win
&& ws
->r
!= NULL
) {
3663 /* Set input focus if no input hint, or indicated by hint. */
3664 if (!(win
->hints
.flags
& XCB_ICCCM_WM_HINT_INPUT
) ||
3665 (win
->hints
.flags
& XCB_ICCCM_WM_HINT_INPUT
&&
3667 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
,
3668 win
->id
, last_event_time
);
3670 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
,
3671 ws
->r
->id
, XCB_CURRENT_TIME
);
3673 /* Tell app it can adjust focus to a specific window. */
3674 if (win
->take_focus
) {
3675 /* java is special; always tell parent */
3676 if (TRANS(win
) && win
->java
)
3677 client_msg(parent
, a_takefocus
,
3680 client_msg(win
, a_takefocus
, last_event_time
);
3683 if (ws
->cur_layout
->flags
& SWM_L_MAPONFOCUS
||
3685 /* If a parent exists, map it first. */
3687 raise_window(parent
);
3690 /* Map siblings next. */
3691 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
,
3693 if (w
!= win
&& !ICONIC(w
) &&
3694 w
->transient
== parent
->id
) {
3700 /* Map focused window. */
3704 /* Stack any children of focus window. */
3705 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
, tmpw
)
3706 if (w
->transient
== win
->id
&& !ICONIC(w
)) {
3710 } else if (tile_gap
< 0 && !ABOVE(win
)) {
3712 * Windows overlap in the layout.
3713 * Raise focused win above all tiled wins.
3721 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
3722 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1,
3727 /* Update window border even if workspace is hidden. */
3728 update_window_color(win
);
3733 DNPRINTF(SWM_D_FOCUS
, "focus_win: done.\n");
3736 /* If a child window should have focus instead, return it. */
3738 get_focus_magic(struct ws_win
*win
)
3740 struct ws_win
*parent
= NULL
;
3741 struct ws_win
*child
= NULL
;
3743 DNPRINTF(SWM_D_FOCUS
, "get_focus_magic: win %#x\n", WINID(win
));
3748 parent
= find_window(win
->transient
);
3750 /* If parent prefers focus elsewhere, then try to do so. */
3751 if (parent
&& (child
= parent
->focus_child
)) {
3752 if (validate_win(child
) == 0 && child
->mapped
)
3755 parent
->focus_child
= NULL
;
3759 /* If this window prefers focus elsewhere, then try to do so. */
3760 if ((child
= win
->focus_child
)) {
3761 if (validate_win(child
) == 0 && child
->mapped
)
3764 win
->focus_child
= NULL
;
3771 event_drain(uint8_t rt
)
3773 xcb_generic_event_t
*evt
;
3775 /* ensure all pending requests have been processed before filtering. */
3777 while ((evt
= xcb_poll_for_event(conn
))) {
3778 if (XCB_EVENT_RESPONSE_TYPE(evt
) != rt
)
3786 set_region(struct swm_region
*r
)
3788 struct swm_region
*rf
;
3795 /* Unfocus old region bar. */
3800 xcb_change_window_attributes(conn
, rf
->bar
->id
,
3801 XCB_CW_BORDER_PIXEL
,
3802 &r
->s
->c
[SWM_S_COLOR_BAR_BORDER_UNFOCUS
].pixel
);
3805 if (rf
!= NULL
&& rf
!= r
&& (X(rf
) != X(r
) || Y(rf
) != Y(r
) ||
3806 WIDTH(rf
) != WIDTH(r
) || HEIGHT(rf
) != HEIGHT(r
))) {
3807 /* Set _NET_DESKTOP_GEOMETRY. */
3809 vals
[1] = HEIGHT(r
);
3810 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, r
->s
->root
,
3811 ewmh
[_NET_DESKTOP_GEOMETRY
].atom
, XCB_ATOM_CARDINAL
, 32, 2,
3815 /* Set region bar border to focus_color. */
3816 xcb_change_window_attributes(conn
, r
->bar
->id
,
3817 XCB_CW_BORDER_PIXEL
, &r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].pixel
);
3821 ewmh_update_current_desktop();
3825 focus_region(struct swm_region
*r
)
3828 struct swm_region
*old_r
;
3833 old_r
= r
->s
->r_focus
;
3836 nfw
= get_region_focus(r
);
3840 /* New region is empty; need to manually unfocus win. */
3842 unfocus_win(old_r
->ws
->focus
);
3844 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
, r
->id
,
3847 /* Clear bar since empty. */
3853 switchws(struct swm_region
*r
, union arg
*args
)
3855 struct swm_region
*this_r
, *other_r
;
3857 struct workspace
*new_ws
, *old_ws
;
3858 xcb_window_t none
= XCB_WINDOW_NONE
;
3859 int wsid
= args
->id
;
3860 bool unmap_old
= false;
3865 if (wsid
>= workspace_limit
)
3869 old_ws
= this_r
->ws
;
3870 new_ws
= &this_r
->s
->ws
[wsid
];
3872 DNPRINTF(SWM_D_WS
, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
3873 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), old_ws
->idx
, wsid
);
3875 if (new_ws
== NULL
|| old_ws
== NULL
)
3877 if (new_ws
== old_ws
)
3880 if ((win
= old_ws
->focus
) != NULL
) {
3881 update_window_color(win
);
3883 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
3884 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1,
3888 other_r
= new_ws
->r
;
3889 if (other_r
== NULL
) {
3890 /* the other workspace is hidden, hide this one */
3894 /* the other ws is visible in another region, exchange them */
3895 other_r
->ws_prior
= new_ws
;
3896 other_r
->ws
= old_ws
;
3897 old_ws
->r
= other_r
;
3899 this_r
->ws_prior
= old_ws
;
3900 this_r
->ws
= new_ws
;
3903 /* Set focus_pending before stacking, if needed. */
3904 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& (!new_ws
->focus_pending
||
3905 validate_win(new_ws
->focus_pending
))) {
3906 new_ws
->focus_pending
= get_region_focus(new_ws
->r
);
3907 new_ws
->focus
= new_ws
->focus_prev
;
3908 new_ws
->focus_prev
= NULL
;
3911 new_ws
->state
= SWM_WS_STATE_MAPPING
;
3914 /* unmap old windows */
3916 TAILQ_FOREACH(win
, &old_ws
->winlist
, entry
)
3918 old_ws
->state
= SWM_WS_STATE_HIDDEN
;
3921 /* if workspaces were swapped, then don't wait to set focus */
3922 if (old_ws
->r
&& focus_mode
!= SWM_FOCUS_FOLLOW
) {
3923 if (new_ws
->focus_pending
) {
3924 focus_win(new_ws
->focus_pending
);
3925 new_ws
->focus_pending
= NULL
;
3929 /* Clear bar and set focus on region input win if new ws is empty. */
3930 if (new_ws
->focus_pending
== NULL
&& new_ws
->focus
== NULL
) {
3931 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
, r
->id
,
3936 ewmh_update_current_desktop();
3940 new_ws
->state
= SWM_WS_STATE_MAPPED
;
3942 DNPRINTF(SWM_D_WS
, "switchws: done.\n");
3946 cyclews(struct swm_region
*r
, union arg
*args
)
3949 struct swm_screen
*s
= r
->s
;
3950 bool cycle_all
= false, mv
= false;
3952 DNPRINTF(SWM_D_WS
, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3953 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
3959 case SWM_ARG_ID_CYCLEWS_MOVE_UP
:
3962 case SWM_ARG_ID_CYCLEWS_UP_ALL
:
3965 case SWM_ARG_ID_CYCLEWS_UP
:
3966 a
.id
= (a
.id
< workspace_limit
- 1) ? a
.id
+ 1 : 0;
3968 case SWM_ARG_ID_CYCLEWS_MOVE_DOWN
:
3971 case SWM_ARG_ID_CYCLEWS_DOWN_ALL
:
3974 case SWM_ARG_ID_CYCLEWS_DOWN
:
3975 a
.id
= (a
.id
> 0) ? a
.id
- 1 : workspace_limit
- 1;
3982 (!cycle_empty
&& TAILQ_EMPTY(&s
->ws
[a
.id
].winlist
)))
3984 if (!cycle_visible
&& s
->ws
[a
.id
].r
!= NULL
)
3991 } while (a
.id
!= r
->ws
->idx
);
3993 DNPRINTF(SWM_D_FOCUS
, "cyclews: done\n");
3997 priorws(struct swm_region
*r
, union arg
*args
)
4003 DNPRINTF(SWM_D_WS
, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
4004 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
4006 if (r
->ws_prior
== NULL
)
4009 a
.id
= r
->ws_prior
->idx
;
4011 DNPRINTF(SWM_D_FOCUS
, "priorws: done\n");
4015 focusrg(struct swm_region
*r
, union arg
*args
)
4017 int ridx
= args
->id
, i
, num_screens
;
4018 struct swm_region
*rr
= NULL
;
4020 num_screens
= get_screen_count();
4021 /* do nothing if we don't have more than one screen */
4022 if (!(num_screens
> 1 || outputs
> 1))
4025 DNPRINTF(SWM_D_FOCUS
, "focusrg: id: %d\n", ridx
);
4027 rr
= TAILQ_FIRST(&r
->s
->rl
);
4028 for (i
= 0; i
< ridx
&& rr
!= NULL
; ++i
)
4029 rr
= TAILQ_NEXT(rr
, entry
);
4037 DNPRINTF(SWM_D_FOCUS
, "focusrg: done\n");
4041 cyclerg(struct swm_region
*r
, union arg
*args
)
4043 struct swm_region
*rr
= NULL
;
4046 num_screens
= get_screen_count();
4047 /* do nothing if we don't have more than one screen */
4048 if (!(num_screens
> 1 || outputs
> 1))
4052 DNPRINTF(SWM_D_FOCUS
, "cyclerg: id: %d, region: %d\n", args
->id
, i
);
4055 case SWM_ARG_ID_CYCLERG_UP
:
4056 rr
= TAILQ_NEXT(r
, entry
);
4058 rr
= TAILQ_FIRST(&screens
[i
].rl
);
4060 case SWM_ARG_ID_CYCLERG_DOWN
:
4061 rr
= TAILQ_PREV(r
, swm_region_list
, entry
);
4063 rr
= TAILQ_LAST(&screens
[i
].rl
, swm_region_list
);
4074 DNPRINTF(SWM_D_FOCUS
, "cyclerg: done\n");
4077 /* Sorts transients after parent. */
4079 sort_windows(struct ws_win_list
*wl
)
4081 struct ws_win
*win
, *parent
, *nxt
;
4086 for (win
= TAILQ_FIRST(wl
); win
!= TAILQ_END(wl
); win
= nxt
) {
4087 nxt
= TAILQ_NEXT(win
, entry
);
4089 parent
= find_window(win
->transient
);
4090 if (parent
== NULL
) {
4091 warnx("not possible bug");
4094 TAILQ_REMOVE(wl
, win
, entry
);
4095 TAILQ_INSERT_AFTER(wl
, parent
, win
, entry
);
4101 swapwin(struct swm_region
*r
, union arg
*args
)
4103 struct ws_win
*target
, *source
;
4104 struct ws_win
*cur_focus
;
4105 struct ws_win_list
*wl
;
4107 DNPRINTF(SWM_D_WS
, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
4108 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
4110 cur_focus
= r
->ws
->focus
;
4111 if (cur_focus
== NULL
|| FULLSCREEN(cur_focus
))
4114 /* Adjust stacking in floating layer. */
4115 if (ABOVE(cur_focus
)) {
4117 case SWM_ARG_ID_SWAPPREV
:
4118 target
= TAILQ_PREV(cur_focus
, ws_win_stack
,
4120 if (target
!= NULL
&& FLOATING(target
)) {
4121 TAILQ_REMOVE(&cur_focus
->ws
->stack
, cur_focus
,
4123 TAILQ_INSERT_BEFORE(target
, cur_focus
,
4125 update_win_stacking(cur_focus
);
4129 case SWM_ARG_ID_SWAPNEXT
:
4130 target
= TAILQ_NEXT(cur_focus
, stack_entry
);
4131 if (target
!= NULL
&& FLOATING(target
)) {
4132 TAILQ_REMOVE(&cur_focus
->ws
->stack
, cur_focus
,
4134 TAILQ_INSERT_AFTER(&cur_focus
->ws
->stack
,
4135 target
, cur_focus
, stack_entry
);
4136 update_win_stacking(cur_focus
);
4144 if (r
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
4147 clear_maximized(r
->ws
);
4150 wl
= &source
->ws
->winlist
;
4153 case SWM_ARG_ID_SWAPPREV
:
4155 source
= find_window(source
->transient
);
4156 target
= TAILQ_PREV(source
, ws_win_list
, entry
);
4157 if (target
&& target
->transient
)
4158 target
= find_window(target
->transient
);
4159 TAILQ_REMOVE(wl
, source
, entry
);
4161 TAILQ_INSERT_TAIL(wl
, source
, entry
);
4163 TAILQ_INSERT_BEFORE(target
, source
, entry
);
4165 case SWM_ARG_ID_SWAPNEXT
:
4166 target
= TAILQ_NEXT(source
, entry
);
4167 /* move the parent and let the sort handle the move */
4169 source
= find_window(source
->transient
);
4170 TAILQ_REMOVE(wl
, source
, entry
);
4172 TAILQ_INSERT_HEAD(wl
, source
, entry
);
4174 TAILQ_INSERT_AFTER(wl
, target
, source
, entry
);
4176 case SWM_ARG_ID_SWAPMAIN
:
4177 target
= TAILQ_FIRST(wl
);
4178 if (target
== source
) {
4179 if (source
->ws
->focus_prev
!= NULL
&&
4180 source
->ws
->focus_prev
!= target
)
4181 source
= source
->ws
->focus_prev
;
4185 if (target
== NULL
|| source
== NULL
)
4187 source
->ws
->focus_prev
= target
;
4188 TAILQ_REMOVE(wl
, target
, entry
);
4189 TAILQ_INSERT_BEFORE(source
, target
, entry
);
4190 TAILQ_REMOVE(wl
, source
, entry
);
4191 TAILQ_INSERT_HEAD(wl
, source
, entry
);
4193 case SWM_ARG_ID_MOVELAST
:
4194 TAILQ_REMOVE(wl
, source
, entry
);
4195 TAILQ_INSERT_TAIL(wl
, source
, entry
);
4198 DNPRINTF(SWM_D_MOVE
, "swapwin: invalid id: %d\n", args
->id
);
4203 ewmh_update_client_list();
4209 DNPRINTF(SWM_D_MOVE
, "swapwin: done\n");
4213 get_focus_prev(struct ws_win
*win
)
4215 struct ws_win
*winfocus
= NULL
;
4216 struct ws_win
*cur_focus
= NULL
;
4217 struct ws_win_list
*wl
= NULL
;
4218 struct workspace
*ws
= NULL
;
4220 if (!(win
&& win
->ws
))
4225 cur_focus
= ws
->focus
;
4227 DNPRINTF(SWM_D_FOCUS
, "get_focus_prev: win %#x, cur_focus: %#x, "
4228 "focus_prev: %#x\n", WINID(win
), WINID(cur_focus
),
4229 WINID(ws
->focus_prev
));
4231 /* pickle, just focus on whatever */
4232 if (cur_focus
== NULL
) {
4233 /* use prev_focus if valid */
4234 if (ws
->focus_prev
&& find_window(ws
->focus_prev
->id
))
4235 winfocus
= ws
->focus_prev
;
4239 /* if transient focus on parent */
4240 if (TRANS(cur_focus
)) {
4241 winfocus
= find_window(cur_focus
->transient
);
4245 /* if in max_stack try harder */
4246 if ((win
->quirks
& SWM_Q_FOCUSPREV
) ||
4247 (ws
->cur_layout
->flags
& SWM_L_FOCUSPREV
)) {
4248 if (cur_focus
!= ws
->focus_prev
)
4249 winfocus
= ws
->focus_prev
;
4251 winfocus
= TAILQ_PREV(win
, ws_win_list
, entry
);
4256 DNPRINTF(SWM_D_FOCUS
, "get_focus_prev: focus_close: %d\n", focus_close
);
4258 if (winfocus
== NULL
|| winfocus
== win
) {
4259 switch (focus_close
) {
4260 case SWM_STACK_BOTTOM
:
4261 TAILQ_FOREACH(winfocus
, wl
, entry
)
4262 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4266 TAILQ_FOREACH_REVERSE(winfocus
, wl
, ws_win_list
, entry
)
4267 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4270 case SWM_STACK_ABOVE
:
4271 winfocus
= TAILQ_NEXT(cur_focus
, entry
);
4272 while (winfocus
&& ICONIC(winfocus
))
4273 winfocus
= TAILQ_NEXT(winfocus
, entry
);
4275 if (winfocus
== NULL
) {
4276 if (focus_close_wrap
) {
4277 TAILQ_FOREACH(winfocus
, wl
, entry
)
4278 if (!ICONIC(winfocus
) &&
4279 winfocus
!= cur_focus
)
4282 TAILQ_FOREACH_REVERSE(winfocus
, wl
,
4284 if (!ICONIC(winfocus
) &&
4285 winfocus
!= cur_focus
)
4290 case SWM_STACK_BELOW
:
4291 winfocus
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
4292 while (winfocus
&& ICONIC(winfocus
))
4293 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
,
4296 if (winfocus
== NULL
) {
4297 if (focus_close_wrap
) {
4298 TAILQ_FOREACH_REVERSE(winfocus
, wl
,
4300 if (!ICONIC(winfocus
) &&
4301 winfocus
!= cur_focus
)
4304 TAILQ_FOREACH(winfocus
, wl
, entry
)
4305 if (!ICONIC(winfocus
) &&
4306 winfocus
!= cur_focus
)
4314 if (winfocus
== NULL
||
4315 (winfocus
&& (ICONIC(winfocus
) || winfocus
== cur_focus
))) {
4316 if (focus_default
== SWM_STACK_TOP
) {
4317 TAILQ_FOREACH_REVERSE(winfocus
, wl
, ws_win_list
, entry
)
4318 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4321 TAILQ_FOREACH(winfocus
, wl
, entry
)
4322 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4329 return get_focus_magic(winfocus
);
4333 get_region_focus(struct swm_region
*r
)
4335 struct ws_win
*winfocus
= NULL
;
4340 if (r
->ws
->focus
&& !ICONIC(r
->ws
->focus
))
4341 winfocus
= r
->ws
->focus
;
4342 else if (r
->ws
->focus_prev
&& !ICONIC(r
->ws
->focus_prev
))
4343 winfocus
= r
->ws
->focus_prev
;
4345 TAILQ_FOREACH(winfocus
, &r
->ws
->winlist
, entry
)
4346 if (!ICONIC(winfocus
))
4349 return get_focus_magic(winfocus
);
4353 focus(struct swm_region
*r
, union arg
*args
)
4355 struct ws_win
*head
, *cur_focus
= NULL
, *winfocus
= NULL
;
4356 struct ws_win_list
*wl
= NULL
;
4357 struct workspace
*ws
= NULL
;
4364 cur_focus
= r
->ws
->focus
;
4368 DNPRINTF(SWM_D_FOCUS
, "focus: id: %d, cur_focus: %#x\n", args
->id
,
4371 /* Make sure an uniconified window has focus, if one exists. */
4372 if (cur_focus
== NULL
) {
4373 cur_focus
= TAILQ_FIRST(wl
);
4374 while (cur_focus
!= NULL
&& ICONIC(cur_focus
))
4375 cur_focus
= TAILQ_NEXT(cur_focus
, entry
);
4377 DNPRINTF(SWM_D_FOCUS
, "focus: new cur_focus: %#x\n",
4382 case SWM_ARG_ID_FOCUSPREV
:
4383 if (cur_focus
== NULL
)
4386 winfocus
= cur_focus
;
4388 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
, entry
);
4389 if (winfocus
== NULL
)
4390 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
4391 if (winfocus
== cur_focus
)
4393 } while (winfocus
&& (ICONIC(winfocus
) ||
4394 winfocus
->id
== cur_focus
->transient
||
4395 (cur_focus
->transient
!= XCB_WINDOW_NONE
&&
4396 winfocus
->transient
== cur_focus
->transient
)));
4398 case SWM_ARG_ID_FOCUSNEXT
:
4399 if (cur_focus
== NULL
)
4402 winfocus
= cur_focus
;
4404 winfocus
= TAILQ_NEXT(winfocus
, entry
);
4405 if (winfocus
== NULL
)
4406 winfocus
= TAILQ_FIRST(wl
);
4407 if (winfocus
== cur_focus
)
4409 } while (winfocus
&& (ICONIC(winfocus
) ||
4410 winfocus
->id
== cur_focus
->transient
||
4411 (cur_focus
->transient
!= XCB_WINDOW_NONE
&&
4412 winfocus
->transient
== cur_focus
->transient
)));
4414 case SWM_ARG_ID_FOCUSMAIN
:
4415 if (cur_focus
== NULL
)
4418 winfocus
= TAILQ_FIRST(wl
);
4419 if (winfocus
== cur_focus
)
4420 winfocus
= cur_focus
->ws
->focus_prev
;
4422 case SWM_ARG_ID_FOCUSURGENT
:
4423 /* Search forward for the next urgent window. */
4427 for (i
= 0; i
<= workspace_limit
; ++i
) {
4429 head
= TAILQ_FIRST(&r
->s
->ws
[(ws
->idx
+ i
) %
4430 workspace_limit
].winlist
);
4433 if (head
== cur_focus
) {
4435 winfocus
= cur_focus
;
4438 } else if (get_urgent(head
)) {
4443 head
= TAILQ_NEXT(head
, entry
);
4450 /* Switch ws if new focus is on a different ws. */
4451 if (winfocus
&& winfocus
->ws
!= ws
) {
4452 a
.id
= winfocus
->ws
->idx
;
4460 if (clear_maximized(ws
) > 0)
4463 focus_win(get_focus_magic(winfocus
));
4468 DNPRINTF(SWM_D_FOCUS
, "focus: done\n");
4472 cycle_layout(struct swm_region
*r
, union arg
*args
)
4474 struct workspace
*ws
= r
->ws
;
4476 /* suppress unused warning since var is needed */
4479 DNPRINTF(SWM_D_EVENT
, "cycle_layout: workspace: %d\n", ws
->idx
);
4482 if (ws
->cur_layout
->l_stack
== NULL
)
4483 ws
->cur_layout
= &layouts
[0];
4485 clear_maximized(ws
);
4490 focus_win(get_region_focus(r
));
4497 stack_config(struct swm_region
*r
, union arg
*args
)
4499 struct workspace
*ws
= r
->ws
;
4501 DNPRINTF(SWM_D_STACK
, "stack_config: id: %d workspace: %d\n",
4504 if (clear_maximized(ws
) > 0)
4507 if (ws
->cur_layout
->l_config
!= NULL
)
4508 ws
->cur_layout
->l_config(ws
, args
->id
);
4510 if (args
->id
!= SWM_ARG_ID_STACKINIT
)
4520 struct swm_geometry g
;
4521 struct swm_region
*r
, *r_prev
= NULL
;
4528 DNPRINTF(SWM_D_STACK
, "stack: begin\n");
4530 num_screens
= get_screen_count();
4531 for (i
= 0; i
< num_screens
; i
++) {
4535 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
4536 /* Adjust stack area for region bar and padding. */
4538 g
.x
+= region_padding
;
4539 g
.y
+= region_padding
;
4540 g
.w
-= 2 * border_width
+ 2 * region_padding
;
4541 g
.h
-= 2 * border_width
+ 2 * region_padding
;
4542 if (bar_enabled
&& r
->ws
->bar_enabled
) {
4548 DNPRINTF(SWM_D_STACK
, "stack: workspace: %d (screen: "
4549 "%d, region: %d), (x,y) WxH: (%d,%d) %d x %d\n",
4550 r
->ws
->idx
, i
, j
++, g
.x
, g
.y
, g
.w
, g
.h
);
4553 /* Stack bar/input relative to prev. region. */
4554 val
[1] = XCB_STACK_MODE_ABOVE
;
4556 val
[0] = r_prev
->id
;
4557 DNPRINTF(SWM_D_STACK
, "stack: region input %#x "
4558 "relative to %#x.\n", r
->id
, val
[0]);
4559 xcb_configure_window(conn
, r
->id
,
4560 XCB_CONFIG_WINDOW_SIBLING
|
4561 XCB_CONFIG_WINDOW_STACK_MODE
, val
);
4563 val
[0] = r_prev
->bar
->id
;
4564 DNPRINTF(SWM_D_STACK
, "stack: region bar %#x "
4565 "relative to %#x.\n", r
->bar
->id
, val
[0]);
4566 xcb_configure_window(conn
, r
->bar
->id
,
4567 XCB_CONFIG_WINDOW_SIBLING
|
4568 XCB_CONFIG_WINDOW_STACK_MODE
, val
);
4571 r
->ws
->cur_layout
->l_stack(r
->ws
, &g
);
4572 r
->ws
->cur_layout
->l_string(r
->ws
);
4573 /* save r so we can track region changes */
4581 DNPRINTF(SWM_D_STACK
, "stack: end\n");
4585 store_float_geom(struct ws_win
*win
)
4587 if (win
== NULL
|| win
->ws
->r
== NULL
)
4590 /* retain window geom and region geom */
4591 win
->g_float
= win
->g
;
4592 win
->g_float
.x
-= X(win
->ws
->r
);
4593 win
->g_float
.y
-= Y(win
->ws
->r
);
4594 win
->g_floatvalid
= true;
4595 DNPRINTF(SWM_D_MISC
, "store_float_geom: win %#x, g: (%d,%d)"
4596 " %d x %d, g_float: (%d,%d) %d x %d\n", win
->id
, X(win
), Y(win
),
4597 WIDTH(win
), HEIGHT(win
), win
->g_float
.x
, win
->g_float
.y
,
4598 win
->g_float
.w
, win
->g_float
.h
);
4602 load_float_geom(struct ws_win
*win
)
4604 if (win
== NULL
|| win
->ws
->r
== NULL
)
4607 if (win
->g_floatvalid
) {
4608 win
->g
= win
->g_float
;
4609 X(win
) += X(win
->ws
->r
);
4610 Y(win
) += Y(win
->ws
->r
);
4611 DNPRINTF(SWM_D_MISC
, "load_float_geom: win %#x, g: (%d,%d)"
4612 "%d x %d\n", win
->id
, X(win
), Y(win
), WIDTH(win
),
4615 DNPRINTF(SWM_D_MISC
, "load_float_geom: win %#x, g_float "
4616 "is not set.\n", win
->id
);
4621 update_floater(struct ws_win
*win
)
4623 struct workspace
*ws
;
4624 struct swm_region
*r
;
4631 if ((r
= ws
->r
) == NULL
)
4634 DNPRINTF(SWM_D_MISC
, "update_floater: win %#x\n", win
->id
);
4636 win
->bordered
= true;
4638 if (FULLSCREEN(win
)) {
4639 /* _NET_WM_FULLSCREEN: fullscreen without border. */
4640 if (!win
->g_floatvalid
)
4641 store_float_geom(win
);
4644 win
->bordered
= false;
4645 } else if (MAXIMIZED(win
)) {
4646 /* Maximize: like a single stacked window. */
4647 if (!win
->g_floatvalid
)
4648 store_float_geom(win
);
4652 if (bar_enabled
&& ws
->bar_enabled
) {
4654 Y(win
) += bar_height
;
4655 HEIGHT(win
) -= bar_height
;
4656 } else if (disable_border
) {
4657 win
->bordered
= false;
4660 if (win
->bordered
) {
4661 HEIGHT(win
) -= 2 * border_width
;
4662 WIDTH(win
) -= 2 * border_width
;
4665 /* Normal floating window. */
4666 /* Update geometry if new region. */
4668 load_float_geom(win
);
4670 if ((win
->quirks
& SWM_Q_FULLSCREEN
) &&
4671 WIDTH(win
) >= WIDTH(r
) && HEIGHT(win
) >= HEIGHT(r
)) {
4672 /* Remove border for FULLSCREEN quirk. */
4673 win
->bordered
= false;
4674 } else if (!MANUAL(win
)) {
4675 if (TRANS(win
) && (win
->quirks
& SWM_Q_TRANSSZ
)) {
4676 /* Adjust size on TRANSSZ quirk. */
4677 WIDTH(win
) = (double)WIDTH(r
) * dialog_ratio
;
4678 HEIGHT(win
) = (double)HEIGHT(r
) * dialog_ratio
;
4681 if (!(win
->quirks
& SWM_Q_ANYWHERE
)) {
4683 * Floaters and transients are auto-centred
4684 * unless manually moved, resized or ANYWHERE
4687 X(win
) = X(r
) + (WIDTH(r
) - WIDTH(win
)) / 2 -
4689 Y(win
) = Y(r
) + (HEIGHT(r
) - HEIGHT(win
)) / 2 -
4691 store_float_geom(win
);
4696 /* Ensure at least 1 pixel of the window is in the region. */
4697 region_containment(win
, r
, SWM_CW_ALLSIDES
);
4703 * Send keystrokes to terminal to decrease/increase the font size as the
4704 * window size changes.
4707 adjust_font(struct ws_win
*win
)
4709 if (!(win
->quirks
& SWM_Q_XTERM_FONTADJ
) ||
4710 ABOVE(win
) || TRANS(win
))
4713 if (win
->sh
.width_inc
&& win
->last_inc
!= win
->sh
.width_inc
&&
4714 WIDTH(win
) / win
->sh
.width_inc
< term_width
&&
4715 win
->font_steps
< SWM_MAX_FONT_STEPS
) {
4716 win
->font_size_boundary
[win
->font_steps
] =
4717 (win
->sh
.width_inc
* term_width
) + win
->sh
.base_width
;
4720 win
->last_inc
= win
->sh
.width_inc
;
4721 fake_keypress(win
, XK_KP_Subtract
, XCB_MOD_MASK_SHIFT
);
4722 } else if (win
->font_steps
&& win
->last_inc
!= win
->sh
.width_inc
&&
4723 WIDTH(win
) > win
->font_size_boundary
[win
->font_steps
- 1]) {
4726 win
->last_inc
= win
->sh
.width_inc
;
4727 fake_keypress(win
, XK_KP_Add
, XCB_MOD_MASK_SHIFT
);
4731 #define SWAPXY(g) do { \
4733 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
4734 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
4737 stack_master(struct workspace
*ws
, struct swm_geometry
*g
, int rot
, bool flip
)
4739 struct swm_geometry win_g
, r_g
= *g
;
4741 int i
, j
, s
, stacks
;
4742 int w_inc
= 1, h_inc
, w_base
= 1, h_base
;
4743 int hrh
, extra
= 0, h_slice
, last_h
= 0;
4744 int split
, colno
, winno
, mwin
, msize
, mscale
;
4745 int remain
, missing
, v_slice
;
4746 bool bordered
= true, reconfigure
= false;
4748 DNPRINTF(SWM_D_STACK
, "stack_master: workspace: %d, rot: %s, "
4749 "flip: %s\n", ws
->idx
, YESNO(rot
), YESNO(flip
));
4751 /* Prepare tiling variables, if needed. */
4752 if ((winno
= count_win(ws
, false)) > 0) {
4753 /* Find first tiled window. */
4754 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
4755 if (!FLOATING(win
) && !ICONIC(win
))
4758 /* Take into account size hints of first tiled window. */
4760 w_inc
= win
->sh
.width_inc
;
4761 w_base
= win
->sh
.base_width
;
4762 mwin
= ws
->l_state
.horizontal_mwin
;
4763 mscale
= ws
->l_state
.horizontal_msize
;
4764 stacks
= ws
->l_state
.horizontal_stacks
;
4767 w_inc
= win
->sh
.height_inc
;
4768 w_base
= win
->sh
.base_height
;
4769 mwin
= ws
->l_state
.vertical_mwin
;
4770 mscale
= ws
->l_state
.vertical_msize
;
4771 stacks
= ws
->l_state
.vertical_stacks
;
4775 if (stacks
> winno
- mwin
)
4776 stacks
= winno
- mwin
;
4780 h_slice
= r_g
.h
/ SWM_H_SLICE
;
4781 if (mwin
&& winno
> mwin
) {
4782 v_slice
= r_g
.w
/ SWM_V_SLICE
;
4786 win_g
.w
= v_slice
* mscale
;
4788 if (w_inc
> 1 && w_inc
< v_slice
) {
4789 /* Adjust for requested size increment. */
4790 remain
= (win_g
.w
- w_base
) % w_inc
;
4796 win_g
.x
+= r_g
.w
- msize
;
4799 colno
= split
= winno
/ stacks
;
4800 win_g
.w
= ((r_g
.w
- (stacks
* 2 * border_width
) +
4801 2 * border_width
) / stacks
);
4803 hrh
= r_g
.h
/ colno
;
4804 extra
= r_g
.h
- (colno
* hrh
);
4805 win_g
.h
= hrh
- 2 * border_width
;
4806 i
= j
= 0, s
= stacks
;
4809 /* Update window geometry. */
4810 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
4814 if (FLOATING(win
)) {
4815 update_floater(win
);
4820 if (split
&& i
== split
) {
4821 colno
= (winno
- mwin
) / stacks
;
4822 if (s
<= (winno
- mwin
) % stacks
)
4825 hrh
= r_g
.h
/ colno
;
4826 extra
= r_g
.h
- (colno
* hrh
);
4829 win_g
.x
+= win_g
.w
+ 2 * border_width
+
4832 win_g
.w
= (r_g
.w
- msize
-
4833 (stacks
* (2 * border_width
+ tile_gap
))) / stacks
;
4835 win_g
.w
+= (r_g
.w
- msize
-
4836 (stacks
* (2 * border_width
+ tile_gap
))) %
4840 win_g
.x
-= win_g
.w
+ 2 * border_width
+
4846 win_g
.h
= hrh
- 2 * border_width
- tile_gap
;
4849 h_inc
= win
->sh
.width_inc
;
4850 h_base
= win
->sh
.base_width
;
4852 h_inc
= win
->sh
.height_inc
;
4853 h_base
= win
->sh
.base_height
;
4856 if (j
== colno
- 1) {
4857 win_g
.h
= hrh
+ extra
;
4858 } else if (h_inc
> 1 && h_inc
< h_slice
) {
4859 /* adjust for window's requested size increment */
4860 remain
= (win_g
.h
- h_base
) % h_inc
;
4861 missing
= h_inc
- remain
;
4863 if (missing
<= extra
|| j
== 0) {
4875 win_g
.y
+= last_h
+ 2 * border_width
+ tile_gap
;
4877 if (disable_border
&& !(bar_enabled
&& ws
->bar_enabled
) &&
4880 win_g
.w
+= 2 * border_width
;
4881 win_g
.h
+= 2 * border_width
;
4887 if (X(win
) != win_g
.y
|| Y(win
) != win_g
.x
||
4888 WIDTH(win
) != win_g
.h
|| HEIGHT(win
) != win_g
.w
) {
4892 WIDTH(win
) = win_g
.h
;
4893 HEIGHT(win
) = win_g
.w
;
4896 if (X(win
) != win_g
.x
|| Y(win
) != win_g
.y
||
4897 WIDTH(win
) != win_g
.w
|| HEIGHT(win
) != win_g
.h
) {
4901 WIDTH(win
) = win_g
.w
;
4902 HEIGHT(win
) = win_g
.h
;
4906 if (bordered
!= win
->bordered
) {
4908 win
->bordered
= bordered
;
4921 /* Stack all windows from bottom up. */
4922 TAILQ_FOREACH_REVERSE(win
, &ws
->stack
, ws_win_stack
, stack_entry
)
4924 update_win_stacking(win
);
4926 /* Map all windows from top down. */
4927 TAILQ_FOREACH(win
, &ws
->stack
, stack_entry
)
4931 DNPRINTF(SWM_D_STACK
, "stack_master: done\n");
4935 vertical_config(struct workspace
*ws
, int id
)
4937 DNPRINTF(SWM_D_STACK
, "vertical_config: id: %d, workspace: %d\n",
4941 case SWM_ARG_ID_STACKRESET
:
4942 case SWM_ARG_ID_STACKINIT
:
4943 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ 2;
4944 ws
->l_state
.vertical_mwin
= 1;
4945 ws
->l_state
.vertical_stacks
= 1;
4947 case SWM_ARG_ID_MASTERSHRINK
:
4948 if (ws
->l_state
.vertical_msize
> 1)
4949 ws
->l_state
.vertical_msize
--;
4951 case SWM_ARG_ID_MASTERGROW
:
4952 if (ws
->l_state
.vertical_msize
< SWM_V_SLICE
- 1)
4953 ws
->l_state
.vertical_msize
++;
4955 case SWM_ARG_ID_MASTERADD
:
4956 ws
->l_state
.vertical_mwin
++;
4958 case SWM_ARG_ID_MASTERDEL
:
4959 if (ws
->l_state
.vertical_mwin
> 0)
4960 ws
->l_state
.vertical_mwin
--;
4962 case SWM_ARG_ID_STACKINC
:
4963 ws
->l_state
.vertical_stacks
++;
4965 case SWM_ARG_ID_STACKDEC
:
4966 if (ws
->l_state
.vertical_stacks
> 1)
4967 ws
->l_state
.vertical_stacks
--;
4969 case SWM_ARG_ID_FLIPLAYOUT
:
4970 ws
->l_state
.vertical_flip
= !ws
->l_state
.vertical_flip
;
4978 vertical_stack(struct workspace
*ws
, struct swm_geometry
*g
)
4980 DNPRINTF(SWM_D_STACK
, "vertical_stack: workspace: %d\n", ws
->idx
);
4982 stack_master(ws
, g
, 0, ws
->l_state
.vertical_flip
);
4986 horizontal_config(struct workspace
*ws
, int id
)
4988 DNPRINTF(SWM_D_STACK
, "horizontal_config: workspace: %d\n", ws
->idx
);
4991 case SWM_ARG_ID_STACKRESET
:
4992 case SWM_ARG_ID_STACKINIT
:
4993 ws
->l_state
.horizontal_mwin
= 1;
4994 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ 2;
4995 ws
->l_state
.horizontal_stacks
= 1;
4997 case SWM_ARG_ID_MASTERSHRINK
:
4998 if (ws
->l_state
.horizontal_msize
> 1)
4999 ws
->l_state
.horizontal_msize
--;
5001 case SWM_ARG_ID_MASTERGROW
:
5002 if (ws
->l_state
.horizontal_msize
< SWM_H_SLICE
- 1)
5003 ws
->l_state
.horizontal_msize
++;
5005 case SWM_ARG_ID_MASTERADD
:
5006 ws
->l_state
.horizontal_mwin
++;
5008 case SWM_ARG_ID_MASTERDEL
:
5009 if (ws
->l_state
.horizontal_mwin
> 0)
5010 ws
->l_state
.horizontal_mwin
--;
5012 case SWM_ARG_ID_STACKINC
:
5013 ws
->l_state
.horizontal_stacks
++;
5015 case SWM_ARG_ID_STACKDEC
:
5016 if (ws
->l_state
.horizontal_stacks
> 1)
5017 ws
->l_state
.horizontal_stacks
--;
5019 case SWM_ARG_ID_FLIPLAYOUT
:
5020 ws
->l_state
.horizontal_flip
= !ws
->l_state
.horizontal_flip
;
5028 horizontal_stack(struct workspace
*ws
, struct swm_geometry
*g
)
5030 DNPRINTF(SWM_D_STACK
, "horizontal_stack: workspace: %d\n", ws
->idx
);
5032 stack_master(ws
, g
, 1, ws
->l_state
.horizontal_flip
);
5035 /* fullscreen view */
5037 max_stack(struct workspace
*ws
, struct swm_geometry
*g
)
5039 struct swm_geometry gg
= *g
;
5040 struct ws_win
*w
, *win
= NULL
, *parent
= NULL
, *tmpw
;
5043 DNPRINTF(SWM_D_STACK
, "max_stack: workspace: %d\n", ws
->idx
);
5048 winno
= count_win(ws
, false);
5049 if (winno
== 0 && count_win(ws
, true) == 0)
5052 /* Figure out which top level window should be visible. */
5053 if (ws
->focus_pending
)
5054 win
= ws
->focus_pending
;
5057 else if (ws
->focus_prev
)
5058 win
= ws
->focus_prev
;
5060 win
= TAILQ_FIRST(&ws
->winlist
);
5062 DNPRINTF(SWM_D_STACK
, "max_stack: focus_pending: %#x, focus: %#x, "
5063 "focus_prev: %#x, first: %#x, win: %#x\n", WINID(ws
->focus_pending
),
5064 WINID(ws
->focus
), WINID(ws
->focus_prev
),
5065 WINID(TAILQ_FIRST(&ws
->winlist
)), win
->id
);
5067 /* Update window geometry. */
5068 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
5077 /* Set maximized flag for all maxed windows. */
5078 if (!MAXIMIZED(w
)) {
5079 /* Preserve floating geometry. */
5081 store_float_geom(w
);
5083 ewmh_apply_flags(w
, w
->ewmh_flags
| EWMH_F_MAXIMIZED
);
5084 ewmh_update_wm_state(w
);
5087 /* Only reconfigure if necessary. */
5088 if (X(w
) != gg
.x
|| Y(w
) != gg
.y
|| WIDTH(w
) != gg
.w
||
5089 HEIGHT(w
) != gg
.h
) {
5092 if (disable_border
&& !(bar_enabled
&& ws
->bar_enabled
)) {
5093 w
->bordered
= false;
5094 WIDTH(w
) += 2 * border_width
;
5095 HEIGHT(w
) += 2 * border_width
;
5104 /* If transient, stack parent and its children. */
5105 if (TRANS(win
) && (parent
= find_window(win
->transient
))) {
5106 raise_window(parent
);
5108 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
, tmpw
)
5109 if (w
->transient
== parent
->id
)
5113 /* Make sure focus window is on top. */
5116 /* Stack any children of focus window. */
5117 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
, tmpw
)
5118 if (w
->transient
== win
->id
)
5121 /* Map all windows. */
5122 TAILQ_FOREACH(w
, &ws
->stack
, stack_entry
)
5128 send_to_rg(struct swm_region
*r
, union arg
*args
)
5130 int ridx
= args
->id
, i
, num_screens
;
5131 struct swm_region
*rr
= NULL
;
5134 num_screens
= get_screen_count();
5135 /* do nothing if we don't have more than one screen */
5136 if (!(num_screens
> 1 || outputs
> 1))
5139 DNPRINTF(SWM_D_FOCUS
, "send_to_rg: id: %d\n", ridx
);
5141 rr
= TAILQ_FIRST(&r
->s
->rl
);
5142 for (i
= 0; i
< ridx
&& rr
!= NULL
; ++i
)
5143 rr
= TAILQ_NEXT(rr
, entry
);
5154 region_under(struct swm_screen
*s
, int x
, int y
)
5156 struct swm_region
*r
= NULL
;
5158 TAILQ_FOREACH(r
, &s
->rl
, entry
) {
5159 DNPRINTF(SWM_D_MISC
, "region_under: ws: %d, region g: (%d,%d) "
5160 "%d x %d, coords: (%d,%d)\n", r
->ws
->idx
, X(r
), Y(r
),
5161 WIDTH(r
), HEIGHT(r
), x
, y
);
5162 if (X(r
) <= x
&& x
< MAX_X(r
))
5163 if (Y(r
) <= y
&& y
< MAX_Y(r
))
5170 /* Transfer focused window to target workspace and focus. */
5172 send_to_ws(struct swm_region
*r
, union arg
*args
)
5174 int wsid
= args
->id
;
5175 struct ws_win
*win
= NULL
;
5177 if (r
&& r
->ws
&& r
->ws
->focus
)
5182 DNPRINTF(SWM_D_MOVE
, "send_to_ws: win %#x, ws %d\n", win
->id
, wsid
);
5184 if (wsid
< 0 || wsid
>= workspace_limit
)
5187 if (win
->ws
->idx
== wsid
)
5190 win_to_ws(win
, wsid
, true);
5192 /* Set new focus on target ws. */
5193 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
5194 win
->ws
->focus_prev
= win
->ws
->focus
;
5195 win
->ws
->focus
= win
;
5196 win
->ws
->focus_pending
= NULL
;
5198 if (win
->ws
->focus_prev
)
5199 update_window_color(win
->ws
->focus_prev
);
5202 DNPRINTF(SWM_D_STACK
, "send_to_ws: focus_pending: %#x, focus: %#x, "
5203 "focus_prev: %#x, first: %#x, win: %#x\n",
5204 WINID(r
->ws
->focus_pending
), WINID(r
->ws
->focus
),
5205 WINID(r
->ws
->focus_prev
), WINID(TAILQ_FIRST(&r
->ws
->winlist
)),
5208 ewmh_apply_flags(win
, win
->ewmh_flags
& ~EWMH_F_MAXIMIZED
);
5209 ewmh_update_wm_state(win
);
5211 /* Restack and set new focus on current ws. */
5213 load_float_geom(win
);
5217 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
5218 if (r
->ws
->focus
!= NULL
) {
5219 focus_win(r
->ws
->focus
);
5221 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
, r
->id
,
5232 win_to_ws(struct ws_win
*win
, int wsid
, bool unfocus
)
5234 struct ws_win
*parent
;
5235 struct workspace
*ws
, *nws
, *pws
;
5237 if (wsid
< 0 || wsid
>= workspace_limit
)
5240 if (win
->ws
->idx
== wsid
)
5244 nws
= &win
->s
->ws
[wsid
];
5246 DNPRINTF(SWM_D_MOVE
, "win_to_ws: win %#x, ws %d -> %d\n", win
->id
,
5249 /* Cleanup focus on source ws. */
5250 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
5251 (ws
->focus
== win
|| ws
->focus_pending
== win
))
5252 ws
->focus_pending
= get_focus_prev(win
);
5254 /* Move the parent if this is a transient window. */
5256 parent
= find_window(win
->transient
);
5259 /* Set new focus in parent's ws if needed. */
5260 if (pws
->focus
== parent
) {
5261 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
5262 pws
->focus_pending
=
5263 get_focus_prev(parent
);
5265 unfocus_win(parent
);
5267 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
5268 pws
->focus
= pws
->focus_pending
;
5269 pws
->focus_pending
= NULL
;
5273 /* Don't unmap parent if new ws is visible */
5275 unmap_window(parent
);
5278 TAILQ_REMOVE(&ws
->winlist
, parent
, entry
);
5279 TAILQ_REMOVE(&ws
->stack
, parent
, stack_entry
);
5280 TAILQ_INSERT_TAIL(&nws
->winlist
, parent
, entry
);
5281 TAILQ_INSERT_TAIL(&nws
->stack
, parent
, stack_entry
);
5284 DNPRINTF(SWM_D_PROP
, "win_to_ws: set property: "
5285 "_NET_WM_DESKTOP: %d\n", wsid
);
5286 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
5287 parent
->id
, ewmh
[_NET_WM_DESKTOP
].atom
,
5288 XCB_ATOM_CARDINAL
, 32, 1, &wsid
);
5295 if (ws
->focus_prev
== win
)
5296 ws
->focus_prev
= NULL
;
5298 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& ws
->focus_pending
!= NULL
) {
5299 ws
->focus
= ws
->focus_pending
;
5300 ws
->focus_pending
= NULL
;
5303 /* Don't unmap if new ws is visible */
5308 TAILQ_REMOVE(&ws
->winlist
, win
, entry
);
5309 TAILQ_REMOVE(&ws
->stack
, win
, stack_entry
);
5310 TAILQ_INSERT_TAIL(&nws
->winlist
, win
, entry
);
5311 TAILQ_INSERT_TAIL(&nws
->stack
, win
, stack_entry
);
5314 /* Update the window's workspace property: _NET_WM_DESKTOP */
5315 DNPRINTF(SWM_D_PROP
, "win_to_ws: set property: "
5316 "_NET_WM_DESKTOP: %d\n", wsid
);
5317 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
5318 ewmh
[_NET_WM_DESKTOP
].atom
, XCB_ATOM_CARDINAL
, 32, 1, &wsid
);
5320 ewmh_update_client_list();
5322 DNPRINTF(SWM_D_MOVE
, "win_to_ws: done.\n");
5326 pressbutton(struct swm_region
*r
, union arg
*args
)
5328 /* suppress unused warning since var is needed */
5331 xcb_test_fake_input(conn
, XCB_BUTTON_PRESS
, args
->id
,
5332 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
5333 xcb_test_fake_input(conn
, XCB_BUTTON_RELEASE
, args
->id
,
5334 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
5338 raise_toggle(struct swm_region
*r
, union arg
*args
)
5340 /* Suppress warning. */
5343 if (r
== NULL
|| r
->ws
== NULL
)
5346 if (r
->ws
->focus
&& MAXIMIZED(r
->ws
->focus
))
5349 r
->ws
->always_raise
= !r
->ws
->always_raise
;
5351 /* Update focused win stacking order based on new always_raise value. */
5352 raise_window(r
->ws
->focus
);
5358 iconify(struct swm_region
*r
, union arg
*args
)
5362 /* Suppress warning. */
5365 if ((w
= r
->ws
->focus
) == NULL
)
5368 ewmh_apply_flags(w
, w
->ewmh_flags
| EWMH_F_HIDDEN
);
5369 ewmh_update_wm_state(w
);
5377 get_win_name(xcb_window_t win
)
5380 xcb_get_property_cookie_t c
;
5381 xcb_get_property_reply_t
*r
;
5383 /* First try _NET_WM_NAME for UTF-8. */
5384 c
= xcb_get_property(conn
, 0, win
, ewmh
[_NET_WM_NAME
].atom
,
5385 XCB_GET_PROPERTY_TYPE_ANY
, 0, UINT_MAX
);
5386 r
= xcb_get_property_reply(conn
, c
, NULL
);
5387 if (r
&& r
->type
== XCB_NONE
) {
5389 /* Use WM_NAME instead; no UTF-8. */
5390 c
= xcb_get_property(conn
, 0, win
, XCB_ATOM_WM_NAME
,
5391 XCB_GET_PROPERTY_TYPE_ANY
, 0, UINT_MAX
);
5392 r
= xcb_get_property_reply(conn
, c
, NULL
);
5395 if (r
&& r
->type
!= XCB_NONE
&& r
->length
> 0)
5396 name
= strndup(xcb_get_property_value(r
),
5397 xcb_get_property_value_length(r
));
5402 err(1, "get_win_name: failed to allocate memory.");
5410 uniconify(struct swm_region
*r
, union arg
*args
)
5417 DNPRINTF(SWM_D_MISC
, "uniconify\n");
5419 if (r
== NULL
|| r
->ws
== NULL
)
5422 /* make sure we have anything to uniconify */
5423 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
5424 if (win
->ws
== NULL
)
5425 continue; /* should never happen */
5431 DNPRINTF(SWM_D_MISC
, "uniconify: count: %d\n", count
);
5437 search_resp_action
= SWM_SEARCH_UNICONIFY
;
5439 spawn_select(r
, args
, "search", &searchpid
);
5441 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
5444 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
5445 if (win
->ws
== NULL
)
5446 continue; /* should never happen */
5450 name
= get_win_name(win
->id
);
5451 fprintf(lfile
, "%s.%u\n", name
, win
->id
);
5459 name_workspace(struct swm_region
*r
, union arg
*args
)
5463 DNPRINTF(SWM_D_MISC
, "name_workspace\n");
5469 search_resp_action
= SWM_SEARCH_NAME_WORKSPACE
;
5471 spawn_select(r
, args
, "name_workspace", &searchpid
);
5473 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
5476 fprintf(lfile
, "%s", "");
5481 search_workspace(struct swm_region
*r
, union arg
*args
)
5484 struct workspace
*ws
;
5487 DNPRINTF(SWM_D_MISC
, "search_workspace\n");
5493 search_resp_action
= SWM_SEARCH_SEARCH_WORKSPACE
;
5495 spawn_select(r
, args
, "search", &searchpid
);
5497 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
5500 for (i
= 0; i
< workspace_limit
; i
++) {
5504 fprintf(lfile
, "%d%s%s\n", ws
->idx
+ 1,
5505 (ws
->name
? ":" : ""), (ws
->name
? ws
->name
: ""));
5512 search_win_cleanup(void)
5514 struct search_window
*sw
= NULL
;
5516 while ((sw
= TAILQ_FIRST(&search_wl
)) != NULL
) {
5517 xcb_destroy_window(conn
, sw
->indicator
);
5518 TAILQ_REMOVE(&search_wl
, sw
, entry
);
5524 search_win(struct swm_region
*r
, union arg
*args
)
5526 struct ws_win
*win
= NULL
;
5527 struct search_window
*sw
= NULL
;
5530 xcb_screen_t
*screen
;
5531 int i
, width
, height
;
5539 XRectangle l_ibox
, l_lbox
;
5541 DNPRINTF(SWM_D_MISC
, "search_win\n");
5544 search_resp_action
= SWM_SEARCH_SEARCH_WINDOW
;
5546 spawn_select(r
, args
, "search", &searchpid
);
5548 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
5551 if ((screen
= get_screen(r
->s
->idx
)) == NULL
)
5552 errx(1, "ERROR: can't get screen %d.", r
->s
->idx
);
5554 TAILQ_INIT(&search_wl
);
5557 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
5561 sw
= calloc(1, sizeof(struct search_window
));
5563 warn("search_win: calloc");
5565 search_win_cleanup();
5571 snprintf(s
, sizeof s
, "%d", i
);
5574 w
= xcb_generate_id(conn
);
5575 wa
[0] = r
->s
->c
[SWM_S_COLOR_FOCUS
].pixel
;
5576 wa
[1] = r
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
;
5577 wa
[2] = screen
->default_colormap
;
5579 if (bar_font_legacy
) {
5580 XmbTextExtents(bar_fs
, s
, len
, &l_ibox
, &l_lbox
);
5581 width
= l_lbox
.width
+ 4;
5582 height
= bar_fs_extents
->max_logical_extent
.height
+ 4;
5584 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
,
5586 width
= info
.width
+ 4;
5587 height
= bar_font
->height
+ 4;
5590 xcb_create_window(conn
, screen
->root_depth
, w
, win
->id
, 0, 0,
5591 width
, height
, 1, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
5592 screen
->root_visual
, XCB_CW_BACK_PIXEL
|
5593 XCB_CW_BORDER_PIXEL
| XCB_CW_COLORMAP
, wa
);
5595 xcb_map_window(conn
, w
);
5598 TAILQ_INSERT_TAIL(&search_wl
, sw
, entry
);
5600 if (bar_font_legacy
) {
5601 l_gcv
.graphics_exposures
= 0;
5602 l_draw
= XCreateGC(display
, w
, 0, &l_gcv
);
5604 XSetForeground(display
, l_draw
,
5605 r
->s
->c
[SWM_S_COLOR_BAR
].pixel
);
5607 DRAWSTRING(display
, w
, bar_fs
, l_draw
, 2,
5608 (bar_fs_extents
->max_logical_extent
.height
-
5609 l_lbox
.height
) / 2 - l_lbox
.y
, s
, len
);
5611 XFreeGC(display
, l_draw
);
5614 draw
= XftDrawCreate(display
, w
,
5615 DefaultVisual(display
, r
->s
->idx
),
5616 DefaultColormap(display
, r
->s
->idx
));
5618 XftDrawStringUtf8(draw
, &search_font_color
, bar_font
, 2,
5619 (HEIGHT(r
->bar
) + bar_font
->height
) / 2 -
5620 bar_font
->descent
, (FcChar8
*)s
, len
);
5622 XftDrawDestroy(draw
);
5625 DNPRINTF(SWM_D_MISC
, "search_win: mapped win %#x\n", w
);
5627 fprintf(lfile
, "%d\n", i
);
5637 search_resp_uniconify(const char *resp
, size_t len
)
5643 DNPRINTF(SWM_D_MISC
, "search_resp_uniconify: resp: %s\n", resp
);
5645 TAILQ_FOREACH(win
, &search_r
->ws
->winlist
, entry
) {
5648 name
= get_win_name(win
->id
);
5649 if (asprintf(&s
, "%s.%u", name
, win
->id
) == -1) {
5654 if (strncmp(s
, resp
, len
) == 0) {
5655 /* XXX this should be a callback to generalize */
5656 ewmh_apply_flags(win
, win
->ewmh_flags
& ~EWMH_F_HIDDEN
);
5657 ewmh_update_wm_state(win
);
5667 search_resp_name_workspace(const char *resp
, size_t len
)
5669 struct workspace
*ws
;
5671 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: resp: %s\n", resp
);
5673 if (search_r
->ws
== NULL
)
5678 free(search_r
->ws
->name
);
5679 search_r
->ws
->name
= NULL
;
5683 ws
->name
= strdup(resp
);
5684 if (ws
->name
== NULL
) {
5685 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: "
5686 "strdup: %s", strerror(errno
));
5690 ewmh_update_desktop_names();
5691 ewmh_get_desktop_names();
5696 ewmh_update_desktop_names(void)
5698 char *name_list
= NULL
, *p
;
5699 int num_screens
, i
, j
, len
= 0, tot
= 0;
5701 num_screens
= get_screen_count();
5702 for (i
= 0; i
< num_screens
; ++i
) {
5703 for (j
= 0; j
< workspace_limit
; ++j
) {
5704 if (screens
[i
].ws
[j
].name
!= NULL
)
5705 len
+= strlen(screens
[i
].ws
[j
].name
);
5709 if((name_list
= calloc(len
, sizeof(char))) == NULL
)
5710 err(1, "update_desktop_names: calloc: failed to "
5711 "allocate memory.");
5714 for (j
= 0; j
< workspace_limit
; ++j
) {
5715 if (screens
[i
].ws
[j
].name
!= NULL
) {
5716 len
= strlen(screens
[i
].ws
[j
].name
);
5717 memcpy(p
, screens
[i
].ws
[j
].name
, len
);
5726 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
5727 screens
[i
].root
, ewmh
[_NET_DESKTOP_NAMES
].atom
,
5728 a_utf8_string
, 8, tot
, name_list
);
5738 ewmh_get_desktop_names(void)
5741 xcb_get_property_cookie_t c
;
5742 xcb_get_property_reply_t
*r
;
5743 int num_screens
, i
, j
, n
, k
;
5745 num_screens
= get_screen_count();
5746 for (i
= 0; i
< num_screens
; ++i
) {
5747 for (j
= 0; j
< workspace_limit
; ++j
) {
5748 free(screens
[i
].ws
[j
].name
);
5749 screens
[i
].ws
[j
].name
= NULL
;
5752 c
= xcb_get_property(conn
, 0, screens
[i
].root
,
5753 ewmh
[_NET_DESKTOP_NAMES
].atom
,
5754 a_utf8_string
, 0, UINT32_MAX
);
5755 r
= xcb_get_property_reply(conn
, c
, NULL
);
5759 names
= xcb_get_property_value(r
);
5760 n
= xcb_get_property_value_length(r
);
5762 for (j
= 0, k
= 0; j
< n
; ++j
) {
5763 if (*(names
+ j
) != '\0') {
5764 screens
[i
].ws
[k
].name
= strdup(names
+ j
);
5765 j
+= strlen(names
+ j
);
5774 ewmh_update_client_list(void)
5777 int num_screens
, i
, j
, k
= 0, count
= 0;
5780 num_screens
= get_screen_count();
5781 for (i
= 0; i
< num_screens
; ++i
) {
5782 for (j
= 0; j
< workspace_limit
; ++j
)
5783 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
5786 DNPRINTF(SWM_D_PROP
, "ewmh_update_client_list: win count: %d\n",
5792 wins
= calloc(count
, sizeof(xcb_window_t
));
5794 err(1, "ewmh_update_client_list: calloc: failed to "
5795 "allocate memory.");
5797 for (j
= 0, k
= 0; j
< workspace_limit
; ++j
)
5798 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
5799 wins
[k
++] = win
->id
;
5801 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
5802 screens
[i
].root
, ewmh
[_NET_CLIENT_LIST
].atom
,
5803 XCB_ATOM_WINDOW
, 32, count
, wins
);
5810 ewmh_update_current_desktop(void)
5814 num_screens
= get_screen_count();
5815 for (i
= 0; i
< num_screens
; ++i
)
5816 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
5817 screens
[i
].root
, ewmh
[_NET_CURRENT_DESKTOP
].atom
,
5818 XCB_ATOM_CARDINAL
, 32, 1, &screens
[i
].r_focus
->ws
->idx
);
5822 ewmh_update_desktops(void)
5824 int num_screens
, i
, j
;
5827 vals
= calloc(workspace_limit
* 2, sizeof(uint32_t));
5829 err(1, "ewmh_update_desktops: calloc: failed to allocate "
5832 num_screens
= get_screen_count();
5833 for (i
= 0; i
< num_screens
; i
++) {
5834 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
5835 screens
[i
].root
, ewmh
[_NET_NUMBER_OF_DESKTOPS
].atom
,
5836 XCB_ATOM_CARDINAL
, 32, 1, &workspace_limit
);
5838 for (j
= 0; j
< workspace_limit
; ++j
) {
5839 if (screens
[i
].ws
[j
].r
!= NULL
) {
5840 vals
[j
* 2] = X(screens
[i
].ws
[j
].r
);
5841 vals
[j
* 2 + 1] = Y(screens
[i
].ws
[j
].r
);
5842 } else if (screens
[i
].ws
[j
].old_r
!= NULL
) {
5843 vals
[j
* 2] = X(screens
[i
].ws
[j
].old_r
);
5844 vals
[j
* 2 + 1] = Y(screens
[i
].ws
[j
].old_r
);
5846 vals
[j
* 2] = vals
[j
* 2 + 1] = 0;
5850 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
5851 screens
[i
].root
, ewmh
[_NET_DESKTOP_VIEWPORT
].atom
,
5852 XCB_ATOM_CARDINAL
, 32, workspace_limit
* 2, vals
);
5859 search_resp_search_workspace(const char *resp
)
5866 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: resp: %s\n", resp
);
5870 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: strdup: %s",
5877 ws_idx
= (int)strtonum(q
, 1, workspace_limit
, &errstr
);
5879 DNPRINTF(SWM_D_MISC
, "workspace idx is %s: %s",
5886 switchws(search_r
, &a
);
5890 search_resp_search_window(const char *resp
)
5895 struct search_window
*sw
;
5897 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: resp: %s\n", resp
);
5901 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: strdup: %s",
5906 idx
= (int)strtonum(s
, 1, INT_MAX
, &errstr
);
5908 DNPRINTF(SWM_D_MISC
, "window idx is %s: %s",
5915 TAILQ_FOREACH(sw
, &search_wl
, entry
)
5916 if (idx
== sw
->idx
) {
5922 #define MAX_RESP_LEN 1024
5925 search_do_resp(void)
5931 DNPRINTF(SWM_D_MISC
, "search_do_resp:\n");
5936 if ((resp
= calloc(1, MAX_RESP_LEN
+ 1)) == NULL
) {
5937 warn("search: calloc");
5941 rbytes
= read(select_resp_pipe
[0], resp
, MAX_RESP_LEN
);
5943 warn("search: read error");
5946 resp
[rbytes
] = '\0';
5949 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
5950 * newline, so work around that by sanitizing the resp now.
5952 resp
[strcspn(resp
, "\n")] = '\0';
5955 switch (search_resp_action
) {
5956 case SWM_SEARCH_UNICONIFY
:
5957 search_resp_uniconify(resp
, len
);
5959 case SWM_SEARCH_NAME_WORKSPACE
:
5960 search_resp_name_workspace(resp
, len
);
5962 case SWM_SEARCH_SEARCH_WORKSPACE
:
5963 search_resp_search_workspace(resp
);
5965 case SWM_SEARCH_SEARCH_WINDOW
:
5966 search_resp_search_window(resp
);
5971 if (search_resp_action
== SWM_SEARCH_SEARCH_WINDOW
)
5972 search_win_cleanup();
5974 search_resp_action
= SWM_SEARCH_NONE
;
5975 close(select_resp_pipe
[0]);
5982 wkill(struct swm_region
*r
, union arg
*args
)
5984 DNPRINTF(SWM_D_MISC
, "wkill: win %#x, id: %d\n", WINID(r
->ws
->focus
),
5987 if (r
->ws
->focus
== NULL
)
5990 if (args
->id
== SWM_ARG_ID_KILLWINDOW
)
5991 xcb_kill_client(conn
, r
->ws
->focus
->id
);
5993 if (r
->ws
->focus
->can_delete
)
5994 client_msg(r
->ws
->focus
, a_delete
, 0);
6000 clear_maximized(struct workspace
*ws
)
6005 /* Clear any maximized win(s) on ws, from bottom up. */
6006 TAILQ_FOREACH_REVERSE(w
, &ws
->stack
, ws_win_stack
, stack_entry
)
6008 ewmh_apply_flags(w
, w
->ewmh_flags
& ~EWMH_F_MAXIMIZED
);
6009 ewmh_update_wm_state(w
);
6017 maximize_toggle(struct swm_region
*r
, union arg
*args
)
6019 struct ws_win
*w
= r
->ws
->focus
;
6021 /* suppress unused warning since var is needed */
6027 DNPRINTF(SWM_D_MISC
, "maximize_toggle: win %#x\n", w
->id
);
6032 if (w
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
6035 ewmh_apply_flags(w
, w
->ewmh_flags
^ EWMH_F_MAXIMIZED
);
6036 ewmh_update_wm_state(w
);
6040 if (w
== w
->ws
->focus
)
6045 DNPRINTF(SWM_D_MISC
, "maximize_toggle: done\n");
6049 floating_toggle(struct swm_region
*r
, union arg
*args
)
6051 struct ws_win
*w
= r
->ws
->focus
;
6053 /* suppress unused warning since var is needed */
6059 DNPRINTF(SWM_D_MISC
, "floating_toggle: win %#x\n", w
->id
);
6061 if (FULLSCREEN(w
) || TRANS(w
))
6064 if (w
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
6067 ewmh_apply_flags(w
, w
->ewmh_flags
^ EWMH_F_ABOVE
);
6068 ewmh_update_wm_state(w
);
6072 if (w
== w
->ws
->focus
)
6077 DNPRINTF(SWM_D_MISC
, "floating_toggle: done\n");
6081 region_containment(struct ws_win
*win
, struct swm_region
*r
, int opts
)
6083 struct swm_geometry g
= r
->g
;
6084 int rt
, lt
, tp
, bm
, bw
;
6086 bw
= (opts
& SWM_CW_SOFTBOUNDARY
) ? boundary_width
: 0;
6089 * Perpendicular distance of each side of the window to the respective
6090 * side of the region boundary. Positive values indicate the side of
6091 * the window has passed beyond the region boundary.
6093 rt
= opts
& SWM_CW_RIGHT
? MAX_X(win
) + BORDER(win
) - MAX_X(r
) : bw
;
6094 lt
= opts
& SWM_CW_LEFT
? X(r
) - X(win
) + BORDER(win
) : bw
;
6095 bm
= opts
& SWM_CW_BOTTOM
? MAX_Y(win
) + BORDER(win
) - MAX_Y(r
) : bw
;
6096 tp
= opts
& SWM_CW_TOP
? Y(r
) - Y(win
) + BORDER(win
) : bw
;
6098 DNPRINTF(SWM_D_MISC
, "region_containment: win %#x, rt: %d, lt: %d, "
6099 "bm: %d, tp: %d, SOFTBOUNDARY: %s, HARDBOUNDARY: %s\n", win
->id
, rt
,
6100 lt
, bm
, tp
, YESNO(opts
& SWM_CW_SOFTBOUNDARY
),
6101 YESNO(opts
& SWM_CW_HARDBOUNDARY
));
6104 * Disable containment if any of the flagged sides went beyond the
6105 * containment boundary, or if containment is disabled.
6107 if (!(opts
& SWM_CW_HARDBOUNDARY
|| opts
& SWM_CW_SOFTBOUNDARY
) ||
6108 (bw
!= 0 && ((rt
> bw
) || (lt
> bw
) || (bm
> bw
) || (tp
> bw
)))) {
6109 /* Make sure window has at least 1 pixel in the region */
6110 g
.x
+= 1 - WIDTH(win
);
6111 g
.y
+= 1 - HEIGHT(win
);
6112 g
.w
+= 2 * WIDTH(win
) - 2;
6113 g
.h
+= 2 * HEIGHT(win
) - 2;
6116 constrain_window(win
, &g
, &opts
);
6119 /* Move or resize a window so that flagged side(s) fit into the supplied box. */
6121 constrain_window(struct ws_win
*win
, struct swm_geometry
*b
, int *opts
)
6123 DNPRINTF(SWM_D_MISC
, "constrain_window: win %#x, (x,y) w x h: "
6124 "(%d,%d) %d x %d, box: (x,y) w x h: (%d,%d) %d x %d, rt: %s, "
6125 "lt: %s, bt: %s, tp: %s, allow resize: %s\n", win
->id
, X(win
),
6126 Y(win
), WIDTH(win
), HEIGHT(win
), b
->x
, b
->y
, b
->w
, b
->h
,
6127 YESNO(*opts
& SWM_CW_RIGHT
), YESNO(*opts
& SWM_CW_LEFT
),
6128 YESNO(*opts
& SWM_CW_BOTTOM
), YESNO(*opts
& SWM_CW_TOP
),
6129 YESNO(*opts
& SWM_CW_RESIZABLE
));
6131 if ((*opts
& SWM_CW_RIGHT
) && MAX_X(win
) + BORDER(win
) > b
->x
+ b
->w
) {
6132 if (*opts
& SWM_CW_RESIZABLE
)
6133 WIDTH(win
) = b
->x
+ b
->w
- X(win
) - BORDER(win
);
6135 X(win
) = b
->x
+ b
->w
- WIDTH(win
) - BORDER(win
);
6138 if ((*opts
& SWM_CW_LEFT
) && X(win
) + BORDER(win
) < b
->x
) {
6139 if (*opts
& SWM_CW_RESIZABLE
)
6140 WIDTH(win
) -= b
->x
- X(win
) - BORDER(win
);
6142 X(win
) = b
->x
- BORDER(win
);
6145 if ((*opts
& SWM_CW_BOTTOM
) && MAX_Y(win
) + BORDER(win
) > b
->y
+ b
->h
) {
6146 if (*opts
& SWM_CW_RESIZABLE
)
6147 HEIGHT(win
) = b
->y
+ b
->h
- Y(win
) - BORDER(win
);
6149 Y(win
) = b
->y
+ b
->h
- HEIGHT(win
) - BORDER(win
);
6152 if ((*opts
& SWM_CW_TOP
) && Y(win
) + BORDER(win
) < b
->y
) {
6153 if (*opts
& SWM_CW_RESIZABLE
)
6154 HEIGHT(win
) -= b
->y
- Y(win
) - BORDER(win
);
6156 Y(win
) = b
->y
- BORDER(win
);
6159 if (*opts
& SWM_CW_RESIZABLE
) {
6162 if (HEIGHT(win
) < 1)
6168 update_window_color(struct ws_win
*win
)
6172 if (WS_FOCUSED(win
->ws
) && win
->ws
->focus
== win
)
6173 pixel
= MAXIMIZED(win
) ?
6174 &win
->s
->c
[SWM_S_COLOR_FOCUS_MAXIMIZED
].pixel
:
6175 &win
->s
->c
[SWM_S_COLOR_FOCUS
].pixel
;
6177 pixel
= MAXIMIZED(win
) ?
6178 &win
->s
->c
[SWM_S_COLOR_UNFOCUS_MAXIMIZED
].pixel
:
6179 &win
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
;
6181 xcb_change_window_attributes(conn
, win
->id
,
6182 XCB_CW_BORDER_PIXEL
, pixel
);
6186 update_window(struct ws_win
*win
)
6191 mask
= XCB_CONFIG_WINDOW_X
| XCB_CONFIG_WINDOW_Y
|
6192 XCB_CONFIG_WINDOW_WIDTH
| XCB_CONFIG_WINDOW_HEIGHT
|
6193 XCB_CONFIG_WINDOW_BORDER_WIDTH
;
6197 wc
[3] = HEIGHT(win
);
6198 wc
[4] = BORDER(win
);
6200 DNPRINTF(SWM_D_EVENT
, "update_window: win %#x, (x,y) w x h: "
6201 "(%d,%d) %d x %d, bordered: %s\n", win
->id
, wc
[0], wc
[1], wc
[2],
6202 wc
[3], YESNO(win
->bordered
));
6204 xcb_configure_window(conn
, win
->id
, mask
, wc
);
6207 #define SWM_RESIZE_STEPS (50)
6210 resize(struct ws_win
*win
, union arg
*args
)
6212 xcb_timestamp_t timestamp
= 0;
6213 struct swm_region
*r
= NULL
;
6214 struct swm_geometry g
;
6216 int top
= 0, left
= 0;
6218 xcb_cursor_t cursor
;
6219 xcb_query_pointer_reply_t
*xpr
= NULL
;
6220 xcb_generic_event_t
*evt
;
6221 xcb_motion_notify_event_t
*mne
;
6228 if (FULLSCREEN(win
))
6231 /* In max_stack mode, should only resize transients. */
6232 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
] && !TRANS(win
))
6235 DNPRINTF(SWM_D_EVENT
, "resize: win %#x, floating: %s, "
6236 "transient: %#x\n", win
->id
, YESNO(ABOVE(win
)),
6240 store_float_geom(win
);
6241 else if (!(TRANS(win
) || ABOVE(win
)))
6244 ewmh_apply_flags(win
, (win
->ewmh_flags
| SWM_F_MANUAL
| EWMH_F_ABOVE
) &
6246 ewmh_update_wm_state(win
);
6252 /* It's possible for win to have been freed during focus_flush(). */
6253 if (validate_win(win
)) {
6254 DNPRINTF(SWM_D_EVENT
, "resize: invalid win.\n");
6259 case SWM_ARG_ID_WIDTHSHRINK
:
6260 WIDTH(win
) -= SWM_RESIZE_STEPS
;
6263 case SWM_ARG_ID_WIDTHGROW
:
6264 WIDTH(win
) += SWM_RESIZE_STEPS
;
6267 case SWM_ARG_ID_HEIGHTSHRINK
:
6268 HEIGHT(win
) -= SWM_RESIZE_STEPS
;
6271 case SWM_ARG_ID_HEIGHTGROW
:
6272 HEIGHT(win
) += SWM_RESIZE_STEPS
;
6279 region_containment(win
, r
, SWM_CW_ALLSIDES
| SWM_CW_RESIZABLE
|
6280 SWM_CW_HARDBOUNDARY
);
6282 store_float_geom(win
);
6286 region_containment(win
, r
, SWM_CW_ALLSIDES
| SWM_CW_RESIZABLE
|
6287 SWM_CW_SOFTBOUNDARY
);
6290 /* get cursor offset from window root */
6291 xpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
6298 if (xpr
->win_x
< WIDTH(win
) / 2)
6301 if (xpr
->win_y
< HEIGHT(win
) / 2)
6304 if (args
->id
== SWM_ARG_ID_CENTER
)
6305 cursor
= cursors
[XC_SIZING
].cid
;
6307 cursor
= cursors
[left
? XC_TOP_LEFT_CORNER
:
6308 XC_TOP_RIGHT_CORNER
].cid
;
6310 cursor
= cursors
[left
? XC_BOTTOM_LEFT_CORNER
:
6311 XC_BOTTOM_RIGHT_CORNER
].cid
;
6313 xcb_grab_pointer(conn
, 0, win
->id
, MOUSEMASK
,
6314 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
, XCB_WINDOW_NONE
, cursor
,
6319 while (resizing
&& (evt
= xcb_wait_for_event(conn
))) {
6320 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
6321 case XCB_BUTTON_RELEASE
:
6322 DNPRINTF(SWM_D_EVENT
, "resize: BUTTON_RELEASE\n");
6325 case XCB_MOTION_NOTIFY
:
6326 mne
= (xcb_motion_notify_event_t
*)evt
;
6327 /* cursor offset/delta from start of the operation */
6328 dx
= mne
->root_x
- xpr
->root_x
;
6329 dy
= mne
->root_y
- xpr
->root_y
;
6335 if (args
->id
== SWM_ARG_ID_CENTER
) {
6336 if (g
.h
/ 2 + dy
< 1)
6340 HEIGHT(win
) = g
.h
+ 2 * dy
;
6348 HEIGHT(win
) = g
.h
+ dy
;
6355 if (args
->id
== SWM_ARG_ID_CENTER
) {
6356 if (g
.w
/ 2 + dx
< 1)
6360 WIDTH(win
) = g
.w
+ 2 * dx
;
6368 WIDTH(win
) = g
.w
+ dx
;
6371 /* not free, don't sync more than 120 times / second */
6372 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
6373 timestamp
= mne
->time
;
6374 regionize(win
, mne
->root_x
, mne
->root_y
);
6375 region_containment(win
, r
, SWM_CW_ALLSIDES
|
6376 SWM_CW_RESIZABLE
| SWM_CW_HARDBOUNDARY
|
6377 SWM_CW_SOFTBOUNDARY
);
6384 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
6385 ((xcb_key_press_event_t
*)evt
)->time
);
6391 /* It's possible for win to have been freed above. */
6392 if (validate_win(win
)) {
6393 DNPRINTF(SWM_D_EVENT
, "resize: invalid win.\n");
6401 region_containment(win
, r
, SWM_CW_ALLSIDES
| SWM_CW_RESIZABLE
|
6402 SWM_CW_HARDBOUNDARY
| SWM_CW_SOFTBOUNDARY
);
6406 store_float_geom(win
);
6408 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
6410 DNPRINTF(SWM_D_EVENT
, "resize: done.\n");
6414 resize_step(struct swm_region
*r
, union arg
*args
)
6416 struct ws_win
*win
= NULL
;
6418 if (r
&& r
->ws
&& r
->ws
->focus
)
6428 /* Try to set window region based on supplied coordinates or window center. */
6430 regionize(struct ws_win
*win
, int x
, int y
)
6432 struct swm_region
*r
= NULL
;
6434 r
= region_under(win
->s
, x
, y
);
6436 r
= region_under(win
->s
, X(win
) + WIDTH(win
) / 2,
6437 Y(win
) + HEIGHT(win
) / 2);
6439 if (r
!= NULL
&& r
!= win
->ws
->r
) {
6440 if (clear_maximized(r
->ws
) > 0)
6443 win_to_ws(win
, r
->ws
->idx
, false);
6445 /* Set focus on new ws. */
6446 unfocus_win(r
->ws
->focus
);
6454 #define SWM_MOVE_STEPS (50)
6457 move(struct ws_win
*win
, union arg
*args
)
6459 struct swm_region
*r
;
6460 xcb_timestamp_t timestamp
= 0;
6462 xcb_query_pointer_reply_t
*qpr
= NULL
;
6463 xcb_generic_event_t
*evt
;
6464 xcb_motion_notify_event_t
*mne
;
6465 bool moving
, restack
= false;
6470 if ((r
= win
->ws
->r
) == NULL
)
6473 if (FULLSCREEN(win
))
6476 DNPRINTF(SWM_D_EVENT
, "move: win %#x, floating: %s, transient: "
6477 "%#x\n", win
->id
, YESNO(ABOVE(win
)), win
->transient
);
6479 /* in max_stack mode should only move transients */
6480 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
] && !TRANS(win
))
6483 if (!(ABOVE(win
) || TRANS(win
)) || MAXIMIZED(win
)) {
6484 store_float_geom(win
);
6488 ewmh_apply_flags(win
, (win
->ewmh_flags
| SWM_F_MANUAL
| EWMH_F_ABOVE
) &
6490 ewmh_update_wm_state(win
);
6497 /* It's possible for win to have been freed during focus_flush(). */
6498 if (validate_win(win
)) {
6499 DNPRINTF(SWM_D_EVENT
, "move: invalid win.\n");
6505 case SWM_ARG_ID_MOVELEFT
:
6506 X(win
) -= (SWM_MOVE_STEPS
- border_width
);
6509 case SWM_ARG_ID_MOVERIGHT
:
6510 X(win
) += (SWM_MOVE_STEPS
- border_width
);
6513 case SWM_ARG_ID_MOVEUP
:
6514 Y(win
) -= (SWM_MOVE_STEPS
- border_width
);
6517 case SWM_ARG_ID_MOVEDOWN
:
6518 Y(win
) += (SWM_MOVE_STEPS
- border_width
);
6525 regionize(win
, -1, -1);
6526 region_containment(win
, win
->ws
->r
, SWM_CW_ALLSIDES
);
6528 store_float_geom(win
);
6532 xcb_grab_pointer(conn
, 0, win
->id
, MOUSEMASK
,
6533 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
,
6534 XCB_WINDOW_NONE
, cursors
[XC_FLEUR
].cid
, XCB_CURRENT_TIME
);
6536 /* get cursor offset from window root */
6537 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
6540 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
6544 regionize(win
, qpr
->root_x
, qpr
->root_y
);
6545 region_containment(win
, win
->ws
->r
, SWM_CW_ALLSIDES
|
6546 SWM_CW_SOFTBOUNDARY
);
6550 while (moving
&& (evt
= xcb_wait_for_event(conn
))) {
6551 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
6552 case XCB_BUTTON_RELEASE
:
6553 DNPRINTF(SWM_D_EVENT
, "move: BUTTON_RELEASE\n");
6556 case XCB_MOTION_NOTIFY
:
6557 mne
= (xcb_motion_notify_event_t
*)evt
;
6558 DNPRINTF(SWM_D_EVENT
, "motion: root: %#x\n", mne
->root
);
6559 X(win
) = mne
->root_x
- qpr
->win_x
- border_width
;
6560 Y(win
) = mne
->root_y
- qpr
->win_y
- border_width
;
6562 /* not free, don't sync more than 120 times / second */
6563 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
6564 timestamp
= mne
->time
;
6565 regionize(win
, mne
->root_x
, mne
->root_y
);
6566 region_containment(win
, win
->ws
->r
,
6567 SWM_CW_ALLSIDES
| SWM_CW_SOFTBOUNDARY
);
6574 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
6575 ((xcb_key_press_event_t
*)evt
)->time
);
6581 /* It's possible for win to have been freed above. */
6582 if (validate_win(win
)) {
6583 DNPRINTF(SWM_D_EVENT
, "move: invalid win.\n");
6591 region_containment(win
, win
->ws
->r
, SWM_CW_ALLSIDES
|
6592 SWM_CW_SOFTBOUNDARY
);
6596 store_float_geom(win
);
6598 /* New region set to fullscreen layout. */
6599 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
]) {
6606 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
6607 DNPRINTF(SWM_D_EVENT
, "move: done.\n");
6611 move_step(struct swm_region
*r
, union arg
*args
)
6613 struct ws_win
*win
= NULL
;
6615 if (r
&& r
->ws
&& r
->ws
->focus
)
6620 if (!TRANS(win
) && !ABOVE(win
))
6628 /* key definitions */
6630 char name
[SWM_FUNCNAME_LEN
];
6631 void (*func
)(struct swm_region
*r
, union arg
*);
6633 } keyfuncs
[KF_INVALID
+ 1] = {
6634 /* name function argument */
6635 { "bar_toggle", bar_toggle
, {.id
= SWM_ARG_ID_BAR_TOGGLE
} },
6636 { "bar_toggle_ws", bar_toggle
, {.id
= SWM_ARG_ID_BAR_TOGGLE_WS
} },
6637 { "button2", pressbutton
, {2} },
6638 { "cycle_layout", cycle_layout
, {0} },
6639 { "flip_layout", stack_config
, {.id
= SWM_ARG_ID_FLIPLAYOUT
} },
6640 { "float_toggle", floating_toggle
,{0} },
6641 { "focus_main", focus
, {.id
= SWM_ARG_ID_FOCUSMAIN
} },
6642 { "focus_next", focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
6643 { "focus_prev", focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
6644 { "focus_urgent", focus
, {.id
= SWM_ARG_ID_FOCUSURGENT
} },
6645 { "maximize_toggle", maximize_toggle
,{0} },
6646 { "height_grow", resize_step
, {.id
= SWM_ARG_ID_HEIGHTGROW
} },
6647 { "height_shrink", resize_step
, {.id
= SWM_ARG_ID_HEIGHTSHRINK
} },
6648 { "iconify", iconify
, {0} },
6649 { "master_shrink", stack_config
, {.id
= SWM_ARG_ID_MASTERSHRINK
} },
6650 { "master_grow", stack_config
, {.id
= SWM_ARG_ID_MASTERGROW
} },
6651 { "master_add", stack_config
, {.id
= SWM_ARG_ID_MASTERADD
} },
6652 { "master_del", stack_config
, {.id
= SWM_ARG_ID_MASTERDEL
} },
6653 { "move_down", move_step
, {.id
= SWM_ARG_ID_MOVEDOWN
} },
6654 { "move_left", move_step
, {.id
= SWM_ARG_ID_MOVELEFT
} },
6655 { "move_right", move_step
, {.id
= SWM_ARG_ID_MOVERIGHT
} },
6656 { "move_up", move_step
, {.id
= SWM_ARG_ID_MOVEUP
} },
6657 { "mvrg_1", send_to_rg
, {.id
= 0} },
6658 { "mvrg_2", send_to_rg
, {.id
= 1} },
6659 { "mvrg_3", send_to_rg
, {.id
= 2} },
6660 { "mvrg_4", send_to_rg
, {.id
= 3} },
6661 { "mvrg_5", send_to_rg
, {.id
= 4} },
6662 { "mvrg_6", send_to_rg
, {.id
= 5} },
6663 { "mvrg_7", send_to_rg
, {.id
= 6} },
6664 { "mvrg_8", send_to_rg
, {.id
= 7} },
6665 { "mvrg_9", send_to_rg
, {.id
= 8} },
6666 { "mvws_1", send_to_ws
, {.id
= 0} },
6667 { "mvws_2", send_to_ws
, {.id
= 1} },
6668 { "mvws_3", send_to_ws
, {.id
= 2} },
6669 { "mvws_4", send_to_ws
, {.id
= 3} },
6670 { "mvws_5", send_to_ws
, {.id
= 4} },
6671 { "mvws_6", send_to_ws
, {.id
= 5} },
6672 { "mvws_7", send_to_ws
, {.id
= 6} },
6673 { "mvws_8", send_to_ws
, {.id
= 7} },
6674 { "mvws_9", send_to_ws
, {.id
= 8} },
6675 { "mvws_10", send_to_ws
, {.id
= 9} },
6676 { "mvws_11", send_to_ws
, {.id
= 10} },
6677 { "mvws_12", send_to_ws
, {.id
= 11} },
6678 { "mvws_13", send_to_ws
, {.id
= 12} },
6679 { "mvws_14", send_to_ws
, {.id
= 13} },
6680 { "mvws_15", send_to_ws
, {.id
= 14} },
6681 { "mvws_16", send_to_ws
, {.id
= 15} },
6682 { "mvws_17", send_to_ws
, {.id
= 16} },
6683 { "mvws_18", send_to_ws
, {.id
= 17} },
6684 { "mvws_19", send_to_ws
, {.id
= 18} },
6685 { "mvws_20", send_to_ws
, {.id
= 19} },
6686 { "mvws_21", send_to_ws
, {.id
= 20} },
6687 { "mvws_22", send_to_ws
, {.id
= 21} },
6688 { "name_workspace", name_workspace
, {0} },
6689 { "quit", quit
, {0} },
6690 { "raise_toggle", raise_toggle
, {0} },
6691 { "restart", restart
, {0} },
6692 { "rg_1", focusrg
, {.id
= 0} },
6693 { "rg_2", focusrg
, {.id
= 1} },
6694 { "rg_3", focusrg
, {.id
= 2} },
6695 { "rg_4", focusrg
, {.id
= 3} },
6696 { "rg_5", focusrg
, {.id
= 4} },
6697 { "rg_6", focusrg
, {.id
= 5} },
6698 { "rg_7", focusrg
, {.id
= 6} },
6699 { "rg_8", focusrg
, {.id
= 7} },
6700 { "rg_9", focusrg
, {.id
= 8} },
6701 { "rg_next", cyclerg
, {.id
= SWM_ARG_ID_CYCLERG_UP
} },
6702 { "rg_prev", cyclerg
, {.id
= SWM_ARG_ID_CYCLERG_DOWN
} },
6703 { "screen_next", cyclerg
, {.id
= SWM_ARG_ID_CYCLERG_UP
} },
6704 { "screen_prev", cyclerg
, {.id
= SWM_ARG_ID_CYCLERG_DOWN
} },
6705 { "search_win", search_win
, {0} },
6706 { "search_workspace", search_workspace
, {0} },
6707 { "spawn_custom", NULL
, {0} },
6708 { "stack_inc", stack_config
, {.id
= SWM_ARG_ID_STACKINC
} },
6709 { "stack_dec", stack_config
, {.id
= SWM_ARG_ID_STACKDEC
} },
6710 { "stack_reset", stack_config
, {.id
= SWM_ARG_ID_STACKRESET
} },
6711 { "swap_main", swapwin
, {.id
= SWM_ARG_ID_SWAPMAIN
} },
6712 { "swap_next", swapwin
, {.id
= SWM_ARG_ID_SWAPNEXT
} },
6713 { "swap_prev", swapwin
, {.id
= SWM_ARG_ID_SWAPPREV
} },
6714 { "uniconify", uniconify
, {0} },
6715 { "version", version
, {0} },
6716 { "width_grow", resize_step
, {.id
= SWM_ARG_ID_WIDTHGROW
} },
6717 { "width_shrink", resize_step
, {.id
= SWM_ARG_ID_WIDTHSHRINK
} },
6718 { "wind_del", wkill
, {.id
= SWM_ARG_ID_DELETEWINDOW
} },
6719 { "wind_kill", wkill
, {.id
= SWM_ARG_ID_KILLWINDOW
} },
6720 { "ws_1", switchws
, {.id
= 0} },
6721 { "ws_2", switchws
, {.id
= 1} },
6722 { "ws_3", switchws
, {.id
= 2} },
6723 { "ws_4", switchws
, {.id
= 3} },
6724 { "ws_5", switchws
, {.id
= 4} },
6725 { "ws_6", switchws
, {.id
= 5} },
6726 { "ws_7", switchws
, {.id
= 6} },
6727 { "ws_8", switchws
, {.id
= 7} },
6728 { "ws_9", switchws
, {.id
= 8} },
6729 { "ws_10", switchws
, {.id
= 9} },
6730 { "ws_11", switchws
, {.id
= 10} },
6731 { "ws_12", switchws
, {.id
= 11} },
6732 { "ws_13", switchws
, {.id
= 12} },
6733 { "ws_14", switchws
, {.id
= 13} },
6734 { "ws_15", switchws
, {.id
= 14} },
6735 { "ws_16", switchws
, {.id
= 15} },
6736 { "ws_17", switchws
, {.id
= 16} },
6737 { "ws_18", switchws
, {.id
= 17} },
6738 { "ws_19", switchws
, {.id
= 18} },
6739 { "ws_20", switchws
, {.id
= 19} },
6740 { "ws_21", switchws
, {.id
= 20} },
6741 { "ws_22", switchws
, {.id
= 21} },
6742 { "ws_next", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP
} },
6743 { "ws_next_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP_ALL
} },
6744 { "ws_next_move", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_MOVE_UP
} },
6745 { "ws_prev", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN
} },
6746 { "ws_prev_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN_ALL
} },
6747 { "ws_prev_move", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_MOVE_DOWN
} },
6748 { "ws_prior", priorws
, {0} },
6749 { "dumpwins", dumpwins
, {0} }, /* MUST BE LAST */
6750 { "invalid key func", NULL
, {0} },
6754 key_cmp(struct key
*kp1
, struct key
*kp2
)
6756 if (kp1
->keysym
< kp2
->keysym
)
6758 if (kp1
->keysym
> kp2
->keysym
)
6761 if (kp1
->mod
< kp2
->mod
)
6763 if (kp1
->mod
> kp2
->mod
)
6770 enum { client_click
, root_click
};
6772 unsigned int action
;
6774 unsigned int button
;
6775 void (*func
)(struct ws_win
*, union arg
*);
6778 #define MODKEY_SHIFT MODKEY | XCB_MOD_MASK_SHIFT
6779 /* action key mouse button func args */
6780 { client_click
, MODKEY
, XCB_BUTTON_INDEX_3
, resize
, {.id
= SWM_ARG_ID_DONTCENTER
} },
6781 { client_click
, MODKEY_SHIFT
, XCB_BUTTON_INDEX_3
, resize
, {.id
= SWM_ARG_ID_CENTER
} },
6782 { client_click
, MODKEY
, XCB_BUTTON_INDEX_1
, move
, {0} },
6787 update_modkey(unsigned int mod
)
6793 RB_FOREACH(kp
, key_tree
, &keys
)
6794 if (kp
->mod
& XCB_MOD_MASK_SHIFT
)
6795 kp
->mod
= mod
| XCB_MOD_MASK_SHIFT
;
6799 for (i
= 0; i
< LENGTH(buttons
); i
++)
6800 if (buttons
[i
].mask
& XCB_MOD_MASK_SHIFT
)
6801 buttons
[i
].mask
= mod
| XCB_MOD_MASK_SHIFT
;
6803 buttons
[i
].mask
= mod
;
6807 update_mousebutton(unsigned char type
, unsigned int but
)
6813 mouse_button_move
= but
;
6816 mouse_button_resize
= but
;
6820 for (i
= 0; i
< LENGTH(buttons
); i
++) {
6821 if (buttons
[i
].func
== move
)
6822 buttons
[i
].button
= mouse_button_move
;
6824 if (buttons
[i
].func
== resize
)
6825 buttons
[i
].button
= mouse_button_resize
;
6832 spawn_expand(struct swm_region
*r
, union arg
*args
, const char *spawn_name
,
6835 struct spawn_prog
*prog
= NULL
;
6837 char *ap
, **real_args
;
6839 /* suppress unused warning since var is needed */
6842 DNPRINTF(SWM_D_SPAWN
, "spawn_expand: %s\n", spawn_name
);
6845 TAILQ_FOREACH(prog
, &spawns
, entry
) {
6846 if (strcasecmp(spawn_name
, prog
->name
) == 0)
6850 warnx("spawn_custom: program %s not found", spawn_name
);
6854 /* make room for expanded args */
6855 if ((real_args
= calloc(prog
->argc
+ 1, sizeof(char *))) == NULL
)
6856 err(1, "spawn_custom: calloc real_args");
6858 /* expand spawn_args into real_args */
6859 for (i
= c
= 0; i
< prog
->argc
; i
++) {
6861 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: raw arg: %s\n", ap
);
6862 if (strcasecmp(ap
, "$bar_border") == 0) {
6864 strdup(r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].name
))
6866 err(1, "spawn_custom border color");
6867 } else if (strcasecmp(ap
, "$bar_color") == 0) {
6869 strdup(r
->s
->c
[SWM_S_COLOR_BAR
].name
))
6871 err(1, "spawn_custom bar color");
6872 } else if (strcasecmp(ap
, "$bar_font") == 0) {
6873 if ((real_args
[c
] = strdup(bar_fonts
))
6875 err(1, "spawn_custom bar fonts");
6876 } else if (strcasecmp(ap
, "$bar_font_color") == 0) {
6878 strdup(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].name
))
6880 err(1, "spawn_custom color font");
6881 } else if (strcasecmp(ap
, "$color_focus") == 0) {
6883 strdup(r
->s
->c
[SWM_S_COLOR_FOCUS
].name
))
6885 err(1, "spawn_custom color focus");
6886 } else if (strcasecmp(ap
, "$color_focus_maximized") == 0) {
6888 strdup(r
->s
->c
[SWM_S_COLOR_FOCUS_MAXIMIZED
].name
))
6890 err(1, "spawn_custom color focus maximized");
6891 } else if (strcasecmp(ap
, "$color_unfocus") == 0) {
6893 strdup(r
->s
->c
[SWM_S_COLOR_UNFOCUS
].name
))
6895 err(1, "spawn_custom color unfocus");
6896 } else if (strcasecmp(ap
, "$color_unfocus_maximized") == 0) {
6898 strdup(r
->s
->c
[SWM_S_COLOR_UNFOCUS_MAXIMIZED
].name
))
6900 err(1, "spawn_custom color unfocus maximized");
6901 } else if (strcasecmp(ap
, "$region_index") == 0) {
6902 if (asprintf(&real_args
[c
], "%d",
6903 get_region_index(r
) + 1) < 1)
6904 err(1, "spawn_custom region index");
6905 } else if (strcasecmp(ap
, "$workspace_index") == 0) {
6906 if (asprintf(&real_args
[c
], "%d", r
->ws
->idx
+ 1) < 1)
6907 err(1, "spawn_custom workspace index");
6908 } else if (strcasecmp(ap
, "$dmenu_bottom") == 0) {
6911 if ((real_args
[c
] = strdup("-b")) == NULL
)
6912 err(1, "spawn_custom workspace index");
6914 /* no match --> copy as is */
6915 if ((real_args
[c
] = strdup(ap
)) == NULL
)
6916 err(1, "spawn_custom strdup(ap)");
6918 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: cooked arg: %s\n",
6924 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: result: ");
6925 for (i
= 0; i
< c
; ++i
)
6926 DPRINTF("\"%s\" ", real_args
[i
]);
6929 *ret_args
= real_args
;
6934 spawn_custom(struct swm_region
*r
, union arg
*args
, const char *spawn_name
)
6940 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
6944 spawn(r
->ws
->idx
, &a
, true);
6946 for (i
= 0; i
< spawn_argc
; i
++)
6952 spawn_select(struct swm_region
*r
, union arg
*args
, const char *spawn_name
,
6959 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
6963 if (pipe(select_list_pipe
) == -1)
6964 err(1, "pipe error");
6965 if (pipe(select_resp_pipe
) == -1)
6966 err(1, "pipe error");
6968 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
6969 err(1, "could not disable SIGPIPE");
6970 switch (*pid
= fork()) {
6972 err(1, "cannot fork");
6975 if (dup2(select_list_pipe
[0], STDIN_FILENO
) == -1)
6977 if (dup2(select_resp_pipe
[1], STDOUT_FILENO
) == -1)
6979 close(select_list_pipe
[1]);
6980 close(select_resp_pipe
[0]);
6981 spawn(r
->ws
->idx
, &a
, false);
6983 default: /* parent */
6984 close(select_list_pipe
[0]);
6985 close(select_resp_pipe
[1]);
6989 for (i
= 0; i
< spawn_argc
; i
++)
6994 /* Argument tokenizer. */
6997 char *arg
, *cp
, *next
;
6998 bool single_quoted
= false, double_quoted
= false;
7003 /* Eat and move characters until end of argument is found. */
7004 for (arg
= next
= cp
= *sp
; *cp
!= '\0'; ++cp
) {
7005 if (!double_quoted
&& *cp
== '\'') {
7006 /* Eat single-quote. */
7007 single_quoted
= !single_quoted
;
7008 } else if (!single_quoted
&& *cp
== '"') {
7009 /* Eat double-quote. */
7010 double_quoted
= !double_quoted
;
7011 } else if (!single_quoted
&& *cp
== '\\' && *(cp
+ 1) == '"') {
7012 /* Eat backslash; copy escaped character to arg. */
7014 } else if (!single_quoted
&& !double_quoted
&& *cp
== '\\' &&
7015 (*(cp
+ 1) == '\'' || *(cp
+ 1) == ' ')) {
7016 /* Eat backslash; move escaped character. */
7018 } else if (!single_quoted
&& !double_quoted
&&
7019 (*cp
== ' ' || *cp
== '\t')) {
7020 /* Terminate argument. */
7022 /* Point sp to beginning of next argument. */
7026 /* Move regular character. */
7031 /* Terminate argument if end of string. */
7041 spawn_insert(const char *name
, const char *args
, int flags
)
7043 struct spawn_prog
*sp
;
7044 char *arg
, *cp
, *ptr
;
7046 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: %s[%s]\n", name
, args
);
7048 if (args
== NULL
|| *args
== '\0')
7051 if ((sp
= calloc(1, sizeof *sp
)) == NULL
)
7052 err(1, "spawn_insert: calloc");
7053 if ((sp
->name
= strdup(name
)) == NULL
)
7054 err(1, "spawn_insert: strdup");
7056 /* Convert the arguments to an argument list. */
7057 if ((ptr
= cp
= strdup(args
)) == NULL
)
7058 err(1, "spawn_insert: strdup");
7059 while ((arg
= argsep(&ptr
)) != NULL
) {
7060 /* Null argument; skip it. */
7065 if ((sp
->argv
= realloc(sp
->argv
, sp
->argc
*
7066 sizeof *sp
->argv
)) == NULL
)
7067 err(1, "spawn_insert: realloc");
7068 if ((sp
->argv
[sp
->argc
- 1] = strdup(arg
)) == NULL
)
7069 err(1, "spawn_insert: strdup");
7075 DNPRINTF(SWM_D_SPAWN
, "arg %d: [%s]\n", sp
->argc
, sp
->argv
[sp
->argc
-1]);
7076 TAILQ_INSERT_TAIL(&spawns
, sp
, entry
);
7077 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: leave\n");
7081 spawn_remove(struct spawn_prog
*sp
)
7085 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: %s\n", sp
->name
);
7087 TAILQ_REMOVE(&spawns
, sp
, entry
);
7088 for (i
= 0; i
< sp
->argc
; i
++)
7094 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: leave\n");
7098 spawn_find(const char *name
)
7100 struct spawn_prog
*sp
;
7102 TAILQ_FOREACH(sp
, &spawns
, entry
)
7103 if (strcasecmp(sp
->name
, name
) == 0)
7110 setspawn(const char *name
, const char *args
, int flags
)
7112 struct spawn_prog
*sp
;
7114 DNPRINTF(SWM_D_SPAWN
, "setspawn: %s\n", name
);
7119 /* Remove any old spawn under the same name. */
7120 if ((sp
= spawn_find(name
)) != NULL
)
7124 spawn_insert(name
, args
, flags
);
7126 warnx("error: setspawn: cannot find program: %s", name
);
7128 DNPRINTF(SWM_D_SPAWN
, "setspawn: leave\n");
7132 setconfspawn(const char *selector
, const char *value
, int flags
)
7136 if (selector
== NULL
|| strlen(selector
) == 0)
7139 args
= expand_tilde(value
);
7141 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: [%s] [%s]\n", selector
, args
);
7143 setspawn(selector
, args
, flags
);
7146 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: done.\n");
7151 validate_spawns(void)
7153 struct spawn_prog
*sp
;
7154 char which
[PATH_MAX
];
7159 RB_FOREACH(kp
, key_tree
, &keys
) {
7160 if (kp
->funcid
!= KF_SPAWN_CUSTOM
)
7164 sp
= spawn_find(kp
->spawn_name
);
7165 if (sp
== NULL
|| sp
->flags
& SWM_SPAWN_OPTIONAL
)
7168 /* verify we have the goods */
7169 snprintf(which
, sizeof which
, "which %s", sp
->argv
[0]);
7170 DNPRINTF(SWM_D_CONF
, "validate_spawns: which %s\n",
7172 for (i
= strlen("which "); i
< strlen(which
); i
++)
7173 if (which
[i
] == ' ') {
7177 if (system(which
) != 0)
7178 add_startup_exception("could not find %s",
7179 &which
[strlen("which ")]);
7186 setconfspawn("lock", "xlock", 0);
7188 setconfspawn("term", "xterm", 0);
7189 setconfspawn("spawn_term", "xterm", 0);
7191 setconfspawn("menu", "dmenu_run"
7195 " -nf $bar_font_color"
7197 " -sf $bar_color", 0);
7199 setconfspawn("search", "dmenu"
7204 " -nf $bar_font_color"
7206 " -sf $bar_color", 0);
7208 setconfspawn("name_workspace", "dmenu"
7213 " -nf $bar_font_color"
7215 " -sf $bar_color", 0);
7217 /* These are not verified for existence, even with a binding set. */
7218 setconfspawn("screenshot_all", "screenshot.sh full", SWM_SPAWN_OPTIONAL
);
7219 setconfspawn("screenshot_wind", "screenshot.sh window", SWM_SPAWN_OPTIONAL
);
7220 setconfspawn("initscr", "initscreen.sh", SWM_SPAWN_OPTIONAL
);
7224 #define SWM_MODNAME_SIZE 32
7225 #define SWM_KEY_WS "\n+ \t"
7227 parsekeys(const char *keystr
, unsigned int currmod
, unsigned int *mod
, KeySym
*ks
)
7229 char *str
, *cp
, *name
;
7232 DNPRINTF(SWM_D_KEY
, "parsekeys: enter [%s]\n", keystr
);
7233 if (mod
== NULL
|| ks
== NULL
) {
7234 DNPRINTF(SWM_D_KEY
, "parsekeys: no mod or key vars\n");
7237 if (keystr
== NULL
|| strlen(keystr
) == 0) {
7238 DNPRINTF(SWM_D_KEY
, "parsekeys: no keystr\n");
7242 if ((cp
= str
= strdup(keystr
)) == NULL
)
7243 err(1, "parsekeys: strdup");
7245 *ks
= XCB_NO_SYMBOL
;
7247 while ((name
= strsep(&cp
, SWM_KEY_WS
)) != NULL
) {
7248 DNPRINTF(SWM_D_KEY
, "parsekeys: key [%s]\n", name
);
7250 cp
+= (long)strspn(cp
, SWM_KEY_WS
);
7251 if (strncasecmp(name
, "MOD", SWM_MODNAME_SIZE
) == 0)
7253 else if (strncasecmp(name
, "Mod1", SWM_MODNAME_SIZE
) == 0)
7254 *mod
|= XCB_MOD_MASK_1
;
7255 else if (strncasecmp(name
, "Mod2", SWM_MODNAME_SIZE
) == 0)
7256 *mod
+= XCB_MOD_MASK_2
;
7257 else if (strncmp(name
, "Mod3", SWM_MODNAME_SIZE
) == 0)
7258 *mod
|= XCB_MOD_MASK_3
;
7259 else if (strncmp(name
, "Mod4", SWM_MODNAME_SIZE
) == 0)
7260 *mod
|= XCB_MOD_MASK_4
;
7261 else if (strncmp(name
, "Mod5", SWM_MODNAME_SIZE
) == 0)
7262 *mod
|= XCB_MOD_MASK_5
;
7263 else if (strncasecmp(name
, "SHIFT", SWM_MODNAME_SIZE
) == 0)
7264 *mod
|= XCB_MOD_MASK_SHIFT
;
7265 else if (strncasecmp(name
, "CONTROL", SWM_MODNAME_SIZE
) == 0)
7266 *mod
|= XCB_MOD_MASK_CONTROL
;
7268 *ks
= XStringToKeysym(name
);
7269 XConvertCase(*ks
, ks
, &uks
);
7270 if (ks
== XCB_NO_SYMBOL
) {
7272 "parsekeys: invalid key %s\n",
7281 DNPRINTF(SWM_D_KEY
, "parsekeys: leave ok\n");
7286 strdupsafe(const char *str
)
7291 return (strdup(str
));
7295 key_insert(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
7296 const char *spawn_name
)
7300 DNPRINTF(SWM_D_KEY
, "key_insert: enter %s [%s]\n",
7301 keyfuncs
[kfid
].name
, spawn_name
);
7303 if ((kp
= malloc(sizeof *kp
)) == NULL
)
7304 err(1, "key_insert: malloc");
7309 kp
->spawn_name
= strdupsafe(spawn_name
);
7310 RB_INSERT(key_tree
, &keys
, kp
);
7312 DNPRINTF(SWM_D_KEY
, "key_insert: leave\n");
7316 key_lookup(unsigned int mod
, KeySym ks
)
7323 return (RB_FIND(key_tree
, &keys
, &kp
));
7327 key_remove(struct key
*kp
)
7329 DNPRINTF(SWM_D_KEY
, "key_remove: %s\n", keyfuncs
[kp
->funcid
].name
);
7331 RB_REMOVE(key_tree
, &keys
, kp
);
7332 free(kp
->spawn_name
);
7335 DNPRINTF(SWM_D_KEY
, "key_remove: leave\n");
7339 key_replace(struct key
*kp
, unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
7340 const char *spawn_name
)
7342 DNPRINTF(SWM_D_KEY
, "key_replace: %s [%s]\n", keyfuncs
[kp
->funcid
].name
,
7346 key_insert(mod
, ks
, kfid
, spawn_name
);
7348 DNPRINTF(SWM_D_KEY
, "key_replace: leave\n");
7352 setkeybinding(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
7353 const char *spawn_name
)
7357 DNPRINTF(SWM_D_KEY
, "setkeybinding: enter %s [%s]\n",
7358 keyfuncs
[kfid
].name
, spawn_name
);
7360 if ((kp
= key_lookup(mod
, ks
)) != NULL
) {
7361 if (kfid
== KF_INVALID
)
7364 key_replace(kp
, mod
, ks
, kfid
, spawn_name
);
7365 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
7368 if (kfid
== KF_INVALID
) {
7369 warnx("bind: Key combination already unbound.");
7370 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
7374 key_insert(mod
, ks
, kfid
, spawn_name
);
7375 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
7379 setconfbinding(const char *selector
, const char *value
, int flags
)
7381 enum keyfuncid kfid
;
7384 struct spawn_prog
*sp
;
7386 /* suppress unused warning since var is needed */
7389 DNPRINTF(SWM_D_KEY
, "setconfbinding: enter selector: [%s], "
7390 "value: [%s]\n", selector
, value
);
7391 if (selector
== NULL
|| strlen(selector
) == 0) {
7392 DNPRINTF(SWM_D_KEY
, "setconfbinding: unbind %s\n", value
);
7393 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
7395 setkeybinding(mod
, ks
, kfid
, NULL
);
7400 /* search by key function name */
7401 for (kfid
= 0; kfid
< KF_INVALID
; (kfid
)++) {
7402 if (strncasecmp(selector
, keyfuncs
[kfid
].name
,
7403 SWM_FUNCNAME_LEN
) == 0) {
7404 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match "
7405 "keyfunc\n", selector
);
7406 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
7407 setkeybinding(mod
, ks
, kfid
, NULL
);
7413 /* search by custom spawn name */
7414 if ((sp
= spawn_find(selector
)) != NULL
) {
7415 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match "
7416 "spawn\n", selector
);
7417 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
7418 setkeybinding(mod
, ks
, KF_SPAWN_CUSTOM
,
7424 DNPRINTF(SWM_D_KEY
, "setconfbinding: no match\n");
7431 #define MODKEY_SHIFT MODKEY | XCB_MOD_MASK_SHIFT
7432 setkeybinding(MODKEY
, XK_b
, KF_BAR_TOGGLE
, NULL
);
7433 setkeybinding(MODKEY_SHIFT
, XK_b
, KF_BAR_TOGGLE_WS
,NULL
);
7434 setkeybinding(MODKEY
, XK_v
, KF_BUTTON2
, NULL
);
7435 setkeybinding(MODKEY
, XK_space
, KF_CYCLE_LAYOUT
,NULL
);
7436 setkeybinding(MODKEY_SHIFT
, XK_backslash
, KF_FLIP_LAYOUT
, NULL
);
7437 setkeybinding(MODKEY
, XK_t
, KF_FLOAT_TOGGLE
,NULL
);
7438 setkeybinding(MODKEY
, XK_m
, KF_FOCUS_MAIN
, NULL
);
7439 setkeybinding(MODKEY
, XK_j
, KF_FOCUS_NEXT
, NULL
);
7440 setkeybinding(MODKEY
, XK_Tab
, KF_FOCUS_NEXT
, NULL
);
7441 setkeybinding(MODKEY
, XK_k
, KF_FOCUS_PREV
, NULL
);
7442 setkeybinding(MODKEY_SHIFT
, XK_Tab
, KF_FOCUS_PREV
, NULL
);
7443 setkeybinding(MODKEY
, XK_u
, KF_FOCUS_URGENT
,NULL
);
7444 setkeybinding(MODKEY
, XK_e
, KF_MAXIMIZE_TOGGLE
,NULL
);
7445 setkeybinding(MODKEY_SHIFT
, XK_equal
, KF_HEIGHT_GROW
,NULL
);
7446 setkeybinding(MODKEY_SHIFT
, XK_minus
, KF_HEIGHT_SHRINK
,NULL
);
7447 setkeybinding(MODKEY
, XK_w
, KF_ICONIFY
, NULL
);
7448 setkeybinding(MODKEY
, XK_h
, KF_MASTER_SHRINK
, NULL
);
7449 setkeybinding(MODKEY
, XK_l
, KF_MASTER_GROW
, NULL
);
7450 setkeybinding(MODKEY
, XK_comma
, KF_MASTER_ADD
, NULL
);
7451 setkeybinding(MODKEY
, XK_period
, KF_MASTER_DEL
, NULL
);
7452 setkeybinding(MODKEY_SHIFT
, XK_bracketright
,KF_MOVE_DOWN
,NULL
);
7453 setkeybinding(MODKEY
, XK_bracketleft
, KF_MOVE_LEFT
,NULL
);
7454 setkeybinding(MODKEY
, XK_bracketright
,KF_MOVE_RIGHT
,NULL
);
7455 setkeybinding(MODKEY_SHIFT
, XK_bracketleft
, KF_MOVE_UP
, NULL
);
7456 setkeybinding(MODKEY_SHIFT
, XK_KP_End
, KF_MVRG_1
, NULL
);
7457 setkeybinding(MODKEY_SHIFT
, XK_KP_Down
, KF_MVRG_2
, NULL
);
7458 setkeybinding(MODKEY_SHIFT
, XK_KP_Next
, KF_MVRG_3
, NULL
);
7459 setkeybinding(MODKEY_SHIFT
, XK_KP_Left
, KF_MVRG_4
, NULL
);
7460 setkeybinding(MODKEY_SHIFT
, XK_KP_Begin
, KF_MVRG_5
, NULL
);
7461 setkeybinding(MODKEY_SHIFT
, XK_KP_Right
, KF_MVRG_6
, NULL
);
7462 setkeybinding(MODKEY_SHIFT
, XK_KP_Home
, KF_MVRG_7
, NULL
);
7463 setkeybinding(MODKEY_SHIFT
, XK_KP_Up
, KF_MVRG_8
, NULL
);
7464 setkeybinding(MODKEY_SHIFT
, XK_KP_Prior
, KF_MVRG_9
, NULL
);
7465 setkeybinding(MODKEY_SHIFT
, XK_1
, KF_MVWS_1
, NULL
);
7466 setkeybinding(MODKEY_SHIFT
, XK_2
, KF_MVWS_2
, NULL
);
7467 setkeybinding(MODKEY_SHIFT
, XK_3
, KF_MVWS_3
, NULL
);
7468 setkeybinding(MODKEY_SHIFT
, XK_4
, KF_MVWS_4
, NULL
);
7469 setkeybinding(MODKEY_SHIFT
, XK_5
, KF_MVWS_5
, NULL
);
7470 setkeybinding(MODKEY_SHIFT
, XK_6
, KF_MVWS_6
, NULL
);
7471 setkeybinding(MODKEY_SHIFT
, XK_7
, KF_MVWS_7
, NULL
);
7472 setkeybinding(MODKEY_SHIFT
, XK_8
, KF_MVWS_8
, NULL
);
7473 setkeybinding(MODKEY_SHIFT
, XK_9
, KF_MVWS_9
, NULL
);
7474 setkeybinding(MODKEY_SHIFT
, XK_0
, KF_MVWS_10
, NULL
);
7475 setkeybinding(MODKEY_SHIFT
, XK_F1
, KF_MVWS_11
, NULL
);
7476 setkeybinding(MODKEY_SHIFT
, XK_F2
, KF_MVWS_12
, NULL
);
7477 setkeybinding(MODKEY_SHIFT
, XK_F3
, KF_MVWS_13
, NULL
);
7478 setkeybinding(MODKEY_SHIFT
, XK_F4
, KF_MVWS_14
, NULL
);
7479 setkeybinding(MODKEY_SHIFT
, XK_F5
, KF_MVWS_15
, NULL
);
7480 setkeybinding(MODKEY_SHIFT
, XK_F6
, KF_MVWS_16
, NULL
);
7481 setkeybinding(MODKEY_SHIFT
, XK_F7
, KF_MVWS_17
, NULL
);
7482 setkeybinding(MODKEY_SHIFT
, XK_F8
, KF_MVWS_18
, NULL
);
7483 setkeybinding(MODKEY_SHIFT
, XK_F9
, KF_MVWS_19
, NULL
);
7484 setkeybinding(MODKEY_SHIFT
, XK_F10
, KF_MVWS_20
, NULL
);
7485 setkeybinding(MODKEY_SHIFT
, XK_F11
, KF_MVWS_21
, NULL
);
7486 setkeybinding(MODKEY_SHIFT
, XK_F12
, KF_MVWS_22
, NULL
);
7487 setkeybinding(MODKEY_SHIFT
, XK_slash
, KF_NAME_WORKSPACE
,NULL
);
7488 setkeybinding(MODKEY_SHIFT
, XK_q
, KF_QUIT
, NULL
);
7489 setkeybinding(MODKEY_SHIFT
, XK_r
, KF_RAISE_TOGGLE
,NULL
);
7490 setkeybinding(MODKEY
, XK_q
, KF_RESTART
, NULL
);
7491 setkeybinding(MODKEY
, XK_KP_End
, KF_RG_1
, NULL
);
7492 setkeybinding(MODKEY
, XK_KP_Down
, KF_RG_2
, NULL
);
7493 setkeybinding(MODKEY
, XK_KP_Next
, KF_RG_3
, NULL
);
7494 setkeybinding(MODKEY
, XK_KP_Left
, KF_RG_4
, NULL
);
7495 setkeybinding(MODKEY
, XK_KP_Begin
, KF_RG_5
, NULL
);
7496 setkeybinding(MODKEY
, XK_KP_Right
, KF_RG_6
, NULL
);
7497 setkeybinding(MODKEY
, XK_KP_Home
, KF_RG_7
, NULL
);
7498 setkeybinding(MODKEY
, XK_KP_Up
, KF_RG_8
, NULL
);
7499 setkeybinding(MODKEY
, XK_KP_Prior
, KF_RG_9
, NULL
);
7500 setkeybinding(MODKEY_SHIFT
, XK_Right
, KF_RG_NEXT
, NULL
);
7501 setkeybinding(MODKEY_SHIFT
, XK_Left
, KF_RG_PREV
, NULL
);
7502 setkeybinding(MODKEY
, XK_f
, KF_SEARCH_WIN
, NULL
);
7503 setkeybinding(MODKEY
, XK_slash
, KF_SEARCH_WORKSPACE
,NULL
);
7504 setkeybinding(MODKEY_SHIFT
, XK_i
, KF_SPAWN_CUSTOM
,"initscr");
7505 setkeybinding(MODKEY_SHIFT
, XK_Delete
, KF_SPAWN_CUSTOM
,"lock");
7506 setkeybinding(MODKEY
, XK_p
, KF_SPAWN_CUSTOM
,"menu");
7507 setkeybinding(MODKEY
, XK_s
, KF_SPAWN_CUSTOM
,"screenshot_all");
7508 setkeybinding(MODKEY_SHIFT
, XK_s
, KF_SPAWN_CUSTOM
,"screenshot_wind");
7509 setkeybinding(MODKEY_SHIFT
, XK_Return
, KF_SPAWN_CUSTOM
,"term");
7510 setkeybinding(MODKEY_SHIFT
, XK_comma
, KF_STACK_INC
, NULL
);
7511 setkeybinding(MODKEY_SHIFT
, XK_period
, KF_STACK_DEC
, NULL
);
7512 setkeybinding(MODKEY_SHIFT
, XK_space
, KF_STACK_RESET
, NULL
);
7513 setkeybinding(MODKEY
, XK_Return
, KF_SWAP_MAIN
, NULL
);
7514 setkeybinding(MODKEY_SHIFT
, XK_j
, KF_SWAP_NEXT
, NULL
);
7515 setkeybinding(MODKEY_SHIFT
, XK_k
, KF_SWAP_PREV
, NULL
);
7516 setkeybinding(MODKEY_SHIFT
, XK_w
, KF_UNICONIFY
, NULL
);
7517 setkeybinding(MODKEY_SHIFT
, XK_v
, KF_VERSION
, NULL
);
7518 setkeybinding(MODKEY
, XK_equal
, KF_WIDTH_GROW
, NULL
);
7519 setkeybinding(MODKEY
, XK_minus
, KF_WIDTH_SHRINK
,NULL
);
7520 setkeybinding(MODKEY
, XK_x
, KF_WIND_DEL
, NULL
);
7521 setkeybinding(MODKEY_SHIFT
, XK_x
, KF_WIND_KILL
, NULL
);
7522 setkeybinding(MODKEY
, XK_1
, KF_WS_1
, NULL
);
7523 setkeybinding(MODKEY
, XK_2
, KF_WS_2
, NULL
);
7524 setkeybinding(MODKEY
, XK_3
, KF_WS_3
, NULL
);
7525 setkeybinding(MODKEY
, XK_4
, KF_WS_4
, NULL
);
7526 setkeybinding(MODKEY
, XK_5
, KF_WS_5
, NULL
);
7527 setkeybinding(MODKEY
, XK_6
, KF_WS_6
, NULL
);
7528 setkeybinding(MODKEY
, XK_7
, KF_WS_7
, NULL
);
7529 setkeybinding(MODKEY
, XK_8
, KF_WS_8
, NULL
);
7530 setkeybinding(MODKEY
, XK_9
, KF_WS_9
, NULL
);
7531 setkeybinding(MODKEY
, XK_0
, KF_WS_10
, NULL
);
7532 setkeybinding(MODKEY
, XK_F1
, KF_WS_11
, NULL
);
7533 setkeybinding(MODKEY
, XK_F2
, KF_WS_12
, NULL
);
7534 setkeybinding(MODKEY
, XK_F3
, KF_WS_13
, NULL
);
7535 setkeybinding(MODKEY
, XK_F4
, KF_WS_14
, NULL
);
7536 setkeybinding(MODKEY
, XK_F5
, KF_WS_15
, NULL
);
7537 setkeybinding(MODKEY
, XK_F6
, KF_WS_16
, NULL
);
7538 setkeybinding(MODKEY
, XK_F7
, KF_WS_17
, NULL
);
7539 setkeybinding(MODKEY
, XK_F8
, KF_WS_18
, NULL
);
7540 setkeybinding(MODKEY
, XK_F9
, KF_WS_19
, NULL
);
7541 setkeybinding(MODKEY
, XK_F10
, KF_WS_20
, NULL
);
7542 setkeybinding(MODKEY
, XK_F11
, KF_WS_21
, NULL
);
7543 setkeybinding(MODKEY
, XK_F12
, KF_WS_22
, NULL
);
7544 setkeybinding(MODKEY
, XK_Right
, KF_WS_NEXT
, NULL
);
7545 setkeybinding(MODKEY
, XK_Left
, KF_WS_PREV
, NULL
);
7546 setkeybinding(MODKEY
, XK_Up
, KF_WS_NEXT_ALL
, NULL
);
7547 setkeybinding(MODKEY
, XK_Down
, KF_WS_PREV_ALL
, NULL
);
7548 setkeybinding(MODKEY_SHIFT
, XK_Up
, KF_WS_NEXT_MOVE
,NULL
);
7549 setkeybinding(MODKEY_SHIFT
, XK_Down
, KF_WS_PREV_MOVE
,NULL
);
7550 setkeybinding(MODKEY
, XK_a
, KF_WS_PRIOR
, NULL
);
7552 setkeybinding(MODKEY_SHIFT
, XK_d
, KF_DUMPWINS
, NULL
);
7562 while (RB_EMPTY(&keys
) == 0) {
7563 kp
= RB_ROOT(&keys
);
7569 setkeymapping(const char *selector
, const char *value
, int flags
)
7571 char *keymapping_file
;
7573 /* suppress unused warnings since vars are needed */
7577 DNPRINTF(SWM_D_KEY
, "setkeymapping: enter\n");
7579 keymapping_file
= expand_tilde(value
);
7582 /* load new key bindings; if it fails, revert to default bindings */
7583 if (conf_load(keymapping_file
, SWM_CONF_KEYMAPPING
)) {
7588 free(keymapping_file
);
7590 DNPRINTF(SWM_D_KEY
, "setkeymapping: leave\n");
7595 updatenumlockmask(void)
7598 xcb_get_modifier_mapping_reply_t
*modmap_r
;
7599 xcb_keycode_t
*modmap
, kc
, *keycode
;
7603 modmap_r
= xcb_get_modifier_mapping_reply(conn
,
7604 xcb_get_modifier_mapping(conn
),
7607 modmap
= xcb_get_modifier_mapping_keycodes(modmap_r
);
7608 for (i
= 0; i
< 8; i
++) {
7609 for (j
= 0; j
< modmap_r
->keycodes_per_modifier
; j
++) {
7610 kc
= modmap
[i
* modmap_r
->keycodes_per_modifier
7612 keycode
= xcb_key_symbols_get_keycode(syms
,
7616 numlockmask
= (1 << i
);
7623 DNPRINTF(SWM_D_MISC
, "updatenumlockmask: %d\n", numlockmask
);
7630 int num_screens
, k
, j
;
7631 unsigned int modifiers
[4];
7632 xcb_keycode_t
*code
;
7634 DNPRINTF(SWM_D_MISC
, "grabkeys\n");
7635 updatenumlockmask();
7638 modifiers
[1] = numlockmask
;
7639 modifiers
[2] = XCB_MOD_MASK_LOCK
;
7640 modifiers
[3] = numlockmask
| XCB_MOD_MASK_LOCK
;
7642 num_screens
= get_screen_count();
7643 for (k
= 0; k
< num_screens
; k
++) {
7644 if (TAILQ_EMPTY(&screens
[k
].rl
))
7646 xcb_ungrab_key(conn
, XCB_GRAB_ANY
, screens
[k
].root
,
7648 RB_FOREACH(kp
, key_tree
, &keys
) {
7649 /* Skip unused ws binds. */
7650 if ((int)kp
->funcid
> KF_WS_1
+ workspace_limit
- 1 &&
7651 kp
->funcid
<= KF_WS_22
)
7654 /* Skip unused mvws binds. */
7655 if ((int)kp
->funcid
> KF_MVWS_1
+ workspace_limit
- 1 &&
7656 kp
->funcid
<= KF_MVWS_22
)
7659 if ((code
= xcb_key_symbols_get_keycode(syms
,
7661 for (j
= 0; j
< LENGTH(modifiers
); j
++)
7662 xcb_grab_key(conn
, 1,
7664 kp
->mod
| modifiers
[j
],
7665 *code
, XCB_GRAB_MODE_SYNC
,
7666 XCB_GRAB_MODE_SYNC
);
7674 grabbuttons(struct ws_win
*win
)
7676 unsigned int modifiers
[4];
7679 DNPRINTF(SWM_D_MOUSE
, "grabbuttons: win %#x\n", win
->id
);
7680 updatenumlockmask();
7683 modifiers
[1] = numlockmask
;
7684 modifiers
[2] = XCB_MOD_MASK_LOCK
;
7685 modifiers
[3] = numlockmask
| XCB_MOD_MASK_LOCK
;
7687 for (i
= 0; i
< LENGTH(buttons
); i
++)
7688 if (buttons
[i
].action
== client_click
)
7689 for (j
= 0; j
< LENGTH(modifiers
); ++j
)
7690 xcb_grab_button(conn
, 0, win
->id
, BUTTONMASK
,
7691 XCB_GRAB_MODE_SYNC
, XCB_GRAB_MODE_ASYNC
,
7692 XCB_WINDOW_NONE
, XCB_CURSOR_NONE
,
7693 buttons
[i
].button
, buttons
[i
].mask
|
7697 const char *quirkname
[] = {
7698 "NONE", /* config string for "no value" */
7706 "FOCUSONMAP_SINGLE",
7712 /* SWM_Q_DELIM: retain '|' for back compat for now (2009-08-11) */
7713 #define SWM_Q_DELIM "\n|+ \t"
7715 parsequirks(const char *qstr
, uint32_t *quirk
, int *ws
)
7717 char *str
, *cp
, *name
;
7720 if (quirk
== NULL
|| qstr
== NULL
)
7723 if ((str
= strdup(qstr
)) == NULL
)
7724 err(1, "parsequirks: strdup");
7728 while ((name
= strsep(&cp
, SWM_Q_DELIM
)) != NULL
) {
7730 cp
+= (long)strspn(cp
, SWM_Q_DELIM
);
7732 if (sscanf(name
, "WS[%d]", ws
) == 1) {
7738 for (i
= 0; i
< LENGTH(quirkname
); i
++) {
7739 if (strncasecmp(name
, quirkname
[i
],
7740 SWM_QUIRK_LEN
) == 0) {
7741 DNPRINTF(SWM_D_QUIRK
,
7742 "parsequirks: %s\n", name
);
7748 *quirk
|= 1 << (i
-1);
7752 if (i
>= LENGTH(quirkname
)) {
7753 DNPRINTF(SWM_D_QUIRK
,
7754 "parsequirks: invalid quirk [%s]\n", name
);
7765 quirk_insert(const char *class, const char *instance
, const char *name
,
7766 uint32_t quirk
, int ws
)
7770 bool failed
= false;
7772 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: class: %s, instance: %s, name: %s,"
7773 " value: %u, ws: %d\n", class, instance
, name
, quirk
, ws
);
7775 if ((qp
= malloc(sizeof *qp
)) == NULL
)
7776 err(1, "quirk_insert: malloc");
7778 if ((qp
->class = strdup(class)) == NULL
)
7779 err(1, "quirk_insert: strdup");
7780 if ((qp
->instance
= strdup(instance
)) == NULL
)
7781 err(1, "quirk_insert: strdup");
7782 if ((qp
->name
= strdup(name
)) == NULL
)
7783 err(1, "quirk_insert: strdup");
7785 if (asprintf(&str
, "^%s$", class) == -1)
7786 err(1, "quirk_insert: asprintf");
7787 if (regcomp(&qp
->regex_class
, str
, REG_EXTENDED
| REG_NOSUB
)) {
7788 add_startup_exception("regex failed to compile quirk 'class' "
7789 "field: %s", class);
7792 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: compiled: %s\n", str
);
7795 if (asprintf(&str
, "^%s$", instance
) == -1)
7796 err(1, "quirk_insert: asprintf");
7797 if (regcomp(&qp
->regex_instance
, str
, REG_EXTENDED
| REG_NOSUB
)) {
7798 add_startup_exception("regex failed to compile quirk 'instance'"
7799 " field: %s", instance
);
7802 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: compiled: %s\n", str
);
7805 if (asprintf(&str
, "^%s$", name
) == -1)
7806 err(1, "quirk_insert: asprintf");
7807 if (regcomp(&qp
->regex_name
, str
, REG_EXTENDED
| REG_NOSUB
)) {
7808 add_startup_exception("regex failed to compile quirk 'name' "
7812 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: compiled: %s\n", str
);
7816 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: regex error; skipping.\n");
7821 TAILQ_INSERT_TAIL(&quirks
, qp
, entry
);
7823 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: leave\n");
7827 quirk_remove(struct quirk
*qp
)
7829 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: %s:%s [%u]\n", qp
->class,
7830 qp
->name
, qp
->quirk
);
7832 TAILQ_REMOVE(&quirks
, qp
, entry
);
7835 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: leave\n");
7839 quirk_free(struct quirk
*qp
)
7841 regfree(&qp
->regex_class
);
7842 regfree(&qp
->regex_instance
);
7843 regfree(&qp
->regex_name
);
7851 quirk_replace(struct quirk
*qp
, const char *class, const char *instance
,
7852 const char *name
, uint32_t quirk
, int ws
)
7854 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: %s:%s:%s [%u], ws: %d\n", qp
->class,
7855 qp
->instance
, qp
->name
, qp
->quirk
, qp
->ws
);
7858 quirk_insert(class, instance
, name
, quirk
, ws
);
7860 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: leave\n");
7864 setquirk(const char *class, const char *instance
, const char *name
,
7865 uint32_t quirk
, int ws
)
7869 DNPRINTF(SWM_D_QUIRK
, "setquirk: enter %s:%s:%s [%u], ws: %d\n", class,
7870 instance
, name
, quirk
, ws
);
7872 /* Remove/replace existing quirk. */
7873 TAILQ_FOREACH(qp
, &quirks
, entry
) {
7874 if (strcmp(qp
->class, class) == 0 &&
7875 strcmp(qp
->instance
, instance
) == 0 &&
7876 strcmp(qp
->name
, name
) == 0) {
7877 if (quirk
== 0 && ws
== -1)
7880 quirk_replace(qp
, class, instance
, name
, quirk
,
7882 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
7887 /* Only insert if quirk is not NONE or forced ws is set. */
7888 if (quirk
|| ws
!= -1)
7889 quirk_insert(class, instance
, name
, quirk
, ws
);
7891 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
7894 /* Eat '\' in str used to escape square brackets and colon. */
7896 unescape_selector(char *str
)
7900 for (cp
= str
; *str
!= '\0'; ++str
, ++cp
) {
7901 if (*str
== '\\' && (*(str
+ 1) == ':' || *(str
+ 1) == ']' ||
7911 setconfquirk(const char *selector
, const char *value
, int flags
)
7913 char *str
, *cp
, *class;
7914 char *instance
= NULL
, *name
= NULL
;
7915 int retval
, count
= 0, ws
= -1;
7918 /* suppress unused warning since var is needed */
7921 if (selector
== NULL
|| strlen(selector
) == 0)
7924 if ((str
= strdup(selector
)) == NULL
)
7925 err(1, "setconfquirk: strdup");
7927 /* Find non-escaped colon. */
7934 for (++cp
; *cp
!= '\0'; ++cp
) {
7935 if (*cp
== ':' && *(cp
- 1) != '\\') {
7941 unescape_selector(class);
7943 instance
= class + strlen(class) + 1;
7944 unescape_selector(instance
);
7950 name
= instance
+ strlen(instance
) + 1;
7951 unescape_selector(name
);
7956 DNPRINTF(SWM_D_CONF
, "setconfquirk: class: %s, instance: %s, "
7957 "name: %s\n", class, instance
, name
);
7959 if ((retval
= parsequirks(value
, &qrks
, &ws
)) == 0)
7960 setquirk(class, instance
, name
, qrks
, ws
);
7969 setquirk("MPlayer", "xv", ".*",
7970 SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
| SWM_Q_FOCUSPREV
, -1);
7971 setquirk("OpenOffice.org 3.2", "VCLSalFrame", ".*",
7973 setquirk("Firefox-bin", "firefox-bin", ".*",
7975 setquirk("Firefox", "Dialog", ".*",
7977 setquirk("Gimp", "gimp", ".*",
7978 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
7979 setquirk("XTerm", "xterm", ".*",
7980 SWM_Q_XTERM_FONTADJ
, -1);
7981 setquirk("xine", "Xine Window", ".*",
7982 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
7983 setquirk("Xitk", "Xitk Combo", ".*",
7984 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
7985 setquirk("xine", "xine Panel", ".*",
7986 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
7987 setquirk("Xitk", "Xine Window", ".*",
7988 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
7989 setquirk("xine", "xine Video Fullscreen Window", ".*",
7990 SWM_Q_FULLSCREEN
| SWM_Q_FLOAT
, -1);
7991 setquirk("pcb", "pcb", ".*",
7993 setquirk("SDL_App", "SDL_App", ".*",
7994 SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
, -1);
7997 /* conf file stuff */
7998 #define SWM_CONF_FILE "spectrwm.conf"
7999 #define SWM_CONF_FILE_OLD "scrotwm.conf"
8003 SWM_S_BAR_AT_BOTTOM
,
8004 SWM_S_BAR_BORDER_WIDTH
,
8007 SWM_S_BAR_ENABLED_WS
,
8012 SWM_S_BOUNDARY_WIDTH
,
8013 SWM_S_CLOCK_ENABLED
,
8016 SWM_S_CYCLE_VISIBLE
,
8018 SWM_S_DISABLE_BORDER
,
8020 SWM_S_FOCUS_CLOSE_WRAP
,
8021 SWM_S_FOCUS_DEFAULT
,
8023 SWM_S_ICONIC_ENABLED
,
8024 SWM_S_JAVA_WORKAROUND
,
8025 SWM_S_REGION_PADDING
,
8030 SWM_S_STACK_ENABLED
,
8033 SWM_S_URGENT_COLLAPSE
,
8034 SWM_S_URGENT_ENABLED
,
8035 SWM_S_VERBOSE_LAYOUT
,
8037 SWM_S_WINDOW_CLASS_ENABLED
,
8038 SWM_S_WINDOW_INSTANCE_ENABLED
,
8039 SWM_S_WINDOW_NAME_ENABLED
,
8040 SWM_S_WORKSPACE_LIMIT
,
8041 SWM_S_WORKSPACE_NAME
,
8045 setconfvalue(const char *selector
, const char *value
, int flags
)
8047 struct workspace
*ws
;
8048 int i
, ws_id
, num_screens
;
8049 char *b
, *str
, s
[1024];
8052 case SWM_S_BAR_ACTION
:
8054 if ((bar_argv
[0] = expand_tilde(value
)) == NULL
)
8055 err(1, "setconfvalue: bar_action");
8057 case SWM_S_BAR_AT_BOTTOM
:
8058 bar_at_bottom
= (atoi(value
) != 0);
8060 case SWM_S_BAR_BORDER_WIDTH
:
8061 bar_border_width
= atoi(value
);
8062 if (bar_border_width
< 0)
8063 bar_border_width
= 0;
8065 case SWM_S_BAR_DELAY
:
8066 /* No longer needed; leave to not break old conf files. */
8068 case SWM_S_BAR_ENABLED
:
8069 bar_enabled
= (atoi(value
) != 0);
8071 case SWM_S_BAR_ENABLED_WS
:
8072 ws_id
= atoi(selector
) - 1;
8073 if (ws_id
< 0 || ws_id
>= workspace_limit
)
8074 errx(1, "setconfvalue: bar_enabled_ws: invalid "
8075 "workspace %d.", ws_id
+ 1);
8077 num_screens
= get_screen_count();
8078 for (i
= 0; i
< num_screens
; i
++) {
8079 ws
= (struct workspace
*)&screens
[i
].ws
;
8080 ws
[ws_id
].bar_enabled
= (atoi(value
) != 0);
8083 case SWM_S_BAR_FONT
:
8085 if (asprintf(&bar_fonts
, "%s,%s", value
, bar_fonts
) == -1)
8086 err(1, "setconfvalue: asprintf: failed to allocate "
8087 "memory for bar_fonts.");
8090 /* If already in xft mode, then we are done. */
8091 if (!bar_font_legacy
)
8094 if ((str
= strdup(value
)) == NULL
)
8095 err(1, "setconfvalue: strdup");
8097 /* If there are any non-XLFD entries, switch to Xft mode. */
8098 while ((b
= strsep(&str
, ",")) != NULL
) {
8102 bar_font_legacy
= false;
8109 case SWM_S_BAR_FORMAT
:
8111 if ((bar_format
= strdup(value
)) == NULL
)
8112 err(1, "setconfvalue: bar_format");
8114 case SWM_S_BAR_JUSTIFY
:
8115 if (strcmp(value
, "left") == 0)
8116 bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
8117 else if (strcmp(value
, "center") == 0)
8118 bar_justify
= SWM_BAR_JUSTIFY_CENTER
;
8119 else if (strcmp(value
, "right") == 0)
8120 bar_justify
= SWM_BAR_JUSTIFY_RIGHT
;
8122 errx(1, "invalid bar_justify");
8124 case SWM_S_BORDER_WIDTH
:
8125 border_width
= atoi(value
);
8126 if (border_width
< 0)
8129 case SWM_S_BOUNDARY_WIDTH
:
8130 boundary_width
= atoi(value
);
8131 if (boundary_width
< 0)
8134 case SWM_S_CLOCK_ENABLED
:
8135 clock_enabled
= (atoi(value
) != 0);
8137 case SWM_S_CLOCK_FORMAT
:
8138 #ifndef SWM_DENY_CLOCK_FORMAT
8140 if ((clock_format
= strdup(value
)) == NULL
)
8141 err(1, "setconfvalue: clock_format");
8144 case SWM_S_CYCLE_EMPTY
:
8145 cycle_empty
= (atoi(value
) != 0);
8147 case SWM_S_CYCLE_VISIBLE
:
8148 cycle_visible
= (atoi(value
) != 0);
8150 case SWM_S_DIALOG_RATIO
:
8151 dialog_ratio
= atof(value
);
8152 if (dialog_ratio
> 1.0 || dialog_ratio
<= .3)
8155 case SWM_S_DISABLE_BORDER
:
8156 disable_border
= (atoi(value
) != 0);
8158 case SWM_S_FOCUS_CLOSE
:
8159 if (strcmp(value
, "first") == 0)
8160 focus_close
= SWM_STACK_BOTTOM
;
8161 else if (strcmp(value
, "last") == 0)
8162 focus_close
= SWM_STACK_TOP
;
8163 else if (strcmp(value
, "next") == 0)
8164 focus_close
= SWM_STACK_ABOVE
;
8165 else if (strcmp(value
, "previous") == 0)
8166 focus_close
= SWM_STACK_BELOW
;
8168 errx(1, "focus_close");
8170 case SWM_S_FOCUS_CLOSE_WRAP
:
8171 focus_close_wrap
= (atoi(value
) != 0);
8173 case SWM_S_FOCUS_DEFAULT
:
8174 if (strcmp(value
, "last") == 0)
8175 focus_default
= SWM_STACK_TOP
;
8176 else if (strcmp(value
, "first") == 0)
8177 focus_default
= SWM_STACK_BOTTOM
;
8179 errx(1, "focus_default");
8181 case SWM_S_FOCUS_MODE
:
8182 if (strcmp(value
, "default") == 0)
8183 focus_mode
= SWM_FOCUS_DEFAULT
;
8184 else if (strcmp(value
, "follow") == 0 ||
8185 strcmp(value
, "follow_cursor") == 0)
8186 focus_mode
= SWM_FOCUS_FOLLOW
;
8187 else if (strcmp(value
, "manual") == 0)
8188 focus_mode
= SWM_FOCUS_MANUAL
;
8190 errx(1, "focus_mode");
8192 case SWM_S_ICONIC_ENABLED
:
8193 iconic_enabled
= (atoi(value
) != 0);
8195 case SWM_S_JAVA_WORKAROUND
:
8196 java_workaround
= (atoi(value
) != 0);
8198 case SWM_S_REGION_PADDING
:
8199 region_padding
= atoi(value
);
8200 if (region_padding
< 0)
8203 case SWM_S_SPAWN_ORDER
:
8204 if (strcmp(value
, "first") == 0)
8205 spawn_position
= SWM_STACK_BOTTOM
;
8206 else if (strcmp(value
, "last") == 0)
8207 spawn_position
= SWM_STACK_TOP
;
8208 else if (strcmp(value
, "next") == 0)
8209 spawn_position
= SWM_STACK_ABOVE
;
8210 else if (strcmp(value
, "previous") == 0)
8211 spawn_position
= SWM_STACK_BELOW
;
8213 errx(1, "spawn_position");
8215 case SWM_S_SPAWN_TERM
:
8216 setconfspawn("term", value
, 0);
8217 setconfspawn("spawn_term", value
, 0);
8220 /* No longer needed; leave to not break old conf files. */
8222 case SWM_S_SS_ENABLED
:
8223 /* No longer needed; leave to not break old conf files. */
8225 case SWM_S_STACK_ENABLED
:
8226 stack_enabled
= (atoi(value
) != 0);
8228 case SWM_S_TERM_WIDTH
:
8229 term_width
= atoi(value
);
8233 case SWM_S_TILE_GAP
:
8234 tile_gap
= atoi(value
);
8236 case SWM_S_URGENT_COLLAPSE
:
8237 urgent_collapse
= (atoi(value
) != 0);
8239 case SWM_S_URGENT_ENABLED
:
8240 urgent_enabled
= (atoi(value
) != 0);
8242 case SWM_S_VERBOSE_LAYOUT
:
8243 verbose_layout
= (atoi(value
) != 0);
8244 for (i
= 0; layouts
[i
].l_stack
!= NULL
; i
++) {
8246 layouts
[i
].l_string
= fancy_stacker
;
8248 layouts
[i
].l_string
= plain_stacker
;
8251 case SWM_S_WARP_POINTER
:
8252 warp_pointer
= (atoi(value
) != 0);
8254 case SWM_S_WINDOW_CLASS_ENABLED
:
8255 window_class_enabled
= (atoi(value
) != 0);
8257 case SWM_S_WINDOW_INSTANCE_ENABLED
:
8258 window_instance_enabled
= (atoi(value
) != 0);
8260 case SWM_S_WINDOW_NAME_ENABLED
:
8261 window_name_enabled
= (atoi(value
) != 0);
8263 case SWM_S_WORKSPACE_LIMIT
:
8264 workspace_limit
= atoi(value
);
8265 if (workspace_limit
> SWM_WS_MAX
)
8266 workspace_limit
= SWM_WS_MAX
;
8267 else if (workspace_limit
< 1)
8268 workspace_limit
= 1;
8270 ewmh_update_desktops();
8272 case SWM_S_WORKSPACE_NAME
:
8273 if (getenv("SWM_STARTED") != NULL
)
8277 if (sscanf(value
, "ws[%d]:%1023c", &ws_id
, s
) != 2)
8278 errx(1, "invalid entry, should be 'ws[<idx>]:name'");
8280 if (ws_id
< 0 || ws_id
>= workspace_limit
)
8281 errx(1, "setconfvalue: workspace_name: invalid "
8282 "workspace %d.", ws_id
+ 1);
8284 num_screens
= get_screen_count();
8285 for (i
= 0; i
< num_screens
; ++i
) {
8286 ws
= (struct workspace
*)&screens
[i
].ws
;
8288 if (strlen(s
) > 0) {
8289 free(ws
[ws_id
].name
);
8290 if ((ws
[ws_id
].name
= strdup(s
)) == NULL
)
8291 err(1, "setconfvalue: workspace_name.");
8293 ewmh_update_desktop_names();
8294 ewmh_get_desktop_names();
8305 setconfmodkey(const char *selector
, const char *value
, int flags
)
8307 /* suppress unused warnings since vars are needed */
8311 if (strncasecmp(value
, "Mod1", strlen("Mod1")) == 0)
8312 update_modkey(XCB_MOD_MASK_1
);
8313 else if (strncasecmp(value
, "Mod2", strlen("Mod2")) == 0)
8314 update_modkey(XCB_MOD_MASK_2
);
8315 else if (strncasecmp(value
, "Mod3", strlen("Mod3")) == 0)
8316 update_modkey(XCB_MOD_MASK_3
);
8317 else if (strncasecmp(value
, "Mod4", strlen("Mod4")) == 0)
8318 update_modkey(XCB_MOD_MASK_4
);
8319 else if (strncasecmp(value
, "Mod5", strlen("Mod5")) == 0)
8320 update_modkey(XCB_MOD_MASK_5
);
8327 setconfmousebuttonmove(const char *selector
, const char *value
, int flags
)
8329 /* suppress unused warnings since vars are needed */
8333 if (strncasecmp(value
, "But1", strlen("But1")) == 0) {
8334 if (!update_mousebutton(0, XCB_BUTTON_INDEX_1
))
8336 } else if (strncasecmp(value
, "But2", strlen("But2")) == 0) {
8337 if (!update_mousebutton(0, XCB_BUTTON_INDEX_2
))
8339 } else if (strncasecmp(value
, "But3", strlen("But3")) == 0) {
8340 if (!update_mousebutton(0, XCB_BUTTON_INDEX_3
))
8348 setconfmousebuttonresize(const char *selector
, const char *value
, int flags
)
8350 /* suppress unused warnings since vars are needed */
8354 if (strncasecmp(value
, "But1", strlen("But1")) == 0) {
8355 if (!update_mousebutton(1, XCB_BUTTON_INDEX_1
))
8357 } else if (strncasecmp(value
, "But2", strlen("But2")) == 0) {
8358 if (!update_mousebutton(1, XCB_BUTTON_INDEX_2
))
8360 } else if (strncasecmp(value
, "But3", strlen("But3")) == 0) {
8361 if (!update_mousebutton(1, XCB_BUTTON_INDEX_3
))
8369 setconfcolor(const char *selector
, const char *value
, int flags
)
8371 int first
, last
, i
= 0, num_screens
;
8373 num_screens
= get_screen_count();
8375 /* conf screen indices begin at 1; treat vals <= 0 as 'all screens.' */
8376 if (selector
== NULL
|| strlen(selector
) == 0 ||
8377 (last
= atoi(selector
) - 1) < 0) {
8379 last
= num_screens
- 1;
8384 if (last
>= num_screens
) {
8385 add_startup_exception("invalid screen index: %d out of bounds "
8386 "(maximum %d)", last
+ 1, num_screens
);
8390 for (i
= first
; i
<= last
; ++i
) {
8391 setscreencolor(value
, i
, flags
);
8394 * When setting focus/unfocus colors, we need to also
8395 * set maximize colors to match if they haven't been customized.
8397 if (flags
== SWM_S_COLOR_FOCUS
&&
8398 !screens
[i
].c
[SWM_S_COLOR_FOCUS_MAXIMIZED
].manual
)
8399 setscreencolor(value
, i
, SWM_S_COLOR_FOCUS_MAXIMIZED
);
8400 else if (flags
== SWM_S_COLOR_UNFOCUS
&&
8401 !screens
[i
].c
[SWM_S_COLOR_UNFOCUS_MAXIMIZED
].manual
)
8402 setscreencolor(value
, i
, SWM_S_COLOR_UNFOCUS_MAXIMIZED
);
8404 screens
[i
].c
[flags
].manual
= 1;
8411 setconfregion(const char *selector
, const char *value
, int flags
)
8413 /* suppress unused warnings since vars are needed */
8417 custom_region(value
);
8422 setautorun(const char *selector
, const char *value
, int flags
)
8432 /* suppress unused warnings since vars are needed */
8436 if (getenv("SWM_STARTED"))
8440 if (sscanf(value
, "ws[%d]:%1023c", &ws_id
, s
) != 2)
8441 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
8443 if (ws_id
< 0 || ws_id
>= workspace_limit
)
8444 errx(1, "autorun: invalid workspace %d", ws_id
+ 1);
8446 sp
= expand_tilde((char *)&s
);
8449 * This is a little intricate
8451 * If the pid already exists we simply reuse it because it means it was
8452 * used before AND not claimed by manage_window. We get away with
8453 * altering it in the parent after INSERT because this can not be a race
8456 while ((ap
= strsep(&sp
, " \t")) != NULL
) {
8459 DNPRINTF(SWM_D_SPAWN
, "setautorun: arg [%s]\n", ap
);
8461 if ((a
.argv
= realloc(a
.argv
, argc
* sizeof(char *))) == NULL
)
8462 err(1, "setautorun: realloc");
8463 a
.argv
[argc
- 1] = ap
;
8467 if ((a
.argv
= realloc(a
.argv
, (argc
+ 1) * sizeof(char *))) == NULL
)
8468 err(1, "setautorun: realloc");
8469 a
.argv
[argc
] = NULL
;
8471 if ((pid
= fork()) == 0) {
8472 spawn(ws_id
, &a
, true);
8481 p
= calloc(1, sizeof *p
);
8484 TAILQ_INSERT_TAIL(&pidlist
, p
, entry
);
8494 setlayout(const char *selector
, const char *value
, int flags
)
8496 struct workspace
*ws
;
8497 int ws_id
, i
, x
, mg
, ma
, si
, ar
;
8498 int st
= SWM_V_STACK
, num_screens
;
8502 /* suppress unused warnings since vars are needed */
8506 if (getenv("SWM_STARTED"))
8510 if (sscanf(value
, "ws[%d]:%d:%d:%d:%d:%1023c",
8511 &ws_id
, &mg
, &ma
, &si
, &ar
, s
) != 6)
8512 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
8513 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
8516 if (ws_id
< 0 || ws_id
>= workspace_limit
)
8517 errx(1, "layout: invalid workspace %d", ws_id
+ 1);
8519 if (strcasecmp(s
, "vertical") == 0)
8521 else if (strcasecmp(s
, "vertical_flip") == 0) {
8524 } else if (strcasecmp(s
, "horizontal") == 0)
8526 else if (strcasecmp(s
, "horizontal_flip") == 0) {
8529 } else if (strcasecmp(s
, "fullscreen") == 0)
8532 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
8533 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
8536 num_screens
= get_screen_count();
8537 for (i
= 0; i
< num_screens
; i
++) {
8538 ws
= (struct workspace
*)&screens
[i
].ws
;
8539 ws
[ws_id
].cur_layout
= &layouts
[st
];
8541 ws
[ws_id
].always_raise
= (ar
!= 0);
8542 if (st
== SWM_MAX_STACK
)
8546 for (x
= 0; x
< abs(mg
); x
++) {
8547 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
8548 mg
>= 0 ? SWM_ARG_ID_MASTERGROW
:
8549 SWM_ARG_ID_MASTERSHRINK
);
8552 for (x
= 0; x
< abs(ma
); x
++) {
8553 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
8554 ma
>= 0 ? SWM_ARG_ID_MASTERADD
:
8555 SWM_ARG_ID_MASTERDEL
);
8558 for (x
= 0; x
< abs(si
); x
++) {
8559 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
8560 si
>= 0 ? SWM_ARG_ID_STACKINC
:
8561 SWM_ARG_ID_STACKDEC
);
8565 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
8566 SWM_ARG_ID_FLIPLAYOUT
);
8573 /* config options */
8574 struct config_option
{
8576 int (*func
)(const char*, const char*, int);
8579 struct config_option configopt
[] = {
8580 { "autorun", setautorun
, 0 },
8581 { "bar_action", setconfvalue
, SWM_S_BAR_ACTION
},
8582 { "bar_at_bottom", setconfvalue
, SWM_S_BAR_AT_BOTTOM
},
8583 { "bar_border", setconfcolor
, SWM_S_COLOR_BAR_BORDER
},
8584 { "bar_border_unfocus", setconfcolor
, SWM_S_COLOR_BAR_BORDER_UNFOCUS
},
8585 { "bar_border_width", setconfvalue
, SWM_S_BAR_BORDER_WIDTH
},
8586 { "bar_color", setconfcolor
, SWM_S_COLOR_BAR
},
8587 { "bar_delay", setconfvalue
, SWM_S_BAR_DELAY
},
8588 { "bar_enabled", setconfvalue
, SWM_S_BAR_ENABLED
},
8589 { "bar_enabled_ws", setconfvalue
, SWM_S_BAR_ENABLED_WS
},
8590 { "bar_font", setconfvalue
, SWM_S_BAR_FONT
},
8591 { "bar_font_color", setconfcolor
, SWM_S_COLOR_BAR_FONT
},
8592 { "bar_format", setconfvalue
, SWM_S_BAR_FORMAT
},
8593 { "bar_justify", setconfvalue
, SWM_S_BAR_JUSTIFY
},
8594 { "bind", setconfbinding
, 0 },
8595 { "border_width", setconfvalue
, SWM_S_BORDER_WIDTH
},
8596 { "boundary_width", setconfvalue
, SWM_S_BOUNDARY_WIDTH
},
8597 { "clock_enabled", setconfvalue
, SWM_S_CLOCK_ENABLED
},
8598 { "clock_format", setconfvalue
, SWM_S_CLOCK_FORMAT
},
8599 { "color_focus", setconfcolor
, SWM_S_COLOR_FOCUS
},
8600 { "color_focus_maximized", setconfcolor
, SWM_S_COLOR_FOCUS_MAXIMIZED
},
8601 { "color_unfocus", setconfcolor
, SWM_S_COLOR_UNFOCUS
},
8602 { "color_unfocus_maximized", setconfcolor
, SWM_S_COLOR_UNFOCUS_MAXIMIZED
},
8603 { "cycle_empty", setconfvalue
, SWM_S_CYCLE_EMPTY
},
8604 { "cycle_visible", setconfvalue
, SWM_S_CYCLE_VISIBLE
},
8605 { "dialog_ratio", setconfvalue
, SWM_S_DIALOG_RATIO
},
8606 { "disable_border", setconfvalue
, SWM_S_DISABLE_BORDER
},
8607 { "focus_close", setconfvalue
, SWM_S_FOCUS_CLOSE
},
8608 { "focus_close_wrap", setconfvalue
, SWM_S_FOCUS_CLOSE_WRAP
},
8609 { "focus_default", setconfvalue
, SWM_S_FOCUS_DEFAULT
},
8610 { "focus_mode", setconfvalue
, SWM_S_FOCUS_MODE
},
8611 { "iconic_enabled", setconfvalue
, SWM_S_ICONIC_ENABLED
},
8612 { "java_workaround", setconfvalue
, SWM_S_JAVA_WORKAROUND
},
8613 { "keyboard_mapping", setkeymapping
, 0 },
8614 { "layout", setlayout
, 0 },
8615 { "modkey", setconfmodkey
, 0 },
8616 { "move_button", setconfmousebuttonmove
, 0 },
8617 { "resize_button", setconfmousebuttonresize
, 0 },
8618 { "program", setconfspawn
, 0 },
8619 { "quirk", setconfquirk
, 0 },
8620 { "region", setconfregion
, 0 },
8621 { "region_padding", setconfvalue
, SWM_S_REGION_PADDING
},
8622 { "screenshot_app", setconfvalue
, SWM_S_SS_APP
},
8623 { "screenshot_enabled", setconfvalue
, SWM_S_SS_ENABLED
},
8624 { "spawn_position", setconfvalue
, SWM_S_SPAWN_ORDER
},
8625 { "spawn_term", setconfvalue
, SWM_S_SPAWN_TERM
},
8626 { "stack_enabled", setconfvalue
, SWM_S_STACK_ENABLED
},
8627 { "term_width", setconfvalue
, SWM_S_TERM_WIDTH
},
8628 { "tile_gap", setconfvalue
, SWM_S_TILE_GAP
},
8629 { "title_class_enabled", setconfvalue
, SWM_S_WINDOW_CLASS_ENABLED
}, /* For backwards compat. */
8630 { "title_name_enabled", setconfvalue
, SWM_S_WINDOW_INSTANCE_ENABLED
}, /* For backwards compat. */
8631 { "urgent_collapse", setconfvalue
, SWM_S_URGENT_COLLAPSE
},
8632 { "urgent_enabled", setconfvalue
, SWM_S_URGENT_ENABLED
},
8633 { "verbose_layout", setconfvalue
, SWM_S_VERBOSE_LAYOUT
},
8634 { "warp_pointer", setconfvalue
, SWM_S_WARP_POINTER
},
8635 { "window_class_enabled", setconfvalue
, SWM_S_WINDOW_CLASS_ENABLED
},
8636 { "window_instance_enabled", setconfvalue
, SWM_S_WINDOW_INSTANCE_ENABLED
},
8637 { "window_name_enabled", setconfvalue
, SWM_S_WINDOW_NAME_ENABLED
},
8638 { "workspace_limit", setconfvalue
, SWM_S_WORKSPACE_LIMIT
},
8639 { "name", setconfvalue
, SWM_S_WORKSPACE_NAME
},
8643 _add_startup_exception(const char *fmt
, va_list ap
)
8645 if (vasprintf(&startup_exception
, fmt
, ap
) == -1)
8646 warn("%s: asprintf", __func__
);
8650 add_startup_exception(const char *fmt
, ...)
8656 if (startup_exception
)
8659 /* force bar to be enabled due to exception */
8663 _add_startup_exception(fmt
, ap
);
8668 conf_load(const char *filename
, int keymapping
)
8671 char *line
= NULL
, *cp
, *ce
, *optsub
, *optval
= NULL
;
8672 size_t linelen
, lineno
= 0;
8673 int wordlen
, i
, optidx
, count
;
8674 struct config_option
*opt
= NULL
;
8676 DNPRINTF(SWM_D_CONF
, "conf_load: begin\n");
8678 if (filename
== NULL
) {
8679 warnx("conf_load: no filename");
8683 DNPRINTF(SWM_D_CONF
, "conf_load: open %s\n", filename
);
8685 if ((config
= fopen(filename
, "r")) == NULL
) {
8686 warn("conf_load: fopen: %s", filename
);
8690 while (!feof(config
)) {
8694 if ((line
= fparseln(config
, &linelen
, &lineno
, NULL
,
8695 FPARSELN_UNESCCOMM
| FPARSELN_UNESCCONT
)) == NULL
) {
8697 err(1, "%s", filename
);
8702 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
8703 if (cp
[0] == '\0') {
8707 /* get config option */
8708 wordlen
= strcspn(cp
, "=[ \t\n");
8710 add_startup_exception("%s: line %zd: no option found",
8715 for (i
= 0; i
< LENGTH(configopt
); i
++) {
8716 opt
= &configopt
[i
];
8717 if (strncasecmp(cp
, opt
->optname
, wordlen
) == 0 &&
8718 (int)strlen(opt
->optname
) == wordlen
) {
8724 add_startup_exception("%s: line %zd: unknown option "
8725 "%.*s", filename
, lineno
, wordlen
, cp
);
8728 if (keymapping
&& opt
&& strcmp(opt
->optname
, "bind")) {
8729 add_startup_exception("%s: line %zd: invalid option "
8730 "%.*s", filename
, lineno
, wordlen
, cp
);
8734 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
8736 /* get [selector] if any */
8741 /* Get length of selector. */
8742 for (ce
= ++cp
; *ce
!= '\0'; ++ce
) {
8743 /* Find matching (not escaped) bracket. */
8744 if (*ce
== ']' && *(ce
- 1) != '\\') {
8751 add_startup_exception("%s: line %zd: syntax "
8752 "error: unterminated selector", filename
,
8757 /* ce points at ']'; terminate optsub. */
8762 cp
+= strspn(cp
, "= \t\n"); /* eat trailing */
8765 if (strlen(optval
) == 0) {
8766 add_startup_exception("%s: line %zd: must supply value "
8767 "to %s", filename
, lineno
,
8768 configopt
[optidx
].optname
);
8771 /* call function to deal with it all */
8772 if (configopt
[optidx
].func(optsub
, optval
,
8773 configopt
[optidx
].funcflags
) != 0) {
8774 add_startup_exception("%s: line %zd: invalid data for "
8775 "%s", filename
, lineno
, configopt
[optidx
].optname
);
8784 if (mouse_button_move
== mouse_button_resize
) {
8785 add_startup_exception("%s: move and resize mouse buttons match",
8789 DNPRINTF(SWM_D_CONF
, "conf_load: end\n");
8795 set_child_transient(struct ws_win
*win
, xcb_window_t
*trans
)
8797 struct ws_win
*parent
, *w
;
8798 struct swm_region
*r
;
8799 struct workspace
*ws
;
8800 xcb_icccm_wm_hints_t wmh
;
8802 parent
= find_window(win
->transient
);
8804 parent
->focus_child
= win
;
8806 DNPRINTF(SWM_D_MISC
, "set_child_transient: parent doesn't exist"
8807 " for %#x trans %#x\n", win
->id
, win
->transient
);
8809 r
= root_to_region(win
->s
->root
, SWM_CK_ALL
);
8811 /* parent doen't exist in our window list */
8812 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
8813 if (xcb_icccm_get_wm_hints_reply(conn
,
8814 xcb_icccm_get_wm_hints(conn
, w
->id
),
8816 warnx("can't get hints for %#x", w
->id
);
8820 if (win
->hints
.window_group
!= wmh
.window_group
)
8823 w
->focus_child
= win
;
8824 win
->transient
= w
->id
;
8826 DNPRINTF(SWM_D_MISC
, "set_child_transient: adjusting "
8827 "transient to %#x\n", win
->transient
);
8834 window_get_pid(xcb_window_t win
)
8839 xcb_get_property_cookie_t pc
;
8840 xcb_get_property_reply_t
*pr
;
8842 apid
= get_atom_from_string("_NET_WM_PID");
8843 if (apid
== XCB_ATOM_NONE
)
8846 pc
= xcb_get_property(conn
, 0, win
, apid
, XCB_ATOM_CARDINAL
, 0, 1);
8847 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
8850 if (pr
->type
!= XCB_ATOM_CARDINAL
) {
8855 if (pr
->type
== apid
&& pr
->format
== 32)
8856 ret
= *((pid_t
*)xcb_get_property_value(pr
));
8862 apid
= get_atom_from_string("_SWM_PID");
8863 pc
= xcb_get_property(conn
, 0, win
, apid
, XCB_ATOM_STRING
,
8865 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
8868 if (pr
->type
!= apid
) {
8873 ret
= (pid_t
)strtonum(xcb_get_property_value(pr
), 0, INT_MAX
, &errstr
);
8880 get_swm_ws(xcb_window_t id
)
8886 xcb_get_property_reply_t
*gpr
;
8888 gpr
= xcb_get_property_reply(conn
,
8889 xcb_get_property(conn
, 0, id
, a_swm_ws
,
8890 XCB_ATOM_STRING
, 0, SWM_PROPLEN
),
8895 proplen
= xcb_get_property_value_length(gpr
);
8897 prop
= malloc(proplen
+ 1);
8900 xcb_get_property_value(gpr
),
8902 prop
[proplen
] = '\0';
8909 DNPRINTF(SWM_D_PROP
, "get_swm_ws: _SWM_WS: %s\n", prop
);
8910 ws_idx
= (int)strtonum(prop
, 0, workspace_limit
- 1, &errstr
);
8912 DNPRINTF(SWM_D_PROP
, "get_swm_ws: win #%s: %s",
8922 get_ws_idx(struct ws_win
*win
)
8924 xcb_get_property_reply_t
*gpr
;
8930 gpr
= xcb_get_property_reply(conn
,
8931 xcb_get_property(conn
, 0, win
->id
, ewmh
[_NET_WM_DESKTOP
].atom
,
8932 XCB_ATOM_CARDINAL
, 0, 1),
8935 if (gpr
->type
== XCB_ATOM_CARDINAL
&& gpr
->format
== 32)
8936 ws_idx
= *((int *)xcb_get_property_value(gpr
));
8940 if (ws_idx
== -1 && !(win
->quirks
& SWM_Q_IGNORESPAWNWS
))
8941 ws_idx
= get_swm_ws(win
->id
);
8943 if (ws_idx
> workspace_limit
- 1 || ws_idx
< -1)
8946 DNPRINTF(SWM_D_PROP
, "get_ws_idx: win %#x, ws_idx: %d\n", win
->id
,
8953 manage_window(xcb_window_t id
, int spawn_pos
, bool mapped
)
8955 struct ws_win
*win
, *ww
;
8956 struct swm_region
*r
;
8959 xcb_get_geometry_reply_t
*gr
;
8960 xcb_window_t trans
= XCB_WINDOW_NONE
;
8961 uint32_t i
, wa
[2], new_flags
;
8962 int ws_idx
, force_ws
= -1;
8963 char *class, *instance
, *name
;
8965 if ((win
= find_window(id
)) != NULL
) {
8966 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x already "
8967 "managed; skipping.)\n", id
);
8968 return (win
); /* Already managed. */
8971 /* See if window is on the unmanaged list. */
8972 if ((win
= find_unmanaged_window(id
)) != NULL
) {
8973 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x found on "
8974 "unmanaged list.\n", id
);
8975 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
8978 set_child_transient(win
, &trans
);
8982 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x is new.\n", id
);
8985 /* Try to get initial window geometry. */
8986 gr
= xcb_get_geometry_reply(conn
, xcb_get_geometry(conn
, id
), NULL
);
8988 DNPRINTF(SWM_D_MISC
, "manage_window: get geometry failed.\n");
8992 /* Create and initialize ws_win object. */
8993 if ((win
= calloc(1, sizeof(struct ws_win
))) == NULL
)
8994 err(1, "manage_window: calloc: failed to allocate memory for "
8999 /* Figureout which region the window belongs to. */
9000 r
= root_to_region(gr
->root
, SWM_CK_ALL
);
9002 /* Ignore window border if there is one. */
9003 WIDTH(win
) = gr
->width
;
9004 HEIGHT(win
) = gr
->height
;
9005 X(win
) = gr
->x
+ gr
->border_width
- border_width
;
9006 Y(win
) = gr
->y
+ gr
->border_width
- border_width
;
9007 win
->bordered
= true;
9008 win
->mapped
= mapped
;
9009 win
->s
= r
->s
; /* this never changes */
9013 /* Select which X events to monitor and set border pixel color. */
9014 wa
[0] = win
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
;
9015 wa
[1] = XCB_EVENT_MASK_ENTER_WINDOW
| XCB_EVENT_MASK_PROPERTY_CHANGE
|
9016 XCB_EVENT_MASK_STRUCTURE_NOTIFY
;
9018 wa
[1] |= XCB_EVENT_MASK_LEAVE_WINDOW
| XCB_EVENT_MASK_FOCUS_CHANGE
;
9021 xcb_change_window_attributes(conn
, win
->id
, XCB_CW_BORDER_PIXEL
|
9022 XCB_CW_EVENT_MASK
, wa
);
9024 /* Get WM_SIZE_HINTS. */
9025 xcb_icccm_get_wm_normal_hints_reply(conn
,
9026 xcb_icccm_get_wm_normal_hints(conn
, win
->id
),
9030 xcb_icccm_get_wm_hints_reply(conn
,
9031 xcb_icccm_get_wm_hints(conn
, win
->id
),
9034 /* Get WM_TRANSIENT_FOR; see if window is a transient. */
9035 xcb_icccm_get_wm_transient_for_reply(conn
,
9036 xcb_icccm_get_wm_transient_for(conn
, win
->id
),
9039 win
->transient
= trans
;
9040 set_child_transient(win
, &win
->transient
);
9043 /* Get WM_PROTOCOLS. */
9044 get_wm_protocols(win
);
9046 /* Set initial quirks based on EWMH. */
9047 ewmh_autoquirk(win
);
9049 /* Determine initial quirks. */
9050 xcb_icccm_get_wm_class_reply(conn
,
9051 xcb_icccm_get_wm_class(conn
, win
->id
),
9054 class = win
->ch
.class_name
? win
->ch
.class_name
: "";
9055 instance
= win
->ch
.instance_name
? win
->ch
.instance_name
: "";
9056 name
= get_win_name(win
->id
);
9058 DNPRINTF(SWM_D_CLASS
, "manage_window: class: %s, instance: %s, "
9059 "name: %s\n", class, instance
, name
);
9061 /* java is retarded so treat it special */
9062 if (strstr(instance
, "sun-awt")) {
9063 DNPRINTF(SWM_D_CLASS
, "manage_window: java window detected.\n");
9067 TAILQ_FOREACH(qp
, &quirks
, entry
) {
9068 if (regexec(&qp
->regex_class
, class, 0, NULL
, 0) == 0 &&
9069 regexec(&qp
->regex_instance
, instance
, 0, NULL
, 0) == 0 &&
9070 regexec(&qp
->regex_name
, name
, 0, NULL
, 0) == 0) {
9071 DNPRINTF(SWM_D_CLASS
, "manage_window: matched "
9072 "quirk: %s:%s:%s mask: %#x, ws: %d\n", qp
->class,
9073 qp
->instance
, qp
->name
, qp
->quirk
, qp
->ws
);
9074 win
->quirks
= qp
->quirk
;
9075 if (qp
->ws
>= 0 && qp
->ws
< workspace_limit
)
9082 /* Reset font sizes (the bruteforce way; no default keybinding). */
9083 if (win
->quirks
& SWM_Q_XTERM_FONTADJ
) {
9084 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
9085 fake_keypress(win
, XK_KP_Subtract
, XCB_MOD_MASK_SHIFT
);
9086 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
9087 fake_keypress(win
, XK_KP_Add
, XCB_MOD_MASK_SHIFT
);
9090 /* Figure out which workspace the window belongs to. */
9091 if (!(win
->quirks
& SWM_Q_IGNOREPID
) &&
9092 (p
= find_pid(window_get_pid(win
->id
))) != NULL
) {
9093 win
->ws
= &r
->s
->ws
[p
->ws
];
9094 TAILQ_REMOVE(&pidlist
, p
, entry
);
9097 } else if ((ws_idx
= get_ws_idx(win
)) != -1 &&
9099 /* _SWM_WS is set; use that. */
9100 win
->ws
= &r
->s
->ws
[ws_idx
];
9101 } else if (trans
&& (ww
= find_window(trans
)) != NULL
) {
9102 /* Launch transients in the same ws as parent. */
9109 win
->ws
= &r
->s
->ws
[force_ws
];
9111 /* Set the _NET_WM_DESKTOP atom. */
9112 DNPRINTF(SWM_D_PROP
, "manage_window: set _NET_WM_DESKTOP: %d\n",
9114 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
9115 ewmh
[_NET_WM_DESKTOP
].atom
, XCB_ATOM_CARDINAL
, 32, 1, &win
->ws
->idx
);
9117 /* Remove any _SWM_WS now that we set _NET_WM_DESKTOP. */
9118 xcb_delete_property(conn
, win
->id
, a_swm_ws
);
9120 /* WS must already be set for this to work. */
9121 store_float_geom(win
);
9123 /* Make sure window is positioned inside its region, if its active. */
9125 region_containment(win
, r
, SWM_CW_ALLSIDES
|
9126 SWM_CW_HARDBOUNDARY
);
9131 /* Figure out where to insert the window in the workspace list. */
9132 if (trans
&& (ww
= find_window(trans
)))
9133 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, ww
, win
, entry
);
9134 else if (win
->ws
->focus
&& spawn_pos
== SWM_STACK_ABOVE
)
9135 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, win
->ws
->focus
, win
,
9137 else if (win
->ws
->focus
&& spawn_pos
== SWM_STACK_BELOW
)
9138 TAILQ_INSERT_BEFORE(win
->ws
->focus
, win
, entry
);
9139 else switch (spawn_pos
) {
9142 case SWM_STACK_ABOVE
:
9143 TAILQ_INSERT_TAIL(&win
->ws
->winlist
, win
, entry
);
9145 case SWM_STACK_BOTTOM
:
9146 case SWM_STACK_BELOW
:
9147 TAILQ_INSERT_HEAD(&win
->ws
->winlist
, win
, entry
);
9150 ewmh_update_client_list();
9152 TAILQ_INSERT_HEAD(&win
->ws
->stack
, win
, stack_entry
);
9155 /* Get/apply initial _NET_WM_STATE */
9156 ewmh_get_wm_state(win
);
9159 new_flags
= win
->ewmh_flags
;
9161 if (win
->quirks
& SWM_Q_FLOAT
)
9162 new_flags
|= EWMH_F_ABOVE
;
9164 if (win
->quirks
& SWM_Q_ANYWHERE
)
9165 new_flags
|= SWM_F_MANUAL
;
9167 ewmh_apply_flags(win
, new_flags
);
9168 ewmh_update_wm_state(win
);
9170 /* Set initial _NET_WM_ALLOWED_ACTIONS */
9171 ewmh_update_actions(win
);
9175 DNPRINTF(SWM_D_MISC
, "manage_window: done. win %#x, (x,y) w x h: "
9176 "(%d,%d) %d x %d, ws: %d, iconic: %s, transient: %#x\n", win
->id
,
9177 X(win
), Y(win
), WIDTH(win
), HEIGHT(win
), win
->ws
->idx
,
9178 YESNO(ICONIC(win
)), win
->transient
);
9184 free_window(struct ws_win
*win
)
9186 DNPRINTF(SWM_D_MISC
, "free_window: win %#x\n", win
->id
);
9191 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
9193 xcb_icccm_get_wm_class_reply_wipe(&win
->ch
);
9198 memset(win
, 0xff, sizeof *win
); /* XXX kill later */
9201 DNPRINTF(SWM_D_MISC
, "free_window: done.\n");
9205 unmanage_window(struct ws_win
*win
)
9207 struct ws_win
*parent
;
9212 DNPRINTF(SWM_D_MISC
, "unmanage_window: win %#x\n", win
->id
);
9215 parent
= find_window(win
->transient
);
9217 parent
->focus_child
= NULL
;
9220 TAILQ_REMOVE(&win
->ws
->stack
, win
, stack_entry
);
9221 TAILQ_REMOVE(&win
->ws
->winlist
, win
, entry
);
9222 TAILQ_INSERT_TAIL(&win
->ws
->unmanagedlist
, win
, entry
);
9224 ewmh_update_client_list();
9228 expose(xcb_expose_event_t
*e
)
9231 struct swm_region
*r
;
9233 DNPRINTF(SWM_D_EVENT
, "expose: win %#x\n", e
->window
);
9235 num_screens
= get_screen_count();
9236 for (i
= 0; i
< num_screens
; i
++)
9237 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
9238 if (e
->window
== WINID(r
->bar
))
9246 focusin(xcb_focus_in_event_t
*e
)
9248 DNPRINTF(SWM_D_EVENT
, "focusin: win %#x, mode: %s(%u), "
9249 "detail: %s(%u)\n", e
->event
, get_notify_mode_label(e
->mode
),
9250 e
->mode
, get_notify_detail_label(e
->detail
), e
->detail
);
9254 focusout(xcb_focus_out_event_t
*e
)
9256 DNPRINTF(SWM_D_EVENT
, "focusout: win %#x, mode: %s(%u), "
9257 "detail: %s(%u)\n", e
->event
, get_notify_mode_label(e
->mode
),
9258 e
->mode
, get_notify_detail_label(e
->detail
), e
->detail
);
9263 keypress(xcb_key_press_event_t
*e
)
9265 xcb_keysym_t keysym
;
9268 keysym
= xcb_key_press_lookup_keysym(syms
, e
, 0);
9270 DNPRINTF(SWM_D_EVENT
, "keypress: keysym: %u, win (x,y): %#x (%d,%d), "
9271 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
9272 "state: %u, same_screen: %s\n", keysym
, e
->event
, e
->event_x
,
9273 e
->event_y
, e
->detail
, e
->time
, e
->root
, e
->root_x
, e
->root_y
,
9274 e
->child
, e
->state
, YESNO(e
->same_screen
));
9276 if ((kp
= key_lookup(CLEANMASK(e
->state
), keysym
)) == NULL
)
9279 last_event_time
= e
->time
;
9281 if (kp
->funcid
== KF_SPAWN_CUSTOM
)
9282 spawn_custom(root_to_region(e
->root
, SWM_CK_ALL
),
9283 &(keyfuncs
[kp
->funcid
].args
), kp
->spawn_name
);
9284 else if (keyfuncs
[kp
->funcid
].func
)
9285 keyfuncs
[kp
->funcid
].func(root_to_region(e
->root
, SWM_CK_ALL
),
9286 &(keyfuncs
[kp
->funcid
].args
));
9289 /* Unfreeze grab events. */
9290 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
, e
->time
);
9293 DNPRINTF(SWM_D_EVENT
, "keypress: done.\n");
9297 buttonpress(xcb_button_press_event_t
*e
)
9299 struct ws_win
*win
= NULL
;
9300 struct swm_region
*r
, *old_r
;
9302 bool handled
= false;
9304 DNPRINTF(SWM_D_EVENT
, "buttonpress: win (x,y): %#x (%d,%d), "
9305 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
9306 "state: %u, same_screen: %s\n", e
->event
, e
->event_x
, e
->event_y
,
9307 e
->detail
, e
->time
, e
->root
, e
->root_x
, e
->root_y
, e
->child
,
9308 e
->state
, YESNO(e
->same_screen
));
9310 if (e
->event
== e
->root
) {
9311 if (e
->child
!= 0) {
9312 win
= find_window(e
->child
);
9313 /* Pass ButtonPress to window if it isn't managed. */
9317 /* Focus on empty region */
9318 /* If no windows on region if its empty. */
9319 r
= root_to_region(e
->root
, SWM_CK_POINTER
);
9321 DNPRINTF(SWM_D_EVENT
, "buttonpress: "
9322 "NULL region; ignoring.\n");
9326 if (TAILQ_EMPTY(&r
->ws
->winlist
)) {
9327 old_r
= root_to_region(e
->root
, SWM_CK_FOCUS
);
9328 if (old_r
&& old_r
!= r
)
9329 unfocus_win(old_r
->ws
->focus
);
9331 xcb_set_input_focus(conn
,
9332 XCB_INPUT_FOCUS_PARENT
, e
->root
, e
->time
);
9334 /* Clear bar since empty. */
9342 win
= find_window(e
->event
);
9348 last_event_time
= e
->time
;
9350 focus_win(get_focus_magic(win
));
9352 for (i
= 0; i
< LENGTH(buttons
); i
++)
9353 if (client_click
== buttons
[i
].action
&& buttons
[i
].func
&&
9354 buttons
[i
].button
== e
->detail
&&
9355 CLEANMASK(buttons
[i
].mask
) == CLEANMASK(e
->state
)) {
9356 buttons
[i
].func(win
, &buttons
[i
].args
);
9362 DNPRINTF(SWM_D_EVENT
, "buttonpress: passing to window.\n");
9363 /* Replay event to event window */
9364 xcb_allow_events(conn
, XCB_ALLOW_REPLAY_POINTER
, e
->time
);
9366 DNPRINTF(SWM_D_EVENT
, "buttonpress: handled.\n");
9367 /* Unfreeze grab events. */
9368 xcb_allow_events(conn
, XCB_ALLOW_SYNC_POINTER
, e
->time
);
9376 print_win_geom(xcb_window_t w
)
9378 xcb_get_geometry_reply_t
*wa
;
9380 wa
= xcb_get_geometry_reply(conn
, xcb_get_geometry(conn
, w
), NULL
);
9382 DNPRINTF(SWM_D_MISC
, "print_win_geom: window not found: %#x\n",
9387 DNPRINTF(SWM_D_MISC
, "print_win_geom: win %#x, root: %#x, "
9388 "depth: %u, (x,y) w x h: (%d,%d) %d x %d, border: %d\n",
9389 w
, wa
->root
, wa
->depth
, wa
->x
, wa
->y
, wa
->width
, wa
->height
,
9398 get_stack_mode_name(uint8_t mode
)
9403 case XCB_STACK_MODE_ABOVE
:
9406 case XCB_STACK_MODE_BELOW
:
9409 case XCB_STACK_MODE_TOP_IF
:
9412 case XCB_STACK_MODE_BOTTOM_IF
:
9415 case XCB_STACK_MODE_OPPOSITE
:
9427 configurerequest(xcb_configure_request_event_t
*e
)
9430 struct swm_region
*r
= NULL
;
9432 uint32_t wc
[7] = {0};
9436 if ((win
= find_window(e
->window
)) == NULL
)
9437 if ((win
= find_unmanaged_window(e
->window
)) == NULL
)
9441 if (swm_debug
& SWM_D_EVENT
) {
9442 print_win_geom(e
->window
);
9444 DNPRINTF(SWM_D_EVENT
, "configurerequest: win %#x, "
9445 "parent: %#x, new: %s, value_mask: %u { ", e
->window
,
9446 e
->parent
, YESNO(new), e
->value_mask
);
9447 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
)
9448 DPRINTF("X: %d ", e
->x
);
9449 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
)
9450 DPRINTF("Y: %d ", e
->y
);
9451 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
)
9452 DPRINTF("W: %u ", e
->width
);
9453 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
)
9454 DPRINTF("H: %u ", e
->height
);
9455 if (e
->value_mask
& XCB_CONFIG_WINDOW_BORDER_WIDTH
)
9456 DPRINTF("Border: %u ", e
->border_width
);
9457 if (e
->value_mask
& XCB_CONFIG_WINDOW_SIBLING
)
9458 DPRINTF("Sibling: %#x ", e
->sibling
);
9459 if (e
->value_mask
& XCB_CONFIG_WINDOW_STACK_MODE
)
9460 DPRINTF("StackMode: %s(%u) ",
9461 get_stack_mode_name(e
->stack_mode
), e
->stack_mode
);
9467 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
) {
9468 mask
|= XCB_CONFIG_WINDOW_X
;
9471 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
) {
9472 mask
|= XCB_CONFIG_WINDOW_Y
;
9475 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
) {
9476 mask
|= XCB_CONFIG_WINDOW_WIDTH
;
9479 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
) {
9480 mask
|= XCB_CONFIG_WINDOW_HEIGHT
;
9481 wc
[i
++] = e
->height
;
9483 if (e
->value_mask
& XCB_CONFIG_WINDOW_BORDER_WIDTH
) {
9484 mask
|= XCB_CONFIG_WINDOW_BORDER_WIDTH
;
9485 wc
[i
++] = e
->border_width
;
9487 if (e
->value_mask
& XCB_CONFIG_WINDOW_SIBLING
) {
9488 mask
|= XCB_CONFIG_WINDOW_SIBLING
;
9489 wc
[i
++] = e
->sibling
;
9491 if (e
->value_mask
& XCB_CONFIG_WINDOW_STACK_MODE
) {
9492 mask
|= XCB_CONFIG_WINDOW_STACK_MODE
;
9493 wc
[i
++] = e
->stack_mode
;
9497 xcb_configure_window(conn
, e
->window
, mask
, wc
);
9500 } else if ((!MANUAL(win
) || win
->quirks
& SWM_Q_ANYWHERE
) &&
9501 !FULLSCREEN(win
) && !MAXIMIZED(win
)) {
9504 else if (win
->ws
->old_r
)
9507 /* windows are centered unless ANYWHERE quirk is set. */
9508 if (win
->quirks
& SWM_Q_ANYWHERE
) {
9509 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
) {
9510 win
->g_float
.x
= e
->x
;
9512 win
->g_float
.x
-= X(r
);
9515 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
) {
9516 win
->g_float
.y
= e
->y
;
9518 win
->g_float
.y
-= Y(r
);
9522 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
)
9523 win
->g_float
.w
= e
->width
;
9525 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
)
9526 win
->g_float
.h
= e
->height
;
9528 win
->g_floatvalid
= true;
9530 if (!MAXIMIZED(win
) && !FULLSCREEN(win
) &&
9531 (TRANS(win
) || (ABOVE(win
) &&
9532 win
->ws
->cur_layout
!= &layouts
[SWM_MAX_STACK
]))) {
9533 WIDTH(win
) = win
->g_float
.w
;
9534 HEIGHT(win
) = win
->g_float
.h
;
9537 update_floater(win
);
9552 DNPRINTF(SWM_D_EVENT
, "configurerequest: done.\n");
9556 configurenotify(xcb_configure_notify_event_t
*e
)
9560 DNPRINTF(SWM_D_EVENT
, "configurenotify: win %#x, event win: %#x, "
9561 "(x,y) WxH: (%d,%d) %ux%u, border: %u, above_sibling: %#x, "
9562 "override_redirect: %s\n", e
->window
, e
->event
, e
->x
, e
->y
,
9563 e
->width
, e
->height
, e
->border_width
, e
->above_sibling
,
9564 YESNO(e
->override_redirect
));
9566 win
= find_window(e
->window
);
9568 xcb_icccm_get_wm_normal_hints_reply(conn
,
9569 xcb_icccm_get_wm_normal_hints(conn
, win
->id
),
9572 if (font_adjusted
) {
9580 destroynotify(xcb_destroy_notify_event_t
*e
)
9584 DNPRINTF(SWM_D_EVENT
, "destroynotify: win %#x\n", e
->window
);
9586 if ((win
= find_window(e
->window
)) == NULL
) {
9587 if ((win
= find_unmanaged_window(e
->window
)) == NULL
)
9593 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
9594 /* If we were focused, make sure we focus on something else. */
9595 if (win
== win
->ws
->focus
)
9596 win
->ws
->focus_pending
= get_focus_prev(win
);
9599 unmanage_window(win
);
9602 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& WS_FOCUSED(win
->ws
)) {
9603 if (win
->ws
->focus_pending
) {
9604 focus_win(win
->ws
->focus_pending
);
9605 win
->ws
->focus_pending
= NULL
;
9606 } else if (win
== win
->ws
->focus
) {
9607 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
,
9608 win
->ws
->r
->id
, XCB_CURRENT_TIME
);
9619 get_notify_detail_label(uint8_t detail
)
9624 case XCB_NOTIFY_DETAIL_ANCESTOR
:
9627 case XCB_NOTIFY_DETAIL_VIRTUAL
:
9630 case XCB_NOTIFY_DETAIL_INFERIOR
:
9633 case XCB_NOTIFY_DETAIL_NONLINEAR
:
9634 label
= "Nonlinear";
9636 case XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL
:
9637 label
= "NonlinearVirtual";
9639 case XCB_NOTIFY_DETAIL_POINTER
:
9642 case XCB_NOTIFY_DETAIL_POINTER_ROOT
:
9643 label
= "PointerRoot";
9645 case XCB_NOTIFY_DETAIL_NONE
:
9656 get_notify_mode_label(uint8_t mode
)
9661 case XCB_NOTIFY_MODE_NORMAL
:
9664 case XCB_NOTIFY_MODE_GRAB
:
9667 case XCB_NOTIFY_MODE_UNGRAB
:
9670 case XCB_NOTIFY_MODE_WHILE_GRABBED
:
9671 label
= "WhileGrabbed";
9682 enternotify(xcb_enter_notify_event_t
*e
)
9685 struct swm_region
*r
;
9687 DNPRINTF(SWM_D_FOCUS
, "enternotify: time: %u, win (x,y): %#x "
9688 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
9689 "child: %#x, same_screen_focus: %s, state: %d\n",
9690 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
9691 get_notify_mode_label(e
->mode
), e
->mode
,
9692 get_notify_detail_label(e
->detail
), e
->detail
,
9693 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
9694 YESNO(e
->same_screen_focus
), e
->state
);
9696 if (focus_mode
== SWM_FOCUS_MANUAL
&&
9697 e
->mode
== XCB_NOTIFY_MODE_NORMAL
) {
9698 DNPRINTF(SWM_D_EVENT
, "enternotify: manual focus; ignoring.\n");
9702 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
9703 e
->mode
== XCB_NOTIFY_MODE_UNGRAB
) {
9704 DNPRINTF(SWM_D_EVENT
, "enternotify: ungrab; ignoring.\n");
9708 last_event_time
= e
->time
;
9710 if ((win
= find_window(e
->event
)) == NULL
) {
9711 if (e
->event
== e
->root
) {
9712 /* If no windows on pointer region, then focus root. */
9713 r
= root_to_region(e
->root
, SWM_CK_POINTER
);
9715 DNPRINTF(SWM_D_EVENT
, "enternotify: "
9716 "NULL region; ignoring.\n");
9722 DNPRINTF(SWM_D_EVENT
, "enternotify: window is NULL; "
9727 if (e
->mode
== XCB_NOTIFY_MODE_NORMAL
&&
9728 e
->detail
== XCB_NOTIFY_DETAIL_INFERIOR
) {
9729 DNPRINTF(SWM_D_EVENT
, "enternotify: entering from "
9730 "inferior; ignoring\n");
9734 focus_win(get_focus_magic(win
));
9742 leavenotify(xcb_leave_notify_event_t
*e
)
9744 DNPRINTF(SWM_D_FOCUS
, "leavenotify: time: %u, win (x,y): %#x "
9745 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
9746 "child: %#x, same_screen_focus: %s, state: %d\n",
9747 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
9748 get_notify_mode_label(e
->mode
), e
->mode
,
9749 get_notify_detail_label(e
->detail
), e
->detail
,
9750 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
9751 YESNO(e
->same_screen_focus
), e
->state
);
9756 mapnotify(xcb_map_notify_event_t
*e
)
9758 struct ws_win
*win
, *parent
= NULL
;
9759 struct workspace
*ws
;
9761 DNPRINTF(SWM_D_EVENT
, "mapnotify: win %#x\n", e
->window
);
9763 if ((win
= manage_window(e
->window
, spawn_position
, true)) == NULL
)
9767 /* Need to know if win was mapped due to ws switch. */
9768 if (ws
->state
== SWM_WS_STATE_MAPPED
) {
9769 if (ws
->focus_pending
&& TRANS(ws
->focus_pending
))
9770 parent
= find_window(win
->transient
);
9772 /* If window's parent is maximized, don't clear it. */
9773 if ((parent
== NULL
) || !MAXIMIZED(parent
))
9774 if (clear_maximized(ws
) > 0)
9779 set_win_state(win
, XCB_ICCCM_WM_STATE_NORMAL
);
9781 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& WS_FOCUSED(win
->ws
)) {
9782 if (ws
->focus_pending
== win
) {
9784 ws
->focus_pending
= NULL
;
9785 center_pointer(win
->ws
->r
);
9794 mappingnotify(xcb_mapping_notify_event_t
*e
)
9797 int i
, j
, num_screens
;
9799 xcb_refresh_keyboard_mapping(syms
, e
);
9801 if (e
->request
== XCB_MAPPING_KEYBOARD
) {
9804 /* Regrab buttons on all managed windows. */
9805 num_screens
= get_screen_count();
9806 for (i
= 0; i
< num_screens
; i
++)
9807 for (j
= 0; j
< workspace_limit
; j
++)
9808 TAILQ_FOREACH(w
, &screens
[i
].ws
[j
].winlist
,
9815 maprequest(xcb_map_request_event_t
*e
)
9817 struct ws_win
*win
, *w
= NULL
;
9818 xcb_get_window_attributes_reply_t
*war
;
9820 DNPRINTF(SWM_D_EVENT
, "maprequest: win %#x\n",
9823 war
= xcb_get_window_attributes_reply(conn
,
9824 xcb_get_window_attributes(conn
, e
->window
),
9827 DNPRINTF(SWM_D_EVENT
, "maprequest: window lost.\n");
9831 if (war
->override_redirect
) {
9832 DNPRINTF(SWM_D_EVENT
, "maprequest: override_redirect; "
9837 win
= manage_window(e
->window
, spawn_position
,
9838 (war
->map_state
== XCB_MAP_STATE_VIEWABLE
));
9842 /* The new window should get focus; prepare. */
9843 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
9844 !(win
->quirks
& SWM_Q_NOFOCUSONMAP
) &&
9845 (!(win
->hints
.flags
& XCB_ICCCM_WM_HINT_INPUT
) ||
9846 (win
->hints
.flags
& XCB_ICCCM_WM_HINT_INPUT
&&
9847 win
->hints
.input
))) {
9848 if (win
->quirks
& SWM_Q_FOCUSONMAP_SINGLE
) {
9849 /* See if other wins of same type are already mapped. */
9850 TAILQ_FOREACH(w
, &win
->ws
->winlist
, entry
) {
9851 if (w
== win
|| !w
->mapped
)
9854 if (w
->ch
.class_name
&&
9855 win
->ch
.class_name
&&
9856 strcmp(w
->ch
.class_name
,
9857 win
->ch
.class_name
) == 0 &&
9858 w
->ch
.instance_name
&&
9859 win
->ch
.instance_name
&&
9860 strcmp(w
->ch
.instance_name
,
9861 win
->ch
.instance_name
) == 0)
9867 win
->ws
->focus_pending
= get_focus_magic(win
);
9870 /* All windows need to be mapped if they are in the current workspace.*/
9874 /* Ignore EnterNotify to handle the mapnotify without interference. */
9875 if (focus_mode
== SWM_FOCUS_DEFAULT
)
9876 event_drain(XCB_ENTER_NOTIFY
);
9879 DNPRINTF(SWM_D_EVENT
, "maprequest: done.\n");
9883 motionnotify(xcb_motion_notify_event_t
*e
)
9885 struct swm_region
*r
= NULL
;
9888 DNPRINTF(SWM_D_FOCUS
, "motionnotify: time: %u, win (x,y): %#x "
9889 "(%d,%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
9890 "child: %#x, same_screen_focus: %s, state: %d\n",
9891 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
9892 get_notify_detail_label(e
->detail
), e
->detail
,
9893 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
9894 YESNO(e
->same_screen
), e
->state
);
9896 last_event_time
= e
->time
;
9898 if (focus_mode
== SWM_FOCUS_MANUAL
)
9901 num_screens
= get_screen_count();
9902 for (i
= 0; i
< num_screens
; i
++)
9903 if (screens
[i
].root
== e
->root
)
9906 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
9907 if (X(r
) <= e
->root_x
&& e
->root_x
< MAX_X(r
) &&
9908 Y(r
) <= e
->root_y
&& e
->root_y
< MAX_Y(r
))
9916 get_atom_name(xcb_atom_t atom
)
9919 #ifdef SWM_DEBUG_ATOM_NAMES
9921 * This should be disabled during most debugging since
9922 * xcb_get_* causes an xcb_flush.
9925 xcb_get_atom_name_reply_t
*r
;
9927 r
= xcb_get_atom_name_reply(conn
,
9928 xcb_get_atom_name(conn
, atom
),
9931 len
= xcb_get_atom_name_name_length(r
);
9933 name
= malloc(len
+ 1);
9935 memcpy(name
, xcb_get_atom_name_name(r
), len
);
9949 propertynotify(xcb_property_notify_event_t
*e
)
9952 struct workspace
*ws
;
9956 name
= get_atom_name(e
->atom
);
9957 DNPRINTF(SWM_D_EVENT
, "propertynotify: win %#x, atom: %s(%u), "
9958 "time: %#x, state: %u\n", e
->window
, name
, e
->atom
, e
->time
,
9962 win
= find_window(e
->window
);
9968 last_event_time
= e
->time
;
9970 if (e
->atom
== a_state
) {
9971 /* State just changed, make sure it gets focused if mapped. */
9972 if (e
->state
== XCB_PROPERTY_NEW_VALUE
) {
9973 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& WS_FOCUSED(ws
)) {
9975 ws
->focus_pending
== win
) {
9976 focus_win(ws
->focus_pending
);
9977 ws
->focus_pending
= NULL
;
9981 } else if (e
->atom
== XCB_ATOM_WM_CLASS
||
9982 e
->atom
== XCB_ATOM_WM_NAME
) {
9984 } else if (e
->atom
== a_prot
) {
9985 get_wm_protocols(win
);
9992 unmapnotify(xcb_unmap_notify_event_t
*e
)
9995 struct workspace
*ws
;
9997 DNPRINTF(SWM_D_EVENT
, "unmapnotify: win %#x\n", e
->window
);
9999 /* If we aren't managing the window, then ignore. */
10000 win
= find_window(e
->window
);
10001 if (win
== NULL
|| win
->id
!= e
->window
)
10004 /* Do nothing if already withdrawn. */
10005 if (!win
->mapped
&& !ICONIC(win
))
10009 win
->mapped
= false;
10011 /* If win was focused, make sure to focus on something else. */
10012 if (win
== ws
->focus
) {
10013 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
10014 ws
->focus_pending
= get_focus_prev(win
);
10015 DNPRINTF(SWM_D_EVENT
, "unmapnotify: "
10016 "focus_pending: %#x\n",
10017 WINID(ws
->focus_pending
));
10025 set_win_state(win
, XCB_ICCCM_WM_STATE_ICONIC
);
10028 set_win_state(win
, XCB_ICCCM_WM_STATE_WITHDRAWN
);
10029 unmanage_window(win
);
10035 /* Update focus if ws is active. */
10036 if (WS_FOCUSED(ws
)) {
10037 if (focus_mode
== SWM_FOCUS_FOLLOW
) {
10038 focus_win(get_pointer_win(ws
->r
->s
->root
));
10039 } else if (ws
->focus_pending
) {
10040 focus_win(ws
->focus_pending
);
10041 ws
->focus_pending
= NULL
;
10042 } else if (ws
->focus
== NULL
) {
10043 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
,
10044 ws
->r
->id
, XCB_CURRENT_TIME
);
10048 center_pointer(ws
->r
);
10054 get_source_type_label(uint32_t type
)
10059 case EWMH_SOURCE_TYPE_NONE
:
10062 case EWMH_SOURCE_TYPE_NORMAL
:
10065 case EWMH_SOURCE_TYPE_OTHER
:
10077 clientmessage(xcb_client_message_event_t
*e
)
10079 struct ws_win
*win
;
10080 struct swm_region
*r
= NULL
;
10083 int num_screens
, i
;
10084 xcb_map_request_event_t mre
;
10088 name
= get_atom_name(e
->type
);
10089 DNPRINTF(SWM_D_EVENT
, "clientmessage: win %#x, atom: %s(%u)\n",
10090 e
->window
, name
, e
->type
);
10094 if (e
->type
== ewmh
[_NET_CURRENT_DESKTOP
].atom
) {
10095 num_screens
= get_screen_count();
10096 for (i
= 0; i
< num_screens
; i
++)
10097 if (screens
[i
].root
== e
->window
) {
10098 r
= screens
[i
].r_focus
;
10102 if (r
&& e
->data
.data32
[0] < (uint32_t)workspace_limit
) {
10103 a
.id
= e
->data
.data32
[0];
10111 win
= find_window(e
->window
);
10113 if (e
->type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
10114 /* Manage the window with maprequest. */
10115 DNPRINTF(SWM_D_EVENT
, "clientmessage: request focus on "
10116 "unmanaged window.\n");
10117 mre
.window
= e
->window
;
10123 if (e
->type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
10124 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_ACTIVE_WINDOW, "
10125 "source_type: %s(%d)\n",
10126 get_source_type_label(e
->data
.data32
[0]),
10127 e
->data
.data32
[0]);
10130 * Allow focus changes that are a result of direct user
10131 * action and from applications that use the old EWMH spec.
10133 if (e
->data
.data32
[0] != EWMH_SOURCE_TYPE_NORMAL
||
10134 win
->quirks
& SWM_Q_OBEYAPPFOCUSREQ
) {
10135 if (WS_FOCUSED(win
->ws
))
10138 win
->ws
->focus_pending
= win
;
10140 } else if (e
->type
== ewmh
[_NET_CLOSE_WINDOW
].atom
) {
10141 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_CLOSE_WINDOW\n");
10142 if (win
->can_delete
)
10143 client_msg(win
, a_delete
, 0);
10145 xcb_kill_client(conn
, win
->id
);
10146 } else if (e
->type
== ewmh
[_NET_MOVERESIZE_WINDOW
].atom
) {
10147 DNPRINTF(SWM_D_EVENT
,
10148 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
10150 if (e
->data
.data32
[0] & (1<<8)) /* x */
10151 X(win
) = e
->data
.data32
[1];
10152 if (e
->data
.data32
[0] & (1<<9)) /* y */
10153 Y(win
) = e
->data
.data32
[2];
10154 if (e
->data
.data32
[0] & (1<<10)) /* width */
10155 WIDTH(win
) = e
->data
.data32
[3];
10156 if (e
->data
.data32
[0] & (1<<11)) /* height */
10157 HEIGHT(win
) = e
->data
.data32
[4];
10159 update_window(win
);
10161 /* Notify no change was made. */
10162 config_win(win
, NULL
);
10163 /* TODO: Change stack sizes */
10165 } else if (e
->type
== ewmh
[_NET_RESTACK_WINDOW
].atom
) {
10166 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_RESTACK_WINDOW\n");
10167 val
[0] = e
->data
.data32
[1]; /* Sibling window. */
10168 val
[1] = e
->data
.data32
[2]; /* Stack mode detail. */
10170 xcb_configure_window(conn
, win
->id
, XCB_CONFIG_WINDOW_SIBLING
|
10171 XCB_CONFIG_WINDOW_STACK_MODE
, val
);
10172 } else if (e
->type
== ewmh
[_NET_WM_STATE
].atom
) {
10173 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_WM_STATE\n");
10174 ewmh_change_wm_state(win
, e
->data
.data32
[1], e
->data
.data32
[0]);
10175 if (e
->data
.data32
[2])
10176 ewmh_change_wm_state(win
, e
->data
.data32
[2],
10177 e
->data
.data32
[0]);
10179 ewmh_update_wm_state(win
);
10181 } else if (e
->type
== ewmh
[_NET_WM_DESKTOP
].atom
) {
10182 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_WM_DESKTOP\n");
10185 win_to_ws(win
, e
->data
.data32
[0], true);
10187 /* Restack if either the source or destination ws is mapped. */
10188 if (r
!= NULL
|| win
->ws
->r
!= NULL
) {
10190 load_float_geom(win
);
10202 int errcode
= xcb_connection_has_error(conn
);
10203 #ifdef XCB_CONN_ERROR
10206 case XCB_CONN_ERROR
:
10207 s
= "Socket error, pipe error or other stream error.";
10209 case XCB_CONN_CLOSED_EXT_NOTSUPPORTED
:
10210 s
= "Extension not supported.";
10212 case XCB_CONN_CLOSED_MEM_INSUFFICIENT
:
10213 s
= "Insufficient memory.";
10215 case XCB_CONN_CLOSED_REQ_LEN_EXCEED
:
10216 s
= "Request length was exceeded.";
10218 case XCB_CONN_CLOSED_PARSE_ERR
:
10219 s
= "Error parsing display string.";
10222 s
= "Unknown error.";
10225 errx(errcode
, "X CONNECTION ERROR: %s", s
);
10228 errx(errcode
, "X CONNECTION ERROR");
10235 int num_screens
, i
;
10236 const uint32_t val
= XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT
|
10237 XCB_EVENT_MASK_ENTER_WINDOW
;
10239 xcb_void_cookie_t wac
;
10240 xcb_generic_error_t
*error
;
10242 /* this causes an error if some other window manager is running */
10243 num_screens
= get_screen_count();
10244 for (i
= 0; i
< num_screens
; i
++) {
10245 if ((sc
= get_screen(i
)) == NULL
)
10246 errx(1, "ERROR: can't get screen %d.", i
);
10247 DNPRINTF(SWM_D_INIT
, "enable_wm: screen %d, root: %#x\n",
10249 wac
= xcb_change_window_attributes_checked(conn
, sc
->root
,
10250 XCB_CW_EVENT_MASK
, &val
);
10251 if ((error
= xcb_request_check(conn
, wac
))) {
10252 DNPRINTF(SWM_D_INIT
, "enable_wm: error_code: %u\n",
10253 error
->error_code
);
10258 /* click to focus on empty region */
10259 xcb_grab_button(conn
, 1, sc
->root
, BUTTONMASK
,
10260 XCB_GRAB_MODE_SYNC
, XCB_GRAB_MODE_ASYNC
, XCB_WINDOW_NONE
,
10261 XCB_CURSOR_NONE
, XCB_BUTTON_INDEX_1
, XCB_BUTTON_MASK_ANY
);
10268 new_region(struct swm_screen
*s
, int x
, int y
, int w
, int h
)
10270 struct swm_region
*r
= NULL
, *n
;
10271 struct workspace
*ws
= NULL
;
10275 DNPRINTF(SWM_D_MISC
, "new region: screen[%d]:%dx%d+%d+%d\n",
10276 s
->idx
, w
, h
, x
, y
);
10278 /* remove any conflicting regions */
10279 n
= TAILQ_FIRST(&s
->rl
);
10282 n
= TAILQ_NEXT(r
, entry
);
10283 if (X(r
) < (x
+ w
) && (X(r
) + WIDTH(r
)) > x
&&
10284 Y(r
) < (y
+ h
) && (Y(r
) + HEIGHT(r
)) > y
) {
10285 if (r
->ws
->r
!= NULL
)
10286 r
->ws
->old_r
= r
->ws
->r
;
10289 xcb_destroy_window(conn
, r
->id
);
10290 TAILQ_REMOVE(&s
->rl
, r
, entry
);
10291 TAILQ_INSERT_TAIL(&s
->orl
, r
, entry
);
10295 /* search old regions for one to reuse */
10297 /* size + location match */
10298 TAILQ_FOREACH(r
, &s
->orl
, entry
)
10299 if (X(r
) == x
&& Y(r
) == y
&&
10300 HEIGHT(r
) == h
&& WIDTH(r
) == w
)
10304 TAILQ_FOREACH(r
, &s
->orl
, entry
)
10305 if (HEIGHT(r
) == h
&& WIDTH(r
) == w
)
10309 TAILQ_REMOVE(&s
->orl
, r
, entry
);
10310 /* try to use old region's workspace */
10311 if (r
->ws
->r
== NULL
)
10314 if ((r
= calloc(1, sizeof(struct swm_region
))) == NULL
)
10315 err(1, "new_region: calloc: failed to allocate memory "
10318 /* if we don't have a workspace already, find one */
10320 for (i
= 0; i
< workspace_limit
; i
++)
10321 if (s
->ws
[i
].r
== NULL
) {
10328 errx(1, "new_region: no free workspaces");
10330 if (ws
->state
== SWM_WS_STATE_HIDDEN
)
10331 ws
->state
= SWM_WS_STATE_MAPPING
;
10339 r
->ws_prior
= NULL
;
10342 TAILQ_INSERT_TAIL(&s
->rl
, r
, entry
);
10344 /* Invisible region window to detect pointer events on empty regions. */
10345 r
->id
= xcb_generate_id(conn
);
10346 wa
[0] = XCB_EVENT_MASK_POINTER_MOTION
|
10347 XCB_EVENT_MASK_POINTER_MOTION_HINT
;
10349 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, r
->id
, r
->s
->root
,
10350 X(r
), Y(r
), WIDTH(r
), HEIGHT(r
), 0, XCB_WINDOW_CLASS_INPUT_ONLY
,
10351 XCB_COPY_FROM_PARENT
, XCB_CW_EVENT_MASK
, wa
);
10353 /* Make sure region input is at the bottom. */
10354 wa
[0] = XCB_STACK_MODE_BELOW
;
10355 xcb_configure_window(conn
, r
->id
, XCB_CONFIG_WINDOW_STACK_MODE
, wa
);
10357 xcb_map_window(conn
, r
->id
);
10361 scan_randr(int idx
)
10363 #ifdef SWM_XRR_HAS_CRTC
10366 #endif /* SWM_XRR_HAS_CRTC */
10367 struct swm_region
*r
;
10368 struct ws_win
*win
;
10370 xcb_randr_get_screen_resources_current_cookie_t src
;
10371 xcb_randr_get_screen_resources_current_reply_t
*srr
;
10372 xcb_randr_get_crtc_info_cookie_t cic
;
10373 xcb_randr_get_crtc_info_reply_t
*cir
= NULL
;
10374 xcb_randr_crtc_t
*crtc
;
10375 xcb_screen_t
*screen
;
10377 DNPRINTF(SWM_D_MISC
, "scan_randr: screen: %d\n", idx
);
10379 if ((screen
= get_screen(idx
)) == NULL
)
10380 errx(1, "ERROR: can't get screen %d.", idx
);
10382 num_screens
= get_screen_count();
10383 if (idx
>= num_screens
)
10384 errx(1, "scan_randr: invalid screen");
10386 /* remove any old regions */
10387 while ((r
= TAILQ_FIRST(&screens
[idx
].rl
)) != NULL
) {
10388 r
->ws
->old_r
= r
->ws
->r
= NULL
;
10390 xcb_destroy_window(conn
, r
->id
);
10391 TAILQ_REMOVE(&screens
[idx
].rl
, r
, entry
);
10392 TAILQ_INSERT_TAIL(&screens
[idx
].orl
, r
, entry
);
10396 /* map virtual screens onto physical screens */
10397 #ifdef SWM_XRR_HAS_CRTC
10398 if (randr_support
) {
10399 src
= xcb_randr_get_screen_resources_current(conn
,
10400 screens
[idx
].root
);
10401 srr
= xcb_randr_get_screen_resources_current_reply(conn
, src
,
10404 new_region(&screens
[idx
], 0, 0,
10405 screen
->width_in_pixels
,
10406 screen
->height_in_pixels
);
10409 ncrtc
= srr
->num_crtcs
;
10411 crtc
= xcb_randr_get_screen_resources_current_crtcs(srr
);
10412 for (c
= 0; c
< ncrtc
; c
++) {
10413 cic
= xcb_randr_get_crtc_info(conn
, crtc
[c
],
10415 cir
= xcb_randr_get_crtc_info_reply(conn
, cic
, NULL
);
10418 if (cir
->num_outputs
== 0) {
10423 if (cir
->mode
== 0)
10424 new_region(&screens
[idx
], 0, 0,
10425 screen
->width_in_pixels
,
10426 screen
->height_in_pixels
);
10428 new_region(&screens
[idx
],
10429 cir
->x
, cir
->y
, cir
->width
, cir
->height
);
10434 #endif /* SWM_XRR_HAS_CRTC */
10436 /* If detection failed, create a single region that spans the screen. */
10437 if (TAILQ_EMPTY(&screens
[idx
].rl
))
10438 new_region(&screens
[idx
], 0, 0, screen
->width_in_pixels
,
10439 screen
->height_in_pixels
);
10442 /* Cleanup unused previously visible workspaces. */
10443 TAILQ_FOREACH(r
, &screens
[idx
].orl
, entry
) {
10444 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
)
10446 r
->ws
->state
= SWM_WS_STATE_HIDDEN
;
10448 /* The screen shouldn't focus on an unused region. */
10449 if (screens
[idx
].r_focus
== r
)
10450 screens
[idx
].r_focus
= NULL
;
10453 DNPRINTF(SWM_D_MISC
, "scan_randr: done.\n");
10457 screenchange(xcb_randr_screen_change_notify_event_t
*e
)
10459 struct swm_region
*r
;
10460 int i
, num_screens
;
10462 DNPRINTF(SWM_D_EVENT
, "screenchange: root: %#x\n", e
->root
);
10464 num_screens
= get_screen_count();
10465 /* silly event doesn't include the screen index */
10466 for (i
= 0; i
< num_screens
; i
++)
10467 if (screens
[i
].root
== e
->root
)
10469 if (i
>= num_screens
)
10470 errx(1, "screenchange: screen not found");
10472 /* brute force for now, just re-enumerate the regions */
10476 print_win_geom(e
->root
);
10478 /* add bars to all regions */
10479 for (i
= 0; i
< num_screens
; i
++) {
10480 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
10486 /* Make sure a region has focus on each screen. */
10487 for (i
= 0; i
< num_screens
; i
++) {
10488 if (screens
[i
].r_focus
== NULL
) {
10489 r
= TAILQ_FIRST(&screens
[i
].rl
);
10498 /* Update workspace state on all regions. */
10499 for (i
= 0; i
< num_screens
; i
++)
10500 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
10501 r
->ws
->state
= SWM_WS_STATE_MAPPED
;
10507 struct swm_region
*r
= NULL
;
10508 xcb_window_t
*wins
= NULL
, trans
, *cwins
= NULL
;
10509 int i
, j
, k
, n
, no
, num_screens
;
10511 bool manage
, mapped
;
10513 xcb_query_tree_cookie_t qtc
;
10514 xcb_query_tree_reply_t
*qtr
;
10515 xcb_get_window_attributes_cookie_t gac
;
10516 xcb_get_window_attributes_reply_t
*gar
;
10517 xcb_get_property_cookie_t pc
;
10518 xcb_get_property_reply_t
*pr
;
10520 DNPRINTF(SWM_D_INIT
, "grab_windows: begin\n");
10521 num_screens
= get_screen_count();
10522 for (i
= 0; i
< num_screens
; i
++) {
10523 qtc
= xcb_query_tree(conn
, screens
[i
].root
);
10524 qtr
= xcb_query_tree_reply(conn
, qtc
, NULL
);
10527 wins
= xcb_query_tree_children(qtr
);
10528 no
= xcb_query_tree_children_length(qtr
);
10530 /* Try to sort windows according to _NET_CLIENT_LIST. */
10531 pr
= xcb_get_property_reply(conn
, xcb_get_property(conn
, 0,
10532 screens
[i
].root
, ewmh
[_NET_CLIENT_LIST
].atom
,
10533 XCB_ATOM_WINDOW
, 0, UINT32_MAX
), NULL
);
10535 cwins
= xcb_get_property_value(pr
);
10536 n
= xcb_get_property_value_length(pr
) /
10537 sizeof(xcb_atom_t
);
10539 for (j
= 0; j
< n
; ++j
) {
10540 for (k
= j
; k
< no
; ++k
) {
10541 if (wins
[k
] == cwins
[j
]) {
10542 /* Swap wins j and k. */
10544 wins
[j
] = cwins
[j
];
10552 /* attach windows to a region */
10553 /* normal windows */
10554 DNPRINTF(SWM_D_INIT
, "grab_windows: grab top level windows.\n");
10555 for (j
= 0; j
< no
; j
++) {
10556 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
10557 if (r
->id
== wins
[j
]) {
10558 DNPRINTF(SWM_D_INIT
, "grab_windows: "
10559 "skip %#x; region input window.\n",
10562 } else if (r
->bar
->id
== wins
[j
]) {
10563 DNPRINTF(SWM_D_INIT
, "grab_windows: "
10564 "skip %#x; region bar.\n",
10573 gac
= xcb_get_window_attributes(conn
, wins
[j
]);
10574 gar
= xcb_get_window_attributes_reply(conn
, gac
, NULL
);
10576 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
10577 "doesn't exist.\n", wins
[j
]);
10581 if (gar
->override_redirect
) {
10582 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
10583 "override_redirect set.\n", wins
[j
]);
10588 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
10589 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
10591 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
10592 "is transient for %#x.\n", wins
[j
], trans
);
10597 state
= get_win_state(wins
[j
]);
10598 manage
= state
!= XCB_ICCCM_WM_STATE_WITHDRAWN
;
10599 mapped
= gar
->map_state
== XCB_MAP_STATE_VIEWABLE
;
10600 if (mapped
|| manage
)
10601 manage_window(wins
[j
], SWM_STACK_TOP
, mapped
);
10604 /* transient windows */
10605 DNPRINTF(SWM_D_INIT
, "grab_windows: grab transient windows.\n");
10606 for (j
= 0; j
< no
; j
++) {
10607 gac
= xcb_get_window_attributes(conn
, wins
[j
]);
10608 gar
= xcb_get_window_attributes_reply(conn
, gac
, NULL
);
10610 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
10611 "doesn't exist.\n", wins
[j
]);
10615 if (gar
->override_redirect
) {
10616 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
10617 "override_redirect set.\n", wins
[j
]);
10622 state
= get_win_state(wins
[j
]);
10623 manage
= state
!= XCB_ICCCM_WM_STATE_WITHDRAWN
;
10624 mapped
= gar
->map_state
== XCB_MAP_STATE_VIEWABLE
;
10625 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
10626 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
10627 &trans
, NULL
) && manage
)
10628 manage_window(wins
[j
], SWM_STACK_TOP
, mapped
);
10633 DNPRINTF(SWM_D_INIT
, "grab_windows: done.\n");
10637 setup_screens(void)
10639 int i
, j
, k
, num_screens
;
10640 struct workspace
*ws
;
10641 uint32_t gcv
[1], wa
[1];
10642 const xcb_query_extension_reply_t
*qep
;
10643 xcb_screen_t
*screen
;
10644 xcb_randr_query_version_cookie_t c
;
10645 xcb_randr_query_version_reply_t
*r
;
10647 num_screens
= get_screen_count();
10648 if ((screens
= calloc(num_screens
,
10649 sizeof(struct swm_screen
))) == NULL
)
10650 err(1, "setup_screens: calloc: failed to allocate memory for "
10653 /* Initial RandR setup. */
10654 randr_support
= false;
10655 qep
= xcb_get_extension_data(conn
, &xcb_randr_id
);
10656 if (qep
->present
) {
10657 c
= xcb_randr_query_version(conn
, 1, 1);
10658 r
= xcb_randr_query_version_reply(conn
, c
, NULL
);
10660 if (r
->major_version
>= 1) {
10661 randr_support
= true;
10662 randr_eventbase
= qep
->first_event
;
10668 wa
[0] = cursors
[XC_LEFT_PTR
].cid
;
10670 /* map physical screens */
10671 for (i
= 0; i
< num_screens
; i
++) {
10672 DNPRINTF(SWM_D_WS
, "setup_screens: init screen: %d\n", i
);
10673 screens
[i
].idx
= i
;
10674 screens
[i
].r_focus
= NULL
;
10676 TAILQ_INIT(&screens
[i
].rl
);
10677 TAILQ_INIT(&screens
[i
].orl
);
10678 if ((screen
= get_screen(i
)) == NULL
)
10679 errx(1, "ERROR: can't get screen %d.", i
);
10680 screens
[i
].root
= screen
->root
;
10682 /* set default colors */
10683 setscreencolor("red", i
, SWM_S_COLOR_FOCUS
);
10684 setscreencolor("rgb:88/88/88", i
, SWM_S_COLOR_UNFOCUS
);
10685 setscreencolor("rgb:00/80/80", i
, SWM_S_COLOR_BAR_BORDER
);
10686 setscreencolor("rgb:00/40/40", i
,
10687 SWM_S_COLOR_BAR_BORDER_UNFOCUS
);
10688 setscreencolor("black", i
, SWM_S_COLOR_BAR
);
10689 setscreencolor("rgb:a0/a0/a0", i
, SWM_S_COLOR_BAR_FONT
);
10690 setscreencolor("red", i
, SWM_S_COLOR_FOCUS_MAXIMIZED
);
10691 setscreencolor("rgb:88/88/88", i
,
10692 SWM_S_COLOR_UNFOCUS_MAXIMIZED
);
10694 /* create graphics context on screen */
10695 screens
[i
].bar_gc
= xcb_generate_id(conn
);
10697 xcb_create_gc(conn
, screens
[i
].bar_gc
, screens
[i
].root
,
10698 XCB_GC_GRAPHICS_EXPOSURES
, gcv
);
10700 /* set default cursor */
10701 xcb_change_window_attributes(conn
, screens
[i
].root
,
10702 XCB_CW_CURSOR
, wa
);
10704 /* init all workspaces */
10705 /* XXX these should be dynamically allocated too */
10706 for (j
= 0; j
< SWM_WS_MAX
; j
++) {
10707 ws
= &screens
[i
].ws
[j
];
10710 ws
->bar_enabled
= true;
10712 ws
->focus_prev
= NULL
;
10713 ws
->focus_pending
= NULL
;
10716 ws
->state
= SWM_WS_STATE_HIDDEN
;
10717 TAILQ_INIT(&ws
->stack
);
10718 TAILQ_INIT(&ws
->winlist
);
10719 TAILQ_INIT(&ws
->unmanagedlist
);
10721 for (k
= 0; layouts
[k
].l_stack
!= NULL
; k
++)
10722 if (layouts
[k
].l_config
!= NULL
)
10723 layouts
[k
].l_config(ws
,
10724 SWM_ARG_ID_STACKINIT
);
10725 ws
->cur_layout
= &layouts
[0];
10726 ws
->cur_layout
->l_string(ws
);
10732 xcb_randr_select_input(conn
, screens
[i
].root
,
10733 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE
);
10738 setup_globals(void)
10740 if ((bar_fonts
= strdup(SWM_BAR_FONTS
)) == NULL
)
10741 err(1, "setup_globals: strdup: failed to allocate memory.");
10743 if ((clock_format
= strdup("%a %b %d %R %Z %Y")) == NULL
)
10744 err(1, "setup_globals: strdup: failed to allocate memory.");
10746 if ((syms
= xcb_key_symbols_alloc(conn
)) == NULL
)
10747 errx(1, "unable to allocate key symbols");
10749 a_state
= get_atom_from_string("WM_STATE");
10750 a_prot
= get_atom_from_string("WM_PROTOCOLS");
10751 a_delete
= get_atom_from_string("WM_DELETE_WINDOW");
10752 a_net_supported
= get_atom_from_string("_NET_SUPPORTED");
10753 a_net_wm_check
= get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
10754 a_takefocus
= get_atom_from_string("WM_TAKE_FOCUS");
10755 a_utf8_string
= get_atom_from_string("UTF8_STRING");
10756 a_swm_ws
= get_atom_from_string("_SWM_WS");
10760 shutdown_cleanup(void)
10762 int i
, num_screens
;
10764 /* disable alarm because the following code may not be interrupted */
10766 if (signal(SIGALRM
, SIG_IGN
) == SIG_ERR
)
10767 err(1, "can't disable alarm");
10776 num_screens
= get_screen_count();
10777 for (i
= 0; i
< num_screens
; ++i
) {
10778 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
10779 screens
[i
].root
, XCB_CURRENT_TIME
);
10781 if (screens
[i
].bar_gc
!= XCB_NONE
)
10782 xcb_free_gc(conn
, screens
[i
].bar_gc
);
10783 if (!bar_font_legacy
) {
10784 XftColorFree(display
, DefaultVisual(display
, i
),
10785 DefaultColormap(display
, i
), &bar_font_color
);
10786 XftColorFree(display
, DefaultVisual(display
, i
),
10787 DefaultColormap(display
, i
), &search_font_color
);
10791 if (bar_font_legacy
)
10792 XFreeFontSet(display
, bar_fs
);
10794 XftFontClose(display
, bar_font
);
10797 xcb_key_symbols_free(syms
);
10799 xcb_disconnect(conn
);
10803 event_error(xcb_generic_error_t
*e
)
10807 DNPRINTF(SWM_D_EVENT
, "event_error: %s(%u) from %s(%u), sequence: %u, "
10808 "resource_id: %u, minor_code: %u\n",
10809 xcb_event_get_error_label(e
->error_code
), e
->error_code
,
10810 xcb_event_get_request_label(e
->major_code
), e
->major_code
,
10811 e
->sequence
, e
->resource_id
, e
->minor_code
);
10815 event_handle(xcb_generic_event_t
*evt
)
10817 uint8_t type
= XCB_EVENT_RESPONSE_TYPE(evt
);
10819 DNPRINTF(SWM_D_EVENT
, "XCB Event: %s(%d), seq %u\n",
10820 xcb_event_get_label(XCB_EVENT_RESPONSE_TYPE(evt
)),
10821 XCB_EVENT_RESPONSE_TYPE(evt
), evt
->sequence
);
10824 #define EVENT(type, callback) case type: callback((void *)evt); return
10825 EVENT(0, event_error
);
10826 EVENT(XCB_BUTTON_PRESS
, buttonpress
);
10827 /*EVENT(XCB_BUTTON_RELEASE, buttonpress);*/
10828 /*EVENT(XCB_CIRCULATE_NOTIFY, );*/
10829 /*EVENT(XCB_CIRCULATE_REQUEST, );*/
10830 EVENT(XCB_CLIENT_MESSAGE
, clientmessage
);
10831 /*EVENT(XCB_COLORMAP_NOTIFY, );*/
10832 EVENT(XCB_CONFIGURE_NOTIFY
, configurenotify
);
10833 EVENT(XCB_CONFIGURE_REQUEST
, configurerequest
);
10834 /*EVENT(XCB_CREATE_NOTIFY, );*/
10835 EVENT(XCB_DESTROY_NOTIFY
, destroynotify
);
10836 EVENT(XCB_ENTER_NOTIFY
, enternotify
);
10837 EVENT(XCB_EXPOSE
, expose
);
10839 EVENT(XCB_FOCUS_IN
, focusin
);
10840 EVENT(XCB_FOCUS_OUT
, focusout
);
10842 /*EVENT(XCB_GRAPHICS_EXPOSURE, );*/
10843 /*EVENT(XCB_GRAVITY_NOTIFY, );*/
10844 EVENT(XCB_KEY_PRESS
, keypress
);
10845 /*EVENT(XCB_KEY_RELEASE, keypress);*/
10846 /*EVENT(XCB_KEYMAP_NOTIFY, );*/
10848 EVENT(XCB_LEAVE_NOTIFY
, leavenotify
);
10850 EVENT(XCB_MAP_NOTIFY
, mapnotify
);
10851 EVENT(XCB_MAP_REQUEST
, maprequest
);
10852 EVENT(XCB_MAPPING_NOTIFY
, mappingnotify
);
10853 EVENT(XCB_MOTION_NOTIFY
, motionnotify
);
10854 /*EVENT(XCB_NO_EXPOSURE, );*/
10855 EVENT(XCB_PROPERTY_NOTIFY
, propertynotify
);
10856 /*EVENT(XCB_REPARENT_NOTIFY, );*/
10857 /*EVENT(XCB_RESIZE_REQUEST, );*/
10858 /*EVENT(XCB_SELECTION_CLEAR, );*/
10859 /*EVENT(XCB_SELECTION_NOTIFY, );*/
10860 /*EVENT(XCB_SELECTION_REQUEST, );*/
10861 EVENT(XCB_UNMAP_NOTIFY
, unmapnotify
);
10862 /*EVENT(XCB_VISIBILITY_NOTIFY, );*/
10865 if (type
- randr_eventbase
== XCB_RANDR_SCREEN_CHANGE_NOTIFY
)
10866 screenchange((void *)evt
);
10870 main(int argc
, char *argv
[])
10872 struct swm_region
*r
;
10873 char conf
[PATH_MAX
], *cfile
= NULL
;
10875 int xfd
, i
, num_screens
;
10876 struct sigaction sact
;
10877 xcb_generic_event_t
*evt
;
10879 struct pollfd pfd
[2];
10880 bool stdin_ready
= false, startup
= true;
10882 /* suppress unused warning since var is needed */
10886 time_started
= time(NULL
);
10890 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION
, buildstr
);
10891 if (setlocale(LC_CTYPE
, "") == NULL
|| setlocale(LC_TIME
, "") == NULL
)
10892 warnx("no locale support");
10894 /* handle some signals */
10895 bzero(&sact
, sizeof(sact
));
10896 sigemptyset(&sact
.sa_mask
);
10898 sact
.sa_handler
= sighdlr
;
10899 sigaction(SIGINT
, &sact
, NULL
);
10900 sigaction(SIGQUIT
, &sact
, NULL
);
10901 sigaction(SIGTERM
, &sact
, NULL
);
10902 sigaction(SIGHUP
, &sact
, NULL
);
10904 sact
.sa_handler
= sighdlr
;
10905 sact
.sa_flags
= SA_NOCLDSTOP
;
10906 sigaction(SIGCHLD
, &sact
, NULL
);
10908 if ((display
= XOpenDisplay(0)) == NULL
)
10909 errx(1, "can not open display");
10911 conn
= XGetXCBConnection(display
);
10912 if (xcb_connection_has_error(conn
))
10913 errx(1, "can not get XCB connection");
10915 XSetEventQueueOwner(display
, XCBOwnsEventQueue
);
10917 xcb_prefetch_extension_data(conn
, &xcb_randr_id
);
10918 xfd
= xcb_get_file_descriptor(conn
);
10920 /* look for local and global conf file */
10921 pwd
= getpwuid(getuid());
10923 errx(1, "invalid user: %d", getuid());
10925 xcb_grab_server(conn
);
10926 xcb_aux_sync(conn
);
10928 /* flush all events */
10929 while ((evt
= xcb_poll_for_event(conn
))) {
10930 if (XCB_EVENT_RESPONSE_TYPE(evt
) == 0)
10936 errx(1, "another window manager is currently running");
10938 /* Load Xcursors and/or cursorfont glyph cursors. */
10941 xcb_aux_sync(conn
);
10951 for (i
= 0; ; i
++) {
10956 snprintf(conf
, sizeof conf
, "%s/.%s",
10957 pwd
->pw_dir
, SWM_CONF_FILE
);
10961 snprintf(conf
, sizeof conf
, "/etc/%s",
10966 snprintf(conf
, sizeof conf
, "%s/.%s",
10967 pwd
->pw_dir
, SWM_CONF_FILE_OLD
);
10970 /* global compat */
10971 snprintf(conf
, sizeof conf
, "/etc/%s",
10972 SWM_CONF_FILE_OLD
);
10978 if (strlen(conf
) && stat(conf
, &sb
) != -1)
10979 if (S_ISREG(sb
.st_mode
)) {
10986 /* load conf (if any) */
10988 conf_load(cfile
, SWM_CONF_DEFAULT
);
10992 if (getenv("SWM_STARTED") == NULL
)
10993 setenv("SWM_STARTED", "YES", 1);
10995 /* setup all bars */
10996 num_screens
= get_screen_count();
10997 for (i
= 0; i
< num_screens
; i
++)
10998 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
11001 /* Manage existing windows. */
11008 xcb_ungrab_server(conn
);
11011 /* Update state of each newly mapped workspace. */
11012 for (i
= 0; i
< num_screens
; i
++)
11013 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
11014 r
->ws
->state
= SWM_WS_STATE_MAPPED
;
11016 memset(&pfd
, 0, sizeof(pfd
));
11018 pfd
[0].events
= POLLIN
;
11019 pfd
[1].fd
= STDIN_FILENO
;
11020 pfd
[1].events
= POLLIN
;
11023 while ((evt
= xcb_poll_for_event(conn
))) {
11030 /* If just (re)started, set default focus if needed. */
11034 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
11035 r
= TAILQ_FIRST(&screens
[0].rl
);
11044 num_readable
= poll(pfd
, bar_extra
? 2 : 1, 1000);
11045 if (num_readable
== -1) {
11046 DNPRINTF(SWM_D_MISC
, "poll failed: %s", strerror(errno
));
11047 } else if (num_readable
> 0 && bar_extra
&& pfd
[1].revents
& POLLIN
) {
11048 stdin_ready
= true;
11052 restart(NULL
, NULL
);
11061 stdin_ready
= false;
11062 bar_extra_update();
11069 shutdown_cleanup();