2 * Copyright (c) 2009-2012 Marco Peereboom <marco@peereboom.us>
3 * Copyright (c) 2009-2011 Ryan McBride <mcbride@countersiege.com>
4 * Copyright (c) 2009 Darrin Chandler <dwchandler@stilyagin.com>
5 * Copyright (c) 2009 Pierre-Yves Ritschard <pyr@spootnik.org>
6 * Copyright (c) 2010 Tuukka Kataja <stuge@xor.fi>
7 * Copyright (c) 2011 Jason L. Wright <jason@thought.net>
8 * Copyright (c) 2011-2014 Reginald Kennedy <rk@rejii.com>
9 * Copyright (c) 2011-2012 Lawrence Teo <lteo@lteo.net>
10 * Copyright (c) 2011-2012 Tiago Cunha <tcunha@gmx.com>
11 * Copyright (c) 2012-2013 David Hill <dhill@mindcry.org>
13 * Permission to use, copy, modify, and distribute this software for any
14 * purpose with or without fee is hereby granted, provided that the above
15 * copyright notice and this permission notice appear in all copies.
17 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
18 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
20 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
21 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
22 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
23 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 * Much code and ideas taken from dwm under the following license:
27 * MIT/X Consortium License
29 * 2006-2008 Anselm R Garbe <garbeam at gmail dot com>
30 * 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
31 * 2006-2007 Jukka Salmi <jukka at salmi dot ch>
32 * 2007 Premysl Hruby <dfenze at gmail dot com>
33 * 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
34 * 2007 Christof Musik <christof at sendfax dot de>
35 * 2007-2008 Enno Gottox Boland <gottox at s01 dot de>
36 * 2007-2008 Peter Hartlich <sgkkr at hartlich dot com>
37 * 2008 Martin Hurton <martin dot hurton at gmail dot com>
39 * Permission is hereby granted, free of charge, to any person obtaining a
40 * copy of this software and associated documentation files (the "Software"),
41 * to deal in the Software without restriction, including without limitation
42 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
43 * and/or sell copies of the Software, and to permit persons to whom the
44 * Software is furnished to do so, subject to the following conditions:
46 * The above copyright notice and this permission notice shall be included in
47 * all copies or substantial portions of the Software.
49 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
50 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
51 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
52 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
53 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
54 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
55 * DEALINGS IN THE SOFTWARE.
59 #include <sys/types.h>
63 #include <sys/queue.h>
64 #include <sys/param.h>
65 #include <sys/select.h>
66 #if defined(__linux__)
68 #elif defined(__OpenBSD__)
70 #elif defined(__FreeBSD__)
93 #include <X11/cursorfont.h>
94 #include <X11/extensions/Xrandr.h>
95 #include <X11/Xcursor/Xcursor.h>
96 #include <X11/Xft/Xft.h>
97 #include <X11/Xlib-xcb.h>
98 #include <xcb/xcb_atom.h>
99 #include <xcb/xcb_aux.h>
100 #include <xcb/xcb_event.h>
101 #include <xcb/xcb_icccm.h>
102 #include <xcb/xcb_keysyms.h>
103 #include <xcb/xtest.h>
104 #include <xcb/randr.h>
112 #ifdef SPECTRWM_BUILDSTR
113 static const char *buildstr
= SPECTRWM_BUILDSTR
;
115 static const char *buildstr
= SPECTRWM_VERSION
;
118 #if !defined(__CYGWIN__) /* cygwin chokes on randr stuff */
120 # error RandR versions less than 1.0 are not supported
123 # if RANDR_MAJOR >= 1
124 # if RANDR_MINOR >= 2
125 # define SWM_XRR_HAS_CRTC
128 #endif /* __CYGWIN__ */
130 #ifndef XCB_ICCCM_NUM_WM_HINTS_ELEMENTS
131 #define XCB_ICCCM_SIZE_HINT_P_MIN_SIZE XCB_SIZE_HINT_P_MIN_SIZE
132 #define XCB_ICCCM_SIZE_HINT_P_MAX_SIZE XCB_SIZE_HINT_P_MAX_SIZE
133 #define XCB_ICCCM_SIZE_HINT_P_RESIZE_INC XCB_SIZE_HINT_P_RESIZE_INC
134 #define XCB_ICCCM_WM_HINT_INPUT XCB_WM_HINT_INPUT
135 #define XCB_ICCCM_WM_HINT_X_URGENCY XCB_WM_HINT_X_URGENCY
136 #define XCB_ICCCM_WM_STATE_ICONIC XCB_WM_STATE_ICONIC
137 #define XCB_ICCCM_WM_STATE_WITHDRAWN XCB_WM_STATE_WITHDRAWN
138 #define XCB_ICCCM_WM_STATE_NORMAL XCB_WM_STATE_NORMAL
139 #define xcb_icccm_get_text_property_reply_t xcb_get_text_property_reply_t
140 #define xcb_icccm_get_text_property_reply_wipe xcb_get_text_property_reply_wipe
141 #define xcb_icccm_get_wm_class xcb_get_wm_class
142 #define xcb_icccm_get_wm_class_reply xcb_get_wm_class_reply
143 #define xcb_icccm_get_wm_class_reply_t xcb_get_wm_class_reply_t
144 #define xcb_icccm_get_wm_class_reply_wipe xcb_get_wm_class_reply_wipe
145 #define xcb_icccm_get_wm_hints xcb_get_wm_hints
146 #define xcb_icccm_wm_hints_get_urgency xcb_wm_hints_get_urgency
147 #define xcb_icccm_get_wm_hints_reply xcb_get_wm_hints_reply
148 #define xcb_icccm_get_wm_name xcb_get_wm_name
149 #define xcb_icccm_get_wm_name_reply xcb_get_wm_name_reply
150 #define xcb_icccm_get_wm_normal_hints xcb_get_wm_normal_hints
151 #define xcb_icccm_get_wm_normal_hints_reply xcb_get_wm_normal_hints_reply
152 #define xcb_icccm_get_wm_protocols xcb_get_wm_protocols
153 #define xcb_icccm_get_wm_protocols_reply xcb_get_wm_protocols_reply
154 #define xcb_icccm_get_wm_protocols_reply_t xcb_get_wm_protocols_reply_t
155 #define xcb_icccm_get_wm_protocols_reply_wipe xcb_get_wm_protocols_reply_wipe
156 #define xcb_icccm_get_wm_transient_for xcb_get_wm_transient_for
157 #define xcb_icccm_get_wm_transient_for_reply xcb_get_wm_transient_for_reply
158 #define xcb_icccm_wm_hints_t xcb_wm_hints_t
161 /*#define SWM_DEBUG*/
163 #define DPRINTF(x...) do { \
165 fprintf(stderr, x); \
167 #define DNPRINTF(n,x...) do { \
168 if (swm_debug & n) { \
169 fprintf(stderr, "%ld ", (long)(time(NULL) - time_started)); \
170 fprintf(stderr, x); \
173 #define SWM_D_MISC 0x0001
174 #define SWM_D_EVENT 0x0002
175 #define SWM_D_WS 0x0004
176 #define SWM_D_FOCUS 0x0008
177 #define SWM_D_MOVE 0x0010
178 #define SWM_D_STACK 0x0020
179 #define SWM_D_MOUSE 0x0040
180 #define SWM_D_PROP 0x0080
181 #define SWM_D_CLASS 0x0100
182 #define SWM_D_KEY 0x0200
183 #define SWM_D_QUIRK 0x0400
184 #define SWM_D_SPAWN 0x0800
185 #define SWM_D_EVENTQ 0x1000
186 #define SWM_D_CONF 0x2000
187 #define SWM_D_BAR 0x4000
188 #define SWM_D_INIT 0x8000
190 uint32_t swm_debug
= 0
209 #define DPRINTF(x...)
210 #define DNPRINTF(n,x...)
213 #define SWM_EWMH_ACTION_COUNT_MAX (8)
214 #define EWMH_F_FULLSCREEN (0x001)
215 #define EWMH_F_ABOVE (0x002)
216 #define EWMH_F_HIDDEN (0x004)
217 #define EWMH_F_MAXIMIZED_VERT (0x008)
218 #define EWMH_F_MAXIMIZED_HORZ (0x010)
219 #define EWMH_F_SKIP_PAGER (0x020)
220 #define EWMH_F_SKIP_TASKBAR (0x040)
221 #define SWM_F_MANUAL (0x080)
223 #define EWMH_F_MAXIMIZED (EWMH_F_MAXIMIZED_VERT | EWMH_F_MAXIMIZED_HORZ)
225 /* convert 8-bit to 16-bit */
226 #define RGB_8_TO_16(col) (((col) << 8) + (col))
228 #define PIXEL_TO_XRENDERCOLOR(px, xrc) \
229 xrc.red = RGB_8_TO_16((px) >> 16 & 0xff); \
230 xrc.green = RGB_8_TO_16((px) >> 8 & 0xff); \
231 xrc.blue = RGB_8_TO_16((px) & 0xff); \
234 #define LENGTH(x) (int)(sizeof (x) / sizeof (x)[0])
235 #define MODKEY XCB_MOD_MASK_1
236 #define CLEANMASK(mask) ((mask) & ~(numlockmask | XCB_MOD_MASK_LOCK))
237 #define BUTTONMASK (XCB_EVENT_MASK_BUTTON_PRESS | \
238 XCB_EVENT_MASK_BUTTON_RELEASE)
239 #define MOUSEMASK (BUTTONMASK|XCB_EVENT_MASK_POINTER_MOTION)
240 #define SWM_PROPLEN (16)
241 #define SWM_FUNCNAME_LEN (32)
242 #define SWM_KEYS_LEN (255)
243 #define SWM_QUIRK_LEN (64)
244 #define X(r) ((r)->g.x)
245 #define Y(r) ((r)->g.y)
246 #define WIDTH(r) ((r)->g.w)
247 #define HEIGHT(r) ((r)->g.h)
248 #define BORDER(w) ((w)->bordered ? border_width : 0)
249 #define MAX_X(r) ((r)->g.x + (r)->g.w)
250 #define MAX_Y(r) ((r)->g.y + (r)->g.h)
251 #define SH_MIN(w) ((w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE)
252 #define SH_MIN_W(w) ((w)->sh.min_width)
253 #define SH_MIN_H(w) ((w)->sh.min_height)
254 #define SH_MAX(w) ((w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MAX_SIZE)
255 #define SH_MAX_W(w) ((w)->sh.max_width)
256 #define SH_MAX_H(w) ((w)->sh.max_height)
257 #define SH_INC(w) ((w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_RESIZE_INC)
258 #define SH_INC_W(w) ((w)->sh.width_inc)
259 #define SH_INC_H(w) ((w)->sh.height_inc)
260 #define SWM_MAX_FONT_STEPS (3)
261 #define WINID(w) ((w) ? (w)->id : XCB_WINDOW_NONE)
262 #define WS_FOCUSED(ws) ((ws)->r && (ws)->r->s->r_focus == (ws)->r)
263 #define YESNO(x) ((x) ? "yes" : "no")
264 #define ICONIC(w) ((w)->ewmh_flags & EWMH_F_HIDDEN)
265 #define ABOVE(w) ((w)->ewmh_flags & EWMH_F_ABOVE)
266 #define FULLSCREEN(w) ((w)->ewmh_flags & EWMH_F_FULLSCREEN)
267 #define MAXIMIZED_VERT(w) ((w)->ewmh_flags & EWMH_F_MAXIMIZED_VERT)
268 #define MAXIMIZED_HORZ(w) ((w)->ewmh_flags & EWMH_F_MAXIMIZED_HORZ)
269 #define MAXIMIZED(w) (MAXIMIZED_VERT(w) || MAXIMIZED_HORZ(w))
270 #define MANUAL(w) ((w)->ewmh_flags & SWM_F_MANUAL)
271 #define TRANS(w) ((w)->transient != XCB_WINDOW_NONE)
272 #define FLOATING(w) (ABOVE(w) || TRANS(w) || FULLSCREEN(w) || \
275 /* Constrain Window flags */
276 #define SWM_CW_RESIZABLE (0x01)
277 #define SWM_CW_SOFTBOUNDARY (0x02)
278 #define SWM_CW_HARDBOUNDARY (0x04)
279 #define SWM_CW_RIGHT (0x10)
280 #define SWM_CW_LEFT (0x20)
281 #define SWM_CW_BOTTOM (0x40)
282 #define SWM_CW_TOP (0x80)
283 #define SWM_CW_ALLSIDES (0xf0)
285 #define SWM_FOCUS_DEFAULT (0)
286 #define SWM_FOCUS_FOLLOW (1)
287 #define SWM_FOCUS_MANUAL (2)
289 #define SWM_CK_NONE (0)
290 #define SWM_CK_ALL (0xf)
291 #define SWM_CK_FOCUS (0x1)
292 #define SWM_CK_POINTER (0x2)
293 #define SWM_CK_FALLBACK (0x4)
294 #define SWM_CK_REGION (0x8)
296 #define SWM_G_ALL (0xf)
297 #define SWM_G_SIZE (0x1)
298 #define SWM_G_POS (0x2)
300 #define SWM_CONF_DEFAULT (0)
301 #define SWM_CONF_KEYMAPPING (1)
304 #define SWM_LIB "/usr/local/lib/libswmhack.so"
311 xcb_atom_t a_net_wm_check
;
312 xcb_atom_t a_net_supported
;
313 xcb_atom_t a_takefocus
;
314 xcb_atom_t a_utf8_string
;
316 volatile sig_atomic_t running
= 1;
317 volatile sig_atomic_t restart_wm
= 0;
318 xcb_timestamp_t last_event_time
= 0;
322 unsigned int numlockmask
= 0;
325 xcb_connection_t
*conn
;
326 xcb_key_symbols_t
*syms
;
328 int boundary_width
= 50;
329 bool cycle_empty
= false;
330 bool cycle_visible
= false;
332 int font_adjusted
= 0;
333 unsigned int mod_key
= MODKEY
;
334 bool warp_pointer
= false;
337 struct swm_region
*search_r
;
338 int select_list_pipe
[2];
339 int select_resp_pipe
[2];
341 volatile sig_atomic_t search_resp
;
342 int search_resp_action
;
344 struct search_window
{
345 TAILQ_ENTRY(search_window
) entry
;
349 xcb_window_t indicator
;
351 TAILQ_HEAD(search_winlist
, search_window
);
352 struct search_winlist search_wl
;
357 SWM_SEARCH_UNICONIFY
,
358 SWM_SEARCH_NAME_WORKSPACE
,
359 SWM_SEARCH_SEARCH_WORKSPACE
,
360 SWM_SEARCH_SEARCH_WINDOW
363 #define SWM_STACK_TOP (0)
364 #define SWM_STACK_BOTTOM (1)
365 #define SWM_STACK_ABOVE (2)
366 #define SWM_STACK_BELOW (3)
369 double dialog_ratio
= 0.6;
371 #define SWM_BAR_MAX (256)
372 #define SWM_BAR_JUSTIFY_LEFT (0)
373 #define SWM_BAR_JUSTIFY_CENTER (1)
374 #define SWM_BAR_JUSTIFY_RIGHT (2)
375 #define SWM_BAR_OFFSET (4)
376 #define SWM_BAR_FONTS "-*-terminus-medium-*-*-*-12-*-*-*-*-*-*-*," \
377 "-*-profont-*-*-*-*-12-*-*-*-*-*-*-*," \
378 "-*-times-medium-r-*-*-12-*-*-*-*-*-*-*," \
379 "-misc-fixed-medium-r-*-*-12-*-*-*-*-*-*-*," \
380 "-*-*-*-r-*-*-*-*-*-*-*-*-*-*"
382 #ifdef X_HAVE_UTF8_STRING
383 #define DRAWSTRING(x...) Xutf8DrawString(x)
385 #define DRAWSTRING(x...) XmbDrawString(x)
388 char *bar_argv
[] = { NULL
, NULL
};
390 char bar_ext
[SWM_BAR_MAX
];
391 char bar_ext_buf
[SWM_BAR_MAX
];
392 char bar_vertext
[SWM_BAR_MAX
];
393 bool bar_version
= false;
394 bool bar_enabled
= true;
395 int bar_border_width
= 1;
396 bool bar_at_bottom
= false;
397 bool bar_extra
= false;
399 int bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
400 char *bar_format
= NULL
;
401 bool stack_enabled
= true;
402 bool clock_enabled
= true;
403 bool iconic_enabled
= false;
404 bool urgent_enabled
= false;
405 bool urgent_collapse
= false;
406 char *clock_format
= NULL
;
407 bool window_class_enabled
= false;
408 bool window_instance_enabled
= false;
409 bool window_name_enabled
= false;
410 int focus_mode
= SWM_FOCUS_DEFAULT
;
411 int focus_close
= SWM_STACK_BELOW
;
412 bool focus_close_wrap
= true;
413 int focus_default
= SWM_STACK_TOP
;
414 int spawn_position
= SWM_STACK_TOP
;
415 bool disable_border
= false;
416 int border_width
= 1;
417 int region_padding
= 0;
419 bool java_workaround
= true;
420 bool verbose_layout
= false;
426 XFontSetExtents
*bar_fs_extents
;
428 bool bar_font_legacy
= true;
430 XftColor bar_font_color
;
431 XftColor search_font_color
;
433 char *startup_exception
;
434 unsigned int nr_exceptions
= 0;
436 /* layout manager data */
437 struct swm_geometry
{
450 struct swm_geometry g
;
453 /* virtual "screens" */
455 TAILQ_ENTRY(swm_region
) entry
;
457 struct swm_geometry g
;
458 struct workspace
*ws
; /* current workspace on this region */
459 struct workspace
*ws_prior
; /* prior workspace on this region */
460 struct swm_screen
*s
; /* screen idx */
463 TAILQ_HEAD(swm_region_list
, swm_region
);
466 TAILQ_ENTRY(ws_win
) entry
;
467 TAILQ_ENTRY(ws_win
) stack_entry
;
469 xcb_window_t transient
;
470 struct ws_win
*focus_child
; /* focus on child transient */
471 struct swm_geometry g
; /* current geometry */
472 struct swm_geometry g_float
; /* region coordinates */
473 bool g_floatvalid
; /* g_float geometry validity */
477 int font_size_boundary
[SWM_MAX_FONT_STEPS
];
484 struct workspace
*ws
; /* always valid */
485 struct swm_screen
*s
; /* always valid, never changes */
487 xcb_icccm_get_wm_class_reply_t ch
;
488 xcb_icccm_wm_hints_t hints
;
490 TAILQ_HEAD(ws_win_list
, ws_win
);
491 TAILQ_HEAD(ws_win_stack
, ws_win
);
495 TAILQ_ENTRY(pid_e
) entry
;
499 TAILQ_HEAD(pid_list
, pid_e
);
500 struct pid_list pidlist
= TAILQ_HEAD_INITIALIZER(pidlist
);
502 /* layout handlers */
504 void vertical_config(struct workspace
*, int);
505 void vertical_stack(struct workspace
*, struct swm_geometry
*);
506 void horizontal_config(struct workspace
*, int);
507 void horizontal_stack(struct workspace
*, struct swm_geometry
*);
508 void max_stack(struct workspace
*, struct swm_geometry
*);
509 void plain_stacker(struct workspace
*);
510 void fancy_stacker(struct workspace
*);
513 void (*l_stack
)(struct workspace
*, struct swm_geometry
*);
514 void (*l_config
)(struct workspace
*, int);
516 #define SWM_L_FOCUSPREV (1<<0)
517 #define SWM_L_MAPONFOCUS (1<<1)
518 void (*l_string
)(struct workspace
*);
520 /* stack, configure */
521 { vertical_stack
, vertical_config
, 0, plain_stacker
},
522 { horizontal_stack
, horizontal_config
, 0, plain_stacker
},
524 SWM_L_MAPONFOCUS
| SWM_L_FOCUSPREV
, plain_stacker
},
525 { NULL
, NULL
, 0, NULL
},
528 /* position of max_stack mode in the layouts array, index into layouts! */
529 #define SWM_V_STACK (0)
530 #define SWM_H_STACK (1)
531 #define SWM_MAX_STACK (2)
533 #define SWM_H_SLICE (32)
534 #define SWM_V_SLICE (32)
536 /* define work spaces */
538 int idx
; /* workspace index */
539 char *name
; /* workspace name */
540 bool always_raise
; /* raise windows on focus */
541 bool bar_enabled
; /* bar visibility */
542 struct layout
*cur_layout
; /* current layout handlers */
543 struct ws_win
*focus
; /* may be NULL */
544 struct ws_win
*focus_prev
; /* may be NULL */
545 struct ws_win
*focus_pending
; /* may be NULL */
546 struct swm_region
*r
; /* may be NULL */
547 struct swm_region
*old_r
; /* may be NULL */
548 struct ws_win_list winlist
; /* list of windows in ws */
549 struct ws_win_list unmanagedlist
; /* list of dead windows in ws */
550 struct ws_win_stack stack
; /* stacking order */
551 int state
; /* mapping state */
552 char stacker
[10]; /* display stacker and layout */
556 int horizontal_msize
;
558 int horizontal_stacks
;
559 bool horizontal_flip
;
569 SWM_WS_STATE_MAPPING
,
575 SWM_S_COLOR_BAR_BORDER
,
576 SWM_S_COLOR_BAR_BORDER_UNFOCUS
,
577 SWM_S_COLOR_BAR_FONT
,
579 SWM_S_COLOR_FOCUS_MAXIMIZED
,
581 SWM_S_COLOR_UNFOCUS_MAXIMIZED
,
585 /* physical screen mapping */
586 #define SWM_WS_MAX (22) /* hard limit */
587 int workspace_limit
= 10; /* soft limit */
590 int idx
; /* screen index */
591 struct swm_region_list rl
; /* list of regions on this screen */
592 struct swm_region_list orl
; /* list of old regions */
594 struct workspace ws
[SWM_WS_MAX
];
595 struct swm_region
*r_focus
;
602 } c
[SWM_S_COLOR_MAX
];
604 xcb_gcontext_t bar_gc
;
607 struct swm_screen
*screens
;
609 /* args to functions */
612 #define SWM_ARG_ID_FOCUSNEXT (0)
613 #define SWM_ARG_ID_FOCUSPREV (1)
614 #define SWM_ARG_ID_FOCUSMAIN (2)
615 #define SWM_ARG_ID_FOCUSURGENT (3)
616 #define SWM_ARG_ID_SWAPNEXT (10)
617 #define SWM_ARG_ID_SWAPPREV (11)
618 #define SWM_ARG_ID_SWAPMAIN (12)
619 #define SWM_ARG_ID_MOVELAST (13)
620 #define SWM_ARG_ID_MASTERSHRINK (20)
621 #define SWM_ARG_ID_MASTERGROW (21)
622 #define SWM_ARG_ID_MASTERADD (22)
623 #define SWM_ARG_ID_MASTERDEL (23)
624 #define SWM_ARG_ID_FLIPLAYOUT (24)
625 #define SWM_ARG_ID_STACKRESET (30)
626 #define SWM_ARG_ID_STACKINIT (31)
627 #define SWM_ARG_ID_CYCLEWS_UP (40)
628 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
629 #define SWM_ARG_ID_CYCLERG_UP (42)
630 #define SWM_ARG_ID_CYCLERG_DOWN (43)
631 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
632 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
633 #define SWM_ARG_ID_CYCLEWS_MOVE_UP (46)
634 #define SWM_ARG_ID_CYCLEWS_MOVE_DOWN (47)
635 #define SWM_ARG_ID_STACKINC (50)
636 #define SWM_ARG_ID_STACKDEC (51)
637 #define SWM_ARG_ID_SS_ALL (60)
638 #define SWM_ARG_ID_SS_WINDOW (61)
639 #define SWM_ARG_ID_DONTCENTER (70)
640 #define SWM_ARG_ID_CENTER (71)
641 #define SWM_ARG_ID_KILLWINDOW (80)
642 #define SWM_ARG_ID_DELETEWINDOW (81)
643 #define SWM_ARG_ID_WIDTHGROW (90)
644 #define SWM_ARG_ID_WIDTHSHRINK (91)
645 #define SWM_ARG_ID_HEIGHTGROW (92)
646 #define SWM_ARG_ID_HEIGHTSHRINK (93)
647 #define SWM_ARG_ID_MOVEUP (100)
648 #define SWM_ARG_ID_MOVEDOWN (101)
649 #define SWM_ARG_ID_MOVELEFT (102)
650 #define SWM_ARG_ID_MOVERIGHT (103)
651 #define SWM_ARG_ID_RAISE (105)
652 #define SWM_ARG_ID_LOWER (106)
653 #define SWM_ARG_ID_BAR_TOGGLE (110)
654 #define SWM_ARG_ID_BAR_TOGGLE_WS (111)
660 TAILQ_ENTRY(quirk
) entry
;
661 char *class; /* WM_CLASS:class */
662 char *instance
; /* WM_CLASS:instance */
663 char *name
; /* WM_NAME */
665 regex_t regex_instance
;
668 int ws
; /* Initial workspace. */
669 #define SWM_Q_FLOAT (1<<0) /* float this window */
670 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
671 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
672 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
673 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
674 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
675 #define SWM_Q_NOFOCUSONMAP (1<<6) /* Don't focus on window when mapped. */
676 #define SWM_Q_FOCUSONMAP_SINGLE (1<<7) /* Only focus if single win of type. */
677 #define SWM_Q_OBEYAPPFOCUSREQ (1<<8) /* Focus when applications ask. */
678 #define SWM_Q_IGNOREPID (1<<9) /* Ignore PID when determining ws. */
679 #define SWM_Q_IGNORESPAWNWS (1<<10) /* Ignore _SWM_WS when managing win. */
681 TAILQ_HEAD(quirk_list
, quirk
);
682 struct quirk_list quirks
= TAILQ_HEAD_INITIALIZER(quirks
);
685 * Supported EWMH hints should be added to
686 * both the enum and the ewmh array
692 _NET_CURRENT_DESKTOP
,
693 _NET_DESKTOP_GEOMETRY
,
695 _NET_DESKTOP_VIEWPORT
,
696 _NET_MOVERESIZE_WINDOW
,
697 _NET_NUMBER_OF_DESKTOPS
,
699 _NET_WM_ACTION_ABOVE
,
700 _NET_WM_ACTION_CLOSE
,
701 _NET_WM_ACTION_FULLSCREEN
,
703 _NET_WM_ACTION_RESIZE
,
704 _NET_WM_ALLOWED_ACTIONS
,
706 _NET_WM_FULL_PLACEMENT
,
710 _NET_WM_STATE_FULLSCREEN
,
711 _NET_WM_STATE_HIDDEN
,
712 _NET_WM_STATE_MAXIMIZED_VERT
,
713 _NET_WM_STATE_MAXIMIZED_HORZ
,
714 _NET_WM_STATE_SKIP_PAGER
,
715 _NET_WM_STATE_SKIP_TASKBAR
,
717 _NET_WM_WINDOW_TYPE_DIALOG
,
718 _NET_WM_WINDOW_TYPE_DOCK
,
719 _NET_WM_WINDOW_TYPE_NORMAL
,
720 _NET_WM_WINDOW_TYPE_SPLASH
,
721 _NET_WM_WINDOW_TYPE_TOOLBAR
,
722 _NET_WM_WINDOW_TYPE_UTILITY
,
723 _SWM_WM_STATE_MANUAL
,
730 } ewmh
[SWM_EWMH_HINT_MAX
] = {
731 /* must be in same order as in the enum */
732 {"_NET_ACTIVE_WINDOW", XCB_ATOM_NONE
},
733 {"_NET_CLIENT_LIST", XCB_ATOM_NONE
},
734 {"_NET_CLOSE_WINDOW", XCB_ATOM_NONE
},
735 {"_NET_CURRENT_DESKTOP", XCB_ATOM_NONE
},
736 {"_NET_DESKTOP_GEOMETRY", XCB_ATOM_NONE
},
737 {"_NET_DESKTOP_NAMES", XCB_ATOM_NONE
},
738 {"_NET_DESKTOP_VIEWPORT", XCB_ATOM_NONE
},
739 {"_NET_MOVERESIZE_WINDOW", XCB_ATOM_NONE
},
740 {"_NET_NUMBER_OF_DESKTOPS", XCB_ATOM_NONE
},
741 {"_NET_RESTACK_WINDOW", XCB_ATOM_NONE
},
742 {"_NET_WM_ACTION_ABOVE", XCB_ATOM_NONE
},
743 {"_NET_WM_ACTION_CLOSE", XCB_ATOM_NONE
},
744 {"_NET_WM_ACTION_FULLSCREEN", XCB_ATOM_NONE
},
745 {"_NET_WM_ACTION_MOVE", XCB_ATOM_NONE
},
746 {"_NET_WM_ACTION_RESIZE", XCB_ATOM_NONE
},
747 {"_NET_WM_ALLOWED_ACTIONS", XCB_ATOM_NONE
},
748 {"_NET_WM_DESKTOP", XCB_ATOM_NONE
},
749 {"_NET_WM_FULL_PLACEMENT", XCB_ATOM_NONE
},
750 {"_NET_WM_NAME", XCB_ATOM_NONE
},
751 {"_NET_WM_STATE", XCB_ATOM_NONE
},
752 {"_NET_WM_STATE_ABOVE", XCB_ATOM_NONE
},
753 {"_NET_WM_STATE_FULLSCREEN", XCB_ATOM_NONE
},
754 {"_NET_WM_STATE_HIDDEN", XCB_ATOM_NONE
},
755 {"_NET_WM_STATE_MAXIMIZED_VERT", XCB_ATOM_NONE
},
756 {"_NET_WM_STATE_MAXIMIZED_HORZ", XCB_ATOM_NONE
},
757 {"_NET_WM_STATE_SKIP_PAGER", XCB_ATOM_NONE
},
758 {"_NET_WM_STATE_SKIP_TASKBAR", XCB_ATOM_NONE
},
759 {"_NET_WM_WINDOW_TYPE", XCB_ATOM_NONE
},
760 {"_NET_WM_WINDOW_TYPE_DIALOG", XCB_ATOM_NONE
},
761 {"_NET_WM_WINDOW_TYPE_DOCK", XCB_ATOM_NONE
},
762 {"_NET_WM_WINDOW_TYPE_NORMAL", XCB_ATOM_NONE
},
763 {"_NET_WM_WINDOW_TYPE_SPLASH", XCB_ATOM_NONE
},
764 {"_NET_WM_WINDOW_TYPE_TOOLBAR", XCB_ATOM_NONE
},
765 {"_NET_WM_WINDOW_TYPE_UTILITY", XCB_ATOM_NONE
},
766 {"_SWM_WM_STATE_MANUAL", XCB_ATOM_NONE
},
769 /* EWMH source type */
771 EWMH_SOURCE_TYPE_NONE
= 0,
772 EWMH_SOURCE_TYPE_NORMAL
= 1,
773 EWMH_SOURCE_TYPE_OTHER
= 2,
780 XC_BOTTOM_LEFT_CORNER
,
781 XC_BOTTOM_RIGHT_CORNER
,
789 char *name
; /* Name used by Xcursor .*/
790 uint8_t cf_char
; /* cursorfont index. */
792 } cursors
[XC_MAX
] = {
793 {"fleur", XC_fleur
, XCB_CURSOR_NONE
},
794 {"left_ptr", XC_left_ptr
, XCB_CURSOR_NONE
},
795 {"bottom_left_corner", XC_bottom_left_corner
, XCB_CURSOR_NONE
},
796 {"bottom_right_corner", XC_bottom_right_corner
, XCB_CURSOR_NONE
},
797 {"sizing", XC_sizing
, XCB_CURSOR_NONE
},
798 {"top_left_corner", XC_top_left_corner
, XCB_CURSOR_NONE
},
799 {"top_right_corner", XC_top_right_corner
, XCB_CURSOR_NONE
},
802 #define SWM_SPAWN_OPTIONAL 0x1
806 TAILQ_ENTRY(spawn_prog
) entry
;
812 TAILQ_HEAD(spawn_list
, spawn_prog
);
813 struct spawn_list spawns
= TAILQ_HEAD_INITIALIZER(spawns
);
815 /* user/key callable function IDs */
931 KF_DUMPWINS
, /* MUST BE LAST */
939 enum keyfuncid funcid
;
942 RB_HEAD(key_tree
, key
);
944 /* function prototypes */
945 void adjust_font(struct ws_win
*);
946 char *argsep(char **);
947 void bar_cleanup(struct swm_region
*);
948 void bar_extra_setup(void);
949 void bar_extra_stop(void);
950 int bar_extra_update(void);
951 void bar_fmt(const char *, char *, struct swm_region
*, size_t);
952 void bar_fmt_expand(char *, size_t);
954 void bar_print(struct swm_region
*, const char *);
955 void bar_print_legacy(struct swm_region
*, const char *);
956 void bar_replace(char *, char *, struct swm_region
*, size_t);
957 void bar_replace_pad(char *, int *, size_t);
958 char *bar_replace_seq(char *, char *, struct swm_region
*, size_t *, size_t);
959 void bar_setup(struct swm_region
*);
960 void bar_toggle(struct swm_region
*, union arg
*);
961 void bar_urgent(char *, size_t);
962 void bar_window_class(char *, size_t, struct swm_region
*);
963 void bar_window_class_instance(char *, size_t, struct swm_region
*);
964 void bar_window_float(char *, size_t, struct swm_region
*);
965 void bar_window_instance(char *, size_t, struct swm_region
*);
966 void bar_window_name(char *, size_t, struct swm_region
*);
967 void bar_window_state(char *, size_t, struct swm_region
*);
968 void bar_workspace_name(char *, size_t, struct swm_region
*);
969 void buttonpress(xcb_button_press_event_t
*);
970 void center_pointer(struct swm_region
*);
971 void check_conn(void);
972 void clear_keys(void);
973 int clear_maximized(struct workspace
*);
974 void clientmessage(xcb_client_message_event_t
*);
975 void client_msg(struct ws_win
*, xcb_atom_t
, xcb_timestamp_t
);
976 int conf_load(const char *, int);
977 void configurenotify(xcb_configure_notify_event_t
*);
978 void configurerequest(xcb_configure_request_event_t
*);
979 void config_win(struct ws_win
*, xcb_configure_request_event_t
*);
980 void constrain_window(struct ws_win
*, struct swm_geometry
*, int *);
981 int count_win(struct workspace
*, bool);
982 void cursors_cleanup(void);
983 void cursors_load(void);
984 void custom_region(const char *);
985 void cyclerg(struct swm_region
*, union arg
*);
986 void cyclews(struct swm_region
*, union arg
*);
987 void cycle_layout(struct swm_region
*, union arg
*);
988 void destroynotify(xcb_destroy_notify_event_t
*);
989 void dumpwins(struct swm_region
*, union arg
*);
991 void enternotify(xcb_enter_notify_event_t
*);
992 void event_drain(uint8_t);
993 void event_error(xcb_generic_error_t
*);
994 void event_handle(xcb_generic_event_t
*);
995 void ewmh_apply_flags(struct ws_win
*, uint32_t);
996 void ewmh_autoquirk(struct ws_win
*);
997 void ewmh_get_desktop_names(void);
998 void ewmh_get_wm_state(struct ws_win
*);
999 void ewmh_update_actions(struct ws_win
*);
1000 void ewmh_update_client_list(void);
1001 void ewmh_update_current_desktop(void);
1002 void ewmh_update_desktop_names(void);
1003 void ewmh_update_desktops(void);
1004 void ewmh_change_wm_state(struct ws_win
*, xcb_atom_t
, long);
1005 void ewmh_update_wm_state(struct ws_win
*);
1006 char *expand_tilde(const char *);
1007 void expose(xcb_expose_event_t
*);
1008 void fake_keypress(struct ws_win
*, xcb_keysym_t
, uint16_t);
1009 struct pid_e
*find_pid(pid_t
);
1010 struct ws_win
*find_unmanaged_window(xcb_window_t
);
1011 struct ws_win
*find_window(xcb_window_t
);
1012 void floating_toggle(struct swm_region
*, union arg
*);
1013 void focus(struct swm_region
*, union arg
*);
1015 void focusin(xcb_focus_in_event_t
*);
1016 void focusout(xcb_focus_out_event_t
*);
1018 void focus_flush(void);
1019 void focus_region(struct swm_region
*);
1020 void focusrg(struct swm_region
*, union arg
*);
1021 void focus_win(struct ws_win
*);
1022 void fontset_init(void);
1023 void free_window(struct ws_win
*);
1024 xcb_atom_t
get_atom_from_string(const char *);
1026 char *get_atom_name(xcb_atom_t
);
1028 struct ws_win
*get_focus_magic(struct ws_win
*);
1029 struct ws_win
*get_focus_prev(struct ws_win
*);
1031 char *get_notify_detail_label(uint8_t);
1032 char *get_notify_mode_label(uint8_t);
1034 struct ws_win
*get_pointer_win(xcb_window_t
);
1035 struct ws_win
*get_region_focus(struct swm_region
*);
1036 int get_region_index(struct swm_region
*);
1037 xcb_screen_t
*get_screen(int);
1038 int get_screen_count(void);
1040 char *get_source_type_label(uint32_t);
1041 char *get_stack_mode_name(uint8_t);
1043 int32_t get_swm_ws(xcb_window_t
);
1044 bool get_urgent(struct ws_win
*);
1045 char *get_win_name(xcb_window_t
);
1046 uint8_t get_win_state(xcb_window_t
);
1047 void get_wm_protocols(struct ws_win
*);
1048 int get_ws_idx(struct ws_win
*);
1049 void grabbuttons(struct ws_win
*);
1050 void grabkeys(void);
1051 void grab_windows(void);
1052 void iconify(struct swm_region
*, union arg
*);
1053 bool isxlfd(char *);
1054 void keypress(xcb_key_press_event_t
*);
1055 int key_cmp(struct key
*, struct key
*);
1056 void key_insert(unsigned int, KeySym
, enum keyfuncid
, const char *);
1057 struct key
*key_lookup(unsigned int, KeySym
);
1058 void key_remove(struct key
*);
1059 void key_replace(struct key
*, unsigned int, KeySym
, enum keyfuncid
,
1061 void kill_bar_extra_atexit(void);
1062 void kill_refs(struct ws_win
*);
1064 void leavenotify(xcb_leave_notify_event_t
*);
1066 void load_float_geom(struct ws_win
*);
1067 void lower_window(struct ws_win
*);
1068 struct ws_win
*manage_window(xcb_window_t
, int, bool);
1069 void map_window(struct ws_win
*);
1070 void mapnotify(xcb_map_notify_event_t
*);
1071 void mappingnotify(xcb_mapping_notify_event_t
*);
1072 void maprequest(xcb_map_request_event_t
*);
1073 void maximize_toggle(struct swm_region
*, union arg
*);
1074 void motionnotify(xcb_motion_notify_event_t
*);
1075 void move(struct ws_win
*, union arg
*);
1076 void move_step(struct swm_region
*, union arg
*);
1077 uint32_t name_to_pixel(int, const char *);
1078 void name_workspace(struct swm_region
*, union arg
*);
1079 void new_region(struct swm_screen
*, int, int, int, int);
1080 int parsekeys(const char *, unsigned int, unsigned int *, KeySym
*);
1081 int parsequirks(const char *, uint32_t *, int *);
1082 int parse_rgb(const char *, uint16_t *, uint16_t *, uint16_t *);
1083 void pressbutton(struct swm_region
*, union arg
*);
1084 void priorws(struct swm_region
*, union arg
*);
1086 void print_win_geom(xcb_window_t
);
1088 void propertynotify(xcb_property_notify_event_t
*);
1089 void quirk_free(struct quirk
*);
1090 void quirk_insert(const char *, const char *, const char *, uint32_t, int);
1091 void quirk_remove(struct quirk
*);
1092 void quirk_replace(struct quirk
*, const char *, const char *, const char *,
1094 void quit(struct swm_region
*, union arg
*);
1095 void raise_toggle(struct swm_region
*, union arg
*);
1096 void raise_window(struct ws_win
*);
1097 void region_containment(struct ws_win
*, struct swm_region
*, int);
1098 struct swm_region
*region_under(struct swm_screen
*, int, int);
1099 void regionize(struct ws_win
*, int, int);
1100 void resize(struct ws_win
*, union arg
*);
1101 void resize_step(struct swm_region
*, union arg
*);
1102 void restart(struct swm_region
*, union arg
*);
1103 struct swm_region
*root_to_region(xcb_window_t
, int);
1104 void screenchange(xcb_randr_screen_change_notify_event_t
*);
1105 void scan_randr(int);
1106 void search_do_resp(void);
1107 void search_resp_name_workspace(const char *, size_t);
1108 void search_resp_search_window(const char *);
1109 void search_resp_search_workspace(const char *);
1110 void search_resp_uniconify(const char *, size_t);
1111 void search_win(struct swm_region
*, union arg
*);
1112 void search_win_cleanup(void);
1113 void search_workspace(struct swm_region
*, union arg
*);
1114 void send_to_rg(struct swm_region
*, union arg
*);
1115 void send_to_ws(struct swm_region
*, union arg
*);
1116 void set_region(struct swm_region
*);
1117 int setautorun(const char *, const char *, int);
1118 int setconfbinding(const char *, const char *, int);
1119 int setconfcolor(const char *, const char *, int);
1120 int setconfmodkey(const char *, const char *, int);
1121 int setconfquirk(const char *, const char *, int);
1122 int setconfregion(const char *, const char *, int);
1123 int setconfspawn(const char *, const char *, int);
1124 int setconfvalue(const char *, const char *, int);
1125 void setkeybinding(unsigned int, KeySym
, enum keyfuncid
, const char *);
1126 int setkeymapping(const char *, const char *, int);
1127 int setlayout(const char *, const char *, int);
1128 void setquirk(const char *, const char *, const char *, uint32_t, int);
1129 void setscreencolor(const char *, int, int);
1130 void setspawn(const char *, const char *, int);
1131 void setup_ewmh(void);
1132 void setup_globals(void);
1133 void setup_keys(void);
1134 void setup_quirks(void);
1135 void setup_screens(void);
1136 void setup_spawn(void);
1137 void set_child_transient(struct ws_win
*, xcb_window_t
*);
1138 void set_win_state(struct ws_win
*, uint8_t);
1139 void shutdown_cleanup(void);
1141 void socket_setnonblock(int);
1142 void sort_windows(struct ws_win_list
*);
1143 void spawn(int, union arg
*, bool);
1144 void spawn_custom(struct swm_region
*, union arg
*, const char *);
1145 int spawn_expand(struct swm_region
*, union arg
*, const char *, char ***);
1146 void spawn_insert(const char *, const char *, int);
1147 struct spawn_prog
*spawn_find(const char *);
1148 void spawn_remove(struct spawn_prog
*);
1149 void spawn_replace(struct spawn_prog
*, const char *, const char *, int);
1150 void spawn_select(struct swm_region
*, union arg
*, const char *, int *);
1151 void stack_config(struct swm_region
*, union arg
*);
1152 void stack_master(struct workspace
*, struct swm_geometry
*, int, bool);
1153 void store_float_geom(struct ws_win
*);
1154 char *strdupsafe(const char *);
1155 void swapwin(struct swm_region
*, union arg
*);
1156 void switchws(struct swm_region
*, union arg
*);
1157 void teardown_ewmh(void);
1158 void unescape_selector(char *);
1159 void unfocus_win(struct ws_win
*);
1160 void uniconify(struct swm_region
*, union arg
*);
1161 void unmanage_window(struct ws_win
*);
1162 void unmapnotify(xcb_unmap_notify_event_t
*);
1163 void unmap_all(void);
1164 void unmap_window(struct ws_win
*);
1165 void updatenumlockmask(void);
1166 void update_floater(struct ws_win
*);
1167 void update_modkey(unsigned int);
1168 void update_win_stacking(struct ws_win
*);
1169 void update_window(struct ws_win
*);
1170 void update_window_color(struct ws_win
*);
1171 void update_wm_state(struct ws_win
*win
);
1172 void validate_spawns(void);
1173 int validate_win(struct ws_win
*);
1174 int validate_ws(struct workspace
*);
1175 void version(struct swm_region
*, union arg
*);
1176 void win_to_ws(struct ws_win
*, int, bool);
1177 pid_t
window_get_pid(xcb_window_t
);
1178 void wkill(struct swm_region
*, union arg
*);
1179 void update_ws_stack(struct workspace
*);
1180 void xft_init(struct swm_region
*);
1181 void _add_startup_exception(const char *, va_list);
1182 void add_startup_exception(const char *, ...);
1184 RB_PROTOTYPE(key_tree
, key
, entry
, key_cmp
);
1185 RB_GENERATE(key_tree
, key
, entry
, key_cmp
);
1186 struct key_tree keys
;
1191 xcb_font_t cf
= XCB_NONE
;
1194 for (i
= 0; i
< LENGTH(cursors
); ++i
) {
1195 /* try to load Xcursor first. */
1196 cursors
[i
].cid
= XcursorLibraryLoadCursor(display
,
1199 /* fallback to cursorfont. */
1200 if (cursors
[i
].cid
== XCB_CURSOR_NONE
) {
1201 if (cf
== XCB_NONE
) {
1202 cf
= xcb_generate_id(conn
);
1203 xcb_open_font(conn
, cf
, strlen("cursor"),
1207 cursors
[i
].cid
= xcb_generate_id(conn
);
1208 xcb_create_glyph_cursor(conn
, cursors
[i
].cid
, cf
, cf
,
1209 cursors
[i
].cf_char
, cursors
[i
].cf_char
+ 1, 0, 0, 0,
1210 0xffff, 0xffff, 0xffff);
1216 xcb_close_font(conn
, cf
);
1220 cursors_cleanup(void)
1223 for (i
= 0; i
< LENGTH(cursors
); ++i
)
1224 xcb_free_cursor(conn
, cursors
[i
].cid
);
1228 expand_tilde(const char *s
)
1230 struct passwd
*ppwd
;
1238 errx(1, "expand_tilde: NULL string.");
1241 result
= strdup(sc
);
1247 if ((max
= sysconf(_SC_LOGIN_NAME_MAX
)) == -1)
1248 errx(1, "expand_tilde: sysconf");
1250 if ((user
= calloc(1, max
+ 1)) == NULL
)
1251 errx(1, "expand_tilde: calloc");
1253 for (i
= 0; s
[i
] != '/' && s
[i
] != '\0'; ++i
)
1258 ppwd
= strlen(user
) == 0 ? getpwuid(getuid()) : getpwnam(user
);
1262 result
= strdup(sc
);
1264 if (asprintf(&result
, "%s%s", ppwd
->pw_dir
, s
) == -1)
1268 errx(1, "expand_tilde: failed to allocate memory.");
1274 parse_rgb(const char *rgb
, uint16_t *rr
, uint16_t *gg
, uint16_t *bb
)
1276 unsigned int tmpr
, tmpg
, tmpb
;
1278 if (sscanf(rgb
, "rgb:%x/%x/%x", &tmpr
, &tmpg
, &tmpb
) != 3)
1281 *rr
= RGB_8_TO_16(tmpr
);
1282 *gg
= RGB_8_TO_16(tmpg
);
1283 *bb
= RGB_8_TO_16(tmpb
);
1289 get_screen(int screen
)
1291 const xcb_setup_t
*r
;
1292 xcb_screen_iterator_t iter
;
1294 if ((r
= xcb_get_setup(conn
)) == NULL
) {
1295 DNPRINTF(SWM_D_MISC
, "get_screen: xcb_get_setup\n");
1299 iter
= xcb_setup_roots_iterator(r
);
1300 for (; iter
.rem
; --screen
, xcb_screen_next(&iter
))
1308 get_screen_count(void)
1310 const xcb_setup_t
*r
;
1312 if ((r
= xcb_get_setup(conn
)) == NULL
) {
1313 DNPRINTF(SWM_D_MISC
, "get_screen_count: xcb_get_setup\n");
1317 return xcb_setup_roots_length(r
);
1321 get_region_index(struct swm_region
*r
)
1323 struct swm_region
*rr
;
1329 TAILQ_FOREACH(rr
, &r
->s
->rl
, entry
) {
1344 if (focus_mode
== SWM_FOCUS_DEFAULT
)
1345 event_drain(XCB_ENTER_NOTIFY
);
1351 get_atom_from_string(const char *str
)
1353 xcb_intern_atom_cookie_t c
;
1354 xcb_intern_atom_reply_t
*r
;
1357 c
= xcb_intern_atom(conn
, 0, strlen(str
), str
);
1358 r
= xcb_intern_atom_reply(conn
, c
, NULL
);
1366 return (XCB_ATOM_NONE
);
1370 get_wm_protocols(struct ws_win
*win
) {
1372 xcb_icccm_get_wm_protocols_reply_t wpr
;
1374 if (xcb_icccm_get_wm_protocols_reply(conn
,
1375 xcb_icccm_get_wm_protocols(conn
, win
->id
, a_prot
),
1377 for (i
= 0; i
< (int)wpr
.atoms_len
; i
++) {
1378 if (wpr
.atoms
[i
] == a_takefocus
)
1379 win
->take_focus
= true;
1380 if (wpr
.atoms
[i
] == a_delete
)
1381 win
->can_delete
= true;
1383 xcb_icccm_get_wm_protocols_reply_wipe(&wpr
);
1390 xcb_window_t root
, win
;
1391 int i
, j
, num_screens
;
1393 for (i
= 0; i
< LENGTH(ewmh
); i
++)
1394 ewmh
[i
].atom
= get_atom_from_string(ewmh
[i
].name
);
1396 num_screens
= get_screen_count();
1397 for (i
= 0; i
< num_screens
; i
++) {
1398 root
= screens
[i
].root
;
1400 /* Set up _NET_SUPPORTING_WM_CHECK. */
1401 win
= xcb_generate_id(conn
);
1402 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, win
, root
,
1403 0, 0, 1, 1, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
1404 XCB_COPY_FROM_PARENT
, 0, NULL
);
1406 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, root
,
1407 a_net_wm_check
, XCB_ATOM_WINDOW
, 32, 1, &win
);
1408 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
1409 a_net_wm_check
, XCB_ATOM_WINDOW
, 32, 1, &win
);
1412 * Impersonate LG3D non-reparenting WM, written by Sun, to
1413 * workaround a Java GUI rendering issue.
1415 if (java_workaround
)
1416 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
1417 ewmh
[_NET_WM_NAME
].atom
, a_utf8_string
,
1418 8, strlen("LG3D"), "LG3D");
1420 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
1421 ewmh
[_NET_WM_NAME
].atom
, a_utf8_string
,
1422 8, strlen("spectrwm"), "spectrwm");
1424 /* Report supported atoms */
1425 xcb_delete_property(conn
, root
, a_net_supported
);
1426 for (j
= 0; j
< LENGTH(ewmh
); j
++)
1427 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, root
,
1428 a_net_supported
, XCB_ATOM_ATOM
, 32, 1,
1433 ewmh_update_desktops();
1434 ewmh_get_desktop_names();
1442 xcb_get_property_cookie_t pc
;
1443 xcb_get_property_reply_t
*pr
;
1445 num_screens
= get_screen_count();
1447 for (i
= 0; i
< num_screens
; i
++) {
1448 /* Get the support check window and destroy it */
1449 pc
= xcb_get_property(conn
, 0, screens
[i
].root
, a_net_wm_check
,
1450 XCB_ATOM_WINDOW
, 0, 1);
1451 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
1454 if (pr
->format
== a_net_wm_check
) {
1455 id
= *((xcb_window_t
*)xcb_get_property_value(pr
));
1457 xcb_destroy_window(conn
, id
);
1458 xcb_delete_property(conn
, screens
[i
].root
,
1460 xcb_delete_property(conn
, screens
[i
].root
,
1468 ewmh_autoquirk(struct ws_win
*win
)
1470 xcb_get_property_reply_t
*r
;
1471 xcb_get_property_cookie_t c
;
1475 c
= xcb_get_property(conn
, 0, win
->id
,
1476 ewmh
[_NET_WM_WINDOW_TYPE
].atom
, XCB_ATOM_ATOM
, 0, UINT32_MAX
);
1477 r
= xcb_get_property_reply(conn
, c
, NULL
);
1481 type
= xcb_get_property_value(r
);
1482 n
= xcb_get_property_value_length(r
) / sizeof(xcb_atom_t
);
1484 for (i
= 0; i
< n
; i
++) {
1485 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_NORMAL
].atom
)
1487 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_DOCK
].atom
||
1488 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_TOOLBAR
].atom
||
1489 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_UTILITY
].atom
) {
1490 win
->quirks
= SWM_Q_FLOAT
| SWM_Q_ANYWHERE
;
1493 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_SPLASH
].atom
||
1494 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_DIALOG
].atom
) {
1495 win
->quirks
= SWM_Q_FLOAT
;
1503 ewmh_update_actions(struct ws_win
*win
)
1505 xcb_atom_t actions
[SWM_EWMH_ACTION_COUNT_MAX
];
1511 actions
[n
++] = ewmh
[_NET_WM_ACTION_CLOSE
].atom
;
1514 actions
[n
++] = ewmh
[_NET_WM_ACTION_MOVE
].atom
;
1515 actions
[n
++] = ewmh
[_NET_WM_ACTION_RESIZE
].atom
;
1516 actions
[n
++] = ewmh
[_NET_WM_ACTION_ABOVE
].atom
;
1519 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
1520 ewmh
[_NET_WM_ALLOWED_ACTIONS
].atom
, XCB_ATOM_ATOM
, 32, 1, actions
);
1523 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
1524 #define _NET_WM_STATE_ADD 1 /* add/set property */
1525 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
1528 ewmh_change_wm_state(struct ws_win
*win
, xcb_atom_t state
, long action
)
1535 name
= get_atom_name(state
);
1536 DNPRINTF(SWM_D_PROP
, "ewmh_change_wm_state: win %#x, state: %s, "
1537 "action: %ld\n", WINID(win
), name
, action
);
1543 if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
)
1544 flag
= EWMH_F_FULLSCREEN
;
1545 else if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
)
1546 flag
= EWMH_F_ABOVE
;
1547 else if (state
== ewmh
[_NET_WM_STATE_HIDDEN
].atom
)
1548 flag
= EWMH_F_HIDDEN
;
1549 else if (state
== ewmh
[_NET_WM_STATE_MAXIMIZED_VERT
].atom
||
1550 state
== ewmh
[_NET_WM_STATE_MAXIMIZED_HORZ
].atom
)
1551 flag
= EWMH_F_MAXIMIZED
;
1552 else if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
)
1553 flag
= SWM_F_MANUAL
;
1554 else if (state
== ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
)
1555 flag
= EWMH_F_SKIP_PAGER
;
1556 else if (state
== ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
)
1557 flag
= EWMH_F_SKIP_TASKBAR
;
1559 /* Disallow unfloating transients. */
1560 if (TRANS(win
) && flag
== EWMH_F_ABOVE
)
1563 new_flags
= win
->ewmh_flags
;
1566 case _NET_WM_STATE_REMOVE
:
1569 case _NET_WM_STATE_ADD
:
1572 case _NET_WM_STATE_TOGGLE
:
1577 ewmh_apply_flags(win
, new_flags
);
1580 DNPRINTF(SWM_D_PROP
, "ewmh_change_wm_state: done.\n");
1584 ewmh_apply_flags(struct ws_win
*win
, uint32_t pending
)
1586 struct workspace
*ws
;
1589 changed
= win
->ewmh_flags
^ pending
;
1593 DNPRINTF(SWM_D_PROP
, "ewmh_apply_flags: pending: %d\n", pending
);
1595 win
->ewmh_flags
= pending
;
1598 if (changed
& EWMH_F_HIDDEN
) {
1600 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
1601 ws
->focus_pending
= get_focus_prev(win
);
1606 /* Reload floating geometry in case region changed. */
1608 load_float_geom(win
);
1610 /* The window is no longer iconic, prepare focus. */
1611 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
1612 ws
->focus_pending
= get_focus_magic(win
);
1617 if (changed
& EWMH_F_ABOVE
) {
1618 if (ws
->cur_layout
!= &layouts
[SWM_MAX_STACK
]) {
1620 load_float_geom(win
);
1621 else if (!MAXIMIZED(win
))
1622 store_float_geom(win
);
1624 win
->ewmh_flags
&= ~EWMH_F_MAXIMIZED
;
1625 changed
&= ~EWMH_F_MAXIMIZED
;
1629 win
->ewmh_flags
^= EWMH_F_ABOVE
& pending
;
1633 if (changed
& EWMH_F_MAXIMIZED
) {
1634 /* VERT and/or HORZ changed. */
1636 if (!MAXIMIZED(win
))
1637 load_float_geom(win
);
1639 store_float_geom(win
);
1642 if (MAXIMIZED(win
)) {
1643 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
1644 ws
->cur_layout
!= &layouts
[SWM_MAX_STACK
]) {
1648 ws
->focus_pending
= win
;
1652 update_window_color(win
);
1656 if (changed
& EWMH_F_FULLSCREEN
) {
1657 if (FULLSCREEN(win
)) {
1658 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
1662 ws
->focus_pending
= win
;
1665 load_float_geom(win
);
1668 win
->ewmh_flags
&= ~EWMH_F_MAXIMIZED
;
1672 DNPRINTF(SWM_D_PROP
, "ewmh_apply_flags: done.\n");
1676 ewmh_update_wm_state(struct ws_win
*win
) {
1677 xcb_atom_t vals
[SWM_EWMH_ACTION_COUNT_MAX
];
1681 vals
[n
++] = ewmh
[_NET_WM_STATE_HIDDEN
].atom
;
1682 if (FULLSCREEN(win
))
1683 vals
[n
++] = ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
;
1684 if (MAXIMIZED_VERT(win
))
1685 vals
[n
++] = ewmh
[_NET_WM_STATE_MAXIMIZED_VERT
].atom
;
1686 if (MAXIMIZED_HORZ(win
))
1687 vals
[n
++] = ewmh
[_NET_WM_STATE_MAXIMIZED_HORZ
].atom
;
1688 if (win
->ewmh_flags
& EWMH_F_SKIP_PAGER
)
1689 vals
[n
++] = ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
;
1690 if (win
->ewmh_flags
& EWMH_F_SKIP_TASKBAR
)
1691 vals
[n
++] = ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
;
1692 if (win
->ewmh_flags
& EWMH_F_ABOVE
)
1693 vals
[n
++] = ewmh
[_NET_WM_STATE_ABOVE
].atom
;
1694 if (win
->ewmh_flags
& SWM_F_MANUAL
)
1695 vals
[n
++] = ewmh
[_SWM_WM_STATE_MANUAL
].atom
;
1698 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
1699 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, n
, vals
);
1701 xcb_delete_property(conn
, win
->id
, ewmh
[_NET_WM_STATE
].atom
);
1705 ewmh_get_wm_state(struct ws_win
*win
)
1708 xcb_get_property_cookie_t c
;
1709 xcb_get_property_reply_t
*r
;
1715 win
->ewmh_flags
= 0;
1717 c
= xcb_get_property(conn
, 0, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
1718 XCB_ATOM_ATOM
, 0, UINT32_MAX
);
1719 r
= xcb_get_property_reply(conn
, c
, NULL
);
1723 states
= xcb_get_property_value(r
);
1724 n
= xcb_get_property_value_length(r
) / sizeof(xcb_atom_t
);
1726 for (i
= 0; i
< n
; i
++)
1727 ewmh_change_wm_state(win
, states
[i
], _NET_WM_STATE_ADD
);
1735 dumpwins(struct swm_region
*r
, union arg
*args
)
1739 xcb_get_window_attributes_cookie_t c
;
1740 xcb_get_window_attributes_reply_t
*wa
;
1742 /* suppress unused warning since var is needed */
1745 if (r
->ws
== NULL
) {
1746 DPRINTF("dumpwins: invalid workspace\n");
1750 DPRINTF("=== managed window list ws %02d ===\n", r
->ws
->idx
);
1751 TAILQ_FOREACH(w
, &r
->ws
->winlist
, entry
) {
1752 state
= get_win_state(w
->id
);
1753 c
= xcb_get_window_attributes(conn
, w
->id
);
1754 wa
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
1756 DPRINTF("win %#x, map_state: %d, state: %u, "
1757 "transient: %#x\n", w
->id
, wa
->map_state
,
1758 state
, w
->transient
);
1761 DPRINTF("win %#x, failed xcb_get_window_attributes\n",
1765 DPRINTF("=== stacking order (top down) === \n");
1766 TAILQ_FOREACH(w
, &r
->ws
->stack
, stack_entry
) {
1767 DPRINTF("win %#x, fs: %s, maximized: %s, above: %s, "
1768 "iconic: %s\n", w
->id
, YESNO(FULLSCREEN(w
)),
1769 YESNO(MAXIMIZED(w
)), YESNO(ABOVE(w
)), YESNO(ICONIC(w
)));
1772 DPRINTF("===== unmanaged window list =====\n");
1773 TAILQ_FOREACH(w
, &r
->ws
->unmanagedlist
, entry
) {
1774 state
= get_win_state(w
->id
);
1775 c
= xcb_get_window_attributes(conn
, w
->id
);
1776 wa
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
1778 DPRINTF("win %#x, map_state: %d, state: %u, "
1779 "transient: %#x\n", w
->id
, wa
->map_state
,
1780 state
, w
->transient
);
1783 DPRINTF("win %#x, failed xcb_get_window_attributes\n",
1787 DPRINTF("=================================\n");
1791 dumpwins(struct swm_region
*r
, union arg
*s
)
1796 #endif /* SWM_DEBUG */
1801 int saved_errno
, status
;
1804 saved_errno
= errno
;
1808 while ((pid
= waitpid(WAIT_ANY
, &status
, WNOHANG
)) != 0) {
1813 if (errno
!= ECHILD
)
1814 warn("sighdlr: waitpid");
1815 #endif /* SWM_DEBUG */
1818 if (pid
== searchpid
)
1822 if (WIFEXITED(status
)) {
1823 if (WEXITSTATUS(status
) != 0)
1824 warnx("sighdlr: child exit status: %d",
1825 WEXITSTATUS(status
));
1827 warnx("sighdlr: child is terminated "
1829 #endif /* SWM_DEBUG */
1843 errno
= saved_errno
;
1849 struct pid_e
*p
= NULL
;
1851 DNPRINTF(SWM_D_MISC
, "find_pid: %d\n", pid
);
1856 TAILQ_FOREACH(p
, &pidlist
, entry
) {
1865 name_to_pixel(int sidx
, const char *colorname
)
1867 uint32_t result
= 0;
1868 char cname
[32] = "#";
1869 xcb_screen_t
*screen
;
1870 xcb_colormap_t cmap
;
1871 xcb_alloc_color_reply_t
*cr
;
1872 xcb_alloc_named_color_reply_t
*nr
;
1873 uint16_t rr
, gg
, bb
;
1875 screen
= get_screen(sidx
);
1876 cmap
= screen
->default_colormap
;
1878 /* color is in format rgb://rr/gg/bb */
1879 if (strncmp(colorname
, "rgb:", 4) == 0) {
1880 if (parse_rgb(colorname
, &rr
, &gg
, &bb
) == -1)
1881 warnx("could not parse rgb %s", colorname
);
1883 cr
= xcb_alloc_color_reply(conn
,
1884 xcb_alloc_color(conn
, cmap
, rr
, gg
, bb
),
1890 warnx("color '%s' not found", colorname
);
1893 nr
= xcb_alloc_named_color_reply(conn
,
1894 xcb_alloc_named_color(conn
, cmap
, strlen(colorname
),
1897 strlcat(cname
, colorname
+ 2, sizeof cname
- 1);
1898 nr
= xcb_alloc_named_color_reply(conn
,
1899 xcb_alloc_named_color(conn
, cmap
, strlen(cname
),
1906 warnx("color '%s' not found", colorname
);
1913 setscreencolor(const char *val
, int i
, int c
)
1915 if (i
< 0 || i
>= get_screen_count())
1918 screens
[i
].c
[c
].pixel
= name_to_pixel(i
, val
);
1919 free(screens
[i
].c
[c
].name
);
1920 if ((screens
[i
].c
[c
].name
= strdup(val
)) == NULL
)
1925 fancy_stacker(struct workspace
*ws
)
1927 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1928 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1929 snprintf(ws
->stacker
, sizeof ws
->stacker
,
1930 ws
->l_state
.vertical_flip
? "[%d>%d]" : "[%d|%d]",
1931 ws
->l_state
.vertical_mwin
, ws
->l_state
.vertical_stacks
);
1932 else if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1933 snprintf(ws
->stacker
, sizeof ws
->stacker
,
1934 ws
->l_state
.horizontal_flip
? "[%dv%d]" : "[%d-%d]",
1935 ws
->l_state
.horizontal_mwin
, ws
->l_state
.horizontal_stacks
);
1939 plain_stacker(struct workspace
*ws
)
1941 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1942 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1943 strlcpy(ws
->stacker
, ws
->l_state
.vertical_flip
? "[>]" : "[|]",
1944 sizeof ws
->stacker
);
1945 else if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1946 strlcpy(ws
->stacker
, ws
->l_state
.horizontal_flip
? "[v]" : "[-]",
1947 sizeof ws
->stacker
);
1951 custom_region(const char *val
)
1953 unsigned int x
, y
, w
, h
;
1954 int sidx
, num_screens
;
1955 xcb_screen_t
*screen
;
1957 num_screens
= get_screen_count();
1958 if (sscanf(val
, "screen[%d]:%ux%u+%u+%u", &sidx
, &w
, &h
, &x
, &y
) != 5)
1959 errx(1, "invalid custom region, "
1960 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1961 if (sidx
< 1 || sidx
> num_screens
)
1962 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1966 if ((screen
= get_screen(sidx
)) == NULL
)
1967 errx(1, "ERROR: can't get screen %d.", sidx
);
1970 errx(1, "region %ux%u+%u+%u too small", w
, h
, x
, y
);
1972 if (x
> screen
->width_in_pixels
||
1973 y
> screen
->height_in_pixels
||
1974 w
+ x
> screen
->width_in_pixels
||
1975 h
+ y
> screen
->height_in_pixels
) {
1976 warnx("ignoring region %ux%u+%u+%u - not within screen "
1977 "boundaries (%ux%u)", w
, h
, x
, y
,
1978 screen
->width_in_pixels
, screen
->height_in_pixels
);
1982 new_region(&screens
[sidx
], x
, y
, w
, h
);
1986 socket_setnonblock(int fd
)
1990 if ((flags
= fcntl(fd
, F_GETFL
, 0)) == -1)
1991 err(1, "fcntl F_GETFL");
1992 flags
|= O_NONBLOCK
;
1993 if ((flags
= fcntl(fd
, F_SETFL
, flags
)) == -1)
1994 err(1, "fcntl F_SETFL");
1998 bar_print_legacy(struct swm_region
*r
, const char *s
)
2000 xcb_rectangle_t rect
;
2005 XRectangle ibox
, lbox
;
2009 XmbTextExtents(bar_fs
, s
, len
, &ibox
, &lbox
);
2011 switch (bar_justify
) {
2012 case SWM_BAR_JUSTIFY_LEFT
:
2015 case SWM_BAR_JUSTIFY_CENTER
:
2016 x
= (WIDTH(r
) - lbox
.width
) / 2;
2018 case SWM_BAR_JUSTIFY_RIGHT
:
2019 x
= WIDTH(r
) - lbox
.width
- SWM_BAR_OFFSET
;
2023 if (x
< SWM_BAR_OFFSET
)
2028 rect
.width
= WIDTH(r
->bar
);
2029 rect
.height
= HEIGHT(r
->bar
);
2031 /* clear back buffer */
2032 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
2033 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
2034 xcb_poly_fill_rectangle(conn
, r
->bar
->buffer
, r
->s
->bar_gc
, 1, &rect
);
2036 /* draw back buffer */
2037 gcvd
.graphics_exposures
= 0;
2038 draw
= XCreateGC(display
, r
->bar
->buffer
, GCGraphicsExposures
, &gcvd
);
2039 XSetForeground(display
, draw
, r
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
);
2040 DRAWSTRING(display
, r
->bar
->buffer
, bar_fs
, draw
,
2041 x
, (bar_fs_extents
->max_logical_extent
.height
- lbox
.height
) / 2 -
2043 XFreeGC(display
, draw
);
2046 xcb_copy_area(conn
, r
->bar
->buffer
, r
->bar
->id
, r
->s
->bar_gc
, 0, 0,
2047 0, 0, WIDTH(r
->bar
), HEIGHT(r
->bar
));
2051 bar_print(struct swm_region
*r
, const char *s
)
2054 xcb_rectangle_t rect
;
2062 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
, &info
);
2064 switch (bar_justify
) {
2065 case SWM_BAR_JUSTIFY_LEFT
:
2068 case SWM_BAR_JUSTIFY_CENTER
:
2069 x
= (WIDTH(r
) - info
.width
) / 2;
2071 case SWM_BAR_JUSTIFY_RIGHT
:
2072 x
= WIDTH(r
) - info
.width
- SWM_BAR_OFFSET
;
2076 if (x
< SWM_BAR_OFFSET
)
2081 rect
.width
= WIDTH(r
->bar
);
2082 rect
.height
= HEIGHT(r
->bar
);
2084 /* clear back buffer */
2085 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
2086 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
2087 xcb_poly_fill_rectangle(conn
, r
->bar
->buffer
, r
->s
->bar_gc
, 1, &rect
);
2089 /* draw back buffer */
2090 draw
= XftDrawCreate(display
, r
->bar
->buffer
,
2091 DefaultVisual(display
, r
->s
->idx
),
2092 DefaultColormap(display
, r
->s
->idx
));
2094 XftDrawStringUtf8(draw
, &bar_font_color
, bar_font
, x
,
2095 (HEIGHT(r
->bar
) + bar_font
->height
) / 2 - bar_font
->descent
,
2098 XftDrawDestroy(draw
);
2101 xcb_copy_area(conn
, r
->bar
->buffer
, r
->bar
->id
, r
->s
->bar_gc
, 0, 0,
2102 0, 0, WIDTH(r
->bar
), HEIGHT(r
->bar
));
2106 bar_extra_stop(void)
2110 bzero(bar_pipe
, sizeof bar_pipe
);
2113 kill(bar_pid
, SIGTERM
);
2116 strlcpy(bar_ext
, "", sizeof bar_ext
);
2121 bar_window_class(char *s
, size_t sz
, struct swm_region
*r
)
2123 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2125 if (r
->ws
->focus
->ch
.class_name
!= NULL
)
2126 strlcat(s
, r
->ws
->focus
->ch
.class_name
, sz
);
2130 bar_window_instance(char *s
, size_t sz
, struct swm_region
*r
)
2132 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2134 if (r
->ws
->focus
->ch
.instance_name
!= NULL
)
2135 strlcat(s
, r
->ws
->focus
->ch
.instance_name
, sz
);
2139 bar_window_class_instance(char *s
, size_t sz
, struct swm_region
*r
)
2141 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2144 bar_window_class(s
, sz
, r
);
2145 strlcat(s
, ":", sz
);
2146 bar_window_instance(s
, sz
, r
);
2150 bar_window_state(char *s
, size_t sz
, struct swm_region
*r
)
2152 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2154 if (MAXIMIZED(r
->ws
->focus
))
2155 strlcat(s
, "(m)", sz
);
2156 else if (ABOVE(r
->ws
->focus
))
2157 strlcat(s
, "(f)", sz
);
2161 bar_window_name(char *s
, size_t sz
, struct swm_region
*r
)
2165 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2168 title
= get_win_name(r
->ws
->focus
->id
);
2169 strlcat(s
, title
, sz
);
2174 get_urgent(struct ws_win
*win
)
2176 xcb_icccm_wm_hints_t hints
;
2177 xcb_get_property_cookie_t c
;
2178 bool urgent
= false;
2181 c
= xcb_icccm_get_wm_hints(conn
, win
->id
);
2182 if (xcb_icccm_get_wm_hints_reply(conn
, c
, &hints
, NULL
))
2183 urgent
= xcb_icccm_wm_hints_get_urgency(&hints
);
2190 bar_urgent(char *s
, size_t sz
)
2193 int i
, j
, num_screens
;
2194 bool urgent
[SWM_WS_MAX
];
2197 for (i
= 0; i
< workspace_limit
; i
++)
2200 num_screens
= get_screen_count();
2201 for (i
= 0; i
< num_screens
; i
++)
2202 for (j
= 0; j
< workspace_limit
; j
++)
2203 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2204 if (get_urgent(win
))
2207 for (i
= 0; i
< workspace_limit
; i
++) {
2209 snprintf(b
, sizeof b
, "%d ", i
+ 1);
2211 } else if (!urgent_collapse
) {
2212 strlcat(s
, "- ", sz
);
2218 bar_workspace_name(char *s
, size_t sz
, struct swm_region
*r
)
2220 if (r
== NULL
|| r
->ws
== NULL
)
2222 if (r
->ws
->name
!= NULL
)
2223 strlcat(s
, r
->ws
->name
, sz
);
2226 /* build the default bar format according to the defined enabled options */
2228 bar_fmt(const char *fmtexp
, char *fmtnew
, struct swm_region
*r
, size_t sz
)
2232 /* if format provided, just copy the buffers */
2233 if (bar_format
!= NULL
) {
2234 strlcpy(fmtnew
, fmtexp
, sz
);
2238 /* reset the output buffer */
2241 strlcat(fmtnew
, "+N:+I ", sz
);
2243 strlcat(fmtnew
, "+S", sz
);
2244 strlcat(fmtnew
, " ", sz
);
2246 /* only show the workspace name if there's actually one */
2247 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->name
!= NULL
)
2248 strlcat(fmtnew
, "<+D>", sz
);
2250 /* If enabled, only show the iconic count if there are iconic wins. */
2251 if (iconic_enabled
&& r
!= NULL
&& r
->ws
!= NULL
)
2252 TAILQ_FOREACH(w
, &r
->ws
->winlist
, entry
)
2254 strlcat(fmtnew
, "{+M}", sz
);
2258 strlcat(fmtnew
, "+3<", sz
);
2260 if (clock_enabled
) {
2261 strlcat(fmtnew
, fmtexp
, sz
);
2262 strlcat(fmtnew
, "+4<", sz
);
2265 /* bar_urgent already adds the space before the last asterisk */
2267 strlcat(fmtnew
, "* +U*+4<", sz
);
2269 if (window_class_enabled
) {
2270 strlcat(fmtnew
, "+C", sz
);
2271 if (!window_instance_enabled
)
2272 strlcat(fmtnew
, "+4<", sz
);
2275 /* checks needed by the colon and floating strlcat(3) calls below */
2276 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->focus
!= NULL
) {
2277 if (window_instance_enabled
) {
2278 if (window_class_enabled
)
2279 strlcat(fmtnew
, ":", sz
);
2280 strlcat(fmtnew
, "+T+4<", sz
);
2282 if (window_name_enabled
) {
2283 if (ABOVE(r
->ws
->focus
) || MAXIMIZED(r
->ws
->focus
))
2284 strlcat(fmtnew
, "+F ", sz
);
2285 strlcat(fmtnew
, "+64W ", sz
);
2289 /* finally add the action script output and the version */
2290 strlcat(fmtnew
, "+4<+A+4<+V", sz
);
2294 bar_replace_pad(char *tmp
, int *limit
, size_t sz
)
2296 /* special case; no limit given, pad one space, instead */
2297 if (*limit
== (int)sz
- 1)
2299 snprintf(tmp
, sz
, "%*s", *limit
, " ");
2302 /* replaces the bar format character sequences (like in tmux(1)) */
2304 bar_replace_seq(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t *offrep
,
2309 char tmp
[SWM_BAR_MAX
];
2310 int limit
, size
, count
;
2313 /* reset strlcat(3) buffer */
2316 /* get number, if any */
2319 if (sscanf(fmt
, "%d%n", &limit
, &size
) != 1)
2320 limit
= sizeof tmp
- 1;
2321 if (limit
<= 0 || limit
>= (int)sizeof tmp
)
2322 limit
= sizeof tmp
- 1;
2324 /* there is nothing to replace (ie EOL) */
2331 bar_replace_pad(tmp
, &limit
, sizeof tmp
);
2334 snprintf(tmp
, sizeof tmp
, "%s", bar_ext
);
2337 bar_window_class(tmp
, sizeof tmp
, r
);
2340 bar_workspace_name(tmp
, sizeof tmp
, r
);
2343 bar_window_state(tmp
, sizeof tmp
, r
);
2346 snprintf(tmp
, sizeof tmp
, "%d", r
->ws
->idx
+ 1);
2350 TAILQ_FOREACH(w
, &r
->ws
->winlist
, entry
)
2354 snprintf(tmp
, sizeof tmp
, "%d", count
);
2357 snprintf(tmp
, sizeof tmp
, "%d", r
->s
->idx
+ 1);
2360 bar_window_class_instance(tmp
, sizeof tmp
, r
);
2363 snprintf(tmp
, sizeof tmp
, "%s", r
->ws
->stacker
);
2366 bar_window_instance(tmp
, sizeof tmp
, r
);
2369 bar_urgent(tmp
, sizeof tmp
);
2372 snprintf(tmp
, sizeof tmp
, "%s", bar_vertext
);
2375 bar_window_name(tmp
, sizeof tmp
, r
);
2378 /* unknown character sequence; copy as-is */
2379 snprintf(tmp
, sizeof tmp
, "+%c", *fmt
);
2385 if ((int)len
< limit
)
2387 while (limit
-- > 0) {
2388 if (*offrep
>= sz
- 1)
2390 fmtrep
[(*offrep
)++] = *ptr
++;
2398 bar_replace(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t sz
)
2403 while (*fmt
!= '\0') {
2405 /* skip ordinary characters */
2408 fmtrep
[off
++] = *fmt
++;
2412 /* character sequence found; replace it */
2413 fmt
= bar_replace_seq(fmt
, fmtrep
, r
, &off
, sz
);
2422 bar_fmt_expand(char *fmtexp
, size_t sz
)
2429 /* start by grabbing the current time and date */
2431 localtime_r(&tmt
, &tm
);
2433 /* figure out what to expand */
2434 if (bar_format
!= NULL
)
2436 else if (bar_format
== NULL
&& clock_enabled
)
2438 /* if nothing to expand bail out */
2444 /* copy as-is, just in case the format shouldn't be expanded below */
2445 strlcpy(fmtexp
, fmt
, sz
);
2446 /* finally pass the string through strftime(3) */
2447 #ifndef SWM_DENY_CLOCK_FORMAT
2448 if ((len
= strftime(fmtexp
, sz
, fmt
, &tm
)) == 0)
2449 warnx("format too long");
2454 /* Redraws the bar; need to follow with xcb_flush() or focus_flush(). */
2458 char fmtexp
[SWM_BAR_MAX
], fmtnew
[SWM_BAR_MAX
];
2459 char fmtrep
[SWM_BAR_MAX
];
2461 struct swm_region
*r
;
2463 /* expand the format by first passing it through strftime(3) */
2464 bar_fmt_expand(fmtexp
, sizeof fmtexp
);
2466 num_screens
= get_screen_count();
2467 for (i
= 0; i
< num_screens
; i
++) {
2468 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
2472 if (bar_enabled
&& r
->ws
->bar_enabled
)
2473 xcb_map_window(conn
, r
->bar
->id
);
2475 xcb_unmap_window(conn
, r
->bar
->id
);
2479 if (startup_exception
)
2480 snprintf(fmtrep
, sizeof fmtrep
, "total "
2481 "exceptions: %d, first exception: %s",
2485 bar_fmt(fmtexp
, fmtnew
, r
, sizeof fmtnew
);
2486 bar_replace(fmtnew
, fmtrep
, r
, sizeof fmtrep
);
2488 if (bar_font_legacy
)
2489 bar_print_legacy(r
, fmtrep
);
2491 bar_print(r
, fmtrep
);
2497 * Reads external script output; call when stdin is readable.
2498 * Returns 1 if bar_ext was updated; otherwise 0.
2501 bar_extra_update(void)
2504 char b
[SWM_BAR_MAX
];
2505 bool changed
= false;
2510 while (fgets(b
, sizeof(b
), stdin
) != NULL
) {
2513 if (b
[len
- 1] == '\n') {
2514 /* Remove newline. */
2517 /* "Clear" bar_ext. */
2520 /* Flush buffered output. */
2521 strlcpy(bar_ext
, bar_ext_buf
, sizeof(bar_ext
));
2522 bar_ext_buf
[0] = '\0';
2524 /* Append new output to bar. */
2525 strlcat(bar_ext
, b
, sizeof(bar_ext
));
2529 /* Buffer output. */
2530 strlcat(bar_ext_buf
, b
, sizeof(bar_ext_buf
));
2535 if (errno
!= EAGAIN
) {
2536 warn("bar_action failed");
2545 bar_toggle(struct swm_region
*r
, union arg
*args
)
2547 struct swm_region
*tmpr
;
2550 /* suppress unused warnings since vars are needed */
2554 DNPRINTF(SWM_D_BAR
, "bar_toggle\n");
2557 case SWM_ARG_ID_BAR_TOGGLE_WS
:
2558 /* Only change if master switch is enabled. */
2560 r
->ws
->bar_enabled
= !r
->ws
->bar_enabled
;
2562 bar_enabled
= r
->ws
->bar_enabled
= true;
2564 case SWM_ARG_ID_BAR_TOGGLE
:
2565 bar_enabled
= !bar_enabled
;
2569 /* update bars as necessary */
2570 num_screens
= get_screen_count();
2571 for (i
= 0; i
< num_screens
; i
++)
2572 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
2574 if (bar_enabled
&& tmpr
->ws
->bar_enabled
)
2575 xcb_map_window(conn
, tmpr
->bar
->id
);
2577 xcb_unmap_window(conn
, tmpr
->bar
->id
);
2582 /* must be after stack */
2589 bar_extra_setup(void)
2591 /* do this here because the conf file is in memory */
2592 if (!bar_extra
&& bar_argv
[0]) {
2593 /* launch external status app */
2595 if (pipe(bar_pipe
) == -1)
2596 err(1, "pipe error");
2597 socket_setnonblock(bar_pipe
[0]);
2598 socket_setnonblock(bar_pipe
[1]); /* XXX hmmm, really? */
2600 /* Set stdin to read from the pipe. */
2601 if (dup2(bar_pipe
[0], STDIN_FILENO
) == -1)
2604 /* Set stdout to write to the pipe. */
2605 if (dup2(bar_pipe
[1], STDOUT_FILENO
) == -1)
2608 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
2609 err(1, "could not disable SIGPIPE");
2610 switch (bar_pid
= fork()) {
2612 err(1, "cannot fork");
2616 execvp(bar_argv
[0], bar_argv
);
2617 err(1, "%s external app failed", bar_argv
[0]);
2619 default: /* parent */
2624 atexit(kill_bar_extra_atexit
);
2629 kill_bar_extra_atexit(void)
2632 kill(bar_pid
, SIGTERM
);
2640 while ((s
= index(s
, '-'))) {
2645 return (count
== 14);
2651 char *default_string
;
2652 char **missing_charsets
;
2653 int num_missing_charsets
= 0;
2657 XFreeFontSet(display
, bar_fs
);
2661 DNPRINTF(SWM_D_INIT
, "fontset_init: loading bar_fonts: %s\n", bar_fonts
);
2663 bar_fs
= XCreateFontSet(display
, bar_fonts
, &missing_charsets
,
2664 &num_missing_charsets
, &default_string
);
2666 if (num_missing_charsets
> 0) {
2667 warnx("Unable to load charset(s):");
2669 for (i
= 0; i
< num_missing_charsets
; ++i
)
2670 warnx("%s", missing_charsets
[i
]);
2672 XFreeStringList(missing_charsets
);
2674 if (strcmp(default_string
, ""))
2675 warnx("Glyphs from those sets will be replaced "
2676 "by '%s'.", default_string
);
2678 warnx("Glyphs from those sets won't be drawn.");
2682 errx(1, "Error creating font set structure.");
2684 bar_fs_extents
= XExtentsOfFontSet(bar_fs
);
2686 bar_height
= bar_fs_extents
->max_logical_extent
.height
+
2687 2 * bar_border_width
;
2694 xft_init(struct swm_region
*r
)
2696 char *font
, *d
, *search
;
2699 if (bar_font
== NULL
) {
2700 if ((d
= strdup(bar_fonts
)) == NULL
)
2701 errx(1, "insufficient memory.");
2703 while ((font
= strsep(&search
, ",")) != NULL
) {
2707 DNPRINTF(SWM_D_INIT
, "xft_init: try font %s\n", font
);
2710 bar_font
= XftFontOpenXlfd(display
, r
->s
->idx
,
2713 bar_font
= XftFontOpenName(display
, r
->s
->idx
,
2717 if (bar_font
== NULL
) {
2718 warnx("unable to load font %s", font
);
2721 DNPRINTF(SWM_D_INIT
, "successfully opened "
2729 if (bar_font
== NULL
)
2730 errx(1, "unable to open a font");
2732 PIXEL_TO_XRENDERCOLOR(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
, color
);
2734 if (!XftColorAllocValue(display
, DefaultVisual(display
, r
->s
->idx
),
2735 DefaultColormap(display
, r
->s
->idx
), &color
, &bar_font_color
))
2736 warn("Xft error: unable to allocate color.");
2738 PIXEL_TO_XRENDERCOLOR(r
->s
->c
[SWM_S_COLOR_BAR
].pixel
, color
);
2740 if (!XftColorAllocValue(display
, DefaultVisual(display
, r
->s
->idx
),
2741 DefaultColormap(display
, r
->s
->idx
), &color
, &search_font_color
))
2742 warn("Xft error: unable to allocate color.");
2744 bar_height
= bar_font
->height
+ 2 * bar_border_width
;
2751 bar_setup(struct swm_region
*r
)
2753 xcb_screen_t
*screen
;
2756 DNPRINTF(SWM_D_BAR
, "bar_setup: screen %d.\n",
2759 if ((screen
= get_screen(r
->s
->idx
)) == NULL
)
2760 errx(1, "ERROR: can't get screen %d.", r
->s
->idx
);
2765 if ((r
->bar
= calloc(1, sizeof(struct swm_bar
))) == NULL
)
2766 err(1, "bar_setup: calloc: failed to allocate memory.");
2768 if (bar_font_legacy
)
2774 Y(r
->bar
) = bar_at_bottom
? (Y(r
) + HEIGHT(r
) - bar_height
) : Y(r
);
2775 WIDTH(r
->bar
) = WIDTH(r
) - 2 * bar_border_width
;
2776 HEIGHT(r
->bar
) = bar_height
- 2 * bar_border_width
;
2778 /* Assume region is unfocused when we create the bar. */
2779 r
->bar
->id
= xcb_generate_id(conn
);
2780 wa
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
2781 wa
[1] = r
->s
->c
[SWM_S_COLOR_BAR_BORDER_UNFOCUS
].pixel
;
2782 wa
[2] = XCB_EVENT_MASK_EXPOSURE
| XCB_EVENT_MASK_POINTER_MOTION
|
2783 XCB_EVENT_MASK_POINTER_MOTION_HINT
;
2785 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, r
->bar
->id
, r
->s
->root
,
2786 X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
), HEIGHT(r
->bar
),
2787 bar_border_width
, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
2788 XCB_COPY_FROM_PARENT
, XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
2789 | XCB_CW_EVENT_MASK
, wa
);
2791 /* Stack bar window above region window to start. */
2793 wa
[1] = XCB_STACK_MODE_ABOVE
;
2795 xcb_configure_window(conn
, r
->bar
->id
, XCB_CONFIG_WINDOW_SIBLING
|
2796 XCB_CONFIG_WINDOW_STACK_MODE
, wa
);
2798 r
->bar
->buffer
= xcb_generate_id(conn
);
2799 xcb_create_pixmap(conn
, screen
->root_depth
, r
->bar
->buffer
, r
->bar
->id
,
2800 WIDTH(r
->bar
), HEIGHT(r
->bar
));
2803 xcb_randr_select_input(conn
, r
->bar
->id
,
2804 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE
);
2807 xcb_map_window(conn
, r
->bar
->id
);
2809 DNPRINTF(SWM_D_BAR
, "bar_setup: win %#x, (x,y) w x h: (%d,%d) "
2810 "%d x %d\n", WINID(r
->bar
), X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
),
2817 bar_cleanup(struct swm_region
*r
)
2821 xcb_destroy_window(conn
, r
->bar
->id
);
2822 xcb_free_pixmap(conn
, r
->bar
->buffer
);
2828 set_win_state(struct ws_win
*win
, uint8_t state
)
2830 uint16_t data
[2] = { state
, XCB_ATOM_NONE
};
2832 DNPRINTF(SWM_D_EVENT
, "set_win_state: win %#x, state: %u\n",
2838 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
, a_state
,
2839 a_state
, 32, 2, data
);
2843 get_win_state(xcb_window_t w
)
2845 xcb_get_property_reply_t
*r
;
2846 xcb_get_property_cookie_t c
;
2847 uint32_t result
= 0;
2849 c
= xcb_get_property(conn
, 0, w
, a_state
, a_state
, 0L, 2L);
2850 r
= xcb_get_property_reply(conn
, c
, NULL
);
2852 if (r
->type
== a_state
&& r
->format
== 32 && r
->length
== 2)
2853 result
= *((uint32_t *)xcb_get_property_value(r
));
2857 DNPRINTF(SWM_D_MISC
, "get_win_state property: win %#x state %u\n", w
,
2863 version(struct swm_region
*r
, union arg
*args
)
2865 /* suppress unused warnings since vars are needed */
2869 bar_version
= !bar_version
;
2871 snprintf(bar_vertext
, sizeof bar_vertext
,
2872 "Version: %s Build: %s", SPECTRWM_VERSION
, buildstr
);
2874 strlcpy(bar_vertext
, "", sizeof bar_vertext
);
2881 client_msg(struct ws_win
*win
, xcb_atom_t a
, xcb_timestamp_t t
)
2883 xcb_client_message_event_t ev
;
2891 name
= get_atom_name(a
);
2892 DNPRINTF(SWM_D_EVENT
, "client_msg: win %#x, atom: %s(%u), "
2894 win
->id
, name
, a
, t
);
2898 bzero(&ev
, sizeof ev
);
2899 ev
.response_type
= XCB_CLIENT_MESSAGE
;
2900 ev
.window
= win
->id
;
2903 ev
.data
.data32
[0] = a
;
2904 ev
.data
.data32
[1] = t
;
2906 xcb_send_event(conn
, 0, win
->id
,
2907 XCB_EVENT_MASK_NO_EVENT
, (const char *)&ev
);
2910 /* synthetic response to a ConfigureRequest when not making a change */
2912 config_win(struct ws_win
*win
, xcb_configure_request_event_t
*ev
)
2914 xcb_configure_notify_event_t ce
;
2919 /* send notification of unchanged state. */
2920 bzero(&ce
, sizeof(ce
));
2921 ce
.response_type
= XCB_CONFIGURE_NOTIFY
;
2924 ce
.width
= WIDTH(win
);
2925 ce
.height
= HEIGHT(win
);
2926 ce
.override_redirect
= 0;
2931 ce
.window
= win
->id
;
2932 ce
.border_width
= BORDER(win
);
2933 ce
.above_sibling
= XCB_WINDOW_NONE
;
2936 ce
.event
= ev
->window
;
2937 ce
.window
= ev
->window
;
2939 /* make response appear more WM_SIZE_HINTS-compliant */
2940 if (win
->sh
.flags
) {
2941 DNPRINTF(SWM_D_MISC
, "config_win: hints: win %#x,"
2942 " sh.flags: %u, min: %d x %d, max: %d x %d, inc: "
2943 "%d x %d\n", win
->id
, win
->sh
.flags
, SH_MIN_W(win
),
2944 SH_MIN_H(win
), SH_MAX_W(win
), SH_MAX_H(win
),
2945 SH_INC_W(win
), SH_INC_H(win
));
2950 /* the hint may be set... to 0! */
2951 if (SH_MIN_W(win
) > 0 && ce
.width
< SH_MIN_W(win
))
2952 ce
.width
= SH_MIN_W(win
);
2953 if (SH_MIN_H(win
) > 0 && ce
.height
< SH_MIN_H(win
))
2954 ce
.height
= SH_MIN_H(win
);
2959 /* may also be advertized as 0 */
2960 if (SH_MAX_W(win
) > 0 && ce
.width
> SH_MAX_W(win
))
2961 ce
.width
= SH_MAX_W(win
);
2962 if (SH_MAX_H(win
) > 0 && ce
.height
> SH_MAX_H(win
))
2963 ce
.height
= SH_MAX_H(win
);
2966 /* resize increment. */
2968 if (SH_INC_W(win
) > 1 && ce
.width
> SH_INC_W(win
))
2969 ce
.width
-= (ce
.width
- SH_MIN_W(win
)) %
2971 if (SH_INC_H(win
) > 1 && ce
.height
> SH_INC_H(win
))
2972 ce
.height
-= (ce
.height
- SH_MIN_H(win
)) %
2976 /* adjust x and y for requested border_width. */
2977 ce
.x
+= BORDER(win
) - ev
->border_width
;
2978 ce
.y
+= BORDER(win
) - ev
->border_width
;
2979 ce
.border_width
= ev
->border_width
;
2980 ce
.above_sibling
= ev
->sibling
;
2983 DNPRINTF(SWM_D_MISC
, "config_win: ewmh: %s, win %#x, (x,y) w x h: "
2984 "(%d,%d) %d x %d, border: %d\n", YESNO(ev
== NULL
), win
->id
, ce
.x
,
2985 ce
.y
, ce
.width
, ce
.height
, ce
.border_width
);
2987 xcb_send_event(conn
, 0, win
->id
, XCB_EVENT_MASK_STRUCTURE_NOTIFY
,
2992 count_win(struct workspace
*ws
, bool count_transient
)
2997 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
2998 if (!count_transient
&& FLOATING(win
))
3004 DNPRINTF(SWM_D_MISC
, "count_win: %d\n", count
);
3010 quit(struct swm_region
*r
, union arg
*args
)
3012 /* suppress unused warnings since vars are needed */
3016 DNPRINTF(SWM_D_MISC
, "quit\n");
3021 lower_window(struct ws_win
*win
)
3023 struct ws_win
*target
= NULL
;
3024 struct workspace
*ws
;
3031 DNPRINTF(SWM_D_EVENT
, "lower_window: win %#x\n", win
->id
);
3033 TAILQ_FOREACH(target
, &ws
->stack
, stack_entry
) {
3034 if (target
== win
|| ICONIC(target
))
3036 if (ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
3039 if (win
->transient
== target
->transient
)
3041 if (win
->transient
== target
->id
)
3044 if (FULLSCREEN(target
))
3046 if (FULLSCREEN(win
))
3048 if (MAXIMIZED(target
))
3052 if (ABOVE(target
) || TRANS(target
))
3054 if (ABOVE(win
) || TRANS(win
))
3058 /* Change stack position. */
3059 TAILQ_REMOVE(&ws
->stack
, win
, stack_entry
);
3061 TAILQ_INSERT_BEFORE(target
, win
, stack_entry
);
3063 TAILQ_INSERT_TAIL(&ws
->stack
, win
, stack_entry
);
3065 update_win_stacking(win
);
3068 if (swm_debug
& SWM_D_STACK
) {
3069 DPRINTF("=== stacking order (top down) === \n");
3070 TAILQ_FOREACH(target
, &ws
->stack
, stack_entry
) {
3071 DPRINTF("win %#x, fs: %s, maximized: %s, above: %s, "
3072 "iconic: %s\n", target
->id
, YESNO(FULLSCREEN(target
)),
3073 YESNO(MAXIMIZED(target
)), YESNO(ABOVE(target
)),
3074 YESNO(ICONIC(target
)));
3078 DNPRINTF(SWM_D_EVENT
, "lower_window: done\n");
3082 raise_window(struct ws_win
*win
)
3084 struct ws_win
*target
= NULL
;
3085 struct workspace
*ws
;
3091 DNPRINTF(SWM_D_EVENT
, "raise_window: win %#x\n", win
->id
);
3093 TAILQ_FOREACH(target
, &ws
->stack
, stack_entry
) {
3094 if (target
== win
|| ICONIC(target
))
3096 if (ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
3098 if (TRANS(win
) && (win
->transient
== target
->transient
||
3099 win
->transient
== target
->id
))
3101 if (FULLSCREEN(win
))
3103 if (FULLSCREEN(target
))
3107 if (MAXIMIZED(target
))
3109 if (ABOVE(win
) || TRANS(win
) ||
3110 (win
->ws
->focus
== win
&& ws
->always_raise
))
3112 if (!ABOVE(target
) && !TRANS(target
))
3116 TAILQ_REMOVE(&ws
->stack
, win
, stack_entry
);
3118 TAILQ_INSERT_BEFORE(target
, win
, stack_entry
);
3120 TAILQ_INSERT_TAIL(&ws
->stack
, win
, stack_entry
);
3122 update_win_stacking(win
);
3125 if (swm_debug
& SWM_D_STACK
) {
3126 DPRINTF("=== stacking order (top down) === \n");
3127 TAILQ_FOREACH(target
, &ws
->stack
, stack_entry
) {
3128 DPRINTF("win %#x, fs: %s, maximized: %s, above: %s, "
3129 "iconic: %s\n", target
->id
, YESNO(FULLSCREEN(target
)),
3130 YESNO(MAXIMIZED(target
)), YESNO(ABOVE(target
)),
3131 YESNO(ICONIC(target
)));
3135 DNPRINTF(SWM_D_EVENT
, "raise_window: done\n");
3139 update_win_stacking(struct ws_win
*win
)
3141 struct ws_win
*sibling
;
3142 struct swm_region
*r
;
3145 if (win
== NULL
|| (r
= win
->ws
->r
) == NULL
)
3148 sibling
= TAILQ_NEXT(win
, stack_entry
);
3149 if (sibling
!= NULL
&& (FLOATING(win
) == FLOATING(sibling
) ||
3150 (win
->ws
->always_raise
&& win
->ws
->focus
== win
)))
3151 val
[0] = sibling
->id
;
3152 else if (FLOATING(win
) || (win
->ws
->always_raise
&&
3153 win
->ws
->focus
== win
))
3154 val
[0] = r
->bar
->id
;
3158 DNPRINTF(SWM_D_EVENT
, "update_win_stacking: %#x, sibling %#x\n",
3161 val
[1] = XCB_STACK_MODE_ABOVE
;
3163 xcb_configure_window(conn
, win
->id
, XCB_CONFIG_WINDOW_SIBLING
|
3164 XCB_CONFIG_WINDOW_STACK_MODE
, val
);
3168 map_window(struct ws_win
*win
)
3173 DNPRINTF(SWM_D_EVENT
, "map_window: win %#x, mapped: %s\n",
3174 win
->id
, YESNO(win
->mapped
));
3179 xcb_map_window(conn
, win
->id
);
3180 set_win_state(win
, XCB_ICCCM_WM_STATE_NORMAL
);
3185 unmap_window(struct ws_win
*win
)
3190 DNPRINTF(SWM_D_EVENT
, "unmap_window: win %#x, mapped: %s\n", win
->id
,
3191 YESNO(win
->mapped
));
3196 xcb_unmap_window(conn
, win
->id
);
3197 set_win_state(win
, XCB_ICCCM_WM_STATE_ICONIC
);
3198 win
->mapped
= false;
3205 int i
, j
, num_screens
;
3207 num_screens
= get_screen_count();
3208 for (i
= 0; i
< num_screens
; i
++)
3209 for (j
= 0; j
< workspace_limit
; j
++)
3210 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
3215 fake_keypress(struct ws_win
*win
, xcb_keysym_t keysym
, uint16_t modifiers
)
3217 xcb_key_press_event_t event
;
3218 xcb_keycode_t
*keycode
;
3223 keycode
= xcb_key_symbols_get_keycode(syms
, keysym
);
3225 DNPRINTF(SWM_D_MISC
, "fake_keypress: win %#x, keycode %u\n",
3228 bzero(&event
, sizeof(event
));
3229 event
.event
= win
->id
;
3230 event
.root
= win
->s
->root
;
3231 event
.child
= XCB_WINDOW_NONE
;
3232 event
.time
= XCB_CURRENT_TIME
;
3233 event
.event_x
= X(win
);
3234 event
.event_y
= Y(win
);
3237 event
.same_screen
= 1;
3238 event
.detail
= *keycode
;
3239 event
.state
= modifiers
;
3241 event
.response_type
= XCB_KEY_PRESS
;
3242 xcb_send_event(conn
, 1, win
->id
,
3243 XCB_EVENT_MASK_KEY_PRESS
, (const char *)&event
);
3245 event
.response_type
= XCB_KEY_RELEASE
;
3246 xcb_send_event(conn
, 1, win
->id
,
3247 XCB_EVENT_MASK_KEY_RELEASE
, (const char *)&event
);
3253 restart(struct swm_region
*r
, union arg
*args
)
3255 /* suppress unused warning since var is needed */
3259 DNPRINTF(SWM_D_MISC
, "restart: %s\n", start_argv
[0]);
3263 execvp(start_argv
[0], start_argv
);
3264 warn("execvp failed");
3269 get_pointer_win(xcb_window_t root
)
3271 struct ws_win
*win
= NULL
;
3272 xcb_query_pointer_reply_t
*r
;
3274 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: root: %#x.\n", root
);
3276 r
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, root
), NULL
);
3278 win
= find_window(r
->child
);
3280 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: %#x.\n",
3283 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: none.\n");
3292 center_pointer(struct swm_region
*r
)
3296 if (!warp_pointer
|| r
== NULL
)
3301 DNPRINTF(SWM_D_EVENT
, "center_pointer: win %#x.\n", WINID(win
));
3303 if (win
&& win
->mapped
)
3304 xcb_warp_pointer(conn
, XCB_NONE
, win
->id
, 0, 0, 0, 0,
3305 WIDTH(win
) / 2, HEIGHT(win
) / 2);
3307 xcb_warp_pointer(conn
, XCB_NONE
, r
->id
, 0, 0, 0, 0,
3308 WIDTH(r
) / 2, HEIGHT(r
) / 2);
3312 root_to_region(xcb_window_t root
, int check
)
3315 struct swm_region
*r
= NULL
;
3317 xcb_query_pointer_reply_t
*qpr
;
3318 xcb_get_input_focus_reply_t
*gifr
;
3320 DNPRINTF(SWM_D_MISC
, "root_to_region: win %#x\n", root
);
3322 num_screens
= get_screen_count();
3323 for (i
= 0; i
< num_screens
; i
++)
3324 if (screens
[i
].root
== root
)
3327 if (check
& SWM_CK_REGION
)
3328 r
= screens
[i
].r_focus
;
3330 if (r
== NULL
&& check
& SWM_CK_FOCUS
) {
3331 /* Try to find an actively focused window */
3332 gifr
= xcb_get_input_focus_reply(conn
,
3333 xcb_get_input_focus(conn
), NULL
);
3335 cfw
= find_window(gifr
->focus
);
3336 if (cfw
&& cfw
->ws
->r
)
3343 if (r
== NULL
&& check
& SWM_CK_POINTER
) {
3344 /* No region with an active focus; try to use pointer. */
3345 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
,
3346 screens
[i
].root
), NULL
);
3348 DNPRINTF(SWM_D_MISC
, "root_to_region: pointer: "
3349 "(%d,%d)\n", qpr
->root_x
, qpr
->root_y
);
3350 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3351 if (X(r
) <= qpr
->root_x
&&
3352 qpr
->root_x
< MAX_X(r
) &&
3353 Y(r
) <= qpr
->root_y
&&
3354 qpr
->root_y
< MAX_Y(r
))
3361 if (r
== NULL
&& check
& SWM_CK_FALLBACK
)
3362 r
= TAILQ_FIRST(&screens
[i
].rl
);
3364 DNPRINTF(SWM_D_MISC
, "root_to_region: idx: %d\n", get_region_index(r
));
3370 find_unmanaged_window(xcb_window_t id
)
3373 int i
, j
, num_screens
;
3375 num_screens
= get_screen_count();
3376 for (i
= 0; i
< num_screens
; i
++)
3377 for (j
= 0; j
< workspace_limit
; j
++)
3378 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].unmanagedlist
,
3386 find_window(xcb_window_t id
)
3389 int i
, j
, num_screens
;
3390 xcb_query_tree_reply_t
*r
;
3392 num_screens
= get_screen_count();
3393 for (i
= 0; i
< num_screens
; i
++)
3394 for (j
= 0; j
< workspace_limit
; j
++)
3395 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
3399 r
= xcb_query_tree_reply(conn
, xcb_query_tree(conn
, id
), NULL
);
3403 /* if we were looking for the parent return that window instead */
3404 if (r
->parent
== 0 || r
->root
== r
->parent
) {
3409 /* look for parent */
3410 for (i
= 0; i
< num_screens
; i
++)
3411 for (j
= 0; j
< workspace_limit
; j
++)
3412 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
3413 if (r
->parent
== win
->id
) {
3423 spawn(int ws_idx
, union arg
*args
, bool close_fd
)
3428 DNPRINTF(SWM_D_MISC
, "spawn: %s\n", args
->argv
[0]);
3430 close(xcb_get_file_descriptor(conn
));
3432 setenv("LD_PRELOAD", SWM_LIB
, 1);
3434 if (asprintf(&ret
, "%d", ws_idx
) == -1) {
3435 warn("spawn: asprintf SWM_WS");
3438 setenv("_SWM_WS", ret
, 1);
3442 if (asprintf(&ret
, "%d", getpid()) == -1) {
3443 warn("spawn: asprintf _SWM_PID");
3446 setenv("_SWM_PID", ret
, 1);
3450 if (setsid() == -1) {
3451 warn("spawn: setsid");
3457 * close stdin and stdout to prevent interaction between apps
3458 * and the baraction script
3459 * leave stderr open to record errors
3461 if ((fd
= open(_PATH_DEVNULL
, O_RDWR
, 0)) == -1) {
3462 warn("spawn: open");
3465 dup2(fd
, STDIN_FILENO
);
3466 dup2(fd
, STDOUT_FILENO
);
3471 execvp(args
->argv
[0], args
->argv
);
3473 warn("spawn: execvp");
3478 kill_refs(struct ws_win
*win
)
3480 int i
, x
, num_screens
;
3481 struct swm_region
*r
;
3482 struct workspace
*ws
;
3487 num_screens
= get_screen_count();
3488 for (i
= 0; i
< num_screens
; i
++)
3489 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3490 for (x
= 0; x
< workspace_limit
; x
++) {
3492 if (win
== ws
->focus
)
3494 if (win
== ws
->focus_prev
)
3495 ws
->focus_prev
= NULL
;
3500 validate_win(struct ws_win
*testwin
)
3503 struct workspace
*ws
;
3504 struct swm_region
*r
;
3505 int i
, x
, num_screens
;
3507 if (testwin
== NULL
)
3510 num_screens
= get_screen_count();
3511 for (i
= 0; i
< num_screens
; i
++)
3512 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3513 for (x
= 0; x
< workspace_limit
; x
++) {
3515 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
3523 validate_ws(struct workspace
*testws
)
3525 struct swm_region
*r
;
3526 struct workspace
*ws
;
3527 int i
, x
, num_screens
;
3529 /* validate all ws */
3530 num_screens
= get_screen_count();
3531 for (i
= 0; i
< num_screens
; i
++)
3532 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3533 for (x
= 0; x
< workspace_limit
; x
++) {
3542 unfocus_win(struct ws_win
*win
)
3544 xcb_window_t none
= XCB_WINDOW_NONE
;
3546 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: win %#x\n", WINID(win
));
3551 if (win
->ws
== NULL
) {
3552 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: NULL ws.\n");
3556 if (validate_ws(win
->ws
)) {
3557 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: invalid ws.\n");
3561 if (win
->ws
->r
== NULL
) {
3562 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: NULL region.\n");
3566 if (validate_win(win
)) {
3567 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: invalid win.\n");
3572 if (win
->ws
->focus
== win
) {
3573 win
->ws
->focus
= NULL
;
3574 win
->ws
->focus_prev
= win
;
3577 if (validate_win(win
->ws
->focus
)) {
3578 kill_refs(win
->ws
->focus
);
3579 win
->ws
->focus
= NULL
;
3582 if (validate_win(win
->ws
->focus_prev
)) {
3583 kill_refs(win
->ws
->focus_prev
);
3584 win
->ws
->focus_prev
= NULL
;
3587 update_window_color(win
);
3589 /* Raise window to "top unfocused position." */
3590 if (win
->ws
->always_raise
)
3593 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
3594 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1, &none
);
3596 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: done.\n");
3600 focus_win(struct ws_win
*win
)
3602 struct ws_win
*cfw
= NULL
, *parent
= NULL
, *w
, *tmpw
;
3603 struct workspace
*ws
;
3604 xcb_get_input_focus_reply_t
*gifr
;
3606 DNPRINTF(SWM_D_FOCUS
, "focus_win: win %#x\n", WINID(win
));
3608 if (win
== NULL
|| win
->ws
== NULL
|| !win
->mapped
)
3613 if (validate_ws(ws
))
3616 if (validate_win(win
)) {
3621 gifr
= xcb_get_input_focus_reply(conn
, xcb_get_input_focus(conn
), NULL
);
3623 cfw
= find_window(gifr
->focus
);
3624 if (cfw
!= NULL
&& cfw
!= win
) {
3625 if (cfw
->ws
!= ws
&& cfw
->ws
->r
!= NULL
) {
3626 /* Change border to unfocused color. */
3627 xcb_change_window_attributes(conn
, cfw
->id
,
3628 XCB_CW_BORDER_PIXEL
,
3629 &cfw
->s
->c
[(MAXIMIZED(cfw
) ?
3630 SWM_S_COLOR_UNFOCUS_MAXIMIZED
:
3631 SWM_S_COLOR_UNFOCUS
)].pixel
);
3639 if (ws
->focus
!= win
) {
3640 if (ws
->focus
&& ws
->focus
!= cfw
)
3641 unfocus_win(ws
->focus
);
3645 /* If this window directs focus to a child window, then clear. */
3646 if (win
->focus_child
)
3647 win
->focus_child
= NULL
;
3649 /* If transient, adjust parent's focus child for focus_magic. */
3651 parent
= find_window(win
->transient
);
3652 if (parent
&& parent
->focus_child
!= win
)
3653 parent
->focus_child
= win
;
3656 if (cfw
!= win
&& ws
->r
!= NULL
) {
3657 /* Set input focus if no input hint, or indicated by hint. */
3658 if (!(win
->hints
.flags
& XCB_ICCCM_WM_HINT_INPUT
) ||
3659 (win
->hints
.flags
& XCB_ICCCM_WM_HINT_INPUT
&&
3661 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
,
3662 win
->id
, last_event_time
);
3664 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
,
3665 ws
->r
->id
, XCB_CURRENT_TIME
);
3667 /* Tell app it can adjust focus to a specific window. */
3668 if (win
->take_focus
) {
3669 /* java is special; always tell parent */
3670 if (TRANS(win
) && win
->java
)
3671 client_msg(parent
, a_takefocus
,
3674 client_msg(win
, a_takefocus
, last_event_time
);
3677 if (ws
->cur_layout
->flags
& SWM_L_MAPONFOCUS
||
3679 /* If a parent exists, map it first. */
3681 raise_window(parent
);
3684 /* Map siblings next. */
3685 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
,
3687 if (w
!= win
&& !ICONIC(w
) &&
3688 w
->transient
== parent
->id
) {
3694 /* Map focused window. */
3698 /* Stack any children of focus window. */
3699 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
, tmpw
)
3700 if (w
->transient
== win
->id
&& !ICONIC(w
)) {
3704 } else if (tile_gap
< 0 && !ABOVE(win
)) {
3706 * Windows overlap in the layout.
3707 * Raise focused win above all tiled wins.
3715 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
3716 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1,
3721 /* Update window border even if workspace is hidden. */
3722 update_window_color(win
);
3727 DNPRINTF(SWM_D_FOCUS
, "focus_win: done.\n");
3730 /* If a child window should have focus instead, return it. */
3732 get_focus_magic(struct ws_win
*win
)
3734 struct ws_win
*parent
= NULL
;
3735 struct ws_win
*child
= NULL
;
3737 DNPRINTF(SWM_D_FOCUS
, "get_focus_magic: win %#x\n", WINID(win
));
3742 parent
= find_window(win
->transient
);
3744 /* If parent prefers focus elsewhere, then try to do so. */
3745 if (parent
&& (child
= parent
->focus_child
)) {
3746 if (validate_win(child
) == 0 && child
->mapped
)
3749 parent
->focus_child
= NULL
;
3753 /* If this window prefers focus elsewhere, then try to do so. */
3754 if ((child
= win
->focus_child
)) {
3755 if (validate_win(child
) == 0 && child
->mapped
)
3758 win
->focus_child
= NULL
;
3765 event_drain(uint8_t rt
)
3767 xcb_generic_event_t
*evt
;
3769 /* ensure all pending requests have been processed before filtering. */
3771 while ((evt
= xcb_poll_for_event(conn
))) {
3772 if (XCB_EVENT_RESPONSE_TYPE(evt
) != rt
)
3780 set_region(struct swm_region
*r
)
3782 struct swm_region
*rf
;
3789 /* Unfocus old region bar. */
3794 xcb_change_window_attributes(conn
, rf
->bar
->id
,
3795 XCB_CW_BORDER_PIXEL
,
3796 &r
->s
->c
[SWM_S_COLOR_BAR_BORDER_UNFOCUS
].pixel
);
3799 if (rf
!= NULL
&& rf
!= r
&& (X(rf
) != X(r
) || Y(rf
) != Y(r
) ||
3800 WIDTH(rf
) != WIDTH(r
) || HEIGHT(rf
) != HEIGHT(r
))) {
3801 /* Set _NET_DESKTOP_GEOMETRY. */
3803 vals
[1] = HEIGHT(r
);
3804 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, r
->s
->root
,
3805 ewmh
[_NET_DESKTOP_GEOMETRY
].atom
, XCB_ATOM_CARDINAL
, 32, 2,
3809 /* Set region bar border to focus_color. */
3810 xcb_change_window_attributes(conn
, r
->bar
->id
,
3811 XCB_CW_BORDER_PIXEL
, &r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].pixel
);
3815 ewmh_update_current_desktop();
3819 focus_region(struct swm_region
*r
)
3822 struct swm_region
*old_r
;
3827 old_r
= r
->s
->r_focus
;
3830 nfw
= get_region_focus(r
);
3834 /* New region is empty; need to manually unfocus win. */
3836 unfocus_win(old_r
->ws
->focus
);
3838 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
, r
->id
,
3841 /* Clear bar since empty. */
3847 switchws(struct swm_region
*r
, union arg
*args
)
3849 struct swm_region
*this_r
, *other_r
;
3851 struct workspace
*new_ws
, *old_ws
;
3852 xcb_window_t none
= XCB_WINDOW_NONE
;
3853 int wsid
= args
->id
;
3854 bool unmap_old
= false;
3859 if (wsid
>= workspace_limit
)
3863 old_ws
= this_r
->ws
;
3864 new_ws
= &this_r
->s
->ws
[wsid
];
3866 DNPRINTF(SWM_D_WS
, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
3867 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), old_ws
->idx
, wsid
);
3869 if (new_ws
== NULL
|| old_ws
== NULL
)
3871 if (new_ws
== old_ws
)
3874 if ((win
= old_ws
->focus
) != NULL
) {
3875 update_window_color(win
);
3877 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
3878 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1,
3882 other_r
= new_ws
->r
;
3883 if (other_r
== NULL
) {
3884 /* the other workspace is hidden, hide this one */
3888 /* the other ws is visible in another region, exchange them */
3889 other_r
->ws_prior
= new_ws
;
3890 other_r
->ws
= old_ws
;
3891 old_ws
->r
= other_r
;
3893 this_r
->ws_prior
= old_ws
;
3894 this_r
->ws
= new_ws
;
3897 /* Set focus_pending before stacking, if needed. */
3898 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& (!new_ws
->focus_pending
||
3899 validate_win(new_ws
->focus_pending
))) {
3900 new_ws
->focus_pending
= get_region_focus(new_ws
->r
);
3901 new_ws
->focus
= new_ws
->focus_prev
;
3902 new_ws
->focus_prev
= NULL
;
3905 new_ws
->state
= SWM_WS_STATE_MAPPING
;
3908 /* unmap old windows */
3910 TAILQ_FOREACH(win
, &old_ws
->winlist
, entry
)
3912 old_ws
->state
= SWM_WS_STATE_HIDDEN
;
3915 /* if workspaces were swapped, then don't wait to set focus */
3916 if (old_ws
->r
&& focus_mode
!= SWM_FOCUS_FOLLOW
) {
3917 if (new_ws
->focus_pending
) {
3918 focus_win(new_ws
->focus_pending
);
3919 new_ws
->focus_pending
= NULL
;
3923 /* Clear bar and set focus on region input win if new ws is empty. */
3924 if (new_ws
->focus_pending
== NULL
&& new_ws
->focus
== NULL
) {
3925 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
, r
->id
,
3930 ewmh_update_current_desktop();
3934 new_ws
->state
= SWM_WS_STATE_MAPPED
;
3936 DNPRINTF(SWM_D_WS
, "switchws: done.\n");
3940 cyclews(struct swm_region
*r
, union arg
*args
)
3943 struct swm_screen
*s
= r
->s
;
3944 bool cycle_all
= false, mv
= false;
3946 DNPRINTF(SWM_D_WS
, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3947 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
3953 case SWM_ARG_ID_CYCLEWS_MOVE_UP
:
3956 case SWM_ARG_ID_CYCLEWS_UP_ALL
:
3959 case SWM_ARG_ID_CYCLEWS_UP
:
3960 a
.id
= (a
.id
< workspace_limit
- 1) ? a
.id
+ 1 : 0;
3962 case SWM_ARG_ID_CYCLEWS_MOVE_DOWN
:
3965 case SWM_ARG_ID_CYCLEWS_DOWN_ALL
:
3968 case SWM_ARG_ID_CYCLEWS_DOWN
:
3969 a
.id
= (a
.id
> 0) ? a
.id
- 1 : workspace_limit
- 1;
3976 (!cycle_empty
&& TAILQ_EMPTY(&s
->ws
[a
.id
].winlist
)))
3978 if (!cycle_visible
&& s
->ws
[a
.id
].r
!= NULL
)
3985 } while (a
.id
!= r
->ws
->idx
);
3987 DNPRINTF(SWM_D_FOCUS
, "cyclews: done\n");
3991 priorws(struct swm_region
*r
, union arg
*args
)
3997 DNPRINTF(SWM_D_WS
, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3998 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
4000 if (r
->ws_prior
== NULL
)
4003 a
.id
= r
->ws_prior
->idx
;
4005 DNPRINTF(SWM_D_FOCUS
, "priorws: done\n");
4009 focusrg(struct swm_region
*r
, union arg
*args
)
4011 int ridx
= args
->id
, i
, num_screens
;
4012 struct swm_region
*rr
= NULL
;
4014 num_screens
= get_screen_count();
4015 /* do nothing if we don't have more than one screen */
4016 if (!(num_screens
> 1 || outputs
> 1))
4019 DNPRINTF(SWM_D_FOCUS
, "focusrg: id: %d\n", ridx
);
4021 rr
= TAILQ_FIRST(&r
->s
->rl
);
4022 for (i
= 0; i
< ridx
&& rr
!= NULL
; ++i
)
4023 rr
= TAILQ_NEXT(rr
, entry
);
4031 DNPRINTF(SWM_D_FOCUS
, "focusrg: done\n");
4035 cyclerg(struct swm_region
*r
, union arg
*args
)
4037 struct swm_region
*rr
= NULL
;
4040 num_screens
= get_screen_count();
4041 /* do nothing if we don't have more than one screen */
4042 if (!(num_screens
> 1 || outputs
> 1))
4046 DNPRINTF(SWM_D_FOCUS
, "cyclerg: id: %d, region: %d\n", args
->id
, i
);
4049 case SWM_ARG_ID_CYCLERG_UP
:
4050 rr
= TAILQ_NEXT(r
, entry
);
4052 rr
= TAILQ_FIRST(&screens
[i
].rl
);
4054 case SWM_ARG_ID_CYCLERG_DOWN
:
4055 rr
= TAILQ_PREV(r
, swm_region_list
, entry
);
4057 rr
= TAILQ_LAST(&screens
[i
].rl
, swm_region_list
);
4068 DNPRINTF(SWM_D_FOCUS
, "cyclerg: done\n");
4071 /* Sorts transients after parent. */
4073 sort_windows(struct ws_win_list
*wl
)
4075 struct ws_win
*win
, *parent
, *nxt
;
4080 for (win
= TAILQ_FIRST(wl
); win
!= TAILQ_END(wl
); win
= nxt
) {
4081 nxt
= TAILQ_NEXT(win
, entry
);
4083 parent
= find_window(win
->transient
);
4084 if (parent
== NULL
) {
4085 warnx("not possible bug");
4088 TAILQ_REMOVE(wl
, win
, entry
);
4089 TAILQ_INSERT_AFTER(wl
, parent
, win
, entry
);
4095 swapwin(struct swm_region
*r
, union arg
*args
)
4097 struct ws_win
*target
, *source
;
4098 struct ws_win
*cur_focus
;
4099 struct ws_win_list
*wl
;
4101 DNPRINTF(SWM_D_WS
, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
4102 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
4104 cur_focus
= r
->ws
->focus
;
4105 if (cur_focus
== NULL
|| FULLSCREEN(cur_focus
))
4108 /* Adjust stacking in floating layer. */
4109 if (ABOVE(cur_focus
)) {
4111 case SWM_ARG_ID_SWAPPREV
:
4112 target
= TAILQ_PREV(cur_focus
, ws_win_stack
,
4114 if (target
!= NULL
&& FLOATING(target
)) {
4115 TAILQ_REMOVE(&cur_focus
->ws
->stack
, cur_focus
,
4117 TAILQ_INSERT_BEFORE(target
, cur_focus
,
4119 update_win_stacking(cur_focus
);
4123 case SWM_ARG_ID_SWAPNEXT
:
4124 target
= TAILQ_NEXT(cur_focus
, stack_entry
);
4125 if (target
!= NULL
&& FLOATING(target
)) {
4126 TAILQ_REMOVE(&cur_focus
->ws
->stack
, cur_focus
,
4128 TAILQ_INSERT_AFTER(&cur_focus
->ws
->stack
,
4129 target
, cur_focus
, stack_entry
);
4130 update_win_stacking(cur_focus
);
4138 if (r
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
4141 clear_maximized(r
->ws
);
4144 wl
= &source
->ws
->winlist
;
4147 case SWM_ARG_ID_SWAPPREV
:
4149 source
= find_window(source
->transient
);
4150 target
= TAILQ_PREV(source
, ws_win_list
, entry
);
4151 if (target
&& target
->transient
)
4152 target
= find_window(target
->transient
);
4153 TAILQ_REMOVE(wl
, source
, entry
);
4155 TAILQ_INSERT_TAIL(wl
, source
, entry
);
4157 TAILQ_INSERT_BEFORE(target
, source
, entry
);
4159 case SWM_ARG_ID_SWAPNEXT
:
4160 target
= TAILQ_NEXT(source
, entry
);
4161 /* move the parent and let the sort handle the move */
4163 source
= find_window(source
->transient
);
4164 TAILQ_REMOVE(wl
, source
, entry
);
4166 TAILQ_INSERT_HEAD(wl
, source
, entry
);
4168 TAILQ_INSERT_AFTER(wl
, target
, source
, entry
);
4170 case SWM_ARG_ID_SWAPMAIN
:
4171 target
= TAILQ_FIRST(wl
);
4172 if (target
== source
) {
4173 if (source
->ws
->focus_prev
!= NULL
&&
4174 source
->ws
->focus_prev
!= target
)
4175 source
= source
->ws
->focus_prev
;
4179 if (target
== NULL
|| source
== NULL
)
4181 source
->ws
->focus_prev
= target
;
4182 TAILQ_REMOVE(wl
, target
, entry
);
4183 TAILQ_INSERT_BEFORE(source
, target
, entry
);
4184 TAILQ_REMOVE(wl
, source
, entry
);
4185 TAILQ_INSERT_HEAD(wl
, source
, entry
);
4187 case SWM_ARG_ID_MOVELAST
:
4188 TAILQ_REMOVE(wl
, source
, entry
);
4189 TAILQ_INSERT_TAIL(wl
, source
, entry
);
4192 DNPRINTF(SWM_D_MOVE
, "swapwin: invalid id: %d\n", args
->id
);
4197 ewmh_update_client_list();
4203 DNPRINTF(SWM_D_MOVE
, "swapwin: done\n");
4207 get_focus_prev(struct ws_win
*win
)
4209 struct ws_win
*winfocus
= NULL
;
4210 struct ws_win
*cur_focus
= NULL
;
4211 struct ws_win_list
*wl
= NULL
;
4212 struct workspace
*ws
= NULL
;
4214 if (!(win
&& win
->ws
))
4219 cur_focus
= ws
->focus
;
4221 DNPRINTF(SWM_D_FOCUS
, "get_focus_prev: win %#x, cur_focus: %#x, "
4222 "focus_prev: %#x\n", WINID(win
), WINID(cur_focus
),
4223 WINID(ws
->focus_prev
));
4225 /* pickle, just focus on whatever */
4226 if (cur_focus
== NULL
) {
4227 /* use prev_focus if valid */
4228 if (ws
->focus_prev
&& find_window(ws
->focus_prev
->id
))
4229 winfocus
= ws
->focus_prev
;
4233 /* if transient focus on parent */
4234 if (TRANS(cur_focus
)) {
4235 winfocus
= find_window(cur_focus
->transient
);
4239 /* if in max_stack try harder */
4240 if ((win
->quirks
& SWM_Q_FOCUSPREV
) ||
4241 (ws
->cur_layout
->flags
& SWM_L_FOCUSPREV
)) {
4242 if (cur_focus
!= ws
->focus_prev
)
4243 winfocus
= ws
->focus_prev
;
4245 winfocus
= TAILQ_PREV(win
, ws_win_list
, entry
);
4250 DNPRINTF(SWM_D_FOCUS
, "get_focus_prev: focus_close: %d\n", focus_close
);
4252 if (winfocus
== NULL
|| winfocus
== win
) {
4253 switch (focus_close
) {
4254 case SWM_STACK_BOTTOM
:
4255 TAILQ_FOREACH(winfocus
, wl
, entry
)
4256 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4260 TAILQ_FOREACH_REVERSE(winfocus
, wl
, ws_win_list
, entry
)
4261 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4264 case SWM_STACK_ABOVE
:
4265 winfocus
= TAILQ_NEXT(cur_focus
, entry
);
4266 while (winfocus
&& ICONIC(winfocus
))
4267 winfocus
= TAILQ_NEXT(winfocus
, entry
);
4269 if (winfocus
== NULL
) {
4270 if (focus_close_wrap
) {
4271 TAILQ_FOREACH(winfocus
, wl
, entry
)
4272 if (!ICONIC(winfocus
) &&
4273 winfocus
!= cur_focus
)
4276 TAILQ_FOREACH_REVERSE(winfocus
, wl
,
4278 if (!ICONIC(winfocus
) &&
4279 winfocus
!= cur_focus
)
4284 case SWM_STACK_BELOW
:
4285 winfocus
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
4286 while (winfocus
&& ICONIC(winfocus
))
4287 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
,
4290 if (winfocus
== NULL
) {
4291 if (focus_close_wrap
) {
4292 TAILQ_FOREACH_REVERSE(winfocus
, wl
,
4294 if (!ICONIC(winfocus
) &&
4295 winfocus
!= cur_focus
)
4298 TAILQ_FOREACH(winfocus
, wl
, entry
)
4299 if (!ICONIC(winfocus
) &&
4300 winfocus
!= cur_focus
)
4308 if (winfocus
== NULL
||
4309 (winfocus
&& (ICONIC(winfocus
) || winfocus
== cur_focus
))) {
4310 if (focus_default
== SWM_STACK_TOP
) {
4311 TAILQ_FOREACH_REVERSE(winfocus
, wl
, ws_win_list
, entry
)
4312 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4315 TAILQ_FOREACH(winfocus
, wl
, entry
)
4316 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4323 return get_focus_magic(winfocus
);
4327 get_region_focus(struct swm_region
*r
)
4329 struct ws_win
*winfocus
= NULL
;
4334 if (r
->ws
->focus
&& !ICONIC(r
->ws
->focus
))
4335 winfocus
= r
->ws
->focus
;
4336 else if (r
->ws
->focus_prev
&& !ICONIC(r
->ws
->focus_prev
))
4337 winfocus
= r
->ws
->focus_prev
;
4339 TAILQ_FOREACH(winfocus
, &r
->ws
->winlist
, entry
)
4340 if (!ICONIC(winfocus
))
4343 return get_focus_magic(winfocus
);
4347 focus(struct swm_region
*r
, union arg
*args
)
4349 struct ws_win
*head
, *cur_focus
= NULL
, *winfocus
= NULL
;
4350 struct ws_win_list
*wl
= NULL
;
4351 struct workspace
*ws
= NULL
;
4358 cur_focus
= r
->ws
->focus
;
4362 DNPRINTF(SWM_D_FOCUS
, "focus: id: %d, cur_focus: %#x\n", args
->id
,
4365 /* Make sure an uniconified window has focus, if one exists. */
4366 if (cur_focus
== NULL
) {
4367 cur_focus
= TAILQ_FIRST(wl
);
4368 while (cur_focus
!= NULL
&& ICONIC(cur_focus
))
4369 cur_focus
= TAILQ_NEXT(cur_focus
, entry
);
4371 DNPRINTF(SWM_D_FOCUS
, "focus: new cur_focus: %#x\n",
4376 case SWM_ARG_ID_FOCUSPREV
:
4377 if (cur_focus
== NULL
)
4380 winfocus
= cur_focus
;
4382 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
, entry
);
4383 if (winfocus
== NULL
)
4384 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
4385 if (winfocus
== cur_focus
)
4387 } while (winfocus
&& (ICONIC(winfocus
) ||
4388 winfocus
->id
== cur_focus
->transient
||
4389 (cur_focus
->transient
!= XCB_WINDOW_NONE
&&
4390 winfocus
->transient
== cur_focus
->transient
)));
4392 case SWM_ARG_ID_FOCUSNEXT
:
4393 if (cur_focus
== NULL
)
4396 winfocus
= cur_focus
;
4398 winfocus
= TAILQ_NEXT(winfocus
, entry
);
4399 if (winfocus
== NULL
)
4400 winfocus
= TAILQ_FIRST(wl
);
4401 if (winfocus
== cur_focus
)
4403 } while (winfocus
&& (ICONIC(winfocus
) ||
4404 winfocus
->id
== cur_focus
->transient
||
4405 (cur_focus
->transient
!= XCB_WINDOW_NONE
&&
4406 winfocus
->transient
== cur_focus
->transient
)));
4408 case SWM_ARG_ID_FOCUSMAIN
:
4409 if (cur_focus
== NULL
)
4412 winfocus
= TAILQ_FIRST(wl
);
4413 if (winfocus
== cur_focus
)
4414 winfocus
= cur_focus
->ws
->focus_prev
;
4416 case SWM_ARG_ID_FOCUSURGENT
:
4417 /* Search forward for the next urgent window. */
4421 for (i
= 0; i
<= workspace_limit
; ++i
) {
4423 head
= TAILQ_FIRST(&r
->s
->ws
[(ws
->idx
+ i
) %
4424 workspace_limit
].winlist
);
4427 if (head
== cur_focus
) {
4429 winfocus
= cur_focus
;
4432 } else if (get_urgent(head
)) {
4437 head
= TAILQ_NEXT(head
, entry
);
4444 /* Switch ws if new focus is on a different ws. */
4445 if (winfocus
&& winfocus
->ws
!= ws
) {
4446 a
.id
= winfocus
->ws
->idx
;
4454 if (clear_maximized(ws
) > 0)
4457 focus_win(get_focus_magic(winfocus
));
4462 DNPRINTF(SWM_D_FOCUS
, "focus: done\n");
4466 cycle_layout(struct swm_region
*r
, union arg
*args
)
4468 struct workspace
*ws
= r
->ws
;
4470 /* suppress unused warning since var is needed */
4473 DNPRINTF(SWM_D_EVENT
, "cycle_layout: workspace: %d\n", ws
->idx
);
4476 if (ws
->cur_layout
->l_stack
== NULL
)
4477 ws
->cur_layout
= &layouts
[0];
4479 clear_maximized(ws
);
4484 focus_win(get_region_focus(r
));
4491 stack_config(struct swm_region
*r
, union arg
*args
)
4493 struct workspace
*ws
= r
->ws
;
4495 DNPRINTF(SWM_D_STACK
, "stack_config: id: %d workspace: %d\n",
4498 if (clear_maximized(ws
) > 0)
4501 if (ws
->cur_layout
->l_config
!= NULL
)
4502 ws
->cur_layout
->l_config(ws
, args
->id
);
4504 if (args
->id
!= SWM_ARG_ID_STACKINIT
)
4514 struct swm_geometry g
;
4515 struct swm_region
*r
, *r_prev
= NULL
;
4522 DNPRINTF(SWM_D_STACK
, "stack: begin\n");
4524 num_screens
= get_screen_count();
4525 for (i
= 0; i
< num_screens
; i
++) {
4529 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
4530 /* Adjust stack area for region bar and padding. */
4532 g
.x
+= region_padding
;
4533 g
.y
+= region_padding
;
4534 g
.w
-= 2 * border_width
+ 2 * region_padding
;
4535 g
.h
-= 2 * border_width
+ 2 * region_padding
;
4536 if (bar_enabled
&& r
->ws
->bar_enabled
) {
4542 DNPRINTF(SWM_D_STACK
, "stack: workspace: %d (screen: "
4543 "%d, region: %d), (x,y) WxH: (%d,%d) %d x %d\n",
4544 r
->ws
->idx
, i
, j
++, g
.x
, g
.y
, g
.w
, g
.h
);
4547 /* Stack bar/input relative to prev. region. */
4548 val
[1] = XCB_STACK_MODE_ABOVE
;
4550 val
[0] = r_prev
->id
;
4551 DNPRINTF(SWM_D_STACK
, "stack: region input %#x "
4552 "relative to %#x.\n", r
->id
, val
[0]);
4553 xcb_configure_window(conn
, r
->id
,
4554 XCB_CONFIG_WINDOW_SIBLING
|
4555 XCB_CONFIG_WINDOW_STACK_MODE
, val
);
4557 val
[0] = r_prev
->bar
->id
;
4558 DNPRINTF(SWM_D_STACK
, "stack: region bar %#x "
4559 "relative to %#x.\n", r
->bar
->id
, val
[0]);
4560 xcb_configure_window(conn
, r
->bar
->id
,
4561 XCB_CONFIG_WINDOW_SIBLING
|
4562 XCB_CONFIG_WINDOW_STACK_MODE
, val
);
4565 r
->ws
->cur_layout
->l_stack(r
->ws
, &g
);
4566 r
->ws
->cur_layout
->l_string(r
->ws
);
4567 /* save r so we can track region changes */
4575 DNPRINTF(SWM_D_STACK
, "stack: end\n");
4579 store_float_geom(struct ws_win
*win
)
4581 if (win
== NULL
|| win
->ws
->r
== NULL
)
4584 /* retain window geom and region geom */
4585 win
->g_float
= win
->g
;
4586 win
->g_float
.x
-= X(win
->ws
->r
);
4587 win
->g_float
.y
-= Y(win
->ws
->r
);
4588 win
->g_floatvalid
= true;
4589 DNPRINTF(SWM_D_MISC
, "store_float_geom: win %#x, g: (%d,%d)"
4590 " %d x %d, g_float: (%d,%d) %d x %d\n", win
->id
, X(win
), Y(win
),
4591 WIDTH(win
), HEIGHT(win
), win
->g_float
.x
, win
->g_float
.y
,
4592 win
->g_float
.w
, win
->g_float
.h
);
4596 load_float_geom(struct ws_win
*win
)
4598 if (win
== NULL
|| win
->ws
->r
== NULL
)
4601 if (win
->g_floatvalid
) {
4602 win
->g
= win
->g_float
;
4603 X(win
) += X(win
->ws
->r
);
4604 Y(win
) += Y(win
->ws
->r
);
4605 DNPRINTF(SWM_D_MISC
, "load_float_geom: win %#x, g: (%d,%d)"
4606 "%d x %d\n", win
->id
, X(win
), Y(win
), WIDTH(win
),
4609 DNPRINTF(SWM_D_MISC
, "load_float_geom: win %#x, g_float "
4610 "is not set.\n", win
->id
);
4615 update_floater(struct ws_win
*win
)
4617 struct workspace
*ws
;
4618 struct swm_region
*r
;
4625 if ((r
= ws
->r
) == NULL
)
4628 DNPRINTF(SWM_D_MISC
, "update_floater: win %#x\n", win
->id
);
4630 win
->bordered
= true;
4632 if (FULLSCREEN(win
)) {
4633 /* _NET_WM_FULLSCREEN: fullscreen without border. */
4634 if (!win
->g_floatvalid
)
4635 store_float_geom(win
);
4638 win
->bordered
= false;
4639 } else if (MAXIMIZED(win
)) {
4640 /* Maximize: like a single stacked window. */
4641 if (!win
->g_floatvalid
)
4642 store_float_geom(win
);
4646 if (bar_enabled
&& ws
->bar_enabled
) {
4648 Y(win
) += bar_height
;
4649 HEIGHT(win
) -= bar_height
;
4650 } else if (disable_border
) {
4651 win
->bordered
= false;
4654 if (win
->bordered
) {
4655 HEIGHT(win
) -= 2 * border_width
;
4656 WIDTH(win
) -= 2 * border_width
;
4659 /* Normal floating window. */
4660 /* Update geometry if new region. */
4662 load_float_geom(win
);
4664 if ((win
->quirks
& SWM_Q_FULLSCREEN
) &&
4665 WIDTH(win
) >= WIDTH(r
) && HEIGHT(win
) >= HEIGHT(r
)) {
4666 /* Remove border for FULLSCREEN quirk. */
4667 win
->bordered
= false;
4668 } else if (!MANUAL(win
)) {
4669 if (TRANS(win
) && (win
->quirks
& SWM_Q_TRANSSZ
)) {
4670 /* Adjust size on TRANSSZ quirk. */
4671 WIDTH(win
) = (double)WIDTH(r
) * dialog_ratio
;
4672 HEIGHT(win
) = (double)HEIGHT(r
) * dialog_ratio
;
4675 if (!(win
->quirks
& SWM_Q_ANYWHERE
)) {
4677 * Floaters and transients are auto-centred
4678 * unless manually moved, resized or ANYWHERE
4681 X(win
) = X(r
) + (WIDTH(r
) - WIDTH(win
)) / 2 -
4683 Y(win
) = Y(r
) + (HEIGHT(r
) - HEIGHT(win
)) / 2 -
4685 store_float_geom(win
);
4690 /* Ensure at least 1 pixel of the window is in the region. */
4691 region_containment(win
, r
, SWM_CW_ALLSIDES
);
4697 * Send keystrokes to terminal to decrease/increase the font size as the
4698 * window size changes.
4701 adjust_font(struct ws_win
*win
)
4703 if (!(win
->quirks
& SWM_Q_XTERM_FONTADJ
) ||
4704 ABOVE(win
) || TRANS(win
))
4707 if (win
->sh
.width_inc
&& win
->last_inc
!= win
->sh
.width_inc
&&
4708 WIDTH(win
) / win
->sh
.width_inc
< term_width
&&
4709 win
->font_steps
< SWM_MAX_FONT_STEPS
) {
4710 win
->font_size_boundary
[win
->font_steps
] =
4711 (win
->sh
.width_inc
* term_width
) + win
->sh
.base_width
;
4714 win
->last_inc
= win
->sh
.width_inc
;
4715 fake_keypress(win
, XK_KP_Subtract
, XCB_MOD_MASK_SHIFT
);
4716 } else if (win
->font_steps
&& win
->last_inc
!= win
->sh
.width_inc
&&
4717 WIDTH(win
) > win
->font_size_boundary
[win
->font_steps
- 1]) {
4720 win
->last_inc
= win
->sh
.width_inc
;
4721 fake_keypress(win
, XK_KP_Add
, XCB_MOD_MASK_SHIFT
);
4725 #define SWAPXY(g) do { \
4727 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
4728 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
4731 stack_master(struct workspace
*ws
, struct swm_geometry
*g
, int rot
, bool flip
)
4733 struct swm_geometry win_g
, r_g
= *g
;
4735 int i
, j
, s
, stacks
;
4736 int w_inc
= 1, h_inc
, w_base
= 1, h_base
;
4737 int hrh
, extra
= 0, h_slice
, last_h
= 0;
4738 int split
, colno
, winno
, mwin
, msize
, mscale
;
4739 int remain
, missing
, v_slice
;
4740 bool bordered
= true, reconfigure
= false;
4742 DNPRINTF(SWM_D_STACK
, "stack_master: workspace: %d, rot: %s, "
4743 "flip: %s\n", ws
->idx
, YESNO(rot
), YESNO(flip
));
4745 /* Prepare tiling variables, if needed. */
4746 if ((winno
= count_win(ws
, false)) > 0) {
4747 /* Find first tiled window. */
4748 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
4749 if (!FLOATING(win
) && !ICONIC(win
))
4752 /* Take into account size hints of first tiled window. */
4754 w_inc
= win
->sh
.width_inc
;
4755 w_base
= win
->sh
.base_width
;
4756 mwin
= ws
->l_state
.horizontal_mwin
;
4757 mscale
= ws
->l_state
.horizontal_msize
;
4758 stacks
= ws
->l_state
.horizontal_stacks
;
4761 w_inc
= win
->sh
.height_inc
;
4762 w_base
= win
->sh
.base_height
;
4763 mwin
= ws
->l_state
.vertical_mwin
;
4764 mscale
= ws
->l_state
.vertical_msize
;
4765 stacks
= ws
->l_state
.vertical_stacks
;
4769 if (stacks
> winno
- mwin
)
4770 stacks
= winno
- mwin
;
4774 h_slice
= r_g
.h
/ SWM_H_SLICE
;
4775 if (mwin
&& winno
> mwin
) {
4776 v_slice
= r_g
.w
/ SWM_V_SLICE
;
4780 win_g
.w
= v_slice
* mscale
;
4782 if (w_inc
> 1 && w_inc
< v_slice
) {
4783 /* Adjust for requested size increment. */
4784 remain
= (win_g
.w
- w_base
) % w_inc
;
4790 win_g
.x
+= r_g
.w
- msize
;
4793 colno
= split
= winno
/ stacks
;
4794 win_g
.w
= ((r_g
.w
- (stacks
* 2 * border_width
) +
4795 2 * border_width
) / stacks
);
4797 hrh
= r_g
.h
/ colno
;
4798 extra
= r_g
.h
- (colno
* hrh
);
4799 win_g
.h
= hrh
- 2 * border_width
;
4800 i
= j
= 0, s
= stacks
;
4803 /* Update window geometry. */
4804 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
4808 if (FLOATING(win
)) {
4809 update_floater(win
);
4814 if (split
&& i
== split
) {
4815 colno
= (winno
- mwin
) / stacks
;
4816 if (s
<= (winno
- mwin
) % stacks
)
4819 hrh
= r_g
.h
/ colno
;
4820 extra
= r_g
.h
- (colno
* hrh
);
4823 win_g
.x
+= win_g
.w
+ 2 * border_width
+
4826 win_g
.w
= (r_g
.w
- msize
-
4827 (stacks
* (2 * border_width
+ tile_gap
))) / stacks
;
4829 win_g
.w
+= (r_g
.w
- msize
-
4830 (stacks
* (2 * border_width
+ tile_gap
))) %
4834 win_g
.x
-= win_g
.w
+ 2 * border_width
+
4840 win_g
.h
= hrh
- 2 * border_width
- tile_gap
;
4843 h_inc
= win
->sh
.width_inc
;
4844 h_base
= win
->sh
.base_width
;
4846 h_inc
= win
->sh
.height_inc
;
4847 h_base
= win
->sh
.base_height
;
4850 if (j
== colno
- 1) {
4851 win_g
.h
= hrh
+ extra
;
4852 } else if (h_inc
> 1 && h_inc
< h_slice
) {
4853 /* adjust for window's requested size increment */
4854 remain
= (win_g
.h
- h_base
) % h_inc
;
4855 missing
= h_inc
- remain
;
4857 if (missing
<= extra
|| j
== 0) {
4869 win_g
.y
+= last_h
+ 2 * border_width
+ tile_gap
;
4871 if (disable_border
&& !(bar_enabled
&& ws
->bar_enabled
) &&
4874 win_g
.w
+= 2 * border_width
;
4875 win_g
.h
+= 2 * border_width
;
4881 if (X(win
) != win_g
.y
|| Y(win
) != win_g
.x
||
4882 WIDTH(win
) != win_g
.h
|| HEIGHT(win
) != win_g
.w
) {
4886 WIDTH(win
) = win_g
.h
;
4887 HEIGHT(win
) = win_g
.w
;
4890 if (X(win
) != win_g
.x
|| Y(win
) != win_g
.y
||
4891 WIDTH(win
) != win_g
.w
|| HEIGHT(win
) != win_g
.h
) {
4895 WIDTH(win
) = win_g
.w
;
4896 HEIGHT(win
) = win_g
.h
;
4900 if (bordered
!= win
->bordered
) {
4902 win
->bordered
= bordered
;
4915 /* Stack all windows from bottom up. */
4916 TAILQ_FOREACH_REVERSE(win
, &ws
->stack
, ws_win_stack
, stack_entry
)
4918 update_win_stacking(win
);
4920 /* Map all windows from top down. */
4921 TAILQ_FOREACH(win
, &ws
->stack
, stack_entry
)
4925 DNPRINTF(SWM_D_STACK
, "stack_master: done\n");
4929 vertical_config(struct workspace
*ws
, int id
)
4931 DNPRINTF(SWM_D_STACK
, "vertical_config: id: %d, workspace: %d\n",
4935 case SWM_ARG_ID_STACKRESET
:
4936 case SWM_ARG_ID_STACKINIT
:
4937 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ 2;
4938 ws
->l_state
.vertical_mwin
= 1;
4939 ws
->l_state
.vertical_stacks
= 1;
4941 case SWM_ARG_ID_MASTERSHRINK
:
4942 if (ws
->l_state
.vertical_msize
> 1)
4943 ws
->l_state
.vertical_msize
--;
4945 case SWM_ARG_ID_MASTERGROW
:
4946 if (ws
->l_state
.vertical_msize
< SWM_V_SLICE
- 1)
4947 ws
->l_state
.vertical_msize
++;
4949 case SWM_ARG_ID_MASTERADD
:
4950 ws
->l_state
.vertical_mwin
++;
4952 case SWM_ARG_ID_MASTERDEL
:
4953 if (ws
->l_state
.vertical_mwin
> 0)
4954 ws
->l_state
.vertical_mwin
--;
4956 case SWM_ARG_ID_STACKINC
:
4957 ws
->l_state
.vertical_stacks
++;
4959 case SWM_ARG_ID_STACKDEC
:
4960 if (ws
->l_state
.vertical_stacks
> 1)
4961 ws
->l_state
.vertical_stacks
--;
4963 case SWM_ARG_ID_FLIPLAYOUT
:
4964 ws
->l_state
.vertical_flip
= !ws
->l_state
.vertical_flip
;
4972 vertical_stack(struct workspace
*ws
, struct swm_geometry
*g
)
4974 DNPRINTF(SWM_D_STACK
, "vertical_stack: workspace: %d\n", ws
->idx
);
4976 stack_master(ws
, g
, 0, ws
->l_state
.vertical_flip
);
4980 horizontal_config(struct workspace
*ws
, int id
)
4982 DNPRINTF(SWM_D_STACK
, "horizontal_config: workspace: %d\n", ws
->idx
);
4985 case SWM_ARG_ID_STACKRESET
:
4986 case SWM_ARG_ID_STACKINIT
:
4987 ws
->l_state
.horizontal_mwin
= 1;
4988 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ 2;
4989 ws
->l_state
.horizontal_stacks
= 1;
4991 case SWM_ARG_ID_MASTERSHRINK
:
4992 if (ws
->l_state
.horizontal_msize
> 1)
4993 ws
->l_state
.horizontal_msize
--;
4995 case SWM_ARG_ID_MASTERGROW
:
4996 if (ws
->l_state
.horizontal_msize
< SWM_H_SLICE
- 1)
4997 ws
->l_state
.horizontal_msize
++;
4999 case SWM_ARG_ID_MASTERADD
:
5000 ws
->l_state
.horizontal_mwin
++;
5002 case SWM_ARG_ID_MASTERDEL
:
5003 if (ws
->l_state
.horizontal_mwin
> 0)
5004 ws
->l_state
.horizontal_mwin
--;
5006 case SWM_ARG_ID_STACKINC
:
5007 ws
->l_state
.horizontal_stacks
++;
5009 case SWM_ARG_ID_STACKDEC
:
5010 if (ws
->l_state
.horizontal_stacks
> 1)
5011 ws
->l_state
.horizontal_stacks
--;
5013 case SWM_ARG_ID_FLIPLAYOUT
:
5014 ws
->l_state
.horizontal_flip
= !ws
->l_state
.horizontal_flip
;
5022 horizontal_stack(struct workspace
*ws
, struct swm_geometry
*g
)
5024 DNPRINTF(SWM_D_STACK
, "horizontal_stack: workspace: %d\n", ws
->idx
);
5026 stack_master(ws
, g
, 1, ws
->l_state
.horizontal_flip
);
5029 /* fullscreen view */
5031 max_stack(struct workspace
*ws
, struct swm_geometry
*g
)
5033 struct swm_geometry gg
= *g
;
5034 struct ws_win
*w
, *win
= NULL
, *parent
= NULL
, *tmpw
;
5037 DNPRINTF(SWM_D_STACK
, "max_stack: workspace: %d\n", ws
->idx
);
5042 winno
= count_win(ws
, false);
5043 if (winno
== 0 && count_win(ws
, true) == 0)
5046 /* Figure out which top level window should be visible. */
5047 if (ws
->focus_pending
)
5048 win
= ws
->focus_pending
;
5051 else if (ws
->focus_prev
)
5052 win
= ws
->focus_prev
;
5054 win
= TAILQ_FIRST(&ws
->winlist
);
5056 DNPRINTF(SWM_D_STACK
, "max_stack: focus_pending: %#x, focus: %#x, "
5057 "focus_prev: %#x, first: %#x, win: %#x\n", WINID(ws
->focus_pending
),
5058 WINID(ws
->focus
), WINID(ws
->focus_prev
),
5059 WINID(TAILQ_FIRST(&ws
->winlist
)), win
->id
);
5061 /* Update window geometry. */
5062 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
5071 /* Set maximized flag for all maxed windows. */
5072 if (!MAXIMIZED(w
)) {
5073 /* Preserve floating geometry. */
5075 store_float_geom(w
);
5077 ewmh_apply_flags(w
, w
->ewmh_flags
| EWMH_F_MAXIMIZED
);
5078 ewmh_update_wm_state(w
);
5081 /* Only reconfigure if necessary. */
5082 if (X(w
) != gg
.x
|| Y(w
) != gg
.y
|| WIDTH(w
) != gg
.w
||
5083 HEIGHT(w
) != gg
.h
) {
5086 if (disable_border
&& !(bar_enabled
&& ws
->bar_enabled
)) {
5087 w
->bordered
= false;
5088 WIDTH(w
) += 2 * border_width
;
5089 HEIGHT(w
) += 2 * border_width
;
5098 /* If transient, stack parent and its children. */
5099 if (TRANS(win
) && (parent
= find_window(win
->transient
))) {
5100 raise_window(parent
);
5102 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
, tmpw
)
5103 if (w
->transient
== parent
->id
)
5107 /* Make sure focus window is on top. */
5110 /* Stack any children of focus window. */
5111 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
, tmpw
)
5112 if (w
->transient
== win
->id
)
5115 /* Map all windows. */
5116 TAILQ_FOREACH(w
, &ws
->stack
, stack_entry
)
5122 send_to_rg(struct swm_region
*r
, union arg
*args
)
5124 int ridx
= args
->id
, i
, num_screens
;
5125 struct swm_region
*rr
= NULL
;
5128 num_screens
= get_screen_count();
5129 /* do nothing if we don't have more than one screen */
5130 if (!(num_screens
> 1 || outputs
> 1))
5133 DNPRINTF(SWM_D_FOCUS
, "send_to_rg: id: %d\n", ridx
);
5135 rr
= TAILQ_FIRST(&r
->s
->rl
);
5136 for (i
= 0; i
< ridx
&& rr
!= NULL
; ++i
)
5137 rr
= TAILQ_NEXT(rr
, entry
);
5148 region_under(struct swm_screen
*s
, int x
, int y
)
5150 struct swm_region
*r
= NULL
;
5152 TAILQ_FOREACH(r
, &s
->rl
, entry
) {
5153 DNPRINTF(SWM_D_MISC
, "region_under: ws: %d, region g: (%d,%d) "
5154 "%d x %d, coords: (%d,%d)\n", r
->ws
->idx
, X(r
), Y(r
),
5155 WIDTH(r
), HEIGHT(r
), x
, y
);
5156 if (X(r
) <= x
&& x
< MAX_X(r
))
5157 if (Y(r
) <= y
&& y
< MAX_Y(r
))
5164 /* Transfer focused window to target workspace and focus. */
5166 send_to_ws(struct swm_region
*r
, union arg
*args
)
5168 int wsid
= args
->id
;
5169 struct ws_win
*win
= NULL
;
5171 if (r
&& r
->ws
&& r
->ws
->focus
)
5176 DNPRINTF(SWM_D_MOVE
, "send_to_ws: win %#x, ws %d\n", win
->id
, wsid
);
5178 if (wsid
< 0 || wsid
>= workspace_limit
)
5181 if (win
->ws
->idx
== wsid
)
5184 win_to_ws(win
, wsid
, true);
5186 /* Set new focus on target ws. */
5187 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
5188 win
->ws
->focus_prev
= win
->ws
->focus
;
5189 win
->ws
->focus
= win
;
5190 win
->ws
->focus_pending
= NULL
;
5192 if (win
->ws
->focus_prev
)
5193 update_window_color(win
->ws
->focus_prev
);
5196 DNPRINTF(SWM_D_STACK
, "send_to_ws: focus_pending: %#x, focus: %#x, "
5197 "focus_prev: %#x, first: %#x, win: %#x\n",
5198 WINID(r
->ws
->focus_pending
), WINID(r
->ws
->focus
),
5199 WINID(r
->ws
->focus_prev
), WINID(TAILQ_FIRST(&r
->ws
->winlist
)),
5202 ewmh_apply_flags(win
, win
->ewmh_flags
& ~EWMH_F_MAXIMIZED
);
5203 ewmh_update_wm_state(win
);
5205 /* Restack and set new focus on current ws. */
5207 load_float_geom(win
);
5211 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
5212 if (r
->ws
->focus
!= NULL
) {
5213 focus_win(r
->ws
->focus
);
5215 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
, r
->id
,
5226 win_to_ws(struct ws_win
*win
, int wsid
, bool unfocus
)
5228 struct ws_win
*parent
;
5229 struct workspace
*ws
, *nws
, *pws
;
5231 if (wsid
< 0 || wsid
>= workspace_limit
)
5234 if (win
->ws
->idx
== wsid
)
5238 nws
= &win
->s
->ws
[wsid
];
5240 DNPRINTF(SWM_D_MOVE
, "win_to_ws: win %#x, ws %d -> %d\n", win
->id
,
5243 /* Cleanup focus on source ws. */
5244 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
5245 (ws
->focus
== win
|| ws
->focus_pending
== win
))
5246 ws
->focus_pending
= get_focus_prev(win
);
5248 /* Move the parent if this is a transient window. */
5250 parent
= find_window(win
->transient
);
5253 /* Set new focus in parent's ws if needed. */
5254 if (pws
->focus
== parent
) {
5255 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
5256 pws
->focus_pending
=
5257 get_focus_prev(parent
);
5259 unfocus_win(parent
);
5261 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
5262 pws
->focus
= pws
->focus_pending
;
5263 pws
->focus_pending
= NULL
;
5267 /* Don't unmap parent if new ws is visible */
5269 unmap_window(parent
);
5272 TAILQ_REMOVE(&ws
->winlist
, parent
, entry
);
5273 TAILQ_REMOVE(&ws
->stack
, parent
, stack_entry
);
5274 TAILQ_INSERT_TAIL(&nws
->winlist
, parent
, entry
);
5275 TAILQ_INSERT_TAIL(&nws
->stack
, parent
, stack_entry
);
5278 DNPRINTF(SWM_D_PROP
, "win_to_ws: set property: "
5279 "_NET_WM_DESKTOP: %d\n", wsid
);
5280 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
5281 parent
->id
, ewmh
[_NET_WM_DESKTOP
].atom
,
5282 XCB_ATOM_CARDINAL
, 32, 1, &wsid
);
5289 if (ws
->focus_prev
== win
)
5290 ws
->focus_prev
= NULL
;
5292 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& ws
->focus_pending
!= NULL
) {
5293 ws
->focus
= ws
->focus_pending
;
5294 ws
->focus_pending
= NULL
;
5297 /* Don't unmap if new ws is visible */
5302 TAILQ_REMOVE(&ws
->winlist
, win
, entry
);
5303 TAILQ_REMOVE(&ws
->stack
, win
, stack_entry
);
5304 TAILQ_INSERT_TAIL(&nws
->winlist
, win
, entry
);
5305 TAILQ_INSERT_TAIL(&nws
->stack
, win
, stack_entry
);
5308 /* Update the window's workspace property: _NET_WM_DESKTOP */
5309 DNPRINTF(SWM_D_PROP
, "win_to_ws: set property: "
5310 "_NET_WM_DESKTOP: %d\n", wsid
);
5311 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
5312 ewmh
[_NET_WM_DESKTOP
].atom
, XCB_ATOM_CARDINAL
, 32, 1, &wsid
);
5314 ewmh_update_client_list();
5316 DNPRINTF(SWM_D_MOVE
, "win_to_ws: done.\n");
5320 pressbutton(struct swm_region
*r
, union arg
*args
)
5322 /* suppress unused warning since var is needed */
5325 xcb_test_fake_input(conn
, XCB_BUTTON_PRESS
, args
->id
,
5326 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
5327 xcb_test_fake_input(conn
, XCB_BUTTON_RELEASE
, args
->id
,
5328 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
5332 raise_toggle(struct swm_region
*r
, union arg
*args
)
5334 /* Suppress warning. */
5337 if (r
== NULL
|| r
->ws
== NULL
)
5340 if (r
->ws
->focus
&& MAXIMIZED(r
->ws
->focus
))
5343 r
->ws
->always_raise
= !r
->ws
->always_raise
;
5345 /* Update focused win stacking order based on new always_raise value. */
5346 raise_window(r
->ws
->focus
);
5352 iconify(struct swm_region
*r
, union arg
*args
)
5356 /* Suppress warning. */
5359 if ((w
= r
->ws
->focus
) == NULL
)
5362 ewmh_apply_flags(w
, w
->ewmh_flags
| EWMH_F_HIDDEN
);
5363 ewmh_update_wm_state(w
);
5371 get_win_name(xcb_window_t win
)
5374 xcb_get_property_cookie_t c
;
5375 xcb_get_property_reply_t
*r
;
5377 /* First try _NET_WM_NAME for UTF-8. */
5378 c
= xcb_get_property(conn
, 0, win
, ewmh
[_NET_WM_NAME
].atom
,
5379 XCB_GET_PROPERTY_TYPE_ANY
, 0, UINT_MAX
);
5380 r
= xcb_get_property_reply(conn
, c
, NULL
);
5381 if (r
&& r
->type
== XCB_NONE
) {
5383 /* Use WM_NAME instead; no UTF-8. */
5384 c
= xcb_get_property(conn
, 0, win
, XCB_ATOM_WM_NAME
,
5385 XCB_GET_PROPERTY_TYPE_ANY
, 0, UINT_MAX
);
5386 r
= xcb_get_property_reply(conn
, c
, NULL
);
5389 if (r
&& r
->type
!= XCB_NONE
&& r
->length
> 0)
5390 name
= strndup(xcb_get_property_value(r
),
5391 xcb_get_property_value_length(r
));
5396 err(1, "get_win_name: failed to allocate memory.");
5404 uniconify(struct swm_region
*r
, union arg
*args
)
5411 DNPRINTF(SWM_D_MISC
, "uniconify\n");
5413 if (r
== NULL
|| r
->ws
== NULL
)
5416 /* make sure we have anything to uniconify */
5417 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
5418 if (win
->ws
== NULL
)
5419 continue; /* should never happen */
5425 DNPRINTF(SWM_D_MISC
, "uniconify: count: %d\n", count
);
5431 search_resp_action
= SWM_SEARCH_UNICONIFY
;
5433 spawn_select(r
, args
, "search", &searchpid
);
5435 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
5438 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
5439 if (win
->ws
== NULL
)
5440 continue; /* should never happen */
5444 name
= get_win_name(win
->id
);
5445 fprintf(lfile
, "%s.%u\n", name
, win
->id
);
5453 name_workspace(struct swm_region
*r
, union arg
*args
)
5457 DNPRINTF(SWM_D_MISC
, "name_workspace\n");
5463 search_resp_action
= SWM_SEARCH_NAME_WORKSPACE
;
5465 spawn_select(r
, args
, "name_workspace", &searchpid
);
5467 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
5470 fprintf(lfile
, "%s", "");
5475 search_workspace(struct swm_region
*r
, union arg
*args
)
5478 struct workspace
*ws
;
5481 DNPRINTF(SWM_D_MISC
, "search_workspace\n");
5487 search_resp_action
= SWM_SEARCH_SEARCH_WORKSPACE
;
5489 spawn_select(r
, args
, "search", &searchpid
);
5491 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
5494 for (i
= 0; i
< workspace_limit
; i
++) {
5498 fprintf(lfile
, "%d%s%s\n", ws
->idx
+ 1,
5499 (ws
->name
? ":" : ""), (ws
->name
? ws
->name
: ""));
5506 search_win_cleanup(void)
5508 struct search_window
*sw
= NULL
;
5510 while ((sw
= TAILQ_FIRST(&search_wl
)) != NULL
) {
5511 xcb_destroy_window(conn
, sw
->indicator
);
5512 TAILQ_REMOVE(&search_wl
, sw
, entry
);
5518 search_win(struct swm_region
*r
, union arg
*args
)
5520 struct ws_win
*win
= NULL
;
5521 struct search_window
*sw
= NULL
;
5524 xcb_screen_t
*screen
;
5525 int i
, width
, height
;
5533 XRectangle l_ibox
, l_lbox
;
5535 DNPRINTF(SWM_D_MISC
, "search_win\n");
5538 search_resp_action
= SWM_SEARCH_SEARCH_WINDOW
;
5540 spawn_select(r
, args
, "search", &searchpid
);
5542 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
5545 if ((screen
= get_screen(r
->s
->idx
)) == NULL
)
5546 errx(1, "ERROR: can't get screen %d.", r
->s
->idx
);
5548 TAILQ_INIT(&search_wl
);
5551 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
5555 sw
= calloc(1, sizeof(struct search_window
));
5557 warn("search_win: calloc");
5559 search_win_cleanup();
5565 snprintf(s
, sizeof s
, "%d", i
);
5568 w
= xcb_generate_id(conn
);
5569 wa
[0] = r
->s
->c
[SWM_S_COLOR_FOCUS
].pixel
;
5570 wa
[1] = r
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
;
5571 wa
[2] = screen
->default_colormap
;
5573 if (bar_font_legacy
) {
5574 XmbTextExtents(bar_fs
, s
, len
, &l_ibox
, &l_lbox
);
5575 width
= l_lbox
.width
+ 4;
5576 height
= bar_fs_extents
->max_logical_extent
.height
+ 4;
5578 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
,
5580 width
= info
.width
+ 4;
5581 height
= bar_font
->height
+ 4;
5584 xcb_create_window(conn
, screen
->root_depth
, w
, win
->id
, 0, 0,
5585 width
, height
, 1, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
5586 screen
->root_visual
, XCB_CW_BACK_PIXEL
|
5587 XCB_CW_BORDER_PIXEL
| XCB_CW_COLORMAP
, wa
);
5589 xcb_map_window(conn
, w
);
5592 TAILQ_INSERT_TAIL(&search_wl
, sw
, entry
);
5594 if (bar_font_legacy
) {
5595 l_gcv
.graphics_exposures
= 0;
5596 l_draw
= XCreateGC(display
, w
, 0, &l_gcv
);
5598 XSetForeground(display
, l_draw
,
5599 r
->s
->c
[SWM_S_COLOR_BAR
].pixel
);
5601 DRAWSTRING(display
, w
, bar_fs
, l_draw
, 2,
5602 (bar_fs_extents
->max_logical_extent
.height
-
5603 l_lbox
.height
) / 2 - l_lbox
.y
, s
, len
);
5605 XFreeGC(display
, l_draw
);
5608 draw
= XftDrawCreate(display
, w
,
5609 DefaultVisual(display
, r
->s
->idx
),
5610 DefaultColormap(display
, r
->s
->idx
));
5612 XftDrawStringUtf8(draw
, &search_font_color
, bar_font
, 2,
5613 (HEIGHT(r
->bar
) + bar_font
->height
) / 2 -
5614 bar_font
->descent
, (FcChar8
*)s
, len
);
5616 XftDrawDestroy(draw
);
5619 DNPRINTF(SWM_D_MISC
, "search_win: mapped win %#x\n", w
);
5621 fprintf(lfile
, "%d\n", i
);
5631 search_resp_uniconify(const char *resp
, size_t len
)
5637 DNPRINTF(SWM_D_MISC
, "search_resp_uniconify: resp: %s\n", resp
);
5639 TAILQ_FOREACH(win
, &search_r
->ws
->winlist
, entry
) {
5642 name
= get_win_name(win
->id
);
5643 if (asprintf(&s
, "%s.%u", name
, win
->id
) == -1) {
5648 if (strncmp(s
, resp
, len
) == 0) {
5649 /* XXX this should be a callback to generalize */
5650 ewmh_apply_flags(win
, win
->ewmh_flags
& ~EWMH_F_HIDDEN
);
5651 ewmh_update_wm_state(win
);
5661 search_resp_name_workspace(const char *resp
, size_t len
)
5663 struct workspace
*ws
;
5665 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: resp: %s\n", resp
);
5667 if (search_r
->ws
== NULL
)
5672 free(search_r
->ws
->name
);
5673 search_r
->ws
->name
= NULL
;
5677 ws
->name
= strdup(resp
);
5678 if (ws
->name
== NULL
) {
5679 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: "
5680 "strdup: %s", strerror(errno
));
5684 ewmh_update_desktop_names();
5685 ewmh_get_desktop_names();
5690 ewmh_update_desktop_names(void)
5692 char *name_list
= NULL
, *p
;
5693 int num_screens
, i
, j
, len
= 0, tot
= 0;
5695 num_screens
= get_screen_count();
5696 for (i
= 0; i
< num_screens
; ++i
) {
5697 for (j
= 0; j
< workspace_limit
; ++j
) {
5698 if (screens
[i
].ws
[j
].name
!= NULL
)
5699 len
+= strlen(screens
[i
].ws
[j
].name
);
5703 if((name_list
= calloc(len
, sizeof(char))) == NULL
)
5704 err(1, "update_desktop_names: calloc: failed to "
5705 "allocate memory.");
5708 for (j
= 0; j
< workspace_limit
; ++j
) {
5709 if (screens
[i
].ws
[j
].name
!= NULL
) {
5710 len
= strlen(screens
[i
].ws
[j
].name
);
5711 memcpy(p
, screens
[i
].ws
[j
].name
, len
);
5720 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
5721 screens
[i
].root
, ewmh
[_NET_DESKTOP_NAMES
].atom
,
5722 a_utf8_string
, 8, tot
, name_list
);
5732 ewmh_get_desktop_names(void)
5735 xcb_get_property_cookie_t c
;
5736 xcb_get_property_reply_t
*r
;
5737 int num_screens
, i
, j
, n
, k
;
5739 num_screens
= get_screen_count();
5740 for (i
= 0; i
< num_screens
; ++i
) {
5741 for (j
= 0; j
< workspace_limit
; ++j
) {
5742 free(screens
[i
].ws
[j
].name
);
5743 screens
[i
].ws
[j
].name
= NULL
;
5746 c
= xcb_get_property(conn
, 0, screens
[i
].root
,
5747 ewmh
[_NET_DESKTOP_NAMES
].atom
,
5748 a_utf8_string
, 0, UINT32_MAX
);
5749 r
= xcb_get_property_reply(conn
, c
, NULL
);
5753 names
= xcb_get_property_value(r
);
5754 n
= xcb_get_property_value_length(r
);
5756 for (j
= 0, k
= 0; j
< n
; ++j
) {
5757 if (*(names
+ j
) != '\0') {
5758 screens
[i
].ws
[k
].name
= strdup(names
+ j
);
5759 j
+= strlen(names
+ j
);
5768 ewmh_update_client_list(void)
5771 int num_screens
, i
, j
, k
= 0, count
= 0;
5774 num_screens
= get_screen_count();
5775 for (i
= 0; i
< num_screens
; ++i
) {
5776 for (j
= 0; j
< workspace_limit
; ++j
)
5777 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
5780 DNPRINTF(SWM_D_PROP
, "ewmh_update_client_list: win count: %d\n",
5786 wins
= calloc(count
, sizeof(xcb_window_t
));
5788 err(1, "ewmh_update_client_list: calloc: failed to "
5789 "allocate memory.");
5791 for (j
= 0, k
= 0; j
< workspace_limit
; ++j
)
5792 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
5793 wins
[k
++] = win
->id
;
5795 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
5796 screens
[i
].root
, ewmh
[_NET_CLIENT_LIST
].atom
,
5797 XCB_ATOM_WINDOW
, 32, count
, wins
);
5804 ewmh_update_current_desktop(void)
5808 num_screens
= get_screen_count();
5809 for (i
= 0; i
< num_screens
; ++i
)
5810 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
5811 screens
[i
].root
, ewmh
[_NET_CURRENT_DESKTOP
].atom
,
5812 XCB_ATOM_CARDINAL
, 32, 1, &screens
[i
].r_focus
->ws
->idx
);
5816 ewmh_update_desktops(void)
5818 int num_screens
, i
, j
;
5821 vals
= calloc(workspace_limit
* 2, sizeof(uint32_t));
5823 err(1, "ewmh_update_desktops: calloc: failed to allocate "
5826 num_screens
= get_screen_count();
5827 for (i
= 0; i
< num_screens
; i
++) {
5828 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
5829 screens
[i
].root
, ewmh
[_NET_NUMBER_OF_DESKTOPS
].atom
,
5830 XCB_ATOM_CARDINAL
, 32, 1, &workspace_limit
);
5832 for (j
= 0; j
< workspace_limit
; ++j
) {
5833 if (screens
[i
].ws
[j
].r
!= NULL
) {
5834 vals
[j
* 2] = X(screens
[i
].ws
[j
].r
);
5835 vals
[j
* 2 + 1] = Y(screens
[i
].ws
[j
].r
);
5836 } else if (screens
[i
].ws
[j
].old_r
!= NULL
) {
5837 vals
[j
* 2] = X(screens
[i
].ws
[j
].old_r
);
5838 vals
[j
* 2 + 1] = Y(screens
[i
].ws
[j
].old_r
);
5840 vals
[j
* 2] = vals
[j
* 2 + 1] = 0;
5844 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
5845 screens
[i
].root
, ewmh
[_NET_DESKTOP_VIEWPORT
].atom
,
5846 XCB_ATOM_CARDINAL
, 32, workspace_limit
* 2, vals
);
5853 search_resp_search_workspace(const char *resp
)
5860 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: resp: %s\n", resp
);
5864 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: strdup: %s",
5871 ws_idx
= (int)strtonum(q
, 1, workspace_limit
, &errstr
);
5873 DNPRINTF(SWM_D_MISC
, "workspace idx is %s: %s",
5880 switchws(search_r
, &a
);
5884 search_resp_search_window(const char *resp
)
5889 struct search_window
*sw
;
5891 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: resp: %s\n", resp
);
5895 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: strdup: %s",
5900 idx
= (int)strtonum(s
, 1, INT_MAX
, &errstr
);
5902 DNPRINTF(SWM_D_MISC
, "window idx is %s: %s",
5909 TAILQ_FOREACH(sw
, &search_wl
, entry
)
5910 if (idx
== sw
->idx
) {
5916 #define MAX_RESP_LEN 1024
5919 search_do_resp(void)
5925 DNPRINTF(SWM_D_MISC
, "search_do_resp:\n");
5930 if ((resp
= calloc(1, MAX_RESP_LEN
+ 1)) == NULL
) {
5931 warn("search: calloc");
5935 rbytes
= read(select_resp_pipe
[0], resp
, MAX_RESP_LEN
);
5937 warn("search: read error");
5940 resp
[rbytes
] = '\0';
5943 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
5944 * newline, so work around that by sanitizing the resp now.
5946 resp
[strcspn(resp
, "\n")] = '\0';
5949 switch (search_resp_action
) {
5950 case SWM_SEARCH_UNICONIFY
:
5951 search_resp_uniconify(resp
, len
);
5953 case SWM_SEARCH_NAME_WORKSPACE
:
5954 search_resp_name_workspace(resp
, len
);
5956 case SWM_SEARCH_SEARCH_WORKSPACE
:
5957 search_resp_search_workspace(resp
);
5959 case SWM_SEARCH_SEARCH_WINDOW
:
5960 search_resp_search_window(resp
);
5965 if (search_resp_action
== SWM_SEARCH_SEARCH_WINDOW
)
5966 search_win_cleanup();
5968 search_resp_action
= SWM_SEARCH_NONE
;
5969 close(select_resp_pipe
[0]);
5976 wkill(struct swm_region
*r
, union arg
*args
)
5978 DNPRINTF(SWM_D_MISC
, "wkill: win %#x, id: %d\n", WINID(r
->ws
->focus
),
5981 if (r
->ws
->focus
== NULL
)
5984 if (args
->id
== SWM_ARG_ID_KILLWINDOW
)
5985 xcb_kill_client(conn
, r
->ws
->focus
->id
);
5987 if (r
->ws
->focus
->can_delete
)
5988 client_msg(r
->ws
->focus
, a_delete
, 0);
5994 clear_maximized(struct workspace
*ws
)
5999 /* Clear any maximized win(s) on ws, from bottom up. */
6000 TAILQ_FOREACH_REVERSE(w
, &ws
->stack
, ws_win_stack
, stack_entry
)
6002 ewmh_apply_flags(w
, w
->ewmh_flags
& ~EWMH_F_MAXIMIZED
);
6003 ewmh_update_wm_state(w
);
6011 maximize_toggle(struct swm_region
*r
, union arg
*args
)
6013 struct ws_win
*w
= r
->ws
->focus
;
6015 /* suppress unused warning since var is needed */
6021 DNPRINTF(SWM_D_MISC
, "maximize_toggle: win %#x\n", w
->id
);
6026 if (w
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
6029 ewmh_apply_flags(w
, w
->ewmh_flags
^ EWMH_F_MAXIMIZED
);
6030 ewmh_update_wm_state(w
);
6034 if (w
== w
->ws
->focus
)
6039 DNPRINTF(SWM_D_MISC
, "maximize_toggle: done\n");
6043 floating_toggle(struct swm_region
*r
, union arg
*args
)
6045 struct ws_win
*w
= r
->ws
->focus
;
6047 /* suppress unused warning since var is needed */
6053 DNPRINTF(SWM_D_MISC
, "floating_toggle: win %#x\n", w
->id
);
6055 if (FULLSCREEN(w
) || TRANS(w
))
6058 if (w
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
6061 ewmh_apply_flags(w
, w
->ewmh_flags
^ EWMH_F_ABOVE
);
6062 ewmh_update_wm_state(w
);
6066 if (w
== w
->ws
->focus
)
6071 DNPRINTF(SWM_D_MISC
, "floating_toggle: done\n");
6075 region_containment(struct ws_win
*win
, struct swm_region
*r
, int opts
)
6077 struct swm_geometry g
= r
->g
;
6078 int rt
, lt
, tp
, bm
, bw
;
6080 bw
= (opts
& SWM_CW_SOFTBOUNDARY
) ? boundary_width
: 0;
6083 * Perpendicular distance of each side of the window to the respective
6084 * side of the region boundary. Positive values indicate the side of
6085 * the window has passed beyond the region boundary.
6087 rt
= opts
& SWM_CW_RIGHT
? MAX_X(win
) + BORDER(win
) - MAX_X(r
) : bw
;
6088 lt
= opts
& SWM_CW_LEFT
? X(r
) - X(win
) + BORDER(win
) : bw
;
6089 bm
= opts
& SWM_CW_BOTTOM
? MAX_Y(win
) + BORDER(win
) - MAX_Y(r
) : bw
;
6090 tp
= opts
& SWM_CW_TOP
? Y(r
) - Y(win
) + BORDER(win
) : bw
;
6092 DNPRINTF(SWM_D_MISC
, "region_containment: win %#x, rt: %d, lt: %d, "
6093 "bm: %d, tp: %d, SOFTBOUNDARY: %s, HARDBOUNDARY: %s\n", win
->id
, rt
,
6094 lt
, bm
, tp
, YESNO(opts
& SWM_CW_SOFTBOUNDARY
),
6095 YESNO(opts
& SWM_CW_HARDBOUNDARY
));
6098 * Disable containment if any of the flagged sides went beyond the
6099 * containment boundary, or if containment is disabled.
6101 if (!(opts
& SWM_CW_HARDBOUNDARY
|| opts
& SWM_CW_SOFTBOUNDARY
) ||
6102 (bw
!= 0 && ((rt
> bw
) || (lt
> bw
) || (bm
> bw
) || (tp
> bw
)))) {
6103 /* Make sure window has at least 1 pixel in the region */
6104 g
.x
+= 1 - WIDTH(win
);
6105 g
.y
+= 1 - HEIGHT(win
);
6106 g
.w
+= 2 * WIDTH(win
) - 2;
6107 g
.h
+= 2 * HEIGHT(win
) - 2;
6110 constrain_window(win
, &g
, &opts
);
6113 /* Move or resize a window so that flagged side(s) fit into the supplied box. */
6115 constrain_window(struct ws_win
*win
, struct swm_geometry
*b
, int *opts
)
6117 DNPRINTF(SWM_D_MISC
, "constrain_window: win %#x, (x,y) w x h: "
6118 "(%d,%d) %d x %d, box: (x,y) w x h: (%d,%d) %d x %d, rt: %s, "
6119 "lt: %s, bt: %s, tp: %s, allow resize: %s\n", win
->id
, X(win
),
6120 Y(win
), WIDTH(win
), HEIGHT(win
), b
->x
, b
->y
, b
->w
, b
->h
,
6121 YESNO(*opts
& SWM_CW_RIGHT
), YESNO(*opts
& SWM_CW_LEFT
),
6122 YESNO(*opts
& SWM_CW_BOTTOM
), YESNO(*opts
& SWM_CW_TOP
),
6123 YESNO(*opts
& SWM_CW_RESIZABLE
));
6125 if ((*opts
& SWM_CW_RIGHT
) && MAX_X(win
) + BORDER(win
) > b
->x
+ b
->w
) {
6126 if (*opts
& SWM_CW_RESIZABLE
)
6127 WIDTH(win
) = b
->x
+ b
->w
- X(win
) - BORDER(win
);
6129 X(win
) = b
->x
+ b
->w
- WIDTH(win
) - BORDER(win
);
6132 if ((*opts
& SWM_CW_LEFT
) && X(win
) + BORDER(win
) < b
->x
) {
6133 if (*opts
& SWM_CW_RESIZABLE
)
6134 WIDTH(win
) -= b
->x
- X(win
) - BORDER(win
);
6136 X(win
) = b
->x
- BORDER(win
);
6139 if ((*opts
& SWM_CW_BOTTOM
) && MAX_Y(win
) + BORDER(win
) > b
->y
+ b
->h
) {
6140 if (*opts
& SWM_CW_RESIZABLE
)
6141 HEIGHT(win
) = b
->y
+ b
->h
- Y(win
) - BORDER(win
);
6143 Y(win
) = b
->y
+ b
->h
- HEIGHT(win
) - BORDER(win
);
6146 if ((*opts
& SWM_CW_TOP
) && Y(win
) + BORDER(win
) < b
->y
) {
6147 if (*opts
& SWM_CW_RESIZABLE
)
6148 HEIGHT(win
) -= b
->y
- Y(win
) - BORDER(win
);
6150 Y(win
) = b
->y
- BORDER(win
);
6153 if (*opts
& SWM_CW_RESIZABLE
) {
6156 if (HEIGHT(win
) < 1)
6162 update_window_color(struct ws_win
*win
)
6166 if (WS_FOCUSED(win
->ws
) && win
->ws
->focus
== win
)
6167 pixel
= MAXIMIZED(win
) ?
6168 &win
->s
->c
[SWM_S_COLOR_FOCUS_MAXIMIZED
].pixel
:
6169 &win
->s
->c
[SWM_S_COLOR_FOCUS
].pixel
;
6171 pixel
= MAXIMIZED(win
) ?
6172 &win
->s
->c
[SWM_S_COLOR_UNFOCUS_MAXIMIZED
].pixel
:
6173 &win
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
;
6175 xcb_change_window_attributes(conn
, win
->id
,
6176 XCB_CW_BORDER_PIXEL
, pixel
);
6180 update_window(struct ws_win
*win
)
6185 mask
= XCB_CONFIG_WINDOW_X
| XCB_CONFIG_WINDOW_Y
|
6186 XCB_CONFIG_WINDOW_WIDTH
| XCB_CONFIG_WINDOW_HEIGHT
|
6187 XCB_CONFIG_WINDOW_BORDER_WIDTH
;
6191 wc
[3] = HEIGHT(win
);
6192 wc
[4] = BORDER(win
);
6194 DNPRINTF(SWM_D_EVENT
, "update_window: win %#x, (x,y) w x h: "
6195 "(%d,%d) %d x %d, bordered: %s\n", win
->id
, wc
[0], wc
[1], wc
[2],
6196 wc
[3], YESNO(win
->bordered
));
6198 xcb_configure_window(conn
, win
->id
, mask
, wc
);
6201 #define SWM_RESIZE_STEPS (50)
6204 resize(struct ws_win
*win
, union arg
*args
)
6206 xcb_timestamp_t timestamp
= 0;
6207 struct swm_region
*r
= NULL
;
6208 struct swm_geometry g
;
6210 int top
= 0, left
= 0;
6212 xcb_cursor_t cursor
;
6213 xcb_query_pointer_reply_t
*xpr
= NULL
;
6214 xcb_generic_event_t
*evt
;
6215 xcb_motion_notify_event_t
*mne
;
6222 if (FULLSCREEN(win
))
6225 /* In max_stack mode, should only resize transients. */
6226 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
] && !TRANS(win
))
6229 DNPRINTF(SWM_D_EVENT
, "resize: win %#x, floating: %s, "
6230 "transient: %#x\n", win
->id
, YESNO(ABOVE(win
)),
6234 store_float_geom(win
);
6235 else if (!(TRANS(win
) || ABOVE(win
)))
6238 ewmh_apply_flags(win
, (win
->ewmh_flags
| SWM_F_MANUAL
| EWMH_F_ABOVE
) &
6240 ewmh_update_wm_state(win
);
6246 /* It's possible for win to have been freed during focus_flush(). */
6247 if (validate_win(win
)) {
6248 DNPRINTF(SWM_D_EVENT
, "resize: invalid win.\n");
6253 case SWM_ARG_ID_WIDTHSHRINK
:
6254 WIDTH(win
) -= SWM_RESIZE_STEPS
;
6257 case SWM_ARG_ID_WIDTHGROW
:
6258 WIDTH(win
) += SWM_RESIZE_STEPS
;
6261 case SWM_ARG_ID_HEIGHTSHRINK
:
6262 HEIGHT(win
) -= SWM_RESIZE_STEPS
;
6265 case SWM_ARG_ID_HEIGHTGROW
:
6266 HEIGHT(win
) += SWM_RESIZE_STEPS
;
6273 region_containment(win
, r
, SWM_CW_ALLSIDES
| SWM_CW_RESIZABLE
|
6274 SWM_CW_HARDBOUNDARY
);
6276 store_float_geom(win
);
6280 region_containment(win
, r
, SWM_CW_ALLSIDES
| SWM_CW_RESIZABLE
|
6281 SWM_CW_SOFTBOUNDARY
);
6284 /* get cursor offset from window root */
6285 xpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
6292 if (xpr
->win_x
< WIDTH(win
) / 2)
6295 if (xpr
->win_y
< HEIGHT(win
) / 2)
6298 if (args
->id
== SWM_ARG_ID_CENTER
)
6299 cursor
= cursors
[XC_SIZING
].cid
;
6301 cursor
= cursors
[left
? XC_TOP_LEFT_CORNER
:
6302 XC_TOP_RIGHT_CORNER
].cid
;
6304 cursor
= cursors
[left
? XC_BOTTOM_LEFT_CORNER
:
6305 XC_BOTTOM_RIGHT_CORNER
].cid
;
6307 xcb_grab_pointer(conn
, 0, win
->id
, MOUSEMASK
,
6308 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
, XCB_WINDOW_NONE
, cursor
,
6313 while (resizing
&& (evt
= xcb_wait_for_event(conn
))) {
6314 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
6315 case XCB_BUTTON_RELEASE
:
6316 DNPRINTF(SWM_D_EVENT
, "resize: BUTTON_RELEASE\n");
6319 case XCB_MOTION_NOTIFY
:
6320 mne
= (xcb_motion_notify_event_t
*)evt
;
6321 /* cursor offset/delta from start of the operation */
6322 dx
= mne
->root_x
- xpr
->root_x
;
6323 dy
= mne
->root_y
- xpr
->root_y
;
6329 if (args
->id
== SWM_ARG_ID_CENTER
) {
6330 if (g
.h
/ 2 + dy
< 1)
6334 HEIGHT(win
) = g
.h
+ 2 * dy
;
6342 HEIGHT(win
) = g
.h
+ dy
;
6349 if (args
->id
== SWM_ARG_ID_CENTER
) {
6350 if (g
.w
/ 2 + dx
< 1)
6354 WIDTH(win
) = g
.w
+ 2 * dx
;
6362 WIDTH(win
) = g
.w
+ dx
;
6365 /* not free, don't sync more than 120 times / second */
6366 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
6367 timestamp
= mne
->time
;
6368 regionize(win
, mne
->root_x
, mne
->root_y
);
6369 region_containment(win
, r
, SWM_CW_ALLSIDES
|
6370 SWM_CW_RESIZABLE
| SWM_CW_HARDBOUNDARY
|
6371 SWM_CW_SOFTBOUNDARY
);
6378 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
6379 ((xcb_key_press_event_t
*)evt
)->time
);
6385 /* It's possible for win to have been freed above. */
6386 if (validate_win(win
)) {
6387 DNPRINTF(SWM_D_EVENT
, "resize: invalid win.\n");
6395 region_containment(win
, r
, SWM_CW_ALLSIDES
| SWM_CW_RESIZABLE
|
6396 SWM_CW_HARDBOUNDARY
| SWM_CW_SOFTBOUNDARY
);
6400 store_float_geom(win
);
6402 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
6404 DNPRINTF(SWM_D_EVENT
, "resize: done.\n");
6408 resize_step(struct swm_region
*r
, union arg
*args
)
6410 struct ws_win
*win
= NULL
;
6412 if (r
&& r
->ws
&& r
->ws
->focus
)
6422 /* Try to set window region based on supplied coordinates or window center. */
6424 regionize(struct ws_win
*win
, int x
, int y
)
6426 struct swm_region
*r
= NULL
;
6428 r
= region_under(win
->s
, x
, y
);
6430 r
= region_under(win
->s
, X(win
) + WIDTH(win
) / 2,
6431 Y(win
) + HEIGHT(win
) / 2);
6433 if (r
!= NULL
&& r
!= win
->ws
->r
) {
6434 if (clear_maximized(r
->ws
) > 0)
6437 win_to_ws(win
, r
->ws
->idx
, false);
6439 /* Set focus on new ws. */
6440 unfocus_win(r
->ws
->focus
);
6448 #define SWM_MOVE_STEPS (50)
6451 move(struct ws_win
*win
, union arg
*args
)
6453 struct swm_region
*r
;
6454 xcb_timestamp_t timestamp
= 0;
6456 xcb_query_pointer_reply_t
*qpr
= NULL
;
6457 xcb_generic_event_t
*evt
;
6458 xcb_motion_notify_event_t
*mne
;
6459 bool moving
, restack
= false;
6464 if ((r
= win
->ws
->r
) == NULL
)
6467 if (FULLSCREEN(win
))
6470 DNPRINTF(SWM_D_EVENT
, "move: win %#x, floating: %s, transient: "
6471 "%#x\n", win
->id
, YESNO(ABOVE(win
)), win
->transient
);
6473 /* in max_stack mode should only move transients */
6474 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
] && !TRANS(win
))
6477 if (!(ABOVE(win
) || TRANS(win
)) || MAXIMIZED(win
)) {
6478 store_float_geom(win
);
6482 ewmh_apply_flags(win
, (win
->ewmh_flags
| SWM_F_MANUAL
| EWMH_F_ABOVE
) &
6484 ewmh_update_wm_state(win
);
6491 /* It's possible for win to have been freed during focus_flush(). */
6492 if (validate_win(win
)) {
6493 DNPRINTF(SWM_D_EVENT
, "move: invalid win.\n");
6499 case SWM_ARG_ID_MOVELEFT
:
6500 X(win
) -= (SWM_MOVE_STEPS
- border_width
);
6503 case SWM_ARG_ID_MOVERIGHT
:
6504 X(win
) += (SWM_MOVE_STEPS
- border_width
);
6507 case SWM_ARG_ID_MOVEUP
:
6508 Y(win
) -= (SWM_MOVE_STEPS
- border_width
);
6511 case SWM_ARG_ID_MOVEDOWN
:
6512 Y(win
) += (SWM_MOVE_STEPS
- border_width
);
6519 regionize(win
, -1, -1);
6520 region_containment(win
, win
->ws
->r
, SWM_CW_ALLSIDES
);
6522 store_float_geom(win
);
6526 xcb_grab_pointer(conn
, 0, win
->id
, MOUSEMASK
,
6527 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
,
6528 XCB_WINDOW_NONE
, cursors
[XC_FLEUR
].cid
, XCB_CURRENT_TIME
);
6530 /* get cursor offset from window root */
6531 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
6534 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
6538 regionize(win
, qpr
->root_x
, qpr
->root_y
);
6539 region_containment(win
, win
->ws
->r
, SWM_CW_ALLSIDES
|
6540 SWM_CW_SOFTBOUNDARY
);
6544 while (moving
&& (evt
= xcb_wait_for_event(conn
))) {
6545 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
6546 case XCB_BUTTON_RELEASE
:
6547 DNPRINTF(SWM_D_EVENT
, "move: BUTTON_RELEASE\n");
6550 case XCB_MOTION_NOTIFY
:
6551 mne
= (xcb_motion_notify_event_t
*)evt
;
6552 DNPRINTF(SWM_D_EVENT
, "motion: root: %#x\n", mne
->root
);
6553 X(win
) = mne
->root_x
- qpr
->win_x
- border_width
;
6554 Y(win
) = mne
->root_y
- qpr
->win_y
- border_width
;
6556 /* not free, don't sync more than 120 times / second */
6557 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
6558 timestamp
= mne
->time
;
6559 regionize(win
, mne
->root_x
, mne
->root_y
);
6560 region_containment(win
, win
->ws
->r
,
6561 SWM_CW_ALLSIDES
| SWM_CW_SOFTBOUNDARY
);
6568 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
6569 ((xcb_key_press_event_t
*)evt
)->time
);
6575 /* It's possible for win to have been freed above. */
6576 if (validate_win(win
)) {
6577 DNPRINTF(SWM_D_EVENT
, "move: invalid win.\n");
6585 region_containment(win
, win
->ws
->r
, SWM_CW_ALLSIDES
|
6586 SWM_CW_SOFTBOUNDARY
);
6590 store_float_geom(win
);
6592 /* New region set to fullscreen layout. */
6593 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
]) {
6600 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
6601 DNPRINTF(SWM_D_EVENT
, "move: done.\n");
6605 move_step(struct swm_region
*r
, union arg
*args
)
6607 struct ws_win
*win
= NULL
;
6609 if (r
&& r
->ws
&& r
->ws
->focus
)
6614 if (!TRANS(win
) && !ABOVE(win
))
6622 /* key definitions */
6624 char name
[SWM_FUNCNAME_LEN
];
6625 void (*func
)(struct swm_region
*r
, union arg
*);
6627 } keyfuncs
[KF_INVALID
+ 1] = {
6628 /* name function argument */
6629 { "bar_toggle", bar_toggle
, {.id
= SWM_ARG_ID_BAR_TOGGLE
} },
6630 { "bar_toggle_ws", bar_toggle
, {.id
= SWM_ARG_ID_BAR_TOGGLE_WS
} },
6631 { "button2", pressbutton
, {2} },
6632 { "cycle_layout", cycle_layout
, {0} },
6633 { "flip_layout", stack_config
, {.id
= SWM_ARG_ID_FLIPLAYOUT
} },
6634 { "float_toggle", floating_toggle
,{0} },
6635 { "focus_main", focus
, {.id
= SWM_ARG_ID_FOCUSMAIN
} },
6636 { "focus_next", focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
6637 { "focus_prev", focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
6638 { "focus_urgent", focus
, {.id
= SWM_ARG_ID_FOCUSURGENT
} },
6639 { "maximize_toggle", maximize_toggle
,{0} },
6640 { "height_grow", resize_step
, {.id
= SWM_ARG_ID_HEIGHTGROW
} },
6641 { "height_shrink", resize_step
, {.id
= SWM_ARG_ID_HEIGHTSHRINK
} },
6642 { "iconify", iconify
, {0} },
6643 { "master_shrink", stack_config
, {.id
= SWM_ARG_ID_MASTERSHRINK
} },
6644 { "master_grow", stack_config
, {.id
= SWM_ARG_ID_MASTERGROW
} },
6645 { "master_add", stack_config
, {.id
= SWM_ARG_ID_MASTERADD
} },
6646 { "master_del", stack_config
, {.id
= SWM_ARG_ID_MASTERDEL
} },
6647 { "move_down", move_step
, {.id
= SWM_ARG_ID_MOVEDOWN
} },
6648 { "move_left", move_step
, {.id
= SWM_ARG_ID_MOVELEFT
} },
6649 { "move_right", move_step
, {.id
= SWM_ARG_ID_MOVERIGHT
} },
6650 { "move_up", move_step
, {.id
= SWM_ARG_ID_MOVEUP
} },
6651 { "mvrg_1", send_to_rg
, {.id
= 0} },
6652 { "mvrg_2", send_to_rg
, {.id
= 1} },
6653 { "mvrg_3", send_to_rg
, {.id
= 2} },
6654 { "mvrg_4", send_to_rg
, {.id
= 3} },
6655 { "mvrg_5", send_to_rg
, {.id
= 4} },
6656 { "mvrg_6", send_to_rg
, {.id
= 5} },
6657 { "mvrg_7", send_to_rg
, {.id
= 6} },
6658 { "mvrg_8", send_to_rg
, {.id
= 7} },
6659 { "mvrg_9", send_to_rg
, {.id
= 8} },
6660 { "mvws_1", send_to_ws
, {.id
= 0} },
6661 { "mvws_2", send_to_ws
, {.id
= 1} },
6662 { "mvws_3", send_to_ws
, {.id
= 2} },
6663 { "mvws_4", send_to_ws
, {.id
= 3} },
6664 { "mvws_5", send_to_ws
, {.id
= 4} },
6665 { "mvws_6", send_to_ws
, {.id
= 5} },
6666 { "mvws_7", send_to_ws
, {.id
= 6} },
6667 { "mvws_8", send_to_ws
, {.id
= 7} },
6668 { "mvws_9", send_to_ws
, {.id
= 8} },
6669 { "mvws_10", send_to_ws
, {.id
= 9} },
6670 { "mvws_11", send_to_ws
, {.id
= 10} },
6671 { "mvws_12", send_to_ws
, {.id
= 11} },
6672 { "mvws_13", send_to_ws
, {.id
= 12} },
6673 { "mvws_14", send_to_ws
, {.id
= 13} },
6674 { "mvws_15", send_to_ws
, {.id
= 14} },
6675 { "mvws_16", send_to_ws
, {.id
= 15} },
6676 { "mvws_17", send_to_ws
, {.id
= 16} },
6677 { "mvws_18", send_to_ws
, {.id
= 17} },
6678 { "mvws_19", send_to_ws
, {.id
= 18} },
6679 { "mvws_20", send_to_ws
, {.id
= 19} },
6680 { "mvws_21", send_to_ws
, {.id
= 20} },
6681 { "mvws_22", send_to_ws
, {.id
= 21} },
6682 { "name_workspace", name_workspace
, {0} },
6683 { "quit", quit
, {0} },
6684 { "raise_toggle", raise_toggle
, {0} },
6685 { "restart", restart
, {0} },
6686 { "rg_1", focusrg
, {.id
= 0} },
6687 { "rg_2", focusrg
, {.id
= 1} },
6688 { "rg_3", focusrg
, {.id
= 2} },
6689 { "rg_4", focusrg
, {.id
= 3} },
6690 { "rg_5", focusrg
, {.id
= 4} },
6691 { "rg_6", focusrg
, {.id
= 5} },
6692 { "rg_7", focusrg
, {.id
= 6} },
6693 { "rg_8", focusrg
, {.id
= 7} },
6694 { "rg_9", focusrg
, {.id
= 8} },
6695 { "rg_next", cyclerg
, {.id
= SWM_ARG_ID_CYCLERG_UP
} },
6696 { "rg_prev", cyclerg
, {.id
= SWM_ARG_ID_CYCLERG_DOWN
} },
6697 { "screen_next", cyclerg
, {.id
= SWM_ARG_ID_CYCLERG_UP
} },
6698 { "screen_prev", cyclerg
, {.id
= SWM_ARG_ID_CYCLERG_DOWN
} },
6699 { "search_win", search_win
, {0} },
6700 { "search_workspace", search_workspace
, {0} },
6701 { "spawn_custom", NULL
, {0} },
6702 { "stack_inc", stack_config
, {.id
= SWM_ARG_ID_STACKINC
} },
6703 { "stack_dec", stack_config
, {.id
= SWM_ARG_ID_STACKDEC
} },
6704 { "stack_reset", stack_config
, {.id
= SWM_ARG_ID_STACKRESET
} },
6705 { "swap_main", swapwin
, {.id
= SWM_ARG_ID_SWAPMAIN
} },
6706 { "swap_next", swapwin
, {.id
= SWM_ARG_ID_SWAPNEXT
} },
6707 { "swap_prev", swapwin
, {.id
= SWM_ARG_ID_SWAPPREV
} },
6708 { "uniconify", uniconify
, {0} },
6709 { "version", version
, {0} },
6710 { "width_grow", resize_step
, {.id
= SWM_ARG_ID_WIDTHGROW
} },
6711 { "width_shrink", resize_step
, {.id
= SWM_ARG_ID_WIDTHSHRINK
} },
6712 { "wind_del", wkill
, {.id
= SWM_ARG_ID_DELETEWINDOW
} },
6713 { "wind_kill", wkill
, {.id
= SWM_ARG_ID_KILLWINDOW
} },
6714 { "ws_1", switchws
, {.id
= 0} },
6715 { "ws_2", switchws
, {.id
= 1} },
6716 { "ws_3", switchws
, {.id
= 2} },
6717 { "ws_4", switchws
, {.id
= 3} },
6718 { "ws_5", switchws
, {.id
= 4} },
6719 { "ws_6", switchws
, {.id
= 5} },
6720 { "ws_7", switchws
, {.id
= 6} },
6721 { "ws_8", switchws
, {.id
= 7} },
6722 { "ws_9", switchws
, {.id
= 8} },
6723 { "ws_10", switchws
, {.id
= 9} },
6724 { "ws_11", switchws
, {.id
= 10} },
6725 { "ws_12", switchws
, {.id
= 11} },
6726 { "ws_13", switchws
, {.id
= 12} },
6727 { "ws_14", switchws
, {.id
= 13} },
6728 { "ws_15", switchws
, {.id
= 14} },
6729 { "ws_16", switchws
, {.id
= 15} },
6730 { "ws_17", switchws
, {.id
= 16} },
6731 { "ws_18", switchws
, {.id
= 17} },
6732 { "ws_19", switchws
, {.id
= 18} },
6733 { "ws_20", switchws
, {.id
= 19} },
6734 { "ws_21", switchws
, {.id
= 20} },
6735 { "ws_22", switchws
, {.id
= 21} },
6736 { "ws_next", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP
} },
6737 { "ws_next_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP_ALL
} },
6738 { "ws_next_move", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_MOVE_UP
} },
6739 { "ws_prev", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN
} },
6740 { "ws_prev_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN_ALL
} },
6741 { "ws_prev_move", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_MOVE_DOWN
} },
6742 { "ws_prior", priorws
, {0} },
6743 { "dumpwins", dumpwins
, {0} }, /* MUST BE LAST */
6744 { "invalid key func", NULL
, {0} },
6748 key_cmp(struct key
*kp1
, struct key
*kp2
)
6750 if (kp1
->keysym
< kp2
->keysym
)
6752 if (kp1
->keysym
> kp2
->keysym
)
6755 if (kp1
->mod
< kp2
->mod
)
6757 if (kp1
->mod
> kp2
->mod
)
6764 enum { client_click
, root_click
};
6766 unsigned int action
;
6768 unsigned int button
;
6769 void (*func
)(struct ws_win
*, union arg
*);
6772 #define MODKEY_SHIFT MODKEY | XCB_MOD_MASK_SHIFT
6773 /* action key mouse button func args */
6774 { client_click
, MODKEY
, XCB_BUTTON_INDEX_3
, resize
, {.id
= SWM_ARG_ID_DONTCENTER
} },
6775 { client_click
, MODKEY_SHIFT
, XCB_BUTTON_INDEX_3
, resize
, {.id
= SWM_ARG_ID_CENTER
} },
6776 { client_click
, MODKEY
, XCB_BUTTON_INDEX_1
, move
, {0} },
6781 update_modkey(unsigned int mod
)
6787 RB_FOREACH(kp
, key_tree
, &keys
)
6788 if (kp
->mod
& XCB_MOD_MASK_SHIFT
)
6789 kp
->mod
= mod
| XCB_MOD_MASK_SHIFT
;
6793 for (i
= 0; i
< LENGTH(buttons
); i
++)
6794 if (buttons
[i
].mask
& XCB_MOD_MASK_SHIFT
)
6795 buttons
[i
].mask
= mod
| XCB_MOD_MASK_SHIFT
;
6797 buttons
[i
].mask
= mod
;
6801 spawn_expand(struct swm_region
*r
, union arg
*args
, const char *spawn_name
,
6804 struct spawn_prog
*prog
= NULL
;
6806 char *ap
, **real_args
;
6808 /* suppress unused warning since var is needed */
6811 DNPRINTF(SWM_D_SPAWN
, "spawn_expand: %s\n", spawn_name
);
6814 TAILQ_FOREACH(prog
, &spawns
, entry
) {
6815 if (strcasecmp(spawn_name
, prog
->name
) == 0)
6819 warnx("spawn_custom: program %s not found", spawn_name
);
6823 /* make room for expanded args */
6824 if ((real_args
= calloc(prog
->argc
+ 1, sizeof(char *))) == NULL
)
6825 err(1, "spawn_custom: calloc real_args");
6827 /* expand spawn_args into real_args */
6828 for (i
= c
= 0; i
< prog
->argc
; i
++) {
6830 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: raw arg: %s\n", ap
);
6831 if (strcasecmp(ap
, "$bar_border") == 0) {
6833 strdup(r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].name
))
6835 err(1, "spawn_custom border color");
6836 } else if (strcasecmp(ap
, "$bar_color") == 0) {
6838 strdup(r
->s
->c
[SWM_S_COLOR_BAR
].name
))
6840 err(1, "spawn_custom bar color");
6841 } else if (strcasecmp(ap
, "$bar_font") == 0) {
6842 if ((real_args
[c
] = strdup(bar_fonts
))
6844 err(1, "spawn_custom bar fonts");
6845 } else if (strcasecmp(ap
, "$bar_font_color") == 0) {
6847 strdup(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].name
))
6849 err(1, "spawn_custom color font");
6850 } else if (strcasecmp(ap
, "$color_focus") == 0) {
6852 strdup(r
->s
->c
[SWM_S_COLOR_FOCUS
].name
))
6854 err(1, "spawn_custom color focus");
6855 } else if (strcasecmp(ap
, "$color_focus_maximized") == 0) {
6857 strdup(r
->s
->c
[SWM_S_COLOR_FOCUS_MAXIMIZED
].name
))
6859 err(1, "spawn_custom color focus maximized");
6860 } else if (strcasecmp(ap
, "$color_unfocus") == 0) {
6862 strdup(r
->s
->c
[SWM_S_COLOR_UNFOCUS
].name
))
6864 err(1, "spawn_custom color unfocus");
6865 } else if (strcasecmp(ap
, "$color_unfocus_maximized") == 0) {
6867 strdup(r
->s
->c
[SWM_S_COLOR_UNFOCUS_MAXIMIZED
].name
))
6869 err(1, "spawn_custom color unfocus maximized");
6870 } else if (strcasecmp(ap
, "$region_index") == 0) {
6871 if (asprintf(&real_args
[c
], "%d",
6872 get_region_index(r
) + 1) < 1)
6873 err(1, "spawn_custom region index");
6874 } else if (strcasecmp(ap
, "$workspace_index") == 0) {
6875 if (asprintf(&real_args
[c
], "%d", r
->ws
->idx
+ 1) < 1)
6876 err(1, "spawn_custom workspace index");
6877 } else if (strcasecmp(ap
, "$dmenu_bottom") == 0) {
6880 if ((real_args
[c
] = strdup("-b")) == NULL
)
6881 err(1, "spawn_custom workspace index");
6883 /* no match --> copy as is */
6884 if ((real_args
[c
] = strdup(ap
)) == NULL
)
6885 err(1, "spawn_custom strdup(ap)");
6887 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: cooked arg: %s\n",
6893 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: result: ");
6894 for (i
= 0; i
< c
; ++i
)
6895 DPRINTF("\"%s\" ", real_args
[i
]);
6898 *ret_args
= real_args
;
6903 spawn_custom(struct swm_region
*r
, union arg
*args
, const char *spawn_name
)
6909 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
6913 spawn(r
->ws
->idx
, &a
, true);
6915 for (i
= 0; i
< spawn_argc
; i
++)
6921 spawn_select(struct swm_region
*r
, union arg
*args
, const char *spawn_name
,
6928 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
6932 if (pipe(select_list_pipe
) == -1)
6933 err(1, "pipe error");
6934 if (pipe(select_resp_pipe
) == -1)
6935 err(1, "pipe error");
6937 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
6938 err(1, "could not disable SIGPIPE");
6939 switch (*pid
= fork()) {
6941 err(1, "cannot fork");
6944 if (dup2(select_list_pipe
[0], STDIN_FILENO
) == -1)
6946 if (dup2(select_resp_pipe
[1], STDOUT_FILENO
) == -1)
6948 close(select_list_pipe
[1]);
6949 close(select_resp_pipe
[0]);
6950 spawn(r
->ws
->idx
, &a
, false);
6952 default: /* parent */
6953 close(select_list_pipe
[0]);
6954 close(select_resp_pipe
[1]);
6958 for (i
= 0; i
< spawn_argc
; i
++)
6963 /* Argument tokenizer. */
6966 char *arg
, *cp
, *next
;
6967 bool single_quoted
= false, double_quoted
= false;
6972 /* Eat and move characters until end of argument is found. */
6973 for (arg
= next
= cp
= *sp
; *cp
!= '\0'; ++cp
) {
6974 if (!double_quoted
&& *cp
== '\'') {
6975 /* Eat single-quote. */
6976 single_quoted
= !single_quoted
;
6977 } else if (!single_quoted
&& *cp
== '"') {
6978 /* Eat double-quote. */
6979 double_quoted
= !double_quoted
;
6980 } else if (!single_quoted
&& *cp
== '\\' && *(cp
+ 1) == '"') {
6981 /* Eat backslash; copy escaped character to arg. */
6983 } else if (!single_quoted
&& !double_quoted
&& *cp
== '\\' &&
6984 (*(cp
+ 1) == '\'' || *(cp
+ 1) == ' ')) {
6985 /* Eat backslash; move escaped character. */
6987 } else if (!single_quoted
&& !double_quoted
&&
6988 (*cp
== ' ' || *cp
== '\t')) {
6989 /* Terminate argument. */
6991 /* Point sp to beginning of next argument. */
6995 /* Move regular character. */
7000 /* Terminate argument if end of string. */
7010 spawn_insert(const char *name
, const char *args
, int flags
)
7012 struct spawn_prog
*sp
;
7013 char *arg
, *cp
, *ptr
;
7015 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: %s[%s]\n", name
, args
);
7017 if (args
== NULL
|| *args
== '\0')
7020 if ((sp
= calloc(1, sizeof *sp
)) == NULL
)
7021 err(1, "spawn_insert: calloc");
7022 if ((sp
->name
= strdup(name
)) == NULL
)
7023 err(1, "spawn_insert: strdup");
7025 /* Convert the arguments to an argument list. */
7026 if ((ptr
= cp
= strdup(args
)) == NULL
)
7027 err(1, "spawn_insert: strdup");
7028 while ((arg
= argsep(&ptr
)) != NULL
) {
7029 /* Null argument; skip it. */
7034 if ((sp
->argv
= realloc(sp
->argv
, sp
->argc
*
7035 sizeof *sp
->argv
)) == NULL
)
7036 err(1, "spawn_insert: realloc");
7037 if ((sp
->argv
[sp
->argc
- 1] = strdup(arg
)) == NULL
)
7038 err(1, "spawn_insert: strdup");
7044 DNPRINTF(SWM_D_SPAWN
, "arg %d: [%s]\n", sp
->argc
, sp
->argv
[sp
->argc
-1]);
7045 TAILQ_INSERT_TAIL(&spawns
, sp
, entry
);
7046 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: leave\n");
7050 spawn_remove(struct spawn_prog
*sp
)
7054 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: %s\n", sp
->name
);
7056 TAILQ_REMOVE(&spawns
, sp
, entry
);
7057 for (i
= 0; i
< sp
->argc
; i
++)
7063 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: leave\n");
7067 spawn_find(const char *name
)
7069 struct spawn_prog
*sp
;
7071 TAILQ_FOREACH(sp
, &spawns
, entry
)
7072 if (strcasecmp(sp
->name
, name
) == 0)
7079 setspawn(const char *name
, const char *args
, int flags
)
7081 struct spawn_prog
*sp
;
7083 DNPRINTF(SWM_D_SPAWN
, "setspawn: %s\n", name
);
7088 /* Remove any old spawn under the same name. */
7089 if ((sp
= spawn_find(name
)) != NULL
)
7093 spawn_insert(name
, args
, flags
);
7095 warnx("error: setspawn: cannot find program: %s", name
);
7097 DNPRINTF(SWM_D_SPAWN
, "setspawn: leave\n");
7101 setconfspawn(const char *selector
, const char *value
, int flags
)
7105 if (selector
== NULL
|| strlen(selector
) == 0)
7108 args
= expand_tilde(value
);
7110 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: [%s] [%s]\n", selector
, args
);
7112 setspawn(selector
, args
, flags
);
7115 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: done.\n");
7120 validate_spawns(void)
7122 struct spawn_prog
*sp
;
7123 char which
[PATH_MAX
];
7128 RB_FOREACH(kp
, key_tree
, &keys
) {
7129 if (kp
->funcid
!= KF_SPAWN_CUSTOM
)
7133 sp
= spawn_find(kp
->spawn_name
);
7134 if (sp
== NULL
|| sp
->flags
& SWM_SPAWN_OPTIONAL
)
7137 /* verify we have the goods */
7138 snprintf(which
, sizeof which
, "which %s", sp
->argv
[0]);
7139 DNPRINTF(SWM_D_CONF
, "validate_spawns: which %s\n",
7141 for (i
= strlen("which "); i
< strlen(which
); i
++)
7142 if (which
[i
] == ' ') {
7146 if (system(which
) != 0)
7147 add_startup_exception("could not find %s",
7148 &which
[strlen("which ")]);
7155 setconfspawn("lock", "xlock", 0);
7157 setconfspawn("term", "xterm", 0);
7158 setconfspawn("spawn_term", "xterm", 0);
7160 setconfspawn("menu", "dmenu_run"
7164 " -nf $bar_font_color"
7166 " -sf $bar_color", 0);
7168 setconfspawn("search", "dmenu"
7173 " -nf $bar_font_color"
7175 " -sf $bar_color", 0);
7177 setconfspawn("name_workspace", "dmenu"
7182 " -nf $bar_font_color"
7184 " -sf $bar_color", 0);
7186 /* These are not verified for existence, even with a binding set. */
7187 setconfspawn("screenshot_all", "screenshot.sh full", SWM_SPAWN_OPTIONAL
);
7188 setconfspawn("screenshot_wind", "screenshot.sh window", SWM_SPAWN_OPTIONAL
);
7189 setconfspawn("initscr", "initscreen.sh", SWM_SPAWN_OPTIONAL
);
7193 #define SWM_MODNAME_SIZE 32
7194 #define SWM_KEY_WS "\n+ \t"
7196 parsekeys(const char *keystr
, unsigned int currmod
, unsigned int *mod
, KeySym
*ks
)
7198 char *str
, *cp
, *name
;
7201 DNPRINTF(SWM_D_KEY
, "parsekeys: enter [%s]\n", keystr
);
7202 if (mod
== NULL
|| ks
== NULL
) {
7203 DNPRINTF(SWM_D_KEY
, "parsekeys: no mod or key vars\n");
7206 if (keystr
== NULL
|| strlen(keystr
) == 0) {
7207 DNPRINTF(SWM_D_KEY
, "parsekeys: no keystr\n");
7211 if ((cp
= str
= strdup(keystr
)) == NULL
)
7212 err(1, "parsekeys: strdup");
7214 *ks
= XCB_NO_SYMBOL
;
7216 while ((name
= strsep(&cp
, SWM_KEY_WS
)) != NULL
) {
7217 DNPRINTF(SWM_D_KEY
, "parsekeys: key [%s]\n", name
);
7219 cp
+= (long)strspn(cp
, SWM_KEY_WS
);
7220 if (strncasecmp(name
, "MOD", SWM_MODNAME_SIZE
) == 0)
7222 else if (strncasecmp(name
, "Mod1", SWM_MODNAME_SIZE
) == 0)
7223 *mod
|= XCB_MOD_MASK_1
;
7224 else if (strncasecmp(name
, "Mod2", SWM_MODNAME_SIZE
) == 0)
7225 *mod
+= XCB_MOD_MASK_2
;
7226 else if (strncmp(name
, "Mod3", SWM_MODNAME_SIZE
) == 0)
7227 *mod
|= XCB_MOD_MASK_3
;
7228 else if (strncmp(name
, "Mod4", SWM_MODNAME_SIZE
) == 0)
7229 *mod
|= XCB_MOD_MASK_4
;
7230 else if (strncasecmp(name
, "SHIFT", SWM_MODNAME_SIZE
) == 0)
7231 *mod
|= XCB_MOD_MASK_SHIFT
;
7232 else if (strncasecmp(name
, "CONTROL", SWM_MODNAME_SIZE
) == 0)
7233 *mod
|= XCB_MOD_MASK_CONTROL
;
7235 *ks
= XStringToKeysym(name
);
7236 XConvertCase(*ks
, ks
, &uks
);
7237 if (ks
== XCB_NO_SYMBOL
) {
7239 "parsekeys: invalid key %s\n",
7248 DNPRINTF(SWM_D_KEY
, "parsekeys: leave ok\n");
7253 strdupsafe(const char *str
)
7258 return (strdup(str
));
7262 key_insert(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
7263 const char *spawn_name
)
7267 DNPRINTF(SWM_D_KEY
, "key_insert: enter %s [%s]\n",
7268 keyfuncs
[kfid
].name
, spawn_name
);
7270 if ((kp
= malloc(sizeof *kp
)) == NULL
)
7271 err(1, "key_insert: malloc");
7276 kp
->spawn_name
= strdupsafe(spawn_name
);
7277 RB_INSERT(key_tree
, &keys
, kp
);
7279 DNPRINTF(SWM_D_KEY
, "key_insert: leave\n");
7283 key_lookup(unsigned int mod
, KeySym ks
)
7290 return (RB_FIND(key_tree
, &keys
, &kp
));
7294 key_remove(struct key
*kp
)
7296 DNPRINTF(SWM_D_KEY
, "key_remove: %s\n", keyfuncs
[kp
->funcid
].name
);
7298 RB_REMOVE(key_tree
, &keys
, kp
);
7299 free(kp
->spawn_name
);
7302 DNPRINTF(SWM_D_KEY
, "key_remove: leave\n");
7306 key_replace(struct key
*kp
, unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
7307 const char *spawn_name
)
7309 DNPRINTF(SWM_D_KEY
, "key_replace: %s [%s]\n", keyfuncs
[kp
->funcid
].name
,
7313 key_insert(mod
, ks
, kfid
, spawn_name
);
7315 DNPRINTF(SWM_D_KEY
, "key_replace: leave\n");
7319 setkeybinding(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
7320 const char *spawn_name
)
7324 DNPRINTF(SWM_D_KEY
, "setkeybinding: enter %s [%s]\n",
7325 keyfuncs
[kfid
].name
, spawn_name
);
7327 if ((kp
= key_lookup(mod
, ks
)) != NULL
) {
7328 if (kfid
== KF_INVALID
)
7331 key_replace(kp
, mod
, ks
, kfid
, spawn_name
);
7332 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
7335 if (kfid
== KF_INVALID
) {
7336 warnx("bind: Key combination already unbound.");
7337 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
7341 key_insert(mod
, ks
, kfid
, spawn_name
);
7342 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
7346 setconfbinding(const char *selector
, const char *value
, int flags
)
7348 enum keyfuncid kfid
;
7351 struct spawn_prog
*sp
;
7353 /* suppress unused warning since var is needed */
7356 DNPRINTF(SWM_D_KEY
, "setconfbinding: enter selector: [%s], "
7357 "value: [%s]\n", selector
, value
);
7358 if (selector
== NULL
|| strlen(selector
) == 0) {
7359 DNPRINTF(SWM_D_KEY
, "setconfbinding: unbind %s\n", value
);
7360 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
7362 setkeybinding(mod
, ks
, kfid
, NULL
);
7367 /* search by key function name */
7368 for (kfid
= 0; kfid
< KF_INVALID
; (kfid
)++) {
7369 if (strncasecmp(selector
, keyfuncs
[kfid
].name
,
7370 SWM_FUNCNAME_LEN
) == 0) {
7371 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match "
7372 "keyfunc\n", selector
);
7373 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
7374 setkeybinding(mod
, ks
, kfid
, NULL
);
7380 /* search by custom spawn name */
7381 if ((sp
= spawn_find(selector
)) != NULL
) {
7382 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match "
7383 "spawn\n", selector
);
7384 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
7385 setkeybinding(mod
, ks
, KF_SPAWN_CUSTOM
,
7391 DNPRINTF(SWM_D_KEY
, "setconfbinding: no match\n");
7398 #define MODKEY_SHIFT MODKEY | XCB_MOD_MASK_SHIFT
7399 setkeybinding(MODKEY
, XK_b
, KF_BAR_TOGGLE
, NULL
);
7400 setkeybinding(MODKEY_SHIFT
, XK_b
, KF_BAR_TOGGLE_WS
,NULL
);
7401 setkeybinding(MODKEY
, XK_v
, KF_BUTTON2
, NULL
);
7402 setkeybinding(MODKEY
, XK_space
, KF_CYCLE_LAYOUT
,NULL
);
7403 setkeybinding(MODKEY_SHIFT
, XK_backslash
, KF_FLIP_LAYOUT
, NULL
);
7404 setkeybinding(MODKEY
, XK_t
, KF_FLOAT_TOGGLE
,NULL
);
7405 setkeybinding(MODKEY
, XK_m
, KF_FOCUS_MAIN
, NULL
);
7406 setkeybinding(MODKEY
, XK_j
, KF_FOCUS_NEXT
, NULL
);
7407 setkeybinding(MODKEY
, XK_Tab
, KF_FOCUS_NEXT
, NULL
);
7408 setkeybinding(MODKEY
, XK_k
, KF_FOCUS_PREV
, NULL
);
7409 setkeybinding(MODKEY_SHIFT
, XK_Tab
, KF_FOCUS_PREV
, NULL
);
7410 setkeybinding(MODKEY
, XK_u
, KF_FOCUS_URGENT
,NULL
);
7411 setkeybinding(MODKEY
, XK_e
, KF_MAXIMIZE_TOGGLE
,NULL
);
7412 setkeybinding(MODKEY_SHIFT
, XK_equal
, KF_HEIGHT_GROW
,NULL
);
7413 setkeybinding(MODKEY_SHIFT
, XK_minus
, KF_HEIGHT_SHRINK
,NULL
);
7414 setkeybinding(MODKEY
, XK_w
, KF_ICONIFY
, NULL
);
7415 setkeybinding(MODKEY
, XK_h
, KF_MASTER_SHRINK
, NULL
);
7416 setkeybinding(MODKEY
, XK_l
, KF_MASTER_GROW
, NULL
);
7417 setkeybinding(MODKEY
, XK_comma
, KF_MASTER_ADD
, NULL
);
7418 setkeybinding(MODKEY
, XK_period
, KF_MASTER_DEL
, NULL
);
7419 setkeybinding(MODKEY_SHIFT
, XK_bracketright
,KF_MOVE_DOWN
,NULL
);
7420 setkeybinding(MODKEY
, XK_bracketleft
, KF_MOVE_LEFT
,NULL
);
7421 setkeybinding(MODKEY
, XK_bracketright
,KF_MOVE_RIGHT
,NULL
);
7422 setkeybinding(MODKEY_SHIFT
, XK_bracketleft
, KF_MOVE_UP
, NULL
);
7423 setkeybinding(MODKEY_SHIFT
, XK_KP_End
, KF_MVRG_1
, NULL
);
7424 setkeybinding(MODKEY_SHIFT
, XK_KP_Down
, KF_MVRG_2
, NULL
);
7425 setkeybinding(MODKEY_SHIFT
, XK_KP_Next
, KF_MVRG_3
, NULL
);
7426 setkeybinding(MODKEY_SHIFT
, XK_KP_Left
, KF_MVRG_4
, NULL
);
7427 setkeybinding(MODKEY_SHIFT
, XK_KP_Begin
, KF_MVRG_5
, NULL
);
7428 setkeybinding(MODKEY_SHIFT
, XK_KP_Right
, KF_MVRG_6
, NULL
);
7429 setkeybinding(MODKEY_SHIFT
, XK_KP_Home
, KF_MVRG_7
, NULL
);
7430 setkeybinding(MODKEY_SHIFT
, XK_KP_Up
, KF_MVRG_8
, NULL
);
7431 setkeybinding(MODKEY_SHIFT
, XK_KP_Prior
, KF_MVRG_9
, NULL
);
7432 setkeybinding(MODKEY_SHIFT
, XK_1
, KF_MVWS_1
, NULL
);
7433 setkeybinding(MODKEY_SHIFT
, XK_2
, KF_MVWS_2
, NULL
);
7434 setkeybinding(MODKEY_SHIFT
, XK_3
, KF_MVWS_3
, NULL
);
7435 setkeybinding(MODKEY_SHIFT
, XK_4
, KF_MVWS_4
, NULL
);
7436 setkeybinding(MODKEY_SHIFT
, XK_5
, KF_MVWS_5
, NULL
);
7437 setkeybinding(MODKEY_SHIFT
, XK_6
, KF_MVWS_6
, NULL
);
7438 setkeybinding(MODKEY_SHIFT
, XK_7
, KF_MVWS_7
, NULL
);
7439 setkeybinding(MODKEY_SHIFT
, XK_8
, KF_MVWS_8
, NULL
);
7440 setkeybinding(MODKEY_SHIFT
, XK_9
, KF_MVWS_9
, NULL
);
7441 setkeybinding(MODKEY_SHIFT
, XK_0
, KF_MVWS_10
, NULL
);
7442 setkeybinding(MODKEY_SHIFT
, XK_F1
, KF_MVWS_11
, NULL
);
7443 setkeybinding(MODKEY_SHIFT
, XK_F2
, KF_MVWS_12
, NULL
);
7444 setkeybinding(MODKEY_SHIFT
, XK_F3
, KF_MVWS_13
, NULL
);
7445 setkeybinding(MODKEY_SHIFT
, XK_F4
, KF_MVWS_14
, NULL
);
7446 setkeybinding(MODKEY_SHIFT
, XK_F5
, KF_MVWS_15
, NULL
);
7447 setkeybinding(MODKEY_SHIFT
, XK_F6
, KF_MVWS_16
, NULL
);
7448 setkeybinding(MODKEY_SHIFT
, XK_F7
, KF_MVWS_17
, NULL
);
7449 setkeybinding(MODKEY_SHIFT
, XK_F8
, KF_MVWS_18
, NULL
);
7450 setkeybinding(MODKEY_SHIFT
, XK_F9
, KF_MVWS_19
, NULL
);
7451 setkeybinding(MODKEY_SHIFT
, XK_F10
, KF_MVWS_20
, NULL
);
7452 setkeybinding(MODKEY_SHIFT
, XK_F11
, KF_MVWS_21
, NULL
);
7453 setkeybinding(MODKEY_SHIFT
, XK_F12
, KF_MVWS_22
, NULL
);
7454 setkeybinding(MODKEY_SHIFT
, XK_slash
, KF_NAME_WORKSPACE
,NULL
);
7455 setkeybinding(MODKEY_SHIFT
, XK_q
, KF_QUIT
, NULL
);
7456 setkeybinding(MODKEY_SHIFT
, XK_r
, KF_RAISE_TOGGLE
,NULL
);
7457 setkeybinding(MODKEY
, XK_q
, KF_RESTART
, NULL
);
7458 setkeybinding(MODKEY
, XK_KP_End
, KF_RG_1
, NULL
);
7459 setkeybinding(MODKEY
, XK_KP_Down
, KF_RG_2
, NULL
);
7460 setkeybinding(MODKEY
, XK_KP_Next
, KF_RG_3
, NULL
);
7461 setkeybinding(MODKEY
, XK_KP_Left
, KF_RG_4
, NULL
);
7462 setkeybinding(MODKEY
, XK_KP_Begin
, KF_RG_5
, NULL
);
7463 setkeybinding(MODKEY
, XK_KP_Right
, KF_RG_6
, NULL
);
7464 setkeybinding(MODKEY
, XK_KP_Home
, KF_RG_7
, NULL
);
7465 setkeybinding(MODKEY
, XK_KP_Up
, KF_RG_8
, NULL
);
7466 setkeybinding(MODKEY
, XK_KP_Prior
, KF_RG_9
, NULL
);
7467 setkeybinding(MODKEY_SHIFT
, XK_Right
, KF_RG_NEXT
, NULL
);
7468 setkeybinding(MODKEY_SHIFT
, XK_Left
, KF_RG_PREV
, NULL
);
7469 setkeybinding(MODKEY
, XK_f
, KF_SEARCH_WIN
, NULL
);
7470 setkeybinding(MODKEY
, XK_slash
, KF_SEARCH_WORKSPACE
,NULL
);
7471 setkeybinding(MODKEY_SHIFT
, XK_i
, KF_SPAWN_CUSTOM
,"initscr");
7472 setkeybinding(MODKEY_SHIFT
, XK_Delete
, KF_SPAWN_CUSTOM
,"lock");
7473 setkeybinding(MODKEY
, XK_p
, KF_SPAWN_CUSTOM
,"menu");
7474 setkeybinding(MODKEY
, XK_s
, KF_SPAWN_CUSTOM
,"screenshot_all");
7475 setkeybinding(MODKEY_SHIFT
, XK_s
, KF_SPAWN_CUSTOM
,"screenshot_wind");
7476 setkeybinding(MODKEY_SHIFT
, XK_Return
, KF_SPAWN_CUSTOM
,"term");
7477 setkeybinding(MODKEY_SHIFT
, XK_comma
, KF_STACK_INC
, NULL
);
7478 setkeybinding(MODKEY_SHIFT
, XK_period
, KF_STACK_DEC
, NULL
);
7479 setkeybinding(MODKEY_SHIFT
, XK_space
, KF_STACK_RESET
, NULL
);
7480 setkeybinding(MODKEY
, XK_Return
, KF_SWAP_MAIN
, NULL
);
7481 setkeybinding(MODKEY_SHIFT
, XK_j
, KF_SWAP_NEXT
, NULL
);
7482 setkeybinding(MODKEY_SHIFT
, XK_k
, KF_SWAP_PREV
, NULL
);
7483 setkeybinding(MODKEY_SHIFT
, XK_w
, KF_UNICONIFY
, NULL
);
7484 setkeybinding(MODKEY_SHIFT
, XK_v
, KF_VERSION
, NULL
);
7485 setkeybinding(MODKEY
, XK_equal
, KF_WIDTH_GROW
, NULL
);
7486 setkeybinding(MODKEY
, XK_minus
, KF_WIDTH_SHRINK
,NULL
);
7487 setkeybinding(MODKEY
, XK_x
, KF_WIND_DEL
, NULL
);
7488 setkeybinding(MODKEY_SHIFT
, XK_x
, KF_WIND_KILL
, NULL
);
7489 setkeybinding(MODKEY
, XK_1
, KF_WS_1
, NULL
);
7490 setkeybinding(MODKEY
, XK_2
, KF_WS_2
, NULL
);
7491 setkeybinding(MODKEY
, XK_3
, KF_WS_3
, NULL
);
7492 setkeybinding(MODKEY
, XK_4
, KF_WS_4
, NULL
);
7493 setkeybinding(MODKEY
, XK_5
, KF_WS_5
, NULL
);
7494 setkeybinding(MODKEY
, XK_6
, KF_WS_6
, NULL
);
7495 setkeybinding(MODKEY
, XK_7
, KF_WS_7
, NULL
);
7496 setkeybinding(MODKEY
, XK_8
, KF_WS_8
, NULL
);
7497 setkeybinding(MODKEY
, XK_9
, KF_WS_9
, NULL
);
7498 setkeybinding(MODKEY
, XK_0
, KF_WS_10
, NULL
);
7499 setkeybinding(MODKEY
, XK_F1
, KF_WS_11
, NULL
);
7500 setkeybinding(MODKEY
, XK_F2
, KF_WS_12
, NULL
);
7501 setkeybinding(MODKEY
, XK_F3
, KF_WS_13
, NULL
);
7502 setkeybinding(MODKEY
, XK_F4
, KF_WS_14
, NULL
);
7503 setkeybinding(MODKEY
, XK_F5
, KF_WS_15
, NULL
);
7504 setkeybinding(MODKEY
, XK_F6
, KF_WS_16
, NULL
);
7505 setkeybinding(MODKEY
, XK_F7
, KF_WS_17
, NULL
);
7506 setkeybinding(MODKEY
, XK_F8
, KF_WS_18
, NULL
);
7507 setkeybinding(MODKEY
, XK_F9
, KF_WS_19
, NULL
);
7508 setkeybinding(MODKEY
, XK_F10
, KF_WS_20
, NULL
);
7509 setkeybinding(MODKEY
, XK_F11
, KF_WS_21
, NULL
);
7510 setkeybinding(MODKEY
, XK_F12
, KF_WS_22
, NULL
);
7511 setkeybinding(MODKEY
, XK_Right
, KF_WS_NEXT
, NULL
);
7512 setkeybinding(MODKEY
, XK_Left
, KF_WS_PREV
, NULL
);
7513 setkeybinding(MODKEY
, XK_Up
, KF_WS_NEXT_ALL
, NULL
);
7514 setkeybinding(MODKEY
, XK_Down
, KF_WS_PREV_ALL
, NULL
);
7515 setkeybinding(MODKEY_SHIFT
, XK_Up
, KF_WS_NEXT_MOVE
,NULL
);
7516 setkeybinding(MODKEY_SHIFT
, XK_Down
, KF_WS_PREV_MOVE
,NULL
);
7517 setkeybinding(MODKEY
, XK_a
, KF_WS_PRIOR
, NULL
);
7519 setkeybinding(MODKEY_SHIFT
, XK_d
, KF_DUMPWINS
, NULL
);
7529 while (RB_EMPTY(&keys
) == 0) {
7530 kp
= RB_ROOT(&keys
);
7536 setkeymapping(const char *selector
, const char *value
, int flags
)
7538 char *keymapping_file
;
7540 /* suppress unused warnings since vars are needed */
7544 DNPRINTF(SWM_D_KEY
, "setkeymapping: enter\n");
7546 keymapping_file
= expand_tilde(value
);
7549 /* load new key bindings; if it fails, revert to default bindings */
7550 if (conf_load(keymapping_file
, SWM_CONF_KEYMAPPING
)) {
7555 free(keymapping_file
);
7557 DNPRINTF(SWM_D_KEY
, "setkeymapping: leave\n");
7562 updatenumlockmask(void)
7565 xcb_get_modifier_mapping_reply_t
*modmap_r
;
7566 xcb_keycode_t
*modmap
, kc
, *keycode
;
7570 modmap_r
= xcb_get_modifier_mapping_reply(conn
,
7571 xcb_get_modifier_mapping(conn
),
7574 modmap
= xcb_get_modifier_mapping_keycodes(modmap_r
);
7575 for (i
= 0; i
< 8; i
++) {
7576 for (j
= 0; j
< modmap_r
->keycodes_per_modifier
; j
++) {
7577 kc
= modmap
[i
* modmap_r
->keycodes_per_modifier
7579 keycode
= xcb_key_symbols_get_keycode(syms
,
7583 numlockmask
= (1 << i
);
7590 DNPRINTF(SWM_D_MISC
, "updatenumlockmask: %d\n", numlockmask
);
7597 int num_screens
, k
, j
;
7598 unsigned int modifiers
[4];
7599 xcb_keycode_t
*code
;
7601 DNPRINTF(SWM_D_MISC
, "grabkeys\n");
7602 updatenumlockmask();
7605 modifiers
[1] = numlockmask
;
7606 modifiers
[2] = XCB_MOD_MASK_LOCK
;
7607 modifiers
[3] = numlockmask
| XCB_MOD_MASK_LOCK
;
7609 num_screens
= get_screen_count();
7610 for (k
= 0; k
< num_screens
; k
++) {
7611 if (TAILQ_EMPTY(&screens
[k
].rl
))
7613 xcb_ungrab_key(conn
, XCB_GRAB_ANY
, screens
[k
].root
,
7615 RB_FOREACH(kp
, key_tree
, &keys
) {
7616 /* Skip unused ws binds. */
7617 if ((int)kp
->funcid
> KF_WS_1
+ workspace_limit
- 1 &&
7618 kp
->funcid
<= KF_WS_22
)
7621 /* Skip unused mvws binds. */
7622 if ((int)kp
->funcid
> KF_MVWS_1
+ workspace_limit
- 1 &&
7623 kp
->funcid
<= KF_MVWS_22
)
7626 if ((code
= xcb_key_symbols_get_keycode(syms
,
7628 for (j
= 0; j
< LENGTH(modifiers
); j
++)
7629 xcb_grab_key(conn
, 1,
7631 kp
->mod
| modifiers
[j
],
7632 *code
, XCB_GRAB_MODE_SYNC
,
7633 XCB_GRAB_MODE_SYNC
);
7641 grabbuttons(struct ws_win
*win
)
7643 unsigned int modifiers
[4];
7646 DNPRINTF(SWM_D_MOUSE
, "grabbuttons: win %#x\n", win
->id
);
7647 updatenumlockmask();
7650 modifiers
[1] = numlockmask
;
7651 modifiers
[2] = XCB_MOD_MASK_LOCK
;
7652 modifiers
[3] = numlockmask
| XCB_MOD_MASK_LOCK
;
7654 for (i
= 0; i
< LENGTH(buttons
); i
++)
7655 if (buttons
[i
].action
== client_click
)
7656 for (j
= 0; j
< LENGTH(modifiers
); ++j
)
7657 xcb_grab_button(conn
, 0, win
->id
, BUTTONMASK
,
7658 XCB_GRAB_MODE_SYNC
, XCB_GRAB_MODE_ASYNC
,
7659 XCB_WINDOW_NONE
, XCB_CURSOR_NONE
,
7660 buttons
[i
].button
, buttons
[i
].mask
|
7664 const char *quirkname
[] = {
7665 "NONE", /* config string for "no value" */
7673 "FOCUSONMAP_SINGLE",
7679 /* SWM_Q_DELIM: retain '|' for back compat for now (2009-08-11) */
7680 #define SWM_Q_DELIM "\n|+ \t"
7682 parsequirks(const char *qstr
, uint32_t *quirk
, int *ws
)
7684 char *str
, *cp
, *name
;
7687 if (quirk
== NULL
|| qstr
== NULL
)
7690 if ((str
= strdup(qstr
)) == NULL
)
7691 err(1, "parsequirks: strdup");
7695 while ((name
= strsep(&cp
, SWM_Q_DELIM
)) != NULL
) {
7697 cp
+= (long)strspn(cp
, SWM_Q_DELIM
);
7699 if (sscanf(name
, "WS[%d]", ws
) == 1) {
7705 for (i
= 0; i
< LENGTH(quirkname
); i
++) {
7706 if (strncasecmp(name
, quirkname
[i
],
7707 SWM_QUIRK_LEN
) == 0) {
7708 DNPRINTF(SWM_D_QUIRK
,
7709 "parsequirks: %s\n", name
);
7715 *quirk
|= 1 << (i
-1);
7719 if (i
>= LENGTH(quirkname
)) {
7720 DNPRINTF(SWM_D_QUIRK
,
7721 "parsequirks: invalid quirk [%s]\n", name
);
7732 quirk_insert(const char *class, const char *instance
, const char *name
,
7733 uint32_t quirk
, int ws
)
7737 bool failed
= false;
7739 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: class: %s, instance: %s, name: %s,"
7740 " value: %u, ws: %d\n", class, instance
, name
, quirk
, ws
);
7742 if ((qp
= malloc(sizeof *qp
)) == NULL
)
7743 err(1, "quirk_insert: malloc");
7745 if ((qp
->class = strdup(class)) == NULL
)
7746 err(1, "quirk_insert: strdup");
7747 if ((qp
->instance
= strdup(instance
)) == NULL
)
7748 err(1, "quirk_insert: strdup");
7749 if ((qp
->name
= strdup(name
)) == NULL
)
7750 err(1, "quirk_insert: strdup");
7752 if (asprintf(&str
, "^%s$", class) == -1)
7753 err(1, "quirk_insert: asprintf");
7754 if (regcomp(&qp
->regex_class
, str
, REG_EXTENDED
| REG_NOSUB
)) {
7755 add_startup_exception("regex failed to compile quirk 'class' "
7756 "field: %s", class);
7759 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: compiled: %s\n", str
);
7762 if (asprintf(&str
, "^%s$", instance
) == -1)
7763 err(1, "quirk_insert: asprintf");
7764 if (regcomp(&qp
->regex_instance
, str
, REG_EXTENDED
| REG_NOSUB
)) {
7765 add_startup_exception("regex failed to compile quirk 'instance'"
7766 " field: %s", instance
);
7769 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: compiled: %s\n", str
);
7772 if (asprintf(&str
, "^%s$", name
) == -1)
7773 err(1, "quirk_insert: asprintf");
7774 if (regcomp(&qp
->regex_name
, str
, REG_EXTENDED
| REG_NOSUB
)) {
7775 add_startup_exception("regex failed to compile quirk 'name' "
7779 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: compiled: %s\n", str
);
7783 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: regex error; skipping.\n");
7788 TAILQ_INSERT_TAIL(&quirks
, qp
, entry
);
7790 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: leave\n");
7794 quirk_remove(struct quirk
*qp
)
7796 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: %s:%s [%u]\n", qp
->class,
7797 qp
->name
, qp
->quirk
);
7799 TAILQ_REMOVE(&quirks
, qp
, entry
);
7802 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: leave\n");
7806 quirk_free(struct quirk
*qp
)
7808 regfree(&qp
->regex_class
);
7809 regfree(&qp
->regex_instance
);
7810 regfree(&qp
->regex_name
);
7818 quirk_replace(struct quirk
*qp
, const char *class, const char *instance
,
7819 const char *name
, uint32_t quirk
, int ws
)
7821 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: %s:%s:%s [%u], ws: %d\n", qp
->class,
7822 qp
->instance
, qp
->name
, qp
->quirk
, qp
->ws
);
7825 quirk_insert(class, instance
, name
, quirk
, ws
);
7827 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: leave\n");
7831 setquirk(const char *class, const char *instance
, const char *name
,
7832 uint32_t quirk
, int ws
)
7836 DNPRINTF(SWM_D_QUIRK
, "setquirk: enter %s:%s:%s [%u], ws: %d\n", class,
7837 instance
, name
, quirk
, ws
);
7839 /* Remove/replace existing quirk. */
7840 TAILQ_FOREACH(qp
, &quirks
, entry
) {
7841 if (strcmp(qp
->class, class) == 0 &&
7842 strcmp(qp
->instance
, instance
) == 0 &&
7843 strcmp(qp
->name
, name
) == 0) {
7844 if (quirk
== 0 && ws
== -1)
7847 quirk_replace(qp
, class, instance
, name
, quirk
,
7849 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
7854 /* Only insert if quirk is not NONE or forced ws is set. */
7855 if (quirk
|| ws
!= -1)
7856 quirk_insert(class, instance
, name
, quirk
, ws
);
7858 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
7861 /* Eat '\' in str used to escape square brackets and colon. */
7863 unescape_selector(char *str
)
7867 for (cp
= str
; *str
!= '\0'; ++str
, ++cp
) {
7868 if (*str
== '\\' && (*(str
+ 1) == ':' || *(str
+ 1) == ']' ||
7878 setconfquirk(const char *selector
, const char *value
, int flags
)
7880 char *str
, *cp
, *class;
7881 char *instance
= NULL
, *name
= NULL
;
7882 int retval
, count
= 0, ws
= -1;
7885 /* suppress unused warning since var is needed */
7888 if (selector
== NULL
|| strlen(selector
) == 0)
7891 if ((str
= strdup(selector
)) == NULL
)
7892 err(1, "setconfquirk: strdup");
7894 /* Find non-escaped colon. */
7901 for (++cp
; *cp
!= '\0'; ++cp
) {
7902 if (*cp
== ':' && *(cp
- 1) != '\\') {
7908 unescape_selector(class);
7910 instance
= class + strlen(class) + 1;
7911 unescape_selector(instance
);
7917 name
= instance
+ strlen(instance
) + 1;
7918 unescape_selector(name
);
7923 DNPRINTF(SWM_D_CONF
, "setconfquirk: class: %s, instance: %s, "
7924 "name: %s\n", class, instance
, name
);
7926 if ((retval
= parsequirks(value
, &qrks
, &ws
)) == 0)
7927 setquirk(class, instance
, name
, qrks
, ws
);
7936 setquirk("MPlayer", "xv", ".*",
7937 SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
| SWM_Q_FOCUSPREV
, -1);
7938 setquirk("OpenOffice.org 3.2", "VCLSalFrame", ".*",
7940 setquirk("Firefox-bin", "firefox-bin", ".*",
7942 setquirk("Firefox", "Dialog", ".*",
7944 setquirk("Gimp", "gimp", ".*",
7945 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
7946 setquirk("XTerm", "xterm", ".*",
7947 SWM_Q_XTERM_FONTADJ
, -1);
7948 setquirk("xine", "Xine Window", ".*",
7949 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
7950 setquirk("Xitk", "Xitk Combo", ".*",
7951 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
7952 setquirk("xine", "xine Panel", ".*",
7953 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
7954 setquirk("Xitk", "Xine Window", ".*",
7955 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
7956 setquirk("xine", "xine Video Fullscreen Window", ".*",
7957 SWM_Q_FULLSCREEN
| SWM_Q_FLOAT
, -1);
7958 setquirk("pcb", "pcb", ".*",
7960 setquirk("SDL_App", "SDL_App", ".*",
7961 SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
, -1);
7964 /* conf file stuff */
7965 #define SWM_CONF_FILE "spectrwm.conf"
7966 #define SWM_CONF_FILE_OLD "scrotwm.conf"
7970 SWM_S_BAR_AT_BOTTOM
,
7971 SWM_S_BAR_BORDER_WIDTH
,
7974 SWM_S_BAR_ENABLED_WS
,
7979 SWM_S_BOUNDARY_WIDTH
,
7980 SWM_S_CLOCK_ENABLED
,
7983 SWM_S_CYCLE_VISIBLE
,
7985 SWM_S_DISABLE_BORDER
,
7987 SWM_S_FOCUS_CLOSE_WRAP
,
7988 SWM_S_FOCUS_DEFAULT
,
7990 SWM_S_ICONIC_ENABLED
,
7991 SWM_S_JAVA_WORKAROUND
,
7992 SWM_S_REGION_PADDING
,
7997 SWM_S_STACK_ENABLED
,
8000 SWM_S_URGENT_COLLAPSE
,
8001 SWM_S_URGENT_ENABLED
,
8002 SWM_S_VERBOSE_LAYOUT
,
8004 SWM_S_WINDOW_CLASS_ENABLED
,
8005 SWM_S_WINDOW_INSTANCE_ENABLED
,
8006 SWM_S_WINDOW_NAME_ENABLED
,
8007 SWM_S_WORKSPACE_LIMIT
,
8008 SWM_S_WORKSPACE_NAME
,
8012 setconfvalue(const char *selector
, const char *value
, int flags
)
8014 struct workspace
*ws
;
8015 int i
, ws_id
, num_screens
;
8016 char *b
, *str
, s
[1024];
8019 case SWM_S_BAR_ACTION
:
8021 if ((bar_argv
[0] = expand_tilde(value
)) == NULL
)
8022 err(1, "setconfvalue: bar_action");
8024 case SWM_S_BAR_AT_BOTTOM
:
8025 bar_at_bottom
= (atoi(value
) != 0);
8027 case SWM_S_BAR_BORDER_WIDTH
:
8028 bar_border_width
= atoi(value
);
8029 if (bar_border_width
< 0)
8030 bar_border_width
= 0;
8032 case SWM_S_BAR_DELAY
:
8033 /* No longer needed; leave to not break old conf files. */
8035 case SWM_S_BAR_ENABLED
:
8036 bar_enabled
= (atoi(value
) != 0);
8038 case SWM_S_BAR_ENABLED_WS
:
8039 ws_id
= atoi(selector
) - 1;
8040 if (ws_id
< 0 || ws_id
>= workspace_limit
)
8041 errx(1, "setconfvalue: bar_enabled_ws: invalid "
8042 "workspace %d.", ws_id
+ 1);
8044 num_screens
= get_screen_count();
8045 for (i
= 0; i
< num_screens
; i
++) {
8046 ws
= (struct workspace
*)&screens
[i
].ws
;
8047 ws
[ws_id
].bar_enabled
= (atoi(value
) != 0);
8050 case SWM_S_BAR_FONT
:
8052 if (asprintf(&bar_fonts
, "%s,%s", value
, bar_fonts
) == -1)
8053 err(1, "setconfvalue: asprintf: failed to allocate "
8054 "memory for bar_fonts.");
8057 /* If already in xft mode, then we are done. */
8058 if (!bar_font_legacy
)
8061 if ((str
= strdup(value
)) == NULL
)
8062 err(1, "setconfvalue: strdup");
8064 /* If there are any non-XLFD entries, switch to Xft mode. */
8065 while ((b
= strsep(&str
, ",")) != NULL
) {
8069 bar_font_legacy
= false;
8076 case SWM_S_BAR_FORMAT
:
8078 if ((bar_format
= strdup(value
)) == NULL
)
8079 err(1, "setconfvalue: bar_format");
8081 case SWM_S_BAR_JUSTIFY
:
8082 if (strcmp(value
, "left") == 0)
8083 bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
8084 else if (strcmp(value
, "center") == 0)
8085 bar_justify
= SWM_BAR_JUSTIFY_CENTER
;
8086 else if (strcmp(value
, "right") == 0)
8087 bar_justify
= SWM_BAR_JUSTIFY_RIGHT
;
8089 errx(1, "invalid bar_justify");
8091 case SWM_S_BORDER_WIDTH
:
8092 border_width
= atoi(value
);
8093 if (border_width
< 0)
8096 case SWM_S_BOUNDARY_WIDTH
:
8097 boundary_width
= atoi(value
);
8098 if (boundary_width
< 0)
8101 case SWM_S_CLOCK_ENABLED
:
8102 clock_enabled
= (atoi(value
) != 0);
8104 case SWM_S_CLOCK_FORMAT
:
8105 #ifndef SWM_DENY_CLOCK_FORMAT
8107 if ((clock_format
= strdup(value
)) == NULL
)
8108 err(1, "setconfvalue: clock_format");
8111 case SWM_S_CYCLE_EMPTY
:
8112 cycle_empty
= (atoi(value
) != 0);
8114 case SWM_S_CYCLE_VISIBLE
:
8115 cycle_visible
= (atoi(value
) != 0);
8117 case SWM_S_DIALOG_RATIO
:
8118 dialog_ratio
= atof(value
);
8119 if (dialog_ratio
> 1.0 || dialog_ratio
<= .3)
8122 case SWM_S_DISABLE_BORDER
:
8123 disable_border
= (atoi(value
) != 0);
8125 case SWM_S_FOCUS_CLOSE
:
8126 if (strcmp(value
, "first") == 0)
8127 focus_close
= SWM_STACK_BOTTOM
;
8128 else if (strcmp(value
, "last") == 0)
8129 focus_close
= SWM_STACK_TOP
;
8130 else if (strcmp(value
, "next") == 0)
8131 focus_close
= SWM_STACK_ABOVE
;
8132 else if (strcmp(value
, "previous") == 0)
8133 focus_close
= SWM_STACK_BELOW
;
8135 errx(1, "focus_close");
8137 case SWM_S_FOCUS_CLOSE_WRAP
:
8138 focus_close_wrap
= (atoi(value
) != 0);
8140 case SWM_S_FOCUS_DEFAULT
:
8141 if (strcmp(value
, "last") == 0)
8142 focus_default
= SWM_STACK_TOP
;
8143 else if (strcmp(value
, "first") == 0)
8144 focus_default
= SWM_STACK_BOTTOM
;
8146 errx(1, "focus_default");
8148 case SWM_S_FOCUS_MODE
:
8149 if (strcmp(value
, "default") == 0)
8150 focus_mode
= SWM_FOCUS_DEFAULT
;
8151 else if (strcmp(value
, "follow") == 0 ||
8152 strcmp(value
, "follow_cursor") == 0)
8153 focus_mode
= SWM_FOCUS_FOLLOW
;
8154 else if (strcmp(value
, "manual") == 0)
8155 focus_mode
= SWM_FOCUS_MANUAL
;
8157 errx(1, "focus_mode");
8159 case SWM_S_ICONIC_ENABLED
:
8160 iconic_enabled
= (atoi(value
) != 0);
8162 case SWM_S_JAVA_WORKAROUND
:
8163 java_workaround
= (atoi(value
) != 0);
8165 case SWM_S_REGION_PADDING
:
8166 region_padding
= atoi(value
);
8167 if (region_padding
< 0)
8170 case SWM_S_SPAWN_ORDER
:
8171 if (strcmp(value
, "first") == 0)
8172 spawn_position
= SWM_STACK_BOTTOM
;
8173 else if (strcmp(value
, "last") == 0)
8174 spawn_position
= SWM_STACK_TOP
;
8175 else if (strcmp(value
, "next") == 0)
8176 spawn_position
= SWM_STACK_ABOVE
;
8177 else if (strcmp(value
, "previous") == 0)
8178 spawn_position
= SWM_STACK_BELOW
;
8180 errx(1, "spawn_position");
8182 case SWM_S_SPAWN_TERM
:
8183 setconfspawn("term", value
, 0);
8184 setconfspawn("spawn_term", value
, 0);
8187 /* No longer needed; leave to not break old conf files. */
8189 case SWM_S_SS_ENABLED
:
8190 /* No longer needed; leave to not break old conf files. */
8192 case SWM_S_STACK_ENABLED
:
8193 stack_enabled
= (atoi(value
) != 0);
8195 case SWM_S_TERM_WIDTH
:
8196 term_width
= atoi(value
);
8200 case SWM_S_TILE_GAP
:
8201 tile_gap
= atoi(value
);
8203 case SWM_S_URGENT_COLLAPSE
:
8204 urgent_collapse
= (atoi(value
) != 0);
8206 case SWM_S_URGENT_ENABLED
:
8207 urgent_enabled
= (atoi(value
) != 0);
8209 case SWM_S_VERBOSE_LAYOUT
:
8210 verbose_layout
= (atoi(value
) != 0);
8211 for (i
= 0; layouts
[i
].l_stack
!= NULL
; i
++) {
8213 layouts
[i
].l_string
= fancy_stacker
;
8215 layouts
[i
].l_string
= plain_stacker
;
8218 case SWM_S_WARP_POINTER
:
8219 warp_pointer
= (atoi(value
) != 0);
8221 case SWM_S_WINDOW_CLASS_ENABLED
:
8222 window_class_enabled
= (atoi(value
) != 0);
8224 case SWM_S_WINDOW_INSTANCE_ENABLED
:
8225 window_instance_enabled
= (atoi(value
) != 0);
8227 case SWM_S_WINDOW_NAME_ENABLED
:
8228 window_name_enabled
= (atoi(value
) != 0);
8230 case SWM_S_WORKSPACE_LIMIT
:
8231 workspace_limit
= atoi(value
);
8232 if (workspace_limit
> SWM_WS_MAX
)
8233 workspace_limit
= SWM_WS_MAX
;
8234 else if (workspace_limit
< 1)
8235 workspace_limit
= 1;
8237 ewmh_update_desktops();
8239 case SWM_S_WORKSPACE_NAME
:
8240 if (getenv("SWM_STARTED") != NULL
)
8244 if (sscanf(value
, "ws[%d]:%1023c", &ws_id
, s
) != 2)
8245 errx(1, "invalid entry, should be 'ws[<idx>]:name'");
8247 if (ws_id
< 0 || ws_id
>= workspace_limit
)
8248 errx(1, "setconfvalue: workspace_name: invalid "
8249 "workspace %d.", ws_id
+ 1);
8251 num_screens
= get_screen_count();
8252 for (i
= 0; i
< num_screens
; ++i
) {
8253 ws
= (struct workspace
*)&screens
[i
].ws
;
8255 if (strlen(s
) > 0) {
8256 free(ws
[ws_id
].name
);
8257 if ((ws
[ws_id
].name
= strdup(s
)) == NULL
)
8258 err(1, "setconfvalue: workspace_name.");
8260 ewmh_update_desktop_names();
8261 ewmh_get_desktop_names();
8272 setconfmodkey(const char *selector
, const char *value
, int flags
)
8274 /* suppress unused warnings since vars are needed */
8278 if (strncasecmp(value
, "Mod1", strlen("Mod1")) == 0)
8279 update_modkey(XCB_MOD_MASK_1
);
8280 else if (strncasecmp(value
, "Mod2", strlen("Mod2")) == 0)
8281 update_modkey(XCB_MOD_MASK_2
);
8282 else if (strncasecmp(value
, "Mod3", strlen("Mod3")) == 0)
8283 update_modkey(XCB_MOD_MASK_3
);
8284 else if (strncasecmp(value
, "Mod4", strlen("Mod4")) == 0)
8285 update_modkey(XCB_MOD_MASK_4
);
8292 setconfcolor(const char *selector
, const char *value
, int flags
)
8294 int first
, last
, i
= 0, num_screens
;
8296 num_screens
= get_screen_count();
8298 /* conf screen indices begin at 1; treat vals <= 0 as 'all screens.' */
8299 if (selector
== NULL
|| strlen(selector
) == 0 ||
8300 (last
= atoi(selector
) - 1) < 0) {
8302 last
= num_screens
- 1;
8307 if (last
>= num_screens
) {
8308 add_startup_exception("invalid screen index: %d out of bounds "
8309 "(maximum %d)", last
+ 1, num_screens
);
8313 for (i
= first
; i
<= last
; ++i
) {
8314 setscreencolor(value
, i
, flags
);
8317 * When setting focus/unfocus colors, we need to also
8318 * set maximize colors to match if they haven't been customized.
8320 if (flags
== SWM_S_COLOR_FOCUS
&&
8321 !screens
[i
].c
[SWM_S_COLOR_FOCUS_MAXIMIZED
].manual
)
8322 setscreencolor(value
, i
, SWM_S_COLOR_FOCUS_MAXIMIZED
);
8323 else if (flags
== SWM_S_COLOR_UNFOCUS
&&
8324 !screens
[i
].c
[SWM_S_COLOR_UNFOCUS_MAXIMIZED
].manual
)
8325 setscreencolor(value
, i
, SWM_S_COLOR_UNFOCUS_MAXIMIZED
);
8327 screens
[i
].c
[flags
].manual
= 1;
8334 setconfregion(const char *selector
, const char *value
, int flags
)
8336 /* suppress unused warnings since vars are needed */
8340 custom_region(value
);
8345 setautorun(const char *selector
, const char *value
, int flags
)
8355 /* suppress unused warnings since vars are needed */
8359 if (getenv("SWM_STARTED"))
8363 if (sscanf(value
, "ws[%d]:%1023c", &ws_id
, s
) != 2)
8364 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
8366 if (ws_id
< 0 || ws_id
>= workspace_limit
)
8367 errx(1, "autorun: invalid workspace %d", ws_id
+ 1);
8369 sp
= expand_tilde((char *)&s
);
8372 * This is a little intricate
8374 * If the pid already exists we simply reuse it because it means it was
8375 * used before AND not claimed by manage_window. We get away with
8376 * altering it in the parent after INSERT because this can not be a race
8379 while ((ap
= strsep(&sp
, " \t")) != NULL
) {
8382 DNPRINTF(SWM_D_SPAWN
, "setautorun: arg [%s]\n", ap
);
8384 if ((a
.argv
= realloc(a
.argv
, argc
* sizeof(char *))) == NULL
)
8385 err(1, "setautorun: realloc");
8386 a
.argv
[argc
- 1] = ap
;
8390 if ((a
.argv
= realloc(a
.argv
, (argc
+ 1) * sizeof(char *))) == NULL
)
8391 err(1, "setautorun: realloc");
8392 a
.argv
[argc
] = NULL
;
8394 if ((pid
= fork()) == 0) {
8395 spawn(ws_id
, &a
, true);
8404 p
= calloc(1, sizeof *p
);
8407 TAILQ_INSERT_TAIL(&pidlist
, p
, entry
);
8417 setlayout(const char *selector
, const char *value
, int flags
)
8419 struct workspace
*ws
;
8420 int ws_id
, i
, x
, mg
, ma
, si
, ar
;
8421 int st
= SWM_V_STACK
, num_screens
;
8425 /* suppress unused warnings since vars are needed */
8429 if (getenv("SWM_STARTED"))
8433 if (sscanf(value
, "ws[%d]:%d:%d:%d:%d:%1023c",
8434 &ws_id
, &mg
, &ma
, &si
, &ar
, s
) != 6)
8435 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
8436 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
8439 if (ws_id
< 0 || ws_id
>= workspace_limit
)
8440 errx(1, "layout: invalid workspace %d", ws_id
+ 1);
8442 if (strcasecmp(s
, "vertical") == 0)
8444 else if (strcasecmp(s
, "vertical_flip") == 0) {
8447 } else if (strcasecmp(s
, "horizontal") == 0)
8449 else if (strcasecmp(s
, "horizontal_flip") == 0) {
8452 } else if (strcasecmp(s
, "fullscreen") == 0)
8455 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
8456 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
8459 num_screens
= get_screen_count();
8460 for (i
= 0; i
< num_screens
; i
++) {
8461 ws
= (struct workspace
*)&screens
[i
].ws
;
8462 ws
[ws_id
].cur_layout
= &layouts
[st
];
8464 ws
[ws_id
].always_raise
= (ar
!= 0);
8465 if (st
== SWM_MAX_STACK
)
8469 for (x
= 0; x
< abs(mg
); x
++) {
8470 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
8471 mg
>= 0 ? SWM_ARG_ID_MASTERGROW
:
8472 SWM_ARG_ID_MASTERSHRINK
);
8475 for (x
= 0; x
< abs(ma
); x
++) {
8476 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
8477 ma
>= 0 ? SWM_ARG_ID_MASTERADD
:
8478 SWM_ARG_ID_MASTERDEL
);
8481 for (x
= 0; x
< abs(si
); x
++) {
8482 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
8483 si
>= 0 ? SWM_ARG_ID_STACKINC
:
8484 SWM_ARG_ID_STACKDEC
);
8488 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
8489 SWM_ARG_ID_FLIPLAYOUT
);
8496 /* config options */
8497 struct config_option
{
8499 int (*func
)(const char*, const char*, int);
8502 struct config_option configopt
[] = {
8503 { "autorun", setautorun
, 0 },
8504 { "bar_action", setconfvalue
, SWM_S_BAR_ACTION
},
8505 { "bar_at_bottom", setconfvalue
, SWM_S_BAR_AT_BOTTOM
},
8506 { "bar_border", setconfcolor
, SWM_S_COLOR_BAR_BORDER
},
8507 { "bar_border_unfocus", setconfcolor
, SWM_S_COLOR_BAR_BORDER_UNFOCUS
},
8508 { "bar_border_width", setconfvalue
, SWM_S_BAR_BORDER_WIDTH
},
8509 { "bar_color", setconfcolor
, SWM_S_COLOR_BAR
},
8510 { "bar_delay", setconfvalue
, SWM_S_BAR_DELAY
},
8511 { "bar_enabled", setconfvalue
, SWM_S_BAR_ENABLED
},
8512 { "bar_enabled_ws", setconfvalue
, SWM_S_BAR_ENABLED_WS
},
8513 { "bar_font", setconfvalue
, SWM_S_BAR_FONT
},
8514 { "bar_font_color", setconfcolor
, SWM_S_COLOR_BAR_FONT
},
8515 { "bar_format", setconfvalue
, SWM_S_BAR_FORMAT
},
8516 { "bar_justify", setconfvalue
, SWM_S_BAR_JUSTIFY
},
8517 { "bind", setconfbinding
, 0 },
8518 { "border_width", setconfvalue
, SWM_S_BORDER_WIDTH
},
8519 { "boundary_width", setconfvalue
, SWM_S_BOUNDARY_WIDTH
},
8520 { "clock_enabled", setconfvalue
, SWM_S_CLOCK_ENABLED
},
8521 { "clock_format", setconfvalue
, SWM_S_CLOCK_FORMAT
},
8522 { "color_focus", setconfcolor
, SWM_S_COLOR_FOCUS
},
8523 { "color_focus_maximized", setconfcolor
, SWM_S_COLOR_FOCUS_MAXIMIZED
},
8524 { "color_unfocus", setconfcolor
, SWM_S_COLOR_UNFOCUS
},
8525 { "color_unfocus_maximized", setconfcolor
, SWM_S_COLOR_UNFOCUS_MAXIMIZED
},
8526 { "cycle_empty", setconfvalue
, SWM_S_CYCLE_EMPTY
},
8527 { "cycle_visible", setconfvalue
, SWM_S_CYCLE_VISIBLE
},
8528 { "dialog_ratio", setconfvalue
, SWM_S_DIALOG_RATIO
},
8529 { "disable_border", setconfvalue
, SWM_S_DISABLE_BORDER
},
8530 { "focus_close", setconfvalue
, SWM_S_FOCUS_CLOSE
},
8531 { "focus_close_wrap", setconfvalue
, SWM_S_FOCUS_CLOSE_WRAP
},
8532 { "focus_default", setconfvalue
, SWM_S_FOCUS_DEFAULT
},
8533 { "focus_mode", setconfvalue
, SWM_S_FOCUS_MODE
},
8534 { "iconic_enabled", setconfvalue
, SWM_S_ICONIC_ENABLED
},
8535 { "java_workaround", setconfvalue
, SWM_S_JAVA_WORKAROUND
},
8536 { "keyboard_mapping", setkeymapping
, 0 },
8537 { "layout", setlayout
, 0 },
8538 { "modkey", setconfmodkey
, 0 },
8539 { "program", setconfspawn
, 0 },
8540 { "quirk", setconfquirk
, 0 },
8541 { "region", setconfregion
, 0 },
8542 { "region_padding", setconfvalue
, SWM_S_REGION_PADDING
},
8543 { "screenshot_app", setconfvalue
, SWM_S_SS_APP
},
8544 { "screenshot_enabled", setconfvalue
, SWM_S_SS_ENABLED
},
8545 { "spawn_position", setconfvalue
, SWM_S_SPAWN_ORDER
},
8546 { "spawn_term", setconfvalue
, SWM_S_SPAWN_TERM
},
8547 { "stack_enabled", setconfvalue
, SWM_S_STACK_ENABLED
},
8548 { "term_width", setconfvalue
, SWM_S_TERM_WIDTH
},
8549 { "tile_gap", setconfvalue
, SWM_S_TILE_GAP
},
8550 { "title_class_enabled", setconfvalue
, SWM_S_WINDOW_CLASS_ENABLED
}, /* For backwards compat. */
8551 { "title_name_enabled", setconfvalue
, SWM_S_WINDOW_INSTANCE_ENABLED
}, /* For backwards compat. */
8552 { "urgent_collapse", setconfvalue
, SWM_S_URGENT_COLLAPSE
},
8553 { "urgent_enabled", setconfvalue
, SWM_S_URGENT_ENABLED
},
8554 { "verbose_layout", setconfvalue
, SWM_S_VERBOSE_LAYOUT
},
8555 { "warp_pointer", setconfvalue
, SWM_S_WARP_POINTER
},
8556 { "window_class_enabled", setconfvalue
, SWM_S_WINDOW_CLASS_ENABLED
},
8557 { "window_instance_enabled", setconfvalue
, SWM_S_WINDOW_INSTANCE_ENABLED
},
8558 { "window_name_enabled", setconfvalue
, SWM_S_WINDOW_NAME_ENABLED
},
8559 { "workspace_limit", setconfvalue
, SWM_S_WORKSPACE_LIMIT
},
8560 { "name", setconfvalue
, SWM_S_WORKSPACE_NAME
},
8564 _add_startup_exception(const char *fmt
, va_list ap
)
8566 if (vasprintf(&startup_exception
, fmt
, ap
) == -1)
8567 warn("%s: asprintf", __func__
);
8571 add_startup_exception(const char *fmt
, ...)
8577 if (startup_exception
)
8580 /* force bar to be enabled due to exception */
8584 _add_startup_exception(fmt
, ap
);
8589 conf_load(const char *filename
, int keymapping
)
8592 char *line
= NULL
, *cp
, *ce
, *optsub
, *optval
= NULL
;
8593 size_t linelen
, lineno
= 0;
8594 int wordlen
, i
, optidx
, count
;
8595 struct config_option
*opt
= NULL
;
8597 DNPRINTF(SWM_D_CONF
, "conf_load: begin\n");
8599 if (filename
== NULL
) {
8600 warnx("conf_load: no filename");
8604 DNPRINTF(SWM_D_CONF
, "conf_load: open %s\n", filename
);
8606 if ((config
= fopen(filename
, "r")) == NULL
) {
8607 warn("conf_load: fopen: %s", filename
);
8611 while (!feof(config
)) {
8615 if ((line
= fparseln(config
, &linelen
, &lineno
, NULL
,
8616 FPARSELN_UNESCCOMM
| FPARSELN_UNESCCONT
)) == NULL
) {
8618 err(1, "%s", filename
);
8623 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
8624 if (cp
[0] == '\0') {
8628 /* get config option */
8629 wordlen
= strcspn(cp
, "=[ \t\n");
8631 add_startup_exception("%s: line %zd: no option found",
8636 for (i
= 0; i
< LENGTH(configopt
); i
++) {
8637 opt
= &configopt
[i
];
8638 if (strncasecmp(cp
, opt
->optname
, wordlen
) == 0 &&
8639 (int)strlen(opt
->optname
) == wordlen
) {
8645 add_startup_exception("%s: line %zd: unknown option "
8646 "%.*s", filename
, lineno
, wordlen
, cp
);
8649 if (keymapping
&& opt
&& strcmp(opt
->optname
, "bind")) {
8650 add_startup_exception("%s: line %zd: invalid option "
8651 "%.*s", filename
, lineno
, wordlen
, cp
);
8655 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
8657 /* get [selector] if any */
8662 /* Get length of selector. */
8663 for (ce
= ++cp
; *ce
!= '\0'; ++ce
) {
8664 /* Find matching (not escaped) bracket. */
8665 if (*ce
== ']' && *(ce
- 1) != '\\') {
8672 add_startup_exception("%s: line %zd: syntax "
8673 "error: unterminated selector", filename
,
8678 /* ce points at ']'; terminate optsub. */
8683 cp
+= strspn(cp
, "= \t\n"); /* eat trailing */
8686 if (strlen(optval
) == 0) {
8687 add_startup_exception("%s: line %zd: must supply value "
8688 "to %s", filename
, lineno
,
8689 configopt
[optidx
].optname
);
8692 /* call function to deal with it all */
8693 if (configopt
[optidx
].func(optsub
, optval
,
8694 configopt
[optidx
].funcflags
) != 0) {
8695 add_startup_exception("%s: line %zd: invalid data for "
8696 "%s", filename
, lineno
, configopt
[optidx
].optname
);
8704 DNPRINTF(SWM_D_CONF
, "conf_load: end\n");
8710 set_child_transient(struct ws_win
*win
, xcb_window_t
*trans
)
8712 struct ws_win
*parent
, *w
;
8713 struct swm_region
*r
;
8714 struct workspace
*ws
;
8715 xcb_icccm_wm_hints_t wmh
;
8717 parent
= find_window(win
->transient
);
8719 parent
->focus_child
= win
;
8721 DNPRINTF(SWM_D_MISC
, "set_child_transient: parent doesn't exist"
8722 " for %#x trans %#x\n", win
->id
, win
->transient
);
8724 r
= root_to_region(win
->s
->root
, SWM_CK_ALL
);
8726 /* parent doen't exist in our window list */
8727 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
8728 if (xcb_icccm_get_wm_hints_reply(conn
,
8729 xcb_icccm_get_wm_hints(conn
, w
->id
),
8731 warnx("can't get hints for %#x", w
->id
);
8735 if (win
->hints
.window_group
!= wmh
.window_group
)
8738 w
->focus_child
= win
;
8739 win
->transient
= w
->id
;
8741 DNPRINTF(SWM_D_MISC
, "set_child_transient: adjusting "
8742 "transient to %#x\n", win
->transient
);
8749 window_get_pid(xcb_window_t win
)
8754 xcb_get_property_cookie_t pc
;
8755 xcb_get_property_reply_t
*pr
;
8757 apid
= get_atom_from_string("_NET_WM_PID");
8758 if (apid
== XCB_ATOM_NONE
)
8761 pc
= xcb_get_property(conn
, 0, win
, apid
, XCB_ATOM_CARDINAL
, 0, 1);
8762 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
8765 if (pr
->type
!= XCB_ATOM_CARDINAL
) {
8770 if (pr
->type
== apid
&& pr
->format
== 32)
8771 ret
= *((pid_t
*)xcb_get_property_value(pr
));
8777 apid
= get_atom_from_string("_SWM_PID");
8778 pc
= xcb_get_property(conn
, 0, win
, apid
, XCB_ATOM_STRING
,
8780 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
8783 if (pr
->type
!= apid
) {
8788 ret
= (pid_t
)strtonum(xcb_get_property_value(pr
), 0, INT_MAX
, &errstr
);
8795 get_swm_ws(xcb_window_t id
)
8801 xcb_get_property_reply_t
*gpr
;
8803 gpr
= xcb_get_property_reply(conn
,
8804 xcb_get_property(conn
, 0, id
, a_swm_ws
,
8805 XCB_ATOM_STRING
, 0, SWM_PROPLEN
),
8810 proplen
= xcb_get_property_value_length(gpr
);
8812 prop
= malloc(proplen
+ 1);
8815 xcb_get_property_value(gpr
),
8817 prop
[proplen
] = '\0';
8824 DNPRINTF(SWM_D_PROP
, "get_swm_ws: _SWM_WS: %s\n", prop
);
8825 ws_idx
= (int)strtonum(prop
, 0, workspace_limit
- 1, &errstr
);
8827 DNPRINTF(SWM_D_PROP
, "get_swm_ws: win #%s: %s",
8837 get_ws_idx(struct ws_win
*win
)
8839 xcb_get_property_reply_t
*gpr
;
8845 gpr
= xcb_get_property_reply(conn
,
8846 xcb_get_property(conn
, 0, win
->id
, ewmh
[_NET_WM_DESKTOP
].atom
,
8847 XCB_ATOM_CARDINAL
, 0, 1),
8850 if (gpr
->type
== XCB_ATOM_CARDINAL
&& gpr
->format
== 32)
8851 ws_idx
= *((int *)xcb_get_property_value(gpr
));
8855 if (ws_idx
== -1 && !(win
->quirks
& SWM_Q_IGNORESPAWNWS
))
8856 ws_idx
= get_swm_ws(win
->id
);
8858 if (ws_idx
> workspace_limit
- 1 || ws_idx
< -1)
8861 DNPRINTF(SWM_D_PROP
, "get_ws_idx: win %#x, ws_idx: %d\n", win
->id
,
8868 manage_window(xcb_window_t id
, int spawn_pos
, bool mapped
)
8870 struct ws_win
*win
, *ww
;
8871 struct swm_region
*r
;
8874 xcb_get_geometry_reply_t
*gr
;
8875 xcb_window_t trans
= XCB_WINDOW_NONE
;
8876 uint32_t i
, wa
[2], new_flags
;
8877 int ws_idx
, force_ws
= -1;
8878 char *class, *instance
, *name
;
8880 if ((win
= find_window(id
)) != NULL
) {
8881 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x already "
8882 "managed; skipping.)\n", id
);
8883 return (win
); /* Already managed. */
8886 /* See if window is on the unmanaged list. */
8887 if ((win
= find_unmanaged_window(id
)) != NULL
) {
8888 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x found on "
8889 "unmanaged list.\n", id
);
8890 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
8893 set_child_transient(win
, &trans
);
8897 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x is new.\n", id
);
8900 /* Try to get initial window geometry. */
8901 gr
= xcb_get_geometry_reply(conn
, xcb_get_geometry(conn
, id
), NULL
);
8903 DNPRINTF(SWM_D_MISC
, "manage_window: get geometry failed.\n");
8907 /* Create and initialize ws_win object. */
8908 if ((win
= calloc(1, sizeof(struct ws_win
))) == NULL
)
8909 err(1, "manage_window: calloc: failed to allocate memory for "
8914 /* Figureout which region the window belongs to. */
8915 r
= root_to_region(gr
->root
, SWM_CK_ALL
);
8917 /* Ignore window border if there is one. */
8918 WIDTH(win
) = gr
->width
;
8919 HEIGHT(win
) = gr
->height
;
8920 X(win
) = gr
->x
+ gr
->border_width
- border_width
;
8921 Y(win
) = gr
->y
+ gr
->border_width
- border_width
;
8922 win
->bordered
= true;
8923 win
->mapped
= mapped
;
8924 win
->s
= r
->s
; /* this never changes */
8928 /* Select which X events to monitor and set border pixel color. */
8929 wa
[0] = win
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
;
8930 wa
[1] = XCB_EVENT_MASK_ENTER_WINDOW
| XCB_EVENT_MASK_PROPERTY_CHANGE
|
8931 XCB_EVENT_MASK_STRUCTURE_NOTIFY
;
8933 wa
[1] |= XCB_EVENT_MASK_LEAVE_WINDOW
| XCB_EVENT_MASK_FOCUS_CHANGE
;
8936 xcb_change_window_attributes(conn
, win
->id
, XCB_CW_BORDER_PIXEL
|
8937 XCB_CW_EVENT_MASK
, wa
);
8939 /* Get WM_SIZE_HINTS. */
8940 xcb_icccm_get_wm_normal_hints_reply(conn
,
8941 xcb_icccm_get_wm_normal_hints(conn
, win
->id
),
8945 xcb_icccm_get_wm_hints_reply(conn
,
8946 xcb_icccm_get_wm_hints(conn
, win
->id
),
8949 /* Get WM_TRANSIENT_FOR; see if window is a transient. */
8950 xcb_icccm_get_wm_transient_for_reply(conn
,
8951 xcb_icccm_get_wm_transient_for(conn
, win
->id
),
8954 win
->transient
= trans
;
8955 set_child_transient(win
, &win
->transient
);
8958 /* Get WM_PROTOCOLS. */
8959 get_wm_protocols(win
);
8961 /* Set initial quirks based on EWMH. */
8962 ewmh_autoquirk(win
);
8964 /* Determine initial quirks. */
8965 xcb_icccm_get_wm_class_reply(conn
,
8966 xcb_icccm_get_wm_class(conn
, win
->id
),
8969 class = win
->ch
.class_name
? win
->ch
.class_name
: "";
8970 instance
= win
->ch
.instance_name
? win
->ch
.instance_name
: "";
8971 name
= get_win_name(win
->id
);
8973 DNPRINTF(SWM_D_CLASS
, "manage_window: class: %s, instance: %s, "
8974 "name: %s\n", class, instance
, name
);
8976 /* java is retarded so treat it special */
8977 if (strstr(instance
, "sun-awt")) {
8978 DNPRINTF(SWM_D_CLASS
, "manage_window: java window detected.\n");
8982 TAILQ_FOREACH(qp
, &quirks
, entry
) {
8983 if (regexec(&qp
->regex_class
, class, 0, NULL
, 0) == 0 &&
8984 regexec(&qp
->regex_instance
, instance
, 0, NULL
, 0) == 0 &&
8985 regexec(&qp
->regex_name
, name
, 0, NULL
, 0) == 0) {
8986 DNPRINTF(SWM_D_CLASS
, "manage_window: matched "
8987 "quirk: %s:%s:%s mask: %#x, ws: %d\n", qp
->class,
8988 qp
->instance
, qp
->name
, qp
->quirk
, qp
->ws
);
8989 win
->quirks
= qp
->quirk
;
8990 if (qp
->ws
>= 0 && qp
->ws
< workspace_limit
)
8997 /* Reset font sizes (the bruteforce way; no default keybinding). */
8998 if (win
->quirks
& SWM_Q_XTERM_FONTADJ
) {
8999 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
9000 fake_keypress(win
, XK_KP_Subtract
, XCB_MOD_MASK_SHIFT
);
9001 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
9002 fake_keypress(win
, XK_KP_Add
, XCB_MOD_MASK_SHIFT
);
9005 /* Figure out which workspace the window belongs to. */
9006 if (!(win
->quirks
& SWM_Q_IGNOREPID
) &&
9007 (p
= find_pid(window_get_pid(win
->id
))) != NULL
) {
9008 win
->ws
= &r
->s
->ws
[p
->ws
];
9009 TAILQ_REMOVE(&pidlist
, p
, entry
);
9012 } else if ((ws_idx
= get_ws_idx(win
)) != -1 &&
9014 /* _SWM_WS is set; use that. */
9015 win
->ws
= &r
->s
->ws
[ws_idx
];
9016 } else if (trans
&& (ww
= find_window(trans
)) != NULL
) {
9017 /* Launch transients in the same ws as parent. */
9024 win
->ws
= &r
->s
->ws
[force_ws
];
9026 /* Set the _NET_WM_DESKTOP atom. */
9027 DNPRINTF(SWM_D_PROP
, "manage_window: set _NET_WM_DESKTOP: %d\n",
9029 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
9030 ewmh
[_NET_WM_DESKTOP
].atom
, XCB_ATOM_CARDINAL
, 32, 1, &win
->ws
->idx
);
9032 /* Remove any _SWM_WS now that we set _NET_WM_DESKTOP. */
9033 xcb_delete_property(conn
, win
->id
, a_swm_ws
);
9035 /* WS must already be set for this to work. */
9036 store_float_geom(win
);
9038 /* Make sure window is positioned inside its region, if its active. */
9040 region_containment(win
, r
, SWM_CW_ALLSIDES
|
9041 SWM_CW_HARDBOUNDARY
);
9046 /* Figure out where to insert the window in the workspace list. */
9047 if (trans
&& (ww
= find_window(trans
)))
9048 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, ww
, win
, entry
);
9049 else if (win
->ws
->focus
&& spawn_pos
== SWM_STACK_ABOVE
)
9050 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, win
->ws
->focus
, win
,
9052 else if (win
->ws
->focus
&& spawn_pos
== SWM_STACK_BELOW
)
9053 TAILQ_INSERT_BEFORE(win
->ws
->focus
, win
, entry
);
9054 else switch (spawn_pos
) {
9057 case SWM_STACK_ABOVE
:
9058 TAILQ_INSERT_TAIL(&win
->ws
->winlist
, win
, entry
);
9060 case SWM_STACK_BOTTOM
:
9061 case SWM_STACK_BELOW
:
9062 TAILQ_INSERT_HEAD(&win
->ws
->winlist
, win
, entry
);
9065 ewmh_update_client_list();
9067 TAILQ_INSERT_HEAD(&win
->ws
->stack
, win
, stack_entry
);
9070 /* Get/apply initial _NET_WM_STATE */
9071 ewmh_get_wm_state(win
);
9074 new_flags
= win
->ewmh_flags
;
9076 if (win
->quirks
& SWM_Q_FLOAT
)
9077 new_flags
|= EWMH_F_ABOVE
;
9079 if (win
->quirks
& SWM_Q_ANYWHERE
)
9080 new_flags
|= SWM_F_MANUAL
;
9082 ewmh_apply_flags(win
, new_flags
);
9083 ewmh_update_wm_state(win
);
9085 /* Set initial _NET_WM_ALLOWED_ACTIONS */
9086 ewmh_update_actions(win
);
9090 DNPRINTF(SWM_D_MISC
, "manage_window: done. win %#x, (x,y) w x h: "
9091 "(%d,%d) %d x %d, ws: %d, iconic: %s, transient: %#x\n", win
->id
,
9092 X(win
), Y(win
), WIDTH(win
), HEIGHT(win
), win
->ws
->idx
,
9093 YESNO(ICONIC(win
)), win
->transient
);
9099 free_window(struct ws_win
*win
)
9101 DNPRINTF(SWM_D_MISC
, "free_window: win %#x\n", win
->id
);
9106 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
9108 xcb_icccm_get_wm_class_reply_wipe(&win
->ch
);
9113 memset(win
, 0xff, sizeof *win
); /* XXX kill later */
9116 DNPRINTF(SWM_D_MISC
, "free_window: done.\n");
9120 unmanage_window(struct ws_win
*win
)
9122 struct ws_win
*parent
;
9127 DNPRINTF(SWM_D_MISC
, "unmanage_window: win %#x\n", win
->id
);
9130 parent
= find_window(win
->transient
);
9132 parent
->focus_child
= NULL
;
9135 TAILQ_REMOVE(&win
->ws
->stack
, win
, stack_entry
);
9136 TAILQ_REMOVE(&win
->ws
->winlist
, win
, entry
);
9137 TAILQ_INSERT_TAIL(&win
->ws
->unmanagedlist
, win
, entry
);
9139 ewmh_update_client_list();
9143 expose(xcb_expose_event_t
*e
)
9146 struct swm_region
*r
;
9148 DNPRINTF(SWM_D_EVENT
, "expose: win %#x\n", e
->window
);
9150 num_screens
= get_screen_count();
9151 for (i
= 0; i
< num_screens
; i
++)
9152 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
9153 if (e
->window
== WINID(r
->bar
))
9161 focusin(xcb_focus_in_event_t
*e
)
9163 DNPRINTF(SWM_D_EVENT
, "focusin: win %#x, mode: %s(%u), "
9164 "detail: %s(%u)\n", e
->event
, get_notify_mode_label(e
->mode
),
9165 e
->mode
, get_notify_detail_label(e
->detail
), e
->detail
);
9169 focusout(xcb_focus_out_event_t
*e
)
9171 DNPRINTF(SWM_D_EVENT
, "focusout: win %#x, mode: %s(%u), "
9172 "detail: %s(%u)\n", e
->event
, get_notify_mode_label(e
->mode
),
9173 e
->mode
, get_notify_detail_label(e
->detail
), e
->detail
);
9178 keypress(xcb_key_press_event_t
*e
)
9180 xcb_keysym_t keysym
;
9183 keysym
= xcb_key_press_lookup_keysym(syms
, e
, 0);
9185 DNPRINTF(SWM_D_EVENT
, "keypress: keysym: %u, win (x,y): %#x (%d,%d), "
9186 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
9187 "state: %u, same_screen: %s\n", keysym
, e
->event
, e
->event_x
,
9188 e
->event_y
, e
->detail
, e
->time
, e
->root
, e
->root_x
, e
->root_y
,
9189 e
->child
, e
->state
, YESNO(e
->same_screen
));
9191 if ((kp
= key_lookup(CLEANMASK(e
->state
), keysym
)) == NULL
)
9194 last_event_time
= e
->time
;
9196 if (kp
->funcid
== KF_SPAWN_CUSTOM
)
9197 spawn_custom(root_to_region(e
->root
, SWM_CK_ALL
),
9198 &(keyfuncs
[kp
->funcid
].args
), kp
->spawn_name
);
9199 else if (keyfuncs
[kp
->funcid
].func
)
9200 keyfuncs
[kp
->funcid
].func(root_to_region(e
->root
, SWM_CK_ALL
),
9201 &(keyfuncs
[kp
->funcid
].args
));
9204 /* Unfreeze grab events. */
9205 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
, e
->time
);
9208 DNPRINTF(SWM_D_EVENT
, "keypress: done.\n");
9212 buttonpress(xcb_button_press_event_t
*e
)
9214 struct ws_win
*win
= NULL
;
9215 struct swm_region
*r
, *old_r
;
9217 bool handled
= false;
9219 DNPRINTF(SWM_D_EVENT
, "buttonpress: win (x,y): %#x (%d,%d), "
9220 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
9221 "state: %u, same_screen: %s\n", e
->event
, e
->event_x
, e
->event_y
,
9222 e
->detail
, e
->time
, e
->root
, e
->root_x
, e
->root_y
, e
->child
,
9223 e
->state
, YESNO(e
->same_screen
));
9225 if (e
->event
== e
->root
) {
9226 if (e
->child
!= 0) {
9227 win
= find_window(e
->child
);
9228 /* Pass ButtonPress to window if it isn't managed. */
9232 /* Focus on empty region */
9233 /* If no windows on region if its empty. */
9234 r
= root_to_region(e
->root
, SWM_CK_POINTER
);
9236 DNPRINTF(SWM_D_EVENT
, "buttonpress: "
9237 "NULL region; ignoring.\n");
9241 if (TAILQ_EMPTY(&r
->ws
->winlist
)) {
9242 old_r
= root_to_region(e
->root
, SWM_CK_FOCUS
);
9243 if (old_r
&& old_r
!= r
)
9244 unfocus_win(old_r
->ws
->focus
);
9246 xcb_set_input_focus(conn
,
9247 XCB_INPUT_FOCUS_PARENT
, e
->root
, e
->time
);
9249 /* Clear bar since empty. */
9257 win
= find_window(e
->event
);
9263 last_event_time
= e
->time
;
9265 focus_win(get_focus_magic(win
));
9267 for (i
= 0; i
< LENGTH(buttons
); i
++)
9268 if (client_click
== buttons
[i
].action
&& buttons
[i
].func
&&
9269 buttons
[i
].button
== e
->detail
&&
9270 CLEANMASK(buttons
[i
].mask
) == CLEANMASK(e
->state
)) {
9271 buttons
[i
].func(win
, &buttons
[i
].args
);
9277 DNPRINTF(SWM_D_EVENT
, "buttonpress: passing to window.\n");
9278 /* Replay event to event window */
9279 xcb_allow_events(conn
, XCB_ALLOW_REPLAY_POINTER
, e
->time
);
9281 DNPRINTF(SWM_D_EVENT
, "buttonpress: handled.\n");
9282 /* Unfreeze grab events. */
9283 xcb_allow_events(conn
, XCB_ALLOW_SYNC_POINTER
, e
->time
);
9291 print_win_geom(xcb_window_t w
)
9293 xcb_get_geometry_reply_t
*wa
;
9295 wa
= xcb_get_geometry_reply(conn
, xcb_get_geometry(conn
, w
), NULL
);
9297 DNPRINTF(SWM_D_MISC
, "print_win_geom: window not found: %#x\n",
9302 DNPRINTF(SWM_D_MISC
, "print_win_geom: win %#x, root: %#x, "
9303 "depth: %u, (x,y) w x h: (%d,%d) %d x %d, border: %d\n",
9304 w
, wa
->root
, wa
->depth
, wa
->x
, wa
->y
, wa
->width
, wa
->height
,
9313 get_stack_mode_name(uint8_t mode
)
9318 case XCB_STACK_MODE_ABOVE
:
9321 case XCB_STACK_MODE_BELOW
:
9324 case XCB_STACK_MODE_TOP_IF
:
9327 case XCB_STACK_MODE_BOTTOM_IF
:
9330 case XCB_STACK_MODE_OPPOSITE
:
9342 configurerequest(xcb_configure_request_event_t
*e
)
9345 struct swm_region
*r
= NULL
;
9347 uint32_t wc
[7] = {0};
9351 if ((win
= find_window(e
->window
)) == NULL
)
9352 if ((win
= find_unmanaged_window(e
->window
)) == NULL
)
9356 if (swm_debug
& SWM_D_EVENT
) {
9357 print_win_geom(e
->window
);
9359 DNPRINTF(SWM_D_EVENT
, "configurerequest: win %#x, "
9360 "parent: %#x, new: %s, value_mask: %u { ", e
->window
,
9361 e
->parent
, YESNO(new), e
->value_mask
);
9362 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
)
9363 DPRINTF("X: %d ", e
->x
);
9364 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
)
9365 DPRINTF("Y: %d ", e
->y
);
9366 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
)
9367 DPRINTF("W: %u ", e
->width
);
9368 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
)
9369 DPRINTF("H: %u ", e
->height
);
9370 if (e
->value_mask
& XCB_CONFIG_WINDOW_BORDER_WIDTH
)
9371 DPRINTF("Border: %u ", e
->border_width
);
9372 if (e
->value_mask
& XCB_CONFIG_WINDOW_SIBLING
)
9373 DPRINTF("Sibling: %#x ", e
->sibling
);
9374 if (e
->value_mask
& XCB_CONFIG_WINDOW_STACK_MODE
)
9375 DPRINTF("StackMode: %s(%u) ",
9376 get_stack_mode_name(e
->stack_mode
), e
->stack_mode
);
9382 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
) {
9383 mask
|= XCB_CONFIG_WINDOW_X
;
9386 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
) {
9387 mask
|= XCB_CONFIG_WINDOW_Y
;
9390 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
) {
9391 mask
|= XCB_CONFIG_WINDOW_WIDTH
;
9394 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
) {
9395 mask
|= XCB_CONFIG_WINDOW_HEIGHT
;
9396 wc
[i
++] = e
->height
;
9398 if (e
->value_mask
& XCB_CONFIG_WINDOW_BORDER_WIDTH
) {
9399 mask
|= XCB_CONFIG_WINDOW_BORDER_WIDTH
;
9400 wc
[i
++] = e
->border_width
;
9402 if (e
->value_mask
& XCB_CONFIG_WINDOW_SIBLING
) {
9403 mask
|= XCB_CONFIG_WINDOW_SIBLING
;
9404 wc
[i
++] = e
->sibling
;
9406 if (e
->value_mask
& XCB_CONFIG_WINDOW_STACK_MODE
) {
9407 mask
|= XCB_CONFIG_WINDOW_STACK_MODE
;
9408 wc
[i
++] = e
->stack_mode
;
9412 xcb_configure_window(conn
, e
->window
, mask
, wc
);
9415 } else if ((!MANUAL(win
) || win
->quirks
& SWM_Q_ANYWHERE
) &&
9416 !FULLSCREEN(win
) && !MAXIMIZED(win
)) {
9419 else if (win
->ws
->old_r
)
9422 /* windows are centered unless ANYWHERE quirk is set. */
9423 if (win
->quirks
& SWM_Q_ANYWHERE
) {
9424 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
) {
9425 win
->g_float
.x
= e
->x
;
9427 win
->g_float
.x
-= X(r
);
9430 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
) {
9431 win
->g_float
.y
= e
->y
;
9433 win
->g_float
.y
-= Y(r
);
9437 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
)
9438 win
->g_float
.w
= e
->width
;
9440 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
)
9441 win
->g_float
.h
= e
->height
;
9443 win
->g_floatvalid
= true;
9445 if (!MAXIMIZED(win
) && !FULLSCREEN(win
) &&
9446 (TRANS(win
) || (ABOVE(win
) &&
9447 win
->ws
->cur_layout
!= &layouts
[SWM_MAX_STACK
]))) {
9448 WIDTH(win
) = win
->g_float
.w
;
9449 HEIGHT(win
) = win
->g_float
.h
;
9452 update_floater(win
);
9467 DNPRINTF(SWM_D_EVENT
, "configurerequest: done.\n");
9471 configurenotify(xcb_configure_notify_event_t
*e
)
9475 DNPRINTF(SWM_D_EVENT
, "configurenotify: win %#x, event win: %#x, "
9476 "(x,y) WxH: (%d,%d) %ux%u, border: %u, above_sibling: %#x, "
9477 "override_redirect: %s\n", e
->window
, e
->event
, e
->x
, e
->y
,
9478 e
->width
, e
->height
, e
->border_width
, e
->above_sibling
,
9479 YESNO(e
->override_redirect
));
9481 win
= find_window(e
->window
);
9483 xcb_icccm_get_wm_normal_hints_reply(conn
,
9484 xcb_icccm_get_wm_normal_hints(conn
, win
->id
),
9487 if (font_adjusted
) {
9495 destroynotify(xcb_destroy_notify_event_t
*e
)
9499 DNPRINTF(SWM_D_EVENT
, "destroynotify: win %#x\n", e
->window
);
9501 if ((win
= find_window(e
->window
)) == NULL
) {
9502 if ((win
= find_unmanaged_window(e
->window
)) == NULL
)
9508 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
9509 /* If we were focused, make sure we focus on something else. */
9510 if (win
== win
->ws
->focus
)
9511 win
->ws
->focus_pending
= get_focus_prev(win
);
9514 unmanage_window(win
);
9517 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& WS_FOCUSED(win
->ws
)) {
9518 if (win
->ws
->focus_pending
) {
9519 focus_win(win
->ws
->focus_pending
);
9520 win
->ws
->focus_pending
= NULL
;
9521 } else if (win
== win
->ws
->focus
) {
9522 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
,
9523 win
->ws
->r
->id
, XCB_CURRENT_TIME
);
9534 get_notify_detail_label(uint8_t detail
)
9539 case XCB_NOTIFY_DETAIL_ANCESTOR
:
9542 case XCB_NOTIFY_DETAIL_VIRTUAL
:
9545 case XCB_NOTIFY_DETAIL_INFERIOR
:
9548 case XCB_NOTIFY_DETAIL_NONLINEAR
:
9549 label
= "Nonlinear";
9551 case XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL
:
9552 label
= "NonlinearVirtual";
9554 case XCB_NOTIFY_DETAIL_POINTER
:
9557 case XCB_NOTIFY_DETAIL_POINTER_ROOT
:
9558 label
= "PointerRoot";
9560 case XCB_NOTIFY_DETAIL_NONE
:
9571 get_notify_mode_label(uint8_t mode
)
9576 case XCB_NOTIFY_MODE_NORMAL
:
9579 case XCB_NOTIFY_MODE_GRAB
:
9582 case XCB_NOTIFY_MODE_UNGRAB
:
9585 case XCB_NOTIFY_MODE_WHILE_GRABBED
:
9586 label
= "WhileGrabbed";
9597 enternotify(xcb_enter_notify_event_t
*e
)
9600 struct swm_region
*r
;
9602 DNPRINTF(SWM_D_FOCUS
, "enternotify: time: %u, win (x,y): %#x "
9603 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
9604 "child: %#x, same_screen_focus: %s, state: %d\n",
9605 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
9606 get_notify_mode_label(e
->mode
), e
->mode
,
9607 get_notify_detail_label(e
->detail
), e
->detail
,
9608 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
9609 YESNO(e
->same_screen_focus
), e
->state
);
9611 if (focus_mode
== SWM_FOCUS_MANUAL
&&
9612 e
->mode
== XCB_NOTIFY_MODE_NORMAL
) {
9613 DNPRINTF(SWM_D_EVENT
, "enternotify: manual focus; ignoring.\n");
9617 last_event_time
= e
->time
;
9619 if ((win
= find_window(e
->event
)) == NULL
) {
9620 if (e
->event
== e
->root
) {
9621 /* If no windows on pointer region, then focus root. */
9622 r
= root_to_region(e
->root
, SWM_CK_POINTER
);
9624 DNPRINTF(SWM_D_EVENT
, "enternotify: "
9625 "NULL region; ignoring.\n");
9631 DNPRINTF(SWM_D_EVENT
, "enternotify: window is NULL; "
9636 if (e
->mode
== XCB_NOTIFY_MODE_NORMAL
&&
9637 e
->detail
== XCB_NOTIFY_DETAIL_INFERIOR
) {
9638 DNPRINTF(SWM_D_EVENT
, "enternotify: entering from "
9639 "inferior; ignoring\n");
9643 focus_win(get_focus_magic(win
));
9651 leavenotify(xcb_leave_notify_event_t
*e
)
9653 DNPRINTF(SWM_D_FOCUS
, "leavenotify: time: %u, win (x,y): %#x "
9654 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
9655 "child: %#x, same_screen_focus: %s, state: %d\n",
9656 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
9657 get_notify_mode_label(e
->mode
), e
->mode
,
9658 get_notify_detail_label(e
->detail
), e
->detail
,
9659 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
9660 YESNO(e
->same_screen_focus
), e
->state
);
9665 mapnotify(xcb_map_notify_event_t
*e
)
9667 struct ws_win
*win
, *parent
= NULL
;
9668 struct workspace
*ws
;
9670 DNPRINTF(SWM_D_EVENT
, "mapnotify: win %#x\n", e
->window
);
9672 if ((win
= manage_window(e
->window
, spawn_position
, true)) == NULL
)
9676 /* Need to know if win was mapped due to ws switch. */
9677 if (ws
->state
== SWM_WS_STATE_MAPPED
) {
9678 if (ws
->focus_pending
&& TRANS(ws
->focus_pending
))
9679 parent
= find_window(win
->transient
);
9681 /* If window's parent is maximized, don't clear it. */
9682 if ((parent
== NULL
) || !MAXIMIZED(parent
))
9683 if (clear_maximized(ws
) > 0)
9688 set_win_state(win
, XCB_ICCCM_WM_STATE_NORMAL
);
9690 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& WS_FOCUSED(win
->ws
)) {
9691 if (ws
->focus_pending
== win
) {
9693 ws
->focus_pending
= NULL
;
9694 center_pointer(win
->ws
->r
);
9703 mappingnotify(xcb_mapping_notify_event_t
*e
)
9706 int i
, j
, num_screens
;
9708 xcb_refresh_keyboard_mapping(syms
, e
);
9710 if (e
->request
== XCB_MAPPING_KEYBOARD
) {
9713 /* Regrab buttons on all managed windows. */
9714 num_screens
= get_screen_count();
9715 for (i
= 0; i
< num_screens
; i
++)
9716 for (j
= 0; j
< workspace_limit
; j
++)
9717 TAILQ_FOREACH(w
, &screens
[i
].ws
[j
].winlist
,
9724 maprequest(xcb_map_request_event_t
*e
)
9726 struct ws_win
*win
, *w
= NULL
;
9727 xcb_get_window_attributes_reply_t
*war
;
9729 DNPRINTF(SWM_D_EVENT
, "maprequest: win %#x\n",
9732 war
= xcb_get_window_attributes_reply(conn
,
9733 xcb_get_window_attributes(conn
, e
->window
),
9736 DNPRINTF(SWM_D_EVENT
, "maprequest: window lost.\n");
9740 if (war
->override_redirect
) {
9741 DNPRINTF(SWM_D_EVENT
, "maprequest: override_redirect; "
9746 win
= manage_window(e
->window
, spawn_position
,
9747 (war
->map_state
== XCB_MAP_STATE_VIEWABLE
));
9751 /* The new window should get focus; prepare. */
9752 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
9753 !(win
->quirks
& SWM_Q_NOFOCUSONMAP
) &&
9754 (!(win
->hints
.flags
& XCB_ICCCM_WM_HINT_INPUT
) ||
9755 (win
->hints
.flags
& XCB_ICCCM_WM_HINT_INPUT
&&
9756 win
->hints
.input
))) {
9757 if (win
->quirks
& SWM_Q_FOCUSONMAP_SINGLE
) {
9758 /* See if other wins of same type are already mapped. */
9759 TAILQ_FOREACH(w
, &win
->ws
->winlist
, entry
) {
9760 if (w
== win
|| !w
->mapped
)
9763 if (w
->ch
.class_name
&&
9764 win
->ch
.class_name
&&
9765 strcmp(w
->ch
.class_name
,
9766 win
->ch
.class_name
) == 0 &&
9767 w
->ch
.instance_name
&&
9768 win
->ch
.instance_name
&&
9769 strcmp(w
->ch
.instance_name
,
9770 win
->ch
.instance_name
) == 0)
9776 win
->ws
->focus_pending
= get_focus_magic(win
);
9779 /* All windows need to be mapped if they are in the current workspace.*/
9783 /* Ignore EnterNotify to handle the mapnotify without interference. */
9784 if (focus_mode
== SWM_FOCUS_DEFAULT
)
9785 event_drain(XCB_ENTER_NOTIFY
);
9788 DNPRINTF(SWM_D_EVENT
, "maprequest: done.\n");
9792 motionnotify(xcb_motion_notify_event_t
*e
)
9794 struct swm_region
*r
= NULL
;
9797 DNPRINTF(SWM_D_FOCUS
, "motionnotify: time: %u, win (x,y): %#x "
9798 "(%d,%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
9799 "child: %#x, same_screen_focus: %s, state: %d\n",
9800 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
9801 get_notify_detail_label(e
->detail
), e
->detail
,
9802 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
9803 YESNO(e
->same_screen
), e
->state
);
9805 last_event_time
= e
->time
;
9807 if (focus_mode
== SWM_FOCUS_MANUAL
)
9810 num_screens
= get_screen_count();
9811 for (i
= 0; i
< num_screens
; i
++)
9812 if (screens
[i
].root
== e
->root
)
9815 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
9816 if (X(r
) <= e
->root_x
&& e
->root_x
< MAX_X(r
) &&
9817 Y(r
) <= e
->root_y
&& e
->root_y
< MAX_Y(r
))
9825 get_atom_name(xcb_atom_t atom
)
9828 #ifdef SWM_DEBUG_ATOM_NAMES
9830 * This should be disabled during most debugging since
9831 * xcb_get_* causes an xcb_flush.
9834 xcb_get_atom_name_reply_t
*r
;
9836 r
= xcb_get_atom_name_reply(conn
,
9837 xcb_get_atom_name(conn
, atom
),
9840 len
= xcb_get_atom_name_name_length(r
);
9842 name
= malloc(len
+ 1);
9844 memcpy(name
, xcb_get_atom_name_name(r
), len
);
9858 propertynotify(xcb_property_notify_event_t
*e
)
9861 struct workspace
*ws
;
9865 name
= get_atom_name(e
->atom
);
9866 DNPRINTF(SWM_D_EVENT
, "propertynotify: win %#x, atom: %s(%u), "
9867 "time: %#x, state: %u\n", e
->window
, name
, e
->atom
, e
->time
,
9871 win
= find_window(e
->window
);
9877 last_event_time
= e
->time
;
9879 if (e
->atom
== a_state
) {
9880 /* State just changed, make sure it gets focused if mapped. */
9881 if (e
->state
== XCB_PROPERTY_NEW_VALUE
) {
9882 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& WS_FOCUSED(ws
)) {
9884 ws
->focus_pending
== win
) {
9885 focus_win(ws
->focus_pending
);
9886 ws
->focus_pending
= NULL
;
9890 } else if (e
->atom
== XCB_ATOM_WM_CLASS
||
9891 e
->atom
== XCB_ATOM_WM_NAME
) {
9893 } else if (e
->atom
== a_prot
) {
9894 get_wm_protocols(win
);
9901 unmapnotify(xcb_unmap_notify_event_t
*e
)
9904 struct workspace
*ws
;
9906 DNPRINTF(SWM_D_EVENT
, "unmapnotify: win %#x\n", e
->window
);
9908 /* If we aren't managing the window, then ignore. */
9909 win
= find_window(e
->window
);
9910 if (win
== NULL
|| win
->id
!= e
->window
)
9913 /* Do nothing if already withdrawn. */
9914 if (!win
->mapped
&& !ICONIC(win
))
9918 win
->mapped
= false;
9920 /* If win was focused, make sure to focus on something else. */
9921 if (win
== ws
->focus
) {
9922 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
9923 ws
->focus_pending
= get_focus_prev(win
);
9924 DNPRINTF(SWM_D_EVENT
, "unmapnotify: "
9925 "focus_pending: %#x\n",
9926 WINID(ws
->focus_pending
));
9934 set_win_state(win
, XCB_ICCCM_WM_STATE_ICONIC
);
9937 set_win_state(win
, XCB_ICCCM_WM_STATE_WITHDRAWN
);
9938 unmanage_window(win
);
9944 /* Update focus if ws is active. */
9945 if (WS_FOCUSED(ws
)) {
9946 if (focus_mode
== SWM_FOCUS_FOLLOW
) {
9947 focus_win(get_pointer_win(ws
->r
->s
->root
));
9948 } else if (ws
->focus_pending
) {
9949 focus_win(ws
->focus_pending
);
9950 ws
->focus_pending
= NULL
;
9951 } else if (ws
->focus
== NULL
) {
9952 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
,
9953 ws
->r
->id
, XCB_CURRENT_TIME
);
9957 center_pointer(ws
->r
);
9963 get_source_type_label(uint32_t type
)
9968 case EWMH_SOURCE_TYPE_NONE
:
9971 case EWMH_SOURCE_TYPE_NORMAL
:
9974 case EWMH_SOURCE_TYPE_OTHER
:
9986 clientmessage(xcb_client_message_event_t
*e
)
9989 struct swm_region
*r
= NULL
;
9993 xcb_map_request_event_t mre
;
9997 name
= get_atom_name(e
->type
);
9998 DNPRINTF(SWM_D_EVENT
, "clientmessage: win %#x, atom: %s(%u)\n",
9999 e
->window
, name
, e
->type
);
10003 if (e
->type
== ewmh
[_NET_CURRENT_DESKTOP
].atom
) {
10004 num_screens
= get_screen_count();
10005 for (i
= 0; i
< num_screens
; i
++)
10006 if (screens
[i
].root
== e
->window
) {
10007 r
= screens
[i
].r_focus
;
10011 if (r
&& e
->data
.data32
[0] < (uint32_t)workspace_limit
) {
10012 a
.id
= e
->data
.data32
[0];
10020 win
= find_window(e
->window
);
10022 if (e
->type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
10023 /* Manage the window with maprequest. */
10024 DNPRINTF(SWM_D_EVENT
, "clientmessage: request focus on "
10025 "unmanaged window.\n");
10026 mre
.window
= e
->window
;
10032 if (e
->type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
10033 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_ACTIVE_WINDOW, "
10034 "source_type: %s(%d)\n",
10035 get_source_type_label(e
->data
.data32
[0]),
10036 e
->data
.data32
[0]);
10039 * Allow focus changes that are a result of direct user
10040 * action and from applications that use the old EWMH spec.
10042 if (e
->data
.data32
[0] != EWMH_SOURCE_TYPE_NORMAL
||
10043 win
->quirks
& SWM_Q_OBEYAPPFOCUSREQ
) {
10044 if (WS_FOCUSED(win
->ws
))
10047 win
->ws
->focus_pending
= win
;
10049 } else if (e
->type
== ewmh
[_NET_CLOSE_WINDOW
].atom
) {
10050 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_CLOSE_WINDOW\n");
10051 if (win
->can_delete
)
10052 client_msg(win
, a_delete
, 0);
10054 xcb_kill_client(conn
, win
->id
);
10055 } else if (e
->type
== ewmh
[_NET_MOVERESIZE_WINDOW
].atom
) {
10056 DNPRINTF(SWM_D_EVENT
,
10057 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
10059 if (e
->data
.data32
[0] & (1<<8)) /* x */
10060 X(win
) = e
->data
.data32
[1];
10061 if (e
->data
.data32
[0] & (1<<9)) /* y */
10062 Y(win
) = e
->data
.data32
[2];
10063 if (e
->data
.data32
[0] & (1<<10)) /* width */
10064 WIDTH(win
) = e
->data
.data32
[3];
10065 if (e
->data
.data32
[0] & (1<<11)) /* height */
10066 HEIGHT(win
) = e
->data
.data32
[4];
10068 update_window(win
);
10070 /* Notify no change was made. */
10071 config_win(win
, NULL
);
10072 /* TODO: Change stack sizes */
10074 } else if (e
->type
== ewmh
[_NET_RESTACK_WINDOW
].atom
) {
10075 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_RESTACK_WINDOW\n");
10076 val
[0] = e
->data
.data32
[1]; /* Sibling window. */
10077 val
[1] = e
->data
.data32
[2]; /* Stack mode detail. */
10079 xcb_configure_window(conn
, win
->id
, XCB_CONFIG_WINDOW_SIBLING
|
10080 XCB_CONFIG_WINDOW_STACK_MODE
, val
);
10081 } else if (e
->type
== ewmh
[_NET_WM_STATE
].atom
) {
10082 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_WM_STATE\n");
10083 ewmh_change_wm_state(win
, e
->data
.data32
[1], e
->data
.data32
[0]);
10084 if (e
->data
.data32
[2])
10085 ewmh_change_wm_state(win
, e
->data
.data32
[2],
10086 e
->data
.data32
[0]);
10088 ewmh_update_wm_state(win
);
10090 } else if (e
->type
== ewmh
[_NET_WM_DESKTOP
].atom
) {
10091 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_WM_DESKTOP\n");
10094 win_to_ws(win
, e
->data
.data32
[0], true);
10096 /* Restack if either the source or destination ws is mapped. */
10097 if (r
!= NULL
|| win
->ws
->r
!= NULL
) {
10099 load_float_geom(win
);
10111 int errcode
= xcb_connection_has_error(conn
);
10112 #ifdef XCB_CONN_ERROR
10115 case XCB_CONN_ERROR
:
10116 s
= "Socket error, pipe error or other stream error.";
10118 case XCB_CONN_CLOSED_EXT_NOTSUPPORTED
:
10119 s
= "Extension not supported.";
10121 case XCB_CONN_CLOSED_MEM_INSUFFICIENT
:
10122 s
= "Insufficient memory.";
10124 case XCB_CONN_CLOSED_REQ_LEN_EXCEED
:
10125 s
= "Request length was exceeded.";
10127 case XCB_CONN_CLOSED_PARSE_ERR
:
10128 s
= "Error parsing display string.";
10131 s
= "Unknown error.";
10134 errx(errcode
, "X CONNECTION ERROR: %s", s
);
10137 errx(errcode
, "X CONNECTION ERROR");
10144 int num_screens
, i
;
10145 const uint32_t val
= XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT
|
10146 XCB_EVENT_MASK_ENTER_WINDOW
;
10148 xcb_void_cookie_t wac
;
10149 xcb_generic_error_t
*error
;
10151 /* this causes an error if some other window manager is running */
10152 num_screens
= get_screen_count();
10153 for (i
= 0; i
< num_screens
; i
++) {
10154 if ((sc
= get_screen(i
)) == NULL
)
10155 errx(1, "ERROR: can't get screen %d.", i
);
10156 DNPRINTF(SWM_D_INIT
, "enable_wm: screen %d, root: %#x\n",
10158 wac
= xcb_change_window_attributes_checked(conn
, sc
->root
,
10159 XCB_CW_EVENT_MASK
, &val
);
10160 if ((error
= xcb_request_check(conn
, wac
))) {
10161 DNPRINTF(SWM_D_INIT
, "enable_wm: error_code: %u\n",
10162 error
->error_code
);
10167 /* click to focus on empty region */
10168 xcb_grab_button(conn
, 1, sc
->root
, BUTTONMASK
,
10169 XCB_GRAB_MODE_SYNC
, XCB_GRAB_MODE_ASYNC
, XCB_WINDOW_NONE
,
10170 XCB_CURSOR_NONE
, XCB_BUTTON_INDEX_1
, XCB_BUTTON_MASK_ANY
);
10177 new_region(struct swm_screen
*s
, int x
, int y
, int w
, int h
)
10179 struct swm_region
*r
= NULL
, *n
;
10180 struct workspace
*ws
= NULL
;
10184 DNPRINTF(SWM_D_MISC
, "new region: screen[%d]:%dx%d+%d+%d\n",
10185 s
->idx
, w
, h
, x
, y
);
10187 /* remove any conflicting regions */
10188 n
= TAILQ_FIRST(&s
->rl
);
10191 n
= TAILQ_NEXT(r
, entry
);
10192 if (X(r
) < (x
+ w
) && (X(r
) + WIDTH(r
)) > x
&&
10193 Y(r
) < (y
+ h
) && (Y(r
) + HEIGHT(r
)) > y
) {
10194 if (r
->ws
->r
!= NULL
)
10195 r
->ws
->old_r
= r
->ws
->r
;
10198 xcb_destroy_window(conn
, r
->id
);
10199 TAILQ_REMOVE(&s
->rl
, r
, entry
);
10200 TAILQ_INSERT_TAIL(&s
->orl
, r
, entry
);
10204 /* search old regions for one to reuse */
10206 /* size + location match */
10207 TAILQ_FOREACH(r
, &s
->orl
, entry
)
10208 if (X(r
) == x
&& Y(r
) == y
&&
10209 HEIGHT(r
) == h
&& WIDTH(r
) == w
)
10213 TAILQ_FOREACH(r
, &s
->orl
, entry
)
10214 if (HEIGHT(r
) == h
&& WIDTH(r
) == w
)
10218 TAILQ_REMOVE(&s
->orl
, r
, entry
);
10219 /* try to use old region's workspace */
10220 if (r
->ws
->r
== NULL
)
10223 if ((r
= calloc(1, sizeof(struct swm_region
))) == NULL
)
10224 err(1, "new_region: calloc: failed to allocate memory "
10227 /* if we don't have a workspace already, find one */
10229 for (i
= 0; i
< workspace_limit
; i
++)
10230 if (s
->ws
[i
].r
== NULL
) {
10237 errx(1, "new_region: no free workspaces");
10239 if (ws
->state
== SWM_WS_STATE_HIDDEN
)
10240 ws
->state
= SWM_WS_STATE_MAPPING
;
10248 r
->ws_prior
= NULL
;
10251 TAILQ_INSERT_TAIL(&s
->rl
, r
, entry
);
10253 /* Invisible region window to detect pointer events on empty regions. */
10254 r
->id
= xcb_generate_id(conn
);
10255 wa
[0] = XCB_EVENT_MASK_POINTER_MOTION
|
10256 XCB_EVENT_MASK_POINTER_MOTION_HINT
;
10258 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, r
->id
, r
->s
->root
,
10259 X(r
), Y(r
), WIDTH(r
), HEIGHT(r
), 0, XCB_WINDOW_CLASS_INPUT_ONLY
,
10260 XCB_COPY_FROM_PARENT
, XCB_CW_EVENT_MASK
, wa
);
10262 /* Make sure region input is at the bottom. */
10263 wa
[0] = XCB_STACK_MODE_BELOW
;
10264 xcb_configure_window(conn
, r
->id
, XCB_CONFIG_WINDOW_STACK_MODE
, wa
);
10266 xcb_map_window(conn
, r
->id
);
10270 scan_randr(int idx
)
10272 #ifdef SWM_XRR_HAS_CRTC
10275 #endif /* SWM_XRR_HAS_CRTC */
10276 struct swm_region
*r
;
10277 struct ws_win
*win
;
10279 xcb_randr_get_screen_resources_current_cookie_t src
;
10280 xcb_randr_get_screen_resources_current_reply_t
*srr
;
10281 xcb_randr_get_crtc_info_cookie_t cic
;
10282 xcb_randr_get_crtc_info_reply_t
*cir
= NULL
;
10283 xcb_randr_crtc_t
*crtc
;
10284 xcb_screen_t
*screen
;
10286 DNPRINTF(SWM_D_MISC
, "scan_randr: screen: %d\n", idx
);
10288 if ((screen
= get_screen(idx
)) == NULL
)
10289 errx(1, "ERROR: can't get screen %d.", idx
);
10291 num_screens
= get_screen_count();
10292 if (idx
>= num_screens
)
10293 errx(1, "scan_randr: invalid screen");
10295 /* remove any old regions */
10296 while ((r
= TAILQ_FIRST(&screens
[idx
].rl
)) != NULL
) {
10297 r
->ws
->old_r
= r
->ws
->r
= NULL
;
10299 xcb_destroy_window(conn
, r
->id
);
10300 TAILQ_REMOVE(&screens
[idx
].rl
, r
, entry
);
10301 TAILQ_INSERT_TAIL(&screens
[idx
].orl
, r
, entry
);
10305 /* map virtual screens onto physical screens */
10306 #ifdef SWM_XRR_HAS_CRTC
10307 if (randr_support
) {
10308 src
= xcb_randr_get_screen_resources_current(conn
,
10309 screens
[idx
].root
);
10310 srr
= xcb_randr_get_screen_resources_current_reply(conn
, src
,
10313 new_region(&screens
[idx
], 0, 0,
10314 screen
->width_in_pixels
,
10315 screen
->height_in_pixels
);
10318 ncrtc
= srr
->num_crtcs
;
10320 crtc
= xcb_randr_get_screen_resources_current_crtcs(srr
);
10321 for (c
= 0; c
< ncrtc
; c
++) {
10322 cic
= xcb_randr_get_crtc_info(conn
, crtc
[c
],
10324 cir
= xcb_randr_get_crtc_info_reply(conn
, cic
, NULL
);
10327 if (cir
->num_outputs
== 0) {
10332 if (cir
->mode
== 0)
10333 new_region(&screens
[idx
], 0, 0,
10334 screen
->width_in_pixels
,
10335 screen
->height_in_pixels
);
10337 new_region(&screens
[idx
],
10338 cir
->x
, cir
->y
, cir
->width
, cir
->height
);
10343 #endif /* SWM_XRR_HAS_CRTC */
10345 /* If detection failed, create a single region that spans the screen. */
10346 if (TAILQ_EMPTY(&screens
[idx
].rl
))
10347 new_region(&screens
[idx
], 0, 0, screen
->width_in_pixels
,
10348 screen
->height_in_pixels
);
10351 /* Cleanup unused previously visible workspaces. */
10352 TAILQ_FOREACH(r
, &screens
[idx
].orl
, entry
) {
10353 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
)
10355 r
->ws
->state
= SWM_WS_STATE_HIDDEN
;
10357 /* The screen shouldn't focus on an unused region. */
10358 if (screens
[idx
].r_focus
== r
)
10359 screens
[idx
].r_focus
= NULL
;
10362 DNPRINTF(SWM_D_MISC
, "scan_randr: done.\n");
10366 screenchange(xcb_randr_screen_change_notify_event_t
*e
)
10368 struct swm_region
*r
;
10369 int i
, num_screens
;
10371 DNPRINTF(SWM_D_EVENT
, "screenchange: root: %#x\n", e
->root
);
10373 num_screens
= get_screen_count();
10374 /* silly event doesn't include the screen index */
10375 for (i
= 0; i
< num_screens
; i
++)
10376 if (screens
[i
].root
== e
->root
)
10378 if (i
>= num_screens
)
10379 errx(1, "screenchange: screen not found");
10381 /* brute force for now, just re-enumerate the regions */
10385 print_win_geom(e
->root
);
10387 /* add bars to all regions */
10388 for (i
= 0; i
< num_screens
; i
++) {
10389 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
10395 /* Make sure a region has focus on each screen. */
10396 for (i
= 0; i
< num_screens
; i
++) {
10397 if (screens
[i
].r_focus
== NULL
) {
10398 r
= TAILQ_FIRST(&screens
[i
].rl
);
10407 /* Update workspace state on all regions. */
10408 for (i
= 0; i
< num_screens
; i
++)
10409 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
10410 r
->ws
->state
= SWM_WS_STATE_MAPPED
;
10416 struct swm_region
*r
= NULL
;
10417 xcb_window_t
*wins
= NULL
, trans
, *cwins
= NULL
;
10418 int i
, j
, k
, n
, no
, num_screens
;
10420 bool manage
, mapped
;
10422 xcb_query_tree_cookie_t qtc
;
10423 xcb_query_tree_reply_t
*qtr
;
10424 xcb_get_window_attributes_cookie_t gac
;
10425 xcb_get_window_attributes_reply_t
*gar
;
10426 xcb_get_property_cookie_t pc
;
10427 xcb_get_property_reply_t
*pr
;
10429 DNPRINTF(SWM_D_INIT
, "grab_windows: begin\n");
10430 num_screens
= get_screen_count();
10431 for (i
= 0; i
< num_screens
; i
++) {
10432 qtc
= xcb_query_tree(conn
, screens
[i
].root
);
10433 qtr
= xcb_query_tree_reply(conn
, qtc
, NULL
);
10436 wins
= xcb_query_tree_children(qtr
);
10437 no
= xcb_query_tree_children_length(qtr
);
10439 /* Try to sort windows according to _NET_CLIENT_LIST. */
10440 pr
= xcb_get_property_reply(conn
, xcb_get_property(conn
, 0,
10441 screens
[i
].root
, ewmh
[_NET_CLIENT_LIST
].atom
,
10442 XCB_ATOM_WINDOW
, 0, UINT32_MAX
), NULL
);
10444 cwins
= xcb_get_property_value(pr
);
10445 n
= xcb_get_property_value_length(pr
) /
10446 sizeof(xcb_atom_t
);
10448 for (j
= 0; j
< n
; ++j
) {
10449 for (k
= j
; k
< no
; ++k
) {
10450 if (wins
[k
] == cwins
[j
]) {
10451 /* Swap wins j and k. */
10453 wins
[j
] = cwins
[j
];
10461 /* attach windows to a region */
10462 /* normal windows */
10463 DNPRINTF(SWM_D_INIT
, "grab_windows: grab top level windows.\n");
10464 for (j
= 0; j
< no
; j
++) {
10465 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
10466 if (r
->id
== wins
[j
]) {
10467 DNPRINTF(SWM_D_INIT
, "grab_windows: "
10468 "skip %#x; region input window.\n",
10471 } else if (r
->bar
->id
== wins
[j
]) {
10472 DNPRINTF(SWM_D_INIT
, "grab_windows: "
10473 "skip %#x; region bar.\n",
10482 gac
= xcb_get_window_attributes(conn
, wins
[j
]);
10483 gar
= xcb_get_window_attributes_reply(conn
, gac
, NULL
);
10485 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
10486 "doesn't exist.\n", wins
[j
]);
10490 if (gar
->override_redirect
) {
10491 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
10492 "override_redirect set.\n", wins
[j
]);
10497 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
10498 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
10500 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
10501 "is transient for %#x.\n", wins
[j
], trans
);
10506 state
= get_win_state(wins
[j
]);
10507 manage
= state
!= XCB_ICCCM_WM_STATE_WITHDRAWN
;
10508 mapped
= gar
->map_state
== XCB_MAP_STATE_VIEWABLE
;
10509 if (mapped
|| manage
)
10510 manage_window(wins
[j
], SWM_STACK_TOP
, mapped
);
10513 /* transient windows */
10514 DNPRINTF(SWM_D_INIT
, "grab_windows: grab transient windows.\n");
10515 for (j
= 0; j
< no
; j
++) {
10516 gac
= xcb_get_window_attributes(conn
, wins
[j
]);
10517 gar
= xcb_get_window_attributes_reply(conn
, gac
, NULL
);
10519 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
10520 "doesn't exist.\n", wins
[j
]);
10524 if (gar
->override_redirect
) {
10525 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
10526 "override_redirect set.\n", wins
[j
]);
10531 state
= get_win_state(wins
[j
]);
10532 manage
= state
!= XCB_ICCCM_WM_STATE_WITHDRAWN
;
10533 mapped
= gar
->map_state
== XCB_MAP_STATE_VIEWABLE
;
10534 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
10535 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
10536 &trans
, NULL
) && manage
)
10537 manage_window(wins
[j
], SWM_STACK_TOP
, mapped
);
10542 DNPRINTF(SWM_D_INIT
, "grab_windows: done.\n");
10546 setup_screens(void)
10548 int i
, j
, k
, num_screens
;
10549 struct workspace
*ws
;
10550 uint32_t gcv
[1], wa
[1];
10551 const xcb_query_extension_reply_t
*qep
;
10552 xcb_screen_t
*screen
;
10553 xcb_randr_query_version_cookie_t c
;
10554 xcb_randr_query_version_reply_t
*r
;
10556 num_screens
= get_screen_count();
10557 if ((screens
= calloc(num_screens
,
10558 sizeof(struct swm_screen
))) == NULL
)
10559 err(1, "setup_screens: calloc: failed to allocate memory for "
10562 /* Initial RandR setup. */
10563 randr_support
= false;
10564 qep
= xcb_get_extension_data(conn
, &xcb_randr_id
);
10565 if (qep
->present
) {
10566 c
= xcb_randr_query_version(conn
, 1, 1);
10567 r
= xcb_randr_query_version_reply(conn
, c
, NULL
);
10569 if (r
->major_version
>= 1) {
10570 randr_support
= true;
10571 randr_eventbase
= qep
->first_event
;
10577 wa
[0] = cursors
[XC_LEFT_PTR
].cid
;
10579 /* map physical screens */
10580 for (i
= 0; i
< num_screens
; i
++) {
10581 DNPRINTF(SWM_D_WS
, "setup_screens: init screen: %d\n", i
);
10582 screens
[i
].idx
= i
;
10583 screens
[i
].r_focus
= NULL
;
10585 TAILQ_INIT(&screens
[i
].rl
);
10586 TAILQ_INIT(&screens
[i
].orl
);
10587 if ((screen
= get_screen(i
)) == NULL
)
10588 errx(1, "ERROR: can't get screen %d.", i
);
10589 screens
[i
].root
= screen
->root
;
10591 /* set default colors */
10592 setscreencolor("red", i
, SWM_S_COLOR_FOCUS
);
10593 setscreencolor("rgb:88/88/88", i
, SWM_S_COLOR_UNFOCUS
);
10594 setscreencolor("rgb:00/80/80", i
, SWM_S_COLOR_BAR_BORDER
);
10595 setscreencolor("rgb:00/40/40", i
,
10596 SWM_S_COLOR_BAR_BORDER_UNFOCUS
);
10597 setscreencolor("black", i
, SWM_S_COLOR_BAR
);
10598 setscreencolor("rgb:a0/a0/a0", i
, SWM_S_COLOR_BAR_FONT
);
10599 setscreencolor("red", i
, SWM_S_COLOR_FOCUS_MAXIMIZED
);
10600 setscreencolor("rgb:88/88/88", i
,
10601 SWM_S_COLOR_UNFOCUS_MAXIMIZED
);
10603 /* create graphics context on screen */
10604 screens
[i
].bar_gc
= xcb_generate_id(conn
);
10606 xcb_create_gc(conn
, screens
[i
].bar_gc
, screens
[i
].root
,
10607 XCB_GC_GRAPHICS_EXPOSURES
, gcv
);
10609 /* set default cursor */
10610 xcb_change_window_attributes(conn
, screens
[i
].root
,
10611 XCB_CW_CURSOR
, wa
);
10613 /* init all workspaces */
10614 /* XXX these should be dynamically allocated too */
10615 for (j
= 0; j
< SWM_WS_MAX
; j
++) {
10616 ws
= &screens
[i
].ws
[j
];
10619 ws
->bar_enabled
= true;
10621 ws
->focus_prev
= NULL
;
10622 ws
->focus_pending
= NULL
;
10625 ws
->state
= SWM_WS_STATE_HIDDEN
;
10626 TAILQ_INIT(&ws
->stack
);
10627 TAILQ_INIT(&ws
->winlist
);
10628 TAILQ_INIT(&ws
->unmanagedlist
);
10630 for (k
= 0; layouts
[k
].l_stack
!= NULL
; k
++)
10631 if (layouts
[k
].l_config
!= NULL
)
10632 layouts
[k
].l_config(ws
,
10633 SWM_ARG_ID_STACKINIT
);
10634 ws
->cur_layout
= &layouts
[0];
10635 ws
->cur_layout
->l_string(ws
);
10641 xcb_randr_select_input(conn
, screens
[i
].root
,
10642 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE
);
10647 setup_globals(void)
10649 if ((bar_fonts
= strdup(SWM_BAR_FONTS
)) == NULL
)
10650 err(1, "setup_globals: strdup: failed to allocate memory.");
10652 if ((clock_format
= strdup("%a %b %d %R %Z %Y")) == NULL
)
10653 err(1, "setup_globals: strdup: failed to allocate memory.");
10655 if ((syms
= xcb_key_symbols_alloc(conn
)) == NULL
)
10656 errx(1, "unable to allocate key symbols");
10658 a_state
= get_atom_from_string("WM_STATE");
10659 a_prot
= get_atom_from_string("WM_PROTOCOLS");
10660 a_delete
= get_atom_from_string("WM_DELETE_WINDOW");
10661 a_net_supported
= get_atom_from_string("_NET_SUPPORTED");
10662 a_net_wm_check
= get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
10663 a_takefocus
= get_atom_from_string("WM_TAKE_FOCUS");
10664 a_utf8_string
= get_atom_from_string("UTF8_STRING");
10665 a_swm_ws
= get_atom_from_string("_SWM_WS");
10669 shutdown_cleanup(void)
10671 int i
, num_screens
;
10673 /* disable alarm because the following code may not be interrupted */
10675 if (signal(SIGALRM
, SIG_IGN
) == SIG_ERR
)
10676 err(1, "can't disable alarm");
10685 num_screens
= get_screen_count();
10686 for (i
= 0; i
< num_screens
; ++i
) {
10687 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
10688 screens
[i
].root
, XCB_CURRENT_TIME
);
10690 if (screens
[i
].bar_gc
!= XCB_NONE
)
10691 xcb_free_gc(conn
, screens
[i
].bar_gc
);
10692 if (!bar_font_legacy
) {
10693 XftColorFree(display
, DefaultVisual(display
, i
),
10694 DefaultColormap(display
, i
), &bar_font_color
);
10695 XftColorFree(display
, DefaultVisual(display
, i
),
10696 DefaultColormap(display
, i
), &search_font_color
);
10700 if (bar_font_legacy
)
10701 XFreeFontSet(display
, bar_fs
);
10703 XftFontClose(display
, bar_font
);
10706 xcb_key_symbols_free(syms
);
10708 xcb_disconnect(conn
);
10712 event_error(xcb_generic_error_t
*e
)
10716 DNPRINTF(SWM_D_EVENT
, "event_error: %s(%u) from %s(%u), sequence: %u, "
10717 "resource_id: %u, minor_code: %u\n",
10718 xcb_event_get_error_label(e
->error_code
), e
->error_code
,
10719 xcb_event_get_request_label(e
->major_code
), e
->major_code
,
10720 e
->sequence
, e
->resource_id
, e
->minor_code
);
10724 event_handle(xcb_generic_event_t
*evt
)
10726 uint8_t type
= XCB_EVENT_RESPONSE_TYPE(evt
);
10728 DNPRINTF(SWM_D_EVENT
, "XCB Event: %s(%d), seq %u\n",
10729 xcb_event_get_label(XCB_EVENT_RESPONSE_TYPE(evt
)),
10730 XCB_EVENT_RESPONSE_TYPE(evt
), evt
->sequence
);
10733 #define EVENT(type, callback) case type: callback((void *)evt); return
10734 EVENT(0, event_error
);
10735 EVENT(XCB_BUTTON_PRESS
, buttonpress
);
10736 /*EVENT(XCB_BUTTON_RELEASE, buttonpress);*/
10737 /*EVENT(XCB_CIRCULATE_NOTIFY, );*/
10738 /*EVENT(XCB_CIRCULATE_REQUEST, );*/
10739 EVENT(XCB_CLIENT_MESSAGE
, clientmessage
);
10740 /*EVENT(XCB_COLORMAP_NOTIFY, );*/
10741 EVENT(XCB_CONFIGURE_NOTIFY
, configurenotify
);
10742 EVENT(XCB_CONFIGURE_REQUEST
, configurerequest
);
10743 /*EVENT(XCB_CREATE_NOTIFY, );*/
10744 EVENT(XCB_DESTROY_NOTIFY
, destroynotify
);
10745 EVENT(XCB_ENTER_NOTIFY
, enternotify
);
10746 EVENT(XCB_EXPOSE
, expose
);
10748 EVENT(XCB_FOCUS_IN
, focusin
);
10749 EVENT(XCB_FOCUS_OUT
, focusout
);
10751 /*EVENT(XCB_GRAPHICS_EXPOSURE, );*/
10752 /*EVENT(XCB_GRAVITY_NOTIFY, );*/
10753 EVENT(XCB_KEY_PRESS
, keypress
);
10754 /*EVENT(XCB_KEY_RELEASE, keypress);*/
10755 /*EVENT(XCB_KEYMAP_NOTIFY, );*/
10757 EVENT(XCB_LEAVE_NOTIFY
, leavenotify
);
10759 EVENT(XCB_MAP_NOTIFY
, mapnotify
);
10760 EVENT(XCB_MAP_REQUEST
, maprequest
);
10761 EVENT(XCB_MAPPING_NOTIFY
, mappingnotify
);
10762 EVENT(XCB_MOTION_NOTIFY
, motionnotify
);
10763 /*EVENT(XCB_NO_EXPOSURE, );*/
10764 EVENT(XCB_PROPERTY_NOTIFY
, propertynotify
);
10765 /*EVENT(XCB_REPARENT_NOTIFY, );*/
10766 /*EVENT(XCB_RESIZE_REQUEST, );*/
10767 /*EVENT(XCB_SELECTION_CLEAR, );*/
10768 /*EVENT(XCB_SELECTION_NOTIFY, );*/
10769 /*EVENT(XCB_SELECTION_REQUEST, );*/
10770 EVENT(XCB_UNMAP_NOTIFY
, unmapnotify
);
10771 /*EVENT(XCB_VISIBILITY_NOTIFY, );*/
10774 if (type
- randr_eventbase
== XCB_RANDR_SCREEN_CHANGE_NOTIFY
)
10775 screenchange((void *)evt
);
10779 main(int argc
, char *argv
[])
10781 struct swm_region
*r
;
10782 char conf
[PATH_MAX
], *cfile
= NULL
;
10784 int xfd
, i
, num_screens
;
10785 struct sigaction sact
;
10786 xcb_generic_event_t
*evt
;
10791 bool stdin_ready
= false, startup
= true;
10793 /* suppress unused warning since var is needed */
10797 time_started
= time(NULL
);
10801 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION
, buildstr
);
10802 if (setlocale(LC_CTYPE
, "") == NULL
|| setlocale(LC_TIME
, "") == NULL
)
10803 warnx("no locale support");
10805 /* handle some signals */
10806 bzero(&sact
, sizeof(sact
));
10807 sigemptyset(&sact
.sa_mask
);
10809 sact
.sa_handler
= sighdlr
;
10810 sigaction(SIGINT
, &sact
, NULL
);
10811 sigaction(SIGQUIT
, &sact
, NULL
);
10812 sigaction(SIGTERM
, &sact
, NULL
);
10813 sigaction(SIGHUP
, &sact
, NULL
);
10815 sact
.sa_handler
= sighdlr
;
10816 sact
.sa_flags
= SA_NOCLDSTOP
;
10817 sigaction(SIGCHLD
, &sact
, NULL
);
10819 if ((display
= XOpenDisplay(0)) == NULL
)
10820 errx(1, "can not open display");
10822 conn
= XGetXCBConnection(display
);
10823 if (xcb_connection_has_error(conn
))
10824 errx(1, "can not get XCB connection");
10826 XSetEventQueueOwner(display
, XCBOwnsEventQueue
);
10828 xcb_prefetch_extension_data(conn
, &xcb_randr_id
);
10829 xfd
= xcb_get_file_descriptor(conn
);
10831 /* look for local and global conf file */
10832 pwd
= getpwuid(getuid());
10834 errx(1, "invalid user: %d", getuid());
10836 xcb_grab_server(conn
);
10837 xcb_aux_sync(conn
);
10839 /* flush all events */
10840 while ((evt
= xcb_poll_for_event(conn
))) {
10841 if (XCB_EVENT_RESPONSE_TYPE(evt
) == 0)
10847 errx(1, "another window manager is currently running");
10849 /* Load Xcursors and/or cursorfont glyph cursors. */
10852 xcb_aux_sync(conn
);
10862 for (i
= 0; ; i
++) {
10867 snprintf(conf
, sizeof conf
, "%s/.%s",
10868 pwd
->pw_dir
, SWM_CONF_FILE
);
10872 snprintf(conf
, sizeof conf
, "/etc/%s",
10877 snprintf(conf
, sizeof conf
, "%s/.%s",
10878 pwd
->pw_dir
, SWM_CONF_FILE_OLD
);
10881 /* global compat */
10882 snprintf(conf
, sizeof conf
, "/etc/%s",
10883 SWM_CONF_FILE_OLD
);
10889 if (strlen(conf
) && stat(conf
, &sb
) != -1)
10890 if (S_ISREG(sb
.st_mode
)) {
10897 /* load conf (if any) */
10899 conf_load(cfile
, SWM_CONF_DEFAULT
);
10903 if (getenv("SWM_STARTED") == NULL
)
10904 setenv("SWM_STARTED", "YES", 1);
10906 /* setup all bars */
10907 num_screens
= get_screen_count();
10908 for (i
= 0; i
< num_screens
; i
++)
10909 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
10912 /* Manage existing windows. */
10919 xcb_ungrab_server(conn
);
10922 /* Update state of each newly mapped workspace. */
10923 for (i
= 0; i
< num_screens
; i
++)
10924 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
10925 r
->ws
->state
= SWM_WS_STATE_MAPPED
;
10927 rd_max
= xfd
> STDIN_FILENO
? xfd
: STDIN_FILENO
;
10930 while ((evt
= xcb_poll_for_event(conn
))) {
10937 /* If just (re)started, set default focus if needed. */
10941 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
10942 r
= TAILQ_FIRST(&screens
[0].rl
);
10954 FD_SET(STDIN_FILENO
, &rd
);
10959 num_readable
= select(rd_max
+ 1, &rd
, NULL
, NULL
, &tv
);
10960 if (num_readable
== -1 && errno
!= EINTR
) {
10961 DNPRINTF(SWM_D_MISC
, "select failed");
10962 } else if (num_readable
> 0 && FD_ISSET(STDIN_FILENO
, &rd
)) {
10963 stdin_ready
= true;
10967 restart(NULL
, NULL
);
10976 stdin_ready
= false;
10977 bar_extra_update();
10984 shutdown_cleanup();