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 char *get_win_name(xcb_window_t
);
1045 uint8_t get_win_state(xcb_window_t
);
1046 void get_wm_protocols(struct ws_win
*);
1047 int get_ws_idx(struct ws_win
*);
1048 void grabbuttons(struct ws_win
*);
1049 void grabkeys(void);
1050 void grab_windows(void);
1051 void iconify(struct swm_region
*, union arg
*);
1052 bool isxlfd(char *);
1053 void keypress(xcb_key_press_event_t
*);
1054 int key_cmp(struct key
*, struct key
*);
1055 void key_insert(unsigned int, KeySym
, enum keyfuncid
, const char *);
1056 struct key
*key_lookup(unsigned int, KeySym
);
1057 void key_remove(struct key
*);
1058 void key_replace(struct key
*, unsigned int, KeySym
, enum keyfuncid
,
1060 void kill_bar_extra_atexit(void);
1061 void kill_refs(struct ws_win
*);
1063 void leavenotify(xcb_leave_notify_event_t
*);
1065 void load_float_geom(struct ws_win
*);
1066 struct ws_win
*manage_window(xcb_window_t
, int, bool);
1067 void map_window(struct ws_win
*);
1068 void mapnotify(xcb_map_notify_event_t
*);
1069 void mappingnotify(xcb_mapping_notify_event_t
*);
1070 void maprequest(xcb_map_request_event_t
*);
1071 void maximize_toggle(struct swm_region
*, union arg
*);
1072 void motionnotify(xcb_motion_notify_event_t
*);
1073 void move(struct ws_win
*, union arg
*);
1074 void move_step(struct swm_region
*, union arg
*);
1075 uint32_t name_to_pixel(int, const char *);
1076 void name_workspace(struct swm_region
*, union arg
*);
1077 void new_region(struct swm_screen
*, int, int, int, int);
1078 int parsekeys(const char *, unsigned int, unsigned int *, KeySym
*);
1079 int parsequirks(const char *, uint32_t *, int *);
1080 int parse_rgb(const char *, uint16_t *, uint16_t *, uint16_t *);
1081 void pressbutton(struct swm_region
*, union arg
*);
1082 void priorws(struct swm_region
*, union arg
*);
1084 void print_win_geom(xcb_window_t
);
1086 void propertynotify(xcb_property_notify_event_t
*);
1087 void quirk_free(struct quirk
*);
1088 void quirk_insert(const char *, const char *, const char *, uint32_t, int);
1089 void quirk_remove(struct quirk
*);
1090 void quirk_replace(struct quirk
*, const char *, const char *, const char *,
1092 void quit(struct swm_region
*, union arg
*);
1093 void raise_toggle(struct swm_region
*, union arg
*);
1094 void raise_window(struct ws_win
*);
1095 void region_containment(struct ws_win
*, struct swm_region
*, int);
1096 struct swm_region
*region_under(struct swm_screen
*, int, int);
1097 void regionize(struct ws_win
*, int, int);
1098 void resize(struct ws_win
*, union arg
*);
1099 void resize_step(struct swm_region
*, union arg
*);
1100 void restart(struct swm_region
*, union arg
*);
1101 struct swm_region
*root_to_region(xcb_window_t
, int);
1102 void screenchange(xcb_randr_screen_change_notify_event_t
*);
1103 void scan_randr(int);
1104 void search_do_resp(void);
1105 void search_resp_name_workspace(const char *, size_t);
1106 void search_resp_search_window(const char *);
1107 void search_resp_search_workspace(const char *);
1108 void search_resp_uniconify(const char *, size_t);
1109 void search_win(struct swm_region
*, union arg
*);
1110 void search_win_cleanup(void);
1111 void search_workspace(struct swm_region
*, union arg
*);
1112 void send_to_rg(struct swm_region
*, union arg
*);
1113 void send_to_ws(struct swm_region
*, union arg
*);
1114 void set_region(struct swm_region
*);
1115 int setautorun(const char *, const char *, int);
1116 int setconfbinding(const char *, const char *, int);
1117 int setconfcolor(const char *, const char *, int);
1118 int setconfmodkey(const char *, const char *, int);
1119 int setconfquirk(const char *, const char *, int);
1120 int setconfregion(const char *, const char *, int);
1121 int setconfspawn(const char *, const char *, int);
1122 int setconfvalue(const char *, const char *, int);
1123 void setkeybinding(unsigned int, KeySym
, enum keyfuncid
, const char *);
1124 int setkeymapping(const char *, const char *, int);
1125 int setlayout(const char *, const char *, int);
1126 void setquirk(const char *, const char *, const char *, uint32_t, int);
1127 void setscreencolor(const char *, int, int);
1128 void setspawn(const char *, const char *, int);
1129 void setup_ewmh(void);
1130 void setup_globals(void);
1131 void setup_keys(void);
1132 void setup_quirks(void);
1133 void setup_screens(void);
1134 void setup_spawn(void);
1135 void set_child_transient(struct ws_win
*, xcb_window_t
*);
1136 void set_win_state(struct ws_win
*, uint8_t);
1137 void shutdown_cleanup(void);
1139 void socket_setnonblock(int);
1140 void sort_windows(struct ws_win_list
*);
1141 void spawn(int, union arg
*, bool);
1142 void spawn_custom(struct swm_region
*, union arg
*, const char *);
1143 int spawn_expand(struct swm_region
*, union arg
*, const char *, char ***);
1144 void spawn_insert(const char *, const char *, int);
1145 struct spawn_prog
*spawn_find(const char *);
1146 void spawn_remove(struct spawn_prog
*);
1147 void spawn_replace(struct spawn_prog
*, const char *, const char *, int);
1148 void spawn_select(struct swm_region
*, union arg
*, const char *, int *);
1149 void stack_config(struct swm_region
*, union arg
*);
1150 void stack_master(struct workspace
*, struct swm_geometry
*, int, bool);
1151 void store_float_geom(struct ws_win
*);
1152 char *strdupsafe(const char *);
1153 void swapwin(struct swm_region
*, union arg
*);
1154 void switchws(struct swm_region
*, union arg
*);
1155 void teardown_ewmh(void);
1156 void unescape_selector(char *);
1157 void unfocus_win(struct ws_win
*);
1158 void uniconify(struct swm_region
*, union arg
*);
1159 void unmanage_window(struct ws_win
*);
1160 void unmapnotify(xcb_unmap_notify_event_t
*);
1161 void unmap_all(void);
1162 void unmap_window(struct ws_win
*);
1163 void updatenumlockmask(void);
1164 void update_floater(struct ws_win
*);
1165 void update_modkey(unsigned int);
1166 void update_win_stacking(struct ws_win
*);
1167 void update_window(struct ws_win
*);
1168 void update_window_color(struct ws_win
*);
1169 void update_wm_state(struct ws_win
*win
);
1170 void validate_spawns(void);
1171 int validate_win(struct ws_win
*);
1172 int validate_ws(struct workspace
*);
1173 void version(struct swm_region
*, union arg
*);
1174 void win_to_ws(struct ws_win
*, int, bool);
1175 pid_t
window_get_pid(xcb_window_t
);
1176 void wkill(struct swm_region
*, union arg
*);
1177 void update_ws_stack(struct workspace
*);
1178 void xft_init(struct swm_region
*);
1179 void _add_startup_exception(const char *, va_list);
1180 void add_startup_exception(const char *, ...);
1182 RB_PROTOTYPE(key_tree
, key
, entry
, key_cmp
);
1183 RB_GENERATE(key_tree
, key
, entry
, key_cmp
);
1184 struct key_tree keys
;
1189 xcb_font_t cf
= XCB_NONE
;
1192 for (i
= 0; i
< LENGTH(cursors
); ++i
) {
1193 /* try to load Xcursor first. */
1194 cursors
[i
].cid
= XcursorLibraryLoadCursor(display
,
1197 /* fallback to cursorfont. */
1198 if (cursors
[i
].cid
== XCB_CURSOR_NONE
) {
1199 if (cf
== XCB_NONE
) {
1200 cf
= xcb_generate_id(conn
);
1201 xcb_open_font(conn
, cf
, strlen("cursor"),
1205 cursors
[i
].cid
= xcb_generate_id(conn
);
1206 xcb_create_glyph_cursor(conn
, cursors
[i
].cid
, cf
, cf
,
1207 cursors
[i
].cf_char
, cursors
[i
].cf_char
+ 1, 0, 0, 0,
1208 0xffff, 0xffff, 0xffff);
1214 xcb_close_font(conn
, cf
);
1218 cursors_cleanup(void)
1221 for (i
= 0; i
< LENGTH(cursors
); ++i
)
1222 xcb_free_cursor(conn
, cursors
[i
].cid
);
1226 expand_tilde(const char *s
)
1228 struct passwd
*ppwd
;
1236 errx(1, "expand_tilde: NULL string.");
1239 result
= strdup(sc
);
1245 if ((max
= sysconf(_SC_LOGIN_NAME_MAX
)) == -1)
1246 errx(1, "expand_tilde: sysconf");
1248 if ((user
= calloc(1, max
+ 1)) == NULL
)
1249 errx(1, "expand_tilde: calloc");
1251 for (i
= 0; s
[i
] != '/' && s
[i
] != '\0'; ++i
)
1256 ppwd
= strlen(user
) == 0 ? getpwuid(getuid()) : getpwnam(user
);
1260 result
= strdup(sc
);
1262 if (asprintf(&result
, "%s%s", ppwd
->pw_dir
, s
) == -1)
1266 errx(1, "expand_tilde: failed to allocate memory.");
1272 parse_rgb(const char *rgb
, uint16_t *rr
, uint16_t *gg
, uint16_t *bb
)
1274 unsigned int tmpr
, tmpg
, tmpb
;
1276 if (sscanf(rgb
, "rgb:%x/%x/%x", &tmpr
, &tmpg
, &tmpb
) != 3)
1279 *rr
= RGB_8_TO_16(tmpr
);
1280 *gg
= RGB_8_TO_16(tmpg
);
1281 *bb
= RGB_8_TO_16(tmpb
);
1287 get_screen(int screen
)
1289 const xcb_setup_t
*r
;
1290 xcb_screen_iterator_t iter
;
1292 if ((r
= xcb_get_setup(conn
)) == NULL
) {
1293 DNPRINTF(SWM_D_MISC
, "get_screen: xcb_get_setup\n");
1297 iter
= xcb_setup_roots_iterator(r
);
1298 for (; iter
.rem
; --screen
, xcb_screen_next(&iter
))
1306 get_screen_count(void)
1308 const xcb_setup_t
*r
;
1310 if ((r
= xcb_get_setup(conn
)) == NULL
) {
1311 DNPRINTF(SWM_D_MISC
, "get_screen_count: xcb_get_setup\n");
1315 return xcb_setup_roots_length(r
);
1319 get_region_index(struct swm_region
*r
)
1321 struct swm_region
*rr
;
1327 TAILQ_FOREACH(rr
, &r
->s
->rl
, entry
) {
1342 if (focus_mode
== SWM_FOCUS_DEFAULT
)
1343 event_drain(XCB_ENTER_NOTIFY
);
1349 get_atom_from_string(const char *str
)
1351 xcb_intern_atom_cookie_t c
;
1352 xcb_intern_atom_reply_t
*r
;
1355 c
= xcb_intern_atom(conn
, 0, strlen(str
), str
);
1356 r
= xcb_intern_atom_reply(conn
, c
, NULL
);
1364 return (XCB_ATOM_NONE
);
1368 get_wm_protocols(struct ws_win
*win
) {
1370 xcb_icccm_get_wm_protocols_reply_t wpr
;
1372 if (xcb_icccm_get_wm_protocols_reply(conn
,
1373 xcb_icccm_get_wm_protocols(conn
, win
->id
, a_prot
),
1375 for (i
= 0; i
< (int)wpr
.atoms_len
; i
++) {
1376 if (wpr
.atoms
[i
] == a_takefocus
)
1377 win
->take_focus
= true;
1378 if (wpr
.atoms
[i
] == a_delete
)
1379 win
->can_delete
= true;
1381 xcb_icccm_get_wm_protocols_reply_wipe(&wpr
);
1388 xcb_window_t root
, win
;
1389 int i
, j
, num_screens
;
1391 for (i
= 0; i
< LENGTH(ewmh
); i
++)
1392 ewmh
[i
].atom
= get_atom_from_string(ewmh
[i
].name
);
1394 num_screens
= get_screen_count();
1395 for (i
= 0; i
< num_screens
; i
++) {
1396 root
= screens
[i
].root
;
1398 /* Set up _NET_SUPPORTING_WM_CHECK. */
1399 win
= xcb_generate_id(conn
);
1400 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, win
, root
,
1401 0, 0, 1, 1, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
1402 XCB_COPY_FROM_PARENT
, 0, NULL
);
1404 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, root
,
1405 a_net_wm_check
, XCB_ATOM_WINDOW
, 32, 1, &win
);
1406 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
1407 a_net_wm_check
, XCB_ATOM_WINDOW
, 32, 1, &win
);
1410 * Impersonate LG3D non-reparenting WM, written by Sun, to
1411 * workaround a Java GUI rendering issue.
1413 if (java_workaround
)
1414 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
1415 ewmh
[_NET_WM_NAME
].atom
, a_utf8_string
,
1416 8, strlen("LG3D"), "LG3D");
1418 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
1419 ewmh
[_NET_WM_NAME
].atom
, a_utf8_string
,
1420 8, strlen("spectrwm"), "spectrwm");
1422 /* Report supported atoms */
1423 xcb_delete_property(conn
, root
, a_net_supported
);
1424 for (j
= 0; j
< LENGTH(ewmh
); j
++)
1425 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, root
,
1426 a_net_supported
, XCB_ATOM_ATOM
, 32, 1,
1431 ewmh_update_desktops();
1432 ewmh_get_desktop_names();
1440 xcb_get_property_cookie_t pc
;
1441 xcb_get_property_reply_t
*pr
;
1443 num_screens
= get_screen_count();
1445 for (i
= 0; i
< num_screens
; i
++) {
1446 /* Get the support check window and destroy it */
1447 pc
= xcb_get_property(conn
, 0, screens
[i
].root
, a_net_wm_check
,
1448 XCB_ATOM_WINDOW
, 0, 1);
1449 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
1452 if (pr
->format
== a_net_wm_check
) {
1453 id
= *((xcb_window_t
*)xcb_get_property_value(pr
));
1455 xcb_destroy_window(conn
, id
);
1456 xcb_delete_property(conn
, screens
[i
].root
,
1458 xcb_delete_property(conn
, screens
[i
].root
,
1466 ewmh_autoquirk(struct ws_win
*win
)
1468 xcb_get_property_reply_t
*r
;
1469 xcb_get_property_cookie_t c
;
1473 c
= xcb_get_property(conn
, 0, win
->id
,
1474 ewmh
[_NET_WM_WINDOW_TYPE
].atom
, XCB_ATOM_ATOM
, 0, UINT32_MAX
);
1475 r
= xcb_get_property_reply(conn
, c
, NULL
);
1479 type
= xcb_get_property_value(r
);
1480 n
= xcb_get_property_value_length(r
) / sizeof(xcb_atom_t
);
1482 for (i
= 0; i
< n
; i
++) {
1483 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_NORMAL
].atom
)
1485 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_DOCK
].atom
||
1486 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_TOOLBAR
].atom
||
1487 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_UTILITY
].atom
) {
1488 win
->quirks
= SWM_Q_FLOAT
| SWM_Q_ANYWHERE
;
1491 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_SPLASH
].atom
||
1492 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_DIALOG
].atom
) {
1493 win
->quirks
= SWM_Q_FLOAT
;
1501 ewmh_update_actions(struct ws_win
*win
)
1503 xcb_atom_t actions
[SWM_EWMH_ACTION_COUNT_MAX
];
1509 actions
[n
++] = ewmh
[_NET_WM_ACTION_CLOSE
].atom
;
1512 actions
[n
++] = ewmh
[_NET_WM_ACTION_MOVE
].atom
;
1513 actions
[n
++] = ewmh
[_NET_WM_ACTION_RESIZE
].atom
;
1514 actions
[n
++] = ewmh
[_NET_WM_ACTION_ABOVE
].atom
;
1517 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
1518 ewmh
[_NET_WM_ALLOWED_ACTIONS
].atom
, XCB_ATOM_ATOM
, 32, 1, actions
);
1521 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
1522 #define _NET_WM_STATE_ADD 1 /* add/set property */
1523 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
1526 ewmh_change_wm_state(struct ws_win
*win
, xcb_atom_t state
, long action
)
1533 name
= get_atom_name(state
);
1534 DNPRINTF(SWM_D_PROP
, "ewmh_change_wm_state: win %#x, state: %s, "
1535 "action: %ld\n", WINID(win
), name
, action
);
1541 if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
)
1542 flag
= EWMH_F_FULLSCREEN
;
1543 else if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
)
1544 flag
= EWMH_F_ABOVE
;
1545 else if (state
== ewmh
[_NET_WM_STATE_HIDDEN
].atom
)
1546 flag
= EWMH_F_HIDDEN
;
1547 else if (state
== ewmh
[_NET_WM_STATE_MAXIMIZED_VERT
].atom
||
1548 state
== ewmh
[_NET_WM_STATE_MAXIMIZED_HORZ
].atom
)
1549 flag
= EWMH_F_MAXIMIZED
;
1550 else if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
)
1551 flag
= SWM_F_MANUAL
;
1552 else if (state
== ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
)
1553 flag
= EWMH_F_SKIP_PAGER
;
1554 else if (state
== ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
)
1555 flag
= EWMH_F_SKIP_TASKBAR
;
1557 /* Disallow unfloating transients. */
1558 if (TRANS(win
) && flag
== EWMH_F_ABOVE
)
1561 new_flags
= win
->ewmh_flags
;
1564 case _NET_WM_STATE_REMOVE
:
1567 case _NET_WM_STATE_ADD
:
1570 case _NET_WM_STATE_TOGGLE
:
1575 ewmh_apply_flags(win
, new_flags
);
1578 DNPRINTF(SWM_D_PROP
, "ewmh_change_wm_state: done.\n");
1582 ewmh_apply_flags(struct ws_win
*win
, uint32_t pending
)
1584 struct workspace
*ws
;
1587 changed
= win
->ewmh_flags
^ pending
;
1591 DNPRINTF(SWM_D_PROP
, "ewmh_apply_flags: pending: %d\n", pending
);
1593 win
->ewmh_flags
= pending
;
1596 if (changed
& EWMH_F_HIDDEN
) {
1598 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
1599 ws
->focus_pending
= get_focus_prev(win
);
1604 /* Reload floating geometry in case region changed. */
1606 load_float_geom(win
);
1608 /* The window is no longer iconic, prepare focus. */
1609 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
1610 ws
->focus_pending
= get_focus_magic(win
);
1615 if (changed
& EWMH_F_ABOVE
) {
1616 if (ws
->cur_layout
!= &layouts
[SWM_MAX_STACK
]) {
1618 load_float_geom(win
);
1619 else if (!MAXIMIZED(win
))
1620 store_float_geom(win
);
1622 win
->ewmh_flags
&= ~EWMH_F_MAXIMIZED
;
1623 changed
&= ~EWMH_F_MAXIMIZED
;
1627 win
->ewmh_flags
^= EWMH_F_ABOVE
& pending
;
1631 if (changed
& EWMH_F_MAXIMIZED
) {
1632 /* VERT and/or HORZ changed. */
1634 if (!MAXIMIZED(win
))
1635 load_float_geom(win
);
1637 store_float_geom(win
);
1640 if (MAXIMIZED(win
)) {
1641 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
1642 ws
->cur_layout
!= &layouts
[SWM_MAX_STACK
]) {
1646 ws
->focus_pending
= win
;
1650 update_window_color(win
);
1654 if (changed
& EWMH_F_FULLSCREEN
) {
1655 if (FULLSCREEN(win
)) {
1656 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
1660 ws
->focus_pending
= win
;
1663 load_float_geom(win
);
1666 win
->ewmh_flags
&= ~EWMH_F_MAXIMIZED
;
1670 DNPRINTF(SWM_D_PROP
, "ewmh_apply_flags: done.\n");
1674 ewmh_update_wm_state(struct ws_win
*win
) {
1675 xcb_atom_t vals
[SWM_EWMH_ACTION_COUNT_MAX
];
1679 vals
[n
++] = ewmh
[_NET_WM_STATE_HIDDEN
].atom
;
1680 if (FULLSCREEN(win
))
1681 vals
[n
++] = ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
;
1682 if (MAXIMIZED_VERT(win
))
1683 vals
[n
++] = ewmh
[_NET_WM_STATE_MAXIMIZED_VERT
].atom
;
1684 if (MAXIMIZED_HORZ(win
))
1685 vals
[n
++] = ewmh
[_NET_WM_STATE_MAXIMIZED_HORZ
].atom
;
1686 if (win
->ewmh_flags
& EWMH_F_SKIP_PAGER
)
1687 vals
[n
++] = ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
;
1688 if (win
->ewmh_flags
& EWMH_F_SKIP_TASKBAR
)
1689 vals
[n
++] = ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
;
1690 if (win
->ewmh_flags
& EWMH_F_ABOVE
)
1691 vals
[n
++] = ewmh
[_NET_WM_STATE_ABOVE
].atom
;
1692 if (win
->ewmh_flags
& SWM_F_MANUAL
)
1693 vals
[n
++] = ewmh
[_SWM_WM_STATE_MANUAL
].atom
;
1696 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
1697 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, n
, vals
);
1699 xcb_delete_property(conn
, win
->id
, ewmh
[_NET_WM_STATE
].atom
);
1703 ewmh_get_wm_state(struct ws_win
*win
)
1706 xcb_get_property_cookie_t c
;
1707 xcb_get_property_reply_t
*r
;
1713 win
->ewmh_flags
= 0;
1715 c
= xcb_get_property(conn
, 0, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
1716 XCB_ATOM_ATOM
, 0, UINT32_MAX
);
1717 r
= xcb_get_property_reply(conn
, c
, NULL
);
1721 states
= xcb_get_property_value(r
);
1722 n
= xcb_get_property_value_length(r
) / sizeof(xcb_atom_t
);
1724 for (i
= 0; i
< n
; i
++)
1725 ewmh_change_wm_state(win
, states
[i
], _NET_WM_STATE_ADD
);
1733 dumpwins(struct swm_region
*r
, union arg
*args
)
1737 xcb_get_window_attributes_cookie_t c
;
1738 xcb_get_window_attributes_reply_t
*wa
;
1740 /* suppress unused warning since var is needed */
1743 if (r
->ws
== NULL
) {
1744 DPRINTF("dumpwins: invalid workspace\n");
1748 DPRINTF("=== managed window list ws %02d ===\n", r
->ws
->idx
);
1749 TAILQ_FOREACH(w
, &r
->ws
->winlist
, entry
) {
1750 state
= get_win_state(w
->id
);
1751 c
= xcb_get_window_attributes(conn
, w
->id
);
1752 wa
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
1754 DPRINTF("win %#x, map_state: %d, state: %u, "
1755 "transient: %#x\n", w
->id
, wa
->map_state
,
1756 state
, w
->transient
);
1759 DPRINTF("win %#x, failed xcb_get_window_attributes\n",
1763 DPRINTF("=== stacking order (top down) === \n");
1764 TAILQ_FOREACH(w
, &r
->ws
->stack
, stack_entry
) {
1765 DPRINTF("win %#x, fs: %s, maximized: %s, above: %s, "
1766 "iconic: %s\n", w
->id
, YESNO(FULLSCREEN(w
)),
1767 YESNO(MAXIMIZED(w
)), YESNO(ABOVE(w
)), YESNO(ICONIC(w
)));
1770 DPRINTF("===== unmanaged window list =====\n");
1771 TAILQ_FOREACH(w
, &r
->ws
->unmanagedlist
, entry
) {
1772 state
= get_win_state(w
->id
);
1773 c
= xcb_get_window_attributes(conn
, w
->id
);
1774 wa
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
1776 DPRINTF("win %#x, map_state: %d, state: %u, "
1777 "transient: %#x\n", w
->id
, wa
->map_state
,
1778 state
, w
->transient
);
1781 DPRINTF("win %#x, failed xcb_get_window_attributes\n",
1785 DPRINTF("=================================\n");
1789 dumpwins(struct swm_region
*r
, union arg
*s
)
1794 #endif /* SWM_DEBUG */
1799 int saved_errno
, status
;
1802 saved_errno
= errno
;
1806 while ((pid
= waitpid(WAIT_ANY
, &status
, WNOHANG
)) != 0) {
1811 if (errno
!= ECHILD
)
1812 warn("sighdlr: waitpid");
1813 #endif /* SWM_DEBUG */
1816 if (pid
== searchpid
)
1820 if (WIFEXITED(status
)) {
1821 if (WEXITSTATUS(status
) != 0)
1822 warnx("sighdlr: child exit status: %d",
1823 WEXITSTATUS(status
));
1825 warnx("sighdlr: child is terminated "
1827 #endif /* SWM_DEBUG */
1841 errno
= saved_errno
;
1847 struct pid_e
*p
= NULL
;
1849 DNPRINTF(SWM_D_MISC
, "find_pid: %d\n", pid
);
1854 TAILQ_FOREACH(p
, &pidlist
, entry
) {
1863 name_to_pixel(int sidx
, const char *colorname
)
1865 uint32_t result
= 0;
1866 char cname
[32] = "#";
1867 xcb_screen_t
*screen
;
1868 xcb_colormap_t cmap
;
1869 xcb_alloc_color_reply_t
*cr
;
1870 xcb_alloc_named_color_reply_t
*nr
;
1871 uint16_t rr
, gg
, bb
;
1873 screen
= get_screen(sidx
);
1874 cmap
= screen
->default_colormap
;
1876 /* color is in format rgb://rr/gg/bb */
1877 if (strncmp(colorname
, "rgb:", 4) == 0) {
1878 if (parse_rgb(colorname
, &rr
, &gg
, &bb
) == -1)
1879 warnx("could not parse rgb %s", colorname
);
1881 cr
= xcb_alloc_color_reply(conn
,
1882 xcb_alloc_color(conn
, cmap
, rr
, gg
, bb
),
1888 warnx("color '%s' not found", colorname
);
1891 nr
= xcb_alloc_named_color_reply(conn
,
1892 xcb_alloc_named_color(conn
, cmap
, strlen(colorname
),
1895 strlcat(cname
, colorname
+ 2, sizeof cname
- 1);
1896 nr
= xcb_alloc_named_color_reply(conn
,
1897 xcb_alloc_named_color(conn
, cmap
, strlen(cname
),
1904 warnx("color '%s' not found", colorname
);
1911 setscreencolor(const char *val
, int i
, int c
)
1913 if (i
< 0 || i
>= get_screen_count())
1916 screens
[i
].c
[c
].pixel
= name_to_pixel(i
, val
);
1917 free(screens
[i
].c
[c
].name
);
1918 if ((screens
[i
].c
[c
].name
= strdup(val
)) == NULL
)
1923 fancy_stacker(struct workspace
*ws
)
1925 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1926 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1927 snprintf(ws
->stacker
, sizeof ws
->stacker
,
1928 ws
->l_state
.vertical_flip
? "[%d>%d]" : "[%d|%d]",
1929 ws
->l_state
.vertical_mwin
, ws
->l_state
.vertical_stacks
);
1930 else if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1931 snprintf(ws
->stacker
, sizeof ws
->stacker
,
1932 ws
->l_state
.horizontal_flip
? "[%dv%d]" : "[%d-%d]",
1933 ws
->l_state
.horizontal_mwin
, ws
->l_state
.horizontal_stacks
);
1937 plain_stacker(struct workspace
*ws
)
1939 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1940 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1941 strlcpy(ws
->stacker
, ws
->l_state
.vertical_flip
? "[>]" : "[|]",
1942 sizeof ws
->stacker
);
1943 else if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1944 strlcpy(ws
->stacker
, ws
->l_state
.horizontal_flip
? "[v]" : "[-]",
1945 sizeof ws
->stacker
);
1949 custom_region(const char *val
)
1951 unsigned int x
, y
, w
, h
;
1952 int sidx
, num_screens
;
1953 xcb_screen_t
*screen
;
1955 num_screens
= get_screen_count();
1956 if (sscanf(val
, "screen[%d]:%ux%u+%u+%u", &sidx
, &w
, &h
, &x
, &y
) != 5)
1957 errx(1, "invalid custom region, "
1958 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1959 if (sidx
< 1 || sidx
> num_screens
)
1960 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1964 if ((screen
= get_screen(sidx
)) == NULL
)
1965 errx(1, "ERROR: can't get screen %d.", sidx
);
1968 errx(1, "region %ux%u+%u+%u too small", w
, h
, x
, y
);
1970 if (x
> screen
->width_in_pixels
||
1971 y
> screen
->height_in_pixels
||
1972 w
+ x
> screen
->width_in_pixels
||
1973 h
+ y
> screen
->height_in_pixels
) {
1974 warnx("ignoring region %ux%u+%u+%u - not within screen "
1975 "boundaries (%ux%u)", w
, h
, x
, y
,
1976 screen
->width_in_pixels
, screen
->height_in_pixels
);
1980 new_region(&screens
[sidx
], x
, y
, w
, h
);
1984 socket_setnonblock(int fd
)
1988 if ((flags
= fcntl(fd
, F_GETFL
, 0)) == -1)
1989 err(1, "fcntl F_GETFL");
1990 flags
|= O_NONBLOCK
;
1991 if ((flags
= fcntl(fd
, F_SETFL
, flags
)) == -1)
1992 err(1, "fcntl F_SETFL");
1996 bar_print_legacy(struct swm_region
*r
, const char *s
)
1998 xcb_rectangle_t rect
;
2003 XRectangle ibox
, lbox
;
2007 XmbTextExtents(bar_fs
, s
, len
, &ibox
, &lbox
);
2009 switch (bar_justify
) {
2010 case SWM_BAR_JUSTIFY_LEFT
:
2013 case SWM_BAR_JUSTIFY_CENTER
:
2014 x
= (WIDTH(r
) - lbox
.width
) / 2;
2016 case SWM_BAR_JUSTIFY_RIGHT
:
2017 x
= WIDTH(r
) - lbox
.width
- SWM_BAR_OFFSET
;
2021 if (x
< SWM_BAR_OFFSET
)
2026 rect
.width
= WIDTH(r
->bar
);
2027 rect
.height
= HEIGHT(r
->bar
);
2029 /* clear back buffer */
2030 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
2031 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
2032 xcb_poly_fill_rectangle(conn
, r
->bar
->buffer
, r
->s
->bar_gc
, 1, &rect
);
2034 /* draw back buffer */
2035 gcvd
.graphics_exposures
= 0;
2036 draw
= XCreateGC(display
, r
->bar
->buffer
, GCGraphicsExposures
, &gcvd
);
2037 XSetForeground(display
, draw
, r
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
);
2038 DRAWSTRING(display
, r
->bar
->buffer
, bar_fs
, draw
,
2039 x
, (bar_fs_extents
->max_logical_extent
.height
- lbox
.height
) / 2 -
2041 XFreeGC(display
, draw
);
2044 xcb_copy_area(conn
, r
->bar
->buffer
, r
->bar
->id
, r
->s
->bar_gc
, 0, 0,
2045 0, 0, WIDTH(r
->bar
), HEIGHT(r
->bar
));
2049 bar_print(struct swm_region
*r
, const char *s
)
2052 xcb_rectangle_t rect
;
2060 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
, &info
);
2062 switch (bar_justify
) {
2063 case SWM_BAR_JUSTIFY_LEFT
:
2066 case SWM_BAR_JUSTIFY_CENTER
:
2067 x
= (WIDTH(r
) - info
.width
) / 2;
2069 case SWM_BAR_JUSTIFY_RIGHT
:
2070 x
= WIDTH(r
) - info
.width
- SWM_BAR_OFFSET
;
2074 if (x
< SWM_BAR_OFFSET
)
2079 rect
.width
= WIDTH(r
->bar
);
2080 rect
.height
= HEIGHT(r
->bar
);
2082 /* clear back buffer */
2083 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
2084 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
2085 xcb_poly_fill_rectangle(conn
, r
->bar
->buffer
, r
->s
->bar_gc
, 1, &rect
);
2087 /* draw back buffer */
2088 draw
= XftDrawCreate(display
, r
->bar
->buffer
,
2089 DefaultVisual(display
, r
->s
->idx
),
2090 DefaultColormap(display
, r
->s
->idx
));
2092 XftDrawStringUtf8(draw
, &bar_font_color
, bar_font
, x
,
2093 (HEIGHT(r
->bar
) + bar_font
->height
) / 2 - bar_font
->descent
,
2096 XftDrawDestroy(draw
);
2099 xcb_copy_area(conn
, r
->bar
->buffer
, r
->bar
->id
, r
->s
->bar_gc
, 0, 0,
2100 0, 0, WIDTH(r
->bar
), HEIGHT(r
->bar
));
2104 bar_extra_stop(void)
2108 bzero(bar_pipe
, sizeof bar_pipe
);
2111 kill(bar_pid
, SIGTERM
);
2114 strlcpy(bar_ext
, "", sizeof bar_ext
);
2119 bar_window_class(char *s
, size_t sz
, struct swm_region
*r
)
2121 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2123 if (r
->ws
->focus
->ch
.class_name
!= NULL
)
2124 strlcat(s
, r
->ws
->focus
->ch
.class_name
, sz
);
2128 bar_window_instance(char *s
, size_t sz
, struct swm_region
*r
)
2130 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2132 if (r
->ws
->focus
->ch
.instance_name
!= NULL
)
2133 strlcat(s
, r
->ws
->focus
->ch
.instance_name
, sz
);
2137 bar_window_class_instance(char *s
, size_t sz
, struct swm_region
*r
)
2139 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2142 bar_window_class(s
, sz
, r
);
2143 strlcat(s
, ":", sz
);
2144 bar_window_instance(s
, sz
, r
);
2148 bar_window_state(char *s
, size_t sz
, struct swm_region
*r
)
2150 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2152 if (MAXIMIZED(r
->ws
->focus
))
2153 strlcat(s
, "(m)", sz
);
2154 else if (ABOVE(r
->ws
->focus
))
2155 strlcat(s
, "(f)", sz
);
2159 bar_window_name(char *s
, size_t sz
, struct swm_region
*r
)
2163 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2166 title
= get_win_name(r
->ws
->focus
->id
);
2167 strlcat(s
, title
, sz
);
2172 bar_urgent(char *s
, size_t sz
)
2175 int i
, j
, num_screens
;
2176 bool urgent
[SWM_WS_MAX
];
2178 xcb_get_property_cookie_t c
;
2179 xcb_icccm_wm_hints_t hints
;
2181 for (i
= 0; i
< workspace_limit
; i
++)
2184 num_screens
= get_screen_count();
2185 for (i
= 0; i
< num_screens
; i
++)
2186 for (j
= 0; j
< workspace_limit
; j
++)
2187 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
) {
2188 c
= xcb_icccm_get_wm_hints(conn
, win
->id
);
2189 if (xcb_icccm_get_wm_hints_reply(conn
, c
,
2192 if (hints
.flags
& XCB_ICCCM_WM_HINT_X_URGENCY
)
2196 for (i
= 0; i
< workspace_limit
; i
++) {
2198 snprintf(b
, sizeof b
, "%d ", i
+ 1);
2200 } else if (!urgent_collapse
) {
2201 strlcat(s
, "- ", sz
);
2207 bar_workspace_name(char *s
, size_t sz
, struct swm_region
*r
)
2209 if (r
== NULL
|| r
->ws
== NULL
)
2211 if (r
->ws
->name
!= NULL
)
2212 strlcat(s
, r
->ws
->name
, sz
);
2215 /* build the default bar format according to the defined enabled options */
2217 bar_fmt(const char *fmtexp
, char *fmtnew
, struct swm_region
*r
, size_t sz
)
2221 /* if format provided, just copy the buffers */
2222 if (bar_format
!= NULL
) {
2223 strlcpy(fmtnew
, fmtexp
, sz
);
2227 /* reset the output buffer */
2230 strlcat(fmtnew
, "+N:+I ", sz
);
2232 strlcat(fmtnew
, "+S", sz
);
2233 strlcat(fmtnew
, " ", sz
);
2235 /* only show the workspace name if there's actually one */
2236 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->name
!= NULL
)
2237 strlcat(fmtnew
, "<+D>", sz
);
2239 /* If enabled, only show the iconic count if there are iconic wins. */
2240 if (iconic_enabled
&& r
!= NULL
&& r
->ws
!= NULL
)
2241 TAILQ_FOREACH(w
, &r
->ws
->winlist
, entry
)
2243 strlcat(fmtnew
, "{+M}", sz
);
2247 strlcat(fmtnew
, "+3<", sz
);
2249 if (clock_enabled
) {
2250 strlcat(fmtnew
, fmtexp
, sz
);
2251 strlcat(fmtnew
, "+4<", sz
);
2254 /* bar_urgent already adds the space before the last asterisk */
2256 strlcat(fmtnew
, "* +U*+4<", sz
);
2258 if (window_class_enabled
) {
2259 strlcat(fmtnew
, "+C", sz
);
2260 if (!window_instance_enabled
)
2261 strlcat(fmtnew
, "+4<", sz
);
2264 /* checks needed by the colon and floating strlcat(3) calls below */
2265 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->focus
!= NULL
) {
2266 if (window_instance_enabled
) {
2267 if (window_class_enabled
)
2268 strlcat(fmtnew
, ":", sz
);
2269 strlcat(fmtnew
, "+T+4<", sz
);
2271 if (window_name_enabled
) {
2272 if (ABOVE(r
->ws
->focus
) || MAXIMIZED(r
->ws
->focus
))
2273 strlcat(fmtnew
, "+F ", sz
);
2274 strlcat(fmtnew
, "+64W ", sz
);
2278 /* finally add the action script output and the version */
2279 strlcat(fmtnew
, "+4<+A+4<+V", sz
);
2283 bar_replace_pad(char *tmp
, int *limit
, size_t sz
)
2285 /* special case; no limit given, pad one space, instead */
2286 if (*limit
== (int)sz
- 1)
2288 snprintf(tmp
, sz
, "%*s", *limit
, " ");
2291 /* replaces the bar format character sequences (like in tmux(1)) */
2293 bar_replace_seq(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t *offrep
,
2298 char tmp
[SWM_BAR_MAX
];
2299 int limit
, size
, count
;
2302 /* reset strlcat(3) buffer */
2305 /* get number, if any */
2308 if (sscanf(fmt
, "%d%n", &limit
, &size
) != 1)
2309 limit
= sizeof tmp
- 1;
2310 if (limit
<= 0 || limit
>= (int)sizeof tmp
)
2311 limit
= sizeof tmp
- 1;
2313 /* there is nothing to replace (ie EOL) */
2320 bar_replace_pad(tmp
, &limit
, sizeof tmp
);
2323 snprintf(tmp
, sizeof tmp
, "%s", bar_ext
);
2326 bar_window_class(tmp
, sizeof tmp
, r
);
2329 bar_workspace_name(tmp
, sizeof tmp
, r
);
2332 bar_window_state(tmp
, sizeof tmp
, r
);
2335 snprintf(tmp
, sizeof tmp
, "%d", r
->ws
->idx
+ 1);
2339 TAILQ_FOREACH(w
, &r
->ws
->winlist
, entry
)
2343 snprintf(tmp
, sizeof tmp
, "%d", count
);
2346 snprintf(tmp
, sizeof tmp
, "%d", r
->s
->idx
+ 1);
2349 bar_window_class_instance(tmp
, sizeof tmp
, r
);
2352 snprintf(tmp
, sizeof tmp
, "%s", r
->ws
->stacker
);
2355 bar_window_instance(tmp
, sizeof tmp
, r
);
2358 bar_urgent(tmp
, sizeof tmp
);
2361 snprintf(tmp
, sizeof tmp
, "%s", bar_vertext
);
2364 bar_window_name(tmp
, sizeof tmp
, r
);
2367 /* unknown character sequence; copy as-is */
2368 snprintf(tmp
, sizeof tmp
, "+%c", *fmt
);
2374 if ((int)len
< limit
)
2376 while (limit
-- > 0) {
2377 if (*offrep
>= sz
- 1)
2379 fmtrep
[(*offrep
)++] = *ptr
++;
2387 bar_replace(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t sz
)
2392 while (*fmt
!= '\0') {
2394 /* skip ordinary characters */
2397 fmtrep
[off
++] = *fmt
++;
2401 /* character sequence found; replace it */
2402 fmt
= bar_replace_seq(fmt
, fmtrep
, r
, &off
, sz
);
2411 bar_fmt_expand(char *fmtexp
, size_t sz
)
2418 /* start by grabbing the current time and date */
2420 localtime_r(&tmt
, &tm
);
2422 /* figure out what to expand */
2423 if (bar_format
!= NULL
)
2425 else if (bar_format
== NULL
&& clock_enabled
)
2427 /* if nothing to expand bail out */
2433 /* copy as-is, just in case the format shouldn't be expanded below */
2434 strlcpy(fmtexp
, fmt
, sz
);
2435 /* finally pass the string through strftime(3) */
2436 #ifndef SWM_DENY_CLOCK_FORMAT
2437 if ((len
= strftime(fmtexp
, sz
, fmt
, &tm
)) == 0)
2438 warnx("format too long");
2443 /* Redraws the bar; need to follow with xcb_flush() or focus_flush(). */
2447 char fmtexp
[SWM_BAR_MAX
], fmtnew
[SWM_BAR_MAX
];
2448 char fmtrep
[SWM_BAR_MAX
];
2450 struct swm_region
*r
;
2452 /* expand the format by first passing it through strftime(3) */
2453 bar_fmt_expand(fmtexp
, sizeof fmtexp
);
2455 num_screens
= get_screen_count();
2456 for (i
= 0; i
< num_screens
; i
++) {
2457 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
2461 if (bar_enabled
&& r
->ws
->bar_enabled
)
2462 xcb_map_window(conn
, r
->bar
->id
);
2464 xcb_unmap_window(conn
, r
->bar
->id
);
2468 if (startup_exception
)
2469 snprintf(fmtrep
, sizeof fmtrep
, "total "
2470 "exceptions: %d, first exception: %s",
2474 bar_fmt(fmtexp
, fmtnew
, r
, sizeof fmtnew
);
2475 bar_replace(fmtnew
, fmtrep
, r
, sizeof fmtrep
);
2477 if (bar_font_legacy
)
2478 bar_print_legacy(r
, fmtrep
);
2480 bar_print(r
, fmtrep
);
2486 * Reads external script output; call when stdin is readable.
2487 * Returns 1 if bar_ext was updated; otherwise 0.
2490 bar_extra_update(void)
2493 char b
[SWM_BAR_MAX
];
2494 bool changed
= false;
2499 while (fgets(b
, sizeof(b
), stdin
) != NULL
) {
2502 if (b
[len
- 1] == '\n') {
2503 /* Remove newline. */
2506 /* "Clear" bar_ext. */
2509 /* Flush buffered output. */
2510 strlcpy(bar_ext
, bar_ext_buf
, sizeof(bar_ext
));
2511 bar_ext_buf
[0] = '\0';
2513 /* Append new output to bar. */
2514 strlcat(bar_ext
, b
, sizeof(bar_ext
));
2518 /* Buffer output. */
2519 strlcat(bar_ext_buf
, b
, sizeof(bar_ext_buf
));
2524 if (errno
!= EAGAIN
) {
2525 warn("bar_action failed");
2534 bar_toggle(struct swm_region
*r
, union arg
*args
)
2536 struct swm_region
*tmpr
;
2539 /* suppress unused warnings since vars are needed */
2543 DNPRINTF(SWM_D_BAR
, "bar_toggle\n");
2546 case SWM_ARG_ID_BAR_TOGGLE_WS
:
2547 /* Only change if master switch is enabled. */
2549 r
->ws
->bar_enabled
= !r
->ws
->bar_enabled
;
2551 bar_enabled
= r
->ws
->bar_enabled
= true;
2553 case SWM_ARG_ID_BAR_TOGGLE
:
2554 bar_enabled
= !bar_enabled
;
2558 /* update bars as necessary */
2559 num_screens
= get_screen_count();
2560 for (i
= 0; i
< num_screens
; i
++)
2561 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
2563 if (bar_enabled
&& tmpr
->ws
->bar_enabled
)
2564 xcb_map_window(conn
, tmpr
->bar
->id
);
2566 xcb_unmap_window(conn
, tmpr
->bar
->id
);
2571 /* must be after stack */
2578 bar_extra_setup(void)
2580 /* do this here because the conf file is in memory */
2581 if (!bar_extra
&& bar_argv
[0]) {
2582 /* launch external status app */
2584 if (pipe(bar_pipe
) == -1)
2585 err(1, "pipe error");
2586 socket_setnonblock(bar_pipe
[0]);
2587 socket_setnonblock(bar_pipe
[1]); /* XXX hmmm, really? */
2589 /* Set stdin to read from the pipe. */
2590 if (dup2(bar_pipe
[0], STDIN_FILENO
) == -1)
2593 /* Set stdout to write to the pipe. */
2594 if (dup2(bar_pipe
[1], STDOUT_FILENO
) == -1)
2597 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
2598 err(1, "could not disable SIGPIPE");
2599 switch (bar_pid
= fork()) {
2601 err(1, "cannot fork");
2605 execvp(bar_argv
[0], bar_argv
);
2606 err(1, "%s external app failed", bar_argv
[0]);
2608 default: /* parent */
2613 atexit(kill_bar_extra_atexit
);
2618 kill_bar_extra_atexit(void)
2621 kill(bar_pid
, SIGTERM
);
2629 while ((s
= index(s
, '-'))) {
2634 return (count
== 14);
2640 char *default_string
;
2641 char **missing_charsets
;
2642 int num_missing_charsets
= 0;
2646 XFreeFontSet(display
, bar_fs
);
2650 DNPRINTF(SWM_D_INIT
, "fontset_init: loading bar_fonts: %s\n", bar_fonts
);
2652 bar_fs
= XCreateFontSet(display
, bar_fonts
, &missing_charsets
,
2653 &num_missing_charsets
, &default_string
);
2655 if (num_missing_charsets
> 0) {
2656 warnx("Unable to load charset(s):");
2658 for (i
= 0; i
< num_missing_charsets
; ++i
)
2659 warnx("%s", missing_charsets
[i
]);
2661 XFreeStringList(missing_charsets
);
2663 if (strcmp(default_string
, ""))
2664 warnx("Glyphs from those sets will be replaced "
2665 "by '%s'.", default_string
);
2667 warnx("Glyphs from those sets won't be drawn.");
2671 errx(1, "Error creating font set structure.");
2673 bar_fs_extents
= XExtentsOfFontSet(bar_fs
);
2675 bar_height
= bar_fs_extents
->max_logical_extent
.height
+
2676 2 * bar_border_width
;
2683 xft_init(struct swm_region
*r
)
2685 char *font
, *d
, *search
;
2688 if (bar_font
== NULL
) {
2689 if ((d
= strdup(bar_fonts
)) == NULL
)
2690 errx(1, "insufficient memory.");
2692 while ((font
= strsep(&search
, ",")) != NULL
) {
2696 DNPRINTF(SWM_D_INIT
, "xft_init: try font %s\n", font
);
2699 bar_font
= XftFontOpenXlfd(display
, r
->s
->idx
,
2702 bar_font
= XftFontOpenName(display
, r
->s
->idx
,
2706 if (bar_font
== NULL
) {
2707 warnx("unable to load font %s", font
);
2710 DNPRINTF(SWM_D_INIT
, "successfully opened "
2718 if (bar_font
== NULL
)
2719 errx(1, "unable to open a font");
2721 PIXEL_TO_XRENDERCOLOR(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
, color
);
2723 if (!XftColorAllocValue(display
, DefaultVisual(display
, r
->s
->idx
),
2724 DefaultColormap(display
, r
->s
->idx
), &color
, &bar_font_color
))
2725 warn("Xft error: unable to allocate color.");
2727 PIXEL_TO_XRENDERCOLOR(r
->s
->c
[SWM_S_COLOR_BAR
].pixel
, color
);
2729 if (!XftColorAllocValue(display
, DefaultVisual(display
, r
->s
->idx
),
2730 DefaultColormap(display
, r
->s
->idx
), &color
, &search_font_color
))
2731 warn("Xft error: unable to allocate color.");
2733 bar_height
= bar_font
->height
+ 2 * bar_border_width
;
2740 bar_setup(struct swm_region
*r
)
2742 xcb_screen_t
*screen
;
2745 DNPRINTF(SWM_D_BAR
, "bar_setup: screen %d.\n",
2748 if ((screen
= get_screen(r
->s
->idx
)) == NULL
)
2749 errx(1, "ERROR: can't get screen %d.", r
->s
->idx
);
2754 if ((r
->bar
= calloc(1, sizeof(struct swm_bar
))) == NULL
)
2755 err(1, "bar_setup: calloc: failed to allocate memory.");
2757 if (bar_font_legacy
)
2763 Y(r
->bar
) = bar_at_bottom
? (Y(r
) + HEIGHT(r
) - bar_height
) : Y(r
);
2764 WIDTH(r
->bar
) = WIDTH(r
) - 2 * bar_border_width
;
2765 HEIGHT(r
->bar
) = bar_height
- 2 * bar_border_width
;
2767 /* Assume region is unfocused when we create the bar. */
2768 r
->bar
->id
= xcb_generate_id(conn
);
2769 wa
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
2770 wa
[1] = r
->s
->c
[SWM_S_COLOR_BAR_BORDER_UNFOCUS
].pixel
;
2771 wa
[2] = XCB_EVENT_MASK_EXPOSURE
| XCB_EVENT_MASK_POINTER_MOTION
|
2772 XCB_EVENT_MASK_POINTER_MOTION_HINT
;
2774 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, r
->bar
->id
, r
->s
->root
,
2775 X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
), HEIGHT(r
->bar
),
2776 bar_border_width
, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
2777 XCB_COPY_FROM_PARENT
, XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
2778 | XCB_CW_EVENT_MASK
, wa
);
2780 /* Stack bar window above region window to start. */
2782 wa
[1] = XCB_STACK_MODE_ABOVE
;
2784 xcb_configure_window(conn
, r
->bar
->id
, XCB_CONFIG_WINDOW_SIBLING
|
2785 XCB_CONFIG_WINDOW_STACK_MODE
, wa
);
2787 r
->bar
->buffer
= xcb_generate_id(conn
);
2788 xcb_create_pixmap(conn
, screen
->root_depth
, r
->bar
->buffer
, r
->bar
->id
,
2789 WIDTH(r
->bar
), HEIGHT(r
->bar
));
2792 xcb_randr_select_input(conn
, r
->bar
->id
,
2793 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE
);
2796 xcb_map_window(conn
, r
->bar
->id
);
2798 DNPRINTF(SWM_D_BAR
, "bar_setup: win %#x, (x,y) w x h: (%d,%d) "
2799 "%d x %d\n", WINID(r
->bar
), X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
),
2806 bar_cleanup(struct swm_region
*r
)
2810 xcb_destroy_window(conn
, r
->bar
->id
);
2811 xcb_free_pixmap(conn
, r
->bar
->buffer
);
2817 set_win_state(struct ws_win
*win
, uint8_t state
)
2819 uint16_t data
[2] = { state
, XCB_ATOM_NONE
};
2821 DNPRINTF(SWM_D_EVENT
, "set_win_state: win %#x, state: %u\n",
2827 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
, a_state
,
2828 a_state
, 32, 2, data
);
2832 get_win_state(xcb_window_t w
)
2834 xcb_get_property_reply_t
*r
;
2835 xcb_get_property_cookie_t c
;
2836 uint32_t result
= 0;
2838 c
= xcb_get_property(conn
, 0, w
, a_state
, a_state
, 0L, 2L);
2839 r
= xcb_get_property_reply(conn
, c
, NULL
);
2841 if (r
->type
== a_state
&& r
->format
== 32 && r
->length
== 2)
2842 result
= *((uint32_t *)xcb_get_property_value(r
));
2846 DNPRINTF(SWM_D_MISC
, "get_win_state property: win %#x state %u\n", w
,
2852 version(struct swm_region
*r
, union arg
*args
)
2854 /* suppress unused warnings since vars are needed */
2858 bar_version
= !bar_version
;
2860 snprintf(bar_vertext
, sizeof bar_vertext
,
2861 "Version: %s Build: %s", SPECTRWM_VERSION
, buildstr
);
2863 strlcpy(bar_vertext
, "", sizeof bar_vertext
);
2870 client_msg(struct ws_win
*win
, xcb_atom_t a
, xcb_timestamp_t t
)
2872 xcb_client_message_event_t ev
;
2880 name
= get_atom_name(a
);
2881 DNPRINTF(SWM_D_EVENT
, "client_msg: win %#x, atom: %s(%u), "
2883 win
->id
, name
, a
, t
);
2887 bzero(&ev
, sizeof ev
);
2888 ev
.response_type
= XCB_CLIENT_MESSAGE
;
2889 ev
.window
= win
->id
;
2892 ev
.data
.data32
[0] = a
;
2893 ev
.data
.data32
[1] = t
;
2895 xcb_send_event(conn
, 0, win
->id
,
2896 XCB_EVENT_MASK_NO_EVENT
, (const char *)&ev
);
2899 /* synthetic response to a ConfigureRequest when not making a change */
2901 config_win(struct ws_win
*win
, xcb_configure_request_event_t
*ev
)
2903 xcb_configure_notify_event_t ce
;
2908 /* send notification of unchanged state. */
2909 bzero(&ce
, sizeof(ce
));
2910 ce
.response_type
= XCB_CONFIGURE_NOTIFY
;
2913 ce
.width
= WIDTH(win
);
2914 ce
.height
= HEIGHT(win
);
2915 ce
.override_redirect
= 0;
2920 ce
.window
= win
->id
;
2921 ce
.border_width
= BORDER(win
);
2922 ce
.above_sibling
= XCB_WINDOW_NONE
;
2925 ce
.event
= ev
->window
;
2926 ce
.window
= ev
->window
;
2928 /* make response appear more WM_SIZE_HINTS-compliant */
2929 if (win
->sh
.flags
) {
2930 DNPRINTF(SWM_D_MISC
, "config_win: hints: win %#x,"
2931 " sh.flags: %u, min: %d x %d, max: %d x %d, inc: "
2932 "%d x %d\n", win
->id
, win
->sh
.flags
, SH_MIN_W(win
),
2933 SH_MIN_H(win
), SH_MAX_W(win
), SH_MAX_H(win
),
2934 SH_INC_W(win
), SH_INC_H(win
));
2939 /* the hint may be set... to 0! */
2940 if (SH_MIN_W(win
) > 0 && ce
.width
< SH_MIN_W(win
))
2941 ce
.width
= SH_MIN_W(win
);
2942 if (SH_MIN_H(win
) > 0 && ce
.height
< SH_MIN_H(win
))
2943 ce
.height
= SH_MIN_H(win
);
2948 /* may also be advertized as 0 */
2949 if (SH_MAX_W(win
) > 0 && ce
.width
> SH_MAX_W(win
))
2950 ce
.width
= SH_MAX_W(win
);
2951 if (SH_MAX_H(win
) > 0 && ce
.height
> SH_MAX_H(win
))
2952 ce
.height
= SH_MAX_H(win
);
2955 /* resize increment. */
2957 if (SH_INC_W(win
) > 1 && ce
.width
> SH_INC_W(win
))
2958 ce
.width
-= (ce
.width
- SH_MIN_W(win
)) %
2960 if (SH_INC_H(win
) > 1 && ce
.height
> SH_INC_H(win
))
2961 ce
.height
-= (ce
.height
- SH_MIN_H(win
)) %
2965 /* adjust x and y for requested border_width. */
2966 ce
.x
+= BORDER(win
) - ev
->border_width
;
2967 ce
.y
+= BORDER(win
) - ev
->border_width
;
2968 ce
.border_width
= ev
->border_width
;
2969 ce
.above_sibling
= ev
->sibling
;
2972 DNPRINTF(SWM_D_MISC
, "config_win: ewmh: %s, win %#x, (x,y) w x h: "
2973 "(%d,%d) %d x %d, border: %d\n", YESNO(ev
== NULL
), win
->id
, ce
.x
,
2974 ce
.y
, ce
.width
, ce
.height
, ce
.border_width
);
2976 xcb_send_event(conn
, 0, win
->id
, XCB_EVENT_MASK_STRUCTURE_NOTIFY
,
2981 count_win(struct workspace
*ws
, bool count_transient
)
2986 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
2987 if (!count_transient
&& FLOATING(win
))
2993 DNPRINTF(SWM_D_MISC
, "count_win: %d\n", count
);
2999 quit(struct swm_region
*r
, union arg
*args
)
3001 /* suppress unused warnings since vars are needed */
3005 DNPRINTF(SWM_D_MISC
, "quit\n");
3010 raise_window(struct ws_win
*win
)
3012 struct ws_win
*target
= NULL
;
3013 struct swm_region
*r
;
3014 struct workspace
*ws
;
3016 if (win
== NULL
|| (r
= win
->ws
->r
) == NULL
)
3020 DNPRINTF(SWM_D_EVENT
, "raise_window: win %#x\n", win
->id
);
3022 TAILQ_FOREACH(target
, &ws
->stack
, stack_entry
) {
3023 if (target
== win
|| ICONIC(target
))
3025 if (ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
3027 if (TRANS(win
) && (win
->transient
== target
->transient
||
3028 win
->transient
== target
->id
))
3030 if (FULLSCREEN(win
))
3032 if (FULLSCREEN(target
))
3036 if (MAXIMIZED(target
))
3038 if (ABOVE(win
) || TRANS(win
))
3040 if (!ABOVE(target
) && !TRANS(target
))
3044 if (target
!= NULL
) {
3045 /* Change stack position. */
3046 TAILQ_REMOVE(&ws
->stack
, win
, stack_entry
);
3047 TAILQ_INSERT_BEFORE(target
, win
, stack_entry
);
3048 update_win_stacking(win
);
3052 if (swm_debug
& SWM_D_STACK
) {
3053 DPRINTF("=== stacking order (top down) === \n");
3054 TAILQ_FOREACH(target
, &r
->ws
->stack
, stack_entry
) {
3055 DPRINTF("win %#x, fs: %s, maximized: %s, above: %s, "
3056 "iconic: %s\n", target
->id
, YESNO(FULLSCREEN(target
)),
3057 YESNO(MAXIMIZED(target
)), YESNO(ABOVE(target
)),
3058 YESNO(ICONIC(target
)));
3062 DNPRINTF(SWM_D_EVENT
, "raise_window: done\n");
3066 update_win_stacking(struct ws_win
*win
)
3068 struct ws_win
*sibling
;
3069 struct swm_region
*r
;
3072 if (win
== NULL
|| (r
= win
->ws
->r
) == NULL
)
3075 sibling
= TAILQ_NEXT(win
, stack_entry
);
3076 if (sibling
!= NULL
&& FLOATING(win
) == FLOATING(sibling
))
3077 val
[0] = sibling
->id
;
3079 val
[0] = FLOATING(win
) ? r
->bar
->id
: r
->id
;
3081 DNPRINTF(SWM_D_EVENT
, "update_win_stacking: %#x, sibling %#x\n",
3084 val
[1] = XCB_STACK_MODE_ABOVE
;
3086 xcb_configure_window(conn
, win
->id
, XCB_CONFIG_WINDOW_SIBLING
|
3087 XCB_CONFIG_WINDOW_STACK_MODE
, val
);
3091 map_window(struct ws_win
*win
)
3096 DNPRINTF(SWM_D_EVENT
, "map_window: win %#x, mapped: %s\n",
3097 win
->id
, YESNO(win
->mapped
));
3102 xcb_map_window(conn
, win
->id
);
3103 set_win_state(win
, XCB_ICCCM_WM_STATE_NORMAL
);
3108 unmap_window(struct ws_win
*win
)
3113 DNPRINTF(SWM_D_EVENT
, "unmap_window: win %#x, mapped: %s\n", win
->id
,
3114 YESNO(win
->mapped
));
3119 xcb_unmap_window(conn
, win
->id
);
3120 set_win_state(win
, XCB_ICCCM_WM_STATE_ICONIC
);
3121 win
->mapped
= false;
3128 int i
, j
, num_screens
;
3130 num_screens
= get_screen_count();
3131 for (i
= 0; i
< num_screens
; i
++)
3132 for (j
= 0; j
< workspace_limit
; j
++)
3133 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
3138 fake_keypress(struct ws_win
*win
, xcb_keysym_t keysym
, uint16_t modifiers
)
3140 xcb_key_press_event_t event
;
3141 xcb_keycode_t
*keycode
;
3146 keycode
= xcb_key_symbols_get_keycode(syms
, keysym
);
3148 DNPRINTF(SWM_D_MISC
, "fake_keypress: win %#x, keycode %u\n",
3151 bzero(&event
, sizeof(event
));
3152 event
.event
= win
->id
;
3153 event
.root
= win
->s
->root
;
3154 event
.child
= XCB_WINDOW_NONE
;
3155 event
.time
= XCB_CURRENT_TIME
;
3156 event
.event_x
= X(win
);
3157 event
.event_y
= Y(win
);
3160 event
.same_screen
= 1;
3161 event
.detail
= *keycode
;
3162 event
.state
= modifiers
;
3164 event
.response_type
= XCB_KEY_PRESS
;
3165 xcb_send_event(conn
, 1, win
->id
,
3166 XCB_EVENT_MASK_KEY_PRESS
, (const char *)&event
);
3168 event
.response_type
= XCB_KEY_RELEASE
;
3169 xcb_send_event(conn
, 1, win
->id
,
3170 XCB_EVENT_MASK_KEY_RELEASE
, (const char *)&event
);
3176 restart(struct swm_region
*r
, union arg
*args
)
3178 /* suppress unused warning since var is needed */
3182 DNPRINTF(SWM_D_MISC
, "restart: %s\n", start_argv
[0]);
3186 execvp(start_argv
[0], start_argv
);
3187 warn("execvp failed");
3192 get_pointer_win(xcb_window_t root
)
3194 struct ws_win
*win
= NULL
;
3195 xcb_query_pointer_reply_t
*r
;
3197 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: root: %#x.\n", root
);
3199 r
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, root
), NULL
);
3201 win
= find_window(r
->child
);
3203 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: %#x.\n",
3206 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: none.\n");
3215 center_pointer(struct swm_region
*r
)
3219 if (!warp_pointer
|| r
== NULL
)
3224 DNPRINTF(SWM_D_EVENT
, "center_pointer: win %#x.\n", WINID(win
));
3226 if (win
&& win
->mapped
)
3227 xcb_warp_pointer(conn
, XCB_NONE
, win
->id
, 0, 0, 0, 0,
3228 WIDTH(win
) / 2, HEIGHT(win
) / 2);
3230 xcb_warp_pointer(conn
, XCB_NONE
, r
->id
, 0, 0, 0, 0,
3231 WIDTH(r
) / 2, HEIGHT(r
) / 2);
3235 root_to_region(xcb_window_t root
, int check
)
3238 struct swm_region
*r
= NULL
;
3240 xcb_query_pointer_reply_t
*qpr
;
3241 xcb_get_input_focus_reply_t
*gifr
;
3243 DNPRINTF(SWM_D_MISC
, "root_to_region: win %#x\n", root
);
3245 num_screens
= get_screen_count();
3246 for (i
= 0; i
< num_screens
; i
++)
3247 if (screens
[i
].root
== root
)
3250 if (check
& SWM_CK_REGION
)
3251 r
= screens
[i
].r_focus
;
3253 if (r
== NULL
&& check
& SWM_CK_FOCUS
) {
3254 /* Try to find an actively focused window */
3255 gifr
= xcb_get_input_focus_reply(conn
,
3256 xcb_get_input_focus(conn
), NULL
);
3258 cfw
= find_window(gifr
->focus
);
3259 if (cfw
&& cfw
->ws
->r
)
3266 if (r
== NULL
&& check
& SWM_CK_POINTER
) {
3267 /* No region with an active focus; try to use pointer. */
3268 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
,
3269 screens
[i
].root
), NULL
);
3271 DNPRINTF(SWM_D_MISC
, "root_to_region: pointer: "
3272 "(%d,%d)\n", qpr
->root_x
, qpr
->root_y
);
3273 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3274 if (X(r
) <= qpr
->root_x
&&
3275 qpr
->root_x
< MAX_X(r
) &&
3276 Y(r
) <= qpr
->root_y
&&
3277 qpr
->root_y
< MAX_Y(r
))
3284 if (r
== NULL
&& check
& SWM_CK_FALLBACK
)
3285 r
= TAILQ_FIRST(&screens
[i
].rl
);
3287 DNPRINTF(SWM_D_MISC
, "root_to_region: idx: %d\n", get_region_index(r
));
3293 find_unmanaged_window(xcb_window_t id
)
3296 int i
, j
, num_screens
;
3298 num_screens
= get_screen_count();
3299 for (i
= 0; i
< num_screens
; i
++)
3300 for (j
= 0; j
< workspace_limit
; j
++)
3301 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].unmanagedlist
,
3309 find_window(xcb_window_t id
)
3312 int i
, j
, num_screens
;
3313 xcb_query_tree_reply_t
*r
;
3315 num_screens
= get_screen_count();
3316 for (i
= 0; i
< num_screens
; i
++)
3317 for (j
= 0; j
< workspace_limit
; j
++)
3318 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
3322 r
= xcb_query_tree_reply(conn
, xcb_query_tree(conn
, id
), NULL
);
3326 /* if we were looking for the parent return that window instead */
3327 if (r
->parent
== 0 || r
->root
== r
->parent
) {
3332 /* look for parent */
3333 for (i
= 0; i
< num_screens
; i
++)
3334 for (j
= 0; j
< workspace_limit
; j
++)
3335 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
3336 if (r
->parent
== win
->id
) {
3346 spawn(int ws_idx
, union arg
*args
, bool close_fd
)
3351 DNPRINTF(SWM_D_MISC
, "spawn: %s\n", args
->argv
[0]);
3353 close(xcb_get_file_descriptor(conn
));
3355 setenv("LD_PRELOAD", SWM_LIB
, 1);
3357 if (asprintf(&ret
, "%d", ws_idx
) == -1) {
3358 warn("spawn: asprintf SWM_WS");
3361 setenv("_SWM_WS", ret
, 1);
3365 if (asprintf(&ret
, "%d", getpid()) == -1) {
3366 warn("spawn: asprintf _SWM_PID");
3369 setenv("_SWM_PID", ret
, 1);
3373 if (setsid() == -1) {
3374 warn("spawn: setsid");
3380 * close stdin and stdout to prevent interaction between apps
3381 * and the baraction script
3382 * leave stderr open to record errors
3384 if ((fd
= open(_PATH_DEVNULL
, O_RDWR
, 0)) == -1) {
3385 warn("spawn: open");
3388 dup2(fd
, STDIN_FILENO
);
3389 dup2(fd
, STDOUT_FILENO
);
3394 execvp(args
->argv
[0], args
->argv
);
3396 warn("spawn: execvp");
3401 kill_refs(struct ws_win
*win
)
3403 int i
, x
, num_screens
;
3404 struct swm_region
*r
;
3405 struct workspace
*ws
;
3410 num_screens
= get_screen_count();
3411 for (i
= 0; i
< num_screens
; i
++)
3412 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3413 for (x
= 0; x
< workspace_limit
; x
++) {
3415 if (win
== ws
->focus
)
3417 if (win
== ws
->focus_prev
)
3418 ws
->focus_prev
= NULL
;
3423 validate_win(struct ws_win
*testwin
)
3426 struct workspace
*ws
;
3427 struct swm_region
*r
;
3428 int i
, x
, num_screens
;
3430 if (testwin
== NULL
)
3433 num_screens
= get_screen_count();
3434 for (i
= 0; i
< num_screens
; i
++)
3435 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3436 for (x
= 0; x
< workspace_limit
; x
++) {
3438 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
3446 validate_ws(struct workspace
*testws
)
3448 struct swm_region
*r
;
3449 struct workspace
*ws
;
3450 int i
, x
, num_screens
;
3452 /* validate all ws */
3453 num_screens
= get_screen_count();
3454 for (i
= 0; i
< num_screens
; i
++)
3455 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3456 for (x
= 0; x
< workspace_limit
; x
++) {
3465 unfocus_win(struct ws_win
*win
)
3467 xcb_window_t none
= XCB_WINDOW_NONE
;
3469 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: win %#x\n", WINID(win
));
3474 if (win
->ws
== NULL
) {
3475 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: NULL ws.\n");
3479 if (validate_ws(win
->ws
)) {
3480 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: invalid ws.\n");
3484 if (win
->ws
->r
== NULL
) {
3485 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: NULL region.\n");
3489 if (validate_win(win
)) {
3490 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: invalid win.\n");
3495 if (win
->ws
->focus
== win
) {
3496 win
->ws
->focus
= NULL
;
3497 win
->ws
->focus_prev
= win
;
3500 if (validate_win(win
->ws
->focus
)) {
3501 kill_refs(win
->ws
->focus
);
3502 win
->ws
->focus
= NULL
;
3505 if (validate_win(win
->ws
->focus_prev
)) {
3506 kill_refs(win
->ws
->focus_prev
);
3507 win
->ws
->focus_prev
= NULL
;
3510 update_window_color(win
);
3512 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
3513 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1, &none
);
3515 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: done.\n");
3519 focus_win(struct ws_win
*win
)
3521 struct ws_win
*cfw
= NULL
, *parent
= NULL
, *w
, *tmpw
;
3522 struct workspace
*ws
;
3523 xcb_get_input_focus_reply_t
*gifr
;
3525 DNPRINTF(SWM_D_FOCUS
, "focus_win: win %#x\n", WINID(win
));
3527 if (win
== NULL
|| win
->ws
== NULL
|| !win
->mapped
)
3532 if (validate_ws(ws
))
3535 if (validate_win(win
)) {
3540 gifr
= xcb_get_input_focus_reply(conn
, xcb_get_input_focus(conn
), NULL
);
3542 cfw
= find_window(gifr
->focus
);
3543 if (cfw
!= NULL
&& cfw
!= win
) {
3544 if (cfw
->ws
!= ws
&& cfw
->ws
->r
!= NULL
) {
3545 /* Change border to unfocused color. */
3546 xcb_change_window_attributes(conn
, cfw
->id
,
3547 XCB_CW_BORDER_PIXEL
,
3548 &cfw
->s
->c
[(MAXIMIZED(cfw
) ?
3549 SWM_S_COLOR_UNFOCUS_MAXIMIZED
:
3550 SWM_S_COLOR_UNFOCUS
)].pixel
);
3558 if (ws
->focus
!= win
) {
3559 if (ws
->focus
&& ws
->focus
!= cfw
)
3560 unfocus_win(ws
->focus
);
3564 /* If this window directs focus to a child window, then clear. */
3565 if (win
->focus_child
)
3566 win
->focus_child
= NULL
;
3568 /* If transient, adjust parent's focus child for focus_magic. */
3570 parent
= find_window(win
->transient
);
3571 if (parent
&& parent
->focus_child
!= win
)
3572 parent
->focus_child
= win
;
3575 if (cfw
!= win
&& ws
->r
!= NULL
) {
3576 /* Set input focus if no input hint, or indicated by hint. */
3577 if (!(win
->hints
.flags
& XCB_ICCCM_WM_HINT_INPUT
) ||
3578 (win
->hints
.flags
& XCB_ICCCM_WM_HINT_INPUT
&&
3580 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
,
3581 win
->id
, last_event_time
);
3583 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
,
3584 ws
->r
->id
, XCB_CURRENT_TIME
);
3586 /* Tell app it can adjust focus to a specific window. */
3587 if (win
->take_focus
) {
3588 /* java is special; always tell parent */
3589 if (TRANS(win
) && win
->java
)
3590 client_msg(parent
, a_takefocus
,
3593 client_msg(win
, a_takefocus
, last_event_time
);
3596 if (ws
->cur_layout
->flags
& SWM_L_MAPONFOCUS
||
3598 /* If a parent exists, map it first. */
3600 raise_window(parent
);
3603 /* Map siblings next. */
3604 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
,
3606 if (w
!= win
&& !ICONIC(w
) &&
3607 w
->transient
== parent
->id
) {
3613 /* Map focused window. */
3617 /* Stack any children of focus window. */
3618 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
, tmpw
)
3619 if (w
->transient
== win
->id
&& !ICONIC(w
)) {
3623 } else if (tile_gap
< 0 && !ABOVE(win
)) {
3625 * Windows overlap in the layout.
3626 * Raise focused win above all tiled wins.
3634 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
3635 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1,
3640 /* Update window border even if workspace is hidden. */
3641 update_window_color(win
);
3646 DNPRINTF(SWM_D_FOCUS
, "focus_win: done.\n");
3649 /* If a child window should have focus instead, return it. */
3651 get_focus_magic(struct ws_win
*win
)
3653 struct ws_win
*parent
= NULL
;
3654 struct ws_win
*child
= NULL
;
3656 DNPRINTF(SWM_D_FOCUS
, "get_focus_magic: win %#x\n", WINID(win
));
3661 parent
= find_window(win
->transient
);
3663 /* If parent prefers focus elsewhere, then try to do so. */
3664 if (parent
&& (child
= parent
->focus_child
)) {
3665 if (validate_win(child
) == 0 && child
->mapped
)
3668 parent
->focus_child
= NULL
;
3672 /* If this window prefers focus elsewhere, then try to do so. */
3673 if ((child
= win
->focus_child
)) {
3674 if (validate_win(child
) == 0 && child
->mapped
)
3677 win
->focus_child
= NULL
;
3684 event_drain(uint8_t rt
)
3686 xcb_generic_event_t
*evt
;
3688 /* ensure all pending requests have been processed before filtering. */
3690 while ((evt
= xcb_poll_for_event(conn
))) {
3691 if (XCB_EVENT_RESPONSE_TYPE(evt
) != rt
)
3699 set_region(struct swm_region
*r
)
3701 struct swm_region
*rf
;
3708 /* Unfocus old region bar. */
3713 xcb_change_window_attributes(conn
, rf
->bar
->id
,
3714 XCB_CW_BORDER_PIXEL
,
3715 &r
->s
->c
[SWM_S_COLOR_BAR_BORDER_UNFOCUS
].pixel
);
3718 if (rf
!= NULL
&& rf
!= r
&& (X(rf
) != X(r
) || Y(rf
) != Y(r
) ||
3719 WIDTH(rf
) != WIDTH(r
) || HEIGHT(rf
) != HEIGHT(r
))) {
3720 /* Set _NET_DESKTOP_GEOMETRY. */
3722 vals
[1] = HEIGHT(r
);
3723 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, r
->s
->root
,
3724 ewmh
[_NET_DESKTOP_GEOMETRY
].atom
, XCB_ATOM_CARDINAL
, 32, 2,
3728 /* Set region bar border to focus_color. */
3729 xcb_change_window_attributes(conn
, r
->bar
->id
,
3730 XCB_CW_BORDER_PIXEL
, &r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].pixel
);
3734 ewmh_update_current_desktop();
3738 focus_region(struct swm_region
*r
)
3741 struct swm_region
*old_r
;
3746 old_r
= r
->s
->r_focus
;
3749 nfw
= get_region_focus(r
);
3753 /* New region is empty; need to manually unfocus win. */
3755 unfocus_win(old_r
->ws
->focus
);
3757 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
, r
->id
,
3760 /* Clear bar since empty. */
3766 switchws(struct swm_region
*r
, union arg
*args
)
3768 struct swm_region
*this_r
, *other_r
;
3770 struct workspace
*new_ws
, *old_ws
;
3771 xcb_window_t none
= XCB_WINDOW_NONE
;
3772 int wsid
= args
->id
;
3773 bool unmap_old
= false;
3778 if (wsid
>= workspace_limit
)
3782 old_ws
= this_r
->ws
;
3783 new_ws
= &this_r
->s
->ws
[wsid
];
3785 DNPRINTF(SWM_D_WS
, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
3786 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), old_ws
->idx
, wsid
);
3788 if (new_ws
== NULL
|| old_ws
== NULL
)
3790 if (new_ws
== old_ws
)
3793 if ((win
= old_ws
->focus
) != NULL
) {
3794 update_window_color(win
);
3796 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
3797 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1,
3801 other_r
= new_ws
->r
;
3802 if (other_r
== NULL
) {
3803 /* the other workspace is hidden, hide this one */
3807 /* the other ws is visible in another region, exchange them */
3808 other_r
->ws_prior
= new_ws
;
3809 other_r
->ws
= old_ws
;
3810 old_ws
->r
= other_r
;
3812 this_r
->ws_prior
= old_ws
;
3813 this_r
->ws
= new_ws
;
3816 /* Set focus_pending before stacking, if needed. */
3817 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& (!new_ws
->focus_pending
||
3818 validate_win(new_ws
->focus_pending
))) {
3819 new_ws
->focus_pending
= get_region_focus(new_ws
->r
);
3820 new_ws
->focus
= new_ws
->focus_prev
;
3821 new_ws
->focus_prev
= NULL
;
3824 new_ws
->state
= SWM_WS_STATE_MAPPING
;
3827 /* unmap old windows */
3829 TAILQ_FOREACH(win
, &old_ws
->winlist
, entry
)
3831 old_ws
->state
= SWM_WS_STATE_HIDDEN
;
3834 /* if workspaces were swapped, then don't wait to set focus */
3835 if (old_ws
->r
&& focus_mode
!= SWM_FOCUS_FOLLOW
) {
3836 if (new_ws
->focus_pending
) {
3837 focus_win(new_ws
->focus_pending
);
3838 new_ws
->focus_pending
= NULL
;
3842 /* Clear bar and set focus on region input win if new ws is empty. */
3843 if (new_ws
->focus_pending
== NULL
&& new_ws
->focus
== NULL
) {
3844 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
, r
->id
,
3849 ewmh_update_current_desktop();
3853 new_ws
->state
= SWM_WS_STATE_MAPPED
;
3855 DNPRINTF(SWM_D_WS
, "switchws: done.\n");
3859 cyclews(struct swm_region
*r
, union arg
*args
)
3862 struct swm_screen
*s
= r
->s
;
3863 bool cycle_all
= false, mv
= false;
3865 DNPRINTF(SWM_D_WS
, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3866 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
3872 case SWM_ARG_ID_CYCLEWS_MOVE_UP
:
3875 case SWM_ARG_ID_CYCLEWS_UP_ALL
:
3878 case SWM_ARG_ID_CYCLEWS_UP
:
3879 a
.id
= (a
.id
< workspace_limit
- 1) ? a
.id
+ 1 : 0;
3881 case SWM_ARG_ID_CYCLEWS_MOVE_DOWN
:
3884 case SWM_ARG_ID_CYCLEWS_DOWN_ALL
:
3887 case SWM_ARG_ID_CYCLEWS_DOWN
:
3888 a
.id
= (a
.id
> 0) ? a
.id
- 1 : workspace_limit
- 1;
3895 (!cycle_empty
&& TAILQ_EMPTY(&s
->ws
[a
.id
].winlist
)))
3897 if (!cycle_visible
&& s
->ws
[a
.id
].r
!= NULL
)
3904 } while (a
.id
!= r
->ws
->idx
);
3906 DNPRINTF(SWM_D_FOCUS
, "cyclews: done\n");
3910 priorws(struct swm_region
*r
, union arg
*args
)
3916 DNPRINTF(SWM_D_WS
, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3917 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
3919 if (r
->ws_prior
== NULL
)
3922 a
.id
= r
->ws_prior
->idx
;
3924 DNPRINTF(SWM_D_FOCUS
, "priorws: done\n");
3928 focusrg(struct swm_region
*r
, union arg
*args
)
3930 int ridx
= args
->id
, i
, num_screens
;
3931 struct swm_region
*rr
= NULL
;
3933 num_screens
= get_screen_count();
3934 /* do nothing if we don't have more than one screen */
3935 if (!(num_screens
> 1 || outputs
> 1))
3938 DNPRINTF(SWM_D_FOCUS
, "focusrg: id: %d\n", ridx
);
3940 rr
= TAILQ_FIRST(&r
->s
->rl
);
3941 for (i
= 0; i
< ridx
&& rr
!= NULL
; ++i
)
3942 rr
= TAILQ_NEXT(rr
, entry
);
3950 DNPRINTF(SWM_D_FOCUS
, "focusrg: done\n");
3954 cyclerg(struct swm_region
*r
, union arg
*args
)
3956 struct swm_region
*rr
= NULL
;
3959 num_screens
= get_screen_count();
3960 /* do nothing if we don't have more than one screen */
3961 if (!(num_screens
> 1 || outputs
> 1))
3965 DNPRINTF(SWM_D_FOCUS
, "cyclerg: id: %d, region: %d\n", args
->id
, i
);
3968 case SWM_ARG_ID_CYCLERG_UP
:
3969 rr
= TAILQ_NEXT(r
, entry
);
3971 rr
= TAILQ_FIRST(&screens
[i
].rl
);
3973 case SWM_ARG_ID_CYCLERG_DOWN
:
3974 rr
= TAILQ_PREV(r
, swm_region_list
, entry
);
3976 rr
= TAILQ_LAST(&screens
[i
].rl
, swm_region_list
);
3987 DNPRINTF(SWM_D_FOCUS
, "cyclerg: done\n");
3990 /* Sorts transients after parent. */
3992 sort_windows(struct ws_win_list
*wl
)
3994 struct ws_win
*win
, *parent
, *nxt
;
3999 for (win
= TAILQ_FIRST(wl
); win
!= TAILQ_END(wl
); win
= nxt
) {
4000 nxt
= TAILQ_NEXT(win
, entry
);
4002 parent
= find_window(win
->transient
);
4003 if (parent
== NULL
) {
4004 warnx("not possible bug");
4007 TAILQ_REMOVE(wl
, win
, entry
);
4008 TAILQ_INSERT_AFTER(wl
, parent
, win
, entry
);
4014 swapwin(struct swm_region
*r
, union arg
*args
)
4016 struct ws_win
*target
, *source
;
4017 struct ws_win
*cur_focus
;
4018 struct ws_win_list
*wl
;
4020 DNPRINTF(SWM_D_WS
, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
4021 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
4023 cur_focus
= r
->ws
->focus
;
4024 if (cur_focus
== NULL
|| FULLSCREEN(cur_focus
))
4027 /* Adjust stacking in floating layer. */
4028 if (ABOVE(cur_focus
)) {
4030 case SWM_ARG_ID_SWAPPREV
:
4031 target
= TAILQ_PREV(cur_focus
, ws_win_stack
,
4033 if (target
!= NULL
&& FLOATING(target
)) {
4034 TAILQ_REMOVE(&cur_focus
->ws
->stack
, cur_focus
,
4036 TAILQ_INSERT_BEFORE(target
, cur_focus
,
4038 update_win_stacking(cur_focus
);
4042 case SWM_ARG_ID_SWAPNEXT
:
4043 target
= TAILQ_NEXT(cur_focus
, stack_entry
);
4044 if (target
!= NULL
&& FLOATING(target
)) {
4045 TAILQ_REMOVE(&cur_focus
->ws
->stack
, cur_focus
,
4047 TAILQ_INSERT_AFTER(&cur_focus
->ws
->stack
,
4048 target
, cur_focus
, stack_entry
);
4049 update_win_stacking(cur_focus
);
4057 if (r
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
4060 clear_maximized(r
->ws
);
4063 wl
= &source
->ws
->winlist
;
4066 case SWM_ARG_ID_SWAPPREV
:
4068 source
= find_window(source
->transient
);
4069 target
= TAILQ_PREV(source
, ws_win_list
, entry
);
4070 if (target
&& target
->transient
)
4071 target
= find_window(target
->transient
);
4072 TAILQ_REMOVE(wl
, source
, entry
);
4074 TAILQ_INSERT_TAIL(wl
, source
, entry
);
4076 TAILQ_INSERT_BEFORE(target
, source
, entry
);
4078 case SWM_ARG_ID_SWAPNEXT
:
4079 target
= TAILQ_NEXT(source
, entry
);
4080 /* move the parent and let the sort handle the move */
4082 source
= find_window(source
->transient
);
4083 TAILQ_REMOVE(wl
, source
, entry
);
4085 TAILQ_INSERT_HEAD(wl
, source
, entry
);
4087 TAILQ_INSERT_AFTER(wl
, target
, source
, entry
);
4089 case SWM_ARG_ID_SWAPMAIN
:
4090 target
= TAILQ_FIRST(wl
);
4091 if (target
== source
) {
4092 if (source
->ws
->focus_prev
!= NULL
&&
4093 source
->ws
->focus_prev
!= target
)
4094 source
= source
->ws
->focus_prev
;
4098 if (target
== NULL
|| source
== NULL
)
4100 source
->ws
->focus_prev
= target
;
4101 TAILQ_REMOVE(wl
, target
, entry
);
4102 TAILQ_INSERT_BEFORE(source
, target
, entry
);
4103 TAILQ_REMOVE(wl
, source
, entry
);
4104 TAILQ_INSERT_HEAD(wl
, source
, entry
);
4106 case SWM_ARG_ID_MOVELAST
:
4107 TAILQ_REMOVE(wl
, source
, entry
);
4108 TAILQ_INSERT_TAIL(wl
, source
, entry
);
4111 DNPRINTF(SWM_D_MOVE
, "swapwin: invalid id: %d\n", args
->id
);
4116 ewmh_update_client_list();
4122 DNPRINTF(SWM_D_MOVE
, "swapwin: done\n");
4126 get_focus_prev(struct ws_win
*win
)
4128 struct ws_win
*winfocus
= NULL
;
4129 struct ws_win
*cur_focus
= NULL
;
4130 struct ws_win_list
*wl
= NULL
;
4131 struct workspace
*ws
= NULL
;
4133 if (!(win
&& win
->ws
))
4138 cur_focus
= ws
->focus
;
4140 DNPRINTF(SWM_D_FOCUS
, "get_focus_prev: win %#x, cur_focus: %#x, "
4141 "focus_prev: %#x\n", WINID(win
), WINID(cur_focus
),
4142 WINID(ws
->focus_prev
));
4144 /* pickle, just focus on whatever */
4145 if (cur_focus
== NULL
) {
4146 /* use prev_focus if valid */
4147 if (ws
->focus_prev
&& find_window(ws
->focus_prev
->id
))
4148 winfocus
= ws
->focus_prev
;
4152 /* if transient focus on parent */
4153 if (TRANS(cur_focus
)) {
4154 winfocus
= find_window(cur_focus
->transient
);
4158 /* if in max_stack try harder */
4159 if ((win
->quirks
& SWM_Q_FOCUSPREV
) ||
4160 (ws
->cur_layout
->flags
& SWM_L_FOCUSPREV
)) {
4161 if (cur_focus
!= ws
->focus_prev
)
4162 winfocus
= ws
->focus_prev
;
4164 winfocus
= TAILQ_PREV(win
, ws_win_list
, entry
);
4169 DNPRINTF(SWM_D_FOCUS
, "get_focus_prev: focus_close: %d\n", focus_close
);
4171 if (winfocus
== NULL
|| winfocus
== win
) {
4172 switch (focus_close
) {
4173 case SWM_STACK_BOTTOM
:
4174 TAILQ_FOREACH(winfocus
, wl
, entry
)
4175 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4179 TAILQ_FOREACH_REVERSE(winfocus
, wl
, ws_win_list
, entry
)
4180 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4183 case SWM_STACK_ABOVE
:
4184 winfocus
= TAILQ_NEXT(cur_focus
, entry
);
4185 while (winfocus
&& ICONIC(winfocus
))
4186 winfocus
= TAILQ_NEXT(winfocus
, entry
);
4188 if (winfocus
== NULL
) {
4189 if (focus_close_wrap
) {
4190 TAILQ_FOREACH(winfocus
, wl
, entry
)
4191 if (!ICONIC(winfocus
) &&
4192 winfocus
!= cur_focus
)
4195 TAILQ_FOREACH_REVERSE(winfocus
, wl
,
4197 if (!ICONIC(winfocus
) &&
4198 winfocus
!= cur_focus
)
4203 case SWM_STACK_BELOW
:
4204 winfocus
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
4205 while (winfocus
&& ICONIC(winfocus
))
4206 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
,
4209 if (winfocus
== NULL
) {
4210 if (focus_close_wrap
) {
4211 TAILQ_FOREACH_REVERSE(winfocus
, wl
,
4213 if (!ICONIC(winfocus
) &&
4214 winfocus
!= cur_focus
)
4217 TAILQ_FOREACH(winfocus
, wl
, entry
)
4218 if (!ICONIC(winfocus
) &&
4219 winfocus
!= cur_focus
)
4227 if (winfocus
== NULL
||
4228 (winfocus
&& (ICONIC(winfocus
) || winfocus
== cur_focus
))) {
4229 if (focus_default
== SWM_STACK_TOP
) {
4230 TAILQ_FOREACH_REVERSE(winfocus
, wl
, ws_win_list
, entry
)
4231 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4234 TAILQ_FOREACH(winfocus
, wl
, entry
)
4235 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4242 return get_focus_magic(winfocus
);
4246 get_region_focus(struct swm_region
*r
)
4248 struct ws_win
*winfocus
= NULL
;
4253 if (r
->ws
->focus
&& !ICONIC(r
->ws
->focus
))
4254 winfocus
= r
->ws
->focus
;
4255 else if (r
->ws
->focus_prev
&& !ICONIC(r
->ws
->focus_prev
))
4256 winfocus
= r
->ws
->focus_prev
;
4258 TAILQ_FOREACH(winfocus
, &r
->ws
->winlist
, entry
)
4259 if (!ICONIC(winfocus
))
4262 return get_focus_magic(winfocus
);
4266 focus(struct swm_region
*r
, union arg
*args
)
4268 struct ws_win
*head
, *cur_focus
= NULL
, *winfocus
= NULL
;
4269 struct ws_win_list
*wl
= NULL
;
4270 struct workspace
*ws
= NULL
;
4273 xcb_icccm_wm_hints_t hints
;
4278 cur_focus
= r
->ws
->focus
;
4282 DNPRINTF(SWM_D_FOCUS
, "focus: id: %d, cur_focus: %#x\n", args
->id
,
4285 /* Make sure an uniconified window has focus, if one exists. */
4286 if (cur_focus
== NULL
) {
4287 cur_focus
= TAILQ_FIRST(wl
);
4288 while (cur_focus
!= NULL
&& ICONIC(cur_focus
))
4289 cur_focus
= TAILQ_NEXT(cur_focus
, entry
);
4291 DNPRINTF(SWM_D_FOCUS
, "focus: new cur_focus: %#x\n",
4296 case SWM_ARG_ID_FOCUSPREV
:
4297 if (cur_focus
== NULL
)
4300 winfocus
= cur_focus
;
4302 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
, entry
);
4303 if (winfocus
== NULL
)
4304 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
4305 if (winfocus
== cur_focus
)
4307 } while (winfocus
&& (ICONIC(winfocus
) ||
4308 winfocus
->id
== cur_focus
->transient
||
4309 (cur_focus
->transient
!= XCB_WINDOW_NONE
&&
4310 winfocus
->transient
== cur_focus
->transient
)));
4312 case SWM_ARG_ID_FOCUSNEXT
:
4313 if (cur_focus
== NULL
)
4316 winfocus
= cur_focus
;
4318 winfocus
= TAILQ_NEXT(winfocus
, entry
);
4319 if (winfocus
== NULL
)
4320 winfocus
= TAILQ_FIRST(wl
);
4321 if (winfocus
== cur_focus
)
4323 } while (winfocus
&& (ICONIC(winfocus
) ||
4324 winfocus
->id
== cur_focus
->transient
||
4325 (cur_focus
->transient
!= XCB_WINDOW_NONE
&&
4326 winfocus
->transient
== cur_focus
->transient
)));
4328 case SWM_ARG_ID_FOCUSMAIN
:
4329 if (cur_focus
== NULL
)
4332 winfocus
= TAILQ_FIRST(wl
);
4333 if (winfocus
== cur_focus
)
4334 winfocus
= cur_focus
->ws
->focus_prev
;
4336 case SWM_ARG_ID_FOCUSURGENT
:
4337 /* Search forward for the next urgent window. */
4341 for (i
= 0; i
<= workspace_limit
; ++i
) {
4343 head
= TAILQ_FIRST(&r
->s
->ws
[(ws
->idx
+ i
) %
4344 workspace_limit
].winlist
);
4346 while (head
!= NULL
&&
4347 (head
= TAILQ_NEXT(head
, entry
)) != NULL
) {
4348 if (head
== cur_focus
) {
4349 winfocus
= cur_focus
;
4352 if (xcb_icccm_get_wm_hints_reply(conn
,
4353 xcb_icccm_get_wm_hints(conn
, head
->id
),
4354 &hints
, NULL
) != 0 &&
4355 xcb_icccm_wm_hints_get_urgency(&hints
)) {
4361 if (winfocus
!= NULL
)
4365 /* Switch ws if new focus is on a different ws. */
4366 if (winfocus
!= NULL
&& winfocus
->ws
!= ws
) {
4367 a
.id
= winfocus
->ws
->idx
;
4375 if (clear_maximized(ws
) > 0)
4378 focus_win(get_focus_magic(winfocus
));
4383 DNPRINTF(SWM_D_FOCUS
, "focus: done\n");
4387 cycle_layout(struct swm_region
*r
, union arg
*args
)
4389 struct workspace
*ws
= r
->ws
;
4391 /* suppress unused warning since var is needed */
4394 DNPRINTF(SWM_D_EVENT
, "cycle_layout: workspace: %d\n", ws
->idx
);
4397 if (ws
->cur_layout
->l_stack
== NULL
)
4398 ws
->cur_layout
= &layouts
[0];
4400 clear_maximized(ws
);
4405 focus_win(get_region_focus(r
));
4412 stack_config(struct swm_region
*r
, union arg
*args
)
4414 struct workspace
*ws
= r
->ws
;
4416 DNPRINTF(SWM_D_STACK
, "stack_config: id: %d workspace: %d\n",
4419 if (clear_maximized(ws
) > 0)
4422 if (ws
->cur_layout
->l_config
!= NULL
)
4423 ws
->cur_layout
->l_config(ws
, args
->id
);
4425 if (args
->id
!= SWM_ARG_ID_STACKINIT
)
4435 struct swm_geometry g
;
4436 struct swm_region
*r
, *r_prev
= NULL
;
4443 DNPRINTF(SWM_D_STACK
, "stack: begin\n");
4445 num_screens
= get_screen_count();
4446 for (i
= 0; i
< num_screens
; i
++) {
4450 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
4451 /* Adjust stack area for region bar and padding. */
4453 g
.x
+= region_padding
;
4454 g
.y
+= region_padding
;
4455 g
.w
-= 2 * border_width
+ 2 * region_padding
;
4456 g
.h
-= 2 * border_width
+ 2 * region_padding
;
4457 if (bar_enabled
&& r
->ws
->bar_enabled
) {
4463 DNPRINTF(SWM_D_STACK
, "stack: workspace: %d (screen: "
4464 "%d, region: %d), (x,y) WxH: (%d,%d) %d x %d\n",
4465 r
->ws
->idx
, i
, j
++, g
.x
, g
.y
, g
.w
, g
.h
);
4468 /* Stack bar/input relative to prev. region. */
4469 val
[1] = XCB_STACK_MODE_ABOVE
;
4471 val
[0] = r_prev
->id
;
4472 DNPRINTF(SWM_D_STACK
, "stack: region input %#x "
4473 "relative to %#x.\n", r
->id
, val
[0]);
4474 xcb_configure_window(conn
, r
->id
,
4475 XCB_CONFIG_WINDOW_SIBLING
|
4476 XCB_CONFIG_WINDOW_STACK_MODE
, val
);
4478 val
[0] = r_prev
->bar
->id
;
4479 DNPRINTF(SWM_D_STACK
, "stack: region bar %#x "
4480 "relative to %#x.\n", r
->bar
->id
, val
[0]);
4481 xcb_configure_window(conn
, r
->bar
->id
,
4482 XCB_CONFIG_WINDOW_SIBLING
|
4483 XCB_CONFIG_WINDOW_STACK_MODE
, val
);
4486 r
->ws
->cur_layout
->l_stack(r
->ws
, &g
);
4487 r
->ws
->cur_layout
->l_string(r
->ws
);
4488 /* save r so we can track region changes */
4496 DNPRINTF(SWM_D_STACK
, "stack: end\n");
4500 store_float_geom(struct ws_win
*win
)
4502 if (win
== NULL
|| win
->ws
->r
== NULL
)
4505 /* retain window geom and region geom */
4506 win
->g_float
= win
->g
;
4507 win
->g_float
.x
-= X(win
->ws
->r
);
4508 win
->g_float
.y
-= Y(win
->ws
->r
);
4509 win
->g_floatvalid
= true;
4510 DNPRINTF(SWM_D_MISC
, "store_float_geom: win %#x, g: (%d,%d)"
4511 " %d x %d, g_float: (%d,%d) %d x %d\n", win
->id
, X(win
), Y(win
),
4512 WIDTH(win
), HEIGHT(win
), win
->g_float
.x
, win
->g_float
.y
,
4513 win
->g_float
.w
, win
->g_float
.h
);
4517 load_float_geom(struct ws_win
*win
)
4519 if (win
== NULL
|| win
->ws
->r
== NULL
)
4522 if (win
->g_floatvalid
) {
4523 win
->g
= win
->g_float
;
4524 X(win
) += X(win
->ws
->r
);
4525 Y(win
) += Y(win
->ws
->r
);
4526 DNPRINTF(SWM_D_MISC
, "load_float_geom: win %#x, g: (%d,%d)"
4527 "%d x %d\n", win
->id
, X(win
), Y(win
), WIDTH(win
),
4530 DNPRINTF(SWM_D_MISC
, "load_float_geom: win %#x, g_float "
4531 "is not set.\n", win
->id
);
4536 update_floater(struct ws_win
*win
)
4538 struct workspace
*ws
;
4539 struct swm_region
*r
;
4546 if ((r
= ws
->r
) == NULL
)
4549 DNPRINTF(SWM_D_MISC
, "update_floater: win %#x\n", win
->id
);
4551 win
->bordered
= true;
4553 if (FULLSCREEN(win
)) {
4554 /* _NET_WM_FULLSCREEN: fullscreen without border. */
4555 if (!win
->g_floatvalid
)
4556 store_float_geom(win
);
4559 win
->bordered
= false;
4560 } else if (MAXIMIZED(win
)) {
4561 /* Maximize: like a single stacked window. */
4562 if (!win
->g_floatvalid
)
4563 store_float_geom(win
);
4567 if (bar_enabled
&& ws
->bar_enabled
) {
4569 Y(win
) += bar_height
;
4570 HEIGHT(win
) -= bar_height
;
4571 } else if (disable_border
) {
4572 win
->bordered
= false;
4575 if (win
->bordered
) {
4576 HEIGHT(win
) -= 2 * border_width
;
4577 WIDTH(win
) -= 2 * border_width
;
4580 /* Normal floating window. */
4581 /* Update geometry if new region. */
4583 load_float_geom(win
);
4585 if ((win
->quirks
& SWM_Q_FULLSCREEN
) &&
4586 WIDTH(win
) >= WIDTH(r
) && HEIGHT(win
) >= HEIGHT(r
)) {
4587 /* Remove border for FULLSCREEN quirk. */
4588 win
->bordered
= false;
4589 } else if (!MANUAL(win
)) {
4590 if (TRANS(win
) && (win
->quirks
& SWM_Q_TRANSSZ
)) {
4591 /* Adjust size on TRANSSZ quirk. */
4592 WIDTH(win
) = (double)WIDTH(r
) * dialog_ratio
;
4593 HEIGHT(win
) = (double)HEIGHT(r
) * dialog_ratio
;
4596 if (!(win
->quirks
& SWM_Q_ANYWHERE
)) {
4598 * Floaters and transients are auto-centred
4599 * unless manually moved, resized or ANYWHERE
4602 X(win
) = X(r
) + (WIDTH(r
) - WIDTH(win
)) / 2 -
4604 Y(win
) = Y(r
) + (HEIGHT(r
) - HEIGHT(win
)) / 2 -
4606 store_float_geom(win
);
4611 /* Ensure at least 1 pixel of the window is in the region. */
4612 region_containment(win
, r
, SWM_CW_ALLSIDES
);
4618 * Send keystrokes to terminal to decrease/increase the font size as the
4619 * window size changes.
4622 adjust_font(struct ws_win
*win
)
4624 if (!(win
->quirks
& SWM_Q_XTERM_FONTADJ
) ||
4625 ABOVE(win
) || TRANS(win
))
4628 if (win
->sh
.width_inc
&& win
->last_inc
!= win
->sh
.width_inc
&&
4629 WIDTH(win
) / win
->sh
.width_inc
< term_width
&&
4630 win
->font_steps
< SWM_MAX_FONT_STEPS
) {
4631 win
->font_size_boundary
[win
->font_steps
] =
4632 (win
->sh
.width_inc
* term_width
) + win
->sh
.base_width
;
4635 win
->last_inc
= win
->sh
.width_inc
;
4636 fake_keypress(win
, XK_KP_Subtract
, XCB_MOD_MASK_SHIFT
);
4637 } else if (win
->font_steps
&& win
->last_inc
!= win
->sh
.width_inc
&&
4638 WIDTH(win
) > win
->font_size_boundary
[win
->font_steps
- 1]) {
4641 win
->last_inc
= win
->sh
.width_inc
;
4642 fake_keypress(win
, XK_KP_Add
, XCB_MOD_MASK_SHIFT
);
4646 #define SWAPXY(g) do { \
4648 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
4649 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
4652 stack_master(struct workspace
*ws
, struct swm_geometry
*g
, int rot
, bool flip
)
4654 struct swm_geometry win_g
, r_g
= *g
;
4656 int i
, j
, s
, stacks
;
4657 int w_inc
= 1, h_inc
, w_base
= 1, h_base
;
4658 int hrh
, extra
= 0, h_slice
, last_h
= 0;
4659 int split
, colno
, winno
, mwin
, msize
, mscale
;
4660 int remain
, missing
, v_slice
;
4661 bool bordered
= true, reconfigure
= false;
4663 DNPRINTF(SWM_D_STACK
, "stack_master: workspace: %d, rot: %s, "
4664 "flip: %s\n", ws
->idx
, YESNO(rot
), YESNO(flip
));
4666 /* Prepare tiling variables, if needed. */
4667 if ((winno
= count_win(ws
, false)) > 0) {
4668 /* Find first tiled window. */
4669 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
4670 if (!FLOATING(win
) && !ICONIC(win
))
4673 /* Take into account size hints of first tiled window. */
4675 w_inc
= win
->sh
.width_inc
;
4676 w_base
= win
->sh
.base_width
;
4677 mwin
= ws
->l_state
.horizontal_mwin
;
4678 mscale
= ws
->l_state
.horizontal_msize
;
4679 stacks
= ws
->l_state
.horizontal_stacks
;
4682 w_inc
= win
->sh
.height_inc
;
4683 w_base
= win
->sh
.base_height
;
4684 mwin
= ws
->l_state
.vertical_mwin
;
4685 mscale
= ws
->l_state
.vertical_msize
;
4686 stacks
= ws
->l_state
.vertical_stacks
;
4690 if (stacks
> winno
- mwin
)
4691 stacks
= winno
- mwin
;
4695 h_slice
= r_g
.h
/ SWM_H_SLICE
;
4696 if (mwin
&& winno
> mwin
) {
4697 v_slice
= r_g
.w
/ SWM_V_SLICE
;
4701 win_g
.w
= v_slice
* mscale
;
4703 if (w_inc
> 1 && w_inc
< v_slice
) {
4704 /* Adjust for requested size increment. */
4705 remain
= (win_g
.w
- w_base
) % w_inc
;
4711 win_g
.x
+= r_g
.w
- msize
;
4714 colno
= split
= winno
/ stacks
;
4715 win_g
.w
= ((r_g
.w
- (stacks
* 2 * border_width
) +
4716 2 * border_width
) / stacks
);
4718 hrh
= r_g
.h
/ colno
;
4719 extra
= r_g
.h
- (colno
* hrh
);
4720 win_g
.h
= hrh
- 2 * border_width
;
4721 i
= j
= 0, s
= stacks
;
4724 /* Update window geometry. */
4725 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
4729 if (FLOATING(win
)) {
4730 update_floater(win
);
4735 if (split
&& i
== split
) {
4736 colno
= (winno
- mwin
) / stacks
;
4737 if (s
<= (winno
- mwin
) % stacks
)
4740 hrh
= r_g
.h
/ colno
;
4741 extra
= r_g
.h
- (colno
* hrh
);
4744 win_g
.x
+= win_g
.w
+ 2 * border_width
+
4747 win_g
.w
= (r_g
.w
- msize
-
4748 (stacks
* (2 * border_width
+ tile_gap
))) / stacks
;
4750 win_g
.w
+= (r_g
.w
- msize
-
4751 (stacks
* (2 * border_width
+ tile_gap
))) %
4755 win_g
.x
-= win_g
.w
+ 2 * border_width
+
4761 win_g
.h
= hrh
- 2 * border_width
- tile_gap
;
4764 h_inc
= win
->sh
.width_inc
;
4765 h_base
= win
->sh
.base_width
;
4767 h_inc
= win
->sh
.height_inc
;
4768 h_base
= win
->sh
.base_height
;
4771 if (j
== colno
- 1) {
4772 win_g
.h
= hrh
+ extra
;
4773 } else if (h_inc
> 1 && h_inc
< h_slice
) {
4774 /* adjust for window's requested size increment */
4775 remain
= (win_g
.h
- h_base
) % h_inc
;
4776 missing
= h_inc
- remain
;
4778 if (missing
<= extra
|| j
== 0) {
4790 win_g
.y
+= last_h
+ 2 * border_width
+ tile_gap
;
4792 if (disable_border
&& !(bar_enabled
&& ws
->bar_enabled
) &&
4795 win_g
.w
+= 2 * border_width
;
4796 win_g
.h
+= 2 * border_width
;
4802 if (X(win
) != win_g
.y
|| Y(win
) != win_g
.x
||
4803 WIDTH(win
) != win_g
.h
|| HEIGHT(win
) != win_g
.w
) {
4807 WIDTH(win
) = win_g
.h
;
4808 HEIGHT(win
) = win_g
.w
;
4811 if (X(win
) != win_g
.x
|| Y(win
) != win_g
.y
||
4812 WIDTH(win
) != win_g
.w
|| HEIGHT(win
) != win_g
.h
) {
4816 WIDTH(win
) = win_g
.w
;
4817 HEIGHT(win
) = win_g
.h
;
4821 if (bordered
!= win
->bordered
) {
4823 win
->bordered
= bordered
;
4836 /* Stack all windows from bottom up. */
4837 TAILQ_FOREACH_REVERSE(win
, &ws
->stack
, ws_win_stack
, stack_entry
)
4839 update_win_stacking(win
);
4841 /* Map all windows from top down. */
4842 TAILQ_FOREACH(win
, &ws
->stack
, stack_entry
)
4846 DNPRINTF(SWM_D_STACK
, "stack_master: done\n");
4850 vertical_config(struct workspace
*ws
, int id
)
4852 DNPRINTF(SWM_D_STACK
, "vertical_config: id: %d, workspace: %d\n",
4856 case SWM_ARG_ID_STACKRESET
:
4857 case SWM_ARG_ID_STACKINIT
:
4858 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ 2;
4859 ws
->l_state
.vertical_mwin
= 1;
4860 ws
->l_state
.vertical_stacks
= 1;
4862 case SWM_ARG_ID_MASTERSHRINK
:
4863 if (ws
->l_state
.vertical_msize
> 1)
4864 ws
->l_state
.vertical_msize
--;
4866 case SWM_ARG_ID_MASTERGROW
:
4867 if (ws
->l_state
.vertical_msize
< SWM_V_SLICE
- 1)
4868 ws
->l_state
.vertical_msize
++;
4870 case SWM_ARG_ID_MASTERADD
:
4871 ws
->l_state
.vertical_mwin
++;
4873 case SWM_ARG_ID_MASTERDEL
:
4874 if (ws
->l_state
.vertical_mwin
> 0)
4875 ws
->l_state
.vertical_mwin
--;
4877 case SWM_ARG_ID_STACKINC
:
4878 ws
->l_state
.vertical_stacks
++;
4880 case SWM_ARG_ID_STACKDEC
:
4881 if (ws
->l_state
.vertical_stacks
> 1)
4882 ws
->l_state
.vertical_stacks
--;
4884 case SWM_ARG_ID_FLIPLAYOUT
:
4885 ws
->l_state
.vertical_flip
= !ws
->l_state
.vertical_flip
;
4893 vertical_stack(struct workspace
*ws
, struct swm_geometry
*g
)
4895 DNPRINTF(SWM_D_STACK
, "vertical_stack: workspace: %d\n", ws
->idx
);
4897 stack_master(ws
, g
, 0, ws
->l_state
.vertical_flip
);
4901 horizontal_config(struct workspace
*ws
, int id
)
4903 DNPRINTF(SWM_D_STACK
, "horizontal_config: workspace: %d\n", ws
->idx
);
4906 case SWM_ARG_ID_STACKRESET
:
4907 case SWM_ARG_ID_STACKINIT
:
4908 ws
->l_state
.horizontal_mwin
= 1;
4909 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ 2;
4910 ws
->l_state
.horizontal_stacks
= 1;
4912 case SWM_ARG_ID_MASTERSHRINK
:
4913 if (ws
->l_state
.horizontal_msize
> 1)
4914 ws
->l_state
.horizontal_msize
--;
4916 case SWM_ARG_ID_MASTERGROW
:
4917 if (ws
->l_state
.horizontal_msize
< SWM_H_SLICE
- 1)
4918 ws
->l_state
.horizontal_msize
++;
4920 case SWM_ARG_ID_MASTERADD
:
4921 ws
->l_state
.horizontal_mwin
++;
4923 case SWM_ARG_ID_MASTERDEL
:
4924 if (ws
->l_state
.horizontal_mwin
> 0)
4925 ws
->l_state
.horizontal_mwin
--;
4927 case SWM_ARG_ID_STACKINC
:
4928 ws
->l_state
.horizontal_stacks
++;
4930 case SWM_ARG_ID_STACKDEC
:
4931 if (ws
->l_state
.horizontal_stacks
> 1)
4932 ws
->l_state
.horizontal_stacks
--;
4934 case SWM_ARG_ID_FLIPLAYOUT
:
4935 ws
->l_state
.horizontal_flip
= !ws
->l_state
.horizontal_flip
;
4943 horizontal_stack(struct workspace
*ws
, struct swm_geometry
*g
)
4945 DNPRINTF(SWM_D_STACK
, "horizontal_stack: workspace: %d\n", ws
->idx
);
4947 stack_master(ws
, g
, 1, ws
->l_state
.horizontal_flip
);
4950 /* fullscreen view */
4952 max_stack(struct workspace
*ws
, struct swm_geometry
*g
)
4954 struct swm_geometry gg
= *g
;
4955 struct ws_win
*w
, *win
= NULL
, *parent
= NULL
, *tmpw
;
4958 DNPRINTF(SWM_D_STACK
, "max_stack: workspace: %d\n", ws
->idx
);
4963 winno
= count_win(ws
, false);
4964 if (winno
== 0 && count_win(ws
, true) == 0)
4967 /* Figure out which top level window should be visible. */
4968 if (ws
->focus_pending
)
4969 win
= ws
->focus_pending
;
4972 else if (ws
->focus_prev
)
4973 win
= ws
->focus_prev
;
4975 win
= TAILQ_FIRST(&ws
->winlist
);
4977 DNPRINTF(SWM_D_STACK
, "max_stack: focus_pending: %#x, focus: %#x, "
4978 "focus_prev: %#x, first: %#x, win: %#x\n", WINID(ws
->focus_pending
),
4979 WINID(ws
->focus
), WINID(ws
->focus_prev
),
4980 WINID(TAILQ_FIRST(&ws
->winlist
)), win
->id
);
4982 /* Update window geometry. */
4983 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
4992 /* Set maximized flag for all maxed windows. */
4993 if (!MAXIMIZED(w
)) {
4994 /* Preserve floating geometry. */
4996 store_float_geom(w
);
4998 ewmh_apply_flags(w
, w
->ewmh_flags
| EWMH_F_MAXIMIZED
);
4999 ewmh_update_wm_state(w
);
5002 /* Only reconfigure if necessary. */
5003 if (X(w
) != gg
.x
|| Y(w
) != gg
.y
|| WIDTH(w
) != gg
.w
||
5004 HEIGHT(w
) != gg
.h
) {
5007 if (disable_border
&& !(bar_enabled
&& ws
->bar_enabled
)) {
5008 w
->bordered
= false;
5009 WIDTH(w
) += 2 * border_width
;
5010 HEIGHT(w
) += 2 * border_width
;
5019 /* If transient, stack parent and its children. */
5020 if (TRANS(win
) && (parent
= find_window(win
->transient
))) {
5021 raise_window(parent
);
5023 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
, tmpw
)
5024 if (w
->transient
== parent
->id
)
5028 /* Make sure focus window is on top. */
5031 /* Stack any children of focus window. */
5032 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
, tmpw
)
5033 if (w
->transient
== win
->id
)
5036 /* Map all windows. */
5037 TAILQ_FOREACH(w
, &ws
->stack
, stack_entry
)
5043 send_to_rg(struct swm_region
*r
, union arg
*args
)
5045 int ridx
= args
->id
, i
, num_screens
;
5046 struct swm_region
*rr
= NULL
;
5049 num_screens
= get_screen_count();
5050 /* do nothing if we don't have more than one screen */
5051 if (!(num_screens
> 1 || outputs
> 1))
5054 DNPRINTF(SWM_D_FOCUS
, "send_to_rg: id: %d\n", ridx
);
5056 rr
= TAILQ_FIRST(&r
->s
->rl
);
5057 for (i
= 0; i
< ridx
&& rr
!= NULL
; ++i
)
5058 rr
= TAILQ_NEXT(rr
, entry
);
5069 region_under(struct swm_screen
*s
, int x
, int y
)
5071 struct swm_region
*r
= NULL
;
5073 TAILQ_FOREACH(r
, &s
->rl
, entry
) {
5074 DNPRINTF(SWM_D_MISC
, "region_under: ws: %d, region g: (%d,%d) "
5075 "%d x %d, coords: (%d,%d)\n", r
->ws
->idx
, X(r
), Y(r
),
5076 WIDTH(r
), HEIGHT(r
), x
, y
);
5077 if (X(r
) <= x
&& x
< MAX_X(r
))
5078 if (Y(r
) <= y
&& y
< MAX_Y(r
))
5085 /* Transfer focused window to target workspace and focus. */
5087 send_to_ws(struct swm_region
*r
, union arg
*args
)
5089 int wsid
= args
->id
;
5090 struct ws_win
*win
= NULL
;
5092 if (r
&& r
->ws
&& r
->ws
->focus
)
5097 DNPRINTF(SWM_D_MOVE
, "send_to_ws: win %#x, ws %d\n", win
->id
, wsid
);
5099 if (wsid
< 0 || wsid
>= workspace_limit
)
5102 if (win
->ws
->idx
== wsid
)
5105 win_to_ws(win
, wsid
, true);
5107 /* Set new focus on target ws. */
5108 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
5109 win
->ws
->focus_prev
= win
->ws
->focus
;
5110 win
->ws
->focus
= win
;
5111 win
->ws
->focus_pending
= NULL
;
5113 if (win
->ws
->focus_prev
)
5114 update_window_color(win
->ws
->focus_prev
);
5117 DNPRINTF(SWM_D_STACK
, "send_to_ws: focus_pending: %#x, focus: %#x, "
5118 "focus_prev: %#x, first: %#x, win: %#x\n",
5119 WINID(r
->ws
->focus_pending
), WINID(r
->ws
->focus
),
5120 WINID(r
->ws
->focus_prev
), WINID(TAILQ_FIRST(&r
->ws
->winlist
)),
5123 ewmh_apply_flags(win
, win
->ewmh_flags
& ~EWMH_F_MAXIMIZED
);
5124 ewmh_update_wm_state(win
);
5126 /* Restack and set new focus on current ws. */
5128 load_float_geom(win
);
5132 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
5133 if (r
->ws
->focus
!= NULL
) {
5134 focus_win(r
->ws
->focus
);
5136 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
, r
->id
,
5147 win_to_ws(struct ws_win
*win
, int wsid
, bool unfocus
)
5149 struct ws_win
*parent
;
5150 struct workspace
*ws
, *nws
, *pws
;
5152 if (wsid
< 0 || wsid
>= workspace_limit
)
5155 if (win
->ws
->idx
== wsid
)
5159 nws
= &win
->s
->ws
[wsid
];
5161 DNPRINTF(SWM_D_MOVE
, "win_to_ws: win %#x, ws %d -> %d\n", win
->id
,
5164 /* Cleanup focus on source ws. */
5165 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
5166 (ws
->focus
== win
|| ws
->focus_pending
== win
))
5167 ws
->focus_pending
= get_focus_prev(win
);
5169 /* Move the parent if this is a transient window. */
5171 parent
= find_window(win
->transient
);
5174 /* Set new focus in parent's ws if needed. */
5175 if (pws
->focus
== parent
) {
5176 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
5177 pws
->focus_pending
=
5178 get_focus_prev(parent
);
5180 unfocus_win(parent
);
5182 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
5183 pws
->focus
= pws
->focus_pending
;
5184 pws
->focus_pending
= NULL
;
5188 /* Don't unmap parent if new ws is visible */
5190 unmap_window(parent
);
5193 TAILQ_REMOVE(&ws
->winlist
, parent
, entry
);
5194 TAILQ_REMOVE(&ws
->stack
, parent
, stack_entry
);
5195 TAILQ_INSERT_TAIL(&nws
->winlist
, parent
, entry
);
5196 TAILQ_INSERT_TAIL(&nws
->stack
, parent
, stack_entry
);
5199 DNPRINTF(SWM_D_PROP
, "win_to_ws: set property: "
5200 "_NET_WM_DESKTOP: %d\n", wsid
);
5201 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
5202 parent
->id
, ewmh
[_NET_WM_DESKTOP
].atom
,
5203 XCB_ATOM_CARDINAL
, 32, 1, &wsid
);
5210 if (ws
->focus_prev
== win
)
5211 ws
->focus_prev
= NULL
;
5213 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& ws
->focus_pending
!= NULL
) {
5214 ws
->focus
= ws
->focus_pending
;
5215 ws
->focus_pending
= NULL
;
5218 /* Don't unmap if new ws is visible */
5223 TAILQ_REMOVE(&ws
->winlist
, win
, entry
);
5224 TAILQ_REMOVE(&ws
->stack
, win
, stack_entry
);
5225 TAILQ_INSERT_TAIL(&nws
->winlist
, win
, entry
);
5226 TAILQ_INSERT_TAIL(&nws
->stack
, win
, stack_entry
);
5229 /* Update the window's workspace property: _NET_WM_DESKTOP */
5230 DNPRINTF(SWM_D_PROP
, "win_to_ws: set property: "
5231 "_NET_WM_DESKTOP: %d\n", wsid
);
5232 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
5233 ewmh
[_NET_WM_DESKTOP
].atom
, XCB_ATOM_CARDINAL
, 32, 1, &wsid
);
5235 ewmh_update_client_list();
5237 DNPRINTF(SWM_D_MOVE
, "win_to_ws: done.\n");
5241 pressbutton(struct swm_region
*r
, union arg
*args
)
5243 /* suppress unused warning since var is needed */
5246 xcb_test_fake_input(conn
, XCB_BUTTON_PRESS
, args
->id
,
5247 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
5248 xcb_test_fake_input(conn
, XCB_BUTTON_RELEASE
, args
->id
,
5249 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
5253 raise_toggle(struct swm_region
*r
, union arg
*args
)
5255 /* suppress unused warning since var is needed */
5258 if (r
== NULL
|| r
->ws
== NULL
)
5261 if (r
->ws
->focus
&& MAXIMIZED(r
->ws
->focus
))
5264 r
->ws
->always_raise
= !r
->ws
->always_raise
;
5266 /* bring floaters back to top */
5267 if (!r
->ws
->always_raise
)
5274 iconify(struct swm_region
*r
, union arg
*args
)
5277 /* suppress unused warning since var is needed */
5280 if ((w
= r
->ws
->focus
) == NULL
)
5283 ewmh_apply_flags(w
, w
->ewmh_flags
| EWMH_F_HIDDEN
);
5284 ewmh_update_wm_state(w
);
5292 get_win_name(xcb_window_t win
)
5295 xcb_get_property_cookie_t c
;
5296 xcb_get_property_reply_t
*r
;
5298 /* First try _NET_WM_NAME for UTF-8. */
5299 c
= xcb_get_property(conn
, 0, win
, ewmh
[_NET_WM_NAME
].atom
,
5300 XCB_GET_PROPERTY_TYPE_ANY
, 0, UINT_MAX
);
5301 r
= xcb_get_property_reply(conn
, c
, NULL
);
5302 if (r
&& r
->type
== XCB_NONE
) {
5304 /* Use WM_NAME instead; no UTF-8. */
5305 c
= xcb_get_property(conn
, 0, win
, XCB_ATOM_WM_NAME
,
5306 XCB_GET_PROPERTY_TYPE_ANY
, 0, UINT_MAX
);
5307 r
= xcb_get_property_reply(conn
, c
, NULL
);
5310 if (r
&& r
->type
!= XCB_NONE
&& r
->length
> 0)
5311 name
= strndup(xcb_get_property_value(r
),
5312 xcb_get_property_value_length(r
));
5317 err(1, "get_win_name: failed to allocate memory.");
5325 uniconify(struct swm_region
*r
, union arg
*args
)
5332 DNPRINTF(SWM_D_MISC
, "uniconify\n");
5334 if (r
== NULL
|| r
->ws
== NULL
)
5337 /* make sure we have anything to uniconify */
5338 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
5339 if (win
->ws
== NULL
)
5340 continue; /* should never happen */
5346 DNPRINTF(SWM_D_MISC
, "uniconify: count: %d\n", count
);
5352 search_resp_action
= SWM_SEARCH_UNICONIFY
;
5354 spawn_select(r
, args
, "search", &searchpid
);
5356 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
5359 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
5360 if (win
->ws
== NULL
)
5361 continue; /* should never happen */
5365 name
= get_win_name(win
->id
);
5366 fprintf(lfile
, "%s.%u\n", name
, win
->id
);
5374 name_workspace(struct swm_region
*r
, union arg
*args
)
5378 DNPRINTF(SWM_D_MISC
, "name_workspace\n");
5384 search_resp_action
= SWM_SEARCH_NAME_WORKSPACE
;
5386 spawn_select(r
, args
, "name_workspace", &searchpid
);
5388 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
5391 fprintf(lfile
, "%s", "");
5396 search_workspace(struct swm_region
*r
, union arg
*args
)
5399 struct workspace
*ws
;
5402 DNPRINTF(SWM_D_MISC
, "search_workspace\n");
5408 search_resp_action
= SWM_SEARCH_SEARCH_WORKSPACE
;
5410 spawn_select(r
, args
, "search", &searchpid
);
5412 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
5415 for (i
= 0; i
< workspace_limit
; i
++) {
5419 fprintf(lfile
, "%d%s%s\n", ws
->idx
+ 1,
5420 (ws
->name
? ":" : ""), (ws
->name
? ws
->name
: ""));
5427 search_win_cleanup(void)
5429 struct search_window
*sw
= NULL
;
5431 while ((sw
= TAILQ_FIRST(&search_wl
)) != NULL
) {
5432 xcb_destroy_window(conn
, sw
->indicator
);
5433 TAILQ_REMOVE(&search_wl
, sw
, entry
);
5439 search_win(struct swm_region
*r
, union arg
*args
)
5441 struct ws_win
*win
= NULL
;
5442 struct search_window
*sw
= NULL
;
5445 xcb_screen_t
*screen
;
5446 int i
, width
, height
;
5454 XRectangle l_ibox
, l_lbox
;
5456 DNPRINTF(SWM_D_MISC
, "search_win\n");
5459 search_resp_action
= SWM_SEARCH_SEARCH_WINDOW
;
5461 spawn_select(r
, args
, "search", &searchpid
);
5463 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
5466 if ((screen
= get_screen(r
->s
->idx
)) == NULL
)
5467 errx(1, "ERROR: can't get screen %d.", r
->s
->idx
);
5469 TAILQ_INIT(&search_wl
);
5472 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
5476 sw
= calloc(1, sizeof(struct search_window
));
5478 warn("search_win: calloc");
5480 search_win_cleanup();
5486 snprintf(s
, sizeof s
, "%d", i
);
5489 w
= xcb_generate_id(conn
);
5490 wa
[0] = r
->s
->c
[SWM_S_COLOR_FOCUS
].pixel
;
5491 wa
[1] = r
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
;
5492 wa
[2] = screen
->default_colormap
;
5494 if (bar_font_legacy
) {
5495 XmbTextExtents(bar_fs
, s
, len
, &l_ibox
, &l_lbox
);
5496 width
= l_lbox
.width
+ 4;
5497 height
= bar_fs_extents
->max_logical_extent
.height
+ 4;
5499 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
,
5501 width
= info
.width
+ 4;
5502 height
= bar_font
->height
+ 4;
5505 xcb_create_window(conn
, screen
->root_depth
, w
, win
->id
, 0, 0,
5506 width
, height
, 1, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
5507 screen
->root_visual
, XCB_CW_BACK_PIXEL
|
5508 XCB_CW_BORDER_PIXEL
| XCB_CW_COLORMAP
, wa
);
5510 xcb_map_window(conn
, w
);
5513 TAILQ_INSERT_TAIL(&search_wl
, sw
, entry
);
5515 if (bar_font_legacy
) {
5516 l_gcv
.graphics_exposures
= 0;
5517 l_draw
= XCreateGC(display
, w
, 0, &l_gcv
);
5519 XSetForeground(display
, l_draw
,
5520 r
->s
->c
[SWM_S_COLOR_BAR
].pixel
);
5522 DRAWSTRING(display
, w
, bar_fs
, l_draw
, 2,
5523 (bar_fs_extents
->max_logical_extent
.height
-
5524 l_lbox
.height
) / 2 - l_lbox
.y
, s
, len
);
5526 XFreeGC(display
, l_draw
);
5529 draw
= XftDrawCreate(display
, w
,
5530 DefaultVisual(display
, r
->s
->idx
),
5531 DefaultColormap(display
, r
->s
->idx
));
5533 XftDrawStringUtf8(draw
, &search_font_color
, bar_font
, 2,
5534 (HEIGHT(r
->bar
) + bar_font
->height
) / 2 -
5535 bar_font
->descent
, (FcChar8
*)s
, len
);
5537 XftDrawDestroy(draw
);
5540 DNPRINTF(SWM_D_MISC
, "search_win: mapped win %#x\n", w
);
5542 fprintf(lfile
, "%d\n", i
);
5552 search_resp_uniconify(const char *resp
, size_t len
)
5558 DNPRINTF(SWM_D_MISC
, "search_resp_uniconify: resp: %s\n", resp
);
5560 TAILQ_FOREACH(win
, &search_r
->ws
->winlist
, entry
) {
5563 name
= get_win_name(win
->id
);
5564 if (asprintf(&s
, "%s.%u", name
, win
->id
) == -1) {
5569 if (strncmp(s
, resp
, len
) == 0) {
5570 /* XXX this should be a callback to generalize */
5571 ewmh_apply_flags(win
, win
->ewmh_flags
& ~EWMH_F_HIDDEN
);
5572 ewmh_update_wm_state(win
);
5582 search_resp_name_workspace(const char *resp
, size_t len
)
5584 struct workspace
*ws
;
5586 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: resp: %s\n", resp
);
5588 if (search_r
->ws
== NULL
)
5593 free(search_r
->ws
->name
);
5594 search_r
->ws
->name
= NULL
;
5598 ws
->name
= strdup(resp
);
5599 if (ws
->name
== NULL
) {
5600 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: "
5601 "strdup: %s", strerror(errno
));
5605 ewmh_update_desktop_names();
5606 ewmh_get_desktop_names();
5611 ewmh_update_desktop_names(void)
5613 char *name_list
= NULL
, *p
;
5614 int num_screens
, i
, j
, len
= 0, tot
= 0;
5616 num_screens
= get_screen_count();
5617 for (i
= 0; i
< num_screens
; ++i
) {
5618 for (j
= 0; j
< workspace_limit
; ++j
) {
5619 if (screens
[i
].ws
[j
].name
!= NULL
)
5620 len
+= strlen(screens
[i
].ws
[j
].name
);
5624 if((name_list
= calloc(len
, sizeof(char))) == NULL
)
5625 err(1, "update_desktop_names: calloc: failed to "
5626 "allocate memory.");
5629 for (j
= 0; j
< workspace_limit
; ++j
) {
5630 if (screens
[i
].ws
[j
].name
!= NULL
) {
5631 len
= strlen(screens
[i
].ws
[j
].name
);
5632 memcpy(p
, screens
[i
].ws
[j
].name
, len
);
5641 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
5642 screens
[i
].root
, ewmh
[_NET_DESKTOP_NAMES
].atom
,
5643 a_utf8_string
, 8, tot
, name_list
);
5653 ewmh_get_desktop_names(void)
5656 xcb_get_property_cookie_t c
;
5657 xcb_get_property_reply_t
*r
;
5658 int num_screens
, i
, j
, n
, k
;
5660 num_screens
= get_screen_count();
5661 for (i
= 0; i
< num_screens
; ++i
) {
5662 for (j
= 0; j
< workspace_limit
; ++j
) {
5663 free(screens
[i
].ws
[j
].name
);
5664 screens
[i
].ws
[j
].name
= NULL
;
5667 c
= xcb_get_property(conn
, 0, screens
[i
].root
,
5668 ewmh
[_NET_DESKTOP_NAMES
].atom
,
5669 a_utf8_string
, 0, UINT32_MAX
);
5670 r
= xcb_get_property_reply(conn
, c
, NULL
);
5674 names
= xcb_get_property_value(r
);
5675 n
= xcb_get_property_value_length(r
);
5677 for (j
= 0, k
= 0; j
< n
; ++j
) {
5678 if (*(names
+ j
) != '\0') {
5679 screens
[i
].ws
[k
].name
= strdup(names
+ j
);
5680 j
+= strlen(names
+ j
);
5689 ewmh_update_client_list(void)
5692 int num_screens
, i
, j
, k
= 0, count
= 0;
5695 num_screens
= get_screen_count();
5696 for (i
= 0; i
< num_screens
; ++i
) {
5697 for (j
= 0; j
< workspace_limit
; ++j
)
5698 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
5701 DNPRINTF(SWM_D_PROP
, "ewmh_update_client_list: win count: %d\n",
5707 wins
= calloc(count
, sizeof(xcb_window_t
));
5709 err(1, "ewmh_update_client_list: calloc: failed to "
5710 "allocate memory.");
5712 for (j
= 0, k
= 0; j
< workspace_limit
; ++j
)
5713 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
5714 wins
[k
++] = win
->id
;
5716 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
5717 screens
[i
].root
, ewmh
[_NET_CLIENT_LIST
].atom
,
5718 XCB_ATOM_WINDOW
, 32, count
, wins
);
5725 ewmh_update_current_desktop(void)
5729 num_screens
= get_screen_count();
5730 for (i
= 0; i
< num_screens
; ++i
)
5731 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
5732 screens
[i
].root
, ewmh
[_NET_CURRENT_DESKTOP
].atom
,
5733 XCB_ATOM_CARDINAL
, 32, 1, &screens
[i
].r_focus
->ws
->idx
);
5737 ewmh_update_desktops(void)
5739 int num_screens
, i
, j
;
5742 vals
= calloc(workspace_limit
* 2, sizeof(uint32_t));
5744 err(1, "ewmh_update_desktops: calloc: failed to allocate "
5747 num_screens
= get_screen_count();
5748 for (i
= 0; i
< num_screens
; i
++) {
5749 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
5750 screens
[i
].root
, ewmh
[_NET_NUMBER_OF_DESKTOPS
].atom
,
5751 XCB_ATOM_CARDINAL
, 32, 1, &workspace_limit
);
5753 for (j
= 0; j
< workspace_limit
; ++j
) {
5754 if (screens
[i
].ws
[j
].r
!= NULL
) {
5755 vals
[j
* 2] = X(screens
[i
].ws
[j
].r
);
5756 vals
[j
* 2 + 1] = Y(screens
[i
].ws
[j
].r
);
5757 } else if (screens
[i
].ws
[j
].old_r
!= NULL
) {
5758 vals
[j
* 2] = X(screens
[i
].ws
[j
].old_r
);
5759 vals
[j
* 2 + 1] = Y(screens
[i
].ws
[j
].old_r
);
5761 vals
[j
* 2] = vals
[j
* 2 + 1] = 0;
5765 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
5766 screens
[i
].root
, ewmh
[_NET_DESKTOP_VIEWPORT
].atom
,
5767 XCB_ATOM_CARDINAL
, 32, workspace_limit
* 2, vals
);
5774 search_resp_search_workspace(const char *resp
)
5781 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: resp: %s\n", resp
);
5785 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: strdup: %s",
5792 ws_idx
= (int)strtonum(q
, 1, workspace_limit
, &errstr
);
5794 DNPRINTF(SWM_D_MISC
, "workspace idx is %s: %s",
5801 switchws(search_r
, &a
);
5805 search_resp_search_window(const char *resp
)
5810 struct search_window
*sw
;
5812 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: resp: %s\n", resp
);
5816 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: strdup: %s",
5821 idx
= (int)strtonum(s
, 1, INT_MAX
, &errstr
);
5823 DNPRINTF(SWM_D_MISC
, "window idx is %s: %s",
5830 TAILQ_FOREACH(sw
, &search_wl
, entry
)
5831 if (idx
== sw
->idx
) {
5837 #define MAX_RESP_LEN 1024
5840 search_do_resp(void)
5846 DNPRINTF(SWM_D_MISC
, "search_do_resp:\n");
5851 if ((resp
= calloc(1, MAX_RESP_LEN
+ 1)) == NULL
) {
5852 warn("search: calloc");
5856 rbytes
= read(select_resp_pipe
[0], resp
, MAX_RESP_LEN
);
5858 warn("search: read error");
5861 resp
[rbytes
] = '\0';
5864 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
5865 * newline, so work around that by sanitizing the resp now.
5867 resp
[strcspn(resp
, "\n")] = '\0';
5870 switch (search_resp_action
) {
5871 case SWM_SEARCH_UNICONIFY
:
5872 search_resp_uniconify(resp
, len
);
5874 case SWM_SEARCH_NAME_WORKSPACE
:
5875 search_resp_name_workspace(resp
, len
);
5877 case SWM_SEARCH_SEARCH_WORKSPACE
:
5878 search_resp_search_workspace(resp
);
5880 case SWM_SEARCH_SEARCH_WINDOW
:
5881 search_resp_search_window(resp
);
5886 if (search_resp_action
== SWM_SEARCH_SEARCH_WINDOW
)
5887 search_win_cleanup();
5889 search_resp_action
= SWM_SEARCH_NONE
;
5890 close(select_resp_pipe
[0]);
5897 wkill(struct swm_region
*r
, union arg
*args
)
5899 DNPRINTF(SWM_D_MISC
, "wkill: win %#x, id: %d\n", WINID(r
->ws
->focus
),
5902 if (r
->ws
->focus
== NULL
)
5905 if (args
->id
== SWM_ARG_ID_KILLWINDOW
)
5906 xcb_kill_client(conn
, r
->ws
->focus
->id
);
5908 if (r
->ws
->focus
->can_delete
)
5909 client_msg(r
->ws
->focus
, a_delete
, 0);
5915 clear_maximized(struct workspace
*ws
)
5920 /* Clear any maximized win(s) on ws, from bottom up. */
5921 TAILQ_FOREACH_REVERSE(w
, &ws
->stack
, ws_win_stack
, stack_entry
)
5923 ewmh_apply_flags(w
, w
->ewmh_flags
& ~EWMH_F_MAXIMIZED
);
5924 ewmh_update_wm_state(w
);
5932 maximize_toggle(struct swm_region
*r
, union arg
*args
)
5934 struct ws_win
*w
= r
->ws
->focus
;
5936 /* suppress unused warning since var is needed */
5942 DNPRINTF(SWM_D_MISC
, "maximize_toggle: win %#x\n", w
->id
);
5947 if (w
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
5950 ewmh_apply_flags(w
, w
->ewmh_flags
^ EWMH_F_MAXIMIZED
);
5951 ewmh_update_wm_state(w
);
5955 if (w
== w
->ws
->focus
)
5960 DNPRINTF(SWM_D_MISC
, "maximize_toggle: done\n");
5964 floating_toggle(struct swm_region
*r
, union arg
*args
)
5966 struct ws_win
*w
= r
->ws
->focus
;
5968 /* suppress unused warning since var is needed */
5974 DNPRINTF(SWM_D_MISC
, "floating_toggle: win %#x\n", w
->id
);
5976 if (FULLSCREEN(w
) || TRANS(w
))
5979 if (w
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
5982 ewmh_apply_flags(w
, w
->ewmh_flags
^ EWMH_F_ABOVE
);
5983 ewmh_update_wm_state(w
);
5987 if (w
== w
->ws
->focus
)
5992 DNPRINTF(SWM_D_MISC
, "floating_toggle: done\n");
5996 region_containment(struct ws_win
*win
, struct swm_region
*r
, int opts
)
5998 struct swm_geometry g
= r
->g
;
5999 int rt
, lt
, tp
, bm
, bw
;
6001 bw
= (opts
& SWM_CW_SOFTBOUNDARY
) ? boundary_width
: 0;
6004 * Perpendicular distance of each side of the window to the respective
6005 * side of the region boundary. Positive values indicate the side of
6006 * the window has passed beyond the region boundary.
6008 rt
= opts
& SWM_CW_RIGHT
? MAX_X(win
) + BORDER(win
) - MAX_X(r
) : bw
;
6009 lt
= opts
& SWM_CW_LEFT
? X(r
) - X(win
) + BORDER(win
) : bw
;
6010 bm
= opts
& SWM_CW_BOTTOM
? MAX_Y(win
) + BORDER(win
) - MAX_Y(r
) : bw
;
6011 tp
= opts
& SWM_CW_TOP
? Y(r
) - Y(win
) + BORDER(win
) : bw
;
6013 DNPRINTF(SWM_D_MISC
, "region_containment: win %#x, rt: %d, lt: %d, "
6014 "bm: %d, tp: %d, SOFTBOUNDARY: %s, HARDBOUNDARY: %s\n", win
->id
, rt
,
6015 lt
, bm
, tp
, YESNO(opts
& SWM_CW_SOFTBOUNDARY
),
6016 YESNO(opts
& SWM_CW_HARDBOUNDARY
));
6019 * Disable containment if any of the flagged sides went beyond the
6020 * containment boundary, or if containment is disabled.
6022 if (!(opts
& SWM_CW_HARDBOUNDARY
|| opts
& SWM_CW_SOFTBOUNDARY
) ||
6023 (bw
!= 0 && ((rt
> bw
) || (lt
> bw
) || (bm
> bw
) || (tp
> bw
)))) {
6024 /* Make sure window has at least 1 pixel in the region */
6025 g
.x
+= 1 - WIDTH(win
);
6026 g
.y
+= 1 - HEIGHT(win
);
6027 g
.w
+= 2 * WIDTH(win
) - 2;
6028 g
.h
+= 2 * HEIGHT(win
) - 2;
6031 constrain_window(win
, &g
, &opts
);
6034 /* Move or resize a window so that flagged side(s) fit into the supplied box. */
6036 constrain_window(struct ws_win
*win
, struct swm_geometry
*b
, int *opts
)
6038 DNPRINTF(SWM_D_MISC
, "constrain_window: win %#x, (x,y) w x h: "
6039 "(%d,%d) %d x %d, box: (x,y) w x h: (%d,%d) %d x %d, rt: %s, "
6040 "lt: %s, bt: %s, tp: %s, allow resize: %s\n", win
->id
, X(win
),
6041 Y(win
), WIDTH(win
), HEIGHT(win
), b
->x
, b
->y
, b
->w
, b
->h
,
6042 YESNO(*opts
& SWM_CW_RIGHT
), YESNO(*opts
& SWM_CW_LEFT
),
6043 YESNO(*opts
& SWM_CW_BOTTOM
), YESNO(*opts
& SWM_CW_TOP
),
6044 YESNO(*opts
& SWM_CW_RESIZABLE
));
6046 if ((*opts
& SWM_CW_RIGHT
) && MAX_X(win
) + BORDER(win
) > b
->x
+ b
->w
) {
6047 if (*opts
& SWM_CW_RESIZABLE
)
6048 WIDTH(win
) = b
->x
+ b
->w
- X(win
) - BORDER(win
);
6050 X(win
) = b
->x
+ b
->w
- WIDTH(win
) - BORDER(win
);
6053 if ((*opts
& SWM_CW_LEFT
) && X(win
) + BORDER(win
) < b
->x
) {
6054 if (*opts
& SWM_CW_RESIZABLE
)
6055 WIDTH(win
) -= b
->x
- X(win
) - BORDER(win
);
6057 X(win
) = b
->x
- BORDER(win
);
6060 if ((*opts
& SWM_CW_BOTTOM
) && MAX_Y(win
) + BORDER(win
) > b
->y
+ b
->h
) {
6061 if (*opts
& SWM_CW_RESIZABLE
)
6062 HEIGHT(win
) = b
->y
+ b
->h
- Y(win
) - BORDER(win
);
6064 Y(win
) = b
->y
+ b
->h
- HEIGHT(win
) - BORDER(win
);
6067 if ((*opts
& SWM_CW_TOP
) && Y(win
) + BORDER(win
) < b
->y
) {
6068 if (*opts
& SWM_CW_RESIZABLE
)
6069 HEIGHT(win
) -= b
->y
- Y(win
) - BORDER(win
);
6071 Y(win
) = b
->y
- BORDER(win
);
6074 if (*opts
& SWM_CW_RESIZABLE
) {
6077 if (HEIGHT(win
) < 1)
6083 update_window_color(struct ws_win
*win
)
6087 if (WS_FOCUSED(win
->ws
) && win
->ws
->focus
== win
)
6088 pixel
= MAXIMIZED(win
) ?
6089 &win
->s
->c
[SWM_S_COLOR_FOCUS_MAXIMIZED
].pixel
:
6090 &win
->s
->c
[SWM_S_COLOR_FOCUS
].pixel
;
6092 pixel
= MAXIMIZED(win
) ?
6093 &win
->s
->c
[SWM_S_COLOR_UNFOCUS_MAXIMIZED
].pixel
:
6094 &win
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
;
6096 xcb_change_window_attributes(conn
, win
->id
,
6097 XCB_CW_BORDER_PIXEL
, pixel
);
6101 update_window(struct ws_win
*win
)
6106 mask
= XCB_CONFIG_WINDOW_X
| XCB_CONFIG_WINDOW_Y
|
6107 XCB_CONFIG_WINDOW_WIDTH
| XCB_CONFIG_WINDOW_HEIGHT
|
6108 XCB_CONFIG_WINDOW_BORDER_WIDTH
;
6112 wc
[3] = HEIGHT(win
);
6113 wc
[4] = BORDER(win
);
6115 DNPRINTF(SWM_D_EVENT
, "update_window: win %#x, (x,y) w x h: "
6116 "(%d,%d) %d x %d, bordered: %s\n", win
->id
, wc
[0], wc
[1], wc
[2],
6117 wc
[3], YESNO(win
->bordered
));
6119 xcb_configure_window(conn
, win
->id
, mask
, wc
);
6122 #define SWM_RESIZE_STEPS (50)
6125 resize(struct ws_win
*win
, union arg
*args
)
6127 xcb_timestamp_t timestamp
= 0;
6128 struct swm_region
*r
= NULL
;
6129 struct swm_geometry g
;
6131 int top
= 0, left
= 0;
6133 xcb_cursor_t cursor
;
6134 xcb_query_pointer_reply_t
*xpr
= NULL
;
6135 xcb_generic_event_t
*evt
;
6136 xcb_motion_notify_event_t
*mne
;
6143 if (FULLSCREEN(win
))
6146 /* In max_stack mode, should only resize transients. */
6147 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
] && !TRANS(win
))
6150 DNPRINTF(SWM_D_EVENT
, "resize: win %#x, floating: %s, "
6151 "transient: %#x\n", win
->id
, YESNO(ABOVE(win
)),
6155 store_float_geom(win
);
6156 else if (!(TRANS(win
) || ABOVE(win
)))
6159 ewmh_apply_flags(win
, (win
->ewmh_flags
| SWM_F_MANUAL
| EWMH_F_ABOVE
) &
6161 ewmh_update_wm_state(win
);
6167 /* It's possible for win to have been freed during focus_flush(). */
6168 if (validate_win(win
)) {
6169 DNPRINTF(SWM_D_EVENT
, "resize: invalid win.\n");
6174 case SWM_ARG_ID_WIDTHSHRINK
:
6175 WIDTH(win
) -= SWM_RESIZE_STEPS
;
6178 case SWM_ARG_ID_WIDTHGROW
:
6179 WIDTH(win
) += SWM_RESIZE_STEPS
;
6182 case SWM_ARG_ID_HEIGHTSHRINK
:
6183 HEIGHT(win
) -= SWM_RESIZE_STEPS
;
6186 case SWM_ARG_ID_HEIGHTGROW
:
6187 HEIGHT(win
) += SWM_RESIZE_STEPS
;
6194 region_containment(win
, r
, SWM_CW_ALLSIDES
| SWM_CW_RESIZABLE
|
6195 SWM_CW_HARDBOUNDARY
);
6197 store_float_geom(win
);
6201 region_containment(win
, r
, SWM_CW_ALLSIDES
| SWM_CW_RESIZABLE
|
6202 SWM_CW_SOFTBOUNDARY
);
6205 /* get cursor offset from window root */
6206 xpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
6213 if (xpr
->win_x
< WIDTH(win
) / 2)
6216 if (xpr
->win_y
< HEIGHT(win
) / 2)
6219 if (args
->id
== SWM_ARG_ID_CENTER
)
6220 cursor
= cursors
[XC_SIZING
].cid
;
6222 cursor
= cursors
[left
? XC_TOP_LEFT_CORNER
:
6223 XC_TOP_RIGHT_CORNER
].cid
;
6225 cursor
= cursors
[left
? XC_BOTTOM_LEFT_CORNER
:
6226 XC_BOTTOM_RIGHT_CORNER
].cid
;
6228 xcb_grab_pointer(conn
, 0, win
->id
, MOUSEMASK
,
6229 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
, XCB_WINDOW_NONE
, cursor
,
6234 while (resizing
&& (evt
= xcb_wait_for_event(conn
))) {
6235 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
6236 case XCB_BUTTON_RELEASE
:
6237 DNPRINTF(SWM_D_EVENT
, "resize: BUTTON_RELEASE\n");
6240 case XCB_MOTION_NOTIFY
:
6241 mne
= (xcb_motion_notify_event_t
*)evt
;
6242 /* cursor offset/delta from start of the operation */
6243 dx
= mne
->root_x
- xpr
->root_x
;
6244 dy
= mne
->root_y
- xpr
->root_y
;
6250 if (args
->id
== SWM_ARG_ID_CENTER
) {
6251 if (g
.h
/ 2 + dy
< 1)
6255 HEIGHT(win
) = g
.h
+ 2 * dy
;
6263 HEIGHT(win
) = g
.h
+ dy
;
6270 if (args
->id
== SWM_ARG_ID_CENTER
) {
6271 if (g
.w
/ 2 + dx
< 1)
6275 WIDTH(win
) = g
.w
+ 2 * dx
;
6283 WIDTH(win
) = g
.w
+ dx
;
6286 /* not free, don't sync more than 120 times / second */
6287 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
6288 timestamp
= mne
->time
;
6289 regionize(win
, mne
->root_x
, mne
->root_y
);
6290 region_containment(win
, r
, SWM_CW_ALLSIDES
|
6291 SWM_CW_RESIZABLE
| SWM_CW_HARDBOUNDARY
|
6292 SWM_CW_SOFTBOUNDARY
);
6299 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
6300 ((xcb_key_press_event_t
*)evt
)->time
);
6306 /* It's possible for win to have been freed above. */
6307 if (validate_win(win
)) {
6308 DNPRINTF(SWM_D_EVENT
, "resize: invalid win.\n");
6316 region_containment(win
, r
, SWM_CW_ALLSIDES
| SWM_CW_RESIZABLE
|
6317 SWM_CW_HARDBOUNDARY
| SWM_CW_SOFTBOUNDARY
);
6321 store_float_geom(win
);
6323 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
6325 DNPRINTF(SWM_D_EVENT
, "resize: done.\n");
6329 resize_step(struct swm_region
*r
, union arg
*args
)
6331 struct ws_win
*win
= NULL
;
6333 if (r
&& r
->ws
&& r
->ws
->focus
)
6343 /* Try to set window region based on supplied coordinates or window center. */
6345 regionize(struct ws_win
*win
, int x
, int y
)
6347 struct swm_region
*r
= NULL
;
6349 r
= region_under(win
->s
, x
, y
);
6351 r
= region_under(win
->s
, X(win
) + WIDTH(win
) / 2,
6352 Y(win
) + HEIGHT(win
) / 2);
6354 if (r
!= NULL
&& r
!= win
->ws
->r
) {
6355 if (clear_maximized(r
->ws
) > 0)
6358 win_to_ws(win
, r
->ws
->idx
, false);
6360 /* Set focus on new ws. */
6361 unfocus_win(r
->ws
->focus
);
6369 #define SWM_MOVE_STEPS (50)
6372 move(struct ws_win
*win
, union arg
*args
)
6374 struct swm_region
*r
;
6375 xcb_timestamp_t timestamp
= 0;
6377 xcb_query_pointer_reply_t
*qpr
= NULL
;
6378 xcb_generic_event_t
*evt
;
6379 xcb_motion_notify_event_t
*mne
;
6380 bool moving
, restack
= false;
6385 if ((r
= win
->ws
->r
) == NULL
)
6388 if (FULLSCREEN(win
))
6391 DNPRINTF(SWM_D_EVENT
, "move: win %#x, floating: %s, transient: "
6392 "%#x\n", win
->id
, YESNO(ABOVE(win
)), win
->transient
);
6394 /* in max_stack mode should only move transients */
6395 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
] && !TRANS(win
))
6398 if (!(ABOVE(win
) || TRANS(win
)) || MAXIMIZED(win
)) {
6399 store_float_geom(win
);
6403 ewmh_apply_flags(win
, (win
->ewmh_flags
| SWM_F_MANUAL
| EWMH_F_ABOVE
) &
6405 ewmh_update_wm_state(win
);
6412 /* It's possible for win to have been freed during focus_flush(). */
6413 if (validate_win(win
)) {
6414 DNPRINTF(SWM_D_EVENT
, "move: invalid win.\n");
6420 case SWM_ARG_ID_MOVELEFT
:
6421 X(win
) -= (SWM_MOVE_STEPS
- border_width
);
6424 case SWM_ARG_ID_MOVERIGHT
:
6425 X(win
) += (SWM_MOVE_STEPS
- border_width
);
6428 case SWM_ARG_ID_MOVEUP
:
6429 Y(win
) -= (SWM_MOVE_STEPS
- border_width
);
6432 case SWM_ARG_ID_MOVEDOWN
:
6433 Y(win
) += (SWM_MOVE_STEPS
- border_width
);
6440 regionize(win
, -1, -1);
6441 region_containment(win
, win
->ws
->r
, SWM_CW_ALLSIDES
);
6443 store_float_geom(win
);
6447 xcb_grab_pointer(conn
, 0, win
->id
, MOUSEMASK
,
6448 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
,
6449 XCB_WINDOW_NONE
, cursors
[XC_FLEUR
].cid
, XCB_CURRENT_TIME
);
6451 /* get cursor offset from window root */
6452 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
6455 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
6459 regionize(win
, qpr
->root_x
, qpr
->root_y
);
6460 region_containment(win
, win
->ws
->r
, SWM_CW_ALLSIDES
|
6461 SWM_CW_SOFTBOUNDARY
);
6465 while (moving
&& (evt
= xcb_wait_for_event(conn
))) {
6466 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
6467 case XCB_BUTTON_RELEASE
:
6468 DNPRINTF(SWM_D_EVENT
, "move: BUTTON_RELEASE\n");
6471 case XCB_MOTION_NOTIFY
:
6472 mne
= (xcb_motion_notify_event_t
*)evt
;
6473 DNPRINTF(SWM_D_EVENT
, "motion: root: %#x\n", mne
->root
);
6474 X(win
) = mne
->root_x
- qpr
->win_x
- border_width
;
6475 Y(win
) = mne
->root_y
- qpr
->win_y
- border_width
;
6477 /* not free, don't sync more than 120 times / second */
6478 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
6479 timestamp
= mne
->time
;
6480 regionize(win
, mne
->root_x
, mne
->root_y
);
6481 region_containment(win
, win
->ws
->r
,
6482 SWM_CW_ALLSIDES
| SWM_CW_SOFTBOUNDARY
);
6489 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
6490 ((xcb_key_press_event_t
*)evt
)->time
);
6496 /* It's possible for win to have been freed above. */
6497 if (validate_win(win
)) {
6498 DNPRINTF(SWM_D_EVENT
, "move: invalid win.\n");
6506 region_containment(win
, win
->ws
->r
, SWM_CW_ALLSIDES
|
6507 SWM_CW_SOFTBOUNDARY
);
6511 store_float_geom(win
);
6513 /* New region set to fullscreen layout. */
6514 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
]) {
6521 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
6522 DNPRINTF(SWM_D_EVENT
, "move: done.\n");
6526 move_step(struct swm_region
*r
, union arg
*args
)
6528 struct ws_win
*win
= NULL
;
6530 if (r
&& r
->ws
&& r
->ws
->focus
)
6535 if (!TRANS(win
) && !ABOVE(win
))
6543 /* key definitions */
6545 char name
[SWM_FUNCNAME_LEN
];
6546 void (*func
)(struct swm_region
*r
, union arg
*);
6548 } keyfuncs
[KF_INVALID
+ 1] = {
6549 /* name function argument */
6550 { "bar_toggle", bar_toggle
, {.id
= SWM_ARG_ID_BAR_TOGGLE
} },
6551 { "bar_toggle_ws", bar_toggle
, {.id
= SWM_ARG_ID_BAR_TOGGLE_WS
} },
6552 { "button2", pressbutton
, {2} },
6553 { "cycle_layout", cycle_layout
, {0} },
6554 { "flip_layout", stack_config
, {.id
= SWM_ARG_ID_FLIPLAYOUT
} },
6555 { "float_toggle", floating_toggle
,{0} },
6556 { "focus_main", focus
, {.id
= SWM_ARG_ID_FOCUSMAIN
} },
6557 { "focus_next", focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
6558 { "focus_prev", focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
6559 { "focus_urgent", focus
, {.id
= SWM_ARG_ID_FOCUSURGENT
} },
6560 { "maximize_toggle", maximize_toggle
,{0} },
6561 { "height_grow", resize_step
, {.id
= SWM_ARG_ID_HEIGHTGROW
} },
6562 { "height_shrink", resize_step
, {.id
= SWM_ARG_ID_HEIGHTSHRINK
} },
6563 { "iconify", iconify
, {0} },
6564 { "master_shrink", stack_config
, {.id
= SWM_ARG_ID_MASTERSHRINK
} },
6565 { "master_grow", stack_config
, {.id
= SWM_ARG_ID_MASTERGROW
} },
6566 { "master_add", stack_config
, {.id
= SWM_ARG_ID_MASTERADD
} },
6567 { "master_del", stack_config
, {.id
= SWM_ARG_ID_MASTERDEL
} },
6568 { "move_down", move_step
, {.id
= SWM_ARG_ID_MOVEDOWN
} },
6569 { "move_left", move_step
, {.id
= SWM_ARG_ID_MOVELEFT
} },
6570 { "move_right", move_step
, {.id
= SWM_ARG_ID_MOVERIGHT
} },
6571 { "move_up", move_step
, {.id
= SWM_ARG_ID_MOVEUP
} },
6572 { "mvrg_1", send_to_rg
, {.id
= 0} },
6573 { "mvrg_2", send_to_rg
, {.id
= 1} },
6574 { "mvrg_3", send_to_rg
, {.id
= 2} },
6575 { "mvrg_4", send_to_rg
, {.id
= 3} },
6576 { "mvrg_5", send_to_rg
, {.id
= 4} },
6577 { "mvrg_6", send_to_rg
, {.id
= 5} },
6578 { "mvrg_7", send_to_rg
, {.id
= 6} },
6579 { "mvrg_8", send_to_rg
, {.id
= 7} },
6580 { "mvrg_9", send_to_rg
, {.id
= 8} },
6581 { "mvws_1", send_to_ws
, {.id
= 0} },
6582 { "mvws_2", send_to_ws
, {.id
= 1} },
6583 { "mvws_3", send_to_ws
, {.id
= 2} },
6584 { "mvws_4", send_to_ws
, {.id
= 3} },
6585 { "mvws_5", send_to_ws
, {.id
= 4} },
6586 { "mvws_6", send_to_ws
, {.id
= 5} },
6587 { "mvws_7", send_to_ws
, {.id
= 6} },
6588 { "mvws_8", send_to_ws
, {.id
= 7} },
6589 { "mvws_9", send_to_ws
, {.id
= 8} },
6590 { "mvws_10", send_to_ws
, {.id
= 9} },
6591 { "mvws_11", send_to_ws
, {.id
= 10} },
6592 { "mvws_12", send_to_ws
, {.id
= 11} },
6593 { "mvws_13", send_to_ws
, {.id
= 12} },
6594 { "mvws_14", send_to_ws
, {.id
= 13} },
6595 { "mvws_15", send_to_ws
, {.id
= 14} },
6596 { "mvws_16", send_to_ws
, {.id
= 15} },
6597 { "mvws_17", send_to_ws
, {.id
= 16} },
6598 { "mvws_18", send_to_ws
, {.id
= 17} },
6599 { "mvws_19", send_to_ws
, {.id
= 18} },
6600 { "mvws_20", send_to_ws
, {.id
= 19} },
6601 { "mvws_21", send_to_ws
, {.id
= 20} },
6602 { "mvws_22", send_to_ws
, {.id
= 21} },
6603 { "name_workspace", name_workspace
, {0} },
6604 { "quit", quit
, {0} },
6605 { "raise_toggle", raise_toggle
, {0} },
6606 { "restart", restart
, {0} },
6607 { "rg_1", focusrg
, {.id
= 0} },
6608 { "rg_2", focusrg
, {.id
= 1} },
6609 { "rg_3", focusrg
, {.id
= 2} },
6610 { "rg_4", focusrg
, {.id
= 3} },
6611 { "rg_5", focusrg
, {.id
= 4} },
6612 { "rg_6", focusrg
, {.id
= 5} },
6613 { "rg_7", focusrg
, {.id
= 6} },
6614 { "rg_8", focusrg
, {.id
= 7} },
6615 { "rg_9", focusrg
, {.id
= 8} },
6616 { "rg_next", cyclerg
, {.id
= SWM_ARG_ID_CYCLERG_UP
} },
6617 { "rg_prev", cyclerg
, {.id
= SWM_ARG_ID_CYCLERG_DOWN
} },
6618 { "screen_next", cyclerg
, {.id
= SWM_ARG_ID_CYCLERG_UP
} },
6619 { "screen_prev", cyclerg
, {.id
= SWM_ARG_ID_CYCLERG_DOWN
} },
6620 { "search_win", search_win
, {0} },
6621 { "search_workspace", search_workspace
, {0} },
6622 { "spawn_custom", NULL
, {0} },
6623 { "stack_inc", stack_config
, {.id
= SWM_ARG_ID_STACKINC
} },
6624 { "stack_dec", stack_config
, {.id
= SWM_ARG_ID_STACKDEC
} },
6625 { "stack_reset", stack_config
, {.id
= SWM_ARG_ID_STACKRESET
} },
6626 { "swap_main", swapwin
, {.id
= SWM_ARG_ID_SWAPMAIN
} },
6627 { "swap_next", swapwin
, {.id
= SWM_ARG_ID_SWAPNEXT
} },
6628 { "swap_prev", swapwin
, {.id
= SWM_ARG_ID_SWAPPREV
} },
6629 { "uniconify", uniconify
, {0} },
6630 { "version", version
, {0} },
6631 { "width_grow", resize_step
, {.id
= SWM_ARG_ID_WIDTHGROW
} },
6632 { "width_shrink", resize_step
, {.id
= SWM_ARG_ID_WIDTHSHRINK
} },
6633 { "wind_del", wkill
, {.id
= SWM_ARG_ID_DELETEWINDOW
} },
6634 { "wind_kill", wkill
, {.id
= SWM_ARG_ID_KILLWINDOW
} },
6635 { "ws_1", switchws
, {.id
= 0} },
6636 { "ws_2", switchws
, {.id
= 1} },
6637 { "ws_3", switchws
, {.id
= 2} },
6638 { "ws_4", switchws
, {.id
= 3} },
6639 { "ws_5", switchws
, {.id
= 4} },
6640 { "ws_6", switchws
, {.id
= 5} },
6641 { "ws_7", switchws
, {.id
= 6} },
6642 { "ws_8", switchws
, {.id
= 7} },
6643 { "ws_9", switchws
, {.id
= 8} },
6644 { "ws_10", switchws
, {.id
= 9} },
6645 { "ws_11", switchws
, {.id
= 10} },
6646 { "ws_12", switchws
, {.id
= 11} },
6647 { "ws_13", switchws
, {.id
= 12} },
6648 { "ws_14", switchws
, {.id
= 13} },
6649 { "ws_15", switchws
, {.id
= 14} },
6650 { "ws_16", switchws
, {.id
= 15} },
6651 { "ws_17", switchws
, {.id
= 16} },
6652 { "ws_18", switchws
, {.id
= 17} },
6653 { "ws_19", switchws
, {.id
= 18} },
6654 { "ws_20", switchws
, {.id
= 19} },
6655 { "ws_21", switchws
, {.id
= 20} },
6656 { "ws_22", switchws
, {.id
= 21} },
6657 { "ws_next", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP
} },
6658 { "ws_next_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP_ALL
} },
6659 { "ws_next_move", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_MOVE_UP
} },
6660 { "ws_prev", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN
} },
6661 { "ws_prev_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN_ALL
} },
6662 { "ws_prev_move", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_MOVE_DOWN
} },
6663 { "ws_prior", priorws
, {0} },
6664 { "dumpwins", dumpwins
, {0} }, /* MUST BE LAST */
6665 { "invalid key func", NULL
, {0} },
6669 key_cmp(struct key
*kp1
, struct key
*kp2
)
6671 if (kp1
->keysym
< kp2
->keysym
)
6673 if (kp1
->keysym
> kp2
->keysym
)
6676 if (kp1
->mod
< kp2
->mod
)
6678 if (kp1
->mod
> kp2
->mod
)
6685 enum { client_click
, root_click
};
6687 unsigned int action
;
6689 unsigned int button
;
6690 void (*func
)(struct ws_win
*, union arg
*);
6693 #define MODKEY_SHIFT MODKEY | XCB_MOD_MASK_SHIFT
6694 /* action key mouse button func args */
6695 { client_click
, MODKEY
, XCB_BUTTON_INDEX_3
, resize
, {.id
= SWM_ARG_ID_DONTCENTER
} },
6696 { client_click
, MODKEY_SHIFT
, XCB_BUTTON_INDEX_3
, resize
, {.id
= SWM_ARG_ID_CENTER
} },
6697 { client_click
, MODKEY
, XCB_BUTTON_INDEX_1
, move
, {0} },
6702 update_modkey(unsigned int mod
)
6708 RB_FOREACH(kp
, key_tree
, &keys
)
6709 if (kp
->mod
& XCB_MOD_MASK_SHIFT
)
6710 kp
->mod
= mod
| XCB_MOD_MASK_SHIFT
;
6714 for (i
= 0; i
< LENGTH(buttons
); i
++)
6715 if (buttons
[i
].mask
& XCB_MOD_MASK_SHIFT
)
6716 buttons
[i
].mask
= mod
| XCB_MOD_MASK_SHIFT
;
6718 buttons
[i
].mask
= mod
;
6722 spawn_expand(struct swm_region
*r
, union arg
*args
, const char *spawn_name
,
6725 struct spawn_prog
*prog
= NULL
;
6727 char *ap
, **real_args
;
6729 /* suppress unused warning since var is needed */
6732 DNPRINTF(SWM_D_SPAWN
, "spawn_expand: %s\n", spawn_name
);
6735 TAILQ_FOREACH(prog
, &spawns
, entry
) {
6736 if (strcasecmp(spawn_name
, prog
->name
) == 0)
6740 warnx("spawn_custom: program %s not found", spawn_name
);
6744 /* make room for expanded args */
6745 if ((real_args
= calloc(prog
->argc
+ 1, sizeof(char *))) == NULL
)
6746 err(1, "spawn_custom: calloc real_args");
6748 /* expand spawn_args into real_args */
6749 for (i
= c
= 0; i
< prog
->argc
; i
++) {
6751 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: raw arg: %s\n", ap
);
6752 if (strcasecmp(ap
, "$bar_border") == 0) {
6754 strdup(r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].name
))
6756 err(1, "spawn_custom border color");
6757 } else if (strcasecmp(ap
, "$bar_color") == 0) {
6759 strdup(r
->s
->c
[SWM_S_COLOR_BAR
].name
))
6761 err(1, "spawn_custom bar color");
6762 } else if (strcasecmp(ap
, "$bar_font") == 0) {
6763 if ((real_args
[c
] = strdup(bar_fonts
))
6765 err(1, "spawn_custom bar fonts");
6766 } else if (strcasecmp(ap
, "$bar_font_color") == 0) {
6768 strdup(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].name
))
6770 err(1, "spawn_custom color font");
6771 } else if (strcasecmp(ap
, "$color_focus") == 0) {
6773 strdup(r
->s
->c
[SWM_S_COLOR_FOCUS
].name
))
6775 err(1, "spawn_custom color focus");
6776 } else if (strcasecmp(ap
, "$color_focus_maximized") == 0) {
6778 strdup(r
->s
->c
[SWM_S_COLOR_FOCUS_MAXIMIZED
].name
))
6780 err(1, "spawn_custom color focus maximized");
6781 } else if (strcasecmp(ap
, "$color_unfocus") == 0) {
6783 strdup(r
->s
->c
[SWM_S_COLOR_UNFOCUS
].name
))
6785 err(1, "spawn_custom color unfocus");
6786 } else if (strcasecmp(ap
, "$color_unfocus_maximized") == 0) {
6788 strdup(r
->s
->c
[SWM_S_COLOR_UNFOCUS_MAXIMIZED
].name
))
6790 err(1, "spawn_custom color unfocus maximized");
6791 } else if (strcasecmp(ap
, "$region_index") == 0) {
6792 if (asprintf(&real_args
[c
], "%d",
6793 get_region_index(r
) + 1) < 1)
6794 err(1, "spawn_custom region index");
6795 } else if (strcasecmp(ap
, "$workspace_index") == 0) {
6796 if (asprintf(&real_args
[c
], "%d", r
->ws
->idx
+ 1) < 1)
6797 err(1, "spawn_custom workspace index");
6798 } else if (strcasecmp(ap
, "$dmenu_bottom") == 0) {
6801 if ((real_args
[c
] = strdup("-b")) == NULL
)
6802 err(1, "spawn_custom workspace index");
6804 /* no match --> copy as is */
6805 if ((real_args
[c
] = strdup(ap
)) == NULL
)
6806 err(1, "spawn_custom strdup(ap)");
6808 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: cooked arg: %s\n",
6814 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: result: ");
6815 for (i
= 0; i
< c
; ++i
)
6816 DPRINTF("\"%s\" ", real_args
[i
]);
6819 *ret_args
= real_args
;
6824 spawn_custom(struct swm_region
*r
, union arg
*args
, const char *spawn_name
)
6830 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
6834 spawn(r
->ws
->idx
, &a
, true);
6836 for (i
= 0; i
< spawn_argc
; i
++)
6842 spawn_select(struct swm_region
*r
, union arg
*args
, const char *spawn_name
,
6849 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
6853 if (pipe(select_list_pipe
) == -1)
6854 err(1, "pipe error");
6855 if (pipe(select_resp_pipe
) == -1)
6856 err(1, "pipe error");
6858 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
6859 err(1, "could not disable SIGPIPE");
6860 switch (*pid
= fork()) {
6862 err(1, "cannot fork");
6865 if (dup2(select_list_pipe
[0], STDIN_FILENO
) == -1)
6867 if (dup2(select_resp_pipe
[1], STDOUT_FILENO
) == -1)
6869 close(select_list_pipe
[1]);
6870 close(select_resp_pipe
[0]);
6871 spawn(r
->ws
->idx
, &a
, false);
6873 default: /* parent */
6874 close(select_list_pipe
[0]);
6875 close(select_resp_pipe
[1]);
6879 for (i
= 0; i
< spawn_argc
; i
++)
6884 /* Argument tokenizer. */
6887 char *arg
, *cp
, *next
;
6888 bool single_quoted
= false, double_quoted
= false;
6893 /* Eat and move characters until end of argument is found. */
6894 for (arg
= next
= cp
= *sp
; *cp
!= '\0'; ++cp
) {
6895 if (!double_quoted
&& *cp
== '\'') {
6896 /* Eat single-quote. */
6897 single_quoted
= !single_quoted
;
6898 } else if (!single_quoted
&& *cp
== '"') {
6899 /* Eat double-quote. */
6900 double_quoted
= !double_quoted
;
6901 } else if (!single_quoted
&& *cp
== '\\' && *(cp
+ 1) == '"') {
6902 /* Eat backslash; copy escaped character to arg. */
6904 } else if (!single_quoted
&& !double_quoted
&& *cp
== '\\' &&
6905 (*(cp
+ 1) == '\'' || *(cp
+ 1) == ' ')) {
6906 /* Eat backslash; move escaped character. */
6908 } else if (!single_quoted
&& !double_quoted
&&
6909 (*cp
== ' ' || *cp
== '\t')) {
6910 /* Terminate argument. */
6912 /* Point sp to beginning of next argument. */
6916 /* Move regular character. */
6921 /* Terminate argument if end of string. */
6931 spawn_insert(const char *name
, const char *args
, int flags
)
6933 struct spawn_prog
*sp
;
6934 char *arg
, *cp
, *ptr
;
6936 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: %s[%s]\n", name
, args
);
6938 if (args
== NULL
|| *args
== '\0')
6941 if ((sp
= calloc(1, sizeof *sp
)) == NULL
)
6942 err(1, "spawn_insert: calloc");
6943 if ((sp
->name
= strdup(name
)) == NULL
)
6944 err(1, "spawn_insert: strdup");
6946 /* Convert the arguments to an argument list. */
6947 if ((ptr
= cp
= strdup(args
)) == NULL
)
6948 err(1, "spawn_insert: strdup");
6949 while ((arg
= argsep(&ptr
)) != NULL
) {
6950 /* Null argument; skip it. */
6955 if ((sp
->argv
= realloc(sp
->argv
, sp
->argc
*
6956 sizeof *sp
->argv
)) == NULL
)
6957 err(1, "spawn_insert: realloc");
6958 if ((sp
->argv
[sp
->argc
- 1] = strdup(arg
)) == NULL
)
6959 err(1, "spawn_insert: strdup");
6965 DNPRINTF(SWM_D_SPAWN
, "arg %d: [%s]\n", sp
->argc
, sp
->argv
[sp
->argc
-1]);
6966 TAILQ_INSERT_TAIL(&spawns
, sp
, entry
);
6967 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: leave\n");
6971 spawn_remove(struct spawn_prog
*sp
)
6975 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: %s\n", sp
->name
);
6977 TAILQ_REMOVE(&spawns
, sp
, entry
);
6978 for (i
= 0; i
< sp
->argc
; i
++)
6984 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: leave\n");
6988 spawn_find(const char *name
)
6990 struct spawn_prog
*sp
;
6992 TAILQ_FOREACH(sp
, &spawns
, entry
)
6993 if (strcasecmp(sp
->name
, name
) == 0)
7000 setspawn(const char *name
, const char *args
, int flags
)
7002 struct spawn_prog
*sp
;
7004 DNPRINTF(SWM_D_SPAWN
, "setspawn: %s\n", name
);
7009 /* Remove any old spawn under the same name. */
7010 if ((sp
= spawn_find(name
)) != NULL
)
7014 spawn_insert(name
, args
, flags
);
7016 warnx("error: setspawn: cannot find program: %s", name
);
7018 DNPRINTF(SWM_D_SPAWN
, "setspawn: leave\n");
7022 setconfspawn(const char *selector
, const char *value
, int flags
)
7026 if (selector
== NULL
|| strlen(selector
) == 0)
7029 args
= expand_tilde(value
);
7031 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: [%s] [%s]\n", selector
, args
);
7033 setspawn(selector
, args
, flags
);
7036 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: done.\n");
7041 validate_spawns(void)
7043 struct spawn_prog
*sp
;
7044 char which
[PATH_MAX
];
7049 RB_FOREACH(kp
, key_tree
, &keys
) {
7050 if (kp
->funcid
!= KF_SPAWN_CUSTOM
)
7054 sp
= spawn_find(kp
->spawn_name
);
7055 if (sp
== NULL
|| sp
->flags
& SWM_SPAWN_OPTIONAL
)
7058 /* verify we have the goods */
7059 snprintf(which
, sizeof which
, "which %s", sp
->argv
[0]);
7060 DNPRINTF(SWM_D_CONF
, "validate_spawns: which %s\n",
7062 for (i
= strlen("which "); i
< strlen(which
); i
++)
7063 if (which
[i
] == ' ') {
7067 if (system(which
) != 0)
7068 add_startup_exception("could not find %s",
7069 &which
[strlen("which ")]);
7076 setconfspawn("lock", "xlock", 0);
7078 setconfspawn("term", "xterm", 0);
7079 setconfspawn("spawn_term", "xterm", 0);
7081 setconfspawn("menu", "dmenu_run"
7085 " -nf $bar_font_color"
7087 " -sf $bar_color", 0);
7089 setconfspawn("search", "dmenu"
7094 " -nf $bar_font_color"
7096 " -sf $bar_color", 0);
7098 setconfspawn("name_workspace", "dmenu"
7103 " -nf $bar_font_color"
7105 " -sf $bar_color", 0);
7107 /* These are not verified for existence, even with a binding set. */
7108 setconfspawn("screenshot_all", "screenshot.sh full", SWM_SPAWN_OPTIONAL
);
7109 setconfspawn("screenshot_wind", "screenshot.sh window", SWM_SPAWN_OPTIONAL
);
7110 setconfspawn("initscr", "initscreen.sh", SWM_SPAWN_OPTIONAL
);
7114 #define SWM_MODNAME_SIZE 32
7115 #define SWM_KEY_WS "\n+ \t"
7117 parsekeys(const char *keystr
, unsigned int currmod
, unsigned int *mod
, KeySym
*ks
)
7119 char *str
, *cp
, *name
;
7122 DNPRINTF(SWM_D_KEY
, "parsekeys: enter [%s]\n", keystr
);
7123 if (mod
== NULL
|| ks
== NULL
) {
7124 DNPRINTF(SWM_D_KEY
, "parsekeys: no mod or key vars\n");
7127 if (keystr
== NULL
|| strlen(keystr
) == 0) {
7128 DNPRINTF(SWM_D_KEY
, "parsekeys: no keystr\n");
7132 if ((cp
= str
= strdup(keystr
)) == NULL
)
7133 err(1, "parsekeys: strdup");
7135 *ks
= XCB_NO_SYMBOL
;
7137 while ((name
= strsep(&cp
, SWM_KEY_WS
)) != NULL
) {
7138 DNPRINTF(SWM_D_KEY
, "parsekeys: key [%s]\n", name
);
7140 cp
+= (long)strspn(cp
, SWM_KEY_WS
);
7141 if (strncasecmp(name
, "MOD", SWM_MODNAME_SIZE
) == 0)
7143 else if (strncasecmp(name
, "Mod1", SWM_MODNAME_SIZE
) == 0)
7144 *mod
|= XCB_MOD_MASK_1
;
7145 else if (strncasecmp(name
, "Mod2", SWM_MODNAME_SIZE
) == 0)
7146 *mod
+= XCB_MOD_MASK_2
;
7147 else if (strncmp(name
, "Mod3", SWM_MODNAME_SIZE
) == 0)
7148 *mod
|= XCB_MOD_MASK_3
;
7149 else if (strncmp(name
, "Mod4", SWM_MODNAME_SIZE
) == 0)
7150 *mod
|= XCB_MOD_MASK_4
;
7151 else if (strncasecmp(name
, "SHIFT", SWM_MODNAME_SIZE
) == 0)
7152 *mod
|= XCB_MOD_MASK_SHIFT
;
7153 else if (strncasecmp(name
, "CONTROL", SWM_MODNAME_SIZE
) == 0)
7154 *mod
|= XCB_MOD_MASK_CONTROL
;
7156 *ks
= XStringToKeysym(name
);
7157 XConvertCase(*ks
, ks
, &uks
);
7158 if (ks
== XCB_NO_SYMBOL
) {
7160 "parsekeys: invalid key %s\n",
7169 DNPRINTF(SWM_D_KEY
, "parsekeys: leave ok\n");
7174 strdupsafe(const char *str
)
7179 return (strdup(str
));
7183 key_insert(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
7184 const char *spawn_name
)
7188 DNPRINTF(SWM_D_KEY
, "key_insert: enter %s [%s]\n",
7189 keyfuncs
[kfid
].name
, spawn_name
);
7191 if ((kp
= malloc(sizeof *kp
)) == NULL
)
7192 err(1, "key_insert: malloc");
7197 kp
->spawn_name
= strdupsafe(spawn_name
);
7198 RB_INSERT(key_tree
, &keys
, kp
);
7200 DNPRINTF(SWM_D_KEY
, "key_insert: leave\n");
7204 key_lookup(unsigned int mod
, KeySym ks
)
7211 return (RB_FIND(key_tree
, &keys
, &kp
));
7215 key_remove(struct key
*kp
)
7217 DNPRINTF(SWM_D_KEY
, "key_remove: %s\n", keyfuncs
[kp
->funcid
].name
);
7219 RB_REMOVE(key_tree
, &keys
, kp
);
7220 free(kp
->spawn_name
);
7223 DNPRINTF(SWM_D_KEY
, "key_remove: leave\n");
7227 key_replace(struct key
*kp
, unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
7228 const char *spawn_name
)
7230 DNPRINTF(SWM_D_KEY
, "key_replace: %s [%s]\n", keyfuncs
[kp
->funcid
].name
,
7234 key_insert(mod
, ks
, kfid
, spawn_name
);
7236 DNPRINTF(SWM_D_KEY
, "key_replace: leave\n");
7240 setkeybinding(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
7241 const char *spawn_name
)
7245 DNPRINTF(SWM_D_KEY
, "setkeybinding: enter %s [%s]\n",
7246 keyfuncs
[kfid
].name
, spawn_name
);
7248 if ((kp
= key_lookup(mod
, ks
)) != NULL
) {
7249 if (kfid
== KF_INVALID
)
7252 key_replace(kp
, mod
, ks
, kfid
, spawn_name
);
7253 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
7256 if (kfid
== KF_INVALID
) {
7257 warnx("bind: Key combination already unbound.");
7258 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
7262 key_insert(mod
, ks
, kfid
, spawn_name
);
7263 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
7267 setconfbinding(const char *selector
, const char *value
, int flags
)
7269 enum keyfuncid kfid
;
7272 struct spawn_prog
*sp
;
7274 /* suppress unused warning since var is needed */
7277 DNPRINTF(SWM_D_KEY
, "setconfbinding: enter selector: [%s], "
7278 "value: [%s]\n", selector
, value
);
7279 if (selector
== NULL
|| strlen(selector
) == 0) {
7280 DNPRINTF(SWM_D_KEY
, "setconfbinding: unbind %s\n", value
);
7281 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
7283 setkeybinding(mod
, ks
, kfid
, NULL
);
7288 /* search by key function name */
7289 for (kfid
= 0; kfid
< KF_INVALID
; (kfid
)++) {
7290 if (strncasecmp(selector
, keyfuncs
[kfid
].name
,
7291 SWM_FUNCNAME_LEN
) == 0) {
7292 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match "
7293 "keyfunc\n", selector
);
7294 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
7295 setkeybinding(mod
, ks
, kfid
, NULL
);
7301 /* search by custom spawn name */
7302 if ((sp
= spawn_find(selector
)) != NULL
) {
7303 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match "
7304 "spawn\n", selector
);
7305 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
7306 setkeybinding(mod
, ks
, KF_SPAWN_CUSTOM
,
7312 DNPRINTF(SWM_D_KEY
, "setconfbinding: no match\n");
7319 #define MODKEY_SHIFT MODKEY | XCB_MOD_MASK_SHIFT
7320 setkeybinding(MODKEY
, XK_b
, KF_BAR_TOGGLE
, NULL
);
7321 setkeybinding(MODKEY_SHIFT
, XK_b
, KF_BAR_TOGGLE_WS
,NULL
);
7322 setkeybinding(MODKEY
, XK_v
, KF_BUTTON2
, NULL
);
7323 setkeybinding(MODKEY
, XK_space
, KF_CYCLE_LAYOUT
,NULL
);
7324 setkeybinding(MODKEY_SHIFT
, XK_backslash
, KF_FLIP_LAYOUT
, NULL
);
7325 setkeybinding(MODKEY
, XK_t
, KF_FLOAT_TOGGLE
,NULL
);
7326 setkeybinding(MODKEY
, XK_m
, KF_FOCUS_MAIN
, NULL
);
7327 setkeybinding(MODKEY
, XK_j
, KF_FOCUS_NEXT
, NULL
);
7328 setkeybinding(MODKEY
, XK_Tab
, KF_FOCUS_NEXT
, NULL
);
7329 setkeybinding(MODKEY
, XK_k
, KF_FOCUS_PREV
, NULL
);
7330 setkeybinding(MODKEY_SHIFT
, XK_Tab
, KF_FOCUS_PREV
, NULL
);
7331 setkeybinding(MODKEY
, XK_u
, KF_FOCUS_URGENT
,NULL
);
7332 setkeybinding(MODKEY
, XK_e
, KF_MAXIMIZE_TOGGLE
,NULL
);
7333 setkeybinding(MODKEY_SHIFT
, XK_equal
, KF_HEIGHT_GROW
,NULL
);
7334 setkeybinding(MODKEY_SHIFT
, XK_minus
, KF_HEIGHT_SHRINK
,NULL
);
7335 setkeybinding(MODKEY
, XK_w
, KF_ICONIFY
, NULL
);
7336 setkeybinding(MODKEY
, XK_h
, KF_MASTER_SHRINK
, NULL
);
7337 setkeybinding(MODKEY
, XK_l
, KF_MASTER_GROW
, NULL
);
7338 setkeybinding(MODKEY
, XK_comma
, KF_MASTER_ADD
, NULL
);
7339 setkeybinding(MODKEY
, XK_period
, KF_MASTER_DEL
, NULL
);
7340 setkeybinding(MODKEY_SHIFT
, XK_bracketright
,KF_MOVE_DOWN
,NULL
);
7341 setkeybinding(MODKEY
, XK_bracketleft
, KF_MOVE_LEFT
,NULL
);
7342 setkeybinding(MODKEY
, XK_bracketright
,KF_MOVE_RIGHT
,NULL
);
7343 setkeybinding(MODKEY_SHIFT
, XK_bracketleft
, KF_MOVE_UP
, NULL
);
7344 setkeybinding(MODKEY_SHIFT
, XK_KP_End
, KF_MVRG_1
, NULL
);
7345 setkeybinding(MODKEY_SHIFT
, XK_KP_Down
, KF_MVRG_2
, NULL
);
7346 setkeybinding(MODKEY_SHIFT
, XK_KP_Next
, KF_MVRG_3
, NULL
);
7347 setkeybinding(MODKEY_SHIFT
, XK_KP_Left
, KF_MVRG_4
, NULL
);
7348 setkeybinding(MODKEY_SHIFT
, XK_KP_Begin
, KF_MVRG_5
, NULL
);
7349 setkeybinding(MODKEY_SHIFT
, XK_KP_Right
, KF_MVRG_6
, NULL
);
7350 setkeybinding(MODKEY_SHIFT
, XK_KP_Home
, KF_MVRG_7
, NULL
);
7351 setkeybinding(MODKEY_SHIFT
, XK_KP_Up
, KF_MVRG_8
, NULL
);
7352 setkeybinding(MODKEY_SHIFT
, XK_KP_Prior
, KF_MVRG_9
, NULL
);
7353 setkeybinding(MODKEY_SHIFT
, XK_1
, KF_MVWS_1
, NULL
);
7354 setkeybinding(MODKEY_SHIFT
, XK_2
, KF_MVWS_2
, NULL
);
7355 setkeybinding(MODKEY_SHIFT
, XK_3
, KF_MVWS_3
, NULL
);
7356 setkeybinding(MODKEY_SHIFT
, XK_4
, KF_MVWS_4
, NULL
);
7357 setkeybinding(MODKEY_SHIFT
, XK_5
, KF_MVWS_5
, NULL
);
7358 setkeybinding(MODKEY_SHIFT
, XK_6
, KF_MVWS_6
, NULL
);
7359 setkeybinding(MODKEY_SHIFT
, XK_7
, KF_MVWS_7
, NULL
);
7360 setkeybinding(MODKEY_SHIFT
, XK_8
, KF_MVWS_8
, NULL
);
7361 setkeybinding(MODKEY_SHIFT
, XK_9
, KF_MVWS_9
, NULL
);
7362 setkeybinding(MODKEY_SHIFT
, XK_0
, KF_MVWS_10
, NULL
);
7363 setkeybinding(MODKEY_SHIFT
, XK_F1
, KF_MVWS_11
, NULL
);
7364 setkeybinding(MODKEY_SHIFT
, XK_F2
, KF_MVWS_12
, NULL
);
7365 setkeybinding(MODKEY_SHIFT
, XK_F3
, KF_MVWS_13
, NULL
);
7366 setkeybinding(MODKEY_SHIFT
, XK_F4
, KF_MVWS_14
, NULL
);
7367 setkeybinding(MODKEY_SHIFT
, XK_F5
, KF_MVWS_15
, NULL
);
7368 setkeybinding(MODKEY_SHIFT
, XK_F6
, KF_MVWS_16
, NULL
);
7369 setkeybinding(MODKEY_SHIFT
, XK_F7
, KF_MVWS_17
, NULL
);
7370 setkeybinding(MODKEY_SHIFT
, XK_F8
, KF_MVWS_18
, NULL
);
7371 setkeybinding(MODKEY_SHIFT
, XK_F9
, KF_MVWS_19
, NULL
);
7372 setkeybinding(MODKEY_SHIFT
, XK_F10
, KF_MVWS_20
, NULL
);
7373 setkeybinding(MODKEY_SHIFT
, XK_F11
, KF_MVWS_21
, NULL
);
7374 setkeybinding(MODKEY_SHIFT
, XK_F12
, KF_MVWS_22
, NULL
);
7375 setkeybinding(MODKEY_SHIFT
, XK_slash
, KF_NAME_WORKSPACE
,NULL
);
7376 setkeybinding(MODKEY_SHIFT
, XK_q
, KF_QUIT
, NULL
);
7377 setkeybinding(MODKEY_SHIFT
, XK_r
, KF_RAISE_TOGGLE
,NULL
);
7378 setkeybinding(MODKEY
, XK_q
, KF_RESTART
, NULL
);
7379 setkeybinding(MODKEY
, XK_KP_End
, KF_RG_1
, NULL
);
7380 setkeybinding(MODKEY
, XK_KP_Down
, KF_RG_2
, NULL
);
7381 setkeybinding(MODKEY
, XK_KP_Next
, KF_RG_3
, NULL
);
7382 setkeybinding(MODKEY
, XK_KP_Left
, KF_RG_4
, NULL
);
7383 setkeybinding(MODKEY
, XK_KP_Begin
, KF_RG_5
, NULL
);
7384 setkeybinding(MODKEY
, XK_KP_Right
, KF_RG_6
, NULL
);
7385 setkeybinding(MODKEY
, XK_KP_Home
, KF_RG_7
, NULL
);
7386 setkeybinding(MODKEY
, XK_KP_Up
, KF_RG_8
, NULL
);
7387 setkeybinding(MODKEY
, XK_KP_Prior
, KF_RG_9
, NULL
);
7388 setkeybinding(MODKEY_SHIFT
, XK_Right
, KF_RG_NEXT
, NULL
);
7389 setkeybinding(MODKEY_SHIFT
, XK_Left
, KF_RG_PREV
, NULL
);
7390 setkeybinding(MODKEY
, XK_f
, KF_SEARCH_WIN
, NULL
);
7391 setkeybinding(MODKEY
, XK_slash
, KF_SEARCH_WORKSPACE
,NULL
);
7392 setkeybinding(MODKEY_SHIFT
, XK_i
, KF_SPAWN_CUSTOM
,"initscr");
7393 setkeybinding(MODKEY_SHIFT
, XK_Delete
, KF_SPAWN_CUSTOM
,"lock");
7394 setkeybinding(MODKEY
, XK_p
, KF_SPAWN_CUSTOM
,"menu");
7395 setkeybinding(MODKEY
, XK_s
, KF_SPAWN_CUSTOM
,"screenshot_all");
7396 setkeybinding(MODKEY_SHIFT
, XK_s
, KF_SPAWN_CUSTOM
,"screenshot_wind");
7397 setkeybinding(MODKEY_SHIFT
, XK_Return
, KF_SPAWN_CUSTOM
,"term");
7398 setkeybinding(MODKEY_SHIFT
, XK_comma
, KF_STACK_INC
, NULL
);
7399 setkeybinding(MODKEY_SHIFT
, XK_period
, KF_STACK_DEC
, NULL
);
7400 setkeybinding(MODKEY_SHIFT
, XK_space
, KF_STACK_RESET
, NULL
);
7401 setkeybinding(MODKEY
, XK_Return
, KF_SWAP_MAIN
, NULL
);
7402 setkeybinding(MODKEY_SHIFT
, XK_j
, KF_SWAP_NEXT
, NULL
);
7403 setkeybinding(MODKEY_SHIFT
, XK_k
, KF_SWAP_PREV
, NULL
);
7404 setkeybinding(MODKEY_SHIFT
, XK_w
, KF_UNICONIFY
, NULL
);
7405 setkeybinding(MODKEY_SHIFT
, XK_v
, KF_VERSION
, NULL
);
7406 setkeybinding(MODKEY
, XK_equal
, KF_WIDTH_GROW
, NULL
);
7407 setkeybinding(MODKEY
, XK_minus
, KF_WIDTH_SHRINK
,NULL
);
7408 setkeybinding(MODKEY
, XK_x
, KF_WIND_DEL
, NULL
);
7409 setkeybinding(MODKEY_SHIFT
, XK_x
, KF_WIND_KILL
, NULL
);
7410 setkeybinding(MODKEY
, XK_1
, KF_WS_1
, NULL
);
7411 setkeybinding(MODKEY
, XK_2
, KF_WS_2
, NULL
);
7412 setkeybinding(MODKEY
, XK_3
, KF_WS_3
, NULL
);
7413 setkeybinding(MODKEY
, XK_4
, KF_WS_4
, NULL
);
7414 setkeybinding(MODKEY
, XK_5
, KF_WS_5
, NULL
);
7415 setkeybinding(MODKEY
, XK_6
, KF_WS_6
, NULL
);
7416 setkeybinding(MODKEY
, XK_7
, KF_WS_7
, NULL
);
7417 setkeybinding(MODKEY
, XK_8
, KF_WS_8
, NULL
);
7418 setkeybinding(MODKEY
, XK_9
, KF_WS_9
, NULL
);
7419 setkeybinding(MODKEY
, XK_0
, KF_WS_10
, NULL
);
7420 setkeybinding(MODKEY
, XK_F1
, KF_WS_11
, NULL
);
7421 setkeybinding(MODKEY
, XK_F2
, KF_WS_12
, NULL
);
7422 setkeybinding(MODKEY
, XK_F3
, KF_WS_13
, NULL
);
7423 setkeybinding(MODKEY
, XK_F4
, KF_WS_14
, NULL
);
7424 setkeybinding(MODKEY
, XK_F5
, KF_WS_15
, NULL
);
7425 setkeybinding(MODKEY
, XK_F6
, KF_WS_16
, NULL
);
7426 setkeybinding(MODKEY
, XK_F7
, KF_WS_17
, NULL
);
7427 setkeybinding(MODKEY
, XK_F8
, KF_WS_18
, NULL
);
7428 setkeybinding(MODKEY
, XK_F9
, KF_WS_19
, NULL
);
7429 setkeybinding(MODKEY
, XK_F10
, KF_WS_20
, NULL
);
7430 setkeybinding(MODKEY
, XK_F11
, KF_WS_21
, NULL
);
7431 setkeybinding(MODKEY
, XK_F12
, KF_WS_22
, NULL
);
7432 setkeybinding(MODKEY
, XK_Right
, KF_WS_NEXT
, NULL
);
7433 setkeybinding(MODKEY
, XK_Left
, KF_WS_PREV
, NULL
);
7434 setkeybinding(MODKEY
, XK_Up
, KF_WS_NEXT_ALL
, NULL
);
7435 setkeybinding(MODKEY
, XK_Down
, KF_WS_PREV_ALL
, NULL
);
7436 setkeybinding(MODKEY_SHIFT
, XK_Up
, KF_WS_NEXT_MOVE
,NULL
);
7437 setkeybinding(MODKEY_SHIFT
, XK_Down
, KF_WS_PREV_MOVE
,NULL
);
7438 setkeybinding(MODKEY
, XK_a
, KF_WS_PRIOR
, NULL
);
7440 setkeybinding(MODKEY_SHIFT
, XK_d
, KF_DUMPWINS
, NULL
);
7450 while (RB_EMPTY(&keys
) == 0) {
7451 kp
= RB_ROOT(&keys
);
7457 setkeymapping(const char *selector
, const char *value
, int flags
)
7459 char *keymapping_file
;
7461 /* suppress unused warnings since vars are needed */
7465 DNPRINTF(SWM_D_KEY
, "setkeymapping: enter\n");
7467 keymapping_file
= expand_tilde(value
);
7470 /* load new key bindings; if it fails, revert to default bindings */
7471 if (conf_load(keymapping_file
, SWM_CONF_KEYMAPPING
)) {
7476 free(keymapping_file
);
7478 DNPRINTF(SWM_D_KEY
, "setkeymapping: leave\n");
7483 updatenumlockmask(void)
7486 xcb_get_modifier_mapping_reply_t
*modmap_r
;
7487 xcb_keycode_t
*modmap
, kc
, *keycode
;
7491 modmap_r
= xcb_get_modifier_mapping_reply(conn
,
7492 xcb_get_modifier_mapping(conn
),
7495 modmap
= xcb_get_modifier_mapping_keycodes(modmap_r
);
7496 for (i
= 0; i
< 8; i
++) {
7497 for (j
= 0; j
< modmap_r
->keycodes_per_modifier
; j
++) {
7498 kc
= modmap
[i
* modmap_r
->keycodes_per_modifier
7500 keycode
= xcb_key_symbols_get_keycode(syms
,
7504 numlockmask
= (1 << i
);
7511 DNPRINTF(SWM_D_MISC
, "updatenumlockmask: %d\n", numlockmask
);
7518 int num_screens
, k
, j
;
7519 unsigned int modifiers
[4];
7520 xcb_keycode_t
*code
;
7522 DNPRINTF(SWM_D_MISC
, "grabkeys\n");
7523 updatenumlockmask();
7526 modifiers
[1] = numlockmask
;
7527 modifiers
[2] = XCB_MOD_MASK_LOCK
;
7528 modifiers
[3] = numlockmask
| XCB_MOD_MASK_LOCK
;
7530 num_screens
= get_screen_count();
7531 for (k
= 0; k
< num_screens
; k
++) {
7532 if (TAILQ_EMPTY(&screens
[k
].rl
))
7534 xcb_ungrab_key(conn
, XCB_GRAB_ANY
, screens
[k
].root
,
7536 RB_FOREACH(kp
, key_tree
, &keys
) {
7537 /* Skip unused ws binds. */
7538 if ((int)kp
->funcid
> KF_WS_1
+ workspace_limit
- 1 &&
7539 kp
->funcid
<= KF_WS_22
)
7542 /* Skip unused mvws binds. */
7543 if ((int)kp
->funcid
> KF_MVWS_1
+ workspace_limit
- 1 &&
7544 kp
->funcid
<= KF_MVWS_22
)
7547 if ((code
= xcb_key_symbols_get_keycode(syms
,
7549 for (j
= 0; j
< LENGTH(modifiers
); j
++)
7550 xcb_grab_key(conn
, 1,
7552 kp
->mod
| modifiers
[j
],
7553 *code
, XCB_GRAB_MODE_SYNC
,
7554 XCB_GRAB_MODE_SYNC
);
7562 grabbuttons(struct ws_win
*win
)
7564 unsigned int modifiers
[4];
7567 DNPRINTF(SWM_D_MOUSE
, "grabbuttons: win %#x\n", win
->id
);
7568 updatenumlockmask();
7571 modifiers
[1] = numlockmask
;
7572 modifiers
[2] = XCB_MOD_MASK_LOCK
;
7573 modifiers
[3] = numlockmask
| XCB_MOD_MASK_LOCK
;
7575 for (i
= 0; i
< LENGTH(buttons
); i
++)
7576 if (buttons
[i
].action
== client_click
)
7577 for (j
= 0; j
< LENGTH(modifiers
); ++j
)
7578 xcb_grab_button(conn
, 0, win
->id
, BUTTONMASK
,
7579 XCB_GRAB_MODE_SYNC
, XCB_GRAB_MODE_ASYNC
,
7580 XCB_WINDOW_NONE
, XCB_CURSOR_NONE
,
7581 buttons
[i
].button
, buttons
[i
].mask
|
7585 const char *quirkname
[] = {
7586 "NONE", /* config string for "no value" */
7594 "FOCUSONMAP_SINGLE",
7600 /* SWM_Q_DELIM: retain '|' for back compat for now (2009-08-11) */
7601 #define SWM_Q_DELIM "\n|+ \t"
7603 parsequirks(const char *qstr
, uint32_t *quirk
, int *ws
)
7605 char *str
, *cp
, *name
;
7608 if (quirk
== NULL
|| qstr
== NULL
)
7611 if ((str
= strdup(qstr
)) == NULL
)
7612 err(1, "parsequirks: strdup");
7616 while ((name
= strsep(&cp
, SWM_Q_DELIM
)) != NULL
) {
7618 cp
+= (long)strspn(cp
, SWM_Q_DELIM
);
7620 if (sscanf(name
, "WS[%d]", ws
) == 1) {
7626 for (i
= 0; i
< LENGTH(quirkname
); i
++) {
7627 if (strncasecmp(name
, quirkname
[i
],
7628 SWM_QUIRK_LEN
) == 0) {
7629 DNPRINTF(SWM_D_QUIRK
,
7630 "parsequirks: %s\n", name
);
7636 *quirk
|= 1 << (i
-1);
7640 if (i
>= LENGTH(quirkname
)) {
7641 DNPRINTF(SWM_D_QUIRK
,
7642 "parsequirks: invalid quirk [%s]\n", name
);
7653 quirk_insert(const char *class, const char *instance
, const char *name
,
7654 uint32_t quirk
, int ws
)
7658 bool failed
= false;
7660 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: class: %s, instance: %s, name: %s,"
7661 " value: %u, ws: %d\n", class, instance
, name
, quirk
, ws
);
7663 if ((qp
= malloc(sizeof *qp
)) == NULL
)
7664 err(1, "quirk_insert: malloc");
7666 if ((qp
->class = strdup(class)) == NULL
)
7667 err(1, "quirk_insert: strdup");
7668 if ((qp
->instance
= strdup(instance
)) == NULL
)
7669 err(1, "quirk_insert: strdup");
7670 if ((qp
->name
= strdup(name
)) == NULL
)
7671 err(1, "quirk_insert: strdup");
7673 if (asprintf(&str
, "^%s$", class) == -1)
7674 err(1, "quirk_insert: asprintf");
7675 if (regcomp(&qp
->regex_class
, str
, REG_EXTENDED
| REG_NOSUB
)) {
7676 add_startup_exception("regex failed to compile quirk 'class' "
7677 "field: %s", class);
7680 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: compiled: %s\n", str
);
7683 if (asprintf(&str
, "^%s$", instance
) == -1)
7684 err(1, "quirk_insert: asprintf");
7685 if (regcomp(&qp
->regex_instance
, str
, REG_EXTENDED
| REG_NOSUB
)) {
7686 add_startup_exception("regex failed to compile quirk 'instance'"
7687 " field: %s", instance
);
7690 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: compiled: %s\n", str
);
7693 if (asprintf(&str
, "^%s$", name
) == -1)
7694 err(1, "quirk_insert: asprintf");
7695 if (regcomp(&qp
->regex_name
, str
, REG_EXTENDED
| REG_NOSUB
)) {
7696 add_startup_exception("regex failed to compile quirk 'name' "
7700 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: compiled: %s\n", str
);
7704 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: regex error; skipping.\n");
7709 TAILQ_INSERT_TAIL(&quirks
, qp
, entry
);
7711 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: leave\n");
7715 quirk_remove(struct quirk
*qp
)
7717 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: %s:%s [%u]\n", qp
->class,
7718 qp
->name
, qp
->quirk
);
7720 TAILQ_REMOVE(&quirks
, qp
, entry
);
7723 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: leave\n");
7727 quirk_free(struct quirk
*qp
)
7729 regfree(&qp
->regex_class
);
7730 regfree(&qp
->regex_instance
);
7731 regfree(&qp
->regex_name
);
7739 quirk_replace(struct quirk
*qp
, const char *class, const char *instance
,
7740 const char *name
, uint32_t quirk
, int ws
)
7742 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: %s:%s:%s [%u], ws: %d\n", qp
->class,
7743 qp
->instance
, qp
->name
, qp
->quirk
, qp
->ws
);
7746 quirk_insert(class, instance
, name
, quirk
, ws
);
7748 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: leave\n");
7752 setquirk(const char *class, const char *instance
, const char *name
,
7753 uint32_t quirk
, int ws
)
7757 DNPRINTF(SWM_D_QUIRK
, "setquirk: enter %s:%s:%s [%u], ws: %d\n", class,
7758 instance
, name
, quirk
, ws
);
7760 /* Remove/replace existing quirk. */
7761 TAILQ_FOREACH(qp
, &quirks
, entry
) {
7762 if (strcmp(qp
->class, class) == 0 &&
7763 strcmp(qp
->instance
, instance
) == 0 &&
7764 strcmp(qp
->name
, name
) == 0) {
7765 if (quirk
== 0 && ws
== -1)
7768 quirk_replace(qp
, class, instance
, name
, quirk
,
7770 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
7775 /* Only insert if quirk is not NONE or forced ws is set. */
7776 if (quirk
|| ws
!= -1)
7777 quirk_insert(class, instance
, name
, quirk
, ws
);
7779 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
7782 /* Eat '\' in str used to escape square brackets and colon. */
7784 unescape_selector(char *str
)
7788 for (cp
= str
; *str
!= '\0'; ++str
, ++cp
) {
7789 if (*str
== '\\' && (*(str
+ 1) == ':' || *(str
+ 1) == ']' ||
7799 setconfquirk(const char *selector
, const char *value
, int flags
)
7801 char *str
, *cp
, *class;
7802 char *instance
= NULL
, *name
= NULL
;
7803 int retval
, count
= 0, ws
= -1;
7806 /* suppress unused warning since var is needed */
7809 if (selector
== NULL
|| strlen(selector
) == 0)
7812 if ((str
= strdup(selector
)) == NULL
)
7813 err(1, "setconfquirk: strdup");
7815 /* Find non-escaped colon. */
7822 for (++cp
; *cp
!= '\0'; ++cp
) {
7823 if (*cp
== ':' && *(cp
- 1) != '\\') {
7829 unescape_selector(class);
7831 instance
= class + strlen(class) + 1;
7832 unescape_selector(instance
);
7838 name
= instance
+ strlen(instance
) + 1;
7839 unescape_selector(name
);
7844 DNPRINTF(SWM_D_CONF
, "setconfquirk: class: %s, instance: %s, "
7845 "name: %s\n", class, instance
, name
);
7847 if ((retval
= parsequirks(value
, &qrks
, &ws
)) == 0)
7848 setquirk(class, instance
, name
, qrks
, ws
);
7857 setquirk("MPlayer", "xv", ".*",
7858 SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
| SWM_Q_FOCUSPREV
, -1);
7859 setquirk("OpenOffice.org 3.2", "VCLSalFrame", ".*",
7861 setquirk("Firefox-bin", "firefox-bin", ".*",
7863 setquirk("Firefox", "Dialog", ".*",
7865 setquirk("Gimp", "gimp", ".*",
7866 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
7867 setquirk("XTerm", "xterm", ".*",
7868 SWM_Q_XTERM_FONTADJ
, -1);
7869 setquirk("xine", "Xine Window", ".*",
7870 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
7871 setquirk("Xitk", "Xitk Combo", ".*",
7872 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
7873 setquirk("xine", "xine Panel", ".*",
7874 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
7875 setquirk("Xitk", "Xine Window", ".*",
7876 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
7877 setquirk("xine", "xine Video Fullscreen Window", ".*",
7878 SWM_Q_FULLSCREEN
| SWM_Q_FLOAT
, -1);
7879 setquirk("pcb", "pcb", ".*",
7881 setquirk("SDL_App", "SDL_App", ".*",
7882 SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
, -1);
7885 /* conf file stuff */
7886 #define SWM_CONF_FILE "spectrwm.conf"
7887 #define SWM_CONF_FILE_OLD "scrotwm.conf"
7891 SWM_S_BAR_AT_BOTTOM
,
7892 SWM_S_BAR_BORDER_WIDTH
,
7895 SWM_S_BAR_ENABLED_WS
,
7900 SWM_S_BOUNDARY_WIDTH
,
7901 SWM_S_CLOCK_ENABLED
,
7904 SWM_S_CYCLE_VISIBLE
,
7906 SWM_S_DISABLE_BORDER
,
7908 SWM_S_FOCUS_CLOSE_WRAP
,
7909 SWM_S_FOCUS_DEFAULT
,
7911 SWM_S_ICONIC_ENABLED
,
7912 SWM_S_JAVA_WORKAROUND
,
7913 SWM_S_REGION_PADDING
,
7918 SWM_S_STACK_ENABLED
,
7921 SWM_S_URGENT_COLLAPSE
,
7922 SWM_S_URGENT_ENABLED
,
7923 SWM_S_VERBOSE_LAYOUT
,
7925 SWM_S_WINDOW_CLASS_ENABLED
,
7926 SWM_S_WINDOW_INSTANCE_ENABLED
,
7927 SWM_S_WINDOW_NAME_ENABLED
,
7928 SWM_S_WORKSPACE_LIMIT
,
7929 SWM_S_WORKSPACE_NAME
,
7933 setconfvalue(const char *selector
, const char *value
, int flags
)
7935 struct workspace
*ws
;
7936 int i
, ws_id
, num_screens
;
7937 char *b
, *str
, s
[1024];
7940 case SWM_S_BAR_ACTION
:
7942 if ((bar_argv
[0] = expand_tilde(value
)) == NULL
)
7943 err(1, "setconfvalue: bar_action");
7945 case SWM_S_BAR_AT_BOTTOM
:
7946 bar_at_bottom
= (atoi(value
) != 0);
7948 case SWM_S_BAR_BORDER_WIDTH
:
7949 bar_border_width
= atoi(value
);
7950 if (bar_border_width
< 0)
7951 bar_border_width
= 0;
7953 case SWM_S_BAR_DELAY
:
7954 /* No longer needed; leave to not break old conf files. */
7956 case SWM_S_BAR_ENABLED
:
7957 bar_enabled
= (atoi(value
) != 0);
7959 case SWM_S_BAR_ENABLED_WS
:
7960 ws_id
= atoi(selector
) - 1;
7961 if (ws_id
< 0 || ws_id
>= workspace_limit
)
7962 errx(1, "setconfvalue: bar_enabled_ws: invalid "
7963 "workspace %d.", ws_id
+ 1);
7965 num_screens
= get_screen_count();
7966 for (i
= 0; i
< num_screens
; i
++) {
7967 ws
= (struct workspace
*)&screens
[i
].ws
;
7968 ws
[ws_id
].bar_enabled
= (atoi(value
) != 0);
7971 case SWM_S_BAR_FONT
:
7973 if (asprintf(&bar_fonts
, "%s,%s", value
, bar_fonts
) == -1)
7974 err(1, "setconfvalue: asprintf: failed to allocate "
7975 "memory for bar_fonts.");
7978 /* If already in xft mode, then we are done. */
7979 if (!bar_font_legacy
)
7982 if ((str
= strdup(value
)) == NULL
)
7983 err(1, "setconfvalue: strdup");
7985 /* If there are any non-XLFD entries, switch to Xft mode. */
7986 while ((b
= strsep(&str
, ",")) != NULL
) {
7990 bar_font_legacy
= false;
7997 case SWM_S_BAR_FORMAT
:
7999 if ((bar_format
= strdup(value
)) == NULL
)
8000 err(1, "setconfvalue: bar_format");
8002 case SWM_S_BAR_JUSTIFY
:
8003 if (strcmp(value
, "left") == 0)
8004 bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
8005 else if (strcmp(value
, "center") == 0)
8006 bar_justify
= SWM_BAR_JUSTIFY_CENTER
;
8007 else if (strcmp(value
, "right") == 0)
8008 bar_justify
= SWM_BAR_JUSTIFY_RIGHT
;
8010 errx(1, "invalid bar_justify");
8012 case SWM_S_BORDER_WIDTH
:
8013 border_width
= atoi(value
);
8014 if (border_width
< 0)
8017 case SWM_S_BOUNDARY_WIDTH
:
8018 boundary_width
= atoi(value
);
8019 if (boundary_width
< 0)
8022 case SWM_S_CLOCK_ENABLED
:
8023 clock_enabled
= (atoi(value
) != 0);
8025 case SWM_S_CLOCK_FORMAT
:
8026 #ifndef SWM_DENY_CLOCK_FORMAT
8028 if ((clock_format
= strdup(value
)) == NULL
)
8029 err(1, "setconfvalue: clock_format");
8032 case SWM_S_CYCLE_EMPTY
:
8033 cycle_empty
= (atoi(value
) != 0);
8035 case SWM_S_CYCLE_VISIBLE
:
8036 cycle_visible
= (atoi(value
) != 0);
8038 case SWM_S_DIALOG_RATIO
:
8039 dialog_ratio
= atof(value
);
8040 if (dialog_ratio
> 1.0 || dialog_ratio
<= .3)
8043 case SWM_S_DISABLE_BORDER
:
8044 disable_border
= (atoi(value
) != 0);
8046 case SWM_S_FOCUS_CLOSE
:
8047 if (strcmp(value
, "first") == 0)
8048 focus_close
= SWM_STACK_BOTTOM
;
8049 else if (strcmp(value
, "last") == 0)
8050 focus_close
= SWM_STACK_TOP
;
8051 else if (strcmp(value
, "next") == 0)
8052 focus_close
= SWM_STACK_ABOVE
;
8053 else if (strcmp(value
, "previous") == 0)
8054 focus_close
= SWM_STACK_BELOW
;
8056 errx(1, "focus_close");
8058 case SWM_S_FOCUS_CLOSE_WRAP
:
8059 focus_close_wrap
= (atoi(value
) != 0);
8061 case SWM_S_FOCUS_DEFAULT
:
8062 if (strcmp(value
, "last") == 0)
8063 focus_default
= SWM_STACK_TOP
;
8064 else if (strcmp(value
, "first") == 0)
8065 focus_default
= SWM_STACK_BOTTOM
;
8067 errx(1, "focus_default");
8069 case SWM_S_FOCUS_MODE
:
8070 if (strcmp(value
, "default") == 0)
8071 focus_mode
= SWM_FOCUS_DEFAULT
;
8072 else if (strcmp(value
, "follow") == 0 ||
8073 strcmp(value
, "follow_cursor") == 0)
8074 focus_mode
= SWM_FOCUS_FOLLOW
;
8075 else if (strcmp(value
, "manual") == 0)
8076 focus_mode
= SWM_FOCUS_MANUAL
;
8078 errx(1, "focus_mode");
8080 case SWM_S_ICONIC_ENABLED
:
8081 iconic_enabled
= (atoi(value
) != 0);
8083 case SWM_S_JAVA_WORKAROUND
:
8084 java_workaround
= (atoi(value
) != 0);
8086 case SWM_S_REGION_PADDING
:
8087 region_padding
= atoi(value
);
8088 if (region_padding
< 0)
8091 case SWM_S_SPAWN_ORDER
:
8092 if (strcmp(value
, "first") == 0)
8093 spawn_position
= SWM_STACK_BOTTOM
;
8094 else if (strcmp(value
, "last") == 0)
8095 spawn_position
= SWM_STACK_TOP
;
8096 else if (strcmp(value
, "next") == 0)
8097 spawn_position
= SWM_STACK_ABOVE
;
8098 else if (strcmp(value
, "previous") == 0)
8099 spawn_position
= SWM_STACK_BELOW
;
8101 errx(1, "spawn_position");
8103 case SWM_S_SPAWN_TERM
:
8104 setconfspawn("term", value
, 0);
8105 setconfspawn("spawn_term", value
, 0);
8108 /* No longer needed; leave to not break old conf files. */
8110 case SWM_S_SS_ENABLED
:
8111 /* No longer needed; leave to not break old conf files. */
8113 case SWM_S_STACK_ENABLED
:
8114 stack_enabled
= (atoi(value
) != 0);
8116 case SWM_S_TERM_WIDTH
:
8117 term_width
= atoi(value
);
8121 case SWM_S_TILE_GAP
:
8122 tile_gap
= atoi(value
);
8124 case SWM_S_URGENT_COLLAPSE
:
8125 urgent_collapse
= (atoi(value
) != 0);
8127 case SWM_S_URGENT_ENABLED
:
8128 urgent_enabled
= (atoi(value
) != 0);
8130 case SWM_S_VERBOSE_LAYOUT
:
8131 verbose_layout
= (atoi(value
) != 0);
8132 for (i
= 0; layouts
[i
].l_stack
!= NULL
; i
++) {
8134 layouts
[i
].l_string
= fancy_stacker
;
8136 layouts
[i
].l_string
= plain_stacker
;
8139 case SWM_S_WARP_POINTER
:
8140 warp_pointer
= (atoi(value
) != 0);
8142 case SWM_S_WINDOW_CLASS_ENABLED
:
8143 window_class_enabled
= (atoi(value
) != 0);
8145 case SWM_S_WINDOW_INSTANCE_ENABLED
:
8146 window_instance_enabled
= (atoi(value
) != 0);
8148 case SWM_S_WINDOW_NAME_ENABLED
:
8149 window_name_enabled
= (atoi(value
) != 0);
8151 case SWM_S_WORKSPACE_LIMIT
:
8152 workspace_limit
= atoi(value
);
8153 if (workspace_limit
> SWM_WS_MAX
)
8154 workspace_limit
= SWM_WS_MAX
;
8155 else if (workspace_limit
< 1)
8156 workspace_limit
= 1;
8158 ewmh_update_desktops();
8160 case SWM_S_WORKSPACE_NAME
:
8161 if (getenv("SWM_STARTED") != NULL
)
8165 if (sscanf(value
, "ws[%d]:%1023c", &ws_id
, s
) != 2)
8166 errx(1, "invalid entry, should be 'ws[<idx>]:name'");
8168 if (ws_id
< 0 || ws_id
>= workspace_limit
)
8169 errx(1, "setconfvalue: workspace_name: invalid "
8170 "workspace %d.", ws_id
+ 1);
8172 num_screens
= get_screen_count();
8173 for (i
= 0; i
< num_screens
; ++i
) {
8174 ws
= (struct workspace
*)&screens
[i
].ws
;
8176 if (strlen(s
) > 0) {
8177 free(ws
[ws_id
].name
);
8178 if ((ws
[ws_id
].name
= strdup(s
)) == NULL
)
8179 err(1, "setconfvalue: workspace_name.");
8181 ewmh_update_desktop_names();
8182 ewmh_get_desktop_names();
8193 setconfmodkey(const char *selector
, const char *value
, int flags
)
8195 /* suppress unused warnings since vars are needed */
8199 if (strncasecmp(value
, "Mod1", strlen("Mod1")) == 0)
8200 update_modkey(XCB_MOD_MASK_1
);
8201 else if (strncasecmp(value
, "Mod2", strlen("Mod2")) == 0)
8202 update_modkey(XCB_MOD_MASK_2
);
8203 else if (strncasecmp(value
, "Mod3", strlen("Mod3")) == 0)
8204 update_modkey(XCB_MOD_MASK_3
);
8205 else if (strncasecmp(value
, "Mod4", strlen("Mod4")) == 0)
8206 update_modkey(XCB_MOD_MASK_4
);
8213 setconfcolor(const char *selector
, const char *value
, int flags
)
8215 int first
, last
, i
= 0, num_screens
;
8217 num_screens
= get_screen_count();
8219 /* conf screen indices begin at 1; treat vals <= 0 as 'all screens.' */
8220 if (selector
== NULL
|| strlen(selector
) == 0 ||
8221 (last
= atoi(selector
) - 1) < 0) {
8223 last
= num_screens
- 1;
8228 if (last
>= num_screens
) {
8229 add_startup_exception("invalid screen index: %d out of bounds "
8230 "(maximum %d)", last
+ 1, num_screens
);
8234 for (i
= first
; i
<= last
; ++i
) {
8235 setscreencolor(value
, i
, flags
);
8238 * When setting focus/unfocus colors, we need to also
8239 * set maximize colors to match if they haven't been customized.
8241 if (flags
== SWM_S_COLOR_FOCUS
&&
8242 !screens
[i
].c
[SWM_S_COLOR_FOCUS_MAXIMIZED
].manual
)
8243 setscreencolor(value
, i
, SWM_S_COLOR_FOCUS_MAXIMIZED
);
8244 else if (flags
== SWM_S_COLOR_UNFOCUS
&&
8245 !screens
[i
].c
[SWM_S_COLOR_UNFOCUS_MAXIMIZED
].manual
)
8246 setscreencolor(value
, i
, SWM_S_COLOR_UNFOCUS_MAXIMIZED
);
8248 screens
[i
].c
[flags
].manual
= 1;
8255 setconfregion(const char *selector
, const char *value
, int flags
)
8257 /* suppress unused warnings since vars are needed */
8261 custom_region(value
);
8266 setautorun(const char *selector
, const char *value
, int flags
)
8276 /* suppress unused warnings since vars are needed */
8280 if (getenv("SWM_STARTED"))
8284 if (sscanf(value
, "ws[%d]:%1023c", &ws_id
, s
) != 2)
8285 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
8287 if (ws_id
< 0 || ws_id
>= workspace_limit
)
8288 errx(1, "autorun: invalid workspace %d", ws_id
+ 1);
8290 sp
= expand_tilde((char *)&s
);
8293 * This is a little intricate
8295 * If the pid already exists we simply reuse it because it means it was
8296 * used before AND not claimed by manage_window. We get away with
8297 * altering it in the parent after INSERT because this can not be a race
8300 while ((ap
= strsep(&sp
, " \t")) != NULL
) {
8303 DNPRINTF(SWM_D_SPAWN
, "setautorun: arg [%s]\n", ap
);
8305 if ((a
.argv
= realloc(a
.argv
, argc
* sizeof(char *))) == NULL
)
8306 err(1, "setautorun: realloc");
8307 a
.argv
[argc
- 1] = ap
;
8311 if ((a
.argv
= realloc(a
.argv
, (argc
+ 1) * sizeof(char *))) == NULL
)
8312 err(1, "setautorun: realloc");
8313 a
.argv
[argc
] = NULL
;
8315 if ((pid
= fork()) == 0) {
8316 spawn(ws_id
, &a
, true);
8325 p
= calloc(1, sizeof *p
);
8328 TAILQ_INSERT_TAIL(&pidlist
, p
, entry
);
8338 setlayout(const char *selector
, const char *value
, int flags
)
8340 struct workspace
*ws
;
8341 int ws_id
, i
, x
, mg
, ma
, si
, ar
;
8342 int st
= SWM_V_STACK
, num_screens
;
8346 /* suppress unused warnings since vars are needed */
8350 if (getenv("SWM_STARTED"))
8354 if (sscanf(value
, "ws[%d]:%d:%d:%d:%d:%1023c",
8355 &ws_id
, &mg
, &ma
, &si
, &ar
, s
) != 6)
8356 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
8357 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
8360 if (ws_id
< 0 || ws_id
>= workspace_limit
)
8361 errx(1, "layout: invalid workspace %d", ws_id
+ 1);
8363 if (strcasecmp(s
, "vertical") == 0)
8365 else if (strcasecmp(s
, "vertical_flip") == 0) {
8368 } else if (strcasecmp(s
, "horizontal") == 0)
8370 else if (strcasecmp(s
, "horizontal_flip") == 0) {
8373 } else if (strcasecmp(s
, "fullscreen") == 0)
8376 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
8377 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
8380 num_screens
= get_screen_count();
8381 for (i
= 0; i
< num_screens
; i
++) {
8382 ws
= (struct workspace
*)&screens
[i
].ws
;
8383 ws
[ws_id
].cur_layout
= &layouts
[st
];
8385 ws
[ws_id
].always_raise
= (ar
!= 0);
8386 if (st
== SWM_MAX_STACK
)
8390 for (x
= 0; x
< abs(mg
); x
++) {
8391 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
8392 mg
>= 0 ? SWM_ARG_ID_MASTERGROW
:
8393 SWM_ARG_ID_MASTERSHRINK
);
8396 for (x
= 0; x
< abs(ma
); x
++) {
8397 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
8398 ma
>= 0 ? SWM_ARG_ID_MASTERADD
:
8399 SWM_ARG_ID_MASTERDEL
);
8402 for (x
= 0; x
< abs(si
); x
++) {
8403 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
8404 si
>= 0 ? SWM_ARG_ID_STACKINC
:
8405 SWM_ARG_ID_STACKDEC
);
8409 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
8410 SWM_ARG_ID_FLIPLAYOUT
);
8417 /* config options */
8418 struct config_option
{
8420 int (*func
)(const char*, const char*, int);
8423 struct config_option configopt
[] = {
8424 { "autorun", setautorun
, 0 },
8425 { "bar_action", setconfvalue
, SWM_S_BAR_ACTION
},
8426 { "bar_at_bottom", setconfvalue
, SWM_S_BAR_AT_BOTTOM
},
8427 { "bar_border", setconfcolor
, SWM_S_COLOR_BAR_BORDER
},
8428 { "bar_border_unfocus", setconfcolor
, SWM_S_COLOR_BAR_BORDER_UNFOCUS
},
8429 { "bar_border_width", setconfvalue
, SWM_S_BAR_BORDER_WIDTH
},
8430 { "bar_color", setconfcolor
, SWM_S_COLOR_BAR
},
8431 { "bar_delay", setconfvalue
, SWM_S_BAR_DELAY
},
8432 { "bar_enabled", setconfvalue
, SWM_S_BAR_ENABLED
},
8433 { "bar_enabled_ws", setconfvalue
, SWM_S_BAR_ENABLED_WS
},
8434 { "bar_font", setconfvalue
, SWM_S_BAR_FONT
},
8435 { "bar_font_color", setconfcolor
, SWM_S_COLOR_BAR_FONT
},
8436 { "bar_format", setconfvalue
, SWM_S_BAR_FORMAT
},
8437 { "bar_justify", setconfvalue
, SWM_S_BAR_JUSTIFY
},
8438 { "bind", setconfbinding
, 0 },
8439 { "border_width", setconfvalue
, SWM_S_BORDER_WIDTH
},
8440 { "boundary_width", setconfvalue
, SWM_S_BOUNDARY_WIDTH
},
8441 { "clock_enabled", setconfvalue
, SWM_S_CLOCK_ENABLED
},
8442 { "clock_format", setconfvalue
, SWM_S_CLOCK_FORMAT
},
8443 { "color_focus", setconfcolor
, SWM_S_COLOR_FOCUS
},
8444 { "color_focus_maximized", setconfcolor
, SWM_S_COLOR_FOCUS_MAXIMIZED
},
8445 { "color_unfocus", setconfcolor
, SWM_S_COLOR_UNFOCUS
},
8446 { "color_unfocus_maximized", setconfcolor
, SWM_S_COLOR_UNFOCUS_MAXIMIZED
},
8447 { "cycle_empty", setconfvalue
, SWM_S_CYCLE_EMPTY
},
8448 { "cycle_visible", setconfvalue
, SWM_S_CYCLE_VISIBLE
},
8449 { "dialog_ratio", setconfvalue
, SWM_S_DIALOG_RATIO
},
8450 { "disable_border", setconfvalue
, SWM_S_DISABLE_BORDER
},
8451 { "focus_close", setconfvalue
, SWM_S_FOCUS_CLOSE
},
8452 { "focus_close_wrap", setconfvalue
, SWM_S_FOCUS_CLOSE_WRAP
},
8453 { "focus_default", setconfvalue
, SWM_S_FOCUS_DEFAULT
},
8454 { "focus_mode", setconfvalue
, SWM_S_FOCUS_MODE
},
8455 { "iconic_enabled", setconfvalue
, SWM_S_ICONIC_ENABLED
},
8456 { "java_workaround", setconfvalue
, SWM_S_JAVA_WORKAROUND
},
8457 { "keyboard_mapping", setkeymapping
, 0 },
8458 { "layout", setlayout
, 0 },
8459 { "modkey", setconfmodkey
, 0 },
8460 { "program", setconfspawn
, 0 },
8461 { "quirk", setconfquirk
, 0 },
8462 { "region", setconfregion
, 0 },
8463 { "region_padding", setconfvalue
, SWM_S_REGION_PADDING
},
8464 { "screenshot_app", setconfvalue
, SWM_S_SS_APP
},
8465 { "screenshot_enabled", setconfvalue
, SWM_S_SS_ENABLED
},
8466 { "spawn_position", setconfvalue
, SWM_S_SPAWN_ORDER
},
8467 { "spawn_term", setconfvalue
, SWM_S_SPAWN_TERM
},
8468 { "stack_enabled", setconfvalue
, SWM_S_STACK_ENABLED
},
8469 { "term_width", setconfvalue
, SWM_S_TERM_WIDTH
},
8470 { "tile_gap", setconfvalue
, SWM_S_TILE_GAP
},
8471 { "title_class_enabled", setconfvalue
, SWM_S_WINDOW_CLASS_ENABLED
}, /* For backwards compat. */
8472 { "title_name_enabled", setconfvalue
, SWM_S_WINDOW_INSTANCE_ENABLED
}, /* For backwards compat. */
8473 { "urgent_collapse", setconfvalue
, SWM_S_URGENT_COLLAPSE
},
8474 { "urgent_enabled", setconfvalue
, SWM_S_URGENT_ENABLED
},
8475 { "verbose_layout", setconfvalue
, SWM_S_VERBOSE_LAYOUT
},
8476 { "warp_pointer", setconfvalue
, SWM_S_WARP_POINTER
},
8477 { "window_class_enabled", setconfvalue
, SWM_S_WINDOW_CLASS_ENABLED
},
8478 { "window_instance_enabled", setconfvalue
, SWM_S_WINDOW_INSTANCE_ENABLED
},
8479 { "window_name_enabled", setconfvalue
, SWM_S_WINDOW_NAME_ENABLED
},
8480 { "workspace_limit", setconfvalue
, SWM_S_WORKSPACE_LIMIT
},
8481 { "name", setconfvalue
, SWM_S_WORKSPACE_NAME
},
8485 _add_startup_exception(const char *fmt
, va_list ap
)
8487 if (vasprintf(&startup_exception
, fmt
, ap
) == -1)
8488 warn("%s: asprintf", __func__
);
8492 add_startup_exception(const char *fmt
, ...)
8498 if (startup_exception
)
8501 /* force bar to be enabled due to exception */
8505 _add_startup_exception(fmt
, ap
);
8510 conf_load(const char *filename
, int keymapping
)
8513 char *line
= NULL
, *cp
, *ce
, *optsub
, *optval
= NULL
;
8514 size_t linelen
, lineno
= 0;
8515 int wordlen
, i
, optidx
, count
;
8516 struct config_option
*opt
= NULL
;
8518 DNPRINTF(SWM_D_CONF
, "conf_load: begin\n");
8520 if (filename
== NULL
) {
8521 warnx("conf_load: no filename");
8525 DNPRINTF(SWM_D_CONF
, "conf_load: open %s\n", filename
);
8527 if ((config
= fopen(filename
, "r")) == NULL
) {
8528 warn("conf_load: fopen: %s", filename
);
8532 while (!feof(config
)) {
8536 if ((line
= fparseln(config
, &linelen
, &lineno
, NULL
,
8537 FPARSELN_UNESCCOMM
| FPARSELN_UNESCCONT
)) == NULL
) {
8539 err(1, "%s", filename
);
8544 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
8545 if (cp
[0] == '\0') {
8549 /* get config option */
8550 wordlen
= strcspn(cp
, "=[ \t\n");
8552 add_startup_exception("%s: line %zd: no option found",
8557 for (i
= 0; i
< LENGTH(configopt
); i
++) {
8558 opt
= &configopt
[i
];
8559 if (strncasecmp(cp
, opt
->optname
, wordlen
) == 0 &&
8560 (int)strlen(opt
->optname
) == wordlen
) {
8566 add_startup_exception("%s: line %zd: unknown option "
8567 "%.*s", filename
, lineno
, wordlen
, cp
);
8570 if (keymapping
&& opt
&& strcmp(opt
->optname
, "bind")) {
8571 add_startup_exception("%s: line %zd: invalid option "
8572 "%.*s", filename
, lineno
, wordlen
, cp
);
8576 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
8578 /* get [selector] if any */
8583 /* Get length of selector. */
8584 for (ce
= ++cp
; *ce
!= '\0'; ++ce
) {
8585 /* Find matching (not escaped) bracket. */
8586 if (*ce
== ']' && *(ce
- 1) != '\\') {
8593 add_startup_exception("%s: line %zd: syntax "
8594 "error: unterminated selector", filename
,
8599 /* ce points at ']'; terminate optsub. */
8604 cp
+= strspn(cp
, "= \t\n"); /* eat trailing */
8607 if (strlen(optval
) == 0) {
8608 add_startup_exception("%s: line %zd: must supply value "
8609 "to %s", filename
, lineno
,
8610 configopt
[optidx
].optname
);
8613 /* call function to deal with it all */
8614 if (configopt
[optidx
].func(optsub
, optval
,
8615 configopt
[optidx
].funcflags
) != 0) {
8616 add_startup_exception("%s: line %zd: invalid data for "
8617 "%s", filename
, lineno
, configopt
[optidx
].optname
);
8625 DNPRINTF(SWM_D_CONF
, "conf_load: end\n");
8631 set_child_transient(struct ws_win
*win
, xcb_window_t
*trans
)
8633 struct ws_win
*parent
, *w
;
8634 struct swm_region
*r
;
8635 struct workspace
*ws
;
8636 xcb_icccm_wm_hints_t wmh
;
8638 parent
= find_window(win
->transient
);
8640 parent
->focus_child
= win
;
8642 DNPRINTF(SWM_D_MISC
, "set_child_transient: parent doesn't exist"
8643 " for %#x trans %#x\n", win
->id
, win
->transient
);
8645 r
= root_to_region(win
->s
->root
, SWM_CK_ALL
);
8647 /* parent doen't exist in our window list */
8648 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
8649 if (xcb_icccm_get_wm_hints_reply(conn
,
8650 xcb_icccm_get_wm_hints(conn
, w
->id
),
8652 warnx("can't get hints for %#x", w
->id
);
8656 if (win
->hints
.window_group
!= wmh
.window_group
)
8659 w
->focus_child
= win
;
8660 win
->transient
= w
->id
;
8662 DNPRINTF(SWM_D_MISC
, "set_child_transient: adjusting "
8663 "transient to %#x\n", win
->transient
);
8670 window_get_pid(xcb_window_t win
)
8675 xcb_get_property_cookie_t pc
;
8676 xcb_get_property_reply_t
*pr
;
8678 apid
= get_atom_from_string("_NET_WM_PID");
8679 if (apid
== XCB_ATOM_NONE
)
8682 pc
= xcb_get_property(conn
, 0, win
, apid
, XCB_ATOM_CARDINAL
, 0, 1);
8683 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
8686 if (pr
->type
!= XCB_ATOM_CARDINAL
) {
8691 if (pr
->type
== apid
&& pr
->format
== 32)
8692 ret
= *((pid_t
*)xcb_get_property_value(pr
));
8698 apid
= get_atom_from_string("_SWM_PID");
8699 pc
= xcb_get_property(conn
, 0, win
, apid
, XCB_ATOM_STRING
,
8701 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
8704 if (pr
->type
!= apid
) {
8709 ret
= (pid_t
)strtonum(xcb_get_property_value(pr
), 0, INT_MAX
, &errstr
);
8716 get_swm_ws(xcb_window_t id
)
8722 xcb_get_property_reply_t
*gpr
;
8724 gpr
= xcb_get_property_reply(conn
,
8725 xcb_get_property(conn
, 0, id
, a_swm_ws
,
8726 XCB_ATOM_STRING
, 0, SWM_PROPLEN
),
8731 proplen
= xcb_get_property_value_length(gpr
);
8733 prop
= malloc(proplen
+ 1);
8736 xcb_get_property_value(gpr
),
8738 prop
[proplen
] = '\0';
8745 DNPRINTF(SWM_D_PROP
, "get_swm_ws: _SWM_WS: %s\n", prop
);
8746 ws_idx
= (int)strtonum(prop
, 0, workspace_limit
- 1, &errstr
);
8748 DNPRINTF(SWM_D_PROP
, "get_swm_ws: win #%s: %s",
8758 get_ws_idx(struct ws_win
*win
)
8760 xcb_get_property_reply_t
*gpr
;
8766 gpr
= xcb_get_property_reply(conn
,
8767 xcb_get_property(conn
, 0, win
->id
, ewmh
[_NET_WM_DESKTOP
].atom
,
8768 XCB_ATOM_CARDINAL
, 0, 1),
8771 if (gpr
->type
== XCB_ATOM_CARDINAL
&& gpr
->format
== 32)
8772 ws_idx
= *((int *)xcb_get_property_value(gpr
));
8776 if (ws_idx
== -1 && !(win
->quirks
& SWM_Q_IGNORESPAWNWS
))
8777 ws_idx
= get_swm_ws(win
->id
);
8779 if (ws_idx
> workspace_limit
- 1 || ws_idx
< -1)
8782 DNPRINTF(SWM_D_PROP
, "get_ws_idx: win %#x, ws_idx: %d\n", win
->id
,
8789 manage_window(xcb_window_t id
, int spawn_pos
, bool mapped
)
8791 struct ws_win
*win
, *ww
;
8792 struct swm_region
*r
;
8795 xcb_get_geometry_reply_t
*gr
;
8796 xcb_window_t trans
= XCB_WINDOW_NONE
;
8797 uint32_t i
, wa
[2], new_flags
;
8798 int ws_idx
, force_ws
= -1;
8799 char *class, *instance
, *name
;
8801 if ((win
= find_window(id
)) != NULL
) {
8802 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x already "
8803 "managed; skipping.)\n", id
);
8804 return (win
); /* Already managed. */
8807 /* See if window is on the unmanaged list. */
8808 if ((win
= find_unmanaged_window(id
)) != NULL
) {
8809 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x found on "
8810 "unmanaged list.\n", id
);
8811 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
8814 set_child_transient(win
, &trans
);
8818 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x is new.\n", id
);
8821 /* Try to get initial window geometry. */
8822 gr
= xcb_get_geometry_reply(conn
, xcb_get_geometry(conn
, id
), NULL
);
8824 DNPRINTF(SWM_D_MISC
, "manage_window: get geometry failed.\n");
8828 /* Create and initialize ws_win object. */
8829 if ((win
= calloc(1, sizeof(struct ws_win
))) == NULL
)
8830 err(1, "manage_window: calloc: failed to allocate memory for "
8835 /* Figureout which region the window belongs to. */
8836 r
= root_to_region(gr
->root
, SWM_CK_ALL
);
8838 /* Ignore window border if there is one. */
8839 WIDTH(win
) = gr
->width
;
8840 HEIGHT(win
) = gr
->height
;
8841 X(win
) = gr
->x
+ gr
->border_width
- border_width
;
8842 Y(win
) = gr
->y
+ gr
->border_width
- border_width
;
8843 win
->bordered
= true;
8844 win
->mapped
= mapped
;
8845 win
->s
= r
->s
; /* this never changes */
8849 /* Select which X events to monitor and set border pixel color. */
8850 wa
[0] = win
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
;
8851 wa
[1] = XCB_EVENT_MASK_ENTER_WINDOW
| XCB_EVENT_MASK_PROPERTY_CHANGE
|
8852 XCB_EVENT_MASK_STRUCTURE_NOTIFY
;
8854 wa
[1] |= XCB_EVENT_MASK_LEAVE_WINDOW
| XCB_EVENT_MASK_FOCUS_CHANGE
;
8857 xcb_change_window_attributes(conn
, win
->id
, XCB_CW_BORDER_PIXEL
|
8858 XCB_CW_EVENT_MASK
, wa
);
8860 /* Get WM_SIZE_HINTS. */
8861 xcb_icccm_get_wm_normal_hints_reply(conn
,
8862 xcb_icccm_get_wm_normal_hints(conn
, win
->id
),
8866 xcb_icccm_get_wm_hints_reply(conn
,
8867 xcb_icccm_get_wm_hints(conn
, win
->id
),
8870 /* Get WM_TRANSIENT_FOR; see if window is a transient. */
8871 xcb_icccm_get_wm_transient_for_reply(conn
,
8872 xcb_icccm_get_wm_transient_for(conn
, win
->id
),
8875 win
->transient
= trans
;
8876 set_child_transient(win
, &win
->transient
);
8879 /* Get WM_PROTOCOLS. */
8880 get_wm_protocols(win
);
8882 /* Set initial quirks based on EWMH. */
8883 ewmh_autoquirk(win
);
8885 /* Determine initial quirks. */
8886 xcb_icccm_get_wm_class_reply(conn
,
8887 xcb_icccm_get_wm_class(conn
, win
->id
),
8890 class = win
->ch
.class_name
? win
->ch
.class_name
: "";
8891 instance
= win
->ch
.instance_name
? win
->ch
.instance_name
: "";
8892 name
= get_win_name(win
->id
);
8894 DNPRINTF(SWM_D_CLASS
, "manage_window: class: %s, instance: %s, "
8895 "name: %s\n", class, instance
, name
);
8897 /* java is retarded so treat it special */
8898 if (strstr(instance
, "sun-awt")) {
8899 DNPRINTF(SWM_D_CLASS
, "manage_window: java window detected.\n");
8903 TAILQ_FOREACH(qp
, &quirks
, entry
) {
8904 if (regexec(&qp
->regex_class
, class, 0, NULL
, 0) == 0 &&
8905 regexec(&qp
->regex_instance
, instance
, 0, NULL
, 0) == 0 &&
8906 regexec(&qp
->regex_name
, name
, 0, NULL
, 0) == 0) {
8907 DNPRINTF(SWM_D_CLASS
, "manage_window: matched "
8908 "quirk: %s:%s:%s mask: %#x, ws: %d\n", qp
->class,
8909 qp
->instance
, qp
->name
, qp
->quirk
, qp
->ws
);
8910 win
->quirks
= qp
->quirk
;
8911 if (qp
->ws
>= 0 && qp
->ws
< workspace_limit
)
8918 /* Reset font sizes (the bruteforce way; no default keybinding). */
8919 if (win
->quirks
& SWM_Q_XTERM_FONTADJ
) {
8920 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
8921 fake_keypress(win
, XK_KP_Subtract
, XCB_MOD_MASK_SHIFT
);
8922 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
8923 fake_keypress(win
, XK_KP_Add
, XCB_MOD_MASK_SHIFT
);
8926 /* Figure out which workspace the window belongs to. */
8927 if (!(win
->quirks
& SWM_Q_IGNOREPID
) &&
8928 (p
= find_pid(window_get_pid(win
->id
))) != NULL
) {
8929 win
->ws
= &r
->s
->ws
[p
->ws
];
8930 TAILQ_REMOVE(&pidlist
, p
, entry
);
8933 } else if ((ws_idx
= get_ws_idx(win
)) != -1 &&
8935 /* _SWM_WS is set; use that. */
8936 win
->ws
= &r
->s
->ws
[ws_idx
];
8937 } else if (trans
&& (ww
= find_window(trans
)) != NULL
) {
8938 /* Launch transients in the same ws as parent. */
8945 win
->ws
= &r
->s
->ws
[force_ws
];
8947 /* Set the _NET_WM_DESKTOP atom. */
8948 DNPRINTF(SWM_D_PROP
, "manage_window: set _NET_WM_DESKTOP: %d\n",
8950 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
8951 ewmh
[_NET_WM_DESKTOP
].atom
, XCB_ATOM_CARDINAL
, 32, 1, &win
->ws
->idx
);
8953 /* Remove any _SWM_WS now that we set _NET_WM_DESKTOP. */
8954 xcb_delete_property(conn
, win
->id
, a_swm_ws
);
8956 /* WS must already be set for this to work. */
8957 store_float_geom(win
);
8959 /* Make sure window is positioned inside its region, if its active. */
8961 region_containment(win
, r
, SWM_CW_ALLSIDES
|
8962 SWM_CW_HARDBOUNDARY
);
8967 /* Figure out where to stack the window in the workspace. */
8968 if (trans
&& (ww
= find_window(trans
)))
8969 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, ww
, win
, entry
);
8970 else if (win
->ws
->focus
&& spawn_pos
== SWM_STACK_ABOVE
)
8971 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, win
->ws
->focus
, win
,
8973 else if (win
->ws
->focus
&& spawn_pos
== SWM_STACK_BELOW
)
8974 TAILQ_INSERT_BEFORE(win
->ws
->focus
, win
, entry
);
8975 else switch (spawn_pos
) {
8978 case SWM_STACK_ABOVE
:
8979 TAILQ_INSERT_TAIL(&win
->ws
->winlist
, win
, entry
);
8981 case SWM_STACK_BOTTOM
:
8982 case SWM_STACK_BELOW
:
8983 TAILQ_INSERT_HEAD(&win
->ws
->winlist
, win
, entry
);
8986 ewmh_update_client_list();
8988 TAILQ_INSERT_TAIL(&win
->ws
->stack
, win
, stack_entry
);
8990 /* Get/apply initial _NET_WM_STATE */
8991 ewmh_get_wm_state(win
);
8994 new_flags
= win
->ewmh_flags
;
8996 if (win
->quirks
& SWM_Q_FLOAT
)
8997 new_flags
|= EWMH_F_ABOVE
;
8999 if (win
->quirks
& SWM_Q_ANYWHERE
)
9000 new_flags
|= SWM_F_MANUAL
;
9002 ewmh_apply_flags(win
, new_flags
);
9003 ewmh_update_wm_state(win
);
9005 /* Set initial _NET_WM_ALLOWED_ACTIONS */
9006 ewmh_update_actions(win
);
9010 DNPRINTF(SWM_D_MISC
, "manage_window: done. win %#x, (x,y) w x h: "
9011 "(%d,%d) %d x %d, ws: %d, iconic: %s, transient: %#x\n", win
->id
,
9012 X(win
), Y(win
), WIDTH(win
), HEIGHT(win
), win
->ws
->idx
,
9013 YESNO(ICONIC(win
)), win
->transient
);
9019 free_window(struct ws_win
*win
)
9021 DNPRINTF(SWM_D_MISC
, "free_window: win %#x\n", win
->id
);
9026 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
9028 xcb_icccm_get_wm_class_reply_wipe(&win
->ch
);
9033 memset(win
, 0xff, sizeof *win
); /* XXX kill later */
9036 DNPRINTF(SWM_D_MISC
, "free_window: done.\n");
9040 unmanage_window(struct ws_win
*win
)
9042 struct ws_win
*parent
;
9047 DNPRINTF(SWM_D_MISC
, "unmanage_window: win %#x\n", win
->id
);
9050 parent
= find_window(win
->transient
);
9052 parent
->focus_child
= NULL
;
9055 TAILQ_REMOVE(&win
->ws
->stack
, win
, stack_entry
);
9056 TAILQ_REMOVE(&win
->ws
->winlist
, win
, entry
);
9057 TAILQ_INSERT_TAIL(&win
->ws
->unmanagedlist
, win
, entry
);
9059 ewmh_update_client_list();
9063 expose(xcb_expose_event_t
*e
)
9066 struct swm_region
*r
;
9068 DNPRINTF(SWM_D_EVENT
, "expose: win %#x\n", e
->window
);
9070 num_screens
= get_screen_count();
9071 for (i
= 0; i
< num_screens
; i
++)
9072 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
9073 if (e
->window
== WINID(r
->bar
))
9081 focusin(xcb_focus_in_event_t
*e
)
9083 DNPRINTF(SWM_D_EVENT
, "focusin: win %#x, mode: %s(%u), "
9084 "detail: %s(%u)\n", e
->event
, get_notify_mode_label(e
->mode
),
9085 e
->mode
, get_notify_detail_label(e
->detail
), e
->detail
);
9089 focusout(xcb_focus_out_event_t
*e
)
9091 DNPRINTF(SWM_D_EVENT
, "focusout: win %#x, mode: %s(%u), "
9092 "detail: %s(%u)\n", e
->event
, get_notify_mode_label(e
->mode
),
9093 e
->mode
, get_notify_detail_label(e
->detail
), e
->detail
);
9098 keypress(xcb_key_press_event_t
*e
)
9100 xcb_keysym_t keysym
;
9103 keysym
= xcb_key_press_lookup_keysym(syms
, e
, 0);
9105 DNPRINTF(SWM_D_EVENT
, "keypress: keysym: %u, win (x,y): %#x (%d,%d), "
9106 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
9107 "state: %u, same_screen: %s\n", keysym
, e
->event
, e
->event_x
,
9108 e
->event_y
, e
->detail
, e
->time
, e
->root
, e
->root_x
, e
->root_y
,
9109 e
->child
, e
->state
, YESNO(e
->same_screen
));
9111 if ((kp
= key_lookup(CLEANMASK(e
->state
), keysym
)) == NULL
)
9114 last_event_time
= e
->time
;
9116 if (kp
->funcid
== KF_SPAWN_CUSTOM
)
9117 spawn_custom(root_to_region(e
->root
, SWM_CK_ALL
),
9118 &(keyfuncs
[kp
->funcid
].args
), kp
->spawn_name
);
9119 else if (keyfuncs
[kp
->funcid
].func
)
9120 keyfuncs
[kp
->funcid
].func(root_to_region(e
->root
, SWM_CK_ALL
),
9121 &(keyfuncs
[kp
->funcid
].args
));
9124 /* Unfreeze grab events. */
9125 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
, e
->time
);
9128 DNPRINTF(SWM_D_EVENT
, "keypress: done.\n");
9132 buttonpress(xcb_button_press_event_t
*e
)
9134 struct ws_win
*win
= NULL
;
9135 struct swm_region
*r
, *old_r
;
9137 bool handled
= false;
9139 DNPRINTF(SWM_D_EVENT
, "buttonpress: win (x,y): %#x (%d,%d), "
9140 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
9141 "state: %u, same_screen: %s\n", e
->event
, e
->event_x
, e
->event_y
,
9142 e
->detail
, e
->time
, e
->root
, e
->root_x
, e
->root_y
, e
->child
,
9143 e
->state
, YESNO(e
->same_screen
));
9145 if (e
->event
== e
->root
) {
9146 if (e
->child
!= 0) {
9147 win
= find_window(e
->child
);
9148 /* Pass ButtonPress to window if it isn't managed. */
9152 /* Focus on empty region */
9153 /* If no windows on region if its empty. */
9154 r
= root_to_region(e
->root
, SWM_CK_POINTER
);
9156 DNPRINTF(SWM_D_EVENT
, "buttonpress: "
9157 "NULL region; ignoring.\n");
9161 if (TAILQ_EMPTY(&r
->ws
->winlist
)) {
9162 old_r
= root_to_region(e
->root
, SWM_CK_FOCUS
);
9163 if (old_r
&& old_r
!= r
)
9164 unfocus_win(old_r
->ws
->focus
);
9166 xcb_set_input_focus(conn
,
9167 XCB_INPUT_FOCUS_PARENT
, e
->root
, e
->time
);
9169 /* Clear bar since empty. */
9177 win
= find_window(e
->event
);
9183 last_event_time
= e
->time
;
9185 focus_win(get_focus_magic(win
));
9187 for (i
= 0; i
< LENGTH(buttons
); i
++)
9188 if (client_click
== buttons
[i
].action
&& buttons
[i
].func
&&
9189 buttons
[i
].button
== e
->detail
&&
9190 CLEANMASK(buttons
[i
].mask
) == CLEANMASK(e
->state
)) {
9191 buttons
[i
].func(win
, &buttons
[i
].args
);
9197 DNPRINTF(SWM_D_EVENT
, "buttonpress: passing to window.\n");
9198 /* Replay event to event window */
9199 xcb_allow_events(conn
, XCB_ALLOW_REPLAY_POINTER
, e
->time
);
9201 DNPRINTF(SWM_D_EVENT
, "buttonpress: handled.\n");
9202 /* Unfreeze grab events. */
9203 xcb_allow_events(conn
, XCB_ALLOW_SYNC_POINTER
, e
->time
);
9211 print_win_geom(xcb_window_t w
)
9213 xcb_get_geometry_reply_t
*wa
;
9215 wa
= xcb_get_geometry_reply(conn
, xcb_get_geometry(conn
, w
), NULL
);
9217 DNPRINTF(SWM_D_MISC
, "print_win_geom: window not found: %#x\n",
9222 DNPRINTF(SWM_D_MISC
, "print_win_geom: win %#x, root: %#x, "
9223 "depth: %u, (x,y) w x h: (%d,%d) %d x %d, border: %d\n",
9224 w
, wa
->root
, wa
->depth
, wa
->x
, wa
->y
, wa
->width
, wa
->height
,
9233 get_stack_mode_name(uint8_t mode
)
9238 case XCB_STACK_MODE_ABOVE
:
9241 case XCB_STACK_MODE_BELOW
:
9244 case XCB_STACK_MODE_TOP_IF
:
9247 case XCB_STACK_MODE_BOTTOM_IF
:
9250 case XCB_STACK_MODE_OPPOSITE
:
9262 configurerequest(xcb_configure_request_event_t
*e
)
9265 struct swm_region
*r
= NULL
;
9267 uint32_t wc
[7] = {0};
9271 if ((win
= find_window(e
->window
)) == NULL
)
9272 if ((win
= find_unmanaged_window(e
->window
)) == NULL
)
9276 if (swm_debug
& SWM_D_EVENT
) {
9277 print_win_geom(e
->window
);
9279 DNPRINTF(SWM_D_EVENT
, "configurerequest: win %#x, "
9280 "parent: %#x, new: %s, value_mask: %u { ", e
->window
,
9281 e
->parent
, YESNO(new), e
->value_mask
);
9282 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
)
9283 DPRINTF("X: %d ", e
->x
);
9284 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
)
9285 DPRINTF("Y: %d ", e
->y
);
9286 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
)
9287 DPRINTF("W: %u ", e
->width
);
9288 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
)
9289 DPRINTF("H: %u ", e
->height
);
9290 if (e
->value_mask
& XCB_CONFIG_WINDOW_BORDER_WIDTH
)
9291 DPRINTF("Border: %u ", e
->border_width
);
9292 if (e
->value_mask
& XCB_CONFIG_WINDOW_SIBLING
)
9293 DPRINTF("Sibling: %#x ", e
->sibling
);
9294 if (e
->value_mask
& XCB_CONFIG_WINDOW_STACK_MODE
)
9295 DPRINTF("StackMode: %s(%u) ",
9296 get_stack_mode_name(e
->stack_mode
), e
->stack_mode
);
9302 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
) {
9303 mask
|= XCB_CONFIG_WINDOW_X
;
9306 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
) {
9307 mask
|= XCB_CONFIG_WINDOW_Y
;
9310 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
) {
9311 mask
|= XCB_CONFIG_WINDOW_WIDTH
;
9314 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
) {
9315 mask
|= XCB_CONFIG_WINDOW_HEIGHT
;
9316 wc
[i
++] = e
->height
;
9318 if (e
->value_mask
& XCB_CONFIG_WINDOW_BORDER_WIDTH
) {
9319 mask
|= XCB_CONFIG_WINDOW_BORDER_WIDTH
;
9320 wc
[i
++] = e
->border_width
;
9322 if (e
->value_mask
& XCB_CONFIG_WINDOW_SIBLING
) {
9323 mask
|= XCB_CONFIG_WINDOW_SIBLING
;
9324 wc
[i
++] = e
->sibling
;
9326 if (e
->value_mask
& XCB_CONFIG_WINDOW_STACK_MODE
) {
9327 mask
|= XCB_CONFIG_WINDOW_STACK_MODE
;
9328 wc
[i
++] = e
->stack_mode
;
9332 xcb_configure_window(conn
, e
->window
, mask
, wc
);
9335 } else if ((!MANUAL(win
) || win
->quirks
& SWM_Q_ANYWHERE
) &&
9336 !FULLSCREEN(win
) && !MAXIMIZED(win
)) {
9339 else if (win
->ws
->old_r
)
9342 /* windows are centered unless ANYWHERE quirk is set. */
9343 if (win
->quirks
& SWM_Q_ANYWHERE
) {
9344 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
) {
9345 win
->g_float
.x
= e
->x
;
9347 win
->g_float
.x
-= X(r
);
9350 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
) {
9351 win
->g_float
.y
= e
->y
;
9353 win
->g_float
.y
-= Y(r
);
9357 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
)
9358 win
->g_float
.w
= e
->width
;
9360 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
)
9361 win
->g_float
.h
= e
->height
;
9363 win
->g_floatvalid
= true;
9365 if (!MAXIMIZED(win
) && !FULLSCREEN(win
) &&
9366 (TRANS(win
) || (ABOVE(win
) &&
9367 win
->ws
->cur_layout
!= &layouts
[SWM_MAX_STACK
]))) {
9368 WIDTH(win
) = win
->g_float
.w
;
9369 HEIGHT(win
) = win
->g_float
.h
;
9372 update_floater(win
);
9387 DNPRINTF(SWM_D_EVENT
, "configurerequest: done.\n");
9391 configurenotify(xcb_configure_notify_event_t
*e
)
9395 DNPRINTF(SWM_D_EVENT
, "configurenotify: win %#x, event win: %#x, "
9396 "(x,y) WxH: (%d,%d) %ux%u, border: %u, above_sibling: %#x, "
9397 "override_redirect: %s\n", e
->window
, e
->event
, e
->x
, e
->y
,
9398 e
->width
, e
->height
, e
->border_width
, e
->above_sibling
,
9399 YESNO(e
->override_redirect
));
9401 win
= find_window(e
->window
);
9403 xcb_icccm_get_wm_normal_hints_reply(conn
,
9404 xcb_icccm_get_wm_normal_hints(conn
, win
->id
),
9407 if (font_adjusted
) {
9415 destroynotify(xcb_destroy_notify_event_t
*e
)
9419 DNPRINTF(SWM_D_EVENT
, "destroynotify: win %#x\n", e
->window
);
9421 if ((win
= find_window(e
->window
)) == NULL
) {
9422 if ((win
= find_unmanaged_window(e
->window
)) == NULL
)
9428 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
9429 /* If we were focused, make sure we focus on something else. */
9430 if (win
== win
->ws
->focus
)
9431 win
->ws
->focus_pending
= get_focus_prev(win
);
9434 unmanage_window(win
);
9437 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& WS_FOCUSED(win
->ws
)) {
9438 if (win
->ws
->focus_pending
) {
9439 focus_win(win
->ws
->focus_pending
);
9440 win
->ws
->focus_pending
= NULL
;
9441 } else if (win
== win
->ws
->focus
) {
9442 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
,
9443 win
->ws
->r
->id
, XCB_CURRENT_TIME
);
9454 get_notify_detail_label(uint8_t detail
)
9459 case XCB_NOTIFY_DETAIL_ANCESTOR
:
9462 case XCB_NOTIFY_DETAIL_VIRTUAL
:
9465 case XCB_NOTIFY_DETAIL_INFERIOR
:
9468 case XCB_NOTIFY_DETAIL_NONLINEAR
:
9469 label
= "Nonlinear";
9471 case XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL
:
9472 label
= "NonlinearVirtual";
9474 case XCB_NOTIFY_DETAIL_POINTER
:
9477 case XCB_NOTIFY_DETAIL_POINTER_ROOT
:
9478 label
= "PointerRoot";
9480 case XCB_NOTIFY_DETAIL_NONE
:
9491 get_notify_mode_label(uint8_t mode
)
9496 case XCB_NOTIFY_MODE_NORMAL
:
9499 case XCB_NOTIFY_MODE_GRAB
:
9502 case XCB_NOTIFY_MODE_UNGRAB
:
9505 case XCB_NOTIFY_MODE_WHILE_GRABBED
:
9506 label
= "WhileGrabbed";
9517 enternotify(xcb_enter_notify_event_t
*e
)
9520 struct swm_region
*r
;
9522 DNPRINTF(SWM_D_FOCUS
, "enternotify: time: %u, win (x,y): %#x "
9523 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
9524 "child: %#x, same_screen_focus: %s, state: %d\n",
9525 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
9526 get_notify_mode_label(e
->mode
), e
->mode
,
9527 get_notify_detail_label(e
->detail
), e
->detail
,
9528 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
9529 YESNO(e
->same_screen_focus
), e
->state
);
9531 if (focus_mode
== SWM_FOCUS_MANUAL
&&
9532 e
->mode
== XCB_NOTIFY_MODE_NORMAL
) {
9533 DNPRINTF(SWM_D_EVENT
, "enternotify: manual focus; ignoring.\n");
9537 last_event_time
= e
->time
;
9539 if ((win
= find_window(e
->event
)) == NULL
) {
9540 if (e
->event
== e
->root
) {
9541 /* If no windows on pointer region, then focus root. */
9542 r
= root_to_region(e
->root
, SWM_CK_POINTER
);
9544 DNPRINTF(SWM_D_EVENT
, "enternotify: "
9545 "NULL region; ignoring.\n");
9551 DNPRINTF(SWM_D_EVENT
, "enternotify: window is NULL; "
9556 if (e
->mode
== XCB_NOTIFY_MODE_NORMAL
&&
9557 e
->detail
== XCB_NOTIFY_DETAIL_INFERIOR
) {
9558 DNPRINTF(SWM_D_EVENT
, "enternotify: entering from "
9559 "inferior; ignoring\n");
9563 focus_win(get_focus_magic(win
));
9571 leavenotify(xcb_leave_notify_event_t
*e
)
9573 DNPRINTF(SWM_D_FOCUS
, "leavenotify: time: %u, win (x,y): %#x "
9574 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
9575 "child: %#x, same_screen_focus: %s, state: %d\n",
9576 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
9577 get_notify_mode_label(e
->mode
), e
->mode
,
9578 get_notify_detail_label(e
->detail
), e
->detail
,
9579 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
9580 YESNO(e
->same_screen_focus
), e
->state
);
9585 mapnotify(xcb_map_notify_event_t
*e
)
9587 struct ws_win
*win
, *parent
= NULL
;
9588 struct workspace
*ws
;
9590 DNPRINTF(SWM_D_EVENT
, "mapnotify: win %#x\n", e
->window
);
9592 if ((win
= manage_window(e
->window
, spawn_position
, true)) == NULL
)
9596 /* Need to know if win was mapped due to ws switch. */
9597 if (ws
->state
== SWM_WS_STATE_MAPPED
) {
9598 if (ws
->focus_pending
&& TRANS(ws
->focus_pending
))
9599 parent
= find_window(win
->transient
);
9601 /* If window's parent is maximized, don't clear it. */
9602 if ((parent
== NULL
) || !MAXIMIZED(parent
))
9603 if (clear_maximized(ws
) > 0)
9608 set_win_state(win
, XCB_ICCCM_WM_STATE_NORMAL
);
9610 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& WS_FOCUSED(win
->ws
)) {
9611 if (ws
->focus_pending
== win
) {
9613 ws
->focus_pending
= NULL
;
9614 center_pointer(win
->ws
->r
);
9623 mappingnotify(xcb_mapping_notify_event_t
*e
)
9626 int i
, j
, num_screens
;
9628 xcb_refresh_keyboard_mapping(syms
, e
);
9630 if (e
->request
== XCB_MAPPING_KEYBOARD
) {
9633 /* Regrab buttons on all managed windows. */
9634 num_screens
= get_screen_count();
9635 for (i
= 0; i
< num_screens
; i
++)
9636 for (j
= 0; j
< workspace_limit
; j
++)
9637 TAILQ_FOREACH(w
, &screens
[i
].ws
[j
].winlist
,
9644 maprequest(xcb_map_request_event_t
*e
)
9646 struct ws_win
*win
, *w
= NULL
;
9647 xcb_get_window_attributes_reply_t
*war
;
9649 DNPRINTF(SWM_D_EVENT
, "maprequest: win %#x\n",
9652 war
= xcb_get_window_attributes_reply(conn
,
9653 xcb_get_window_attributes(conn
, e
->window
),
9656 DNPRINTF(SWM_D_EVENT
, "maprequest: window lost.\n");
9660 if (war
->override_redirect
) {
9661 DNPRINTF(SWM_D_EVENT
, "maprequest: override_redirect; "
9666 win
= manage_window(e
->window
, spawn_position
,
9667 (war
->map_state
== XCB_MAP_STATE_VIEWABLE
));
9671 /* The new window should get focus; prepare. */
9672 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
9673 !(win
->quirks
& SWM_Q_NOFOCUSONMAP
) &&
9674 (!(win
->hints
.flags
& XCB_ICCCM_WM_HINT_INPUT
) ||
9675 (win
->hints
.flags
& XCB_ICCCM_WM_HINT_INPUT
&&
9676 win
->hints
.input
))) {
9677 if (win
->quirks
& SWM_Q_FOCUSONMAP_SINGLE
) {
9678 /* See if other wins of same type are already mapped. */
9679 TAILQ_FOREACH(w
, &win
->ws
->winlist
, entry
) {
9680 if (w
== win
|| !w
->mapped
)
9683 if (w
->ch
.class_name
&&
9684 win
->ch
.class_name
&&
9685 strcmp(w
->ch
.class_name
,
9686 win
->ch
.class_name
) == 0 &&
9687 w
->ch
.instance_name
&&
9688 win
->ch
.instance_name
&&
9689 strcmp(w
->ch
.instance_name
,
9690 win
->ch
.instance_name
) == 0)
9696 win
->ws
->focus_pending
= get_focus_magic(win
);
9699 /* All windows need to be mapped if they are in the current workspace.*/
9703 /* Ignore EnterNotify to handle the mapnotify without interference. */
9704 if (focus_mode
== SWM_FOCUS_DEFAULT
)
9705 event_drain(XCB_ENTER_NOTIFY
);
9708 DNPRINTF(SWM_D_EVENT
, "maprequest: done.\n");
9712 motionnotify(xcb_motion_notify_event_t
*e
)
9714 struct swm_region
*r
= NULL
;
9717 DNPRINTF(SWM_D_FOCUS
, "motionnotify: time: %u, win (x,y): %#x "
9718 "(%d,%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
9719 "child: %#x, same_screen_focus: %s, state: %d\n",
9720 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
9721 get_notify_detail_label(e
->detail
), e
->detail
,
9722 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
9723 YESNO(e
->same_screen
), e
->state
);
9725 last_event_time
= e
->time
;
9727 if (focus_mode
== SWM_FOCUS_MANUAL
)
9730 num_screens
= get_screen_count();
9731 for (i
= 0; i
< num_screens
; i
++)
9732 if (screens
[i
].root
== e
->root
)
9735 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
9736 if (X(r
) <= e
->root_x
&& e
->root_x
< MAX_X(r
) &&
9737 Y(r
) <= e
->root_y
&& e
->root_y
< MAX_Y(r
))
9745 get_atom_name(xcb_atom_t atom
)
9748 #ifdef SWM_DEBUG_ATOM_NAMES
9750 * This should be disabled during most debugging since
9751 * xcb_get_* causes an xcb_flush.
9754 xcb_get_atom_name_reply_t
*r
;
9756 r
= xcb_get_atom_name_reply(conn
,
9757 xcb_get_atom_name(conn
, atom
),
9760 len
= xcb_get_atom_name_name_length(r
);
9762 name
= malloc(len
+ 1);
9764 memcpy(name
, xcb_get_atom_name_name(r
), len
);
9778 propertynotify(xcb_property_notify_event_t
*e
)
9781 struct workspace
*ws
;
9785 name
= get_atom_name(e
->atom
);
9786 DNPRINTF(SWM_D_EVENT
, "propertynotify: win %#x, atom: %s(%u), "
9787 "time: %#x, state: %u\n", e
->window
, name
, e
->atom
, e
->time
,
9791 win
= find_window(e
->window
);
9797 last_event_time
= e
->time
;
9799 if (e
->atom
== a_state
) {
9800 /* State just changed, make sure it gets focused if mapped. */
9801 if (e
->state
== XCB_PROPERTY_NEW_VALUE
) {
9802 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& WS_FOCUSED(ws
)) {
9804 ws
->focus_pending
== win
) {
9805 focus_win(ws
->focus_pending
);
9806 ws
->focus_pending
= NULL
;
9810 } else if (e
->atom
== XCB_ATOM_WM_CLASS
||
9811 e
->atom
== XCB_ATOM_WM_NAME
) {
9813 } else if (e
->atom
== a_prot
) {
9814 get_wm_protocols(win
);
9821 unmapnotify(xcb_unmap_notify_event_t
*e
)
9824 struct workspace
*ws
;
9826 DNPRINTF(SWM_D_EVENT
, "unmapnotify: win %#x\n", e
->window
);
9828 /* If we aren't managing the window, then ignore. */
9829 win
= find_window(e
->window
);
9830 if (win
== NULL
|| win
->id
!= e
->window
)
9833 /* Do nothing if already withdrawn. */
9834 if (!win
->mapped
&& !ICONIC(win
))
9838 win
->mapped
= false;
9840 /* If win was focused, make sure to focus on something else. */
9841 if (win
== ws
->focus
) {
9842 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
9843 ws
->focus_pending
= get_focus_prev(win
);
9844 DNPRINTF(SWM_D_EVENT
, "unmapnotify: "
9845 "focus_pending: %#x\n",
9846 WINID(ws
->focus_pending
));
9854 set_win_state(win
, XCB_ICCCM_WM_STATE_ICONIC
);
9857 set_win_state(win
, XCB_ICCCM_WM_STATE_WITHDRAWN
);
9858 unmanage_window(win
);
9864 /* Update focus if ws is active. */
9865 if (WS_FOCUSED(ws
)) {
9866 if (focus_mode
== SWM_FOCUS_FOLLOW
) {
9867 focus_win(get_pointer_win(ws
->r
->s
->root
));
9868 } else if (ws
->focus_pending
) {
9869 focus_win(ws
->focus_pending
);
9870 ws
->focus_pending
= NULL
;
9871 } else if (ws
->focus
== NULL
) {
9872 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
,
9873 ws
->r
->id
, XCB_CURRENT_TIME
);
9877 center_pointer(ws
->r
);
9883 get_source_type_label(uint32_t type
)
9888 case EWMH_SOURCE_TYPE_NONE
:
9891 case EWMH_SOURCE_TYPE_NORMAL
:
9894 case EWMH_SOURCE_TYPE_OTHER
:
9906 clientmessage(xcb_client_message_event_t
*e
)
9909 struct swm_region
*r
= NULL
;
9913 xcb_map_request_event_t mre
;
9917 name
= get_atom_name(e
->type
);
9918 DNPRINTF(SWM_D_EVENT
, "clientmessage: win %#x, atom: %s(%u)\n",
9919 e
->window
, name
, e
->type
);
9923 if (e
->type
== ewmh
[_NET_CURRENT_DESKTOP
].atom
) {
9924 num_screens
= get_screen_count();
9925 for (i
= 0; i
< num_screens
; i
++)
9926 if (screens
[i
].root
== e
->window
) {
9927 r
= screens
[i
].r_focus
;
9931 if (r
&& e
->data
.data32
[0] < (uint32_t)workspace_limit
) {
9932 a
.id
= e
->data
.data32
[0];
9940 win
= find_window(e
->window
);
9942 if (e
->type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
9943 /* Manage the window with maprequest. */
9944 DNPRINTF(SWM_D_EVENT
, "clientmessage: request focus on "
9945 "unmanaged window.\n");
9946 mre
.window
= e
->window
;
9952 if (e
->type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
9953 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_ACTIVE_WINDOW, "
9954 "source_type: %s(%d)\n",
9955 get_source_type_label(e
->data
.data32
[0]),
9959 * Allow focus changes that are a result of direct user
9960 * action and from applications that use the old EWMH spec.
9962 if (e
->data
.data32
[0] != EWMH_SOURCE_TYPE_NORMAL
||
9963 win
->quirks
& SWM_Q_OBEYAPPFOCUSREQ
) {
9964 if (WS_FOCUSED(win
->ws
))
9967 win
->ws
->focus_pending
= win
;
9969 } else if (e
->type
== ewmh
[_NET_CLOSE_WINDOW
].atom
) {
9970 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_CLOSE_WINDOW\n");
9971 if (win
->can_delete
)
9972 client_msg(win
, a_delete
, 0);
9974 xcb_kill_client(conn
, win
->id
);
9975 } else if (e
->type
== ewmh
[_NET_MOVERESIZE_WINDOW
].atom
) {
9976 DNPRINTF(SWM_D_EVENT
,
9977 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
9979 if (e
->data
.data32
[0] & (1<<8)) /* x */
9980 X(win
) = e
->data
.data32
[1];
9981 if (e
->data
.data32
[0] & (1<<9)) /* y */
9982 Y(win
) = e
->data
.data32
[2];
9983 if (e
->data
.data32
[0] & (1<<10)) /* width */
9984 WIDTH(win
) = e
->data
.data32
[3];
9985 if (e
->data
.data32
[0] & (1<<11)) /* height */
9986 HEIGHT(win
) = e
->data
.data32
[4];
9990 /* Notify no change was made. */
9991 config_win(win
, NULL
);
9992 /* TODO: Change stack sizes */
9994 } else if (e
->type
== ewmh
[_NET_RESTACK_WINDOW
].atom
) {
9995 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_RESTACK_WINDOW\n");
9996 val
[0] = e
->data
.data32
[1]; /* Sibling window. */
9997 val
[1] = e
->data
.data32
[2]; /* Stack mode detail. */
9999 xcb_configure_window(conn
, win
->id
, XCB_CONFIG_WINDOW_SIBLING
|
10000 XCB_CONFIG_WINDOW_STACK_MODE
, val
);
10001 } else if (e
->type
== ewmh
[_NET_WM_STATE
].atom
) {
10002 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_WM_STATE\n");
10003 ewmh_change_wm_state(win
, e
->data
.data32
[1], e
->data
.data32
[0]);
10004 if (e
->data
.data32
[2])
10005 ewmh_change_wm_state(win
, e
->data
.data32
[2],
10006 e
->data
.data32
[0]);
10008 ewmh_update_wm_state(win
);
10010 } else if (e
->type
== ewmh
[_NET_WM_DESKTOP
].atom
) {
10011 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_WM_DESKTOP\n");
10014 win_to_ws(win
, e
->data
.data32
[0], true);
10016 /* Restack if either the source or destination ws is mapped. */
10017 if (r
!= NULL
|| win
->ws
->r
!= NULL
) {
10019 load_float_geom(win
);
10031 int errcode
= xcb_connection_has_error(conn
);
10032 #ifdef XCB_CONN_ERROR
10035 case XCB_CONN_ERROR
:
10036 s
= "Socket error, pipe error or other stream error.";
10038 case XCB_CONN_CLOSED_EXT_NOTSUPPORTED
:
10039 s
= "Extension not supported.";
10041 case XCB_CONN_CLOSED_MEM_INSUFFICIENT
:
10042 s
= "Insufficient memory.";
10044 case XCB_CONN_CLOSED_REQ_LEN_EXCEED
:
10045 s
= "Request length was exceeded.";
10047 case XCB_CONN_CLOSED_PARSE_ERR
:
10048 s
= "Error parsing display string.";
10051 s
= "Unknown error.";
10054 errx(errcode
, "X CONNECTION ERROR: %s", s
);
10057 errx(errcode
, "X CONNECTION ERROR");
10064 int num_screens
, i
;
10065 const uint32_t val
= XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT
|
10066 XCB_EVENT_MASK_ENTER_WINDOW
;
10068 xcb_void_cookie_t wac
;
10069 xcb_generic_error_t
*error
;
10071 /* this causes an error if some other window manager is running */
10072 num_screens
= get_screen_count();
10073 for (i
= 0; i
< num_screens
; i
++) {
10074 if ((sc
= get_screen(i
)) == NULL
)
10075 errx(1, "ERROR: can't get screen %d.", i
);
10076 DNPRINTF(SWM_D_INIT
, "enable_wm: screen %d, root: %#x\n",
10078 wac
= xcb_change_window_attributes_checked(conn
, sc
->root
,
10079 XCB_CW_EVENT_MASK
, &val
);
10080 if ((error
= xcb_request_check(conn
, wac
))) {
10081 DNPRINTF(SWM_D_INIT
, "enable_wm: error_code: %u\n",
10082 error
->error_code
);
10087 /* click to focus on empty region */
10088 xcb_grab_button(conn
, 1, sc
->root
, BUTTONMASK
,
10089 XCB_GRAB_MODE_SYNC
, XCB_GRAB_MODE_ASYNC
, XCB_WINDOW_NONE
,
10090 XCB_CURSOR_NONE
, XCB_BUTTON_INDEX_1
, XCB_BUTTON_MASK_ANY
);
10097 new_region(struct swm_screen
*s
, int x
, int y
, int w
, int h
)
10099 struct swm_region
*r
= NULL
, *n
;
10100 struct workspace
*ws
= NULL
;
10104 DNPRINTF(SWM_D_MISC
, "new region: screen[%d]:%dx%d+%d+%d\n",
10105 s
->idx
, w
, h
, x
, y
);
10107 /* remove any conflicting regions */
10108 n
= TAILQ_FIRST(&s
->rl
);
10111 n
= TAILQ_NEXT(r
, entry
);
10112 if (X(r
) < (x
+ w
) && (X(r
) + WIDTH(r
)) > x
&&
10113 Y(r
) < (y
+ h
) && (Y(r
) + HEIGHT(r
)) > y
) {
10114 if (r
->ws
->r
!= NULL
)
10115 r
->ws
->old_r
= r
->ws
->r
;
10118 xcb_destroy_window(conn
, r
->id
);
10119 TAILQ_REMOVE(&s
->rl
, r
, entry
);
10120 TAILQ_INSERT_TAIL(&s
->orl
, r
, entry
);
10124 /* search old regions for one to reuse */
10126 /* size + location match */
10127 TAILQ_FOREACH(r
, &s
->orl
, entry
)
10128 if (X(r
) == x
&& Y(r
) == y
&&
10129 HEIGHT(r
) == h
&& WIDTH(r
) == w
)
10133 TAILQ_FOREACH(r
, &s
->orl
, entry
)
10134 if (HEIGHT(r
) == h
&& WIDTH(r
) == w
)
10138 TAILQ_REMOVE(&s
->orl
, r
, entry
);
10139 /* try to use old region's workspace */
10140 if (r
->ws
->r
== NULL
)
10143 if ((r
= calloc(1, sizeof(struct swm_region
))) == NULL
)
10144 err(1, "new_region: calloc: failed to allocate memory "
10147 /* if we don't have a workspace already, find one */
10149 for (i
= 0; i
< workspace_limit
; i
++)
10150 if (s
->ws
[i
].r
== NULL
) {
10157 errx(1, "new_region: no free workspaces");
10159 if (ws
->state
== SWM_WS_STATE_HIDDEN
)
10160 ws
->state
= SWM_WS_STATE_MAPPING
;
10168 r
->ws_prior
= NULL
;
10171 TAILQ_INSERT_TAIL(&s
->rl
, r
, entry
);
10173 /* Invisible region window to detect pointer events on empty regions. */
10174 r
->id
= xcb_generate_id(conn
);
10175 wa
[0] = XCB_EVENT_MASK_POINTER_MOTION
|
10176 XCB_EVENT_MASK_POINTER_MOTION_HINT
;
10178 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, r
->id
, r
->s
->root
,
10179 X(r
), Y(r
), WIDTH(r
), HEIGHT(r
), 0, XCB_WINDOW_CLASS_INPUT_ONLY
,
10180 XCB_COPY_FROM_PARENT
, XCB_CW_EVENT_MASK
, wa
);
10182 /* Make sure region input is at the bottom. */
10183 wa
[0] = XCB_STACK_MODE_BELOW
;
10184 xcb_configure_window(conn
, r
->id
, XCB_CONFIG_WINDOW_STACK_MODE
, wa
);
10186 xcb_map_window(conn
, r
->id
);
10190 scan_randr(int idx
)
10192 #ifdef SWM_XRR_HAS_CRTC
10195 #endif /* SWM_XRR_HAS_CRTC */
10196 struct swm_region
*r
;
10197 struct ws_win
*win
;
10199 xcb_randr_get_screen_resources_current_cookie_t src
;
10200 xcb_randr_get_screen_resources_current_reply_t
*srr
;
10201 xcb_randr_get_crtc_info_cookie_t cic
;
10202 xcb_randr_get_crtc_info_reply_t
*cir
= NULL
;
10203 xcb_randr_crtc_t
*crtc
;
10204 xcb_screen_t
*screen
;
10206 DNPRINTF(SWM_D_MISC
, "scan_randr: screen: %d\n", idx
);
10208 if ((screen
= get_screen(idx
)) == NULL
)
10209 errx(1, "ERROR: can't get screen %d.", idx
);
10211 num_screens
= get_screen_count();
10212 if (idx
>= num_screens
)
10213 errx(1, "scan_randr: invalid screen");
10215 /* remove any old regions */
10216 while ((r
= TAILQ_FIRST(&screens
[idx
].rl
)) != NULL
) {
10217 r
->ws
->old_r
= r
->ws
->r
= NULL
;
10219 xcb_destroy_window(conn
, r
->id
);
10220 TAILQ_REMOVE(&screens
[idx
].rl
, r
, entry
);
10221 TAILQ_INSERT_TAIL(&screens
[idx
].orl
, r
, entry
);
10225 /* map virtual screens onto physical screens */
10226 #ifdef SWM_XRR_HAS_CRTC
10227 if (randr_support
) {
10228 src
= xcb_randr_get_screen_resources_current(conn
,
10229 screens
[idx
].root
);
10230 srr
= xcb_randr_get_screen_resources_current_reply(conn
, src
,
10233 new_region(&screens
[idx
], 0, 0,
10234 screen
->width_in_pixels
,
10235 screen
->height_in_pixels
);
10238 ncrtc
= srr
->num_crtcs
;
10240 crtc
= xcb_randr_get_screen_resources_current_crtcs(srr
);
10241 for (c
= 0; c
< ncrtc
; c
++) {
10242 cic
= xcb_randr_get_crtc_info(conn
, crtc
[c
],
10244 cir
= xcb_randr_get_crtc_info_reply(conn
, cic
, NULL
);
10247 if (cir
->num_outputs
== 0) {
10252 if (cir
->mode
== 0)
10253 new_region(&screens
[idx
], 0, 0,
10254 screen
->width_in_pixels
,
10255 screen
->height_in_pixels
);
10257 new_region(&screens
[idx
],
10258 cir
->x
, cir
->y
, cir
->width
, cir
->height
);
10263 #endif /* SWM_XRR_HAS_CRTC */
10265 /* If detection failed, create a single region that spans the screen. */
10266 if (TAILQ_EMPTY(&screens
[idx
].rl
))
10267 new_region(&screens
[idx
], 0, 0, screen
->width_in_pixels
,
10268 screen
->height_in_pixels
);
10271 /* Cleanup unused previously visible workspaces. */
10272 TAILQ_FOREACH(r
, &screens
[idx
].orl
, entry
) {
10273 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
)
10275 r
->ws
->state
= SWM_WS_STATE_HIDDEN
;
10277 /* The screen shouldn't focus on an unused region. */
10278 if (screens
[idx
].r_focus
== r
)
10279 screens
[idx
].r_focus
= NULL
;
10282 DNPRINTF(SWM_D_MISC
, "scan_randr: done.\n");
10286 screenchange(xcb_randr_screen_change_notify_event_t
*e
)
10288 struct swm_region
*r
;
10289 int i
, num_screens
;
10291 DNPRINTF(SWM_D_EVENT
, "screenchange: root: %#x\n", e
->root
);
10293 num_screens
= get_screen_count();
10294 /* silly event doesn't include the screen index */
10295 for (i
= 0; i
< num_screens
; i
++)
10296 if (screens
[i
].root
== e
->root
)
10298 if (i
>= num_screens
)
10299 errx(1, "screenchange: screen not found");
10301 /* brute force for now, just re-enumerate the regions */
10305 print_win_geom(e
->root
);
10307 /* add bars to all regions */
10308 for (i
= 0; i
< num_screens
; i
++) {
10309 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
10315 /* Make sure a region has focus on each screen. */
10316 for (i
= 0; i
< num_screens
; i
++) {
10317 if (screens
[i
].r_focus
== NULL
) {
10318 r
= TAILQ_FIRST(&screens
[i
].rl
);
10327 /* Update workspace state on all regions. */
10328 for (i
= 0; i
< num_screens
; i
++)
10329 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
10330 r
->ws
->state
= SWM_WS_STATE_MAPPED
;
10336 struct swm_region
*r
= NULL
;
10337 xcb_window_t
*wins
= NULL
, trans
, *cwins
= NULL
;
10338 int i
, j
, k
, n
, no
, num_screens
;
10340 bool manage
, mapped
;
10342 xcb_query_tree_cookie_t qtc
;
10343 xcb_query_tree_reply_t
*qtr
;
10344 xcb_get_window_attributes_cookie_t gac
;
10345 xcb_get_window_attributes_reply_t
*gar
;
10346 xcb_get_property_cookie_t pc
;
10347 xcb_get_property_reply_t
*pr
;
10349 DNPRINTF(SWM_D_INIT
, "grab_windows: begin\n");
10350 num_screens
= get_screen_count();
10351 for (i
= 0; i
< num_screens
; i
++) {
10352 qtc
= xcb_query_tree(conn
, screens
[i
].root
);
10353 qtr
= xcb_query_tree_reply(conn
, qtc
, NULL
);
10356 wins
= xcb_query_tree_children(qtr
);
10357 no
= xcb_query_tree_children_length(qtr
);
10359 /* Try to sort windows according to _NET_CLIENT_LIST. */
10360 pr
= xcb_get_property_reply(conn
, xcb_get_property(conn
, 0,
10361 screens
[i
].root
, ewmh
[_NET_CLIENT_LIST
].atom
,
10362 XCB_ATOM_WINDOW
, 0, UINT32_MAX
), NULL
);
10364 cwins
= xcb_get_property_value(pr
);
10365 n
= xcb_get_property_value_length(pr
) /
10366 sizeof(xcb_atom_t
);
10368 for (j
= 0; j
< n
; ++j
) {
10369 for (k
= j
; k
< no
; ++k
) {
10370 if (wins
[k
] == cwins
[j
]) {
10371 /* Swap wins j and k. */
10373 wins
[j
] = cwins
[j
];
10381 /* attach windows to a region */
10382 /* normal windows */
10383 DNPRINTF(SWM_D_INIT
, "grab_windows: grab top level windows.\n");
10384 for (j
= 0; j
< no
; j
++) {
10385 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
10386 if (r
->id
== wins
[j
]) {
10387 DNPRINTF(SWM_D_INIT
, "grab_windows: "
10388 "skip %#x; region input window.\n",
10391 } else if (r
->bar
->id
== wins
[j
]) {
10392 DNPRINTF(SWM_D_INIT
, "grab_windows: "
10393 "skip %#x; region bar.\n",
10402 gac
= xcb_get_window_attributes(conn
, wins
[j
]);
10403 gar
= xcb_get_window_attributes_reply(conn
, gac
, NULL
);
10405 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
10406 "doesn't exist.\n", wins
[j
]);
10410 if (gar
->override_redirect
) {
10411 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
10412 "override_redirect set.\n", wins
[j
]);
10417 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
10418 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
10420 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
10421 "is transient for %#x.\n", wins
[j
], trans
);
10426 state
= get_win_state(wins
[j
]);
10427 manage
= state
!= XCB_ICCCM_WM_STATE_WITHDRAWN
;
10428 mapped
= gar
->map_state
== XCB_MAP_STATE_VIEWABLE
;
10429 if (mapped
|| manage
)
10430 manage_window(wins
[j
], SWM_STACK_TOP
, mapped
);
10433 /* transient windows */
10434 DNPRINTF(SWM_D_INIT
, "grab_windows: grab transient windows.\n");
10435 for (j
= 0; j
< no
; j
++) {
10436 gac
= xcb_get_window_attributes(conn
, wins
[j
]);
10437 gar
= xcb_get_window_attributes_reply(conn
, gac
, NULL
);
10439 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
10440 "doesn't exist.\n", wins
[j
]);
10444 if (gar
->override_redirect
) {
10445 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
10446 "override_redirect set.\n", wins
[j
]);
10451 state
= get_win_state(wins
[j
]);
10452 manage
= state
!= XCB_ICCCM_WM_STATE_WITHDRAWN
;
10453 mapped
= gar
->map_state
== XCB_MAP_STATE_VIEWABLE
;
10454 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
10455 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
10456 &trans
, NULL
) && manage
)
10457 manage_window(wins
[j
], SWM_STACK_TOP
, mapped
);
10462 DNPRINTF(SWM_D_INIT
, "grab_windows: done.\n");
10466 setup_screens(void)
10468 int i
, j
, k
, num_screens
;
10469 struct workspace
*ws
;
10470 uint32_t gcv
[1], wa
[1];
10471 const xcb_query_extension_reply_t
*qep
;
10472 xcb_screen_t
*screen
;
10473 xcb_randr_query_version_cookie_t c
;
10474 xcb_randr_query_version_reply_t
*r
;
10476 num_screens
= get_screen_count();
10477 if ((screens
= calloc(num_screens
,
10478 sizeof(struct swm_screen
))) == NULL
)
10479 err(1, "setup_screens: calloc: failed to allocate memory for "
10482 /* Initial RandR setup. */
10483 randr_support
= false;
10484 qep
= xcb_get_extension_data(conn
, &xcb_randr_id
);
10485 if (qep
->present
) {
10486 c
= xcb_randr_query_version(conn
, 1, 1);
10487 r
= xcb_randr_query_version_reply(conn
, c
, NULL
);
10489 if (r
->major_version
>= 1) {
10490 randr_support
= true;
10491 randr_eventbase
= qep
->first_event
;
10497 wa
[0] = cursors
[XC_LEFT_PTR
].cid
;
10499 /* map physical screens */
10500 for (i
= 0; i
< num_screens
; i
++) {
10501 DNPRINTF(SWM_D_WS
, "setup_screens: init screen: %d\n", i
);
10502 screens
[i
].idx
= i
;
10503 screens
[i
].r_focus
= NULL
;
10505 TAILQ_INIT(&screens
[i
].rl
);
10506 TAILQ_INIT(&screens
[i
].orl
);
10507 if ((screen
= get_screen(i
)) == NULL
)
10508 errx(1, "ERROR: can't get screen %d.", i
);
10509 screens
[i
].root
= screen
->root
;
10511 /* set default colors */
10512 setscreencolor("red", i
, SWM_S_COLOR_FOCUS
);
10513 setscreencolor("rgb:88/88/88", i
, SWM_S_COLOR_UNFOCUS
);
10514 setscreencolor("rgb:00/80/80", i
, SWM_S_COLOR_BAR_BORDER
);
10515 setscreencolor("rgb:00/40/40", i
,
10516 SWM_S_COLOR_BAR_BORDER_UNFOCUS
);
10517 setscreencolor("black", i
, SWM_S_COLOR_BAR
);
10518 setscreencolor("rgb:a0/a0/a0", i
, SWM_S_COLOR_BAR_FONT
);
10519 setscreencolor("red", i
, SWM_S_COLOR_FOCUS_MAXIMIZED
);
10520 setscreencolor("rgb:88/88/88", i
,
10521 SWM_S_COLOR_UNFOCUS_MAXIMIZED
);
10523 /* create graphics context on screen */
10524 screens
[i
].bar_gc
= xcb_generate_id(conn
);
10526 xcb_create_gc(conn
, screens
[i
].bar_gc
, screens
[i
].root
,
10527 XCB_GC_GRAPHICS_EXPOSURES
, gcv
);
10529 /* set default cursor */
10530 xcb_change_window_attributes(conn
, screens
[i
].root
,
10531 XCB_CW_CURSOR
, wa
);
10533 /* init all workspaces */
10534 /* XXX these should be dynamically allocated too */
10535 for (j
= 0; j
< SWM_WS_MAX
; j
++) {
10536 ws
= &screens
[i
].ws
[j
];
10539 ws
->bar_enabled
= true;
10541 ws
->focus_prev
= NULL
;
10542 ws
->focus_pending
= NULL
;
10545 ws
->state
= SWM_WS_STATE_HIDDEN
;
10546 TAILQ_INIT(&ws
->stack
);
10547 TAILQ_INIT(&ws
->winlist
);
10548 TAILQ_INIT(&ws
->unmanagedlist
);
10550 for (k
= 0; layouts
[k
].l_stack
!= NULL
; k
++)
10551 if (layouts
[k
].l_config
!= NULL
)
10552 layouts
[k
].l_config(ws
,
10553 SWM_ARG_ID_STACKINIT
);
10554 ws
->cur_layout
= &layouts
[0];
10555 ws
->cur_layout
->l_string(ws
);
10561 xcb_randr_select_input(conn
, screens
[i
].root
,
10562 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE
);
10567 setup_globals(void)
10569 if ((bar_fonts
= strdup(SWM_BAR_FONTS
)) == NULL
)
10570 err(1, "setup_globals: strdup: failed to allocate memory.");
10572 if ((clock_format
= strdup("%a %b %d %R %Z %Y")) == NULL
)
10573 err(1, "setup_globals: strdup: failed to allocate memory.");
10575 if ((syms
= xcb_key_symbols_alloc(conn
)) == NULL
)
10576 errx(1, "unable to allocate key symbols");
10578 a_state
= get_atom_from_string("WM_STATE");
10579 a_prot
= get_atom_from_string("WM_PROTOCOLS");
10580 a_delete
= get_atom_from_string("WM_DELETE_WINDOW");
10581 a_net_supported
= get_atom_from_string("_NET_SUPPORTED");
10582 a_net_wm_check
= get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
10583 a_takefocus
= get_atom_from_string("WM_TAKE_FOCUS");
10584 a_utf8_string
= get_atom_from_string("UTF8_STRING");
10585 a_swm_ws
= get_atom_from_string("_SWM_WS");
10589 shutdown_cleanup(void)
10591 int i
, num_screens
;
10593 /* disable alarm because the following code may not be interrupted */
10595 if (signal(SIGALRM
, SIG_IGN
) == SIG_ERR
)
10596 err(1, "can't disable alarm");
10605 num_screens
= get_screen_count();
10606 for (i
= 0; i
< num_screens
; ++i
) {
10607 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
10608 screens
[i
].root
, XCB_CURRENT_TIME
);
10610 if (screens
[i
].bar_gc
!= XCB_NONE
)
10611 xcb_free_gc(conn
, screens
[i
].bar_gc
);
10612 if (!bar_font_legacy
) {
10613 XftColorFree(display
, DefaultVisual(display
, i
),
10614 DefaultColormap(display
, i
), &bar_font_color
);
10615 XftColorFree(display
, DefaultVisual(display
, i
),
10616 DefaultColormap(display
, i
), &search_font_color
);
10620 if (bar_font_legacy
)
10621 XFreeFontSet(display
, bar_fs
);
10623 XftFontClose(display
, bar_font
);
10626 xcb_key_symbols_free(syms
);
10628 xcb_disconnect(conn
);
10632 event_error(xcb_generic_error_t
*e
)
10636 DNPRINTF(SWM_D_EVENT
, "event_error: %s(%u) from %s(%u), sequence: %u, "
10637 "resource_id: %u, minor_code: %u\n",
10638 xcb_event_get_error_label(e
->error_code
), e
->error_code
,
10639 xcb_event_get_request_label(e
->major_code
), e
->major_code
,
10640 e
->sequence
, e
->resource_id
, e
->minor_code
);
10644 event_handle(xcb_generic_event_t
*evt
)
10646 uint8_t type
= XCB_EVENT_RESPONSE_TYPE(evt
);
10648 DNPRINTF(SWM_D_EVENT
, "XCB Event: %s(%d), seq %u\n",
10649 xcb_event_get_label(XCB_EVENT_RESPONSE_TYPE(evt
)),
10650 XCB_EVENT_RESPONSE_TYPE(evt
), evt
->sequence
);
10653 #define EVENT(type, callback) case type: callback((void *)evt); return
10654 EVENT(0, event_error
);
10655 EVENT(XCB_BUTTON_PRESS
, buttonpress
);
10656 /*EVENT(XCB_BUTTON_RELEASE, buttonpress);*/
10657 /*EVENT(XCB_CIRCULATE_NOTIFY, );*/
10658 /*EVENT(XCB_CIRCULATE_REQUEST, );*/
10659 EVENT(XCB_CLIENT_MESSAGE
, clientmessage
);
10660 /*EVENT(XCB_COLORMAP_NOTIFY, );*/
10661 EVENT(XCB_CONFIGURE_NOTIFY
, configurenotify
);
10662 EVENT(XCB_CONFIGURE_REQUEST
, configurerequest
);
10663 /*EVENT(XCB_CREATE_NOTIFY, );*/
10664 EVENT(XCB_DESTROY_NOTIFY
, destroynotify
);
10665 EVENT(XCB_ENTER_NOTIFY
, enternotify
);
10666 EVENT(XCB_EXPOSE
, expose
);
10668 EVENT(XCB_FOCUS_IN
, focusin
);
10669 EVENT(XCB_FOCUS_OUT
, focusout
);
10671 /*EVENT(XCB_GRAPHICS_EXPOSURE, );*/
10672 /*EVENT(XCB_GRAVITY_NOTIFY, );*/
10673 EVENT(XCB_KEY_PRESS
, keypress
);
10674 /*EVENT(XCB_KEY_RELEASE, keypress);*/
10675 /*EVENT(XCB_KEYMAP_NOTIFY, );*/
10677 EVENT(XCB_LEAVE_NOTIFY
, leavenotify
);
10679 EVENT(XCB_MAP_NOTIFY
, mapnotify
);
10680 EVENT(XCB_MAP_REQUEST
, maprequest
);
10681 EVENT(XCB_MAPPING_NOTIFY
, mappingnotify
);
10682 EVENT(XCB_MOTION_NOTIFY
, motionnotify
);
10683 /*EVENT(XCB_NO_EXPOSURE, );*/
10684 EVENT(XCB_PROPERTY_NOTIFY
, propertynotify
);
10685 /*EVENT(XCB_REPARENT_NOTIFY, );*/
10686 /*EVENT(XCB_RESIZE_REQUEST, );*/
10687 /*EVENT(XCB_SELECTION_CLEAR, );*/
10688 /*EVENT(XCB_SELECTION_NOTIFY, );*/
10689 /*EVENT(XCB_SELECTION_REQUEST, );*/
10690 EVENT(XCB_UNMAP_NOTIFY
, unmapnotify
);
10691 /*EVENT(XCB_VISIBILITY_NOTIFY, );*/
10694 if (type
- randr_eventbase
== XCB_RANDR_SCREEN_CHANGE_NOTIFY
)
10695 screenchange((void *)evt
);
10699 main(int argc
, char *argv
[])
10701 struct swm_region
*r
;
10702 char conf
[PATH_MAX
], *cfile
= NULL
;
10704 int xfd
, i
, num_screens
;
10705 struct sigaction sact
;
10706 xcb_generic_event_t
*evt
;
10711 bool stdin_ready
= false, startup
= true;
10713 /* suppress unused warning since var is needed */
10717 time_started
= time(NULL
);
10721 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION
, buildstr
);
10722 if (setlocale(LC_CTYPE
, "") == NULL
|| setlocale(LC_TIME
, "") == NULL
)
10723 warnx("no locale support");
10725 /* handle some signals */
10726 bzero(&sact
, sizeof(sact
));
10727 sigemptyset(&sact
.sa_mask
);
10729 sact
.sa_handler
= sighdlr
;
10730 sigaction(SIGINT
, &sact
, NULL
);
10731 sigaction(SIGQUIT
, &sact
, NULL
);
10732 sigaction(SIGTERM
, &sact
, NULL
);
10733 sigaction(SIGHUP
, &sact
, NULL
);
10735 sact
.sa_handler
= sighdlr
;
10736 sact
.sa_flags
= SA_NOCLDSTOP
;
10737 sigaction(SIGCHLD
, &sact
, NULL
);
10739 if ((display
= XOpenDisplay(0)) == NULL
)
10740 errx(1, "can not open display");
10742 conn
= XGetXCBConnection(display
);
10743 if (xcb_connection_has_error(conn
))
10744 errx(1, "can not get XCB connection");
10746 XSetEventQueueOwner(display
, XCBOwnsEventQueue
);
10748 xcb_prefetch_extension_data(conn
, &xcb_randr_id
);
10749 xfd
= xcb_get_file_descriptor(conn
);
10751 /* look for local and global conf file */
10752 pwd
= getpwuid(getuid());
10754 errx(1, "invalid user: %d", getuid());
10756 xcb_grab_server(conn
);
10757 xcb_aux_sync(conn
);
10759 /* flush all events */
10760 while ((evt
= xcb_poll_for_event(conn
))) {
10761 if (XCB_EVENT_RESPONSE_TYPE(evt
) == 0)
10767 errx(1, "another window manager is currently running");
10769 /* Load Xcursors and/or cursorfont glyph cursors. */
10772 xcb_aux_sync(conn
);
10782 for (i
= 0; ; i
++) {
10787 snprintf(conf
, sizeof conf
, "%s/.%s",
10788 pwd
->pw_dir
, SWM_CONF_FILE
);
10792 snprintf(conf
, sizeof conf
, "/etc/%s",
10797 snprintf(conf
, sizeof conf
, "%s/.%s",
10798 pwd
->pw_dir
, SWM_CONF_FILE_OLD
);
10801 /* global compat */
10802 snprintf(conf
, sizeof conf
, "/etc/%s",
10803 SWM_CONF_FILE_OLD
);
10809 if (strlen(conf
) && stat(conf
, &sb
) != -1)
10810 if (S_ISREG(sb
.st_mode
)) {
10817 /* load conf (if any) */
10819 conf_load(cfile
, SWM_CONF_DEFAULT
);
10823 if (getenv("SWM_STARTED") == NULL
)
10824 setenv("SWM_STARTED", "YES", 1);
10826 /* setup all bars */
10827 num_screens
= get_screen_count();
10828 for (i
= 0; i
< num_screens
; i
++)
10829 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
10832 /* Manage existing windows. */
10839 xcb_ungrab_server(conn
);
10842 /* Update state of each newly mapped workspace. */
10843 for (i
= 0; i
< num_screens
; i
++)
10844 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
10845 r
->ws
->state
= SWM_WS_STATE_MAPPED
;
10847 rd_max
= xfd
> STDIN_FILENO
? xfd
: STDIN_FILENO
;
10850 while ((evt
= xcb_poll_for_event(conn
))) {
10857 /* If just (re)started, set default focus if needed. */
10861 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
10862 r
= TAILQ_FIRST(&screens
[0].rl
);
10874 FD_SET(STDIN_FILENO
, &rd
);
10879 num_readable
= select(rd_max
+ 1, &rd
, NULL
, NULL
, &tv
);
10880 if (num_readable
== -1 && errno
!= EINTR
) {
10881 DNPRINTF(SWM_D_MISC
, "select failed");
10882 } else if (num_readable
> 0 && FD_ISSET(STDIN_FILENO
, &rd
)) {
10883 stdin_ready
= true;
10887 restart(NULL
, NULL
);
10896 stdin_ready
= false;
10897 bar_extra_update();
10904 shutdown_cleanup();