2 * Copyright (c) 2009-2012 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-2014 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-2013 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__)
93 #include <X11/cursorfont.h>
94 #include <X11/extensions/Xrandr.h>
95 #include <X11/Xcursor/Xcursor.h>
96 #include <X11/Xft/Xft.h>
97 #include <X11/Xlib-xcb.h>
98 #include <xcb/xcb_atom.h>
99 #include <xcb/xcb_aux.h>
100 #include <xcb/xcb_event.h>
101 #include <xcb/xcb_icccm.h>
102 #include <xcb/xcb_keysyms.h>
103 #include <xcb/xtest.h>
104 #include <xcb/randr.h>
112 #ifdef SPECTRWM_BUILDSTR
113 static const char *buildstr
= SPECTRWM_BUILDSTR
;
115 static const char *buildstr
= SPECTRWM_VERSION
;
118 #if !defined(__CYGWIN__) /* cygwin chokes on randr stuff */
120 # error RandR versions less than 1.0 are not supported
123 # if RANDR_MAJOR >= 1
124 # if RANDR_MINOR >= 2
125 # define SWM_XRR_HAS_CRTC
128 #endif /* __CYGWIN__ */
130 #ifndef XCB_ICCCM_NUM_WM_HINTS_ELEMENTS
131 #define XCB_ICCCM_SIZE_HINT_P_MIN_SIZE XCB_SIZE_HINT_P_MIN_SIZE
132 #define XCB_ICCCM_SIZE_HINT_P_MAX_SIZE XCB_SIZE_HINT_P_MAX_SIZE
133 #define XCB_ICCCM_SIZE_HINT_P_RESIZE_INC XCB_SIZE_HINT_P_RESIZE_INC
134 #define XCB_ICCCM_WM_HINT_INPUT XCB_WM_HINT_INPUT
135 #define XCB_ICCCM_WM_HINT_X_URGENCY XCB_WM_HINT_X_URGENCY
136 #define XCB_ICCCM_WM_STATE_ICONIC XCB_WM_STATE_ICONIC
137 #define XCB_ICCCM_WM_STATE_WITHDRAWN XCB_WM_STATE_WITHDRAWN
138 #define XCB_ICCCM_WM_STATE_NORMAL XCB_WM_STATE_NORMAL
139 #define xcb_icccm_get_text_property_reply_t xcb_get_text_property_reply_t
140 #define xcb_icccm_get_text_property_reply_wipe xcb_get_text_property_reply_wipe
141 #define xcb_icccm_get_wm_class xcb_get_wm_class
142 #define xcb_icccm_get_wm_class_reply xcb_get_wm_class_reply
143 #define xcb_icccm_get_wm_class_reply_t xcb_get_wm_class_reply_t
144 #define xcb_icccm_get_wm_class_reply_wipe xcb_get_wm_class_reply_wipe
145 #define xcb_icccm_get_wm_hints xcb_get_wm_hints
146 #define xcb_icccm_wm_hints_get_urgency xcb_wm_hints_get_urgency
147 #define xcb_icccm_get_wm_hints_reply xcb_get_wm_hints_reply
148 #define xcb_icccm_get_wm_name xcb_get_wm_name
149 #define xcb_icccm_get_wm_name_reply xcb_get_wm_name_reply
150 #define xcb_icccm_get_wm_normal_hints xcb_get_wm_normal_hints
151 #define xcb_icccm_get_wm_normal_hints_reply xcb_get_wm_normal_hints_reply
152 #define xcb_icccm_get_wm_protocols xcb_get_wm_protocols
153 #define xcb_icccm_get_wm_protocols_reply xcb_get_wm_protocols_reply
154 #define xcb_icccm_get_wm_protocols_reply_t xcb_get_wm_protocols_reply_t
155 #define xcb_icccm_get_wm_protocols_reply_wipe xcb_get_wm_protocols_reply_wipe
156 #define xcb_icccm_get_wm_transient_for xcb_get_wm_transient_for
157 #define xcb_icccm_get_wm_transient_for_reply xcb_get_wm_transient_for_reply
158 #define xcb_icccm_wm_hints_t xcb_wm_hints_t
161 /*#define SWM_DEBUG*/
163 #define DPRINTF(x...) do { \
165 fprintf(stderr, x); \
167 #define DNPRINTF(n,x...) do { \
168 if (swm_debug & n) { \
169 fprintf(stderr, "%ld ", (long)(time(NULL) - time_started)); \
170 fprintf(stderr, x); \
173 #define SWM_D_MISC 0x0001
174 #define SWM_D_EVENT 0x0002
175 #define SWM_D_WS 0x0004
176 #define SWM_D_FOCUS 0x0008
177 #define SWM_D_MOVE 0x0010
178 #define SWM_D_STACK 0x0020
179 #define SWM_D_MOUSE 0x0040
180 #define SWM_D_PROP 0x0080
181 #define SWM_D_CLASS 0x0100
182 #define SWM_D_KEY 0x0200
183 #define SWM_D_QUIRK 0x0400
184 #define SWM_D_SPAWN 0x0800
185 #define SWM_D_EVENTQ 0x1000
186 #define SWM_D_CONF 0x2000
187 #define SWM_D_BAR 0x4000
188 #define SWM_D_INIT 0x8000
190 uint32_t swm_debug
= 0
209 #define DPRINTF(x...)
210 #define DNPRINTF(n,x...)
213 #define SWM_EWMH_ACTION_COUNT_MAX (8)
214 #define EWMH_F_FULLSCREEN (0x001)
215 #define EWMH_F_ABOVE (0x002)
216 #define EWMH_F_HIDDEN (0x004)
217 #define EWMH_F_MAXIMIZED_VERT (0x008)
218 #define EWMH_F_MAXIMIZED_HORZ (0x010)
219 #define EWMH_F_SKIP_PAGER (0x020)
220 #define EWMH_F_SKIP_TASKBAR (0x040)
221 #define SWM_F_MANUAL (0x080)
223 #define EWMH_F_MAXIMIZED (EWMH_F_MAXIMIZED_VERT | EWMH_F_MAXIMIZED_HORZ)
225 /* convert 8-bit to 16-bit */
226 #define RGB_8_TO_16(col) (((col) << 8) + (col))
228 #define PIXEL_TO_XRENDERCOLOR(px, xrc) \
229 xrc.red = RGB_8_TO_16((px) >> 16 & 0xff); \
230 xrc.green = RGB_8_TO_16((px) >> 8 & 0xff); \
231 xrc.blue = RGB_8_TO_16((px) & 0xff); \
234 #define LENGTH(x) (int)(sizeof (x) / sizeof (x)[0])
235 #define MODKEY XCB_MOD_MASK_1
236 #define CLEANMASK(mask) ((mask) & ~(numlockmask | XCB_MOD_MASK_LOCK))
237 #define BUTTONMASK (XCB_EVENT_MASK_BUTTON_PRESS | \
238 XCB_EVENT_MASK_BUTTON_RELEASE)
239 #define MOUSEMASK (BUTTONMASK|XCB_EVENT_MASK_POINTER_MOTION)
240 #define SWM_PROPLEN (16)
241 #define SWM_FUNCNAME_LEN (32)
242 #define SWM_KEYS_LEN (255)
243 #define SWM_QUIRK_LEN (64)
244 #define X(r) ((r)->g.x)
245 #define Y(r) ((r)->g.y)
246 #define WIDTH(r) ((r)->g.w)
247 #define HEIGHT(r) ((r)->g.h)
248 #define BORDER(w) ((w)->bordered ? border_width : 0)
249 #define MAX_X(r) ((r)->g.x + (r)->g.w)
250 #define MAX_Y(r) ((r)->g.y + (r)->g.h)
251 #define SH_MIN(w) ((w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE)
252 #define SH_MIN_W(w) ((w)->sh.min_width)
253 #define SH_MIN_H(w) ((w)->sh.min_height)
254 #define SH_MAX(w) ((w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MAX_SIZE)
255 #define SH_MAX_W(w) ((w)->sh.max_width)
256 #define SH_MAX_H(w) ((w)->sh.max_height)
257 #define SH_INC(w) ((w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_RESIZE_INC)
258 #define SH_INC_W(w) ((w)->sh.width_inc)
259 #define SH_INC_H(w) ((w)->sh.height_inc)
260 #define SWM_MAX_FONT_STEPS (3)
261 #define WINID(w) ((w) ? (w)->id : XCB_WINDOW_NONE)
262 #define WS_FOCUSED(ws) ((ws)->r && (ws)->r->s->r_focus == (ws)->r)
263 #define YESNO(x) ((x) ? "yes" : "no")
264 #define ICONIC(w) ((w)->ewmh_flags & EWMH_F_HIDDEN)
265 #define ABOVE(w) ((w)->ewmh_flags & EWMH_F_ABOVE)
266 #define FULLSCREEN(w) ((w)->ewmh_flags & EWMH_F_FULLSCREEN)
267 #define MAXIMIZED_VERT(w) ((w)->ewmh_flags & EWMH_F_MAXIMIZED_VERT)
268 #define MAXIMIZED_HORZ(w) ((w)->ewmh_flags & EWMH_F_MAXIMIZED_HORZ)
269 #define MAXIMIZED(w) (MAXIMIZED_VERT(w) || MAXIMIZED_HORZ(w))
270 #define MANUAL(w) ((w)->ewmh_flags & SWM_F_MANUAL)
271 #define TRANS(w) ((w)->transient != XCB_WINDOW_NONE)
272 #define FLOATING(w) (ABOVE(w) || TRANS(w) || FULLSCREEN(w) || \
275 /* Constrain Window flags */
276 #define SWM_CW_RESIZABLE (0x01)
277 #define SWM_CW_SOFTBOUNDARY (0x02)
278 #define SWM_CW_HARDBOUNDARY (0x04)
279 #define SWM_CW_RIGHT (0x10)
280 #define SWM_CW_LEFT (0x20)
281 #define SWM_CW_BOTTOM (0x40)
282 #define SWM_CW_TOP (0x80)
283 #define SWM_CW_ALLSIDES (0xf0)
285 #define SWM_FOCUS_DEFAULT (0)
286 #define SWM_FOCUS_FOLLOW (1)
287 #define SWM_FOCUS_MANUAL (2)
289 #define SWM_CK_NONE (0)
290 #define SWM_CK_ALL (0xf)
291 #define SWM_CK_FOCUS (0x1)
292 #define SWM_CK_POINTER (0x2)
293 #define SWM_CK_FALLBACK (0x4)
294 #define SWM_CK_REGION (0x8)
296 #define SWM_G_ALL (0xf)
297 #define SWM_G_SIZE (0x1)
298 #define SWM_G_POS (0x2)
300 #define SWM_CONF_DEFAULT (0)
301 #define SWM_CONF_KEYMAPPING (1)
304 #define SWM_LIB "/usr/local/lib/libswmhack.so"
311 xcb_atom_t a_net_wm_check
;
312 xcb_atom_t a_net_supported
;
313 xcb_atom_t a_takefocus
;
314 xcb_atom_t a_utf8_string
;
316 volatile sig_atomic_t running
= 1;
317 volatile sig_atomic_t restart_wm
= 0;
318 xcb_timestamp_t last_event_time
= 0;
322 unsigned int numlockmask
= 0;
325 xcb_connection_t
*conn
;
326 xcb_key_symbols_t
*syms
;
328 int boundary_width
= 50;
329 bool cycle_empty
= false;
330 bool cycle_visible
= false;
332 int font_adjusted
= 0;
333 unsigned int mod_key
= MODKEY
;
334 bool warp_pointer
= false;
337 struct swm_region
*search_r
;
338 int select_list_pipe
[2];
339 int select_resp_pipe
[2];
341 volatile sig_atomic_t search_resp
;
342 int search_resp_action
;
344 struct search_window
{
345 TAILQ_ENTRY(search_window
) entry
;
349 xcb_window_t indicator
;
351 TAILQ_HEAD(search_winlist
, search_window
);
352 struct search_winlist search_wl
;
357 SWM_SEARCH_UNICONIFY
,
358 SWM_SEARCH_NAME_WORKSPACE
,
359 SWM_SEARCH_SEARCH_WORKSPACE
,
360 SWM_SEARCH_SEARCH_WINDOW
363 #define SWM_STACK_TOP (0)
364 #define SWM_STACK_BOTTOM (1)
365 #define SWM_STACK_ABOVE (2)
366 #define SWM_STACK_BELOW (3)
369 double dialog_ratio
= 0.6;
371 #define SWM_BAR_MAX (256)
372 #define SWM_BAR_JUSTIFY_LEFT (0)
373 #define SWM_BAR_JUSTIFY_CENTER (1)
374 #define SWM_BAR_JUSTIFY_RIGHT (2)
375 #define SWM_BAR_OFFSET (4)
376 #define SWM_BAR_FONTS "-*-terminus-medium-*-*-*-12-*-*-*-*-*-*-*," \
377 "-*-profont-*-*-*-*-12-*-*-*-*-*-*-*," \
378 "-*-times-medium-r-*-*-12-*-*-*-*-*-*-*," \
379 "-misc-fixed-medium-r-*-*-12-*-*-*-*-*-*-*," \
380 "-*-*-*-r-*-*-*-*-*-*-*-*-*-*"
382 #ifdef X_HAVE_UTF8_STRING
383 #define DRAWSTRING(x...) Xutf8DrawString(x)
385 #define DRAWSTRING(x...) XmbDrawString(x)
388 char *bar_argv
[] = { NULL
, NULL
};
390 char bar_ext
[SWM_BAR_MAX
];
391 char bar_ext_buf
[SWM_BAR_MAX
];
392 char bar_vertext
[SWM_BAR_MAX
];
393 bool bar_version
= false;
394 bool bar_enabled
= true;
395 int bar_border_width
= 1;
396 bool bar_at_bottom
= false;
397 bool bar_extra
= false;
399 int bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
400 char *bar_format
= NULL
;
401 bool stack_enabled
= true;
402 bool clock_enabled
= true;
403 bool iconic_enabled
= false;
404 bool urgent_enabled
= false;
405 bool urgent_collapse
= false;
406 char *clock_format
= NULL
;
407 bool window_class_enabled
= false;
408 bool window_instance_enabled
= false;
409 bool window_name_enabled
= false;
410 int focus_mode
= SWM_FOCUS_DEFAULT
;
411 int focus_close
= SWM_STACK_BELOW
;
412 bool focus_close_wrap
= true;
413 int focus_default
= SWM_STACK_TOP
;
414 int spawn_position
= SWM_STACK_TOP
;
415 bool disable_border
= false;
416 int border_width
= 1;
417 int region_padding
= 0;
419 bool java_workaround
= true;
420 bool verbose_layout
= false;
426 XFontSetExtents
*bar_fs_extents
;
428 bool bar_font_legacy
= true;
430 XftColor bar_font_color
;
431 XftColor search_font_color
;
433 char *startup_exception
;
434 unsigned int nr_exceptions
= 0;
436 /* layout manager data */
437 struct swm_geometry
{
450 struct swm_geometry g
;
453 /* virtual "screens" */
455 TAILQ_ENTRY(swm_region
) entry
;
457 struct swm_geometry g
;
458 struct workspace
*ws
; /* current workspace on this region */
459 struct workspace
*ws_prior
; /* prior workspace on this region */
460 struct swm_screen
*s
; /* screen idx */
463 TAILQ_HEAD(swm_region_list
, swm_region
);
466 TAILQ_ENTRY(ws_win
) entry
;
467 TAILQ_ENTRY(ws_win
) stack_entry
;
469 xcb_window_t transient
;
470 struct ws_win
*focus_child
; /* focus on child transient */
471 struct swm_geometry g
; /* current geometry */
472 struct swm_geometry g_float
; /* region coordinates */
473 bool g_floatvalid
; /* g_float geometry validity */
477 int font_size_boundary
[SWM_MAX_FONT_STEPS
];
484 struct workspace
*ws
; /* always valid */
485 struct swm_screen
*s
; /* always valid, never changes */
487 xcb_icccm_get_wm_class_reply_t ch
;
488 xcb_icccm_wm_hints_t hints
;
490 TAILQ_HEAD(ws_win_list
, ws_win
);
491 TAILQ_HEAD(ws_win_stack
, ws_win
);
495 TAILQ_ENTRY(pid_e
) entry
;
499 TAILQ_HEAD(pid_list
, pid_e
);
500 struct pid_list pidlist
= TAILQ_HEAD_INITIALIZER(pidlist
);
502 /* layout handlers */
504 void vertical_config(struct workspace
*, int);
505 void vertical_stack(struct workspace
*, struct swm_geometry
*);
506 void horizontal_config(struct workspace
*, int);
507 void horizontal_stack(struct workspace
*, struct swm_geometry
*);
508 void max_stack(struct workspace
*, struct swm_geometry
*);
509 void plain_stacker(struct workspace
*);
510 void fancy_stacker(struct workspace
*);
513 void (*l_stack
)(struct workspace
*, struct swm_geometry
*);
514 void (*l_config
)(struct workspace
*, int);
516 #define SWM_L_FOCUSPREV (1<<0)
517 #define SWM_L_MAPONFOCUS (1<<1)
518 void (*l_string
)(struct workspace
*);
520 /* stack, configure */
521 { vertical_stack
, vertical_config
, 0, plain_stacker
},
522 { horizontal_stack
, horizontal_config
, 0, plain_stacker
},
524 SWM_L_MAPONFOCUS
| SWM_L_FOCUSPREV
, plain_stacker
},
525 { NULL
, NULL
, 0, NULL
},
528 /* position of max_stack mode in the layouts array, index into layouts! */
529 #define SWM_V_STACK (0)
530 #define SWM_H_STACK (1)
531 #define SWM_MAX_STACK (2)
533 #define SWM_H_SLICE (32)
534 #define SWM_V_SLICE (32)
536 /* define work spaces */
538 int idx
; /* workspace index */
539 char *name
; /* workspace name */
540 bool always_raise
; /* raise windows on focus */
541 bool bar_enabled
; /* bar visibility */
542 struct layout
*cur_layout
; /* current layout handlers */
543 struct ws_win
*focus
; /* may be NULL */
544 struct ws_win
*focus_prev
; /* may be NULL */
545 struct ws_win
*focus_pending
; /* may be NULL */
546 struct swm_region
*r
; /* may be NULL */
547 struct swm_region
*old_r
; /* may be NULL */
548 struct ws_win_list winlist
; /* list of windows in ws */
549 struct ws_win_list unmanagedlist
; /* list of dead windows in ws */
550 struct ws_win_stack stack
; /* stacking order */
551 int state
; /* mapping state */
552 char stacker
[10]; /* display stacker and layout */
556 int horizontal_msize
;
558 int horizontal_stacks
;
559 bool horizontal_flip
;
569 SWM_WS_STATE_MAPPING
,
575 SWM_S_COLOR_BAR_BORDER
,
576 SWM_S_COLOR_BAR_BORDER_UNFOCUS
,
577 SWM_S_COLOR_BAR_FONT
,
579 SWM_S_COLOR_FOCUS_MAXIMIZED
,
581 SWM_S_COLOR_UNFOCUS_MAXIMIZED
,
585 /* physical screen mapping */
586 #define SWM_WS_MAX (22) /* hard limit */
587 int workspace_limit
= 10; /* soft limit */
590 int idx
; /* screen index */
591 struct swm_region_list rl
; /* list of regions on this screen */
592 struct swm_region_list orl
; /* list of old regions */
594 struct workspace ws
[SWM_WS_MAX
];
595 struct swm_region
*r_focus
;
602 } c
[SWM_S_COLOR_MAX
];
604 xcb_gcontext_t bar_gc
;
607 struct swm_screen
*screens
;
609 /* args to functions */
612 #define SWM_ARG_ID_FOCUSNEXT (0)
613 #define SWM_ARG_ID_FOCUSPREV (1)
614 #define SWM_ARG_ID_FOCUSMAIN (2)
615 #define SWM_ARG_ID_FOCUSURGENT (3)
616 #define SWM_ARG_ID_SWAPNEXT (10)
617 #define SWM_ARG_ID_SWAPPREV (11)
618 #define SWM_ARG_ID_SWAPMAIN (12)
619 #define SWM_ARG_ID_MOVELAST (13)
620 #define SWM_ARG_ID_MASTERSHRINK (20)
621 #define SWM_ARG_ID_MASTERGROW (21)
622 #define SWM_ARG_ID_MASTERADD (22)
623 #define SWM_ARG_ID_MASTERDEL (23)
624 #define SWM_ARG_ID_FLIPLAYOUT (24)
625 #define SWM_ARG_ID_STACKRESET (30)
626 #define SWM_ARG_ID_STACKINIT (31)
627 #define SWM_ARG_ID_CYCLEWS_UP (40)
628 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
629 #define SWM_ARG_ID_CYCLERG_UP (42)
630 #define SWM_ARG_ID_CYCLERG_DOWN (43)
631 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
632 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
633 #define SWM_ARG_ID_CYCLEWS_MOVE_UP (46)
634 #define SWM_ARG_ID_CYCLEWS_MOVE_DOWN (47)
635 #define SWM_ARG_ID_STACKINC (50)
636 #define SWM_ARG_ID_STACKDEC (51)
637 #define SWM_ARG_ID_SS_ALL (60)
638 #define SWM_ARG_ID_SS_WINDOW (61)
639 #define SWM_ARG_ID_DONTCENTER (70)
640 #define SWM_ARG_ID_CENTER (71)
641 #define SWM_ARG_ID_KILLWINDOW (80)
642 #define SWM_ARG_ID_DELETEWINDOW (81)
643 #define SWM_ARG_ID_WIDTHGROW (90)
644 #define SWM_ARG_ID_WIDTHSHRINK (91)
645 #define SWM_ARG_ID_HEIGHTGROW (92)
646 #define SWM_ARG_ID_HEIGHTSHRINK (93)
647 #define SWM_ARG_ID_MOVEUP (100)
648 #define SWM_ARG_ID_MOVEDOWN (101)
649 #define SWM_ARG_ID_MOVELEFT (102)
650 #define SWM_ARG_ID_MOVERIGHT (103)
651 #define SWM_ARG_ID_RAISE (105)
652 #define SWM_ARG_ID_LOWER (106)
653 #define SWM_ARG_ID_BAR_TOGGLE (110)
654 #define SWM_ARG_ID_BAR_TOGGLE_WS (111)
660 TAILQ_ENTRY(quirk
) entry
;
661 char *class; /* WM_CLASS:class */
662 char *instance
; /* WM_CLASS:instance */
663 char *name
; /* WM_NAME */
665 regex_t regex_instance
;
668 int ws
; /* Initial workspace. */
669 #define SWM_Q_FLOAT (1<<0) /* float this window */
670 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
671 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
672 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
673 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
674 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
675 #define SWM_Q_NOFOCUSONMAP (1<<6) /* Don't focus on window when mapped. */
676 #define SWM_Q_FOCUSONMAP_SINGLE (1<<7) /* Only focus if single win of type. */
677 #define SWM_Q_OBEYAPPFOCUSREQ (1<<8) /* Focus when applications ask. */
678 #define SWM_Q_IGNOREPID (1<<9) /* Ignore PID when determining ws. */
679 #define SWM_Q_IGNORESPAWNWS (1<<10) /* Ignore _SWM_WS when managing win. */
681 TAILQ_HEAD(quirk_list
, quirk
);
682 struct quirk_list quirks
= TAILQ_HEAD_INITIALIZER(quirks
);
685 * Supported EWMH hints should be added to
686 * both the enum and the ewmh array
692 _NET_CURRENT_DESKTOP
,
693 _NET_DESKTOP_GEOMETRY
,
695 _NET_DESKTOP_VIEWPORT
,
696 _NET_MOVERESIZE_WINDOW
,
697 _NET_NUMBER_OF_DESKTOPS
,
699 _NET_WM_ACTION_ABOVE
,
700 _NET_WM_ACTION_CLOSE
,
701 _NET_WM_ACTION_FULLSCREEN
,
703 _NET_WM_ACTION_RESIZE
,
704 _NET_WM_ALLOWED_ACTIONS
,
706 _NET_WM_FULL_PLACEMENT
,
710 _NET_WM_STATE_FULLSCREEN
,
711 _NET_WM_STATE_HIDDEN
,
712 _NET_WM_STATE_MAXIMIZED_VERT
,
713 _NET_WM_STATE_MAXIMIZED_HORZ
,
714 _NET_WM_STATE_SKIP_PAGER
,
715 _NET_WM_STATE_SKIP_TASKBAR
,
717 _NET_WM_WINDOW_TYPE_DIALOG
,
718 _NET_WM_WINDOW_TYPE_DOCK
,
719 _NET_WM_WINDOW_TYPE_NORMAL
,
720 _NET_WM_WINDOW_TYPE_SPLASH
,
721 _NET_WM_WINDOW_TYPE_TOOLBAR
,
722 _NET_WM_WINDOW_TYPE_UTILITY
,
723 _SWM_WM_STATE_MANUAL
,
730 } ewmh
[SWM_EWMH_HINT_MAX
] = {
731 /* must be in same order as in the enum */
732 {"_NET_ACTIVE_WINDOW", XCB_ATOM_NONE
},
733 {"_NET_CLIENT_LIST", XCB_ATOM_NONE
},
734 {"_NET_CLOSE_WINDOW", XCB_ATOM_NONE
},
735 {"_NET_CURRENT_DESKTOP", XCB_ATOM_NONE
},
736 {"_NET_DESKTOP_GEOMETRY", XCB_ATOM_NONE
},
737 {"_NET_DESKTOP_NAMES", XCB_ATOM_NONE
},
738 {"_NET_DESKTOP_VIEWPORT", XCB_ATOM_NONE
},
739 {"_NET_MOVERESIZE_WINDOW", XCB_ATOM_NONE
},
740 {"_NET_NUMBER_OF_DESKTOPS", XCB_ATOM_NONE
},
741 {"_NET_RESTACK_WINDOW", XCB_ATOM_NONE
},
742 {"_NET_WM_ACTION_ABOVE", XCB_ATOM_NONE
},
743 {"_NET_WM_ACTION_CLOSE", XCB_ATOM_NONE
},
744 {"_NET_WM_ACTION_FULLSCREEN", XCB_ATOM_NONE
},
745 {"_NET_WM_ACTION_MOVE", XCB_ATOM_NONE
},
746 {"_NET_WM_ACTION_RESIZE", XCB_ATOM_NONE
},
747 {"_NET_WM_ALLOWED_ACTIONS", XCB_ATOM_NONE
},
748 {"_NET_WM_DESKTOP", XCB_ATOM_NONE
},
749 {"_NET_WM_FULL_PLACEMENT", XCB_ATOM_NONE
},
750 {"_NET_WM_NAME", XCB_ATOM_NONE
},
751 {"_NET_WM_STATE", XCB_ATOM_NONE
},
752 {"_NET_WM_STATE_ABOVE", XCB_ATOM_NONE
},
753 {"_NET_WM_STATE_FULLSCREEN", XCB_ATOM_NONE
},
754 {"_NET_WM_STATE_HIDDEN", XCB_ATOM_NONE
},
755 {"_NET_WM_STATE_MAXIMIZED_VERT", XCB_ATOM_NONE
},
756 {"_NET_WM_STATE_MAXIMIZED_HORZ", XCB_ATOM_NONE
},
757 {"_NET_WM_STATE_SKIP_PAGER", XCB_ATOM_NONE
},
758 {"_NET_WM_STATE_SKIP_TASKBAR", XCB_ATOM_NONE
},
759 {"_NET_WM_WINDOW_TYPE", XCB_ATOM_NONE
},
760 {"_NET_WM_WINDOW_TYPE_DIALOG", XCB_ATOM_NONE
},
761 {"_NET_WM_WINDOW_TYPE_DOCK", XCB_ATOM_NONE
},
762 {"_NET_WM_WINDOW_TYPE_NORMAL", XCB_ATOM_NONE
},
763 {"_NET_WM_WINDOW_TYPE_SPLASH", XCB_ATOM_NONE
},
764 {"_NET_WM_WINDOW_TYPE_TOOLBAR", XCB_ATOM_NONE
},
765 {"_NET_WM_WINDOW_TYPE_UTILITY", XCB_ATOM_NONE
},
766 {"_SWM_WM_STATE_MANUAL", XCB_ATOM_NONE
},
769 /* EWMH source type */
771 EWMH_SOURCE_TYPE_NONE
= 0,
772 EWMH_SOURCE_TYPE_NORMAL
= 1,
773 EWMH_SOURCE_TYPE_OTHER
= 2,
780 XC_BOTTOM_LEFT_CORNER
,
781 XC_BOTTOM_RIGHT_CORNER
,
789 char *name
; /* Name used by Xcursor .*/
790 uint8_t cf_char
; /* cursorfont index. */
792 } cursors
[XC_MAX
] = {
793 {"fleur", XC_fleur
, XCB_CURSOR_NONE
},
794 {"left_ptr", XC_left_ptr
, XCB_CURSOR_NONE
},
795 {"bottom_left_corner", XC_bottom_left_corner
, XCB_CURSOR_NONE
},
796 {"bottom_right_corner", XC_bottom_right_corner
, XCB_CURSOR_NONE
},
797 {"sizing", XC_sizing
, XCB_CURSOR_NONE
},
798 {"top_left_corner", XC_top_left_corner
, XCB_CURSOR_NONE
},
799 {"top_right_corner", XC_top_right_corner
, XCB_CURSOR_NONE
},
802 #define SWM_SPAWN_OPTIONAL 0x1
806 TAILQ_ENTRY(spawn_prog
) entry
;
812 TAILQ_HEAD(spawn_list
, spawn_prog
);
813 struct spawn_list spawns
= TAILQ_HEAD_INITIALIZER(spawns
);
815 /* user/key callable function IDs */
931 KF_DUMPWINS
, /* MUST BE LAST */
939 enum keyfuncid funcid
;
942 RB_HEAD(key_tree
, key
);
944 /* function prototypes */
945 void adjust_font(struct ws_win
*);
946 char *argsep(char **);
947 void bar_cleanup(struct swm_region
*);
948 void bar_extra_setup(void);
949 void bar_extra_stop(void);
950 int bar_extra_update(void);
951 void bar_fmt(const char *, char *, struct swm_region
*, size_t);
952 void bar_fmt_expand(char *, size_t);
954 void bar_print(struct swm_region
*, const char *);
955 void bar_print_legacy(struct swm_region
*, const char *);
956 void bar_replace(char *, char *, struct swm_region
*, size_t);
957 void bar_replace_pad(char *, int *, size_t);
958 char *bar_replace_seq(char *, char *, struct swm_region
*, size_t *, size_t);
959 void bar_setup(struct swm_region
*);
960 void bar_toggle(struct swm_region
*, union arg
*);
961 void bar_urgent(char *, size_t);
962 void bar_window_class(char *, size_t, struct swm_region
*);
963 void bar_window_class_instance(char *, size_t, struct swm_region
*);
964 void bar_window_float(char *, size_t, struct swm_region
*);
965 void bar_window_instance(char *, size_t, struct swm_region
*);
966 void bar_window_name(char *, size_t, struct swm_region
*);
967 void bar_window_state(char *, size_t, struct swm_region
*);
968 void bar_workspace_name(char *, size_t, struct swm_region
*);
969 void buttonpress(xcb_button_press_event_t
*);
970 void center_pointer(struct swm_region
*);
971 void check_conn(void);
972 void clear_keys(void);
973 int clear_maximized(struct workspace
*);
974 void clientmessage(xcb_client_message_event_t
*);
975 void client_msg(struct ws_win
*, xcb_atom_t
, xcb_timestamp_t
);
976 int conf_load(const char *, int);
977 void configurenotify(xcb_configure_notify_event_t
*);
978 void configurerequest(xcb_configure_request_event_t
*);
979 void config_win(struct ws_win
*, xcb_configure_request_event_t
*);
980 void constrain_window(struct ws_win
*, struct swm_geometry
*, int *);
981 int count_win(struct workspace
*, bool);
982 void cursors_cleanup(void);
983 void cursors_load(void);
984 void custom_region(const char *);
985 void cyclerg(struct swm_region
*, union arg
*);
986 void cyclews(struct swm_region
*, union arg
*);
987 void cycle_layout(struct swm_region
*, union arg
*);
988 void destroynotify(xcb_destroy_notify_event_t
*);
989 void dumpwins(struct swm_region
*, union arg
*);
991 void enternotify(xcb_enter_notify_event_t
*);
992 void event_drain(uint8_t);
993 void event_error(xcb_generic_error_t
*);
994 void event_handle(xcb_generic_event_t
*);
995 void ewmh_apply_flags(struct ws_win
*, uint32_t);
996 void ewmh_autoquirk(struct ws_win
*);
997 void ewmh_get_desktop_names(void);
998 void ewmh_get_wm_state(struct ws_win
*);
999 void ewmh_update_actions(struct ws_win
*);
1000 void ewmh_update_client_list(void);
1001 void ewmh_update_current_desktop(void);
1002 void ewmh_update_desktop_names(void);
1003 void ewmh_update_desktops(void);
1004 void ewmh_change_wm_state(struct ws_win
*, xcb_atom_t
, long);
1005 void ewmh_update_wm_state(struct ws_win
*);
1006 char *expand_tilde(const char *);
1007 void expose(xcb_expose_event_t
*);
1008 void fake_keypress(struct ws_win
*, xcb_keysym_t
, uint16_t);
1009 struct pid_e
*find_pid(pid_t
);
1010 struct ws_win
*find_unmanaged_window(xcb_window_t
);
1011 struct ws_win
*find_window(xcb_window_t
);
1012 void floating_toggle(struct swm_region
*, union arg
*);
1013 void focus(struct swm_region
*, union arg
*);
1015 void focusin(xcb_focus_in_event_t
*);
1016 void focusout(xcb_focus_out_event_t
*);
1018 void focus_flush(void);
1019 void focus_region(struct swm_region
*);
1020 void focusrg(struct swm_region
*, union arg
*);
1021 void focus_win(struct ws_win
*);
1022 void fontset_init(void);
1023 void free_window(struct ws_win
*);
1024 xcb_atom_t
get_atom_from_string(const char *);
1026 char *get_atom_name(xcb_atom_t
);
1028 struct ws_win
*get_focus_magic(struct ws_win
*);
1029 struct ws_win
*get_focus_prev(struct ws_win
*);
1031 char *get_notify_detail_label(uint8_t);
1032 char *get_notify_mode_label(uint8_t);
1034 struct ws_win
*get_pointer_win(xcb_window_t
);
1035 struct ws_win
*get_region_focus(struct swm_region
*);
1036 int get_region_index(struct swm_region
*);
1037 xcb_screen_t
*get_screen(int);
1038 int get_screen_count(void);
1040 char *get_source_type_label(uint32_t);
1041 char *get_stack_mode_name(uint8_t);
1043 int32_t get_swm_ws(xcb_window_t
);
1044 bool get_urgent(struct ws_win
*);
1045 char *get_win_name(xcb_window_t
);
1046 uint8_t get_win_state(xcb_window_t
);
1047 void get_wm_protocols(struct ws_win
*);
1048 int get_ws_idx(struct ws_win
*);
1049 void grabbuttons(struct ws_win
*);
1050 void grabkeys(void);
1051 void grab_windows(void);
1052 void iconify(struct swm_region
*, union arg
*);
1053 bool isxlfd(char *);
1054 void keypress(xcb_key_press_event_t
*);
1055 int key_cmp(struct key
*, struct key
*);
1056 void key_insert(unsigned int, KeySym
, enum keyfuncid
, const char *);
1057 struct key
*key_lookup(unsigned int, KeySym
);
1058 void key_remove(struct key
*);
1059 void key_replace(struct key
*, unsigned int, KeySym
, enum keyfuncid
,
1061 void kill_bar_extra_atexit(void);
1062 void kill_refs(struct ws_win
*);
1064 void leavenotify(xcb_leave_notify_event_t
*);
1066 void load_float_geom(struct ws_win
*);
1067 void lower_window(struct ws_win
*);
1068 struct ws_win
*manage_window(xcb_window_t
, int, bool);
1069 void map_window(struct ws_win
*);
1070 void mapnotify(xcb_map_notify_event_t
*);
1071 void mappingnotify(xcb_mapping_notify_event_t
*);
1072 void maprequest(xcb_map_request_event_t
*);
1073 void maximize_toggle(struct swm_region
*, union arg
*);
1074 void motionnotify(xcb_motion_notify_event_t
*);
1075 void move(struct ws_win
*, union arg
*);
1076 void move_step(struct swm_region
*, union arg
*);
1077 uint32_t name_to_pixel(int, const char *);
1078 void name_workspace(struct swm_region
*, union arg
*);
1079 void new_region(struct swm_screen
*, int, int, int, int);
1080 int parsekeys(const char *, unsigned int, unsigned int *, KeySym
*);
1081 int parsequirks(const char *, uint32_t *, int *);
1082 int parse_rgb(const char *, uint16_t *, uint16_t *, uint16_t *);
1083 void pressbutton(struct swm_region
*, union arg
*);
1084 void priorws(struct swm_region
*, union arg
*);
1086 void print_win_geom(xcb_window_t
);
1088 void propertynotify(xcb_property_notify_event_t
*);
1089 void quirk_free(struct quirk
*);
1090 void quirk_insert(const char *, const char *, const char *, uint32_t, int);
1091 void quirk_remove(struct quirk
*);
1092 void quirk_replace(struct quirk
*, const char *, const char *, const char *,
1094 void quit(struct swm_region
*, union arg
*);
1095 void raise_toggle(struct swm_region
*, union arg
*);
1096 void raise_window(struct ws_win
*);
1097 void region_containment(struct ws_win
*, struct swm_region
*, int);
1098 struct swm_region
*region_under(struct swm_screen
*, int, int);
1099 void regionize(struct ws_win
*, int, int);
1100 void resize(struct ws_win
*, union arg
*);
1101 void resize_step(struct swm_region
*, union arg
*);
1102 void restart(struct swm_region
*, union arg
*);
1103 struct swm_region
*root_to_region(xcb_window_t
, int);
1104 void screenchange(xcb_randr_screen_change_notify_event_t
*);
1105 void scan_randr(int);
1106 void search_do_resp(void);
1107 void search_resp_name_workspace(const char *, size_t);
1108 void search_resp_search_window(const char *);
1109 void search_resp_search_workspace(const char *);
1110 void search_resp_uniconify(const char *, size_t);
1111 void search_win(struct swm_region
*, union arg
*);
1112 void search_win_cleanup(void);
1113 void search_workspace(struct swm_region
*, union arg
*);
1114 void send_to_rg(struct swm_region
*, union arg
*);
1115 void send_to_ws(struct swm_region
*, union arg
*);
1116 void set_region(struct swm_region
*);
1117 int setautorun(const char *, const char *, int);
1118 int setconfbinding(const char *, const char *, int);
1119 int setconfcolor(const char *, const char *, int);
1120 int setconfmodkey(const char *, const char *, int);
1121 int setconfquirk(const char *, const char *, int);
1122 int setconfregion(const char *, const char *, int);
1123 int setconfspawn(const char *, const char *, int);
1124 int setconfvalue(const char *, const char *, int);
1125 void setkeybinding(unsigned int, KeySym
, enum keyfuncid
, const char *);
1126 int setkeymapping(const char *, const char *, int);
1127 int setlayout(const char *, const char *, int);
1128 void setquirk(const char *, const char *, const char *, uint32_t, int);
1129 void setscreencolor(const char *, int, int);
1130 void setspawn(const char *, const char *, int);
1131 void setup_ewmh(void);
1132 void setup_globals(void);
1133 void setup_keys(void);
1134 void setup_quirks(void);
1135 void setup_screens(void);
1136 void setup_spawn(void);
1137 void set_child_transient(struct ws_win
*, xcb_window_t
*);
1138 void set_win_state(struct ws_win
*, uint8_t);
1139 void shutdown_cleanup(void);
1141 void socket_setnonblock(int);
1142 void sort_windows(struct ws_win_list
*);
1143 void spawn(int, union arg
*, bool);
1144 void spawn_custom(struct swm_region
*, union arg
*, const char *);
1145 int spawn_expand(struct swm_region
*, union arg
*, const char *, char ***);
1146 void spawn_insert(const char *, const char *, int);
1147 struct spawn_prog
*spawn_find(const char *);
1148 void spawn_remove(struct spawn_prog
*);
1149 void spawn_replace(struct spawn_prog
*, const char *, const char *, int);
1150 void spawn_select(struct swm_region
*, union arg
*, const char *, int *);
1151 void stack_config(struct swm_region
*, union arg
*);
1152 void stack_master(struct workspace
*, struct swm_geometry
*, int, bool);
1153 void store_float_geom(struct ws_win
*);
1154 char *strdupsafe(const char *);
1155 void swapwin(struct swm_region
*, union arg
*);
1156 void switchws(struct swm_region
*, union arg
*);
1157 void teardown_ewmh(void);
1158 void unescape_selector(char *);
1159 void unfocus_win(struct ws_win
*);
1160 void uniconify(struct swm_region
*, union arg
*);
1161 void unmanage_window(struct ws_win
*);
1162 void unmapnotify(xcb_unmap_notify_event_t
*);
1163 void unmap_all(void);
1164 void unmap_window(struct ws_win
*);
1165 void updatenumlockmask(void);
1166 void update_floater(struct ws_win
*);
1167 void update_modkey(unsigned int);
1168 void update_win_stacking(struct ws_win
*);
1169 void update_window(struct ws_win
*);
1170 void update_window_color(struct ws_win
*);
1171 void update_wm_state(struct ws_win
*win
);
1172 void validate_spawns(void);
1173 int validate_win(struct ws_win
*);
1174 int validate_ws(struct workspace
*);
1175 void version(struct swm_region
*, union arg
*);
1176 void win_to_ws(struct ws_win
*, int, bool);
1177 pid_t
window_get_pid(xcb_window_t
);
1178 void wkill(struct swm_region
*, union arg
*);
1179 void update_ws_stack(struct workspace
*);
1180 void xft_init(struct swm_region
*);
1181 void _add_startup_exception(const char *, va_list);
1182 void add_startup_exception(const char *, ...);
1184 RB_PROTOTYPE(key_tree
, key
, entry
, key_cmp
);
1185 RB_GENERATE(key_tree
, key
, entry
, key_cmp
);
1186 struct key_tree keys
;
1191 xcb_font_t cf
= XCB_NONE
;
1194 for (i
= 0; i
< LENGTH(cursors
); ++i
) {
1195 /* try to load Xcursor first. */
1196 cursors
[i
].cid
= XcursorLibraryLoadCursor(display
,
1199 /* fallback to cursorfont. */
1200 if (cursors
[i
].cid
== XCB_CURSOR_NONE
) {
1201 if (cf
== XCB_NONE
) {
1202 cf
= xcb_generate_id(conn
);
1203 xcb_open_font(conn
, cf
, strlen("cursor"),
1207 cursors
[i
].cid
= xcb_generate_id(conn
);
1208 xcb_create_glyph_cursor(conn
, cursors
[i
].cid
, cf
, cf
,
1209 cursors
[i
].cf_char
, cursors
[i
].cf_char
+ 1, 0, 0, 0,
1210 0xffff, 0xffff, 0xffff);
1216 xcb_close_font(conn
, cf
);
1220 cursors_cleanup(void)
1223 for (i
= 0; i
< LENGTH(cursors
); ++i
)
1224 xcb_free_cursor(conn
, cursors
[i
].cid
);
1228 expand_tilde(const char *s
)
1230 struct passwd
*ppwd
;
1238 errx(1, "expand_tilde: NULL string.");
1241 result
= strdup(sc
);
1247 if ((max
= sysconf(_SC_LOGIN_NAME_MAX
)) == -1)
1248 errx(1, "expand_tilde: sysconf");
1250 if ((user
= calloc(1, max
+ 1)) == NULL
)
1251 errx(1, "expand_tilde: calloc");
1253 for (i
= 0; s
[i
] != '/' && s
[i
] != '\0'; ++i
)
1258 ppwd
= strlen(user
) == 0 ? getpwuid(getuid()) : getpwnam(user
);
1262 result
= strdup(sc
);
1264 if (asprintf(&result
, "%s%s", ppwd
->pw_dir
, s
) == -1)
1268 errx(1, "expand_tilde: failed to allocate memory.");
1274 parse_rgb(const char *rgb
, uint16_t *rr
, uint16_t *gg
, uint16_t *bb
)
1276 unsigned int tmpr
, tmpg
, tmpb
;
1278 if (sscanf(rgb
, "rgb:%x/%x/%x", &tmpr
, &tmpg
, &tmpb
) != 3)
1281 *rr
= RGB_8_TO_16(tmpr
);
1282 *gg
= RGB_8_TO_16(tmpg
);
1283 *bb
= RGB_8_TO_16(tmpb
);
1289 get_screen(int screen
)
1291 const xcb_setup_t
*r
;
1292 xcb_screen_iterator_t iter
;
1294 if ((r
= xcb_get_setup(conn
)) == NULL
) {
1295 DNPRINTF(SWM_D_MISC
, "get_screen: xcb_get_setup\n");
1299 iter
= xcb_setup_roots_iterator(r
);
1300 for (; iter
.rem
; --screen
, xcb_screen_next(&iter
))
1308 get_screen_count(void)
1310 const xcb_setup_t
*r
;
1312 if ((r
= xcb_get_setup(conn
)) == NULL
) {
1313 DNPRINTF(SWM_D_MISC
, "get_screen_count: xcb_get_setup\n");
1317 return xcb_setup_roots_length(r
);
1321 get_region_index(struct swm_region
*r
)
1323 struct swm_region
*rr
;
1329 TAILQ_FOREACH(rr
, &r
->s
->rl
, entry
) {
1344 if (focus_mode
== SWM_FOCUS_DEFAULT
)
1345 event_drain(XCB_ENTER_NOTIFY
);
1351 get_atom_from_string(const char *str
)
1353 xcb_intern_atom_cookie_t c
;
1354 xcb_intern_atom_reply_t
*r
;
1357 c
= xcb_intern_atom(conn
, 0, strlen(str
), str
);
1358 r
= xcb_intern_atom_reply(conn
, c
, NULL
);
1366 return (XCB_ATOM_NONE
);
1370 get_wm_protocols(struct ws_win
*win
) {
1372 xcb_icccm_get_wm_protocols_reply_t wpr
;
1374 if (xcb_icccm_get_wm_protocols_reply(conn
,
1375 xcb_icccm_get_wm_protocols(conn
, win
->id
, a_prot
),
1377 for (i
= 0; i
< (int)wpr
.atoms_len
; i
++) {
1378 if (wpr
.atoms
[i
] == a_takefocus
)
1379 win
->take_focus
= true;
1380 if (wpr
.atoms
[i
] == a_delete
)
1381 win
->can_delete
= true;
1383 xcb_icccm_get_wm_protocols_reply_wipe(&wpr
);
1390 xcb_window_t root
, win
;
1391 int i
, j
, num_screens
;
1393 for (i
= 0; i
< LENGTH(ewmh
); i
++)
1394 ewmh
[i
].atom
= get_atom_from_string(ewmh
[i
].name
);
1396 num_screens
= get_screen_count();
1397 for (i
= 0; i
< num_screens
; i
++) {
1398 root
= screens
[i
].root
;
1400 /* Set up _NET_SUPPORTING_WM_CHECK. */
1401 win
= xcb_generate_id(conn
);
1402 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, win
, root
,
1403 0, 0, 1, 1, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
1404 XCB_COPY_FROM_PARENT
, 0, NULL
);
1406 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, root
,
1407 a_net_wm_check
, XCB_ATOM_WINDOW
, 32, 1, &win
);
1408 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
1409 a_net_wm_check
, XCB_ATOM_WINDOW
, 32, 1, &win
);
1412 * Impersonate LG3D non-reparenting WM, written by Sun, to
1413 * workaround a Java GUI rendering issue.
1415 if (java_workaround
)
1416 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
1417 ewmh
[_NET_WM_NAME
].atom
, a_utf8_string
,
1418 8, strlen("LG3D"), "LG3D");
1420 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
1421 ewmh
[_NET_WM_NAME
].atom
, a_utf8_string
,
1422 8, strlen("spectrwm"), "spectrwm");
1424 /* Report supported atoms */
1425 xcb_delete_property(conn
, root
, a_net_supported
);
1426 for (j
= 0; j
< LENGTH(ewmh
); j
++)
1427 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, root
,
1428 a_net_supported
, XCB_ATOM_ATOM
, 32, 1,
1433 ewmh_update_desktops();
1434 ewmh_get_desktop_names();
1442 xcb_get_property_cookie_t pc
;
1443 xcb_get_property_reply_t
*pr
;
1445 num_screens
= get_screen_count();
1447 for (i
= 0; i
< num_screens
; i
++) {
1448 /* Get the support check window and destroy it */
1449 pc
= xcb_get_property(conn
, 0, screens
[i
].root
, a_net_wm_check
,
1450 XCB_ATOM_WINDOW
, 0, 1);
1451 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
1454 if (pr
->format
== a_net_wm_check
) {
1455 id
= *((xcb_window_t
*)xcb_get_property_value(pr
));
1457 xcb_destroy_window(conn
, id
);
1458 xcb_delete_property(conn
, screens
[i
].root
,
1460 xcb_delete_property(conn
, screens
[i
].root
,
1468 ewmh_autoquirk(struct ws_win
*win
)
1470 xcb_get_property_reply_t
*r
;
1471 xcb_get_property_cookie_t c
;
1475 c
= xcb_get_property(conn
, 0, win
->id
,
1476 ewmh
[_NET_WM_WINDOW_TYPE
].atom
, XCB_ATOM_ATOM
, 0, UINT32_MAX
);
1477 r
= xcb_get_property_reply(conn
, c
, NULL
);
1481 type
= xcb_get_property_value(r
);
1482 n
= xcb_get_property_value_length(r
) / sizeof(xcb_atom_t
);
1484 for (i
= 0; i
< n
; i
++) {
1485 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_NORMAL
].atom
)
1487 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_DOCK
].atom
||
1488 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_TOOLBAR
].atom
||
1489 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_UTILITY
].atom
) {
1490 win
->quirks
= SWM_Q_FLOAT
| SWM_Q_ANYWHERE
;
1493 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_SPLASH
].atom
||
1494 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_DIALOG
].atom
) {
1495 win
->quirks
= SWM_Q_FLOAT
;
1503 ewmh_update_actions(struct ws_win
*win
)
1505 xcb_atom_t actions
[SWM_EWMH_ACTION_COUNT_MAX
];
1511 actions
[n
++] = ewmh
[_NET_WM_ACTION_CLOSE
].atom
;
1514 actions
[n
++] = ewmh
[_NET_WM_ACTION_MOVE
].atom
;
1515 actions
[n
++] = ewmh
[_NET_WM_ACTION_RESIZE
].atom
;
1516 actions
[n
++] = ewmh
[_NET_WM_ACTION_ABOVE
].atom
;
1519 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
1520 ewmh
[_NET_WM_ALLOWED_ACTIONS
].atom
, XCB_ATOM_ATOM
, 32, 1, actions
);
1523 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
1524 #define _NET_WM_STATE_ADD 1 /* add/set property */
1525 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
1528 ewmh_change_wm_state(struct ws_win
*win
, xcb_atom_t state
, long action
)
1535 name
= get_atom_name(state
);
1536 DNPRINTF(SWM_D_PROP
, "ewmh_change_wm_state: win %#x, state: %s, "
1537 "action: %ld\n", WINID(win
), name
, action
);
1543 if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
)
1544 flag
= EWMH_F_FULLSCREEN
;
1545 else if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
)
1546 flag
= EWMH_F_ABOVE
;
1547 else if (state
== ewmh
[_NET_WM_STATE_HIDDEN
].atom
)
1548 flag
= EWMH_F_HIDDEN
;
1549 else if (state
== ewmh
[_NET_WM_STATE_MAXIMIZED_VERT
].atom
||
1550 state
== ewmh
[_NET_WM_STATE_MAXIMIZED_HORZ
].atom
)
1551 flag
= EWMH_F_MAXIMIZED
;
1552 else if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
)
1553 flag
= SWM_F_MANUAL
;
1554 else if (state
== ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
)
1555 flag
= EWMH_F_SKIP_PAGER
;
1556 else if (state
== ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
)
1557 flag
= EWMH_F_SKIP_TASKBAR
;
1559 /* Disallow unfloating transients. */
1560 if (TRANS(win
) && flag
== EWMH_F_ABOVE
)
1563 new_flags
= win
->ewmh_flags
;
1566 case _NET_WM_STATE_REMOVE
:
1569 case _NET_WM_STATE_ADD
:
1572 case _NET_WM_STATE_TOGGLE
:
1577 ewmh_apply_flags(win
, new_flags
);
1580 DNPRINTF(SWM_D_PROP
, "ewmh_change_wm_state: done.\n");
1584 ewmh_apply_flags(struct ws_win
*win
, uint32_t pending
)
1586 struct workspace
*ws
;
1589 changed
= win
->ewmh_flags
^ pending
;
1593 DNPRINTF(SWM_D_PROP
, "ewmh_apply_flags: pending: %d\n", pending
);
1595 win
->ewmh_flags
= pending
;
1598 if (changed
& EWMH_F_HIDDEN
) {
1600 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
1601 ws
->focus_pending
= get_focus_prev(win
);
1606 /* Reload floating geometry in case region changed. */
1608 load_float_geom(win
);
1610 /* The window is no longer iconic, prepare focus. */
1611 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
1612 ws
->focus_pending
= get_focus_magic(win
);
1617 if (changed
& EWMH_F_ABOVE
) {
1618 if (ws
->cur_layout
!= &layouts
[SWM_MAX_STACK
]) {
1620 load_float_geom(win
);
1621 else if (!MAXIMIZED(win
))
1622 store_float_geom(win
);
1624 win
->ewmh_flags
&= ~EWMH_F_MAXIMIZED
;
1625 changed
&= ~EWMH_F_MAXIMIZED
;
1629 win
->ewmh_flags
^= EWMH_F_ABOVE
& pending
;
1633 if (changed
& EWMH_F_MAXIMIZED
) {
1634 /* VERT and/or HORZ changed. */
1636 if (!MAXIMIZED(win
))
1637 load_float_geom(win
);
1639 store_float_geom(win
);
1642 if (MAXIMIZED(win
)) {
1643 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
1644 ws
->cur_layout
!= &layouts
[SWM_MAX_STACK
]) {
1648 ws
->focus_pending
= win
;
1652 update_window_color(win
);
1656 if (changed
& EWMH_F_FULLSCREEN
) {
1657 if (FULLSCREEN(win
)) {
1658 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
1662 ws
->focus_pending
= win
;
1665 load_float_geom(win
);
1668 win
->ewmh_flags
&= ~EWMH_F_MAXIMIZED
;
1672 DNPRINTF(SWM_D_PROP
, "ewmh_apply_flags: done.\n");
1676 ewmh_update_wm_state(struct ws_win
*win
) {
1677 xcb_atom_t vals
[SWM_EWMH_ACTION_COUNT_MAX
];
1681 vals
[n
++] = ewmh
[_NET_WM_STATE_HIDDEN
].atom
;
1682 if (FULLSCREEN(win
))
1683 vals
[n
++] = ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
;
1684 if (MAXIMIZED_VERT(win
))
1685 vals
[n
++] = ewmh
[_NET_WM_STATE_MAXIMIZED_VERT
].atom
;
1686 if (MAXIMIZED_HORZ(win
))
1687 vals
[n
++] = ewmh
[_NET_WM_STATE_MAXIMIZED_HORZ
].atom
;
1688 if (win
->ewmh_flags
& EWMH_F_SKIP_PAGER
)
1689 vals
[n
++] = ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
;
1690 if (win
->ewmh_flags
& EWMH_F_SKIP_TASKBAR
)
1691 vals
[n
++] = ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
;
1692 if (win
->ewmh_flags
& EWMH_F_ABOVE
)
1693 vals
[n
++] = ewmh
[_NET_WM_STATE_ABOVE
].atom
;
1694 if (win
->ewmh_flags
& SWM_F_MANUAL
)
1695 vals
[n
++] = ewmh
[_SWM_WM_STATE_MANUAL
].atom
;
1698 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
1699 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, n
, vals
);
1701 xcb_delete_property(conn
, win
->id
, ewmh
[_NET_WM_STATE
].atom
);
1705 ewmh_get_wm_state(struct ws_win
*win
)
1708 xcb_get_property_cookie_t c
;
1709 xcb_get_property_reply_t
*r
;
1715 win
->ewmh_flags
= 0;
1717 c
= xcb_get_property(conn
, 0, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
1718 XCB_ATOM_ATOM
, 0, UINT32_MAX
);
1719 r
= xcb_get_property_reply(conn
, c
, NULL
);
1723 states
= xcb_get_property_value(r
);
1724 n
= xcb_get_property_value_length(r
) / sizeof(xcb_atom_t
);
1726 for (i
= 0; i
< n
; i
++)
1727 ewmh_change_wm_state(win
, states
[i
], _NET_WM_STATE_ADD
);
1735 dumpwins(struct swm_region
*r
, union arg
*args
)
1739 xcb_get_window_attributes_cookie_t c
;
1740 xcb_get_window_attributes_reply_t
*wa
;
1742 /* suppress unused warning since var is needed */
1745 if (r
->ws
== NULL
) {
1746 DPRINTF("dumpwins: invalid workspace\n");
1750 DPRINTF("=== managed window list ws %02d ===\n", r
->ws
->idx
);
1751 TAILQ_FOREACH(w
, &r
->ws
->winlist
, entry
) {
1752 state
= get_win_state(w
->id
);
1753 c
= xcb_get_window_attributes(conn
, w
->id
);
1754 wa
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
1756 DPRINTF("win %#x, map_state: %d, state: %u, "
1757 "transient: %#x\n", w
->id
, wa
->map_state
,
1758 state
, w
->transient
);
1761 DPRINTF("win %#x, failed xcb_get_window_attributes\n",
1765 DPRINTF("=== stacking order (top down) === \n");
1766 TAILQ_FOREACH(w
, &r
->ws
->stack
, stack_entry
) {
1767 DPRINTF("win %#x, fs: %s, maximized: %s, above: %s, "
1768 "iconic: %s\n", w
->id
, YESNO(FULLSCREEN(w
)),
1769 YESNO(MAXIMIZED(w
)), YESNO(ABOVE(w
)), YESNO(ICONIC(w
)));
1772 DPRINTF("===== unmanaged window list =====\n");
1773 TAILQ_FOREACH(w
, &r
->ws
->unmanagedlist
, entry
) {
1774 state
= get_win_state(w
->id
);
1775 c
= xcb_get_window_attributes(conn
, w
->id
);
1776 wa
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
1778 DPRINTF("win %#x, map_state: %d, state: %u, "
1779 "transient: %#x\n", w
->id
, wa
->map_state
,
1780 state
, w
->transient
);
1783 DPRINTF("win %#x, failed xcb_get_window_attributes\n",
1787 DPRINTF("=================================\n");
1791 dumpwins(struct swm_region
*r
, union arg
*s
)
1796 #endif /* SWM_DEBUG */
1801 int saved_errno
, status
;
1804 saved_errno
= errno
;
1808 while ((pid
= waitpid(WAIT_ANY
, &status
, WNOHANG
)) != 0) {
1813 if (errno
!= ECHILD
)
1814 warn("sighdlr: waitpid");
1815 #endif /* SWM_DEBUG */
1818 if (pid
== searchpid
)
1822 if (WIFEXITED(status
)) {
1823 if (WEXITSTATUS(status
) != 0)
1824 warnx("sighdlr: child exit status: %d",
1825 WEXITSTATUS(status
));
1827 warnx("sighdlr: child is terminated "
1829 #endif /* SWM_DEBUG */
1843 errno
= saved_errno
;
1849 struct pid_e
*p
= NULL
;
1851 DNPRINTF(SWM_D_MISC
, "find_pid: %d\n", pid
);
1856 TAILQ_FOREACH(p
, &pidlist
, entry
) {
1865 name_to_pixel(int sidx
, const char *colorname
)
1867 uint32_t result
= 0;
1868 char cname
[32] = "#";
1869 xcb_screen_t
*screen
;
1870 xcb_colormap_t cmap
;
1871 xcb_alloc_color_reply_t
*cr
;
1872 xcb_alloc_named_color_reply_t
*nr
;
1873 uint16_t rr
, gg
, bb
;
1875 screen
= get_screen(sidx
);
1876 cmap
= screen
->default_colormap
;
1878 /* color is in format rgb://rr/gg/bb */
1879 if (strncmp(colorname
, "rgb:", 4) == 0) {
1880 if (parse_rgb(colorname
, &rr
, &gg
, &bb
) == -1)
1881 warnx("could not parse rgb %s", colorname
);
1883 cr
= xcb_alloc_color_reply(conn
,
1884 xcb_alloc_color(conn
, cmap
, rr
, gg
, bb
),
1890 warnx("color '%s' not found", colorname
);
1893 nr
= xcb_alloc_named_color_reply(conn
,
1894 xcb_alloc_named_color(conn
, cmap
, strlen(colorname
),
1897 strlcat(cname
, colorname
+ 2, sizeof cname
- 1);
1898 nr
= xcb_alloc_named_color_reply(conn
,
1899 xcb_alloc_named_color(conn
, cmap
, strlen(cname
),
1906 warnx("color '%s' not found", colorname
);
1913 setscreencolor(const char *val
, int i
, int c
)
1915 if (i
< 0 || i
>= get_screen_count())
1918 screens
[i
].c
[c
].pixel
= name_to_pixel(i
, val
);
1919 free(screens
[i
].c
[c
].name
);
1920 if ((screens
[i
].c
[c
].name
= strdup(val
)) == NULL
)
1925 fancy_stacker(struct workspace
*ws
)
1927 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1928 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1929 snprintf(ws
->stacker
, sizeof ws
->stacker
,
1930 ws
->l_state
.vertical_flip
? "[%d>%d]" : "[%d|%d]",
1931 ws
->l_state
.vertical_mwin
, ws
->l_state
.vertical_stacks
);
1932 else if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1933 snprintf(ws
->stacker
, sizeof ws
->stacker
,
1934 ws
->l_state
.horizontal_flip
? "[%dv%d]" : "[%d-%d]",
1935 ws
->l_state
.horizontal_mwin
, ws
->l_state
.horizontal_stacks
);
1939 plain_stacker(struct workspace
*ws
)
1941 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1942 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1943 strlcpy(ws
->stacker
, ws
->l_state
.vertical_flip
? "[>]" : "[|]",
1944 sizeof ws
->stacker
);
1945 else if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1946 strlcpy(ws
->stacker
, ws
->l_state
.horizontal_flip
? "[v]" : "[-]",
1947 sizeof ws
->stacker
);
1951 custom_region(const char *val
)
1953 unsigned int x
, y
, w
, h
;
1954 int sidx
, num_screens
;
1955 xcb_screen_t
*screen
;
1957 num_screens
= get_screen_count();
1958 if (sscanf(val
, "screen[%d]:%ux%u+%u+%u", &sidx
, &w
, &h
, &x
, &y
) != 5)
1959 errx(1, "invalid custom region, "
1960 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1961 if (sidx
< 1 || sidx
> num_screens
)
1962 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1966 if ((screen
= get_screen(sidx
)) == NULL
)
1967 errx(1, "ERROR: can't get screen %d.", sidx
);
1970 errx(1, "region %ux%u+%u+%u too small", w
, h
, x
, y
);
1972 if (x
> screen
->width_in_pixels
||
1973 y
> screen
->height_in_pixels
||
1974 w
+ x
> screen
->width_in_pixels
||
1975 h
+ y
> screen
->height_in_pixels
) {
1976 warnx("ignoring region %ux%u+%u+%u - not within screen "
1977 "boundaries (%ux%u)", w
, h
, x
, y
,
1978 screen
->width_in_pixels
, screen
->height_in_pixels
);
1982 new_region(&screens
[sidx
], x
, y
, w
, h
);
1986 socket_setnonblock(int fd
)
1990 if ((flags
= fcntl(fd
, F_GETFL
, 0)) == -1)
1991 err(1, "fcntl F_GETFL");
1992 flags
|= O_NONBLOCK
;
1993 if ((flags
= fcntl(fd
, F_SETFL
, flags
)) == -1)
1994 err(1, "fcntl F_SETFL");
1998 bar_print_legacy(struct swm_region
*r
, const char *s
)
2000 xcb_rectangle_t rect
;
2005 XRectangle ibox
, lbox
;
2009 XmbTextExtents(bar_fs
, s
, len
, &ibox
, &lbox
);
2011 switch (bar_justify
) {
2012 case SWM_BAR_JUSTIFY_LEFT
:
2015 case SWM_BAR_JUSTIFY_CENTER
:
2016 x
= (WIDTH(r
) - lbox
.width
) / 2;
2018 case SWM_BAR_JUSTIFY_RIGHT
:
2019 x
= WIDTH(r
) - lbox
.width
- SWM_BAR_OFFSET
;
2023 if (x
< SWM_BAR_OFFSET
)
2028 rect
.width
= WIDTH(r
->bar
);
2029 rect
.height
= HEIGHT(r
->bar
);
2031 /* clear back buffer */
2032 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
2033 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
2034 xcb_poly_fill_rectangle(conn
, r
->bar
->buffer
, r
->s
->bar_gc
, 1, &rect
);
2036 /* draw back buffer */
2037 gcvd
.graphics_exposures
= 0;
2038 draw
= XCreateGC(display
, r
->bar
->buffer
, GCGraphicsExposures
, &gcvd
);
2039 XSetForeground(display
, draw
, r
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
);
2040 DRAWSTRING(display
, r
->bar
->buffer
, bar_fs
, draw
,
2041 x
, (bar_fs_extents
->max_logical_extent
.height
- lbox
.height
) / 2 -
2043 XFreeGC(display
, draw
);
2046 xcb_copy_area(conn
, r
->bar
->buffer
, r
->bar
->id
, r
->s
->bar_gc
, 0, 0,
2047 0, 0, WIDTH(r
->bar
), HEIGHT(r
->bar
));
2051 bar_print(struct swm_region
*r
, const char *s
)
2054 xcb_rectangle_t rect
;
2062 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
, &info
);
2064 switch (bar_justify
) {
2065 case SWM_BAR_JUSTIFY_LEFT
:
2068 case SWM_BAR_JUSTIFY_CENTER
:
2069 x
= (WIDTH(r
) - info
.width
) / 2;
2071 case SWM_BAR_JUSTIFY_RIGHT
:
2072 x
= WIDTH(r
) - info
.width
- SWM_BAR_OFFSET
;
2076 if (x
< SWM_BAR_OFFSET
)
2081 rect
.width
= WIDTH(r
->bar
);
2082 rect
.height
= HEIGHT(r
->bar
);
2084 /* clear back buffer */
2085 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
2086 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
2087 xcb_poly_fill_rectangle(conn
, r
->bar
->buffer
, r
->s
->bar_gc
, 1, &rect
);
2089 /* draw back buffer */
2090 draw
= XftDrawCreate(display
, r
->bar
->buffer
,
2091 DefaultVisual(display
, r
->s
->idx
),
2092 DefaultColormap(display
, r
->s
->idx
));
2094 XftDrawStringUtf8(draw
, &bar_font_color
, bar_font
, x
,
2095 (HEIGHT(r
->bar
) + bar_font
->height
) / 2 - bar_font
->descent
,
2098 XftDrawDestroy(draw
);
2101 xcb_copy_area(conn
, r
->bar
->buffer
, r
->bar
->id
, r
->s
->bar_gc
, 0, 0,
2102 0, 0, WIDTH(r
->bar
), HEIGHT(r
->bar
));
2106 bar_extra_stop(void)
2110 bzero(bar_pipe
, sizeof bar_pipe
);
2113 kill(bar_pid
, SIGTERM
);
2116 strlcpy(bar_ext
, "", sizeof bar_ext
);
2121 bar_window_class(char *s
, size_t sz
, struct swm_region
*r
)
2123 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2125 if (r
->ws
->focus
->ch
.class_name
!= NULL
)
2126 strlcat(s
, r
->ws
->focus
->ch
.class_name
, sz
);
2130 bar_window_instance(char *s
, size_t sz
, struct swm_region
*r
)
2132 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2134 if (r
->ws
->focus
->ch
.instance_name
!= NULL
)
2135 strlcat(s
, r
->ws
->focus
->ch
.instance_name
, sz
);
2139 bar_window_class_instance(char *s
, size_t sz
, struct swm_region
*r
)
2141 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2144 bar_window_class(s
, sz
, r
);
2145 strlcat(s
, ":", sz
);
2146 bar_window_instance(s
, sz
, r
);
2150 bar_window_state(char *s
, size_t sz
, struct swm_region
*r
)
2152 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2154 if (MAXIMIZED(r
->ws
->focus
))
2155 strlcat(s
, "(m)", sz
);
2156 else if (ABOVE(r
->ws
->focus
))
2157 strlcat(s
, "(f)", sz
);
2161 bar_window_name(char *s
, size_t sz
, struct swm_region
*r
)
2165 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2168 title
= get_win_name(r
->ws
->focus
->id
);
2169 strlcat(s
, title
, sz
);
2174 get_urgent(struct ws_win
*win
)
2176 xcb_icccm_wm_hints_t hints
;
2177 xcb_get_property_cookie_t c
;
2178 bool urgent
= false;
2181 c
= xcb_icccm_get_wm_hints(conn
, win
->id
);
2182 if (xcb_icccm_get_wm_hints_reply(conn
, c
, &hints
, NULL
))
2183 urgent
= xcb_icccm_wm_hints_get_urgency(&hints
);
2190 bar_urgent(char *s
, size_t sz
)
2193 int i
, j
, num_screens
;
2194 bool urgent
[SWM_WS_MAX
];
2197 for (i
= 0; i
< workspace_limit
; i
++)
2200 num_screens
= get_screen_count();
2201 for (i
= 0; i
< num_screens
; i
++)
2202 for (j
= 0; j
< workspace_limit
; j
++)
2203 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2204 urgent
[j
] = get_urgent(win
);
2206 for (i
= 0; i
< workspace_limit
; i
++) {
2208 snprintf(b
, sizeof b
, "%d ", i
+ 1);
2210 } else if (!urgent_collapse
) {
2211 strlcat(s
, "- ", sz
);
2217 bar_workspace_name(char *s
, size_t sz
, struct swm_region
*r
)
2219 if (r
== NULL
|| r
->ws
== NULL
)
2221 if (r
->ws
->name
!= NULL
)
2222 strlcat(s
, r
->ws
->name
, sz
);
2225 /* build the default bar format according to the defined enabled options */
2227 bar_fmt(const char *fmtexp
, char *fmtnew
, struct swm_region
*r
, size_t sz
)
2231 /* if format provided, just copy the buffers */
2232 if (bar_format
!= NULL
) {
2233 strlcpy(fmtnew
, fmtexp
, sz
);
2237 /* reset the output buffer */
2240 strlcat(fmtnew
, "+N:+I ", sz
);
2242 strlcat(fmtnew
, "+S", sz
);
2243 strlcat(fmtnew
, " ", sz
);
2245 /* only show the workspace name if there's actually one */
2246 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->name
!= NULL
)
2247 strlcat(fmtnew
, "<+D>", sz
);
2249 /* If enabled, only show the iconic count if there are iconic wins. */
2250 if (iconic_enabled
&& r
!= NULL
&& r
->ws
!= NULL
)
2251 TAILQ_FOREACH(w
, &r
->ws
->winlist
, entry
)
2253 strlcat(fmtnew
, "{+M}", sz
);
2257 strlcat(fmtnew
, "+3<", sz
);
2259 if (clock_enabled
) {
2260 strlcat(fmtnew
, fmtexp
, sz
);
2261 strlcat(fmtnew
, "+4<", sz
);
2264 /* bar_urgent already adds the space before the last asterisk */
2266 strlcat(fmtnew
, "* +U*+4<", sz
);
2268 if (window_class_enabled
) {
2269 strlcat(fmtnew
, "+C", sz
);
2270 if (!window_instance_enabled
)
2271 strlcat(fmtnew
, "+4<", sz
);
2274 /* checks needed by the colon and floating strlcat(3) calls below */
2275 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->focus
!= NULL
) {
2276 if (window_instance_enabled
) {
2277 if (window_class_enabled
)
2278 strlcat(fmtnew
, ":", sz
);
2279 strlcat(fmtnew
, "+T+4<", sz
);
2281 if (window_name_enabled
) {
2282 if (ABOVE(r
->ws
->focus
) || MAXIMIZED(r
->ws
->focus
))
2283 strlcat(fmtnew
, "+F ", sz
);
2284 strlcat(fmtnew
, "+64W ", sz
);
2288 /* finally add the action script output and the version */
2289 strlcat(fmtnew
, "+4<+A+4<+V", sz
);
2293 bar_replace_pad(char *tmp
, int *limit
, size_t sz
)
2295 /* special case; no limit given, pad one space, instead */
2296 if (*limit
== (int)sz
- 1)
2298 snprintf(tmp
, sz
, "%*s", *limit
, " ");
2301 /* replaces the bar format character sequences (like in tmux(1)) */
2303 bar_replace_seq(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t *offrep
,
2308 char tmp
[SWM_BAR_MAX
];
2309 int limit
, size
, count
;
2312 /* reset strlcat(3) buffer */
2315 /* get number, if any */
2318 if (sscanf(fmt
, "%d%n", &limit
, &size
) != 1)
2319 limit
= sizeof tmp
- 1;
2320 if (limit
<= 0 || limit
>= (int)sizeof tmp
)
2321 limit
= sizeof tmp
- 1;
2323 /* there is nothing to replace (ie EOL) */
2330 bar_replace_pad(tmp
, &limit
, sizeof tmp
);
2333 snprintf(tmp
, sizeof tmp
, "%s", bar_ext
);
2336 bar_window_class(tmp
, sizeof tmp
, r
);
2339 bar_workspace_name(tmp
, sizeof tmp
, r
);
2342 bar_window_state(tmp
, sizeof tmp
, r
);
2345 snprintf(tmp
, sizeof tmp
, "%d", r
->ws
->idx
+ 1);
2349 TAILQ_FOREACH(w
, &r
->ws
->winlist
, entry
)
2353 snprintf(tmp
, sizeof tmp
, "%d", count
);
2356 snprintf(tmp
, sizeof tmp
, "%d", r
->s
->idx
+ 1);
2359 bar_window_class_instance(tmp
, sizeof tmp
, r
);
2362 snprintf(tmp
, sizeof tmp
, "%s", r
->ws
->stacker
);
2365 bar_window_instance(tmp
, sizeof tmp
, r
);
2368 bar_urgent(tmp
, sizeof tmp
);
2371 snprintf(tmp
, sizeof tmp
, "%s", bar_vertext
);
2374 bar_window_name(tmp
, sizeof tmp
, r
);
2377 /* unknown character sequence; copy as-is */
2378 snprintf(tmp
, sizeof tmp
, "+%c", *fmt
);
2384 if ((int)len
< limit
)
2386 while (limit
-- > 0) {
2387 if (*offrep
>= sz
- 1)
2389 fmtrep
[(*offrep
)++] = *ptr
++;
2397 bar_replace(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t sz
)
2402 while (*fmt
!= '\0') {
2404 /* skip ordinary characters */
2407 fmtrep
[off
++] = *fmt
++;
2411 /* character sequence found; replace it */
2412 fmt
= bar_replace_seq(fmt
, fmtrep
, r
, &off
, sz
);
2421 bar_fmt_expand(char *fmtexp
, size_t sz
)
2428 /* start by grabbing the current time and date */
2430 localtime_r(&tmt
, &tm
);
2432 /* figure out what to expand */
2433 if (bar_format
!= NULL
)
2435 else if (bar_format
== NULL
&& clock_enabled
)
2437 /* if nothing to expand bail out */
2443 /* copy as-is, just in case the format shouldn't be expanded below */
2444 strlcpy(fmtexp
, fmt
, sz
);
2445 /* finally pass the string through strftime(3) */
2446 #ifndef SWM_DENY_CLOCK_FORMAT
2447 if ((len
= strftime(fmtexp
, sz
, fmt
, &tm
)) == 0)
2448 warnx("format too long");
2453 /* Redraws the bar; need to follow with xcb_flush() or focus_flush(). */
2457 char fmtexp
[SWM_BAR_MAX
], fmtnew
[SWM_BAR_MAX
];
2458 char fmtrep
[SWM_BAR_MAX
];
2460 struct swm_region
*r
;
2462 /* expand the format by first passing it through strftime(3) */
2463 bar_fmt_expand(fmtexp
, sizeof fmtexp
);
2465 num_screens
= get_screen_count();
2466 for (i
= 0; i
< num_screens
; i
++) {
2467 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
2471 if (bar_enabled
&& r
->ws
->bar_enabled
)
2472 xcb_map_window(conn
, r
->bar
->id
);
2474 xcb_unmap_window(conn
, r
->bar
->id
);
2478 if (startup_exception
)
2479 snprintf(fmtrep
, sizeof fmtrep
, "total "
2480 "exceptions: %d, first exception: %s",
2484 bar_fmt(fmtexp
, fmtnew
, r
, sizeof fmtnew
);
2485 bar_replace(fmtnew
, fmtrep
, r
, sizeof fmtrep
);
2487 if (bar_font_legacy
)
2488 bar_print_legacy(r
, fmtrep
);
2490 bar_print(r
, fmtrep
);
2496 * Reads external script output; call when stdin is readable.
2497 * Returns 1 if bar_ext was updated; otherwise 0.
2500 bar_extra_update(void)
2503 char b
[SWM_BAR_MAX
];
2504 bool changed
= false;
2509 while (fgets(b
, sizeof(b
), stdin
) != NULL
) {
2512 if (b
[len
- 1] == '\n') {
2513 /* Remove newline. */
2516 /* "Clear" bar_ext. */
2519 /* Flush buffered output. */
2520 strlcpy(bar_ext
, bar_ext_buf
, sizeof(bar_ext
));
2521 bar_ext_buf
[0] = '\0';
2523 /* Append new output to bar. */
2524 strlcat(bar_ext
, b
, sizeof(bar_ext
));
2528 /* Buffer output. */
2529 strlcat(bar_ext_buf
, b
, sizeof(bar_ext_buf
));
2534 if (errno
!= EAGAIN
) {
2535 warn("bar_action failed");
2544 bar_toggle(struct swm_region
*r
, union arg
*args
)
2546 struct swm_region
*tmpr
;
2549 /* suppress unused warnings since vars are needed */
2553 DNPRINTF(SWM_D_BAR
, "bar_toggle\n");
2556 case SWM_ARG_ID_BAR_TOGGLE_WS
:
2557 /* Only change if master switch is enabled. */
2559 r
->ws
->bar_enabled
= !r
->ws
->bar_enabled
;
2561 bar_enabled
= r
->ws
->bar_enabled
= true;
2563 case SWM_ARG_ID_BAR_TOGGLE
:
2564 bar_enabled
= !bar_enabled
;
2568 /* update bars as necessary */
2569 num_screens
= get_screen_count();
2570 for (i
= 0; i
< num_screens
; i
++)
2571 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
2573 if (bar_enabled
&& tmpr
->ws
->bar_enabled
)
2574 xcb_map_window(conn
, tmpr
->bar
->id
);
2576 xcb_unmap_window(conn
, tmpr
->bar
->id
);
2581 /* must be after stack */
2588 bar_extra_setup(void)
2590 /* do this here because the conf file is in memory */
2591 if (!bar_extra
&& bar_argv
[0]) {
2592 /* launch external status app */
2594 if (pipe(bar_pipe
) == -1)
2595 err(1, "pipe error");
2596 socket_setnonblock(bar_pipe
[0]);
2597 socket_setnonblock(bar_pipe
[1]); /* XXX hmmm, really? */
2599 /* Set stdin to read from the pipe. */
2600 if (dup2(bar_pipe
[0], STDIN_FILENO
) == -1)
2603 /* Set stdout to write to the pipe. */
2604 if (dup2(bar_pipe
[1], STDOUT_FILENO
) == -1)
2607 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
2608 err(1, "could not disable SIGPIPE");
2609 switch (bar_pid
= fork()) {
2611 err(1, "cannot fork");
2615 execvp(bar_argv
[0], bar_argv
);
2616 err(1, "%s external app failed", bar_argv
[0]);
2618 default: /* parent */
2623 atexit(kill_bar_extra_atexit
);
2628 kill_bar_extra_atexit(void)
2631 kill(bar_pid
, SIGTERM
);
2639 while ((s
= index(s
, '-'))) {
2644 return (count
== 14);
2650 char *default_string
;
2651 char **missing_charsets
;
2652 int num_missing_charsets
= 0;
2656 XFreeFontSet(display
, bar_fs
);
2660 DNPRINTF(SWM_D_INIT
, "fontset_init: loading bar_fonts: %s\n", bar_fonts
);
2662 bar_fs
= XCreateFontSet(display
, bar_fonts
, &missing_charsets
,
2663 &num_missing_charsets
, &default_string
);
2665 if (num_missing_charsets
> 0) {
2666 warnx("Unable to load charset(s):");
2668 for (i
= 0; i
< num_missing_charsets
; ++i
)
2669 warnx("%s", missing_charsets
[i
]);
2671 XFreeStringList(missing_charsets
);
2673 if (strcmp(default_string
, ""))
2674 warnx("Glyphs from those sets will be replaced "
2675 "by '%s'.", default_string
);
2677 warnx("Glyphs from those sets won't be drawn.");
2681 errx(1, "Error creating font set structure.");
2683 bar_fs_extents
= XExtentsOfFontSet(bar_fs
);
2685 bar_height
= bar_fs_extents
->max_logical_extent
.height
+
2686 2 * bar_border_width
;
2693 xft_init(struct swm_region
*r
)
2695 char *font
, *d
, *search
;
2698 if (bar_font
== NULL
) {
2699 if ((d
= strdup(bar_fonts
)) == NULL
)
2700 errx(1, "insufficient memory.");
2702 while ((font
= strsep(&search
, ",")) != NULL
) {
2706 DNPRINTF(SWM_D_INIT
, "xft_init: try font %s\n", font
);
2709 bar_font
= XftFontOpenXlfd(display
, r
->s
->idx
,
2712 bar_font
= XftFontOpenName(display
, r
->s
->idx
,
2716 if (bar_font
== NULL
) {
2717 warnx("unable to load font %s", font
);
2720 DNPRINTF(SWM_D_INIT
, "successfully opened "
2728 if (bar_font
== NULL
)
2729 errx(1, "unable to open a font");
2731 PIXEL_TO_XRENDERCOLOR(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
, color
);
2733 if (!XftColorAllocValue(display
, DefaultVisual(display
, r
->s
->idx
),
2734 DefaultColormap(display
, r
->s
->idx
), &color
, &bar_font_color
))
2735 warn("Xft error: unable to allocate color.");
2737 PIXEL_TO_XRENDERCOLOR(r
->s
->c
[SWM_S_COLOR_BAR
].pixel
, color
);
2739 if (!XftColorAllocValue(display
, DefaultVisual(display
, r
->s
->idx
),
2740 DefaultColormap(display
, r
->s
->idx
), &color
, &search_font_color
))
2741 warn("Xft error: unable to allocate color.");
2743 bar_height
= bar_font
->height
+ 2 * bar_border_width
;
2750 bar_setup(struct swm_region
*r
)
2752 xcb_screen_t
*screen
;
2755 DNPRINTF(SWM_D_BAR
, "bar_setup: screen %d.\n",
2758 if ((screen
= get_screen(r
->s
->idx
)) == NULL
)
2759 errx(1, "ERROR: can't get screen %d.", r
->s
->idx
);
2764 if ((r
->bar
= calloc(1, sizeof(struct swm_bar
))) == NULL
)
2765 err(1, "bar_setup: calloc: failed to allocate memory.");
2767 if (bar_font_legacy
)
2773 Y(r
->bar
) = bar_at_bottom
? (Y(r
) + HEIGHT(r
) - bar_height
) : Y(r
);
2774 WIDTH(r
->bar
) = WIDTH(r
) - 2 * bar_border_width
;
2775 HEIGHT(r
->bar
) = bar_height
- 2 * bar_border_width
;
2777 /* Assume region is unfocused when we create the bar. */
2778 r
->bar
->id
= xcb_generate_id(conn
);
2779 wa
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
2780 wa
[1] = r
->s
->c
[SWM_S_COLOR_BAR_BORDER_UNFOCUS
].pixel
;
2781 wa
[2] = XCB_EVENT_MASK_EXPOSURE
| XCB_EVENT_MASK_POINTER_MOTION
|
2782 XCB_EVENT_MASK_POINTER_MOTION_HINT
;
2784 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, r
->bar
->id
, r
->s
->root
,
2785 X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
), HEIGHT(r
->bar
),
2786 bar_border_width
, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
2787 XCB_COPY_FROM_PARENT
, XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
2788 | XCB_CW_EVENT_MASK
, wa
);
2790 /* Stack bar window above region window to start. */
2792 wa
[1] = XCB_STACK_MODE_ABOVE
;
2794 xcb_configure_window(conn
, r
->bar
->id
, XCB_CONFIG_WINDOW_SIBLING
|
2795 XCB_CONFIG_WINDOW_STACK_MODE
, wa
);
2797 r
->bar
->buffer
= xcb_generate_id(conn
);
2798 xcb_create_pixmap(conn
, screen
->root_depth
, r
->bar
->buffer
, r
->bar
->id
,
2799 WIDTH(r
->bar
), HEIGHT(r
->bar
));
2802 xcb_randr_select_input(conn
, r
->bar
->id
,
2803 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE
);
2806 xcb_map_window(conn
, r
->bar
->id
);
2808 DNPRINTF(SWM_D_BAR
, "bar_setup: win %#x, (x,y) w x h: (%d,%d) "
2809 "%d x %d\n", WINID(r
->bar
), X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
),
2816 bar_cleanup(struct swm_region
*r
)
2820 xcb_destroy_window(conn
, r
->bar
->id
);
2821 xcb_free_pixmap(conn
, r
->bar
->buffer
);
2827 set_win_state(struct ws_win
*win
, uint8_t state
)
2829 uint16_t data
[2] = { state
, XCB_ATOM_NONE
};
2831 DNPRINTF(SWM_D_EVENT
, "set_win_state: win %#x, state: %u\n",
2837 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
, a_state
,
2838 a_state
, 32, 2, data
);
2842 get_win_state(xcb_window_t w
)
2844 xcb_get_property_reply_t
*r
;
2845 xcb_get_property_cookie_t c
;
2846 uint32_t result
= 0;
2848 c
= xcb_get_property(conn
, 0, w
, a_state
, a_state
, 0L, 2L);
2849 r
= xcb_get_property_reply(conn
, c
, NULL
);
2851 if (r
->type
== a_state
&& r
->format
== 32 && r
->length
== 2)
2852 result
= *((uint32_t *)xcb_get_property_value(r
));
2856 DNPRINTF(SWM_D_MISC
, "get_win_state property: win %#x state %u\n", w
,
2862 version(struct swm_region
*r
, union arg
*args
)
2864 /* suppress unused warnings since vars are needed */
2868 bar_version
= !bar_version
;
2870 snprintf(bar_vertext
, sizeof bar_vertext
,
2871 "Version: %s Build: %s", SPECTRWM_VERSION
, buildstr
);
2873 strlcpy(bar_vertext
, "", sizeof bar_vertext
);
2880 client_msg(struct ws_win
*win
, xcb_atom_t a
, xcb_timestamp_t t
)
2882 xcb_client_message_event_t ev
;
2890 name
= get_atom_name(a
);
2891 DNPRINTF(SWM_D_EVENT
, "client_msg: win %#x, atom: %s(%u), "
2893 win
->id
, name
, a
, t
);
2897 bzero(&ev
, sizeof ev
);
2898 ev
.response_type
= XCB_CLIENT_MESSAGE
;
2899 ev
.window
= win
->id
;
2902 ev
.data
.data32
[0] = a
;
2903 ev
.data
.data32
[1] = t
;
2905 xcb_send_event(conn
, 0, win
->id
,
2906 XCB_EVENT_MASK_NO_EVENT
, (const char *)&ev
);
2909 /* synthetic response to a ConfigureRequest when not making a change */
2911 config_win(struct ws_win
*win
, xcb_configure_request_event_t
*ev
)
2913 xcb_configure_notify_event_t ce
;
2918 /* send notification of unchanged state. */
2919 bzero(&ce
, sizeof(ce
));
2920 ce
.response_type
= XCB_CONFIGURE_NOTIFY
;
2923 ce
.width
= WIDTH(win
);
2924 ce
.height
= HEIGHT(win
);
2925 ce
.override_redirect
= 0;
2930 ce
.window
= win
->id
;
2931 ce
.border_width
= BORDER(win
);
2932 ce
.above_sibling
= XCB_WINDOW_NONE
;
2935 ce
.event
= ev
->window
;
2936 ce
.window
= ev
->window
;
2938 /* make response appear more WM_SIZE_HINTS-compliant */
2939 if (win
->sh
.flags
) {
2940 DNPRINTF(SWM_D_MISC
, "config_win: hints: win %#x,"
2941 " sh.flags: %u, min: %d x %d, max: %d x %d, inc: "
2942 "%d x %d\n", win
->id
, win
->sh
.flags
, SH_MIN_W(win
),
2943 SH_MIN_H(win
), SH_MAX_W(win
), SH_MAX_H(win
),
2944 SH_INC_W(win
), SH_INC_H(win
));
2949 /* the hint may be set... to 0! */
2950 if (SH_MIN_W(win
) > 0 && ce
.width
< SH_MIN_W(win
))
2951 ce
.width
= SH_MIN_W(win
);
2952 if (SH_MIN_H(win
) > 0 && ce
.height
< SH_MIN_H(win
))
2953 ce
.height
= SH_MIN_H(win
);
2958 /* may also be advertized as 0 */
2959 if (SH_MAX_W(win
) > 0 && ce
.width
> SH_MAX_W(win
))
2960 ce
.width
= SH_MAX_W(win
);
2961 if (SH_MAX_H(win
) > 0 && ce
.height
> SH_MAX_H(win
))
2962 ce
.height
= SH_MAX_H(win
);
2965 /* resize increment. */
2967 if (SH_INC_W(win
) > 1 && ce
.width
> SH_INC_W(win
))
2968 ce
.width
-= (ce
.width
- SH_MIN_W(win
)) %
2970 if (SH_INC_H(win
) > 1 && ce
.height
> SH_INC_H(win
))
2971 ce
.height
-= (ce
.height
- SH_MIN_H(win
)) %
2975 /* adjust x and y for requested border_width. */
2976 ce
.x
+= BORDER(win
) - ev
->border_width
;
2977 ce
.y
+= BORDER(win
) - ev
->border_width
;
2978 ce
.border_width
= ev
->border_width
;
2979 ce
.above_sibling
= ev
->sibling
;
2982 DNPRINTF(SWM_D_MISC
, "config_win: ewmh: %s, win %#x, (x,y) w x h: "
2983 "(%d,%d) %d x %d, border: %d\n", YESNO(ev
== NULL
), win
->id
, ce
.x
,
2984 ce
.y
, ce
.width
, ce
.height
, ce
.border_width
);
2986 xcb_send_event(conn
, 0, win
->id
, XCB_EVENT_MASK_STRUCTURE_NOTIFY
,
2991 count_win(struct workspace
*ws
, bool count_transient
)
2996 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
2997 if (!count_transient
&& FLOATING(win
))
3003 DNPRINTF(SWM_D_MISC
, "count_win: %d\n", count
);
3009 quit(struct swm_region
*r
, union arg
*args
)
3011 /* suppress unused warnings since vars are needed */
3015 DNPRINTF(SWM_D_MISC
, "quit\n");
3020 lower_window(struct ws_win
*win
)
3022 struct ws_win
*target
= NULL
;
3023 struct workspace
*ws
;
3030 DNPRINTF(SWM_D_EVENT
, "lower_window: win %#x\n", win
->id
);
3032 TAILQ_FOREACH(target
, &ws
->stack
, stack_entry
) {
3033 if (target
== win
|| ICONIC(target
))
3035 if (ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
3038 if (win
->transient
== target
->transient
)
3040 if (win
->transient
== target
->id
)
3043 if (FULLSCREEN(target
))
3045 if (FULLSCREEN(win
))
3047 if (MAXIMIZED(target
))
3051 if (ABOVE(target
) || TRANS(target
))
3053 if (ABOVE(win
) || TRANS(win
))
3057 /* Change stack position. */
3058 TAILQ_REMOVE(&ws
->stack
, win
, stack_entry
);
3060 TAILQ_INSERT_BEFORE(target
, win
, stack_entry
);
3062 TAILQ_INSERT_TAIL(&ws
->stack
, win
, stack_entry
);
3064 update_win_stacking(win
);
3067 if (swm_debug
& SWM_D_STACK
) {
3068 DPRINTF("=== stacking order (top down) === \n");
3069 TAILQ_FOREACH(target
, &ws
->stack
, stack_entry
) {
3070 DPRINTF("win %#x, fs: %s, maximized: %s, above: %s, "
3071 "iconic: %s\n", target
->id
, YESNO(FULLSCREEN(target
)),
3072 YESNO(MAXIMIZED(target
)), YESNO(ABOVE(target
)),
3073 YESNO(ICONIC(target
)));
3077 DNPRINTF(SWM_D_EVENT
, "lower_window: done\n");
3081 raise_window(struct ws_win
*win
)
3083 struct ws_win
*target
= NULL
;
3084 struct workspace
*ws
;
3090 DNPRINTF(SWM_D_EVENT
, "raise_window: win %#x\n", win
->id
);
3092 TAILQ_FOREACH(target
, &ws
->stack
, stack_entry
) {
3093 if (target
== win
|| ICONIC(target
))
3095 if (ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
3097 if (TRANS(win
) && (win
->transient
== target
->transient
||
3098 win
->transient
== target
->id
))
3100 if (FULLSCREEN(win
))
3102 if (FULLSCREEN(target
))
3106 if (MAXIMIZED(target
))
3108 if (ABOVE(win
) || TRANS(win
) ||
3109 (win
->ws
->focus
== win
&& ws
->always_raise
))
3111 if (!ABOVE(target
) && !TRANS(target
))
3115 TAILQ_REMOVE(&ws
->stack
, win
, stack_entry
);
3117 TAILQ_INSERT_BEFORE(target
, win
, stack_entry
);
3119 TAILQ_INSERT_TAIL(&ws
->stack
, win
, stack_entry
);
3121 update_win_stacking(win
);
3124 if (swm_debug
& SWM_D_STACK
) {
3125 DPRINTF("=== stacking order (top down) === \n");
3126 TAILQ_FOREACH(target
, &ws
->stack
, stack_entry
) {
3127 DPRINTF("win %#x, fs: %s, maximized: %s, above: %s, "
3128 "iconic: %s\n", target
->id
, YESNO(FULLSCREEN(target
)),
3129 YESNO(MAXIMIZED(target
)), YESNO(ABOVE(target
)),
3130 YESNO(ICONIC(target
)));
3134 DNPRINTF(SWM_D_EVENT
, "raise_window: done\n");
3138 update_win_stacking(struct ws_win
*win
)
3140 struct ws_win
*sibling
;
3141 struct swm_region
*r
;
3144 if (win
== NULL
|| (r
= win
->ws
->r
) == NULL
)
3147 sibling
= TAILQ_NEXT(win
, stack_entry
);
3148 if (sibling
!= NULL
&& (FLOATING(win
) == FLOATING(sibling
) ||
3149 (win
->ws
->always_raise
&& win
->ws
->focus
== win
)))
3150 val
[0] = sibling
->id
;
3151 else if (FLOATING(win
) || (win
->ws
->always_raise
&&
3152 win
->ws
->focus
== win
))
3153 val
[0] = r
->bar
->id
;
3157 DNPRINTF(SWM_D_EVENT
, "update_win_stacking: %#x, sibling %#x\n",
3160 val
[1] = XCB_STACK_MODE_ABOVE
;
3162 xcb_configure_window(conn
, win
->id
, XCB_CONFIG_WINDOW_SIBLING
|
3163 XCB_CONFIG_WINDOW_STACK_MODE
, val
);
3167 map_window(struct ws_win
*win
)
3172 DNPRINTF(SWM_D_EVENT
, "map_window: win %#x, mapped: %s\n",
3173 win
->id
, YESNO(win
->mapped
));
3178 xcb_map_window(conn
, win
->id
);
3179 set_win_state(win
, XCB_ICCCM_WM_STATE_NORMAL
);
3184 unmap_window(struct ws_win
*win
)
3189 DNPRINTF(SWM_D_EVENT
, "unmap_window: win %#x, mapped: %s\n", win
->id
,
3190 YESNO(win
->mapped
));
3195 xcb_unmap_window(conn
, win
->id
);
3196 set_win_state(win
, XCB_ICCCM_WM_STATE_ICONIC
);
3197 win
->mapped
= false;
3204 int i
, j
, num_screens
;
3206 num_screens
= get_screen_count();
3207 for (i
= 0; i
< num_screens
; i
++)
3208 for (j
= 0; j
< workspace_limit
; j
++)
3209 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
3214 fake_keypress(struct ws_win
*win
, xcb_keysym_t keysym
, uint16_t modifiers
)
3216 xcb_key_press_event_t event
;
3217 xcb_keycode_t
*keycode
;
3222 keycode
= xcb_key_symbols_get_keycode(syms
, keysym
);
3224 DNPRINTF(SWM_D_MISC
, "fake_keypress: win %#x, keycode %u\n",
3227 bzero(&event
, sizeof(event
));
3228 event
.event
= win
->id
;
3229 event
.root
= win
->s
->root
;
3230 event
.child
= XCB_WINDOW_NONE
;
3231 event
.time
= XCB_CURRENT_TIME
;
3232 event
.event_x
= X(win
);
3233 event
.event_y
= Y(win
);
3236 event
.same_screen
= 1;
3237 event
.detail
= *keycode
;
3238 event
.state
= modifiers
;
3240 event
.response_type
= XCB_KEY_PRESS
;
3241 xcb_send_event(conn
, 1, win
->id
,
3242 XCB_EVENT_MASK_KEY_PRESS
, (const char *)&event
);
3244 event
.response_type
= XCB_KEY_RELEASE
;
3245 xcb_send_event(conn
, 1, win
->id
,
3246 XCB_EVENT_MASK_KEY_RELEASE
, (const char *)&event
);
3252 restart(struct swm_region
*r
, union arg
*args
)
3254 /* suppress unused warning since var is needed */
3258 DNPRINTF(SWM_D_MISC
, "restart: %s\n", start_argv
[0]);
3262 execvp(start_argv
[0], start_argv
);
3263 warn("execvp failed");
3268 get_pointer_win(xcb_window_t root
)
3270 struct ws_win
*win
= NULL
;
3271 xcb_query_pointer_reply_t
*r
;
3273 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: root: %#x.\n", root
);
3275 r
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, root
), NULL
);
3277 win
= find_window(r
->child
);
3279 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: %#x.\n",
3282 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: none.\n");
3291 center_pointer(struct swm_region
*r
)
3295 if (!warp_pointer
|| r
== NULL
)
3300 DNPRINTF(SWM_D_EVENT
, "center_pointer: win %#x.\n", WINID(win
));
3302 if (win
&& win
->mapped
)
3303 xcb_warp_pointer(conn
, XCB_NONE
, win
->id
, 0, 0, 0, 0,
3304 WIDTH(win
) / 2, HEIGHT(win
) / 2);
3306 xcb_warp_pointer(conn
, XCB_NONE
, r
->id
, 0, 0, 0, 0,
3307 WIDTH(r
) / 2, HEIGHT(r
) / 2);
3311 root_to_region(xcb_window_t root
, int check
)
3314 struct swm_region
*r
= NULL
;
3316 xcb_query_pointer_reply_t
*qpr
;
3317 xcb_get_input_focus_reply_t
*gifr
;
3319 DNPRINTF(SWM_D_MISC
, "root_to_region: win %#x\n", root
);
3321 num_screens
= get_screen_count();
3322 for (i
= 0; i
< num_screens
; i
++)
3323 if (screens
[i
].root
== root
)
3326 if (check
& SWM_CK_REGION
)
3327 r
= screens
[i
].r_focus
;
3329 if (r
== NULL
&& check
& SWM_CK_FOCUS
) {
3330 /* Try to find an actively focused window */
3331 gifr
= xcb_get_input_focus_reply(conn
,
3332 xcb_get_input_focus(conn
), NULL
);
3334 cfw
= find_window(gifr
->focus
);
3335 if (cfw
&& cfw
->ws
->r
)
3342 if (r
== NULL
&& check
& SWM_CK_POINTER
) {
3343 /* No region with an active focus; try to use pointer. */
3344 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
,
3345 screens
[i
].root
), NULL
);
3347 DNPRINTF(SWM_D_MISC
, "root_to_region: pointer: "
3348 "(%d,%d)\n", qpr
->root_x
, qpr
->root_y
);
3349 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3350 if (X(r
) <= qpr
->root_x
&&
3351 qpr
->root_x
< MAX_X(r
) &&
3352 Y(r
) <= qpr
->root_y
&&
3353 qpr
->root_y
< MAX_Y(r
))
3360 if (r
== NULL
&& check
& SWM_CK_FALLBACK
)
3361 r
= TAILQ_FIRST(&screens
[i
].rl
);
3363 DNPRINTF(SWM_D_MISC
, "root_to_region: idx: %d\n", get_region_index(r
));
3369 find_unmanaged_window(xcb_window_t id
)
3372 int i
, j
, num_screens
;
3374 num_screens
= get_screen_count();
3375 for (i
= 0; i
< num_screens
; i
++)
3376 for (j
= 0; j
< workspace_limit
; j
++)
3377 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].unmanagedlist
,
3385 find_window(xcb_window_t id
)
3388 int i
, j
, num_screens
;
3389 xcb_query_tree_reply_t
*r
;
3391 num_screens
= get_screen_count();
3392 for (i
= 0; i
< num_screens
; i
++)
3393 for (j
= 0; j
< workspace_limit
; j
++)
3394 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
3398 r
= xcb_query_tree_reply(conn
, xcb_query_tree(conn
, id
), NULL
);
3402 /* if we were looking for the parent return that window instead */
3403 if (r
->parent
== 0 || r
->root
== r
->parent
) {
3408 /* look for parent */
3409 for (i
= 0; i
< num_screens
; i
++)
3410 for (j
= 0; j
< workspace_limit
; j
++)
3411 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
3412 if (r
->parent
== win
->id
) {
3422 spawn(int ws_idx
, union arg
*args
, bool close_fd
)
3427 DNPRINTF(SWM_D_MISC
, "spawn: %s\n", args
->argv
[0]);
3429 close(xcb_get_file_descriptor(conn
));
3431 setenv("LD_PRELOAD", SWM_LIB
, 1);
3433 if (asprintf(&ret
, "%d", ws_idx
) == -1) {
3434 warn("spawn: asprintf SWM_WS");
3437 setenv("_SWM_WS", ret
, 1);
3441 if (asprintf(&ret
, "%d", getpid()) == -1) {
3442 warn("spawn: asprintf _SWM_PID");
3445 setenv("_SWM_PID", ret
, 1);
3449 if (setsid() == -1) {
3450 warn("spawn: setsid");
3456 * close stdin and stdout to prevent interaction between apps
3457 * and the baraction script
3458 * leave stderr open to record errors
3460 if ((fd
= open(_PATH_DEVNULL
, O_RDWR
, 0)) == -1) {
3461 warn("spawn: open");
3464 dup2(fd
, STDIN_FILENO
);
3465 dup2(fd
, STDOUT_FILENO
);
3470 execvp(args
->argv
[0], args
->argv
);
3472 warn("spawn: execvp");
3477 kill_refs(struct ws_win
*win
)
3479 int i
, x
, num_screens
;
3480 struct swm_region
*r
;
3481 struct workspace
*ws
;
3486 num_screens
= get_screen_count();
3487 for (i
= 0; i
< num_screens
; i
++)
3488 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3489 for (x
= 0; x
< workspace_limit
; x
++) {
3491 if (win
== ws
->focus
)
3493 if (win
== ws
->focus_prev
)
3494 ws
->focus_prev
= NULL
;
3499 validate_win(struct ws_win
*testwin
)
3502 struct workspace
*ws
;
3503 struct swm_region
*r
;
3504 int i
, x
, num_screens
;
3506 if (testwin
== NULL
)
3509 num_screens
= get_screen_count();
3510 for (i
= 0; i
< num_screens
; i
++)
3511 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3512 for (x
= 0; x
< workspace_limit
; x
++) {
3514 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
3522 validate_ws(struct workspace
*testws
)
3524 struct swm_region
*r
;
3525 struct workspace
*ws
;
3526 int i
, x
, num_screens
;
3528 /* validate all ws */
3529 num_screens
= get_screen_count();
3530 for (i
= 0; i
< num_screens
; i
++)
3531 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3532 for (x
= 0; x
< workspace_limit
; x
++) {
3541 unfocus_win(struct ws_win
*win
)
3543 xcb_window_t none
= XCB_WINDOW_NONE
;
3545 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: win %#x\n", WINID(win
));
3550 if (win
->ws
== NULL
) {
3551 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: NULL ws.\n");
3555 if (validate_ws(win
->ws
)) {
3556 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: invalid ws.\n");
3560 if (win
->ws
->r
== NULL
) {
3561 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: NULL region.\n");
3565 if (validate_win(win
)) {
3566 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: invalid win.\n");
3571 if (win
->ws
->focus
== win
) {
3572 win
->ws
->focus
= NULL
;
3573 win
->ws
->focus_prev
= win
;
3576 if (validate_win(win
->ws
->focus
)) {
3577 kill_refs(win
->ws
->focus
);
3578 win
->ws
->focus
= NULL
;
3581 if (validate_win(win
->ws
->focus_prev
)) {
3582 kill_refs(win
->ws
->focus_prev
);
3583 win
->ws
->focus_prev
= NULL
;
3586 update_window_color(win
);
3588 /* Raise window to "top unfocused position." */
3589 if (win
->ws
->always_raise
)
3592 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
3593 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1, &none
);
3595 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: done.\n");
3599 focus_win(struct ws_win
*win
)
3601 struct ws_win
*cfw
= NULL
, *parent
= NULL
, *w
, *tmpw
;
3602 struct workspace
*ws
;
3603 xcb_get_input_focus_reply_t
*gifr
;
3605 DNPRINTF(SWM_D_FOCUS
, "focus_win: win %#x\n", WINID(win
));
3607 if (win
== NULL
|| win
->ws
== NULL
|| !win
->mapped
)
3612 if (validate_ws(ws
))
3615 if (validate_win(win
)) {
3620 gifr
= xcb_get_input_focus_reply(conn
, xcb_get_input_focus(conn
), NULL
);
3622 cfw
= find_window(gifr
->focus
);
3623 if (cfw
!= NULL
&& cfw
!= win
) {
3624 if (cfw
->ws
!= ws
&& cfw
->ws
->r
!= NULL
) {
3625 /* Change border to unfocused color. */
3626 xcb_change_window_attributes(conn
, cfw
->id
,
3627 XCB_CW_BORDER_PIXEL
,
3628 &cfw
->s
->c
[(MAXIMIZED(cfw
) ?
3629 SWM_S_COLOR_UNFOCUS_MAXIMIZED
:
3630 SWM_S_COLOR_UNFOCUS
)].pixel
);
3638 if (ws
->focus
!= win
) {
3639 if (ws
->focus
&& ws
->focus
!= cfw
)
3640 unfocus_win(ws
->focus
);
3644 /* If this window directs focus to a child window, then clear. */
3645 if (win
->focus_child
)
3646 win
->focus_child
= NULL
;
3648 /* If transient, adjust parent's focus child for focus_magic. */
3650 parent
= find_window(win
->transient
);
3651 if (parent
&& parent
->focus_child
!= win
)
3652 parent
->focus_child
= win
;
3655 if (cfw
!= win
&& ws
->r
!= NULL
) {
3656 /* Set input focus if no input hint, or indicated by hint. */
3657 if (!(win
->hints
.flags
& XCB_ICCCM_WM_HINT_INPUT
) ||
3658 (win
->hints
.flags
& XCB_ICCCM_WM_HINT_INPUT
&&
3660 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
,
3661 win
->id
, last_event_time
);
3663 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
,
3664 ws
->r
->id
, XCB_CURRENT_TIME
);
3666 /* Tell app it can adjust focus to a specific window. */
3667 if (win
->take_focus
) {
3668 /* java is special; always tell parent */
3669 if (TRANS(win
) && win
->java
)
3670 client_msg(parent
, a_takefocus
,
3673 client_msg(win
, a_takefocus
, last_event_time
);
3676 if (ws
->cur_layout
->flags
& SWM_L_MAPONFOCUS
||
3678 /* If a parent exists, map it first. */
3680 raise_window(parent
);
3683 /* Map siblings next. */
3684 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
,
3686 if (w
!= win
&& !ICONIC(w
) &&
3687 w
->transient
== parent
->id
) {
3693 /* Map focused window. */
3697 /* Stack any children of focus window. */
3698 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
, tmpw
)
3699 if (w
->transient
== win
->id
&& !ICONIC(w
)) {
3703 } else if (tile_gap
< 0 && !ABOVE(win
)) {
3705 * Windows overlap in the layout.
3706 * Raise focused win above all tiled wins.
3714 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
3715 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1,
3720 /* Update window border even if workspace is hidden. */
3721 update_window_color(win
);
3726 DNPRINTF(SWM_D_FOCUS
, "focus_win: done.\n");
3729 /* If a child window should have focus instead, return it. */
3731 get_focus_magic(struct ws_win
*win
)
3733 struct ws_win
*parent
= NULL
;
3734 struct ws_win
*child
= NULL
;
3736 DNPRINTF(SWM_D_FOCUS
, "get_focus_magic: win %#x\n", WINID(win
));
3741 parent
= find_window(win
->transient
);
3743 /* If parent prefers focus elsewhere, then try to do so. */
3744 if (parent
&& (child
= parent
->focus_child
)) {
3745 if (validate_win(child
) == 0 && child
->mapped
)
3748 parent
->focus_child
= NULL
;
3752 /* If this window prefers focus elsewhere, then try to do so. */
3753 if ((child
= win
->focus_child
)) {
3754 if (validate_win(child
) == 0 && child
->mapped
)
3757 win
->focus_child
= NULL
;
3764 event_drain(uint8_t rt
)
3766 xcb_generic_event_t
*evt
;
3768 /* ensure all pending requests have been processed before filtering. */
3770 while ((evt
= xcb_poll_for_event(conn
))) {
3771 if (XCB_EVENT_RESPONSE_TYPE(evt
) != rt
)
3779 set_region(struct swm_region
*r
)
3781 struct swm_region
*rf
;
3788 /* Unfocus old region bar. */
3793 xcb_change_window_attributes(conn
, rf
->bar
->id
,
3794 XCB_CW_BORDER_PIXEL
,
3795 &r
->s
->c
[SWM_S_COLOR_BAR_BORDER_UNFOCUS
].pixel
);
3798 if (rf
!= NULL
&& rf
!= r
&& (X(rf
) != X(r
) || Y(rf
) != Y(r
) ||
3799 WIDTH(rf
) != WIDTH(r
) || HEIGHT(rf
) != HEIGHT(r
))) {
3800 /* Set _NET_DESKTOP_GEOMETRY. */
3802 vals
[1] = HEIGHT(r
);
3803 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, r
->s
->root
,
3804 ewmh
[_NET_DESKTOP_GEOMETRY
].atom
, XCB_ATOM_CARDINAL
, 32, 2,
3808 /* Set region bar border to focus_color. */
3809 xcb_change_window_attributes(conn
, r
->bar
->id
,
3810 XCB_CW_BORDER_PIXEL
, &r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].pixel
);
3814 ewmh_update_current_desktop();
3818 focus_region(struct swm_region
*r
)
3821 struct swm_region
*old_r
;
3826 old_r
= r
->s
->r_focus
;
3829 nfw
= get_region_focus(r
);
3833 /* New region is empty; need to manually unfocus win. */
3835 unfocus_win(old_r
->ws
->focus
);
3837 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
, r
->id
,
3840 /* Clear bar since empty. */
3846 switchws(struct swm_region
*r
, union arg
*args
)
3848 struct swm_region
*this_r
, *other_r
;
3850 struct workspace
*new_ws
, *old_ws
;
3851 xcb_window_t none
= XCB_WINDOW_NONE
;
3852 int wsid
= args
->id
;
3853 bool unmap_old
= false;
3858 if (wsid
>= workspace_limit
)
3862 old_ws
= this_r
->ws
;
3863 new_ws
= &this_r
->s
->ws
[wsid
];
3865 DNPRINTF(SWM_D_WS
, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
3866 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), old_ws
->idx
, wsid
);
3868 if (new_ws
== NULL
|| old_ws
== NULL
)
3870 if (new_ws
== old_ws
)
3873 if ((win
= old_ws
->focus
) != NULL
) {
3874 update_window_color(win
);
3876 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
3877 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1,
3881 other_r
= new_ws
->r
;
3882 if (other_r
== NULL
) {
3883 /* the other workspace is hidden, hide this one */
3887 /* the other ws is visible in another region, exchange them */
3888 other_r
->ws_prior
= new_ws
;
3889 other_r
->ws
= old_ws
;
3890 old_ws
->r
= other_r
;
3892 this_r
->ws_prior
= old_ws
;
3893 this_r
->ws
= new_ws
;
3896 /* Set focus_pending before stacking, if needed. */
3897 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& (!new_ws
->focus_pending
||
3898 validate_win(new_ws
->focus_pending
))) {
3899 new_ws
->focus_pending
= get_region_focus(new_ws
->r
);
3900 new_ws
->focus
= new_ws
->focus_prev
;
3901 new_ws
->focus_prev
= NULL
;
3904 new_ws
->state
= SWM_WS_STATE_MAPPING
;
3907 /* unmap old windows */
3909 TAILQ_FOREACH(win
, &old_ws
->winlist
, entry
)
3911 old_ws
->state
= SWM_WS_STATE_HIDDEN
;
3914 /* if workspaces were swapped, then don't wait to set focus */
3915 if (old_ws
->r
&& focus_mode
!= SWM_FOCUS_FOLLOW
) {
3916 if (new_ws
->focus_pending
) {
3917 focus_win(new_ws
->focus_pending
);
3918 new_ws
->focus_pending
= NULL
;
3922 /* Clear bar and set focus on region input win if new ws is empty. */
3923 if (new_ws
->focus_pending
== NULL
&& new_ws
->focus
== NULL
) {
3924 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
, r
->id
,
3929 ewmh_update_current_desktop();
3933 new_ws
->state
= SWM_WS_STATE_MAPPED
;
3935 DNPRINTF(SWM_D_WS
, "switchws: done.\n");
3939 cyclews(struct swm_region
*r
, union arg
*args
)
3942 struct swm_screen
*s
= r
->s
;
3943 bool cycle_all
= false, mv
= false;
3945 DNPRINTF(SWM_D_WS
, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3946 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
3952 case SWM_ARG_ID_CYCLEWS_MOVE_UP
:
3955 case SWM_ARG_ID_CYCLEWS_UP_ALL
:
3958 case SWM_ARG_ID_CYCLEWS_UP
:
3959 a
.id
= (a
.id
< workspace_limit
- 1) ? a
.id
+ 1 : 0;
3961 case SWM_ARG_ID_CYCLEWS_MOVE_DOWN
:
3964 case SWM_ARG_ID_CYCLEWS_DOWN_ALL
:
3967 case SWM_ARG_ID_CYCLEWS_DOWN
:
3968 a
.id
= (a
.id
> 0) ? a
.id
- 1 : workspace_limit
- 1;
3975 (!cycle_empty
&& TAILQ_EMPTY(&s
->ws
[a
.id
].winlist
)))
3977 if (!cycle_visible
&& s
->ws
[a
.id
].r
!= NULL
)
3984 } while (a
.id
!= r
->ws
->idx
);
3986 DNPRINTF(SWM_D_FOCUS
, "cyclews: done\n");
3990 priorws(struct swm_region
*r
, union arg
*args
)
3996 DNPRINTF(SWM_D_WS
, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3997 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
3999 if (r
->ws_prior
== NULL
)
4002 a
.id
= r
->ws_prior
->idx
;
4004 DNPRINTF(SWM_D_FOCUS
, "priorws: done\n");
4008 focusrg(struct swm_region
*r
, union arg
*args
)
4010 int ridx
= args
->id
, i
, num_screens
;
4011 struct swm_region
*rr
= NULL
;
4013 num_screens
= get_screen_count();
4014 /* do nothing if we don't have more than one screen */
4015 if (!(num_screens
> 1 || outputs
> 1))
4018 DNPRINTF(SWM_D_FOCUS
, "focusrg: id: %d\n", ridx
);
4020 rr
= TAILQ_FIRST(&r
->s
->rl
);
4021 for (i
= 0; i
< ridx
&& rr
!= NULL
; ++i
)
4022 rr
= TAILQ_NEXT(rr
, entry
);
4030 DNPRINTF(SWM_D_FOCUS
, "focusrg: done\n");
4034 cyclerg(struct swm_region
*r
, union arg
*args
)
4036 struct swm_region
*rr
= NULL
;
4039 num_screens
= get_screen_count();
4040 /* do nothing if we don't have more than one screen */
4041 if (!(num_screens
> 1 || outputs
> 1))
4045 DNPRINTF(SWM_D_FOCUS
, "cyclerg: id: %d, region: %d\n", args
->id
, i
);
4048 case SWM_ARG_ID_CYCLERG_UP
:
4049 rr
= TAILQ_NEXT(r
, entry
);
4051 rr
= TAILQ_FIRST(&screens
[i
].rl
);
4053 case SWM_ARG_ID_CYCLERG_DOWN
:
4054 rr
= TAILQ_PREV(r
, swm_region_list
, entry
);
4056 rr
= TAILQ_LAST(&screens
[i
].rl
, swm_region_list
);
4067 DNPRINTF(SWM_D_FOCUS
, "cyclerg: done\n");
4070 /* Sorts transients after parent. */
4072 sort_windows(struct ws_win_list
*wl
)
4074 struct ws_win
*win
, *parent
, *nxt
;
4079 for (win
= TAILQ_FIRST(wl
); win
!= TAILQ_END(wl
); win
= nxt
) {
4080 nxt
= TAILQ_NEXT(win
, entry
);
4082 parent
= find_window(win
->transient
);
4083 if (parent
== NULL
) {
4084 warnx("not possible bug");
4087 TAILQ_REMOVE(wl
, win
, entry
);
4088 TAILQ_INSERT_AFTER(wl
, parent
, win
, entry
);
4094 swapwin(struct swm_region
*r
, union arg
*args
)
4096 struct ws_win
*target
, *source
;
4097 struct ws_win
*cur_focus
;
4098 struct ws_win_list
*wl
;
4100 DNPRINTF(SWM_D_WS
, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
4101 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
4103 cur_focus
= r
->ws
->focus
;
4104 if (cur_focus
== NULL
|| FULLSCREEN(cur_focus
))
4107 /* Adjust stacking in floating layer. */
4108 if (ABOVE(cur_focus
)) {
4110 case SWM_ARG_ID_SWAPPREV
:
4111 target
= TAILQ_PREV(cur_focus
, ws_win_stack
,
4113 if (target
!= NULL
&& FLOATING(target
)) {
4114 TAILQ_REMOVE(&cur_focus
->ws
->stack
, cur_focus
,
4116 TAILQ_INSERT_BEFORE(target
, cur_focus
,
4118 update_win_stacking(cur_focus
);
4122 case SWM_ARG_ID_SWAPNEXT
:
4123 target
= TAILQ_NEXT(cur_focus
, stack_entry
);
4124 if (target
!= NULL
&& FLOATING(target
)) {
4125 TAILQ_REMOVE(&cur_focus
->ws
->stack
, cur_focus
,
4127 TAILQ_INSERT_AFTER(&cur_focus
->ws
->stack
,
4128 target
, cur_focus
, stack_entry
);
4129 update_win_stacking(cur_focus
);
4137 if (r
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
4140 clear_maximized(r
->ws
);
4143 wl
= &source
->ws
->winlist
;
4146 case SWM_ARG_ID_SWAPPREV
:
4148 source
= find_window(source
->transient
);
4149 target
= TAILQ_PREV(source
, ws_win_list
, entry
);
4150 if (target
&& target
->transient
)
4151 target
= find_window(target
->transient
);
4152 TAILQ_REMOVE(wl
, source
, entry
);
4154 TAILQ_INSERT_TAIL(wl
, source
, entry
);
4156 TAILQ_INSERT_BEFORE(target
, source
, entry
);
4158 case SWM_ARG_ID_SWAPNEXT
:
4159 target
= TAILQ_NEXT(source
, entry
);
4160 /* move the parent and let the sort handle the move */
4162 source
= find_window(source
->transient
);
4163 TAILQ_REMOVE(wl
, source
, entry
);
4165 TAILQ_INSERT_HEAD(wl
, source
, entry
);
4167 TAILQ_INSERT_AFTER(wl
, target
, source
, entry
);
4169 case SWM_ARG_ID_SWAPMAIN
:
4170 target
= TAILQ_FIRST(wl
);
4171 if (target
== source
) {
4172 if (source
->ws
->focus_prev
!= NULL
&&
4173 source
->ws
->focus_prev
!= target
)
4174 source
= source
->ws
->focus_prev
;
4178 if (target
== NULL
|| source
== NULL
)
4180 source
->ws
->focus_prev
= target
;
4181 TAILQ_REMOVE(wl
, target
, entry
);
4182 TAILQ_INSERT_BEFORE(source
, target
, entry
);
4183 TAILQ_REMOVE(wl
, source
, entry
);
4184 TAILQ_INSERT_HEAD(wl
, source
, entry
);
4186 case SWM_ARG_ID_MOVELAST
:
4187 TAILQ_REMOVE(wl
, source
, entry
);
4188 TAILQ_INSERT_TAIL(wl
, source
, entry
);
4191 DNPRINTF(SWM_D_MOVE
, "swapwin: invalid id: %d\n", args
->id
);
4196 ewmh_update_client_list();
4202 DNPRINTF(SWM_D_MOVE
, "swapwin: done\n");
4206 get_focus_prev(struct ws_win
*win
)
4208 struct ws_win
*winfocus
= NULL
;
4209 struct ws_win
*cur_focus
= NULL
;
4210 struct ws_win_list
*wl
= NULL
;
4211 struct workspace
*ws
= NULL
;
4213 if (!(win
&& win
->ws
))
4218 cur_focus
= ws
->focus
;
4220 DNPRINTF(SWM_D_FOCUS
, "get_focus_prev: win %#x, cur_focus: %#x, "
4221 "focus_prev: %#x\n", WINID(win
), WINID(cur_focus
),
4222 WINID(ws
->focus_prev
));
4224 /* pickle, just focus on whatever */
4225 if (cur_focus
== NULL
) {
4226 /* use prev_focus if valid */
4227 if (ws
->focus_prev
&& find_window(ws
->focus_prev
->id
))
4228 winfocus
= ws
->focus_prev
;
4232 /* if transient focus on parent */
4233 if (TRANS(cur_focus
)) {
4234 winfocus
= find_window(cur_focus
->transient
);
4238 /* if in max_stack try harder */
4239 if ((win
->quirks
& SWM_Q_FOCUSPREV
) ||
4240 (ws
->cur_layout
->flags
& SWM_L_FOCUSPREV
)) {
4241 if (cur_focus
!= ws
->focus_prev
)
4242 winfocus
= ws
->focus_prev
;
4244 winfocus
= TAILQ_PREV(win
, ws_win_list
, entry
);
4249 DNPRINTF(SWM_D_FOCUS
, "get_focus_prev: focus_close: %d\n", focus_close
);
4251 if (winfocus
== NULL
|| winfocus
== win
) {
4252 switch (focus_close
) {
4253 case SWM_STACK_BOTTOM
:
4254 TAILQ_FOREACH(winfocus
, wl
, entry
)
4255 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4259 TAILQ_FOREACH_REVERSE(winfocus
, wl
, ws_win_list
, entry
)
4260 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4263 case SWM_STACK_ABOVE
:
4264 winfocus
= TAILQ_NEXT(cur_focus
, entry
);
4265 while (winfocus
&& ICONIC(winfocus
))
4266 winfocus
= TAILQ_NEXT(winfocus
, entry
);
4268 if (winfocus
== NULL
) {
4269 if (focus_close_wrap
) {
4270 TAILQ_FOREACH(winfocus
, wl
, entry
)
4271 if (!ICONIC(winfocus
) &&
4272 winfocus
!= cur_focus
)
4275 TAILQ_FOREACH_REVERSE(winfocus
, wl
,
4277 if (!ICONIC(winfocus
) &&
4278 winfocus
!= cur_focus
)
4283 case SWM_STACK_BELOW
:
4284 winfocus
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
4285 while (winfocus
&& ICONIC(winfocus
))
4286 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
,
4289 if (winfocus
== NULL
) {
4290 if (focus_close_wrap
) {
4291 TAILQ_FOREACH_REVERSE(winfocus
, wl
,
4293 if (!ICONIC(winfocus
) &&
4294 winfocus
!= cur_focus
)
4297 TAILQ_FOREACH(winfocus
, wl
, entry
)
4298 if (!ICONIC(winfocus
) &&
4299 winfocus
!= cur_focus
)
4307 if (winfocus
== NULL
||
4308 (winfocus
&& (ICONIC(winfocus
) || winfocus
== cur_focus
))) {
4309 if (focus_default
== SWM_STACK_TOP
) {
4310 TAILQ_FOREACH_REVERSE(winfocus
, wl
, ws_win_list
, entry
)
4311 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4314 TAILQ_FOREACH(winfocus
, wl
, entry
)
4315 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4322 return get_focus_magic(winfocus
);
4326 get_region_focus(struct swm_region
*r
)
4328 struct ws_win
*winfocus
= NULL
;
4333 if (r
->ws
->focus
&& !ICONIC(r
->ws
->focus
))
4334 winfocus
= r
->ws
->focus
;
4335 else if (r
->ws
->focus_prev
&& !ICONIC(r
->ws
->focus_prev
))
4336 winfocus
= r
->ws
->focus_prev
;
4338 TAILQ_FOREACH(winfocus
, &r
->ws
->winlist
, entry
)
4339 if (!ICONIC(winfocus
))
4342 return get_focus_magic(winfocus
);
4346 focus(struct swm_region
*r
, union arg
*args
)
4348 struct ws_win
*head
, *cur_focus
= NULL
, *winfocus
= NULL
;
4349 struct ws_win_list
*wl
= NULL
;
4350 struct workspace
*ws
= NULL
;
4357 cur_focus
= r
->ws
->focus
;
4361 DNPRINTF(SWM_D_FOCUS
, "focus: id: %d, cur_focus: %#x\n", args
->id
,
4364 /* Make sure an uniconified window has focus, if one exists. */
4365 if (cur_focus
== NULL
) {
4366 cur_focus
= TAILQ_FIRST(wl
);
4367 while (cur_focus
!= NULL
&& ICONIC(cur_focus
))
4368 cur_focus
= TAILQ_NEXT(cur_focus
, entry
);
4370 DNPRINTF(SWM_D_FOCUS
, "focus: new cur_focus: %#x\n",
4375 case SWM_ARG_ID_FOCUSPREV
:
4376 if (cur_focus
== NULL
)
4379 winfocus
= cur_focus
;
4381 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
, entry
);
4382 if (winfocus
== NULL
)
4383 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
4384 if (winfocus
== cur_focus
)
4386 } while (winfocus
&& (ICONIC(winfocus
) ||
4387 winfocus
->id
== cur_focus
->transient
||
4388 (cur_focus
->transient
!= XCB_WINDOW_NONE
&&
4389 winfocus
->transient
== cur_focus
->transient
)));
4391 case SWM_ARG_ID_FOCUSNEXT
:
4392 if (cur_focus
== NULL
)
4395 winfocus
= cur_focus
;
4397 winfocus
= TAILQ_NEXT(winfocus
, entry
);
4398 if (winfocus
== NULL
)
4399 winfocus
= TAILQ_FIRST(wl
);
4400 if (winfocus
== cur_focus
)
4402 } while (winfocus
&& (ICONIC(winfocus
) ||
4403 winfocus
->id
== cur_focus
->transient
||
4404 (cur_focus
->transient
!= XCB_WINDOW_NONE
&&
4405 winfocus
->transient
== cur_focus
->transient
)));
4407 case SWM_ARG_ID_FOCUSMAIN
:
4408 if (cur_focus
== NULL
)
4411 winfocus
= TAILQ_FIRST(wl
);
4412 if (winfocus
== cur_focus
)
4413 winfocus
= cur_focus
->ws
->focus_prev
;
4415 case SWM_ARG_ID_FOCUSURGENT
:
4416 /* Search forward for the next urgent window. */
4420 for (i
= 0; i
<= workspace_limit
; ++i
) {
4422 head
= TAILQ_FIRST(&r
->s
->ws
[(ws
->idx
+ i
) %
4423 workspace_limit
].winlist
);
4426 if (head
== cur_focus
) {
4428 winfocus
= cur_focus
;
4431 } else if (get_urgent(head
)) {
4436 head
= TAILQ_NEXT(head
, entry
);
4443 /* Switch ws if new focus is on a different ws. */
4444 if (winfocus
&& winfocus
->ws
!= ws
) {
4445 a
.id
= winfocus
->ws
->idx
;
4453 if (clear_maximized(ws
) > 0)
4456 focus_win(get_focus_magic(winfocus
));
4461 DNPRINTF(SWM_D_FOCUS
, "focus: done\n");
4465 cycle_layout(struct swm_region
*r
, union arg
*args
)
4467 struct workspace
*ws
= r
->ws
;
4469 /* suppress unused warning since var is needed */
4472 DNPRINTF(SWM_D_EVENT
, "cycle_layout: workspace: %d\n", ws
->idx
);
4475 if (ws
->cur_layout
->l_stack
== NULL
)
4476 ws
->cur_layout
= &layouts
[0];
4478 clear_maximized(ws
);
4483 focus_win(get_region_focus(r
));
4490 stack_config(struct swm_region
*r
, union arg
*args
)
4492 struct workspace
*ws
= r
->ws
;
4494 DNPRINTF(SWM_D_STACK
, "stack_config: id: %d workspace: %d\n",
4497 if (clear_maximized(ws
) > 0)
4500 if (ws
->cur_layout
->l_config
!= NULL
)
4501 ws
->cur_layout
->l_config(ws
, args
->id
);
4503 if (args
->id
!= SWM_ARG_ID_STACKINIT
)
4513 struct swm_geometry g
;
4514 struct swm_region
*r
, *r_prev
= NULL
;
4521 DNPRINTF(SWM_D_STACK
, "stack: begin\n");
4523 num_screens
= get_screen_count();
4524 for (i
= 0; i
< num_screens
; i
++) {
4528 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
4529 /* Adjust stack area for region bar and padding. */
4531 g
.x
+= region_padding
;
4532 g
.y
+= region_padding
;
4533 g
.w
-= 2 * border_width
+ 2 * region_padding
;
4534 g
.h
-= 2 * border_width
+ 2 * region_padding
;
4535 if (bar_enabled
&& r
->ws
->bar_enabled
) {
4541 DNPRINTF(SWM_D_STACK
, "stack: workspace: %d (screen: "
4542 "%d, region: %d), (x,y) WxH: (%d,%d) %d x %d\n",
4543 r
->ws
->idx
, i
, j
++, g
.x
, g
.y
, g
.w
, g
.h
);
4546 /* Stack bar/input relative to prev. region. */
4547 val
[1] = XCB_STACK_MODE_ABOVE
;
4549 val
[0] = r_prev
->id
;
4550 DNPRINTF(SWM_D_STACK
, "stack: region input %#x "
4551 "relative to %#x.\n", r
->id
, val
[0]);
4552 xcb_configure_window(conn
, r
->id
,
4553 XCB_CONFIG_WINDOW_SIBLING
|
4554 XCB_CONFIG_WINDOW_STACK_MODE
, val
);
4556 val
[0] = r_prev
->bar
->id
;
4557 DNPRINTF(SWM_D_STACK
, "stack: region bar %#x "
4558 "relative to %#x.\n", r
->bar
->id
, val
[0]);
4559 xcb_configure_window(conn
, r
->bar
->id
,
4560 XCB_CONFIG_WINDOW_SIBLING
|
4561 XCB_CONFIG_WINDOW_STACK_MODE
, val
);
4564 r
->ws
->cur_layout
->l_stack(r
->ws
, &g
);
4565 r
->ws
->cur_layout
->l_string(r
->ws
);
4566 /* save r so we can track region changes */
4574 DNPRINTF(SWM_D_STACK
, "stack: end\n");
4578 store_float_geom(struct ws_win
*win
)
4580 if (win
== NULL
|| win
->ws
->r
== NULL
)
4583 /* retain window geom and region geom */
4584 win
->g_float
= win
->g
;
4585 win
->g_float
.x
-= X(win
->ws
->r
);
4586 win
->g_float
.y
-= Y(win
->ws
->r
);
4587 win
->g_floatvalid
= true;
4588 DNPRINTF(SWM_D_MISC
, "store_float_geom: win %#x, g: (%d,%d)"
4589 " %d x %d, g_float: (%d,%d) %d x %d\n", win
->id
, X(win
), Y(win
),
4590 WIDTH(win
), HEIGHT(win
), win
->g_float
.x
, win
->g_float
.y
,
4591 win
->g_float
.w
, win
->g_float
.h
);
4595 load_float_geom(struct ws_win
*win
)
4597 if (win
== NULL
|| win
->ws
->r
== NULL
)
4600 if (win
->g_floatvalid
) {
4601 win
->g
= win
->g_float
;
4602 X(win
) += X(win
->ws
->r
);
4603 Y(win
) += Y(win
->ws
->r
);
4604 DNPRINTF(SWM_D_MISC
, "load_float_geom: win %#x, g: (%d,%d)"
4605 "%d x %d\n", win
->id
, X(win
), Y(win
), WIDTH(win
),
4608 DNPRINTF(SWM_D_MISC
, "load_float_geom: win %#x, g_float "
4609 "is not set.\n", win
->id
);
4614 update_floater(struct ws_win
*win
)
4616 struct workspace
*ws
;
4617 struct swm_region
*r
;
4624 if ((r
= ws
->r
) == NULL
)
4627 DNPRINTF(SWM_D_MISC
, "update_floater: win %#x\n", win
->id
);
4629 win
->bordered
= true;
4631 if (FULLSCREEN(win
)) {
4632 /* _NET_WM_FULLSCREEN: fullscreen without border. */
4633 if (!win
->g_floatvalid
)
4634 store_float_geom(win
);
4637 win
->bordered
= false;
4638 } else if (MAXIMIZED(win
)) {
4639 /* Maximize: like a single stacked window. */
4640 if (!win
->g_floatvalid
)
4641 store_float_geom(win
);
4645 if (bar_enabled
&& ws
->bar_enabled
) {
4647 Y(win
) += bar_height
;
4648 HEIGHT(win
) -= bar_height
;
4649 } else if (disable_border
) {
4650 win
->bordered
= false;
4653 if (win
->bordered
) {
4654 HEIGHT(win
) -= 2 * border_width
;
4655 WIDTH(win
) -= 2 * border_width
;
4658 /* Normal floating window. */
4659 /* Update geometry if new region. */
4661 load_float_geom(win
);
4663 if ((win
->quirks
& SWM_Q_FULLSCREEN
) &&
4664 WIDTH(win
) >= WIDTH(r
) && HEIGHT(win
) >= HEIGHT(r
)) {
4665 /* Remove border for FULLSCREEN quirk. */
4666 win
->bordered
= false;
4667 } else if (!MANUAL(win
)) {
4668 if (TRANS(win
) && (win
->quirks
& SWM_Q_TRANSSZ
)) {
4669 /* Adjust size on TRANSSZ quirk. */
4670 WIDTH(win
) = (double)WIDTH(r
) * dialog_ratio
;
4671 HEIGHT(win
) = (double)HEIGHT(r
) * dialog_ratio
;
4674 if (!(win
->quirks
& SWM_Q_ANYWHERE
)) {
4676 * Floaters and transients are auto-centred
4677 * unless manually moved, resized or ANYWHERE
4680 X(win
) = X(r
) + (WIDTH(r
) - WIDTH(win
)) / 2 -
4682 Y(win
) = Y(r
) + (HEIGHT(r
) - HEIGHT(win
)) / 2 -
4684 store_float_geom(win
);
4689 /* Ensure at least 1 pixel of the window is in the region. */
4690 region_containment(win
, r
, SWM_CW_ALLSIDES
);
4696 * Send keystrokes to terminal to decrease/increase the font size as the
4697 * window size changes.
4700 adjust_font(struct ws_win
*win
)
4702 if (!(win
->quirks
& SWM_Q_XTERM_FONTADJ
) ||
4703 ABOVE(win
) || TRANS(win
))
4706 if (win
->sh
.width_inc
&& win
->last_inc
!= win
->sh
.width_inc
&&
4707 WIDTH(win
) / win
->sh
.width_inc
< term_width
&&
4708 win
->font_steps
< SWM_MAX_FONT_STEPS
) {
4709 win
->font_size_boundary
[win
->font_steps
] =
4710 (win
->sh
.width_inc
* term_width
) + win
->sh
.base_width
;
4713 win
->last_inc
= win
->sh
.width_inc
;
4714 fake_keypress(win
, XK_KP_Subtract
, XCB_MOD_MASK_SHIFT
);
4715 } else if (win
->font_steps
&& win
->last_inc
!= win
->sh
.width_inc
&&
4716 WIDTH(win
) > win
->font_size_boundary
[win
->font_steps
- 1]) {
4719 win
->last_inc
= win
->sh
.width_inc
;
4720 fake_keypress(win
, XK_KP_Add
, XCB_MOD_MASK_SHIFT
);
4724 #define SWAPXY(g) do { \
4726 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
4727 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
4730 stack_master(struct workspace
*ws
, struct swm_geometry
*g
, int rot
, bool flip
)
4732 struct swm_geometry win_g
, r_g
= *g
;
4734 int i
, j
, s
, stacks
;
4735 int w_inc
= 1, h_inc
, w_base
= 1, h_base
;
4736 int hrh
, extra
= 0, h_slice
, last_h
= 0;
4737 int split
, colno
, winno
, mwin
, msize
, mscale
;
4738 int remain
, missing
, v_slice
;
4739 bool bordered
= true, reconfigure
= false;
4741 DNPRINTF(SWM_D_STACK
, "stack_master: workspace: %d, rot: %s, "
4742 "flip: %s\n", ws
->idx
, YESNO(rot
), YESNO(flip
));
4744 /* Prepare tiling variables, if needed. */
4745 if ((winno
= count_win(ws
, false)) > 0) {
4746 /* Find first tiled window. */
4747 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
4748 if (!FLOATING(win
) && !ICONIC(win
))
4751 /* Take into account size hints of first tiled window. */
4753 w_inc
= win
->sh
.width_inc
;
4754 w_base
= win
->sh
.base_width
;
4755 mwin
= ws
->l_state
.horizontal_mwin
;
4756 mscale
= ws
->l_state
.horizontal_msize
;
4757 stacks
= ws
->l_state
.horizontal_stacks
;
4760 w_inc
= win
->sh
.height_inc
;
4761 w_base
= win
->sh
.base_height
;
4762 mwin
= ws
->l_state
.vertical_mwin
;
4763 mscale
= ws
->l_state
.vertical_msize
;
4764 stacks
= ws
->l_state
.vertical_stacks
;
4768 if (stacks
> winno
- mwin
)
4769 stacks
= winno
- mwin
;
4773 h_slice
= r_g
.h
/ SWM_H_SLICE
;
4774 if (mwin
&& winno
> mwin
) {
4775 v_slice
= r_g
.w
/ SWM_V_SLICE
;
4779 win_g
.w
= v_slice
* mscale
;
4781 if (w_inc
> 1 && w_inc
< v_slice
) {
4782 /* Adjust for requested size increment. */
4783 remain
= (win_g
.w
- w_base
) % w_inc
;
4789 win_g
.x
+= r_g
.w
- msize
;
4792 colno
= split
= winno
/ stacks
;
4793 win_g
.w
= ((r_g
.w
- (stacks
* 2 * border_width
) +
4794 2 * border_width
) / stacks
);
4796 hrh
= r_g
.h
/ colno
;
4797 extra
= r_g
.h
- (colno
* hrh
);
4798 win_g
.h
= hrh
- 2 * border_width
;
4799 i
= j
= 0, s
= stacks
;
4802 /* Update window geometry. */
4803 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
4807 if (FLOATING(win
)) {
4808 update_floater(win
);
4813 if (split
&& i
== split
) {
4814 colno
= (winno
- mwin
) / stacks
;
4815 if (s
<= (winno
- mwin
) % stacks
)
4818 hrh
= r_g
.h
/ colno
;
4819 extra
= r_g
.h
- (colno
* hrh
);
4822 win_g
.x
+= win_g
.w
+ 2 * border_width
+
4825 win_g
.w
= (r_g
.w
- msize
-
4826 (stacks
* (2 * border_width
+ tile_gap
))) / stacks
;
4828 win_g
.w
+= (r_g
.w
- msize
-
4829 (stacks
* (2 * border_width
+ tile_gap
))) %
4833 win_g
.x
-= win_g
.w
+ 2 * border_width
+
4839 win_g
.h
= hrh
- 2 * border_width
- tile_gap
;
4842 h_inc
= win
->sh
.width_inc
;
4843 h_base
= win
->sh
.base_width
;
4845 h_inc
= win
->sh
.height_inc
;
4846 h_base
= win
->sh
.base_height
;
4849 if (j
== colno
- 1) {
4850 win_g
.h
= hrh
+ extra
;
4851 } else if (h_inc
> 1 && h_inc
< h_slice
) {
4852 /* adjust for window's requested size increment */
4853 remain
= (win_g
.h
- h_base
) % h_inc
;
4854 missing
= h_inc
- remain
;
4856 if (missing
<= extra
|| j
== 0) {
4868 win_g
.y
+= last_h
+ 2 * border_width
+ tile_gap
;
4870 if (disable_border
&& !(bar_enabled
&& ws
->bar_enabled
) &&
4873 win_g
.w
+= 2 * border_width
;
4874 win_g
.h
+= 2 * border_width
;
4880 if (X(win
) != win_g
.y
|| Y(win
) != win_g
.x
||
4881 WIDTH(win
) != win_g
.h
|| HEIGHT(win
) != win_g
.w
) {
4885 WIDTH(win
) = win_g
.h
;
4886 HEIGHT(win
) = win_g
.w
;
4889 if (X(win
) != win_g
.x
|| Y(win
) != win_g
.y
||
4890 WIDTH(win
) != win_g
.w
|| HEIGHT(win
) != win_g
.h
) {
4894 WIDTH(win
) = win_g
.w
;
4895 HEIGHT(win
) = win_g
.h
;
4899 if (bordered
!= win
->bordered
) {
4901 win
->bordered
= bordered
;
4914 /* Stack all windows from bottom up. */
4915 TAILQ_FOREACH_REVERSE(win
, &ws
->stack
, ws_win_stack
, stack_entry
)
4917 update_win_stacking(win
);
4919 /* Map all windows from top down. */
4920 TAILQ_FOREACH(win
, &ws
->stack
, stack_entry
)
4924 DNPRINTF(SWM_D_STACK
, "stack_master: done\n");
4928 vertical_config(struct workspace
*ws
, int id
)
4930 DNPRINTF(SWM_D_STACK
, "vertical_config: id: %d, workspace: %d\n",
4934 case SWM_ARG_ID_STACKRESET
:
4935 case SWM_ARG_ID_STACKINIT
:
4936 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ 2;
4937 ws
->l_state
.vertical_mwin
= 1;
4938 ws
->l_state
.vertical_stacks
= 1;
4940 case SWM_ARG_ID_MASTERSHRINK
:
4941 if (ws
->l_state
.vertical_msize
> 1)
4942 ws
->l_state
.vertical_msize
--;
4944 case SWM_ARG_ID_MASTERGROW
:
4945 if (ws
->l_state
.vertical_msize
< SWM_V_SLICE
- 1)
4946 ws
->l_state
.vertical_msize
++;
4948 case SWM_ARG_ID_MASTERADD
:
4949 ws
->l_state
.vertical_mwin
++;
4951 case SWM_ARG_ID_MASTERDEL
:
4952 if (ws
->l_state
.vertical_mwin
> 0)
4953 ws
->l_state
.vertical_mwin
--;
4955 case SWM_ARG_ID_STACKINC
:
4956 ws
->l_state
.vertical_stacks
++;
4958 case SWM_ARG_ID_STACKDEC
:
4959 if (ws
->l_state
.vertical_stacks
> 1)
4960 ws
->l_state
.vertical_stacks
--;
4962 case SWM_ARG_ID_FLIPLAYOUT
:
4963 ws
->l_state
.vertical_flip
= !ws
->l_state
.vertical_flip
;
4971 vertical_stack(struct workspace
*ws
, struct swm_geometry
*g
)
4973 DNPRINTF(SWM_D_STACK
, "vertical_stack: workspace: %d\n", ws
->idx
);
4975 stack_master(ws
, g
, 0, ws
->l_state
.vertical_flip
);
4979 horizontal_config(struct workspace
*ws
, int id
)
4981 DNPRINTF(SWM_D_STACK
, "horizontal_config: workspace: %d\n", ws
->idx
);
4984 case SWM_ARG_ID_STACKRESET
:
4985 case SWM_ARG_ID_STACKINIT
:
4986 ws
->l_state
.horizontal_mwin
= 1;
4987 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ 2;
4988 ws
->l_state
.horizontal_stacks
= 1;
4990 case SWM_ARG_ID_MASTERSHRINK
:
4991 if (ws
->l_state
.horizontal_msize
> 1)
4992 ws
->l_state
.horizontal_msize
--;
4994 case SWM_ARG_ID_MASTERGROW
:
4995 if (ws
->l_state
.horizontal_msize
< SWM_H_SLICE
- 1)
4996 ws
->l_state
.horizontal_msize
++;
4998 case SWM_ARG_ID_MASTERADD
:
4999 ws
->l_state
.horizontal_mwin
++;
5001 case SWM_ARG_ID_MASTERDEL
:
5002 if (ws
->l_state
.horizontal_mwin
> 0)
5003 ws
->l_state
.horizontal_mwin
--;
5005 case SWM_ARG_ID_STACKINC
:
5006 ws
->l_state
.horizontal_stacks
++;
5008 case SWM_ARG_ID_STACKDEC
:
5009 if (ws
->l_state
.horizontal_stacks
> 1)
5010 ws
->l_state
.horizontal_stacks
--;
5012 case SWM_ARG_ID_FLIPLAYOUT
:
5013 ws
->l_state
.horizontal_flip
= !ws
->l_state
.horizontal_flip
;
5021 horizontal_stack(struct workspace
*ws
, struct swm_geometry
*g
)
5023 DNPRINTF(SWM_D_STACK
, "horizontal_stack: workspace: %d\n", ws
->idx
);
5025 stack_master(ws
, g
, 1, ws
->l_state
.horizontal_flip
);
5028 /* fullscreen view */
5030 max_stack(struct workspace
*ws
, struct swm_geometry
*g
)
5032 struct swm_geometry gg
= *g
;
5033 struct ws_win
*w
, *win
= NULL
, *parent
= NULL
, *tmpw
;
5036 DNPRINTF(SWM_D_STACK
, "max_stack: workspace: %d\n", ws
->idx
);
5041 winno
= count_win(ws
, false);
5042 if (winno
== 0 && count_win(ws
, true) == 0)
5045 /* Figure out which top level window should be visible. */
5046 if (ws
->focus_pending
)
5047 win
= ws
->focus_pending
;
5050 else if (ws
->focus_prev
)
5051 win
= ws
->focus_prev
;
5053 win
= TAILQ_FIRST(&ws
->winlist
);
5055 DNPRINTF(SWM_D_STACK
, "max_stack: focus_pending: %#x, focus: %#x, "
5056 "focus_prev: %#x, first: %#x, win: %#x\n", WINID(ws
->focus_pending
),
5057 WINID(ws
->focus
), WINID(ws
->focus_prev
),
5058 WINID(TAILQ_FIRST(&ws
->winlist
)), win
->id
);
5060 /* Update window geometry. */
5061 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
5070 /* Set maximized flag for all maxed windows. */
5071 if (!MAXIMIZED(w
)) {
5072 /* Preserve floating geometry. */
5074 store_float_geom(w
);
5076 ewmh_apply_flags(w
, w
->ewmh_flags
| EWMH_F_MAXIMIZED
);
5077 ewmh_update_wm_state(w
);
5080 /* Only reconfigure if necessary. */
5081 if (X(w
) != gg
.x
|| Y(w
) != gg
.y
|| WIDTH(w
) != gg
.w
||
5082 HEIGHT(w
) != gg
.h
) {
5085 if (disable_border
&& !(bar_enabled
&& ws
->bar_enabled
)) {
5086 w
->bordered
= false;
5087 WIDTH(w
) += 2 * border_width
;
5088 HEIGHT(w
) += 2 * border_width
;
5097 /* If transient, stack parent and its children. */
5098 if (TRANS(win
) && (parent
= find_window(win
->transient
))) {
5099 raise_window(parent
);
5101 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
, tmpw
)
5102 if (w
->transient
== parent
->id
)
5106 /* Make sure focus window is on top. */
5109 /* Stack any children of focus window. */
5110 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
, tmpw
)
5111 if (w
->transient
== win
->id
)
5114 /* Map all windows. */
5115 TAILQ_FOREACH(w
, &ws
->stack
, stack_entry
)
5121 send_to_rg(struct swm_region
*r
, union arg
*args
)
5123 int ridx
= args
->id
, i
, num_screens
;
5124 struct swm_region
*rr
= NULL
;
5127 num_screens
= get_screen_count();
5128 /* do nothing if we don't have more than one screen */
5129 if (!(num_screens
> 1 || outputs
> 1))
5132 DNPRINTF(SWM_D_FOCUS
, "send_to_rg: id: %d\n", ridx
);
5134 rr
= TAILQ_FIRST(&r
->s
->rl
);
5135 for (i
= 0; i
< ridx
&& rr
!= NULL
; ++i
)
5136 rr
= TAILQ_NEXT(rr
, entry
);
5147 region_under(struct swm_screen
*s
, int x
, int y
)
5149 struct swm_region
*r
= NULL
;
5151 TAILQ_FOREACH(r
, &s
->rl
, entry
) {
5152 DNPRINTF(SWM_D_MISC
, "region_under: ws: %d, region g: (%d,%d) "
5153 "%d x %d, coords: (%d,%d)\n", r
->ws
->idx
, X(r
), Y(r
),
5154 WIDTH(r
), HEIGHT(r
), x
, y
);
5155 if (X(r
) <= x
&& x
< MAX_X(r
))
5156 if (Y(r
) <= y
&& y
< MAX_Y(r
))
5163 /* Transfer focused window to target workspace and focus. */
5165 send_to_ws(struct swm_region
*r
, union arg
*args
)
5167 int wsid
= args
->id
;
5168 struct ws_win
*win
= NULL
;
5170 if (r
&& r
->ws
&& r
->ws
->focus
)
5175 DNPRINTF(SWM_D_MOVE
, "send_to_ws: win %#x, ws %d\n", win
->id
, wsid
);
5177 if (wsid
< 0 || wsid
>= workspace_limit
)
5180 if (win
->ws
->idx
== wsid
)
5183 win_to_ws(win
, wsid
, true);
5185 /* Set new focus on target ws. */
5186 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
5187 win
->ws
->focus_prev
= win
->ws
->focus
;
5188 win
->ws
->focus
= win
;
5189 win
->ws
->focus_pending
= NULL
;
5191 if (win
->ws
->focus_prev
)
5192 update_window_color(win
->ws
->focus_prev
);
5195 DNPRINTF(SWM_D_STACK
, "send_to_ws: focus_pending: %#x, focus: %#x, "
5196 "focus_prev: %#x, first: %#x, win: %#x\n",
5197 WINID(r
->ws
->focus_pending
), WINID(r
->ws
->focus
),
5198 WINID(r
->ws
->focus_prev
), WINID(TAILQ_FIRST(&r
->ws
->winlist
)),
5201 ewmh_apply_flags(win
, win
->ewmh_flags
& ~EWMH_F_MAXIMIZED
);
5202 ewmh_update_wm_state(win
);
5204 /* Restack and set new focus on current ws. */
5206 load_float_geom(win
);
5210 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
5211 if (r
->ws
->focus
!= NULL
) {
5212 focus_win(r
->ws
->focus
);
5214 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
, r
->id
,
5225 win_to_ws(struct ws_win
*win
, int wsid
, bool unfocus
)
5227 struct ws_win
*parent
;
5228 struct workspace
*ws
, *nws
, *pws
;
5230 if (wsid
< 0 || wsid
>= workspace_limit
)
5233 if (win
->ws
->idx
== wsid
)
5237 nws
= &win
->s
->ws
[wsid
];
5239 DNPRINTF(SWM_D_MOVE
, "win_to_ws: win %#x, ws %d -> %d\n", win
->id
,
5242 /* Cleanup focus on source ws. */
5243 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
5244 (ws
->focus
== win
|| ws
->focus_pending
== win
))
5245 ws
->focus_pending
= get_focus_prev(win
);
5247 /* Move the parent if this is a transient window. */
5249 parent
= find_window(win
->transient
);
5252 /* Set new focus in parent's ws if needed. */
5253 if (pws
->focus
== parent
) {
5254 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
5255 pws
->focus_pending
=
5256 get_focus_prev(parent
);
5258 unfocus_win(parent
);
5260 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
5261 pws
->focus
= pws
->focus_pending
;
5262 pws
->focus_pending
= NULL
;
5266 /* Don't unmap parent if new ws is visible */
5268 unmap_window(parent
);
5271 TAILQ_REMOVE(&ws
->winlist
, parent
, entry
);
5272 TAILQ_REMOVE(&ws
->stack
, parent
, stack_entry
);
5273 TAILQ_INSERT_TAIL(&nws
->winlist
, parent
, entry
);
5274 TAILQ_INSERT_TAIL(&nws
->stack
, parent
, stack_entry
);
5277 DNPRINTF(SWM_D_PROP
, "win_to_ws: set property: "
5278 "_NET_WM_DESKTOP: %d\n", wsid
);
5279 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
5280 parent
->id
, ewmh
[_NET_WM_DESKTOP
].atom
,
5281 XCB_ATOM_CARDINAL
, 32, 1, &wsid
);
5288 if (ws
->focus_prev
== win
)
5289 ws
->focus_prev
= NULL
;
5291 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& ws
->focus_pending
!= NULL
) {
5292 ws
->focus
= ws
->focus_pending
;
5293 ws
->focus_pending
= NULL
;
5296 /* Don't unmap if new ws is visible */
5301 TAILQ_REMOVE(&ws
->winlist
, win
, entry
);
5302 TAILQ_REMOVE(&ws
->stack
, win
, stack_entry
);
5303 TAILQ_INSERT_TAIL(&nws
->winlist
, win
, entry
);
5304 TAILQ_INSERT_TAIL(&nws
->stack
, win
, stack_entry
);
5307 /* Update the window's workspace property: _NET_WM_DESKTOP */
5308 DNPRINTF(SWM_D_PROP
, "win_to_ws: set property: "
5309 "_NET_WM_DESKTOP: %d\n", wsid
);
5310 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
5311 ewmh
[_NET_WM_DESKTOP
].atom
, XCB_ATOM_CARDINAL
, 32, 1, &wsid
);
5313 ewmh_update_client_list();
5315 DNPRINTF(SWM_D_MOVE
, "win_to_ws: done.\n");
5319 pressbutton(struct swm_region
*r
, union arg
*args
)
5321 /* suppress unused warning since var is needed */
5324 xcb_test_fake_input(conn
, XCB_BUTTON_PRESS
, args
->id
,
5325 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
5326 xcb_test_fake_input(conn
, XCB_BUTTON_RELEASE
, args
->id
,
5327 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
5331 raise_toggle(struct swm_region
*r
, union arg
*args
)
5333 /* Suppress warning. */
5336 if (r
== NULL
|| r
->ws
== NULL
)
5339 if (r
->ws
->focus
&& MAXIMIZED(r
->ws
->focus
))
5342 r
->ws
->always_raise
= !r
->ws
->always_raise
;
5344 /* Update focused win stacking order based on new always_raise value. */
5345 raise_window(r
->ws
->focus
);
5351 iconify(struct swm_region
*r
, union arg
*args
)
5355 /* Suppress warning. */
5358 if ((w
= r
->ws
->focus
) == NULL
)
5361 ewmh_apply_flags(w
, w
->ewmh_flags
| EWMH_F_HIDDEN
);
5362 ewmh_update_wm_state(w
);
5370 get_win_name(xcb_window_t win
)
5373 xcb_get_property_cookie_t c
;
5374 xcb_get_property_reply_t
*r
;
5376 /* First try _NET_WM_NAME for UTF-8. */
5377 c
= xcb_get_property(conn
, 0, win
, ewmh
[_NET_WM_NAME
].atom
,
5378 XCB_GET_PROPERTY_TYPE_ANY
, 0, UINT_MAX
);
5379 r
= xcb_get_property_reply(conn
, c
, NULL
);
5380 if (r
&& r
->type
== XCB_NONE
) {
5382 /* Use WM_NAME instead; no UTF-8. */
5383 c
= xcb_get_property(conn
, 0, win
, XCB_ATOM_WM_NAME
,
5384 XCB_GET_PROPERTY_TYPE_ANY
, 0, UINT_MAX
);
5385 r
= xcb_get_property_reply(conn
, c
, NULL
);
5388 if (r
&& r
->type
!= XCB_NONE
&& r
->length
> 0)
5389 name
= strndup(xcb_get_property_value(r
),
5390 xcb_get_property_value_length(r
));
5395 err(1, "get_win_name: failed to allocate memory.");
5403 uniconify(struct swm_region
*r
, union arg
*args
)
5410 DNPRINTF(SWM_D_MISC
, "uniconify\n");
5412 if (r
== NULL
|| r
->ws
== NULL
)
5415 /* make sure we have anything to uniconify */
5416 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
5417 if (win
->ws
== NULL
)
5418 continue; /* should never happen */
5424 DNPRINTF(SWM_D_MISC
, "uniconify: count: %d\n", count
);
5430 search_resp_action
= SWM_SEARCH_UNICONIFY
;
5432 spawn_select(r
, args
, "search", &searchpid
);
5434 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
5437 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
5438 if (win
->ws
== NULL
)
5439 continue; /* should never happen */
5443 name
= get_win_name(win
->id
);
5444 fprintf(lfile
, "%s.%u\n", name
, win
->id
);
5452 name_workspace(struct swm_region
*r
, union arg
*args
)
5456 DNPRINTF(SWM_D_MISC
, "name_workspace\n");
5462 search_resp_action
= SWM_SEARCH_NAME_WORKSPACE
;
5464 spawn_select(r
, args
, "name_workspace", &searchpid
);
5466 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
5469 fprintf(lfile
, "%s", "");
5474 search_workspace(struct swm_region
*r
, union arg
*args
)
5477 struct workspace
*ws
;
5480 DNPRINTF(SWM_D_MISC
, "search_workspace\n");
5486 search_resp_action
= SWM_SEARCH_SEARCH_WORKSPACE
;
5488 spawn_select(r
, args
, "search", &searchpid
);
5490 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
5493 for (i
= 0; i
< workspace_limit
; i
++) {
5497 fprintf(lfile
, "%d%s%s\n", ws
->idx
+ 1,
5498 (ws
->name
? ":" : ""), (ws
->name
? ws
->name
: ""));
5505 search_win_cleanup(void)
5507 struct search_window
*sw
= NULL
;
5509 while ((sw
= TAILQ_FIRST(&search_wl
)) != NULL
) {
5510 xcb_destroy_window(conn
, sw
->indicator
);
5511 TAILQ_REMOVE(&search_wl
, sw
, entry
);
5517 search_win(struct swm_region
*r
, union arg
*args
)
5519 struct ws_win
*win
= NULL
;
5520 struct search_window
*sw
= NULL
;
5523 xcb_screen_t
*screen
;
5524 int i
, width
, height
;
5532 XRectangle l_ibox
, l_lbox
;
5534 DNPRINTF(SWM_D_MISC
, "search_win\n");
5537 search_resp_action
= SWM_SEARCH_SEARCH_WINDOW
;
5539 spawn_select(r
, args
, "search", &searchpid
);
5541 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
5544 if ((screen
= get_screen(r
->s
->idx
)) == NULL
)
5545 errx(1, "ERROR: can't get screen %d.", r
->s
->idx
);
5547 TAILQ_INIT(&search_wl
);
5550 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
5554 sw
= calloc(1, sizeof(struct search_window
));
5556 warn("search_win: calloc");
5558 search_win_cleanup();
5564 snprintf(s
, sizeof s
, "%d", i
);
5567 w
= xcb_generate_id(conn
);
5568 wa
[0] = r
->s
->c
[SWM_S_COLOR_FOCUS
].pixel
;
5569 wa
[1] = r
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
;
5570 wa
[2] = screen
->default_colormap
;
5572 if (bar_font_legacy
) {
5573 XmbTextExtents(bar_fs
, s
, len
, &l_ibox
, &l_lbox
);
5574 width
= l_lbox
.width
+ 4;
5575 height
= bar_fs_extents
->max_logical_extent
.height
+ 4;
5577 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
,
5579 width
= info
.width
+ 4;
5580 height
= bar_font
->height
+ 4;
5583 xcb_create_window(conn
, screen
->root_depth
, w
, win
->id
, 0, 0,
5584 width
, height
, 1, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
5585 screen
->root_visual
, XCB_CW_BACK_PIXEL
|
5586 XCB_CW_BORDER_PIXEL
| XCB_CW_COLORMAP
, wa
);
5588 xcb_map_window(conn
, w
);
5591 TAILQ_INSERT_TAIL(&search_wl
, sw
, entry
);
5593 if (bar_font_legacy
) {
5594 l_gcv
.graphics_exposures
= 0;
5595 l_draw
= XCreateGC(display
, w
, 0, &l_gcv
);
5597 XSetForeground(display
, l_draw
,
5598 r
->s
->c
[SWM_S_COLOR_BAR
].pixel
);
5600 DRAWSTRING(display
, w
, bar_fs
, l_draw
, 2,
5601 (bar_fs_extents
->max_logical_extent
.height
-
5602 l_lbox
.height
) / 2 - l_lbox
.y
, s
, len
);
5604 XFreeGC(display
, l_draw
);
5607 draw
= XftDrawCreate(display
, w
,
5608 DefaultVisual(display
, r
->s
->idx
),
5609 DefaultColormap(display
, r
->s
->idx
));
5611 XftDrawStringUtf8(draw
, &search_font_color
, bar_font
, 2,
5612 (HEIGHT(r
->bar
) + bar_font
->height
) / 2 -
5613 bar_font
->descent
, (FcChar8
*)s
, len
);
5615 XftDrawDestroy(draw
);
5618 DNPRINTF(SWM_D_MISC
, "search_win: mapped win %#x\n", w
);
5620 fprintf(lfile
, "%d\n", i
);
5630 search_resp_uniconify(const char *resp
, size_t len
)
5636 DNPRINTF(SWM_D_MISC
, "search_resp_uniconify: resp: %s\n", resp
);
5638 TAILQ_FOREACH(win
, &search_r
->ws
->winlist
, entry
) {
5641 name
= get_win_name(win
->id
);
5642 if (asprintf(&s
, "%s.%u", name
, win
->id
) == -1) {
5647 if (strncmp(s
, resp
, len
) == 0) {
5648 /* XXX this should be a callback to generalize */
5649 ewmh_apply_flags(win
, win
->ewmh_flags
& ~EWMH_F_HIDDEN
);
5650 ewmh_update_wm_state(win
);
5660 search_resp_name_workspace(const char *resp
, size_t len
)
5662 struct workspace
*ws
;
5664 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: resp: %s\n", resp
);
5666 if (search_r
->ws
== NULL
)
5671 free(search_r
->ws
->name
);
5672 search_r
->ws
->name
= NULL
;
5676 ws
->name
= strdup(resp
);
5677 if (ws
->name
== NULL
) {
5678 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: "
5679 "strdup: %s", strerror(errno
));
5683 ewmh_update_desktop_names();
5684 ewmh_get_desktop_names();
5689 ewmh_update_desktop_names(void)
5691 char *name_list
= NULL
, *p
;
5692 int num_screens
, i
, j
, len
= 0, tot
= 0;
5694 num_screens
= get_screen_count();
5695 for (i
= 0; i
< num_screens
; ++i
) {
5696 for (j
= 0; j
< workspace_limit
; ++j
) {
5697 if (screens
[i
].ws
[j
].name
!= NULL
)
5698 len
+= strlen(screens
[i
].ws
[j
].name
);
5702 if((name_list
= calloc(len
, sizeof(char))) == NULL
)
5703 err(1, "update_desktop_names: calloc: failed to "
5704 "allocate memory.");
5707 for (j
= 0; j
< workspace_limit
; ++j
) {
5708 if (screens
[i
].ws
[j
].name
!= NULL
) {
5709 len
= strlen(screens
[i
].ws
[j
].name
);
5710 memcpy(p
, screens
[i
].ws
[j
].name
, len
);
5719 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
5720 screens
[i
].root
, ewmh
[_NET_DESKTOP_NAMES
].atom
,
5721 a_utf8_string
, 8, tot
, name_list
);
5731 ewmh_get_desktop_names(void)
5734 xcb_get_property_cookie_t c
;
5735 xcb_get_property_reply_t
*r
;
5736 int num_screens
, i
, j
, n
, k
;
5738 num_screens
= get_screen_count();
5739 for (i
= 0; i
< num_screens
; ++i
) {
5740 for (j
= 0; j
< workspace_limit
; ++j
) {
5741 free(screens
[i
].ws
[j
].name
);
5742 screens
[i
].ws
[j
].name
= NULL
;
5745 c
= xcb_get_property(conn
, 0, screens
[i
].root
,
5746 ewmh
[_NET_DESKTOP_NAMES
].atom
,
5747 a_utf8_string
, 0, UINT32_MAX
);
5748 r
= xcb_get_property_reply(conn
, c
, NULL
);
5752 names
= xcb_get_property_value(r
);
5753 n
= xcb_get_property_value_length(r
);
5755 for (j
= 0, k
= 0; j
< n
; ++j
) {
5756 if (*(names
+ j
) != '\0') {
5757 screens
[i
].ws
[k
].name
= strdup(names
+ j
);
5758 j
+= strlen(names
+ j
);
5767 ewmh_update_client_list(void)
5770 int num_screens
, i
, j
, k
= 0, count
= 0;
5773 num_screens
= get_screen_count();
5774 for (i
= 0; i
< num_screens
; ++i
) {
5775 for (j
= 0; j
< workspace_limit
; ++j
)
5776 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
5779 DNPRINTF(SWM_D_PROP
, "ewmh_update_client_list: win count: %d\n",
5785 wins
= calloc(count
, sizeof(xcb_window_t
));
5787 err(1, "ewmh_update_client_list: calloc: failed to "
5788 "allocate memory.");
5790 for (j
= 0, k
= 0; j
< workspace_limit
; ++j
)
5791 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
5792 wins
[k
++] = win
->id
;
5794 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
5795 screens
[i
].root
, ewmh
[_NET_CLIENT_LIST
].atom
,
5796 XCB_ATOM_WINDOW
, 32, count
, wins
);
5803 ewmh_update_current_desktop(void)
5807 num_screens
= get_screen_count();
5808 for (i
= 0; i
< num_screens
; ++i
)
5809 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
5810 screens
[i
].root
, ewmh
[_NET_CURRENT_DESKTOP
].atom
,
5811 XCB_ATOM_CARDINAL
, 32, 1, &screens
[i
].r_focus
->ws
->idx
);
5815 ewmh_update_desktops(void)
5817 int num_screens
, i
, j
;
5820 vals
= calloc(workspace_limit
* 2, sizeof(uint32_t));
5822 err(1, "ewmh_update_desktops: calloc: failed to allocate "
5825 num_screens
= get_screen_count();
5826 for (i
= 0; i
< num_screens
; i
++) {
5827 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
5828 screens
[i
].root
, ewmh
[_NET_NUMBER_OF_DESKTOPS
].atom
,
5829 XCB_ATOM_CARDINAL
, 32, 1, &workspace_limit
);
5831 for (j
= 0; j
< workspace_limit
; ++j
) {
5832 if (screens
[i
].ws
[j
].r
!= NULL
) {
5833 vals
[j
* 2] = X(screens
[i
].ws
[j
].r
);
5834 vals
[j
* 2 + 1] = Y(screens
[i
].ws
[j
].r
);
5835 } else if (screens
[i
].ws
[j
].old_r
!= NULL
) {
5836 vals
[j
* 2] = X(screens
[i
].ws
[j
].old_r
);
5837 vals
[j
* 2 + 1] = Y(screens
[i
].ws
[j
].old_r
);
5839 vals
[j
* 2] = vals
[j
* 2 + 1] = 0;
5843 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
5844 screens
[i
].root
, ewmh
[_NET_DESKTOP_VIEWPORT
].atom
,
5845 XCB_ATOM_CARDINAL
, 32, workspace_limit
* 2, vals
);
5852 search_resp_search_workspace(const char *resp
)
5859 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: resp: %s\n", resp
);
5863 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: strdup: %s",
5870 ws_idx
= (int)strtonum(q
, 1, workspace_limit
, &errstr
);
5872 DNPRINTF(SWM_D_MISC
, "workspace idx is %s: %s",
5879 switchws(search_r
, &a
);
5883 search_resp_search_window(const char *resp
)
5888 struct search_window
*sw
;
5890 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: resp: %s\n", resp
);
5894 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: strdup: %s",
5899 idx
= (int)strtonum(s
, 1, INT_MAX
, &errstr
);
5901 DNPRINTF(SWM_D_MISC
, "window idx is %s: %s",
5908 TAILQ_FOREACH(sw
, &search_wl
, entry
)
5909 if (idx
== sw
->idx
) {
5915 #define MAX_RESP_LEN 1024
5918 search_do_resp(void)
5924 DNPRINTF(SWM_D_MISC
, "search_do_resp:\n");
5929 if ((resp
= calloc(1, MAX_RESP_LEN
+ 1)) == NULL
) {
5930 warn("search: calloc");
5934 rbytes
= read(select_resp_pipe
[0], resp
, MAX_RESP_LEN
);
5936 warn("search: read error");
5939 resp
[rbytes
] = '\0';
5942 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
5943 * newline, so work around that by sanitizing the resp now.
5945 resp
[strcspn(resp
, "\n")] = '\0';
5948 switch (search_resp_action
) {
5949 case SWM_SEARCH_UNICONIFY
:
5950 search_resp_uniconify(resp
, len
);
5952 case SWM_SEARCH_NAME_WORKSPACE
:
5953 search_resp_name_workspace(resp
, len
);
5955 case SWM_SEARCH_SEARCH_WORKSPACE
:
5956 search_resp_search_workspace(resp
);
5958 case SWM_SEARCH_SEARCH_WINDOW
:
5959 search_resp_search_window(resp
);
5964 if (search_resp_action
== SWM_SEARCH_SEARCH_WINDOW
)
5965 search_win_cleanup();
5967 search_resp_action
= SWM_SEARCH_NONE
;
5968 close(select_resp_pipe
[0]);
5975 wkill(struct swm_region
*r
, union arg
*args
)
5977 DNPRINTF(SWM_D_MISC
, "wkill: win %#x, id: %d\n", WINID(r
->ws
->focus
),
5980 if (r
->ws
->focus
== NULL
)
5983 if (args
->id
== SWM_ARG_ID_KILLWINDOW
)
5984 xcb_kill_client(conn
, r
->ws
->focus
->id
);
5986 if (r
->ws
->focus
->can_delete
)
5987 client_msg(r
->ws
->focus
, a_delete
, 0);
5993 clear_maximized(struct workspace
*ws
)
5998 /* Clear any maximized win(s) on ws, from bottom up. */
5999 TAILQ_FOREACH_REVERSE(w
, &ws
->stack
, ws_win_stack
, stack_entry
)
6001 ewmh_apply_flags(w
, w
->ewmh_flags
& ~EWMH_F_MAXIMIZED
);
6002 ewmh_update_wm_state(w
);
6010 maximize_toggle(struct swm_region
*r
, union arg
*args
)
6012 struct ws_win
*w
= r
->ws
->focus
;
6014 /* suppress unused warning since var is needed */
6020 DNPRINTF(SWM_D_MISC
, "maximize_toggle: win %#x\n", w
->id
);
6025 if (w
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
6028 ewmh_apply_flags(w
, w
->ewmh_flags
^ EWMH_F_MAXIMIZED
);
6029 ewmh_update_wm_state(w
);
6033 if (w
== w
->ws
->focus
)
6038 DNPRINTF(SWM_D_MISC
, "maximize_toggle: done\n");
6042 floating_toggle(struct swm_region
*r
, union arg
*args
)
6044 struct ws_win
*w
= r
->ws
->focus
;
6046 /* suppress unused warning since var is needed */
6052 DNPRINTF(SWM_D_MISC
, "floating_toggle: win %#x\n", w
->id
);
6054 if (FULLSCREEN(w
) || TRANS(w
))
6057 if (w
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
6060 ewmh_apply_flags(w
, w
->ewmh_flags
^ EWMH_F_ABOVE
);
6061 ewmh_update_wm_state(w
);
6065 if (w
== w
->ws
->focus
)
6070 DNPRINTF(SWM_D_MISC
, "floating_toggle: done\n");
6074 region_containment(struct ws_win
*win
, struct swm_region
*r
, int opts
)
6076 struct swm_geometry g
= r
->g
;
6077 int rt
, lt
, tp
, bm
, bw
;
6079 bw
= (opts
& SWM_CW_SOFTBOUNDARY
) ? boundary_width
: 0;
6082 * Perpendicular distance of each side of the window to the respective
6083 * side of the region boundary. Positive values indicate the side of
6084 * the window has passed beyond the region boundary.
6086 rt
= opts
& SWM_CW_RIGHT
? MAX_X(win
) + BORDER(win
) - MAX_X(r
) : bw
;
6087 lt
= opts
& SWM_CW_LEFT
? X(r
) - X(win
) + BORDER(win
) : bw
;
6088 bm
= opts
& SWM_CW_BOTTOM
? MAX_Y(win
) + BORDER(win
) - MAX_Y(r
) : bw
;
6089 tp
= opts
& SWM_CW_TOP
? Y(r
) - Y(win
) + BORDER(win
) : bw
;
6091 DNPRINTF(SWM_D_MISC
, "region_containment: win %#x, rt: %d, lt: %d, "
6092 "bm: %d, tp: %d, SOFTBOUNDARY: %s, HARDBOUNDARY: %s\n", win
->id
, rt
,
6093 lt
, bm
, tp
, YESNO(opts
& SWM_CW_SOFTBOUNDARY
),
6094 YESNO(opts
& SWM_CW_HARDBOUNDARY
));
6097 * Disable containment if any of the flagged sides went beyond the
6098 * containment boundary, or if containment is disabled.
6100 if (!(opts
& SWM_CW_HARDBOUNDARY
|| opts
& SWM_CW_SOFTBOUNDARY
) ||
6101 (bw
!= 0 && ((rt
> bw
) || (lt
> bw
) || (bm
> bw
) || (tp
> bw
)))) {
6102 /* Make sure window has at least 1 pixel in the region */
6103 g
.x
+= 1 - WIDTH(win
);
6104 g
.y
+= 1 - HEIGHT(win
);
6105 g
.w
+= 2 * WIDTH(win
) - 2;
6106 g
.h
+= 2 * HEIGHT(win
) - 2;
6109 constrain_window(win
, &g
, &opts
);
6112 /* Move or resize a window so that flagged side(s) fit into the supplied box. */
6114 constrain_window(struct ws_win
*win
, struct swm_geometry
*b
, int *opts
)
6116 DNPRINTF(SWM_D_MISC
, "constrain_window: win %#x, (x,y) w x h: "
6117 "(%d,%d) %d x %d, box: (x,y) w x h: (%d,%d) %d x %d, rt: %s, "
6118 "lt: %s, bt: %s, tp: %s, allow resize: %s\n", win
->id
, X(win
),
6119 Y(win
), WIDTH(win
), HEIGHT(win
), b
->x
, b
->y
, b
->w
, b
->h
,
6120 YESNO(*opts
& SWM_CW_RIGHT
), YESNO(*opts
& SWM_CW_LEFT
),
6121 YESNO(*opts
& SWM_CW_BOTTOM
), YESNO(*opts
& SWM_CW_TOP
),
6122 YESNO(*opts
& SWM_CW_RESIZABLE
));
6124 if ((*opts
& SWM_CW_RIGHT
) && MAX_X(win
) + BORDER(win
) > b
->x
+ b
->w
) {
6125 if (*opts
& SWM_CW_RESIZABLE
)
6126 WIDTH(win
) = b
->x
+ b
->w
- X(win
) - BORDER(win
);
6128 X(win
) = b
->x
+ b
->w
- WIDTH(win
) - BORDER(win
);
6131 if ((*opts
& SWM_CW_LEFT
) && X(win
) + BORDER(win
) < b
->x
) {
6132 if (*opts
& SWM_CW_RESIZABLE
)
6133 WIDTH(win
) -= b
->x
- X(win
) - BORDER(win
);
6135 X(win
) = b
->x
- BORDER(win
);
6138 if ((*opts
& SWM_CW_BOTTOM
) && MAX_Y(win
) + BORDER(win
) > b
->y
+ b
->h
) {
6139 if (*opts
& SWM_CW_RESIZABLE
)
6140 HEIGHT(win
) = b
->y
+ b
->h
- Y(win
) - BORDER(win
);
6142 Y(win
) = b
->y
+ b
->h
- HEIGHT(win
) - BORDER(win
);
6145 if ((*opts
& SWM_CW_TOP
) && Y(win
) + BORDER(win
) < b
->y
) {
6146 if (*opts
& SWM_CW_RESIZABLE
)
6147 HEIGHT(win
) -= b
->y
- Y(win
) - BORDER(win
);
6149 Y(win
) = b
->y
- BORDER(win
);
6152 if (*opts
& SWM_CW_RESIZABLE
) {
6155 if (HEIGHT(win
) < 1)
6161 update_window_color(struct ws_win
*win
)
6165 if (WS_FOCUSED(win
->ws
) && win
->ws
->focus
== win
)
6166 pixel
= MAXIMIZED(win
) ?
6167 &win
->s
->c
[SWM_S_COLOR_FOCUS_MAXIMIZED
].pixel
:
6168 &win
->s
->c
[SWM_S_COLOR_FOCUS
].pixel
;
6170 pixel
= MAXIMIZED(win
) ?
6171 &win
->s
->c
[SWM_S_COLOR_UNFOCUS_MAXIMIZED
].pixel
:
6172 &win
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
;
6174 xcb_change_window_attributes(conn
, win
->id
,
6175 XCB_CW_BORDER_PIXEL
, pixel
);
6179 update_window(struct ws_win
*win
)
6184 mask
= XCB_CONFIG_WINDOW_X
| XCB_CONFIG_WINDOW_Y
|
6185 XCB_CONFIG_WINDOW_WIDTH
| XCB_CONFIG_WINDOW_HEIGHT
|
6186 XCB_CONFIG_WINDOW_BORDER_WIDTH
;
6190 wc
[3] = HEIGHT(win
);
6191 wc
[4] = BORDER(win
);
6193 DNPRINTF(SWM_D_EVENT
, "update_window: win %#x, (x,y) w x h: "
6194 "(%d,%d) %d x %d, bordered: %s\n", win
->id
, wc
[0], wc
[1], wc
[2],
6195 wc
[3], YESNO(win
->bordered
));
6197 xcb_configure_window(conn
, win
->id
, mask
, wc
);
6200 #define SWM_RESIZE_STEPS (50)
6203 resize(struct ws_win
*win
, union arg
*args
)
6205 xcb_timestamp_t timestamp
= 0;
6206 struct swm_region
*r
= NULL
;
6207 struct swm_geometry g
;
6209 int top
= 0, left
= 0;
6211 xcb_cursor_t cursor
;
6212 xcb_query_pointer_reply_t
*xpr
= NULL
;
6213 xcb_generic_event_t
*evt
;
6214 xcb_motion_notify_event_t
*mne
;
6221 if (FULLSCREEN(win
))
6224 /* In max_stack mode, should only resize transients. */
6225 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
] && !TRANS(win
))
6228 DNPRINTF(SWM_D_EVENT
, "resize: win %#x, floating: %s, "
6229 "transient: %#x\n", win
->id
, YESNO(ABOVE(win
)),
6233 store_float_geom(win
);
6234 else if (!(TRANS(win
) || ABOVE(win
)))
6237 ewmh_apply_flags(win
, (win
->ewmh_flags
| SWM_F_MANUAL
| EWMH_F_ABOVE
) &
6239 ewmh_update_wm_state(win
);
6245 /* It's possible for win to have been freed during focus_flush(). */
6246 if (validate_win(win
)) {
6247 DNPRINTF(SWM_D_EVENT
, "resize: invalid win.\n");
6252 case SWM_ARG_ID_WIDTHSHRINK
:
6253 WIDTH(win
) -= SWM_RESIZE_STEPS
;
6256 case SWM_ARG_ID_WIDTHGROW
:
6257 WIDTH(win
) += SWM_RESIZE_STEPS
;
6260 case SWM_ARG_ID_HEIGHTSHRINK
:
6261 HEIGHT(win
) -= SWM_RESIZE_STEPS
;
6264 case SWM_ARG_ID_HEIGHTGROW
:
6265 HEIGHT(win
) += SWM_RESIZE_STEPS
;
6272 region_containment(win
, r
, SWM_CW_ALLSIDES
| SWM_CW_RESIZABLE
|
6273 SWM_CW_HARDBOUNDARY
);
6275 store_float_geom(win
);
6279 region_containment(win
, r
, SWM_CW_ALLSIDES
| SWM_CW_RESIZABLE
|
6280 SWM_CW_SOFTBOUNDARY
);
6283 /* get cursor offset from window root */
6284 xpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
6291 if (xpr
->win_x
< WIDTH(win
) / 2)
6294 if (xpr
->win_y
< HEIGHT(win
) / 2)
6297 if (args
->id
== SWM_ARG_ID_CENTER
)
6298 cursor
= cursors
[XC_SIZING
].cid
;
6300 cursor
= cursors
[left
? XC_TOP_LEFT_CORNER
:
6301 XC_TOP_RIGHT_CORNER
].cid
;
6303 cursor
= cursors
[left
? XC_BOTTOM_LEFT_CORNER
:
6304 XC_BOTTOM_RIGHT_CORNER
].cid
;
6306 xcb_grab_pointer(conn
, 0, win
->id
, MOUSEMASK
,
6307 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
, XCB_WINDOW_NONE
, cursor
,
6312 while (resizing
&& (evt
= xcb_wait_for_event(conn
))) {
6313 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
6314 case XCB_BUTTON_RELEASE
:
6315 DNPRINTF(SWM_D_EVENT
, "resize: BUTTON_RELEASE\n");
6318 case XCB_MOTION_NOTIFY
:
6319 mne
= (xcb_motion_notify_event_t
*)evt
;
6320 /* cursor offset/delta from start of the operation */
6321 dx
= mne
->root_x
- xpr
->root_x
;
6322 dy
= mne
->root_y
- xpr
->root_y
;
6328 if (args
->id
== SWM_ARG_ID_CENTER
) {
6329 if (g
.h
/ 2 + dy
< 1)
6333 HEIGHT(win
) = g
.h
+ 2 * dy
;
6341 HEIGHT(win
) = g
.h
+ dy
;
6348 if (args
->id
== SWM_ARG_ID_CENTER
) {
6349 if (g
.w
/ 2 + dx
< 1)
6353 WIDTH(win
) = g
.w
+ 2 * dx
;
6361 WIDTH(win
) = g
.w
+ dx
;
6364 /* not free, don't sync more than 120 times / second */
6365 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
6366 timestamp
= mne
->time
;
6367 regionize(win
, mne
->root_x
, mne
->root_y
);
6368 region_containment(win
, r
, SWM_CW_ALLSIDES
|
6369 SWM_CW_RESIZABLE
| SWM_CW_HARDBOUNDARY
|
6370 SWM_CW_SOFTBOUNDARY
);
6377 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
6378 ((xcb_key_press_event_t
*)evt
)->time
);
6384 /* It's possible for win to have been freed above. */
6385 if (validate_win(win
)) {
6386 DNPRINTF(SWM_D_EVENT
, "resize: invalid win.\n");
6394 region_containment(win
, r
, SWM_CW_ALLSIDES
| SWM_CW_RESIZABLE
|
6395 SWM_CW_HARDBOUNDARY
| SWM_CW_SOFTBOUNDARY
);
6399 store_float_geom(win
);
6401 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
6403 DNPRINTF(SWM_D_EVENT
, "resize: done.\n");
6407 resize_step(struct swm_region
*r
, union arg
*args
)
6409 struct ws_win
*win
= NULL
;
6411 if (r
&& r
->ws
&& r
->ws
->focus
)
6421 /* Try to set window region based on supplied coordinates or window center. */
6423 regionize(struct ws_win
*win
, int x
, int y
)
6425 struct swm_region
*r
= NULL
;
6427 r
= region_under(win
->s
, x
, y
);
6429 r
= region_under(win
->s
, X(win
) + WIDTH(win
) / 2,
6430 Y(win
) + HEIGHT(win
) / 2);
6432 if (r
!= NULL
&& r
!= win
->ws
->r
) {
6433 if (clear_maximized(r
->ws
) > 0)
6436 win_to_ws(win
, r
->ws
->idx
, false);
6438 /* Set focus on new ws. */
6439 unfocus_win(r
->ws
->focus
);
6447 #define SWM_MOVE_STEPS (50)
6450 move(struct ws_win
*win
, union arg
*args
)
6452 struct swm_region
*r
;
6453 xcb_timestamp_t timestamp
= 0;
6455 xcb_query_pointer_reply_t
*qpr
= NULL
;
6456 xcb_generic_event_t
*evt
;
6457 xcb_motion_notify_event_t
*mne
;
6458 bool moving
, restack
= false;
6463 if ((r
= win
->ws
->r
) == NULL
)
6466 if (FULLSCREEN(win
))
6469 DNPRINTF(SWM_D_EVENT
, "move: win %#x, floating: %s, transient: "
6470 "%#x\n", win
->id
, YESNO(ABOVE(win
)), win
->transient
);
6472 /* in max_stack mode should only move transients */
6473 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
] && !TRANS(win
))
6476 if (!(ABOVE(win
) || TRANS(win
)) || MAXIMIZED(win
)) {
6477 store_float_geom(win
);
6481 ewmh_apply_flags(win
, (win
->ewmh_flags
| SWM_F_MANUAL
| EWMH_F_ABOVE
) &
6483 ewmh_update_wm_state(win
);
6490 /* It's possible for win to have been freed during focus_flush(). */
6491 if (validate_win(win
)) {
6492 DNPRINTF(SWM_D_EVENT
, "move: invalid win.\n");
6498 case SWM_ARG_ID_MOVELEFT
:
6499 X(win
) -= (SWM_MOVE_STEPS
- border_width
);
6502 case SWM_ARG_ID_MOVERIGHT
:
6503 X(win
) += (SWM_MOVE_STEPS
- border_width
);
6506 case SWM_ARG_ID_MOVEUP
:
6507 Y(win
) -= (SWM_MOVE_STEPS
- border_width
);
6510 case SWM_ARG_ID_MOVEDOWN
:
6511 Y(win
) += (SWM_MOVE_STEPS
- border_width
);
6518 regionize(win
, -1, -1);
6519 region_containment(win
, win
->ws
->r
, SWM_CW_ALLSIDES
);
6521 store_float_geom(win
);
6525 xcb_grab_pointer(conn
, 0, win
->id
, MOUSEMASK
,
6526 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
,
6527 XCB_WINDOW_NONE
, cursors
[XC_FLEUR
].cid
, XCB_CURRENT_TIME
);
6529 /* get cursor offset from window root */
6530 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
6533 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
6537 regionize(win
, qpr
->root_x
, qpr
->root_y
);
6538 region_containment(win
, win
->ws
->r
, SWM_CW_ALLSIDES
|
6539 SWM_CW_SOFTBOUNDARY
);
6543 while (moving
&& (evt
= xcb_wait_for_event(conn
))) {
6544 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
6545 case XCB_BUTTON_RELEASE
:
6546 DNPRINTF(SWM_D_EVENT
, "move: BUTTON_RELEASE\n");
6549 case XCB_MOTION_NOTIFY
:
6550 mne
= (xcb_motion_notify_event_t
*)evt
;
6551 DNPRINTF(SWM_D_EVENT
, "motion: root: %#x\n", mne
->root
);
6552 X(win
) = mne
->root_x
- qpr
->win_x
- border_width
;
6553 Y(win
) = mne
->root_y
- qpr
->win_y
- border_width
;
6555 /* not free, don't sync more than 120 times / second */
6556 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
6557 timestamp
= mne
->time
;
6558 regionize(win
, mne
->root_x
, mne
->root_y
);
6559 region_containment(win
, win
->ws
->r
,
6560 SWM_CW_ALLSIDES
| SWM_CW_SOFTBOUNDARY
);
6567 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
6568 ((xcb_key_press_event_t
*)evt
)->time
);
6574 /* It's possible for win to have been freed above. */
6575 if (validate_win(win
)) {
6576 DNPRINTF(SWM_D_EVENT
, "move: invalid win.\n");
6584 region_containment(win
, win
->ws
->r
, SWM_CW_ALLSIDES
|
6585 SWM_CW_SOFTBOUNDARY
);
6589 store_float_geom(win
);
6591 /* New region set to fullscreen layout. */
6592 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
]) {
6599 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
6600 DNPRINTF(SWM_D_EVENT
, "move: done.\n");
6604 move_step(struct swm_region
*r
, union arg
*args
)
6606 struct ws_win
*win
= NULL
;
6608 if (r
&& r
->ws
&& r
->ws
->focus
)
6613 if (!TRANS(win
) && !ABOVE(win
))
6621 /* key definitions */
6623 char name
[SWM_FUNCNAME_LEN
];
6624 void (*func
)(struct swm_region
*r
, union arg
*);
6626 } keyfuncs
[KF_INVALID
+ 1] = {
6627 /* name function argument */
6628 { "bar_toggle", bar_toggle
, {.id
= SWM_ARG_ID_BAR_TOGGLE
} },
6629 { "bar_toggle_ws", bar_toggle
, {.id
= SWM_ARG_ID_BAR_TOGGLE_WS
} },
6630 { "button2", pressbutton
, {2} },
6631 { "cycle_layout", cycle_layout
, {0} },
6632 { "flip_layout", stack_config
, {.id
= SWM_ARG_ID_FLIPLAYOUT
} },
6633 { "float_toggle", floating_toggle
,{0} },
6634 { "focus_main", focus
, {.id
= SWM_ARG_ID_FOCUSMAIN
} },
6635 { "focus_next", focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
6636 { "focus_prev", focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
6637 { "focus_urgent", focus
, {.id
= SWM_ARG_ID_FOCUSURGENT
} },
6638 { "maximize_toggle", maximize_toggle
,{0} },
6639 { "height_grow", resize_step
, {.id
= SWM_ARG_ID_HEIGHTGROW
} },
6640 { "height_shrink", resize_step
, {.id
= SWM_ARG_ID_HEIGHTSHRINK
} },
6641 { "iconify", iconify
, {0} },
6642 { "master_shrink", stack_config
, {.id
= SWM_ARG_ID_MASTERSHRINK
} },
6643 { "master_grow", stack_config
, {.id
= SWM_ARG_ID_MASTERGROW
} },
6644 { "master_add", stack_config
, {.id
= SWM_ARG_ID_MASTERADD
} },
6645 { "master_del", stack_config
, {.id
= SWM_ARG_ID_MASTERDEL
} },
6646 { "move_down", move_step
, {.id
= SWM_ARG_ID_MOVEDOWN
} },
6647 { "move_left", move_step
, {.id
= SWM_ARG_ID_MOVELEFT
} },
6648 { "move_right", move_step
, {.id
= SWM_ARG_ID_MOVERIGHT
} },
6649 { "move_up", move_step
, {.id
= SWM_ARG_ID_MOVEUP
} },
6650 { "mvrg_1", send_to_rg
, {.id
= 0} },
6651 { "mvrg_2", send_to_rg
, {.id
= 1} },
6652 { "mvrg_3", send_to_rg
, {.id
= 2} },
6653 { "mvrg_4", send_to_rg
, {.id
= 3} },
6654 { "mvrg_5", send_to_rg
, {.id
= 4} },
6655 { "mvrg_6", send_to_rg
, {.id
= 5} },
6656 { "mvrg_7", send_to_rg
, {.id
= 6} },
6657 { "mvrg_8", send_to_rg
, {.id
= 7} },
6658 { "mvrg_9", send_to_rg
, {.id
= 8} },
6659 { "mvws_1", send_to_ws
, {.id
= 0} },
6660 { "mvws_2", send_to_ws
, {.id
= 1} },
6661 { "mvws_3", send_to_ws
, {.id
= 2} },
6662 { "mvws_4", send_to_ws
, {.id
= 3} },
6663 { "mvws_5", send_to_ws
, {.id
= 4} },
6664 { "mvws_6", send_to_ws
, {.id
= 5} },
6665 { "mvws_7", send_to_ws
, {.id
= 6} },
6666 { "mvws_8", send_to_ws
, {.id
= 7} },
6667 { "mvws_9", send_to_ws
, {.id
= 8} },
6668 { "mvws_10", send_to_ws
, {.id
= 9} },
6669 { "mvws_11", send_to_ws
, {.id
= 10} },
6670 { "mvws_12", send_to_ws
, {.id
= 11} },
6671 { "mvws_13", send_to_ws
, {.id
= 12} },
6672 { "mvws_14", send_to_ws
, {.id
= 13} },
6673 { "mvws_15", send_to_ws
, {.id
= 14} },
6674 { "mvws_16", send_to_ws
, {.id
= 15} },
6675 { "mvws_17", send_to_ws
, {.id
= 16} },
6676 { "mvws_18", send_to_ws
, {.id
= 17} },
6677 { "mvws_19", send_to_ws
, {.id
= 18} },
6678 { "mvws_20", send_to_ws
, {.id
= 19} },
6679 { "mvws_21", send_to_ws
, {.id
= 20} },
6680 { "mvws_22", send_to_ws
, {.id
= 21} },
6681 { "name_workspace", name_workspace
, {0} },
6682 { "quit", quit
, {0} },
6683 { "raise_toggle", raise_toggle
, {0} },
6684 { "restart", restart
, {0} },
6685 { "rg_1", focusrg
, {.id
= 0} },
6686 { "rg_2", focusrg
, {.id
= 1} },
6687 { "rg_3", focusrg
, {.id
= 2} },
6688 { "rg_4", focusrg
, {.id
= 3} },
6689 { "rg_5", focusrg
, {.id
= 4} },
6690 { "rg_6", focusrg
, {.id
= 5} },
6691 { "rg_7", focusrg
, {.id
= 6} },
6692 { "rg_8", focusrg
, {.id
= 7} },
6693 { "rg_9", focusrg
, {.id
= 8} },
6694 { "rg_next", cyclerg
, {.id
= SWM_ARG_ID_CYCLERG_UP
} },
6695 { "rg_prev", cyclerg
, {.id
= SWM_ARG_ID_CYCLERG_DOWN
} },
6696 { "screen_next", cyclerg
, {.id
= SWM_ARG_ID_CYCLERG_UP
} },
6697 { "screen_prev", cyclerg
, {.id
= SWM_ARG_ID_CYCLERG_DOWN
} },
6698 { "search_win", search_win
, {0} },
6699 { "search_workspace", search_workspace
, {0} },
6700 { "spawn_custom", NULL
, {0} },
6701 { "stack_inc", stack_config
, {.id
= SWM_ARG_ID_STACKINC
} },
6702 { "stack_dec", stack_config
, {.id
= SWM_ARG_ID_STACKDEC
} },
6703 { "stack_reset", stack_config
, {.id
= SWM_ARG_ID_STACKRESET
} },
6704 { "swap_main", swapwin
, {.id
= SWM_ARG_ID_SWAPMAIN
} },
6705 { "swap_next", swapwin
, {.id
= SWM_ARG_ID_SWAPNEXT
} },
6706 { "swap_prev", swapwin
, {.id
= SWM_ARG_ID_SWAPPREV
} },
6707 { "uniconify", uniconify
, {0} },
6708 { "version", version
, {0} },
6709 { "width_grow", resize_step
, {.id
= SWM_ARG_ID_WIDTHGROW
} },
6710 { "width_shrink", resize_step
, {.id
= SWM_ARG_ID_WIDTHSHRINK
} },
6711 { "wind_del", wkill
, {.id
= SWM_ARG_ID_DELETEWINDOW
} },
6712 { "wind_kill", wkill
, {.id
= SWM_ARG_ID_KILLWINDOW
} },
6713 { "ws_1", switchws
, {.id
= 0} },
6714 { "ws_2", switchws
, {.id
= 1} },
6715 { "ws_3", switchws
, {.id
= 2} },
6716 { "ws_4", switchws
, {.id
= 3} },
6717 { "ws_5", switchws
, {.id
= 4} },
6718 { "ws_6", switchws
, {.id
= 5} },
6719 { "ws_7", switchws
, {.id
= 6} },
6720 { "ws_8", switchws
, {.id
= 7} },
6721 { "ws_9", switchws
, {.id
= 8} },
6722 { "ws_10", switchws
, {.id
= 9} },
6723 { "ws_11", switchws
, {.id
= 10} },
6724 { "ws_12", switchws
, {.id
= 11} },
6725 { "ws_13", switchws
, {.id
= 12} },
6726 { "ws_14", switchws
, {.id
= 13} },
6727 { "ws_15", switchws
, {.id
= 14} },
6728 { "ws_16", switchws
, {.id
= 15} },
6729 { "ws_17", switchws
, {.id
= 16} },
6730 { "ws_18", switchws
, {.id
= 17} },
6731 { "ws_19", switchws
, {.id
= 18} },
6732 { "ws_20", switchws
, {.id
= 19} },
6733 { "ws_21", switchws
, {.id
= 20} },
6734 { "ws_22", switchws
, {.id
= 21} },
6735 { "ws_next", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP
} },
6736 { "ws_next_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP_ALL
} },
6737 { "ws_next_move", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_MOVE_UP
} },
6738 { "ws_prev", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN
} },
6739 { "ws_prev_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN_ALL
} },
6740 { "ws_prev_move", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_MOVE_DOWN
} },
6741 { "ws_prior", priorws
, {0} },
6742 { "dumpwins", dumpwins
, {0} }, /* MUST BE LAST */
6743 { "invalid key func", NULL
, {0} },
6747 key_cmp(struct key
*kp1
, struct key
*kp2
)
6749 if (kp1
->keysym
< kp2
->keysym
)
6751 if (kp1
->keysym
> kp2
->keysym
)
6754 if (kp1
->mod
< kp2
->mod
)
6756 if (kp1
->mod
> kp2
->mod
)
6763 enum { client_click
, root_click
};
6765 unsigned int action
;
6767 unsigned int button
;
6768 void (*func
)(struct ws_win
*, union arg
*);
6771 #define MODKEY_SHIFT MODKEY | XCB_MOD_MASK_SHIFT
6772 /* action key mouse button func args */
6773 { client_click
, MODKEY
, XCB_BUTTON_INDEX_3
, resize
, {.id
= SWM_ARG_ID_DONTCENTER
} },
6774 { client_click
, MODKEY_SHIFT
, XCB_BUTTON_INDEX_3
, resize
, {.id
= SWM_ARG_ID_CENTER
} },
6775 { client_click
, MODKEY
, XCB_BUTTON_INDEX_1
, move
, {0} },
6780 update_modkey(unsigned int mod
)
6786 RB_FOREACH(kp
, key_tree
, &keys
)
6787 if (kp
->mod
& XCB_MOD_MASK_SHIFT
)
6788 kp
->mod
= mod
| XCB_MOD_MASK_SHIFT
;
6792 for (i
= 0; i
< LENGTH(buttons
); i
++)
6793 if (buttons
[i
].mask
& XCB_MOD_MASK_SHIFT
)
6794 buttons
[i
].mask
= mod
| XCB_MOD_MASK_SHIFT
;
6796 buttons
[i
].mask
= mod
;
6800 spawn_expand(struct swm_region
*r
, union arg
*args
, const char *spawn_name
,
6803 struct spawn_prog
*prog
= NULL
;
6805 char *ap
, **real_args
;
6807 /* suppress unused warning since var is needed */
6810 DNPRINTF(SWM_D_SPAWN
, "spawn_expand: %s\n", spawn_name
);
6813 TAILQ_FOREACH(prog
, &spawns
, entry
) {
6814 if (strcasecmp(spawn_name
, prog
->name
) == 0)
6818 warnx("spawn_custom: program %s not found", spawn_name
);
6822 /* make room for expanded args */
6823 if ((real_args
= calloc(prog
->argc
+ 1, sizeof(char *))) == NULL
)
6824 err(1, "spawn_custom: calloc real_args");
6826 /* expand spawn_args into real_args */
6827 for (i
= c
= 0; i
< prog
->argc
; i
++) {
6829 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: raw arg: %s\n", ap
);
6830 if (strcasecmp(ap
, "$bar_border") == 0) {
6832 strdup(r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].name
))
6834 err(1, "spawn_custom border color");
6835 } else if (strcasecmp(ap
, "$bar_color") == 0) {
6837 strdup(r
->s
->c
[SWM_S_COLOR_BAR
].name
))
6839 err(1, "spawn_custom bar color");
6840 } else if (strcasecmp(ap
, "$bar_font") == 0) {
6841 if ((real_args
[c
] = strdup(bar_fonts
))
6843 err(1, "spawn_custom bar fonts");
6844 } else if (strcasecmp(ap
, "$bar_font_color") == 0) {
6846 strdup(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].name
))
6848 err(1, "spawn_custom color font");
6849 } else if (strcasecmp(ap
, "$color_focus") == 0) {
6851 strdup(r
->s
->c
[SWM_S_COLOR_FOCUS
].name
))
6853 err(1, "spawn_custom color focus");
6854 } else if (strcasecmp(ap
, "$color_focus_maximized") == 0) {
6856 strdup(r
->s
->c
[SWM_S_COLOR_FOCUS_MAXIMIZED
].name
))
6858 err(1, "spawn_custom color focus maximized");
6859 } else if (strcasecmp(ap
, "$color_unfocus") == 0) {
6861 strdup(r
->s
->c
[SWM_S_COLOR_UNFOCUS
].name
))
6863 err(1, "spawn_custom color unfocus");
6864 } else if (strcasecmp(ap
, "$color_unfocus_maximized") == 0) {
6866 strdup(r
->s
->c
[SWM_S_COLOR_UNFOCUS_MAXIMIZED
].name
))
6868 err(1, "spawn_custom color unfocus maximized");
6869 } else if (strcasecmp(ap
, "$region_index") == 0) {
6870 if (asprintf(&real_args
[c
], "%d",
6871 get_region_index(r
) + 1) < 1)
6872 err(1, "spawn_custom region index");
6873 } else if (strcasecmp(ap
, "$workspace_index") == 0) {
6874 if (asprintf(&real_args
[c
], "%d", r
->ws
->idx
+ 1) < 1)
6875 err(1, "spawn_custom workspace index");
6876 } else if (strcasecmp(ap
, "$dmenu_bottom") == 0) {
6879 if ((real_args
[c
] = strdup("-b")) == NULL
)
6880 err(1, "spawn_custom workspace index");
6882 /* no match --> copy as is */
6883 if ((real_args
[c
] = strdup(ap
)) == NULL
)
6884 err(1, "spawn_custom strdup(ap)");
6886 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: cooked arg: %s\n",
6892 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: result: ");
6893 for (i
= 0; i
< c
; ++i
)
6894 DPRINTF("\"%s\" ", real_args
[i
]);
6897 *ret_args
= real_args
;
6902 spawn_custom(struct swm_region
*r
, union arg
*args
, const char *spawn_name
)
6908 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
6912 spawn(r
->ws
->idx
, &a
, true);
6914 for (i
= 0; i
< spawn_argc
; i
++)
6920 spawn_select(struct swm_region
*r
, union arg
*args
, const char *spawn_name
,
6927 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
6931 if (pipe(select_list_pipe
) == -1)
6932 err(1, "pipe error");
6933 if (pipe(select_resp_pipe
) == -1)
6934 err(1, "pipe error");
6936 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
6937 err(1, "could not disable SIGPIPE");
6938 switch (*pid
= fork()) {
6940 err(1, "cannot fork");
6943 if (dup2(select_list_pipe
[0], STDIN_FILENO
) == -1)
6945 if (dup2(select_resp_pipe
[1], STDOUT_FILENO
) == -1)
6947 close(select_list_pipe
[1]);
6948 close(select_resp_pipe
[0]);
6949 spawn(r
->ws
->idx
, &a
, false);
6951 default: /* parent */
6952 close(select_list_pipe
[0]);
6953 close(select_resp_pipe
[1]);
6957 for (i
= 0; i
< spawn_argc
; i
++)
6962 /* Argument tokenizer. */
6965 char *arg
, *cp
, *next
;
6966 bool single_quoted
= false, double_quoted
= false;
6971 /* Eat and move characters until end of argument is found. */
6972 for (arg
= next
= cp
= *sp
; *cp
!= '\0'; ++cp
) {
6973 if (!double_quoted
&& *cp
== '\'') {
6974 /* Eat single-quote. */
6975 single_quoted
= !single_quoted
;
6976 } else if (!single_quoted
&& *cp
== '"') {
6977 /* Eat double-quote. */
6978 double_quoted
= !double_quoted
;
6979 } else if (!single_quoted
&& *cp
== '\\' && *(cp
+ 1) == '"') {
6980 /* Eat backslash; copy escaped character to arg. */
6982 } else if (!single_quoted
&& !double_quoted
&& *cp
== '\\' &&
6983 (*(cp
+ 1) == '\'' || *(cp
+ 1) == ' ')) {
6984 /* Eat backslash; move escaped character. */
6986 } else if (!single_quoted
&& !double_quoted
&&
6987 (*cp
== ' ' || *cp
== '\t')) {
6988 /* Terminate argument. */
6990 /* Point sp to beginning of next argument. */
6994 /* Move regular character. */
6999 /* Terminate argument if end of string. */
7009 spawn_insert(const char *name
, const char *args
, int flags
)
7011 struct spawn_prog
*sp
;
7012 char *arg
, *cp
, *ptr
;
7014 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: %s[%s]\n", name
, args
);
7016 if (args
== NULL
|| *args
== '\0')
7019 if ((sp
= calloc(1, sizeof *sp
)) == NULL
)
7020 err(1, "spawn_insert: calloc");
7021 if ((sp
->name
= strdup(name
)) == NULL
)
7022 err(1, "spawn_insert: strdup");
7024 /* Convert the arguments to an argument list. */
7025 if ((ptr
= cp
= strdup(args
)) == NULL
)
7026 err(1, "spawn_insert: strdup");
7027 while ((arg
= argsep(&ptr
)) != NULL
) {
7028 /* Null argument; skip it. */
7033 if ((sp
->argv
= realloc(sp
->argv
, sp
->argc
*
7034 sizeof *sp
->argv
)) == NULL
)
7035 err(1, "spawn_insert: realloc");
7036 if ((sp
->argv
[sp
->argc
- 1] = strdup(arg
)) == NULL
)
7037 err(1, "spawn_insert: strdup");
7043 DNPRINTF(SWM_D_SPAWN
, "arg %d: [%s]\n", sp
->argc
, sp
->argv
[sp
->argc
-1]);
7044 TAILQ_INSERT_TAIL(&spawns
, sp
, entry
);
7045 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: leave\n");
7049 spawn_remove(struct spawn_prog
*sp
)
7053 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: %s\n", sp
->name
);
7055 TAILQ_REMOVE(&spawns
, sp
, entry
);
7056 for (i
= 0; i
< sp
->argc
; i
++)
7062 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: leave\n");
7066 spawn_find(const char *name
)
7068 struct spawn_prog
*sp
;
7070 TAILQ_FOREACH(sp
, &spawns
, entry
)
7071 if (strcasecmp(sp
->name
, name
) == 0)
7078 setspawn(const char *name
, const char *args
, int flags
)
7080 struct spawn_prog
*sp
;
7082 DNPRINTF(SWM_D_SPAWN
, "setspawn: %s\n", name
);
7087 /* Remove any old spawn under the same name. */
7088 if ((sp
= spawn_find(name
)) != NULL
)
7092 spawn_insert(name
, args
, flags
);
7094 warnx("error: setspawn: cannot find program: %s", name
);
7096 DNPRINTF(SWM_D_SPAWN
, "setspawn: leave\n");
7100 setconfspawn(const char *selector
, const char *value
, int flags
)
7104 if (selector
== NULL
|| strlen(selector
) == 0)
7107 args
= expand_tilde(value
);
7109 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: [%s] [%s]\n", selector
, args
);
7111 setspawn(selector
, args
, flags
);
7114 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: done.\n");
7119 validate_spawns(void)
7121 struct spawn_prog
*sp
;
7122 char which
[PATH_MAX
];
7127 RB_FOREACH(kp
, key_tree
, &keys
) {
7128 if (kp
->funcid
!= KF_SPAWN_CUSTOM
)
7132 sp
= spawn_find(kp
->spawn_name
);
7133 if (sp
== NULL
|| sp
->flags
& SWM_SPAWN_OPTIONAL
)
7136 /* verify we have the goods */
7137 snprintf(which
, sizeof which
, "which %s", sp
->argv
[0]);
7138 DNPRINTF(SWM_D_CONF
, "validate_spawns: which %s\n",
7140 for (i
= strlen("which "); i
< strlen(which
); i
++)
7141 if (which
[i
] == ' ') {
7145 if (system(which
) != 0)
7146 add_startup_exception("could not find %s",
7147 &which
[strlen("which ")]);
7154 setconfspawn("lock", "xlock", 0);
7156 setconfspawn("term", "xterm", 0);
7157 setconfspawn("spawn_term", "xterm", 0);
7159 setconfspawn("menu", "dmenu_run"
7163 " -nf $bar_font_color"
7165 " -sf $bar_color", 0);
7167 setconfspawn("search", "dmenu"
7172 " -nf $bar_font_color"
7174 " -sf $bar_color", 0);
7176 setconfspawn("name_workspace", "dmenu"
7181 " -nf $bar_font_color"
7183 " -sf $bar_color", 0);
7185 /* These are not verified for existence, even with a binding set. */
7186 setconfspawn("screenshot_all", "screenshot.sh full", SWM_SPAWN_OPTIONAL
);
7187 setconfspawn("screenshot_wind", "screenshot.sh window", SWM_SPAWN_OPTIONAL
);
7188 setconfspawn("initscr", "initscreen.sh", SWM_SPAWN_OPTIONAL
);
7192 #define SWM_MODNAME_SIZE 32
7193 #define SWM_KEY_WS "\n+ \t"
7195 parsekeys(const char *keystr
, unsigned int currmod
, unsigned int *mod
, KeySym
*ks
)
7197 char *str
, *cp
, *name
;
7200 DNPRINTF(SWM_D_KEY
, "parsekeys: enter [%s]\n", keystr
);
7201 if (mod
== NULL
|| ks
== NULL
) {
7202 DNPRINTF(SWM_D_KEY
, "parsekeys: no mod or key vars\n");
7205 if (keystr
== NULL
|| strlen(keystr
) == 0) {
7206 DNPRINTF(SWM_D_KEY
, "parsekeys: no keystr\n");
7210 if ((cp
= str
= strdup(keystr
)) == NULL
)
7211 err(1, "parsekeys: strdup");
7213 *ks
= XCB_NO_SYMBOL
;
7215 while ((name
= strsep(&cp
, SWM_KEY_WS
)) != NULL
) {
7216 DNPRINTF(SWM_D_KEY
, "parsekeys: key [%s]\n", name
);
7218 cp
+= (long)strspn(cp
, SWM_KEY_WS
);
7219 if (strncasecmp(name
, "MOD", SWM_MODNAME_SIZE
) == 0)
7221 else if (strncasecmp(name
, "Mod1", SWM_MODNAME_SIZE
) == 0)
7222 *mod
|= XCB_MOD_MASK_1
;
7223 else if (strncasecmp(name
, "Mod2", SWM_MODNAME_SIZE
) == 0)
7224 *mod
+= XCB_MOD_MASK_2
;
7225 else if (strncmp(name
, "Mod3", SWM_MODNAME_SIZE
) == 0)
7226 *mod
|= XCB_MOD_MASK_3
;
7227 else if (strncmp(name
, "Mod4", SWM_MODNAME_SIZE
) == 0)
7228 *mod
|= XCB_MOD_MASK_4
;
7229 else if (strncasecmp(name
, "SHIFT", SWM_MODNAME_SIZE
) == 0)
7230 *mod
|= XCB_MOD_MASK_SHIFT
;
7231 else if (strncasecmp(name
, "CONTROL", SWM_MODNAME_SIZE
) == 0)
7232 *mod
|= XCB_MOD_MASK_CONTROL
;
7234 *ks
= XStringToKeysym(name
);
7235 XConvertCase(*ks
, ks
, &uks
);
7236 if (ks
== XCB_NO_SYMBOL
) {
7238 "parsekeys: invalid key %s\n",
7247 DNPRINTF(SWM_D_KEY
, "parsekeys: leave ok\n");
7252 strdupsafe(const char *str
)
7257 return (strdup(str
));
7261 key_insert(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
7262 const char *spawn_name
)
7266 DNPRINTF(SWM_D_KEY
, "key_insert: enter %s [%s]\n",
7267 keyfuncs
[kfid
].name
, spawn_name
);
7269 if ((kp
= malloc(sizeof *kp
)) == NULL
)
7270 err(1, "key_insert: malloc");
7275 kp
->spawn_name
= strdupsafe(spawn_name
);
7276 RB_INSERT(key_tree
, &keys
, kp
);
7278 DNPRINTF(SWM_D_KEY
, "key_insert: leave\n");
7282 key_lookup(unsigned int mod
, KeySym ks
)
7289 return (RB_FIND(key_tree
, &keys
, &kp
));
7293 key_remove(struct key
*kp
)
7295 DNPRINTF(SWM_D_KEY
, "key_remove: %s\n", keyfuncs
[kp
->funcid
].name
);
7297 RB_REMOVE(key_tree
, &keys
, kp
);
7298 free(kp
->spawn_name
);
7301 DNPRINTF(SWM_D_KEY
, "key_remove: leave\n");
7305 key_replace(struct key
*kp
, unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
7306 const char *spawn_name
)
7308 DNPRINTF(SWM_D_KEY
, "key_replace: %s [%s]\n", keyfuncs
[kp
->funcid
].name
,
7312 key_insert(mod
, ks
, kfid
, spawn_name
);
7314 DNPRINTF(SWM_D_KEY
, "key_replace: leave\n");
7318 setkeybinding(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
7319 const char *spawn_name
)
7323 DNPRINTF(SWM_D_KEY
, "setkeybinding: enter %s [%s]\n",
7324 keyfuncs
[kfid
].name
, spawn_name
);
7326 if ((kp
= key_lookup(mod
, ks
)) != NULL
) {
7327 if (kfid
== KF_INVALID
)
7330 key_replace(kp
, mod
, ks
, kfid
, spawn_name
);
7331 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
7334 if (kfid
== KF_INVALID
) {
7335 warnx("bind: Key combination already unbound.");
7336 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
7340 key_insert(mod
, ks
, kfid
, spawn_name
);
7341 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
7345 setconfbinding(const char *selector
, const char *value
, int flags
)
7347 enum keyfuncid kfid
;
7350 struct spawn_prog
*sp
;
7352 /* suppress unused warning since var is needed */
7355 DNPRINTF(SWM_D_KEY
, "setconfbinding: enter selector: [%s], "
7356 "value: [%s]\n", selector
, value
);
7357 if (selector
== NULL
|| strlen(selector
) == 0) {
7358 DNPRINTF(SWM_D_KEY
, "setconfbinding: unbind %s\n", value
);
7359 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
7361 setkeybinding(mod
, ks
, kfid
, NULL
);
7366 /* search by key function name */
7367 for (kfid
= 0; kfid
< KF_INVALID
; (kfid
)++) {
7368 if (strncasecmp(selector
, keyfuncs
[kfid
].name
,
7369 SWM_FUNCNAME_LEN
) == 0) {
7370 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match "
7371 "keyfunc\n", selector
);
7372 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
7373 setkeybinding(mod
, ks
, kfid
, NULL
);
7379 /* search by custom spawn name */
7380 if ((sp
= spawn_find(selector
)) != NULL
) {
7381 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match "
7382 "spawn\n", selector
);
7383 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
7384 setkeybinding(mod
, ks
, KF_SPAWN_CUSTOM
,
7390 DNPRINTF(SWM_D_KEY
, "setconfbinding: no match\n");
7397 #define MODKEY_SHIFT MODKEY | XCB_MOD_MASK_SHIFT
7398 setkeybinding(MODKEY
, XK_b
, KF_BAR_TOGGLE
, NULL
);
7399 setkeybinding(MODKEY_SHIFT
, XK_b
, KF_BAR_TOGGLE_WS
,NULL
);
7400 setkeybinding(MODKEY
, XK_v
, KF_BUTTON2
, NULL
);
7401 setkeybinding(MODKEY
, XK_space
, KF_CYCLE_LAYOUT
,NULL
);
7402 setkeybinding(MODKEY_SHIFT
, XK_backslash
, KF_FLIP_LAYOUT
, NULL
);
7403 setkeybinding(MODKEY
, XK_t
, KF_FLOAT_TOGGLE
,NULL
);
7404 setkeybinding(MODKEY
, XK_m
, KF_FOCUS_MAIN
, NULL
);
7405 setkeybinding(MODKEY
, XK_j
, KF_FOCUS_NEXT
, NULL
);
7406 setkeybinding(MODKEY
, XK_Tab
, KF_FOCUS_NEXT
, NULL
);
7407 setkeybinding(MODKEY
, XK_k
, KF_FOCUS_PREV
, NULL
);
7408 setkeybinding(MODKEY_SHIFT
, XK_Tab
, KF_FOCUS_PREV
, NULL
);
7409 setkeybinding(MODKEY
, XK_u
, KF_FOCUS_URGENT
,NULL
);
7410 setkeybinding(MODKEY
, XK_e
, KF_MAXIMIZE_TOGGLE
,NULL
);
7411 setkeybinding(MODKEY_SHIFT
, XK_equal
, KF_HEIGHT_GROW
,NULL
);
7412 setkeybinding(MODKEY_SHIFT
, XK_minus
, KF_HEIGHT_SHRINK
,NULL
);
7413 setkeybinding(MODKEY
, XK_w
, KF_ICONIFY
, NULL
);
7414 setkeybinding(MODKEY
, XK_h
, KF_MASTER_SHRINK
, NULL
);
7415 setkeybinding(MODKEY
, XK_l
, KF_MASTER_GROW
, NULL
);
7416 setkeybinding(MODKEY
, XK_comma
, KF_MASTER_ADD
, NULL
);
7417 setkeybinding(MODKEY
, XK_period
, KF_MASTER_DEL
, NULL
);
7418 setkeybinding(MODKEY_SHIFT
, XK_bracketright
,KF_MOVE_DOWN
,NULL
);
7419 setkeybinding(MODKEY
, XK_bracketleft
, KF_MOVE_LEFT
,NULL
);
7420 setkeybinding(MODKEY
, XK_bracketright
,KF_MOVE_RIGHT
,NULL
);
7421 setkeybinding(MODKEY_SHIFT
, XK_bracketleft
, KF_MOVE_UP
, NULL
);
7422 setkeybinding(MODKEY_SHIFT
, XK_KP_End
, KF_MVRG_1
, NULL
);
7423 setkeybinding(MODKEY_SHIFT
, XK_KP_Down
, KF_MVRG_2
, NULL
);
7424 setkeybinding(MODKEY_SHIFT
, XK_KP_Next
, KF_MVRG_3
, NULL
);
7425 setkeybinding(MODKEY_SHIFT
, XK_KP_Left
, KF_MVRG_4
, NULL
);
7426 setkeybinding(MODKEY_SHIFT
, XK_KP_Begin
, KF_MVRG_5
, NULL
);
7427 setkeybinding(MODKEY_SHIFT
, XK_KP_Right
, KF_MVRG_6
, NULL
);
7428 setkeybinding(MODKEY_SHIFT
, XK_KP_Home
, KF_MVRG_7
, NULL
);
7429 setkeybinding(MODKEY_SHIFT
, XK_KP_Up
, KF_MVRG_8
, NULL
);
7430 setkeybinding(MODKEY_SHIFT
, XK_KP_Prior
, KF_MVRG_9
, NULL
);
7431 setkeybinding(MODKEY_SHIFT
, XK_1
, KF_MVWS_1
, NULL
);
7432 setkeybinding(MODKEY_SHIFT
, XK_2
, KF_MVWS_2
, NULL
);
7433 setkeybinding(MODKEY_SHIFT
, XK_3
, KF_MVWS_3
, NULL
);
7434 setkeybinding(MODKEY_SHIFT
, XK_4
, KF_MVWS_4
, NULL
);
7435 setkeybinding(MODKEY_SHIFT
, XK_5
, KF_MVWS_5
, NULL
);
7436 setkeybinding(MODKEY_SHIFT
, XK_6
, KF_MVWS_6
, NULL
);
7437 setkeybinding(MODKEY_SHIFT
, XK_7
, KF_MVWS_7
, NULL
);
7438 setkeybinding(MODKEY_SHIFT
, XK_8
, KF_MVWS_8
, NULL
);
7439 setkeybinding(MODKEY_SHIFT
, XK_9
, KF_MVWS_9
, NULL
);
7440 setkeybinding(MODKEY_SHIFT
, XK_0
, KF_MVWS_10
, NULL
);
7441 setkeybinding(MODKEY_SHIFT
, XK_F1
, KF_MVWS_11
, NULL
);
7442 setkeybinding(MODKEY_SHIFT
, XK_F2
, KF_MVWS_12
, NULL
);
7443 setkeybinding(MODKEY_SHIFT
, XK_F3
, KF_MVWS_13
, NULL
);
7444 setkeybinding(MODKEY_SHIFT
, XK_F4
, KF_MVWS_14
, NULL
);
7445 setkeybinding(MODKEY_SHIFT
, XK_F5
, KF_MVWS_15
, NULL
);
7446 setkeybinding(MODKEY_SHIFT
, XK_F6
, KF_MVWS_16
, NULL
);
7447 setkeybinding(MODKEY_SHIFT
, XK_F7
, KF_MVWS_17
, NULL
);
7448 setkeybinding(MODKEY_SHIFT
, XK_F8
, KF_MVWS_18
, NULL
);
7449 setkeybinding(MODKEY_SHIFT
, XK_F9
, KF_MVWS_19
, NULL
);
7450 setkeybinding(MODKEY_SHIFT
, XK_F10
, KF_MVWS_20
, NULL
);
7451 setkeybinding(MODKEY_SHIFT
, XK_F11
, KF_MVWS_21
, NULL
);
7452 setkeybinding(MODKEY_SHIFT
, XK_F12
, KF_MVWS_22
, NULL
);
7453 setkeybinding(MODKEY_SHIFT
, XK_slash
, KF_NAME_WORKSPACE
,NULL
);
7454 setkeybinding(MODKEY_SHIFT
, XK_q
, KF_QUIT
, NULL
);
7455 setkeybinding(MODKEY_SHIFT
, XK_r
, KF_RAISE_TOGGLE
,NULL
);
7456 setkeybinding(MODKEY
, XK_q
, KF_RESTART
, NULL
);
7457 setkeybinding(MODKEY
, XK_KP_End
, KF_RG_1
, NULL
);
7458 setkeybinding(MODKEY
, XK_KP_Down
, KF_RG_2
, NULL
);
7459 setkeybinding(MODKEY
, XK_KP_Next
, KF_RG_3
, NULL
);
7460 setkeybinding(MODKEY
, XK_KP_Left
, KF_RG_4
, NULL
);
7461 setkeybinding(MODKEY
, XK_KP_Begin
, KF_RG_5
, NULL
);
7462 setkeybinding(MODKEY
, XK_KP_Right
, KF_RG_6
, NULL
);
7463 setkeybinding(MODKEY
, XK_KP_Home
, KF_RG_7
, NULL
);
7464 setkeybinding(MODKEY
, XK_KP_Up
, KF_RG_8
, NULL
);
7465 setkeybinding(MODKEY
, XK_KP_Prior
, KF_RG_9
, NULL
);
7466 setkeybinding(MODKEY_SHIFT
, XK_Right
, KF_RG_NEXT
, NULL
);
7467 setkeybinding(MODKEY_SHIFT
, XK_Left
, KF_RG_PREV
, NULL
);
7468 setkeybinding(MODKEY
, XK_f
, KF_SEARCH_WIN
, NULL
);
7469 setkeybinding(MODKEY
, XK_slash
, KF_SEARCH_WORKSPACE
,NULL
);
7470 setkeybinding(MODKEY_SHIFT
, XK_i
, KF_SPAWN_CUSTOM
,"initscr");
7471 setkeybinding(MODKEY_SHIFT
, XK_Delete
, KF_SPAWN_CUSTOM
,"lock");
7472 setkeybinding(MODKEY
, XK_p
, KF_SPAWN_CUSTOM
,"menu");
7473 setkeybinding(MODKEY
, XK_s
, KF_SPAWN_CUSTOM
,"screenshot_all");
7474 setkeybinding(MODKEY_SHIFT
, XK_s
, KF_SPAWN_CUSTOM
,"screenshot_wind");
7475 setkeybinding(MODKEY_SHIFT
, XK_Return
, KF_SPAWN_CUSTOM
,"term");
7476 setkeybinding(MODKEY_SHIFT
, XK_comma
, KF_STACK_INC
, NULL
);
7477 setkeybinding(MODKEY_SHIFT
, XK_period
, KF_STACK_DEC
, NULL
);
7478 setkeybinding(MODKEY_SHIFT
, XK_space
, KF_STACK_RESET
, NULL
);
7479 setkeybinding(MODKEY
, XK_Return
, KF_SWAP_MAIN
, NULL
);
7480 setkeybinding(MODKEY_SHIFT
, XK_j
, KF_SWAP_NEXT
, NULL
);
7481 setkeybinding(MODKEY_SHIFT
, XK_k
, KF_SWAP_PREV
, NULL
);
7482 setkeybinding(MODKEY_SHIFT
, XK_w
, KF_UNICONIFY
, NULL
);
7483 setkeybinding(MODKEY_SHIFT
, XK_v
, KF_VERSION
, NULL
);
7484 setkeybinding(MODKEY
, XK_equal
, KF_WIDTH_GROW
, NULL
);
7485 setkeybinding(MODKEY
, XK_minus
, KF_WIDTH_SHRINK
,NULL
);
7486 setkeybinding(MODKEY
, XK_x
, KF_WIND_DEL
, NULL
);
7487 setkeybinding(MODKEY_SHIFT
, XK_x
, KF_WIND_KILL
, NULL
);
7488 setkeybinding(MODKEY
, XK_1
, KF_WS_1
, NULL
);
7489 setkeybinding(MODKEY
, XK_2
, KF_WS_2
, NULL
);
7490 setkeybinding(MODKEY
, XK_3
, KF_WS_3
, NULL
);
7491 setkeybinding(MODKEY
, XK_4
, KF_WS_4
, NULL
);
7492 setkeybinding(MODKEY
, XK_5
, KF_WS_5
, NULL
);
7493 setkeybinding(MODKEY
, XK_6
, KF_WS_6
, NULL
);
7494 setkeybinding(MODKEY
, XK_7
, KF_WS_7
, NULL
);
7495 setkeybinding(MODKEY
, XK_8
, KF_WS_8
, NULL
);
7496 setkeybinding(MODKEY
, XK_9
, KF_WS_9
, NULL
);
7497 setkeybinding(MODKEY
, XK_0
, KF_WS_10
, NULL
);
7498 setkeybinding(MODKEY
, XK_F1
, KF_WS_11
, NULL
);
7499 setkeybinding(MODKEY
, XK_F2
, KF_WS_12
, NULL
);
7500 setkeybinding(MODKEY
, XK_F3
, KF_WS_13
, NULL
);
7501 setkeybinding(MODKEY
, XK_F4
, KF_WS_14
, NULL
);
7502 setkeybinding(MODKEY
, XK_F5
, KF_WS_15
, NULL
);
7503 setkeybinding(MODKEY
, XK_F6
, KF_WS_16
, NULL
);
7504 setkeybinding(MODKEY
, XK_F7
, KF_WS_17
, NULL
);
7505 setkeybinding(MODKEY
, XK_F8
, KF_WS_18
, NULL
);
7506 setkeybinding(MODKEY
, XK_F9
, KF_WS_19
, NULL
);
7507 setkeybinding(MODKEY
, XK_F10
, KF_WS_20
, NULL
);
7508 setkeybinding(MODKEY
, XK_F11
, KF_WS_21
, NULL
);
7509 setkeybinding(MODKEY
, XK_F12
, KF_WS_22
, NULL
);
7510 setkeybinding(MODKEY
, XK_Right
, KF_WS_NEXT
, NULL
);
7511 setkeybinding(MODKEY
, XK_Left
, KF_WS_PREV
, NULL
);
7512 setkeybinding(MODKEY
, XK_Up
, KF_WS_NEXT_ALL
, NULL
);
7513 setkeybinding(MODKEY
, XK_Down
, KF_WS_PREV_ALL
, NULL
);
7514 setkeybinding(MODKEY_SHIFT
, XK_Up
, KF_WS_NEXT_MOVE
,NULL
);
7515 setkeybinding(MODKEY_SHIFT
, XK_Down
, KF_WS_PREV_MOVE
,NULL
);
7516 setkeybinding(MODKEY
, XK_a
, KF_WS_PRIOR
, NULL
);
7518 setkeybinding(MODKEY_SHIFT
, XK_d
, KF_DUMPWINS
, NULL
);
7528 while (RB_EMPTY(&keys
) == 0) {
7529 kp
= RB_ROOT(&keys
);
7535 setkeymapping(const char *selector
, const char *value
, int flags
)
7537 char *keymapping_file
;
7539 /* suppress unused warnings since vars are needed */
7543 DNPRINTF(SWM_D_KEY
, "setkeymapping: enter\n");
7545 keymapping_file
= expand_tilde(value
);
7548 /* load new key bindings; if it fails, revert to default bindings */
7549 if (conf_load(keymapping_file
, SWM_CONF_KEYMAPPING
)) {
7554 free(keymapping_file
);
7556 DNPRINTF(SWM_D_KEY
, "setkeymapping: leave\n");
7561 updatenumlockmask(void)
7564 xcb_get_modifier_mapping_reply_t
*modmap_r
;
7565 xcb_keycode_t
*modmap
, kc
, *keycode
;
7569 modmap_r
= xcb_get_modifier_mapping_reply(conn
,
7570 xcb_get_modifier_mapping(conn
),
7573 modmap
= xcb_get_modifier_mapping_keycodes(modmap_r
);
7574 for (i
= 0; i
< 8; i
++) {
7575 for (j
= 0; j
< modmap_r
->keycodes_per_modifier
; j
++) {
7576 kc
= modmap
[i
* modmap_r
->keycodes_per_modifier
7578 keycode
= xcb_key_symbols_get_keycode(syms
,
7582 numlockmask
= (1 << i
);
7589 DNPRINTF(SWM_D_MISC
, "updatenumlockmask: %d\n", numlockmask
);
7596 int num_screens
, k
, j
;
7597 unsigned int modifiers
[4];
7598 xcb_keycode_t
*code
;
7600 DNPRINTF(SWM_D_MISC
, "grabkeys\n");
7601 updatenumlockmask();
7604 modifiers
[1] = numlockmask
;
7605 modifiers
[2] = XCB_MOD_MASK_LOCK
;
7606 modifiers
[3] = numlockmask
| XCB_MOD_MASK_LOCK
;
7608 num_screens
= get_screen_count();
7609 for (k
= 0; k
< num_screens
; k
++) {
7610 if (TAILQ_EMPTY(&screens
[k
].rl
))
7612 xcb_ungrab_key(conn
, XCB_GRAB_ANY
, screens
[k
].root
,
7614 RB_FOREACH(kp
, key_tree
, &keys
) {
7615 /* Skip unused ws binds. */
7616 if ((int)kp
->funcid
> KF_WS_1
+ workspace_limit
- 1 &&
7617 kp
->funcid
<= KF_WS_22
)
7620 /* Skip unused mvws binds. */
7621 if ((int)kp
->funcid
> KF_MVWS_1
+ workspace_limit
- 1 &&
7622 kp
->funcid
<= KF_MVWS_22
)
7625 if ((code
= xcb_key_symbols_get_keycode(syms
,
7627 for (j
= 0; j
< LENGTH(modifiers
); j
++)
7628 xcb_grab_key(conn
, 1,
7630 kp
->mod
| modifiers
[j
],
7631 *code
, XCB_GRAB_MODE_SYNC
,
7632 XCB_GRAB_MODE_SYNC
);
7640 grabbuttons(struct ws_win
*win
)
7642 unsigned int modifiers
[4];
7645 DNPRINTF(SWM_D_MOUSE
, "grabbuttons: win %#x\n", win
->id
);
7646 updatenumlockmask();
7649 modifiers
[1] = numlockmask
;
7650 modifiers
[2] = XCB_MOD_MASK_LOCK
;
7651 modifiers
[3] = numlockmask
| XCB_MOD_MASK_LOCK
;
7653 for (i
= 0; i
< LENGTH(buttons
); i
++)
7654 if (buttons
[i
].action
== client_click
)
7655 for (j
= 0; j
< LENGTH(modifiers
); ++j
)
7656 xcb_grab_button(conn
, 0, win
->id
, BUTTONMASK
,
7657 XCB_GRAB_MODE_SYNC
, XCB_GRAB_MODE_ASYNC
,
7658 XCB_WINDOW_NONE
, XCB_CURSOR_NONE
,
7659 buttons
[i
].button
, buttons
[i
].mask
|
7663 const char *quirkname
[] = {
7664 "NONE", /* config string for "no value" */
7672 "FOCUSONMAP_SINGLE",
7678 /* SWM_Q_DELIM: retain '|' for back compat for now (2009-08-11) */
7679 #define SWM_Q_DELIM "\n|+ \t"
7681 parsequirks(const char *qstr
, uint32_t *quirk
, int *ws
)
7683 char *str
, *cp
, *name
;
7686 if (quirk
== NULL
|| qstr
== NULL
)
7689 if ((str
= strdup(qstr
)) == NULL
)
7690 err(1, "parsequirks: strdup");
7694 while ((name
= strsep(&cp
, SWM_Q_DELIM
)) != NULL
) {
7696 cp
+= (long)strspn(cp
, SWM_Q_DELIM
);
7698 if (sscanf(name
, "WS[%d]", ws
) == 1) {
7704 for (i
= 0; i
< LENGTH(quirkname
); i
++) {
7705 if (strncasecmp(name
, quirkname
[i
],
7706 SWM_QUIRK_LEN
) == 0) {
7707 DNPRINTF(SWM_D_QUIRK
,
7708 "parsequirks: %s\n", name
);
7714 *quirk
|= 1 << (i
-1);
7718 if (i
>= LENGTH(quirkname
)) {
7719 DNPRINTF(SWM_D_QUIRK
,
7720 "parsequirks: invalid quirk [%s]\n", name
);
7731 quirk_insert(const char *class, const char *instance
, const char *name
,
7732 uint32_t quirk
, int ws
)
7736 bool failed
= false;
7738 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: class: %s, instance: %s, name: %s,"
7739 " value: %u, ws: %d\n", class, instance
, name
, quirk
, ws
);
7741 if ((qp
= malloc(sizeof *qp
)) == NULL
)
7742 err(1, "quirk_insert: malloc");
7744 if ((qp
->class = strdup(class)) == NULL
)
7745 err(1, "quirk_insert: strdup");
7746 if ((qp
->instance
= strdup(instance
)) == NULL
)
7747 err(1, "quirk_insert: strdup");
7748 if ((qp
->name
= strdup(name
)) == NULL
)
7749 err(1, "quirk_insert: strdup");
7751 if (asprintf(&str
, "^%s$", class) == -1)
7752 err(1, "quirk_insert: asprintf");
7753 if (regcomp(&qp
->regex_class
, str
, REG_EXTENDED
| REG_NOSUB
)) {
7754 add_startup_exception("regex failed to compile quirk 'class' "
7755 "field: %s", class);
7758 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: compiled: %s\n", str
);
7761 if (asprintf(&str
, "^%s$", instance
) == -1)
7762 err(1, "quirk_insert: asprintf");
7763 if (regcomp(&qp
->regex_instance
, str
, REG_EXTENDED
| REG_NOSUB
)) {
7764 add_startup_exception("regex failed to compile quirk 'instance'"
7765 " field: %s", instance
);
7768 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: compiled: %s\n", str
);
7771 if (asprintf(&str
, "^%s$", name
) == -1)
7772 err(1, "quirk_insert: asprintf");
7773 if (regcomp(&qp
->regex_name
, str
, REG_EXTENDED
| REG_NOSUB
)) {
7774 add_startup_exception("regex failed to compile quirk 'name' "
7778 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: compiled: %s\n", str
);
7782 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: regex error; skipping.\n");
7787 TAILQ_INSERT_TAIL(&quirks
, qp
, entry
);
7789 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: leave\n");
7793 quirk_remove(struct quirk
*qp
)
7795 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: %s:%s [%u]\n", qp
->class,
7796 qp
->name
, qp
->quirk
);
7798 TAILQ_REMOVE(&quirks
, qp
, entry
);
7801 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: leave\n");
7805 quirk_free(struct quirk
*qp
)
7807 regfree(&qp
->regex_class
);
7808 regfree(&qp
->regex_instance
);
7809 regfree(&qp
->regex_name
);
7817 quirk_replace(struct quirk
*qp
, const char *class, const char *instance
,
7818 const char *name
, uint32_t quirk
, int ws
)
7820 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: %s:%s:%s [%u], ws: %d\n", qp
->class,
7821 qp
->instance
, qp
->name
, qp
->quirk
, qp
->ws
);
7824 quirk_insert(class, instance
, name
, quirk
, ws
);
7826 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: leave\n");
7830 setquirk(const char *class, const char *instance
, const char *name
,
7831 uint32_t quirk
, int ws
)
7835 DNPRINTF(SWM_D_QUIRK
, "setquirk: enter %s:%s:%s [%u], ws: %d\n", class,
7836 instance
, name
, quirk
, ws
);
7838 /* Remove/replace existing quirk. */
7839 TAILQ_FOREACH(qp
, &quirks
, entry
) {
7840 if (strcmp(qp
->class, class) == 0 &&
7841 strcmp(qp
->instance
, instance
) == 0 &&
7842 strcmp(qp
->name
, name
) == 0) {
7843 if (quirk
== 0 && ws
== -1)
7846 quirk_replace(qp
, class, instance
, name
, quirk
,
7848 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
7853 /* Only insert if quirk is not NONE or forced ws is set. */
7854 if (quirk
|| ws
!= -1)
7855 quirk_insert(class, instance
, name
, quirk
, ws
);
7857 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
7860 /* Eat '\' in str used to escape square brackets and colon. */
7862 unescape_selector(char *str
)
7866 for (cp
= str
; *str
!= '\0'; ++str
, ++cp
) {
7867 if (*str
== '\\' && (*(str
+ 1) == ':' || *(str
+ 1) == ']' ||
7877 setconfquirk(const char *selector
, const char *value
, int flags
)
7879 char *str
, *cp
, *class;
7880 char *instance
= NULL
, *name
= NULL
;
7881 int retval
, count
= 0, ws
= -1;
7884 /* suppress unused warning since var is needed */
7887 if (selector
== NULL
|| strlen(selector
) == 0)
7890 if ((str
= strdup(selector
)) == NULL
)
7891 err(1, "setconfquirk: strdup");
7893 /* Find non-escaped colon. */
7900 for (++cp
; *cp
!= '\0'; ++cp
) {
7901 if (*cp
== ':' && *(cp
- 1) != '\\') {
7907 unescape_selector(class);
7909 instance
= class + strlen(class) + 1;
7910 unescape_selector(instance
);
7916 name
= instance
+ strlen(instance
) + 1;
7917 unescape_selector(name
);
7922 DNPRINTF(SWM_D_CONF
, "setconfquirk: class: %s, instance: %s, "
7923 "name: %s\n", class, instance
, name
);
7925 if ((retval
= parsequirks(value
, &qrks
, &ws
)) == 0)
7926 setquirk(class, instance
, name
, qrks
, ws
);
7935 setquirk("MPlayer", "xv", ".*",
7936 SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
| SWM_Q_FOCUSPREV
, -1);
7937 setquirk("OpenOffice.org 3.2", "VCLSalFrame", ".*",
7939 setquirk("Firefox-bin", "firefox-bin", ".*",
7941 setquirk("Firefox", "Dialog", ".*",
7943 setquirk("Gimp", "gimp", ".*",
7944 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
7945 setquirk("XTerm", "xterm", ".*",
7946 SWM_Q_XTERM_FONTADJ
, -1);
7947 setquirk("xine", "Xine Window", ".*",
7948 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
7949 setquirk("Xitk", "Xitk Combo", ".*",
7950 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
7951 setquirk("xine", "xine Panel", ".*",
7952 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
7953 setquirk("Xitk", "Xine Window", ".*",
7954 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
7955 setquirk("xine", "xine Video Fullscreen Window", ".*",
7956 SWM_Q_FULLSCREEN
| SWM_Q_FLOAT
, -1);
7957 setquirk("pcb", "pcb", ".*",
7959 setquirk("SDL_App", "SDL_App", ".*",
7960 SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
, -1);
7963 /* conf file stuff */
7964 #define SWM_CONF_FILE "spectrwm.conf"
7965 #define SWM_CONF_FILE_OLD "scrotwm.conf"
7969 SWM_S_BAR_AT_BOTTOM
,
7970 SWM_S_BAR_BORDER_WIDTH
,
7973 SWM_S_BAR_ENABLED_WS
,
7978 SWM_S_BOUNDARY_WIDTH
,
7979 SWM_S_CLOCK_ENABLED
,
7982 SWM_S_CYCLE_VISIBLE
,
7984 SWM_S_DISABLE_BORDER
,
7986 SWM_S_FOCUS_CLOSE_WRAP
,
7987 SWM_S_FOCUS_DEFAULT
,
7989 SWM_S_ICONIC_ENABLED
,
7990 SWM_S_JAVA_WORKAROUND
,
7991 SWM_S_REGION_PADDING
,
7996 SWM_S_STACK_ENABLED
,
7999 SWM_S_URGENT_COLLAPSE
,
8000 SWM_S_URGENT_ENABLED
,
8001 SWM_S_VERBOSE_LAYOUT
,
8003 SWM_S_WINDOW_CLASS_ENABLED
,
8004 SWM_S_WINDOW_INSTANCE_ENABLED
,
8005 SWM_S_WINDOW_NAME_ENABLED
,
8006 SWM_S_WORKSPACE_LIMIT
,
8007 SWM_S_WORKSPACE_NAME
,
8011 setconfvalue(const char *selector
, const char *value
, int flags
)
8013 struct workspace
*ws
;
8014 int i
, ws_id
, num_screens
;
8015 char *b
, *str
, s
[1024];
8018 case SWM_S_BAR_ACTION
:
8020 if ((bar_argv
[0] = expand_tilde(value
)) == NULL
)
8021 err(1, "setconfvalue: bar_action");
8023 case SWM_S_BAR_AT_BOTTOM
:
8024 bar_at_bottom
= (atoi(value
) != 0);
8026 case SWM_S_BAR_BORDER_WIDTH
:
8027 bar_border_width
= atoi(value
);
8028 if (bar_border_width
< 0)
8029 bar_border_width
= 0;
8031 case SWM_S_BAR_DELAY
:
8032 /* No longer needed; leave to not break old conf files. */
8034 case SWM_S_BAR_ENABLED
:
8035 bar_enabled
= (atoi(value
) != 0);
8037 case SWM_S_BAR_ENABLED_WS
:
8038 ws_id
= atoi(selector
) - 1;
8039 if (ws_id
< 0 || ws_id
>= workspace_limit
)
8040 errx(1, "setconfvalue: bar_enabled_ws: invalid "
8041 "workspace %d.", ws_id
+ 1);
8043 num_screens
= get_screen_count();
8044 for (i
= 0; i
< num_screens
; i
++) {
8045 ws
= (struct workspace
*)&screens
[i
].ws
;
8046 ws
[ws_id
].bar_enabled
= (atoi(value
) != 0);
8049 case SWM_S_BAR_FONT
:
8051 if (asprintf(&bar_fonts
, "%s,%s", value
, bar_fonts
) == -1)
8052 err(1, "setconfvalue: asprintf: failed to allocate "
8053 "memory for bar_fonts.");
8056 /* If already in xft mode, then we are done. */
8057 if (!bar_font_legacy
)
8060 if ((str
= strdup(value
)) == NULL
)
8061 err(1, "setconfvalue: strdup");
8063 /* If there are any non-XLFD entries, switch to Xft mode. */
8064 while ((b
= strsep(&str
, ",")) != NULL
) {
8068 bar_font_legacy
= false;
8075 case SWM_S_BAR_FORMAT
:
8077 if ((bar_format
= strdup(value
)) == NULL
)
8078 err(1, "setconfvalue: bar_format");
8080 case SWM_S_BAR_JUSTIFY
:
8081 if (strcmp(value
, "left") == 0)
8082 bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
8083 else if (strcmp(value
, "center") == 0)
8084 bar_justify
= SWM_BAR_JUSTIFY_CENTER
;
8085 else if (strcmp(value
, "right") == 0)
8086 bar_justify
= SWM_BAR_JUSTIFY_RIGHT
;
8088 errx(1, "invalid bar_justify");
8090 case SWM_S_BORDER_WIDTH
:
8091 border_width
= atoi(value
);
8092 if (border_width
< 0)
8095 case SWM_S_BOUNDARY_WIDTH
:
8096 boundary_width
= atoi(value
);
8097 if (boundary_width
< 0)
8100 case SWM_S_CLOCK_ENABLED
:
8101 clock_enabled
= (atoi(value
) != 0);
8103 case SWM_S_CLOCK_FORMAT
:
8104 #ifndef SWM_DENY_CLOCK_FORMAT
8106 if ((clock_format
= strdup(value
)) == NULL
)
8107 err(1, "setconfvalue: clock_format");
8110 case SWM_S_CYCLE_EMPTY
:
8111 cycle_empty
= (atoi(value
) != 0);
8113 case SWM_S_CYCLE_VISIBLE
:
8114 cycle_visible
= (atoi(value
) != 0);
8116 case SWM_S_DIALOG_RATIO
:
8117 dialog_ratio
= atof(value
);
8118 if (dialog_ratio
> 1.0 || dialog_ratio
<= .3)
8121 case SWM_S_DISABLE_BORDER
:
8122 disable_border
= (atoi(value
) != 0);
8124 case SWM_S_FOCUS_CLOSE
:
8125 if (strcmp(value
, "first") == 0)
8126 focus_close
= SWM_STACK_BOTTOM
;
8127 else if (strcmp(value
, "last") == 0)
8128 focus_close
= SWM_STACK_TOP
;
8129 else if (strcmp(value
, "next") == 0)
8130 focus_close
= SWM_STACK_ABOVE
;
8131 else if (strcmp(value
, "previous") == 0)
8132 focus_close
= SWM_STACK_BELOW
;
8134 errx(1, "focus_close");
8136 case SWM_S_FOCUS_CLOSE_WRAP
:
8137 focus_close_wrap
= (atoi(value
) != 0);
8139 case SWM_S_FOCUS_DEFAULT
:
8140 if (strcmp(value
, "last") == 0)
8141 focus_default
= SWM_STACK_TOP
;
8142 else if (strcmp(value
, "first") == 0)
8143 focus_default
= SWM_STACK_BOTTOM
;
8145 errx(1, "focus_default");
8147 case SWM_S_FOCUS_MODE
:
8148 if (strcmp(value
, "default") == 0)
8149 focus_mode
= SWM_FOCUS_DEFAULT
;
8150 else if (strcmp(value
, "follow") == 0 ||
8151 strcmp(value
, "follow_cursor") == 0)
8152 focus_mode
= SWM_FOCUS_FOLLOW
;
8153 else if (strcmp(value
, "manual") == 0)
8154 focus_mode
= SWM_FOCUS_MANUAL
;
8156 errx(1, "focus_mode");
8158 case SWM_S_ICONIC_ENABLED
:
8159 iconic_enabled
= (atoi(value
) != 0);
8161 case SWM_S_JAVA_WORKAROUND
:
8162 java_workaround
= (atoi(value
) != 0);
8164 case SWM_S_REGION_PADDING
:
8165 region_padding
= atoi(value
);
8166 if (region_padding
< 0)
8169 case SWM_S_SPAWN_ORDER
:
8170 if (strcmp(value
, "first") == 0)
8171 spawn_position
= SWM_STACK_BOTTOM
;
8172 else if (strcmp(value
, "last") == 0)
8173 spawn_position
= SWM_STACK_TOP
;
8174 else if (strcmp(value
, "next") == 0)
8175 spawn_position
= SWM_STACK_ABOVE
;
8176 else if (strcmp(value
, "previous") == 0)
8177 spawn_position
= SWM_STACK_BELOW
;
8179 errx(1, "spawn_position");
8181 case SWM_S_SPAWN_TERM
:
8182 setconfspawn("term", value
, 0);
8183 setconfspawn("spawn_term", value
, 0);
8186 /* No longer needed; leave to not break old conf files. */
8188 case SWM_S_SS_ENABLED
:
8189 /* No longer needed; leave to not break old conf files. */
8191 case SWM_S_STACK_ENABLED
:
8192 stack_enabled
= (atoi(value
) != 0);
8194 case SWM_S_TERM_WIDTH
:
8195 term_width
= atoi(value
);
8199 case SWM_S_TILE_GAP
:
8200 tile_gap
= atoi(value
);
8202 case SWM_S_URGENT_COLLAPSE
:
8203 urgent_collapse
= (atoi(value
) != 0);
8205 case SWM_S_URGENT_ENABLED
:
8206 urgent_enabled
= (atoi(value
) != 0);
8208 case SWM_S_VERBOSE_LAYOUT
:
8209 verbose_layout
= (atoi(value
) != 0);
8210 for (i
= 0; layouts
[i
].l_stack
!= NULL
; i
++) {
8212 layouts
[i
].l_string
= fancy_stacker
;
8214 layouts
[i
].l_string
= plain_stacker
;
8217 case SWM_S_WARP_POINTER
:
8218 warp_pointer
= (atoi(value
) != 0);
8220 case SWM_S_WINDOW_CLASS_ENABLED
:
8221 window_class_enabled
= (atoi(value
) != 0);
8223 case SWM_S_WINDOW_INSTANCE_ENABLED
:
8224 window_instance_enabled
= (atoi(value
) != 0);
8226 case SWM_S_WINDOW_NAME_ENABLED
:
8227 window_name_enabled
= (atoi(value
) != 0);
8229 case SWM_S_WORKSPACE_LIMIT
:
8230 workspace_limit
= atoi(value
);
8231 if (workspace_limit
> SWM_WS_MAX
)
8232 workspace_limit
= SWM_WS_MAX
;
8233 else if (workspace_limit
< 1)
8234 workspace_limit
= 1;
8236 ewmh_update_desktops();
8238 case SWM_S_WORKSPACE_NAME
:
8239 if (getenv("SWM_STARTED") != NULL
)
8243 if (sscanf(value
, "ws[%d]:%1023c", &ws_id
, s
) != 2)
8244 errx(1, "invalid entry, should be 'ws[<idx>]:name'");
8246 if (ws_id
< 0 || ws_id
>= workspace_limit
)
8247 errx(1, "setconfvalue: workspace_name: invalid "
8248 "workspace %d.", ws_id
+ 1);
8250 num_screens
= get_screen_count();
8251 for (i
= 0; i
< num_screens
; ++i
) {
8252 ws
= (struct workspace
*)&screens
[i
].ws
;
8254 if (strlen(s
) > 0) {
8255 free(ws
[ws_id
].name
);
8256 if ((ws
[ws_id
].name
= strdup(s
)) == NULL
)
8257 err(1, "setconfvalue: workspace_name.");
8259 ewmh_update_desktop_names();
8260 ewmh_get_desktop_names();
8271 setconfmodkey(const char *selector
, const char *value
, int flags
)
8273 /* suppress unused warnings since vars are needed */
8277 if (strncasecmp(value
, "Mod1", strlen("Mod1")) == 0)
8278 update_modkey(XCB_MOD_MASK_1
);
8279 else if (strncasecmp(value
, "Mod2", strlen("Mod2")) == 0)
8280 update_modkey(XCB_MOD_MASK_2
);
8281 else if (strncasecmp(value
, "Mod3", strlen("Mod3")) == 0)
8282 update_modkey(XCB_MOD_MASK_3
);
8283 else if (strncasecmp(value
, "Mod4", strlen("Mod4")) == 0)
8284 update_modkey(XCB_MOD_MASK_4
);
8291 setconfcolor(const char *selector
, const char *value
, int flags
)
8293 int first
, last
, i
= 0, num_screens
;
8295 num_screens
= get_screen_count();
8297 /* conf screen indices begin at 1; treat vals <= 0 as 'all screens.' */
8298 if (selector
== NULL
|| strlen(selector
) == 0 ||
8299 (last
= atoi(selector
) - 1) < 0) {
8301 last
= num_screens
- 1;
8306 if (last
>= num_screens
) {
8307 add_startup_exception("invalid screen index: %d out of bounds "
8308 "(maximum %d)", last
+ 1, num_screens
);
8312 for (i
= first
; i
<= last
; ++i
) {
8313 setscreencolor(value
, i
, flags
);
8316 * When setting focus/unfocus colors, we need to also
8317 * set maximize colors to match if they haven't been customized.
8319 if (flags
== SWM_S_COLOR_FOCUS
&&
8320 !screens
[i
].c
[SWM_S_COLOR_FOCUS_MAXIMIZED
].manual
)
8321 setscreencolor(value
, i
, SWM_S_COLOR_FOCUS_MAXIMIZED
);
8322 else if (flags
== SWM_S_COLOR_UNFOCUS
&&
8323 !screens
[i
].c
[SWM_S_COLOR_UNFOCUS_MAXIMIZED
].manual
)
8324 setscreencolor(value
, i
, SWM_S_COLOR_UNFOCUS_MAXIMIZED
);
8326 screens
[i
].c
[flags
].manual
= 1;
8333 setconfregion(const char *selector
, const char *value
, int flags
)
8335 /* suppress unused warnings since vars are needed */
8339 custom_region(value
);
8344 setautorun(const char *selector
, const char *value
, int flags
)
8354 /* suppress unused warnings since vars are needed */
8358 if (getenv("SWM_STARTED"))
8362 if (sscanf(value
, "ws[%d]:%1023c", &ws_id
, s
) != 2)
8363 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
8365 if (ws_id
< 0 || ws_id
>= workspace_limit
)
8366 errx(1, "autorun: invalid workspace %d", ws_id
+ 1);
8368 sp
= expand_tilde((char *)&s
);
8371 * This is a little intricate
8373 * If the pid already exists we simply reuse it because it means it was
8374 * used before AND not claimed by manage_window. We get away with
8375 * altering it in the parent after INSERT because this can not be a race
8378 while ((ap
= strsep(&sp
, " \t")) != NULL
) {
8381 DNPRINTF(SWM_D_SPAWN
, "setautorun: arg [%s]\n", ap
);
8383 if ((a
.argv
= realloc(a
.argv
, argc
* sizeof(char *))) == NULL
)
8384 err(1, "setautorun: realloc");
8385 a
.argv
[argc
- 1] = ap
;
8389 if ((a
.argv
= realloc(a
.argv
, (argc
+ 1) * sizeof(char *))) == NULL
)
8390 err(1, "setautorun: realloc");
8391 a
.argv
[argc
] = NULL
;
8393 if ((pid
= fork()) == 0) {
8394 spawn(ws_id
, &a
, true);
8403 p
= calloc(1, sizeof *p
);
8406 TAILQ_INSERT_TAIL(&pidlist
, p
, entry
);
8416 setlayout(const char *selector
, const char *value
, int flags
)
8418 struct workspace
*ws
;
8419 int ws_id
, i
, x
, mg
, ma
, si
, ar
;
8420 int st
= SWM_V_STACK
, num_screens
;
8424 /* suppress unused warnings since vars are needed */
8428 if (getenv("SWM_STARTED"))
8432 if (sscanf(value
, "ws[%d]:%d:%d:%d:%d:%1023c",
8433 &ws_id
, &mg
, &ma
, &si
, &ar
, s
) != 6)
8434 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
8435 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
8438 if (ws_id
< 0 || ws_id
>= workspace_limit
)
8439 errx(1, "layout: invalid workspace %d", ws_id
+ 1);
8441 if (strcasecmp(s
, "vertical") == 0)
8443 else if (strcasecmp(s
, "vertical_flip") == 0) {
8446 } else if (strcasecmp(s
, "horizontal") == 0)
8448 else if (strcasecmp(s
, "horizontal_flip") == 0) {
8451 } else if (strcasecmp(s
, "fullscreen") == 0)
8454 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
8455 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
8458 num_screens
= get_screen_count();
8459 for (i
= 0; i
< num_screens
; i
++) {
8460 ws
= (struct workspace
*)&screens
[i
].ws
;
8461 ws
[ws_id
].cur_layout
= &layouts
[st
];
8463 ws
[ws_id
].always_raise
= (ar
!= 0);
8464 if (st
== SWM_MAX_STACK
)
8468 for (x
= 0; x
< abs(mg
); x
++) {
8469 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
8470 mg
>= 0 ? SWM_ARG_ID_MASTERGROW
:
8471 SWM_ARG_ID_MASTERSHRINK
);
8474 for (x
= 0; x
< abs(ma
); x
++) {
8475 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
8476 ma
>= 0 ? SWM_ARG_ID_MASTERADD
:
8477 SWM_ARG_ID_MASTERDEL
);
8480 for (x
= 0; x
< abs(si
); x
++) {
8481 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
8482 si
>= 0 ? SWM_ARG_ID_STACKINC
:
8483 SWM_ARG_ID_STACKDEC
);
8487 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
8488 SWM_ARG_ID_FLIPLAYOUT
);
8495 /* config options */
8496 struct config_option
{
8498 int (*func
)(const char*, const char*, int);
8501 struct config_option configopt
[] = {
8502 { "autorun", setautorun
, 0 },
8503 { "bar_action", setconfvalue
, SWM_S_BAR_ACTION
},
8504 { "bar_at_bottom", setconfvalue
, SWM_S_BAR_AT_BOTTOM
},
8505 { "bar_border", setconfcolor
, SWM_S_COLOR_BAR_BORDER
},
8506 { "bar_border_unfocus", setconfcolor
, SWM_S_COLOR_BAR_BORDER_UNFOCUS
},
8507 { "bar_border_width", setconfvalue
, SWM_S_BAR_BORDER_WIDTH
},
8508 { "bar_color", setconfcolor
, SWM_S_COLOR_BAR
},
8509 { "bar_delay", setconfvalue
, SWM_S_BAR_DELAY
},
8510 { "bar_enabled", setconfvalue
, SWM_S_BAR_ENABLED
},
8511 { "bar_enabled_ws", setconfvalue
, SWM_S_BAR_ENABLED_WS
},
8512 { "bar_font", setconfvalue
, SWM_S_BAR_FONT
},
8513 { "bar_font_color", setconfcolor
, SWM_S_COLOR_BAR_FONT
},
8514 { "bar_format", setconfvalue
, SWM_S_BAR_FORMAT
},
8515 { "bar_justify", setconfvalue
, SWM_S_BAR_JUSTIFY
},
8516 { "bind", setconfbinding
, 0 },
8517 { "border_width", setconfvalue
, SWM_S_BORDER_WIDTH
},
8518 { "boundary_width", setconfvalue
, SWM_S_BOUNDARY_WIDTH
},
8519 { "clock_enabled", setconfvalue
, SWM_S_CLOCK_ENABLED
},
8520 { "clock_format", setconfvalue
, SWM_S_CLOCK_FORMAT
},
8521 { "color_focus", setconfcolor
, SWM_S_COLOR_FOCUS
},
8522 { "color_focus_maximized", setconfcolor
, SWM_S_COLOR_FOCUS_MAXIMIZED
},
8523 { "color_unfocus", setconfcolor
, SWM_S_COLOR_UNFOCUS
},
8524 { "color_unfocus_maximized", setconfcolor
, SWM_S_COLOR_UNFOCUS_MAXIMIZED
},
8525 { "cycle_empty", setconfvalue
, SWM_S_CYCLE_EMPTY
},
8526 { "cycle_visible", setconfvalue
, SWM_S_CYCLE_VISIBLE
},
8527 { "dialog_ratio", setconfvalue
, SWM_S_DIALOG_RATIO
},
8528 { "disable_border", setconfvalue
, SWM_S_DISABLE_BORDER
},
8529 { "focus_close", setconfvalue
, SWM_S_FOCUS_CLOSE
},
8530 { "focus_close_wrap", setconfvalue
, SWM_S_FOCUS_CLOSE_WRAP
},
8531 { "focus_default", setconfvalue
, SWM_S_FOCUS_DEFAULT
},
8532 { "focus_mode", setconfvalue
, SWM_S_FOCUS_MODE
},
8533 { "iconic_enabled", setconfvalue
, SWM_S_ICONIC_ENABLED
},
8534 { "java_workaround", setconfvalue
, SWM_S_JAVA_WORKAROUND
},
8535 { "keyboard_mapping", setkeymapping
, 0 },
8536 { "layout", setlayout
, 0 },
8537 { "modkey", setconfmodkey
, 0 },
8538 { "program", setconfspawn
, 0 },
8539 { "quirk", setconfquirk
, 0 },
8540 { "region", setconfregion
, 0 },
8541 { "region_padding", setconfvalue
, SWM_S_REGION_PADDING
},
8542 { "screenshot_app", setconfvalue
, SWM_S_SS_APP
},
8543 { "screenshot_enabled", setconfvalue
, SWM_S_SS_ENABLED
},
8544 { "spawn_position", setconfvalue
, SWM_S_SPAWN_ORDER
},
8545 { "spawn_term", setconfvalue
, SWM_S_SPAWN_TERM
},
8546 { "stack_enabled", setconfvalue
, SWM_S_STACK_ENABLED
},
8547 { "term_width", setconfvalue
, SWM_S_TERM_WIDTH
},
8548 { "tile_gap", setconfvalue
, SWM_S_TILE_GAP
},
8549 { "title_class_enabled", setconfvalue
, SWM_S_WINDOW_CLASS_ENABLED
}, /* For backwards compat. */
8550 { "title_name_enabled", setconfvalue
, SWM_S_WINDOW_INSTANCE_ENABLED
}, /* For backwards compat. */
8551 { "urgent_collapse", setconfvalue
, SWM_S_URGENT_COLLAPSE
},
8552 { "urgent_enabled", setconfvalue
, SWM_S_URGENT_ENABLED
},
8553 { "verbose_layout", setconfvalue
, SWM_S_VERBOSE_LAYOUT
},
8554 { "warp_pointer", setconfvalue
, SWM_S_WARP_POINTER
},
8555 { "window_class_enabled", setconfvalue
, SWM_S_WINDOW_CLASS_ENABLED
},
8556 { "window_instance_enabled", setconfvalue
, SWM_S_WINDOW_INSTANCE_ENABLED
},
8557 { "window_name_enabled", setconfvalue
, SWM_S_WINDOW_NAME_ENABLED
},
8558 { "workspace_limit", setconfvalue
, SWM_S_WORKSPACE_LIMIT
},
8559 { "name", setconfvalue
, SWM_S_WORKSPACE_NAME
},
8563 _add_startup_exception(const char *fmt
, va_list ap
)
8565 if (vasprintf(&startup_exception
, fmt
, ap
) == -1)
8566 warn("%s: asprintf", __func__
);
8570 add_startup_exception(const char *fmt
, ...)
8576 if (startup_exception
)
8579 /* force bar to be enabled due to exception */
8583 _add_startup_exception(fmt
, ap
);
8588 conf_load(const char *filename
, int keymapping
)
8591 char *line
= NULL
, *cp
, *ce
, *optsub
, *optval
= NULL
;
8592 size_t linelen
, lineno
= 0;
8593 int wordlen
, i
, optidx
, count
;
8594 struct config_option
*opt
= NULL
;
8596 DNPRINTF(SWM_D_CONF
, "conf_load: begin\n");
8598 if (filename
== NULL
) {
8599 warnx("conf_load: no filename");
8603 DNPRINTF(SWM_D_CONF
, "conf_load: open %s\n", filename
);
8605 if ((config
= fopen(filename
, "r")) == NULL
) {
8606 warn("conf_load: fopen: %s", filename
);
8610 while (!feof(config
)) {
8614 if ((line
= fparseln(config
, &linelen
, &lineno
, NULL
,
8615 FPARSELN_UNESCCOMM
| FPARSELN_UNESCCONT
)) == NULL
) {
8617 err(1, "%s", filename
);
8622 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
8623 if (cp
[0] == '\0') {
8627 /* get config option */
8628 wordlen
= strcspn(cp
, "=[ \t\n");
8630 add_startup_exception("%s: line %zd: no option found",
8635 for (i
= 0; i
< LENGTH(configopt
); i
++) {
8636 opt
= &configopt
[i
];
8637 if (strncasecmp(cp
, opt
->optname
, wordlen
) == 0 &&
8638 (int)strlen(opt
->optname
) == wordlen
) {
8644 add_startup_exception("%s: line %zd: unknown option "
8645 "%.*s", filename
, lineno
, wordlen
, cp
);
8648 if (keymapping
&& opt
&& strcmp(opt
->optname
, "bind")) {
8649 add_startup_exception("%s: line %zd: invalid option "
8650 "%.*s", filename
, lineno
, wordlen
, cp
);
8654 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
8656 /* get [selector] if any */
8661 /* Get length of selector. */
8662 for (ce
= ++cp
; *ce
!= '\0'; ++ce
) {
8663 /* Find matching (not escaped) bracket. */
8664 if (*ce
== ']' && *(ce
- 1) != '\\') {
8671 add_startup_exception("%s: line %zd: syntax "
8672 "error: unterminated selector", filename
,
8677 /* ce points at ']'; terminate optsub. */
8682 cp
+= strspn(cp
, "= \t\n"); /* eat trailing */
8685 if (strlen(optval
) == 0) {
8686 add_startup_exception("%s: line %zd: must supply value "
8687 "to %s", filename
, lineno
,
8688 configopt
[optidx
].optname
);
8691 /* call function to deal with it all */
8692 if (configopt
[optidx
].func(optsub
, optval
,
8693 configopt
[optidx
].funcflags
) != 0) {
8694 add_startup_exception("%s: line %zd: invalid data for "
8695 "%s", filename
, lineno
, configopt
[optidx
].optname
);
8703 DNPRINTF(SWM_D_CONF
, "conf_load: end\n");
8709 set_child_transient(struct ws_win
*win
, xcb_window_t
*trans
)
8711 struct ws_win
*parent
, *w
;
8712 struct swm_region
*r
;
8713 struct workspace
*ws
;
8714 xcb_icccm_wm_hints_t wmh
;
8716 parent
= find_window(win
->transient
);
8718 parent
->focus_child
= win
;
8720 DNPRINTF(SWM_D_MISC
, "set_child_transient: parent doesn't exist"
8721 " for %#x trans %#x\n", win
->id
, win
->transient
);
8723 r
= root_to_region(win
->s
->root
, SWM_CK_ALL
);
8725 /* parent doen't exist in our window list */
8726 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
8727 if (xcb_icccm_get_wm_hints_reply(conn
,
8728 xcb_icccm_get_wm_hints(conn
, w
->id
),
8730 warnx("can't get hints for %#x", w
->id
);
8734 if (win
->hints
.window_group
!= wmh
.window_group
)
8737 w
->focus_child
= win
;
8738 win
->transient
= w
->id
;
8740 DNPRINTF(SWM_D_MISC
, "set_child_transient: adjusting "
8741 "transient to %#x\n", win
->transient
);
8748 window_get_pid(xcb_window_t win
)
8753 xcb_get_property_cookie_t pc
;
8754 xcb_get_property_reply_t
*pr
;
8756 apid
= get_atom_from_string("_NET_WM_PID");
8757 if (apid
== XCB_ATOM_NONE
)
8760 pc
= xcb_get_property(conn
, 0, win
, apid
, XCB_ATOM_CARDINAL
, 0, 1);
8761 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
8764 if (pr
->type
!= XCB_ATOM_CARDINAL
) {
8769 if (pr
->type
== apid
&& pr
->format
== 32)
8770 ret
= *((pid_t
*)xcb_get_property_value(pr
));
8776 apid
= get_atom_from_string("_SWM_PID");
8777 pc
= xcb_get_property(conn
, 0, win
, apid
, XCB_ATOM_STRING
,
8779 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
8782 if (pr
->type
!= apid
) {
8787 ret
= (pid_t
)strtonum(xcb_get_property_value(pr
), 0, INT_MAX
, &errstr
);
8794 get_swm_ws(xcb_window_t id
)
8800 xcb_get_property_reply_t
*gpr
;
8802 gpr
= xcb_get_property_reply(conn
,
8803 xcb_get_property(conn
, 0, id
, a_swm_ws
,
8804 XCB_ATOM_STRING
, 0, SWM_PROPLEN
),
8809 proplen
= xcb_get_property_value_length(gpr
);
8811 prop
= malloc(proplen
+ 1);
8814 xcb_get_property_value(gpr
),
8816 prop
[proplen
] = '\0';
8823 DNPRINTF(SWM_D_PROP
, "get_swm_ws: _SWM_WS: %s\n", prop
);
8824 ws_idx
= (int)strtonum(prop
, 0, workspace_limit
- 1, &errstr
);
8826 DNPRINTF(SWM_D_PROP
, "get_swm_ws: win #%s: %s",
8836 get_ws_idx(struct ws_win
*win
)
8838 xcb_get_property_reply_t
*gpr
;
8844 gpr
= xcb_get_property_reply(conn
,
8845 xcb_get_property(conn
, 0, win
->id
, ewmh
[_NET_WM_DESKTOP
].atom
,
8846 XCB_ATOM_CARDINAL
, 0, 1),
8849 if (gpr
->type
== XCB_ATOM_CARDINAL
&& gpr
->format
== 32)
8850 ws_idx
= *((int *)xcb_get_property_value(gpr
));
8854 if (ws_idx
== -1 && !(win
->quirks
& SWM_Q_IGNORESPAWNWS
))
8855 ws_idx
= get_swm_ws(win
->id
);
8857 if (ws_idx
> workspace_limit
- 1 || ws_idx
< -1)
8860 DNPRINTF(SWM_D_PROP
, "get_ws_idx: win %#x, ws_idx: %d\n", win
->id
,
8867 manage_window(xcb_window_t id
, int spawn_pos
, bool mapped
)
8869 struct ws_win
*win
, *ww
;
8870 struct swm_region
*r
;
8873 xcb_get_geometry_reply_t
*gr
;
8874 xcb_window_t trans
= XCB_WINDOW_NONE
;
8875 uint32_t i
, wa
[2], new_flags
;
8876 int ws_idx
, force_ws
= -1;
8877 char *class, *instance
, *name
;
8879 if ((win
= find_window(id
)) != NULL
) {
8880 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x already "
8881 "managed; skipping.)\n", id
);
8882 return (win
); /* Already managed. */
8885 /* See if window is on the unmanaged list. */
8886 if ((win
= find_unmanaged_window(id
)) != NULL
) {
8887 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x found on "
8888 "unmanaged list.\n", id
);
8889 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
8892 set_child_transient(win
, &trans
);
8896 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x is new.\n", id
);
8899 /* Try to get initial window geometry. */
8900 gr
= xcb_get_geometry_reply(conn
, xcb_get_geometry(conn
, id
), NULL
);
8902 DNPRINTF(SWM_D_MISC
, "manage_window: get geometry failed.\n");
8906 /* Create and initialize ws_win object. */
8907 if ((win
= calloc(1, sizeof(struct ws_win
))) == NULL
)
8908 err(1, "manage_window: calloc: failed to allocate memory for "
8913 /* Figureout which region the window belongs to. */
8914 r
= root_to_region(gr
->root
, SWM_CK_ALL
);
8916 /* Ignore window border if there is one. */
8917 WIDTH(win
) = gr
->width
;
8918 HEIGHT(win
) = gr
->height
;
8919 X(win
) = gr
->x
+ gr
->border_width
- border_width
;
8920 Y(win
) = gr
->y
+ gr
->border_width
- border_width
;
8921 win
->bordered
= true;
8922 win
->mapped
= mapped
;
8923 win
->s
= r
->s
; /* this never changes */
8927 /* Select which X events to monitor and set border pixel color. */
8928 wa
[0] = win
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
;
8929 wa
[1] = XCB_EVENT_MASK_ENTER_WINDOW
| XCB_EVENT_MASK_PROPERTY_CHANGE
|
8930 XCB_EVENT_MASK_STRUCTURE_NOTIFY
;
8932 wa
[1] |= XCB_EVENT_MASK_LEAVE_WINDOW
| XCB_EVENT_MASK_FOCUS_CHANGE
;
8935 xcb_change_window_attributes(conn
, win
->id
, XCB_CW_BORDER_PIXEL
|
8936 XCB_CW_EVENT_MASK
, wa
);
8938 /* Get WM_SIZE_HINTS. */
8939 xcb_icccm_get_wm_normal_hints_reply(conn
,
8940 xcb_icccm_get_wm_normal_hints(conn
, win
->id
),
8944 xcb_icccm_get_wm_hints_reply(conn
,
8945 xcb_icccm_get_wm_hints(conn
, win
->id
),
8948 /* Get WM_TRANSIENT_FOR; see if window is a transient. */
8949 xcb_icccm_get_wm_transient_for_reply(conn
,
8950 xcb_icccm_get_wm_transient_for(conn
, win
->id
),
8953 win
->transient
= trans
;
8954 set_child_transient(win
, &win
->transient
);
8957 /* Get WM_PROTOCOLS. */
8958 get_wm_protocols(win
);
8960 /* Set initial quirks based on EWMH. */
8961 ewmh_autoquirk(win
);
8963 /* Determine initial quirks. */
8964 xcb_icccm_get_wm_class_reply(conn
,
8965 xcb_icccm_get_wm_class(conn
, win
->id
),
8968 class = win
->ch
.class_name
? win
->ch
.class_name
: "";
8969 instance
= win
->ch
.instance_name
? win
->ch
.instance_name
: "";
8970 name
= get_win_name(win
->id
);
8972 DNPRINTF(SWM_D_CLASS
, "manage_window: class: %s, instance: %s, "
8973 "name: %s\n", class, instance
, name
);
8975 /* java is retarded so treat it special */
8976 if (strstr(instance
, "sun-awt")) {
8977 DNPRINTF(SWM_D_CLASS
, "manage_window: java window detected.\n");
8981 TAILQ_FOREACH(qp
, &quirks
, entry
) {
8982 if (regexec(&qp
->regex_class
, class, 0, NULL
, 0) == 0 &&
8983 regexec(&qp
->regex_instance
, instance
, 0, NULL
, 0) == 0 &&
8984 regexec(&qp
->regex_name
, name
, 0, NULL
, 0) == 0) {
8985 DNPRINTF(SWM_D_CLASS
, "manage_window: matched "
8986 "quirk: %s:%s:%s mask: %#x, ws: %d\n", qp
->class,
8987 qp
->instance
, qp
->name
, qp
->quirk
, qp
->ws
);
8988 win
->quirks
= qp
->quirk
;
8989 if (qp
->ws
>= 0 && qp
->ws
< workspace_limit
)
8996 /* Reset font sizes (the bruteforce way; no default keybinding). */
8997 if (win
->quirks
& SWM_Q_XTERM_FONTADJ
) {
8998 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
8999 fake_keypress(win
, XK_KP_Subtract
, XCB_MOD_MASK_SHIFT
);
9000 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
9001 fake_keypress(win
, XK_KP_Add
, XCB_MOD_MASK_SHIFT
);
9004 /* Figure out which workspace the window belongs to. */
9005 if (!(win
->quirks
& SWM_Q_IGNOREPID
) &&
9006 (p
= find_pid(window_get_pid(win
->id
))) != NULL
) {
9007 win
->ws
= &r
->s
->ws
[p
->ws
];
9008 TAILQ_REMOVE(&pidlist
, p
, entry
);
9011 } else if ((ws_idx
= get_ws_idx(win
)) != -1 &&
9013 /* _SWM_WS is set; use that. */
9014 win
->ws
= &r
->s
->ws
[ws_idx
];
9015 } else if (trans
&& (ww
= find_window(trans
)) != NULL
) {
9016 /* Launch transients in the same ws as parent. */
9023 win
->ws
= &r
->s
->ws
[force_ws
];
9025 /* Set the _NET_WM_DESKTOP atom. */
9026 DNPRINTF(SWM_D_PROP
, "manage_window: set _NET_WM_DESKTOP: %d\n",
9028 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
9029 ewmh
[_NET_WM_DESKTOP
].atom
, XCB_ATOM_CARDINAL
, 32, 1, &win
->ws
->idx
);
9031 /* Remove any _SWM_WS now that we set _NET_WM_DESKTOP. */
9032 xcb_delete_property(conn
, win
->id
, a_swm_ws
);
9034 /* WS must already be set for this to work. */
9035 store_float_geom(win
);
9037 /* Make sure window is positioned inside its region, if its active. */
9039 region_containment(win
, r
, SWM_CW_ALLSIDES
|
9040 SWM_CW_HARDBOUNDARY
);
9045 /* Figure out where to insert the window in the workspace list. */
9046 if (trans
&& (ww
= find_window(trans
)))
9047 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, ww
, win
, entry
);
9048 else if (win
->ws
->focus
&& spawn_pos
== SWM_STACK_ABOVE
)
9049 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, win
->ws
->focus
, win
,
9051 else if (win
->ws
->focus
&& spawn_pos
== SWM_STACK_BELOW
)
9052 TAILQ_INSERT_BEFORE(win
->ws
->focus
, win
, entry
);
9053 else switch (spawn_pos
) {
9056 case SWM_STACK_ABOVE
:
9057 TAILQ_INSERT_TAIL(&win
->ws
->winlist
, win
, entry
);
9059 case SWM_STACK_BOTTOM
:
9060 case SWM_STACK_BELOW
:
9061 TAILQ_INSERT_HEAD(&win
->ws
->winlist
, win
, entry
);
9064 ewmh_update_client_list();
9066 TAILQ_INSERT_HEAD(&win
->ws
->stack
, win
, stack_entry
);
9069 /* Get/apply initial _NET_WM_STATE */
9070 ewmh_get_wm_state(win
);
9073 new_flags
= win
->ewmh_flags
;
9075 if (win
->quirks
& SWM_Q_FLOAT
)
9076 new_flags
|= EWMH_F_ABOVE
;
9078 if (win
->quirks
& SWM_Q_ANYWHERE
)
9079 new_flags
|= SWM_F_MANUAL
;
9081 ewmh_apply_flags(win
, new_flags
);
9082 ewmh_update_wm_state(win
);
9084 /* Set initial _NET_WM_ALLOWED_ACTIONS */
9085 ewmh_update_actions(win
);
9089 DNPRINTF(SWM_D_MISC
, "manage_window: done. win %#x, (x,y) w x h: "
9090 "(%d,%d) %d x %d, ws: %d, iconic: %s, transient: %#x\n", win
->id
,
9091 X(win
), Y(win
), WIDTH(win
), HEIGHT(win
), win
->ws
->idx
,
9092 YESNO(ICONIC(win
)), win
->transient
);
9098 free_window(struct ws_win
*win
)
9100 DNPRINTF(SWM_D_MISC
, "free_window: win %#x\n", win
->id
);
9105 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
9107 xcb_icccm_get_wm_class_reply_wipe(&win
->ch
);
9112 memset(win
, 0xff, sizeof *win
); /* XXX kill later */
9115 DNPRINTF(SWM_D_MISC
, "free_window: done.\n");
9119 unmanage_window(struct ws_win
*win
)
9121 struct ws_win
*parent
;
9126 DNPRINTF(SWM_D_MISC
, "unmanage_window: win %#x\n", win
->id
);
9129 parent
= find_window(win
->transient
);
9131 parent
->focus_child
= NULL
;
9134 TAILQ_REMOVE(&win
->ws
->stack
, win
, stack_entry
);
9135 TAILQ_REMOVE(&win
->ws
->winlist
, win
, entry
);
9136 TAILQ_INSERT_TAIL(&win
->ws
->unmanagedlist
, win
, entry
);
9138 ewmh_update_client_list();
9142 expose(xcb_expose_event_t
*e
)
9145 struct swm_region
*r
;
9147 DNPRINTF(SWM_D_EVENT
, "expose: win %#x\n", e
->window
);
9149 num_screens
= get_screen_count();
9150 for (i
= 0; i
< num_screens
; i
++)
9151 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
9152 if (e
->window
== WINID(r
->bar
))
9160 focusin(xcb_focus_in_event_t
*e
)
9162 DNPRINTF(SWM_D_EVENT
, "focusin: win %#x, mode: %s(%u), "
9163 "detail: %s(%u)\n", e
->event
, get_notify_mode_label(e
->mode
),
9164 e
->mode
, get_notify_detail_label(e
->detail
), e
->detail
);
9168 focusout(xcb_focus_out_event_t
*e
)
9170 DNPRINTF(SWM_D_EVENT
, "focusout: win %#x, mode: %s(%u), "
9171 "detail: %s(%u)\n", e
->event
, get_notify_mode_label(e
->mode
),
9172 e
->mode
, get_notify_detail_label(e
->detail
), e
->detail
);
9177 keypress(xcb_key_press_event_t
*e
)
9179 xcb_keysym_t keysym
;
9182 keysym
= xcb_key_press_lookup_keysym(syms
, e
, 0);
9184 DNPRINTF(SWM_D_EVENT
, "keypress: keysym: %u, win (x,y): %#x (%d,%d), "
9185 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
9186 "state: %u, same_screen: %s\n", keysym
, e
->event
, e
->event_x
,
9187 e
->event_y
, e
->detail
, e
->time
, e
->root
, e
->root_x
, e
->root_y
,
9188 e
->child
, e
->state
, YESNO(e
->same_screen
));
9190 if ((kp
= key_lookup(CLEANMASK(e
->state
), keysym
)) == NULL
)
9193 last_event_time
= e
->time
;
9195 if (kp
->funcid
== KF_SPAWN_CUSTOM
)
9196 spawn_custom(root_to_region(e
->root
, SWM_CK_ALL
),
9197 &(keyfuncs
[kp
->funcid
].args
), kp
->spawn_name
);
9198 else if (keyfuncs
[kp
->funcid
].func
)
9199 keyfuncs
[kp
->funcid
].func(root_to_region(e
->root
, SWM_CK_ALL
),
9200 &(keyfuncs
[kp
->funcid
].args
));
9203 /* Unfreeze grab events. */
9204 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
, e
->time
);
9207 DNPRINTF(SWM_D_EVENT
, "keypress: done.\n");
9211 buttonpress(xcb_button_press_event_t
*e
)
9213 struct ws_win
*win
= NULL
;
9214 struct swm_region
*r
, *old_r
;
9216 bool handled
= false;
9218 DNPRINTF(SWM_D_EVENT
, "buttonpress: win (x,y): %#x (%d,%d), "
9219 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
9220 "state: %u, same_screen: %s\n", e
->event
, e
->event_x
, e
->event_y
,
9221 e
->detail
, e
->time
, e
->root
, e
->root_x
, e
->root_y
, e
->child
,
9222 e
->state
, YESNO(e
->same_screen
));
9224 if (e
->event
== e
->root
) {
9225 if (e
->child
!= 0) {
9226 win
= find_window(e
->child
);
9227 /* Pass ButtonPress to window if it isn't managed. */
9231 /* Focus on empty region */
9232 /* If no windows on region if its empty. */
9233 r
= root_to_region(e
->root
, SWM_CK_POINTER
);
9235 DNPRINTF(SWM_D_EVENT
, "buttonpress: "
9236 "NULL region; ignoring.\n");
9240 if (TAILQ_EMPTY(&r
->ws
->winlist
)) {
9241 old_r
= root_to_region(e
->root
, SWM_CK_FOCUS
);
9242 if (old_r
&& old_r
!= r
)
9243 unfocus_win(old_r
->ws
->focus
);
9245 xcb_set_input_focus(conn
,
9246 XCB_INPUT_FOCUS_PARENT
, e
->root
, e
->time
);
9248 /* Clear bar since empty. */
9256 win
= find_window(e
->event
);
9262 last_event_time
= e
->time
;
9264 focus_win(get_focus_magic(win
));
9266 for (i
= 0; i
< LENGTH(buttons
); i
++)
9267 if (client_click
== buttons
[i
].action
&& buttons
[i
].func
&&
9268 buttons
[i
].button
== e
->detail
&&
9269 CLEANMASK(buttons
[i
].mask
) == CLEANMASK(e
->state
)) {
9270 buttons
[i
].func(win
, &buttons
[i
].args
);
9276 DNPRINTF(SWM_D_EVENT
, "buttonpress: passing to window.\n");
9277 /* Replay event to event window */
9278 xcb_allow_events(conn
, XCB_ALLOW_REPLAY_POINTER
, e
->time
);
9280 DNPRINTF(SWM_D_EVENT
, "buttonpress: handled.\n");
9281 /* Unfreeze grab events. */
9282 xcb_allow_events(conn
, XCB_ALLOW_SYNC_POINTER
, e
->time
);
9290 print_win_geom(xcb_window_t w
)
9292 xcb_get_geometry_reply_t
*wa
;
9294 wa
= xcb_get_geometry_reply(conn
, xcb_get_geometry(conn
, w
), NULL
);
9296 DNPRINTF(SWM_D_MISC
, "print_win_geom: window not found: %#x\n",
9301 DNPRINTF(SWM_D_MISC
, "print_win_geom: win %#x, root: %#x, "
9302 "depth: %u, (x,y) w x h: (%d,%d) %d x %d, border: %d\n",
9303 w
, wa
->root
, wa
->depth
, wa
->x
, wa
->y
, wa
->width
, wa
->height
,
9312 get_stack_mode_name(uint8_t mode
)
9317 case XCB_STACK_MODE_ABOVE
:
9320 case XCB_STACK_MODE_BELOW
:
9323 case XCB_STACK_MODE_TOP_IF
:
9326 case XCB_STACK_MODE_BOTTOM_IF
:
9329 case XCB_STACK_MODE_OPPOSITE
:
9341 configurerequest(xcb_configure_request_event_t
*e
)
9344 struct swm_region
*r
= NULL
;
9346 uint32_t wc
[7] = {0};
9350 if ((win
= find_window(e
->window
)) == NULL
)
9351 if ((win
= find_unmanaged_window(e
->window
)) == NULL
)
9355 if (swm_debug
& SWM_D_EVENT
) {
9356 print_win_geom(e
->window
);
9358 DNPRINTF(SWM_D_EVENT
, "configurerequest: win %#x, "
9359 "parent: %#x, new: %s, value_mask: %u { ", e
->window
,
9360 e
->parent
, YESNO(new), e
->value_mask
);
9361 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
)
9362 DPRINTF("X: %d ", e
->x
);
9363 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
)
9364 DPRINTF("Y: %d ", e
->y
);
9365 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
)
9366 DPRINTF("W: %u ", e
->width
);
9367 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
)
9368 DPRINTF("H: %u ", e
->height
);
9369 if (e
->value_mask
& XCB_CONFIG_WINDOW_BORDER_WIDTH
)
9370 DPRINTF("Border: %u ", e
->border_width
);
9371 if (e
->value_mask
& XCB_CONFIG_WINDOW_SIBLING
)
9372 DPRINTF("Sibling: %#x ", e
->sibling
);
9373 if (e
->value_mask
& XCB_CONFIG_WINDOW_STACK_MODE
)
9374 DPRINTF("StackMode: %s(%u) ",
9375 get_stack_mode_name(e
->stack_mode
), e
->stack_mode
);
9381 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
) {
9382 mask
|= XCB_CONFIG_WINDOW_X
;
9385 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
) {
9386 mask
|= XCB_CONFIG_WINDOW_Y
;
9389 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
) {
9390 mask
|= XCB_CONFIG_WINDOW_WIDTH
;
9393 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
) {
9394 mask
|= XCB_CONFIG_WINDOW_HEIGHT
;
9395 wc
[i
++] = e
->height
;
9397 if (e
->value_mask
& XCB_CONFIG_WINDOW_BORDER_WIDTH
) {
9398 mask
|= XCB_CONFIG_WINDOW_BORDER_WIDTH
;
9399 wc
[i
++] = e
->border_width
;
9401 if (e
->value_mask
& XCB_CONFIG_WINDOW_SIBLING
) {
9402 mask
|= XCB_CONFIG_WINDOW_SIBLING
;
9403 wc
[i
++] = e
->sibling
;
9405 if (e
->value_mask
& XCB_CONFIG_WINDOW_STACK_MODE
) {
9406 mask
|= XCB_CONFIG_WINDOW_STACK_MODE
;
9407 wc
[i
++] = e
->stack_mode
;
9411 xcb_configure_window(conn
, e
->window
, mask
, wc
);
9414 } else if ((!MANUAL(win
) || win
->quirks
& SWM_Q_ANYWHERE
) &&
9415 !FULLSCREEN(win
) && !MAXIMIZED(win
)) {
9418 else if (win
->ws
->old_r
)
9421 /* windows are centered unless ANYWHERE quirk is set. */
9422 if (win
->quirks
& SWM_Q_ANYWHERE
) {
9423 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
) {
9424 win
->g_float
.x
= e
->x
;
9426 win
->g_float
.x
-= X(r
);
9429 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
) {
9430 win
->g_float
.y
= e
->y
;
9432 win
->g_float
.y
-= Y(r
);
9436 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
)
9437 win
->g_float
.w
= e
->width
;
9439 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
)
9440 win
->g_float
.h
= e
->height
;
9442 win
->g_floatvalid
= true;
9444 if (!MAXIMIZED(win
) && !FULLSCREEN(win
) &&
9445 (TRANS(win
) || (ABOVE(win
) &&
9446 win
->ws
->cur_layout
!= &layouts
[SWM_MAX_STACK
]))) {
9447 WIDTH(win
) = win
->g_float
.w
;
9448 HEIGHT(win
) = win
->g_float
.h
;
9451 update_floater(win
);
9466 DNPRINTF(SWM_D_EVENT
, "configurerequest: done.\n");
9470 configurenotify(xcb_configure_notify_event_t
*e
)
9474 DNPRINTF(SWM_D_EVENT
, "configurenotify: win %#x, event win: %#x, "
9475 "(x,y) WxH: (%d,%d) %ux%u, border: %u, above_sibling: %#x, "
9476 "override_redirect: %s\n", e
->window
, e
->event
, e
->x
, e
->y
,
9477 e
->width
, e
->height
, e
->border_width
, e
->above_sibling
,
9478 YESNO(e
->override_redirect
));
9480 win
= find_window(e
->window
);
9482 xcb_icccm_get_wm_normal_hints_reply(conn
,
9483 xcb_icccm_get_wm_normal_hints(conn
, win
->id
),
9486 if (font_adjusted
) {
9494 destroynotify(xcb_destroy_notify_event_t
*e
)
9498 DNPRINTF(SWM_D_EVENT
, "destroynotify: win %#x\n", e
->window
);
9500 if ((win
= find_window(e
->window
)) == NULL
) {
9501 if ((win
= find_unmanaged_window(e
->window
)) == NULL
)
9507 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
9508 /* If we were focused, make sure we focus on something else. */
9509 if (win
== win
->ws
->focus
)
9510 win
->ws
->focus_pending
= get_focus_prev(win
);
9513 unmanage_window(win
);
9516 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& WS_FOCUSED(win
->ws
)) {
9517 if (win
->ws
->focus_pending
) {
9518 focus_win(win
->ws
->focus_pending
);
9519 win
->ws
->focus_pending
= NULL
;
9520 } else if (win
== win
->ws
->focus
) {
9521 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
,
9522 win
->ws
->r
->id
, XCB_CURRENT_TIME
);
9533 get_notify_detail_label(uint8_t detail
)
9538 case XCB_NOTIFY_DETAIL_ANCESTOR
:
9541 case XCB_NOTIFY_DETAIL_VIRTUAL
:
9544 case XCB_NOTIFY_DETAIL_INFERIOR
:
9547 case XCB_NOTIFY_DETAIL_NONLINEAR
:
9548 label
= "Nonlinear";
9550 case XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL
:
9551 label
= "NonlinearVirtual";
9553 case XCB_NOTIFY_DETAIL_POINTER
:
9556 case XCB_NOTIFY_DETAIL_POINTER_ROOT
:
9557 label
= "PointerRoot";
9559 case XCB_NOTIFY_DETAIL_NONE
:
9570 get_notify_mode_label(uint8_t mode
)
9575 case XCB_NOTIFY_MODE_NORMAL
:
9578 case XCB_NOTIFY_MODE_GRAB
:
9581 case XCB_NOTIFY_MODE_UNGRAB
:
9584 case XCB_NOTIFY_MODE_WHILE_GRABBED
:
9585 label
= "WhileGrabbed";
9596 enternotify(xcb_enter_notify_event_t
*e
)
9599 struct swm_region
*r
;
9601 DNPRINTF(SWM_D_FOCUS
, "enternotify: time: %u, win (x,y): %#x "
9602 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
9603 "child: %#x, same_screen_focus: %s, state: %d\n",
9604 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
9605 get_notify_mode_label(e
->mode
), e
->mode
,
9606 get_notify_detail_label(e
->detail
), e
->detail
,
9607 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
9608 YESNO(e
->same_screen_focus
), e
->state
);
9610 if (focus_mode
== SWM_FOCUS_MANUAL
&&
9611 e
->mode
== XCB_NOTIFY_MODE_NORMAL
) {
9612 DNPRINTF(SWM_D_EVENT
, "enternotify: manual focus; ignoring.\n");
9616 last_event_time
= e
->time
;
9618 if ((win
= find_window(e
->event
)) == NULL
) {
9619 if (e
->event
== e
->root
) {
9620 /* If no windows on pointer region, then focus root. */
9621 r
= root_to_region(e
->root
, SWM_CK_POINTER
);
9623 DNPRINTF(SWM_D_EVENT
, "enternotify: "
9624 "NULL region; ignoring.\n");
9630 DNPRINTF(SWM_D_EVENT
, "enternotify: window is NULL; "
9635 if (e
->mode
== XCB_NOTIFY_MODE_NORMAL
&&
9636 e
->detail
== XCB_NOTIFY_DETAIL_INFERIOR
) {
9637 DNPRINTF(SWM_D_EVENT
, "enternotify: entering from "
9638 "inferior; ignoring\n");
9642 focus_win(get_focus_magic(win
));
9650 leavenotify(xcb_leave_notify_event_t
*e
)
9652 DNPRINTF(SWM_D_FOCUS
, "leavenotify: time: %u, win (x,y): %#x "
9653 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
9654 "child: %#x, same_screen_focus: %s, state: %d\n",
9655 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
9656 get_notify_mode_label(e
->mode
), e
->mode
,
9657 get_notify_detail_label(e
->detail
), e
->detail
,
9658 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
9659 YESNO(e
->same_screen_focus
), e
->state
);
9664 mapnotify(xcb_map_notify_event_t
*e
)
9666 struct ws_win
*win
, *parent
= NULL
;
9667 struct workspace
*ws
;
9669 DNPRINTF(SWM_D_EVENT
, "mapnotify: win %#x\n", e
->window
);
9671 if ((win
= manage_window(e
->window
, spawn_position
, true)) == NULL
)
9675 /* Need to know if win was mapped due to ws switch. */
9676 if (ws
->state
== SWM_WS_STATE_MAPPED
) {
9677 if (ws
->focus_pending
&& TRANS(ws
->focus_pending
))
9678 parent
= find_window(win
->transient
);
9680 /* If window's parent is maximized, don't clear it. */
9681 if ((parent
== NULL
) || !MAXIMIZED(parent
))
9682 if (clear_maximized(ws
) > 0)
9687 set_win_state(win
, XCB_ICCCM_WM_STATE_NORMAL
);
9689 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& WS_FOCUSED(win
->ws
)) {
9690 if (ws
->focus_pending
== win
) {
9692 ws
->focus_pending
= NULL
;
9693 center_pointer(win
->ws
->r
);
9702 mappingnotify(xcb_mapping_notify_event_t
*e
)
9705 int i
, j
, num_screens
;
9707 xcb_refresh_keyboard_mapping(syms
, e
);
9709 if (e
->request
== XCB_MAPPING_KEYBOARD
) {
9712 /* Regrab buttons on all managed windows. */
9713 num_screens
= get_screen_count();
9714 for (i
= 0; i
< num_screens
; i
++)
9715 for (j
= 0; j
< workspace_limit
; j
++)
9716 TAILQ_FOREACH(w
, &screens
[i
].ws
[j
].winlist
,
9723 maprequest(xcb_map_request_event_t
*e
)
9725 struct ws_win
*win
, *w
= NULL
;
9726 xcb_get_window_attributes_reply_t
*war
;
9728 DNPRINTF(SWM_D_EVENT
, "maprequest: win %#x\n",
9731 war
= xcb_get_window_attributes_reply(conn
,
9732 xcb_get_window_attributes(conn
, e
->window
),
9735 DNPRINTF(SWM_D_EVENT
, "maprequest: window lost.\n");
9739 if (war
->override_redirect
) {
9740 DNPRINTF(SWM_D_EVENT
, "maprequest: override_redirect; "
9745 win
= manage_window(e
->window
, spawn_position
,
9746 (war
->map_state
== XCB_MAP_STATE_VIEWABLE
));
9750 /* The new window should get focus; prepare. */
9751 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
9752 !(win
->quirks
& SWM_Q_NOFOCUSONMAP
) &&
9753 (!(win
->hints
.flags
& XCB_ICCCM_WM_HINT_INPUT
) ||
9754 (win
->hints
.flags
& XCB_ICCCM_WM_HINT_INPUT
&&
9755 win
->hints
.input
))) {
9756 if (win
->quirks
& SWM_Q_FOCUSONMAP_SINGLE
) {
9757 /* See if other wins of same type are already mapped. */
9758 TAILQ_FOREACH(w
, &win
->ws
->winlist
, entry
) {
9759 if (w
== win
|| !w
->mapped
)
9762 if (w
->ch
.class_name
&&
9763 win
->ch
.class_name
&&
9764 strcmp(w
->ch
.class_name
,
9765 win
->ch
.class_name
) == 0 &&
9766 w
->ch
.instance_name
&&
9767 win
->ch
.instance_name
&&
9768 strcmp(w
->ch
.instance_name
,
9769 win
->ch
.instance_name
) == 0)
9775 win
->ws
->focus_pending
= get_focus_magic(win
);
9778 /* All windows need to be mapped if they are in the current workspace.*/
9782 /* Ignore EnterNotify to handle the mapnotify without interference. */
9783 if (focus_mode
== SWM_FOCUS_DEFAULT
)
9784 event_drain(XCB_ENTER_NOTIFY
);
9787 DNPRINTF(SWM_D_EVENT
, "maprequest: done.\n");
9791 motionnotify(xcb_motion_notify_event_t
*e
)
9793 struct swm_region
*r
= NULL
;
9796 DNPRINTF(SWM_D_FOCUS
, "motionnotify: time: %u, win (x,y): %#x "
9797 "(%d,%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
9798 "child: %#x, same_screen_focus: %s, state: %d\n",
9799 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
9800 get_notify_detail_label(e
->detail
), e
->detail
,
9801 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
9802 YESNO(e
->same_screen
), e
->state
);
9804 last_event_time
= e
->time
;
9806 if (focus_mode
== SWM_FOCUS_MANUAL
)
9809 num_screens
= get_screen_count();
9810 for (i
= 0; i
< num_screens
; i
++)
9811 if (screens
[i
].root
== e
->root
)
9814 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
9815 if (X(r
) <= e
->root_x
&& e
->root_x
< MAX_X(r
) &&
9816 Y(r
) <= e
->root_y
&& e
->root_y
< MAX_Y(r
))
9824 get_atom_name(xcb_atom_t atom
)
9827 #ifdef SWM_DEBUG_ATOM_NAMES
9829 * This should be disabled during most debugging since
9830 * xcb_get_* causes an xcb_flush.
9833 xcb_get_atom_name_reply_t
*r
;
9835 r
= xcb_get_atom_name_reply(conn
,
9836 xcb_get_atom_name(conn
, atom
),
9839 len
= xcb_get_atom_name_name_length(r
);
9841 name
= malloc(len
+ 1);
9843 memcpy(name
, xcb_get_atom_name_name(r
), len
);
9857 propertynotify(xcb_property_notify_event_t
*e
)
9860 struct workspace
*ws
;
9864 name
= get_atom_name(e
->atom
);
9865 DNPRINTF(SWM_D_EVENT
, "propertynotify: win %#x, atom: %s(%u), "
9866 "time: %#x, state: %u\n", e
->window
, name
, e
->atom
, e
->time
,
9870 win
= find_window(e
->window
);
9876 last_event_time
= e
->time
;
9878 if (e
->atom
== a_state
) {
9879 /* State just changed, make sure it gets focused if mapped. */
9880 if (e
->state
== XCB_PROPERTY_NEW_VALUE
) {
9881 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& WS_FOCUSED(ws
)) {
9883 ws
->focus_pending
== win
) {
9884 focus_win(ws
->focus_pending
);
9885 ws
->focus_pending
= NULL
;
9889 } else if (e
->atom
== XCB_ATOM_WM_CLASS
||
9890 e
->atom
== XCB_ATOM_WM_NAME
) {
9892 } else if (e
->atom
== a_prot
) {
9893 get_wm_protocols(win
);
9900 unmapnotify(xcb_unmap_notify_event_t
*e
)
9903 struct workspace
*ws
;
9905 DNPRINTF(SWM_D_EVENT
, "unmapnotify: win %#x\n", e
->window
);
9907 /* If we aren't managing the window, then ignore. */
9908 win
= find_window(e
->window
);
9909 if (win
== NULL
|| win
->id
!= e
->window
)
9912 /* Do nothing if already withdrawn. */
9913 if (!win
->mapped
&& !ICONIC(win
))
9917 win
->mapped
= false;
9919 /* If win was focused, make sure to focus on something else. */
9920 if (win
== ws
->focus
) {
9921 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
9922 ws
->focus_pending
= get_focus_prev(win
);
9923 DNPRINTF(SWM_D_EVENT
, "unmapnotify: "
9924 "focus_pending: %#x\n",
9925 WINID(ws
->focus_pending
));
9933 set_win_state(win
, XCB_ICCCM_WM_STATE_ICONIC
);
9936 set_win_state(win
, XCB_ICCCM_WM_STATE_WITHDRAWN
);
9937 unmanage_window(win
);
9943 /* Update focus if ws is active. */
9944 if (WS_FOCUSED(ws
)) {
9945 if (focus_mode
== SWM_FOCUS_FOLLOW
) {
9946 focus_win(get_pointer_win(ws
->r
->s
->root
));
9947 } else if (ws
->focus_pending
) {
9948 focus_win(ws
->focus_pending
);
9949 ws
->focus_pending
= NULL
;
9950 } else if (ws
->focus
== NULL
) {
9951 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
,
9952 ws
->r
->id
, XCB_CURRENT_TIME
);
9956 center_pointer(ws
->r
);
9962 get_source_type_label(uint32_t type
)
9967 case EWMH_SOURCE_TYPE_NONE
:
9970 case EWMH_SOURCE_TYPE_NORMAL
:
9973 case EWMH_SOURCE_TYPE_OTHER
:
9985 clientmessage(xcb_client_message_event_t
*e
)
9988 struct swm_region
*r
= NULL
;
9992 xcb_map_request_event_t mre
;
9996 name
= get_atom_name(e
->type
);
9997 DNPRINTF(SWM_D_EVENT
, "clientmessage: win %#x, atom: %s(%u)\n",
9998 e
->window
, name
, e
->type
);
10002 if (e
->type
== ewmh
[_NET_CURRENT_DESKTOP
].atom
) {
10003 num_screens
= get_screen_count();
10004 for (i
= 0; i
< num_screens
; i
++)
10005 if (screens
[i
].root
== e
->window
) {
10006 r
= screens
[i
].r_focus
;
10010 if (r
&& e
->data
.data32
[0] < (uint32_t)workspace_limit
) {
10011 a
.id
= e
->data
.data32
[0];
10019 win
= find_window(e
->window
);
10021 if (e
->type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
10022 /* Manage the window with maprequest. */
10023 DNPRINTF(SWM_D_EVENT
, "clientmessage: request focus on "
10024 "unmanaged window.\n");
10025 mre
.window
= e
->window
;
10031 if (e
->type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
10032 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_ACTIVE_WINDOW, "
10033 "source_type: %s(%d)\n",
10034 get_source_type_label(e
->data
.data32
[0]),
10035 e
->data
.data32
[0]);
10038 * Allow focus changes that are a result of direct user
10039 * action and from applications that use the old EWMH spec.
10041 if (e
->data
.data32
[0] != EWMH_SOURCE_TYPE_NORMAL
||
10042 win
->quirks
& SWM_Q_OBEYAPPFOCUSREQ
) {
10043 if (WS_FOCUSED(win
->ws
))
10046 win
->ws
->focus_pending
= win
;
10048 } else if (e
->type
== ewmh
[_NET_CLOSE_WINDOW
].atom
) {
10049 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_CLOSE_WINDOW\n");
10050 if (win
->can_delete
)
10051 client_msg(win
, a_delete
, 0);
10053 xcb_kill_client(conn
, win
->id
);
10054 } else if (e
->type
== ewmh
[_NET_MOVERESIZE_WINDOW
].atom
) {
10055 DNPRINTF(SWM_D_EVENT
,
10056 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
10058 if (e
->data
.data32
[0] & (1<<8)) /* x */
10059 X(win
) = e
->data
.data32
[1];
10060 if (e
->data
.data32
[0] & (1<<9)) /* y */
10061 Y(win
) = e
->data
.data32
[2];
10062 if (e
->data
.data32
[0] & (1<<10)) /* width */
10063 WIDTH(win
) = e
->data
.data32
[3];
10064 if (e
->data
.data32
[0] & (1<<11)) /* height */
10065 HEIGHT(win
) = e
->data
.data32
[4];
10067 update_window(win
);
10069 /* Notify no change was made. */
10070 config_win(win
, NULL
);
10071 /* TODO: Change stack sizes */
10073 } else if (e
->type
== ewmh
[_NET_RESTACK_WINDOW
].atom
) {
10074 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_RESTACK_WINDOW\n");
10075 val
[0] = e
->data
.data32
[1]; /* Sibling window. */
10076 val
[1] = e
->data
.data32
[2]; /* Stack mode detail. */
10078 xcb_configure_window(conn
, win
->id
, XCB_CONFIG_WINDOW_SIBLING
|
10079 XCB_CONFIG_WINDOW_STACK_MODE
, val
);
10080 } else if (e
->type
== ewmh
[_NET_WM_STATE
].atom
) {
10081 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_WM_STATE\n");
10082 ewmh_change_wm_state(win
, e
->data
.data32
[1], e
->data
.data32
[0]);
10083 if (e
->data
.data32
[2])
10084 ewmh_change_wm_state(win
, e
->data
.data32
[2],
10085 e
->data
.data32
[0]);
10087 ewmh_update_wm_state(win
);
10089 } else if (e
->type
== ewmh
[_NET_WM_DESKTOP
].atom
) {
10090 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_WM_DESKTOP\n");
10093 win_to_ws(win
, e
->data
.data32
[0], true);
10095 /* Restack if either the source or destination ws is mapped. */
10096 if (r
!= NULL
|| win
->ws
->r
!= NULL
) {
10098 load_float_geom(win
);
10110 int errcode
= xcb_connection_has_error(conn
);
10111 #ifdef XCB_CONN_ERROR
10114 case XCB_CONN_ERROR
:
10115 s
= "Socket error, pipe error or other stream error.";
10117 case XCB_CONN_CLOSED_EXT_NOTSUPPORTED
:
10118 s
= "Extension not supported.";
10120 case XCB_CONN_CLOSED_MEM_INSUFFICIENT
:
10121 s
= "Insufficient memory.";
10123 case XCB_CONN_CLOSED_REQ_LEN_EXCEED
:
10124 s
= "Request length was exceeded.";
10126 case XCB_CONN_CLOSED_PARSE_ERR
:
10127 s
= "Error parsing display string.";
10130 s
= "Unknown error.";
10133 errx(errcode
, "X CONNECTION ERROR: %s", s
);
10136 errx(errcode
, "X CONNECTION ERROR");
10143 int num_screens
, i
;
10144 const uint32_t val
= XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT
|
10145 XCB_EVENT_MASK_ENTER_WINDOW
;
10147 xcb_void_cookie_t wac
;
10148 xcb_generic_error_t
*error
;
10150 /* this causes an error if some other window manager is running */
10151 num_screens
= get_screen_count();
10152 for (i
= 0; i
< num_screens
; i
++) {
10153 if ((sc
= get_screen(i
)) == NULL
)
10154 errx(1, "ERROR: can't get screen %d.", i
);
10155 DNPRINTF(SWM_D_INIT
, "enable_wm: screen %d, root: %#x\n",
10157 wac
= xcb_change_window_attributes_checked(conn
, sc
->root
,
10158 XCB_CW_EVENT_MASK
, &val
);
10159 if ((error
= xcb_request_check(conn
, wac
))) {
10160 DNPRINTF(SWM_D_INIT
, "enable_wm: error_code: %u\n",
10161 error
->error_code
);
10166 /* click to focus on empty region */
10167 xcb_grab_button(conn
, 1, sc
->root
, BUTTONMASK
,
10168 XCB_GRAB_MODE_SYNC
, XCB_GRAB_MODE_ASYNC
, XCB_WINDOW_NONE
,
10169 XCB_CURSOR_NONE
, XCB_BUTTON_INDEX_1
, XCB_BUTTON_MASK_ANY
);
10176 new_region(struct swm_screen
*s
, int x
, int y
, int w
, int h
)
10178 struct swm_region
*r
= NULL
, *n
;
10179 struct workspace
*ws
= NULL
;
10183 DNPRINTF(SWM_D_MISC
, "new region: screen[%d]:%dx%d+%d+%d\n",
10184 s
->idx
, w
, h
, x
, y
);
10186 /* remove any conflicting regions */
10187 n
= TAILQ_FIRST(&s
->rl
);
10190 n
= TAILQ_NEXT(r
, entry
);
10191 if (X(r
) < (x
+ w
) && (X(r
) + WIDTH(r
)) > x
&&
10192 Y(r
) < (y
+ h
) && (Y(r
) + HEIGHT(r
)) > y
) {
10193 if (r
->ws
->r
!= NULL
)
10194 r
->ws
->old_r
= r
->ws
->r
;
10197 xcb_destroy_window(conn
, r
->id
);
10198 TAILQ_REMOVE(&s
->rl
, r
, entry
);
10199 TAILQ_INSERT_TAIL(&s
->orl
, r
, entry
);
10203 /* search old regions for one to reuse */
10205 /* size + location match */
10206 TAILQ_FOREACH(r
, &s
->orl
, entry
)
10207 if (X(r
) == x
&& Y(r
) == y
&&
10208 HEIGHT(r
) == h
&& WIDTH(r
) == w
)
10212 TAILQ_FOREACH(r
, &s
->orl
, entry
)
10213 if (HEIGHT(r
) == h
&& WIDTH(r
) == w
)
10217 TAILQ_REMOVE(&s
->orl
, r
, entry
);
10218 /* try to use old region's workspace */
10219 if (r
->ws
->r
== NULL
)
10222 if ((r
= calloc(1, sizeof(struct swm_region
))) == NULL
)
10223 err(1, "new_region: calloc: failed to allocate memory "
10226 /* if we don't have a workspace already, find one */
10228 for (i
= 0; i
< workspace_limit
; i
++)
10229 if (s
->ws
[i
].r
== NULL
) {
10236 errx(1, "new_region: no free workspaces");
10238 if (ws
->state
== SWM_WS_STATE_HIDDEN
)
10239 ws
->state
= SWM_WS_STATE_MAPPING
;
10247 r
->ws_prior
= NULL
;
10250 TAILQ_INSERT_TAIL(&s
->rl
, r
, entry
);
10252 /* Invisible region window to detect pointer events on empty regions. */
10253 r
->id
= xcb_generate_id(conn
);
10254 wa
[0] = XCB_EVENT_MASK_POINTER_MOTION
|
10255 XCB_EVENT_MASK_POINTER_MOTION_HINT
;
10257 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, r
->id
, r
->s
->root
,
10258 X(r
), Y(r
), WIDTH(r
), HEIGHT(r
), 0, XCB_WINDOW_CLASS_INPUT_ONLY
,
10259 XCB_COPY_FROM_PARENT
, XCB_CW_EVENT_MASK
, wa
);
10261 /* Make sure region input is at the bottom. */
10262 wa
[0] = XCB_STACK_MODE_BELOW
;
10263 xcb_configure_window(conn
, r
->id
, XCB_CONFIG_WINDOW_STACK_MODE
, wa
);
10265 xcb_map_window(conn
, r
->id
);
10269 scan_randr(int idx
)
10271 #ifdef SWM_XRR_HAS_CRTC
10274 #endif /* SWM_XRR_HAS_CRTC */
10275 struct swm_region
*r
;
10276 struct ws_win
*win
;
10278 xcb_randr_get_screen_resources_current_cookie_t src
;
10279 xcb_randr_get_screen_resources_current_reply_t
*srr
;
10280 xcb_randr_get_crtc_info_cookie_t cic
;
10281 xcb_randr_get_crtc_info_reply_t
*cir
= NULL
;
10282 xcb_randr_crtc_t
*crtc
;
10283 xcb_screen_t
*screen
;
10285 DNPRINTF(SWM_D_MISC
, "scan_randr: screen: %d\n", idx
);
10287 if ((screen
= get_screen(idx
)) == NULL
)
10288 errx(1, "ERROR: can't get screen %d.", idx
);
10290 num_screens
= get_screen_count();
10291 if (idx
>= num_screens
)
10292 errx(1, "scan_randr: invalid screen");
10294 /* remove any old regions */
10295 while ((r
= TAILQ_FIRST(&screens
[idx
].rl
)) != NULL
) {
10296 r
->ws
->old_r
= r
->ws
->r
= NULL
;
10298 xcb_destroy_window(conn
, r
->id
);
10299 TAILQ_REMOVE(&screens
[idx
].rl
, r
, entry
);
10300 TAILQ_INSERT_TAIL(&screens
[idx
].orl
, r
, entry
);
10304 /* map virtual screens onto physical screens */
10305 #ifdef SWM_XRR_HAS_CRTC
10306 if (randr_support
) {
10307 src
= xcb_randr_get_screen_resources_current(conn
,
10308 screens
[idx
].root
);
10309 srr
= xcb_randr_get_screen_resources_current_reply(conn
, src
,
10312 new_region(&screens
[idx
], 0, 0,
10313 screen
->width_in_pixels
,
10314 screen
->height_in_pixels
);
10317 ncrtc
= srr
->num_crtcs
;
10319 crtc
= xcb_randr_get_screen_resources_current_crtcs(srr
);
10320 for (c
= 0; c
< ncrtc
; c
++) {
10321 cic
= xcb_randr_get_crtc_info(conn
, crtc
[c
],
10323 cir
= xcb_randr_get_crtc_info_reply(conn
, cic
, NULL
);
10326 if (cir
->num_outputs
== 0) {
10331 if (cir
->mode
== 0)
10332 new_region(&screens
[idx
], 0, 0,
10333 screen
->width_in_pixels
,
10334 screen
->height_in_pixels
);
10336 new_region(&screens
[idx
],
10337 cir
->x
, cir
->y
, cir
->width
, cir
->height
);
10342 #endif /* SWM_XRR_HAS_CRTC */
10344 /* If detection failed, create a single region that spans the screen. */
10345 if (TAILQ_EMPTY(&screens
[idx
].rl
))
10346 new_region(&screens
[idx
], 0, 0, screen
->width_in_pixels
,
10347 screen
->height_in_pixels
);
10350 /* Cleanup unused previously visible workspaces. */
10351 TAILQ_FOREACH(r
, &screens
[idx
].orl
, entry
) {
10352 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
)
10354 r
->ws
->state
= SWM_WS_STATE_HIDDEN
;
10356 /* The screen shouldn't focus on an unused region. */
10357 if (screens
[idx
].r_focus
== r
)
10358 screens
[idx
].r_focus
= NULL
;
10361 DNPRINTF(SWM_D_MISC
, "scan_randr: done.\n");
10365 screenchange(xcb_randr_screen_change_notify_event_t
*e
)
10367 struct swm_region
*r
;
10368 int i
, num_screens
;
10370 DNPRINTF(SWM_D_EVENT
, "screenchange: root: %#x\n", e
->root
);
10372 num_screens
= get_screen_count();
10373 /* silly event doesn't include the screen index */
10374 for (i
= 0; i
< num_screens
; i
++)
10375 if (screens
[i
].root
== e
->root
)
10377 if (i
>= num_screens
)
10378 errx(1, "screenchange: screen not found");
10380 /* brute force for now, just re-enumerate the regions */
10384 print_win_geom(e
->root
);
10386 /* add bars to all regions */
10387 for (i
= 0; i
< num_screens
; i
++) {
10388 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
10394 /* Make sure a region has focus on each screen. */
10395 for (i
= 0; i
< num_screens
; i
++) {
10396 if (screens
[i
].r_focus
== NULL
) {
10397 r
= TAILQ_FIRST(&screens
[i
].rl
);
10406 /* Update workspace state on all regions. */
10407 for (i
= 0; i
< num_screens
; i
++)
10408 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
10409 r
->ws
->state
= SWM_WS_STATE_MAPPED
;
10415 struct swm_region
*r
= NULL
;
10416 xcb_window_t
*wins
= NULL
, trans
, *cwins
= NULL
;
10417 int i
, j
, k
, n
, no
, num_screens
;
10419 bool manage
, mapped
;
10421 xcb_query_tree_cookie_t qtc
;
10422 xcb_query_tree_reply_t
*qtr
;
10423 xcb_get_window_attributes_cookie_t gac
;
10424 xcb_get_window_attributes_reply_t
*gar
;
10425 xcb_get_property_cookie_t pc
;
10426 xcb_get_property_reply_t
*pr
;
10428 DNPRINTF(SWM_D_INIT
, "grab_windows: begin\n");
10429 num_screens
= get_screen_count();
10430 for (i
= 0; i
< num_screens
; i
++) {
10431 qtc
= xcb_query_tree(conn
, screens
[i
].root
);
10432 qtr
= xcb_query_tree_reply(conn
, qtc
, NULL
);
10435 wins
= xcb_query_tree_children(qtr
);
10436 no
= xcb_query_tree_children_length(qtr
);
10438 /* Try to sort windows according to _NET_CLIENT_LIST. */
10439 pr
= xcb_get_property_reply(conn
, xcb_get_property(conn
, 0,
10440 screens
[i
].root
, ewmh
[_NET_CLIENT_LIST
].atom
,
10441 XCB_ATOM_WINDOW
, 0, UINT32_MAX
), NULL
);
10443 cwins
= xcb_get_property_value(pr
);
10444 n
= xcb_get_property_value_length(pr
) /
10445 sizeof(xcb_atom_t
);
10447 for (j
= 0; j
< n
; ++j
) {
10448 for (k
= j
; k
< no
; ++k
) {
10449 if (wins
[k
] == cwins
[j
]) {
10450 /* Swap wins j and k. */
10452 wins
[j
] = cwins
[j
];
10460 /* attach windows to a region */
10461 /* normal windows */
10462 DNPRINTF(SWM_D_INIT
, "grab_windows: grab top level windows.\n");
10463 for (j
= 0; j
< no
; j
++) {
10464 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
10465 if (r
->id
== wins
[j
]) {
10466 DNPRINTF(SWM_D_INIT
, "grab_windows: "
10467 "skip %#x; region input window.\n",
10470 } else if (r
->bar
->id
== wins
[j
]) {
10471 DNPRINTF(SWM_D_INIT
, "grab_windows: "
10472 "skip %#x; region bar.\n",
10481 gac
= xcb_get_window_attributes(conn
, wins
[j
]);
10482 gar
= xcb_get_window_attributes_reply(conn
, gac
, NULL
);
10484 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
10485 "doesn't exist.\n", wins
[j
]);
10489 if (gar
->override_redirect
) {
10490 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
10491 "override_redirect set.\n", wins
[j
]);
10496 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
10497 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
10499 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
10500 "is transient for %#x.\n", wins
[j
], trans
);
10505 state
= get_win_state(wins
[j
]);
10506 manage
= state
!= XCB_ICCCM_WM_STATE_WITHDRAWN
;
10507 mapped
= gar
->map_state
== XCB_MAP_STATE_VIEWABLE
;
10508 if (mapped
|| manage
)
10509 manage_window(wins
[j
], SWM_STACK_TOP
, mapped
);
10512 /* transient windows */
10513 DNPRINTF(SWM_D_INIT
, "grab_windows: grab transient windows.\n");
10514 for (j
= 0; j
< no
; j
++) {
10515 gac
= xcb_get_window_attributes(conn
, wins
[j
]);
10516 gar
= xcb_get_window_attributes_reply(conn
, gac
, NULL
);
10518 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
10519 "doesn't exist.\n", wins
[j
]);
10523 if (gar
->override_redirect
) {
10524 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
10525 "override_redirect set.\n", wins
[j
]);
10530 state
= get_win_state(wins
[j
]);
10531 manage
= state
!= XCB_ICCCM_WM_STATE_WITHDRAWN
;
10532 mapped
= gar
->map_state
== XCB_MAP_STATE_VIEWABLE
;
10533 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
10534 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
10535 &trans
, NULL
) && manage
)
10536 manage_window(wins
[j
], SWM_STACK_TOP
, mapped
);
10541 DNPRINTF(SWM_D_INIT
, "grab_windows: done.\n");
10545 setup_screens(void)
10547 int i
, j
, k
, num_screens
;
10548 struct workspace
*ws
;
10549 uint32_t gcv
[1], wa
[1];
10550 const xcb_query_extension_reply_t
*qep
;
10551 xcb_screen_t
*screen
;
10552 xcb_randr_query_version_cookie_t c
;
10553 xcb_randr_query_version_reply_t
*r
;
10555 num_screens
= get_screen_count();
10556 if ((screens
= calloc(num_screens
,
10557 sizeof(struct swm_screen
))) == NULL
)
10558 err(1, "setup_screens: calloc: failed to allocate memory for "
10561 /* Initial RandR setup. */
10562 randr_support
= false;
10563 qep
= xcb_get_extension_data(conn
, &xcb_randr_id
);
10564 if (qep
->present
) {
10565 c
= xcb_randr_query_version(conn
, 1, 1);
10566 r
= xcb_randr_query_version_reply(conn
, c
, NULL
);
10568 if (r
->major_version
>= 1) {
10569 randr_support
= true;
10570 randr_eventbase
= qep
->first_event
;
10576 wa
[0] = cursors
[XC_LEFT_PTR
].cid
;
10578 /* map physical screens */
10579 for (i
= 0; i
< num_screens
; i
++) {
10580 DNPRINTF(SWM_D_WS
, "setup_screens: init screen: %d\n", i
);
10581 screens
[i
].idx
= i
;
10582 screens
[i
].r_focus
= NULL
;
10584 TAILQ_INIT(&screens
[i
].rl
);
10585 TAILQ_INIT(&screens
[i
].orl
);
10586 if ((screen
= get_screen(i
)) == NULL
)
10587 errx(1, "ERROR: can't get screen %d.", i
);
10588 screens
[i
].root
= screen
->root
;
10590 /* set default colors */
10591 setscreencolor("red", i
, SWM_S_COLOR_FOCUS
);
10592 setscreencolor("rgb:88/88/88", i
, SWM_S_COLOR_UNFOCUS
);
10593 setscreencolor("rgb:00/80/80", i
, SWM_S_COLOR_BAR_BORDER
);
10594 setscreencolor("rgb:00/40/40", i
,
10595 SWM_S_COLOR_BAR_BORDER_UNFOCUS
);
10596 setscreencolor("black", i
, SWM_S_COLOR_BAR
);
10597 setscreencolor("rgb:a0/a0/a0", i
, SWM_S_COLOR_BAR_FONT
);
10598 setscreencolor("red", i
, SWM_S_COLOR_FOCUS_MAXIMIZED
);
10599 setscreencolor("rgb:88/88/88", i
,
10600 SWM_S_COLOR_UNFOCUS_MAXIMIZED
);
10602 /* create graphics context on screen */
10603 screens
[i
].bar_gc
= xcb_generate_id(conn
);
10605 xcb_create_gc(conn
, screens
[i
].bar_gc
, screens
[i
].root
,
10606 XCB_GC_GRAPHICS_EXPOSURES
, gcv
);
10608 /* set default cursor */
10609 xcb_change_window_attributes(conn
, screens
[i
].root
,
10610 XCB_CW_CURSOR
, wa
);
10612 /* init all workspaces */
10613 /* XXX these should be dynamically allocated too */
10614 for (j
= 0; j
< SWM_WS_MAX
; j
++) {
10615 ws
= &screens
[i
].ws
[j
];
10618 ws
->bar_enabled
= true;
10620 ws
->focus_prev
= NULL
;
10621 ws
->focus_pending
= NULL
;
10624 ws
->state
= SWM_WS_STATE_HIDDEN
;
10625 TAILQ_INIT(&ws
->stack
);
10626 TAILQ_INIT(&ws
->winlist
);
10627 TAILQ_INIT(&ws
->unmanagedlist
);
10629 for (k
= 0; layouts
[k
].l_stack
!= NULL
; k
++)
10630 if (layouts
[k
].l_config
!= NULL
)
10631 layouts
[k
].l_config(ws
,
10632 SWM_ARG_ID_STACKINIT
);
10633 ws
->cur_layout
= &layouts
[0];
10634 ws
->cur_layout
->l_string(ws
);
10640 xcb_randr_select_input(conn
, screens
[i
].root
,
10641 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE
);
10646 setup_globals(void)
10648 if ((bar_fonts
= strdup(SWM_BAR_FONTS
)) == NULL
)
10649 err(1, "setup_globals: strdup: failed to allocate memory.");
10651 if ((clock_format
= strdup("%a %b %d %R %Z %Y")) == NULL
)
10652 err(1, "setup_globals: strdup: failed to allocate memory.");
10654 if ((syms
= xcb_key_symbols_alloc(conn
)) == NULL
)
10655 errx(1, "unable to allocate key symbols");
10657 a_state
= get_atom_from_string("WM_STATE");
10658 a_prot
= get_atom_from_string("WM_PROTOCOLS");
10659 a_delete
= get_atom_from_string("WM_DELETE_WINDOW");
10660 a_net_supported
= get_atom_from_string("_NET_SUPPORTED");
10661 a_net_wm_check
= get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
10662 a_takefocus
= get_atom_from_string("WM_TAKE_FOCUS");
10663 a_utf8_string
= get_atom_from_string("UTF8_STRING");
10664 a_swm_ws
= get_atom_from_string("_SWM_WS");
10668 shutdown_cleanup(void)
10670 int i
, num_screens
;
10672 /* disable alarm because the following code may not be interrupted */
10674 if (signal(SIGALRM
, SIG_IGN
) == SIG_ERR
)
10675 err(1, "can't disable alarm");
10684 num_screens
= get_screen_count();
10685 for (i
= 0; i
< num_screens
; ++i
) {
10686 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
10687 screens
[i
].root
, XCB_CURRENT_TIME
);
10689 if (screens
[i
].bar_gc
!= XCB_NONE
)
10690 xcb_free_gc(conn
, screens
[i
].bar_gc
);
10691 if (!bar_font_legacy
) {
10692 XftColorFree(display
, DefaultVisual(display
, i
),
10693 DefaultColormap(display
, i
), &bar_font_color
);
10694 XftColorFree(display
, DefaultVisual(display
, i
),
10695 DefaultColormap(display
, i
), &search_font_color
);
10699 if (bar_font_legacy
)
10700 XFreeFontSet(display
, bar_fs
);
10702 XftFontClose(display
, bar_font
);
10705 xcb_key_symbols_free(syms
);
10707 xcb_disconnect(conn
);
10711 event_error(xcb_generic_error_t
*e
)
10715 DNPRINTF(SWM_D_EVENT
, "event_error: %s(%u) from %s(%u), sequence: %u, "
10716 "resource_id: %u, minor_code: %u\n",
10717 xcb_event_get_error_label(e
->error_code
), e
->error_code
,
10718 xcb_event_get_request_label(e
->major_code
), e
->major_code
,
10719 e
->sequence
, e
->resource_id
, e
->minor_code
);
10723 event_handle(xcb_generic_event_t
*evt
)
10725 uint8_t type
= XCB_EVENT_RESPONSE_TYPE(evt
);
10727 DNPRINTF(SWM_D_EVENT
, "XCB Event: %s(%d), seq %u\n",
10728 xcb_event_get_label(XCB_EVENT_RESPONSE_TYPE(evt
)),
10729 XCB_EVENT_RESPONSE_TYPE(evt
), evt
->sequence
);
10732 #define EVENT(type, callback) case type: callback((void *)evt); return
10733 EVENT(0, event_error
);
10734 EVENT(XCB_BUTTON_PRESS
, buttonpress
);
10735 /*EVENT(XCB_BUTTON_RELEASE, buttonpress);*/
10736 /*EVENT(XCB_CIRCULATE_NOTIFY, );*/
10737 /*EVENT(XCB_CIRCULATE_REQUEST, );*/
10738 EVENT(XCB_CLIENT_MESSAGE
, clientmessage
);
10739 /*EVENT(XCB_COLORMAP_NOTIFY, );*/
10740 EVENT(XCB_CONFIGURE_NOTIFY
, configurenotify
);
10741 EVENT(XCB_CONFIGURE_REQUEST
, configurerequest
);
10742 /*EVENT(XCB_CREATE_NOTIFY, );*/
10743 EVENT(XCB_DESTROY_NOTIFY
, destroynotify
);
10744 EVENT(XCB_ENTER_NOTIFY
, enternotify
);
10745 EVENT(XCB_EXPOSE
, expose
);
10747 EVENT(XCB_FOCUS_IN
, focusin
);
10748 EVENT(XCB_FOCUS_OUT
, focusout
);
10750 /*EVENT(XCB_GRAPHICS_EXPOSURE, );*/
10751 /*EVENT(XCB_GRAVITY_NOTIFY, );*/
10752 EVENT(XCB_KEY_PRESS
, keypress
);
10753 /*EVENT(XCB_KEY_RELEASE, keypress);*/
10754 /*EVENT(XCB_KEYMAP_NOTIFY, );*/
10756 EVENT(XCB_LEAVE_NOTIFY
, leavenotify
);
10758 EVENT(XCB_MAP_NOTIFY
, mapnotify
);
10759 EVENT(XCB_MAP_REQUEST
, maprequest
);
10760 EVENT(XCB_MAPPING_NOTIFY
, mappingnotify
);
10761 EVENT(XCB_MOTION_NOTIFY
, motionnotify
);
10762 /*EVENT(XCB_NO_EXPOSURE, );*/
10763 EVENT(XCB_PROPERTY_NOTIFY
, propertynotify
);
10764 /*EVENT(XCB_REPARENT_NOTIFY, );*/
10765 /*EVENT(XCB_RESIZE_REQUEST, );*/
10766 /*EVENT(XCB_SELECTION_CLEAR, );*/
10767 /*EVENT(XCB_SELECTION_NOTIFY, );*/
10768 /*EVENT(XCB_SELECTION_REQUEST, );*/
10769 EVENT(XCB_UNMAP_NOTIFY
, unmapnotify
);
10770 /*EVENT(XCB_VISIBILITY_NOTIFY, );*/
10773 if (type
- randr_eventbase
== XCB_RANDR_SCREEN_CHANGE_NOTIFY
)
10774 screenchange((void *)evt
);
10778 main(int argc
, char *argv
[])
10780 struct swm_region
*r
;
10781 char conf
[PATH_MAX
], *cfile
= NULL
;
10783 int xfd
, i
, num_screens
;
10784 struct sigaction sact
;
10785 xcb_generic_event_t
*evt
;
10790 bool stdin_ready
= false, startup
= true;
10792 /* suppress unused warning since var is needed */
10796 time_started
= time(NULL
);
10800 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION
, buildstr
);
10801 if (setlocale(LC_CTYPE
, "") == NULL
|| setlocale(LC_TIME
, "") == NULL
)
10802 warnx("no locale support");
10804 /* handle some signals */
10805 bzero(&sact
, sizeof(sact
));
10806 sigemptyset(&sact
.sa_mask
);
10808 sact
.sa_handler
= sighdlr
;
10809 sigaction(SIGINT
, &sact
, NULL
);
10810 sigaction(SIGQUIT
, &sact
, NULL
);
10811 sigaction(SIGTERM
, &sact
, NULL
);
10812 sigaction(SIGHUP
, &sact
, NULL
);
10814 sact
.sa_handler
= sighdlr
;
10815 sact
.sa_flags
= SA_NOCLDSTOP
;
10816 sigaction(SIGCHLD
, &sact
, NULL
);
10818 if ((display
= XOpenDisplay(0)) == NULL
)
10819 errx(1, "can not open display");
10821 conn
= XGetXCBConnection(display
);
10822 if (xcb_connection_has_error(conn
))
10823 errx(1, "can not get XCB connection");
10825 XSetEventQueueOwner(display
, XCBOwnsEventQueue
);
10827 xcb_prefetch_extension_data(conn
, &xcb_randr_id
);
10828 xfd
= xcb_get_file_descriptor(conn
);
10830 /* look for local and global conf file */
10831 pwd
= getpwuid(getuid());
10833 errx(1, "invalid user: %d", getuid());
10835 xcb_grab_server(conn
);
10836 xcb_aux_sync(conn
);
10838 /* flush all events */
10839 while ((evt
= xcb_poll_for_event(conn
))) {
10840 if (XCB_EVENT_RESPONSE_TYPE(evt
) == 0)
10846 errx(1, "another window manager is currently running");
10848 /* Load Xcursors and/or cursorfont glyph cursors. */
10851 xcb_aux_sync(conn
);
10861 for (i
= 0; ; i
++) {
10866 snprintf(conf
, sizeof conf
, "%s/.%s",
10867 pwd
->pw_dir
, SWM_CONF_FILE
);
10871 snprintf(conf
, sizeof conf
, "/etc/%s",
10876 snprintf(conf
, sizeof conf
, "%s/.%s",
10877 pwd
->pw_dir
, SWM_CONF_FILE_OLD
);
10880 /* global compat */
10881 snprintf(conf
, sizeof conf
, "/etc/%s",
10882 SWM_CONF_FILE_OLD
);
10888 if (strlen(conf
) && stat(conf
, &sb
) != -1)
10889 if (S_ISREG(sb
.st_mode
)) {
10896 /* load conf (if any) */
10898 conf_load(cfile
, SWM_CONF_DEFAULT
);
10902 if (getenv("SWM_STARTED") == NULL
)
10903 setenv("SWM_STARTED", "YES", 1);
10905 /* setup all bars */
10906 num_screens
= get_screen_count();
10907 for (i
= 0; i
< num_screens
; i
++)
10908 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
10911 /* Manage existing windows. */
10918 xcb_ungrab_server(conn
);
10921 /* Update state of each newly mapped workspace. */
10922 for (i
= 0; i
< num_screens
; i
++)
10923 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
10924 r
->ws
->state
= SWM_WS_STATE_MAPPED
;
10926 rd_max
= xfd
> STDIN_FILENO
? xfd
: STDIN_FILENO
;
10929 while ((evt
= xcb_poll_for_event(conn
))) {
10936 /* If just (re)started, set default focus if needed. */
10940 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
10941 r
= TAILQ_FIRST(&screens
[0].rl
);
10953 FD_SET(STDIN_FILENO
, &rd
);
10958 num_readable
= select(rd_max
+ 1, &rd
, NULL
, NULL
, &tv
);
10959 if (num_readable
== -1 && errno
!= EINTR
) {
10960 DNPRINTF(SWM_D_MISC
, "select failed");
10961 } else if (num_readable
> 0 && FD_ISSET(STDIN_FILENO
, &rd
)) {
10962 stdin_ready
= true;
10966 restart(NULL
, NULL
);
10975 stdin_ready
= false;
10976 bar_extra_update();
10983 shutdown_cleanup();