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;
336 struct swm_region *search_r;
337 int select_list_pipe[2];
338 int select_resp_pipe[2];
340 volatile sig_atomic_t search_resp;
341 int search_resp_action;
343 struct search_window {
344 TAILQ_ENTRY(search_window) entry;
348 xcb_window_t indicator;
350 TAILQ_HEAD(search_winlist, search_window);
351 struct search_winlist search_wl;
356 SWM_SEARCH_UNICONIFY,
357 SWM_SEARCH_NAME_WORKSPACE,
358 SWM_SEARCH_SEARCH_WORKSPACE,
359 SWM_SEARCH_SEARCH_WINDOW
362 #define SWM_STACK_TOP (0)
363 #define SWM_STACK_BOTTOM (1)
364 #define SWM_STACK_ABOVE (2)
365 #define SWM_STACK_BELOW (3)
368 double dialog_ratio = 0.6;
370 #define SWM_BAR_MAX (356)
371 #define SWM_BAR_JUSTIFY_LEFT (0)
372 #define SWM_BAR_JUSTIFY_CENTER (1)
373 #define SWM_BAR_JUSTIFY_RIGHT (2)
374 #define SWM_BAR_OFFSET (4)
375 #define SWM_BAR_FONTS "-*-terminus-medium-*-*-*-12-*-*-*-*-*-*-*," \
376 "-*-profont-*-*-*-*-12-*-*-*-*-*-*-*," \
377 "-*-times-medium-r-*-*-12-*-*-*-*-*-*-*," \
378 "-misc-fixed-medium-r-*-*-12-*-*-*-*-*-*-*," \
379 "-*-*-*-r-*-*-*-*-*-*-*-*-*-*"
381 #ifdef X_HAVE_UTF8_STRING
382 #define DRAWSTRING(x...) Xutf8DrawString(x)
384 #define DRAWSTRING(x...) XmbDrawString(x)
387 char *bar_argv[] = { NULL, NULL };
389 char bar_ext[SWM_BAR_MAX];
390 char bar_ext_buf[SWM_BAR_MAX];
391 char bar_vertext[SWM_BAR_MAX];
392 bool bar_version = false;
393 bool bar_enabled = true;
394 int bar_border_width = 1;
395 bool bar_at_bottom = false;
396 bool bar_extra = false;
398 int bar_justify = SWM_BAR_JUSTIFY_LEFT;
399 char *bar_format = NULL;
400 bool stack_enabled = true;
401 bool clock_enabled = true;
402 bool iconic_enabled = false;
403 bool urgent_enabled = false;
404 int composite_enabled = 0;
405 double opacity_focus = 1.0;
406 double opacity_unfocus = 0.6;
407 char *clock_format = NULL;
408 bool window_class_enabled = false;
409 bool window_instance_enabled = false;
410 bool window_name_enabled = false;
411 int focus_mode = SWM_FOCUS_DEFAULT;
412 int focus_close = SWM_STACK_BELOW;
413 bool focus_close_wrap = true;
414 int focus_default = SWM_STACK_TOP;
415 int spawn_position = SWM_STACK_TOP;
416 bool disable_border = false;
417 int border_width = 1;
418 int region_padding = 0;
420 bool java_workaround = true;
421 bool verbose_layout = false;
427 XFontSetExtents *bar_fs_extents;
429 bool bar_font_legacy = true;
431 XftColor bar_font_color;
432 XftColor search_font_color;
434 char *startup_exception;
435 unsigned int nr_exceptions = 0;
437 /* layout manager data */
438 struct swm_geometry {
451 struct swm_geometry g;
454 /* virtual "screens" */
456 TAILQ_ENTRY(swm_region) entry;
458 struct swm_geometry g;
459 struct workspace *ws; /* current workspace on this region */
460 struct workspace *ws_prior; /* prior workspace on this region */
461 struct swm_screen *s; /* screen idx */
464 TAILQ_HEAD(swm_region_list, swm_region);
467 TAILQ_ENTRY(ws_win) entry;
468 TAILQ_ENTRY(ws_win) stack_entry;
470 xcb_window_t transient;
471 struct ws_win *focus_child; /* focus on child transient */
472 struct swm_geometry g; /* current geometry */
473 struct swm_geometry g_float; /* region coordinates */
474 bool g_floatvalid; /* g_float geometry validity */
478 int font_size_boundary[SWM_MAX_FONT_STEPS];
484 unsigned long quirks;
485 struct workspace *ws; /* always valid */
486 struct swm_screen *s; /* always valid, never changes */
488 xcb_icccm_get_wm_class_reply_t ch;
489 xcb_icccm_wm_hints_t hints;
491 TAILQ_HEAD(ws_win_list, ws_win);
492 TAILQ_HEAD(ws_win_stack, ws_win);
496 TAILQ_ENTRY(pid_e) entry;
500 TAILQ_HEAD(pid_list, pid_e);
501 struct pid_list pidlist = TAILQ_HEAD_INITIALIZER(pidlist);
503 /* layout handlers */
505 void vertical_config(struct workspace *, int);
506 void vertical_stack(struct workspace *, struct swm_geometry *);
507 void horizontal_config(struct workspace *, int);
508 void horizontal_stack(struct workspace *, struct swm_geometry *);
509 void max_stack(struct workspace *, struct swm_geometry *);
510 void plain_stacker(struct workspace *);
511 void fancy_stacker(struct workspace *);
514 void (*l_stack)(struct workspace *, struct swm_geometry *);
515 void (*l_config)(struct workspace *, int);
517 #define SWM_L_FOCUSPREV (1<<0)
518 #define SWM_L_MAPONFOCUS (1<<1)
519 void (*l_string)(struct workspace *);
521 /* stack, configure */
522 { vertical_stack, vertical_config, 0, plain_stacker },
523 // { horizontal_stack, horizontal_config, 0, plain_stacker },
525 SWM_L_MAPONFOCUS | SWM_L_FOCUSPREV, plain_stacker },
526 { NULL, NULL, 0, NULL },
529 /* position of max_stack mode in the layouts array, index into layouts! */
530 #define SWM_V_STACK (0)
531 #define SWM_H_STACK (1)
532 #define SWM_MAX_STACK (1)
534 #define SWM_H_SLICE (32)
535 #define SWM_V_SLICE (32)
537 /* define work spaces */
539 int idx; /* workspace index */
540 char *name; /* workspace name */
541 bool always_raise; /* raise windows on focus */
542 bool bar_enabled; /* bar visibility */
543 struct layout *cur_layout; /* current layout handlers */
544 struct ws_win *focus; /* may be NULL */
545 struct ws_win *focus_prev; /* may be NULL */
546 struct ws_win *focus_pending; /* may be NULL */
547 struct swm_region *r; /* may be NULL */
548 struct swm_region *old_r; /* may be NULL */
549 struct ws_win_list winlist; /* list of windows in ws */
550 struct ws_win_list unmanagedlist; /* list of dead windows in ws */
551 struct ws_win_stack stack; /* stacking order */
552 int state; /* mapping state */
553 char stacker[10]; /* display stacker and layout */
557 int horizontal_msize;
559 int horizontal_stacks;
560 bool horizontal_flip;
570 SWM_WS_STATE_MAPPING,
576 SWM_S_COLOR_BAR_BORDER,
577 SWM_S_COLOR_BAR_BORDER_UNFOCUS,
578 SWM_S_COLOR_BAR_FONT,
580 SWM_S_COLOR_FOCUS_MAXIMIZED,
582 SWM_S_COLOR_UNFOCUS_MAXIMIZED,
586 /* physical screen mapping */
587 #define SWM_WS_MAX (22) /* hard limit */
588 int workspace_limit = 10; /* soft limit */
591 int idx; /* screen index */
592 struct swm_region_list rl; /* list of regions on this screen */
593 struct swm_region_list orl; /* list of old regions */
595 struct workspace ws[SWM_WS_MAX];
596 struct swm_region *r_focus;
603 } c[SWM_S_COLOR_MAX];
605 xcb_gcontext_t bar_gc;
608 struct swm_screen *screens;
610 /* args to functions */
613 #define SWM_ARG_ID_FOCUSNEXT (0)
614 #define SWM_ARG_ID_FOCUSPREV (1)
615 #define SWM_ARG_ID_FOCUSMAIN (2)
616 #define SWM_ARG_ID_FOCUSURGENT (3)
617 #define SWM_ARG_ID_SWAPNEXT (10)
618 #define SWM_ARG_ID_SWAPPREV (11)
619 #define SWM_ARG_ID_SWAPMAIN (12)
620 #define SWM_ARG_ID_MOVELAST (13)
621 #define SWM_ARG_ID_MASTERSHRINK (20)
622 #define SWM_ARG_ID_MASTERGROW (21)
623 #define SWM_ARG_ID_MASTERADD (22)
624 #define SWM_ARG_ID_MASTERDEL (23)
625 #define SWM_ARG_ID_FLIPLAYOUT (24)
626 #define SWM_ARG_ID_STACKRESET (30)
627 #define SWM_ARG_ID_STACKINIT (31)
628 #define SWM_ARG_ID_CYCLEWS_UP (40)
629 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
630 #define SWM_ARG_ID_CYCLERG_UP (42)
631 #define SWM_ARG_ID_CYCLERG_DOWN (43)
632 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
633 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
634 #define SWM_ARG_ID_CYCLEWS_MOVE_UP (46)
635 #define SWM_ARG_ID_CYCLEWS_MOVE_DOWN (47)
636 #define SWM_ARG_ID_STACKINC (50)
637 #define SWM_ARG_ID_STACKDEC (51)
638 #define SWM_ARG_ID_SS_ALL (60)
639 #define SWM_ARG_ID_SS_WINDOW (61)
640 #define SWM_ARG_ID_DONTCENTER (70)
641 #define SWM_ARG_ID_CENTER (71)
642 #define SWM_ARG_ID_KILLWINDOW (80)
643 #define SWM_ARG_ID_DELETEWINDOW (81)
644 #define SWM_ARG_ID_WIDTHGROW (90)
645 #define SWM_ARG_ID_WIDTHSHRINK (91)
646 #define SWM_ARG_ID_HEIGHTGROW (92)
647 #define SWM_ARG_ID_HEIGHTSHRINK (93)
648 #define SWM_ARG_ID_MOVEUP (100)
649 #define SWM_ARG_ID_MOVEDOWN (101)
650 #define SWM_ARG_ID_MOVELEFT (102)
651 #define SWM_ARG_ID_MOVERIGHT (103)
652 #define SWM_ARG_ID_RAISE (105)
653 #define SWM_ARG_ID_LOWER (106)
654 #define SWM_ARG_ID_BAR_TOGGLE (110)
655 #define SWM_ARG_ID_BAR_TOGGLE_WS (111)
661 TAILQ_ENTRY(quirk) entry;
662 char *class; /* WM_CLASS:class */
663 char *instance; /* WM_CLASS:instance */
664 char *name; /* WM_NAME */
666 regex_t regex_instance;
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,
716 _NET_WM_WINDOW_OPACITY,
718 _NET_WM_WINDOW_TYPE_DIALOG,
719 _NET_WM_WINDOW_TYPE_DOCK,
720 _NET_WM_WINDOW_TYPE_NORMAL,
721 _NET_WM_WINDOW_TYPE_SPLASH,
722 _NET_WM_WINDOW_TYPE_TOOLBAR,
723 _NET_WM_WINDOW_TYPE_UTILITY,
724 _SWM_WM_STATE_MANUAL,
731 } ewmh[SWM_EWMH_HINT_MAX] = {
732 /* must be in same order as in the enum */
733 {"_NET_ACTIVE_WINDOW", XCB_ATOM_NONE},
734 {"_NET_CLIENT_LIST", XCB_ATOM_NONE},
735 {"_NET_CLOSE_WINDOW", XCB_ATOM_NONE},
736 {"_NET_CURRENT_DESKTOP", XCB_ATOM_NONE},
737 {"_NET_DESKTOP_GEOMETRY", XCB_ATOM_NONE},
738 {"_NET_DESKTOP_NAMES", XCB_ATOM_NONE},
739 {"_NET_DESKTOP_VIEWPORT", XCB_ATOM_NONE},
740 {"_NET_MOVERESIZE_WINDOW", XCB_ATOM_NONE},
741 {"_NET_NUMBER_OF_DESKTOPS", XCB_ATOM_NONE},
742 {"_NET_RESTACK_WINDOW", XCB_ATOM_NONE},
743 {"_NET_WM_ACTION_ABOVE", XCB_ATOM_NONE},
744 {"_NET_WM_ACTION_CLOSE", XCB_ATOM_NONE},
745 {"_NET_WM_ACTION_FULLSCREEN", XCB_ATOM_NONE},
746 {"_NET_WM_ACTION_MOVE", XCB_ATOM_NONE},
747 {"_NET_WM_ACTION_RESIZE", XCB_ATOM_NONE},
748 {"_NET_WM_ALLOWED_ACTIONS", XCB_ATOM_NONE},
749 {"_NET_WM_DESKTOP", XCB_ATOM_NONE},
750 {"_NET_WM_FULL_PLACEMENT", XCB_ATOM_NONE},
751 {"_NET_WM_NAME", XCB_ATOM_NONE},
752 {"_NET_WM_STATE", XCB_ATOM_NONE},
753 {"_NET_WM_STATE_ABOVE", XCB_ATOM_NONE},
754 {"_NET_WM_STATE_FULLSCREEN", XCB_ATOM_NONE},
755 {"_NET_WM_STATE_HIDDEN", XCB_ATOM_NONE},
756 {"_NET_WM_STATE_MAXIMIZED_VERT", XCB_ATOM_NONE},
757 {"_NET_WM_STATE_MAXIMIZED_HORZ", XCB_ATOM_NONE},
758 {"_NET_WM_STATE_SKIP_PAGER", XCB_ATOM_NONE},
759 {"_NET_WM_STATE_SKIP_TASKBAR", XCB_ATOM_NONE},
760 {"_NET_WM_WINDOW_OPACITY", XCB_ATOM_NONE},
761 {"_NET_WM_WINDOW_TYPE", XCB_ATOM_NONE},
762 {"_NET_WM_WINDOW_TYPE_DIALOG", XCB_ATOM_NONE},
763 {"_NET_WM_WINDOW_TYPE_DOCK", XCB_ATOM_NONE},
764 {"_NET_WM_WINDOW_TYPE_NORMAL", XCB_ATOM_NONE},
765 {"_NET_WM_WINDOW_TYPE_SPLASH", XCB_ATOM_NONE},
766 {"_NET_WM_WINDOW_TYPE_TOOLBAR", XCB_ATOM_NONE},
767 {"_NET_WM_WINDOW_TYPE_UTILITY", XCB_ATOM_NONE},
768 {"_SWM_WM_STATE_MANUAL", XCB_ATOM_NONE},
771 /* EWMH source type */
773 EWMH_SOURCE_TYPE_NONE = 0,
774 EWMH_SOURCE_TYPE_NORMAL = 1,
775 EWMH_SOURCE_TYPE_OTHER = 2,
782 XC_BOTTOM_LEFT_CORNER,
783 XC_BOTTOM_RIGHT_CORNER,
791 char *name; /* Name used by Xcursor .*/
792 uint8_t cf_char; /* cursorfont index. */
794 } cursors[XC_MAX] = {
795 {"fleur", XC_fleur, XCB_CURSOR_NONE},
796 {"left_ptr", XC_left_ptr, XCB_CURSOR_NONE},
797 {"bottom_left_corner", XC_bottom_left_corner, XCB_CURSOR_NONE},
798 {"bottom_right_corner", XC_bottom_right_corner, XCB_CURSOR_NONE},
799 {"sizing", XC_sizing, XCB_CURSOR_NONE},
800 {"top_left_corner", XC_top_left_corner, XCB_CURSOR_NONE},
801 {"top_right_corner", XC_top_right_corner, XCB_CURSOR_NONE},
804 #define SWM_SPAWN_OPTIONAL 0x1
808 TAILQ_ENTRY(spawn_prog) entry;
814 TAILQ_HEAD(spawn_list, spawn_prog);
815 struct spawn_list spawns = TAILQ_HEAD_INITIALIZER(spawns);
817 /* user/key callable function IDs */
933 KF_DUMPWINS, /* MUST BE LAST */
941 enum keyfuncid funcid;
944 RB_HEAD(key_tree, key);
946 /* function prototypes */
947 void adjust_font(struct ws_win *);
948 char *argsep(char **);
949 void bar_cleanup(struct swm_region *);
950 void bar_extra_setup(void);
951 void bar_extra_stop(void);
952 int bar_extra_update(void);
953 void bar_fmt(const char *, char *, struct swm_region *, size_t);
954 void bar_fmt_expand(char *, size_t);
956 void bar_print(struct swm_region *, const char *);
957 void bar_print_legacy(struct swm_region *, const char *);
958 void bar_replace(char *, char *, struct swm_region *, size_t);
959 void bar_replace_pad(char *, int *, size_t);
960 char *bar_replace_seq(char *, char *, struct swm_region *, size_t *, size_t);
961 void bar_setup(struct swm_region *);
962 void bar_toggle(struct swm_region *, union arg *);
963 void bar_urgent(char *, size_t);
964 void bar_window_class(char *, size_t, struct swm_region *);
965 void bar_window_class_instance(char *, size_t, struct swm_region *);
966 void bar_window_float(char *, size_t, struct swm_region *);
967 void bar_window_instance(char *, size_t, struct swm_region *);
968 void bar_window_name(char *, size_t, struct swm_region *);
969 void bar_window_state(char *, size_t, struct swm_region *);
970 void bar_workspace_name(char *, size_t, struct swm_region *);
971 void buttonpress(xcb_button_press_event_t *);
972 void check_conn(void);
973 void clear_keys(void);
974 int clear_maximized(struct workspace *);
975 void clientmessage(xcb_client_message_event_t *);
976 void client_msg(struct ws_win *, xcb_atom_t, xcb_timestamp_t);
977 int conf_load(const char *, int);
978 void configurenotify(xcb_configure_notify_event_t *);
979 void configurerequest(xcb_configure_request_event_t *);
980 void config_win(struct ws_win *, xcb_configure_request_event_t *);
981 void constrain_window(struct ws_win *, struct swm_geometry *, int *);
982 int count_win(struct workspace *, bool);
983 void cursors_cleanup(void);
984 void cursors_load(void);
985 void custom_region(const char *);
986 void cyclerg(struct swm_region *, union arg *);
987 void cyclews(struct swm_region *, union arg *);
988 void cycle_layout(struct swm_region *, union arg *);
989 void destroynotify(xcb_destroy_notify_event_t *);
990 void dumpwins(struct swm_region *, union arg *);
992 void enternotify(xcb_enter_notify_event_t *);
993 void event_drain(uint8_t);
994 void event_error(xcb_generic_error_t *);
995 void event_handle(xcb_generic_event_t *);
996 void ewmh_apply_flags(struct ws_win *, uint32_t);
997 void ewmh_autoquirk(struct ws_win *);
998 void ewmh_get_desktop_names(void);
999 void ewmh_get_wm_state(struct ws_win *);
1000 void ewmh_update_actions(struct ws_win *);
1001 void ewmh_update_client_list(void);
1002 void ewmh_update_current_desktop(void);
1003 void ewmh_update_desktop_names(void);
1004 void ewmh_update_desktops(void);
1005 void ewmh_change_wm_state(struct ws_win *, xcb_atom_t, long);
1006 void ewmh_update_wm_state(struct ws_win *);
1007 char *expand_tilde(const char *);
1008 void expose(xcb_expose_event_t *);
1009 void fake_keypress(struct ws_win *, xcb_keysym_t, uint16_t);
1010 struct pid_e *find_pid(pid_t);
1011 struct ws_win *find_unmanaged_window(xcb_window_t);
1012 struct ws_win *find_window(xcb_window_t);
1013 void floating_toggle(struct swm_region *, union arg *);
1014 void focus(struct swm_region *, union arg *);
1016 void focusin(xcb_focus_in_event_t *);
1017 void focusout(xcb_focus_out_event_t *);
1019 void focus_flush(void);
1020 void focus_region(struct swm_region *);
1021 void focusrg(struct swm_region *, union arg *);
1022 void focus_win(struct ws_win *);
1023 void fontset_init(void);
1024 void free_window(struct ws_win *);
1025 xcb_atom_t get_atom_from_string(const char *);
1027 char *get_atom_name(xcb_atom_t);
1029 struct ws_win *get_focus_magic(struct ws_win *);
1030 struct ws_win *get_focus_prev(struct ws_win *);
1032 char *get_notify_detail_label(uint8_t);
1033 char *get_notify_mode_label(uint8_t);
1035 struct ws_win *get_pointer_win(xcb_window_t);
1036 struct ws_win *get_region_focus(struct swm_region *);
1037 int get_region_index(struct swm_region *);
1038 xcb_screen_t *get_screen(int);
1039 int get_screen_count(void);
1041 char *get_source_type_label(uint32_t);
1042 char *get_stack_mode_name(uint8_t);
1044 int32_t get_swm_ws(xcb_window_t);
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 struct ws_win *manage_window(xcb_window_t, int, bool);
1068 void map_window(struct ws_win *);
1069 void mapnotify(xcb_map_notify_event_t *);
1070 void mappingnotify(xcb_mapping_notify_event_t *);
1071 void maprequest(xcb_map_request_event_t *);
1072 void maximize_toggle(struct swm_region *, union arg *);
1073 void motionnotify(xcb_motion_notify_event_t *);
1074 void move(struct ws_win *, union arg *);
1075 void move_step(struct swm_region *, union arg *);
1076 uint32_t name_to_pixel(int, const char *);
1077 void name_workspace(struct swm_region *, union arg *);
1078 void new_region(struct swm_screen *, int, int, int, int);
1079 int parsekeys(const char *, unsigned int, unsigned int *, KeySym *);
1080 int parsequirks(const char *, unsigned long *);
1081 int parse_rgb(const char *, uint16_t *, uint16_t *, uint16_t *);
1082 void pressbutton(struct swm_region *, union arg *);
1083 void priorws(struct swm_region *, union arg *);
1085 void print_win_geom(xcb_window_t);
1087 void propertynotify(xcb_property_notify_event_t *);
1088 void quirk_free(struct quirk *);
1089 void quirk_insert(const char *, const char *, const char *,unsigned long);
1090 void quirk_remove(struct quirk *);
1091 void quirk_replace(struct quirk *, const char *, const char *, const char *,
1093 void quit(struct swm_region *, union arg *);
1094 void raise_toggle(struct swm_region *, union arg *);
1095 void raise_window(struct ws_win *);
1096 void region_containment(struct ws_win *, struct swm_region *, int);
1097 struct swm_region *region_under(struct swm_screen *, int, int);
1098 void regionize(struct ws_win *, int, int);
1099 void resize(struct ws_win *, union arg *);
1100 void resize_step(struct swm_region *, union arg *);
1101 void restart(struct swm_region *, union arg *);
1102 struct swm_region *root_to_region(xcb_window_t, int);
1103 void screenchange(xcb_randr_screen_change_notify_event_t *);
1104 void scan_randr(int);
1105 void search_do_resp(void);
1106 void search_resp_name_workspace(const char *, size_t);
1107 void search_resp_search_window(const char *);
1108 void search_resp_search_workspace(const char *);
1109 void search_resp_uniconify(const char *, size_t);
1110 void search_win(struct swm_region *, union arg *);
1111 void search_win_cleanup(void);
1112 void search_workspace(struct swm_region *, union arg *);
1113 void send_to_rg(struct swm_region *, union arg *);
1114 void send_to_ws(struct swm_region *, union arg *);
1115 void set_region(struct swm_region *);
1116 int setautorun(const char *, const char *, int);
1117 int setconfbinding(const char *, const char *, int);
1118 int setconfcolor(const char *, const char *, int);
1119 int setconfmodkey(const char *, const char *, int);
1120 int setconfquirk(const char *, const char *, int);
1121 int setconfregion(const char *, const char *, int);
1122 int setconfspawn(const char *, const char *, int);
1123 int setconfvalue(const char *, const char *, int);
1124 void setkeybinding(unsigned int, KeySym, enum keyfuncid, const char *);
1125 int setkeymapping(const char *, const char *, int);
1126 int setlayout(const char *, const char *, int);
1127 void setquirk(const char *, const char *, const char *,unsigned long);
1128 void setscreencolor(const char *, int, int);
1129 void setspawn(const char *, const char *, int);
1130 void setup_ewmh(void);
1131 void setup_globals(void);
1132 void setup_keys(void);
1133 void setup_quirks(void);
1134 void setup_screens(void);
1135 void setup_spawn(void);
1136 void set_child_transient(struct ws_win *, xcb_window_t *);
1137 void set_opacity(struct ws_win *, uint32_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 bar_urgent(char *s, size_t sz)
2177 int i, j, num_screens;
2178 bool urgent[SWM_WS_MAX];
2180 xcb_get_property_cookie_t c;
2181 xcb_icccm_wm_hints_t hints;
2183 for (i = 0; i < workspace_limit; i++)
2186 num_screens = get_screen_count();
2187 for (i = 0; i < num_screens; i++)
2188 for (j = 0; j < workspace_limit; j++)
2189 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry) {
2190 c = xcb_icccm_get_wm_hints(conn, win->id);
2191 if (xcb_icccm_get_wm_hints_reply(conn, c,
2194 if (hints.flags & XCB_ICCCM_WM_HINT_X_URGENCY)
2198 for (i = 0; i < workspace_limit; i++) {
2200 snprintf(b, sizeof b, "%d ", i + 1);
2202 snprintf(b, sizeof b, "- ");
2208 bar_workspace_name(char *s, size_t sz, struct swm_region *r)
2210 if (r == NULL || r->ws == NULL)
2212 if (r->ws->name != NULL)
2213 strlcat(s, r->ws->name, sz);
2216 /* build the default bar format according to the defined enabled options */
2218 bar_fmt(const char *fmtexp, char *fmtnew, struct swm_region *r, size_t sz)
2222 /* if format provided, just copy the buffers */
2223 if (bar_format != NULL) {
2224 strlcpy(fmtnew, fmtexp, sz);
2228 /* reset the output buffer */
2231 strlcat(fmtnew, "+N:+I ", sz);
2233 strlcat(fmtnew, "+S", sz);
2234 strlcat(fmtnew, " ", sz);
2236 /* only show the workspace name if there's actually one */
2237 if (r != NULL && r->ws != NULL && r->ws->name != NULL)
2238 strlcat(fmtnew, "<+D>", sz);
2240 /* If enabled, only show the iconic count if there are iconic wins. */
2241 if (iconic_enabled && r != NULL && r->ws != NULL)
2242 TAILQ_FOREACH(w, &r->ws->winlist, entry)
2244 strlcat(fmtnew, "{+M}", sz);
2248 strlcat(fmtnew, "+3<", sz);
2250 if (clock_enabled) {
2251 strlcat(fmtnew, fmtexp, sz);
2252 strlcat(fmtnew, "+4<", sz);
2255 /* bar_urgent already adds the space before the last asterisk */
2257 strlcat(fmtnew, "* +U*+4<", sz);
2259 if (window_class_enabled) {
2260 strlcat(fmtnew, "+C", sz);
2261 if (!window_instance_enabled)
2262 strlcat(fmtnew, "+4<", sz);
2265 /* checks needed by the colon and floating strlcat(3) calls below */
2266 if (r != NULL && r->ws != NULL && r->ws->focus != NULL) {
2267 if (window_instance_enabled) {
2268 if (window_class_enabled)
2269 strlcat(fmtnew, ":", sz);
2270 strlcat(fmtnew, "+T+4<", sz);
2272 if (window_name_enabled) {
2273 if (ABOVE(r->ws->focus) || MAXIMIZED(r->ws->focus))
2274 strlcat(fmtnew, "+F ", sz);
2275 strlcat(fmtnew, "+64W ", sz);
2279 /* finally add the action script output and the version */
2280 strlcat(fmtnew, "+4<+A+4<+V", sz);
2284 bar_replace_pad(char *tmp, int *limit, size_t sz)
2286 /* special case; no limit given, pad one space, instead */
2287 if (*limit == (int)sz - 1)
2289 snprintf(tmp, sz, "%*s", *limit, " ");
2292 /* replaces the bar format character sequences (like in tmux(1)) */
2294 bar_replace_seq(char *fmt, char *fmtrep, struct swm_region *r, size_t *offrep,
2299 char tmp[SWM_BAR_MAX];
2300 int limit, size, count;
2303 /* reset strlcat(3) buffer */
2306 /* get number, if any */
2309 if (sscanf(fmt, "%d%n", &limit, &size) != 1)
2310 limit = sizeof tmp - 1;
2311 if (limit <= 0 || limit >= (int)sizeof tmp)
2312 limit = sizeof tmp - 1;
2314 /* there is nothing to replace (ie EOL) */
2321 bar_replace_pad(tmp, &limit, sizeof tmp);
2324 snprintf(tmp, sizeof tmp, "%s", bar_ext);
2327 bar_window_class(tmp, sizeof tmp, r);
2330 bar_workspace_name(tmp, sizeof tmp, r);
2333 bar_window_state(tmp, sizeof tmp, r);
2336 snprintf(tmp, sizeof tmp, "%d", r->ws->idx + 1);
2340 TAILQ_FOREACH(w, &r->ws->winlist, entry)
2344 snprintf(tmp, sizeof tmp, "%d", count);
2347 snprintf(tmp, sizeof tmp, "%d", r->s->idx + 1);
2350 bar_window_class_instance(tmp, sizeof tmp, r);
2353 snprintf(tmp, sizeof tmp, "%s", r->ws->stacker);
2356 bar_window_instance(tmp, sizeof tmp, r);
2359 bar_urgent(tmp, sizeof tmp);
2362 snprintf(tmp, sizeof tmp, "%s", bar_vertext);
2365 bar_window_name(tmp, sizeof tmp, r);
2368 /* unknown character sequence; copy as-is */
2369 snprintf(tmp, sizeof tmp, "+%c", *fmt);
2375 if ((int)len < limit)
2377 while (limit-- > 0) {
2378 if (*offrep >= sz - 1)
2380 fmtrep[(*offrep)++] = *ptr++;
2388 bar_replace(char *fmt, char *fmtrep, struct swm_region *r, size_t sz)
2393 while (*fmt != '\0') {
2395 /* skip ordinary characters */
2398 fmtrep[off++] = *fmt++;
2402 /* character sequence found; replace it */
2403 fmt = bar_replace_seq(fmt, fmtrep, r, &off, sz);
2412 bar_fmt_expand(char *fmtexp, size_t sz)
2419 /* start by grabbing the current time and date */
2421 localtime_r(&tmt, &tm);
2423 /* figure out what to expand */
2424 if (bar_format != NULL)
2426 else if (bar_format == NULL && clock_enabled)
2428 /* if nothing to expand bail out */
2434 /* copy as-is, just in case the format shouldn't be expanded below */
2435 strlcpy(fmtexp, fmt, sz);
2436 /* finally pass the string through strftime(3) */
2437 #ifndef SWM_DENY_CLOCK_FORMAT
2438 if ((len = strftime(fmtexp, sz, fmt, &tm)) == 0)
2439 warnx("format too long");
2444 /* Redraws the bar; need to follow with xcb_flush() or focus_flush(). */
2448 char fmtexp[SWM_BAR_MAX], fmtnew[SWM_BAR_MAX];
2449 char fmtrep[SWM_BAR_MAX];
2451 struct swm_region *r;
2453 /* expand the format by first passing it through strftime(3) */
2454 bar_fmt_expand(fmtexp, sizeof fmtexp);
2456 num_screens = get_screen_count();
2457 for (i = 0; i < num_screens; i++) {
2458 TAILQ_FOREACH(r, &screens[i].rl, entry) {
2462 if (bar_enabled && r->ws->bar_enabled)
2463 xcb_map_window(conn, r->bar->id);
2465 xcb_unmap_window(conn, r->bar->id);
2469 if (startup_exception)
2470 snprintf(fmtrep, sizeof fmtrep, "total "
2471 "exceptions: %d, first exception: %s",
2475 bar_fmt(fmtexp, fmtnew, r, sizeof fmtnew);
2476 bar_replace(fmtnew, fmtrep, r, sizeof fmtrep);
2478 if (bar_font_legacy)
2479 bar_print_legacy(r, fmtrep);
2481 bar_print(r, fmtrep);
2487 * Reads external script output; call when stdin is readable.
2488 * Returns 1 if bar_ext was updated; otherwise 0.
2491 bar_extra_update(void)
2494 char b[SWM_BAR_MAX];
2495 bool changed = false;
2500 while (fgets(b, sizeof(b), stdin) != NULL) {
2503 if (b[len - 1] == '\n') {
2504 /* Remove newline. */
2507 /* "Clear" bar_ext. */
2510 /* Flush buffered output. */
2511 strlcpy(bar_ext, bar_ext_buf, sizeof(bar_ext));
2512 bar_ext_buf[0] = '\0';
2514 /* Append new output to bar. */
2515 strlcat(bar_ext, b, sizeof(bar_ext));
2519 /* Buffer output. */
2520 strlcat(bar_ext_buf, b, sizeof(bar_ext_buf));
2525 if (errno != EAGAIN) {
2526 warn("bar_action failed");
2535 bar_toggle(struct swm_region *r, union arg *args)
2537 struct swm_region *tmpr;
2540 /* suppress unused warnings since vars are needed */
2544 DNPRINTF(SWM_D_BAR, "bar_toggle\n");
2547 case SWM_ARG_ID_BAR_TOGGLE_WS:
2548 /* Only change if master switch is enabled. */
2550 r->ws->bar_enabled = !r->ws->bar_enabled;
2552 bar_enabled = r->ws->bar_enabled = true;
2554 case SWM_ARG_ID_BAR_TOGGLE:
2555 bar_enabled = !bar_enabled;
2559 /* update bars as necessary */
2560 num_screens = get_screen_count();
2561 for (i = 0; i < num_screens; i++)
2562 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
2564 if (bar_enabled && tmpr->ws->bar_enabled)
2565 xcb_map_window(conn, tmpr->bar->id);
2567 xcb_unmap_window(conn, tmpr->bar->id);
2572 /* must be after stack */
2579 bar_extra_setup(void)
2581 /* do this here because the conf file is in memory */
2582 if (!bar_extra && bar_argv[0]) {
2583 /* launch external status app */
2585 if (pipe(bar_pipe) == -1)
2586 err(1, "pipe error");
2587 socket_setnonblock(bar_pipe[0]);
2588 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
2590 /* Set stdin to read from the pipe. */
2591 if (dup2(bar_pipe[0], STDIN_FILENO) == -1)
2594 /* Set stdout to write to the pipe. */
2595 if (dup2(bar_pipe[1], STDOUT_FILENO) == -1)
2598 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
2599 err(1, "could not disable SIGPIPE");
2600 switch (bar_pid = fork()) {
2602 err(1, "cannot fork");
2606 execvp(bar_argv[0], bar_argv);
2607 err(1, "%s external app failed", bar_argv[0]);
2609 default: /* parent */
2614 atexit(kill_bar_extra_atexit);
2619 kill_bar_extra_atexit(void)
2622 kill(bar_pid, SIGTERM);
2630 while ((s = index(s, '-'))) {
2635 return (count == 14);
2641 char *default_string;
2642 char **missing_charsets;
2643 int num_missing_charsets = 0;
2647 XFreeFontSet(display, bar_fs);
2651 DNPRINTF(SWM_D_INIT, "fontset_init: loading bar_fonts: %s\n", bar_fonts);
2653 bar_fs = XCreateFontSet(display, bar_fonts, &missing_charsets,
2654 &num_missing_charsets, &default_string);
2656 if (num_missing_charsets > 0) {
2657 warnx("Unable to load charset(s):");
2659 for (i = 0; i < num_missing_charsets; ++i)
2660 warnx("%s", missing_charsets[i]);
2662 XFreeStringList(missing_charsets);
2664 if (strcmp(default_string, ""))
2665 warnx("Glyphs from those sets will be replaced "
2666 "by '%s'.", default_string);
2668 warnx("Glyphs from those sets won't be drawn.");
2672 errx(1, "Error creating font set structure.");
2674 bar_fs_extents = XExtentsOfFontSet(bar_fs);
2676 bar_height = bar_fs_extents->max_logical_extent.height +
2677 2 * bar_border_width;
2684 xft_init(struct swm_region *r)
2686 char *font, *d, *search;
2689 if (bar_font == NULL) {
2690 if ((d = strdup(bar_fonts)) == NULL)
2691 errx(1, "insufficient memory.");
2693 while ((font = strsep(&search, ",")) != NULL) {
2697 DNPRINTF(SWM_D_INIT, "xft_init: try font %s\n", font);
2700 bar_font = XftFontOpenXlfd(display, r->s->idx,
2703 bar_font = XftFontOpenName(display, r->s->idx,
2707 if (bar_font == NULL) {
2708 warnx("unable to load font %s", font);
2711 DNPRINTF(SWM_D_INIT, "successfully opened "
2719 if (bar_font == NULL)
2720 errx(1, "unable to open a font");
2722 PIXEL_TO_XRENDERCOLOR(r->s->c[SWM_S_COLOR_BAR_FONT].pixel, color);
2724 if (!XftColorAllocValue(display, DefaultVisual(display, r->s->idx),
2725 DefaultColormap(display, r->s->idx), &color, &bar_font_color))
2726 warn("Xft error: unable to allocate color.");
2728 PIXEL_TO_XRENDERCOLOR(r->s->c[SWM_S_COLOR_BAR].pixel, color);
2730 if (!XftColorAllocValue(display, DefaultVisual(display, r->s->idx),
2731 DefaultColormap(display, r->s->idx), &color, &search_font_color))
2732 warn("Xft error: unable to allocate color.");
2734 bar_height = bar_font->height + 2 * bar_border_width;
2741 bar_setup(struct swm_region *r)
2743 xcb_screen_t *screen;
2746 DNPRINTF(SWM_D_BAR, "bar_setup: screen %d.\n",
2749 if ((screen = get_screen(r->s->idx)) == NULL)
2750 errx(1, "ERROR: can't get screen %d.", r->s->idx);
2755 if ((r->bar = calloc(1, sizeof(struct swm_bar))) == NULL)
2756 err(1, "bar_setup: calloc: failed to allocate memory.");
2758 if (bar_font_legacy)
2764 Y(r->bar) = bar_at_bottom ? (Y(r) + HEIGHT(r) - bar_height) : Y(r);
2765 WIDTH(r->bar) = WIDTH(r) - 2 * bar_border_width;
2766 HEIGHT(r->bar) = bar_height - 2 * bar_border_width;
2768 /* Assume region is unfocused when we create the bar. */
2769 r->bar->id = xcb_generate_id(conn);
2770 wa[0] = r->s->c[SWM_S_COLOR_BAR].pixel;
2771 wa[1] = r->s->c[SWM_S_COLOR_BAR_BORDER_UNFOCUS].pixel;
2772 wa[2] = XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_POINTER_MOTION |
2773 XCB_EVENT_MASK_POINTER_MOTION_HINT;
2775 xcb_create_window(conn, XCB_COPY_FROM_PARENT, r->bar->id, r->s->root,
2776 X(r->bar), Y(r->bar), WIDTH(r->bar), HEIGHT(r->bar),
2777 bar_border_width, XCB_WINDOW_CLASS_INPUT_OUTPUT,
2778 XCB_COPY_FROM_PARENT, XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL
2779 | XCB_CW_EVENT_MASK, wa);
2781 /* Stack bar window above region window to start. */
2783 wa[1] = XCB_STACK_MODE_ABOVE;
2785 xcb_configure_window(conn, r->bar->id, XCB_CONFIG_WINDOW_SIBLING |
2786 XCB_CONFIG_WINDOW_STACK_MODE, wa);
2788 r->bar->buffer = xcb_generate_id(conn);
2789 xcb_create_pixmap(conn, screen->root_depth, r->bar->buffer, r->bar->id,
2790 WIDTH(r->bar), HEIGHT(r->bar));
2793 xcb_randr_select_input(conn, r->bar->id,
2794 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE);
2797 xcb_map_window(conn, r->bar->id);
2799 DNPRINTF(SWM_D_BAR, "bar_setup: win %#x, (x,y) w x h: (%d,%d) "
2800 "%d x %d\n", WINID(r->bar), X(r->bar), Y(r->bar), WIDTH(r->bar),
2807 bar_cleanup(struct swm_region *r)
2811 xcb_destroy_window(conn, r->bar->id);
2812 xcb_free_pixmap(conn, r->bar->buffer);
2818 set_win_state(struct ws_win *win, uint8_t state)
2820 uint16_t data[2] = { state, XCB_ATOM_NONE };
2822 DNPRINTF(SWM_D_EVENT, "set_win_state: win %#x, state: %u\n",
2828 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id, a_state,
2829 a_state, 32, 2, data);
2833 get_win_state(xcb_window_t w)
2835 xcb_get_property_reply_t *r;
2836 xcb_get_property_cookie_t c;
2837 uint32_t result = 0;
2839 c = xcb_get_property(conn, 0, w, a_state, a_state, 0L, 2L);
2840 r = xcb_get_property_reply(conn, c, NULL);
2842 if (r->type == a_state && r->format == 32 && r->length == 2)
2843 result = *((uint32_t *)xcb_get_property_value(r));
2847 DNPRINTF(SWM_D_MISC, "get_win_state property: win %#x state %u\n", w,
2853 version(struct swm_region *r, union arg *args)
2855 /* suppress unused warnings since vars are needed */
2859 bar_version = !bar_version;
2861 snprintf(bar_vertext, sizeof bar_vertext,
2862 "Version: %s Build: %s", SPECTRWM_VERSION, buildstr);
2864 strlcpy(bar_vertext, "", sizeof bar_vertext);
2871 client_msg(struct ws_win *win, xcb_atom_t a, xcb_timestamp_t t)
2873 xcb_client_message_event_t ev;
2881 name = get_atom_name(a);
2882 DNPRINTF(SWM_D_EVENT, "client_msg: win %#x, atom: %s(%u), "
2884 win->id, name, a, t);
2888 bzero(&ev, sizeof ev);
2889 ev.response_type = XCB_CLIENT_MESSAGE;
2890 ev.window = win->id;
2893 ev.data.data32[0] = a;
2894 ev.data.data32[1] = t;
2896 xcb_send_event(conn, 0, win->id,
2897 XCB_EVENT_MASK_NO_EVENT, (const char *)&ev);
2900 /* synthetic response to a ConfigureRequest when not making a change */
2902 config_win(struct ws_win *win, xcb_configure_request_event_t *ev)
2904 xcb_configure_notify_event_t ce;
2909 /* send notification of unchanged state. */
2910 bzero(&ce, sizeof(ce));
2911 ce.response_type = XCB_CONFIGURE_NOTIFY;
2914 ce.width = WIDTH(win);
2915 ce.height = HEIGHT(win);
2916 ce.override_redirect = 0;
2921 ce.window = win->id;
2922 ce.border_width = BORDER(win);
2923 ce.above_sibling = XCB_WINDOW_NONE;
2926 ce.event = ev->window;
2927 ce.window = ev->window;
2929 /* make response appear more WM_SIZE_HINTS-compliant */
2930 if (win->sh.flags) {
2931 DNPRINTF(SWM_D_MISC, "config_win: hints: win %#x,"
2932 " sh.flags: %u, min: %d x %d, max: %d x %d, inc: "
2933 "%d x %d\n", win->id, win->sh.flags, SH_MIN_W(win),
2934 SH_MIN_H(win), SH_MAX_W(win), SH_MAX_H(win),
2935 SH_INC_W(win), SH_INC_H(win));
2940 /* the hint may be set... to 0! */
2941 if (SH_MIN_W(win) > 0 && ce.width < SH_MIN_W(win))
2942 ce.width = SH_MIN_W(win);
2943 if (SH_MIN_H(win) > 0 && ce.height < SH_MIN_H(win))
2944 ce.height = SH_MIN_H(win);
2949 /* may also be advertized as 0 */
2950 if (SH_MAX_W(win) > 0 && ce.width > SH_MAX_W(win))
2951 ce.width = SH_MAX_W(win);
2952 if (SH_MAX_H(win) > 0 && ce.height > SH_MAX_H(win))
2953 ce.height = SH_MAX_H(win);
2956 /* resize increment. */
2958 if (SH_INC_W(win) > 1 && ce.width > SH_INC_W(win))
2959 ce.width -= (ce.width - SH_MIN_W(win)) %
2961 if (SH_INC_H(win) > 1 && ce.height > SH_INC_H(win))
2962 ce.height -= (ce.height - SH_MIN_H(win)) %
2966 /* adjust x and y for requested border_width. */
2967 ce.x += BORDER(win) - ev->border_width;
2968 ce.y += BORDER(win) - ev->border_width;
2969 ce.border_width = ev->border_width;
2970 ce.above_sibling = ev->sibling;
2973 DNPRINTF(SWM_D_MISC, "config_win: ewmh: %s, win %#x, (x,y) w x h: "
2974 "(%d,%d) %d x %d, border: %d\n", YESNO(ev == NULL), win->id, ce.x,
2975 ce.y, ce.width, ce.height, ce.border_width);
2977 xcb_send_event(conn, 0, win->id, XCB_EVENT_MASK_STRUCTURE_NOTIFY,
2982 count_win(struct workspace *ws, bool count_transient)
2987 TAILQ_FOREACH(win, &ws->winlist, entry) {
2988 if (!count_transient && FLOATING(win))
2994 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
3000 quit(struct swm_region *r, union arg *args)
3002 /* suppress unused warnings since vars are needed */
3006 DNPRINTF(SWM_D_MISC, "quit\n");
3011 raise_window(struct ws_win *win)
3013 struct ws_win *target = NULL;
3014 struct swm_region *r;
3015 struct workspace *ws;
3017 if (win == NULL || (r = win->ws->r) == NULL)
3021 DNPRINTF(SWM_D_EVENT, "raise_window: win %#x\n", win->id);
3023 TAILQ_FOREACH(target, &ws->stack, stack_entry) {
3024 if (target == win || ICONIC(target))
3026 if (ws->cur_layout == &layouts[SWM_MAX_STACK])
3028 if (TRANS(win) && (win->transient == target->transient ||
3029 win->transient == target->id))
3031 if (FULLSCREEN(win))
3033 if (FULLSCREEN(target))
3037 if (MAXIMIZED(target))
3039 if (ABOVE(win) || TRANS(win))
3041 if (!ABOVE(target) && !TRANS(target))
3045 if (target != NULL) {
3046 /* Change stack position. */
3047 TAILQ_REMOVE(&ws->stack, win, stack_entry);
3048 TAILQ_INSERT_BEFORE(target, win, stack_entry);
3049 update_win_stacking(win);
3053 if (swm_debug & SWM_D_STACK) {
3054 DPRINTF("=== stacking order (top down) === \n");
3055 TAILQ_FOREACH(target, &r->ws->stack, stack_entry) {
3056 DPRINTF("win %#x, fs: %s, maximized: %s, above: %s, "
3057 "iconic: %s\n", target->id, YESNO(FULLSCREEN(target)),
3058 YESNO(MAXIMIZED(target)), YESNO(ABOVE(target)),
3059 YESNO(ICONIC(target)));
3063 DNPRINTF(SWM_D_EVENT, "raise_window: done\n");
3067 update_win_stacking(struct ws_win *win)
3069 struct ws_win *sibling;
3070 struct swm_region *r;
3073 if (win == NULL || (r = win->ws->r) == NULL)
3076 sibling = TAILQ_NEXT(win, stack_entry);
3077 if (sibling != NULL && FLOATING(win) == FLOATING(sibling))
3078 val[0] = sibling->id;
3080 val[0] = FLOATING(win) ? r->bar->id : r->id;
3082 DNPRINTF(SWM_D_EVENT, "update_win_stacking: %#x, sibling %#x\n",
3085 val[1] = XCB_STACK_MODE_ABOVE;
3087 xcb_configure_window(conn, win->id, XCB_CONFIG_WINDOW_SIBLING |
3088 XCB_CONFIG_WINDOW_STACK_MODE, val);
3092 map_window(struct ws_win *win)
3097 DNPRINTF(SWM_D_EVENT, "map_window: win %#x, mapped: %s\n",
3098 win->id, YESNO(win->mapped));
3103 xcb_map_window(conn, win->id);
3104 set_win_state(win, XCB_ICCCM_WM_STATE_NORMAL);
3109 unmap_window(struct ws_win *win)
3114 DNPRINTF(SWM_D_EVENT, "unmap_window: win %#x, mapped: %s\n", win->id,
3115 YESNO(win->mapped));
3120 xcb_unmap_window(conn, win->id);
3121 set_win_state(win, XCB_ICCCM_WM_STATE_ICONIC);
3122 win->mapped = false;
3129 int i, j, num_screens;
3131 num_screens = get_screen_count();
3132 for (i = 0; i < num_screens; i++)
3133 for (j = 0; j < workspace_limit; j++)
3134 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
3139 fake_keypress(struct ws_win *win, xcb_keysym_t keysym, uint16_t modifiers)
3141 xcb_key_press_event_t event;
3142 xcb_keycode_t *keycode;
3147 keycode = xcb_key_symbols_get_keycode(syms, keysym);
3149 DNPRINTF(SWM_D_MISC, "fake_keypress: win %#x, keycode %u\n",
3152 bzero(&event, sizeof(event));
3153 event.event = win->id;
3154 event.root = win->s->root;
3155 event.child = XCB_WINDOW_NONE;
3156 event.time = XCB_CURRENT_TIME;
3157 event.event_x = X(win);
3158 event.event_y = Y(win);
3161 event.same_screen = 1;
3162 event.detail = *keycode;
3163 event.state = modifiers;
3165 event.response_type = XCB_KEY_PRESS;
3166 xcb_send_event(conn, 1, win->id,
3167 XCB_EVENT_MASK_KEY_PRESS, (const char *)&event);
3169 event.response_type = XCB_KEY_RELEASE;
3170 xcb_send_event(conn, 1, win->id,
3171 XCB_EVENT_MASK_KEY_RELEASE, (const char *)&event);
3177 restart(struct swm_region *r, union arg *args)
3179 /* suppress unused warning since var is needed */
3183 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
3187 execvp(start_argv[0], start_argv);
3188 warn("execvp failed");
3193 get_pointer_win(xcb_window_t root)
3195 struct ws_win *win = NULL;
3196 xcb_query_pointer_reply_t *r;
3198 DNPRINTF(SWM_D_EVENT, "get_pointer_win: root: %#x.\n", root);
3200 r = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, root), NULL);
3202 win = find_window(r->child);
3204 DNPRINTF(SWM_D_EVENT, "get_pointer_win: %#x.\n",
3207 DNPRINTF(SWM_D_EVENT, "get_pointer_win: none.\n");
3216 root_to_region(xcb_window_t root, int check)
3219 struct swm_region *r = NULL;
3221 xcb_query_pointer_reply_t *qpr;
3222 xcb_get_input_focus_reply_t *gifr;
3224 DNPRINTF(SWM_D_MISC, "root_to_region: win %#x\n", root);
3226 num_screens = get_screen_count();
3227 for (i = 0; i < num_screens; i++)
3228 if (screens[i].root == root)
3231 if (check & SWM_CK_REGION)
3232 r = screens[i].r_focus;
3234 if (r == NULL && check & SWM_CK_FOCUS) {
3235 /* Try to find an actively focused window */
3236 gifr = xcb_get_input_focus_reply(conn,
3237 xcb_get_input_focus(conn), NULL);
3239 cfw = find_window(gifr->focus);
3240 if (cfw && cfw->ws->r)
3247 if (r == NULL && check & SWM_CK_POINTER) {
3248 /* No region with an active focus; try to use pointer. */
3249 qpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn,
3250 screens[i].root), NULL);
3252 DNPRINTF(SWM_D_MISC, "root_to_region: pointer: "
3253 "(%d,%d)\n", qpr->root_x, qpr->root_y);
3254 TAILQ_FOREACH(r, &screens[i].rl, entry)
3255 if (X(r) <= qpr->root_x &&
3256 qpr->root_x < MAX_X(r) &&
3257 Y(r) <= qpr->root_y &&
3258 qpr->root_y < MAX_Y(r))
3265 if (r == NULL && check & SWM_CK_FALLBACK)
3266 r = TAILQ_FIRST(&screens[i].rl);
3268 DNPRINTF(SWM_D_MISC, "root_to_region: idx: %d\n", get_region_index(r));
3274 find_unmanaged_window(xcb_window_t id)
3277 int i, j, num_screens;
3279 num_screens = get_screen_count();
3280 for (i = 0; i < num_screens; i++)
3281 for (j = 0; j < workspace_limit; j++)
3282 TAILQ_FOREACH(win, &screens[i].ws[j].unmanagedlist,
3290 find_window(xcb_window_t id)
3293 int i, j, num_screens;
3294 xcb_query_tree_reply_t *r;
3296 num_screens = get_screen_count();
3297 for (i = 0; i < num_screens; i++)
3298 for (j = 0; j < workspace_limit; j++)
3299 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
3303 r = xcb_query_tree_reply(conn, xcb_query_tree(conn, id), NULL);
3307 /* if we were looking for the parent return that window instead */
3308 if (r->parent == 0 || r->root == r->parent) {
3313 /* look for parent */
3314 for (i = 0; i < num_screens; i++)
3315 for (j = 0; j < workspace_limit; j++)
3316 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
3317 if (r->parent == win->id) {
3327 spawn(int ws_idx, union arg *args, bool close_fd)
3332 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
3334 close(xcb_get_file_descriptor(conn));
3336 setenv("LD_PRELOAD", SWM_LIB, 1);
3338 if (asprintf(&ret, "%d", ws_idx) == -1) {
3339 warn("spawn: asprintf SWM_WS");
3342 setenv("_SWM_WS", ret, 1);
3346 if (asprintf(&ret, "%d", getpid()) == -1) {
3347 warn("spawn: asprintf _SWM_PID");
3350 setenv("_SWM_PID", ret, 1);
3354 if (setsid() == -1) {
3355 warn("spawn: setsid");
3361 * close stdin and stdout to prevent interaction between apps
3362 * and the baraction script
3363 * leave stderr open to record errors
3365 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) == -1) {
3366 warn("spawn: open");
3369 dup2(fd, STDIN_FILENO);
3370 dup2(fd, STDOUT_FILENO);
3375 execvp(args->argv[0], args->argv);
3377 warn("spawn: execvp");
3382 kill_refs(struct ws_win *win)
3384 int i, x, num_screens;
3385 struct swm_region *r;
3386 struct workspace *ws;
3391 num_screens = get_screen_count();
3392 for (i = 0; i < num_screens; i++)
3393 TAILQ_FOREACH(r, &screens[i].rl, entry)
3394 for (x = 0; x < workspace_limit; x++) {
3396 if (win == ws->focus)
3398 if (win == ws->focus_prev)
3399 ws->focus_prev = NULL;
3404 validate_win(struct ws_win *testwin)
3407 struct workspace *ws;
3408 struct swm_region *r;
3409 int i, x, num_screens;
3411 if (testwin == NULL)
3414 num_screens = get_screen_count();
3415 for (i = 0; i < num_screens; i++)
3416 TAILQ_FOREACH(r, &screens[i].rl, entry)
3417 for (x = 0; x < workspace_limit; x++) {
3419 TAILQ_FOREACH(win, &ws->winlist, entry)
3427 validate_ws(struct workspace *testws)
3429 struct swm_region *r;
3430 struct workspace *ws;
3431 int i, x, num_screens;
3433 /* validate all ws */
3434 num_screens = get_screen_count();
3435 for (i = 0; i < num_screens; i++)
3436 TAILQ_FOREACH(r, &screens[i].rl, entry)
3437 for (x = 0; x < workspace_limit; x++) {
3445 #define OPAQUE 0xffffffff
3447 set_opacity(struct ws_win *win, uint32_t opacity)
3449 if (opacity != OPAQUE)
3450 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
3451 ewmh[_NET_WM_WINDOW_OPACITY].atom, XCB_ATOM_CARDINAL, 32, 1,
3454 xcb_delete_property(conn, win->id,
3455 ewmh[_NET_WM_WINDOW_OPACITY].atom);
3459 unfocus_win(struct ws_win *win)
3461 xcb_window_t none = XCB_WINDOW_NONE;
3463 DNPRINTF(SWM_D_FOCUS, "unfocus_win: win %#x\n", WINID(win));
3468 if (win->ws == NULL) {
3469 DNPRINTF(SWM_D_FOCUS, "unfocus_win: NULL ws.\n");
3473 if (validate_ws(win->ws)) {
3474 DNPRINTF(SWM_D_FOCUS, "unfocus_win: invalid ws.\n");
3478 if (win->ws->r == NULL) {
3479 DNPRINTF(SWM_D_FOCUS, "unfocus_win: NULL region.\n");
3483 if (validate_win(win)) {
3484 DNPRINTF(SWM_D_FOCUS, "unfocus_win: invalid win.\n");
3489 if (win->ws->focus == win) {
3490 win->ws->focus = NULL;
3491 win->ws->focus_prev = win;
3494 if (validate_win(win->ws->focus)) {
3495 kill_refs(win->ws->focus);
3496 win->ws->focus = NULL;
3499 if (validate_win(win->ws->focus_prev)) {
3500 kill_refs(win->ws->focus_prev);
3501 win->ws->focus_prev = NULL;
3504 update_window_color(win);
3506 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
3507 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1, &none);
3509 DNPRINTF(SWM_D_FOCUS, "unfocus_win: done.\n");
3513 focus_win(struct ws_win *win)
3515 struct ws_win *cfw = NULL, *parent = NULL, *w, *tmpw;
3516 struct workspace *ws;
3517 xcb_get_input_focus_reply_t *gifr;
3519 DNPRINTF(SWM_D_FOCUS, "focus_win: win %#x\n", WINID(win));
3521 if (win == NULL || win->ws == NULL || !win->mapped)
3526 if (validate_ws(ws))
3529 if (validate_win(win)) {
3534 gifr = xcb_get_input_focus_reply(conn, xcb_get_input_focus(conn), NULL);
3536 cfw = find_window(gifr->focus);
3537 if (cfw != NULL && cfw != win) {
3538 if (cfw->ws != ws && cfw->ws->r != NULL) {
3539 /* Change border to unfocused color. */
3540 xcb_change_window_attributes(conn, cfw->id,
3541 XCB_CW_BORDER_PIXEL,
3542 &cfw->s->c[(MAXIMIZED(cfw) ?
3543 SWM_S_COLOR_UNFOCUS_MAXIMIZED :
3544 SWM_S_COLOR_UNFOCUS)].pixel);
3546 if (composite_enabled)
3548 opacity_unfocus * OPAQUE);
3556 if (ws->focus != win) {
3557 if (ws->focus && ws->focus != cfw)
3558 unfocus_win(ws->focus);
3562 /* If this window directs focus to a child window, then clear. */
3563 if (win->focus_child)
3564 win->focus_child = NULL;
3566 /* If transient, adjust parent's focus child for focus_magic. */
3568 parent = find_window(win->transient);
3569 if (parent && parent->focus_child != win)
3570 parent->focus_child = win;
3573 if (cfw != win && ws->r != NULL) {
3574 /* Set input focus if no input hint, or indicated by hint. */
3575 if (!(win->hints.flags & XCB_ICCCM_WM_HINT_INPUT) ||
3576 (win->hints.flags & XCB_ICCCM_WM_HINT_INPUT &&
3578 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT,
3579 win->id, last_event_time);
3581 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT,
3582 ws->r->id, XCB_CURRENT_TIME);
3584 /* Tell app it can adjust focus to a specific window. */
3585 if (win->take_focus) {
3586 /* java is special; always tell parent */
3587 if (TRANS(win) && win->java)
3588 client_msg(parent, a_takefocus,
3591 client_msg(win, a_takefocus, last_event_time);
3594 if (ws->cur_layout->flags & SWM_L_MAPONFOCUS ||
3596 /* If a parent exists, map it first. */
3598 raise_window(parent);
3601 /* Map siblings next. */
3602 TAILQ_FOREACH_SAFE(w, &ws->stack, stack_entry,
3604 if (w != win && !ICONIC(w) &&
3605 w->transient == parent->id) {
3611 /* Map focused window. */
3615 /* Stack any children of focus window. */
3616 TAILQ_FOREACH_SAFE(w, &ws->stack, stack_entry, tmpw)
3617 if (w->transient == win->id && !ICONIC(w)) {
3621 } else if (tile_gap < 0 && !ABOVE(win)) {
3623 * Windows overlap in the layout.
3624 * Raise focused win above all tiled wins.
3632 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
3633 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1,
3638 /* Update window border even if workspace is hidden. */
3639 update_window_color(win);
3644 DNPRINTF(SWM_D_FOCUS, "focus_win: done.\n");
3647 /* If a child window should have focus instead, return it. */
3649 get_focus_magic(struct ws_win *win)
3651 struct ws_win *parent = NULL;
3652 struct ws_win *child = NULL;
3654 DNPRINTF(SWM_D_FOCUS, "get_focus_magic: win %#x\n", WINID(win));
3659 parent = find_window(win->transient);
3661 /* If parent prefers focus elsewhere, then try to do so. */
3662 if (parent && (child = parent->focus_child)) {
3663 if (validate_win(child) == 0 && child->mapped)
3666 parent->focus_child = NULL;
3670 /* If this window prefers focus elsewhere, then try to do so. */
3671 if ((child = win->focus_child)) {
3672 if (validate_win(child) == 0 && child->mapped)
3675 win->focus_child = NULL;
3682 event_drain(uint8_t rt)
3684 xcb_generic_event_t *evt;
3686 /* ensure all pending requests have been processed before filtering. */
3688 while ((evt = xcb_poll_for_event(conn))) {
3689 if (XCB_EVENT_RESPONSE_TYPE(evt) != rt)
3697 set_region(struct swm_region *r)
3699 struct swm_region *rf;
3706 /* Unfocus old region bar. */
3711 xcb_change_window_attributes(conn, rf->bar->id,
3712 XCB_CW_BORDER_PIXEL,
3713 &r->s->c[SWM_S_COLOR_BAR_BORDER_UNFOCUS].pixel);
3716 if (rf != NULL && rf != r && (X(rf) != X(r) || Y(rf) != Y(r) ||
3717 WIDTH(rf) != WIDTH(r) || HEIGHT(rf) != HEIGHT(r))) {
3718 /* Set _NET_DESKTOP_GEOMETRY. */
3720 vals[1] = HEIGHT(r);
3721 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, r->s->root,
3722 ewmh[_NET_DESKTOP_GEOMETRY].atom, XCB_ATOM_CARDINAL, 32, 2,
3726 /* Set region bar border to focus_color. */
3727 xcb_change_window_attributes(conn, r->bar->id,
3728 XCB_CW_BORDER_PIXEL, &r->s->c[SWM_S_COLOR_BAR_BORDER].pixel);
3732 ewmh_update_current_desktop();
3736 focus_region(struct swm_region *r)
3739 struct swm_region *old_r;
3744 old_r = r->s->r_focus;
3747 nfw = get_region_focus(r);
3751 /* New region is empty; need to manually unfocus win. */
3753 unfocus_win(old_r->ws->focus);
3755 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT, r->id,
3758 /* Clear bar since empty. */
3764 switchws(struct swm_region *r, union arg *args)
3766 struct swm_region *this_r, *other_r;
3768 struct workspace *new_ws, *old_ws;
3769 xcb_window_t none = XCB_WINDOW_NONE;
3770 int wsid = args->id;
3771 bool unmap_old = false;
3776 if (wsid >= workspace_limit)
3780 old_ws = this_r->ws;
3781 new_ws = &this_r->s->ws[wsid];
3783 DNPRINTF(SWM_D_WS, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
3784 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), old_ws->idx, wsid);
3786 if (new_ws == NULL || old_ws == NULL)
3788 if (new_ws == old_ws)
3791 if ((win = old_ws->focus) != NULL) {
3792 update_window_color(win);
3794 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
3795 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1,
3799 other_r = new_ws->r;
3800 if (other_r == NULL) {
3801 /* the other workspace is hidden, hide this one */
3805 /* the other ws is visible in another region, exchange them */
3806 other_r->ws_prior = new_ws;
3807 other_r->ws = old_ws;
3808 old_ws->r = other_r;
3810 this_r->ws_prior = old_ws;
3811 this_r->ws = new_ws;
3814 /* Set focus_pending before stacking, if needed. */
3815 if (focus_mode != SWM_FOCUS_FOLLOW && (!new_ws->focus_pending ||
3816 validate_win(new_ws->focus_pending))) {
3817 new_ws->focus_pending = get_region_focus(new_ws->r);
3818 new_ws->focus = new_ws->focus_prev;
3819 new_ws->focus_prev = NULL;
3822 new_ws->state = SWM_WS_STATE_MAPPING;
3825 /* unmap old windows */
3827 TAILQ_FOREACH(win, &old_ws->winlist, entry)
3829 old_ws->state = SWM_WS_STATE_HIDDEN;
3832 /* if workspaces were swapped, then don't wait to set focus */
3833 if (old_ws->r && focus_mode != SWM_FOCUS_FOLLOW) {
3834 if (new_ws->focus_pending) {
3835 focus_win(new_ws->focus_pending);
3836 new_ws->focus_pending = NULL;
3840 /* Clear bar and set focus on region input win if new ws is empty. */
3841 if (new_ws->focus_pending == NULL && new_ws->focus == NULL) {
3842 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT, r->id,
3847 ewmh_update_current_desktop();
3850 new_ws->state = SWM_WS_STATE_MAPPED;
3852 DNPRINTF(SWM_D_WS, "switchws: done.\n");
3856 cyclews(struct swm_region *r, union arg *args)
3859 struct swm_screen *s = r->s;
3860 bool cycle_all = false, mv = false;
3862 DNPRINTF(SWM_D_WS, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3863 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
3869 case SWM_ARG_ID_CYCLEWS_MOVE_UP:
3872 case SWM_ARG_ID_CYCLEWS_UP_ALL:
3875 case SWM_ARG_ID_CYCLEWS_UP:
3876 a.id = (a.id < workspace_limit - 1) ? a.id + 1 : 0;
3878 case SWM_ARG_ID_CYCLEWS_MOVE_DOWN:
3881 case SWM_ARG_ID_CYCLEWS_DOWN_ALL:
3884 case SWM_ARG_ID_CYCLEWS_DOWN:
3885 a.id = (a.id > 0) ? a.id - 1 : workspace_limit - 1;
3892 (!cycle_empty && TAILQ_EMPTY(&s->ws[a.id].winlist)))
3894 if (!cycle_visible && s->ws[a.id].r != NULL)
3901 } while (a.id != r->ws->idx);
3903 DNPRINTF(SWM_D_FOCUS, "cyclews: done\n");
3907 priorws(struct swm_region *r, union arg *args)
3913 DNPRINTF(SWM_D_WS, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3914 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
3916 if (r->ws_prior == NULL)
3919 a.id = r->ws_prior->idx;
3921 DNPRINTF(SWM_D_FOCUS, "priorws: done\n");
3925 focusrg(struct swm_region *r, union arg *args)
3927 int ridx = args->id, i, num_screens;
3928 struct swm_region *rr = NULL;
3930 num_screens = get_screen_count();
3931 /* do nothing if we don't have more than one screen */
3932 if (!(num_screens > 1 || outputs > 1))
3935 DNPRINTF(SWM_D_FOCUS, "focusrg: id: %d\n", ridx);
3937 rr = TAILQ_FIRST(&r->s->rl);
3938 for (i = 0; i < ridx && rr != NULL; ++i)
3939 rr = TAILQ_NEXT(rr, entry);
3946 DNPRINTF(SWM_D_FOCUS, "focusrg: done\n");
3950 cyclerg(struct swm_region *r, union arg *args)
3952 struct swm_region *rr = NULL;
3955 num_screens = get_screen_count();
3956 /* do nothing if we don't have more than one screen */
3957 if (!(num_screens > 1 || outputs > 1))
3961 DNPRINTF(SWM_D_FOCUS, "cyclerg: id: %d, region: %d\n", args->id, i);
3964 case SWM_ARG_ID_CYCLERG_UP:
3965 rr = TAILQ_NEXT(r, entry);
3967 rr = TAILQ_FIRST(&screens[i].rl);
3969 case SWM_ARG_ID_CYCLERG_DOWN:
3970 rr = TAILQ_PREV(r, swm_region_list, entry);
3972 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
3982 DNPRINTF(SWM_D_FOCUS, "cyclerg: done\n");
3985 /* Sorts transients after parent. */
3987 sort_windows(struct ws_win_list *wl)
3989 struct ws_win *win, *parent, *nxt;
3994 for (win = TAILQ_FIRST(wl); win != TAILQ_END(wl); win = nxt) {
3995 nxt = TAILQ_NEXT(win, entry);
3997 parent = find_window(win->transient);
3998 if (parent == NULL) {
3999 warnx("not possible bug");
4002 TAILQ_REMOVE(wl, win, entry);
4003 TAILQ_INSERT_AFTER(wl, parent, win, entry);
4009 swapwin(struct swm_region *r, union arg *args)
4011 struct ws_win *target, *source;
4012 struct ws_win *cur_focus;
4013 struct ws_win_list *wl;
4015 DNPRINTF(SWM_D_WS, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
4016 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
4018 cur_focus = r->ws->focus;
4019 if (cur_focus == NULL || FULLSCREEN(cur_focus))
4022 /* Adjust stacking in floating layer. */
4023 if (ABOVE(cur_focus)) {
4025 case SWM_ARG_ID_SWAPPREV:
4026 target = TAILQ_PREV(cur_focus, ws_win_stack,
4028 if (target != NULL && FLOATING(target)) {
4029 TAILQ_REMOVE(&cur_focus->ws->stack, cur_focus,
4031 TAILQ_INSERT_BEFORE(target, cur_focus,
4033 update_win_stacking(cur_focus);
4037 case SWM_ARG_ID_SWAPNEXT:
4038 target = TAILQ_NEXT(cur_focus, stack_entry);
4039 if (target != NULL && FLOATING(target)) {
4040 TAILQ_REMOVE(&cur_focus->ws->stack, cur_focus,
4042 TAILQ_INSERT_AFTER(&cur_focus->ws->stack,
4043 target, cur_focus, stack_entry);
4044 update_win_stacking(cur_focus);
4052 if (r->ws->cur_layout == &layouts[SWM_MAX_STACK])
4055 clear_maximized(r->ws);
4058 wl = &source->ws->winlist;
4061 case SWM_ARG_ID_SWAPPREV:
4063 source = find_window(source->transient);
4064 target = TAILQ_PREV(source, ws_win_list, entry);
4065 if (target && target->transient)
4066 target = find_window(target->transient);
4067 TAILQ_REMOVE(wl, source, entry);
4069 TAILQ_INSERT_TAIL(wl, source, entry);
4071 TAILQ_INSERT_BEFORE(target, source, entry);
4073 case SWM_ARG_ID_SWAPNEXT:
4074 target = TAILQ_NEXT(source, entry);
4075 /* move the parent and let the sort handle the move */
4077 source = find_window(source->transient);
4078 TAILQ_REMOVE(wl, source, entry);
4080 TAILQ_INSERT_HEAD(wl, source, entry);
4082 TAILQ_INSERT_AFTER(wl, target, source, entry);
4084 case SWM_ARG_ID_SWAPMAIN:
4085 target = TAILQ_FIRST(wl);
4086 if (target == source) {
4087 if (source->ws->focus_prev != NULL &&
4088 source->ws->focus_prev != target)
4089 source = source->ws->focus_prev;
4093 if (target == NULL || source == NULL)
4095 source->ws->focus_prev = target;
4096 TAILQ_REMOVE(wl, target, entry);
4097 TAILQ_INSERT_BEFORE(source, target, entry);
4098 TAILQ_REMOVE(wl, source, entry);
4099 TAILQ_INSERT_HEAD(wl, source, entry);
4101 case SWM_ARG_ID_MOVELAST:
4102 TAILQ_REMOVE(wl, source, entry);
4103 TAILQ_INSERT_TAIL(wl, source, entry);
4106 DNPRINTF(SWM_D_MOVE, "swapwin: invalid id: %d\n", args->id);
4111 ewmh_update_client_list();
4116 DNPRINTF(SWM_D_MOVE, "swapwin: done\n");
4120 get_focus_prev(struct ws_win *win)
4122 struct ws_win *winfocus = NULL;
4123 struct ws_win *cur_focus = NULL;
4124 struct ws_win_list *wl = NULL;
4125 struct workspace *ws = NULL;
4127 if (!(win && win->ws))
4132 cur_focus = ws->focus;
4134 DNPRINTF(SWM_D_FOCUS, "get_focus_prev: win %#x, cur_focus: %#x, "
4135 "focus_prev: %#x\n", WINID(win), WINID(cur_focus),
4136 WINID(ws->focus_prev));
4138 /* pickle, just focus on whatever */
4139 if (cur_focus == NULL) {
4140 /* use prev_focus if valid */
4141 if (ws->focus_prev && find_window(ws->focus_prev->id))
4142 winfocus = ws->focus_prev;
4146 /* if transient focus on parent */
4147 if (TRANS(cur_focus)) {
4148 winfocus = find_window(cur_focus->transient);
4152 /* if in max_stack try harder */
4153 if ((win->quirks & SWM_Q_FOCUSPREV) ||
4154 (ws->cur_layout->flags & SWM_L_FOCUSPREV)) {
4155 if (cur_focus != ws->focus_prev)
4156 winfocus = ws->focus_prev;
4158 winfocus = TAILQ_PREV(win, ws_win_list, entry);
4163 DNPRINTF(SWM_D_FOCUS, "get_focus_prev: focus_close: %d\n", focus_close);
4165 if (winfocus == NULL || winfocus == win) {
4166 switch (focus_close) {
4167 case SWM_STACK_BOTTOM:
4168 TAILQ_FOREACH(winfocus, wl, entry)
4169 if (!ICONIC(winfocus) && winfocus != cur_focus)
4173 TAILQ_FOREACH_REVERSE(winfocus, wl, ws_win_list, entry)
4174 if (!ICONIC(winfocus) && winfocus != cur_focus)
4177 case SWM_STACK_ABOVE:
4178 winfocus = TAILQ_NEXT(cur_focus, entry);
4179 while (winfocus && ICONIC(winfocus))
4180 winfocus = TAILQ_NEXT(winfocus, entry);
4182 if (winfocus == NULL) {
4183 if (focus_close_wrap) {
4184 TAILQ_FOREACH(winfocus, wl, entry)
4185 if (!ICONIC(winfocus) &&
4186 winfocus != cur_focus)
4189 TAILQ_FOREACH_REVERSE(winfocus, wl,
4191 if (!ICONIC(winfocus) &&
4192 winfocus != cur_focus)
4197 case SWM_STACK_BELOW:
4198 winfocus = TAILQ_PREV(cur_focus, ws_win_list, entry);
4199 while (winfocus && ICONIC(winfocus))
4200 winfocus = TAILQ_PREV(winfocus, ws_win_list,
4203 if (winfocus == NULL) {
4204 if (focus_close_wrap) {
4205 TAILQ_FOREACH_REVERSE(winfocus, wl,
4207 if (!ICONIC(winfocus) &&
4208 winfocus != cur_focus)
4211 TAILQ_FOREACH(winfocus, wl, entry)
4212 if (!ICONIC(winfocus) &&
4213 winfocus != cur_focus)
4221 if (winfocus == NULL ||
4222 (winfocus && (ICONIC(winfocus) || winfocus == cur_focus))) {
4223 if (focus_default == SWM_STACK_TOP) {
4224 TAILQ_FOREACH_REVERSE(winfocus, wl, ws_win_list, entry)
4225 if (!ICONIC(winfocus) && winfocus != cur_focus)
4228 TAILQ_FOREACH(winfocus, wl, entry)
4229 if (!ICONIC(winfocus) && winfocus != cur_focus)
4236 return get_focus_magic(winfocus);
4240 get_region_focus(struct swm_region *r)
4242 struct ws_win *winfocus = NULL;
4247 if (r->ws->focus && !ICONIC(r->ws->focus))
4248 winfocus = r->ws->focus;
4249 else if (r->ws->focus_prev && !ICONIC(r->ws->focus_prev))
4250 winfocus = r->ws->focus_prev;
4252 TAILQ_FOREACH(winfocus, &r->ws->winlist, entry)
4253 if (!ICONIC(winfocus))
4256 return get_focus_magic(winfocus);
4260 focus(struct swm_region *r, union arg *args)
4262 struct ws_win *head, *cur_focus = NULL, *winfocus = NULL;
4263 struct ws_win_list *wl = NULL;
4264 struct workspace *ws = NULL;
4267 xcb_icccm_wm_hints_t hints;
4272 cur_focus = r->ws->focus;
4276 DNPRINTF(SWM_D_FOCUS, "focus: id: %d, cur_focus: %#x\n", args->id,
4279 /* Make sure an uniconified window has focus, if one exists. */
4280 if (cur_focus == NULL) {
4281 cur_focus = TAILQ_FIRST(wl);
4282 while (cur_focus != NULL && ICONIC(cur_focus))
4283 cur_focus = TAILQ_NEXT(cur_focus, entry);
4285 DNPRINTF(SWM_D_FOCUS, "focus: new cur_focus: %#x\n",
4290 case SWM_ARG_ID_FOCUSPREV:
4291 if (cur_focus == NULL)
4294 winfocus = cur_focus;
4296 winfocus = TAILQ_PREV(winfocus, ws_win_list, entry);
4297 if (winfocus == NULL)
4298 winfocus = TAILQ_LAST(wl, ws_win_list);
4299 if (winfocus == cur_focus)
4301 } while (winfocus && (ICONIC(winfocus) ||
4302 winfocus->id == cur_focus->transient ||
4303 (cur_focus->transient != XCB_WINDOW_NONE &&
4304 winfocus->transient == cur_focus->transient)));
4306 case SWM_ARG_ID_FOCUSNEXT:
4307 if (cur_focus == NULL)
4310 winfocus = cur_focus;
4312 winfocus = TAILQ_NEXT(winfocus, entry);
4313 if (winfocus == NULL)
4314 winfocus = TAILQ_FIRST(wl);
4315 if (winfocus == cur_focus)
4317 } while (winfocus && (ICONIC(winfocus) ||
4318 winfocus->id == cur_focus->transient ||
4319 (cur_focus->transient != XCB_WINDOW_NONE &&
4320 winfocus->transient == cur_focus->transient)));
4322 case SWM_ARG_ID_FOCUSMAIN:
4323 if (cur_focus == NULL)
4326 winfocus = TAILQ_FIRST(wl);
4327 if (winfocus == cur_focus)
4330 case SWM_ARG_ID_FOCUSURGENT:
4331 /* Search forward for the next urgent window. */
4335 for (i = 0; i <= workspace_limit; ++i) {
4337 head = TAILQ_FIRST(&r->s->ws[(ws->idx + i) %
4338 workspace_limit].winlist);
4340 while (head != NULL &&
4341 (head = TAILQ_NEXT(head, entry)) != NULL) {
4342 if (head == cur_focus) {
4343 winfocus = cur_focus;
4346 if (xcb_icccm_get_wm_hints_reply(conn,
4347 xcb_icccm_get_wm_hints(conn, head->id),
4348 &hints, NULL) != 0 &&
4349 xcb_icccm_wm_hints_get_urgency(&hints)) {
4355 if (winfocus != NULL)
4359 /* Switch ws if new focus is on a different ws. */
4360 if (winfocus != NULL && winfocus->ws != ws) {
4361 a.id = winfocus->ws->idx;
4369 if (clear_maximized(ws) > 0)
4372 focus_win(get_focus_magic(winfocus));
4376 DNPRINTF(SWM_D_FOCUS, "focus: done\n");
4380 cycle_layout(struct swm_region *r, union arg *args)
4382 struct workspace *ws = r->ws;
4384 /* suppress unused warning since var is needed */
4387 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
4390 if (ws->cur_layout->l_stack == NULL)
4391 ws->cur_layout = &layouts[0];
4393 clear_maximized(ws);
4398 focus_win(get_region_focus(r));
4404 stack_config(struct swm_region *r, union arg *args)
4406 struct workspace *ws = r->ws;
4408 DNPRINTF(SWM_D_STACK, "stack_config: id: %d workspace: %d\n",
4411 if (clear_maximized(ws) > 0)
4414 if (ws->cur_layout->l_config != NULL)
4415 ws->cur_layout->l_config(ws, args->id);
4417 if (args->id != SWM_ARG_ID_STACKINIT)
4426 struct swm_geometry g;
4427 struct swm_region *r, *r_prev = NULL;
4434 DNPRINTF(SWM_D_STACK, "stack: begin\n");
4436 num_screens = get_screen_count();
4437 for (i = 0; i < num_screens; i++) {
4441 TAILQ_FOREACH(r, &screens[i].rl, entry) {
4442 /* Adjust stack area for region bar and padding. */
4444 g.x += region_padding;
4445 g.y += region_padding;
4446 g.w -= 2 * border_width + 2 * region_padding;
4447 g.h -= 2 * border_width + 2 * region_padding;
4448 if (bar_enabled && r->ws->bar_enabled) {
4454 DNPRINTF(SWM_D_STACK, "stack: workspace: %d (screen: "
4455 "%d, region: %d), (x,y) WxH: (%d,%d) %d x %d\n",
4456 r->ws->idx, i, j++, g.x, g.y, g.w, g.h);
4459 /* Stack bar/input relative to prev. region. */
4460 val[1] = XCB_STACK_MODE_ABOVE;
4462 val[0] = r_prev->id;
4463 DNPRINTF(SWM_D_STACK, "stack: region input %#x "
4464 "relative to %#x.\n", r->id, val[0]);
4465 xcb_configure_window(conn, r->id,
4466 XCB_CONFIG_WINDOW_SIBLING |
4467 XCB_CONFIG_WINDOW_STACK_MODE, val);
4469 val[0] = r_prev->bar->id;
4470 DNPRINTF(SWM_D_STACK, "stack: region bar %#x "
4471 "relative to %#x.\n", r->bar->id, val[0]);
4472 xcb_configure_window(conn, r->bar->id,
4473 XCB_CONFIG_WINDOW_SIBLING |
4474 XCB_CONFIG_WINDOW_STACK_MODE, val);
4477 r->ws->cur_layout->l_stack(r->ws, &g);
4478 r->ws->cur_layout->l_string(r->ws);
4479 /* save r so we can track region changes */
4487 DNPRINTF(SWM_D_STACK, "stack: end\n");
4491 store_float_geom(struct ws_win *win)
4493 if (win == NULL || win->ws->r == NULL)
4496 /* retain window geom and region geom */
4497 win->g_float = win->g;
4498 win->g_float.x -= X(win->ws->r);
4499 win->g_float.y -= Y(win->ws->r);
4500 win->g_floatvalid = true;
4501 DNPRINTF(SWM_D_MISC, "store_float_geom: win %#x, g: (%d,%d)"
4502 " %d x %d, g_float: (%d,%d) %d x %d\n", win->id, X(win), Y(win),
4503 WIDTH(win), HEIGHT(win), win->g_float.x, win->g_float.y,
4504 win->g_float.w, win->g_float.h);
4508 load_float_geom(struct ws_win *win)
4510 if (win == NULL || win->ws->r == NULL)
4513 if (win->g_floatvalid) {
4514 win->g = win->g_float;
4515 X(win) += X(win->ws->r);
4516 Y(win) += Y(win->ws->r);
4517 DNPRINTF(SWM_D_MISC, "load_float_geom: win %#x, g: (%d,%d)"
4518 "%d x %d\n", win->id, X(win), Y(win), WIDTH(win),
4521 DNPRINTF(SWM_D_MISC, "load_float_geom: win %#x, g_float "
4522 "is not set.\n", win->id);
4527 update_floater(struct ws_win *win)
4529 struct workspace *ws;
4530 struct swm_region *r;
4537 if ((r = ws->r) == NULL)
4540 DNPRINTF(SWM_D_MISC, "update_floater: win %#x\n", win->id);
4542 win->bordered = true;
4544 if (FULLSCREEN(win)) {
4545 /* _NET_WM_FULLSCREEN: fullscreen without border. */
4546 if (!win->g_floatvalid)
4547 store_float_geom(win);
4550 win->bordered = false;
4551 } else if (MAXIMIZED(win)) {
4552 /* Maximize: like a single stacked window. */
4553 if (!win->g_floatvalid)
4554 store_float_geom(win);
4558 if (bar_enabled && ws->bar_enabled) {
4560 Y(win) += bar_height;
4561 HEIGHT(win) -= bar_height;
4562 } else if (disable_border) {
4563 win->bordered = false;
4566 if (win->bordered) {
4567 HEIGHT(win) -= 2 * border_width;
4568 WIDTH(win) -= 2 * border_width;
4571 /* Normal floating window. */
4572 /* Update geometry if new region. */
4574 load_float_geom(win);
4576 if ((win->quirks & SWM_Q_FULLSCREEN) &&
4577 WIDTH(win) >= WIDTH(r) && HEIGHT(win) >= HEIGHT(r)) {
4578 /* Remove border for FULLSCREEN quirk. */
4579 win->bordered = false;
4580 } else if (!MANUAL(win)) {
4581 if (TRANS(win) && (win->quirks & SWM_Q_TRANSSZ)) {
4582 /* Adjust size on TRANSSZ quirk. */
4583 WIDTH(win) = (double)WIDTH(r) * dialog_ratio;
4584 HEIGHT(win) = (double)HEIGHT(r) * dialog_ratio;
4587 if (!(win->quirks & SWM_Q_ANYWHERE)) {
4589 * Floaters and transients are auto-centred
4590 * unless manually moved, resized or ANYWHERE
4593 X(win) = X(r) + (WIDTH(r) - WIDTH(win)) / 2 -
4595 Y(win) = Y(r) + (HEIGHT(r) - HEIGHT(win)) / 2 -
4597 store_float_geom(win);
4602 /* Ensure at least 1 pixel of the window is in the region. */
4603 region_containment(win, r, SWM_CW_ALLSIDES);
4609 * Send keystrokes to terminal to decrease/increase the font size as the
4610 * window size changes.
4613 adjust_font(struct ws_win *win)
4615 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
4616 ABOVE(win) || TRANS(win))
4619 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
4620 WIDTH(win) / win->sh.width_inc < term_width &&
4621 win->font_steps < SWM_MAX_FONT_STEPS) {
4622 win->font_size_boundary[win->font_steps] =
4623 (win->sh.width_inc * term_width) + win->sh.base_width;
4626 win->last_inc = win->sh.width_inc;
4627 fake_keypress(win, XK_KP_Subtract, XCB_MOD_MASK_SHIFT);
4628 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
4629 WIDTH(win) > win->font_size_boundary[win->font_steps - 1]) {
4632 win->last_inc = win->sh.width_inc;
4633 fake_keypress(win, XK_KP_Add, XCB_MOD_MASK_SHIFT);
4637 #define SWAPXY(g) do { \
4639 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
4640 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
4643 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, bool flip)
4645 struct swm_geometry win_g, r_g = *g;
4647 int i, j, s, stacks;
4648 int w_inc = 1, h_inc, w_base = 1, h_base;
4649 int hrh, extra = 0, h_slice, last_h = 0;
4650 int split, colno, winno, mwin, msize, mscale;
4651 int remain, missing, v_slice;
4652 bool bordered = true, reconfigure = false;
4654 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d, rot: %s, "
4655 "flip: %s\n", ws->idx, YESNO(rot), YESNO(flip));
4657 /* Prepare tiling variables, if needed. */
4658 if ((winno = count_win(ws, false)) > 0) {
4659 /* Find first tiled window. */
4660 TAILQ_FOREACH(win, &ws->winlist, entry)
4661 if (!FLOATING(win) && !ICONIC(win))
4664 /* Take into account size hints of first tiled window. */
4666 w_inc = win->sh.width_inc;
4667 w_base = win->sh.base_width;
4668 mwin = ws->l_state.horizontal_mwin;
4669 mscale = ws->l_state.horizontal_msize;
4670 stacks = ws->l_state.horizontal_stacks;
4673 w_inc = win->sh.height_inc;
4674 w_base = win->sh.base_height;
4675 mwin = ws->l_state.vertical_mwin;
4676 mscale = ws->l_state.vertical_msize;
4677 stacks = ws->l_state.vertical_stacks;
4681 if (stacks > winno - mwin)
4682 stacks = winno - mwin;
4686 h_slice = r_g.h / SWM_H_SLICE;
4687 if (mwin && winno > mwin) {
4688 v_slice = r_g.w / SWM_V_SLICE;
4692 win_g.w = v_slice * mscale;
4694 if (w_inc > 1 && w_inc < v_slice) {
4695 /* Adjust for requested size increment. */
4696 remain = (win_g.w - w_base) % w_inc;
4702 win_g.x += r_g.w - msize;
4705 colno = split = winno / stacks;
4706 win_g.w = ((r_g.w - (stacks * 2 * border_width) +
4707 2 * border_width) / stacks);
4709 hrh = r_g.h / colno;
4710 extra = r_g.h - (colno * hrh);
4711 win_g.h = hrh - 2 * border_width;
4712 i = j = 0, s = stacks;
4715 /* Update window geometry. */
4716 TAILQ_FOREACH(win, &ws->winlist, entry) {
4720 if (FLOATING(win)) {
4721 update_floater(win);
4726 if (split && i == split) {
4727 colno = (winno - mwin) / stacks;
4728 if (s <= (winno - mwin) % stacks)
4731 hrh = r_g.h / colno;
4732 extra = r_g.h - (colno * hrh);
4735 win_g.x += win_g.w + 2 * border_width +
4738 win_g.w = (r_g.w - msize -
4739 (stacks * (2 * border_width + tile_gap))) / stacks;
4741 win_g.w += (r_g.w - msize -
4742 (stacks * (2 * border_width + tile_gap))) %
4746 win_g.x -= win_g.w + 2 * border_width +
4752 win_g.h = hrh - 2 * border_width - tile_gap;
4755 h_inc = win->sh.width_inc;
4756 h_base = win->sh.base_width;
4758 h_inc = win->sh.height_inc;
4759 h_base = win->sh.base_height;
4762 if (j == colno - 1) {
4763 win_g.h = hrh + extra;
4764 } else if (h_inc > 1 && h_inc < h_slice) {
4765 /* adjust for window's requested size increment */
4766 remain = (win_g.h - h_base) % h_inc;
4767 missing = h_inc - remain;
4769 if (missing <= extra || j == 0) {
4781 win_g.y += last_h + 2 * border_width + tile_gap;
4783 if (disable_border && !(bar_enabled && ws->bar_enabled) &&
4786 win_g.w += 2 * border_width;
4787 win_g.h += 2 * border_width;
4793 if (X(win) != win_g.y || Y(win) != win_g.x ||
4794 WIDTH(win) != win_g.h || HEIGHT(win) != win_g.w) {
4798 WIDTH(win) = win_g.h;
4799 HEIGHT(win) = win_g.w;
4802 if (X(win) != win_g.x || Y(win) != win_g.y ||
4803 WIDTH(win) != win_g.w || HEIGHT(win) != win_g.h) {
4807 WIDTH(win) = win_g.w;
4808 HEIGHT(win) = win_g.h;
4812 if (bordered != win->bordered) {
4814 win->bordered = bordered;
4827 /* Stack all windows from bottom up. */
4828 TAILQ_FOREACH_REVERSE(win, &ws->stack, ws_win_stack, stack_entry)
4830 update_win_stacking(win);
4832 /* Map all windows from top down. */
4833 TAILQ_FOREACH(win, &ws->stack, stack_entry)
4837 DNPRINTF(SWM_D_STACK, "stack_master: done\n");
4841 vertical_config(struct workspace *ws, int id)
4843 DNPRINTF(SWM_D_STACK, "vertical_config: id: %d, workspace: %d\n",
4847 case SWM_ARG_ID_STACKRESET:
4848 case SWM_ARG_ID_STACKINIT:
4849 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
4850 ws->l_state.vertical_mwin = 1;
4851 ws->l_state.vertical_stacks = 1;
4853 case SWM_ARG_ID_MASTERSHRINK:
4854 if (ws->l_state.vertical_msize > 1)
4855 ws->l_state.vertical_msize--;
4857 case SWM_ARG_ID_MASTERGROW:
4858 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
4859 ws->l_state.vertical_msize++;
4861 case SWM_ARG_ID_MASTERADD:
4862 ws->l_state.vertical_mwin++;
4864 case SWM_ARG_ID_MASTERDEL:
4865 if (ws->l_state.vertical_mwin > 0)
4866 ws->l_state.vertical_mwin--;
4868 case SWM_ARG_ID_STACKINC:
4869 ws->l_state.vertical_stacks++;
4871 case SWM_ARG_ID_STACKDEC:
4872 if (ws->l_state.vertical_stacks > 1)
4873 ws->l_state.vertical_stacks--;
4875 case SWM_ARG_ID_FLIPLAYOUT:
4876 ws->l_state.vertical_flip = !ws->l_state.vertical_flip;
4884 vertical_stack(struct workspace *ws, struct swm_geometry *g)
4886 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
4888 stack_master(ws, g, 0, ws->l_state.vertical_flip);
4892 horizontal_config(struct workspace *ws, int id)
4894 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
4897 case SWM_ARG_ID_STACKRESET:
4898 case SWM_ARG_ID_STACKINIT:
4899 ws->l_state.horizontal_mwin = 1;
4900 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
4901 ws->l_state.horizontal_stacks = 1;
4903 case SWM_ARG_ID_MASTERSHRINK:
4904 if (ws->l_state.horizontal_msize > 1)
4905 ws->l_state.horizontal_msize--;
4907 case SWM_ARG_ID_MASTERGROW:
4908 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
4909 ws->l_state.horizontal_msize++;
4911 case SWM_ARG_ID_MASTERADD:
4912 ws->l_state.horizontal_mwin++;
4914 case SWM_ARG_ID_MASTERDEL:
4915 if (ws->l_state.horizontal_mwin > 0)
4916 ws->l_state.horizontal_mwin--;
4918 case SWM_ARG_ID_STACKINC:
4919 ws->l_state.horizontal_stacks++;
4921 case SWM_ARG_ID_STACKDEC:
4922 if (ws->l_state.horizontal_stacks > 1)
4923 ws->l_state.horizontal_stacks--;
4925 case SWM_ARG_ID_FLIPLAYOUT:
4926 ws->l_state.horizontal_flip = !ws->l_state.horizontal_flip;
4934 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
4936 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
4938 stack_master(ws, g, 1, ws->l_state.horizontal_flip);
4941 /* fullscreen view */
4943 max_stack(struct workspace *ws, struct swm_geometry *g)
4945 struct swm_geometry gg = *g;
4946 struct ws_win *w, *win = NULL, *parent = NULL, *tmpw;
4949 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
4954 winno = count_win(ws, false);
4955 if (winno == 0 && count_win(ws, true) == 0)
4958 /* Figure out which top level window should be visible. */
4959 if (ws->focus_pending)
4960 win = ws->focus_pending;
4963 else if (ws->focus_prev)
4964 win = ws->focus_prev;
4966 win = TAILQ_FIRST(&ws->winlist);
4968 DNPRINTF(SWM_D_STACK, "max_stack: focus_pending: %#x, focus: %#x, "
4969 "focus_prev: %#x, first: %#x, win: %#x\n", WINID(ws->focus_pending),
4970 WINID(ws->focus), WINID(ws->focus_prev),
4971 WINID(TAILQ_FIRST(&ws->winlist)), win->id);
4973 /* Update window geometry. */
4974 TAILQ_FOREACH(w, &ws->winlist, entry) {
4983 /* Set maximized flag for all maxed windows. */
4984 if (!MAXIMIZED(w)) {
4985 /* Preserve floating geometry. */
4987 store_float_geom(w);
4989 ewmh_apply_flags(w, w->ewmh_flags | EWMH_F_MAXIMIZED);
4990 ewmh_update_wm_state(w);
4993 /* Only reconfigure if necessary. */
4994 if (X(w) != gg.x || Y(w) != gg.y || WIDTH(w) != gg.w ||
4995 HEIGHT(w) != gg.h) {
4997 if (bar_enabled && ws->bar_enabled){
5000 w->bordered = false;
5001 WIDTH(w) += 2 * border_width;
5002 HEIGHT(w) += 2 * border_width;
5009 /* If transient, stack parent and its children. */
5010 if (TRANS(win) && (parent = find_window(win->transient))) {
5011 raise_window(parent);
5013 TAILQ_FOREACH_SAFE(w, &ws->stack, stack_entry, tmpw)
5014 if (w->transient == parent->id)
5018 /* Make sure focus window is on top. */
5021 /* Stack any children of focus window. */
5022 TAILQ_FOREACH_SAFE(w, &ws->stack, stack_entry, tmpw)
5023 if (w->transient == win->id)
5026 /* Map all windows. */
5027 TAILQ_FOREACH(w, &ws->stack, stack_entry)
5033 send_to_rg(struct swm_region *r, union arg *args)
5035 int ridx = args->id, i, num_screens;
5036 struct swm_region *rr = NULL;
5039 num_screens = get_screen_count();
5040 /* do nothing if we don't have more than one screen */
5041 if (!(num_screens > 1 || outputs > 1))
5044 DNPRINTF(SWM_D_FOCUS, "send_to_rg: id: %d\n", ridx);
5046 rr = TAILQ_FIRST(&r->s->rl);
5047 for (i = 0; i < ridx && rr != NULL; ++i)
5048 rr = TAILQ_NEXT(rr, entry);
5059 region_under(struct swm_screen *s, int x, int y)
5061 struct swm_region *r = NULL;
5063 TAILQ_FOREACH(r, &s->rl, entry) {
5064 DNPRINTF(SWM_D_MISC, "region_under: ws: %d, region g: (%d,%d) "
5065 "%d x %d, coords: (%d,%d)\n", r->ws->idx, X(r), Y(r),
5066 WIDTH(r), HEIGHT(r), x, y);
5067 if (X(r) <= x && x < MAX_X(r))
5068 if (Y(r) <= y && y < MAX_Y(r))
5075 /* Transfer focused window to target workspace and focus. */
5077 send_to_ws(struct swm_region *r, union arg *args)
5079 int wsid = args->id;
5080 struct ws_win *win = NULL;
5082 if (r && r->ws && r->ws->focus)
5087 DNPRINTF(SWM_D_MOVE, "send_to_ws: win %#x, ws %d\n", win->id, wsid);
5089 if (wsid < 0 || wsid >= workspace_limit)
5092 if (win->ws->idx == wsid)
5095 win_to_ws(win, wsid, true);
5097 /* Set new focus on target ws. */
5098 if (focus_mode != SWM_FOCUS_FOLLOW) {
5099 win->ws->focus_prev = win->ws->focus;
5100 win->ws->focus = win;
5101 win->ws->focus_pending = NULL;
5103 if (win->ws->focus_prev)
5104 update_window_color(win->ws->focus_prev);
5107 DNPRINTF(SWM_D_STACK, "send_to_ws: focus_pending: %#x, focus: %#x, "
5108 "focus_prev: %#x, first: %#x, win: %#x\n",
5109 WINID(r->ws->focus_pending), WINID(r->ws->focus),
5110 WINID(r->ws->focus_prev), WINID(TAILQ_FIRST(&r->ws->winlist)),
5113 ewmh_apply_flags(win, win->ewmh_flags & ~EWMH_F_MAXIMIZED);
5114 ewmh_update_wm_state(win);
5116 /* Restack and set new focus on current ws. */
5118 load_float_geom(win);
5122 if (focus_mode != SWM_FOCUS_FOLLOW) {
5123 if (r->ws->focus != NULL) {
5124 focus_win(r->ws->focus);
5126 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT, r->id,
5136 win_to_ws(struct ws_win *win, int wsid, bool unfocus)
5138 struct ws_win *parent;
5139 struct workspace *ws, *nws, *pws;
5141 if (wsid < 0 || wsid >= workspace_limit)
5144 if (win->ws->idx == wsid)
5148 nws = &win->s->ws[wsid];
5150 DNPRINTF(SWM_D_MOVE, "win_to_ws: win %#x, ws %d -> %d\n", win->id,
5153 /* Cleanup focus on source ws. */
5154 if (focus_mode != SWM_FOCUS_FOLLOW &&
5155 (ws->focus == win || ws->focus_pending == win))
5156 ws->focus_pending = get_focus_prev(win);
5158 /* Move the parent if this is a transient window. */
5160 parent = find_window(win->transient);
5163 /* Set new focus in parent's ws if needed. */
5164 if (pws->focus == parent) {
5165 if (focus_mode != SWM_FOCUS_FOLLOW)
5166 pws->focus_pending =
5167 get_focus_prev(parent);
5169 unfocus_win(parent);
5171 if (focus_mode != SWM_FOCUS_FOLLOW) {
5172 pws->focus = pws->focus_pending;
5173 pws->focus_pending = NULL;
5177 /* Don't unmap parent if new ws is visible */
5179 unmap_window(parent);
5182 TAILQ_REMOVE(&ws->winlist, parent, entry);
5183 TAILQ_REMOVE(&ws->stack, parent, stack_entry);
5184 TAILQ_INSERT_TAIL(&nws->winlist, parent, entry);
5185 TAILQ_INSERT_TAIL(&nws->stack, parent, stack_entry);
5188 DNPRINTF(SWM_D_PROP, "win_to_ws: set property: "
5189 "_NET_WM_DESKTOP: %d\n", wsid);
5190 xcb_change_property(conn, XCB_PROP_MODE_REPLACE,
5191 parent->id, ewmh[_NET_WM_DESKTOP].atom,
5192 XCB_ATOM_CARDINAL, 32, 1, &wsid);
5199 if (ws->focus_prev == win)
5200 ws->focus_prev = NULL;
5202 if (focus_mode != SWM_FOCUS_FOLLOW && ws->focus_pending != NULL) {
5203 ws->focus = ws->focus_pending;
5204 ws->focus_pending = NULL;
5207 /* Don't unmap if new ws is visible */
5212 TAILQ_REMOVE(&ws->winlist, win, entry);
5213 TAILQ_REMOVE(&ws->stack, win, stack_entry);
5214 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
5215 TAILQ_INSERT_TAIL(&nws->stack, win, stack_entry);
5218 /* Update the window's workspace property: _NET_WM_DESKTOP */
5219 DNPRINTF(SWM_D_PROP, "win_to_ws: set property: "
5220 "_NET_WM_DESKTOP: %d\n", wsid);
5221 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
5222 ewmh[_NET_WM_DESKTOP].atom, XCB_ATOM_CARDINAL, 32, 1, &wsid);
5224 ewmh_update_client_list();
5226 DNPRINTF(SWM_D_MOVE, "win_to_ws: done.\n");
5230 pressbutton(struct swm_region *r, union arg *args)
5232 /* suppress unused warning since var is needed */
5235 xcb_test_fake_input(conn, XCB_BUTTON_PRESS, args->id,
5236 XCB_CURRENT_TIME, XCB_WINDOW_NONE, 0, 0, 0);
5237 xcb_test_fake_input(conn, XCB_BUTTON_RELEASE, args->id,
5238 XCB_CURRENT_TIME, XCB_WINDOW_NONE, 0, 0, 0);
5242 raise_toggle(struct swm_region *r, union arg *args)
5244 /* suppress unused warning since var is needed */
5247 if (r == NULL || r->ws == NULL)
5250 if (r->ws->focus && MAXIMIZED(r->ws->focus))
5253 r->ws->always_raise = !r->ws->always_raise;
5255 /* bring floaters back to top */
5256 if (!r->ws->always_raise)
5263 iconify(struct swm_region *r, union arg *args)
5266 /* suppress unused warning since var is needed */
5269 if ((w = r->ws->focus) == NULL)
5272 ewmh_apply_flags(w, w->ewmh_flags | EWMH_F_HIDDEN);
5273 ewmh_update_wm_state(w);
5281 get_win_name(xcb_window_t win)
5284 xcb_get_property_cookie_t c;
5285 xcb_get_property_reply_t *r;
5287 /* First try _NET_WM_NAME for UTF-8. */
5288 c = xcb_get_property(conn, 0, win, ewmh[_NET_WM_NAME].atom,
5289 XCB_GET_PROPERTY_TYPE_ANY, 0, UINT_MAX);
5290 r = xcb_get_property_reply(conn, c, NULL);
5291 if (r && r->type == XCB_NONE) {
5293 /* Use WM_NAME instead; no UTF-8. */
5294 c = xcb_get_property(conn, 0, win, XCB_ATOM_WM_NAME,
5295 XCB_GET_PROPERTY_TYPE_ANY, 0, UINT_MAX);
5296 r = xcb_get_property_reply(conn, c, NULL);
5299 if (r && r->type != XCB_NONE && r->length > 0)
5300 name = strndup(xcb_get_property_value(r),
5301 xcb_get_property_value_length(r));
5306 err(1, "get_win_name: failed to allocate memory.");
5314 uniconify(struct swm_region *r, union arg *args)
5321 DNPRINTF(SWM_D_MISC, "uniconify\n");
5323 if (r == NULL || r->ws == NULL)
5326 /* make sure we have anything to uniconify */
5327 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
5328 if (win->ws == NULL)
5329 continue; /* should never happen */
5335 DNPRINTF(SWM_D_MISC, "uniconify: count: %d\n", count);
5341 search_resp_action = SWM_SEARCH_UNICONIFY;
5343 spawn_select(r, args, "search", &searchpid);
5345 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
5348 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
5349 if (win->ws == NULL)
5350 continue; /* should never happen */
5354 name = get_win_name(win->id);
5355 fprintf(lfile, "%s.%u\n", name, win->id);
5363 name_workspace(struct swm_region *r, union arg *args)
5367 DNPRINTF(SWM_D_MISC, "name_workspace\n");
5373 search_resp_action = SWM_SEARCH_NAME_WORKSPACE;
5375 spawn_select(r, args, "name_workspace", &searchpid);
5377 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
5380 fprintf(lfile, "%s", "");
5385 search_workspace(struct swm_region *r, union arg *args)
5388 struct workspace *ws;
5391 DNPRINTF(SWM_D_MISC, "search_workspace\n");
5397 search_resp_action = SWM_SEARCH_SEARCH_WORKSPACE;
5399 spawn_select(r, args, "search", &searchpid);
5401 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
5404 for (i = 0; i < workspace_limit; i++) {
5408 fprintf(lfile, "%d%s%s\n", ws->idx + 1,
5409 (ws->name ? ":" : ""), (ws->name ? ws->name : ""));
5416 search_win_cleanup(void)
5418 struct search_window *sw = NULL;
5420 while ((sw = TAILQ_FIRST(&search_wl)) != NULL) {
5421 xcb_destroy_window(conn, sw->indicator);
5422 TAILQ_REMOVE(&search_wl, sw, entry);
5428 search_win(struct swm_region *r, union arg *args)
5430 struct ws_win *win = NULL;
5431 struct search_window *sw = NULL;
5434 xcb_screen_t *screen;
5435 int i, width, height;
5443 XRectangle l_ibox, l_lbox;
5445 DNPRINTF(SWM_D_MISC, "search_win\n");
5448 search_resp_action = SWM_SEARCH_SEARCH_WINDOW;
5450 spawn_select(r, args, "search", &searchpid);
5452 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
5455 if ((screen = get_screen(r->s->idx)) == NULL)
5456 errx(1, "ERROR: can't get screen %d.", r->s->idx);
5458 TAILQ_INIT(&search_wl);
5461 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
5465 sw = calloc(1, sizeof(struct search_window));
5467 warn("search_win: calloc");
5469 search_win_cleanup();
5475 snprintf(s, sizeof s, "%d", i);
5478 w = xcb_generate_id(conn);
5479 wa[0] = r->s->c[SWM_S_COLOR_FOCUS].pixel;
5480 wa[1] = r->s->c[SWM_S_COLOR_UNFOCUS].pixel;
5481 wa[2] = screen->default_colormap;
5483 if (bar_font_legacy) {
5484 XmbTextExtents(bar_fs, s, len, &l_ibox, &l_lbox);
5485 width = l_lbox.width + 4;
5486 height = bar_fs_extents->max_logical_extent.height + 4;
5488 XftTextExtentsUtf8(display, bar_font, (FcChar8 *)s, len,
5490 width = info.width + 4;
5491 height = bar_font->height + 4;
5494 xcb_create_window(conn, screen->root_depth, w, win->id, 0, 0,
5495 width, height, 1, XCB_WINDOW_CLASS_INPUT_OUTPUT,
5496 screen->root_visual, XCB_CW_BACK_PIXEL |
5497 XCB_CW_BORDER_PIXEL | XCB_CW_COLORMAP, wa);
5499 xcb_map_window(conn, w);
5502 TAILQ_INSERT_TAIL(&search_wl, sw, entry);
5504 if (bar_font_legacy) {
5505 l_gcv.graphics_exposures = 0;
5506 l_draw = XCreateGC(display, w, 0, &l_gcv);
5508 XSetForeground(display, l_draw,
5509 r->s->c[SWM_S_COLOR_BAR].pixel);
5511 DRAWSTRING(display, w, bar_fs, l_draw, 2,
5512 (bar_fs_extents->max_logical_extent.height -
5513 l_lbox.height) / 2 - l_lbox.y, s, len);
5515 XFreeGC(display, l_draw);
5518 draw = XftDrawCreate(display, w,
5519 DefaultVisual(display, r->s->idx),
5520 DefaultColormap(display, r->s->idx));
5522 XftDrawStringUtf8(draw, &search_font_color, bar_font, 2,
5523 (HEIGHT(r->bar) + bar_font->height) / 2 -
5524 bar_font->descent, (FcChar8 *)s, len);
5526 XftDrawDestroy(draw);
5529 DNPRINTF(SWM_D_MISC, "search_win: mapped win %#x\n", w);
5531 fprintf(lfile, "%d\n", i);
5541 search_resp_uniconify(const char *resp, size_t len)
5547 DNPRINTF(SWM_D_MISC, "search_resp_uniconify: resp: %s\n", resp);
5549 TAILQ_FOREACH(win, &search_r->ws->winlist, entry) {
5552 name = get_win_name(win->id);
5553 if (asprintf(&s, "%s.%u", name, win->id) == -1) {
5558 if (strncmp(s, resp, len) == 0) {
5559 /* XXX this should be a callback to generalize */
5560 ewmh_apply_flags(win, win->ewmh_flags & ~EWMH_F_HIDDEN);
5561 ewmh_update_wm_state(win);
5571 search_resp_name_workspace(const char *resp, size_t len)
5573 struct workspace *ws;
5575 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: resp: %s\n", resp);
5577 if (search_r->ws == NULL)
5582 free(search_r->ws->name);
5583 search_r->ws->name = NULL;
5587 ws->name = strdup(resp);
5588 if (ws->name == NULL) {
5589 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: "
5590 "strdup: %s", strerror(errno));
5594 ewmh_update_desktop_names();
5595 ewmh_get_desktop_names();
5600 ewmh_update_desktop_names(void)
5602 char *name_list = NULL, *p;
5603 int num_screens, i, j, len = 0, tot = 0;
5605 num_screens = get_screen_count();
5606 for (i = 0; i < num_screens; ++i) {
5607 for (j = 0; j < workspace_limit; ++j) {
5608 if (screens[i].ws[j].name != NULL)
5609 len += strlen(screens[i].ws[j].name);
5613 if((name_list = calloc(len, sizeof(char))) == NULL)
5614 err(1, "update_desktop_names: calloc: failed to "
5615 "allocate memory.");
5618 for (j = 0; j < workspace_limit; ++j) {
5619 if (screens[i].ws[j].name != NULL) {
5620 len = strlen(screens[i].ws[j].name);
5621 memcpy(p, screens[i].ws[j].name, len);
5630 xcb_change_property(conn, XCB_PROP_MODE_REPLACE,
5631 screens[i].root, ewmh[_NET_DESKTOP_NAMES].atom,
5632 a_utf8_string, 8, tot, name_list);
5642 ewmh_get_desktop_names(void)
5645 xcb_get_property_cookie_t c;
5646 xcb_get_property_reply_t *r;
5647 int num_screens, i, j, n, k;
5649 num_screens = get_screen_count();
5650 for (i = 0; i < num_screens; ++i) {
5651 for (j = 0; j < workspace_limit; ++j) {
5652 free(screens[i].ws[j].name);
5653 screens[i].ws[j].name = NULL;
5656 c = xcb_get_property(conn, 0, screens[i].root,
5657 ewmh[_NET_DESKTOP_NAMES].atom,
5658 a_utf8_string, 0, UINT32_MAX);
5659 r = xcb_get_property_reply(conn, c, NULL);
5663 names = xcb_get_property_value(r);
5664 n = xcb_get_property_value_length(r);
5666 for (j = 0, k = 0; j < n; ++j) {
5667 if (*(names + j) != '\0') {
5668 screens[i].ws[k].name = strdup(names + j);
5669 j += strlen(names + j);
5678 ewmh_update_client_list(void)
5681 int num_screens, i, j, k = 0, count = 0;
5684 num_screens = get_screen_count();
5685 for (i = 0; i < num_screens; ++i) {
5686 for (j = 0; j < workspace_limit; ++j)
5687 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
5690 DNPRINTF(SWM_D_PROP, "ewmh_update_client_list: win count: %d\n",
5696 wins = calloc(count, sizeof(xcb_window_t));
5698 err(1, "ewmh_update_client_list: calloc: failed to "
5699 "allocate memory.");
5701 for (j = 0, k = 0; j < workspace_limit; ++j)
5702 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
5703 wins[k++] = win->id;
5705 xcb_change_property(conn, XCB_PROP_MODE_REPLACE,
5706 screens[i].root, ewmh[_NET_CLIENT_LIST].atom,
5707 XCB_ATOM_WINDOW, 32, count, wins);
5714 ewmh_update_current_desktop(void)
5718 num_screens = get_screen_count();
5719 for (i = 0; i < num_screens; ++i)
5720 xcb_change_property(conn, XCB_PROP_MODE_REPLACE,
5721 screens[i].root, ewmh[_NET_CURRENT_DESKTOP].atom,
5722 XCB_ATOM_CARDINAL, 32, 1, &screens[i].r_focus->ws->idx);
5726 ewmh_update_desktops(void)
5728 int num_screens, i, j;
5731 vals = calloc(workspace_limit * 2, sizeof(uint32_t));
5733 err(1, "ewmh_update_desktops: calloc: failed to allocate "
5736 num_screens = get_screen_count();
5737 for (i = 0; i < num_screens; i++) {
5738 xcb_change_property(conn, XCB_PROP_MODE_REPLACE,
5739 screens[i].root, ewmh[_NET_NUMBER_OF_DESKTOPS].atom,
5740 XCB_ATOM_CARDINAL, 32, 1, &workspace_limit);
5742 for (j = 0; j < workspace_limit; ++j) {
5743 if (screens[i].ws[j].r != NULL) {
5744 vals[j * 2] = X(screens[i].ws[j].r);
5745 vals[j * 2 + 1] = Y(screens[i].ws[j].r);
5746 } else if (screens[i].ws[j].old_r != NULL) {
5747 vals[j * 2] = X(screens[i].ws[j].old_r);
5748 vals[j * 2 + 1] = Y(screens[i].ws[j].old_r);
5750 vals[j * 2] = vals[j * 2 + 1] = 0;
5754 xcb_change_property(conn, XCB_PROP_MODE_REPLACE,
5755 screens[i].root, ewmh[_NET_DESKTOP_VIEWPORT].atom,
5756 XCB_ATOM_CARDINAL, 32, workspace_limit * 2, vals);
5763 search_resp_search_workspace(const char *resp)
5770 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: resp: %s\n", resp);
5774 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: strdup: %s",
5781 ws_idx = (int)strtonum(q, 1, workspace_limit, &errstr);
5783 DNPRINTF(SWM_D_MISC, "workspace idx is %s: %s",
5790 switchws(search_r, &a);
5794 search_resp_search_window(const char *resp)
5799 struct search_window *sw;
5801 DNPRINTF(SWM_D_MISC, "search_resp_search_window: resp: %s\n", resp);
5805 DNPRINTF(SWM_D_MISC, "search_resp_search_window: strdup: %s",
5810 idx = (int)strtonum(s, 1, INT_MAX, &errstr);
5812 DNPRINTF(SWM_D_MISC, "window idx is %s: %s",
5819 TAILQ_FOREACH(sw, &search_wl, entry)
5820 if (idx == sw->idx) {
5826 #define MAX_RESP_LEN 1024
5829 search_do_resp(void)
5835 DNPRINTF(SWM_D_MISC, "search_do_resp:\n");
5840 if ((resp = calloc(1, MAX_RESP_LEN + 1)) == NULL) {
5841 warn("search: calloc");
5845 rbytes = read(select_resp_pipe[0], resp, MAX_RESP_LEN);
5847 warn("search: read error");
5850 resp[rbytes] = '\0';
5853 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
5854 * newline, so work around that by sanitizing the resp now.
5856 resp[strcspn(resp, "\n")] = '\0';
5859 switch (search_resp_action) {
5860 case SWM_SEARCH_UNICONIFY:
5861 search_resp_uniconify(resp, len);
5863 case SWM_SEARCH_NAME_WORKSPACE:
5864 search_resp_name_workspace(resp, len);
5866 case SWM_SEARCH_SEARCH_WORKSPACE:
5867 search_resp_search_workspace(resp);
5869 case SWM_SEARCH_SEARCH_WINDOW:
5870 search_resp_search_window(resp);
5875 if (search_resp_action == SWM_SEARCH_SEARCH_WINDOW)
5876 search_win_cleanup();
5878 search_resp_action = SWM_SEARCH_NONE;
5879 close(select_resp_pipe[0]);
5886 wkill(struct swm_region *r, union arg *args)
5888 DNPRINTF(SWM_D_MISC, "wkill: win %#x, id: %d\n", WINID(r->ws->focus),
5891 if (r->ws->focus == NULL)
5894 if (args->id == SWM_ARG_ID_KILLWINDOW)
5895 xcb_kill_client(conn, r->ws->focus->id);
5897 if (r->ws->focus->can_delete)
5898 client_msg(r->ws->focus, a_delete, 0);
5904 clear_maximized(struct workspace *ws)
5909 /* Clear any maximized win(s) on ws, from bottom up. */
5910 TAILQ_FOREACH_REVERSE(w, &ws->stack, ws_win_stack, stack_entry)
5912 ewmh_apply_flags(w, w->ewmh_flags & ~EWMH_F_MAXIMIZED);
5913 ewmh_update_wm_state(w);
5921 maximize_toggle(struct swm_region *r, union arg *args)
5923 struct ws_win *w = r->ws->focus;
5925 /* suppress unused warning since var is needed */
5931 DNPRINTF(SWM_D_MISC, "maximize_toggle: win %#x\n", w->id);
5936 if (w->ws->cur_layout == &layouts[SWM_MAX_STACK])
5939 ewmh_apply_flags(w, w->ewmh_flags ^ EWMH_F_MAXIMIZED);
5940 ewmh_update_wm_state(w);
5944 if (w == w->ws->focus)
5948 DNPRINTF(SWM_D_MISC, "maximize_toggle: done\n");
5952 floating_toggle(struct swm_region *r, union arg *args)
5954 struct ws_win *w = r->ws->focus;
5956 /* suppress unused warning since var is needed */
5962 DNPRINTF(SWM_D_MISC, "floating_toggle: win %#x\n", w->id);
5964 if (FULLSCREEN(w) || TRANS(w))
5967 if (w->ws->cur_layout == &layouts[SWM_MAX_STACK])
5970 ewmh_apply_flags(w, w->ewmh_flags ^ EWMH_F_ABOVE);
5971 ewmh_update_wm_state(w);
5975 if (w == w->ws->focus)
5979 DNPRINTF(SWM_D_MISC, "floating_toggle: done\n");
5983 region_containment(struct ws_win *win, struct swm_region *r, int opts)
5985 struct swm_geometry g = r->g;
5986 int rt, lt, tp, bm, bw;
5988 bw = (opts & SWM_CW_SOFTBOUNDARY) ? boundary_width : 0;
5991 * Perpendicular distance of each side of the window to the respective
5992 * side of the region boundary. Positive values indicate the side of
5993 * the window has passed beyond the region boundary.
5995 rt = opts & SWM_CW_RIGHT ? MAX_X(win) + BORDER(win) - MAX_X(r) : bw;
5996 lt = opts & SWM_CW_LEFT ? X(r) - X(win) + BORDER(win) : bw;
5997 bm = opts & SWM_CW_BOTTOM ? MAX_Y(win) + BORDER(win) - MAX_Y(r) : bw;
5998 tp = opts & SWM_CW_TOP ? Y(r) - Y(win) + BORDER(win) : bw;
6000 DNPRINTF(SWM_D_MISC, "region_containment: win %#x, rt: %d, lt: %d, "
6001 "bm: %d, tp: %d, SOFTBOUNDARY: %s, HARDBOUNDARY: %s\n", win->id, rt,
6002 lt, bm, tp, YESNO(opts & SWM_CW_SOFTBOUNDARY),
6003 YESNO(opts & SWM_CW_HARDBOUNDARY));
6006 * Disable containment if any of the flagged sides went beyond the
6007 * containment boundary, or if containment is disabled.
6009 if (!(opts & SWM_CW_HARDBOUNDARY || opts & SWM_CW_SOFTBOUNDARY) ||
6010 (bw != 0 && ((rt > bw) || (lt > bw) || (bm > bw) || (tp > bw)))) {
6011 /* Make sure window has at least 1 pixel in the region */
6012 g.x += 1 - WIDTH(win);
6013 g.y += 1 - HEIGHT(win);
6014 g.w += 2 * WIDTH(win) - 2;
6015 g.h += 2 * HEIGHT(win) - 2;
6018 constrain_window(win, &g, &opts);
6021 /* Move or resize a window so that flagged side(s) fit into the supplied box. */
6023 constrain_window(struct ws_win *win, struct swm_geometry *b, int *opts)
6025 DNPRINTF(SWM_D_MISC, "constrain_window: win %#x, (x,y) w x h: "
6026 "(%d,%d) %d x %d, box: (x,y) w x h: (%d,%d) %d x %d, rt: %s, "
6027 "lt: %s, bt: %s, tp: %s, allow resize: %s\n", win->id, X(win),
6028 Y(win), WIDTH(win), HEIGHT(win), b->x, b->y, b->w, b->h,
6029 YESNO(*opts & SWM_CW_RIGHT), YESNO(*opts & SWM_CW_LEFT),
6030 YESNO(*opts & SWM_CW_BOTTOM), YESNO(*opts & SWM_CW_TOP),
6031 YESNO(*opts & SWM_CW_RESIZABLE));
6033 if ((*opts & SWM_CW_RIGHT) && MAX_X(win) + BORDER(win) > b->x + b->w) {
6034 if (*opts & SWM_CW_RESIZABLE)
6035 WIDTH(win) = b->x + b->w - X(win) - BORDER(win);
6037 X(win) = b->x + b->w - WIDTH(win) - BORDER(win);
6040 if ((*opts & SWM_CW_LEFT) && X(win) + BORDER(win) < b->x) {
6041 if (*opts & SWM_CW_RESIZABLE)
6042 WIDTH(win) -= b->x - X(win) - BORDER(win);
6044 X(win) = b->x - BORDER(win);
6047 if ((*opts & SWM_CW_BOTTOM) && MAX_Y(win) + BORDER(win) > b->y + b->h) {
6048 if (*opts & SWM_CW_RESIZABLE)
6049 HEIGHT(win) = b->y + b->h - Y(win) - BORDER(win);
6051 Y(win) = b->y + b->h - HEIGHT(win) - BORDER(win);
6054 if ((*opts & SWM_CW_TOP) && Y(win) + BORDER(win) < b->y) {
6055 if (*opts & SWM_CW_RESIZABLE)
6056 HEIGHT(win) -= b->y - Y(win) - BORDER(win);
6058 Y(win) = b->y - BORDER(win);
6061 if (*opts & SWM_CW_RESIZABLE) {
6064 if (HEIGHT(win) < 1)
6070 update_window_color(struct ws_win *win)
6074 if (WS_FOCUSED(win->ws) && win->ws->focus == win) {
6075 pixel = MAXIMIZED(win) ?
6076 &win->s->c[SWM_S_COLOR_FOCUS_MAXIMIZED].pixel :
6077 &win->s->c[SWM_S_COLOR_FOCUS].pixel;
6079 if (composite_enabled)
6080 set_opacity(win, opacity_focus * OPAQUE);
6082 pixel = MAXIMIZED(win) ?
6083 &win->s->c[SWM_S_COLOR_UNFOCUS_MAXIMIZED].pixel :
6084 &win->s->c[SWM_S_COLOR_UNFOCUS].pixel;
6086 if (composite_enabled)
6087 set_opacity(win, opacity_unfocus * OPAQUE);
6090 xcb_change_window_attributes(conn, win->id,
6091 XCB_CW_BORDER_PIXEL, pixel);
6095 update_window(struct ws_win *win)
6100 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
6101 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
6102 XCB_CONFIG_WINDOW_BORDER_WIDTH;
6106 wc[3] = HEIGHT(win);
6107 wc[4] = BORDER(win);
6109 DNPRINTF(SWM_D_EVENT, "update_window: win %#x, (x,y) w x h: "
6110 "(%d,%d) %d x %d, bordered: %s\n", win->id, wc[0], wc[1], wc[2],
6111 wc[3], YESNO(win->bordered));
6113 xcb_configure_window(conn, win->id, mask, wc);
6116 #define SWM_RESIZE_STEPS (50)
6119 resize(struct ws_win *win, union arg *args)
6121 xcb_timestamp_t timestamp = 0;
6122 struct swm_region *r = NULL;
6123 struct swm_geometry g;
6125 int top = 0, left = 0;
6127 xcb_cursor_t cursor;
6128 xcb_query_pointer_reply_t *xpr = NULL;
6129 xcb_generic_event_t *evt;
6130 xcb_motion_notify_event_t *mne;
6137 if (FULLSCREEN(win))
6140 /* In max_stack mode, should only resize transients. */
6141 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !TRANS(win))
6144 DNPRINTF(SWM_D_EVENT, "resize: win %#x, floating: %s, "
6145 "transient: %#x\n", win->id, YESNO(ABOVE(win)),
6149 store_float_geom(win);
6150 else if (!(TRANS(win) || ABOVE(win)))
6153 ewmh_apply_flags(win, (win->ewmh_flags | SWM_F_MANUAL | EWMH_F_ABOVE) &
6155 ewmh_update_wm_state(win);
6161 /* It's possible for win to have been freed during focus_flush(). */
6162 if (validate_win(win)) {
6163 DNPRINTF(SWM_D_EVENT, "resize: invalid win.\n");
6168 case SWM_ARG_ID_WIDTHSHRINK:
6169 WIDTH(win) -= SWM_RESIZE_STEPS;
6172 case SWM_ARG_ID_WIDTHGROW:
6173 WIDTH(win) += SWM_RESIZE_STEPS;
6176 case SWM_ARG_ID_HEIGHTSHRINK:
6177 HEIGHT(win) -= SWM_RESIZE_STEPS;
6180 case SWM_ARG_ID_HEIGHTGROW:
6181 HEIGHT(win) += SWM_RESIZE_STEPS;
6188 region_containment(win, r, SWM_CW_ALLSIDES | SWM_CW_RESIZABLE |
6189 SWM_CW_HARDBOUNDARY);
6191 store_float_geom(win);
6195 region_containment(win, r, SWM_CW_ALLSIDES | SWM_CW_RESIZABLE |
6196 SWM_CW_SOFTBOUNDARY);
6199 /* get cursor offset from window root */
6200 xpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, win->id),
6207 if (xpr->win_x < WIDTH(win) / 2)
6210 if (xpr->win_y < HEIGHT(win) / 2)
6213 if (args->id == SWM_ARG_ID_CENTER)
6214 cursor = cursors[XC_SIZING].cid;
6216 cursor = cursors[left ? XC_TOP_LEFT_CORNER :
6217 XC_TOP_RIGHT_CORNER].cid;
6219 cursor = cursors[left ? XC_BOTTOM_LEFT_CORNER :
6220 XC_BOTTOM_RIGHT_CORNER].cid;
6222 xcb_grab_pointer(conn, 0, win->id, MOUSEMASK,
6223 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, XCB_WINDOW_NONE, cursor,
6228 while (resizing && (evt = xcb_wait_for_event(conn))) {
6229 switch (XCB_EVENT_RESPONSE_TYPE(evt)) {
6230 case XCB_BUTTON_RELEASE:
6231 DNPRINTF(SWM_D_EVENT, "resize: BUTTON_RELEASE\n");
6234 case XCB_MOTION_NOTIFY:
6235 mne = (xcb_motion_notify_event_t *)evt;
6236 /* cursor offset/delta from start of the operation */
6237 dx = mne->root_x - xpr->root_x;
6238 dy = mne->root_y - xpr->root_y;
6244 if (args->id == SWM_ARG_ID_CENTER) {
6245 if (g.h / 2 + dy < 1)
6249 HEIGHT(win) = g.h + 2 * dy;
6257 HEIGHT(win) = g.h + dy;
6264 if (args->id == SWM_ARG_ID_CENTER) {
6265 if (g.w / 2 + dx < 1)
6269 WIDTH(win) = g.w + 2 * dx;
6277 WIDTH(win) = g.w + dx;
6280 /* not free, don't sync more than 120 times / second */
6281 if ((mne->time - timestamp) > (1000 / 120) ) {
6282 timestamp = mne->time;
6283 regionize(win, mne->root_x, mne->root_y);
6284 region_containment(win, r, SWM_CW_ALLSIDES |
6285 SWM_CW_RESIZABLE | SWM_CW_HARDBOUNDARY |
6286 SWM_CW_SOFTBOUNDARY);
6293 xcb_allow_events(conn, XCB_ALLOW_ASYNC_KEYBOARD,
6294 ((xcb_key_press_event_t *)evt)->time);
6300 /* It's possible for win to have been freed above. */
6301 if (validate_win(win)) {
6302 DNPRINTF(SWM_D_EVENT, "resize: invalid win.\n");
6310 region_containment(win, r, SWM_CW_ALLSIDES | SWM_CW_RESIZABLE |
6311 SWM_CW_HARDBOUNDARY | SWM_CW_SOFTBOUNDARY);
6315 store_float_geom(win);
6317 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
6319 DNPRINTF(SWM_D_EVENT, "resize: done.\n");
6323 resize_step(struct swm_region *r, union arg *args)
6325 struct ws_win *win = NULL;
6327 if (r && r->ws && r->ws->focus)
6336 /* Try to set window region based on supplied coordinates or window center. */
6338 regionize(struct ws_win *win, int x, int y)
6340 struct swm_region *r = NULL;
6342 r = region_under(win->s, x, y);
6344 r = region_under(win->s, X(win) + WIDTH(win) / 2,
6345 Y(win) + HEIGHT(win) / 2);
6347 if (r != NULL && r != win->ws->r) {
6348 if (clear_maximized(r->ws) > 0)
6351 win_to_ws(win, r->ws->idx, false);
6353 /* Set focus on new ws. */
6354 unfocus_win(r->ws->focus);
6362 #define SWM_MOVE_STEPS (50)
6365 move(struct ws_win *win, union arg *args)
6367 struct swm_region *r;
6368 xcb_timestamp_t timestamp = 0;
6370 xcb_query_pointer_reply_t *qpr = NULL;
6371 xcb_generic_event_t *evt;
6372 xcb_motion_notify_event_t *mne;
6373 bool moving, restack = false;
6378 if ((r = win->ws->r) == NULL)
6381 if (FULLSCREEN(win))
6384 DNPRINTF(SWM_D_EVENT, "move: win %#x, floating: %s, transient: "
6385 "%#x\n", win->id, YESNO(ABOVE(win)), win->transient);
6387 /* in max_stack mode should only move transients */
6388 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !TRANS(win))
6391 if (!(ABOVE(win) || TRANS(win)) || MAXIMIZED(win)) {
6392 store_float_geom(win);
6396 ewmh_apply_flags(win, (win->ewmh_flags | SWM_F_MANUAL | EWMH_F_ABOVE) &
6398 ewmh_update_wm_state(win);
6405 /* It's possible for win to have been freed during focus_flush(). */
6406 if (validate_win(win)) {
6407 DNPRINTF(SWM_D_EVENT, "move: invalid win.\n");
6413 case SWM_ARG_ID_MOVELEFT:
6414 X(win) -= (SWM_MOVE_STEPS - border_width);
6417 case SWM_ARG_ID_MOVERIGHT:
6418 X(win) += (SWM_MOVE_STEPS - border_width);
6421 case SWM_ARG_ID_MOVEUP:
6422 Y(win) -= (SWM_MOVE_STEPS - border_width);
6425 case SWM_ARG_ID_MOVEDOWN:
6426 Y(win) += (SWM_MOVE_STEPS - border_width);
6433 regionize(win, -1, -1);
6434 region_containment(win, win->ws->r, SWM_CW_ALLSIDES);
6436 store_float_geom(win);
6440 xcb_grab_pointer(conn, 0, win->id, MOUSEMASK,
6441 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC,
6442 XCB_WINDOW_NONE, cursors[XC_FLEUR].cid, XCB_CURRENT_TIME);
6444 /* get cursor offset from window root */
6445 qpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, win->id),
6448 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
6452 regionize(win, qpr->root_x, qpr->root_y);
6453 region_containment(win, win->ws->r, SWM_CW_ALLSIDES |
6454 SWM_CW_SOFTBOUNDARY);
6458 while (moving && (evt = xcb_wait_for_event(conn))) {
6459 switch (XCB_EVENT_RESPONSE_TYPE(evt)) {
6460 case XCB_BUTTON_RELEASE:
6461 DNPRINTF(SWM_D_EVENT, "move: BUTTON_RELEASE\n");
6464 case XCB_MOTION_NOTIFY:
6465 mne = (xcb_motion_notify_event_t *)evt;
6466 DNPRINTF(SWM_D_EVENT, "motion: root: %#x\n", mne->root);
6467 X(win) = mne->root_x - qpr->win_x - border_width;
6468 Y(win) = mne->root_y - qpr->win_y - border_width;
6470 /* not free, don't sync more than 120 times / second */
6471 if ((mne->time - timestamp) > (1000 / 120) ) {
6472 timestamp = mne->time;
6473 regionize(win, mne->root_x, mne->root_y);
6474 region_containment(win, win->ws->r,
6475 SWM_CW_ALLSIDES | SWM_CW_SOFTBOUNDARY);
6482 xcb_allow_events(conn, XCB_ALLOW_ASYNC_KEYBOARD,
6483 ((xcb_key_press_event_t *)evt)->time);
6489 /* It's possible for win to have been freed above. */
6490 if (validate_win(win)) {
6491 DNPRINTF(SWM_D_EVENT, "move: invalid win.\n");
6499 region_containment(win, win->ws->r, SWM_CW_ALLSIDES |
6500 SWM_CW_SOFTBOUNDARY);
6504 store_float_geom(win);
6506 /* New region set to fullscreen layout. */
6507 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK]) {
6514 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
6515 DNPRINTF(SWM_D_EVENT, "move: done.\n");
6519 move_step(struct swm_region *r, union arg *args)
6521 struct ws_win *win = NULL;
6523 if (r && r->ws && r->ws->focus)
6528 if (!TRANS(win) && !ABOVE(win))
6535 /* key definitions */
6537 char name[SWM_FUNCNAME_LEN];
6538 void (*func)(struct swm_region *r, union arg *);
6540 } keyfuncs[KF_INVALID + 1] = {
6541 /* name function argument */
6542 { "bar_toggle", bar_toggle, {.id = SWM_ARG_ID_BAR_TOGGLE} },
6543 { "bar_toggle_ws", bar_toggle, {.id = SWM_ARG_ID_BAR_TOGGLE_WS} },
6544 { "button2", pressbutton, {2} },
6545 { "cycle_layout", cycle_layout, {0} },
6546 { "flip_layout", stack_config, {.id = SWM_ARG_ID_FLIPLAYOUT} },
6547 { "float_toggle", floating_toggle,{0} },
6548 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
6549 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
6550 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
6551 { "focus_urgent", focus, {.id = SWM_ARG_ID_FOCUSURGENT} },
6552 { "maximize_toggle", maximize_toggle,{0} },
6553 { "height_grow", resize_step, {.id = SWM_ARG_ID_HEIGHTGROW} },
6554 { "height_shrink", resize_step, {.id = SWM_ARG_ID_HEIGHTSHRINK} },
6555 { "iconify", iconify, {0} },
6556 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
6557 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
6558 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
6559 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
6560 { "move_down", move_step, {.id = SWM_ARG_ID_MOVEDOWN} },
6561 { "move_left", move_step, {.id = SWM_ARG_ID_MOVELEFT} },
6562 { "move_right", move_step, {.id = SWM_ARG_ID_MOVERIGHT} },
6563 { "move_up", move_step, {.id = SWM_ARG_ID_MOVEUP} },
6564 { "mvrg_1", send_to_rg, {.id = 0} },
6565 { "mvrg_2", send_to_rg, {.id = 1} },
6566 { "mvrg_3", send_to_rg, {.id = 2} },
6567 { "mvrg_4", send_to_rg, {.id = 3} },
6568 { "mvrg_5", send_to_rg, {.id = 4} },
6569 { "mvrg_6", send_to_rg, {.id = 5} },
6570 { "mvrg_7", send_to_rg, {.id = 6} },
6571 { "mvrg_8", send_to_rg, {.id = 7} },
6572 { "mvrg_9", send_to_rg, {.id = 8} },
6573 { "mvws_1", send_to_ws, {.id = 0} },
6574 { "mvws_2", send_to_ws, {.id = 1} },
6575 { "mvws_3", send_to_ws, {.id = 2} },
6576 { "mvws_4", send_to_ws, {.id = 3} },
6577 { "mvws_5", send_to_ws, {.id = 4} },
6578 { "mvws_6", send_to_ws, {.id = 5} },
6579 { "mvws_7", send_to_ws, {.id = 6} },
6580 { "mvws_8", send_to_ws, {.id = 7} },
6581 { "mvws_9", send_to_ws, {.id = 8} },
6582 { "mvws_10", send_to_ws, {.id = 9} },
6583 { "mvws_11", send_to_ws, {.id = 10} },
6584 { "mvws_12", send_to_ws, {.id = 11} },
6585 { "mvws_13", send_to_ws, {.id = 12} },
6586 { "mvws_14", send_to_ws, {.id = 13} },
6587 { "mvws_15", send_to_ws, {.id = 14} },
6588 { "mvws_16", send_to_ws, {.id = 15} },
6589 { "mvws_17", send_to_ws, {.id = 16} },
6590 { "mvws_18", send_to_ws, {.id = 17} },
6591 { "mvws_19", send_to_ws, {.id = 18} },
6592 { "mvws_20", send_to_ws, {.id = 19} },
6593 { "mvws_21", send_to_ws, {.id = 20} },
6594 { "mvws_22", send_to_ws, {.id = 21} },
6595 { "name_workspace", name_workspace, {0} },
6596 { "quit", quit, {0} },
6597 { "raise_toggle", raise_toggle, {0} },
6598 { "restart", restart, {0} },
6599 { "rg_1", focusrg, {.id = 0} },
6600 { "rg_2", focusrg, {.id = 1} },
6601 { "rg_3", focusrg, {.id = 2} },
6602 { "rg_4", focusrg, {.id = 3} },
6603 { "rg_5", focusrg, {.id = 4} },
6604 { "rg_6", focusrg, {.id = 5} },
6605 { "rg_7", focusrg, {.id = 6} },
6606 { "rg_8", focusrg, {.id = 7} },
6607 { "rg_9", focusrg, {.id = 8} },
6608 { "rg_next", cyclerg, {.id = SWM_ARG_ID_CYCLERG_UP} },
6609 { "rg_prev", cyclerg, {.id = SWM_ARG_ID_CYCLERG_DOWN} },
6610 { "screen_next", cyclerg, {.id = SWM_ARG_ID_CYCLERG_UP} },
6611 { "screen_prev", cyclerg, {.id = SWM_ARG_ID_CYCLERG_DOWN} },
6612 { "search_win", search_win, {0} },
6613 { "search_workspace", search_workspace, {0} },
6614 { "spawn_custom", NULL, {0} },
6615 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
6616 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
6617 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
6618 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
6619 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
6620 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
6621 { "uniconify", uniconify, {0} },
6622 { "version", version, {0} },
6623 { "width_grow", resize_step, {.id = SWM_ARG_ID_WIDTHGROW} },
6624 { "width_shrink", resize_step, {.id = SWM_ARG_ID_WIDTHSHRINK} },
6625 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
6626 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
6627 { "ws_1", switchws, {.id = 0} },
6628 { "ws_2", switchws, {.id = 1} },
6629 { "ws_3", switchws, {.id = 2} },
6630 { "ws_4", switchws, {.id = 3} },
6631 { "ws_5", switchws, {.id = 4} },
6632 { "ws_6", switchws, {.id = 5} },
6633 { "ws_7", switchws, {.id = 6} },
6634 { "ws_8", switchws, {.id = 7} },
6635 { "ws_9", switchws, {.id = 8} },
6636 { "ws_10", switchws, {.id = 9} },
6637 { "ws_11", switchws, {.id = 10} },
6638 { "ws_12", switchws, {.id = 11} },
6639 { "ws_13", switchws, {.id = 12} },
6640 { "ws_14", switchws, {.id = 13} },
6641 { "ws_15", switchws, {.id = 14} },
6642 { "ws_16", switchws, {.id = 15} },
6643 { "ws_17", switchws, {.id = 16} },
6644 { "ws_18", switchws, {.id = 17} },
6645 { "ws_19", switchws, {.id = 18} },
6646 { "ws_20", switchws, {.id = 19} },
6647 { "ws_21", switchws, {.id = 20} },
6648 { "ws_22", switchws, {.id = 21} },
6649 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
6650 { "ws_next_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP_ALL} },
6651 { "ws_next_move", cyclews, {.id = SWM_ARG_ID_CYCLEWS_MOVE_UP} },
6652 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
6653 { "ws_prev_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN_ALL} },
6654 { "ws_prev_move", cyclews, {.id = SWM_ARG_ID_CYCLEWS_MOVE_DOWN} },
6655 { "ws_prior", priorws, {0} },
6656 { "dumpwins", dumpwins, {0} }, /* MUST BE LAST */
6657 { "invalid key func", NULL, {0} },
6661 key_cmp(struct key *kp1, struct key *kp2)
6663 if (kp1->keysym < kp2->keysym)
6665 if (kp1->keysym > kp2->keysym)
6668 if (kp1->mod < kp2->mod)
6670 if (kp1->mod > kp2->mod)
6677 enum { client_click, root_click };
6679 unsigned int action;
6681 unsigned int button;
6682 void (*func)(struct ws_win *, union arg *);
6685 #define MODKEY_SHIFT MODKEY | XCB_MOD_MASK_SHIFT
6686 /* action key mouse button func args */
6687 { client_click, MODKEY, XCB_BUTTON_INDEX_3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
6688 { client_click, MODKEY_SHIFT, XCB_BUTTON_INDEX_3, resize, {.id = SWM_ARG_ID_CENTER} },
6689 { client_click, MODKEY, XCB_BUTTON_INDEX_1, move, {0} },
6694 update_modkey(unsigned int mod)
6700 RB_FOREACH(kp, key_tree, &keys)
6701 if (kp->mod & XCB_MOD_MASK_SHIFT)
6702 kp->mod = mod | XCB_MOD_MASK_SHIFT;
6706 for (i = 0; i < LENGTH(buttons); i++)
6707 if (buttons[i].mask & XCB_MOD_MASK_SHIFT)
6708 buttons[i].mask = mod | XCB_MOD_MASK_SHIFT;
6710 buttons[i].mask = mod;
6714 spawn_expand(struct swm_region *r, union arg *args, const char *spawn_name,
6717 struct spawn_prog *prog = NULL;
6719 char *ap, **real_args;
6721 /* suppress unused warning since var is needed */
6724 DNPRINTF(SWM_D_SPAWN, "spawn_expand: %s\n", spawn_name);
6727 TAILQ_FOREACH(prog, &spawns, entry) {
6728 if (strcasecmp(spawn_name, prog->name) == 0)
6732 warnx("spawn_custom: program %s not found", spawn_name);
6736 /* make room for expanded args */
6737 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
6738 err(1, "spawn_custom: calloc real_args");
6740 /* expand spawn_args into real_args */
6741 for (i = c = 0; i < prog->argc; i++) {
6743 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg: %s\n", ap);
6744 if (strcasecmp(ap, "$bar_border") == 0) {
6746 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
6748 err(1, "spawn_custom border color");
6749 } else if (strcasecmp(ap, "$bar_color") == 0) {
6751 strdup(r->s->c[SWM_S_COLOR_BAR].name))
6753 err(1, "spawn_custom bar color");
6754 } else if (strcasecmp(ap, "$bar_font") == 0) {
6755 if ((real_args[c] = strdup(bar_fonts))
6757 err(1, "spawn_custom bar fonts");
6758 } else if (strcasecmp(ap, "$bar_font_color") == 0) {
6760 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
6762 err(1, "spawn_custom color font");
6763 } else if (strcasecmp(ap, "$color_focus") == 0) {
6765 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
6767 err(1, "spawn_custom color focus");
6768 } else if (strcasecmp(ap, "$color_focus_maximized") == 0) {
6770 strdup(r->s->c[SWM_S_COLOR_FOCUS_MAXIMIZED].name))
6772 err(1, "spawn_custom color focus maximized");
6773 } else if (strcasecmp(ap, "$color_unfocus") == 0) {
6775 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
6777 err(1, "spawn_custom color unfocus");
6778 } else if (strcasecmp(ap, "$color_unfocus_maximized") == 0) {
6780 strdup(r->s->c[SWM_S_COLOR_UNFOCUS_MAXIMIZED].name))
6782 err(1, "spawn_custom color unfocus maximized");
6783 } else if (strcasecmp(ap, "$region_index") == 0) {
6784 if (asprintf(&real_args[c], "%d",
6785 get_region_index(r) + 1) < 1)
6786 err(1, "spawn_custom region index");
6787 } else if (strcasecmp(ap, "$workspace_index") == 0) {
6788 if (asprintf(&real_args[c], "%d", r->ws->idx + 1) < 1)
6789 err(1, "spawn_custom workspace index");
6790 } else if (strcasecmp(ap, "$dmenu_bottom") == 0) {
6793 if ((real_args[c] = strdup("-b")) == NULL)
6794 err(1, "spawn_custom workspace index");
6796 /* no match --> copy as is */
6797 if ((real_args[c] = strdup(ap)) == NULL)
6798 err(1, "spawn_custom strdup(ap)");
6800 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg: %s\n",
6806 DNPRINTF(SWM_D_SPAWN, "spawn_custom: result: ");
6807 for (i = 0; i < c; ++i)
6808 DPRINTF("\"%s\" ", real_args[i]);
6811 *ret_args = real_args;
6816 spawn_custom(struct swm_region *r, union arg *args, const char *spawn_name)
6822 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
6826 spawn(r->ws->idx, &a, true);
6828 for (i = 0; i < spawn_argc; i++)
6834 spawn_select(struct swm_region *r, union arg *args, const char *spawn_name,
6841 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
6845 if (pipe(select_list_pipe) == -1)
6846 err(1, "pipe error");
6847 if (pipe(select_resp_pipe) == -1)
6848 err(1, "pipe error");
6850 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
6851 err(1, "could not disable SIGPIPE");
6852 switch (*pid = fork()) {
6854 err(1, "cannot fork");
6857 if (dup2(select_list_pipe[0], STDIN_FILENO) == -1)
6859 if (dup2(select_resp_pipe[1], STDOUT_FILENO) == -1)
6861 close(select_list_pipe[1]);
6862 close(select_resp_pipe[0]);
6863 spawn(r->ws->idx, &a, false);
6865 default: /* parent */
6866 close(select_list_pipe[0]);
6867 close(select_resp_pipe[1]);
6871 for (i = 0; i < spawn_argc; i++)
6876 /* Argument tokenizer. */
6879 char *arg, *cp, *next;
6880 bool single_quoted = false, double_quoted = false;
6885 /* Eat and move characters until end of argument is found. */
6886 for (arg = next = cp = *sp; *cp != '\0'; ++cp) {
6887 if (!double_quoted && *cp == '\'') {
6888 /* Eat single-quote. */
6889 single_quoted = !single_quoted;
6890 } else if (!single_quoted && *cp == '"') {
6891 /* Eat double-quote. */
6892 double_quoted = !double_quoted;
6893 } else if (!single_quoted && *cp == '\\' && *(cp + 1) == '"') {
6894 /* Eat backslash; copy escaped character to arg. */
6896 } else if (!single_quoted && !double_quoted && *cp == '\\' &&
6897 (*(cp + 1) == '\'' || *(cp + 1) == ' ')) {
6898 /* Eat backslash; move escaped character. */
6900 } else if (!single_quoted && !double_quoted &&
6901 (*cp == ' ' || *cp == '\t')) {
6902 /* Terminate argument. */
6904 /* Point sp to beginning of next argument. */
6908 /* Move regular character. */
6913 /* Terminate argument if end of string. */
6923 spawn_insert(const char *name, const char *args, int flags)
6925 struct spawn_prog *sp;
6926 char *arg, *cp, *ptr;
6928 DNPRINTF(SWM_D_SPAWN, "spawn_insert: %s[%s]\n", name, args);
6930 if (args == NULL || *args == '\0')
6933 if ((sp = calloc(1, sizeof *sp)) == NULL)
6934 err(1, "spawn_insert: calloc");
6935 if ((sp->name = strdup(name)) == NULL)
6936 err(1, "spawn_insert: strdup");
6938 /* Convert the arguments to an argument list. */
6939 if ((ptr = cp = strdup(args)) == NULL)
6940 err(1, "spawn_insert: strdup");
6941 while ((arg = argsep(&ptr)) != NULL) {
6942 /* Null argument; skip it. */
6947 if ((sp->argv = realloc(sp->argv, sp->argc *
6948 sizeof *sp->argv)) == NULL)
6949 err(1, "spawn_insert: realloc");
6950 if ((sp->argv[sp->argc - 1] = strdup(arg)) == NULL)
6951 err(1, "spawn_insert: strdup");
6957 DNPRINTF(SWM_D_SPAWN, "arg %d: [%s]\n", sp->argc, sp->argv[sp->argc-1]);
6958 TAILQ_INSERT_TAIL(&spawns, sp, entry);
6959 DNPRINTF(SWM_D_SPAWN, "spawn_insert: leave\n");
6963 spawn_remove(struct spawn_prog *sp)
6967 DNPRINTF(SWM_D_SPAWN, "spawn_remove: %s\n", sp->name);
6969 TAILQ_REMOVE(&spawns, sp, entry);
6970 for (i = 0; i < sp->argc; i++)
6976 DNPRINTF(SWM_D_SPAWN, "spawn_remove: leave\n");
6980 spawn_find(const char *name)
6982 struct spawn_prog *sp;
6984 TAILQ_FOREACH(sp, &spawns, entry)
6985 if (strcasecmp(sp->name, name) == 0)
6992 setspawn(const char *name, const char *args, int flags)
6994 struct spawn_prog *sp;
6996 DNPRINTF(SWM_D_SPAWN, "setspawn: %s\n", name);
7001 /* Remove any old spawn under the same name. */
7002 if ((sp = spawn_find(name)) != NULL)
7006 spawn_insert(name, args, flags);
7008 warnx("error: setspawn: cannot find program: %s", name);
7010 DNPRINTF(SWM_D_SPAWN, "setspawn: leave\n");
7014 setconfspawn(const char *selector, const char *value, int flags)
7018 if (selector == NULL || strlen(selector) == 0)
7021 args = expand_tilde(value);
7023 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, args);
7025 setspawn(selector, args, flags);
7028 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done.\n");
7033 validate_spawns(void)
7035 struct spawn_prog *sp;
7036 char which[PATH_MAX];
7041 RB_FOREACH(kp, key_tree, &keys) {
7042 if (kp->funcid != KF_SPAWN_CUSTOM)
7046 sp = spawn_find(kp->spawn_name);
7047 if (sp == NULL || sp->flags & SWM_SPAWN_OPTIONAL)
7050 /* verify we have the goods */
7051 snprintf(which, sizeof which, "which %s", sp->argv[0]);
7052 DNPRINTF(SWM_D_CONF, "validate_spawns: which %s\n",
7054 for (i = strlen("which "); i < strlen(which); i++)
7055 if (which[i] == ' ') {
7059 if (system(which) != 0)
7060 add_startup_exception("could not find %s",
7061 &which[strlen("which ")]);
7068 setconfspawn("lock", "xlock", 0);
7070 setconfspawn("term", "xterm", 0);
7071 setconfspawn("spawn_term", "xterm", 0);
7073 setconfspawn("menu", "dmenu_run"
7077 " -nf $bar_font_color"
7079 " -sf $bar_color", 0);
7081 setconfspawn("search", "dmenu"
7086 " -nf $bar_font_color"
7088 " -sf $bar_color", 0);
7090 setconfspawn("name_workspace", "dmenu"
7095 " -nf $bar_font_color"
7097 " -sf $bar_color", 0);
7099 /* These are not verified for existence, even with a binding set. */
7100 setconfspawn("screenshot_all", "screenshot.sh full", SWM_SPAWN_OPTIONAL);
7101 setconfspawn("screenshot_wind", "screenshot.sh window", SWM_SPAWN_OPTIONAL);
7102 setconfspawn("initscr", "initscreen.sh", SWM_SPAWN_OPTIONAL);
7106 #define SWM_MODNAME_SIZE 32
7107 #define SWM_KEY_WS "\n+ \t"
7109 parsekeys(const char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
7111 char *str, *cp, *name;
7114 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
7115 if (mod == NULL || ks == NULL) {
7116 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
7119 if (keystr == NULL || strlen(keystr) == 0) {
7120 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
7124 if ((cp = str = strdup(keystr)) == NULL)
7125 err(1, "parsekeys: strdup");
7127 *ks = XCB_NO_SYMBOL;
7129 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
7130 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
7132 cp += (long)strspn(cp, SWM_KEY_WS);
7133 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
7135 else if (strncasecmp(name, "Mod1", SWM_MODNAME_SIZE) == 0)
7136 *mod |= XCB_MOD_MASK_1;
7137 else if (strncasecmp(name, "Mod2", SWM_MODNAME_SIZE) == 0)
7138 *mod += XCB_MOD_MASK_2;
7139 else if (strncmp(name, "Mod3", SWM_MODNAME_SIZE) == 0)
7140 *mod |= XCB_MOD_MASK_3;
7141 else if (strncmp(name, "Mod4", SWM_MODNAME_SIZE) == 0)
7142 *mod |= XCB_MOD_MASK_4;
7143 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
7144 *mod |= XCB_MOD_MASK_SHIFT;
7145 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
7146 *mod |= XCB_MOD_MASK_CONTROL;
7148 *ks = XStringToKeysym(name);
7149 XConvertCase(*ks, ks, &uks);
7150 if (ks == XCB_NO_SYMBOL) {
7152 "parsekeys: invalid key %s\n",
7161 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
7166 strdupsafe(const char *str)
7171 return (strdup(str));
7175 key_insert(unsigned int mod, KeySym ks, enum keyfuncid kfid,
7176 const char *spawn_name)
7180 DNPRINTF(SWM_D_KEY, "key_insert: enter %s [%s]\n",
7181 keyfuncs[kfid].name, spawn_name);
7183 if ((kp = malloc(sizeof *kp)) == NULL)
7184 err(1, "key_insert: malloc");
7189 kp->spawn_name = strdupsafe(spawn_name);
7190 RB_INSERT(key_tree, &keys, kp);
7192 DNPRINTF(SWM_D_KEY, "key_insert: leave\n");
7196 key_lookup(unsigned int mod, KeySym ks)
7203 return (RB_FIND(key_tree, &keys, &kp));
7207 key_remove(struct key *kp)
7209 DNPRINTF(SWM_D_KEY, "key_remove: %s\n", keyfuncs[kp->funcid].name);
7211 RB_REMOVE(key_tree, &keys, kp);
7212 free(kp->spawn_name);
7215 DNPRINTF(SWM_D_KEY, "key_remove: leave\n");
7219 key_replace(struct key *kp, unsigned int mod, KeySym ks, enum keyfuncid kfid,
7220 const char *spawn_name)
7222 DNPRINTF(SWM_D_KEY, "key_replace: %s [%s]\n", keyfuncs[kp->funcid].name,
7226 key_insert(mod, ks, kfid, spawn_name);
7228 DNPRINTF(SWM_D_KEY, "key_replace: leave\n");
7232 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid,
7233 const char *spawn_name)
7237 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
7238 keyfuncs[kfid].name, spawn_name);
7240 if ((kp = key_lookup(mod, ks)) != NULL) {
7241 if (kfid == KF_INVALID)
7244 key_replace(kp, mod, ks, kfid, spawn_name);
7245 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
7248 if (kfid == KF_INVALID) {
7249 warnx("bind: Key combination already unbound.");
7250 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
7254 key_insert(mod, ks, kfid, spawn_name);
7255 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
7259 setconfbinding(const char *selector, const char *value, int flags)
7261 enum keyfuncid kfid;
7264 struct spawn_prog *sp;
7266 /* suppress unused warning since var is needed */
7269 DNPRINTF(SWM_D_KEY, "setconfbinding: enter selector: [%s], "
7270 "value: [%s]\n", selector, value);
7271 if (selector == NULL || strlen(selector) == 0) {
7272 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
7273 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
7275 setkeybinding(mod, ks, kfid, NULL);
7280 /* search by key function name */
7281 for (kfid = 0; kfid < KF_INVALID; (kfid)++) {
7282 if (strncasecmp(selector, keyfuncs[kfid].name,
7283 SWM_FUNCNAME_LEN) == 0) {
7284 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match "
7285 "keyfunc\n", selector);
7286 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
7287 setkeybinding(mod, ks, kfid, NULL);
7293 /* search by custom spawn name */
7294 if ((sp = spawn_find(selector)) != NULL) {
7295 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match "
7296 "spawn\n", selector);
7297 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
7298 setkeybinding(mod, ks, KF_SPAWN_CUSTOM,
7304 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
7311 #define MODKEY_SHIFT MODKEY | XCB_MOD_MASK_SHIFT
7312 setkeybinding(MODKEY, XK_b, KF_BAR_TOGGLE, NULL);
7313 setkeybinding(MODKEY_SHIFT, XK_b, KF_BAR_TOGGLE_WS,NULL);
7314 setkeybinding(MODKEY, XK_v, KF_BUTTON2, NULL);
7315 setkeybinding(MODKEY, XK_space, KF_CYCLE_LAYOUT,NULL);
7316 setkeybinding(MODKEY_SHIFT, XK_backslash, KF_FLIP_LAYOUT, NULL);
7317 setkeybinding(MODKEY, XK_t, KF_FLOAT_TOGGLE,NULL);
7318 setkeybinding(MODKEY, XK_m, KF_FOCUS_MAIN, NULL);
7319 setkeybinding(MODKEY, XK_j, KF_FOCUS_NEXT, NULL);
7320 setkeybinding(MODKEY, XK_Tab, KF_FOCUS_NEXT, NULL);
7321 setkeybinding(MODKEY, XK_k, KF_FOCUS_PREV, NULL);
7322 setkeybinding(MODKEY_SHIFT, XK_Tab, KF_FOCUS_PREV, NULL);
7323 setkeybinding(MODKEY, XK_u, KF_FOCUS_URGENT,NULL);
7324 setkeybinding(MODKEY, XK_e, KF_MAXIMIZE_TOGGLE,NULL);
7325 setkeybinding(MODKEY_SHIFT, XK_equal, KF_HEIGHT_GROW,NULL);
7326 setkeybinding(MODKEY_SHIFT, XK_minus, KF_HEIGHT_SHRINK,NULL);
7327 setkeybinding(MODKEY, XK_w, KF_ICONIFY, NULL);
7328 setkeybinding(MODKEY, XK_h, KF_MASTER_SHRINK, NULL);
7329 setkeybinding(MODKEY, XK_l, KF_MASTER_GROW, NULL);
7330 setkeybinding(MODKEY, XK_comma, KF_MASTER_ADD, NULL);
7331 setkeybinding(MODKEY, XK_period, KF_MASTER_DEL, NULL);
7332 setkeybinding(MODKEY_SHIFT, XK_bracketright,KF_MOVE_DOWN,NULL);
7333 setkeybinding(MODKEY, XK_bracketleft, KF_MOVE_LEFT,NULL);
7334 setkeybinding(MODKEY, XK_bracketright,KF_MOVE_RIGHT,NULL);
7335 setkeybinding(MODKEY_SHIFT, XK_bracketleft, KF_MOVE_UP, NULL);
7336 setkeybinding(MODKEY_SHIFT, XK_KP_End, KF_MVRG_1, NULL);
7337 setkeybinding(MODKEY_SHIFT, XK_KP_Down, KF_MVRG_2, NULL);
7338 setkeybinding(MODKEY_SHIFT, XK_KP_Next, KF_MVRG_3, NULL);
7339 setkeybinding(MODKEY_SHIFT, XK_KP_Left, KF_MVRG_4, NULL);
7340 setkeybinding(MODKEY_SHIFT, XK_KP_Begin, KF_MVRG_5, NULL);
7341 setkeybinding(MODKEY_SHIFT, XK_KP_Right, KF_MVRG_6, NULL);
7342 setkeybinding(MODKEY_SHIFT, XK_KP_Home, KF_MVRG_7, NULL);
7343 setkeybinding(MODKEY_SHIFT, XK_KP_Up, KF_MVRG_8, NULL);
7344 setkeybinding(MODKEY_SHIFT, XK_KP_Prior, KF_MVRG_9, NULL);
7345 setkeybinding(MODKEY_SHIFT, XK_1, KF_MVWS_1, NULL);
7346 setkeybinding(MODKEY_SHIFT, XK_2, KF_MVWS_2, NULL);
7347 setkeybinding(MODKEY_SHIFT, XK_3, KF_MVWS_3, NULL);
7348 setkeybinding(MODKEY_SHIFT, XK_4, KF_MVWS_4, NULL);
7349 setkeybinding(MODKEY_SHIFT, XK_5, KF_MVWS_5, NULL);
7350 setkeybinding(MODKEY_SHIFT, XK_6, KF_MVWS_6, NULL);
7351 setkeybinding(MODKEY_SHIFT, XK_7, KF_MVWS_7, NULL);
7352 setkeybinding(MODKEY_SHIFT, XK_8, KF_MVWS_8, NULL);
7353 setkeybinding(MODKEY_SHIFT, XK_9, KF_MVWS_9, NULL);
7354 setkeybinding(MODKEY_SHIFT, XK_0, KF_MVWS_10, NULL);
7355 setkeybinding(MODKEY_SHIFT, XK_F1, KF_MVWS_11, NULL);
7356 setkeybinding(MODKEY_SHIFT, XK_F2, KF_MVWS_12, NULL);
7357 setkeybinding(MODKEY_SHIFT, XK_F3, KF_MVWS_13, NULL);
7358 setkeybinding(MODKEY_SHIFT, XK_F4, KF_MVWS_14, NULL);
7359 setkeybinding(MODKEY_SHIFT, XK_F5, KF_MVWS_15, NULL);
7360 setkeybinding(MODKEY_SHIFT, XK_F6, KF_MVWS_16, NULL);
7361 setkeybinding(MODKEY_SHIFT, XK_F7, KF_MVWS_17, NULL);
7362 setkeybinding(MODKEY_SHIFT, XK_F8, KF_MVWS_18, NULL);
7363 setkeybinding(MODKEY_SHIFT, XK_F9, KF_MVWS_19, NULL);
7364 setkeybinding(MODKEY_SHIFT, XK_F10, KF_MVWS_20, NULL);
7365 setkeybinding(MODKEY_SHIFT, XK_F11, KF_MVWS_21, NULL);
7366 setkeybinding(MODKEY_SHIFT, XK_F12, KF_MVWS_22, NULL);
7367 setkeybinding(MODKEY_SHIFT, XK_slash, KF_NAME_WORKSPACE,NULL);
7368 setkeybinding(MODKEY_SHIFT, XK_q, KF_QUIT, NULL);
7369 setkeybinding(MODKEY_SHIFT, XK_r, KF_RAISE_TOGGLE,NULL);
7370 setkeybinding(MODKEY, XK_q, KF_RESTART, NULL);
7371 setkeybinding(MODKEY, XK_KP_End, KF_RG_1, NULL);
7372 setkeybinding(MODKEY, XK_KP_Down, KF_RG_2, NULL);
7373 setkeybinding(MODKEY, XK_KP_Next, KF_RG_3, NULL);
7374 setkeybinding(MODKEY, XK_KP_Left, KF_RG_4, NULL);
7375 setkeybinding(MODKEY, XK_KP_Begin, KF_RG_5, NULL);
7376 setkeybinding(MODKEY, XK_KP_Right, KF_RG_6, NULL);
7377 setkeybinding(MODKEY, XK_KP_Home, KF_RG_7, NULL);
7378 setkeybinding(MODKEY, XK_KP_Up, KF_RG_8, NULL);
7379 setkeybinding(MODKEY, XK_KP_Prior, KF_RG_9, NULL);
7380 setkeybinding(MODKEY_SHIFT, XK_Right, KF_RG_NEXT, NULL);
7381 setkeybinding(MODKEY_SHIFT, XK_Left, KF_RG_PREV, NULL);
7382 setkeybinding(MODKEY, XK_f, KF_SEARCH_WIN, NULL);
7383 setkeybinding(MODKEY, XK_slash, KF_SEARCH_WORKSPACE,NULL);
7384 setkeybinding(MODKEY_SHIFT, XK_i, KF_SPAWN_CUSTOM,"initscr");
7385 setkeybinding(MODKEY_SHIFT, XK_Delete, KF_SPAWN_CUSTOM,"lock");
7386 setkeybinding(MODKEY, XK_p, KF_SPAWN_CUSTOM,"menu");
7387 setkeybinding(MODKEY, XK_s, KF_SPAWN_CUSTOM,"screenshot_all");
7388 setkeybinding(MODKEY_SHIFT, XK_s, KF_SPAWN_CUSTOM,"screenshot_wind");
7389 setkeybinding(MODKEY_SHIFT, XK_Return, KF_SPAWN_CUSTOM,"term");
7390 setkeybinding(MODKEY_SHIFT, XK_comma, KF_STACK_INC, NULL);
7391 setkeybinding(MODKEY_SHIFT, XK_period, KF_STACK_DEC, NULL);
7392 setkeybinding(MODKEY_SHIFT, XK_space, KF_STACK_RESET, NULL);
7393 setkeybinding(MODKEY, XK_Return, KF_SWAP_MAIN, NULL);
7394 setkeybinding(MODKEY_SHIFT, XK_j, KF_SWAP_NEXT, NULL);
7395 setkeybinding(MODKEY_SHIFT, XK_k, KF_SWAP_PREV, NULL);
7396 setkeybinding(MODKEY_SHIFT, XK_w, KF_UNICONIFY, NULL);
7397 setkeybinding(MODKEY_SHIFT, XK_v, KF_VERSION, NULL);
7398 setkeybinding(MODKEY, XK_equal, KF_WIDTH_GROW, NULL);
7399 setkeybinding(MODKEY, XK_minus, KF_WIDTH_SHRINK,NULL);
7400 setkeybinding(MODKEY, XK_x, KF_WIND_DEL, NULL);
7401 setkeybinding(MODKEY_SHIFT, XK_x, KF_WIND_KILL, NULL);
7402 setkeybinding(MODKEY, XK_1, KF_WS_1, NULL);
7403 setkeybinding(MODKEY, XK_2, KF_WS_2, NULL);
7404 setkeybinding(MODKEY, XK_3, KF_WS_3, NULL);
7405 setkeybinding(MODKEY, XK_4, KF_WS_4, NULL);
7406 setkeybinding(MODKEY, XK_5, KF_WS_5, NULL);
7407 setkeybinding(MODKEY, XK_6, KF_WS_6, NULL);
7408 setkeybinding(MODKEY, XK_7, KF_WS_7, NULL);
7409 setkeybinding(MODKEY, XK_8, KF_WS_8, NULL);
7410 setkeybinding(MODKEY, XK_9, KF_WS_9, NULL);
7411 setkeybinding(MODKEY, XK_0, KF_WS_10, NULL);
7412 setkeybinding(MODKEY, XK_F1, KF_WS_11, NULL);
7413 setkeybinding(MODKEY, XK_F2, KF_WS_12, NULL);
7414 setkeybinding(MODKEY, XK_F3, KF_WS_13, NULL);
7415 setkeybinding(MODKEY, XK_F4, KF_WS_14, NULL);
7416 setkeybinding(MODKEY, XK_F5, KF_WS_15, NULL);
7417 setkeybinding(MODKEY, XK_F6, KF_WS_16, NULL);
7418 setkeybinding(MODKEY, XK_F7, KF_WS_17, NULL);
7419 setkeybinding(MODKEY, XK_F8, KF_WS_18, NULL);
7420 setkeybinding(MODKEY, XK_F9, KF_WS_19, NULL);
7421 setkeybinding(MODKEY, XK_F10, KF_WS_20, NULL);
7422 setkeybinding(MODKEY, XK_F11, KF_WS_21, NULL);
7423 setkeybinding(MODKEY, XK_F12, KF_WS_22, NULL);
7424 setkeybinding(MODKEY, XK_Right, KF_WS_NEXT, NULL);
7425 setkeybinding(MODKEY, XK_Left, KF_WS_PREV, NULL);
7426 setkeybinding(MODKEY, XK_Up, KF_WS_NEXT_ALL, NULL);
7427 setkeybinding(MODKEY, XK_Down, KF_WS_PREV_ALL, NULL);
7428 setkeybinding(MODKEY_SHIFT, XK_Up, KF_WS_NEXT_MOVE,NULL);
7429 setkeybinding(MODKEY_SHIFT, XK_Down, KF_WS_PREV_MOVE,NULL);
7430 setkeybinding(MODKEY, XK_a, KF_WS_PRIOR, NULL);
7432 setkeybinding(MODKEY_SHIFT, XK_d, KF_DUMPWINS, NULL);
7442 while (RB_EMPTY(&keys) == 0) {
7443 kp = RB_ROOT(&keys);
7449 setkeymapping(const char *selector, const char *value, int flags)
7451 char *keymapping_file;
7453 /* suppress unused warnings since vars are needed */
7457 DNPRINTF(SWM_D_KEY, "setkeymapping: enter\n");
7459 keymapping_file = expand_tilde(value);
7462 /* load new key bindings; if it fails, revert to default bindings */
7463 if (conf_load(keymapping_file, SWM_CONF_KEYMAPPING)) {
7468 free(keymapping_file);
7470 DNPRINTF(SWM_D_KEY, "setkeymapping: leave\n");
7475 updatenumlockmask(void)
7478 xcb_get_modifier_mapping_reply_t *modmap_r;
7479 xcb_keycode_t *modmap, kc, *keycode;
7483 modmap_r = xcb_get_modifier_mapping_reply(conn,
7484 xcb_get_modifier_mapping(conn),
7487 modmap = xcb_get_modifier_mapping_keycodes(modmap_r);
7488 for (i = 0; i < 8; i++) {
7489 for (j = 0; j < modmap_r->keycodes_per_modifier; j++) {
7490 kc = modmap[i * modmap_r->keycodes_per_modifier
7492 keycode = xcb_key_symbols_get_keycode(syms,
7496 numlockmask = (1 << i);
7503 DNPRINTF(SWM_D_MISC, "updatenumlockmask: %d\n", numlockmask);
7510 int num_screens, k, j;
7511 unsigned int modifiers[4];
7512 xcb_keycode_t *code;
7514 DNPRINTF(SWM_D_MISC, "grabkeys\n");
7515 updatenumlockmask();
7518 modifiers[1] = numlockmask;
7519 modifiers[2] = XCB_MOD_MASK_LOCK;
7520 modifiers[3] = numlockmask | XCB_MOD_MASK_LOCK;
7522 num_screens = get_screen_count();
7523 for (k = 0; k < num_screens; k++) {
7524 if (TAILQ_EMPTY(&screens[k].rl))
7526 xcb_ungrab_key(conn, XCB_GRAB_ANY, screens[k].root,
7528 RB_FOREACH(kp, key_tree, &keys) {
7529 /* Skip unused ws binds. */
7530 if ((int)kp->funcid > KF_WS_1 + workspace_limit - 1 &&
7531 kp->funcid <= KF_WS_22)
7534 /* Skip unused mvws binds. */
7535 if ((int)kp->funcid > KF_MVWS_1 + workspace_limit - 1 &&
7536 kp->funcid <= KF_MVWS_22)
7539 if ((code = xcb_key_symbols_get_keycode(syms,
7541 for (j = 0; j < LENGTH(modifiers); j++)
7542 xcb_grab_key(conn, 1,
7544 kp->mod | modifiers[j],
7545 *code, XCB_GRAB_MODE_SYNC,
7546 XCB_GRAB_MODE_SYNC);
7554 grabbuttons(struct ws_win *win)
7556 unsigned int modifiers[4];
7559 DNPRINTF(SWM_D_MOUSE, "grabbuttons: win %#x\n", win->id);
7560 updatenumlockmask();
7563 modifiers[1] = numlockmask;
7564 modifiers[2] = XCB_MOD_MASK_LOCK;
7565 modifiers[3] = numlockmask | XCB_MOD_MASK_LOCK;
7567 for (i = 0; i < LENGTH(buttons); i++)
7568 if (buttons[i].action == client_click)
7569 for (j = 0; j < LENGTH(modifiers); ++j)
7570 xcb_grab_button(conn, 0, win->id, BUTTONMASK,
7571 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC,
7572 XCB_WINDOW_NONE, XCB_CURSOR_NONE,
7573 buttons[i].button, buttons[i].mask |
7577 const char *quirkname[] = {
7578 "NONE", /* config string for "no value" */
7586 "FOCUSONMAP_SINGLE",
7592 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
7593 #define SWM_Q_WS "\n|+ \t"
7595 parsequirks(const char *qstr, unsigned long *quirk)
7597 char *str, *cp, *name;
7600 if (quirk == NULL || qstr == NULL)
7603 if ((str = strdup(qstr)) == NULL)
7604 err(1, "parsequirks: strdup");
7608 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
7610 cp += (long)strspn(cp, SWM_Q_WS);
7611 for (i = 0; i < LENGTH(quirkname); i++) {
7612 if (strncasecmp(name, quirkname[i],
7613 SWM_QUIRK_LEN) == 0) {
7614 DNPRINTF(SWM_D_QUIRK,
7615 "parsequirks: %s\n", name);
7621 *quirk |= 1 << (i-1);
7625 if (i >= LENGTH(quirkname)) {
7626 DNPRINTF(SWM_D_QUIRK,
7627 "parsequirks: invalid quirk [%s]\n", name);
7638 quirk_insert(const char *class, const char *instance, const char *name,
7639 unsigned long quirk)
7643 bool failed = false;
7645 DNPRINTF(SWM_D_QUIRK, "quirk_insert: class: %s, instance: %s, name: %s,"
7646 " value: %lu\n", class, instance, name, quirk);
7648 if ((qp = malloc(sizeof *qp)) == NULL)
7649 err(1, "quirk_insert: malloc");
7651 if ((qp->class = strdup(class)) == NULL)
7652 err(1, "quirk_insert: strdup");
7653 if ((qp->instance = strdup(instance)) == NULL)
7654 err(1, "quirk_insert: strdup");
7655 if ((qp->name = strdup(name)) == NULL)
7656 err(1, "quirk_insert: strdup");
7658 if (asprintf(&str, "^%s$", class) == -1)
7659 err(1, "quirk_insert: asprintf");
7660 if (regcomp(&qp->regex_class, str, REG_EXTENDED | REG_NOSUB)) {
7661 add_startup_exception("regex failed to compile quirk 'class' "
7662 "field: %s", class);
7665 DNPRINTF(SWM_D_QUIRK, "quirk_insert: compiled: %s\n", str);
7668 if (asprintf(&str, "^%s$", instance) == -1)
7669 err(1, "quirk_insert: asprintf");
7670 if (regcomp(&qp->regex_instance, str, REG_EXTENDED | REG_NOSUB)) {
7671 add_startup_exception("regex failed to compile quirk 'instance'"
7672 " field: %s", instance);
7675 DNPRINTF(SWM_D_QUIRK, "quirk_insert: compiled: %s\n", str);
7678 if (asprintf(&str, "^%s$", name) == -1)
7679 err(1, "quirk_insert: asprintf");
7680 if (regcomp(&qp->regex_name, str, REG_EXTENDED | REG_NOSUB)) {
7681 add_startup_exception("regex failed to compile quirk 'name' "
7685 DNPRINTF(SWM_D_QUIRK, "quirk_insert: compiled: %s\n", str);
7689 DNPRINTF(SWM_D_QUIRK, "quirk_insert: regex error; skipping.\n");
7693 TAILQ_INSERT_TAIL(&quirks, qp, entry);
7695 DNPRINTF(SWM_D_QUIRK, "quirk_insert: leave\n");
7699 quirk_remove(struct quirk *qp)
7701 DNPRINTF(SWM_D_QUIRK, "quirk_remove: %s:%s [%lu]\n", qp->class,
7702 qp->name, qp->quirk);
7704 TAILQ_REMOVE(&quirks, qp, entry);
7707 DNPRINTF(SWM_D_QUIRK, "quirk_remove: leave\n");
7711 quirk_free(struct quirk *qp)
7713 regfree(&qp->regex_class);
7714 regfree(&qp->regex_instance);
7715 regfree(&qp->regex_name);
7723 quirk_replace(struct quirk *qp, const char *class, const char *instance,
7724 const char *name, unsigned long quirk)
7726 DNPRINTF(SWM_D_QUIRK, "quirk_replace: %s:%s:%s [%lu]\n", qp->class,
7727 qp->instance, qp->name, qp->quirk);
7730 quirk_insert(class, instance, name, quirk);
7732 DNPRINTF(SWM_D_QUIRK, "quirk_replace: leave\n");
7736 setquirk(const char *class, const char *instance, const char *name,
7737 unsigned long quirk)
7741 DNPRINTF(SWM_D_QUIRK, "setquirk: enter %s:%s:%s [%lu]\n", class,
7742 instance, name, quirk);
7744 /* Remove/replace existing quirk. */
7745 TAILQ_FOREACH(qp, &quirks, entry) {
7746 if (strcmp(qp->class, class) == 0 &&
7747 strcmp(qp->instance, instance) == 0 &&
7748 strcmp(qp->name, name) == 0) {
7752 quirk_replace(qp, class, instance, name, quirk);
7753 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
7758 /* Only insert if quirk is not NONE. */
7760 quirk_insert(class, instance, name, quirk);
7762 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
7765 /* Eat '\' in str used to escape square brackets and colon. */
7767 unescape_selector(char *str)
7771 for (cp = str; *str != '\0'; ++str, ++cp) {
7772 if (*str == '\\' && (*(str + 1) == ':' || *(str + 1) == ']' ||
7782 setconfquirk(const char *selector, const char *value, int flags)
7784 char *str, *cp, *class;
7785 char *instance = NULL, *name = NULL;
7786 int retval, count = 0;
7789 /* suppress unused warning since var is needed */
7792 if (selector == NULL || strlen(selector) == 0)
7795 if ((str = strdup(selector)) == NULL)
7796 err(1, "setconfquirk: strdup");
7798 /* Find non-escaped colon. */
7805 for (++cp; *cp != '\0'; ++cp) {
7806 if (*cp == ':' && *(cp - 1) != '\\') {
7812 unescape_selector(class);
7814 instance = class + strlen(class) + 1;
7815 unescape_selector(instance);
7821 name = instance + strlen(instance) + 1;
7822 unescape_selector(name);
7827 DNPRINTF(SWM_D_CONF, "setconfquirk: class: %s, instance: %s, "
7828 "name: %s\n", class, instance, name);
7830 if ((retval = parsequirks(value, &qrks)) == 0)
7831 setquirk(class, instance, name, qrks);
7840 setquirk("MPlayer", "xv", ".*", SWM_Q_FLOAT | SWM_Q_FULLSCREEN | SWM_Q_FOCUSPREV);
7841 setquirk("OpenOffice.org 3.2", "VCLSalFrame", ".*", SWM_Q_FLOAT);
7842 setquirk("Firefox-bin", "firefox-bin", ".*", SWM_Q_TRANSSZ);
7843 setquirk("Firefox", "Dialog", ".*", SWM_Q_FLOAT);
7844 setquirk("Gimp", "gimp", ".*", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
7845 setquirk("XTerm", "xterm", ".*", SWM_Q_XTERM_FONTADJ);
7846 setquirk("xine", "Xine Window", ".*", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
7847 setquirk("Xitk", "Xitk Combo", ".*", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
7848 setquirk("xine", "xine Panel", ".*", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
7849 setquirk("Xitk", "Xine Window", ".*", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
7850 setquirk("xine", "xine Video Fullscreen Window", ".*", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
7851 setquirk("pcb", "pcb", ".*", SWM_Q_FLOAT);
7852 setquirk("SDL_App", "SDL_App", ".*", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
7855 /* conf file stuff */
7856 #define SWM_CONF_FILE "spectrwm.conf"
7857 #define SWM_CONF_FILE_OLD "scrotwm.conf"
7861 SWM_S_BAR_AT_BOTTOM,
7862 SWM_S_BAR_BORDER_WIDTH,
7865 SWM_S_BAR_ENABLED_WS,
7870 SWM_S_BOUNDARY_WIDTH,
7871 SWM_S_CLOCK_ENABLED,
7873 SWM_S_COMPOSITE_ENABLED,
7875 SWM_S_CYCLE_VISIBLE,
7877 SWM_S_DISABLE_BORDER,
7879 SWM_S_FOCUS_CLOSE_WRAP,
7880 SWM_S_FOCUS_DEFAULT,
7882 SWM_S_ICONIC_ENABLED,
7883 SWM_S_JAVA_WORKAROUND,
7884 SWM_S_OPACITY_FOCUS,
7885 SWM_S_OPACITY_UNFOCUS,
7886 SWM_S_REGION_PADDING,
7891 SWM_S_STACK_ENABLED,
7894 SWM_S_URGENT_ENABLED,
7895 SWM_S_VERBOSE_LAYOUT,
7896 SWM_S_WINDOW_CLASS_ENABLED,
7897 SWM_S_WINDOW_INSTANCE_ENABLED,
7898 SWM_S_WINDOW_NAME_ENABLED,
7899 SWM_S_WORKSPACE_LIMIT,
7900 SWM_S_WORKSPACE_NAME,
7904 setconfvalue(const char *selector, const char *value, int flags)
7906 struct workspace *ws;
7907 int i, ws_id, num_screens;
7908 char *b, *str, s[1024];
7911 case SWM_S_BAR_ACTION:
7913 if ((bar_argv[0] = expand_tilde(value)) == NULL)
7914 err(1, "setconfvalue: bar_action");
7916 case SWM_S_BAR_AT_BOTTOM:
7917 bar_at_bottom = (atoi(value) != 0);
7919 case SWM_S_BAR_BORDER_WIDTH:
7920 bar_border_width = atoi(value);
7921 if (bar_border_width < 0)
7922 bar_border_width = 0;
7924 case SWM_S_BAR_DELAY:
7925 /* No longer needed; leave to not break old conf files. */
7927 case SWM_S_BAR_ENABLED:
7928 bar_enabled = (atoi(value) != 0);
7930 case SWM_S_BAR_ENABLED_WS:
7931 ws_id = atoi(selector) - 1;
7932 if (ws_id < 0 || ws_id >= workspace_limit)
7933 errx(1, "setconfvalue: bar_enabled_ws: invalid "
7934 "workspace %d.", ws_id + 1);
7936 num_screens = get_screen_count();
7937 for (i = 0; i < num_screens; i++) {
7938 ws = (struct workspace *)&screens[i].ws;
7939 ws[ws_id].bar_enabled = (atoi(value) != 0);
7942 case SWM_S_BAR_FONT:
7944 if (asprintf(&bar_fonts, "%s,%s", value, bar_fonts) == -1)
7945 err(1, "setconfvalue: asprintf: failed to allocate "
7946 "memory for bar_fonts.");
7949 /* If already in xft mode, then we are done. */
7950 if (!bar_font_legacy)
7953 if ((str = strdup(value)) == NULL)
7954 err(1, "setconfvalue: strdup");
7956 /* If there are any non-XLFD entries, switch to Xft mode. */
7957 while ((b = strsep(&str, ",")) != NULL) {
7961 bar_font_legacy = false;
7968 case SWM_S_BAR_FORMAT:
7970 if ((bar_format = strdup(value)) == NULL)
7971 err(1, "setconfvalue: bar_format");
7973 case SWM_S_BAR_JUSTIFY:
7974 if (strcmp(value, "left") == 0)
7975 bar_justify = SWM_BAR_JUSTIFY_LEFT;
7976 else if (strcmp(value, "center") == 0)
7977 bar_justify = SWM_BAR_JUSTIFY_CENTER;
7978 else if (strcmp(value, "right") == 0)
7979 bar_justify = SWM_BAR_JUSTIFY_RIGHT;
7981 errx(1, "invalid bar_justify");
7983 case SWM_S_BORDER_WIDTH:
7984 border_width = atoi(value);
7985 if (border_width < 0)
7988 case SWM_S_BOUNDARY_WIDTH:
7989 boundary_width = atoi(value);
7990 if (boundary_width < 0)
7993 case SWM_S_CLOCK_ENABLED:
7994 clock_enabled = (atoi(value) != 0);
7996 case SWM_S_CLOCK_FORMAT:
7997 #ifndef SWM_DENY_CLOCK_FORMAT
7999 if ((clock_format = strdup(value)) == NULL)
8000 err(1, "setconfvalue: clock_format");
8003 case SWM_S_CYCLE_EMPTY:
8004 cycle_empty = (atoi(value) != 0);
8006 case SWM_S_CYCLE_VISIBLE:
8007 cycle_visible = (atoi(value) != 0);
8009 case SWM_S_DIALOG_RATIO:
8010 dialog_ratio = atof(value);
8011 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
8014 case SWM_S_DISABLE_BORDER:
8015 disable_border = (atoi(value) != 0);
8017 case SWM_S_FOCUS_CLOSE:
8018 if (strcmp(value, "first") == 0)
8019 focus_close = SWM_STACK_BOTTOM;
8020 else if (strcmp(value, "last") == 0)
8021 focus_close = SWM_STACK_TOP;
8022 else if (strcmp(value, "next") == 0)
8023 focus_close = SWM_STACK_ABOVE;
8024 else if (strcmp(value, "previous") == 0)
8025 focus_close = SWM_STACK_BELOW;
8027 errx(1, "focus_close");
8029 case SWM_S_FOCUS_CLOSE_WRAP:
8030 focus_close_wrap = (atoi(value) != 0);
8032 case SWM_S_FOCUS_DEFAULT:
8033 if (strcmp(value, "last") == 0)
8034 focus_default = SWM_STACK_TOP;
8035 else if (strcmp(value, "first") == 0)
8036 focus_default = SWM_STACK_BOTTOM;
8038 errx(1, "focus_default");
8040 case SWM_S_FOCUS_MODE:
8041 if (strcmp(value, "default") == 0)
8042 focus_mode = SWM_FOCUS_DEFAULT;
8043 else if (strcmp(value, "follow") == 0 ||
8044 strcmp(value, "follow_cursor") == 0)
8045 focus_mode = SWM_FOCUS_FOLLOW;
8046 else if (strcmp(value, "manual") == 0)
8047 focus_mode = SWM_FOCUS_MANUAL;
8049 errx(1, "focus_mode");
8051 case SWM_S_ICONIC_ENABLED:
8052 iconic_enabled = (atoi(value) != 0);
8054 case SWM_S_JAVA_WORKAROUND:
8055 java_workaround = (atoi(value) != 0);
8057 case SWM_S_REGION_PADDING:
8058 region_padding = atoi(value);
8059 if (region_padding < 0)
8062 case SWM_S_SPAWN_ORDER:
8063 if (strcmp(value, "first") == 0)
8064 spawn_position = SWM_STACK_BOTTOM;
8065 else if (strcmp(value, "last") == 0)
8066 spawn_position = SWM_STACK_TOP;
8067 else if (strcmp(value, "next") == 0)
8068 spawn_position = SWM_STACK_ABOVE;
8069 else if (strcmp(value, "previous") == 0)
8070 spawn_position = SWM_STACK_BELOW;
8072 errx(1, "spawn_position");
8074 case SWM_S_SPAWN_TERM:
8075 setconfspawn("term", value, 0);
8076 setconfspawn("spawn_term", value, 0);
8079 /* No longer needed; leave to not break old conf files. */
8081 case SWM_S_SS_ENABLED:
8082 /* No longer needed; leave to not break old conf files. */
8084 case SWM_S_STACK_ENABLED:
8085 stack_enabled = (atoi(value) != 0);
8087 case SWM_S_TERM_WIDTH:
8088 term_width = atoi(value);
8092 case SWM_S_TILE_GAP:
8093 tile_gap = atoi(value);
8095 case SWM_S_URGENT_ENABLED:
8096 urgent_enabled = (atoi(value) != 0);
8098 case SWM_S_COMPOSITE_ENABLED:
8099 composite_enabled = atoi(value);
8101 case SWM_S_OPACITY_FOCUS:
8102 opacity_focus = atof(value);
8103 if (opacity_focus > 1.0)
8104 opacity_focus = 1.0;
8105 else if (opacity_focus < 0.0)
8106 opacity_focus = 0.0;
8108 case SWM_S_OPACITY_UNFOCUS:
8109 opacity_unfocus = atof(value);
8110 if (opacity_unfocus > 1.0)
8111 opacity_unfocus = 1.0;
8112 else if (opacity_unfocus < 0.0)
8113 opacity_unfocus = 0.0;
8115 case SWM_S_VERBOSE_LAYOUT:
8116 verbose_layout = (atoi(value) != 0);
8117 for (i = 0; layouts[i].l_stack != NULL; i++) {
8119 layouts[i].l_string = fancy_stacker;
8121 layouts[i].l_string = plain_stacker;
8124 case SWM_S_WINDOW_CLASS_ENABLED:
8125 window_class_enabled = (atoi(value) != 0);
8127 case SWM_S_WINDOW_INSTANCE_ENABLED:
8128 window_instance_enabled = (atoi(value) != 0);
8130 case SWM_S_WINDOW_NAME_ENABLED:
8131 window_name_enabled = (atoi(value) != 0);
8133 case SWM_S_WORKSPACE_LIMIT:
8134 workspace_limit = atoi(value);
8135 if (workspace_limit > SWM_WS_MAX)
8136 workspace_limit = SWM_WS_MAX;
8137 else if (workspace_limit < 1)
8138 workspace_limit = 1;
8140 ewmh_update_desktops();
8142 case SWM_S_WORKSPACE_NAME:
8143 if (getenv("SWM_STARTED") != NULL)
8147 if (sscanf(value, "ws[%d]:%1023c", &ws_id, s) != 2)
8148 errx(1, "invalid entry, should be 'ws[<idx>]:name'");
8150 if (ws_id < 0 || ws_id >= workspace_limit)
8151 errx(1, "setconfvalue: workspace_name: invalid "
8152 "workspace %d.", ws_id + 1);
8154 num_screens = get_screen_count();
8155 for (i = 0; i < num_screens; ++i) {
8156 ws = (struct workspace *)&screens[i].ws;
8158 if (strlen(s) > 0) {
8159 free(ws[ws_id].name);
8160 if ((ws[ws_id].name = strdup(s)) == NULL)
8161 err(1, "setconfvalue: workspace_name.");
8163 ewmh_update_desktop_names();
8164 ewmh_get_desktop_names();
8175 setconfmodkey(const char *selector, const char *value, int flags)
8177 /* suppress unused warnings since vars are needed */
8181 if (strncasecmp(value, "Mod1", strlen("Mod1")) == 0)
8182 update_modkey(XCB_MOD_MASK_1);
8183 else if (strncasecmp(value, "Mod2", strlen("Mod2")) == 0)
8184 update_modkey(XCB_MOD_MASK_2);
8185 else if (strncasecmp(value, "Mod3", strlen("Mod3")) == 0)
8186 update_modkey(XCB_MOD_MASK_3);
8187 else if (strncasecmp(value, "Mod4", strlen("Mod4")) == 0)
8188 update_modkey(XCB_MOD_MASK_4);
8195 setconfcolor(const char *selector, const char *value, int flags)
8197 int first, last, i = 0, num_screens;
8199 num_screens = get_screen_count();
8201 /* conf screen indices begin at 1; treat vals <= 0 as 'all screens.' */
8202 if (selector == NULL || strlen(selector) == 0 ||
8203 (last = atoi(selector) - 1) < 0) {
8205 last = num_screens - 1;
8210 if (last >= num_screens) {
8211 add_startup_exception("invalid screen index: %d out of bounds "
8212 "(maximum %d)", last + 1, num_screens);
8216 for (i = first; i <= last; ++i) {
8217 setscreencolor(value, i, flags);
8220 * When setting focus/unfocus colors, we need to also
8221 * set maximize colors to match if they haven't been customized.
8223 if (flags == SWM_S_COLOR_FOCUS &&
8224 !screens[i].c[SWM_S_COLOR_FOCUS_MAXIMIZED].manual)
8225 setscreencolor(value, i, SWM_S_COLOR_FOCUS_MAXIMIZED);
8226 else if (flags == SWM_S_COLOR_UNFOCUS &&
8227 !screens[i].c[SWM_S_COLOR_UNFOCUS_MAXIMIZED].manual)
8228 setscreencolor(value, i, SWM_S_COLOR_UNFOCUS_MAXIMIZED);
8230 screens[i].c[flags].manual = 1;
8237 setconfregion(const char *selector, const char *value, int flags)
8239 /* suppress unused warnings since vars are needed */
8243 custom_region(value);
8248 setautorun(const char *selector, const char *value, int flags)
8258 /* suppress unused warnings since vars are needed */
8262 if (getenv("SWM_STARTED"))
8266 if (sscanf(value, "ws[%d]:%1023c", &ws_id, s) != 2)
8267 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
8269 if (ws_id < 0 || ws_id >= workspace_limit)
8270 errx(1, "autorun: invalid workspace %d", ws_id + 1);
8272 sp = expand_tilde((char *)&s);
8275 * This is a little intricate
8277 * If the pid already exists we simply reuse it because it means it was
8278 * used before AND not claimed by manage_window. We get away with
8279 * altering it in the parent after INSERT because this can not be a race
8282 while ((ap = strsep(&sp, " \t")) != NULL) {
8285 DNPRINTF(SWM_D_SPAWN, "setautorun: arg [%s]\n", ap);
8287 if ((a.argv = realloc(a.argv, argc * sizeof(char *))) == NULL)
8288 err(1, "setautorun: realloc");
8289 a.argv[argc - 1] = ap;
8293 if ((a.argv = realloc(a.argv, (argc + 1) * sizeof(char *))) == NULL)
8294 err(1, "setautorun: realloc");
8295 a.argv[argc] = NULL;
8297 if ((pid = fork()) == 0) {
8298 spawn(ws_id, &a, true);
8307 p = calloc(1, sizeof *p);
8310 TAILQ_INSERT_TAIL(&pidlist, p, entry);
8320 setlayout(const char *selector, const char *value, int flags)
8322 struct workspace *ws;
8323 int ws_id, i, x, mg, ma, si, ar;
8324 int st = SWM_V_STACK, num_screens;
8328 /* suppress unused warnings since vars are needed */
8332 if (getenv("SWM_STARTED"))
8336 if (sscanf(value, "ws[%d]:%d:%d:%d:%d:%1023c",
8337 &ws_id, &mg, &ma, &si, &ar, s) != 6)
8338 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
8339 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
8342 if (ws_id < 0 || ws_id >= workspace_limit)
8343 errx(1, "layout: invalid workspace %d", ws_id + 1);
8345 if (strcasecmp(s, "vertical") == 0)
8347 else if (strcasecmp(s, "vertical_flip") == 0) {
8350 } else if (strcasecmp(s, "horizontal") == 0)
8352 else if (strcasecmp(s, "horizontal_flip") == 0) {
8355 } else if (strcasecmp(s, "fullscreen") == 0)
8358 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
8359 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
8362 num_screens = get_screen_count();
8363 for (i = 0; i < num_screens; i++) {
8364 ws = (struct workspace *)&screens[i].ws;
8365 ws[ws_id].cur_layout = &layouts[st];
8367 ws[ws_id].always_raise = (ar != 0);
8368 if (st == SWM_MAX_STACK)
8372 for (x = 0; x < abs(mg); x++) {
8373 ws[ws_id].cur_layout->l_config(&ws[ws_id],
8374 mg >= 0 ? SWM_ARG_ID_MASTERGROW :
8375 SWM_ARG_ID_MASTERSHRINK);
8378 for (x = 0; x < abs(ma); x++) {
8379 ws[ws_id].cur_layout->l_config(&ws[ws_id],
8380 ma >= 0 ? SWM_ARG_ID_MASTERADD :
8381 SWM_ARG_ID_MASTERDEL);
8384 for (x = 0; x < abs(si); x++) {
8385 ws[ws_id].cur_layout->l_config(&ws[ws_id],
8386 si >= 0 ? SWM_ARG_ID_STACKINC :
8387 SWM_ARG_ID_STACKDEC);
8391 ws[ws_id].cur_layout->l_config(&ws[ws_id],
8392 SWM_ARG_ID_FLIPLAYOUT);
8399 /* config options */
8400 struct config_option {
8402 int (*func)(const char*, const char*, int);
8405 struct config_option configopt[] = {
8406 { "autorun", setautorun, 0 },
8407 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
8408 { "bar_at_bottom", setconfvalue, SWM_S_BAR_AT_BOTTOM },
8409 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
8410 { "bar_border_unfocus", setconfcolor, SWM_S_COLOR_BAR_BORDER_UNFOCUS },
8411 { "bar_border_width", setconfvalue, SWM_S_BAR_BORDER_WIDTH },
8412 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
8413 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
8414 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
8415 { "bar_enabled_ws", setconfvalue, SWM_S_BAR_ENABLED_WS },
8416 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
8417 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
8418 { "bar_format", setconfvalue, SWM_S_BAR_FORMAT },
8419 { "bar_justify", setconfvalue, SWM_S_BAR_JUSTIFY },
8420 { "bind", setconfbinding, 0 },
8421 { "border_width", setconfvalue, SWM_S_BORDER_WIDTH },
8422 { "boundary_width", setconfvalue, SWM_S_BOUNDARY_WIDTH },
8423 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
8424 { "clock_format", setconfvalue, SWM_S_CLOCK_FORMAT },
8425 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
8426 { "color_focus_maximized", setconfcolor, SWM_S_COLOR_FOCUS_MAXIMIZED },
8427 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
8428 { "color_unfocus_maximized", setconfcolor, SWM_S_COLOR_UNFOCUS_MAXIMIZED },
8429 { "composite_enabled", setconfvalue, SWM_S_COMPOSITE_ENABLED },
8430 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
8431 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
8432 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
8433 { "disable_border", setconfvalue, SWM_S_DISABLE_BORDER },
8434 { "focus_close", setconfvalue, SWM_S_FOCUS_CLOSE },
8435 { "focus_close_wrap", setconfvalue, SWM_S_FOCUS_CLOSE_WRAP },
8436 { "focus_default", setconfvalue, SWM_S_FOCUS_DEFAULT },
8437 { "focus_mode", setconfvalue, SWM_S_FOCUS_MODE },
8438 { "iconic_enabled", setconfvalue, SWM_S_ICONIC_ENABLED },
8439 { "java_workaround", setconfvalue, SWM_S_JAVA_WORKAROUND },
8440 { "keyboard_mapping", setkeymapping, 0 },
8441 { "layout", setlayout, 0 },
8442 { "modkey", setconfmodkey, 0 },
8443 { "opacity_focus", setconfvalue, SWM_S_OPACITY_FOCUS },
8444 { "opacity_unfocus", setconfvalue, SWM_S_OPACITY_UNFOCUS },
8445 { "program", setconfspawn, 0 },
8446 { "quirk", setconfquirk, 0 },
8447 { "region", setconfregion, 0 },
8448 { "region_padding", setconfvalue, SWM_S_REGION_PADDING },
8449 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
8450 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
8451 { "spawn_position", setconfvalue, SWM_S_SPAWN_ORDER },
8452 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
8453 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
8454 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
8455 { "tile_gap", setconfvalue, SWM_S_TILE_GAP },
8456 { "title_class_enabled", setconfvalue, SWM_S_WINDOW_CLASS_ENABLED }, /* For backwards compat. */
8457 { "title_name_enabled", setconfvalue, SWM_S_WINDOW_INSTANCE_ENABLED }, /* For backwards compat. */
8458 { "urgent_enabled", setconfvalue, SWM_S_URGENT_ENABLED },
8459 { "verbose_layout", setconfvalue, SWM_S_VERBOSE_LAYOUT },
8460 { "window_class_enabled", setconfvalue, SWM_S_WINDOW_CLASS_ENABLED },
8461 { "window_instance_enabled", setconfvalue, SWM_S_WINDOW_INSTANCE_ENABLED },
8462 { "window_name_enabled", setconfvalue, SWM_S_WINDOW_NAME_ENABLED },
8463 { "workspace_limit", setconfvalue, SWM_S_WORKSPACE_LIMIT },
8464 { "name", setconfvalue, SWM_S_WORKSPACE_NAME },
8468 _add_startup_exception(const char *fmt, va_list ap)
8470 if (vasprintf(&startup_exception, fmt, ap) == -1)
8471 warn("%s: asprintf", __func__);
8475 add_startup_exception(const char *fmt, ...)
8481 if (startup_exception)
8484 /* force bar to be enabled due to exception */
8488 _add_startup_exception(fmt, ap);
8493 conf_load(const char *filename, int keymapping)
8496 char *line = NULL, *cp, *ce, *optsub, *optval = NULL;
8497 size_t linelen, lineno = 0;
8498 int wordlen, i, optidx, count;
8499 struct config_option *opt = NULL;
8501 DNPRINTF(SWM_D_CONF, "conf_load: begin\n");
8503 if (filename == NULL) {
8504 warnx("conf_load: no filename");
8508 DNPRINTF(SWM_D_CONF, "conf_load: open %s\n", filename);
8510 if ((config = fopen(filename, "r")) == NULL) {
8511 warn("conf_load: fopen: %s", filename);
8515 while (!feof(config)) {
8519 if ((line = fparseln(config, &linelen, &lineno, NULL,
8520 FPARSELN_UNESCCOMM | FPARSELN_UNESCCONT)) == NULL) {
8522 err(1, "%s", filename);
8527 cp += strspn(cp, " \t\n"); /* eat whitespace */
8528 if (cp[0] == '\0') {
8532 /* get config option */
8533 wordlen = strcspn(cp, "=[ \t\n");
8535 add_startup_exception("%s: line %zd: no option found",
8540 for (i = 0; i < LENGTH(configopt); i++) {
8541 opt = &configopt[i];
8542 if (strncasecmp(cp, opt->optname, wordlen) == 0 &&
8543 (int)strlen(opt->optname) == wordlen) {
8549 add_startup_exception("%s: line %zd: unknown option "
8550 "%.*s", filename, lineno, wordlen, cp);
8553 if (keymapping && opt && strcmp(opt->optname, "bind")) {
8554 add_startup_exception("%s: line %zd: invalid option "
8555 "%.*s", filename, lineno, wordlen, cp);
8559 cp += strspn(cp, " \t\n"); /* eat whitespace */
8561 /* get [selector] if any */
8566 /* Get length of selector. */
8567 for (ce = ++cp; *ce != '\0'; ++ce) {
8568 /* Find matching (not escaped) bracket. */
8569 if (*ce == ']' && *(ce - 1) != '\\') {
8576 add_startup_exception("%s: line %zd: syntax "
8577 "error: unterminated selector", filename,
8582 /* ce points at ']'; terminate optsub. */
8587 cp += strspn(cp, "= \t\n"); /* eat trailing */
8590 if (strlen(optval) == 0) {
8591 add_startup_exception("%s: line %zd: must supply value "
8592 "to %s", filename, lineno,
8593 configopt[optidx].optname);
8596 /* call function to deal with it all */
8597 if (configopt[optidx].func(optsub, optval,
8598 configopt[optidx].funcflags) != 0) {
8599 add_startup_exception("%s: line %zd: invalid data for "
8600 "%s", filename, lineno, configopt[optidx].optname);
8608 DNPRINTF(SWM_D_CONF, "conf_load: end\n");
8614 set_child_transient(struct ws_win *win, xcb_window_t *trans)
8616 struct ws_win *parent, *w;
8617 struct swm_region *r;
8618 struct workspace *ws;
8619 xcb_icccm_wm_hints_t wmh;
8621 parent = find_window(win->transient);
8623 parent->focus_child = win;
8625 DNPRINTF(SWM_D_MISC, "set_child_transient: parent doesn't exist"
8626 " for %#x trans %#x\n", win->id, win->transient);
8628 r = root_to_region(win->s->root, SWM_CK_ALL);
8630 /* parent doen't exist in our window list */
8631 TAILQ_FOREACH(w, &ws->winlist, entry) {
8632 if (xcb_icccm_get_wm_hints_reply(conn,
8633 xcb_icccm_get_wm_hints(conn, w->id),
8635 warnx("can't get hints for %#x", w->id);
8639 if (win->hints.window_group != wmh.window_group)
8642 w->focus_child = win;
8643 win->transient = w->id;
8645 DNPRINTF(SWM_D_MISC, "set_child_transient: adjusting "
8646 "transient to %#x\n", win->transient);
8653 window_get_pid(xcb_window_t win)
8658 xcb_get_property_cookie_t pc;
8659 xcb_get_property_reply_t *pr;
8661 apid = get_atom_from_string("_NET_WM_PID");
8662 if (apid == XCB_ATOM_NONE)
8665 pc = xcb_get_property(conn, 0, win, apid, XCB_ATOM_CARDINAL, 0, 1);
8666 pr = xcb_get_property_reply(conn, pc, NULL);
8669 if (pr->type != XCB_ATOM_CARDINAL) {
8674 if (pr->type == apid && pr->format == 32)
8675 ret = *((pid_t *)xcb_get_property_value(pr));
8681 apid = get_atom_from_string("_SWM_PID");
8682 pc = xcb_get_property(conn, 0, win, apid, XCB_ATOM_STRING,
8684 pr = xcb_get_property_reply(conn, pc, NULL);
8687 if (pr->type != apid) {
8692 ret = (pid_t)strtonum(xcb_get_property_value(pr), 0, INT_MAX, &errstr);
8699 get_swm_ws(xcb_window_t id)
8705 xcb_get_property_reply_t *gpr;
8707 gpr = xcb_get_property_reply(conn,
8708 xcb_get_property(conn, 0, id, a_swm_ws,
8709 XCB_ATOM_STRING, 0, SWM_PROPLEN),
8714 proplen = xcb_get_property_value_length(gpr);
8716 prop = malloc(proplen + 1);
8719 xcb_get_property_value(gpr),
8721 prop[proplen] = '\0';
8728 DNPRINTF(SWM_D_PROP, "get_swm_ws: _SWM_WS: %s\n", prop);
8729 ws_idx = (int)strtonum(prop, 0, workspace_limit - 1, &errstr);
8731 DNPRINTF(SWM_D_PROP, "get_swm_ws: win #%s: %s",
8741 get_ws_idx(struct ws_win *win)
8743 xcb_get_property_reply_t *gpr;
8749 gpr = xcb_get_property_reply(conn,
8750 xcb_get_property(conn, 0, win->id, ewmh[_NET_WM_DESKTOP].atom,
8751 XCB_ATOM_CARDINAL, 0, 1),
8754 if (gpr->type == XCB_ATOM_CARDINAL && gpr->format == 32)
8755 ws_idx = *((int *)xcb_get_property_value(gpr));
8759 if (ws_idx == -1 && !(win->quirks & SWM_Q_IGNORESPAWNWS))
8760 ws_idx = get_swm_ws(win->id);
8762 if (ws_idx > workspace_limit - 1 || ws_idx < -1)
8765 DNPRINTF(SWM_D_PROP, "get_ws_idx: win %#x, ws_idx: %d\n", win->id,
8772 manage_window(xcb_window_t id, int spawn_pos, bool mapped)
8774 struct ws_win *win, *ww;
8775 struct swm_region *r;
8778 xcb_get_geometry_reply_t *gr;
8779 xcb_window_t trans = XCB_WINDOW_NONE;
8780 uint32_t i, wa[2], new_flags;
8782 char *class, *instance, *name;
8784 if ((win = find_window(id)) != NULL) {
8785 DNPRINTF(SWM_D_MISC, "manage_window: win %#x already "
8786 "managed; skipping.)\n", id);
8787 return (win); /* Already managed. */
8790 /* See if window is on the unmanaged list. */
8791 if ((win = find_unmanaged_window(id)) != NULL) {
8792 DNPRINTF(SWM_D_MISC, "manage_window: win %#x found on "
8793 "unmanaged list.\n", id);
8794 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
8797 set_child_transient(win, &trans);
8801 DNPRINTF(SWM_D_MISC, "manage_window: win %#x is new.\n", id);
8804 /* Try to get initial window geometry. */
8805 gr = xcb_get_geometry_reply(conn, xcb_get_geometry(conn, id), NULL);
8807 DNPRINTF(SWM_D_MISC, "manage_window: get geometry failed.\n");
8811 /* Create and initialize ws_win object. */
8812 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
8813 err(1, "manage_window: calloc: failed to allocate memory for "
8818 /* Figureout which region the window belongs to. */
8819 r = root_to_region(gr->root, SWM_CK_ALL);
8821 /* Ignore window border if there is one. */
8822 WIDTH(win) = gr->width;
8823 HEIGHT(win) = gr->height;
8824 X(win) = gr->x + gr->border_width - border_width;
8825 Y(win) = gr->y + gr->border_width - border_width;
8826 win->bordered = true;
8827 win->mapped = mapped;
8828 win->s = r->s; /* this never changes */
8832 /* Select which X events to monitor and set border pixel color. */
8833 wa[0] = win->s->c[SWM_S_COLOR_UNFOCUS].pixel;
8834 wa[1] = XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_PROPERTY_CHANGE |
8835 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
8837 wa[1] |= XCB_EVENT_MASK_LEAVE_WINDOW | XCB_EVENT_MASK_FOCUS_CHANGE;
8840 xcb_change_window_attributes(conn, win->id, XCB_CW_BORDER_PIXEL |
8841 XCB_CW_EVENT_MASK, wa);
8843 if (composite_enabled)
8844 set_opacity(win, opacity_unfocus * OPAQUE);
8846 /* Get WM_SIZE_HINTS. */
8847 xcb_icccm_get_wm_normal_hints_reply(conn,
8848 xcb_icccm_get_wm_normal_hints(conn, win->id),
8852 xcb_icccm_get_wm_hints_reply(conn,
8853 xcb_icccm_get_wm_hints(conn, win->id),
8856 /* Get WM_TRANSIENT_FOR; see if window is a transient. */
8857 xcb_icccm_get_wm_transient_for_reply(conn,
8858 xcb_icccm_get_wm_transient_for(conn, win->id),
8861 win->transient = trans;
8862 set_child_transient(win, &win->transient);
8865 /* Get WM_PROTOCOLS. */
8866 get_wm_protocols(win);
8868 /* Set initial quirks based on EWMH. */
8869 ewmh_autoquirk(win);
8871 /* Determine initial quirks. */
8872 xcb_icccm_get_wm_class_reply(conn,
8873 xcb_icccm_get_wm_class(conn, win->id),
8876 class = win->ch.class_name ? win->ch.class_name : "";
8877 instance = win->ch.instance_name ? win->ch.instance_name : "";
8878 name = get_win_name(win->id);
8880 DNPRINTF(SWM_D_CLASS, "manage_window: class: %s, instance: %s, "
8881 "name: %s\n", class, instance, name);
8883 /* java is retarded so treat it special */
8884 if (strstr(instance, "sun-awt")) {
8885 DNPRINTF(SWM_D_CLASS, "manage_window: java window detected.\n");
8889 TAILQ_FOREACH(qp, &quirks, entry) {
8890 if (regexec(&qp->regex_class, class, 0, NULL, 0) == 0 &&
8891 regexec(&qp->regex_instance, instance, 0, NULL, 0) == 0 &&
8892 regexec(&qp->regex_name, name, 0, NULL, 0) == 0) {
8893 DNPRINTF(SWM_D_CLASS, "manage_window: matched "
8894 "quirk: %s:%s:%s mask: %#lx\n", qp->class,
8895 qp->instance, qp->name, qp->quirk);
8896 win->quirks = qp->quirk;
8902 /* Reset font sizes (the bruteforce way; no default keybinding). */
8903 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
8904 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
8905 fake_keypress(win, XK_KP_Subtract, XCB_MOD_MASK_SHIFT);
8906 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
8907 fake_keypress(win, XK_KP_Add, XCB_MOD_MASK_SHIFT);
8910 /* Figure out which workspace the window belongs to. */
8911 if (!(win->quirks & SWM_Q_IGNOREPID) &&
8912 (p = find_pid(window_get_pid(win->id))) != NULL) {
8913 win->ws = &r->s->ws[p->ws];
8914 TAILQ_REMOVE(&pidlist, p, entry);
8917 } else if ((ws_idx = get_ws_idx(win)) != -1 &&
8919 /* _SWM_WS is set; use that. */
8920 win->ws = &r->s->ws[ws_idx];
8921 } else if (trans && (ww = find_window(trans)) != NULL) {
8922 /* Launch transients in the same ws as parent. */
8928 /* Set the _NET_WM_DESKTOP atom. */
8929 DNPRINTF(SWM_D_PROP, "manage_window: set _NET_WM_DESKTOP: %d\n",
8931 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
8932 ewmh[_NET_WM_DESKTOP].atom, XCB_ATOM_CARDINAL, 32, 1, &win->ws->idx);
8934 /* Remove any _SWM_WS now that we set _NET_WM_DESKTOP. */
8935 xcb_delete_property(conn, win->id, a_swm_ws);
8937 /* WS must already be set for this to work. */
8938 store_float_geom(win);
8940 /* Make sure window is positioned inside its region, if its active. */
8942 region_containment(win, r, SWM_CW_ALLSIDES |
8943 SWM_CW_HARDBOUNDARY);
8948 /* Figure out where to stack the window in the workspace. */
8949 if (trans && (ww = find_window(trans)))
8950 TAILQ_INSERT_AFTER(&win->ws->winlist, ww, win, entry);
8951 else if (win->ws->focus && spawn_pos == SWM_STACK_ABOVE)
8952 TAILQ_INSERT_AFTER(&win->ws->winlist, win->ws->focus, win,
8954 else if (win->ws->focus && spawn_pos == SWM_STACK_BELOW)
8955 TAILQ_INSERT_BEFORE(win->ws->focus, win, entry);
8956 else switch (spawn_pos) {
8959 case SWM_STACK_ABOVE:
8960 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
8962 case SWM_STACK_BOTTOM:
8963 case SWM_STACK_BELOW:
8964 TAILQ_INSERT_HEAD(&win->ws->winlist, win, entry);
8967 ewmh_update_client_list();
8969 TAILQ_INSERT_TAIL(&win->ws->stack, win, stack_entry);
8971 /* Get/apply initial _NET_WM_STATE */
8972 ewmh_get_wm_state(win);
8975 new_flags = win->ewmh_flags;
8977 if (win->quirks & SWM_Q_FLOAT)
8978 new_flags |= EWMH_F_ABOVE;
8980 if (win->quirks & SWM_Q_ANYWHERE)
8981 new_flags |= SWM_F_MANUAL;
8983 ewmh_apply_flags(win, new_flags);
8984 ewmh_update_wm_state(win);
8986 /* Set initial _NET_WM_ALLOWED_ACTIONS */
8987 ewmh_update_actions(win);
8991 DNPRINTF(SWM_D_MISC, "manage_window: done. win %#x, (x,y) w x h: "
8992 "(%d,%d) %d x %d, ws: %d, iconic: %s, transient: %#x\n", win->id,
8993 X(win), Y(win), WIDTH(win), HEIGHT(win), win->ws->idx,
8994 YESNO(ICONIC(win)), win->transient);
9000 free_window(struct ws_win *win)
9002 DNPRINTF(SWM_D_MISC, "free_window: win %#x\n", win->id);
9007 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
9009 xcb_icccm_get_wm_class_reply_wipe(&win->ch);
9014 memset(win, 0xff, sizeof *win); /* XXX kill later */
9017 DNPRINTF(SWM_D_MISC, "free_window: done.\n");
9021 unmanage_window(struct ws_win *win)
9023 struct ws_win *parent;
9028 DNPRINTF(SWM_D_MISC, "unmanage_window: win %#x\n", win->id);
9031 parent = find_window(win->transient);
9033 parent->focus_child = NULL;
9036 TAILQ_REMOVE(&win->ws->stack, win, stack_entry);
9037 TAILQ_REMOVE(&win->ws->winlist, win, entry);
9038 TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
9040 ewmh_update_client_list();
9044 expose(xcb_expose_event_t *e)
9047 struct swm_region *r;
9049 DNPRINTF(SWM_D_EVENT, "expose: win %#x\n", e->window);
9051 num_screens = get_screen_count();
9052 for (i = 0; i < num_screens; i++)
9053 TAILQ_FOREACH(r, &screens[i].rl, entry)
9054 if (e->window == WINID(r->bar))
9062 focusin(xcb_focus_in_event_t *e)
9064 DNPRINTF(SWM_D_EVENT, "focusin: win %#x, mode: %s(%u), "
9065 "detail: %s(%u)\n", e->event, get_notify_mode_label(e->mode),
9066 e->mode, get_notify_detail_label(e->detail), e->detail);
9070 focusout(xcb_focus_out_event_t *e)
9072 DNPRINTF(SWM_D_EVENT, "focusout: win %#x, mode: %s(%u), "
9073 "detail: %s(%u)\n", e->event, get_notify_mode_label(e->mode),
9074 e->mode, get_notify_detail_label(e->detail), e->detail);
9079 keypress(xcb_key_press_event_t *e)
9081 xcb_keysym_t keysym;
9084 keysym = xcb_key_press_lookup_keysym(syms, e, 0);
9086 DNPRINTF(SWM_D_EVENT, "keypress: keysym: %u, win (x,y): %#x (%d,%d), "
9087 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
9088 "state: %u, same_screen: %s\n", keysym, e->event, e->event_x,
9089 e->event_y, e->detail, e->time, e->root, e->root_x, e->root_y,
9090 e->child, e->state, YESNO(e->same_screen));
9092 if ((kp = key_lookup(CLEANMASK(e->state), keysym)) == NULL)
9095 last_event_time = e->time;
9097 if (kp->funcid == KF_SPAWN_CUSTOM)
9098 spawn_custom(root_to_region(e->root, SWM_CK_ALL),
9099 &(keyfuncs[kp->funcid].args), kp->spawn_name);
9100 else if (keyfuncs[kp->funcid].func)
9101 keyfuncs[kp->funcid].func(root_to_region(e->root, SWM_CK_ALL),
9102 &(keyfuncs[kp->funcid].args));
9105 /* Unfreeze grab events. */
9106 xcb_allow_events(conn, XCB_ALLOW_ASYNC_KEYBOARD, e->time);
9109 DNPRINTF(SWM_D_EVENT, "keypress: done.\n");
9113 buttonpress(xcb_button_press_event_t *e)
9115 struct ws_win *win = NULL;
9116 struct swm_region *r, *old_r;
9118 bool handled = false;
9120 DNPRINTF(SWM_D_EVENT, "buttonpress: win (x,y): %#x (%d,%d), "
9121 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
9122 "state: %u, same_screen: %s\n", e->event, e->event_x, e->event_y,
9123 e->detail, e->time, e->root, e->root_x, e->root_y, e->child,
9124 e->state, YESNO(e->same_screen));
9126 if (e->event == e->root) {
9127 if (e->child != 0) {
9128 win = find_window(e->child);
9129 /* Pass ButtonPress to window if it isn't managed. */
9133 /* Focus on empty region */
9134 /* If no windows on region if its empty. */
9135 r = root_to_region(e->root, SWM_CK_POINTER);
9137 DNPRINTF(SWM_D_EVENT, "buttonpress: "
9138 "NULL region; ignoring.\n");
9142 if (TAILQ_EMPTY(&r->ws->winlist)) {
9143 old_r = root_to_region(e->root, SWM_CK_FOCUS);
9144 if (old_r && old_r != r)
9145 unfocus_win(old_r->ws->focus);
9147 xcb_set_input_focus(conn,
9148 XCB_INPUT_FOCUS_PARENT, e->root, e->time);
9150 /* Clear bar since empty. */
9158 win = find_window(e->event);
9164 last_event_time = e->time;
9166 focus_win(get_focus_magic(win));
9168 for (i = 0; i < LENGTH(buttons); i++)
9169 if (client_click == buttons[i].action && buttons[i].func &&
9170 buttons[i].button == e->detail &&
9171 CLEANMASK(buttons[i].mask) == CLEANMASK(e->state)) {
9172 buttons[i].func(win, &buttons[i].args);
9178 DNPRINTF(SWM_D_EVENT, "buttonpress: passing to window.\n");
9179 /* Replay event to event window */
9180 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, e->time);
9182 DNPRINTF(SWM_D_EVENT, "buttonpress: handled.\n");
9183 /* Unfreeze grab events. */
9184 xcb_allow_events(conn, XCB_ALLOW_SYNC_POINTER, e->time);
9192 print_win_geom(xcb_window_t w)
9194 xcb_get_geometry_reply_t *wa;
9196 wa = xcb_get_geometry_reply(conn, xcb_get_geometry(conn, w), NULL);
9198 DNPRINTF(SWM_D_MISC, "print_win_geom: window not found: %#x\n",
9203 DNPRINTF(SWM_D_MISC, "print_win_geom: win %#x, root: %#x, "
9204 "depth: %u, (x,y) w x h: (%d,%d) %d x %d, border: %d\n",
9205 w, wa->root, wa->depth, wa->x, wa->y, wa->width, wa->height,
9214 get_stack_mode_name(uint8_t mode)
9219 case XCB_STACK_MODE_ABOVE:
9222 case XCB_STACK_MODE_BELOW:
9225 case XCB_STACK_MODE_TOP_IF:
9228 case XCB_STACK_MODE_BOTTOM_IF:
9231 case XCB_STACK_MODE_OPPOSITE:
9243 configurerequest(xcb_configure_request_event_t *e)
9246 struct swm_region *r = NULL;
9248 uint32_t wc[7] = {0};
9252 if ((win = find_window(e->window)) == NULL)
9253 if ((win = find_unmanaged_window(e->window)) == NULL)
9257 if (swm_debug & SWM_D_EVENT) {
9258 print_win_geom(e->window);
9260 DNPRINTF(SWM_D_EVENT, "configurerequest: win %#x, "
9261 "parent: %#x, new: %s, value_mask: %u { ", e->window,
9262 e->parent, YESNO(new), e->value_mask);
9263 if (e->value_mask & XCB_CONFIG_WINDOW_X)
9264 DPRINTF("X: %d ", e->x);
9265 if (e->value_mask & XCB_CONFIG_WINDOW_Y)
9266 DPRINTF("Y: %d ", e->y);
9267 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH)
9268 DPRINTF("W: %u ", e->width);
9269 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
9270 DPRINTF("H: %u ", e->height);
9271 if (e->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH)
9272 DPRINTF("Border: %u ", e->border_width);
9273 if (e->value_mask & XCB_CONFIG_WINDOW_SIBLING)
9274 DPRINTF("Sibling: %#x ", e->sibling);
9275 if (e->value_mask & XCB_CONFIG_WINDOW_STACK_MODE)
9276 DPRINTF("StackMode: %s(%u) ",
9277 get_stack_mode_name(e->stack_mode), e->stack_mode);
9283 if (e->value_mask & XCB_CONFIG_WINDOW_X) {
9284 mask |= XCB_CONFIG_WINDOW_X;
9287 if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
9288 mask |= XCB_CONFIG_WINDOW_Y;
9291 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
9292 mask |= XCB_CONFIG_WINDOW_WIDTH;
9295 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
9296 mask |= XCB_CONFIG_WINDOW_HEIGHT;
9297 wc[i++] = e->height;
9299 if (e->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH) {
9300 mask |= XCB_CONFIG_WINDOW_BORDER_WIDTH;
9301 wc[i++] = e->border_width;
9303 if (e->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
9304 mask |= XCB_CONFIG_WINDOW_SIBLING;
9305 wc[i++] = e->sibling;
9307 if (e->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
9308 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
9309 wc[i++] = e->stack_mode;
9313 xcb_configure_window(conn, e->window, mask, wc);
9316 } else if ((!MANUAL(win) || win->quirks & SWM_Q_ANYWHERE) &&
9317 !FULLSCREEN(win) && !MAXIMIZED(win)) {
9320 else if (win->ws->old_r)
9323 /* windows are centered unless ANYWHERE quirk is set. */
9324 if (win->quirks & SWM_Q_ANYWHERE) {
9325 if (e->value_mask & XCB_CONFIG_WINDOW_X) {
9326 win->g_float.x = e->x;
9328 win->g_float.x -= X(r);
9331 if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
9332 win->g_float.y = e->y;
9334 win->g_float.y -= Y(r);
9338 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH)
9339 win->g_float.w = e->width;
9341 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
9342 win->g_float.h = e->height;
9344 win->g_floatvalid = true;
9346 if (!MAXIMIZED(win) && !FULLSCREEN(win) &&
9347 (TRANS(win) || (ABOVE(win) &&
9348 win->ws->cur_layout != &layouts[SWM_MAX_STACK]))) {
9349 WIDTH(win) = win->g_float.w;
9350 HEIGHT(win) = win->g_float.h;
9353 update_floater(win);
9368 DNPRINTF(SWM_D_EVENT, "configurerequest: done.\n");
9372 configurenotify(xcb_configure_notify_event_t *e)
9376 DNPRINTF(SWM_D_EVENT, "configurenotify: win %#x, event win: %#x, "
9377 "(x,y) WxH: (%d,%d) %ux%u, border: %u, above_sibling: %#x, "
9378 "override_redirect: %s\n", e->window, e->event, e->x, e->y,
9379 e->width, e->height, e->border_width, e->above_sibling,
9380 YESNO(e->override_redirect));
9382 win = find_window(e->window);
9384 xcb_icccm_get_wm_normal_hints_reply(conn,
9385 xcb_icccm_get_wm_normal_hints(conn, win->id),
9388 if (font_adjusted) {
9396 destroynotify(xcb_destroy_notify_event_t *e)
9400 DNPRINTF(SWM_D_EVENT, "destroynotify: win %#x\n", e->window);
9402 if ((win = find_window(e->window)) == NULL) {
9403 if ((win = find_unmanaged_window(e->window)) == NULL)
9409 if (focus_mode != SWM_FOCUS_FOLLOW) {
9410 /* If we were focused, make sure we focus on something else. */
9411 if (win == win->ws->focus)
9412 win->ws->focus_pending = get_focus_prev(win);
9415 unmanage_window(win);
9418 if (focus_mode != SWM_FOCUS_FOLLOW && WS_FOCUSED(win->ws)) {
9419 if (win->ws->focus_pending) {
9420 focus_win(win->ws->focus_pending);
9421 win->ws->focus_pending = NULL;
9422 } else if (win == win->ws->focus) {
9423 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT,
9424 win->ws->r->id, XCB_CURRENT_TIME);
9435 get_notify_detail_label(uint8_t detail)
9440 case XCB_NOTIFY_DETAIL_ANCESTOR:
9443 case XCB_NOTIFY_DETAIL_VIRTUAL:
9446 case XCB_NOTIFY_DETAIL_INFERIOR:
9449 case XCB_NOTIFY_DETAIL_NONLINEAR:
9450 label = "Nonlinear";
9452 case XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL:
9453 label = "NonlinearVirtual";
9455 case XCB_NOTIFY_DETAIL_POINTER:
9458 case XCB_NOTIFY_DETAIL_POINTER_ROOT:
9459 label = "PointerRoot";
9461 case XCB_NOTIFY_DETAIL_NONE:
9472 get_notify_mode_label(uint8_t mode)
9477 case XCB_NOTIFY_MODE_NORMAL:
9480 case XCB_NOTIFY_MODE_GRAB:
9483 case XCB_NOTIFY_MODE_UNGRAB:
9486 case XCB_NOTIFY_MODE_WHILE_GRABBED:
9487 label = "WhileGrabbed";
9498 enternotify(xcb_enter_notify_event_t *e)
9501 struct swm_region *r;
9503 DNPRINTF(SWM_D_FOCUS, "enternotify: time: %u, win (x,y): %#x "
9504 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
9505 "child: %#x, same_screen_focus: %s, state: %d\n",
9506 e->time, e->event, e->event_x, e->event_y,
9507 get_notify_mode_label(e->mode), e->mode,
9508 get_notify_detail_label(e->detail), e->detail,
9509 e->root, e->root_x, e->root_y, e->child,
9510 YESNO(e->same_screen_focus), e->state);
9512 if (focus_mode == SWM_FOCUS_MANUAL &&
9513 e->mode == XCB_NOTIFY_MODE_NORMAL) {
9514 DNPRINTF(SWM_D_EVENT, "enternotify: manual focus; ignoring.\n");
9518 last_event_time = e->time;
9520 if ((win = find_window(e->event)) == NULL) {
9521 if (e->event == e->root) {
9522 /* If no windows on pointer region, then focus root. */
9523 r = root_to_region(e->root, SWM_CK_POINTER);
9525 DNPRINTF(SWM_D_EVENT, "enternotify: "
9526 "NULL region; ignoring.\n");
9532 DNPRINTF(SWM_D_EVENT, "enternotify: window is NULL; "
9537 if (e->mode == XCB_NOTIFY_MODE_NORMAL &&
9538 e->detail == XCB_NOTIFY_DETAIL_INFERIOR) {
9539 DNPRINTF(SWM_D_EVENT, "enternotify: entering from "
9540 "inferior; ignoring\n");
9544 focus_win(get_focus_magic(win));
9552 leavenotify(xcb_leave_notify_event_t *e)
9554 DNPRINTF(SWM_D_FOCUS, "leavenotify: time: %u, win (x,y): %#x "
9555 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
9556 "child: %#x, same_screen_focus: %s, state: %d\n",
9557 e->time, e->event, e->event_x, e->event_y,
9558 get_notify_mode_label(e->mode), e->mode,
9559 get_notify_detail_label(e->detail), e->detail,
9560 e->root, e->root_x, e->root_y, e->child,
9561 YESNO(e->same_screen_focus), e->state);
9566 mapnotify(xcb_map_notify_event_t *e)
9568 struct ws_win *win, *parent = NULL;
9569 struct workspace *ws;
9571 DNPRINTF(SWM_D_EVENT, "mapnotify: win %#x\n", e->window);
9573 if ((win = manage_window(e->window, spawn_position, true)) == NULL)
9577 /* Need to know if win was mapped due to ws switch. */
9578 if (ws->state == SWM_WS_STATE_MAPPED) {
9579 if (ws->focus_pending && TRANS(ws->focus_pending))
9580 parent = find_window(win->transient);
9582 /* If window's parent is maximized, don't clear it. */
9583 if ((parent == NULL) || !MAXIMIZED(parent))
9584 if (clear_maximized(ws) > 0)
9589 set_win_state(win, XCB_ICCCM_WM_STATE_NORMAL);
9591 if (focus_mode != SWM_FOCUS_FOLLOW && WS_FOCUSED(win->ws)) {
9592 if (ws->focus_pending == win) {
9594 ws->focus_pending = NULL;
9603 mappingnotify(xcb_mapping_notify_event_t *e)
9606 int i, j, num_screens;
9608 xcb_refresh_keyboard_mapping(syms, e);
9610 if (e->request == XCB_MAPPING_KEYBOARD) {
9613 /* Regrab buttons on all managed windows. */
9614 num_screens = get_screen_count();
9615 for (i = 0; i < num_screens; i++)
9616 for (j = 0; j < workspace_limit; j++)
9617 TAILQ_FOREACH(w, &screens[i].ws[j].winlist,
9624 maprequest(xcb_map_request_event_t *e)
9626 struct ws_win *win, *w = NULL;
9627 xcb_get_window_attributes_reply_t *war;
9629 DNPRINTF(SWM_D_EVENT, "maprequest: win %#x\n",
9632 war = xcb_get_window_attributes_reply(conn,
9633 xcb_get_window_attributes(conn, e->window),
9636 DNPRINTF(SWM_D_EVENT, "maprequest: window lost.\n");
9640 if (war->override_redirect) {
9641 DNPRINTF(SWM_D_EVENT, "maprequest: override_redirect; "
9646 win = manage_window(e->window, spawn_position,
9647 (war->map_state == XCB_MAP_STATE_VIEWABLE));
9651 /* The new window should get focus; prepare. */
9652 if (focus_mode != SWM_FOCUS_FOLLOW &&
9653 !(win->quirks & SWM_Q_NOFOCUSONMAP) &&
9654 (!(win->hints.flags & XCB_ICCCM_WM_HINT_INPUT) ||
9655 (win->hints.flags & XCB_ICCCM_WM_HINT_INPUT &&
9656 win->hints.input))) {
9657 if (win->quirks & SWM_Q_FOCUSONMAP_SINGLE) {
9658 /* See if other wins of same type are already mapped. */
9659 TAILQ_FOREACH(w, &win->ws->winlist, entry) {
9660 if (w == win || !w->mapped)
9663 if (w->ch.class_name &&
9664 win->ch.class_name &&
9665 strcmp(w->ch.class_name,
9666 win->ch.class_name) == 0 &&
9667 w->ch.instance_name &&
9668 win->ch.instance_name &&
9669 strcmp(w->ch.instance_name,
9670 win->ch.instance_name) == 0)
9676 win->ws->focus_pending = get_focus_magic(win);
9679 /* All windows need to be mapped if they are in the current workspace.*/
9683 /* Ignore EnterNotify to handle the mapnotify without interference. */
9684 if (focus_mode == SWM_FOCUS_DEFAULT)
9685 event_drain(XCB_ENTER_NOTIFY);
9688 DNPRINTF(SWM_D_EVENT, "maprequest: done.\n");
9692 motionnotify(xcb_motion_notify_event_t *e)
9694 struct swm_region *r = NULL;
9697 DNPRINTF(SWM_D_FOCUS, "motionnotify: time: %u, win (x,y): %#x "
9698 "(%d,%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
9699 "child: %#x, same_screen_focus: %s, state: %d\n",
9700 e->time, e->event, e->event_x, e->event_y,
9701 get_notify_detail_label(e->detail), e->detail,
9702 e->root, e->root_x, e->root_y, e->child,
9703 YESNO(e->same_screen), e->state);
9705 last_event_time = e->time;
9707 if (focus_mode == SWM_FOCUS_MANUAL)
9710 num_screens = get_screen_count();
9711 for (i = 0; i < num_screens; i++)
9712 if (screens[i].root == e->root)
9715 TAILQ_FOREACH(r, &screens[i].rl, entry)
9716 if (X(r) <= e->root_x && e->root_x < MAX_X(r) &&
9717 Y(r) <= e->root_y && e->root_y < MAX_Y(r))
9725 get_atom_name(xcb_atom_t atom)
9728 #ifdef SWM_DEBUG_ATOM_NAMES
9730 * This should be disabled during most debugging since
9731 * xcb_get_* causes an xcb_flush.
9734 xcb_get_atom_name_reply_t *r;
9736 r = xcb_get_atom_name_reply(conn,
9737 xcb_get_atom_name(conn, atom),
9740 len = xcb_get_atom_name_name_length(r);
9742 name = malloc(len + 1);
9744 memcpy(name, xcb_get_atom_name_name(r), len);
9758 propertynotify(xcb_property_notify_event_t *e)
9761 struct workspace *ws;
9765 name = get_atom_name(e->atom);
9766 DNPRINTF(SWM_D_EVENT, "propertynotify: win %#x, atom: %s(%u), "
9767 "time: %#x, state: %u\n", e->window, name, e->atom, e->time,
9771 win = find_window(e->window);
9777 last_event_time = e->time;
9779 if (e->atom == a_state) {
9780 /* State just changed, make sure it gets focused if mapped. */
9781 if (e->state == XCB_PROPERTY_NEW_VALUE) {
9782 if (focus_mode != SWM_FOCUS_FOLLOW && WS_FOCUSED(ws)) {
9784 ws->focus_pending == win) {
9785 focus_win(ws->focus_pending);
9786 ws->focus_pending = NULL;
9790 } else if (e->atom == XCB_ATOM_WM_CLASS ||
9791 e->atom == XCB_ATOM_WM_NAME) {
9793 } else if (e->atom == a_prot) {
9794 get_wm_protocols(win);
9801 unmapnotify(xcb_unmap_notify_event_t *e)
9804 struct workspace *ws;
9806 DNPRINTF(SWM_D_EVENT, "unmapnotify: win %#x\n", e->window);
9808 /* If we aren't managing the window, then ignore. */
9809 win = find_window(e->window);
9810 if (win == NULL || win->id != e->window)
9813 /* Do nothing if already withdrawn. */
9814 if (!win->mapped && !ICONIC(win))
9818 win->mapped = false;
9820 /* If win was focused, make sure to focus on something else. */
9821 if (win == ws->focus) {
9822 if (focus_mode != SWM_FOCUS_FOLLOW) {
9823 ws->focus_pending = get_focus_prev(win);
9824 DNPRINTF(SWM_D_EVENT, "unmapnotify: "
9825 "focus_pending: %#x\n",
9826 WINID(ws->focus_pending));
9834 set_win_state(win, XCB_ICCCM_WM_STATE_ICONIC);
9837 set_win_state(win, XCB_ICCCM_WM_STATE_WITHDRAWN);
9838 unmanage_window(win);
9844 /* Update focus if ws is active. */
9845 if (WS_FOCUSED(ws)) {
9846 if (focus_mode == SWM_FOCUS_FOLLOW) {
9847 focus_win(get_pointer_win(ws->r->s->root));
9848 } else if (ws->focus_pending) {
9849 focus_win(ws->focus_pending);
9850 ws->focus_pending = NULL;
9851 } else if (ws->focus == NULL) {
9852 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT,
9853 ws->r->id, XCB_CURRENT_TIME);
9862 get_source_type_label(uint32_t type)
9867 case EWMH_SOURCE_TYPE_NONE:
9870 case EWMH_SOURCE_TYPE_NORMAL:
9873 case EWMH_SOURCE_TYPE_OTHER:
9885 clientmessage(xcb_client_message_event_t *e)
9888 struct swm_region *r = NULL;
9892 xcb_map_request_event_t mre;
9896 name = get_atom_name(e->type);
9897 DNPRINTF(SWM_D_EVENT, "clientmessage: win %#x, atom: %s(%u)\n",
9898 e->window, name, e->type);
9902 if (e->type == ewmh[_NET_CURRENT_DESKTOP].atom) {
9903 num_screens = get_screen_count();
9904 for (i = 0; i < num_screens; i++)
9905 if (screens[i].root == e->window) {
9906 r = screens[i].r_focus;
9910 if (r && e->data.data32[0] < (uint32_t)workspace_limit) {
9911 a.id = e->data.data32[0];
9919 win = find_window(e->window);
9921 if (e->type == ewmh[_NET_ACTIVE_WINDOW].atom) {
9922 /* Manage the window with maprequest. */
9923 DNPRINTF(SWM_D_EVENT, "clientmessage: request focus on "
9924 "unmanaged window.\n");
9925 mre.window = e->window;
9931 if (e->type == ewmh[_NET_ACTIVE_WINDOW].atom) {
9932 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_ACTIVE_WINDOW, "
9933 "source_type: %s(%d)\n",
9934 get_source_type_label(e->data.data32[0]),
9938 * Allow focus changes that are a result of direct user
9939 * action and from applications that use the old EWMH spec.
9941 if (e->data.data32[0] != EWMH_SOURCE_TYPE_NORMAL ||
9942 win->quirks & SWM_Q_OBEYAPPFOCUSREQ) {
9943 if (WS_FOCUSED(win->ws))
9946 win->ws->focus_pending = win;
9948 } else if (e->type == ewmh[_NET_CLOSE_WINDOW].atom) {
9949 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_CLOSE_WINDOW\n");
9950 if (win->can_delete)
9951 client_msg(win, a_delete, 0);
9953 xcb_kill_client(conn, win->id);
9954 } else if (e->type == ewmh[_NET_MOVERESIZE_WINDOW].atom) {
9955 DNPRINTF(SWM_D_EVENT,
9956 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
9958 if (e->data.data32[0] & (1<<8)) /* x */
9959 X(win) = e->data.data32[1];
9960 if (e->data.data32[0] & (1<<9)) /* y */
9961 Y(win) = e->data.data32[2];
9962 if (e->data.data32[0] & (1<<10)) /* width */
9963 WIDTH(win) = e->data.data32[3];
9964 if (e->data.data32[0] & (1<<11)) /* height */
9965 HEIGHT(win) = e->data.data32[4];
9969 /* Notify no change was made. */
9970 config_win(win, NULL);
9971 /* TODO: Change stack sizes */
9973 } else if (e->type == ewmh[_NET_RESTACK_WINDOW].atom) {
9974 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_RESTACK_WINDOW\n");
9975 val[0] = e->data.data32[1]; /* Sibling window. */
9976 val[1] = e->data.data32[2]; /* Stack mode detail. */
9978 xcb_configure_window(conn, win->id, XCB_CONFIG_WINDOW_SIBLING |
9979 XCB_CONFIG_WINDOW_STACK_MODE, val);
9980 } else if (e->type == ewmh[_NET_WM_STATE].atom) {
9981 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_WM_STATE\n");
9982 ewmh_change_wm_state(win, e->data.data32[1], e->data.data32[0]);
9983 if (e->data.data32[2])
9984 ewmh_change_wm_state(win, e->data.data32[2],
9987 ewmh_update_wm_state(win);
9989 } else if (e->type == ewmh[_NET_WM_DESKTOP].atom) {
9990 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_WM_DESKTOP\n");
9993 win_to_ws(win, e->data.data32[0], true);
9995 /* Restack if either the source or destination ws is mapped. */
9996 if (r != NULL || win->ws->r != NULL) {
9998 load_float_geom(win);
10010 int errcode = xcb_connection_has_error(conn);
10011 #ifdef XCB_CONN_ERROR
10014 case XCB_CONN_ERROR:
10015 s = "Socket error, pipe error or other stream error.";
10017 case XCB_CONN_CLOSED_EXT_NOTSUPPORTED:
10018 s = "Extension not supported.";
10020 case XCB_CONN_CLOSED_MEM_INSUFFICIENT:
10021 s = "Insufficient memory.";
10023 case XCB_CONN_CLOSED_REQ_LEN_EXCEED:
10024 s = "Request length was exceeded.";
10026 case XCB_CONN_CLOSED_PARSE_ERR:
10027 s = "Error parsing display string.";
10030 s = "Unknown error.";
10033 errx(errcode, "X CONNECTION ERROR: %s", s);
10036 errx(errcode, "X CONNECTION ERROR");
10043 int num_screens, i;
10044 const uint32_t val = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
10045 XCB_EVENT_MASK_ENTER_WINDOW;
10047 xcb_void_cookie_t wac;
10048 xcb_generic_error_t *error;
10050 /* this causes an error if some other window manager is running */
10051 num_screens = get_screen_count();
10052 for (i = 0; i < num_screens; i++) {
10053 if ((sc = get_screen(i)) == NULL)
10054 errx(1, "ERROR: can't get screen %d.", i);
10055 DNPRINTF(SWM_D_INIT, "enable_wm: screen %d, root: %#x\n",
10057 wac = xcb_change_window_attributes_checked(conn, sc->root,
10058 XCB_CW_EVENT_MASK, &val);
10059 if ((error = xcb_request_check(conn, wac))) {
10060 DNPRINTF(SWM_D_INIT, "enable_wm: error_code: %u\n",
10061 error->error_code);
10066 /* click to focus on empty region */
10067 xcb_grab_button(conn, 1, sc->root, BUTTONMASK,
10068 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC, XCB_WINDOW_NONE,
10069 XCB_CURSOR_NONE, XCB_BUTTON_INDEX_1, XCB_BUTTON_MASK_ANY);
10076 new_region(struct swm_screen *s, int x, int y, int w, int h)
10078 struct swm_region *r = NULL, *n;
10079 struct workspace *ws = NULL;
10083 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
10084 s->idx, w, h, x, y);
10086 /* remove any conflicting regions */
10087 n = TAILQ_FIRST(&s->rl);
10090 n = TAILQ_NEXT(r, entry);
10091 if (X(r) < (x + w) && (X(r) + WIDTH(r)) > x &&
10092 Y(r) < (y + h) && (Y(r) + HEIGHT(r)) > y) {
10093 if (r->ws->r != NULL)
10094 r->ws->old_r = r->ws->r;
10097 xcb_destroy_window(conn, r->id);
10098 TAILQ_REMOVE(&s->rl, r, entry);
10099 TAILQ_INSERT_TAIL(&s->orl, r, entry);
10103 /* search old regions for one to reuse */
10105 /* size + location match */
10106 TAILQ_FOREACH(r, &s->orl, entry)
10107 if (X(r) == x && Y(r) == y &&
10108 HEIGHT(r) == h && WIDTH(r) == w)
10112 TAILQ_FOREACH(r, &s->orl, entry)
10113 if (HEIGHT(r) == h && WIDTH(r) == w)
10117 TAILQ_REMOVE(&s->orl, r, entry);
10118 /* try to use old region's workspace */
10119 if (r->ws->r == NULL)
10122 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
10123 err(1, "new_region: calloc: failed to allocate memory "
10126 /* if we don't have a workspace already, find one */
10128 for (i = 0; i < workspace_limit; i++)
10129 if (s->ws[i].r == NULL) {
10136 errx(1, "new_region: no free workspaces");
10138 if (ws->state == SWM_WS_STATE_HIDDEN)
10139 ws->state = SWM_WS_STATE_MAPPING;
10147 r->ws_prior = NULL;
10150 TAILQ_INSERT_TAIL(&s->rl, r, entry);
10152 /* Invisible region window to detect pointer events on empty regions. */
10153 r->id = xcb_generate_id(conn);
10154 wa[0] = XCB_EVENT_MASK_POINTER_MOTION |
10155 XCB_EVENT_MASK_POINTER_MOTION_HINT;
10157 xcb_create_window(conn, XCB_COPY_FROM_PARENT, r->id, r->s->root,
10158 X(r), Y(r), WIDTH(r), HEIGHT(r), 0, XCB_WINDOW_CLASS_INPUT_ONLY,
10159 XCB_COPY_FROM_PARENT, XCB_CW_EVENT_MASK, wa);
10161 /* Make sure region input is at the bottom. */
10162 wa[0] = XCB_STACK_MODE_BELOW;
10163 xcb_configure_window(conn, r->id, XCB_CONFIG_WINDOW_STACK_MODE, wa);
10165 xcb_map_window(conn, r->id);
10169 scan_randr(int idx)
10171 #ifdef SWM_XRR_HAS_CRTC
10174 #endif /* SWM_XRR_HAS_CRTC */
10175 struct swm_region *r;
10176 struct ws_win *win;
10178 xcb_randr_get_screen_resources_current_cookie_t src;
10179 xcb_randr_get_screen_resources_current_reply_t *srr;
10180 xcb_randr_get_crtc_info_cookie_t cic;
10181 xcb_randr_get_crtc_info_reply_t *cir = NULL;
10182 xcb_randr_crtc_t *crtc;
10183 xcb_screen_t *screen;
10185 DNPRINTF(SWM_D_MISC, "scan_randr: screen: %d\n", idx);
10187 if ((screen = get_screen(idx)) == NULL)
10188 errx(1, "ERROR: can't get screen %d.", idx);
10190 num_screens = get_screen_count();
10191 if (idx >= num_screens)
10192 errx(1, "scan_randr: invalid screen");
10194 /* remove any old regions */
10195 while ((r = TAILQ_FIRST(&screens[idx].rl)) != NULL) {
10196 r->ws->old_r = r->ws->r = NULL;
10198 xcb_destroy_window(conn, r->id);
10199 TAILQ_REMOVE(&screens[idx].rl, r, entry);
10200 TAILQ_INSERT_TAIL(&screens[idx].orl, r, entry);
10204 /* map virtual screens onto physical screens */
10205 #ifdef SWM_XRR_HAS_CRTC
10206 if (randr_support) {
10207 src = xcb_randr_get_screen_resources_current(conn,
10208 screens[idx].root);
10209 srr = xcb_randr_get_screen_resources_current_reply(conn, src,
10212 new_region(&screens[idx], 0, 0,
10213 screen->width_in_pixels,
10214 screen->height_in_pixels);
10217 ncrtc = srr->num_crtcs;
10219 crtc = xcb_randr_get_screen_resources_current_crtcs(srr);
10220 for (c = 0; c < ncrtc; c++) {
10221 cic = xcb_randr_get_crtc_info(conn, crtc[c],
10223 cir = xcb_randr_get_crtc_info_reply(conn, cic, NULL);
10226 if (cir->num_outputs == 0) {
10231 if (cir->mode == 0)
10232 new_region(&screens[idx], 0, 0,
10233 screen->width_in_pixels,
10234 screen->height_in_pixels);
10236 new_region(&screens[idx],
10237 cir->x, cir->y, cir->width, cir->height);
10242 #endif /* SWM_XRR_HAS_CRTC */
10244 /* If detection failed, create a single region that spans the screen. */
10245 if (TAILQ_EMPTY(&screens[idx].rl))
10246 new_region(&screens[idx], 0, 0, screen->width_in_pixels,
10247 screen->height_in_pixels);
10250 /* Cleanup unused previously visible workspaces. */
10251 TAILQ_FOREACH(r, &screens[idx].orl, entry) {
10252 TAILQ_FOREACH(win, &r->ws->winlist, entry)
10254 r->ws->state = SWM_WS_STATE_HIDDEN;
10256 /* The screen shouldn't focus on an unused region. */
10257 if (screens[idx].r_focus == r)
10258 screens[idx].r_focus = NULL;
10261 DNPRINTF(SWM_D_MISC, "scan_randr: done.\n");
10265 screenchange(xcb_randr_screen_change_notify_event_t *e)
10267 struct swm_region *r;
10268 int i, num_screens;
10270 DNPRINTF(SWM_D_EVENT, "screenchange: root: %#x\n", e->root);
10272 num_screens = get_screen_count();
10273 /* silly event doesn't include the screen index */
10274 for (i = 0; i < num_screens; i++)
10275 if (screens[i].root == e->root)
10277 if (i >= num_screens)
10278 errx(1, "screenchange: screen not found");
10280 /* brute force for now, just re-enumerate the regions */
10284 print_win_geom(e->root);
10286 /* add bars to all regions */
10287 for (i = 0; i < num_screens; i++) {
10288 TAILQ_FOREACH(r, &screens[i].rl, entry)
10294 /* Make sure a region has focus on each screen. */
10295 for (i = 0; i < num_screens; i++) {
10296 if (screens[i].r_focus == NULL) {
10297 r = TAILQ_FIRST(&screens[i].rl);
10306 /* Update workspace state on all regions. */
10307 for (i = 0; i < num_screens; i++)
10308 TAILQ_FOREACH(r, &screens[i].rl, entry)
10309 r->ws->state = SWM_WS_STATE_MAPPED;
10315 struct swm_region *r = NULL;
10316 xcb_window_t *wins = NULL, trans, *cwins = NULL;
10317 int i, j, k, n, no, num_screens;
10319 bool manage, mapped;
10321 xcb_query_tree_cookie_t qtc;
10322 xcb_query_tree_reply_t *qtr;
10323 xcb_get_window_attributes_cookie_t gac;
10324 xcb_get_window_attributes_reply_t *gar;
10325 xcb_get_property_cookie_t pc;
10326 xcb_get_property_reply_t *pr;
10328 DNPRINTF(SWM_D_INIT, "grab_windows: begin\n");
10329 num_screens = get_screen_count();
10330 for (i = 0; i < num_screens; i++) {
10331 qtc = xcb_query_tree(conn, screens[i].root);
10332 qtr = xcb_query_tree_reply(conn, qtc, NULL);
10335 wins = xcb_query_tree_children(qtr);
10336 no = xcb_query_tree_children_length(qtr);
10338 /* Try to sort windows according to _NET_CLIENT_LIST. */
10339 pr = xcb_get_property_reply(conn, xcb_get_property(conn, 0,
10340 screens[i].root, ewmh[_NET_CLIENT_LIST].atom,
10341 XCB_ATOM_WINDOW, 0, UINT32_MAX), NULL);
10343 cwins = xcb_get_property_value(pr);
10344 n = xcb_get_property_value_length(pr) /
10345 sizeof(xcb_atom_t);
10347 for (j = 0; j < n; ++j) {
10348 for (k = j; k < no; ++k) {
10349 if (wins[k] == cwins[j]) {
10350 /* Swap wins j and k. */
10352 wins[j] = cwins[j];
10360 /* attach windows to a region */
10361 /* normal windows */
10362 DNPRINTF(SWM_D_INIT, "grab_windows: grab top level windows.\n");
10363 for (j = 0; j < no; j++) {
10364 TAILQ_FOREACH(r, &screens[i].rl, entry) {
10365 if (r->id == wins[j]) {
10366 DNPRINTF(SWM_D_INIT, "grab_windows: "
10367 "skip %#x; region input window.\n",
10370 } else if (r->bar->id == wins[j]) {
10371 DNPRINTF(SWM_D_INIT, "grab_windows: "
10372 "skip %#x; region bar.\n",
10381 gac = xcb_get_window_attributes(conn, wins[j]);
10382 gar = xcb_get_window_attributes_reply(conn, gac, NULL);
10384 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
10385 "doesn't exist.\n", wins[j]);
10389 if (gar->override_redirect) {
10390 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
10391 "override_redirect set.\n", wins[j]);
10396 pc = xcb_icccm_get_wm_transient_for(conn, wins[j]);
10397 if (xcb_icccm_get_wm_transient_for_reply(conn, pc,
10399 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
10400 "is transient for %#x.\n", wins[j], trans);
10405 state = get_win_state(wins[j]);
10406 manage = state != XCB_ICCCM_WM_STATE_WITHDRAWN;
10407 mapped = gar->map_state == XCB_MAP_STATE_VIEWABLE;
10408 if (mapped || manage)
10409 manage_window(wins[j], SWM_STACK_TOP, mapped);
10412 /* transient windows */
10413 DNPRINTF(SWM_D_INIT, "grab_windows: grab transient windows.\n");
10414 for (j = 0; j < no; j++) {
10415 gac = xcb_get_window_attributes(conn, wins[j]);
10416 gar = xcb_get_window_attributes_reply(conn, gac, NULL);
10418 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
10419 "doesn't exist.\n", wins[j]);
10423 if (gar->override_redirect) {
10424 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
10425 "override_redirect set.\n", wins[j]);
10430 state = get_win_state(wins[j]);
10431 manage = state != XCB_ICCCM_WM_STATE_WITHDRAWN;
10432 mapped = gar->map_state == XCB_MAP_STATE_VIEWABLE;
10433 pc = xcb_icccm_get_wm_transient_for(conn, wins[j]);
10434 if (xcb_icccm_get_wm_transient_for_reply(conn, pc,
10435 &trans, NULL) && manage)
10436 manage_window(wins[j], SWM_STACK_TOP, mapped);
10441 DNPRINTF(SWM_D_INIT, "grab_windows: done.\n");
10445 setup_screens(void)
10447 int i, j, k, num_screens;
10448 struct workspace *ws;
10449 uint32_t gcv[1], wa[1];
10450 const xcb_query_extension_reply_t *qep;
10451 xcb_screen_t *screen;
10452 xcb_randr_query_version_cookie_t c;
10453 xcb_randr_query_version_reply_t *r;
10455 num_screens = get_screen_count();
10456 if ((screens = calloc(num_screens,
10457 sizeof(struct swm_screen))) == NULL)
10458 err(1, "setup_screens: calloc: failed to allocate memory for "
10461 /* Initial RandR setup. */
10462 randr_support = false;
10463 qep = xcb_get_extension_data(conn, &xcb_randr_id);
10464 if (qep->present) {
10465 c = xcb_randr_query_version(conn, 1, 1);
10466 r = xcb_randr_query_version_reply(conn, c, NULL);
10468 if (r->major_version >= 1) {
10469 randr_support = true;
10470 randr_eventbase = qep->first_event;
10476 wa[0] = cursors[XC_LEFT_PTR].cid;
10478 /* map physical screens */
10479 for (i = 0; i < num_screens; i++) {
10480 DNPRINTF(SWM_D_WS, "setup_screens: init screen: %d\n", i);
10481 screens[i].idx = i;
10482 screens[i].r_focus = NULL;
10484 TAILQ_INIT(&screens[i].rl);
10485 TAILQ_INIT(&screens[i].orl);
10486 if ((screen = get_screen(i)) == NULL)
10487 errx(1, "ERROR: can't get screen %d.", i);
10488 screens[i].root = screen->root;
10490 /* set default colors */
10491 setscreencolor("red", i, SWM_S_COLOR_FOCUS);
10492 setscreencolor("rgb:88/88/88", i, SWM_S_COLOR_UNFOCUS);
10493 setscreencolor("rgb:00/80/80", i, SWM_S_COLOR_BAR_BORDER);
10494 setscreencolor("rgb:00/40/40", i,
10495 SWM_S_COLOR_BAR_BORDER_UNFOCUS);
10496 setscreencolor("black", i, SWM_S_COLOR_BAR);
10497 setscreencolor("rgb:a0/a0/a0", i, SWM_S_COLOR_BAR_FONT);
10498 setscreencolor("red", i, SWM_S_COLOR_FOCUS_MAXIMIZED);
10499 setscreencolor("rgb:88/88/88", i,
10500 SWM_S_COLOR_UNFOCUS_MAXIMIZED);
10502 /* create graphics context on screen */
10503 screens[i].bar_gc = xcb_generate_id(conn);
10505 xcb_create_gc(conn, screens[i].bar_gc, screens[i].root,
10506 XCB_GC_GRAPHICS_EXPOSURES, gcv);
10508 /* set default cursor */
10509 xcb_change_window_attributes(conn, screens[i].root,
10510 XCB_CW_CURSOR, wa);
10512 /* init all workspaces */
10513 /* XXX these should be dynamically allocated too */
10514 for (j = 0; j < SWM_WS_MAX; j++) {
10515 ws = &screens[i].ws[j];
10518 ws->bar_enabled = true;
10520 ws->focus_prev = NULL;
10521 ws->focus_pending = NULL;
10524 ws->state = SWM_WS_STATE_HIDDEN;
10525 TAILQ_INIT(&ws->stack);
10526 TAILQ_INIT(&ws->winlist);
10527 TAILQ_INIT(&ws->unmanagedlist);
10529 for (k = 0; layouts[k].l_stack != NULL; k++)
10530 if (layouts[k].l_config != NULL)
10531 layouts[k].l_config(ws,
10532 SWM_ARG_ID_STACKINIT);
10533 ws->cur_layout = &layouts[0];
10534 ws->cur_layout->l_string(ws);
10540 xcb_randr_select_input(conn, screens[i].root,
10541 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE);
10546 setup_globals(void)
10548 if ((bar_fonts = strdup(SWM_BAR_FONTS)) == NULL)
10549 err(1, "setup_globals: strdup: failed to allocate memory.");
10551 if ((clock_format = strdup("%a %b %d %R %Z %Y")) == NULL)
10552 err(1, "setup_globals: strdup: failed to allocate memory.");
10554 if ((syms = xcb_key_symbols_alloc(conn)) == NULL)
10555 errx(1, "unable to allocate key symbols");
10557 a_state = get_atom_from_string("WM_STATE");
10558 a_prot = get_atom_from_string("WM_PROTOCOLS");
10559 a_delete = get_atom_from_string("WM_DELETE_WINDOW");
10560 a_net_supported = get_atom_from_string("_NET_SUPPORTED");
10561 a_net_wm_check = get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
10562 a_takefocus = get_atom_from_string("WM_TAKE_FOCUS");
10563 a_utf8_string = get_atom_from_string("UTF8_STRING");
10564 a_swm_ws = get_atom_from_string("_SWM_WS");
10568 shutdown_cleanup(void)
10570 int i, num_screens;
10572 /* disable alarm because the following code may not be interrupted */
10574 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
10575 err(1, "can't disable alarm");
10584 num_screens = get_screen_count();
10585 for (i = 0; i < num_screens; ++i) {
10586 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT,
10587 screens[i].root, XCB_CURRENT_TIME);
10589 if (screens[i].bar_gc != XCB_NONE)
10590 xcb_free_gc(conn, screens[i].bar_gc);
10591 if (!bar_font_legacy) {
10592 XftColorFree(display, DefaultVisual(display, i),
10593 DefaultColormap(display, i), &bar_font_color);
10594 XftColorFree(display, DefaultVisual(display, i),
10595 DefaultColormap(display, i), &search_font_color);
10599 if (bar_font_legacy)
10600 XFreeFontSet(display, bar_fs);
10602 XftFontClose(display, bar_font);
10605 xcb_key_symbols_free(syms);
10607 xcb_disconnect(conn);
10611 event_error(xcb_generic_error_t *e)
10615 DNPRINTF(SWM_D_EVENT, "event_error: %s(%u) from %s(%u), sequence: %u, "
10616 "resource_id: %u, minor_code: %u\n",
10617 xcb_event_get_error_label(e->error_code), e->error_code,
10618 xcb_event_get_request_label(e->major_code), e->major_code,
10619 e->sequence, e->resource_id, e->minor_code);
10623 event_handle(xcb_generic_event_t *evt)
10625 uint8_t type = XCB_EVENT_RESPONSE_TYPE(evt);
10627 DNPRINTF(SWM_D_EVENT, "XCB Event: %s(%d), seq %u\n",
10628 xcb_event_get_label(XCB_EVENT_RESPONSE_TYPE(evt)),
10629 XCB_EVENT_RESPONSE_TYPE(evt), evt->sequence);
10632 #define EVENT(type, callback) case type: callback((void *)evt); return
10633 EVENT(0, event_error);
10634 EVENT(XCB_BUTTON_PRESS, buttonpress);
10635 /*EVENT(XCB_BUTTON_RELEASE, buttonpress);*/
10636 /*EVENT(XCB_CIRCULATE_NOTIFY, );*/
10637 /*EVENT(XCB_CIRCULATE_REQUEST, );*/
10638 EVENT(XCB_CLIENT_MESSAGE, clientmessage);
10639 /*EVENT(XCB_COLORMAP_NOTIFY, );*/
10640 EVENT(XCB_CONFIGURE_NOTIFY, configurenotify);
10641 EVENT(XCB_CONFIGURE_REQUEST, configurerequest);
10642 /*EVENT(XCB_CREATE_NOTIFY, );*/
10643 EVENT(XCB_DESTROY_NOTIFY, destroynotify);
10644 EVENT(XCB_ENTER_NOTIFY, enternotify);
10645 EVENT(XCB_EXPOSE, expose);
10647 EVENT(XCB_FOCUS_IN, focusin);
10648 EVENT(XCB_FOCUS_OUT, focusout);
10650 /*EVENT(XCB_GRAPHICS_EXPOSURE, );*/
10651 /*EVENT(XCB_GRAVITY_NOTIFY, );*/
10652 EVENT(XCB_KEY_PRESS, keypress);
10653 /*EVENT(XCB_KEY_RELEASE, keypress);*/
10654 /*EVENT(XCB_KEYMAP_NOTIFY, );*/
10656 EVENT(XCB_LEAVE_NOTIFY, leavenotify);
10658 EVENT(XCB_MAP_NOTIFY, mapnotify);
10659 EVENT(XCB_MAP_REQUEST, maprequest);
10660 EVENT(XCB_MAPPING_NOTIFY, mappingnotify);
10661 EVENT(XCB_MOTION_NOTIFY, motionnotify);
10662 /*EVENT(XCB_NO_EXPOSURE, );*/
10663 EVENT(XCB_PROPERTY_NOTIFY, propertynotify);
10664 /*EVENT(XCB_REPARENT_NOTIFY, );*/
10665 /*EVENT(XCB_RESIZE_REQUEST, );*/
10666 /*EVENT(XCB_SELECTION_CLEAR, );*/
10667 /*EVENT(XCB_SELECTION_NOTIFY, );*/
10668 /*EVENT(XCB_SELECTION_REQUEST, );*/
10669 EVENT(XCB_UNMAP_NOTIFY, unmapnotify);
10670 /*EVENT(XCB_VISIBILITY_NOTIFY, );*/
10673 if (type - randr_eventbase == XCB_RANDR_SCREEN_CHANGE_NOTIFY)
10674 screenchange((void *)evt);
10678 main(int argc, char *argv[])
10680 struct swm_region *r;
10681 char conf[PATH_MAX], *cfile = NULL;
10683 int xfd, i, num_screens;
10684 struct sigaction sact;
10685 xcb_generic_event_t *evt;
10690 bool stdin_ready = false, startup = true;
10692 /* suppress unused warning since var is needed */
10696 time_started = time(NULL);
10700 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION, buildstr);
10701 if (setlocale(LC_CTYPE, "") == NULL || setlocale(LC_TIME, "") == NULL)
10702 warnx("no locale support");
10704 /* handle some signals */
10705 bzero(&sact, sizeof(sact));
10706 sigemptyset(&sact.sa_mask);
10708 sact.sa_handler = sighdlr;
10709 sigaction(SIGINT, &sact, NULL);
10710 sigaction(SIGQUIT, &sact, NULL);
10711 sigaction(SIGTERM, &sact, NULL);
10712 sigaction(SIGHUP, &sact, NULL);
10714 sact.sa_handler = sighdlr;
10715 sact.sa_flags = SA_NOCLDSTOP;
10716 sigaction(SIGCHLD, &sact, NULL);
10718 if ((display = XOpenDisplay(0)) == NULL)
10719 errx(1, "can not open display");
10721 conn = XGetXCBConnection(display);
10722 if (xcb_connection_has_error(conn))
10723 errx(1, "can not get XCB connection");
10725 XSetEventQueueOwner(display, XCBOwnsEventQueue);
10727 xcb_prefetch_extension_data(conn, &xcb_randr_id);
10728 xfd = xcb_get_file_descriptor(conn);
10730 /* look for local and global conf file */
10731 pwd = getpwuid(getuid());
10733 errx(1, "invalid user: %d", getuid());
10735 xcb_grab_server(conn);
10736 xcb_aux_sync(conn);
10738 /* flush all events */
10739 while ((evt = xcb_poll_for_event(conn))) {
10740 if (XCB_EVENT_RESPONSE_TYPE(evt) == 0)
10746 errx(1, "another window manager is currently running");
10748 /* Load Xcursors and/or cursorfont glyph cursors. */
10751 xcb_aux_sync(conn);
10761 for (i = 0; ; i++) {
10766 snprintf(conf, sizeof conf, "%s/.%s",
10767 pwd->pw_dir, SWM_CONF_FILE);
10771 snprintf(conf, sizeof conf, "/etc/%s",
10776 snprintf(conf, sizeof conf, "%s/.%s",
10777 pwd->pw_dir, SWM_CONF_FILE_OLD);
10780 /* global compat */
10781 snprintf(conf, sizeof conf, "/etc/%s",
10782 SWM_CONF_FILE_OLD);
10788 if (strlen(conf) && stat(conf, &sb) != -1)
10789 if (S_ISREG(sb.st_mode)) {
10796 /* load conf (if any) */
10798 conf_load(cfile, SWM_CONF_DEFAULT);
10802 if (getenv("SWM_STARTED") == NULL)
10803 setenv("SWM_STARTED", "YES", 1);
10805 /* setup all bars */
10806 num_screens = get_screen_count();
10807 for (i = 0; i < num_screens; i++)
10808 TAILQ_FOREACH(r, &screens[i].rl, entry)
10811 /* Manage existing windows. */
10818 xcb_ungrab_server(conn);
10821 /* Update state of each newly mapped workspace. */
10822 for (i = 0; i < num_screens; i++)
10823 TAILQ_FOREACH(r, &screens[i].rl, entry)
10824 r->ws->state = SWM_WS_STATE_MAPPED;
10826 rd_max = xfd > STDIN_FILENO ? xfd : STDIN_FILENO;
10829 while ((evt = xcb_poll_for_event(conn))) {
10836 /* If just (re)started, set default focus if needed. */
10840 if (focus_mode != SWM_FOCUS_FOLLOW) {
10841 r = TAILQ_FIRST(&screens[0].rl);
10853 FD_SET(STDIN_FILENO, &rd);
10858 num_readable = select(rd_max + 1, &rd, NULL, NULL, &tv);
10859 if (num_readable == -1 && errno != EINTR) {
10860 DNPRINTF(SWM_D_MISC, "select failed");
10861 } else if (num_readable > 0 && FD_ISSET(STDIN_FILENO, &rd)) {
10862 stdin_ready = true;
10866 restart(NULL, NULL);
10875 stdin_ready = false;
10876 bar_extra_update();
10883 shutdown_cleanup();