2 * Copyright (c) 2009-2012 Marco Peereboom <marco@peereboom.us>
3 * Copyright (c) 2009-2011 Ryan McBride <mcbride@countersiege.com>
4 * Copyright (c) 2009 Darrin Chandler <dwchandler@stilyagin.com>
5 * Copyright (c) 2009 Pierre-Yves Ritschard <pyr@spootnik.org>
6 * Copyright (c) 2010 Tuukka Kataja <stuge@xor.fi>
7 * Copyright (c) 2011 Jason L. Wright <jason@thought.net>
8 * Copyright (c) 2011-2014 Reginald Kennedy <rk@rejii.com>
9 * Copyright (c) 2011-2012 Lawrence Teo <lteo@lteo.net>
10 * Copyright (c) 2011-2012 Tiago Cunha <tcunha@gmx.com>
11 * Copyright (c) 2012-2013 David Hill <dhill@mindcry.org>
13 * Permission to use, copy, modify, and distribute this software for any
14 * purpose with or without fee is hereby granted, provided that the above
15 * copyright notice and this permission notice appear in all copies.
17 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
18 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
20 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
21 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
22 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
23 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 * Much code and ideas taken from dwm under the following license:
27 * MIT/X Consortium License
29 * 2006-2008 Anselm R Garbe <garbeam at gmail dot com>
30 * 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
31 * 2006-2007 Jukka Salmi <jukka at salmi dot ch>
32 * 2007 Premysl Hruby <dfenze at gmail dot com>
33 * 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
34 * 2007 Christof Musik <christof at sendfax dot de>
35 * 2007-2008 Enno Gottox Boland <gottox at s01 dot de>
36 * 2007-2008 Peter Hartlich <sgkkr at hartlich dot com>
37 * 2008 Martin Hurton <martin dot hurton at gmail dot com>
39 * Permission is hereby granted, free of charge, to any person obtaining a
40 * copy of this software and associated documentation files (the "Software"),
41 * to deal in the Software without restriction, including without limitation
42 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
43 * and/or sell copies of the Software, and to permit persons to whom the
44 * Software is furnished to do so, subject to the following conditions:
46 * The above copyright notice and this permission notice shall be included in
47 * all copies or substantial portions of the Software.
49 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
50 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
51 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
52 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
53 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
54 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
55 * DEALINGS IN THE SOFTWARE.
59 #include <sys/types.h>
63 #include <sys/queue.h>
64 #include <sys/param.h>
65 #include <sys/select.h>
66 #if defined(__linux__)
68 #elif defined(__OpenBSD__)
70 #elif defined(__FreeBSD__)
93 #include <X11/cursorfont.h>
94 #include <X11/extensions/Xrandr.h>
95 #include <X11/Xcursor/Xcursor.h>
96 #include <X11/Xft/Xft.h>
97 #include <X11/Xlib-xcb.h>
98 #include <xcb/xcb_atom.h>
99 #include <xcb/xcb_aux.h>
100 #include <xcb/xcb_event.h>
101 #include <xcb/xcb_icccm.h>
102 #include <xcb/xcb_keysyms.h>
103 #include <xcb/xtest.h>
104 #include <xcb/randr.h>
112 #ifdef SPECTRWM_BUILDSTR
113 static const char *buildstr = SPECTRWM_BUILDSTR;
115 static const char *buildstr = SPECTRWM_VERSION;
118 #if !defined(__CYGWIN__) /* cygwin chokes on randr stuff */
120 # error RandR versions less than 1.0 are not supported
123 # if RANDR_MAJOR >= 1
124 # if RANDR_MINOR >= 2
125 # define SWM_XRR_HAS_CRTC
128 #endif /* __CYGWIN__ */
130 #ifndef XCB_ICCCM_NUM_WM_HINTS_ELEMENTS
131 #define XCB_ICCCM_SIZE_HINT_P_MIN_SIZE XCB_SIZE_HINT_P_MIN_SIZE
132 #define XCB_ICCCM_SIZE_HINT_P_MAX_SIZE XCB_SIZE_HINT_P_MAX_SIZE
133 #define XCB_ICCCM_SIZE_HINT_P_RESIZE_INC XCB_SIZE_HINT_P_RESIZE_INC
134 #define XCB_ICCCM_WM_HINT_INPUT XCB_WM_HINT_INPUT
135 #define XCB_ICCCM_WM_HINT_X_URGENCY XCB_WM_HINT_X_URGENCY
136 #define XCB_ICCCM_WM_STATE_ICONIC XCB_WM_STATE_ICONIC
137 #define XCB_ICCCM_WM_STATE_WITHDRAWN XCB_WM_STATE_WITHDRAWN
138 #define XCB_ICCCM_WM_STATE_NORMAL XCB_WM_STATE_NORMAL
139 #define xcb_icccm_get_text_property_reply_t xcb_get_text_property_reply_t
140 #define xcb_icccm_get_text_property_reply_wipe xcb_get_text_property_reply_wipe
141 #define xcb_icccm_get_wm_class xcb_get_wm_class
142 #define xcb_icccm_get_wm_class_reply xcb_get_wm_class_reply
143 #define xcb_icccm_get_wm_class_reply_t xcb_get_wm_class_reply_t
144 #define xcb_icccm_get_wm_class_reply_wipe xcb_get_wm_class_reply_wipe
145 #define xcb_icccm_get_wm_hints xcb_get_wm_hints
146 #define xcb_icccm_wm_hints_get_urgency xcb_wm_hints_get_urgency
147 #define xcb_icccm_get_wm_hints_reply xcb_get_wm_hints_reply
148 #define xcb_icccm_get_wm_name xcb_get_wm_name
149 #define xcb_icccm_get_wm_name_reply xcb_get_wm_name_reply
150 #define xcb_icccm_get_wm_normal_hints xcb_get_wm_normal_hints
151 #define xcb_icccm_get_wm_normal_hints_reply xcb_get_wm_normal_hints_reply
152 #define xcb_icccm_get_wm_protocols xcb_get_wm_protocols
153 #define xcb_icccm_get_wm_protocols_reply xcb_get_wm_protocols_reply
154 #define xcb_icccm_get_wm_protocols_reply_t xcb_get_wm_protocols_reply_t
155 #define xcb_icccm_get_wm_protocols_reply_wipe xcb_get_wm_protocols_reply_wipe
156 #define xcb_icccm_get_wm_transient_for xcb_get_wm_transient_for
157 #define xcb_icccm_get_wm_transient_for_reply xcb_get_wm_transient_for_reply
158 #define xcb_icccm_wm_hints_t xcb_wm_hints_t
161 /*#define SWM_DEBUG*/
163 #define DPRINTF(x...) do { \
165 fprintf(stderr, x); \
167 #define DNPRINTF(n,x...) do { \
168 if (swm_debug & n) { \
169 fprintf(stderr, "%ld ", (long)(time(NULL) - time_started)); \
170 fprintf(stderr, x); \
173 #define SWM_D_MISC 0x0001
174 #define SWM_D_EVENT 0x0002
175 #define SWM_D_WS 0x0004
176 #define SWM_D_FOCUS 0x0008
177 #define SWM_D_MOVE 0x0010
178 #define SWM_D_STACK 0x0020
179 #define SWM_D_MOUSE 0x0040
180 #define SWM_D_PROP 0x0080
181 #define SWM_D_CLASS 0x0100
182 #define SWM_D_KEY 0x0200
183 #define SWM_D_QUIRK 0x0400
184 #define SWM_D_SPAWN 0x0800
185 #define SWM_D_EVENTQ 0x1000
186 #define SWM_D_CONF 0x2000
187 #define SWM_D_BAR 0x4000
188 #define SWM_D_INIT 0x8000
190 uint32_t swm_debug = 0
209 #define DPRINTF(x...)
210 #define DNPRINTF(n,x...)
213 #define SWM_EWMH_ACTION_COUNT_MAX (8)
214 #define EWMH_F_FULLSCREEN (0x001)
215 #define EWMH_F_ABOVE (0x002)
216 #define EWMH_F_HIDDEN (0x004)
217 #define EWMH_F_MAXIMIZED_VERT (0x008)
218 #define EWMH_F_MAXIMIZED_HORZ (0x010)
219 #define EWMH_F_SKIP_PAGER (0x020)
220 #define EWMH_F_SKIP_TASKBAR (0x040)
221 #define SWM_F_MANUAL (0x080)
223 #define EWMH_F_MAXIMIZED (EWMH_F_MAXIMIZED_VERT | EWMH_F_MAXIMIZED_HORZ)
225 /* convert 8-bit to 16-bit */
226 #define RGB_8_TO_16(col) (((col) << 8) + (col))
228 #define PIXEL_TO_XRENDERCOLOR(px, xrc) \
229 xrc.red = RGB_8_TO_16((px) >> 16 & 0xff); \
230 xrc.green = RGB_8_TO_16((px) >> 8 & 0xff); \
231 xrc.blue = RGB_8_TO_16((px) & 0xff); \
234 #define LENGTH(x) (int)(sizeof (x) / sizeof (x)[0])
235 #define MODKEY XCB_MOD_MASK_1
236 #define CLEANMASK(mask) ((mask) & ~(numlockmask | XCB_MOD_MASK_LOCK))
237 #define BUTTONMASK (XCB_EVENT_MASK_BUTTON_PRESS | \
238 XCB_EVENT_MASK_BUTTON_RELEASE)
239 #define MOUSEMASK (BUTTONMASK|XCB_EVENT_MASK_POINTER_MOTION)
240 #define SWM_PROPLEN (16)
241 #define SWM_FUNCNAME_LEN (32)
242 #define SWM_KEYS_LEN (255)
243 #define SWM_QUIRK_LEN (64)
244 #define X(r) ((r)->g.x)
245 #define Y(r) ((r)->g.y)
246 #define WIDTH(r) ((r)->g.w)
247 #define HEIGHT(r) ((r)->g.h)
248 #define BORDER(w) ((w)->bordered ? border_width : 0)
249 #define MAX_X(r) ((r)->g.x + (r)->g.w)
250 #define MAX_Y(r) ((r)->g.y + (r)->g.h)
251 #define SH_MIN(w) ((w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE)
252 #define SH_MIN_W(w) ((w)->sh.min_width)
253 #define SH_MIN_H(w) ((w)->sh.min_height)
254 #define SH_MAX(w) ((w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MAX_SIZE)
255 #define SH_MAX_W(w) ((w)->sh.max_width)
256 #define SH_MAX_H(w) ((w)->sh.max_height)
257 #define SH_INC(w) ((w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_RESIZE_INC)
258 #define SH_INC_W(w) ((w)->sh.width_inc)
259 #define SH_INC_H(w) ((w)->sh.height_inc)
260 #define SWM_MAX_FONT_STEPS (3)
261 #define WINID(w) ((w) ? (w)->id : XCB_WINDOW_NONE)
262 #define WS_FOCUSED(ws) ((ws)->r && (ws)->r->s->r_focus == (ws)->r)
263 #define YESNO(x) ((x) ? "yes" : "no")
264 #define ICONIC(w) ((w)->ewmh_flags & EWMH_F_HIDDEN)
265 #define ABOVE(w) ((w)->ewmh_flags & EWMH_F_ABOVE)
266 #define FULLSCREEN(w) ((w)->ewmh_flags & EWMH_F_FULLSCREEN)
267 #define MAXIMIZED_VERT(w) ((w)->ewmh_flags & EWMH_F_MAXIMIZED_VERT)
268 #define MAXIMIZED_HORZ(w) ((w)->ewmh_flags & EWMH_F_MAXIMIZED_HORZ)
269 #define MAXIMIZED(w) (MAXIMIZED_VERT(w) || MAXIMIZED_HORZ(w))
270 #define MANUAL(w) ((w)->ewmh_flags & SWM_F_MANUAL)
271 #define TRANS(w) ((w)->transient != XCB_WINDOW_NONE)
272 #define FLOATING(w) (ABOVE(w) || TRANS(w) || FULLSCREEN(w) || \
275 /* Constrain Window flags */
276 #define SWM_CW_RESIZABLE (0x01)
277 #define SWM_CW_SOFTBOUNDARY (0x02)
278 #define SWM_CW_HARDBOUNDARY (0x04)
279 #define SWM_CW_RIGHT (0x10)
280 #define SWM_CW_LEFT (0x20)
281 #define SWM_CW_BOTTOM (0x40)
282 #define SWM_CW_TOP (0x80)
283 #define SWM_CW_ALLSIDES (0xf0)
285 #define SWM_FOCUS_DEFAULT (0)
286 #define SWM_FOCUS_FOLLOW (1)
287 #define SWM_FOCUS_MANUAL (2)
289 #define SWM_CK_NONE (0)
290 #define SWM_CK_ALL (0xf)
291 #define SWM_CK_FOCUS (0x1)
292 #define SWM_CK_POINTER (0x2)
293 #define SWM_CK_FALLBACK (0x4)
294 #define SWM_CK_REGION (0x8)
296 #define SWM_G_ALL (0xf)
297 #define SWM_G_SIZE (0x1)
298 #define SWM_G_POS (0x2)
300 #define SWM_CONF_DEFAULT (0)
301 #define SWM_CONF_KEYMAPPING (1)
304 #define SWM_LIB "/usr/local/lib/libswmhack.so"
311 xcb_atom_t a_net_wm_check;
312 xcb_atom_t a_net_supported;
313 xcb_atom_t a_takefocus;
314 xcb_atom_t a_utf8_string;
316 volatile sig_atomic_t running = 1;
317 volatile sig_atomic_t restart_wm = 0;
318 xcb_timestamp_t last_event_time = 0;
322 unsigned int numlockmask = 0;
325 xcb_connection_t *conn;
326 xcb_key_symbols_t *syms;
328 int boundary_width = 50;
329 bool cycle_empty = false;
330 bool cycle_visible = false;
332 int font_adjusted = 0;
333 unsigned int mod_key = MODKEY;
334 bool warp_pointer = false;
337 struct swm_region *search_r;
338 int select_list_pipe[2];
339 int select_resp_pipe[2];
341 volatile sig_atomic_t search_resp;
342 int search_resp_action;
344 struct search_window {
345 TAILQ_ENTRY(search_window) entry;
349 xcb_window_t indicator;
351 TAILQ_HEAD(search_winlist, search_window);
352 struct search_winlist search_wl;
357 SWM_SEARCH_UNICONIFY,
358 SWM_SEARCH_NAME_WORKSPACE,
359 SWM_SEARCH_SEARCH_WORKSPACE,
360 SWM_SEARCH_SEARCH_WINDOW
363 #define SWM_STACK_TOP (0)
364 #define SWM_STACK_BOTTOM (1)
365 #define SWM_STACK_ABOVE (2)
366 #define SWM_STACK_BELOW (3)
369 double dialog_ratio = 0.6;
371 #define SWM_BAR_MAX (256)
372 #define SWM_BAR_JUSTIFY_LEFT (0)
373 #define SWM_BAR_JUSTIFY_CENTER (1)
374 #define SWM_BAR_JUSTIFY_RIGHT (2)
375 #define SWM_BAR_OFFSET (4)
376 #define SWM_BAR_FONTS "-*-terminus-medium-*-*-*-12-*-*-*-*-*-*-*," \
377 "-*-profont-*-*-*-*-12-*-*-*-*-*-*-*," \
378 "-*-times-medium-r-*-*-12-*-*-*-*-*-*-*," \
379 "-misc-fixed-medium-r-*-*-12-*-*-*-*-*-*-*," \
380 "-*-*-*-r-*-*-*-*-*-*-*-*-*-*"
382 #ifdef X_HAVE_UTF8_STRING
383 #define DRAWSTRING(x...) Xutf8DrawString(x)
385 #define DRAWSTRING(x...) XmbDrawString(x)
388 char *bar_argv[] = { NULL, NULL };
390 char bar_ext[SWM_BAR_MAX];
391 char bar_ext_buf[SWM_BAR_MAX];
392 char bar_vertext[SWM_BAR_MAX];
393 bool bar_version = false;
394 bool bar_enabled = true;
395 int bar_border_width = 1;
396 bool bar_at_bottom = false;
397 bool bar_extra = false;
399 int bar_justify = SWM_BAR_JUSTIFY_LEFT;
400 char *bar_format = NULL;
401 bool stack_enabled = true;
402 bool clock_enabled = true;
403 bool iconic_enabled = false;
404 bool urgent_enabled = false;
405 char *clock_format = NULL;
406 bool window_class_enabled = false;
407 bool window_instance_enabled = false;
408 bool window_name_enabled = false;
409 int focus_mode = SWM_FOCUS_DEFAULT;
410 int focus_close = SWM_STACK_BELOW;
411 bool focus_close_wrap = true;
412 int focus_default = SWM_STACK_TOP;
413 int spawn_position = SWM_STACK_TOP;
414 bool disable_border = false;
415 int border_width = 1;
416 int region_padding = 0;
418 bool java_workaround = true;
419 bool verbose_layout = false;
425 XFontSetExtents *bar_fs_extents;
427 bool bar_font_legacy = true;
429 XftColor bar_font_color;
430 XftColor search_font_color;
432 char *startup_exception;
433 unsigned int nr_exceptions = 0;
435 /* layout manager data */
436 struct swm_geometry {
449 struct swm_geometry g;
452 /* virtual "screens" */
454 TAILQ_ENTRY(swm_region) entry;
456 struct swm_geometry g;
457 struct workspace *ws; /* current workspace on this region */
458 struct workspace *ws_prior; /* prior workspace on this region */
459 struct swm_screen *s; /* screen idx */
462 TAILQ_HEAD(swm_region_list, swm_region);
465 TAILQ_ENTRY(ws_win) entry;
466 TAILQ_ENTRY(ws_win) stack_entry;
468 xcb_window_t transient;
469 struct ws_win *focus_child; /* focus on child transient */
470 struct swm_geometry g; /* current geometry */
471 struct swm_geometry g_float; /* region coordinates */
472 bool g_floatvalid; /* g_float geometry validity */
476 int font_size_boundary[SWM_MAX_FONT_STEPS];
483 struct workspace *ws; /* always valid */
484 struct swm_screen *s; /* always valid, never changes */
486 xcb_icccm_get_wm_class_reply_t ch;
487 xcb_icccm_wm_hints_t hints;
489 TAILQ_HEAD(ws_win_list, ws_win);
490 TAILQ_HEAD(ws_win_stack, ws_win);
494 TAILQ_ENTRY(pid_e) entry;
498 TAILQ_HEAD(pid_list, pid_e);
499 struct pid_list pidlist = TAILQ_HEAD_INITIALIZER(pidlist);
501 /* layout handlers */
503 void vertical_config(struct workspace *, int);
504 void vertical_stack(struct workspace *, struct swm_geometry *);
505 void horizontal_config(struct workspace *, int);
506 void horizontal_stack(struct workspace *, struct swm_geometry *);
507 void max_stack(struct workspace *, struct swm_geometry *);
508 void plain_stacker(struct workspace *);
509 void fancy_stacker(struct workspace *);
512 void (*l_stack)(struct workspace *, struct swm_geometry *);
513 void (*l_config)(struct workspace *, int);
515 #define SWM_L_FOCUSPREV (1<<0)
516 #define SWM_L_MAPONFOCUS (1<<1)
517 void (*l_string)(struct workspace *);
519 /* stack, configure */
520 { vertical_stack, vertical_config, 0, plain_stacker },
521 { horizontal_stack, horizontal_config, 0, plain_stacker },
523 SWM_L_MAPONFOCUS | SWM_L_FOCUSPREV, plain_stacker },
524 { NULL, NULL, 0, NULL },
527 /* position of max_stack mode in the layouts array, index into layouts! */
528 #define SWM_V_STACK (0)
529 #define SWM_H_STACK (1)
530 #define SWM_MAX_STACK (2)
532 #define SWM_H_SLICE (32)
533 #define SWM_V_SLICE (32)
535 /* define work spaces */
537 int idx; /* workspace index */
538 char *name; /* workspace name */
539 bool always_raise; /* raise windows on focus */
540 bool bar_enabled; /* bar visibility */
541 struct layout *cur_layout; /* current layout handlers */
542 struct ws_win *focus; /* may be NULL */
543 struct ws_win *focus_prev; /* may be NULL */
544 struct ws_win *focus_pending; /* may be NULL */
545 struct swm_region *r; /* may be NULL */
546 struct swm_region *old_r; /* may be NULL */
547 struct ws_win_list winlist; /* list of windows in ws */
548 struct ws_win_list unmanagedlist; /* list of dead windows in ws */
549 struct ws_win_stack stack; /* stacking order */
550 int state; /* mapping state */
551 char stacker[10]; /* display stacker and layout */
555 int horizontal_msize;
557 int horizontal_stacks;
558 bool horizontal_flip;
568 SWM_WS_STATE_MAPPING,
574 SWM_S_COLOR_BAR_BORDER,
575 SWM_S_COLOR_BAR_BORDER_UNFOCUS,
576 SWM_S_COLOR_BAR_FONT,
578 SWM_S_COLOR_FOCUS_MAXIMIZED,
580 SWM_S_COLOR_UNFOCUS_MAXIMIZED,
584 /* physical screen mapping */
585 #define SWM_WS_MAX (22) /* hard limit */
586 int workspace_limit = 10; /* soft limit */
589 int idx; /* screen index */
590 struct swm_region_list rl; /* list of regions on this screen */
591 struct swm_region_list orl; /* list of old regions */
593 struct workspace ws[SWM_WS_MAX];
594 struct swm_region *r_focus;
601 } c[SWM_S_COLOR_MAX];
603 xcb_gcontext_t bar_gc;
606 struct swm_screen *screens;
608 /* args to functions */
611 #define SWM_ARG_ID_FOCUSNEXT (0)
612 #define SWM_ARG_ID_FOCUSPREV (1)
613 #define SWM_ARG_ID_FOCUSMAIN (2)
614 #define SWM_ARG_ID_FOCUSURGENT (3)
615 #define SWM_ARG_ID_SWAPNEXT (10)
616 #define SWM_ARG_ID_SWAPPREV (11)
617 #define SWM_ARG_ID_SWAPMAIN (12)
618 #define SWM_ARG_ID_MOVELAST (13)
619 #define SWM_ARG_ID_MASTERSHRINK (20)
620 #define SWM_ARG_ID_MASTERGROW (21)
621 #define SWM_ARG_ID_MASTERADD (22)
622 #define SWM_ARG_ID_MASTERDEL (23)
623 #define SWM_ARG_ID_FLIPLAYOUT (24)
624 #define SWM_ARG_ID_STACKRESET (30)
625 #define SWM_ARG_ID_STACKINIT (31)
626 #define SWM_ARG_ID_CYCLEWS_UP (40)
627 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
628 #define SWM_ARG_ID_CYCLERG_UP (42)
629 #define SWM_ARG_ID_CYCLERG_DOWN (43)
630 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
631 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
632 #define SWM_ARG_ID_CYCLEWS_MOVE_UP (46)
633 #define SWM_ARG_ID_CYCLEWS_MOVE_DOWN (47)
634 #define SWM_ARG_ID_STACKINC (50)
635 #define SWM_ARG_ID_STACKDEC (51)
636 #define SWM_ARG_ID_SS_ALL (60)
637 #define SWM_ARG_ID_SS_WINDOW (61)
638 #define SWM_ARG_ID_DONTCENTER (70)
639 #define SWM_ARG_ID_CENTER (71)
640 #define SWM_ARG_ID_KILLWINDOW (80)
641 #define SWM_ARG_ID_DELETEWINDOW (81)
642 #define SWM_ARG_ID_WIDTHGROW (90)
643 #define SWM_ARG_ID_WIDTHSHRINK (91)
644 #define SWM_ARG_ID_HEIGHTGROW (92)
645 #define SWM_ARG_ID_HEIGHTSHRINK (93)
646 #define SWM_ARG_ID_MOVEUP (100)
647 #define SWM_ARG_ID_MOVEDOWN (101)
648 #define SWM_ARG_ID_MOVELEFT (102)
649 #define SWM_ARG_ID_MOVERIGHT (103)
650 #define SWM_ARG_ID_RAISE (105)
651 #define SWM_ARG_ID_LOWER (106)
652 #define SWM_ARG_ID_BAR_TOGGLE (110)
653 #define SWM_ARG_ID_BAR_TOGGLE_WS (111)
659 TAILQ_ENTRY(quirk) entry;
660 char *class; /* WM_CLASS:class */
661 char *instance; /* WM_CLASS:instance */
662 char *name; /* WM_NAME */
664 regex_t regex_instance;
667 int ws; /* Initial workspace. */
668 #define SWM_Q_FLOAT (1<<0) /* float this window */
669 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
670 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
671 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
672 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
673 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
674 #define SWM_Q_NOFOCUSONMAP (1<<6) /* Don't focus on window when mapped. */
675 #define SWM_Q_FOCUSONMAP_SINGLE (1<<7) /* Only focus if single win of type. */
676 #define SWM_Q_OBEYAPPFOCUSREQ (1<<8) /* Focus when applications ask. */
677 #define SWM_Q_IGNOREPID (1<<9) /* Ignore PID when determining ws. */
678 #define SWM_Q_IGNORESPAWNWS (1<<10) /* Ignore _SWM_WS when managing win. */
680 TAILQ_HEAD(quirk_list, quirk);
681 struct quirk_list quirks = TAILQ_HEAD_INITIALIZER(quirks);
684 * Supported EWMH hints should be added to
685 * both the enum and the ewmh array
691 _NET_CURRENT_DESKTOP,
692 _NET_DESKTOP_GEOMETRY,
694 _NET_DESKTOP_VIEWPORT,
695 _NET_MOVERESIZE_WINDOW,
696 _NET_NUMBER_OF_DESKTOPS,
698 _NET_WM_ACTION_ABOVE,
699 _NET_WM_ACTION_CLOSE,
700 _NET_WM_ACTION_FULLSCREEN,
702 _NET_WM_ACTION_RESIZE,
703 _NET_WM_ALLOWED_ACTIONS,
705 _NET_WM_FULL_PLACEMENT,
709 _NET_WM_STATE_FULLSCREEN,
710 _NET_WM_STATE_HIDDEN,
711 _NET_WM_STATE_MAXIMIZED_VERT,
712 _NET_WM_STATE_MAXIMIZED_HORZ,
713 _NET_WM_STATE_SKIP_PAGER,
714 _NET_WM_STATE_SKIP_TASKBAR,
716 _NET_WM_WINDOW_TYPE_DIALOG,
717 _NET_WM_WINDOW_TYPE_DOCK,
718 _NET_WM_WINDOW_TYPE_NORMAL,
719 _NET_WM_WINDOW_TYPE_SPLASH,
720 _NET_WM_WINDOW_TYPE_TOOLBAR,
721 _NET_WM_WINDOW_TYPE_UTILITY,
722 _SWM_WM_STATE_MANUAL,
729 } ewmh[SWM_EWMH_HINT_MAX] = {
730 /* must be in same order as in the enum */
731 {"_NET_ACTIVE_WINDOW", XCB_ATOM_NONE},
732 {"_NET_CLIENT_LIST", XCB_ATOM_NONE},
733 {"_NET_CLOSE_WINDOW", XCB_ATOM_NONE},
734 {"_NET_CURRENT_DESKTOP", XCB_ATOM_NONE},
735 {"_NET_DESKTOP_GEOMETRY", XCB_ATOM_NONE},
736 {"_NET_DESKTOP_NAMES", XCB_ATOM_NONE},
737 {"_NET_DESKTOP_VIEWPORT", XCB_ATOM_NONE},
738 {"_NET_MOVERESIZE_WINDOW", XCB_ATOM_NONE},
739 {"_NET_NUMBER_OF_DESKTOPS", XCB_ATOM_NONE},
740 {"_NET_RESTACK_WINDOW", XCB_ATOM_NONE},
741 {"_NET_WM_ACTION_ABOVE", XCB_ATOM_NONE},
742 {"_NET_WM_ACTION_CLOSE", XCB_ATOM_NONE},
743 {"_NET_WM_ACTION_FULLSCREEN", XCB_ATOM_NONE},
744 {"_NET_WM_ACTION_MOVE", XCB_ATOM_NONE},
745 {"_NET_WM_ACTION_RESIZE", XCB_ATOM_NONE},
746 {"_NET_WM_ALLOWED_ACTIONS", XCB_ATOM_NONE},
747 {"_NET_WM_DESKTOP", XCB_ATOM_NONE},
748 {"_NET_WM_FULL_PLACEMENT", XCB_ATOM_NONE},
749 {"_NET_WM_NAME", XCB_ATOM_NONE},
750 {"_NET_WM_STATE", XCB_ATOM_NONE},
751 {"_NET_WM_STATE_ABOVE", XCB_ATOM_NONE},
752 {"_NET_WM_STATE_FULLSCREEN", XCB_ATOM_NONE},
753 {"_NET_WM_STATE_HIDDEN", XCB_ATOM_NONE},
754 {"_NET_WM_STATE_MAXIMIZED_VERT", XCB_ATOM_NONE},
755 {"_NET_WM_STATE_MAXIMIZED_HORZ", XCB_ATOM_NONE},
756 {"_NET_WM_STATE_SKIP_PAGER", XCB_ATOM_NONE},
757 {"_NET_WM_STATE_SKIP_TASKBAR", XCB_ATOM_NONE},
758 {"_NET_WM_WINDOW_TYPE", XCB_ATOM_NONE},
759 {"_NET_WM_WINDOW_TYPE_DIALOG", XCB_ATOM_NONE},
760 {"_NET_WM_WINDOW_TYPE_DOCK", XCB_ATOM_NONE},
761 {"_NET_WM_WINDOW_TYPE_NORMAL", XCB_ATOM_NONE},
762 {"_NET_WM_WINDOW_TYPE_SPLASH", XCB_ATOM_NONE},
763 {"_NET_WM_WINDOW_TYPE_TOOLBAR", XCB_ATOM_NONE},
764 {"_NET_WM_WINDOW_TYPE_UTILITY", XCB_ATOM_NONE},
765 {"_SWM_WM_STATE_MANUAL", XCB_ATOM_NONE},
768 /* EWMH source type */
770 EWMH_SOURCE_TYPE_NONE = 0,
771 EWMH_SOURCE_TYPE_NORMAL = 1,
772 EWMH_SOURCE_TYPE_OTHER = 2,
779 XC_BOTTOM_LEFT_CORNER,
780 XC_BOTTOM_RIGHT_CORNER,
788 char *name; /* Name used by Xcursor .*/
789 uint8_t cf_char; /* cursorfont index. */
791 } cursors[XC_MAX] = {
792 {"fleur", XC_fleur, XCB_CURSOR_NONE},
793 {"left_ptr", XC_left_ptr, XCB_CURSOR_NONE},
794 {"bottom_left_corner", XC_bottom_left_corner, XCB_CURSOR_NONE},
795 {"bottom_right_corner", XC_bottom_right_corner, XCB_CURSOR_NONE},
796 {"sizing", XC_sizing, XCB_CURSOR_NONE},
797 {"top_left_corner", XC_top_left_corner, XCB_CURSOR_NONE},
798 {"top_right_corner", XC_top_right_corner, XCB_CURSOR_NONE},
801 #define SWM_SPAWN_OPTIONAL 0x1
805 TAILQ_ENTRY(spawn_prog) entry;
811 TAILQ_HEAD(spawn_list, spawn_prog);
812 struct spawn_list spawns = TAILQ_HEAD_INITIALIZER(spawns);
814 /* user/key callable function IDs */
930 KF_DUMPWINS, /* MUST BE LAST */
938 enum keyfuncid funcid;
941 RB_HEAD(key_tree, key);
943 /* function prototypes */
944 void adjust_font(struct ws_win *);
945 char *argsep(char **);
946 void bar_cleanup(struct swm_region *);
947 void bar_extra_setup(void);
948 void bar_extra_stop(void);
949 int bar_extra_update(void);
950 void bar_fmt(const char *, char *, struct swm_region *, size_t);
951 void bar_fmt_expand(char *, size_t);
953 void bar_print(struct swm_region *, const char *);
954 void bar_print_legacy(struct swm_region *, const char *);
955 void bar_replace(char *, char *, struct swm_region *, size_t);
956 void bar_replace_pad(char *, int *, size_t);
957 char *bar_replace_seq(char *, char *, struct swm_region *, size_t *, size_t);
958 void bar_setup(struct swm_region *);
959 void bar_toggle(struct swm_region *, union arg *);
960 void bar_urgent(char *, size_t);
961 void bar_window_class(char *, size_t, struct swm_region *);
962 void bar_window_class_instance(char *, size_t, struct swm_region *);
963 void bar_window_float(char *, size_t, struct swm_region *);
964 void bar_window_instance(char *, size_t, struct swm_region *);
965 void bar_window_name(char *, size_t, struct swm_region *);
966 void bar_window_state(char *, size_t, struct swm_region *);
967 void bar_workspace_name(char *, size_t, struct swm_region *);
968 void buttonpress(xcb_button_press_event_t *);
969 void center_pointer(struct swm_region *);
970 void check_conn(void);
971 void clear_keys(void);
972 int clear_maximized(struct workspace *);
973 void clientmessage(xcb_client_message_event_t *);
974 void client_msg(struct ws_win *, xcb_atom_t, xcb_timestamp_t);
975 int conf_load(const char *, int);
976 void configurenotify(xcb_configure_notify_event_t *);
977 void configurerequest(xcb_configure_request_event_t *);
978 void config_win(struct ws_win *, xcb_configure_request_event_t *);
979 void constrain_window(struct ws_win *, struct swm_geometry *, int *);
980 int count_win(struct workspace *, bool);
981 void cursors_cleanup(void);
982 void cursors_load(void);
983 void custom_region(const char *);
984 void cyclerg(struct swm_region *, union arg *);
985 void cyclews(struct swm_region *, union arg *);
986 void cycle_layout(struct swm_region *, union arg *);
987 void destroynotify(xcb_destroy_notify_event_t *);
988 void dumpwins(struct swm_region *, union arg *);
990 void enternotify(xcb_enter_notify_event_t *);
991 void event_drain(uint8_t);
992 void event_error(xcb_generic_error_t *);
993 void event_handle(xcb_generic_event_t *);
994 void ewmh_apply_flags(struct ws_win *, uint32_t);
995 void ewmh_autoquirk(struct ws_win *);
996 void ewmh_get_desktop_names(void);
997 void ewmh_get_wm_state(struct ws_win *);
998 void ewmh_update_actions(struct ws_win *);
999 void ewmh_update_client_list(void);
1000 void ewmh_update_current_desktop(void);
1001 void ewmh_update_desktop_names(void);
1002 void ewmh_update_desktops(void);
1003 void ewmh_change_wm_state(struct ws_win *, xcb_atom_t, long);
1004 void ewmh_update_wm_state(struct ws_win *);
1005 char *expand_tilde(const char *);
1006 void expose(xcb_expose_event_t *);
1007 void fake_keypress(struct ws_win *, xcb_keysym_t, uint16_t);
1008 struct pid_e *find_pid(pid_t);
1009 struct ws_win *find_unmanaged_window(xcb_window_t);
1010 struct ws_win *find_window(xcb_window_t);
1011 void floating_toggle(struct swm_region *, union arg *);
1012 void focus(struct swm_region *, union arg *);
1014 void focusin(xcb_focus_in_event_t *);
1015 void focusout(xcb_focus_out_event_t *);
1017 void focus_flush(void);
1018 void focus_region(struct swm_region *);
1019 void focusrg(struct swm_region *, union arg *);
1020 void focus_win(struct ws_win *);
1021 void fontset_init(void);
1022 void free_window(struct ws_win *);
1023 xcb_atom_t get_atom_from_string(const char *);
1025 char *get_atom_name(xcb_atom_t);
1027 struct ws_win *get_focus_magic(struct ws_win *);
1028 struct ws_win *get_focus_prev(struct ws_win *);
1030 char *get_notify_detail_label(uint8_t);
1031 char *get_notify_mode_label(uint8_t);
1033 struct ws_win *get_pointer_win(xcb_window_t);
1034 struct ws_win *get_region_focus(struct swm_region *);
1035 int get_region_index(struct swm_region *);
1036 xcb_screen_t *get_screen(int);
1037 int get_screen_count(void);
1039 char *get_source_type_label(uint32_t);
1040 char *get_stack_mode_name(uint8_t);
1042 int32_t get_swm_ws(xcb_window_t);
1043 char *get_win_name(xcb_window_t);
1044 uint8_t get_win_state(xcb_window_t);
1045 void get_wm_protocols(struct ws_win *);
1046 int get_ws_idx(struct ws_win *);
1047 void grabbuttons(struct ws_win *);
1048 void grabkeys(void);
1049 void grab_windows(void);
1050 void iconify(struct swm_region *, union arg *);
1051 bool isxlfd(char *);
1052 void keypress(xcb_key_press_event_t *);
1053 int key_cmp(struct key *, struct key *);
1054 void key_insert(unsigned int, KeySym, enum keyfuncid, const char *);
1055 struct key *key_lookup(unsigned int, KeySym);
1056 void key_remove(struct key *);
1057 void key_replace(struct key *, unsigned int, KeySym, enum keyfuncid,
1059 void kill_bar_extra_atexit(void);
1060 void kill_refs(struct ws_win *);
1062 void leavenotify(xcb_leave_notify_event_t *);
1064 void load_float_geom(struct ws_win *);
1065 struct ws_win *manage_window(xcb_window_t, int, bool);
1066 void map_window(struct ws_win *);
1067 void mapnotify(xcb_map_notify_event_t *);
1068 void mappingnotify(xcb_mapping_notify_event_t *);
1069 void maprequest(xcb_map_request_event_t *);
1070 void maximize_toggle(struct swm_region *, union arg *);
1071 void motionnotify(xcb_motion_notify_event_t *);
1072 void move(struct ws_win *, union arg *);
1073 void move_step(struct swm_region *, union arg *);
1074 uint32_t name_to_pixel(int, const char *);
1075 void name_workspace(struct swm_region *, union arg *);
1076 void new_region(struct swm_screen *, int, int, int, int);
1077 int parsekeys(const char *, unsigned int, unsigned int *, KeySym *);
1078 int parsequirks(const char *, uint32_t *, int *);
1079 int parse_rgb(const char *, uint16_t *, uint16_t *, uint16_t *);
1080 void pressbutton(struct swm_region *, union arg *);
1081 void priorws(struct swm_region *, union arg *);
1083 void print_win_geom(xcb_window_t);
1085 void propertynotify(xcb_property_notify_event_t *);
1086 void quirk_free(struct quirk *);
1087 void quirk_insert(const char *, const char *, const char *, uint32_t, int);
1088 void quirk_remove(struct quirk *);
1089 void quirk_replace(struct quirk *, const char *, const char *, const char *,
1091 void quit(struct swm_region *, union arg *);
1092 void raise_toggle(struct swm_region *, union arg *);
1093 void raise_window(struct ws_win *);
1094 void region_containment(struct ws_win *, struct swm_region *, int);
1095 struct swm_region *region_under(struct swm_screen *, int, int);
1096 void regionize(struct ws_win *, int, int);
1097 void resize(struct ws_win *, union arg *);
1098 void resize_step(struct swm_region *, union arg *);
1099 void restart(struct swm_region *, union arg *);
1100 struct swm_region *root_to_region(xcb_window_t, int);
1101 void screenchange(xcb_randr_screen_change_notify_event_t *);
1102 void scan_randr(int);
1103 void search_do_resp(void);
1104 void search_resp_name_workspace(const char *, size_t);
1105 void search_resp_search_window(const char *);
1106 void search_resp_search_workspace(const char *);
1107 void search_resp_uniconify(const char *, size_t);
1108 void search_win(struct swm_region *, union arg *);
1109 void search_win_cleanup(void);
1110 void search_workspace(struct swm_region *, union arg *);
1111 void send_to_rg(struct swm_region *, union arg *);
1112 void send_to_ws(struct swm_region *, union arg *);
1113 void set_region(struct swm_region *);
1114 int setautorun(const char *, const char *, int);
1115 int setconfbinding(const char *, const char *, int);
1116 int setconfcolor(const char *, const char *, int);
1117 int setconfmodkey(const char *, const char *, int);
1118 int setconfquirk(const char *, const char *, int);
1119 int setconfregion(const char *, const char *, int);
1120 int setconfspawn(const char *, const char *, int);
1121 int setconfvalue(const char *, const char *, int);
1122 void setkeybinding(unsigned int, KeySym, enum keyfuncid, const char *);
1123 int setkeymapping(const char *, const char *, int);
1124 int setlayout(const char *, const char *, int);
1125 void setquirk(const char *, const char *, const char *, uint32_t, int);
1126 void setscreencolor(const char *, int, int);
1127 void setspawn(const char *, const char *, int);
1128 void setup_ewmh(void);
1129 void setup_globals(void);
1130 void setup_keys(void);
1131 void setup_quirks(void);
1132 void setup_screens(void);
1133 void setup_spawn(void);
1134 void set_child_transient(struct ws_win *, xcb_window_t *);
1135 void set_win_state(struct ws_win *, uint8_t);
1136 void shutdown_cleanup(void);
1138 void socket_setnonblock(int);
1139 void sort_windows(struct ws_win_list *);
1140 void spawn(int, union arg *, bool);
1141 void spawn_custom(struct swm_region *, union arg *, const char *);
1142 int spawn_expand(struct swm_region *, union arg *, const char *, char ***);
1143 void spawn_insert(const char *, const char *, int);
1144 struct spawn_prog *spawn_find(const char *);
1145 void spawn_remove(struct spawn_prog *);
1146 void spawn_replace(struct spawn_prog *, const char *, const char *, int);
1147 void spawn_select(struct swm_region *, union arg *, const char *, int *);
1148 void stack_config(struct swm_region *, union arg *);
1149 void stack_master(struct workspace *, struct swm_geometry *, int, bool);
1150 void store_float_geom(struct ws_win *);
1151 char *strdupsafe(const char *);
1152 void swapwin(struct swm_region *, union arg *);
1153 void switchws(struct swm_region *, union arg *);
1154 void teardown_ewmh(void);
1155 void unescape_selector(char *);
1156 void unfocus_win(struct ws_win *);
1157 void uniconify(struct swm_region *, union arg *);
1158 void unmanage_window(struct ws_win *);
1159 void unmapnotify(xcb_unmap_notify_event_t *);
1160 void unmap_all(void);
1161 void unmap_window(struct ws_win *);
1162 void updatenumlockmask(void);
1163 void update_floater(struct ws_win *);
1164 void update_modkey(unsigned int);
1165 void update_win_stacking(struct ws_win *);
1166 void update_window(struct ws_win *);
1167 void update_window_color(struct ws_win *);
1168 void update_wm_state(struct ws_win *win);
1169 void validate_spawns(void);
1170 int validate_win(struct ws_win *);
1171 int validate_ws(struct workspace *);
1172 void version(struct swm_region *, union arg *);
1173 void win_to_ws(struct ws_win *, int, bool);
1174 pid_t window_get_pid(xcb_window_t);
1175 void wkill(struct swm_region *, union arg *);
1176 void update_ws_stack(struct workspace *);
1177 void xft_init(struct swm_region *);
1178 void _add_startup_exception(const char *, va_list);
1179 void add_startup_exception(const char *, ...);
1181 RB_PROTOTYPE(key_tree, key, entry, key_cmp);
1182 RB_GENERATE(key_tree, key, entry, key_cmp);
1183 struct key_tree keys;
1188 xcb_font_t cf = XCB_NONE;
1191 for (i = 0; i < LENGTH(cursors); ++i) {
1192 /* try to load Xcursor first. */
1193 cursors[i].cid = XcursorLibraryLoadCursor(display,
1196 /* fallback to cursorfont. */
1197 if (cursors[i].cid == XCB_CURSOR_NONE) {
1198 if (cf == XCB_NONE) {
1199 cf = xcb_generate_id(conn);
1200 xcb_open_font(conn, cf, strlen("cursor"),
1204 cursors[i].cid = xcb_generate_id(conn);
1205 xcb_create_glyph_cursor(conn, cursors[i].cid, cf, cf,
1206 cursors[i].cf_char, cursors[i].cf_char + 1, 0, 0, 0,
1207 0xffff, 0xffff, 0xffff);
1213 xcb_close_font(conn, cf);
1217 cursors_cleanup(void)
1220 for (i = 0; i < LENGTH(cursors); ++i)
1221 xcb_free_cursor(conn, cursors[i].cid);
1225 expand_tilde(const char *s)
1227 struct passwd *ppwd;
1235 errx(1, "expand_tilde: NULL string.");
1238 result = strdup(sc);
1244 if ((max = sysconf(_SC_LOGIN_NAME_MAX)) == -1)
1245 errx(1, "expand_tilde: sysconf");
1247 if ((user = calloc(1, max + 1)) == NULL)
1248 errx(1, "expand_tilde: calloc");
1250 for (i = 0; s[i] != '/' && s[i] != '\0'; ++i)
1255 ppwd = strlen(user) == 0 ? getpwuid(getuid()) : getpwnam(user);
1259 result = strdup(sc);
1261 if (asprintf(&result, "%s%s", ppwd->pw_dir, s) == -1)
1265 errx(1, "expand_tilde: failed to allocate memory.");
1271 parse_rgb(const char *rgb, uint16_t *rr, uint16_t *gg, uint16_t *bb)
1273 unsigned int tmpr, tmpg, tmpb;
1275 if (sscanf(rgb, "rgb:%x/%x/%x", &tmpr, &tmpg, &tmpb) != 3)
1278 *rr = RGB_8_TO_16(tmpr);
1279 *gg = RGB_8_TO_16(tmpg);
1280 *bb = RGB_8_TO_16(tmpb);
1286 get_screen(int screen)
1288 const xcb_setup_t *r;
1289 xcb_screen_iterator_t iter;
1291 if ((r = xcb_get_setup(conn)) == NULL) {
1292 DNPRINTF(SWM_D_MISC, "get_screen: xcb_get_setup\n");
1296 iter = xcb_setup_roots_iterator(r);
1297 for (; iter.rem; --screen, xcb_screen_next(&iter))
1305 get_screen_count(void)
1307 const xcb_setup_t *r;
1309 if ((r = xcb_get_setup(conn)) == NULL) {
1310 DNPRINTF(SWM_D_MISC, "get_screen_count: xcb_get_setup\n");
1314 return xcb_setup_roots_length(r);
1318 get_region_index(struct swm_region *r)
1320 struct swm_region *rr;
1326 TAILQ_FOREACH(rr, &r->s->rl, entry) {
1341 if (focus_mode == SWM_FOCUS_DEFAULT)
1342 event_drain(XCB_ENTER_NOTIFY);
1348 get_atom_from_string(const char *str)
1350 xcb_intern_atom_cookie_t c;
1351 xcb_intern_atom_reply_t *r;
1354 c = xcb_intern_atom(conn, 0, strlen(str), str);
1355 r = xcb_intern_atom_reply(conn, c, NULL);
1363 return (XCB_ATOM_NONE);
1367 get_wm_protocols(struct ws_win *win) {
1369 xcb_icccm_get_wm_protocols_reply_t wpr;
1371 if (xcb_icccm_get_wm_protocols_reply(conn,
1372 xcb_icccm_get_wm_protocols(conn, win->id, a_prot),
1374 for (i = 0; i < (int)wpr.atoms_len; i++) {
1375 if (wpr.atoms[i] == a_takefocus)
1376 win->take_focus = true;
1377 if (wpr.atoms[i] == a_delete)
1378 win->can_delete = true;
1380 xcb_icccm_get_wm_protocols_reply_wipe(&wpr);
1387 xcb_window_t root, win;
1388 int i, j, num_screens;
1390 for (i = 0; i < LENGTH(ewmh); i++)
1391 ewmh[i].atom = get_atom_from_string(ewmh[i].name);
1393 num_screens = get_screen_count();
1394 for (i = 0; i < num_screens; i++) {
1395 root = screens[i].root;
1397 /* Set up _NET_SUPPORTING_WM_CHECK. */
1398 win = xcb_generate_id(conn);
1399 xcb_create_window(conn, XCB_COPY_FROM_PARENT, win, root,
1400 0, 0, 1, 1, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT,
1401 XCB_COPY_FROM_PARENT, 0, NULL);
1403 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root,
1404 a_net_wm_check, XCB_ATOM_WINDOW, 32, 1, &win);
1405 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win,
1406 a_net_wm_check, XCB_ATOM_WINDOW, 32, 1, &win);
1409 * Impersonate LG3D non-reparenting WM, written by Sun, to
1410 * workaround a Java GUI rendering issue.
1412 if (java_workaround)
1413 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win,
1414 ewmh[_NET_WM_NAME].atom, a_utf8_string,
1415 8, strlen("LG3D"), "LG3D");
1417 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win,
1418 ewmh[_NET_WM_NAME].atom, a_utf8_string,
1419 8, strlen("spectrwm"), "spectrwm");
1421 /* Report supported atoms */
1422 xcb_delete_property(conn, root, a_net_supported);
1423 for (j = 0; j < LENGTH(ewmh); j++)
1424 xcb_change_property(conn, XCB_PROP_MODE_APPEND, root,
1425 a_net_supported, XCB_ATOM_ATOM, 32, 1,
1430 ewmh_update_desktops();
1431 ewmh_get_desktop_names();
1439 xcb_get_property_cookie_t pc;
1440 xcb_get_property_reply_t *pr;
1442 num_screens = get_screen_count();
1444 for (i = 0; i < num_screens; i++) {
1445 /* Get the support check window and destroy it */
1446 pc = xcb_get_property(conn, 0, screens[i].root, a_net_wm_check,
1447 XCB_ATOM_WINDOW, 0, 1);
1448 pr = xcb_get_property_reply(conn, pc, NULL);
1451 if (pr->format == a_net_wm_check) {
1452 id = *((xcb_window_t *)xcb_get_property_value(pr));
1454 xcb_destroy_window(conn, id);
1455 xcb_delete_property(conn, screens[i].root,
1457 xcb_delete_property(conn, screens[i].root,
1465 ewmh_autoquirk(struct ws_win *win)
1467 xcb_get_property_reply_t *r;
1468 xcb_get_property_cookie_t c;
1472 c = xcb_get_property(conn, 0, win->id,
1473 ewmh[_NET_WM_WINDOW_TYPE].atom, XCB_ATOM_ATOM, 0, UINT32_MAX);
1474 r = xcb_get_property_reply(conn, c, NULL);
1478 type = xcb_get_property_value(r);
1479 n = xcb_get_property_value_length(r) / sizeof(xcb_atom_t);
1481 for (i = 0; i < n; i++) {
1482 if (type[i] == ewmh[_NET_WM_WINDOW_TYPE_NORMAL].atom)
1484 if (type[i] == ewmh[_NET_WM_WINDOW_TYPE_DOCK].atom ||
1485 type[i] == ewmh[_NET_WM_WINDOW_TYPE_TOOLBAR].atom ||
1486 type[i] == ewmh[_NET_WM_WINDOW_TYPE_UTILITY].atom) {
1487 win->quirks = SWM_Q_FLOAT | SWM_Q_ANYWHERE;
1490 if (type[i] == ewmh[_NET_WM_WINDOW_TYPE_SPLASH].atom ||
1491 type[i] == ewmh[_NET_WM_WINDOW_TYPE_DIALOG].atom) {
1492 win->quirks = SWM_Q_FLOAT;
1500 ewmh_update_actions(struct ws_win *win)
1502 xcb_atom_t actions[SWM_EWMH_ACTION_COUNT_MAX];
1508 actions[n++] = ewmh[_NET_WM_ACTION_CLOSE].atom;
1511 actions[n++] = ewmh[_NET_WM_ACTION_MOVE].atom;
1512 actions[n++] = ewmh[_NET_WM_ACTION_RESIZE].atom;
1513 actions[n++] = ewmh[_NET_WM_ACTION_ABOVE].atom;
1516 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
1517 ewmh[_NET_WM_ALLOWED_ACTIONS].atom, XCB_ATOM_ATOM, 32, 1, actions);
1520 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
1521 #define _NET_WM_STATE_ADD 1 /* add/set property */
1522 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
1525 ewmh_change_wm_state(struct ws_win *win, xcb_atom_t state, long action)
1532 name = get_atom_name(state);
1533 DNPRINTF(SWM_D_PROP, "ewmh_change_wm_state: win %#x, state: %s, "
1534 "action: %ld\n", WINID(win), name, action);
1540 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
1541 flag = EWMH_F_FULLSCREEN;
1542 else if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
1543 flag = EWMH_F_ABOVE;
1544 else if (state == ewmh[_NET_WM_STATE_HIDDEN].atom)
1545 flag = EWMH_F_HIDDEN;
1546 else if (state == ewmh[_NET_WM_STATE_MAXIMIZED_VERT].atom ||
1547 state == ewmh[_NET_WM_STATE_MAXIMIZED_HORZ].atom)
1548 flag = EWMH_F_MAXIMIZED;
1549 else if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
1550 flag = SWM_F_MANUAL;
1551 else if (state == ewmh[_NET_WM_STATE_SKIP_PAGER].atom)
1552 flag = EWMH_F_SKIP_PAGER;
1553 else if (state == ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom)
1554 flag = EWMH_F_SKIP_TASKBAR;
1556 /* Disallow unfloating transients. */
1557 if (TRANS(win) && flag == EWMH_F_ABOVE)
1560 new_flags = win->ewmh_flags;
1563 case _NET_WM_STATE_REMOVE:
1566 case _NET_WM_STATE_ADD:
1569 case _NET_WM_STATE_TOGGLE:
1574 ewmh_apply_flags(win, new_flags);
1577 DNPRINTF(SWM_D_PROP, "ewmh_change_wm_state: done.\n");
1581 ewmh_apply_flags(struct ws_win *win, uint32_t pending)
1583 struct workspace *ws;
1586 changed = win->ewmh_flags ^ pending;
1590 DNPRINTF(SWM_D_PROP, "ewmh_apply_flags: pending: %d\n", pending);
1592 win->ewmh_flags = pending;
1595 if (changed & EWMH_F_HIDDEN) {
1597 if (focus_mode != SWM_FOCUS_FOLLOW)
1598 ws->focus_pending = get_focus_prev(win);
1603 /* Reload floating geometry in case region changed. */
1605 load_float_geom(win);
1607 /* The window is no longer iconic, prepare focus. */
1608 if (focus_mode != SWM_FOCUS_FOLLOW)
1609 ws->focus_pending = get_focus_magic(win);
1614 if (changed & EWMH_F_ABOVE) {
1615 if (ws->cur_layout != &layouts[SWM_MAX_STACK]) {
1617 load_float_geom(win);
1618 else if (!MAXIMIZED(win))
1619 store_float_geom(win);
1621 win->ewmh_flags &= ~EWMH_F_MAXIMIZED;
1622 changed &= ~EWMH_F_MAXIMIZED;
1626 win->ewmh_flags ^= EWMH_F_ABOVE & pending;
1630 if (changed & EWMH_F_MAXIMIZED) {
1631 /* VERT and/or HORZ changed. */
1633 if (!MAXIMIZED(win))
1634 load_float_geom(win);
1636 store_float_geom(win);
1639 if (MAXIMIZED(win)) {
1640 if (focus_mode != SWM_FOCUS_FOLLOW &&
1641 ws->cur_layout != &layouts[SWM_MAX_STACK]) {
1645 ws->focus_pending = win;
1649 update_window_color(win);
1653 if (changed & EWMH_F_FULLSCREEN) {
1654 if (FULLSCREEN(win)) {
1655 if (focus_mode != SWM_FOCUS_FOLLOW) {
1659 ws->focus_pending = win;
1662 load_float_geom(win);
1665 win->ewmh_flags &= ~EWMH_F_MAXIMIZED;
1669 DNPRINTF(SWM_D_PROP, "ewmh_apply_flags: done.\n");
1673 ewmh_update_wm_state(struct ws_win *win) {
1674 xcb_atom_t vals[SWM_EWMH_ACTION_COUNT_MAX];
1678 vals[n++] = ewmh[_NET_WM_STATE_HIDDEN].atom;
1679 if (FULLSCREEN(win))
1680 vals[n++] = ewmh[_NET_WM_STATE_FULLSCREEN].atom;
1681 if (MAXIMIZED_VERT(win))
1682 vals[n++] = ewmh[_NET_WM_STATE_MAXIMIZED_VERT].atom;
1683 if (MAXIMIZED_HORZ(win))
1684 vals[n++] = ewmh[_NET_WM_STATE_MAXIMIZED_HORZ].atom;
1685 if (win->ewmh_flags & EWMH_F_SKIP_PAGER)
1686 vals[n++] = ewmh[_NET_WM_STATE_SKIP_PAGER].atom;
1687 if (win->ewmh_flags & EWMH_F_SKIP_TASKBAR)
1688 vals[n++] = ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom;
1689 if (win->ewmh_flags & EWMH_F_ABOVE)
1690 vals[n++] = ewmh[_NET_WM_STATE_ABOVE].atom;
1691 if (win->ewmh_flags & SWM_F_MANUAL)
1692 vals[n++] = ewmh[_SWM_WM_STATE_MANUAL].atom;
1695 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
1696 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, n, vals);
1698 xcb_delete_property(conn, win->id, ewmh[_NET_WM_STATE].atom);
1702 ewmh_get_wm_state(struct ws_win *win)
1705 xcb_get_property_cookie_t c;
1706 xcb_get_property_reply_t *r;
1712 win->ewmh_flags = 0;
1714 c = xcb_get_property(conn, 0, win->id, ewmh[_NET_WM_STATE].atom,
1715 XCB_ATOM_ATOM, 0, UINT32_MAX);
1716 r = xcb_get_property_reply(conn, c, NULL);
1720 states = xcb_get_property_value(r);
1721 n = xcb_get_property_value_length(r) / sizeof(xcb_atom_t);
1723 for (i = 0; i < n; i++)
1724 ewmh_change_wm_state(win, states[i], _NET_WM_STATE_ADD);
1732 dumpwins(struct swm_region *r, union arg *args)
1736 xcb_get_window_attributes_cookie_t c;
1737 xcb_get_window_attributes_reply_t *wa;
1739 /* suppress unused warning since var is needed */
1742 if (r->ws == NULL) {
1743 DPRINTF("dumpwins: invalid workspace\n");
1747 DPRINTF("=== managed window list ws %02d ===\n", r->ws->idx);
1748 TAILQ_FOREACH(w, &r->ws->winlist, entry) {
1749 state = get_win_state(w->id);
1750 c = xcb_get_window_attributes(conn, w->id);
1751 wa = xcb_get_window_attributes_reply(conn, c, NULL);
1753 DPRINTF("win %#x, map_state: %d, state: %u, "
1754 "transient: %#x\n", w->id, wa->map_state,
1755 state, w->transient);
1758 DPRINTF("win %#x, failed xcb_get_window_attributes\n",
1762 DPRINTF("=== stacking order (top down) === \n");
1763 TAILQ_FOREACH(w, &r->ws->stack, stack_entry) {
1764 DPRINTF("win %#x, fs: %s, maximized: %s, above: %s, "
1765 "iconic: %s\n", w->id, YESNO(FULLSCREEN(w)),
1766 YESNO(MAXIMIZED(w)), YESNO(ABOVE(w)), YESNO(ICONIC(w)));
1769 DPRINTF("===== unmanaged window list =====\n");
1770 TAILQ_FOREACH(w, &r->ws->unmanagedlist, entry) {
1771 state = get_win_state(w->id);
1772 c = xcb_get_window_attributes(conn, w->id);
1773 wa = xcb_get_window_attributes_reply(conn, c, NULL);
1775 DPRINTF("win %#x, map_state: %d, state: %u, "
1776 "transient: %#x\n", w->id, wa->map_state,
1777 state, w->transient);
1780 DPRINTF("win %#x, failed xcb_get_window_attributes\n",
1784 DPRINTF("=================================\n");
1788 dumpwins(struct swm_region *r, union arg *s)
1793 #endif /* SWM_DEBUG */
1798 int saved_errno, status;
1801 saved_errno = errno;
1805 while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) != 0) {
1810 if (errno != ECHILD)
1811 warn("sighdlr: waitpid");
1812 #endif /* SWM_DEBUG */
1815 if (pid == searchpid)
1819 if (WIFEXITED(status)) {
1820 if (WEXITSTATUS(status) != 0)
1821 warnx("sighdlr: child exit status: %d",
1822 WEXITSTATUS(status));
1824 warnx("sighdlr: child is terminated "
1826 #endif /* SWM_DEBUG */
1840 errno = saved_errno;
1846 struct pid_e *p = NULL;
1848 DNPRINTF(SWM_D_MISC, "find_pid: %d\n", pid);
1853 TAILQ_FOREACH(p, &pidlist, entry) {
1862 name_to_pixel(int sidx, const char *colorname)
1864 uint32_t result = 0;
1865 char cname[32] = "#";
1866 xcb_screen_t *screen;
1867 xcb_colormap_t cmap;
1868 xcb_alloc_color_reply_t *cr;
1869 xcb_alloc_named_color_reply_t *nr;
1870 uint16_t rr, gg, bb;
1872 screen = get_screen(sidx);
1873 cmap = screen->default_colormap;
1875 /* color is in format rgb://rr/gg/bb */
1876 if (strncmp(colorname, "rgb:", 4) == 0) {
1877 if (parse_rgb(colorname, &rr, &gg, &bb) == -1)
1878 warnx("could not parse rgb %s", colorname);
1880 cr = xcb_alloc_color_reply(conn,
1881 xcb_alloc_color(conn, cmap, rr, gg, bb),
1887 warnx("color '%s' not found", colorname);
1890 nr = xcb_alloc_named_color_reply(conn,
1891 xcb_alloc_named_color(conn, cmap, strlen(colorname),
1894 strlcat(cname, colorname + 2, sizeof cname - 1);
1895 nr = xcb_alloc_named_color_reply(conn,
1896 xcb_alloc_named_color(conn, cmap, strlen(cname),
1903 warnx("color '%s' not found", colorname);
1910 setscreencolor(const char *val, int i, int c)
1912 if (i < 0 || i >= get_screen_count())
1915 screens[i].c[c].pixel = name_to_pixel(i, val);
1916 free(screens[i].c[c].name);
1917 if ((screens[i].c[c].name = strdup(val)) == NULL)
1922 fancy_stacker(struct workspace *ws)
1924 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1925 if (ws->cur_layout->l_stack == vertical_stack)
1926 snprintf(ws->stacker, sizeof ws->stacker,
1927 ws->l_state.vertical_flip ? "[%d>%d]" : "[%d|%d]",
1928 ws->l_state.vertical_mwin, ws->l_state.vertical_stacks);
1929 else if (ws->cur_layout->l_stack == horizontal_stack)
1930 snprintf(ws->stacker, sizeof ws->stacker,
1931 ws->l_state.horizontal_flip ? "[%dv%d]" : "[%d-%d]",
1932 ws->l_state.horizontal_mwin, ws->l_state.horizontal_stacks);
1936 plain_stacker(struct workspace *ws)
1938 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1939 if (ws->cur_layout->l_stack == vertical_stack)
1940 strlcpy(ws->stacker, ws->l_state.vertical_flip ? "[>]" : "[|]",
1941 sizeof ws->stacker);
1942 else if (ws->cur_layout->l_stack == horizontal_stack)
1943 strlcpy(ws->stacker, ws->l_state.horizontal_flip ? "[v]" : "[-]",
1944 sizeof ws->stacker);
1948 custom_region(const char *val)
1950 unsigned int x, y, w, h;
1951 int sidx, num_screens;
1952 xcb_screen_t *screen;
1954 num_screens = get_screen_count();
1955 if (sscanf(val, "screen[%d]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
1956 errx(1, "invalid custom region, "
1957 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1958 if (sidx < 1 || sidx > num_screens)
1959 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1963 if ((screen = get_screen(sidx)) == NULL)
1964 errx(1, "ERROR: can't get screen %d.", sidx);
1967 errx(1, "region %ux%u+%u+%u too small", w, h, x, y);
1969 if (x > screen->width_in_pixels ||
1970 y > screen->height_in_pixels ||
1971 w + x > screen->width_in_pixels ||
1972 h + y > screen->height_in_pixels) {
1973 warnx("ignoring region %ux%u+%u+%u - not within screen "
1974 "boundaries (%ux%u)", w, h, x, y,
1975 screen->width_in_pixels, screen->height_in_pixels);
1979 new_region(&screens[sidx], x, y, w, h);
1983 socket_setnonblock(int fd)
1987 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
1988 err(1, "fcntl F_GETFL");
1989 flags |= O_NONBLOCK;
1990 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
1991 err(1, "fcntl F_SETFL");
1995 bar_print_legacy(struct swm_region *r, const char *s)
1997 xcb_rectangle_t rect;
2002 XRectangle ibox, lbox;
2006 XmbTextExtents(bar_fs, s, len, &ibox, &lbox);
2008 switch (bar_justify) {
2009 case SWM_BAR_JUSTIFY_LEFT:
2012 case SWM_BAR_JUSTIFY_CENTER:
2013 x = (WIDTH(r) - lbox.width) / 2;
2015 case SWM_BAR_JUSTIFY_RIGHT:
2016 x = WIDTH(r) - lbox.width - SWM_BAR_OFFSET;
2020 if (x < SWM_BAR_OFFSET)
2025 rect.width = WIDTH(r->bar);
2026 rect.height = HEIGHT(r->bar);
2028 /* clear back buffer */
2029 gcv[0] = r->s->c[SWM_S_COLOR_BAR].pixel;
2030 xcb_change_gc(conn, r->s->bar_gc, XCB_GC_FOREGROUND, gcv);
2031 xcb_poly_fill_rectangle(conn, r->bar->buffer, r->s->bar_gc, 1, &rect);
2033 /* draw back buffer */
2034 gcvd.graphics_exposures = 0;
2035 draw = XCreateGC(display, r->bar->buffer, GCGraphicsExposures, &gcvd);
2036 XSetForeground(display, draw, r->s->c[SWM_S_COLOR_BAR_FONT].pixel);
2037 DRAWSTRING(display, r->bar->buffer, bar_fs, draw,
2038 x, (bar_fs_extents->max_logical_extent.height - lbox.height) / 2 -
2040 XFreeGC(display, draw);
2043 xcb_copy_area(conn, r->bar->buffer, r->bar->id, r->s->bar_gc, 0, 0,
2044 0, 0, WIDTH(r->bar), HEIGHT(r->bar));
2048 bar_print(struct swm_region *r, const char *s)
2051 xcb_rectangle_t rect;
2059 XftTextExtentsUtf8(display, bar_font, (FcChar8 *)s, len, &info);
2061 switch (bar_justify) {
2062 case SWM_BAR_JUSTIFY_LEFT:
2065 case SWM_BAR_JUSTIFY_CENTER:
2066 x = (WIDTH(r) - info.width) / 2;
2068 case SWM_BAR_JUSTIFY_RIGHT:
2069 x = WIDTH(r) - info.width - SWM_BAR_OFFSET;
2073 if (x < SWM_BAR_OFFSET)
2078 rect.width = WIDTH(r->bar);
2079 rect.height = HEIGHT(r->bar);
2081 /* clear back buffer */
2082 gcv[0] = r->s->c[SWM_S_COLOR_BAR].pixel;
2083 xcb_change_gc(conn, r->s->bar_gc, XCB_GC_FOREGROUND, gcv);
2084 xcb_poly_fill_rectangle(conn, r->bar->buffer, r->s->bar_gc, 1, &rect);
2086 /* draw back buffer */
2087 draw = XftDrawCreate(display, r->bar->buffer,
2088 DefaultVisual(display, r->s->idx),
2089 DefaultColormap(display, r->s->idx));
2091 XftDrawStringUtf8(draw, &bar_font_color, bar_font, x,
2092 (HEIGHT(r->bar) + bar_font->height) / 2 - bar_font->descent,
2095 XftDrawDestroy(draw);
2098 xcb_copy_area(conn, r->bar->buffer, r->bar->id, r->s->bar_gc, 0, 0,
2099 0, 0, WIDTH(r->bar), HEIGHT(r->bar));
2103 bar_extra_stop(void)
2107 bzero(bar_pipe, sizeof bar_pipe);
2110 kill(bar_pid, SIGTERM);
2113 strlcpy(bar_ext, "", sizeof bar_ext);
2118 bar_window_class(char *s, size_t sz, struct swm_region *r)
2120 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
2122 if (r->ws->focus->ch.class_name != NULL)
2123 strlcat(s, r->ws->focus->ch.class_name, sz);
2127 bar_window_instance(char *s, size_t sz, struct swm_region *r)
2129 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
2131 if (r->ws->focus->ch.instance_name != NULL)
2132 strlcat(s, r->ws->focus->ch.instance_name, sz);
2136 bar_window_class_instance(char *s, size_t sz, struct swm_region *r)
2138 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
2141 bar_window_class(s, sz, r);
2142 strlcat(s, ":", sz);
2143 bar_window_instance(s, sz, r);
2147 bar_window_state(char *s, size_t sz, struct swm_region *r)
2149 if (r == NULL || r ->ws == NULL || r->ws->focus == NULL)
2151 if (MAXIMIZED(r->ws->focus))
2152 strlcat(s, "(m)", sz);
2153 else if (ABOVE(r->ws->focus))
2154 strlcat(s, "(f)", sz);
2158 bar_window_name(char *s, size_t sz, struct swm_region *r)
2162 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
2165 title = get_win_name(r->ws->focus->id);
2166 strlcat(s, title, sz);
2171 bar_urgent(char *s, size_t sz)
2174 int i, j, num_screens;
2175 bool urgent[SWM_WS_MAX];
2177 xcb_get_property_cookie_t c;
2178 xcb_icccm_wm_hints_t hints;
2180 for (i = 0; i < workspace_limit; i++)
2183 num_screens = get_screen_count();
2184 for (i = 0; i < num_screens; i++)
2185 for (j = 0; j < workspace_limit; j++)
2186 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry) {
2187 c = xcb_icccm_get_wm_hints(conn, win->id);
2188 if (xcb_icccm_get_wm_hints_reply(conn, c,
2191 if (hints.flags & XCB_ICCCM_WM_HINT_X_URGENCY)
2195 for (i = 0; i < workspace_limit; i++) {
2197 snprintf(b, sizeof b, "%d ", i + 1);
2199 snprintf(b, sizeof b, "- ");
2205 bar_workspace_name(char *s, size_t sz, struct swm_region *r)
2207 if (r == NULL || r->ws == NULL)
2209 if (r->ws->name != NULL)
2210 strlcat(s, r->ws->name, sz);
2213 /* build the default bar format according to the defined enabled options */
2215 bar_fmt(const char *fmtexp, char *fmtnew, struct swm_region *r, size_t sz)
2219 /* if format provided, just copy the buffers */
2220 if (bar_format != NULL) {
2221 strlcpy(fmtnew, fmtexp, sz);
2225 /* reset the output buffer */
2228 strlcat(fmtnew, "+N:+I ", sz);
2230 strlcat(fmtnew, "+S", sz);
2231 strlcat(fmtnew, " ", sz);
2233 /* only show the workspace name if there's actually one */
2234 if (r != NULL && r->ws != NULL && r->ws->name != NULL)
2235 strlcat(fmtnew, "<+D>", sz);
2237 /* If enabled, only show the iconic count if there are iconic wins. */
2238 if (iconic_enabled && r != NULL && r->ws != NULL)
2239 TAILQ_FOREACH(w, &r->ws->winlist, entry)
2241 strlcat(fmtnew, "{+M}", sz);
2245 strlcat(fmtnew, "+3<", sz);
2247 if (clock_enabled) {
2248 strlcat(fmtnew, fmtexp, sz);
2249 strlcat(fmtnew, "+4<", sz);
2252 /* bar_urgent already adds the space before the last asterisk */
2254 strlcat(fmtnew, "* +U*+4<", sz);
2256 if (window_class_enabled) {
2257 strlcat(fmtnew, "+C", sz);
2258 if (!window_instance_enabled)
2259 strlcat(fmtnew, "+4<", sz);
2262 /* checks needed by the colon and floating strlcat(3) calls below */
2263 if (r != NULL && r->ws != NULL && r->ws->focus != NULL) {
2264 if (window_instance_enabled) {
2265 if (window_class_enabled)
2266 strlcat(fmtnew, ":", sz);
2267 strlcat(fmtnew, "+T+4<", sz);
2269 if (window_name_enabled) {
2270 if (ABOVE(r->ws->focus) || MAXIMIZED(r->ws->focus))
2271 strlcat(fmtnew, "+F ", sz);
2272 strlcat(fmtnew, "+64W ", sz);
2276 /* finally add the action script output and the version */
2277 strlcat(fmtnew, "+4<+A+4<+V", sz);
2281 bar_replace_pad(char *tmp, int *limit, size_t sz)
2283 /* special case; no limit given, pad one space, instead */
2284 if (*limit == (int)sz - 1)
2286 snprintf(tmp, sz, "%*s", *limit, " ");
2289 /* replaces the bar format character sequences (like in tmux(1)) */
2291 bar_replace_seq(char *fmt, char *fmtrep, struct swm_region *r, size_t *offrep,
2296 char tmp[SWM_BAR_MAX];
2297 int limit, size, count;
2300 /* reset strlcat(3) buffer */
2303 /* get number, if any */
2306 if (sscanf(fmt, "%d%n", &limit, &size) != 1)
2307 limit = sizeof tmp - 1;
2308 if (limit <= 0 || limit >= (int)sizeof tmp)
2309 limit = sizeof tmp - 1;
2311 /* there is nothing to replace (ie EOL) */
2318 bar_replace_pad(tmp, &limit, sizeof tmp);
2321 snprintf(tmp, sizeof tmp, "%s", bar_ext);
2324 bar_window_class(tmp, sizeof tmp, r);
2327 bar_workspace_name(tmp, sizeof tmp, r);
2330 bar_window_state(tmp, sizeof tmp, r);
2333 snprintf(tmp, sizeof tmp, "%d", r->ws->idx + 1);
2337 TAILQ_FOREACH(w, &r->ws->winlist, entry)
2341 snprintf(tmp, sizeof tmp, "%d", count);
2344 snprintf(tmp, sizeof tmp, "%d", r->s->idx + 1);
2347 bar_window_class_instance(tmp, sizeof tmp, r);
2350 snprintf(tmp, sizeof tmp, "%s", r->ws->stacker);
2353 bar_window_instance(tmp, sizeof tmp, r);
2356 bar_urgent(tmp, sizeof tmp);
2359 snprintf(tmp, sizeof tmp, "%s", bar_vertext);
2362 bar_window_name(tmp, sizeof tmp, r);
2365 /* unknown character sequence; copy as-is */
2366 snprintf(tmp, sizeof tmp, "+%c", *fmt);
2372 if ((int)len < limit)
2374 while (limit-- > 0) {
2375 if (*offrep >= sz - 1)
2377 fmtrep[(*offrep)++] = *ptr++;
2385 bar_replace(char *fmt, char *fmtrep, struct swm_region *r, size_t sz)
2390 while (*fmt != '\0') {
2392 /* skip ordinary characters */
2395 fmtrep[off++] = *fmt++;
2399 /* character sequence found; replace it */
2400 fmt = bar_replace_seq(fmt, fmtrep, r, &off, sz);
2409 bar_fmt_expand(char *fmtexp, size_t sz)
2416 /* start by grabbing the current time and date */
2418 localtime_r(&tmt, &tm);
2420 /* figure out what to expand */
2421 if (bar_format != NULL)
2423 else if (bar_format == NULL && clock_enabled)
2425 /* if nothing to expand bail out */
2431 /* copy as-is, just in case the format shouldn't be expanded below */
2432 strlcpy(fmtexp, fmt, sz);
2433 /* finally pass the string through strftime(3) */
2434 #ifndef SWM_DENY_CLOCK_FORMAT
2435 if ((len = strftime(fmtexp, sz, fmt, &tm)) == 0)
2436 warnx("format too long");
2441 /* Redraws the bar; need to follow with xcb_flush() or focus_flush(). */
2445 char fmtexp[SWM_BAR_MAX], fmtnew[SWM_BAR_MAX];
2446 char fmtrep[SWM_BAR_MAX];
2448 struct swm_region *r;
2450 /* expand the format by first passing it through strftime(3) */
2451 bar_fmt_expand(fmtexp, sizeof fmtexp);
2453 num_screens = get_screen_count();
2454 for (i = 0; i < num_screens; i++) {
2455 TAILQ_FOREACH(r, &screens[i].rl, entry) {
2459 if (bar_enabled && r->ws->bar_enabled)
2460 xcb_map_window(conn, r->bar->id);
2462 xcb_unmap_window(conn, r->bar->id);
2466 if (startup_exception)
2467 snprintf(fmtrep, sizeof fmtrep, "total "
2468 "exceptions: %d, first exception: %s",
2472 bar_fmt(fmtexp, fmtnew, r, sizeof fmtnew);
2473 bar_replace(fmtnew, fmtrep, r, sizeof fmtrep);
2475 if (bar_font_legacy)
2476 bar_print_legacy(r, fmtrep);
2478 bar_print(r, fmtrep);
2484 * Reads external script output; call when stdin is readable.
2485 * Returns 1 if bar_ext was updated; otherwise 0.
2488 bar_extra_update(void)
2491 char b[SWM_BAR_MAX];
2492 bool changed = false;
2497 while (fgets(b, sizeof(b), stdin) != NULL) {
2500 if (b[len - 1] == '\n') {
2501 /* Remove newline. */
2504 /* "Clear" bar_ext. */
2507 /* Flush buffered output. */
2508 strlcpy(bar_ext, bar_ext_buf, sizeof(bar_ext));
2509 bar_ext_buf[0] = '\0';
2511 /* Append new output to bar. */
2512 strlcat(bar_ext, b, sizeof(bar_ext));
2516 /* Buffer output. */
2517 strlcat(bar_ext_buf, b, sizeof(bar_ext_buf));
2522 if (errno != EAGAIN) {
2523 warn("bar_action failed");
2532 bar_toggle(struct swm_region *r, union arg *args)
2534 struct swm_region *tmpr;
2537 /* suppress unused warnings since vars are needed */
2541 DNPRINTF(SWM_D_BAR, "bar_toggle\n");
2544 case SWM_ARG_ID_BAR_TOGGLE_WS:
2545 /* Only change if master switch is enabled. */
2547 r->ws->bar_enabled = !r->ws->bar_enabled;
2549 bar_enabled = r->ws->bar_enabled = true;
2551 case SWM_ARG_ID_BAR_TOGGLE:
2552 bar_enabled = !bar_enabled;
2556 /* update bars as necessary */
2557 num_screens = get_screen_count();
2558 for (i = 0; i < num_screens; i++)
2559 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
2561 if (bar_enabled && tmpr->ws->bar_enabled)
2562 xcb_map_window(conn, tmpr->bar->id);
2564 xcb_unmap_window(conn, tmpr->bar->id);
2569 /* must be after stack */
2576 bar_extra_setup(void)
2578 /* do this here because the conf file is in memory */
2579 if (!bar_extra && bar_argv[0]) {
2580 /* launch external status app */
2582 if (pipe(bar_pipe) == -1)
2583 err(1, "pipe error");
2584 socket_setnonblock(bar_pipe[0]);
2585 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
2587 /* Set stdin to read from the pipe. */
2588 if (dup2(bar_pipe[0], STDIN_FILENO) == -1)
2591 /* Set stdout to write to the pipe. */
2592 if (dup2(bar_pipe[1], STDOUT_FILENO) == -1)
2595 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
2596 err(1, "could not disable SIGPIPE");
2597 switch (bar_pid = fork()) {
2599 err(1, "cannot fork");
2603 execvp(bar_argv[0], bar_argv);
2604 err(1, "%s external app failed", bar_argv[0]);
2606 default: /* parent */
2611 atexit(kill_bar_extra_atexit);
2616 kill_bar_extra_atexit(void)
2619 kill(bar_pid, SIGTERM);
2627 while ((s = index(s, '-'))) {
2632 return (count == 14);
2638 char *default_string;
2639 char **missing_charsets;
2640 int num_missing_charsets = 0;
2644 XFreeFontSet(display, bar_fs);
2648 DNPRINTF(SWM_D_INIT, "fontset_init: loading bar_fonts: %s\n", bar_fonts);
2650 bar_fs = XCreateFontSet(display, bar_fonts, &missing_charsets,
2651 &num_missing_charsets, &default_string);
2653 if (num_missing_charsets > 0) {
2654 warnx("Unable to load charset(s):");
2656 for (i = 0; i < num_missing_charsets; ++i)
2657 warnx("%s", missing_charsets[i]);
2659 XFreeStringList(missing_charsets);
2661 if (strcmp(default_string, ""))
2662 warnx("Glyphs from those sets will be replaced "
2663 "by '%s'.", default_string);
2665 warnx("Glyphs from those sets won't be drawn.");
2669 errx(1, "Error creating font set structure.");
2671 bar_fs_extents = XExtentsOfFontSet(bar_fs);
2673 bar_height = bar_fs_extents->max_logical_extent.height +
2674 2 * bar_border_width;
2681 xft_init(struct swm_region *r)
2683 char *font, *d, *search;
2686 if (bar_font == NULL) {
2687 if ((d = strdup(bar_fonts)) == NULL)
2688 errx(1, "insufficient memory.");
2690 while ((font = strsep(&search, ",")) != NULL) {
2694 DNPRINTF(SWM_D_INIT, "xft_init: try font %s\n", font);
2697 bar_font = XftFontOpenXlfd(display, r->s->idx,
2700 bar_font = XftFontOpenName(display, r->s->idx,
2704 if (bar_font == NULL) {
2705 warnx("unable to load font %s", font);
2708 DNPRINTF(SWM_D_INIT, "successfully opened "
2716 if (bar_font == NULL)
2717 errx(1, "unable to open a font");
2719 PIXEL_TO_XRENDERCOLOR(r->s->c[SWM_S_COLOR_BAR_FONT].pixel, color);
2721 if (!XftColorAllocValue(display, DefaultVisual(display, r->s->idx),
2722 DefaultColormap(display, r->s->idx), &color, &bar_font_color))
2723 warn("Xft error: unable to allocate color.");
2725 PIXEL_TO_XRENDERCOLOR(r->s->c[SWM_S_COLOR_BAR].pixel, color);
2727 if (!XftColorAllocValue(display, DefaultVisual(display, r->s->idx),
2728 DefaultColormap(display, r->s->idx), &color, &search_font_color))
2729 warn("Xft error: unable to allocate color.");
2731 bar_height = bar_font->height + 2 * bar_border_width;
2738 bar_setup(struct swm_region *r)
2740 xcb_screen_t *screen;
2743 DNPRINTF(SWM_D_BAR, "bar_setup: screen %d.\n",
2746 if ((screen = get_screen(r->s->idx)) == NULL)
2747 errx(1, "ERROR: can't get screen %d.", r->s->idx);
2752 if ((r->bar = calloc(1, sizeof(struct swm_bar))) == NULL)
2753 err(1, "bar_setup: calloc: failed to allocate memory.");
2755 if (bar_font_legacy)
2761 Y(r->bar) = bar_at_bottom ? (Y(r) + HEIGHT(r) - bar_height) : Y(r);
2762 WIDTH(r->bar) = WIDTH(r) - 2 * bar_border_width;
2763 HEIGHT(r->bar) = bar_height - 2 * bar_border_width;
2765 /* Assume region is unfocused when we create the bar. */
2766 r->bar->id = xcb_generate_id(conn);
2767 wa[0] = r->s->c[SWM_S_COLOR_BAR].pixel;
2768 wa[1] = r->s->c[SWM_S_COLOR_BAR_BORDER_UNFOCUS].pixel;
2769 wa[2] = XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_POINTER_MOTION |
2770 XCB_EVENT_MASK_POINTER_MOTION_HINT;
2772 xcb_create_window(conn, XCB_COPY_FROM_PARENT, r->bar->id, r->s->root,
2773 X(r->bar), Y(r->bar), WIDTH(r->bar), HEIGHT(r->bar),
2774 bar_border_width, XCB_WINDOW_CLASS_INPUT_OUTPUT,
2775 XCB_COPY_FROM_PARENT, XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL
2776 | XCB_CW_EVENT_MASK, wa);
2778 /* Stack bar window above region window to start. */
2780 wa[1] = XCB_STACK_MODE_ABOVE;
2782 xcb_configure_window(conn, r->bar->id, XCB_CONFIG_WINDOW_SIBLING |
2783 XCB_CONFIG_WINDOW_STACK_MODE, wa);
2785 r->bar->buffer = xcb_generate_id(conn);
2786 xcb_create_pixmap(conn, screen->root_depth, r->bar->buffer, r->bar->id,
2787 WIDTH(r->bar), HEIGHT(r->bar));
2790 xcb_randr_select_input(conn, r->bar->id,
2791 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE);
2794 xcb_map_window(conn, r->bar->id);
2796 DNPRINTF(SWM_D_BAR, "bar_setup: win %#x, (x,y) w x h: (%d,%d) "
2797 "%d x %d\n", WINID(r->bar), X(r->bar), Y(r->bar), WIDTH(r->bar),
2804 bar_cleanup(struct swm_region *r)
2808 xcb_destroy_window(conn, r->bar->id);
2809 xcb_free_pixmap(conn, r->bar->buffer);
2815 set_win_state(struct ws_win *win, uint8_t state)
2817 uint16_t data[2] = { state, XCB_ATOM_NONE };
2819 DNPRINTF(SWM_D_EVENT, "set_win_state: win %#x, state: %u\n",
2825 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id, a_state,
2826 a_state, 32, 2, data);
2830 get_win_state(xcb_window_t w)
2832 xcb_get_property_reply_t *r;
2833 xcb_get_property_cookie_t c;
2834 uint32_t result = 0;
2836 c = xcb_get_property(conn, 0, w, a_state, a_state, 0L, 2L);
2837 r = xcb_get_property_reply(conn, c, NULL);
2839 if (r->type == a_state && r->format == 32 && r->length == 2)
2840 result = *((uint32_t *)xcb_get_property_value(r));
2844 DNPRINTF(SWM_D_MISC, "get_win_state property: win %#x state %u\n", w,
2850 version(struct swm_region *r, union arg *args)
2852 /* suppress unused warnings since vars are needed */
2856 bar_version = !bar_version;
2858 snprintf(bar_vertext, sizeof bar_vertext,
2859 "Version: %s Build: %s", SPECTRWM_VERSION, buildstr);
2861 strlcpy(bar_vertext, "", sizeof bar_vertext);
2868 client_msg(struct ws_win *win, xcb_atom_t a, xcb_timestamp_t t)
2870 xcb_client_message_event_t ev;
2878 name = get_atom_name(a);
2879 DNPRINTF(SWM_D_EVENT, "client_msg: win %#x, atom: %s(%u), "
2881 win->id, name, a, t);
2885 bzero(&ev, sizeof ev);
2886 ev.response_type = XCB_CLIENT_MESSAGE;
2887 ev.window = win->id;
2890 ev.data.data32[0] = a;
2891 ev.data.data32[1] = t;
2893 xcb_send_event(conn, 0, win->id,
2894 XCB_EVENT_MASK_NO_EVENT, (const char *)&ev);
2897 /* synthetic response to a ConfigureRequest when not making a change */
2899 config_win(struct ws_win *win, xcb_configure_request_event_t *ev)
2901 xcb_configure_notify_event_t ce;
2906 /* send notification of unchanged state. */
2907 bzero(&ce, sizeof(ce));
2908 ce.response_type = XCB_CONFIGURE_NOTIFY;
2911 ce.width = WIDTH(win);
2912 ce.height = HEIGHT(win);
2913 ce.override_redirect = 0;
2918 ce.window = win->id;
2919 ce.border_width = BORDER(win);
2920 ce.above_sibling = XCB_WINDOW_NONE;
2923 ce.event = ev->window;
2924 ce.window = ev->window;
2926 /* make response appear more WM_SIZE_HINTS-compliant */
2927 if (win->sh.flags) {
2928 DNPRINTF(SWM_D_MISC, "config_win: hints: win %#x,"
2929 " sh.flags: %u, min: %d x %d, max: %d x %d, inc: "
2930 "%d x %d\n", win->id, win->sh.flags, SH_MIN_W(win),
2931 SH_MIN_H(win), SH_MAX_W(win), SH_MAX_H(win),
2932 SH_INC_W(win), SH_INC_H(win));
2937 /* the hint may be set... to 0! */
2938 if (SH_MIN_W(win) > 0 && ce.width < SH_MIN_W(win))
2939 ce.width = SH_MIN_W(win);
2940 if (SH_MIN_H(win) > 0 && ce.height < SH_MIN_H(win))
2941 ce.height = SH_MIN_H(win);
2946 /* may also be advertized as 0 */
2947 if (SH_MAX_W(win) > 0 && ce.width > SH_MAX_W(win))
2948 ce.width = SH_MAX_W(win);
2949 if (SH_MAX_H(win) > 0 && ce.height > SH_MAX_H(win))
2950 ce.height = SH_MAX_H(win);
2953 /* resize increment. */
2955 if (SH_INC_W(win) > 1 && ce.width > SH_INC_W(win))
2956 ce.width -= (ce.width - SH_MIN_W(win)) %
2958 if (SH_INC_H(win) > 1 && ce.height > SH_INC_H(win))
2959 ce.height -= (ce.height - SH_MIN_H(win)) %
2963 /* adjust x and y for requested border_width. */
2964 ce.x += BORDER(win) - ev->border_width;
2965 ce.y += BORDER(win) - ev->border_width;
2966 ce.border_width = ev->border_width;
2967 ce.above_sibling = ev->sibling;
2970 DNPRINTF(SWM_D_MISC, "config_win: ewmh: %s, win %#x, (x,y) w x h: "
2971 "(%d,%d) %d x %d, border: %d\n", YESNO(ev == NULL), win->id, ce.x,
2972 ce.y, ce.width, ce.height, ce.border_width);
2974 xcb_send_event(conn, 0, win->id, XCB_EVENT_MASK_STRUCTURE_NOTIFY,
2979 count_win(struct workspace *ws, bool count_transient)
2984 TAILQ_FOREACH(win, &ws->winlist, entry) {
2985 if (!count_transient && FLOATING(win))
2991 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
2997 quit(struct swm_region *r, union arg *args)
2999 /* suppress unused warnings since vars are needed */
3003 DNPRINTF(SWM_D_MISC, "quit\n");
3008 raise_window(struct ws_win *win)
3010 struct ws_win *target = NULL;
3011 struct swm_region *r;
3012 struct workspace *ws;
3014 if (win == NULL || (r = win->ws->r) == NULL)
3018 DNPRINTF(SWM_D_EVENT, "raise_window: win %#x\n", win->id);
3020 TAILQ_FOREACH(target, &ws->stack, stack_entry) {
3021 if (target == win || ICONIC(target))
3023 if (ws->cur_layout == &layouts[SWM_MAX_STACK])
3025 if (TRANS(win) && (win->transient == target->transient ||
3026 win->transient == target->id))
3028 if (FULLSCREEN(win))
3030 if (FULLSCREEN(target))
3034 if (MAXIMIZED(target))
3036 if (ABOVE(win) || TRANS(win))
3038 if (!ABOVE(target) && !TRANS(target))
3042 if (target != NULL) {
3043 /* Change stack position. */
3044 TAILQ_REMOVE(&ws->stack, win, stack_entry);
3045 TAILQ_INSERT_BEFORE(target, win, stack_entry);
3046 update_win_stacking(win);
3050 if (swm_debug & SWM_D_STACK) {
3051 DPRINTF("=== stacking order (top down) === \n");
3052 TAILQ_FOREACH(target, &r->ws->stack, stack_entry) {
3053 DPRINTF("win %#x, fs: %s, maximized: %s, above: %s, "
3054 "iconic: %s\n", target->id, YESNO(FULLSCREEN(target)),
3055 YESNO(MAXIMIZED(target)), YESNO(ABOVE(target)),
3056 YESNO(ICONIC(target)));
3060 DNPRINTF(SWM_D_EVENT, "raise_window: done\n");
3064 update_win_stacking(struct ws_win *win)
3066 struct ws_win *sibling;
3067 struct swm_region *r;
3070 if (win == NULL || (r = win->ws->r) == NULL)
3073 sibling = TAILQ_NEXT(win, stack_entry);
3074 if (sibling != NULL && FLOATING(win) == FLOATING(sibling))
3075 val[0] = sibling->id;
3077 val[0] = FLOATING(win) ? r->bar->id : r->id;
3079 DNPRINTF(SWM_D_EVENT, "update_win_stacking: %#x, sibling %#x\n",
3082 val[1] = XCB_STACK_MODE_ABOVE;
3084 xcb_configure_window(conn, win->id, XCB_CONFIG_WINDOW_SIBLING |
3085 XCB_CONFIG_WINDOW_STACK_MODE, val);
3089 map_window(struct ws_win *win)
3094 DNPRINTF(SWM_D_EVENT, "map_window: win %#x, mapped: %s\n",
3095 win->id, YESNO(win->mapped));
3100 xcb_map_window(conn, win->id);
3101 set_win_state(win, XCB_ICCCM_WM_STATE_NORMAL);
3106 unmap_window(struct ws_win *win)
3111 DNPRINTF(SWM_D_EVENT, "unmap_window: win %#x, mapped: %s\n", win->id,
3112 YESNO(win->mapped));
3117 xcb_unmap_window(conn, win->id);
3118 set_win_state(win, XCB_ICCCM_WM_STATE_ICONIC);
3119 win->mapped = false;
3126 int i, j, num_screens;
3128 num_screens = get_screen_count();
3129 for (i = 0; i < num_screens; i++)
3130 for (j = 0; j < workspace_limit; j++)
3131 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
3136 fake_keypress(struct ws_win *win, xcb_keysym_t keysym, uint16_t modifiers)
3138 xcb_key_press_event_t event;
3139 xcb_keycode_t *keycode;
3144 keycode = xcb_key_symbols_get_keycode(syms, keysym);
3146 DNPRINTF(SWM_D_MISC, "fake_keypress: win %#x, keycode %u\n",
3149 bzero(&event, sizeof(event));
3150 event.event = win->id;
3151 event.root = win->s->root;
3152 event.child = XCB_WINDOW_NONE;
3153 event.time = XCB_CURRENT_TIME;
3154 event.event_x = X(win);
3155 event.event_y = Y(win);
3158 event.same_screen = 1;
3159 event.detail = *keycode;
3160 event.state = modifiers;
3162 event.response_type = XCB_KEY_PRESS;
3163 xcb_send_event(conn, 1, win->id,
3164 XCB_EVENT_MASK_KEY_PRESS, (const char *)&event);
3166 event.response_type = XCB_KEY_RELEASE;
3167 xcb_send_event(conn, 1, win->id,
3168 XCB_EVENT_MASK_KEY_RELEASE, (const char *)&event);
3174 restart(struct swm_region *r, union arg *args)
3176 /* suppress unused warning since var is needed */
3180 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
3184 execvp(start_argv[0], start_argv);
3185 warn("execvp failed");
3190 get_pointer_win(xcb_window_t root)
3192 struct ws_win *win = NULL;
3193 xcb_query_pointer_reply_t *r;
3195 DNPRINTF(SWM_D_EVENT, "get_pointer_win: root: %#x.\n", root);
3197 r = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, root), NULL);
3199 win = find_window(r->child);
3201 DNPRINTF(SWM_D_EVENT, "get_pointer_win: %#x.\n",
3204 DNPRINTF(SWM_D_EVENT, "get_pointer_win: none.\n");
3213 center_pointer(struct swm_region *r)
3217 if (!warp_pointer || r == NULL)
3221 if (win && win->mapped)
3222 xcb_warp_pointer(conn, XCB_NONE, win->id, 0, 0, 0, 0,
3223 WIDTH(win) / 2, HEIGHT(win) / 2);
3225 xcb_warp_pointer(conn, XCB_NONE, r->id, 0, 0, 0, 0,
3226 WIDTH(r) / 2, HEIGHT(r) / 2);
3230 root_to_region(xcb_window_t root, int check)
3233 struct swm_region *r = NULL;
3235 xcb_query_pointer_reply_t *qpr;
3236 xcb_get_input_focus_reply_t *gifr;
3238 DNPRINTF(SWM_D_MISC, "root_to_region: win %#x\n", root);
3240 num_screens = get_screen_count();
3241 for (i = 0; i < num_screens; i++)
3242 if (screens[i].root == root)
3245 if (check & SWM_CK_REGION)
3246 r = screens[i].r_focus;
3248 if (r == NULL && check & SWM_CK_FOCUS) {
3249 /* Try to find an actively focused window */
3250 gifr = xcb_get_input_focus_reply(conn,
3251 xcb_get_input_focus(conn), NULL);
3253 cfw = find_window(gifr->focus);
3254 if (cfw && cfw->ws->r)
3261 if (r == NULL && check & SWM_CK_POINTER) {
3262 /* No region with an active focus; try to use pointer. */
3263 qpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn,
3264 screens[i].root), NULL);
3266 DNPRINTF(SWM_D_MISC, "root_to_region: pointer: "
3267 "(%d,%d)\n", qpr->root_x, qpr->root_y);
3268 TAILQ_FOREACH(r, &screens[i].rl, entry)
3269 if (X(r) <= qpr->root_x &&
3270 qpr->root_x < MAX_X(r) &&
3271 Y(r) <= qpr->root_y &&
3272 qpr->root_y < MAX_Y(r))
3279 if (r == NULL && check & SWM_CK_FALLBACK)
3280 r = TAILQ_FIRST(&screens[i].rl);
3282 DNPRINTF(SWM_D_MISC, "root_to_region: idx: %d\n", get_region_index(r));
3288 find_unmanaged_window(xcb_window_t id)
3291 int i, j, num_screens;
3293 num_screens = get_screen_count();
3294 for (i = 0; i < num_screens; i++)
3295 for (j = 0; j < workspace_limit; j++)
3296 TAILQ_FOREACH(win, &screens[i].ws[j].unmanagedlist,
3304 find_window(xcb_window_t id)
3307 int i, j, num_screens;
3308 xcb_query_tree_reply_t *r;
3310 num_screens = get_screen_count();
3311 for (i = 0; i < num_screens; i++)
3312 for (j = 0; j < workspace_limit; j++)
3313 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
3317 r = xcb_query_tree_reply(conn, xcb_query_tree(conn, id), NULL);
3321 /* if we were looking for the parent return that window instead */
3322 if (r->parent == 0 || r->root == r->parent) {
3327 /* look for parent */
3328 for (i = 0; i < num_screens; i++)
3329 for (j = 0; j < workspace_limit; j++)
3330 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
3331 if (r->parent == win->id) {
3341 spawn(int ws_idx, union arg *args, bool close_fd)
3346 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
3348 close(xcb_get_file_descriptor(conn));
3350 setenv("LD_PRELOAD", SWM_LIB, 1);
3352 if (asprintf(&ret, "%d", ws_idx) == -1) {
3353 warn("spawn: asprintf SWM_WS");
3356 setenv("_SWM_WS", ret, 1);
3360 if (asprintf(&ret, "%d", getpid()) == -1) {
3361 warn("spawn: asprintf _SWM_PID");
3364 setenv("_SWM_PID", ret, 1);
3368 if (setsid() == -1) {
3369 warn("spawn: setsid");
3375 * close stdin and stdout to prevent interaction between apps
3376 * and the baraction script
3377 * leave stderr open to record errors
3379 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) == -1) {
3380 warn("spawn: open");
3383 dup2(fd, STDIN_FILENO);
3384 dup2(fd, STDOUT_FILENO);
3389 execvp(args->argv[0], args->argv);
3391 warn("spawn: execvp");
3396 kill_refs(struct ws_win *win)
3398 int i, x, num_screens;
3399 struct swm_region *r;
3400 struct workspace *ws;
3405 num_screens = get_screen_count();
3406 for (i = 0; i < num_screens; i++)
3407 TAILQ_FOREACH(r, &screens[i].rl, entry)
3408 for (x = 0; x < workspace_limit; x++) {
3410 if (win == ws->focus)
3412 if (win == ws->focus_prev)
3413 ws->focus_prev = NULL;
3418 validate_win(struct ws_win *testwin)
3421 struct workspace *ws;
3422 struct swm_region *r;
3423 int i, x, num_screens;
3425 if (testwin == NULL)
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++) {
3433 TAILQ_FOREACH(win, &ws->winlist, entry)
3441 validate_ws(struct workspace *testws)
3443 struct swm_region *r;
3444 struct workspace *ws;
3445 int i, x, num_screens;
3447 /* validate all ws */
3448 num_screens = get_screen_count();
3449 for (i = 0; i < num_screens; i++)
3450 TAILQ_FOREACH(r, &screens[i].rl, entry)
3451 for (x = 0; x < workspace_limit; x++) {
3460 unfocus_win(struct ws_win *win)
3462 xcb_window_t none = XCB_WINDOW_NONE;
3464 DNPRINTF(SWM_D_FOCUS, "unfocus_win: win %#x\n", WINID(win));
3469 if (win->ws == NULL) {
3470 DNPRINTF(SWM_D_FOCUS, "unfocus_win: NULL ws.\n");
3474 if (validate_ws(win->ws)) {
3475 DNPRINTF(SWM_D_FOCUS, "unfocus_win: invalid ws.\n");
3479 if (win->ws->r == NULL) {
3480 DNPRINTF(SWM_D_FOCUS, "unfocus_win: NULL region.\n");
3484 if (validate_win(win)) {
3485 DNPRINTF(SWM_D_FOCUS, "unfocus_win: invalid win.\n");
3490 if (win->ws->focus == win) {
3491 win->ws->focus = NULL;
3492 win->ws->focus_prev = win;
3495 if (validate_win(win->ws->focus)) {
3496 kill_refs(win->ws->focus);
3497 win->ws->focus = NULL;
3500 if (validate_win(win->ws->focus_prev)) {
3501 kill_refs(win->ws->focus_prev);
3502 win->ws->focus_prev = NULL;
3505 update_window_color(win);
3507 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
3508 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1, &none);
3510 DNPRINTF(SWM_D_FOCUS, "unfocus_win: done.\n");
3514 focus_win(struct ws_win *win)
3516 struct ws_win *cfw = NULL, *parent = NULL, *w, *tmpw;
3517 struct workspace *ws;
3518 xcb_get_input_focus_reply_t *gifr;
3520 DNPRINTF(SWM_D_FOCUS, "focus_win: win %#x\n", WINID(win));
3522 if (win == NULL || win->ws == NULL || !win->mapped)
3527 if (validate_ws(ws))
3530 if (validate_win(win)) {
3535 gifr = xcb_get_input_focus_reply(conn, xcb_get_input_focus(conn), NULL);
3537 cfw = find_window(gifr->focus);
3538 if (cfw != NULL && cfw != win) {
3539 if (cfw->ws != ws && cfw->ws->r != NULL) {
3540 /* Change border to unfocused color. */
3541 xcb_change_window_attributes(conn, cfw->id,
3542 XCB_CW_BORDER_PIXEL,
3543 &cfw->s->c[(MAXIMIZED(cfw) ?
3544 SWM_S_COLOR_UNFOCUS_MAXIMIZED :
3545 SWM_S_COLOR_UNFOCUS)].pixel);
3553 if (ws->focus != win) {
3554 if (ws->focus && ws->focus != cfw)
3555 unfocus_win(ws->focus);
3559 /* If this window directs focus to a child window, then clear. */
3560 if (win->focus_child)
3561 win->focus_child = NULL;
3563 /* If transient, adjust parent's focus child for focus_magic. */
3565 parent = find_window(win->transient);
3566 if (parent && parent->focus_child != win)
3567 parent->focus_child = win;
3570 if (cfw != win && ws->r != NULL) {
3571 /* Set input focus if no input hint, or indicated by hint. */
3572 if (!(win->hints.flags & XCB_ICCCM_WM_HINT_INPUT) ||
3573 (win->hints.flags & XCB_ICCCM_WM_HINT_INPUT &&
3575 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT,
3576 win->id, last_event_time);
3578 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT,
3579 ws->r->id, XCB_CURRENT_TIME);
3581 /* Tell app it can adjust focus to a specific window. */
3582 if (win->take_focus) {
3583 /* java is special; always tell parent */
3584 if (TRANS(win) && win->java)
3585 client_msg(parent, a_takefocus,
3588 client_msg(win, a_takefocus, last_event_time);
3591 if (ws->cur_layout->flags & SWM_L_MAPONFOCUS ||
3593 /* If a parent exists, map it first. */
3595 raise_window(parent);
3598 /* Map siblings next. */
3599 TAILQ_FOREACH_SAFE(w, &ws->stack, stack_entry,
3601 if (w != win && !ICONIC(w) &&
3602 w->transient == parent->id) {
3608 /* Map focused window. */
3612 /* Stack any children of focus window. */
3613 TAILQ_FOREACH_SAFE(w, &ws->stack, stack_entry, tmpw)
3614 if (w->transient == win->id && !ICONIC(w)) {
3618 } else if (tile_gap < 0 && !ABOVE(win)) {
3620 * Windows overlap in the layout.
3621 * Raise focused win above all tiled wins.
3629 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
3630 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1,
3635 /* Update window border even if workspace is hidden. */
3636 update_window_color(win);
3641 DNPRINTF(SWM_D_FOCUS, "focus_win: done.\n");
3644 /* If a child window should have focus instead, return it. */
3646 get_focus_magic(struct ws_win *win)
3648 struct ws_win *parent = NULL;
3649 struct ws_win *child = NULL;
3651 DNPRINTF(SWM_D_FOCUS, "get_focus_magic: win %#x\n", WINID(win));
3656 parent = find_window(win->transient);
3658 /* If parent prefers focus elsewhere, then try to do so. */
3659 if (parent && (child = parent->focus_child)) {
3660 if (validate_win(child) == 0 && child->mapped)
3663 parent->focus_child = NULL;
3667 /* If this window prefers focus elsewhere, then try to do so. */
3668 if ((child = win->focus_child)) {
3669 if (validate_win(child) == 0 && child->mapped)
3672 win->focus_child = NULL;
3679 event_drain(uint8_t rt)
3681 xcb_generic_event_t *evt;
3683 /* ensure all pending requests have been processed before filtering. */
3685 while ((evt = xcb_poll_for_event(conn))) {
3686 if (XCB_EVENT_RESPONSE_TYPE(evt) != rt)
3694 set_region(struct swm_region *r)
3696 struct swm_region *rf;
3703 /* Unfocus old region bar. */
3708 xcb_change_window_attributes(conn, rf->bar->id,
3709 XCB_CW_BORDER_PIXEL,
3710 &r->s->c[SWM_S_COLOR_BAR_BORDER_UNFOCUS].pixel);
3713 if (rf != NULL && rf != r && (X(rf) != X(r) || Y(rf) != Y(r) ||
3714 WIDTH(rf) != WIDTH(r) || HEIGHT(rf) != HEIGHT(r))) {
3715 /* Set _NET_DESKTOP_GEOMETRY. */
3717 vals[1] = HEIGHT(r);
3718 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, r->s->root,
3719 ewmh[_NET_DESKTOP_GEOMETRY].atom, XCB_ATOM_CARDINAL, 32, 2,
3723 /* Set region bar border to focus_color. */
3724 xcb_change_window_attributes(conn, r->bar->id,
3725 XCB_CW_BORDER_PIXEL, &r->s->c[SWM_S_COLOR_BAR_BORDER].pixel);
3729 ewmh_update_current_desktop();
3733 focus_region(struct swm_region *r)
3736 struct swm_region *old_r;
3741 old_r = r->s->r_focus;
3744 nfw = get_region_focus(r);
3748 /* New region is empty; need to manually unfocus win. */
3750 unfocus_win(old_r->ws->focus);
3752 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT, r->id,
3755 /* Clear bar since empty. */
3761 switchws(struct swm_region *r, union arg *args)
3763 struct swm_region *this_r, *other_r;
3765 struct workspace *new_ws, *old_ws;
3766 xcb_window_t none = XCB_WINDOW_NONE;
3767 int wsid = args->id;
3768 bool unmap_old = false;
3773 if (wsid >= workspace_limit)
3777 old_ws = this_r->ws;
3778 new_ws = &this_r->s->ws[wsid];
3780 DNPRINTF(SWM_D_WS, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
3781 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), old_ws->idx, wsid);
3783 if (new_ws == NULL || old_ws == NULL)
3785 if (new_ws == old_ws)
3788 if ((win = old_ws->focus) != NULL) {
3789 update_window_color(win);
3791 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
3792 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1,
3796 other_r = new_ws->r;
3797 if (other_r == NULL) {
3798 /* the other workspace is hidden, hide this one */
3802 /* the other ws is visible in another region, exchange them */
3803 other_r->ws_prior = new_ws;
3804 other_r->ws = old_ws;
3805 old_ws->r = other_r;
3807 this_r->ws_prior = old_ws;
3808 this_r->ws = new_ws;
3811 /* Set focus_pending before stacking, if needed. */
3812 if (focus_mode != SWM_FOCUS_FOLLOW && (!new_ws->focus_pending ||
3813 validate_win(new_ws->focus_pending))) {
3814 new_ws->focus_pending = get_region_focus(new_ws->r);
3815 new_ws->focus = new_ws->focus_prev;
3816 new_ws->focus_prev = NULL;
3819 new_ws->state = SWM_WS_STATE_MAPPING;
3822 /* unmap old windows */
3824 TAILQ_FOREACH(win, &old_ws->winlist, entry)
3826 old_ws->state = SWM_WS_STATE_HIDDEN;
3829 /* if workspaces were swapped, then don't wait to set focus */
3830 if (old_ws->r && focus_mode != SWM_FOCUS_FOLLOW) {
3831 if (new_ws->focus_pending) {
3832 focus_win(new_ws->focus_pending);
3833 new_ws->focus_pending = NULL;
3837 /* Clear bar and set focus on region input win if new ws is empty. */
3838 if (new_ws->focus_pending == NULL && new_ws->focus == NULL) {
3839 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT, r->id,
3844 ewmh_update_current_desktop();
3848 new_ws->state = SWM_WS_STATE_MAPPED;
3850 DNPRINTF(SWM_D_WS, "switchws: done.\n");
3854 cyclews(struct swm_region *r, union arg *args)
3857 struct swm_screen *s = r->s;
3858 bool cycle_all = false, mv = false;
3860 DNPRINTF(SWM_D_WS, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3861 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
3867 case SWM_ARG_ID_CYCLEWS_MOVE_UP:
3870 case SWM_ARG_ID_CYCLEWS_UP_ALL:
3873 case SWM_ARG_ID_CYCLEWS_UP:
3874 a.id = (a.id < workspace_limit - 1) ? a.id + 1 : 0;
3876 case SWM_ARG_ID_CYCLEWS_MOVE_DOWN:
3879 case SWM_ARG_ID_CYCLEWS_DOWN_ALL:
3882 case SWM_ARG_ID_CYCLEWS_DOWN:
3883 a.id = (a.id > 0) ? a.id - 1 : workspace_limit - 1;
3890 (!cycle_empty && TAILQ_EMPTY(&s->ws[a.id].winlist)))
3892 if (!cycle_visible && s->ws[a.id].r != NULL)
3899 } while (a.id != r->ws->idx);
3901 DNPRINTF(SWM_D_FOCUS, "cyclews: done\n");
3905 priorws(struct swm_region *r, union arg *args)
3911 DNPRINTF(SWM_D_WS, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3912 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
3914 if (r->ws_prior == NULL)
3917 a.id = r->ws_prior->idx;
3919 DNPRINTF(SWM_D_FOCUS, "priorws: done\n");
3923 focusrg(struct swm_region *r, union arg *args)
3925 int ridx = args->id, i, num_screens;
3926 struct swm_region *rr = NULL;
3928 num_screens = get_screen_count();
3929 /* do nothing if we don't have more than one screen */
3930 if (!(num_screens > 1 || outputs > 1))
3933 DNPRINTF(SWM_D_FOCUS, "focusrg: id: %d\n", ridx);
3935 rr = TAILQ_FIRST(&r->s->rl);
3936 for (i = 0; i < ridx && rr != NULL; ++i)
3937 rr = TAILQ_NEXT(rr, entry);
3945 DNPRINTF(SWM_D_FOCUS, "focusrg: done\n");
3949 cyclerg(struct swm_region *r, union arg *args)
3951 struct swm_region *rr = NULL;
3954 num_screens = get_screen_count();
3955 /* do nothing if we don't have more than one screen */
3956 if (!(num_screens > 1 || outputs > 1))
3960 DNPRINTF(SWM_D_FOCUS, "cyclerg: id: %d, region: %d\n", args->id, i);
3963 case SWM_ARG_ID_CYCLERG_UP:
3964 rr = TAILQ_NEXT(r, entry);
3966 rr = TAILQ_FIRST(&screens[i].rl);
3968 case SWM_ARG_ID_CYCLERG_DOWN:
3969 rr = TAILQ_PREV(r, swm_region_list, entry);
3971 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
3982 DNPRINTF(SWM_D_FOCUS, "cyclerg: done\n");
3985 /* Sorts transients after parent. */
3987 sort_windows(struct ws_win_list *wl)
3989 struct ws_win *win, *parent, *nxt;
3994 for (win = TAILQ_FIRST(wl); win != TAILQ_END(wl); win = nxt) {
3995 nxt = TAILQ_NEXT(win, entry);
3997 parent = find_window(win->transient);
3998 if (parent == NULL) {
3999 warnx("not possible bug");
4002 TAILQ_REMOVE(wl, win, entry);
4003 TAILQ_INSERT_AFTER(wl, parent, win, entry);
4009 swapwin(struct swm_region *r, union arg *args)
4011 struct ws_win *target, *source;
4012 struct ws_win *cur_focus;
4013 struct ws_win_list *wl;
4015 DNPRINTF(SWM_D_WS, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
4016 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
4018 cur_focus = r->ws->focus;
4019 if (cur_focus == NULL || FULLSCREEN(cur_focus))
4022 /* Adjust stacking in floating layer. */
4023 if (ABOVE(cur_focus)) {
4025 case SWM_ARG_ID_SWAPPREV:
4026 target = TAILQ_PREV(cur_focus, ws_win_stack,
4028 if (target != NULL && FLOATING(target)) {
4029 TAILQ_REMOVE(&cur_focus->ws->stack, cur_focus,
4031 TAILQ_INSERT_BEFORE(target, cur_focus,
4033 update_win_stacking(cur_focus);
4037 case SWM_ARG_ID_SWAPNEXT:
4038 target = TAILQ_NEXT(cur_focus, stack_entry);
4039 if (target != NULL && FLOATING(target)) {
4040 TAILQ_REMOVE(&cur_focus->ws->stack, cur_focus,
4042 TAILQ_INSERT_AFTER(&cur_focus->ws->stack,
4043 target, cur_focus, stack_entry);
4044 update_win_stacking(cur_focus);
4052 if (r->ws->cur_layout == &layouts[SWM_MAX_STACK])
4055 clear_maximized(r->ws);
4058 wl = &source->ws->winlist;
4061 case SWM_ARG_ID_SWAPPREV:
4063 source = find_window(source->transient);
4064 target = TAILQ_PREV(source, ws_win_list, entry);
4065 if (target && target->transient)
4066 target = find_window(target->transient);
4067 TAILQ_REMOVE(wl, source, entry);
4069 TAILQ_INSERT_TAIL(wl, source, entry);
4071 TAILQ_INSERT_BEFORE(target, source, entry);
4073 case SWM_ARG_ID_SWAPNEXT:
4074 target = TAILQ_NEXT(source, entry);
4075 /* move the parent and let the sort handle the move */
4077 source = find_window(source->transient);
4078 TAILQ_REMOVE(wl, source, entry);
4080 TAILQ_INSERT_HEAD(wl, source, entry);
4082 TAILQ_INSERT_AFTER(wl, target, source, entry);
4084 case SWM_ARG_ID_SWAPMAIN:
4085 target = TAILQ_FIRST(wl);
4086 if (target == source) {
4087 if (source->ws->focus_prev != NULL &&
4088 source->ws->focus_prev != target)
4089 source = source->ws->focus_prev;
4093 if (target == NULL || source == NULL)
4095 source->ws->focus_prev = target;
4096 TAILQ_REMOVE(wl, target, entry);
4097 TAILQ_INSERT_BEFORE(source, target, entry);
4098 TAILQ_REMOVE(wl, source, entry);
4099 TAILQ_INSERT_HEAD(wl, source, entry);
4101 case SWM_ARG_ID_MOVELAST:
4102 TAILQ_REMOVE(wl, source, entry);
4103 TAILQ_INSERT_TAIL(wl, source, entry);
4106 DNPRINTF(SWM_D_MOVE, "swapwin: invalid id: %d\n", args->id);
4111 ewmh_update_client_list();
4116 DNPRINTF(SWM_D_MOVE, "swapwin: done\n");
4120 get_focus_prev(struct ws_win *win)
4122 struct ws_win *winfocus = NULL;
4123 struct ws_win *cur_focus = NULL;
4124 struct ws_win_list *wl = NULL;
4125 struct workspace *ws = NULL;
4127 if (!(win && win->ws))
4132 cur_focus = ws->focus;
4134 DNPRINTF(SWM_D_FOCUS, "get_focus_prev: win %#x, cur_focus: %#x, "
4135 "focus_prev: %#x\n", WINID(win), WINID(cur_focus),
4136 WINID(ws->focus_prev));
4138 /* pickle, just focus on whatever */
4139 if (cur_focus == NULL) {
4140 /* use prev_focus if valid */
4141 if (ws->focus_prev && find_window(ws->focus_prev->id))
4142 winfocus = ws->focus_prev;
4146 /* if transient focus on parent */
4147 if (TRANS(cur_focus)) {
4148 winfocus = find_window(cur_focus->transient);
4152 /* if in max_stack try harder */
4153 if ((win->quirks & SWM_Q_FOCUSPREV) ||
4154 (ws->cur_layout->flags & SWM_L_FOCUSPREV)) {
4155 if (cur_focus != ws->focus_prev)
4156 winfocus = ws->focus_prev;
4158 winfocus = TAILQ_PREV(win, ws_win_list, entry);
4163 DNPRINTF(SWM_D_FOCUS, "get_focus_prev: focus_close: %d\n", focus_close);
4165 if (winfocus == NULL || winfocus == win) {
4166 switch (focus_close) {
4167 case SWM_STACK_BOTTOM:
4168 TAILQ_FOREACH(winfocus, wl, entry)
4169 if (!ICONIC(winfocus) && winfocus != cur_focus)
4173 TAILQ_FOREACH_REVERSE(winfocus, wl, ws_win_list, entry)
4174 if (!ICONIC(winfocus) && winfocus != cur_focus)
4177 case SWM_STACK_ABOVE:
4178 winfocus = TAILQ_NEXT(cur_focus, entry);
4179 while (winfocus && ICONIC(winfocus))
4180 winfocus = TAILQ_NEXT(winfocus, entry);
4182 if (winfocus == NULL) {
4183 if (focus_close_wrap) {
4184 TAILQ_FOREACH(winfocus, wl, entry)
4185 if (!ICONIC(winfocus) &&
4186 winfocus != cur_focus)
4189 TAILQ_FOREACH_REVERSE(winfocus, wl,
4191 if (!ICONIC(winfocus) &&
4192 winfocus != cur_focus)
4197 case SWM_STACK_BELOW:
4198 winfocus = TAILQ_PREV(cur_focus, ws_win_list, entry);
4199 while (winfocus && ICONIC(winfocus))
4200 winfocus = TAILQ_PREV(winfocus, ws_win_list,
4203 if (winfocus == NULL) {
4204 if (focus_close_wrap) {
4205 TAILQ_FOREACH_REVERSE(winfocus, wl,
4207 if (!ICONIC(winfocus) &&
4208 winfocus != cur_focus)
4211 TAILQ_FOREACH(winfocus, wl, entry)
4212 if (!ICONIC(winfocus) &&
4213 winfocus != cur_focus)
4221 if (winfocus == NULL ||
4222 (winfocus && (ICONIC(winfocus) || winfocus == cur_focus))) {
4223 if (focus_default == SWM_STACK_TOP) {
4224 TAILQ_FOREACH_REVERSE(winfocus, wl, ws_win_list, entry)
4225 if (!ICONIC(winfocus) && winfocus != cur_focus)
4228 TAILQ_FOREACH(winfocus, wl, entry)
4229 if (!ICONIC(winfocus) && winfocus != cur_focus)
4236 return get_focus_magic(winfocus);
4240 get_region_focus(struct swm_region *r)
4242 struct ws_win *winfocus = NULL;
4247 if (r->ws->focus && !ICONIC(r->ws->focus))
4248 winfocus = r->ws->focus;
4249 else if (r->ws->focus_prev && !ICONIC(r->ws->focus_prev))
4250 winfocus = r->ws->focus_prev;
4252 TAILQ_FOREACH(winfocus, &r->ws->winlist, entry)
4253 if (!ICONIC(winfocus))
4256 return get_focus_magic(winfocus);
4260 focus(struct swm_region *r, union arg *args)
4262 struct ws_win *head, *cur_focus = NULL, *winfocus = NULL;
4263 struct ws_win_list *wl = NULL;
4264 struct workspace *ws = NULL;
4267 xcb_icccm_wm_hints_t hints;
4272 cur_focus = r->ws->focus;
4276 DNPRINTF(SWM_D_FOCUS, "focus: id: %d, cur_focus: %#x\n", args->id,
4279 /* Make sure an uniconified window has focus, if one exists. */
4280 if (cur_focus == NULL) {
4281 cur_focus = TAILQ_FIRST(wl);
4282 while (cur_focus != NULL && ICONIC(cur_focus))
4283 cur_focus = TAILQ_NEXT(cur_focus, entry);
4285 DNPRINTF(SWM_D_FOCUS, "focus: new cur_focus: %#x\n",
4290 case SWM_ARG_ID_FOCUSPREV:
4291 if (cur_focus == NULL)
4294 winfocus = cur_focus;
4296 winfocus = TAILQ_PREV(winfocus, ws_win_list, entry);
4297 if (winfocus == NULL)
4298 winfocus = TAILQ_LAST(wl, ws_win_list);
4299 if (winfocus == cur_focus)
4301 } while (winfocus && (ICONIC(winfocus) ||
4302 winfocus->id == cur_focus->transient ||
4303 (cur_focus->transient != XCB_WINDOW_NONE &&
4304 winfocus->transient == cur_focus->transient)));
4306 case SWM_ARG_ID_FOCUSNEXT:
4307 if (cur_focus == NULL)
4310 winfocus = cur_focus;
4312 winfocus = TAILQ_NEXT(winfocus, entry);
4313 if (winfocus == NULL)
4314 winfocus = TAILQ_FIRST(wl);
4315 if (winfocus == cur_focus)
4317 } while (winfocus && (ICONIC(winfocus) ||
4318 winfocus->id == cur_focus->transient ||
4319 (cur_focus->transient != XCB_WINDOW_NONE &&
4320 winfocus->transient == cur_focus->transient)));
4322 case SWM_ARG_ID_FOCUSMAIN:
4323 if (cur_focus == NULL)
4326 winfocus = TAILQ_FIRST(wl);
4327 if (winfocus == cur_focus)
4328 winfocus = cur_focus->ws->focus_prev;
4330 case SWM_ARG_ID_FOCUSURGENT:
4331 /* Search forward for the next urgent window. */
4335 for (i = 0; i <= workspace_limit; ++i) {
4337 head = TAILQ_FIRST(&r->s->ws[(ws->idx + i) %
4338 workspace_limit].winlist);
4340 while (head != NULL &&
4341 (head = TAILQ_NEXT(head, entry)) != NULL) {
4342 if (head == cur_focus) {
4343 winfocus = cur_focus;
4346 if (xcb_icccm_get_wm_hints_reply(conn,
4347 xcb_icccm_get_wm_hints(conn, head->id),
4348 &hints, NULL) != 0 &&
4349 xcb_icccm_wm_hints_get_urgency(&hints)) {
4355 if (winfocus != NULL)
4359 /* Switch ws if new focus is on a different ws. */
4360 if (winfocus != NULL && winfocus->ws != ws) {
4361 a.id = winfocus->ws->idx;
4369 if (clear_maximized(ws) > 0)
4372 focus_win(get_focus_magic(winfocus));
4377 DNPRINTF(SWM_D_FOCUS, "focus: done\n");
4381 cycle_layout(struct swm_region *r, union arg *args)
4383 struct workspace *ws = r->ws;
4385 /* suppress unused warning since var is needed */
4388 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
4391 if (ws->cur_layout->l_stack == NULL)
4392 ws->cur_layout = &layouts[0];
4394 clear_maximized(ws);
4399 focus_win(get_region_focus(r));
4405 stack_config(struct swm_region *r, union arg *args)
4407 struct workspace *ws = r->ws;
4409 DNPRINTF(SWM_D_STACK, "stack_config: id: %d workspace: %d\n",
4412 if (clear_maximized(ws) > 0)
4415 if (ws->cur_layout->l_config != NULL)
4416 ws->cur_layout->l_config(ws, args->id);
4418 if (args->id != SWM_ARG_ID_STACKINIT)
4427 struct swm_geometry g;
4428 struct swm_region *r, *r_prev = NULL;
4435 DNPRINTF(SWM_D_STACK, "stack: begin\n");
4437 num_screens = get_screen_count();
4438 for (i = 0; i < num_screens; i++) {
4442 TAILQ_FOREACH(r, &screens[i].rl, entry) {
4443 /* Adjust stack area for region bar and padding. */
4445 g.x += region_padding;
4446 g.y += region_padding;
4447 g.w -= 2 * border_width + 2 * region_padding;
4448 g.h -= 2 * border_width + 2 * region_padding;
4449 if (bar_enabled && r->ws->bar_enabled) {
4455 DNPRINTF(SWM_D_STACK, "stack: workspace: %d (screen: "
4456 "%d, region: %d), (x,y) WxH: (%d,%d) %d x %d\n",
4457 r->ws->idx, i, j++, g.x, g.y, g.w, g.h);
4460 /* Stack bar/input relative to prev. region. */
4461 val[1] = XCB_STACK_MODE_ABOVE;
4463 val[0] = r_prev->id;
4464 DNPRINTF(SWM_D_STACK, "stack: region input %#x "
4465 "relative to %#x.\n", r->id, val[0]);
4466 xcb_configure_window(conn, r->id,
4467 XCB_CONFIG_WINDOW_SIBLING |
4468 XCB_CONFIG_WINDOW_STACK_MODE, val);
4470 val[0] = r_prev->bar->id;
4471 DNPRINTF(SWM_D_STACK, "stack: region bar %#x "
4472 "relative to %#x.\n", r->bar->id, val[0]);
4473 xcb_configure_window(conn, r->bar->id,
4474 XCB_CONFIG_WINDOW_SIBLING |
4475 XCB_CONFIG_WINDOW_STACK_MODE, val);
4478 r->ws->cur_layout->l_stack(r->ws, &g);
4479 r->ws->cur_layout->l_string(r->ws);
4480 /* save r so we can track region changes */
4488 DNPRINTF(SWM_D_STACK, "stack: end\n");
4492 store_float_geom(struct ws_win *win)
4494 if (win == NULL || win->ws->r == NULL)
4497 /* retain window geom and region geom */
4498 win->g_float = win->g;
4499 win->g_float.x -= X(win->ws->r);
4500 win->g_float.y -= Y(win->ws->r);
4501 win->g_floatvalid = true;
4502 DNPRINTF(SWM_D_MISC, "store_float_geom: win %#x, g: (%d,%d)"
4503 " %d x %d, g_float: (%d,%d) %d x %d\n", win->id, X(win), Y(win),
4504 WIDTH(win), HEIGHT(win), win->g_float.x, win->g_float.y,
4505 win->g_float.w, win->g_float.h);
4509 load_float_geom(struct ws_win *win)
4511 if (win == NULL || win->ws->r == NULL)
4514 if (win->g_floatvalid) {
4515 win->g = win->g_float;
4516 X(win) += X(win->ws->r);
4517 Y(win) += Y(win->ws->r);
4518 DNPRINTF(SWM_D_MISC, "load_float_geom: win %#x, g: (%d,%d)"
4519 "%d x %d\n", win->id, X(win), Y(win), WIDTH(win),
4522 DNPRINTF(SWM_D_MISC, "load_float_geom: win %#x, g_float "
4523 "is not set.\n", win->id);
4528 update_floater(struct ws_win *win)
4530 struct workspace *ws;
4531 struct swm_region *r;
4538 if ((r = ws->r) == NULL)
4541 DNPRINTF(SWM_D_MISC, "update_floater: win %#x\n", win->id);
4543 win->bordered = true;
4545 if (FULLSCREEN(win)) {
4546 /* _NET_WM_FULLSCREEN: fullscreen without border. */
4547 if (!win->g_floatvalid)
4548 store_float_geom(win);
4551 win->bordered = false;
4552 } else if (MAXIMIZED(win)) {
4553 /* Maximize: like a single stacked window. */
4554 if (!win->g_floatvalid)
4555 store_float_geom(win);
4559 if (bar_enabled && ws->bar_enabled) {
4561 Y(win) += bar_height;
4562 HEIGHT(win) -= bar_height;
4563 } else if (disable_border) {
4564 win->bordered = false;
4567 if (win->bordered) {
4568 HEIGHT(win) -= 2 * border_width;
4569 WIDTH(win) -= 2 * border_width;
4572 /* Normal floating window. */
4573 /* Update geometry if new region. */
4575 load_float_geom(win);
4577 if ((win->quirks & SWM_Q_FULLSCREEN) &&
4578 WIDTH(win) >= WIDTH(r) && HEIGHT(win) >= HEIGHT(r)) {
4579 /* Remove border for FULLSCREEN quirk. */
4580 win->bordered = false;
4581 } else if (!MANUAL(win)) {
4582 if (TRANS(win) && (win->quirks & SWM_Q_TRANSSZ)) {
4583 /* Adjust size on TRANSSZ quirk. */
4584 WIDTH(win) = (double)WIDTH(r) * dialog_ratio;
4585 HEIGHT(win) = (double)HEIGHT(r) * dialog_ratio;
4588 if (!(win->quirks & SWM_Q_ANYWHERE)) {
4590 * Floaters and transients are auto-centred
4591 * unless manually moved, resized or ANYWHERE
4594 X(win) = X(r) + (WIDTH(r) - WIDTH(win)) / 2 -
4596 Y(win) = Y(r) + (HEIGHT(r) - HEIGHT(win)) / 2 -
4598 store_float_geom(win);
4603 /* Ensure at least 1 pixel of the window is in the region. */
4604 region_containment(win, r, SWM_CW_ALLSIDES);
4610 * Send keystrokes to terminal to decrease/increase the font size as the
4611 * window size changes.
4614 adjust_font(struct ws_win *win)
4616 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
4617 ABOVE(win) || TRANS(win))
4620 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
4621 WIDTH(win) / win->sh.width_inc < term_width &&
4622 win->font_steps < SWM_MAX_FONT_STEPS) {
4623 win->font_size_boundary[win->font_steps] =
4624 (win->sh.width_inc * term_width) + win->sh.base_width;
4627 win->last_inc = win->sh.width_inc;
4628 fake_keypress(win, XK_KP_Subtract, XCB_MOD_MASK_SHIFT);
4629 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
4630 WIDTH(win) > win->font_size_boundary[win->font_steps - 1]) {
4633 win->last_inc = win->sh.width_inc;
4634 fake_keypress(win, XK_KP_Add, XCB_MOD_MASK_SHIFT);
4638 #define SWAPXY(g) do { \
4640 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
4641 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
4644 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, bool flip)
4646 struct swm_geometry win_g, r_g = *g;
4648 int i, j, s, stacks;
4649 int w_inc = 1, h_inc, w_base = 1, h_base;
4650 int hrh, extra = 0, h_slice, last_h = 0;
4651 int split, colno, winno, mwin, msize, mscale;
4652 int remain, missing, v_slice;
4653 bool bordered = true, reconfigure = false;
4655 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d, rot: %s, "
4656 "flip: %s\n", ws->idx, YESNO(rot), YESNO(flip));
4658 /* Prepare tiling variables, if needed. */
4659 if ((winno = count_win(ws, false)) > 0) {
4660 /* Find first tiled window. */
4661 TAILQ_FOREACH(win, &ws->winlist, entry)
4662 if (!FLOATING(win) && !ICONIC(win))
4665 /* Take into account size hints of first tiled window. */
4667 w_inc = win->sh.width_inc;
4668 w_base = win->sh.base_width;
4669 mwin = ws->l_state.horizontal_mwin;
4670 mscale = ws->l_state.horizontal_msize;
4671 stacks = ws->l_state.horizontal_stacks;
4674 w_inc = win->sh.height_inc;
4675 w_base = win->sh.base_height;
4676 mwin = ws->l_state.vertical_mwin;
4677 mscale = ws->l_state.vertical_msize;
4678 stacks = ws->l_state.vertical_stacks;
4682 if (stacks > winno - mwin)
4683 stacks = winno - mwin;
4687 h_slice = r_g.h / SWM_H_SLICE;
4688 if (mwin && winno > mwin) {
4689 v_slice = r_g.w / SWM_V_SLICE;
4693 win_g.w = v_slice * mscale;
4695 if (w_inc > 1 && w_inc < v_slice) {
4696 /* Adjust for requested size increment. */
4697 remain = (win_g.w - w_base) % w_inc;
4703 win_g.x += r_g.w - msize;
4706 colno = split = winno / stacks;
4707 win_g.w = ((r_g.w - (stacks * 2 * border_width) +
4708 2 * border_width) / stacks);
4710 hrh = r_g.h / colno;
4711 extra = r_g.h - (colno * hrh);
4712 win_g.h = hrh - 2 * border_width;
4713 i = j = 0, s = stacks;
4716 /* Update window geometry. */
4717 TAILQ_FOREACH(win, &ws->winlist, entry) {
4721 if (FLOATING(win)) {
4722 update_floater(win);
4727 if (split && i == split) {
4728 colno = (winno - mwin) / stacks;
4729 if (s <= (winno - mwin) % stacks)
4732 hrh = r_g.h / colno;
4733 extra = r_g.h - (colno * hrh);
4736 win_g.x += win_g.w + 2 * border_width +
4739 win_g.w = (r_g.w - msize -
4740 (stacks * (2 * border_width + tile_gap))) / stacks;
4742 win_g.w += (r_g.w - msize -
4743 (stacks * (2 * border_width + tile_gap))) %
4747 win_g.x -= win_g.w + 2 * border_width +
4753 win_g.h = hrh - 2 * border_width - tile_gap;
4756 h_inc = win->sh.width_inc;
4757 h_base = win->sh.base_width;
4759 h_inc = win->sh.height_inc;
4760 h_base = win->sh.base_height;
4763 if (j == colno - 1) {
4764 win_g.h = hrh + extra;
4765 } else if (h_inc > 1 && h_inc < h_slice) {
4766 /* adjust for window's requested size increment */
4767 remain = (win_g.h - h_base) % h_inc;
4768 missing = h_inc - remain;
4770 if (missing <= extra || j == 0) {
4782 win_g.y += last_h + 2 * border_width + tile_gap;
4784 if (disable_border && !(bar_enabled && ws->bar_enabled) &&
4787 win_g.w += 2 * border_width;
4788 win_g.h += 2 * border_width;
4794 if (X(win) != win_g.y || Y(win) != win_g.x ||
4795 WIDTH(win) != win_g.h || HEIGHT(win) != win_g.w) {
4799 WIDTH(win) = win_g.h;
4800 HEIGHT(win) = win_g.w;
4803 if (X(win) != win_g.x || Y(win) != win_g.y ||
4804 WIDTH(win) != win_g.w || HEIGHT(win) != win_g.h) {
4808 WIDTH(win) = win_g.w;
4809 HEIGHT(win) = win_g.h;
4813 if (bordered != win->bordered) {
4815 win->bordered = bordered;
4828 /* Stack all windows from bottom up. */
4829 TAILQ_FOREACH_REVERSE(win, &ws->stack, ws_win_stack, stack_entry)
4831 update_win_stacking(win);
4833 /* Map all windows from top down. */
4834 TAILQ_FOREACH(win, &ws->stack, stack_entry)
4838 DNPRINTF(SWM_D_STACK, "stack_master: done\n");
4842 vertical_config(struct workspace *ws, int id)
4844 DNPRINTF(SWM_D_STACK, "vertical_config: id: %d, workspace: %d\n",
4848 case SWM_ARG_ID_STACKRESET:
4849 case SWM_ARG_ID_STACKINIT:
4850 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
4851 ws->l_state.vertical_mwin = 1;
4852 ws->l_state.vertical_stacks = 1;
4854 case SWM_ARG_ID_MASTERSHRINK:
4855 if (ws->l_state.vertical_msize > 1)
4856 ws->l_state.vertical_msize--;
4858 case SWM_ARG_ID_MASTERGROW:
4859 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
4860 ws->l_state.vertical_msize++;
4862 case SWM_ARG_ID_MASTERADD:
4863 ws->l_state.vertical_mwin++;
4865 case SWM_ARG_ID_MASTERDEL:
4866 if (ws->l_state.vertical_mwin > 0)
4867 ws->l_state.vertical_mwin--;
4869 case SWM_ARG_ID_STACKINC:
4870 ws->l_state.vertical_stacks++;
4872 case SWM_ARG_ID_STACKDEC:
4873 if (ws->l_state.vertical_stacks > 1)
4874 ws->l_state.vertical_stacks--;
4876 case SWM_ARG_ID_FLIPLAYOUT:
4877 ws->l_state.vertical_flip = !ws->l_state.vertical_flip;
4885 vertical_stack(struct workspace *ws, struct swm_geometry *g)
4887 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
4889 stack_master(ws, g, 0, ws->l_state.vertical_flip);
4893 horizontal_config(struct workspace *ws, int id)
4895 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
4898 case SWM_ARG_ID_STACKRESET:
4899 case SWM_ARG_ID_STACKINIT:
4900 ws->l_state.horizontal_mwin = 1;
4901 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
4902 ws->l_state.horizontal_stacks = 1;
4904 case SWM_ARG_ID_MASTERSHRINK:
4905 if (ws->l_state.horizontal_msize > 1)
4906 ws->l_state.horizontal_msize--;
4908 case SWM_ARG_ID_MASTERGROW:
4909 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
4910 ws->l_state.horizontal_msize++;
4912 case SWM_ARG_ID_MASTERADD:
4913 ws->l_state.horizontal_mwin++;
4915 case SWM_ARG_ID_MASTERDEL:
4916 if (ws->l_state.horizontal_mwin > 0)
4917 ws->l_state.horizontal_mwin--;
4919 case SWM_ARG_ID_STACKINC:
4920 ws->l_state.horizontal_stacks++;
4922 case SWM_ARG_ID_STACKDEC:
4923 if (ws->l_state.horizontal_stacks > 1)
4924 ws->l_state.horizontal_stacks--;
4926 case SWM_ARG_ID_FLIPLAYOUT:
4927 ws->l_state.horizontal_flip = !ws->l_state.horizontal_flip;
4935 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
4937 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
4939 stack_master(ws, g, 1, ws->l_state.horizontal_flip);
4942 /* fullscreen view */
4944 max_stack(struct workspace *ws, struct swm_geometry *g)
4946 struct swm_geometry gg = *g;
4947 struct ws_win *w, *win = NULL, *parent = NULL, *tmpw;
4950 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
4955 winno = count_win(ws, false);
4956 if (winno == 0 && count_win(ws, true) == 0)
4959 /* Figure out which top level window should be visible. */
4960 if (ws->focus_pending)
4961 win = ws->focus_pending;
4964 else if (ws->focus_prev)
4965 win = ws->focus_prev;
4967 win = TAILQ_FIRST(&ws->winlist);
4969 DNPRINTF(SWM_D_STACK, "max_stack: focus_pending: %#x, focus: %#x, "
4970 "focus_prev: %#x, first: %#x, win: %#x\n", WINID(ws->focus_pending),
4971 WINID(ws->focus), WINID(ws->focus_prev),
4972 WINID(TAILQ_FIRST(&ws->winlist)), win->id);
4974 /* Update window geometry. */
4975 TAILQ_FOREACH(w, &ws->winlist, entry) {
4984 /* Set maximized flag for all maxed windows. */
4985 if (!MAXIMIZED(w)) {
4986 /* Preserve floating geometry. */
4988 store_float_geom(w);
4990 ewmh_apply_flags(w, w->ewmh_flags | EWMH_F_MAXIMIZED);
4991 ewmh_update_wm_state(w);
4994 /* Only reconfigure if necessary. */
4995 if (X(w) != gg.x || Y(w) != gg.y || WIDTH(w) != gg.w ||
4996 HEIGHT(w) != gg.h) {
4998 if (bar_enabled && ws->bar_enabled){
5001 w->bordered = false;
5002 WIDTH(w) += 2 * border_width;
5003 HEIGHT(w) += 2 * border_width;
5010 /* If transient, stack parent and its children. */
5011 if (TRANS(win) && (parent = find_window(win->transient))) {
5012 raise_window(parent);
5014 TAILQ_FOREACH_SAFE(w, &ws->stack, stack_entry, tmpw)
5015 if (w->transient == parent->id)
5019 /* Make sure focus window is on top. */
5022 /* Stack any children of focus window. */
5023 TAILQ_FOREACH_SAFE(w, &ws->stack, stack_entry, tmpw)
5024 if (w->transient == win->id)
5027 /* Map all windows. */
5028 TAILQ_FOREACH(w, &ws->stack, stack_entry)
5034 send_to_rg(struct swm_region *r, union arg *args)
5036 int ridx = args->id, i, num_screens;
5037 struct swm_region *rr = NULL;
5040 num_screens = get_screen_count();
5041 /* do nothing if we don't have more than one screen */
5042 if (!(num_screens > 1 || outputs > 1))
5045 DNPRINTF(SWM_D_FOCUS, "send_to_rg: id: %d\n", ridx);
5047 rr = TAILQ_FIRST(&r->s->rl);
5048 for (i = 0; i < ridx && rr != NULL; ++i)
5049 rr = TAILQ_NEXT(rr, entry);
5060 region_under(struct swm_screen *s, int x, int y)
5062 struct swm_region *r = NULL;
5064 TAILQ_FOREACH(r, &s->rl, entry) {
5065 DNPRINTF(SWM_D_MISC, "region_under: ws: %d, region g: (%d,%d) "
5066 "%d x %d, coords: (%d,%d)\n", r->ws->idx, X(r), Y(r),
5067 WIDTH(r), HEIGHT(r), x, y);
5068 if (X(r) <= x && x < MAX_X(r))
5069 if (Y(r) <= y && y < MAX_Y(r))
5076 /* Transfer focused window to target workspace and focus. */
5078 send_to_ws(struct swm_region *r, union arg *args)
5080 int wsid = args->id;
5081 struct ws_win *win = NULL;
5083 if (r && r->ws && r->ws->focus)
5088 DNPRINTF(SWM_D_MOVE, "send_to_ws: win %#x, ws %d\n", win->id, wsid);
5090 if (wsid < 0 || wsid >= workspace_limit)
5093 if (win->ws->idx == wsid)
5096 win_to_ws(win, wsid, true);
5098 /* Set new focus on target ws. */
5099 if (focus_mode != SWM_FOCUS_FOLLOW) {
5100 win->ws->focus_prev = win->ws->focus;
5101 win->ws->focus = win;
5102 win->ws->focus_pending = NULL;
5104 if (win->ws->focus_prev)
5105 update_window_color(win->ws->focus_prev);
5108 DNPRINTF(SWM_D_STACK, "send_to_ws: focus_pending: %#x, focus: %#x, "
5109 "focus_prev: %#x, first: %#x, win: %#x\n",
5110 WINID(r->ws->focus_pending), WINID(r->ws->focus),
5111 WINID(r->ws->focus_prev), WINID(TAILQ_FIRST(&r->ws->winlist)),
5114 ewmh_apply_flags(win, win->ewmh_flags & ~EWMH_F_MAXIMIZED);
5115 ewmh_update_wm_state(win);
5117 /* Restack and set new focus on current ws. */
5119 load_float_geom(win);
5123 if (focus_mode != SWM_FOCUS_FOLLOW) {
5124 if (r->ws->focus != NULL) {
5125 focus_win(r->ws->focus);
5127 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT, r->id,
5138 win_to_ws(struct ws_win *win, int wsid, bool unfocus)
5140 struct ws_win *parent;
5141 struct workspace *ws, *nws, *pws;
5143 if (wsid < 0 || wsid >= workspace_limit)
5146 if (win->ws->idx == wsid)
5150 nws = &win->s->ws[wsid];
5152 DNPRINTF(SWM_D_MOVE, "win_to_ws: win %#x, ws %d -> %d\n", win->id,
5155 /* Cleanup focus on source ws. */
5156 if (focus_mode != SWM_FOCUS_FOLLOW &&
5157 (ws->focus == win || ws->focus_pending == win))
5158 ws->focus_pending = get_focus_prev(win);
5160 /* Move the parent if this is a transient window. */
5162 parent = find_window(win->transient);
5165 /* Set new focus in parent's ws if needed. */
5166 if (pws->focus == parent) {
5167 if (focus_mode != SWM_FOCUS_FOLLOW)
5168 pws->focus_pending =
5169 get_focus_prev(parent);
5171 unfocus_win(parent);
5173 if (focus_mode != SWM_FOCUS_FOLLOW) {
5174 pws->focus = pws->focus_pending;
5175 pws->focus_pending = NULL;
5179 /* Don't unmap parent if new ws is visible */
5181 unmap_window(parent);
5184 TAILQ_REMOVE(&ws->winlist, parent, entry);
5185 TAILQ_REMOVE(&ws->stack, parent, stack_entry);
5186 TAILQ_INSERT_TAIL(&nws->winlist, parent, entry);
5187 TAILQ_INSERT_TAIL(&nws->stack, parent, stack_entry);
5190 DNPRINTF(SWM_D_PROP, "win_to_ws: set property: "
5191 "_NET_WM_DESKTOP: %d\n", wsid);
5192 xcb_change_property(conn, XCB_PROP_MODE_REPLACE,
5193 parent->id, ewmh[_NET_WM_DESKTOP].atom,
5194 XCB_ATOM_CARDINAL, 32, 1, &wsid);
5201 if (ws->focus_prev == win)
5202 ws->focus_prev = NULL;
5204 if (focus_mode != SWM_FOCUS_FOLLOW && ws->focus_pending != NULL) {
5205 ws->focus = ws->focus_pending;
5206 ws->focus_pending = NULL;
5209 /* Don't unmap if new ws is visible */
5214 TAILQ_REMOVE(&ws->winlist, win, entry);
5215 TAILQ_REMOVE(&ws->stack, win, stack_entry);
5216 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
5217 TAILQ_INSERT_TAIL(&nws->stack, win, stack_entry);
5220 /* Update the window's workspace property: _NET_WM_DESKTOP */
5221 DNPRINTF(SWM_D_PROP, "win_to_ws: set property: "
5222 "_NET_WM_DESKTOP: %d\n", wsid);
5223 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
5224 ewmh[_NET_WM_DESKTOP].atom, XCB_ATOM_CARDINAL, 32, 1, &wsid);
5226 ewmh_update_client_list();
5228 DNPRINTF(SWM_D_MOVE, "win_to_ws: done.\n");
5232 pressbutton(struct swm_region *r, union arg *args)
5234 /* suppress unused warning since var is needed */
5237 xcb_test_fake_input(conn, XCB_BUTTON_PRESS, args->id,
5238 XCB_CURRENT_TIME, XCB_WINDOW_NONE, 0, 0, 0);
5239 xcb_test_fake_input(conn, XCB_BUTTON_RELEASE, args->id,
5240 XCB_CURRENT_TIME, XCB_WINDOW_NONE, 0, 0, 0);
5244 raise_toggle(struct swm_region *r, union arg *args)
5246 /* suppress unused warning since var is needed */
5249 if (r == NULL || r->ws == NULL)
5252 if (r->ws->focus && MAXIMIZED(r->ws->focus))
5255 r->ws->always_raise = !r->ws->always_raise;
5257 /* bring floaters back to top */
5258 if (!r->ws->always_raise)
5265 iconify(struct swm_region *r, union arg *args)
5268 /* suppress unused warning since var is needed */
5271 if ((w = r->ws->focus) == NULL)
5274 ewmh_apply_flags(w, w->ewmh_flags | EWMH_F_HIDDEN);
5275 ewmh_update_wm_state(w);
5283 get_win_name(xcb_window_t win)
5286 xcb_get_property_cookie_t c;
5287 xcb_get_property_reply_t *r;
5289 /* First try _NET_WM_NAME for UTF-8. */
5290 c = xcb_get_property(conn, 0, win, ewmh[_NET_WM_NAME].atom,
5291 XCB_GET_PROPERTY_TYPE_ANY, 0, UINT_MAX);
5292 r = xcb_get_property_reply(conn, c, NULL);
5293 if (r && r->type == XCB_NONE) {
5295 /* Use WM_NAME instead; no UTF-8. */
5296 c = xcb_get_property(conn, 0, win, XCB_ATOM_WM_NAME,
5297 XCB_GET_PROPERTY_TYPE_ANY, 0, UINT_MAX);
5298 r = xcb_get_property_reply(conn, c, NULL);
5301 if (r && r->type != XCB_NONE && r->length > 0)
5302 name = strndup(xcb_get_property_value(r),
5303 xcb_get_property_value_length(r));
5308 err(1, "get_win_name: failed to allocate memory.");
5316 uniconify(struct swm_region *r, union arg *args)
5323 DNPRINTF(SWM_D_MISC, "uniconify\n");
5325 if (r == NULL || r->ws == NULL)
5328 /* make sure we have anything to uniconify */
5329 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
5330 if (win->ws == NULL)
5331 continue; /* should never happen */
5337 DNPRINTF(SWM_D_MISC, "uniconify: count: %d\n", count);
5343 search_resp_action = SWM_SEARCH_UNICONIFY;
5345 spawn_select(r, args, "search", &searchpid);
5347 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
5350 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
5351 if (win->ws == NULL)
5352 continue; /* should never happen */
5356 name = get_win_name(win->id);
5357 fprintf(lfile, "%s.%u\n", name, win->id);
5365 name_workspace(struct swm_region *r, union arg *args)
5369 DNPRINTF(SWM_D_MISC, "name_workspace\n");
5375 search_resp_action = SWM_SEARCH_NAME_WORKSPACE;
5377 spawn_select(r, args, "name_workspace", &searchpid);
5379 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
5382 fprintf(lfile, "%s", "");
5387 search_workspace(struct swm_region *r, union arg *args)
5390 struct workspace *ws;
5393 DNPRINTF(SWM_D_MISC, "search_workspace\n");
5399 search_resp_action = SWM_SEARCH_SEARCH_WORKSPACE;
5401 spawn_select(r, args, "search", &searchpid);
5403 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
5406 for (i = 0; i < workspace_limit; i++) {
5410 fprintf(lfile, "%d%s%s\n", ws->idx + 1,
5411 (ws->name ? ":" : ""), (ws->name ? ws->name : ""));
5418 search_win_cleanup(void)
5420 struct search_window *sw = NULL;
5422 while ((sw = TAILQ_FIRST(&search_wl)) != NULL) {
5423 xcb_destroy_window(conn, sw->indicator);
5424 TAILQ_REMOVE(&search_wl, sw, entry);
5430 search_win(struct swm_region *r, union arg *args)
5432 struct ws_win *win = NULL;
5433 struct search_window *sw = NULL;
5436 xcb_screen_t *screen;
5437 int i, width, height;
5445 XRectangle l_ibox, l_lbox;
5447 DNPRINTF(SWM_D_MISC, "search_win\n");
5450 search_resp_action = SWM_SEARCH_SEARCH_WINDOW;
5452 spawn_select(r, args, "search", &searchpid);
5454 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
5457 if ((screen = get_screen(r->s->idx)) == NULL)
5458 errx(1, "ERROR: can't get screen %d.", r->s->idx);
5460 TAILQ_INIT(&search_wl);
5463 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
5467 sw = calloc(1, sizeof(struct search_window));
5469 warn("search_win: calloc");
5471 search_win_cleanup();
5477 snprintf(s, sizeof s, "%d", i);
5480 w = xcb_generate_id(conn);
5481 wa[0] = r->s->c[SWM_S_COLOR_FOCUS].pixel;
5482 wa[1] = r->s->c[SWM_S_COLOR_UNFOCUS].pixel;
5483 wa[2] = screen->default_colormap;
5485 if (bar_font_legacy) {
5486 XmbTextExtents(bar_fs, s, len, &l_ibox, &l_lbox);
5487 width = l_lbox.width + 4;
5488 height = bar_fs_extents->max_logical_extent.height + 4;
5490 XftTextExtentsUtf8(display, bar_font, (FcChar8 *)s, len,
5492 width = info.width + 4;
5493 height = bar_font->height + 4;
5496 xcb_create_window(conn, screen->root_depth, w, win->id, 0, 0,
5497 width, height, 1, XCB_WINDOW_CLASS_INPUT_OUTPUT,
5498 screen->root_visual, XCB_CW_BACK_PIXEL |
5499 XCB_CW_BORDER_PIXEL | XCB_CW_COLORMAP, wa);
5501 xcb_map_window(conn, w);
5504 TAILQ_INSERT_TAIL(&search_wl, sw, entry);
5506 if (bar_font_legacy) {
5507 l_gcv.graphics_exposures = 0;
5508 l_draw = XCreateGC(display, w, 0, &l_gcv);
5510 XSetForeground(display, l_draw,
5511 r->s->c[SWM_S_COLOR_BAR].pixel);
5513 DRAWSTRING(display, w, bar_fs, l_draw, 2,
5514 (bar_fs_extents->max_logical_extent.height -
5515 l_lbox.height) / 2 - l_lbox.y, s, len);
5517 XFreeGC(display, l_draw);
5520 draw = XftDrawCreate(display, w,
5521 DefaultVisual(display, r->s->idx),
5522 DefaultColormap(display, r->s->idx));
5524 XftDrawStringUtf8(draw, &search_font_color, bar_font, 2,
5525 (HEIGHT(r->bar) + bar_font->height) / 2 -
5526 bar_font->descent, (FcChar8 *)s, len);
5528 XftDrawDestroy(draw);
5531 DNPRINTF(SWM_D_MISC, "search_win: mapped win %#x\n", w);
5533 fprintf(lfile, "%d\n", i);
5543 search_resp_uniconify(const char *resp, size_t len)
5549 DNPRINTF(SWM_D_MISC, "search_resp_uniconify: resp: %s\n", resp);
5551 TAILQ_FOREACH(win, &search_r->ws->winlist, entry) {
5554 name = get_win_name(win->id);
5555 if (asprintf(&s, "%s.%u", name, win->id) == -1) {
5560 if (strncmp(s, resp, len) == 0) {
5561 /* XXX this should be a callback to generalize */
5562 ewmh_apply_flags(win, win->ewmh_flags & ~EWMH_F_HIDDEN);
5563 ewmh_update_wm_state(win);
5573 search_resp_name_workspace(const char *resp, size_t len)
5575 struct workspace *ws;
5577 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: resp: %s\n", resp);
5579 if (search_r->ws == NULL)
5584 free(search_r->ws->name);
5585 search_r->ws->name = NULL;
5589 ws->name = strdup(resp);
5590 if (ws->name == NULL) {
5591 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: "
5592 "strdup: %s", strerror(errno));
5596 ewmh_update_desktop_names();
5597 ewmh_get_desktop_names();
5602 ewmh_update_desktop_names(void)
5604 char *name_list = NULL, *p;
5605 int num_screens, i, j, len = 0, tot = 0;
5607 num_screens = get_screen_count();
5608 for (i = 0; i < num_screens; ++i) {
5609 for (j = 0; j < workspace_limit; ++j) {
5610 if (screens[i].ws[j].name != NULL)
5611 len += strlen(screens[i].ws[j].name);
5615 if((name_list = calloc(len, sizeof(char))) == NULL)
5616 err(1, "update_desktop_names: calloc: failed to "
5617 "allocate memory.");
5620 for (j = 0; j < workspace_limit; ++j) {
5621 if (screens[i].ws[j].name != NULL) {
5622 len = strlen(screens[i].ws[j].name);
5623 memcpy(p, screens[i].ws[j].name, len);
5632 xcb_change_property(conn, XCB_PROP_MODE_REPLACE,
5633 screens[i].root, ewmh[_NET_DESKTOP_NAMES].atom,
5634 a_utf8_string, 8, tot, name_list);
5644 ewmh_get_desktop_names(void)
5647 xcb_get_property_cookie_t c;
5648 xcb_get_property_reply_t *r;
5649 int num_screens, i, j, n, k;
5651 num_screens = get_screen_count();
5652 for (i = 0; i < num_screens; ++i) {
5653 for (j = 0; j < workspace_limit; ++j) {
5654 free(screens[i].ws[j].name);
5655 screens[i].ws[j].name = NULL;
5658 c = xcb_get_property(conn, 0, screens[i].root,
5659 ewmh[_NET_DESKTOP_NAMES].atom,
5660 a_utf8_string, 0, UINT32_MAX);
5661 r = xcb_get_property_reply(conn, c, NULL);
5665 names = xcb_get_property_value(r);
5666 n = xcb_get_property_value_length(r);
5668 for (j = 0, k = 0; j < n; ++j) {
5669 if (*(names + j) != '\0') {
5670 screens[i].ws[k].name = strdup(names + j);
5671 j += strlen(names + j);
5680 ewmh_update_client_list(void)
5683 int num_screens, i, j, k = 0, count = 0;
5686 num_screens = get_screen_count();
5687 for (i = 0; i < num_screens; ++i) {
5688 for (j = 0; j < workspace_limit; ++j)
5689 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
5692 DNPRINTF(SWM_D_PROP, "ewmh_update_client_list: win count: %d\n",
5698 wins = calloc(count, sizeof(xcb_window_t));
5700 err(1, "ewmh_update_client_list: calloc: failed to "
5701 "allocate memory.");
5703 for (j = 0, k = 0; j < workspace_limit; ++j)
5704 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
5705 wins[k++] = win->id;
5707 xcb_change_property(conn, XCB_PROP_MODE_REPLACE,
5708 screens[i].root, ewmh[_NET_CLIENT_LIST].atom,
5709 XCB_ATOM_WINDOW, 32, count, wins);
5716 ewmh_update_current_desktop(void)
5720 num_screens = get_screen_count();
5721 for (i = 0; i < num_screens; ++i)
5722 xcb_change_property(conn, XCB_PROP_MODE_REPLACE,
5723 screens[i].root, ewmh[_NET_CURRENT_DESKTOP].atom,
5724 XCB_ATOM_CARDINAL, 32, 1, &screens[i].r_focus->ws->idx);
5728 ewmh_update_desktops(void)
5730 int num_screens, i, j;
5733 vals = calloc(workspace_limit * 2, sizeof(uint32_t));
5735 err(1, "ewmh_update_desktops: calloc: failed to allocate "
5738 num_screens = get_screen_count();
5739 for (i = 0; i < num_screens; i++) {
5740 xcb_change_property(conn, XCB_PROP_MODE_REPLACE,
5741 screens[i].root, ewmh[_NET_NUMBER_OF_DESKTOPS].atom,
5742 XCB_ATOM_CARDINAL, 32, 1, &workspace_limit);
5744 for (j = 0; j < workspace_limit; ++j) {
5745 if (screens[i].ws[j].r != NULL) {
5746 vals[j * 2] = X(screens[i].ws[j].r);
5747 vals[j * 2 + 1] = Y(screens[i].ws[j].r);
5748 } else if (screens[i].ws[j].old_r != NULL) {
5749 vals[j * 2] = X(screens[i].ws[j].old_r);
5750 vals[j * 2 + 1] = Y(screens[i].ws[j].old_r);
5752 vals[j * 2] = vals[j * 2 + 1] = 0;
5756 xcb_change_property(conn, XCB_PROP_MODE_REPLACE,
5757 screens[i].root, ewmh[_NET_DESKTOP_VIEWPORT].atom,
5758 XCB_ATOM_CARDINAL, 32, workspace_limit * 2, vals);
5765 search_resp_search_workspace(const char *resp)
5772 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: resp: %s\n", resp);
5776 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: strdup: %s",
5783 ws_idx = (int)strtonum(q, 1, workspace_limit, &errstr);
5785 DNPRINTF(SWM_D_MISC, "workspace idx is %s: %s",
5792 switchws(search_r, &a);
5796 search_resp_search_window(const char *resp)
5801 struct search_window *sw;
5803 DNPRINTF(SWM_D_MISC, "search_resp_search_window: resp: %s\n", resp);
5807 DNPRINTF(SWM_D_MISC, "search_resp_search_window: strdup: %s",
5812 idx = (int)strtonum(s, 1, INT_MAX, &errstr);
5814 DNPRINTF(SWM_D_MISC, "window idx is %s: %s",
5821 TAILQ_FOREACH(sw, &search_wl, entry)
5822 if (idx == sw->idx) {
5828 #define MAX_RESP_LEN 1024
5831 search_do_resp(void)
5837 DNPRINTF(SWM_D_MISC, "search_do_resp:\n");
5842 if ((resp = calloc(1, MAX_RESP_LEN + 1)) == NULL) {
5843 warn("search: calloc");
5847 rbytes = read(select_resp_pipe[0], resp, MAX_RESP_LEN);
5849 warn("search: read error");
5852 resp[rbytes] = '\0';
5855 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
5856 * newline, so work around that by sanitizing the resp now.
5858 resp[strcspn(resp, "\n")] = '\0';
5861 switch (search_resp_action) {
5862 case SWM_SEARCH_UNICONIFY:
5863 search_resp_uniconify(resp, len);
5865 case SWM_SEARCH_NAME_WORKSPACE:
5866 search_resp_name_workspace(resp, len);
5868 case SWM_SEARCH_SEARCH_WORKSPACE:
5869 search_resp_search_workspace(resp);
5871 case SWM_SEARCH_SEARCH_WINDOW:
5872 search_resp_search_window(resp);
5877 if (search_resp_action == SWM_SEARCH_SEARCH_WINDOW)
5878 search_win_cleanup();
5880 search_resp_action = SWM_SEARCH_NONE;
5881 close(select_resp_pipe[0]);
5888 wkill(struct swm_region *r, union arg *args)
5890 DNPRINTF(SWM_D_MISC, "wkill: win %#x, id: %d\n", WINID(r->ws->focus),
5893 if (r->ws->focus == NULL)
5896 if (args->id == SWM_ARG_ID_KILLWINDOW)
5897 xcb_kill_client(conn, r->ws->focus->id);
5899 if (r->ws->focus->can_delete)
5900 client_msg(r->ws->focus, a_delete, 0);
5906 clear_maximized(struct workspace *ws)
5911 /* Clear any maximized win(s) on ws, from bottom up. */
5912 TAILQ_FOREACH_REVERSE(w, &ws->stack, ws_win_stack, stack_entry)
5914 ewmh_apply_flags(w, w->ewmh_flags & ~EWMH_F_MAXIMIZED);
5915 ewmh_update_wm_state(w);
5923 maximize_toggle(struct swm_region *r, union arg *args)
5925 struct ws_win *w = r->ws->focus;
5927 /* suppress unused warning since var is needed */
5933 DNPRINTF(SWM_D_MISC, "maximize_toggle: win %#x\n", w->id);
5938 if (w->ws->cur_layout == &layouts[SWM_MAX_STACK])
5941 ewmh_apply_flags(w, w->ewmh_flags ^ EWMH_F_MAXIMIZED);
5942 ewmh_update_wm_state(w);
5946 if (w == w->ws->focus)
5950 DNPRINTF(SWM_D_MISC, "maximize_toggle: done\n");
5954 floating_toggle(struct swm_region *r, union arg *args)
5956 struct ws_win *w = r->ws->focus;
5958 /* suppress unused warning since var is needed */
5964 DNPRINTF(SWM_D_MISC, "floating_toggle: win %#x\n", w->id);
5966 if (FULLSCREEN(w) || TRANS(w))
5969 if (w->ws->cur_layout == &layouts[SWM_MAX_STACK])
5972 ewmh_apply_flags(w, w->ewmh_flags ^ EWMH_F_ABOVE);
5973 ewmh_update_wm_state(w);
5977 if (w == w->ws->focus)
5982 DNPRINTF(SWM_D_MISC, "floating_toggle: done\n");
5986 region_containment(struct ws_win *win, struct swm_region *r, int opts)
5988 struct swm_geometry g = r->g;
5989 int rt, lt, tp, bm, bw;
5991 bw = (opts & SWM_CW_SOFTBOUNDARY) ? boundary_width : 0;
5994 * Perpendicular distance of each side of the window to the respective
5995 * side of the region boundary. Positive values indicate the side of
5996 * the window has passed beyond the region boundary.
5998 rt = opts & SWM_CW_RIGHT ? MAX_X(win) + BORDER(win) - MAX_X(r) : bw;
5999 lt = opts & SWM_CW_LEFT ? X(r) - X(win) + BORDER(win) : bw;
6000 bm = opts & SWM_CW_BOTTOM ? MAX_Y(win) + BORDER(win) - MAX_Y(r) : bw;
6001 tp = opts & SWM_CW_TOP ? Y(r) - Y(win) + BORDER(win) : bw;
6003 DNPRINTF(SWM_D_MISC, "region_containment: win %#x, rt: %d, lt: %d, "
6004 "bm: %d, tp: %d, SOFTBOUNDARY: %s, HARDBOUNDARY: %s\n", win->id, rt,
6005 lt, bm, tp, YESNO(opts & SWM_CW_SOFTBOUNDARY),
6006 YESNO(opts & SWM_CW_HARDBOUNDARY));
6009 * Disable containment if any of the flagged sides went beyond the
6010 * containment boundary, or if containment is disabled.
6012 if (!(opts & SWM_CW_HARDBOUNDARY || opts & SWM_CW_SOFTBOUNDARY) ||
6013 (bw != 0 && ((rt > bw) || (lt > bw) || (bm > bw) || (tp > bw)))) {
6014 /* Make sure window has at least 1 pixel in the region */
6015 g.x += 1 - WIDTH(win);
6016 g.y += 1 - HEIGHT(win);
6017 g.w += 2 * WIDTH(win) - 2;
6018 g.h += 2 * HEIGHT(win) - 2;
6021 constrain_window(win, &g, &opts);
6024 /* Move or resize a window so that flagged side(s) fit into the supplied box. */
6026 constrain_window(struct ws_win *win, struct swm_geometry *b, int *opts)
6028 DNPRINTF(SWM_D_MISC, "constrain_window: win %#x, (x,y) w x h: "
6029 "(%d,%d) %d x %d, box: (x,y) w x h: (%d,%d) %d x %d, rt: %s, "
6030 "lt: %s, bt: %s, tp: %s, allow resize: %s\n", win->id, X(win),
6031 Y(win), WIDTH(win), HEIGHT(win), b->x, b->y, b->w, b->h,
6032 YESNO(*opts & SWM_CW_RIGHT), YESNO(*opts & SWM_CW_LEFT),
6033 YESNO(*opts & SWM_CW_BOTTOM), YESNO(*opts & SWM_CW_TOP),
6034 YESNO(*opts & SWM_CW_RESIZABLE));
6036 if ((*opts & SWM_CW_RIGHT) && MAX_X(win) + BORDER(win) > b->x + b->w) {
6037 if (*opts & SWM_CW_RESIZABLE)
6038 WIDTH(win) = b->x + b->w - X(win) - BORDER(win);
6040 X(win) = b->x + b->w - WIDTH(win) - BORDER(win);
6043 if ((*opts & SWM_CW_LEFT) && X(win) + BORDER(win) < b->x) {
6044 if (*opts & SWM_CW_RESIZABLE)
6045 WIDTH(win) -= b->x - X(win) - BORDER(win);
6047 X(win) = b->x - BORDER(win);
6050 if ((*opts & SWM_CW_BOTTOM) && MAX_Y(win) + BORDER(win) > b->y + b->h) {
6051 if (*opts & SWM_CW_RESIZABLE)
6052 HEIGHT(win) = b->y + b->h - Y(win) - BORDER(win);
6054 Y(win) = b->y + b->h - HEIGHT(win) - BORDER(win);
6057 if ((*opts & SWM_CW_TOP) && Y(win) + BORDER(win) < b->y) {
6058 if (*opts & SWM_CW_RESIZABLE)
6059 HEIGHT(win) -= b->y - Y(win) - BORDER(win);
6061 Y(win) = b->y - BORDER(win);
6064 if (*opts & SWM_CW_RESIZABLE) {
6067 if (HEIGHT(win) < 1)
6073 update_window_color(struct ws_win *win)
6077 if (WS_FOCUSED(win->ws) && win->ws->focus == win)
6078 pixel = MAXIMIZED(win) ?
6079 &win->s->c[SWM_S_COLOR_FOCUS_MAXIMIZED].pixel :
6080 &win->s->c[SWM_S_COLOR_FOCUS].pixel;
6082 pixel = MAXIMIZED(win) ?
6083 &win->s->c[SWM_S_COLOR_UNFOCUS_MAXIMIZED].pixel :
6084 &win->s->c[SWM_S_COLOR_UNFOCUS].pixel;
6086 xcb_change_window_attributes(conn, win->id,
6087 XCB_CW_BORDER_PIXEL, pixel);
6091 update_window(struct ws_win *win)
6096 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
6097 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
6098 XCB_CONFIG_WINDOW_BORDER_WIDTH;
6102 wc[3] = HEIGHT(win);
6103 wc[4] = BORDER(win);
6105 DNPRINTF(SWM_D_EVENT, "update_window: win %#x, (x,y) w x h: "
6106 "(%d,%d) %d x %d, bordered: %s\n", win->id, wc[0], wc[1], wc[2],
6107 wc[3], YESNO(win->bordered));
6109 xcb_configure_window(conn, win->id, mask, wc);
6112 #define SWM_RESIZE_STEPS (50)
6115 resize(struct ws_win *win, union arg *args)
6117 xcb_timestamp_t timestamp = 0;
6118 struct swm_region *r = NULL;
6119 struct swm_geometry g;
6121 int top = 0, left = 0;
6123 xcb_cursor_t cursor;
6124 xcb_query_pointer_reply_t *xpr = NULL;
6125 xcb_generic_event_t *evt;
6126 xcb_motion_notify_event_t *mne;
6133 if (FULLSCREEN(win))
6136 /* In max_stack mode, should only resize transients. */
6137 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !TRANS(win))
6140 DNPRINTF(SWM_D_EVENT, "resize: win %#x, floating: %s, "
6141 "transient: %#x\n", win->id, YESNO(ABOVE(win)),
6145 store_float_geom(win);
6146 else if (!(TRANS(win) || ABOVE(win)))
6149 ewmh_apply_flags(win, (win->ewmh_flags | SWM_F_MANUAL | EWMH_F_ABOVE) &
6151 ewmh_update_wm_state(win);
6157 /* It's possible for win to have been freed during focus_flush(). */
6158 if (validate_win(win)) {
6159 DNPRINTF(SWM_D_EVENT, "resize: invalid win.\n");
6164 case SWM_ARG_ID_WIDTHSHRINK:
6165 WIDTH(win) -= SWM_RESIZE_STEPS;
6168 case SWM_ARG_ID_WIDTHGROW:
6169 WIDTH(win) += SWM_RESIZE_STEPS;
6172 case SWM_ARG_ID_HEIGHTSHRINK:
6173 HEIGHT(win) -= SWM_RESIZE_STEPS;
6176 case SWM_ARG_ID_HEIGHTGROW:
6177 HEIGHT(win) += SWM_RESIZE_STEPS;
6184 region_containment(win, r, SWM_CW_ALLSIDES | SWM_CW_RESIZABLE |
6185 SWM_CW_HARDBOUNDARY);
6187 store_float_geom(win);
6191 region_containment(win, r, SWM_CW_ALLSIDES | SWM_CW_RESIZABLE |
6192 SWM_CW_SOFTBOUNDARY);
6195 /* get cursor offset from window root */
6196 xpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, win->id),
6203 if (xpr->win_x < WIDTH(win) / 2)
6206 if (xpr->win_y < HEIGHT(win) / 2)
6209 if (args->id == SWM_ARG_ID_CENTER)
6210 cursor = cursors[XC_SIZING].cid;
6212 cursor = cursors[left ? XC_TOP_LEFT_CORNER :
6213 XC_TOP_RIGHT_CORNER].cid;
6215 cursor = cursors[left ? XC_BOTTOM_LEFT_CORNER :
6216 XC_BOTTOM_RIGHT_CORNER].cid;
6218 xcb_grab_pointer(conn, 0, win->id, MOUSEMASK,
6219 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, XCB_WINDOW_NONE, cursor,
6224 while (resizing && (evt = xcb_wait_for_event(conn))) {
6225 switch (XCB_EVENT_RESPONSE_TYPE(evt)) {
6226 case XCB_BUTTON_RELEASE:
6227 DNPRINTF(SWM_D_EVENT, "resize: BUTTON_RELEASE\n");
6230 case XCB_MOTION_NOTIFY:
6231 mne = (xcb_motion_notify_event_t *)evt;
6232 /* cursor offset/delta from start of the operation */
6233 dx = mne->root_x - xpr->root_x;
6234 dy = mne->root_y - xpr->root_y;
6240 if (args->id == SWM_ARG_ID_CENTER) {
6241 if (g.h / 2 + dy < 1)
6245 HEIGHT(win) = g.h + 2 * dy;
6253 HEIGHT(win) = g.h + dy;
6260 if (args->id == SWM_ARG_ID_CENTER) {
6261 if (g.w / 2 + dx < 1)
6265 WIDTH(win) = g.w + 2 * dx;
6273 WIDTH(win) = g.w + dx;
6276 /* not free, don't sync more than 120 times / second */
6277 if ((mne->time - timestamp) > (1000 / 120) ) {
6278 timestamp = mne->time;
6279 regionize(win, mne->root_x, mne->root_y);
6280 region_containment(win, r, SWM_CW_ALLSIDES |
6281 SWM_CW_RESIZABLE | SWM_CW_HARDBOUNDARY |
6282 SWM_CW_SOFTBOUNDARY);
6289 xcb_allow_events(conn, XCB_ALLOW_ASYNC_KEYBOARD,
6290 ((xcb_key_press_event_t *)evt)->time);
6296 /* It's possible for win to have been freed above. */
6297 if (validate_win(win)) {
6298 DNPRINTF(SWM_D_EVENT, "resize: invalid win.\n");
6306 region_containment(win, r, SWM_CW_ALLSIDES | SWM_CW_RESIZABLE |
6307 SWM_CW_HARDBOUNDARY | SWM_CW_SOFTBOUNDARY);
6311 store_float_geom(win);
6313 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
6315 DNPRINTF(SWM_D_EVENT, "resize: done.\n");
6319 resize_step(struct swm_region *r, union arg *args)
6321 struct ws_win *win = NULL;
6323 if (r && r->ws && r->ws->focus)
6332 /* Try to set window region based on supplied coordinates or window center. */
6334 regionize(struct ws_win *win, int x, int y)
6336 struct swm_region *r = NULL;
6338 r = region_under(win->s, x, y);
6340 r = region_under(win->s, X(win) + WIDTH(win) / 2,
6341 Y(win) + HEIGHT(win) / 2);
6343 if (r != NULL && r != win->ws->r) {
6344 if (clear_maximized(r->ws) > 0)
6347 win_to_ws(win, r->ws->idx, false);
6349 /* Set focus on new ws. */
6350 unfocus_win(r->ws->focus);
6358 #define SWM_MOVE_STEPS (50)
6361 move(struct ws_win *win, union arg *args)
6363 struct swm_region *r;
6364 xcb_timestamp_t timestamp = 0;
6366 xcb_query_pointer_reply_t *qpr = NULL;
6367 xcb_generic_event_t *evt;
6368 xcb_motion_notify_event_t *mne;
6369 bool moving, restack = false;
6374 if ((r = win->ws->r) == NULL)
6377 if (FULLSCREEN(win))
6380 DNPRINTF(SWM_D_EVENT, "move: win %#x, floating: %s, transient: "
6381 "%#x\n", win->id, YESNO(ABOVE(win)), win->transient);
6383 /* in max_stack mode should only move transients */
6384 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !TRANS(win))
6387 if (!(ABOVE(win) || TRANS(win)) || MAXIMIZED(win)) {
6388 store_float_geom(win);
6392 ewmh_apply_flags(win, (win->ewmh_flags | SWM_F_MANUAL | EWMH_F_ABOVE) &
6394 ewmh_update_wm_state(win);
6401 /* It's possible for win to have been freed during focus_flush(). */
6402 if (validate_win(win)) {
6403 DNPRINTF(SWM_D_EVENT, "move: invalid win.\n");
6409 case SWM_ARG_ID_MOVELEFT:
6410 X(win) -= (SWM_MOVE_STEPS - border_width);
6413 case SWM_ARG_ID_MOVERIGHT:
6414 X(win) += (SWM_MOVE_STEPS - border_width);
6417 case SWM_ARG_ID_MOVEUP:
6418 Y(win) -= (SWM_MOVE_STEPS - border_width);
6421 case SWM_ARG_ID_MOVEDOWN:
6422 Y(win) += (SWM_MOVE_STEPS - border_width);
6429 regionize(win, -1, -1);
6430 region_containment(win, win->ws->r, SWM_CW_ALLSIDES);
6432 store_float_geom(win);
6436 xcb_grab_pointer(conn, 0, win->id, MOUSEMASK,
6437 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC,
6438 XCB_WINDOW_NONE, cursors[XC_FLEUR].cid, XCB_CURRENT_TIME);
6440 /* get cursor offset from window root */
6441 qpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, win->id),
6444 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
6448 regionize(win, qpr->root_x, qpr->root_y);
6449 region_containment(win, win->ws->r, SWM_CW_ALLSIDES |
6450 SWM_CW_SOFTBOUNDARY);
6454 while (moving && (evt = xcb_wait_for_event(conn))) {
6455 switch (XCB_EVENT_RESPONSE_TYPE(evt)) {
6456 case XCB_BUTTON_RELEASE:
6457 DNPRINTF(SWM_D_EVENT, "move: BUTTON_RELEASE\n");
6460 case XCB_MOTION_NOTIFY:
6461 mne = (xcb_motion_notify_event_t *)evt;
6462 DNPRINTF(SWM_D_EVENT, "motion: root: %#x\n", mne->root);
6463 X(win) = mne->root_x - qpr->win_x - border_width;
6464 Y(win) = mne->root_y - qpr->win_y - border_width;
6466 /* not free, don't sync more than 120 times / second */
6467 if ((mne->time - timestamp) > (1000 / 120) ) {
6468 timestamp = mne->time;
6469 regionize(win, mne->root_x, mne->root_y);
6470 region_containment(win, win->ws->r,
6471 SWM_CW_ALLSIDES | SWM_CW_SOFTBOUNDARY);
6478 xcb_allow_events(conn, XCB_ALLOW_ASYNC_KEYBOARD,
6479 ((xcb_key_press_event_t *)evt)->time);
6485 /* It's possible for win to have been freed above. */
6486 if (validate_win(win)) {
6487 DNPRINTF(SWM_D_EVENT, "move: invalid win.\n");
6495 region_containment(win, win->ws->r, SWM_CW_ALLSIDES |
6496 SWM_CW_SOFTBOUNDARY);
6500 store_float_geom(win);
6502 /* New region set to fullscreen layout. */
6503 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK]) {
6510 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
6511 DNPRINTF(SWM_D_EVENT, "move: done.\n");
6515 move_step(struct swm_region *r, union arg *args)
6517 struct ws_win *win = NULL;
6519 if (r && r->ws && r->ws->focus)
6524 if (!TRANS(win) && !ABOVE(win))
6531 /* key definitions */
6533 char name[SWM_FUNCNAME_LEN];
6534 void (*func)(struct swm_region *r, union arg *);
6536 } keyfuncs[KF_INVALID + 1] = {
6537 /* name function argument */
6538 { "bar_toggle", bar_toggle, {.id = SWM_ARG_ID_BAR_TOGGLE} },
6539 { "bar_toggle_ws", bar_toggle, {.id = SWM_ARG_ID_BAR_TOGGLE_WS} },
6540 { "button2", pressbutton, {2} },
6541 { "cycle_layout", cycle_layout, {0} },
6542 { "flip_layout", stack_config, {.id = SWM_ARG_ID_FLIPLAYOUT} },
6543 { "float_toggle", floating_toggle,{0} },
6544 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
6545 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
6546 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
6547 { "focus_urgent", focus, {.id = SWM_ARG_ID_FOCUSURGENT} },
6548 { "maximize_toggle", maximize_toggle,{0} },
6549 { "height_grow", resize_step, {.id = SWM_ARG_ID_HEIGHTGROW} },
6550 { "height_shrink", resize_step, {.id = SWM_ARG_ID_HEIGHTSHRINK} },
6551 { "iconify", iconify, {0} },
6552 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
6553 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
6554 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
6555 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
6556 { "move_down", move_step, {.id = SWM_ARG_ID_MOVEDOWN} },
6557 { "move_left", move_step, {.id = SWM_ARG_ID_MOVELEFT} },
6558 { "move_right", move_step, {.id = SWM_ARG_ID_MOVERIGHT} },
6559 { "move_up", move_step, {.id = SWM_ARG_ID_MOVEUP} },
6560 { "mvrg_1", send_to_rg, {.id = 0} },
6561 { "mvrg_2", send_to_rg, {.id = 1} },
6562 { "mvrg_3", send_to_rg, {.id = 2} },
6563 { "mvrg_4", send_to_rg, {.id = 3} },
6564 { "mvrg_5", send_to_rg, {.id = 4} },
6565 { "mvrg_6", send_to_rg, {.id = 5} },
6566 { "mvrg_7", send_to_rg, {.id = 6} },
6567 { "mvrg_8", send_to_rg, {.id = 7} },
6568 { "mvrg_9", send_to_rg, {.id = 8} },
6569 { "mvws_1", send_to_ws, {.id = 0} },
6570 { "mvws_2", send_to_ws, {.id = 1} },
6571 { "mvws_3", send_to_ws, {.id = 2} },
6572 { "mvws_4", send_to_ws, {.id = 3} },
6573 { "mvws_5", send_to_ws, {.id = 4} },
6574 { "mvws_6", send_to_ws, {.id = 5} },
6575 { "mvws_7", send_to_ws, {.id = 6} },
6576 { "mvws_8", send_to_ws, {.id = 7} },
6577 { "mvws_9", send_to_ws, {.id = 8} },
6578 { "mvws_10", send_to_ws, {.id = 9} },
6579 { "mvws_11", send_to_ws, {.id = 10} },
6580 { "mvws_12", send_to_ws, {.id = 11} },
6581 { "mvws_13", send_to_ws, {.id = 12} },
6582 { "mvws_14", send_to_ws, {.id = 13} },
6583 { "mvws_15", send_to_ws, {.id = 14} },
6584 { "mvws_16", send_to_ws, {.id = 15} },
6585 { "mvws_17", send_to_ws, {.id = 16} },
6586 { "mvws_18", send_to_ws, {.id = 17} },
6587 { "mvws_19", send_to_ws, {.id = 18} },
6588 { "mvws_20", send_to_ws, {.id = 19} },
6589 { "mvws_21", send_to_ws, {.id = 20} },
6590 { "mvws_22", send_to_ws, {.id = 21} },
6591 { "name_workspace", name_workspace, {0} },
6592 { "quit", quit, {0} },
6593 { "raise_toggle", raise_toggle, {0} },
6594 { "restart", restart, {0} },
6595 { "rg_1", focusrg, {.id = 0} },
6596 { "rg_2", focusrg, {.id = 1} },
6597 { "rg_3", focusrg, {.id = 2} },
6598 { "rg_4", focusrg, {.id = 3} },
6599 { "rg_5", focusrg, {.id = 4} },
6600 { "rg_6", focusrg, {.id = 5} },
6601 { "rg_7", focusrg, {.id = 6} },
6602 { "rg_8", focusrg, {.id = 7} },
6603 { "rg_9", focusrg, {.id = 8} },
6604 { "rg_next", cyclerg, {.id = SWM_ARG_ID_CYCLERG_UP} },
6605 { "rg_prev", cyclerg, {.id = SWM_ARG_ID_CYCLERG_DOWN} },
6606 { "screen_next", cyclerg, {.id = SWM_ARG_ID_CYCLERG_UP} },
6607 { "screen_prev", cyclerg, {.id = SWM_ARG_ID_CYCLERG_DOWN} },
6608 { "search_win", search_win, {0} },
6609 { "search_workspace", search_workspace, {0} },
6610 { "spawn_custom", NULL, {0} },
6611 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
6612 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
6613 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
6614 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
6615 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
6616 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
6617 { "uniconify", uniconify, {0} },
6618 { "version", version, {0} },
6619 { "width_grow", resize_step, {.id = SWM_ARG_ID_WIDTHGROW} },
6620 { "width_shrink", resize_step, {.id = SWM_ARG_ID_WIDTHSHRINK} },
6621 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
6622 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
6623 { "ws_1", switchws, {.id = 0} },
6624 { "ws_2", switchws, {.id = 1} },
6625 { "ws_3", switchws, {.id = 2} },
6626 { "ws_4", switchws, {.id = 3} },
6627 { "ws_5", switchws, {.id = 4} },
6628 { "ws_6", switchws, {.id = 5} },
6629 { "ws_7", switchws, {.id = 6} },
6630 { "ws_8", switchws, {.id = 7} },
6631 { "ws_9", switchws, {.id = 8} },
6632 { "ws_10", switchws, {.id = 9} },
6633 { "ws_11", switchws, {.id = 10} },
6634 { "ws_12", switchws, {.id = 11} },
6635 { "ws_13", switchws, {.id = 12} },
6636 { "ws_14", switchws, {.id = 13} },
6637 { "ws_15", switchws, {.id = 14} },
6638 { "ws_16", switchws, {.id = 15} },
6639 { "ws_17", switchws, {.id = 16} },
6640 { "ws_18", switchws, {.id = 17} },
6641 { "ws_19", switchws, {.id = 18} },
6642 { "ws_20", switchws, {.id = 19} },
6643 { "ws_21", switchws, {.id = 20} },
6644 { "ws_22", switchws, {.id = 21} },
6645 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
6646 { "ws_next_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP_ALL} },
6647 { "ws_next_move", cyclews, {.id = SWM_ARG_ID_CYCLEWS_MOVE_UP} },
6648 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
6649 { "ws_prev_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN_ALL} },
6650 { "ws_prev_move", cyclews, {.id = SWM_ARG_ID_CYCLEWS_MOVE_DOWN} },
6651 { "ws_prior", priorws, {0} },
6652 { "dumpwins", dumpwins, {0} }, /* MUST BE LAST */
6653 { "invalid key func", NULL, {0} },
6657 key_cmp(struct key *kp1, struct key *kp2)
6659 if (kp1->keysym < kp2->keysym)
6661 if (kp1->keysym > kp2->keysym)
6664 if (kp1->mod < kp2->mod)
6666 if (kp1->mod > kp2->mod)
6673 enum { client_click, root_click };
6675 unsigned int action;
6677 unsigned int button;
6678 void (*func)(struct ws_win *, union arg *);
6681 #define MODKEY_SHIFT MODKEY | XCB_MOD_MASK_SHIFT
6682 /* action key mouse button func args */
6683 { client_click, MODKEY, XCB_BUTTON_INDEX_3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
6684 { client_click, MODKEY_SHIFT, XCB_BUTTON_INDEX_3, resize, {.id = SWM_ARG_ID_CENTER} },
6685 { client_click, MODKEY, XCB_BUTTON_INDEX_1, move, {0} },
6690 update_modkey(unsigned int mod)
6696 RB_FOREACH(kp, key_tree, &keys)
6697 if (kp->mod & XCB_MOD_MASK_SHIFT)
6698 kp->mod = mod | XCB_MOD_MASK_SHIFT;
6702 for (i = 0; i < LENGTH(buttons); i++)
6703 if (buttons[i].mask & XCB_MOD_MASK_SHIFT)
6704 buttons[i].mask = mod | XCB_MOD_MASK_SHIFT;
6706 buttons[i].mask = mod;
6710 spawn_expand(struct swm_region *r, union arg *args, const char *spawn_name,
6713 struct spawn_prog *prog = NULL;
6715 char *ap, **real_args;
6717 /* suppress unused warning since var is needed */
6720 DNPRINTF(SWM_D_SPAWN, "spawn_expand: %s\n", spawn_name);
6723 TAILQ_FOREACH(prog, &spawns, entry) {
6724 if (strcasecmp(spawn_name, prog->name) == 0)
6728 warnx("spawn_custom: program %s not found", spawn_name);
6732 /* make room for expanded args */
6733 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
6734 err(1, "spawn_custom: calloc real_args");
6736 /* expand spawn_args into real_args */
6737 for (i = c = 0; i < prog->argc; i++) {
6739 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg: %s\n", ap);
6740 if (strcasecmp(ap, "$bar_border") == 0) {
6742 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
6744 err(1, "spawn_custom border color");
6745 } else if (strcasecmp(ap, "$bar_color") == 0) {
6747 strdup(r->s->c[SWM_S_COLOR_BAR].name))
6749 err(1, "spawn_custom bar color");
6750 } else if (strcasecmp(ap, "$bar_font") == 0) {
6751 if ((real_args[c] = strdup(bar_fonts))
6753 err(1, "spawn_custom bar fonts");
6754 } else if (strcasecmp(ap, "$bar_font_color") == 0) {
6756 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
6758 err(1, "spawn_custom color font");
6759 } else if (strcasecmp(ap, "$color_focus") == 0) {
6761 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
6763 err(1, "spawn_custom color focus");
6764 } else if (strcasecmp(ap, "$color_focus_maximized") == 0) {
6766 strdup(r->s->c[SWM_S_COLOR_FOCUS_MAXIMIZED].name))
6768 err(1, "spawn_custom color focus maximized");
6769 } else if (strcasecmp(ap, "$color_unfocus") == 0) {
6771 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
6773 err(1, "spawn_custom color unfocus");
6774 } else if (strcasecmp(ap, "$color_unfocus_maximized") == 0) {
6776 strdup(r->s->c[SWM_S_COLOR_UNFOCUS_MAXIMIZED].name))
6778 err(1, "spawn_custom color unfocus maximized");
6779 } else if (strcasecmp(ap, "$region_index") == 0) {
6780 if (asprintf(&real_args[c], "%d",
6781 get_region_index(r) + 1) < 1)
6782 err(1, "spawn_custom region index");
6783 } else if (strcasecmp(ap, "$workspace_index") == 0) {
6784 if (asprintf(&real_args[c], "%d", r->ws->idx + 1) < 1)
6785 err(1, "spawn_custom workspace index");
6786 } else if (strcasecmp(ap, "$dmenu_bottom") == 0) {
6789 if ((real_args[c] = strdup("-b")) == NULL)
6790 err(1, "spawn_custom workspace index");
6792 /* no match --> copy as is */
6793 if ((real_args[c] = strdup(ap)) == NULL)
6794 err(1, "spawn_custom strdup(ap)");
6796 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg: %s\n",
6802 DNPRINTF(SWM_D_SPAWN, "spawn_custom: result: ");
6803 for (i = 0; i < c; ++i)
6804 DPRINTF("\"%s\" ", real_args[i]);
6807 *ret_args = real_args;
6812 spawn_custom(struct swm_region *r, union arg *args, const char *spawn_name)
6818 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
6822 spawn(r->ws->idx, &a, true);
6824 for (i = 0; i < spawn_argc; i++)
6830 spawn_select(struct swm_region *r, union arg *args, const char *spawn_name,
6837 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
6841 if (pipe(select_list_pipe) == -1)
6842 err(1, "pipe error");
6843 if (pipe(select_resp_pipe) == -1)
6844 err(1, "pipe error");
6846 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
6847 err(1, "could not disable SIGPIPE");
6848 switch (*pid = fork()) {
6850 err(1, "cannot fork");
6853 if (dup2(select_list_pipe[0], STDIN_FILENO) == -1)
6855 if (dup2(select_resp_pipe[1], STDOUT_FILENO) == -1)
6857 close(select_list_pipe[1]);
6858 close(select_resp_pipe[0]);
6859 spawn(r->ws->idx, &a, false);
6861 default: /* parent */
6862 close(select_list_pipe[0]);
6863 close(select_resp_pipe[1]);
6867 for (i = 0; i < spawn_argc; i++)
6872 /* Argument tokenizer. */
6875 char *arg, *cp, *next;
6876 bool single_quoted = false, double_quoted = false;
6881 /* Eat and move characters until end of argument is found. */
6882 for (arg = next = cp = *sp; *cp != '\0'; ++cp) {
6883 if (!double_quoted && *cp == '\'') {
6884 /* Eat single-quote. */
6885 single_quoted = !single_quoted;
6886 } else if (!single_quoted && *cp == '"') {
6887 /* Eat double-quote. */
6888 double_quoted = !double_quoted;
6889 } else if (!single_quoted && *cp == '\\' && *(cp + 1) == '"') {
6890 /* Eat backslash; copy escaped character to arg. */
6892 } else if (!single_quoted && !double_quoted && *cp == '\\' &&
6893 (*(cp + 1) == '\'' || *(cp + 1) == ' ')) {
6894 /* Eat backslash; move escaped character. */
6896 } else if (!single_quoted && !double_quoted &&
6897 (*cp == ' ' || *cp == '\t')) {
6898 /* Terminate argument. */
6900 /* Point sp to beginning of next argument. */
6904 /* Move regular character. */
6909 /* Terminate argument if end of string. */
6919 spawn_insert(const char *name, const char *args, int flags)
6921 struct spawn_prog *sp;
6922 char *arg, *cp, *ptr;
6924 DNPRINTF(SWM_D_SPAWN, "spawn_insert: %s[%s]\n", name, args);
6926 if (args == NULL || *args == '\0')
6929 if ((sp = calloc(1, sizeof *sp)) == NULL)
6930 err(1, "spawn_insert: calloc");
6931 if ((sp->name = strdup(name)) == NULL)
6932 err(1, "spawn_insert: strdup");
6934 /* Convert the arguments to an argument list. */
6935 if ((ptr = cp = strdup(args)) == NULL)
6936 err(1, "spawn_insert: strdup");
6937 while ((arg = argsep(&ptr)) != NULL) {
6938 /* Null argument; skip it. */
6943 if ((sp->argv = realloc(sp->argv, sp->argc *
6944 sizeof *sp->argv)) == NULL)
6945 err(1, "spawn_insert: realloc");
6946 if ((sp->argv[sp->argc - 1] = strdup(arg)) == NULL)
6947 err(1, "spawn_insert: strdup");
6953 DNPRINTF(SWM_D_SPAWN, "arg %d: [%s]\n", sp->argc, sp->argv[sp->argc-1]);
6954 TAILQ_INSERT_TAIL(&spawns, sp, entry);
6955 DNPRINTF(SWM_D_SPAWN, "spawn_insert: leave\n");
6959 spawn_remove(struct spawn_prog *sp)
6963 DNPRINTF(SWM_D_SPAWN, "spawn_remove: %s\n", sp->name);
6965 TAILQ_REMOVE(&spawns, sp, entry);
6966 for (i = 0; i < sp->argc; i++)
6972 DNPRINTF(SWM_D_SPAWN, "spawn_remove: leave\n");
6976 spawn_find(const char *name)
6978 struct spawn_prog *sp;
6980 TAILQ_FOREACH(sp, &spawns, entry)
6981 if (strcasecmp(sp->name, name) == 0)
6988 setspawn(const char *name, const char *args, int flags)
6990 struct spawn_prog *sp;
6992 DNPRINTF(SWM_D_SPAWN, "setspawn: %s\n", name);
6997 /* Remove any old spawn under the same name. */
6998 if ((sp = spawn_find(name)) != NULL)
7002 spawn_insert(name, args, flags);
7004 warnx("error: setspawn: cannot find program: %s", name);
7006 DNPRINTF(SWM_D_SPAWN, "setspawn: leave\n");
7010 setconfspawn(const char *selector, const char *value, int flags)
7014 if (selector == NULL || strlen(selector) == 0)
7017 args = expand_tilde(value);
7019 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, args);
7021 setspawn(selector, args, flags);
7024 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done.\n");
7029 validate_spawns(void)
7031 struct spawn_prog *sp;
7032 char which[PATH_MAX];
7037 RB_FOREACH(kp, key_tree, &keys) {
7038 if (kp->funcid != KF_SPAWN_CUSTOM)
7042 sp = spawn_find(kp->spawn_name);
7043 if (sp == NULL || sp->flags & SWM_SPAWN_OPTIONAL)
7046 /* verify we have the goods */
7047 snprintf(which, sizeof which, "which %s", sp->argv[0]);
7048 DNPRINTF(SWM_D_CONF, "validate_spawns: which %s\n",
7050 for (i = strlen("which "); i < strlen(which); i++)
7051 if (which[i] == ' ') {
7055 if (system(which) != 0)
7056 add_startup_exception("could not find %s",
7057 &which[strlen("which ")]);
7064 setconfspawn("lock", "xlock", 0);
7066 setconfspawn("term", "xterm", 0);
7067 setconfspawn("spawn_term", "xterm", 0);
7069 setconfspawn("menu", "dmenu_run"
7073 " -nf $bar_font_color"
7075 " -sf $bar_color", 0);
7077 setconfspawn("search", "dmenu"
7082 " -nf $bar_font_color"
7084 " -sf $bar_color", 0);
7086 setconfspawn("name_workspace", "dmenu"
7091 " -nf $bar_font_color"
7093 " -sf $bar_color", 0);
7095 /* These are not verified for existence, even with a binding set. */
7096 setconfspawn("screenshot_all", "screenshot.sh full", SWM_SPAWN_OPTIONAL);
7097 setconfspawn("screenshot_wind", "screenshot.sh window", SWM_SPAWN_OPTIONAL);
7098 setconfspawn("initscr", "initscreen.sh", SWM_SPAWN_OPTIONAL);
7102 #define SWM_MODNAME_SIZE 32
7103 #define SWM_KEY_WS "\n+ \t"
7105 parsekeys(const char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
7107 char *str, *cp, *name;
7110 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
7111 if (mod == NULL || ks == NULL) {
7112 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
7115 if (keystr == NULL || strlen(keystr) == 0) {
7116 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
7120 if ((cp = str = strdup(keystr)) == NULL)
7121 err(1, "parsekeys: strdup");
7123 *ks = XCB_NO_SYMBOL;
7125 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
7126 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
7128 cp += (long)strspn(cp, SWM_KEY_WS);
7129 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
7131 else if (strncasecmp(name, "Mod1", SWM_MODNAME_SIZE) == 0)
7132 *mod |= XCB_MOD_MASK_1;
7133 else if (strncasecmp(name, "Mod2", SWM_MODNAME_SIZE) == 0)
7134 *mod += XCB_MOD_MASK_2;
7135 else if (strncmp(name, "Mod3", SWM_MODNAME_SIZE) == 0)
7136 *mod |= XCB_MOD_MASK_3;
7137 else if (strncmp(name, "Mod4", SWM_MODNAME_SIZE) == 0)
7138 *mod |= XCB_MOD_MASK_4;
7139 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
7140 *mod |= XCB_MOD_MASK_SHIFT;
7141 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
7142 *mod |= XCB_MOD_MASK_CONTROL;
7144 *ks = XStringToKeysym(name);
7145 XConvertCase(*ks, ks, &uks);
7146 if (ks == XCB_NO_SYMBOL) {
7148 "parsekeys: invalid key %s\n",
7157 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
7162 strdupsafe(const char *str)
7167 return (strdup(str));
7171 key_insert(unsigned int mod, KeySym ks, enum keyfuncid kfid,
7172 const char *spawn_name)
7176 DNPRINTF(SWM_D_KEY, "key_insert: enter %s [%s]\n",
7177 keyfuncs[kfid].name, spawn_name);
7179 if ((kp = malloc(sizeof *kp)) == NULL)
7180 err(1, "key_insert: malloc");
7185 kp->spawn_name = strdupsafe(spawn_name);
7186 RB_INSERT(key_tree, &keys, kp);
7188 DNPRINTF(SWM_D_KEY, "key_insert: leave\n");
7192 key_lookup(unsigned int mod, KeySym ks)
7199 return (RB_FIND(key_tree, &keys, &kp));
7203 key_remove(struct key *kp)
7205 DNPRINTF(SWM_D_KEY, "key_remove: %s\n", keyfuncs[kp->funcid].name);
7207 RB_REMOVE(key_tree, &keys, kp);
7208 free(kp->spawn_name);
7211 DNPRINTF(SWM_D_KEY, "key_remove: leave\n");
7215 key_replace(struct key *kp, unsigned int mod, KeySym ks, enum keyfuncid kfid,
7216 const char *spawn_name)
7218 DNPRINTF(SWM_D_KEY, "key_replace: %s [%s]\n", keyfuncs[kp->funcid].name,
7222 key_insert(mod, ks, kfid, spawn_name);
7224 DNPRINTF(SWM_D_KEY, "key_replace: leave\n");
7228 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid,
7229 const char *spawn_name)
7233 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
7234 keyfuncs[kfid].name, spawn_name);
7236 if ((kp = key_lookup(mod, ks)) != NULL) {
7237 if (kfid == KF_INVALID)
7240 key_replace(kp, mod, ks, kfid, spawn_name);
7241 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
7244 if (kfid == KF_INVALID) {
7245 warnx("bind: Key combination already unbound.");
7246 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
7250 key_insert(mod, ks, kfid, spawn_name);
7251 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
7255 setconfbinding(const char *selector, const char *value, int flags)
7257 enum keyfuncid kfid;
7260 struct spawn_prog *sp;
7262 /* suppress unused warning since var is needed */
7265 DNPRINTF(SWM_D_KEY, "setconfbinding: enter selector: [%s], "
7266 "value: [%s]\n", selector, value);
7267 if (selector == NULL || strlen(selector) == 0) {
7268 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
7269 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
7271 setkeybinding(mod, ks, kfid, NULL);
7276 /* search by key function name */
7277 for (kfid = 0; kfid < KF_INVALID; (kfid)++) {
7278 if (strncasecmp(selector, keyfuncs[kfid].name,
7279 SWM_FUNCNAME_LEN) == 0) {
7280 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match "
7281 "keyfunc\n", selector);
7282 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
7283 setkeybinding(mod, ks, kfid, NULL);
7289 /* search by custom spawn name */
7290 if ((sp = spawn_find(selector)) != NULL) {
7291 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match "
7292 "spawn\n", selector);
7293 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
7294 setkeybinding(mod, ks, KF_SPAWN_CUSTOM,
7300 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
7307 #define MODKEY_SHIFT MODKEY | XCB_MOD_MASK_SHIFT
7308 setkeybinding(MODKEY, XK_b, KF_BAR_TOGGLE, NULL);
7309 setkeybinding(MODKEY_SHIFT, XK_b, KF_BAR_TOGGLE_WS,NULL);
7310 setkeybinding(MODKEY, XK_v, KF_BUTTON2, NULL);
7311 setkeybinding(MODKEY, XK_space, KF_CYCLE_LAYOUT,NULL);
7312 setkeybinding(MODKEY_SHIFT, XK_backslash, KF_FLIP_LAYOUT, NULL);
7313 setkeybinding(MODKEY, XK_t, KF_FLOAT_TOGGLE,NULL);
7314 setkeybinding(MODKEY, XK_m, KF_FOCUS_MAIN, NULL);
7315 setkeybinding(MODKEY, XK_j, KF_FOCUS_NEXT, NULL);
7316 setkeybinding(MODKEY, XK_Tab, KF_FOCUS_NEXT, NULL);
7317 setkeybinding(MODKEY, XK_k, KF_FOCUS_PREV, NULL);
7318 setkeybinding(MODKEY_SHIFT, XK_Tab, KF_FOCUS_PREV, NULL);
7319 setkeybinding(MODKEY, XK_u, KF_FOCUS_URGENT,NULL);
7320 setkeybinding(MODKEY, XK_e, KF_MAXIMIZE_TOGGLE,NULL);
7321 setkeybinding(MODKEY_SHIFT, XK_equal, KF_HEIGHT_GROW,NULL);
7322 setkeybinding(MODKEY_SHIFT, XK_minus, KF_HEIGHT_SHRINK,NULL);
7323 setkeybinding(MODKEY, XK_w, KF_ICONIFY, NULL);
7324 setkeybinding(MODKEY, XK_h, KF_MASTER_SHRINK, NULL);
7325 setkeybinding(MODKEY, XK_l, KF_MASTER_GROW, NULL);
7326 setkeybinding(MODKEY, XK_comma, KF_MASTER_ADD, NULL);
7327 setkeybinding(MODKEY, XK_period, KF_MASTER_DEL, NULL);
7328 setkeybinding(MODKEY_SHIFT, XK_bracketright,KF_MOVE_DOWN,NULL);
7329 setkeybinding(MODKEY, XK_bracketleft, KF_MOVE_LEFT,NULL);
7330 setkeybinding(MODKEY, XK_bracketright,KF_MOVE_RIGHT,NULL);
7331 setkeybinding(MODKEY_SHIFT, XK_bracketleft, KF_MOVE_UP, NULL);
7332 setkeybinding(MODKEY_SHIFT, XK_KP_End, KF_MVRG_1, NULL);
7333 setkeybinding(MODKEY_SHIFT, XK_KP_Down, KF_MVRG_2, NULL);
7334 setkeybinding(MODKEY_SHIFT, XK_KP_Next, KF_MVRG_3, NULL);
7335 setkeybinding(MODKEY_SHIFT, XK_KP_Left, KF_MVRG_4, NULL);
7336 setkeybinding(MODKEY_SHIFT, XK_KP_Begin, KF_MVRG_5, NULL);
7337 setkeybinding(MODKEY_SHIFT, XK_KP_Right, KF_MVRG_6, NULL);
7338 setkeybinding(MODKEY_SHIFT, XK_KP_Home, KF_MVRG_7, NULL);
7339 setkeybinding(MODKEY_SHIFT, XK_KP_Up, KF_MVRG_8, NULL);
7340 setkeybinding(MODKEY_SHIFT, XK_KP_Prior, KF_MVRG_9, NULL);
7341 setkeybinding(MODKEY_SHIFT, XK_1, KF_MVWS_1, NULL);
7342 setkeybinding(MODKEY_SHIFT, XK_2, KF_MVWS_2, NULL);
7343 setkeybinding(MODKEY_SHIFT, XK_3, KF_MVWS_3, NULL);
7344 setkeybinding(MODKEY_SHIFT, XK_4, KF_MVWS_4, NULL);
7345 setkeybinding(MODKEY_SHIFT, XK_5, KF_MVWS_5, NULL);
7346 setkeybinding(MODKEY_SHIFT, XK_6, KF_MVWS_6, NULL);
7347 setkeybinding(MODKEY_SHIFT, XK_7, KF_MVWS_7, NULL);
7348 setkeybinding(MODKEY_SHIFT, XK_8, KF_MVWS_8, NULL);
7349 setkeybinding(MODKEY_SHIFT, XK_9, KF_MVWS_9, NULL);
7350 setkeybinding(MODKEY_SHIFT, XK_0, KF_MVWS_10, NULL);
7351 setkeybinding(MODKEY_SHIFT, XK_F1, KF_MVWS_11, NULL);
7352 setkeybinding(MODKEY_SHIFT, XK_F2, KF_MVWS_12, NULL);
7353 setkeybinding(MODKEY_SHIFT, XK_F3, KF_MVWS_13, NULL);
7354 setkeybinding(MODKEY_SHIFT, XK_F4, KF_MVWS_14, NULL);
7355 setkeybinding(MODKEY_SHIFT, XK_F5, KF_MVWS_15, NULL);
7356 setkeybinding(MODKEY_SHIFT, XK_F6, KF_MVWS_16, NULL);
7357 setkeybinding(MODKEY_SHIFT, XK_F7, KF_MVWS_17, NULL);
7358 setkeybinding(MODKEY_SHIFT, XK_F8, KF_MVWS_18, NULL);
7359 setkeybinding(MODKEY_SHIFT, XK_F9, KF_MVWS_19, NULL);
7360 setkeybinding(MODKEY_SHIFT, XK_F10, KF_MVWS_20, NULL);
7361 setkeybinding(MODKEY_SHIFT, XK_F11, KF_MVWS_21, NULL);
7362 setkeybinding(MODKEY_SHIFT, XK_F12, KF_MVWS_22, NULL);
7363 setkeybinding(MODKEY_SHIFT, XK_slash, KF_NAME_WORKSPACE,NULL);
7364 setkeybinding(MODKEY_SHIFT, XK_q, KF_QUIT, NULL);
7365 setkeybinding(MODKEY_SHIFT, XK_r, KF_RAISE_TOGGLE,NULL);
7366 setkeybinding(MODKEY, XK_q, KF_RESTART, NULL);
7367 setkeybinding(MODKEY, XK_KP_End, KF_RG_1, NULL);
7368 setkeybinding(MODKEY, XK_KP_Down, KF_RG_2, NULL);
7369 setkeybinding(MODKEY, XK_KP_Next, KF_RG_3, NULL);
7370 setkeybinding(MODKEY, XK_KP_Left, KF_RG_4, NULL);
7371 setkeybinding(MODKEY, XK_KP_Begin, KF_RG_5, NULL);
7372 setkeybinding(MODKEY, XK_KP_Right, KF_RG_6, NULL);
7373 setkeybinding(MODKEY, XK_KP_Home, KF_RG_7, NULL);
7374 setkeybinding(MODKEY, XK_KP_Up, KF_RG_8, NULL);
7375 setkeybinding(MODKEY, XK_KP_Prior, KF_RG_9, NULL);
7376 setkeybinding(MODKEY_SHIFT, XK_Right, KF_RG_NEXT, NULL);
7377 setkeybinding(MODKEY_SHIFT, XK_Left, KF_RG_PREV, NULL);
7378 setkeybinding(MODKEY, XK_f, KF_SEARCH_WIN, NULL);
7379 setkeybinding(MODKEY, XK_slash, KF_SEARCH_WORKSPACE,NULL);
7380 setkeybinding(MODKEY_SHIFT, XK_i, KF_SPAWN_CUSTOM,"initscr");
7381 setkeybinding(MODKEY_SHIFT, XK_Delete, KF_SPAWN_CUSTOM,"lock");
7382 setkeybinding(MODKEY, XK_p, KF_SPAWN_CUSTOM,"menu");
7383 setkeybinding(MODKEY, XK_s, KF_SPAWN_CUSTOM,"screenshot_all");
7384 setkeybinding(MODKEY_SHIFT, XK_s, KF_SPAWN_CUSTOM,"screenshot_wind");
7385 setkeybinding(MODKEY_SHIFT, XK_Return, KF_SPAWN_CUSTOM,"term");
7386 setkeybinding(MODKEY_SHIFT, XK_comma, KF_STACK_INC, NULL);
7387 setkeybinding(MODKEY_SHIFT, XK_period, KF_STACK_DEC, NULL);
7388 setkeybinding(MODKEY_SHIFT, XK_space, KF_STACK_RESET, NULL);
7389 setkeybinding(MODKEY, XK_Return, KF_SWAP_MAIN, NULL);
7390 setkeybinding(MODKEY_SHIFT, XK_j, KF_SWAP_NEXT, NULL);
7391 setkeybinding(MODKEY_SHIFT, XK_k, KF_SWAP_PREV, NULL);
7392 setkeybinding(MODKEY_SHIFT, XK_w, KF_UNICONIFY, NULL);
7393 setkeybinding(MODKEY_SHIFT, XK_v, KF_VERSION, NULL);
7394 setkeybinding(MODKEY, XK_equal, KF_WIDTH_GROW, NULL);
7395 setkeybinding(MODKEY, XK_minus, KF_WIDTH_SHRINK,NULL);
7396 setkeybinding(MODKEY, XK_x, KF_WIND_DEL, NULL);
7397 setkeybinding(MODKEY_SHIFT, XK_x, KF_WIND_KILL, NULL);
7398 setkeybinding(MODKEY, XK_1, KF_WS_1, NULL);
7399 setkeybinding(MODKEY, XK_2, KF_WS_2, NULL);
7400 setkeybinding(MODKEY, XK_3, KF_WS_3, NULL);
7401 setkeybinding(MODKEY, XK_4, KF_WS_4, NULL);
7402 setkeybinding(MODKEY, XK_5, KF_WS_5, NULL);
7403 setkeybinding(MODKEY, XK_6, KF_WS_6, NULL);
7404 setkeybinding(MODKEY, XK_7, KF_WS_7, NULL);
7405 setkeybinding(MODKEY, XK_8, KF_WS_8, NULL);
7406 setkeybinding(MODKEY, XK_9, KF_WS_9, NULL);
7407 setkeybinding(MODKEY, XK_0, KF_WS_10, NULL);
7408 setkeybinding(MODKEY, XK_F1, KF_WS_11, NULL);
7409 setkeybinding(MODKEY, XK_F2, KF_WS_12, NULL);
7410 setkeybinding(MODKEY, XK_F3, KF_WS_13, NULL);
7411 setkeybinding(MODKEY, XK_F4, KF_WS_14, NULL);
7412 setkeybinding(MODKEY, XK_F5, KF_WS_15, NULL);
7413 setkeybinding(MODKEY, XK_F6, KF_WS_16, NULL);
7414 setkeybinding(MODKEY, XK_F7, KF_WS_17, NULL);
7415 setkeybinding(MODKEY, XK_F8, KF_WS_18, NULL);
7416 setkeybinding(MODKEY, XK_F9, KF_WS_19, NULL);
7417 setkeybinding(MODKEY, XK_F10, KF_WS_20, NULL);
7418 setkeybinding(MODKEY, XK_F11, KF_WS_21, NULL);
7419 setkeybinding(MODKEY, XK_F12, KF_WS_22, NULL);
7420 setkeybinding(MODKEY, XK_Right, KF_WS_NEXT, NULL);
7421 setkeybinding(MODKEY, XK_Left, KF_WS_PREV, NULL);
7422 setkeybinding(MODKEY, XK_Up, KF_WS_NEXT_ALL, NULL);
7423 setkeybinding(MODKEY, XK_Down, KF_WS_PREV_ALL, NULL);
7424 setkeybinding(MODKEY_SHIFT, XK_Up, KF_WS_NEXT_MOVE,NULL);
7425 setkeybinding(MODKEY_SHIFT, XK_Down, KF_WS_PREV_MOVE,NULL);
7426 setkeybinding(MODKEY, XK_a, KF_WS_PRIOR, NULL);
7428 setkeybinding(MODKEY_SHIFT, XK_d, KF_DUMPWINS, NULL);
7438 while (RB_EMPTY(&keys) == 0) {
7439 kp = RB_ROOT(&keys);
7445 setkeymapping(const char *selector, const char *value, int flags)
7447 char *keymapping_file;
7449 /* suppress unused warnings since vars are needed */
7453 DNPRINTF(SWM_D_KEY, "setkeymapping: enter\n");
7455 keymapping_file = expand_tilde(value);
7458 /* load new key bindings; if it fails, revert to default bindings */
7459 if (conf_load(keymapping_file, SWM_CONF_KEYMAPPING)) {
7464 free(keymapping_file);
7466 DNPRINTF(SWM_D_KEY, "setkeymapping: leave\n");
7471 updatenumlockmask(void)
7474 xcb_get_modifier_mapping_reply_t *modmap_r;
7475 xcb_keycode_t *modmap, kc, *keycode;
7479 modmap_r = xcb_get_modifier_mapping_reply(conn,
7480 xcb_get_modifier_mapping(conn),
7483 modmap = xcb_get_modifier_mapping_keycodes(modmap_r);
7484 for (i = 0; i < 8; i++) {
7485 for (j = 0; j < modmap_r->keycodes_per_modifier; j++) {
7486 kc = modmap[i * modmap_r->keycodes_per_modifier
7488 keycode = xcb_key_symbols_get_keycode(syms,
7492 numlockmask = (1 << i);
7499 DNPRINTF(SWM_D_MISC, "updatenumlockmask: %d\n", numlockmask);
7506 int num_screens, k, j;
7507 unsigned int modifiers[4];
7508 xcb_keycode_t *code;
7510 DNPRINTF(SWM_D_MISC, "grabkeys\n");
7511 updatenumlockmask();
7514 modifiers[1] = numlockmask;
7515 modifiers[2] = XCB_MOD_MASK_LOCK;
7516 modifiers[3] = numlockmask | XCB_MOD_MASK_LOCK;
7518 num_screens = get_screen_count();
7519 for (k = 0; k < num_screens; k++) {
7520 if (TAILQ_EMPTY(&screens[k].rl))
7522 xcb_ungrab_key(conn, XCB_GRAB_ANY, screens[k].root,
7524 RB_FOREACH(kp, key_tree, &keys) {
7525 /* Skip unused ws binds. */
7526 if ((int)kp->funcid > KF_WS_1 + workspace_limit - 1 &&
7527 kp->funcid <= KF_WS_22)
7530 /* Skip unused mvws binds. */
7531 if ((int)kp->funcid > KF_MVWS_1 + workspace_limit - 1 &&
7532 kp->funcid <= KF_MVWS_22)
7535 if ((code = xcb_key_symbols_get_keycode(syms,
7537 for (j = 0; j < LENGTH(modifiers); j++)
7538 xcb_grab_key(conn, 1,
7540 kp->mod | modifiers[j],
7541 *code, XCB_GRAB_MODE_SYNC,
7542 XCB_GRAB_MODE_SYNC);
7550 grabbuttons(struct ws_win *win)
7552 unsigned int modifiers[4];
7555 DNPRINTF(SWM_D_MOUSE, "grabbuttons: win %#x\n", win->id);
7556 updatenumlockmask();
7559 modifiers[1] = numlockmask;
7560 modifiers[2] = XCB_MOD_MASK_LOCK;
7561 modifiers[3] = numlockmask | XCB_MOD_MASK_LOCK;
7563 for (i = 0; i < LENGTH(buttons); i++)
7564 if (buttons[i].action == client_click)
7565 for (j = 0; j < LENGTH(modifiers); ++j)
7566 xcb_grab_button(conn, 0, win->id, BUTTONMASK,
7567 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC,
7568 XCB_WINDOW_NONE, XCB_CURSOR_NONE,
7569 buttons[i].button, buttons[i].mask |
7573 const char *quirkname[] = {
7574 "NONE", /* config string for "no value" */
7582 "FOCUSONMAP_SINGLE",
7588 /* SWM_Q_DELIM: retain '|' for back compat for now (2009-08-11) */
7589 #define SWM_Q_DELIM "\n|+ \t"
7591 parsequirks(const char *qstr, uint32_t *quirk, int *ws)
7593 char *str, *cp, *name;
7596 if (quirk == NULL || qstr == NULL)
7599 if ((str = strdup(qstr)) == NULL)
7600 err(1, "parsequirks: strdup");
7604 while ((name = strsep(&cp, SWM_Q_DELIM)) != NULL) {
7606 cp += (long)strspn(cp, SWM_Q_DELIM);
7608 if (sscanf(name, "WS[%d]", ws) == 1) {
7614 for (i = 0; i < LENGTH(quirkname); i++) {
7615 if (strncasecmp(name, quirkname[i],
7616 SWM_QUIRK_LEN) == 0) {
7617 DNPRINTF(SWM_D_QUIRK,
7618 "parsequirks: %s\n", name);
7624 *quirk |= 1 << (i-1);
7628 if (i >= LENGTH(quirkname)) {
7629 DNPRINTF(SWM_D_QUIRK,
7630 "parsequirks: invalid quirk [%s]\n", name);
7641 quirk_insert(const char *class, const char *instance, const char *name,
7642 uint32_t quirk, int ws)
7646 bool failed = false;
7648 DNPRINTF(SWM_D_QUIRK, "quirk_insert: class: %s, instance: %s, name: %s,"
7649 " value: %u, ws: %d\n", class, instance, name, quirk, ws);
7651 if ((qp = malloc(sizeof *qp)) == NULL)
7652 err(1, "quirk_insert: malloc");
7654 if ((qp->class = strdup(class)) == NULL)
7655 err(1, "quirk_insert: strdup");
7656 if ((qp->instance = strdup(instance)) == NULL)
7657 err(1, "quirk_insert: strdup");
7658 if ((qp->name = strdup(name)) == NULL)
7659 err(1, "quirk_insert: strdup");
7661 if (asprintf(&str, "^%s$", class) == -1)
7662 err(1, "quirk_insert: asprintf");
7663 if (regcomp(&qp->regex_class, str, REG_EXTENDED | REG_NOSUB)) {
7664 add_startup_exception("regex failed to compile quirk 'class' "
7665 "field: %s", class);
7668 DNPRINTF(SWM_D_QUIRK, "quirk_insert: compiled: %s\n", str);
7671 if (asprintf(&str, "^%s$", instance) == -1)
7672 err(1, "quirk_insert: asprintf");
7673 if (regcomp(&qp->regex_instance, str, REG_EXTENDED | REG_NOSUB)) {
7674 add_startup_exception("regex failed to compile quirk 'instance'"
7675 " field: %s", instance);
7678 DNPRINTF(SWM_D_QUIRK, "quirk_insert: compiled: %s\n", str);
7681 if (asprintf(&str, "^%s$", name) == -1)
7682 err(1, "quirk_insert: asprintf");
7683 if (regcomp(&qp->regex_name, str, REG_EXTENDED | REG_NOSUB)) {
7684 add_startup_exception("regex failed to compile quirk 'name' "
7688 DNPRINTF(SWM_D_QUIRK, "quirk_insert: compiled: %s\n", str);
7692 DNPRINTF(SWM_D_QUIRK, "quirk_insert: regex error; skipping.\n");
7697 TAILQ_INSERT_TAIL(&quirks, qp, entry);
7699 DNPRINTF(SWM_D_QUIRK, "quirk_insert: leave\n");
7703 quirk_remove(struct quirk *qp)
7705 DNPRINTF(SWM_D_QUIRK, "quirk_remove: %s:%s [%u]\n", qp->class,
7706 qp->name, qp->quirk);
7708 TAILQ_REMOVE(&quirks, qp, entry);
7711 DNPRINTF(SWM_D_QUIRK, "quirk_remove: leave\n");
7715 quirk_free(struct quirk *qp)
7717 regfree(&qp->regex_class);
7718 regfree(&qp->regex_instance);
7719 regfree(&qp->regex_name);
7727 quirk_replace(struct quirk *qp, const char *class, const char *instance,
7728 const char *name, uint32_t quirk, int ws)
7730 DNPRINTF(SWM_D_QUIRK, "quirk_replace: %s:%s:%s [%u], ws: %d\n", qp->class,
7731 qp->instance, qp->name, qp->quirk, qp->ws);
7734 quirk_insert(class, instance, name, quirk, ws);
7736 DNPRINTF(SWM_D_QUIRK, "quirk_replace: leave\n");
7740 setquirk(const char *class, const char *instance, const char *name,
7741 uint32_t quirk, int ws)
7745 DNPRINTF(SWM_D_QUIRK, "setquirk: enter %s:%s:%s [%u], ws: %d\n", class,
7746 instance, name, quirk, ws);
7748 /* Remove/replace existing quirk. */
7749 TAILQ_FOREACH(qp, &quirks, entry) {
7750 if (strcmp(qp->class, class) == 0 &&
7751 strcmp(qp->instance, instance) == 0 &&
7752 strcmp(qp->name, name) == 0) {
7753 if (quirk == 0 && ws == -1)
7756 quirk_replace(qp, class, instance, name, quirk,
7758 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
7763 /* Only insert if quirk is not NONE or forced ws is set. */
7764 if (quirk || ws != -1)
7765 quirk_insert(class, instance, name, quirk, ws);
7767 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
7770 /* Eat '\' in str used to escape square brackets and colon. */
7772 unescape_selector(char *str)
7776 for (cp = str; *str != '\0'; ++str, ++cp) {
7777 if (*str == '\\' && (*(str + 1) == ':' || *(str + 1) == ']' ||
7787 setconfquirk(const char *selector, const char *value, int flags)
7789 char *str, *cp, *class;
7790 char *instance = NULL, *name = NULL;
7791 int retval, count = 0, ws = -1;
7794 /* suppress unused warning since var is needed */
7797 if (selector == NULL || strlen(selector) == 0)
7800 if ((str = strdup(selector)) == NULL)
7801 err(1, "setconfquirk: strdup");
7803 /* Find non-escaped colon. */
7810 for (++cp; *cp != '\0'; ++cp) {
7811 if (*cp == ':' && *(cp - 1) != '\\') {
7817 unescape_selector(class);
7819 instance = class + strlen(class) + 1;
7820 unescape_selector(instance);
7826 name = instance + strlen(instance) + 1;
7827 unescape_selector(name);
7832 DNPRINTF(SWM_D_CONF, "setconfquirk: class: %s, instance: %s, "
7833 "name: %s\n", class, instance, name);
7835 if ((retval = parsequirks(value, &qrks, &ws)) == 0)
7836 setquirk(class, instance, name, qrks, ws);
7845 setquirk("MPlayer", "xv", ".*",
7846 SWM_Q_FLOAT | SWM_Q_FULLSCREEN | SWM_Q_FOCUSPREV, -1);
7847 setquirk("OpenOffice.org 3.2", "VCLSalFrame", ".*",
7849 setquirk("Firefox-bin", "firefox-bin", ".*",
7851 setquirk("Firefox", "Dialog", ".*",
7853 setquirk("Gimp", "gimp", ".*",
7854 SWM_Q_FLOAT | SWM_Q_ANYWHERE, -1);
7855 setquirk("XTerm", "xterm", ".*",
7856 SWM_Q_XTERM_FONTADJ, -1);
7857 setquirk("xine", "Xine Window", ".*",
7858 SWM_Q_FLOAT | SWM_Q_ANYWHERE, -1);
7859 setquirk("Xitk", "Xitk Combo", ".*",
7860 SWM_Q_FLOAT | SWM_Q_ANYWHERE, -1);
7861 setquirk("xine", "xine Panel", ".*",
7862 SWM_Q_FLOAT | SWM_Q_ANYWHERE, -1);
7863 setquirk("Xitk", "Xine Window", ".*",
7864 SWM_Q_FLOAT | SWM_Q_ANYWHERE, -1);
7865 setquirk("xine", "xine Video Fullscreen Window", ".*",
7866 SWM_Q_FULLSCREEN | SWM_Q_FLOAT, -1);
7867 setquirk("pcb", "pcb", ".*",
7869 setquirk("SDL_App", "SDL_App", ".*",
7870 SWM_Q_FLOAT | SWM_Q_FULLSCREEN, -1);
7873 /* conf file stuff */
7874 #define SWM_CONF_FILE "spectrwm.conf"
7875 #define SWM_CONF_FILE_OLD "scrotwm.conf"
7879 SWM_S_BAR_AT_BOTTOM,
7880 SWM_S_BAR_BORDER_WIDTH,
7883 SWM_S_BAR_ENABLED_WS,
7888 SWM_S_BOUNDARY_WIDTH,
7889 SWM_S_CLOCK_ENABLED,
7892 SWM_S_CYCLE_VISIBLE,
7894 SWM_S_DISABLE_BORDER,
7896 SWM_S_FOCUS_CLOSE_WRAP,
7897 SWM_S_FOCUS_DEFAULT,
7899 SWM_S_ICONIC_ENABLED,
7900 SWM_S_JAVA_WORKAROUND,
7901 SWM_S_REGION_PADDING,
7906 SWM_S_STACK_ENABLED,
7909 SWM_S_URGENT_ENABLED,
7910 SWM_S_VERBOSE_LAYOUT,
7912 SWM_S_WINDOW_CLASS_ENABLED,
7913 SWM_S_WINDOW_INSTANCE_ENABLED,
7914 SWM_S_WINDOW_NAME_ENABLED,
7915 SWM_S_WORKSPACE_LIMIT,
7916 SWM_S_WORKSPACE_NAME,
7920 setconfvalue(const char *selector, const char *value, int flags)
7922 struct workspace *ws;
7923 int i, ws_id, num_screens;
7924 char *b, *str, s[1024];
7927 case SWM_S_BAR_ACTION:
7929 if ((bar_argv[0] = expand_tilde(value)) == NULL)
7930 err(1, "setconfvalue: bar_action");
7932 case SWM_S_BAR_AT_BOTTOM:
7933 bar_at_bottom = (atoi(value) != 0);
7935 case SWM_S_BAR_BORDER_WIDTH:
7936 bar_border_width = atoi(value);
7937 if (bar_border_width < 0)
7938 bar_border_width = 0;
7940 case SWM_S_BAR_DELAY:
7941 /* No longer needed; leave to not break old conf files. */
7943 case SWM_S_BAR_ENABLED:
7944 bar_enabled = (atoi(value) != 0);
7946 case SWM_S_BAR_ENABLED_WS:
7947 ws_id = atoi(selector) - 1;
7948 if (ws_id < 0 || ws_id >= workspace_limit)
7949 errx(1, "setconfvalue: bar_enabled_ws: invalid "
7950 "workspace %d.", ws_id + 1);
7952 num_screens = get_screen_count();
7953 for (i = 0; i < num_screens; i++) {
7954 ws = (struct workspace *)&screens[i].ws;
7955 ws[ws_id].bar_enabled = (atoi(value) != 0);
7958 case SWM_S_BAR_FONT:
7960 if (asprintf(&bar_fonts, "%s,%s", value, bar_fonts) == -1)
7961 err(1, "setconfvalue: asprintf: failed to allocate "
7962 "memory for bar_fonts.");
7965 /* If already in xft mode, then we are done. */
7966 if (!bar_font_legacy)
7969 if ((str = strdup(value)) == NULL)
7970 err(1, "setconfvalue: strdup");
7972 /* If there are any non-XLFD entries, switch to Xft mode. */
7973 while ((b = strsep(&str, ",")) != NULL) {
7977 bar_font_legacy = false;
7984 case SWM_S_BAR_FORMAT:
7986 if ((bar_format = strdup(value)) == NULL)
7987 err(1, "setconfvalue: bar_format");
7989 case SWM_S_BAR_JUSTIFY:
7990 if (strcmp(value, "left") == 0)
7991 bar_justify = SWM_BAR_JUSTIFY_LEFT;
7992 else if (strcmp(value, "center") == 0)
7993 bar_justify = SWM_BAR_JUSTIFY_CENTER;
7994 else if (strcmp(value, "right") == 0)
7995 bar_justify = SWM_BAR_JUSTIFY_RIGHT;
7997 errx(1, "invalid bar_justify");
7999 case SWM_S_BORDER_WIDTH:
8000 border_width = atoi(value);
8001 if (border_width < 0)
8004 case SWM_S_BOUNDARY_WIDTH:
8005 boundary_width = atoi(value);
8006 if (boundary_width < 0)
8009 case SWM_S_CLOCK_ENABLED:
8010 clock_enabled = (atoi(value) != 0);
8012 case SWM_S_CLOCK_FORMAT:
8013 #ifndef SWM_DENY_CLOCK_FORMAT
8015 if ((clock_format = strdup(value)) == NULL)
8016 err(1, "setconfvalue: clock_format");
8019 case SWM_S_CYCLE_EMPTY:
8020 cycle_empty = (atoi(value) != 0);
8022 case SWM_S_CYCLE_VISIBLE:
8023 cycle_visible = (atoi(value) != 0);
8025 case SWM_S_DIALOG_RATIO:
8026 dialog_ratio = atof(value);
8027 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
8030 case SWM_S_DISABLE_BORDER:
8031 disable_border = (atoi(value) != 0);
8033 case SWM_S_FOCUS_CLOSE:
8034 if (strcmp(value, "first") == 0)
8035 focus_close = SWM_STACK_BOTTOM;
8036 else if (strcmp(value, "last") == 0)
8037 focus_close = SWM_STACK_TOP;
8038 else if (strcmp(value, "next") == 0)
8039 focus_close = SWM_STACK_ABOVE;
8040 else if (strcmp(value, "previous") == 0)
8041 focus_close = SWM_STACK_BELOW;
8043 errx(1, "focus_close");
8045 case SWM_S_FOCUS_CLOSE_WRAP:
8046 focus_close_wrap = (atoi(value) != 0);
8048 case SWM_S_FOCUS_DEFAULT:
8049 if (strcmp(value, "last") == 0)
8050 focus_default = SWM_STACK_TOP;
8051 else if (strcmp(value, "first") == 0)
8052 focus_default = SWM_STACK_BOTTOM;
8054 errx(1, "focus_default");
8056 case SWM_S_FOCUS_MODE:
8057 if (strcmp(value, "default") == 0)
8058 focus_mode = SWM_FOCUS_DEFAULT;
8059 else if (strcmp(value, "follow") == 0 ||
8060 strcmp(value, "follow_cursor") == 0)
8061 focus_mode = SWM_FOCUS_FOLLOW;
8062 else if (strcmp(value, "manual") == 0)
8063 focus_mode = SWM_FOCUS_MANUAL;
8065 errx(1, "focus_mode");
8067 case SWM_S_ICONIC_ENABLED:
8068 iconic_enabled = (atoi(value) != 0);
8070 case SWM_S_JAVA_WORKAROUND:
8071 java_workaround = (atoi(value) != 0);
8073 case SWM_S_REGION_PADDING:
8074 region_padding = atoi(value);
8075 if (region_padding < 0)
8078 case SWM_S_SPAWN_ORDER:
8079 if (strcmp(value, "first") == 0)
8080 spawn_position = SWM_STACK_BOTTOM;
8081 else if (strcmp(value, "last") == 0)
8082 spawn_position = SWM_STACK_TOP;
8083 else if (strcmp(value, "next") == 0)
8084 spawn_position = SWM_STACK_ABOVE;
8085 else if (strcmp(value, "previous") == 0)
8086 spawn_position = SWM_STACK_BELOW;
8088 errx(1, "spawn_position");
8090 case SWM_S_SPAWN_TERM:
8091 setconfspawn("term", value, 0);
8092 setconfspawn("spawn_term", value, 0);
8095 /* No longer needed; leave to not break old conf files. */
8097 case SWM_S_SS_ENABLED:
8098 /* No longer needed; leave to not break old conf files. */
8100 case SWM_S_STACK_ENABLED:
8101 stack_enabled = (atoi(value) != 0);
8103 case SWM_S_TERM_WIDTH:
8104 term_width = atoi(value);
8108 case SWM_S_TILE_GAP:
8109 tile_gap = atoi(value);
8111 case SWM_S_URGENT_ENABLED:
8112 urgent_enabled = (atoi(value) != 0);
8114 case SWM_S_VERBOSE_LAYOUT:
8115 verbose_layout = (atoi(value) != 0);
8116 for (i = 0; layouts[i].l_stack != NULL; i++) {
8118 layouts[i].l_string = fancy_stacker;
8120 layouts[i].l_string = plain_stacker;
8123 case SWM_S_WARP_POINTER:
8124 warp_pointer = (atoi(value) != 0);
8126 case SWM_S_WINDOW_CLASS_ENABLED:
8127 window_class_enabled = (atoi(value) != 0);
8129 case SWM_S_WINDOW_INSTANCE_ENABLED:
8130 window_instance_enabled = (atoi(value) != 0);
8132 case SWM_S_WINDOW_NAME_ENABLED:
8133 window_name_enabled = (atoi(value) != 0);
8135 case SWM_S_WORKSPACE_LIMIT:
8136 workspace_limit = atoi(value);
8137 if (workspace_limit > SWM_WS_MAX)
8138 workspace_limit = SWM_WS_MAX;
8139 else if (workspace_limit < 1)
8140 workspace_limit = 1;
8142 ewmh_update_desktops();
8144 case SWM_S_WORKSPACE_NAME:
8145 if (getenv("SWM_STARTED") != NULL)
8149 if (sscanf(value, "ws[%d]:%1023c", &ws_id, s) != 2)
8150 errx(1, "invalid entry, should be 'ws[<idx>]:name'");
8152 if (ws_id < 0 || ws_id >= workspace_limit)
8153 errx(1, "setconfvalue: workspace_name: invalid "
8154 "workspace %d.", ws_id + 1);
8156 num_screens = get_screen_count();
8157 for (i = 0; i < num_screens; ++i) {
8158 ws = (struct workspace *)&screens[i].ws;
8160 if (strlen(s) > 0) {
8161 free(ws[ws_id].name);
8162 if ((ws[ws_id].name = strdup(s)) == NULL)
8163 err(1, "setconfvalue: workspace_name.");
8165 ewmh_update_desktop_names();
8166 ewmh_get_desktop_names();
8177 setconfmodkey(const char *selector, const char *value, int flags)
8179 /* suppress unused warnings since vars are needed */
8183 if (strncasecmp(value, "Mod1", strlen("Mod1")) == 0)
8184 update_modkey(XCB_MOD_MASK_1);
8185 else if (strncasecmp(value, "Mod2", strlen("Mod2")) == 0)
8186 update_modkey(XCB_MOD_MASK_2);
8187 else if (strncasecmp(value, "Mod3", strlen("Mod3")) == 0)
8188 update_modkey(XCB_MOD_MASK_3);
8189 else if (strncasecmp(value, "Mod4", strlen("Mod4")) == 0)
8190 update_modkey(XCB_MOD_MASK_4);
8197 setconfcolor(const char *selector, const char *value, int flags)
8199 int first, last, i = 0, num_screens;
8201 num_screens = get_screen_count();
8203 /* conf screen indices begin at 1; treat vals <= 0 as 'all screens.' */
8204 if (selector == NULL || strlen(selector) == 0 ||
8205 (last = atoi(selector) - 1) < 0) {
8207 last = num_screens - 1;
8212 if (last >= num_screens) {
8213 add_startup_exception("invalid screen index: %d out of bounds "
8214 "(maximum %d)", last + 1, num_screens);
8218 for (i = first; i <= last; ++i) {
8219 setscreencolor(value, i, flags);
8222 * When setting focus/unfocus colors, we need to also
8223 * set maximize colors to match if they haven't been customized.
8225 if (flags == SWM_S_COLOR_FOCUS &&
8226 !screens[i].c[SWM_S_COLOR_FOCUS_MAXIMIZED].manual)
8227 setscreencolor(value, i, SWM_S_COLOR_FOCUS_MAXIMIZED);
8228 else if (flags == SWM_S_COLOR_UNFOCUS &&
8229 !screens[i].c[SWM_S_COLOR_UNFOCUS_MAXIMIZED].manual)
8230 setscreencolor(value, i, SWM_S_COLOR_UNFOCUS_MAXIMIZED);
8232 screens[i].c[flags].manual = 1;
8239 setconfregion(const char *selector, const char *value, int flags)
8241 /* suppress unused warnings since vars are needed */
8245 custom_region(value);
8250 setautorun(const char *selector, const char *value, int flags)
8260 /* suppress unused warnings since vars are needed */
8264 if (getenv("SWM_STARTED"))
8268 if (sscanf(value, "ws[%d]:%1023c", &ws_id, s) != 2)
8269 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
8271 if (ws_id < 0 || ws_id >= workspace_limit)
8272 errx(1, "autorun: invalid workspace %d", ws_id + 1);
8274 sp = expand_tilde((char *)&s);
8277 * This is a little intricate
8279 * If the pid already exists we simply reuse it because it means it was
8280 * used before AND not claimed by manage_window. We get away with
8281 * altering it in the parent after INSERT because this can not be a race
8284 while ((ap = strsep(&sp, " \t")) != NULL) {
8287 DNPRINTF(SWM_D_SPAWN, "setautorun: arg [%s]\n", ap);
8289 if ((a.argv = realloc(a.argv, argc * sizeof(char *))) == NULL)
8290 err(1, "setautorun: realloc");
8291 a.argv[argc - 1] = ap;
8295 if ((a.argv = realloc(a.argv, (argc + 1) * sizeof(char *))) == NULL)
8296 err(1, "setautorun: realloc");
8297 a.argv[argc] = NULL;
8299 if ((pid = fork()) == 0) {
8300 spawn(ws_id, &a, true);
8309 p = calloc(1, sizeof *p);
8312 TAILQ_INSERT_TAIL(&pidlist, p, entry);
8322 setlayout(const char *selector, const char *value, int flags)
8324 struct workspace *ws;
8325 int ws_id, i, x, mg, ma, si, ar;
8326 int st = SWM_V_STACK, num_screens;
8330 /* suppress unused warnings since vars are needed */
8334 if (getenv("SWM_STARTED"))
8338 if (sscanf(value, "ws[%d]:%d:%d:%d:%d:%1023c",
8339 &ws_id, &mg, &ma, &si, &ar, s) != 6)
8340 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
8341 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
8344 if (ws_id < 0 || ws_id >= workspace_limit)
8345 errx(1, "layout: invalid workspace %d", ws_id + 1);
8347 if (strcasecmp(s, "vertical") == 0)
8349 else if (strcasecmp(s, "vertical_flip") == 0) {
8352 } else if (strcasecmp(s, "horizontal") == 0)
8354 else if (strcasecmp(s, "horizontal_flip") == 0) {
8357 } else if (strcasecmp(s, "fullscreen") == 0)
8360 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
8361 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
8364 num_screens = get_screen_count();
8365 for (i = 0; i < num_screens; i++) {
8366 ws = (struct workspace *)&screens[i].ws;
8367 ws[ws_id].cur_layout = &layouts[st];
8369 ws[ws_id].always_raise = (ar != 0);
8370 if (st == SWM_MAX_STACK)
8374 for (x = 0; x < abs(mg); x++) {
8375 ws[ws_id].cur_layout->l_config(&ws[ws_id],
8376 mg >= 0 ? SWM_ARG_ID_MASTERGROW :
8377 SWM_ARG_ID_MASTERSHRINK);
8380 for (x = 0; x < abs(ma); x++) {
8381 ws[ws_id].cur_layout->l_config(&ws[ws_id],
8382 ma >= 0 ? SWM_ARG_ID_MASTERADD :
8383 SWM_ARG_ID_MASTERDEL);
8386 for (x = 0; x < abs(si); x++) {
8387 ws[ws_id].cur_layout->l_config(&ws[ws_id],
8388 si >= 0 ? SWM_ARG_ID_STACKINC :
8389 SWM_ARG_ID_STACKDEC);
8393 ws[ws_id].cur_layout->l_config(&ws[ws_id],
8394 SWM_ARG_ID_FLIPLAYOUT);
8401 /* config options */
8402 struct config_option {
8404 int (*func)(const char*, const char*, int);
8407 struct config_option configopt[] = {
8408 { "autorun", setautorun, 0 },
8409 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
8410 { "bar_at_bottom", setconfvalue, SWM_S_BAR_AT_BOTTOM },
8411 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
8412 { "bar_border_unfocus", setconfcolor, SWM_S_COLOR_BAR_BORDER_UNFOCUS },
8413 { "bar_border_width", setconfvalue, SWM_S_BAR_BORDER_WIDTH },
8414 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
8415 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
8416 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
8417 { "bar_enabled_ws", setconfvalue, SWM_S_BAR_ENABLED_WS },
8418 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
8419 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
8420 { "bar_format", setconfvalue, SWM_S_BAR_FORMAT },
8421 { "bar_justify", setconfvalue, SWM_S_BAR_JUSTIFY },
8422 { "bind", setconfbinding, 0 },
8423 { "border_width", setconfvalue, SWM_S_BORDER_WIDTH },
8424 { "boundary_width", setconfvalue, SWM_S_BOUNDARY_WIDTH },
8425 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
8426 { "clock_format", setconfvalue, SWM_S_CLOCK_FORMAT },
8427 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
8428 { "color_focus_maximized", setconfcolor, SWM_S_COLOR_FOCUS_MAXIMIZED },
8429 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
8430 { "color_unfocus_maximized", setconfcolor, SWM_S_COLOR_UNFOCUS_MAXIMIZED },
8431 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
8432 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
8433 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
8434 { "disable_border", setconfvalue, SWM_S_DISABLE_BORDER },
8435 { "focus_close", setconfvalue, SWM_S_FOCUS_CLOSE },
8436 { "focus_close_wrap", setconfvalue, SWM_S_FOCUS_CLOSE_WRAP },
8437 { "focus_default", setconfvalue, SWM_S_FOCUS_DEFAULT },
8438 { "focus_mode", setconfvalue, SWM_S_FOCUS_MODE },
8439 { "iconic_enabled", setconfvalue, SWM_S_ICONIC_ENABLED },
8440 { "java_workaround", setconfvalue, SWM_S_JAVA_WORKAROUND },
8441 { "keyboard_mapping", setkeymapping, 0 },
8442 { "layout", setlayout, 0 },
8443 { "modkey", setconfmodkey, 0 },
8444 { "program", setconfspawn, 0 },
8445 { "quirk", setconfquirk, 0 },
8446 { "region", setconfregion, 0 },
8447 { "region_padding", setconfvalue, SWM_S_REGION_PADDING },
8448 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
8449 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
8450 { "spawn_position", setconfvalue, SWM_S_SPAWN_ORDER },
8451 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
8452 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
8453 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
8454 { "tile_gap", setconfvalue, SWM_S_TILE_GAP },
8455 { "title_class_enabled", setconfvalue, SWM_S_WINDOW_CLASS_ENABLED }, /* For backwards compat. */
8456 { "title_name_enabled", setconfvalue, SWM_S_WINDOW_INSTANCE_ENABLED }, /* For backwards compat. */
8457 { "urgent_enabled", setconfvalue, SWM_S_URGENT_ENABLED },
8458 { "verbose_layout", setconfvalue, SWM_S_VERBOSE_LAYOUT },
8459 { "warp_pointer", setconfvalue, SWM_S_WARP_POINTER },
8460 { "window_class_enabled", setconfvalue, SWM_S_WINDOW_CLASS_ENABLED },
8461 { "window_instance_enabled", setconfvalue, SWM_S_WINDOW_INSTANCE_ENABLED },
8462 { "window_name_enabled", setconfvalue, SWM_S_WINDOW_NAME_ENABLED },
8463 { "workspace_limit", setconfvalue, SWM_S_WORKSPACE_LIMIT },
8464 { "name", setconfvalue, SWM_S_WORKSPACE_NAME },
8468 _add_startup_exception(const char *fmt, va_list ap)
8470 if (vasprintf(&startup_exception, fmt, ap) == -1)
8471 warn("%s: asprintf", __func__);
8475 add_startup_exception(const char *fmt, ...)
8481 if (startup_exception)
8484 /* force bar to be enabled due to exception */
8488 _add_startup_exception(fmt, ap);
8493 conf_load(const char *filename, int keymapping)
8496 char *line = NULL, *cp, *ce, *optsub, *optval = NULL;
8497 size_t linelen, lineno = 0;
8498 int wordlen, i, optidx, count;
8499 struct config_option *opt = NULL;
8501 DNPRINTF(SWM_D_CONF, "conf_load: begin\n");
8503 if (filename == NULL) {
8504 warnx("conf_load: no filename");
8508 DNPRINTF(SWM_D_CONF, "conf_load: open %s\n", filename);
8510 if ((config = fopen(filename, "r")) == NULL) {
8511 warn("conf_load: fopen: %s", filename);
8515 while (!feof(config)) {
8519 if ((line = fparseln(config, &linelen, &lineno, NULL,
8520 FPARSELN_UNESCCOMM | FPARSELN_UNESCCONT)) == NULL) {
8522 err(1, "%s", filename);
8527 cp += strspn(cp, " \t\n"); /* eat whitespace */
8528 if (cp[0] == '\0') {
8532 /* get config option */
8533 wordlen = strcspn(cp, "=[ \t\n");
8535 add_startup_exception("%s: line %zd: no option found",
8540 for (i = 0; i < LENGTH(configopt); i++) {
8541 opt = &configopt[i];
8542 if (strncasecmp(cp, opt->optname, wordlen) == 0 &&
8543 (int)strlen(opt->optname) == wordlen) {
8549 add_startup_exception("%s: line %zd: unknown option "
8550 "%.*s", filename, lineno, wordlen, cp);
8553 if (keymapping && opt && strcmp(opt->optname, "bind")) {
8554 add_startup_exception("%s: line %zd: invalid option "
8555 "%.*s", filename, lineno, wordlen, cp);
8559 cp += strspn(cp, " \t\n"); /* eat whitespace */
8561 /* get [selector] if any */
8566 /* Get length of selector. */
8567 for (ce = ++cp; *ce != '\0'; ++ce) {
8568 /* Find matching (not escaped) bracket. */
8569 if (*ce == ']' && *(ce - 1) != '\\') {
8576 add_startup_exception("%s: line %zd: syntax "
8577 "error: unterminated selector", filename,
8582 /* ce points at ']'; terminate optsub. */
8587 cp += strspn(cp, "= \t\n"); /* eat trailing */
8590 if (strlen(optval) == 0) {
8591 add_startup_exception("%s: line %zd: must supply value "
8592 "to %s", filename, lineno,
8593 configopt[optidx].optname);
8596 /* call function to deal with it all */
8597 if (configopt[optidx].func(optsub, optval,
8598 configopt[optidx].funcflags) != 0) {
8599 add_startup_exception("%s: line %zd: invalid data for "
8600 "%s", filename, lineno, configopt[optidx].optname);
8608 DNPRINTF(SWM_D_CONF, "conf_load: end\n");
8614 set_child_transient(struct ws_win *win, xcb_window_t *trans)
8616 struct ws_win *parent, *w;
8617 struct swm_region *r;
8618 struct workspace *ws;
8619 xcb_icccm_wm_hints_t wmh;
8621 parent = find_window(win->transient);
8623 parent->focus_child = win;
8625 DNPRINTF(SWM_D_MISC, "set_child_transient: parent doesn't exist"
8626 " for %#x trans %#x\n", win->id, win->transient);
8628 r = root_to_region(win->s->root, SWM_CK_ALL);
8630 /* parent doen't exist in our window list */
8631 TAILQ_FOREACH(w, &ws->winlist, entry) {
8632 if (xcb_icccm_get_wm_hints_reply(conn,
8633 xcb_icccm_get_wm_hints(conn, w->id),
8635 warnx("can't get hints for %#x", w->id);
8639 if (win->hints.window_group != wmh.window_group)
8642 w->focus_child = win;
8643 win->transient = w->id;
8645 DNPRINTF(SWM_D_MISC, "set_child_transient: adjusting "
8646 "transient to %#x\n", win->transient);
8653 window_get_pid(xcb_window_t win)
8658 xcb_get_property_cookie_t pc;
8659 xcb_get_property_reply_t *pr;
8661 apid = get_atom_from_string("_NET_WM_PID");
8662 if (apid == XCB_ATOM_NONE)
8665 pc = xcb_get_property(conn, 0, win, apid, XCB_ATOM_CARDINAL, 0, 1);
8666 pr = xcb_get_property_reply(conn, pc, NULL);
8669 if (pr->type != XCB_ATOM_CARDINAL) {
8674 if (pr->type == apid && pr->format == 32)
8675 ret = *((pid_t *)xcb_get_property_value(pr));
8681 apid = get_atom_from_string("_SWM_PID");
8682 pc = xcb_get_property(conn, 0, win, apid, XCB_ATOM_STRING,
8684 pr = xcb_get_property_reply(conn, pc, NULL);
8687 if (pr->type != apid) {
8692 ret = (pid_t)strtonum(xcb_get_property_value(pr), 0, INT_MAX, &errstr);
8699 get_swm_ws(xcb_window_t id)
8705 xcb_get_property_reply_t *gpr;
8707 gpr = xcb_get_property_reply(conn,
8708 xcb_get_property(conn, 0, id, a_swm_ws,
8709 XCB_ATOM_STRING, 0, SWM_PROPLEN),
8714 proplen = xcb_get_property_value_length(gpr);
8716 prop = malloc(proplen + 1);
8719 xcb_get_property_value(gpr),
8721 prop[proplen] = '\0';
8728 DNPRINTF(SWM_D_PROP, "get_swm_ws: _SWM_WS: %s\n", prop);
8729 ws_idx = (int)strtonum(prop, 0, workspace_limit - 1, &errstr);
8731 DNPRINTF(SWM_D_PROP, "get_swm_ws: win #%s: %s",
8741 get_ws_idx(struct ws_win *win)
8743 xcb_get_property_reply_t *gpr;
8749 gpr = xcb_get_property_reply(conn,
8750 xcb_get_property(conn, 0, win->id, ewmh[_NET_WM_DESKTOP].atom,
8751 XCB_ATOM_CARDINAL, 0, 1),
8754 if (gpr->type == XCB_ATOM_CARDINAL && gpr->format == 32)
8755 ws_idx = *((int *)xcb_get_property_value(gpr));
8759 if (ws_idx == -1 && !(win->quirks & SWM_Q_IGNORESPAWNWS))
8760 ws_idx = get_swm_ws(win->id);
8762 if (ws_idx > workspace_limit - 1 || ws_idx < -1)
8765 DNPRINTF(SWM_D_PROP, "get_ws_idx: win %#x, ws_idx: %d\n", win->id,
8772 manage_window(xcb_window_t id, int spawn_pos, bool mapped)
8774 struct ws_win *win, *ww;
8775 struct swm_region *r;
8778 xcb_get_geometry_reply_t *gr;
8779 xcb_window_t trans = XCB_WINDOW_NONE;
8780 uint32_t i, wa[2], new_flags;
8781 int ws_idx, force_ws = -1;
8782 char *class, *instance, *name;
8784 if ((win = find_window(id)) != NULL) {
8785 DNPRINTF(SWM_D_MISC, "manage_window: win %#x already "
8786 "managed; skipping.)\n", id);
8787 return (win); /* Already managed. */
8790 /* See if window is on the unmanaged list. */
8791 if ((win = find_unmanaged_window(id)) != NULL) {
8792 DNPRINTF(SWM_D_MISC, "manage_window: win %#x found on "
8793 "unmanaged list.\n", id);
8794 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
8797 set_child_transient(win, &trans);
8801 DNPRINTF(SWM_D_MISC, "manage_window: win %#x is new.\n", id);
8804 /* Try to get initial window geometry. */
8805 gr = xcb_get_geometry_reply(conn, xcb_get_geometry(conn, id), NULL);
8807 DNPRINTF(SWM_D_MISC, "manage_window: get geometry failed.\n");
8811 /* Create and initialize ws_win object. */
8812 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
8813 err(1, "manage_window: calloc: failed to allocate memory for "
8818 /* Figureout which region the window belongs to. */
8819 r = root_to_region(gr->root, SWM_CK_ALL);
8821 /* Ignore window border if there is one. */
8822 WIDTH(win) = gr->width;
8823 HEIGHT(win) = gr->height;
8824 X(win) = gr->x + gr->border_width - border_width;
8825 Y(win) = gr->y + gr->border_width - border_width;
8826 win->bordered = true;
8827 win->mapped = mapped;
8828 win->s = r->s; /* this never changes */
8832 /* Select which X events to monitor and set border pixel color. */
8833 wa[0] = win->s->c[SWM_S_COLOR_UNFOCUS].pixel;
8834 wa[1] = XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_PROPERTY_CHANGE |
8835 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
8837 wa[1] |= XCB_EVENT_MASK_LEAVE_WINDOW | XCB_EVENT_MASK_FOCUS_CHANGE;
8840 xcb_change_window_attributes(conn, win->id, XCB_CW_BORDER_PIXEL |
8841 XCB_CW_EVENT_MASK, wa);
8843 /* Get WM_SIZE_HINTS. */
8844 xcb_icccm_get_wm_normal_hints_reply(conn,
8845 xcb_icccm_get_wm_normal_hints(conn, win->id),
8849 xcb_icccm_get_wm_hints_reply(conn,
8850 xcb_icccm_get_wm_hints(conn, win->id),
8853 /* Get WM_TRANSIENT_FOR; see if window is a transient. */
8854 xcb_icccm_get_wm_transient_for_reply(conn,
8855 xcb_icccm_get_wm_transient_for(conn, win->id),
8858 win->transient = trans;
8859 set_child_transient(win, &win->transient);
8862 /* Get WM_PROTOCOLS. */
8863 get_wm_protocols(win);
8865 /* Set initial quirks based on EWMH. */
8866 ewmh_autoquirk(win);
8868 /* Determine initial quirks. */
8869 xcb_icccm_get_wm_class_reply(conn,
8870 xcb_icccm_get_wm_class(conn, win->id),
8873 class = win->ch.class_name ? win->ch.class_name : "";
8874 instance = win->ch.instance_name ? win->ch.instance_name : "";
8875 name = get_win_name(win->id);
8877 DNPRINTF(SWM_D_CLASS, "manage_window: class: %s, instance: %s, "
8878 "name: %s\n", class, instance, name);
8880 /* java is retarded so treat it special */
8881 if (strstr(instance, "sun-awt")) {
8882 DNPRINTF(SWM_D_CLASS, "manage_window: java window detected.\n");
8886 TAILQ_FOREACH(qp, &quirks, entry) {
8887 if (regexec(&qp->regex_class, class, 0, NULL, 0) == 0 &&
8888 regexec(&qp->regex_instance, instance, 0, NULL, 0) == 0 &&
8889 regexec(&qp->regex_name, name, 0, NULL, 0) == 0) {
8890 DNPRINTF(SWM_D_CLASS, "manage_window: matched "
8891 "quirk: %s:%s:%s mask: %#x, ws: %d\n", qp->class,
8892 qp->instance, qp->name, qp->quirk, qp->ws);
8893 win->quirks = qp->quirk;
8894 if (qp->ws >= 0 && qp->ws < workspace_limit)
8901 /* Reset font sizes (the bruteforce way; no default keybinding). */
8902 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
8903 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
8904 fake_keypress(win, XK_KP_Subtract, XCB_MOD_MASK_SHIFT);
8905 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
8906 fake_keypress(win, XK_KP_Add, XCB_MOD_MASK_SHIFT);
8909 /* Figure out which workspace the window belongs to. */
8910 if (!(win->quirks & SWM_Q_IGNOREPID) &&
8911 (p = find_pid(window_get_pid(win->id))) != NULL) {
8912 win->ws = &r->s->ws[p->ws];
8913 TAILQ_REMOVE(&pidlist, p, entry);
8916 } else if ((ws_idx = get_ws_idx(win)) != -1 &&
8918 /* _SWM_WS is set; use that. */
8919 win->ws = &r->s->ws[ws_idx];
8920 } else if (trans && (ww = find_window(trans)) != NULL) {
8921 /* Launch transients in the same ws as parent. */
8928 win->ws = &r->s->ws[force_ws];
8930 /* Set the _NET_WM_DESKTOP atom. */
8931 DNPRINTF(SWM_D_PROP, "manage_window: set _NET_WM_DESKTOP: %d\n",
8933 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
8934 ewmh[_NET_WM_DESKTOP].atom, XCB_ATOM_CARDINAL, 32, 1, &win->ws->idx);
8936 /* Remove any _SWM_WS now that we set _NET_WM_DESKTOP. */
8937 xcb_delete_property(conn, win->id, a_swm_ws);
8939 /* WS must already be set for this to work. */
8940 store_float_geom(win);
8942 /* Make sure window is positioned inside its region, if its active. */
8944 region_containment(win, r, SWM_CW_ALLSIDES |
8945 SWM_CW_HARDBOUNDARY);
8950 /* Figure out where to stack the window in the workspace. */
8951 if (trans && (ww = find_window(trans)))
8952 TAILQ_INSERT_AFTER(&win->ws->winlist, ww, win, entry);
8953 else if (win->ws->focus && spawn_pos == SWM_STACK_ABOVE)
8954 TAILQ_INSERT_AFTER(&win->ws->winlist, win->ws->focus, win,
8956 else if (win->ws->focus && spawn_pos == SWM_STACK_BELOW)
8957 TAILQ_INSERT_BEFORE(win->ws->focus, win, entry);
8958 else switch (spawn_pos) {
8961 case SWM_STACK_ABOVE:
8962 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
8964 case SWM_STACK_BOTTOM:
8965 case SWM_STACK_BELOW:
8966 TAILQ_INSERT_HEAD(&win->ws->winlist, win, entry);
8969 ewmh_update_client_list();
8971 TAILQ_INSERT_TAIL(&win->ws->stack, win, stack_entry);
8973 /* Get/apply initial _NET_WM_STATE */
8974 ewmh_get_wm_state(win);
8977 new_flags = win->ewmh_flags;
8979 if (win->quirks & SWM_Q_FLOAT)
8980 new_flags |= EWMH_F_ABOVE;
8982 if (win->quirks & SWM_Q_ANYWHERE)
8983 new_flags |= SWM_F_MANUAL;
8985 ewmh_apply_flags(win, new_flags);
8986 ewmh_update_wm_state(win);
8988 /* Set initial _NET_WM_ALLOWED_ACTIONS */
8989 ewmh_update_actions(win);
8993 DNPRINTF(SWM_D_MISC, "manage_window: done. win %#x, (x,y) w x h: "
8994 "(%d,%d) %d x %d, ws: %d, iconic: %s, transient: %#x\n", win->id,
8995 X(win), Y(win), WIDTH(win), HEIGHT(win), win->ws->idx,
8996 YESNO(ICONIC(win)), win->transient);
9002 free_window(struct ws_win *win)
9004 DNPRINTF(SWM_D_MISC, "free_window: win %#x\n", win->id);
9009 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
9011 xcb_icccm_get_wm_class_reply_wipe(&win->ch);
9016 memset(win, 0xff, sizeof *win); /* XXX kill later */
9019 DNPRINTF(SWM_D_MISC, "free_window: done.\n");
9023 unmanage_window(struct ws_win *win)
9025 struct ws_win *parent;
9030 DNPRINTF(SWM_D_MISC, "unmanage_window: win %#x\n", win->id);
9033 parent = find_window(win->transient);
9035 parent->focus_child = NULL;
9038 TAILQ_REMOVE(&win->ws->stack, win, stack_entry);
9039 TAILQ_REMOVE(&win->ws->winlist, win, entry);
9040 TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
9042 ewmh_update_client_list();
9046 expose(xcb_expose_event_t *e)
9049 struct swm_region *r;
9051 DNPRINTF(SWM_D_EVENT, "expose: win %#x\n", e->window);
9053 num_screens = get_screen_count();
9054 for (i = 0; i < num_screens; i++)
9055 TAILQ_FOREACH(r, &screens[i].rl, entry)
9056 if (e->window == WINID(r->bar))
9064 focusin(xcb_focus_in_event_t *e)
9066 DNPRINTF(SWM_D_EVENT, "focusin: win %#x, mode: %s(%u), "
9067 "detail: %s(%u)\n", e->event, get_notify_mode_label(e->mode),
9068 e->mode, get_notify_detail_label(e->detail), e->detail);
9072 focusout(xcb_focus_out_event_t *e)
9074 DNPRINTF(SWM_D_EVENT, "focusout: win %#x, mode: %s(%u), "
9075 "detail: %s(%u)\n", e->event, get_notify_mode_label(e->mode),
9076 e->mode, get_notify_detail_label(e->detail), e->detail);
9081 keypress(xcb_key_press_event_t *e)
9083 xcb_keysym_t keysym;
9086 keysym = xcb_key_press_lookup_keysym(syms, e, 0);
9088 DNPRINTF(SWM_D_EVENT, "keypress: keysym: %u, win (x,y): %#x (%d,%d), "
9089 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
9090 "state: %u, same_screen: %s\n", keysym, e->event, e->event_x,
9091 e->event_y, e->detail, e->time, e->root, e->root_x, e->root_y,
9092 e->child, e->state, YESNO(e->same_screen));
9094 if ((kp = key_lookup(CLEANMASK(e->state), keysym)) == NULL)
9097 last_event_time = e->time;
9099 if (kp->funcid == KF_SPAWN_CUSTOM)
9100 spawn_custom(root_to_region(e->root, SWM_CK_ALL),
9101 &(keyfuncs[kp->funcid].args), kp->spawn_name);
9102 else if (keyfuncs[kp->funcid].func)
9103 keyfuncs[kp->funcid].func(root_to_region(e->root, SWM_CK_ALL),
9104 &(keyfuncs[kp->funcid].args));
9107 /* Unfreeze grab events. */
9108 xcb_allow_events(conn, XCB_ALLOW_ASYNC_KEYBOARD, e->time);
9111 DNPRINTF(SWM_D_EVENT, "keypress: done.\n");
9115 buttonpress(xcb_button_press_event_t *e)
9117 struct ws_win *win = NULL;
9118 struct swm_region *r, *old_r;
9120 bool handled = false;
9122 DNPRINTF(SWM_D_EVENT, "buttonpress: win (x,y): %#x (%d,%d), "
9123 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
9124 "state: %u, same_screen: %s\n", e->event, e->event_x, e->event_y,
9125 e->detail, e->time, e->root, e->root_x, e->root_y, e->child,
9126 e->state, YESNO(e->same_screen));
9128 if (e->event == e->root) {
9129 if (e->child != 0) {
9130 win = find_window(e->child);
9131 /* Pass ButtonPress to window if it isn't managed. */
9135 /* Focus on empty region */
9136 /* If no windows on region if its empty. */
9137 r = root_to_region(e->root, SWM_CK_POINTER);
9139 DNPRINTF(SWM_D_EVENT, "buttonpress: "
9140 "NULL region; ignoring.\n");
9144 if (TAILQ_EMPTY(&r->ws->winlist)) {
9145 old_r = root_to_region(e->root, SWM_CK_FOCUS);
9146 if (old_r && old_r != r)
9147 unfocus_win(old_r->ws->focus);
9149 xcb_set_input_focus(conn,
9150 XCB_INPUT_FOCUS_PARENT, e->root, e->time);
9152 /* Clear bar since empty. */
9160 win = find_window(e->event);
9166 last_event_time = e->time;
9168 focus_win(get_focus_magic(win));
9170 for (i = 0; i < LENGTH(buttons); i++)
9171 if (client_click == buttons[i].action && buttons[i].func &&
9172 buttons[i].button == e->detail &&
9173 CLEANMASK(buttons[i].mask) == CLEANMASK(e->state)) {
9174 buttons[i].func(win, &buttons[i].args);
9180 DNPRINTF(SWM_D_EVENT, "buttonpress: passing to window.\n");
9181 /* Replay event to event window */
9182 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, e->time);
9184 DNPRINTF(SWM_D_EVENT, "buttonpress: handled.\n");
9185 /* Unfreeze grab events. */
9186 xcb_allow_events(conn, XCB_ALLOW_SYNC_POINTER, e->time);
9194 print_win_geom(xcb_window_t w)
9196 xcb_get_geometry_reply_t *wa;
9198 wa = xcb_get_geometry_reply(conn, xcb_get_geometry(conn, w), NULL);
9200 DNPRINTF(SWM_D_MISC, "print_win_geom: window not found: %#x\n",
9205 DNPRINTF(SWM_D_MISC, "print_win_geom: win %#x, root: %#x, "
9206 "depth: %u, (x,y) w x h: (%d,%d) %d x %d, border: %d\n",
9207 w, wa->root, wa->depth, wa->x, wa->y, wa->width, wa->height,
9216 get_stack_mode_name(uint8_t mode)
9221 case XCB_STACK_MODE_ABOVE:
9224 case XCB_STACK_MODE_BELOW:
9227 case XCB_STACK_MODE_TOP_IF:
9230 case XCB_STACK_MODE_BOTTOM_IF:
9233 case XCB_STACK_MODE_OPPOSITE:
9245 configurerequest(xcb_configure_request_event_t *e)
9248 struct swm_region *r = NULL;
9250 uint32_t wc[7] = {0};
9254 if ((win = find_window(e->window)) == NULL)
9255 if ((win = find_unmanaged_window(e->window)) == NULL)
9259 if (swm_debug & SWM_D_EVENT) {
9260 print_win_geom(e->window);
9262 DNPRINTF(SWM_D_EVENT, "configurerequest: win %#x, "
9263 "parent: %#x, new: %s, value_mask: %u { ", e->window,
9264 e->parent, YESNO(new), e->value_mask);
9265 if (e->value_mask & XCB_CONFIG_WINDOW_X)
9266 DPRINTF("X: %d ", e->x);
9267 if (e->value_mask & XCB_CONFIG_WINDOW_Y)
9268 DPRINTF("Y: %d ", e->y);
9269 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH)
9270 DPRINTF("W: %u ", e->width);
9271 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
9272 DPRINTF("H: %u ", e->height);
9273 if (e->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH)
9274 DPRINTF("Border: %u ", e->border_width);
9275 if (e->value_mask & XCB_CONFIG_WINDOW_SIBLING)
9276 DPRINTF("Sibling: %#x ", e->sibling);
9277 if (e->value_mask & XCB_CONFIG_WINDOW_STACK_MODE)
9278 DPRINTF("StackMode: %s(%u) ",
9279 get_stack_mode_name(e->stack_mode), e->stack_mode);
9285 if (e->value_mask & XCB_CONFIG_WINDOW_X) {
9286 mask |= XCB_CONFIG_WINDOW_X;
9289 if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
9290 mask |= XCB_CONFIG_WINDOW_Y;
9293 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
9294 mask |= XCB_CONFIG_WINDOW_WIDTH;
9297 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
9298 mask |= XCB_CONFIG_WINDOW_HEIGHT;
9299 wc[i++] = e->height;
9301 if (e->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH) {
9302 mask |= XCB_CONFIG_WINDOW_BORDER_WIDTH;
9303 wc[i++] = e->border_width;
9305 if (e->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
9306 mask |= XCB_CONFIG_WINDOW_SIBLING;
9307 wc[i++] = e->sibling;
9309 if (e->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
9310 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
9311 wc[i++] = e->stack_mode;
9315 xcb_configure_window(conn, e->window, mask, wc);
9318 } else if ((!MANUAL(win) || win->quirks & SWM_Q_ANYWHERE) &&
9319 !FULLSCREEN(win) && !MAXIMIZED(win)) {
9322 else if (win->ws->old_r)
9325 /* windows are centered unless ANYWHERE quirk is set. */
9326 if (win->quirks & SWM_Q_ANYWHERE) {
9327 if (e->value_mask & XCB_CONFIG_WINDOW_X) {
9328 win->g_float.x = e->x;
9330 win->g_float.x -= X(r);
9333 if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
9334 win->g_float.y = e->y;
9336 win->g_float.y -= Y(r);
9340 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH)
9341 win->g_float.w = e->width;
9343 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
9344 win->g_float.h = e->height;
9346 win->g_floatvalid = true;
9348 if (!MAXIMIZED(win) && !FULLSCREEN(win) &&
9349 (TRANS(win) || (ABOVE(win) &&
9350 win->ws->cur_layout != &layouts[SWM_MAX_STACK]))) {
9351 WIDTH(win) = win->g_float.w;
9352 HEIGHT(win) = win->g_float.h;
9355 update_floater(win);
9370 DNPRINTF(SWM_D_EVENT, "configurerequest: done.\n");
9374 configurenotify(xcb_configure_notify_event_t *e)
9378 DNPRINTF(SWM_D_EVENT, "configurenotify: win %#x, event win: %#x, "
9379 "(x,y) WxH: (%d,%d) %ux%u, border: %u, above_sibling: %#x, "
9380 "override_redirect: %s\n", e->window, e->event, e->x, e->y,
9381 e->width, e->height, e->border_width, e->above_sibling,
9382 YESNO(e->override_redirect));
9384 win = find_window(e->window);
9386 xcb_icccm_get_wm_normal_hints_reply(conn,
9387 xcb_icccm_get_wm_normal_hints(conn, win->id),
9390 if (font_adjusted) {
9398 destroynotify(xcb_destroy_notify_event_t *e)
9402 DNPRINTF(SWM_D_EVENT, "destroynotify: win %#x\n", e->window);
9404 if ((win = find_window(e->window)) == NULL) {
9405 if ((win = find_unmanaged_window(e->window)) == NULL)
9411 if (focus_mode != SWM_FOCUS_FOLLOW) {
9412 /* If we were focused, make sure we focus on something else. */
9413 if (win == win->ws->focus)
9414 win->ws->focus_pending = get_focus_prev(win);
9417 unmanage_window(win);
9420 if (focus_mode != SWM_FOCUS_FOLLOW && WS_FOCUSED(win->ws)) {
9421 if (win->ws->focus_pending) {
9422 focus_win(win->ws->focus_pending);
9423 win->ws->focus_pending = NULL;
9424 } else if (win == win->ws->focus) {
9425 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT,
9426 win->ws->r->id, XCB_CURRENT_TIME);
9437 get_notify_detail_label(uint8_t detail)
9442 case XCB_NOTIFY_DETAIL_ANCESTOR:
9445 case XCB_NOTIFY_DETAIL_VIRTUAL:
9448 case XCB_NOTIFY_DETAIL_INFERIOR:
9451 case XCB_NOTIFY_DETAIL_NONLINEAR:
9452 label = "Nonlinear";
9454 case XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL:
9455 label = "NonlinearVirtual";
9457 case XCB_NOTIFY_DETAIL_POINTER:
9460 case XCB_NOTIFY_DETAIL_POINTER_ROOT:
9461 label = "PointerRoot";
9463 case XCB_NOTIFY_DETAIL_NONE:
9474 get_notify_mode_label(uint8_t mode)
9479 case XCB_NOTIFY_MODE_NORMAL:
9482 case XCB_NOTIFY_MODE_GRAB:
9485 case XCB_NOTIFY_MODE_UNGRAB:
9488 case XCB_NOTIFY_MODE_WHILE_GRABBED:
9489 label = "WhileGrabbed";
9500 enternotify(xcb_enter_notify_event_t *e)
9503 struct swm_region *r;
9505 DNPRINTF(SWM_D_FOCUS, "enternotify: time: %u, win (x,y): %#x "
9506 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
9507 "child: %#x, same_screen_focus: %s, state: %d\n",
9508 e->time, e->event, e->event_x, e->event_y,
9509 get_notify_mode_label(e->mode), e->mode,
9510 get_notify_detail_label(e->detail), e->detail,
9511 e->root, e->root_x, e->root_y, e->child,
9512 YESNO(e->same_screen_focus), e->state);
9514 if (focus_mode == SWM_FOCUS_MANUAL &&
9515 e->mode == XCB_NOTIFY_MODE_NORMAL) {
9516 DNPRINTF(SWM_D_EVENT, "enternotify: manual focus; ignoring.\n");
9520 last_event_time = e->time;
9522 if ((win = find_window(e->event)) == NULL) {
9523 if (e->event == e->root) {
9524 /* If no windows on pointer region, then focus root. */
9525 r = root_to_region(e->root, SWM_CK_POINTER);
9527 DNPRINTF(SWM_D_EVENT, "enternotify: "
9528 "NULL region; ignoring.\n");
9534 DNPRINTF(SWM_D_EVENT, "enternotify: window is NULL; "
9539 if (e->mode == XCB_NOTIFY_MODE_NORMAL &&
9540 e->detail == XCB_NOTIFY_DETAIL_INFERIOR) {
9541 DNPRINTF(SWM_D_EVENT, "enternotify: entering from "
9542 "inferior; ignoring\n");
9546 focus_win(get_focus_magic(win));
9554 leavenotify(xcb_leave_notify_event_t *e)
9556 DNPRINTF(SWM_D_FOCUS, "leavenotify: time: %u, win (x,y): %#x "
9557 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
9558 "child: %#x, same_screen_focus: %s, state: %d\n",
9559 e->time, e->event, e->event_x, e->event_y,
9560 get_notify_mode_label(e->mode), e->mode,
9561 get_notify_detail_label(e->detail), e->detail,
9562 e->root, e->root_x, e->root_y, e->child,
9563 YESNO(e->same_screen_focus), e->state);
9568 mapnotify(xcb_map_notify_event_t *e)
9570 struct ws_win *win, *parent = NULL;
9571 struct workspace *ws;
9573 DNPRINTF(SWM_D_EVENT, "mapnotify: win %#x\n", e->window);
9575 if ((win = manage_window(e->window, spawn_position, true)) == NULL)
9579 /* Need to know if win was mapped due to ws switch. */
9580 if (ws->state == SWM_WS_STATE_MAPPED) {
9581 if (ws->focus_pending && TRANS(ws->focus_pending))
9582 parent = find_window(win->transient);
9584 /* If window's parent is maximized, don't clear it. */
9585 if ((parent == NULL) || !MAXIMIZED(parent))
9586 if (clear_maximized(ws) > 0)
9591 set_win_state(win, XCB_ICCCM_WM_STATE_NORMAL);
9593 if (focus_mode != SWM_FOCUS_FOLLOW && WS_FOCUSED(win->ws)) {
9594 if (ws->focus_pending == win) {
9596 ws->focus_pending = NULL;
9597 center_pointer(win->ws->r);
9606 mappingnotify(xcb_mapping_notify_event_t *e)
9609 int i, j, num_screens;
9611 xcb_refresh_keyboard_mapping(syms, e);
9613 if (e->request == XCB_MAPPING_KEYBOARD) {
9616 /* Regrab buttons on all managed windows. */
9617 num_screens = get_screen_count();
9618 for (i = 0; i < num_screens; i++)
9619 for (j = 0; j < workspace_limit; j++)
9620 TAILQ_FOREACH(w, &screens[i].ws[j].winlist,
9627 maprequest(xcb_map_request_event_t *e)
9629 struct ws_win *win, *w = NULL;
9630 xcb_get_window_attributes_reply_t *war;
9632 DNPRINTF(SWM_D_EVENT, "maprequest: win %#x\n",
9635 war = xcb_get_window_attributes_reply(conn,
9636 xcb_get_window_attributes(conn, e->window),
9639 DNPRINTF(SWM_D_EVENT, "maprequest: window lost.\n");
9643 if (war->override_redirect) {
9644 DNPRINTF(SWM_D_EVENT, "maprequest: override_redirect; "
9649 win = manage_window(e->window, spawn_position,
9650 (war->map_state == XCB_MAP_STATE_VIEWABLE));
9654 /* The new window should get focus; prepare. */
9655 if (focus_mode != SWM_FOCUS_FOLLOW &&
9656 !(win->quirks & SWM_Q_NOFOCUSONMAP) &&
9657 (!(win->hints.flags & XCB_ICCCM_WM_HINT_INPUT) ||
9658 (win->hints.flags & XCB_ICCCM_WM_HINT_INPUT &&
9659 win->hints.input))) {
9660 if (win->quirks & SWM_Q_FOCUSONMAP_SINGLE) {
9661 /* See if other wins of same type are already mapped. */
9662 TAILQ_FOREACH(w, &win->ws->winlist, entry) {
9663 if (w == win || !w->mapped)
9666 if (w->ch.class_name &&
9667 win->ch.class_name &&
9668 strcmp(w->ch.class_name,
9669 win->ch.class_name) == 0 &&
9670 w->ch.instance_name &&
9671 win->ch.instance_name &&
9672 strcmp(w->ch.instance_name,
9673 win->ch.instance_name) == 0)
9679 win->ws->focus_pending = get_focus_magic(win);
9682 /* All windows need to be mapped if they are in the current workspace.*/
9686 /* Ignore EnterNotify to handle the mapnotify without interference. */
9687 if (focus_mode == SWM_FOCUS_DEFAULT)
9688 event_drain(XCB_ENTER_NOTIFY);
9691 DNPRINTF(SWM_D_EVENT, "maprequest: done.\n");
9695 motionnotify(xcb_motion_notify_event_t *e)
9697 struct swm_region *r = NULL;
9700 DNPRINTF(SWM_D_FOCUS, "motionnotify: time: %u, win (x,y): %#x "
9701 "(%d,%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
9702 "child: %#x, same_screen_focus: %s, state: %d\n",
9703 e->time, e->event, e->event_x, e->event_y,
9704 get_notify_detail_label(e->detail), e->detail,
9705 e->root, e->root_x, e->root_y, e->child,
9706 YESNO(e->same_screen), e->state);
9708 last_event_time = e->time;
9710 if (focus_mode == SWM_FOCUS_MANUAL)
9713 num_screens = get_screen_count();
9714 for (i = 0; i < num_screens; i++)
9715 if (screens[i].root == e->root)
9718 TAILQ_FOREACH(r, &screens[i].rl, entry)
9719 if (X(r) <= e->root_x && e->root_x < MAX_X(r) &&
9720 Y(r) <= e->root_y && e->root_y < MAX_Y(r))
9728 get_atom_name(xcb_atom_t atom)
9731 #ifdef SWM_DEBUG_ATOM_NAMES
9733 * This should be disabled during most debugging since
9734 * xcb_get_* causes an xcb_flush.
9737 xcb_get_atom_name_reply_t *r;
9739 r = xcb_get_atom_name_reply(conn,
9740 xcb_get_atom_name(conn, atom),
9743 len = xcb_get_atom_name_name_length(r);
9745 name = malloc(len + 1);
9747 memcpy(name, xcb_get_atom_name_name(r), len);
9761 propertynotify(xcb_property_notify_event_t *e)
9764 struct workspace *ws;
9768 name = get_atom_name(e->atom);
9769 DNPRINTF(SWM_D_EVENT, "propertynotify: win %#x, atom: %s(%u), "
9770 "time: %#x, state: %u\n", e->window, name, e->atom, e->time,
9774 win = find_window(e->window);
9780 last_event_time = e->time;
9782 if (e->atom == a_state) {
9783 /* State just changed, make sure it gets focused if mapped. */
9784 if (e->state == XCB_PROPERTY_NEW_VALUE) {
9785 if (focus_mode != SWM_FOCUS_FOLLOW && WS_FOCUSED(ws)) {
9787 ws->focus_pending == win) {
9788 focus_win(ws->focus_pending);
9789 ws->focus_pending = NULL;
9793 } else if (e->atom == XCB_ATOM_WM_CLASS ||
9794 e->atom == XCB_ATOM_WM_NAME) {
9796 } else if (e->atom == a_prot) {
9797 get_wm_protocols(win);
9804 unmapnotify(xcb_unmap_notify_event_t *e)
9807 struct workspace *ws;
9809 DNPRINTF(SWM_D_EVENT, "unmapnotify: win %#x\n", e->window);
9811 /* If we aren't managing the window, then ignore. */
9812 win = find_window(e->window);
9813 if (win == NULL || win->id != e->window)
9816 /* Do nothing if already withdrawn. */
9817 if (!win->mapped && !ICONIC(win))
9821 win->mapped = false;
9823 /* If win was focused, make sure to focus on something else. */
9824 if (win == ws->focus) {
9825 if (focus_mode != SWM_FOCUS_FOLLOW) {
9826 ws->focus_pending = get_focus_prev(win);
9827 DNPRINTF(SWM_D_EVENT, "unmapnotify: "
9828 "focus_pending: %#x\n",
9829 WINID(ws->focus_pending));
9837 set_win_state(win, XCB_ICCCM_WM_STATE_ICONIC);
9840 set_win_state(win, XCB_ICCCM_WM_STATE_WITHDRAWN);
9841 unmanage_window(win);
9847 /* Update focus if ws is active. */
9848 if (WS_FOCUSED(ws)) {
9849 if (focus_mode == SWM_FOCUS_FOLLOW) {
9850 focus_win(get_pointer_win(ws->r->s->root));
9851 } else if (ws->focus_pending) {
9852 focus_win(ws->focus_pending);
9853 ws->focus_pending = NULL;
9854 } else if (ws->focus == NULL) {
9855 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT,
9856 ws->r->id, XCB_CURRENT_TIME);
9860 center_pointer(ws->r);
9866 get_source_type_label(uint32_t type)
9871 case EWMH_SOURCE_TYPE_NONE:
9874 case EWMH_SOURCE_TYPE_NORMAL:
9877 case EWMH_SOURCE_TYPE_OTHER:
9889 clientmessage(xcb_client_message_event_t *e)
9892 struct swm_region *r = NULL;
9896 xcb_map_request_event_t mre;
9900 name = get_atom_name(e->type);
9901 DNPRINTF(SWM_D_EVENT, "clientmessage: win %#x, atom: %s(%u)\n",
9902 e->window, name, e->type);
9906 if (e->type == ewmh[_NET_CURRENT_DESKTOP].atom) {
9907 num_screens = get_screen_count();
9908 for (i = 0; i < num_screens; i++)
9909 if (screens[i].root == e->window) {
9910 r = screens[i].r_focus;
9914 if (r && e->data.data32[0] < (uint32_t)workspace_limit) {
9915 a.id = e->data.data32[0];
9923 win = find_window(e->window);
9925 if (e->type == ewmh[_NET_ACTIVE_WINDOW].atom) {
9926 /* Manage the window with maprequest. */
9927 DNPRINTF(SWM_D_EVENT, "clientmessage: request focus on "
9928 "unmanaged window.\n");
9929 mre.window = e->window;
9935 if (e->type == ewmh[_NET_ACTIVE_WINDOW].atom) {
9936 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_ACTIVE_WINDOW, "
9937 "source_type: %s(%d)\n",
9938 get_source_type_label(e->data.data32[0]),
9942 * Allow focus changes that are a result of direct user
9943 * action and from applications that use the old EWMH spec.
9945 if (e->data.data32[0] != EWMH_SOURCE_TYPE_NORMAL ||
9946 win->quirks & SWM_Q_OBEYAPPFOCUSREQ) {
9947 if (WS_FOCUSED(win->ws))
9950 win->ws->focus_pending = win;
9952 } else if (e->type == ewmh[_NET_CLOSE_WINDOW].atom) {
9953 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_CLOSE_WINDOW\n");
9954 if (win->can_delete)
9955 client_msg(win, a_delete, 0);
9957 xcb_kill_client(conn, win->id);
9958 } else if (e->type == ewmh[_NET_MOVERESIZE_WINDOW].atom) {
9959 DNPRINTF(SWM_D_EVENT,
9960 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
9962 if (e->data.data32[0] & (1<<8)) /* x */
9963 X(win) = e->data.data32[1];
9964 if (e->data.data32[0] & (1<<9)) /* y */
9965 Y(win) = e->data.data32[2];
9966 if (e->data.data32[0] & (1<<10)) /* width */
9967 WIDTH(win) = e->data.data32[3];
9968 if (e->data.data32[0] & (1<<11)) /* height */
9969 HEIGHT(win) = e->data.data32[4];
9973 /* Notify no change was made. */
9974 config_win(win, NULL);
9975 /* TODO: Change stack sizes */
9977 } else if (e->type == ewmh[_NET_RESTACK_WINDOW].atom) {
9978 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_RESTACK_WINDOW\n");
9979 val[0] = e->data.data32[1]; /* Sibling window. */
9980 val[1] = e->data.data32[2]; /* Stack mode detail. */
9982 xcb_configure_window(conn, win->id, XCB_CONFIG_WINDOW_SIBLING |
9983 XCB_CONFIG_WINDOW_STACK_MODE, val);
9984 } else if (e->type == ewmh[_NET_WM_STATE].atom) {
9985 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_WM_STATE\n");
9986 ewmh_change_wm_state(win, e->data.data32[1], e->data.data32[0]);
9987 if (e->data.data32[2])
9988 ewmh_change_wm_state(win, e->data.data32[2],
9991 ewmh_update_wm_state(win);
9993 } else if (e->type == ewmh[_NET_WM_DESKTOP].atom) {
9994 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_WM_DESKTOP\n");
9997 win_to_ws(win, e->data.data32[0], true);
9999 /* Restack if either the source or destination ws is mapped. */
10000 if (r != NULL || win->ws->r != NULL) {
10002 load_float_geom(win);
10014 int errcode = xcb_connection_has_error(conn);
10015 #ifdef XCB_CONN_ERROR
10018 case XCB_CONN_ERROR:
10019 s = "Socket error, pipe error or other stream error.";
10021 case XCB_CONN_CLOSED_EXT_NOTSUPPORTED:
10022 s = "Extension not supported.";
10024 case XCB_CONN_CLOSED_MEM_INSUFFICIENT:
10025 s = "Insufficient memory.";
10027 case XCB_CONN_CLOSED_REQ_LEN_EXCEED:
10028 s = "Request length was exceeded.";
10030 case XCB_CONN_CLOSED_PARSE_ERR:
10031 s = "Error parsing display string.";
10034 s = "Unknown error.";
10037 errx(errcode, "X CONNECTION ERROR: %s", s);
10040 errx(errcode, "X CONNECTION ERROR");
10047 int num_screens, i;
10048 const uint32_t val = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
10049 XCB_EVENT_MASK_ENTER_WINDOW;
10051 xcb_void_cookie_t wac;
10052 xcb_generic_error_t *error;
10054 /* this causes an error if some other window manager is running */
10055 num_screens = get_screen_count();
10056 for (i = 0; i < num_screens; i++) {
10057 if ((sc = get_screen(i)) == NULL)
10058 errx(1, "ERROR: can't get screen %d.", i);
10059 DNPRINTF(SWM_D_INIT, "enable_wm: screen %d, root: %#x\n",
10061 wac = xcb_change_window_attributes_checked(conn, sc->root,
10062 XCB_CW_EVENT_MASK, &val);
10063 if ((error = xcb_request_check(conn, wac))) {
10064 DNPRINTF(SWM_D_INIT, "enable_wm: error_code: %u\n",
10065 error->error_code);
10070 /* click to focus on empty region */
10071 xcb_grab_button(conn, 1, sc->root, BUTTONMASK,
10072 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC, XCB_WINDOW_NONE,
10073 XCB_CURSOR_NONE, XCB_BUTTON_INDEX_1, XCB_BUTTON_MASK_ANY);
10080 new_region(struct swm_screen *s, int x, int y, int w, int h)
10082 struct swm_region *r = NULL, *n;
10083 struct workspace *ws = NULL;
10087 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
10088 s->idx, w, h, x, y);
10090 /* remove any conflicting regions */
10091 n = TAILQ_FIRST(&s->rl);
10094 n = TAILQ_NEXT(r, entry);
10095 if (X(r) < (x + w) && (X(r) + WIDTH(r)) > x &&
10096 Y(r) < (y + h) && (Y(r) + HEIGHT(r)) > y) {
10097 if (r->ws->r != NULL)
10098 r->ws->old_r = r->ws->r;
10101 xcb_destroy_window(conn, r->id);
10102 TAILQ_REMOVE(&s->rl, r, entry);
10103 TAILQ_INSERT_TAIL(&s->orl, r, entry);
10107 /* search old regions for one to reuse */
10109 /* size + location match */
10110 TAILQ_FOREACH(r, &s->orl, entry)
10111 if (X(r) == x && Y(r) == y &&
10112 HEIGHT(r) == h && WIDTH(r) == w)
10116 TAILQ_FOREACH(r, &s->orl, entry)
10117 if (HEIGHT(r) == h && WIDTH(r) == w)
10121 TAILQ_REMOVE(&s->orl, r, entry);
10122 /* try to use old region's workspace */
10123 if (r->ws->r == NULL)
10126 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
10127 err(1, "new_region: calloc: failed to allocate memory "
10130 /* if we don't have a workspace already, find one */
10132 for (i = 0; i < workspace_limit; i++)
10133 if (s->ws[i].r == NULL) {
10140 errx(1, "new_region: no free workspaces");
10142 if (ws->state == SWM_WS_STATE_HIDDEN)
10143 ws->state = SWM_WS_STATE_MAPPING;
10151 r->ws_prior = NULL;
10154 TAILQ_INSERT_TAIL(&s->rl, r, entry);
10156 /* Invisible region window to detect pointer events on empty regions. */
10157 r->id = xcb_generate_id(conn);
10158 wa[0] = XCB_EVENT_MASK_POINTER_MOTION |
10159 XCB_EVENT_MASK_POINTER_MOTION_HINT;
10161 xcb_create_window(conn, XCB_COPY_FROM_PARENT, r->id, r->s->root,
10162 X(r), Y(r), WIDTH(r), HEIGHT(r), 0, XCB_WINDOW_CLASS_INPUT_ONLY,
10163 XCB_COPY_FROM_PARENT, XCB_CW_EVENT_MASK, wa);
10165 /* Make sure region input is at the bottom. */
10166 wa[0] = XCB_STACK_MODE_BELOW;
10167 xcb_configure_window(conn, r->id, XCB_CONFIG_WINDOW_STACK_MODE, wa);
10169 xcb_map_window(conn, r->id);
10173 scan_randr(int idx)
10175 #ifdef SWM_XRR_HAS_CRTC
10178 #endif /* SWM_XRR_HAS_CRTC */
10179 struct swm_region *r;
10180 struct ws_win *win;
10182 xcb_randr_get_screen_resources_current_cookie_t src;
10183 xcb_randr_get_screen_resources_current_reply_t *srr;
10184 xcb_randr_get_crtc_info_cookie_t cic;
10185 xcb_randr_get_crtc_info_reply_t *cir = NULL;
10186 xcb_randr_crtc_t *crtc;
10187 xcb_screen_t *screen;
10189 DNPRINTF(SWM_D_MISC, "scan_randr: screen: %d\n", idx);
10191 if ((screen = get_screen(idx)) == NULL)
10192 errx(1, "ERROR: can't get screen %d.", idx);
10194 num_screens = get_screen_count();
10195 if (idx >= num_screens)
10196 errx(1, "scan_randr: invalid screen");
10198 /* remove any old regions */
10199 while ((r = TAILQ_FIRST(&screens[idx].rl)) != NULL) {
10200 r->ws->old_r = r->ws->r = NULL;
10202 xcb_destroy_window(conn, r->id);
10203 TAILQ_REMOVE(&screens[idx].rl, r, entry);
10204 TAILQ_INSERT_TAIL(&screens[idx].orl, r, entry);
10208 /* map virtual screens onto physical screens */
10209 #ifdef SWM_XRR_HAS_CRTC
10210 if (randr_support) {
10211 src = xcb_randr_get_screen_resources_current(conn,
10212 screens[idx].root);
10213 srr = xcb_randr_get_screen_resources_current_reply(conn, src,
10216 new_region(&screens[idx], 0, 0,
10217 screen->width_in_pixels,
10218 screen->height_in_pixels);
10221 ncrtc = srr->num_crtcs;
10223 crtc = xcb_randr_get_screen_resources_current_crtcs(srr);
10224 for (c = 0; c < ncrtc; c++) {
10225 cic = xcb_randr_get_crtc_info(conn, crtc[c],
10227 cir = xcb_randr_get_crtc_info_reply(conn, cic, NULL);
10230 if (cir->num_outputs == 0) {
10235 if (cir->mode == 0)
10236 new_region(&screens[idx], 0, 0,
10237 screen->width_in_pixels,
10238 screen->height_in_pixels);
10240 new_region(&screens[idx],
10241 cir->x, cir->y, cir->width, cir->height);
10246 #endif /* SWM_XRR_HAS_CRTC */
10248 /* If detection failed, create a single region that spans the screen. */
10249 if (TAILQ_EMPTY(&screens[idx].rl))
10250 new_region(&screens[idx], 0, 0, screen->width_in_pixels,
10251 screen->height_in_pixels);
10254 /* Cleanup unused previously visible workspaces. */
10255 TAILQ_FOREACH(r, &screens[idx].orl, entry) {
10256 TAILQ_FOREACH(win, &r->ws->winlist, entry)
10258 r->ws->state = SWM_WS_STATE_HIDDEN;
10260 /* The screen shouldn't focus on an unused region. */
10261 if (screens[idx].r_focus == r)
10262 screens[idx].r_focus = NULL;
10265 DNPRINTF(SWM_D_MISC, "scan_randr: done.\n");
10269 screenchange(xcb_randr_screen_change_notify_event_t *e)
10271 struct swm_region *r;
10272 int i, num_screens;
10274 DNPRINTF(SWM_D_EVENT, "screenchange: root: %#x\n", e->root);
10276 num_screens = get_screen_count();
10277 /* silly event doesn't include the screen index */
10278 for (i = 0; i < num_screens; i++)
10279 if (screens[i].root == e->root)
10281 if (i >= num_screens)
10282 errx(1, "screenchange: screen not found");
10284 /* brute force for now, just re-enumerate the regions */
10288 print_win_geom(e->root);
10290 /* add bars to all regions */
10291 for (i = 0; i < num_screens; i++) {
10292 TAILQ_FOREACH(r, &screens[i].rl, entry)
10298 /* Make sure a region has focus on each screen. */
10299 for (i = 0; i < num_screens; i++) {
10300 if (screens[i].r_focus == NULL) {
10301 r = TAILQ_FIRST(&screens[i].rl);
10310 /* Update workspace state on all regions. */
10311 for (i = 0; i < num_screens; i++)
10312 TAILQ_FOREACH(r, &screens[i].rl, entry)
10313 r->ws->state = SWM_WS_STATE_MAPPED;
10319 struct swm_region *r = NULL;
10320 xcb_window_t *wins = NULL, trans, *cwins = NULL;
10321 int i, j, k, n, no, num_screens;
10323 bool manage, mapped;
10325 xcb_query_tree_cookie_t qtc;
10326 xcb_query_tree_reply_t *qtr;
10327 xcb_get_window_attributes_cookie_t gac;
10328 xcb_get_window_attributes_reply_t *gar;
10329 xcb_get_property_cookie_t pc;
10330 xcb_get_property_reply_t *pr;
10332 DNPRINTF(SWM_D_INIT, "grab_windows: begin\n");
10333 num_screens = get_screen_count();
10334 for (i = 0; i < num_screens; i++) {
10335 qtc = xcb_query_tree(conn, screens[i].root);
10336 qtr = xcb_query_tree_reply(conn, qtc, NULL);
10339 wins = xcb_query_tree_children(qtr);
10340 no = xcb_query_tree_children_length(qtr);
10342 /* Try to sort windows according to _NET_CLIENT_LIST. */
10343 pr = xcb_get_property_reply(conn, xcb_get_property(conn, 0,
10344 screens[i].root, ewmh[_NET_CLIENT_LIST].atom,
10345 XCB_ATOM_WINDOW, 0, UINT32_MAX), NULL);
10347 cwins = xcb_get_property_value(pr);
10348 n = xcb_get_property_value_length(pr) /
10349 sizeof(xcb_atom_t);
10351 for (j = 0; j < n; ++j) {
10352 for (k = j; k < no; ++k) {
10353 if (wins[k] == cwins[j]) {
10354 /* Swap wins j and k. */
10356 wins[j] = cwins[j];
10364 /* attach windows to a region */
10365 /* normal windows */
10366 DNPRINTF(SWM_D_INIT, "grab_windows: grab top level windows.\n");
10367 for (j = 0; j < no; j++) {
10368 TAILQ_FOREACH(r, &screens[i].rl, entry) {
10369 if (r->id == wins[j]) {
10370 DNPRINTF(SWM_D_INIT, "grab_windows: "
10371 "skip %#x; region input window.\n",
10374 } else if (r->bar->id == wins[j]) {
10375 DNPRINTF(SWM_D_INIT, "grab_windows: "
10376 "skip %#x; region bar.\n",
10385 gac = xcb_get_window_attributes(conn, wins[j]);
10386 gar = xcb_get_window_attributes_reply(conn, gac, NULL);
10388 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
10389 "doesn't exist.\n", wins[j]);
10393 if (gar->override_redirect) {
10394 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
10395 "override_redirect set.\n", wins[j]);
10400 pc = xcb_icccm_get_wm_transient_for(conn, wins[j]);
10401 if (xcb_icccm_get_wm_transient_for_reply(conn, pc,
10403 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
10404 "is transient for %#x.\n", wins[j], trans);
10409 state = get_win_state(wins[j]);
10410 manage = state != XCB_ICCCM_WM_STATE_WITHDRAWN;
10411 mapped = gar->map_state == XCB_MAP_STATE_VIEWABLE;
10412 if (mapped || manage)
10413 manage_window(wins[j], SWM_STACK_TOP, mapped);
10416 /* transient windows */
10417 DNPRINTF(SWM_D_INIT, "grab_windows: grab transient windows.\n");
10418 for (j = 0; j < no; j++) {
10419 gac = xcb_get_window_attributes(conn, wins[j]);
10420 gar = xcb_get_window_attributes_reply(conn, gac, NULL);
10422 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
10423 "doesn't exist.\n", wins[j]);
10427 if (gar->override_redirect) {
10428 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
10429 "override_redirect set.\n", wins[j]);
10434 state = get_win_state(wins[j]);
10435 manage = state != XCB_ICCCM_WM_STATE_WITHDRAWN;
10436 mapped = gar->map_state == XCB_MAP_STATE_VIEWABLE;
10437 pc = xcb_icccm_get_wm_transient_for(conn, wins[j]);
10438 if (xcb_icccm_get_wm_transient_for_reply(conn, pc,
10439 &trans, NULL) && manage)
10440 manage_window(wins[j], SWM_STACK_TOP, mapped);
10445 DNPRINTF(SWM_D_INIT, "grab_windows: done.\n");
10449 setup_screens(void)
10451 int i, j, k, num_screens;
10452 struct workspace *ws;
10453 uint32_t gcv[1], wa[1];
10454 const xcb_query_extension_reply_t *qep;
10455 xcb_screen_t *screen;
10456 xcb_randr_query_version_cookie_t c;
10457 xcb_randr_query_version_reply_t *r;
10459 num_screens = get_screen_count();
10460 if ((screens = calloc(num_screens,
10461 sizeof(struct swm_screen))) == NULL)
10462 err(1, "setup_screens: calloc: failed to allocate memory for "
10465 /* Initial RandR setup. */
10466 randr_support = false;
10467 qep = xcb_get_extension_data(conn, &xcb_randr_id);
10468 if (qep->present) {
10469 c = xcb_randr_query_version(conn, 1, 1);
10470 r = xcb_randr_query_version_reply(conn, c, NULL);
10472 if (r->major_version >= 1) {
10473 randr_support = true;
10474 randr_eventbase = qep->first_event;
10480 wa[0] = cursors[XC_LEFT_PTR].cid;
10482 /* map physical screens */
10483 for (i = 0; i < num_screens; i++) {
10484 DNPRINTF(SWM_D_WS, "setup_screens: init screen: %d\n", i);
10485 screens[i].idx = i;
10486 screens[i].r_focus = NULL;
10488 TAILQ_INIT(&screens[i].rl);
10489 TAILQ_INIT(&screens[i].orl);
10490 if ((screen = get_screen(i)) == NULL)
10491 errx(1, "ERROR: can't get screen %d.", i);
10492 screens[i].root = screen->root;
10494 /* set default colors */
10495 setscreencolor("red", i, SWM_S_COLOR_FOCUS);
10496 setscreencolor("rgb:88/88/88", i, SWM_S_COLOR_UNFOCUS);
10497 setscreencolor("rgb:00/80/80", i, SWM_S_COLOR_BAR_BORDER);
10498 setscreencolor("rgb:00/40/40", i,
10499 SWM_S_COLOR_BAR_BORDER_UNFOCUS);
10500 setscreencolor("black", i, SWM_S_COLOR_BAR);
10501 setscreencolor("rgb:a0/a0/a0", i, SWM_S_COLOR_BAR_FONT);
10502 setscreencolor("red", i, SWM_S_COLOR_FOCUS_MAXIMIZED);
10503 setscreencolor("rgb:88/88/88", i,
10504 SWM_S_COLOR_UNFOCUS_MAXIMIZED);
10506 /* create graphics context on screen */
10507 screens[i].bar_gc = xcb_generate_id(conn);
10509 xcb_create_gc(conn, screens[i].bar_gc, screens[i].root,
10510 XCB_GC_GRAPHICS_EXPOSURES, gcv);
10512 /* set default cursor */
10513 xcb_change_window_attributes(conn, screens[i].root,
10514 XCB_CW_CURSOR, wa);
10516 /* init all workspaces */
10517 /* XXX these should be dynamically allocated too */
10518 for (j = 0; j < SWM_WS_MAX; j++) {
10519 ws = &screens[i].ws[j];
10522 ws->bar_enabled = true;
10524 ws->focus_prev = NULL;
10525 ws->focus_pending = NULL;
10528 ws->state = SWM_WS_STATE_HIDDEN;
10529 TAILQ_INIT(&ws->stack);
10530 TAILQ_INIT(&ws->winlist);
10531 TAILQ_INIT(&ws->unmanagedlist);
10533 for (k = 0; layouts[k].l_stack != NULL; k++)
10534 if (layouts[k].l_config != NULL)
10535 layouts[k].l_config(ws,
10536 SWM_ARG_ID_STACKINIT);
10537 ws->cur_layout = &layouts[0];
10538 ws->cur_layout->l_string(ws);
10544 xcb_randr_select_input(conn, screens[i].root,
10545 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE);
10550 setup_globals(void)
10552 if ((bar_fonts = strdup(SWM_BAR_FONTS)) == NULL)
10553 err(1, "setup_globals: strdup: failed to allocate memory.");
10555 if ((clock_format = strdup("%a %b %d %R %Z %Y")) == NULL)
10556 err(1, "setup_globals: strdup: failed to allocate memory.");
10558 if ((syms = xcb_key_symbols_alloc(conn)) == NULL)
10559 errx(1, "unable to allocate key symbols");
10561 a_state = get_atom_from_string("WM_STATE");
10562 a_prot = get_atom_from_string("WM_PROTOCOLS");
10563 a_delete = get_atom_from_string("WM_DELETE_WINDOW");
10564 a_net_supported = get_atom_from_string("_NET_SUPPORTED");
10565 a_net_wm_check = get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
10566 a_takefocus = get_atom_from_string("WM_TAKE_FOCUS");
10567 a_utf8_string = get_atom_from_string("UTF8_STRING");
10568 a_swm_ws = get_atom_from_string("_SWM_WS");
10572 shutdown_cleanup(void)
10574 int i, num_screens;
10576 /* disable alarm because the following code may not be interrupted */
10578 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
10579 err(1, "can't disable alarm");
10588 num_screens = get_screen_count();
10589 for (i = 0; i < num_screens; ++i) {
10590 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT,
10591 screens[i].root, XCB_CURRENT_TIME);
10593 if (screens[i].bar_gc != XCB_NONE)
10594 xcb_free_gc(conn, screens[i].bar_gc);
10595 if (!bar_font_legacy) {
10596 XftColorFree(display, DefaultVisual(display, i),
10597 DefaultColormap(display, i), &bar_font_color);
10598 XftColorFree(display, DefaultVisual(display, i),
10599 DefaultColormap(display, i), &search_font_color);
10603 if (bar_font_legacy)
10604 XFreeFontSet(display, bar_fs);
10606 XftFontClose(display, bar_font);
10609 xcb_key_symbols_free(syms);
10611 xcb_disconnect(conn);
10615 event_error(xcb_generic_error_t *e)
10619 DNPRINTF(SWM_D_EVENT, "event_error: %s(%u) from %s(%u), sequence: %u, "
10620 "resource_id: %u, minor_code: %u\n",
10621 xcb_event_get_error_label(e->error_code), e->error_code,
10622 xcb_event_get_request_label(e->major_code), e->major_code,
10623 e->sequence, e->resource_id, e->minor_code);
10627 event_handle(xcb_generic_event_t *evt)
10629 uint8_t type = XCB_EVENT_RESPONSE_TYPE(evt);
10631 DNPRINTF(SWM_D_EVENT, "XCB Event: %s(%d), seq %u\n",
10632 xcb_event_get_label(XCB_EVENT_RESPONSE_TYPE(evt)),
10633 XCB_EVENT_RESPONSE_TYPE(evt), evt->sequence);
10636 #define EVENT(type, callback) case type: callback((void *)evt); return
10637 EVENT(0, event_error);
10638 EVENT(XCB_BUTTON_PRESS, buttonpress);
10639 /*EVENT(XCB_BUTTON_RELEASE, buttonpress);*/
10640 /*EVENT(XCB_CIRCULATE_NOTIFY, );*/
10641 /*EVENT(XCB_CIRCULATE_REQUEST, );*/
10642 EVENT(XCB_CLIENT_MESSAGE, clientmessage);
10643 /*EVENT(XCB_COLORMAP_NOTIFY, );*/
10644 EVENT(XCB_CONFIGURE_NOTIFY, configurenotify);
10645 EVENT(XCB_CONFIGURE_REQUEST, configurerequest);
10646 /*EVENT(XCB_CREATE_NOTIFY, );*/
10647 EVENT(XCB_DESTROY_NOTIFY, destroynotify);
10648 EVENT(XCB_ENTER_NOTIFY, enternotify);
10649 EVENT(XCB_EXPOSE, expose);
10651 EVENT(XCB_FOCUS_IN, focusin);
10652 EVENT(XCB_FOCUS_OUT, focusout);
10654 /*EVENT(XCB_GRAPHICS_EXPOSURE, );*/
10655 /*EVENT(XCB_GRAVITY_NOTIFY, );*/
10656 EVENT(XCB_KEY_PRESS, keypress);
10657 /*EVENT(XCB_KEY_RELEASE, keypress);*/
10658 /*EVENT(XCB_KEYMAP_NOTIFY, );*/
10660 EVENT(XCB_LEAVE_NOTIFY, leavenotify);
10662 EVENT(XCB_MAP_NOTIFY, mapnotify);
10663 EVENT(XCB_MAP_REQUEST, maprequest);
10664 EVENT(XCB_MAPPING_NOTIFY, mappingnotify);
10665 EVENT(XCB_MOTION_NOTIFY, motionnotify);
10666 /*EVENT(XCB_NO_EXPOSURE, );*/
10667 EVENT(XCB_PROPERTY_NOTIFY, propertynotify);
10668 /*EVENT(XCB_REPARENT_NOTIFY, );*/
10669 /*EVENT(XCB_RESIZE_REQUEST, );*/
10670 /*EVENT(XCB_SELECTION_CLEAR, );*/
10671 /*EVENT(XCB_SELECTION_NOTIFY, );*/
10672 /*EVENT(XCB_SELECTION_REQUEST, );*/
10673 EVENT(XCB_UNMAP_NOTIFY, unmapnotify);
10674 /*EVENT(XCB_VISIBILITY_NOTIFY, );*/
10677 if (type - randr_eventbase == XCB_RANDR_SCREEN_CHANGE_NOTIFY)
10678 screenchange((void *)evt);
10682 main(int argc, char *argv[])
10684 struct swm_region *r;
10685 char conf[PATH_MAX], *cfile = NULL;
10687 int xfd, i, num_screens;
10688 struct sigaction sact;
10689 xcb_generic_event_t *evt;
10694 bool stdin_ready = false, startup = true;
10696 /* suppress unused warning since var is needed */
10700 time_started = time(NULL);
10704 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION, buildstr);
10705 if (setlocale(LC_CTYPE, "") == NULL || setlocale(LC_TIME, "") == NULL)
10706 warnx("no locale support");
10708 /* handle some signals */
10709 bzero(&sact, sizeof(sact));
10710 sigemptyset(&sact.sa_mask);
10712 sact.sa_handler = sighdlr;
10713 sigaction(SIGINT, &sact, NULL);
10714 sigaction(SIGQUIT, &sact, NULL);
10715 sigaction(SIGTERM, &sact, NULL);
10716 sigaction(SIGHUP, &sact, NULL);
10718 sact.sa_handler = sighdlr;
10719 sact.sa_flags = SA_NOCLDSTOP;
10720 sigaction(SIGCHLD, &sact, NULL);
10722 if ((display = XOpenDisplay(0)) == NULL)
10723 errx(1, "can not open display");
10725 conn = XGetXCBConnection(display);
10726 if (xcb_connection_has_error(conn))
10727 errx(1, "can not get XCB connection");
10729 XSetEventQueueOwner(display, XCBOwnsEventQueue);
10731 xcb_prefetch_extension_data(conn, &xcb_randr_id);
10732 xfd = xcb_get_file_descriptor(conn);
10734 /* look for local and global conf file */
10735 pwd = getpwuid(getuid());
10737 errx(1, "invalid user: %d", getuid());
10739 xcb_grab_server(conn);
10740 xcb_aux_sync(conn);
10742 /* flush all events */
10743 while ((evt = xcb_poll_for_event(conn))) {
10744 if (XCB_EVENT_RESPONSE_TYPE(evt) == 0)
10750 errx(1, "another window manager is currently running");
10752 /* Load Xcursors and/or cursorfont glyph cursors. */
10755 xcb_aux_sync(conn);
10765 for (i = 0; ; i++) {
10770 snprintf(conf, sizeof conf, "%s/.%s",
10771 pwd->pw_dir, SWM_CONF_FILE);
10775 snprintf(conf, sizeof conf, "/etc/%s",
10780 snprintf(conf, sizeof conf, "%s/.%s",
10781 pwd->pw_dir, SWM_CONF_FILE_OLD);
10784 /* global compat */
10785 snprintf(conf, sizeof conf, "/etc/%s",
10786 SWM_CONF_FILE_OLD);
10792 if (strlen(conf) && stat(conf, &sb) != -1)
10793 if (S_ISREG(sb.st_mode)) {
10800 /* load conf (if any) */
10802 conf_load(cfile, SWM_CONF_DEFAULT);
10806 if (getenv("SWM_STARTED") == NULL)
10807 setenv("SWM_STARTED", "YES", 1);
10809 /* setup all bars */
10810 num_screens = get_screen_count();
10811 for (i = 0; i < num_screens; i++)
10812 TAILQ_FOREACH(r, &screens[i].rl, entry)
10815 /* Manage existing windows. */
10822 xcb_ungrab_server(conn);
10825 /* Update state of each newly mapped workspace. */
10826 for (i = 0; i < num_screens; i++)
10827 TAILQ_FOREACH(r, &screens[i].rl, entry)
10828 r->ws->state = SWM_WS_STATE_MAPPED;
10830 rd_max = xfd > STDIN_FILENO ? xfd : STDIN_FILENO;
10833 while ((evt = xcb_poll_for_event(conn))) {
10840 /* If just (re)started, set default focus if needed. */
10844 if (focus_mode != SWM_FOCUS_FOLLOW) {
10845 r = TAILQ_FIRST(&screens[0].rl);
10857 FD_SET(STDIN_FILENO, &rd);
10862 num_readable = select(rd_max + 1, &rd, NULL, NULL, &tv);
10863 if (num_readable == -1 && errno != EINTR) {
10864 DNPRINTF(SWM_D_MISC, "select failed");
10865 } else if (num_readable > 0 && FD_ISSET(STDIN_FILENO, &rd)) {
10866 stdin_ready = true;
10870 restart(NULL, NULL);
10879 stdin_ready = false;
10880 bar_extra_update();
10887 shutdown_cleanup();