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_get_wm_hints_reply xcb_get_wm_hints_reply
145 #define xcb_icccm_get_wm_name xcb_get_wm_name
146 #define xcb_icccm_get_wm_name_reply xcb_get_wm_name_reply
147 #define xcb_icccm_get_wm_normal_hints xcb_get_wm_normal_hints
148 #define xcb_icccm_get_wm_normal_hints_reply xcb_get_wm_normal_hints_reply
149 #define xcb_icccm_get_wm_protocols xcb_get_wm_protocols
150 #define xcb_icccm_get_wm_protocols_reply xcb_get_wm_protocols_reply
151 #define xcb_icccm_get_wm_protocols_reply_t xcb_get_wm_protocols_reply_t
152 #define xcb_icccm_get_wm_protocols_reply_wipe xcb_get_wm_protocols_reply_wipe
153 #define xcb_icccm_get_wm_transient_for xcb_get_wm_transient_for
154 #define xcb_icccm_get_wm_transient_for_reply xcb_get_wm_transient_for_reply
155 #define xcb_icccm_wm_hints_t xcb_wm_hints_t
158 /*#define SWM_DEBUG*/
160 #define DPRINTF(x...) do { \
162 fprintf(stderr, x); \
164 #define DNPRINTF(n,x...) do { \
165 if (swm_debug & n) { \
166 fprintf(stderr, "%ld ", (long)(time(NULL) - time_started)); \
167 fprintf(stderr, x); \
170 #define SWM_D_MISC 0x0001
171 #define SWM_D_EVENT 0x0002
172 #define SWM_D_WS 0x0004
173 #define SWM_D_FOCUS 0x0008
174 #define SWM_D_MOVE 0x0010
175 #define SWM_D_STACK 0x0020
176 #define SWM_D_MOUSE 0x0040
177 #define SWM_D_PROP 0x0080
178 #define SWM_D_CLASS 0x0100
179 #define SWM_D_KEY 0x0200
180 #define SWM_D_QUIRK 0x0400
181 #define SWM_D_SPAWN 0x0800
182 #define SWM_D_EVENTQ 0x1000
183 #define SWM_D_CONF 0x2000
184 #define SWM_D_BAR 0x4000
185 #define SWM_D_INIT 0x8000
187 u_int32_t swm_debug = 0
206 #define DPRINTF(x...)
207 #define DNPRINTF(n,x...)
210 /* convert 8-bit to 16-bit */
211 #define RGB_8_TO_16(col) ((col) << 8) + (col)
213 #define PIXEL_TO_XRENDERCOLOR(px, xrc) \
214 xrc.red = RGB_8_TO_16((px) >> 16 & 0xff); \
215 xrc.green = RGB_8_TO_16((px) >> 8 & 0xff); \
216 xrc.blue = RGB_8_TO_16((px) & 0xff); \
219 #define LENGTH(x) (int)(sizeof (x) / sizeof (x)[0])
220 #define MODKEY XCB_MOD_MASK_1
221 #define CLEANMASK(mask) ((mask) & ~(numlockmask | XCB_MOD_MASK_LOCK))
222 #define BUTTONMASK (XCB_EVENT_MASK_BUTTON_PRESS | \
223 XCB_EVENT_MASK_BUTTON_RELEASE)
224 #define MOUSEMASK (BUTTONMASK|XCB_EVENT_MASK_POINTER_MOTION)
225 #define SWM_PROPLEN (16)
226 #define SWM_FUNCNAME_LEN (32)
227 #define SWM_KEYS_LEN (255)
228 #define SWM_QUIRK_LEN (64)
229 #define X(r) (r)->g.x
230 #define Y(r) (r)->g.y
231 #define WIDTH(r) (r)->g.w
232 #define HEIGHT(r) (r)->g.h
233 #define BORDER(w) ((w)->bordered ? border_width : 0)
234 #define MAX_X(r) ((r)->g.x + (r)->g.w)
235 #define MAX_Y(r) ((r)->g.y + (r)->g.h)
236 #define SH_MIN(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE
237 #define SH_MIN_W(w) (w)->sh.min_width
238 #define SH_MIN_H(w) (w)->sh.min_height
239 #define SH_MAX(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MAX_SIZE
240 #define SH_MAX_W(w) (w)->sh.max_width
241 #define SH_MAX_H(w) (w)->sh.max_height
242 #define SH_INC(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_RESIZE_INC
243 #define SH_INC_W(w) (w)->sh.width_inc
244 #define SH_INC_H(w) (w)->sh.height_inc
245 #define SWM_MAX_FONT_STEPS (3)
246 #define WINID(w) ((w) ? (w)->id : XCB_WINDOW_NONE)
247 #define WS_FOCUSED(ws) ((ws)->r && (ws)->r->s->r_focus == (ws)->r)
248 #define YESNO(x) ((x) ? "yes" : "no")
250 /* Constrain Window flags */
251 #define SWM_CW_RESIZABLE (0x01)
252 #define SWM_CW_SOFTBOUNDARY (0x02)
253 #define SWM_CW_HARDBOUNDARY (0x04)
254 #define SWM_CW_RIGHT (0x10)
255 #define SWM_CW_LEFT (0x20)
256 #define SWM_CW_BOTTOM (0x40)
257 #define SWM_CW_TOP (0x80)
258 #define SWM_CW_ALLSIDES (0xf0)
260 #define SWM_FOCUS_DEFAULT (0)
261 #define SWM_FOCUS_FOLLOW (1)
262 #define SWM_FOCUS_MANUAL (2)
264 #define SWM_CK_NONE 0
265 #define SWM_CK_ALL 0xf
266 #define SWM_CK_FOCUS 0x1
267 #define SWM_CK_POINTER 0x2
268 #define SWM_CK_FALLBACK 0x4
269 #define SWM_CK_REGION 0x8
271 #define SWM_CONF_DEFAULT (0)
272 #define SWM_CONF_KEYMAPPING (1)
275 #define SWM_LIB "/usr/local/lib/libswmhack.so"
282 xcb_atom_t a_takefocus;
283 xcb_atom_t a_utf8_string;
284 xcb_atom_t a_swm_iconic;
286 volatile sig_atomic_t running = 1;
287 volatile sig_atomic_t restart_wm = 0;
288 xcb_timestamp_t last_event_time = 0;
292 int xrandr_eventbase;
293 unsigned int numlockmask = 0;
296 xcb_connection_t *conn;
297 xcb_key_symbols_t *syms;
299 int boundary_width = 50;
301 int cycle_visible = 0;
303 int font_adjusted = 0;
304 unsigned int mod_key = MODKEY;
307 struct swm_region *search_r;
308 int select_list_pipe[2];
309 int select_resp_pipe[2];
311 volatile sig_atomic_t search_resp;
312 int search_resp_action;
314 struct search_window {
315 TAILQ_ENTRY(search_window) entry;
319 xcb_window_t indicator;
321 TAILQ_HEAD(search_winlist, search_window);
322 struct search_winlist search_wl;
327 SWM_SEARCH_UNICONIFY,
328 SWM_SEARCH_NAME_WORKSPACE,
329 SWM_SEARCH_SEARCH_WORKSPACE,
330 SWM_SEARCH_SEARCH_WINDOW
333 #define SWM_STACK_TOP (0)
334 #define SWM_STACK_BOTTOM (1)
335 #define SWM_STACK_ABOVE (2)
336 #define SWM_STACK_BELOW (3)
339 double dialog_ratio = 0.6;
341 #define SWM_BAR_MAX (256)
342 #define SWM_BAR_JUSTIFY_LEFT (0)
343 #define SWM_BAR_JUSTIFY_CENTER (1)
344 #define SWM_BAR_JUSTIFY_RIGHT (2)
345 #define SWM_BAR_OFFSET (4)
346 #define SWM_BAR_FONTS "-*-terminus-medium-*-*-*-12-*-*-*-*-*-*-*," \
347 "-*-profont-*-*-*-*-12-*-*-*-*-*-*-*," \
348 "-*-times-medium-r-*-*-12-*-*-*-*-*-*-*," \
349 "-misc-fixed-medium-r-*-*-12-*-*-*-*-*-*-*," \
350 "-*-*-*-r-*-*-*-*-*-*-*-*-*-*"
352 #ifdef X_HAVE_UTF8_STRING
353 #define DRAWSTRING(x...) Xutf8DrawString(x)
355 #define DRAWSTRING(x...) XmbDrawString(x)
358 char *bar_argv[] = { NULL, NULL };
360 char bar_ext[SWM_BAR_MAX];
361 char bar_ext_buf[SWM_BAR_MAX];
362 char bar_vertext[SWM_BAR_MAX];
365 int bar_border_width = 1;
366 int bar_at_bottom = 0;
370 int bar_justify = SWM_BAR_JUSTIFY_LEFT;
371 char *bar_format = NULL;
372 int stack_enabled = 1;
373 int clock_enabled = 1;
374 int urgent_enabled = 0;
375 char *clock_format = NULL;
376 int title_name_enabled = 0;
377 int title_class_enabled = 0;
378 int window_name_enabled = 0;
379 int focus_mode = SWM_FOCUS_DEFAULT;
380 int focus_close = SWM_STACK_BELOW;
381 int focus_close_wrap = 1;
382 int focus_default = SWM_STACK_TOP;
383 int spawn_position = SWM_STACK_TOP;
384 int disable_border = 0;
385 int border_width = 1;
386 int region_padding = 0;
388 int verbose_layout = 0;
392 XFontSetExtents *bar_fs_extents;
394 int bar_font_legacy = 1;
396 XftColor bar_font_color;
398 char *startup_exception;
399 unsigned int nr_exceptions = 0;
401 /* layout manager data */
402 struct swm_geometry {
415 struct swm_geometry g;
418 /* virtual "screens" */
420 TAILQ_ENTRY(swm_region) entry;
422 struct swm_geometry g;
423 struct workspace *ws; /* current workspace on this region */
424 struct workspace *ws_prior; /* prior workspace on this region */
425 struct swm_screen *s; /* screen idx */
428 TAILQ_HEAD(swm_region_list, swm_region);
431 TAILQ_ENTRY(ws_win) entry;
433 xcb_window_t transient;
434 struct ws_win *focus_child; /* focus on child transient */
435 struct swm_geometry g; /* current geometry */
436 struct swm_geometry g_float; /* region coordinates */
437 int g_floatvalid; /* g_float geometry validity */
438 int floatmaxed; /* whether maxed by max_stack */
444 unsigned int ewmh_flags;
445 int font_size_boundary[SWM_MAX_FONT_STEPS];
451 unsigned long quirks;
452 struct workspace *ws; /* always valid */
453 struct swm_screen *s; /* always valid, never changes */
454 xcb_get_geometry_reply_t *wa;
456 xcb_icccm_get_wm_class_reply_t ch;
457 xcb_icccm_wm_hints_t hints;
459 TAILQ_HEAD(ws_win_list, ws_win);
463 TAILQ_ENTRY(pid_e) entry;
467 TAILQ_HEAD(pid_list, pid_e);
468 struct pid_list pidlist = TAILQ_HEAD_INITIALIZER(pidlist);
470 /* layout handlers */
472 void vertical_config(struct workspace *, int);
473 void vertical_stack(struct workspace *, struct swm_geometry *);
474 void horizontal_config(struct workspace *, int);
475 void horizontal_stack(struct workspace *, struct swm_geometry *);
476 void max_stack(struct workspace *, struct swm_geometry *);
477 void plain_stacker(struct workspace *);
478 void fancy_stacker(struct workspace *);
481 void (*l_stack)(struct workspace *, struct swm_geometry *);
482 void (*l_config)(struct workspace *, int);
484 #define SWM_L_FOCUSPREV (1<<0)
485 #define SWM_L_MAPONFOCUS (1<<1)
486 void (*l_string)(struct workspace *);
488 /* stack, configure */
489 { vertical_stack, vertical_config, 0, plain_stacker },
490 { horizontal_stack, horizontal_config, 0, plain_stacker },
492 SWM_L_MAPONFOCUS | SWM_L_FOCUSPREV, plain_stacker },
493 { NULL, NULL, 0, NULL },
496 /* position of max_stack mode in the layouts array, index into layouts! */
497 #define SWM_V_STACK (0)
498 #define SWM_H_STACK (1)
499 #define SWM_MAX_STACK (2)
501 #define SWM_H_SLICE (32)
502 #define SWM_V_SLICE (32)
504 /* define work spaces */
506 int idx; /* workspace index */
507 char *name; /* workspace name */
508 int always_raise; /* raise windows on focus */
509 int bar_enabled; /* bar visibility */
510 struct layout *cur_layout; /* current layout handlers */
511 struct ws_win *focus; /* may be NULL */
512 struct ws_win *focus_prev; /* may be NULL */
513 struct ws_win *focus_pending; /* may be NULL */
514 struct swm_region *r; /* may be NULL */
515 struct swm_region *old_r; /* may be NULL */
516 struct ws_win_list winlist; /* list of windows in ws */
517 struct ws_win_list unmanagedlist; /* list of dead windows in ws */
518 char stacker[10]; /* display stacker and layout */
522 int horizontal_msize;
524 int horizontal_stacks;
535 SWM_S_COLOR_BAR_BORDER,
536 SWM_S_COLOR_BAR_BORDER_UNFOCUS,
537 SWM_S_COLOR_BAR_FONT,
543 /* physical screen mapping */
544 #define SWM_WS_MAX (22) /* hard limit */
545 int workspace_limit = 10; /* soft limit */
548 int idx; /* screen index */
549 struct swm_region_list rl; /* list of regions on this screen */
550 struct swm_region_list orl; /* list of old regions */
552 struct workspace ws[SWM_WS_MAX];
553 struct swm_region *r_focus;
559 } c[SWM_S_COLOR_MAX];
561 xcb_gcontext_t bar_gc;
564 struct swm_screen *screens;
566 /* args to functions */
569 #define SWM_ARG_ID_FOCUSNEXT (0)
570 #define SWM_ARG_ID_FOCUSPREV (1)
571 #define SWM_ARG_ID_FOCUSMAIN (2)
572 #define SWM_ARG_ID_FOCUSURGENT (3)
573 #define SWM_ARG_ID_SWAPNEXT (10)
574 #define SWM_ARG_ID_SWAPPREV (11)
575 #define SWM_ARG_ID_SWAPMAIN (12)
576 #define SWM_ARG_ID_MOVELAST (13)
577 #define SWM_ARG_ID_MASTERSHRINK (20)
578 #define SWM_ARG_ID_MASTERGROW (21)
579 #define SWM_ARG_ID_MASTERADD (22)
580 #define SWM_ARG_ID_MASTERDEL (23)
581 #define SWM_ARG_ID_FLIPLAYOUT (24)
582 #define SWM_ARG_ID_STACKRESET (30)
583 #define SWM_ARG_ID_STACKINIT (31)
584 #define SWM_ARG_ID_CYCLEWS_UP (40)
585 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
586 #define SWM_ARG_ID_CYCLERG_UP (42)
587 #define SWM_ARG_ID_CYCLERG_DOWN (43)
588 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
589 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
590 #define SWM_ARG_ID_CYCLEWS_MOVE_UP (46)
591 #define SWM_ARG_ID_CYCLEWS_MOVE_DOWN (47)
592 #define SWM_ARG_ID_STACKINC (50)
593 #define SWM_ARG_ID_STACKDEC (51)
594 #define SWM_ARG_ID_SS_ALL (60)
595 #define SWM_ARG_ID_SS_WINDOW (61)
596 #define SWM_ARG_ID_DONTCENTER (70)
597 #define SWM_ARG_ID_CENTER (71)
598 #define SWM_ARG_ID_KILLWINDOW (80)
599 #define SWM_ARG_ID_DELETEWINDOW (81)
600 #define SWM_ARG_ID_WIDTHGROW (90)
601 #define SWM_ARG_ID_WIDTHSHRINK (91)
602 #define SWM_ARG_ID_HEIGHTGROW (92)
603 #define SWM_ARG_ID_HEIGHTSHRINK (93)
604 #define SWM_ARG_ID_MOVEUP (100)
605 #define SWM_ARG_ID_MOVEDOWN (101)
606 #define SWM_ARG_ID_MOVELEFT (102)
607 #define SWM_ARG_ID_MOVERIGHT (103)
608 #define SWM_ARG_ID_BAR_TOGGLE (110)
609 #define SWM_ARG_ID_BAR_TOGGLE_WS (111)
615 TAILQ_ENTRY(quirk) entry;
619 #define SWM_Q_FLOAT (1<<0) /* float this window */
620 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
621 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
622 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
623 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
624 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
625 #define SWM_Q_NOFOCUSONMAP (1<<6) /* Don't focus on window when mapped. */
626 #define SWM_Q_FOCUSONMAP_SINGLE (1<<7) /* Only focus if single win of type. */
628 TAILQ_HEAD(quirk_list, quirk);
629 struct quirk_list quirks = TAILQ_HEAD_INITIALIZER(quirks);
632 * Supported EWMH hints should be added to
633 * both the enum and the ewmh array
638 _NET_MOVERESIZE_WINDOW,
639 _NET_WM_ACTION_ABOVE,
640 _NET_WM_ACTION_CLOSE,
641 _NET_WM_ACTION_FULLSCREEN,
643 _NET_WM_ACTION_RESIZE,
644 _NET_WM_ALLOWED_ACTIONS,
648 _NET_WM_STATE_FULLSCREEN,
649 _NET_WM_STATE_HIDDEN,
650 _NET_WM_STATE_MAXIMIZED_HORZ,
651 _NET_WM_STATE_MAXIMIZED_VERT,
652 _NET_WM_STATE_SKIP_PAGER,
653 _NET_WM_STATE_SKIP_TASKBAR,
655 _NET_WM_WINDOW_TYPE_DIALOG,
656 _NET_WM_WINDOW_TYPE_DOCK,
657 _NET_WM_WINDOW_TYPE_NORMAL,
658 _NET_WM_WINDOW_TYPE_SPLASH,
659 _NET_WM_WINDOW_TYPE_TOOLBAR,
660 _NET_WM_WINDOW_TYPE_UTILITY,
661 _SWM_WM_STATE_MANUAL,
668 } ewmh[SWM_EWMH_HINT_MAX] = {
669 /* must be in same order as in the enum */
670 {"_NET_ACTIVE_WINDOW", XCB_ATOM_NONE},
671 {"_NET_CLOSE_WINDOW", XCB_ATOM_NONE},
672 {"_NET_MOVERESIZE_WINDOW", XCB_ATOM_NONE},
673 {"_NET_WM_ACTION_ABOVE", XCB_ATOM_NONE},
674 {"_NET_WM_ACTION_CLOSE", XCB_ATOM_NONE},
675 {"_NET_WM_ACTION_FULLSCREEN", XCB_ATOM_NONE},
676 {"_NET_WM_ACTION_MOVE", XCB_ATOM_NONE},
677 {"_NET_WM_ACTION_RESIZE", XCB_ATOM_NONE},
678 {"_NET_WM_ALLOWED_ACTIONS", XCB_ATOM_NONE},
679 {"_NET_WM_NAME", XCB_ATOM_NONE},
680 {"_NET_WM_STATE", XCB_ATOM_NONE},
681 {"_NET_WM_STATE_ABOVE", XCB_ATOM_NONE},
682 {"_NET_WM_STATE_FULLSCREEN", XCB_ATOM_NONE},
683 {"_NET_WM_STATE_HIDDEN", XCB_ATOM_NONE},
684 {"_NET_WM_STATE_MAXIMIZED_HORZ", XCB_ATOM_NONE},
685 {"_NET_WM_STATE_MAXIMIZED_VERT", XCB_ATOM_NONE},
686 {"_NET_WM_STATE_SKIP_PAGER", XCB_ATOM_NONE},
687 {"_NET_WM_STATE_SKIP_TASKBAR", XCB_ATOM_NONE},
688 {"_NET_WM_WINDOW_TYPE", XCB_ATOM_NONE},
689 {"_NET_WM_WINDOW_TYPE_DIALOG", XCB_ATOM_NONE},
690 {"_NET_WM_WINDOW_TYPE_DOCK", XCB_ATOM_NONE},
691 {"_NET_WM_WINDOW_TYPE_NORMAL", XCB_ATOM_NONE},
692 {"_NET_WM_WINDOW_TYPE_SPLASH", XCB_ATOM_NONE},
693 {"_NET_WM_WINDOW_TYPE_TOOLBAR", XCB_ATOM_NONE},
694 {"_NET_WM_WINDOW_TYPE_UTILITY", XCB_ATOM_NONE},
695 {"_SWM_WM_STATE_MANUAL", XCB_ATOM_NONE},
702 XC_BOTTOM_LEFT_CORNER,
703 XC_BOTTOM_RIGHT_CORNER,
711 char *name; /* Name used by Xcursor .*/
712 uint8_t cf_char; /* cursorfont index. */
714 } cursors[XC_MAX] = {
715 {"fleur", XC_fleur, XCB_CURSOR_NONE},
716 {"left_ptr", XC_left_ptr, XCB_CURSOR_NONE},
717 {"bottom_left_corner", XC_bottom_left_corner, XCB_CURSOR_NONE},
718 {"bottom_right_corner", XC_bottom_right_corner, XCB_CURSOR_NONE},
719 {"sizing", XC_sizing, XCB_CURSOR_NONE},
720 {"top_left_corner", XC_top_left_corner, XCB_CURSOR_NONE},
721 {"top_right_corner", XC_top_right_corner, XCB_CURSOR_NONE},
724 #define SWM_SPAWN_OPTIONAL 0x1
728 TAILQ_ENTRY(spawn_prog) entry;
734 TAILQ_HEAD(spawn_list, spawn_prog);
735 struct spawn_list spawns = TAILQ_HEAD_INITIALIZER(spawns);
737 /* user/key callable function IDs */
852 KF_DUMPWINS, /* MUST BE LAST */
860 enum keyfuncid funcid;
863 RB_HEAD(key_tree, key);
865 /* function prototypes */
866 void adjust_font(struct ws_win *);
867 void bar_class_name(char *, size_t, struct swm_region *);
868 void bar_class_title_name(char *, size_t, struct swm_region *);
869 void bar_cleanup(struct swm_region *);
870 void bar_extra_setup(void);
871 void bar_extra_stop(void);
872 int bar_extra_update(void);
873 void bar_fmt(const char *, char *, struct swm_region *, size_t);
874 void bar_fmt_expand(char *, size_t);
876 void bar_print(struct swm_region *, const char *);
877 void bar_print_legacy(struct swm_region *, const char *);
878 void bar_replace(char *, char *, struct swm_region *, size_t);
879 void bar_replace_pad(char *, int *, size_t);
880 char *bar_replace_seq(char *, char *, struct swm_region *, size_t *, size_t);
881 void bar_setup(struct swm_region *);
882 void bar_title_name(char *, size_t, struct swm_region *);
883 void bar_toggle(struct swm_region *, union arg *);
884 void bar_urgent(char *, size_t);
885 void bar_window_float(char *, size_t, struct swm_region *);
886 void bar_window_name(char *, size_t, struct swm_region *);
887 void bar_workspace_name(char *, size_t, struct swm_region *);
888 void buttonpress(xcb_button_press_event_t *);
889 void check_conn(void);
890 void clear_keys(void);
891 void clientmessage(xcb_client_message_event_t *);
892 void client_msg(struct ws_win *, xcb_atom_t, xcb_timestamp_t);
893 int conf_load(const char *, int);
894 void configurenotify(xcb_configure_notify_event_t *);
895 void configurerequest(xcb_configure_request_event_t *);
896 void config_win(struct ws_win *, xcb_configure_request_event_t *);
897 void constrain_window(struct ws_win *, struct swm_geometry *, int *);
898 int count_win(struct workspace *, int);
899 void cursors_cleanup(void);
900 void cursors_load(void);
901 void custom_region(char *);
902 void cyclerg(struct swm_region *, union arg *);
903 void cyclews(struct swm_region *, union arg *);
904 void cycle_layout(struct swm_region *, union arg *);
905 void destroynotify(xcb_destroy_notify_event_t *);
906 void dumpwins(struct swm_region *, union arg *);
908 void enternotify(xcb_enter_notify_event_t *);
909 void event_drain(uint8_t);
910 void event_error(xcb_generic_error_t *);
911 void event_handle(xcb_generic_event_t *);
912 void ewmh_autoquirk(struct ws_win *);
913 void ewmh_get_win_state(struct ws_win *);
914 int ewmh_set_win_fullscreen(struct ws_win *, int);
915 void ewmh_update_actions(struct ws_win *);
916 void ewmh_update_win_state(struct ws_win *, xcb_atom_t, long);
917 char *expand_tilde(const char *);
918 void expose(xcb_expose_event_t *);
919 void fake_keypress(struct ws_win *, xcb_keysym_t, uint16_t);
920 struct pid_e *find_pid(pid_t);
921 struct ws_win *find_unmanaged_window(xcb_window_t);
922 struct ws_win *find_window(xcb_window_t);
923 void floating_toggle(struct swm_region *, union arg *);
924 int floating_toggle_win(struct ws_win *);
925 void focus(struct swm_region *, union arg *);
927 void focusin(xcb_focus_in_event_t *);
928 void focusout(xcb_focus_out_event_t *);
930 void focus_flush(void);
931 void focus_region(struct swm_region *);
932 void focusrg(struct swm_region *, union arg *);
933 void focus_win(struct ws_win *);
934 void fontset_init(void);
935 void free_window(struct ws_win *);
936 xcb_atom_t get_atom_from_string(const char *);
938 char *get_atom_name(xcb_atom_t);
940 struct ws_win *get_focus_magic(struct ws_win *);
941 struct ws_win *get_focus_prev(struct ws_win *);
943 char *get_notify_detail_label(uint8_t);
944 char *get_notify_mode_label(uint8_t);
946 struct ws_win *get_pointer_win(xcb_window_t);
947 struct ws_win *get_region_focus(struct swm_region *);
948 int get_region_index(struct swm_region *);
949 xcb_screen_t *get_screen(int);
950 xcb_window_t get_sibling(struct ws_win *, int);
951 int get_screen_count(void);
953 char *get_stack_mode_name(uint8_t);
955 int32_t get_swm_iconic(struct ws_win *);
956 char *get_win_name(xcb_window_t);
957 int get_ws_idx(xcb_window_t);
958 uint32_t getstate(xcb_window_t);
959 void grabbuttons(struct ws_win *);
961 void grab_windows(void);
962 void iconify(struct swm_region *, union arg *);
964 void keypress(xcb_key_press_event_t *);
965 int key_cmp(struct key *, struct key *);
966 void key_insert(unsigned int, KeySym, enum keyfuncid, const char *);
967 struct key *key_lookup(unsigned int, KeySym);
968 void key_remove(struct key *);
969 void key_replace(struct key *, unsigned int, KeySym, enum keyfuncid,
971 void kill_bar_extra_atexit(void);
972 void kill_refs(struct ws_win *);
974 void leavenotify(xcb_leave_notify_event_t *);
976 void load_float_geom(struct ws_win *);
977 struct ws_win *manage_window(xcb_window_t, uint16_t);
978 void map_window(struct ws_win *, xcb_window_t);
979 void mapnotify(xcb_map_notify_event_t *);
980 void mappingnotify(xcb_mapping_notify_event_t *);
981 void maprequest(xcb_map_request_event_t *);
982 void motionnotify(xcb_motion_notify_event_t *);
983 void move(struct ws_win *, union arg *);
984 void move_step(struct swm_region *, union arg *);
985 uint32_t name_to_pixel(int, const char *);
986 void name_workspace(struct swm_region *, union arg *);
987 void new_region(struct swm_screen *, int, int, int, int);
988 int parsekeys(char *, unsigned int, unsigned int *, KeySym *);
989 int parsequirks(char *, unsigned long *);
990 int parse_rgb(const char *, uint16_t *, uint16_t *, uint16_t *);
991 void pressbutton(struct swm_region *, union arg *);
992 void priorws(struct swm_region *, union arg *);
994 void print_win_geom(xcb_window_t);
996 void propertynotify(xcb_property_notify_event_t *);
997 void quirk_insert(const char *, const char *, unsigned long);
998 void quirk_remove(struct quirk *);
999 void quirk_replace(struct quirk *, const char *, const char *,
1001 void quit(struct swm_region *, union arg *);
1002 void raise_toggle(struct swm_region *, union arg *);
1003 void region_containment(struct ws_win *, struct swm_region *, int);
1004 struct swm_region *region_under(struct swm_screen *, int, int);
1005 void regionize(struct ws_win *, int, int);
1006 void resize(struct ws_win *, union arg *);
1007 void resize_step(struct swm_region *, union arg *);
1008 void restart(struct swm_region *, union arg *);
1009 struct swm_region *root_to_region(xcb_window_t, int);
1010 void screenchange(xcb_randr_screen_change_notify_event_t *);
1011 void scan_xrandr(int);
1012 void search_do_resp(void);
1013 void search_resp_name_workspace(const char *, size_t);
1014 void search_resp_search_window(const char *);
1015 void search_resp_search_workspace(const char *);
1016 void search_resp_uniconify(const char *, size_t);
1017 void search_win(struct swm_region *, union arg *);
1018 void search_win_cleanup(void);
1019 void search_workspace(struct swm_region *, union arg *);
1020 void send_to_rg(struct swm_region *, union arg *);
1021 void send_to_ws(struct swm_region *, union arg *);
1022 void set_region(struct swm_region *);
1023 int setautorun(char *, char *, int);
1024 int setconfbinding(char *, char *, int);
1025 int setconfcolor(char *, char *, int);
1026 int setconfmodkey(char *, char *, int);
1027 int setconfquirk(char *, char *, int);
1028 int setconfregion(char *, char *, int);
1029 int setconfspawn(char *, char *, int);
1030 int setconfvalue(char *, char *, int);
1031 void setkeybinding(unsigned int, KeySym, enum keyfuncid, const char *);
1032 int setkeymapping(char *, char *, int);
1033 int setlayout(char *, char *, int);
1034 void setquirk(const char *, const char *, unsigned long);
1035 void setscreencolor(char *, int, int);
1036 void setspawn(const char *, const char *, int);
1037 void setup_ewmh(void);
1038 void setup_globals(void);
1039 void setup_keys(void);
1040 void setup_quirks(void);
1041 void setup_screens(void);
1042 void setup_spawn(void);
1043 void set_child_transient(struct ws_win *, xcb_window_t *);
1044 void set_swm_iconic(struct ws_win *, int);
1045 void set_win_state(struct ws_win *, uint16_t);
1046 void shutdown_cleanup(void);
1048 void socket_setnonblock(int);
1049 void sort_windows(struct ws_win_list *);
1050 void spawn(int, union arg *, int);
1051 void spawn_custom(struct swm_region *, union arg *, const char *);
1052 int spawn_expand(struct swm_region *, union arg *, const char *, char ***);
1053 void spawn_insert(const char *, const char *, int);
1054 struct spawn_prog *spawn_find(const char *);
1055 void spawn_remove(struct spawn_prog *);
1056 void spawn_replace(struct spawn_prog *, const char *, const char *, int);
1057 void spawn_select(struct swm_region *, union arg *, const char *, int *);
1058 void stack_config(struct swm_region *, union arg *);
1059 void stack_floater(struct ws_win *, struct swm_region *);
1060 void stack_master(struct workspace *, struct swm_geometry *, int, int);
1061 void store_float_geom(struct ws_win *);
1062 char *strdupsafe(const char *);
1063 void swapwin(struct swm_region *, union arg *);
1064 void switchws(struct swm_region *, union arg *);
1065 void teardown_ewmh(void);
1066 void unfocus_win(struct ws_win *);
1067 void uniconify(struct swm_region *, union arg *);
1068 void unmanage_window(struct ws_win *);
1069 void unmapnotify(xcb_unmap_notify_event_t *);
1070 void unmap_all(void);
1071 void unmap_window(struct ws_win *);
1072 void updatenumlockmask(void);
1073 void update_modkey(unsigned int);
1074 void update_window(struct ws_win *);
1075 void validate_spawns(void);
1076 int validate_win(struct ws_win *);
1077 int validate_ws(struct workspace *);
1078 /*void visibilitynotify(xcb_visibility_notify_event_t *);*/
1079 void version(struct swm_region *, union arg *);
1080 void win_to_ws(struct ws_win *, int, int);
1081 pid_t window_get_pid(xcb_window_t);
1082 void wkill(struct swm_region *, union arg *);
1083 void workaround(void);
1084 void xft_init(struct swm_region *);
1085 void _add_startup_exception(const char *, va_list);
1086 void add_startup_exception(const char *, ...);
1088 RB_PROTOTYPE(key_tree, key, entry, key_cmp);
1089 RB_GENERATE(key_tree, key, entry, key_cmp);
1090 struct key_tree keys;
1095 xcb_font_t cf = XCB_NONE;
1098 for (i = 0; i < LENGTH(cursors); ++i) {
1099 /* try to load Xcursor first. */
1100 cursors[i].cid = XcursorLibraryLoadCursor(display,
1103 /* fallback to cursorfont. */
1104 if (cursors[i].cid == XCB_CURSOR_NONE) {
1105 if (cf == XCB_NONE) {
1106 cf = xcb_generate_id(conn);
1107 xcb_open_font(conn, cf, strlen("cursor"),
1111 cursors[i].cid = xcb_generate_id(conn);
1112 xcb_create_glyph_cursor(conn, cursors[i].cid, cf, cf,
1113 cursors[i].cf_char, cursors[i].cf_char + 1, 0, 0, 0,
1114 0xffff, 0xffff, 0xffff);
1120 xcb_close_font(conn, cf);
1124 cursors_cleanup(void)
1127 for (i = 0; i < LENGTH(cursors); ++i)
1128 xcb_free_cursor(conn, cursors[i].cid);
1132 expand_tilde(const char *s)
1134 struct passwd *ppwd;
1142 errx(1, "expand_tilde: NULL string.");
1145 result = strdup(sc);
1151 if ((max = sysconf(_SC_LOGIN_NAME_MAX)) == -1)
1152 errx(1, "expand_tilde: sysconf");
1154 if ((user = calloc(1, max + 1)) == NULL)
1155 errx(1, "expand_tilde: calloc");
1157 for (i = 0; s[i] != '/' && s[i] != '\0'; ++i)
1162 ppwd = strlen(user) == 0 ? getpwuid(getuid()) : getpwnam(user);
1166 result = strdup(sc);
1168 if (asprintf(&result, "%s%s", ppwd->pw_dir, s) == -1)
1172 errx(1, "expand_tilde: failed to allocate memory.");
1178 parse_rgb(const char *rgb, uint16_t *rr, uint16_t *gg, uint16_t *bb)
1180 unsigned int tmpr, tmpg, tmpb;
1182 if (sscanf(rgb, "rgb:%x/%x/%x", &tmpr, &tmpg, &tmpb) != 3)
1185 *rr = RGB_8_TO_16(tmpr);
1186 *gg = RGB_8_TO_16(tmpg);
1187 *bb = RGB_8_TO_16(tmpb);
1193 get_screen(int screen)
1195 const xcb_setup_t *r;
1196 xcb_screen_iterator_t iter;
1198 if ((r = xcb_get_setup(conn)) == NULL) {
1199 DNPRINTF(SWM_D_MISC, "get_screen: xcb_get_setup\n");
1203 iter = xcb_setup_roots_iterator(r);
1204 for (; iter.rem; --screen, xcb_screen_next(&iter))
1212 get_screen_count(void)
1214 const xcb_setup_t *r;
1216 if ((r = xcb_get_setup(conn)) == NULL) {
1217 DNPRINTF(SWM_D_MISC, "get_screen_count: xcb_get_setup\n");
1221 return xcb_setup_roots_length(r);
1225 get_region_index(struct swm_region *r)
1227 struct swm_region *rr;
1233 TAILQ_FOREACH(rr, &r->s->rl, entry) {
1248 if (focus_mode == SWM_FOCUS_DEFAULT)
1249 event_drain(XCB_ENTER_NOTIFY);
1255 get_atom_from_string(const char *str)
1257 xcb_intern_atom_cookie_t c;
1258 xcb_intern_atom_reply_t *r;
1261 c = xcb_intern_atom(conn, 0, strlen(str), str);
1262 r = xcb_intern_atom_reply(conn, c, NULL);
1270 return (XCB_ATOM_NONE);
1274 set_swm_iconic(struct ws_win *win, int newv)
1281 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
1282 a_swm_iconic, XCB_ATOM_INTEGER, 32, 1, &v);
1284 xcb_delete_property(conn, win->id, a_swm_iconic);
1288 get_swm_iconic(struct ws_win *win)
1291 xcb_get_property_reply_t *pr = NULL;
1293 pr = xcb_get_property_reply(conn,
1294 xcb_get_property(conn, 0, win->id, a_swm_iconic,
1295 XCB_ATOM_INTEGER, 0, 1), NULL);
1298 if (pr->type != XCB_ATOM_INTEGER || pr->format != 32)
1300 v = *((int32_t *)xcb_get_property_value(pr));
1310 xcb_atom_t sup_list;
1311 int i, j, num_screens;
1313 sup_list = get_atom_from_string("_NET_SUPPORTED");
1315 for (i = 0; i < LENGTH(ewmh); i++)
1316 ewmh[i].atom = get_atom_from_string(ewmh[i].name);
1318 num_screens = get_screen_count();
1319 for (i = 0; i < num_screens; i++) {
1320 /* Support check window will be created by workaround(). */
1322 /* Report supported atoms */
1323 xcb_delete_property(conn, screens[i].root, sup_list);
1324 for (j = 0; j < LENGTH(ewmh); j++)
1325 xcb_change_property(conn, XCB_PROP_MODE_APPEND,
1326 screens[i].root, sup_list, XCB_ATOM_ATOM, 32, 1,
1335 xcb_atom_t sup_check, sup_list;
1337 xcb_get_property_cookie_t pc;
1338 xcb_get_property_reply_t *pr;
1340 sup_check = get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
1341 sup_list = get_atom_from_string("_NET_SUPPORTED");
1342 num_screens = get_screen_count();
1344 for (i = 0; i < num_screens; i++) {
1345 /* Get the support check window and destroy it */
1346 pc = xcb_get_property(conn, 0, screens[i].root, sup_check,
1347 XCB_ATOM_WINDOW, 0, 1);
1348 pr = xcb_get_property_reply(conn, pc, NULL);
1351 if (pr->format == sup_check) {
1352 id = *((xcb_window_t *)xcb_get_property_value(pr));
1354 xcb_destroy_window(conn, id);
1355 xcb_delete_property(conn, screens[i].root, sup_check);
1356 xcb_delete_property(conn, screens[i].root, sup_list);
1363 ewmh_autoquirk(struct ws_win *win)
1367 xcb_get_property_cookie_t c;
1368 xcb_get_property_reply_t *r;
1370 c = xcb_get_property(conn, 0, win->id,
1371 ewmh[_NET_WM_WINDOW_TYPE].atom, XCB_ATOM_ATOM, 0, UINT32_MAX);
1372 r = xcb_get_property_reply(conn, c, NULL);
1376 n = xcb_get_property_value_length(r);
1377 type = xcb_get_property_value(r);
1379 for (i = 0; i < n; i++) {
1380 if (type[i] == ewmh[_NET_WM_WINDOW_TYPE_NORMAL].atom)
1382 if (type[i] == ewmh[_NET_WM_WINDOW_TYPE_DOCK].atom ||
1383 type[i] == ewmh[_NET_WM_WINDOW_TYPE_TOOLBAR].atom ||
1384 type[i] == ewmh[_NET_WM_WINDOW_TYPE_UTILITY].atom) {
1386 win->quirks = SWM_Q_FLOAT | SWM_Q_ANYWHERE;
1389 if (type[i] == ewmh[_NET_WM_WINDOW_TYPE_SPLASH].atom ||
1390 type[i] == ewmh[_NET_WM_WINDOW_TYPE_DIALOG].atom) {
1392 win->quirks = SWM_Q_FLOAT;
1399 #define SWM_EWMH_ACTION_COUNT_MAX (6)
1400 #define EWMH_F_FULLSCREEN (1<<0)
1401 #define EWMH_F_ABOVE (1<<1)
1402 #define EWMH_F_HIDDEN (1<<2)
1403 #define EWMH_F_SKIP_PAGER (1<<3)
1404 #define EWMH_F_SKIP_TASKBAR (1<<4)
1405 #define SWM_F_MANUAL (1<<5)
1408 ewmh_set_win_fullscreen(struct ws_win *win, int fs)
1416 DNPRINTF(SWM_D_MISC, "ewmh_set_win_fullscreen: window: 0x%x, "
1417 "fullscreen %s\n", win->id, YESNO(fs));
1420 if (!win->g_floatvalid)
1421 store_float_geom(win);
1423 win->g = win->ws->r->g;
1426 load_float_geom(win);
1433 ewmh_update_actions(struct ws_win *win)
1435 xcb_atom_t actions[SWM_EWMH_ACTION_COUNT_MAX];
1441 actions[n++] = ewmh[_NET_WM_ACTION_CLOSE].atom;
1443 if (win->floating) {
1444 actions[n++] = ewmh[_NET_WM_ACTION_MOVE].atom;
1445 actions[n++] = ewmh[_NET_WM_ACTION_RESIZE].atom;
1446 actions[n++] = ewmh[_NET_WM_ACTION_ABOVE].atom;
1449 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
1450 ewmh[_NET_WM_ALLOWED_ACTIONS].atom, XCB_ATOM_ATOM, 32, 1, actions);
1453 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
1454 #define _NET_WM_STATE_ADD 1 /* add/set property */
1455 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
1458 ewmh_update_win_state(struct ws_win *win, xcb_atom_t state, long action)
1460 unsigned int mask = 0;
1461 unsigned int changed = 0;
1462 unsigned int orig_flags;
1467 DNPRINTF(SWM_D_PROP, "ewmh_update_win_state: window: 0x%x, state: %ld, "
1468 "action: %ld\n", win->id, (unsigned long)state, action);
1470 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
1471 mask = EWMH_F_FULLSCREEN;
1472 else if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
1473 mask = EWMH_F_ABOVE;
1474 else if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
1475 mask = SWM_F_MANUAL;
1476 else if (state == ewmh[_NET_WM_STATE_SKIP_PAGER].atom)
1477 mask = EWMH_F_SKIP_PAGER;
1478 else if (state == ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom)
1479 mask = EWMH_F_SKIP_TASKBAR;
1481 orig_flags = win->ewmh_flags;
1484 case _NET_WM_STATE_REMOVE:
1485 win->ewmh_flags &= ~mask;
1487 case _NET_WM_STATE_ADD:
1488 win->ewmh_flags |= mask;
1490 case _NET_WM_STATE_TOGGLE:
1491 win->ewmh_flags ^= mask;
1495 changed = (win->ewmh_flags & mask) ^ (orig_flags & mask) ? 1 : 0;
1497 if (state == ewmh[_NET_WM_STATE_ABOVE].atom) {
1498 if (changed && !floating_toggle_win(win))
1499 win->ewmh_flags = orig_flags; /* revert */
1500 } else if (state == ewmh[_SWM_WM_STATE_MANUAL].atom) {
1502 win->manual = (win->ewmh_flags & SWM_F_MANUAL) != 0;
1503 } else if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom) {
1504 if (changed && !ewmh_set_win_fullscreen(win,
1505 win->ewmh_flags & EWMH_F_FULLSCREEN))
1506 win->ewmh_flags = orig_flags; /* revert */
1509 xcb_delete_property(conn, win->id, ewmh[_NET_WM_STATE].atom);
1511 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
1512 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1513 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1514 &ewmh[_NET_WM_STATE_FULLSCREEN].atom);
1515 else if (win->ewmh_flags & EWMH_F_SKIP_PAGER)
1516 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1517 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1518 &ewmh[_NET_WM_STATE_SKIP_PAGER].atom);
1519 else if (win->ewmh_flags & EWMH_F_SKIP_TASKBAR)
1520 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1521 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1522 &ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom);
1523 else if (win->ewmh_flags & EWMH_F_ABOVE)
1524 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1525 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1526 &ewmh[_NET_WM_STATE_ABOVE].atom);
1527 else if (win->ewmh_flags & SWM_F_MANUAL)
1528 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1529 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1530 &ewmh[_SWM_WM_STATE_MANUAL].atom);
1534 ewmh_get_win_state(struct ws_win *win)
1537 xcb_get_property_cookie_t c;
1538 xcb_get_property_reply_t *r;
1544 win->ewmh_flags = 0;
1546 win->ewmh_flags |= EWMH_F_ABOVE;
1548 win->ewmh_flags |= SWM_F_MANUAL;
1550 c = xcb_get_property(conn, 0, win->id, ewmh[_NET_WM_STATE].atom,
1551 XCB_ATOM_ATOM, 0, UINT32_MAX);
1552 r = xcb_get_property_reply(conn, c, NULL);
1556 states = xcb_get_property_value(r);
1557 n = xcb_get_property_value_length(r);
1559 for (i = 0; i < n; i++)
1560 ewmh_update_win_state(win, states[i], _NET_WM_STATE_ADD);
1568 dumpwins(struct swm_region *r, union arg *args)
1572 xcb_get_window_attributes_cookie_t c;
1573 xcb_get_window_attributes_reply_t *wa;
1575 /* suppress unused warning since var is needed */
1578 if (r->ws == NULL) {
1579 warnx("dumpwins: invalid workspace");
1583 warnx("=== managed window list ws %02d ===", r->ws->idx);
1584 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
1585 state = getstate(win->id);
1586 c = xcb_get_window_attributes(conn, win->id);
1587 wa = xcb_get_window_attributes_reply(conn, c, NULL);
1589 warnx("window: 0x%x, map_state: %d, state: %u, "
1590 "transient: 0x%x", win->id, wa->map_state,
1591 state, win->transient);
1594 warnx("window: 0x%x, failed xcb_get_window_attributes",
1598 warnx("===== unmanaged window list =====");
1599 TAILQ_FOREACH(win, &r->ws->unmanagedlist, entry) {
1600 state = getstate(win->id);
1601 c = xcb_get_window_attributes(conn, win->id);
1602 wa = xcb_get_window_attributes_reply(conn, c, NULL);
1604 warnx("window: 0x%x, map_state: %d, state: %u, "
1605 "transient: 0x%x", win->id, wa->map_state,
1606 state, win->transient);
1609 warnx("window: 0x%x, failed xcb_get_window_attributes",
1613 warnx("=================================");
1617 dumpwins(struct swm_region *r, union arg *s)
1622 #endif /* SWM_DEBUG */
1627 int saved_errno, status;
1630 saved_errno = errno;
1634 while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) != 0) {
1639 if (errno != ECHILD)
1640 warn("sighdlr: waitpid");
1641 #endif /* SWM_DEBUG */
1644 if (pid == searchpid)
1648 if (WIFEXITED(status)) {
1649 if (WEXITSTATUS(status) != 0)
1650 warnx("sighdlr: child exit status: %d",
1651 WEXITSTATUS(status));
1653 warnx("sighdlr: child is terminated "
1655 #endif /* SWM_DEBUG */
1669 errno = saved_errno;
1675 struct pid_e *p = NULL;
1677 DNPRINTF(SWM_D_MISC, "find_pid: %d\n", pid);
1682 TAILQ_FOREACH(p, &pidlist, entry) {
1691 name_to_pixel(int sidx, const char *colorname)
1693 uint32_t result = 0;
1694 char cname[32] = "#";
1695 xcb_screen_t *screen;
1696 xcb_colormap_t cmap;
1697 xcb_alloc_color_reply_t *cr;
1698 xcb_alloc_named_color_reply_t *nr;
1699 uint16_t rr, gg, bb;
1701 screen = get_screen(sidx);
1702 cmap = screen->default_colormap;
1704 /* color is in format rgb://rr/gg/bb */
1705 if (strncmp(colorname, "rgb:", 4) == 0) {
1706 if (parse_rgb(colorname, &rr, &gg, &bb) == -1)
1707 warnx("could not parse rgb %s", colorname);
1709 cr = xcb_alloc_color_reply(conn,
1710 xcb_alloc_color(conn, cmap, rr, gg, bb),
1716 warnx("color '%s' not found", colorname);
1719 nr = xcb_alloc_named_color_reply(conn,
1720 xcb_alloc_named_color(conn, cmap, strlen(colorname),
1723 strlcat(cname, colorname + 2, sizeof cname - 1);
1724 nr = xcb_alloc_named_color_reply(conn,
1725 xcb_alloc_named_color(conn, cmap, strlen(cname),
1732 warnx("color '%s' not found", colorname);
1739 setscreencolor(char *val, int i, int c)
1743 num_screens = get_screen_count();
1744 if (i > 0 && i <= num_screens) {
1745 screens[i - 1].c[c].pixel = name_to_pixel(i - 1, val);
1746 free(screens[i - 1].c[c].name);
1747 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
1749 } else if (i == -1) {
1750 for (i = 0; i < num_screens; i++) {
1751 screens[i].c[c].pixel = name_to_pixel(0, val);
1752 free(screens[i].c[c].name);
1753 if ((screens[i].c[c].name = strdup(val)) == NULL)
1757 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1762 fancy_stacker(struct workspace *ws)
1764 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1765 if (ws->cur_layout->l_stack == vertical_stack)
1766 snprintf(ws->stacker, sizeof ws->stacker,
1767 ws->l_state.vertical_flip ? "[%d>%d]" : "[%d|%d]",
1768 ws->l_state.vertical_mwin, ws->l_state.vertical_stacks);
1769 if (ws->cur_layout->l_stack == horizontal_stack)
1770 snprintf(ws->stacker, sizeof ws->stacker,
1771 ws->l_state.horizontal_flip ? "[%dv%d]" : "[%d-%d]",
1772 ws->l_state.horizontal_mwin, ws->l_state.horizontal_stacks);
1776 plain_stacker(struct workspace *ws)
1778 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1779 if (ws->cur_layout->l_stack == vertical_stack)
1780 strlcpy(ws->stacker, ws->l_state.vertical_flip ? "[>]" : "[|]",
1781 sizeof ws->stacker);
1782 if (ws->cur_layout->l_stack == horizontal_stack)
1783 strlcpy(ws->stacker, ws->l_state.horizontal_flip ? "[v]" : "[-]",
1784 sizeof ws->stacker);
1788 custom_region(char *val)
1790 unsigned int x, y, w, h;
1791 int sidx, num_screens;
1792 xcb_screen_t *screen;
1794 num_screens = get_screen_count();
1795 if (sscanf(val, "screen[%d]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
1796 errx(1, "invalid custom region, "
1797 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1798 if (sidx < 1 || sidx > num_screens)
1799 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1803 if ((screen = get_screen(sidx)) == NULL)
1804 errx(1, "ERROR: can't get screen %d.", sidx);
1807 errx(1, "region %ux%u+%u+%u too small", w, h, x, y);
1809 if (x > screen->width_in_pixels ||
1810 y > screen->height_in_pixels ||
1811 w + x > screen->width_in_pixels ||
1812 h + y > screen->height_in_pixels) {
1813 warnx("ignoring region %ux%u+%u+%u - not within screen "
1814 "boundaries (%ux%u)", w, h, x, y,
1815 screen->width_in_pixels, screen->height_in_pixels);
1819 new_region(&screens[sidx], x, y, w, h);
1823 socket_setnonblock(int fd)
1827 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
1828 err(1, "fcntl F_GETFL");
1829 flags |= O_NONBLOCK;
1830 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
1831 err(1, "fcntl F_SETFL");
1835 bar_print_legacy(struct swm_region *r, const char *s)
1837 xcb_rectangle_t rect;
1842 XRectangle ibox, lbox;
1846 XmbTextExtents(bar_fs, s, len, &ibox, &lbox);
1848 switch (bar_justify) {
1849 case SWM_BAR_JUSTIFY_LEFT:
1852 case SWM_BAR_JUSTIFY_CENTER:
1853 x = (WIDTH(r) - lbox.width) / 2;
1855 case SWM_BAR_JUSTIFY_RIGHT:
1856 x = WIDTH(r) - lbox.width - SWM_BAR_OFFSET;
1860 if (x < SWM_BAR_OFFSET)
1865 rect.width = WIDTH(r->bar);
1866 rect.height = HEIGHT(r->bar);
1868 /* clear back buffer */
1869 gcv[0] = r->s->c[SWM_S_COLOR_BAR].pixel;
1870 xcb_change_gc(conn, r->s->bar_gc, XCB_GC_FOREGROUND, gcv);
1871 xcb_poly_fill_rectangle(conn, r->bar->buffer, r->s->bar_gc, 1, &rect);
1873 /* draw back buffer */
1874 gcvd.graphics_exposures = 0;
1875 draw = XCreateGC(display, r->bar->buffer, GCGraphicsExposures, &gcvd);
1876 XSetForeground(display, draw, r->s->c[SWM_S_COLOR_BAR_FONT].pixel);
1877 DRAWSTRING(display, r->bar->buffer, bar_fs, draw,
1878 x, (bar_fs_extents->max_logical_extent.height - lbox.height) / 2 -
1880 XFreeGC(display, draw);
1883 xcb_copy_area(conn, r->bar->buffer, r->bar->id, r->s->bar_gc, 0, 0,
1884 0, 0, WIDTH(r->bar), HEIGHT(r->bar));
1888 bar_print(struct swm_region *r, const char *s)
1891 xcb_rectangle_t rect;
1899 XftTextExtentsUtf8(display, bar_font, (FcChar8 *)s, len, &info);
1901 switch (bar_justify) {
1902 case SWM_BAR_JUSTIFY_LEFT:
1905 case SWM_BAR_JUSTIFY_CENTER:
1906 x = (WIDTH(r) - info.width) / 2;
1908 case SWM_BAR_JUSTIFY_RIGHT:
1909 x = WIDTH(r) - info.width - SWM_BAR_OFFSET;
1913 if (x < SWM_BAR_OFFSET)
1918 rect.width = WIDTH(r->bar);
1919 rect.height = HEIGHT(r->bar);
1921 /* clear back buffer */
1922 gcv[0] = r->s->c[SWM_S_COLOR_BAR].pixel;
1923 xcb_change_gc(conn, r->s->bar_gc, XCB_GC_FOREGROUND, gcv);
1924 xcb_poly_fill_rectangle(conn, r->bar->buffer, r->s->bar_gc, 1, &rect);
1926 /* draw back buffer */
1927 draw = XftDrawCreate(display, r->bar->buffer,
1928 DefaultVisual(display, r->s->idx),
1929 DefaultColormap(display, r->s->idx));
1931 XftDrawStringUtf8(draw, &bar_font_color, bar_font, x,
1932 (HEIGHT(r->bar) + bar_font->height) / 2 - bar_font->descent,
1935 XftDrawDestroy(draw);
1938 xcb_copy_area(conn, r->bar->buffer, r->bar->id, r->s->bar_gc, 0, 0,
1939 0, 0, WIDTH(r->bar), HEIGHT(r->bar));
1943 bar_extra_stop(void)
1947 bzero(bar_pipe, sizeof bar_pipe);
1950 kill(bar_pid, SIGTERM);
1953 strlcpy(bar_ext, "", sizeof bar_ext);
1958 bar_class_name(char *s, size_t sz, struct swm_region *r)
1960 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1962 if (r->ws->focus->ch.class_name != NULL)
1963 strlcat(s, r->ws->focus->ch.class_name, sz);
1967 bar_title_name(char *s, size_t sz, struct swm_region *r)
1969 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1971 if (r->ws->focus->ch.instance_name != NULL)
1972 strlcat(s, r->ws->focus->ch.instance_name, sz);
1976 bar_class_title_name(char *s, size_t sz, struct swm_region *r)
1978 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1981 bar_class_name(s, sz, r);
1982 strlcat(s, ":", sz);
1983 bar_title_name(s, sz, r);
1987 bar_window_float(char *s, size_t sz, struct swm_region *r)
1989 if (r == NULL || r ->ws == NULL || r->ws->focus == NULL)
1991 if (r->ws->focus->floating)
1992 strlcat(s, "(f)", sz);
1996 bar_window_name(char *s, size_t sz, struct swm_region *r)
2000 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
2002 if ((title = get_win_name(r->ws->focus->id)) == NULL)
2005 strlcat(s, title, sz);
2010 bar_urgent(char *s, size_t sz)
2013 int i, j, num_screens;
2014 int urgent[SWM_WS_MAX];
2016 xcb_get_property_cookie_t c;
2017 xcb_icccm_wm_hints_t hints;
2019 for (i = 0; i < workspace_limit; i++)
2022 num_screens = get_screen_count();
2023 for (i = 0; i < num_screens; i++)
2024 for (j = 0; j < workspace_limit; j++)
2025 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry) {
2026 c = xcb_icccm_get_wm_hints(conn, win->id);
2027 if (xcb_icccm_get_wm_hints_reply(conn, c,
2030 if (hints.flags & XCB_ICCCM_WM_HINT_X_URGENCY)
2034 for (i = 0; i < workspace_limit; i++) {
2036 snprintf(b, sizeof b, "%d ", i + 1);
2038 snprintf(b, sizeof b, "- ");
2044 bar_workspace_name(char *s, size_t sz, struct swm_region *r)
2046 if (r == NULL || r->ws == NULL)
2048 if (r->ws->name != NULL)
2049 strlcat(s, r->ws->name, sz);
2052 /* build the default bar format according to the defined enabled options */
2054 bar_fmt(const char *fmtexp, char *fmtnew, struct swm_region *r, size_t sz)
2056 /* if format provided, just copy the buffers */
2057 if (bar_format != NULL) {
2058 strlcpy(fmtnew, fmtexp, sz);
2062 /* reset the output buffer */
2065 strlcat(fmtnew, "+N:+I ", sz);
2067 strlcat(fmtnew, "+S", sz);
2068 strlcat(fmtnew, " ", sz);
2070 /* only show the workspace name if there's actually one */
2071 if (r != NULL && r->ws != NULL && r->ws->name != NULL)
2072 strlcat(fmtnew, "<+D>", sz);
2073 strlcat(fmtnew, "+3<", sz);
2075 if (clock_enabled) {
2076 strlcat(fmtnew, fmtexp, sz);
2077 strlcat(fmtnew, "+4<", sz);
2080 /* bar_urgent already adds the space before the last asterisk */
2082 strlcat(fmtnew, "* +U*+4<", sz);
2084 if (title_class_enabled) {
2085 strlcat(fmtnew, "+C", sz);
2086 if (!title_name_enabled)
2087 strlcat(fmtnew, "+4<", sz);
2090 /* checks needed by the colon and floating strlcat(3) calls below */
2091 if (r != NULL && r->ws != NULL && r->ws->focus != NULL) {
2092 if (title_name_enabled) {
2093 if (title_class_enabled)
2094 strlcat(fmtnew, ":", sz);
2095 strlcat(fmtnew, "+T+4<", sz);
2097 if (window_name_enabled) {
2098 if (r->ws->focus->floating)
2099 strlcat(fmtnew, "+F ", sz);
2100 strlcat(fmtnew, "+64W ", sz);
2104 /* finally add the action script output and the version */
2105 strlcat(fmtnew, "+4<+A+4<+V", sz);
2109 bar_replace_pad(char *tmp, int *limit, size_t sz)
2111 /* special case; no limit given, pad one space, instead */
2112 if (*limit == (int)sz - 1)
2114 snprintf(tmp, sz, "%*s", *limit, " ");
2117 /* replaces the bar format character sequences (like in tmux(1)) */
2119 bar_replace_seq(char *fmt, char *fmtrep, struct swm_region *r, size_t *offrep,
2123 char tmp[SWM_BAR_MAX];
2127 /* reset strlcat(3) buffer */
2130 /* get number, if any */
2133 if (sscanf(fmt, "%d%n", &limit, &size) != 1)
2134 limit = sizeof tmp - 1;
2135 if (limit <= 0 || limit >= (int)sizeof tmp)
2136 limit = sizeof tmp - 1;
2138 /* there is nothing to replace (ie EOL) */
2145 bar_replace_pad(tmp, &limit, sizeof tmp);
2148 snprintf(tmp, sizeof tmp, "%s", bar_ext);
2151 bar_class_name(tmp, sizeof tmp, r);
2154 bar_workspace_name(tmp, sizeof tmp, r);
2157 bar_window_float(tmp, sizeof tmp, r);
2160 snprintf(tmp, sizeof tmp, "%d", r->ws->idx + 1);
2163 snprintf(tmp, sizeof tmp, "%d", r->s->idx + 1);
2166 bar_class_title_name(tmp, sizeof tmp, r);
2169 snprintf(tmp, sizeof tmp, "%s", r->ws->stacker);
2172 bar_title_name(tmp, sizeof tmp, r);
2175 bar_urgent(tmp, sizeof tmp);
2178 snprintf(tmp, sizeof tmp, "%s", bar_vertext);
2181 bar_window_name(tmp, sizeof tmp, r);
2184 /* unknown character sequence; copy as-is */
2185 snprintf(tmp, sizeof tmp, "+%c", *fmt);
2191 if ((int)len < limit)
2193 while (limit-- > 0) {
2194 if (*offrep >= sz - 1)
2196 fmtrep[(*offrep)++] = *ptr++;
2204 bar_replace(char *fmt, char *fmtrep, struct swm_region *r, size_t sz)
2209 while (*fmt != '\0') {
2211 /* skip ordinary characters */
2214 fmtrep[off++] = *fmt++;
2218 /* character sequence found; replace it */
2219 fmt = bar_replace_seq(fmt, fmtrep, r, &off, sz);
2228 bar_fmt_expand(char *fmtexp, size_t sz)
2235 /* start by grabbing the current time and date */
2237 localtime_r(&tmt, &tm);
2239 /* figure out what to expand */
2240 if (bar_format != NULL)
2242 else if (bar_format == NULL && clock_enabled)
2244 /* if nothing to expand bail out */
2250 /* copy as-is, just in case the format shouldn't be expanded below */
2251 strlcpy(fmtexp, fmt, sz);
2252 /* finally pass the string through strftime(3) */
2253 #ifndef SWM_DENY_CLOCK_FORMAT
2254 if ((len = strftime(fmtexp, sz, fmt, &tm)) == 0)
2255 warnx("format too long");
2260 /* Redraws the bar; need to follow with xcb_flush() or focus_flush(). */
2264 char fmtexp[SWM_BAR_MAX], fmtnew[SWM_BAR_MAX];
2265 char fmtrep[SWM_BAR_MAX];
2267 struct swm_region *r;
2269 /* expand the format by first passing it through strftime(3) */
2270 bar_fmt_expand(fmtexp, sizeof fmtexp);
2272 num_screens = get_screen_count();
2273 for (i = 0; i < num_screens; i++) {
2274 TAILQ_FOREACH(r, &screens[i].rl, entry) {
2278 if (bar_enabled && r->ws->bar_enabled)
2279 xcb_map_window(conn, r->bar->id);
2281 xcb_unmap_window(conn, r->bar->id);
2285 if (startup_exception)
2286 snprintf(fmtrep, sizeof fmtrep, "total "
2287 "exceptions: %d, first exception: %s",
2291 bar_fmt(fmtexp, fmtnew, r, sizeof fmtnew);
2292 bar_replace(fmtnew, fmtrep, r, sizeof fmtrep);
2294 if (bar_font_legacy)
2295 bar_print_legacy(r, fmtrep);
2297 bar_print(r, fmtrep);
2303 * Reads external script output; call when stdin is readable.
2304 * Returns 1 if bar_ext was updated; otherwise 0.
2307 bar_extra_update(void)
2310 char b[SWM_BAR_MAX];
2316 while (fgets(b, sizeof(b), stdin) != NULL) {
2319 if (b[len - 1] == '\n') {
2320 /* Remove newline. */
2323 /* "Clear" bar_ext. */
2326 /* Flush buffered output. */
2327 strlcpy(bar_ext, bar_ext_buf, sizeof(bar_ext));
2328 bar_ext_buf[0] = '\0';
2330 /* Append new output to bar. */
2331 strlcat(bar_ext, b, sizeof(bar_ext));
2335 /* Buffer output. */
2336 strlcat(bar_ext_buf, b, sizeof(bar_ext_buf));
2341 if (errno != EAGAIN) {
2342 warn("bar_action failed");
2351 bar_toggle(struct swm_region *r, union arg *args)
2353 struct swm_region *tmpr;
2356 /* suppress unused warnings since vars are needed */
2360 DNPRINTF(SWM_D_BAR, "bar_toggle\n");
2363 case SWM_ARG_ID_BAR_TOGGLE_WS:
2364 /* Only change if master switch is enabled. */
2366 r->ws->bar_enabled = !r->ws->bar_enabled;
2368 bar_enabled = r->ws->bar_enabled = 1;
2370 case SWM_ARG_ID_BAR_TOGGLE:
2371 bar_enabled = !bar_enabled;
2375 /* update bars as necessary */
2376 num_screens = get_screen_count();
2377 for (i = 0; i < num_screens; i++)
2378 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
2380 if (bar_enabled && tmpr->ws->bar_enabled)
2381 xcb_map_window(conn, tmpr->bar->id);
2383 xcb_unmap_window(conn, tmpr->bar->id);
2388 /* must be after stack */
2395 bar_extra_setup(void)
2397 /* do this here because the conf file is in memory */
2398 if (!bar_extra && bar_argv[0]) {
2399 /* launch external status app */
2401 if (pipe(bar_pipe) == -1)
2402 err(1, "pipe error");
2403 socket_setnonblock(bar_pipe[0]);
2404 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
2406 /* Set stdin to read from the pipe. */
2407 if (dup2(bar_pipe[0], STDIN_FILENO) == -1)
2410 /* Set stdout to write to the pipe. */
2411 if (dup2(bar_pipe[1], STDOUT_FILENO) == -1)
2414 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
2415 err(1, "could not disable SIGPIPE");
2416 switch (bar_pid = fork()) {
2418 err(1, "cannot fork");
2422 execvp(bar_argv[0], bar_argv);
2423 err(1, "%s external app failed", bar_argv[0]);
2425 default: /* parent */
2430 atexit(kill_bar_extra_atexit);
2435 kill_bar_extra_atexit(void)
2438 kill(bar_pid, SIGTERM);
2446 while ((s = index(s, '-'))) {
2451 return (count == 14);
2457 char *default_string;
2458 char **missing_charsets;
2459 int num_missing_charsets = 0;
2463 XFreeFontSet(display, bar_fs);
2467 DNPRINTF(SWM_D_INIT, "fontset_init: loading bar_fonts: %s\n", bar_fonts);
2469 bar_fs = XCreateFontSet(display, bar_fonts, &missing_charsets,
2470 &num_missing_charsets, &default_string);
2472 if (num_missing_charsets > 0) {
2473 warnx("Unable to load charset(s):");
2475 for (i = 0; i < num_missing_charsets; ++i)
2476 warnx("%s", missing_charsets[i]);
2478 XFreeStringList(missing_charsets);
2480 if (strcmp(default_string, ""))
2481 warnx("Glyphs from those sets will be replaced "
2482 "by '%s'.", default_string);
2484 warnx("Glyphs from those sets won't be drawn.");
2488 errx(1, "Error creating font set structure.");
2490 bar_fs_extents = XExtentsOfFontSet(bar_fs);
2492 bar_height = bar_fs_extents->max_logical_extent.height +
2493 2 * bar_border_width;
2500 xft_init(struct swm_region *r)
2502 char *font, *d, *search;
2505 if (bar_font == NULL) {
2506 if ((d = strdup(bar_fonts)) == NULL)
2507 errx(1, "insufficient memory.");
2509 while ((font = strsep(&search, ",")) != NULL) {
2513 DNPRINTF(SWM_D_INIT, "xft_init: try font %s\n", font);
2516 bar_font = XftFontOpenXlfd(display, r->s->idx,
2519 bar_font = XftFontOpenName(display, r->s->idx,
2524 warnx("unable to load font %s", font);
2527 DNPRINTF(SWM_D_INIT, "successfully opened "
2535 if (bar_font == NULL)
2536 errx(1, "unable to open a font");
2538 PIXEL_TO_XRENDERCOLOR(r->s->c[SWM_S_COLOR_BAR_FONT].pixel, color);
2540 if (!XftColorAllocValue(display, DefaultVisual(display, r->s->idx),
2541 DefaultColormap(display, r->s->idx), &color, &bar_font_color))
2542 warn("Xft error: unable to allocate color.");
2544 bar_height = bar_font->height + 2 * bar_border_width;
2551 bar_setup(struct swm_region *r)
2553 xcb_screen_t *screen;
2556 DNPRINTF(SWM_D_BAR, "bar_setup: screen %d.\n",
2559 if ((screen = get_screen(r->s->idx)) == NULL)
2560 errx(1, "ERROR: can't get screen %d.", r->s->idx);
2565 if ((r->bar = calloc(1, sizeof(struct swm_bar))) == NULL)
2566 err(1, "bar_setup: calloc: failed to allocate memory.");
2568 if (bar_font_legacy)
2574 Y(r->bar) = bar_at_bottom ? (Y(r) + HEIGHT(r) - bar_height) : Y(r);
2575 WIDTH(r->bar) = WIDTH(r) - 2 * bar_border_width;
2576 HEIGHT(r->bar) = bar_height - 2 * bar_border_width;
2578 /* Assume region is unfocused when we create the bar. */
2579 r->bar->id = xcb_generate_id(conn);
2580 wa[0] = r->s->c[SWM_S_COLOR_BAR].pixel;
2581 wa[1] = r->s->c[SWM_S_COLOR_BAR_BORDER_UNFOCUS].pixel;
2582 wa[2] = XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_POINTER_MOTION |
2583 XCB_EVENT_MASK_POINTER_MOTION_HINT;
2585 xcb_create_window(conn, XCB_COPY_FROM_PARENT, r->bar->id, r->s->root,
2586 X(r->bar), Y(r->bar), WIDTH(r->bar), HEIGHT(r->bar),
2587 bar_border_width, XCB_WINDOW_CLASS_INPUT_OUTPUT,
2588 XCB_COPY_FROM_PARENT, XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL
2589 | XCB_CW_EVENT_MASK, wa);
2591 r->bar->buffer = xcb_generate_id(conn);
2592 xcb_create_pixmap(conn, screen->root_depth, r->bar->buffer, r->bar->id,
2593 WIDTH(r->bar), HEIGHT(r->bar));
2596 xcb_randr_select_input(conn, r->bar->id,
2597 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE);
2600 xcb_map_window(conn, r->bar->id);
2602 DNPRINTF(SWM_D_BAR, "bar_setup: window: 0x%x, (x,y) w x h: (%d,%d) "
2603 "%d x %d\n", WINID(r->bar), X(r->bar), Y(r->bar), WIDTH(r->bar),
2610 bar_cleanup(struct swm_region *r)
2614 xcb_destroy_window(conn, r->bar->id);
2615 xcb_free_pixmap(conn, r->bar->buffer);
2621 set_win_state(struct ws_win *win, uint16_t state)
2623 uint16_t data[2] = { state, XCB_ATOM_NONE };
2625 DNPRINTF(SWM_D_EVENT, "set_win_state: window: 0x%x, state: %u\n",
2631 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id, a_state,
2632 a_state, 32, 2, data);
2636 getstate(xcb_window_t w)
2638 uint32_t result = 0;
2639 xcb_get_property_cookie_t c;
2640 xcb_get_property_reply_t *r;
2642 c = xcb_get_property(conn, 0, w, a_state, a_state, 0L, 2L);
2643 r = xcb_get_property_reply(conn, c, NULL);
2646 if (r->type == a_state && r->format == 32 && r->length == 2)
2647 result = *((uint32_t *)xcb_get_property_value(r));
2651 DNPRINTF(SWM_D_MISC, "getstate property: win 0x%x state %u\n", w,
2657 version(struct swm_region *r, union arg *args)
2659 /* suppress unused warnings since vars are needed */
2663 bar_version = !bar_version;
2665 snprintf(bar_vertext, sizeof bar_vertext,
2666 "Version: %s Build: %s", SPECTRWM_VERSION, buildstr);
2668 strlcpy(bar_vertext, "", sizeof bar_vertext);
2675 client_msg(struct ws_win *win, xcb_atom_t a, xcb_timestamp_t t)
2677 xcb_client_message_event_t ev;
2685 name = get_atom_name(a);
2686 DNPRINTF(SWM_D_EVENT, "client_msg: window: 0x%x, atom: %s(%u), "
2688 win->id, name, a, t);
2692 bzero(&ev, sizeof ev);
2693 ev.response_type = XCB_CLIENT_MESSAGE;
2694 ev.window = win->id;
2697 ev.data.data32[0] = a;
2698 ev.data.data32[1] = t;
2700 xcb_send_event(conn, 0, win->id,
2701 XCB_EVENT_MASK_NO_EVENT, (const char *)&ev);
2704 /* synthetic response to a ConfigureRequest when not making a change */
2706 config_win(struct ws_win *win, xcb_configure_request_event_t *ev)
2708 xcb_configure_notify_event_t ce;
2713 /* send notification of unchanged state. */
2714 bzero(&ce, sizeof(ce));
2715 ce.response_type = XCB_CONFIGURE_NOTIFY;
2718 ce.width = WIDTH(win);
2719 ce.height = HEIGHT(win);
2720 ce.override_redirect = 0;
2725 ce.window = win->id;
2726 ce.border_width = BORDER(win);
2727 ce.above_sibling = XCB_WINDOW_NONE;
2730 ce.event = ev->window;
2731 ce.window = ev->window;
2733 /* make response appear more WM_SIZE_HINTS-compliant */
2734 if (win->sh.flags) {
2735 DNPRINTF(SWM_D_MISC, "config_win: hints: window: 0x%x,"
2736 " sh.flags: %u, min: %d x %d, max: %d x %d, inc: "
2737 "%d x %d\n", win->id, win->sh.flags, SH_MIN_W(win),
2738 SH_MIN_H(win), SH_MAX_W(win), SH_MAX_H(win),
2739 SH_INC_W(win), SH_INC_H(win));
2744 /* the hint may be set... to 0! */
2745 if (SH_MIN_W(win) > 0 && ce.width < SH_MIN_W(win))
2746 ce.width = SH_MIN_W(win);
2747 if (SH_MIN_H(win) > 0 && ce.height < SH_MIN_H(win))
2748 ce.height = SH_MIN_H(win);
2753 /* may also be advertized as 0 */
2754 if (SH_MAX_W(win) > 0 && ce.width > SH_MAX_W(win))
2755 ce.width = SH_MAX_W(win);
2756 if (SH_MAX_H(win) > 0 && ce.height > SH_MAX_H(win))
2757 ce.height = SH_MAX_H(win);
2760 /* resize increment. */
2762 if (SH_INC_W(win) > 1 && ce.width > SH_INC_W(win))
2763 ce.width -= (ce.width - SH_MIN_W(win)) %
2765 if (SH_INC_H(win) > 1 && ce.height > SH_INC_H(win))
2766 ce.height -= (ce.height - SH_MIN_H(win)) %
2770 /* adjust x and y for requested border_width. */
2771 ce.x += BORDER(win) - ev->border_width;
2772 ce.y += BORDER(win) - ev->border_width;
2773 ce.border_width = ev->border_width;
2774 ce.above_sibling = ev->sibling;
2777 DNPRINTF(SWM_D_MISC, "config_win: ewmh: %s, window: 0x%x, (x,y) w x h: "
2778 "(%d,%d) %d x %d, border: %d\n", YESNO(ev == NULL), win->id, ce.x,
2779 ce.y, ce.width, ce.height, ce.border_width);
2781 xcb_send_event(conn, 0, win->id, XCB_EVENT_MASK_STRUCTURE_NOTIFY,
2786 count_win(struct workspace *ws, int count_transient)
2791 TAILQ_FOREACH(win, &ws->winlist, entry) {
2792 if (!count_transient && win->floating)
2794 if (!count_transient && win->transient)
2800 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
2806 quit(struct swm_region *r, union arg *args)
2808 /* suppress unused warnings since vars are needed */
2812 DNPRINTF(SWM_D_MISC, "quit\n");
2817 map_window(struct ws_win *win, xcb_window_t sibling)
2819 uint16_t mode = XCB_CONFIG_WINDOW_STACK_MODE;
2823 /* If sibling is specified, stack right above it. */
2824 if (sibling != XCB_WINDOW_NONE) {
2825 mode |= XCB_CONFIG_WINDOW_SIBLING;
2829 val[i] = XCB_STACK_MODE_ABOVE;
2834 DNPRINTF(SWM_D_EVENT, "map_window: win 0x%x, mapped: %s, "
2835 "sibling: 0x%x\n", win->id, YESNO(win->mapped), sibling);
2837 xcb_configure_window(conn, win->id, mode, val);
2842 xcb_map_window(conn, win->id);
2843 set_win_state(win, XCB_ICCCM_WM_STATE_NORMAL);
2848 unmap_window(struct ws_win *win)
2853 DNPRINTF(SWM_D_EVENT, "unmap_window: win 0x%x, mapped: %s\n", win->id,
2854 YESNO(win->mapped));
2859 xcb_unmap_window(conn, win->id);
2860 set_win_state(win, XCB_ICCCM_WM_STATE_ICONIC);
2868 int i, j, num_screens;
2870 num_screens = get_screen_count();
2871 for (i = 0; i < num_screens; i++)
2872 for (j = 0; j < workspace_limit; j++)
2873 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2878 fake_keypress(struct ws_win *win, xcb_keysym_t keysym, uint16_t modifiers)
2880 xcb_key_press_event_t event;
2881 xcb_keycode_t *keycode;
2886 keycode = xcb_key_symbols_get_keycode(syms, keysym);
2888 DNPRINTF(SWM_D_MISC, "fake_keypress: win 0x%x keycode %u\n",
2891 bzero(&event, sizeof(event));
2892 event.event = win->id;
2893 event.root = win->s->root;
2894 event.child = XCB_WINDOW_NONE;
2895 event.time = XCB_CURRENT_TIME;
2896 event.event_x = X(win);
2897 event.event_y = Y(win);
2900 event.same_screen = 1;
2901 event.detail = *keycode;
2902 event.state = modifiers;
2904 event.response_type = XCB_KEY_PRESS;
2905 xcb_send_event(conn, 1, win->id,
2906 XCB_EVENT_MASK_KEY_PRESS, (const char *)&event);
2908 event.response_type = XCB_KEY_RELEASE;
2909 xcb_send_event(conn, 1, win->id,
2910 XCB_EVENT_MASK_KEY_RELEASE, (const char *)&event);
2916 restart(struct swm_region *r, union arg *args)
2918 /* suppress unused warning since var is needed */
2922 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
2926 execvp(start_argv[0], start_argv);
2927 warn("execvp failed");
2932 get_pointer_win(xcb_window_t root)
2934 struct ws_win *win = NULL;
2935 xcb_query_pointer_reply_t *r;
2937 DNPRINTF(SWM_D_EVENT, "get_pointer_win: root: 0x%x.\n", root);
2939 r = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, root), NULL);
2941 win = find_window(r->child);
2943 DNPRINTF(SWM_D_EVENT, "get_pointer_win: 0x%x.\n",
2946 DNPRINTF(SWM_D_EVENT, "get_pointer_win: none.\n");
2955 root_to_region(xcb_window_t root, int check)
2958 struct swm_region *r = NULL;
2960 xcb_query_pointer_reply_t *qpr;
2961 xcb_get_input_focus_reply_t *gifr;
2963 DNPRINTF(SWM_D_MISC, "root_to_region: window: 0x%x\n", root);
2965 num_screens = get_screen_count();
2966 for (i = 0; i < num_screens; i++)
2967 if (screens[i].root == root)
2970 if (check & SWM_CK_REGION)
2971 r = screens[i].r_focus;
2973 if (r == NULL && check & SWM_CK_FOCUS) {
2974 /* Try to find an actively focused window */
2975 gifr = xcb_get_input_focus_reply(conn,
2976 xcb_get_input_focus(conn), NULL);
2978 cfw = find_window(gifr->focus);
2979 if (cfw && cfw->ws->r)
2986 if (r == NULL && check & SWM_CK_POINTER) {
2987 /* No region with an active focus; try to use pointer. */
2988 qpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn,
2989 screens[i].root), NULL);
2992 DNPRINTF(SWM_D_MISC, "root_to_region: pointer: "
2993 "(%d,%d)\n", qpr->root_x, qpr->root_y);
2994 TAILQ_FOREACH(r, &screens[i].rl, entry)
2995 if (X(r) <= qpr->root_x &&
2996 qpr->root_x < MAX_X(r) &&
2997 Y(r) <= qpr->root_y &&
2998 qpr->root_y < MAX_Y(r))
3005 if (r == NULL && check & SWM_CK_FALLBACK)
3006 r = TAILQ_FIRST(&screens[i].rl);
3008 DNPRINTF(SWM_D_MISC, "root_to_region: idx: %d\n", get_region_index(r));
3014 find_unmanaged_window(xcb_window_t id)
3017 int i, j, num_screens;
3019 num_screens = get_screen_count();
3020 for (i = 0; i < num_screens; i++)
3021 for (j = 0; j < workspace_limit; j++)
3022 TAILQ_FOREACH(win, &screens[i].ws[j].unmanagedlist,
3030 find_window(xcb_window_t id)
3033 int i, j, num_screens;
3034 xcb_query_tree_reply_t *r;
3036 num_screens = get_screen_count();
3037 for (i = 0; i < num_screens; i++)
3038 for (j = 0; j < workspace_limit; j++)
3039 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
3043 r = xcb_query_tree_reply(conn, xcb_query_tree(conn, id), NULL);
3047 /* if we were looking for the parent return that window instead */
3048 if (r->parent == 0 || r->root == r->parent) {
3053 /* look for parent */
3054 for (i = 0; i < num_screens; i++)
3055 for (j = 0; j < workspace_limit; j++)
3056 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
3057 if (r->parent == win->id) {
3067 spawn(int ws_idx, union arg *args, int close_fd)
3072 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
3074 close(xcb_get_file_descriptor(conn));
3076 setenv("LD_PRELOAD", SWM_LIB, 1);
3078 if (asprintf(&ret, "%d", ws_idx) == -1) {
3079 warn("spawn: asprintf SWM_WS");
3082 setenv("_SWM_WS", ret, 1);
3086 if (asprintf(&ret, "%d", getpid()) == -1) {
3087 warn("spawn: asprintf _SWM_PID");
3090 setenv("_SWM_PID", ret, 1);
3094 if (setsid() == -1) {
3095 warn("spawn: setsid");
3101 * close stdin and stdout to prevent interaction between apps
3102 * and the baraction script
3103 * leave stderr open to record errors
3105 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) == -1) {
3106 warn("spawn: open");
3109 dup2(fd, STDIN_FILENO);
3110 dup2(fd, STDOUT_FILENO);
3115 execvp(args->argv[0], args->argv);
3117 warn("spawn: execvp");
3122 kill_refs(struct ws_win *win)
3124 int i, x, num_screens;
3125 struct swm_region *r;
3126 struct workspace *ws;
3131 num_screens = get_screen_count();
3132 for (i = 0; i < num_screens; i++)
3133 TAILQ_FOREACH(r, &screens[i].rl, entry)
3134 for (x = 0; x < workspace_limit; x++) {
3136 if (win == ws->focus)
3138 if (win == ws->focus_prev)
3139 ws->focus_prev = NULL;
3144 validate_win(struct ws_win *testwin)
3147 struct workspace *ws;
3148 struct swm_region *r;
3149 int i, x, num_screens;
3151 if (testwin == NULL)
3154 num_screens = get_screen_count();
3155 for (i = 0; i < num_screens; i++)
3156 TAILQ_FOREACH(r, &screens[i].rl, entry)
3157 for (x = 0; x < workspace_limit; x++) {
3159 TAILQ_FOREACH(win, &ws->winlist, entry)
3167 validate_ws(struct workspace *testws)
3169 struct swm_region *r;
3170 struct workspace *ws;
3171 int i, x, num_screens;
3173 /* validate all ws */
3174 num_screens = get_screen_count();
3175 for (i = 0; i < num_screens; i++)
3176 TAILQ_FOREACH(r, &screens[i].rl, entry)
3177 for (x = 0; x < workspace_limit; x++) {
3186 get_sibling(struct ws_win *win, int mode)
3188 struct ws_win *w = win;
3192 TAILQ_FOREACH_REVERSE(w, &w->ws->winlist, ws_win_list, entry)
3193 if (w != win && !w->floating && !w->iconic)
3196 case SWM_STACK_ABOVE:
3198 w = TAILQ_NEXT(w, entry);
3199 } while (w != NULL && (w == win || w->floating || w->iconic));
3201 case SWM_STACK_BELOW:
3203 w = TAILQ_PREV(w, ws_win_list, entry);
3204 } while (w != NULL && (w == win || w->floating || w->iconic));
3206 case SWM_STACK_BOTTOM:
3207 TAILQ_FOREACH(w, &w->ws->winlist, entry)
3208 if (w != win && !w->floating && !w->iconic)
3216 return (win->ws->r->id);
3222 unfocus_win(struct ws_win *win)
3224 xcb_window_t none = XCB_WINDOW_NONE;
3226 DNPRINTF(SWM_D_FOCUS, "unfocus_win: window: 0x%x\n", WINID(win));
3230 if (win->ws == NULL) {
3231 DNPRINTF(SWM_D_FOCUS, "unfocus_win: NULL ws.\n");
3235 if (validate_ws(win->ws)) {
3236 DNPRINTF(SWM_D_FOCUS, "unfocus_win: invalid ws.\n");
3240 if (win->ws->r == NULL) {
3241 DNPRINTF(SWM_D_FOCUS, "unfocus_win: NULL region.\n");
3245 if (validate_win(win)) {
3246 DNPRINTF(SWM_D_FOCUS, "unfocus_win: invalid win.\n");
3251 if (win->ws->focus == win) {
3252 if (tile_gap < 0 && !win->floating)
3253 map_window(win, get_sibling(win, SWM_STACK_BELOW));
3254 win->ws->focus = NULL;
3255 win->ws->focus_prev = win;
3258 if (validate_win(win->ws->focus)) {
3259 kill_refs(win->ws->focus);
3260 win->ws->focus = NULL;
3263 if (validate_win(win->ws->focus_prev)) {
3264 kill_refs(win->ws->focus_prev);
3265 win->ws->focus_prev = NULL;
3268 xcb_change_window_attributes(conn, win->id, XCB_CW_BORDER_PIXEL,
3269 &win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].pixel);
3271 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
3272 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1, &none);
3274 DNPRINTF(SWM_D_FOCUS, "unfocus_win: done.\n");
3278 focus_win(struct ws_win *win)
3280 struct ws_win *cfw = NULL, *parent = NULL, *w;
3281 struct workspace *ws;
3282 xcb_get_input_focus_reply_t *r;
3284 DNPRINTF(SWM_D_FOCUS, "focus_win: window: 0x%x\n", WINID(win));
3289 if (win->ws == NULL)
3297 if (validate_ws(ws))
3300 if (validate_win(win)) {
3305 r = xcb_get_input_focus_reply(conn, xcb_get_input_focus(conn), NULL);
3307 cfw = find_window(r->focus);
3313 if (ws->focus != win) {
3314 if (ws->focus && ws->focus != cfw)
3315 unfocus_win(ws->focus);
3319 /* If this window directs focus to a child window, then clear. */
3320 if (win->focus_child)
3321 win->focus_child = NULL;
3323 /* If transient, adjust parent's focus child for focus_magic. */
3324 if (win->transient) {
3325 parent = find_window(win->transient);
3326 if (parent && parent->focus_child != win)
3327 parent->focus_child = win;
3330 if (cfw != win && ws->r != NULL) {
3331 /* Set input focus if no input hint, or indicated by hint. */
3332 if (!(win->hints.flags & XCB_ICCCM_WM_HINT_INPUT) ||
3333 (win->hints.flags & XCB_ICCCM_WM_HINT_INPUT &&
3335 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT,
3336 win->id, last_event_time);
3338 /* Tell app it can adjust focus to a specific window. */
3339 if (win->take_focus) {
3340 /* java is special; always tell parent */
3341 if (win->transient && win->java)
3342 client_msg(parent, a_takefocus,
3345 client_msg(win, a_takefocus, last_event_time);
3348 xcb_change_window_attributes(conn, win->id, XCB_CW_BORDER_PIXEL,
3349 &ws->r->s->c[SWM_S_COLOR_FOCUS].pixel);
3351 if (ws->cur_layout->flags & SWM_L_MAPONFOCUS ||
3353 /* If a parent exists, map it first. */
3355 map_window(parent, XCB_WINDOW_NONE);
3357 /* Map siblings next. */
3358 TAILQ_FOREACH(w, &ws->winlist, entry)
3359 if (w != win && !w->iconic &&
3360 w->transient == parent->id)
3361 map_window(w, XCB_WINDOW_NONE);
3364 /* Map focused window. */
3365 map_window(win, XCB_WINDOW_NONE);
3367 /* Finally, map children of focus window. */
3368 TAILQ_FOREACH(w, &ws->winlist, entry)
3369 if (w->transient == win->id && !w->iconic)
3370 map_window(w, XCB_WINDOW_NONE);
3371 } else if (tile_gap < 0 && !win->floating) {
3373 * Windows overlap in the layout.
3374 * Raise focused win above all tiled wins.
3376 if (tile_gap < 0 && !win->floating)
3378 get_sibling(win, SWM_STACK_TOP));
3383 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
3384 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1,
3391 DNPRINTF(SWM_D_FOCUS, "focus_win: done.\n");
3394 /* If a child window should have focus instead, return it. */
3396 get_focus_magic(struct ws_win *win)
3398 struct ws_win *parent = NULL;
3399 struct ws_win *child = NULL;
3401 DNPRINTF(SWM_D_FOCUS, "get_focus_magic: window: 0x%x\n", WINID(win));
3405 if (win->transient) {
3406 parent = find_window(win->transient);
3408 /* If parent prefers focus elsewhere, then try to do so. */
3409 if (parent && (child = parent->focus_child)) {
3410 if (validate_win(child) == 0 && child->mapped)
3413 parent->focus_child = NULL;
3417 /* If this window prefers focus elsewhere, then try to do so. */
3418 if ((child = win->focus_child)) {
3419 if (validate_win(child) == 0 && child->mapped)
3422 win->focus_child = NULL;
3429 event_drain(uint8_t rt)
3431 xcb_generic_event_t *evt;
3433 /* ensure all pending requests have been processed before filtering. */
3435 while ((evt = xcb_poll_for_event(conn))) {
3436 if (XCB_EVENT_RESPONSE_TYPE(evt) != rt)
3444 set_region(struct swm_region *r)
3446 struct swm_region *rf;
3452 /* Unfocus old region bar. */
3457 xcb_change_window_attributes(conn, rf->bar->id,
3458 XCB_CW_BORDER_PIXEL,
3459 &r->s->c[SWM_S_COLOR_BAR_BORDER_UNFOCUS].pixel);
3462 /* Set region bar border to focus_color. */
3463 xcb_change_window_attributes(conn, r->bar->id,
3464 XCB_CW_BORDER_PIXEL, &r->s->c[SWM_S_COLOR_BAR_BORDER].pixel);
3470 focus_region(struct swm_region *r)
3473 struct swm_region *old_r;
3478 old_r = r->s->r_focus;
3481 nfw = get_region_focus(r);
3485 /* New region is empty; need to manually unfocus win. */
3487 unfocus_win(old_r->ws->focus);
3489 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT, r->id,
3492 /* Clear bar since empty. */
3498 switchws(struct swm_region *r, union arg *args)
3500 struct swm_region *this_r, *other_r;
3502 struct workspace *new_ws, *old_ws;
3503 xcb_window_t none = XCB_WINDOW_NONE;
3504 int wsid = args->id, unmap_old = 0;
3509 if (wsid >= workspace_limit)
3513 old_ws = this_r->ws;
3514 new_ws = &this_r->s->ws[wsid];
3516 DNPRINTF(SWM_D_WS, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
3517 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), old_ws->idx, wsid);
3519 if (new_ws == NULL || old_ws == NULL)
3521 if (new_ws == old_ws)
3524 if ((win = old_ws->focus) != NULL) {
3525 xcb_change_window_attributes(conn, win->id, XCB_CW_BORDER_PIXEL,
3526 &win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].pixel);
3528 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
3529 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1,
3533 other_r = new_ws->r;
3534 if (other_r == NULL) {
3535 /* the other workspace is hidden, hide this one */
3539 /* the other ws is visible in another region, exchange them */
3540 other_r->ws_prior = new_ws;
3541 other_r->ws = old_ws;
3542 old_ws->r = other_r;
3544 this_r->ws_prior = old_ws;
3545 this_r->ws = new_ws;
3548 /* Set focus_pending before stacking, if needed. */
3549 if (focus_mode != SWM_FOCUS_FOLLOW && (!new_ws->focus_pending ||
3550 validate_win(new_ws->focus_pending))) {
3551 new_ws->focus_pending = get_region_focus(new_ws->r);
3552 new_ws->focus = new_ws->focus_prev;
3553 new_ws->focus_prev = NULL;
3558 /* unmap old windows */
3560 TAILQ_FOREACH(win, &old_ws->winlist, entry)
3563 /* if workspaces were swapped, then don't wait to set focus */
3564 if (old_ws->r && focus_mode != SWM_FOCUS_FOLLOW) {
3565 if (new_ws->focus_pending) {
3566 focus_win(new_ws->focus_pending);
3567 new_ws->focus_pending = NULL;
3571 /* Clear bar and set focus on region input win if new ws is empty. */
3572 if (new_ws->focus_pending == NULL && new_ws->focus == NULL) {
3573 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT, r->id,
3580 DNPRINTF(SWM_D_WS, "switchws: done.\n");
3584 cyclews(struct swm_region *r, union arg *args)
3587 struct swm_screen *s = r->s;
3591 DNPRINTF(SWM_D_WS, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3592 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
3598 case SWM_ARG_ID_CYCLEWS_MOVE_UP:
3601 case SWM_ARG_ID_CYCLEWS_UP_ALL:
3604 case SWM_ARG_ID_CYCLEWS_UP:
3605 a.id = (a.id < workspace_limit - 1) ? a.id + 1 : 0;
3607 case SWM_ARG_ID_CYCLEWS_MOVE_DOWN:
3610 case SWM_ARG_ID_CYCLEWS_DOWN_ALL:
3613 case SWM_ARG_ID_CYCLEWS_DOWN:
3614 a.id = (a.id > 0) ? a.id - 1 : workspace_limit - 1;
3621 (!cycle_empty && TAILQ_EMPTY(&s->ws[a.id].winlist)))
3623 if (!cycle_visible && s->ws[a.id].r != NULL)
3630 } while (a.id != r->ws->idx);
3634 priorws(struct swm_region *r, union arg *args)
3640 DNPRINTF(SWM_D_WS, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3641 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
3643 if (r->ws_prior == NULL)
3646 a.id = r->ws_prior->idx;
3651 focusrg(struct swm_region *r, union arg *args)
3653 int ridx = args->id, i, num_screens;
3654 struct swm_region *rr = NULL;
3656 num_screens = get_screen_count();
3657 /* do nothing if we don't have more than one screen */
3658 if (!(num_screens > 1 || outputs > 1))
3661 DNPRINTF(SWM_D_FOCUS, "focusrg: id: %d\n", ridx);
3663 rr = TAILQ_FIRST(&r->s->rl);
3664 for (i = 0; i < ridx && rr != NULL; ++i)
3665 rr = TAILQ_NEXT(rr, entry);
3675 cyclerg(struct swm_region *r, union arg *args)
3677 struct swm_region *rr = NULL;
3680 num_screens = get_screen_count();
3681 /* do nothing if we don't have more than one screen */
3682 if (!(num_screens > 1 || outputs > 1))
3687 case SWM_ARG_ID_CYCLERG_UP:
3688 rr = TAILQ_NEXT(r, entry);
3690 rr = TAILQ_FIRST(&screens[i].rl);
3692 case SWM_ARG_ID_CYCLERG_DOWN:
3693 rr = TAILQ_PREV(r, swm_region_list, entry);
3695 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
3708 sort_windows(struct ws_win_list *wl)
3710 struct ws_win *win, *parent, *nxt;
3715 for (win = TAILQ_FIRST(wl); win != TAILQ_END(wl); win = nxt) {
3716 nxt = TAILQ_NEXT(win, entry);
3717 if (win->transient) {
3718 parent = find_window(win->transient);
3719 if (parent == NULL) {
3720 warnx("not possible bug");
3723 TAILQ_REMOVE(wl, win, entry);
3724 TAILQ_INSERT_AFTER(wl, parent, win, entry);
3731 swapwin(struct swm_region *r, union arg *args)
3733 struct ws_win *target, *source;
3734 struct ws_win *cur_focus;
3735 struct ws_win_list *wl;
3737 DNPRINTF(SWM_D_WS, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3738 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
3740 cur_focus = r->ws->focus;
3741 if (cur_focus == NULL)
3745 wl = &source->ws->winlist;
3748 case SWM_ARG_ID_SWAPPREV:
3749 if (source->transient)
3750 source = find_window(source->transient);
3751 target = TAILQ_PREV(source, ws_win_list, entry);
3752 if (target && target->transient)
3753 target = find_window(target->transient);
3754 TAILQ_REMOVE(wl, source, entry);
3756 TAILQ_INSERT_TAIL(wl, source, entry);
3758 TAILQ_INSERT_BEFORE(target, source, entry);
3760 case SWM_ARG_ID_SWAPNEXT:
3761 target = TAILQ_NEXT(source, entry);
3762 /* move the parent and let the sort handle the move */
3763 if (source->transient)
3764 source = find_window(source->transient);
3765 TAILQ_REMOVE(wl, source, entry);
3767 TAILQ_INSERT_HEAD(wl, source, entry);
3769 TAILQ_INSERT_AFTER(wl, target, source, entry);
3771 case SWM_ARG_ID_SWAPMAIN:
3772 target = TAILQ_FIRST(wl);
3773 if (target == source) {
3774 if (source->ws->focus_prev != NULL &&
3775 source->ws->focus_prev != target)
3776 source = source->ws->focus_prev;
3780 if (target == NULL || source == NULL)
3782 source->ws->focus_prev = target;
3783 TAILQ_REMOVE(wl, target, entry);
3784 TAILQ_INSERT_BEFORE(source, target, entry);
3785 TAILQ_REMOVE(wl, source, entry);
3786 TAILQ_INSERT_HEAD(wl, source, entry);
3788 case SWM_ARG_ID_MOVELAST:
3789 TAILQ_REMOVE(wl, source, entry);
3790 TAILQ_INSERT_TAIL(wl, source, entry);
3793 DNPRINTF(SWM_D_MOVE, "swapwin: invalid id: %d\n", args->id);
3805 get_focus_prev(struct ws_win *win)
3807 struct ws_win *winfocus = NULL;
3808 struct ws_win *cur_focus = NULL;
3809 struct ws_win_list *wl = NULL;
3810 struct workspace *ws = NULL;
3812 if (!(win && win->ws))
3817 cur_focus = ws->focus;
3819 DNPRINTF(SWM_D_FOCUS, "get_focus_prev: window: 0x%x, cur_focus: 0x%x\n",
3820 WINID(win), WINID(cur_focus));
3822 /* pickle, just focus on whatever */
3823 if (cur_focus == NULL) {
3824 /* use prev_focus if valid */
3825 if (ws->focus_prev && ws->focus_prev != cur_focus &&
3826 find_window(WINID(ws->focus_prev)))
3827 winfocus = ws->focus_prev;
3831 /* if transient focus on parent */
3832 if (cur_focus->transient) {
3833 winfocus = find_window(cur_focus->transient);
3837 /* if in max_stack try harder */
3838 if ((win->quirks & SWM_Q_FOCUSPREV) ||
3839 (ws->cur_layout->flags & SWM_L_FOCUSPREV)) {
3840 if (cur_focus != ws->focus_prev)
3841 winfocus = ws->focus_prev;
3843 winfocus = TAILQ_PREV(win, ws_win_list, entry);
3848 DNPRINTF(SWM_D_FOCUS, "get_focus_prev: focus_close: %d\n", focus_close);
3850 if (winfocus == NULL || winfocus == win) {
3851 switch (focus_close) {
3852 case SWM_STACK_BOTTOM:
3853 TAILQ_FOREACH(winfocus, wl, entry)
3854 if (!winfocus->iconic && winfocus != cur_focus)
3858 TAILQ_FOREACH_REVERSE(winfocus, wl, ws_win_list, entry)
3859 if (!winfocus->iconic && winfocus != cur_focus)
3862 case SWM_STACK_ABOVE:
3863 winfocus = TAILQ_NEXT(cur_focus, entry);
3864 while (winfocus && winfocus->iconic)
3865 winfocus = TAILQ_NEXT(winfocus, entry);
3867 if (winfocus == NULL) {
3868 if (focus_close_wrap) {
3869 TAILQ_FOREACH(winfocus, wl, entry)
3870 if (!winfocus->iconic &&
3871 winfocus != cur_focus)
3874 TAILQ_FOREACH_REVERSE(winfocus, wl,
3876 if (!winfocus->iconic &&
3877 winfocus != cur_focus)
3882 case SWM_STACK_BELOW:
3883 winfocus = TAILQ_PREV(cur_focus, ws_win_list, entry);
3884 while (winfocus && winfocus->iconic)
3885 winfocus = TAILQ_PREV(winfocus, ws_win_list,
3888 if (winfocus == NULL) {
3889 if (focus_close_wrap) {
3890 TAILQ_FOREACH_REVERSE(winfocus, wl,
3892 if (!winfocus->iconic &&
3893 winfocus != cur_focus)
3896 TAILQ_FOREACH(winfocus, wl, entry)
3897 if (!winfocus->iconic &&
3898 winfocus != cur_focus)
3906 if (winfocus == NULL ||
3907 (winfocus && (winfocus->iconic || winfocus == cur_focus))) {
3908 if (focus_default == SWM_STACK_TOP) {
3909 TAILQ_FOREACH_REVERSE(winfocus, wl, ws_win_list, entry)
3910 if (!winfocus->iconic && winfocus != cur_focus)
3913 TAILQ_FOREACH(winfocus, wl, entry)
3914 if (!winfocus->iconic && winfocus != cur_focus)
3921 return get_focus_magic(winfocus);
3925 get_region_focus(struct swm_region *r)
3927 struct ws_win *winfocus = NULL;
3932 if (r->ws->focus && !r->ws->focus->iconic)
3933 winfocus = r->ws->focus;
3934 else if (r->ws->focus_prev && !r->ws->focus_prev->iconic)
3935 winfocus = r->ws->focus_prev;
3937 TAILQ_FOREACH(winfocus, &r->ws->winlist, entry)
3938 if (!winfocus->iconic)
3941 return get_focus_magic(winfocus);
3945 focus(struct swm_region *r, union arg *args)
3947 struct ws_win *head, *cur_focus = NULL, *winfocus = NULL;
3948 struct ws_win_list *wl = NULL;
3949 struct workspace *ws = NULL;
3952 xcb_icccm_wm_hints_t hints;
3957 DNPRINTF(SWM_D_FOCUS, "focus: id: %d\n", args->id);
3959 cur_focus = r->ws->focus;
3963 /* Make sure an uniconified window has focus, if one exists. */
3964 if (cur_focus == NULL) {
3965 cur_focus = TAILQ_FIRST(wl);
3966 while (cur_focus != NULL && cur_focus->iconic)
3967 cur_focus = TAILQ_NEXT(cur_focus, entry);
3971 case SWM_ARG_ID_FOCUSPREV:
3972 if (cur_focus == NULL)
3975 winfocus = cur_focus;
3977 winfocus = TAILQ_PREV(winfocus, ws_win_list, entry);
3978 if (winfocus == NULL)
3979 winfocus = TAILQ_LAST(wl, ws_win_list);
3980 if (winfocus == cur_focus)
3982 } while (winfocus != NULL &&
3983 (winfocus->iconic || winfocus->id == cur_focus->transient));
3985 case SWM_ARG_ID_FOCUSNEXT:
3986 if (cur_focus == NULL)
3989 winfocus = cur_focus;
3991 winfocus = TAILQ_NEXT(winfocus, entry);
3992 if (winfocus == NULL)
3993 winfocus = TAILQ_FIRST(wl);
3994 if (winfocus == cur_focus)
3996 } while (winfocus != NULL &&
3997 (winfocus->iconic || winfocus->id == cur_focus->transient));
3999 case SWM_ARG_ID_FOCUSMAIN:
4000 if (cur_focus == NULL)
4003 winfocus = TAILQ_FIRST(wl);
4004 if (winfocus == cur_focus)
4005 winfocus = cur_focus->ws->focus_prev;
4007 case SWM_ARG_ID_FOCUSURGENT:
4008 /* Search forward for the next urgent window. */
4012 for (i = 0; i <= workspace_limit; ++i) {
4014 head = TAILQ_FIRST(&r->s->ws[(ws->idx + i) %
4015 workspace_limit].winlist);
4017 while (head != NULL &&
4018 (head = TAILQ_NEXT(head, entry)) != NULL) {
4019 if (head == cur_focus) {
4020 winfocus = cur_focus;
4023 if (xcb_icccm_get_wm_hints_reply(conn,
4024 xcb_icccm_get_wm_hints(conn, head->id),
4025 &hints, NULL) != 0 &&
4026 xcb_icccm_wm_hints_get_urgency(&hints)) {
4032 if (winfocus != NULL)
4036 /* Switch ws if new focus is on a different ws. */
4037 if (winfocus != NULL && winfocus->ws != ws) {
4038 a.id = winfocus->ws->idx;
4046 focus_win(get_focus_magic(winfocus));
4050 DNPRINTF(SWM_D_FOCUS, "focus: done\n");
4054 cycle_layout(struct swm_region *r, union arg *args)
4056 struct workspace *ws = r->ws;
4058 /* suppress unused warning since var is needed */
4061 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
4064 if (ws->cur_layout->l_stack == NULL)
4065 ws->cur_layout = &layouts[0];
4070 focus_win(get_region_focus(r));
4076 stack_config(struct swm_region *r, union arg *args)
4078 struct workspace *ws = r->ws;
4080 DNPRINTF(SWM_D_STACK, "stack_config: id: %d workspace: %d\n",
4083 if (ws->cur_layout->l_config != NULL)
4084 ws->cur_layout->l_config(ws, args->id);
4086 if (args->id != SWM_ARG_ID_STACKINIT)
4095 struct swm_geometry g;
4096 struct swm_region *r;
4102 DNPRINTF(SWM_D_STACK, "stack: begin\n");
4104 num_screens = get_screen_count();
4105 for (i = 0; i < num_screens; i++) {
4109 TAILQ_FOREACH(r, &screens[i].rl, entry) {
4110 DNPRINTF(SWM_D_STACK, "stack: workspace: %d "
4111 "(screen: %d, region: %d)\n", r->ws->idx, i, j++);
4113 /* Adjust stack area for region bar and padding. */
4115 g.x += region_padding;
4116 g.y += region_padding;
4117 g.w -= 2 * border_width + 2 * region_padding;
4118 g.h -= 2 * border_width + 2 * region_padding;
4119 if (bar_enabled && r->ws->bar_enabled) {
4124 r->ws->cur_layout->l_stack(r->ws, &g);
4125 r->ws->cur_layout->l_string(r->ws);
4126 /* save r so we can track region changes */
4133 DNPRINTF(SWM_D_STACK, "stack: end\n");
4137 store_float_geom(struct ws_win *win)
4139 if (win == NULL || win->ws->r == NULL)
4142 /* retain window geom and region geom */
4143 win->g_float = win->g;
4144 win->g_float.x -= X(win->ws->r);
4145 win->g_float.y -= Y(win->ws->r);
4146 win->g_floatvalid = 1;
4147 DNPRINTF(SWM_D_MISC, "store_float_geom: window: 0x%x, g: (%d,%d)"
4148 " %d x %d, g_float: (%d,%d) %d x %d\n", win->id, X(win), Y(win),
4149 WIDTH(win), HEIGHT(win), win->g_float.x, win->g_float.y,
4150 win->g_float.w, win->g_float.h);
4154 load_float_geom(struct ws_win *win)
4156 if (win == NULL || win->ws->r == NULL)
4159 if (win->g_floatvalid) {
4160 win->g = win->g_float;
4161 X(win) += X(win->ws->r);
4162 Y(win) += Y(win->ws->r);
4163 DNPRINTF(SWM_D_MISC, "load_float_geom: window: 0x%x, g: (%d,%d)"
4164 "%d x %d\n", win->id, X(win), Y(win), WIDTH(win),
4167 DNPRINTF(SWM_D_MISC, "load_float_geom: window: 0x%x, g_float "
4168 "is not set.\n", win->id);
4173 stack_floater(struct ws_win *win, struct swm_region *r)
4178 DNPRINTF(SWM_D_MISC, "stack_floater: window: 0x%x\n", win->id);
4181 * to allow windows to change their size (e.g. mplayer fs) only retrieve
4182 * geom on ws switches or return from max mode
4184 if (win->floatmaxed || (r != r->ws->old_r &&
4185 !(win->ewmh_flags & EWMH_F_FULLSCREEN))) {
4186 /* update geometry for the new region */
4187 load_float_geom(win);
4190 win->floatmaxed = 0;
4193 * if set to fullscreen mode, configure window to maximum size.
4195 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
4196 if (!win->g_floatvalid)
4197 store_float_geom(win);
4203 * remove border on fullscreen floater when in fullscreen mode or when
4204 * the quirk is present.
4206 if ((win->ewmh_flags & EWMH_F_FULLSCREEN) ||
4207 ((win->quirks & SWM_Q_FULLSCREEN) &&
4208 (WIDTH(win) >= WIDTH(r)) && (HEIGHT(win) >= HEIGHT(r)))) {
4209 if (win->bordered) {
4211 X(win) += border_width;
4212 Y(win) += border_width;
4214 } else if (!win->bordered) {
4216 X(win) -= border_width;
4217 Y(win) -= border_width;
4220 if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
4221 WIDTH(win) = (double)WIDTH(r) * dialog_ratio;
4222 HEIGHT(win) = (double)HEIGHT(r) * dialog_ratio;
4225 if (!win->manual && !(win->ewmh_flags & EWMH_F_FULLSCREEN) &&
4226 !(win->quirks & SWM_Q_ANYWHERE)) {
4228 * floaters and transients are auto-centred unless moved,
4229 * resized or ANYWHERE quirk is set.
4231 X(win) = X(r) + (WIDTH(r) - WIDTH(win)) / 2 - BORDER(win);
4232 Y(win) = Y(r) + (HEIGHT(r) - HEIGHT(win)) / 2 - BORDER(win);
4234 store_float_geom(win);
4237 /* Ensure at least 1 pixel of the window is in the region. */
4238 region_containment(win, r, SWM_CW_ALLSIDES);
4244 * Send keystrokes to terminal to decrease/increase the font size as the
4245 * window size changes.
4248 adjust_font(struct ws_win *win)
4250 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
4251 win->floating || win->transient)
4254 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
4255 WIDTH(win) / win->sh.width_inc < term_width &&
4256 win->font_steps < SWM_MAX_FONT_STEPS) {
4257 win->font_size_boundary[win->font_steps] =
4258 (win->sh.width_inc * term_width) + win->sh.base_width;
4261 win->last_inc = win->sh.width_inc;
4262 fake_keypress(win, XK_KP_Subtract, XCB_MOD_MASK_SHIFT);
4263 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
4264 WIDTH(win) > win->font_size_boundary[win->font_steps - 1]) {
4267 win->last_inc = win->sh.width_inc;
4268 fake_keypress(win, XK_KP_Add, XCB_MOD_MASK_SHIFT);
4272 #define SWAPXY(g) do { \
4274 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
4275 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
4278 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
4280 struct swm_geometry win_g, r_g = *g;
4281 struct ws_win *win, *fs_win = NULL;
4282 int i, j, s, stacks;
4283 int w_inc = 1, h_inc, w_base = 1, h_base;
4284 int hrh, extra = 0, h_slice, last_h = 0;
4285 int split, colno, winno, mwin, msize, mscale;
4286 int remain, missing, v_slice, reconfigure = 0;
4289 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d, rot: %s, "
4290 "flip: %s\n", ws->idx, YESNO(rot), YESNO(flip));
4292 winno = count_win(ws, 0);
4293 if (winno == 0 && count_win(ws, 1) == 0)
4296 TAILQ_FOREACH(win, &ws->winlist, entry)
4297 if (!win->transient && !win->floating && !win->iconic)
4304 w_inc = win->sh.width_inc;
4305 w_base = win->sh.base_width;
4306 mwin = ws->l_state.horizontal_mwin;
4307 mscale = ws->l_state.horizontal_msize;
4308 stacks = ws->l_state.horizontal_stacks;
4311 w_inc = win->sh.height_inc;
4312 w_base = win->sh.base_height;
4313 mwin = ws->l_state.vertical_mwin;
4314 mscale = ws->l_state.vertical_msize;
4315 stacks = ws->l_state.vertical_stacks;
4319 if (stacks > winno - mwin)
4320 stacks = winno - mwin;
4324 h_slice = r_g.h / SWM_H_SLICE;
4325 if (mwin && winno > mwin) {
4326 v_slice = r_g.w / SWM_V_SLICE;
4330 win_g.w = v_slice * mscale;
4332 if (w_inc > 1 && w_inc < v_slice) {
4333 /* adjust for window's requested size increment */
4334 remain = (win_g.w - w_base) % w_inc;
4340 win_g.x += r_g.w - msize;
4343 colno = split = winno / stacks;
4344 win_g.w = ((r_g.w - (stacks * 2 * border_width) +
4345 2 * border_width) / stacks);
4347 hrh = r_g.h / colno;
4348 extra = r_g.h - (colno * hrh);
4349 win_g.h = hrh - 2 * border_width;
4351 /* stack all the tiled windows */
4352 i = j = 0, s = stacks;
4353 TAILQ_FOREACH(win, &ws->winlist, entry) {
4354 if (win->transient || win->floating || win->iconic)
4357 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
4362 if (split && i == split) {
4363 colno = (winno - mwin) / stacks;
4364 if (s <= (winno - mwin) % stacks)
4367 hrh = r_g.h / colno;
4368 extra = r_g.h - (colno * hrh);
4371 win_g.x += win_g.w + 2 * border_width +
4374 win_g.w = (r_g.w - msize -
4375 (stacks * (2 * border_width + tile_gap))) / stacks;
4377 win_g.w += (r_g.w - msize -
4378 (stacks * (2 * border_width + tile_gap))) %
4382 win_g.x -= win_g.w + 2 * border_width +
4388 win_g.h = hrh - 2 * border_width - tile_gap;
4391 h_inc = win->sh.width_inc;
4392 h_base = win->sh.base_width;
4394 h_inc = win->sh.height_inc;
4395 h_base = win->sh.base_height;
4398 if (j == colno - 1) {
4399 win_g.h = hrh + extra;
4400 } else if (h_inc > 1 && h_inc < h_slice) {
4401 /* adjust for window's requested size increment */
4402 remain = (win_g.h - h_base) % h_inc;
4403 missing = h_inc - remain;
4405 if (missing <= extra || j == 0) {
4417 win_g.y += last_h + 2 * border_width + tile_gap;
4419 if (disable_border && !(bar_enabled && ws->bar_enabled) &&
4422 win_g.w += 2 * border_width;
4423 win_g.h += 2 * border_width;
4429 if (X(win) != win_g.y || Y(win) != win_g.x ||
4430 WIDTH(win) != win_g.h || HEIGHT(win) != win_g.w) {
4434 WIDTH(win) = win_g.h;
4435 HEIGHT(win) = win_g.w;
4438 if (X(win) != win_g.x || Y(win) != win_g.y ||
4439 WIDTH(win) != win_g.w || HEIGHT(win) != win_g.h) {
4443 WIDTH(win) = win_g.w;
4444 HEIGHT(win) = win_g.h;
4448 if (bordered != win->bordered) {
4450 win->bordered = bordered;
4458 map_window(win, get_sibling(win, SWM_STACK_BELOW));
4465 /* Map/raise focused tiled window to top if windows could overlap. */
4466 if (tile_gap < 0 && ws->focus != NULL && !ws->focus->floating)
4467 map_window(ws->focus, get_sibling(ws->focus, SWM_STACK_TOP));
4470 /* now, stack all the floaters and transients */
4471 TAILQ_FOREACH(win, &ws->winlist, entry) {
4472 if (!win->transient && !win->floating)
4476 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
4481 stack_floater(win, ws->r);
4482 map_window(win, XCB_WINDOW_NONE);
4485 /* Make sure fs_win is stacked last so it's on top. */
4487 stack_floater(fs_win, ws->r);
4488 map_window(fs_win, XCB_WINDOW_NONE);
4493 vertical_config(struct workspace *ws, int id)
4495 DNPRINTF(SWM_D_STACK, "vertical_config: id: %d, workspace: %d\n",
4499 case SWM_ARG_ID_STACKRESET:
4500 case SWM_ARG_ID_STACKINIT:
4501 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
4502 ws->l_state.vertical_mwin = 1;
4503 ws->l_state.vertical_stacks = 1;
4505 case SWM_ARG_ID_MASTERSHRINK:
4506 if (ws->l_state.vertical_msize > 1)
4507 ws->l_state.vertical_msize--;
4509 case SWM_ARG_ID_MASTERGROW:
4510 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
4511 ws->l_state.vertical_msize++;
4513 case SWM_ARG_ID_MASTERADD:
4514 ws->l_state.vertical_mwin++;
4516 case SWM_ARG_ID_MASTERDEL:
4517 if (ws->l_state.vertical_mwin > 0)
4518 ws->l_state.vertical_mwin--;
4520 case SWM_ARG_ID_STACKINC:
4521 ws->l_state.vertical_stacks++;
4523 case SWM_ARG_ID_STACKDEC:
4524 if (ws->l_state.vertical_stacks > 1)
4525 ws->l_state.vertical_stacks--;
4527 case SWM_ARG_ID_FLIPLAYOUT:
4528 ws->l_state.vertical_flip = !ws->l_state.vertical_flip;
4536 vertical_stack(struct workspace *ws, struct swm_geometry *g)
4538 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
4540 stack_master(ws, g, 0, ws->l_state.vertical_flip);
4544 horizontal_config(struct workspace *ws, int id)
4546 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
4549 case SWM_ARG_ID_STACKRESET:
4550 case SWM_ARG_ID_STACKINIT:
4551 ws->l_state.horizontal_mwin = 1;
4552 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
4553 ws->l_state.horizontal_stacks = 1;
4555 case SWM_ARG_ID_MASTERSHRINK:
4556 if (ws->l_state.horizontal_msize > 1)
4557 ws->l_state.horizontal_msize--;
4559 case SWM_ARG_ID_MASTERGROW:
4560 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
4561 ws->l_state.horizontal_msize++;
4563 case SWM_ARG_ID_MASTERADD:
4564 ws->l_state.horizontal_mwin++;
4566 case SWM_ARG_ID_MASTERDEL:
4567 if (ws->l_state.horizontal_mwin > 0)
4568 ws->l_state.horizontal_mwin--;
4570 case SWM_ARG_ID_STACKINC:
4571 ws->l_state.horizontal_stacks++;
4573 case SWM_ARG_ID_STACKDEC:
4574 if (ws->l_state.horizontal_stacks > 1)
4575 ws->l_state.horizontal_stacks--;
4577 case SWM_ARG_ID_FLIPLAYOUT:
4578 ws->l_state.horizontal_flip = !ws->l_state.horizontal_flip;
4586 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
4588 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
4590 stack_master(ws, g, 1, ws->l_state.horizontal_flip);
4593 /* fullscreen view */
4595 max_stack(struct workspace *ws, struct swm_geometry *g)
4597 struct swm_geometry gg = *g;
4598 struct ws_win *w, *win = NULL, *parent = NULL;
4601 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
4606 winno = count_win(ws, 0);
4607 if (winno == 0 && count_win(ws, 1) == 0)
4610 /* Figure out which top level window should be visible. */
4611 if (ws->focus_pending)
4612 win = ws->focus_pending;
4615 else if (ws->focus_prev)
4616 win = ws->focus_prev;
4618 win = TAILQ_FIRST(&ws->winlist);
4621 parent = find_window(win->transient);
4623 DNPRINTF(SWM_D_STACK, "max_stack: win: 0x%x\n", win->id);
4625 /* maximize all top level windows */
4626 TAILQ_FOREACH(w, &ws->winlist, entry) {
4627 if (w->transient || w->iconic)
4630 if (!w->mapped && w != win)
4631 map_window(w, XCB_WINDOW_NONE);
4633 if (w->floating && !w->floatmaxed) {
4635 * retain geometry for retrieval on exit from
4638 store_float_geom(w);
4642 /* only reconfigure if necessary */
4643 if (X(w) != gg.x || Y(w) != gg.y || WIDTH(w) != gg.w ||
4644 HEIGHT(w) != gg.h) {
4646 if (bar_enabled && ws->bar_enabled){
4650 WIDTH(w) += 2 * border_width;
4651 HEIGHT(w) += 2 * border_width;
4658 /* If a parent exists, map/raise it first. */
4660 map_window(parent, XCB_WINDOW_NONE);
4662 /* Map siblings next. */
4663 TAILQ_FOREACH(w, &ws->winlist, entry)
4664 if (w != win && !w->iconic &&
4665 w->transient == parent->id) {
4666 stack_floater(w, ws->r);
4667 map_window(w, XCB_WINDOW_NONE);
4671 /* Map/raise focused window. */
4672 map_window(win, XCB_WINDOW_NONE);
4674 /* Finally, map/raise children of focus window. */
4675 TAILQ_FOREACH(w, &ws->winlist, entry)
4676 if (w->transient == win->id && !w->iconic) {
4677 stack_floater(w, ws->r);
4678 map_window(w, XCB_WINDOW_NONE);
4683 send_to_rg(struct swm_region *r, union arg *args)
4685 int ridx = args->id, i, num_screens;
4686 struct swm_region *rr = NULL;
4689 num_screens = get_screen_count();
4690 /* do nothing if we don't have more than one screen */
4691 if (!(num_screens > 1 || outputs > 1))
4694 DNPRINTF(SWM_D_FOCUS, "send_to_rg: id: %d\n", ridx);
4696 rr = TAILQ_FIRST(&r->s->rl);
4697 for (i = 0; i < ridx && rr != NULL; ++i)
4698 rr = TAILQ_NEXT(rr, entry);
4709 region_under(struct swm_screen *s, int x, int y)
4711 struct swm_region *r = NULL;
4713 TAILQ_FOREACH(r, &s->rl, entry) {
4714 DNPRINTF(SWM_D_MISC, "region_under: ws: %d, region g: (%d,%d) "
4715 "%d x %d, coords: (%d,%d)\n", r->ws->idx, X(r), Y(r),
4716 WIDTH(r), HEIGHT(r), x, y);
4717 if (X(r) <= x && x < MAX_X(r))
4718 if (Y(r) <= y && y < MAX_Y(r))
4726 send_to_ws(struct swm_region *r, union arg *args)
4728 int wsid = args->id;
4729 struct ws_win *win = NULL;
4731 if (r && r->ws && r->ws->focus)
4736 DNPRINTF(SWM_D_MOVE, "send_to_ws: win 0x%x, ws %d\n", win->id, wsid);
4738 win_to_ws(win, wsid, 1);
4740 /* Restack and set new focus. */
4743 if (focus_mode != SWM_FOCUS_FOLLOW) {
4744 if (r->ws->focus_pending) {
4745 focus_win(r->ws->focus_pending);
4746 r->ws->focus_pending = NULL;
4748 xcb_set_input_focus(conn,
4749 XCB_INPUT_FOCUS_PARENT, r->id,
4758 win_to_ws(struct ws_win *win, int wsid, int unfocus)
4760 struct ws_win *parent;
4761 struct workspace *ws, *nws, *pws;
4762 char ws_idx_str[SWM_PROPLEN];
4764 if (wsid >= workspace_limit)
4767 if (win->ws->idx == wsid)
4771 nws = &win->s->ws[wsid];
4773 DNPRINTF(SWM_D_MOVE, "win_to_ws: win 0x%x, ws %d -> %d\n", win->id,
4776 /* Update the window's workspace property: _SWM_WS */
4777 if (snprintf(ws_idx_str, SWM_PROPLEN, "%d", nws->idx) < SWM_PROPLEN) {
4778 if (focus_mode != SWM_FOCUS_FOLLOW)
4779 ws->focus_pending = get_focus_prev(win);
4781 /* Move the parent if this is a transient window. */
4782 if (win->transient) {
4783 parent = find_window(win->transient);
4786 /* Set new focus in parent's ws if needed. */
4787 if (pws->focus == parent) {
4788 if (focus_mode != SWM_FOCUS_FOLLOW)
4789 pws->focus_pending =
4790 get_focus_prev(parent);
4792 unfocus_win(parent);
4794 if (focus_mode != SWM_FOCUS_FOLLOW) {
4795 pws->focus = pws->focus_pending;
4796 pws->focus_pending = NULL;
4800 /* Don't unmap parent if new ws is visible */
4802 unmap_window(parent);
4805 TAILQ_REMOVE(&ws->winlist, parent, entry);
4806 TAILQ_INSERT_TAIL(&nws->winlist, parent, entry);
4809 DNPRINTF(SWM_D_PROP, "win_to_ws: set "
4810 "property: _SWM_WS: %s\n", ws_idx_str);
4811 xcb_change_property(conn, XCB_PROP_MODE_REPLACE,
4812 parent->id, a_swm_ws, XCB_ATOM_STRING, 8,
4813 strlen(ws_idx_str), ws_idx_str);
4820 /* Don't unmap if new ws is visible */
4825 TAILQ_REMOVE(&ws->winlist, win, entry);
4826 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
4829 /* Set focus on new ws. */
4830 unfocus_win(nws->focus);
4833 DNPRINTF(SWM_D_PROP, "win_to_ws: set property: _SWM_WS: %s\n",
4835 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
4836 a_swm_ws, XCB_ATOM_STRING, 8, strlen(ws_idx_str),
4840 DNPRINTF(SWM_D_MOVE, "win_to_ws: done.\n");
4844 pressbutton(struct swm_region *r, union arg *args)
4846 /* suppress unused warning since var is needed */
4849 xcb_test_fake_input(conn, XCB_BUTTON_PRESS, args->id,
4850 XCB_CURRENT_TIME, XCB_WINDOW_NONE, 0, 0, 0);
4851 xcb_test_fake_input(conn, XCB_BUTTON_RELEASE, args->id,
4852 XCB_CURRENT_TIME, XCB_WINDOW_NONE, 0, 0, 0);
4856 raise_toggle(struct swm_region *r, union arg *args)
4858 /* suppress unused warning since var is needed */
4861 if (r == NULL || r->ws == NULL)
4864 r->ws->always_raise = !r->ws->always_raise;
4866 /* bring floaters back to top */
4867 if (!r->ws->always_raise)
4874 iconify(struct swm_region *r, union arg *args)
4876 /* suppress unused warning since var is needed */
4879 if (r->ws->focus == NULL)
4882 set_swm_iconic(r->ws->focus, 1);
4888 get_win_name(xcb_window_t win)
4891 xcb_get_property_cookie_t c;
4892 xcb_get_property_reply_t *r;
4894 /* First try _NET_WM_NAME for UTF-8. */
4895 c = xcb_get_property(conn, 0, win, ewmh[_NET_WM_NAME].atom,
4896 XCB_GET_PROPERTY_TYPE_ANY, 0, UINT_MAX);
4897 r = xcb_get_property_reply(conn, c, NULL);
4900 if (r->type == XCB_NONE) {
4902 /* Use WM_NAME instead; no UTF-8. */
4903 c = xcb_get_property(conn, 0, win, XCB_ATOM_WM_NAME,
4904 XCB_GET_PROPERTY_TYPE_ANY, 0, UINT_MAX);
4905 r = xcb_get_property_reply(conn, c, NULL);
4909 if (r->type == XCB_NONE) {
4915 name = strndup(xcb_get_property_value(r),
4916 xcb_get_property_value_length(r));
4925 uniconify(struct swm_region *r, union arg *args)
4932 DNPRINTF(SWM_D_MISC, "uniconify\n");
4934 if (r == NULL || r->ws == NULL)
4937 /* make sure we have anything to uniconify */
4938 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
4939 if (win->ws == NULL)
4940 continue; /* should never happen */
4949 search_resp_action = SWM_SEARCH_UNICONIFY;
4951 spawn_select(r, args, "search", &searchpid);
4953 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
4956 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
4957 if (win->ws == NULL)
4958 continue; /* should never happen */
4962 name = get_win_name(win->id);
4965 fprintf(lfile, "%s.%u\n", name, win->id);
4973 name_workspace(struct swm_region *r, union arg *args)
4977 DNPRINTF(SWM_D_MISC, "name_workspace\n");
4983 search_resp_action = SWM_SEARCH_NAME_WORKSPACE;
4985 spawn_select(r, args, "name_workspace", &searchpid);
4987 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
4990 fprintf(lfile, "%s", "");
4995 search_workspace(struct swm_region *r, union arg *args)
4998 struct workspace *ws;
5001 DNPRINTF(SWM_D_MISC, "search_workspace\n");
5007 search_resp_action = SWM_SEARCH_SEARCH_WORKSPACE;
5009 spawn_select(r, args, "search", &searchpid);
5011 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
5014 for (i = 0; i < workspace_limit; i++) {
5018 fprintf(lfile, "%d%s%s\n", ws->idx + 1,
5019 (ws->name ? ":" : ""), (ws->name ? ws->name : ""));
5026 search_win_cleanup(void)
5028 struct search_window *sw = NULL;
5030 while ((sw = TAILQ_FIRST(&search_wl)) != NULL) {
5031 xcb_destroy_window(conn, sw->indicator);
5032 TAILQ_REMOVE(&search_wl, sw, entry);
5038 search_win(struct swm_region *r, union arg *args)
5040 struct ws_win *win = NULL;
5041 struct search_window *sw = NULL;
5044 int i, width, height;
5052 XRectangle l_ibox, l_lbox;
5054 DNPRINTF(SWM_D_MISC, "search_win\n");
5057 search_resp_action = SWM_SEARCH_SEARCH_WINDOW;
5059 spawn_select(r, args, "search", &searchpid);
5061 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
5064 TAILQ_INIT(&search_wl);
5067 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
5071 sw = calloc(1, sizeof(struct search_window));
5073 warn("search_win: calloc");
5075 search_win_cleanup();
5081 snprintf(s, sizeof s, "%d", i);
5084 w = xcb_generate_id(conn);
5085 wa[0] = r->s->c[SWM_S_COLOR_FOCUS].pixel;
5086 wa[1] = r->s->c[SWM_S_COLOR_UNFOCUS].pixel;
5088 if (bar_font_legacy) {
5089 XmbTextExtents(bar_fs, s, len, &l_ibox, &l_lbox);
5090 width = l_lbox.width + 4;
5091 height = bar_fs_extents->max_logical_extent.height + 4;
5093 XftTextExtentsUtf8(display, bar_font, (FcChar8 *)s, len,
5095 width = info.width + 4;
5096 height = bar_font->height + 4;
5099 xcb_create_window(conn, XCB_COPY_FROM_PARENT, w, win->id, 0, 0,
5100 width, height, 1, XCB_WINDOW_CLASS_INPUT_OUTPUT,
5101 XCB_COPY_FROM_PARENT, XCB_CW_BACK_PIXEL |
5102 XCB_CW_BORDER_PIXEL, wa);
5104 xcb_map_window(conn, w);
5107 TAILQ_INSERT_TAIL(&search_wl, sw, entry);
5109 if (bar_font_legacy) {
5110 l_gcv.graphics_exposures = 0;
5111 l_draw = XCreateGC(display, w, 0, &l_gcv);
5113 XSetForeground(display, l_draw,
5114 r->s->c[SWM_S_COLOR_BAR].pixel);
5116 DRAWSTRING(display, w, bar_fs, l_draw, 2,
5117 (bar_fs_extents->max_logical_extent.height -
5118 l_lbox.height) / 2 - l_lbox.y, s, len);
5120 XFreeGC(display, l_draw);
5123 draw = XftDrawCreate(display, w,
5124 DefaultVisual(display, r->s->idx),
5125 DefaultColormap(display, r->s->idx));
5127 XftDrawStringUtf8(draw, &bar_font_color, bar_font, 2,
5128 (HEIGHT(r->bar) + bar_font->height) / 2 -
5129 bar_font->descent, (FcChar8 *)s, len);
5131 XftDrawDestroy(draw);
5134 DNPRINTF(SWM_D_MISC, "search_win: mapped window: 0x%x\n", w);
5136 fprintf(lfile, "%d\n", i);
5146 search_resp_uniconify(const char *resp, size_t len)
5152 DNPRINTF(SWM_D_MISC, "search_resp_uniconify: resp: %s\n", resp);
5154 TAILQ_FOREACH(win, &search_r->ws->winlist, entry) {
5157 name = get_win_name(win->id);
5160 if (asprintf(&s, "%s.%u", name, win->id) == -1) {
5165 if (strncmp(s, resp, len) == 0) {
5166 /* XXX this should be a callback to generalize */
5167 set_swm_iconic(win, 0);
5176 search_resp_name_workspace(const char *resp, size_t len)
5178 struct workspace *ws;
5180 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: resp: %s\n", resp);
5182 if (search_r->ws == NULL)
5187 free(search_r->ws->name);
5188 search_r->ws->name = NULL;
5192 ws->name = strdup(resp);
5193 if (ws->name == NULL) {
5194 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: "
5195 "strdup: %s", strerror(errno));
5202 search_resp_search_workspace(const char *resp)
5209 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: resp: %s\n", resp);
5213 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: strdup: %s",
5220 ws_idx = (int)strtonum(q, 1, workspace_limit, &errstr);
5222 DNPRINTF(SWM_D_MISC, "workspace idx is %s: %s",
5229 switchws(search_r, &a);
5233 search_resp_search_window(const char *resp)
5238 struct search_window *sw;
5240 DNPRINTF(SWM_D_MISC, "search_resp_search_window: resp: %s\n", resp);
5244 DNPRINTF(SWM_D_MISC, "search_resp_search_window: strdup: %s",
5249 idx = (int)strtonum(s, 1, INT_MAX, &errstr);
5251 DNPRINTF(SWM_D_MISC, "window idx is %s: %s",
5258 TAILQ_FOREACH(sw, &search_wl, entry)
5259 if (idx == sw->idx) {
5265 #define MAX_RESP_LEN 1024
5268 search_do_resp(void)
5274 DNPRINTF(SWM_D_MISC, "search_do_resp:\n");
5279 if ((resp = calloc(1, MAX_RESP_LEN + 1)) == NULL) {
5280 warn("search: calloc");
5284 rbytes = read(select_resp_pipe[0], resp, MAX_RESP_LEN);
5286 warn("search: read error");
5289 resp[rbytes] = '\0';
5292 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
5293 * newline, so work around that by sanitizing the resp now.
5295 resp[strcspn(resp, "\n")] = '\0';
5298 switch (search_resp_action) {
5299 case SWM_SEARCH_UNICONIFY:
5300 search_resp_uniconify(resp, len);
5302 case SWM_SEARCH_NAME_WORKSPACE:
5303 search_resp_name_workspace(resp, len);
5305 case SWM_SEARCH_SEARCH_WORKSPACE:
5306 search_resp_search_workspace(resp);
5308 case SWM_SEARCH_SEARCH_WINDOW:
5309 search_resp_search_window(resp);
5314 if (search_resp_action == SWM_SEARCH_SEARCH_WINDOW)
5315 search_win_cleanup();
5317 search_resp_action = SWM_SEARCH_NONE;
5318 close(select_resp_pipe[0]);
5325 wkill(struct swm_region *r, union arg *args)
5327 DNPRINTF(SWM_D_MISC, "wkill: id: %d\n", args->id);
5329 if (r->ws->focus == NULL)
5332 if (args->id == SWM_ARG_ID_KILLWINDOW)
5333 xcb_kill_client(conn, r->ws->focus->id);
5335 if (r->ws->focus->can_delete)
5336 client_msg(r->ws->focus, a_delete, 0);
5342 floating_toggle_win(struct ws_win *win)
5350 /* reject floating toggles in max stack mode */
5351 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK])
5354 if (win->floating) {
5355 if (!win->floatmaxed) {
5356 /* retain position for refloat */
5357 store_float_geom(win);
5361 load_float_geom(win);
5365 ewmh_update_actions(win);
5371 floating_toggle(struct swm_region *r, union arg *args)
5373 struct ws_win *win = r->ws->focus;
5375 /* suppress unused warning since var is needed */
5381 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
5384 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
5385 _NET_WM_STATE_TOGGLE);
5389 if (win == win->ws->focus)
5396 region_containment(struct ws_win *win, struct swm_region *r, int opts)
5398 struct swm_geometry g = r->g;
5399 int rt, lt, tp, bm, bw;
5401 bw = (opts & SWM_CW_SOFTBOUNDARY) ? boundary_width : 0;
5404 * Perpendicular distance of each side of the window to the respective
5405 * side of the region boundary. Positive values indicate the side of
5406 * the window has passed beyond the region boundary.
5408 rt = opts & SWM_CW_RIGHT ? MAX_X(win) + BORDER(win) - MAX_X(r) : bw;
5409 lt = opts & SWM_CW_LEFT ? X(r) - X(win) + BORDER(win) : bw;
5410 bm = opts & SWM_CW_BOTTOM ? MAX_Y(win) + BORDER(win) - MAX_Y(r) : bw;
5411 tp = opts & SWM_CW_TOP ? Y(r) - Y(win) + BORDER(win) : bw;
5413 DNPRINTF(SWM_D_MISC, "region_containment: win 0x%x, rt: %d, lt: %d, "
5414 "bm: %d, tp: %d, SOFTBOUNDARY: %s, HARDBOUNDARY: %s\n", win->id, rt,
5415 lt, bm, tp, YESNO(opts & SWM_CW_SOFTBOUNDARY),
5416 YESNO(opts & SWM_CW_HARDBOUNDARY));
5419 * Disable containment if any of the flagged sides went beyond the
5420 * containment boundary, or if containment is disabled.
5422 if (!(opts & SWM_CW_HARDBOUNDARY || opts & SWM_CW_SOFTBOUNDARY) ||
5423 (bw != 0 && ((rt > bw) || (lt > bw) || (bm > bw) || (tp > bw)))) {
5424 /* Make sure window has at least 1 pixel in the region */
5425 g.x += 1 - WIDTH(win);
5426 g.y += 1 - HEIGHT(win);
5427 g.w += 2 * WIDTH(win) - 2;
5428 g.h += 2 * HEIGHT(win) - 2;
5431 constrain_window(win, &g, &opts);
5434 /* Move or resize a window so that flagged side(s) fit into the supplied box. */
5436 constrain_window(struct ws_win *win, struct swm_geometry *b, int *opts)
5438 DNPRINTF(SWM_D_MISC, "constrain_window: window: 0x%x, (x,y) w x h: "
5439 "(%d,%d) %d x %d, box: (x,y) w x h: (%d,%d) %d x %d, rt: %s, "
5440 "lt: %s, bt: %s, tp: %s, allow resize: %s\n", win->id, X(win),
5441 Y(win), WIDTH(win), HEIGHT(win), b->x, b->y, b->w, b->h,
5442 YESNO(*opts & SWM_CW_RIGHT), YESNO(*opts & SWM_CW_LEFT),
5443 YESNO(*opts & SWM_CW_BOTTOM), YESNO(*opts & SWM_CW_TOP),
5444 YESNO(*opts & SWM_CW_RESIZABLE));
5446 if ((*opts & SWM_CW_RIGHT) && MAX_X(win) + BORDER(win) > b->x + b->w) {
5447 if (*opts & SWM_CW_RESIZABLE)
5448 WIDTH(win) = b->x + b->w - X(win) - BORDER(win);
5450 X(win) = b->x + b->w - WIDTH(win) - BORDER(win);
5453 if ((*opts & SWM_CW_LEFT) && X(win) + BORDER(win) < b->x) {
5454 if (*opts & SWM_CW_RESIZABLE)
5455 WIDTH(win) -= b->x - X(win) - BORDER(win);
5457 X(win) = b->x - BORDER(win);
5460 if ((*opts & SWM_CW_BOTTOM) && MAX_Y(win) + BORDER(win) > b->y + b->h) {
5461 if (*opts & SWM_CW_RESIZABLE)
5462 HEIGHT(win) = b->y + b->h - Y(win) - BORDER(win);
5464 Y(win) = b->y + b->h - HEIGHT(win) - BORDER(win);
5467 if ((*opts & SWM_CW_TOP) && Y(win) + BORDER(win) < b->y) {
5468 if (*opts & SWM_CW_RESIZABLE)
5469 HEIGHT(win) -= b->y - Y(win) - BORDER(win);
5471 Y(win) = b->y - BORDER(win);
5474 if (*opts & SWM_CW_RESIZABLE) {
5477 if (HEIGHT(win) < 1)
5483 update_window(struct ws_win *win)
5488 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
5489 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
5490 XCB_CONFIG_WINDOW_BORDER_WIDTH;
5494 wc[3] = HEIGHT(win);
5495 wc[4] = BORDER(win);
5497 DNPRINTF(SWM_D_EVENT, "update_window: window: 0x%x, (x,y) w x h: "
5498 "(%d,%d) %d x %d, bordered: %s\n", win->id, wc[0], wc[1], wc[2],
5499 wc[3], YESNO(win->bordered));
5501 xcb_configure_window(conn, win->id, mask, wc);
5504 #define SWM_RESIZE_STEPS (50)
5507 resize(struct ws_win *win, union arg *args)
5509 xcb_timestamp_t timestamp = 0;
5510 struct swm_region *r = NULL;
5512 struct swm_geometry g;
5513 int top = 0, left = 0, resizing;
5515 xcb_cursor_t cursor;
5516 xcb_query_pointer_reply_t *xpr = NULL;
5517 xcb_generic_event_t *evt;
5518 xcb_motion_notify_event_t *mne;
5524 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
5527 DNPRINTF(SWM_D_EVENT, "resize: window: 0x%x, floating: %s, "
5528 "transient: 0x%x\n", win->id, YESNO(win->floating),
5531 if (!win->transient && !win->floating)
5534 /* reject resizes in max mode for floaters (transient ok) */
5535 if (win->floatmaxed)
5539 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
5546 /* It's possible for win to have been freed during focus_flush(). */
5547 if (validate_win(win)) {
5548 DNPRINTF(SWM_D_EVENT, "resize: invalid win.\n");
5553 case SWM_ARG_ID_WIDTHSHRINK:
5554 WIDTH(win) -= SWM_RESIZE_STEPS;
5557 case SWM_ARG_ID_WIDTHGROW:
5558 WIDTH(win) += SWM_RESIZE_STEPS;
5561 case SWM_ARG_ID_HEIGHTSHRINK:
5562 HEIGHT(win) -= SWM_RESIZE_STEPS;
5565 case SWM_ARG_ID_HEIGHTGROW:
5566 HEIGHT(win) += SWM_RESIZE_STEPS;
5573 region_containment(win, r, SWM_CW_ALLSIDES | SWM_CW_RESIZABLE |
5574 SWM_CW_HARDBOUNDARY);
5576 store_float_geom(win);
5580 region_containment(win, r, SWM_CW_ALLSIDES | SWM_CW_RESIZABLE |
5581 SWM_CW_SOFTBOUNDARY);
5584 /* get cursor offset from window root */
5585 xpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, win->id),
5592 if (xpr->win_x < WIDTH(win) / 2)
5595 if (xpr->win_y < HEIGHT(win) / 2)
5598 if (args->id == SWM_ARG_ID_CENTER)
5599 cursor = cursors[XC_SIZING].cid;
5601 cursor = cursors[left ? XC_TOP_LEFT_CORNER :
5602 XC_TOP_RIGHT_CORNER].cid;
5604 cursor = cursors[left ? XC_BOTTOM_LEFT_CORNER :
5605 XC_BOTTOM_RIGHT_CORNER].cid;
5607 xcb_grab_pointer(conn, 0, win->id, MOUSEMASK,
5608 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, XCB_WINDOW_NONE, cursor,
5613 while (resizing && (evt = xcb_wait_for_event(conn))) {
5614 switch (XCB_EVENT_RESPONSE_TYPE(evt)) {
5615 case XCB_BUTTON_RELEASE:
5616 DNPRINTF(SWM_D_EVENT, "resize: BUTTON_RELEASE\n");
5619 case XCB_MOTION_NOTIFY:
5620 mne = (xcb_motion_notify_event_t *)evt;
5621 /* cursor offset/delta from start of the operation */
5622 dx = mne->root_x - xpr->root_x;
5623 dy = mne->root_y - xpr->root_y;
5629 if (args->id == SWM_ARG_ID_CENTER) {
5630 if (g.h / 2 + dy < 1)
5634 HEIGHT(win) = g.h + 2 * dy;
5642 HEIGHT(win) = g.h + dy;
5649 if (args->id == SWM_ARG_ID_CENTER) {
5650 if (g.w / 2 + dx < 1)
5654 WIDTH(win) = g.w + 2 * dx;
5662 WIDTH(win) = g.w + dx;
5665 /* not free, don't sync more than 120 times / second */
5666 if ((mne->time - timestamp) > (1000 / 120) ) {
5667 timestamp = mne->time;
5668 regionize(win, mne->root_x, mne->root_y);
5669 region_containment(win, r, SWM_CW_ALLSIDES |
5670 SWM_CW_RESIZABLE | SWM_CW_HARDBOUNDARY |
5671 SWM_CW_SOFTBOUNDARY);
5679 /* It's possible for win to have been freed above. */
5680 if (validate_win(win)) {
5681 DNPRINTF(SWM_D_EVENT, "resize: invalid win.\n");
5689 region_containment(win, r, SWM_CW_ALLSIDES | SWM_CW_RESIZABLE |
5690 SWM_CW_HARDBOUNDARY | SWM_CW_SOFTBOUNDARY);
5694 store_float_geom(win);
5696 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
5698 DNPRINTF(SWM_D_EVENT, "resize: done.\n");
5702 resize_step(struct swm_region *r, union arg *args)
5704 struct ws_win *win = NULL;
5706 if (r && r->ws && r->ws->focus)
5715 /* Try to set window region based on supplied coordinates or window center. */
5717 regionize(struct ws_win *win, int x, int y)
5719 struct swm_region *r = NULL;
5721 r = region_under(win->s, x, y);
5723 r = region_under(win->s, X(win) + WIDTH(win) / 2,
5724 Y(win) + HEIGHT(win) / 2);
5726 if (r && r != win->ws->r) {
5727 win_to_ws(win, r->ws->idx, 0);
5732 #define SWM_MOVE_STEPS (50)
5735 move(struct ws_win *win, union arg *args)
5737 xcb_timestamp_t timestamp = 0;
5738 int move_stp = 0, moving;
5739 xcb_query_pointer_reply_t *qpr = NULL;
5740 xcb_generic_event_t *evt;
5741 xcb_motion_notify_event_t *mne;
5746 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
5749 DNPRINTF(SWM_D_EVENT, "move: window: 0x%x, floating: %s, transient: "
5750 "0x%x\n", win->id, YESNO(win->floating), win->transient);
5752 /* in max_stack mode should only move transients */
5753 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !win->transient)
5758 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
5762 /* When a stacked win is moved, float it and restack. */
5763 if (!win->floating && !win->transient) {
5764 store_float_geom(win);
5765 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
5772 /* It's possible for win to have been freed during focus_flush(). */
5773 if (validate_win(win)) {
5774 DNPRINTF(SWM_D_EVENT, "move: invalid win.\n");
5780 case SWM_ARG_ID_MOVELEFT:
5781 X(win) -= (SWM_MOVE_STEPS - border_width);
5784 case SWM_ARG_ID_MOVERIGHT:
5785 X(win) += (SWM_MOVE_STEPS - border_width);
5788 case SWM_ARG_ID_MOVEUP:
5789 Y(win) -= (SWM_MOVE_STEPS - border_width);
5792 case SWM_ARG_ID_MOVEDOWN:
5793 Y(win) += (SWM_MOVE_STEPS - border_width);
5800 regionize(win, -1, -1);
5801 region_containment(win, win->ws->r, SWM_CW_ALLSIDES);
5803 store_float_geom(win);
5807 xcb_grab_pointer(conn, 0, win->id, MOUSEMASK,
5808 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC,
5809 XCB_WINDOW_NONE, cursors[XC_FLEUR].cid, XCB_CURRENT_TIME);
5811 /* get cursor offset from window root */
5812 qpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, win->id),
5815 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
5819 regionize(win, qpr->root_x, qpr->root_y);
5820 region_containment(win, win->ws->r, SWM_CW_ALLSIDES |
5821 SWM_CW_SOFTBOUNDARY);
5825 while (moving && (evt = xcb_wait_for_event(conn))) {
5826 switch (XCB_EVENT_RESPONSE_TYPE(evt)) {
5827 case XCB_BUTTON_RELEASE:
5828 DNPRINTF(SWM_D_EVENT, "move: BUTTON_RELEASE\n");
5831 case XCB_MOTION_NOTIFY:
5832 mne = (xcb_motion_notify_event_t *)evt;
5833 DNPRINTF(SWM_D_EVENT, "motion: root: 0x%x\n", mne->root);
5834 X(win) = mne->root_x - qpr->win_x - border_width;
5835 Y(win) = mne->root_y - qpr->win_y - border_width;
5837 /* not free, don't sync more than 120 times / second */
5838 if ((mne->time - timestamp) > (1000 / 120) ) {
5839 timestamp = mne->time;
5840 regionize(win, mne->root_x, mne->root_y);
5841 region_containment(win, win->ws->r,
5842 SWM_CW_ALLSIDES | SWM_CW_SOFTBOUNDARY);
5850 /* It's possible for win to have been freed above. */
5851 if (validate_win(win)) {
5852 DNPRINTF(SWM_D_EVENT, "move: invalid win.\n");
5860 region_containment(win, win->ws->r, SWM_CW_ALLSIDES |
5861 SWM_CW_SOFTBOUNDARY);
5865 store_float_geom(win);
5868 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
5869 DNPRINTF(SWM_D_EVENT, "move: done.\n");
5873 move_step(struct swm_region *r, union arg *args)
5875 struct ws_win *win = NULL;
5877 if (r && r->ws && r->ws->focus)
5882 if (!win->transient && !win->floating)
5889 /* key definitions */
5891 char name[SWM_FUNCNAME_LEN];
5892 void (*func)(struct swm_region *r, union arg *);
5894 } keyfuncs[KF_INVALID + 1] = {
5895 /* name function argument */
5896 { "bar_toggle", bar_toggle, {.id = SWM_ARG_ID_BAR_TOGGLE} },
5897 { "bar_toggle_ws", bar_toggle, {.id = SWM_ARG_ID_BAR_TOGGLE_WS} },
5898 { "button2", pressbutton, {2} },
5899 { "cycle_layout", cycle_layout, {0} },
5900 { "flip_layout", stack_config, {.id = SWM_ARG_ID_FLIPLAYOUT} },
5901 { "float_toggle", floating_toggle,{0} },
5902 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
5903 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
5904 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
5905 { "focus_urgent", focus, {.id = SWM_ARG_ID_FOCUSURGENT} },
5906 { "height_grow", resize_step, {.id = SWM_ARG_ID_HEIGHTGROW} },
5907 { "height_shrink", resize_step, {.id = SWM_ARG_ID_HEIGHTSHRINK} },
5908 { "iconify", iconify, {0} },
5909 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
5910 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
5911 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
5912 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
5913 { "move_down", move_step, {.id = SWM_ARG_ID_MOVEDOWN} },
5914 { "move_left", move_step, {.id = SWM_ARG_ID_MOVELEFT} },
5915 { "move_right", move_step, {.id = SWM_ARG_ID_MOVERIGHT} },
5916 { "move_up", move_step, {.id = SWM_ARG_ID_MOVEUP} },
5917 { "mvrg_1", send_to_rg, {.id = 0} },
5918 { "mvrg_2", send_to_rg, {.id = 1} },
5919 { "mvrg_3", send_to_rg, {.id = 2} },
5920 { "mvrg_4", send_to_rg, {.id = 3} },
5921 { "mvrg_5", send_to_rg, {.id = 4} },
5922 { "mvrg_6", send_to_rg, {.id = 5} },
5923 { "mvrg_7", send_to_rg, {.id = 6} },
5924 { "mvrg_8", send_to_rg, {.id = 7} },
5925 { "mvrg_9", send_to_rg, {.id = 8} },
5926 { "mvws_1", send_to_ws, {.id = 0} },
5927 { "mvws_2", send_to_ws, {.id = 1} },
5928 { "mvws_3", send_to_ws, {.id = 2} },
5929 { "mvws_4", send_to_ws, {.id = 3} },
5930 { "mvws_5", send_to_ws, {.id = 4} },
5931 { "mvws_6", send_to_ws, {.id = 5} },
5932 { "mvws_7", send_to_ws, {.id = 6} },
5933 { "mvws_8", send_to_ws, {.id = 7} },
5934 { "mvws_9", send_to_ws, {.id = 8} },
5935 { "mvws_10", send_to_ws, {.id = 9} },
5936 { "mvws_11", send_to_ws, {.id = 10} },
5937 { "mvws_12", send_to_ws, {.id = 11} },
5938 { "mvws_13", send_to_ws, {.id = 12} },
5939 { "mvws_14", send_to_ws, {.id = 13} },
5940 { "mvws_15", send_to_ws, {.id = 14} },
5941 { "mvws_16", send_to_ws, {.id = 15} },
5942 { "mvws_17", send_to_ws, {.id = 16} },
5943 { "mvws_18", send_to_ws, {.id = 17} },
5944 { "mvws_19", send_to_ws, {.id = 18} },
5945 { "mvws_20", send_to_ws, {.id = 19} },
5946 { "mvws_21", send_to_ws, {.id = 20} },
5947 { "mvws_22", send_to_ws, {.id = 21} },
5948 { "name_workspace", name_workspace, {0} },
5949 { "quit", quit, {0} },
5950 { "raise_toggle", raise_toggle, {0} },
5951 { "restart", restart, {0} },
5952 { "rg_1", focusrg, {.id = 0} },
5953 { "rg_2", focusrg, {.id = 1} },
5954 { "rg_3", focusrg, {.id = 2} },
5955 { "rg_4", focusrg, {.id = 3} },
5956 { "rg_5", focusrg, {.id = 4} },
5957 { "rg_6", focusrg, {.id = 5} },
5958 { "rg_7", focusrg, {.id = 6} },
5959 { "rg_8", focusrg, {.id = 7} },
5960 { "rg_9", focusrg, {.id = 8} },
5961 { "rg_next", cyclerg, {.id = SWM_ARG_ID_CYCLERG_UP} },
5962 { "rg_prev", cyclerg, {.id = SWM_ARG_ID_CYCLERG_DOWN} },
5963 { "screen_next", cyclerg, {.id = SWM_ARG_ID_CYCLERG_UP} },
5964 { "screen_prev", cyclerg, {.id = SWM_ARG_ID_CYCLERG_DOWN} },
5965 { "search_win", search_win, {0} },
5966 { "search_workspace", search_workspace, {0} },
5967 { "spawn_custom", NULL, {0} },
5968 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
5969 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
5970 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
5971 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
5972 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
5973 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
5974 { "uniconify", uniconify, {0} },
5975 { "version", version, {0} },
5976 { "width_grow", resize_step, {.id = SWM_ARG_ID_WIDTHGROW} },
5977 { "width_shrink", resize_step, {.id = SWM_ARG_ID_WIDTHSHRINK} },
5978 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
5979 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
5980 { "ws_1", switchws, {.id = 0} },
5981 { "ws_2", switchws, {.id = 1} },
5982 { "ws_3", switchws, {.id = 2} },
5983 { "ws_4", switchws, {.id = 3} },
5984 { "ws_5", switchws, {.id = 4} },
5985 { "ws_6", switchws, {.id = 5} },
5986 { "ws_7", switchws, {.id = 6} },
5987 { "ws_8", switchws, {.id = 7} },
5988 { "ws_9", switchws, {.id = 8} },
5989 { "ws_10", switchws, {.id = 9} },
5990 { "ws_11", switchws, {.id = 10} },
5991 { "ws_12", switchws, {.id = 11} },
5992 { "ws_13", switchws, {.id = 12} },
5993 { "ws_14", switchws, {.id = 13} },
5994 { "ws_15", switchws, {.id = 14} },
5995 { "ws_16", switchws, {.id = 15} },
5996 { "ws_17", switchws, {.id = 16} },
5997 { "ws_18", switchws, {.id = 17} },
5998 { "ws_19", switchws, {.id = 18} },
5999 { "ws_20", switchws, {.id = 19} },
6000 { "ws_21", switchws, {.id = 20} },
6001 { "ws_22", switchws, {.id = 21} },
6002 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
6003 { "ws_next_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP_ALL} },
6004 { "ws_next_move", cyclews, {.id = SWM_ARG_ID_CYCLEWS_MOVE_UP} },
6005 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
6006 { "ws_prev_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN_ALL} },
6007 { "ws_prev_move", cyclews, {.id = SWM_ARG_ID_CYCLEWS_MOVE_DOWN} },
6008 { "ws_prior", priorws, {0} },
6009 { "dumpwins", dumpwins, {0} }, /* MUST BE LAST */
6010 { "invalid key func", NULL, {0} },
6014 key_cmp(struct key *kp1, struct key *kp2)
6016 if (kp1->keysym < kp2->keysym)
6018 if (kp1->keysym > kp2->keysym)
6021 if (kp1->mod < kp2->mod)
6023 if (kp1->mod > kp2->mod)
6030 enum { client_click, root_click };
6032 unsigned int action;
6034 unsigned int button;
6035 void (*func)(struct ws_win *, union arg *);
6038 #define MODKEY_SHIFT MODKEY | XCB_MOD_MASK_SHIFT
6039 /* action key mouse button func args */
6040 { client_click, MODKEY, XCB_BUTTON_INDEX_3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
6041 { client_click, MODKEY_SHIFT, XCB_BUTTON_INDEX_3, resize, {.id = SWM_ARG_ID_CENTER} },
6042 { client_click, MODKEY, XCB_BUTTON_INDEX_1, move, {0} },
6047 update_modkey(unsigned int mod)
6053 RB_FOREACH(kp, key_tree, &keys)
6054 if (kp->mod & XCB_MOD_MASK_SHIFT)
6055 kp->mod = mod | XCB_MOD_MASK_SHIFT;
6059 for (i = 0; i < LENGTH(buttons); i++)
6060 if (buttons[i].mask & XCB_MOD_MASK_SHIFT)
6061 buttons[i].mask = mod | XCB_MOD_MASK_SHIFT;
6063 buttons[i].mask = mod;
6067 spawn_expand(struct swm_region *r, union arg *args, const char *spawn_name,
6070 struct spawn_prog *prog = NULL;
6072 char *ap, **real_args;
6074 /* suppress unused warning since var is needed */
6077 DNPRINTF(SWM_D_SPAWN, "spawn_expand: %s\n", spawn_name);
6080 TAILQ_FOREACH(prog, &spawns, entry) {
6081 if (!strcasecmp(spawn_name, prog->name))
6085 warnx("spawn_custom: program %s not found", spawn_name);
6089 /* make room for expanded args */
6090 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
6091 err(1, "spawn_custom: calloc real_args");
6093 /* expand spawn_args into real_args */
6094 for (i = c = 0; i < prog->argc; i++) {
6096 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg: %s\n", ap);
6097 if (!strcasecmp(ap, "$bar_border")) {
6099 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
6101 err(1, "spawn_custom border color");
6102 } else if (!strcasecmp(ap, "$bar_color")) {
6104 strdup(r->s->c[SWM_S_COLOR_BAR].name))
6106 err(1, "spawn_custom bar color");
6107 } else if (!strcasecmp(ap, "$bar_font")) {
6108 if ((real_args[c] = strdup(bar_fonts))
6110 err(1, "spawn_custom bar fonts");
6111 } else if (!strcasecmp(ap, "$bar_font_color")) {
6113 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
6115 err(1, "spawn_custom color font");
6116 } else if (!strcasecmp(ap, "$color_focus")) {
6118 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
6120 err(1, "spawn_custom color focus");
6121 } else if (!strcasecmp(ap, "$color_unfocus")) {
6123 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
6125 err(1, "spawn_custom color unfocus");
6126 } else if (!strcasecmp(ap, "$region_index")) {
6127 if (asprintf(&real_args[c], "%d",
6128 get_region_index(r) + 1) < 1)
6129 err(1, "spawn_custom region index");
6130 } else if (!strcasecmp(ap, "$workspace_index")) {
6131 if (asprintf(&real_args[c], "%d", r->ws->idx + 1) < 1)
6132 err(1, "spawn_custom workspace index");
6133 } else if (!strcasecmp(ap, "$dmenu_bottom")) {
6136 if ((real_args[c] = strdup("-b")) == NULL)
6137 err(1, "spawn_custom workspace index");
6139 /* no match --> copy as is */
6140 if ((real_args[c] = strdup(ap)) == NULL)
6141 err(1, "spawn_custom strdup(ap)");
6143 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg: %s\n",
6149 DNPRINTF(SWM_D_SPAWN, "spawn_custom: result: ");
6150 for (i = 0; i < c; ++i)
6151 DPRINTF("\"%s\" ", real_args[i]);
6154 *ret_args = real_args;
6159 spawn_custom(struct swm_region *r, union arg *args, const char *spawn_name)
6165 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
6169 spawn(r->ws->idx, &a, 1);
6171 for (i = 0; i < spawn_argc; i++)
6177 spawn_select(struct swm_region *r, union arg *args, const char *spawn_name,
6184 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
6188 if (pipe(select_list_pipe) == -1)
6189 err(1, "pipe error");
6190 if (pipe(select_resp_pipe) == -1)
6191 err(1, "pipe error");
6193 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
6194 err(1, "could not disable SIGPIPE");
6195 switch (*pid = fork()) {
6197 err(1, "cannot fork");
6200 if (dup2(select_list_pipe[0], STDIN_FILENO) == -1)
6202 if (dup2(select_resp_pipe[1], STDOUT_FILENO) == -1)
6204 close(select_list_pipe[1]);
6205 close(select_resp_pipe[0]);
6206 spawn(r->ws->idx, &a, 0);
6208 default: /* parent */
6209 close(select_list_pipe[0]);
6210 close(select_resp_pipe[1]);
6214 for (i = 0; i < spawn_argc; i++)
6220 spawn_insert(const char *name, const char *args, int flags)
6222 char *arg, *cp, *ptr;
6223 struct spawn_prog *sp;
6225 DNPRINTF(SWM_D_SPAWN, "spawn_insert: %s\n", name);
6227 if ((sp = calloc(1, sizeof *sp)) == NULL)
6228 err(1, "spawn_insert: calloc");
6229 if ((sp->name = strdup(name)) == NULL)
6230 err(1, "spawn_insert: strdup");
6232 /* convert the arguments to an argument list */
6233 if ((ptr = cp = strdup(args)) == NULL)
6234 err(1, "spawn_insert: strdup");
6235 while ((arg = strsep(&ptr, " \t")) != NULL) {
6236 /* empty field; skip it */
6241 if ((sp->argv = realloc(sp->argv, sp->argc *
6242 sizeof *sp->argv)) == NULL)
6243 err(1, "spawn_insert: realloc");
6244 if ((sp->argv[sp->argc - 1] = strdup(arg)) == NULL)
6245 err(1, "spawn_insert: strdup");
6251 TAILQ_INSERT_TAIL(&spawns, sp, entry);
6252 DNPRINTF(SWM_D_SPAWN, "spawn_insert: leave\n");
6256 spawn_remove(struct spawn_prog *sp)
6260 DNPRINTF(SWM_D_SPAWN, "spawn_remove: %s\n", sp->name);
6262 TAILQ_REMOVE(&spawns, sp, entry);
6263 for (i = 0; i < sp->argc; i++)
6269 DNPRINTF(SWM_D_SPAWN, "spawn_remove: leave\n");
6273 spawn_find(const char *name)
6275 struct spawn_prog *sp;
6277 TAILQ_FOREACH(sp, &spawns, entry)
6278 if (!strcasecmp(sp->name, name))
6285 setspawn(const char *name, const char *args, int flags)
6287 struct spawn_prog *sp;
6289 DNPRINTF(SWM_D_SPAWN, "setspawn: %s\n", name);
6294 /* Remove any old spawn under the same name. */
6295 if ((sp = spawn_find(name)) != NULL)
6299 spawn_insert(name, args, flags);
6301 warnx("error: setspawn: cannot find program: %s", name);
6303 DNPRINTF(SWM_D_SPAWN, "setspawn: leave\n");
6307 setconfspawn(char *selector, char *value, int flags)
6311 args = expand_tilde(value);
6313 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, args);
6315 setspawn(selector, args, flags);
6318 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done.\n");
6323 validate_spawns(void)
6325 struct spawn_prog *sp;
6326 char which[PATH_MAX];
6331 RB_FOREACH(kp, key_tree, &keys) {
6332 if (kp->funcid != KF_SPAWN_CUSTOM)
6336 sp = spawn_find(kp->spawn_name);
6337 if (sp == NULL || sp->flags & SWM_SPAWN_OPTIONAL)
6340 /* verify we have the goods */
6341 snprintf(which, sizeof which, "which %s", sp->argv[0]);
6342 DNPRINTF(SWM_D_CONF, "validate_spawns: which %s\n",
6344 for (i = strlen("which "); i < strlen(which); i++)
6345 if (which[i] == ' ') {
6349 if (system(which) != 0)
6350 add_startup_exception("could not find %s",
6351 &which[strlen("which ")]);
6358 setconfspawn("lock", "xlock", 0);
6360 setconfspawn("term", "xterm", 0);
6361 setconfspawn("spawn_term", "xterm", 0);
6363 setconfspawn("menu", "dmenu_run"
6367 " -nf $bar_font_color"
6369 " -sf $bar_color", 0);
6371 setconfspawn("search", "dmenu"
6376 " -nf $bar_font_color"
6378 " -sf $bar_color", 0);
6380 setconfspawn("name_workspace", "dmenu"
6385 " -nf $bar_font_color"
6387 " -sf $bar_color", 0);
6389 /* These are not verified for existence, even with a binding set. */
6390 setconfspawn("screenshot_all", "screenshot.sh full", SWM_SPAWN_OPTIONAL);
6391 setconfspawn("screenshot_wind", "screenshot.sh window", SWM_SPAWN_OPTIONAL);
6392 setconfspawn("initscr", "initscreen.sh", SWM_SPAWN_OPTIONAL);
6396 #define SWM_MODNAME_SIZE 32
6397 #define SWM_KEY_WS "\n+ \t"
6399 parsekeys(char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
6403 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
6404 if (mod == NULL || ks == NULL) {
6405 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
6408 if (keystr == NULL || strlen(keystr) == 0) {
6409 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
6413 *ks = XCB_NO_SYMBOL;
6415 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
6416 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
6418 cp += (long)strspn(cp, SWM_KEY_WS);
6419 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
6421 else if (!strncasecmp(name, "Mod1", SWM_MODNAME_SIZE))
6422 *mod |= XCB_MOD_MASK_1;
6423 else if (!strncasecmp(name, "Mod2", SWM_MODNAME_SIZE))
6424 *mod += XCB_MOD_MASK_2;
6425 else if (!strncmp(name, "Mod3", SWM_MODNAME_SIZE))
6426 *mod |= XCB_MOD_MASK_3;
6427 else if (!strncmp(name, "Mod4", SWM_MODNAME_SIZE))
6428 *mod |= XCB_MOD_MASK_4;
6429 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
6430 *mod |= XCB_MOD_MASK_SHIFT;
6431 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
6432 *mod |= XCB_MOD_MASK_CONTROL;
6434 *ks = XStringToKeysym(name);
6435 XConvertCase(*ks, ks, &uks);
6436 if (ks == XCB_NO_SYMBOL) {
6438 "parsekeys: invalid key %s\n",
6444 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
6449 strdupsafe(const char *str)
6454 return (strdup(str));
6458 key_insert(unsigned int mod, KeySym ks, enum keyfuncid kfid,
6459 const char *spawn_name)
6463 DNPRINTF(SWM_D_KEY, "key_insert: enter %s [%s]\n",
6464 keyfuncs[kfid].name, spawn_name);
6466 if ((kp = malloc(sizeof *kp)) == NULL)
6467 err(1, "key_insert: malloc");
6472 kp->spawn_name = strdupsafe(spawn_name);
6473 RB_INSERT(key_tree, &keys, kp);
6475 DNPRINTF(SWM_D_KEY, "key_insert: leave\n");
6479 key_lookup(unsigned int mod, KeySym ks)
6486 return (RB_FIND(key_tree, &keys, &kp));
6490 key_remove(struct key *kp)
6492 DNPRINTF(SWM_D_KEY, "key_remove: %s\n", keyfuncs[kp->funcid].name);
6494 RB_REMOVE(key_tree, &keys, kp);
6495 free(kp->spawn_name);
6498 DNPRINTF(SWM_D_KEY, "key_remove: leave\n");
6502 key_replace(struct key *kp, unsigned int mod, KeySym ks, enum keyfuncid kfid,
6503 const char *spawn_name)
6505 DNPRINTF(SWM_D_KEY, "key_replace: %s [%s]\n", keyfuncs[kp->funcid].name,
6509 key_insert(mod, ks, kfid, spawn_name);
6511 DNPRINTF(SWM_D_KEY, "key_replace: leave\n");
6515 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid,
6516 const char *spawn_name)
6520 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
6521 keyfuncs[kfid].name, spawn_name);
6523 if ((kp = key_lookup(mod, ks)) != NULL) {
6524 if (kfid == KF_INVALID)
6527 key_replace(kp, mod, ks, kfid, spawn_name);
6528 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
6531 if (kfid == KF_INVALID) {
6532 warnx("bind: Key combination already unbound.");
6533 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
6537 key_insert(mod, ks, kfid, spawn_name);
6538 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
6542 setconfbinding(char *selector, char *value, int flags)
6544 enum keyfuncid kfid;
6547 struct spawn_prog *sp;
6549 /* suppress unused warning since var is needed */
6552 DNPRINTF(SWM_D_KEY, "setconfbinding: enter\n");
6553 if (selector == NULL) {
6554 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
6555 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
6557 setkeybinding(mod, ks, kfid, NULL);
6562 /* search by key function name */
6563 for (kfid = 0; kfid < KF_INVALID; (kfid)++) {
6564 if (strncasecmp(selector, keyfuncs[kfid].name,
6565 SWM_FUNCNAME_LEN) == 0) {
6566 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match "
6567 "keyfunc\n", selector);
6568 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
6569 setkeybinding(mod, ks, kfid, NULL);
6575 /* search by custom spawn name */
6576 if ((sp = spawn_find(selector)) != NULL) {
6577 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match "
6578 "spawn\n", selector);
6579 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
6580 setkeybinding(mod, ks, KF_SPAWN_CUSTOM,
6586 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
6593 #define MODKEY_SHIFT MODKEY | XCB_MOD_MASK_SHIFT
6594 setkeybinding(MODKEY, XK_b, KF_BAR_TOGGLE, NULL);
6595 setkeybinding(MODKEY_SHIFT, XK_b, KF_BAR_TOGGLE_WS,NULL);
6596 setkeybinding(MODKEY, XK_v, KF_BUTTON2, NULL);
6597 setkeybinding(MODKEY, XK_space, KF_CYCLE_LAYOUT,NULL);
6598 setkeybinding(MODKEY_SHIFT, XK_backslash, KF_FLIP_LAYOUT, NULL);
6599 setkeybinding(MODKEY, XK_t, KF_FLOAT_TOGGLE,NULL);
6600 setkeybinding(MODKEY, XK_m, KF_FOCUS_MAIN, NULL);
6601 setkeybinding(MODKEY, XK_j, KF_FOCUS_NEXT, NULL);
6602 setkeybinding(MODKEY, XK_Tab, KF_FOCUS_NEXT, NULL);
6603 setkeybinding(MODKEY, XK_k, KF_FOCUS_PREV, NULL);
6604 setkeybinding(MODKEY_SHIFT, XK_Tab, KF_FOCUS_PREV, NULL);
6605 setkeybinding(MODKEY, XK_u, KF_FOCUS_URGENT,NULL);
6606 setkeybinding(MODKEY_SHIFT, XK_equal, KF_HEIGHT_GROW,NULL);
6607 setkeybinding(MODKEY_SHIFT, XK_minus, KF_HEIGHT_SHRINK,NULL);
6608 setkeybinding(MODKEY, XK_w, KF_ICONIFY, NULL);
6609 setkeybinding(MODKEY, XK_h, KF_MASTER_SHRINK, NULL);
6610 setkeybinding(MODKEY, XK_l, KF_MASTER_GROW, NULL);
6611 setkeybinding(MODKEY, XK_comma, KF_MASTER_ADD, NULL);
6612 setkeybinding(MODKEY, XK_period, KF_MASTER_DEL, NULL);
6613 setkeybinding(MODKEY_SHIFT, XK_bracketright,KF_MOVE_DOWN,NULL);
6614 setkeybinding(MODKEY, XK_bracketleft, KF_MOVE_LEFT,NULL);
6615 setkeybinding(MODKEY, XK_bracketright,KF_MOVE_RIGHT,NULL);
6616 setkeybinding(MODKEY_SHIFT, XK_bracketleft, KF_MOVE_UP, NULL);
6617 setkeybinding(MODKEY_SHIFT, XK_KP_End, KF_MVRG_1, NULL);
6618 setkeybinding(MODKEY_SHIFT, XK_KP_Down, KF_MVRG_2, NULL);
6619 setkeybinding(MODKEY_SHIFT, XK_KP_Next, KF_MVRG_3, NULL);
6620 setkeybinding(MODKEY_SHIFT, XK_KP_Left, KF_MVRG_4, NULL);
6621 setkeybinding(MODKEY_SHIFT, XK_KP_Begin, KF_MVRG_5, NULL);
6622 setkeybinding(MODKEY_SHIFT, XK_KP_Right, KF_MVRG_6, NULL);
6623 setkeybinding(MODKEY_SHIFT, XK_KP_Home, KF_MVRG_7, NULL);
6624 setkeybinding(MODKEY_SHIFT, XK_KP_Up, KF_MVRG_8, NULL);
6625 setkeybinding(MODKEY_SHIFT, XK_KP_Prior, KF_MVRG_9, NULL);
6626 setkeybinding(MODKEY_SHIFT, XK_1, KF_MVWS_1, NULL);
6627 setkeybinding(MODKEY_SHIFT, XK_2, KF_MVWS_2, NULL);
6628 setkeybinding(MODKEY_SHIFT, XK_3, KF_MVWS_3, NULL);
6629 setkeybinding(MODKEY_SHIFT, XK_4, KF_MVWS_4, NULL);
6630 setkeybinding(MODKEY_SHIFT, XK_5, KF_MVWS_5, NULL);
6631 setkeybinding(MODKEY_SHIFT, XK_6, KF_MVWS_6, NULL);
6632 setkeybinding(MODKEY_SHIFT, XK_7, KF_MVWS_7, NULL);
6633 setkeybinding(MODKEY_SHIFT, XK_8, KF_MVWS_8, NULL);
6634 setkeybinding(MODKEY_SHIFT, XK_9, KF_MVWS_9, NULL);
6635 setkeybinding(MODKEY_SHIFT, XK_0, KF_MVWS_10, NULL);
6636 setkeybinding(MODKEY_SHIFT, XK_F1, KF_MVWS_11, NULL);
6637 setkeybinding(MODKEY_SHIFT, XK_F2, KF_MVWS_12, NULL);
6638 setkeybinding(MODKEY_SHIFT, XK_F3, KF_MVWS_13, NULL);
6639 setkeybinding(MODKEY_SHIFT, XK_F4, KF_MVWS_14, NULL);
6640 setkeybinding(MODKEY_SHIFT, XK_F5, KF_MVWS_15, NULL);
6641 setkeybinding(MODKEY_SHIFT, XK_F6, KF_MVWS_16, NULL);
6642 setkeybinding(MODKEY_SHIFT, XK_F7, KF_MVWS_17, NULL);
6643 setkeybinding(MODKEY_SHIFT, XK_F8, KF_MVWS_18, NULL);
6644 setkeybinding(MODKEY_SHIFT, XK_F9, KF_MVWS_19, NULL);
6645 setkeybinding(MODKEY_SHIFT, XK_F10, KF_MVWS_20, NULL);
6646 setkeybinding(MODKEY_SHIFT, XK_F11, KF_MVWS_21, NULL);
6647 setkeybinding(MODKEY_SHIFT, XK_F12, KF_MVWS_22, NULL);
6648 setkeybinding(MODKEY_SHIFT, XK_slash, KF_NAME_WORKSPACE,NULL);
6649 setkeybinding(MODKEY_SHIFT, XK_q, KF_QUIT, NULL);
6650 setkeybinding(MODKEY_SHIFT, XK_r, KF_RAISE_TOGGLE,NULL);
6651 setkeybinding(MODKEY, XK_q, KF_RESTART, NULL);
6652 setkeybinding(MODKEY, XK_KP_End, KF_RG_1, NULL);
6653 setkeybinding(MODKEY, XK_KP_Down, KF_RG_2, NULL);
6654 setkeybinding(MODKEY, XK_KP_Next, KF_RG_3, NULL);
6655 setkeybinding(MODKEY, XK_KP_Left, KF_RG_4, NULL);
6656 setkeybinding(MODKEY, XK_KP_Begin, KF_RG_5, NULL);
6657 setkeybinding(MODKEY, XK_KP_Right, KF_RG_6, NULL);
6658 setkeybinding(MODKEY, XK_KP_Home, KF_RG_7, NULL);
6659 setkeybinding(MODKEY, XK_KP_Up, KF_RG_8, NULL);
6660 setkeybinding(MODKEY, XK_KP_Prior, KF_RG_9, NULL);
6661 setkeybinding(MODKEY_SHIFT, XK_Right, KF_RG_NEXT, NULL);
6662 setkeybinding(MODKEY_SHIFT, XK_Left, KF_RG_PREV, NULL);
6663 setkeybinding(MODKEY, XK_f, KF_SEARCH_WIN, NULL);
6664 setkeybinding(MODKEY, XK_slash, KF_SEARCH_WORKSPACE,NULL);
6665 setkeybinding(MODKEY_SHIFT, XK_i, KF_SPAWN_CUSTOM,"initscr");
6666 setkeybinding(MODKEY_SHIFT, XK_Delete, KF_SPAWN_CUSTOM,"lock");
6667 setkeybinding(MODKEY, XK_p, KF_SPAWN_CUSTOM,"menu");
6668 setkeybinding(MODKEY, XK_s, KF_SPAWN_CUSTOM,"screenshot_all");
6669 setkeybinding(MODKEY_SHIFT, XK_s, KF_SPAWN_CUSTOM,"screenshot_wind");
6670 setkeybinding(MODKEY_SHIFT, XK_Return, KF_SPAWN_CUSTOM,"term");
6671 setkeybinding(MODKEY_SHIFT, XK_comma, KF_STACK_INC, NULL);
6672 setkeybinding(MODKEY_SHIFT, XK_period, KF_STACK_DEC, NULL);
6673 setkeybinding(MODKEY_SHIFT, XK_space, KF_STACK_RESET, NULL);
6674 setkeybinding(MODKEY, XK_Return, KF_SWAP_MAIN, NULL);
6675 setkeybinding(MODKEY_SHIFT, XK_j, KF_SWAP_NEXT, NULL);
6676 setkeybinding(MODKEY_SHIFT, XK_k, KF_SWAP_PREV, NULL);
6677 setkeybinding(MODKEY_SHIFT, XK_w, KF_UNICONIFY, NULL);
6678 setkeybinding(MODKEY_SHIFT, XK_v, KF_VERSION, NULL);
6679 setkeybinding(MODKEY, XK_equal, KF_WIDTH_GROW, NULL);
6680 setkeybinding(MODKEY, XK_minus, KF_WIDTH_SHRINK,NULL);
6681 setkeybinding(MODKEY, XK_x, KF_WIND_DEL, NULL);
6682 setkeybinding(MODKEY_SHIFT, XK_x, KF_WIND_KILL, NULL);
6683 setkeybinding(MODKEY, XK_1, KF_WS_1, NULL);
6684 setkeybinding(MODKEY, XK_2, KF_WS_2, NULL);
6685 setkeybinding(MODKEY, XK_3, KF_WS_3, NULL);
6686 setkeybinding(MODKEY, XK_4, KF_WS_4, NULL);
6687 setkeybinding(MODKEY, XK_5, KF_WS_5, NULL);
6688 setkeybinding(MODKEY, XK_6, KF_WS_6, NULL);
6689 setkeybinding(MODKEY, XK_7, KF_WS_7, NULL);
6690 setkeybinding(MODKEY, XK_8, KF_WS_8, NULL);
6691 setkeybinding(MODKEY, XK_9, KF_WS_9, NULL);
6692 setkeybinding(MODKEY, XK_0, KF_WS_10, NULL);
6693 setkeybinding(MODKEY, XK_F1, KF_WS_11, NULL);
6694 setkeybinding(MODKEY, XK_F2, KF_WS_12, NULL);
6695 setkeybinding(MODKEY, XK_F3, KF_WS_13, NULL);
6696 setkeybinding(MODKEY, XK_F4, KF_WS_14, NULL);
6697 setkeybinding(MODKEY, XK_F5, KF_WS_15, NULL);
6698 setkeybinding(MODKEY, XK_F6, KF_WS_16, NULL);
6699 setkeybinding(MODKEY, XK_F7, KF_WS_17, NULL);
6700 setkeybinding(MODKEY, XK_F8, KF_WS_18, NULL);
6701 setkeybinding(MODKEY, XK_F9, KF_WS_19, NULL);
6702 setkeybinding(MODKEY, XK_F10, KF_WS_20, NULL);
6703 setkeybinding(MODKEY, XK_F11, KF_WS_21, NULL);
6704 setkeybinding(MODKEY, XK_F12, KF_WS_22, NULL);
6705 setkeybinding(MODKEY, XK_Right, KF_WS_NEXT, NULL);
6706 setkeybinding(MODKEY, XK_Left, KF_WS_PREV, NULL);
6707 setkeybinding(MODKEY, XK_Up, KF_WS_NEXT_ALL, NULL);
6708 setkeybinding(MODKEY, XK_Down, KF_WS_PREV_ALL, NULL);
6709 setkeybinding(MODKEY_SHIFT, XK_Up, KF_WS_NEXT_MOVE,NULL);
6710 setkeybinding(MODKEY_SHIFT, XK_Down, KF_WS_PREV_MOVE,NULL);
6711 setkeybinding(MODKEY, XK_a, KF_WS_PRIOR, NULL);
6713 setkeybinding(MODKEY_SHIFT, XK_d, KF_DUMPWINS, NULL);
6723 while (RB_EMPTY(&keys) == 0) {
6724 kp = RB_ROOT(&keys);
6730 setkeymapping(char *selector, char *value, int flags)
6732 char *keymapping_file;
6734 /* suppress unused warnings since vars are needed */
6738 DNPRINTF(SWM_D_KEY, "setkeymapping: enter\n");
6740 keymapping_file = expand_tilde(value);
6743 /* load new key bindings; if it fails, revert to default bindings */
6744 if (conf_load(keymapping_file, SWM_CONF_KEYMAPPING)) {
6749 free(keymapping_file);
6751 DNPRINTF(SWM_D_KEY, "setkeymapping: leave\n");
6756 updatenumlockmask(void)
6759 xcb_get_modifier_mapping_reply_t *modmap_r;
6760 xcb_keycode_t *modmap, kc, *keycode;
6764 modmap_r = xcb_get_modifier_mapping_reply(conn,
6765 xcb_get_modifier_mapping(conn),
6768 modmap = xcb_get_modifier_mapping_keycodes(modmap_r);
6769 for (i = 0; i < 8; i++) {
6770 for (j = 0; j < modmap_r->keycodes_per_modifier; j++) {
6771 kc = modmap[i * modmap_r->keycodes_per_modifier
6773 keycode = xcb_key_symbols_get_keycode(syms,
6776 numlockmask = (1 << i);
6782 DNPRINTF(SWM_D_MISC, "updatenumlockmask: %d\n", numlockmask);
6789 int num_screens, k, j;
6790 unsigned int modifiers[4];
6791 xcb_keycode_t *code;
6793 DNPRINTF(SWM_D_MISC, "grabkeys\n");
6794 updatenumlockmask();
6797 modifiers[1] = numlockmask;
6798 modifiers[2] = XCB_MOD_MASK_LOCK;
6799 modifiers[3] = numlockmask | XCB_MOD_MASK_LOCK;
6801 num_screens = get_screen_count();
6802 for (k = 0; k < num_screens; k++) {
6803 if (TAILQ_EMPTY(&screens[k].rl))
6805 xcb_ungrab_key(conn, XCB_GRAB_ANY, screens[k].root,
6807 RB_FOREACH(kp, key_tree, &keys) {
6808 /* Skip unused ws binds. */
6809 if ((int)kp->funcid > KF_WS_1 + workspace_limit - 1 &&
6810 kp->funcid <= KF_WS_22)
6813 /* Skip unused mvws binds. */
6814 if ((int)kp->funcid > KF_MVWS_1 + workspace_limit - 1 &&
6815 kp->funcid <= KF_MVWS_22)
6818 if ((code = xcb_key_symbols_get_keycode(syms,
6820 for (j = 0; j < LENGTH(modifiers); j++)
6821 xcb_grab_key(conn, 1,
6823 kp->mod | modifiers[j],
6824 *code, XCB_GRAB_MODE_SYNC,
6825 XCB_GRAB_MODE_SYNC);
6833 grabbuttons(struct ws_win *win)
6835 unsigned int modifiers[4];
6838 DNPRINTF(SWM_D_MOUSE, "grabbuttons: win 0x%x\n", win->id);
6839 updatenumlockmask();
6842 modifiers[1] = numlockmask;
6843 modifiers[2] = XCB_MOD_MASK_LOCK;
6844 modifiers[3] = numlockmask | XCB_MOD_MASK_LOCK;
6846 for (i = 0; i < LENGTH(buttons); i++)
6847 if (buttons[i].action == client_click)
6848 for (j = 0; j < LENGTH(modifiers); ++j)
6849 xcb_grab_button(conn, 0, win->id, BUTTONMASK,
6850 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC,
6851 XCB_WINDOW_NONE, XCB_CURSOR_NONE,
6852 buttons[i].button, buttons[i].mask |
6856 const char *quirkname[] = {
6857 "NONE", /* config string for "no value" */
6865 "FOCUSONMAP_SINGLE",
6868 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
6869 #define SWM_Q_WS "\n|+ \t"
6871 parsequirks(char *qstr, unsigned long *quirk)
6881 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
6883 cp += (long)strspn(cp, SWM_Q_WS);
6884 for (i = 0; i < LENGTH(quirkname); i++) {
6885 if (!strncasecmp(name, quirkname[i], SWM_QUIRK_LEN)) {
6886 DNPRINTF(SWM_D_QUIRK,
6887 "parsequirks: %s\n", name);
6892 *quirk |= 1 << (i-1);
6896 if (i >= LENGTH(quirkname)) {
6897 DNPRINTF(SWM_D_QUIRK,
6898 "parsequirks: invalid quirk [%s]\n", name);
6906 quirk_insert(const char *class, const char *name, unsigned long quirk)
6910 DNPRINTF(SWM_D_QUIRK, "quirk_insert: %s:%s [%lu]\n", class, name,
6913 if ((qp = malloc(sizeof *qp)) == NULL)
6914 err(1, "quirk_insert: malloc");
6915 if ((qp->class = strdup(class)) == NULL)
6916 err(1, "quirk_insert: strdup");
6917 if ((qp->name = strdup(name)) == NULL)
6918 err(1, "quirk_insert: strdup");
6921 TAILQ_INSERT_TAIL(&quirks, qp, entry);
6923 DNPRINTF(SWM_D_QUIRK, "quirk_insert: leave\n");
6927 quirk_remove(struct quirk *qp)
6929 DNPRINTF(SWM_D_QUIRK, "quirk_remove: %s:%s [%lu]\n", qp->class,
6930 qp->name, qp->quirk);
6932 TAILQ_REMOVE(&quirks, qp, entry);
6937 DNPRINTF(SWM_D_QUIRK, "quirk_remove: leave\n");
6941 quirk_replace(struct quirk *qp, const char *class, const char *name,
6942 unsigned long quirk)
6944 DNPRINTF(SWM_D_QUIRK, "quirk_replace: %s:%s [%lu]\n", qp->class,
6945 qp->name, qp->quirk);
6948 quirk_insert(class, name, quirk);
6950 DNPRINTF(SWM_D_QUIRK, "quirk_replace: leave\n");
6954 setquirk(const char *class, const char *name, unsigned long quirk)
6958 DNPRINTF(SWM_D_QUIRK, "setquirk: enter %s:%s [%lu]\n", class, name,
6961 /* Remove/replace existing quirk. */
6962 TAILQ_FOREACH(qp, &quirks, entry) {
6963 if (!strcmp(qp->class, class) && !strcmp(qp->name, name)) {
6967 quirk_replace(qp, class, name, quirk);
6968 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
6973 /* Only insert if quirk is not NONE. */
6975 quirk_insert(class, name, quirk);
6977 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
6981 setconfquirk(char *selector, char *value, int flags)
6983 char *cp, *class, *name;
6987 /* suppress unused warning since var is needed */
6990 if (selector == NULL)
6992 if ((cp = strchr(selector, ':')) == NULL)
6997 if ((retval = parsequirks(value, &qrks)) == 0)
6998 setquirk(class, name, qrks);
7005 setquirk("MPlayer", "xv", SWM_Q_FLOAT | SWM_Q_FULLSCREEN | SWM_Q_FOCUSPREV);
7006 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT);
7007 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ);
7008 setquirk("Firefox", "Dialog", SWM_Q_FLOAT);
7009 setquirk("Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
7010 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ);
7011 setquirk("xine", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
7012 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
7013 setquirk("xine", "xine Panel", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
7014 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
7015 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
7016 setquirk("pcb", "pcb", SWM_Q_FLOAT);
7017 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
7020 /* conf file stuff */
7021 #define SWM_CONF_FILE "spectrwm.conf"
7022 #define SWM_CONF_FILE_OLD "scrotwm.conf"
7026 SWM_S_BAR_AT_BOTTOM,
7027 SWM_S_BAR_BORDER_WIDTH,
7030 SWM_S_BAR_ENABLED_WS,
7035 SWM_S_BOUNDARY_WIDTH,
7036 SWM_S_CLOCK_ENABLED,
7039 SWM_S_CYCLE_VISIBLE,
7041 SWM_S_DISABLE_BORDER,
7043 SWM_S_FOCUS_CLOSE_WRAP,
7044 SWM_S_FOCUS_DEFAULT,
7046 SWM_S_REGION_PADDING,
7051 SWM_S_STACK_ENABLED,
7054 SWM_S_TITLE_CLASS_ENABLED,
7055 SWM_S_TITLE_NAME_ENABLED,
7056 SWM_S_URGENT_ENABLED,
7057 SWM_S_VERBOSE_LAYOUT,
7058 SWM_S_WINDOW_NAME_ENABLED,
7059 SWM_S_WORKSPACE_LIMIT
7063 setconfvalue(char *selector, char *value, int flags)
7065 struct workspace *ws;
7066 int i, ws_id, num_screens;
7069 /* suppress unused warning since var is needed */
7073 case SWM_S_BAR_ACTION:
7075 if ((bar_argv[0] = expand_tilde(value)) == NULL)
7076 err(1, "setconfvalue: bar_action");
7078 case SWM_S_BAR_AT_BOTTOM:
7079 bar_at_bottom = atoi(value);
7081 case SWM_S_BAR_BORDER_WIDTH:
7082 bar_border_width = atoi(value);
7083 if (bar_border_width < 0)
7084 bar_border_width = 0;
7086 case SWM_S_BAR_DELAY:
7087 /* No longer needed; leave to not break old conf files. */
7089 case SWM_S_BAR_ENABLED:
7090 bar_enabled = atoi(value);
7092 case SWM_S_BAR_ENABLED_WS:
7093 ws_id = atoi(selector) - 1;
7094 if (ws_id < 0 || ws_id >= workspace_limit)
7095 errx(1, "setconfvalue: bar_enabled_ws: invalid "
7096 "workspace %d.", ws_id + 1);
7098 num_screens = get_screen_count();
7099 for (i = 0; i < num_screens; i++) {
7100 ws = (struct workspace *)&screens[i].ws;
7101 ws[ws_id].bar_enabled = atoi(value);
7104 case SWM_S_BAR_FONT:
7106 if (asprintf(&bar_fonts, "%s,%s", value, bar_fonts) == -1)
7107 err(1, "setconfvalue: asprintf: failed to allocate "
7108 "memory for bar_fonts.");
7111 /* If already in xft mode, then we are done. */
7112 if (!bar_font_legacy)
7115 /* If there are any non-XLFD entries, switch to Xft mode. */
7116 while ((b = strsep(&value, ",")) != NULL) {
7120 bar_font_legacy = 0;
7125 case SWM_S_BAR_FORMAT:
7127 if ((bar_format = strdup(value)) == NULL)
7128 err(1, "setconfvalue: bar_format");
7130 case SWM_S_BAR_JUSTIFY:
7131 if (!strcmp(value, "left"))
7132 bar_justify = SWM_BAR_JUSTIFY_LEFT;
7133 else if (!strcmp(value, "center"))
7134 bar_justify = SWM_BAR_JUSTIFY_CENTER;
7135 else if (!strcmp(value, "right"))
7136 bar_justify = SWM_BAR_JUSTIFY_RIGHT;
7138 errx(1, "invalid bar_justify");
7140 case SWM_S_BORDER_WIDTH:
7141 border_width = atoi(value);
7142 if (border_width < 0)
7145 case SWM_S_BOUNDARY_WIDTH:
7146 boundary_width = atoi(value);
7147 if (boundary_width < 0)
7150 case SWM_S_CLOCK_ENABLED:
7151 clock_enabled = atoi(value);
7153 case SWM_S_CLOCK_FORMAT:
7154 #ifndef SWM_DENY_CLOCK_FORMAT
7156 if ((clock_format = strdup(value)) == NULL)
7157 err(1, "setconfvalue: clock_format");
7160 case SWM_S_CYCLE_EMPTY:
7161 cycle_empty = atoi(value);
7163 case SWM_S_CYCLE_VISIBLE:
7164 cycle_visible = atoi(value);
7166 case SWM_S_DIALOG_RATIO:
7167 dialog_ratio = atof(value);
7168 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
7171 case SWM_S_DISABLE_BORDER:
7172 disable_border = atoi(value);
7174 case SWM_S_FOCUS_CLOSE:
7175 if (!strcmp(value, "first"))
7176 focus_close = SWM_STACK_BOTTOM;
7177 else if (!strcmp(value, "last"))
7178 focus_close = SWM_STACK_TOP;
7179 else if (!strcmp(value, "next"))
7180 focus_close = SWM_STACK_ABOVE;
7181 else if (!strcmp(value, "previous"))
7182 focus_close = SWM_STACK_BELOW;
7184 errx(1, "focus_close");
7186 case SWM_S_FOCUS_CLOSE_WRAP:
7187 focus_close_wrap = atoi(value);
7189 case SWM_S_FOCUS_DEFAULT:
7190 if (!strcmp(value, "last"))
7191 focus_default = SWM_STACK_TOP;
7192 else if (!strcmp(value, "first"))
7193 focus_default = SWM_STACK_BOTTOM;
7195 errx(1, "focus_default");
7197 case SWM_S_FOCUS_MODE:
7198 if (!strcmp(value, "default"))
7199 focus_mode = SWM_FOCUS_DEFAULT;
7200 else if (!strcmp(value, "follow") ||
7201 !strcmp(value, "follow_cursor"))
7202 focus_mode = SWM_FOCUS_FOLLOW;
7203 else if (!strcmp(value, "manual"))
7204 focus_mode = SWM_FOCUS_MANUAL;
7206 errx(1, "focus_mode");
7208 case SWM_S_REGION_PADDING:
7209 region_padding = atoi(value);
7210 if (region_padding < 0)
7213 case SWM_S_SPAWN_ORDER:
7214 if (!strcmp(value, "first"))
7215 spawn_position = SWM_STACK_BOTTOM;
7216 else if (!strcmp(value, "last"))
7217 spawn_position = SWM_STACK_TOP;
7218 else if (!strcmp(value, "next"))
7219 spawn_position = SWM_STACK_ABOVE;
7220 else if (!strcmp(value, "previous"))
7221 spawn_position = SWM_STACK_BELOW;
7223 errx(1, "spawn_position");
7225 case SWM_S_SPAWN_TERM:
7226 setconfspawn("term", value, 0);
7227 setconfspawn("spawn_term", value, 0);
7230 /* No longer needed; leave to not break old conf files. */
7232 case SWM_S_SS_ENABLED:
7233 /* No longer needed; leave to not break old conf files. */
7235 case SWM_S_STACK_ENABLED:
7236 stack_enabled = atoi(value);
7238 case SWM_S_TERM_WIDTH:
7239 term_width = atoi(value);
7243 case SWM_S_TILE_GAP:
7244 tile_gap = atoi(value);
7246 case SWM_S_TITLE_CLASS_ENABLED:
7247 title_class_enabled = atoi(value);
7249 case SWM_S_TITLE_NAME_ENABLED:
7250 title_name_enabled = atoi(value);
7252 case SWM_S_URGENT_ENABLED:
7253 urgent_enabled = atoi(value);
7255 case SWM_S_VERBOSE_LAYOUT:
7256 verbose_layout = atoi(value);
7257 for (i = 0; layouts[i].l_stack != NULL; i++) {
7259 layouts[i].l_string = fancy_stacker;
7261 layouts[i].l_string = plain_stacker;
7264 case SWM_S_WINDOW_NAME_ENABLED:
7265 window_name_enabled = atoi(value);
7267 case SWM_S_WORKSPACE_LIMIT:
7268 workspace_limit = atoi(value);
7269 if (workspace_limit > SWM_WS_MAX)
7270 workspace_limit = SWM_WS_MAX;
7271 else if (workspace_limit < 1)
7272 workspace_limit = 1;
7281 setconfmodkey(char *selector, char *value, int flags)
7283 /* suppress unused warnings since vars are needed */
7287 if (!strncasecmp(value, "Mod1", strlen("Mod1")))
7288 update_modkey(XCB_MOD_MASK_1);
7289 else if (!strncasecmp(value, "Mod2", strlen("Mod2")))
7290 update_modkey(XCB_MOD_MASK_2);
7291 else if (!strncasecmp(value, "Mod3", strlen("Mod3")))
7292 update_modkey(XCB_MOD_MASK_3);
7293 else if (!strncasecmp(value, "Mod4", strlen("Mod4")))
7294 update_modkey(XCB_MOD_MASK_4);
7301 setconfcolor(char *selector, char *value, int flags)
7303 setscreencolor(value, ((selector == NULL)?-1:atoi(selector)), flags);
7308 setconfregion(char *selector, char *value, int flags)
7310 /* suppress unused warnings since vars are needed */
7314 custom_region(value);
7319 setautorun(char *selector, char *value, int flags)
7329 /* suppress unused warnings since vars are needed */
7333 if (getenv("SWM_STARTED"))
7337 if (sscanf(value, "ws[%d]:%1023c", &ws_id, s) != 2)
7338 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
7340 if (ws_id < 0 || ws_id >= workspace_limit)
7341 errx(1, "autorun: invalid workspace %d", ws_id + 1);
7343 sp = expand_tilde((char *)&s);
7346 * This is a little intricate
7348 * If the pid already exists we simply reuse it because it means it was
7349 * used before AND not claimed by manage_window. We get away with
7350 * altering it in the parent after INSERT because this can not be a race
7353 while ((ap = strsep(&sp, " \t")) != NULL) {
7356 DNPRINTF(SWM_D_SPAWN, "setautorun: arg [%s]\n", ap);
7358 if ((a.argv = realloc(a.argv, argc * sizeof(char *))) == NULL)
7359 err(1, "setautorun: realloc");
7360 a.argv[argc - 1] = ap;
7364 if ((a.argv = realloc(a.argv, (argc + 1) * sizeof(char *))) == NULL)
7365 err(1, "setautorun: realloc");
7366 a.argv[argc] = NULL;
7368 if ((pid = fork()) == 0) {
7369 spawn(ws_id, &a, 1);
7378 p = calloc(1, sizeof *p);
7381 TAILQ_INSERT_TAIL(&pidlist, p, entry);
7391 setlayout(char *selector, char *value, int flags)
7393 int ws_id, i, x, mg, ma, si, ar, f = 0;
7394 int st = SWM_V_STACK, num_screens;
7396 struct workspace *ws;
7398 /* suppress unused warnings since vars are needed */
7402 if (getenv("SWM_STARTED"))
7406 if (sscanf(value, "ws[%d]:%d:%d:%d:%d:%1023c",
7407 &ws_id, &mg, &ma, &si, &ar, s) != 6)
7408 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
7409 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
7412 if (ws_id < 0 || ws_id >= workspace_limit)
7413 errx(1, "layout: invalid workspace %d", ws_id + 1);
7415 if (!strcasecmp(s, "vertical"))
7417 else if (!strcasecmp(s, "vertical_flip")) {
7420 } else if (!strcasecmp(s, "horizontal"))
7422 else if (!strcasecmp(s, "horizontal_flip")) {
7425 } else if (!strcasecmp(s, "fullscreen"))
7428 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
7429 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
7432 num_screens = get_screen_count();
7433 for (i = 0; i < num_screens; i++) {
7434 ws = (struct workspace *)&screens[i].ws;
7435 ws[ws_id].cur_layout = &layouts[st];
7437 ws[ws_id].always_raise = ar;
7438 if (st == SWM_MAX_STACK)
7442 for (x = 0; x < abs(mg); x++) {
7443 ws[ws_id].cur_layout->l_config(&ws[ws_id],
7444 mg >= 0 ? SWM_ARG_ID_MASTERGROW :
7445 SWM_ARG_ID_MASTERSHRINK);
7449 for (x = 0; x < abs(ma); x++) {
7450 ws[ws_id].cur_layout->l_config(&ws[ws_id],
7451 ma >= 0 ? SWM_ARG_ID_MASTERADD :
7452 SWM_ARG_ID_MASTERDEL);
7456 for (x = 0; x < abs(si); x++) {
7457 ws[ws_id].cur_layout->l_config(&ws[ws_id],
7458 si >= 0 ? SWM_ARG_ID_STACKINC :
7459 SWM_ARG_ID_STACKDEC);
7464 ws[ws_id].cur_layout->l_config(&ws[ws_id],
7465 SWM_ARG_ID_FLIPLAYOUT);
7475 /* config options */
7476 struct config_option {
7478 int (*func)(char*, char*, int);
7481 struct config_option configopt[] = {
7482 { "autorun", setautorun, 0 },
7483 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
7484 { "bar_at_bottom", setconfvalue, SWM_S_BAR_AT_BOTTOM },
7485 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
7486 { "bar_border_unfocus", setconfcolor, SWM_S_COLOR_BAR_BORDER_UNFOCUS },
7487 { "bar_border_width", setconfvalue, SWM_S_BAR_BORDER_WIDTH },
7488 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
7489 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
7490 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
7491 { "bar_enabled_ws", setconfvalue, SWM_S_BAR_ENABLED_WS },
7492 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
7493 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
7494 { "bar_format", setconfvalue, SWM_S_BAR_FORMAT },
7495 { "bar_justify", setconfvalue, SWM_S_BAR_JUSTIFY },
7496 { "bind", setconfbinding, 0 },
7497 { "border_width", setconfvalue, SWM_S_BORDER_WIDTH },
7498 { "boundary_width", setconfvalue, SWM_S_BOUNDARY_WIDTH },
7499 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
7500 { "clock_format", setconfvalue, SWM_S_CLOCK_FORMAT },
7501 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
7502 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
7503 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
7504 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
7505 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
7506 { "disable_border", setconfvalue, SWM_S_DISABLE_BORDER },
7507 { "focus_close", setconfvalue, SWM_S_FOCUS_CLOSE },
7508 { "focus_close_wrap", setconfvalue, SWM_S_FOCUS_CLOSE_WRAP },
7509 { "focus_default", setconfvalue, SWM_S_FOCUS_DEFAULT },
7510 { "focus_mode", setconfvalue, SWM_S_FOCUS_MODE },
7511 { "keyboard_mapping", setkeymapping, 0 },
7512 { "layout", setlayout, 0 },
7513 { "modkey", setconfmodkey, 0 },
7514 { "program", setconfspawn, 0 },
7515 { "quirk", setconfquirk, 0 },
7516 { "region", setconfregion, 0 },
7517 { "region_padding", setconfvalue, SWM_S_REGION_PADDING },
7518 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
7519 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
7520 { "spawn_position", setconfvalue, SWM_S_SPAWN_ORDER },
7521 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
7522 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
7523 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
7524 { "tile_gap", setconfvalue, SWM_S_TILE_GAP },
7525 { "title_class_enabled", setconfvalue, SWM_S_TITLE_CLASS_ENABLED },
7526 { "title_name_enabled", setconfvalue, SWM_S_TITLE_NAME_ENABLED },
7527 { "urgent_enabled", setconfvalue, SWM_S_URGENT_ENABLED },
7528 { "verbose_layout", setconfvalue, SWM_S_VERBOSE_LAYOUT },
7529 { "window_name_enabled", setconfvalue, SWM_S_WINDOW_NAME_ENABLED },
7530 { "workspace_limit", setconfvalue, SWM_S_WORKSPACE_LIMIT },
7534 _add_startup_exception(const char *fmt, va_list ap)
7536 if (vasprintf(&startup_exception, fmt, ap) == -1)
7537 warn("%s: asprintf", __func__);
7541 add_startup_exception(const char *fmt, ...)
7547 if (startup_exception)
7550 /* force bar to be enabled due to exception */
7554 _add_startup_exception(fmt, ap);
7559 conf_load(const char *filename, int keymapping)
7562 char *line = NULL, *cp, *optsub, *optval = NULL;
7563 size_t linelen, lineno = 0;
7564 int wordlen, i, optidx;
7565 struct config_option *opt = NULL;
7567 DNPRINTF(SWM_D_CONF, "conf_load: begin\n");
7569 if (filename == NULL) {
7570 warnx("conf_load: no filename");
7574 DNPRINTF(SWM_D_CONF, "conf_load: open %s\n", filename);
7576 if ((config = fopen(filename, "r")) == NULL) {
7577 warn("conf_load: fopen: %s", filename);
7581 while (!feof(config)) {
7585 if ((line = fparseln(config, &linelen, &lineno, NULL,
7586 FPARSELN_UNESCCOMM | FPARSELN_UNESCCONT)) == NULL) {
7588 err(1, "%s", filename);
7593 cp += strspn(cp, " \t\n"); /* eat whitespace */
7594 if (cp[0] == '\0') {
7598 /* get config option */
7599 wordlen = strcspn(cp, "=[ \t\n");
7601 add_startup_exception("%s: line %zd: no option found",
7606 for (i = 0; i < LENGTH(configopt); i++) {
7607 opt = &configopt[i];
7608 if (!strncasecmp(cp, opt->optname, wordlen) &&
7609 (int)strlen(opt->optname) == wordlen) {
7615 add_startup_exception("%s: line %zd: unknown option "
7616 "%.*s", filename, lineno, wordlen, cp);
7619 if (keymapping && opt && strcmp(opt->optname, "bind")) {
7620 add_startup_exception("%s: line %zd: invalid option "
7621 "%.*s", filename, lineno, wordlen, cp);
7625 cp += strspn(cp, " \t\n"); /* eat whitespace */
7627 /* from here on out we call goto invalid to continue */
7629 /* get [selector] if any */
7633 wordlen = strcspn(cp, "]");
7636 add_startup_exception("%s: line %zd: "
7637 "syntax error", filename, lineno);
7641 if (asprintf(&optsub, "%.*s", wordlen, cp) ==
7643 add_startup_exception("%s: line %zd: "
7644 "unable to allocatememory for "
7645 "selector", filename, lineno);
7650 cp += strspn(cp, "] \t\n"); /* eat trailing */
7652 cp += strspn(cp, "= \t\n"); /* eat trailing */
7654 optval = strdup(cp);
7655 if (strlen(optval) == 0) {
7656 add_startup_exception("%s: line %zd: must supply value "
7657 "to %s", filename, lineno,
7658 configopt[optidx].optname);
7661 /* call function to deal with it all */
7662 if (configopt[optidx].func(optsub, optval,
7663 configopt[optidx].funcflags) != 0) {
7664 add_startup_exception("%s: line %zd: invalid data for "
7665 "%s", filename, lineno, configopt[optidx].optname);
7682 DNPRINTF(SWM_D_CONF, "conf_load: end\n");
7688 set_child_transient(struct ws_win *win, xcb_window_t *trans)
7690 struct ws_win *parent, *w;
7691 struct swm_region *r;
7692 struct workspace *ws;
7693 xcb_icccm_wm_hints_t wmh;
7695 parent = find_window(win->transient);
7697 parent->focus_child = win;
7699 DNPRINTF(SWM_D_MISC, "set_child_transient: parent doesn't exist"
7700 " for 0x%x trans 0x%x\n", win->id, win->transient);
7702 r = root_to_region(win->wa->root, SWM_CK_ALL);
7704 /* parent doen't exist in our window list */
7705 TAILQ_FOREACH(w, &ws->winlist, entry) {
7706 if (xcb_icccm_get_wm_hints_reply(conn,
7707 xcb_icccm_get_wm_hints(conn, w->id),
7709 warnx("can't get hints for 0x%x", w->id);
7713 if (win->hints.window_group != wmh.window_group)
7716 w->focus_child = win;
7717 win->transient = w->id;
7719 DNPRINTF(SWM_D_MISC, "set_child_transient: adjusting "
7720 "transient to 0x%x\n", win->transient);
7727 window_get_pid(xcb_window_t win)
7732 xcb_get_property_cookie_t pc;
7733 xcb_get_property_reply_t *pr;
7735 apid = get_atom_from_string("_NET_WM_PID");
7736 if (apid == XCB_ATOM_NONE)
7739 pc = xcb_get_property(conn, 0, win, apid, XCB_ATOM_CARDINAL, 0, 1);
7740 pr = xcb_get_property_reply(conn, pc, NULL);
7743 if (pr->type != XCB_ATOM_CARDINAL) {
7748 if (pr->type == apid && pr->format == 32)
7749 ret = *((pid_t *)xcb_get_property_value(pr));
7755 apid = get_atom_from_string("_SWM_PID");
7756 pc = xcb_get_property(conn, 0, win, apid, XCB_ATOM_STRING,
7758 pr = xcb_get_property_reply(conn, pc, NULL);
7761 if (pr->type != apid) {
7766 ret = (pid_t)strtonum(xcb_get_property_value(pr), 0, INT_MAX, &errstr);
7773 get_ws_idx(xcb_window_t id)
7779 xcb_get_property_reply_t *gpr;
7781 gpr = xcb_get_property_reply(conn,
7782 xcb_get_property(conn, 0, id, a_swm_ws,
7783 XCB_ATOM_STRING, 0, SWM_PROPLEN),
7788 proplen = xcb_get_property_value_length(gpr);
7790 prop = malloc(proplen + 1);
7793 xcb_get_property_value(gpr),
7795 prop[proplen] = '\0';
7802 DNPRINTF(SWM_D_PROP, "get_ws_idx: _SWM_WS: %s\n", prop);
7803 ws_idx = (int)strtonum(prop, 0, workspace_limit - 1, &errstr);
7805 DNPRINTF(SWM_D_PROP, "get_ws_idx: window: #%s: %s",
7815 manage_window(xcb_window_t id, uint16_t mapped)
7817 xcb_window_t trans = XCB_WINDOW_NONE;
7818 struct ws_win *win, *ww;
7820 char ws_idx_str[SWM_PROPLEN];
7821 struct swm_region *r;
7825 xcb_icccm_get_wm_protocols_reply_t wpr;
7827 if ((win = find_window(id)) != NULL) {
7828 DNPRINTF(SWM_D_MISC, "manage_window: win 0x%x already "
7829 "managed; skipping.)\n", id);
7830 return (win); /* Already managed. */
7833 /* See if window is on the unmanaged list. */
7834 if ((win = find_unmanaged_window(id)) != NULL) {
7835 DNPRINTF(SWM_D_MISC, "manage_window: win 0x%x found on "
7836 "unmanaged list.\n", id);
7837 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
7840 set_child_transient(win, &trans);
7844 DNPRINTF(SWM_D_MISC, "manage_window: win 0x%x is new.\n", id);
7847 /* Create and initialize ws_win object. */
7848 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
7849 err(1, "manage_window: calloc: failed to allocate memory for "
7854 /* Get window geometry. */
7855 win->wa = xcb_get_geometry_reply(conn,
7856 xcb_get_geometry(conn, win->id),
7859 /* Figure out which region the window belongs to. */
7860 r = root_to_region(win->wa->root, SWM_CK_ALL);
7862 /* Ignore window border if there is one. */
7863 WIDTH(win) = win->wa->width;
7864 HEIGHT(win) = win->wa->height;
7865 X(win) = win->wa->x + win->wa->border_width - border_width;
7866 Y(win) = win->wa->y + win->wa->border_width - border_width;
7868 win->mapped = mapped;
7869 win->floatmaxed = 0;
7870 win->ewmh_flags = 0;
7871 win->s = r->s; /* this never changes */
7873 /* Get WM_SIZE_HINTS. */
7874 xcb_icccm_get_wm_normal_hints_reply(conn,
7875 xcb_icccm_get_wm_normal_hints(conn, win->id),
7879 xcb_icccm_get_wm_hints_reply(conn,
7880 xcb_icccm_get_wm_hints(conn, win->id),
7883 /* Get WM_TRANSIENT_FOR; see if window is a transient. */
7884 xcb_icccm_get_wm_transient_for_reply(conn,
7885 xcb_icccm_get_wm_transient_for(conn, win->id),
7888 win->transient = trans;
7889 set_child_transient(win, &win->transient);
7892 /* Get supported protocols. */
7893 if (xcb_icccm_get_wm_protocols_reply(conn,
7894 xcb_icccm_get_wm_protocols(conn, win->id, a_prot),
7896 for (i = 0; i < wpr.atoms_len; i++) {
7897 if (wpr.atoms[i] == a_takefocus)
7898 win->take_focus = 1;
7899 if (wpr.atoms[i] == a_delete)
7900 win->can_delete = 1;
7902 xcb_icccm_get_wm_protocols_reply_wipe(&wpr);
7905 win->iconic = get_swm_iconic(win);
7907 /* Figure out which workspace the window belongs to. */
7908 if ((p = find_pid(window_get_pid(win->id))) != NULL) {
7909 win->ws = &r->s->ws[p->ws];
7910 TAILQ_REMOVE(&pidlist, p, entry);
7913 } else if ((ws_idx = get_ws_idx(win->id)) != -1 &&
7915 /* _SWM_WS is set; use that. */
7916 win->ws = &r->s->ws[ws_idx];
7917 } else if (trans && (ww = find_window(trans)) != NULL) {
7918 /* Launch transients in the same ws as parent. */
7924 /* Set the _SWM_WS atom so we can remember this after reincarnation. */
7925 if (snprintf(ws_idx_str, SWM_PROPLEN, "%d", win->ws->idx) <
7927 DNPRINTF(SWM_D_PROP, "manage_window: set _SWM_WS: %s\n",
7929 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
7930 a_swm_ws, XCB_ATOM_STRING, 8, strlen(ws_idx_str),
7934 /* WS must already be set for this to work. */
7935 store_float_geom(win);
7938 ewmh_autoquirk(win);
7940 /* Determine initial quirks. */
7941 if (xcb_icccm_get_wm_class_reply(conn,
7942 xcb_icccm_get_wm_class(conn, win->id),
7944 DNPRINTF(SWM_D_CLASS, "manage_window: class: %s, name: %s\n",
7945 win->ch.class_name, win->ch.instance_name);
7947 /* java is retarded so treat it special */
7948 if (strstr(win->ch.instance_name, "sun-awt")) {
7949 DNPRINTF(SWM_D_CLASS, "manage_window: java window "
7954 TAILQ_FOREACH(qp, &quirks, entry) {
7955 if (!strcmp(win->ch.class_name, qp->class) &&
7956 !strcmp(win->ch.instance_name, qp->name)) {
7957 DNPRINTF(SWM_D_CLASS, "manage_window: on quirks"
7958 "list; mask: 0x%lx\n", qp->quirk);
7959 if (qp->quirk & SWM_Q_FLOAT)
7961 win->quirks = qp->quirk;
7966 /* Alter window position if quirky */
7967 if (win->quirks & SWM_Q_ANYWHERE)
7970 /* Reset font sizes (the bruteforce way; no default keybinding). */
7971 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
7972 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
7973 fake_keypress(win, XK_KP_Subtract, XCB_MOD_MASK_SHIFT);
7974 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
7975 fake_keypress(win, XK_KP_Add, XCB_MOD_MASK_SHIFT);
7978 /* Make sure window is positioned inside its region, if its active. */
7980 region_containment(win, r, SWM_CW_ALLSIDES |
7981 SWM_CW_HARDBOUNDARY);
7985 /* Select which X events to monitor and set border pixel color. */
7986 wa[0] = win->s->c[SWM_S_COLOR_UNFOCUS].pixel;
7987 wa[1] = XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_PROPERTY_CHANGE |
7988 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
7990 wa[1] |= XCB_EVENT_MASK_LEAVE_WINDOW | XCB_EVENT_MASK_FOCUS_CHANGE;
7993 xcb_change_window_attributes(conn, win->id, XCB_CW_BORDER_PIXEL |
7994 XCB_CW_EVENT_MASK, wa);
7997 /* Figure out where to stack the window in the workspace. */
7998 if (trans && (ww = find_window(trans)))
7999 TAILQ_INSERT_AFTER(&win->ws->winlist, ww, win, entry);
8000 else if (win->ws->focus && spawn_position == SWM_STACK_ABOVE)
8001 TAILQ_INSERT_AFTER(&win->ws->winlist, win->ws->focus, win,
8003 else if (win->ws->focus && spawn_position == SWM_STACK_BELOW)
8004 TAILQ_INSERT_BEFORE(win->ws->focus, win, entry);
8005 else switch (spawn_position) {
8008 case SWM_STACK_ABOVE:
8009 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
8011 case SWM_STACK_BOTTOM:
8012 case SWM_STACK_BELOW:
8013 TAILQ_INSERT_HEAD(&win->ws->winlist, win, entry);
8016 /* Get initial _NET_WM_STATE */
8017 ewmh_get_win_state(win);
8018 /* Set initial _NET_WM_ALLOWED_ACTIONS */
8019 ewmh_update_actions(win);
8023 DNPRINTF(SWM_D_MISC, "manage_window: done. window: 0x%x, (x,y) w x h: "
8024 "(%d,%d) %d x %d, ws: %d, iconic: %s, transient: 0x%x\n", win->id,
8025 X(win), Y(win), WIDTH(win), HEIGHT(win), win->ws->idx,
8026 YESNO(win->iconic), win->transient);
8032 free_window(struct ws_win *win)
8034 DNPRINTF(SWM_D_MISC, "free_window: window: 0x%x\n", win->id);
8039 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
8044 xcb_icccm_get_wm_class_reply_wipe(&win->ch);
8049 memset(win, 0xff, sizeof *win); /* XXX kill later */
8052 DNPRINTF(SWM_D_MISC, "free_window: done.\n");
8056 unmanage_window(struct ws_win *win)
8058 struct ws_win *parent;
8063 DNPRINTF(SWM_D_MISC, "unmanage_window: window: 0x%x\n", win->id);
8065 if (win->transient) {
8066 parent = find_window(win->transient);
8068 parent->focus_child = NULL;
8071 TAILQ_REMOVE(&win->ws->winlist, win, entry);
8072 TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
8076 expose(xcb_expose_event_t *e)
8079 struct swm_region *r;
8081 DNPRINTF(SWM_D_EVENT, "expose: window: 0x%x\n", e->window);
8083 num_screens = get_screen_count();
8084 for (i = 0; i < num_screens; i++)
8085 TAILQ_FOREACH(r, &screens[i].rl, entry)
8086 if (e->window == WINID(r->bar))
8094 focusin(xcb_focus_in_event_t *e)
8096 DNPRINTF(SWM_D_EVENT, "focusin: window: 0x%x, mode: %s(%u), "
8097 "detail: %s(%u)\n", e->event, get_notify_mode_label(e->mode),
8098 e->mode, get_notify_detail_label(e->detail), e->detail);
8102 focusout(xcb_focus_out_event_t *e)
8104 DNPRINTF(SWM_D_EVENT, "focusout: window: 0x%x, mode: %s(%u), "
8105 "detail: %s(%u)\n", e->event, get_notify_mode_label(e->mode),
8106 e->mode, get_notify_detail_label(e->detail), e->detail);
8111 keypress(xcb_key_press_event_t *e)
8113 xcb_keysym_t keysym;
8116 keysym = xcb_key_press_lookup_keysym(syms, e, 0);
8118 DNPRINTF(SWM_D_EVENT, "keypress: keysym: %u, win (x,y): 0x%x (%d,%d), "
8119 "detail: %u, time: %u, root (x,y): 0x%x (%d,%d), child: 0x%x, "
8120 "state: %u, same_screen: %s\n", keysym, e->event, e->event_x,
8121 e->event_y, e->detail, e->time, e->root, e->root_x, e->root_y,
8122 e->child, e->state, YESNO(e->same_screen));
8124 if ((kp = key_lookup(CLEANMASK(e->state), keysym)) == NULL)
8127 last_event_time = e->time;
8129 if (kp->funcid == KF_SPAWN_CUSTOM)
8130 spawn_custom(root_to_region(e->root, SWM_CK_ALL),
8131 &(keyfuncs[kp->funcid].args), kp->spawn_name);
8132 else if (keyfuncs[kp->funcid].func)
8133 keyfuncs[kp->funcid].func(root_to_region(e->root, SWM_CK_ALL),
8134 &(keyfuncs[kp->funcid].args));
8137 /* Unfreeze grab events. */
8138 xcb_allow_events(conn, XCB_ALLOW_ASYNC_KEYBOARD, e->time);
8141 DNPRINTF(SWM_D_EVENT, "keypress: done.\n");
8145 buttonpress(xcb_button_press_event_t *e)
8147 struct ws_win *win = NULL;
8148 struct swm_region *r, *old_r;
8152 DNPRINTF(SWM_D_EVENT, "buttonpress: win (x,y): 0x%x (%d,%d), "
8153 "detail: %u, time: %u, root (x,y): 0x%x (%d,%d), child: 0x%x, "
8154 "state: %u, same_screen: %s\n", e->event, e->event_x, e->event_y,
8155 e->detail, e->time, e->root, e->root_x, e->root_y, e->child,
8156 e->state, YESNO(e->same_screen));
8158 if (e->event == e->root) {
8159 if (e->child != 0) {
8160 win = find_window(e->child);
8161 /* Pass ButtonPress to window if it isn't managed. */
8165 /* Focus on empty region */
8166 /* If no windows on region if its empty. */
8167 r = root_to_region(e->root, SWM_CK_POINTER);
8169 DNPRINTF(SWM_D_EVENT, "buttonpress: "
8170 "NULL region; ignoring.\n");
8174 if (TAILQ_EMPTY(&r->ws->winlist)) {
8175 old_r = root_to_region(e->root, SWM_CK_FOCUS);
8176 if (old_r && old_r != r)
8177 unfocus_win(old_r->ws->focus);
8179 xcb_set_input_focus(conn,
8180 XCB_INPUT_FOCUS_PARENT, e->root, e->time);
8182 /* Clear bar since empty. */
8190 win = find_window(e->event);
8196 last_event_time = e->time;
8198 focus_win(get_focus_magic(win));
8200 for (i = 0; i < LENGTH(buttons); i++)
8201 if (client_click == buttons[i].action && buttons[i].func &&
8202 buttons[i].button == e->detail &&
8203 CLEANMASK(buttons[i].mask) == CLEANMASK(e->state)) {
8204 buttons[i].func(win, &buttons[i].args);
8210 DNPRINTF(SWM_D_EVENT, "buttonpress: passing to window.\n");
8211 /* Replay event to event window */
8212 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, e->time);
8214 DNPRINTF(SWM_D_EVENT, "buttonpress: handled.\n");
8215 /* Unfreeze grab events. */
8216 xcb_allow_events(conn, XCB_ALLOW_SYNC_POINTER, e->time);
8224 print_win_geom(xcb_window_t w)
8226 xcb_get_geometry_reply_t *wa;
8228 wa = xcb_get_geometry_reply(conn, xcb_get_geometry(conn, w), NULL);
8231 DNPRINTF(SWM_D_MISC, "print_win_geom: window not found: 0x%x\n",
8236 DNPRINTF(SWM_D_MISC, "print_win_geom: window: 0x%x, root: 0x%x, "
8237 "depth: %u, (x,y) w x h: (%d,%d) %d x %d, border: %d\n",
8238 w, wa->root, wa->depth, wa->x, wa->y, wa->width, wa->height,
8247 get_stack_mode_name(uint8_t mode)
8252 case XCB_STACK_MODE_ABOVE:
8255 case XCB_STACK_MODE_BELOW:
8258 case XCB_STACK_MODE_TOP_IF:
8261 case XCB_STACK_MODE_BOTTOM_IF:
8264 case XCB_STACK_MODE_OPPOSITE:
8276 configurerequest(xcb_configure_request_event_t *e)
8279 struct swm_region *r = NULL;
8282 uint32_t wc[7] = {0};
8284 if ((win = find_window(e->window)) == NULL)
8285 if ((win = find_unmanaged_window(e->window)) == NULL)
8289 if (swm_debug & SWM_D_EVENT) {
8290 print_win_geom(e->window);
8292 DNPRINTF(SWM_D_EVENT, "configurerequest: window: 0x%x, "
8293 "parent: 0x%x, new: %s, value_mask: %u { ", e->window,
8294 e->parent, YESNO(new), e->value_mask);
8295 if (e->value_mask & XCB_CONFIG_WINDOW_X)
8296 DPRINTF("X: %d ", e->x);
8297 if (e->value_mask & XCB_CONFIG_WINDOW_Y)
8298 DPRINTF("Y: %d ", e->y);
8299 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH)
8300 DPRINTF("W: %u ", e->width);
8301 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
8302 DPRINTF("H: %u ", e->height);
8303 if (e->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH)
8304 DPRINTF("Border: %u ", e->border_width);
8305 if (e->value_mask & XCB_CONFIG_WINDOW_SIBLING)
8306 DPRINTF("Sibling: 0x%x ", e->sibling);
8307 if (e->value_mask & XCB_CONFIG_WINDOW_STACK_MODE)
8308 DPRINTF("StackMode: %s(%u) ",
8309 get_stack_mode_name(e->stack_mode), e->stack_mode);
8315 if (e->value_mask & XCB_CONFIG_WINDOW_X) {
8316 mask |= XCB_CONFIG_WINDOW_X;
8319 if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
8320 mask |= XCB_CONFIG_WINDOW_Y;
8323 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
8324 mask |= XCB_CONFIG_WINDOW_WIDTH;
8327 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
8328 mask |= XCB_CONFIG_WINDOW_HEIGHT;
8329 wc[i++] = e->height;
8331 if (e->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH) {
8332 mask |= XCB_CONFIG_WINDOW_BORDER_WIDTH;
8333 wc[i++] = e->border_width;
8335 if (e->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
8336 mask |= XCB_CONFIG_WINDOW_SIBLING;
8337 wc[i++] = e->sibling;
8339 if (e->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
8340 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
8341 wc[i++] = e->stack_mode;
8345 xcb_configure_window(conn, e->window, mask, wc);
8348 } else if ((!win->manual || win->quirks & SWM_Q_ANYWHERE) &&
8349 !(win->ewmh_flags & EWMH_F_FULLSCREEN)) {
8352 else if (win->ws->old_r)
8355 /* windows are centered unless ANYWHERE quirk is set. */
8356 if (win->quirks & SWM_Q_ANYWHERE) {
8357 if (e->value_mask & XCB_CONFIG_WINDOW_X) {
8358 win->g_float.x = e->x;
8360 win->g_float.x -= X(r);
8363 if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
8364 win->g_float.y = e->y;
8366 win->g_float.y -= Y(r);
8370 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH)
8371 win->g_float.w = e->width;
8373 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
8374 win->g_float.h = e->height;
8376 win->g_floatvalid = 1;
8378 if (win->floating && r && (win->transient ||
8379 win->ws->cur_layout != &layouts[SWM_MAX_STACK])) {
8380 WIDTH(win) = win->g_float.w;
8381 HEIGHT(win) = win->g_float.h;
8384 stack_floater(win, r);
8396 DNPRINTF(SWM_D_EVENT, "configurerequest: done.\n");
8400 configurenotify(xcb_configure_notify_event_t *e)
8404 DNPRINTF(SWM_D_EVENT, "configurenotify: win 0x%x, event win: 0x%x, "
8405 "(x,y) WxH: (%d,%d) %ux%u, border: %u, above_sibling: 0x%x, "
8406 "override_redirect: %s\n", e->window, e->event, e->x, e->y,
8407 e->width, e->height, e->border_width, e->above_sibling,
8408 YESNO(e->override_redirect));
8410 win = find_window(e->window);
8412 xcb_icccm_get_wm_normal_hints_reply(conn,
8413 xcb_icccm_get_wm_normal_hints(conn, win->id),
8416 if (font_adjusted) {
8424 destroynotify(xcb_destroy_notify_event_t *e)
8428 DNPRINTF(SWM_D_EVENT, "destroynotify: window: 0x%x\n", e->window);
8430 if ((win = find_window(e->window)) == NULL) {
8431 if ((win = find_unmanaged_window(e->window)) == NULL)
8437 if (focus_mode != SWM_FOCUS_FOLLOW) {
8438 /* If we were focused, make sure we focus on something else. */
8439 if (win == win->ws->focus)
8440 win->ws->focus_pending = get_focus_prev(win);
8443 unmanage_window(win);
8446 if (focus_mode != SWM_FOCUS_FOLLOW && WS_FOCUSED(win->ws)) {
8447 if (win->ws->focus_pending) {
8448 focus_win(win->ws->focus_pending);
8449 win->ws->focus_pending = NULL;
8450 } else if (win == win->ws->focus) {
8451 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT,
8452 win->ws->r->id, XCB_CURRENT_TIME);
8463 get_notify_detail_label(uint8_t detail)
8468 case XCB_NOTIFY_DETAIL_ANCESTOR:
8471 case XCB_NOTIFY_DETAIL_VIRTUAL:
8474 case XCB_NOTIFY_DETAIL_INFERIOR:
8477 case XCB_NOTIFY_DETAIL_NONLINEAR:
8478 label = "Nonlinear";
8480 case XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL:
8481 label = "NonlinearVirtual";
8483 case XCB_NOTIFY_DETAIL_POINTER:
8486 case XCB_NOTIFY_DETAIL_POINTER_ROOT:
8487 label = "PointerRoot";
8489 case XCB_NOTIFY_DETAIL_NONE:
8500 get_notify_mode_label(uint8_t mode)
8505 case XCB_NOTIFY_MODE_NORMAL:
8508 case XCB_NOTIFY_MODE_GRAB:
8511 case XCB_NOTIFY_MODE_UNGRAB:
8514 case XCB_NOTIFY_MODE_WHILE_GRABBED:
8515 label = "WhileGrabbed";
8526 enternotify(xcb_enter_notify_event_t *e)
8529 struct swm_region *r;
8531 DNPRINTF(SWM_D_FOCUS, "enternotify: time: %u, win (x,y): 0x%x "
8532 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): 0x%x (%d,%d), "
8533 "child: 0x%x, same_screen_focus: %s, state: %d\n",
8534 e->time, e->event, e->event_x, e->event_y,
8535 get_notify_mode_label(e->mode), e->mode,
8536 get_notify_detail_label(e->detail), e->detail,
8537 e->root, e->root_x, e->root_y, e->child,
8538 YESNO(e->same_screen_focus), e->state);
8540 if (focus_mode == SWM_FOCUS_MANUAL &&
8541 e->mode == XCB_NOTIFY_MODE_NORMAL) {
8542 DNPRINTF(SWM_D_EVENT, "enternotify: manual focus; ignoring.\n");
8546 last_event_time = e->time;
8548 if ((win = find_window(e->event)) == NULL) {
8549 if (e->event == e->root) {
8550 /* If no windows on pointer region, then focus root. */
8551 r = root_to_region(e->root, SWM_CK_POINTER);
8553 DNPRINTF(SWM_D_EVENT, "enternotify: "
8554 "NULL region; ignoring.\n");
8560 DNPRINTF(SWM_D_EVENT, "enternotify: window is NULL; "
8565 focus_win(get_focus_magic(win));
8573 leavenotify(xcb_leave_notify_event_t *e)
8575 DNPRINTF(SWM_D_FOCUS, "leavenotify: time: %u, win (x,y): 0x%x "
8576 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): 0x%x (%d,%d), "
8577 "child: 0x%x, same_screen_focus: %s, state: %d\n",
8578 e->time, e->event, e->event_x, e->event_y,
8579 get_notify_mode_label(e->mode), e->mode,
8580 get_notify_detail_label(e->detail), e->detail,
8581 e->root, e->root_x, e->root_y, e->child,
8582 YESNO(e->same_screen_focus), e->state);
8587 mapnotify(xcb_map_notify_event_t *e)
8591 DNPRINTF(SWM_D_EVENT, "mapnotify: window: 0x%x\n", e->window);
8593 if ((win = find_window(e->window)) == NULL)
8594 win = manage_window(e->window, 1);
8597 set_win_state(win, XCB_ICCCM_WM_STATE_NORMAL);
8599 if (focus_mode != SWM_FOCUS_FOLLOW && WS_FOCUSED(win->ws)) {
8600 if (win->ws->focus_pending == win) {
8602 win->ws->focus_pending = NULL;
8611 mappingnotify(xcb_mapping_notify_event_t *e)
8613 xcb_refresh_keyboard_mapping(syms, e);
8615 if (e->request == XCB_MAPPING_KEYBOARD)
8620 maprequest(xcb_map_request_event_t *e)
8622 struct ws_win *win, *w = NULL;
8623 xcb_get_window_attributes_reply_t *war;
8625 DNPRINTF(SWM_D_EVENT, "maprequest: win 0x%x\n",
8628 war = xcb_get_window_attributes_reply(conn,
8629 xcb_get_window_attributes(conn, e->window),
8632 DNPRINTF(SWM_D_EVENT, "maprequest: window lost.\n");
8636 if (war->override_redirect) {
8637 DNPRINTF(SWM_D_EVENT, "maprequest: override_redirect; "
8642 win = manage_window(e->window,
8643 (war->map_state == XCB_MAP_STATE_VIEWABLE));
8645 /* The new window should get focus; prepare. */
8646 if (focus_mode != SWM_FOCUS_FOLLOW &&
8647 !(win->quirks & SWM_Q_NOFOCUSONMAP)) {
8648 if (win->quirks & SWM_Q_FOCUSONMAP_SINGLE) {
8649 /* See if other wins of same type are already mapped. */
8650 TAILQ_FOREACH(w, &win->ws->winlist, entry) {
8651 if (w == win || !w->mapped)
8654 if (w->ch.class_name &&
8655 win->ch.class_name &&
8656 !strcmp(w->ch.class_name,
8657 win->ch.class_name) &&
8658 w->ch.instance_name &&
8659 win->ch.instance_name &&
8660 !strcmp(w->ch.instance_name,
8661 win->ch.instance_name))
8667 win->ws->focus_pending = get_focus_magic(win);
8670 /* All windows need to be mapped if they are in the current workspace.*/
8674 /* Ignore EnterNotify to handle the mapnotify without interference. */
8675 if (focus_mode == SWM_FOCUS_DEFAULT)
8676 event_drain(XCB_ENTER_NOTIFY);
8679 DNPRINTF(SWM_D_EVENT, "maprequest: done.\n");
8683 motionnotify(xcb_motion_notify_event_t *e)
8685 struct swm_region *r;
8688 DNPRINTF(SWM_D_FOCUS, "motionnotify: time: %u, win (x,y): 0x%x "
8689 "(%d,%d), detail: %s(%d), root (x,y): 0x%x (%d,%d), "
8690 "child: 0x%x, same_screen_focus: %s, state: %d\n",
8691 e->time, e->event, e->event_x, e->event_y,
8692 get_notify_detail_label(e->detail), e->detail,
8693 e->root, e->root_x, e->root_y, e->child,
8694 YESNO(e->same_screen), e->state);
8696 last_event_time = e->time;
8698 if (focus_mode == SWM_FOCUS_MANUAL)
8701 num_screens = get_screen_count();
8702 for (i = 0; i < num_screens; i++)
8703 if (screens[i].root == e->root)
8706 TAILQ_FOREACH(r, &screens[i].rl, entry)
8707 if (X(r) <= e->root_x && e->root_x < MAX_X(r) &&
8708 Y(r) <= e->root_y && e->root_y < MAX_Y(r))
8716 get_atom_name(xcb_atom_t atom)
8721 * This should be disabled during most debugging since
8722 * xcb_get_* causes an xcb_flush.
8725 xcb_get_atom_name_reply_t *r;
8727 r = xcb_get_atom_name_reply(conn,
8728 xcb_get_atom_name(conn, atom),
8731 len = xcb_get_atom_name_name_length(r);
8733 name = malloc(len + 1);
8735 memcpy(name, xcb_get_atom_name_name(r), len);
8749 propertynotify(xcb_property_notify_event_t *e)
8752 struct workspace *ws;
8756 name = get_atom_name(e->atom);
8757 DNPRINTF(SWM_D_EVENT, "propertynotify: window: 0x%x, atom: %s(%u), "
8758 "time: %#x, state: %u\n", e->window, name, e->atom, e->time,
8762 win = find_window(e->window);
8768 last_event_time = e->time;
8770 if (e->atom == a_swm_iconic) {
8771 if (e->state == XCB_PROPERTY_NEW_VALUE) {
8772 if (focus_mode != SWM_FOCUS_FOLLOW)
8773 ws->focus_pending = get_focus_prev(win);
8781 if (focus_mode != SWM_FOCUS_FOLLOW &&
8783 if (ws->focus_pending) {
8784 focus_win(ws->focus_pending);
8785 ws->focus_pending = NULL;
8787 xcb_set_input_focus(conn,
8788 XCB_INPUT_FOCUS_PARENT,
8796 } else if (e->state == XCB_PROPERTY_DELETE) {
8797 /* The window is no longer iconic, restack ws. */
8798 if (focus_mode != SWM_FOCUS_FOLLOW)
8799 ws->focus_pending = get_focus_magic(win);
8803 /* Flush EnterNotify for mapnotify, if needed. */
8806 } else if (e->atom == a_state) {
8807 /* State just changed, make sure it gets focused if mapped. */
8808 if (e->state == XCB_PROPERTY_NEW_VALUE) {
8809 if (focus_mode != SWM_FOCUS_FOLLOW && WS_FOCUSED(ws)) {
8811 ws->focus_pending == win) {
8812 focus_win(ws->focus_pending);
8813 ws->focus_pending = NULL;
8817 } else if (e->atom == XCB_ATOM_WM_CLASS ||
8818 e->atom == XCB_ATOM_WM_NAME) {
8826 unmapnotify(xcb_unmap_notify_event_t *e)
8829 struct workspace *ws;
8831 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: 0x%x\n", e->window);
8833 /* If we aren't managing the window, then ignore. */
8834 win = find_window(e->window);
8835 if (win == NULL || win->id != e->window)
8840 if (getstate(e->window) != XCB_ICCCM_WM_STATE_ICONIC)
8841 set_win_state(win, XCB_ICCCM_WM_STATE_ICONIC);
8844 /* window unmapped itself */
8845 /* do unmap/unfocus/restack and unmanage */
8848 /* If win was focused, make sure to focus on something else. */
8849 if (win == ws->focus) {
8850 if (focus_mode != SWM_FOCUS_FOLLOW) {
8851 ws->focus_pending = get_focus_prev(win);
8852 DNPRINTF(SWM_D_EVENT, "unmapnotify: "
8853 "focus_pending: 0x%x\n",
8854 WINID(ws->focus_pending));
8860 unmanage_window(win);
8865 if (WS_FOCUSED(ws)) {
8866 if (focus_mode == SWM_FOCUS_FOLLOW) {
8867 focus_win(get_pointer_win(ws->r->s->root));
8868 } else if (ws->focus_pending) {
8869 focus_win(ws->focus_pending);
8870 ws->focus_pending = NULL;
8871 } else if (ws->focus == NULL) {
8872 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT,
8873 ws->r->id, XCB_CURRENT_TIME);
8878 if (getstate(e->window) == XCB_ICCCM_WM_STATE_NORMAL)
8879 set_win_state(win, XCB_ICCCM_WM_STATE_ICONIC);
8886 visibilitynotify(xcb_visibility_notify_event_t *e)
8888 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: 0x%x\n",
8894 clientmessage(xcb_client_message_event_t *e)
8897 xcb_map_request_event_t mre;
8901 name = get_atom_name(e->type);
8902 DNPRINTF(SWM_D_EVENT, "clientmessage: window: 0x%x, atom: %s(%u)\n",
8903 e->window, name, e->type);
8906 win = find_window(e->window);
8909 if (e->type == ewmh[_NET_ACTIVE_WINDOW].atom) {
8910 /* Manage the window with maprequest. */
8911 DNPRINTF(SWM_D_EVENT, "clientmessage: request focus on "
8912 "unmanaged window.\n");
8913 mre.window = e->window;
8919 if (e->type == ewmh[_NET_ACTIVE_WINDOW].atom) {
8920 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_ACTIVE_WINDOW\n");
8921 if (WS_FOCUSED(win->ws))
8924 win->ws->focus_pending = win;
8926 if (e->type == ewmh[_NET_CLOSE_WINDOW].atom) {
8927 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_CLOSE_WINDOW\n");
8928 if (win->can_delete)
8929 client_msg(win, a_delete, 0);
8931 xcb_kill_client(conn, win->id);
8933 if (e->type == ewmh[_NET_MOVERESIZE_WINDOW].atom) {
8934 DNPRINTF(SWM_D_EVENT,
8935 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
8936 if (win->floating) {
8937 if (e->data.data32[0] & (1<<8)) /* x */
8938 X(win) = e->data.data32[1];
8939 if (e->data.data32[0] & (1<<9)) /* y */
8940 Y(win) = e->data.data32[2];
8941 if (e->data.data32[0] & (1<<10)) /* width */
8942 WIDTH(win) = e->data.data32[3];
8943 if (e->data.data32[0] & (1<<11)) /* height */
8944 HEIGHT(win) = e->data.data32[4];
8949 /* TODO: Change stack sizes */
8950 /* notify no change was made. */
8951 config_win(win, NULL);
8954 if (e->type == ewmh[_NET_WM_STATE].atom) {
8955 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_WM_STATE\n");
8956 ewmh_update_win_state(win, e->data.data32[1], e->data.data32[0]);
8957 if (e->data.data32[2])
8958 ewmh_update_win_state(win, e->data.data32[2],
8970 int errcode = xcb_connection_has_error(conn);
8971 #ifdef XCB_CONN_ERROR
8974 case XCB_CONN_ERROR:
8975 s = "Socket error, pipe error or other stream error.";
8977 case XCB_CONN_CLOSED_EXT_NOTSUPPORTED:
8978 s = "Extension not supported.";
8980 case XCB_CONN_CLOSED_MEM_INSUFFICIENT:
8981 s = "Insufficient memory.";
8983 case XCB_CONN_CLOSED_REQ_LEN_EXCEED:
8984 s = "Request length was exceeded.";
8986 case XCB_CONN_CLOSED_PARSE_ERR:
8987 s = "Error parsing display string.";
8990 s = "Unknown error.";
8993 errx(errcode, "X CONNECTION ERROR: %s", s);
8996 errx(errcode, "X CONNECTION ERROR");
9004 const uint32_t val = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
9005 XCB_EVENT_MASK_ENTER_WINDOW;
9007 xcb_void_cookie_t wac;
9008 xcb_generic_error_t *error;
9010 /* this causes an error if some other window manager is running */
9011 num_screens = get_screen_count();
9012 for (i = 0; i < num_screens; i++) {
9013 if ((sc = get_screen(i)) == NULL)
9014 errx(1, "ERROR: can't get screen %d.", i);
9015 DNPRINTF(SWM_D_INIT, "enable_wm: screen %d, root: 0x%x\n",
9017 wac = xcb_change_window_attributes_checked(conn, sc->root,
9018 XCB_CW_EVENT_MASK, &val);
9019 if ((error = xcb_request_check(conn, wac))) {
9020 DNPRINTF(SWM_D_INIT, "enable_wm: error_code: %u\n",
9026 /* click to focus on empty region */
9027 xcb_grab_button(conn, 1, sc->root, BUTTONMASK,
9028 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC, XCB_WINDOW_NONE,
9029 XCB_CURSOR_NONE, XCB_BUTTON_INDEX_1, XCB_BUTTON_MASK_ANY);
9036 new_region(struct swm_screen *s, int x, int y, int w, int h)
9038 struct swm_region *r, *n;
9039 struct workspace *ws = NULL;
9043 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
9044 s->idx, w, h, x, y);
9046 /* remove any conflicting regions */
9047 n = TAILQ_FIRST(&s->rl);
9050 n = TAILQ_NEXT(r, entry);
9051 if (X(r) < (x + w) && (X(r) + WIDTH(r)) > x &&
9052 Y(r) < (y + h) && (Y(r) + HEIGHT(r)) > y) {
9053 if (r->ws->r != NULL)
9054 r->ws->old_r = r->ws->r;
9057 xcb_destroy_window(conn, r->id);
9058 TAILQ_REMOVE(&s->rl, r, entry);
9059 TAILQ_INSERT_TAIL(&s->orl, r, entry);
9063 /* search old regions for one to reuse */
9065 /* size + location match */
9066 TAILQ_FOREACH(r, &s->orl, entry)
9067 if (X(r) == x && Y(r) == y &&
9068 HEIGHT(r) == h && WIDTH(r) == w)
9072 TAILQ_FOREACH(r, &s->orl, entry)
9073 if (HEIGHT(r) == h && WIDTH(r) == w)
9077 TAILQ_REMOVE(&s->orl, r, entry);
9078 /* try to use old region's workspace */
9079 if (r->ws->r == NULL)
9082 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
9083 err(1, "new_region: calloc: failed to allocate memory "
9086 /* if we don't have a workspace already, find one */
9088 for (i = 0; i < workspace_limit; i++)
9089 if (s->ws[i].r == NULL) {
9096 errx(1, "new_region: no free workspaces");
9107 TAILQ_INSERT_TAIL(&s->rl, r, entry);
9109 /* Invisible region window to detect pointer events on empty regions. */
9110 r->id = xcb_generate_id(conn);
9111 wa[0] = XCB_EVENT_MASK_POINTER_MOTION |
9112 XCB_EVENT_MASK_POINTER_MOTION_HINT;
9114 xcb_create_window(conn, XCB_COPY_FROM_PARENT, r->id, r->s->root,
9115 X(r), Y(r), WIDTH(r), HEIGHT(r), 0, XCB_WINDOW_CLASS_INPUT_ONLY,
9116 XCB_COPY_FROM_PARENT, XCB_CW_EVENT_MASK, wa);
9118 xcb_map_window(conn, r->id);
9124 #ifdef SWM_XRR_HAS_CRTC
9127 #endif /* SWM_XRR_HAS_CRTC */
9128 struct swm_region *r;
9131 xcb_randr_get_screen_resources_current_cookie_t src;
9132 xcb_randr_get_screen_resources_current_reply_t *srr;
9133 xcb_randr_get_crtc_info_cookie_t cic;
9134 xcb_randr_get_crtc_info_reply_t *cir = NULL;
9135 xcb_randr_crtc_t *crtc;
9136 xcb_screen_t *screen;
9138 DNPRINTF(SWM_D_MISC, "scan_xrandr: screen: %d\n", i);
9140 if ((screen = get_screen(i)) == NULL)
9141 errx(1, "ERROR: can't get screen %d.", i);
9143 num_screens = get_screen_count();
9144 if (i >= num_screens)
9145 errx(1, "scan_xrandr: invalid screen");
9147 /* remove any old regions */
9148 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
9149 r->ws->old_r = r->ws->r = NULL;
9151 xcb_destroy_window(conn, r->id);
9152 TAILQ_REMOVE(&screens[i].rl, r, entry);
9153 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
9157 /* map virtual screens onto physical screens */
9158 #ifdef SWM_XRR_HAS_CRTC
9159 if (xrandr_support) {
9160 src = xcb_randr_get_screen_resources_current(conn,
9162 srr = xcb_randr_get_screen_resources_current_reply(conn, src,
9165 new_region(&screens[i], 0, 0,
9166 screen->width_in_pixels,
9167 screen->height_in_pixels);
9170 ncrtc = srr->num_crtcs;
9172 crtc = xcb_randr_get_screen_resources_current_crtcs(srr);
9173 for (c = 0; c < ncrtc; c++) {
9174 cic = xcb_randr_get_crtc_info(conn, crtc[c],
9176 cir = xcb_randr_get_crtc_info_reply(conn, cic, NULL);
9179 if (cir->num_outputs == 0) {
9185 new_region(&screens[i], 0, 0,
9186 screen->width_in_pixels,
9187 screen->height_in_pixels);
9189 new_region(&screens[i],
9190 cir->x, cir->y, cir->width, cir->height);
9195 #endif /* SWM_XRR_HAS_CRTC */
9197 /* If detection failed, create a single region that spans the screen. */
9198 if (TAILQ_EMPTY(&screens[i].rl))
9199 new_region(&screens[i], 0, 0, screen->width_in_pixels,
9200 screen->height_in_pixels);
9203 /* Cleanup unused previously visible workspaces. */
9204 TAILQ_FOREACH(r, &screens[i].orl, entry) {
9205 TAILQ_FOREACH(win, &r->ws->winlist, entry)
9208 /* The screen shouldn't focus on an unused region. */
9209 if (screens[i].r_focus == r)
9210 screens[i].r_focus = NULL;
9213 DNPRINTF(SWM_D_MISC, "scan_xrandr: done.\n");
9217 screenchange(xcb_randr_screen_change_notify_event_t *e)
9219 struct swm_region *r;
9222 DNPRINTF(SWM_D_EVENT, "screenchange: root: 0x%x\n", e->root);
9224 num_screens = get_screen_count();
9225 /* silly event doesn't include the screen index */
9226 for (i = 0; i < num_screens; i++)
9227 if (screens[i].root == e->root)
9229 if (i >= num_screens)
9230 errx(1, "screenchange: screen not found");
9232 /* brute force for now, just re-enumerate the regions */
9236 print_win_geom(e->root);
9238 /* add bars to all regions */
9239 for (i = 0; i < num_screens; i++) {
9240 TAILQ_FOREACH(r, &screens[i].rl, entry)
9246 /* Make sure a region has focus on each screen. */
9247 for (i = 0; i < num_screens; i++) {
9248 if (screens[i].r_focus == NULL) {
9249 r = TAILQ_FIRST(&screens[i].rl);
9262 struct swm_region *r = NULL;
9263 xcb_window_t *wins = NULL, trans;
9265 int i, j, num_screens;
9266 uint16_t state, manage, mapped;
9268 xcb_query_tree_cookie_t qtc;
9269 xcb_query_tree_reply_t *qtr;
9270 xcb_get_window_attributes_cookie_t gac;
9271 xcb_get_window_attributes_reply_t *gar;
9272 xcb_get_property_cookie_t pc;
9274 DNPRINTF(SWM_D_INIT, "grab_windows: begin\n");
9275 num_screens = get_screen_count();
9276 for (i = 0; i < num_screens; i++) {
9277 qtc = xcb_query_tree(conn, screens[i].root);
9278 qtr = xcb_query_tree_reply(conn, qtc, NULL);
9281 wins = xcb_query_tree_children(qtr);
9282 no = xcb_query_tree_children_length(qtr);
9283 /* attach windows to a region */
9284 /* normal windows */
9285 DNPRINTF(SWM_D_INIT, "grab_windows: grab top level windows.\n");
9286 for (j = 0; j < no; j++) {
9287 TAILQ_FOREACH(r, &screens[i].rl, entry) {
9288 if (r->id == wins[j]) {
9289 DNPRINTF(SWM_D_INIT, "grab_windows: "
9290 "skip %#x; region input window.\n",
9299 gac = xcb_get_window_attributes(conn, wins[j]);
9300 gar = xcb_get_window_attributes_reply(conn, gac, NULL);
9302 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
9303 "doesn't exist.\n", wins[j]);
9307 if (gar->override_redirect) {
9308 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
9309 "override_redirect set.\n", wins[j]);
9314 pc = xcb_icccm_get_wm_transient_for(conn, wins[j]);
9315 if (xcb_icccm_get_wm_transient_for_reply(conn, pc,
9317 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
9318 "is transient for %#x.\n", wins[j], trans);
9323 state = getstate(wins[j]);
9324 manage = state != XCB_ICCCM_WM_STATE_WITHDRAWN;
9325 mapped = gar->map_state != XCB_MAP_STATE_UNMAPPED;
9326 if (mapped || manage)
9327 manage_window(wins[j], mapped);
9330 /* transient windows */
9331 DNPRINTF(SWM_D_INIT, "grab_windows: grab transient windows.\n");
9332 for (j = 0; j < no; j++) {
9333 gac = xcb_get_window_attributes(conn, wins[j]);
9334 gar = xcb_get_window_attributes_reply(conn, gac, NULL);
9336 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
9337 "doesn't exist.\n", wins[j]);
9341 if (gar->override_redirect) {
9342 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
9343 "override_redirect set.\n", wins[j]);
9348 state = getstate(wins[j]);
9349 manage = state != XCB_ICCCM_WM_STATE_WITHDRAWN;
9350 mapped = gar->map_state != XCB_MAP_STATE_UNMAPPED;
9351 pc = xcb_icccm_get_wm_transient_for(conn, wins[j]);
9352 if (xcb_icccm_get_wm_transient_for_reply(conn, pc,
9353 &trans, NULL) && manage)
9354 manage_window(wins[j], mapped);
9359 DNPRINTF(SWM_D_INIT, "grab_windows: done.\n");
9365 int i, j, k, num_screens;
9366 struct workspace *ws;
9367 uint32_t gcv[1], wa[1];
9368 const xcb_query_extension_reply_t *qep;
9369 xcb_screen_t *screen;
9370 xcb_randr_query_version_cookie_t c;
9371 xcb_randr_query_version_reply_t *r;
9373 num_screens = get_screen_count();
9374 if ((screens = calloc(num_screens,
9375 sizeof(struct swm_screen))) == NULL)
9376 err(1, "setup_screens: calloc: failed to allocate memory for "
9379 /* initial Xrandr setup */
9381 qep = xcb_get_extension_data(conn, &xcb_randr_id);
9383 c = xcb_randr_query_version(conn, 1, 1);
9384 r = xcb_randr_query_version_reply(conn, c, NULL);
9386 if (r->major_version >= 1) {
9388 xrandr_eventbase = qep->first_event;
9394 wa[0] = cursors[XC_LEFT_PTR].cid;
9396 /* map physical screens */
9397 for (i = 0; i < num_screens; i++) {
9398 DNPRINTF(SWM_D_WS, "setup_screens: init screen: %d\n", i);
9400 screens[i].r_focus = NULL;
9402 TAILQ_INIT(&screens[i].rl);
9403 TAILQ_INIT(&screens[i].orl);
9404 if ((screen = get_screen(i)) == NULL)
9405 errx(1, "ERROR: can't get screen %d.", i);
9406 screens[i].root = screen->root;
9408 /* set default colors */
9409 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
9410 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
9411 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
9412 setscreencolor("rgb:00/40/40", i + 1,
9413 SWM_S_COLOR_BAR_BORDER_UNFOCUS);
9414 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
9415 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
9417 /* create graphics context on screen */
9418 screens[i].bar_gc = xcb_generate_id(conn);
9420 xcb_create_gc(conn, screens[i].bar_gc, screens[i].root,
9421 XCB_GC_GRAPHICS_EXPOSURES, gcv);
9423 /* set default cursor */
9424 xcb_change_window_attributes(conn, screens[i].root,
9427 /* init all workspaces */
9428 /* XXX these should be dynamically allocated too */
9429 for (j = 0; j < SWM_WS_MAX; j++) {
9430 ws = &screens[i].ws[j];
9433 ws->bar_enabled = 1;
9435 ws->focus_prev = NULL;
9436 ws->focus_pending = NULL;
9439 TAILQ_INIT(&ws->winlist);
9440 TAILQ_INIT(&ws->unmanagedlist);
9442 for (k = 0; layouts[k].l_stack != NULL; k++)
9443 if (layouts[k].l_config != NULL)
9444 layouts[k].l_config(ws,
9445 SWM_ARG_ID_STACKINIT);
9446 ws->cur_layout = &layouts[0];
9447 ws->cur_layout->l_string(ws);
9453 xcb_randr_select_input(conn, screens[i].root,
9454 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE);
9461 if ((bar_fonts = strdup(SWM_BAR_FONTS)) == NULL)
9462 err(1, "setup_globals: strdup: failed to allocate memory.");
9464 if ((clock_format = strdup("%a %b %d %R %Z %Y")) == NULL)
9465 err(1, "setup_globals: strdup: failed to allocate memory.");
9467 if ((syms = xcb_key_symbols_alloc(conn)) == NULL)
9468 errx(1, "unable to allocate key symbols");
9470 a_state = get_atom_from_string("WM_STATE");
9471 a_prot = get_atom_from_string("WM_PROTOCOLS");
9472 a_delete = get_atom_from_string("WM_DELETE_WINDOW");
9473 a_takefocus = get_atom_from_string("WM_TAKE_FOCUS");
9474 a_utf8_string = get_atom_from_string("UTF8_STRING");
9475 a_swm_iconic = get_atom_from_string("_SWM_ICONIC");
9476 a_swm_ws = get_atom_from_string("_SWM_WS");
9483 xcb_atom_t netwmcheck;
9484 xcb_window_t root, win;
9486 /* work around sun jdk bugs, code from wmname */
9487 netwmcheck = get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
9489 num_screens = get_screen_count();
9490 for (i = 0; i < num_screens; i++) {
9491 root = screens[i].root;
9493 win = xcb_generate_id(conn);
9494 xcb_create_window(conn, XCB_COPY_FROM_PARENT, win, root,
9495 0, 0, 1, 1, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT,
9496 XCB_COPY_FROM_PARENT, 0, NULL);
9498 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root,
9499 netwmcheck, XCB_ATOM_WINDOW, 32, 1, &win);
9500 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win,
9501 netwmcheck, XCB_ATOM_WINDOW, 32, 1, &win);
9502 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win,
9503 ewmh[_NET_WM_NAME].atom, a_utf8_string, 8, strlen("LG3D"),
9509 shutdown_cleanup(void)
9513 /* disable alarm because the following code may not be interrupted */
9515 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
9516 err(1, "can't disable alarm");
9525 num_screens = get_screen_count();
9526 for (i = 0; i < num_screens; ++i) {
9527 if (screens[i].bar_gc != 0)
9528 xcb_free_gc(conn, screens[i].bar_gc);
9529 if (!bar_font_legacy)
9530 XftColorFree(display, DefaultVisual(display, i),
9531 DefaultColormap(display, i), &bar_font_color);
9534 if (bar_font_legacy)
9535 XFreeFontSet(display, bar_fs);
9537 XftFontClose(display, bar_font);
9540 xcb_key_symbols_free(syms);
9542 xcb_disconnect(conn);
9546 event_error(xcb_generic_error_t *e)
9550 DNPRINTF(SWM_D_EVENT, "event_error: %s(%u) from %s(%u), sequence: %u, "
9551 "resource_id: %u, minor_code: %u\n",
9552 xcb_event_get_error_label(e->error_code), e->error_code,
9553 xcb_event_get_request_label(e->major_code), e->major_code,
9554 e->sequence, e->resource_id, e->minor_code);
9558 event_handle(xcb_generic_event_t *evt)
9560 uint8_t type = XCB_EVENT_RESPONSE_TYPE(evt);
9562 DNPRINTF(SWM_D_EVENT, "XCB Event: %s(%d), seq %u\n",
9563 xcb_event_get_label(XCB_EVENT_RESPONSE_TYPE(evt)),
9564 XCB_EVENT_RESPONSE_TYPE(evt), evt->sequence);
9567 #define EVENT(type, callback) case type: callback((void *)evt); return
9568 EVENT(0, event_error);
9569 EVENT(XCB_BUTTON_PRESS, buttonpress);
9570 /*EVENT(XCB_BUTTON_RELEASE, buttonpress);*/
9571 /*EVENT(XCB_CIRCULATE_NOTIFY, );*/
9572 /*EVENT(XCB_CIRCULATE_REQUEST, );*/
9573 EVENT(XCB_CLIENT_MESSAGE, clientmessage);
9574 /*EVENT(XCB_COLORMAP_NOTIFY, );*/
9575 EVENT(XCB_CONFIGURE_NOTIFY, configurenotify);
9576 EVENT(XCB_CONFIGURE_REQUEST, configurerequest);
9577 /*EVENT(XCB_CREATE_NOTIFY, );*/
9578 EVENT(XCB_DESTROY_NOTIFY, destroynotify);
9579 EVENT(XCB_ENTER_NOTIFY, enternotify);
9580 EVENT(XCB_EXPOSE, expose);
9582 EVENT(XCB_FOCUS_IN, focusin);
9583 EVENT(XCB_FOCUS_OUT, focusout);
9585 /*EVENT(XCB_GRAPHICS_EXPOSURE, );*/
9586 /*EVENT(XCB_GRAVITY_NOTIFY, );*/
9587 EVENT(XCB_KEY_PRESS, keypress);
9588 /*EVENT(XCB_KEY_RELEASE, keypress);*/
9589 /*EVENT(XCB_KEYMAP_NOTIFY, );*/
9591 EVENT(XCB_LEAVE_NOTIFY, leavenotify);
9593 EVENT(XCB_MAP_NOTIFY, mapnotify);
9594 EVENT(XCB_MAP_REQUEST, maprequest);
9595 EVENT(XCB_MAPPING_NOTIFY, mappingnotify);
9596 EVENT(XCB_MOTION_NOTIFY, motionnotify);
9597 /*EVENT(XCB_NO_EXPOSURE, );*/
9598 EVENT(XCB_PROPERTY_NOTIFY, propertynotify);
9599 /*EVENT(XCB_REPARENT_NOTIFY, );*/
9600 /*EVENT(XCB_RESIZE_REQUEST, );*/
9601 /*EVENT(XCB_SELECTION_CLEAR, );*/
9602 /*EVENT(XCB_SELECTION_NOTIFY, );*/
9603 /*EVENT(XCB_SELECTION_REQUEST, );*/
9604 EVENT(XCB_UNMAP_NOTIFY, unmapnotify);
9605 /*EVENT(XCB_VISIBILITY_NOTIFY, visibilitynotify);*/
9608 if (type - xrandr_eventbase == XCB_RANDR_SCREEN_CHANGE_NOTIFY)
9609 screenchange((void *)evt);
9613 main(int argc, char *argv[])
9615 struct swm_region *r;
9616 char conf[PATH_MAX], *cfile = NULL;
9618 int xfd, i, num_screens, startup = 1;
9619 struct sigaction sact;
9620 xcb_generic_event_t *evt;
9624 int stdin_ready = 0;
9627 /* suppress unused warning since var is needed */
9630 time_started = time(NULL);
9633 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION, buildstr);
9634 if (!setlocale(LC_CTYPE, "") || !setlocale(LC_TIME, ""))
9635 warnx("no locale support");
9637 /* handle some signals */
9638 bzero(&sact, sizeof(sact));
9639 sigemptyset(&sact.sa_mask);
9641 sact.sa_handler = sighdlr;
9642 sigaction(SIGINT, &sact, NULL);
9643 sigaction(SIGQUIT, &sact, NULL);
9644 sigaction(SIGTERM, &sact, NULL);
9645 sigaction(SIGHUP, &sact, NULL);
9647 sact.sa_handler = sighdlr;
9648 sact.sa_flags = SA_NOCLDSTOP;
9649 sigaction(SIGCHLD, &sact, NULL);
9651 if (!(display = XOpenDisplay(0)))
9652 errx(1, "can not open display");
9654 conn = XGetXCBConnection(display);
9655 if (xcb_connection_has_error(conn))
9656 errx(1, "can not get XCB connection");
9658 XSetEventQueueOwner(display, XCBOwnsEventQueue);
9660 xcb_prefetch_extension_data(conn, &xcb_randr_id);
9661 xfd = xcb_get_file_descriptor(conn);
9663 /* look for local and global conf file */
9664 pwd = getpwuid(getuid());
9666 errx(1, "invalid user: %d", getuid());
9668 xcb_grab_server(conn);
9671 /* flush all events */
9672 while ((evt = xcb_poll_for_event(conn))) {
9673 if (XCB_EVENT_RESPONSE_TYPE(evt) == 0)
9678 if (enable_wm() != 0)
9679 errx(1, "another window manager is currently running");
9681 /* Load Xcursors and/or cursorfont glyph cursors. */
9693 for (i = 0; ; i++) {
9698 snprintf(conf, sizeof conf, "%s/.%s",
9699 pwd->pw_dir, SWM_CONF_FILE);
9703 snprintf(conf, sizeof conf, "/etc/%s",
9708 snprintf(conf, sizeof conf, "%s/.%s",
9709 pwd->pw_dir, SWM_CONF_FILE_OLD);
9713 snprintf(conf, sizeof conf, "/etc/%s",
9720 if (strlen(conf) && stat(conf, &sb) != -1)
9721 if (S_ISREG(sb.st_mode)) {
9728 /* load conf (if any) */
9730 conf_load(cfile, SWM_CONF_DEFAULT);
9735 /* set some values to work around bad programs */
9737 /* grab existing windows (before we build the bars) */
9740 if (getenv("SWM_STARTED") == NULL)
9741 setenv("SWM_STARTED", "YES", 1);
9743 /* setup all bars */
9744 num_screens = get_screen_count();
9745 for (i = 0; i < num_screens; i++)
9746 TAILQ_FOREACH(r, &screens[i].rl, entry)
9753 xcb_ungrab_server(conn);
9756 rd_max = xfd > STDIN_FILENO ? xfd : STDIN_FILENO;
9759 while ((evt = xcb_poll_for_event(conn))) {
9766 /* If just (re)started, set default focus if needed. */
9770 if (focus_mode != SWM_FOCUS_FOLLOW) {
9771 r = TAILQ_FIRST(&screens[0].rl);
9783 FD_SET(STDIN_FILENO, &rd);
9788 num_readable = select(rd_max + 1, &rd, NULL, NULL, &tv);
9789 if (num_readable == -1 && errno != EINTR) {
9790 DNPRINTF(SWM_D_MISC, "select failed");
9791 } else if (num_readable > 0 && FD_ISSET(STDIN_FILENO, &rd)) {
9796 restart(NULL, NULL);