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 (256)
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 char *clock_format = NULL;
405 bool window_class_enabled = false;
406 bool window_instance_enabled = false;
407 bool window_name_enabled = false;
408 int focus_mode = SWM_FOCUS_DEFAULT;
409 int focus_close = SWM_STACK_BELOW;
410 bool focus_close_wrap = true;
411 int focus_default = SWM_STACK_TOP;
412 int spawn_position = SWM_STACK_TOP;
413 bool disable_border = false;
414 int border_width = 1;
415 int region_padding = 0;
417 bool java_workaround = true;
418 bool verbose_layout = false;
424 XFontSetExtents *bar_fs_extents;
426 bool bar_font_legacy = true;
428 XftColor bar_font_color;
429 XftColor search_font_color;
431 char *startup_exception;
432 unsigned int nr_exceptions = 0;
434 /* layout manager data */
435 struct swm_geometry {
448 struct swm_geometry g;
451 /* virtual "screens" */
453 TAILQ_ENTRY(swm_region) entry;
455 struct swm_geometry g;
456 struct workspace *ws; /* current workspace on this region */
457 struct workspace *ws_prior; /* prior workspace on this region */
458 struct swm_screen *s; /* screen idx */
461 TAILQ_HEAD(swm_region_list, swm_region);
464 TAILQ_ENTRY(ws_win) entry;
465 TAILQ_ENTRY(ws_win) stack_entry;
467 xcb_window_t transient;
468 struct ws_win *focus_child; /* focus on child transient */
469 struct swm_geometry g; /* current geometry */
470 struct swm_geometry g_float; /* region coordinates */
471 bool g_floatvalid; /* g_float geometry validity */
475 int font_size_boundary[SWM_MAX_FONT_STEPS];
481 unsigned long quirks;
482 struct workspace *ws; /* always valid */
483 struct swm_screen *s; /* always valid, never changes */
485 xcb_icccm_get_wm_class_reply_t ch;
486 xcb_icccm_wm_hints_t hints;
488 TAILQ_HEAD(ws_win_list, ws_win);
489 TAILQ_HEAD(ws_win_stack, ws_win);
493 TAILQ_ENTRY(pid_e) entry;
497 TAILQ_HEAD(pid_list, pid_e);
498 struct pid_list pidlist = TAILQ_HEAD_INITIALIZER(pidlist);
500 /* layout handlers */
502 void vertical_config(struct workspace *, int);
503 void vertical_stack(struct workspace *, struct swm_geometry *);
504 void horizontal_config(struct workspace *, int);
505 void horizontal_stack(struct workspace *, struct swm_geometry *);
506 void max_stack(struct workspace *, struct swm_geometry *);
507 void plain_stacker(struct workspace *);
508 void fancy_stacker(struct workspace *);
511 void (*l_stack)(struct workspace *, struct swm_geometry *);
512 void (*l_config)(struct workspace *, int);
514 #define SWM_L_FOCUSPREV (1<<0)
515 #define SWM_L_MAPONFOCUS (1<<1)
516 void (*l_string)(struct workspace *);
518 /* stack, configure */
519 { vertical_stack, vertical_config, 0, plain_stacker },
520 { horizontal_stack, horizontal_config, 0, plain_stacker },
522 SWM_L_MAPONFOCUS | SWM_L_FOCUSPREV, plain_stacker },
523 { NULL, NULL, 0, NULL },
526 /* position of max_stack mode in the layouts array, index into layouts! */
527 #define SWM_V_STACK (0)
528 #define SWM_H_STACK (1)
529 #define SWM_MAX_STACK (2)
531 #define SWM_H_SLICE (32)
532 #define SWM_V_SLICE (32)
534 /* define work spaces */
536 int idx; /* workspace index */
537 char *name; /* workspace name */
538 bool always_raise; /* raise windows on focus */
539 bool bar_enabled; /* bar visibility */
540 struct layout *cur_layout; /* current layout handlers */
541 struct ws_win *focus; /* may be NULL */
542 struct ws_win *focus_prev; /* may be NULL */
543 struct ws_win *focus_pending; /* may be NULL */
544 struct swm_region *r; /* may be NULL */
545 struct swm_region *old_r; /* may be NULL */
546 struct ws_win_list winlist; /* list of windows in ws */
547 struct ws_win_list unmanagedlist; /* list of dead windows in ws */
548 struct ws_win_stack stack; /* stacking order */
549 int state; /* mapping state */
550 char stacker[10]; /* display stacker and layout */
554 int horizontal_msize;
556 int horizontal_stacks;
557 bool horizontal_flip;
567 SWM_WS_STATE_MAPPING,
573 SWM_S_COLOR_BAR_BORDER,
574 SWM_S_COLOR_BAR_BORDER_UNFOCUS,
575 SWM_S_COLOR_BAR_FONT,
577 SWM_S_COLOR_FOCUS_MAXIMIZED,
579 SWM_S_COLOR_UNFOCUS_MAXIMIZED,
583 /* physical screen mapping */
584 #define SWM_WS_MAX (22) /* hard limit */
585 int workspace_limit = 10; /* soft limit */
588 int idx; /* screen index */
589 struct swm_region_list rl; /* list of regions on this screen */
590 struct swm_region_list orl; /* list of old regions */
592 struct workspace ws[SWM_WS_MAX];
593 struct swm_region *r_focus;
600 } c[SWM_S_COLOR_MAX];
602 xcb_gcontext_t bar_gc;
605 struct swm_screen *screens;
607 /* args to functions */
610 #define SWM_ARG_ID_FOCUSNEXT (0)
611 #define SWM_ARG_ID_FOCUSPREV (1)
612 #define SWM_ARG_ID_FOCUSMAIN (2)
613 #define SWM_ARG_ID_FOCUSURGENT (3)
614 #define SWM_ARG_ID_SWAPNEXT (10)
615 #define SWM_ARG_ID_SWAPPREV (11)
616 #define SWM_ARG_ID_SWAPMAIN (12)
617 #define SWM_ARG_ID_MOVELAST (13)
618 #define SWM_ARG_ID_MASTERSHRINK (20)
619 #define SWM_ARG_ID_MASTERGROW (21)
620 #define SWM_ARG_ID_MASTERADD (22)
621 #define SWM_ARG_ID_MASTERDEL (23)
622 #define SWM_ARG_ID_FLIPLAYOUT (24)
623 #define SWM_ARG_ID_STACKRESET (30)
624 #define SWM_ARG_ID_STACKINIT (31)
625 #define SWM_ARG_ID_CYCLEWS_UP (40)
626 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
627 #define SWM_ARG_ID_CYCLERG_UP (42)
628 #define SWM_ARG_ID_CYCLERG_DOWN (43)
629 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
630 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
631 #define SWM_ARG_ID_CYCLEWS_MOVE_UP (46)
632 #define SWM_ARG_ID_CYCLEWS_MOVE_DOWN (47)
633 #define SWM_ARG_ID_STACKINC (50)
634 #define SWM_ARG_ID_STACKDEC (51)
635 #define SWM_ARG_ID_SS_ALL (60)
636 #define SWM_ARG_ID_SS_WINDOW (61)
637 #define SWM_ARG_ID_DONTCENTER (70)
638 #define SWM_ARG_ID_CENTER (71)
639 #define SWM_ARG_ID_KILLWINDOW (80)
640 #define SWM_ARG_ID_DELETEWINDOW (81)
641 #define SWM_ARG_ID_WIDTHGROW (90)
642 #define SWM_ARG_ID_WIDTHSHRINK (91)
643 #define SWM_ARG_ID_HEIGHTGROW (92)
644 #define SWM_ARG_ID_HEIGHTSHRINK (93)
645 #define SWM_ARG_ID_MOVEUP (100)
646 #define SWM_ARG_ID_MOVEDOWN (101)
647 #define SWM_ARG_ID_MOVELEFT (102)
648 #define SWM_ARG_ID_MOVERIGHT (103)
649 #define SWM_ARG_ID_RAISE (105)
650 #define SWM_ARG_ID_LOWER (106)
651 #define SWM_ARG_ID_BAR_TOGGLE (110)
652 #define SWM_ARG_ID_BAR_TOGGLE_WS (111)
658 TAILQ_ENTRY(quirk) entry;
659 char *class; /* WM_CLASS:class */
660 char *instance; /* WM_CLASS:instance */
661 char *name; /* WM_NAME */
663 regex_t regex_instance;
666 #define SWM_Q_FLOAT (1<<0) /* float this window */
667 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
668 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
669 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
670 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
671 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
672 #define SWM_Q_NOFOCUSONMAP (1<<6) /* Don't focus on window when mapped. */
673 #define SWM_Q_FOCUSONMAP_SINGLE (1<<7) /* Only focus if single win of type. */
674 #define SWM_Q_OBEYAPPFOCUSREQ (1<<8) /* Focus when applications ask. */
675 #define SWM_Q_IGNOREPID (1<<9) /* Ignore PID when determining ws. */
676 #define SWM_Q_IGNORESPAWNWS (1<<10) /* Ignore _SWM_WS when managing win. */
678 TAILQ_HEAD(quirk_list, quirk);
679 struct quirk_list quirks = TAILQ_HEAD_INITIALIZER(quirks);
682 * Supported EWMH hints should be added to
683 * both the enum and the ewmh array
689 _NET_CURRENT_DESKTOP,
690 _NET_DESKTOP_GEOMETRY,
692 _NET_DESKTOP_VIEWPORT,
693 _NET_MOVERESIZE_WINDOW,
694 _NET_NUMBER_OF_DESKTOPS,
696 _NET_WM_ACTION_ABOVE,
697 _NET_WM_ACTION_CLOSE,
698 _NET_WM_ACTION_FULLSCREEN,
700 _NET_WM_ACTION_RESIZE,
701 _NET_WM_ALLOWED_ACTIONS,
703 _NET_WM_FULL_PLACEMENT,
707 _NET_WM_STATE_FULLSCREEN,
708 _NET_WM_STATE_HIDDEN,
709 _NET_WM_STATE_MAXIMIZED_VERT,
710 _NET_WM_STATE_MAXIMIZED_HORZ,
711 _NET_WM_STATE_SKIP_PAGER,
712 _NET_WM_STATE_SKIP_TASKBAR,
714 _NET_WM_WINDOW_TYPE_DIALOG,
715 _NET_WM_WINDOW_TYPE_DOCK,
716 _NET_WM_WINDOW_TYPE_NORMAL,
717 _NET_WM_WINDOW_TYPE_SPLASH,
718 _NET_WM_WINDOW_TYPE_TOOLBAR,
719 _NET_WM_WINDOW_TYPE_UTILITY,
720 _SWM_WM_STATE_MANUAL,
727 } ewmh[SWM_EWMH_HINT_MAX] = {
728 /* must be in same order as in the enum */
729 {"_NET_ACTIVE_WINDOW", XCB_ATOM_NONE},
730 {"_NET_CLIENT_LIST", XCB_ATOM_NONE},
731 {"_NET_CLOSE_WINDOW", XCB_ATOM_NONE},
732 {"_NET_CURRENT_DESKTOP", XCB_ATOM_NONE},
733 {"_NET_DESKTOP_GEOMETRY", XCB_ATOM_NONE},
734 {"_NET_DESKTOP_NAMES", XCB_ATOM_NONE},
735 {"_NET_DESKTOP_VIEWPORT", XCB_ATOM_NONE},
736 {"_NET_MOVERESIZE_WINDOW", XCB_ATOM_NONE},
737 {"_NET_NUMBER_OF_DESKTOPS", XCB_ATOM_NONE},
738 {"_NET_RESTACK_WINDOW", XCB_ATOM_NONE},
739 {"_NET_WM_ACTION_ABOVE", XCB_ATOM_NONE},
740 {"_NET_WM_ACTION_CLOSE", XCB_ATOM_NONE},
741 {"_NET_WM_ACTION_FULLSCREEN", XCB_ATOM_NONE},
742 {"_NET_WM_ACTION_MOVE", XCB_ATOM_NONE},
743 {"_NET_WM_ACTION_RESIZE", XCB_ATOM_NONE},
744 {"_NET_WM_ALLOWED_ACTIONS", XCB_ATOM_NONE},
745 {"_NET_WM_DESKTOP", XCB_ATOM_NONE},
746 {"_NET_WM_FULL_PLACEMENT", XCB_ATOM_NONE},
747 {"_NET_WM_NAME", XCB_ATOM_NONE},
748 {"_NET_WM_STATE", XCB_ATOM_NONE},
749 {"_NET_WM_STATE_ABOVE", XCB_ATOM_NONE},
750 {"_NET_WM_STATE_FULLSCREEN", XCB_ATOM_NONE},
751 {"_NET_WM_STATE_HIDDEN", XCB_ATOM_NONE},
752 {"_NET_WM_STATE_MAXIMIZED_VERT", XCB_ATOM_NONE},
753 {"_NET_WM_STATE_MAXIMIZED_HORZ", XCB_ATOM_NONE},
754 {"_NET_WM_STATE_SKIP_PAGER", XCB_ATOM_NONE},
755 {"_NET_WM_STATE_SKIP_TASKBAR", XCB_ATOM_NONE},
756 {"_NET_WM_WINDOW_TYPE", XCB_ATOM_NONE},
757 {"_NET_WM_WINDOW_TYPE_DIALOG", XCB_ATOM_NONE},
758 {"_NET_WM_WINDOW_TYPE_DOCK", XCB_ATOM_NONE},
759 {"_NET_WM_WINDOW_TYPE_NORMAL", XCB_ATOM_NONE},
760 {"_NET_WM_WINDOW_TYPE_SPLASH", XCB_ATOM_NONE},
761 {"_NET_WM_WINDOW_TYPE_TOOLBAR", XCB_ATOM_NONE},
762 {"_NET_WM_WINDOW_TYPE_UTILITY", XCB_ATOM_NONE},
763 {"_SWM_WM_STATE_MANUAL", XCB_ATOM_NONE},
766 /* EWMH source type */
768 EWMH_SOURCE_TYPE_NONE = 0,
769 EWMH_SOURCE_TYPE_NORMAL = 1,
770 EWMH_SOURCE_TYPE_OTHER = 2,
777 XC_BOTTOM_LEFT_CORNER,
778 XC_BOTTOM_RIGHT_CORNER,
786 char *name; /* Name used by Xcursor .*/
787 uint8_t cf_char; /* cursorfont index. */
789 } cursors[XC_MAX] = {
790 {"fleur", XC_fleur, XCB_CURSOR_NONE},
791 {"left_ptr", XC_left_ptr, XCB_CURSOR_NONE},
792 {"bottom_left_corner", XC_bottom_left_corner, XCB_CURSOR_NONE},
793 {"bottom_right_corner", XC_bottom_right_corner, XCB_CURSOR_NONE},
794 {"sizing", XC_sizing, XCB_CURSOR_NONE},
795 {"top_left_corner", XC_top_left_corner, XCB_CURSOR_NONE},
796 {"top_right_corner", XC_top_right_corner, XCB_CURSOR_NONE},
799 #define SWM_SPAWN_OPTIONAL 0x1
803 TAILQ_ENTRY(spawn_prog) entry;
809 TAILQ_HEAD(spawn_list, spawn_prog);
810 struct spawn_list spawns = TAILQ_HEAD_INITIALIZER(spawns);
812 /* user/key callable function IDs */
928 KF_DUMPWINS, /* MUST BE LAST */
936 enum keyfuncid funcid;
939 RB_HEAD(key_tree, key);
941 /* function prototypes */
942 void adjust_font(struct ws_win *);
943 char *argsep(char **);
944 void bar_cleanup(struct swm_region *);
945 void bar_extra_setup(void);
946 void bar_extra_stop(void);
947 int bar_extra_update(void);
948 void bar_fmt(const char *, char *, struct swm_region *, size_t);
949 void bar_fmt_expand(char *, size_t);
951 void bar_print(struct swm_region *, const char *);
952 void bar_print_legacy(struct swm_region *, const char *);
953 void bar_replace(char *, char *, struct swm_region *, size_t);
954 void bar_replace_pad(char *, int *, size_t);
955 char *bar_replace_seq(char *, char *, struct swm_region *, size_t *, size_t);
956 void bar_setup(struct swm_region *);
957 void bar_toggle(struct swm_region *, union arg *);
958 void bar_urgent(char *, size_t);
959 void bar_window_class(char *, size_t, struct swm_region *);
960 void bar_window_class_instance(char *, size_t, struct swm_region *);
961 void bar_window_float(char *, size_t, struct swm_region *);
962 void bar_window_instance(char *, size_t, struct swm_region *);
963 void bar_window_name(char *, size_t, struct swm_region *);
964 void bar_window_state(char *, size_t, struct swm_region *);
965 void bar_workspace_name(char *, size_t, struct swm_region *);
966 void buttonpress(xcb_button_press_event_t *);
967 void check_conn(void);
968 void clear_keys(void);
969 int clear_maximized(struct workspace *);
970 void clientmessage(xcb_client_message_event_t *);
971 void client_msg(struct ws_win *, xcb_atom_t, xcb_timestamp_t);
972 int conf_load(const char *, int);
973 void configurenotify(xcb_configure_notify_event_t *);
974 void configurerequest(xcb_configure_request_event_t *);
975 void config_win(struct ws_win *, xcb_configure_request_event_t *);
976 void constrain_window(struct ws_win *, struct swm_geometry *, int *);
977 int count_win(struct workspace *, bool);
978 void cursors_cleanup(void);
979 void cursors_load(void);
980 void custom_region(const char *);
981 void cyclerg(struct swm_region *, union arg *);
982 void cyclews(struct swm_region *, union arg *);
983 void cycle_layout(struct swm_region *, union arg *);
984 void destroynotify(xcb_destroy_notify_event_t *);
985 void dumpwins(struct swm_region *, union arg *);
987 void enternotify(xcb_enter_notify_event_t *);
988 void event_drain(uint8_t);
989 void event_error(xcb_generic_error_t *);
990 void event_handle(xcb_generic_event_t *);
991 void ewmh_apply_flags(struct ws_win *, uint32_t);
992 void ewmh_autoquirk(struct ws_win *);
993 void ewmh_get_desktop_names(void);
994 void ewmh_get_wm_state(struct ws_win *);
995 void ewmh_update_actions(struct ws_win *);
996 void ewmh_update_client_list(void);
997 void ewmh_update_current_desktop(void);
998 void ewmh_update_desktop_names(void);
999 void ewmh_update_desktops(void);
1000 void ewmh_change_wm_state(struct ws_win *, xcb_atom_t, long);
1001 void ewmh_update_wm_state(struct ws_win *);
1002 char *expand_tilde(const char *);
1003 void expose(xcb_expose_event_t *);
1004 void fake_keypress(struct ws_win *, xcb_keysym_t, uint16_t);
1005 struct pid_e *find_pid(pid_t);
1006 struct ws_win *find_unmanaged_window(xcb_window_t);
1007 struct ws_win *find_window(xcb_window_t);
1008 void floating_toggle(struct swm_region *, union arg *);
1009 void focus(struct swm_region *, union arg *);
1011 void focusin(xcb_focus_in_event_t *);
1012 void focusout(xcb_focus_out_event_t *);
1014 void focus_flush(void);
1015 void focus_region(struct swm_region *);
1016 void focusrg(struct swm_region *, union arg *);
1017 void focus_win(struct ws_win *);
1018 void fontset_init(void);
1019 void free_window(struct ws_win *);
1020 xcb_atom_t get_atom_from_string(const char *);
1022 char *get_atom_name(xcb_atom_t);
1024 struct ws_win *get_focus_magic(struct ws_win *);
1025 struct ws_win *get_focus_prev(struct ws_win *);
1027 char *get_notify_detail_label(uint8_t);
1028 char *get_notify_mode_label(uint8_t);
1030 struct ws_win *get_pointer_win(xcb_window_t);
1031 struct ws_win *get_region_focus(struct swm_region *);
1032 int get_region_index(struct swm_region *);
1033 xcb_screen_t *get_screen(int);
1034 int get_screen_count(void);
1036 char *get_source_type_label(uint32_t);
1037 char *get_stack_mode_name(uint8_t);
1039 int32_t get_swm_ws(xcb_window_t);
1040 char *get_win_name(xcb_window_t);
1041 uint8_t get_win_state(xcb_window_t);
1042 void get_wm_protocols(struct ws_win *);
1043 int get_ws_idx(struct ws_win *);
1044 void grabbuttons(struct ws_win *);
1045 void grabkeys(void);
1046 void grab_windows(void);
1047 void iconify(struct swm_region *, union arg *);
1048 bool isxlfd(char *);
1049 void keypress(xcb_key_press_event_t *);
1050 int key_cmp(struct key *, struct key *);
1051 void key_insert(unsigned int, KeySym, enum keyfuncid, const char *);
1052 struct key *key_lookup(unsigned int, KeySym);
1053 void key_remove(struct key *);
1054 void key_replace(struct key *, unsigned int, KeySym, enum keyfuncid,
1056 void kill_bar_extra_atexit(void);
1057 void kill_refs(struct ws_win *);
1059 void leavenotify(xcb_leave_notify_event_t *);
1061 void load_float_geom(struct ws_win *);
1062 struct ws_win *manage_window(xcb_window_t, int, bool);
1063 void map_window(struct ws_win *);
1064 void mapnotify(xcb_map_notify_event_t *);
1065 void mappingnotify(xcb_mapping_notify_event_t *);
1066 void maprequest(xcb_map_request_event_t *);
1067 void maximize_toggle(struct swm_region *, union arg *);
1068 void motionnotify(xcb_motion_notify_event_t *);
1069 void move(struct ws_win *, union arg *);
1070 void move_step(struct swm_region *, union arg *);
1071 uint32_t name_to_pixel(int, const char *);
1072 void name_workspace(struct swm_region *, union arg *);
1073 void new_region(struct swm_screen *, int, int, int, int);
1074 int parsekeys(const char *, unsigned int, unsigned int *, KeySym *);
1075 int parsequirks(const char *, unsigned long *);
1076 int parse_rgb(const char *, uint16_t *, uint16_t *, uint16_t *);
1077 void pressbutton(struct swm_region *, union arg *);
1078 void priorws(struct swm_region *, union arg *);
1080 void print_win_geom(xcb_window_t);
1082 void propertynotify(xcb_property_notify_event_t *);
1083 void quirk_free(struct quirk *);
1084 void quirk_insert(const char *, const char *, const char *,unsigned long);
1085 void quirk_remove(struct quirk *);
1086 void quirk_replace(struct quirk *, const char *, const char *, const char *,
1088 void quit(struct swm_region *, union arg *);
1089 void raise_toggle(struct swm_region *, union arg *);
1090 void raise_window(struct ws_win *);
1091 void region_containment(struct ws_win *, struct swm_region *, int);
1092 struct swm_region *region_under(struct swm_screen *, int, int);
1093 void regionize(struct ws_win *, int, int);
1094 void resize(struct ws_win *, union arg *);
1095 void resize_step(struct swm_region *, union arg *);
1096 void restart(struct swm_region *, union arg *);
1097 struct swm_region *root_to_region(xcb_window_t, int);
1098 void screenchange(xcb_randr_screen_change_notify_event_t *);
1099 void scan_randr(int);
1100 void search_do_resp(void);
1101 void search_resp_name_workspace(const char *, size_t);
1102 void search_resp_search_window(const char *);
1103 void search_resp_search_workspace(const char *);
1104 void search_resp_uniconify(const char *, size_t);
1105 void search_win(struct swm_region *, union arg *);
1106 void search_win_cleanup(void);
1107 void search_workspace(struct swm_region *, union arg *);
1108 void send_to_rg(struct swm_region *, union arg *);
1109 void send_to_ws(struct swm_region *, union arg *);
1110 void set_region(struct swm_region *);
1111 int setautorun(const char *, const char *, int);
1112 int setconfbinding(const char *, const char *, int);
1113 int setconfcolor(const char *, const char *, int);
1114 int setconfmodkey(const char *, const char *, int);
1115 int setconfquirk(const char *, const char *, int);
1116 int setconfregion(const char *, const char *, int);
1117 int setconfspawn(const char *, const char *, int);
1118 int setconfvalue(const char *, const char *, int);
1119 void setkeybinding(unsigned int, KeySym, enum keyfuncid, const char *);
1120 int setkeymapping(const char *, const char *, int);
1121 int setlayout(const char *, const char *, int);
1122 void setquirk(const char *, const char *, const char *,unsigned long);
1123 void setscreencolor(const char *, int, int);
1124 void setspawn(const char *, const char *, int);
1125 void setup_ewmh(void);
1126 void setup_globals(void);
1127 void setup_keys(void);
1128 void setup_quirks(void);
1129 void setup_screens(void);
1130 void setup_spawn(void);
1131 void set_child_transient(struct ws_win *, xcb_window_t *);
1132 void set_win_state(struct ws_win *, uint8_t);
1133 void shutdown_cleanup(void);
1135 void socket_setnonblock(int);
1136 void sort_windows(struct ws_win_list *);
1137 void spawn(int, union arg *, bool);
1138 void spawn_custom(struct swm_region *, union arg *, const char *);
1139 int spawn_expand(struct swm_region *, union arg *, const char *, char ***);
1140 void spawn_insert(const char *, const char *, int);
1141 struct spawn_prog *spawn_find(const char *);
1142 void spawn_remove(struct spawn_prog *);
1143 void spawn_replace(struct spawn_prog *, const char *, const char *, int);
1144 void spawn_select(struct swm_region *, union arg *, const char *, int *);
1145 void stack_config(struct swm_region *, union arg *);
1146 void stack_master(struct workspace *, struct swm_geometry *, int, bool);
1147 void store_float_geom(struct ws_win *);
1148 char *strdupsafe(const char *);
1149 void swapwin(struct swm_region *, union arg *);
1150 void switchws(struct swm_region *, union arg *);
1151 void teardown_ewmh(void);
1152 void unescape_selector(char *);
1153 void unfocus_win(struct ws_win *);
1154 void uniconify(struct swm_region *, union arg *);
1155 void unmanage_window(struct ws_win *);
1156 void unmapnotify(xcb_unmap_notify_event_t *);
1157 void unmap_all(void);
1158 void unmap_window(struct ws_win *);
1159 void updatenumlockmask(void);
1160 void update_floater(struct ws_win *);
1161 void update_modkey(unsigned int);
1162 void update_win_stacking(struct ws_win *);
1163 void update_window(struct ws_win *);
1164 void update_window_color(struct ws_win *);
1165 void update_wm_state(struct ws_win *win);
1166 void validate_spawns(void);
1167 int validate_win(struct ws_win *);
1168 int validate_ws(struct workspace *);
1169 void version(struct swm_region *, union arg *);
1170 void win_to_ws(struct ws_win *, int, bool);
1171 pid_t window_get_pid(xcb_window_t);
1172 void wkill(struct swm_region *, union arg *);
1173 void update_ws_stack(struct workspace *);
1174 void xft_init(struct swm_region *);
1175 void _add_startup_exception(const char *, va_list);
1176 void add_startup_exception(const char *, ...);
1178 RB_PROTOTYPE(key_tree, key, entry, key_cmp);
1179 RB_GENERATE(key_tree, key, entry, key_cmp);
1180 struct key_tree keys;
1185 xcb_font_t cf = XCB_NONE;
1188 for (i = 0; i < LENGTH(cursors); ++i) {
1189 /* try to load Xcursor first. */
1190 cursors[i].cid = XcursorLibraryLoadCursor(display,
1193 /* fallback to cursorfont. */
1194 if (cursors[i].cid == XCB_CURSOR_NONE) {
1195 if (cf == XCB_NONE) {
1196 cf = xcb_generate_id(conn);
1197 xcb_open_font(conn, cf, strlen("cursor"),
1201 cursors[i].cid = xcb_generate_id(conn);
1202 xcb_create_glyph_cursor(conn, cursors[i].cid, cf, cf,
1203 cursors[i].cf_char, cursors[i].cf_char + 1, 0, 0, 0,
1204 0xffff, 0xffff, 0xffff);
1210 xcb_close_font(conn, cf);
1214 cursors_cleanup(void)
1217 for (i = 0; i < LENGTH(cursors); ++i)
1218 xcb_free_cursor(conn, cursors[i].cid);
1222 expand_tilde(const char *s)
1224 struct passwd *ppwd;
1232 errx(1, "expand_tilde: NULL string.");
1235 result = strdup(sc);
1241 if ((max = sysconf(_SC_LOGIN_NAME_MAX)) == -1)
1242 errx(1, "expand_tilde: sysconf");
1244 if ((user = calloc(1, max + 1)) == NULL)
1245 errx(1, "expand_tilde: calloc");
1247 for (i = 0; s[i] != '/' && s[i] != '\0'; ++i)
1252 ppwd = strlen(user) == 0 ? getpwuid(getuid()) : getpwnam(user);
1256 result = strdup(sc);
1258 if (asprintf(&result, "%s%s", ppwd->pw_dir, s) == -1)
1262 errx(1, "expand_tilde: failed to allocate memory.");
1268 parse_rgb(const char *rgb, uint16_t *rr, uint16_t *gg, uint16_t *bb)
1270 unsigned int tmpr, tmpg, tmpb;
1272 if (sscanf(rgb, "rgb:%x/%x/%x", &tmpr, &tmpg, &tmpb) != 3)
1275 *rr = RGB_8_TO_16(tmpr);
1276 *gg = RGB_8_TO_16(tmpg);
1277 *bb = RGB_8_TO_16(tmpb);
1283 get_screen(int screen)
1285 const xcb_setup_t *r;
1286 xcb_screen_iterator_t iter;
1288 if ((r = xcb_get_setup(conn)) == NULL) {
1289 DNPRINTF(SWM_D_MISC, "get_screen: xcb_get_setup\n");
1293 iter = xcb_setup_roots_iterator(r);
1294 for (; iter.rem; --screen, xcb_screen_next(&iter))
1302 get_screen_count(void)
1304 const xcb_setup_t *r;
1306 if ((r = xcb_get_setup(conn)) == NULL) {
1307 DNPRINTF(SWM_D_MISC, "get_screen_count: xcb_get_setup\n");
1311 return xcb_setup_roots_length(r);
1315 get_region_index(struct swm_region *r)
1317 struct swm_region *rr;
1323 TAILQ_FOREACH(rr, &r->s->rl, entry) {
1338 if (focus_mode == SWM_FOCUS_DEFAULT)
1339 event_drain(XCB_ENTER_NOTIFY);
1345 get_atom_from_string(const char *str)
1347 xcb_intern_atom_cookie_t c;
1348 xcb_intern_atom_reply_t *r;
1351 c = xcb_intern_atom(conn, 0, strlen(str), str);
1352 r = xcb_intern_atom_reply(conn, c, NULL);
1360 return (XCB_ATOM_NONE);
1364 get_wm_protocols(struct ws_win *win) {
1366 xcb_icccm_get_wm_protocols_reply_t wpr;
1368 if (xcb_icccm_get_wm_protocols_reply(conn,
1369 xcb_icccm_get_wm_protocols(conn, win->id, a_prot),
1371 for (i = 0; i < (int)wpr.atoms_len; i++) {
1372 if (wpr.atoms[i] == a_takefocus)
1373 win->take_focus = true;
1374 if (wpr.atoms[i] == a_delete)
1375 win->can_delete = true;
1377 xcb_icccm_get_wm_protocols_reply_wipe(&wpr);
1384 xcb_window_t root, win;
1385 int i, j, num_screens;
1387 for (i = 0; i < LENGTH(ewmh); i++)
1388 ewmh[i].atom = get_atom_from_string(ewmh[i].name);
1390 num_screens = get_screen_count();
1391 for (i = 0; i < num_screens; i++) {
1392 root = screens[i].root;
1394 /* Set up _NET_SUPPORTING_WM_CHECK. */
1395 win = xcb_generate_id(conn);
1396 xcb_create_window(conn, XCB_COPY_FROM_PARENT, win, root,
1397 0, 0, 1, 1, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT,
1398 XCB_COPY_FROM_PARENT, 0, NULL);
1400 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root,
1401 a_net_wm_check, XCB_ATOM_WINDOW, 32, 1, &win);
1402 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win,
1403 a_net_wm_check, XCB_ATOM_WINDOW, 32, 1, &win);
1406 * Impersonate LG3D non-reparenting WM, written by Sun, to
1407 * workaround a Java GUI rendering issue.
1409 if (java_workaround)
1410 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win,
1411 ewmh[_NET_WM_NAME].atom, a_utf8_string,
1412 8, strlen("LG3D"), "LG3D");
1414 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win,
1415 ewmh[_NET_WM_NAME].atom, a_utf8_string,
1416 8, strlen("spectrwm"), "spectrwm");
1418 /* Report supported atoms */
1419 xcb_delete_property(conn, root, a_net_supported);
1420 for (j = 0; j < LENGTH(ewmh); j++)
1421 xcb_change_property(conn, XCB_PROP_MODE_APPEND, root,
1422 a_net_supported, XCB_ATOM_ATOM, 32, 1,
1427 ewmh_update_desktops();
1428 ewmh_get_desktop_names();
1436 xcb_get_property_cookie_t pc;
1437 xcb_get_property_reply_t *pr;
1439 num_screens = get_screen_count();
1441 for (i = 0; i < num_screens; i++) {
1442 /* Get the support check window and destroy it */
1443 pc = xcb_get_property(conn, 0, screens[i].root, a_net_wm_check,
1444 XCB_ATOM_WINDOW, 0, 1);
1445 pr = xcb_get_property_reply(conn, pc, NULL);
1448 if (pr->format == a_net_wm_check) {
1449 id = *((xcb_window_t *)xcb_get_property_value(pr));
1451 xcb_destroy_window(conn, id);
1452 xcb_delete_property(conn, screens[i].root,
1454 xcb_delete_property(conn, screens[i].root,
1462 ewmh_autoquirk(struct ws_win *win)
1464 xcb_get_property_reply_t *r;
1465 xcb_get_property_cookie_t c;
1469 c = xcb_get_property(conn, 0, win->id,
1470 ewmh[_NET_WM_WINDOW_TYPE].atom, XCB_ATOM_ATOM, 0, UINT32_MAX);
1471 r = xcb_get_property_reply(conn, c, NULL);
1475 type = xcb_get_property_value(r);
1476 n = xcb_get_property_value_length(r) / sizeof(xcb_atom_t);
1478 for (i = 0; i < n; i++) {
1479 if (type[i] == ewmh[_NET_WM_WINDOW_TYPE_NORMAL].atom)
1481 if (type[i] == ewmh[_NET_WM_WINDOW_TYPE_DOCK].atom ||
1482 type[i] == ewmh[_NET_WM_WINDOW_TYPE_TOOLBAR].atom ||
1483 type[i] == ewmh[_NET_WM_WINDOW_TYPE_UTILITY].atom) {
1484 win->quirks = SWM_Q_FLOAT | SWM_Q_ANYWHERE;
1487 if (type[i] == ewmh[_NET_WM_WINDOW_TYPE_SPLASH].atom ||
1488 type[i] == ewmh[_NET_WM_WINDOW_TYPE_DIALOG].atom) {
1489 win->quirks = SWM_Q_FLOAT;
1497 ewmh_update_actions(struct ws_win *win)
1499 xcb_atom_t actions[SWM_EWMH_ACTION_COUNT_MAX];
1505 actions[n++] = ewmh[_NET_WM_ACTION_CLOSE].atom;
1508 actions[n++] = ewmh[_NET_WM_ACTION_MOVE].atom;
1509 actions[n++] = ewmh[_NET_WM_ACTION_RESIZE].atom;
1510 actions[n++] = ewmh[_NET_WM_ACTION_ABOVE].atom;
1513 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
1514 ewmh[_NET_WM_ALLOWED_ACTIONS].atom, XCB_ATOM_ATOM, 32, 1, actions);
1517 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
1518 #define _NET_WM_STATE_ADD 1 /* add/set property */
1519 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
1522 ewmh_change_wm_state(struct ws_win *win, xcb_atom_t state, long action)
1529 name = get_atom_name(state);
1530 DNPRINTF(SWM_D_PROP, "ewmh_change_wm_state: win %#x, state: %s, "
1531 "action: %ld\n", WINID(win), name, action);
1537 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
1538 flag = EWMH_F_FULLSCREEN;
1539 else if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
1540 flag = EWMH_F_ABOVE;
1541 else if (state == ewmh[_NET_WM_STATE_HIDDEN].atom)
1542 flag = EWMH_F_HIDDEN;
1543 else if (state == ewmh[_NET_WM_STATE_MAXIMIZED_VERT].atom ||
1544 state == ewmh[_NET_WM_STATE_MAXIMIZED_HORZ].atom)
1545 flag = EWMH_F_MAXIMIZED;
1546 else if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
1547 flag = SWM_F_MANUAL;
1548 else if (state == ewmh[_NET_WM_STATE_SKIP_PAGER].atom)
1549 flag = EWMH_F_SKIP_PAGER;
1550 else if (state == ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom)
1551 flag = EWMH_F_SKIP_TASKBAR;
1553 /* Disallow unfloating transients. */
1554 if (TRANS(win) && flag == EWMH_F_ABOVE)
1557 new_flags = win->ewmh_flags;
1560 case _NET_WM_STATE_REMOVE:
1563 case _NET_WM_STATE_ADD:
1566 case _NET_WM_STATE_TOGGLE:
1571 ewmh_apply_flags(win, new_flags);
1574 DNPRINTF(SWM_D_PROP, "ewmh_change_wm_state: done.\n");
1578 ewmh_apply_flags(struct ws_win *win, uint32_t pending)
1580 struct workspace *ws;
1583 changed = win->ewmh_flags ^ pending;
1587 DNPRINTF(SWM_D_PROP, "ewmh_apply_flags: pending: %d\n", pending);
1589 win->ewmh_flags = pending;
1592 if (changed & EWMH_F_HIDDEN) {
1594 if (focus_mode != SWM_FOCUS_FOLLOW)
1595 ws->focus_pending = get_focus_prev(win);
1600 /* Reload floating geometry in case region changed. */
1602 load_float_geom(win);
1604 /* The window is no longer iconic, prepare focus. */
1605 if (focus_mode != SWM_FOCUS_FOLLOW)
1606 ws->focus_pending = get_focus_magic(win);
1611 if (changed & EWMH_F_ABOVE) {
1612 if (ws->cur_layout != &layouts[SWM_MAX_STACK]) {
1614 load_float_geom(win);
1615 else if (!MAXIMIZED(win))
1616 store_float_geom(win);
1618 win->ewmh_flags &= ~EWMH_F_MAXIMIZED;
1619 changed &= ~EWMH_F_MAXIMIZED;
1623 win->ewmh_flags ^= EWMH_F_ABOVE & pending;
1627 if (changed & EWMH_F_MAXIMIZED) {
1628 /* VERT and/or HORZ changed. */
1630 if (!MAXIMIZED(win))
1631 load_float_geom(win);
1633 store_float_geom(win);
1636 if (MAXIMIZED(win)) {
1637 if (focus_mode != SWM_FOCUS_FOLLOW &&
1638 ws->cur_layout != &layouts[SWM_MAX_STACK]) {
1642 ws->focus_pending = win;
1646 update_window_color(win);
1650 if (changed & EWMH_F_FULLSCREEN) {
1651 if (FULLSCREEN(win)) {
1652 if (focus_mode != SWM_FOCUS_FOLLOW) {
1656 ws->focus_pending = win;
1659 load_float_geom(win);
1662 win->ewmh_flags &= ~EWMH_F_MAXIMIZED;
1666 DNPRINTF(SWM_D_PROP, "ewmh_apply_flags: done.\n");
1670 ewmh_update_wm_state(struct ws_win *win) {
1671 xcb_atom_t vals[SWM_EWMH_ACTION_COUNT_MAX];
1675 vals[n++] = ewmh[_NET_WM_STATE_HIDDEN].atom;
1676 if (FULLSCREEN(win))
1677 vals[n++] = ewmh[_NET_WM_STATE_FULLSCREEN].atom;
1678 if (MAXIMIZED_VERT(win))
1679 vals[n++] = ewmh[_NET_WM_STATE_MAXIMIZED_VERT].atom;
1680 if (MAXIMIZED_HORZ(win))
1681 vals[n++] = ewmh[_NET_WM_STATE_MAXIMIZED_HORZ].atom;
1682 if (win->ewmh_flags & EWMH_F_SKIP_PAGER)
1683 vals[n++] = ewmh[_NET_WM_STATE_SKIP_PAGER].atom;
1684 if (win->ewmh_flags & EWMH_F_SKIP_TASKBAR)
1685 vals[n++] = ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom;
1686 if (win->ewmh_flags & EWMH_F_ABOVE)
1687 vals[n++] = ewmh[_NET_WM_STATE_ABOVE].atom;
1688 if (win->ewmh_flags & SWM_F_MANUAL)
1689 vals[n++] = ewmh[_SWM_WM_STATE_MANUAL].atom;
1692 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
1693 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, n, vals);
1695 xcb_delete_property(conn, win->id, ewmh[_NET_WM_STATE].atom);
1699 ewmh_get_wm_state(struct ws_win *win)
1702 xcb_get_property_cookie_t c;
1703 xcb_get_property_reply_t *r;
1709 win->ewmh_flags = 0;
1711 c = xcb_get_property(conn, 0, win->id, ewmh[_NET_WM_STATE].atom,
1712 XCB_ATOM_ATOM, 0, UINT32_MAX);
1713 r = xcb_get_property_reply(conn, c, NULL);
1717 states = xcb_get_property_value(r);
1718 n = xcb_get_property_value_length(r) / sizeof(xcb_atom_t);
1720 for (i = 0; i < n; i++)
1721 ewmh_change_wm_state(win, states[i], _NET_WM_STATE_ADD);
1729 dumpwins(struct swm_region *r, union arg *args)
1733 xcb_get_window_attributes_cookie_t c;
1734 xcb_get_window_attributes_reply_t *wa;
1736 /* suppress unused warning since var is needed */
1739 if (r->ws == NULL) {
1740 DPRINTF("dumpwins: invalid workspace\n");
1744 DPRINTF("=== managed window list ws %02d ===\n", r->ws->idx);
1745 TAILQ_FOREACH(w, &r->ws->winlist, entry) {
1746 state = get_win_state(w->id);
1747 c = xcb_get_window_attributes(conn, w->id);
1748 wa = xcb_get_window_attributes_reply(conn, c, NULL);
1750 DPRINTF("win %#x, map_state: %d, state: %u, "
1751 "transient: %#x\n", w->id, wa->map_state,
1752 state, w->transient);
1755 DPRINTF("win %#x, failed xcb_get_window_attributes\n",
1759 DPRINTF("=== stacking order (top down) === \n");
1760 TAILQ_FOREACH(w, &r->ws->stack, stack_entry) {
1761 DPRINTF("win %#x, fs: %s, maximized: %s, above: %s, "
1762 "iconic: %s\n", w->id, YESNO(FULLSCREEN(w)),
1763 YESNO(MAXIMIZED(w)), YESNO(ABOVE(w)), YESNO(ICONIC(w)));
1766 DPRINTF("===== unmanaged window list =====\n");
1767 TAILQ_FOREACH(w, &r->ws->unmanagedlist, entry) {
1768 state = get_win_state(w->id);
1769 c = xcb_get_window_attributes(conn, w->id);
1770 wa = xcb_get_window_attributes_reply(conn, c, NULL);
1772 DPRINTF("win %#x, map_state: %d, state: %u, "
1773 "transient: %#x\n", w->id, wa->map_state,
1774 state, w->transient);
1777 DPRINTF("win %#x, failed xcb_get_window_attributes\n",
1781 DPRINTF("=================================\n");
1785 dumpwins(struct swm_region *r, union arg *s)
1790 #endif /* SWM_DEBUG */
1795 int saved_errno, status;
1798 saved_errno = errno;
1802 while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) != 0) {
1807 if (errno != ECHILD)
1808 warn("sighdlr: waitpid");
1809 #endif /* SWM_DEBUG */
1812 if (pid == searchpid)
1816 if (WIFEXITED(status)) {
1817 if (WEXITSTATUS(status) != 0)
1818 warnx("sighdlr: child exit status: %d",
1819 WEXITSTATUS(status));
1821 warnx("sighdlr: child is terminated "
1823 #endif /* SWM_DEBUG */
1837 errno = saved_errno;
1843 struct pid_e *p = NULL;
1845 DNPRINTF(SWM_D_MISC, "find_pid: %d\n", pid);
1850 TAILQ_FOREACH(p, &pidlist, entry) {
1859 name_to_pixel(int sidx, const char *colorname)
1861 uint32_t result = 0;
1862 char cname[32] = "#";
1863 xcb_screen_t *screen;
1864 xcb_colormap_t cmap;
1865 xcb_alloc_color_reply_t *cr;
1866 xcb_alloc_named_color_reply_t *nr;
1867 uint16_t rr, gg, bb;
1869 screen = get_screen(sidx);
1870 cmap = screen->default_colormap;
1872 /* color is in format rgb://rr/gg/bb */
1873 if (strncmp(colorname, "rgb:", 4) == 0) {
1874 if (parse_rgb(colorname, &rr, &gg, &bb) == -1)
1875 warnx("could not parse rgb %s", colorname);
1877 cr = xcb_alloc_color_reply(conn,
1878 xcb_alloc_color(conn, cmap, rr, gg, bb),
1884 warnx("color '%s' not found", colorname);
1887 nr = xcb_alloc_named_color_reply(conn,
1888 xcb_alloc_named_color(conn, cmap, strlen(colorname),
1891 strlcat(cname, colorname + 2, sizeof cname - 1);
1892 nr = xcb_alloc_named_color_reply(conn,
1893 xcb_alloc_named_color(conn, cmap, strlen(cname),
1900 warnx("color '%s' not found", colorname);
1907 setscreencolor(const char *val, int i, int c)
1909 if (i < 0 || i >= get_screen_count())
1912 screens[i].c[c].pixel = name_to_pixel(i, val);
1913 free(screens[i].c[c].name);
1914 if ((screens[i].c[c].name = strdup(val)) == NULL)
1919 fancy_stacker(struct workspace *ws)
1921 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1922 if (ws->cur_layout->l_stack == vertical_stack)
1923 snprintf(ws->stacker, sizeof ws->stacker,
1924 ws->l_state.vertical_flip ? "[%d>%d]" : "[%d|%d]",
1925 ws->l_state.vertical_mwin, ws->l_state.vertical_stacks);
1926 else if (ws->cur_layout->l_stack == horizontal_stack)
1927 snprintf(ws->stacker, sizeof ws->stacker,
1928 ws->l_state.horizontal_flip ? "[%dv%d]" : "[%d-%d]",
1929 ws->l_state.horizontal_mwin, ws->l_state.horizontal_stacks);
1933 plain_stacker(struct workspace *ws)
1935 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1936 if (ws->cur_layout->l_stack == vertical_stack)
1937 strlcpy(ws->stacker, ws->l_state.vertical_flip ? "[>]" : "[|]",
1938 sizeof ws->stacker);
1939 else if (ws->cur_layout->l_stack == horizontal_stack)
1940 strlcpy(ws->stacker, ws->l_state.horizontal_flip ? "[v]" : "[-]",
1941 sizeof ws->stacker);
1945 custom_region(const char *val)
1947 unsigned int x, y, w, h;
1948 int sidx, num_screens;
1949 xcb_screen_t *screen;
1951 num_screens = get_screen_count();
1952 if (sscanf(val, "screen[%d]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
1953 errx(1, "invalid custom region, "
1954 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1955 if (sidx < 1 || sidx > num_screens)
1956 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1960 if ((screen = get_screen(sidx)) == NULL)
1961 errx(1, "ERROR: can't get screen %d.", sidx);
1964 errx(1, "region %ux%u+%u+%u too small", w, h, x, y);
1966 if (x > screen->width_in_pixels ||
1967 y > screen->height_in_pixels ||
1968 w + x > screen->width_in_pixels ||
1969 h + y > screen->height_in_pixels) {
1970 warnx("ignoring region %ux%u+%u+%u - not within screen "
1971 "boundaries (%ux%u)", w, h, x, y,
1972 screen->width_in_pixels, screen->height_in_pixels);
1976 new_region(&screens[sidx], x, y, w, h);
1980 socket_setnonblock(int fd)
1984 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
1985 err(1, "fcntl F_GETFL");
1986 flags |= O_NONBLOCK;
1987 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
1988 err(1, "fcntl F_SETFL");
1992 bar_print_legacy(struct swm_region *r, const char *s)
1994 xcb_rectangle_t rect;
1999 XRectangle ibox, lbox;
2003 XmbTextExtents(bar_fs, s, len, &ibox, &lbox);
2005 switch (bar_justify) {
2006 case SWM_BAR_JUSTIFY_LEFT:
2009 case SWM_BAR_JUSTIFY_CENTER:
2010 x = (WIDTH(r) - lbox.width) / 2;
2012 case SWM_BAR_JUSTIFY_RIGHT:
2013 x = WIDTH(r) - lbox.width - SWM_BAR_OFFSET;
2017 if (x < SWM_BAR_OFFSET)
2022 rect.width = WIDTH(r->bar);
2023 rect.height = HEIGHT(r->bar);
2025 /* clear back buffer */
2026 gcv[0] = r->s->c[SWM_S_COLOR_BAR].pixel;
2027 xcb_change_gc(conn, r->s->bar_gc, XCB_GC_FOREGROUND, gcv);
2028 xcb_poly_fill_rectangle(conn, r->bar->buffer, r->s->bar_gc, 1, &rect);
2030 /* draw back buffer */
2031 gcvd.graphics_exposures = 0;
2032 draw = XCreateGC(display, r->bar->buffer, GCGraphicsExposures, &gcvd);
2033 XSetForeground(display, draw, r->s->c[SWM_S_COLOR_BAR_FONT].pixel);
2034 DRAWSTRING(display, r->bar->buffer, bar_fs, draw,
2035 x, (bar_fs_extents->max_logical_extent.height - lbox.height) / 2 -
2037 XFreeGC(display, draw);
2040 xcb_copy_area(conn, r->bar->buffer, r->bar->id, r->s->bar_gc, 0, 0,
2041 0, 0, WIDTH(r->bar), HEIGHT(r->bar));
2045 bar_print(struct swm_region *r, const char *s)
2048 xcb_rectangle_t rect;
2056 XftTextExtentsUtf8(display, bar_font, (FcChar8 *)s, len, &info);
2058 switch (bar_justify) {
2059 case SWM_BAR_JUSTIFY_LEFT:
2062 case SWM_BAR_JUSTIFY_CENTER:
2063 x = (WIDTH(r) - info.width) / 2;
2065 case SWM_BAR_JUSTIFY_RIGHT:
2066 x = WIDTH(r) - info.width - SWM_BAR_OFFSET;
2070 if (x < SWM_BAR_OFFSET)
2075 rect.width = WIDTH(r->bar);
2076 rect.height = HEIGHT(r->bar);
2078 /* clear back buffer */
2079 gcv[0] = r->s->c[SWM_S_COLOR_BAR].pixel;
2080 xcb_change_gc(conn, r->s->bar_gc, XCB_GC_FOREGROUND, gcv);
2081 xcb_poly_fill_rectangle(conn, r->bar->buffer, r->s->bar_gc, 1, &rect);
2083 /* draw back buffer */
2084 draw = XftDrawCreate(display, r->bar->buffer,
2085 DefaultVisual(display, r->s->idx),
2086 DefaultColormap(display, r->s->idx));
2088 XftDrawStringUtf8(draw, &bar_font_color, bar_font, x,
2089 (HEIGHT(r->bar) + bar_font->height) / 2 - bar_font->descent,
2092 XftDrawDestroy(draw);
2095 xcb_copy_area(conn, r->bar->buffer, r->bar->id, r->s->bar_gc, 0, 0,
2096 0, 0, WIDTH(r->bar), HEIGHT(r->bar));
2100 bar_extra_stop(void)
2104 bzero(bar_pipe, sizeof bar_pipe);
2107 kill(bar_pid, SIGTERM);
2110 strlcpy(bar_ext, "", sizeof bar_ext);
2115 bar_window_class(char *s, size_t sz, struct swm_region *r)
2117 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
2119 if (r->ws->focus->ch.class_name != NULL)
2120 strlcat(s, r->ws->focus->ch.class_name, sz);
2124 bar_window_instance(char *s, size_t sz, struct swm_region *r)
2126 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
2128 if (r->ws->focus->ch.instance_name != NULL)
2129 strlcat(s, r->ws->focus->ch.instance_name, sz);
2133 bar_window_class_instance(char *s, size_t sz, struct swm_region *r)
2135 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
2138 bar_window_class(s, sz, r);
2139 strlcat(s, ":", sz);
2140 bar_window_instance(s, sz, r);
2144 bar_window_state(char *s, size_t sz, struct swm_region *r)
2146 if (r == NULL || r ->ws == NULL || r->ws->focus == NULL)
2148 if (MAXIMIZED(r->ws->focus))
2149 strlcat(s, "(m)", sz);
2150 else if (ABOVE(r->ws->focus))
2151 strlcat(s, "(f)", sz);
2155 bar_window_name(char *s, size_t sz, struct swm_region *r)
2159 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
2162 title = get_win_name(r->ws->focus->id);
2163 strlcat(s, title, sz);
2168 bar_urgent(char *s, size_t sz)
2171 int i, j, num_screens;
2172 bool urgent[SWM_WS_MAX];
2174 xcb_get_property_cookie_t c;
2175 xcb_icccm_wm_hints_t hints;
2177 for (i = 0; i < workspace_limit; i++)
2180 num_screens = get_screen_count();
2181 for (i = 0; i < num_screens; i++)
2182 for (j = 0; j < workspace_limit; j++)
2183 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry) {
2184 c = xcb_icccm_get_wm_hints(conn, win->id);
2185 if (xcb_icccm_get_wm_hints_reply(conn, c,
2188 if (hints.flags & XCB_ICCCM_WM_HINT_X_URGENCY)
2192 for (i = 0; i < workspace_limit; i++) {
2194 snprintf(b, sizeof b, "%d ", i + 1);
2196 snprintf(b, sizeof b, "- ");
2202 bar_workspace_name(char *s, size_t sz, struct swm_region *r)
2204 if (r == NULL || r->ws == NULL)
2206 if (r->ws->name != NULL)
2207 strlcat(s, r->ws->name, sz);
2210 /* build the default bar format according to the defined enabled options */
2212 bar_fmt(const char *fmtexp, char *fmtnew, struct swm_region *r, size_t sz)
2216 /* if format provided, just copy the buffers */
2217 if (bar_format != NULL) {
2218 strlcpy(fmtnew, fmtexp, sz);
2222 /* reset the output buffer */
2225 strlcat(fmtnew, "+N:+I ", sz);
2227 strlcat(fmtnew, "+S", sz);
2228 strlcat(fmtnew, " ", sz);
2230 /* only show the workspace name if there's actually one */
2231 if (r != NULL && r->ws != NULL && r->ws->name != NULL)
2232 strlcat(fmtnew, "<+D>", sz);
2234 /* If enabled, only show the iconic count if there are iconic wins. */
2235 if (iconic_enabled && r != NULL && r->ws != NULL)
2236 TAILQ_FOREACH(w, &r->ws->winlist, entry)
2238 strlcat(fmtnew, "{+M}", sz);
2242 strlcat(fmtnew, "+3<", sz);
2244 if (clock_enabled) {
2245 strlcat(fmtnew, fmtexp, sz);
2246 strlcat(fmtnew, "+4<", sz);
2249 /* bar_urgent already adds the space before the last asterisk */
2251 strlcat(fmtnew, "* +U*+4<", sz);
2253 if (window_class_enabled) {
2254 strlcat(fmtnew, "+C", sz);
2255 if (!window_instance_enabled)
2256 strlcat(fmtnew, "+4<", sz);
2259 /* checks needed by the colon and floating strlcat(3) calls below */
2260 if (r != NULL && r->ws != NULL && r->ws->focus != NULL) {
2261 if (window_instance_enabled) {
2262 if (window_class_enabled)
2263 strlcat(fmtnew, ":", sz);
2264 strlcat(fmtnew, "+T+4<", sz);
2266 if (window_name_enabled) {
2267 if (ABOVE(r->ws->focus) || MAXIMIZED(r->ws->focus))
2268 strlcat(fmtnew, "+F ", sz);
2269 strlcat(fmtnew, "+64W ", sz);
2273 /* finally add the action script output and the version */
2274 strlcat(fmtnew, "+4<+A+4<+V", sz);
2278 bar_replace_pad(char *tmp, int *limit, size_t sz)
2280 /* special case; no limit given, pad one space, instead */
2281 if (*limit == (int)sz - 1)
2283 snprintf(tmp, sz, "%*s", *limit, " ");
2286 /* replaces the bar format character sequences (like in tmux(1)) */
2288 bar_replace_seq(char *fmt, char *fmtrep, struct swm_region *r, size_t *offrep,
2293 char tmp[SWM_BAR_MAX];
2294 int limit, size, count;
2297 /* reset strlcat(3) buffer */
2300 /* get number, if any */
2303 if (sscanf(fmt, "%d%n", &limit, &size) != 1)
2304 limit = sizeof tmp - 1;
2305 if (limit <= 0 || limit >= (int)sizeof tmp)
2306 limit = sizeof tmp - 1;
2308 /* there is nothing to replace (ie EOL) */
2315 bar_replace_pad(tmp, &limit, sizeof tmp);
2318 snprintf(tmp, sizeof tmp, "%s", bar_ext);
2321 bar_window_class(tmp, sizeof tmp, r);
2324 bar_workspace_name(tmp, sizeof tmp, r);
2327 bar_window_state(tmp, sizeof tmp, r);
2330 snprintf(tmp, sizeof tmp, "%d", r->ws->idx + 1);
2334 TAILQ_FOREACH(w, &r->ws->winlist, entry)
2338 snprintf(tmp, sizeof tmp, "%d", count);
2341 snprintf(tmp, sizeof tmp, "%d", r->s->idx + 1);
2344 bar_window_class_instance(tmp, sizeof tmp, r);
2347 snprintf(tmp, sizeof tmp, "%s", r->ws->stacker);
2350 bar_window_instance(tmp, sizeof tmp, r);
2353 bar_urgent(tmp, sizeof tmp);
2356 snprintf(tmp, sizeof tmp, "%s", bar_vertext);
2359 bar_window_name(tmp, sizeof tmp, r);
2362 /* unknown character sequence; copy as-is */
2363 snprintf(tmp, sizeof tmp, "+%c", *fmt);
2369 if ((int)len < limit)
2371 while (limit-- > 0) {
2372 if (*offrep >= sz - 1)
2374 fmtrep[(*offrep)++] = *ptr++;
2382 bar_replace(char *fmt, char *fmtrep, struct swm_region *r, size_t sz)
2387 while (*fmt != '\0') {
2389 /* skip ordinary characters */
2392 fmtrep[off++] = *fmt++;
2396 /* character sequence found; replace it */
2397 fmt = bar_replace_seq(fmt, fmtrep, r, &off, sz);
2406 bar_fmt_expand(char *fmtexp, size_t sz)
2413 /* start by grabbing the current time and date */
2415 localtime_r(&tmt, &tm);
2417 /* figure out what to expand */
2418 if (bar_format != NULL)
2420 else if (bar_format == NULL && clock_enabled)
2422 /* if nothing to expand bail out */
2428 /* copy as-is, just in case the format shouldn't be expanded below */
2429 strlcpy(fmtexp, fmt, sz);
2430 /* finally pass the string through strftime(3) */
2431 #ifndef SWM_DENY_CLOCK_FORMAT
2432 if ((len = strftime(fmtexp, sz, fmt, &tm)) == 0)
2433 warnx("format too long");
2438 /* Redraws the bar; need to follow with xcb_flush() or focus_flush(). */
2442 char fmtexp[SWM_BAR_MAX], fmtnew[SWM_BAR_MAX];
2443 char fmtrep[SWM_BAR_MAX];
2445 struct swm_region *r;
2447 /* expand the format by first passing it through strftime(3) */
2448 bar_fmt_expand(fmtexp, sizeof fmtexp);
2450 num_screens = get_screen_count();
2451 for (i = 0; i < num_screens; i++) {
2452 TAILQ_FOREACH(r, &screens[i].rl, entry) {
2456 if (bar_enabled && r->ws->bar_enabled)
2457 xcb_map_window(conn, r->bar->id);
2459 xcb_unmap_window(conn, r->bar->id);
2463 if (startup_exception)
2464 snprintf(fmtrep, sizeof fmtrep, "total "
2465 "exceptions: %d, first exception: %s",
2469 bar_fmt(fmtexp, fmtnew, r, sizeof fmtnew);
2470 bar_replace(fmtnew, fmtrep, r, sizeof fmtrep);
2472 if (bar_font_legacy)
2473 bar_print_legacy(r, fmtrep);
2475 bar_print(r, fmtrep);
2481 * Reads external script output; call when stdin is readable.
2482 * Returns 1 if bar_ext was updated; otherwise 0.
2485 bar_extra_update(void)
2488 char b[SWM_BAR_MAX];
2489 bool changed = false;
2494 while (fgets(b, sizeof(b), stdin) != NULL) {
2497 if (b[len - 1] == '\n') {
2498 /* Remove newline. */
2501 /* "Clear" bar_ext. */
2504 /* Flush buffered output. */
2505 strlcpy(bar_ext, bar_ext_buf, sizeof(bar_ext));
2506 bar_ext_buf[0] = '\0';
2508 /* Append new output to bar. */
2509 strlcat(bar_ext, b, sizeof(bar_ext));
2513 /* Buffer output. */
2514 strlcat(bar_ext_buf, b, sizeof(bar_ext_buf));
2519 if (errno != EAGAIN) {
2520 warn("bar_action failed");
2529 bar_toggle(struct swm_region *r, union arg *args)
2531 struct swm_region *tmpr;
2534 /* suppress unused warnings since vars are needed */
2538 DNPRINTF(SWM_D_BAR, "bar_toggle\n");
2541 case SWM_ARG_ID_BAR_TOGGLE_WS:
2542 /* Only change if master switch is enabled. */
2544 r->ws->bar_enabled = !r->ws->bar_enabled;
2546 bar_enabled = r->ws->bar_enabled = true;
2548 case SWM_ARG_ID_BAR_TOGGLE:
2549 bar_enabled = !bar_enabled;
2553 /* update bars as necessary */
2554 num_screens = get_screen_count();
2555 for (i = 0; i < num_screens; i++)
2556 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
2558 if (bar_enabled && tmpr->ws->bar_enabled)
2559 xcb_map_window(conn, tmpr->bar->id);
2561 xcb_unmap_window(conn, tmpr->bar->id);
2566 /* must be after stack */
2573 bar_extra_setup(void)
2575 /* do this here because the conf file is in memory */
2576 if (!bar_extra && bar_argv[0]) {
2577 /* launch external status app */
2579 if (pipe(bar_pipe) == -1)
2580 err(1, "pipe error");
2581 socket_setnonblock(bar_pipe[0]);
2582 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
2584 /* Set stdin to read from the pipe. */
2585 if (dup2(bar_pipe[0], STDIN_FILENO) == -1)
2588 /* Set stdout to write to the pipe. */
2589 if (dup2(bar_pipe[1], STDOUT_FILENO) == -1)
2592 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
2593 err(1, "could not disable SIGPIPE");
2594 switch (bar_pid = fork()) {
2596 err(1, "cannot fork");
2600 execvp(bar_argv[0], bar_argv);
2601 err(1, "%s external app failed", bar_argv[0]);
2603 default: /* parent */
2608 atexit(kill_bar_extra_atexit);
2613 kill_bar_extra_atexit(void)
2616 kill(bar_pid, SIGTERM);
2624 while ((s = index(s, '-'))) {
2629 return (count == 14);
2635 char *default_string;
2636 char **missing_charsets;
2637 int num_missing_charsets = 0;
2641 XFreeFontSet(display, bar_fs);
2645 DNPRINTF(SWM_D_INIT, "fontset_init: loading bar_fonts: %s\n", bar_fonts);
2647 bar_fs = XCreateFontSet(display, bar_fonts, &missing_charsets,
2648 &num_missing_charsets, &default_string);
2650 if (num_missing_charsets > 0) {
2651 warnx("Unable to load charset(s):");
2653 for (i = 0; i < num_missing_charsets; ++i)
2654 warnx("%s", missing_charsets[i]);
2656 XFreeStringList(missing_charsets);
2658 if (strcmp(default_string, ""))
2659 warnx("Glyphs from those sets will be replaced "
2660 "by '%s'.", default_string);
2662 warnx("Glyphs from those sets won't be drawn.");
2666 errx(1, "Error creating font set structure.");
2668 bar_fs_extents = XExtentsOfFontSet(bar_fs);
2670 bar_height = bar_fs_extents->max_logical_extent.height +
2671 2 * bar_border_width;
2678 xft_init(struct swm_region *r)
2680 char *font, *d, *search;
2683 if (bar_font == NULL) {
2684 if ((d = strdup(bar_fonts)) == NULL)
2685 errx(1, "insufficient memory.");
2687 while ((font = strsep(&search, ",")) != NULL) {
2691 DNPRINTF(SWM_D_INIT, "xft_init: try font %s\n", font);
2694 bar_font = XftFontOpenXlfd(display, r->s->idx,
2697 bar_font = XftFontOpenName(display, r->s->idx,
2701 if (bar_font == NULL) {
2702 warnx("unable to load font %s", font);
2705 DNPRINTF(SWM_D_INIT, "successfully opened "
2713 if (bar_font == NULL)
2714 errx(1, "unable to open a font");
2716 PIXEL_TO_XRENDERCOLOR(r->s->c[SWM_S_COLOR_BAR_FONT].pixel, color);
2718 if (!XftColorAllocValue(display, DefaultVisual(display, r->s->idx),
2719 DefaultColormap(display, r->s->idx), &color, &bar_font_color))
2720 warn("Xft error: unable to allocate color.");
2722 PIXEL_TO_XRENDERCOLOR(r->s->c[SWM_S_COLOR_BAR].pixel, color);
2724 if (!XftColorAllocValue(display, DefaultVisual(display, r->s->idx),
2725 DefaultColormap(display, r->s->idx), &color, &search_font_color))
2726 warn("Xft error: unable to allocate color.");
2728 bar_height = bar_font->height + 2 * bar_border_width;
2735 bar_setup(struct swm_region *r)
2737 xcb_screen_t *screen;
2740 DNPRINTF(SWM_D_BAR, "bar_setup: screen %d.\n",
2743 if ((screen = get_screen(r->s->idx)) == NULL)
2744 errx(1, "ERROR: can't get screen %d.", r->s->idx);
2749 if ((r->bar = calloc(1, sizeof(struct swm_bar))) == NULL)
2750 err(1, "bar_setup: calloc: failed to allocate memory.");
2752 if (bar_font_legacy)
2758 Y(r->bar) = bar_at_bottom ? (Y(r) + HEIGHT(r) - bar_height) : Y(r);
2759 WIDTH(r->bar) = WIDTH(r) - 2 * bar_border_width;
2760 HEIGHT(r->bar) = bar_height - 2 * bar_border_width;
2762 /* Assume region is unfocused when we create the bar. */
2763 r->bar->id = xcb_generate_id(conn);
2764 wa[0] = r->s->c[SWM_S_COLOR_BAR].pixel;
2765 wa[1] = r->s->c[SWM_S_COLOR_BAR_BORDER_UNFOCUS].pixel;
2766 wa[2] = XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_POINTER_MOTION |
2767 XCB_EVENT_MASK_POINTER_MOTION_HINT;
2769 xcb_create_window(conn, XCB_COPY_FROM_PARENT, r->bar->id, r->s->root,
2770 X(r->bar), Y(r->bar), WIDTH(r->bar), HEIGHT(r->bar),
2771 bar_border_width, XCB_WINDOW_CLASS_INPUT_OUTPUT,
2772 XCB_COPY_FROM_PARENT, XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL
2773 | XCB_CW_EVENT_MASK, wa);
2775 /* Stack bar window above region window to start. */
2777 wa[1] = XCB_STACK_MODE_ABOVE;
2779 xcb_configure_window(conn, r->bar->id, XCB_CONFIG_WINDOW_SIBLING |
2780 XCB_CONFIG_WINDOW_STACK_MODE, wa);
2782 r->bar->buffer = xcb_generate_id(conn);
2783 xcb_create_pixmap(conn, screen->root_depth, r->bar->buffer, r->bar->id,
2784 WIDTH(r->bar), HEIGHT(r->bar));
2787 xcb_randr_select_input(conn, r->bar->id,
2788 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE);
2791 xcb_map_window(conn, r->bar->id);
2793 DNPRINTF(SWM_D_BAR, "bar_setup: win %#x, (x,y) w x h: (%d,%d) "
2794 "%d x %d\n", WINID(r->bar), X(r->bar), Y(r->bar), WIDTH(r->bar),
2801 bar_cleanup(struct swm_region *r)
2805 xcb_destroy_window(conn, r->bar->id);
2806 xcb_free_pixmap(conn, r->bar->buffer);
2812 set_win_state(struct ws_win *win, uint8_t state)
2814 uint16_t data[2] = { state, XCB_ATOM_NONE };
2816 DNPRINTF(SWM_D_EVENT, "set_win_state: win %#x, state: %u\n",
2822 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id, a_state,
2823 a_state, 32, 2, data);
2827 get_win_state(xcb_window_t w)
2829 xcb_get_property_reply_t *r;
2830 xcb_get_property_cookie_t c;
2831 uint32_t result = 0;
2833 c = xcb_get_property(conn, 0, w, a_state, a_state, 0L, 2L);
2834 r = xcb_get_property_reply(conn, c, NULL);
2836 if (r->type == a_state && r->format == 32 && r->length == 2)
2837 result = *((uint32_t *)xcb_get_property_value(r));
2841 DNPRINTF(SWM_D_MISC, "get_win_state property: win %#x state %u\n", w,
2847 version(struct swm_region *r, union arg *args)
2849 /* suppress unused warnings since vars are needed */
2853 bar_version = !bar_version;
2855 snprintf(bar_vertext, sizeof bar_vertext,
2856 "Version: %s Build: %s", SPECTRWM_VERSION, buildstr);
2858 strlcpy(bar_vertext, "", sizeof bar_vertext);
2865 client_msg(struct ws_win *win, xcb_atom_t a, xcb_timestamp_t t)
2867 xcb_client_message_event_t ev;
2875 name = get_atom_name(a);
2876 DNPRINTF(SWM_D_EVENT, "client_msg: win %#x, atom: %s(%u), "
2878 win->id, name, a, t);
2882 bzero(&ev, sizeof ev);
2883 ev.response_type = XCB_CLIENT_MESSAGE;
2884 ev.window = win->id;
2887 ev.data.data32[0] = a;
2888 ev.data.data32[1] = t;
2890 xcb_send_event(conn, 0, win->id,
2891 XCB_EVENT_MASK_NO_EVENT, (const char *)&ev);
2894 /* synthetic response to a ConfigureRequest when not making a change */
2896 config_win(struct ws_win *win, xcb_configure_request_event_t *ev)
2898 xcb_configure_notify_event_t ce;
2903 /* send notification of unchanged state. */
2904 bzero(&ce, sizeof(ce));
2905 ce.response_type = XCB_CONFIGURE_NOTIFY;
2908 ce.width = WIDTH(win);
2909 ce.height = HEIGHT(win);
2910 ce.override_redirect = 0;
2915 ce.window = win->id;
2916 ce.border_width = BORDER(win);
2917 ce.above_sibling = XCB_WINDOW_NONE;
2920 ce.event = ev->window;
2921 ce.window = ev->window;
2923 /* make response appear more WM_SIZE_HINTS-compliant */
2924 if (win->sh.flags) {
2925 DNPRINTF(SWM_D_MISC, "config_win: hints: win %#x,"
2926 " sh.flags: %u, min: %d x %d, max: %d x %d, inc: "
2927 "%d x %d\n", win->id, win->sh.flags, SH_MIN_W(win),
2928 SH_MIN_H(win), SH_MAX_W(win), SH_MAX_H(win),
2929 SH_INC_W(win), SH_INC_H(win));
2934 /* the hint may be set... to 0! */
2935 if (SH_MIN_W(win) > 0 && ce.width < SH_MIN_W(win))
2936 ce.width = SH_MIN_W(win);
2937 if (SH_MIN_H(win) > 0 && ce.height < SH_MIN_H(win))
2938 ce.height = SH_MIN_H(win);
2943 /* may also be advertized as 0 */
2944 if (SH_MAX_W(win) > 0 && ce.width > SH_MAX_W(win))
2945 ce.width = SH_MAX_W(win);
2946 if (SH_MAX_H(win) > 0 && ce.height > SH_MAX_H(win))
2947 ce.height = SH_MAX_H(win);
2950 /* resize increment. */
2952 if (SH_INC_W(win) > 1 && ce.width > SH_INC_W(win))
2953 ce.width -= (ce.width - SH_MIN_W(win)) %
2955 if (SH_INC_H(win) > 1 && ce.height > SH_INC_H(win))
2956 ce.height -= (ce.height - SH_MIN_H(win)) %
2960 /* adjust x and y for requested border_width. */
2961 ce.x += BORDER(win) - ev->border_width;
2962 ce.y += BORDER(win) - ev->border_width;
2963 ce.border_width = ev->border_width;
2964 ce.above_sibling = ev->sibling;
2967 DNPRINTF(SWM_D_MISC, "config_win: ewmh: %s, win %#x, (x,y) w x h: "
2968 "(%d,%d) %d x %d, border: %d\n", YESNO(ev == NULL), win->id, ce.x,
2969 ce.y, ce.width, ce.height, ce.border_width);
2971 xcb_send_event(conn, 0, win->id, XCB_EVENT_MASK_STRUCTURE_NOTIFY,
2976 count_win(struct workspace *ws, bool count_transient)
2981 TAILQ_FOREACH(win, &ws->winlist, entry) {
2982 if (!count_transient && FLOATING(win))
2988 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
2994 quit(struct swm_region *r, union arg *args)
2996 /* suppress unused warnings since vars are needed */
3000 DNPRINTF(SWM_D_MISC, "quit\n");
3005 raise_window(struct ws_win *win)
3007 struct ws_win *target = NULL;
3008 struct swm_region *r;
3009 struct workspace *ws;
3011 if (win == NULL || (r = win->ws->r) == NULL)
3015 DNPRINTF(SWM_D_EVENT, "raise_window: win %#x\n", win->id);
3017 TAILQ_FOREACH(target, &ws->stack, stack_entry) {
3018 if (target == win || ICONIC(target))
3020 if (ws->cur_layout == &layouts[SWM_MAX_STACK])
3022 if (TRANS(win) && (win->transient == target->transient ||
3023 win->transient == target->id))
3025 if (FULLSCREEN(win))
3027 if (FULLSCREEN(target))
3031 if (MAXIMIZED(target))
3033 if (ABOVE(win) || TRANS(win))
3035 if (!ABOVE(target) && !TRANS(target))
3039 if (target != NULL) {
3040 /* Change stack position. */
3041 TAILQ_REMOVE(&ws->stack, win, stack_entry);
3042 TAILQ_INSERT_BEFORE(target, win, stack_entry);
3043 update_win_stacking(win);
3047 if (swm_debug & SWM_D_STACK) {
3048 DPRINTF("=== stacking order (top down) === \n");
3049 TAILQ_FOREACH(target, &r->ws->stack, stack_entry) {
3050 DPRINTF("win %#x, fs: %s, maximized: %s, above: %s, "
3051 "iconic: %s\n", target->id, YESNO(FULLSCREEN(target)),
3052 YESNO(MAXIMIZED(target)), YESNO(ABOVE(target)),
3053 YESNO(ICONIC(target)));
3057 DNPRINTF(SWM_D_EVENT, "raise_window: done\n");
3061 update_win_stacking(struct ws_win *win)
3063 struct ws_win *sibling;
3064 struct swm_region *r;
3067 if (win == NULL || (r = win->ws->r) == NULL)
3070 sibling = TAILQ_NEXT(win, stack_entry);
3071 if (sibling != NULL && FLOATING(win) == FLOATING(sibling))
3072 val[0] = sibling->id;
3074 val[0] = FLOATING(win) ? r->bar->id : r->id;
3076 DNPRINTF(SWM_D_EVENT, "update_win_stacking: %#x, sibling %#x\n",
3079 val[1] = XCB_STACK_MODE_ABOVE;
3081 xcb_configure_window(conn, win->id, XCB_CONFIG_WINDOW_SIBLING |
3082 XCB_CONFIG_WINDOW_STACK_MODE, val);
3086 map_window(struct ws_win *win)
3091 DNPRINTF(SWM_D_EVENT, "map_window: win %#x, mapped: %s\n",
3092 win->id, YESNO(win->mapped));
3097 xcb_map_window(conn, win->id);
3098 set_win_state(win, XCB_ICCCM_WM_STATE_NORMAL);
3103 unmap_window(struct ws_win *win)
3108 DNPRINTF(SWM_D_EVENT, "unmap_window: win %#x, mapped: %s\n", win->id,
3109 YESNO(win->mapped));
3114 xcb_unmap_window(conn, win->id);
3115 set_win_state(win, XCB_ICCCM_WM_STATE_ICONIC);
3116 win->mapped = false;
3123 int i, j, num_screens;
3125 num_screens = get_screen_count();
3126 for (i = 0; i < num_screens; i++)
3127 for (j = 0; j < workspace_limit; j++)
3128 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
3133 fake_keypress(struct ws_win *win, xcb_keysym_t keysym, uint16_t modifiers)
3135 xcb_key_press_event_t event;
3136 xcb_keycode_t *keycode;
3141 keycode = xcb_key_symbols_get_keycode(syms, keysym);
3143 DNPRINTF(SWM_D_MISC, "fake_keypress: win %#x, keycode %u\n",
3146 bzero(&event, sizeof(event));
3147 event.event = win->id;
3148 event.root = win->s->root;
3149 event.child = XCB_WINDOW_NONE;
3150 event.time = XCB_CURRENT_TIME;
3151 event.event_x = X(win);
3152 event.event_y = Y(win);
3155 event.same_screen = 1;
3156 event.detail = *keycode;
3157 event.state = modifiers;
3159 event.response_type = XCB_KEY_PRESS;
3160 xcb_send_event(conn, 1, win->id,
3161 XCB_EVENT_MASK_KEY_PRESS, (const char *)&event);
3163 event.response_type = XCB_KEY_RELEASE;
3164 xcb_send_event(conn, 1, win->id,
3165 XCB_EVENT_MASK_KEY_RELEASE, (const char *)&event);
3171 restart(struct swm_region *r, union arg *args)
3173 /* suppress unused warning since var is needed */
3177 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
3181 execvp(start_argv[0], start_argv);
3182 warn("execvp failed");
3187 get_pointer_win(xcb_window_t root)
3189 struct ws_win *win = NULL;
3190 xcb_query_pointer_reply_t *r;
3192 DNPRINTF(SWM_D_EVENT, "get_pointer_win: root: %#x.\n", root);
3194 r = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, root), NULL);
3196 win = find_window(r->child);
3198 DNPRINTF(SWM_D_EVENT, "get_pointer_win: %#x.\n",
3201 DNPRINTF(SWM_D_EVENT, "get_pointer_win: none.\n");
3210 root_to_region(xcb_window_t root, int check)
3213 struct swm_region *r = NULL;
3215 xcb_query_pointer_reply_t *qpr;
3216 xcb_get_input_focus_reply_t *gifr;
3218 DNPRINTF(SWM_D_MISC, "root_to_region: win %#x\n", root);
3220 num_screens = get_screen_count();
3221 for (i = 0; i < num_screens; i++)
3222 if (screens[i].root == root)
3225 if (check & SWM_CK_REGION)
3226 r = screens[i].r_focus;
3228 if (r == NULL && check & SWM_CK_FOCUS) {
3229 /* Try to find an actively focused window */
3230 gifr = xcb_get_input_focus_reply(conn,
3231 xcb_get_input_focus(conn), NULL);
3233 cfw = find_window(gifr->focus);
3234 if (cfw && cfw->ws->r)
3241 if (r == NULL && check & SWM_CK_POINTER) {
3242 /* No region with an active focus; try to use pointer. */
3243 qpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn,
3244 screens[i].root), NULL);
3246 DNPRINTF(SWM_D_MISC, "root_to_region: pointer: "
3247 "(%d,%d)\n", qpr->root_x, qpr->root_y);
3248 TAILQ_FOREACH(r, &screens[i].rl, entry)
3249 if (X(r) <= qpr->root_x &&
3250 qpr->root_x < MAX_X(r) &&
3251 Y(r) <= qpr->root_y &&
3252 qpr->root_y < MAX_Y(r))
3259 if (r == NULL && check & SWM_CK_FALLBACK)
3260 r = TAILQ_FIRST(&screens[i].rl);
3262 DNPRINTF(SWM_D_MISC, "root_to_region: idx: %d\n", get_region_index(r));
3268 find_unmanaged_window(xcb_window_t id)
3271 int i, j, num_screens;
3273 num_screens = get_screen_count();
3274 for (i = 0; i < num_screens; i++)
3275 for (j = 0; j < workspace_limit; j++)
3276 TAILQ_FOREACH(win, &screens[i].ws[j].unmanagedlist,
3284 find_window(xcb_window_t id)
3287 int i, j, num_screens;
3288 xcb_query_tree_reply_t *r;
3290 num_screens = get_screen_count();
3291 for (i = 0; i < num_screens; i++)
3292 for (j = 0; j < workspace_limit; j++)
3293 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
3297 r = xcb_query_tree_reply(conn, xcb_query_tree(conn, id), NULL);
3301 /* if we were looking for the parent return that window instead */
3302 if (r->parent == 0 || r->root == r->parent) {
3307 /* look for parent */
3308 for (i = 0; i < num_screens; i++)
3309 for (j = 0; j < workspace_limit; j++)
3310 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
3311 if (r->parent == win->id) {
3321 spawn(int ws_idx, union arg *args, bool close_fd)
3326 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
3328 close(xcb_get_file_descriptor(conn));
3330 setenv("LD_PRELOAD", SWM_LIB, 1);
3332 if (asprintf(&ret, "%d", ws_idx) == -1) {
3333 warn("spawn: asprintf SWM_WS");
3336 setenv("_SWM_WS", ret, 1);
3340 if (asprintf(&ret, "%d", getpid()) == -1) {
3341 warn("spawn: asprintf _SWM_PID");
3344 setenv("_SWM_PID", ret, 1);
3348 if (setsid() == -1) {
3349 warn("spawn: setsid");
3355 * close stdin and stdout to prevent interaction between apps
3356 * and the baraction script
3357 * leave stderr open to record errors
3359 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) == -1) {
3360 warn("spawn: open");
3363 dup2(fd, STDIN_FILENO);
3364 dup2(fd, STDOUT_FILENO);
3369 execvp(args->argv[0], args->argv);
3371 warn("spawn: execvp");
3376 kill_refs(struct ws_win *win)
3378 int i, x, num_screens;
3379 struct swm_region *r;
3380 struct workspace *ws;
3385 num_screens = get_screen_count();
3386 for (i = 0; i < num_screens; i++)
3387 TAILQ_FOREACH(r, &screens[i].rl, entry)
3388 for (x = 0; x < workspace_limit; x++) {
3390 if (win == ws->focus)
3392 if (win == ws->focus_prev)
3393 ws->focus_prev = NULL;
3398 validate_win(struct ws_win *testwin)
3401 struct workspace *ws;
3402 struct swm_region *r;
3403 int i, x, num_screens;
3405 if (testwin == NULL)
3408 num_screens = get_screen_count();
3409 for (i = 0; i < num_screens; i++)
3410 TAILQ_FOREACH(r, &screens[i].rl, entry)
3411 for (x = 0; x < workspace_limit; x++) {
3413 TAILQ_FOREACH(win, &ws->winlist, entry)
3421 validate_ws(struct workspace *testws)
3423 struct swm_region *r;
3424 struct workspace *ws;
3425 int i, x, num_screens;
3427 /* validate all ws */
3428 num_screens = get_screen_count();
3429 for (i = 0; i < num_screens; i++)
3430 TAILQ_FOREACH(r, &screens[i].rl, entry)
3431 for (x = 0; x < workspace_limit; x++) {
3440 unfocus_win(struct ws_win *win)
3442 xcb_window_t none = XCB_WINDOW_NONE;
3444 DNPRINTF(SWM_D_FOCUS, "unfocus_win: win %#x\n", WINID(win));
3449 if (win->ws == NULL) {
3450 DNPRINTF(SWM_D_FOCUS, "unfocus_win: NULL ws.\n");
3454 if (validate_ws(win->ws)) {
3455 DNPRINTF(SWM_D_FOCUS, "unfocus_win: invalid ws.\n");
3459 if (win->ws->r == NULL) {
3460 DNPRINTF(SWM_D_FOCUS, "unfocus_win: NULL region.\n");
3464 if (validate_win(win)) {
3465 DNPRINTF(SWM_D_FOCUS, "unfocus_win: invalid win.\n");
3470 if (win->ws->focus == win) {
3471 win->ws->focus = NULL;
3472 win->ws->focus_prev = win;
3475 if (validate_win(win->ws->focus)) {
3476 kill_refs(win->ws->focus);
3477 win->ws->focus = NULL;
3480 if (validate_win(win->ws->focus_prev)) {
3481 kill_refs(win->ws->focus_prev);
3482 win->ws->focus_prev = NULL;
3485 update_window_color(win);
3487 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
3488 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1, &none);
3490 DNPRINTF(SWM_D_FOCUS, "unfocus_win: done.\n");
3494 focus_win(struct ws_win *win)
3496 struct ws_win *cfw = NULL, *parent = NULL, *w, *tmpw;
3497 struct workspace *ws;
3498 xcb_get_input_focus_reply_t *gifr;
3500 DNPRINTF(SWM_D_FOCUS, "focus_win: win %#x\n", WINID(win));
3502 if (win == NULL || win->ws == NULL || !win->mapped)
3507 if (validate_ws(ws))
3510 if (validate_win(win)) {
3515 gifr = xcb_get_input_focus_reply(conn, xcb_get_input_focus(conn), NULL);
3517 cfw = find_window(gifr->focus);
3518 if (cfw != NULL && cfw != win) {
3519 if (cfw->ws != ws && cfw->ws->r != NULL) {
3520 /* Change border to unfocused color. */
3521 xcb_change_window_attributes(conn, cfw->id,
3522 XCB_CW_BORDER_PIXEL,
3523 &cfw->s->c[(MAXIMIZED(cfw) ?
3524 SWM_S_COLOR_UNFOCUS_MAXIMIZED :
3525 SWM_S_COLOR_UNFOCUS)].pixel);
3533 if (ws->focus != win) {
3534 if (ws->focus && ws->focus != cfw)
3535 unfocus_win(ws->focus);
3539 /* If this window directs focus to a child window, then clear. */
3540 if (win->focus_child)
3541 win->focus_child = NULL;
3543 /* If transient, adjust parent's focus child for focus_magic. */
3545 parent = find_window(win->transient);
3546 if (parent && parent->focus_child != win)
3547 parent->focus_child = win;
3550 if (cfw != win && ws->r != NULL) {
3551 /* Set input focus if no input hint, or indicated by hint. */
3552 if (!(win->hints.flags & XCB_ICCCM_WM_HINT_INPUT) ||
3553 (win->hints.flags & XCB_ICCCM_WM_HINT_INPUT &&
3555 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT,
3556 win->id, last_event_time);
3558 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT,
3559 ws->r->id, XCB_CURRENT_TIME);
3561 /* Tell app it can adjust focus to a specific window. */
3562 if (win->take_focus) {
3563 /* java is special; always tell parent */
3564 if (TRANS(win) && win->java)
3565 client_msg(parent, a_takefocus,
3568 client_msg(win, a_takefocus, last_event_time);
3571 if (ws->cur_layout->flags & SWM_L_MAPONFOCUS ||
3573 /* If a parent exists, map it first. */
3575 raise_window(parent);
3578 /* Map siblings next. */
3579 TAILQ_FOREACH_SAFE(w, &ws->stack, stack_entry,
3581 if (w != win && !ICONIC(w) &&
3582 w->transient == parent->id) {
3588 /* Map focused window. */
3592 /* Stack any children of focus window. */
3593 TAILQ_FOREACH_SAFE(w, &ws->stack, stack_entry, tmpw)
3594 if (w->transient == win->id && !ICONIC(w)) {
3598 } else if (tile_gap < 0 && !ABOVE(win)) {
3600 * Windows overlap in the layout.
3601 * Raise focused win above all tiled wins.
3609 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
3610 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1,
3615 /* Update window border even if workspace is hidden. */
3616 update_window_color(win);
3621 DNPRINTF(SWM_D_FOCUS, "focus_win: done.\n");
3624 /* If a child window should have focus instead, return it. */
3626 get_focus_magic(struct ws_win *win)
3628 struct ws_win *parent = NULL;
3629 struct ws_win *child = NULL;
3631 DNPRINTF(SWM_D_FOCUS, "get_focus_magic: win %#x\n", WINID(win));
3636 parent = find_window(win->transient);
3638 /* If parent prefers focus elsewhere, then try to do so. */
3639 if (parent && (child = parent->focus_child)) {
3640 if (validate_win(child) == 0 && child->mapped)
3643 parent->focus_child = NULL;
3647 /* If this window prefers focus elsewhere, then try to do so. */
3648 if ((child = win->focus_child)) {
3649 if (validate_win(child) == 0 && child->mapped)
3652 win->focus_child = NULL;
3659 event_drain(uint8_t rt)
3661 xcb_generic_event_t *evt;
3663 /* ensure all pending requests have been processed before filtering. */
3665 while ((evt = xcb_poll_for_event(conn))) {
3666 if (XCB_EVENT_RESPONSE_TYPE(evt) != rt)
3674 set_region(struct swm_region *r)
3676 struct swm_region *rf;
3683 /* Unfocus old region bar. */
3688 xcb_change_window_attributes(conn, rf->bar->id,
3689 XCB_CW_BORDER_PIXEL,
3690 &r->s->c[SWM_S_COLOR_BAR_BORDER_UNFOCUS].pixel);
3693 if (rf != NULL && rf != r && (X(rf) != X(r) || Y(rf) != Y(r) ||
3694 WIDTH(rf) != WIDTH(r) || HEIGHT(rf) != HEIGHT(r))) {
3695 /* Set _NET_DESKTOP_GEOMETRY. */
3697 vals[1] = HEIGHT(r);
3698 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, r->s->root,
3699 ewmh[_NET_DESKTOP_GEOMETRY].atom, XCB_ATOM_CARDINAL, 32, 2,
3703 /* Set region bar border to focus_color. */
3704 xcb_change_window_attributes(conn, r->bar->id,
3705 XCB_CW_BORDER_PIXEL, &r->s->c[SWM_S_COLOR_BAR_BORDER].pixel);
3709 ewmh_update_current_desktop();
3713 focus_region(struct swm_region *r)
3716 struct swm_region *old_r;
3721 old_r = r->s->r_focus;
3724 nfw = get_region_focus(r);
3728 /* New region is empty; need to manually unfocus win. */
3730 unfocus_win(old_r->ws->focus);
3732 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT, r->id,
3735 /* Clear bar since empty. */
3741 switchws(struct swm_region *r, union arg *args)
3743 struct swm_region *this_r, *other_r;
3745 struct workspace *new_ws, *old_ws;
3746 xcb_window_t none = XCB_WINDOW_NONE;
3747 int wsid = args->id;
3748 bool unmap_old = false;
3753 if (wsid >= workspace_limit)
3757 old_ws = this_r->ws;
3758 new_ws = &this_r->s->ws[wsid];
3760 DNPRINTF(SWM_D_WS, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
3761 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), old_ws->idx, wsid);
3763 if (new_ws == NULL || old_ws == NULL)
3765 if (new_ws == old_ws)
3768 if ((win = old_ws->focus) != NULL) {
3769 update_window_color(win);
3771 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
3772 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1,
3776 other_r = new_ws->r;
3777 if (other_r == NULL) {
3778 /* the other workspace is hidden, hide this one */
3782 /* the other ws is visible in another region, exchange them */
3783 other_r->ws_prior = new_ws;
3784 other_r->ws = old_ws;
3785 old_ws->r = other_r;
3787 this_r->ws_prior = old_ws;
3788 this_r->ws = new_ws;
3791 /* Set focus_pending before stacking, if needed. */
3792 if (focus_mode != SWM_FOCUS_FOLLOW && (!new_ws->focus_pending ||
3793 validate_win(new_ws->focus_pending))) {
3794 new_ws->focus_pending = get_region_focus(new_ws->r);
3795 new_ws->focus = new_ws->focus_prev;
3796 new_ws->focus_prev = NULL;
3799 new_ws->state = SWM_WS_STATE_MAPPING;
3802 /* unmap old windows */
3804 TAILQ_FOREACH(win, &old_ws->winlist, entry)
3806 old_ws->state = SWM_WS_STATE_HIDDEN;
3809 /* if workspaces were swapped, then don't wait to set focus */
3810 if (old_ws->r && focus_mode != SWM_FOCUS_FOLLOW) {
3811 if (new_ws->focus_pending) {
3812 focus_win(new_ws->focus_pending);
3813 new_ws->focus_pending = NULL;
3817 /* Clear bar and set focus on region input win if new ws is empty. */
3818 if (new_ws->focus_pending == NULL && new_ws->focus == NULL) {
3819 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT, r->id,
3824 ewmh_update_current_desktop();
3827 new_ws->state = SWM_WS_STATE_MAPPED;
3829 DNPRINTF(SWM_D_WS, "switchws: done.\n");
3833 cyclews(struct swm_region *r, union arg *args)
3836 struct swm_screen *s = r->s;
3837 bool cycle_all = false, mv = false;
3839 DNPRINTF(SWM_D_WS, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3840 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
3846 case SWM_ARG_ID_CYCLEWS_MOVE_UP:
3849 case SWM_ARG_ID_CYCLEWS_UP_ALL:
3852 case SWM_ARG_ID_CYCLEWS_UP:
3853 a.id = (a.id < workspace_limit - 1) ? a.id + 1 : 0;
3855 case SWM_ARG_ID_CYCLEWS_MOVE_DOWN:
3858 case SWM_ARG_ID_CYCLEWS_DOWN_ALL:
3861 case SWM_ARG_ID_CYCLEWS_DOWN:
3862 a.id = (a.id > 0) ? a.id - 1 : workspace_limit - 1;
3869 (!cycle_empty && TAILQ_EMPTY(&s->ws[a.id].winlist)))
3871 if (!cycle_visible && s->ws[a.id].r != NULL)
3878 } while (a.id != r->ws->idx);
3880 DNPRINTF(SWM_D_FOCUS, "cyclews: done\n");
3884 priorws(struct swm_region *r, union arg *args)
3890 DNPRINTF(SWM_D_WS, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3891 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
3893 if (r->ws_prior == NULL)
3896 a.id = r->ws_prior->idx;
3898 DNPRINTF(SWM_D_FOCUS, "priorws: done\n");
3902 focusrg(struct swm_region *r, union arg *args)
3904 int ridx = args->id, i, num_screens;
3905 struct swm_region *rr = NULL;
3907 num_screens = get_screen_count();
3908 /* do nothing if we don't have more than one screen */
3909 if (!(num_screens > 1 || outputs > 1))
3912 DNPRINTF(SWM_D_FOCUS, "focusrg: id: %d\n", ridx);
3914 rr = TAILQ_FIRST(&r->s->rl);
3915 for (i = 0; i < ridx && rr != NULL; ++i)
3916 rr = TAILQ_NEXT(rr, entry);
3923 DNPRINTF(SWM_D_FOCUS, "focusrg: done\n");
3927 cyclerg(struct swm_region *r, union arg *args)
3929 struct swm_region *rr = NULL;
3932 num_screens = get_screen_count();
3933 /* do nothing if we don't have more than one screen */
3934 if (!(num_screens > 1 || outputs > 1))
3938 DNPRINTF(SWM_D_FOCUS, "cyclerg: id: %d, region: %d\n", args->id, i);
3941 case SWM_ARG_ID_CYCLERG_UP:
3942 rr = TAILQ_NEXT(r, entry);
3944 rr = TAILQ_FIRST(&screens[i].rl);
3946 case SWM_ARG_ID_CYCLERG_DOWN:
3947 rr = TAILQ_PREV(r, swm_region_list, entry);
3949 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
3959 DNPRINTF(SWM_D_FOCUS, "cyclerg: done\n");
3962 /* Sorts transients after parent. */
3964 sort_windows(struct ws_win_list *wl)
3966 struct ws_win *win, *parent, *nxt;
3971 for (win = TAILQ_FIRST(wl); win != TAILQ_END(wl); win = nxt) {
3972 nxt = TAILQ_NEXT(win, entry);
3974 parent = find_window(win->transient);
3975 if (parent == NULL) {
3976 warnx("not possible bug");
3979 TAILQ_REMOVE(wl, win, entry);
3980 TAILQ_INSERT_AFTER(wl, parent, win, entry);
3986 swapwin(struct swm_region *r, union arg *args)
3988 struct ws_win *target, *source;
3989 struct ws_win *cur_focus;
3990 struct ws_win_list *wl;
3992 DNPRINTF(SWM_D_WS, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3993 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
3995 cur_focus = r->ws->focus;
3996 if (cur_focus == NULL || FULLSCREEN(cur_focus))
3999 /* Adjust stacking in floating layer. */
4000 if (ABOVE(cur_focus)) {
4002 case SWM_ARG_ID_SWAPPREV:
4003 target = TAILQ_PREV(cur_focus, ws_win_stack,
4005 if (target != NULL && FLOATING(target)) {
4006 TAILQ_REMOVE(&cur_focus->ws->stack, cur_focus,
4008 TAILQ_INSERT_BEFORE(target, cur_focus,
4010 update_win_stacking(cur_focus);
4014 case SWM_ARG_ID_SWAPNEXT:
4015 target = TAILQ_NEXT(cur_focus, stack_entry);
4016 if (target != NULL && FLOATING(target)) {
4017 TAILQ_REMOVE(&cur_focus->ws->stack, cur_focus,
4019 TAILQ_INSERT_AFTER(&cur_focus->ws->stack,
4020 target, cur_focus, stack_entry);
4021 update_win_stacking(cur_focus);
4029 if (r->ws->cur_layout == &layouts[SWM_MAX_STACK])
4032 clear_maximized(r->ws);
4035 wl = &source->ws->winlist;
4038 case SWM_ARG_ID_SWAPPREV:
4040 source = find_window(source->transient);
4041 target = TAILQ_PREV(source, ws_win_list, entry);
4042 if (target && target->transient)
4043 target = find_window(target->transient);
4044 TAILQ_REMOVE(wl, source, entry);
4046 TAILQ_INSERT_TAIL(wl, source, entry);
4048 TAILQ_INSERT_BEFORE(target, source, entry);
4050 case SWM_ARG_ID_SWAPNEXT:
4051 target = TAILQ_NEXT(source, entry);
4052 /* move the parent and let the sort handle the move */
4054 source = find_window(source->transient);
4055 TAILQ_REMOVE(wl, source, entry);
4057 TAILQ_INSERT_HEAD(wl, source, entry);
4059 TAILQ_INSERT_AFTER(wl, target, source, entry);
4061 case SWM_ARG_ID_SWAPMAIN:
4062 target = TAILQ_FIRST(wl);
4063 if (target == source) {
4064 if (source->ws->focus_prev != NULL &&
4065 source->ws->focus_prev != target)
4066 source = source->ws->focus_prev;
4070 if (target == NULL || source == NULL)
4072 source->ws->focus_prev = target;
4073 TAILQ_REMOVE(wl, target, entry);
4074 TAILQ_INSERT_BEFORE(source, target, entry);
4075 TAILQ_REMOVE(wl, source, entry);
4076 TAILQ_INSERT_HEAD(wl, source, entry);
4078 case SWM_ARG_ID_MOVELAST:
4079 TAILQ_REMOVE(wl, source, entry);
4080 TAILQ_INSERT_TAIL(wl, source, entry);
4083 DNPRINTF(SWM_D_MOVE, "swapwin: invalid id: %d\n", args->id);
4088 ewmh_update_client_list();
4093 DNPRINTF(SWM_D_MOVE, "swapwin: done\n");
4097 get_focus_prev(struct ws_win *win)
4099 struct ws_win *winfocus = NULL;
4100 struct ws_win *cur_focus = NULL;
4101 struct ws_win_list *wl = NULL;
4102 struct workspace *ws = NULL;
4104 if (!(win && win->ws))
4109 cur_focus = ws->focus;
4111 DNPRINTF(SWM_D_FOCUS, "get_focus_prev: win %#x, cur_focus: %#x, "
4112 "focus_prev: %#x\n", WINID(win), WINID(cur_focus),
4113 WINID(ws->focus_prev));
4115 /* pickle, just focus on whatever */
4116 if (cur_focus == NULL) {
4117 /* use prev_focus if valid */
4118 if (ws->focus_prev && find_window(ws->focus_prev->id))
4119 winfocus = ws->focus_prev;
4123 /* if transient focus on parent */
4124 if (TRANS(cur_focus)) {
4125 winfocus = find_window(cur_focus->transient);
4129 /* if in max_stack try harder */
4130 if ((win->quirks & SWM_Q_FOCUSPREV) ||
4131 (ws->cur_layout->flags & SWM_L_FOCUSPREV)) {
4132 if (cur_focus != ws->focus_prev)
4133 winfocus = ws->focus_prev;
4135 winfocus = TAILQ_PREV(win, ws_win_list, entry);
4140 DNPRINTF(SWM_D_FOCUS, "get_focus_prev: focus_close: %d\n", focus_close);
4142 if (winfocus == NULL || winfocus == win) {
4143 switch (focus_close) {
4144 case SWM_STACK_BOTTOM:
4145 TAILQ_FOREACH(winfocus, wl, entry)
4146 if (!ICONIC(winfocus) && winfocus != cur_focus)
4150 TAILQ_FOREACH_REVERSE(winfocus, wl, ws_win_list, entry)
4151 if (!ICONIC(winfocus) && winfocus != cur_focus)
4154 case SWM_STACK_ABOVE:
4155 winfocus = TAILQ_NEXT(cur_focus, entry);
4156 while (winfocus && ICONIC(winfocus))
4157 winfocus = TAILQ_NEXT(winfocus, entry);
4159 if (winfocus == NULL) {
4160 if (focus_close_wrap) {
4161 TAILQ_FOREACH(winfocus, wl, entry)
4162 if (!ICONIC(winfocus) &&
4163 winfocus != cur_focus)
4166 TAILQ_FOREACH_REVERSE(winfocus, wl,
4168 if (!ICONIC(winfocus) &&
4169 winfocus != cur_focus)
4174 case SWM_STACK_BELOW:
4175 winfocus = TAILQ_PREV(cur_focus, ws_win_list, entry);
4176 while (winfocus && ICONIC(winfocus))
4177 winfocus = TAILQ_PREV(winfocus, ws_win_list,
4180 if (winfocus == NULL) {
4181 if (focus_close_wrap) {
4182 TAILQ_FOREACH_REVERSE(winfocus, wl,
4184 if (!ICONIC(winfocus) &&
4185 winfocus != cur_focus)
4188 TAILQ_FOREACH(winfocus, wl, entry)
4189 if (!ICONIC(winfocus) &&
4190 winfocus != cur_focus)
4198 if (winfocus == NULL ||
4199 (winfocus && (ICONIC(winfocus) || winfocus == cur_focus))) {
4200 if (focus_default == SWM_STACK_TOP) {
4201 TAILQ_FOREACH_REVERSE(winfocus, wl, ws_win_list, entry)
4202 if (!ICONIC(winfocus) && winfocus != cur_focus)
4205 TAILQ_FOREACH(winfocus, wl, entry)
4206 if (!ICONIC(winfocus) && winfocus != cur_focus)
4213 return get_focus_magic(winfocus);
4217 get_region_focus(struct swm_region *r)
4219 struct ws_win *winfocus = NULL;
4224 if (r->ws->focus && !ICONIC(r->ws->focus))
4225 winfocus = r->ws->focus;
4226 else if (r->ws->focus_prev && !ICONIC(r->ws->focus_prev))
4227 winfocus = r->ws->focus_prev;
4229 TAILQ_FOREACH(winfocus, &r->ws->winlist, entry)
4230 if (!ICONIC(winfocus))
4233 return get_focus_magic(winfocus);
4237 focus(struct swm_region *r, union arg *args)
4239 struct ws_win *head, *cur_focus = NULL, *winfocus = NULL;
4240 struct ws_win_list *wl = NULL;
4241 struct workspace *ws = NULL;
4244 xcb_icccm_wm_hints_t hints;
4249 cur_focus = r->ws->focus;
4253 DNPRINTF(SWM_D_FOCUS, "focus: id: %d, cur_focus: %#x\n", args->id,
4256 /* Make sure an uniconified window has focus, if one exists. */
4257 if (cur_focus == NULL) {
4258 cur_focus = TAILQ_FIRST(wl);
4259 while (cur_focus != NULL && ICONIC(cur_focus))
4260 cur_focus = TAILQ_NEXT(cur_focus, entry);
4262 DNPRINTF(SWM_D_FOCUS, "focus: new cur_focus: %#x\n",
4267 case SWM_ARG_ID_FOCUSPREV:
4268 if (cur_focus == NULL)
4271 winfocus = cur_focus;
4273 winfocus = TAILQ_PREV(winfocus, ws_win_list, entry);
4274 if (winfocus == NULL)
4275 winfocus = TAILQ_LAST(wl, ws_win_list);
4276 if (winfocus == cur_focus)
4278 } while (winfocus && (ICONIC(winfocus) ||
4279 winfocus->id == cur_focus->transient ||
4280 (cur_focus->transient != XCB_WINDOW_NONE &&
4281 winfocus->transient == cur_focus->transient)));
4283 case SWM_ARG_ID_FOCUSNEXT:
4284 if (cur_focus == NULL)
4287 winfocus = cur_focus;
4289 winfocus = TAILQ_NEXT(winfocus, entry);
4290 if (winfocus == NULL)
4291 winfocus = TAILQ_FIRST(wl);
4292 if (winfocus == cur_focus)
4294 } while (winfocus && (ICONIC(winfocus) ||
4295 winfocus->id == cur_focus->transient ||
4296 (cur_focus->transient != XCB_WINDOW_NONE &&
4297 winfocus->transient == cur_focus->transient)));
4299 case SWM_ARG_ID_FOCUSMAIN:
4300 if (cur_focus == NULL)
4303 winfocus = TAILQ_FIRST(wl);
4304 if (winfocus == cur_focus)
4305 winfocus = cur_focus->ws->focus_prev;
4307 case SWM_ARG_ID_FOCUSURGENT:
4308 /* Search forward for the next urgent window. */
4312 for (i = 0; i <= workspace_limit; ++i) {
4314 head = TAILQ_FIRST(&r->s->ws[(ws->idx + i) %
4315 workspace_limit].winlist);
4317 while (head != NULL &&
4318 (head = TAILQ_NEXT(head, entry)) != NULL) {
4319 if (head == cur_focus) {
4320 winfocus = cur_focus;
4323 if (xcb_icccm_get_wm_hints_reply(conn,
4324 xcb_icccm_get_wm_hints(conn, head->id),
4325 &hints, NULL) != 0 &&
4326 xcb_icccm_wm_hints_get_urgency(&hints)) {
4332 if (winfocus != NULL)
4336 /* Switch ws if new focus is on a different ws. */
4337 if (winfocus != NULL && winfocus->ws != ws) {
4338 a.id = winfocus->ws->idx;
4346 if (clear_maximized(ws) > 0)
4349 focus_win(get_focus_magic(winfocus));
4353 DNPRINTF(SWM_D_FOCUS, "focus: done\n");
4357 cycle_layout(struct swm_region *r, union arg *args)
4359 struct workspace *ws = r->ws;
4361 /* suppress unused warning since var is needed */
4364 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
4367 if (ws->cur_layout->l_stack == NULL)
4368 ws->cur_layout = &layouts[0];
4370 clear_maximized(ws);
4375 focus_win(get_region_focus(r));
4381 stack_config(struct swm_region *r, union arg *args)
4383 struct workspace *ws = r->ws;
4385 DNPRINTF(SWM_D_STACK, "stack_config: id: %d workspace: %d\n",
4388 if (clear_maximized(ws) > 0)
4391 if (ws->cur_layout->l_config != NULL)
4392 ws->cur_layout->l_config(ws, args->id);
4394 if (args->id != SWM_ARG_ID_STACKINIT)
4403 struct swm_geometry g;
4404 struct swm_region *r, *r_prev = NULL;
4411 DNPRINTF(SWM_D_STACK, "stack: begin\n");
4413 num_screens = get_screen_count();
4414 for (i = 0; i < num_screens; i++) {
4418 TAILQ_FOREACH(r, &screens[i].rl, entry) {
4419 /* Adjust stack area for region bar and padding. */
4421 g.x += region_padding;
4422 g.y += region_padding;
4423 g.w -= 2 * border_width + 2 * region_padding;
4424 g.h -= 2 * border_width + 2 * region_padding;
4425 if (bar_enabled && r->ws->bar_enabled) {
4431 DNPRINTF(SWM_D_STACK, "stack: workspace: %d (screen: "
4432 "%d, region: %d), (x,y) WxH: (%d,%d) %d x %d\n",
4433 r->ws->idx, i, j++, g.x, g.y, g.w, g.h);
4436 /* Stack bar/input relative to prev. region. */
4437 val[1] = XCB_STACK_MODE_ABOVE;
4439 val[0] = r_prev->id;
4440 DNPRINTF(SWM_D_STACK, "stack: region input %#x "
4441 "relative to %#x.\n", r->id, val[0]);
4442 xcb_configure_window(conn, r->id,
4443 XCB_CONFIG_WINDOW_SIBLING |
4444 XCB_CONFIG_WINDOW_STACK_MODE, val);
4446 val[0] = r_prev->bar->id;
4447 DNPRINTF(SWM_D_STACK, "stack: region bar %#x "
4448 "relative to %#x.\n", r->bar->id, val[0]);
4449 xcb_configure_window(conn, r->bar->id,
4450 XCB_CONFIG_WINDOW_SIBLING |
4451 XCB_CONFIG_WINDOW_STACK_MODE, val);
4454 r->ws->cur_layout->l_stack(r->ws, &g);
4455 r->ws->cur_layout->l_string(r->ws);
4456 /* save r so we can track region changes */
4464 DNPRINTF(SWM_D_STACK, "stack: end\n");
4468 store_float_geom(struct ws_win *win)
4470 if (win == NULL || win->ws->r == NULL)
4473 /* retain window geom and region geom */
4474 win->g_float = win->g;
4475 win->g_float.x -= X(win->ws->r);
4476 win->g_float.y -= Y(win->ws->r);
4477 win->g_floatvalid = true;
4478 DNPRINTF(SWM_D_MISC, "store_float_geom: win %#x, g: (%d,%d)"
4479 " %d x %d, g_float: (%d,%d) %d x %d\n", win->id, X(win), Y(win),
4480 WIDTH(win), HEIGHT(win), win->g_float.x, win->g_float.y,
4481 win->g_float.w, win->g_float.h);
4485 load_float_geom(struct ws_win *win)
4487 if (win == NULL || win->ws->r == NULL)
4490 if (win->g_floatvalid) {
4491 win->g = win->g_float;
4492 X(win) += X(win->ws->r);
4493 Y(win) += Y(win->ws->r);
4494 DNPRINTF(SWM_D_MISC, "load_float_geom: win %#x, g: (%d,%d)"
4495 "%d x %d\n", win->id, X(win), Y(win), WIDTH(win),
4498 DNPRINTF(SWM_D_MISC, "load_float_geom: win %#x, g_float "
4499 "is not set.\n", win->id);
4504 update_floater(struct ws_win *win)
4506 struct workspace *ws;
4507 struct swm_region *r;
4514 if ((r = ws->r) == NULL)
4517 DNPRINTF(SWM_D_MISC, "update_floater: win %#x\n", win->id);
4519 win->bordered = true;
4521 if (FULLSCREEN(win)) {
4522 /* _NET_WM_FULLSCREEN: fullscreen without border. */
4523 if (!win->g_floatvalid)
4524 store_float_geom(win);
4527 win->bordered = false;
4528 } else if (MAXIMIZED(win)) {
4529 /* Maximize: like a single stacked window. */
4530 if (!win->g_floatvalid)
4531 store_float_geom(win);
4535 if (bar_enabled && ws->bar_enabled) {
4537 Y(win) += bar_height;
4538 HEIGHT(win) -= bar_height;
4539 } else if (disable_border) {
4540 win->bordered = false;
4543 if (win->bordered) {
4544 HEIGHT(win) -= 2 * border_width;
4545 WIDTH(win) -= 2 * border_width;
4548 /* Normal floating window. */
4549 /* Update geometry if new region. */
4551 load_float_geom(win);
4553 if ((win->quirks & SWM_Q_FULLSCREEN) &&
4554 WIDTH(win) >= WIDTH(r) && HEIGHT(win) >= HEIGHT(r)) {
4555 /* Remove border for FULLSCREEN quirk. */
4556 win->bordered = false;
4557 } else if (!MANUAL(win)) {
4558 if (TRANS(win) && (win->quirks & SWM_Q_TRANSSZ)) {
4559 /* Adjust size on TRANSSZ quirk. */
4560 WIDTH(win) = (double)WIDTH(r) * dialog_ratio;
4561 HEIGHT(win) = (double)HEIGHT(r) * dialog_ratio;
4564 if (!(win->quirks & SWM_Q_ANYWHERE)) {
4566 * Floaters and transients are auto-centred
4567 * unless manually moved, resized or ANYWHERE
4570 X(win) = X(r) + (WIDTH(r) - WIDTH(win)) / 2 -
4572 Y(win) = Y(r) + (HEIGHT(r) - HEIGHT(win)) / 2 -
4574 store_float_geom(win);
4579 /* Ensure at least 1 pixel of the window is in the region. */
4580 region_containment(win, r, SWM_CW_ALLSIDES);
4586 * Send keystrokes to terminal to decrease/increase the font size as the
4587 * window size changes.
4590 adjust_font(struct ws_win *win)
4592 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
4593 ABOVE(win) || TRANS(win))
4596 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
4597 WIDTH(win) / win->sh.width_inc < term_width &&
4598 win->font_steps < SWM_MAX_FONT_STEPS) {
4599 win->font_size_boundary[win->font_steps] =
4600 (win->sh.width_inc * term_width) + win->sh.base_width;
4603 win->last_inc = win->sh.width_inc;
4604 fake_keypress(win, XK_KP_Subtract, XCB_MOD_MASK_SHIFT);
4605 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
4606 WIDTH(win) > win->font_size_boundary[win->font_steps - 1]) {
4609 win->last_inc = win->sh.width_inc;
4610 fake_keypress(win, XK_KP_Add, XCB_MOD_MASK_SHIFT);
4614 #define SWAPXY(g) do { \
4616 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
4617 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
4620 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, bool flip)
4622 struct swm_geometry win_g, r_g = *g;
4624 int i, j, s, stacks;
4625 int w_inc = 1, h_inc, w_base = 1, h_base;
4626 int hrh, extra = 0, h_slice, last_h = 0;
4627 int split, colno, winno, mwin, msize, mscale;
4628 int remain, missing, v_slice;
4629 bool bordered = true, reconfigure = false;
4631 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d, rot: %s, "
4632 "flip: %s\n", ws->idx, YESNO(rot), YESNO(flip));
4634 /* Prepare tiling variables, if needed. */
4635 if ((winno = count_win(ws, false)) > 0) {
4636 /* Find first tiled window. */
4637 TAILQ_FOREACH(win, &ws->winlist, entry)
4638 if (!FLOATING(win) && !ICONIC(win))
4641 /* Take into account size hints of first tiled window. */
4643 w_inc = win->sh.width_inc;
4644 w_base = win->sh.base_width;
4645 mwin = ws->l_state.horizontal_mwin;
4646 mscale = ws->l_state.horizontal_msize;
4647 stacks = ws->l_state.horizontal_stacks;
4650 w_inc = win->sh.height_inc;
4651 w_base = win->sh.base_height;
4652 mwin = ws->l_state.vertical_mwin;
4653 mscale = ws->l_state.vertical_msize;
4654 stacks = ws->l_state.vertical_stacks;
4658 if (stacks > winno - mwin)
4659 stacks = winno - mwin;
4663 h_slice = r_g.h / SWM_H_SLICE;
4664 if (mwin && winno > mwin) {
4665 v_slice = r_g.w / SWM_V_SLICE;
4669 win_g.w = v_slice * mscale;
4671 if (w_inc > 1 && w_inc < v_slice) {
4672 /* Adjust for requested size increment. */
4673 remain = (win_g.w - w_base) % w_inc;
4679 win_g.x += r_g.w - msize;
4682 colno = split = winno / stacks;
4683 win_g.w = ((r_g.w - (stacks * 2 * border_width) +
4684 2 * border_width) / stacks);
4686 hrh = r_g.h / colno;
4687 extra = r_g.h - (colno * hrh);
4688 win_g.h = hrh - 2 * border_width;
4689 i = j = 0, s = stacks;
4692 /* Update window geometry. */
4693 TAILQ_FOREACH(win, &ws->winlist, entry) {
4697 if (FLOATING(win)) {
4698 update_floater(win);
4703 if (split && i == split) {
4704 colno = (winno - mwin) / stacks;
4705 if (s <= (winno - mwin) % stacks)
4708 hrh = r_g.h / colno;
4709 extra = r_g.h - (colno * hrh);
4712 win_g.x += win_g.w + 2 * border_width +
4715 win_g.w = (r_g.w - msize -
4716 (stacks * (2 * border_width + tile_gap))) / stacks;
4718 win_g.w += (r_g.w - msize -
4719 (stacks * (2 * border_width + tile_gap))) %
4723 win_g.x -= win_g.w + 2 * border_width +
4729 win_g.h = hrh - 2 * border_width - tile_gap;
4732 h_inc = win->sh.width_inc;
4733 h_base = win->sh.base_width;
4735 h_inc = win->sh.height_inc;
4736 h_base = win->sh.base_height;
4739 if (j == colno - 1) {
4740 win_g.h = hrh + extra;
4741 } else if (h_inc > 1 && h_inc < h_slice) {
4742 /* adjust for window's requested size increment */
4743 remain = (win_g.h - h_base) % h_inc;
4744 missing = h_inc - remain;
4746 if (missing <= extra || j == 0) {
4758 win_g.y += last_h + 2 * border_width + tile_gap;
4760 if (disable_border && !(bar_enabled && ws->bar_enabled) &&
4763 win_g.w += 2 * border_width;
4764 win_g.h += 2 * border_width;
4770 if (X(win) != win_g.y || Y(win) != win_g.x ||
4771 WIDTH(win) != win_g.h || HEIGHT(win) != win_g.w) {
4775 WIDTH(win) = win_g.h;
4776 HEIGHT(win) = win_g.w;
4779 if (X(win) != win_g.x || Y(win) != win_g.y ||
4780 WIDTH(win) != win_g.w || HEIGHT(win) != win_g.h) {
4784 WIDTH(win) = win_g.w;
4785 HEIGHT(win) = win_g.h;
4789 if (bordered != win->bordered) {
4791 win->bordered = bordered;
4804 /* Stack all windows from bottom up. */
4805 TAILQ_FOREACH_REVERSE(win, &ws->stack, ws_win_stack, stack_entry)
4807 update_win_stacking(win);
4809 /* Map all windows from top down. */
4810 TAILQ_FOREACH(win, &ws->stack, stack_entry)
4814 DNPRINTF(SWM_D_STACK, "stack_master: done\n");
4818 vertical_config(struct workspace *ws, int id)
4820 DNPRINTF(SWM_D_STACK, "vertical_config: id: %d, workspace: %d\n",
4824 case SWM_ARG_ID_STACKRESET:
4825 case SWM_ARG_ID_STACKINIT:
4826 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
4827 ws->l_state.vertical_mwin = 1;
4828 ws->l_state.vertical_stacks = 1;
4830 case SWM_ARG_ID_MASTERSHRINK:
4831 if (ws->l_state.vertical_msize > 1)
4832 ws->l_state.vertical_msize--;
4834 case SWM_ARG_ID_MASTERGROW:
4835 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
4836 ws->l_state.vertical_msize++;
4838 case SWM_ARG_ID_MASTERADD:
4839 ws->l_state.vertical_mwin++;
4841 case SWM_ARG_ID_MASTERDEL:
4842 if (ws->l_state.vertical_mwin > 0)
4843 ws->l_state.vertical_mwin--;
4845 case SWM_ARG_ID_STACKINC:
4846 ws->l_state.vertical_stacks++;
4848 case SWM_ARG_ID_STACKDEC:
4849 if (ws->l_state.vertical_stacks > 1)
4850 ws->l_state.vertical_stacks--;
4852 case SWM_ARG_ID_FLIPLAYOUT:
4853 ws->l_state.vertical_flip = !ws->l_state.vertical_flip;
4861 vertical_stack(struct workspace *ws, struct swm_geometry *g)
4863 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
4865 stack_master(ws, g, 0, ws->l_state.vertical_flip);
4869 horizontal_config(struct workspace *ws, int id)
4871 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
4874 case SWM_ARG_ID_STACKRESET:
4875 case SWM_ARG_ID_STACKINIT:
4876 ws->l_state.horizontal_mwin = 1;
4877 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
4878 ws->l_state.horizontal_stacks = 1;
4880 case SWM_ARG_ID_MASTERSHRINK:
4881 if (ws->l_state.horizontal_msize > 1)
4882 ws->l_state.horizontal_msize--;
4884 case SWM_ARG_ID_MASTERGROW:
4885 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
4886 ws->l_state.horizontal_msize++;
4888 case SWM_ARG_ID_MASTERADD:
4889 ws->l_state.horizontal_mwin++;
4891 case SWM_ARG_ID_MASTERDEL:
4892 if (ws->l_state.horizontal_mwin > 0)
4893 ws->l_state.horizontal_mwin--;
4895 case SWM_ARG_ID_STACKINC:
4896 ws->l_state.horizontal_stacks++;
4898 case SWM_ARG_ID_STACKDEC:
4899 if (ws->l_state.horizontal_stacks > 1)
4900 ws->l_state.horizontal_stacks--;
4902 case SWM_ARG_ID_FLIPLAYOUT:
4903 ws->l_state.horizontal_flip = !ws->l_state.horizontal_flip;
4911 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
4913 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
4915 stack_master(ws, g, 1, ws->l_state.horizontal_flip);
4918 /* fullscreen view */
4920 max_stack(struct workspace *ws, struct swm_geometry *g)
4922 struct swm_geometry gg = *g;
4923 struct ws_win *w, *win = NULL, *parent = NULL, *tmpw;
4926 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
4931 winno = count_win(ws, false);
4932 if (winno == 0 && count_win(ws, true) == 0)
4935 /* Figure out which top level window should be visible. */
4936 if (ws->focus_pending)
4937 win = ws->focus_pending;
4940 else if (ws->focus_prev)
4941 win = ws->focus_prev;
4943 win = TAILQ_FIRST(&ws->winlist);
4945 DNPRINTF(SWM_D_STACK, "max_stack: focus_pending: %#x, focus: %#x, "
4946 "focus_prev: %#x, first: %#x, win: %#x\n", WINID(ws->focus_pending),
4947 WINID(ws->focus), WINID(ws->focus_prev),
4948 WINID(TAILQ_FIRST(&ws->winlist)), win->id);
4950 /* Update window geometry. */
4951 TAILQ_FOREACH(w, &ws->winlist, entry) {
4960 /* Set maximized flag for all maxed windows. */
4961 if (!MAXIMIZED(w)) {
4962 /* Preserve floating geometry. */
4964 store_float_geom(w);
4966 ewmh_apply_flags(w, w->ewmh_flags | EWMH_F_MAXIMIZED);
4967 ewmh_update_wm_state(w);
4970 /* Only reconfigure if necessary. */
4971 if (X(w) != gg.x || Y(w) != gg.y || WIDTH(w) != gg.w ||
4972 HEIGHT(w) != gg.h) {
4974 if (bar_enabled && ws->bar_enabled){
4977 w->bordered = false;
4978 WIDTH(w) += 2 * border_width;
4979 HEIGHT(w) += 2 * border_width;
4986 /* If transient, stack parent and its children. */
4987 if (TRANS(win) && (parent = find_window(win->transient))) {
4988 raise_window(parent);
4990 TAILQ_FOREACH_SAFE(w, &ws->stack, stack_entry, tmpw)
4991 if (w->transient == parent->id)
4995 /* Make sure focus window is on top. */
4998 /* Stack any children of focus window. */
4999 TAILQ_FOREACH_SAFE(w, &ws->stack, stack_entry, tmpw)
5000 if (w->transient == win->id)
5003 /* Map all windows. */
5004 TAILQ_FOREACH(w, &ws->stack, stack_entry)
5010 send_to_rg(struct swm_region *r, union arg *args)
5012 int ridx = args->id, i, num_screens;
5013 struct swm_region *rr = NULL;
5016 num_screens = get_screen_count();
5017 /* do nothing if we don't have more than one screen */
5018 if (!(num_screens > 1 || outputs > 1))
5021 DNPRINTF(SWM_D_FOCUS, "send_to_rg: id: %d\n", ridx);
5023 rr = TAILQ_FIRST(&r->s->rl);
5024 for (i = 0; i < ridx && rr != NULL; ++i)
5025 rr = TAILQ_NEXT(rr, entry);
5036 region_under(struct swm_screen *s, int x, int y)
5038 struct swm_region *r = NULL;
5040 TAILQ_FOREACH(r, &s->rl, entry) {
5041 DNPRINTF(SWM_D_MISC, "region_under: ws: %d, region g: (%d,%d) "
5042 "%d x %d, coords: (%d,%d)\n", r->ws->idx, X(r), Y(r),
5043 WIDTH(r), HEIGHT(r), x, y);
5044 if (X(r) <= x && x < MAX_X(r))
5045 if (Y(r) <= y && y < MAX_Y(r))
5052 /* Transfer focused window to target workspace and focus. */
5054 send_to_ws(struct swm_region *r, union arg *args)
5056 int wsid = args->id;
5057 struct ws_win *win = NULL;
5059 if (r && r->ws && r->ws->focus)
5064 DNPRINTF(SWM_D_MOVE, "send_to_ws: win %#x, ws %d\n", win->id, wsid);
5066 if (wsid < 0 || wsid >= workspace_limit)
5069 if (win->ws->idx == wsid)
5072 win_to_ws(win, wsid, true);
5074 /* Set new focus on target ws. */
5075 if (focus_mode != SWM_FOCUS_FOLLOW) {
5076 win->ws->focus_prev = win->ws->focus;
5077 win->ws->focus = win;
5078 win->ws->focus_pending = NULL;
5080 if (win->ws->focus_prev)
5081 update_window_color(win->ws->focus_prev);
5084 DNPRINTF(SWM_D_STACK, "send_to_ws: focus_pending: %#x, focus: %#x, "
5085 "focus_prev: %#x, first: %#x, win: %#x\n",
5086 WINID(r->ws->focus_pending), WINID(r->ws->focus),
5087 WINID(r->ws->focus_prev), WINID(TAILQ_FIRST(&r->ws->winlist)),
5090 ewmh_apply_flags(win, win->ewmh_flags & ~EWMH_F_MAXIMIZED);
5091 ewmh_update_wm_state(win);
5093 /* Restack and set new focus on current ws. */
5095 load_float_geom(win);
5099 if (focus_mode != SWM_FOCUS_FOLLOW) {
5100 if (r->ws->focus != NULL) {
5101 focus_win(r->ws->focus);
5103 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT, r->id,
5113 win_to_ws(struct ws_win *win, int wsid, bool unfocus)
5115 struct ws_win *parent;
5116 struct workspace *ws, *nws, *pws;
5118 if (wsid < 0 || wsid >= workspace_limit)
5121 if (win->ws->idx == wsid)
5125 nws = &win->s->ws[wsid];
5127 DNPRINTF(SWM_D_MOVE, "win_to_ws: win %#x, ws %d -> %d\n", win->id,
5130 /* Cleanup focus on source ws. */
5131 if (focus_mode != SWM_FOCUS_FOLLOW &&
5132 (ws->focus == win || ws->focus_pending == win))
5133 ws->focus_pending = get_focus_prev(win);
5135 /* Move the parent if this is a transient window. */
5137 parent = find_window(win->transient);
5140 /* Set new focus in parent's ws if needed. */
5141 if (pws->focus == parent) {
5142 if (focus_mode != SWM_FOCUS_FOLLOW)
5143 pws->focus_pending =
5144 get_focus_prev(parent);
5146 unfocus_win(parent);
5148 if (focus_mode != SWM_FOCUS_FOLLOW) {
5149 pws->focus = pws->focus_pending;
5150 pws->focus_pending = NULL;
5154 /* Don't unmap parent if new ws is visible */
5156 unmap_window(parent);
5159 TAILQ_REMOVE(&ws->winlist, parent, entry);
5160 TAILQ_REMOVE(&ws->stack, parent, stack_entry);
5161 TAILQ_INSERT_TAIL(&nws->winlist, parent, entry);
5162 TAILQ_INSERT_TAIL(&nws->stack, parent, stack_entry);
5165 DNPRINTF(SWM_D_PROP, "win_to_ws: set property: "
5166 "_NET_WM_DESKTOP: %d\n", wsid);
5167 xcb_change_property(conn, XCB_PROP_MODE_REPLACE,
5168 parent->id, ewmh[_NET_WM_DESKTOP].atom,
5169 XCB_ATOM_CARDINAL, 32, 1, &wsid);
5176 if (ws->focus_prev == win)
5177 ws->focus_prev = NULL;
5179 if (focus_mode != SWM_FOCUS_FOLLOW && ws->focus_pending != NULL) {
5180 ws->focus = ws->focus_pending;
5181 ws->focus_pending = NULL;
5184 /* Don't unmap if new ws is visible */
5189 TAILQ_REMOVE(&ws->winlist, win, entry);
5190 TAILQ_REMOVE(&ws->stack, win, stack_entry);
5191 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
5192 TAILQ_INSERT_TAIL(&nws->stack, win, stack_entry);
5195 /* Update the window's workspace property: _NET_WM_DESKTOP */
5196 DNPRINTF(SWM_D_PROP, "win_to_ws: set property: "
5197 "_NET_WM_DESKTOP: %d\n", wsid);
5198 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
5199 ewmh[_NET_WM_DESKTOP].atom, XCB_ATOM_CARDINAL, 32, 1, &wsid);
5201 ewmh_update_client_list();
5203 DNPRINTF(SWM_D_MOVE, "win_to_ws: done.\n");
5207 pressbutton(struct swm_region *r, union arg *args)
5209 /* suppress unused warning since var is needed */
5212 xcb_test_fake_input(conn, XCB_BUTTON_PRESS, args->id,
5213 XCB_CURRENT_TIME, XCB_WINDOW_NONE, 0, 0, 0);
5214 xcb_test_fake_input(conn, XCB_BUTTON_RELEASE, args->id,
5215 XCB_CURRENT_TIME, XCB_WINDOW_NONE, 0, 0, 0);
5219 raise_toggle(struct swm_region *r, union arg *args)
5221 /* suppress unused warning since var is needed */
5224 if (r == NULL || r->ws == NULL)
5227 if (r->ws->focus && MAXIMIZED(r->ws->focus))
5230 r->ws->always_raise = !r->ws->always_raise;
5232 /* bring floaters back to top */
5233 if (!r->ws->always_raise)
5240 iconify(struct swm_region *r, union arg *args)
5243 /* suppress unused warning since var is needed */
5246 if ((w = r->ws->focus) == NULL)
5249 ewmh_apply_flags(w, w->ewmh_flags | EWMH_F_HIDDEN);
5250 ewmh_update_wm_state(w);
5258 get_win_name(xcb_window_t win)
5261 xcb_get_property_cookie_t c;
5262 xcb_get_property_reply_t *r;
5264 /* First try _NET_WM_NAME for UTF-8. */
5265 c = xcb_get_property(conn, 0, win, ewmh[_NET_WM_NAME].atom,
5266 XCB_GET_PROPERTY_TYPE_ANY, 0, UINT_MAX);
5267 r = xcb_get_property_reply(conn, c, NULL);
5268 if (r && r->type == XCB_NONE) {
5270 /* Use WM_NAME instead; no UTF-8. */
5271 c = xcb_get_property(conn, 0, win, XCB_ATOM_WM_NAME,
5272 XCB_GET_PROPERTY_TYPE_ANY, 0, UINT_MAX);
5273 r = xcb_get_property_reply(conn, c, NULL);
5276 if (r && r->type != XCB_NONE && r->length > 0)
5277 name = strndup(xcb_get_property_value(r),
5278 xcb_get_property_value_length(r));
5283 err(1, "get_win_name: failed to allocate memory.");
5291 uniconify(struct swm_region *r, union arg *args)
5298 DNPRINTF(SWM_D_MISC, "uniconify\n");
5300 if (r == NULL || r->ws == NULL)
5303 /* make sure we have anything to uniconify */
5304 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
5305 if (win->ws == NULL)
5306 continue; /* should never happen */
5312 DNPRINTF(SWM_D_MISC, "uniconify: count: %d\n", count);
5318 search_resp_action = SWM_SEARCH_UNICONIFY;
5320 spawn_select(r, args, "search", &searchpid);
5322 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
5325 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
5326 if (win->ws == NULL)
5327 continue; /* should never happen */
5331 name = get_win_name(win->id);
5332 fprintf(lfile, "%s.%u\n", name, win->id);
5340 name_workspace(struct swm_region *r, union arg *args)
5344 DNPRINTF(SWM_D_MISC, "name_workspace\n");
5350 search_resp_action = SWM_SEARCH_NAME_WORKSPACE;
5352 spawn_select(r, args, "name_workspace", &searchpid);
5354 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
5357 fprintf(lfile, "%s", "");
5362 search_workspace(struct swm_region *r, union arg *args)
5365 struct workspace *ws;
5368 DNPRINTF(SWM_D_MISC, "search_workspace\n");
5374 search_resp_action = SWM_SEARCH_SEARCH_WORKSPACE;
5376 spawn_select(r, args, "search", &searchpid);
5378 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
5381 for (i = 0; i < workspace_limit; i++) {
5385 fprintf(lfile, "%d%s%s\n", ws->idx + 1,
5386 (ws->name ? ":" : ""), (ws->name ? ws->name : ""));
5393 search_win_cleanup(void)
5395 struct search_window *sw = NULL;
5397 while ((sw = TAILQ_FIRST(&search_wl)) != NULL) {
5398 xcb_destroy_window(conn, sw->indicator);
5399 TAILQ_REMOVE(&search_wl, sw, entry);
5405 search_win(struct swm_region *r, union arg *args)
5407 struct ws_win *win = NULL;
5408 struct search_window *sw = NULL;
5411 xcb_screen_t *screen;
5412 int i, width, height;
5420 XRectangle l_ibox, l_lbox;
5422 DNPRINTF(SWM_D_MISC, "search_win\n");
5425 search_resp_action = SWM_SEARCH_SEARCH_WINDOW;
5427 spawn_select(r, args, "search", &searchpid);
5429 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
5432 if ((screen = get_screen(r->s->idx)) == NULL)
5433 errx(1, "ERROR: can't get screen %d.", r->s->idx);
5435 TAILQ_INIT(&search_wl);
5438 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
5442 sw = calloc(1, sizeof(struct search_window));
5444 warn("search_win: calloc");
5446 search_win_cleanup();
5452 snprintf(s, sizeof s, "%d", i);
5455 w = xcb_generate_id(conn);
5456 wa[0] = r->s->c[SWM_S_COLOR_FOCUS].pixel;
5457 wa[1] = r->s->c[SWM_S_COLOR_UNFOCUS].pixel;
5458 wa[2] = screen->default_colormap;
5460 if (bar_font_legacy) {
5461 XmbTextExtents(bar_fs, s, len, &l_ibox, &l_lbox);
5462 width = l_lbox.width + 4;
5463 height = bar_fs_extents->max_logical_extent.height + 4;
5465 XftTextExtentsUtf8(display, bar_font, (FcChar8 *)s, len,
5467 width = info.width + 4;
5468 height = bar_font->height + 4;
5471 xcb_create_window(conn, screen->root_depth, w, win->id, 0, 0,
5472 width, height, 1, XCB_WINDOW_CLASS_INPUT_OUTPUT,
5473 screen->root_visual, XCB_CW_BACK_PIXEL |
5474 XCB_CW_BORDER_PIXEL | XCB_CW_COLORMAP, wa);
5476 xcb_map_window(conn, w);
5479 TAILQ_INSERT_TAIL(&search_wl, sw, entry);
5481 if (bar_font_legacy) {
5482 l_gcv.graphics_exposures = 0;
5483 l_draw = XCreateGC(display, w, 0, &l_gcv);
5485 XSetForeground(display, l_draw,
5486 r->s->c[SWM_S_COLOR_BAR].pixel);
5488 DRAWSTRING(display, w, bar_fs, l_draw, 2,
5489 (bar_fs_extents->max_logical_extent.height -
5490 l_lbox.height) / 2 - l_lbox.y, s, len);
5492 XFreeGC(display, l_draw);
5495 draw = XftDrawCreate(display, w,
5496 DefaultVisual(display, r->s->idx),
5497 DefaultColormap(display, r->s->idx));
5499 XftDrawStringUtf8(draw, &search_font_color, bar_font, 2,
5500 (HEIGHT(r->bar) + bar_font->height) / 2 -
5501 bar_font->descent, (FcChar8 *)s, len);
5503 XftDrawDestroy(draw);
5506 DNPRINTF(SWM_D_MISC, "search_win: mapped win %#x\n", w);
5508 fprintf(lfile, "%d\n", i);
5518 search_resp_uniconify(const char *resp, size_t len)
5524 DNPRINTF(SWM_D_MISC, "search_resp_uniconify: resp: %s\n", resp);
5526 TAILQ_FOREACH(win, &search_r->ws->winlist, entry) {
5529 name = get_win_name(win->id);
5530 if (asprintf(&s, "%s.%u", name, win->id) == -1) {
5535 if (strncmp(s, resp, len) == 0) {
5536 /* XXX this should be a callback to generalize */
5537 ewmh_apply_flags(win, win->ewmh_flags & ~EWMH_F_HIDDEN);
5538 ewmh_update_wm_state(win);
5548 search_resp_name_workspace(const char *resp, size_t len)
5550 struct workspace *ws;
5552 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: resp: %s\n", resp);
5554 if (search_r->ws == NULL)
5559 free(search_r->ws->name);
5560 search_r->ws->name = NULL;
5564 ws->name = strdup(resp);
5565 if (ws->name == NULL) {
5566 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: "
5567 "strdup: %s", strerror(errno));
5571 ewmh_update_desktop_names();
5572 ewmh_get_desktop_names();
5577 ewmh_update_desktop_names(void)
5579 char *name_list = NULL, *p;
5580 int num_screens, i, j, len = 0, tot = 0;
5582 num_screens = get_screen_count();
5583 for (i = 0; i < num_screens; ++i) {
5584 for (j = 0; j < workspace_limit; ++j) {
5585 if (screens[i].ws[j].name != NULL)
5586 len += strlen(screens[i].ws[j].name);
5590 if((name_list = calloc(len, sizeof(char))) == NULL)
5591 err(1, "update_desktop_names: calloc: failed to "
5592 "allocate memory.");
5595 for (j = 0; j < workspace_limit; ++j) {
5596 if (screens[i].ws[j].name != NULL) {
5597 len = strlen(screens[i].ws[j].name);
5598 memcpy(p, screens[i].ws[j].name, len);
5607 xcb_change_property(conn, XCB_PROP_MODE_REPLACE,
5608 screens[i].root, ewmh[_NET_DESKTOP_NAMES].atom,
5609 a_utf8_string, 8, tot, name_list);
5619 ewmh_get_desktop_names(void)
5622 xcb_get_property_cookie_t c;
5623 xcb_get_property_reply_t *r;
5624 int num_screens, i, j, n, k;
5626 num_screens = get_screen_count();
5627 for (i = 0; i < num_screens; ++i) {
5628 for (j = 0; j < workspace_limit; ++j) {
5629 free(screens[i].ws[j].name);
5630 screens[i].ws[j].name = NULL;
5633 c = xcb_get_property(conn, 0, screens[i].root,
5634 ewmh[_NET_DESKTOP_NAMES].atom,
5635 a_utf8_string, 0, UINT32_MAX);
5636 r = xcb_get_property_reply(conn, c, NULL);
5640 names = xcb_get_property_value(r);
5641 n = xcb_get_property_value_length(r);
5643 for (j = 0, k = 0; j < n; ++j) {
5644 if (*(names + j) != '\0') {
5645 screens[i].ws[k].name = strdup(names + j);
5646 j += strlen(names + j);
5655 ewmh_update_client_list(void)
5658 int num_screens, i, j, k = 0, count = 0;
5661 num_screens = get_screen_count();
5662 for (i = 0; i < num_screens; ++i) {
5663 for (j = 0; j < workspace_limit; ++j)
5664 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
5667 DNPRINTF(SWM_D_PROP, "ewmh_update_client_list: win count: %d\n",
5673 wins = calloc(count, sizeof(xcb_window_t));
5675 err(1, "ewmh_update_client_list: calloc: failed to "
5676 "allocate memory.");
5678 for (j = 0, k = 0; j < workspace_limit; ++j)
5679 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
5680 wins[k++] = win->id;
5682 xcb_change_property(conn, XCB_PROP_MODE_REPLACE,
5683 screens[i].root, ewmh[_NET_CLIENT_LIST].atom,
5684 XCB_ATOM_WINDOW, 32, count, wins);
5691 ewmh_update_current_desktop(void)
5695 num_screens = get_screen_count();
5696 for (i = 0; i < num_screens; ++i)
5697 xcb_change_property(conn, XCB_PROP_MODE_REPLACE,
5698 screens[i].root, ewmh[_NET_CURRENT_DESKTOP].atom,
5699 XCB_ATOM_CARDINAL, 32, 1, &screens[i].r_focus->ws->idx);
5703 ewmh_update_desktops(void)
5705 int num_screens, i, j;
5708 vals = calloc(workspace_limit * 2, sizeof(uint32_t));
5710 err(1, "ewmh_update_desktops: calloc: failed to allocate "
5713 num_screens = get_screen_count();
5714 for (i = 0; i < num_screens; i++) {
5715 xcb_change_property(conn, XCB_PROP_MODE_REPLACE,
5716 screens[i].root, ewmh[_NET_NUMBER_OF_DESKTOPS].atom,
5717 XCB_ATOM_CARDINAL, 32, 1, &workspace_limit);
5719 for (j = 0; j < workspace_limit; ++j) {
5720 if (screens[i].ws[j].r != NULL) {
5721 vals[j * 2] = X(screens[i].ws[j].r);
5722 vals[j * 2 + 1] = Y(screens[i].ws[j].r);
5723 } else if (screens[i].ws[j].old_r != NULL) {
5724 vals[j * 2] = X(screens[i].ws[j].old_r);
5725 vals[j * 2 + 1] = Y(screens[i].ws[j].old_r);
5727 vals[j * 2] = vals[j * 2 + 1] = 0;
5731 xcb_change_property(conn, XCB_PROP_MODE_REPLACE,
5732 screens[i].root, ewmh[_NET_DESKTOP_VIEWPORT].atom,
5733 XCB_ATOM_CARDINAL, 32, workspace_limit * 2, vals);
5740 search_resp_search_workspace(const char *resp)
5747 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: resp: %s\n", resp);
5751 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: strdup: %s",
5758 ws_idx = (int)strtonum(q, 1, workspace_limit, &errstr);
5760 DNPRINTF(SWM_D_MISC, "workspace idx is %s: %s",
5767 switchws(search_r, &a);
5771 search_resp_search_window(const char *resp)
5776 struct search_window *sw;
5778 DNPRINTF(SWM_D_MISC, "search_resp_search_window: resp: %s\n", resp);
5782 DNPRINTF(SWM_D_MISC, "search_resp_search_window: strdup: %s",
5787 idx = (int)strtonum(s, 1, INT_MAX, &errstr);
5789 DNPRINTF(SWM_D_MISC, "window idx is %s: %s",
5796 TAILQ_FOREACH(sw, &search_wl, entry)
5797 if (idx == sw->idx) {
5803 #define MAX_RESP_LEN 1024
5806 search_do_resp(void)
5812 DNPRINTF(SWM_D_MISC, "search_do_resp:\n");
5817 if ((resp = calloc(1, MAX_RESP_LEN + 1)) == NULL) {
5818 warn("search: calloc");
5822 rbytes = read(select_resp_pipe[0], resp, MAX_RESP_LEN);
5824 warn("search: read error");
5827 resp[rbytes] = '\0';
5830 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
5831 * newline, so work around that by sanitizing the resp now.
5833 resp[strcspn(resp, "\n")] = '\0';
5836 switch (search_resp_action) {
5837 case SWM_SEARCH_UNICONIFY:
5838 search_resp_uniconify(resp, len);
5840 case SWM_SEARCH_NAME_WORKSPACE:
5841 search_resp_name_workspace(resp, len);
5843 case SWM_SEARCH_SEARCH_WORKSPACE:
5844 search_resp_search_workspace(resp);
5846 case SWM_SEARCH_SEARCH_WINDOW:
5847 search_resp_search_window(resp);
5852 if (search_resp_action == SWM_SEARCH_SEARCH_WINDOW)
5853 search_win_cleanup();
5855 search_resp_action = SWM_SEARCH_NONE;
5856 close(select_resp_pipe[0]);
5863 wkill(struct swm_region *r, union arg *args)
5865 DNPRINTF(SWM_D_MISC, "wkill: win %#x, id: %d\n", WINID(r->ws->focus),
5868 if (r->ws->focus == NULL)
5871 if (args->id == SWM_ARG_ID_KILLWINDOW)
5872 xcb_kill_client(conn, r->ws->focus->id);
5874 if (r->ws->focus->can_delete)
5875 client_msg(r->ws->focus, a_delete, 0);
5881 clear_maximized(struct workspace *ws)
5886 /* Clear any maximized win(s) on ws, from bottom up. */
5887 TAILQ_FOREACH_REVERSE(w, &ws->stack, ws_win_stack, stack_entry)
5889 ewmh_apply_flags(w, w->ewmh_flags & ~EWMH_F_MAXIMIZED);
5890 ewmh_update_wm_state(w);
5898 maximize_toggle(struct swm_region *r, union arg *args)
5900 struct ws_win *w = r->ws->focus;
5902 /* suppress unused warning since var is needed */
5908 DNPRINTF(SWM_D_MISC, "maximize_toggle: win %#x\n", w->id);
5913 if (w->ws->cur_layout == &layouts[SWM_MAX_STACK])
5916 ewmh_apply_flags(w, w->ewmh_flags ^ EWMH_F_MAXIMIZED);
5917 ewmh_update_wm_state(w);
5921 if (w == w->ws->focus)
5925 DNPRINTF(SWM_D_MISC, "maximize_toggle: done\n");
5929 floating_toggle(struct swm_region *r, union arg *args)
5931 struct ws_win *w = r->ws->focus;
5933 /* suppress unused warning since var is needed */
5939 DNPRINTF(SWM_D_MISC, "floating_toggle: win %#x\n", w->id);
5941 if (FULLSCREEN(w) || TRANS(w))
5944 if (w->ws->cur_layout == &layouts[SWM_MAX_STACK])
5947 ewmh_apply_flags(w, w->ewmh_flags ^ EWMH_F_ABOVE);
5948 ewmh_update_wm_state(w);
5952 if (w == w->ws->focus)
5956 DNPRINTF(SWM_D_MISC, "floating_toggle: done\n");
5960 region_containment(struct ws_win *win, struct swm_region *r, int opts)
5962 struct swm_geometry g = r->g;
5963 int rt, lt, tp, bm, bw;
5965 bw = (opts & SWM_CW_SOFTBOUNDARY) ? boundary_width : 0;
5968 * Perpendicular distance of each side of the window to the respective
5969 * side of the region boundary. Positive values indicate the side of
5970 * the window has passed beyond the region boundary.
5972 rt = opts & SWM_CW_RIGHT ? MAX_X(win) + BORDER(win) - MAX_X(r) : bw;
5973 lt = opts & SWM_CW_LEFT ? X(r) - X(win) + BORDER(win) : bw;
5974 bm = opts & SWM_CW_BOTTOM ? MAX_Y(win) + BORDER(win) - MAX_Y(r) : bw;
5975 tp = opts & SWM_CW_TOP ? Y(r) - Y(win) + BORDER(win) : bw;
5977 DNPRINTF(SWM_D_MISC, "region_containment: win %#x, rt: %d, lt: %d, "
5978 "bm: %d, tp: %d, SOFTBOUNDARY: %s, HARDBOUNDARY: %s\n", win->id, rt,
5979 lt, bm, tp, YESNO(opts & SWM_CW_SOFTBOUNDARY),
5980 YESNO(opts & SWM_CW_HARDBOUNDARY));
5983 * Disable containment if any of the flagged sides went beyond the
5984 * containment boundary, or if containment is disabled.
5986 if (!(opts & SWM_CW_HARDBOUNDARY || opts & SWM_CW_SOFTBOUNDARY) ||
5987 (bw != 0 && ((rt > bw) || (lt > bw) || (bm > bw) || (tp > bw)))) {
5988 /* Make sure window has at least 1 pixel in the region */
5989 g.x += 1 - WIDTH(win);
5990 g.y += 1 - HEIGHT(win);
5991 g.w += 2 * WIDTH(win) - 2;
5992 g.h += 2 * HEIGHT(win) - 2;
5995 constrain_window(win, &g, &opts);
5998 /* Move or resize a window so that flagged side(s) fit into the supplied box. */
6000 constrain_window(struct ws_win *win, struct swm_geometry *b, int *opts)
6002 DNPRINTF(SWM_D_MISC, "constrain_window: win %#x, (x,y) w x h: "
6003 "(%d,%d) %d x %d, box: (x,y) w x h: (%d,%d) %d x %d, rt: %s, "
6004 "lt: %s, bt: %s, tp: %s, allow resize: %s\n", win->id, X(win),
6005 Y(win), WIDTH(win), HEIGHT(win), b->x, b->y, b->w, b->h,
6006 YESNO(*opts & SWM_CW_RIGHT), YESNO(*opts & SWM_CW_LEFT),
6007 YESNO(*opts & SWM_CW_BOTTOM), YESNO(*opts & SWM_CW_TOP),
6008 YESNO(*opts & SWM_CW_RESIZABLE));
6010 if ((*opts & SWM_CW_RIGHT) && MAX_X(win) + BORDER(win) > b->x + b->w) {
6011 if (*opts & SWM_CW_RESIZABLE)
6012 WIDTH(win) = b->x + b->w - X(win) - BORDER(win);
6014 X(win) = b->x + b->w - WIDTH(win) - BORDER(win);
6017 if ((*opts & SWM_CW_LEFT) && X(win) + BORDER(win) < b->x) {
6018 if (*opts & SWM_CW_RESIZABLE)
6019 WIDTH(win) -= b->x - X(win) - BORDER(win);
6021 X(win) = b->x - BORDER(win);
6024 if ((*opts & SWM_CW_BOTTOM) && MAX_Y(win) + BORDER(win) > b->y + b->h) {
6025 if (*opts & SWM_CW_RESIZABLE)
6026 HEIGHT(win) = b->y + b->h - Y(win) - BORDER(win);
6028 Y(win) = b->y + b->h - HEIGHT(win) - BORDER(win);
6031 if ((*opts & SWM_CW_TOP) && Y(win) + BORDER(win) < b->y) {
6032 if (*opts & SWM_CW_RESIZABLE)
6033 HEIGHT(win) -= b->y - Y(win) - BORDER(win);
6035 Y(win) = b->y - BORDER(win);
6038 if (*opts & SWM_CW_RESIZABLE) {
6041 if (HEIGHT(win) < 1)
6047 update_window_color(struct ws_win *win)
6051 if (WS_FOCUSED(win->ws) && win->ws->focus == win)
6052 pixel = MAXIMIZED(win) ?
6053 &win->s->c[SWM_S_COLOR_FOCUS_MAXIMIZED].pixel :
6054 &win->s->c[SWM_S_COLOR_FOCUS].pixel;
6056 pixel = MAXIMIZED(win) ?
6057 &win->s->c[SWM_S_COLOR_UNFOCUS_MAXIMIZED].pixel :
6058 &win->s->c[SWM_S_COLOR_UNFOCUS].pixel;
6060 xcb_change_window_attributes(conn, win->id,
6061 XCB_CW_BORDER_PIXEL, pixel);
6065 update_window(struct ws_win *win)
6070 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
6071 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
6072 XCB_CONFIG_WINDOW_BORDER_WIDTH;
6076 wc[3] = HEIGHT(win);
6077 wc[4] = BORDER(win);
6079 DNPRINTF(SWM_D_EVENT, "update_window: win %#x, (x,y) w x h: "
6080 "(%d,%d) %d x %d, bordered: %s\n", win->id, wc[0], wc[1], wc[2],
6081 wc[3], YESNO(win->bordered));
6083 xcb_configure_window(conn, win->id, mask, wc);
6086 #define SWM_RESIZE_STEPS (50)
6089 resize(struct ws_win *win, union arg *args)
6091 xcb_timestamp_t timestamp = 0;
6092 struct swm_region *r = NULL;
6093 struct swm_geometry g;
6095 int top = 0, left = 0;
6097 xcb_cursor_t cursor;
6098 xcb_query_pointer_reply_t *xpr = NULL;
6099 xcb_generic_event_t *evt;
6100 xcb_motion_notify_event_t *mne;
6107 if (FULLSCREEN(win))
6110 /* In max_stack mode, should only resize transients. */
6111 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !TRANS(win))
6114 DNPRINTF(SWM_D_EVENT, "resize: win %#x, floating: %s, "
6115 "transient: %#x\n", win->id, YESNO(ABOVE(win)),
6119 store_float_geom(win);
6120 else if (!(TRANS(win) || ABOVE(win)))
6123 ewmh_apply_flags(win, (win->ewmh_flags | SWM_F_MANUAL | EWMH_F_ABOVE) &
6125 ewmh_update_wm_state(win);
6131 /* It's possible for win to have been freed during focus_flush(). */
6132 if (validate_win(win)) {
6133 DNPRINTF(SWM_D_EVENT, "resize: invalid win.\n");
6138 case SWM_ARG_ID_WIDTHSHRINK:
6139 WIDTH(win) -= SWM_RESIZE_STEPS;
6142 case SWM_ARG_ID_WIDTHGROW:
6143 WIDTH(win) += SWM_RESIZE_STEPS;
6146 case SWM_ARG_ID_HEIGHTSHRINK:
6147 HEIGHT(win) -= SWM_RESIZE_STEPS;
6150 case SWM_ARG_ID_HEIGHTGROW:
6151 HEIGHT(win) += SWM_RESIZE_STEPS;
6158 region_containment(win, r, SWM_CW_ALLSIDES | SWM_CW_RESIZABLE |
6159 SWM_CW_HARDBOUNDARY);
6161 store_float_geom(win);
6165 region_containment(win, r, SWM_CW_ALLSIDES | SWM_CW_RESIZABLE |
6166 SWM_CW_SOFTBOUNDARY);
6169 /* get cursor offset from window root */
6170 xpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, win->id),
6177 if (xpr->win_x < WIDTH(win) / 2)
6180 if (xpr->win_y < HEIGHT(win) / 2)
6183 if (args->id == SWM_ARG_ID_CENTER)
6184 cursor = cursors[XC_SIZING].cid;
6186 cursor = cursors[left ? XC_TOP_LEFT_CORNER :
6187 XC_TOP_RIGHT_CORNER].cid;
6189 cursor = cursors[left ? XC_BOTTOM_LEFT_CORNER :
6190 XC_BOTTOM_RIGHT_CORNER].cid;
6192 xcb_grab_pointer(conn, 0, win->id, MOUSEMASK,
6193 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, XCB_WINDOW_NONE, cursor,
6198 while (resizing && (evt = xcb_wait_for_event(conn))) {
6199 switch (XCB_EVENT_RESPONSE_TYPE(evt)) {
6200 case XCB_BUTTON_RELEASE:
6201 DNPRINTF(SWM_D_EVENT, "resize: BUTTON_RELEASE\n");
6204 case XCB_MOTION_NOTIFY:
6205 mne = (xcb_motion_notify_event_t *)evt;
6206 /* cursor offset/delta from start of the operation */
6207 dx = mne->root_x - xpr->root_x;
6208 dy = mne->root_y - xpr->root_y;
6214 if (args->id == SWM_ARG_ID_CENTER) {
6215 if (g.h / 2 + dy < 1)
6219 HEIGHT(win) = g.h + 2 * dy;
6227 HEIGHT(win) = g.h + dy;
6234 if (args->id == SWM_ARG_ID_CENTER) {
6235 if (g.w / 2 + dx < 1)
6239 WIDTH(win) = g.w + 2 * dx;
6247 WIDTH(win) = g.w + dx;
6250 /* not free, don't sync more than 120 times / second */
6251 if ((mne->time - timestamp) > (1000 / 120) ) {
6252 timestamp = mne->time;
6253 regionize(win, mne->root_x, mne->root_y);
6254 region_containment(win, r, SWM_CW_ALLSIDES |
6255 SWM_CW_RESIZABLE | SWM_CW_HARDBOUNDARY |
6256 SWM_CW_SOFTBOUNDARY);
6263 xcb_allow_events(conn, XCB_ALLOW_ASYNC_KEYBOARD,
6264 ((xcb_key_press_event_t *)evt)->time);
6270 /* It's possible for win to have been freed above. */
6271 if (validate_win(win)) {
6272 DNPRINTF(SWM_D_EVENT, "resize: invalid win.\n");
6280 region_containment(win, r, SWM_CW_ALLSIDES | SWM_CW_RESIZABLE |
6281 SWM_CW_HARDBOUNDARY | SWM_CW_SOFTBOUNDARY);
6285 store_float_geom(win);
6287 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
6289 DNPRINTF(SWM_D_EVENT, "resize: done.\n");
6293 resize_step(struct swm_region *r, union arg *args)
6295 struct ws_win *win = NULL;
6297 if (r && r->ws && r->ws->focus)
6306 /* Try to set window region based on supplied coordinates or window center. */
6308 regionize(struct ws_win *win, int x, int y)
6310 struct swm_region *r = NULL;
6312 r = region_under(win->s, x, y);
6314 r = region_under(win->s, X(win) + WIDTH(win) / 2,
6315 Y(win) + HEIGHT(win) / 2);
6317 if (r != NULL && r != win->ws->r) {
6318 if (clear_maximized(r->ws) > 0)
6321 win_to_ws(win, r->ws->idx, false);
6323 /* Set focus on new ws. */
6324 unfocus_win(r->ws->focus);
6332 #define SWM_MOVE_STEPS (50)
6335 move(struct ws_win *win, union arg *args)
6337 struct swm_region *r;
6338 xcb_timestamp_t timestamp = 0;
6340 xcb_query_pointer_reply_t *qpr = NULL;
6341 xcb_generic_event_t *evt;
6342 xcb_motion_notify_event_t *mne;
6343 bool moving, restack = false;
6348 if ((r = win->ws->r) == NULL)
6351 if (FULLSCREEN(win))
6354 DNPRINTF(SWM_D_EVENT, "move: win %#x, floating: %s, transient: "
6355 "%#x\n", win->id, YESNO(ABOVE(win)), win->transient);
6357 /* in max_stack mode should only move transients */
6358 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !TRANS(win))
6361 if (!(ABOVE(win) || TRANS(win)) || MAXIMIZED(win)) {
6362 store_float_geom(win);
6366 ewmh_apply_flags(win, (win->ewmh_flags | SWM_F_MANUAL | EWMH_F_ABOVE) &
6368 ewmh_update_wm_state(win);
6375 /* It's possible for win to have been freed during focus_flush(). */
6376 if (validate_win(win)) {
6377 DNPRINTF(SWM_D_EVENT, "move: invalid win.\n");
6383 case SWM_ARG_ID_MOVELEFT:
6384 X(win) -= (SWM_MOVE_STEPS - border_width);
6387 case SWM_ARG_ID_MOVERIGHT:
6388 X(win) += (SWM_MOVE_STEPS - border_width);
6391 case SWM_ARG_ID_MOVEUP:
6392 Y(win) -= (SWM_MOVE_STEPS - border_width);
6395 case SWM_ARG_ID_MOVEDOWN:
6396 Y(win) += (SWM_MOVE_STEPS - border_width);
6403 regionize(win, -1, -1);
6404 region_containment(win, win->ws->r, SWM_CW_ALLSIDES);
6406 store_float_geom(win);
6410 xcb_grab_pointer(conn, 0, win->id, MOUSEMASK,
6411 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC,
6412 XCB_WINDOW_NONE, cursors[XC_FLEUR].cid, XCB_CURRENT_TIME);
6414 /* get cursor offset from window root */
6415 qpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, win->id),
6418 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
6422 regionize(win, qpr->root_x, qpr->root_y);
6423 region_containment(win, win->ws->r, SWM_CW_ALLSIDES |
6424 SWM_CW_SOFTBOUNDARY);
6428 while (moving && (evt = xcb_wait_for_event(conn))) {
6429 switch (XCB_EVENT_RESPONSE_TYPE(evt)) {
6430 case XCB_BUTTON_RELEASE:
6431 DNPRINTF(SWM_D_EVENT, "move: BUTTON_RELEASE\n");
6434 case XCB_MOTION_NOTIFY:
6435 mne = (xcb_motion_notify_event_t *)evt;
6436 DNPRINTF(SWM_D_EVENT, "motion: root: %#x\n", mne->root);
6437 X(win) = mne->root_x - qpr->win_x - border_width;
6438 Y(win) = mne->root_y - qpr->win_y - border_width;
6440 /* not free, don't sync more than 120 times / second */
6441 if ((mne->time - timestamp) > (1000 / 120) ) {
6442 timestamp = mne->time;
6443 regionize(win, mne->root_x, mne->root_y);
6444 region_containment(win, win->ws->r,
6445 SWM_CW_ALLSIDES | SWM_CW_SOFTBOUNDARY);
6452 xcb_allow_events(conn, XCB_ALLOW_ASYNC_KEYBOARD,
6453 ((xcb_key_press_event_t *)evt)->time);
6459 /* It's possible for win to have been freed above. */
6460 if (validate_win(win)) {
6461 DNPRINTF(SWM_D_EVENT, "move: invalid win.\n");
6469 region_containment(win, win->ws->r, SWM_CW_ALLSIDES |
6470 SWM_CW_SOFTBOUNDARY);
6474 store_float_geom(win);
6476 /* New region set to fullscreen layout. */
6477 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK]) {
6484 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
6485 DNPRINTF(SWM_D_EVENT, "move: done.\n");
6489 move_step(struct swm_region *r, union arg *args)
6491 struct ws_win *win = NULL;
6493 if (r && r->ws && r->ws->focus)
6498 if (!TRANS(win) && !ABOVE(win))
6505 /* key definitions */
6507 char name[SWM_FUNCNAME_LEN];
6508 void (*func)(struct swm_region *r, union arg *);
6510 } keyfuncs[KF_INVALID + 1] = {
6511 /* name function argument */
6512 { "bar_toggle", bar_toggle, {.id = SWM_ARG_ID_BAR_TOGGLE} },
6513 { "bar_toggle_ws", bar_toggle, {.id = SWM_ARG_ID_BAR_TOGGLE_WS} },
6514 { "button2", pressbutton, {2} },
6515 { "cycle_layout", cycle_layout, {0} },
6516 { "flip_layout", stack_config, {.id = SWM_ARG_ID_FLIPLAYOUT} },
6517 { "float_toggle", floating_toggle,{0} },
6518 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
6519 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
6520 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
6521 { "focus_urgent", focus, {.id = SWM_ARG_ID_FOCUSURGENT} },
6522 { "maximize_toggle", maximize_toggle,{0} },
6523 { "height_grow", resize_step, {.id = SWM_ARG_ID_HEIGHTGROW} },
6524 { "height_shrink", resize_step, {.id = SWM_ARG_ID_HEIGHTSHRINK} },
6525 { "iconify", iconify, {0} },
6526 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
6527 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
6528 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
6529 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
6530 { "move_down", move_step, {.id = SWM_ARG_ID_MOVEDOWN} },
6531 { "move_left", move_step, {.id = SWM_ARG_ID_MOVELEFT} },
6532 { "move_right", move_step, {.id = SWM_ARG_ID_MOVERIGHT} },
6533 { "move_up", move_step, {.id = SWM_ARG_ID_MOVEUP} },
6534 { "mvrg_1", send_to_rg, {.id = 0} },
6535 { "mvrg_2", send_to_rg, {.id = 1} },
6536 { "mvrg_3", send_to_rg, {.id = 2} },
6537 { "mvrg_4", send_to_rg, {.id = 3} },
6538 { "mvrg_5", send_to_rg, {.id = 4} },
6539 { "mvrg_6", send_to_rg, {.id = 5} },
6540 { "mvrg_7", send_to_rg, {.id = 6} },
6541 { "mvrg_8", send_to_rg, {.id = 7} },
6542 { "mvrg_9", send_to_rg, {.id = 8} },
6543 { "mvws_1", send_to_ws, {.id = 0} },
6544 { "mvws_2", send_to_ws, {.id = 1} },
6545 { "mvws_3", send_to_ws, {.id = 2} },
6546 { "mvws_4", send_to_ws, {.id = 3} },
6547 { "mvws_5", send_to_ws, {.id = 4} },
6548 { "mvws_6", send_to_ws, {.id = 5} },
6549 { "mvws_7", send_to_ws, {.id = 6} },
6550 { "mvws_8", send_to_ws, {.id = 7} },
6551 { "mvws_9", send_to_ws, {.id = 8} },
6552 { "mvws_10", send_to_ws, {.id = 9} },
6553 { "mvws_11", send_to_ws, {.id = 10} },
6554 { "mvws_12", send_to_ws, {.id = 11} },
6555 { "mvws_13", send_to_ws, {.id = 12} },
6556 { "mvws_14", send_to_ws, {.id = 13} },
6557 { "mvws_15", send_to_ws, {.id = 14} },
6558 { "mvws_16", send_to_ws, {.id = 15} },
6559 { "mvws_17", send_to_ws, {.id = 16} },
6560 { "mvws_18", send_to_ws, {.id = 17} },
6561 { "mvws_19", send_to_ws, {.id = 18} },
6562 { "mvws_20", send_to_ws, {.id = 19} },
6563 { "mvws_21", send_to_ws, {.id = 20} },
6564 { "mvws_22", send_to_ws, {.id = 21} },
6565 { "name_workspace", name_workspace, {0} },
6566 { "quit", quit, {0} },
6567 { "raise_toggle", raise_toggle, {0} },
6568 { "restart", restart, {0} },
6569 { "rg_1", focusrg, {.id = 0} },
6570 { "rg_2", focusrg, {.id = 1} },
6571 { "rg_3", focusrg, {.id = 2} },
6572 { "rg_4", focusrg, {.id = 3} },
6573 { "rg_5", focusrg, {.id = 4} },
6574 { "rg_6", focusrg, {.id = 5} },
6575 { "rg_7", focusrg, {.id = 6} },
6576 { "rg_8", focusrg, {.id = 7} },
6577 { "rg_9", focusrg, {.id = 8} },
6578 { "rg_next", cyclerg, {.id = SWM_ARG_ID_CYCLERG_UP} },
6579 { "rg_prev", cyclerg, {.id = SWM_ARG_ID_CYCLERG_DOWN} },
6580 { "screen_next", cyclerg, {.id = SWM_ARG_ID_CYCLERG_UP} },
6581 { "screen_prev", cyclerg, {.id = SWM_ARG_ID_CYCLERG_DOWN} },
6582 { "search_win", search_win, {0} },
6583 { "search_workspace", search_workspace, {0} },
6584 { "spawn_custom", NULL, {0} },
6585 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
6586 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
6587 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
6588 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
6589 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
6590 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
6591 { "uniconify", uniconify, {0} },
6592 { "version", version, {0} },
6593 { "width_grow", resize_step, {.id = SWM_ARG_ID_WIDTHGROW} },
6594 { "width_shrink", resize_step, {.id = SWM_ARG_ID_WIDTHSHRINK} },
6595 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
6596 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
6597 { "ws_1", switchws, {.id = 0} },
6598 { "ws_2", switchws, {.id = 1} },
6599 { "ws_3", switchws, {.id = 2} },
6600 { "ws_4", switchws, {.id = 3} },
6601 { "ws_5", switchws, {.id = 4} },
6602 { "ws_6", switchws, {.id = 5} },
6603 { "ws_7", switchws, {.id = 6} },
6604 { "ws_8", switchws, {.id = 7} },
6605 { "ws_9", switchws, {.id = 8} },
6606 { "ws_10", switchws, {.id = 9} },
6607 { "ws_11", switchws, {.id = 10} },
6608 { "ws_12", switchws, {.id = 11} },
6609 { "ws_13", switchws, {.id = 12} },
6610 { "ws_14", switchws, {.id = 13} },
6611 { "ws_15", switchws, {.id = 14} },
6612 { "ws_16", switchws, {.id = 15} },
6613 { "ws_17", switchws, {.id = 16} },
6614 { "ws_18", switchws, {.id = 17} },
6615 { "ws_19", switchws, {.id = 18} },
6616 { "ws_20", switchws, {.id = 19} },
6617 { "ws_21", switchws, {.id = 20} },
6618 { "ws_22", switchws, {.id = 21} },
6619 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
6620 { "ws_next_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP_ALL} },
6621 { "ws_next_move", cyclews, {.id = SWM_ARG_ID_CYCLEWS_MOVE_UP} },
6622 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
6623 { "ws_prev_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN_ALL} },
6624 { "ws_prev_move", cyclews, {.id = SWM_ARG_ID_CYCLEWS_MOVE_DOWN} },
6625 { "ws_prior", priorws, {0} },
6626 { "dumpwins", dumpwins, {0} }, /* MUST BE LAST */
6627 { "invalid key func", NULL, {0} },
6631 key_cmp(struct key *kp1, struct key *kp2)
6633 if (kp1->keysym < kp2->keysym)
6635 if (kp1->keysym > kp2->keysym)
6638 if (kp1->mod < kp2->mod)
6640 if (kp1->mod > kp2->mod)
6647 enum { client_click, root_click };
6649 unsigned int action;
6651 unsigned int button;
6652 void (*func)(struct ws_win *, union arg *);
6655 #define MODKEY_SHIFT MODKEY | XCB_MOD_MASK_SHIFT
6656 /* action key mouse button func args */
6657 { client_click, MODKEY, XCB_BUTTON_INDEX_3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
6658 { client_click, MODKEY_SHIFT, XCB_BUTTON_INDEX_3, resize, {.id = SWM_ARG_ID_CENTER} },
6659 { client_click, MODKEY, XCB_BUTTON_INDEX_1, move, {0} },
6664 update_modkey(unsigned int mod)
6670 RB_FOREACH(kp, key_tree, &keys)
6671 if (kp->mod & XCB_MOD_MASK_SHIFT)
6672 kp->mod = mod | XCB_MOD_MASK_SHIFT;
6676 for (i = 0; i < LENGTH(buttons); i++)
6677 if (buttons[i].mask & XCB_MOD_MASK_SHIFT)
6678 buttons[i].mask = mod | XCB_MOD_MASK_SHIFT;
6680 buttons[i].mask = mod;
6684 spawn_expand(struct swm_region *r, union arg *args, const char *spawn_name,
6687 struct spawn_prog *prog = NULL;
6689 char *ap, **real_args;
6691 /* suppress unused warning since var is needed */
6694 DNPRINTF(SWM_D_SPAWN, "spawn_expand: %s\n", spawn_name);
6697 TAILQ_FOREACH(prog, &spawns, entry) {
6698 if (strcasecmp(spawn_name, prog->name) == 0)
6702 warnx("spawn_custom: program %s not found", spawn_name);
6706 /* make room for expanded args */
6707 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
6708 err(1, "spawn_custom: calloc real_args");
6710 /* expand spawn_args into real_args */
6711 for (i = c = 0; i < prog->argc; i++) {
6713 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg: %s\n", ap);
6714 if (strcasecmp(ap, "$bar_border") == 0) {
6716 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
6718 err(1, "spawn_custom border color");
6719 } else if (strcasecmp(ap, "$bar_color") == 0) {
6721 strdup(r->s->c[SWM_S_COLOR_BAR].name))
6723 err(1, "spawn_custom bar color");
6724 } else if (strcasecmp(ap, "$bar_font") == 0) {
6725 if ((real_args[c] = strdup(bar_fonts))
6727 err(1, "spawn_custom bar fonts");
6728 } else if (strcasecmp(ap, "$bar_font_color") == 0) {
6730 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
6732 err(1, "spawn_custom color font");
6733 } else if (strcasecmp(ap, "$color_focus") == 0) {
6735 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
6737 err(1, "spawn_custom color focus");
6738 } else if (strcasecmp(ap, "$color_focus_maximized") == 0) {
6740 strdup(r->s->c[SWM_S_COLOR_FOCUS_MAXIMIZED].name))
6742 err(1, "spawn_custom color focus maximized");
6743 } else if (strcasecmp(ap, "$color_unfocus") == 0) {
6745 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
6747 err(1, "spawn_custom color unfocus");
6748 } else if (strcasecmp(ap, "$color_unfocus_maximized") == 0) {
6750 strdup(r->s->c[SWM_S_COLOR_UNFOCUS_MAXIMIZED].name))
6752 err(1, "spawn_custom color unfocus maximized");
6753 } else if (strcasecmp(ap, "$region_index") == 0) {
6754 if (asprintf(&real_args[c], "%d",
6755 get_region_index(r) + 1) < 1)
6756 err(1, "spawn_custom region index");
6757 } else if (strcasecmp(ap, "$workspace_index") == 0) {
6758 if (asprintf(&real_args[c], "%d", r->ws->idx + 1) < 1)
6759 err(1, "spawn_custom workspace index");
6760 } else if (strcasecmp(ap, "$dmenu_bottom") == 0) {
6763 if ((real_args[c] = strdup("-b")) == NULL)
6764 err(1, "spawn_custom workspace index");
6766 /* no match --> copy as is */
6767 if ((real_args[c] = strdup(ap)) == NULL)
6768 err(1, "spawn_custom strdup(ap)");
6770 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg: %s\n",
6776 DNPRINTF(SWM_D_SPAWN, "spawn_custom: result: ");
6777 for (i = 0; i < c; ++i)
6778 DPRINTF("\"%s\" ", real_args[i]);
6781 *ret_args = real_args;
6786 spawn_custom(struct swm_region *r, union arg *args, const char *spawn_name)
6792 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
6796 spawn(r->ws->idx, &a, true);
6798 for (i = 0; i < spawn_argc; i++)
6804 spawn_select(struct swm_region *r, union arg *args, const char *spawn_name,
6811 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
6815 if (pipe(select_list_pipe) == -1)
6816 err(1, "pipe error");
6817 if (pipe(select_resp_pipe) == -1)
6818 err(1, "pipe error");
6820 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
6821 err(1, "could not disable SIGPIPE");
6822 switch (*pid = fork()) {
6824 err(1, "cannot fork");
6827 if (dup2(select_list_pipe[0], STDIN_FILENO) == -1)
6829 if (dup2(select_resp_pipe[1], STDOUT_FILENO) == -1)
6831 close(select_list_pipe[1]);
6832 close(select_resp_pipe[0]);
6833 spawn(r->ws->idx, &a, false);
6835 default: /* parent */
6836 close(select_list_pipe[0]);
6837 close(select_resp_pipe[1]);
6841 for (i = 0; i < spawn_argc; i++)
6846 /* Argument tokenizer. */
6849 char *arg, *cp, *next;
6850 bool single_quoted = false, double_quoted = false;
6855 /* Eat and move characters until end of argument is found. */
6856 for (arg = next = cp = *sp; *cp != '\0'; ++cp) {
6857 if (!double_quoted && *cp == '\'') {
6858 /* Eat single-quote. */
6859 single_quoted = !single_quoted;
6860 } else if (!single_quoted && *cp == '"') {
6861 /* Eat double-quote. */
6862 double_quoted = !double_quoted;
6863 } else if (!single_quoted && *cp == '\\' && *(cp + 1) == '"') {
6864 /* Eat backslash; copy escaped character to arg. */
6866 } else if (!single_quoted && !double_quoted && *cp == '\\' &&
6867 (*(cp + 1) == '\'' || *(cp + 1) == ' ')) {
6868 /* Eat backslash; move escaped character. */
6870 } else if (!single_quoted && !double_quoted &&
6871 (*cp == ' ' || *cp == '\t')) {
6872 /* Terminate argument. */
6874 /* Point sp to beginning of next argument. */
6878 /* Move regular character. */
6883 /* Terminate argument if end of string. */
6893 spawn_insert(const char *name, const char *args, int flags)
6895 struct spawn_prog *sp;
6896 char *arg, *cp, *ptr;
6898 DNPRINTF(SWM_D_SPAWN, "spawn_insert: %s[%s]\n", name, args);
6900 if (args == NULL || *args == '\0')
6903 if ((sp = calloc(1, sizeof *sp)) == NULL)
6904 err(1, "spawn_insert: calloc");
6905 if ((sp->name = strdup(name)) == NULL)
6906 err(1, "spawn_insert: strdup");
6908 /* Convert the arguments to an argument list. */
6909 if ((ptr = cp = strdup(args)) == NULL)
6910 err(1, "spawn_insert: strdup");
6911 while ((arg = argsep(&ptr)) != NULL) {
6912 /* Null argument; skip it. */
6917 if ((sp->argv = realloc(sp->argv, sp->argc *
6918 sizeof *sp->argv)) == NULL)
6919 err(1, "spawn_insert: realloc");
6920 if ((sp->argv[sp->argc - 1] = strdup(arg)) == NULL)
6921 err(1, "spawn_insert: strdup");
6927 DNPRINTF(SWM_D_SPAWN, "arg %d: [%s]\n", sp->argc, sp->argv[sp->argc-1]);
6928 TAILQ_INSERT_TAIL(&spawns, sp, entry);
6929 DNPRINTF(SWM_D_SPAWN, "spawn_insert: leave\n");
6933 spawn_remove(struct spawn_prog *sp)
6937 DNPRINTF(SWM_D_SPAWN, "spawn_remove: %s\n", sp->name);
6939 TAILQ_REMOVE(&spawns, sp, entry);
6940 for (i = 0; i < sp->argc; i++)
6946 DNPRINTF(SWM_D_SPAWN, "spawn_remove: leave\n");
6950 spawn_find(const char *name)
6952 struct spawn_prog *sp;
6954 TAILQ_FOREACH(sp, &spawns, entry)
6955 if (strcasecmp(sp->name, name) == 0)
6962 setspawn(const char *name, const char *args, int flags)
6964 struct spawn_prog *sp;
6966 DNPRINTF(SWM_D_SPAWN, "setspawn: %s\n", name);
6971 /* Remove any old spawn under the same name. */
6972 if ((sp = spawn_find(name)) != NULL)
6976 spawn_insert(name, args, flags);
6978 warnx("error: setspawn: cannot find program: %s", name);
6980 DNPRINTF(SWM_D_SPAWN, "setspawn: leave\n");
6984 setconfspawn(const char *selector, const char *value, int flags)
6988 if (selector == NULL || strlen(selector) == 0)
6991 args = expand_tilde(value);
6993 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, args);
6995 setspawn(selector, args, flags);
6998 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done.\n");
7003 validate_spawns(void)
7005 struct spawn_prog *sp;
7006 char which[PATH_MAX];
7011 RB_FOREACH(kp, key_tree, &keys) {
7012 if (kp->funcid != KF_SPAWN_CUSTOM)
7016 sp = spawn_find(kp->spawn_name);
7017 if (sp == NULL || sp->flags & SWM_SPAWN_OPTIONAL)
7020 /* verify we have the goods */
7021 snprintf(which, sizeof which, "which %s", sp->argv[0]);
7022 DNPRINTF(SWM_D_CONF, "validate_spawns: which %s\n",
7024 for (i = strlen("which "); i < strlen(which); i++)
7025 if (which[i] == ' ') {
7029 if (system(which) != 0)
7030 add_startup_exception("could not find %s",
7031 &which[strlen("which ")]);
7038 setconfspawn("lock", "xlock", 0);
7040 setconfspawn("term", "xterm", 0);
7041 setconfspawn("spawn_term", "xterm", 0);
7043 setconfspawn("menu", "dmenu_run"
7047 " -nf $bar_font_color"
7049 " -sf $bar_color", 0);
7051 setconfspawn("search", "dmenu"
7056 " -nf $bar_font_color"
7058 " -sf $bar_color", 0);
7060 setconfspawn("name_workspace", "dmenu"
7065 " -nf $bar_font_color"
7067 " -sf $bar_color", 0);
7069 /* These are not verified for existence, even with a binding set. */
7070 setconfspawn("screenshot_all", "screenshot.sh full", SWM_SPAWN_OPTIONAL);
7071 setconfspawn("screenshot_wind", "screenshot.sh window", SWM_SPAWN_OPTIONAL);
7072 setconfspawn("initscr", "initscreen.sh", SWM_SPAWN_OPTIONAL);
7076 #define SWM_MODNAME_SIZE 32
7077 #define SWM_KEY_WS "\n+ \t"
7079 parsekeys(const char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
7081 char *str, *cp, *name;
7084 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
7085 if (mod == NULL || ks == NULL) {
7086 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
7089 if (keystr == NULL || strlen(keystr) == 0) {
7090 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
7094 if ((cp = str = strdup(keystr)) == NULL)
7095 err(1, "parsekeys: strdup");
7097 *ks = XCB_NO_SYMBOL;
7099 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
7100 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
7102 cp += (long)strspn(cp, SWM_KEY_WS);
7103 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
7105 else if (strncasecmp(name, "Mod1", SWM_MODNAME_SIZE) == 0)
7106 *mod |= XCB_MOD_MASK_1;
7107 else if (strncasecmp(name, "Mod2", SWM_MODNAME_SIZE) == 0)
7108 *mod += XCB_MOD_MASK_2;
7109 else if (strncmp(name, "Mod3", SWM_MODNAME_SIZE) == 0)
7110 *mod |= XCB_MOD_MASK_3;
7111 else if (strncmp(name, "Mod4", SWM_MODNAME_SIZE) == 0)
7112 *mod |= XCB_MOD_MASK_4;
7113 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
7114 *mod |= XCB_MOD_MASK_SHIFT;
7115 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
7116 *mod |= XCB_MOD_MASK_CONTROL;
7118 *ks = XStringToKeysym(name);
7119 XConvertCase(*ks, ks, &uks);
7120 if (ks == XCB_NO_SYMBOL) {
7122 "parsekeys: invalid key %s\n",
7131 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
7136 strdupsafe(const char *str)
7141 return (strdup(str));
7145 key_insert(unsigned int mod, KeySym ks, enum keyfuncid kfid,
7146 const char *spawn_name)
7150 DNPRINTF(SWM_D_KEY, "key_insert: enter %s [%s]\n",
7151 keyfuncs[kfid].name, spawn_name);
7153 if ((kp = malloc(sizeof *kp)) == NULL)
7154 err(1, "key_insert: malloc");
7159 kp->spawn_name = strdupsafe(spawn_name);
7160 RB_INSERT(key_tree, &keys, kp);
7162 DNPRINTF(SWM_D_KEY, "key_insert: leave\n");
7166 key_lookup(unsigned int mod, KeySym ks)
7173 return (RB_FIND(key_tree, &keys, &kp));
7177 key_remove(struct key *kp)
7179 DNPRINTF(SWM_D_KEY, "key_remove: %s\n", keyfuncs[kp->funcid].name);
7181 RB_REMOVE(key_tree, &keys, kp);
7182 free(kp->spawn_name);
7185 DNPRINTF(SWM_D_KEY, "key_remove: leave\n");
7189 key_replace(struct key *kp, unsigned int mod, KeySym ks, enum keyfuncid kfid,
7190 const char *spawn_name)
7192 DNPRINTF(SWM_D_KEY, "key_replace: %s [%s]\n", keyfuncs[kp->funcid].name,
7196 key_insert(mod, ks, kfid, spawn_name);
7198 DNPRINTF(SWM_D_KEY, "key_replace: leave\n");
7202 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid,
7203 const char *spawn_name)
7207 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
7208 keyfuncs[kfid].name, spawn_name);
7210 if ((kp = key_lookup(mod, ks)) != NULL) {
7211 if (kfid == KF_INVALID)
7214 key_replace(kp, mod, ks, kfid, spawn_name);
7215 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
7218 if (kfid == KF_INVALID) {
7219 warnx("bind: Key combination already unbound.");
7220 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
7224 key_insert(mod, ks, kfid, spawn_name);
7225 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
7229 setconfbinding(const char *selector, const char *value, int flags)
7231 enum keyfuncid kfid;
7234 struct spawn_prog *sp;
7236 /* suppress unused warning since var is needed */
7239 DNPRINTF(SWM_D_KEY, "setconfbinding: enter selector: [%s], "
7240 "value: [%s]\n", selector, value);
7241 if (selector == NULL || strlen(selector) == 0) {
7242 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
7243 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
7245 setkeybinding(mod, ks, kfid, NULL);
7250 /* search by key function name */
7251 for (kfid = 0; kfid < KF_INVALID; (kfid)++) {
7252 if (strncasecmp(selector, keyfuncs[kfid].name,
7253 SWM_FUNCNAME_LEN) == 0) {
7254 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match "
7255 "keyfunc\n", selector);
7256 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
7257 setkeybinding(mod, ks, kfid, NULL);
7263 /* search by custom spawn name */
7264 if ((sp = spawn_find(selector)) != NULL) {
7265 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match "
7266 "spawn\n", selector);
7267 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
7268 setkeybinding(mod, ks, KF_SPAWN_CUSTOM,
7274 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
7281 #define MODKEY_SHIFT MODKEY | XCB_MOD_MASK_SHIFT
7282 setkeybinding(MODKEY, XK_b, KF_BAR_TOGGLE, NULL);
7283 setkeybinding(MODKEY_SHIFT, XK_b, KF_BAR_TOGGLE_WS,NULL);
7284 setkeybinding(MODKEY, XK_v, KF_BUTTON2, NULL);
7285 setkeybinding(MODKEY, XK_space, KF_CYCLE_LAYOUT,NULL);
7286 setkeybinding(MODKEY_SHIFT, XK_backslash, KF_FLIP_LAYOUT, NULL);
7287 setkeybinding(MODKEY, XK_t, KF_FLOAT_TOGGLE,NULL);
7288 setkeybinding(MODKEY, XK_m, KF_FOCUS_MAIN, NULL);
7289 setkeybinding(MODKEY, XK_j, KF_FOCUS_NEXT, NULL);
7290 setkeybinding(MODKEY, XK_Tab, KF_FOCUS_NEXT, NULL);
7291 setkeybinding(MODKEY, XK_k, KF_FOCUS_PREV, NULL);
7292 setkeybinding(MODKEY_SHIFT, XK_Tab, KF_FOCUS_PREV, NULL);
7293 setkeybinding(MODKEY, XK_u, KF_FOCUS_URGENT,NULL);
7294 setkeybinding(MODKEY, XK_e, KF_MAXIMIZE_TOGGLE,NULL);
7295 setkeybinding(MODKEY_SHIFT, XK_equal, KF_HEIGHT_GROW,NULL);
7296 setkeybinding(MODKEY_SHIFT, XK_minus, KF_HEIGHT_SHRINK,NULL);
7297 setkeybinding(MODKEY, XK_w, KF_ICONIFY, NULL);
7298 setkeybinding(MODKEY, XK_h, KF_MASTER_SHRINK, NULL);
7299 setkeybinding(MODKEY, XK_l, KF_MASTER_GROW, NULL);
7300 setkeybinding(MODKEY, XK_comma, KF_MASTER_ADD, NULL);
7301 setkeybinding(MODKEY, XK_period, KF_MASTER_DEL, NULL);
7302 setkeybinding(MODKEY_SHIFT, XK_bracketright,KF_MOVE_DOWN,NULL);
7303 setkeybinding(MODKEY, XK_bracketleft, KF_MOVE_LEFT,NULL);
7304 setkeybinding(MODKEY, XK_bracketright,KF_MOVE_RIGHT,NULL);
7305 setkeybinding(MODKEY_SHIFT, XK_bracketleft, KF_MOVE_UP, NULL);
7306 setkeybinding(MODKEY_SHIFT, XK_KP_End, KF_MVRG_1, NULL);
7307 setkeybinding(MODKEY_SHIFT, XK_KP_Down, KF_MVRG_2, NULL);
7308 setkeybinding(MODKEY_SHIFT, XK_KP_Next, KF_MVRG_3, NULL);
7309 setkeybinding(MODKEY_SHIFT, XK_KP_Left, KF_MVRG_4, NULL);
7310 setkeybinding(MODKEY_SHIFT, XK_KP_Begin, KF_MVRG_5, NULL);
7311 setkeybinding(MODKEY_SHIFT, XK_KP_Right, KF_MVRG_6, NULL);
7312 setkeybinding(MODKEY_SHIFT, XK_KP_Home, KF_MVRG_7, NULL);
7313 setkeybinding(MODKEY_SHIFT, XK_KP_Up, KF_MVRG_8, NULL);
7314 setkeybinding(MODKEY_SHIFT, XK_KP_Prior, KF_MVRG_9, NULL);
7315 setkeybinding(MODKEY_SHIFT, XK_1, KF_MVWS_1, NULL);
7316 setkeybinding(MODKEY_SHIFT, XK_2, KF_MVWS_2, NULL);
7317 setkeybinding(MODKEY_SHIFT, XK_3, KF_MVWS_3, NULL);
7318 setkeybinding(MODKEY_SHIFT, XK_4, KF_MVWS_4, NULL);
7319 setkeybinding(MODKEY_SHIFT, XK_5, KF_MVWS_5, NULL);
7320 setkeybinding(MODKEY_SHIFT, XK_6, KF_MVWS_6, NULL);
7321 setkeybinding(MODKEY_SHIFT, XK_7, KF_MVWS_7, NULL);
7322 setkeybinding(MODKEY_SHIFT, XK_8, KF_MVWS_8, NULL);
7323 setkeybinding(MODKEY_SHIFT, XK_9, KF_MVWS_9, NULL);
7324 setkeybinding(MODKEY_SHIFT, XK_0, KF_MVWS_10, NULL);
7325 setkeybinding(MODKEY_SHIFT, XK_F1, KF_MVWS_11, NULL);
7326 setkeybinding(MODKEY_SHIFT, XK_F2, KF_MVWS_12, NULL);
7327 setkeybinding(MODKEY_SHIFT, XK_F3, KF_MVWS_13, NULL);
7328 setkeybinding(MODKEY_SHIFT, XK_F4, KF_MVWS_14, NULL);
7329 setkeybinding(MODKEY_SHIFT, XK_F5, KF_MVWS_15, NULL);
7330 setkeybinding(MODKEY_SHIFT, XK_F6, KF_MVWS_16, NULL);
7331 setkeybinding(MODKEY_SHIFT, XK_F7, KF_MVWS_17, NULL);
7332 setkeybinding(MODKEY_SHIFT, XK_F8, KF_MVWS_18, NULL);
7333 setkeybinding(MODKEY_SHIFT, XK_F9, KF_MVWS_19, NULL);
7334 setkeybinding(MODKEY_SHIFT, XK_F10, KF_MVWS_20, NULL);
7335 setkeybinding(MODKEY_SHIFT, XK_F11, KF_MVWS_21, NULL);
7336 setkeybinding(MODKEY_SHIFT, XK_F12, KF_MVWS_22, NULL);
7337 setkeybinding(MODKEY_SHIFT, XK_slash, KF_NAME_WORKSPACE,NULL);
7338 setkeybinding(MODKEY_SHIFT, XK_q, KF_QUIT, NULL);
7339 setkeybinding(MODKEY_SHIFT, XK_r, KF_RAISE_TOGGLE,NULL);
7340 setkeybinding(MODKEY, XK_q, KF_RESTART, NULL);
7341 setkeybinding(MODKEY, XK_KP_End, KF_RG_1, NULL);
7342 setkeybinding(MODKEY, XK_KP_Down, KF_RG_2, NULL);
7343 setkeybinding(MODKEY, XK_KP_Next, KF_RG_3, NULL);
7344 setkeybinding(MODKEY, XK_KP_Left, KF_RG_4, NULL);
7345 setkeybinding(MODKEY, XK_KP_Begin, KF_RG_5, NULL);
7346 setkeybinding(MODKEY, XK_KP_Right, KF_RG_6, NULL);
7347 setkeybinding(MODKEY, XK_KP_Home, KF_RG_7, NULL);
7348 setkeybinding(MODKEY, XK_KP_Up, KF_RG_8, NULL);
7349 setkeybinding(MODKEY, XK_KP_Prior, KF_RG_9, NULL);
7350 setkeybinding(MODKEY_SHIFT, XK_Right, KF_RG_NEXT, NULL);
7351 setkeybinding(MODKEY_SHIFT, XK_Left, KF_RG_PREV, NULL);
7352 setkeybinding(MODKEY, XK_f, KF_SEARCH_WIN, NULL);
7353 setkeybinding(MODKEY, XK_slash, KF_SEARCH_WORKSPACE,NULL);
7354 setkeybinding(MODKEY_SHIFT, XK_i, KF_SPAWN_CUSTOM,"initscr");
7355 setkeybinding(MODKEY_SHIFT, XK_Delete, KF_SPAWN_CUSTOM,"lock");
7356 setkeybinding(MODKEY, XK_p, KF_SPAWN_CUSTOM,"menu");
7357 setkeybinding(MODKEY, XK_s, KF_SPAWN_CUSTOM,"screenshot_all");
7358 setkeybinding(MODKEY_SHIFT, XK_s, KF_SPAWN_CUSTOM,"screenshot_wind");
7359 setkeybinding(MODKEY_SHIFT, XK_Return, KF_SPAWN_CUSTOM,"term");
7360 setkeybinding(MODKEY_SHIFT, XK_comma, KF_STACK_INC, NULL);
7361 setkeybinding(MODKEY_SHIFT, XK_period, KF_STACK_DEC, NULL);
7362 setkeybinding(MODKEY_SHIFT, XK_space, KF_STACK_RESET, NULL);
7363 setkeybinding(MODKEY, XK_Return, KF_SWAP_MAIN, NULL);
7364 setkeybinding(MODKEY_SHIFT, XK_j, KF_SWAP_NEXT, NULL);
7365 setkeybinding(MODKEY_SHIFT, XK_k, KF_SWAP_PREV, NULL);
7366 setkeybinding(MODKEY_SHIFT, XK_w, KF_UNICONIFY, NULL);
7367 setkeybinding(MODKEY_SHIFT, XK_v, KF_VERSION, NULL);
7368 setkeybinding(MODKEY, XK_equal, KF_WIDTH_GROW, NULL);
7369 setkeybinding(MODKEY, XK_minus, KF_WIDTH_SHRINK,NULL);
7370 setkeybinding(MODKEY, XK_x, KF_WIND_DEL, NULL);
7371 setkeybinding(MODKEY_SHIFT, XK_x, KF_WIND_KILL, NULL);
7372 setkeybinding(MODKEY, XK_1, KF_WS_1, NULL);
7373 setkeybinding(MODKEY, XK_2, KF_WS_2, NULL);
7374 setkeybinding(MODKEY, XK_3, KF_WS_3, NULL);
7375 setkeybinding(MODKEY, XK_4, KF_WS_4, NULL);
7376 setkeybinding(MODKEY, XK_5, KF_WS_5, NULL);
7377 setkeybinding(MODKEY, XK_6, KF_WS_6, NULL);
7378 setkeybinding(MODKEY, XK_7, KF_WS_7, NULL);
7379 setkeybinding(MODKEY, XK_8, KF_WS_8, NULL);
7380 setkeybinding(MODKEY, XK_9, KF_WS_9, NULL);
7381 setkeybinding(MODKEY, XK_0, KF_WS_10, NULL);
7382 setkeybinding(MODKEY, XK_F1, KF_WS_11, NULL);
7383 setkeybinding(MODKEY, XK_F2, KF_WS_12, NULL);
7384 setkeybinding(MODKEY, XK_F3, KF_WS_13, NULL);
7385 setkeybinding(MODKEY, XK_F4, KF_WS_14, NULL);
7386 setkeybinding(MODKEY, XK_F5, KF_WS_15, NULL);
7387 setkeybinding(MODKEY, XK_F6, KF_WS_16, NULL);
7388 setkeybinding(MODKEY, XK_F7, KF_WS_17, NULL);
7389 setkeybinding(MODKEY, XK_F8, KF_WS_18, NULL);
7390 setkeybinding(MODKEY, XK_F9, KF_WS_19, NULL);
7391 setkeybinding(MODKEY, XK_F10, KF_WS_20, NULL);
7392 setkeybinding(MODKEY, XK_F11, KF_WS_21, NULL);
7393 setkeybinding(MODKEY, XK_F12, KF_WS_22, NULL);
7394 setkeybinding(MODKEY, XK_Right, KF_WS_NEXT, NULL);
7395 setkeybinding(MODKEY, XK_Left, KF_WS_PREV, NULL);
7396 setkeybinding(MODKEY, XK_Up, KF_WS_NEXT_ALL, NULL);
7397 setkeybinding(MODKEY, XK_Down, KF_WS_PREV_ALL, NULL);
7398 setkeybinding(MODKEY_SHIFT, XK_Up, KF_WS_NEXT_MOVE,NULL);
7399 setkeybinding(MODKEY_SHIFT, XK_Down, KF_WS_PREV_MOVE,NULL);
7400 setkeybinding(MODKEY, XK_a, KF_WS_PRIOR, NULL);
7402 setkeybinding(MODKEY_SHIFT, XK_d, KF_DUMPWINS, NULL);
7412 while (RB_EMPTY(&keys) == 0) {
7413 kp = RB_ROOT(&keys);
7419 setkeymapping(const char *selector, const char *value, int flags)
7421 char *keymapping_file;
7423 /* suppress unused warnings since vars are needed */
7427 DNPRINTF(SWM_D_KEY, "setkeymapping: enter\n");
7429 keymapping_file = expand_tilde(value);
7432 /* load new key bindings; if it fails, revert to default bindings */
7433 if (conf_load(keymapping_file, SWM_CONF_KEYMAPPING)) {
7438 free(keymapping_file);
7440 DNPRINTF(SWM_D_KEY, "setkeymapping: leave\n");
7445 updatenumlockmask(void)
7448 xcb_get_modifier_mapping_reply_t *modmap_r;
7449 xcb_keycode_t *modmap, kc, *keycode;
7453 modmap_r = xcb_get_modifier_mapping_reply(conn,
7454 xcb_get_modifier_mapping(conn),
7457 modmap = xcb_get_modifier_mapping_keycodes(modmap_r);
7458 for (i = 0; i < 8; i++) {
7459 for (j = 0; j < modmap_r->keycodes_per_modifier; j++) {
7460 kc = modmap[i * modmap_r->keycodes_per_modifier
7462 keycode = xcb_key_symbols_get_keycode(syms,
7466 numlockmask = (1 << i);
7473 DNPRINTF(SWM_D_MISC, "updatenumlockmask: %d\n", numlockmask);
7480 int num_screens, k, j;
7481 unsigned int modifiers[4];
7482 xcb_keycode_t *code;
7484 DNPRINTF(SWM_D_MISC, "grabkeys\n");
7485 updatenumlockmask();
7488 modifiers[1] = numlockmask;
7489 modifiers[2] = XCB_MOD_MASK_LOCK;
7490 modifiers[3] = numlockmask | XCB_MOD_MASK_LOCK;
7492 num_screens = get_screen_count();
7493 for (k = 0; k < num_screens; k++) {
7494 if (TAILQ_EMPTY(&screens[k].rl))
7496 xcb_ungrab_key(conn, XCB_GRAB_ANY, screens[k].root,
7498 RB_FOREACH(kp, key_tree, &keys) {
7499 /* Skip unused ws binds. */
7500 if ((int)kp->funcid > KF_WS_1 + workspace_limit - 1 &&
7501 kp->funcid <= KF_WS_22)
7504 /* Skip unused mvws binds. */
7505 if ((int)kp->funcid > KF_MVWS_1 + workspace_limit - 1 &&
7506 kp->funcid <= KF_MVWS_22)
7509 if ((code = xcb_key_symbols_get_keycode(syms,
7511 for (j = 0; j < LENGTH(modifiers); j++)
7512 xcb_grab_key(conn, 1,
7514 kp->mod | modifiers[j],
7515 *code, XCB_GRAB_MODE_SYNC,
7516 XCB_GRAB_MODE_SYNC);
7524 grabbuttons(struct ws_win *win)
7526 unsigned int modifiers[4];
7529 DNPRINTF(SWM_D_MOUSE, "grabbuttons: win %#x\n", win->id);
7530 updatenumlockmask();
7533 modifiers[1] = numlockmask;
7534 modifiers[2] = XCB_MOD_MASK_LOCK;
7535 modifiers[3] = numlockmask | XCB_MOD_MASK_LOCK;
7537 for (i = 0; i < LENGTH(buttons); i++)
7538 if (buttons[i].action == client_click)
7539 for (j = 0; j < LENGTH(modifiers); ++j)
7540 xcb_grab_button(conn, 0, win->id, BUTTONMASK,
7541 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC,
7542 XCB_WINDOW_NONE, XCB_CURSOR_NONE,
7543 buttons[i].button, buttons[i].mask |
7547 const char *quirkname[] = {
7548 "NONE", /* config string for "no value" */
7556 "FOCUSONMAP_SINGLE",
7562 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
7563 #define SWM_Q_WS "\n|+ \t"
7565 parsequirks(const char *qstr, unsigned long *quirk)
7567 char *str, *cp, *name;
7570 if (quirk == NULL || qstr == NULL)
7573 if ((str = strdup(qstr)) == NULL)
7574 err(1, "parsequirks: strdup");
7578 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
7580 cp += (long)strspn(cp, SWM_Q_WS);
7581 for (i = 0; i < LENGTH(quirkname); i++) {
7582 if (strncasecmp(name, quirkname[i],
7583 SWM_QUIRK_LEN) == 0) {
7584 DNPRINTF(SWM_D_QUIRK,
7585 "parsequirks: %s\n", name);
7591 *quirk |= 1 << (i-1);
7595 if (i >= LENGTH(quirkname)) {
7596 DNPRINTF(SWM_D_QUIRK,
7597 "parsequirks: invalid quirk [%s]\n", name);
7608 quirk_insert(const char *class, const char *instance, const char *name,
7609 unsigned long quirk)
7613 bool failed = false;
7615 DNPRINTF(SWM_D_QUIRK, "quirk_insert: class: %s, instance: %s, name: %s,"
7616 " value: %lu\n", class, instance, name, quirk);
7618 if ((qp = malloc(sizeof *qp)) == NULL)
7619 err(1, "quirk_insert: malloc");
7621 if ((qp->class = strdup(class)) == NULL)
7622 err(1, "quirk_insert: strdup");
7623 if ((qp->instance = strdup(instance)) == NULL)
7624 err(1, "quirk_insert: strdup");
7625 if ((qp->name = strdup(name)) == NULL)
7626 err(1, "quirk_insert: strdup");
7628 if (asprintf(&str, "^%s$", class) == -1)
7629 err(1, "quirk_insert: asprintf");
7630 if (regcomp(&qp->regex_class, str, REG_EXTENDED | REG_NOSUB)) {
7631 add_startup_exception("regex failed to compile quirk 'class' "
7632 "field: %s", class);
7635 DNPRINTF(SWM_D_QUIRK, "quirk_insert: compiled: %s\n", str);
7638 if (asprintf(&str, "^%s$", instance) == -1)
7639 err(1, "quirk_insert: asprintf");
7640 if (regcomp(&qp->regex_instance, str, REG_EXTENDED | REG_NOSUB)) {
7641 add_startup_exception("regex failed to compile quirk 'instance'"
7642 " field: %s", instance);
7645 DNPRINTF(SWM_D_QUIRK, "quirk_insert: compiled: %s\n", str);
7648 if (asprintf(&str, "^%s$", name) == -1)
7649 err(1, "quirk_insert: asprintf");
7650 if (regcomp(&qp->regex_name, str, REG_EXTENDED | REG_NOSUB)) {
7651 add_startup_exception("regex failed to compile quirk 'name' "
7655 DNPRINTF(SWM_D_QUIRK, "quirk_insert: compiled: %s\n", str);
7659 DNPRINTF(SWM_D_QUIRK, "quirk_insert: regex error; skipping.\n");
7663 TAILQ_INSERT_TAIL(&quirks, qp, entry);
7665 DNPRINTF(SWM_D_QUIRK, "quirk_insert: leave\n");
7669 quirk_remove(struct quirk *qp)
7671 DNPRINTF(SWM_D_QUIRK, "quirk_remove: %s:%s [%lu]\n", qp->class,
7672 qp->name, qp->quirk);
7674 TAILQ_REMOVE(&quirks, qp, entry);
7677 DNPRINTF(SWM_D_QUIRK, "quirk_remove: leave\n");
7681 quirk_free(struct quirk *qp)
7683 regfree(&qp->regex_class);
7684 regfree(&qp->regex_instance);
7685 regfree(&qp->regex_name);
7693 quirk_replace(struct quirk *qp, const char *class, const char *instance,
7694 const char *name, unsigned long quirk)
7696 DNPRINTF(SWM_D_QUIRK, "quirk_replace: %s:%s:%s [%lu]\n", qp->class,
7697 qp->instance, qp->name, qp->quirk);
7700 quirk_insert(class, instance, name, quirk);
7702 DNPRINTF(SWM_D_QUIRK, "quirk_replace: leave\n");
7706 setquirk(const char *class, const char *instance, const char *name,
7707 unsigned long quirk)
7711 DNPRINTF(SWM_D_QUIRK, "setquirk: enter %s:%s:%s [%lu]\n", class,
7712 instance, name, quirk);
7714 /* Remove/replace existing quirk. */
7715 TAILQ_FOREACH(qp, &quirks, entry) {
7716 if (strcmp(qp->class, class) == 0 &&
7717 strcmp(qp->instance, instance) == 0 &&
7718 strcmp(qp->name, name) == 0) {
7722 quirk_replace(qp, class, instance, name, quirk);
7723 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
7728 /* Only insert if quirk is not NONE. */
7730 quirk_insert(class, instance, name, quirk);
7732 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
7735 /* Eat '\' in str used to escape square brackets and colon. */
7737 unescape_selector(char *str)
7741 for (cp = str; *str != '\0'; ++str, ++cp) {
7742 if (*str == '\\' && (*(str + 1) == ':' || *(str + 1) == ']' ||
7752 setconfquirk(const char *selector, const char *value, int flags)
7754 char *str, *cp, *class;
7755 char *instance = NULL, *name = NULL;
7756 int retval, count = 0;
7759 /* suppress unused warning since var is needed */
7762 if (selector == NULL || strlen(selector) == 0)
7765 if ((str = strdup(selector)) == NULL)
7766 err(1, "setconfquirk: strdup");
7768 /* Find non-escaped colon. */
7775 for (++cp; *cp != '\0'; ++cp) {
7776 if (*cp == ':' && *(cp - 1) != '\\') {
7782 unescape_selector(class);
7784 instance = class + strlen(class) + 1;
7785 unescape_selector(instance);
7791 name = instance + strlen(instance) + 1;
7792 unescape_selector(name);
7797 DNPRINTF(SWM_D_CONF, "setconfquirk: class: %s, instance: %s, "
7798 "name: %s\n", class, instance, name);
7800 if ((retval = parsequirks(value, &qrks)) == 0)
7801 setquirk(class, instance, name, qrks);
7810 setquirk("MPlayer", "xv", ".*", SWM_Q_FLOAT | SWM_Q_FULLSCREEN | SWM_Q_FOCUSPREV);
7811 setquirk("OpenOffice.org 3.2", "VCLSalFrame", ".*", SWM_Q_FLOAT);
7812 setquirk("Firefox-bin", "firefox-bin", ".*", SWM_Q_TRANSSZ);
7813 setquirk("Firefox", "Dialog", ".*", SWM_Q_FLOAT);
7814 setquirk("Gimp", "gimp", ".*", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
7815 setquirk("XTerm", "xterm", ".*", SWM_Q_XTERM_FONTADJ);
7816 setquirk("xine", "Xine Window", ".*", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
7817 setquirk("Xitk", "Xitk Combo", ".*", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
7818 setquirk("xine", "xine Panel", ".*", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
7819 setquirk("Xitk", "Xine Window", ".*", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
7820 setquirk("xine", "xine Video Fullscreen Window", ".*", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
7821 setquirk("pcb", "pcb", ".*", SWM_Q_FLOAT);
7822 setquirk("SDL_App", "SDL_App", ".*", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
7825 /* conf file stuff */
7826 #define SWM_CONF_FILE "spectrwm.conf"
7827 #define SWM_CONF_FILE_OLD "scrotwm.conf"
7831 SWM_S_BAR_AT_BOTTOM,
7832 SWM_S_BAR_BORDER_WIDTH,
7835 SWM_S_BAR_ENABLED_WS,
7840 SWM_S_BOUNDARY_WIDTH,
7841 SWM_S_CLOCK_ENABLED,
7844 SWM_S_CYCLE_VISIBLE,
7846 SWM_S_DISABLE_BORDER,
7848 SWM_S_FOCUS_CLOSE_WRAP,
7849 SWM_S_FOCUS_DEFAULT,
7851 SWM_S_ICONIC_ENABLED,
7852 SWM_S_JAVA_WORKAROUND,
7853 SWM_S_REGION_PADDING,
7858 SWM_S_STACK_ENABLED,
7861 SWM_S_URGENT_ENABLED,
7862 SWM_S_VERBOSE_LAYOUT,
7863 SWM_S_WINDOW_CLASS_ENABLED,
7864 SWM_S_WINDOW_INSTANCE_ENABLED,
7865 SWM_S_WINDOW_NAME_ENABLED,
7866 SWM_S_WORKSPACE_LIMIT,
7867 SWM_S_WORKSPACE_NAME,
7871 setconfvalue(const char *selector, const char *value, int flags)
7873 struct workspace *ws;
7874 int i, ws_id, num_screens;
7875 char *b, *str, s[1024];
7878 case SWM_S_BAR_ACTION:
7880 if ((bar_argv[0] = expand_tilde(value)) == NULL)
7881 err(1, "setconfvalue: bar_action");
7883 case SWM_S_BAR_AT_BOTTOM:
7884 bar_at_bottom = (atoi(value) != 0);
7886 case SWM_S_BAR_BORDER_WIDTH:
7887 bar_border_width = atoi(value);
7888 if (bar_border_width < 0)
7889 bar_border_width = 0;
7891 case SWM_S_BAR_DELAY:
7892 /* No longer needed; leave to not break old conf files. */
7894 case SWM_S_BAR_ENABLED:
7895 bar_enabled = (atoi(value) != 0);
7897 case SWM_S_BAR_ENABLED_WS:
7898 ws_id = atoi(selector) - 1;
7899 if (ws_id < 0 || ws_id >= workspace_limit)
7900 errx(1, "setconfvalue: bar_enabled_ws: invalid "
7901 "workspace %d.", ws_id + 1);
7903 num_screens = get_screen_count();
7904 for (i = 0; i < num_screens; i++) {
7905 ws = (struct workspace *)&screens[i].ws;
7906 ws[ws_id].bar_enabled = (atoi(value) != 0);
7909 case SWM_S_BAR_FONT:
7911 if (asprintf(&bar_fonts, "%s,%s", value, bar_fonts) == -1)
7912 err(1, "setconfvalue: asprintf: failed to allocate "
7913 "memory for bar_fonts.");
7916 /* If already in xft mode, then we are done. */
7917 if (!bar_font_legacy)
7920 if ((str = strdup(value)) == NULL)
7921 err(1, "setconfvalue: strdup");
7923 /* If there are any non-XLFD entries, switch to Xft mode. */
7924 while ((b = strsep(&str, ",")) != NULL) {
7928 bar_font_legacy = false;
7935 case SWM_S_BAR_FORMAT:
7937 if ((bar_format = strdup(value)) == NULL)
7938 err(1, "setconfvalue: bar_format");
7940 case SWM_S_BAR_JUSTIFY:
7941 if (strcmp(value, "left") == 0)
7942 bar_justify = SWM_BAR_JUSTIFY_LEFT;
7943 else if (strcmp(value, "center") == 0)
7944 bar_justify = SWM_BAR_JUSTIFY_CENTER;
7945 else if (strcmp(value, "right") == 0)
7946 bar_justify = SWM_BAR_JUSTIFY_RIGHT;
7948 errx(1, "invalid bar_justify");
7950 case SWM_S_BORDER_WIDTH:
7951 border_width = atoi(value);
7952 if (border_width < 0)
7955 case SWM_S_BOUNDARY_WIDTH:
7956 boundary_width = atoi(value);
7957 if (boundary_width < 0)
7960 case SWM_S_CLOCK_ENABLED:
7961 clock_enabled = (atoi(value) != 0);
7963 case SWM_S_CLOCK_FORMAT:
7964 #ifndef SWM_DENY_CLOCK_FORMAT
7966 if ((clock_format = strdup(value)) == NULL)
7967 err(1, "setconfvalue: clock_format");
7970 case SWM_S_CYCLE_EMPTY:
7971 cycle_empty = (atoi(value) != 0);
7973 case SWM_S_CYCLE_VISIBLE:
7974 cycle_visible = (atoi(value) != 0);
7976 case SWM_S_DIALOG_RATIO:
7977 dialog_ratio = atof(value);
7978 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
7981 case SWM_S_DISABLE_BORDER:
7982 disable_border = (atoi(value) != 0);
7984 case SWM_S_FOCUS_CLOSE:
7985 if (strcmp(value, "first") == 0)
7986 focus_close = SWM_STACK_BOTTOM;
7987 else if (strcmp(value, "last") == 0)
7988 focus_close = SWM_STACK_TOP;
7989 else if (strcmp(value, "next") == 0)
7990 focus_close = SWM_STACK_ABOVE;
7991 else if (strcmp(value, "previous") == 0)
7992 focus_close = SWM_STACK_BELOW;
7994 errx(1, "focus_close");
7996 case SWM_S_FOCUS_CLOSE_WRAP:
7997 focus_close_wrap = (atoi(value) != 0);
7999 case SWM_S_FOCUS_DEFAULT:
8000 if (strcmp(value, "last") == 0)
8001 focus_default = SWM_STACK_TOP;
8002 else if (strcmp(value, "first") == 0)
8003 focus_default = SWM_STACK_BOTTOM;
8005 errx(1, "focus_default");
8007 case SWM_S_FOCUS_MODE:
8008 if (strcmp(value, "default") == 0)
8009 focus_mode = SWM_FOCUS_DEFAULT;
8010 else if (strcmp(value, "follow") == 0 ||
8011 strcmp(value, "follow_cursor") == 0)
8012 focus_mode = SWM_FOCUS_FOLLOW;
8013 else if (strcmp(value, "manual") == 0)
8014 focus_mode = SWM_FOCUS_MANUAL;
8016 errx(1, "focus_mode");
8018 case SWM_S_ICONIC_ENABLED:
8019 iconic_enabled = (atoi(value) != 0);
8021 case SWM_S_JAVA_WORKAROUND:
8022 java_workaround = (atoi(value) != 0);
8024 case SWM_S_REGION_PADDING:
8025 region_padding = atoi(value);
8026 if (region_padding < 0)
8029 case SWM_S_SPAWN_ORDER:
8030 if (strcmp(value, "first") == 0)
8031 spawn_position = SWM_STACK_BOTTOM;
8032 else if (strcmp(value, "last") == 0)
8033 spawn_position = SWM_STACK_TOP;
8034 else if (strcmp(value, "next") == 0)
8035 spawn_position = SWM_STACK_ABOVE;
8036 else if (strcmp(value, "previous") == 0)
8037 spawn_position = SWM_STACK_BELOW;
8039 errx(1, "spawn_position");
8041 case SWM_S_SPAWN_TERM:
8042 setconfspawn("term", value, 0);
8043 setconfspawn("spawn_term", value, 0);
8046 /* No longer needed; leave to not break old conf files. */
8048 case SWM_S_SS_ENABLED:
8049 /* No longer needed; leave to not break old conf files. */
8051 case SWM_S_STACK_ENABLED:
8052 stack_enabled = (atoi(value) != 0);
8054 case SWM_S_TERM_WIDTH:
8055 term_width = atoi(value);
8059 case SWM_S_TILE_GAP:
8060 tile_gap = atoi(value);
8062 case SWM_S_URGENT_ENABLED:
8063 urgent_enabled = (atoi(value) != 0);
8065 case SWM_S_VERBOSE_LAYOUT:
8066 verbose_layout = (atoi(value) != 0);
8067 for (i = 0; layouts[i].l_stack != NULL; i++) {
8069 layouts[i].l_string = fancy_stacker;
8071 layouts[i].l_string = plain_stacker;
8074 case SWM_S_WINDOW_CLASS_ENABLED:
8075 window_class_enabled = (atoi(value) != 0);
8077 case SWM_S_WINDOW_INSTANCE_ENABLED:
8078 window_instance_enabled = (atoi(value) != 0);
8080 case SWM_S_WINDOW_NAME_ENABLED:
8081 window_name_enabled = (atoi(value) != 0);
8083 case SWM_S_WORKSPACE_LIMIT:
8084 workspace_limit = atoi(value);
8085 if (workspace_limit > SWM_WS_MAX)
8086 workspace_limit = SWM_WS_MAX;
8087 else if (workspace_limit < 1)
8088 workspace_limit = 1;
8090 ewmh_update_desktops();
8092 case SWM_S_WORKSPACE_NAME:
8093 if (getenv("SWM_STARTED") != NULL)
8097 if (sscanf(value, "ws[%d]:%1023c", &ws_id, s) != 2)
8098 errx(1, "invalid entry, should be 'ws[<idx>]:name'");
8100 if (ws_id < 0 || ws_id >= workspace_limit)
8101 errx(1, "setconfvalue: workspace_name: invalid "
8102 "workspace %d.", ws_id + 1);
8104 num_screens = get_screen_count();
8105 for (i = 0; i < num_screens; ++i) {
8106 ws = (struct workspace *)&screens[i].ws;
8108 if (strlen(s) > 0) {
8109 free(ws[ws_id].name);
8110 if ((ws[ws_id].name = strdup(s)) == NULL)
8111 err(1, "setconfvalue: workspace_name.");
8113 ewmh_update_desktop_names();
8114 ewmh_get_desktop_names();
8125 setconfmodkey(const char *selector, const char *value, int flags)
8127 /* suppress unused warnings since vars are needed */
8131 if (strncasecmp(value, "Mod1", strlen("Mod1")) == 0)
8132 update_modkey(XCB_MOD_MASK_1);
8133 else if (strncasecmp(value, "Mod2", strlen("Mod2")) == 0)
8134 update_modkey(XCB_MOD_MASK_2);
8135 else if (strncasecmp(value, "Mod3", strlen("Mod3")) == 0)
8136 update_modkey(XCB_MOD_MASK_3);
8137 else if (strncasecmp(value, "Mod4", strlen("Mod4")) == 0)
8138 update_modkey(XCB_MOD_MASK_4);
8145 setconfcolor(const char *selector, const char *value, int flags)
8147 int first, last, i = 0, num_screens;
8149 num_screens = get_screen_count();
8151 /* conf screen indices begin at 1; treat vals <= 0 as 'all screens.' */
8152 if (selector == NULL || strlen(selector) == 0 ||
8153 (last = atoi(selector) - 1) < 0) {
8155 last = num_screens - 1;
8160 if (last >= num_screens) {
8161 add_startup_exception("invalid screen index: %d out of bounds "
8162 "(maximum %d)", last + 1, num_screens);
8166 for (i = first; i <= last; ++i) {
8167 setscreencolor(value, i, flags);
8170 * When setting focus/unfocus colors, we need to also
8171 * set maximize colors to match if they haven't been customized.
8173 if (flags == SWM_S_COLOR_FOCUS &&
8174 !screens[i].c[SWM_S_COLOR_FOCUS_MAXIMIZED].manual)
8175 setscreencolor(value, i, SWM_S_COLOR_FOCUS_MAXIMIZED);
8176 else if (flags == SWM_S_COLOR_UNFOCUS &&
8177 !screens[i].c[SWM_S_COLOR_UNFOCUS_MAXIMIZED].manual)
8178 setscreencolor(value, i, SWM_S_COLOR_UNFOCUS_MAXIMIZED);
8180 screens[i].c[flags].manual = 1;
8187 setconfregion(const char *selector, const char *value, int flags)
8189 /* suppress unused warnings since vars are needed */
8193 custom_region(value);
8198 setautorun(const char *selector, const char *value, int flags)
8208 /* suppress unused warnings since vars are needed */
8212 if (getenv("SWM_STARTED"))
8216 if (sscanf(value, "ws[%d]:%1023c", &ws_id, s) != 2)
8217 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
8219 if (ws_id < 0 || ws_id >= workspace_limit)
8220 errx(1, "autorun: invalid workspace %d", ws_id + 1);
8222 sp = expand_tilde((char *)&s);
8225 * This is a little intricate
8227 * If the pid already exists we simply reuse it because it means it was
8228 * used before AND not claimed by manage_window. We get away with
8229 * altering it in the parent after INSERT because this can not be a race
8232 while ((ap = strsep(&sp, " \t")) != NULL) {
8235 DNPRINTF(SWM_D_SPAWN, "setautorun: arg [%s]\n", ap);
8237 if ((a.argv = realloc(a.argv, argc * sizeof(char *))) == NULL)
8238 err(1, "setautorun: realloc");
8239 a.argv[argc - 1] = ap;
8243 if ((a.argv = realloc(a.argv, (argc + 1) * sizeof(char *))) == NULL)
8244 err(1, "setautorun: realloc");
8245 a.argv[argc] = NULL;
8247 if ((pid = fork()) == 0) {
8248 spawn(ws_id, &a, true);
8257 p = calloc(1, sizeof *p);
8260 TAILQ_INSERT_TAIL(&pidlist, p, entry);
8270 setlayout(const char *selector, const char *value, int flags)
8272 struct workspace *ws;
8273 int ws_id, i, x, mg, ma, si, ar;
8274 int st = SWM_V_STACK, num_screens;
8278 /* suppress unused warnings since vars are needed */
8282 if (getenv("SWM_STARTED"))
8286 if (sscanf(value, "ws[%d]:%d:%d:%d:%d:%1023c",
8287 &ws_id, &mg, &ma, &si, &ar, s) != 6)
8288 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
8289 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
8292 if (ws_id < 0 || ws_id >= workspace_limit)
8293 errx(1, "layout: invalid workspace %d", ws_id + 1);
8295 if (strcasecmp(s, "vertical") == 0)
8297 else if (strcasecmp(s, "vertical_flip") == 0) {
8300 } else if (strcasecmp(s, "horizontal") == 0)
8302 else if (strcasecmp(s, "horizontal_flip") == 0) {
8305 } else if (strcasecmp(s, "fullscreen") == 0)
8308 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
8309 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
8312 num_screens = get_screen_count();
8313 for (i = 0; i < num_screens; i++) {
8314 ws = (struct workspace *)&screens[i].ws;
8315 ws[ws_id].cur_layout = &layouts[st];
8317 ws[ws_id].always_raise = (ar != 0);
8318 if (st == SWM_MAX_STACK)
8322 for (x = 0; x < abs(mg); x++) {
8323 ws[ws_id].cur_layout->l_config(&ws[ws_id],
8324 mg >= 0 ? SWM_ARG_ID_MASTERGROW :
8325 SWM_ARG_ID_MASTERSHRINK);
8328 for (x = 0; x < abs(ma); x++) {
8329 ws[ws_id].cur_layout->l_config(&ws[ws_id],
8330 ma >= 0 ? SWM_ARG_ID_MASTERADD :
8331 SWM_ARG_ID_MASTERDEL);
8334 for (x = 0; x < abs(si); x++) {
8335 ws[ws_id].cur_layout->l_config(&ws[ws_id],
8336 si >= 0 ? SWM_ARG_ID_STACKINC :
8337 SWM_ARG_ID_STACKDEC);
8341 ws[ws_id].cur_layout->l_config(&ws[ws_id],
8342 SWM_ARG_ID_FLIPLAYOUT);
8349 /* config options */
8350 struct config_option {
8352 int (*func)(const char*, const char*, int);
8355 struct config_option configopt[] = {
8356 { "autorun", setautorun, 0 },
8357 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
8358 { "bar_at_bottom", setconfvalue, SWM_S_BAR_AT_BOTTOM },
8359 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
8360 { "bar_border_unfocus", setconfcolor, SWM_S_COLOR_BAR_BORDER_UNFOCUS },
8361 { "bar_border_width", setconfvalue, SWM_S_BAR_BORDER_WIDTH },
8362 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
8363 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
8364 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
8365 { "bar_enabled_ws", setconfvalue, SWM_S_BAR_ENABLED_WS },
8366 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
8367 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
8368 { "bar_format", setconfvalue, SWM_S_BAR_FORMAT },
8369 { "bar_justify", setconfvalue, SWM_S_BAR_JUSTIFY },
8370 { "bind", setconfbinding, 0 },
8371 { "border_width", setconfvalue, SWM_S_BORDER_WIDTH },
8372 { "boundary_width", setconfvalue, SWM_S_BOUNDARY_WIDTH },
8373 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
8374 { "clock_format", setconfvalue, SWM_S_CLOCK_FORMAT },
8375 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
8376 { "color_focus_maximized", setconfcolor, SWM_S_COLOR_FOCUS_MAXIMIZED },
8377 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
8378 { "color_unfocus_maximized", setconfcolor, SWM_S_COLOR_UNFOCUS_MAXIMIZED },
8379 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
8380 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
8381 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
8382 { "disable_border", setconfvalue, SWM_S_DISABLE_BORDER },
8383 { "focus_close", setconfvalue, SWM_S_FOCUS_CLOSE },
8384 { "focus_close_wrap", setconfvalue, SWM_S_FOCUS_CLOSE_WRAP },
8385 { "focus_default", setconfvalue, SWM_S_FOCUS_DEFAULT },
8386 { "focus_mode", setconfvalue, SWM_S_FOCUS_MODE },
8387 { "iconic_enabled", setconfvalue, SWM_S_ICONIC_ENABLED },
8388 { "java_workaround", setconfvalue, SWM_S_JAVA_WORKAROUND },
8389 { "keyboard_mapping", setkeymapping, 0 },
8390 { "layout", setlayout, 0 },
8391 { "modkey", setconfmodkey, 0 },
8392 { "program", setconfspawn, 0 },
8393 { "quirk", setconfquirk, 0 },
8394 { "region", setconfregion, 0 },
8395 { "region_padding", setconfvalue, SWM_S_REGION_PADDING },
8396 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
8397 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
8398 { "spawn_position", setconfvalue, SWM_S_SPAWN_ORDER },
8399 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
8400 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
8401 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
8402 { "tile_gap", setconfvalue, SWM_S_TILE_GAP },
8403 { "title_class_enabled", setconfvalue, SWM_S_WINDOW_CLASS_ENABLED }, /* For backwards compat. */
8404 { "title_name_enabled", setconfvalue, SWM_S_WINDOW_INSTANCE_ENABLED }, /* For backwards compat. */
8405 { "urgent_enabled", setconfvalue, SWM_S_URGENT_ENABLED },
8406 { "verbose_layout", setconfvalue, SWM_S_VERBOSE_LAYOUT },
8407 { "window_class_enabled", setconfvalue, SWM_S_WINDOW_CLASS_ENABLED },
8408 { "window_instance_enabled", setconfvalue, SWM_S_WINDOW_INSTANCE_ENABLED },
8409 { "window_name_enabled", setconfvalue, SWM_S_WINDOW_NAME_ENABLED },
8410 { "workspace_limit", setconfvalue, SWM_S_WORKSPACE_LIMIT },
8411 { "name", setconfvalue, SWM_S_WORKSPACE_NAME },
8415 _add_startup_exception(const char *fmt, va_list ap)
8417 if (vasprintf(&startup_exception, fmt, ap) == -1)
8418 warn("%s: asprintf", __func__);
8422 add_startup_exception(const char *fmt, ...)
8428 if (startup_exception)
8431 /* force bar to be enabled due to exception */
8435 _add_startup_exception(fmt, ap);
8440 conf_load(const char *filename, int keymapping)
8443 char *line = NULL, *cp, *ce, *optsub, *optval = NULL;
8444 size_t linelen, lineno = 0;
8445 int wordlen, i, optidx, count;
8446 struct config_option *opt = NULL;
8448 DNPRINTF(SWM_D_CONF, "conf_load: begin\n");
8450 if (filename == NULL) {
8451 warnx("conf_load: no filename");
8455 DNPRINTF(SWM_D_CONF, "conf_load: open %s\n", filename);
8457 if ((config = fopen(filename, "r")) == NULL) {
8458 warn("conf_load: fopen: %s", filename);
8462 while (!feof(config)) {
8466 if ((line = fparseln(config, &linelen, &lineno, NULL,
8467 FPARSELN_UNESCCOMM | FPARSELN_UNESCCONT)) == NULL) {
8469 err(1, "%s", filename);
8474 cp += strspn(cp, " \t\n"); /* eat whitespace */
8475 if (cp[0] == '\0') {
8479 /* get config option */
8480 wordlen = strcspn(cp, "=[ \t\n");
8482 add_startup_exception("%s: line %zd: no option found",
8487 for (i = 0; i < LENGTH(configopt); i++) {
8488 opt = &configopt[i];
8489 if (strncasecmp(cp, opt->optname, wordlen) == 0 &&
8490 (int)strlen(opt->optname) == wordlen) {
8496 add_startup_exception("%s: line %zd: unknown option "
8497 "%.*s", filename, lineno, wordlen, cp);
8500 if (keymapping && opt && strcmp(opt->optname, "bind")) {
8501 add_startup_exception("%s: line %zd: invalid option "
8502 "%.*s", filename, lineno, wordlen, cp);
8506 cp += strspn(cp, " \t\n"); /* eat whitespace */
8508 /* get [selector] if any */
8513 /* Get length of selector. */
8514 for (ce = ++cp; *ce != '\0'; ++ce) {
8515 /* Find matching (not escaped) bracket. */
8516 if (*ce == ']' && *(ce - 1) != '\\') {
8523 add_startup_exception("%s: line %zd: syntax "
8524 "error: unterminated selector", filename,
8529 /* ce points at ']'; terminate optsub. */
8534 cp += strspn(cp, "= \t\n"); /* eat trailing */
8537 if (strlen(optval) == 0) {
8538 add_startup_exception("%s: line %zd: must supply value "
8539 "to %s", filename, lineno,
8540 configopt[optidx].optname);
8543 /* call function to deal with it all */
8544 if (configopt[optidx].func(optsub, optval,
8545 configopt[optidx].funcflags) != 0) {
8546 add_startup_exception("%s: line %zd: invalid data for "
8547 "%s", filename, lineno, configopt[optidx].optname);
8555 DNPRINTF(SWM_D_CONF, "conf_load: end\n");
8561 set_child_transient(struct ws_win *win, xcb_window_t *trans)
8563 struct ws_win *parent, *w;
8564 struct swm_region *r;
8565 struct workspace *ws;
8566 xcb_icccm_wm_hints_t wmh;
8568 parent = find_window(win->transient);
8570 parent->focus_child = win;
8572 DNPRINTF(SWM_D_MISC, "set_child_transient: parent doesn't exist"
8573 " for %#x trans %#x\n", win->id, win->transient);
8575 r = root_to_region(win->s->root, SWM_CK_ALL);
8577 /* parent doen't exist in our window list */
8578 TAILQ_FOREACH(w, &ws->winlist, entry) {
8579 if (xcb_icccm_get_wm_hints_reply(conn,
8580 xcb_icccm_get_wm_hints(conn, w->id),
8582 warnx("can't get hints for %#x", w->id);
8586 if (win->hints.window_group != wmh.window_group)
8589 w->focus_child = win;
8590 win->transient = w->id;
8592 DNPRINTF(SWM_D_MISC, "set_child_transient: adjusting "
8593 "transient to %#x\n", win->transient);
8600 window_get_pid(xcb_window_t win)
8605 xcb_get_property_cookie_t pc;
8606 xcb_get_property_reply_t *pr;
8608 apid = get_atom_from_string("_NET_WM_PID");
8609 if (apid == XCB_ATOM_NONE)
8612 pc = xcb_get_property(conn, 0, win, apid, XCB_ATOM_CARDINAL, 0, 1);
8613 pr = xcb_get_property_reply(conn, pc, NULL);
8616 if (pr->type != XCB_ATOM_CARDINAL) {
8621 if (pr->type == apid && pr->format == 32)
8622 ret = *((pid_t *)xcb_get_property_value(pr));
8628 apid = get_atom_from_string("_SWM_PID");
8629 pc = xcb_get_property(conn, 0, win, apid, XCB_ATOM_STRING,
8631 pr = xcb_get_property_reply(conn, pc, NULL);
8634 if (pr->type != apid) {
8639 ret = (pid_t)strtonum(xcb_get_property_value(pr), 0, INT_MAX, &errstr);
8646 get_swm_ws(xcb_window_t id)
8652 xcb_get_property_reply_t *gpr;
8654 gpr = xcb_get_property_reply(conn,
8655 xcb_get_property(conn, 0, id, a_swm_ws,
8656 XCB_ATOM_STRING, 0, SWM_PROPLEN),
8661 proplen = xcb_get_property_value_length(gpr);
8663 prop = malloc(proplen + 1);
8666 xcb_get_property_value(gpr),
8668 prop[proplen] = '\0';
8675 DNPRINTF(SWM_D_PROP, "get_swm_ws: _SWM_WS: %s\n", prop);
8676 ws_idx = (int)strtonum(prop, 0, workspace_limit - 1, &errstr);
8678 DNPRINTF(SWM_D_PROP, "get_swm_ws: win #%s: %s",
8688 get_ws_idx(struct ws_win *win)
8690 xcb_get_property_reply_t *gpr;
8696 gpr = xcb_get_property_reply(conn,
8697 xcb_get_property(conn, 0, win->id, ewmh[_NET_WM_DESKTOP].atom,
8698 XCB_ATOM_CARDINAL, 0, 1),
8701 if (gpr->type == XCB_ATOM_CARDINAL && gpr->format == 32)
8702 ws_idx = *((int *)xcb_get_property_value(gpr));
8706 if (ws_idx == -1 && !(win->quirks & SWM_Q_IGNORESPAWNWS))
8707 ws_idx = get_swm_ws(win->id);
8709 if (ws_idx > workspace_limit - 1 || ws_idx < -1)
8712 DNPRINTF(SWM_D_PROP, "get_ws_idx: win %#x, ws_idx: %d\n", win->id,
8719 manage_window(xcb_window_t id, int spawn_pos, bool mapped)
8721 struct ws_win *win, *ww;
8722 struct swm_region *r;
8725 xcb_get_geometry_reply_t *gr;
8726 xcb_window_t trans = XCB_WINDOW_NONE;
8727 uint32_t i, wa[2], new_flags;
8729 char *class, *instance, *name;
8731 if ((win = find_window(id)) != NULL) {
8732 DNPRINTF(SWM_D_MISC, "manage_window: win %#x already "
8733 "managed; skipping.)\n", id);
8734 return (win); /* Already managed. */
8737 /* See if window is on the unmanaged list. */
8738 if ((win = find_unmanaged_window(id)) != NULL) {
8739 DNPRINTF(SWM_D_MISC, "manage_window: win %#x found on "
8740 "unmanaged list.\n", id);
8741 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
8744 set_child_transient(win, &trans);
8748 DNPRINTF(SWM_D_MISC, "manage_window: win %#x is new.\n", id);
8751 /* Try to get initial window geometry. */
8752 gr = xcb_get_geometry_reply(conn, xcb_get_geometry(conn, id), NULL);
8754 DNPRINTF(SWM_D_MISC, "manage_window: get geometry failed.\n");
8758 /* Create and initialize ws_win object. */
8759 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
8760 err(1, "manage_window: calloc: failed to allocate memory for "
8765 /* Figureout which region the window belongs to. */
8766 r = root_to_region(gr->root, SWM_CK_ALL);
8768 /* Ignore window border if there is one. */
8769 WIDTH(win) = gr->width;
8770 HEIGHT(win) = gr->height;
8771 X(win) = gr->x + gr->border_width - border_width;
8772 Y(win) = gr->y + gr->border_width - border_width;
8773 win->bordered = true;
8774 win->mapped = mapped;
8775 win->s = r->s; /* this never changes */
8779 /* Select which X events to monitor and set border pixel color. */
8780 wa[0] = win->s->c[SWM_S_COLOR_UNFOCUS].pixel;
8781 wa[1] = XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_PROPERTY_CHANGE |
8782 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
8784 wa[1] |= XCB_EVENT_MASK_LEAVE_WINDOW | XCB_EVENT_MASK_FOCUS_CHANGE;
8787 xcb_change_window_attributes(conn, win->id, XCB_CW_BORDER_PIXEL |
8788 XCB_CW_EVENT_MASK, wa);
8790 /* Get WM_SIZE_HINTS. */
8791 xcb_icccm_get_wm_normal_hints_reply(conn,
8792 xcb_icccm_get_wm_normal_hints(conn, win->id),
8796 xcb_icccm_get_wm_hints_reply(conn,
8797 xcb_icccm_get_wm_hints(conn, win->id),
8800 /* Get WM_TRANSIENT_FOR; see if window is a transient. */
8801 xcb_icccm_get_wm_transient_for_reply(conn,
8802 xcb_icccm_get_wm_transient_for(conn, win->id),
8805 win->transient = trans;
8806 set_child_transient(win, &win->transient);
8809 /* Get WM_PROTOCOLS. */
8810 get_wm_protocols(win);
8812 /* Set initial quirks based on EWMH. */
8813 ewmh_autoquirk(win);
8815 /* Determine initial quirks. */
8816 xcb_icccm_get_wm_class_reply(conn,
8817 xcb_icccm_get_wm_class(conn, win->id),
8820 class = win->ch.class_name ? win->ch.class_name : "";
8821 instance = win->ch.instance_name ? win->ch.instance_name : "";
8822 name = get_win_name(win->id);
8824 DNPRINTF(SWM_D_CLASS, "manage_window: class: %s, instance: %s, "
8825 "name: %s\n", class, instance, name);
8827 /* java is retarded so treat it special */
8828 if (strstr(instance, "sun-awt")) {
8829 DNPRINTF(SWM_D_CLASS, "manage_window: java window detected.\n");
8833 TAILQ_FOREACH(qp, &quirks, entry) {
8834 if (regexec(&qp->regex_class, class, 0, NULL, 0) == 0 &&
8835 regexec(&qp->regex_instance, instance, 0, NULL, 0) == 0 &&
8836 regexec(&qp->regex_name, name, 0, NULL, 0) == 0) {
8837 DNPRINTF(SWM_D_CLASS, "manage_window: matched "
8838 "quirk: %s:%s:%s mask: %#lx\n", qp->class,
8839 qp->instance, qp->name, qp->quirk);
8840 win->quirks = qp->quirk;
8846 /* Reset font sizes (the bruteforce way; no default keybinding). */
8847 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
8848 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
8849 fake_keypress(win, XK_KP_Subtract, XCB_MOD_MASK_SHIFT);
8850 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
8851 fake_keypress(win, XK_KP_Add, XCB_MOD_MASK_SHIFT);
8854 /* Figure out which workspace the window belongs to. */
8855 if (!(win->quirks & SWM_Q_IGNOREPID) &&
8856 (p = find_pid(window_get_pid(win->id))) != NULL) {
8857 win->ws = &r->s->ws[p->ws];
8858 TAILQ_REMOVE(&pidlist, p, entry);
8861 } else if ((ws_idx = get_ws_idx(win)) != -1 &&
8863 /* _SWM_WS is set; use that. */
8864 win->ws = &r->s->ws[ws_idx];
8865 } else if (trans && (ww = find_window(trans)) != NULL) {
8866 /* Launch transients in the same ws as parent. */
8872 /* Set the _NET_WM_DESKTOP atom. */
8873 DNPRINTF(SWM_D_PROP, "manage_window: set _NET_WM_DESKTOP: %d\n",
8875 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
8876 ewmh[_NET_WM_DESKTOP].atom, XCB_ATOM_CARDINAL, 32, 1, &win->ws->idx);
8878 /* Remove any _SWM_WS now that we set _NET_WM_DESKTOP. */
8879 xcb_delete_property(conn, win->id, a_swm_ws);
8881 /* WS must already be set for this to work. */
8882 store_float_geom(win);
8884 /* Make sure window is positioned inside its region, if its active. */
8886 region_containment(win, r, SWM_CW_ALLSIDES |
8887 SWM_CW_HARDBOUNDARY);
8892 /* Figure out where to stack the window in the workspace. */
8893 if (trans && (ww = find_window(trans)))
8894 TAILQ_INSERT_AFTER(&win->ws->winlist, ww, win, entry);
8895 else if (win->ws->focus && spawn_pos == SWM_STACK_ABOVE)
8896 TAILQ_INSERT_AFTER(&win->ws->winlist, win->ws->focus, win,
8898 else if (win->ws->focus && spawn_pos == SWM_STACK_BELOW)
8899 TAILQ_INSERT_BEFORE(win->ws->focus, win, entry);
8900 else switch (spawn_pos) {
8903 case SWM_STACK_ABOVE:
8904 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
8906 case SWM_STACK_BOTTOM:
8907 case SWM_STACK_BELOW:
8908 TAILQ_INSERT_HEAD(&win->ws->winlist, win, entry);
8911 ewmh_update_client_list();
8913 TAILQ_INSERT_TAIL(&win->ws->stack, win, stack_entry);
8915 /* Get/apply initial _NET_WM_STATE */
8916 ewmh_get_wm_state(win);
8919 new_flags = win->ewmh_flags;
8921 if (win->quirks & SWM_Q_FLOAT)
8922 new_flags |= EWMH_F_ABOVE;
8924 if (win->quirks & SWM_Q_ANYWHERE)
8925 new_flags |= SWM_F_MANUAL;
8927 ewmh_apply_flags(win, new_flags);
8928 ewmh_update_wm_state(win);
8930 /* Set initial _NET_WM_ALLOWED_ACTIONS */
8931 ewmh_update_actions(win);
8935 DNPRINTF(SWM_D_MISC, "manage_window: done. win %#x, (x,y) w x h: "
8936 "(%d,%d) %d x %d, ws: %d, iconic: %s, transient: %#x\n", win->id,
8937 X(win), Y(win), WIDTH(win), HEIGHT(win), win->ws->idx,
8938 YESNO(ICONIC(win)), win->transient);
8944 free_window(struct ws_win *win)
8946 DNPRINTF(SWM_D_MISC, "free_window: win %#x\n", win->id);
8951 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
8953 xcb_icccm_get_wm_class_reply_wipe(&win->ch);
8958 memset(win, 0xff, sizeof *win); /* XXX kill later */
8961 DNPRINTF(SWM_D_MISC, "free_window: done.\n");
8965 unmanage_window(struct ws_win *win)
8967 struct ws_win *parent;
8972 DNPRINTF(SWM_D_MISC, "unmanage_window: win %#x\n", win->id);
8975 parent = find_window(win->transient);
8977 parent->focus_child = NULL;
8980 TAILQ_REMOVE(&win->ws->stack, win, stack_entry);
8981 TAILQ_REMOVE(&win->ws->winlist, win, entry);
8982 TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
8984 ewmh_update_client_list();
8988 expose(xcb_expose_event_t *e)
8991 struct swm_region *r;
8993 DNPRINTF(SWM_D_EVENT, "expose: win %#x\n", e->window);
8995 num_screens = get_screen_count();
8996 for (i = 0; i < num_screens; i++)
8997 TAILQ_FOREACH(r, &screens[i].rl, entry)
8998 if (e->window == WINID(r->bar))
9006 focusin(xcb_focus_in_event_t *e)
9008 DNPRINTF(SWM_D_EVENT, "focusin: win %#x, mode: %s(%u), "
9009 "detail: %s(%u)\n", e->event, get_notify_mode_label(e->mode),
9010 e->mode, get_notify_detail_label(e->detail), e->detail);
9014 focusout(xcb_focus_out_event_t *e)
9016 DNPRINTF(SWM_D_EVENT, "focusout: win %#x, mode: %s(%u), "
9017 "detail: %s(%u)\n", e->event, get_notify_mode_label(e->mode),
9018 e->mode, get_notify_detail_label(e->detail), e->detail);
9023 keypress(xcb_key_press_event_t *e)
9025 xcb_keysym_t keysym;
9028 keysym = xcb_key_press_lookup_keysym(syms, e, 0);
9030 DNPRINTF(SWM_D_EVENT, "keypress: keysym: %u, win (x,y): %#x (%d,%d), "
9031 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
9032 "state: %u, same_screen: %s\n", keysym, e->event, e->event_x,
9033 e->event_y, e->detail, e->time, e->root, e->root_x, e->root_y,
9034 e->child, e->state, YESNO(e->same_screen));
9036 if ((kp = key_lookup(CLEANMASK(e->state), keysym)) == NULL)
9039 last_event_time = e->time;
9041 if (kp->funcid == KF_SPAWN_CUSTOM)
9042 spawn_custom(root_to_region(e->root, SWM_CK_ALL),
9043 &(keyfuncs[kp->funcid].args), kp->spawn_name);
9044 else if (keyfuncs[kp->funcid].func)
9045 keyfuncs[kp->funcid].func(root_to_region(e->root, SWM_CK_ALL),
9046 &(keyfuncs[kp->funcid].args));
9049 /* Unfreeze grab events. */
9050 xcb_allow_events(conn, XCB_ALLOW_ASYNC_KEYBOARD, e->time);
9053 DNPRINTF(SWM_D_EVENT, "keypress: done.\n");
9057 buttonpress(xcb_button_press_event_t *e)
9059 struct ws_win *win = NULL;
9060 struct swm_region *r, *old_r;
9062 bool handled = false;
9064 DNPRINTF(SWM_D_EVENT, "buttonpress: win (x,y): %#x (%d,%d), "
9065 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
9066 "state: %u, same_screen: %s\n", e->event, e->event_x, e->event_y,
9067 e->detail, e->time, e->root, e->root_x, e->root_y, e->child,
9068 e->state, YESNO(e->same_screen));
9070 if (e->event == e->root) {
9071 if (e->child != 0) {
9072 win = find_window(e->child);
9073 /* Pass ButtonPress to window if it isn't managed. */
9077 /* Focus on empty region */
9078 /* If no windows on region if its empty. */
9079 r = root_to_region(e->root, SWM_CK_POINTER);
9081 DNPRINTF(SWM_D_EVENT, "buttonpress: "
9082 "NULL region; ignoring.\n");
9086 if (TAILQ_EMPTY(&r->ws->winlist)) {
9087 old_r = root_to_region(e->root, SWM_CK_FOCUS);
9088 if (old_r && old_r != r)
9089 unfocus_win(old_r->ws->focus);
9091 xcb_set_input_focus(conn,
9092 XCB_INPUT_FOCUS_PARENT, e->root, e->time);
9094 /* Clear bar since empty. */
9102 win = find_window(e->event);
9108 last_event_time = e->time;
9110 focus_win(get_focus_magic(win));
9112 for (i = 0; i < LENGTH(buttons); i++)
9113 if (client_click == buttons[i].action && buttons[i].func &&
9114 buttons[i].button == e->detail &&
9115 CLEANMASK(buttons[i].mask) == CLEANMASK(e->state)) {
9116 buttons[i].func(win, &buttons[i].args);
9122 DNPRINTF(SWM_D_EVENT, "buttonpress: passing to window.\n");
9123 /* Replay event to event window */
9124 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, e->time);
9126 DNPRINTF(SWM_D_EVENT, "buttonpress: handled.\n");
9127 /* Unfreeze grab events. */
9128 xcb_allow_events(conn, XCB_ALLOW_SYNC_POINTER, e->time);
9136 print_win_geom(xcb_window_t w)
9138 xcb_get_geometry_reply_t *wa;
9140 wa = xcb_get_geometry_reply(conn, xcb_get_geometry(conn, w), NULL);
9142 DNPRINTF(SWM_D_MISC, "print_win_geom: window not found: %#x\n",
9147 DNPRINTF(SWM_D_MISC, "print_win_geom: win %#x, root: %#x, "
9148 "depth: %u, (x,y) w x h: (%d,%d) %d x %d, border: %d\n",
9149 w, wa->root, wa->depth, wa->x, wa->y, wa->width, wa->height,
9158 get_stack_mode_name(uint8_t mode)
9163 case XCB_STACK_MODE_ABOVE:
9166 case XCB_STACK_MODE_BELOW:
9169 case XCB_STACK_MODE_TOP_IF:
9172 case XCB_STACK_MODE_BOTTOM_IF:
9175 case XCB_STACK_MODE_OPPOSITE:
9187 configurerequest(xcb_configure_request_event_t *e)
9190 struct swm_region *r = NULL;
9192 uint32_t wc[7] = {0};
9196 if ((win = find_window(e->window)) == NULL)
9197 if ((win = find_unmanaged_window(e->window)) == NULL)
9201 if (swm_debug & SWM_D_EVENT) {
9202 print_win_geom(e->window);
9204 DNPRINTF(SWM_D_EVENT, "configurerequest: win %#x, "
9205 "parent: %#x, new: %s, value_mask: %u { ", e->window,
9206 e->parent, YESNO(new), e->value_mask);
9207 if (e->value_mask & XCB_CONFIG_WINDOW_X)
9208 DPRINTF("X: %d ", e->x);
9209 if (e->value_mask & XCB_CONFIG_WINDOW_Y)
9210 DPRINTF("Y: %d ", e->y);
9211 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH)
9212 DPRINTF("W: %u ", e->width);
9213 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
9214 DPRINTF("H: %u ", e->height);
9215 if (e->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH)
9216 DPRINTF("Border: %u ", e->border_width);
9217 if (e->value_mask & XCB_CONFIG_WINDOW_SIBLING)
9218 DPRINTF("Sibling: %#x ", e->sibling);
9219 if (e->value_mask & XCB_CONFIG_WINDOW_STACK_MODE)
9220 DPRINTF("StackMode: %s(%u) ",
9221 get_stack_mode_name(e->stack_mode), e->stack_mode);
9227 if (e->value_mask & XCB_CONFIG_WINDOW_X) {
9228 mask |= XCB_CONFIG_WINDOW_X;
9231 if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
9232 mask |= XCB_CONFIG_WINDOW_Y;
9235 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
9236 mask |= XCB_CONFIG_WINDOW_WIDTH;
9239 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
9240 mask |= XCB_CONFIG_WINDOW_HEIGHT;
9241 wc[i++] = e->height;
9243 if (e->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH) {
9244 mask |= XCB_CONFIG_WINDOW_BORDER_WIDTH;
9245 wc[i++] = e->border_width;
9247 if (e->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
9248 mask |= XCB_CONFIG_WINDOW_SIBLING;
9249 wc[i++] = e->sibling;
9251 if (e->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
9252 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
9253 wc[i++] = e->stack_mode;
9257 xcb_configure_window(conn, e->window, mask, wc);
9260 } else if ((!MANUAL(win) || win->quirks & SWM_Q_ANYWHERE) &&
9261 !FULLSCREEN(win) && !MAXIMIZED(win)) {
9264 else if (win->ws->old_r)
9267 /* windows are centered unless ANYWHERE quirk is set. */
9268 if (win->quirks & SWM_Q_ANYWHERE) {
9269 if (e->value_mask & XCB_CONFIG_WINDOW_X) {
9270 win->g_float.x = e->x;
9272 win->g_float.x -= X(r);
9275 if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
9276 win->g_float.y = e->y;
9278 win->g_float.y -= Y(r);
9282 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH)
9283 win->g_float.w = e->width;
9285 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
9286 win->g_float.h = e->height;
9288 win->g_floatvalid = true;
9290 if (!MAXIMIZED(win) && !FULLSCREEN(win) &&
9291 (TRANS(win) || (ABOVE(win) &&
9292 win->ws->cur_layout != &layouts[SWM_MAX_STACK]))) {
9293 WIDTH(win) = win->g_float.w;
9294 HEIGHT(win) = win->g_float.h;
9297 update_floater(win);
9312 DNPRINTF(SWM_D_EVENT, "configurerequest: done.\n");
9316 configurenotify(xcb_configure_notify_event_t *e)
9320 DNPRINTF(SWM_D_EVENT, "configurenotify: win %#x, event win: %#x, "
9321 "(x,y) WxH: (%d,%d) %ux%u, border: %u, above_sibling: %#x, "
9322 "override_redirect: %s\n", e->window, e->event, e->x, e->y,
9323 e->width, e->height, e->border_width, e->above_sibling,
9324 YESNO(e->override_redirect));
9326 win = find_window(e->window);
9328 xcb_icccm_get_wm_normal_hints_reply(conn,
9329 xcb_icccm_get_wm_normal_hints(conn, win->id),
9332 if (font_adjusted) {
9340 destroynotify(xcb_destroy_notify_event_t *e)
9344 DNPRINTF(SWM_D_EVENT, "destroynotify: win %#x\n", e->window);
9346 if ((win = find_window(e->window)) == NULL) {
9347 if ((win = find_unmanaged_window(e->window)) == NULL)
9353 if (focus_mode != SWM_FOCUS_FOLLOW) {
9354 /* If we were focused, make sure we focus on something else. */
9355 if (win == win->ws->focus)
9356 win->ws->focus_pending = get_focus_prev(win);
9359 unmanage_window(win);
9362 if (focus_mode != SWM_FOCUS_FOLLOW && WS_FOCUSED(win->ws)) {
9363 if (win->ws->focus_pending) {
9364 focus_win(win->ws->focus_pending);
9365 win->ws->focus_pending = NULL;
9366 } else if (win == win->ws->focus) {
9367 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT,
9368 win->ws->r->id, XCB_CURRENT_TIME);
9379 get_notify_detail_label(uint8_t detail)
9384 case XCB_NOTIFY_DETAIL_ANCESTOR:
9387 case XCB_NOTIFY_DETAIL_VIRTUAL:
9390 case XCB_NOTIFY_DETAIL_INFERIOR:
9393 case XCB_NOTIFY_DETAIL_NONLINEAR:
9394 label = "Nonlinear";
9396 case XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL:
9397 label = "NonlinearVirtual";
9399 case XCB_NOTIFY_DETAIL_POINTER:
9402 case XCB_NOTIFY_DETAIL_POINTER_ROOT:
9403 label = "PointerRoot";
9405 case XCB_NOTIFY_DETAIL_NONE:
9416 get_notify_mode_label(uint8_t mode)
9421 case XCB_NOTIFY_MODE_NORMAL:
9424 case XCB_NOTIFY_MODE_GRAB:
9427 case XCB_NOTIFY_MODE_UNGRAB:
9430 case XCB_NOTIFY_MODE_WHILE_GRABBED:
9431 label = "WhileGrabbed";
9442 enternotify(xcb_enter_notify_event_t *e)
9445 struct swm_region *r;
9447 DNPRINTF(SWM_D_FOCUS, "enternotify: time: %u, win (x,y): %#x "
9448 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
9449 "child: %#x, same_screen_focus: %s, state: %d\n",
9450 e->time, e->event, e->event_x, e->event_y,
9451 get_notify_mode_label(e->mode), e->mode,
9452 get_notify_detail_label(e->detail), e->detail,
9453 e->root, e->root_x, e->root_y, e->child,
9454 YESNO(e->same_screen_focus), e->state);
9456 if (focus_mode == SWM_FOCUS_MANUAL &&
9457 e->mode == XCB_NOTIFY_MODE_NORMAL) {
9458 DNPRINTF(SWM_D_EVENT, "enternotify: manual focus; ignoring.\n");
9462 last_event_time = e->time;
9464 if ((win = find_window(e->event)) == NULL) {
9465 if (e->event == e->root) {
9466 /* If no windows on pointer region, then focus root. */
9467 r = root_to_region(e->root, SWM_CK_POINTER);
9469 DNPRINTF(SWM_D_EVENT, "enternotify: "
9470 "NULL region; ignoring.\n");
9476 DNPRINTF(SWM_D_EVENT, "enternotify: window is NULL; "
9481 if (e->mode == XCB_NOTIFY_MODE_NORMAL &&
9482 e->detail == XCB_NOTIFY_DETAIL_INFERIOR) {
9483 DNPRINTF(SWM_D_EVENT, "enternotify: entering from "
9484 "inferior; ignoring\n");
9488 focus_win(get_focus_magic(win));
9496 leavenotify(xcb_leave_notify_event_t *e)
9498 DNPRINTF(SWM_D_FOCUS, "leavenotify: time: %u, win (x,y): %#x "
9499 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
9500 "child: %#x, same_screen_focus: %s, state: %d\n",
9501 e->time, e->event, e->event_x, e->event_y,
9502 get_notify_mode_label(e->mode), e->mode,
9503 get_notify_detail_label(e->detail), e->detail,
9504 e->root, e->root_x, e->root_y, e->child,
9505 YESNO(e->same_screen_focus), e->state);
9510 mapnotify(xcb_map_notify_event_t *e)
9512 struct ws_win *win, *parent = NULL;
9513 struct workspace *ws;
9515 DNPRINTF(SWM_D_EVENT, "mapnotify: win %#x\n", e->window);
9517 if ((win = manage_window(e->window, spawn_position, true)) == NULL)
9521 /* Need to know if win was mapped due to ws switch. */
9522 if (ws->state == SWM_WS_STATE_MAPPED) {
9523 if (ws->focus_pending && TRANS(ws->focus_pending))
9524 parent = find_window(win->transient);
9526 /* If window's parent is maximized, don't clear it. */
9527 if ((parent == NULL) || !MAXIMIZED(parent))
9528 if (clear_maximized(ws) > 0)
9533 set_win_state(win, XCB_ICCCM_WM_STATE_NORMAL);
9535 if (focus_mode != SWM_FOCUS_FOLLOW && WS_FOCUSED(win->ws)) {
9536 if (ws->focus_pending == win) {
9538 ws->focus_pending = NULL;
9547 mappingnotify(xcb_mapping_notify_event_t *e)
9550 int i, j, num_screens;
9552 xcb_refresh_keyboard_mapping(syms, e);
9554 if (e->request == XCB_MAPPING_KEYBOARD) {
9557 /* Regrab buttons on all managed windows. */
9558 num_screens = get_screen_count();
9559 for (i = 0; i < num_screens; i++)
9560 for (j = 0; j < workspace_limit; j++)
9561 TAILQ_FOREACH(w, &screens[i].ws[j].winlist,
9568 maprequest(xcb_map_request_event_t *e)
9570 struct ws_win *win, *w = NULL;
9571 xcb_get_window_attributes_reply_t *war;
9573 DNPRINTF(SWM_D_EVENT, "maprequest: win %#x\n",
9576 war = xcb_get_window_attributes_reply(conn,
9577 xcb_get_window_attributes(conn, e->window),
9580 DNPRINTF(SWM_D_EVENT, "maprequest: window lost.\n");
9584 if (war->override_redirect) {
9585 DNPRINTF(SWM_D_EVENT, "maprequest: override_redirect; "
9590 win = manage_window(e->window, spawn_position,
9591 (war->map_state == XCB_MAP_STATE_VIEWABLE));
9595 /* The new window should get focus; prepare. */
9596 if (focus_mode != SWM_FOCUS_FOLLOW &&
9597 !(win->quirks & SWM_Q_NOFOCUSONMAP) &&
9598 (!(win->hints.flags & XCB_ICCCM_WM_HINT_INPUT) ||
9599 (win->hints.flags & XCB_ICCCM_WM_HINT_INPUT &&
9600 win->hints.input))) {
9601 if (win->quirks & SWM_Q_FOCUSONMAP_SINGLE) {
9602 /* See if other wins of same type are already mapped. */
9603 TAILQ_FOREACH(w, &win->ws->winlist, entry) {
9604 if (w == win || !w->mapped)
9607 if (w->ch.class_name &&
9608 win->ch.class_name &&
9609 strcmp(w->ch.class_name,
9610 win->ch.class_name) == 0 &&
9611 w->ch.instance_name &&
9612 win->ch.instance_name &&
9613 strcmp(w->ch.instance_name,
9614 win->ch.instance_name) == 0)
9620 win->ws->focus_pending = get_focus_magic(win);
9623 /* All windows need to be mapped if they are in the current workspace.*/
9627 /* Ignore EnterNotify to handle the mapnotify without interference. */
9628 if (focus_mode == SWM_FOCUS_DEFAULT)
9629 event_drain(XCB_ENTER_NOTIFY);
9632 DNPRINTF(SWM_D_EVENT, "maprequest: done.\n");
9636 motionnotify(xcb_motion_notify_event_t *e)
9638 struct swm_region *r = NULL;
9641 DNPRINTF(SWM_D_FOCUS, "motionnotify: time: %u, win (x,y): %#x "
9642 "(%d,%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
9643 "child: %#x, same_screen_focus: %s, state: %d\n",
9644 e->time, e->event, e->event_x, e->event_y,
9645 get_notify_detail_label(e->detail), e->detail,
9646 e->root, e->root_x, e->root_y, e->child,
9647 YESNO(e->same_screen), e->state);
9649 last_event_time = e->time;
9651 if (focus_mode == SWM_FOCUS_MANUAL)
9654 num_screens = get_screen_count();
9655 for (i = 0; i < num_screens; i++)
9656 if (screens[i].root == e->root)
9659 TAILQ_FOREACH(r, &screens[i].rl, entry)
9660 if (X(r) <= e->root_x && e->root_x < MAX_X(r) &&
9661 Y(r) <= e->root_y && e->root_y < MAX_Y(r))
9669 get_atom_name(xcb_atom_t atom)
9672 #ifdef SWM_DEBUG_ATOM_NAMES
9674 * This should be disabled during most debugging since
9675 * xcb_get_* causes an xcb_flush.
9678 xcb_get_atom_name_reply_t *r;
9680 r = xcb_get_atom_name_reply(conn,
9681 xcb_get_atom_name(conn, atom),
9684 len = xcb_get_atom_name_name_length(r);
9686 name = malloc(len + 1);
9688 memcpy(name, xcb_get_atom_name_name(r), len);
9702 propertynotify(xcb_property_notify_event_t *e)
9705 struct workspace *ws;
9709 name = get_atom_name(e->atom);
9710 DNPRINTF(SWM_D_EVENT, "propertynotify: win %#x, atom: %s(%u), "
9711 "time: %#x, state: %u\n", e->window, name, e->atom, e->time,
9715 win = find_window(e->window);
9721 last_event_time = e->time;
9723 if (e->atom == a_state) {
9724 /* State just changed, make sure it gets focused if mapped. */
9725 if (e->state == XCB_PROPERTY_NEW_VALUE) {
9726 if (focus_mode != SWM_FOCUS_FOLLOW && WS_FOCUSED(ws)) {
9728 ws->focus_pending == win) {
9729 focus_win(ws->focus_pending);
9730 ws->focus_pending = NULL;
9734 } else if (e->atom == XCB_ATOM_WM_CLASS ||
9735 e->atom == XCB_ATOM_WM_NAME) {
9737 } else if (e->atom == a_prot) {
9738 get_wm_protocols(win);
9745 unmapnotify(xcb_unmap_notify_event_t *e)
9748 struct workspace *ws;
9750 DNPRINTF(SWM_D_EVENT, "unmapnotify: win %#x\n", e->window);
9752 /* If we aren't managing the window, then ignore. */
9753 win = find_window(e->window);
9754 if (win == NULL || win->id != e->window)
9757 /* Do nothing if already withdrawn. */
9758 if (!win->mapped && !ICONIC(win))
9762 win->mapped = false;
9764 /* If win was focused, make sure to focus on something else. */
9765 if (win == ws->focus) {
9766 if (focus_mode != SWM_FOCUS_FOLLOW) {
9767 ws->focus_pending = get_focus_prev(win);
9768 DNPRINTF(SWM_D_EVENT, "unmapnotify: "
9769 "focus_pending: %#x\n",
9770 WINID(ws->focus_pending));
9778 set_win_state(win, XCB_ICCCM_WM_STATE_ICONIC);
9781 set_win_state(win, XCB_ICCCM_WM_STATE_WITHDRAWN);
9782 unmanage_window(win);
9788 /* Update focus if ws is active. */
9789 if (WS_FOCUSED(ws)) {
9790 if (focus_mode == SWM_FOCUS_FOLLOW) {
9791 focus_win(get_pointer_win(ws->r->s->root));
9792 } else if (ws->focus_pending) {
9793 focus_win(ws->focus_pending);
9794 ws->focus_pending = NULL;
9795 } else if (ws->focus == NULL) {
9796 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT,
9797 ws->r->id, XCB_CURRENT_TIME);
9806 get_source_type_label(uint32_t type)
9811 case EWMH_SOURCE_TYPE_NONE:
9814 case EWMH_SOURCE_TYPE_NORMAL:
9817 case EWMH_SOURCE_TYPE_OTHER:
9829 clientmessage(xcb_client_message_event_t *e)
9832 struct swm_region *r = NULL;
9836 xcb_map_request_event_t mre;
9840 name = get_atom_name(e->type);
9841 DNPRINTF(SWM_D_EVENT, "clientmessage: win %#x, atom: %s(%u)\n",
9842 e->window, name, e->type);
9846 if (e->type == ewmh[_NET_CURRENT_DESKTOP].atom) {
9847 num_screens = get_screen_count();
9848 for (i = 0; i < num_screens; i++)
9849 if (screens[i].root == e->window) {
9850 r = screens[i].r_focus;
9854 if (r && e->data.data32[0] < (uint32_t)workspace_limit) {
9855 a.id = e->data.data32[0];
9863 win = find_window(e->window);
9865 if (e->type == ewmh[_NET_ACTIVE_WINDOW].atom) {
9866 /* Manage the window with maprequest. */
9867 DNPRINTF(SWM_D_EVENT, "clientmessage: request focus on "
9868 "unmanaged window.\n");
9869 mre.window = e->window;
9875 if (e->type == ewmh[_NET_ACTIVE_WINDOW].atom) {
9876 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_ACTIVE_WINDOW, "
9877 "source_type: %s(%d)\n",
9878 get_source_type_label(e->data.data32[0]),
9882 * Allow focus changes that are a result of direct user
9883 * action and from applications that use the old EWMH spec.
9885 if (e->data.data32[0] != EWMH_SOURCE_TYPE_NORMAL ||
9886 win->quirks & SWM_Q_OBEYAPPFOCUSREQ) {
9887 if (WS_FOCUSED(win->ws))
9890 win->ws->focus_pending = win;
9892 } else if (e->type == ewmh[_NET_CLOSE_WINDOW].atom) {
9893 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_CLOSE_WINDOW\n");
9894 if (win->can_delete)
9895 client_msg(win, a_delete, 0);
9897 xcb_kill_client(conn, win->id);
9898 } else if (e->type == ewmh[_NET_MOVERESIZE_WINDOW].atom) {
9899 DNPRINTF(SWM_D_EVENT,
9900 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
9902 if (e->data.data32[0] & (1<<8)) /* x */
9903 X(win) = e->data.data32[1];
9904 if (e->data.data32[0] & (1<<9)) /* y */
9905 Y(win) = e->data.data32[2];
9906 if (e->data.data32[0] & (1<<10)) /* width */
9907 WIDTH(win) = e->data.data32[3];
9908 if (e->data.data32[0] & (1<<11)) /* height */
9909 HEIGHT(win) = e->data.data32[4];
9913 /* Notify no change was made. */
9914 config_win(win, NULL);
9915 /* TODO: Change stack sizes */
9917 } else if (e->type == ewmh[_NET_RESTACK_WINDOW].atom) {
9918 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_RESTACK_WINDOW\n");
9919 val[0] = e->data.data32[1]; /* Sibling window. */
9920 val[1] = e->data.data32[2]; /* Stack mode detail. */
9922 xcb_configure_window(conn, win->id, XCB_CONFIG_WINDOW_SIBLING |
9923 XCB_CONFIG_WINDOW_STACK_MODE, val);
9924 } else if (e->type == ewmh[_NET_WM_STATE].atom) {
9925 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_WM_STATE\n");
9926 ewmh_change_wm_state(win, e->data.data32[1], e->data.data32[0]);
9927 if (e->data.data32[2])
9928 ewmh_change_wm_state(win, e->data.data32[2],
9931 ewmh_update_wm_state(win);
9933 } else if (e->type == ewmh[_NET_WM_DESKTOP].atom) {
9934 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_WM_DESKTOP\n");
9937 win_to_ws(win, e->data.data32[0], true);
9939 /* Restack if either the source or destination ws is mapped. */
9940 if (r != NULL || win->ws->r != NULL) {
9942 load_float_geom(win);
9954 int errcode = xcb_connection_has_error(conn);
9955 #ifdef XCB_CONN_ERROR
9958 case XCB_CONN_ERROR:
9959 s = "Socket error, pipe error or other stream error.";
9961 case XCB_CONN_CLOSED_EXT_NOTSUPPORTED:
9962 s = "Extension not supported.";
9964 case XCB_CONN_CLOSED_MEM_INSUFFICIENT:
9965 s = "Insufficient memory.";
9967 case XCB_CONN_CLOSED_REQ_LEN_EXCEED:
9968 s = "Request length was exceeded.";
9970 case XCB_CONN_CLOSED_PARSE_ERR:
9971 s = "Error parsing display string.";
9974 s = "Unknown error.";
9977 errx(errcode, "X CONNECTION ERROR: %s", s);
9980 errx(errcode, "X CONNECTION ERROR");
9988 const uint32_t val = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
9989 XCB_EVENT_MASK_ENTER_WINDOW;
9991 xcb_void_cookie_t wac;
9992 xcb_generic_error_t *error;
9994 /* this causes an error if some other window manager is running */
9995 num_screens = get_screen_count();
9996 for (i = 0; i < num_screens; i++) {
9997 if ((sc = get_screen(i)) == NULL)
9998 errx(1, "ERROR: can't get screen %d.", i);
9999 DNPRINTF(SWM_D_INIT, "enable_wm: screen %d, root: %#x\n",
10001 wac = xcb_change_window_attributes_checked(conn, sc->root,
10002 XCB_CW_EVENT_MASK, &val);
10003 if ((error = xcb_request_check(conn, wac))) {
10004 DNPRINTF(SWM_D_INIT, "enable_wm: error_code: %u\n",
10005 error->error_code);
10010 /* click to focus on empty region */
10011 xcb_grab_button(conn, 1, sc->root, BUTTONMASK,
10012 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC, XCB_WINDOW_NONE,
10013 XCB_CURSOR_NONE, XCB_BUTTON_INDEX_1, XCB_BUTTON_MASK_ANY);
10020 new_region(struct swm_screen *s, int x, int y, int w, int h)
10022 struct swm_region *r = NULL, *n;
10023 struct workspace *ws = NULL;
10027 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
10028 s->idx, w, h, x, y);
10030 /* remove any conflicting regions */
10031 n = TAILQ_FIRST(&s->rl);
10034 n = TAILQ_NEXT(r, entry);
10035 if (X(r) < (x + w) && (X(r) + WIDTH(r)) > x &&
10036 Y(r) < (y + h) && (Y(r) + HEIGHT(r)) > y) {
10037 if (r->ws->r != NULL)
10038 r->ws->old_r = r->ws->r;
10041 xcb_destroy_window(conn, r->id);
10042 TAILQ_REMOVE(&s->rl, r, entry);
10043 TAILQ_INSERT_TAIL(&s->orl, r, entry);
10047 /* search old regions for one to reuse */
10049 /* size + location match */
10050 TAILQ_FOREACH(r, &s->orl, entry)
10051 if (X(r) == x && Y(r) == y &&
10052 HEIGHT(r) == h && WIDTH(r) == w)
10056 TAILQ_FOREACH(r, &s->orl, entry)
10057 if (HEIGHT(r) == h && WIDTH(r) == w)
10061 TAILQ_REMOVE(&s->orl, r, entry);
10062 /* try to use old region's workspace */
10063 if (r->ws->r == NULL)
10066 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
10067 err(1, "new_region: calloc: failed to allocate memory "
10070 /* if we don't have a workspace already, find one */
10072 for (i = 0; i < workspace_limit; i++)
10073 if (s->ws[i].r == NULL) {
10080 errx(1, "new_region: no free workspaces");
10082 if (ws->state == SWM_WS_STATE_HIDDEN)
10083 ws->state = SWM_WS_STATE_MAPPING;
10091 r->ws_prior = NULL;
10094 TAILQ_INSERT_TAIL(&s->rl, r, entry);
10096 /* Invisible region window to detect pointer events on empty regions. */
10097 r->id = xcb_generate_id(conn);
10098 wa[0] = XCB_EVENT_MASK_POINTER_MOTION |
10099 XCB_EVENT_MASK_POINTER_MOTION_HINT;
10101 xcb_create_window(conn, XCB_COPY_FROM_PARENT, r->id, r->s->root,
10102 X(r), Y(r), WIDTH(r), HEIGHT(r), 0, XCB_WINDOW_CLASS_INPUT_ONLY,
10103 XCB_COPY_FROM_PARENT, XCB_CW_EVENT_MASK, wa);
10105 /* Make sure region input is at the bottom. */
10106 wa[0] = XCB_STACK_MODE_BELOW;
10107 xcb_configure_window(conn, r->id, XCB_CONFIG_WINDOW_STACK_MODE, wa);
10109 xcb_map_window(conn, r->id);
10113 scan_randr(int idx)
10115 #ifdef SWM_XRR_HAS_CRTC
10118 #endif /* SWM_XRR_HAS_CRTC */
10119 struct swm_region *r;
10120 struct ws_win *win;
10122 xcb_randr_get_screen_resources_current_cookie_t src;
10123 xcb_randr_get_screen_resources_current_reply_t *srr;
10124 xcb_randr_get_crtc_info_cookie_t cic;
10125 xcb_randr_get_crtc_info_reply_t *cir = NULL;
10126 xcb_randr_crtc_t *crtc;
10127 xcb_screen_t *screen;
10129 DNPRINTF(SWM_D_MISC, "scan_randr: screen: %d\n", idx);
10131 if ((screen = get_screen(idx)) == NULL)
10132 errx(1, "ERROR: can't get screen %d.", idx);
10134 num_screens = get_screen_count();
10135 if (idx >= num_screens)
10136 errx(1, "scan_randr: invalid screen");
10138 /* remove any old regions */
10139 while ((r = TAILQ_FIRST(&screens[idx].rl)) != NULL) {
10140 r->ws->old_r = r->ws->r = NULL;
10142 xcb_destroy_window(conn, r->id);
10143 TAILQ_REMOVE(&screens[idx].rl, r, entry);
10144 TAILQ_INSERT_TAIL(&screens[idx].orl, r, entry);
10148 /* map virtual screens onto physical screens */
10149 #ifdef SWM_XRR_HAS_CRTC
10150 if (randr_support) {
10151 src = xcb_randr_get_screen_resources_current(conn,
10152 screens[idx].root);
10153 srr = xcb_randr_get_screen_resources_current_reply(conn, src,
10156 new_region(&screens[idx], 0, 0,
10157 screen->width_in_pixels,
10158 screen->height_in_pixels);
10161 ncrtc = srr->num_crtcs;
10163 crtc = xcb_randr_get_screen_resources_current_crtcs(srr);
10164 for (c = 0; c < ncrtc; c++) {
10165 cic = xcb_randr_get_crtc_info(conn, crtc[c],
10167 cir = xcb_randr_get_crtc_info_reply(conn, cic, NULL);
10170 if (cir->num_outputs == 0) {
10175 if (cir->mode == 0)
10176 new_region(&screens[idx], 0, 0,
10177 screen->width_in_pixels,
10178 screen->height_in_pixels);
10180 new_region(&screens[idx],
10181 cir->x, cir->y, cir->width, cir->height);
10186 #endif /* SWM_XRR_HAS_CRTC */
10188 /* If detection failed, create a single region that spans the screen. */
10189 if (TAILQ_EMPTY(&screens[idx].rl))
10190 new_region(&screens[idx], 0, 0, screen->width_in_pixels,
10191 screen->height_in_pixels);
10194 /* Cleanup unused previously visible workspaces. */
10195 TAILQ_FOREACH(r, &screens[idx].orl, entry) {
10196 TAILQ_FOREACH(win, &r->ws->winlist, entry)
10198 r->ws->state = SWM_WS_STATE_HIDDEN;
10200 /* The screen shouldn't focus on an unused region. */
10201 if (screens[idx].r_focus == r)
10202 screens[idx].r_focus = NULL;
10205 DNPRINTF(SWM_D_MISC, "scan_randr: done.\n");
10209 screenchange(xcb_randr_screen_change_notify_event_t *e)
10211 struct swm_region *r;
10212 int i, num_screens;
10214 DNPRINTF(SWM_D_EVENT, "screenchange: root: %#x\n", e->root);
10216 num_screens = get_screen_count();
10217 /* silly event doesn't include the screen index */
10218 for (i = 0; i < num_screens; i++)
10219 if (screens[i].root == e->root)
10221 if (i >= num_screens)
10222 errx(1, "screenchange: screen not found");
10224 /* brute force for now, just re-enumerate the regions */
10228 print_win_geom(e->root);
10230 /* add bars to all regions */
10231 for (i = 0; i < num_screens; i++) {
10232 TAILQ_FOREACH(r, &screens[i].rl, entry)
10238 /* Make sure a region has focus on each screen. */
10239 for (i = 0; i < num_screens; i++) {
10240 if (screens[i].r_focus == NULL) {
10241 r = TAILQ_FIRST(&screens[i].rl);
10250 /* Update workspace state on all regions. */
10251 for (i = 0; i < num_screens; i++)
10252 TAILQ_FOREACH(r, &screens[i].rl, entry)
10253 r->ws->state = SWM_WS_STATE_MAPPED;
10259 struct swm_region *r = NULL;
10260 xcb_window_t *wins = NULL, trans, *cwins = NULL;
10261 int i, j, k, n, no, num_screens;
10263 bool manage, mapped;
10265 xcb_query_tree_cookie_t qtc;
10266 xcb_query_tree_reply_t *qtr;
10267 xcb_get_window_attributes_cookie_t gac;
10268 xcb_get_window_attributes_reply_t *gar;
10269 xcb_get_property_cookie_t pc;
10270 xcb_get_property_reply_t *pr;
10272 DNPRINTF(SWM_D_INIT, "grab_windows: begin\n");
10273 num_screens = get_screen_count();
10274 for (i = 0; i < num_screens; i++) {
10275 qtc = xcb_query_tree(conn, screens[i].root);
10276 qtr = xcb_query_tree_reply(conn, qtc, NULL);
10279 wins = xcb_query_tree_children(qtr);
10280 no = xcb_query_tree_children_length(qtr);
10282 /* Try to sort windows according to _NET_CLIENT_LIST. */
10283 pr = xcb_get_property_reply(conn, xcb_get_property(conn, 0,
10284 screens[i].root, ewmh[_NET_CLIENT_LIST].atom,
10285 XCB_ATOM_WINDOW, 0, UINT32_MAX), NULL);
10287 cwins = xcb_get_property_value(pr);
10288 n = xcb_get_property_value_length(pr) /
10289 sizeof(xcb_atom_t);
10291 for (j = 0; j < n; ++j) {
10292 for (k = j; k < no; ++k) {
10293 if (wins[k] == cwins[j]) {
10294 /* Swap wins j and k. */
10296 wins[j] = cwins[j];
10304 /* attach windows to a region */
10305 /* normal windows */
10306 DNPRINTF(SWM_D_INIT, "grab_windows: grab top level windows.\n");
10307 for (j = 0; j < no; j++) {
10308 TAILQ_FOREACH(r, &screens[i].rl, entry) {
10309 if (r->id == wins[j]) {
10310 DNPRINTF(SWM_D_INIT, "grab_windows: "
10311 "skip %#x; region input window.\n",
10314 } else if (r->bar->id == wins[j]) {
10315 DNPRINTF(SWM_D_INIT, "grab_windows: "
10316 "skip %#x; region bar.\n",
10325 gac = xcb_get_window_attributes(conn, wins[j]);
10326 gar = xcb_get_window_attributes_reply(conn, gac, NULL);
10328 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
10329 "doesn't exist.\n", wins[j]);
10333 if (gar->override_redirect) {
10334 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
10335 "override_redirect set.\n", wins[j]);
10340 pc = xcb_icccm_get_wm_transient_for(conn, wins[j]);
10341 if (xcb_icccm_get_wm_transient_for_reply(conn, pc,
10343 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
10344 "is transient for %#x.\n", wins[j], trans);
10349 state = get_win_state(wins[j]);
10350 manage = state != XCB_ICCCM_WM_STATE_WITHDRAWN;
10351 mapped = gar->map_state == XCB_MAP_STATE_VIEWABLE;
10352 if (mapped || manage)
10353 manage_window(wins[j], SWM_STACK_TOP, mapped);
10356 /* transient windows */
10357 DNPRINTF(SWM_D_INIT, "grab_windows: grab transient windows.\n");
10358 for (j = 0; j < no; j++) {
10359 gac = xcb_get_window_attributes(conn, wins[j]);
10360 gar = xcb_get_window_attributes_reply(conn, gac, NULL);
10362 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
10363 "doesn't exist.\n", wins[j]);
10367 if (gar->override_redirect) {
10368 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
10369 "override_redirect set.\n", wins[j]);
10374 state = get_win_state(wins[j]);
10375 manage = state != XCB_ICCCM_WM_STATE_WITHDRAWN;
10376 mapped = gar->map_state == XCB_MAP_STATE_VIEWABLE;
10377 pc = xcb_icccm_get_wm_transient_for(conn, wins[j]);
10378 if (xcb_icccm_get_wm_transient_for_reply(conn, pc,
10379 &trans, NULL) && manage)
10380 manage_window(wins[j], SWM_STACK_TOP, mapped);
10385 DNPRINTF(SWM_D_INIT, "grab_windows: done.\n");
10389 setup_screens(void)
10391 int i, j, k, num_screens;
10392 struct workspace *ws;
10393 uint32_t gcv[1], wa[1];
10394 const xcb_query_extension_reply_t *qep;
10395 xcb_screen_t *screen;
10396 xcb_randr_query_version_cookie_t c;
10397 xcb_randr_query_version_reply_t *r;
10399 num_screens = get_screen_count();
10400 if ((screens = calloc(num_screens,
10401 sizeof(struct swm_screen))) == NULL)
10402 err(1, "setup_screens: calloc: failed to allocate memory for "
10405 /* Initial RandR setup. */
10406 randr_support = false;
10407 qep = xcb_get_extension_data(conn, &xcb_randr_id);
10408 if (qep->present) {
10409 c = xcb_randr_query_version(conn, 1, 1);
10410 r = xcb_randr_query_version_reply(conn, c, NULL);
10412 if (r->major_version >= 1) {
10413 randr_support = true;
10414 randr_eventbase = qep->first_event;
10420 wa[0] = cursors[XC_LEFT_PTR].cid;
10422 /* map physical screens */
10423 for (i = 0; i < num_screens; i++) {
10424 DNPRINTF(SWM_D_WS, "setup_screens: init screen: %d\n", i);
10425 screens[i].idx = i;
10426 screens[i].r_focus = NULL;
10428 TAILQ_INIT(&screens[i].rl);
10429 TAILQ_INIT(&screens[i].orl);
10430 if ((screen = get_screen(i)) == NULL)
10431 errx(1, "ERROR: can't get screen %d.", i);
10432 screens[i].root = screen->root;
10434 /* set default colors */
10435 setscreencolor("red", i, SWM_S_COLOR_FOCUS);
10436 setscreencolor("rgb:88/88/88", i, SWM_S_COLOR_UNFOCUS);
10437 setscreencolor("rgb:00/80/80", i, SWM_S_COLOR_BAR_BORDER);
10438 setscreencolor("rgb:00/40/40", i,
10439 SWM_S_COLOR_BAR_BORDER_UNFOCUS);
10440 setscreencolor("black", i, SWM_S_COLOR_BAR);
10441 setscreencolor("rgb:a0/a0/a0", i, SWM_S_COLOR_BAR_FONT);
10442 setscreencolor("red", i, SWM_S_COLOR_FOCUS_MAXIMIZED);
10443 setscreencolor("rgb:88/88/88", i,
10444 SWM_S_COLOR_UNFOCUS_MAXIMIZED);
10446 /* create graphics context on screen */
10447 screens[i].bar_gc = xcb_generate_id(conn);
10449 xcb_create_gc(conn, screens[i].bar_gc, screens[i].root,
10450 XCB_GC_GRAPHICS_EXPOSURES, gcv);
10452 /* set default cursor */
10453 xcb_change_window_attributes(conn, screens[i].root,
10454 XCB_CW_CURSOR, wa);
10456 /* init all workspaces */
10457 /* XXX these should be dynamically allocated too */
10458 for (j = 0; j < SWM_WS_MAX; j++) {
10459 ws = &screens[i].ws[j];
10462 ws->bar_enabled = true;
10464 ws->focus_prev = NULL;
10465 ws->focus_pending = NULL;
10468 ws->state = SWM_WS_STATE_HIDDEN;
10469 TAILQ_INIT(&ws->stack);
10470 TAILQ_INIT(&ws->winlist);
10471 TAILQ_INIT(&ws->unmanagedlist);
10473 for (k = 0; layouts[k].l_stack != NULL; k++)
10474 if (layouts[k].l_config != NULL)
10475 layouts[k].l_config(ws,
10476 SWM_ARG_ID_STACKINIT);
10477 ws->cur_layout = &layouts[0];
10478 ws->cur_layout->l_string(ws);
10484 xcb_randr_select_input(conn, screens[i].root,
10485 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE);
10490 setup_globals(void)
10492 if ((bar_fonts = strdup(SWM_BAR_FONTS)) == NULL)
10493 err(1, "setup_globals: strdup: failed to allocate memory.");
10495 if ((clock_format = strdup("%a %b %d %R %Z %Y")) == NULL)
10496 err(1, "setup_globals: strdup: failed to allocate memory.");
10498 if ((syms = xcb_key_symbols_alloc(conn)) == NULL)
10499 errx(1, "unable to allocate key symbols");
10501 a_state = get_atom_from_string("WM_STATE");
10502 a_prot = get_atom_from_string("WM_PROTOCOLS");
10503 a_delete = get_atom_from_string("WM_DELETE_WINDOW");
10504 a_net_supported = get_atom_from_string("_NET_SUPPORTED");
10505 a_net_wm_check = get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
10506 a_takefocus = get_atom_from_string("WM_TAKE_FOCUS");
10507 a_utf8_string = get_atom_from_string("UTF8_STRING");
10508 a_swm_ws = get_atom_from_string("_SWM_WS");
10512 shutdown_cleanup(void)
10514 int i, num_screens;
10516 /* disable alarm because the following code may not be interrupted */
10518 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
10519 err(1, "can't disable alarm");
10528 num_screens = get_screen_count();
10529 for (i = 0; i < num_screens; ++i) {
10530 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT,
10531 screens[i].root, XCB_CURRENT_TIME);
10533 if (screens[i].bar_gc != XCB_NONE)
10534 xcb_free_gc(conn, screens[i].bar_gc);
10535 if (!bar_font_legacy) {
10536 XftColorFree(display, DefaultVisual(display, i),
10537 DefaultColormap(display, i), &bar_font_color);
10538 XftColorFree(display, DefaultVisual(display, i),
10539 DefaultColormap(display, i), &search_font_color);
10543 if (bar_font_legacy)
10544 XFreeFontSet(display, bar_fs);
10546 XftFontClose(display, bar_font);
10549 xcb_key_symbols_free(syms);
10551 xcb_disconnect(conn);
10555 event_error(xcb_generic_error_t *e)
10559 DNPRINTF(SWM_D_EVENT, "event_error: %s(%u) from %s(%u), sequence: %u, "
10560 "resource_id: %u, minor_code: %u\n",
10561 xcb_event_get_error_label(e->error_code), e->error_code,
10562 xcb_event_get_request_label(e->major_code), e->major_code,
10563 e->sequence, e->resource_id, e->minor_code);
10567 event_handle(xcb_generic_event_t *evt)
10569 uint8_t type = XCB_EVENT_RESPONSE_TYPE(evt);
10571 DNPRINTF(SWM_D_EVENT, "XCB Event: %s(%d), seq %u\n",
10572 xcb_event_get_label(XCB_EVENT_RESPONSE_TYPE(evt)),
10573 XCB_EVENT_RESPONSE_TYPE(evt), evt->sequence);
10576 #define EVENT(type, callback) case type: callback((void *)evt); return
10577 EVENT(0, event_error);
10578 EVENT(XCB_BUTTON_PRESS, buttonpress);
10579 /*EVENT(XCB_BUTTON_RELEASE, buttonpress);*/
10580 /*EVENT(XCB_CIRCULATE_NOTIFY, );*/
10581 /*EVENT(XCB_CIRCULATE_REQUEST, );*/
10582 EVENT(XCB_CLIENT_MESSAGE, clientmessage);
10583 /*EVENT(XCB_COLORMAP_NOTIFY, );*/
10584 EVENT(XCB_CONFIGURE_NOTIFY, configurenotify);
10585 EVENT(XCB_CONFIGURE_REQUEST, configurerequest);
10586 /*EVENT(XCB_CREATE_NOTIFY, );*/
10587 EVENT(XCB_DESTROY_NOTIFY, destroynotify);
10588 EVENT(XCB_ENTER_NOTIFY, enternotify);
10589 EVENT(XCB_EXPOSE, expose);
10591 EVENT(XCB_FOCUS_IN, focusin);
10592 EVENT(XCB_FOCUS_OUT, focusout);
10594 /*EVENT(XCB_GRAPHICS_EXPOSURE, );*/
10595 /*EVENT(XCB_GRAVITY_NOTIFY, );*/
10596 EVENT(XCB_KEY_PRESS, keypress);
10597 /*EVENT(XCB_KEY_RELEASE, keypress);*/
10598 /*EVENT(XCB_KEYMAP_NOTIFY, );*/
10600 EVENT(XCB_LEAVE_NOTIFY, leavenotify);
10602 EVENT(XCB_MAP_NOTIFY, mapnotify);
10603 EVENT(XCB_MAP_REQUEST, maprequest);
10604 EVENT(XCB_MAPPING_NOTIFY, mappingnotify);
10605 EVENT(XCB_MOTION_NOTIFY, motionnotify);
10606 /*EVENT(XCB_NO_EXPOSURE, );*/
10607 EVENT(XCB_PROPERTY_NOTIFY, propertynotify);
10608 /*EVENT(XCB_REPARENT_NOTIFY, );*/
10609 /*EVENT(XCB_RESIZE_REQUEST, );*/
10610 /*EVENT(XCB_SELECTION_CLEAR, );*/
10611 /*EVENT(XCB_SELECTION_NOTIFY, );*/
10612 /*EVENT(XCB_SELECTION_REQUEST, );*/
10613 EVENT(XCB_UNMAP_NOTIFY, unmapnotify);
10614 /*EVENT(XCB_VISIBILITY_NOTIFY, );*/
10617 if (type - randr_eventbase == XCB_RANDR_SCREEN_CHANGE_NOTIFY)
10618 screenchange((void *)evt);
10622 main(int argc, char *argv[])
10624 struct swm_region *r;
10625 char conf[PATH_MAX], *cfile = NULL;
10627 int xfd, i, num_screens;
10628 struct sigaction sact;
10629 xcb_generic_event_t *evt;
10634 bool stdin_ready = false, startup = true;
10636 /* suppress unused warning since var is needed */
10640 time_started = time(NULL);
10644 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION, buildstr);
10645 if (setlocale(LC_CTYPE, "") == NULL || setlocale(LC_TIME, "") == NULL)
10646 warnx("no locale support");
10648 /* handle some signals */
10649 bzero(&sact, sizeof(sact));
10650 sigemptyset(&sact.sa_mask);
10652 sact.sa_handler = sighdlr;
10653 sigaction(SIGINT, &sact, NULL);
10654 sigaction(SIGQUIT, &sact, NULL);
10655 sigaction(SIGTERM, &sact, NULL);
10656 sigaction(SIGHUP, &sact, NULL);
10658 sact.sa_handler = sighdlr;
10659 sact.sa_flags = SA_NOCLDSTOP;
10660 sigaction(SIGCHLD, &sact, NULL);
10662 if ((display = XOpenDisplay(0)) == NULL)
10663 errx(1, "can not open display");
10665 conn = XGetXCBConnection(display);
10666 if (xcb_connection_has_error(conn))
10667 errx(1, "can not get XCB connection");
10669 XSetEventQueueOwner(display, XCBOwnsEventQueue);
10671 xcb_prefetch_extension_data(conn, &xcb_randr_id);
10672 xfd = xcb_get_file_descriptor(conn);
10674 /* look for local and global conf file */
10675 pwd = getpwuid(getuid());
10677 errx(1, "invalid user: %d", getuid());
10679 xcb_grab_server(conn);
10680 xcb_aux_sync(conn);
10682 /* flush all events */
10683 while ((evt = xcb_poll_for_event(conn))) {
10684 if (XCB_EVENT_RESPONSE_TYPE(evt) == 0)
10690 errx(1, "another window manager is currently running");
10692 /* Load Xcursors and/or cursorfont glyph cursors. */
10695 xcb_aux_sync(conn);
10705 for (i = 0; ; i++) {
10710 snprintf(conf, sizeof conf, "%s/.%s",
10711 pwd->pw_dir, SWM_CONF_FILE);
10715 snprintf(conf, sizeof conf, "/etc/%s",
10720 snprintf(conf, sizeof conf, "%s/.%s",
10721 pwd->pw_dir, SWM_CONF_FILE_OLD);
10724 /* global compat */
10725 snprintf(conf, sizeof conf, "/etc/%s",
10726 SWM_CONF_FILE_OLD);
10732 if (strlen(conf) && stat(conf, &sb) != -1)
10733 if (S_ISREG(sb.st_mode)) {
10740 /* load conf (if any) */
10742 conf_load(cfile, SWM_CONF_DEFAULT);
10746 if (getenv("SWM_STARTED") == NULL)
10747 setenv("SWM_STARTED", "YES", 1);
10749 /* setup all bars */
10750 num_screens = get_screen_count();
10751 for (i = 0; i < num_screens; i++)
10752 TAILQ_FOREACH(r, &screens[i].rl, entry)
10755 /* Manage existing windows. */
10762 xcb_ungrab_server(conn);
10765 /* Update state of each newly mapped workspace. */
10766 for (i = 0; i < num_screens; i++)
10767 TAILQ_FOREACH(r, &screens[i].rl, entry)
10768 r->ws->state = SWM_WS_STATE_MAPPED;
10770 rd_max = xfd > STDIN_FILENO ? xfd : STDIN_FILENO;
10773 while ((evt = xcb_poll_for_event(conn))) {
10780 /* If just (re)started, set default focus if needed. */
10784 if (focus_mode != SWM_FOCUS_FOLLOW) {
10785 r = TAILQ_FIRST(&screens[0].rl);
10797 FD_SET(STDIN_FILENO, &rd);
10802 num_readable = select(rd_max + 1, &rd, NULL, NULL, &tv);
10803 if (num_readable == -1 && errno != EINTR) {
10804 DNPRINTF(SWM_D_MISC, "select failed");
10805 } else if (num_readable > 0 && FD_ISSET(STDIN_FILENO, &rd)) {
10806 stdin_ready = true;
10810 restart(NULL, NULL);
10819 stdin_ready = false;
10820 bar_extra_update();
10827 shutdown_cleanup();