2 * Copyright (c) 2009-2012 Marco Peereboom <marco@peereboom.us>
3 * Copyright (c) 2009-2011 Ryan McBride <mcbride@countersiege.com>
4 * Copyright (c) 2009 Darrin Chandler <dwchandler@stilyagin.com>
5 * Copyright (c) 2009 Pierre-Yves Ritschard <pyr@spootnik.org>
6 * Copyright (c) 2010 Tuukka Kataja <stuge@xor.fi>
7 * Copyright (c) 2011 Jason L. Wright <jason@thought.net>
8 * Copyright (c) 2011-2014 Reginald Kennedy <rk@rejii.com>
9 * Copyright (c) 2011-2012 Lawrence Teo <lteo@lteo.net>
10 * Copyright (c) 2011-2012 Tiago Cunha <tcunha@gmx.com>
11 * Copyright (c) 2012-2013 David Hill <dhill@mindcry.org>
13 * Permission to use, copy, modify, and distribute this software for any
14 * purpose with or without fee is hereby granted, provided that the above
15 * copyright notice and this permission notice appear in all copies.
17 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
18 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
20 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
21 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
22 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
23 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 * Much code and ideas taken from dwm under the following license:
27 * MIT/X Consortium License
29 * 2006-2008 Anselm R Garbe <garbeam at gmail dot com>
30 * 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
31 * 2006-2007 Jukka Salmi <jukka at salmi dot ch>
32 * 2007 Premysl Hruby <dfenze at gmail dot com>
33 * 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
34 * 2007 Christof Musik <christof at sendfax dot de>
35 * 2007-2008 Enno Gottox Boland <gottox at s01 dot de>
36 * 2007-2008 Peter Hartlich <sgkkr at hartlich dot com>
37 * 2008 Martin Hurton <martin dot hurton at gmail dot com>
39 * Permission is hereby granted, free of charge, to any person obtaining a
40 * copy of this software and associated documentation files (the "Software"),
41 * to deal in the Software without restriction, including without limitation
42 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
43 * and/or sell copies of the Software, and to permit persons to whom the
44 * Software is furnished to do so, subject to the following conditions:
46 * The above copyright notice and this permission notice shall be included in
47 * all copies or substantial portions of the Software.
49 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
50 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
51 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
52 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
53 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
54 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
55 * DEALINGS IN THE SOFTWARE.
59 #include <sys/types.h>
63 #include <sys/queue.h>
64 #include <sys/param.h>
65 #include <sys/select.h>
66 #if defined(__linux__)
68 #elif defined(__OpenBSD__)
70 #elif defined(__FreeBSD__)
93 #include <X11/cursorfont.h>
94 #include <X11/extensions/Xrandr.h>
95 #include <X11/Xcursor/Xcursor.h>
96 #include <X11/Xft/Xft.h>
97 #include <X11/Xlib-xcb.h>
98 #include <xcb/xcb_atom.h>
99 #include <xcb/xcb_aux.h>
100 #include <xcb/xcb_event.h>
101 #include <xcb/xcb_icccm.h>
102 #include <xcb/xcb_keysyms.h>
103 #include <xcb/xtest.h>
104 #include <xcb/randr.h>
112 #ifdef SPECTRWM_BUILDSTR
113 static const char *buildstr = SPECTRWM_BUILDSTR;
115 static const char *buildstr = SPECTRWM_VERSION;
118 #if !defined(__CYGWIN__) /* cygwin chokes on randr stuff */
120 # error RandR versions less than 1.0 are not supported
123 # if RANDR_MAJOR >= 1
124 # if RANDR_MINOR >= 2
125 # define SWM_XRR_HAS_CRTC
128 #endif /* __CYGWIN__ */
130 #ifndef XCB_ICCCM_NUM_WM_HINTS_ELEMENTS
131 #define XCB_ICCCM_SIZE_HINT_P_MIN_SIZE XCB_SIZE_HINT_P_MIN_SIZE
132 #define XCB_ICCCM_SIZE_HINT_P_MAX_SIZE XCB_SIZE_HINT_P_MAX_SIZE
133 #define XCB_ICCCM_SIZE_HINT_P_RESIZE_INC XCB_SIZE_HINT_P_RESIZE_INC
134 #define XCB_ICCCM_WM_HINT_INPUT XCB_WM_HINT_INPUT
135 #define XCB_ICCCM_WM_HINT_X_URGENCY XCB_WM_HINT_X_URGENCY
136 #define XCB_ICCCM_WM_STATE_ICONIC XCB_WM_STATE_ICONIC
137 #define XCB_ICCCM_WM_STATE_WITHDRAWN XCB_WM_STATE_WITHDRAWN
138 #define XCB_ICCCM_WM_STATE_NORMAL XCB_WM_STATE_NORMAL
139 #define xcb_icccm_get_text_property_reply_t xcb_get_text_property_reply_t
140 #define xcb_icccm_get_text_property_reply_wipe xcb_get_text_property_reply_wipe
141 #define xcb_icccm_get_wm_class xcb_get_wm_class
142 #define xcb_icccm_get_wm_class_reply xcb_get_wm_class_reply
143 #define xcb_icccm_get_wm_class_reply_t xcb_get_wm_class_reply_t
144 #define xcb_icccm_get_wm_class_reply_wipe xcb_get_wm_class_reply_wipe
145 #define xcb_icccm_get_wm_hints xcb_get_wm_hints
146 #define xcb_icccm_wm_hints_get_urgency xcb_wm_hints_get_urgency
147 #define xcb_icccm_get_wm_hints_reply xcb_get_wm_hints_reply
148 #define xcb_icccm_get_wm_name xcb_get_wm_name
149 #define xcb_icccm_get_wm_name_reply xcb_get_wm_name_reply
150 #define xcb_icccm_get_wm_normal_hints xcb_get_wm_normal_hints
151 #define xcb_icccm_get_wm_normal_hints_reply xcb_get_wm_normal_hints_reply
152 #define xcb_icccm_get_wm_protocols xcb_get_wm_protocols
153 #define xcb_icccm_get_wm_protocols_reply xcb_get_wm_protocols_reply
154 #define xcb_icccm_get_wm_protocols_reply_t xcb_get_wm_protocols_reply_t
155 #define xcb_icccm_get_wm_protocols_reply_wipe xcb_get_wm_protocols_reply_wipe
156 #define xcb_icccm_get_wm_transient_for xcb_get_wm_transient_for
157 #define xcb_icccm_get_wm_transient_for_reply xcb_get_wm_transient_for_reply
158 #define xcb_icccm_wm_hints_t xcb_wm_hints_t
161 /*#define SWM_DEBUG*/
163 #define DPRINTF(x...) do { \
165 fprintf(stderr, x); \
167 #define DNPRINTF(n,x...) do { \
168 if (swm_debug & n) { \
169 fprintf(stderr, "%ld ", (long)(time(NULL) - time_started)); \
170 fprintf(stderr, x); \
173 #define SWM_D_MISC 0x0001
174 #define SWM_D_EVENT 0x0002
175 #define SWM_D_WS 0x0004
176 #define SWM_D_FOCUS 0x0008
177 #define SWM_D_MOVE 0x0010
178 #define SWM_D_STACK 0x0020
179 #define SWM_D_MOUSE 0x0040
180 #define SWM_D_PROP 0x0080
181 #define SWM_D_CLASS 0x0100
182 #define SWM_D_KEY 0x0200
183 #define SWM_D_QUIRK 0x0400
184 #define SWM_D_SPAWN 0x0800
185 #define SWM_D_EVENTQ 0x1000
186 #define SWM_D_CONF 0x2000
187 #define SWM_D_BAR 0x4000
188 #define SWM_D_INIT 0x8000
190 uint32_t swm_debug = 0
209 #define DPRINTF(x...)
210 #define DNPRINTF(n,x...)
213 #define SWM_EWMH_ACTION_COUNT_MAX (8)
214 #define EWMH_F_FULLSCREEN (0x001)
215 #define EWMH_F_ABOVE (0x002)
216 #define EWMH_F_HIDDEN (0x004)
217 #define EWMH_F_MAXIMIZED_VERT (0x008)
218 #define EWMH_F_MAXIMIZED_HORZ (0x010)
219 #define EWMH_F_SKIP_PAGER (0x020)
220 #define EWMH_F_SKIP_TASKBAR (0x040)
221 #define SWM_F_MANUAL (0x080)
223 #define EWMH_F_MAXIMIZED (EWMH_F_MAXIMIZED_VERT | EWMH_F_MAXIMIZED_HORZ)
225 /* convert 8-bit to 16-bit */
226 #define RGB_8_TO_16(col) (((col) << 8) + (col))
228 #define PIXEL_TO_XRENDERCOLOR(px, xrc) \
229 xrc.red = RGB_8_TO_16((px) >> 16 & 0xff); \
230 xrc.green = RGB_8_TO_16((px) >> 8 & 0xff); \
231 xrc.blue = RGB_8_TO_16((px) & 0xff); \
234 #define LENGTH(x) (int)(sizeof (x) / sizeof (x)[0])
235 #define MODKEY XCB_MOD_MASK_1
236 #define CLEANMASK(mask) ((mask) & ~(numlockmask | XCB_MOD_MASK_LOCK))
237 #define BUTTONMASK (XCB_EVENT_MASK_BUTTON_PRESS | \
238 XCB_EVENT_MASK_BUTTON_RELEASE)
239 #define MOUSEMASK (BUTTONMASK|XCB_EVENT_MASK_POINTER_MOTION)
240 #define SWM_PROPLEN (16)
241 #define SWM_FUNCNAME_LEN (32)
242 #define SWM_KEYS_LEN (255)
243 #define SWM_QUIRK_LEN (64)
244 #define X(r) ((r)->g.x)
245 #define Y(r) ((r)->g.y)
246 #define WIDTH(r) ((r)->g.w)
247 #define HEIGHT(r) ((r)->g.h)
248 #define BORDER(w) ((w)->bordered ? border_width : 0)
249 #define MAX_X(r) ((r)->g.x + (r)->g.w)
250 #define MAX_Y(r) ((r)->g.y + (r)->g.h)
251 #define SH_MIN(w) ((w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE)
252 #define SH_MIN_W(w) ((w)->sh.min_width)
253 #define SH_MIN_H(w) ((w)->sh.min_height)
254 #define SH_MAX(w) ((w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MAX_SIZE)
255 #define SH_MAX_W(w) ((w)->sh.max_width)
256 #define SH_MAX_H(w) ((w)->sh.max_height)
257 #define SH_INC(w) ((w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_RESIZE_INC)
258 #define SH_INC_W(w) ((w)->sh.width_inc)
259 #define SH_INC_H(w) ((w)->sh.height_inc)
260 #define SWM_MAX_FONT_STEPS (3)
261 #define WINID(w) ((w) ? (w)->id : XCB_WINDOW_NONE)
262 #define WS_FOCUSED(ws) ((ws)->r && (ws)->r->s->r_focus == (ws)->r)
263 #define YESNO(x) ((x) ? "yes" : "no")
264 #define ICONIC(w) ((w)->ewmh_flags & EWMH_F_HIDDEN)
265 #define ABOVE(w) ((w)->ewmh_flags & EWMH_F_ABOVE)
266 #define FULLSCREEN(w) ((w)->ewmh_flags & EWMH_F_FULLSCREEN)
267 #define MAXIMIZED_VERT(w) ((w)->ewmh_flags & EWMH_F_MAXIMIZED_VERT)
268 #define MAXIMIZED_HORZ(w) ((w)->ewmh_flags & EWMH_F_MAXIMIZED_HORZ)
269 #define MAXIMIZED(w) (MAXIMIZED_VERT(w) || MAXIMIZED_HORZ(w))
270 #define MANUAL(w) ((w)->ewmh_flags & SWM_F_MANUAL)
271 #define TRANS(w) ((w)->transient != XCB_WINDOW_NONE)
272 #define FLOATING(w) (ABOVE(w) || TRANS(w) || FULLSCREEN(w) || \
275 /* Constrain Window flags */
276 #define SWM_CW_RESIZABLE (0x01)
277 #define SWM_CW_SOFTBOUNDARY (0x02)
278 #define SWM_CW_HARDBOUNDARY (0x04)
279 #define SWM_CW_RIGHT (0x10)
280 #define SWM_CW_LEFT (0x20)
281 #define SWM_CW_BOTTOM (0x40)
282 #define SWM_CW_TOP (0x80)
283 #define SWM_CW_ALLSIDES (0xf0)
285 #define SWM_FOCUS_DEFAULT (0)
286 #define SWM_FOCUS_FOLLOW (1)
287 #define SWM_FOCUS_MANUAL (2)
289 #define SWM_CK_NONE (0)
290 #define SWM_CK_ALL (0xf)
291 #define SWM_CK_FOCUS (0x1)
292 #define SWM_CK_POINTER (0x2)
293 #define SWM_CK_FALLBACK (0x4)
294 #define SWM_CK_REGION (0x8)
296 #define SWM_G_ALL (0xf)
297 #define SWM_G_SIZE (0x1)
298 #define SWM_G_POS (0x2)
300 #define SWM_CONF_DEFAULT (0)
301 #define SWM_CONF_KEYMAPPING (1)
304 #define SWM_LIB "/usr/local/lib/libswmhack.so"
311 xcb_atom_t a_net_wm_check;
312 xcb_atom_t a_net_supported;
313 xcb_atom_t a_takefocus;
314 xcb_atom_t a_utf8_string;
316 volatile sig_atomic_t running = 1;
317 volatile sig_atomic_t restart_wm = 0;
318 xcb_timestamp_t last_event_time = 0;
322 unsigned int numlockmask = 0;
325 xcb_connection_t *conn;
326 xcb_key_symbols_t *syms;
328 int boundary_width = 50;
329 bool cycle_empty = false;
330 bool cycle_visible = false;
332 int font_adjusted = 0;
333 unsigned int mod_key = MODKEY;
334 bool warp_pointer = false;
337 struct swm_region *search_r;
338 int select_list_pipe[2];
339 int select_resp_pipe[2];
341 volatile sig_atomic_t search_resp;
342 int search_resp_action;
344 struct search_window {
345 TAILQ_ENTRY(search_window) entry;
349 xcb_window_t indicator;
351 TAILQ_HEAD(search_winlist, search_window);
352 struct search_winlist search_wl;
357 SWM_SEARCH_UNICONIFY,
358 SWM_SEARCH_NAME_WORKSPACE,
359 SWM_SEARCH_SEARCH_WORKSPACE,
360 SWM_SEARCH_SEARCH_WINDOW
363 #define SWM_STACK_TOP (0)
364 #define SWM_STACK_BOTTOM (1)
365 #define SWM_STACK_ABOVE (2)
366 #define SWM_STACK_BELOW (3)
369 double dialog_ratio = 0.6;
371 #define SWM_BAR_MAX (256)
372 #define SWM_BAR_JUSTIFY_LEFT (0)
373 #define SWM_BAR_JUSTIFY_CENTER (1)
374 #define SWM_BAR_JUSTIFY_RIGHT (2)
375 #define SWM_BAR_OFFSET (4)
376 #define SWM_BAR_FONTS "-*-terminus-medium-*-*-*-12-*-*-*-*-*-*-*," \
377 "-*-profont-*-*-*-*-12-*-*-*-*-*-*-*," \
378 "-*-times-medium-r-*-*-12-*-*-*-*-*-*-*," \
379 "-misc-fixed-medium-r-*-*-12-*-*-*-*-*-*-*," \
380 "-*-*-*-r-*-*-*-*-*-*-*-*-*-*"
382 #ifdef X_HAVE_UTF8_STRING
383 #define DRAWSTRING(x...) Xutf8DrawString(x)
385 #define DRAWSTRING(x...) XmbDrawString(x)
388 char *bar_argv[] = { NULL, NULL };
390 char bar_ext[SWM_BAR_MAX];
391 char bar_ext_buf[SWM_BAR_MAX];
392 char bar_vertext[SWM_BAR_MAX];
393 bool bar_version = false;
394 bool bar_enabled = true;
395 int bar_border_width = 1;
396 bool bar_at_bottom = false;
397 bool bar_extra = false;
399 int bar_justify = SWM_BAR_JUSTIFY_LEFT;
400 char *bar_format = NULL;
401 bool stack_enabled = true;
402 bool clock_enabled = true;
403 bool iconic_enabled = false;
404 bool urgent_enabled = false;
405 bool urgent_collapse = false;
406 char *clock_format = NULL;
407 bool window_class_enabled = false;
408 bool window_instance_enabled = false;
409 bool window_name_enabled = false;
410 int focus_mode = SWM_FOCUS_DEFAULT;
411 int focus_close = SWM_STACK_BELOW;
412 bool focus_close_wrap = true;
413 int focus_default = SWM_STACK_TOP;
414 int spawn_position = SWM_STACK_TOP;
415 bool disable_border = false;
416 int border_width = 1;
417 int region_padding = 0;
419 bool java_workaround = true;
420 bool verbose_layout = false;
426 XFontSetExtents *bar_fs_extents;
428 bool bar_font_legacy = true;
430 XftColor bar_font_color;
431 XftColor search_font_color;
433 char *startup_exception;
434 unsigned int nr_exceptions = 0;
436 /* layout manager data */
437 struct swm_geometry {
450 struct swm_geometry g;
453 /* virtual "screens" */
455 TAILQ_ENTRY(swm_region) entry;
457 struct swm_geometry g;
458 struct workspace *ws; /* current workspace on this region */
459 struct workspace *ws_prior; /* prior workspace on this region */
460 struct swm_screen *s; /* screen idx */
463 TAILQ_HEAD(swm_region_list, swm_region);
466 TAILQ_ENTRY(ws_win) entry;
467 TAILQ_ENTRY(ws_win) stack_entry;
469 xcb_window_t transient;
470 struct ws_win *focus_child; /* focus on child transient */
471 struct swm_geometry g; /* current geometry */
472 struct swm_geometry g_float; /* region coordinates */
473 bool g_floatvalid; /* g_float geometry validity */
477 int font_size_boundary[SWM_MAX_FONT_STEPS];
484 struct workspace *ws; /* always valid */
485 struct swm_screen *s; /* always valid, never changes */
487 xcb_icccm_get_wm_class_reply_t ch;
488 xcb_icccm_wm_hints_t hints;
490 TAILQ_HEAD(ws_win_list, ws_win);
491 TAILQ_HEAD(ws_win_stack, ws_win);
495 TAILQ_ENTRY(pid_e) entry;
499 TAILQ_HEAD(pid_list, pid_e);
500 struct pid_list pidlist = TAILQ_HEAD_INITIALIZER(pidlist);
502 /* layout handlers */
504 void vertical_config(struct workspace *, int);
505 void vertical_stack(struct workspace *, struct swm_geometry *);
506 void horizontal_config(struct workspace *, int);
507 void horizontal_stack(struct workspace *, struct swm_geometry *);
508 void max_stack(struct workspace *, struct swm_geometry *);
509 void plain_stacker(struct workspace *);
510 void fancy_stacker(struct workspace *);
513 void (*l_stack)(struct workspace *, struct swm_geometry *);
514 void (*l_config)(struct workspace *, int);
516 #define SWM_L_FOCUSPREV (1<<0)
517 #define SWM_L_MAPONFOCUS (1<<1)
518 void (*l_string)(struct workspace *);
520 /* stack, configure */
521 { vertical_stack, vertical_config, 0, plain_stacker },
522 { horizontal_stack, horizontal_config, 0, plain_stacker },
524 SWM_L_MAPONFOCUS | SWM_L_FOCUSPREV, plain_stacker },
525 { NULL, NULL, 0, NULL },
528 /* position of max_stack mode in the layouts array, index into layouts! */
529 #define SWM_V_STACK (0)
530 #define SWM_H_STACK (1)
531 #define SWM_MAX_STACK (2)
533 #define SWM_H_SLICE (32)
534 #define SWM_V_SLICE (32)
536 /* define work spaces */
538 int idx; /* workspace index */
539 char *name; /* workspace name */
540 bool always_raise; /* raise windows on focus */
541 bool bar_enabled; /* bar visibility */
542 struct layout *cur_layout; /* current layout handlers */
543 struct ws_win *focus; /* may be NULL */
544 struct ws_win *focus_prev; /* may be NULL */
545 struct ws_win *focus_pending; /* may be NULL */
546 struct swm_region *r; /* may be NULL */
547 struct swm_region *old_r; /* may be NULL */
548 struct ws_win_list winlist; /* list of windows in ws */
549 struct ws_win_list unmanagedlist; /* list of dead windows in ws */
550 struct ws_win_stack stack; /* stacking order */
551 int state; /* mapping state */
552 char stacker[10]; /* display stacker and layout */
556 int horizontal_msize;
558 int horizontal_stacks;
559 bool horizontal_flip;
569 SWM_WS_STATE_MAPPING,
575 SWM_S_COLOR_BAR_BORDER,
576 SWM_S_COLOR_BAR_BORDER_UNFOCUS,
577 SWM_S_COLOR_BAR_FONT,
579 SWM_S_COLOR_FOCUS_MAXIMIZED,
581 SWM_S_COLOR_UNFOCUS_MAXIMIZED,
585 /* physical screen mapping */
586 #define SWM_WS_MAX (22) /* hard limit */
587 int workspace_limit = 10; /* soft limit */
590 int idx; /* screen index */
591 struct swm_region_list rl; /* list of regions on this screen */
592 struct swm_region_list orl; /* list of old regions */
594 struct workspace ws[SWM_WS_MAX];
595 struct swm_region *r_focus;
602 } c[SWM_S_COLOR_MAX];
604 xcb_gcontext_t bar_gc;
607 struct swm_screen *screens;
609 /* args to functions */
612 #define SWM_ARG_ID_FOCUSNEXT (0)
613 #define SWM_ARG_ID_FOCUSPREV (1)
614 #define SWM_ARG_ID_FOCUSMAIN (2)
615 #define SWM_ARG_ID_FOCUSURGENT (3)
616 #define SWM_ARG_ID_SWAPNEXT (10)
617 #define SWM_ARG_ID_SWAPPREV (11)
618 #define SWM_ARG_ID_SWAPMAIN (12)
619 #define SWM_ARG_ID_MOVELAST (13)
620 #define SWM_ARG_ID_MASTERSHRINK (20)
621 #define SWM_ARG_ID_MASTERGROW (21)
622 #define SWM_ARG_ID_MASTERADD (22)
623 #define SWM_ARG_ID_MASTERDEL (23)
624 #define SWM_ARG_ID_FLIPLAYOUT (24)
625 #define SWM_ARG_ID_STACKRESET (30)
626 #define SWM_ARG_ID_STACKINIT (31)
627 #define SWM_ARG_ID_CYCLEWS_UP (40)
628 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
629 #define SWM_ARG_ID_CYCLERG_UP (42)
630 #define SWM_ARG_ID_CYCLERG_DOWN (43)
631 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
632 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
633 #define SWM_ARG_ID_CYCLEWS_MOVE_UP (46)
634 #define SWM_ARG_ID_CYCLEWS_MOVE_DOWN (47)
635 #define SWM_ARG_ID_STACKINC (50)
636 #define SWM_ARG_ID_STACKDEC (51)
637 #define SWM_ARG_ID_SS_ALL (60)
638 #define SWM_ARG_ID_SS_WINDOW (61)
639 #define SWM_ARG_ID_DONTCENTER (70)
640 #define SWM_ARG_ID_CENTER (71)
641 #define SWM_ARG_ID_KILLWINDOW (80)
642 #define SWM_ARG_ID_DELETEWINDOW (81)
643 #define SWM_ARG_ID_WIDTHGROW (90)
644 #define SWM_ARG_ID_WIDTHSHRINK (91)
645 #define SWM_ARG_ID_HEIGHTGROW (92)
646 #define SWM_ARG_ID_HEIGHTSHRINK (93)
647 #define SWM_ARG_ID_MOVEUP (100)
648 #define SWM_ARG_ID_MOVEDOWN (101)
649 #define SWM_ARG_ID_MOVELEFT (102)
650 #define SWM_ARG_ID_MOVERIGHT (103)
651 #define SWM_ARG_ID_RAISE (105)
652 #define SWM_ARG_ID_LOWER (106)
653 #define SWM_ARG_ID_BAR_TOGGLE (110)
654 #define SWM_ARG_ID_BAR_TOGGLE_WS (111)
660 TAILQ_ENTRY(quirk) entry;
661 char *class; /* WM_CLASS:class */
662 char *instance; /* WM_CLASS:instance */
663 char *name; /* WM_NAME */
665 regex_t regex_instance;
668 int ws; /* Initial workspace. */
669 #define SWM_Q_FLOAT (1<<0) /* float this window */
670 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
671 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
672 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
673 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
674 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
675 #define SWM_Q_NOFOCUSONMAP (1<<6) /* Don't focus on window when mapped. */
676 #define SWM_Q_FOCUSONMAP_SINGLE (1<<7) /* Only focus if single win of type. */
677 #define SWM_Q_OBEYAPPFOCUSREQ (1<<8) /* Focus when applications ask. */
678 #define SWM_Q_IGNOREPID (1<<9) /* Ignore PID when determining ws. */
679 #define SWM_Q_IGNORESPAWNWS (1<<10) /* Ignore _SWM_WS when managing win. */
681 TAILQ_HEAD(quirk_list, quirk);
682 struct quirk_list quirks = TAILQ_HEAD_INITIALIZER(quirks);
685 * Supported EWMH hints should be added to
686 * both the enum and the ewmh array
692 _NET_CURRENT_DESKTOP,
693 _NET_DESKTOP_GEOMETRY,
695 _NET_DESKTOP_VIEWPORT,
696 _NET_MOVERESIZE_WINDOW,
697 _NET_NUMBER_OF_DESKTOPS,
699 _NET_WM_ACTION_ABOVE,
700 _NET_WM_ACTION_CLOSE,
701 _NET_WM_ACTION_FULLSCREEN,
703 _NET_WM_ACTION_RESIZE,
704 _NET_WM_ALLOWED_ACTIONS,
706 _NET_WM_FULL_PLACEMENT,
710 _NET_WM_STATE_FULLSCREEN,
711 _NET_WM_STATE_HIDDEN,
712 _NET_WM_STATE_MAXIMIZED_VERT,
713 _NET_WM_STATE_MAXIMIZED_HORZ,
714 _NET_WM_STATE_SKIP_PAGER,
715 _NET_WM_STATE_SKIP_TASKBAR,
717 _NET_WM_WINDOW_TYPE_DIALOG,
718 _NET_WM_WINDOW_TYPE_DOCK,
719 _NET_WM_WINDOW_TYPE_NORMAL,
720 _NET_WM_WINDOW_TYPE_SPLASH,
721 _NET_WM_WINDOW_TYPE_TOOLBAR,
722 _NET_WM_WINDOW_TYPE_UTILITY,
723 _SWM_WM_STATE_MANUAL,
730 } ewmh[SWM_EWMH_HINT_MAX] = {
731 /* must be in same order as in the enum */
732 {"_NET_ACTIVE_WINDOW", XCB_ATOM_NONE},
733 {"_NET_CLIENT_LIST", XCB_ATOM_NONE},
734 {"_NET_CLOSE_WINDOW", XCB_ATOM_NONE},
735 {"_NET_CURRENT_DESKTOP", XCB_ATOM_NONE},
736 {"_NET_DESKTOP_GEOMETRY", XCB_ATOM_NONE},
737 {"_NET_DESKTOP_NAMES", XCB_ATOM_NONE},
738 {"_NET_DESKTOP_VIEWPORT", XCB_ATOM_NONE},
739 {"_NET_MOVERESIZE_WINDOW", XCB_ATOM_NONE},
740 {"_NET_NUMBER_OF_DESKTOPS", XCB_ATOM_NONE},
741 {"_NET_RESTACK_WINDOW", XCB_ATOM_NONE},
742 {"_NET_WM_ACTION_ABOVE", XCB_ATOM_NONE},
743 {"_NET_WM_ACTION_CLOSE", XCB_ATOM_NONE},
744 {"_NET_WM_ACTION_FULLSCREEN", XCB_ATOM_NONE},
745 {"_NET_WM_ACTION_MOVE", XCB_ATOM_NONE},
746 {"_NET_WM_ACTION_RESIZE", XCB_ATOM_NONE},
747 {"_NET_WM_ALLOWED_ACTIONS", XCB_ATOM_NONE},
748 {"_NET_WM_DESKTOP", XCB_ATOM_NONE},
749 {"_NET_WM_FULL_PLACEMENT", XCB_ATOM_NONE},
750 {"_NET_WM_NAME", XCB_ATOM_NONE},
751 {"_NET_WM_STATE", XCB_ATOM_NONE},
752 {"_NET_WM_STATE_ABOVE", XCB_ATOM_NONE},
753 {"_NET_WM_STATE_FULLSCREEN", XCB_ATOM_NONE},
754 {"_NET_WM_STATE_HIDDEN", XCB_ATOM_NONE},
755 {"_NET_WM_STATE_MAXIMIZED_VERT", XCB_ATOM_NONE},
756 {"_NET_WM_STATE_MAXIMIZED_HORZ", XCB_ATOM_NONE},
757 {"_NET_WM_STATE_SKIP_PAGER", XCB_ATOM_NONE},
758 {"_NET_WM_STATE_SKIP_TASKBAR", XCB_ATOM_NONE},
759 {"_NET_WM_WINDOW_TYPE", XCB_ATOM_NONE},
760 {"_NET_WM_WINDOW_TYPE_DIALOG", XCB_ATOM_NONE},
761 {"_NET_WM_WINDOW_TYPE_DOCK", XCB_ATOM_NONE},
762 {"_NET_WM_WINDOW_TYPE_NORMAL", XCB_ATOM_NONE},
763 {"_NET_WM_WINDOW_TYPE_SPLASH", XCB_ATOM_NONE},
764 {"_NET_WM_WINDOW_TYPE_TOOLBAR", XCB_ATOM_NONE},
765 {"_NET_WM_WINDOW_TYPE_UTILITY", XCB_ATOM_NONE},
766 {"_SWM_WM_STATE_MANUAL", XCB_ATOM_NONE},
769 /* EWMH source type */
771 EWMH_SOURCE_TYPE_NONE = 0,
772 EWMH_SOURCE_TYPE_NORMAL = 1,
773 EWMH_SOURCE_TYPE_OTHER = 2,
780 XC_BOTTOM_LEFT_CORNER,
781 XC_BOTTOM_RIGHT_CORNER,
789 char *name; /* Name used by Xcursor .*/
790 uint8_t cf_char; /* cursorfont index. */
792 } cursors[XC_MAX] = {
793 {"fleur", XC_fleur, XCB_CURSOR_NONE},
794 {"left_ptr", XC_left_ptr, XCB_CURSOR_NONE},
795 {"bottom_left_corner", XC_bottom_left_corner, XCB_CURSOR_NONE},
796 {"bottom_right_corner", XC_bottom_right_corner, XCB_CURSOR_NONE},
797 {"sizing", XC_sizing, XCB_CURSOR_NONE},
798 {"top_left_corner", XC_top_left_corner, XCB_CURSOR_NONE},
799 {"top_right_corner", XC_top_right_corner, XCB_CURSOR_NONE},
802 #define SWM_SPAWN_OPTIONAL 0x1
806 TAILQ_ENTRY(spawn_prog) entry;
812 TAILQ_HEAD(spawn_list, spawn_prog);
813 struct spawn_list spawns = TAILQ_HEAD_INITIALIZER(spawns);
815 /* user/key callable function IDs */
931 KF_DUMPWINS, /* MUST BE LAST */
939 enum keyfuncid funcid;
942 RB_HEAD(key_tree, key);
944 /* function prototypes */
945 void adjust_font(struct ws_win *);
946 char *argsep(char **);
947 void bar_cleanup(struct swm_region *);
948 void bar_extra_setup(void);
949 void bar_extra_stop(void);
950 int bar_extra_update(void);
951 void bar_fmt(const char *, char *, struct swm_region *, size_t);
952 void bar_fmt_expand(char *, size_t);
954 void bar_print(struct swm_region *, const char *);
955 void bar_print_legacy(struct swm_region *, const char *);
956 void bar_replace(char *, char *, struct swm_region *, size_t);
957 void bar_replace_pad(char *, int *, size_t);
958 char *bar_replace_seq(char *, char *, struct swm_region *, size_t *, size_t);
959 void bar_setup(struct swm_region *);
960 void bar_toggle(struct swm_region *, union arg *);
961 void bar_urgent(char *, size_t);
962 void bar_window_class(char *, size_t, struct swm_region *);
963 void bar_window_class_instance(char *, size_t, struct swm_region *);
964 void bar_window_float(char *, size_t, struct swm_region *);
965 void bar_window_instance(char *, size_t, struct swm_region *);
966 void bar_window_name(char *, size_t, struct swm_region *);
967 void bar_window_state(char *, size_t, struct swm_region *);
968 void bar_workspace_name(char *, size_t, struct swm_region *);
969 void buttonpress(xcb_button_press_event_t *);
970 void center_pointer(struct swm_region *);
971 void check_conn(void);
972 void clear_keys(void);
973 int clear_maximized(struct workspace *);
974 void clientmessage(xcb_client_message_event_t *);
975 void client_msg(struct ws_win *, xcb_atom_t, xcb_timestamp_t);
976 int conf_load(const char *, int);
977 void configurenotify(xcb_configure_notify_event_t *);
978 void configurerequest(xcb_configure_request_event_t *);
979 void config_win(struct ws_win *, xcb_configure_request_event_t *);
980 void constrain_window(struct ws_win *, struct swm_geometry *, int *);
981 int count_win(struct workspace *, bool);
982 void cursors_cleanup(void);
983 void cursors_load(void);
984 void custom_region(const char *);
985 void cyclerg(struct swm_region *, union arg *);
986 void cyclews(struct swm_region *, union arg *);
987 void cycle_layout(struct swm_region *, union arg *);
988 void destroynotify(xcb_destroy_notify_event_t *);
989 void dumpwins(struct swm_region *, union arg *);
991 void enternotify(xcb_enter_notify_event_t *);
992 void event_drain(uint8_t);
993 void event_error(xcb_generic_error_t *);
994 void event_handle(xcb_generic_event_t *);
995 void ewmh_apply_flags(struct ws_win *, uint32_t);
996 void ewmh_autoquirk(struct ws_win *);
997 void ewmh_get_desktop_names(void);
998 void ewmh_get_wm_state(struct ws_win *);
999 void ewmh_update_actions(struct ws_win *);
1000 void ewmh_update_client_list(void);
1001 void ewmh_update_current_desktop(void);
1002 void ewmh_update_desktop_names(void);
1003 void ewmh_update_desktops(void);
1004 void ewmh_change_wm_state(struct ws_win *, xcb_atom_t, long);
1005 void ewmh_update_wm_state(struct ws_win *);
1006 char *expand_tilde(const char *);
1007 void expose(xcb_expose_event_t *);
1008 void fake_keypress(struct ws_win *, xcb_keysym_t, uint16_t);
1009 struct pid_e *find_pid(pid_t);
1010 struct ws_win *find_unmanaged_window(xcb_window_t);
1011 struct ws_win *find_window(xcb_window_t);
1012 void floating_toggle(struct swm_region *, union arg *);
1013 void focus(struct swm_region *, union arg *);
1015 void focusin(xcb_focus_in_event_t *);
1016 void focusout(xcb_focus_out_event_t *);
1018 void focus_flush(void);
1019 void focus_region(struct swm_region *);
1020 void focusrg(struct swm_region *, union arg *);
1021 void focus_win(struct ws_win *);
1022 void fontset_init(void);
1023 void free_window(struct ws_win *);
1024 xcb_atom_t get_atom_from_string(const char *);
1026 char *get_atom_name(xcb_atom_t);
1028 struct ws_win *get_focus_magic(struct ws_win *);
1029 struct ws_win *get_focus_prev(struct ws_win *);
1031 char *get_notify_detail_label(uint8_t);
1032 char *get_notify_mode_label(uint8_t);
1034 struct ws_win *get_pointer_win(xcb_window_t);
1035 struct ws_win *get_region_focus(struct swm_region *);
1036 int get_region_index(struct swm_region *);
1037 xcb_screen_t *get_screen(int);
1038 int get_screen_count(void);
1040 char *get_source_type_label(uint32_t);
1041 char *get_stack_mode_name(uint8_t);
1043 int32_t get_swm_ws(xcb_window_t);
1044 char *get_win_name(xcb_window_t);
1045 uint8_t get_win_state(xcb_window_t);
1046 void get_wm_protocols(struct ws_win *);
1047 int get_ws_idx(struct ws_win *);
1048 void grabbuttons(struct ws_win *);
1049 void grabkeys(void);
1050 void grab_windows(void);
1051 void iconify(struct swm_region *, union arg *);
1052 bool isxlfd(char *);
1053 void keypress(xcb_key_press_event_t *);
1054 int key_cmp(struct key *, struct key *);
1055 void key_insert(unsigned int, KeySym, enum keyfuncid, const char *);
1056 struct key *key_lookup(unsigned int, KeySym);
1057 void key_remove(struct key *);
1058 void key_replace(struct key *, unsigned int, KeySym, enum keyfuncid,
1060 void kill_bar_extra_atexit(void);
1061 void kill_refs(struct ws_win *);
1063 void leavenotify(xcb_leave_notify_event_t *);
1065 void load_float_geom(struct ws_win *);
1066 struct ws_win *manage_window(xcb_window_t, int, bool);
1067 void map_window(struct ws_win *);
1068 void mapnotify(xcb_map_notify_event_t *);
1069 void mappingnotify(xcb_mapping_notify_event_t *);
1070 void maprequest(xcb_map_request_event_t *);
1071 void maximize_toggle(struct swm_region *, union arg *);
1072 void motionnotify(xcb_motion_notify_event_t *);
1073 void move(struct ws_win *, union arg *);
1074 void move_step(struct swm_region *, union arg *);
1075 uint32_t name_to_pixel(int, const char *);
1076 void name_workspace(struct swm_region *, union arg *);
1077 void new_region(struct swm_screen *, int, int, int, int);
1078 int parsekeys(const char *, unsigned int, unsigned int *, KeySym *);
1079 int parsequirks(const char *, uint32_t *, int *);
1080 int parse_rgb(const char *, uint16_t *, uint16_t *, uint16_t *);
1081 void pressbutton(struct swm_region *, union arg *);
1082 void priorws(struct swm_region *, union arg *);
1084 void print_win_geom(xcb_window_t);
1086 void propertynotify(xcb_property_notify_event_t *);
1087 void quirk_free(struct quirk *);
1088 void quirk_insert(const char *, const char *, const char *, uint32_t, int);
1089 void quirk_remove(struct quirk *);
1090 void quirk_replace(struct quirk *, const char *, const char *, const char *,
1092 void quit(struct swm_region *, union arg *);
1093 void raise_toggle(struct swm_region *, union arg *);
1094 void raise_window(struct ws_win *);
1095 void region_containment(struct ws_win *, struct swm_region *, int);
1096 struct swm_region *region_under(struct swm_screen *, int, int);
1097 void regionize(struct ws_win *, int, int);
1098 void resize(struct ws_win *, union arg *);
1099 void resize_step(struct swm_region *, union arg *);
1100 void restart(struct swm_region *, union arg *);
1101 struct swm_region *root_to_region(xcb_window_t, int);
1102 void screenchange(xcb_randr_screen_change_notify_event_t *);
1103 void scan_randr(int);
1104 void search_do_resp(void);
1105 void search_resp_name_workspace(const char *, size_t);
1106 void search_resp_search_window(const char *);
1107 void search_resp_search_workspace(const char *);
1108 void search_resp_uniconify(const char *, size_t);
1109 void search_win(struct swm_region *, union arg *);
1110 void search_win_cleanup(void);
1111 void search_workspace(struct swm_region *, union arg *);
1112 void send_to_rg(struct swm_region *, union arg *);
1113 void send_to_ws(struct swm_region *, union arg *);
1114 void set_region(struct swm_region *);
1115 int setautorun(const char *, const char *, int);
1116 int setconfbinding(const char *, const char *, int);
1117 int setconfcolor(const char *, const char *, int);
1118 int setconfmodkey(const char *, const char *, int);
1119 int setconfquirk(const char *, const char *, int);
1120 int setconfregion(const char *, const char *, int);
1121 int setconfspawn(const char *, const char *, int);
1122 int setconfvalue(const char *, const char *, int);
1123 void setkeybinding(unsigned int, KeySym, enum keyfuncid, const char *);
1124 int setkeymapping(const char *, const char *, int);
1125 int setlayout(const char *, const char *, int);
1126 void setquirk(const char *, const char *, const char *, uint32_t, int);
1127 void setscreencolor(const char *, int, int);
1128 void setspawn(const char *, const char *, int);
1129 void setup_ewmh(void);
1130 void setup_globals(void);
1131 void setup_keys(void);
1132 void setup_quirks(void);
1133 void setup_screens(void);
1134 void setup_spawn(void);
1135 void set_child_transient(struct ws_win *, xcb_window_t *);
1136 void set_win_state(struct ws_win *, uint8_t);
1137 void shutdown_cleanup(void);
1139 void socket_setnonblock(int);
1140 void sort_windows(struct ws_win_list *);
1141 void spawn(int, union arg *, bool);
1142 void spawn_custom(struct swm_region *, union arg *, const char *);
1143 int spawn_expand(struct swm_region *, union arg *, const char *, char ***);
1144 void spawn_insert(const char *, const char *, int);
1145 struct spawn_prog *spawn_find(const char *);
1146 void spawn_remove(struct spawn_prog *);
1147 void spawn_replace(struct spawn_prog *, const char *, const char *, int);
1148 void spawn_select(struct swm_region *, union arg *, const char *, int *);
1149 void stack_config(struct swm_region *, union arg *);
1150 void stack_master(struct workspace *, struct swm_geometry *, int, bool);
1151 void store_float_geom(struct ws_win *);
1152 char *strdupsafe(const char *);
1153 void swapwin(struct swm_region *, union arg *);
1154 void switchws(struct swm_region *, union arg *);
1155 void teardown_ewmh(void);
1156 void unescape_selector(char *);
1157 void unfocus_win(struct ws_win *);
1158 void uniconify(struct swm_region *, union arg *);
1159 void unmanage_window(struct ws_win *);
1160 void unmapnotify(xcb_unmap_notify_event_t *);
1161 void unmap_all(void);
1162 void unmap_window(struct ws_win *);
1163 void updatenumlockmask(void);
1164 void update_floater(struct ws_win *);
1165 void update_modkey(unsigned int);
1166 void update_win_stacking(struct ws_win *);
1167 void update_window(struct ws_win *);
1168 void update_window_color(struct ws_win *);
1169 void update_wm_state(struct ws_win *win);
1170 void validate_spawns(void);
1171 int validate_win(struct ws_win *);
1172 int validate_ws(struct workspace *);
1173 void version(struct swm_region *, union arg *);
1174 void win_to_ws(struct ws_win *, int, bool);
1175 pid_t window_get_pid(xcb_window_t);
1176 void wkill(struct swm_region *, union arg *);
1177 void update_ws_stack(struct workspace *);
1178 void xft_init(struct swm_region *);
1179 void _add_startup_exception(const char *, va_list);
1180 void add_startup_exception(const char *, ...);
1182 RB_PROTOTYPE(key_tree, key, entry, key_cmp);
1183 RB_GENERATE(key_tree, key, entry, key_cmp);
1184 struct key_tree keys;
1189 xcb_font_t cf = XCB_NONE;
1192 for (i = 0; i < LENGTH(cursors); ++i) {
1193 /* try to load Xcursor first. */
1194 cursors[i].cid = XcursorLibraryLoadCursor(display,
1197 /* fallback to cursorfont. */
1198 if (cursors[i].cid == XCB_CURSOR_NONE) {
1199 if (cf == XCB_NONE) {
1200 cf = xcb_generate_id(conn);
1201 xcb_open_font(conn, cf, strlen("cursor"),
1205 cursors[i].cid = xcb_generate_id(conn);
1206 xcb_create_glyph_cursor(conn, cursors[i].cid, cf, cf,
1207 cursors[i].cf_char, cursors[i].cf_char + 1, 0, 0, 0,
1208 0xffff, 0xffff, 0xffff);
1214 xcb_close_font(conn, cf);
1218 cursors_cleanup(void)
1221 for (i = 0; i < LENGTH(cursors); ++i)
1222 xcb_free_cursor(conn, cursors[i].cid);
1226 expand_tilde(const char *s)
1228 struct passwd *ppwd;
1236 errx(1, "expand_tilde: NULL string.");
1239 result = strdup(sc);
1245 if ((max = sysconf(_SC_LOGIN_NAME_MAX)) == -1)
1246 errx(1, "expand_tilde: sysconf");
1248 if ((user = calloc(1, max + 1)) == NULL)
1249 errx(1, "expand_tilde: calloc");
1251 for (i = 0; s[i] != '/' && s[i] != '\0'; ++i)
1256 ppwd = strlen(user) == 0 ? getpwuid(getuid()) : getpwnam(user);
1260 result = strdup(sc);
1262 if (asprintf(&result, "%s%s", ppwd->pw_dir, s) == -1)
1266 errx(1, "expand_tilde: failed to allocate memory.");
1272 parse_rgb(const char *rgb, uint16_t *rr, uint16_t *gg, uint16_t *bb)
1274 unsigned int tmpr, tmpg, tmpb;
1276 if (sscanf(rgb, "rgb:%x/%x/%x", &tmpr, &tmpg, &tmpb) != 3)
1279 *rr = RGB_8_TO_16(tmpr);
1280 *gg = RGB_8_TO_16(tmpg);
1281 *bb = RGB_8_TO_16(tmpb);
1287 get_screen(int screen)
1289 const xcb_setup_t *r;
1290 xcb_screen_iterator_t iter;
1292 if ((r = xcb_get_setup(conn)) == NULL) {
1293 DNPRINTF(SWM_D_MISC, "get_screen: xcb_get_setup\n");
1297 iter = xcb_setup_roots_iterator(r);
1298 for (; iter.rem; --screen, xcb_screen_next(&iter))
1306 get_screen_count(void)
1308 const xcb_setup_t *r;
1310 if ((r = xcb_get_setup(conn)) == NULL) {
1311 DNPRINTF(SWM_D_MISC, "get_screen_count: xcb_get_setup\n");
1315 return xcb_setup_roots_length(r);
1319 get_region_index(struct swm_region *r)
1321 struct swm_region *rr;
1327 TAILQ_FOREACH(rr, &r->s->rl, entry) {
1342 if (focus_mode == SWM_FOCUS_DEFAULT)
1343 event_drain(XCB_ENTER_NOTIFY);
1349 get_atom_from_string(const char *str)
1351 xcb_intern_atom_cookie_t c;
1352 xcb_intern_atom_reply_t *r;
1355 c = xcb_intern_atom(conn, 0, strlen(str), str);
1356 r = xcb_intern_atom_reply(conn, c, NULL);
1364 return (XCB_ATOM_NONE);
1368 get_wm_protocols(struct ws_win *win) {
1370 xcb_icccm_get_wm_protocols_reply_t wpr;
1372 if (xcb_icccm_get_wm_protocols_reply(conn,
1373 xcb_icccm_get_wm_protocols(conn, win->id, a_prot),
1375 for (i = 0; i < (int)wpr.atoms_len; i++) {
1376 if (wpr.atoms[i] == a_takefocus)
1377 win->take_focus = true;
1378 if (wpr.atoms[i] == a_delete)
1379 win->can_delete = true;
1381 xcb_icccm_get_wm_protocols_reply_wipe(&wpr);
1388 xcb_window_t root, win;
1389 int i, j, num_screens;
1391 for (i = 0; i < LENGTH(ewmh); i++)
1392 ewmh[i].atom = get_atom_from_string(ewmh[i].name);
1394 num_screens = get_screen_count();
1395 for (i = 0; i < num_screens; i++) {
1396 root = screens[i].root;
1398 /* Set up _NET_SUPPORTING_WM_CHECK. */
1399 win = xcb_generate_id(conn);
1400 xcb_create_window(conn, XCB_COPY_FROM_PARENT, win, root,
1401 0, 0, 1, 1, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT,
1402 XCB_COPY_FROM_PARENT, 0, NULL);
1404 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root,
1405 a_net_wm_check, XCB_ATOM_WINDOW, 32, 1, &win);
1406 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win,
1407 a_net_wm_check, XCB_ATOM_WINDOW, 32, 1, &win);
1410 * Impersonate LG3D non-reparenting WM, written by Sun, to
1411 * workaround a Java GUI rendering issue.
1413 if (java_workaround)
1414 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win,
1415 ewmh[_NET_WM_NAME].atom, a_utf8_string,
1416 8, strlen("LG3D"), "LG3D");
1418 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win,
1419 ewmh[_NET_WM_NAME].atom, a_utf8_string,
1420 8, strlen("spectrwm"), "spectrwm");
1422 /* Report supported atoms */
1423 xcb_delete_property(conn, root, a_net_supported);
1424 for (j = 0; j < LENGTH(ewmh); j++)
1425 xcb_change_property(conn, XCB_PROP_MODE_APPEND, root,
1426 a_net_supported, XCB_ATOM_ATOM, 32, 1,
1431 ewmh_update_desktops();
1432 ewmh_get_desktop_names();
1440 xcb_get_property_cookie_t pc;
1441 xcb_get_property_reply_t *pr;
1443 num_screens = get_screen_count();
1445 for (i = 0; i < num_screens; i++) {
1446 /* Get the support check window and destroy it */
1447 pc = xcb_get_property(conn, 0, screens[i].root, a_net_wm_check,
1448 XCB_ATOM_WINDOW, 0, 1);
1449 pr = xcb_get_property_reply(conn, pc, NULL);
1452 if (pr->format == a_net_wm_check) {
1453 id = *((xcb_window_t *)xcb_get_property_value(pr));
1455 xcb_destroy_window(conn, id);
1456 xcb_delete_property(conn, screens[i].root,
1458 xcb_delete_property(conn, screens[i].root,
1466 ewmh_autoquirk(struct ws_win *win)
1468 xcb_get_property_reply_t *r;
1469 xcb_get_property_cookie_t c;
1473 c = xcb_get_property(conn, 0, win->id,
1474 ewmh[_NET_WM_WINDOW_TYPE].atom, XCB_ATOM_ATOM, 0, UINT32_MAX);
1475 r = xcb_get_property_reply(conn, c, NULL);
1479 type = xcb_get_property_value(r);
1480 n = xcb_get_property_value_length(r) / sizeof(xcb_atom_t);
1482 for (i = 0; i < n; i++) {
1483 if (type[i] == ewmh[_NET_WM_WINDOW_TYPE_NORMAL].atom)
1485 if (type[i] == ewmh[_NET_WM_WINDOW_TYPE_DOCK].atom ||
1486 type[i] == ewmh[_NET_WM_WINDOW_TYPE_TOOLBAR].atom ||
1487 type[i] == ewmh[_NET_WM_WINDOW_TYPE_UTILITY].atom) {
1488 win->quirks = SWM_Q_FLOAT | SWM_Q_ANYWHERE;
1491 if (type[i] == ewmh[_NET_WM_WINDOW_TYPE_SPLASH].atom ||
1492 type[i] == ewmh[_NET_WM_WINDOW_TYPE_DIALOG].atom) {
1493 win->quirks = SWM_Q_FLOAT;
1501 ewmh_update_actions(struct ws_win *win)
1503 xcb_atom_t actions[SWM_EWMH_ACTION_COUNT_MAX];
1509 actions[n++] = ewmh[_NET_WM_ACTION_CLOSE].atom;
1512 actions[n++] = ewmh[_NET_WM_ACTION_MOVE].atom;
1513 actions[n++] = ewmh[_NET_WM_ACTION_RESIZE].atom;
1514 actions[n++] = ewmh[_NET_WM_ACTION_ABOVE].atom;
1517 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
1518 ewmh[_NET_WM_ALLOWED_ACTIONS].atom, XCB_ATOM_ATOM, 32, 1, actions);
1521 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
1522 #define _NET_WM_STATE_ADD 1 /* add/set property */
1523 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
1526 ewmh_change_wm_state(struct ws_win *win, xcb_atom_t state, long action)
1533 name = get_atom_name(state);
1534 DNPRINTF(SWM_D_PROP, "ewmh_change_wm_state: win %#x, state: %s, "
1535 "action: %ld\n", WINID(win), name, action);
1541 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
1542 flag = EWMH_F_FULLSCREEN;
1543 else if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
1544 flag = EWMH_F_ABOVE;
1545 else if (state == ewmh[_NET_WM_STATE_HIDDEN].atom)
1546 flag = EWMH_F_HIDDEN;
1547 else if (state == ewmh[_NET_WM_STATE_MAXIMIZED_VERT].atom ||
1548 state == ewmh[_NET_WM_STATE_MAXIMIZED_HORZ].atom)
1549 flag = EWMH_F_MAXIMIZED;
1550 else if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
1551 flag = SWM_F_MANUAL;
1552 else if (state == ewmh[_NET_WM_STATE_SKIP_PAGER].atom)
1553 flag = EWMH_F_SKIP_PAGER;
1554 else if (state == ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom)
1555 flag = EWMH_F_SKIP_TASKBAR;
1557 /* Disallow unfloating transients. */
1558 if (TRANS(win) && flag == EWMH_F_ABOVE)
1561 new_flags = win->ewmh_flags;
1564 case _NET_WM_STATE_REMOVE:
1567 case _NET_WM_STATE_ADD:
1570 case _NET_WM_STATE_TOGGLE:
1575 ewmh_apply_flags(win, new_flags);
1578 DNPRINTF(SWM_D_PROP, "ewmh_change_wm_state: done.\n");
1582 ewmh_apply_flags(struct ws_win *win, uint32_t pending)
1584 struct workspace *ws;
1587 changed = win->ewmh_flags ^ pending;
1591 DNPRINTF(SWM_D_PROP, "ewmh_apply_flags: pending: %d\n", pending);
1593 win->ewmh_flags = pending;
1596 if (changed & EWMH_F_HIDDEN) {
1598 if (focus_mode != SWM_FOCUS_FOLLOW)
1599 ws->focus_pending = get_focus_prev(win);
1604 /* Reload floating geometry in case region changed. */
1606 load_float_geom(win);
1608 /* The window is no longer iconic, prepare focus. */
1609 if (focus_mode != SWM_FOCUS_FOLLOW)
1610 ws->focus_pending = get_focus_magic(win);
1615 if (changed & EWMH_F_ABOVE) {
1616 if (ws->cur_layout != &layouts[SWM_MAX_STACK]) {
1618 load_float_geom(win);
1619 else if (!MAXIMIZED(win))
1620 store_float_geom(win);
1622 win->ewmh_flags &= ~EWMH_F_MAXIMIZED;
1623 changed &= ~EWMH_F_MAXIMIZED;
1627 win->ewmh_flags ^= EWMH_F_ABOVE & pending;
1631 if (changed & EWMH_F_MAXIMIZED) {
1632 /* VERT and/or HORZ changed. */
1634 if (!MAXIMIZED(win))
1635 load_float_geom(win);
1637 store_float_geom(win);
1640 if (MAXIMIZED(win)) {
1641 if (focus_mode != SWM_FOCUS_FOLLOW &&
1642 ws->cur_layout != &layouts[SWM_MAX_STACK]) {
1646 ws->focus_pending = win;
1650 update_window_color(win);
1654 if (changed & EWMH_F_FULLSCREEN) {
1655 if (FULLSCREEN(win)) {
1656 if (focus_mode != SWM_FOCUS_FOLLOW) {
1660 ws->focus_pending = win;
1663 load_float_geom(win);
1666 win->ewmh_flags &= ~EWMH_F_MAXIMIZED;
1670 DNPRINTF(SWM_D_PROP, "ewmh_apply_flags: done.\n");
1674 ewmh_update_wm_state(struct ws_win *win) {
1675 xcb_atom_t vals[SWM_EWMH_ACTION_COUNT_MAX];
1679 vals[n++] = ewmh[_NET_WM_STATE_HIDDEN].atom;
1680 if (FULLSCREEN(win))
1681 vals[n++] = ewmh[_NET_WM_STATE_FULLSCREEN].atom;
1682 if (MAXIMIZED_VERT(win))
1683 vals[n++] = ewmh[_NET_WM_STATE_MAXIMIZED_VERT].atom;
1684 if (MAXIMIZED_HORZ(win))
1685 vals[n++] = ewmh[_NET_WM_STATE_MAXIMIZED_HORZ].atom;
1686 if (win->ewmh_flags & EWMH_F_SKIP_PAGER)
1687 vals[n++] = ewmh[_NET_WM_STATE_SKIP_PAGER].atom;
1688 if (win->ewmh_flags & EWMH_F_SKIP_TASKBAR)
1689 vals[n++] = ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom;
1690 if (win->ewmh_flags & EWMH_F_ABOVE)
1691 vals[n++] = ewmh[_NET_WM_STATE_ABOVE].atom;
1692 if (win->ewmh_flags & SWM_F_MANUAL)
1693 vals[n++] = ewmh[_SWM_WM_STATE_MANUAL].atom;
1696 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
1697 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, n, vals);
1699 xcb_delete_property(conn, win->id, ewmh[_NET_WM_STATE].atom);
1703 ewmh_get_wm_state(struct ws_win *win)
1706 xcb_get_property_cookie_t c;
1707 xcb_get_property_reply_t *r;
1713 win->ewmh_flags = 0;
1715 c = xcb_get_property(conn, 0, win->id, ewmh[_NET_WM_STATE].atom,
1716 XCB_ATOM_ATOM, 0, UINT32_MAX);
1717 r = xcb_get_property_reply(conn, c, NULL);
1721 states = xcb_get_property_value(r);
1722 n = xcb_get_property_value_length(r) / sizeof(xcb_atom_t);
1724 for (i = 0; i < n; i++)
1725 ewmh_change_wm_state(win, states[i], _NET_WM_STATE_ADD);
1733 dumpwins(struct swm_region *r, union arg *args)
1737 xcb_get_window_attributes_cookie_t c;
1738 xcb_get_window_attributes_reply_t *wa;
1740 /* suppress unused warning since var is needed */
1743 if (r->ws == NULL) {
1744 DPRINTF("dumpwins: invalid workspace\n");
1748 DPRINTF("=== managed window list ws %02d ===\n", r->ws->idx);
1749 TAILQ_FOREACH(w, &r->ws->winlist, entry) {
1750 state = get_win_state(w->id);
1751 c = xcb_get_window_attributes(conn, w->id);
1752 wa = xcb_get_window_attributes_reply(conn, c, NULL);
1754 DPRINTF("win %#x, map_state: %d, state: %u, "
1755 "transient: %#x\n", w->id, wa->map_state,
1756 state, w->transient);
1759 DPRINTF("win %#x, failed xcb_get_window_attributes\n",
1763 DPRINTF("=== stacking order (top down) === \n");
1764 TAILQ_FOREACH(w, &r->ws->stack, stack_entry) {
1765 DPRINTF("win %#x, fs: %s, maximized: %s, above: %s, "
1766 "iconic: %s\n", w->id, YESNO(FULLSCREEN(w)),
1767 YESNO(MAXIMIZED(w)), YESNO(ABOVE(w)), YESNO(ICONIC(w)));
1770 DPRINTF("===== unmanaged window list =====\n");
1771 TAILQ_FOREACH(w, &r->ws->unmanagedlist, entry) {
1772 state = get_win_state(w->id);
1773 c = xcb_get_window_attributes(conn, w->id);
1774 wa = xcb_get_window_attributes_reply(conn, c, NULL);
1776 DPRINTF("win %#x, map_state: %d, state: %u, "
1777 "transient: %#x\n", w->id, wa->map_state,
1778 state, w->transient);
1781 DPRINTF("win %#x, failed xcb_get_window_attributes\n",
1785 DPRINTF("=================================\n");
1789 dumpwins(struct swm_region *r, union arg *s)
1794 #endif /* SWM_DEBUG */
1799 int saved_errno, status;
1802 saved_errno = errno;
1806 while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) != 0) {
1811 if (errno != ECHILD)
1812 warn("sighdlr: waitpid");
1813 #endif /* SWM_DEBUG */
1816 if (pid == searchpid)
1820 if (WIFEXITED(status)) {
1821 if (WEXITSTATUS(status) != 0)
1822 warnx("sighdlr: child exit status: %d",
1823 WEXITSTATUS(status));
1825 warnx("sighdlr: child is terminated "
1827 #endif /* SWM_DEBUG */
1841 errno = saved_errno;
1847 struct pid_e *p = NULL;
1849 DNPRINTF(SWM_D_MISC, "find_pid: %d\n", pid);
1854 TAILQ_FOREACH(p, &pidlist, entry) {
1863 name_to_pixel(int sidx, const char *colorname)
1865 uint32_t result = 0;
1866 char cname[32] = "#";
1867 xcb_screen_t *screen;
1868 xcb_colormap_t cmap;
1869 xcb_alloc_color_reply_t *cr;
1870 xcb_alloc_named_color_reply_t *nr;
1871 uint16_t rr, gg, bb;
1873 screen = get_screen(sidx);
1874 cmap = screen->default_colormap;
1876 /* color is in format rgb://rr/gg/bb */
1877 if (strncmp(colorname, "rgb:", 4) == 0) {
1878 if (parse_rgb(colorname, &rr, &gg, &bb) == -1)
1879 warnx("could not parse rgb %s", colorname);
1881 cr = xcb_alloc_color_reply(conn,
1882 xcb_alloc_color(conn, cmap, rr, gg, bb),
1888 warnx("color '%s' not found", colorname);
1891 nr = xcb_alloc_named_color_reply(conn,
1892 xcb_alloc_named_color(conn, cmap, strlen(colorname),
1895 strlcat(cname, colorname + 2, sizeof cname - 1);
1896 nr = xcb_alloc_named_color_reply(conn,
1897 xcb_alloc_named_color(conn, cmap, strlen(cname),
1904 warnx("color '%s' not found", colorname);
1911 setscreencolor(const char *val, int i, int c)
1913 if (i < 0 || i >= get_screen_count())
1916 screens[i].c[c].pixel = name_to_pixel(i, val);
1917 free(screens[i].c[c].name);
1918 if ((screens[i].c[c].name = strdup(val)) == NULL)
1923 fancy_stacker(struct workspace *ws)
1925 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1926 if (ws->cur_layout->l_stack == vertical_stack)
1927 snprintf(ws->stacker, sizeof ws->stacker,
1928 ws->l_state.vertical_flip ? "[%d>%d]" : "[%d|%d]",
1929 ws->l_state.vertical_mwin, ws->l_state.vertical_stacks);
1930 else if (ws->cur_layout->l_stack == horizontal_stack)
1931 snprintf(ws->stacker, sizeof ws->stacker,
1932 ws->l_state.horizontal_flip ? "[%dv%d]" : "[%d-%d]",
1933 ws->l_state.horizontal_mwin, ws->l_state.horizontal_stacks);
1937 plain_stacker(struct workspace *ws)
1939 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1940 if (ws->cur_layout->l_stack == vertical_stack)
1941 strlcpy(ws->stacker, ws->l_state.vertical_flip ? "[>]" : "[|]",
1942 sizeof ws->stacker);
1943 else if (ws->cur_layout->l_stack == horizontal_stack)
1944 strlcpy(ws->stacker, ws->l_state.horizontal_flip ? "[v]" : "[-]",
1945 sizeof ws->stacker);
1949 custom_region(const char *val)
1951 unsigned int x, y, w, h;
1952 int sidx, num_screens;
1953 xcb_screen_t *screen;
1955 num_screens = get_screen_count();
1956 if (sscanf(val, "screen[%d]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
1957 errx(1, "invalid custom region, "
1958 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1959 if (sidx < 1 || sidx > num_screens)
1960 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1964 if ((screen = get_screen(sidx)) == NULL)
1965 errx(1, "ERROR: can't get screen %d.", sidx);
1968 errx(1, "region %ux%u+%u+%u too small", w, h, x, y);
1970 if (x > screen->width_in_pixels ||
1971 y > screen->height_in_pixels ||
1972 w + x > screen->width_in_pixels ||
1973 h + y > screen->height_in_pixels) {
1974 warnx("ignoring region %ux%u+%u+%u - not within screen "
1975 "boundaries (%ux%u)", w, h, x, y,
1976 screen->width_in_pixels, screen->height_in_pixels);
1980 new_region(&screens[sidx], x, y, w, h);
1984 socket_setnonblock(int fd)
1988 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
1989 err(1, "fcntl F_GETFL");
1990 flags |= O_NONBLOCK;
1991 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
1992 err(1, "fcntl F_SETFL");
1996 bar_print_legacy(struct swm_region *r, const char *s)
1998 xcb_rectangle_t rect;
2003 XRectangle ibox, lbox;
2007 XmbTextExtents(bar_fs, s, len, &ibox, &lbox);
2009 switch (bar_justify) {
2010 case SWM_BAR_JUSTIFY_LEFT:
2013 case SWM_BAR_JUSTIFY_CENTER:
2014 x = (WIDTH(r) - lbox.width) / 2;
2016 case SWM_BAR_JUSTIFY_RIGHT:
2017 x = WIDTH(r) - lbox.width - SWM_BAR_OFFSET;
2021 if (x < SWM_BAR_OFFSET)
2026 rect.width = WIDTH(r->bar);
2027 rect.height = HEIGHT(r->bar);
2029 /* clear back buffer */
2030 gcv[0] = r->s->c[SWM_S_COLOR_BAR].pixel;
2031 xcb_change_gc(conn, r->s->bar_gc, XCB_GC_FOREGROUND, gcv);
2032 xcb_poly_fill_rectangle(conn, r->bar->buffer, r->s->bar_gc, 1, &rect);
2034 /* draw back buffer */
2035 gcvd.graphics_exposures = 0;
2036 draw = XCreateGC(display, r->bar->buffer, GCGraphicsExposures, &gcvd);
2037 XSetForeground(display, draw, r->s->c[SWM_S_COLOR_BAR_FONT].pixel);
2038 DRAWSTRING(display, r->bar->buffer, bar_fs, draw,
2039 x, (bar_fs_extents->max_logical_extent.height - lbox.height) / 2 -
2041 XFreeGC(display, draw);
2044 xcb_copy_area(conn, r->bar->buffer, r->bar->id, r->s->bar_gc, 0, 0,
2045 0, 0, WIDTH(r->bar), HEIGHT(r->bar));
2049 bar_print(struct swm_region *r, const char *s)
2052 xcb_rectangle_t rect;
2060 XftTextExtentsUtf8(display, bar_font, (FcChar8 *)s, len, &info);
2062 switch (bar_justify) {
2063 case SWM_BAR_JUSTIFY_LEFT:
2066 case SWM_BAR_JUSTIFY_CENTER:
2067 x = (WIDTH(r) - info.width) / 2;
2069 case SWM_BAR_JUSTIFY_RIGHT:
2070 x = WIDTH(r) - info.width - SWM_BAR_OFFSET;
2074 if (x < SWM_BAR_OFFSET)
2079 rect.width = WIDTH(r->bar);
2080 rect.height = HEIGHT(r->bar);
2082 /* clear back buffer */
2083 gcv[0] = r->s->c[SWM_S_COLOR_BAR].pixel;
2084 xcb_change_gc(conn, r->s->bar_gc, XCB_GC_FOREGROUND, gcv);
2085 xcb_poly_fill_rectangle(conn, r->bar->buffer, r->s->bar_gc, 1, &rect);
2087 /* draw back buffer */
2088 draw = XftDrawCreate(display, r->bar->buffer,
2089 DefaultVisual(display, r->s->idx),
2090 DefaultColormap(display, r->s->idx));
2092 XftDrawStringUtf8(draw, &bar_font_color, bar_font, x,
2093 (HEIGHT(r->bar) + bar_font->height) / 2 - bar_font->descent,
2096 XftDrawDestroy(draw);
2099 xcb_copy_area(conn, r->bar->buffer, r->bar->id, r->s->bar_gc, 0, 0,
2100 0, 0, WIDTH(r->bar), HEIGHT(r->bar));
2104 bar_extra_stop(void)
2108 bzero(bar_pipe, sizeof bar_pipe);
2111 kill(bar_pid, SIGTERM);
2114 strlcpy(bar_ext, "", sizeof bar_ext);
2119 bar_window_class(char *s, size_t sz, struct swm_region *r)
2121 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
2123 if (r->ws->focus->ch.class_name != NULL)
2124 strlcat(s, r->ws->focus->ch.class_name, sz);
2128 bar_window_instance(char *s, size_t sz, struct swm_region *r)
2130 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
2132 if (r->ws->focus->ch.instance_name != NULL)
2133 strlcat(s, r->ws->focus->ch.instance_name, sz);
2137 bar_window_class_instance(char *s, size_t sz, struct swm_region *r)
2139 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
2142 bar_window_class(s, sz, r);
2143 strlcat(s, ":", sz);
2144 bar_window_instance(s, sz, r);
2148 bar_window_state(char *s, size_t sz, struct swm_region *r)
2150 if (r == NULL || r ->ws == NULL || r->ws->focus == NULL)
2152 if (MAXIMIZED(r->ws->focus))
2153 strlcat(s, "(m)", sz);
2154 else if (ABOVE(r->ws->focus))
2155 strlcat(s, "(f)", sz);
2159 bar_window_name(char *s, size_t sz, struct swm_region *r)
2163 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
2166 title = get_win_name(r->ws->focus->id);
2167 strlcat(s, title, sz);
2172 bar_urgent(char *s, size_t sz)
2175 int i, j, num_screens;
2176 bool urgent[SWM_WS_MAX];
2178 xcb_get_property_cookie_t c;
2179 xcb_icccm_wm_hints_t hints;
2181 for (i = 0; i < workspace_limit; i++)
2184 num_screens = get_screen_count();
2185 for (i = 0; i < num_screens; i++)
2186 for (j = 0; j < workspace_limit; j++)
2187 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry) {
2188 c = xcb_icccm_get_wm_hints(conn, win->id);
2189 if (xcb_icccm_get_wm_hints_reply(conn, c,
2192 if (hints.flags & XCB_ICCCM_WM_HINT_X_URGENCY)
2196 for (i = 0; i < workspace_limit; i++) {
2198 snprintf(b, sizeof b, "%d ", i + 1);
2200 } else if (!urgent_collapse) {
2201 strlcat(s, "- ", sz);
2207 bar_workspace_name(char *s, size_t sz, struct swm_region *r)
2209 if (r == NULL || r->ws == NULL)
2211 if (r->ws->name != NULL)
2212 strlcat(s, r->ws->name, sz);
2215 /* build the default bar format according to the defined enabled options */
2217 bar_fmt(const char *fmtexp, char *fmtnew, struct swm_region *r, size_t sz)
2221 /* if format provided, just copy the buffers */
2222 if (bar_format != NULL) {
2223 strlcpy(fmtnew, fmtexp, sz);
2227 /* reset the output buffer */
2230 strlcat(fmtnew, "+N:+I ", sz);
2232 strlcat(fmtnew, "+S", sz);
2233 strlcat(fmtnew, " ", sz);
2235 /* only show the workspace name if there's actually one */
2236 if (r != NULL && r->ws != NULL && r->ws->name != NULL)
2237 strlcat(fmtnew, "<+D>", sz);
2239 /* If enabled, only show the iconic count if there are iconic wins. */
2240 if (iconic_enabled && r != NULL && r->ws != NULL)
2241 TAILQ_FOREACH(w, &r->ws->winlist, entry)
2243 strlcat(fmtnew, "{+M}", sz);
2247 strlcat(fmtnew, "+3<", sz);
2249 if (clock_enabled) {
2250 strlcat(fmtnew, fmtexp, sz);
2251 strlcat(fmtnew, "+4<", sz);
2254 /* bar_urgent already adds the space before the last asterisk */
2256 strlcat(fmtnew, "* +U*+4<", sz);
2258 if (window_class_enabled) {
2259 strlcat(fmtnew, "+C", sz);
2260 if (!window_instance_enabled)
2261 strlcat(fmtnew, "+4<", sz);
2264 /* checks needed by the colon and floating strlcat(3) calls below */
2265 if (r != NULL && r->ws != NULL && r->ws->focus != NULL) {
2266 if (window_instance_enabled) {
2267 if (window_class_enabled)
2268 strlcat(fmtnew, ":", sz);
2269 strlcat(fmtnew, "+T+4<", sz);
2271 if (window_name_enabled) {
2272 if (ABOVE(r->ws->focus) || MAXIMIZED(r->ws->focus))
2273 strlcat(fmtnew, "+F ", sz);
2274 strlcat(fmtnew, "+64W ", sz);
2278 /* finally add the action script output and the version */
2279 strlcat(fmtnew, "+4<+A+4<+V", sz);
2283 bar_replace_pad(char *tmp, int *limit, size_t sz)
2285 /* special case; no limit given, pad one space, instead */
2286 if (*limit == (int)sz - 1)
2288 snprintf(tmp, sz, "%*s", *limit, " ");
2291 /* replaces the bar format character sequences (like in tmux(1)) */
2293 bar_replace_seq(char *fmt, char *fmtrep, struct swm_region *r, size_t *offrep,
2298 char tmp[SWM_BAR_MAX];
2299 int limit, size, count;
2302 /* reset strlcat(3) buffer */
2305 /* get number, if any */
2308 if (sscanf(fmt, "%d%n", &limit, &size) != 1)
2309 limit = sizeof tmp - 1;
2310 if (limit <= 0 || limit >= (int)sizeof tmp)
2311 limit = sizeof tmp - 1;
2313 /* there is nothing to replace (ie EOL) */
2320 bar_replace_pad(tmp, &limit, sizeof tmp);
2323 snprintf(tmp, sizeof tmp, "%s", bar_ext);
2326 bar_window_class(tmp, sizeof tmp, r);
2329 bar_workspace_name(tmp, sizeof tmp, r);
2332 bar_window_state(tmp, sizeof tmp, r);
2335 snprintf(tmp, sizeof tmp, "%d", r->ws->idx + 1);
2339 TAILQ_FOREACH(w, &r->ws->winlist, entry)
2343 snprintf(tmp, sizeof tmp, "%d", count);
2346 snprintf(tmp, sizeof tmp, "%d", r->s->idx + 1);
2349 bar_window_class_instance(tmp, sizeof tmp, r);
2352 snprintf(tmp, sizeof tmp, "%s", r->ws->stacker);
2355 bar_window_instance(tmp, sizeof tmp, r);
2358 bar_urgent(tmp, sizeof tmp);
2361 snprintf(tmp, sizeof tmp, "%s", bar_vertext);
2364 bar_window_name(tmp, sizeof tmp, r);
2367 /* unknown character sequence; copy as-is */
2368 snprintf(tmp, sizeof tmp, "+%c", *fmt);
2374 if ((int)len < limit)
2376 while (limit-- > 0) {
2377 if (*offrep >= sz - 1)
2379 fmtrep[(*offrep)++] = *ptr++;
2387 bar_replace(char *fmt, char *fmtrep, struct swm_region *r, size_t sz)
2392 while (*fmt != '\0') {
2394 /* skip ordinary characters */
2397 fmtrep[off++] = *fmt++;
2401 /* character sequence found; replace it */
2402 fmt = bar_replace_seq(fmt, fmtrep, r, &off, sz);
2411 bar_fmt_expand(char *fmtexp, size_t sz)
2418 /* start by grabbing the current time and date */
2420 localtime_r(&tmt, &tm);
2422 /* figure out what to expand */
2423 if (bar_format != NULL)
2425 else if (bar_format == NULL && clock_enabled)
2427 /* if nothing to expand bail out */
2433 /* copy as-is, just in case the format shouldn't be expanded below */
2434 strlcpy(fmtexp, fmt, sz);
2435 /* finally pass the string through strftime(3) */
2436 #ifndef SWM_DENY_CLOCK_FORMAT
2437 if ((len = strftime(fmtexp, sz, fmt, &tm)) == 0)
2438 warnx("format too long");
2443 /* Redraws the bar; need to follow with xcb_flush() or focus_flush(). */
2447 char fmtexp[SWM_BAR_MAX], fmtnew[SWM_BAR_MAX];
2448 char fmtrep[SWM_BAR_MAX];
2450 struct swm_region *r;
2452 /* expand the format by first passing it through strftime(3) */
2453 bar_fmt_expand(fmtexp, sizeof fmtexp);
2455 num_screens = get_screen_count();
2456 for (i = 0; i < num_screens; i++) {
2457 TAILQ_FOREACH(r, &screens[i].rl, entry) {
2461 if (bar_enabled && r->ws->bar_enabled)
2462 xcb_map_window(conn, r->bar->id);
2464 xcb_unmap_window(conn, r->bar->id);
2468 if (startup_exception)
2469 snprintf(fmtrep, sizeof fmtrep, "total "
2470 "exceptions: %d, first exception: %s",
2474 bar_fmt(fmtexp, fmtnew, r, sizeof fmtnew);
2475 bar_replace(fmtnew, fmtrep, r, sizeof fmtrep);
2477 if (bar_font_legacy)
2478 bar_print_legacy(r, fmtrep);
2480 bar_print(r, fmtrep);
2486 * Reads external script output; call when stdin is readable.
2487 * Returns 1 if bar_ext was updated; otherwise 0.
2490 bar_extra_update(void)
2493 char b[SWM_BAR_MAX];
2494 bool changed = false;
2499 while (fgets(b, sizeof(b), stdin) != NULL) {
2502 if (b[len - 1] == '\n') {
2503 /* Remove newline. */
2506 /* "Clear" bar_ext. */
2509 /* Flush buffered output. */
2510 strlcpy(bar_ext, bar_ext_buf, sizeof(bar_ext));
2511 bar_ext_buf[0] = '\0';
2513 /* Append new output to bar. */
2514 strlcat(bar_ext, b, sizeof(bar_ext));
2518 /* Buffer output. */
2519 strlcat(bar_ext_buf, b, sizeof(bar_ext_buf));
2524 if (errno != EAGAIN) {
2525 warn("bar_action failed");
2534 bar_toggle(struct swm_region *r, union arg *args)
2536 struct swm_region *tmpr;
2539 /* suppress unused warnings since vars are needed */
2543 DNPRINTF(SWM_D_BAR, "bar_toggle\n");
2546 case SWM_ARG_ID_BAR_TOGGLE_WS:
2547 /* Only change if master switch is enabled. */
2549 r->ws->bar_enabled = !r->ws->bar_enabled;
2551 bar_enabled = r->ws->bar_enabled = true;
2553 case SWM_ARG_ID_BAR_TOGGLE:
2554 bar_enabled = !bar_enabled;
2558 /* update bars as necessary */
2559 num_screens = get_screen_count();
2560 for (i = 0; i < num_screens; i++)
2561 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
2563 if (bar_enabled && tmpr->ws->bar_enabled)
2564 xcb_map_window(conn, tmpr->bar->id);
2566 xcb_unmap_window(conn, tmpr->bar->id);
2571 /* must be after stack */
2578 bar_extra_setup(void)
2580 /* do this here because the conf file is in memory */
2581 if (!bar_extra && bar_argv[0]) {
2582 /* launch external status app */
2584 if (pipe(bar_pipe) == -1)
2585 err(1, "pipe error");
2586 socket_setnonblock(bar_pipe[0]);
2587 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
2589 /* Set stdin to read from the pipe. */
2590 if (dup2(bar_pipe[0], STDIN_FILENO) == -1)
2593 /* Set stdout to write to the pipe. */
2594 if (dup2(bar_pipe[1], STDOUT_FILENO) == -1)
2597 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
2598 err(1, "could not disable SIGPIPE");
2599 switch (bar_pid = fork()) {
2601 err(1, "cannot fork");
2605 execvp(bar_argv[0], bar_argv);
2606 err(1, "%s external app failed", bar_argv[0]);
2608 default: /* parent */
2613 atexit(kill_bar_extra_atexit);
2618 kill_bar_extra_atexit(void)
2621 kill(bar_pid, SIGTERM);
2629 while ((s = index(s, '-'))) {
2634 return (count == 14);
2640 char *default_string;
2641 char **missing_charsets;
2642 int num_missing_charsets = 0;
2646 XFreeFontSet(display, bar_fs);
2650 DNPRINTF(SWM_D_INIT, "fontset_init: loading bar_fonts: %s\n", bar_fonts);
2652 bar_fs = XCreateFontSet(display, bar_fonts, &missing_charsets,
2653 &num_missing_charsets, &default_string);
2655 if (num_missing_charsets > 0) {
2656 warnx("Unable to load charset(s):");
2658 for (i = 0; i < num_missing_charsets; ++i)
2659 warnx("%s", missing_charsets[i]);
2661 XFreeStringList(missing_charsets);
2663 if (strcmp(default_string, ""))
2664 warnx("Glyphs from those sets will be replaced "
2665 "by '%s'.", default_string);
2667 warnx("Glyphs from those sets won't be drawn.");
2671 errx(1, "Error creating font set structure.");
2673 bar_fs_extents = XExtentsOfFontSet(bar_fs);
2675 bar_height = bar_fs_extents->max_logical_extent.height +
2676 2 * bar_border_width;
2683 xft_init(struct swm_region *r)
2685 char *font, *d, *search;
2688 if (bar_font == NULL) {
2689 if ((d = strdup(bar_fonts)) == NULL)
2690 errx(1, "insufficient memory.");
2692 while ((font = strsep(&search, ",")) != NULL) {
2696 DNPRINTF(SWM_D_INIT, "xft_init: try font %s\n", font);
2699 bar_font = XftFontOpenXlfd(display, r->s->idx,
2702 bar_font = XftFontOpenName(display, r->s->idx,
2706 if (bar_font == NULL) {
2707 warnx("unable to load font %s", font);
2710 DNPRINTF(SWM_D_INIT, "successfully opened "
2718 if (bar_font == NULL)
2719 errx(1, "unable to open a font");
2721 PIXEL_TO_XRENDERCOLOR(r->s->c[SWM_S_COLOR_BAR_FONT].pixel, color);
2723 if (!XftColorAllocValue(display, DefaultVisual(display, r->s->idx),
2724 DefaultColormap(display, r->s->idx), &color, &bar_font_color))
2725 warn("Xft error: unable to allocate color.");
2727 PIXEL_TO_XRENDERCOLOR(r->s->c[SWM_S_COLOR_BAR].pixel, color);
2729 if (!XftColorAllocValue(display, DefaultVisual(display, r->s->idx),
2730 DefaultColormap(display, r->s->idx), &color, &search_font_color))
2731 warn("Xft error: unable to allocate color.");
2733 bar_height = bar_font->height + 2 * bar_border_width;
2740 bar_setup(struct swm_region *r)
2742 xcb_screen_t *screen;
2745 DNPRINTF(SWM_D_BAR, "bar_setup: screen %d.\n",
2748 if ((screen = get_screen(r->s->idx)) == NULL)
2749 errx(1, "ERROR: can't get screen %d.", r->s->idx);
2754 if ((r->bar = calloc(1, sizeof(struct swm_bar))) == NULL)
2755 err(1, "bar_setup: calloc: failed to allocate memory.");
2757 if (bar_font_legacy)
2763 Y(r->bar) = bar_at_bottom ? (Y(r) + HEIGHT(r) - bar_height) : Y(r);
2764 WIDTH(r->bar) = WIDTH(r) - 2 * bar_border_width;
2765 HEIGHT(r->bar) = bar_height - 2 * bar_border_width;
2767 /* Assume region is unfocused when we create the bar. */
2768 r->bar->id = xcb_generate_id(conn);
2769 wa[0] = r->s->c[SWM_S_COLOR_BAR].pixel;
2770 wa[1] = r->s->c[SWM_S_COLOR_BAR_BORDER_UNFOCUS].pixel;
2771 wa[2] = XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_POINTER_MOTION |
2772 XCB_EVENT_MASK_POINTER_MOTION_HINT;
2774 xcb_create_window(conn, XCB_COPY_FROM_PARENT, r->bar->id, r->s->root,
2775 X(r->bar), Y(r->bar), WIDTH(r->bar), HEIGHT(r->bar),
2776 bar_border_width, XCB_WINDOW_CLASS_INPUT_OUTPUT,
2777 XCB_COPY_FROM_PARENT, XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL
2778 | XCB_CW_EVENT_MASK, wa);
2780 /* Stack bar window above region window to start. */
2782 wa[1] = XCB_STACK_MODE_ABOVE;
2784 xcb_configure_window(conn, r->bar->id, XCB_CONFIG_WINDOW_SIBLING |
2785 XCB_CONFIG_WINDOW_STACK_MODE, wa);
2787 r->bar->buffer = xcb_generate_id(conn);
2788 xcb_create_pixmap(conn, screen->root_depth, r->bar->buffer, r->bar->id,
2789 WIDTH(r->bar), HEIGHT(r->bar));
2792 xcb_randr_select_input(conn, r->bar->id,
2793 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE);
2796 xcb_map_window(conn, r->bar->id);
2798 DNPRINTF(SWM_D_BAR, "bar_setup: win %#x, (x,y) w x h: (%d,%d) "
2799 "%d x %d\n", WINID(r->bar), X(r->bar), Y(r->bar), WIDTH(r->bar),
2806 bar_cleanup(struct swm_region *r)
2810 xcb_destroy_window(conn, r->bar->id);
2811 xcb_free_pixmap(conn, r->bar->buffer);
2817 set_win_state(struct ws_win *win, uint8_t state)
2819 uint16_t data[2] = { state, XCB_ATOM_NONE };
2821 DNPRINTF(SWM_D_EVENT, "set_win_state: win %#x, state: %u\n",
2827 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id, a_state,
2828 a_state, 32, 2, data);
2832 get_win_state(xcb_window_t w)
2834 xcb_get_property_reply_t *r;
2835 xcb_get_property_cookie_t c;
2836 uint32_t result = 0;
2838 c = xcb_get_property(conn, 0, w, a_state, a_state, 0L, 2L);
2839 r = xcb_get_property_reply(conn, c, NULL);
2841 if (r->type == a_state && r->format == 32 && r->length == 2)
2842 result = *((uint32_t *)xcb_get_property_value(r));
2846 DNPRINTF(SWM_D_MISC, "get_win_state property: win %#x state %u\n", w,
2852 version(struct swm_region *r, union arg *args)
2854 /* suppress unused warnings since vars are needed */
2858 bar_version = !bar_version;
2860 snprintf(bar_vertext, sizeof bar_vertext,
2861 "Version: %s Build: %s", SPECTRWM_VERSION, buildstr);
2863 strlcpy(bar_vertext, "", sizeof bar_vertext);
2870 client_msg(struct ws_win *win, xcb_atom_t a, xcb_timestamp_t t)
2872 xcb_client_message_event_t ev;
2880 name = get_atom_name(a);
2881 DNPRINTF(SWM_D_EVENT, "client_msg: win %#x, atom: %s(%u), "
2883 win->id, name, a, t);
2887 bzero(&ev, sizeof ev);
2888 ev.response_type = XCB_CLIENT_MESSAGE;
2889 ev.window = win->id;
2892 ev.data.data32[0] = a;
2893 ev.data.data32[1] = t;
2895 xcb_send_event(conn, 0, win->id,
2896 XCB_EVENT_MASK_NO_EVENT, (const char *)&ev);
2899 /* synthetic response to a ConfigureRequest when not making a change */
2901 config_win(struct ws_win *win, xcb_configure_request_event_t *ev)
2903 xcb_configure_notify_event_t ce;
2908 /* send notification of unchanged state. */
2909 bzero(&ce, sizeof(ce));
2910 ce.response_type = XCB_CONFIGURE_NOTIFY;
2913 ce.width = WIDTH(win);
2914 ce.height = HEIGHT(win);
2915 ce.override_redirect = 0;
2920 ce.window = win->id;
2921 ce.border_width = BORDER(win);
2922 ce.above_sibling = XCB_WINDOW_NONE;
2925 ce.event = ev->window;
2926 ce.window = ev->window;
2928 /* make response appear more WM_SIZE_HINTS-compliant */
2929 if (win->sh.flags) {
2930 DNPRINTF(SWM_D_MISC, "config_win: hints: win %#x,"
2931 " sh.flags: %u, min: %d x %d, max: %d x %d, inc: "
2932 "%d x %d\n", win->id, win->sh.flags, SH_MIN_W(win),
2933 SH_MIN_H(win), SH_MAX_W(win), SH_MAX_H(win),
2934 SH_INC_W(win), SH_INC_H(win));
2939 /* the hint may be set... to 0! */
2940 if (SH_MIN_W(win) > 0 && ce.width < SH_MIN_W(win))
2941 ce.width = SH_MIN_W(win);
2942 if (SH_MIN_H(win) > 0 && ce.height < SH_MIN_H(win))
2943 ce.height = SH_MIN_H(win);
2948 /* may also be advertized as 0 */
2949 if (SH_MAX_W(win) > 0 && ce.width > SH_MAX_W(win))
2950 ce.width = SH_MAX_W(win);
2951 if (SH_MAX_H(win) > 0 && ce.height > SH_MAX_H(win))
2952 ce.height = SH_MAX_H(win);
2955 /* resize increment. */
2957 if (SH_INC_W(win) > 1 && ce.width > SH_INC_W(win))
2958 ce.width -= (ce.width - SH_MIN_W(win)) %
2960 if (SH_INC_H(win) > 1 && ce.height > SH_INC_H(win))
2961 ce.height -= (ce.height - SH_MIN_H(win)) %
2965 /* adjust x and y for requested border_width. */
2966 ce.x += BORDER(win) - ev->border_width;
2967 ce.y += BORDER(win) - ev->border_width;
2968 ce.border_width = ev->border_width;
2969 ce.above_sibling = ev->sibling;
2972 DNPRINTF(SWM_D_MISC, "config_win: ewmh: %s, win %#x, (x,y) w x h: "
2973 "(%d,%d) %d x %d, border: %d\n", YESNO(ev == NULL), win->id, ce.x,
2974 ce.y, ce.width, ce.height, ce.border_width);
2976 xcb_send_event(conn, 0, win->id, XCB_EVENT_MASK_STRUCTURE_NOTIFY,
2981 count_win(struct workspace *ws, bool count_transient)
2986 TAILQ_FOREACH(win, &ws->winlist, entry) {
2987 if (!count_transient && FLOATING(win))
2993 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
2999 quit(struct swm_region *r, union arg *args)
3001 /* suppress unused warnings since vars are needed */
3005 DNPRINTF(SWM_D_MISC, "quit\n");
3010 raise_window(struct ws_win *win)
3012 struct ws_win *target = NULL;
3013 struct swm_region *r;
3014 struct workspace *ws;
3016 if (win == NULL || (r = win->ws->r) == NULL)
3020 DNPRINTF(SWM_D_EVENT, "raise_window: win %#x\n", win->id);
3022 TAILQ_FOREACH(target, &ws->stack, stack_entry) {
3023 if (target == win || ICONIC(target))
3025 if (ws->cur_layout == &layouts[SWM_MAX_STACK])
3027 if (TRANS(win) && (win->transient == target->transient ||
3028 win->transient == target->id))
3030 if (FULLSCREEN(win))
3032 if (FULLSCREEN(target))
3036 if (MAXIMIZED(target))
3038 if (ABOVE(win) || TRANS(win))
3040 if (!ABOVE(target) && !TRANS(target))
3044 if (target != NULL) {
3045 /* Change stack position. */
3046 TAILQ_REMOVE(&ws->stack, win, stack_entry);
3047 TAILQ_INSERT_BEFORE(target, win, stack_entry);
3048 update_win_stacking(win);
3052 if (swm_debug & SWM_D_STACK) {
3053 DPRINTF("=== stacking order (top down) === \n");
3054 TAILQ_FOREACH(target, &r->ws->stack, stack_entry) {
3055 DPRINTF("win %#x, fs: %s, maximized: %s, above: %s, "
3056 "iconic: %s\n", target->id, YESNO(FULLSCREEN(target)),
3057 YESNO(MAXIMIZED(target)), YESNO(ABOVE(target)),
3058 YESNO(ICONIC(target)));
3062 DNPRINTF(SWM_D_EVENT, "raise_window: done\n");
3066 update_win_stacking(struct ws_win *win)
3068 struct ws_win *sibling;
3069 struct swm_region *r;
3072 if (win == NULL || (r = win->ws->r) == NULL)
3075 sibling = TAILQ_NEXT(win, stack_entry);
3076 if (sibling != NULL && FLOATING(win) == FLOATING(sibling))
3077 val[0] = sibling->id;
3079 val[0] = FLOATING(win) ? r->bar->id : r->id;
3081 DNPRINTF(SWM_D_EVENT, "update_win_stacking: %#x, sibling %#x\n",
3084 val[1] = XCB_STACK_MODE_ABOVE;
3086 xcb_configure_window(conn, win->id, XCB_CONFIG_WINDOW_SIBLING |
3087 XCB_CONFIG_WINDOW_STACK_MODE, val);
3091 map_window(struct ws_win *win)
3096 DNPRINTF(SWM_D_EVENT, "map_window: win %#x, mapped: %s\n",
3097 win->id, YESNO(win->mapped));
3102 xcb_map_window(conn, win->id);
3103 set_win_state(win, XCB_ICCCM_WM_STATE_NORMAL);
3108 unmap_window(struct ws_win *win)
3113 DNPRINTF(SWM_D_EVENT, "unmap_window: win %#x, mapped: %s\n", win->id,
3114 YESNO(win->mapped));
3119 xcb_unmap_window(conn, win->id);
3120 set_win_state(win, XCB_ICCCM_WM_STATE_ICONIC);
3121 win->mapped = false;
3128 int i, j, num_screens;
3130 num_screens = get_screen_count();
3131 for (i = 0; i < num_screens; i++)
3132 for (j = 0; j < workspace_limit; j++)
3133 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
3138 fake_keypress(struct ws_win *win, xcb_keysym_t keysym, uint16_t modifiers)
3140 xcb_key_press_event_t event;
3141 xcb_keycode_t *keycode;
3146 keycode = xcb_key_symbols_get_keycode(syms, keysym);
3148 DNPRINTF(SWM_D_MISC, "fake_keypress: win %#x, keycode %u\n",
3151 bzero(&event, sizeof(event));
3152 event.event = win->id;
3153 event.root = win->s->root;
3154 event.child = XCB_WINDOW_NONE;
3155 event.time = XCB_CURRENT_TIME;
3156 event.event_x = X(win);
3157 event.event_y = Y(win);
3160 event.same_screen = 1;
3161 event.detail = *keycode;
3162 event.state = modifiers;
3164 event.response_type = XCB_KEY_PRESS;
3165 xcb_send_event(conn, 1, win->id,
3166 XCB_EVENT_MASK_KEY_PRESS, (const char *)&event);
3168 event.response_type = XCB_KEY_RELEASE;
3169 xcb_send_event(conn, 1, win->id,
3170 XCB_EVENT_MASK_KEY_RELEASE, (const char *)&event);
3176 restart(struct swm_region *r, union arg *args)
3178 /* suppress unused warning since var is needed */
3182 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
3186 execvp(start_argv[0], start_argv);
3187 warn("execvp failed");
3192 get_pointer_win(xcb_window_t root)
3194 struct ws_win *win = NULL;
3195 xcb_query_pointer_reply_t *r;
3197 DNPRINTF(SWM_D_EVENT, "get_pointer_win: root: %#x.\n", root);
3199 r = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, root), NULL);
3201 win = find_window(r->child);
3203 DNPRINTF(SWM_D_EVENT, "get_pointer_win: %#x.\n",
3206 DNPRINTF(SWM_D_EVENT, "get_pointer_win: none.\n");
3215 center_pointer(struct swm_region *r)
3219 if (!warp_pointer || r == NULL)
3223 if (win && win->mapped)
3224 xcb_warp_pointer(conn, XCB_NONE, win->id, 0, 0, 0, 0,
3225 WIDTH(win) / 2, HEIGHT(win) / 2);
3227 xcb_warp_pointer(conn, XCB_NONE, r->id, 0, 0, 0, 0,
3228 WIDTH(r) / 2, HEIGHT(r) / 2);
3232 root_to_region(xcb_window_t root, int check)
3235 struct swm_region *r = NULL;
3237 xcb_query_pointer_reply_t *qpr;
3238 xcb_get_input_focus_reply_t *gifr;
3240 DNPRINTF(SWM_D_MISC, "root_to_region: win %#x\n", root);
3242 num_screens = get_screen_count();
3243 for (i = 0; i < num_screens; i++)
3244 if (screens[i].root == root)
3247 if (check & SWM_CK_REGION)
3248 r = screens[i].r_focus;
3250 if (r == NULL && check & SWM_CK_FOCUS) {
3251 /* Try to find an actively focused window */
3252 gifr = xcb_get_input_focus_reply(conn,
3253 xcb_get_input_focus(conn), NULL);
3255 cfw = find_window(gifr->focus);
3256 if (cfw && cfw->ws->r)
3263 if (r == NULL && check & SWM_CK_POINTER) {
3264 /* No region with an active focus; try to use pointer. */
3265 qpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn,
3266 screens[i].root), NULL);
3268 DNPRINTF(SWM_D_MISC, "root_to_region: pointer: "
3269 "(%d,%d)\n", qpr->root_x, qpr->root_y);
3270 TAILQ_FOREACH(r, &screens[i].rl, entry)
3271 if (X(r) <= qpr->root_x &&
3272 qpr->root_x < MAX_X(r) &&
3273 Y(r) <= qpr->root_y &&
3274 qpr->root_y < MAX_Y(r))
3281 if (r == NULL && check & SWM_CK_FALLBACK)
3282 r = TAILQ_FIRST(&screens[i].rl);
3284 DNPRINTF(SWM_D_MISC, "root_to_region: idx: %d\n", get_region_index(r));
3290 find_unmanaged_window(xcb_window_t id)
3293 int i, j, num_screens;
3295 num_screens = get_screen_count();
3296 for (i = 0; i < num_screens; i++)
3297 for (j = 0; j < workspace_limit; j++)
3298 TAILQ_FOREACH(win, &screens[i].ws[j].unmanagedlist,
3306 find_window(xcb_window_t id)
3309 int i, j, num_screens;
3310 xcb_query_tree_reply_t *r;
3312 num_screens = get_screen_count();
3313 for (i = 0; i < num_screens; i++)
3314 for (j = 0; j < workspace_limit; j++)
3315 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
3319 r = xcb_query_tree_reply(conn, xcb_query_tree(conn, id), NULL);
3323 /* if we were looking for the parent return that window instead */
3324 if (r->parent == 0 || r->root == r->parent) {
3329 /* look for parent */
3330 for (i = 0; i < num_screens; i++)
3331 for (j = 0; j < workspace_limit; j++)
3332 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
3333 if (r->parent == win->id) {
3343 spawn(int ws_idx, union arg *args, bool close_fd)
3348 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
3350 close(xcb_get_file_descriptor(conn));
3352 setenv("LD_PRELOAD", SWM_LIB, 1);
3354 if (asprintf(&ret, "%d", ws_idx) == -1) {
3355 warn("spawn: asprintf SWM_WS");
3358 setenv("_SWM_WS", ret, 1);
3362 if (asprintf(&ret, "%d", getpid()) == -1) {
3363 warn("spawn: asprintf _SWM_PID");
3366 setenv("_SWM_PID", ret, 1);
3370 if (setsid() == -1) {
3371 warn("spawn: setsid");
3377 * close stdin and stdout to prevent interaction between apps
3378 * and the baraction script
3379 * leave stderr open to record errors
3381 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) == -1) {
3382 warn("spawn: open");
3385 dup2(fd, STDIN_FILENO);
3386 dup2(fd, STDOUT_FILENO);
3391 execvp(args->argv[0], args->argv);
3393 warn("spawn: execvp");
3398 kill_refs(struct ws_win *win)
3400 int i, x, num_screens;
3401 struct swm_region *r;
3402 struct workspace *ws;
3407 num_screens = get_screen_count();
3408 for (i = 0; i < num_screens; i++)
3409 TAILQ_FOREACH(r, &screens[i].rl, entry)
3410 for (x = 0; x < workspace_limit; x++) {
3412 if (win == ws->focus)
3414 if (win == ws->focus_prev)
3415 ws->focus_prev = NULL;
3420 validate_win(struct ws_win *testwin)
3423 struct workspace *ws;
3424 struct swm_region *r;
3425 int i, x, num_screens;
3427 if (testwin == NULL)
3430 num_screens = get_screen_count();
3431 for (i = 0; i < num_screens; i++)
3432 TAILQ_FOREACH(r, &screens[i].rl, entry)
3433 for (x = 0; x < workspace_limit; x++) {
3435 TAILQ_FOREACH(win, &ws->winlist, entry)
3443 validate_ws(struct workspace *testws)
3445 struct swm_region *r;
3446 struct workspace *ws;
3447 int i, x, num_screens;
3449 /* validate all ws */
3450 num_screens = get_screen_count();
3451 for (i = 0; i < num_screens; i++)
3452 TAILQ_FOREACH(r, &screens[i].rl, entry)
3453 for (x = 0; x < workspace_limit; x++) {
3462 unfocus_win(struct ws_win *win)
3464 xcb_window_t none = XCB_WINDOW_NONE;
3466 DNPRINTF(SWM_D_FOCUS, "unfocus_win: win %#x\n", WINID(win));
3471 if (win->ws == NULL) {
3472 DNPRINTF(SWM_D_FOCUS, "unfocus_win: NULL ws.\n");
3476 if (validate_ws(win->ws)) {
3477 DNPRINTF(SWM_D_FOCUS, "unfocus_win: invalid ws.\n");
3481 if (win->ws->r == NULL) {
3482 DNPRINTF(SWM_D_FOCUS, "unfocus_win: NULL region.\n");
3486 if (validate_win(win)) {
3487 DNPRINTF(SWM_D_FOCUS, "unfocus_win: invalid win.\n");
3492 if (win->ws->focus == win) {
3493 win->ws->focus = NULL;
3494 win->ws->focus_prev = win;
3497 if (validate_win(win->ws->focus)) {
3498 kill_refs(win->ws->focus);
3499 win->ws->focus = NULL;
3502 if (validate_win(win->ws->focus_prev)) {
3503 kill_refs(win->ws->focus_prev);
3504 win->ws->focus_prev = NULL;
3507 update_window_color(win);
3509 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
3510 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1, &none);
3512 DNPRINTF(SWM_D_FOCUS, "unfocus_win: done.\n");
3516 focus_win(struct ws_win *win)
3518 struct ws_win *cfw = NULL, *parent = NULL, *w, *tmpw;
3519 struct workspace *ws;
3520 xcb_get_input_focus_reply_t *gifr;
3522 DNPRINTF(SWM_D_FOCUS, "focus_win: win %#x\n", WINID(win));
3524 if (win == NULL || win->ws == NULL || !win->mapped)
3529 if (validate_ws(ws))
3532 if (validate_win(win)) {
3537 gifr = xcb_get_input_focus_reply(conn, xcb_get_input_focus(conn), NULL);
3539 cfw = find_window(gifr->focus);
3540 if (cfw != NULL && cfw != win) {
3541 if (cfw->ws != ws && cfw->ws->r != NULL) {
3542 /* Change border to unfocused color. */
3543 xcb_change_window_attributes(conn, cfw->id,
3544 XCB_CW_BORDER_PIXEL,
3545 &cfw->s->c[(MAXIMIZED(cfw) ?
3546 SWM_S_COLOR_UNFOCUS_MAXIMIZED :
3547 SWM_S_COLOR_UNFOCUS)].pixel);
3555 if (ws->focus != win) {
3556 if (ws->focus && ws->focus != cfw)
3557 unfocus_win(ws->focus);
3561 /* If this window directs focus to a child window, then clear. */
3562 if (win->focus_child)
3563 win->focus_child = NULL;
3565 /* If transient, adjust parent's focus child for focus_magic. */
3567 parent = find_window(win->transient);
3568 if (parent && parent->focus_child != win)
3569 parent->focus_child = win;
3572 if (cfw != win && ws->r != NULL) {
3573 /* Set input focus if no input hint, or indicated by hint. */
3574 if (!(win->hints.flags & XCB_ICCCM_WM_HINT_INPUT) ||
3575 (win->hints.flags & XCB_ICCCM_WM_HINT_INPUT &&
3577 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT,
3578 win->id, last_event_time);
3580 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT,
3581 ws->r->id, XCB_CURRENT_TIME);
3583 /* Tell app it can adjust focus to a specific window. */
3584 if (win->take_focus) {
3585 /* java is special; always tell parent */
3586 if (TRANS(win) && win->java)
3587 client_msg(parent, a_takefocus,
3590 client_msg(win, a_takefocus, last_event_time);
3593 if (ws->cur_layout->flags & SWM_L_MAPONFOCUS ||
3595 /* If a parent exists, map it first. */
3597 raise_window(parent);
3600 /* Map siblings next. */
3601 TAILQ_FOREACH_SAFE(w, &ws->stack, stack_entry,
3603 if (w != win && !ICONIC(w) &&
3604 w->transient == parent->id) {
3610 /* Map focused window. */
3614 /* Stack any children of focus window. */
3615 TAILQ_FOREACH_SAFE(w, &ws->stack, stack_entry, tmpw)
3616 if (w->transient == win->id && !ICONIC(w)) {
3620 } else if (tile_gap < 0 && !ABOVE(win)) {
3622 * Windows overlap in the layout.
3623 * Raise focused win above all tiled wins.
3631 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
3632 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1,
3637 /* Update window border even if workspace is hidden. */
3638 update_window_color(win);
3643 DNPRINTF(SWM_D_FOCUS, "focus_win: done.\n");
3646 /* If a child window should have focus instead, return it. */
3648 get_focus_magic(struct ws_win *win)
3650 struct ws_win *parent = NULL;
3651 struct ws_win *child = NULL;
3653 DNPRINTF(SWM_D_FOCUS, "get_focus_magic: win %#x\n", WINID(win));
3658 parent = find_window(win->transient);
3660 /* If parent prefers focus elsewhere, then try to do so. */
3661 if (parent && (child = parent->focus_child)) {
3662 if (validate_win(child) == 0 && child->mapped)
3665 parent->focus_child = NULL;
3669 /* If this window prefers focus elsewhere, then try to do so. */
3670 if ((child = win->focus_child)) {
3671 if (validate_win(child) == 0 && child->mapped)
3674 win->focus_child = NULL;
3681 event_drain(uint8_t rt)
3683 xcb_generic_event_t *evt;
3685 /* ensure all pending requests have been processed before filtering. */
3687 while ((evt = xcb_poll_for_event(conn))) {
3688 if (XCB_EVENT_RESPONSE_TYPE(evt) != rt)
3696 set_region(struct swm_region *r)
3698 struct swm_region *rf;
3705 /* Unfocus old region bar. */
3710 xcb_change_window_attributes(conn, rf->bar->id,
3711 XCB_CW_BORDER_PIXEL,
3712 &r->s->c[SWM_S_COLOR_BAR_BORDER_UNFOCUS].pixel);
3715 if (rf != NULL && rf != r && (X(rf) != X(r) || Y(rf) != Y(r) ||
3716 WIDTH(rf) != WIDTH(r) || HEIGHT(rf) != HEIGHT(r))) {
3717 /* Set _NET_DESKTOP_GEOMETRY. */
3719 vals[1] = HEIGHT(r);
3720 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, r->s->root,
3721 ewmh[_NET_DESKTOP_GEOMETRY].atom, XCB_ATOM_CARDINAL, 32, 2,
3725 /* Set region bar border to focus_color. */
3726 xcb_change_window_attributes(conn, r->bar->id,
3727 XCB_CW_BORDER_PIXEL, &r->s->c[SWM_S_COLOR_BAR_BORDER].pixel);
3731 ewmh_update_current_desktop();
3735 focus_region(struct swm_region *r)
3738 struct swm_region *old_r;
3743 old_r = r->s->r_focus;
3746 nfw = get_region_focus(r);
3750 /* New region is empty; need to manually unfocus win. */
3752 unfocus_win(old_r->ws->focus);
3754 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT, r->id,
3757 /* Clear bar since empty. */
3763 switchws(struct swm_region *r, union arg *args)
3765 struct swm_region *this_r, *other_r;
3767 struct workspace *new_ws, *old_ws;
3768 xcb_window_t none = XCB_WINDOW_NONE;
3769 int wsid = args->id;
3770 bool unmap_old = false;
3775 if (wsid >= workspace_limit)
3779 old_ws = this_r->ws;
3780 new_ws = &this_r->s->ws[wsid];
3782 DNPRINTF(SWM_D_WS, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
3783 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), old_ws->idx, wsid);
3785 if (new_ws == NULL || old_ws == NULL)
3787 if (new_ws == old_ws)
3790 if ((win = old_ws->focus) != NULL) {
3791 update_window_color(win);
3793 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
3794 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1,
3798 other_r = new_ws->r;
3799 if (other_r == NULL) {
3800 /* the other workspace is hidden, hide this one */
3804 /* the other ws is visible in another region, exchange them */
3805 other_r->ws_prior = new_ws;
3806 other_r->ws = old_ws;
3807 old_ws->r = other_r;
3809 this_r->ws_prior = old_ws;
3810 this_r->ws = new_ws;
3813 /* Set focus_pending before stacking, if needed. */
3814 if (focus_mode != SWM_FOCUS_FOLLOW && (!new_ws->focus_pending ||
3815 validate_win(new_ws->focus_pending))) {
3816 new_ws->focus_pending = get_region_focus(new_ws->r);
3817 new_ws->focus = new_ws->focus_prev;
3818 new_ws->focus_prev = NULL;
3821 new_ws->state = SWM_WS_STATE_MAPPING;
3824 /* unmap old windows */
3826 TAILQ_FOREACH(win, &old_ws->winlist, entry)
3828 old_ws->state = SWM_WS_STATE_HIDDEN;
3831 /* if workspaces were swapped, then don't wait to set focus */
3832 if (old_ws->r && focus_mode != SWM_FOCUS_FOLLOW) {
3833 if (new_ws->focus_pending) {
3834 focus_win(new_ws->focus_pending);
3835 new_ws->focus_pending = NULL;
3839 /* Clear bar and set focus on region input win if new ws is empty. */
3840 if (new_ws->focus_pending == NULL && new_ws->focus == NULL) {
3841 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT, r->id,
3846 ewmh_update_current_desktop();
3850 new_ws->state = SWM_WS_STATE_MAPPED;
3852 DNPRINTF(SWM_D_WS, "switchws: done.\n");
3856 cyclews(struct swm_region *r, union arg *args)
3859 struct swm_screen *s = r->s;
3860 bool cycle_all = false, mv = false;
3862 DNPRINTF(SWM_D_WS, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3863 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
3869 case SWM_ARG_ID_CYCLEWS_MOVE_UP:
3872 case SWM_ARG_ID_CYCLEWS_UP_ALL:
3875 case SWM_ARG_ID_CYCLEWS_UP:
3876 a.id = (a.id < workspace_limit - 1) ? a.id + 1 : 0;
3878 case SWM_ARG_ID_CYCLEWS_MOVE_DOWN:
3881 case SWM_ARG_ID_CYCLEWS_DOWN_ALL:
3884 case SWM_ARG_ID_CYCLEWS_DOWN:
3885 a.id = (a.id > 0) ? a.id - 1 : workspace_limit - 1;
3892 (!cycle_empty && TAILQ_EMPTY(&s->ws[a.id].winlist)))
3894 if (!cycle_visible && s->ws[a.id].r != NULL)
3901 } while (a.id != r->ws->idx);
3903 DNPRINTF(SWM_D_FOCUS, "cyclews: done\n");
3907 priorws(struct swm_region *r, union arg *args)
3913 DNPRINTF(SWM_D_WS, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3914 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
3916 if (r->ws_prior == NULL)
3919 a.id = r->ws_prior->idx;
3921 DNPRINTF(SWM_D_FOCUS, "priorws: done\n");
3925 focusrg(struct swm_region *r, union arg *args)
3927 int ridx = args->id, i, num_screens;
3928 struct swm_region *rr = NULL;
3930 num_screens = get_screen_count();
3931 /* do nothing if we don't have more than one screen */
3932 if (!(num_screens > 1 || outputs > 1))
3935 DNPRINTF(SWM_D_FOCUS, "focusrg: id: %d\n", ridx);
3937 rr = TAILQ_FIRST(&r->s->rl);
3938 for (i = 0; i < ridx && rr != NULL; ++i)
3939 rr = TAILQ_NEXT(rr, entry);
3947 DNPRINTF(SWM_D_FOCUS, "focusrg: done\n");
3951 cyclerg(struct swm_region *r, union arg *args)
3953 struct swm_region *rr = NULL;
3956 num_screens = get_screen_count();
3957 /* do nothing if we don't have more than one screen */
3958 if (!(num_screens > 1 || outputs > 1))
3962 DNPRINTF(SWM_D_FOCUS, "cyclerg: id: %d, region: %d\n", args->id, i);
3965 case SWM_ARG_ID_CYCLERG_UP:
3966 rr = TAILQ_NEXT(r, entry);
3968 rr = TAILQ_FIRST(&screens[i].rl);
3970 case SWM_ARG_ID_CYCLERG_DOWN:
3971 rr = TAILQ_PREV(r, swm_region_list, entry);
3973 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
3984 DNPRINTF(SWM_D_FOCUS, "cyclerg: done\n");
3987 /* Sorts transients after parent. */
3989 sort_windows(struct ws_win_list *wl)
3991 struct ws_win *win, *parent, *nxt;
3996 for (win = TAILQ_FIRST(wl); win != TAILQ_END(wl); win = nxt) {
3997 nxt = TAILQ_NEXT(win, entry);
3999 parent = find_window(win->transient);
4000 if (parent == NULL) {
4001 warnx("not possible bug");
4004 TAILQ_REMOVE(wl, win, entry);
4005 TAILQ_INSERT_AFTER(wl, parent, win, entry);
4011 swapwin(struct swm_region *r, union arg *args)
4013 struct ws_win *target, *source;
4014 struct ws_win *cur_focus;
4015 struct ws_win_list *wl;
4017 DNPRINTF(SWM_D_WS, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
4018 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
4020 cur_focus = r->ws->focus;
4021 if (cur_focus == NULL || FULLSCREEN(cur_focus))
4024 /* Adjust stacking in floating layer. */
4025 if (ABOVE(cur_focus)) {
4027 case SWM_ARG_ID_SWAPPREV:
4028 target = TAILQ_PREV(cur_focus, ws_win_stack,
4030 if (target != NULL && FLOATING(target)) {
4031 TAILQ_REMOVE(&cur_focus->ws->stack, cur_focus,
4033 TAILQ_INSERT_BEFORE(target, cur_focus,
4035 update_win_stacking(cur_focus);
4039 case SWM_ARG_ID_SWAPNEXT:
4040 target = TAILQ_NEXT(cur_focus, stack_entry);
4041 if (target != NULL && FLOATING(target)) {
4042 TAILQ_REMOVE(&cur_focus->ws->stack, cur_focus,
4044 TAILQ_INSERT_AFTER(&cur_focus->ws->stack,
4045 target, cur_focus, stack_entry);
4046 update_win_stacking(cur_focus);
4054 if (r->ws->cur_layout == &layouts[SWM_MAX_STACK])
4057 clear_maximized(r->ws);
4060 wl = &source->ws->winlist;
4063 case SWM_ARG_ID_SWAPPREV:
4065 source = find_window(source->transient);
4066 target = TAILQ_PREV(source, ws_win_list, entry);
4067 if (target && target->transient)
4068 target = find_window(target->transient);
4069 TAILQ_REMOVE(wl, source, entry);
4071 TAILQ_INSERT_TAIL(wl, source, entry);
4073 TAILQ_INSERT_BEFORE(target, source, entry);
4075 case SWM_ARG_ID_SWAPNEXT:
4076 target = TAILQ_NEXT(source, entry);
4077 /* move the parent and let the sort handle the move */
4079 source = find_window(source->transient);
4080 TAILQ_REMOVE(wl, source, entry);
4082 TAILQ_INSERT_HEAD(wl, source, entry);
4084 TAILQ_INSERT_AFTER(wl, target, source, entry);
4086 case SWM_ARG_ID_SWAPMAIN:
4087 target = TAILQ_FIRST(wl);
4088 if (target == source) {
4089 if (source->ws->focus_prev != NULL &&
4090 source->ws->focus_prev != target)
4091 source = source->ws->focus_prev;
4095 if (target == NULL || source == NULL)
4097 source->ws->focus_prev = target;
4098 TAILQ_REMOVE(wl, target, entry);
4099 TAILQ_INSERT_BEFORE(source, target, entry);
4100 TAILQ_REMOVE(wl, source, entry);
4101 TAILQ_INSERT_HEAD(wl, source, entry);
4103 case SWM_ARG_ID_MOVELAST:
4104 TAILQ_REMOVE(wl, source, entry);
4105 TAILQ_INSERT_TAIL(wl, source, entry);
4108 DNPRINTF(SWM_D_MOVE, "swapwin: invalid id: %d\n", args->id);
4113 ewmh_update_client_list();
4118 DNPRINTF(SWM_D_MOVE, "swapwin: done\n");
4122 get_focus_prev(struct ws_win *win)
4124 struct ws_win *winfocus = NULL;
4125 struct ws_win *cur_focus = NULL;
4126 struct ws_win_list *wl = NULL;
4127 struct workspace *ws = NULL;
4129 if (!(win && win->ws))
4134 cur_focus = ws->focus;
4136 DNPRINTF(SWM_D_FOCUS, "get_focus_prev: win %#x, cur_focus: %#x, "
4137 "focus_prev: %#x\n", WINID(win), WINID(cur_focus),
4138 WINID(ws->focus_prev));
4140 /* pickle, just focus on whatever */
4141 if (cur_focus == NULL) {
4142 /* use prev_focus if valid */
4143 if (ws->focus_prev && find_window(ws->focus_prev->id))
4144 winfocus = ws->focus_prev;
4148 /* if transient focus on parent */
4149 if (TRANS(cur_focus)) {
4150 winfocus = find_window(cur_focus->transient);
4154 /* if in max_stack try harder */
4155 if ((win->quirks & SWM_Q_FOCUSPREV) ||
4156 (ws->cur_layout->flags & SWM_L_FOCUSPREV)) {
4157 if (cur_focus != ws->focus_prev)
4158 winfocus = ws->focus_prev;
4160 winfocus = TAILQ_PREV(win, ws_win_list, entry);
4165 DNPRINTF(SWM_D_FOCUS, "get_focus_prev: focus_close: %d\n", focus_close);
4167 if (winfocus == NULL || winfocus == win) {
4168 switch (focus_close) {
4169 case SWM_STACK_BOTTOM:
4170 TAILQ_FOREACH(winfocus, wl, entry)
4171 if (!ICONIC(winfocus) && winfocus != cur_focus)
4175 TAILQ_FOREACH_REVERSE(winfocus, wl, ws_win_list, entry)
4176 if (!ICONIC(winfocus) && winfocus != cur_focus)
4179 case SWM_STACK_ABOVE:
4180 winfocus = TAILQ_NEXT(cur_focus, entry);
4181 while (winfocus && ICONIC(winfocus))
4182 winfocus = TAILQ_NEXT(winfocus, entry);
4184 if (winfocus == NULL) {
4185 if (focus_close_wrap) {
4186 TAILQ_FOREACH(winfocus, wl, entry)
4187 if (!ICONIC(winfocus) &&
4188 winfocus != cur_focus)
4191 TAILQ_FOREACH_REVERSE(winfocus, wl,
4193 if (!ICONIC(winfocus) &&
4194 winfocus != cur_focus)
4199 case SWM_STACK_BELOW:
4200 winfocus = TAILQ_PREV(cur_focus, ws_win_list, entry);
4201 while (winfocus && ICONIC(winfocus))
4202 winfocus = TAILQ_PREV(winfocus, ws_win_list,
4205 if (winfocus == NULL) {
4206 if (focus_close_wrap) {
4207 TAILQ_FOREACH_REVERSE(winfocus, wl,
4209 if (!ICONIC(winfocus) &&
4210 winfocus != cur_focus)
4213 TAILQ_FOREACH(winfocus, wl, entry)
4214 if (!ICONIC(winfocus) &&
4215 winfocus != cur_focus)
4223 if (winfocus == NULL ||
4224 (winfocus && (ICONIC(winfocus) || winfocus == cur_focus))) {
4225 if (focus_default == SWM_STACK_TOP) {
4226 TAILQ_FOREACH_REVERSE(winfocus, wl, ws_win_list, entry)
4227 if (!ICONIC(winfocus) && winfocus != cur_focus)
4230 TAILQ_FOREACH(winfocus, wl, entry)
4231 if (!ICONIC(winfocus) && winfocus != cur_focus)
4238 return get_focus_magic(winfocus);
4242 get_region_focus(struct swm_region *r)
4244 struct ws_win *winfocus = NULL;
4249 if (r->ws->focus && !ICONIC(r->ws->focus))
4250 winfocus = r->ws->focus;
4251 else if (r->ws->focus_prev && !ICONIC(r->ws->focus_prev))
4252 winfocus = r->ws->focus_prev;
4254 TAILQ_FOREACH(winfocus, &r->ws->winlist, entry)
4255 if (!ICONIC(winfocus))
4258 return get_focus_magic(winfocus);
4262 focus(struct swm_region *r, union arg *args)
4264 struct ws_win *head, *cur_focus = NULL, *winfocus = NULL;
4265 struct ws_win_list *wl = NULL;
4266 struct workspace *ws = NULL;
4269 xcb_icccm_wm_hints_t hints;
4274 cur_focus = r->ws->focus;
4278 DNPRINTF(SWM_D_FOCUS, "focus: id: %d, cur_focus: %#x\n", args->id,
4281 /* Make sure an uniconified window has focus, if one exists. */
4282 if (cur_focus == NULL) {
4283 cur_focus = TAILQ_FIRST(wl);
4284 while (cur_focus != NULL && ICONIC(cur_focus))
4285 cur_focus = TAILQ_NEXT(cur_focus, entry);
4287 DNPRINTF(SWM_D_FOCUS, "focus: new cur_focus: %#x\n",
4292 case SWM_ARG_ID_FOCUSPREV:
4293 if (cur_focus == NULL)
4296 winfocus = cur_focus;
4298 winfocus = TAILQ_PREV(winfocus, ws_win_list, entry);
4299 if (winfocus == NULL)
4300 winfocus = TAILQ_LAST(wl, ws_win_list);
4301 if (winfocus == cur_focus)
4303 } while (winfocus && (ICONIC(winfocus) ||
4304 winfocus->id == cur_focus->transient ||
4305 (cur_focus->transient != XCB_WINDOW_NONE &&
4306 winfocus->transient == cur_focus->transient)));
4308 case SWM_ARG_ID_FOCUSNEXT:
4309 if (cur_focus == NULL)
4312 winfocus = cur_focus;
4314 winfocus = TAILQ_NEXT(winfocus, entry);
4315 if (winfocus == NULL)
4316 winfocus = TAILQ_FIRST(wl);
4317 if (winfocus == cur_focus)
4319 } while (winfocus && (ICONIC(winfocus) ||
4320 winfocus->id == cur_focus->transient ||
4321 (cur_focus->transient != XCB_WINDOW_NONE &&
4322 winfocus->transient == cur_focus->transient)));
4324 case SWM_ARG_ID_FOCUSMAIN:
4325 if (cur_focus == NULL)
4328 winfocus = TAILQ_FIRST(wl);
4329 if (winfocus == cur_focus)
4330 winfocus = cur_focus->ws->focus_prev;
4332 case SWM_ARG_ID_FOCUSURGENT:
4333 /* Search forward for the next urgent window. */
4337 for (i = 0; i <= workspace_limit; ++i) {
4339 head = TAILQ_FIRST(&r->s->ws[(ws->idx + i) %
4340 workspace_limit].winlist);
4342 while (head != NULL &&
4343 (head = TAILQ_NEXT(head, entry)) != NULL) {
4344 if (head == cur_focus) {
4345 winfocus = cur_focus;
4348 if (xcb_icccm_get_wm_hints_reply(conn,
4349 xcb_icccm_get_wm_hints(conn, head->id),
4350 &hints, NULL) != 0 &&
4351 xcb_icccm_wm_hints_get_urgency(&hints)) {
4357 if (winfocus != NULL)
4361 /* Switch ws if new focus is on a different ws. */
4362 if (winfocus != NULL && winfocus->ws != ws) {
4363 a.id = winfocus->ws->idx;
4371 if (clear_maximized(ws) > 0)
4374 focus_win(get_focus_magic(winfocus));
4379 DNPRINTF(SWM_D_FOCUS, "focus: done\n");
4383 cycle_layout(struct swm_region *r, union arg *args)
4385 struct workspace *ws = r->ws;
4387 /* suppress unused warning since var is needed */
4390 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
4393 if (ws->cur_layout->l_stack == NULL)
4394 ws->cur_layout = &layouts[0];
4396 clear_maximized(ws);
4401 focus_win(get_region_focus(r));
4407 stack_config(struct swm_region *r, union arg *args)
4409 struct workspace *ws = r->ws;
4411 DNPRINTF(SWM_D_STACK, "stack_config: id: %d workspace: %d\n",
4414 if (clear_maximized(ws) > 0)
4417 if (ws->cur_layout->l_config != NULL)
4418 ws->cur_layout->l_config(ws, args->id);
4420 if (args->id != SWM_ARG_ID_STACKINIT)
4429 struct swm_geometry g;
4430 struct swm_region *r, *r_prev = NULL;
4437 DNPRINTF(SWM_D_STACK, "stack: begin\n");
4439 num_screens = get_screen_count();
4440 for (i = 0; i < num_screens; i++) {
4444 TAILQ_FOREACH(r, &screens[i].rl, entry) {
4445 /* Adjust stack area for region bar and padding. */
4447 g.x += region_padding;
4448 g.y += region_padding;
4449 g.w -= 2 * border_width + 2 * region_padding;
4450 g.h -= 2 * border_width + 2 * region_padding;
4451 if (bar_enabled && r->ws->bar_enabled) {
4457 DNPRINTF(SWM_D_STACK, "stack: workspace: %d (screen: "
4458 "%d, region: %d), (x,y) WxH: (%d,%d) %d x %d\n",
4459 r->ws->idx, i, j++, g.x, g.y, g.w, g.h);
4462 /* Stack bar/input relative to prev. region. */
4463 val[1] = XCB_STACK_MODE_ABOVE;
4465 val[0] = r_prev->id;
4466 DNPRINTF(SWM_D_STACK, "stack: region input %#x "
4467 "relative to %#x.\n", r->id, val[0]);
4468 xcb_configure_window(conn, r->id,
4469 XCB_CONFIG_WINDOW_SIBLING |
4470 XCB_CONFIG_WINDOW_STACK_MODE, val);
4472 val[0] = r_prev->bar->id;
4473 DNPRINTF(SWM_D_STACK, "stack: region bar %#x "
4474 "relative to %#x.\n", r->bar->id, val[0]);
4475 xcb_configure_window(conn, r->bar->id,
4476 XCB_CONFIG_WINDOW_SIBLING |
4477 XCB_CONFIG_WINDOW_STACK_MODE, val);
4480 r->ws->cur_layout->l_stack(r->ws, &g);
4481 r->ws->cur_layout->l_string(r->ws);
4482 /* save r so we can track region changes */
4490 DNPRINTF(SWM_D_STACK, "stack: end\n");
4494 store_float_geom(struct ws_win *win)
4496 if (win == NULL || win->ws->r == NULL)
4499 /* retain window geom and region geom */
4500 win->g_float = win->g;
4501 win->g_float.x -= X(win->ws->r);
4502 win->g_float.y -= Y(win->ws->r);
4503 win->g_floatvalid = true;
4504 DNPRINTF(SWM_D_MISC, "store_float_geom: win %#x, g: (%d,%d)"
4505 " %d x %d, g_float: (%d,%d) %d x %d\n", win->id, X(win), Y(win),
4506 WIDTH(win), HEIGHT(win), win->g_float.x, win->g_float.y,
4507 win->g_float.w, win->g_float.h);
4511 load_float_geom(struct ws_win *win)
4513 if (win == NULL || win->ws->r == NULL)
4516 if (win->g_floatvalid) {
4517 win->g = win->g_float;
4518 X(win) += X(win->ws->r);
4519 Y(win) += Y(win->ws->r);
4520 DNPRINTF(SWM_D_MISC, "load_float_geom: win %#x, g: (%d,%d)"
4521 "%d x %d\n", win->id, X(win), Y(win), WIDTH(win),
4524 DNPRINTF(SWM_D_MISC, "load_float_geom: win %#x, g_float "
4525 "is not set.\n", win->id);
4530 update_floater(struct ws_win *win)
4532 struct workspace *ws;
4533 struct swm_region *r;
4540 if ((r = ws->r) == NULL)
4543 DNPRINTF(SWM_D_MISC, "update_floater: win %#x\n", win->id);
4545 win->bordered = true;
4547 if (FULLSCREEN(win)) {
4548 /* _NET_WM_FULLSCREEN: fullscreen without border. */
4549 if (!win->g_floatvalid)
4550 store_float_geom(win);
4553 win->bordered = false;
4554 } else if (MAXIMIZED(win)) {
4555 /* Maximize: like a single stacked window. */
4556 if (!win->g_floatvalid)
4557 store_float_geom(win);
4561 if (bar_enabled && ws->bar_enabled) {
4563 Y(win) += bar_height;
4564 HEIGHT(win) -= bar_height;
4565 } else if (disable_border) {
4566 win->bordered = false;
4569 if (win->bordered) {
4570 HEIGHT(win) -= 2 * border_width;
4571 WIDTH(win) -= 2 * border_width;
4574 /* Normal floating window. */
4575 /* Update geometry if new region. */
4577 load_float_geom(win);
4579 if ((win->quirks & SWM_Q_FULLSCREEN) &&
4580 WIDTH(win) >= WIDTH(r) && HEIGHT(win) >= HEIGHT(r)) {
4581 /* Remove border for FULLSCREEN quirk. */
4582 win->bordered = false;
4583 } else if (!MANUAL(win)) {
4584 if (TRANS(win) && (win->quirks & SWM_Q_TRANSSZ)) {
4585 /* Adjust size on TRANSSZ quirk. */
4586 WIDTH(win) = (double)WIDTH(r) * dialog_ratio;
4587 HEIGHT(win) = (double)HEIGHT(r) * dialog_ratio;
4590 if (!(win->quirks & SWM_Q_ANYWHERE)) {
4592 * Floaters and transients are auto-centred
4593 * unless manually moved, resized or ANYWHERE
4596 X(win) = X(r) + (WIDTH(r) - WIDTH(win)) / 2 -
4598 Y(win) = Y(r) + (HEIGHT(r) - HEIGHT(win)) / 2 -
4600 store_float_geom(win);
4605 /* Ensure at least 1 pixel of the window is in the region. */
4606 region_containment(win, r, SWM_CW_ALLSIDES);
4612 * Send keystrokes to terminal to decrease/increase the font size as the
4613 * window size changes.
4616 adjust_font(struct ws_win *win)
4618 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
4619 ABOVE(win) || TRANS(win))
4622 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
4623 WIDTH(win) / win->sh.width_inc < term_width &&
4624 win->font_steps < SWM_MAX_FONT_STEPS) {
4625 win->font_size_boundary[win->font_steps] =
4626 (win->sh.width_inc * term_width) + win->sh.base_width;
4629 win->last_inc = win->sh.width_inc;
4630 fake_keypress(win, XK_KP_Subtract, XCB_MOD_MASK_SHIFT);
4631 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
4632 WIDTH(win) > win->font_size_boundary[win->font_steps - 1]) {
4635 win->last_inc = win->sh.width_inc;
4636 fake_keypress(win, XK_KP_Add, XCB_MOD_MASK_SHIFT);
4640 #define SWAPXY(g) do { \
4642 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
4643 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
4646 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, bool flip)
4648 struct swm_geometry win_g, r_g = *g;
4650 int i, j, s, stacks;
4651 int w_inc = 1, h_inc, w_base = 1, h_base;
4652 int hrh, extra = 0, h_slice, last_h = 0;
4653 int split, colno, winno, mwin, msize, mscale;
4654 int remain, missing, v_slice;
4655 bool bordered = true, reconfigure = false;
4657 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d, rot: %s, "
4658 "flip: %s\n", ws->idx, YESNO(rot), YESNO(flip));
4660 /* Prepare tiling variables, if needed. */
4661 if ((winno = count_win(ws, false)) > 0) {
4662 /* Find first tiled window. */
4663 TAILQ_FOREACH(win, &ws->winlist, entry)
4664 if (!FLOATING(win) && !ICONIC(win))
4667 /* Take into account size hints of first tiled window. */
4669 w_inc = win->sh.width_inc;
4670 w_base = win->sh.base_width;
4671 mwin = ws->l_state.horizontal_mwin;
4672 mscale = ws->l_state.horizontal_msize;
4673 stacks = ws->l_state.horizontal_stacks;
4676 w_inc = win->sh.height_inc;
4677 w_base = win->sh.base_height;
4678 mwin = ws->l_state.vertical_mwin;
4679 mscale = ws->l_state.vertical_msize;
4680 stacks = ws->l_state.vertical_stacks;
4684 if (stacks > winno - mwin)
4685 stacks = winno - mwin;
4689 h_slice = r_g.h / SWM_H_SLICE;
4690 if (mwin && winno > mwin) {
4691 v_slice = r_g.w / SWM_V_SLICE;
4695 win_g.w = v_slice * mscale;
4697 if (w_inc > 1 && w_inc < v_slice) {
4698 /* Adjust for requested size increment. */
4699 remain = (win_g.w - w_base) % w_inc;
4705 win_g.x += r_g.w - msize;
4708 colno = split = winno / stacks;
4709 win_g.w = ((r_g.w - (stacks * 2 * border_width) +
4710 2 * border_width) / stacks);
4712 hrh = r_g.h / colno;
4713 extra = r_g.h - (colno * hrh);
4714 win_g.h = hrh - 2 * border_width;
4715 i = j = 0, s = stacks;
4718 /* Update window geometry. */
4719 TAILQ_FOREACH(win, &ws->winlist, entry) {
4723 if (FLOATING(win)) {
4724 update_floater(win);
4729 if (split && i == split) {
4730 colno = (winno - mwin) / stacks;
4731 if (s <= (winno - mwin) % stacks)
4734 hrh = r_g.h / colno;
4735 extra = r_g.h - (colno * hrh);
4738 win_g.x += win_g.w + 2 * border_width +
4741 win_g.w = (r_g.w - msize -
4742 (stacks * (2 * border_width + tile_gap))) / stacks;
4744 win_g.w += (r_g.w - msize -
4745 (stacks * (2 * border_width + tile_gap))) %
4749 win_g.x -= win_g.w + 2 * border_width +
4755 win_g.h = hrh - 2 * border_width - tile_gap;
4758 h_inc = win->sh.width_inc;
4759 h_base = win->sh.base_width;
4761 h_inc = win->sh.height_inc;
4762 h_base = win->sh.base_height;
4765 if (j == colno - 1) {
4766 win_g.h = hrh + extra;
4767 } else if (h_inc > 1 && h_inc < h_slice) {
4768 /* adjust for window's requested size increment */
4769 remain = (win_g.h - h_base) % h_inc;
4770 missing = h_inc - remain;
4772 if (missing <= extra || j == 0) {
4784 win_g.y += last_h + 2 * border_width + tile_gap;
4786 if (disable_border && !(bar_enabled && ws->bar_enabled) &&
4789 win_g.w += 2 * border_width;
4790 win_g.h += 2 * border_width;
4796 if (X(win) != win_g.y || Y(win) != win_g.x ||
4797 WIDTH(win) != win_g.h || HEIGHT(win) != win_g.w) {
4801 WIDTH(win) = win_g.h;
4802 HEIGHT(win) = win_g.w;
4805 if (X(win) != win_g.x || Y(win) != win_g.y ||
4806 WIDTH(win) != win_g.w || HEIGHT(win) != win_g.h) {
4810 WIDTH(win) = win_g.w;
4811 HEIGHT(win) = win_g.h;
4815 if (bordered != win->bordered) {
4817 win->bordered = bordered;
4830 /* Stack all windows from bottom up. */
4831 TAILQ_FOREACH_REVERSE(win, &ws->stack, ws_win_stack, stack_entry)
4833 update_win_stacking(win);
4835 /* Map all windows from top down. */
4836 TAILQ_FOREACH(win, &ws->stack, stack_entry)
4840 DNPRINTF(SWM_D_STACK, "stack_master: done\n");
4844 vertical_config(struct workspace *ws, int id)
4846 DNPRINTF(SWM_D_STACK, "vertical_config: id: %d, workspace: %d\n",
4850 case SWM_ARG_ID_STACKRESET:
4851 case SWM_ARG_ID_STACKINIT:
4852 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
4853 ws->l_state.vertical_mwin = 1;
4854 ws->l_state.vertical_stacks = 1;
4856 case SWM_ARG_ID_MASTERSHRINK:
4857 if (ws->l_state.vertical_msize > 1)
4858 ws->l_state.vertical_msize--;
4860 case SWM_ARG_ID_MASTERGROW:
4861 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
4862 ws->l_state.vertical_msize++;
4864 case SWM_ARG_ID_MASTERADD:
4865 ws->l_state.vertical_mwin++;
4867 case SWM_ARG_ID_MASTERDEL:
4868 if (ws->l_state.vertical_mwin > 0)
4869 ws->l_state.vertical_mwin--;
4871 case SWM_ARG_ID_STACKINC:
4872 ws->l_state.vertical_stacks++;
4874 case SWM_ARG_ID_STACKDEC:
4875 if (ws->l_state.vertical_stacks > 1)
4876 ws->l_state.vertical_stacks--;
4878 case SWM_ARG_ID_FLIPLAYOUT:
4879 ws->l_state.vertical_flip = !ws->l_state.vertical_flip;
4887 vertical_stack(struct workspace *ws, struct swm_geometry *g)
4889 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
4891 stack_master(ws, g, 0, ws->l_state.vertical_flip);
4895 horizontal_config(struct workspace *ws, int id)
4897 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
4900 case SWM_ARG_ID_STACKRESET:
4901 case SWM_ARG_ID_STACKINIT:
4902 ws->l_state.horizontal_mwin = 1;
4903 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
4904 ws->l_state.horizontal_stacks = 1;
4906 case SWM_ARG_ID_MASTERSHRINK:
4907 if (ws->l_state.horizontal_msize > 1)
4908 ws->l_state.horizontal_msize--;
4910 case SWM_ARG_ID_MASTERGROW:
4911 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
4912 ws->l_state.horizontal_msize++;
4914 case SWM_ARG_ID_MASTERADD:
4915 ws->l_state.horizontal_mwin++;
4917 case SWM_ARG_ID_MASTERDEL:
4918 if (ws->l_state.horizontal_mwin > 0)
4919 ws->l_state.horizontal_mwin--;
4921 case SWM_ARG_ID_STACKINC:
4922 ws->l_state.horizontal_stacks++;
4924 case SWM_ARG_ID_STACKDEC:
4925 if (ws->l_state.horizontal_stacks > 1)
4926 ws->l_state.horizontal_stacks--;
4928 case SWM_ARG_ID_FLIPLAYOUT:
4929 ws->l_state.horizontal_flip = !ws->l_state.horizontal_flip;
4937 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
4939 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
4941 stack_master(ws, g, 1, ws->l_state.horizontal_flip);
4944 /* fullscreen view */
4946 max_stack(struct workspace *ws, struct swm_geometry *g)
4948 struct swm_geometry gg = *g;
4949 struct ws_win *w, *win = NULL, *parent = NULL, *tmpw;
4952 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
4957 winno = count_win(ws, false);
4958 if (winno == 0 && count_win(ws, true) == 0)
4961 /* Figure out which top level window should be visible. */
4962 if (ws->focus_pending)
4963 win = ws->focus_pending;
4966 else if (ws->focus_prev)
4967 win = ws->focus_prev;
4969 win = TAILQ_FIRST(&ws->winlist);
4971 DNPRINTF(SWM_D_STACK, "max_stack: focus_pending: %#x, focus: %#x, "
4972 "focus_prev: %#x, first: %#x, win: %#x\n", WINID(ws->focus_pending),
4973 WINID(ws->focus), WINID(ws->focus_prev),
4974 WINID(TAILQ_FIRST(&ws->winlist)), win->id);
4976 /* Update window geometry. */
4977 TAILQ_FOREACH(w, &ws->winlist, entry) {
4986 /* Set maximized flag for all maxed windows. */
4987 if (!MAXIMIZED(w)) {
4988 /* Preserve floating geometry. */
4990 store_float_geom(w);
4992 ewmh_apply_flags(w, w->ewmh_flags | EWMH_F_MAXIMIZED);
4993 ewmh_update_wm_state(w);
4996 /* Only reconfigure if necessary. */
4997 if (X(w) != gg.x || Y(w) != gg.y || WIDTH(w) != gg.w ||
4998 HEIGHT(w) != gg.h) {
5000 if (bar_enabled && ws->bar_enabled){
5003 w->bordered = false;
5004 WIDTH(w) += 2 * border_width;
5005 HEIGHT(w) += 2 * border_width;
5012 /* If transient, stack parent and its children. */
5013 if (TRANS(win) && (parent = find_window(win->transient))) {
5014 raise_window(parent);
5016 TAILQ_FOREACH_SAFE(w, &ws->stack, stack_entry, tmpw)
5017 if (w->transient == parent->id)
5021 /* Make sure focus window is on top. */
5024 /* Stack any children of focus window. */
5025 TAILQ_FOREACH_SAFE(w, &ws->stack, stack_entry, tmpw)
5026 if (w->transient == win->id)
5029 /* Map all windows. */
5030 TAILQ_FOREACH(w, &ws->stack, stack_entry)
5036 send_to_rg(struct swm_region *r, union arg *args)
5038 int ridx = args->id, i, num_screens;
5039 struct swm_region *rr = NULL;
5042 num_screens = get_screen_count();
5043 /* do nothing if we don't have more than one screen */
5044 if (!(num_screens > 1 || outputs > 1))
5047 DNPRINTF(SWM_D_FOCUS, "send_to_rg: id: %d\n", ridx);
5049 rr = TAILQ_FIRST(&r->s->rl);
5050 for (i = 0; i < ridx && rr != NULL; ++i)
5051 rr = TAILQ_NEXT(rr, entry);
5062 region_under(struct swm_screen *s, int x, int y)
5064 struct swm_region *r = NULL;
5066 TAILQ_FOREACH(r, &s->rl, entry) {
5067 DNPRINTF(SWM_D_MISC, "region_under: ws: %d, region g: (%d,%d) "
5068 "%d x %d, coords: (%d,%d)\n", r->ws->idx, X(r), Y(r),
5069 WIDTH(r), HEIGHT(r), x, y);
5070 if (X(r) <= x && x < MAX_X(r))
5071 if (Y(r) <= y && y < MAX_Y(r))
5078 /* Transfer focused window to target workspace and focus. */
5080 send_to_ws(struct swm_region *r, union arg *args)
5082 int wsid = args->id;
5083 struct ws_win *win = NULL;
5085 if (r && r->ws && r->ws->focus)
5090 DNPRINTF(SWM_D_MOVE, "send_to_ws: win %#x, ws %d\n", win->id, wsid);
5092 if (wsid < 0 || wsid >= workspace_limit)
5095 if (win->ws->idx == wsid)
5098 win_to_ws(win, wsid, true);
5100 /* Set new focus on target ws. */
5101 if (focus_mode != SWM_FOCUS_FOLLOW) {
5102 win->ws->focus_prev = win->ws->focus;
5103 win->ws->focus = win;
5104 win->ws->focus_pending = NULL;
5106 if (win->ws->focus_prev)
5107 update_window_color(win->ws->focus_prev);
5110 DNPRINTF(SWM_D_STACK, "send_to_ws: focus_pending: %#x, focus: %#x, "
5111 "focus_prev: %#x, first: %#x, win: %#x\n",
5112 WINID(r->ws->focus_pending), WINID(r->ws->focus),
5113 WINID(r->ws->focus_prev), WINID(TAILQ_FIRST(&r->ws->winlist)),
5116 ewmh_apply_flags(win, win->ewmh_flags & ~EWMH_F_MAXIMIZED);
5117 ewmh_update_wm_state(win);
5119 /* Restack and set new focus on current ws. */
5121 load_float_geom(win);
5125 if (focus_mode != SWM_FOCUS_FOLLOW) {
5126 if (r->ws->focus != NULL) {
5127 focus_win(r->ws->focus);
5129 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT, r->id,
5140 win_to_ws(struct ws_win *win, int wsid, bool unfocus)
5142 struct ws_win *parent;
5143 struct workspace *ws, *nws, *pws;
5145 if (wsid < 0 || wsid >= workspace_limit)
5148 if (win->ws->idx == wsid)
5152 nws = &win->s->ws[wsid];
5154 DNPRINTF(SWM_D_MOVE, "win_to_ws: win %#x, ws %d -> %d\n", win->id,
5157 /* Cleanup focus on source ws. */
5158 if (focus_mode != SWM_FOCUS_FOLLOW &&
5159 (ws->focus == win || ws->focus_pending == win))
5160 ws->focus_pending = get_focus_prev(win);
5162 /* Move the parent if this is a transient window. */
5164 parent = find_window(win->transient);
5167 /* Set new focus in parent's ws if needed. */
5168 if (pws->focus == parent) {
5169 if (focus_mode != SWM_FOCUS_FOLLOW)
5170 pws->focus_pending =
5171 get_focus_prev(parent);
5173 unfocus_win(parent);
5175 if (focus_mode != SWM_FOCUS_FOLLOW) {
5176 pws->focus = pws->focus_pending;
5177 pws->focus_pending = NULL;
5181 /* Don't unmap parent if new ws is visible */
5183 unmap_window(parent);
5186 TAILQ_REMOVE(&ws->winlist, parent, entry);
5187 TAILQ_REMOVE(&ws->stack, parent, stack_entry);
5188 TAILQ_INSERT_TAIL(&nws->winlist, parent, entry);
5189 TAILQ_INSERT_TAIL(&nws->stack, parent, stack_entry);
5192 DNPRINTF(SWM_D_PROP, "win_to_ws: set property: "
5193 "_NET_WM_DESKTOP: %d\n", wsid);
5194 xcb_change_property(conn, XCB_PROP_MODE_REPLACE,
5195 parent->id, ewmh[_NET_WM_DESKTOP].atom,
5196 XCB_ATOM_CARDINAL, 32, 1, &wsid);
5203 if (ws->focus_prev == win)
5204 ws->focus_prev = NULL;
5206 if (focus_mode != SWM_FOCUS_FOLLOW && ws->focus_pending != NULL) {
5207 ws->focus = ws->focus_pending;
5208 ws->focus_pending = NULL;
5211 /* Don't unmap if new ws is visible */
5216 TAILQ_REMOVE(&ws->winlist, win, entry);
5217 TAILQ_REMOVE(&ws->stack, win, stack_entry);
5218 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
5219 TAILQ_INSERT_TAIL(&nws->stack, win, stack_entry);
5222 /* Update the window's workspace property: _NET_WM_DESKTOP */
5223 DNPRINTF(SWM_D_PROP, "win_to_ws: set property: "
5224 "_NET_WM_DESKTOP: %d\n", wsid);
5225 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
5226 ewmh[_NET_WM_DESKTOP].atom, XCB_ATOM_CARDINAL, 32, 1, &wsid);
5228 ewmh_update_client_list();
5230 DNPRINTF(SWM_D_MOVE, "win_to_ws: done.\n");
5234 pressbutton(struct swm_region *r, union arg *args)
5236 /* suppress unused warning since var is needed */
5239 xcb_test_fake_input(conn, XCB_BUTTON_PRESS, args->id,
5240 XCB_CURRENT_TIME, XCB_WINDOW_NONE, 0, 0, 0);
5241 xcb_test_fake_input(conn, XCB_BUTTON_RELEASE, args->id,
5242 XCB_CURRENT_TIME, XCB_WINDOW_NONE, 0, 0, 0);
5246 raise_toggle(struct swm_region *r, union arg *args)
5248 /* suppress unused warning since var is needed */
5251 if (r == NULL || r->ws == NULL)
5254 if (r->ws->focus && MAXIMIZED(r->ws->focus))
5257 r->ws->always_raise = !r->ws->always_raise;
5259 /* bring floaters back to top */
5260 if (!r->ws->always_raise)
5267 iconify(struct swm_region *r, union arg *args)
5270 /* suppress unused warning since var is needed */
5273 if ((w = r->ws->focus) == NULL)
5276 ewmh_apply_flags(w, w->ewmh_flags | EWMH_F_HIDDEN);
5277 ewmh_update_wm_state(w);
5285 get_win_name(xcb_window_t win)
5288 xcb_get_property_cookie_t c;
5289 xcb_get_property_reply_t *r;
5291 /* First try _NET_WM_NAME for UTF-8. */
5292 c = xcb_get_property(conn, 0, win, ewmh[_NET_WM_NAME].atom,
5293 XCB_GET_PROPERTY_TYPE_ANY, 0, UINT_MAX);
5294 r = xcb_get_property_reply(conn, c, NULL);
5295 if (r && r->type == XCB_NONE) {
5297 /* Use WM_NAME instead; no UTF-8. */
5298 c = xcb_get_property(conn, 0, win, XCB_ATOM_WM_NAME,
5299 XCB_GET_PROPERTY_TYPE_ANY, 0, UINT_MAX);
5300 r = xcb_get_property_reply(conn, c, NULL);
5303 if (r && r->type != XCB_NONE && r->length > 0)
5304 name = strndup(xcb_get_property_value(r),
5305 xcb_get_property_value_length(r));
5310 err(1, "get_win_name: failed to allocate memory.");
5318 uniconify(struct swm_region *r, union arg *args)
5325 DNPRINTF(SWM_D_MISC, "uniconify\n");
5327 if (r == NULL || r->ws == NULL)
5330 /* make sure we have anything to uniconify */
5331 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
5332 if (win->ws == NULL)
5333 continue; /* should never happen */
5339 DNPRINTF(SWM_D_MISC, "uniconify: count: %d\n", count);
5345 search_resp_action = SWM_SEARCH_UNICONIFY;
5347 spawn_select(r, args, "search", &searchpid);
5349 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
5352 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
5353 if (win->ws == NULL)
5354 continue; /* should never happen */
5358 name = get_win_name(win->id);
5359 fprintf(lfile, "%s.%u\n", name, win->id);
5367 name_workspace(struct swm_region *r, union arg *args)
5371 DNPRINTF(SWM_D_MISC, "name_workspace\n");
5377 search_resp_action = SWM_SEARCH_NAME_WORKSPACE;
5379 spawn_select(r, args, "name_workspace", &searchpid);
5381 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
5384 fprintf(lfile, "%s", "");
5389 search_workspace(struct swm_region *r, union arg *args)
5392 struct workspace *ws;
5395 DNPRINTF(SWM_D_MISC, "search_workspace\n");
5401 search_resp_action = SWM_SEARCH_SEARCH_WORKSPACE;
5403 spawn_select(r, args, "search", &searchpid);
5405 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
5408 for (i = 0; i < workspace_limit; i++) {
5412 fprintf(lfile, "%d%s%s\n", ws->idx + 1,
5413 (ws->name ? ":" : ""), (ws->name ? ws->name : ""));
5420 search_win_cleanup(void)
5422 struct search_window *sw = NULL;
5424 while ((sw = TAILQ_FIRST(&search_wl)) != NULL) {
5425 xcb_destroy_window(conn, sw->indicator);
5426 TAILQ_REMOVE(&search_wl, sw, entry);
5432 search_win(struct swm_region *r, union arg *args)
5434 struct ws_win *win = NULL;
5435 struct search_window *sw = NULL;
5438 xcb_screen_t *screen;
5439 int i, width, height;
5447 XRectangle l_ibox, l_lbox;
5449 DNPRINTF(SWM_D_MISC, "search_win\n");
5452 search_resp_action = SWM_SEARCH_SEARCH_WINDOW;
5454 spawn_select(r, args, "search", &searchpid);
5456 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
5459 if ((screen = get_screen(r->s->idx)) == NULL)
5460 errx(1, "ERROR: can't get screen %d.", r->s->idx);
5462 TAILQ_INIT(&search_wl);
5465 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
5469 sw = calloc(1, sizeof(struct search_window));
5471 warn("search_win: calloc");
5473 search_win_cleanup();
5479 snprintf(s, sizeof s, "%d", i);
5482 w = xcb_generate_id(conn);
5483 wa[0] = r->s->c[SWM_S_COLOR_FOCUS].pixel;
5484 wa[1] = r->s->c[SWM_S_COLOR_UNFOCUS].pixel;
5485 wa[2] = screen->default_colormap;
5487 if (bar_font_legacy) {
5488 XmbTextExtents(bar_fs, s, len, &l_ibox, &l_lbox);
5489 width = l_lbox.width + 4;
5490 height = bar_fs_extents->max_logical_extent.height + 4;
5492 XftTextExtentsUtf8(display, bar_font, (FcChar8 *)s, len,
5494 width = info.width + 4;
5495 height = bar_font->height + 4;
5498 xcb_create_window(conn, screen->root_depth, w, win->id, 0, 0,
5499 width, height, 1, XCB_WINDOW_CLASS_INPUT_OUTPUT,
5500 screen->root_visual, XCB_CW_BACK_PIXEL |
5501 XCB_CW_BORDER_PIXEL | XCB_CW_COLORMAP, wa);
5503 xcb_map_window(conn, w);
5506 TAILQ_INSERT_TAIL(&search_wl, sw, entry);
5508 if (bar_font_legacy) {
5509 l_gcv.graphics_exposures = 0;
5510 l_draw = XCreateGC(display, w, 0, &l_gcv);
5512 XSetForeground(display, l_draw,
5513 r->s->c[SWM_S_COLOR_BAR].pixel);
5515 DRAWSTRING(display, w, bar_fs, l_draw, 2,
5516 (bar_fs_extents->max_logical_extent.height -
5517 l_lbox.height) / 2 - l_lbox.y, s, len);
5519 XFreeGC(display, l_draw);
5522 draw = XftDrawCreate(display, w,
5523 DefaultVisual(display, r->s->idx),
5524 DefaultColormap(display, r->s->idx));
5526 XftDrawStringUtf8(draw, &search_font_color, bar_font, 2,
5527 (HEIGHT(r->bar) + bar_font->height) / 2 -
5528 bar_font->descent, (FcChar8 *)s, len);
5530 XftDrawDestroy(draw);
5533 DNPRINTF(SWM_D_MISC, "search_win: mapped win %#x\n", w);
5535 fprintf(lfile, "%d\n", i);
5545 search_resp_uniconify(const char *resp, size_t len)
5551 DNPRINTF(SWM_D_MISC, "search_resp_uniconify: resp: %s\n", resp);
5553 TAILQ_FOREACH(win, &search_r->ws->winlist, entry) {
5556 name = get_win_name(win->id);
5557 if (asprintf(&s, "%s.%u", name, win->id) == -1) {
5562 if (strncmp(s, resp, len) == 0) {
5563 /* XXX this should be a callback to generalize */
5564 ewmh_apply_flags(win, win->ewmh_flags & ~EWMH_F_HIDDEN);
5565 ewmh_update_wm_state(win);
5575 search_resp_name_workspace(const char *resp, size_t len)
5577 struct workspace *ws;
5579 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: resp: %s\n", resp);
5581 if (search_r->ws == NULL)
5586 free(search_r->ws->name);
5587 search_r->ws->name = NULL;
5591 ws->name = strdup(resp);
5592 if (ws->name == NULL) {
5593 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: "
5594 "strdup: %s", strerror(errno));
5598 ewmh_update_desktop_names();
5599 ewmh_get_desktop_names();
5604 ewmh_update_desktop_names(void)
5606 char *name_list = NULL, *p;
5607 int num_screens, i, j, len = 0, tot = 0;
5609 num_screens = get_screen_count();
5610 for (i = 0; i < num_screens; ++i) {
5611 for (j = 0; j < workspace_limit; ++j) {
5612 if (screens[i].ws[j].name != NULL)
5613 len += strlen(screens[i].ws[j].name);
5617 if((name_list = calloc(len, sizeof(char))) == NULL)
5618 err(1, "update_desktop_names: calloc: failed to "
5619 "allocate memory.");
5622 for (j = 0; j < workspace_limit; ++j) {
5623 if (screens[i].ws[j].name != NULL) {
5624 len = strlen(screens[i].ws[j].name);
5625 memcpy(p, screens[i].ws[j].name, len);
5634 xcb_change_property(conn, XCB_PROP_MODE_REPLACE,
5635 screens[i].root, ewmh[_NET_DESKTOP_NAMES].atom,
5636 a_utf8_string, 8, tot, name_list);
5646 ewmh_get_desktop_names(void)
5649 xcb_get_property_cookie_t c;
5650 xcb_get_property_reply_t *r;
5651 int num_screens, i, j, n, k;
5653 num_screens = get_screen_count();
5654 for (i = 0; i < num_screens; ++i) {
5655 for (j = 0; j < workspace_limit; ++j) {
5656 free(screens[i].ws[j].name);
5657 screens[i].ws[j].name = NULL;
5660 c = xcb_get_property(conn, 0, screens[i].root,
5661 ewmh[_NET_DESKTOP_NAMES].atom,
5662 a_utf8_string, 0, UINT32_MAX);
5663 r = xcb_get_property_reply(conn, c, NULL);
5667 names = xcb_get_property_value(r);
5668 n = xcb_get_property_value_length(r);
5670 for (j = 0, k = 0; j < n; ++j) {
5671 if (*(names + j) != '\0') {
5672 screens[i].ws[k].name = strdup(names + j);
5673 j += strlen(names + j);
5682 ewmh_update_client_list(void)
5685 int num_screens, i, j, k = 0, count = 0;
5688 num_screens = get_screen_count();
5689 for (i = 0; i < num_screens; ++i) {
5690 for (j = 0; j < workspace_limit; ++j)
5691 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
5694 DNPRINTF(SWM_D_PROP, "ewmh_update_client_list: win count: %d\n",
5700 wins = calloc(count, sizeof(xcb_window_t));
5702 err(1, "ewmh_update_client_list: calloc: failed to "
5703 "allocate memory.");
5705 for (j = 0, k = 0; j < workspace_limit; ++j)
5706 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
5707 wins[k++] = win->id;
5709 xcb_change_property(conn, XCB_PROP_MODE_REPLACE,
5710 screens[i].root, ewmh[_NET_CLIENT_LIST].atom,
5711 XCB_ATOM_WINDOW, 32, count, wins);
5718 ewmh_update_current_desktop(void)
5722 num_screens = get_screen_count();
5723 for (i = 0; i < num_screens; ++i)
5724 xcb_change_property(conn, XCB_PROP_MODE_REPLACE,
5725 screens[i].root, ewmh[_NET_CURRENT_DESKTOP].atom,
5726 XCB_ATOM_CARDINAL, 32, 1, &screens[i].r_focus->ws->idx);
5730 ewmh_update_desktops(void)
5732 int num_screens, i, j;
5735 vals = calloc(workspace_limit * 2, sizeof(uint32_t));
5737 err(1, "ewmh_update_desktops: calloc: failed to allocate "
5740 num_screens = get_screen_count();
5741 for (i = 0; i < num_screens; i++) {
5742 xcb_change_property(conn, XCB_PROP_MODE_REPLACE,
5743 screens[i].root, ewmh[_NET_NUMBER_OF_DESKTOPS].atom,
5744 XCB_ATOM_CARDINAL, 32, 1, &workspace_limit);
5746 for (j = 0; j < workspace_limit; ++j) {
5747 if (screens[i].ws[j].r != NULL) {
5748 vals[j * 2] = X(screens[i].ws[j].r);
5749 vals[j * 2 + 1] = Y(screens[i].ws[j].r);
5750 } else if (screens[i].ws[j].old_r != NULL) {
5751 vals[j * 2] = X(screens[i].ws[j].old_r);
5752 vals[j * 2 + 1] = Y(screens[i].ws[j].old_r);
5754 vals[j * 2] = vals[j * 2 + 1] = 0;
5758 xcb_change_property(conn, XCB_PROP_MODE_REPLACE,
5759 screens[i].root, ewmh[_NET_DESKTOP_VIEWPORT].atom,
5760 XCB_ATOM_CARDINAL, 32, workspace_limit * 2, vals);
5767 search_resp_search_workspace(const char *resp)
5774 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: resp: %s\n", resp);
5778 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: strdup: %s",
5785 ws_idx = (int)strtonum(q, 1, workspace_limit, &errstr);
5787 DNPRINTF(SWM_D_MISC, "workspace idx is %s: %s",
5794 switchws(search_r, &a);
5798 search_resp_search_window(const char *resp)
5803 struct search_window *sw;
5805 DNPRINTF(SWM_D_MISC, "search_resp_search_window: resp: %s\n", resp);
5809 DNPRINTF(SWM_D_MISC, "search_resp_search_window: strdup: %s",
5814 idx = (int)strtonum(s, 1, INT_MAX, &errstr);
5816 DNPRINTF(SWM_D_MISC, "window idx is %s: %s",
5823 TAILQ_FOREACH(sw, &search_wl, entry)
5824 if (idx == sw->idx) {
5830 #define MAX_RESP_LEN 1024
5833 search_do_resp(void)
5839 DNPRINTF(SWM_D_MISC, "search_do_resp:\n");
5844 if ((resp = calloc(1, MAX_RESP_LEN + 1)) == NULL) {
5845 warn("search: calloc");
5849 rbytes = read(select_resp_pipe[0], resp, MAX_RESP_LEN);
5851 warn("search: read error");
5854 resp[rbytes] = '\0';
5857 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
5858 * newline, so work around that by sanitizing the resp now.
5860 resp[strcspn(resp, "\n")] = '\0';
5863 switch (search_resp_action) {
5864 case SWM_SEARCH_UNICONIFY:
5865 search_resp_uniconify(resp, len);
5867 case SWM_SEARCH_NAME_WORKSPACE:
5868 search_resp_name_workspace(resp, len);
5870 case SWM_SEARCH_SEARCH_WORKSPACE:
5871 search_resp_search_workspace(resp);
5873 case SWM_SEARCH_SEARCH_WINDOW:
5874 search_resp_search_window(resp);
5879 if (search_resp_action == SWM_SEARCH_SEARCH_WINDOW)
5880 search_win_cleanup();
5882 search_resp_action = SWM_SEARCH_NONE;
5883 close(select_resp_pipe[0]);
5890 wkill(struct swm_region *r, union arg *args)
5892 DNPRINTF(SWM_D_MISC, "wkill: win %#x, id: %d\n", WINID(r->ws->focus),
5895 if (r->ws->focus == NULL)
5898 if (args->id == SWM_ARG_ID_KILLWINDOW)
5899 xcb_kill_client(conn, r->ws->focus->id);
5901 if (r->ws->focus->can_delete)
5902 client_msg(r->ws->focus, a_delete, 0);
5908 clear_maximized(struct workspace *ws)
5913 /* Clear any maximized win(s) on ws, from bottom up. */
5914 TAILQ_FOREACH_REVERSE(w, &ws->stack, ws_win_stack, stack_entry)
5916 ewmh_apply_flags(w, w->ewmh_flags & ~EWMH_F_MAXIMIZED);
5917 ewmh_update_wm_state(w);
5925 maximize_toggle(struct swm_region *r, union arg *args)
5927 struct ws_win *w = r->ws->focus;
5929 /* suppress unused warning since var is needed */
5935 DNPRINTF(SWM_D_MISC, "maximize_toggle: win %#x\n", w->id);
5940 if (w->ws->cur_layout == &layouts[SWM_MAX_STACK])
5943 ewmh_apply_flags(w, w->ewmh_flags ^ EWMH_F_MAXIMIZED);
5944 ewmh_update_wm_state(w);
5948 if (w == w->ws->focus)
5952 DNPRINTF(SWM_D_MISC, "maximize_toggle: done\n");
5956 floating_toggle(struct swm_region *r, union arg *args)
5958 struct ws_win *w = r->ws->focus;
5960 /* suppress unused warning since var is needed */
5966 DNPRINTF(SWM_D_MISC, "floating_toggle: win %#x\n", w->id);
5968 if (FULLSCREEN(w) || TRANS(w))
5971 if (w->ws->cur_layout == &layouts[SWM_MAX_STACK])
5974 ewmh_apply_flags(w, w->ewmh_flags ^ EWMH_F_ABOVE);
5975 ewmh_update_wm_state(w);
5979 if (w == w->ws->focus)
5984 DNPRINTF(SWM_D_MISC, "floating_toggle: done\n");
5988 region_containment(struct ws_win *win, struct swm_region *r, int opts)
5990 struct swm_geometry g = r->g;
5991 int rt, lt, tp, bm, bw;
5993 bw = (opts & SWM_CW_SOFTBOUNDARY) ? boundary_width : 0;
5996 * Perpendicular distance of each side of the window to the respective
5997 * side of the region boundary. Positive values indicate the side of
5998 * the window has passed beyond the region boundary.
6000 rt = opts & SWM_CW_RIGHT ? MAX_X(win) + BORDER(win) - MAX_X(r) : bw;
6001 lt = opts & SWM_CW_LEFT ? X(r) - X(win) + BORDER(win) : bw;
6002 bm = opts & SWM_CW_BOTTOM ? MAX_Y(win) + BORDER(win) - MAX_Y(r) : bw;
6003 tp = opts & SWM_CW_TOP ? Y(r) - Y(win) + BORDER(win) : bw;
6005 DNPRINTF(SWM_D_MISC, "region_containment: win %#x, rt: %d, lt: %d, "
6006 "bm: %d, tp: %d, SOFTBOUNDARY: %s, HARDBOUNDARY: %s\n", win->id, rt,
6007 lt, bm, tp, YESNO(opts & SWM_CW_SOFTBOUNDARY),
6008 YESNO(opts & SWM_CW_HARDBOUNDARY));
6011 * Disable containment if any of the flagged sides went beyond the
6012 * containment boundary, or if containment is disabled.
6014 if (!(opts & SWM_CW_HARDBOUNDARY || opts & SWM_CW_SOFTBOUNDARY) ||
6015 (bw != 0 && ((rt > bw) || (lt > bw) || (bm > bw) || (tp > bw)))) {
6016 /* Make sure window has at least 1 pixel in the region */
6017 g.x += 1 - WIDTH(win);
6018 g.y += 1 - HEIGHT(win);
6019 g.w += 2 * WIDTH(win) - 2;
6020 g.h += 2 * HEIGHT(win) - 2;
6023 constrain_window(win, &g, &opts);
6026 /* Move or resize a window so that flagged side(s) fit into the supplied box. */
6028 constrain_window(struct ws_win *win, struct swm_geometry *b, int *opts)
6030 DNPRINTF(SWM_D_MISC, "constrain_window: win %#x, (x,y) w x h: "
6031 "(%d,%d) %d x %d, box: (x,y) w x h: (%d,%d) %d x %d, rt: %s, "
6032 "lt: %s, bt: %s, tp: %s, allow resize: %s\n", win->id, X(win),
6033 Y(win), WIDTH(win), HEIGHT(win), b->x, b->y, b->w, b->h,
6034 YESNO(*opts & SWM_CW_RIGHT), YESNO(*opts & SWM_CW_LEFT),
6035 YESNO(*opts & SWM_CW_BOTTOM), YESNO(*opts & SWM_CW_TOP),
6036 YESNO(*opts & SWM_CW_RESIZABLE));
6038 if ((*opts & SWM_CW_RIGHT) && MAX_X(win) + BORDER(win) > b->x + b->w) {
6039 if (*opts & SWM_CW_RESIZABLE)
6040 WIDTH(win) = b->x + b->w - X(win) - BORDER(win);
6042 X(win) = b->x + b->w - WIDTH(win) - BORDER(win);
6045 if ((*opts & SWM_CW_LEFT) && X(win) + BORDER(win) < b->x) {
6046 if (*opts & SWM_CW_RESIZABLE)
6047 WIDTH(win) -= b->x - X(win) - BORDER(win);
6049 X(win) = b->x - BORDER(win);
6052 if ((*opts & SWM_CW_BOTTOM) && MAX_Y(win) + BORDER(win) > b->y + b->h) {
6053 if (*opts & SWM_CW_RESIZABLE)
6054 HEIGHT(win) = b->y + b->h - Y(win) - BORDER(win);
6056 Y(win) = b->y + b->h - HEIGHT(win) - BORDER(win);
6059 if ((*opts & SWM_CW_TOP) && Y(win) + BORDER(win) < b->y) {
6060 if (*opts & SWM_CW_RESIZABLE)
6061 HEIGHT(win) -= b->y - Y(win) - BORDER(win);
6063 Y(win) = b->y - BORDER(win);
6066 if (*opts & SWM_CW_RESIZABLE) {
6069 if (HEIGHT(win) < 1)
6075 update_window_color(struct ws_win *win)
6079 if (WS_FOCUSED(win->ws) && win->ws->focus == win)
6080 pixel = MAXIMIZED(win) ?
6081 &win->s->c[SWM_S_COLOR_FOCUS_MAXIMIZED].pixel :
6082 &win->s->c[SWM_S_COLOR_FOCUS].pixel;
6084 pixel = MAXIMIZED(win) ?
6085 &win->s->c[SWM_S_COLOR_UNFOCUS_MAXIMIZED].pixel :
6086 &win->s->c[SWM_S_COLOR_UNFOCUS].pixel;
6088 xcb_change_window_attributes(conn, win->id,
6089 XCB_CW_BORDER_PIXEL, pixel);
6093 update_window(struct ws_win *win)
6098 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
6099 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
6100 XCB_CONFIG_WINDOW_BORDER_WIDTH;
6104 wc[3] = HEIGHT(win);
6105 wc[4] = BORDER(win);
6107 DNPRINTF(SWM_D_EVENT, "update_window: win %#x, (x,y) w x h: "
6108 "(%d,%d) %d x %d, bordered: %s\n", win->id, wc[0], wc[1], wc[2],
6109 wc[3], YESNO(win->bordered));
6111 xcb_configure_window(conn, win->id, mask, wc);
6114 #define SWM_RESIZE_STEPS (50)
6117 resize(struct ws_win *win, union arg *args)
6119 xcb_timestamp_t timestamp = 0;
6120 struct swm_region *r = NULL;
6121 struct swm_geometry g;
6123 int top = 0, left = 0;
6125 xcb_cursor_t cursor;
6126 xcb_query_pointer_reply_t *xpr = NULL;
6127 xcb_generic_event_t *evt;
6128 xcb_motion_notify_event_t *mne;
6135 if (FULLSCREEN(win))
6138 /* In max_stack mode, should only resize transients. */
6139 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !TRANS(win))
6142 DNPRINTF(SWM_D_EVENT, "resize: win %#x, floating: %s, "
6143 "transient: %#x\n", win->id, YESNO(ABOVE(win)),
6147 store_float_geom(win);
6148 else if (!(TRANS(win) || ABOVE(win)))
6151 ewmh_apply_flags(win, (win->ewmh_flags | SWM_F_MANUAL | EWMH_F_ABOVE) &
6153 ewmh_update_wm_state(win);
6159 /* It's possible for win to have been freed during focus_flush(). */
6160 if (validate_win(win)) {
6161 DNPRINTF(SWM_D_EVENT, "resize: invalid win.\n");
6166 case SWM_ARG_ID_WIDTHSHRINK:
6167 WIDTH(win) -= SWM_RESIZE_STEPS;
6170 case SWM_ARG_ID_WIDTHGROW:
6171 WIDTH(win) += SWM_RESIZE_STEPS;
6174 case SWM_ARG_ID_HEIGHTSHRINK:
6175 HEIGHT(win) -= SWM_RESIZE_STEPS;
6178 case SWM_ARG_ID_HEIGHTGROW:
6179 HEIGHT(win) += SWM_RESIZE_STEPS;
6186 region_containment(win, r, SWM_CW_ALLSIDES | SWM_CW_RESIZABLE |
6187 SWM_CW_HARDBOUNDARY);
6189 store_float_geom(win);
6193 region_containment(win, r, SWM_CW_ALLSIDES | SWM_CW_RESIZABLE |
6194 SWM_CW_SOFTBOUNDARY);
6197 /* get cursor offset from window root */
6198 xpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, win->id),
6205 if (xpr->win_x < WIDTH(win) / 2)
6208 if (xpr->win_y < HEIGHT(win) / 2)
6211 if (args->id == SWM_ARG_ID_CENTER)
6212 cursor = cursors[XC_SIZING].cid;
6214 cursor = cursors[left ? XC_TOP_LEFT_CORNER :
6215 XC_TOP_RIGHT_CORNER].cid;
6217 cursor = cursors[left ? XC_BOTTOM_LEFT_CORNER :
6218 XC_BOTTOM_RIGHT_CORNER].cid;
6220 xcb_grab_pointer(conn, 0, win->id, MOUSEMASK,
6221 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, XCB_WINDOW_NONE, cursor,
6226 while (resizing && (evt = xcb_wait_for_event(conn))) {
6227 switch (XCB_EVENT_RESPONSE_TYPE(evt)) {
6228 case XCB_BUTTON_RELEASE:
6229 DNPRINTF(SWM_D_EVENT, "resize: BUTTON_RELEASE\n");
6232 case XCB_MOTION_NOTIFY:
6233 mne = (xcb_motion_notify_event_t *)evt;
6234 /* cursor offset/delta from start of the operation */
6235 dx = mne->root_x - xpr->root_x;
6236 dy = mne->root_y - xpr->root_y;
6242 if (args->id == SWM_ARG_ID_CENTER) {
6243 if (g.h / 2 + dy < 1)
6247 HEIGHT(win) = g.h + 2 * dy;
6255 HEIGHT(win) = g.h + dy;
6262 if (args->id == SWM_ARG_ID_CENTER) {
6263 if (g.w / 2 + dx < 1)
6267 WIDTH(win) = g.w + 2 * dx;
6275 WIDTH(win) = g.w + dx;
6278 /* not free, don't sync more than 120 times / second */
6279 if ((mne->time - timestamp) > (1000 / 120) ) {
6280 timestamp = mne->time;
6281 regionize(win, mne->root_x, mne->root_y);
6282 region_containment(win, r, SWM_CW_ALLSIDES |
6283 SWM_CW_RESIZABLE | SWM_CW_HARDBOUNDARY |
6284 SWM_CW_SOFTBOUNDARY);
6291 xcb_allow_events(conn, XCB_ALLOW_ASYNC_KEYBOARD,
6292 ((xcb_key_press_event_t *)evt)->time);
6298 /* It's possible for win to have been freed above. */
6299 if (validate_win(win)) {
6300 DNPRINTF(SWM_D_EVENT, "resize: invalid win.\n");
6308 region_containment(win, r, SWM_CW_ALLSIDES | SWM_CW_RESIZABLE |
6309 SWM_CW_HARDBOUNDARY | SWM_CW_SOFTBOUNDARY);
6313 store_float_geom(win);
6315 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
6317 DNPRINTF(SWM_D_EVENT, "resize: done.\n");
6321 resize_step(struct swm_region *r, union arg *args)
6323 struct ws_win *win = NULL;
6325 if (r && r->ws && r->ws->focus)
6334 /* Try to set window region based on supplied coordinates or window center. */
6336 regionize(struct ws_win *win, int x, int y)
6338 struct swm_region *r = NULL;
6340 r = region_under(win->s, x, y);
6342 r = region_under(win->s, X(win) + WIDTH(win) / 2,
6343 Y(win) + HEIGHT(win) / 2);
6345 if (r != NULL && r != win->ws->r) {
6346 if (clear_maximized(r->ws) > 0)
6349 win_to_ws(win, r->ws->idx, false);
6351 /* Set focus on new ws. */
6352 unfocus_win(r->ws->focus);
6360 #define SWM_MOVE_STEPS (50)
6363 move(struct ws_win *win, union arg *args)
6365 struct swm_region *r;
6366 xcb_timestamp_t timestamp = 0;
6368 xcb_query_pointer_reply_t *qpr = NULL;
6369 xcb_generic_event_t *evt;
6370 xcb_motion_notify_event_t *mne;
6371 bool moving, restack = false;
6376 if ((r = win->ws->r) == NULL)
6379 if (FULLSCREEN(win))
6382 DNPRINTF(SWM_D_EVENT, "move: win %#x, floating: %s, transient: "
6383 "%#x\n", win->id, YESNO(ABOVE(win)), win->transient);
6385 /* in max_stack mode should only move transients */
6386 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !TRANS(win))
6389 if (!(ABOVE(win) || TRANS(win)) || MAXIMIZED(win)) {
6390 store_float_geom(win);
6394 ewmh_apply_flags(win, (win->ewmh_flags | SWM_F_MANUAL | EWMH_F_ABOVE) &
6396 ewmh_update_wm_state(win);
6403 /* It's possible for win to have been freed during focus_flush(). */
6404 if (validate_win(win)) {
6405 DNPRINTF(SWM_D_EVENT, "move: invalid win.\n");
6411 case SWM_ARG_ID_MOVELEFT:
6412 X(win) -= (SWM_MOVE_STEPS - border_width);
6415 case SWM_ARG_ID_MOVERIGHT:
6416 X(win) += (SWM_MOVE_STEPS - border_width);
6419 case SWM_ARG_ID_MOVEUP:
6420 Y(win) -= (SWM_MOVE_STEPS - border_width);
6423 case SWM_ARG_ID_MOVEDOWN:
6424 Y(win) += (SWM_MOVE_STEPS - border_width);
6431 regionize(win, -1, -1);
6432 region_containment(win, win->ws->r, SWM_CW_ALLSIDES);
6434 store_float_geom(win);
6438 xcb_grab_pointer(conn, 0, win->id, MOUSEMASK,
6439 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC,
6440 XCB_WINDOW_NONE, cursors[XC_FLEUR].cid, XCB_CURRENT_TIME);
6442 /* get cursor offset from window root */
6443 qpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, win->id),
6446 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
6450 regionize(win, qpr->root_x, qpr->root_y);
6451 region_containment(win, win->ws->r, SWM_CW_ALLSIDES |
6452 SWM_CW_SOFTBOUNDARY);
6456 while (moving && (evt = xcb_wait_for_event(conn))) {
6457 switch (XCB_EVENT_RESPONSE_TYPE(evt)) {
6458 case XCB_BUTTON_RELEASE:
6459 DNPRINTF(SWM_D_EVENT, "move: BUTTON_RELEASE\n");
6462 case XCB_MOTION_NOTIFY:
6463 mne = (xcb_motion_notify_event_t *)evt;
6464 DNPRINTF(SWM_D_EVENT, "motion: root: %#x\n", mne->root);
6465 X(win) = mne->root_x - qpr->win_x - border_width;
6466 Y(win) = mne->root_y - qpr->win_y - border_width;
6468 /* not free, don't sync more than 120 times / second */
6469 if ((mne->time - timestamp) > (1000 / 120) ) {
6470 timestamp = mne->time;
6471 regionize(win, mne->root_x, mne->root_y);
6472 region_containment(win, win->ws->r,
6473 SWM_CW_ALLSIDES | SWM_CW_SOFTBOUNDARY);
6480 xcb_allow_events(conn, XCB_ALLOW_ASYNC_KEYBOARD,
6481 ((xcb_key_press_event_t *)evt)->time);
6487 /* It's possible for win to have been freed above. */
6488 if (validate_win(win)) {
6489 DNPRINTF(SWM_D_EVENT, "move: invalid win.\n");
6497 region_containment(win, win->ws->r, SWM_CW_ALLSIDES |
6498 SWM_CW_SOFTBOUNDARY);
6502 store_float_geom(win);
6504 /* New region set to fullscreen layout. */
6505 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK]) {
6512 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
6513 DNPRINTF(SWM_D_EVENT, "move: done.\n");
6517 move_step(struct swm_region *r, union arg *args)
6519 struct ws_win *win = NULL;
6521 if (r && r->ws && r->ws->focus)
6526 if (!TRANS(win) && !ABOVE(win))
6533 /* key definitions */
6535 char name[SWM_FUNCNAME_LEN];
6536 void (*func)(struct swm_region *r, union arg *);
6538 } keyfuncs[KF_INVALID + 1] = {
6539 /* name function argument */
6540 { "bar_toggle", bar_toggle, {.id = SWM_ARG_ID_BAR_TOGGLE} },
6541 { "bar_toggle_ws", bar_toggle, {.id = SWM_ARG_ID_BAR_TOGGLE_WS} },
6542 { "button2", pressbutton, {2} },
6543 { "cycle_layout", cycle_layout, {0} },
6544 { "flip_layout", stack_config, {.id = SWM_ARG_ID_FLIPLAYOUT} },
6545 { "float_toggle", floating_toggle,{0} },
6546 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
6547 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
6548 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
6549 { "focus_urgent", focus, {.id = SWM_ARG_ID_FOCUSURGENT} },
6550 { "maximize_toggle", maximize_toggle,{0} },
6551 { "height_grow", resize_step, {.id = SWM_ARG_ID_HEIGHTGROW} },
6552 { "height_shrink", resize_step, {.id = SWM_ARG_ID_HEIGHTSHRINK} },
6553 { "iconify", iconify, {0} },
6554 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
6555 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
6556 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
6557 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
6558 { "move_down", move_step, {.id = SWM_ARG_ID_MOVEDOWN} },
6559 { "move_left", move_step, {.id = SWM_ARG_ID_MOVELEFT} },
6560 { "move_right", move_step, {.id = SWM_ARG_ID_MOVERIGHT} },
6561 { "move_up", move_step, {.id = SWM_ARG_ID_MOVEUP} },
6562 { "mvrg_1", send_to_rg, {.id = 0} },
6563 { "mvrg_2", send_to_rg, {.id = 1} },
6564 { "mvrg_3", send_to_rg, {.id = 2} },
6565 { "mvrg_4", send_to_rg, {.id = 3} },
6566 { "mvrg_5", send_to_rg, {.id = 4} },
6567 { "mvrg_6", send_to_rg, {.id = 5} },
6568 { "mvrg_7", send_to_rg, {.id = 6} },
6569 { "mvrg_8", send_to_rg, {.id = 7} },
6570 { "mvrg_9", send_to_rg, {.id = 8} },
6571 { "mvws_1", send_to_ws, {.id = 0} },
6572 { "mvws_2", send_to_ws, {.id = 1} },
6573 { "mvws_3", send_to_ws, {.id = 2} },
6574 { "mvws_4", send_to_ws, {.id = 3} },
6575 { "mvws_5", send_to_ws, {.id = 4} },
6576 { "mvws_6", send_to_ws, {.id = 5} },
6577 { "mvws_7", send_to_ws, {.id = 6} },
6578 { "mvws_8", send_to_ws, {.id = 7} },
6579 { "mvws_9", send_to_ws, {.id = 8} },
6580 { "mvws_10", send_to_ws, {.id = 9} },
6581 { "mvws_11", send_to_ws, {.id = 10} },
6582 { "mvws_12", send_to_ws, {.id = 11} },
6583 { "mvws_13", send_to_ws, {.id = 12} },
6584 { "mvws_14", send_to_ws, {.id = 13} },
6585 { "mvws_15", send_to_ws, {.id = 14} },
6586 { "mvws_16", send_to_ws, {.id = 15} },
6587 { "mvws_17", send_to_ws, {.id = 16} },
6588 { "mvws_18", send_to_ws, {.id = 17} },
6589 { "mvws_19", send_to_ws, {.id = 18} },
6590 { "mvws_20", send_to_ws, {.id = 19} },
6591 { "mvws_21", send_to_ws, {.id = 20} },
6592 { "mvws_22", send_to_ws, {.id = 21} },
6593 { "name_workspace", name_workspace, {0} },
6594 { "quit", quit, {0} },
6595 { "raise_toggle", raise_toggle, {0} },
6596 { "restart", restart, {0} },
6597 { "rg_1", focusrg, {.id = 0} },
6598 { "rg_2", focusrg, {.id = 1} },
6599 { "rg_3", focusrg, {.id = 2} },
6600 { "rg_4", focusrg, {.id = 3} },
6601 { "rg_5", focusrg, {.id = 4} },
6602 { "rg_6", focusrg, {.id = 5} },
6603 { "rg_7", focusrg, {.id = 6} },
6604 { "rg_8", focusrg, {.id = 7} },
6605 { "rg_9", focusrg, {.id = 8} },
6606 { "rg_next", cyclerg, {.id = SWM_ARG_ID_CYCLERG_UP} },
6607 { "rg_prev", cyclerg, {.id = SWM_ARG_ID_CYCLERG_DOWN} },
6608 { "screen_next", cyclerg, {.id = SWM_ARG_ID_CYCLERG_UP} },
6609 { "screen_prev", cyclerg, {.id = SWM_ARG_ID_CYCLERG_DOWN} },
6610 { "search_win", search_win, {0} },
6611 { "search_workspace", search_workspace, {0} },
6612 { "spawn_custom", NULL, {0} },
6613 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
6614 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
6615 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
6616 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
6617 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
6618 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
6619 { "uniconify", uniconify, {0} },
6620 { "version", version, {0} },
6621 { "width_grow", resize_step, {.id = SWM_ARG_ID_WIDTHGROW} },
6622 { "width_shrink", resize_step, {.id = SWM_ARG_ID_WIDTHSHRINK} },
6623 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
6624 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
6625 { "ws_1", switchws, {.id = 0} },
6626 { "ws_2", switchws, {.id = 1} },
6627 { "ws_3", switchws, {.id = 2} },
6628 { "ws_4", switchws, {.id = 3} },
6629 { "ws_5", switchws, {.id = 4} },
6630 { "ws_6", switchws, {.id = 5} },
6631 { "ws_7", switchws, {.id = 6} },
6632 { "ws_8", switchws, {.id = 7} },
6633 { "ws_9", switchws, {.id = 8} },
6634 { "ws_10", switchws, {.id = 9} },
6635 { "ws_11", switchws, {.id = 10} },
6636 { "ws_12", switchws, {.id = 11} },
6637 { "ws_13", switchws, {.id = 12} },
6638 { "ws_14", switchws, {.id = 13} },
6639 { "ws_15", switchws, {.id = 14} },
6640 { "ws_16", switchws, {.id = 15} },
6641 { "ws_17", switchws, {.id = 16} },
6642 { "ws_18", switchws, {.id = 17} },
6643 { "ws_19", switchws, {.id = 18} },
6644 { "ws_20", switchws, {.id = 19} },
6645 { "ws_21", switchws, {.id = 20} },
6646 { "ws_22", switchws, {.id = 21} },
6647 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
6648 { "ws_next_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP_ALL} },
6649 { "ws_next_move", cyclews, {.id = SWM_ARG_ID_CYCLEWS_MOVE_UP} },
6650 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
6651 { "ws_prev_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN_ALL} },
6652 { "ws_prev_move", cyclews, {.id = SWM_ARG_ID_CYCLEWS_MOVE_DOWN} },
6653 { "ws_prior", priorws, {0} },
6654 { "dumpwins", dumpwins, {0} }, /* MUST BE LAST */
6655 { "invalid key func", NULL, {0} },
6659 key_cmp(struct key *kp1, struct key *kp2)
6661 if (kp1->keysym < kp2->keysym)
6663 if (kp1->keysym > kp2->keysym)
6666 if (kp1->mod < kp2->mod)
6668 if (kp1->mod > kp2->mod)
6675 enum { client_click, root_click };
6677 unsigned int action;
6679 unsigned int button;
6680 void (*func)(struct ws_win *, union arg *);
6683 #define MODKEY_SHIFT MODKEY | XCB_MOD_MASK_SHIFT
6684 /* action key mouse button func args */
6685 { client_click, MODKEY, XCB_BUTTON_INDEX_3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
6686 { client_click, MODKEY_SHIFT, XCB_BUTTON_INDEX_3, resize, {.id = SWM_ARG_ID_CENTER} },
6687 { client_click, MODKEY, XCB_BUTTON_INDEX_1, move, {0} },
6692 update_modkey(unsigned int mod)
6698 RB_FOREACH(kp, key_tree, &keys)
6699 if (kp->mod & XCB_MOD_MASK_SHIFT)
6700 kp->mod = mod | XCB_MOD_MASK_SHIFT;
6704 for (i = 0; i < LENGTH(buttons); i++)
6705 if (buttons[i].mask & XCB_MOD_MASK_SHIFT)
6706 buttons[i].mask = mod | XCB_MOD_MASK_SHIFT;
6708 buttons[i].mask = mod;
6712 spawn_expand(struct swm_region *r, union arg *args, const char *spawn_name,
6715 struct spawn_prog *prog = NULL;
6717 char *ap, **real_args;
6719 /* suppress unused warning since var is needed */
6722 DNPRINTF(SWM_D_SPAWN, "spawn_expand: %s\n", spawn_name);
6725 TAILQ_FOREACH(prog, &spawns, entry) {
6726 if (strcasecmp(spawn_name, prog->name) == 0)
6730 warnx("spawn_custom: program %s not found", spawn_name);
6734 /* make room for expanded args */
6735 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
6736 err(1, "spawn_custom: calloc real_args");
6738 /* expand spawn_args into real_args */
6739 for (i = c = 0; i < prog->argc; i++) {
6741 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg: %s\n", ap);
6742 if (strcasecmp(ap, "$bar_border") == 0) {
6744 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
6746 err(1, "spawn_custom border color");
6747 } else if (strcasecmp(ap, "$bar_color") == 0) {
6749 strdup(r->s->c[SWM_S_COLOR_BAR].name))
6751 err(1, "spawn_custom bar color");
6752 } else if (strcasecmp(ap, "$bar_font") == 0) {
6753 if ((real_args[c] = strdup(bar_fonts))
6755 err(1, "spawn_custom bar fonts");
6756 } else if (strcasecmp(ap, "$bar_font_color") == 0) {
6758 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
6760 err(1, "spawn_custom color font");
6761 } else if (strcasecmp(ap, "$color_focus") == 0) {
6763 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
6765 err(1, "spawn_custom color focus");
6766 } else if (strcasecmp(ap, "$color_focus_maximized") == 0) {
6768 strdup(r->s->c[SWM_S_COLOR_FOCUS_MAXIMIZED].name))
6770 err(1, "spawn_custom color focus maximized");
6771 } else if (strcasecmp(ap, "$color_unfocus") == 0) {
6773 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
6775 err(1, "spawn_custom color unfocus");
6776 } else if (strcasecmp(ap, "$color_unfocus_maximized") == 0) {
6778 strdup(r->s->c[SWM_S_COLOR_UNFOCUS_MAXIMIZED].name))
6780 err(1, "spawn_custom color unfocus maximized");
6781 } else if (strcasecmp(ap, "$region_index") == 0) {
6782 if (asprintf(&real_args[c], "%d",
6783 get_region_index(r) + 1) < 1)
6784 err(1, "spawn_custom region index");
6785 } else if (strcasecmp(ap, "$workspace_index") == 0) {
6786 if (asprintf(&real_args[c], "%d", r->ws->idx + 1) < 1)
6787 err(1, "spawn_custom workspace index");
6788 } else if (strcasecmp(ap, "$dmenu_bottom") == 0) {
6791 if ((real_args[c] = strdup("-b")) == NULL)
6792 err(1, "spawn_custom workspace index");
6794 /* no match --> copy as is */
6795 if ((real_args[c] = strdup(ap)) == NULL)
6796 err(1, "spawn_custom strdup(ap)");
6798 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg: %s\n",
6804 DNPRINTF(SWM_D_SPAWN, "spawn_custom: result: ");
6805 for (i = 0; i < c; ++i)
6806 DPRINTF("\"%s\" ", real_args[i]);
6809 *ret_args = real_args;
6814 spawn_custom(struct swm_region *r, union arg *args, const char *spawn_name)
6820 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
6824 spawn(r->ws->idx, &a, true);
6826 for (i = 0; i < spawn_argc; i++)
6832 spawn_select(struct swm_region *r, union arg *args, const char *spawn_name,
6839 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
6843 if (pipe(select_list_pipe) == -1)
6844 err(1, "pipe error");
6845 if (pipe(select_resp_pipe) == -1)
6846 err(1, "pipe error");
6848 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
6849 err(1, "could not disable SIGPIPE");
6850 switch (*pid = fork()) {
6852 err(1, "cannot fork");
6855 if (dup2(select_list_pipe[0], STDIN_FILENO) == -1)
6857 if (dup2(select_resp_pipe[1], STDOUT_FILENO) == -1)
6859 close(select_list_pipe[1]);
6860 close(select_resp_pipe[0]);
6861 spawn(r->ws->idx, &a, false);
6863 default: /* parent */
6864 close(select_list_pipe[0]);
6865 close(select_resp_pipe[1]);
6869 for (i = 0; i < spawn_argc; i++)
6874 /* Argument tokenizer. */
6877 char *arg, *cp, *next;
6878 bool single_quoted = false, double_quoted = false;
6883 /* Eat and move characters until end of argument is found. */
6884 for (arg = next = cp = *sp; *cp != '\0'; ++cp) {
6885 if (!double_quoted && *cp == '\'') {
6886 /* Eat single-quote. */
6887 single_quoted = !single_quoted;
6888 } else if (!single_quoted && *cp == '"') {
6889 /* Eat double-quote. */
6890 double_quoted = !double_quoted;
6891 } else if (!single_quoted && *cp == '\\' && *(cp + 1) == '"') {
6892 /* Eat backslash; copy escaped character to arg. */
6894 } else if (!single_quoted && !double_quoted && *cp == '\\' &&
6895 (*(cp + 1) == '\'' || *(cp + 1) == ' ')) {
6896 /* Eat backslash; move escaped character. */
6898 } else if (!single_quoted && !double_quoted &&
6899 (*cp == ' ' || *cp == '\t')) {
6900 /* Terminate argument. */
6902 /* Point sp to beginning of next argument. */
6906 /* Move regular character. */
6911 /* Terminate argument if end of string. */
6921 spawn_insert(const char *name, const char *args, int flags)
6923 struct spawn_prog *sp;
6924 char *arg, *cp, *ptr;
6926 DNPRINTF(SWM_D_SPAWN, "spawn_insert: %s[%s]\n", name, args);
6928 if (args == NULL || *args == '\0')
6931 if ((sp = calloc(1, sizeof *sp)) == NULL)
6932 err(1, "spawn_insert: calloc");
6933 if ((sp->name = strdup(name)) == NULL)
6934 err(1, "spawn_insert: strdup");
6936 /* Convert the arguments to an argument list. */
6937 if ((ptr = cp = strdup(args)) == NULL)
6938 err(1, "spawn_insert: strdup");
6939 while ((arg = argsep(&ptr)) != NULL) {
6940 /* Null argument; skip it. */
6945 if ((sp->argv = realloc(sp->argv, sp->argc *
6946 sizeof *sp->argv)) == NULL)
6947 err(1, "spawn_insert: realloc");
6948 if ((sp->argv[sp->argc - 1] = strdup(arg)) == NULL)
6949 err(1, "spawn_insert: strdup");
6955 DNPRINTF(SWM_D_SPAWN, "arg %d: [%s]\n", sp->argc, sp->argv[sp->argc-1]);
6956 TAILQ_INSERT_TAIL(&spawns, sp, entry);
6957 DNPRINTF(SWM_D_SPAWN, "spawn_insert: leave\n");
6961 spawn_remove(struct spawn_prog *sp)
6965 DNPRINTF(SWM_D_SPAWN, "spawn_remove: %s\n", sp->name);
6967 TAILQ_REMOVE(&spawns, sp, entry);
6968 for (i = 0; i < sp->argc; i++)
6974 DNPRINTF(SWM_D_SPAWN, "spawn_remove: leave\n");
6978 spawn_find(const char *name)
6980 struct spawn_prog *sp;
6982 TAILQ_FOREACH(sp, &spawns, entry)
6983 if (strcasecmp(sp->name, name) == 0)
6990 setspawn(const char *name, const char *args, int flags)
6992 struct spawn_prog *sp;
6994 DNPRINTF(SWM_D_SPAWN, "setspawn: %s\n", name);
6999 /* Remove any old spawn under the same name. */
7000 if ((sp = spawn_find(name)) != NULL)
7004 spawn_insert(name, args, flags);
7006 warnx("error: setspawn: cannot find program: %s", name);
7008 DNPRINTF(SWM_D_SPAWN, "setspawn: leave\n");
7012 setconfspawn(const char *selector, const char *value, int flags)
7016 if (selector == NULL || strlen(selector) == 0)
7019 args = expand_tilde(value);
7021 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, args);
7023 setspawn(selector, args, flags);
7026 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done.\n");
7031 validate_spawns(void)
7033 struct spawn_prog *sp;
7034 char which[PATH_MAX];
7039 RB_FOREACH(kp, key_tree, &keys) {
7040 if (kp->funcid != KF_SPAWN_CUSTOM)
7044 sp = spawn_find(kp->spawn_name);
7045 if (sp == NULL || sp->flags & SWM_SPAWN_OPTIONAL)
7048 /* verify we have the goods */
7049 snprintf(which, sizeof which, "which %s", sp->argv[0]);
7050 DNPRINTF(SWM_D_CONF, "validate_spawns: which %s\n",
7052 for (i = strlen("which "); i < strlen(which); i++)
7053 if (which[i] == ' ') {
7057 if (system(which) != 0)
7058 add_startup_exception("could not find %s",
7059 &which[strlen("which ")]);
7066 setconfspawn("lock", "xlock", 0);
7068 setconfspawn("term", "xterm", 0);
7069 setconfspawn("spawn_term", "xterm", 0);
7071 setconfspawn("menu", "dmenu_run"
7075 " -nf $bar_font_color"
7077 " -sf $bar_color", 0);
7079 setconfspawn("search", "dmenu"
7084 " -nf $bar_font_color"
7086 " -sf $bar_color", 0);
7088 setconfspawn("name_workspace", "dmenu"
7093 " -nf $bar_font_color"
7095 " -sf $bar_color", 0);
7097 /* These are not verified for existence, even with a binding set. */
7098 setconfspawn("screenshot_all", "screenshot.sh full", SWM_SPAWN_OPTIONAL);
7099 setconfspawn("screenshot_wind", "screenshot.sh window", SWM_SPAWN_OPTIONAL);
7100 setconfspawn("initscr", "initscreen.sh", SWM_SPAWN_OPTIONAL);
7104 #define SWM_MODNAME_SIZE 32
7105 #define SWM_KEY_WS "\n+ \t"
7107 parsekeys(const char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
7109 char *str, *cp, *name;
7112 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
7113 if (mod == NULL || ks == NULL) {
7114 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
7117 if (keystr == NULL || strlen(keystr) == 0) {
7118 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
7122 if ((cp = str = strdup(keystr)) == NULL)
7123 err(1, "parsekeys: strdup");
7125 *ks = XCB_NO_SYMBOL;
7127 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
7128 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
7130 cp += (long)strspn(cp, SWM_KEY_WS);
7131 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
7133 else if (strncasecmp(name, "Mod1", SWM_MODNAME_SIZE) == 0)
7134 *mod |= XCB_MOD_MASK_1;
7135 else if (strncasecmp(name, "Mod2", SWM_MODNAME_SIZE) == 0)
7136 *mod += XCB_MOD_MASK_2;
7137 else if (strncmp(name, "Mod3", SWM_MODNAME_SIZE) == 0)
7138 *mod |= XCB_MOD_MASK_3;
7139 else if (strncmp(name, "Mod4", SWM_MODNAME_SIZE) == 0)
7140 *mod |= XCB_MOD_MASK_4;
7141 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
7142 *mod |= XCB_MOD_MASK_SHIFT;
7143 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
7144 *mod |= XCB_MOD_MASK_CONTROL;
7146 *ks = XStringToKeysym(name);
7147 XConvertCase(*ks, ks, &uks);
7148 if (ks == XCB_NO_SYMBOL) {
7150 "parsekeys: invalid key %s\n",
7159 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
7164 strdupsafe(const char *str)
7169 return (strdup(str));
7173 key_insert(unsigned int mod, KeySym ks, enum keyfuncid kfid,
7174 const char *spawn_name)
7178 DNPRINTF(SWM_D_KEY, "key_insert: enter %s [%s]\n",
7179 keyfuncs[kfid].name, spawn_name);
7181 if ((kp = malloc(sizeof *kp)) == NULL)
7182 err(1, "key_insert: malloc");
7187 kp->spawn_name = strdupsafe(spawn_name);
7188 RB_INSERT(key_tree, &keys, kp);
7190 DNPRINTF(SWM_D_KEY, "key_insert: leave\n");
7194 key_lookup(unsigned int mod, KeySym ks)
7201 return (RB_FIND(key_tree, &keys, &kp));
7205 key_remove(struct key *kp)
7207 DNPRINTF(SWM_D_KEY, "key_remove: %s\n", keyfuncs[kp->funcid].name);
7209 RB_REMOVE(key_tree, &keys, kp);
7210 free(kp->spawn_name);
7213 DNPRINTF(SWM_D_KEY, "key_remove: leave\n");
7217 key_replace(struct key *kp, unsigned int mod, KeySym ks, enum keyfuncid kfid,
7218 const char *spawn_name)
7220 DNPRINTF(SWM_D_KEY, "key_replace: %s [%s]\n", keyfuncs[kp->funcid].name,
7224 key_insert(mod, ks, kfid, spawn_name);
7226 DNPRINTF(SWM_D_KEY, "key_replace: leave\n");
7230 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid,
7231 const char *spawn_name)
7235 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
7236 keyfuncs[kfid].name, spawn_name);
7238 if ((kp = key_lookup(mod, ks)) != NULL) {
7239 if (kfid == KF_INVALID)
7242 key_replace(kp, mod, ks, kfid, spawn_name);
7243 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
7246 if (kfid == KF_INVALID) {
7247 warnx("bind: Key combination already unbound.");
7248 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
7252 key_insert(mod, ks, kfid, spawn_name);
7253 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
7257 setconfbinding(const char *selector, const char *value, int flags)
7259 enum keyfuncid kfid;
7262 struct spawn_prog *sp;
7264 /* suppress unused warning since var is needed */
7267 DNPRINTF(SWM_D_KEY, "setconfbinding: enter selector: [%s], "
7268 "value: [%s]\n", selector, value);
7269 if (selector == NULL || strlen(selector) == 0) {
7270 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
7271 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
7273 setkeybinding(mod, ks, kfid, NULL);
7278 /* search by key function name */
7279 for (kfid = 0; kfid < KF_INVALID; (kfid)++) {
7280 if (strncasecmp(selector, keyfuncs[kfid].name,
7281 SWM_FUNCNAME_LEN) == 0) {
7282 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match "
7283 "keyfunc\n", selector);
7284 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
7285 setkeybinding(mod, ks, kfid, NULL);
7291 /* search by custom spawn name */
7292 if ((sp = spawn_find(selector)) != NULL) {
7293 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match "
7294 "spawn\n", selector);
7295 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
7296 setkeybinding(mod, ks, KF_SPAWN_CUSTOM,
7302 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
7309 #define MODKEY_SHIFT MODKEY | XCB_MOD_MASK_SHIFT
7310 setkeybinding(MODKEY, XK_b, KF_BAR_TOGGLE, NULL);
7311 setkeybinding(MODKEY_SHIFT, XK_b, KF_BAR_TOGGLE_WS,NULL);
7312 setkeybinding(MODKEY, XK_v, KF_BUTTON2, NULL);
7313 setkeybinding(MODKEY, XK_space, KF_CYCLE_LAYOUT,NULL);
7314 setkeybinding(MODKEY_SHIFT, XK_backslash, KF_FLIP_LAYOUT, NULL);
7315 setkeybinding(MODKEY, XK_t, KF_FLOAT_TOGGLE,NULL);
7316 setkeybinding(MODKEY, XK_m, KF_FOCUS_MAIN, NULL);
7317 setkeybinding(MODKEY, XK_j, KF_FOCUS_NEXT, NULL);
7318 setkeybinding(MODKEY, XK_Tab, KF_FOCUS_NEXT, NULL);
7319 setkeybinding(MODKEY, XK_k, KF_FOCUS_PREV, NULL);
7320 setkeybinding(MODKEY_SHIFT, XK_Tab, KF_FOCUS_PREV, NULL);
7321 setkeybinding(MODKEY, XK_u, KF_FOCUS_URGENT,NULL);
7322 setkeybinding(MODKEY, XK_e, KF_MAXIMIZE_TOGGLE,NULL);
7323 setkeybinding(MODKEY_SHIFT, XK_equal, KF_HEIGHT_GROW,NULL);
7324 setkeybinding(MODKEY_SHIFT, XK_minus, KF_HEIGHT_SHRINK,NULL);
7325 setkeybinding(MODKEY, XK_w, KF_ICONIFY, NULL);
7326 setkeybinding(MODKEY, XK_h, KF_MASTER_SHRINK, NULL);
7327 setkeybinding(MODKEY, XK_l, KF_MASTER_GROW, NULL);
7328 setkeybinding(MODKEY, XK_comma, KF_MASTER_ADD, NULL);
7329 setkeybinding(MODKEY, XK_period, KF_MASTER_DEL, NULL);
7330 setkeybinding(MODKEY_SHIFT, XK_bracketright,KF_MOVE_DOWN,NULL);
7331 setkeybinding(MODKEY, XK_bracketleft, KF_MOVE_LEFT,NULL);
7332 setkeybinding(MODKEY, XK_bracketright,KF_MOVE_RIGHT,NULL);
7333 setkeybinding(MODKEY_SHIFT, XK_bracketleft, KF_MOVE_UP, NULL);
7334 setkeybinding(MODKEY_SHIFT, XK_KP_End, KF_MVRG_1, NULL);
7335 setkeybinding(MODKEY_SHIFT, XK_KP_Down, KF_MVRG_2, NULL);
7336 setkeybinding(MODKEY_SHIFT, XK_KP_Next, KF_MVRG_3, NULL);
7337 setkeybinding(MODKEY_SHIFT, XK_KP_Left, KF_MVRG_4, NULL);
7338 setkeybinding(MODKEY_SHIFT, XK_KP_Begin, KF_MVRG_5, NULL);
7339 setkeybinding(MODKEY_SHIFT, XK_KP_Right, KF_MVRG_6, NULL);
7340 setkeybinding(MODKEY_SHIFT, XK_KP_Home, KF_MVRG_7, NULL);
7341 setkeybinding(MODKEY_SHIFT, XK_KP_Up, KF_MVRG_8, NULL);
7342 setkeybinding(MODKEY_SHIFT, XK_KP_Prior, KF_MVRG_9, NULL);
7343 setkeybinding(MODKEY_SHIFT, XK_1, KF_MVWS_1, NULL);
7344 setkeybinding(MODKEY_SHIFT, XK_2, KF_MVWS_2, NULL);
7345 setkeybinding(MODKEY_SHIFT, XK_3, KF_MVWS_3, NULL);
7346 setkeybinding(MODKEY_SHIFT, XK_4, KF_MVWS_4, NULL);
7347 setkeybinding(MODKEY_SHIFT, XK_5, KF_MVWS_5, NULL);
7348 setkeybinding(MODKEY_SHIFT, XK_6, KF_MVWS_6, NULL);
7349 setkeybinding(MODKEY_SHIFT, XK_7, KF_MVWS_7, NULL);
7350 setkeybinding(MODKEY_SHIFT, XK_8, KF_MVWS_8, NULL);
7351 setkeybinding(MODKEY_SHIFT, XK_9, KF_MVWS_9, NULL);
7352 setkeybinding(MODKEY_SHIFT, XK_0, KF_MVWS_10, NULL);
7353 setkeybinding(MODKEY_SHIFT, XK_F1, KF_MVWS_11, NULL);
7354 setkeybinding(MODKEY_SHIFT, XK_F2, KF_MVWS_12, NULL);
7355 setkeybinding(MODKEY_SHIFT, XK_F3, KF_MVWS_13, NULL);
7356 setkeybinding(MODKEY_SHIFT, XK_F4, KF_MVWS_14, NULL);
7357 setkeybinding(MODKEY_SHIFT, XK_F5, KF_MVWS_15, NULL);
7358 setkeybinding(MODKEY_SHIFT, XK_F6, KF_MVWS_16, NULL);
7359 setkeybinding(MODKEY_SHIFT, XK_F7, KF_MVWS_17, NULL);
7360 setkeybinding(MODKEY_SHIFT, XK_F8, KF_MVWS_18, NULL);
7361 setkeybinding(MODKEY_SHIFT, XK_F9, KF_MVWS_19, NULL);
7362 setkeybinding(MODKEY_SHIFT, XK_F10, KF_MVWS_20, NULL);
7363 setkeybinding(MODKEY_SHIFT, XK_F11, KF_MVWS_21, NULL);
7364 setkeybinding(MODKEY_SHIFT, XK_F12, KF_MVWS_22, NULL);
7365 setkeybinding(MODKEY_SHIFT, XK_slash, KF_NAME_WORKSPACE,NULL);
7366 setkeybinding(MODKEY_SHIFT, XK_q, KF_QUIT, NULL);
7367 setkeybinding(MODKEY_SHIFT, XK_r, KF_RAISE_TOGGLE,NULL);
7368 setkeybinding(MODKEY, XK_q, KF_RESTART, NULL);
7369 setkeybinding(MODKEY, XK_KP_End, KF_RG_1, NULL);
7370 setkeybinding(MODKEY, XK_KP_Down, KF_RG_2, NULL);
7371 setkeybinding(MODKEY, XK_KP_Next, KF_RG_3, NULL);
7372 setkeybinding(MODKEY, XK_KP_Left, KF_RG_4, NULL);
7373 setkeybinding(MODKEY, XK_KP_Begin, KF_RG_5, NULL);
7374 setkeybinding(MODKEY, XK_KP_Right, KF_RG_6, NULL);
7375 setkeybinding(MODKEY, XK_KP_Home, KF_RG_7, NULL);
7376 setkeybinding(MODKEY, XK_KP_Up, KF_RG_8, NULL);
7377 setkeybinding(MODKEY, XK_KP_Prior, KF_RG_9, NULL);
7378 setkeybinding(MODKEY_SHIFT, XK_Right, KF_RG_NEXT, NULL);
7379 setkeybinding(MODKEY_SHIFT, XK_Left, KF_RG_PREV, NULL);
7380 setkeybinding(MODKEY, XK_f, KF_SEARCH_WIN, NULL);
7381 setkeybinding(MODKEY, XK_slash, KF_SEARCH_WORKSPACE,NULL);
7382 setkeybinding(MODKEY_SHIFT, XK_i, KF_SPAWN_CUSTOM,"initscr");
7383 setkeybinding(MODKEY_SHIFT, XK_Delete, KF_SPAWN_CUSTOM,"lock");
7384 setkeybinding(MODKEY, XK_p, KF_SPAWN_CUSTOM,"menu");
7385 setkeybinding(MODKEY, XK_s, KF_SPAWN_CUSTOM,"screenshot_all");
7386 setkeybinding(MODKEY_SHIFT, XK_s, KF_SPAWN_CUSTOM,"screenshot_wind");
7387 setkeybinding(MODKEY_SHIFT, XK_Return, KF_SPAWN_CUSTOM,"term");
7388 setkeybinding(MODKEY_SHIFT, XK_comma, KF_STACK_INC, NULL);
7389 setkeybinding(MODKEY_SHIFT, XK_period, KF_STACK_DEC, NULL);
7390 setkeybinding(MODKEY_SHIFT, XK_space, KF_STACK_RESET, NULL);
7391 setkeybinding(MODKEY, XK_Return, KF_SWAP_MAIN, NULL);
7392 setkeybinding(MODKEY_SHIFT, XK_j, KF_SWAP_NEXT, NULL);
7393 setkeybinding(MODKEY_SHIFT, XK_k, KF_SWAP_PREV, NULL);
7394 setkeybinding(MODKEY_SHIFT, XK_w, KF_UNICONIFY, NULL);
7395 setkeybinding(MODKEY_SHIFT, XK_v, KF_VERSION, NULL);
7396 setkeybinding(MODKEY, XK_equal, KF_WIDTH_GROW, NULL);
7397 setkeybinding(MODKEY, XK_minus, KF_WIDTH_SHRINK,NULL);
7398 setkeybinding(MODKEY, XK_x, KF_WIND_DEL, NULL);
7399 setkeybinding(MODKEY_SHIFT, XK_x, KF_WIND_KILL, NULL);
7400 setkeybinding(MODKEY, XK_1, KF_WS_1, NULL);
7401 setkeybinding(MODKEY, XK_2, KF_WS_2, NULL);
7402 setkeybinding(MODKEY, XK_3, KF_WS_3, NULL);
7403 setkeybinding(MODKEY, XK_4, KF_WS_4, NULL);
7404 setkeybinding(MODKEY, XK_5, KF_WS_5, NULL);
7405 setkeybinding(MODKEY, XK_6, KF_WS_6, NULL);
7406 setkeybinding(MODKEY, XK_7, KF_WS_7, NULL);
7407 setkeybinding(MODKEY, XK_8, KF_WS_8, NULL);
7408 setkeybinding(MODKEY, XK_9, KF_WS_9, NULL);
7409 setkeybinding(MODKEY, XK_0, KF_WS_10, NULL);
7410 setkeybinding(MODKEY, XK_F1, KF_WS_11, NULL);
7411 setkeybinding(MODKEY, XK_F2, KF_WS_12, NULL);
7412 setkeybinding(MODKEY, XK_F3, KF_WS_13, NULL);
7413 setkeybinding(MODKEY, XK_F4, KF_WS_14, NULL);
7414 setkeybinding(MODKEY, XK_F5, KF_WS_15, NULL);
7415 setkeybinding(MODKEY, XK_F6, KF_WS_16, NULL);
7416 setkeybinding(MODKEY, XK_F7, KF_WS_17, NULL);
7417 setkeybinding(MODKEY, XK_F8, KF_WS_18, NULL);
7418 setkeybinding(MODKEY, XK_F9, KF_WS_19, NULL);
7419 setkeybinding(MODKEY, XK_F10, KF_WS_20, NULL);
7420 setkeybinding(MODKEY, XK_F11, KF_WS_21, NULL);
7421 setkeybinding(MODKEY, XK_F12, KF_WS_22, NULL);
7422 setkeybinding(MODKEY, XK_Right, KF_WS_NEXT, NULL);
7423 setkeybinding(MODKEY, XK_Left, KF_WS_PREV, NULL);
7424 setkeybinding(MODKEY, XK_Up, KF_WS_NEXT_ALL, NULL);
7425 setkeybinding(MODKEY, XK_Down, KF_WS_PREV_ALL, NULL);
7426 setkeybinding(MODKEY_SHIFT, XK_Up, KF_WS_NEXT_MOVE,NULL);
7427 setkeybinding(MODKEY_SHIFT, XK_Down, KF_WS_PREV_MOVE,NULL);
7428 setkeybinding(MODKEY, XK_a, KF_WS_PRIOR, NULL);
7430 setkeybinding(MODKEY_SHIFT, XK_d, KF_DUMPWINS, NULL);
7440 while (RB_EMPTY(&keys) == 0) {
7441 kp = RB_ROOT(&keys);
7447 setkeymapping(const char *selector, const char *value, int flags)
7449 char *keymapping_file;
7451 /* suppress unused warnings since vars are needed */
7455 DNPRINTF(SWM_D_KEY, "setkeymapping: enter\n");
7457 keymapping_file = expand_tilde(value);
7460 /* load new key bindings; if it fails, revert to default bindings */
7461 if (conf_load(keymapping_file, SWM_CONF_KEYMAPPING)) {
7466 free(keymapping_file);
7468 DNPRINTF(SWM_D_KEY, "setkeymapping: leave\n");
7473 updatenumlockmask(void)
7476 xcb_get_modifier_mapping_reply_t *modmap_r;
7477 xcb_keycode_t *modmap, kc, *keycode;
7481 modmap_r = xcb_get_modifier_mapping_reply(conn,
7482 xcb_get_modifier_mapping(conn),
7485 modmap = xcb_get_modifier_mapping_keycodes(modmap_r);
7486 for (i = 0; i < 8; i++) {
7487 for (j = 0; j < modmap_r->keycodes_per_modifier; j++) {
7488 kc = modmap[i * modmap_r->keycodes_per_modifier
7490 keycode = xcb_key_symbols_get_keycode(syms,
7494 numlockmask = (1 << i);
7501 DNPRINTF(SWM_D_MISC, "updatenumlockmask: %d\n", numlockmask);
7508 int num_screens, k, j;
7509 unsigned int modifiers[4];
7510 xcb_keycode_t *code;
7512 DNPRINTF(SWM_D_MISC, "grabkeys\n");
7513 updatenumlockmask();
7516 modifiers[1] = numlockmask;
7517 modifiers[2] = XCB_MOD_MASK_LOCK;
7518 modifiers[3] = numlockmask | XCB_MOD_MASK_LOCK;
7520 num_screens = get_screen_count();
7521 for (k = 0; k < num_screens; k++) {
7522 if (TAILQ_EMPTY(&screens[k].rl))
7524 xcb_ungrab_key(conn, XCB_GRAB_ANY, screens[k].root,
7526 RB_FOREACH(kp, key_tree, &keys) {
7527 /* Skip unused ws binds. */
7528 if ((int)kp->funcid > KF_WS_1 + workspace_limit - 1 &&
7529 kp->funcid <= KF_WS_22)
7532 /* Skip unused mvws binds. */
7533 if ((int)kp->funcid > KF_MVWS_1 + workspace_limit - 1 &&
7534 kp->funcid <= KF_MVWS_22)
7537 if ((code = xcb_key_symbols_get_keycode(syms,
7539 for (j = 0; j < LENGTH(modifiers); j++)
7540 xcb_grab_key(conn, 1,
7542 kp->mod | modifiers[j],
7543 *code, XCB_GRAB_MODE_SYNC,
7544 XCB_GRAB_MODE_SYNC);
7552 grabbuttons(struct ws_win *win)
7554 unsigned int modifiers[4];
7557 DNPRINTF(SWM_D_MOUSE, "grabbuttons: win %#x\n", win->id);
7558 updatenumlockmask();
7561 modifiers[1] = numlockmask;
7562 modifiers[2] = XCB_MOD_MASK_LOCK;
7563 modifiers[3] = numlockmask | XCB_MOD_MASK_LOCK;
7565 for (i = 0; i < LENGTH(buttons); i++)
7566 if (buttons[i].action == client_click)
7567 for (j = 0; j < LENGTH(modifiers); ++j)
7568 xcb_grab_button(conn, 0, win->id, BUTTONMASK,
7569 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC,
7570 XCB_WINDOW_NONE, XCB_CURSOR_NONE,
7571 buttons[i].button, buttons[i].mask |
7575 const char *quirkname[] = {
7576 "NONE", /* config string for "no value" */
7584 "FOCUSONMAP_SINGLE",
7590 /* SWM_Q_DELIM: retain '|' for back compat for now (2009-08-11) */
7591 #define SWM_Q_DELIM "\n|+ \t"
7593 parsequirks(const char *qstr, uint32_t *quirk, int *ws)
7595 char *str, *cp, *name;
7598 if (quirk == NULL || qstr == NULL)
7601 if ((str = strdup(qstr)) == NULL)
7602 err(1, "parsequirks: strdup");
7606 while ((name = strsep(&cp, SWM_Q_DELIM)) != NULL) {
7608 cp += (long)strspn(cp, SWM_Q_DELIM);
7610 if (sscanf(name, "WS[%d]", ws) == 1) {
7616 for (i = 0; i < LENGTH(quirkname); i++) {
7617 if (strncasecmp(name, quirkname[i],
7618 SWM_QUIRK_LEN) == 0) {
7619 DNPRINTF(SWM_D_QUIRK,
7620 "parsequirks: %s\n", name);
7626 *quirk |= 1 << (i-1);
7630 if (i >= LENGTH(quirkname)) {
7631 DNPRINTF(SWM_D_QUIRK,
7632 "parsequirks: invalid quirk [%s]\n", name);
7643 quirk_insert(const char *class, const char *instance, const char *name,
7644 uint32_t quirk, int ws)
7648 bool failed = false;
7650 DNPRINTF(SWM_D_QUIRK, "quirk_insert: class: %s, instance: %s, name: %s,"
7651 " value: %u, ws: %d\n", class, instance, name, quirk, ws);
7653 if ((qp = malloc(sizeof *qp)) == NULL)
7654 err(1, "quirk_insert: malloc");
7656 if ((qp->class = strdup(class)) == NULL)
7657 err(1, "quirk_insert: strdup");
7658 if ((qp->instance = strdup(instance)) == NULL)
7659 err(1, "quirk_insert: strdup");
7660 if ((qp->name = strdup(name)) == NULL)
7661 err(1, "quirk_insert: strdup");
7663 if (asprintf(&str, "^%s$", class) == -1)
7664 err(1, "quirk_insert: asprintf");
7665 if (regcomp(&qp->regex_class, str, REG_EXTENDED | REG_NOSUB)) {
7666 add_startup_exception("regex failed to compile quirk 'class' "
7667 "field: %s", class);
7670 DNPRINTF(SWM_D_QUIRK, "quirk_insert: compiled: %s\n", str);
7673 if (asprintf(&str, "^%s$", instance) == -1)
7674 err(1, "quirk_insert: asprintf");
7675 if (regcomp(&qp->regex_instance, str, REG_EXTENDED | REG_NOSUB)) {
7676 add_startup_exception("regex failed to compile quirk 'instance'"
7677 " field: %s", instance);
7680 DNPRINTF(SWM_D_QUIRK, "quirk_insert: compiled: %s\n", str);
7683 if (asprintf(&str, "^%s$", name) == -1)
7684 err(1, "quirk_insert: asprintf");
7685 if (regcomp(&qp->regex_name, str, REG_EXTENDED | REG_NOSUB)) {
7686 add_startup_exception("regex failed to compile quirk 'name' "
7690 DNPRINTF(SWM_D_QUIRK, "quirk_insert: compiled: %s\n", str);
7694 DNPRINTF(SWM_D_QUIRK, "quirk_insert: regex error; skipping.\n");
7699 TAILQ_INSERT_TAIL(&quirks, qp, entry);
7701 DNPRINTF(SWM_D_QUIRK, "quirk_insert: leave\n");
7705 quirk_remove(struct quirk *qp)
7707 DNPRINTF(SWM_D_QUIRK, "quirk_remove: %s:%s [%u]\n", qp->class,
7708 qp->name, qp->quirk);
7710 TAILQ_REMOVE(&quirks, qp, entry);
7713 DNPRINTF(SWM_D_QUIRK, "quirk_remove: leave\n");
7717 quirk_free(struct quirk *qp)
7719 regfree(&qp->regex_class);
7720 regfree(&qp->regex_instance);
7721 regfree(&qp->regex_name);
7729 quirk_replace(struct quirk *qp, const char *class, const char *instance,
7730 const char *name, uint32_t quirk, int ws)
7732 DNPRINTF(SWM_D_QUIRK, "quirk_replace: %s:%s:%s [%u], ws: %d\n", qp->class,
7733 qp->instance, qp->name, qp->quirk, qp->ws);
7736 quirk_insert(class, instance, name, quirk, ws);
7738 DNPRINTF(SWM_D_QUIRK, "quirk_replace: leave\n");
7742 setquirk(const char *class, const char *instance, const char *name,
7743 uint32_t quirk, int ws)
7747 DNPRINTF(SWM_D_QUIRK, "setquirk: enter %s:%s:%s [%u], ws: %d\n", class,
7748 instance, name, quirk, ws);
7750 /* Remove/replace existing quirk. */
7751 TAILQ_FOREACH(qp, &quirks, entry) {
7752 if (strcmp(qp->class, class) == 0 &&
7753 strcmp(qp->instance, instance) == 0 &&
7754 strcmp(qp->name, name) == 0) {
7755 if (quirk == 0 && ws == -1)
7758 quirk_replace(qp, class, instance, name, quirk,
7760 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
7765 /* Only insert if quirk is not NONE or forced ws is set. */
7766 if (quirk || ws != -1)
7767 quirk_insert(class, instance, name, quirk, ws);
7769 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
7772 /* Eat '\' in str used to escape square brackets and colon. */
7774 unescape_selector(char *str)
7778 for (cp = str; *str != '\0'; ++str, ++cp) {
7779 if (*str == '\\' && (*(str + 1) == ':' || *(str + 1) == ']' ||
7789 setconfquirk(const char *selector, const char *value, int flags)
7791 char *str, *cp, *class;
7792 char *instance = NULL, *name = NULL;
7793 int retval, count = 0, ws = -1;
7796 /* suppress unused warning since var is needed */
7799 if (selector == NULL || strlen(selector) == 0)
7802 if ((str = strdup(selector)) == NULL)
7803 err(1, "setconfquirk: strdup");
7805 /* Find non-escaped colon. */
7812 for (++cp; *cp != '\0'; ++cp) {
7813 if (*cp == ':' && *(cp - 1) != '\\') {
7819 unescape_selector(class);
7821 instance = class + strlen(class) + 1;
7822 unescape_selector(instance);
7828 name = instance + strlen(instance) + 1;
7829 unescape_selector(name);
7834 DNPRINTF(SWM_D_CONF, "setconfquirk: class: %s, instance: %s, "
7835 "name: %s\n", class, instance, name);
7837 if ((retval = parsequirks(value, &qrks, &ws)) == 0)
7838 setquirk(class, instance, name, qrks, ws);
7847 setquirk("MPlayer", "xv", ".*",
7848 SWM_Q_FLOAT | SWM_Q_FULLSCREEN | SWM_Q_FOCUSPREV, -1);
7849 setquirk("OpenOffice.org 3.2", "VCLSalFrame", ".*",
7851 setquirk("Firefox-bin", "firefox-bin", ".*",
7853 setquirk("Firefox", "Dialog", ".*",
7855 setquirk("Gimp", "gimp", ".*",
7856 SWM_Q_FLOAT | SWM_Q_ANYWHERE, -1);
7857 setquirk("XTerm", "xterm", ".*",
7858 SWM_Q_XTERM_FONTADJ, -1);
7859 setquirk("xine", "Xine Window", ".*",
7860 SWM_Q_FLOAT | SWM_Q_ANYWHERE, -1);
7861 setquirk("Xitk", "Xitk Combo", ".*",
7862 SWM_Q_FLOAT | SWM_Q_ANYWHERE, -1);
7863 setquirk("xine", "xine Panel", ".*",
7864 SWM_Q_FLOAT | SWM_Q_ANYWHERE, -1);
7865 setquirk("Xitk", "Xine Window", ".*",
7866 SWM_Q_FLOAT | SWM_Q_ANYWHERE, -1);
7867 setquirk("xine", "xine Video Fullscreen Window", ".*",
7868 SWM_Q_FULLSCREEN | SWM_Q_FLOAT, -1);
7869 setquirk("pcb", "pcb", ".*",
7871 setquirk("SDL_App", "SDL_App", ".*",
7872 SWM_Q_FLOAT | SWM_Q_FULLSCREEN, -1);
7875 /* conf file stuff */
7876 #define SWM_CONF_FILE "spectrwm.conf"
7877 #define SWM_CONF_FILE_OLD "scrotwm.conf"
7881 SWM_S_BAR_AT_BOTTOM,
7882 SWM_S_BAR_BORDER_WIDTH,
7885 SWM_S_BAR_ENABLED_WS,
7890 SWM_S_BOUNDARY_WIDTH,
7891 SWM_S_CLOCK_ENABLED,
7894 SWM_S_CYCLE_VISIBLE,
7896 SWM_S_DISABLE_BORDER,
7898 SWM_S_FOCUS_CLOSE_WRAP,
7899 SWM_S_FOCUS_DEFAULT,
7901 SWM_S_ICONIC_ENABLED,
7902 SWM_S_JAVA_WORKAROUND,
7903 SWM_S_REGION_PADDING,
7908 SWM_S_STACK_ENABLED,
7911 SWM_S_URGENT_COLLAPSE,
7912 SWM_S_URGENT_ENABLED,
7913 SWM_S_VERBOSE_LAYOUT,
7915 SWM_S_WINDOW_CLASS_ENABLED,
7916 SWM_S_WINDOW_INSTANCE_ENABLED,
7917 SWM_S_WINDOW_NAME_ENABLED,
7918 SWM_S_WORKSPACE_LIMIT,
7919 SWM_S_WORKSPACE_NAME,
7923 setconfvalue(const char *selector, const char *value, int flags)
7925 struct workspace *ws;
7926 int i, ws_id, num_screens;
7927 char *b, *str, s[1024];
7930 case SWM_S_BAR_ACTION:
7932 if ((bar_argv[0] = expand_tilde(value)) == NULL)
7933 err(1, "setconfvalue: bar_action");
7935 case SWM_S_BAR_AT_BOTTOM:
7936 bar_at_bottom = (atoi(value) != 0);
7938 case SWM_S_BAR_BORDER_WIDTH:
7939 bar_border_width = atoi(value);
7940 if (bar_border_width < 0)
7941 bar_border_width = 0;
7943 case SWM_S_BAR_DELAY:
7944 /* No longer needed; leave to not break old conf files. */
7946 case SWM_S_BAR_ENABLED:
7947 bar_enabled = (atoi(value) != 0);
7949 case SWM_S_BAR_ENABLED_WS:
7950 ws_id = atoi(selector) - 1;
7951 if (ws_id < 0 || ws_id >= workspace_limit)
7952 errx(1, "setconfvalue: bar_enabled_ws: invalid "
7953 "workspace %d.", ws_id + 1);
7955 num_screens = get_screen_count();
7956 for (i = 0; i < num_screens; i++) {
7957 ws = (struct workspace *)&screens[i].ws;
7958 ws[ws_id].bar_enabled = (atoi(value) != 0);
7961 case SWM_S_BAR_FONT:
7963 if (asprintf(&bar_fonts, "%s,%s", value, bar_fonts) == -1)
7964 err(1, "setconfvalue: asprintf: failed to allocate "
7965 "memory for bar_fonts.");
7968 /* If already in xft mode, then we are done. */
7969 if (!bar_font_legacy)
7972 if ((str = strdup(value)) == NULL)
7973 err(1, "setconfvalue: strdup");
7975 /* If there are any non-XLFD entries, switch to Xft mode. */
7976 while ((b = strsep(&str, ",")) != NULL) {
7980 bar_font_legacy = false;
7987 case SWM_S_BAR_FORMAT:
7989 if ((bar_format = strdup(value)) == NULL)
7990 err(1, "setconfvalue: bar_format");
7992 case SWM_S_BAR_JUSTIFY:
7993 if (strcmp(value, "left") == 0)
7994 bar_justify = SWM_BAR_JUSTIFY_LEFT;
7995 else if (strcmp(value, "center") == 0)
7996 bar_justify = SWM_BAR_JUSTIFY_CENTER;
7997 else if (strcmp(value, "right") == 0)
7998 bar_justify = SWM_BAR_JUSTIFY_RIGHT;
8000 errx(1, "invalid bar_justify");
8002 case SWM_S_BORDER_WIDTH:
8003 border_width = atoi(value);
8004 if (border_width < 0)
8007 case SWM_S_BOUNDARY_WIDTH:
8008 boundary_width = atoi(value);
8009 if (boundary_width < 0)
8012 case SWM_S_CLOCK_ENABLED:
8013 clock_enabled = (atoi(value) != 0);
8015 case SWM_S_CLOCK_FORMAT:
8016 #ifndef SWM_DENY_CLOCK_FORMAT
8018 if ((clock_format = strdup(value)) == NULL)
8019 err(1, "setconfvalue: clock_format");
8022 case SWM_S_CYCLE_EMPTY:
8023 cycle_empty = (atoi(value) != 0);
8025 case SWM_S_CYCLE_VISIBLE:
8026 cycle_visible = (atoi(value) != 0);
8028 case SWM_S_DIALOG_RATIO:
8029 dialog_ratio = atof(value);
8030 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
8033 case SWM_S_DISABLE_BORDER:
8034 disable_border = (atoi(value) != 0);
8036 case SWM_S_FOCUS_CLOSE:
8037 if (strcmp(value, "first") == 0)
8038 focus_close = SWM_STACK_BOTTOM;
8039 else if (strcmp(value, "last") == 0)
8040 focus_close = SWM_STACK_TOP;
8041 else if (strcmp(value, "next") == 0)
8042 focus_close = SWM_STACK_ABOVE;
8043 else if (strcmp(value, "previous") == 0)
8044 focus_close = SWM_STACK_BELOW;
8046 errx(1, "focus_close");
8048 case SWM_S_FOCUS_CLOSE_WRAP:
8049 focus_close_wrap = (atoi(value) != 0);
8051 case SWM_S_FOCUS_DEFAULT:
8052 if (strcmp(value, "last") == 0)
8053 focus_default = SWM_STACK_TOP;
8054 else if (strcmp(value, "first") == 0)
8055 focus_default = SWM_STACK_BOTTOM;
8057 errx(1, "focus_default");
8059 case SWM_S_FOCUS_MODE:
8060 if (strcmp(value, "default") == 0)
8061 focus_mode = SWM_FOCUS_DEFAULT;
8062 else if (strcmp(value, "follow") == 0 ||
8063 strcmp(value, "follow_cursor") == 0)
8064 focus_mode = SWM_FOCUS_FOLLOW;
8065 else if (strcmp(value, "manual") == 0)
8066 focus_mode = SWM_FOCUS_MANUAL;
8068 errx(1, "focus_mode");
8070 case SWM_S_ICONIC_ENABLED:
8071 iconic_enabled = (atoi(value) != 0);
8073 case SWM_S_JAVA_WORKAROUND:
8074 java_workaround = (atoi(value) != 0);
8076 case SWM_S_REGION_PADDING:
8077 region_padding = atoi(value);
8078 if (region_padding < 0)
8081 case SWM_S_SPAWN_ORDER:
8082 if (strcmp(value, "first") == 0)
8083 spawn_position = SWM_STACK_BOTTOM;
8084 else if (strcmp(value, "last") == 0)
8085 spawn_position = SWM_STACK_TOP;
8086 else if (strcmp(value, "next") == 0)
8087 spawn_position = SWM_STACK_ABOVE;
8088 else if (strcmp(value, "previous") == 0)
8089 spawn_position = SWM_STACK_BELOW;
8091 errx(1, "spawn_position");
8093 case SWM_S_SPAWN_TERM:
8094 setconfspawn("term", value, 0);
8095 setconfspawn("spawn_term", value, 0);
8098 /* No longer needed; leave to not break old conf files. */
8100 case SWM_S_SS_ENABLED:
8101 /* No longer needed; leave to not break old conf files. */
8103 case SWM_S_STACK_ENABLED:
8104 stack_enabled = (atoi(value) != 0);
8106 case SWM_S_TERM_WIDTH:
8107 term_width = atoi(value);
8111 case SWM_S_TILE_GAP:
8112 tile_gap = atoi(value);
8114 case SWM_S_URGENT_COLLAPSE:
8115 urgent_collapse = (atoi(value) != 0);
8117 case SWM_S_URGENT_ENABLED:
8118 urgent_enabled = (atoi(value) != 0);
8120 case SWM_S_VERBOSE_LAYOUT:
8121 verbose_layout = (atoi(value) != 0);
8122 for (i = 0; layouts[i].l_stack != NULL; i++) {
8124 layouts[i].l_string = fancy_stacker;
8126 layouts[i].l_string = plain_stacker;
8129 case SWM_S_WARP_POINTER:
8130 warp_pointer = (atoi(value) != 0);
8132 case SWM_S_WINDOW_CLASS_ENABLED:
8133 window_class_enabled = (atoi(value) != 0);
8135 case SWM_S_WINDOW_INSTANCE_ENABLED:
8136 window_instance_enabled = (atoi(value) != 0);
8138 case SWM_S_WINDOW_NAME_ENABLED:
8139 window_name_enabled = (atoi(value) != 0);
8141 case SWM_S_WORKSPACE_LIMIT:
8142 workspace_limit = atoi(value);
8143 if (workspace_limit > SWM_WS_MAX)
8144 workspace_limit = SWM_WS_MAX;
8145 else if (workspace_limit < 1)
8146 workspace_limit = 1;
8148 ewmh_update_desktops();
8150 case SWM_S_WORKSPACE_NAME:
8151 if (getenv("SWM_STARTED") != NULL)
8155 if (sscanf(value, "ws[%d]:%1023c", &ws_id, s) != 2)
8156 errx(1, "invalid entry, should be 'ws[<idx>]:name'");
8158 if (ws_id < 0 || ws_id >= workspace_limit)
8159 errx(1, "setconfvalue: workspace_name: invalid "
8160 "workspace %d.", ws_id + 1);
8162 num_screens = get_screen_count();
8163 for (i = 0; i < num_screens; ++i) {
8164 ws = (struct workspace *)&screens[i].ws;
8166 if (strlen(s) > 0) {
8167 free(ws[ws_id].name);
8168 if ((ws[ws_id].name = strdup(s)) == NULL)
8169 err(1, "setconfvalue: workspace_name.");
8171 ewmh_update_desktop_names();
8172 ewmh_get_desktop_names();
8183 setconfmodkey(const char *selector, const char *value, int flags)
8185 /* suppress unused warnings since vars are needed */
8189 if (strncasecmp(value, "Mod1", strlen("Mod1")) == 0)
8190 update_modkey(XCB_MOD_MASK_1);
8191 else if (strncasecmp(value, "Mod2", strlen("Mod2")) == 0)
8192 update_modkey(XCB_MOD_MASK_2);
8193 else if (strncasecmp(value, "Mod3", strlen("Mod3")) == 0)
8194 update_modkey(XCB_MOD_MASK_3);
8195 else if (strncasecmp(value, "Mod4", strlen("Mod4")) == 0)
8196 update_modkey(XCB_MOD_MASK_4);
8203 setconfcolor(const char *selector, const char *value, int flags)
8205 int first, last, i = 0, num_screens;
8207 num_screens = get_screen_count();
8209 /* conf screen indices begin at 1; treat vals <= 0 as 'all screens.' */
8210 if (selector == NULL || strlen(selector) == 0 ||
8211 (last = atoi(selector) - 1) < 0) {
8213 last = num_screens - 1;
8218 if (last >= num_screens) {
8219 add_startup_exception("invalid screen index: %d out of bounds "
8220 "(maximum %d)", last + 1, num_screens);
8224 for (i = first; i <= last; ++i) {
8225 setscreencolor(value, i, flags);
8228 * When setting focus/unfocus colors, we need to also
8229 * set maximize colors to match if they haven't been customized.
8231 if (flags == SWM_S_COLOR_FOCUS &&
8232 !screens[i].c[SWM_S_COLOR_FOCUS_MAXIMIZED].manual)
8233 setscreencolor(value, i, SWM_S_COLOR_FOCUS_MAXIMIZED);
8234 else if (flags == SWM_S_COLOR_UNFOCUS &&
8235 !screens[i].c[SWM_S_COLOR_UNFOCUS_MAXIMIZED].manual)
8236 setscreencolor(value, i, SWM_S_COLOR_UNFOCUS_MAXIMIZED);
8238 screens[i].c[flags].manual = 1;
8245 setconfregion(const char *selector, const char *value, int flags)
8247 /* suppress unused warnings since vars are needed */
8251 custom_region(value);
8256 setautorun(const char *selector, const char *value, int flags)
8266 /* suppress unused warnings since vars are needed */
8270 if (getenv("SWM_STARTED"))
8274 if (sscanf(value, "ws[%d]:%1023c", &ws_id, s) != 2)
8275 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
8277 if (ws_id < 0 || ws_id >= workspace_limit)
8278 errx(1, "autorun: invalid workspace %d", ws_id + 1);
8280 sp = expand_tilde((char *)&s);
8283 * This is a little intricate
8285 * If the pid already exists we simply reuse it because it means it was
8286 * used before AND not claimed by manage_window. We get away with
8287 * altering it in the parent after INSERT because this can not be a race
8290 while ((ap = strsep(&sp, " \t")) != NULL) {
8293 DNPRINTF(SWM_D_SPAWN, "setautorun: arg [%s]\n", ap);
8295 if ((a.argv = realloc(a.argv, argc * sizeof(char *))) == NULL)
8296 err(1, "setautorun: realloc");
8297 a.argv[argc - 1] = ap;
8301 if ((a.argv = realloc(a.argv, (argc + 1) * sizeof(char *))) == NULL)
8302 err(1, "setautorun: realloc");
8303 a.argv[argc] = NULL;
8305 if ((pid = fork()) == 0) {
8306 spawn(ws_id, &a, true);
8315 p = calloc(1, sizeof *p);
8318 TAILQ_INSERT_TAIL(&pidlist, p, entry);
8328 setlayout(const char *selector, const char *value, int flags)
8330 struct workspace *ws;
8331 int ws_id, i, x, mg, ma, si, ar;
8332 int st = SWM_V_STACK, num_screens;
8336 /* suppress unused warnings since vars are needed */
8340 if (getenv("SWM_STARTED"))
8344 if (sscanf(value, "ws[%d]:%d:%d:%d:%d:%1023c",
8345 &ws_id, &mg, &ma, &si, &ar, s) != 6)
8346 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
8347 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
8350 if (ws_id < 0 || ws_id >= workspace_limit)
8351 errx(1, "layout: invalid workspace %d", ws_id + 1);
8353 if (strcasecmp(s, "vertical") == 0)
8355 else if (strcasecmp(s, "vertical_flip") == 0) {
8358 } else if (strcasecmp(s, "horizontal") == 0)
8360 else if (strcasecmp(s, "horizontal_flip") == 0) {
8363 } else if (strcasecmp(s, "fullscreen") == 0)
8366 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
8367 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
8370 num_screens = get_screen_count();
8371 for (i = 0; i < num_screens; i++) {
8372 ws = (struct workspace *)&screens[i].ws;
8373 ws[ws_id].cur_layout = &layouts[st];
8375 ws[ws_id].always_raise = (ar != 0);
8376 if (st == SWM_MAX_STACK)
8380 for (x = 0; x < abs(mg); x++) {
8381 ws[ws_id].cur_layout->l_config(&ws[ws_id],
8382 mg >= 0 ? SWM_ARG_ID_MASTERGROW :
8383 SWM_ARG_ID_MASTERSHRINK);
8386 for (x = 0; x < abs(ma); x++) {
8387 ws[ws_id].cur_layout->l_config(&ws[ws_id],
8388 ma >= 0 ? SWM_ARG_ID_MASTERADD :
8389 SWM_ARG_ID_MASTERDEL);
8392 for (x = 0; x < abs(si); x++) {
8393 ws[ws_id].cur_layout->l_config(&ws[ws_id],
8394 si >= 0 ? SWM_ARG_ID_STACKINC :
8395 SWM_ARG_ID_STACKDEC);
8399 ws[ws_id].cur_layout->l_config(&ws[ws_id],
8400 SWM_ARG_ID_FLIPLAYOUT);
8407 /* config options */
8408 struct config_option {
8410 int (*func)(const char*, const char*, int);
8413 struct config_option configopt[] = {
8414 { "autorun", setautorun, 0 },
8415 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
8416 { "bar_at_bottom", setconfvalue, SWM_S_BAR_AT_BOTTOM },
8417 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
8418 { "bar_border_unfocus", setconfcolor, SWM_S_COLOR_BAR_BORDER_UNFOCUS },
8419 { "bar_border_width", setconfvalue, SWM_S_BAR_BORDER_WIDTH },
8420 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
8421 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
8422 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
8423 { "bar_enabled_ws", setconfvalue, SWM_S_BAR_ENABLED_WS },
8424 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
8425 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
8426 { "bar_format", setconfvalue, SWM_S_BAR_FORMAT },
8427 { "bar_justify", setconfvalue, SWM_S_BAR_JUSTIFY },
8428 { "bind", setconfbinding, 0 },
8429 { "border_width", setconfvalue, SWM_S_BORDER_WIDTH },
8430 { "boundary_width", setconfvalue, SWM_S_BOUNDARY_WIDTH },
8431 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
8432 { "clock_format", setconfvalue, SWM_S_CLOCK_FORMAT },
8433 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
8434 { "color_focus_maximized", setconfcolor, SWM_S_COLOR_FOCUS_MAXIMIZED },
8435 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
8436 { "color_unfocus_maximized", setconfcolor, SWM_S_COLOR_UNFOCUS_MAXIMIZED },
8437 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
8438 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
8439 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
8440 { "disable_border", setconfvalue, SWM_S_DISABLE_BORDER },
8441 { "focus_close", setconfvalue, SWM_S_FOCUS_CLOSE },
8442 { "focus_close_wrap", setconfvalue, SWM_S_FOCUS_CLOSE_WRAP },
8443 { "focus_default", setconfvalue, SWM_S_FOCUS_DEFAULT },
8444 { "focus_mode", setconfvalue, SWM_S_FOCUS_MODE },
8445 { "iconic_enabled", setconfvalue, SWM_S_ICONIC_ENABLED },
8446 { "java_workaround", setconfvalue, SWM_S_JAVA_WORKAROUND },
8447 { "keyboard_mapping", setkeymapping, 0 },
8448 { "layout", setlayout, 0 },
8449 { "modkey", setconfmodkey, 0 },
8450 { "program", setconfspawn, 0 },
8451 { "quirk", setconfquirk, 0 },
8452 { "region", setconfregion, 0 },
8453 { "region_padding", setconfvalue, SWM_S_REGION_PADDING },
8454 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
8455 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
8456 { "spawn_position", setconfvalue, SWM_S_SPAWN_ORDER },
8457 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
8458 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
8459 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
8460 { "tile_gap", setconfvalue, SWM_S_TILE_GAP },
8461 { "title_class_enabled", setconfvalue, SWM_S_WINDOW_CLASS_ENABLED }, /* For backwards compat. */
8462 { "title_name_enabled", setconfvalue, SWM_S_WINDOW_INSTANCE_ENABLED }, /* For backwards compat. */
8463 { "urgent_collapse", setconfvalue, SWM_S_URGENT_COLLAPSE },
8464 { "urgent_enabled", setconfvalue, SWM_S_URGENT_ENABLED },
8465 { "verbose_layout", setconfvalue, SWM_S_VERBOSE_LAYOUT },
8466 { "warp_pointer", setconfvalue, SWM_S_WARP_POINTER },
8467 { "window_class_enabled", setconfvalue, SWM_S_WINDOW_CLASS_ENABLED },
8468 { "window_instance_enabled", setconfvalue, SWM_S_WINDOW_INSTANCE_ENABLED },
8469 { "window_name_enabled", setconfvalue, SWM_S_WINDOW_NAME_ENABLED },
8470 { "workspace_limit", setconfvalue, SWM_S_WORKSPACE_LIMIT },
8471 { "name", setconfvalue, SWM_S_WORKSPACE_NAME },
8475 _add_startup_exception(const char *fmt, va_list ap)
8477 if (vasprintf(&startup_exception, fmt, ap) == -1)
8478 warn("%s: asprintf", __func__);
8482 add_startup_exception(const char *fmt, ...)
8488 if (startup_exception)
8491 /* force bar to be enabled due to exception */
8495 _add_startup_exception(fmt, ap);
8500 conf_load(const char *filename, int keymapping)
8503 char *line = NULL, *cp, *ce, *optsub, *optval = NULL;
8504 size_t linelen, lineno = 0;
8505 int wordlen, i, optidx, count;
8506 struct config_option *opt = NULL;
8508 DNPRINTF(SWM_D_CONF, "conf_load: begin\n");
8510 if (filename == NULL) {
8511 warnx("conf_load: no filename");
8515 DNPRINTF(SWM_D_CONF, "conf_load: open %s\n", filename);
8517 if ((config = fopen(filename, "r")) == NULL) {
8518 warn("conf_load: fopen: %s", filename);
8522 while (!feof(config)) {
8526 if ((line = fparseln(config, &linelen, &lineno, NULL,
8527 FPARSELN_UNESCCOMM | FPARSELN_UNESCCONT)) == NULL) {
8529 err(1, "%s", filename);
8534 cp += strspn(cp, " \t\n"); /* eat whitespace */
8535 if (cp[0] == '\0') {
8539 /* get config option */
8540 wordlen = strcspn(cp, "=[ \t\n");
8542 add_startup_exception("%s: line %zd: no option found",
8547 for (i = 0; i < LENGTH(configopt); i++) {
8548 opt = &configopt[i];
8549 if (strncasecmp(cp, opt->optname, wordlen) == 0 &&
8550 (int)strlen(opt->optname) == wordlen) {
8556 add_startup_exception("%s: line %zd: unknown option "
8557 "%.*s", filename, lineno, wordlen, cp);
8560 if (keymapping && opt && strcmp(opt->optname, "bind")) {
8561 add_startup_exception("%s: line %zd: invalid option "
8562 "%.*s", filename, lineno, wordlen, cp);
8566 cp += strspn(cp, " \t\n"); /* eat whitespace */
8568 /* get [selector] if any */
8573 /* Get length of selector. */
8574 for (ce = ++cp; *ce != '\0'; ++ce) {
8575 /* Find matching (not escaped) bracket. */
8576 if (*ce == ']' && *(ce - 1) != '\\') {
8583 add_startup_exception("%s: line %zd: syntax "
8584 "error: unterminated selector", filename,
8589 /* ce points at ']'; terminate optsub. */
8594 cp += strspn(cp, "= \t\n"); /* eat trailing */
8597 if (strlen(optval) == 0) {
8598 add_startup_exception("%s: line %zd: must supply value "
8599 "to %s", filename, lineno,
8600 configopt[optidx].optname);
8603 /* call function to deal with it all */
8604 if (configopt[optidx].func(optsub, optval,
8605 configopt[optidx].funcflags) != 0) {
8606 add_startup_exception("%s: line %zd: invalid data for "
8607 "%s", filename, lineno, configopt[optidx].optname);
8615 DNPRINTF(SWM_D_CONF, "conf_load: end\n");
8621 set_child_transient(struct ws_win *win, xcb_window_t *trans)
8623 struct ws_win *parent, *w;
8624 struct swm_region *r;
8625 struct workspace *ws;
8626 xcb_icccm_wm_hints_t wmh;
8628 parent = find_window(win->transient);
8630 parent->focus_child = win;
8632 DNPRINTF(SWM_D_MISC, "set_child_transient: parent doesn't exist"
8633 " for %#x trans %#x\n", win->id, win->transient);
8635 r = root_to_region(win->s->root, SWM_CK_ALL);
8637 /* parent doen't exist in our window list */
8638 TAILQ_FOREACH(w, &ws->winlist, entry) {
8639 if (xcb_icccm_get_wm_hints_reply(conn,
8640 xcb_icccm_get_wm_hints(conn, w->id),
8642 warnx("can't get hints for %#x", w->id);
8646 if (win->hints.window_group != wmh.window_group)
8649 w->focus_child = win;
8650 win->transient = w->id;
8652 DNPRINTF(SWM_D_MISC, "set_child_transient: adjusting "
8653 "transient to %#x\n", win->transient);
8660 window_get_pid(xcb_window_t win)
8665 xcb_get_property_cookie_t pc;
8666 xcb_get_property_reply_t *pr;
8668 apid = get_atom_from_string("_NET_WM_PID");
8669 if (apid == XCB_ATOM_NONE)
8672 pc = xcb_get_property(conn, 0, win, apid, XCB_ATOM_CARDINAL, 0, 1);
8673 pr = xcb_get_property_reply(conn, pc, NULL);
8676 if (pr->type != XCB_ATOM_CARDINAL) {
8681 if (pr->type == apid && pr->format == 32)
8682 ret = *((pid_t *)xcb_get_property_value(pr));
8688 apid = get_atom_from_string("_SWM_PID");
8689 pc = xcb_get_property(conn, 0, win, apid, XCB_ATOM_STRING,
8691 pr = xcb_get_property_reply(conn, pc, NULL);
8694 if (pr->type != apid) {
8699 ret = (pid_t)strtonum(xcb_get_property_value(pr), 0, INT_MAX, &errstr);
8706 get_swm_ws(xcb_window_t id)
8712 xcb_get_property_reply_t *gpr;
8714 gpr = xcb_get_property_reply(conn,
8715 xcb_get_property(conn, 0, id, a_swm_ws,
8716 XCB_ATOM_STRING, 0, SWM_PROPLEN),
8721 proplen = xcb_get_property_value_length(gpr);
8723 prop = malloc(proplen + 1);
8726 xcb_get_property_value(gpr),
8728 prop[proplen] = '\0';
8735 DNPRINTF(SWM_D_PROP, "get_swm_ws: _SWM_WS: %s\n", prop);
8736 ws_idx = (int)strtonum(prop, 0, workspace_limit - 1, &errstr);
8738 DNPRINTF(SWM_D_PROP, "get_swm_ws: win #%s: %s",
8748 get_ws_idx(struct ws_win *win)
8750 xcb_get_property_reply_t *gpr;
8756 gpr = xcb_get_property_reply(conn,
8757 xcb_get_property(conn, 0, win->id, ewmh[_NET_WM_DESKTOP].atom,
8758 XCB_ATOM_CARDINAL, 0, 1),
8761 if (gpr->type == XCB_ATOM_CARDINAL && gpr->format == 32)
8762 ws_idx = *((int *)xcb_get_property_value(gpr));
8766 if (ws_idx == -1 && !(win->quirks & SWM_Q_IGNORESPAWNWS))
8767 ws_idx = get_swm_ws(win->id);
8769 if (ws_idx > workspace_limit - 1 || ws_idx < -1)
8772 DNPRINTF(SWM_D_PROP, "get_ws_idx: win %#x, ws_idx: %d\n", win->id,
8779 manage_window(xcb_window_t id, int spawn_pos, bool mapped)
8781 struct ws_win *win, *ww;
8782 struct swm_region *r;
8785 xcb_get_geometry_reply_t *gr;
8786 xcb_window_t trans = XCB_WINDOW_NONE;
8787 uint32_t i, wa[2], new_flags;
8788 int ws_idx, force_ws = -1;
8789 char *class, *instance, *name;
8791 if ((win = find_window(id)) != NULL) {
8792 DNPRINTF(SWM_D_MISC, "manage_window: win %#x already "
8793 "managed; skipping.)\n", id);
8794 return (win); /* Already managed. */
8797 /* See if window is on the unmanaged list. */
8798 if ((win = find_unmanaged_window(id)) != NULL) {
8799 DNPRINTF(SWM_D_MISC, "manage_window: win %#x found on "
8800 "unmanaged list.\n", id);
8801 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
8804 set_child_transient(win, &trans);
8808 DNPRINTF(SWM_D_MISC, "manage_window: win %#x is new.\n", id);
8811 /* Try to get initial window geometry. */
8812 gr = xcb_get_geometry_reply(conn, xcb_get_geometry(conn, id), NULL);
8814 DNPRINTF(SWM_D_MISC, "manage_window: get geometry failed.\n");
8818 /* Create and initialize ws_win object. */
8819 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
8820 err(1, "manage_window: calloc: failed to allocate memory for "
8825 /* Figureout which region the window belongs to. */
8826 r = root_to_region(gr->root, SWM_CK_ALL);
8828 /* Ignore window border if there is one. */
8829 WIDTH(win) = gr->width;
8830 HEIGHT(win) = gr->height;
8831 X(win) = gr->x + gr->border_width - border_width;
8832 Y(win) = gr->y + gr->border_width - border_width;
8833 win->bordered = true;
8834 win->mapped = mapped;
8835 win->s = r->s; /* this never changes */
8839 /* Select which X events to monitor and set border pixel color. */
8840 wa[0] = win->s->c[SWM_S_COLOR_UNFOCUS].pixel;
8841 wa[1] = XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_PROPERTY_CHANGE |
8842 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
8844 wa[1] |= XCB_EVENT_MASK_LEAVE_WINDOW | XCB_EVENT_MASK_FOCUS_CHANGE;
8847 xcb_change_window_attributes(conn, win->id, XCB_CW_BORDER_PIXEL |
8848 XCB_CW_EVENT_MASK, wa);
8850 /* Get WM_SIZE_HINTS. */
8851 xcb_icccm_get_wm_normal_hints_reply(conn,
8852 xcb_icccm_get_wm_normal_hints(conn, win->id),
8856 xcb_icccm_get_wm_hints_reply(conn,
8857 xcb_icccm_get_wm_hints(conn, win->id),
8860 /* Get WM_TRANSIENT_FOR; see if window is a transient. */
8861 xcb_icccm_get_wm_transient_for_reply(conn,
8862 xcb_icccm_get_wm_transient_for(conn, win->id),
8865 win->transient = trans;
8866 set_child_transient(win, &win->transient);
8869 /* Get WM_PROTOCOLS. */
8870 get_wm_protocols(win);
8872 /* Set initial quirks based on EWMH. */
8873 ewmh_autoquirk(win);
8875 /* Determine initial quirks. */
8876 xcb_icccm_get_wm_class_reply(conn,
8877 xcb_icccm_get_wm_class(conn, win->id),
8880 class = win->ch.class_name ? win->ch.class_name : "";
8881 instance = win->ch.instance_name ? win->ch.instance_name : "";
8882 name = get_win_name(win->id);
8884 DNPRINTF(SWM_D_CLASS, "manage_window: class: %s, instance: %s, "
8885 "name: %s\n", class, instance, name);
8887 /* java is retarded so treat it special */
8888 if (strstr(instance, "sun-awt")) {
8889 DNPRINTF(SWM_D_CLASS, "manage_window: java window detected.\n");
8893 TAILQ_FOREACH(qp, &quirks, entry) {
8894 if (regexec(&qp->regex_class, class, 0, NULL, 0) == 0 &&
8895 regexec(&qp->regex_instance, instance, 0, NULL, 0) == 0 &&
8896 regexec(&qp->regex_name, name, 0, NULL, 0) == 0) {
8897 DNPRINTF(SWM_D_CLASS, "manage_window: matched "
8898 "quirk: %s:%s:%s mask: %#x, ws: %d\n", qp->class,
8899 qp->instance, qp->name, qp->quirk, qp->ws);
8900 win->quirks = qp->quirk;
8901 if (qp->ws >= 0 && qp->ws < workspace_limit)
8908 /* Reset font sizes (the bruteforce way; no default keybinding). */
8909 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
8910 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
8911 fake_keypress(win, XK_KP_Subtract, XCB_MOD_MASK_SHIFT);
8912 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
8913 fake_keypress(win, XK_KP_Add, XCB_MOD_MASK_SHIFT);
8916 /* Figure out which workspace the window belongs to. */
8917 if (!(win->quirks & SWM_Q_IGNOREPID) &&
8918 (p = find_pid(window_get_pid(win->id))) != NULL) {
8919 win->ws = &r->s->ws[p->ws];
8920 TAILQ_REMOVE(&pidlist, p, entry);
8923 } else if ((ws_idx = get_ws_idx(win)) != -1 &&
8925 /* _SWM_WS is set; use that. */
8926 win->ws = &r->s->ws[ws_idx];
8927 } else if (trans && (ww = find_window(trans)) != NULL) {
8928 /* Launch transients in the same ws as parent. */
8935 win->ws = &r->s->ws[force_ws];
8937 /* Set the _NET_WM_DESKTOP atom. */
8938 DNPRINTF(SWM_D_PROP, "manage_window: set _NET_WM_DESKTOP: %d\n",
8940 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
8941 ewmh[_NET_WM_DESKTOP].atom, XCB_ATOM_CARDINAL, 32, 1, &win->ws->idx);
8943 /* Remove any _SWM_WS now that we set _NET_WM_DESKTOP. */
8944 xcb_delete_property(conn, win->id, a_swm_ws);
8946 /* WS must already be set for this to work. */
8947 store_float_geom(win);
8949 /* Make sure window is positioned inside its region, if its active. */
8951 region_containment(win, r, SWM_CW_ALLSIDES |
8952 SWM_CW_HARDBOUNDARY);
8957 /* Figure out where to stack the window in the workspace. */
8958 if (trans && (ww = find_window(trans)))
8959 TAILQ_INSERT_AFTER(&win->ws->winlist, ww, win, entry);
8960 else if (win->ws->focus && spawn_pos == SWM_STACK_ABOVE)
8961 TAILQ_INSERT_AFTER(&win->ws->winlist, win->ws->focus, win,
8963 else if (win->ws->focus && spawn_pos == SWM_STACK_BELOW)
8964 TAILQ_INSERT_BEFORE(win->ws->focus, win, entry);
8965 else switch (spawn_pos) {
8968 case SWM_STACK_ABOVE:
8969 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
8971 case SWM_STACK_BOTTOM:
8972 case SWM_STACK_BELOW:
8973 TAILQ_INSERT_HEAD(&win->ws->winlist, win, entry);
8976 ewmh_update_client_list();
8978 TAILQ_INSERT_TAIL(&win->ws->stack, win, stack_entry);
8980 /* Get/apply initial _NET_WM_STATE */
8981 ewmh_get_wm_state(win);
8984 new_flags = win->ewmh_flags;
8986 if (win->quirks & SWM_Q_FLOAT)
8987 new_flags |= EWMH_F_ABOVE;
8989 if (win->quirks & SWM_Q_ANYWHERE)
8990 new_flags |= SWM_F_MANUAL;
8992 ewmh_apply_flags(win, new_flags);
8993 ewmh_update_wm_state(win);
8995 /* Set initial _NET_WM_ALLOWED_ACTIONS */
8996 ewmh_update_actions(win);
9000 DNPRINTF(SWM_D_MISC, "manage_window: done. win %#x, (x,y) w x h: "
9001 "(%d,%d) %d x %d, ws: %d, iconic: %s, transient: %#x\n", win->id,
9002 X(win), Y(win), WIDTH(win), HEIGHT(win), win->ws->idx,
9003 YESNO(ICONIC(win)), win->transient);
9009 free_window(struct ws_win *win)
9011 DNPRINTF(SWM_D_MISC, "free_window: win %#x\n", win->id);
9016 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
9018 xcb_icccm_get_wm_class_reply_wipe(&win->ch);
9023 memset(win, 0xff, sizeof *win); /* XXX kill later */
9026 DNPRINTF(SWM_D_MISC, "free_window: done.\n");
9030 unmanage_window(struct ws_win *win)
9032 struct ws_win *parent;
9037 DNPRINTF(SWM_D_MISC, "unmanage_window: win %#x\n", win->id);
9040 parent = find_window(win->transient);
9042 parent->focus_child = NULL;
9045 TAILQ_REMOVE(&win->ws->stack, win, stack_entry);
9046 TAILQ_REMOVE(&win->ws->winlist, win, entry);
9047 TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
9049 ewmh_update_client_list();
9053 expose(xcb_expose_event_t *e)
9056 struct swm_region *r;
9058 DNPRINTF(SWM_D_EVENT, "expose: win %#x\n", e->window);
9060 num_screens = get_screen_count();
9061 for (i = 0; i < num_screens; i++)
9062 TAILQ_FOREACH(r, &screens[i].rl, entry)
9063 if (e->window == WINID(r->bar))
9071 focusin(xcb_focus_in_event_t *e)
9073 DNPRINTF(SWM_D_EVENT, "focusin: win %#x, mode: %s(%u), "
9074 "detail: %s(%u)\n", e->event, get_notify_mode_label(e->mode),
9075 e->mode, get_notify_detail_label(e->detail), e->detail);
9079 focusout(xcb_focus_out_event_t *e)
9081 DNPRINTF(SWM_D_EVENT, "focusout: win %#x, mode: %s(%u), "
9082 "detail: %s(%u)\n", e->event, get_notify_mode_label(e->mode),
9083 e->mode, get_notify_detail_label(e->detail), e->detail);
9088 keypress(xcb_key_press_event_t *e)
9090 xcb_keysym_t keysym;
9093 keysym = xcb_key_press_lookup_keysym(syms, e, 0);
9095 DNPRINTF(SWM_D_EVENT, "keypress: keysym: %u, win (x,y): %#x (%d,%d), "
9096 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
9097 "state: %u, same_screen: %s\n", keysym, e->event, e->event_x,
9098 e->event_y, e->detail, e->time, e->root, e->root_x, e->root_y,
9099 e->child, e->state, YESNO(e->same_screen));
9101 if ((kp = key_lookup(CLEANMASK(e->state), keysym)) == NULL)
9104 last_event_time = e->time;
9106 if (kp->funcid == KF_SPAWN_CUSTOM)
9107 spawn_custom(root_to_region(e->root, SWM_CK_ALL),
9108 &(keyfuncs[kp->funcid].args), kp->spawn_name);
9109 else if (keyfuncs[kp->funcid].func)
9110 keyfuncs[kp->funcid].func(root_to_region(e->root, SWM_CK_ALL),
9111 &(keyfuncs[kp->funcid].args));
9114 /* Unfreeze grab events. */
9115 xcb_allow_events(conn, XCB_ALLOW_ASYNC_KEYBOARD, e->time);
9118 DNPRINTF(SWM_D_EVENT, "keypress: done.\n");
9122 buttonpress(xcb_button_press_event_t *e)
9124 struct ws_win *win = NULL;
9125 struct swm_region *r, *old_r;
9127 bool handled = false;
9129 DNPRINTF(SWM_D_EVENT, "buttonpress: win (x,y): %#x (%d,%d), "
9130 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
9131 "state: %u, same_screen: %s\n", e->event, e->event_x, e->event_y,
9132 e->detail, e->time, e->root, e->root_x, e->root_y, e->child,
9133 e->state, YESNO(e->same_screen));
9135 if (e->event == e->root) {
9136 if (e->child != 0) {
9137 win = find_window(e->child);
9138 /* Pass ButtonPress to window if it isn't managed. */
9142 /* Focus on empty region */
9143 /* If no windows on region if its empty. */
9144 r = root_to_region(e->root, SWM_CK_POINTER);
9146 DNPRINTF(SWM_D_EVENT, "buttonpress: "
9147 "NULL region; ignoring.\n");
9151 if (TAILQ_EMPTY(&r->ws->winlist)) {
9152 old_r = root_to_region(e->root, SWM_CK_FOCUS);
9153 if (old_r && old_r != r)
9154 unfocus_win(old_r->ws->focus);
9156 xcb_set_input_focus(conn,
9157 XCB_INPUT_FOCUS_PARENT, e->root, e->time);
9159 /* Clear bar since empty. */
9167 win = find_window(e->event);
9173 last_event_time = e->time;
9175 focus_win(get_focus_magic(win));
9177 for (i = 0; i < LENGTH(buttons); i++)
9178 if (client_click == buttons[i].action && buttons[i].func &&
9179 buttons[i].button == e->detail &&
9180 CLEANMASK(buttons[i].mask) == CLEANMASK(e->state)) {
9181 buttons[i].func(win, &buttons[i].args);
9187 DNPRINTF(SWM_D_EVENT, "buttonpress: passing to window.\n");
9188 /* Replay event to event window */
9189 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, e->time);
9191 DNPRINTF(SWM_D_EVENT, "buttonpress: handled.\n");
9192 /* Unfreeze grab events. */
9193 xcb_allow_events(conn, XCB_ALLOW_SYNC_POINTER, e->time);
9201 print_win_geom(xcb_window_t w)
9203 xcb_get_geometry_reply_t *wa;
9205 wa = xcb_get_geometry_reply(conn, xcb_get_geometry(conn, w), NULL);
9207 DNPRINTF(SWM_D_MISC, "print_win_geom: window not found: %#x\n",
9212 DNPRINTF(SWM_D_MISC, "print_win_geom: win %#x, root: %#x, "
9213 "depth: %u, (x,y) w x h: (%d,%d) %d x %d, border: %d\n",
9214 w, wa->root, wa->depth, wa->x, wa->y, wa->width, wa->height,
9223 get_stack_mode_name(uint8_t mode)
9228 case XCB_STACK_MODE_ABOVE:
9231 case XCB_STACK_MODE_BELOW:
9234 case XCB_STACK_MODE_TOP_IF:
9237 case XCB_STACK_MODE_BOTTOM_IF:
9240 case XCB_STACK_MODE_OPPOSITE:
9252 configurerequest(xcb_configure_request_event_t *e)
9255 struct swm_region *r = NULL;
9257 uint32_t wc[7] = {0};
9261 if ((win = find_window(e->window)) == NULL)
9262 if ((win = find_unmanaged_window(e->window)) == NULL)
9266 if (swm_debug & SWM_D_EVENT) {
9267 print_win_geom(e->window);
9269 DNPRINTF(SWM_D_EVENT, "configurerequest: win %#x, "
9270 "parent: %#x, new: %s, value_mask: %u { ", e->window,
9271 e->parent, YESNO(new), e->value_mask);
9272 if (e->value_mask & XCB_CONFIG_WINDOW_X)
9273 DPRINTF("X: %d ", e->x);
9274 if (e->value_mask & XCB_CONFIG_WINDOW_Y)
9275 DPRINTF("Y: %d ", e->y);
9276 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH)
9277 DPRINTF("W: %u ", e->width);
9278 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
9279 DPRINTF("H: %u ", e->height);
9280 if (e->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH)
9281 DPRINTF("Border: %u ", e->border_width);
9282 if (e->value_mask & XCB_CONFIG_WINDOW_SIBLING)
9283 DPRINTF("Sibling: %#x ", e->sibling);
9284 if (e->value_mask & XCB_CONFIG_WINDOW_STACK_MODE)
9285 DPRINTF("StackMode: %s(%u) ",
9286 get_stack_mode_name(e->stack_mode), e->stack_mode);
9292 if (e->value_mask & XCB_CONFIG_WINDOW_X) {
9293 mask |= XCB_CONFIG_WINDOW_X;
9296 if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
9297 mask |= XCB_CONFIG_WINDOW_Y;
9300 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
9301 mask |= XCB_CONFIG_WINDOW_WIDTH;
9304 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
9305 mask |= XCB_CONFIG_WINDOW_HEIGHT;
9306 wc[i++] = e->height;
9308 if (e->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH) {
9309 mask |= XCB_CONFIG_WINDOW_BORDER_WIDTH;
9310 wc[i++] = e->border_width;
9312 if (e->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
9313 mask |= XCB_CONFIG_WINDOW_SIBLING;
9314 wc[i++] = e->sibling;
9316 if (e->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
9317 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
9318 wc[i++] = e->stack_mode;
9322 xcb_configure_window(conn, e->window, mask, wc);
9325 } else if ((!MANUAL(win) || win->quirks & SWM_Q_ANYWHERE) &&
9326 !FULLSCREEN(win) && !MAXIMIZED(win)) {
9329 else if (win->ws->old_r)
9332 /* windows are centered unless ANYWHERE quirk is set. */
9333 if (win->quirks & SWM_Q_ANYWHERE) {
9334 if (e->value_mask & XCB_CONFIG_WINDOW_X) {
9335 win->g_float.x = e->x;
9337 win->g_float.x -= X(r);
9340 if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
9341 win->g_float.y = e->y;
9343 win->g_float.y -= Y(r);
9347 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH)
9348 win->g_float.w = e->width;
9350 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
9351 win->g_float.h = e->height;
9353 win->g_floatvalid = true;
9355 if (!MAXIMIZED(win) && !FULLSCREEN(win) &&
9356 (TRANS(win) || (ABOVE(win) &&
9357 win->ws->cur_layout != &layouts[SWM_MAX_STACK]))) {
9358 WIDTH(win) = win->g_float.w;
9359 HEIGHT(win) = win->g_float.h;
9362 update_floater(win);
9377 DNPRINTF(SWM_D_EVENT, "configurerequest: done.\n");
9381 configurenotify(xcb_configure_notify_event_t *e)
9385 DNPRINTF(SWM_D_EVENT, "configurenotify: win %#x, event win: %#x, "
9386 "(x,y) WxH: (%d,%d) %ux%u, border: %u, above_sibling: %#x, "
9387 "override_redirect: %s\n", e->window, e->event, e->x, e->y,
9388 e->width, e->height, e->border_width, e->above_sibling,
9389 YESNO(e->override_redirect));
9391 win = find_window(e->window);
9393 xcb_icccm_get_wm_normal_hints_reply(conn,
9394 xcb_icccm_get_wm_normal_hints(conn, win->id),
9397 if (font_adjusted) {
9405 destroynotify(xcb_destroy_notify_event_t *e)
9409 DNPRINTF(SWM_D_EVENT, "destroynotify: win %#x\n", e->window);
9411 if ((win = find_window(e->window)) == NULL) {
9412 if ((win = find_unmanaged_window(e->window)) == NULL)
9418 if (focus_mode != SWM_FOCUS_FOLLOW) {
9419 /* If we were focused, make sure we focus on something else. */
9420 if (win == win->ws->focus)
9421 win->ws->focus_pending = get_focus_prev(win);
9424 unmanage_window(win);
9427 if (focus_mode != SWM_FOCUS_FOLLOW && WS_FOCUSED(win->ws)) {
9428 if (win->ws->focus_pending) {
9429 focus_win(win->ws->focus_pending);
9430 win->ws->focus_pending = NULL;
9431 } else if (win == win->ws->focus) {
9432 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT,
9433 win->ws->r->id, XCB_CURRENT_TIME);
9444 get_notify_detail_label(uint8_t detail)
9449 case XCB_NOTIFY_DETAIL_ANCESTOR:
9452 case XCB_NOTIFY_DETAIL_VIRTUAL:
9455 case XCB_NOTIFY_DETAIL_INFERIOR:
9458 case XCB_NOTIFY_DETAIL_NONLINEAR:
9459 label = "Nonlinear";
9461 case XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL:
9462 label = "NonlinearVirtual";
9464 case XCB_NOTIFY_DETAIL_POINTER:
9467 case XCB_NOTIFY_DETAIL_POINTER_ROOT:
9468 label = "PointerRoot";
9470 case XCB_NOTIFY_DETAIL_NONE:
9481 get_notify_mode_label(uint8_t mode)
9486 case XCB_NOTIFY_MODE_NORMAL:
9489 case XCB_NOTIFY_MODE_GRAB:
9492 case XCB_NOTIFY_MODE_UNGRAB:
9495 case XCB_NOTIFY_MODE_WHILE_GRABBED:
9496 label = "WhileGrabbed";
9507 enternotify(xcb_enter_notify_event_t *e)
9510 struct swm_region *r;
9512 DNPRINTF(SWM_D_FOCUS, "enternotify: time: %u, win (x,y): %#x "
9513 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
9514 "child: %#x, same_screen_focus: %s, state: %d\n",
9515 e->time, e->event, e->event_x, e->event_y,
9516 get_notify_mode_label(e->mode), e->mode,
9517 get_notify_detail_label(e->detail), e->detail,
9518 e->root, e->root_x, e->root_y, e->child,
9519 YESNO(e->same_screen_focus), e->state);
9521 if (focus_mode == SWM_FOCUS_MANUAL &&
9522 e->mode == XCB_NOTIFY_MODE_NORMAL) {
9523 DNPRINTF(SWM_D_EVENT, "enternotify: manual focus; ignoring.\n");
9527 last_event_time = e->time;
9529 if ((win = find_window(e->event)) == NULL) {
9530 if (e->event == e->root) {
9531 /* If no windows on pointer region, then focus root. */
9532 r = root_to_region(e->root, SWM_CK_POINTER);
9534 DNPRINTF(SWM_D_EVENT, "enternotify: "
9535 "NULL region; ignoring.\n");
9541 DNPRINTF(SWM_D_EVENT, "enternotify: window is NULL; "
9546 if (e->mode == XCB_NOTIFY_MODE_NORMAL &&
9547 e->detail == XCB_NOTIFY_DETAIL_INFERIOR) {
9548 DNPRINTF(SWM_D_EVENT, "enternotify: entering from "
9549 "inferior; ignoring\n");
9553 focus_win(get_focus_magic(win));
9561 leavenotify(xcb_leave_notify_event_t *e)
9563 DNPRINTF(SWM_D_FOCUS, "leavenotify: time: %u, win (x,y): %#x "
9564 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
9565 "child: %#x, same_screen_focus: %s, state: %d\n",
9566 e->time, e->event, e->event_x, e->event_y,
9567 get_notify_mode_label(e->mode), e->mode,
9568 get_notify_detail_label(e->detail), e->detail,
9569 e->root, e->root_x, e->root_y, e->child,
9570 YESNO(e->same_screen_focus), e->state);
9575 mapnotify(xcb_map_notify_event_t *e)
9577 struct ws_win *win, *parent = NULL;
9578 struct workspace *ws;
9580 DNPRINTF(SWM_D_EVENT, "mapnotify: win %#x\n", e->window);
9582 if ((win = manage_window(e->window, spawn_position, true)) == NULL)
9586 /* Need to know if win was mapped due to ws switch. */
9587 if (ws->state == SWM_WS_STATE_MAPPED) {
9588 if (ws->focus_pending && TRANS(ws->focus_pending))
9589 parent = find_window(win->transient);
9591 /* If window's parent is maximized, don't clear it. */
9592 if ((parent == NULL) || !MAXIMIZED(parent))
9593 if (clear_maximized(ws) > 0)
9598 set_win_state(win, XCB_ICCCM_WM_STATE_NORMAL);
9600 if (focus_mode != SWM_FOCUS_FOLLOW && WS_FOCUSED(win->ws)) {
9601 if (ws->focus_pending == win) {
9603 ws->focus_pending = NULL;
9604 center_pointer(win->ws->r);
9613 mappingnotify(xcb_mapping_notify_event_t *e)
9616 int i, j, num_screens;
9618 xcb_refresh_keyboard_mapping(syms, e);
9620 if (e->request == XCB_MAPPING_KEYBOARD) {
9623 /* Regrab buttons on all managed windows. */
9624 num_screens = get_screen_count();
9625 for (i = 0; i < num_screens; i++)
9626 for (j = 0; j < workspace_limit; j++)
9627 TAILQ_FOREACH(w, &screens[i].ws[j].winlist,
9634 maprequest(xcb_map_request_event_t *e)
9636 struct ws_win *win, *w = NULL;
9637 xcb_get_window_attributes_reply_t *war;
9639 DNPRINTF(SWM_D_EVENT, "maprequest: win %#x\n",
9642 war = xcb_get_window_attributes_reply(conn,
9643 xcb_get_window_attributes(conn, e->window),
9646 DNPRINTF(SWM_D_EVENT, "maprequest: window lost.\n");
9650 if (war->override_redirect) {
9651 DNPRINTF(SWM_D_EVENT, "maprequest: override_redirect; "
9656 win = manage_window(e->window, spawn_position,
9657 (war->map_state == XCB_MAP_STATE_VIEWABLE));
9661 /* The new window should get focus; prepare. */
9662 if (focus_mode != SWM_FOCUS_FOLLOW &&
9663 !(win->quirks & SWM_Q_NOFOCUSONMAP) &&
9664 (!(win->hints.flags & XCB_ICCCM_WM_HINT_INPUT) ||
9665 (win->hints.flags & XCB_ICCCM_WM_HINT_INPUT &&
9666 win->hints.input))) {
9667 if (win->quirks & SWM_Q_FOCUSONMAP_SINGLE) {
9668 /* See if other wins of same type are already mapped. */
9669 TAILQ_FOREACH(w, &win->ws->winlist, entry) {
9670 if (w == win || !w->mapped)
9673 if (w->ch.class_name &&
9674 win->ch.class_name &&
9675 strcmp(w->ch.class_name,
9676 win->ch.class_name) == 0 &&
9677 w->ch.instance_name &&
9678 win->ch.instance_name &&
9679 strcmp(w->ch.instance_name,
9680 win->ch.instance_name) == 0)
9686 win->ws->focus_pending = get_focus_magic(win);
9689 /* All windows need to be mapped if they are in the current workspace.*/
9693 /* Ignore EnterNotify to handle the mapnotify without interference. */
9694 if (focus_mode == SWM_FOCUS_DEFAULT)
9695 event_drain(XCB_ENTER_NOTIFY);
9698 DNPRINTF(SWM_D_EVENT, "maprequest: done.\n");
9702 motionnotify(xcb_motion_notify_event_t *e)
9704 struct swm_region *r = NULL;
9707 DNPRINTF(SWM_D_FOCUS, "motionnotify: time: %u, win (x,y): %#x "
9708 "(%d,%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
9709 "child: %#x, same_screen_focus: %s, state: %d\n",
9710 e->time, e->event, e->event_x, e->event_y,
9711 get_notify_detail_label(e->detail), e->detail,
9712 e->root, e->root_x, e->root_y, e->child,
9713 YESNO(e->same_screen), e->state);
9715 last_event_time = e->time;
9717 if (focus_mode == SWM_FOCUS_MANUAL)
9720 num_screens = get_screen_count();
9721 for (i = 0; i < num_screens; i++)
9722 if (screens[i].root == e->root)
9725 TAILQ_FOREACH(r, &screens[i].rl, entry)
9726 if (X(r) <= e->root_x && e->root_x < MAX_X(r) &&
9727 Y(r) <= e->root_y && e->root_y < MAX_Y(r))
9735 get_atom_name(xcb_atom_t atom)
9738 #ifdef SWM_DEBUG_ATOM_NAMES
9740 * This should be disabled during most debugging since
9741 * xcb_get_* causes an xcb_flush.
9744 xcb_get_atom_name_reply_t *r;
9746 r = xcb_get_atom_name_reply(conn,
9747 xcb_get_atom_name(conn, atom),
9750 len = xcb_get_atom_name_name_length(r);
9752 name = malloc(len + 1);
9754 memcpy(name, xcb_get_atom_name_name(r), len);
9768 propertynotify(xcb_property_notify_event_t *e)
9771 struct workspace *ws;
9775 name = get_atom_name(e->atom);
9776 DNPRINTF(SWM_D_EVENT, "propertynotify: win %#x, atom: %s(%u), "
9777 "time: %#x, state: %u\n", e->window, name, e->atom, e->time,
9781 win = find_window(e->window);
9787 last_event_time = e->time;
9789 if (e->atom == a_state) {
9790 /* State just changed, make sure it gets focused if mapped. */
9791 if (e->state == XCB_PROPERTY_NEW_VALUE) {
9792 if (focus_mode != SWM_FOCUS_FOLLOW && WS_FOCUSED(ws)) {
9794 ws->focus_pending == win) {
9795 focus_win(ws->focus_pending);
9796 ws->focus_pending = NULL;
9800 } else if (e->atom == XCB_ATOM_WM_CLASS ||
9801 e->atom == XCB_ATOM_WM_NAME) {
9803 } else if (e->atom == a_prot) {
9804 get_wm_protocols(win);
9811 unmapnotify(xcb_unmap_notify_event_t *e)
9814 struct workspace *ws;
9816 DNPRINTF(SWM_D_EVENT, "unmapnotify: win %#x\n", e->window);
9818 /* If we aren't managing the window, then ignore. */
9819 win = find_window(e->window);
9820 if (win == NULL || win->id != e->window)
9823 /* Do nothing if already withdrawn. */
9824 if (!win->mapped && !ICONIC(win))
9828 win->mapped = false;
9830 /* If win was focused, make sure to focus on something else. */
9831 if (win == ws->focus) {
9832 if (focus_mode != SWM_FOCUS_FOLLOW) {
9833 ws->focus_pending = get_focus_prev(win);
9834 DNPRINTF(SWM_D_EVENT, "unmapnotify: "
9835 "focus_pending: %#x\n",
9836 WINID(ws->focus_pending));
9844 set_win_state(win, XCB_ICCCM_WM_STATE_ICONIC);
9847 set_win_state(win, XCB_ICCCM_WM_STATE_WITHDRAWN);
9848 unmanage_window(win);
9854 /* Update focus if ws is active. */
9855 if (WS_FOCUSED(ws)) {
9856 if (focus_mode == SWM_FOCUS_FOLLOW) {
9857 focus_win(get_pointer_win(ws->r->s->root));
9858 } else if (ws->focus_pending) {
9859 focus_win(ws->focus_pending);
9860 ws->focus_pending = NULL;
9861 } else if (ws->focus == NULL) {
9862 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT,
9863 ws->r->id, XCB_CURRENT_TIME);
9867 center_pointer(ws->r);
9873 get_source_type_label(uint32_t type)
9878 case EWMH_SOURCE_TYPE_NONE:
9881 case EWMH_SOURCE_TYPE_NORMAL:
9884 case EWMH_SOURCE_TYPE_OTHER:
9896 clientmessage(xcb_client_message_event_t *e)
9899 struct swm_region *r = NULL;
9903 xcb_map_request_event_t mre;
9907 name = get_atom_name(e->type);
9908 DNPRINTF(SWM_D_EVENT, "clientmessage: win %#x, atom: %s(%u)\n",
9909 e->window, name, e->type);
9913 if (e->type == ewmh[_NET_CURRENT_DESKTOP].atom) {
9914 num_screens = get_screen_count();
9915 for (i = 0; i < num_screens; i++)
9916 if (screens[i].root == e->window) {
9917 r = screens[i].r_focus;
9921 if (r && e->data.data32[0] < (uint32_t)workspace_limit) {
9922 a.id = e->data.data32[0];
9930 win = find_window(e->window);
9932 if (e->type == ewmh[_NET_ACTIVE_WINDOW].atom) {
9933 /* Manage the window with maprequest. */
9934 DNPRINTF(SWM_D_EVENT, "clientmessage: request focus on "
9935 "unmanaged window.\n");
9936 mre.window = e->window;
9942 if (e->type == ewmh[_NET_ACTIVE_WINDOW].atom) {
9943 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_ACTIVE_WINDOW, "
9944 "source_type: %s(%d)\n",
9945 get_source_type_label(e->data.data32[0]),
9949 * Allow focus changes that are a result of direct user
9950 * action and from applications that use the old EWMH spec.
9952 if (e->data.data32[0] != EWMH_SOURCE_TYPE_NORMAL ||
9953 win->quirks & SWM_Q_OBEYAPPFOCUSREQ) {
9954 if (WS_FOCUSED(win->ws))
9957 win->ws->focus_pending = win;
9959 } else if (e->type == ewmh[_NET_CLOSE_WINDOW].atom) {
9960 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_CLOSE_WINDOW\n");
9961 if (win->can_delete)
9962 client_msg(win, a_delete, 0);
9964 xcb_kill_client(conn, win->id);
9965 } else if (e->type == ewmh[_NET_MOVERESIZE_WINDOW].atom) {
9966 DNPRINTF(SWM_D_EVENT,
9967 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
9969 if (e->data.data32[0] & (1<<8)) /* x */
9970 X(win) = e->data.data32[1];
9971 if (e->data.data32[0] & (1<<9)) /* y */
9972 Y(win) = e->data.data32[2];
9973 if (e->data.data32[0] & (1<<10)) /* width */
9974 WIDTH(win) = e->data.data32[3];
9975 if (e->data.data32[0] & (1<<11)) /* height */
9976 HEIGHT(win) = e->data.data32[4];
9980 /* Notify no change was made. */
9981 config_win(win, NULL);
9982 /* TODO: Change stack sizes */
9984 } else if (e->type == ewmh[_NET_RESTACK_WINDOW].atom) {
9985 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_RESTACK_WINDOW\n");
9986 val[0] = e->data.data32[1]; /* Sibling window. */
9987 val[1] = e->data.data32[2]; /* Stack mode detail. */
9989 xcb_configure_window(conn, win->id, XCB_CONFIG_WINDOW_SIBLING |
9990 XCB_CONFIG_WINDOW_STACK_MODE, val);
9991 } else if (e->type == ewmh[_NET_WM_STATE].atom) {
9992 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_WM_STATE\n");
9993 ewmh_change_wm_state(win, e->data.data32[1], e->data.data32[0]);
9994 if (e->data.data32[2])
9995 ewmh_change_wm_state(win, e->data.data32[2],
9998 ewmh_update_wm_state(win);
10000 } else if (e->type == ewmh[_NET_WM_DESKTOP].atom) {
10001 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_WM_DESKTOP\n");
10004 win_to_ws(win, e->data.data32[0], true);
10006 /* Restack if either the source or destination ws is mapped. */
10007 if (r != NULL || win->ws->r != NULL) {
10009 load_float_geom(win);
10021 int errcode = xcb_connection_has_error(conn);
10022 #ifdef XCB_CONN_ERROR
10025 case XCB_CONN_ERROR:
10026 s = "Socket error, pipe error or other stream error.";
10028 case XCB_CONN_CLOSED_EXT_NOTSUPPORTED:
10029 s = "Extension not supported.";
10031 case XCB_CONN_CLOSED_MEM_INSUFFICIENT:
10032 s = "Insufficient memory.";
10034 case XCB_CONN_CLOSED_REQ_LEN_EXCEED:
10035 s = "Request length was exceeded.";
10037 case XCB_CONN_CLOSED_PARSE_ERR:
10038 s = "Error parsing display string.";
10041 s = "Unknown error.";
10044 errx(errcode, "X CONNECTION ERROR: %s", s);
10047 errx(errcode, "X CONNECTION ERROR");
10054 int num_screens, i;
10055 const uint32_t val = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
10056 XCB_EVENT_MASK_ENTER_WINDOW;
10058 xcb_void_cookie_t wac;
10059 xcb_generic_error_t *error;
10061 /* this causes an error if some other window manager is running */
10062 num_screens = get_screen_count();
10063 for (i = 0; i < num_screens; i++) {
10064 if ((sc = get_screen(i)) == NULL)
10065 errx(1, "ERROR: can't get screen %d.", i);
10066 DNPRINTF(SWM_D_INIT, "enable_wm: screen %d, root: %#x\n",
10068 wac = xcb_change_window_attributes_checked(conn, sc->root,
10069 XCB_CW_EVENT_MASK, &val);
10070 if ((error = xcb_request_check(conn, wac))) {
10071 DNPRINTF(SWM_D_INIT, "enable_wm: error_code: %u\n",
10072 error->error_code);
10077 /* click to focus on empty region */
10078 xcb_grab_button(conn, 1, sc->root, BUTTONMASK,
10079 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC, XCB_WINDOW_NONE,
10080 XCB_CURSOR_NONE, XCB_BUTTON_INDEX_1, XCB_BUTTON_MASK_ANY);
10087 new_region(struct swm_screen *s, int x, int y, int w, int h)
10089 struct swm_region *r = NULL, *n;
10090 struct workspace *ws = NULL;
10094 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
10095 s->idx, w, h, x, y);
10097 /* remove any conflicting regions */
10098 n = TAILQ_FIRST(&s->rl);
10101 n = TAILQ_NEXT(r, entry);
10102 if (X(r) < (x + w) && (X(r) + WIDTH(r)) > x &&
10103 Y(r) < (y + h) && (Y(r) + HEIGHT(r)) > y) {
10104 if (r->ws->r != NULL)
10105 r->ws->old_r = r->ws->r;
10108 xcb_destroy_window(conn, r->id);
10109 TAILQ_REMOVE(&s->rl, r, entry);
10110 TAILQ_INSERT_TAIL(&s->orl, r, entry);
10114 /* search old regions for one to reuse */
10116 /* size + location match */
10117 TAILQ_FOREACH(r, &s->orl, entry)
10118 if (X(r) == x && Y(r) == y &&
10119 HEIGHT(r) == h && WIDTH(r) == w)
10123 TAILQ_FOREACH(r, &s->orl, entry)
10124 if (HEIGHT(r) == h && WIDTH(r) == w)
10128 TAILQ_REMOVE(&s->orl, r, entry);
10129 /* try to use old region's workspace */
10130 if (r->ws->r == NULL)
10133 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
10134 err(1, "new_region: calloc: failed to allocate memory "
10137 /* if we don't have a workspace already, find one */
10139 for (i = 0; i < workspace_limit; i++)
10140 if (s->ws[i].r == NULL) {
10147 errx(1, "new_region: no free workspaces");
10149 if (ws->state == SWM_WS_STATE_HIDDEN)
10150 ws->state = SWM_WS_STATE_MAPPING;
10158 r->ws_prior = NULL;
10161 TAILQ_INSERT_TAIL(&s->rl, r, entry);
10163 /* Invisible region window to detect pointer events on empty regions. */
10164 r->id = xcb_generate_id(conn);
10165 wa[0] = XCB_EVENT_MASK_POINTER_MOTION |
10166 XCB_EVENT_MASK_POINTER_MOTION_HINT;
10168 xcb_create_window(conn, XCB_COPY_FROM_PARENT, r->id, r->s->root,
10169 X(r), Y(r), WIDTH(r), HEIGHT(r), 0, XCB_WINDOW_CLASS_INPUT_ONLY,
10170 XCB_COPY_FROM_PARENT, XCB_CW_EVENT_MASK, wa);
10172 /* Make sure region input is at the bottom. */
10173 wa[0] = XCB_STACK_MODE_BELOW;
10174 xcb_configure_window(conn, r->id, XCB_CONFIG_WINDOW_STACK_MODE, wa);
10176 xcb_map_window(conn, r->id);
10180 scan_randr(int idx)
10182 #ifdef SWM_XRR_HAS_CRTC
10185 #endif /* SWM_XRR_HAS_CRTC */
10186 struct swm_region *r;
10187 struct ws_win *win;
10189 xcb_randr_get_screen_resources_current_cookie_t src;
10190 xcb_randr_get_screen_resources_current_reply_t *srr;
10191 xcb_randr_get_crtc_info_cookie_t cic;
10192 xcb_randr_get_crtc_info_reply_t *cir = NULL;
10193 xcb_randr_crtc_t *crtc;
10194 xcb_screen_t *screen;
10196 DNPRINTF(SWM_D_MISC, "scan_randr: screen: %d\n", idx);
10198 if ((screen = get_screen(idx)) == NULL)
10199 errx(1, "ERROR: can't get screen %d.", idx);
10201 num_screens = get_screen_count();
10202 if (idx >= num_screens)
10203 errx(1, "scan_randr: invalid screen");
10205 /* remove any old regions */
10206 while ((r = TAILQ_FIRST(&screens[idx].rl)) != NULL) {
10207 r->ws->old_r = r->ws->r = NULL;
10209 xcb_destroy_window(conn, r->id);
10210 TAILQ_REMOVE(&screens[idx].rl, r, entry);
10211 TAILQ_INSERT_TAIL(&screens[idx].orl, r, entry);
10215 /* map virtual screens onto physical screens */
10216 #ifdef SWM_XRR_HAS_CRTC
10217 if (randr_support) {
10218 src = xcb_randr_get_screen_resources_current(conn,
10219 screens[idx].root);
10220 srr = xcb_randr_get_screen_resources_current_reply(conn, src,
10223 new_region(&screens[idx], 0, 0,
10224 screen->width_in_pixels,
10225 screen->height_in_pixels);
10228 ncrtc = srr->num_crtcs;
10230 crtc = xcb_randr_get_screen_resources_current_crtcs(srr);
10231 for (c = 0; c < ncrtc; c++) {
10232 cic = xcb_randr_get_crtc_info(conn, crtc[c],
10234 cir = xcb_randr_get_crtc_info_reply(conn, cic, NULL);
10237 if (cir->num_outputs == 0) {
10242 if (cir->mode == 0)
10243 new_region(&screens[idx], 0, 0,
10244 screen->width_in_pixels,
10245 screen->height_in_pixels);
10247 new_region(&screens[idx],
10248 cir->x, cir->y, cir->width, cir->height);
10253 #endif /* SWM_XRR_HAS_CRTC */
10255 /* If detection failed, create a single region that spans the screen. */
10256 if (TAILQ_EMPTY(&screens[idx].rl))
10257 new_region(&screens[idx], 0, 0, screen->width_in_pixels,
10258 screen->height_in_pixels);
10261 /* Cleanup unused previously visible workspaces. */
10262 TAILQ_FOREACH(r, &screens[idx].orl, entry) {
10263 TAILQ_FOREACH(win, &r->ws->winlist, entry)
10265 r->ws->state = SWM_WS_STATE_HIDDEN;
10267 /* The screen shouldn't focus on an unused region. */
10268 if (screens[idx].r_focus == r)
10269 screens[idx].r_focus = NULL;
10272 DNPRINTF(SWM_D_MISC, "scan_randr: done.\n");
10276 screenchange(xcb_randr_screen_change_notify_event_t *e)
10278 struct swm_region *r;
10279 int i, num_screens;
10281 DNPRINTF(SWM_D_EVENT, "screenchange: root: %#x\n", e->root);
10283 num_screens = get_screen_count();
10284 /* silly event doesn't include the screen index */
10285 for (i = 0; i < num_screens; i++)
10286 if (screens[i].root == e->root)
10288 if (i >= num_screens)
10289 errx(1, "screenchange: screen not found");
10291 /* brute force for now, just re-enumerate the regions */
10295 print_win_geom(e->root);
10297 /* add bars to all regions */
10298 for (i = 0; i < num_screens; i++) {
10299 TAILQ_FOREACH(r, &screens[i].rl, entry)
10305 /* Make sure a region has focus on each screen. */
10306 for (i = 0; i < num_screens; i++) {
10307 if (screens[i].r_focus == NULL) {
10308 r = TAILQ_FIRST(&screens[i].rl);
10317 /* Update workspace state on all regions. */
10318 for (i = 0; i < num_screens; i++)
10319 TAILQ_FOREACH(r, &screens[i].rl, entry)
10320 r->ws->state = SWM_WS_STATE_MAPPED;
10326 struct swm_region *r = NULL;
10327 xcb_window_t *wins = NULL, trans, *cwins = NULL;
10328 int i, j, k, n, no, num_screens;
10330 bool manage, mapped;
10332 xcb_query_tree_cookie_t qtc;
10333 xcb_query_tree_reply_t *qtr;
10334 xcb_get_window_attributes_cookie_t gac;
10335 xcb_get_window_attributes_reply_t *gar;
10336 xcb_get_property_cookie_t pc;
10337 xcb_get_property_reply_t *pr;
10339 DNPRINTF(SWM_D_INIT, "grab_windows: begin\n");
10340 num_screens = get_screen_count();
10341 for (i = 0; i < num_screens; i++) {
10342 qtc = xcb_query_tree(conn, screens[i].root);
10343 qtr = xcb_query_tree_reply(conn, qtc, NULL);
10346 wins = xcb_query_tree_children(qtr);
10347 no = xcb_query_tree_children_length(qtr);
10349 /* Try to sort windows according to _NET_CLIENT_LIST. */
10350 pr = xcb_get_property_reply(conn, xcb_get_property(conn, 0,
10351 screens[i].root, ewmh[_NET_CLIENT_LIST].atom,
10352 XCB_ATOM_WINDOW, 0, UINT32_MAX), NULL);
10354 cwins = xcb_get_property_value(pr);
10355 n = xcb_get_property_value_length(pr) /
10356 sizeof(xcb_atom_t);
10358 for (j = 0; j < n; ++j) {
10359 for (k = j; k < no; ++k) {
10360 if (wins[k] == cwins[j]) {
10361 /* Swap wins j and k. */
10363 wins[j] = cwins[j];
10371 /* attach windows to a region */
10372 /* normal windows */
10373 DNPRINTF(SWM_D_INIT, "grab_windows: grab top level windows.\n");
10374 for (j = 0; j < no; j++) {
10375 TAILQ_FOREACH(r, &screens[i].rl, entry) {
10376 if (r->id == wins[j]) {
10377 DNPRINTF(SWM_D_INIT, "grab_windows: "
10378 "skip %#x; region input window.\n",
10381 } else if (r->bar->id == wins[j]) {
10382 DNPRINTF(SWM_D_INIT, "grab_windows: "
10383 "skip %#x; region bar.\n",
10392 gac = xcb_get_window_attributes(conn, wins[j]);
10393 gar = xcb_get_window_attributes_reply(conn, gac, NULL);
10395 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
10396 "doesn't exist.\n", wins[j]);
10400 if (gar->override_redirect) {
10401 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
10402 "override_redirect set.\n", wins[j]);
10407 pc = xcb_icccm_get_wm_transient_for(conn, wins[j]);
10408 if (xcb_icccm_get_wm_transient_for_reply(conn, pc,
10410 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
10411 "is transient for %#x.\n", wins[j], trans);
10416 state = get_win_state(wins[j]);
10417 manage = state != XCB_ICCCM_WM_STATE_WITHDRAWN;
10418 mapped = gar->map_state == XCB_MAP_STATE_VIEWABLE;
10419 if (mapped || manage)
10420 manage_window(wins[j], SWM_STACK_TOP, mapped);
10423 /* transient windows */
10424 DNPRINTF(SWM_D_INIT, "grab_windows: grab transient windows.\n");
10425 for (j = 0; j < no; j++) {
10426 gac = xcb_get_window_attributes(conn, wins[j]);
10427 gar = xcb_get_window_attributes_reply(conn, gac, NULL);
10429 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
10430 "doesn't exist.\n", wins[j]);
10434 if (gar->override_redirect) {
10435 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
10436 "override_redirect set.\n", wins[j]);
10441 state = get_win_state(wins[j]);
10442 manage = state != XCB_ICCCM_WM_STATE_WITHDRAWN;
10443 mapped = gar->map_state == XCB_MAP_STATE_VIEWABLE;
10444 pc = xcb_icccm_get_wm_transient_for(conn, wins[j]);
10445 if (xcb_icccm_get_wm_transient_for_reply(conn, pc,
10446 &trans, NULL) && manage)
10447 manage_window(wins[j], SWM_STACK_TOP, mapped);
10452 DNPRINTF(SWM_D_INIT, "grab_windows: done.\n");
10456 setup_screens(void)
10458 int i, j, k, num_screens;
10459 struct workspace *ws;
10460 uint32_t gcv[1], wa[1];
10461 const xcb_query_extension_reply_t *qep;
10462 xcb_screen_t *screen;
10463 xcb_randr_query_version_cookie_t c;
10464 xcb_randr_query_version_reply_t *r;
10466 num_screens = get_screen_count();
10467 if ((screens = calloc(num_screens,
10468 sizeof(struct swm_screen))) == NULL)
10469 err(1, "setup_screens: calloc: failed to allocate memory for "
10472 /* Initial RandR setup. */
10473 randr_support = false;
10474 qep = xcb_get_extension_data(conn, &xcb_randr_id);
10475 if (qep->present) {
10476 c = xcb_randr_query_version(conn, 1, 1);
10477 r = xcb_randr_query_version_reply(conn, c, NULL);
10479 if (r->major_version >= 1) {
10480 randr_support = true;
10481 randr_eventbase = qep->first_event;
10487 wa[0] = cursors[XC_LEFT_PTR].cid;
10489 /* map physical screens */
10490 for (i = 0; i < num_screens; i++) {
10491 DNPRINTF(SWM_D_WS, "setup_screens: init screen: %d\n", i);
10492 screens[i].idx = i;
10493 screens[i].r_focus = NULL;
10495 TAILQ_INIT(&screens[i].rl);
10496 TAILQ_INIT(&screens[i].orl);
10497 if ((screen = get_screen(i)) == NULL)
10498 errx(1, "ERROR: can't get screen %d.", i);
10499 screens[i].root = screen->root;
10501 /* set default colors */
10502 setscreencolor("red", i, SWM_S_COLOR_FOCUS);
10503 setscreencolor("rgb:88/88/88", i, SWM_S_COLOR_UNFOCUS);
10504 setscreencolor("rgb:00/80/80", i, SWM_S_COLOR_BAR_BORDER);
10505 setscreencolor("rgb:00/40/40", i,
10506 SWM_S_COLOR_BAR_BORDER_UNFOCUS);
10507 setscreencolor("black", i, SWM_S_COLOR_BAR);
10508 setscreencolor("rgb:a0/a0/a0", i, SWM_S_COLOR_BAR_FONT);
10509 setscreencolor("red", i, SWM_S_COLOR_FOCUS_MAXIMIZED);
10510 setscreencolor("rgb:88/88/88", i,
10511 SWM_S_COLOR_UNFOCUS_MAXIMIZED);
10513 /* create graphics context on screen */
10514 screens[i].bar_gc = xcb_generate_id(conn);
10516 xcb_create_gc(conn, screens[i].bar_gc, screens[i].root,
10517 XCB_GC_GRAPHICS_EXPOSURES, gcv);
10519 /* set default cursor */
10520 xcb_change_window_attributes(conn, screens[i].root,
10521 XCB_CW_CURSOR, wa);
10523 /* init all workspaces */
10524 /* XXX these should be dynamically allocated too */
10525 for (j = 0; j < SWM_WS_MAX; j++) {
10526 ws = &screens[i].ws[j];
10529 ws->bar_enabled = true;
10531 ws->focus_prev = NULL;
10532 ws->focus_pending = NULL;
10535 ws->state = SWM_WS_STATE_HIDDEN;
10536 TAILQ_INIT(&ws->stack);
10537 TAILQ_INIT(&ws->winlist);
10538 TAILQ_INIT(&ws->unmanagedlist);
10540 for (k = 0; layouts[k].l_stack != NULL; k++)
10541 if (layouts[k].l_config != NULL)
10542 layouts[k].l_config(ws,
10543 SWM_ARG_ID_STACKINIT);
10544 ws->cur_layout = &layouts[0];
10545 ws->cur_layout->l_string(ws);
10551 xcb_randr_select_input(conn, screens[i].root,
10552 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE);
10557 setup_globals(void)
10559 if ((bar_fonts = strdup(SWM_BAR_FONTS)) == NULL)
10560 err(1, "setup_globals: strdup: failed to allocate memory.");
10562 if ((clock_format = strdup("%a %b %d %R %Z %Y")) == NULL)
10563 err(1, "setup_globals: strdup: failed to allocate memory.");
10565 if ((syms = xcb_key_symbols_alloc(conn)) == NULL)
10566 errx(1, "unable to allocate key symbols");
10568 a_state = get_atom_from_string("WM_STATE");
10569 a_prot = get_atom_from_string("WM_PROTOCOLS");
10570 a_delete = get_atom_from_string("WM_DELETE_WINDOW");
10571 a_net_supported = get_atom_from_string("_NET_SUPPORTED");
10572 a_net_wm_check = get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
10573 a_takefocus = get_atom_from_string("WM_TAKE_FOCUS");
10574 a_utf8_string = get_atom_from_string("UTF8_STRING");
10575 a_swm_ws = get_atom_from_string("_SWM_WS");
10579 shutdown_cleanup(void)
10581 int i, num_screens;
10583 /* disable alarm because the following code may not be interrupted */
10585 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
10586 err(1, "can't disable alarm");
10595 num_screens = get_screen_count();
10596 for (i = 0; i < num_screens; ++i) {
10597 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT,
10598 screens[i].root, XCB_CURRENT_TIME);
10600 if (screens[i].bar_gc != XCB_NONE)
10601 xcb_free_gc(conn, screens[i].bar_gc);
10602 if (!bar_font_legacy) {
10603 XftColorFree(display, DefaultVisual(display, i),
10604 DefaultColormap(display, i), &bar_font_color);
10605 XftColorFree(display, DefaultVisual(display, i),
10606 DefaultColormap(display, i), &search_font_color);
10610 if (bar_font_legacy)
10611 XFreeFontSet(display, bar_fs);
10613 XftFontClose(display, bar_font);
10616 xcb_key_symbols_free(syms);
10618 xcb_disconnect(conn);
10622 event_error(xcb_generic_error_t *e)
10626 DNPRINTF(SWM_D_EVENT, "event_error: %s(%u) from %s(%u), sequence: %u, "
10627 "resource_id: %u, minor_code: %u\n",
10628 xcb_event_get_error_label(e->error_code), e->error_code,
10629 xcb_event_get_request_label(e->major_code), e->major_code,
10630 e->sequence, e->resource_id, e->minor_code);
10634 event_handle(xcb_generic_event_t *evt)
10636 uint8_t type = XCB_EVENT_RESPONSE_TYPE(evt);
10638 DNPRINTF(SWM_D_EVENT, "XCB Event: %s(%d), seq %u\n",
10639 xcb_event_get_label(XCB_EVENT_RESPONSE_TYPE(evt)),
10640 XCB_EVENT_RESPONSE_TYPE(evt), evt->sequence);
10643 #define EVENT(type, callback) case type: callback((void *)evt); return
10644 EVENT(0, event_error);
10645 EVENT(XCB_BUTTON_PRESS, buttonpress);
10646 /*EVENT(XCB_BUTTON_RELEASE, buttonpress);*/
10647 /*EVENT(XCB_CIRCULATE_NOTIFY, );*/
10648 /*EVENT(XCB_CIRCULATE_REQUEST, );*/
10649 EVENT(XCB_CLIENT_MESSAGE, clientmessage);
10650 /*EVENT(XCB_COLORMAP_NOTIFY, );*/
10651 EVENT(XCB_CONFIGURE_NOTIFY, configurenotify);
10652 EVENT(XCB_CONFIGURE_REQUEST, configurerequest);
10653 /*EVENT(XCB_CREATE_NOTIFY, );*/
10654 EVENT(XCB_DESTROY_NOTIFY, destroynotify);
10655 EVENT(XCB_ENTER_NOTIFY, enternotify);
10656 EVENT(XCB_EXPOSE, expose);
10658 EVENT(XCB_FOCUS_IN, focusin);
10659 EVENT(XCB_FOCUS_OUT, focusout);
10661 /*EVENT(XCB_GRAPHICS_EXPOSURE, );*/
10662 /*EVENT(XCB_GRAVITY_NOTIFY, );*/
10663 EVENT(XCB_KEY_PRESS, keypress);
10664 /*EVENT(XCB_KEY_RELEASE, keypress);*/
10665 /*EVENT(XCB_KEYMAP_NOTIFY, );*/
10667 EVENT(XCB_LEAVE_NOTIFY, leavenotify);
10669 EVENT(XCB_MAP_NOTIFY, mapnotify);
10670 EVENT(XCB_MAP_REQUEST, maprequest);
10671 EVENT(XCB_MAPPING_NOTIFY, mappingnotify);
10672 EVENT(XCB_MOTION_NOTIFY, motionnotify);
10673 /*EVENT(XCB_NO_EXPOSURE, );*/
10674 EVENT(XCB_PROPERTY_NOTIFY, propertynotify);
10675 /*EVENT(XCB_REPARENT_NOTIFY, );*/
10676 /*EVENT(XCB_RESIZE_REQUEST, );*/
10677 /*EVENT(XCB_SELECTION_CLEAR, );*/
10678 /*EVENT(XCB_SELECTION_NOTIFY, );*/
10679 /*EVENT(XCB_SELECTION_REQUEST, );*/
10680 EVENT(XCB_UNMAP_NOTIFY, unmapnotify);
10681 /*EVENT(XCB_VISIBILITY_NOTIFY, );*/
10684 if (type - randr_eventbase == XCB_RANDR_SCREEN_CHANGE_NOTIFY)
10685 screenchange((void *)evt);
10689 main(int argc, char *argv[])
10691 struct swm_region *r;
10692 char conf[PATH_MAX], *cfile = NULL;
10694 int xfd, i, num_screens;
10695 struct sigaction sact;
10696 xcb_generic_event_t *evt;
10701 bool stdin_ready = false, startup = true;
10703 /* suppress unused warning since var is needed */
10707 time_started = time(NULL);
10711 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION, buildstr);
10712 if (setlocale(LC_CTYPE, "") == NULL || setlocale(LC_TIME, "") == NULL)
10713 warnx("no locale support");
10715 /* handle some signals */
10716 bzero(&sact, sizeof(sact));
10717 sigemptyset(&sact.sa_mask);
10719 sact.sa_handler = sighdlr;
10720 sigaction(SIGINT, &sact, NULL);
10721 sigaction(SIGQUIT, &sact, NULL);
10722 sigaction(SIGTERM, &sact, NULL);
10723 sigaction(SIGHUP, &sact, NULL);
10725 sact.sa_handler = sighdlr;
10726 sact.sa_flags = SA_NOCLDSTOP;
10727 sigaction(SIGCHLD, &sact, NULL);
10729 if ((display = XOpenDisplay(0)) == NULL)
10730 errx(1, "can not open display");
10732 conn = XGetXCBConnection(display);
10733 if (xcb_connection_has_error(conn))
10734 errx(1, "can not get XCB connection");
10736 XSetEventQueueOwner(display, XCBOwnsEventQueue);
10738 xcb_prefetch_extension_data(conn, &xcb_randr_id);
10739 xfd = xcb_get_file_descriptor(conn);
10741 /* look for local and global conf file */
10742 pwd = getpwuid(getuid());
10744 errx(1, "invalid user: %d", getuid());
10746 xcb_grab_server(conn);
10747 xcb_aux_sync(conn);
10749 /* flush all events */
10750 while ((evt = xcb_poll_for_event(conn))) {
10751 if (XCB_EVENT_RESPONSE_TYPE(evt) == 0)
10757 errx(1, "another window manager is currently running");
10759 /* Load Xcursors and/or cursorfont glyph cursors. */
10762 xcb_aux_sync(conn);
10772 for (i = 0; ; i++) {
10777 snprintf(conf, sizeof conf, "%s/.%s",
10778 pwd->pw_dir, SWM_CONF_FILE);
10782 snprintf(conf, sizeof conf, "/etc/%s",
10787 snprintf(conf, sizeof conf, "%s/.%s",
10788 pwd->pw_dir, SWM_CONF_FILE_OLD);
10791 /* global compat */
10792 snprintf(conf, sizeof conf, "/etc/%s",
10793 SWM_CONF_FILE_OLD);
10799 if (strlen(conf) && stat(conf, &sb) != -1)
10800 if (S_ISREG(sb.st_mode)) {
10807 /* load conf (if any) */
10809 conf_load(cfile, SWM_CONF_DEFAULT);
10813 if (getenv("SWM_STARTED") == NULL)
10814 setenv("SWM_STARTED", "YES", 1);
10816 /* setup all bars */
10817 num_screens = get_screen_count();
10818 for (i = 0; i < num_screens; i++)
10819 TAILQ_FOREACH(r, &screens[i].rl, entry)
10822 /* Manage existing windows. */
10829 xcb_ungrab_server(conn);
10832 /* Update state of each newly mapped workspace. */
10833 for (i = 0; i < num_screens; i++)
10834 TAILQ_FOREACH(r, &screens[i].rl, entry)
10835 r->ws->state = SWM_WS_STATE_MAPPED;
10837 rd_max = xfd > STDIN_FILENO ? xfd : STDIN_FILENO;
10840 while ((evt = xcb_poll_for_event(conn))) {
10847 /* If just (re)started, set default focus if needed. */
10851 if (focus_mode != SWM_FOCUS_FOLLOW) {
10852 r = TAILQ_FIRST(&screens[0].rl);
10864 FD_SET(STDIN_FILENO, &rd);
10869 num_readable = select(rd_max + 1, &rd, NULL, NULL, &tv);
10870 if (num_readable == -1 && errno != EINTR) {
10871 DNPRINTF(SWM_D_MISC, "select failed");
10872 } else if (num_readable > 0 && FD_ISSET(STDIN_FILENO, &rd)) {
10873 stdin_ready = true;
10877 restart(NULL, NULL);
10886 stdin_ready = false;
10887 bar_extra_update();
10894 shutdown_cleanup();