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-2013 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__)
91 #include <X11/cursorfont.h>
92 #include <X11/extensions/Xrandr.h>
93 #include <X11/Xcursor/Xcursor.h>
94 #include <X11/Xft/Xft.h>
95 #include <X11/Xlib-xcb.h>
96 #include <xcb/xcb_atom.h>
97 #include <xcb/xcb_aux.h>
98 #include <xcb/xcb_event.h>
99 #include <xcb/xcb_icccm.h>
100 #include <xcb/xcb_keysyms.h>
101 #include <xcb/xtest.h>
102 #include <xcb/randr.h>
110 #ifdef SPECTRWM_BUILDSTR
111 static const char *buildstr = SPECTRWM_BUILDSTR;
113 static const char *buildstr = SPECTRWM_VERSION;
116 #if !defined(__CYGWIN__) /* cygwin chokes on xrandr stuff */
118 # error XRandR versions less than 1.0 are not supported
121 # if RANDR_MAJOR >= 1
122 # if RANDR_MINOR >= 2
123 # define SWM_XRR_HAS_CRTC
126 #endif /* __CYGWIN__ */
128 #ifndef XCB_ICCCM_NUM_WM_HINTS_ELEMENTS
129 #define XCB_ICCCM_SIZE_HINT_P_MIN_SIZE XCB_SIZE_HINT_P_MIN_SIZE
130 #define XCB_ICCCM_SIZE_HINT_P_MAX_SIZE XCB_SIZE_HINT_P_MAX_SIZE
131 #define XCB_ICCCM_SIZE_HINT_P_RESIZE_INC XCB_SIZE_HINT_P_RESIZE_INC
132 #define XCB_ICCCM_WM_HINT_INPUT XCB_WM_HINT_INPUT
133 #define XCB_ICCCM_WM_HINT_X_URGENCY XCB_WM_HINT_X_URGENCY
134 #define XCB_ICCCM_WM_STATE_ICONIC XCB_WM_STATE_ICONIC
135 #define XCB_ICCCM_WM_STATE_WITHDRAWN XCB_WM_STATE_WITHDRAWN
136 #define XCB_ICCCM_WM_STATE_NORMAL XCB_WM_STATE_NORMAL
137 #define xcb_icccm_get_text_property_reply_t xcb_get_text_property_reply_t
138 #define xcb_icccm_get_text_property_reply_wipe xcb_get_text_property_reply_wipe
139 #define xcb_icccm_get_wm_class xcb_get_wm_class
140 #define xcb_icccm_get_wm_class_reply xcb_get_wm_class_reply
141 #define xcb_icccm_get_wm_class_reply_t xcb_get_wm_class_reply_t
142 #define xcb_icccm_get_wm_class_reply_wipe xcb_get_wm_class_reply_wipe
143 #define xcb_icccm_get_wm_hints xcb_get_wm_hints
144 #define xcb_icccm_wm_hints_get_urgency xcb_wm_hints_get_urgency
145 #define xcb_icccm_get_wm_hints_reply xcb_get_wm_hints_reply
146 #define xcb_icccm_get_wm_name xcb_get_wm_name
147 #define xcb_icccm_get_wm_name_reply xcb_get_wm_name_reply
148 #define xcb_icccm_get_wm_normal_hints xcb_get_wm_normal_hints
149 #define xcb_icccm_get_wm_normal_hints_reply xcb_get_wm_normal_hints_reply
150 #define xcb_icccm_get_wm_protocols xcb_get_wm_protocols
151 #define xcb_icccm_get_wm_protocols_reply xcb_get_wm_protocols_reply
152 #define xcb_icccm_get_wm_protocols_reply_t xcb_get_wm_protocols_reply_t
153 #define xcb_icccm_get_wm_protocols_reply_wipe xcb_get_wm_protocols_reply_wipe
154 #define xcb_icccm_get_wm_transient_for xcb_get_wm_transient_for
155 #define xcb_icccm_get_wm_transient_for_reply xcb_get_wm_transient_for_reply
156 #define xcb_icccm_wm_hints_t xcb_wm_hints_t
159 /*#define SWM_DEBUG*/
161 #define DPRINTF(x...) do { \
163 fprintf(stderr, x); \
165 #define DNPRINTF(n,x...) do { \
166 if (swm_debug & n) { \
167 fprintf(stderr, "%ld ", (long)(time(NULL) - time_started)); \
168 fprintf(stderr, x); \
171 #define SWM_D_MISC 0x0001
172 #define SWM_D_EVENT 0x0002
173 #define SWM_D_WS 0x0004
174 #define SWM_D_FOCUS 0x0008
175 #define SWM_D_MOVE 0x0010
176 #define SWM_D_STACK 0x0020
177 #define SWM_D_MOUSE 0x0040
178 #define SWM_D_PROP 0x0080
179 #define SWM_D_CLASS 0x0100
180 #define SWM_D_KEY 0x0200
181 #define SWM_D_QUIRK 0x0400
182 #define SWM_D_SPAWN 0x0800
183 #define SWM_D_EVENTQ 0x1000
184 #define SWM_D_CONF 0x2000
185 #define SWM_D_BAR 0x4000
186 #define SWM_D_INIT 0x8000
188 u_int32_t swm_debug = 0
207 #define DPRINTF(x...)
208 #define DNPRINTF(n,x...)
211 /* convert 8-bit to 16-bit */
212 #define RGB_8_TO_16(col) ((col) << 8) + (col)
214 #define PIXEL_TO_XRENDERCOLOR(px, xrc) \
215 xrc.red = RGB_8_TO_16((px) >> 16 & 0xff); \
216 xrc.green = RGB_8_TO_16((px) >> 8 & 0xff); \
217 xrc.blue = RGB_8_TO_16((px) & 0xff); \
220 #define LENGTH(x) (int)(sizeof (x) / sizeof (x)[0])
221 #define MODKEY XCB_MOD_MASK_1
222 #define CLEANMASK(mask) ((mask) & ~(numlockmask | XCB_MOD_MASK_LOCK))
223 #define BUTTONMASK (XCB_EVENT_MASK_BUTTON_PRESS | \
224 XCB_EVENT_MASK_BUTTON_RELEASE)
225 #define MOUSEMASK (BUTTONMASK|XCB_EVENT_MASK_POINTER_MOTION)
226 #define SWM_PROPLEN (16)
227 #define SWM_FUNCNAME_LEN (32)
228 #define SWM_KEYS_LEN (255)
229 #define SWM_QUIRK_LEN (64)
230 #define X(r) (r)->g.x
231 #define Y(r) (r)->g.y
232 #define WIDTH(r) (r)->g.w
233 #define HEIGHT(r) (r)->g.h
234 #define BORDER(w) ((w)->bordered ? border_width : 0)
235 #define MAX_X(r) ((r)->g.x + (r)->g.w)
236 #define MAX_Y(r) ((r)->g.y + (r)->g.h)
237 #define SH_MIN(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE
238 #define SH_MIN_W(w) (w)->sh.min_width
239 #define SH_MIN_H(w) (w)->sh.min_height
240 #define SH_MAX(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MAX_SIZE
241 #define SH_MAX_W(w) (w)->sh.max_width
242 #define SH_MAX_H(w) (w)->sh.max_height
243 #define SH_INC(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_RESIZE_INC
244 #define SH_INC_W(w) (w)->sh.width_inc
245 #define SH_INC_H(w) (w)->sh.height_inc
246 #define SWM_MAX_FONT_STEPS (3)
247 #define WINID(w) ((w) ? (w)->id : XCB_WINDOW_NONE)
248 #define WS_FOCUSED(ws) ((ws)->r && (ws)->r->s->r_focus == (ws)->r)
249 #define YESNO(x) ((x) ? "yes" : "no")
251 /* Constrain Window flags */
252 #define SWM_CW_RESIZABLE (0x01)
253 #define SWM_CW_SOFTBOUNDARY (0x02)
254 #define SWM_CW_HARDBOUNDARY (0x04)
255 #define SWM_CW_RIGHT (0x10)
256 #define SWM_CW_LEFT (0x20)
257 #define SWM_CW_BOTTOM (0x40)
258 #define SWM_CW_TOP (0x80)
259 #define SWM_CW_ALLSIDES (0xf0)
261 #define SWM_FOCUS_DEFAULT (0)
262 #define SWM_FOCUS_FOLLOW (1)
263 #define SWM_FOCUS_MANUAL (2)
265 #define SWM_CK_NONE 0
266 #define SWM_CK_ALL 0xf
267 #define SWM_CK_FOCUS 0x1
268 #define SWM_CK_POINTER 0x2
269 #define SWM_CK_FALLBACK 0x4
270 #define SWM_CK_REGION 0x8
272 #define SWM_CONF_DEFAULT (0)
273 #define SWM_CONF_KEYMAPPING (1)
276 #define SWM_LIB "/usr/local/lib/libswmhack.so"
283 xcb_atom_t a_takefocus;
284 xcb_atom_t a_utf8_string;
285 xcb_atom_t a_swm_iconic;
287 volatile sig_atomic_t running = 1;
288 volatile sig_atomic_t restart_wm = 0;
289 xcb_timestamp_t last_event_time = 0;
293 int xrandr_eventbase;
294 unsigned int numlockmask = 0;
297 xcb_connection_t *conn;
298 xcb_key_symbols_t *syms;
300 int boundary_width = 50;
302 int cycle_visible = 0;
304 int font_adjusted = 0;
305 unsigned int mod_key = MODKEY;
308 struct swm_region *search_r;
309 int select_list_pipe[2];
310 int select_resp_pipe[2];
312 volatile sig_atomic_t search_resp;
313 int search_resp_action;
315 struct search_window {
316 TAILQ_ENTRY(search_window) entry;
320 xcb_window_t indicator;
322 TAILQ_HEAD(search_winlist, search_window);
323 struct search_winlist search_wl;
328 SWM_SEARCH_UNICONIFY,
329 SWM_SEARCH_NAME_WORKSPACE,
330 SWM_SEARCH_SEARCH_WORKSPACE,
331 SWM_SEARCH_SEARCH_WINDOW
334 #define SWM_STACK_TOP (0)
335 #define SWM_STACK_BOTTOM (1)
336 #define SWM_STACK_ABOVE (2)
337 #define SWM_STACK_BELOW (3)
340 double dialog_ratio = 0.6;
342 #define SWM_BAR_MAX (256)
343 #define SWM_BAR_JUSTIFY_LEFT (0)
344 #define SWM_BAR_JUSTIFY_CENTER (1)
345 #define SWM_BAR_JUSTIFY_RIGHT (2)
346 #define SWM_BAR_OFFSET (4)
347 #define SWM_BAR_FONTS "-*-terminus-medium-*-*-*-12-*-*-*-*-*-*-*," \
348 "-*-profont-*-*-*-*-12-*-*-*-*-*-*-*," \
349 "-*-times-medium-r-*-*-12-*-*-*-*-*-*-*," \
350 "-misc-fixed-medium-r-*-*-12-*-*-*-*-*-*-*," \
351 "-*-*-*-r-*-*-*-*-*-*-*-*-*-*"
353 #ifdef X_HAVE_UTF8_STRING
354 #define DRAWSTRING(x...) Xutf8DrawString(x)
356 #define DRAWSTRING(x...) XmbDrawString(x)
359 char *bar_argv[] = { NULL, NULL };
361 char bar_ext[SWM_BAR_MAX];
362 char bar_ext_buf[SWM_BAR_MAX];
363 char bar_vertext[SWM_BAR_MAX];
366 int bar_border_width = 1;
367 int bar_at_bottom = 0;
371 int bar_justify = SWM_BAR_JUSTIFY_LEFT;
372 char *bar_format = NULL;
373 int stack_enabled = 1;
374 int clock_enabled = 1;
375 int urgent_enabled = 0;
376 char *clock_format = NULL;
377 int title_name_enabled = 0;
378 int title_class_enabled = 0;
379 int window_name_enabled = 0;
380 int focus_mode = SWM_FOCUS_DEFAULT;
381 int focus_close = SWM_STACK_BELOW;
382 int focus_close_wrap = 1;
383 int focus_default = SWM_STACK_TOP;
384 int spawn_position = SWM_STACK_TOP;
385 int disable_border = 0;
386 int border_width = 1;
387 int region_padding = 0;
389 int verbose_layout = 0;
393 XFontSetExtents *bar_fs_extents;
395 int bar_font_legacy = 1;
397 XftColor bar_font_color;
399 char *startup_exception;
400 unsigned int nr_exceptions = 0;
402 /* layout manager data */
403 struct swm_geometry {
416 struct swm_geometry g;
419 /* virtual "screens" */
421 TAILQ_ENTRY(swm_region) entry;
423 struct swm_geometry g;
424 struct workspace *ws; /* current workspace on this region */
425 struct workspace *ws_prior; /* prior workspace on this region */
426 struct swm_screen *s; /* screen idx */
429 TAILQ_HEAD(swm_region_list, swm_region);
432 TAILQ_ENTRY(ws_win) entry;
434 xcb_window_t transient;
435 struct ws_win *focus_child; /* focus on child transient */
436 struct swm_geometry g; /* current geometry */
437 struct swm_geometry g_float; /* region coordinates */
438 int g_floatvalid; /* g_float geometry validity */
439 int floatmaxed; /* whether maxed by max_stack */
445 unsigned int ewmh_flags;
446 int font_size_boundary[SWM_MAX_FONT_STEPS];
452 unsigned long quirks;
453 struct workspace *ws; /* always valid */
454 struct swm_screen *s; /* always valid, never changes */
455 xcb_get_geometry_reply_t *wa;
457 xcb_icccm_get_wm_class_reply_t ch;
458 xcb_icccm_wm_hints_t hints;
460 TAILQ_HEAD(ws_win_list, ws_win);
464 TAILQ_ENTRY(pid_e) entry;
468 TAILQ_HEAD(pid_list, pid_e);
469 struct pid_list pidlist = TAILQ_HEAD_INITIALIZER(pidlist);
471 /* layout handlers */
473 void vertical_config(struct workspace *, int);
474 void vertical_stack(struct workspace *, struct swm_geometry *);
475 void horizontal_config(struct workspace *, int);
476 void horizontal_stack(struct workspace *, struct swm_geometry *);
477 void max_stack(struct workspace *, struct swm_geometry *);
478 void plain_stacker(struct workspace *);
479 void fancy_stacker(struct workspace *);
482 void (*l_stack)(struct workspace *, struct swm_geometry *);
483 void (*l_config)(struct workspace *, int);
485 #define SWM_L_FOCUSPREV (1<<0)
486 #define SWM_L_MAPONFOCUS (1<<1)
487 void (*l_string)(struct workspace *);
489 /* stack, configure */
490 { vertical_stack, vertical_config, 0, plain_stacker },
491 { horizontal_stack, horizontal_config, 0, plain_stacker },
493 SWM_L_MAPONFOCUS | SWM_L_FOCUSPREV, plain_stacker },
494 { NULL, NULL, 0, NULL },
497 /* position of max_stack mode in the layouts array, index into layouts! */
498 #define SWM_V_STACK (0)
499 #define SWM_H_STACK (1)
500 #define SWM_MAX_STACK (2)
502 #define SWM_H_SLICE (32)
503 #define SWM_V_SLICE (32)
505 /* define work spaces */
507 int idx; /* workspace index */
508 char *name; /* workspace name */
509 int always_raise; /* raise windows on focus */
510 int bar_enabled; /* bar visibility */
511 struct layout *cur_layout; /* current layout handlers */
512 struct ws_win *focus; /* may be NULL */
513 struct ws_win *focus_prev; /* may be NULL */
514 struct ws_win *focus_pending; /* may be NULL */
515 struct swm_region *r; /* may be NULL */
516 struct swm_region *old_r; /* may be NULL */
517 struct ws_win_list winlist; /* list of windows in ws */
518 struct ws_win_list unmanagedlist; /* list of dead windows in ws */
519 char stacker[10]; /* display stacker and layout */
523 int horizontal_msize;
525 int horizontal_stacks;
536 SWM_S_COLOR_BAR_BORDER,
537 SWM_S_COLOR_BAR_BORDER_UNFOCUS,
538 SWM_S_COLOR_BAR_FONT,
544 /* physical screen mapping */
545 #define SWM_WS_MAX (22) /* hard limit */
546 int workspace_limit = 10; /* soft limit */
549 int idx; /* screen index */
550 struct swm_region_list rl; /* list of regions on this screen */
551 struct swm_region_list orl; /* list of old regions */
553 struct workspace ws[SWM_WS_MAX];
554 struct swm_region *r_focus;
560 } c[SWM_S_COLOR_MAX];
562 xcb_gcontext_t bar_gc;
565 struct swm_screen *screens;
567 /* args to functions */
570 #define SWM_ARG_ID_FOCUSNEXT (0)
571 #define SWM_ARG_ID_FOCUSPREV (1)
572 #define SWM_ARG_ID_FOCUSMAIN (2)
573 #define SWM_ARG_ID_FOCUSURGENT (3)
574 #define SWM_ARG_ID_SWAPNEXT (10)
575 #define SWM_ARG_ID_SWAPPREV (11)
576 #define SWM_ARG_ID_SWAPMAIN (12)
577 #define SWM_ARG_ID_MOVELAST (13)
578 #define SWM_ARG_ID_MASTERSHRINK (20)
579 #define SWM_ARG_ID_MASTERGROW (21)
580 #define SWM_ARG_ID_MASTERADD (22)
581 #define SWM_ARG_ID_MASTERDEL (23)
582 #define SWM_ARG_ID_FLIPLAYOUT (24)
583 #define SWM_ARG_ID_STACKRESET (30)
584 #define SWM_ARG_ID_STACKINIT (31)
585 #define SWM_ARG_ID_CYCLEWS_UP (40)
586 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
587 #define SWM_ARG_ID_CYCLERG_UP (42)
588 #define SWM_ARG_ID_CYCLERG_DOWN (43)
589 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
590 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
591 #define SWM_ARG_ID_CYCLEWS_MOVE_UP (46)
592 #define SWM_ARG_ID_CYCLEWS_MOVE_DOWN (47)
593 #define SWM_ARG_ID_STACKINC (50)
594 #define SWM_ARG_ID_STACKDEC (51)
595 #define SWM_ARG_ID_SS_ALL (60)
596 #define SWM_ARG_ID_SS_WINDOW (61)
597 #define SWM_ARG_ID_DONTCENTER (70)
598 #define SWM_ARG_ID_CENTER (71)
599 #define SWM_ARG_ID_KILLWINDOW (80)
600 #define SWM_ARG_ID_DELETEWINDOW (81)
601 #define SWM_ARG_ID_WIDTHGROW (90)
602 #define SWM_ARG_ID_WIDTHSHRINK (91)
603 #define SWM_ARG_ID_HEIGHTGROW (92)
604 #define SWM_ARG_ID_HEIGHTSHRINK (93)
605 #define SWM_ARG_ID_MOVEUP (100)
606 #define SWM_ARG_ID_MOVEDOWN (101)
607 #define SWM_ARG_ID_MOVELEFT (102)
608 #define SWM_ARG_ID_MOVERIGHT (103)
609 #define SWM_ARG_ID_BAR_TOGGLE (110)
610 #define SWM_ARG_ID_BAR_TOGGLE_WS (111)
616 TAILQ_ENTRY(quirk) entry;
620 #define SWM_Q_FLOAT (1<<0) /* float this window */
621 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
622 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
623 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
624 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
625 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
626 #define SWM_Q_NOFOCUSONMAP (1<<6) /* Don't focus on window when mapped. */
627 #define SWM_Q_FOCUSONMAP_SINGLE (1<<7) /* Only focus if single win of type. */
629 TAILQ_HEAD(quirk_list, quirk);
630 struct quirk_list quirks = TAILQ_HEAD_INITIALIZER(quirks);
633 * Supported EWMH hints should be added to
634 * both the enum and the ewmh array
639 _NET_MOVERESIZE_WINDOW,
640 _NET_WM_ACTION_ABOVE,
641 _NET_WM_ACTION_CLOSE,
642 _NET_WM_ACTION_FULLSCREEN,
644 _NET_WM_ACTION_RESIZE,
645 _NET_WM_ALLOWED_ACTIONS,
649 _NET_WM_STATE_FULLSCREEN,
650 _NET_WM_STATE_HIDDEN,
651 _NET_WM_STATE_MAXIMIZED_HORZ,
652 _NET_WM_STATE_MAXIMIZED_VERT,
653 _NET_WM_STATE_SKIP_PAGER,
654 _NET_WM_STATE_SKIP_TASKBAR,
656 _NET_WM_WINDOW_TYPE_DIALOG,
657 _NET_WM_WINDOW_TYPE_DOCK,
658 _NET_WM_WINDOW_TYPE_NORMAL,
659 _NET_WM_WINDOW_TYPE_SPLASH,
660 _NET_WM_WINDOW_TYPE_TOOLBAR,
661 _NET_WM_WINDOW_TYPE_UTILITY,
662 _SWM_WM_STATE_MANUAL,
669 } ewmh[SWM_EWMH_HINT_MAX] = {
670 /* must be in same order as in the enum */
671 {"_NET_ACTIVE_WINDOW", XCB_ATOM_NONE},
672 {"_NET_CLOSE_WINDOW", XCB_ATOM_NONE},
673 {"_NET_MOVERESIZE_WINDOW", XCB_ATOM_NONE},
674 {"_NET_WM_ACTION_ABOVE", XCB_ATOM_NONE},
675 {"_NET_WM_ACTION_CLOSE", XCB_ATOM_NONE},
676 {"_NET_WM_ACTION_FULLSCREEN", XCB_ATOM_NONE},
677 {"_NET_WM_ACTION_MOVE", XCB_ATOM_NONE},
678 {"_NET_WM_ACTION_RESIZE", XCB_ATOM_NONE},
679 {"_NET_WM_ALLOWED_ACTIONS", XCB_ATOM_NONE},
680 {"_NET_WM_NAME", XCB_ATOM_NONE},
681 {"_NET_WM_STATE", XCB_ATOM_NONE},
682 {"_NET_WM_STATE_ABOVE", XCB_ATOM_NONE},
683 {"_NET_WM_STATE_FULLSCREEN", XCB_ATOM_NONE},
684 {"_NET_WM_STATE_HIDDEN", XCB_ATOM_NONE},
685 {"_NET_WM_STATE_MAXIMIZED_HORZ", XCB_ATOM_NONE},
686 {"_NET_WM_STATE_MAXIMIZED_VERT", XCB_ATOM_NONE},
687 {"_NET_WM_STATE_SKIP_PAGER", XCB_ATOM_NONE},
688 {"_NET_WM_STATE_SKIP_TASKBAR", XCB_ATOM_NONE},
689 {"_NET_WM_WINDOW_TYPE", XCB_ATOM_NONE},
690 {"_NET_WM_WINDOW_TYPE_DIALOG", XCB_ATOM_NONE},
691 {"_NET_WM_WINDOW_TYPE_DOCK", XCB_ATOM_NONE},
692 {"_NET_WM_WINDOW_TYPE_NORMAL", XCB_ATOM_NONE},
693 {"_NET_WM_WINDOW_TYPE_SPLASH", XCB_ATOM_NONE},
694 {"_NET_WM_WINDOW_TYPE_TOOLBAR", XCB_ATOM_NONE},
695 {"_NET_WM_WINDOW_TYPE_UTILITY", XCB_ATOM_NONE},
696 {"_SWM_WM_STATE_MANUAL", XCB_ATOM_NONE},
703 XC_BOTTOM_LEFT_CORNER,
704 XC_BOTTOM_RIGHT_CORNER,
712 char *name; /* Name used by Xcursor .*/
713 uint8_t cf_char; /* cursorfont index. */
715 } cursors[XC_MAX] = {
716 {"fleur", XC_fleur, XCB_CURSOR_NONE},
717 {"left_ptr", XC_left_ptr, XCB_CURSOR_NONE},
718 {"bottom_left_corner", XC_bottom_left_corner, XCB_CURSOR_NONE},
719 {"bottom_right_corner", XC_bottom_right_corner, XCB_CURSOR_NONE},
720 {"sizing", XC_sizing, XCB_CURSOR_NONE},
721 {"top_left_corner", XC_top_left_corner, XCB_CURSOR_NONE},
722 {"top_right_corner", XC_top_right_corner, XCB_CURSOR_NONE},
725 #define SWM_SPAWN_OPTIONAL 0x1
729 TAILQ_ENTRY(spawn_prog) entry;
735 TAILQ_HEAD(spawn_list, spawn_prog);
736 struct spawn_list spawns = TAILQ_HEAD_INITIALIZER(spawns);
738 /* user/key callable function IDs */
853 KF_DUMPWINS, /* MUST BE LAST */
861 enum keyfuncid funcid;
864 RB_HEAD(key_tree, key);
866 /* function prototypes */
867 void adjust_font(struct ws_win *);
868 void bar_class_name(char *, size_t, struct swm_region *);
869 void bar_class_title_name(char *, size_t, struct swm_region *);
870 void bar_cleanup(struct swm_region *);
871 void bar_extra_setup(void);
872 void bar_extra_stop(void);
873 int bar_extra_update(void);
874 void bar_fmt(const char *, char *, struct swm_region *, size_t);
875 void bar_fmt_expand(char *, size_t);
877 void bar_print(struct swm_region *, const char *);
878 void bar_print_legacy(struct swm_region *, const char *);
879 void bar_replace(char *, char *, struct swm_region *, size_t);
880 void bar_replace_pad(char *, int *, size_t);
881 char *bar_replace_seq(char *, char *, struct swm_region *, size_t *, size_t);
882 void bar_setup(struct swm_region *);
883 void bar_title_name(char *, size_t, struct swm_region *);
884 void bar_toggle(struct swm_region *, union arg *);
885 void bar_urgent(char *, size_t);
886 void bar_window_float(char *, size_t, struct swm_region *);
887 void bar_window_name(char *, size_t, struct swm_region *);
888 void bar_workspace_name(char *, size_t, struct swm_region *);
889 void buttonpress(xcb_button_press_event_t *);
890 void check_conn(void);
891 void clear_keys(void);
892 void clientmessage(xcb_client_message_event_t *);
893 void client_msg(struct ws_win *, xcb_atom_t, xcb_timestamp_t);
894 int conf_load(const char *, int);
895 void configurenotify(xcb_configure_notify_event_t *);
896 void configurerequest(xcb_configure_request_event_t *);
897 void config_win(struct ws_win *, xcb_configure_request_event_t *);
898 void constrain_window(struct ws_win *, struct swm_geometry *, int *);
899 int count_win(struct workspace *, int);
900 void cursors_cleanup(void);
901 void cursors_load(void);
902 void custom_region(char *);
903 void cyclerg(struct swm_region *, union arg *);
904 void cyclews(struct swm_region *, union arg *);
905 void cycle_layout(struct swm_region *, union arg *);
906 void destroynotify(xcb_destroy_notify_event_t *);
907 void dumpwins(struct swm_region *, union arg *);
909 void enternotify(xcb_enter_notify_event_t *);
910 void event_drain(uint8_t);
911 void event_error(xcb_generic_error_t *);
912 void event_handle(xcb_generic_event_t *);
913 void ewmh_autoquirk(struct ws_win *);
914 void ewmh_get_win_state(struct ws_win *);
915 int ewmh_set_win_fullscreen(struct ws_win *, int);
916 void ewmh_update_actions(struct ws_win *);
917 void ewmh_update_win_state(struct ws_win *, xcb_atom_t, long);
918 char *expand_tilde(const char *);
919 void expose(xcb_expose_event_t *);
920 void fake_keypress(struct ws_win *, xcb_keysym_t, uint16_t);
921 struct pid_e *find_pid(pid_t);
922 struct ws_win *find_unmanaged_window(xcb_window_t);
923 struct ws_win *find_window(xcb_window_t);
924 void floating_toggle(struct swm_region *, union arg *);
925 int floating_toggle_win(struct ws_win *);
926 void focus(struct swm_region *, union arg *);
928 void focusin(xcb_focus_in_event_t *);
929 void focusout(xcb_focus_out_event_t *);
931 void focus_flush(void);
932 void focus_region(struct swm_region *);
933 void focusrg(struct swm_region *, union arg *);
934 void focus_win(struct ws_win *);
935 void fontset_init(void);
936 void free_window(struct ws_win *);
937 xcb_atom_t get_atom_from_string(const char *);
939 char *get_atom_name(xcb_atom_t);
941 struct ws_win *get_focus_magic(struct ws_win *);
942 struct ws_win *get_focus_prev(struct ws_win *);
944 char *get_notify_detail_label(uint8_t);
945 char *get_notify_mode_label(uint8_t);
947 struct ws_win *get_pointer_win(xcb_window_t);
948 struct ws_win *get_region_focus(struct swm_region *);
949 int get_region_index(struct swm_region *);
950 xcb_screen_t *get_screen(int);
951 xcb_window_t get_sibling(struct ws_win *, int);
952 int get_screen_count(void);
954 char *get_stack_mode_name(uint8_t);
956 int32_t get_swm_iconic(struct ws_win *);
957 char *get_win_name(xcb_window_t);
958 int get_ws_idx(xcb_window_t);
959 uint32_t getstate(xcb_window_t);
960 void grabbuttons(struct ws_win *);
962 void grab_windows(void);
963 void iconify(struct swm_region *, union arg *);
965 void keypress(xcb_key_press_event_t *);
966 int key_cmp(struct key *, struct key *);
967 void key_insert(unsigned int, KeySym, enum keyfuncid, const char *);
968 struct key *key_lookup(unsigned int, KeySym);
969 void key_remove(struct key *);
970 void key_replace(struct key *, unsigned int, KeySym, enum keyfuncid,
972 void kill_bar_extra_atexit(void);
973 void kill_refs(struct ws_win *);
975 void leavenotify(xcb_leave_notify_event_t *);
977 void load_float_geom(struct ws_win *);
978 struct ws_win *manage_window(xcb_window_t, uint16_t);
979 void map_window(struct ws_win *, xcb_window_t);
980 void mapnotify(xcb_map_notify_event_t *);
981 void mappingnotify(xcb_mapping_notify_event_t *);
982 void maprequest(xcb_map_request_event_t *);
983 void motionnotify(xcb_motion_notify_event_t *);
984 void move(struct ws_win *, union arg *);
985 void move_step(struct swm_region *, union arg *);
986 uint32_t name_to_pixel(int, const char *);
987 void name_workspace(struct swm_region *, union arg *);
988 void new_region(struct swm_screen *, int, int, int, int);
989 int parsekeys(char *, unsigned int, unsigned int *, KeySym *);
990 int parsequirks(char *, unsigned long *);
991 int parse_rgb(const char *, uint16_t *, uint16_t *, uint16_t *);
992 void pressbutton(struct swm_region *, union arg *);
993 void priorws(struct swm_region *, union arg *);
995 void print_win_geom(xcb_window_t);
997 void propertynotify(xcb_property_notify_event_t *);
998 void quirk_insert(const char *, const char *, unsigned long);
999 void quirk_remove(struct quirk *);
1000 void quirk_replace(struct quirk *, const char *, const char *,
1002 void quit(struct swm_region *, union arg *);
1003 void raise_toggle(struct swm_region *, union arg *);
1004 void region_containment(struct ws_win *, struct swm_region *, int);
1005 struct swm_region *region_under(struct swm_screen *, int, int);
1006 void regionize(struct ws_win *, int, int);
1007 void resize(struct ws_win *, union arg *);
1008 void resize_step(struct swm_region *, union arg *);
1009 void restart(struct swm_region *, union arg *);
1010 struct swm_region *root_to_region(xcb_window_t, int);
1011 void screenchange(xcb_randr_screen_change_notify_event_t *);
1012 void scan_xrandr(int);
1013 void search_do_resp(void);
1014 void search_resp_name_workspace(const char *, size_t);
1015 void search_resp_search_window(const char *);
1016 void search_resp_search_workspace(const char *);
1017 void search_resp_uniconify(const char *, size_t);
1018 void search_win(struct swm_region *, union arg *);
1019 void search_win_cleanup(void);
1020 void search_workspace(struct swm_region *, union arg *);
1021 void send_to_rg(struct swm_region *, union arg *);
1022 void send_to_ws(struct swm_region *, union arg *);
1023 void set_region(struct swm_region *);
1024 int setautorun(char *, char *, int);
1025 int setconfbinding(char *, char *, int);
1026 int setconfcolor(char *, char *, int);
1027 int setconfmodkey(char *, char *, int);
1028 int setconfquirk(char *, char *, int);
1029 int setconfregion(char *, char *, int);
1030 int setconfspawn(char *, char *, int);
1031 int setconfvalue(char *, char *, int);
1032 void setkeybinding(unsigned int, KeySym, enum keyfuncid, const char *);
1033 int setkeymapping(char *, char *, int);
1034 int setlayout(char *, char *, int);
1035 void setquirk(const char *, const char *, unsigned long);
1036 void setscreencolor(char *, int, int);
1037 void setspawn(const char *, const char *, int);
1038 void setup_ewmh(void);
1039 void setup_globals(void);
1040 void setup_keys(void);
1041 void setup_quirks(void);
1042 void setup_screens(void);
1043 void setup_spawn(void);
1044 void set_child_transient(struct ws_win *, xcb_window_t *);
1045 void set_swm_iconic(struct ws_win *, int);
1046 void set_win_state(struct ws_win *, uint16_t);
1047 void shutdown_cleanup(void);
1049 void socket_setnonblock(int);
1050 void sort_windows(struct ws_win_list *);
1051 void spawn(int, union arg *, int);
1052 void spawn_custom(struct swm_region *, union arg *, const char *);
1053 int spawn_expand(struct swm_region *, union arg *, const char *, char ***);
1054 void spawn_insert(const char *, const char *, int);
1055 struct spawn_prog *spawn_find(const char *);
1056 void spawn_remove(struct spawn_prog *);
1057 void spawn_replace(struct spawn_prog *, const char *, const char *, int);
1058 void spawn_select(struct swm_region *, union arg *, const char *, int *);
1059 void stack_config(struct swm_region *, union arg *);
1060 void stack_floater(struct ws_win *, struct swm_region *);
1061 void stack_master(struct workspace *, struct swm_geometry *, int, int);
1062 void store_float_geom(struct ws_win *);
1063 char *strdupsafe(const char *);
1064 void swapwin(struct swm_region *, union arg *);
1065 void switchws(struct swm_region *, union arg *);
1066 void teardown_ewmh(void);
1067 void unfocus_win(struct ws_win *);
1068 void uniconify(struct swm_region *, union arg *);
1069 void unmanage_window(struct ws_win *);
1070 void unmapnotify(xcb_unmap_notify_event_t *);
1071 void unmap_all(void);
1072 void unmap_window(struct ws_win *);
1073 void updatenumlockmask(void);
1074 void update_modkey(unsigned int);
1075 void update_window(struct ws_win *);
1076 void validate_spawns(void);
1077 int validate_win(struct ws_win *);
1078 int validate_ws(struct workspace *);
1079 /*void visibilitynotify(xcb_visibility_notify_event_t *);*/
1080 void version(struct swm_region *, union arg *);
1081 void win_to_ws(struct ws_win *, int, int);
1082 pid_t window_get_pid(xcb_window_t);
1083 void wkill(struct swm_region *, union arg *);
1084 void workaround(void);
1085 void xft_init(struct swm_region *);
1086 void _add_startup_exception(const char *, va_list);
1087 void add_startup_exception(const char *, ...);
1089 RB_PROTOTYPE(key_tree, key, entry, key_cmp);
1090 RB_GENERATE(key_tree, key, entry, key_cmp);
1091 struct key_tree keys;
1096 xcb_font_t cf = XCB_NONE;
1099 for (i = 0; i < LENGTH(cursors); ++i) {
1100 /* try to load Xcursor first. */
1101 cursors[i].cid = XcursorLibraryLoadCursor(display,
1104 /* fallback to cursorfont. */
1105 if (cursors[i].cid == XCB_CURSOR_NONE) {
1106 if (cf == XCB_NONE) {
1107 cf = xcb_generate_id(conn);
1108 xcb_open_font(conn, cf, strlen("cursor"),
1112 cursors[i].cid = xcb_generate_id(conn);
1113 xcb_create_glyph_cursor(conn, cursors[i].cid, cf, cf,
1114 cursors[i].cf_char, cursors[i].cf_char + 1, 0, 0, 0,
1115 0xffff, 0xffff, 0xffff);
1121 xcb_close_font(conn, cf);
1125 cursors_cleanup(void)
1128 for (i = 0; i < LENGTH(cursors); ++i)
1129 xcb_free_cursor(conn, cursors[i].cid);
1133 expand_tilde(const char *s)
1135 struct passwd *ppwd;
1143 errx(1, "expand_tilde: NULL string.");
1146 result = strdup(sc);
1152 if ((max = sysconf(_SC_LOGIN_NAME_MAX)) == -1)
1153 errx(1, "expand_tilde: sysconf");
1155 if ((user = calloc(1, max + 1)) == NULL)
1156 errx(1, "expand_tilde: calloc");
1158 for (i = 0; s[i] != '/' && s[i] != '\0'; ++i)
1163 ppwd = strlen(user) == 0 ? getpwuid(getuid()) : getpwnam(user);
1167 result = strdup(sc);
1169 if (asprintf(&result, "%s%s", ppwd->pw_dir, s) == -1)
1173 errx(1, "expand_tilde: failed to allocate memory.");
1179 parse_rgb(const char *rgb, uint16_t *rr, uint16_t *gg, uint16_t *bb)
1181 unsigned int tmpr, tmpg, tmpb;
1183 if (sscanf(rgb, "rgb:%x/%x/%x", &tmpr, &tmpg, &tmpb) != 3)
1186 *rr = RGB_8_TO_16(tmpr);
1187 *gg = RGB_8_TO_16(tmpg);
1188 *bb = RGB_8_TO_16(tmpb);
1194 get_screen(int screen)
1196 const xcb_setup_t *r;
1197 xcb_screen_iterator_t iter;
1199 if ((r = xcb_get_setup(conn)) == NULL) {
1200 DNPRINTF(SWM_D_MISC, "get_screen: xcb_get_setup\n");
1204 iter = xcb_setup_roots_iterator(r);
1205 for (; iter.rem; --screen, xcb_screen_next(&iter))
1213 get_screen_count(void)
1215 const xcb_setup_t *r;
1217 if ((r = xcb_get_setup(conn)) == NULL) {
1218 DNPRINTF(SWM_D_MISC, "get_screen_count: xcb_get_setup\n");
1222 return xcb_setup_roots_length(r);
1226 get_region_index(struct swm_region *r)
1228 struct swm_region *rr;
1234 TAILQ_FOREACH(rr, &r->s->rl, entry) {
1249 if (focus_mode == SWM_FOCUS_DEFAULT)
1250 event_drain(XCB_ENTER_NOTIFY);
1256 get_atom_from_string(const char *str)
1258 xcb_intern_atom_cookie_t c;
1259 xcb_intern_atom_reply_t *r;
1262 c = xcb_intern_atom(conn, 0, strlen(str), str);
1263 r = xcb_intern_atom_reply(conn, c, NULL);
1271 return (XCB_ATOM_NONE);
1275 set_swm_iconic(struct ws_win *win, int newv)
1282 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
1283 a_swm_iconic, XCB_ATOM_INTEGER, 32, 1, &v);
1285 xcb_delete_property(conn, win->id, a_swm_iconic);
1289 get_swm_iconic(struct ws_win *win)
1292 xcb_get_property_reply_t *pr = NULL;
1294 pr = xcb_get_property_reply(conn,
1295 xcb_get_property(conn, 0, win->id, a_swm_iconic,
1296 XCB_ATOM_INTEGER, 0, 1), NULL);
1299 if (pr->type != XCB_ATOM_INTEGER || pr->format != 32)
1301 v = *((int32_t *)xcb_get_property_value(pr));
1311 xcb_atom_t sup_list;
1312 int i, j, num_screens;
1314 sup_list = get_atom_from_string("_NET_SUPPORTED");
1316 for (i = 0; i < LENGTH(ewmh); i++)
1317 ewmh[i].atom = get_atom_from_string(ewmh[i].name);
1319 num_screens = get_screen_count();
1320 for (i = 0; i < num_screens; i++) {
1321 /* Support check window will be created by workaround(). */
1323 /* Report supported atoms */
1324 xcb_delete_property(conn, screens[i].root, sup_list);
1325 for (j = 0; j < LENGTH(ewmh); j++)
1326 xcb_change_property(conn, XCB_PROP_MODE_APPEND,
1327 screens[i].root, sup_list, XCB_ATOM_ATOM, 32, 1,
1336 xcb_atom_t sup_check, sup_list;
1338 xcb_get_property_cookie_t pc;
1339 xcb_get_property_reply_t *pr;
1341 sup_check = get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
1342 sup_list = get_atom_from_string("_NET_SUPPORTED");
1343 num_screens = get_screen_count();
1345 for (i = 0; i < num_screens; i++) {
1346 /* Get the support check window and destroy it */
1347 pc = xcb_get_property(conn, 0, screens[i].root, sup_check,
1348 XCB_ATOM_WINDOW, 0, 1);
1349 pr = xcb_get_property_reply(conn, pc, NULL);
1352 if (pr->format == sup_check) {
1353 id = *((xcb_window_t *)xcb_get_property_value(pr));
1355 xcb_destroy_window(conn, id);
1356 xcb_delete_property(conn, screens[i].root, sup_check);
1357 xcb_delete_property(conn, screens[i].root, sup_list);
1364 ewmh_autoquirk(struct ws_win *win)
1368 xcb_get_property_cookie_t c;
1369 xcb_get_property_reply_t *r;
1371 c = xcb_get_property(conn, 0, win->id,
1372 ewmh[_NET_WM_WINDOW_TYPE].atom, XCB_ATOM_ATOM, 0, UINT32_MAX);
1373 r = xcb_get_property_reply(conn, c, NULL);
1377 n = xcb_get_property_value_length(r);
1378 type = xcb_get_property_value(r);
1380 for (i = 0; i < n; i++) {
1381 if (type[i] == ewmh[_NET_WM_WINDOW_TYPE_NORMAL].atom)
1383 if (type[i] == ewmh[_NET_WM_WINDOW_TYPE_DOCK].atom ||
1384 type[i] == ewmh[_NET_WM_WINDOW_TYPE_TOOLBAR].atom ||
1385 type[i] == ewmh[_NET_WM_WINDOW_TYPE_UTILITY].atom) {
1387 win->quirks = SWM_Q_FLOAT | SWM_Q_ANYWHERE;
1390 if (type[i] == ewmh[_NET_WM_WINDOW_TYPE_SPLASH].atom ||
1391 type[i] == ewmh[_NET_WM_WINDOW_TYPE_DIALOG].atom) {
1393 win->quirks = SWM_Q_FLOAT;
1400 #define SWM_EWMH_ACTION_COUNT_MAX (6)
1401 #define EWMH_F_FULLSCREEN (1<<0)
1402 #define EWMH_F_ABOVE (1<<1)
1403 #define EWMH_F_HIDDEN (1<<2)
1404 #define EWMH_F_SKIP_PAGER (1<<3)
1405 #define EWMH_F_SKIP_TASKBAR (1<<4)
1406 #define SWM_F_MANUAL (1<<5)
1409 ewmh_set_win_fullscreen(struct ws_win *win, int fs)
1417 DNPRINTF(SWM_D_MISC, "ewmh_set_win_fullscreen: window: 0x%x, "
1418 "fullscreen %s\n", win->id, YESNO(fs));
1421 if (!win->g_floatvalid)
1422 store_float_geom(win);
1424 win->g = win->ws->r->g;
1427 load_float_geom(win);
1434 ewmh_update_actions(struct ws_win *win)
1436 xcb_atom_t actions[SWM_EWMH_ACTION_COUNT_MAX];
1442 actions[n++] = ewmh[_NET_WM_ACTION_CLOSE].atom;
1444 if (win->floating) {
1445 actions[n++] = ewmh[_NET_WM_ACTION_MOVE].atom;
1446 actions[n++] = ewmh[_NET_WM_ACTION_RESIZE].atom;
1447 actions[n++] = ewmh[_NET_WM_ACTION_ABOVE].atom;
1450 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
1451 ewmh[_NET_WM_ALLOWED_ACTIONS].atom, XCB_ATOM_ATOM, 32, 1, actions);
1454 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
1455 #define _NET_WM_STATE_ADD 1 /* add/set property */
1456 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
1459 ewmh_update_win_state(struct ws_win *win, xcb_atom_t state, long action)
1461 unsigned int mask = 0;
1462 unsigned int changed = 0;
1463 unsigned int orig_flags;
1468 DNPRINTF(SWM_D_PROP, "ewmh_update_win_state: window: 0x%x, state: %ld, "
1469 "action: %ld\n", win->id, (unsigned long)state, action);
1471 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
1472 mask = EWMH_F_FULLSCREEN;
1473 else if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
1474 mask = EWMH_F_ABOVE;
1475 else if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
1476 mask = SWM_F_MANUAL;
1477 else if (state == ewmh[_NET_WM_STATE_SKIP_PAGER].atom)
1478 mask = EWMH_F_SKIP_PAGER;
1479 else if (state == ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom)
1480 mask = EWMH_F_SKIP_TASKBAR;
1482 orig_flags = win->ewmh_flags;
1485 case _NET_WM_STATE_REMOVE:
1486 win->ewmh_flags &= ~mask;
1488 case _NET_WM_STATE_ADD:
1489 win->ewmh_flags |= mask;
1491 case _NET_WM_STATE_TOGGLE:
1492 win->ewmh_flags ^= mask;
1496 changed = (win->ewmh_flags & mask) ^ (orig_flags & mask) ? 1 : 0;
1498 if (state == ewmh[_NET_WM_STATE_ABOVE].atom) {
1499 if (changed && !floating_toggle_win(win))
1500 win->ewmh_flags = orig_flags; /* revert */
1501 } else if (state == ewmh[_SWM_WM_STATE_MANUAL].atom) {
1503 win->manual = (win->ewmh_flags & SWM_F_MANUAL) != 0;
1504 } else if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom) {
1505 if (changed && !ewmh_set_win_fullscreen(win,
1506 win->ewmh_flags & EWMH_F_FULLSCREEN))
1507 win->ewmh_flags = orig_flags; /* revert */
1510 xcb_delete_property(conn, win->id, ewmh[_NET_WM_STATE].atom);
1512 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
1513 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1514 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1515 &ewmh[_NET_WM_STATE_FULLSCREEN].atom);
1516 else if (win->ewmh_flags & EWMH_F_SKIP_PAGER)
1517 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1518 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1519 &ewmh[_NET_WM_STATE_SKIP_PAGER].atom);
1520 else if (win->ewmh_flags & EWMH_F_SKIP_TASKBAR)
1521 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1522 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1523 &ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom);
1524 else if (win->ewmh_flags & EWMH_F_ABOVE)
1525 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1526 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1527 &ewmh[_NET_WM_STATE_ABOVE].atom);
1528 else if (win->ewmh_flags & SWM_F_MANUAL)
1529 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1530 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1531 &ewmh[_SWM_WM_STATE_MANUAL].atom);
1535 ewmh_get_win_state(struct ws_win *win)
1538 xcb_get_property_cookie_t c;
1539 xcb_get_property_reply_t *r;
1545 win->ewmh_flags = 0;
1547 win->ewmh_flags |= EWMH_F_ABOVE;
1549 win->ewmh_flags |= SWM_F_MANUAL;
1551 c = xcb_get_property(conn, 0, win->id, ewmh[_NET_WM_STATE].atom,
1552 XCB_ATOM_ATOM, 0, UINT32_MAX);
1553 r = xcb_get_property_reply(conn, c, NULL);
1557 states = xcb_get_property_value(r);
1558 n = xcb_get_property_value_length(r);
1560 for (i = 0; i < n; i++)
1561 ewmh_update_win_state(win, states[i], _NET_WM_STATE_ADD);
1569 dumpwins(struct swm_region *r, union arg *args)
1573 xcb_get_window_attributes_cookie_t c;
1574 xcb_get_window_attributes_reply_t *wa;
1576 /* suppress unused warning since var is needed */
1579 if (r->ws == NULL) {
1580 warnx("dumpwins: invalid workspace");
1584 warnx("=== managed window list ws %02d ===", r->ws->idx);
1585 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
1586 state = getstate(win->id);
1587 c = xcb_get_window_attributes(conn, win->id);
1588 wa = xcb_get_window_attributes_reply(conn, c, NULL);
1590 warnx("window: 0x%x, map_state: %d, state: %u, "
1591 "transient: 0x%x", win->id, wa->map_state,
1592 state, win->transient);
1595 warnx("window: 0x%x, failed xcb_get_window_attributes",
1599 warnx("===== unmanaged window list =====");
1600 TAILQ_FOREACH(win, &r->ws->unmanagedlist, entry) {
1601 state = getstate(win->id);
1602 c = xcb_get_window_attributes(conn, win->id);
1603 wa = xcb_get_window_attributes_reply(conn, c, NULL);
1605 warnx("window: 0x%x, map_state: %d, state: %u, "
1606 "transient: 0x%x", win->id, wa->map_state,
1607 state, win->transient);
1610 warnx("window: 0x%x, failed xcb_get_window_attributes",
1614 warnx("=================================");
1618 dumpwins(struct swm_region *r, union arg *s)
1623 #endif /* SWM_DEBUG */
1628 int saved_errno, status;
1631 saved_errno = errno;
1635 while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) != 0) {
1640 if (errno != ECHILD)
1641 warn("sighdlr: waitpid");
1642 #endif /* SWM_DEBUG */
1645 if (pid == searchpid)
1649 if (WIFEXITED(status)) {
1650 if (WEXITSTATUS(status) != 0)
1651 warnx("sighdlr: child exit status: %d",
1652 WEXITSTATUS(status));
1654 warnx("sighdlr: child is terminated "
1656 #endif /* SWM_DEBUG */
1670 errno = saved_errno;
1676 struct pid_e *p = NULL;
1678 DNPRINTF(SWM_D_MISC, "find_pid: %d\n", pid);
1683 TAILQ_FOREACH(p, &pidlist, entry) {
1692 name_to_pixel(int sidx, const char *colorname)
1694 uint32_t result = 0;
1695 char cname[32] = "#";
1696 xcb_screen_t *screen;
1697 xcb_colormap_t cmap;
1698 xcb_alloc_color_reply_t *cr;
1699 xcb_alloc_named_color_reply_t *nr;
1700 uint16_t rr, gg, bb;
1702 screen = get_screen(sidx);
1703 cmap = screen->default_colormap;
1705 /* color is in format rgb://rr/gg/bb */
1706 if (strncmp(colorname, "rgb:", 4) == 0) {
1707 if (parse_rgb(colorname, &rr, &gg, &bb) == -1)
1708 warnx("could not parse rgb %s", colorname);
1710 cr = xcb_alloc_color_reply(conn,
1711 xcb_alloc_color(conn, cmap, rr, gg, bb),
1717 warnx("color '%s' not found", colorname);
1720 nr = xcb_alloc_named_color_reply(conn,
1721 xcb_alloc_named_color(conn, cmap, strlen(colorname),
1724 strlcat(cname, colorname + 2, sizeof cname - 1);
1725 nr = xcb_alloc_named_color_reply(conn,
1726 xcb_alloc_named_color(conn, cmap, strlen(cname),
1733 warnx("color '%s' not found", colorname);
1740 setscreencolor(char *val, int i, int c)
1744 num_screens = get_screen_count();
1745 if (i > 0 && i <= num_screens) {
1746 screens[i - 1].c[c].pixel = name_to_pixel(i - 1, val);
1747 free(screens[i - 1].c[c].name);
1748 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
1750 } else if (i == -1) {
1751 for (i = 0; i < num_screens; i++) {
1752 screens[i].c[c].pixel = name_to_pixel(0, val);
1753 free(screens[i].c[c].name);
1754 if ((screens[i].c[c].name = strdup(val)) == NULL)
1758 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1763 fancy_stacker(struct workspace *ws)
1765 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1766 if (ws->cur_layout->l_stack == vertical_stack)
1767 snprintf(ws->stacker, sizeof ws->stacker,
1768 ws->l_state.vertical_flip ? "[%d>%d]" : "[%d|%d]",
1769 ws->l_state.vertical_mwin, ws->l_state.vertical_stacks);
1770 if (ws->cur_layout->l_stack == horizontal_stack)
1771 snprintf(ws->stacker, sizeof ws->stacker,
1772 ws->l_state.horizontal_flip ? "[%dv%d]" : "[%d-%d]",
1773 ws->l_state.horizontal_mwin, ws->l_state.horizontal_stacks);
1777 plain_stacker(struct workspace *ws)
1779 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1780 if (ws->cur_layout->l_stack == vertical_stack)
1781 strlcpy(ws->stacker, ws->l_state.vertical_flip ? "[>]" : "[|]",
1782 sizeof ws->stacker);
1783 if (ws->cur_layout->l_stack == horizontal_stack)
1784 strlcpy(ws->stacker, ws->l_state.horizontal_flip ? "[v]" : "[-]",
1785 sizeof ws->stacker);
1789 custom_region(char *val)
1791 unsigned int x, y, w, h;
1792 int sidx, num_screens;
1793 xcb_screen_t *screen;
1795 num_screens = get_screen_count();
1796 if (sscanf(val, "screen[%d]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
1797 errx(1, "invalid custom region, "
1798 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1799 if (sidx < 1 || sidx > num_screens)
1800 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1804 if ((screen = get_screen(sidx)) == NULL)
1805 errx(1, "ERROR: can't get screen %d.", sidx);
1808 errx(1, "region %ux%u+%u+%u too small", w, h, x, y);
1810 if (x > screen->width_in_pixels ||
1811 y > screen->height_in_pixels ||
1812 w + x > screen->width_in_pixels ||
1813 h + y > screen->height_in_pixels) {
1814 warnx("ignoring region %ux%u+%u+%u - not within screen "
1815 "boundaries (%ux%u)", w, h, x, y,
1816 screen->width_in_pixels, screen->height_in_pixels);
1820 new_region(&screens[sidx], x, y, w, h);
1824 socket_setnonblock(int fd)
1828 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
1829 err(1, "fcntl F_GETFL");
1830 flags |= O_NONBLOCK;
1831 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
1832 err(1, "fcntl F_SETFL");
1836 bar_print_legacy(struct swm_region *r, const char *s)
1838 xcb_rectangle_t rect;
1843 XRectangle ibox, lbox;
1847 XmbTextExtents(bar_fs, s, len, &ibox, &lbox);
1849 switch (bar_justify) {
1850 case SWM_BAR_JUSTIFY_LEFT:
1853 case SWM_BAR_JUSTIFY_CENTER:
1854 x = (WIDTH(r) - lbox.width) / 2;
1856 case SWM_BAR_JUSTIFY_RIGHT:
1857 x = WIDTH(r) - lbox.width - SWM_BAR_OFFSET;
1861 if (x < SWM_BAR_OFFSET)
1866 rect.width = WIDTH(r->bar);
1867 rect.height = HEIGHT(r->bar);
1869 /* clear back buffer */
1870 gcv[0] = r->s->c[SWM_S_COLOR_BAR].pixel;
1871 xcb_change_gc(conn, r->s->bar_gc, XCB_GC_FOREGROUND, gcv);
1872 xcb_poly_fill_rectangle(conn, r->bar->buffer, r->s->bar_gc, 1, &rect);
1874 /* draw back buffer */
1875 gcvd.graphics_exposures = 0;
1876 draw = XCreateGC(display, r->bar->buffer, GCGraphicsExposures, &gcvd);
1877 XSetForeground(display, draw, r->s->c[SWM_S_COLOR_BAR_FONT].pixel);
1878 DRAWSTRING(display, r->bar->buffer, bar_fs, draw,
1879 x, (bar_fs_extents->max_logical_extent.height - lbox.height) / 2 -
1881 XFreeGC(display, draw);
1884 xcb_copy_area(conn, r->bar->buffer, r->bar->id, r->s->bar_gc, 0, 0,
1885 0, 0, WIDTH(r->bar), HEIGHT(r->bar));
1889 bar_print(struct swm_region *r, const char *s)
1892 xcb_rectangle_t rect;
1900 XftTextExtentsUtf8(display, bar_font, (FcChar8 *)s, len, &info);
1902 switch (bar_justify) {
1903 case SWM_BAR_JUSTIFY_LEFT:
1906 case SWM_BAR_JUSTIFY_CENTER:
1907 x = (WIDTH(r) - info.width) / 2;
1909 case SWM_BAR_JUSTIFY_RIGHT:
1910 x = WIDTH(r) - info.width - SWM_BAR_OFFSET;
1914 if (x < SWM_BAR_OFFSET)
1919 rect.width = WIDTH(r->bar);
1920 rect.height = HEIGHT(r->bar);
1922 /* clear back buffer */
1923 gcv[0] = r->s->c[SWM_S_COLOR_BAR].pixel;
1924 xcb_change_gc(conn, r->s->bar_gc, XCB_GC_FOREGROUND, gcv);
1925 xcb_poly_fill_rectangle(conn, r->bar->buffer, r->s->bar_gc, 1, &rect);
1927 /* draw back buffer */
1928 draw = XftDrawCreate(display, r->bar->buffer,
1929 DefaultVisual(display, r->s->idx),
1930 DefaultColormap(display, r->s->idx));
1932 XftDrawStringUtf8(draw, &bar_font_color, bar_font, x,
1933 (HEIGHT(r->bar) + bar_font->height) / 2 - bar_font->descent,
1936 XftDrawDestroy(draw);
1939 xcb_copy_area(conn, r->bar->buffer, r->bar->id, r->s->bar_gc, 0, 0,
1940 0, 0, WIDTH(r->bar), HEIGHT(r->bar));
1944 bar_extra_stop(void)
1948 bzero(bar_pipe, sizeof bar_pipe);
1951 kill(bar_pid, SIGTERM);
1954 strlcpy(bar_ext, "", sizeof bar_ext);
1959 bar_class_name(char *s, size_t sz, struct swm_region *r)
1961 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1963 if (r->ws->focus->ch.class_name != NULL)
1964 strlcat(s, r->ws->focus->ch.class_name, sz);
1968 bar_title_name(char *s, size_t sz, struct swm_region *r)
1970 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1972 if (r->ws->focus->ch.instance_name != NULL)
1973 strlcat(s, r->ws->focus->ch.instance_name, sz);
1977 bar_class_title_name(char *s, size_t sz, struct swm_region *r)
1979 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1982 bar_class_name(s, sz, r);
1983 strlcat(s, ":", sz);
1984 bar_title_name(s, sz, r);
1988 bar_window_float(char *s, size_t sz, struct swm_region *r)
1990 if (r == NULL || r ->ws == NULL || r->ws->focus == NULL)
1992 if (r->ws->focus->floating)
1993 strlcat(s, "(f)", sz);
1997 bar_window_name(char *s, size_t sz, struct swm_region *r)
2001 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
2003 if ((title = get_win_name(r->ws->focus->id)) == NULL)
2006 strlcat(s, title, sz);
2011 bar_urgent(char *s, size_t sz)
2014 int i, j, num_screens;
2015 int urgent[SWM_WS_MAX];
2017 xcb_get_property_cookie_t c;
2018 xcb_icccm_wm_hints_t hints;
2020 for (i = 0; i < workspace_limit; i++)
2023 num_screens = get_screen_count();
2024 for (i = 0; i < num_screens; i++)
2025 for (j = 0; j < workspace_limit; j++)
2026 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry) {
2027 c = xcb_icccm_get_wm_hints(conn, win->id);
2028 if (xcb_icccm_get_wm_hints_reply(conn, c,
2031 if (hints.flags & XCB_ICCCM_WM_HINT_X_URGENCY)
2035 for (i = 0; i < workspace_limit; i++) {
2037 snprintf(b, sizeof b, "%d ", i + 1);
2039 snprintf(b, sizeof b, "- ");
2045 bar_workspace_name(char *s, size_t sz, struct swm_region *r)
2047 if (r == NULL || r->ws == NULL)
2049 if (r->ws->name != NULL)
2050 strlcat(s, r->ws->name, sz);
2053 /* build the default bar format according to the defined enabled options */
2055 bar_fmt(const char *fmtexp, char *fmtnew, struct swm_region *r, size_t sz)
2057 /* if format provided, just copy the buffers */
2058 if (bar_format != NULL) {
2059 strlcpy(fmtnew, fmtexp, sz);
2063 /* reset the output buffer */
2066 strlcat(fmtnew, "+N:+I ", sz);
2068 strlcat(fmtnew, "+S", sz);
2069 strlcat(fmtnew, " ", sz);
2071 /* only show the workspace name if there's actually one */
2072 if (r != NULL && r->ws != NULL && r->ws->name != NULL)
2073 strlcat(fmtnew, "<+D>", sz);
2074 strlcat(fmtnew, "+3<", sz);
2076 if (clock_enabled) {
2077 strlcat(fmtnew, fmtexp, sz);
2078 strlcat(fmtnew, "+4<", sz);
2081 /* bar_urgent already adds the space before the last asterisk */
2083 strlcat(fmtnew, "* +U*+4<", sz);
2085 if (title_class_enabled) {
2086 strlcat(fmtnew, "+C", sz);
2087 if (!title_name_enabled)
2088 strlcat(fmtnew, "+4<", sz);
2091 /* checks needed by the colon and floating strlcat(3) calls below */
2092 if (r != NULL && r->ws != NULL && r->ws->focus != NULL) {
2093 if (title_name_enabled) {
2094 if (title_class_enabled)
2095 strlcat(fmtnew, ":", sz);
2096 strlcat(fmtnew, "+T+4<", sz);
2098 if (window_name_enabled) {
2099 if (r->ws->focus->floating)
2100 strlcat(fmtnew, "+F ", sz);
2101 strlcat(fmtnew, "+64W ", sz);
2105 /* finally add the action script output and the version */
2106 strlcat(fmtnew, "+4<+A+4<+V", sz);
2110 bar_replace_pad(char *tmp, int *limit, size_t sz)
2112 /* special case; no limit given, pad one space, instead */
2113 if (*limit == (int)sz - 1)
2115 snprintf(tmp, sz, "%*s", *limit, " ");
2118 /* replaces the bar format character sequences (like in tmux(1)) */
2120 bar_replace_seq(char *fmt, char *fmtrep, struct swm_region *r, size_t *offrep,
2124 char tmp[SWM_BAR_MAX];
2128 /* reset strlcat(3) buffer */
2131 /* get number, if any */
2134 if (sscanf(fmt, "%d%n", &limit, &size) != 1)
2135 limit = sizeof tmp - 1;
2136 if (limit <= 0 || limit >= (int)sizeof tmp)
2137 limit = sizeof tmp - 1;
2139 /* there is nothing to replace (ie EOL) */
2146 bar_replace_pad(tmp, &limit, sizeof tmp);
2149 snprintf(tmp, sizeof tmp, "%s", bar_ext);
2152 bar_class_name(tmp, sizeof tmp, r);
2155 bar_workspace_name(tmp, sizeof tmp, r);
2158 bar_window_float(tmp, sizeof tmp, r);
2161 snprintf(tmp, sizeof tmp, "%d", r->ws->idx + 1);
2164 snprintf(tmp, sizeof tmp, "%d", r->s->idx + 1);
2167 bar_class_title_name(tmp, sizeof tmp, r);
2170 snprintf(tmp, sizeof tmp, "%s", r->ws->stacker);
2173 bar_title_name(tmp, sizeof tmp, r);
2176 bar_urgent(tmp, sizeof tmp);
2179 snprintf(tmp, sizeof tmp, "%s", bar_vertext);
2182 bar_window_name(tmp, sizeof tmp, r);
2185 /* unknown character sequence; copy as-is */
2186 snprintf(tmp, sizeof tmp, "+%c", *fmt);
2192 if ((int)len < limit)
2194 while (limit-- > 0) {
2195 if (*offrep >= sz - 1)
2197 fmtrep[(*offrep)++] = *ptr++;
2205 bar_replace(char *fmt, char *fmtrep, struct swm_region *r, size_t sz)
2210 while (*fmt != '\0') {
2212 /* skip ordinary characters */
2215 fmtrep[off++] = *fmt++;
2219 /* character sequence found; replace it */
2220 fmt = bar_replace_seq(fmt, fmtrep, r, &off, sz);
2229 bar_fmt_expand(char *fmtexp, size_t sz)
2236 /* start by grabbing the current time and date */
2238 localtime_r(&tmt, &tm);
2240 /* figure out what to expand */
2241 if (bar_format != NULL)
2243 else if (bar_format == NULL && clock_enabled)
2245 /* if nothing to expand bail out */
2251 /* copy as-is, just in case the format shouldn't be expanded below */
2252 strlcpy(fmtexp, fmt, sz);
2253 /* finally pass the string through strftime(3) */
2254 #ifndef SWM_DENY_CLOCK_FORMAT
2255 if ((len = strftime(fmtexp, sz, fmt, &tm)) == 0)
2256 warnx("format too long");
2261 /* Redraws the bar; need to follow with xcb_flush() or focus_flush(). */
2265 char fmtexp[SWM_BAR_MAX], fmtnew[SWM_BAR_MAX];
2266 char fmtrep[SWM_BAR_MAX];
2268 struct swm_region *r;
2270 /* expand the format by first passing it through strftime(3) */
2271 bar_fmt_expand(fmtexp, sizeof fmtexp);
2273 num_screens = get_screen_count();
2274 for (i = 0; i < num_screens; i++) {
2275 TAILQ_FOREACH(r, &screens[i].rl, entry) {
2279 if (bar_enabled && r->ws->bar_enabled)
2280 xcb_map_window(conn, r->bar->id);
2282 xcb_unmap_window(conn, r->bar->id);
2286 if (startup_exception)
2287 snprintf(fmtrep, sizeof fmtrep, "total "
2288 "exceptions: %d, first exception: %s",
2292 bar_fmt(fmtexp, fmtnew, r, sizeof fmtnew);
2293 bar_replace(fmtnew, fmtrep, r, sizeof fmtrep);
2295 if (bar_font_legacy)
2296 bar_print_legacy(r, fmtrep);
2298 bar_print(r, fmtrep);
2304 * Reads external script output; call when stdin is readable.
2305 * Returns 1 if bar_ext was updated; otherwise 0.
2308 bar_extra_update(void)
2311 char b[SWM_BAR_MAX];
2317 while (fgets(b, sizeof(b), stdin) != NULL) {
2320 if (b[len - 1] == '\n') {
2321 /* Remove newline. */
2324 /* "Clear" bar_ext. */
2327 /* Flush buffered output. */
2328 strlcpy(bar_ext, bar_ext_buf, sizeof(bar_ext));
2329 bar_ext_buf[0] = '\0';
2331 /* Append new output to bar. */
2332 strlcat(bar_ext, b, sizeof(bar_ext));
2336 /* Buffer output. */
2337 strlcat(bar_ext_buf, b, sizeof(bar_ext_buf));
2342 if (errno != EAGAIN) {
2343 warn("bar_action failed");
2352 bar_toggle(struct swm_region *r, union arg *args)
2354 struct swm_region *tmpr;
2357 /* suppress unused warnings since vars are needed */
2361 DNPRINTF(SWM_D_BAR, "bar_toggle\n");
2364 case SWM_ARG_ID_BAR_TOGGLE_WS:
2365 /* Only change if master switch is enabled. */
2367 r->ws->bar_enabled = !r->ws->bar_enabled;
2369 bar_enabled = r->ws->bar_enabled = 1;
2371 case SWM_ARG_ID_BAR_TOGGLE:
2372 bar_enabled = !bar_enabled;
2376 /* update bars as necessary */
2377 num_screens = get_screen_count();
2378 for (i = 0; i < num_screens; i++)
2379 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
2381 if (bar_enabled && tmpr->ws->bar_enabled)
2382 xcb_map_window(conn, tmpr->bar->id);
2384 xcb_unmap_window(conn, tmpr->bar->id);
2389 /* must be after stack */
2396 bar_extra_setup(void)
2398 /* do this here because the conf file is in memory */
2399 if (!bar_extra && bar_argv[0]) {
2400 /* launch external status app */
2402 if (pipe(bar_pipe) == -1)
2403 err(1, "pipe error");
2404 socket_setnonblock(bar_pipe[0]);
2405 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
2407 /* Set stdin to read from the pipe. */
2408 if (dup2(bar_pipe[0], STDIN_FILENO) == -1)
2411 /* Set stdout to write to the pipe. */
2412 if (dup2(bar_pipe[1], STDOUT_FILENO) == -1)
2415 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
2416 err(1, "could not disable SIGPIPE");
2417 switch (bar_pid = fork()) {
2419 err(1, "cannot fork");
2423 execvp(bar_argv[0], bar_argv);
2424 err(1, "%s external app failed", bar_argv[0]);
2426 default: /* parent */
2431 atexit(kill_bar_extra_atexit);
2436 kill_bar_extra_atexit(void)
2439 kill(bar_pid, SIGTERM);
2447 while ((s = index(s, '-'))) {
2452 return (count == 14);
2458 char *default_string;
2459 char **missing_charsets;
2460 int num_missing_charsets = 0;
2464 XFreeFontSet(display, bar_fs);
2468 DNPRINTF(SWM_D_INIT, "fontset_init: loading bar_fonts: %s\n", bar_fonts);
2470 bar_fs = XCreateFontSet(display, bar_fonts, &missing_charsets,
2471 &num_missing_charsets, &default_string);
2473 if (num_missing_charsets > 0) {
2474 warnx("Unable to load charset(s):");
2476 for (i = 0; i < num_missing_charsets; ++i)
2477 warnx("%s", missing_charsets[i]);
2479 XFreeStringList(missing_charsets);
2481 if (strcmp(default_string, ""))
2482 warnx("Glyphs from those sets will be replaced "
2483 "by '%s'.", default_string);
2485 warnx("Glyphs from those sets won't be drawn.");
2489 errx(1, "Error creating font set structure.");
2491 bar_fs_extents = XExtentsOfFontSet(bar_fs);
2493 bar_height = bar_fs_extents->max_logical_extent.height +
2494 2 * bar_border_width;
2501 xft_init(struct swm_region *r)
2503 char *font, *d, *search;
2506 if (bar_font == NULL) {
2507 if ((d = strdup(bar_fonts)) == NULL)
2508 errx(1, "insufficient memory.");
2510 while ((font = strsep(&search, ",")) != NULL) {
2514 DNPRINTF(SWM_D_INIT, "xft_init: try font %s\n", font);
2517 bar_font = XftFontOpenXlfd(display, r->s->idx,
2520 bar_font = XftFontOpenName(display, r->s->idx,
2525 warnx("unable to load font %s", font);
2528 DNPRINTF(SWM_D_INIT, "successfully opened "
2536 if (bar_font == NULL)
2537 errx(1, "unable to open a font");
2539 PIXEL_TO_XRENDERCOLOR(r->s->c[SWM_S_COLOR_BAR_FONT].pixel, color);
2541 if (!XftColorAllocValue(display, DefaultVisual(display, r->s->idx),
2542 DefaultColormap(display, r->s->idx), &color, &bar_font_color))
2543 warn("Xft error: unable to allocate color.");
2545 bar_height = bar_font->height + 2 * bar_border_width;
2552 bar_setup(struct swm_region *r)
2554 xcb_screen_t *screen;
2557 DNPRINTF(SWM_D_BAR, "bar_setup: screen %d.\n",
2560 if ((screen = get_screen(r->s->idx)) == NULL)
2561 errx(1, "ERROR: can't get screen %d.", r->s->idx);
2566 if ((r->bar = calloc(1, sizeof(struct swm_bar))) == NULL)
2567 err(1, "bar_setup: calloc: failed to allocate memory.");
2569 if (bar_font_legacy)
2575 Y(r->bar) = bar_at_bottom ? (Y(r) + HEIGHT(r) - bar_height) : Y(r);
2576 WIDTH(r->bar) = WIDTH(r) - 2 * bar_border_width;
2577 HEIGHT(r->bar) = bar_height - 2 * bar_border_width;
2579 /* Assume region is unfocused when we create the bar. */
2580 r->bar->id = xcb_generate_id(conn);
2581 wa[0] = r->s->c[SWM_S_COLOR_BAR].pixel;
2582 wa[1] = r->s->c[SWM_S_COLOR_BAR_BORDER_UNFOCUS].pixel;
2583 wa[2] = XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_POINTER_MOTION |
2584 XCB_EVENT_MASK_POINTER_MOTION_HINT;
2586 xcb_create_window(conn, XCB_COPY_FROM_PARENT, r->bar->id, r->s->root,
2587 X(r->bar), Y(r->bar), WIDTH(r->bar), HEIGHT(r->bar),
2588 bar_border_width, XCB_WINDOW_CLASS_INPUT_OUTPUT,
2589 XCB_COPY_FROM_PARENT, XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL
2590 | XCB_CW_EVENT_MASK, wa);
2592 r->bar->buffer = xcb_generate_id(conn);
2593 xcb_create_pixmap(conn, screen->root_depth, r->bar->buffer, r->bar->id,
2594 WIDTH(r->bar), HEIGHT(r->bar));
2597 xcb_randr_select_input(conn, r->bar->id,
2598 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE);
2601 xcb_map_window(conn, r->bar->id);
2603 DNPRINTF(SWM_D_BAR, "bar_setup: window: 0x%x, (x,y) w x h: (%d,%d) "
2604 "%d x %d\n", WINID(r->bar), X(r->bar), Y(r->bar), WIDTH(r->bar),
2611 bar_cleanup(struct swm_region *r)
2615 xcb_destroy_window(conn, r->bar->id);
2616 xcb_free_pixmap(conn, r->bar->buffer);
2622 set_win_state(struct ws_win *win, uint16_t state)
2624 uint16_t data[2] = { state, XCB_ATOM_NONE };
2626 DNPRINTF(SWM_D_EVENT, "set_win_state: window: 0x%x, state: %u\n",
2632 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id, a_state,
2633 a_state, 32, 2, data);
2637 getstate(xcb_window_t w)
2639 uint32_t result = 0;
2640 xcb_get_property_cookie_t c;
2641 xcb_get_property_reply_t *r;
2643 c = xcb_get_property(conn, 0, w, a_state, a_state, 0L, 2L);
2644 r = xcb_get_property_reply(conn, c, NULL);
2647 if (r->type == a_state && r->format == 32 && r->length == 2)
2648 result = *((uint32_t *)xcb_get_property_value(r));
2652 DNPRINTF(SWM_D_MISC, "getstate property: win 0x%x state %u\n", w,
2658 version(struct swm_region *r, union arg *args)
2660 /* suppress unused warnings since vars are needed */
2664 bar_version = !bar_version;
2666 snprintf(bar_vertext, sizeof bar_vertext,
2667 "Version: %s Build: %s", SPECTRWM_VERSION, buildstr);
2669 strlcpy(bar_vertext, "", sizeof bar_vertext);
2676 client_msg(struct ws_win *win, xcb_atom_t a, xcb_timestamp_t t)
2678 xcb_client_message_event_t ev;
2686 name = get_atom_name(a);
2687 DNPRINTF(SWM_D_EVENT, "client_msg: window: 0x%x, atom: %s(%u), "
2689 win->id, name, a, t);
2693 bzero(&ev, sizeof ev);
2694 ev.response_type = XCB_CLIENT_MESSAGE;
2695 ev.window = win->id;
2698 ev.data.data32[0] = a;
2699 ev.data.data32[1] = t;
2701 xcb_send_event(conn, 0, win->id,
2702 XCB_EVENT_MASK_NO_EVENT, (const char *)&ev);
2705 /* synthetic response to a ConfigureRequest when not making a change */
2707 config_win(struct ws_win *win, xcb_configure_request_event_t *ev)
2709 xcb_configure_notify_event_t ce;
2714 /* send notification of unchanged state. */
2715 bzero(&ce, sizeof(ce));
2716 ce.response_type = XCB_CONFIGURE_NOTIFY;
2719 ce.width = WIDTH(win);
2720 ce.height = HEIGHT(win);
2721 ce.override_redirect = 0;
2726 ce.window = win->id;
2727 ce.border_width = BORDER(win);
2728 ce.above_sibling = XCB_WINDOW_NONE;
2731 ce.event = ev->window;
2732 ce.window = ev->window;
2734 /* make response appear more WM_SIZE_HINTS-compliant */
2735 if (win->sh.flags) {
2736 DNPRINTF(SWM_D_MISC, "config_win: hints: window: 0x%x,"
2737 " sh.flags: %u, min: %d x %d, max: %d x %d, inc: "
2738 "%d x %d\n", win->id, win->sh.flags, SH_MIN_W(win),
2739 SH_MIN_H(win), SH_MAX_W(win), SH_MAX_H(win),
2740 SH_INC_W(win), SH_INC_H(win));
2745 /* the hint may be set... to 0! */
2746 if (SH_MIN_W(win) > 0 && ce.width < SH_MIN_W(win))
2747 ce.width = SH_MIN_W(win);
2748 if (SH_MIN_H(win) > 0 && ce.height < SH_MIN_H(win))
2749 ce.height = SH_MIN_H(win);
2754 /* may also be advertized as 0 */
2755 if (SH_MAX_W(win) > 0 && ce.width > SH_MAX_W(win))
2756 ce.width = SH_MAX_W(win);
2757 if (SH_MAX_H(win) > 0 && ce.height > SH_MAX_H(win))
2758 ce.height = SH_MAX_H(win);
2761 /* resize increment. */
2763 if (SH_INC_W(win) > 1 && ce.width > SH_INC_W(win))
2764 ce.width -= (ce.width - SH_MIN_W(win)) %
2766 if (SH_INC_H(win) > 1 && ce.height > SH_INC_H(win))
2767 ce.height -= (ce.height - SH_MIN_H(win)) %
2771 /* adjust x and y for requested border_width. */
2772 ce.x += BORDER(win) - ev->border_width;
2773 ce.y += BORDER(win) - ev->border_width;
2774 ce.border_width = ev->border_width;
2775 ce.above_sibling = ev->sibling;
2778 DNPRINTF(SWM_D_MISC, "config_win: ewmh: %s, window: 0x%x, (x,y) w x h: "
2779 "(%d,%d) %d x %d, border: %d\n", YESNO(ev == NULL), win->id, ce.x,
2780 ce.y, ce.width, ce.height, ce.border_width);
2782 xcb_send_event(conn, 0, win->id, XCB_EVENT_MASK_STRUCTURE_NOTIFY,
2787 count_win(struct workspace *ws, int count_transient)
2792 TAILQ_FOREACH(win, &ws->winlist, entry) {
2793 if (!count_transient && win->floating)
2795 if (!count_transient && win->transient)
2801 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
2807 quit(struct swm_region *r, union arg *args)
2809 /* suppress unused warnings since vars are needed */
2813 DNPRINTF(SWM_D_MISC, "quit\n");
2818 map_window(struct ws_win *win, xcb_window_t sibling)
2820 uint16_t mode = XCB_CONFIG_WINDOW_STACK_MODE;
2824 /* If sibling is specified, stack right above it. */
2825 if (sibling != XCB_WINDOW_NONE) {
2826 mode |= XCB_CONFIG_WINDOW_SIBLING;
2830 val[i] = XCB_STACK_MODE_ABOVE;
2835 DNPRINTF(SWM_D_EVENT, "map_window: win 0x%x, mapped: %s, "
2836 "sibling: 0x%x\n", win->id, YESNO(win->mapped), sibling);
2838 xcb_configure_window(conn, win->id, mode, val);
2843 xcb_map_window(conn, win->id);
2844 set_win_state(win, XCB_ICCCM_WM_STATE_NORMAL);
2849 unmap_window(struct ws_win *win)
2854 DNPRINTF(SWM_D_EVENT, "unmap_window: win 0x%x, mapped: %s\n", win->id,
2855 YESNO(win->mapped));
2860 xcb_unmap_window(conn, win->id);
2861 set_win_state(win, XCB_ICCCM_WM_STATE_ICONIC);
2869 int i, j, num_screens;
2871 num_screens = get_screen_count();
2872 for (i = 0; i < num_screens; i++)
2873 for (j = 0; j < workspace_limit; j++)
2874 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2879 fake_keypress(struct ws_win *win, xcb_keysym_t keysym, uint16_t modifiers)
2881 xcb_key_press_event_t event;
2882 xcb_keycode_t *keycode;
2887 keycode = xcb_key_symbols_get_keycode(syms, keysym);
2889 DNPRINTF(SWM_D_MISC, "fake_keypress: win 0x%x keycode %u\n",
2892 bzero(&event, sizeof(event));
2893 event.event = win->id;
2894 event.root = win->s->root;
2895 event.child = XCB_WINDOW_NONE;
2896 event.time = XCB_CURRENT_TIME;
2897 event.event_x = X(win);
2898 event.event_y = Y(win);
2901 event.same_screen = 1;
2902 event.detail = *keycode;
2903 event.state = modifiers;
2905 event.response_type = XCB_KEY_PRESS;
2906 xcb_send_event(conn, 1, win->id,
2907 XCB_EVENT_MASK_KEY_PRESS, (const char *)&event);
2909 event.response_type = XCB_KEY_RELEASE;
2910 xcb_send_event(conn, 1, win->id,
2911 XCB_EVENT_MASK_KEY_RELEASE, (const char *)&event);
2917 restart(struct swm_region *r, union arg *args)
2919 /* suppress unused warning since var is needed */
2923 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
2927 execvp(start_argv[0], start_argv);
2928 warn("execvp failed");
2933 get_pointer_win(xcb_window_t root)
2935 struct ws_win *win = NULL;
2936 xcb_query_pointer_reply_t *r;
2938 DNPRINTF(SWM_D_EVENT, "get_pointer_win: root: 0x%x.\n", root);
2940 r = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, root), NULL);
2942 win = find_window(r->child);
2944 DNPRINTF(SWM_D_EVENT, "get_pointer_win: 0x%x.\n",
2947 DNPRINTF(SWM_D_EVENT, "get_pointer_win: none.\n");
2956 root_to_region(xcb_window_t root, int check)
2959 struct swm_region *r = NULL;
2961 xcb_query_pointer_reply_t *qpr;
2962 xcb_get_input_focus_reply_t *gifr;
2964 DNPRINTF(SWM_D_MISC, "root_to_region: window: 0x%x\n", root);
2966 num_screens = get_screen_count();
2967 for (i = 0; i < num_screens; i++)
2968 if (screens[i].root == root)
2971 if (check & SWM_CK_REGION)
2972 r = screens[i].r_focus;
2974 if (r == NULL && check & SWM_CK_FOCUS) {
2975 /* Try to find an actively focused window */
2976 gifr = xcb_get_input_focus_reply(conn,
2977 xcb_get_input_focus(conn), NULL);
2979 cfw = find_window(gifr->focus);
2980 if (cfw && cfw->ws->r)
2987 if (r == NULL && check & SWM_CK_POINTER) {
2988 /* No region with an active focus; try to use pointer. */
2989 qpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn,
2990 screens[i].root), NULL);
2993 DNPRINTF(SWM_D_MISC, "root_to_region: pointer: "
2994 "(%d,%d)\n", qpr->root_x, qpr->root_y);
2995 TAILQ_FOREACH(r, &screens[i].rl, entry)
2996 if (X(r) <= qpr->root_x &&
2997 qpr->root_x < MAX_X(r) &&
2998 Y(r) <= qpr->root_y &&
2999 qpr->root_y < MAX_Y(r))
3006 if (r == NULL && check & SWM_CK_FALLBACK)
3007 r = TAILQ_FIRST(&screens[i].rl);
3009 DNPRINTF(SWM_D_MISC, "root_to_region: idx: %d\n", get_region_index(r));
3015 find_unmanaged_window(xcb_window_t id)
3018 int i, j, num_screens;
3020 num_screens = get_screen_count();
3021 for (i = 0; i < num_screens; i++)
3022 for (j = 0; j < workspace_limit; j++)
3023 TAILQ_FOREACH(win, &screens[i].ws[j].unmanagedlist,
3031 find_window(xcb_window_t id)
3034 int i, j, num_screens;
3035 xcb_query_tree_reply_t *r;
3037 num_screens = get_screen_count();
3038 for (i = 0; i < num_screens; i++)
3039 for (j = 0; j < workspace_limit; j++)
3040 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
3044 r = xcb_query_tree_reply(conn, xcb_query_tree(conn, id), NULL);
3048 /* if we were looking for the parent return that window instead */
3049 if (r->parent == 0 || r->root == r->parent) {
3054 /* look for parent */
3055 for (i = 0; i < num_screens; i++)
3056 for (j = 0; j < workspace_limit; j++)
3057 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
3058 if (r->parent == win->id) {
3068 spawn(int ws_idx, union arg *args, int close_fd)
3073 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
3075 close(xcb_get_file_descriptor(conn));
3077 setenv("LD_PRELOAD", SWM_LIB, 1);
3079 if (asprintf(&ret, "%d", ws_idx) == -1) {
3080 warn("spawn: asprintf SWM_WS");
3083 setenv("_SWM_WS", ret, 1);
3087 if (asprintf(&ret, "%d", getpid()) == -1) {
3088 warn("spawn: asprintf _SWM_PID");
3091 setenv("_SWM_PID", ret, 1);
3095 if (setsid() == -1) {
3096 warn("spawn: setsid");
3102 * close stdin and stdout to prevent interaction between apps
3103 * and the baraction script
3104 * leave stderr open to record errors
3106 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) == -1) {
3107 warn("spawn: open");
3110 dup2(fd, STDIN_FILENO);
3111 dup2(fd, STDOUT_FILENO);
3116 execvp(args->argv[0], args->argv);
3118 warn("spawn: execvp");
3123 kill_refs(struct ws_win *win)
3125 int i, x, num_screens;
3126 struct swm_region *r;
3127 struct workspace *ws;
3132 num_screens = get_screen_count();
3133 for (i = 0; i < num_screens; i++)
3134 TAILQ_FOREACH(r, &screens[i].rl, entry)
3135 for (x = 0; x < workspace_limit; x++) {
3137 if (win == ws->focus)
3139 if (win == ws->focus_prev)
3140 ws->focus_prev = NULL;
3145 validate_win(struct ws_win *testwin)
3148 struct workspace *ws;
3149 struct swm_region *r;
3150 int i, x, num_screens;
3152 if (testwin == NULL)
3155 num_screens = get_screen_count();
3156 for (i = 0; i < num_screens; i++)
3157 TAILQ_FOREACH(r, &screens[i].rl, entry)
3158 for (x = 0; x < workspace_limit; x++) {
3160 TAILQ_FOREACH(win, &ws->winlist, entry)
3168 validate_ws(struct workspace *testws)
3170 struct swm_region *r;
3171 struct workspace *ws;
3172 int i, x, num_screens;
3174 /* validate all ws */
3175 num_screens = get_screen_count();
3176 for (i = 0; i < num_screens; i++)
3177 TAILQ_FOREACH(r, &screens[i].rl, entry)
3178 for (x = 0; x < workspace_limit; x++) {
3187 get_sibling(struct ws_win *win, int mode)
3189 struct ws_win *w = win;
3193 TAILQ_FOREACH_REVERSE(w, &w->ws->winlist, ws_win_list, entry)
3194 if (w != win && !w->floating && !w->iconic)
3197 case SWM_STACK_ABOVE:
3199 w = TAILQ_NEXT(w, entry);
3200 } while (w != NULL && (w == win || w->floating || w->iconic));
3202 case SWM_STACK_BELOW:
3204 w = TAILQ_PREV(w, ws_win_list, entry);
3205 } while (w != NULL && (w == win || w->floating || w->iconic));
3207 case SWM_STACK_BOTTOM:
3208 TAILQ_FOREACH(w, &w->ws->winlist, entry)
3209 if (w != win && !w->floating && !w->iconic)
3217 return (win->ws->r->id);
3223 unfocus_win(struct ws_win *win)
3225 xcb_window_t none = XCB_WINDOW_NONE;
3227 DNPRINTF(SWM_D_FOCUS, "unfocus_win: window: 0x%x\n", WINID(win));
3231 if (win->ws == NULL) {
3232 DNPRINTF(SWM_D_FOCUS, "unfocus_win: NULL ws.\n");
3236 if (validate_ws(win->ws)) {
3237 DNPRINTF(SWM_D_FOCUS, "unfocus_win: invalid ws.\n");
3241 if (win->ws->r == NULL) {
3242 DNPRINTF(SWM_D_FOCUS, "unfocus_win: NULL region.\n");
3246 if (validate_win(win)) {
3247 DNPRINTF(SWM_D_FOCUS, "unfocus_win: invalid win.\n");
3252 if (win->ws->focus == win) {
3253 if (tile_gap < 0 && !win->floating)
3254 map_window(win, get_sibling(win, SWM_STACK_BELOW));
3255 win->ws->focus = NULL;
3256 win->ws->focus_prev = win;
3259 if (validate_win(win->ws->focus)) {
3260 kill_refs(win->ws->focus);
3261 win->ws->focus = NULL;
3264 if (validate_win(win->ws->focus_prev)) {
3265 kill_refs(win->ws->focus_prev);
3266 win->ws->focus_prev = NULL;
3269 xcb_change_window_attributes(conn, win->id, XCB_CW_BORDER_PIXEL,
3270 &win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].pixel);
3272 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
3273 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1, &none);
3275 DNPRINTF(SWM_D_FOCUS, "unfocus_win: done.\n");
3279 focus_win(struct ws_win *win)
3281 struct ws_win *cfw = NULL, *parent = NULL, *w;
3282 struct workspace *ws;
3283 xcb_get_input_focus_reply_t *r;
3285 DNPRINTF(SWM_D_FOCUS, "focus_win: window: 0x%x\n", WINID(win));
3290 if (win->ws == NULL)
3298 if (validate_ws(ws))
3301 if (validate_win(win)) {
3306 r = xcb_get_input_focus_reply(conn, xcb_get_input_focus(conn), NULL);
3308 cfw = find_window(r->focus);
3314 if (ws->focus != win) {
3315 if (ws->focus && ws->focus != cfw)
3316 unfocus_win(ws->focus);
3320 /* If this window directs focus to a child window, then clear. */
3321 if (win->focus_child)
3322 win->focus_child = NULL;
3324 /* If transient, adjust parent's focus child for focus_magic. */
3325 if (win->transient) {
3326 parent = find_window(win->transient);
3327 if (parent && parent->focus_child != win)
3328 parent->focus_child = win;
3331 if (cfw != win && ws->r != NULL) {
3332 /* Set input focus if no input hint, or indicated by hint. */
3333 if (!(win->hints.flags & XCB_ICCCM_WM_HINT_INPUT) ||
3334 (win->hints.flags & XCB_ICCCM_WM_HINT_INPUT &&
3336 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT,
3337 win->id, last_event_time);
3339 /* Tell app it can adjust focus to a specific window. */
3340 if (win->take_focus) {
3341 /* java is special; always tell parent */
3342 if (win->transient && win->java)
3343 client_msg(parent, a_takefocus,
3346 client_msg(win, a_takefocus, last_event_time);
3349 xcb_change_window_attributes(conn, win->id, XCB_CW_BORDER_PIXEL,
3350 &ws->r->s->c[SWM_S_COLOR_FOCUS].pixel);
3352 if (ws->cur_layout->flags & SWM_L_MAPONFOCUS ||
3354 /* If a parent exists, map it first. */
3356 map_window(parent, XCB_WINDOW_NONE);
3358 /* Map siblings next. */
3359 TAILQ_FOREACH(w, &ws->winlist, entry)
3360 if (w != win && !w->iconic &&
3361 w->transient == parent->id)
3362 map_window(w, XCB_WINDOW_NONE);
3365 /* Map focused window. */
3366 map_window(win, XCB_WINDOW_NONE);
3368 /* Finally, map children of focus window. */
3369 TAILQ_FOREACH(w, &ws->winlist, entry)
3370 if (w->transient == win->id && !w->iconic)
3371 map_window(w, XCB_WINDOW_NONE);
3372 } else if (tile_gap < 0 && !win->floating) {
3374 * Windows overlap in the layout.
3375 * Raise focused win above all tiled wins.
3377 if (tile_gap < 0 && !win->floating)
3379 get_sibling(win, SWM_STACK_TOP));
3384 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
3385 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1,
3392 DNPRINTF(SWM_D_FOCUS, "focus_win: done.\n");
3395 /* If a child window should have focus instead, return it. */
3397 get_focus_magic(struct ws_win *win)
3399 struct ws_win *parent = NULL;
3400 struct ws_win *child = NULL;
3402 DNPRINTF(SWM_D_FOCUS, "get_focus_magic: window: 0x%x\n", WINID(win));
3406 if (win->transient) {
3407 parent = find_window(win->transient);
3409 /* If parent prefers focus elsewhere, then try to do so. */
3410 if (parent && (child = parent->focus_child)) {
3411 if (validate_win(child) == 0 && child->mapped)
3414 parent->focus_child = NULL;
3418 /* If this window prefers focus elsewhere, then try to do so. */
3419 if ((child = win->focus_child)) {
3420 if (validate_win(child) == 0 && child->mapped)
3423 win->focus_child = NULL;
3430 event_drain(uint8_t rt)
3432 xcb_generic_event_t *evt;
3434 /* ensure all pending requests have been processed before filtering. */
3436 while ((evt = xcb_poll_for_event(conn))) {
3437 if (XCB_EVENT_RESPONSE_TYPE(evt) != rt)
3445 set_region(struct swm_region *r)
3447 struct swm_region *rf;
3453 /* Unfocus old region bar. */
3458 xcb_change_window_attributes(conn, rf->bar->id,
3459 XCB_CW_BORDER_PIXEL,
3460 &r->s->c[SWM_S_COLOR_BAR_BORDER_UNFOCUS].pixel);
3463 /* Set region bar border to focus_color. */
3464 xcb_change_window_attributes(conn, r->bar->id,
3465 XCB_CW_BORDER_PIXEL, &r->s->c[SWM_S_COLOR_BAR_BORDER].pixel);
3471 focus_region(struct swm_region *r)
3474 struct swm_region *old_r;
3479 old_r = r->s->r_focus;
3482 nfw = get_region_focus(r);
3486 /* New region is empty; need to manually unfocus win. */
3488 unfocus_win(old_r->ws->focus);
3490 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT, r->id,
3493 /* Clear bar since empty. */
3499 switchws(struct swm_region *r, union arg *args)
3501 struct swm_region *this_r, *other_r;
3503 struct workspace *new_ws, *old_ws;
3504 xcb_window_t none = XCB_WINDOW_NONE;
3505 int wsid = args->id, unmap_old = 0;
3510 if (wsid >= workspace_limit)
3514 old_ws = this_r->ws;
3515 new_ws = &this_r->s->ws[wsid];
3517 DNPRINTF(SWM_D_WS, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
3518 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), old_ws->idx, wsid);
3520 if (new_ws == NULL || old_ws == NULL)
3522 if (new_ws == old_ws)
3525 if ((win = old_ws->focus) != NULL) {
3526 xcb_change_window_attributes(conn, win->id, XCB_CW_BORDER_PIXEL,
3527 &win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].pixel);
3529 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
3530 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1,
3534 other_r = new_ws->r;
3535 if (other_r == NULL) {
3536 /* the other workspace is hidden, hide this one */
3540 /* the other ws is visible in another region, exchange them */
3541 other_r->ws_prior = new_ws;
3542 other_r->ws = old_ws;
3543 old_ws->r = other_r;
3545 this_r->ws_prior = old_ws;
3546 this_r->ws = new_ws;
3549 /* Set focus_pending before stacking, if needed. */
3550 if (focus_mode != SWM_FOCUS_FOLLOW && (!new_ws->focus_pending ||
3551 validate_win(new_ws->focus_pending))) {
3552 new_ws->focus_pending = get_region_focus(new_ws->r);
3553 new_ws->focus = new_ws->focus_prev;
3554 new_ws->focus_prev = NULL;
3559 /* unmap old windows */
3561 TAILQ_FOREACH(win, &old_ws->winlist, entry)
3564 /* if workspaces were swapped, then don't wait to set focus */
3565 if (old_ws->r && focus_mode != SWM_FOCUS_FOLLOW) {
3566 if (new_ws->focus_pending) {
3567 focus_win(new_ws->focus_pending);
3568 new_ws->focus_pending = NULL;
3572 /* Clear bar and set focus on region input win if new ws is empty. */
3573 if (new_ws->focus_pending == NULL && new_ws->focus == NULL) {
3574 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT, r->id,
3581 DNPRINTF(SWM_D_WS, "switchws: done.\n");
3585 cyclews(struct swm_region *r, union arg *args)
3588 struct swm_screen *s = r->s;
3592 DNPRINTF(SWM_D_WS, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3593 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
3599 case SWM_ARG_ID_CYCLEWS_MOVE_UP:
3602 case SWM_ARG_ID_CYCLEWS_UP_ALL:
3605 case SWM_ARG_ID_CYCLEWS_UP:
3606 a.id = (a.id < workspace_limit - 1) ? a.id + 1 : 0;
3608 case SWM_ARG_ID_CYCLEWS_MOVE_DOWN:
3611 case SWM_ARG_ID_CYCLEWS_DOWN_ALL:
3614 case SWM_ARG_ID_CYCLEWS_DOWN:
3615 a.id = (a.id > 0) ? a.id - 1 : workspace_limit - 1;
3622 (!cycle_empty && TAILQ_EMPTY(&s->ws[a.id].winlist)))
3624 if (!cycle_visible && s->ws[a.id].r != NULL)
3631 } while (a.id != r->ws->idx);
3635 priorws(struct swm_region *r, union arg *args)
3641 DNPRINTF(SWM_D_WS, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3642 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
3644 if (r->ws_prior == NULL)
3647 a.id = r->ws_prior->idx;
3652 focusrg(struct swm_region *r, union arg *args)
3654 int ridx = args->id, i, num_screens;
3655 struct swm_region *rr = NULL;
3657 num_screens = get_screen_count();
3658 /* do nothing if we don't have more than one screen */
3659 if (!(num_screens > 1 || outputs > 1))
3662 DNPRINTF(SWM_D_FOCUS, "focusrg: id: %d\n", ridx);
3664 rr = TAILQ_FIRST(&r->s->rl);
3665 for (i = 0; i < ridx && rr != NULL; ++i)
3666 rr = TAILQ_NEXT(rr, entry);
3676 cyclerg(struct swm_region *r, union arg *args)
3678 struct swm_region *rr = NULL;
3681 num_screens = get_screen_count();
3682 /* do nothing if we don't have more than one screen */
3683 if (!(num_screens > 1 || outputs > 1))
3688 case SWM_ARG_ID_CYCLERG_UP:
3689 rr = TAILQ_NEXT(r, entry);
3691 rr = TAILQ_FIRST(&screens[i].rl);
3693 case SWM_ARG_ID_CYCLERG_DOWN:
3694 rr = TAILQ_PREV(r, swm_region_list, entry);
3696 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
3709 sort_windows(struct ws_win_list *wl)
3711 struct ws_win *win, *parent, *nxt;
3716 for (win = TAILQ_FIRST(wl); win != TAILQ_END(wl); win = nxt) {
3717 nxt = TAILQ_NEXT(win, entry);
3718 if (win->transient) {
3719 parent = find_window(win->transient);
3720 if (parent == NULL) {
3721 warnx("not possible bug");
3724 TAILQ_REMOVE(wl, win, entry);
3725 TAILQ_INSERT_AFTER(wl, parent, win, entry);
3732 swapwin(struct swm_region *r, union arg *args)
3734 struct ws_win *target, *source;
3735 struct ws_win *cur_focus;
3736 struct ws_win_list *wl;
3738 DNPRINTF(SWM_D_WS, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3739 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
3741 cur_focus = r->ws->focus;
3742 if (cur_focus == NULL)
3746 wl = &source->ws->winlist;
3749 case SWM_ARG_ID_SWAPPREV:
3750 if (source->transient)
3751 source = find_window(source->transient);
3752 target = TAILQ_PREV(source, ws_win_list, entry);
3753 if (target && target->transient)
3754 target = find_window(target->transient);
3755 TAILQ_REMOVE(wl, source, entry);
3757 TAILQ_INSERT_TAIL(wl, source, entry);
3759 TAILQ_INSERT_BEFORE(target, source, entry);
3761 case SWM_ARG_ID_SWAPNEXT:
3762 target = TAILQ_NEXT(source, entry);
3763 /* move the parent and let the sort handle the move */
3764 if (source->transient)
3765 source = find_window(source->transient);
3766 TAILQ_REMOVE(wl, source, entry);
3768 TAILQ_INSERT_HEAD(wl, source, entry);
3770 TAILQ_INSERT_AFTER(wl, target, source, entry);
3772 case SWM_ARG_ID_SWAPMAIN:
3773 target = TAILQ_FIRST(wl);
3774 if (target == source) {
3775 if (source->ws->focus_prev != NULL &&
3776 source->ws->focus_prev != target)
3777 source = source->ws->focus_prev;
3781 if (target == NULL || source == NULL)
3783 source->ws->focus_prev = target;
3784 TAILQ_REMOVE(wl, target, entry);
3785 TAILQ_INSERT_BEFORE(source, target, entry);
3786 TAILQ_REMOVE(wl, source, entry);
3787 TAILQ_INSERT_HEAD(wl, source, entry);
3789 case SWM_ARG_ID_MOVELAST:
3790 TAILQ_REMOVE(wl, source, entry);
3791 TAILQ_INSERT_TAIL(wl, source, entry);
3794 DNPRINTF(SWM_D_MOVE, "swapwin: invalid id: %d\n", args->id);
3806 get_focus_prev(struct ws_win *win)
3808 struct ws_win *winfocus = NULL;
3809 struct ws_win *cur_focus = NULL;
3810 struct ws_win_list *wl = NULL;
3811 struct workspace *ws = NULL;
3813 if (!(win && win->ws))
3818 cur_focus = ws->focus;
3820 DNPRINTF(SWM_D_FOCUS, "get_focus_prev: window: 0x%x, cur_focus: 0x%x\n",
3821 WINID(win), WINID(cur_focus));
3823 /* pickle, just focus on whatever */
3824 if (cur_focus == NULL) {
3825 /* use prev_focus if valid */
3826 if (ws->focus_prev && ws->focus_prev != cur_focus &&
3827 find_window(WINID(ws->focus_prev)))
3828 winfocus = ws->focus_prev;
3832 /* if transient focus on parent */
3833 if (cur_focus->transient) {
3834 winfocus = find_window(cur_focus->transient);
3838 /* if in max_stack try harder */
3839 if ((win->quirks & SWM_Q_FOCUSPREV) ||
3840 (ws->cur_layout->flags & SWM_L_FOCUSPREV)) {
3841 if (cur_focus != ws->focus_prev)
3842 winfocus = ws->focus_prev;
3844 winfocus = TAILQ_PREV(win, ws_win_list, entry);
3849 DNPRINTF(SWM_D_FOCUS, "get_focus_prev: focus_close: %d\n", focus_close);
3851 if (winfocus == NULL || winfocus == win) {
3852 switch (focus_close) {
3853 case SWM_STACK_BOTTOM:
3854 TAILQ_FOREACH(winfocus, wl, entry)
3855 if (!winfocus->iconic && winfocus != cur_focus)
3859 TAILQ_FOREACH_REVERSE(winfocus, wl, ws_win_list, entry)
3860 if (!winfocus->iconic && winfocus != cur_focus)
3863 case SWM_STACK_ABOVE:
3864 winfocus = TAILQ_NEXT(cur_focus, entry);
3865 while (winfocus && winfocus->iconic)
3866 winfocus = TAILQ_NEXT(winfocus, entry);
3868 if (winfocus == NULL) {
3869 if (focus_close_wrap) {
3870 TAILQ_FOREACH(winfocus, wl, entry)
3871 if (!winfocus->iconic &&
3872 winfocus != cur_focus)
3875 TAILQ_FOREACH_REVERSE(winfocus, wl,
3877 if (!winfocus->iconic &&
3878 winfocus != cur_focus)
3883 case SWM_STACK_BELOW:
3884 winfocus = TAILQ_PREV(cur_focus, ws_win_list, entry);
3885 while (winfocus && winfocus->iconic)
3886 winfocus = TAILQ_PREV(winfocus, ws_win_list,
3889 if (winfocus == NULL) {
3890 if (focus_close_wrap) {
3891 TAILQ_FOREACH_REVERSE(winfocus, wl,
3893 if (!winfocus->iconic &&
3894 winfocus != cur_focus)
3897 TAILQ_FOREACH(winfocus, wl, entry)
3898 if (!winfocus->iconic &&
3899 winfocus != cur_focus)
3907 if (winfocus == NULL ||
3908 (winfocus && (winfocus->iconic || winfocus == cur_focus))) {
3909 if (focus_default == SWM_STACK_TOP) {
3910 TAILQ_FOREACH_REVERSE(winfocus, wl, ws_win_list, entry)
3911 if (!winfocus->iconic && winfocus != cur_focus)
3914 TAILQ_FOREACH(winfocus, wl, entry)
3915 if (!winfocus->iconic && winfocus != cur_focus)
3922 return get_focus_magic(winfocus);
3926 get_region_focus(struct swm_region *r)
3928 struct ws_win *winfocus = NULL;
3933 if (r->ws->focus && !r->ws->focus->iconic)
3934 winfocus = r->ws->focus;
3935 else if (r->ws->focus_prev && !r->ws->focus_prev->iconic)
3936 winfocus = r->ws->focus_prev;
3938 TAILQ_FOREACH(winfocus, &r->ws->winlist, entry)
3939 if (!winfocus->iconic)
3942 return get_focus_magic(winfocus);
3946 focus(struct swm_region *r, union arg *args)
3948 struct ws_win *head, *cur_focus = NULL, *winfocus = NULL;
3949 struct ws_win_list *wl = NULL;
3950 struct workspace *ws = NULL;
3953 xcb_icccm_wm_hints_t hints;
3958 DNPRINTF(SWM_D_FOCUS, "focus: id: %d\n", args->id);
3960 cur_focus = r->ws->focus;
3964 /* Make sure an uniconified window has focus, if one exists. */
3965 if (cur_focus == NULL) {
3966 cur_focus = TAILQ_FIRST(wl);
3967 while (cur_focus != NULL && cur_focus->iconic)
3968 cur_focus = TAILQ_NEXT(cur_focus, entry);
3972 case SWM_ARG_ID_FOCUSPREV:
3973 if (cur_focus == NULL)
3976 winfocus = cur_focus;
3978 winfocus = TAILQ_PREV(winfocus, ws_win_list, entry);
3979 if (winfocus == NULL)
3980 winfocus = TAILQ_LAST(wl, ws_win_list);
3981 if (winfocus == cur_focus)
3983 } while (winfocus != NULL &&
3984 (winfocus->iconic || winfocus->id == cur_focus->transient));
3986 case SWM_ARG_ID_FOCUSNEXT:
3987 if (cur_focus == NULL)
3990 winfocus = cur_focus;
3992 winfocus = TAILQ_NEXT(winfocus, entry);
3993 if (winfocus == NULL)
3994 winfocus = TAILQ_FIRST(wl);
3995 if (winfocus == cur_focus)
3997 } while (winfocus != NULL &&
3998 (winfocus->iconic || winfocus->id == cur_focus->transient));
4000 case SWM_ARG_ID_FOCUSMAIN:
4001 if (cur_focus == NULL)
4004 winfocus = TAILQ_FIRST(wl);
4005 if (winfocus == cur_focus)
4006 winfocus = cur_focus->ws->focus_prev;
4008 case SWM_ARG_ID_FOCUSURGENT:
4009 /* Search forward for the next urgent window. */
4013 for (i = 0; i <= workspace_limit; ++i) {
4015 head = TAILQ_FIRST(&r->s->ws[(ws->idx + i) %
4016 workspace_limit].winlist);
4018 while (head != NULL &&
4019 (head = TAILQ_NEXT(head, entry)) != NULL) {
4020 if (head == cur_focus) {
4021 winfocus = cur_focus;
4024 if (xcb_icccm_get_wm_hints_reply(conn,
4025 xcb_icccm_get_wm_hints(conn, head->id),
4026 &hints, NULL) != 0 &&
4027 xcb_icccm_wm_hints_get_urgency(&hints)) {
4033 if (winfocus != NULL)
4037 /* Switch ws if new focus is on a different ws. */
4038 if (winfocus != NULL && winfocus->ws != ws) {
4039 a.id = winfocus->ws->idx;
4047 focus_win(get_focus_magic(winfocus));
4051 DNPRINTF(SWM_D_FOCUS, "focus: done\n");
4055 cycle_layout(struct swm_region *r, union arg *args)
4057 struct workspace *ws = r->ws;
4059 /* suppress unused warning since var is needed */
4062 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
4065 if (ws->cur_layout->l_stack == NULL)
4066 ws->cur_layout = &layouts[0];
4071 focus_win(get_region_focus(r));
4077 stack_config(struct swm_region *r, union arg *args)
4079 struct workspace *ws = r->ws;
4081 DNPRINTF(SWM_D_STACK, "stack_config: id: %d workspace: %d\n",
4084 if (ws->cur_layout->l_config != NULL)
4085 ws->cur_layout->l_config(ws, args->id);
4087 if (args->id != SWM_ARG_ID_STACKINIT)
4096 struct swm_geometry g;
4097 struct swm_region *r;
4103 DNPRINTF(SWM_D_STACK, "stack: begin\n");
4105 num_screens = get_screen_count();
4106 for (i = 0; i < num_screens; i++) {
4110 TAILQ_FOREACH(r, &screens[i].rl, entry) {
4111 DNPRINTF(SWM_D_STACK, "stack: workspace: %d "
4112 "(screen: %d, region: %d)\n", r->ws->idx, i, j++);
4114 /* Adjust stack area for region bar and padding. */
4116 g.x += region_padding;
4117 g.y += region_padding;
4118 g.w -= 2 * border_width + 2 * region_padding;
4119 g.h -= 2 * border_width + 2 * region_padding;
4120 if (bar_enabled && r->ws->bar_enabled) {
4125 r->ws->cur_layout->l_stack(r->ws, &g);
4126 r->ws->cur_layout->l_string(r->ws);
4127 /* save r so we can track region changes */
4134 DNPRINTF(SWM_D_STACK, "stack: end\n");
4138 store_float_geom(struct ws_win *win)
4140 if (win == NULL || win->ws->r == NULL)
4143 /* retain window geom and region geom */
4144 win->g_float = win->g;
4145 win->g_float.x -= X(win->ws->r);
4146 win->g_float.y -= Y(win->ws->r);
4147 win->g_floatvalid = 1;
4148 DNPRINTF(SWM_D_MISC, "store_float_geom: window: 0x%x, g: (%d,%d)"
4149 " %d x %d, g_float: (%d,%d) %d x %d\n", win->id, X(win), Y(win),
4150 WIDTH(win), HEIGHT(win), win->g_float.x, win->g_float.y,
4151 win->g_float.w, win->g_float.h);
4155 load_float_geom(struct ws_win *win)
4157 if (win == NULL || win->ws->r == NULL)
4160 if (win->g_floatvalid) {
4161 win->g = win->g_float;
4162 X(win) += X(win->ws->r);
4163 Y(win) += Y(win->ws->r);
4164 DNPRINTF(SWM_D_MISC, "load_float_geom: window: 0x%x, g: (%d,%d)"
4165 "%d x %d\n", win->id, X(win), Y(win), WIDTH(win),
4168 DNPRINTF(SWM_D_MISC, "load_float_geom: window: 0x%x, g_float "
4169 "is not set.\n", win->id);
4174 stack_floater(struct ws_win *win, struct swm_region *r)
4179 DNPRINTF(SWM_D_MISC, "stack_floater: window: 0x%x\n", win->id);
4182 * to allow windows to change their size (e.g. mplayer fs) only retrieve
4183 * geom on ws switches or return from max mode
4185 if (win->floatmaxed || (r != r->ws->old_r &&
4186 !(win->ewmh_flags & EWMH_F_FULLSCREEN))) {
4187 /* update geometry for the new region */
4188 load_float_geom(win);
4191 win->floatmaxed = 0;
4194 * if set to fullscreen mode, configure window to maximum size.
4196 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
4197 if (!win->g_floatvalid)
4198 store_float_geom(win);
4204 * remove border on fullscreen floater when in fullscreen mode or when
4205 * the quirk is present.
4207 if ((win->ewmh_flags & EWMH_F_FULLSCREEN) ||
4208 ((win->quirks & SWM_Q_FULLSCREEN) &&
4209 (WIDTH(win) >= WIDTH(r)) && (HEIGHT(win) >= HEIGHT(r)))) {
4210 if (win->bordered) {
4212 X(win) += border_width;
4213 Y(win) += border_width;
4215 } else if (!win->bordered) {
4217 X(win) -= border_width;
4218 Y(win) -= border_width;
4221 if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
4222 WIDTH(win) = (double)WIDTH(r) * dialog_ratio;
4223 HEIGHT(win) = (double)HEIGHT(r) * dialog_ratio;
4226 if (!win->manual && !(win->ewmh_flags & EWMH_F_FULLSCREEN) &&
4227 !(win->quirks & SWM_Q_ANYWHERE)) {
4229 * floaters and transients are auto-centred unless moved,
4230 * resized or ANYWHERE quirk is set.
4232 X(win) = X(r) + (WIDTH(r) - WIDTH(win)) / 2 - BORDER(win);
4233 Y(win) = Y(r) + (HEIGHT(r) - HEIGHT(win)) / 2 - BORDER(win);
4235 store_float_geom(win);
4238 /* Ensure at least 1 pixel of the window is in the region. */
4239 region_containment(win, r, SWM_CW_ALLSIDES);
4245 * Send keystrokes to terminal to decrease/increase the font size as the
4246 * window size changes.
4249 adjust_font(struct ws_win *win)
4251 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
4252 win->floating || win->transient)
4255 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
4256 WIDTH(win) / win->sh.width_inc < term_width &&
4257 win->font_steps < SWM_MAX_FONT_STEPS) {
4258 win->font_size_boundary[win->font_steps] =
4259 (win->sh.width_inc * term_width) + win->sh.base_width;
4262 win->last_inc = win->sh.width_inc;
4263 fake_keypress(win, XK_KP_Subtract, XCB_MOD_MASK_SHIFT);
4264 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
4265 WIDTH(win) > win->font_size_boundary[win->font_steps - 1]) {
4268 win->last_inc = win->sh.width_inc;
4269 fake_keypress(win, XK_KP_Add, XCB_MOD_MASK_SHIFT);
4273 #define SWAPXY(g) do { \
4275 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
4276 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
4279 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
4281 struct swm_geometry win_g, r_g = *g;
4282 struct ws_win *win, *fs_win = NULL;
4283 int i, j, s, stacks;
4284 int w_inc = 1, h_inc, w_base = 1, h_base;
4285 int hrh, extra = 0, h_slice, last_h = 0;
4286 int split, colno, winno, mwin, msize, mscale;
4287 int remain, missing, v_slice, reconfigure = 0;
4290 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d, rot: %s, "
4291 "flip: %s\n", ws->idx, YESNO(rot), YESNO(flip));
4293 winno = count_win(ws, 0);
4294 if (winno == 0 && count_win(ws, 1) == 0)
4297 TAILQ_FOREACH(win, &ws->winlist, entry)
4298 if (!win->transient && !win->floating && !win->iconic)
4305 w_inc = win->sh.width_inc;
4306 w_base = win->sh.base_width;
4307 mwin = ws->l_state.horizontal_mwin;
4308 mscale = ws->l_state.horizontal_msize;
4309 stacks = ws->l_state.horizontal_stacks;
4312 w_inc = win->sh.height_inc;
4313 w_base = win->sh.base_height;
4314 mwin = ws->l_state.vertical_mwin;
4315 mscale = ws->l_state.vertical_msize;
4316 stacks = ws->l_state.vertical_stacks;
4320 if (stacks > winno - mwin)
4321 stacks = winno - mwin;
4325 h_slice = r_g.h / SWM_H_SLICE;
4326 if (mwin && winno > mwin) {
4327 v_slice = r_g.w / SWM_V_SLICE;
4331 win_g.w = v_slice * mscale;
4333 if (w_inc > 1 && w_inc < v_slice) {
4334 /* adjust for window's requested size increment */
4335 remain = (win_g.w - w_base) % w_inc;
4341 win_g.x += r_g.w - msize;
4344 colno = split = winno / stacks;
4345 win_g.w = ((r_g.w - (stacks * 2 * border_width) +
4346 2 * border_width) / stacks);
4348 hrh = r_g.h / colno;
4349 extra = r_g.h - (colno * hrh);
4350 win_g.h = hrh - 2 * border_width;
4352 /* stack all the tiled windows */
4353 i = j = 0, s = stacks;
4354 TAILQ_FOREACH(win, &ws->winlist, entry) {
4355 if (win->transient || win->floating || win->iconic)
4358 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
4363 if (split && i == split) {
4364 colno = (winno - mwin) / stacks;
4365 if (s <= (winno - mwin) % stacks)
4368 hrh = r_g.h / colno;
4369 extra = r_g.h - (colno * hrh);
4372 win_g.x += win_g.w + 2 * border_width +
4375 win_g.w = (r_g.w - msize -
4376 (stacks * (2 * border_width + tile_gap))) / stacks;
4378 win_g.w += (r_g.w - msize -
4379 (stacks * (2 * border_width + tile_gap))) %
4383 win_g.x -= win_g.w + 2 * border_width +
4389 win_g.h = hrh - 2 * border_width - tile_gap;
4392 h_inc = win->sh.width_inc;
4393 h_base = win->sh.base_width;
4395 h_inc = win->sh.height_inc;
4396 h_base = win->sh.base_height;
4399 if (j == colno - 1) {
4400 win_g.h = hrh + extra;
4401 } else if (h_inc > 1 && h_inc < h_slice) {
4402 /* adjust for window's requested size increment */
4403 remain = (win_g.h - h_base) % h_inc;
4404 missing = h_inc - remain;
4406 if (missing <= extra || j == 0) {
4418 win_g.y += last_h + 2 * border_width + tile_gap;
4420 if (disable_border && !(bar_enabled && ws->bar_enabled) &&
4423 win_g.w += 2 * border_width;
4424 win_g.h += 2 * border_width;
4430 if (X(win) != win_g.y || Y(win) != win_g.x ||
4431 WIDTH(win) != win_g.h || HEIGHT(win) != win_g.w) {
4435 WIDTH(win) = win_g.h;
4436 HEIGHT(win) = win_g.w;
4439 if (X(win) != win_g.x || Y(win) != win_g.y ||
4440 WIDTH(win) != win_g.w || HEIGHT(win) != win_g.h) {
4444 WIDTH(win) = win_g.w;
4445 HEIGHT(win) = win_g.h;
4449 if (bordered != win->bordered) {
4451 win->bordered = bordered;
4459 map_window(win, get_sibling(win, SWM_STACK_BELOW));
4466 /* Map/raise focused tiled window to top if windows could overlap. */
4467 if (tile_gap < 0 && ws->focus != NULL && !ws->focus->floating)
4468 map_window(ws->focus, get_sibling(ws->focus, SWM_STACK_TOP));
4471 /* now, stack all the floaters and transients */
4472 TAILQ_FOREACH(win, &ws->winlist, entry) {
4473 if (!win->transient && !win->floating)
4477 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
4482 stack_floater(win, ws->r);
4483 map_window(win, XCB_WINDOW_NONE);
4486 /* Make sure fs_win is stacked last so it's on top. */
4488 stack_floater(fs_win, ws->r);
4489 map_window(fs_win, XCB_WINDOW_NONE);
4494 vertical_config(struct workspace *ws, int id)
4496 DNPRINTF(SWM_D_STACK, "vertical_config: id: %d, workspace: %d\n",
4500 case SWM_ARG_ID_STACKRESET:
4501 case SWM_ARG_ID_STACKINIT:
4502 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
4503 ws->l_state.vertical_mwin = 1;
4504 ws->l_state.vertical_stacks = 1;
4506 case SWM_ARG_ID_MASTERSHRINK:
4507 if (ws->l_state.vertical_msize > 1)
4508 ws->l_state.vertical_msize--;
4510 case SWM_ARG_ID_MASTERGROW:
4511 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
4512 ws->l_state.vertical_msize++;
4514 case SWM_ARG_ID_MASTERADD:
4515 ws->l_state.vertical_mwin++;
4517 case SWM_ARG_ID_MASTERDEL:
4518 if (ws->l_state.vertical_mwin > 0)
4519 ws->l_state.vertical_mwin--;
4521 case SWM_ARG_ID_STACKINC:
4522 ws->l_state.vertical_stacks++;
4524 case SWM_ARG_ID_STACKDEC:
4525 if (ws->l_state.vertical_stacks > 1)
4526 ws->l_state.vertical_stacks--;
4528 case SWM_ARG_ID_FLIPLAYOUT:
4529 ws->l_state.vertical_flip = !ws->l_state.vertical_flip;
4537 vertical_stack(struct workspace *ws, struct swm_geometry *g)
4539 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
4541 stack_master(ws, g, 0, ws->l_state.vertical_flip);
4545 horizontal_config(struct workspace *ws, int id)
4547 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
4550 case SWM_ARG_ID_STACKRESET:
4551 case SWM_ARG_ID_STACKINIT:
4552 ws->l_state.horizontal_mwin = 1;
4553 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
4554 ws->l_state.horizontal_stacks = 1;
4556 case SWM_ARG_ID_MASTERSHRINK:
4557 if (ws->l_state.horizontal_msize > 1)
4558 ws->l_state.horizontal_msize--;
4560 case SWM_ARG_ID_MASTERGROW:
4561 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
4562 ws->l_state.horizontal_msize++;
4564 case SWM_ARG_ID_MASTERADD:
4565 ws->l_state.horizontal_mwin++;
4567 case SWM_ARG_ID_MASTERDEL:
4568 if (ws->l_state.horizontal_mwin > 0)
4569 ws->l_state.horizontal_mwin--;
4571 case SWM_ARG_ID_STACKINC:
4572 ws->l_state.horizontal_stacks++;
4574 case SWM_ARG_ID_STACKDEC:
4575 if (ws->l_state.horizontal_stacks > 1)
4576 ws->l_state.horizontal_stacks--;
4578 case SWM_ARG_ID_FLIPLAYOUT:
4579 ws->l_state.horizontal_flip = !ws->l_state.horizontal_flip;
4587 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
4589 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
4591 stack_master(ws, g, 1, ws->l_state.horizontal_flip);
4594 /* fullscreen view */
4596 max_stack(struct workspace *ws, struct swm_geometry *g)
4598 struct swm_geometry gg = *g;
4599 struct ws_win *w, *win = NULL, *parent = NULL;
4602 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
4607 winno = count_win(ws, 0);
4608 if (winno == 0 && count_win(ws, 1) == 0)
4611 /* Figure out which top level window should be visible. */
4612 if (ws->focus_pending)
4613 win = ws->focus_pending;
4616 else if (ws->focus_prev)
4617 win = ws->focus_prev;
4619 win = TAILQ_FIRST(&ws->winlist);
4622 parent = find_window(win->transient);
4624 DNPRINTF(SWM_D_STACK, "max_stack: win: 0x%x\n", win->id);
4626 /* maximize all top level windows */
4627 TAILQ_FOREACH(w, &ws->winlist, entry) {
4628 if (w->transient || w->iconic)
4631 if (!w->mapped && w != win)
4632 map_window(w, XCB_WINDOW_NONE);
4634 if (w->floating && !w->floatmaxed) {
4636 * retain geometry for retrieval on exit from
4639 store_float_geom(w);
4643 /* only reconfigure if necessary */
4644 if (X(w) != gg.x || Y(w) != gg.y || WIDTH(w) != gg.w ||
4645 HEIGHT(w) != gg.h) {
4647 if (bar_enabled && ws->bar_enabled){
4651 WIDTH(w) += 2 * border_width;
4652 HEIGHT(w) += 2 * border_width;
4659 /* If a parent exists, map/raise it first. */
4661 map_window(parent, XCB_WINDOW_NONE);
4663 /* Map siblings next. */
4664 TAILQ_FOREACH(w, &ws->winlist, entry)
4665 if (w != win && !w->iconic &&
4666 w->transient == parent->id) {
4667 stack_floater(w, ws->r);
4668 map_window(w, XCB_WINDOW_NONE);
4672 /* Map/raise focused window. */
4673 map_window(win, XCB_WINDOW_NONE);
4675 /* Finally, map/raise children of focus window. */
4676 TAILQ_FOREACH(w, &ws->winlist, entry)
4677 if (w->transient == win->id && !w->iconic) {
4678 stack_floater(w, ws->r);
4679 map_window(w, XCB_WINDOW_NONE);
4684 send_to_rg(struct swm_region *r, union arg *args)
4686 int ridx = args->id, i, num_screens;
4687 struct swm_region *rr = NULL;
4690 num_screens = get_screen_count();
4691 /* do nothing if we don't have more than one screen */
4692 if (!(num_screens > 1 || outputs > 1))
4695 DNPRINTF(SWM_D_FOCUS, "send_to_rg: id: %d\n", ridx);
4697 rr = TAILQ_FIRST(&r->s->rl);
4698 for (i = 0; i < ridx && rr != NULL; ++i)
4699 rr = TAILQ_NEXT(rr, entry);
4710 region_under(struct swm_screen *s, int x, int y)
4712 struct swm_region *r = NULL;
4714 TAILQ_FOREACH(r, &s->rl, entry) {
4715 DNPRINTF(SWM_D_MISC, "region_under: ws: %d, region g: (%d,%d) "
4716 "%d x %d, coords: (%d,%d)\n", r->ws->idx, X(r), Y(r),
4717 WIDTH(r), HEIGHT(r), x, y);
4718 if (X(r) <= x && x < MAX_X(r))
4719 if (Y(r) <= y && y < MAX_Y(r))
4727 send_to_ws(struct swm_region *r, union arg *args)
4729 int wsid = args->id;
4730 struct ws_win *win = NULL;
4732 if (r && r->ws && r->ws->focus)
4737 DNPRINTF(SWM_D_MOVE, "send_to_ws: win 0x%x, ws %d\n", win->id, wsid);
4739 win_to_ws(win, wsid, 1);
4741 /* Restack and set new focus. */
4744 if (focus_mode != SWM_FOCUS_FOLLOW) {
4745 if (r->ws->focus_pending) {
4746 focus_win(r->ws->focus_pending);
4747 r->ws->focus_pending = NULL;
4749 xcb_set_input_focus(conn,
4750 XCB_INPUT_FOCUS_PARENT, r->id,
4759 win_to_ws(struct ws_win *win, int wsid, int unfocus)
4761 struct ws_win *parent;
4762 struct workspace *ws, *nws, *pws;
4763 char ws_idx_str[SWM_PROPLEN];
4765 if (wsid >= workspace_limit)
4768 if (win->ws->idx == wsid)
4772 nws = &win->s->ws[wsid];
4774 DNPRINTF(SWM_D_MOVE, "win_to_ws: win 0x%x, ws %d -> %d\n", win->id,
4777 /* Update the window's workspace property: _SWM_WS */
4778 if (snprintf(ws_idx_str, SWM_PROPLEN, "%d", nws->idx) < SWM_PROPLEN) {
4779 if (focus_mode != SWM_FOCUS_FOLLOW)
4780 ws->focus_pending = get_focus_prev(win);
4782 /* Move the parent if this is a transient window. */
4783 if (win->transient) {
4784 parent = find_window(win->transient);
4787 /* Set new focus in parent's ws if needed. */
4788 if (pws->focus == parent) {
4789 if (focus_mode != SWM_FOCUS_FOLLOW)
4790 pws->focus_pending =
4791 get_focus_prev(parent);
4793 unfocus_win(parent);
4795 if (focus_mode != SWM_FOCUS_FOLLOW) {
4796 pws->focus = pws->focus_pending;
4797 pws->focus_pending = NULL;
4801 /* Don't unmap parent if new ws is visible */
4803 unmap_window(parent);
4806 TAILQ_REMOVE(&ws->winlist, parent, entry);
4807 TAILQ_INSERT_TAIL(&nws->winlist, parent, entry);
4810 DNPRINTF(SWM_D_PROP, "win_to_ws: set "
4811 "property: _SWM_WS: %s\n", ws_idx_str);
4812 xcb_change_property(conn, XCB_PROP_MODE_REPLACE,
4813 parent->id, a_swm_ws, XCB_ATOM_STRING, 8,
4814 strlen(ws_idx_str), ws_idx_str);
4821 /* Don't unmap if new ws is visible */
4826 TAILQ_REMOVE(&ws->winlist, win, entry);
4827 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
4830 /* Set focus on new ws. */
4831 unfocus_win(nws->focus);
4834 DNPRINTF(SWM_D_PROP, "win_to_ws: set property: _SWM_WS: %s\n",
4836 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
4837 a_swm_ws, XCB_ATOM_STRING, 8, strlen(ws_idx_str),
4841 DNPRINTF(SWM_D_MOVE, "win_to_ws: done.\n");
4845 pressbutton(struct swm_region *r, union arg *args)
4847 /* suppress unused warning since var is needed */
4850 xcb_test_fake_input(conn, XCB_BUTTON_PRESS, args->id,
4851 XCB_CURRENT_TIME, XCB_WINDOW_NONE, 0, 0, 0);
4852 xcb_test_fake_input(conn, XCB_BUTTON_RELEASE, args->id,
4853 XCB_CURRENT_TIME, XCB_WINDOW_NONE, 0, 0, 0);
4857 raise_toggle(struct swm_region *r, union arg *args)
4859 /* suppress unused warning since var is needed */
4862 if (r == NULL || r->ws == NULL)
4865 r->ws->always_raise = !r->ws->always_raise;
4867 /* bring floaters back to top */
4868 if (!r->ws->always_raise)
4875 iconify(struct swm_region *r, union arg *args)
4877 /* suppress unused warning since var is needed */
4880 if (r->ws->focus == NULL)
4883 set_swm_iconic(r->ws->focus, 1);
4889 get_win_name(xcb_window_t win)
4892 xcb_get_property_cookie_t c;
4893 xcb_get_property_reply_t *r;
4895 /* First try _NET_WM_NAME for UTF-8. */
4896 c = xcb_get_property(conn, 0, win, ewmh[_NET_WM_NAME].atom,
4897 XCB_GET_PROPERTY_TYPE_ANY, 0, UINT_MAX);
4898 r = xcb_get_property_reply(conn, c, NULL);
4901 if (r->type == XCB_NONE) {
4903 /* Use WM_NAME instead; no UTF-8. */
4904 c = xcb_get_property(conn, 0, win, XCB_ATOM_WM_NAME,
4905 XCB_GET_PROPERTY_TYPE_ANY, 0, UINT_MAX);
4906 r = xcb_get_property_reply(conn, c, NULL);
4910 if (r->type == XCB_NONE) {
4916 name = strndup(xcb_get_property_value(r),
4917 xcb_get_property_value_length(r));
4926 uniconify(struct swm_region *r, union arg *args)
4933 DNPRINTF(SWM_D_MISC, "uniconify\n");
4935 if (r == NULL || r->ws == NULL)
4938 /* make sure we have anything to uniconify */
4939 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
4940 if (win->ws == NULL)
4941 continue; /* should never happen */
4950 search_resp_action = SWM_SEARCH_UNICONIFY;
4952 spawn_select(r, args, "search", &searchpid);
4954 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
4957 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
4958 if (win->ws == NULL)
4959 continue; /* should never happen */
4963 name = get_win_name(win->id);
4966 fprintf(lfile, "%s.%u\n", name, win->id);
4974 name_workspace(struct swm_region *r, union arg *args)
4978 DNPRINTF(SWM_D_MISC, "name_workspace\n");
4984 search_resp_action = SWM_SEARCH_NAME_WORKSPACE;
4986 spawn_select(r, args, "name_workspace", &searchpid);
4988 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
4991 fprintf(lfile, "%s", "");
4996 search_workspace(struct swm_region *r, union arg *args)
4999 struct workspace *ws;
5002 DNPRINTF(SWM_D_MISC, "search_workspace\n");
5008 search_resp_action = SWM_SEARCH_SEARCH_WORKSPACE;
5010 spawn_select(r, args, "search", &searchpid);
5012 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
5015 for (i = 0; i < workspace_limit; i++) {
5019 fprintf(lfile, "%d%s%s\n", ws->idx + 1,
5020 (ws->name ? ":" : ""), (ws->name ? ws->name : ""));
5027 search_win_cleanup(void)
5029 struct search_window *sw = NULL;
5031 while ((sw = TAILQ_FIRST(&search_wl)) != NULL) {
5032 xcb_destroy_window(conn, sw->indicator);
5033 TAILQ_REMOVE(&search_wl, sw, entry);
5039 search_win(struct swm_region *r, union arg *args)
5041 struct ws_win *win = NULL;
5042 struct search_window *sw = NULL;
5045 int i, width, height;
5053 XRectangle l_ibox, l_lbox;
5055 DNPRINTF(SWM_D_MISC, "search_win\n");
5058 search_resp_action = SWM_SEARCH_SEARCH_WINDOW;
5060 spawn_select(r, args, "search", &searchpid);
5062 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
5065 TAILQ_INIT(&search_wl);
5068 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
5072 sw = calloc(1, sizeof(struct search_window));
5074 warn("search_win: calloc");
5076 search_win_cleanup();
5082 snprintf(s, sizeof s, "%d", i);
5085 w = xcb_generate_id(conn);
5086 wa[0] = r->s->c[SWM_S_COLOR_FOCUS].pixel;
5087 wa[1] = r->s->c[SWM_S_COLOR_UNFOCUS].pixel;
5089 if (bar_font_legacy) {
5090 XmbTextExtents(bar_fs, s, len, &l_ibox, &l_lbox);
5091 width = l_lbox.width + 4;
5092 height = bar_fs_extents->max_logical_extent.height + 4;
5094 XftTextExtentsUtf8(display, bar_font, (FcChar8 *)s, len,
5096 width = info.width + 4;
5097 height = bar_font->height + 4;
5100 xcb_create_window(conn, XCB_COPY_FROM_PARENT, w, win->id, 0, 0,
5101 width, height, 1, XCB_WINDOW_CLASS_INPUT_OUTPUT,
5102 XCB_COPY_FROM_PARENT, XCB_CW_BACK_PIXEL |
5103 XCB_CW_BORDER_PIXEL, wa);
5105 xcb_map_window(conn, w);
5108 TAILQ_INSERT_TAIL(&search_wl, sw, entry);
5110 if (bar_font_legacy) {
5111 l_gcv.graphics_exposures = 0;
5112 l_draw = XCreateGC(display, w, 0, &l_gcv);
5114 XSetForeground(display, l_draw,
5115 r->s->c[SWM_S_COLOR_BAR].pixel);
5117 DRAWSTRING(display, w, bar_fs, l_draw, 2,
5118 (bar_fs_extents->max_logical_extent.height -
5119 l_lbox.height) / 2 - l_lbox.y, s, len);
5121 XFreeGC(display, l_draw);
5124 draw = XftDrawCreate(display, w,
5125 DefaultVisual(display, r->s->idx),
5126 DefaultColormap(display, r->s->idx));
5128 XftDrawStringUtf8(draw, &bar_font_color, bar_font, 2,
5129 (HEIGHT(r->bar) + bar_font->height) / 2 -
5130 bar_font->descent, (FcChar8 *)s, len);
5132 XftDrawDestroy(draw);
5135 DNPRINTF(SWM_D_MISC, "search_win: mapped window: 0x%x\n", w);
5137 fprintf(lfile, "%d\n", i);
5147 search_resp_uniconify(const char *resp, size_t len)
5153 DNPRINTF(SWM_D_MISC, "search_resp_uniconify: resp: %s\n", resp);
5155 TAILQ_FOREACH(win, &search_r->ws->winlist, entry) {
5158 name = get_win_name(win->id);
5161 if (asprintf(&s, "%s.%u", name, win->id) == -1) {
5166 if (strncmp(s, resp, len) == 0) {
5167 /* XXX this should be a callback to generalize */
5168 set_swm_iconic(win, 0);
5177 search_resp_name_workspace(const char *resp, size_t len)
5179 struct workspace *ws;
5181 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: resp: %s\n", resp);
5183 if (search_r->ws == NULL)
5188 free(search_r->ws->name);
5189 search_r->ws->name = NULL;
5193 ws->name = strdup(resp);
5194 if (ws->name == NULL) {
5195 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: "
5196 "strdup: %s", strerror(errno));
5203 search_resp_search_workspace(const char *resp)
5210 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: resp: %s\n", resp);
5214 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: strdup: %s",
5221 ws_idx = (int)strtonum(q, 1, workspace_limit, &errstr);
5223 DNPRINTF(SWM_D_MISC, "workspace idx is %s: %s",
5230 switchws(search_r, &a);
5234 search_resp_search_window(const char *resp)
5239 struct search_window *sw;
5241 DNPRINTF(SWM_D_MISC, "search_resp_search_window: resp: %s\n", resp);
5245 DNPRINTF(SWM_D_MISC, "search_resp_search_window: strdup: %s",
5250 idx = (int)strtonum(s, 1, INT_MAX, &errstr);
5252 DNPRINTF(SWM_D_MISC, "window idx is %s: %s",
5259 TAILQ_FOREACH(sw, &search_wl, entry)
5260 if (idx == sw->idx) {
5266 #define MAX_RESP_LEN 1024
5269 search_do_resp(void)
5275 DNPRINTF(SWM_D_MISC, "search_do_resp:\n");
5280 if ((resp = calloc(1, MAX_RESP_LEN + 1)) == NULL) {
5281 warn("search: calloc");
5285 rbytes = read(select_resp_pipe[0], resp, MAX_RESP_LEN);
5287 warn("search: read error");
5290 resp[rbytes] = '\0';
5293 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
5294 * newline, so work around that by sanitizing the resp now.
5296 resp[strcspn(resp, "\n")] = '\0';
5299 switch (search_resp_action) {
5300 case SWM_SEARCH_UNICONIFY:
5301 search_resp_uniconify(resp, len);
5303 case SWM_SEARCH_NAME_WORKSPACE:
5304 search_resp_name_workspace(resp, len);
5306 case SWM_SEARCH_SEARCH_WORKSPACE:
5307 search_resp_search_workspace(resp);
5309 case SWM_SEARCH_SEARCH_WINDOW:
5310 search_resp_search_window(resp);
5315 if (search_resp_action == SWM_SEARCH_SEARCH_WINDOW)
5316 search_win_cleanup();
5318 search_resp_action = SWM_SEARCH_NONE;
5319 close(select_resp_pipe[0]);
5326 wkill(struct swm_region *r, union arg *args)
5328 DNPRINTF(SWM_D_MISC, "wkill: id: %d\n", args->id);
5330 if (r->ws->focus == NULL)
5333 if (args->id == SWM_ARG_ID_KILLWINDOW)
5334 xcb_kill_client(conn, r->ws->focus->id);
5336 if (r->ws->focus->can_delete)
5337 client_msg(r->ws->focus, a_delete, 0);
5343 floating_toggle_win(struct ws_win *win)
5351 /* reject floating toggles in max stack mode */
5352 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK])
5355 if (win->floating) {
5356 if (!win->floatmaxed) {
5357 /* retain position for refloat */
5358 store_float_geom(win);
5362 load_float_geom(win);
5366 ewmh_update_actions(win);
5372 floating_toggle(struct swm_region *r, union arg *args)
5374 struct ws_win *win = r->ws->focus;
5376 /* suppress unused warning since var is needed */
5382 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
5385 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
5386 _NET_WM_STATE_TOGGLE);
5390 if (win == win->ws->focus)
5397 region_containment(struct ws_win *win, struct swm_region *r, int opts)
5399 struct swm_geometry g = r->g;
5400 int rt, lt, tp, bm, bw;
5402 bw = (opts & SWM_CW_SOFTBOUNDARY) ? boundary_width : 0;
5405 * Perpendicular distance of each side of the window to the respective
5406 * side of the region boundary. Positive values indicate the side of
5407 * the window has passed beyond the region boundary.
5409 rt = opts & SWM_CW_RIGHT ? MAX_X(win) + BORDER(win) - MAX_X(r) : bw;
5410 lt = opts & SWM_CW_LEFT ? X(r) - X(win) + BORDER(win) : bw;
5411 bm = opts & SWM_CW_BOTTOM ? MAX_Y(win) + BORDER(win) - MAX_Y(r) : bw;
5412 tp = opts & SWM_CW_TOP ? Y(r) - Y(win) + BORDER(win) : bw;
5414 DNPRINTF(SWM_D_MISC, "region_containment: win 0x%x, rt: %d, lt: %d, "
5415 "bm: %d, tp: %d, SOFTBOUNDARY: %s, HARDBOUNDARY: %s\n", win->id, rt,
5416 lt, bm, tp, YESNO(opts & SWM_CW_SOFTBOUNDARY),
5417 YESNO(opts & SWM_CW_HARDBOUNDARY));
5420 * Disable containment if any of the flagged sides went beyond the
5421 * containment boundary, or if containment is disabled.
5423 if (!(opts & SWM_CW_HARDBOUNDARY || opts & SWM_CW_SOFTBOUNDARY) ||
5424 (bw != 0 && ((rt > bw) || (lt > bw) || (bm > bw) || (tp > bw)))) {
5425 /* Make sure window has at least 1 pixel in the region */
5426 g.x += 1 - WIDTH(win);
5427 g.y += 1 - HEIGHT(win);
5428 g.w += 2 * WIDTH(win) - 2;
5429 g.h += 2 * HEIGHT(win) - 2;
5432 constrain_window(win, &g, &opts);
5435 /* Move or resize a window so that flagged side(s) fit into the supplied box. */
5437 constrain_window(struct ws_win *win, struct swm_geometry *b, int *opts)
5439 DNPRINTF(SWM_D_MISC, "constrain_window: window: 0x%x, (x,y) w x h: "
5440 "(%d,%d) %d x %d, box: (x,y) w x h: (%d,%d) %d x %d, rt: %s, "
5441 "lt: %s, bt: %s, tp: %s, allow resize: %s\n", win->id, X(win),
5442 Y(win), WIDTH(win), HEIGHT(win), b->x, b->y, b->w, b->h,
5443 YESNO(*opts & SWM_CW_RIGHT), YESNO(*opts & SWM_CW_LEFT),
5444 YESNO(*opts & SWM_CW_BOTTOM), YESNO(*opts & SWM_CW_TOP),
5445 YESNO(*opts & SWM_CW_RESIZABLE));
5447 if ((*opts & SWM_CW_RIGHT) && MAX_X(win) + BORDER(win) > b->x + b->w) {
5448 if (*opts & SWM_CW_RESIZABLE)
5449 WIDTH(win) = b->x + b->w - X(win) - BORDER(win);
5451 X(win) = b->x + b->w - WIDTH(win) - BORDER(win);
5454 if ((*opts & SWM_CW_LEFT) && X(win) + BORDER(win) < b->x) {
5455 if (*opts & SWM_CW_RESIZABLE)
5456 WIDTH(win) -= b->x - X(win) - BORDER(win);
5458 X(win) = b->x - BORDER(win);
5461 if ((*opts & SWM_CW_BOTTOM) && MAX_Y(win) + BORDER(win) > b->y + b->h) {
5462 if (*opts & SWM_CW_RESIZABLE)
5463 HEIGHT(win) = b->y + b->h - Y(win) - BORDER(win);
5465 Y(win) = b->y + b->h - HEIGHT(win) - BORDER(win);
5468 if ((*opts & SWM_CW_TOP) && Y(win) + BORDER(win) < b->y) {
5469 if (*opts & SWM_CW_RESIZABLE)
5470 HEIGHT(win) -= b->y - Y(win) - BORDER(win);
5472 Y(win) = b->y - BORDER(win);
5475 if (*opts & SWM_CW_RESIZABLE) {
5478 if (HEIGHT(win) < 1)
5484 update_window(struct ws_win *win)
5489 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
5490 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
5491 XCB_CONFIG_WINDOW_BORDER_WIDTH;
5495 wc[3] = HEIGHT(win);
5496 wc[4] = BORDER(win);
5498 DNPRINTF(SWM_D_EVENT, "update_window: window: 0x%x, (x,y) w x h: "
5499 "(%d,%d) %d x %d, bordered: %s\n", win->id, wc[0], wc[1], wc[2],
5500 wc[3], YESNO(win->bordered));
5502 xcb_configure_window(conn, win->id, mask, wc);
5505 #define SWM_RESIZE_STEPS (50)
5508 resize(struct ws_win *win, union arg *args)
5510 xcb_timestamp_t timestamp = 0;
5511 struct swm_region *r = NULL;
5513 struct swm_geometry g;
5514 int top = 0, left = 0, resizing;
5516 xcb_cursor_t cursor;
5517 xcb_query_pointer_reply_t *xpr = NULL;
5518 xcb_generic_event_t *evt;
5519 xcb_motion_notify_event_t *mne;
5525 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
5528 DNPRINTF(SWM_D_EVENT, "resize: window: 0x%x, floating: %s, "
5529 "transient: 0x%x\n", win->id, YESNO(win->floating),
5532 if (!win->transient && !win->floating)
5535 /* reject resizes in max mode for floaters (transient ok) */
5536 if (win->floatmaxed)
5540 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
5547 /* It's possible for win to have been freed during focus_flush(). */
5548 if (validate_win(win)) {
5549 DNPRINTF(SWM_D_EVENT, "resize: invalid win.\n");
5554 case SWM_ARG_ID_WIDTHSHRINK:
5555 WIDTH(win) -= SWM_RESIZE_STEPS;
5558 case SWM_ARG_ID_WIDTHGROW:
5559 WIDTH(win) += SWM_RESIZE_STEPS;
5562 case SWM_ARG_ID_HEIGHTSHRINK:
5563 HEIGHT(win) -= SWM_RESIZE_STEPS;
5566 case SWM_ARG_ID_HEIGHTGROW:
5567 HEIGHT(win) += SWM_RESIZE_STEPS;
5574 region_containment(win, r, SWM_CW_ALLSIDES | SWM_CW_RESIZABLE |
5575 SWM_CW_HARDBOUNDARY);
5577 store_float_geom(win);
5581 region_containment(win, r, SWM_CW_ALLSIDES | SWM_CW_RESIZABLE |
5582 SWM_CW_SOFTBOUNDARY);
5585 /* get cursor offset from window root */
5586 xpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, win->id),
5593 if (xpr->win_x < WIDTH(win) / 2)
5596 if (xpr->win_y < HEIGHT(win) / 2)
5599 if (args->id == SWM_ARG_ID_CENTER)
5600 cursor = cursors[XC_SIZING].cid;
5602 cursor = cursors[left ? XC_TOP_LEFT_CORNER :
5603 XC_TOP_RIGHT_CORNER].cid;
5605 cursor = cursors[left ? XC_BOTTOM_LEFT_CORNER :
5606 XC_BOTTOM_RIGHT_CORNER].cid;
5608 xcb_grab_pointer(conn, 0, win->id, MOUSEMASK,
5609 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, XCB_WINDOW_NONE, cursor,
5614 while (resizing && (evt = xcb_wait_for_event(conn))) {
5615 switch (XCB_EVENT_RESPONSE_TYPE(evt)) {
5616 case XCB_BUTTON_RELEASE:
5617 DNPRINTF(SWM_D_EVENT, "resize: BUTTON_RELEASE\n");
5620 case XCB_MOTION_NOTIFY:
5621 mne = (xcb_motion_notify_event_t *)evt;
5622 /* cursor offset/delta from start of the operation */
5623 dx = mne->root_x - xpr->root_x;
5624 dy = mne->root_y - xpr->root_y;
5630 if (args->id == SWM_ARG_ID_CENTER) {
5631 if (g.h / 2 + dy < 1)
5635 HEIGHT(win) = g.h + 2 * dy;
5643 HEIGHT(win) = g.h + dy;
5650 if (args->id == SWM_ARG_ID_CENTER) {
5651 if (g.w / 2 + dx < 1)
5655 WIDTH(win) = g.w + 2 * dx;
5663 WIDTH(win) = g.w + dx;
5666 /* not free, don't sync more than 120 times / second */
5667 if ((mne->time - timestamp) > (1000 / 120) ) {
5668 timestamp = mne->time;
5669 regionize(win, mne->root_x, mne->root_y);
5670 region_containment(win, r, SWM_CW_ALLSIDES |
5671 SWM_CW_RESIZABLE | SWM_CW_HARDBOUNDARY |
5672 SWM_CW_SOFTBOUNDARY);
5680 /* It's possible for win to have been freed above. */
5681 if (validate_win(win)) {
5682 DNPRINTF(SWM_D_EVENT, "resize: invalid win.\n");
5690 region_containment(win, r, SWM_CW_ALLSIDES | SWM_CW_RESIZABLE |
5691 SWM_CW_HARDBOUNDARY | SWM_CW_SOFTBOUNDARY);
5695 store_float_geom(win);
5697 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
5699 DNPRINTF(SWM_D_EVENT, "resize: done.\n");
5703 resize_step(struct swm_region *r, union arg *args)
5705 struct ws_win *win = NULL;
5707 if (r && r->ws && r->ws->focus)
5716 /* Try to set window region based on supplied coordinates or window center. */
5718 regionize(struct ws_win *win, int x, int y)
5720 struct swm_region *r = NULL;
5722 r = region_under(win->s, x, y);
5724 r = region_under(win->s, X(win) + WIDTH(win) / 2,
5725 Y(win) + HEIGHT(win) / 2);
5727 if (r && r != win->ws->r) {
5728 win_to_ws(win, r->ws->idx, 0);
5733 #define SWM_MOVE_STEPS (50)
5736 move(struct ws_win *win, union arg *args)
5738 xcb_timestamp_t timestamp = 0;
5739 int move_stp = 0, moving;
5740 xcb_query_pointer_reply_t *qpr = NULL;
5741 xcb_generic_event_t *evt;
5742 xcb_motion_notify_event_t *mne;
5747 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
5750 DNPRINTF(SWM_D_EVENT, "move: window: 0x%x, floating: %s, transient: "
5751 "0x%x\n", win->id, YESNO(win->floating), win->transient);
5753 /* in max_stack mode should only move transients */
5754 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !win->transient)
5759 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
5763 /* When a stacked win is moved, float it and restack. */
5764 if (!win->floating && !win->transient) {
5765 store_float_geom(win);
5766 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
5773 /* It's possible for win to have been freed during focus_flush(). */
5774 if (validate_win(win)) {
5775 DNPRINTF(SWM_D_EVENT, "move: invalid win.\n");
5781 case SWM_ARG_ID_MOVELEFT:
5782 X(win) -= (SWM_MOVE_STEPS - border_width);
5785 case SWM_ARG_ID_MOVERIGHT:
5786 X(win) += (SWM_MOVE_STEPS - border_width);
5789 case SWM_ARG_ID_MOVEUP:
5790 Y(win) -= (SWM_MOVE_STEPS - border_width);
5793 case SWM_ARG_ID_MOVEDOWN:
5794 Y(win) += (SWM_MOVE_STEPS - border_width);
5801 regionize(win, -1, -1);
5802 region_containment(win, win->ws->r, SWM_CW_ALLSIDES);
5804 store_float_geom(win);
5808 xcb_grab_pointer(conn, 0, win->id, MOUSEMASK,
5809 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC,
5810 XCB_WINDOW_NONE, cursors[XC_FLEUR].cid, XCB_CURRENT_TIME);
5812 /* get cursor offset from window root */
5813 qpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, win->id),
5816 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
5820 regionize(win, qpr->root_x, qpr->root_y);
5821 region_containment(win, win->ws->r, SWM_CW_ALLSIDES |
5822 SWM_CW_SOFTBOUNDARY);
5826 while (moving && (evt = xcb_wait_for_event(conn))) {
5827 switch (XCB_EVENT_RESPONSE_TYPE(evt)) {
5828 case XCB_BUTTON_RELEASE:
5829 DNPRINTF(SWM_D_EVENT, "move: BUTTON_RELEASE\n");
5832 case XCB_MOTION_NOTIFY:
5833 mne = (xcb_motion_notify_event_t *)evt;
5834 DNPRINTF(SWM_D_EVENT, "motion: root: 0x%x\n", mne->root);
5835 X(win) = mne->root_x - qpr->win_x - border_width;
5836 Y(win) = mne->root_y - qpr->win_y - border_width;
5838 /* not free, don't sync more than 120 times / second */
5839 if ((mne->time - timestamp) > (1000 / 120) ) {
5840 timestamp = mne->time;
5841 regionize(win, mne->root_x, mne->root_y);
5842 region_containment(win, win->ws->r,
5843 SWM_CW_ALLSIDES | SWM_CW_SOFTBOUNDARY);
5851 /* It's possible for win to have been freed above. */
5852 if (validate_win(win)) {
5853 DNPRINTF(SWM_D_EVENT, "move: invalid win.\n");
5861 region_containment(win, win->ws->r, SWM_CW_ALLSIDES |
5862 SWM_CW_SOFTBOUNDARY);
5866 store_float_geom(win);
5869 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
5870 DNPRINTF(SWM_D_EVENT, "move: done.\n");
5874 move_step(struct swm_region *r, union arg *args)
5876 struct ws_win *win = NULL;
5878 if (r && r->ws && r->ws->focus)
5883 if (!win->transient && !win->floating)
5890 /* key definitions */
5892 char name[SWM_FUNCNAME_LEN];
5893 void (*func)(struct swm_region *r, union arg *);
5895 } keyfuncs[KF_INVALID + 1] = {
5896 /* name function argument */
5897 { "bar_toggle", bar_toggle, {.id = SWM_ARG_ID_BAR_TOGGLE} },
5898 { "bar_toggle_ws", bar_toggle, {.id = SWM_ARG_ID_BAR_TOGGLE_WS} },
5899 { "button2", pressbutton, {2} },
5900 { "cycle_layout", cycle_layout, {0} },
5901 { "flip_layout", stack_config, {.id = SWM_ARG_ID_FLIPLAYOUT} },
5902 { "float_toggle", floating_toggle,{0} },
5903 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
5904 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
5905 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
5906 { "focus_urgent", focus, {.id = SWM_ARG_ID_FOCUSURGENT} },
5907 { "height_grow", resize_step, {.id = SWM_ARG_ID_HEIGHTGROW} },
5908 { "height_shrink", resize_step, {.id = SWM_ARG_ID_HEIGHTSHRINK} },
5909 { "iconify", iconify, {0} },
5910 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
5911 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
5912 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
5913 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
5914 { "move_down", move_step, {.id = SWM_ARG_ID_MOVEDOWN} },
5915 { "move_left", move_step, {.id = SWM_ARG_ID_MOVELEFT} },
5916 { "move_right", move_step, {.id = SWM_ARG_ID_MOVERIGHT} },
5917 { "move_up", move_step, {.id = SWM_ARG_ID_MOVEUP} },
5918 { "mvrg_1", send_to_rg, {.id = 0} },
5919 { "mvrg_2", send_to_rg, {.id = 1} },
5920 { "mvrg_3", send_to_rg, {.id = 2} },
5921 { "mvrg_4", send_to_rg, {.id = 3} },
5922 { "mvrg_5", send_to_rg, {.id = 4} },
5923 { "mvrg_6", send_to_rg, {.id = 5} },
5924 { "mvrg_7", send_to_rg, {.id = 6} },
5925 { "mvrg_8", send_to_rg, {.id = 7} },
5926 { "mvrg_9", send_to_rg, {.id = 8} },
5927 { "mvws_1", send_to_ws, {.id = 0} },
5928 { "mvws_2", send_to_ws, {.id = 1} },
5929 { "mvws_3", send_to_ws, {.id = 2} },
5930 { "mvws_4", send_to_ws, {.id = 3} },
5931 { "mvws_5", send_to_ws, {.id = 4} },
5932 { "mvws_6", send_to_ws, {.id = 5} },
5933 { "mvws_7", send_to_ws, {.id = 6} },
5934 { "mvws_8", send_to_ws, {.id = 7} },
5935 { "mvws_9", send_to_ws, {.id = 8} },
5936 { "mvws_10", send_to_ws, {.id = 9} },
5937 { "mvws_11", send_to_ws, {.id = 10} },
5938 { "mvws_12", send_to_ws, {.id = 11} },
5939 { "mvws_13", send_to_ws, {.id = 12} },
5940 { "mvws_14", send_to_ws, {.id = 13} },
5941 { "mvws_15", send_to_ws, {.id = 14} },
5942 { "mvws_16", send_to_ws, {.id = 15} },
5943 { "mvws_17", send_to_ws, {.id = 16} },
5944 { "mvws_18", send_to_ws, {.id = 17} },
5945 { "mvws_19", send_to_ws, {.id = 18} },
5946 { "mvws_20", send_to_ws, {.id = 19} },
5947 { "mvws_21", send_to_ws, {.id = 20} },
5948 { "mvws_22", send_to_ws, {.id = 21} },
5949 { "name_workspace", name_workspace, {0} },
5950 { "quit", quit, {0} },
5951 { "raise_toggle", raise_toggle, {0} },
5952 { "restart", restart, {0} },
5953 { "rg_1", focusrg, {.id = 0} },
5954 { "rg_2", focusrg, {.id = 1} },
5955 { "rg_3", focusrg, {.id = 2} },
5956 { "rg_4", focusrg, {.id = 3} },
5957 { "rg_5", focusrg, {.id = 4} },
5958 { "rg_6", focusrg, {.id = 5} },
5959 { "rg_7", focusrg, {.id = 6} },
5960 { "rg_8", focusrg, {.id = 7} },
5961 { "rg_9", focusrg, {.id = 8} },
5962 { "rg_next", cyclerg, {.id = SWM_ARG_ID_CYCLERG_UP} },
5963 { "rg_prev", cyclerg, {.id = SWM_ARG_ID_CYCLERG_DOWN} },
5964 { "screen_next", cyclerg, {.id = SWM_ARG_ID_CYCLERG_UP} },
5965 { "screen_prev", cyclerg, {.id = SWM_ARG_ID_CYCLERG_DOWN} },
5966 { "search_win", search_win, {0} },
5967 { "search_workspace", search_workspace, {0} },
5968 { "spawn_custom", NULL, {0} },
5969 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
5970 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
5971 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
5972 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
5973 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
5974 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
5975 { "uniconify", uniconify, {0} },
5976 { "version", version, {0} },
5977 { "width_grow", resize_step, {.id = SWM_ARG_ID_WIDTHGROW} },
5978 { "width_shrink", resize_step, {.id = SWM_ARG_ID_WIDTHSHRINK} },
5979 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
5980 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
5981 { "ws_1", switchws, {.id = 0} },
5982 { "ws_2", switchws, {.id = 1} },
5983 { "ws_3", switchws, {.id = 2} },
5984 { "ws_4", switchws, {.id = 3} },
5985 { "ws_5", switchws, {.id = 4} },
5986 { "ws_6", switchws, {.id = 5} },
5987 { "ws_7", switchws, {.id = 6} },
5988 { "ws_8", switchws, {.id = 7} },
5989 { "ws_9", switchws, {.id = 8} },
5990 { "ws_10", switchws, {.id = 9} },
5991 { "ws_11", switchws, {.id = 10} },
5992 { "ws_12", switchws, {.id = 11} },
5993 { "ws_13", switchws, {.id = 12} },
5994 { "ws_14", switchws, {.id = 13} },
5995 { "ws_15", switchws, {.id = 14} },
5996 { "ws_16", switchws, {.id = 15} },
5997 { "ws_17", switchws, {.id = 16} },
5998 { "ws_18", switchws, {.id = 17} },
5999 { "ws_19", switchws, {.id = 18} },
6000 { "ws_20", switchws, {.id = 19} },
6001 { "ws_21", switchws, {.id = 20} },
6002 { "ws_22", switchws, {.id = 21} },
6003 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
6004 { "ws_next_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP_ALL} },
6005 { "ws_next_move", cyclews, {.id = SWM_ARG_ID_CYCLEWS_MOVE_UP} },
6006 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
6007 { "ws_prev_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN_ALL} },
6008 { "ws_prev_move", cyclews, {.id = SWM_ARG_ID_CYCLEWS_MOVE_DOWN} },
6009 { "ws_prior", priorws, {0} },
6010 { "dumpwins", dumpwins, {0} }, /* MUST BE LAST */
6011 { "invalid key func", NULL, {0} },
6015 key_cmp(struct key *kp1, struct key *kp2)
6017 if (kp1->keysym < kp2->keysym)
6019 if (kp1->keysym > kp2->keysym)
6022 if (kp1->mod < kp2->mod)
6024 if (kp1->mod > kp2->mod)
6031 enum { client_click, root_click };
6033 unsigned int action;
6035 unsigned int button;
6036 void (*func)(struct ws_win *, union arg *);
6039 #define MODKEY_SHIFT MODKEY | XCB_MOD_MASK_SHIFT
6040 /* action key mouse button func args */
6041 { client_click, MODKEY, XCB_BUTTON_INDEX_3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
6042 { client_click, MODKEY_SHIFT, XCB_BUTTON_INDEX_3, resize, {.id = SWM_ARG_ID_CENTER} },
6043 { client_click, MODKEY, XCB_BUTTON_INDEX_1, move, {0} },
6048 update_modkey(unsigned int mod)
6054 RB_FOREACH(kp, key_tree, &keys)
6055 if (kp->mod & XCB_MOD_MASK_SHIFT)
6056 kp->mod = mod | XCB_MOD_MASK_SHIFT;
6060 for (i = 0; i < LENGTH(buttons); i++)
6061 if (buttons[i].mask & XCB_MOD_MASK_SHIFT)
6062 buttons[i].mask = mod | XCB_MOD_MASK_SHIFT;
6064 buttons[i].mask = mod;
6068 spawn_expand(struct swm_region *r, union arg *args, const char *spawn_name,
6071 struct spawn_prog *prog = NULL;
6073 char *ap, **real_args;
6075 /* suppress unused warning since var is needed */
6078 DNPRINTF(SWM_D_SPAWN, "spawn_expand: %s\n", spawn_name);
6081 TAILQ_FOREACH(prog, &spawns, entry) {
6082 if (!strcasecmp(spawn_name, prog->name))
6086 warnx("spawn_custom: program %s not found", spawn_name);
6090 /* make room for expanded args */
6091 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
6092 err(1, "spawn_custom: calloc real_args");
6094 /* expand spawn_args into real_args */
6095 for (i = c = 0; i < prog->argc; i++) {
6097 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg: %s\n", ap);
6098 if (!strcasecmp(ap, "$bar_border")) {
6100 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
6102 err(1, "spawn_custom border color");
6103 } else if (!strcasecmp(ap, "$bar_color")) {
6105 strdup(r->s->c[SWM_S_COLOR_BAR].name))
6107 err(1, "spawn_custom bar color");
6108 } else if (!strcasecmp(ap, "$bar_font")) {
6109 if ((real_args[c] = strdup(bar_fonts))
6111 err(1, "spawn_custom bar fonts");
6112 } else if (!strcasecmp(ap, "$bar_font_color")) {
6114 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
6116 err(1, "spawn_custom color font");
6117 } else if (!strcasecmp(ap, "$color_focus")) {
6119 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
6121 err(1, "spawn_custom color focus");
6122 } else if (!strcasecmp(ap, "$color_unfocus")) {
6124 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
6126 err(1, "spawn_custom color unfocus");
6127 } else if (!strcasecmp(ap, "$region_index")) {
6128 if (asprintf(&real_args[c], "%d",
6129 get_region_index(r) + 1) < 1)
6130 err(1, "spawn_custom region index");
6131 } else if (!strcasecmp(ap, "$workspace_index")) {
6132 if (asprintf(&real_args[c], "%d", r->ws->idx + 1) < 1)
6133 err(1, "spawn_custom workspace index");
6134 } else if (!strcasecmp(ap, "$dmenu_bottom")) {
6137 if ((real_args[c] = strdup("-b")) == NULL)
6138 err(1, "spawn_custom workspace index");
6140 /* no match --> copy as is */
6141 if ((real_args[c] = strdup(ap)) == NULL)
6142 err(1, "spawn_custom strdup(ap)");
6144 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg: %s\n",
6150 DNPRINTF(SWM_D_SPAWN, "spawn_custom: result: ");
6151 for (i = 0; i < c; ++i)
6152 DPRINTF("\"%s\" ", real_args[i]);
6155 *ret_args = real_args;
6160 spawn_custom(struct swm_region *r, union arg *args, const char *spawn_name)
6166 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
6170 spawn(r->ws->idx, &a, 1);
6172 for (i = 0; i < spawn_argc; i++)
6178 spawn_select(struct swm_region *r, union arg *args, const char *spawn_name,
6185 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
6189 if (pipe(select_list_pipe) == -1)
6190 err(1, "pipe error");
6191 if (pipe(select_resp_pipe) == -1)
6192 err(1, "pipe error");
6194 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
6195 err(1, "could not disable SIGPIPE");
6196 switch (*pid = fork()) {
6198 err(1, "cannot fork");
6201 if (dup2(select_list_pipe[0], STDIN_FILENO) == -1)
6203 if (dup2(select_resp_pipe[1], STDOUT_FILENO) == -1)
6205 close(select_list_pipe[1]);
6206 close(select_resp_pipe[0]);
6207 spawn(r->ws->idx, &a, 0);
6209 default: /* parent */
6210 close(select_list_pipe[0]);
6211 close(select_resp_pipe[1]);
6215 for (i = 0; i < spawn_argc; i++)
6221 spawn_insert(const char *name, const char *args, int flags)
6223 char *arg, *cp, *ptr;
6224 struct spawn_prog *sp;
6226 DNPRINTF(SWM_D_SPAWN, "spawn_insert: %s\n", name);
6228 if ((sp = calloc(1, sizeof *sp)) == NULL)
6229 err(1, "spawn_insert: calloc");
6230 if ((sp->name = strdup(name)) == NULL)
6231 err(1, "spawn_insert: strdup");
6233 /* convert the arguments to an argument list */
6234 if ((ptr = cp = strdup(args)) == NULL)
6235 err(1, "spawn_insert: strdup");
6236 while ((arg = strsep(&ptr, " \t")) != NULL) {
6237 /* empty field; skip it */
6242 if ((sp->argv = realloc(sp->argv, sp->argc *
6243 sizeof *sp->argv)) == NULL)
6244 err(1, "spawn_insert: realloc");
6245 if ((sp->argv[sp->argc - 1] = strdup(arg)) == NULL)
6246 err(1, "spawn_insert: strdup");
6252 TAILQ_INSERT_TAIL(&spawns, sp, entry);
6253 DNPRINTF(SWM_D_SPAWN, "spawn_insert: leave\n");
6257 spawn_remove(struct spawn_prog *sp)
6261 DNPRINTF(SWM_D_SPAWN, "spawn_remove: %s\n", sp->name);
6263 TAILQ_REMOVE(&spawns, sp, entry);
6264 for (i = 0; i < sp->argc; i++)
6270 DNPRINTF(SWM_D_SPAWN, "spawn_remove: leave\n");
6274 spawn_find(const char *name)
6276 struct spawn_prog *sp;
6278 TAILQ_FOREACH(sp, &spawns, entry)
6279 if (!strcasecmp(sp->name, name))
6286 setspawn(const char *name, const char *args, int flags)
6288 struct spawn_prog *sp;
6290 DNPRINTF(SWM_D_SPAWN, "setspawn: %s\n", name);
6295 /* Remove any old spawn under the same name. */
6296 if ((sp = spawn_find(name)) != NULL)
6300 spawn_insert(name, args, flags);
6302 warnx("error: setspawn: cannot find program: %s", name);
6304 DNPRINTF(SWM_D_SPAWN, "setspawn: leave\n");
6308 setconfspawn(char *selector, char *value, int flags)
6312 args = expand_tilde(value);
6314 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, args);
6316 setspawn(selector, args, flags);
6319 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done.\n");
6324 validate_spawns(void)
6326 struct spawn_prog *sp;
6327 char which[PATH_MAX];
6332 RB_FOREACH(kp, key_tree, &keys) {
6333 if (kp->funcid != KF_SPAWN_CUSTOM)
6337 sp = spawn_find(kp->spawn_name);
6338 if (sp == NULL || sp->flags & SWM_SPAWN_OPTIONAL)
6341 /* verify we have the goods */
6342 snprintf(which, sizeof which, "which %s", sp->argv[0]);
6343 DNPRINTF(SWM_D_CONF, "validate_spawns: which %s\n",
6345 for (i = strlen("which "); i < strlen(which); i++)
6346 if (which[i] == ' ') {
6350 if (system(which) != 0)
6351 add_startup_exception("could not find %s",
6352 &which[strlen("which ")]);
6359 setconfspawn("lock", "xlock", 0);
6361 setconfspawn("term", "xterm", 0);
6362 setconfspawn("spawn_term", "xterm", 0);
6364 setconfspawn("menu", "dmenu_run"
6368 " -nf $bar_font_color"
6370 " -sf $bar_color", 0);
6372 setconfspawn("search", "dmenu"
6377 " -nf $bar_font_color"
6379 " -sf $bar_color", 0);
6381 setconfspawn("name_workspace", "dmenu"
6386 " -nf $bar_font_color"
6388 " -sf $bar_color", 0);
6390 /* These are not verified for existence, even with a binding set. */
6391 setconfspawn("screenshot_all", "screenshot.sh full", SWM_SPAWN_OPTIONAL);
6392 setconfspawn("screenshot_wind", "screenshot.sh window", SWM_SPAWN_OPTIONAL);
6393 setconfspawn("initscr", "initscreen.sh", SWM_SPAWN_OPTIONAL);
6397 #define SWM_MODNAME_SIZE 32
6398 #define SWM_KEY_WS "\n+ \t"
6400 parsekeys(char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
6404 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
6405 if (mod == NULL || ks == NULL) {
6406 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
6409 if (keystr == NULL || strlen(keystr) == 0) {
6410 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
6414 *ks = XCB_NO_SYMBOL;
6416 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
6417 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
6419 cp += (long)strspn(cp, SWM_KEY_WS);
6420 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
6422 else if (!strncasecmp(name, "Mod1", SWM_MODNAME_SIZE))
6423 *mod |= XCB_MOD_MASK_1;
6424 else if (!strncasecmp(name, "Mod2", SWM_MODNAME_SIZE))
6425 *mod += XCB_MOD_MASK_2;
6426 else if (!strncmp(name, "Mod3", SWM_MODNAME_SIZE))
6427 *mod |= XCB_MOD_MASK_3;
6428 else if (!strncmp(name, "Mod4", SWM_MODNAME_SIZE))
6429 *mod |= XCB_MOD_MASK_4;
6430 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
6431 *mod |= XCB_MOD_MASK_SHIFT;
6432 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
6433 *mod |= XCB_MOD_MASK_CONTROL;
6435 *ks = XStringToKeysym(name);
6436 XConvertCase(*ks, ks, &uks);
6437 if (ks == XCB_NO_SYMBOL) {
6439 "parsekeys: invalid key %s\n",
6445 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
6450 strdupsafe(const char *str)
6455 return (strdup(str));
6459 key_insert(unsigned int mod, KeySym ks, enum keyfuncid kfid,
6460 const char *spawn_name)
6464 DNPRINTF(SWM_D_KEY, "key_insert: enter %s [%s]\n",
6465 keyfuncs[kfid].name, spawn_name);
6467 if ((kp = malloc(sizeof *kp)) == NULL)
6468 err(1, "key_insert: malloc");
6473 kp->spawn_name = strdupsafe(spawn_name);
6474 RB_INSERT(key_tree, &keys, kp);
6476 DNPRINTF(SWM_D_KEY, "key_insert: leave\n");
6480 key_lookup(unsigned int mod, KeySym ks)
6487 return (RB_FIND(key_tree, &keys, &kp));
6491 key_remove(struct key *kp)
6493 DNPRINTF(SWM_D_KEY, "key_remove: %s\n", keyfuncs[kp->funcid].name);
6495 RB_REMOVE(key_tree, &keys, kp);
6496 free(kp->spawn_name);
6499 DNPRINTF(SWM_D_KEY, "key_remove: leave\n");
6503 key_replace(struct key *kp, unsigned int mod, KeySym ks, enum keyfuncid kfid,
6504 const char *spawn_name)
6506 DNPRINTF(SWM_D_KEY, "key_replace: %s [%s]\n", keyfuncs[kp->funcid].name,
6510 key_insert(mod, ks, kfid, spawn_name);
6512 DNPRINTF(SWM_D_KEY, "key_replace: leave\n");
6516 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid,
6517 const char *spawn_name)
6521 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
6522 keyfuncs[kfid].name, spawn_name);
6524 if ((kp = key_lookup(mod, ks)) != NULL) {
6525 if (kfid == KF_INVALID)
6528 key_replace(kp, mod, ks, kfid, spawn_name);
6529 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
6532 if (kfid == KF_INVALID) {
6533 warnx("bind: Key combination already unbound.");
6534 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
6538 key_insert(mod, ks, kfid, spawn_name);
6539 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
6543 setconfbinding(char *selector, char *value, int flags)
6545 enum keyfuncid kfid;
6548 struct spawn_prog *sp;
6550 /* suppress unused warning since var is needed */
6553 DNPRINTF(SWM_D_KEY, "setconfbinding: enter\n");
6554 if (selector == NULL) {
6555 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
6556 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
6558 setkeybinding(mod, ks, kfid, NULL);
6563 /* search by key function name */
6564 for (kfid = 0; kfid < KF_INVALID; (kfid)++) {
6565 if (strncasecmp(selector, keyfuncs[kfid].name,
6566 SWM_FUNCNAME_LEN) == 0) {
6567 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match "
6568 "keyfunc\n", selector);
6569 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
6570 setkeybinding(mod, ks, kfid, NULL);
6576 /* search by custom spawn name */
6577 if ((sp = spawn_find(selector)) != NULL) {
6578 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match "
6579 "spawn\n", selector);
6580 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
6581 setkeybinding(mod, ks, KF_SPAWN_CUSTOM,
6587 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
6594 #define MODKEY_SHIFT MODKEY | XCB_MOD_MASK_SHIFT
6595 setkeybinding(MODKEY, XK_b, KF_BAR_TOGGLE, NULL);
6596 setkeybinding(MODKEY_SHIFT, XK_b, KF_BAR_TOGGLE_WS,NULL);
6597 setkeybinding(MODKEY, XK_v, KF_BUTTON2, NULL);
6598 setkeybinding(MODKEY, XK_space, KF_CYCLE_LAYOUT,NULL);
6599 setkeybinding(MODKEY_SHIFT, XK_backslash, KF_FLIP_LAYOUT, NULL);
6600 setkeybinding(MODKEY, XK_t, KF_FLOAT_TOGGLE,NULL);
6601 setkeybinding(MODKEY, XK_m, KF_FOCUS_MAIN, NULL);
6602 setkeybinding(MODKEY, XK_j, KF_FOCUS_NEXT, NULL);
6603 setkeybinding(MODKEY, XK_Tab, KF_FOCUS_NEXT, NULL);
6604 setkeybinding(MODKEY, XK_k, KF_FOCUS_PREV, NULL);
6605 setkeybinding(MODKEY_SHIFT, XK_Tab, KF_FOCUS_PREV, NULL);
6606 setkeybinding(MODKEY, XK_u, KF_FOCUS_URGENT,NULL);
6607 setkeybinding(MODKEY_SHIFT, XK_equal, KF_HEIGHT_GROW,NULL);
6608 setkeybinding(MODKEY_SHIFT, XK_minus, KF_HEIGHT_SHRINK,NULL);
6609 setkeybinding(MODKEY, XK_w, KF_ICONIFY, NULL);
6610 setkeybinding(MODKEY, XK_h, KF_MASTER_SHRINK, NULL);
6611 setkeybinding(MODKEY, XK_l, KF_MASTER_GROW, NULL);
6612 setkeybinding(MODKEY, XK_comma, KF_MASTER_ADD, NULL);
6613 setkeybinding(MODKEY, XK_period, KF_MASTER_DEL, NULL);
6614 setkeybinding(MODKEY_SHIFT, XK_bracketright,KF_MOVE_DOWN,NULL);
6615 setkeybinding(MODKEY, XK_bracketleft, KF_MOVE_LEFT,NULL);
6616 setkeybinding(MODKEY, XK_bracketright,KF_MOVE_RIGHT,NULL);
6617 setkeybinding(MODKEY_SHIFT, XK_bracketleft, KF_MOVE_UP, NULL);
6618 setkeybinding(MODKEY_SHIFT, XK_KP_End, KF_MVRG_1, NULL);
6619 setkeybinding(MODKEY_SHIFT, XK_KP_Down, KF_MVRG_2, NULL);
6620 setkeybinding(MODKEY_SHIFT, XK_KP_Next, KF_MVRG_3, NULL);
6621 setkeybinding(MODKEY_SHIFT, XK_KP_Left, KF_MVRG_4, NULL);
6622 setkeybinding(MODKEY_SHIFT, XK_KP_Begin, KF_MVRG_5, NULL);
6623 setkeybinding(MODKEY_SHIFT, XK_KP_Right, KF_MVRG_6, NULL);
6624 setkeybinding(MODKEY_SHIFT, XK_KP_Home, KF_MVRG_7, NULL);
6625 setkeybinding(MODKEY_SHIFT, XK_KP_Up, KF_MVRG_8, NULL);
6626 setkeybinding(MODKEY_SHIFT, XK_KP_Prior, KF_MVRG_9, NULL);
6627 setkeybinding(MODKEY_SHIFT, XK_1, KF_MVWS_1, NULL);
6628 setkeybinding(MODKEY_SHIFT, XK_2, KF_MVWS_2, NULL);
6629 setkeybinding(MODKEY_SHIFT, XK_3, KF_MVWS_3, NULL);
6630 setkeybinding(MODKEY_SHIFT, XK_4, KF_MVWS_4, NULL);
6631 setkeybinding(MODKEY_SHIFT, XK_5, KF_MVWS_5, NULL);
6632 setkeybinding(MODKEY_SHIFT, XK_6, KF_MVWS_6, NULL);
6633 setkeybinding(MODKEY_SHIFT, XK_7, KF_MVWS_7, NULL);
6634 setkeybinding(MODKEY_SHIFT, XK_8, KF_MVWS_8, NULL);
6635 setkeybinding(MODKEY_SHIFT, XK_9, KF_MVWS_9, NULL);
6636 setkeybinding(MODKEY_SHIFT, XK_0, KF_MVWS_10, NULL);
6637 setkeybinding(MODKEY_SHIFT, XK_F1, KF_MVWS_11, NULL);
6638 setkeybinding(MODKEY_SHIFT, XK_F2, KF_MVWS_12, NULL);
6639 setkeybinding(MODKEY_SHIFT, XK_F3, KF_MVWS_13, NULL);
6640 setkeybinding(MODKEY_SHIFT, XK_F4, KF_MVWS_14, NULL);
6641 setkeybinding(MODKEY_SHIFT, XK_F5, KF_MVWS_15, NULL);
6642 setkeybinding(MODKEY_SHIFT, XK_F6, KF_MVWS_16, NULL);
6643 setkeybinding(MODKEY_SHIFT, XK_F7, KF_MVWS_17, NULL);
6644 setkeybinding(MODKEY_SHIFT, XK_F8, KF_MVWS_18, NULL);
6645 setkeybinding(MODKEY_SHIFT, XK_F9, KF_MVWS_19, NULL);
6646 setkeybinding(MODKEY_SHIFT, XK_F10, KF_MVWS_20, NULL);
6647 setkeybinding(MODKEY_SHIFT, XK_F11, KF_MVWS_21, NULL);
6648 setkeybinding(MODKEY_SHIFT, XK_F12, KF_MVWS_22, NULL);
6649 setkeybinding(MODKEY_SHIFT, XK_slash, KF_NAME_WORKSPACE,NULL);
6650 setkeybinding(MODKEY_SHIFT, XK_q, KF_QUIT, NULL);
6651 setkeybinding(MODKEY_SHIFT, XK_r, KF_RAISE_TOGGLE,NULL);
6652 setkeybinding(MODKEY, XK_q, KF_RESTART, NULL);
6653 setkeybinding(MODKEY, XK_KP_End, KF_RG_1, NULL);
6654 setkeybinding(MODKEY, XK_KP_Down, KF_RG_2, NULL);
6655 setkeybinding(MODKEY, XK_KP_Next, KF_RG_3, NULL);
6656 setkeybinding(MODKEY, XK_KP_Left, KF_RG_4, NULL);
6657 setkeybinding(MODKEY, XK_KP_Begin, KF_RG_5, NULL);
6658 setkeybinding(MODKEY, XK_KP_Right, KF_RG_6, NULL);
6659 setkeybinding(MODKEY, XK_KP_Home, KF_RG_7, NULL);
6660 setkeybinding(MODKEY, XK_KP_Up, KF_RG_8, NULL);
6661 setkeybinding(MODKEY, XK_KP_Prior, KF_RG_9, NULL);
6662 setkeybinding(MODKEY_SHIFT, XK_Right, KF_RG_NEXT, NULL);
6663 setkeybinding(MODKEY_SHIFT, XK_Left, KF_RG_PREV, NULL);
6664 setkeybinding(MODKEY, XK_f, KF_SEARCH_WIN, NULL);
6665 setkeybinding(MODKEY, XK_slash, KF_SEARCH_WORKSPACE,NULL);
6666 setkeybinding(MODKEY_SHIFT, XK_i, KF_SPAWN_CUSTOM,"initscr");
6667 setkeybinding(MODKEY_SHIFT, XK_Delete, KF_SPAWN_CUSTOM,"lock");
6668 setkeybinding(MODKEY, XK_p, KF_SPAWN_CUSTOM,"menu");
6669 setkeybinding(MODKEY, XK_s, KF_SPAWN_CUSTOM,"screenshot_all");
6670 setkeybinding(MODKEY_SHIFT, XK_s, KF_SPAWN_CUSTOM,"screenshot_wind");
6671 setkeybinding(MODKEY_SHIFT, XK_Return, KF_SPAWN_CUSTOM,"term");
6672 setkeybinding(MODKEY_SHIFT, XK_comma, KF_STACK_INC, NULL);
6673 setkeybinding(MODKEY_SHIFT, XK_period, KF_STACK_DEC, NULL);
6674 setkeybinding(MODKEY_SHIFT, XK_space, KF_STACK_RESET, NULL);
6675 setkeybinding(MODKEY, XK_Return, KF_SWAP_MAIN, NULL);
6676 setkeybinding(MODKEY_SHIFT, XK_j, KF_SWAP_NEXT, NULL);
6677 setkeybinding(MODKEY_SHIFT, XK_k, KF_SWAP_PREV, NULL);
6678 setkeybinding(MODKEY_SHIFT, XK_w, KF_UNICONIFY, NULL);
6679 setkeybinding(MODKEY_SHIFT, XK_v, KF_VERSION, NULL);
6680 setkeybinding(MODKEY, XK_equal, KF_WIDTH_GROW, NULL);
6681 setkeybinding(MODKEY, XK_minus, KF_WIDTH_SHRINK,NULL);
6682 setkeybinding(MODKEY, XK_x, KF_WIND_DEL, NULL);
6683 setkeybinding(MODKEY_SHIFT, XK_x, KF_WIND_KILL, NULL);
6684 setkeybinding(MODKEY, XK_1, KF_WS_1, NULL);
6685 setkeybinding(MODKEY, XK_2, KF_WS_2, NULL);
6686 setkeybinding(MODKEY, XK_3, KF_WS_3, NULL);
6687 setkeybinding(MODKEY, XK_4, KF_WS_4, NULL);
6688 setkeybinding(MODKEY, XK_5, KF_WS_5, NULL);
6689 setkeybinding(MODKEY, XK_6, KF_WS_6, NULL);
6690 setkeybinding(MODKEY, XK_7, KF_WS_7, NULL);
6691 setkeybinding(MODKEY, XK_8, KF_WS_8, NULL);
6692 setkeybinding(MODKEY, XK_9, KF_WS_9, NULL);
6693 setkeybinding(MODKEY, XK_0, KF_WS_10, NULL);
6694 setkeybinding(MODKEY, XK_F1, KF_WS_11, NULL);
6695 setkeybinding(MODKEY, XK_F2, KF_WS_12, NULL);
6696 setkeybinding(MODKEY, XK_F3, KF_WS_13, NULL);
6697 setkeybinding(MODKEY, XK_F4, KF_WS_14, NULL);
6698 setkeybinding(MODKEY, XK_F5, KF_WS_15, NULL);
6699 setkeybinding(MODKEY, XK_F6, KF_WS_16, NULL);
6700 setkeybinding(MODKEY, XK_F7, KF_WS_17, NULL);
6701 setkeybinding(MODKEY, XK_F8, KF_WS_18, NULL);
6702 setkeybinding(MODKEY, XK_F9, KF_WS_19, NULL);
6703 setkeybinding(MODKEY, XK_F10, KF_WS_20, NULL);
6704 setkeybinding(MODKEY, XK_F11, KF_WS_21, NULL);
6705 setkeybinding(MODKEY, XK_F12, KF_WS_22, NULL);
6706 setkeybinding(MODKEY, XK_Right, KF_WS_NEXT, NULL);
6707 setkeybinding(MODKEY, XK_Left, KF_WS_PREV, NULL);
6708 setkeybinding(MODKEY, XK_Up, KF_WS_NEXT_ALL, NULL);
6709 setkeybinding(MODKEY, XK_Down, KF_WS_PREV_ALL, NULL);
6710 setkeybinding(MODKEY_SHIFT, XK_Up, KF_WS_NEXT_MOVE,NULL);
6711 setkeybinding(MODKEY_SHIFT, XK_Down, KF_WS_PREV_MOVE,NULL);
6712 setkeybinding(MODKEY, XK_a, KF_WS_PRIOR, NULL);
6714 setkeybinding(MODKEY_SHIFT, XK_d, KF_DUMPWINS, NULL);
6724 while (RB_EMPTY(&keys) == 0) {
6725 kp = RB_ROOT(&keys);
6731 setkeymapping(char *selector, char *value, int flags)
6733 char *keymapping_file;
6735 /* suppress unused warnings since vars are needed */
6739 DNPRINTF(SWM_D_KEY, "setkeymapping: enter\n");
6741 keymapping_file = expand_tilde(value);
6744 /* load new key bindings; if it fails, revert to default bindings */
6745 if (conf_load(keymapping_file, SWM_CONF_KEYMAPPING)) {
6750 free(keymapping_file);
6752 DNPRINTF(SWM_D_KEY, "setkeymapping: leave\n");
6757 updatenumlockmask(void)
6760 xcb_get_modifier_mapping_reply_t *modmap_r;
6761 xcb_keycode_t *modmap, kc, *keycode;
6765 modmap_r = xcb_get_modifier_mapping_reply(conn,
6766 xcb_get_modifier_mapping(conn),
6769 modmap = xcb_get_modifier_mapping_keycodes(modmap_r);
6770 for (i = 0; i < 8; i++) {
6771 for (j = 0; j < modmap_r->keycodes_per_modifier; j++) {
6772 kc = modmap[i * modmap_r->keycodes_per_modifier
6774 keycode = xcb_key_symbols_get_keycode(syms,
6777 numlockmask = (1 << i);
6783 DNPRINTF(SWM_D_MISC, "updatenumlockmask: %d\n", numlockmask);
6790 int num_screens, k, j;
6791 unsigned int modifiers[4];
6792 xcb_keycode_t *code;
6794 DNPRINTF(SWM_D_MISC, "grabkeys\n");
6795 updatenumlockmask();
6798 modifiers[1] = numlockmask;
6799 modifiers[2] = XCB_MOD_MASK_LOCK;
6800 modifiers[3] = numlockmask | XCB_MOD_MASK_LOCK;
6802 num_screens = get_screen_count();
6803 for (k = 0; k < num_screens; k++) {
6804 if (TAILQ_EMPTY(&screens[k].rl))
6806 xcb_ungrab_key(conn, XCB_GRAB_ANY, screens[k].root,
6808 RB_FOREACH(kp, key_tree, &keys) {
6809 /* Skip unused ws binds. */
6810 if ((int)kp->funcid > KF_WS_1 + workspace_limit - 1 &&
6811 kp->funcid <= KF_WS_22)
6814 /* Skip unused mvws binds. */
6815 if ((int)kp->funcid > KF_MVWS_1 + workspace_limit - 1 &&
6816 kp->funcid <= KF_MVWS_22)
6819 if ((code = xcb_key_symbols_get_keycode(syms,
6821 for (j = 0; j < LENGTH(modifiers); j++)
6822 xcb_grab_key(conn, 1,
6824 kp->mod | modifiers[j],
6825 *code, XCB_GRAB_MODE_SYNC,
6826 XCB_GRAB_MODE_SYNC);
6834 grabbuttons(struct ws_win *win)
6836 unsigned int modifiers[4];
6839 DNPRINTF(SWM_D_MOUSE, "grabbuttons: win 0x%x\n", win->id);
6840 updatenumlockmask();
6843 modifiers[1] = numlockmask;
6844 modifiers[2] = XCB_MOD_MASK_LOCK;
6845 modifiers[3] = numlockmask | XCB_MOD_MASK_LOCK;
6847 for (i = 0; i < LENGTH(buttons); i++)
6848 if (buttons[i].action == client_click)
6849 for (j = 0; j < LENGTH(modifiers); ++j)
6850 xcb_grab_button(conn, 0, win->id, BUTTONMASK,
6851 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC,
6852 XCB_WINDOW_NONE, XCB_CURSOR_NONE,
6853 buttons[i].button, buttons[i].mask |
6857 const char *quirkname[] = {
6858 "NONE", /* config string for "no value" */
6866 "FOCUSONMAP_SINGLE",
6869 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
6870 #define SWM_Q_WS "\n|+ \t"
6872 parsequirks(char *qstr, unsigned long *quirk)
6882 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
6884 cp += (long)strspn(cp, SWM_Q_WS);
6885 for (i = 0; i < LENGTH(quirkname); i++) {
6886 if (!strncasecmp(name, quirkname[i], SWM_QUIRK_LEN)) {
6887 DNPRINTF(SWM_D_QUIRK,
6888 "parsequirks: %s\n", name);
6893 *quirk |= 1 << (i-1);
6897 if (i >= LENGTH(quirkname)) {
6898 DNPRINTF(SWM_D_QUIRK,
6899 "parsequirks: invalid quirk [%s]\n", name);
6907 quirk_insert(const char *class, const char *name, unsigned long quirk)
6911 DNPRINTF(SWM_D_QUIRK, "quirk_insert: %s:%s [%lu]\n", class, name,
6914 if ((qp = malloc(sizeof *qp)) == NULL)
6915 err(1, "quirk_insert: malloc");
6916 if ((qp->class = strdup(class)) == NULL)
6917 err(1, "quirk_insert: strdup");
6918 if ((qp->name = strdup(name)) == NULL)
6919 err(1, "quirk_insert: strdup");
6922 TAILQ_INSERT_TAIL(&quirks, qp, entry);
6924 DNPRINTF(SWM_D_QUIRK, "quirk_insert: leave\n");
6928 quirk_remove(struct quirk *qp)
6930 DNPRINTF(SWM_D_QUIRK, "quirk_remove: %s:%s [%lu]\n", qp->class,
6931 qp->name, qp->quirk);
6933 TAILQ_REMOVE(&quirks, qp, entry);
6938 DNPRINTF(SWM_D_QUIRK, "quirk_remove: leave\n");
6942 quirk_replace(struct quirk *qp, const char *class, const char *name,
6943 unsigned long quirk)
6945 DNPRINTF(SWM_D_QUIRK, "quirk_replace: %s:%s [%lu]\n", qp->class,
6946 qp->name, qp->quirk);
6949 quirk_insert(class, name, quirk);
6951 DNPRINTF(SWM_D_QUIRK, "quirk_replace: leave\n");
6955 setquirk(const char *class, const char *name, unsigned long quirk)
6959 DNPRINTF(SWM_D_QUIRK, "setquirk: enter %s:%s [%lu]\n", class, name,
6962 /* Remove/replace existing quirk. */
6963 TAILQ_FOREACH(qp, &quirks, entry) {
6964 if (!strcmp(qp->class, class) && !strcmp(qp->name, name)) {
6968 quirk_replace(qp, class, name, quirk);
6969 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
6974 /* Only insert if quirk is not NONE. */
6976 quirk_insert(class, name, quirk);
6978 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
6982 setconfquirk(char *selector, char *value, int flags)
6984 char *cp, *class, *name;
6988 /* suppress unused warning since var is needed */
6991 if (selector == NULL)
6993 if ((cp = strchr(selector, ':')) == NULL)
6998 if ((retval = parsequirks(value, &qrks)) == 0)
6999 setquirk(class, name, qrks);
7006 setquirk("MPlayer", "xv", SWM_Q_FLOAT | SWM_Q_FULLSCREEN | SWM_Q_FOCUSPREV);
7007 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT);
7008 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ);
7009 setquirk("Firefox", "Dialog", SWM_Q_FLOAT);
7010 setquirk("Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
7011 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ);
7012 setquirk("xine", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
7013 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
7014 setquirk("xine", "xine Panel", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
7015 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
7016 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
7017 setquirk("pcb", "pcb", SWM_Q_FLOAT);
7018 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
7021 /* conf file stuff */
7022 #define SWM_CONF_FILE "spectrwm.conf"
7023 #define SWM_CONF_FILE_OLD "scrotwm.conf"
7027 SWM_S_BAR_AT_BOTTOM,
7028 SWM_S_BAR_BORDER_WIDTH,
7031 SWM_S_BAR_ENABLED_WS,
7036 SWM_S_BOUNDARY_WIDTH,
7037 SWM_S_CLOCK_ENABLED,
7040 SWM_S_CYCLE_VISIBLE,
7042 SWM_S_DISABLE_BORDER,
7044 SWM_S_FOCUS_CLOSE_WRAP,
7045 SWM_S_FOCUS_DEFAULT,
7047 SWM_S_REGION_PADDING,
7052 SWM_S_STACK_ENABLED,
7055 SWM_S_TITLE_CLASS_ENABLED,
7056 SWM_S_TITLE_NAME_ENABLED,
7057 SWM_S_URGENT_ENABLED,
7058 SWM_S_VERBOSE_LAYOUT,
7059 SWM_S_WINDOW_NAME_ENABLED,
7060 SWM_S_WORKSPACE_LIMIT
7064 setconfvalue(char *selector, char *value, int flags)
7066 struct workspace *ws;
7067 int i, ws_id, num_screens;
7070 /* suppress unused warning since var is needed */
7074 case SWM_S_BAR_ACTION:
7076 if ((bar_argv[0] = expand_tilde(value)) == NULL)
7077 err(1, "setconfvalue: bar_action");
7079 case SWM_S_BAR_AT_BOTTOM:
7080 bar_at_bottom = atoi(value);
7082 case SWM_S_BAR_BORDER_WIDTH:
7083 bar_border_width = atoi(value);
7084 if (bar_border_width < 0)
7085 bar_border_width = 0;
7087 case SWM_S_BAR_DELAY:
7088 /* No longer needed; leave to not break old conf files. */
7090 case SWM_S_BAR_ENABLED:
7091 bar_enabled = atoi(value);
7093 case SWM_S_BAR_ENABLED_WS:
7094 ws_id = atoi(selector) - 1;
7095 if (ws_id < 0 || ws_id >= workspace_limit)
7096 errx(1, "setconfvalue: bar_enabled_ws: invalid "
7097 "workspace %d.", ws_id + 1);
7099 num_screens = get_screen_count();
7100 for (i = 0; i < num_screens; i++) {
7101 ws = (struct workspace *)&screens[i].ws;
7102 ws[ws_id].bar_enabled = atoi(value);
7105 case SWM_S_BAR_FONT:
7107 if (asprintf(&bar_fonts, "%s,%s", value, bar_fonts) == -1)
7108 err(1, "setconfvalue: asprintf: failed to allocate "
7109 "memory for bar_fonts.");
7112 /* If already in xft mode, then we are done. */
7113 if (!bar_font_legacy)
7116 /* If there are any non-XLFD entries, switch to Xft mode. */
7117 while ((b = strsep(&value, ",")) != NULL) {
7121 bar_font_legacy = 0;
7126 case SWM_S_BAR_FORMAT:
7128 if ((bar_format = strdup(value)) == NULL)
7129 err(1, "setconfvalue: bar_format");
7131 case SWM_S_BAR_JUSTIFY:
7132 if (!strcmp(value, "left"))
7133 bar_justify = SWM_BAR_JUSTIFY_LEFT;
7134 else if (!strcmp(value, "center"))
7135 bar_justify = SWM_BAR_JUSTIFY_CENTER;
7136 else if (!strcmp(value, "right"))
7137 bar_justify = SWM_BAR_JUSTIFY_RIGHT;
7139 errx(1, "invalid bar_justify");
7141 case SWM_S_BORDER_WIDTH:
7142 border_width = atoi(value);
7143 if (border_width < 0)
7146 case SWM_S_BOUNDARY_WIDTH:
7147 boundary_width = atoi(value);
7148 if (boundary_width < 0)
7151 case SWM_S_CLOCK_ENABLED:
7152 clock_enabled = atoi(value);
7154 case SWM_S_CLOCK_FORMAT:
7155 #ifndef SWM_DENY_CLOCK_FORMAT
7157 if ((clock_format = strdup(value)) == NULL)
7158 err(1, "setconfvalue: clock_format");
7161 case SWM_S_CYCLE_EMPTY:
7162 cycle_empty = atoi(value);
7164 case SWM_S_CYCLE_VISIBLE:
7165 cycle_visible = atoi(value);
7167 case SWM_S_DIALOG_RATIO:
7168 dialog_ratio = atof(value);
7169 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
7172 case SWM_S_DISABLE_BORDER:
7173 disable_border = atoi(value);
7175 case SWM_S_FOCUS_CLOSE:
7176 if (!strcmp(value, "first"))
7177 focus_close = SWM_STACK_BOTTOM;
7178 else if (!strcmp(value, "last"))
7179 focus_close = SWM_STACK_TOP;
7180 else if (!strcmp(value, "next"))
7181 focus_close = SWM_STACK_ABOVE;
7182 else if (!strcmp(value, "previous"))
7183 focus_close = SWM_STACK_BELOW;
7185 errx(1, "focus_close");
7187 case SWM_S_FOCUS_CLOSE_WRAP:
7188 focus_close_wrap = atoi(value);
7190 case SWM_S_FOCUS_DEFAULT:
7191 if (!strcmp(value, "last"))
7192 focus_default = SWM_STACK_TOP;
7193 else if (!strcmp(value, "first"))
7194 focus_default = SWM_STACK_BOTTOM;
7196 errx(1, "focus_default");
7198 case SWM_S_FOCUS_MODE:
7199 if (!strcmp(value, "default"))
7200 focus_mode = SWM_FOCUS_DEFAULT;
7201 else if (!strcmp(value, "follow") ||
7202 !strcmp(value, "follow_cursor"))
7203 focus_mode = SWM_FOCUS_FOLLOW;
7204 else if (!strcmp(value, "manual"))
7205 focus_mode = SWM_FOCUS_MANUAL;
7207 errx(1, "focus_mode");
7209 case SWM_S_REGION_PADDING:
7210 region_padding = atoi(value);
7211 if (region_padding < 0)
7214 case SWM_S_SPAWN_ORDER:
7215 if (!strcmp(value, "first"))
7216 spawn_position = SWM_STACK_BOTTOM;
7217 else if (!strcmp(value, "last"))
7218 spawn_position = SWM_STACK_TOP;
7219 else if (!strcmp(value, "next"))
7220 spawn_position = SWM_STACK_ABOVE;
7221 else if (!strcmp(value, "previous"))
7222 spawn_position = SWM_STACK_BELOW;
7224 errx(1, "spawn_position");
7226 case SWM_S_SPAWN_TERM:
7227 setconfspawn("term", value, 0);
7228 setconfspawn("spawn_term", value, 0);
7231 /* No longer needed; leave to not break old conf files. */
7233 case SWM_S_SS_ENABLED:
7234 /* No longer needed; leave to not break old conf files. */
7236 case SWM_S_STACK_ENABLED:
7237 stack_enabled = atoi(value);
7239 case SWM_S_TERM_WIDTH:
7240 term_width = atoi(value);
7244 case SWM_S_TILE_GAP:
7245 tile_gap = atoi(value);
7247 case SWM_S_TITLE_CLASS_ENABLED:
7248 title_class_enabled = atoi(value);
7250 case SWM_S_TITLE_NAME_ENABLED:
7251 title_name_enabled = atoi(value);
7253 case SWM_S_URGENT_ENABLED:
7254 urgent_enabled = atoi(value);
7256 case SWM_S_VERBOSE_LAYOUT:
7257 verbose_layout = atoi(value);
7258 for (i = 0; layouts[i].l_stack != NULL; i++) {
7260 layouts[i].l_string = fancy_stacker;
7262 layouts[i].l_string = plain_stacker;
7265 case SWM_S_WINDOW_NAME_ENABLED:
7266 window_name_enabled = atoi(value);
7268 case SWM_S_WORKSPACE_LIMIT:
7269 workspace_limit = atoi(value);
7270 if (workspace_limit > SWM_WS_MAX)
7271 workspace_limit = SWM_WS_MAX;
7272 else if (workspace_limit < 1)
7273 workspace_limit = 1;
7282 setconfmodkey(char *selector, char *value, int flags)
7284 /* suppress unused warnings since vars are needed */
7288 if (!strncasecmp(value, "Mod1", strlen("Mod1")))
7289 update_modkey(XCB_MOD_MASK_1);
7290 else if (!strncasecmp(value, "Mod2", strlen("Mod2")))
7291 update_modkey(XCB_MOD_MASK_2);
7292 else if (!strncasecmp(value, "Mod3", strlen("Mod3")))
7293 update_modkey(XCB_MOD_MASK_3);
7294 else if (!strncasecmp(value, "Mod4", strlen("Mod4")))
7295 update_modkey(XCB_MOD_MASK_4);
7302 setconfcolor(char *selector, char *value, int flags)
7304 setscreencolor(value, ((selector == NULL)?-1:atoi(selector)), flags);
7309 setconfregion(char *selector, char *value, int flags)
7311 /* suppress unused warnings since vars are needed */
7315 custom_region(value);
7320 setautorun(char *selector, char *value, int flags)
7330 /* suppress unused warnings since vars are needed */
7334 if (getenv("SWM_STARTED"))
7338 if (sscanf(value, "ws[%d]:%1023c", &ws_id, s) != 2)
7339 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
7341 if (ws_id < 0 || ws_id >= workspace_limit)
7342 errx(1, "autorun: invalid workspace %d", ws_id + 1);
7344 sp = expand_tilde((char *)&s);
7347 * This is a little intricate
7349 * If the pid already exists we simply reuse it because it means it was
7350 * used before AND not claimed by manage_window. We get away with
7351 * altering it in the parent after INSERT because this can not be a race
7354 while ((ap = strsep(&sp, " \t")) != NULL) {
7357 DNPRINTF(SWM_D_SPAWN, "setautorun: arg [%s]\n", ap);
7359 if ((a.argv = realloc(a.argv, argc * sizeof(char *))) == NULL)
7360 err(1, "setautorun: realloc");
7361 a.argv[argc - 1] = ap;
7365 if ((a.argv = realloc(a.argv, (argc + 1) * sizeof(char *))) == NULL)
7366 err(1, "setautorun: realloc");
7367 a.argv[argc] = NULL;
7369 if ((pid = fork()) == 0) {
7370 spawn(ws_id, &a, 1);
7379 p = calloc(1, sizeof *p);
7382 TAILQ_INSERT_TAIL(&pidlist, p, entry);
7392 setlayout(char *selector, char *value, int flags)
7394 int ws_id, i, x, mg, ma, si, ar, f = 0;
7395 int st = SWM_V_STACK, num_screens;
7397 struct workspace *ws;
7399 /* suppress unused warnings since vars are needed */
7403 if (getenv("SWM_STARTED"))
7407 if (sscanf(value, "ws[%d]:%d:%d:%d:%d:%1023c",
7408 &ws_id, &mg, &ma, &si, &ar, s) != 6)
7409 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
7410 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
7413 if (ws_id < 0 || ws_id >= workspace_limit)
7414 errx(1, "layout: invalid workspace %d", ws_id + 1);
7416 if (!strcasecmp(s, "vertical"))
7418 else if (!strcasecmp(s, "vertical_flip")) {
7421 } else if (!strcasecmp(s, "horizontal"))
7423 else if (!strcasecmp(s, "horizontal_flip")) {
7426 } else if (!strcasecmp(s, "fullscreen"))
7429 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
7430 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
7433 num_screens = get_screen_count();
7434 for (i = 0; i < num_screens; i++) {
7435 ws = (struct workspace *)&screens[i].ws;
7436 ws[ws_id].cur_layout = &layouts[st];
7438 ws[ws_id].always_raise = ar;
7439 if (st == SWM_MAX_STACK)
7443 for (x = 0; x < abs(mg); x++) {
7444 ws[ws_id].cur_layout->l_config(&ws[ws_id],
7445 mg >= 0 ? SWM_ARG_ID_MASTERGROW :
7446 SWM_ARG_ID_MASTERSHRINK);
7450 for (x = 0; x < abs(ma); x++) {
7451 ws[ws_id].cur_layout->l_config(&ws[ws_id],
7452 ma >= 0 ? SWM_ARG_ID_MASTERADD :
7453 SWM_ARG_ID_MASTERDEL);
7457 for (x = 0; x < abs(si); x++) {
7458 ws[ws_id].cur_layout->l_config(&ws[ws_id],
7459 si >= 0 ? SWM_ARG_ID_STACKINC :
7460 SWM_ARG_ID_STACKDEC);
7465 ws[ws_id].cur_layout->l_config(&ws[ws_id],
7466 SWM_ARG_ID_FLIPLAYOUT);
7476 /* config options */
7477 struct config_option {
7479 int (*func)(char*, char*, int);
7482 struct config_option configopt[] = {
7483 { "autorun", setautorun, 0 },
7484 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
7485 { "bar_at_bottom", setconfvalue, SWM_S_BAR_AT_BOTTOM },
7486 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
7487 { "bar_border_unfocus", setconfcolor, SWM_S_COLOR_BAR_BORDER_UNFOCUS },
7488 { "bar_border_width", setconfvalue, SWM_S_BAR_BORDER_WIDTH },
7489 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
7490 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
7491 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
7492 { "bar_enabled_ws", setconfvalue, SWM_S_BAR_ENABLED_WS },
7493 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
7494 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
7495 { "bar_format", setconfvalue, SWM_S_BAR_FORMAT },
7496 { "bar_justify", setconfvalue, SWM_S_BAR_JUSTIFY },
7497 { "bind", setconfbinding, 0 },
7498 { "border_width", setconfvalue, SWM_S_BORDER_WIDTH },
7499 { "boundary_width", setconfvalue, SWM_S_BOUNDARY_WIDTH },
7500 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
7501 { "clock_format", setconfvalue, SWM_S_CLOCK_FORMAT },
7502 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
7503 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
7504 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
7505 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
7506 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
7507 { "disable_border", setconfvalue, SWM_S_DISABLE_BORDER },
7508 { "focus_close", setconfvalue, SWM_S_FOCUS_CLOSE },
7509 { "focus_close_wrap", setconfvalue, SWM_S_FOCUS_CLOSE_WRAP },
7510 { "focus_default", setconfvalue, SWM_S_FOCUS_DEFAULT },
7511 { "focus_mode", setconfvalue, SWM_S_FOCUS_MODE },
7512 { "keyboard_mapping", setkeymapping, 0 },
7513 { "layout", setlayout, 0 },
7514 { "modkey", setconfmodkey, 0 },
7515 { "program", setconfspawn, 0 },
7516 { "quirk", setconfquirk, 0 },
7517 { "region", setconfregion, 0 },
7518 { "region_padding", setconfvalue, SWM_S_REGION_PADDING },
7519 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
7520 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
7521 { "spawn_position", setconfvalue, SWM_S_SPAWN_ORDER },
7522 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
7523 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
7524 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
7525 { "tile_gap", setconfvalue, SWM_S_TILE_GAP },
7526 { "title_class_enabled", setconfvalue, SWM_S_TITLE_CLASS_ENABLED },
7527 { "title_name_enabled", setconfvalue, SWM_S_TITLE_NAME_ENABLED },
7528 { "urgent_enabled", setconfvalue, SWM_S_URGENT_ENABLED },
7529 { "verbose_layout", setconfvalue, SWM_S_VERBOSE_LAYOUT },
7530 { "window_name_enabled", setconfvalue, SWM_S_WINDOW_NAME_ENABLED },
7531 { "workspace_limit", setconfvalue, SWM_S_WORKSPACE_LIMIT },
7535 _add_startup_exception(const char *fmt, va_list ap)
7537 if (vasprintf(&startup_exception, fmt, ap) == -1)
7538 warn("%s: asprintf", __func__);
7542 add_startup_exception(const char *fmt, ...)
7548 if (startup_exception)
7551 /* force bar to be enabled due to exception */
7555 _add_startup_exception(fmt, ap);
7560 conf_load(const char *filename, int keymapping)
7563 char *line = NULL, *cp, *optsub, *optval = NULL;
7564 size_t linelen, lineno = 0;
7565 int wordlen, i, optidx;
7566 struct config_option *opt = NULL;
7568 DNPRINTF(SWM_D_CONF, "conf_load: begin\n");
7570 if (filename == NULL) {
7571 warnx("conf_load: no filename");
7575 DNPRINTF(SWM_D_CONF, "conf_load: open %s\n", filename);
7577 if ((config = fopen(filename, "r")) == NULL) {
7578 warn("conf_load: fopen: %s", filename);
7582 while (!feof(config)) {
7586 if ((line = fparseln(config, &linelen, &lineno, NULL,
7587 FPARSELN_UNESCCOMM | FPARSELN_UNESCCONT)) == NULL) {
7589 err(1, "%s", filename);
7594 cp += strspn(cp, " \t\n"); /* eat whitespace */
7595 if (cp[0] == '\0') {
7599 /* get config option */
7600 wordlen = strcspn(cp, "=[ \t\n");
7602 add_startup_exception("%s: line %zd: no option found",
7607 for (i = 0; i < LENGTH(configopt); i++) {
7608 opt = &configopt[i];
7609 if (!strncasecmp(cp, opt->optname, wordlen) &&
7610 (int)strlen(opt->optname) == wordlen) {
7616 add_startup_exception("%s: line %zd: unknown option "
7617 "%.*s", filename, lineno, wordlen, cp);
7620 if (keymapping && opt && strcmp(opt->optname, "bind")) {
7621 add_startup_exception("%s: line %zd: invalid option "
7622 "%.*s", filename, lineno, wordlen, cp);
7626 cp += strspn(cp, " \t\n"); /* eat whitespace */
7628 /* from here on out we call goto invalid to continue */
7630 /* get [selector] if any */
7634 wordlen = strcspn(cp, "]");
7637 add_startup_exception("%s: line %zd: "
7638 "syntax error", filename, lineno);
7642 if (asprintf(&optsub, "%.*s", wordlen, cp) ==
7644 add_startup_exception("%s: line %zd: "
7645 "unable to allocatememory for "
7646 "selector", filename, lineno);
7651 cp += strspn(cp, "] \t\n"); /* eat trailing */
7653 cp += strspn(cp, "= \t\n"); /* eat trailing */
7655 optval = strdup(cp);
7656 if (strlen(optval) == 0) {
7657 add_startup_exception("%s: line %zd: must supply value "
7658 "to %s", filename, lineno,
7659 configopt[optidx].optname);
7662 /* call function to deal with it all */
7663 if (configopt[optidx].func(optsub, optval,
7664 configopt[optidx].funcflags) != 0) {
7665 add_startup_exception("%s: line %zd: invalid data for "
7666 "%s", filename, lineno, configopt[optidx].optname);
7683 DNPRINTF(SWM_D_CONF, "conf_load: end\n");
7689 set_child_transient(struct ws_win *win, xcb_window_t *trans)
7691 struct ws_win *parent, *w;
7692 struct swm_region *r;
7693 struct workspace *ws;
7694 xcb_icccm_wm_hints_t wmh;
7696 parent = find_window(win->transient);
7698 parent->focus_child = win;
7700 DNPRINTF(SWM_D_MISC, "set_child_transient: parent doesn't exist"
7701 " for 0x%x trans 0x%x\n", win->id, win->transient);
7703 r = root_to_region(win->wa->root, SWM_CK_ALL);
7705 /* parent doen't exist in our window list */
7706 TAILQ_FOREACH(w, &ws->winlist, entry) {
7707 if (xcb_icccm_get_wm_hints_reply(conn,
7708 xcb_icccm_get_wm_hints(conn, w->id),
7710 warnx("can't get hints for 0x%x", w->id);
7714 if (win->hints.window_group != wmh.window_group)
7717 w->focus_child = win;
7718 win->transient = w->id;
7720 DNPRINTF(SWM_D_MISC, "set_child_transient: adjusting "
7721 "transient to 0x%x\n", win->transient);
7728 window_get_pid(xcb_window_t win)
7733 xcb_get_property_cookie_t pc;
7734 xcb_get_property_reply_t *pr;
7736 apid = get_atom_from_string("_NET_WM_PID");
7737 if (apid == XCB_ATOM_NONE)
7740 pc = xcb_get_property(conn, 0, win, apid, XCB_ATOM_CARDINAL, 0, 1);
7741 pr = xcb_get_property_reply(conn, pc, NULL);
7744 if (pr->type != XCB_ATOM_CARDINAL) {
7749 if (pr->type == apid && pr->format == 32)
7750 ret = *((pid_t *)xcb_get_property_value(pr));
7756 apid = get_atom_from_string("_SWM_PID");
7757 pc = xcb_get_property(conn, 0, win, apid, XCB_ATOM_STRING,
7759 pr = xcb_get_property_reply(conn, pc, NULL);
7762 if (pr->type != apid) {
7767 ret = (pid_t)strtonum(xcb_get_property_value(pr), 0, INT_MAX, &errstr);
7774 get_ws_idx(xcb_window_t id)
7780 xcb_get_property_reply_t *gpr;
7782 gpr = xcb_get_property_reply(conn,
7783 xcb_get_property(conn, 0, id, a_swm_ws,
7784 XCB_ATOM_STRING, 0, SWM_PROPLEN),
7789 proplen = xcb_get_property_value_length(gpr);
7791 prop = malloc(proplen + 1);
7794 xcb_get_property_value(gpr),
7796 prop[proplen] = '\0';
7803 DNPRINTF(SWM_D_PROP, "get_ws_idx: _SWM_WS: %s\n", prop);
7804 ws_idx = (int)strtonum(prop, 0, workspace_limit - 1, &errstr);
7806 DNPRINTF(SWM_D_PROP, "get_ws_idx: window: #%s: %s",
7816 manage_window(xcb_window_t id, uint16_t mapped)
7818 xcb_window_t trans = XCB_WINDOW_NONE;
7819 struct ws_win *win, *ww;
7821 char ws_idx_str[SWM_PROPLEN];
7822 struct swm_region *r;
7826 xcb_icccm_get_wm_protocols_reply_t wpr;
7828 if ((win = find_window(id)) != NULL) {
7829 DNPRINTF(SWM_D_MISC, "manage_window: win 0x%x already "
7830 "managed; skipping.)\n", id);
7831 return (win); /* Already managed. */
7834 /* See if window is on the unmanaged list. */
7835 if ((win = find_unmanaged_window(id)) != NULL) {
7836 DNPRINTF(SWM_D_MISC, "manage_window: win 0x%x found on "
7837 "unmanaged list.\n", id);
7838 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
7841 set_child_transient(win, &trans);
7845 DNPRINTF(SWM_D_MISC, "manage_window: win 0x%x is new.\n", id);
7848 /* Create and initialize ws_win object. */
7849 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
7850 err(1, "manage_window: calloc: failed to allocate memory for "
7855 /* Get window geometry. */
7856 win->wa = xcb_get_geometry_reply(conn,
7857 xcb_get_geometry(conn, win->id),
7860 /* Figure out which region the window belongs to. */
7861 r = root_to_region(win->wa->root, SWM_CK_ALL);
7863 /* Ignore window border if there is one. */
7864 WIDTH(win) = win->wa->width;
7865 HEIGHT(win) = win->wa->height;
7866 X(win) = win->wa->x + win->wa->border_width - border_width;
7867 Y(win) = win->wa->y + win->wa->border_width - border_width;
7869 win->mapped = mapped;
7870 win->floatmaxed = 0;
7871 win->ewmh_flags = 0;
7872 win->s = r->s; /* this never changes */
7874 /* Get WM_SIZE_HINTS. */
7875 xcb_icccm_get_wm_normal_hints_reply(conn,
7876 xcb_icccm_get_wm_normal_hints(conn, win->id),
7880 xcb_icccm_get_wm_hints_reply(conn,
7881 xcb_icccm_get_wm_hints(conn, win->id),
7884 /* Get WM_TRANSIENT_FOR; see if window is a transient. */
7885 xcb_icccm_get_wm_transient_for_reply(conn,
7886 xcb_icccm_get_wm_transient_for(conn, win->id),
7889 win->transient = trans;
7890 set_child_transient(win, &win->transient);
7893 /* Get supported protocols. */
7894 if (xcb_icccm_get_wm_protocols_reply(conn,
7895 xcb_icccm_get_wm_protocols(conn, win->id, a_prot),
7897 for (i = 0; i < wpr.atoms_len; i++) {
7898 if (wpr.atoms[i] == a_takefocus)
7899 win->take_focus = 1;
7900 if (wpr.atoms[i] == a_delete)
7901 win->can_delete = 1;
7903 xcb_icccm_get_wm_protocols_reply_wipe(&wpr);
7906 win->iconic = get_swm_iconic(win);
7908 /* Figure out which workspace the window belongs to. */
7909 if ((p = find_pid(window_get_pid(win->id))) != NULL) {
7910 win->ws = &r->s->ws[p->ws];
7911 TAILQ_REMOVE(&pidlist, p, entry);
7914 } else if ((ws_idx = get_ws_idx(win->id)) != -1 &&
7916 /* _SWM_WS is set; use that. */
7917 win->ws = &r->s->ws[ws_idx];
7918 } else if (trans && (ww = find_window(trans)) != NULL) {
7919 /* Launch transients in the same ws as parent. */
7925 /* Set the _SWM_WS atom so we can remember this after reincarnation. */
7926 if (snprintf(ws_idx_str, SWM_PROPLEN, "%d", win->ws->idx) <
7928 DNPRINTF(SWM_D_PROP, "manage_window: set _SWM_WS: %s\n",
7930 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
7931 a_swm_ws, XCB_ATOM_STRING, 8, strlen(ws_idx_str),
7935 /* WS must already be set for this to work. */
7936 store_float_geom(win);
7939 ewmh_autoquirk(win);
7941 /* Determine initial quirks. */
7942 if (xcb_icccm_get_wm_class_reply(conn,
7943 xcb_icccm_get_wm_class(conn, win->id),
7945 DNPRINTF(SWM_D_CLASS, "manage_window: class: %s, name: %s\n",
7946 win->ch.class_name, win->ch.instance_name);
7948 /* java is retarded so treat it special */
7949 if (strstr(win->ch.instance_name, "sun-awt")) {
7950 DNPRINTF(SWM_D_CLASS, "manage_window: java window "
7955 TAILQ_FOREACH(qp, &quirks, entry) {
7956 if (!strcmp(win->ch.class_name, qp->class) &&
7957 !strcmp(win->ch.instance_name, qp->name)) {
7958 DNPRINTF(SWM_D_CLASS, "manage_window: on quirks"
7959 "list; mask: 0x%lx\n", qp->quirk);
7960 if (qp->quirk & SWM_Q_FLOAT)
7962 win->quirks = qp->quirk;
7967 /* Alter window position if quirky */
7968 if (win->quirks & SWM_Q_ANYWHERE)
7971 /* Reset font sizes (the bruteforce way; no default keybinding). */
7972 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
7973 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
7974 fake_keypress(win, XK_KP_Subtract, XCB_MOD_MASK_SHIFT);
7975 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
7976 fake_keypress(win, XK_KP_Add, XCB_MOD_MASK_SHIFT);
7979 /* Make sure window is positioned inside its region, if its active. */
7981 region_containment(win, r, SWM_CW_ALLSIDES |
7982 SWM_CW_HARDBOUNDARY);
7986 /* Select which X events to monitor and set border pixel color. */
7987 wa[0] = win->s->c[SWM_S_COLOR_UNFOCUS].pixel;
7988 wa[1] = XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_PROPERTY_CHANGE |
7989 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
7991 wa[1] |= XCB_EVENT_MASK_LEAVE_WINDOW | XCB_EVENT_MASK_FOCUS_CHANGE;
7994 xcb_change_window_attributes(conn, win->id, XCB_CW_BORDER_PIXEL |
7995 XCB_CW_EVENT_MASK, wa);
7998 /* Figure out where to stack the window in the workspace. */
7999 if (trans && (ww = find_window(trans)))
8000 TAILQ_INSERT_AFTER(&win->ws->winlist, ww, win, entry);
8001 else if (win->ws->focus && spawn_position == SWM_STACK_ABOVE)
8002 TAILQ_INSERT_AFTER(&win->ws->winlist, win->ws->focus, win,
8004 else if (win->ws->focus && spawn_position == SWM_STACK_BELOW)
8005 TAILQ_INSERT_BEFORE(win->ws->focus, win, entry);
8006 else switch (spawn_position) {
8009 case SWM_STACK_ABOVE:
8010 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
8012 case SWM_STACK_BOTTOM:
8013 case SWM_STACK_BELOW:
8014 TAILQ_INSERT_HEAD(&win->ws->winlist, win, entry);
8017 /* Get initial _NET_WM_STATE */
8018 ewmh_get_win_state(win);
8019 /* Set initial _NET_WM_ALLOWED_ACTIONS */
8020 ewmh_update_actions(win);
8024 DNPRINTF(SWM_D_MISC, "manage_window: done. window: 0x%x, (x,y) w x h: "
8025 "(%d,%d) %d x %d, ws: %d, iconic: %s, transient: 0x%x\n", win->id,
8026 X(win), Y(win), WIDTH(win), HEIGHT(win), win->ws->idx,
8027 YESNO(win->iconic), win->transient);
8033 free_window(struct ws_win *win)
8035 DNPRINTF(SWM_D_MISC, "free_window: window: 0x%x\n", win->id);
8040 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
8045 xcb_icccm_get_wm_class_reply_wipe(&win->ch);
8050 memset(win, 0xff, sizeof *win); /* XXX kill later */
8053 DNPRINTF(SWM_D_MISC, "free_window: done.\n");
8057 unmanage_window(struct ws_win *win)
8059 struct ws_win *parent;
8064 DNPRINTF(SWM_D_MISC, "unmanage_window: window: 0x%x\n", win->id);
8066 if (win->transient) {
8067 parent = find_window(win->transient);
8069 parent->focus_child = NULL;
8072 TAILQ_REMOVE(&win->ws->winlist, win, entry);
8073 TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
8077 expose(xcb_expose_event_t *e)
8080 struct swm_region *r;
8082 DNPRINTF(SWM_D_EVENT, "expose: window: 0x%x\n", e->window);
8084 num_screens = get_screen_count();
8085 for (i = 0; i < num_screens; i++)
8086 TAILQ_FOREACH(r, &screens[i].rl, entry)
8087 if (e->window == WINID(r->bar))
8095 focusin(xcb_focus_in_event_t *e)
8097 DNPRINTF(SWM_D_EVENT, "focusin: window: 0x%x, mode: %s(%u), "
8098 "detail: %s(%u)\n", e->event, get_notify_mode_label(e->mode),
8099 e->mode, get_notify_detail_label(e->detail), e->detail);
8103 focusout(xcb_focus_out_event_t *e)
8105 DNPRINTF(SWM_D_EVENT, "focusout: window: 0x%x, mode: %s(%u), "
8106 "detail: %s(%u)\n", e->event, get_notify_mode_label(e->mode),
8107 e->mode, get_notify_detail_label(e->detail), e->detail);
8112 keypress(xcb_key_press_event_t *e)
8114 xcb_keysym_t keysym;
8117 keysym = xcb_key_press_lookup_keysym(syms, e, 0);
8119 DNPRINTF(SWM_D_EVENT, "keypress: keysym: %u, win (x,y): 0x%x (%d,%d), "
8120 "detail: %u, time: %u, root (x,y): 0x%x (%d,%d), child: 0x%x, "
8121 "state: %u, same_screen: %s\n", keysym, e->event, e->event_x,
8122 e->event_y, e->detail, e->time, e->root, e->root_x, e->root_y,
8123 e->child, e->state, YESNO(e->same_screen));
8125 if ((kp = key_lookup(CLEANMASK(e->state), keysym)) == NULL)
8128 last_event_time = e->time;
8130 if (kp->funcid == KF_SPAWN_CUSTOM)
8131 spawn_custom(root_to_region(e->root, SWM_CK_ALL),
8132 &(keyfuncs[kp->funcid].args), kp->spawn_name);
8133 else if (keyfuncs[kp->funcid].func)
8134 keyfuncs[kp->funcid].func(root_to_region(e->root, SWM_CK_ALL),
8135 &(keyfuncs[kp->funcid].args));
8138 /* Unfreeze grab events. */
8139 xcb_allow_events(conn, XCB_ALLOW_ASYNC_KEYBOARD, e->time);
8142 DNPRINTF(SWM_D_EVENT, "keypress: done.\n");
8146 buttonpress(xcb_button_press_event_t *e)
8148 struct ws_win *win = NULL;
8149 struct swm_region *r, *old_r;
8153 DNPRINTF(SWM_D_EVENT, "buttonpress: win (x,y): 0x%x (%d,%d), "
8154 "detail: %u, time: %u, root (x,y): 0x%x (%d,%d), child: 0x%x, "
8155 "state: %u, same_screen: %s\n", e->event, e->event_x, e->event_y,
8156 e->detail, e->time, e->root, e->root_x, e->root_y, e->child,
8157 e->state, YESNO(e->same_screen));
8159 if (e->event == e->root) {
8160 if (e->child != 0) {
8161 win = find_window(e->child);
8162 /* Pass ButtonPress to window if it isn't managed. */
8166 /* Focus on empty region */
8167 /* If no windows on region if its empty. */
8168 r = root_to_region(e->root, SWM_CK_POINTER);
8170 DNPRINTF(SWM_D_EVENT, "buttonpress: "
8171 "NULL region; ignoring.\n");
8175 if (TAILQ_EMPTY(&r->ws->winlist)) {
8176 old_r = root_to_region(e->root, SWM_CK_FOCUS);
8177 if (old_r && old_r != r)
8178 unfocus_win(old_r->ws->focus);
8180 xcb_set_input_focus(conn,
8181 XCB_INPUT_FOCUS_PARENT, e->root, e->time);
8183 /* Clear bar since empty. */
8191 win = find_window(e->event);
8197 last_event_time = e->time;
8199 focus_win(get_focus_magic(win));
8201 for (i = 0; i < LENGTH(buttons); i++)
8202 if (client_click == buttons[i].action && buttons[i].func &&
8203 buttons[i].button == e->detail &&
8204 CLEANMASK(buttons[i].mask) == CLEANMASK(e->state)) {
8205 buttons[i].func(win, &buttons[i].args);
8211 DNPRINTF(SWM_D_EVENT, "buttonpress: passing to window.\n");
8212 /* Replay event to event window */
8213 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, e->time);
8215 DNPRINTF(SWM_D_EVENT, "buttonpress: handled.\n");
8216 /* Unfreeze grab events. */
8217 xcb_allow_events(conn, XCB_ALLOW_SYNC_POINTER, e->time);
8225 print_win_geom(xcb_window_t w)
8227 xcb_get_geometry_reply_t *wa;
8229 wa = xcb_get_geometry_reply(conn, xcb_get_geometry(conn, w), NULL);
8232 DNPRINTF(SWM_D_MISC, "print_win_geom: window not found: 0x%x\n",
8237 DNPRINTF(SWM_D_MISC, "print_win_geom: window: 0x%x, root: 0x%x, "
8238 "depth: %u, (x,y) w x h: (%d,%d) %d x %d, border: %d\n",
8239 w, wa->root, wa->depth, wa->x, wa->y, wa->width, wa->height,
8248 get_stack_mode_name(uint8_t mode)
8253 case XCB_STACK_MODE_ABOVE:
8256 case XCB_STACK_MODE_BELOW:
8259 case XCB_STACK_MODE_TOP_IF:
8262 case XCB_STACK_MODE_BOTTOM_IF:
8265 case XCB_STACK_MODE_OPPOSITE:
8277 configurerequest(xcb_configure_request_event_t *e)
8280 struct swm_region *r = NULL;
8283 uint32_t wc[7] = {0};
8285 if ((win = find_window(e->window)) == NULL)
8286 if ((win = find_unmanaged_window(e->window)) == NULL)
8290 if (swm_debug & SWM_D_EVENT) {
8291 print_win_geom(e->window);
8293 DNPRINTF(SWM_D_EVENT, "configurerequest: window: 0x%x, "
8294 "parent: 0x%x, new: %s, value_mask: %u { ", e->window,
8295 e->parent, YESNO(new), e->value_mask);
8296 if (e->value_mask & XCB_CONFIG_WINDOW_X)
8297 DPRINTF("X: %d ", e->x);
8298 if (e->value_mask & XCB_CONFIG_WINDOW_Y)
8299 DPRINTF("Y: %d ", e->y);
8300 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH)
8301 DPRINTF("W: %u ", e->width);
8302 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
8303 DPRINTF("H: %u ", e->height);
8304 if (e->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH)
8305 DPRINTF("Border: %u ", e->border_width);
8306 if (e->value_mask & XCB_CONFIG_WINDOW_SIBLING)
8307 DPRINTF("Sibling: 0x%x ", e->sibling);
8308 if (e->value_mask & XCB_CONFIG_WINDOW_STACK_MODE)
8309 DPRINTF("StackMode: %s(%u) ",
8310 get_stack_mode_name(e->stack_mode), e->stack_mode);
8316 if (e->value_mask & XCB_CONFIG_WINDOW_X) {
8317 mask |= XCB_CONFIG_WINDOW_X;
8320 if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
8321 mask |= XCB_CONFIG_WINDOW_Y;
8324 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
8325 mask |= XCB_CONFIG_WINDOW_WIDTH;
8328 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
8329 mask |= XCB_CONFIG_WINDOW_HEIGHT;
8330 wc[i++] = e->height;
8332 if (e->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH) {
8333 mask |= XCB_CONFIG_WINDOW_BORDER_WIDTH;
8334 wc[i++] = e->border_width;
8336 if (e->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
8337 mask |= XCB_CONFIG_WINDOW_SIBLING;
8338 wc[i++] = e->sibling;
8340 if (e->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
8341 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
8342 wc[i++] = e->stack_mode;
8346 xcb_configure_window(conn, e->window, mask, wc);
8349 } else if ((!win->manual || win->quirks & SWM_Q_ANYWHERE) &&
8350 !(win->ewmh_flags & EWMH_F_FULLSCREEN)) {
8353 else if (win->ws->old_r)
8356 /* windows are centered unless ANYWHERE quirk is set. */
8357 if (win->quirks & SWM_Q_ANYWHERE) {
8358 if (e->value_mask & XCB_CONFIG_WINDOW_X) {
8359 win->g_float.x = e->x;
8361 win->g_float.x -= X(r);
8364 if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
8365 win->g_float.y = e->y;
8367 win->g_float.y -= Y(r);
8371 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH)
8372 win->g_float.w = e->width;
8374 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
8375 win->g_float.h = e->height;
8377 win->g_floatvalid = 1;
8379 if (win->floating && r && (win->transient ||
8380 win->ws->cur_layout != &layouts[SWM_MAX_STACK])) {
8381 WIDTH(win) = win->g_float.w;
8382 HEIGHT(win) = win->g_float.h;
8385 stack_floater(win, r);
8397 DNPRINTF(SWM_D_EVENT, "configurerequest: done.\n");
8401 configurenotify(xcb_configure_notify_event_t *e)
8405 DNPRINTF(SWM_D_EVENT, "configurenotify: win 0x%x, event win: 0x%x, "
8406 "(x,y) WxH: (%d,%d) %ux%u, border: %u, above_sibling: 0x%x, "
8407 "override_redirect: %s\n", e->window, e->event, e->x, e->y,
8408 e->width, e->height, e->border_width, e->above_sibling,
8409 YESNO(e->override_redirect));
8411 win = find_window(e->window);
8413 xcb_icccm_get_wm_normal_hints_reply(conn,
8414 xcb_icccm_get_wm_normal_hints(conn, win->id),
8417 if (font_adjusted) {
8425 destroynotify(xcb_destroy_notify_event_t *e)
8429 DNPRINTF(SWM_D_EVENT, "destroynotify: window: 0x%x\n", e->window);
8431 if ((win = find_window(e->window)) == NULL) {
8432 if ((win = find_unmanaged_window(e->window)) == NULL)
8438 if (focus_mode != SWM_FOCUS_FOLLOW) {
8439 /* If we were focused, make sure we focus on something else. */
8440 if (win == win->ws->focus)
8441 win->ws->focus_pending = get_focus_prev(win);
8444 unmanage_window(win);
8447 if (focus_mode != SWM_FOCUS_FOLLOW && WS_FOCUSED(win->ws)) {
8448 if (win->ws->focus_pending) {
8449 focus_win(win->ws->focus_pending);
8450 win->ws->focus_pending = NULL;
8451 } else if (win == win->ws->focus) {
8452 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT,
8453 win->ws->r->id, XCB_CURRENT_TIME);
8464 get_notify_detail_label(uint8_t detail)
8469 case XCB_NOTIFY_DETAIL_ANCESTOR:
8472 case XCB_NOTIFY_DETAIL_VIRTUAL:
8475 case XCB_NOTIFY_DETAIL_INFERIOR:
8478 case XCB_NOTIFY_DETAIL_NONLINEAR:
8479 label = "Nonlinear";
8481 case XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL:
8482 label = "NonlinearVirtual";
8484 case XCB_NOTIFY_DETAIL_POINTER:
8487 case XCB_NOTIFY_DETAIL_POINTER_ROOT:
8488 label = "PointerRoot";
8490 case XCB_NOTIFY_DETAIL_NONE:
8501 get_notify_mode_label(uint8_t mode)
8506 case XCB_NOTIFY_MODE_NORMAL:
8509 case XCB_NOTIFY_MODE_GRAB:
8512 case XCB_NOTIFY_MODE_UNGRAB:
8515 case XCB_NOTIFY_MODE_WHILE_GRABBED:
8516 label = "WhileGrabbed";
8527 enternotify(xcb_enter_notify_event_t *e)
8530 struct swm_region *r;
8532 DNPRINTF(SWM_D_FOCUS, "enternotify: time: %u, win (x,y): 0x%x "
8533 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): 0x%x (%d,%d), "
8534 "child: 0x%x, same_screen_focus: %s, state: %d\n",
8535 e->time, e->event, e->event_x, e->event_y,
8536 get_notify_mode_label(e->mode), e->mode,
8537 get_notify_detail_label(e->detail), e->detail,
8538 e->root, e->root_x, e->root_y, e->child,
8539 YESNO(e->same_screen_focus), e->state);
8541 if (focus_mode == SWM_FOCUS_MANUAL &&
8542 e->mode == XCB_NOTIFY_MODE_NORMAL) {
8543 DNPRINTF(SWM_D_EVENT, "enternotify: manual focus; ignoring.\n");
8547 last_event_time = e->time;
8549 if ((win = find_window(e->event)) == NULL) {
8550 if (e->event == e->root) {
8551 /* If no windows on pointer region, then focus root. */
8552 r = root_to_region(e->root, SWM_CK_POINTER);
8554 DNPRINTF(SWM_D_EVENT, "enternotify: "
8555 "NULL region; ignoring.\n");
8561 DNPRINTF(SWM_D_EVENT, "enternotify: window is NULL; "
8566 focus_win(get_focus_magic(win));
8574 leavenotify(xcb_leave_notify_event_t *e)
8576 DNPRINTF(SWM_D_FOCUS, "leavenotify: time: %u, win (x,y): 0x%x "
8577 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): 0x%x (%d,%d), "
8578 "child: 0x%x, same_screen_focus: %s, state: %d\n",
8579 e->time, e->event, e->event_x, e->event_y,
8580 get_notify_mode_label(e->mode), e->mode,
8581 get_notify_detail_label(e->detail), e->detail,
8582 e->root, e->root_x, e->root_y, e->child,
8583 YESNO(e->same_screen_focus), e->state);
8588 mapnotify(xcb_map_notify_event_t *e)
8592 DNPRINTF(SWM_D_EVENT, "mapnotify: window: 0x%x\n", e->window);
8594 if ((win = find_window(e->window)) == NULL)
8595 win = manage_window(e->window, 1);
8598 set_win_state(win, XCB_ICCCM_WM_STATE_NORMAL);
8600 if (focus_mode != SWM_FOCUS_FOLLOW && WS_FOCUSED(win->ws)) {
8601 if (win->ws->focus_pending == win) {
8603 win->ws->focus_pending = NULL;
8612 mappingnotify(xcb_mapping_notify_event_t *e)
8614 xcb_refresh_keyboard_mapping(syms, e);
8616 if (e->request == XCB_MAPPING_KEYBOARD)
8621 maprequest(xcb_map_request_event_t *e)
8623 struct ws_win *win, *w = NULL;
8624 xcb_get_window_attributes_reply_t *war;
8626 DNPRINTF(SWM_D_EVENT, "maprequest: win 0x%x\n",
8629 war = xcb_get_window_attributes_reply(conn,
8630 xcb_get_window_attributes(conn, e->window),
8633 DNPRINTF(SWM_D_EVENT, "maprequest: window lost.\n");
8637 if (war->override_redirect) {
8638 DNPRINTF(SWM_D_EVENT, "maprequest: override_redirect; "
8643 win = manage_window(e->window,
8644 (war->map_state == XCB_MAP_STATE_VIEWABLE));
8646 /* The new window should get focus; prepare. */
8647 if (focus_mode != SWM_FOCUS_FOLLOW &&
8648 !(win->quirks & SWM_Q_NOFOCUSONMAP)) {
8649 if (win->quirks & SWM_Q_FOCUSONMAP_SINGLE) {
8650 /* See if other wins of same type are already mapped. */
8651 TAILQ_FOREACH(w, &win->ws->winlist, entry) {
8652 if (w == win || !w->mapped)
8655 if (w->ch.class_name &&
8656 win->ch.class_name &&
8657 !strcmp(w->ch.class_name,
8658 win->ch.class_name) &&
8659 w->ch.instance_name &&
8660 win->ch.instance_name &&
8661 !strcmp(w->ch.instance_name,
8662 win->ch.instance_name))
8668 win->ws->focus_pending = get_focus_magic(win);
8671 /* All windows need to be mapped if they are in the current workspace.*/
8675 /* Ignore EnterNotify to handle the mapnotify without interference. */
8676 if (focus_mode == SWM_FOCUS_DEFAULT)
8677 event_drain(XCB_ENTER_NOTIFY);
8680 DNPRINTF(SWM_D_EVENT, "maprequest: done.\n");
8684 motionnotify(xcb_motion_notify_event_t *e)
8686 struct swm_region *r;
8689 DNPRINTF(SWM_D_FOCUS, "motionnotify: time: %u, win (x,y): 0x%x "
8690 "(%d,%d), detail: %s(%d), root (x,y): 0x%x (%d,%d), "
8691 "child: 0x%x, same_screen_focus: %s, state: %d\n",
8692 e->time, e->event, e->event_x, e->event_y,
8693 get_notify_detail_label(e->detail), e->detail,
8694 e->root, e->root_x, e->root_y, e->child,
8695 YESNO(e->same_screen), e->state);
8697 last_event_time = e->time;
8699 if (focus_mode == SWM_FOCUS_MANUAL)
8702 num_screens = get_screen_count();
8703 for (i = 0; i < num_screens; i++)
8704 if (screens[i].root == e->root)
8707 TAILQ_FOREACH(r, &screens[i].rl, entry)
8708 if (X(r) <= e->root_x && e->root_x < MAX_X(r) &&
8709 Y(r) <= e->root_y && e->root_y < MAX_Y(r))
8717 get_atom_name(xcb_atom_t atom)
8722 * This should be disabled during most debugging since
8723 * xcb_get_* causes an xcb_flush.
8726 xcb_get_atom_name_reply_t *r;
8728 r = xcb_get_atom_name_reply(conn,
8729 xcb_get_atom_name(conn, atom),
8732 len = xcb_get_atom_name_name_length(r);
8734 name = malloc(len + 1);
8736 memcpy(name, xcb_get_atom_name_name(r), len);
8750 propertynotify(xcb_property_notify_event_t *e)
8753 struct workspace *ws;
8757 name = get_atom_name(e->atom);
8758 DNPRINTF(SWM_D_EVENT, "propertynotify: window: 0x%x, atom: %s(%u), "
8759 "time: %#x, state: %u\n", e->window, name, e->atom, e->time,
8763 win = find_window(e->window);
8769 last_event_time = e->time;
8771 if (e->atom == a_swm_iconic) {
8772 if (e->state == XCB_PROPERTY_NEW_VALUE) {
8773 if (focus_mode != SWM_FOCUS_FOLLOW)
8774 ws->focus_pending = get_focus_prev(win);
8782 if (focus_mode != SWM_FOCUS_FOLLOW &&
8784 if (ws->focus_pending) {
8785 focus_win(ws->focus_pending);
8786 ws->focus_pending = NULL;
8788 xcb_set_input_focus(conn,
8789 XCB_INPUT_FOCUS_PARENT,
8797 } else if (e->state == XCB_PROPERTY_DELETE) {
8798 /* The window is no longer iconic, restack ws. */
8799 if (focus_mode != SWM_FOCUS_FOLLOW)
8800 ws->focus_pending = get_focus_magic(win);
8804 /* Flush EnterNotify for mapnotify, if needed. */
8807 } else if (e->atom == a_state) {
8808 /* State just changed, make sure it gets focused if mapped. */
8809 if (e->state == XCB_PROPERTY_NEW_VALUE) {
8810 if (focus_mode != SWM_FOCUS_FOLLOW && WS_FOCUSED(ws)) {
8812 ws->focus_pending == win) {
8813 focus_win(ws->focus_pending);
8814 ws->focus_pending = NULL;
8818 } else if (e->atom == XCB_ATOM_WM_CLASS ||
8819 e->atom == XCB_ATOM_WM_NAME) {
8827 unmapnotify(xcb_unmap_notify_event_t *e)
8830 struct workspace *ws;
8832 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: 0x%x\n", e->window);
8834 /* If we aren't managing the window, then ignore. */
8835 win = find_window(e->window);
8836 if (win == NULL || win->id != e->window)
8841 if (getstate(e->window) != XCB_ICCCM_WM_STATE_ICONIC)
8842 set_win_state(win, XCB_ICCCM_WM_STATE_ICONIC);
8845 /* window unmapped itself */
8846 /* do unmap/unfocus/restack and unmanage */
8849 /* If win was focused, make sure to focus on something else. */
8850 if (win == ws->focus) {
8851 if (focus_mode != SWM_FOCUS_FOLLOW) {
8852 ws->focus_pending = get_focus_prev(win);
8853 DNPRINTF(SWM_D_EVENT, "unmapnotify: "
8854 "focus_pending: 0x%x\n",
8855 WINID(ws->focus_pending));
8861 unmanage_window(win);
8866 if (WS_FOCUSED(ws)) {
8867 if (focus_mode == SWM_FOCUS_FOLLOW) {
8868 focus_win(get_pointer_win(ws->r->s->root));
8869 } else if (ws->focus_pending) {
8870 focus_win(ws->focus_pending);
8871 ws->focus_pending = NULL;
8872 } else if (ws->focus == NULL) {
8873 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT,
8874 ws->r->id, XCB_CURRENT_TIME);
8879 if (getstate(e->window) == XCB_ICCCM_WM_STATE_NORMAL)
8880 set_win_state(win, XCB_ICCCM_WM_STATE_ICONIC);
8887 visibilitynotify(xcb_visibility_notify_event_t *e)
8889 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: 0x%x\n",
8895 clientmessage(xcb_client_message_event_t *e)
8898 xcb_map_request_event_t mre;
8902 name = get_atom_name(e->type);
8903 DNPRINTF(SWM_D_EVENT, "clientmessage: window: 0x%x, atom: %s(%u)\n",
8904 e->window, name, e->type);
8907 win = find_window(e->window);
8910 if (e->type == ewmh[_NET_ACTIVE_WINDOW].atom) {
8911 /* Manage the window with maprequest. */
8912 DNPRINTF(SWM_D_EVENT, "clientmessage: request focus on "
8913 "unmanaged window.\n");
8914 mre.window = e->window;
8920 if (e->type == ewmh[_NET_ACTIVE_WINDOW].atom) {
8921 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_ACTIVE_WINDOW\n");
8922 if (WS_FOCUSED(win->ws))
8925 win->ws->focus_pending = win;
8927 if (e->type == ewmh[_NET_CLOSE_WINDOW].atom) {
8928 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_CLOSE_WINDOW\n");
8929 if (win->can_delete)
8930 client_msg(win, a_delete, 0);
8932 xcb_kill_client(conn, win->id);
8934 if (e->type == ewmh[_NET_MOVERESIZE_WINDOW].atom) {
8935 DNPRINTF(SWM_D_EVENT,
8936 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
8937 if (win->floating) {
8938 if (e->data.data32[0] & (1<<8)) /* x */
8939 X(win) = e->data.data32[1];
8940 if (e->data.data32[0] & (1<<9)) /* y */
8941 Y(win) = e->data.data32[2];
8942 if (e->data.data32[0] & (1<<10)) /* width */
8943 WIDTH(win) = e->data.data32[3];
8944 if (e->data.data32[0] & (1<<11)) /* height */
8945 HEIGHT(win) = e->data.data32[4];
8950 /* TODO: Change stack sizes */
8951 /* notify no change was made. */
8952 config_win(win, NULL);
8955 if (e->type == ewmh[_NET_WM_STATE].atom) {
8956 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_WM_STATE\n");
8957 ewmh_update_win_state(win, e->data.data32[1], e->data.data32[0]);
8958 if (e->data.data32[2])
8959 ewmh_update_win_state(win, e->data.data32[2],
8971 int errcode = xcb_connection_has_error(conn);
8972 #ifdef XCB_CONN_ERROR
8975 case XCB_CONN_ERROR:
8976 s = "Socket error, pipe error or other stream error.";
8978 case XCB_CONN_CLOSED_EXT_NOTSUPPORTED:
8979 s = "Extension not supported.";
8981 case XCB_CONN_CLOSED_MEM_INSUFFICIENT:
8982 s = "Insufficient memory.";
8984 case XCB_CONN_CLOSED_REQ_LEN_EXCEED:
8985 s = "Request length was exceeded.";
8987 case XCB_CONN_CLOSED_PARSE_ERR:
8988 s = "Error parsing display string.";
8991 s = "Unknown error.";
8994 errx(errcode, "X CONNECTION ERROR: %s", s);
8997 errx(errcode, "X CONNECTION ERROR");
9005 const uint32_t val = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
9006 XCB_EVENT_MASK_ENTER_WINDOW;
9008 xcb_void_cookie_t wac;
9009 xcb_generic_error_t *error;
9011 /* this causes an error if some other window manager is running */
9012 num_screens = get_screen_count();
9013 for (i = 0; i < num_screens; i++) {
9014 if ((sc = get_screen(i)) == NULL)
9015 errx(1, "ERROR: can't get screen %d.", i);
9016 DNPRINTF(SWM_D_INIT, "enable_wm: screen %d, root: 0x%x\n",
9018 wac = xcb_change_window_attributes_checked(conn, sc->root,
9019 XCB_CW_EVENT_MASK, &val);
9020 if ((error = xcb_request_check(conn, wac))) {
9021 DNPRINTF(SWM_D_INIT, "enable_wm: error_code: %u\n",
9027 /* click to focus on empty region */
9028 xcb_grab_button(conn, 1, sc->root, BUTTONMASK,
9029 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC, XCB_WINDOW_NONE,
9030 XCB_CURSOR_NONE, XCB_BUTTON_INDEX_1, XCB_BUTTON_MASK_ANY);
9037 new_region(struct swm_screen *s, int x, int y, int w, int h)
9039 struct swm_region *r, *n;
9040 struct workspace *ws = NULL;
9044 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
9045 s->idx, w, h, x, y);
9047 /* remove any conflicting regions */
9048 n = TAILQ_FIRST(&s->rl);
9051 n = TAILQ_NEXT(r, entry);
9052 if (X(r) < (x + w) && (X(r) + WIDTH(r)) > x &&
9053 Y(r) < (y + h) && (Y(r) + HEIGHT(r)) > y) {
9054 if (r->ws->r != NULL)
9055 r->ws->old_r = r->ws->r;
9058 xcb_destroy_window(conn, r->id);
9059 TAILQ_REMOVE(&s->rl, r, entry);
9060 TAILQ_INSERT_TAIL(&s->orl, r, entry);
9064 /* search old regions for one to reuse */
9066 /* size + location match */
9067 TAILQ_FOREACH(r, &s->orl, entry)
9068 if (X(r) == x && Y(r) == y &&
9069 HEIGHT(r) == h && WIDTH(r) == w)
9073 TAILQ_FOREACH(r, &s->orl, entry)
9074 if (HEIGHT(r) == h && WIDTH(r) == w)
9078 TAILQ_REMOVE(&s->orl, r, entry);
9079 /* try to use old region's workspace */
9080 if (r->ws->r == NULL)
9083 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
9084 err(1, "new_region: calloc: failed to allocate memory "
9087 /* if we don't have a workspace already, find one */
9089 for (i = 0; i < workspace_limit; i++)
9090 if (s->ws[i].r == NULL) {
9097 errx(1, "new_region: no free workspaces");
9108 TAILQ_INSERT_TAIL(&s->rl, r, entry);
9110 /* Invisible region window to detect pointer events on empty regions. */
9111 r->id = xcb_generate_id(conn);
9112 wa[0] = XCB_EVENT_MASK_POINTER_MOTION |
9113 XCB_EVENT_MASK_POINTER_MOTION_HINT;
9115 xcb_create_window(conn, XCB_COPY_FROM_PARENT, r->id, r->s->root,
9116 X(r), Y(r), WIDTH(r), HEIGHT(r), 0, XCB_WINDOW_CLASS_INPUT_ONLY,
9117 XCB_COPY_FROM_PARENT, XCB_CW_EVENT_MASK, wa);
9119 xcb_map_window(conn, r->id);
9125 #ifdef SWM_XRR_HAS_CRTC
9128 #endif /* SWM_XRR_HAS_CRTC */
9129 struct swm_region *r;
9132 xcb_randr_get_screen_resources_current_cookie_t src;
9133 xcb_randr_get_screen_resources_current_reply_t *srr;
9134 xcb_randr_get_crtc_info_cookie_t cic;
9135 xcb_randr_get_crtc_info_reply_t *cir = NULL;
9136 xcb_randr_crtc_t *crtc;
9137 xcb_screen_t *screen;
9139 DNPRINTF(SWM_D_MISC, "scan_xrandr: screen: %d\n", i);
9141 if ((screen = get_screen(i)) == NULL)
9142 errx(1, "ERROR: can't get screen %d.", i);
9144 num_screens = get_screen_count();
9145 if (i >= num_screens)
9146 errx(1, "scan_xrandr: invalid screen");
9148 /* remove any old regions */
9149 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
9150 r->ws->old_r = r->ws->r = NULL;
9152 xcb_destroy_window(conn, r->id);
9153 TAILQ_REMOVE(&screens[i].rl, r, entry);
9154 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
9158 /* map virtual screens onto physical screens */
9159 #ifdef SWM_XRR_HAS_CRTC
9160 if (xrandr_support) {
9161 src = xcb_randr_get_screen_resources_current(conn,
9163 srr = xcb_randr_get_screen_resources_current_reply(conn, src,
9166 new_region(&screens[i], 0, 0,
9167 screen->width_in_pixels,
9168 screen->height_in_pixels);
9171 ncrtc = srr->num_crtcs;
9173 crtc = xcb_randr_get_screen_resources_current_crtcs(srr);
9174 for (c = 0; c < ncrtc; c++) {
9175 cic = xcb_randr_get_crtc_info(conn, crtc[c],
9177 cir = xcb_randr_get_crtc_info_reply(conn, cic, NULL);
9180 if (cir->num_outputs == 0) {
9186 new_region(&screens[i], 0, 0,
9187 screen->width_in_pixels,
9188 screen->height_in_pixels);
9190 new_region(&screens[i],
9191 cir->x, cir->y, cir->width, cir->height);
9196 #endif /* SWM_XRR_HAS_CRTC */
9198 /* If detection failed, create a single region that spans the screen. */
9199 if (TAILQ_EMPTY(&screens[i].rl))
9200 new_region(&screens[i], 0, 0, screen->width_in_pixels,
9201 screen->height_in_pixels);
9204 /* Cleanup unused previously visible workspaces. */
9205 TAILQ_FOREACH(r, &screens[i].orl, entry) {
9206 TAILQ_FOREACH(win, &r->ws->winlist, entry)
9209 /* The screen shouldn't focus on an unused region. */
9210 if (screens[i].r_focus == r)
9211 screens[i].r_focus = NULL;
9214 DNPRINTF(SWM_D_MISC, "scan_xrandr: done.\n");
9218 screenchange(xcb_randr_screen_change_notify_event_t *e)
9220 struct swm_region *r;
9223 DNPRINTF(SWM_D_EVENT, "screenchange: root: 0x%x\n", e->root);
9225 num_screens = get_screen_count();
9226 /* silly event doesn't include the screen index */
9227 for (i = 0; i < num_screens; i++)
9228 if (screens[i].root == e->root)
9230 if (i >= num_screens)
9231 errx(1, "screenchange: screen not found");
9233 /* brute force for now, just re-enumerate the regions */
9237 print_win_geom(e->root);
9239 /* add bars to all regions */
9240 for (i = 0; i < num_screens; i++) {
9241 TAILQ_FOREACH(r, &screens[i].rl, entry)
9247 /* Make sure a region has focus on each screen. */
9248 for (i = 0; i < num_screens; i++) {
9249 if (screens[i].r_focus == NULL) {
9250 r = TAILQ_FIRST(&screens[i].rl);
9263 struct swm_region *r = NULL;
9264 xcb_window_t *wins = NULL, trans;
9266 int i, j, num_screens;
9267 uint16_t state, manage, mapped;
9269 xcb_query_tree_cookie_t qtc;
9270 xcb_query_tree_reply_t *qtr;
9271 xcb_get_window_attributes_cookie_t gac;
9272 xcb_get_window_attributes_reply_t *gar;
9273 xcb_get_property_cookie_t pc;
9275 DNPRINTF(SWM_D_INIT, "grab_windows: begin\n");
9276 num_screens = get_screen_count();
9277 for (i = 0; i < num_screens; i++) {
9278 qtc = xcb_query_tree(conn, screens[i].root);
9279 qtr = xcb_query_tree_reply(conn, qtc, NULL);
9282 wins = xcb_query_tree_children(qtr);
9283 no = xcb_query_tree_children_length(qtr);
9284 /* attach windows to a region */
9285 /* normal windows */
9286 DNPRINTF(SWM_D_INIT, "grab_windows: grab top level windows.\n");
9287 for (j = 0; j < no; j++) {
9288 TAILQ_FOREACH(r, &screens[i].rl, entry) {
9289 if (r->id == wins[j]) {
9290 DNPRINTF(SWM_D_INIT, "grab_windows: "
9291 "skip %#x; region input window.\n",
9300 gac = xcb_get_window_attributes(conn, wins[j]);
9301 gar = xcb_get_window_attributes_reply(conn, gac, NULL);
9303 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
9304 "doesn't exist.\n", wins[j]);
9308 if (gar->override_redirect) {
9309 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
9310 "override_redirect set.\n", wins[j]);
9315 pc = xcb_icccm_get_wm_transient_for(conn, wins[j]);
9316 if (xcb_icccm_get_wm_transient_for_reply(conn, pc,
9318 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
9319 "is transient for %#x.\n", wins[j], trans);
9324 state = getstate(wins[j]);
9325 manage = state != XCB_ICCCM_WM_STATE_WITHDRAWN;
9326 mapped = gar->map_state != XCB_MAP_STATE_UNMAPPED;
9327 if (mapped || manage)
9328 manage_window(wins[j], mapped);
9331 /* transient windows */
9332 DNPRINTF(SWM_D_INIT, "grab_windows: grab transient windows.\n");
9333 for (j = 0; j < no; j++) {
9334 gac = xcb_get_window_attributes(conn, wins[j]);
9335 gar = xcb_get_window_attributes_reply(conn, gac, NULL);
9337 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
9338 "doesn't exist.\n", wins[j]);
9342 if (gar->override_redirect) {
9343 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
9344 "override_redirect set.\n", wins[j]);
9349 state = getstate(wins[j]);
9350 manage = state != XCB_ICCCM_WM_STATE_WITHDRAWN;
9351 mapped = gar->map_state != XCB_MAP_STATE_UNMAPPED;
9352 pc = xcb_icccm_get_wm_transient_for(conn, wins[j]);
9353 if (xcb_icccm_get_wm_transient_for_reply(conn, pc,
9354 &trans, NULL) && manage)
9355 manage_window(wins[j], mapped);
9360 DNPRINTF(SWM_D_INIT, "grab_windows: done.\n");
9366 int i, j, k, num_screens;
9367 struct workspace *ws;
9368 uint32_t gcv[1], wa[1];
9369 const xcb_query_extension_reply_t *qep;
9370 xcb_screen_t *screen;
9371 xcb_randr_query_version_cookie_t c;
9372 xcb_randr_query_version_reply_t *r;
9374 num_screens = get_screen_count();
9375 if ((screens = calloc(num_screens,
9376 sizeof(struct swm_screen))) == NULL)
9377 err(1, "setup_screens: calloc: failed to allocate memory for "
9380 /* initial Xrandr setup */
9382 qep = xcb_get_extension_data(conn, &xcb_randr_id);
9384 c = xcb_randr_query_version(conn, 1, 1);
9385 r = xcb_randr_query_version_reply(conn, c, NULL);
9387 if (r->major_version >= 1) {
9389 xrandr_eventbase = qep->first_event;
9395 wa[0] = cursors[XC_LEFT_PTR].cid;
9397 /* map physical screens */
9398 for (i = 0; i < num_screens; i++) {
9399 DNPRINTF(SWM_D_WS, "setup_screens: init screen: %d\n", i);
9401 screens[i].r_focus = NULL;
9403 TAILQ_INIT(&screens[i].rl);
9404 TAILQ_INIT(&screens[i].orl);
9405 if ((screen = get_screen(i)) == NULL)
9406 errx(1, "ERROR: can't get screen %d.", i);
9407 screens[i].root = screen->root;
9409 /* set default colors */
9410 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
9411 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
9412 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
9413 setscreencolor("rgb:00/40/40", i + 1,
9414 SWM_S_COLOR_BAR_BORDER_UNFOCUS);
9415 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
9416 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
9418 /* create graphics context on screen */
9419 screens[i].bar_gc = xcb_generate_id(conn);
9421 xcb_create_gc(conn, screens[i].bar_gc, screens[i].root,
9422 XCB_GC_GRAPHICS_EXPOSURES, gcv);
9424 /* set default cursor */
9425 xcb_change_window_attributes(conn, screens[i].root,
9428 /* init all workspaces */
9429 /* XXX these should be dynamically allocated too */
9430 for (j = 0; j < SWM_WS_MAX; j++) {
9431 ws = &screens[i].ws[j];
9434 ws->bar_enabled = 1;
9436 ws->focus_prev = NULL;
9437 ws->focus_pending = NULL;
9440 TAILQ_INIT(&ws->winlist);
9441 TAILQ_INIT(&ws->unmanagedlist);
9443 for (k = 0; layouts[k].l_stack != NULL; k++)
9444 if (layouts[k].l_config != NULL)
9445 layouts[k].l_config(ws,
9446 SWM_ARG_ID_STACKINIT);
9447 ws->cur_layout = &layouts[0];
9448 ws->cur_layout->l_string(ws);
9454 xcb_randr_select_input(conn, screens[i].root,
9455 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE);
9462 if ((bar_fonts = strdup(SWM_BAR_FONTS)) == NULL)
9463 err(1, "setup_globals: strdup: failed to allocate memory.");
9465 if ((clock_format = strdup("%a %b %d %R %Z %Y")) == NULL)
9466 err(1, "setup_globals: strdup: failed to allocate memory.");
9468 if ((syms = xcb_key_symbols_alloc(conn)) == NULL)
9469 errx(1, "unable to allocate key symbols");
9471 a_state = get_atom_from_string("WM_STATE");
9472 a_prot = get_atom_from_string("WM_PROTOCOLS");
9473 a_delete = get_atom_from_string("WM_DELETE_WINDOW");
9474 a_takefocus = get_atom_from_string("WM_TAKE_FOCUS");
9475 a_utf8_string = get_atom_from_string("UTF8_STRING");
9476 a_swm_iconic = get_atom_from_string("_SWM_ICONIC");
9477 a_swm_ws = get_atom_from_string("_SWM_WS");
9484 xcb_atom_t netwmcheck;
9485 xcb_window_t root, win;
9487 /* work around sun jdk bugs, code from wmname */
9488 netwmcheck = get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
9490 num_screens = get_screen_count();
9491 for (i = 0; i < num_screens; i++) {
9492 root = screens[i].root;
9494 win = xcb_generate_id(conn);
9495 xcb_create_window(conn, XCB_COPY_FROM_PARENT, win, root,
9496 0, 0, 1, 1, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT,
9497 XCB_COPY_FROM_PARENT, 0, NULL);
9499 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root,
9500 netwmcheck, XCB_ATOM_WINDOW, 32, 1, &win);
9501 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win,
9502 netwmcheck, XCB_ATOM_WINDOW, 32, 1, &win);
9503 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win,
9504 ewmh[_NET_WM_NAME].atom, a_utf8_string, 8, strlen("LG3D"),
9510 shutdown_cleanup(void)
9514 /* disable alarm because the following code may not be interrupted */
9516 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
9517 err(1, "can't disable alarm");
9526 num_screens = get_screen_count();
9527 for (i = 0; i < num_screens; ++i) {
9528 if (screens[i].bar_gc != 0)
9529 xcb_free_gc(conn, screens[i].bar_gc);
9530 if (!bar_font_legacy)
9531 XftColorFree(display, DefaultVisual(display, i),
9532 DefaultColormap(display, i), &bar_font_color);
9535 if (bar_font_legacy)
9536 XFreeFontSet(display, bar_fs);
9538 XftFontClose(display, bar_font);
9541 xcb_key_symbols_free(syms);
9543 xcb_disconnect(conn);
9547 event_error(xcb_generic_error_t *e)
9551 DNPRINTF(SWM_D_EVENT, "event_error: %s(%u) from %s(%u), sequence: %u, "
9552 "resource_id: %u, minor_code: %u\n",
9553 xcb_event_get_error_label(e->error_code), e->error_code,
9554 xcb_event_get_request_label(e->major_code), e->major_code,
9555 e->sequence, e->resource_id, e->minor_code);
9559 event_handle(xcb_generic_event_t *evt)
9561 uint8_t type = XCB_EVENT_RESPONSE_TYPE(evt);
9563 DNPRINTF(SWM_D_EVENT, "XCB Event: %s(%d), seq %u\n",
9564 xcb_event_get_label(XCB_EVENT_RESPONSE_TYPE(evt)),
9565 XCB_EVENT_RESPONSE_TYPE(evt), evt->sequence);
9568 #define EVENT(type, callback) case type: callback((void *)evt); return
9569 EVENT(0, event_error);
9570 EVENT(XCB_BUTTON_PRESS, buttonpress);
9571 /*EVENT(XCB_BUTTON_RELEASE, buttonpress);*/
9572 /*EVENT(XCB_CIRCULATE_NOTIFY, );*/
9573 /*EVENT(XCB_CIRCULATE_REQUEST, );*/
9574 EVENT(XCB_CLIENT_MESSAGE, clientmessage);
9575 /*EVENT(XCB_COLORMAP_NOTIFY, );*/
9576 EVENT(XCB_CONFIGURE_NOTIFY, configurenotify);
9577 EVENT(XCB_CONFIGURE_REQUEST, configurerequest);
9578 /*EVENT(XCB_CREATE_NOTIFY, );*/
9579 EVENT(XCB_DESTROY_NOTIFY, destroynotify);
9580 EVENT(XCB_ENTER_NOTIFY, enternotify);
9581 EVENT(XCB_EXPOSE, expose);
9583 EVENT(XCB_FOCUS_IN, focusin);
9584 EVENT(XCB_FOCUS_OUT, focusout);
9586 /*EVENT(XCB_GRAPHICS_EXPOSURE, );*/
9587 /*EVENT(XCB_GRAVITY_NOTIFY, );*/
9588 EVENT(XCB_KEY_PRESS, keypress);
9589 /*EVENT(XCB_KEY_RELEASE, keypress);*/
9590 /*EVENT(XCB_KEYMAP_NOTIFY, );*/
9592 EVENT(XCB_LEAVE_NOTIFY, leavenotify);
9594 EVENT(XCB_MAP_NOTIFY, mapnotify);
9595 EVENT(XCB_MAP_REQUEST, maprequest);
9596 EVENT(XCB_MAPPING_NOTIFY, mappingnotify);
9597 EVENT(XCB_MOTION_NOTIFY, motionnotify);
9598 /*EVENT(XCB_NO_EXPOSURE, );*/
9599 EVENT(XCB_PROPERTY_NOTIFY, propertynotify);
9600 /*EVENT(XCB_REPARENT_NOTIFY, );*/
9601 /*EVENT(XCB_RESIZE_REQUEST, );*/
9602 /*EVENT(XCB_SELECTION_CLEAR, );*/
9603 /*EVENT(XCB_SELECTION_NOTIFY, );*/
9604 /*EVENT(XCB_SELECTION_REQUEST, );*/
9605 EVENT(XCB_UNMAP_NOTIFY, unmapnotify);
9606 /*EVENT(XCB_VISIBILITY_NOTIFY, visibilitynotify);*/
9609 if (type - xrandr_eventbase == XCB_RANDR_SCREEN_CHANGE_NOTIFY)
9610 screenchange((void *)evt);
9614 main(int argc, char *argv[])
9616 struct swm_region *r;
9617 char conf[PATH_MAX], *cfile = NULL;
9619 int xfd, i, num_screens, startup = 1;
9620 struct sigaction sact;
9621 xcb_generic_event_t *evt;
9625 int stdin_ready = 0;
9628 /* suppress unused warning since var is needed */
9631 time_started = time(NULL);
9634 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION, buildstr);
9635 if (!setlocale(LC_CTYPE, "") || !setlocale(LC_TIME, ""))
9636 warnx("no locale support");
9638 /* handle some signals */
9639 bzero(&sact, sizeof(sact));
9640 sigemptyset(&sact.sa_mask);
9642 sact.sa_handler = sighdlr;
9643 sigaction(SIGINT, &sact, NULL);
9644 sigaction(SIGQUIT, &sact, NULL);
9645 sigaction(SIGTERM, &sact, NULL);
9646 sigaction(SIGHUP, &sact, NULL);
9648 sact.sa_handler = sighdlr;
9649 sact.sa_flags = SA_NOCLDSTOP;
9650 sigaction(SIGCHLD, &sact, NULL);
9652 if (!(display = XOpenDisplay(0)))
9653 errx(1, "can not open display");
9655 conn = XGetXCBConnection(display);
9656 if (xcb_connection_has_error(conn))
9657 errx(1, "can not get XCB connection");
9659 XSetEventQueueOwner(display, XCBOwnsEventQueue);
9661 xcb_prefetch_extension_data(conn, &xcb_randr_id);
9662 xfd = xcb_get_file_descriptor(conn);
9664 /* look for local and global conf file */
9665 pwd = getpwuid(getuid());
9667 errx(1, "invalid user: %d", getuid());
9669 xcb_grab_server(conn);
9672 /* flush all events */
9673 while ((evt = xcb_poll_for_event(conn))) {
9674 if (XCB_EVENT_RESPONSE_TYPE(evt) == 0)
9679 if (enable_wm() != 0)
9680 errx(1, "another window manager is currently running");
9682 /* Load Xcursors and/or cursorfont glyph cursors. */
9694 for (i = 0; ; i++) {
9699 snprintf(conf, sizeof conf, "%s/.%s",
9700 pwd->pw_dir, SWM_CONF_FILE);
9704 snprintf(conf, sizeof conf, "/etc/%s",
9709 snprintf(conf, sizeof conf, "%s/.%s",
9710 pwd->pw_dir, SWM_CONF_FILE_OLD);
9714 snprintf(conf, sizeof conf, "/etc/%s",
9721 if (strlen(conf) && stat(conf, &sb) != -1)
9722 if (S_ISREG(sb.st_mode)) {
9729 /* load conf (if any) */
9731 conf_load(cfile, SWM_CONF_DEFAULT);
9736 /* set some values to work around bad programs */
9738 /* grab existing windows (before we build the bars) */
9741 if (getenv("SWM_STARTED") == NULL)
9742 setenv("SWM_STARTED", "YES", 1);
9744 /* setup all bars */
9745 num_screens = get_screen_count();
9746 for (i = 0; i < num_screens; i++)
9747 TAILQ_FOREACH(r, &screens[i].rl, entry)
9754 xcb_ungrab_server(conn);
9757 rd_max = xfd > STDIN_FILENO ? xfd : STDIN_FILENO;
9760 while ((evt = xcb_poll_for_event(conn))) {
9767 /* If just (re)started, set default focus if needed. */
9771 if (focus_mode != SWM_FOCUS_FOLLOW) {
9772 r = TAILQ_FIRST(&screens[0].rl);
9784 FD_SET(STDIN_FILENO, &rd);
9789 num_readable = select(rd_max + 1, &rd, NULL, NULL, &tv);
9790 if (num_readable == -1 && errno != EINTR) {
9791 DNPRINTF(SWM_D_MISC, "select failed");
9792 } else if (num_readable > 0 && FD_ISSET(STDIN_FILENO, &rd)) {
9797 restart(NULL, NULL);