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-2012 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 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 : 0)
247 #define YESNO(x) ((x) ? "yes" : "no")
249 #define SWM_FOCUS_DEFAULT (0)
250 #define SWM_FOCUS_FOLLOW (1)
251 #define SWM_FOCUS_MANUAL (2)
253 #define SWM_CK_NONE 0
254 #define SWM_CK_ALL 0xf
255 #define SWM_CK_FOCUS 0x1
256 #define SWM_CK_POINTER 0x2
257 #define SWM_CK_FALLBACK 0x4
258 #define SWM_CK_REGION 0x8
260 #define SWM_CONF_DEFAULT (0)
261 #define SWM_CONF_KEYMAPPING (1)
264 #define SWM_LIB "/usr/local/lib/libswmhack.so"
271 xcb_atom_t a_takefocus;
273 xcb_atom_t a_netwmname;
274 xcb_atom_t a_utf8_string;
276 xcb_atom_t a_swm_iconic;
278 volatile sig_atomic_t running = 1;
279 volatile sig_atomic_t restart_wm = 0;
280 xcb_timestamp_t last_event_time = 0;
284 int xrandr_eventbase;
285 unsigned int numlockmask = 0;
288 xcb_connection_t *conn;
289 xcb_key_symbols_t *syms;
292 int cycle_visible = 0;
294 int font_adjusted = 0;
295 unsigned int mod_key = MODKEY;
298 struct swm_region *search_r;
299 int select_list_pipe[2];
300 int select_resp_pipe[2];
302 volatile sig_atomic_t search_resp;
303 int search_resp_action;
305 struct search_window {
306 TAILQ_ENTRY(search_window) entry;
310 xcb_window_t indicator;
312 TAILQ_HEAD(search_winlist, search_window);
313 struct search_winlist search_wl;
318 SWM_SEARCH_UNICONIFY,
319 SWM_SEARCH_NAME_WORKSPACE,
320 SWM_SEARCH_SEARCH_WORKSPACE,
321 SWM_SEARCH_SEARCH_WINDOW
324 #define SWM_STACK_TOP (0)
325 #define SWM_STACK_BOTTOM (1)
326 #define SWM_STACK_ABOVE (2)
327 #define SWM_STACK_BELOW (3)
330 double dialog_ratio = 0.6;
332 #define SWM_BAR_MAX (256)
333 #define SWM_BAR_JUSTIFY_LEFT (0)
334 #define SWM_BAR_JUSTIFY_CENTER (1)
335 #define SWM_BAR_JUSTIFY_RIGHT (2)
336 #define SWM_BAR_OFFSET (4)
337 #define SWM_BAR_FONTS "-*-terminus-medium-*-*-*-12-*-*-*-*-*-*-*," \
338 "-*-profont-*-*-*-*-12-*-*-*-*-*-*-*," \
339 "-*-times-medium-r-*-*-12-*-*-*-*-*-*-*," \
340 "-misc-fixed-medium-r-*-*-12-*-*-*-*-*-*-*," \
341 "-*-*-*-r-*-*-*-*-*-*-*-*-*-*"
343 #ifdef X_HAVE_UTF8_STRING
344 #define DRAWSTRING(x...) Xutf8DrawString(x)
346 #define DRAWSTRING(x...) XmbDrawString(x)
349 char *bar_argv[] = { NULL, NULL };
351 char bar_ext[SWM_BAR_MAX];
352 char bar_ext_buf[SWM_BAR_MAX];
353 char bar_vertext[SWM_BAR_MAX];
356 int bar_border_width = 1;
357 int bar_at_bottom = 0;
361 int bar_justify = SWM_BAR_JUSTIFY_LEFT;
362 char *bar_format = NULL;
363 int stack_enabled = 1;
364 int clock_enabled = 1;
365 int urgent_enabled = 0;
366 char *clock_format = NULL;
367 int title_name_enabled = 0;
368 int title_class_enabled = 0;
369 int window_name_enabled = 0;
370 int focus_mode = SWM_FOCUS_DEFAULT;
371 int focus_close = SWM_STACK_BELOW;
372 int focus_close_wrap = 1;
373 int focus_default = SWM_STACK_TOP;
374 int spawn_position = SWM_STACK_TOP;
375 int disable_border = 0;
376 int border_width = 1;
377 int region_padding = 0;
379 int verbose_layout = 0;
383 XFontSetExtents *bar_fs_extents;
385 int bar_font_legacy = 1;
387 XftColor bar_font_color;
389 char *startup_exception;
390 unsigned int nr_exceptions = 0;
392 /* layout manager data */
393 struct swm_geometry {
406 struct swm_geometry g;
409 /* virtual "screens" */
411 TAILQ_ENTRY(swm_region) entry;
413 struct swm_geometry g;
414 struct workspace *ws; /* current workspace on this region */
415 struct workspace *ws_prior; /* prior workspace on this region */
416 struct swm_screen *s; /* screen idx */
419 TAILQ_HEAD(swm_region_list, swm_region);
422 TAILQ_ENTRY(ws_win) entry;
424 xcb_window_t transient;
425 struct ws_win *focus_child; /* focus on child transient */
426 struct swm_geometry g; /* current geometry */
427 struct swm_geometry g_float; /* region coordinates */
428 int g_floatvalid; /* g_float geometry validity */
429 int floatmaxed; /* whether maxed by max_stack */
435 unsigned int ewmh_flags;
436 int font_size_boundary[SWM_MAX_FONT_STEPS];
442 unsigned long quirks;
443 struct workspace *ws; /* always valid */
444 struct swm_screen *s; /* always valid, never changes */
445 xcb_get_geometry_reply_t *wa;
447 xcb_icccm_get_wm_class_reply_t ch;
448 xcb_icccm_wm_hints_t hints;
450 TAILQ_HEAD(ws_win_list, ws_win);
454 TAILQ_ENTRY(pid_e) entry;
458 TAILQ_HEAD(pid_list, pid_e);
459 struct pid_list pidlist = TAILQ_HEAD_INITIALIZER(pidlist);
461 /* layout handlers */
463 void vertical_config(struct workspace *, int);
464 void vertical_stack(struct workspace *, struct swm_geometry *);
465 void horizontal_config(struct workspace *, int);
466 void horizontal_stack(struct workspace *, struct swm_geometry *);
467 void max_stack(struct workspace *, struct swm_geometry *);
468 void plain_stacker(struct workspace *);
469 void fancy_stacker(struct workspace *);
472 void (*l_stack)(struct workspace *, struct swm_geometry *);
473 void (*l_config)(struct workspace *, int);
475 #define SWM_L_FOCUSPREV (1<<0)
476 #define SWM_L_MAPONFOCUS (1<<1)
477 void (*l_string)(struct workspace *);
479 /* stack, configure */
480 { vertical_stack, vertical_config, 0, plain_stacker },
481 { horizontal_stack, horizontal_config, 0, plain_stacker },
483 SWM_L_MAPONFOCUS | SWM_L_FOCUSPREV, plain_stacker },
484 { NULL, NULL, 0, NULL },
487 /* position of max_stack mode in the layouts array, index into layouts! */
488 #define SWM_V_STACK (0)
489 #define SWM_H_STACK (1)
490 #define SWM_MAX_STACK (2)
492 #define SWM_H_SLICE (32)
493 #define SWM_V_SLICE (32)
495 /* define work spaces */
497 int idx; /* workspace index */
498 char *name; /* workspace name */
499 int always_raise; /* raise windows on focus */
500 int bar_enabled; /* bar visibility */
501 struct layout *cur_layout; /* current layout handlers */
502 struct ws_win *focus; /* may be NULL */
503 struct ws_win *focus_prev; /* may be NULL */
504 struct ws_win *focus_pending; /* may be NULL */
505 struct swm_region *r; /* may be NULL */
506 struct swm_region *old_r; /* may be NULL */
507 struct ws_win_list winlist; /* list of windows in ws */
508 struct ws_win_list unmanagedlist; /* list of dead windows in ws */
509 char stacker[10]; /* display stacker and layout */
513 int horizontal_msize;
515 int horizontal_stacks;
526 SWM_S_COLOR_BAR_BORDER,
527 SWM_S_COLOR_BAR_BORDER_UNFOCUS,
528 SWM_S_COLOR_BAR_FONT,
534 /* physical screen mapping */
535 #define SWM_WS_MAX (22) /* hard limit */
536 int workspace_limit = 10; /* soft limit */
539 int idx; /* screen index */
540 struct swm_region_list rl; /* list of regions on this screen */
541 struct swm_region_list orl; /* list of old regions */
543 struct workspace ws[SWM_WS_MAX];
544 struct swm_region *r_focus;
550 } c[SWM_S_COLOR_MAX];
552 xcb_gcontext_t bar_gc;
555 struct swm_screen *screens;
557 /* args to functions */
560 #define SWM_ARG_ID_FOCUSNEXT (0)
561 #define SWM_ARG_ID_FOCUSPREV (1)
562 #define SWM_ARG_ID_FOCUSMAIN (2)
563 #define SWM_ARG_ID_SWAPNEXT (10)
564 #define SWM_ARG_ID_SWAPPREV (11)
565 #define SWM_ARG_ID_SWAPMAIN (12)
566 #define SWM_ARG_ID_MOVELAST (13)
567 #define SWM_ARG_ID_MASTERSHRINK (20)
568 #define SWM_ARG_ID_MASTERGROW (21)
569 #define SWM_ARG_ID_MASTERADD (22)
570 #define SWM_ARG_ID_MASTERDEL (23)
571 #define SWM_ARG_ID_FLIPLAYOUT (24)
572 #define SWM_ARG_ID_STACKRESET (30)
573 #define SWM_ARG_ID_STACKINIT (31)
574 #define SWM_ARG_ID_CYCLEWS_UP (40)
575 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
576 #define SWM_ARG_ID_CYCLERG_UP (42)
577 #define SWM_ARG_ID_CYCLERG_DOWN (43)
578 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
579 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
580 #define SWM_ARG_ID_STACKINC (50)
581 #define SWM_ARG_ID_STACKDEC (51)
582 #define SWM_ARG_ID_SS_ALL (60)
583 #define SWM_ARG_ID_SS_WINDOW (61)
584 #define SWM_ARG_ID_DONTCENTER (70)
585 #define SWM_ARG_ID_CENTER (71)
586 #define SWM_ARG_ID_KILLWINDOW (80)
587 #define SWM_ARG_ID_DELETEWINDOW (81)
588 #define SWM_ARG_ID_WIDTHGROW (90)
589 #define SWM_ARG_ID_WIDTHSHRINK (91)
590 #define SWM_ARG_ID_HEIGHTGROW (92)
591 #define SWM_ARG_ID_HEIGHTSHRINK (93)
592 #define SWM_ARG_ID_MOVEUP (100)
593 #define SWM_ARG_ID_MOVEDOWN (101)
594 #define SWM_ARG_ID_MOVELEFT (102)
595 #define SWM_ARG_ID_MOVERIGHT (103)
596 #define SWM_ARG_ID_BAR_TOGGLE (110)
597 #define SWM_ARG_ID_BAR_TOGGLE_WS (111)
603 TAILQ_ENTRY(quirk) entry;
607 #define SWM_Q_FLOAT (1<<0) /* float this window */
608 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
609 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
610 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
611 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
612 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
614 TAILQ_HEAD(quirk_list, quirk);
615 struct quirk_list quirks = TAILQ_HEAD_INITIALIZER(quirks);
618 * Supported EWMH hints should be added to
619 * both the enum and the ewmh array
624 _NET_MOVERESIZE_WINDOW,
625 _NET_WM_ACTION_ABOVE,
626 _NET_WM_ACTION_CLOSE,
627 _NET_WM_ACTION_FULLSCREEN,
629 _NET_WM_ACTION_RESIZE,
630 _NET_WM_ALLOWED_ACTIONS,
634 _NET_WM_STATE_FULLSCREEN,
635 _NET_WM_STATE_HIDDEN,
636 _NET_WM_STATE_MAXIMIZED_HORZ,
637 _NET_WM_STATE_MAXIMIZED_VERT,
638 _NET_WM_STATE_SKIP_PAGER,
639 _NET_WM_STATE_SKIP_TASKBAR,
641 _NET_WM_WINDOW_TYPE_DIALOG,
642 _NET_WM_WINDOW_TYPE_DOCK,
643 _NET_WM_WINDOW_TYPE_NORMAL,
644 _NET_WM_WINDOW_TYPE_SPLASH,
645 _NET_WM_WINDOW_TYPE_TOOLBAR,
646 _NET_WM_WINDOW_TYPE_UTILITY,
647 _SWM_WM_STATE_MANUAL,
654 } ewmh[SWM_EWMH_HINT_MAX] = {
655 /* must be in same order as in the enum */
656 {"_NET_ACTIVE_WINDOW", XCB_ATOM_NONE},
657 {"_NET_CLOSE_WINDOW", XCB_ATOM_NONE},
658 {"_NET_MOVERESIZE_WINDOW", XCB_ATOM_NONE},
659 {"_NET_WM_ACTION_ABOVE", XCB_ATOM_NONE},
660 {"_NET_WM_ACTION_CLOSE", XCB_ATOM_NONE},
661 {"_NET_WM_ACTION_FULLSCREEN", XCB_ATOM_NONE},
662 {"_NET_WM_ACTION_MOVE", XCB_ATOM_NONE},
663 {"_NET_WM_ACTION_RESIZE", XCB_ATOM_NONE},
664 {"_NET_WM_ALLOWED_ACTIONS", XCB_ATOM_NONE},
665 {"_NET_WM_NAME", XCB_ATOM_NONE},
666 {"_NET_WM_STATE", XCB_ATOM_NONE},
667 {"_NET_WM_STATE_ABOVE", XCB_ATOM_NONE},
668 {"_NET_WM_STATE_FULLSCREEN", XCB_ATOM_NONE},
669 {"_NET_WM_STATE_HIDDEN", XCB_ATOM_NONE},
670 {"_NET_WM_STATE_MAXIMIZED_HORZ", XCB_ATOM_NONE},
671 {"_NET_WM_STATE_MAXIMIZED_VERT", XCB_ATOM_NONE},
672 {"_NET_WM_STATE_SKIP_PAGER", XCB_ATOM_NONE},
673 {"_NET_WM_STATE_SKIP_TASKBAR", XCB_ATOM_NONE},
674 {"_NET_WM_WINDOW_TYPE", XCB_ATOM_NONE},
675 {"_NET_WM_WINDOW_TYPE_DIALOG", XCB_ATOM_NONE},
676 {"_NET_WM_WINDOW_TYPE_DOCK", XCB_ATOM_NONE},
677 {"_NET_WM_WINDOW_TYPE_NORMAL", XCB_ATOM_NONE},
678 {"_NET_WM_WINDOW_TYPE_SPLASH", XCB_ATOM_NONE},
679 {"_NET_WM_WINDOW_TYPE_TOOLBAR", XCB_ATOM_NONE},
680 {"_NET_WM_WINDOW_TYPE_UTILITY", XCB_ATOM_NONE},
681 {"_SWM_WM_STATE_MANUAL", XCB_ATOM_NONE},
688 XC_BOTTOM_LEFT_CORNER,
689 XC_BOTTOM_RIGHT_CORNER,
697 char *name; /* Name used by Xcursor .*/
698 uint8_t cf_char; /* cursorfont index. */
700 } cursors[XC_MAX] = {
701 {"fleur", XC_fleur, XCB_CURSOR_NONE},
702 {"left_ptr", XC_left_ptr, XCB_CURSOR_NONE},
703 {"bottom_left_corner", XC_bottom_left_corner, XCB_CURSOR_NONE},
704 {"bottom_right_corner", XC_bottom_right_corner, XCB_CURSOR_NONE},
705 {"sizing", XC_sizing, XCB_CURSOR_NONE},
706 {"top_left_corner", XC_top_left_corner, XCB_CURSOR_NONE},
707 {"top_right_corner", XC_top_right_corner, XCB_CURSOR_NONE},
710 #define SWM_SPAWN_OPTIONAL 0x1
714 TAILQ_ENTRY(spawn_prog) entry;
720 TAILQ_HEAD(spawn_list, spawn_prog);
721 struct spawn_list spawns = TAILQ_HEAD_INITIALIZER(spawns);
723 /* user/key callable function IDs */
835 KF_DUMPWINS, /* MUST BE LAST */
843 enum keyfuncid funcid;
846 RB_HEAD(key_tree, key);
848 /* function prototypes */
849 void adjust_font(struct ws_win *);
850 void bar_class_name(char *, size_t, struct swm_region *);
851 void bar_class_title_name(char *, size_t, struct swm_region *);
852 void bar_cleanup(struct swm_region *);
853 void bar_extra_setup(void);
854 void bar_extra_stop(void);
855 int bar_extra_update(void);
856 void bar_fmt(const char *, char *, struct swm_region *, size_t);
857 void bar_fmt_expand(char *, size_t);
859 void bar_print(struct swm_region *, const char *);
860 void bar_print_legacy(struct swm_region *, const char *);
861 void bar_replace(char *, char *, struct swm_region *, size_t);
862 void bar_replace_pad(char *, int *, size_t);
863 char *bar_replace_seq(char *, char *, struct swm_region *, size_t *, size_t);
864 void bar_setup(struct swm_region *);
865 void bar_title_name(char *, size_t, struct swm_region *);
866 void bar_toggle(struct swm_region *, union arg *);
867 void bar_urgent(char *, size_t);
868 void bar_window_float(char *, size_t, struct swm_region *);
869 void bar_window_name(char *, size_t, struct swm_region *);
870 void bar_workspace_name(char *, size_t, struct swm_region *);
871 void buttonpress(xcb_button_press_event_t *);
872 void check_conn(void);
873 void clear_keys(void);
874 void clientmessage(xcb_client_message_event_t *);
875 void client_msg(struct ws_win *, xcb_atom_t, xcb_timestamp_t);
876 int conf_load(const char *, int);
877 void configurenotify(xcb_configure_notify_event_t *);
878 void configurerequest(xcb_configure_request_event_t *);
879 void config_win(struct ws_win *, xcb_configure_request_event_t *);
880 void constrain_window(struct ws_win *, struct swm_region *, int);
881 int count_win(struct workspace *, int);
882 void cursors_cleanup(void);
883 void cursors_load(void);
884 void custom_region(char *);
885 void cyclerg(struct swm_region *, union arg *);
886 void cyclews(struct swm_region *, union arg *);
887 void cycle_layout(struct swm_region *, union arg *);
888 void destroynotify(xcb_destroy_notify_event_t *);
889 void dumpwins(struct swm_region *, union arg *);
891 void enternotify(xcb_enter_notify_event_t *);
892 void event_drain(uint8_t);
893 void event_error(xcb_generic_error_t *);
894 void event_handle(xcb_generic_event_t *);
895 void ewmh_autoquirk(struct ws_win *);
896 void ewmh_get_win_state(struct ws_win *);
897 int ewmh_set_win_fullscreen(struct ws_win *, int);
898 void ewmh_update_actions(struct ws_win *);
899 void ewmh_update_win_state(struct ws_win *, xcb_atom_t, long);
900 char *expand_tilde(const char *);
901 void expose(xcb_expose_event_t *);
902 void fake_keypress(struct ws_win *, xcb_keysym_t, uint16_t);
903 struct pid_e *find_pid(pid_t);
904 struct ws_win *find_unmanaged_window(xcb_window_t);
905 struct ws_win *find_window(xcb_window_t);
906 void floating_toggle(struct swm_region *, union arg *);
907 int floating_toggle_win(struct ws_win *);
908 void focus(struct swm_region *, union arg *);
910 void focusin(xcb_focus_in_event_t *);
911 void focusout(xcb_focus_out_event_t *);
913 void focus_flush(void);
914 void focus_region(struct swm_region *);
915 void focusrg(struct swm_region *, union arg *);
916 void focus_win(struct ws_win *);
917 void fontset_init(void);
918 void free_window(struct ws_win *);
919 xcb_atom_t get_atom_from_string(const char *);
921 char *get_atom_name(xcb_atom_t);
923 struct ws_win *get_focus_magic(struct ws_win *);
924 struct ws_win *get_focus_prev(struct ws_win *);
926 char *get_notify_detail_label(uint8_t);
927 char *get_notify_mode_label(uint8_t);
929 struct ws_win *get_pointer_win(xcb_window_t);
930 struct ws_win *get_region_focus(struct swm_region *);
931 int get_region_index(struct swm_region *);
932 xcb_screen_t *get_screen(int);
934 char *get_stack_mode_name(uint8_t);
936 int32_t get_swm_iconic(struct ws_win *);
937 char *get_win_name(xcb_window_t);
938 int get_ws_idx(xcb_window_t);
939 uint32_t getstate(xcb_window_t);
940 void grabbuttons(struct ws_win *);
942 void grab_windows(void);
943 void iconify(struct swm_region *, union arg *);
945 void keypress(xcb_key_press_event_t *);
946 int key_cmp(struct key *, struct key *);
947 void key_insert(unsigned int, KeySym, enum keyfuncid, const char *);
948 struct key *key_lookup(unsigned int, KeySym);
949 void key_remove(struct key *);
950 void key_replace(struct key *, unsigned int, KeySym, enum keyfuncid,
952 void kill_bar_extra_atexit(void);
953 void kill_refs(struct ws_win *);
955 void leavenotify(xcb_leave_notify_event_t *);
957 void load_float_geom(struct ws_win *, struct swm_region *);
958 struct ws_win *manage_window(xcb_window_t, uint16_t);
959 void map_window(struct ws_win *);
960 void mapnotify(xcb_map_notify_event_t *);
961 void mappingnotify(xcb_mapping_notify_event_t *);
962 void maprequest(xcb_map_request_event_t *);
963 void motionnotify(xcb_motion_notify_event_t *);
964 void move(struct ws_win *, union arg *);
965 void move_step(struct swm_region *, union arg *);
966 uint32_t name_to_pixel(const char *);
967 void name_workspace(struct swm_region *, union arg *);
968 void new_region(struct swm_screen *, int, int, int, int);
969 int parsekeys(char *, unsigned int, unsigned int *, KeySym *);
970 int parsequirks(char *, unsigned long *);
971 int parse_rgb(const char *, uint16_t *, uint16_t *, uint16_t *);
972 void pressbutton(struct swm_region *, union arg *);
973 void priorws(struct swm_region *, union arg *);
975 void print_win_geom(xcb_window_t);
977 void propertynotify(xcb_property_notify_event_t *);
978 void quirk_insert(const char *, const char *, unsigned long);
979 void quirk_remove(struct quirk *);
980 void quirk_replace(struct quirk *, const char *, const char *,
982 void quit(struct swm_region *, union arg *);
983 void raise_toggle(struct swm_region *, union arg *);
984 void resize(struct ws_win *, union arg *);
985 void resize_step(struct swm_region *, union arg *);
986 void restart(struct swm_region *, union arg *);
987 struct swm_region *root_to_region(xcb_window_t, int);
988 void screenchange(xcb_randr_screen_change_notify_event_t *);
989 void scan_xrandr(int);
990 void search_do_resp(void);
991 void search_resp_name_workspace(const char *, unsigned long);
992 void search_resp_search_window(const char *);
993 void search_resp_search_workspace(const char *);
994 void search_resp_uniconify(const char *, unsigned long);
995 void search_win(struct swm_region *, union arg *);
996 void search_win_cleanup(void);
997 void search_workspace(struct swm_region *, union arg *);
998 void send_to_rg(struct swm_region *, union arg *);
999 void send_to_ws(struct swm_region *, union arg *);
1000 void set_region(struct swm_region *);
1001 int setautorun(char *, char *, int);
1002 int setconfbinding(char *, char *, int);
1003 int setconfcolor(char *, char *, int);
1004 int setconfmodkey(char *, char *, int);
1005 int setconfquirk(char *, char *, int);
1006 int setconfregion(char *, char *, int);
1007 int setconfspawn(char *, char *, int);
1008 int setconfvalue(char *, char *, int);
1009 void setkeybinding(unsigned int, KeySym, enum keyfuncid, const char *);
1010 int setkeymapping(char *, char *, int);
1011 int setlayout(char *, char *, int);
1012 void setquirk(const char *, const char *, unsigned long);
1013 void setscreencolor(char *, int, int);
1014 void setspawn(const char *, const char *, int);
1015 void setup_ewmh(void);
1016 void setup_globals(void);
1017 void setup_keys(void);
1018 void setup_quirks(void);
1019 void setup_screens(void);
1020 void setup_spawn(void);
1021 void set_child_transient(struct ws_win *, xcb_window_t *);
1022 void set_swm_iconic(struct ws_win *, int);
1023 void set_win_state(struct ws_win *, uint16_t);
1024 void shutdown_cleanup(void);
1026 void socket_setnonblock(int);
1027 void sort_windows(struct ws_win_list *);
1028 void spawn(int, union arg *, int);
1029 void spawn_custom(struct swm_region *, union arg *, const char *);
1030 int spawn_expand(struct swm_region *, union arg *, const char *, char ***);
1031 void spawn_insert(const char *, const char *, int);
1032 void spawn_remove(struct spawn_prog *);
1033 void spawn_replace(struct spawn_prog *, const char *, const char *, int);
1034 void spawn_select(struct swm_region *, union arg *, const char *, int *);
1035 void stack_config(struct swm_region *, union arg *);
1036 void stack_floater(struct ws_win *, struct swm_region *);
1037 void stack_master(struct workspace *, struct swm_geometry *, int, int);
1038 void store_float_geom(struct ws_win *, struct swm_region *);
1039 char *strdupsafe(const char *);
1040 void swapwin(struct swm_region *, union arg *);
1041 void switchws(struct swm_region *, union arg *);
1042 void teardown_ewmh(void);
1043 void unfocus_win(struct ws_win *);
1044 void uniconify(struct swm_region *, union arg *);
1045 void unmanage_window(struct ws_win *);
1046 void unmapnotify(xcb_unmap_notify_event_t *);
1047 void unmap_all(void);
1048 void unmap_window(struct ws_win *);
1049 void updatenumlockmask(void);
1050 void update_modkey(unsigned int);
1051 void update_window(struct ws_win *);
1052 void validate_spawns(void);
1053 int validate_win(struct ws_win *);
1054 int validate_ws(struct workspace *);
1055 /*void visibilitynotify(xcb_visibility_notify_event_t *);*/
1056 void version(struct swm_region *, union arg *);
1057 pid_t window_get_pid(xcb_window_t);
1058 void wkill(struct swm_region *, union arg *);
1059 void workaround(void);
1060 void xft_init(struct swm_region *);
1061 void _add_startup_exception(const char *, va_list);
1062 void add_startup_exception(const char *, ...);
1064 RB_PROTOTYPE(key_tree, key, entry, key_cmp);
1065 RB_GENERATE(key_tree, key, entry, key_cmp);
1066 struct key_tree keys;
1071 xcb_font_t cf = XCB_NONE;
1074 for (i = 0; i < LENGTH(cursors); ++i) {
1075 /* try to load Xcursor first. */
1076 cursors[i].cid = XcursorLibraryLoadCursor(display,
1079 /* fallback to cursorfont. */
1080 if (cursors[i].cid == XCB_CURSOR_NONE) {
1081 if (cf == XCB_NONE) {
1082 cf = xcb_generate_id(conn);
1083 xcb_open_font(conn, cf, strlen("cursor"),
1087 cursors[i].cid = xcb_generate_id(conn);
1088 xcb_create_glyph_cursor(conn, cursors[i].cid, cf, cf,
1089 cursors[i].cf_char, cursors[i].cf_char + 1, 0, 0, 0,
1090 0xffff, 0xffff, 0xffff);
1096 xcb_close_font(conn, cf);
1100 cursors_cleanup(void)
1103 for (i = 0; i < LENGTH(cursors); ++i)
1104 xcb_free_cursor(conn, cursors[i].cid);
1108 expand_tilde(const char *s)
1110 struct passwd *ppwd;
1118 errx(1, "expand_tilde: NULL string.");
1121 result = strdup(sc);
1127 if ((max = sysconf(_SC_LOGIN_NAME_MAX)) == -1)
1128 errx(1, "expand_tilde: sysconf");
1130 if ((user = calloc(1, max + 1)) == NULL)
1131 errx(1, "expand_tilde: calloc");
1133 for (i = 0; s[i] != '/' && s[i] != '\0'; ++i)
1138 ppwd = strlen(user) == 0 ? getpwuid(getuid()) : getpwnam(user);
1142 result = strdup(sc);
1144 if (asprintf(&result, "%s%s", ppwd->pw_dir, s) == -1)
1148 errx(1, "expand_tilde: failed to allocate memory.");
1154 parse_rgb(const char *rgb, uint16_t *rr, uint16_t *gg, uint16_t *bb)
1156 unsigned int tmpr, tmpg, tmpb;
1158 if (sscanf(rgb, "rgb:%x/%x/%x", &tmpr, &tmpg, &tmpb) != 3)
1161 *rr = RGB_8_TO_16(tmpr);
1162 *gg = RGB_8_TO_16(tmpg);
1163 *bb = RGB_8_TO_16(tmpb);
1169 get_screen(int screen)
1171 const xcb_setup_t *r;
1172 xcb_screen_iterator_t iter;
1174 if ((r = xcb_get_setup(conn)) == NULL) {
1175 DNPRINTF(SWM_D_MISC, "get_screen: xcb_get_setup\n");
1179 iter = xcb_setup_roots_iterator(r);
1180 for (; iter.rem; --screen, xcb_screen_next(&iter))
1188 get_region_index(struct swm_region *r)
1190 struct swm_region *rr;
1196 TAILQ_FOREACH(rr, &r->s->rl, entry) {
1211 if (focus_mode == SWM_FOCUS_DEFAULT)
1212 event_drain(XCB_ENTER_NOTIFY);
1218 get_atom_from_string(const char *str)
1220 xcb_intern_atom_cookie_t c;
1221 xcb_intern_atom_reply_t *r;
1224 c = xcb_intern_atom(conn, 0, strlen(str), str);
1225 r = xcb_intern_atom_reply(conn, c, NULL);
1233 return (XCB_ATOM_NONE);
1237 set_swm_iconic(struct ws_win *win, int newv)
1244 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
1245 a_swm_iconic, XCB_ATOM_INTEGER, 32, 1, &v);
1247 xcb_delete_property(conn, win->id, a_swm_iconic);
1251 get_swm_iconic(struct ws_win *win)
1254 xcb_get_property_reply_t *pr = NULL;
1256 pr = xcb_get_property_reply(conn,
1257 xcb_get_property(conn, 0, win->id, a_swm_iconic,
1258 XCB_ATOM_INTEGER, 0, 1), NULL);
1261 if (pr->type != XCB_ATOM_INTEGER || pr->format != 32)
1263 v = *((int32_t *)xcb_get_property_value(pr));
1273 xcb_atom_t sup_list;
1274 int i, j, num_screens;
1276 sup_list = get_atom_from_string("_NET_SUPPORTED");
1278 for (i = 0; i < LENGTH(ewmh); i++)
1279 ewmh[i].atom = get_atom_from_string(ewmh[i].name);
1281 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1282 for (i = 0; i < num_screens; i++) {
1283 /* Support check window will be created by workaround(). */
1285 /* Report supported atoms */
1286 xcb_delete_property(conn, screens[i].root, sup_list);
1287 for (j = 0; j < LENGTH(ewmh); j++)
1288 xcb_change_property(conn, XCB_PROP_MODE_APPEND,
1289 screens[i].root, sup_list, XCB_ATOM_ATOM, 32, 1,
1298 xcb_atom_t sup_check, sup_list;
1300 xcb_get_property_cookie_t pc;
1301 xcb_get_property_reply_t *pr;
1303 sup_check = get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
1304 sup_list = get_atom_from_string("_NET_SUPPORTED");
1305 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1307 for (i = 0; i < num_screens; i++) {
1308 /* Get the support check window and destroy it */
1309 pc = xcb_get_property(conn, 0, screens[i].root, sup_check,
1310 XCB_ATOM_WINDOW, 0, 1);
1311 pr = xcb_get_property_reply(conn, pc, NULL);
1314 if (pr->format == sup_check) {
1315 id = *((xcb_window_t *)xcb_get_property_value(pr));
1317 xcb_destroy_window(conn, id);
1318 xcb_delete_property(conn, screens[i].root, sup_check);
1319 xcb_delete_property(conn, screens[i].root, sup_list);
1326 ewmh_autoquirk(struct ws_win *win)
1330 xcb_get_property_cookie_t c;
1331 xcb_get_property_reply_t *r;
1333 c = xcb_get_property(conn, 0, win->id,
1334 ewmh[_NET_WM_WINDOW_TYPE].atom, XCB_ATOM_ATOM, 0, UINT32_MAX);
1335 r = xcb_get_property_reply(conn, c, NULL);
1339 n = xcb_get_property_value_length(r);
1340 type = xcb_get_property_value(r);
1342 for (i = 0; i < n; i++) {
1343 if (type[i] == ewmh[_NET_WM_WINDOW_TYPE_NORMAL].atom)
1345 if (type[i] == ewmh[_NET_WM_WINDOW_TYPE_DOCK].atom ||
1346 type[i] == ewmh[_NET_WM_WINDOW_TYPE_TOOLBAR].atom ||
1347 type[i] == ewmh[_NET_WM_WINDOW_TYPE_UTILITY].atom) {
1349 win->quirks = SWM_Q_FLOAT | SWM_Q_ANYWHERE;
1352 if (type[i] == ewmh[_NET_WM_WINDOW_TYPE_SPLASH].atom ||
1353 type[i] == ewmh[_NET_WM_WINDOW_TYPE_DIALOG].atom) {
1355 win->quirks = SWM_Q_FLOAT;
1362 #define SWM_EWMH_ACTION_COUNT_MAX (6)
1363 #define EWMH_F_FULLSCREEN (1<<0)
1364 #define EWMH_F_ABOVE (1<<1)
1365 #define EWMH_F_HIDDEN (1<<2)
1366 #define EWMH_F_SKIP_PAGER (1<<3)
1367 #define EWMH_F_SKIP_TASKBAR (1<<4)
1368 #define SWM_F_MANUAL (1<<5)
1371 ewmh_set_win_fullscreen(struct ws_win *win, int fs)
1379 DNPRINTF(SWM_D_MISC, "ewmh_set_win_fullscreen: window: 0x%x, "
1380 "fullscreen %s\n", win->id, YESNO(fs));
1383 if (!win->g_floatvalid)
1384 store_float_geom(win, win->ws->r);
1386 win->g = win->ws->r->g;
1389 load_float_geom(win, win->ws->r);
1396 ewmh_update_actions(struct ws_win *win)
1398 xcb_atom_t actions[SWM_EWMH_ACTION_COUNT_MAX];
1404 actions[n++] = ewmh[_NET_WM_ACTION_CLOSE].atom;
1406 if (win->floating) {
1407 actions[n++] = ewmh[_NET_WM_ACTION_MOVE].atom;
1408 actions[n++] = ewmh[_NET_WM_ACTION_RESIZE].atom;
1409 actions[n++] = ewmh[_NET_WM_ACTION_ABOVE].atom;
1412 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
1413 ewmh[_NET_WM_ALLOWED_ACTIONS].atom, XCB_ATOM_ATOM, 32, 1, actions);
1416 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
1417 #define _NET_WM_STATE_ADD 1 /* add/set property */
1418 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
1421 ewmh_update_win_state(struct ws_win *win, xcb_atom_t state, long action)
1423 unsigned int mask = 0;
1424 unsigned int changed = 0;
1425 unsigned int orig_flags;
1430 DNPRINTF(SWM_D_PROP, "ewmh_update_win_state: window: 0x%x, state: %ld, "
1431 "action: %ld\n", win->id, (unsigned long)state, action);
1433 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
1434 mask = EWMH_F_FULLSCREEN;
1435 else if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
1436 mask = EWMH_F_ABOVE;
1437 else if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
1438 mask = SWM_F_MANUAL;
1439 else if (state == ewmh[_NET_WM_STATE_SKIP_PAGER].atom)
1440 mask = EWMH_F_SKIP_PAGER;
1441 else if (state == ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom)
1442 mask = EWMH_F_SKIP_TASKBAR;
1444 orig_flags = win->ewmh_flags;
1447 case _NET_WM_STATE_REMOVE:
1448 win->ewmh_flags &= ~mask;
1450 case _NET_WM_STATE_ADD:
1451 win->ewmh_flags |= mask;
1453 case _NET_WM_STATE_TOGGLE:
1454 win->ewmh_flags ^= mask;
1458 changed = (win->ewmh_flags & mask) ^ (orig_flags & mask) ? 1 : 0;
1460 if (state == ewmh[_NET_WM_STATE_ABOVE].atom) {
1461 if (changed && !floating_toggle_win(win))
1462 win->ewmh_flags = orig_flags; /* revert */
1463 } else if (state == ewmh[_SWM_WM_STATE_MANUAL].atom) {
1465 win->manual = (win->ewmh_flags & SWM_F_MANUAL) != 0;
1466 } else if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom) {
1467 if (changed && !ewmh_set_win_fullscreen(win,
1468 win->ewmh_flags & EWMH_F_FULLSCREEN))
1469 win->ewmh_flags = orig_flags; /* revert */
1472 xcb_delete_property(conn, win->id, ewmh[_NET_WM_STATE].atom);
1474 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
1475 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1476 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1477 &ewmh[_NET_WM_STATE_FULLSCREEN].atom);
1478 else if (win->ewmh_flags & EWMH_F_SKIP_PAGER)
1479 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1480 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1481 &ewmh[_NET_WM_STATE_SKIP_PAGER].atom);
1482 else if (win->ewmh_flags & EWMH_F_SKIP_TASKBAR)
1483 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1484 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1485 &ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom);
1486 else if (win->ewmh_flags & EWMH_F_ABOVE)
1487 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1488 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1489 &ewmh[_NET_WM_STATE_ABOVE].atom);
1490 else if (win->ewmh_flags & SWM_F_MANUAL)
1491 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1492 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1493 &ewmh[_SWM_WM_STATE_MANUAL].atom);
1497 ewmh_get_win_state(struct ws_win *win)
1500 xcb_get_property_cookie_t c;
1501 xcb_get_property_reply_t *r;
1507 win->ewmh_flags = 0;
1509 win->ewmh_flags |= EWMH_F_ABOVE;
1511 win->ewmh_flags |= SWM_F_MANUAL;
1513 c = xcb_get_property(conn, 0, win->id, ewmh[_NET_WM_STATE].atom,
1514 XCB_ATOM_ATOM, 0, UINT32_MAX);
1515 r = xcb_get_property_reply(conn, c, NULL);
1519 states = xcb_get_property_value(r);
1520 n = xcb_get_property_value_length(r);
1522 for (i = 0; i < n; i++)
1523 ewmh_update_win_state(win, states[i], _NET_WM_STATE_ADD);
1531 dumpwins(struct swm_region *r, union arg *args)
1535 xcb_get_window_attributes_cookie_t c;
1536 xcb_get_window_attributes_reply_t *wa;
1538 /* suppress unused warning since var is needed */
1541 if (r->ws == NULL) {
1542 warnx("dumpwins: invalid workspace");
1546 warnx("=== managed window list ws %02d ===", r->ws->idx);
1547 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
1548 state = getstate(win->id);
1549 c = xcb_get_window_attributes(conn, win->id);
1550 wa = xcb_get_window_attributes_reply(conn, c, NULL);
1552 warnx("window: 0x%x, map_state: %d, state: %u, "
1553 "transient: 0x%x", win->id, wa->map_state,
1554 state, win->transient);
1557 warnx("window: 0x%x, failed xcb_get_window_attributes",
1561 warnx("===== unmanaged window list =====");
1562 TAILQ_FOREACH(win, &r->ws->unmanagedlist, entry) {
1563 state = getstate(win->id);
1564 c = xcb_get_window_attributes(conn, win->id);
1565 wa = xcb_get_window_attributes_reply(conn, c, NULL);
1567 warnx("window: 0x%x, map_state: %d, state: %u, "
1568 "transient: 0x%x", win->id, wa->map_state,
1569 state, win->transient);
1572 warnx("window: 0x%x, failed xcb_get_window_attributes",
1576 warnx("=================================");
1580 dumpwins(struct swm_region *r, union arg *s)
1585 #endif /* SWM_DEBUG */
1590 int saved_errno, status;
1593 saved_errno = errno;
1597 while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) != 0) {
1602 if (errno != ECHILD)
1603 warn("sighdlr: waitpid");
1604 #endif /* SWM_DEBUG */
1607 if (pid == searchpid)
1611 if (WIFEXITED(status)) {
1612 if (WEXITSTATUS(status) != 0)
1613 warnx("sighdlr: child exit status: %d",
1614 WEXITSTATUS(status));
1616 warnx("sighdlr: child is terminated "
1618 #endif /* SWM_DEBUG */
1632 errno = saved_errno;
1638 struct pid_e *p = NULL;
1640 DNPRINTF(SWM_D_MISC, "find_pid: %d\n", pid);
1645 TAILQ_FOREACH(p, &pidlist, entry) {
1654 name_to_pixel(const char *colorname)
1656 uint32_t result = 0;
1657 char cname[32] = "#";
1658 xcb_screen_t *screen;
1659 xcb_colormap_t cmap;
1660 xcb_alloc_color_reply_t *cr;
1661 xcb_alloc_named_color_reply_t *nr;
1662 uint16_t rr, gg, bb;
1664 screen = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
1665 cmap = screen->default_colormap;
1667 /* color is in format rgb://rr/gg/bb */
1668 if (strncmp(colorname, "rgb:", 4) == 0) {
1669 if (parse_rgb(colorname, &rr, &gg, &bb) == -1)
1670 warnx("could not parse rgb %s", colorname);
1672 cr = xcb_alloc_color_reply(conn,
1673 xcb_alloc_color(conn, cmap, rr, gg, bb),
1679 warnx("color '%s' not found", colorname);
1682 nr = xcb_alloc_named_color_reply(conn,
1683 xcb_alloc_named_color(conn, cmap, strlen(colorname),
1686 strlcat(cname, colorname + 2, sizeof cname - 1);
1687 nr = xcb_alloc_named_color_reply(conn,
1688 xcb_alloc_named_color(conn, cmap, strlen(cname),
1695 warnx("color '%s' not found", colorname);
1702 setscreencolor(char *val, int i, int c)
1706 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1707 if (i > 0 && i <= num_screens) {
1708 screens[i - 1].c[c].pixel = name_to_pixel(val);
1709 free(screens[i - 1].c[c].name);
1710 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
1712 } else if (i == -1) {
1713 for (i = 0; i < num_screens; i++) {
1714 screens[i].c[c].pixel = name_to_pixel(val);
1715 free(screens[i].c[c].name);
1716 if ((screens[i].c[c].name = strdup(val)) == NULL)
1720 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1725 fancy_stacker(struct workspace *ws)
1727 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1728 if (ws->cur_layout->l_stack == vertical_stack)
1729 snprintf(ws->stacker, sizeof ws->stacker,
1730 ws->l_state.vertical_flip ? "[%d>%d]" : "[%d|%d]",
1731 ws->l_state.vertical_mwin, ws->l_state.vertical_stacks);
1732 if (ws->cur_layout->l_stack == horizontal_stack)
1733 snprintf(ws->stacker, sizeof ws->stacker,
1734 ws->l_state.horizontal_flip ? "[%dv%d]" : "[%d-%d]",
1735 ws->l_state.horizontal_mwin, ws->l_state.horizontal_stacks);
1739 plain_stacker(struct workspace *ws)
1741 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1742 if (ws->cur_layout->l_stack == vertical_stack)
1743 strlcpy(ws->stacker, ws->l_state.vertical_flip ? "[>]" : "[|]",
1744 sizeof ws->stacker);
1745 if (ws->cur_layout->l_stack == horizontal_stack)
1746 strlcpy(ws->stacker, ws->l_state.horizontal_flip ? "[v]" : "[-]",
1747 sizeof ws->stacker);
1751 custom_region(char *val)
1753 unsigned int x, y, w, h;
1754 int sidx, num_screens;
1755 xcb_screen_t *screen;
1757 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1758 if (sscanf(val, "screen[%d]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
1759 errx(1, "invalid custom region, "
1760 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1761 if (sidx < 1 || sidx > num_screens)
1762 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1766 if ((screen = get_screen(sidx)) == NULL)
1767 errx(1, "ERROR: can't get screen %d.", sidx);
1770 errx(1, "region %ux%u+%u+%u too small", w, h, x, y);
1772 if (x > screen->width_in_pixels ||
1773 y > screen->height_in_pixels ||
1774 w + x > screen->width_in_pixels ||
1775 h + y > screen->height_in_pixels) {
1776 warnx("ignoring region %ux%u+%u+%u - not within screen "
1777 "boundaries (%ux%u)", w, h, x, y,
1778 screen->width_in_pixels, screen->height_in_pixels);
1782 new_region(&screens[sidx], x, y, w, h);
1786 socket_setnonblock(int fd)
1790 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
1791 err(1, "fcntl F_GETFL");
1792 flags |= O_NONBLOCK;
1793 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
1794 err(1, "fcntl F_SETFL");
1798 bar_print_legacy(struct swm_region *r, const char *s)
1800 xcb_rectangle_t rect;
1805 XRectangle ibox, lbox;
1809 XmbTextExtents(bar_fs, s, len, &ibox, &lbox);
1811 switch (bar_justify) {
1812 case SWM_BAR_JUSTIFY_LEFT:
1815 case SWM_BAR_JUSTIFY_CENTER:
1816 x = (WIDTH(r) - lbox.width) / 2;
1818 case SWM_BAR_JUSTIFY_RIGHT:
1819 x = WIDTH(r) - lbox.width - SWM_BAR_OFFSET;
1823 if (x < SWM_BAR_OFFSET)
1828 rect.width = WIDTH(r->bar);
1829 rect.height = HEIGHT(r->bar);
1831 /* clear back buffer */
1832 gcv[0] = r->s->c[SWM_S_COLOR_BAR].pixel;
1833 xcb_change_gc(conn, r->s->bar_gc, XCB_GC_FOREGROUND, gcv);
1834 xcb_poly_fill_rectangle(conn, r->bar->buffer, r->s->bar_gc, 1, &rect);
1836 /* draw back buffer */
1837 gcvd.graphics_exposures = 0;
1838 draw = XCreateGC(display, r->bar->buffer, GCGraphicsExposures, &gcvd);
1839 XSetForeground(display, draw, r->s->c[SWM_S_COLOR_BAR_FONT].pixel);
1840 DRAWSTRING(display, r->bar->buffer, bar_fs, draw,
1841 x, (bar_fs_extents->max_logical_extent.height - lbox.height) / 2 -
1843 XFreeGC(display, draw);
1846 xcb_copy_area(conn, r->bar->buffer, r->bar->id, r->s->bar_gc, 0, 0,
1847 0, 0, WIDTH(r->bar), HEIGHT(r->bar));
1851 bar_print(struct swm_region *r, const char *s)
1854 xcb_rectangle_t rect;
1862 XftTextExtentsUtf8(display, bar_font, (FcChar8 *)s, len, &info);
1864 switch (bar_justify) {
1865 case SWM_BAR_JUSTIFY_LEFT:
1868 case SWM_BAR_JUSTIFY_CENTER:
1869 x = (WIDTH(r) - info.width) / 2;
1871 case SWM_BAR_JUSTIFY_RIGHT:
1872 x = WIDTH(r) - info.width - SWM_BAR_OFFSET;
1876 if (x < SWM_BAR_OFFSET)
1881 rect.width = WIDTH(r->bar);
1882 rect.height = HEIGHT(r->bar);
1884 /* clear back buffer */
1885 gcv[0] = r->s->c[SWM_S_COLOR_BAR].pixel;
1886 xcb_change_gc(conn, r->s->bar_gc, XCB_GC_FOREGROUND, gcv);
1887 xcb_poly_fill_rectangle(conn, r->bar->buffer, r->s->bar_gc, 1, &rect);
1889 /* draw back buffer */
1890 draw = XftDrawCreate(display, r->bar->buffer,
1891 DefaultVisual(display, r->s->idx),
1892 DefaultColormap(display, r->s->idx));
1894 XftDrawStringUtf8(draw, &bar_font_color, bar_font, x,
1895 (HEIGHT(r->bar) + bar_font->height) / 2 - bar_font->descent,
1898 XftDrawDestroy(draw);
1901 xcb_copy_area(conn, r->bar->buffer, r->bar->id, r->s->bar_gc, 0, 0,
1902 0, 0, WIDTH(r->bar), HEIGHT(r->bar));
1906 bar_extra_stop(void)
1910 bzero(bar_pipe, sizeof bar_pipe);
1913 kill(bar_pid, SIGTERM);
1916 strlcpy(bar_ext, "", sizeof bar_ext);
1921 bar_class_name(char *s, size_t sz, struct swm_region *r)
1923 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1925 if (r->ws->focus->ch.class_name != NULL)
1926 strlcat(s, r->ws->focus->ch.class_name, sz);
1930 bar_title_name(char *s, size_t sz, struct swm_region *r)
1932 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1934 if (r->ws->focus->ch.instance_name != NULL)
1935 strlcat(s, r->ws->focus->ch.instance_name, sz);
1939 bar_class_title_name(char *s, size_t sz, struct swm_region *r)
1941 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1944 bar_class_name(s, sz, r);
1945 strlcat(s, ":", sz);
1946 bar_title_name(s, sz, r);
1950 bar_window_float(char *s, size_t sz, struct swm_region *r)
1952 if (r == NULL || r ->ws == NULL || r->ws->focus == NULL)
1954 if (r->ws->focus->floating)
1955 strlcat(s, "(f)", sz);
1959 bar_window_name(char *s, size_t sz, struct swm_region *r)
1963 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1965 if ((title = get_win_name(r->ws->focus->id)) == NULL)
1968 strlcat(s, title, sz);
1972 int urgent[SWM_WS_MAX];
1974 bar_urgent(char *s, size_t sz)
1977 int i, j, num_screens;
1979 xcb_get_property_cookie_t c;
1980 xcb_icccm_wm_hints_t hints;
1982 for (i = 0; i < workspace_limit; i++)
1985 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1986 for (i = 0; i < num_screens; i++)
1987 for (j = 0; j < workspace_limit; j++)
1988 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry) {
1989 c = xcb_icccm_get_wm_hints(conn, win->id);
1990 if (xcb_icccm_get_wm_hints_reply(conn, c,
1993 if (hints.flags & XCB_ICCCM_WM_HINT_X_URGENCY)
1997 for (i = 0; i < workspace_limit; i++) {
1999 snprintf(b, sizeof b, "%d ", i + 1);
2001 snprintf(b, sizeof b, "- ");
2007 bar_workspace_name(char *s, size_t sz, struct swm_region *r)
2009 if (r == NULL || r->ws == NULL)
2011 if (r->ws->name != NULL)
2012 strlcat(s, r->ws->name, sz);
2015 /* build the default bar format according to the defined enabled options */
2017 bar_fmt(const char *fmtexp, char *fmtnew, struct swm_region *r, size_t sz)
2019 /* if format provided, just copy the buffers */
2020 if (bar_format != NULL) {
2021 strlcpy(fmtnew, fmtexp, sz);
2025 /* reset the output buffer */
2028 strlcat(fmtnew, "+N:+I ", sz);
2030 strlcat(fmtnew, "+S", sz);
2031 strlcat(fmtnew, " ", sz);
2033 /* only show the workspace name if there's actually one */
2034 if (r != NULL && r->ws != NULL && r->ws->name != NULL)
2035 strlcat(fmtnew, "<+D>", sz);
2036 strlcat(fmtnew, "+3<", sz);
2038 if (clock_enabled) {
2039 strlcat(fmtnew, fmtexp, sz);
2040 strlcat(fmtnew, "+4<", sz);
2043 /* bar_urgent already adds the space before the last asterisk */
2045 strlcat(fmtnew, "* +U*+4<", sz);
2047 if (title_class_enabled) {
2048 strlcat(fmtnew, "+C", sz);
2049 if (!title_name_enabled)
2050 strlcat(fmtnew, "+4<", sz);
2053 /* checks needed by the colon and floating strlcat(3) calls below */
2054 if (r != NULL && r->ws != NULL && r->ws->focus != NULL) {
2055 if (title_name_enabled) {
2056 if (title_class_enabled)
2057 strlcat(fmtnew, ":", sz);
2058 strlcat(fmtnew, "+T+4<", sz);
2060 if (window_name_enabled) {
2061 if (r->ws->focus->floating)
2062 strlcat(fmtnew, "+F ", sz);
2063 strlcat(fmtnew, "+64W ", sz);
2067 /* finally add the action script output and the version */
2068 strlcat(fmtnew, "+4<+A+4<+V", sz);
2072 bar_replace_pad(char *tmp, int *limit, size_t sz)
2074 /* special case; no limit given, pad one space, instead */
2075 if (*limit == (int)sz - 1)
2077 snprintf(tmp, sz, "%*s", *limit, " ");
2080 /* replaces the bar format character sequences (like in tmux(1)) */
2082 bar_replace_seq(char *fmt, char *fmtrep, struct swm_region *r, size_t *offrep,
2086 char tmp[SWM_BAR_MAX];
2090 /* reset strlcat(3) buffer */
2093 /* get number, if any */
2096 if (sscanf(fmt, "%d%n", &limit, &size) != 1)
2097 limit = sizeof tmp - 1;
2098 if (limit <= 0 || limit >= (int)sizeof tmp)
2099 limit = sizeof tmp - 1;
2101 /* there is nothing to replace (ie EOL) */
2108 bar_replace_pad(tmp, &limit, sizeof tmp);
2111 snprintf(tmp, sizeof tmp, "%s", bar_ext);
2114 bar_class_name(tmp, sizeof tmp, r);
2117 bar_workspace_name(tmp, sizeof tmp, r);
2120 bar_window_float(tmp, sizeof tmp, r);
2123 snprintf(tmp, sizeof tmp, "%d", r->ws->idx + 1);
2126 snprintf(tmp, sizeof tmp, "%d", r->s->idx + 1);
2129 bar_class_title_name(tmp, sizeof tmp, r);
2132 snprintf(tmp, sizeof tmp, "%s", r->ws->stacker);
2135 bar_title_name(tmp, sizeof tmp, r);
2138 bar_urgent(tmp, sizeof tmp);
2141 snprintf(tmp, sizeof tmp, "%s", bar_vertext);
2144 bar_window_name(tmp, sizeof tmp, r);
2147 /* unknown character sequence; copy as-is */
2148 snprintf(tmp, sizeof tmp, "+%c", *fmt);
2154 if ((int)len < limit)
2156 while (limit-- > 0) {
2157 if (*offrep >= sz - 1)
2159 fmtrep[(*offrep)++] = *ptr++;
2167 bar_replace(char *fmt, char *fmtrep, struct swm_region *r, size_t sz)
2172 while (*fmt != '\0') {
2174 /* skip ordinary characters */
2177 fmtrep[off++] = *fmt++;
2181 /* character sequence found; replace it */
2182 fmt = bar_replace_seq(fmt, fmtrep, r, &off, sz);
2191 bar_fmt_expand(char *fmtexp, size_t sz)
2198 /* start by grabbing the current time and date */
2200 localtime_r(&tmt, &tm);
2202 /* figure out what to expand */
2203 if (bar_format != NULL)
2205 else if (bar_format == NULL && clock_enabled)
2207 /* if nothing to expand bail out */
2213 /* copy as-is, just in case the format shouldn't be expanded below */
2214 strlcpy(fmtexp, fmt, sz);
2215 /* finally pass the string through strftime(3) */
2216 #ifndef SWM_DENY_CLOCK_FORMAT
2217 if ((len = strftime(fmtexp, sz, fmt, &tm)) == 0)
2218 warnx("format too long");
2223 /* Redraws the bar; need to follow with xcb_flush() or focus_flush(). */
2227 char fmtexp[SWM_BAR_MAX], fmtnew[SWM_BAR_MAX];
2228 char fmtrep[SWM_BAR_MAX];
2230 struct swm_region *r;
2232 /* expand the format by first passing it through strftime(3) */
2233 bar_fmt_expand(fmtexp, sizeof fmtexp);
2235 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2236 for (i = 0; i < num_screens; i++) {
2237 TAILQ_FOREACH(r, &screens[i].rl, entry) {
2241 if (bar_enabled && r->ws->bar_enabled)
2242 xcb_map_window(conn, r->bar->id);
2244 xcb_unmap_window(conn, r->bar->id);
2248 if (startup_exception)
2249 snprintf(fmtrep, sizeof fmtrep, "total "
2250 "exceptions: %d, first exception: %s",
2254 bar_fmt(fmtexp, fmtnew, r, sizeof fmtnew);
2255 bar_replace(fmtnew, fmtrep, r, sizeof fmtrep);
2257 if (bar_font_legacy)
2258 bar_print_legacy(r, fmtrep);
2260 bar_print(r, fmtrep);
2266 * Reads external script output; call when stdin is readable.
2267 * Returns 1 if bar_ext was updated; otherwise 0.
2270 bar_extra_update(void)
2273 char b[SWM_BAR_MAX];
2279 while (fgets(b, sizeof(b), stdin) != NULL) {
2282 if (b[len - 1] == '\n') {
2283 /* Remove newline. */
2286 /* "Clear" bar_ext. */
2289 /* Flush buffered output. */
2290 strlcpy(bar_ext, bar_ext_buf, sizeof(bar_ext));
2291 bar_ext_buf[0] = '\0';
2293 /* Append new output to bar. */
2294 strlcat(bar_ext, b, sizeof(bar_ext));
2298 /* Buffer output. */
2299 strlcat(bar_ext_buf, b, sizeof(bar_ext_buf));
2304 if (errno != EAGAIN) {
2305 warn("bar_action failed");
2314 bar_toggle(struct swm_region *r, union arg *args)
2316 struct swm_region *tmpr;
2319 /* suppress unused warnings since vars are needed */
2323 DNPRINTF(SWM_D_BAR, "bar_toggle\n");
2326 case SWM_ARG_ID_BAR_TOGGLE_WS:
2327 /* Only change if master switch is enabled. */
2329 r->ws->bar_enabled = !r->ws->bar_enabled;
2331 bar_enabled = r->ws->bar_enabled = 1;
2333 case SWM_ARG_ID_BAR_TOGGLE:
2334 bar_enabled = !bar_enabled;
2338 /* update bars as necessary */
2339 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2340 for (i = 0; i < num_screens; i++)
2341 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
2343 if (bar_enabled && tmpr->ws->bar_enabled)
2344 xcb_map_window(conn, tmpr->bar->id);
2346 xcb_unmap_window(conn, tmpr->bar->id);
2351 /* must be after stack */
2358 bar_extra_setup(void)
2360 /* do this here because the conf file is in memory */
2361 if (!bar_extra && bar_argv[0]) {
2362 /* launch external status app */
2364 if (pipe(bar_pipe) == -1)
2365 err(1, "pipe error");
2366 socket_setnonblock(bar_pipe[0]);
2367 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
2369 /* Set stdin to read from the pipe. */
2370 if (dup2(bar_pipe[0], STDIN_FILENO) == -1)
2373 /* Set stdout to write to the pipe. */
2374 if (dup2(bar_pipe[1], STDOUT_FILENO) == -1)
2377 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
2378 err(1, "could not disable SIGPIPE");
2379 switch (bar_pid = fork()) {
2381 err(1, "cannot fork");
2385 execvp(bar_argv[0], bar_argv);
2386 err(1, "%s external app failed", bar_argv[0]);
2388 default: /* parent */
2393 atexit(kill_bar_extra_atexit);
2398 kill_bar_extra_atexit(void)
2401 kill(bar_pid, SIGTERM);
2409 while ((s = index(s, '-'))) {
2414 return (count == 14);
2420 char *default_string;
2421 char **missing_charsets;
2422 int num_missing_charsets = 0;
2426 XFreeFontSet(display, bar_fs);
2430 DNPRINTF(SWM_D_INIT, "fontset_init: loading bar_fonts: %s\n", bar_fonts);
2432 bar_fs = XCreateFontSet(display, bar_fonts, &missing_charsets,
2433 &num_missing_charsets, &default_string);
2435 if (num_missing_charsets > 0) {
2436 warnx("Unable to load charset(s):");
2438 for (i = 0; i < num_missing_charsets; ++i)
2439 warnx("%s", missing_charsets[i]);
2441 XFreeStringList(missing_charsets);
2443 if (strcmp(default_string, ""))
2444 warnx("Glyphs from those sets will be replaced "
2445 "by '%s'.", default_string);
2447 warnx("Glyphs from those sets won't be drawn.");
2451 errx(1, "Error creating font set structure.");
2453 bar_fs_extents = XExtentsOfFontSet(bar_fs);
2455 bar_height = bar_fs_extents->max_logical_extent.height +
2456 2 * bar_border_width;
2463 xft_init(struct swm_region *r)
2465 char *font, *d, *search;
2468 if (bar_font == NULL) {
2469 if ((d = strdup(bar_fonts)) == NULL)
2470 errx(1, "insufficient memory.");
2472 while ((font = strsep(&search, ",")) != NULL) {
2476 DNPRINTF(SWM_D_INIT, "xft_init: try font %s\n", font);
2479 bar_font = XftFontOpenXlfd(display, r->s->idx,
2482 bar_font = XftFontOpenName(display, r->s->idx,
2487 warnx("unable to load font %s", font);
2490 DNPRINTF(SWM_D_INIT, "successfully opened "
2498 if (bar_font == NULL)
2499 errx(1, "unable to open a font");
2501 PIXEL_TO_XRENDERCOLOR(r->s->c[SWM_S_COLOR_BAR_FONT].pixel, color);
2503 if (!XftColorAllocValue(display, DefaultVisual(display, r->s->idx),
2504 DefaultColormap(display, r->s->idx), &color, &bar_font_color))
2505 warn("Xft error: unable to allocate color.");
2507 bar_height = bar_font->height + 2 * bar_border_width;
2514 bar_setup(struct swm_region *r)
2516 xcb_screen_t *screen;
2519 DNPRINTF(SWM_D_BAR, "bar_setup: screen %d.\n",
2522 if ((screen = get_screen(r->s->idx)) == NULL)
2523 errx(1, "ERROR: can't get screen %d.", r->s->idx);
2528 if ((r->bar = calloc(1, sizeof(struct swm_bar))) == NULL)
2529 err(1, "bar_setup: calloc: failed to allocate memory.");
2531 if (bar_font_legacy)
2537 Y(r->bar) = bar_at_bottom ? (Y(r) + HEIGHT(r) - bar_height) : Y(r);
2538 WIDTH(r->bar) = WIDTH(r) - 2 * bar_border_width;
2539 HEIGHT(r->bar) = bar_height - 2 * bar_border_width;
2541 /* Assume region is unfocused when we create the bar. */
2542 r->bar->id = xcb_generate_id(conn);
2543 wa[0] = r->s->c[SWM_S_COLOR_BAR].pixel;
2544 wa[1] = r->s->c[SWM_S_COLOR_BAR_BORDER_UNFOCUS].pixel;
2545 wa[2] = XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_POINTER_MOTION |
2546 XCB_EVENT_MASK_POINTER_MOTION_HINT;
2548 xcb_create_window(conn, XCB_COPY_FROM_PARENT, r->bar->id, r->s->root,
2549 X(r->bar), Y(r->bar), WIDTH(r->bar), HEIGHT(r->bar),
2550 bar_border_width, XCB_WINDOW_CLASS_INPUT_OUTPUT,
2551 XCB_COPY_FROM_PARENT, XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL
2552 | XCB_CW_EVENT_MASK, wa);
2554 r->bar->buffer = xcb_generate_id(conn);
2555 xcb_create_pixmap(conn, screen->root_depth, r->bar->buffer, r->bar->id,
2556 WIDTH(r->bar), HEIGHT(r->bar));
2559 xcb_randr_select_input(conn, r->bar->id,
2560 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE);
2563 xcb_map_window(conn, r->bar->id);
2565 DNPRINTF(SWM_D_BAR, "bar_setup: window: 0x%x, (x,y) w x h: (%d,%d) "
2566 "%d x %d\n", WINID(r->bar), X(r->bar), Y(r->bar), WIDTH(r->bar),
2573 bar_cleanup(struct swm_region *r)
2577 xcb_destroy_window(conn, r->bar->id);
2578 xcb_free_pixmap(conn, r->bar->buffer);
2584 set_win_state(struct ws_win *win, uint16_t state)
2586 uint16_t data[2] = { state, XCB_ATOM_NONE };
2588 DNPRINTF(SWM_D_EVENT, "set_win_state: window: 0x%x, state: %u\n",
2594 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id, a_state,
2595 a_state, 32, 2, data);
2599 getstate(xcb_window_t w)
2601 uint32_t result = 0;
2602 xcb_get_property_cookie_t c;
2603 xcb_get_property_reply_t *r;
2605 c = xcb_get_property(conn, 0, w, a_state, a_state, 0L, 2L);
2606 r = xcb_get_property_reply(conn, c, NULL);
2609 if (r->type == a_state && r->format == 32 && r->length == 2)
2610 result = *((uint32_t *)xcb_get_property_value(r));
2614 DNPRINTF(SWM_D_MISC, "getstate property: win 0x%x state %u\n", w,
2620 version(struct swm_region *r, union arg *args)
2622 /* suppress unused warnings since vars are needed */
2626 bar_version = !bar_version;
2628 snprintf(bar_vertext, sizeof bar_vertext,
2629 "Version: %s Build: %s", SPECTRWM_VERSION, buildstr);
2631 strlcpy(bar_vertext, "", sizeof bar_vertext);
2638 client_msg(struct ws_win *win, xcb_atom_t a, xcb_timestamp_t t)
2640 xcb_client_message_event_t ev;
2648 name = get_atom_name(a);
2649 DNPRINTF(SWM_D_EVENT, "client_msg: window: 0x%x, atom: %s(%u), "
2651 win->id, name, a, t);
2655 bzero(&ev, sizeof ev);
2656 ev.response_type = XCB_CLIENT_MESSAGE;
2657 ev.window = win->id;
2660 ev.data.data32[0] = a;
2661 ev.data.data32[1] = t;
2663 xcb_send_event(conn, 0, win->id,
2664 XCB_EVENT_MASK_NO_EVENT, (const char *)&ev);
2667 /* synthetic response to a ConfigureRequest when not making a change */
2669 config_win(struct ws_win *win, xcb_configure_request_event_t *ev)
2671 xcb_configure_notify_event_t ce;
2676 /* send notification of unchanged state. */
2677 bzero(&ce, sizeof(ce));
2678 ce.response_type = XCB_CONFIGURE_NOTIFY;
2681 ce.width = WIDTH(win);
2682 ce.height = HEIGHT(win);
2683 ce.override_redirect = 0;
2688 ce.window = win->id;
2689 ce.border_width = BORDER(win);
2690 ce.above_sibling = XCB_WINDOW_NONE;
2693 ce.event = ev->window;
2694 ce.window = ev->window;
2696 /* make response appear more WM_SIZE_HINTS-compliant */
2697 if (win->sh.flags) {
2698 DNPRINTF(SWM_D_MISC, "config_win: hints: window: 0x%x,"
2699 " sh.flags: %u, min: %d x %d, max: %d x %d, inc: "
2700 "%d x %d\n", win->id, win->sh.flags, SH_MIN_W(win),
2701 SH_MIN_H(win), SH_MAX_W(win), SH_MAX_H(win),
2702 SH_INC_W(win), SH_INC_H(win));
2707 /* the hint may be set... to 0! */
2708 if (SH_MIN_W(win) > 0 && ce.width < SH_MIN_W(win))
2709 ce.width = SH_MIN_W(win);
2710 if (SH_MIN_H(win) > 0 && ce.height < SH_MIN_H(win))
2711 ce.height = SH_MIN_H(win);
2716 /* may also be advertized as 0 */
2717 if (SH_MAX_W(win) > 0 && ce.width > SH_MAX_W(win))
2718 ce.width = SH_MAX_W(win);
2719 if (SH_MAX_H(win) > 0 && ce.height > SH_MAX_H(win))
2720 ce.height = SH_MAX_H(win);
2723 /* resize increment. */
2725 if (SH_INC_W(win) > 1 && ce.width > SH_INC_W(win))
2726 ce.width -= (ce.width - SH_MIN_W(win)) %
2728 if (SH_INC_H(win) > 1 && ce.height > SH_INC_H(win))
2729 ce.height -= (ce.height - SH_MIN_H(win)) %
2733 /* adjust x and y for requested border_width. */
2734 ce.x += BORDER(win) - ev->border_width;
2735 ce.y += BORDER(win) - ev->border_width;
2736 ce.border_width = ev->border_width;
2737 ce.above_sibling = ev->sibling;
2740 DNPRINTF(SWM_D_MISC, "config_win: ewmh: %s, window: 0x%x, (x,y) w x h: "
2741 "(%d,%d) %d x %d, border: %d\n", YESNO(ev == NULL), win->id, ce.x,
2742 ce.y, ce.width, ce.height, ce.border_width);
2744 xcb_send_event(conn, 0, win->id, XCB_EVENT_MASK_STRUCTURE_NOTIFY,
2749 count_win(struct workspace *ws, int count_transient)
2754 TAILQ_FOREACH(win, &ws->winlist, entry) {
2755 if (!count_transient && win->floating)
2757 if (!count_transient && win->transient)
2763 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
2769 quit(struct swm_region *r, union arg *args)
2771 /* suppress unused warnings since vars are needed */
2775 DNPRINTF(SWM_D_MISC, "quit\n");
2780 map_window(struct ws_win *win)
2782 uint32_t val = XCB_STACK_MODE_ABOVE;
2787 DNPRINTF(SWM_D_EVENT, "map_window: win 0x%x, mapped: %s\n", win->id,
2788 YESNO(win->mapped));
2790 xcb_configure_window(conn, win->id,
2791 XCB_CONFIG_WINDOW_STACK_MODE, &val);
2796 xcb_map_window(conn, win->id);
2797 set_win_state(win, XCB_ICCCM_WM_STATE_NORMAL);
2802 unmap_window(struct ws_win *win)
2807 DNPRINTF(SWM_D_EVENT, "unmap_window: win 0x%x, mapped: %s\n", win->id,
2808 YESNO(win->mapped));
2813 xcb_unmap_window(conn, win->id);
2814 set_win_state(win, XCB_ICCCM_WM_STATE_ICONIC);
2822 int i, j, num_screens;
2824 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2825 for (i = 0; i < num_screens; i++)
2826 for (j = 0; j < workspace_limit; j++)
2827 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2832 fake_keypress(struct ws_win *win, xcb_keysym_t keysym, uint16_t modifiers)
2834 xcb_key_press_event_t event;
2835 xcb_keycode_t *keycode;
2840 keycode = xcb_key_symbols_get_keycode(syms, keysym);
2842 DNPRINTF(SWM_D_MISC, "fake_keypress: win 0x%x keycode %u\n",
2845 bzero(&event, sizeof(event));
2846 event.event = win->id;
2847 event.root = win->s->root;
2848 event.child = XCB_WINDOW_NONE;
2849 event.time = XCB_CURRENT_TIME;
2850 event.event_x = X(win);
2851 event.event_y = Y(win);
2854 event.same_screen = 1;
2855 event.detail = *keycode;
2856 event.state = modifiers;
2858 event.response_type = XCB_KEY_PRESS;
2859 xcb_send_event(conn, 1, win->id,
2860 XCB_EVENT_MASK_KEY_PRESS, (const char *)&event);
2862 event.response_type = XCB_KEY_RELEASE;
2863 xcb_send_event(conn, 1, win->id,
2864 XCB_EVENT_MASK_KEY_RELEASE, (const char *)&event);
2870 restart(struct swm_region *r, union arg *args)
2872 /* suppress unused warning since var is needed */
2876 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
2880 execvp(start_argv[0], start_argv);
2881 warn("execvp failed");
2886 get_pointer_win(xcb_window_t root)
2888 struct ws_win *win = NULL;
2889 xcb_query_pointer_reply_t *r;
2891 DNPRINTF(SWM_D_EVENT, "get_pointer_win: root: 0x%x.\n", root);
2893 r = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, root), NULL);
2895 win = find_window(r->child);
2897 DNPRINTF(SWM_D_EVENT, "get_pointer_win: 0x%x.\n",
2900 DNPRINTF(SWM_D_EVENT, "get_pointer_win: none.\n");
2909 root_to_region(xcb_window_t root, int check)
2912 struct swm_region *r = NULL;
2914 xcb_query_pointer_reply_t *qpr;
2915 xcb_get_input_focus_reply_t *gifr;
2917 DNPRINTF(SWM_D_MISC, "root_to_region: window: 0x%x\n", root);
2919 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2920 for (i = 0; i < num_screens; i++)
2921 if (screens[i].root == root)
2924 if (check & SWM_CK_REGION)
2925 r = screens[i].r_focus;
2927 if (r == NULL && check & SWM_CK_FOCUS) {
2928 /* Try to find an actively focused window */
2929 gifr = xcb_get_input_focus_reply(conn,
2930 xcb_get_input_focus(conn), NULL);
2932 cfw = find_window(gifr->focus);
2933 if (cfw && cfw->ws->r)
2940 if (r == NULL && check & SWM_CK_POINTER) {
2941 /* No region with an active focus; try to use pointer. */
2942 qpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn,
2943 screens[i].root), NULL);
2946 DNPRINTF(SWM_D_MISC, "root_to_region: pointer: "
2947 "(%d,%d)\n", qpr->root_x, qpr->root_y);
2948 TAILQ_FOREACH(r, &screens[i].rl, entry)
2949 if (X(r) <= qpr->root_x &&
2950 qpr->root_x < MAX_X(r) &&
2951 Y(r) <= qpr->root_y &&
2952 qpr->root_y < MAX_Y(r))
2959 if (r == NULL && check & SWM_CK_FALLBACK)
2960 r = TAILQ_FIRST(&screens[i].rl);
2966 find_unmanaged_window(xcb_window_t id)
2969 int i, j, num_screens;
2971 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2972 for (i = 0; i < num_screens; i++)
2973 for (j = 0; j < workspace_limit; j++)
2974 TAILQ_FOREACH(win, &screens[i].ws[j].unmanagedlist,
2982 find_window(xcb_window_t id)
2985 int i, j, num_screens;
2986 xcb_query_tree_reply_t *r;
2988 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2989 for (i = 0; i < num_screens; i++)
2990 for (j = 0; j < workspace_limit; j++)
2991 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2995 r = xcb_query_tree_reply(conn, xcb_query_tree(conn, id), NULL);
2999 /* if we were looking for the parent return that window instead */
3000 if (r->parent == 0 || r->root == r->parent) {
3005 /* look for parent */
3006 for (i = 0; i < num_screens; i++)
3007 for (j = 0; j < workspace_limit; j++)
3008 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
3009 if (r->parent == win->id) {
3019 spawn(int ws_idx, union arg *args, int close_fd)
3024 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
3026 close(xcb_get_file_descriptor(conn));
3028 setenv("LD_PRELOAD", SWM_LIB, 1);
3030 if (asprintf(&ret, "%d", ws_idx) == -1) {
3031 warn("spawn: asprintf SWM_WS");
3034 setenv("_SWM_WS", ret, 1);
3038 if (asprintf(&ret, "%d", getpid()) == -1) {
3039 warn("spawn: asprintf _SWM_PID");
3042 setenv("_SWM_PID", ret, 1);
3046 if (setsid() == -1) {
3047 warn("spawn: setsid");
3053 * close stdin and stdout to prevent interaction between apps
3054 * and the baraction script
3055 * leave stderr open to record errors
3057 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) == -1) {
3058 warn("spawn: open");
3061 dup2(fd, STDIN_FILENO);
3062 dup2(fd, STDOUT_FILENO);
3067 execvp(args->argv[0], args->argv);
3069 warn("spawn: execvp");
3074 kill_refs(struct ws_win *win)
3076 int i, x, num_screens;
3077 struct swm_region *r;
3078 struct workspace *ws;
3083 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
3084 for (i = 0; i < num_screens; i++)
3085 TAILQ_FOREACH(r, &screens[i].rl, entry)
3086 for (x = 0; x < workspace_limit; x++) {
3088 if (win == ws->focus)
3090 if (win == ws->focus_prev)
3091 ws->focus_prev = NULL;
3096 validate_win(struct ws_win *testwin)
3099 struct workspace *ws;
3100 struct swm_region *r;
3101 int i, x, num_screens;
3103 if (testwin == NULL)
3106 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
3107 for (i = 0; i < num_screens; i++)
3108 TAILQ_FOREACH(r, &screens[i].rl, entry)
3109 for (x = 0; x < workspace_limit; x++) {
3111 TAILQ_FOREACH(win, &ws->winlist, entry)
3119 validate_ws(struct workspace *testws)
3121 struct swm_region *r;
3122 struct workspace *ws;
3123 int i, x, num_screens;
3125 /* validate all ws */
3126 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
3127 for (i = 0; i < num_screens; i++)
3128 TAILQ_FOREACH(r, &screens[i].rl, entry)
3129 for (x = 0; x < workspace_limit; x++) {
3138 unfocus_win(struct ws_win *win)
3140 xcb_window_t none = XCB_WINDOW_NONE;
3142 DNPRINTF(SWM_D_FOCUS, "unfocus_win: window: 0x%x\n", WINID(win));
3146 if (win->ws == NULL) {
3147 DNPRINTF(SWM_D_FOCUS, "unfocus_win: NULL ws.\n");
3151 if (validate_ws(win->ws)) {
3152 DNPRINTF(SWM_D_FOCUS, "unfocus_win: invalid ws.\n");
3156 if (win->ws->r == NULL) {
3157 DNPRINTF(SWM_D_FOCUS, "unfocus_win: NULL region.\n");
3161 if (validate_win(win)) {
3162 DNPRINTF(SWM_D_FOCUS, "unfocus_win: invalid win.\n");
3167 if (win->ws->focus == win) {
3168 win->ws->focus = NULL;
3169 win->ws->focus_prev = win;
3172 if (validate_win(win->ws->focus)) {
3173 kill_refs(win->ws->focus);
3174 win->ws->focus = NULL;
3177 if (validate_win(win->ws->focus_prev)) {
3178 kill_refs(win->ws->focus_prev);
3179 win->ws->focus_prev = NULL;
3182 xcb_change_window_attributes(conn, win->id, XCB_CW_BORDER_PIXEL,
3183 &win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].pixel);
3185 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
3186 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1, &none);
3188 DNPRINTF(SWM_D_FOCUS, "unfocus_win: done.\n");
3192 focus_win(struct ws_win *win)
3194 struct ws_win *cfw = NULL, *parent = NULL, *w;
3195 struct workspace *ws;
3196 xcb_get_input_focus_reply_t *r;
3198 DNPRINTF(SWM_D_FOCUS, "focus_win: window: 0x%x\n", WINID(win));
3203 if (win->ws == NULL)
3211 if (validate_ws(ws))
3214 if (validate_win(win)) {
3219 r = xcb_get_input_focus_reply(conn, xcb_get_input_focus(conn), NULL);
3221 cfw = find_window(r->focus);
3227 if (ws->focus != win) {
3228 if (ws->focus && ws->focus != cfw)
3229 unfocus_win(ws->focus);
3233 /* If this window directs focus to a child window, then clear. */
3234 if (win->focus_child)
3235 win->focus_child = NULL;
3237 /* If transient, adjust parent's focus child for focus_magic. */
3238 if (win->transient) {
3239 parent = find_window(win->transient);
3240 if (parent && parent->focus_child != win)
3241 parent->focus_child = win;
3244 if (cfw != win && ws->r != NULL) {
3245 /* Set input focus if no input hint, or indicated by hint. */
3246 if (!(win->hints.flags & XCB_ICCCM_WM_HINT_INPUT) ||
3247 (win->hints.flags & XCB_ICCCM_WM_HINT_INPUT &&
3249 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT,
3250 win->id, last_event_time);
3252 /* Tell app it can adjust focus to a specific window. */
3253 if (win->take_focus) {
3254 /* java is special; always tell parent */
3255 if (win->transient && win->java)
3256 client_msg(parent, a_takefocus,
3259 client_msg(win, a_takefocus, last_event_time);
3262 xcb_change_window_attributes(conn, win->id, XCB_CW_BORDER_PIXEL,
3263 &ws->r->s->c[SWM_S_COLOR_FOCUS].pixel);
3265 if (ws->cur_layout->flags & SWM_L_MAPONFOCUS ||
3267 /* If a parent exists, map it first. */
3271 /* Map siblings next. */
3272 TAILQ_FOREACH(w, &ws->winlist, entry)
3273 if (w != win && !w->iconic &&
3274 w->transient == parent->id)
3278 /* Map focused window. */
3281 /* Finally, map children of focus window. */
3282 TAILQ_FOREACH(w, &ws->winlist, entry)
3283 if (w->transient == win->id && !w->iconic)
3289 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
3290 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1,
3297 DNPRINTF(SWM_D_FOCUS, "focus_win: done.\n");
3300 /* If a child window should have focus instead, return it. */
3302 get_focus_magic(struct ws_win *win)
3304 struct ws_win *parent = NULL;
3305 struct ws_win *child = NULL;
3307 DNPRINTF(SWM_D_FOCUS, "get_focus_magic: window: 0x%x\n", WINID(win));
3311 if (win->transient) {
3312 parent = find_window(win->transient);
3314 /* If parent prefers focus elsewhere, then try to do so. */
3315 if (parent && (child = parent->focus_child)) {
3316 if (validate_win(child) == 0 && child->mapped)
3319 parent->focus_child = NULL;
3323 /* If this window prefers focus elsewhere, then try to do so. */
3324 if ((child = win->focus_child)) {
3325 if (validate_win(child) == 0 && child->mapped)
3328 win->focus_child = NULL;
3335 event_drain(uint8_t rt)
3337 xcb_generic_event_t *evt;
3339 /* ensure all pending requests have been processed before filtering. */
3341 while ((evt = xcb_poll_for_event(conn))) {
3342 if (XCB_EVENT_RESPONSE_TYPE(evt) != rt)
3350 set_region(struct swm_region *r)
3352 struct swm_region *rf;
3358 /* Unfocus old region bar. */
3363 xcb_change_window_attributes(conn, rf->bar->id,
3364 XCB_CW_BORDER_PIXEL,
3365 &r->s->c[SWM_S_COLOR_BAR_BORDER_UNFOCUS].pixel);
3368 /* Set region bar border to focus_color. */
3369 xcb_change_window_attributes(conn, r->bar->id,
3370 XCB_CW_BORDER_PIXEL, &r->s->c[SWM_S_COLOR_BAR_BORDER].pixel);
3376 focus_region(struct swm_region *r)
3379 struct swm_region *old_r;
3384 old_r = r->s->r_focus;
3387 nfw = get_region_focus(r);
3391 /* New region is empty; need to manually unfocus win. */
3393 unfocus_win(old_r->ws->focus);
3395 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT, r->s->root,
3398 /* Clear bar since empty. */
3404 switchws(struct swm_region *r, union arg *args)
3406 int wsid = args->id, unmap_old = 0;
3407 struct swm_region *this_r, *other_r;
3409 struct workspace *new_ws, *old_ws;
3414 if (wsid >= workspace_limit)
3418 old_ws = this_r->ws;
3419 new_ws = &this_r->s->ws[wsid];
3421 DNPRINTF(SWM_D_WS, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
3422 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), old_ws->idx, wsid);
3424 if (new_ws == NULL || old_ws == NULL)
3426 if (new_ws == old_ws)
3429 unfocus_win(old_ws->focus);
3431 other_r = new_ws->r;
3432 if (other_r == NULL) {
3433 /* the other workspace is hidden, hide this one */
3437 /* the other ws is visible in another region, exchange them */
3438 other_r->ws_prior = new_ws;
3439 other_r->ws = old_ws;
3440 old_ws->r = other_r;
3442 this_r->ws_prior = old_ws;
3443 this_r->ws = new_ws;
3446 /* Set focus_pending before stacking. */
3447 if (focus_mode != SWM_FOCUS_FOLLOW)
3448 new_ws->focus_pending = get_region_focus(new_ws->r);
3452 /* unmap old windows */
3454 TAILQ_FOREACH(win, &old_ws->winlist, entry)
3457 /* if workspaces were swapped, then don't wait to set focus */
3458 if (old_ws->r && focus_mode != SWM_FOCUS_FOLLOW) {
3459 if (new_ws->focus_pending) {
3460 focus_win(new_ws->focus_pending);
3462 /* Empty region, focus on root. */
3463 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT,
3464 new_ws->r->s[new_ws->r->s->idx].root,
3469 /* Clear bar if new ws is empty. */
3470 if (new_ws->focus_pending == NULL)
3475 DNPRINTF(SWM_D_WS, "switchws: done.\n");
3479 cyclews(struct swm_region *r, union arg *args)
3482 struct swm_screen *s = r->s;
3485 DNPRINTF(SWM_D_WS, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3486 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
3491 case SWM_ARG_ID_CYCLEWS_UP_ALL:
3494 case SWM_ARG_ID_CYCLEWS_UP:
3495 if (a.id < workspace_limit - 1)
3500 case SWM_ARG_ID_CYCLEWS_DOWN_ALL:
3503 case SWM_ARG_ID_CYCLEWS_DOWN:
3507 a.id = workspace_limit - 1;
3514 (!cycle_empty && TAILQ_EMPTY(&s->ws[a.id].winlist)))
3516 if (!cycle_visible && s->ws[a.id].r != NULL)
3520 } while (a.id != r->ws->idx);
3524 priorws(struct swm_region *r, union arg *args)
3530 DNPRINTF(SWM_D_WS, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3531 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
3533 if (r->ws_prior == NULL)
3536 a.id = r->ws_prior->idx;
3541 focusrg(struct swm_region *r, union arg *args)
3543 int ridx = args->id, i, num_screens;
3544 struct swm_region *rr = NULL;
3546 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
3547 /* do nothing if we don't have more than one screen */
3548 if (!(num_screens > 1 || outputs > 1))
3551 DNPRINTF(SWM_D_FOCUS, "focusrg: id: %d\n", ridx);
3553 rr = TAILQ_FIRST(&r->s->rl);
3554 for (i = 0; i < ridx; ++i)
3555 rr = TAILQ_NEXT(rr, entry);
3565 cyclerg(struct swm_region *r, union arg *args)
3567 struct swm_region *rr = NULL;
3570 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
3571 /* do nothing if we don't have more than one screen */
3572 if (!(num_screens > 1 || outputs > 1))
3577 case SWM_ARG_ID_CYCLERG_UP:
3578 rr = TAILQ_NEXT(r, entry);
3580 rr = TAILQ_FIRST(&screens[i].rl);
3582 case SWM_ARG_ID_CYCLERG_DOWN:
3583 rr = TAILQ_PREV(r, swm_region_list, entry);
3585 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
3598 sort_windows(struct ws_win_list *wl)
3600 struct ws_win *win, *parent, *nxt;
3605 for (win = TAILQ_FIRST(wl); win != TAILQ_END(wl); win = nxt) {
3606 nxt = TAILQ_NEXT(win, entry);
3607 if (win->transient) {
3608 parent = find_window(win->transient);
3609 if (parent == NULL) {
3610 warnx("not possible bug");
3613 TAILQ_REMOVE(wl, win, entry);
3614 TAILQ_INSERT_AFTER(wl, parent, win, entry);
3621 swapwin(struct swm_region *r, union arg *args)
3623 struct ws_win *target, *source;
3624 struct ws_win *cur_focus;
3625 struct ws_win_list *wl;
3627 DNPRINTF(SWM_D_WS, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3628 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
3630 cur_focus = r->ws->focus;
3631 if (cur_focus == NULL)
3635 wl = &source->ws->winlist;
3638 case SWM_ARG_ID_SWAPPREV:
3639 if (source->transient)
3640 source = find_window(source->transient);
3641 target = TAILQ_PREV(source, ws_win_list, entry);
3642 if (target && target->transient)
3643 target = find_window(target->transient);
3644 TAILQ_REMOVE(wl, source, entry);
3646 TAILQ_INSERT_TAIL(wl, source, entry);
3648 TAILQ_INSERT_BEFORE(target, source, entry);
3650 case SWM_ARG_ID_SWAPNEXT:
3651 target = TAILQ_NEXT(source, entry);
3652 /* move the parent and let the sort handle the move */
3653 if (source->transient)
3654 source = find_window(source->transient);
3655 TAILQ_REMOVE(wl, source, entry);
3657 TAILQ_INSERT_HEAD(wl, source, entry);
3659 TAILQ_INSERT_AFTER(wl, target, source, entry);
3661 case SWM_ARG_ID_SWAPMAIN:
3662 target = TAILQ_FIRST(wl);
3663 if (target == source) {
3664 if (source->ws->focus_prev != NULL &&
3665 source->ws->focus_prev != target)
3666 source = source->ws->focus_prev;
3670 if (target == NULL || source == NULL)
3672 source->ws->focus_prev = target;
3673 TAILQ_REMOVE(wl, target, entry);
3674 TAILQ_INSERT_BEFORE(source, target, entry);
3675 TAILQ_REMOVE(wl, source, entry);
3676 TAILQ_INSERT_HEAD(wl, source, entry);
3678 case SWM_ARG_ID_MOVELAST:
3679 TAILQ_REMOVE(wl, source, entry);
3680 TAILQ_INSERT_TAIL(wl, source, entry);
3683 DNPRINTF(SWM_D_MOVE, "swapwin: invalid id: %d\n", args->id);
3695 get_focus_prev(struct ws_win *win)
3697 struct ws_win *winfocus = NULL;
3698 struct ws_win *cur_focus = NULL;
3699 struct ws_win_list *wl = NULL;
3700 struct workspace *ws = NULL;
3702 if (!(win && win->ws))
3707 cur_focus = ws->focus;
3709 DNPRINTF(SWM_D_FOCUS, "get_focus_prev: window: 0x%x, cur_focus: 0x%x\n",
3710 WINID(win), WINID(cur_focus));
3712 /* pickle, just focus on whatever */
3713 if (cur_focus == NULL) {
3714 /* use prev_focus if valid */
3715 if (ws->focus_prev && ws->focus_prev != cur_focus &&
3716 find_window(WINID(ws->focus_prev)))
3717 winfocus = ws->focus_prev;
3721 /* if transient focus on parent */
3722 if (cur_focus->transient) {
3723 winfocus = find_window(cur_focus->transient);
3727 /* if in max_stack try harder */
3728 if ((win->quirks & SWM_Q_FOCUSPREV) ||
3729 (ws->cur_layout->flags & SWM_L_FOCUSPREV)) {
3730 if (cur_focus != ws->focus_prev)
3731 winfocus = ws->focus_prev;
3733 winfocus = TAILQ_PREV(win, ws_win_list, entry);
3738 DNPRINTF(SWM_D_FOCUS, "get_focus_prev: focus_close: %d\n", focus_close);
3740 if (winfocus == NULL || winfocus == win) {
3741 switch (focus_close) {
3742 case SWM_STACK_BOTTOM:
3743 TAILQ_FOREACH(winfocus, wl, entry)
3744 if (!winfocus->iconic && winfocus != cur_focus)
3748 TAILQ_FOREACH_REVERSE(winfocus, wl, ws_win_list, entry)
3749 if (!winfocus->iconic && winfocus != cur_focus)
3752 case SWM_STACK_ABOVE:
3753 winfocus = TAILQ_NEXT(cur_focus, entry);
3754 while (winfocus && winfocus->iconic)
3755 winfocus = TAILQ_NEXT(winfocus, entry);
3757 if (winfocus == NULL) {
3758 if (focus_close_wrap) {
3759 TAILQ_FOREACH(winfocus, wl, entry)
3760 if (!winfocus->iconic &&
3761 winfocus != cur_focus)
3764 TAILQ_FOREACH_REVERSE(winfocus, wl,
3766 if (!winfocus->iconic &&
3767 winfocus != cur_focus)
3772 case SWM_STACK_BELOW:
3773 winfocus = TAILQ_PREV(cur_focus, ws_win_list, entry);
3774 while (winfocus && winfocus->iconic)
3775 winfocus = TAILQ_PREV(winfocus, ws_win_list,
3778 if (winfocus == NULL) {
3779 if (focus_close_wrap) {
3780 TAILQ_FOREACH_REVERSE(winfocus, wl,
3782 if (!winfocus->iconic &&
3783 winfocus != cur_focus)
3786 TAILQ_FOREACH(winfocus, wl, entry)
3787 if (!winfocus->iconic &&
3788 winfocus != cur_focus)
3796 if (winfocus == NULL ||
3797 (winfocus && (winfocus->iconic || winfocus == cur_focus))) {
3798 if (focus_default == SWM_STACK_TOP) {
3799 TAILQ_FOREACH_REVERSE(winfocus, wl, ws_win_list, entry)
3800 if (!winfocus->iconic && winfocus != cur_focus)
3803 TAILQ_FOREACH(winfocus, wl, entry)
3804 if (!winfocus->iconic && winfocus != cur_focus)
3811 return get_focus_magic(winfocus);
3815 get_region_focus(struct swm_region *r)
3817 struct ws_win *winfocus = NULL;
3822 if (r->ws->focus && !r->ws->focus->iconic)
3823 winfocus = r->ws->focus;
3824 else if (r->ws->focus_prev && !r->ws->focus_prev->iconic)
3825 winfocus = r->ws->focus_prev;
3827 TAILQ_FOREACH(winfocus, &r->ws->winlist, entry)
3828 if (!winfocus->iconic)
3831 return get_focus_magic(winfocus);
3835 focus(struct swm_region *r, union arg *args)
3837 struct ws_win *head, *cur_focus = NULL, *winfocus = NULL;
3838 struct ws_win_list *wl = NULL;
3839 struct workspace *ws = NULL;
3845 DNPRINTF(SWM_D_FOCUS, "focus: id: %d\n", args->id);
3847 if ((cur_focus = r->ws->focus) == NULL)
3851 if (TAILQ_EMPTY(wl))
3853 /* make sure there is at least one uniconified window */
3855 TAILQ_FOREACH(winfocus, wl, entry)
3856 if (!winfocus->iconic) {
3864 case SWM_ARG_ID_FOCUSPREV:
3865 head = TAILQ_PREV(cur_focus, ws_win_list, entry);
3867 head = TAILQ_LAST(wl, ws_win_list);
3869 if (WINID(winfocus) == cur_focus->transient) {
3870 head = TAILQ_PREV(winfocus, ws_win_list, entry);
3872 head = TAILQ_LAST(wl, ws_win_list);
3877 if (winfocus && winfocus->iconic) {
3878 while (winfocus != cur_focus) {
3879 if (winfocus == NULL)
3880 winfocus = TAILQ_LAST(wl, ws_win_list);
3881 if (!winfocus->iconic)
3883 winfocus = TAILQ_PREV(winfocus, ws_win_list,
3889 case SWM_ARG_ID_FOCUSNEXT:
3890 head = TAILQ_NEXT(cur_focus, entry);
3892 head = TAILQ_FIRST(wl);
3896 if (winfocus && winfocus->iconic) {
3897 while (winfocus != cur_focus) {
3898 if (winfocus == NULL)
3899 winfocus = TAILQ_FIRST(wl);
3900 if (!winfocus->iconic)
3902 winfocus = TAILQ_NEXT(winfocus, entry);
3907 case SWM_ARG_ID_FOCUSMAIN:
3908 winfocus = TAILQ_FIRST(wl);
3909 if (winfocus == cur_focus)
3910 winfocus = cur_focus->ws->focus_prev;
3917 focus_win(get_focus_magic(winfocus));
3923 cycle_layout(struct swm_region *r, union arg *args)
3925 struct workspace *ws = r->ws;
3927 /* suppress unused warning since var is needed */
3930 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
3933 if (ws->cur_layout->l_stack == NULL)
3934 ws->cur_layout = &layouts[0];
3939 focus_win(get_region_focus(r));
3945 stack_config(struct swm_region *r, union arg *args)
3947 struct workspace *ws = r->ws;
3949 DNPRINTF(SWM_D_STACK, "stack_config: id: %d workspace: %d\n",
3952 if (ws->cur_layout->l_config != NULL)
3953 ws->cur_layout->l_config(ws, args->id);
3955 if (args->id != SWM_ARG_ID_STACKINIT)
3964 struct swm_geometry g;
3965 struct swm_region *r;
3971 DNPRINTF(SWM_D_STACK, "stack: begin\n");
3973 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
3974 for (i = 0; i < num_screens; i++) {
3978 TAILQ_FOREACH(r, &screens[i].rl, entry) {
3979 DNPRINTF(SWM_D_STACK, "stack: workspace: %d "
3980 "(screen: %d, region: %d)\n", r->ws->idx, i, j++);
3982 /* Adjust stack area for region bar and padding. */
3984 g.x += region_padding;
3985 g.y += region_padding;
3986 g.w -= 2 * border_width + 2 * region_padding;
3987 g.h -= 2 * border_width + 2 * region_padding;
3988 if (bar_enabled && r->ws->bar_enabled) {
3993 r->ws->cur_layout->l_stack(r->ws, &g);
3994 r->ws->cur_layout->l_string(r->ws);
3995 /* save r so we can track region changes */
4002 DNPRINTF(SWM_D_STACK, "stack: end\n");
4006 store_float_geom(struct ws_win *win, struct swm_region *r)
4008 if (win == NULL || r == NULL)
4011 /* retain window geom and region geom */
4012 win->g_float = win->g;
4013 win->g_float.x -= X(r);
4014 win->g_float.y -= Y(r);
4015 win->g_floatvalid = 1;
4016 DNPRINTF(SWM_D_MISC, "store_float_geom: window: 0x%x, g: (%d,%d)"
4017 " %d x %d, g_float: (%d,%d) %d x %d\n", win->id, X(win), Y(win),
4018 WIDTH(win), HEIGHT(win), win->g_float.x, win->g_float.y,
4019 win->g_float.w, win->g_float.h);
4023 load_float_geom(struct ws_win *win, struct swm_region *r)
4025 if (win == NULL || r == NULL)
4028 if (win->g_floatvalid) {
4029 win->g = win->g_float;
4032 DNPRINTF(SWM_D_MISC, "load_float_geom: window: 0x%x, g: (%d,%d)"
4033 "%d x %d\n", win->id, X(win), Y(win), WIDTH(win),
4036 DNPRINTF(SWM_D_MISC, "load_float_geom: window: 0x%x, g_float "
4037 "is not set.\n", win->id);
4042 stack_floater(struct ws_win *win, struct swm_region *r)
4047 DNPRINTF(SWM_D_MISC, "stack_floater: window: 0x%x\n", win->id);
4050 * to allow windows to change their size (e.g. mplayer fs) only retrieve
4051 * geom on ws switches or return from max mode
4053 if (win->floatmaxed || (r != r->ws->old_r &&
4054 !(win->ewmh_flags & EWMH_F_FULLSCREEN))) {
4055 /* update geometry for the new region */
4056 load_float_geom(win, r);
4059 win->floatmaxed = 0;
4062 * if set to fullscreen mode, configure window to maximum size.
4064 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
4065 if (!win->g_floatvalid)
4066 store_float_geom(win, win->ws->r);
4072 * remove border on fullscreen floater when in fullscreen mode or when
4073 * the quirk is present.
4075 if ((win->ewmh_flags & EWMH_F_FULLSCREEN) ||
4076 ((win->quirks & SWM_Q_FULLSCREEN) &&
4077 (WIDTH(win) >= WIDTH(r)) && (HEIGHT(win) >= HEIGHT(r)))) {
4078 if (win->bordered) {
4080 X(win) += border_width;
4081 Y(win) += border_width;
4083 } else if (!win->bordered) {
4085 X(win) -= border_width;
4086 Y(win) -= border_width;
4089 if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
4090 WIDTH(win) = (double)WIDTH(r) * dialog_ratio;
4091 HEIGHT(win) = (double)HEIGHT(r) * dialog_ratio;
4094 if (!win->manual && !(win->ewmh_flags & EWMH_F_FULLSCREEN) &&
4095 !(win->quirks & SWM_Q_ANYWHERE)) {
4097 * floaters and transients are auto-centred unless moved,
4098 * resized or ANYWHERE quirk is set.
4100 X(win) = X(r) + (WIDTH(r) - WIDTH(win)) / 2 - BORDER(win);
4101 Y(win) = Y(r) + (HEIGHT(r) - HEIGHT(win)) / 2 - BORDER(win);
4103 store_float_geom(win, r);
4106 /* keep window within region bounds */
4107 constrain_window(win, r, 0);
4113 * Send keystrokes to terminal to decrease/increase the font size as the
4114 * window size changes.
4117 adjust_font(struct ws_win *win)
4119 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
4120 win->floating || win->transient)
4123 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
4124 WIDTH(win) / win->sh.width_inc < term_width &&
4125 win->font_steps < SWM_MAX_FONT_STEPS) {
4126 win->font_size_boundary[win->font_steps] =
4127 (win->sh.width_inc * term_width) + win->sh.base_width;
4130 win->last_inc = win->sh.width_inc;
4131 fake_keypress(win, XK_KP_Subtract, XCB_MOD_MASK_SHIFT);
4132 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
4133 WIDTH(win) > win->font_size_boundary[win->font_steps - 1]) {
4136 win->last_inc = win->sh.width_inc;
4137 fake_keypress(win, XK_KP_Add, XCB_MOD_MASK_SHIFT);
4141 #define SWAPXY(g) do { \
4143 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
4144 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
4147 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
4149 struct swm_geometry win_g, r_g = *g;
4150 struct ws_win *win, *fs_win = NULL;
4151 int i, j, s, stacks;
4152 int w_inc = 1, h_inc, w_base = 1, h_base;
4153 int hrh, extra = 0, h_slice, last_h = 0;
4154 int split, colno, winno, mwin, msize, mscale;
4155 int remain, missing, v_slice, reconfigure = 0;
4158 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d, rot: %s, "
4159 "flip: %s\n", ws->idx, YESNO(rot), YESNO(flip));
4161 winno = count_win(ws, 0);
4162 if (winno == 0 && count_win(ws, 1) == 0)
4165 TAILQ_FOREACH(win, &ws->winlist, entry)
4166 if (!win->transient && !win->floating && !win->iconic)
4173 w_inc = win->sh.width_inc;
4174 w_base = win->sh.base_width;
4175 mwin = ws->l_state.horizontal_mwin;
4176 mscale = ws->l_state.horizontal_msize;
4177 stacks = ws->l_state.horizontal_stacks;
4180 w_inc = win->sh.height_inc;
4181 w_base = win->sh.base_height;
4182 mwin = ws->l_state.vertical_mwin;
4183 mscale = ws->l_state.vertical_msize;
4184 stacks = ws->l_state.vertical_stacks;
4188 if (stacks > winno - mwin)
4189 stacks = winno - mwin;
4193 h_slice = r_g.h / SWM_H_SLICE;
4194 if (mwin && winno > mwin) {
4195 v_slice = r_g.w / SWM_V_SLICE;
4199 win_g.w = v_slice * mscale;
4201 if (w_inc > 1 && w_inc < v_slice) {
4202 /* adjust for window's requested size increment */
4203 remain = (win_g.w - w_base) % w_inc;
4209 win_g.x += r_g.w - msize;
4212 colno = split = winno / stacks;
4213 win_g.w = ((r_g.w - (stacks * 2 * border_width) +
4214 2 * border_width) / stacks);
4216 hrh = r_g.h / colno;
4217 extra = r_g.h - (colno * hrh);
4218 win_g.h = hrh - 2 * border_width;
4220 /* stack all the tiled windows */
4221 i = j = 0, s = stacks;
4222 TAILQ_FOREACH(win, &ws->winlist, entry) {
4223 if (win->transient || win->floating || win->iconic)
4226 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
4231 if (split && i == split) {
4232 colno = (winno - mwin) / stacks;
4233 if (s <= (winno - mwin) % stacks)
4235 split = split + colno;
4236 hrh = (r_g.h / colno);
4237 extra = r_g.h - (colno * hrh);
4241 win_g.x += win_g.w + 2 * border_width +
4243 win_g.w = (r_g.w - msize -
4244 (stacks * (2 * border_width + tile_gap))) / stacks;
4246 win_g.w += (r_g.w - msize -
4247 (stacks * (2 * border_width + tile_gap))) %
4253 win_g.h = hrh - 2 * border_width - tile_gap;
4256 h_inc = win->sh.width_inc;
4257 h_base = win->sh.base_width;
4259 h_inc = win->sh.height_inc;
4260 h_base = win->sh.base_height;
4263 if (j == colno - 1) {
4264 win_g.h = hrh + extra;
4265 } else if (h_inc > 1 && h_inc < h_slice) {
4266 /* adjust for window's requested size increment */
4267 remain = (win_g.h - h_base) % h_inc;
4268 missing = h_inc - remain;
4270 if (missing <= extra || j == 0) {
4282 win_g.y += last_h + 2 * border_width + tile_gap;
4284 if (disable_border && !(bar_enabled && ws->bar_enabled) &&
4287 win_g.w += 2 * border_width;
4288 win_g.h += 2 * border_width;
4294 if (X(win) != win_g.y || Y(win) != win_g.x ||
4295 WIDTH(win) != win_g.h || HEIGHT(win) != win_g.w) {
4299 WIDTH(win) = win_g.h;
4300 HEIGHT(win) = win_g.w;
4303 if (X(win) != win_g.x || Y(win) != win_g.y ||
4304 WIDTH(win) != win_g.w || HEIGHT(win) != win_g.h) {
4308 WIDTH(win) = win_g.w;
4309 HEIGHT(win) = win_g.h;
4313 if (bordered != win->bordered) {
4315 win->bordered = bordered;
4331 /* now, stack all the floaters and transients */
4332 TAILQ_FOREACH(win, &ws->winlist, entry) {
4333 if (!win->transient && !win->floating)
4337 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
4342 stack_floater(win, ws->r);
4346 /* Make sure fs_win is stacked last so it's on top. */
4348 stack_floater(fs_win, ws->r);
4354 vertical_config(struct workspace *ws, int id)
4356 DNPRINTF(SWM_D_STACK, "vertical_config: id: %d, workspace: %d\n",
4360 case SWM_ARG_ID_STACKRESET:
4361 case SWM_ARG_ID_STACKINIT:
4362 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
4363 ws->l_state.vertical_mwin = 1;
4364 ws->l_state.vertical_stacks = 1;
4366 case SWM_ARG_ID_MASTERSHRINK:
4367 if (ws->l_state.vertical_msize > 1)
4368 ws->l_state.vertical_msize--;
4370 case SWM_ARG_ID_MASTERGROW:
4371 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
4372 ws->l_state.vertical_msize++;
4374 case SWM_ARG_ID_MASTERADD:
4375 ws->l_state.vertical_mwin++;
4377 case SWM_ARG_ID_MASTERDEL:
4378 if (ws->l_state.vertical_mwin > 0)
4379 ws->l_state.vertical_mwin--;
4381 case SWM_ARG_ID_STACKINC:
4382 ws->l_state.vertical_stacks++;
4384 case SWM_ARG_ID_STACKDEC:
4385 if (ws->l_state.vertical_stacks > 1)
4386 ws->l_state.vertical_stacks--;
4388 case SWM_ARG_ID_FLIPLAYOUT:
4389 ws->l_state.vertical_flip = !ws->l_state.vertical_flip;
4397 vertical_stack(struct workspace *ws, struct swm_geometry *g)
4399 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
4401 stack_master(ws, g, 0, ws->l_state.vertical_flip);
4405 horizontal_config(struct workspace *ws, int id)
4407 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
4410 case SWM_ARG_ID_STACKRESET:
4411 case SWM_ARG_ID_STACKINIT:
4412 ws->l_state.horizontal_mwin = 1;
4413 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
4414 ws->l_state.horizontal_stacks = 1;
4416 case SWM_ARG_ID_MASTERSHRINK:
4417 if (ws->l_state.horizontal_msize > 1)
4418 ws->l_state.horizontal_msize--;
4420 case SWM_ARG_ID_MASTERGROW:
4421 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
4422 ws->l_state.horizontal_msize++;
4424 case SWM_ARG_ID_MASTERADD:
4425 ws->l_state.horizontal_mwin++;
4427 case SWM_ARG_ID_MASTERDEL:
4428 if (ws->l_state.horizontal_mwin > 0)
4429 ws->l_state.horizontal_mwin--;
4431 case SWM_ARG_ID_STACKINC:
4432 ws->l_state.horizontal_stacks++;
4434 case SWM_ARG_ID_STACKDEC:
4435 if (ws->l_state.horizontal_stacks > 1)
4436 ws->l_state.horizontal_stacks--;
4438 case SWM_ARG_ID_FLIPLAYOUT:
4439 ws->l_state.horizontal_flip = !ws->l_state.horizontal_flip;
4447 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
4449 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
4451 stack_master(ws, g, 1, ws->l_state.horizontal_flip);
4454 /* fullscreen view */
4456 max_stack(struct workspace *ws, struct swm_geometry *g)
4458 struct swm_geometry gg = *g;
4459 struct ws_win *w, *win = NULL, *parent = NULL;
4462 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
4467 winno = count_win(ws, 0);
4468 if (winno == 0 && count_win(ws, 1) == 0)
4471 /* Figure out which top level window should be visible. */
4472 if (ws->focus_pending)
4473 win = ws->focus_pending;
4477 win = TAILQ_FIRST(&ws->winlist);
4480 parent = find_window(win->transient);
4482 DNPRINTF(SWM_D_STACK, "max_stack: win: 0x%x\n", win->id);
4484 /* maximize all top level windows */
4485 TAILQ_FOREACH(w, &ws->winlist, entry) {
4486 if (w->transient || w->iconic)
4489 if (!w->mapped && w != win)
4492 if (w->floating && !w->floatmaxed) {
4494 * retain geometry for retrieval on exit from
4497 store_float_geom(w, ws->r);
4501 /* only reconfigure if necessary */
4502 if (X(w) != gg.x || Y(w) != gg.y || WIDTH(w) != gg.w ||
4503 HEIGHT(w) != gg.h) {
4505 if (bar_enabled && ws->bar_enabled){
4509 WIDTH(w) += 2 * border_width;
4510 HEIGHT(w) += 2 * border_width;
4517 /* If a parent exists, map/raise it first. */
4521 /* Map siblings next. */
4522 TAILQ_FOREACH(w, &ws->winlist, entry)
4523 if (w != win && !w->iconic &&
4524 w->transient == parent->id) {
4525 stack_floater(w, ws->r);
4530 /* Map/raise focused window. */
4533 /* Finally, map/raise children of focus window. */
4534 TAILQ_FOREACH(w, &ws->winlist, entry)
4535 if (w->transient == win->id && !w->iconic) {
4536 stack_floater(w, ws->r);
4542 send_to_rg(struct swm_region *r, union arg *args)
4544 int ridx = args->id, i, num_screens;
4545 struct swm_region *rr = NULL;
4548 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
4549 /* do nothing if we don't have more than one screen */
4550 if (!(num_screens > 1 || outputs > 1))
4553 DNPRINTF(SWM_D_FOCUS, "send_to_rg: id: %d\n", ridx);
4555 rr = TAILQ_FIRST(&r->s->rl);
4556 for (i = 0; i < ridx; ++i)
4557 rr = TAILQ_NEXT(rr, entry);
4568 send_to_ws(struct swm_region *r, union arg *args)
4570 int wsid = args->id;
4571 struct ws_win *win = NULL, *parent;
4572 struct workspace *ws, *nws, *pws;
4573 char ws_idx_str[SWM_PROPLEN];
4575 if (wsid >= workspace_limit)
4578 if (r && r->ws && r->ws->focus)
4583 if (win->ws->idx == wsid)
4586 DNPRINTF(SWM_D_MOVE, "send_to_ws: win 0x%x, ws %d -> %d\n", win->id,
4587 win->ws->idx, wsid);
4590 nws = &win->s->ws[wsid];
4592 /* Update the window's workspace property: _SWM_WS */
4593 if (snprintf(ws_idx_str, SWM_PROPLEN, "%d", nws->idx) < SWM_PROPLEN) {
4594 if (focus_mode != SWM_FOCUS_FOLLOW)
4595 ws->focus_pending = get_focus_prev(win);
4597 /* Move the parent if this is a transient window. */
4598 if (win->transient) {
4599 parent = find_window(win->transient);
4602 /* Set new focus in parent's ws if needed. */
4603 if (pws->focus == parent) {
4604 if (focus_mode != SWM_FOCUS_FOLLOW)
4605 pws->focus_pending =
4606 get_focus_prev(parent);
4608 unfocus_win(parent);
4610 if (focus_mode != SWM_FOCUS_FOLLOW)
4611 pws->focus = pws->focus_pending;
4613 if (focus_mode != SWM_FOCUS_FOLLOW)
4614 pws->focus_pending = NULL;
4617 /* Don't unmap parent if new ws is visible */
4619 unmap_window(parent);
4622 TAILQ_REMOVE(&ws->winlist, parent, entry);
4623 TAILQ_INSERT_TAIL(&nws->winlist, parent, entry);
4626 DNPRINTF(SWM_D_PROP, "send_to_ws: set "
4627 "property: _SWM_WS: %s\n", ws_idx_str);
4628 xcb_change_property(conn, XCB_PROP_MODE_REPLACE,
4629 parent->id, a_swm_ws, XCB_ATOM_STRING, 8,
4630 strlen(ws_idx_str), ws_idx_str);
4636 /* Don't unmap if new ws is visible */
4641 TAILQ_REMOVE(&ws->winlist, win, entry);
4642 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
4645 /* Set focus on new ws. */
4646 unfocus_win(nws->focus);
4649 DNPRINTF(SWM_D_PROP, "send_to_ws: set property: _SWM_WS: %s\n",
4651 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
4652 a_swm_ws, XCB_ATOM_STRING, 8, strlen(ws_idx_str),
4655 /* Restack and set new focus. */
4658 if (focus_mode != SWM_FOCUS_FOLLOW) {
4659 focus_win(ws->focus_pending);
4660 ws->focus_pending = NULL;
4666 DNPRINTF(SWM_D_MOVE, "send_to_ws: done.\n");
4670 pressbutton(struct swm_region *r, union arg *args)
4672 /* suppress unused warning since var is needed */
4675 xcb_test_fake_input(conn, XCB_BUTTON_PRESS, args->id,
4676 XCB_CURRENT_TIME, XCB_WINDOW_NONE, 0, 0, 0);
4677 xcb_test_fake_input(conn, XCB_BUTTON_RELEASE, args->id,
4678 XCB_CURRENT_TIME, XCB_WINDOW_NONE, 0, 0, 0);
4682 raise_toggle(struct swm_region *r, union arg *args)
4684 /* suppress unused warning since var is needed */
4687 if (r == NULL || r->ws == NULL)
4690 r->ws->always_raise = !r->ws->always_raise;
4692 /* bring floaters back to top */
4693 if (!r->ws->always_raise)
4700 iconify(struct swm_region *r, union arg *args)
4702 /* suppress unused warning since var is needed */
4705 if (r->ws->focus == NULL)
4708 set_swm_iconic(r->ws->focus, 1);
4714 get_win_name(xcb_window_t win)
4717 xcb_get_property_cookie_t c;
4718 xcb_get_property_reply_t *r;
4720 /* First try _NET_WM_NAME for UTF-8. */
4721 c = xcb_get_property(conn, 0, win, a_netwmname,
4722 XCB_GET_PROPERTY_TYPE_ANY, 0, UINT_MAX);
4723 r = xcb_get_property_reply(conn, c, NULL);
4726 if (r->type == XCB_NONE) {
4728 /* Use WM_NAME instead; no UTF-8. */
4729 c = xcb_get_property(conn, 0, win, XCB_ATOM_WM_NAME,
4730 XCB_GET_PROPERTY_TYPE_ANY, 0, UINT_MAX);
4731 r = xcb_get_property_reply(conn, c, NULL);
4735 if (r->type == XCB_NONE) {
4741 name = strndup(xcb_get_property_value(r),
4742 xcb_get_property_value_length(r));
4751 uniconify(struct swm_region *r, union arg *args)
4758 DNPRINTF(SWM_D_MISC, "uniconify\n");
4760 if (r == NULL || r->ws == NULL)
4763 /* make sure we have anything to uniconify */
4764 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
4765 if (win->ws == NULL)
4766 continue; /* should never happen */
4775 search_resp_action = SWM_SEARCH_UNICONIFY;
4777 spawn_select(r, args, "search", &searchpid);
4779 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
4782 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
4783 if (win->ws == NULL)
4784 continue; /* should never happen */
4788 name = get_win_name(win->id);
4791 fprintf(lfile, "%s.%u\n", name, win->id);
4799 name_workspace(struct swm_region *r, union arg *args)
4803 DNPRINTF(SWM_D_MISC, "name_workspace\n");
4809 search_resp_action = SWM_SEARCH_NAME_WORKSPACE;
4811 spawn_select(r, args, "name_workspace", &searchpid);
4813 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
4816 fprintf(lfile, "%s", "");
4821 search_workspace(struct swm_region *r, union arg *args)
4824 struct workspace *ws;
4827 DNPRINTF(SWM_D_MISC, "search_workspace\n");
4833 search_resp_action = SWM_SEARCH_SEARCH_WORKSPACE;
4835 spawn_select(r, args, "search", &searchpid);
4837 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
4840 for (i = 0; i < workspace_limit; i++) {
4844 fprintf(lfile, "%d%s%s\n", ws->idx + 1,
4845 (ws->name ? ":" : ""), (ws->name ? ws->name : ""));
4852 search_win_cleanup(void)
4854 struct search_window *sw = NULL;
4856 while ((sw = TAILQ_FIRST(&search_wl)) != NULL) {
4857 xcb_destroy_window(conn, sw->indicator);
4858 TAILQ_REMOVE(&search_wl, sw, entry);
4864 search_win(struct swm_region *r, union arg *args)
4866 struct ws_win *win = NULL;
4867 struct search_window *sw = NULL;
4870 int i, width, height;
4878 XRectangle l_ibox, l_lbox;
4880 DNPRINTF(SWM_D_MISC, "search_win\n");
4883 search_resp_action = SWM_SEARCH_SEARCH_WINDOW;
4885 spawn_select(r, args, "search", &searchpid);
4887 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
4890 TAILQ_INIT(&search_wl);
4893 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
4897 sw = calloc(1, sizeof(struct search_window));
4899 warn("search_win: calloc");
4901 search_win_cleanup();
4907 snprintf(s, sizeof s, "%d", i);
4910 w = xcb_generate_id(conn);
4911 wa[0] = r->s->c[SWM_S_COLOR_FOCUS].pixel;
4912 wa[1] = r->s->c[SWM_S_COLOR_UNFOCUS].pixel;
4914 if (bar_font_legacy) {
4915 XmbTextExtents(bar_fs, s, len, &l_ibox, &l_lbox);
4916 width = l_lbox.width + 4;
4917 height = bar_fs_extents->max_logical_extent.height + 4;
4919 XftTextExtentsUtf8(display, bar_font, (FcChar8 *)s, len,
4921 width = info.width + 4;
4922 height = bar_font->height + 4;
4925 xcb_create_window(conn, XCB_COPY_FROM_PARENT, w, win->id, 0, 0,
4926 width, height, 1, XCB_WINDOW_CLASS_INPUT_OUTPUT,
4927 XCB_COPY_FROM_PARENT, XCB_CW_BACK_PIXEL |
4928 XCB_CW_BORDER_PIXEL, wa);
4930 xcb_map_window(conn, w);
4933 TAILQ_INSERT_TAIL(&search_wl, sw, entry);
4935 if (bar_font_legacy) {
4936 l_gcv.graphics_exposures = 0;
4937 l_draw = XCreateGC(display, w, 0, &l_gcv);
4939 XSetForeground(display, l_draw,
4940 r->s->c[SWM_S_COLOR_BAR].pixel);
4942 DRAWSTRING(display, w, bar_fs, l_draw, 2,
4943 (bar_fs_extents->max_logical_extent.height -
4944 l_lbox.height) / 2 - l_lbox.y, s, len);
4946 XFreeGC(display, l_draw);
4949 draw = XftDrawCreate(display, w,
4950 DefaultVisual(display, r->s->idx),
4951 DefaultColormap(display, r->s->idx));
4953 XftDrawStringUtf8(draw, &bar_font_color, bar_font, 2,
4954 (HEIGHT(r->bar) + bar_font->height) / 2 -
4955 bar_font->descent, (FcChar8 *)s, len);
4957 XftDrawDestroy(draw);
4960 DNPRINTF(SWM_D_MISC, "search_win: mapped window: 0x%x\n", w);
4962 fprintf(lfile, "%d\n", i);
4972 search_resp_uniconify(const char *resp, unsigned long len)
4978 DNPRINTF(SWM_D_MISC, "search_resp_uniconify: resp: %s\n", resp);
4980 TAILQ_FOREACH(win, &search_r->ws->winlist, entry) {
4983 name = get_win_name(win->id);
4986 if (asprintf(&s, "%s.%u", name, win->id) == -1) {
4991 if (strncmp(s, resp, len) == 0) {
4992 /* XXX this should be a callback to generalize */
4993 set_swm_iconic(win, 0);
5002 search_resp_name_workspace(const char *resp, unsigned long len)
5004 struct workspace *ws;
5006 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: resp: %s\n", resp);
5008 if (search_r->ws == NULL)
5013 free(search_r->ws->name);
5014 search_r->ws->name = NULL;
5018 ws->name = strdup(resp);
5019 if (ws->name == NULL) {
5020 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: "
5021 "strdup: %s", strerror(errno));
5028 search_resp_search_workspace(const char *resp)
5035 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: resp: %s\n", resp);
5039 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: strdup: %s",
5046 ws_idx = (int)strtonum(q, 1, workspace_limit, &errstr);
5048 DNPRINTF(SWM_D_MISC, "workspace idx is %s: %s",
5055 switchws(search_r, &a);
5059 search_resp_search_window(const char *resp)
5064 struct search_window *sw;
5066 DNPRINTF(SWM_D_MISC, "search_resp_search_window: resp: %s\n", resp);
5070 DNPRINTF(SWM_D_MISC, "search_resp_search_window: strdup: %s",
5075 idx = (int)strtonum(s, 1, INT_MAX, &errstr);
5077 DNPRINTF(SWM_D_MISC, "window idx is %s: %s",
5084 TAILQ_FOREACH(sw, &search_wl, entry)
5085 if (idx == sw->idx) {
5091 #define MAX_RESP_LEN 1024
5094 search_do_resp(void)
5100 DNPRINTF(SWM_D_MISC, "search_do_resp:\n");
5105 if ((resp = calloc(1, MAX_RESP_LEN + 1)) == NULL) {
5106 warn("search: calloc");
5110 rbytes = read(select_resp_pipe[0], resp, MAX_RESP_LEN);
5112 warn("search: read error");
5115 resp[rbytes] = '\0';
5118 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
5119 * newline, so work around that by sanitizing the resp now.
5121 resp[strcspn(resp, "\n")] = '\0';
5124 switch (search_resp_action) {
5125 case SWM_SEARCH_UNICONIFY:
5126 search_resp_uniconify(resp, len);
5128 case SWM_SEARCH_NAME_WORKSPACE:
5129 search_resp_name_workspace(resp, len);
5131 case SWM_SEARCH_SEARCH_WORKSPACE:
5132 search_resp_search_workspace(resp);
5134 case SWM_SEARCH_SEARCH_WINDOW:
5135 search_resp_search_window(resp);
5140 if (search_resp_action == SWM_SEARCH_SEARCH_WINDOW)
5141 search_win_cleanup();
5143 search_resp_action = SWM_SEARCH_NONE;
5144 close(select_resp_pipe[0]);
5151 wkill(struct swm_region *r, union arg *args)
5153 DNPRINTF(SWM_D_MISC, "wkill: id: %d\n", args->id);
5155 if (r->ws->focus == NULL)
5158 if (args->id == SWM_ARG_ID_KILLWINDOW)
5159 xcb_kill_client(conn, r->ws->focus->id);
5161 if (r->ws->focus->can_delete)
5162 client_msg(r->ws->focus, a_delete, 0);
5168 floating_toggle_win(struct ws_win *win)
5170 struct swm_region *r;
5180 /* reject floating toggles in max stack mode */
5181 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK])
5184 if (win->floating) {
5185 if (!win->floatmaxed) {
5186 /* retain position for refloat */
5187 store_float_geom(win, r);
5191 load_float_geom(win, r);
5195 ewmh_update_actions(win);
5201 floating_toggle(struct swm_region *r, union arg *args)
5203 struct ws_win *win = r->ws->focus;
5205 /* suppress unused warning since var is needed */
5211 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
5214 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
5215 _NET_WM_STATE_TOGGLE);
5219 if (win == win->ws->focus)
5226 constrain_window(struct ws_win *win, struct swm_region *r, int resizable)
5228 if (MAX_X(win) + BORDER(win) > MAX_X(r)) {
5230 WIDTH(win) = MAX_X(r) - X(win) - BORDER(win);
5232 X(win) = MAX_X(r)- WIDTH(win) - BORDER(win);
5235 if (X(win) + BORDER(win) < X(r)) {
5237 WIDTH(win) -= X(r) - X(win) - BORDER(win);
5239 X(win) = X(r) - BORDER(win);
5242 if (MAX_Y(win) + BORDER(win) > MAX_Y(r)) {
5244 HEIGHT(win) = MAX_Y(r) - Y(win) - BORDER(win);
5246 Y(win) = MAX_Y(r) - HEIGHT(win) - BORDER(win);
5249 if (Y(win) + BORDER(win) < Y(r)) {
5251 HEIGHT(win) -= Y(r) - Y(win) - BORDER(win);
5253 Y(win) = Y(r) - BORDER(win);
5259 if (HEIGHT(win) < 1)
5265 update_window(struct ws_win *win)
5270 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
5271 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
5272 XCB_CONFIG_WINDOW_BORDER_WIDTH;
5276 wc[3] = HEIGHT(win);
5277 wc[4] = BORDER(win);
5279 DNPRINTF(SWM_D_EVENT, "update_window: window: 0x%x, (x,y) w x h: "
5280 "(%d,%d) %d x %d, bordered: %s\n", win->id, wc[0], wc[1], wc[2],
5281 wc[3], YESNO(win->bordered));
5283 xcb_configure_window(conn, win->id, mask, wc);
5286 #define SWM_RESIZE_STEPS (50)
5289 resize(struct ws_win *win, union arg *args)
5291 xcb_timestamp_t timestamp = 0;
5292 struct swm_region *r = NULL;
5294 struct swm_geometry g;
5295 int top = 0, left = 0, resizing;
5297 xcb_cursor_t cursor;
5298 xcb_query_pointer_reply_t *xpr;
5299 xcb_generic_event_t *evt;
5300 xcb_motion_notify_event_t *mne;
5306 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
5309 DNPRINTF(SWM_D_EVENT, "resize: window: 0x%x, floating: %s, "
5310 "transient: 0x%x\n", win->id, YESNO(win->floating),
5313 if (!win->transient && !win->floating)
5316 /* reject resizes in max mode for floaters (transient ok) */
5317 if (win->floatmaxed)
5321 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
5328 /* It's possible for win to have been freed during focus_flush(). */
5329 if (validate_win(win)) {
5330 DNPRINTF(SWM_D_EVENT, "move: invalid win.\n");
5335 case SWM_ARG_ID_WIDTHSHRINK:
5336 WIDTH(win) -= SWM_RESIZE_STEPS;
5339 case SWM_ARG_ID_WIDTHGROW:
5340 WIDTH(win) += SWM_RESIZE_STEPS;
5343 case SWM_ARG_ID_HEIGHTSHRINK:
5344 HEIGHT(win) -= SWM_RESIZE_STEPS;
5347 case SWM_ARG_ID_HEIGHTGROW:
5348 HEIGHT(win) += SWM_RESIZE_STEPS;
5355 constrain_window(win, r, 1);
5357 store_float_geom(win,r);
5361 /* get cursor offset from window root */
5362 xpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, win->id),
5369 if (xpr->win_x < WIDTH(win) / 2)
5372 if (xpr->win_y < HEIGHT(win) / 2)
5375 if (args->id == SWM_ARG_ID_CENTER)
5376 cursor = cursors[XC_SIZING].cid;
5378 cursor = cursors[left ? XC_TOP_LEFT_CORNER :
5379 XC_TOP_RIGHT_CORNER].cid;
5381 cursor = cursors[left ? XC_BOTTOM_LEFT_CORNER :
5382 XC_BOTTOM_RIGHT_CORNER].cid;
5384 xcb_grab_pointer(conn, 0, win->id, MOUSEMASK,
5385 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, XCB_WINDOW_NONE, cursor,
5390 while (resizing && (evt = xcb_wait_for_event(conn))) {
5391 switch (XCB_EVENT_RESPONSE_TYPE(evt)) {
5392 case XCB_BUTTON_RELEASE:
5393 DNPRINTF(SWM_D_EVENT, "resize: BUTTON_RELEASE\n");
5396 case XCB_MOTION_NOTIFY:
5397 mne = (xcb_motion_notify_event_t *)evt;
5398 /* cursor offset/delta from start of the operation */
5399 dx = mne->root_x - xpr->root_x;
5400 dy = mne->root_y - xpr->root_y;
5406 if (args->id == SWM_ARG_ID_CENTER) {
5407 if (g.h / 2 + dy < 1)
5411 HEIGHT(win) = g.h + 2 * dy;
5419 HEIGHT(win) = g.h + dy;
5426 if (args->id == SWM_ARG_ID_CENTER) {
5427 if (g.w / 2 + dx < 1)
5431 WIDTH(win) = g.w + 2 * dx;
5439 WIDTH(win) = g.w + dx;
5442 constrain_window(win, r, 1);
5444 /* not free, don't sync more than 120 times / second */
5445 if ((mne->time - timestamp) > (1000 / 120) ) {
5446 timestamp = mne->time;
5454 /* It's possible for win to have been freed above. */
5455 if (validate_win(win)) {
5456 DNPRINTF(SWM_D_EVENT, "move: invalid win.\n");
5467 store_float_geom(win,r);
5469 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
5471 DNPRINTF(SWM_D_EVENT, "resize: done.\n");
5475 resize_step(struct swm_region *r, union arg *args)
5477 struct ws_win *win = NULL;
5479 if (r && r->ws && r->ws->focus)
5488 #define SWM_MOVE_STEPS (50)
5491 move(struct ws_win *win, union arg *args)
5493 xcb_timestamp_t timestamp = 0;
5494 int move_stp = 0, moving;
5495 struct swm_region *r = NULL;
5496 xcb_query_pointer_reply_t *qpr;
5497 xcb_generic_event_t *evt;
5498 xcb_motion_notify_event_t *mne;
5504 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
5507 DNPRINTF(SWM_D_EVENT, "move: window: 0x%x, floating: %s, transient: "
5508 "0x%x\n", win->id, YESNO(win->floating), win->transient);
5510 /* in max_stack mode should only move transients */
5511 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !win->transient)
5515 if (!win->floating && !win->transient) {
5516 store_float_geom(win, r);
5517 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
5520 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
5527 /* It's possible for win to have been freed during focus_flush(). */
5528 if (validate_win(win)) {
5529 DNPRINTF(SWM_D_EVENT, "move: invalid win.\n");
5535 case SWM_ARG_ID_MOVELEFT:
5536 X(win) -= (SWM_MOVE_STEPS - border_width);
5539 case SWM_ARG_ID_MOVERIGHT:
5540 X(win) += (SWM_MOVE_STEPS - border_width);
5543 case SWM_ARG_ID_MOVEUP:
5544 Y(win) -= (SWM_MOVE_STEPS - border_width);
5547 case SWM_ARG_ID_MOVEDOWN:
5548 Y(win) += (SWM_MOVE_STEPS - border_width);
5555 constrain_window(win, r, 0);
5557 store_float_geom(win, r);
5561 xcb_grab_pointer(conn, 0, win->id, MOUSEMASK,
5562 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC,
5563 XCB_WINDOW_NONE, cursors[XC_FLEUR].cid, XCB_CURRENT_TIME);
5565 /* get cursor offset from window root */
5566 qpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, win->id),
5569 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
5575 while (moving && (evt = xcb_wait_for_event(conn))) {
5576 switch (XCB_EVENT_RESPONSE_TYPE(evt)) {
5577 case XCB_BUTTON_RELEASE:
5578 DNPRINTF(SWM_D_EVENT, "move: BUTTON_RELEASE\n");
5581 case XCB_MOTION_NOTIFY:
5582 mne = (xcb_motion_notify_event_t *)evt;
5583 X(win) = mne->root_x - qpr->win_x - border_width;
5584 Y(win) = mne->root_y - qpr->win_y - border_width;
5586 constrain_window(win, r, 0);
5588 /* not free, don't sync more than 120 times / second */
5589 if ((mne->time - timestamp) > (1000 / 120) ) {
5590 timestamp = mne->time;
5598 /* It's possible for win to have been freed above. */
5599 if (validate_win(win)) {
5600 DNPRINTF(SWM_D_EVENT, "move: invalid win.\n");
5611 store_float_geom(win, r);
5614 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
5615 DNPRINTF(SWM_D_EVENT, "move: done.\n");
5619 move_step(struct swm_region *r, union arg *args)
5621 struct ws_win *win = NULL;
5623 if (r && r->ws && r->ws->focus)
5628 if (!win->transient && !win->floating)
5635 /* key definitions */
5637 char name[SWM_FUNCNAME_LEN];
5638 void (*func)(struct swm_region *r, union arg *);
5640 } keyfuncs[KF_INVALID + 1] = {
5641 /* name function argument */
5642 { "bar_toggle", bar_toggle, {.id = SWM_ARG_ID_BAR_TOGGLE} },
5643 { "bar_toggle_ws", bar_toggle, {.id = SWM_ARG_ID_BAR_TOGGLE_WS} },
5644 { "button2", pressbutton, {2} },
5645 { "cycle_layout", cycle_layout, {0} },
5646 { "flip_layout", stack_config, {.id = SWM_ARG_ID_FLIPLAYOUT} },
5647 { "float_toggle", floating_toggle,{0} },
5648 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
5649 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
5650 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
5651 { "height_grow", resize_step, {.id = SWM_ARG_ID_HEIGHTGROW} },
5652 { "height_shrink", resize_step, {.id = SWM_ARG_ID_HEIGHTSHRINK} },
5653 { "iconify", iconify, {0} },
5654 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
5655 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
5656 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
5657 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
5658 { "move_down", move_step, {.id = SWM_ARG_ID_MOVEDOWN} },
5659 { "move_left", move_step, {.id = SWM_ARG_ID_MOVELEFT} },
5660 { "move_right", move_step, {.id = SWM_ARG_ID_MOVERIGHT} },
5661 { "move_up", move_step, {.id = SWM_ARG_ID_MOVEUP} },
5662 { "mvrg_1", send_to_rg, {.id = 0} },
5663 { "mvrg_2", send_to_rg, {.id = 1} },
5664 { "mvrg_3", send_to_rg, {.id = 2} },
5665 { "mvrg_4", send_to_rg, {.id = 3} },
5666 { "mvrg_5", send_to_rg, {.id = 4} },
5667 { "mvrg_6", send_to_rg, {.id = 5} },
5668 { "mvrg_7", send_to_rg, {.id = 6} },
5669 { "mvrg_8", send_to_rg, {.id = 7} },
5670 { "mvrg_9", send_to_rg, {.id = 8} },
5671 { "mvws_1", send_to_ws, {.id = 0} },
5672 { "mvws_2", send_to_ws, {.id = 1} },
5673 { "mvws_3", send_to_ws, {.id = 2} },
5674 { "mvws_4", send_to_ws, {.id = 3} },
5675 { "mvws_5", send_to_ws, {.id = 4} },
5676 { "mvws_6", send_to_ws, {.id = 5} },
5677 { "mvws_7", send_to_ws, {.id = 6} },
5678 { "mvws_8", send_to_ws, {.id = 7} },
5679 { "mvws_9", send_to_ws, {.id = 8} },
5680 { "mvws_10", send_to_ws, {.id = 9} },
5681 { "mvws_11", send_to_ws, {.id = 10} },
5682 { "mvws_12", send_to_ws, {.id = 11} },
5683 { "mvws_13", send_to_ws, {.id = 12} },
5684 { "mvws_14", send_to_ws, {.id = 13} },
5685 { "mvws_15", send_to_ws, {.id = 14} },
5686 { "mvws_16", send_to_ws, {.id = 15} },
5687 { "mvws_17", send_to_ws, {.id = 16} },
5688 { "mvws_18", send_to_ws, {.id = 17} },
5689 { "mvws_19", send_to_ws, {.id = 18} },
5690 { "mvws_20", send_to_ws, {.id = 19} },
5691 { "mvws_21", send_to_ws, {.id = 20} },
5692 { "mvws_22", send_to_ws, {.id = 21} },
5693 { "name_workspace", name_workspace, {0} },
5694 { "quit", quit, {0} },
5695 { "raise_toggle", raise_toggle, {0} },
5696 { "restart", restart, {0} },
5697 { "rg_1", focusrg, {.id = 0} },
5698 { "rg_2", focusrg, {.id = 1} },
5699 { "rg_3", focusrg, {.id = 2} },
5700 { "rg_4", focusrg, {.id = 3} },
5701 { "rg_5", focusrg, {.id = 4} },
5702 { "rg_6", focusrg, {.id = 5} },
5703 { "rg_7", focusrg, {.id = 6} },
5704 { "rg_8", focusrg, {.id = 7} },
5705 { "rg_9", focusrg, {.id = 8} },
5706 { "rg_next", cyclerg, {.id = SWM_ARG_ID_CYCLERG_UP} },
5707 { "rg_prev", cyclerg, {.id = SWM_ARG_ID_CYCLERG_DOWN} },
5708 { "screen_next", cyclerg, {.id = SWM_ARG_ID_CYCLERG_UP} },
5709 { "screen_prev", cyclerg, {.id = SWM_ARG_ID_CYCLERG_DOWN} },
5710 { "search_win", search_win, {0} },
5711 { "search_workspace", search_workspace, {0} },
5712 { "spawn_custom", NULL, {0} },
5713 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
5714 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
5715 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
5716 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
5717 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
5718 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
5719 { "uniconify", uniconify, {0} },
5720 { "version", version, {0} },
5721 { "width_grow", resize_step, {.id = SWM_ARG_ID_WIDTHGROW} },
5722 { "width_shrink", resize_step, {.id = SWM_ARG_ID_WIDTHSHRINK} },
5723 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
5724 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
5725 { "ws_1", switchws, {.id = 0} },
5726 { "ws_2", switchws, {.id = 1} },
5727 { "ws_3", switchws, {.id = 2} },
5728 { "ws_4", switchws, {.id = 3} },
5729 { "ws_5", switchws, {.id = 4} },
5730 { "ws_6", switchws, {.id = 5} },
5731 { "ws_7", switchws, {.id = 6} },
5732 { "ws_8", switchws, {.id = 7} },
5733 { "ws_9", switchws, {.id = 8} },
5734 { "ws_10", switchws, {.id = 9} },
5735 { "ws_11", switchws, {.id = 10} },
5736 { "ws_12", switchws, {.id = 11} },
5737 { "ws_13", switchws, {.id = 12} },
5738 { "ws_14", switchws, {.id = 13} },
5739 { "ws_15", switchws, {.id = 14} },
5740 { "ws_16", switchws, {.id = 15} },
5741 { "ws_17", switchws, {.id = 16} },
5742 { "ws_18", switchws, {.id = 17} },
5743 { "ws_19", switchws, {.id = 18} },
5744 { "ws_20", switchws, {.id = 19} },
5745 { "ws_21", switchws, {.id = 20} },
5746 { "ws_22", switchws, {.id = 21} },
5747 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
5748 { "ws_next_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP_ALL} },
5749 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
5750 { "ws_prev_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN_ALL} },
5751 { "ws_prior", priorws, {0} },
5752 { "dumpwins", dumpwins, {0} }, /* MUST BE LAST */
5753 { "invalid key func", NULL, {0} },
5757 key_cmp(struct key *kp1, struct key *kp2)
5759 if (kp1->keysym < kp2->keysym)
5761 if (kp1->keysym > kp2->keysym)
5764 if (kp1->mod < kp2->mod)
5766 if (kp1->mod > kp2->mod)
5773 enum { client_click, root_click };
5775 unsigned int action;
5777 unsigned int button;
5778 void (*func)(struct ws_win *, union arg *);
5781 #define MODKEY_SHIFT MODKEY | XCB_MOD_MASK_SHIFT
5782 /* action key mouse button func args */
5783 { client_click, MODKEY, XCB_BUTTON_INDEX_3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
5784 { client_click, MODKEY_SHIFT, XCB_BUTTON_INDEX_3, resize, {.id = SWM_ARG_ID_CENTER} },
5785 { client_click, MODKEY, XCB_BUTTON_INDEX_1, move, {0} },
5790 update_modkey(unsigned int mod)
5796 RB_FOREACH(kp, key_tree, &keys)
5797 if (kp->mod & XCB_MOD_MASK_SHIFT)
5798 kp->mod = mod | XCB_MOD_MASK_SHIFT;
5802 for (i = 0; i < LENGTH(buttons); i++)
5803 if (buttons[i].mask & XCB_MOD_MASK_SHIFT)
5804 buttons[i].mask = mod | XCB_MOD_MASK_SHIFT;
5806 buttons[i].mask = mod;
5810 spawn_expand(struct swm_region *r, union arg *args, const char *spawn_name,
5813 struct spawn_prog *prog = NULL;
5815 char *ap, **real_args;
5817 /* suppress unused warning since var is needed */
5820 DNPRINTF(SWM_D_SPAWN, "spawn_expand: %s\n", spawn_name);
5823 TAILQ_FOREACH(prog, &spawns, entry) {
5824 if (!strcasecmp(spawn_name, prog->name))
5828 warnx("spawn_custom: program %s not found", spawn_name);
5832 /* make room for expanded args */
5833 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
5834 err(1, "spawn_custom: calloc real_args");
5836 /* expand spawn_args into real_args */
5837 for (i = 0; i < prog->argc; i++) {
5839 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg: %s\n", ap);
5840 if (!strcasecmp(ap, "$bar_border")) {
5842 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
5844 err(1, "spawn_custom border color");
5845 } else if (!strcasecmp(ap, "$bar_color")) {
5847 strdup(r->s->c[SWM_S_COLOR_BAR].name))
5849 err(1, "spawn_custom bar color");
5850 } else if (!strcasecmp(ap, "$bar_font")) {
5851 if ((real_args[i] = strdup(bar_fonts))
5853 err(1, "spawn_custom bar fonts");
5854 } else if (!strcasecmp(ap, "$bar_font_color")) {
5856 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
5858 err(1, "spawn_custom color font");
5859 } else if (!strcasecmp(ap, "$color_focus")) {
5861 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
5863 err(1, "spawn_custom color focus");
5864 } else if (!strcasecmp(ap, "$color_unfocus")) {
5866 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
5868 err(1, "spawn_custom color unfocus");
5869 } else if (!strcasecmp(ap, "$region_index")) {
5870 if (asprintf(&real_args[i], "%d",
5871 get_region_index(r) + 1) < 1)
5872 err(1, "spawn_custom region index");
5873 } else if (!strcasecmp(ap, "$workspace_index")) {
5874 if (asprintf(&real_args[i], "%d", r->ws->idx + 1) < 1)
5875 err(1, "spawn_custom workspace index");
5877 /* no match --> copy as is */
5878 if ((real_args[i] = strdup(ap)) == NULL)
5879 err(1, "spawn_custom strdup(ap)");
5881 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg: %s\n",
5886 DNPRINTF(SWM_D_SPAWN, "spawn_custom: result: ");
5887 for (i = 0; i < prog->argc; i++)
5888 DNPRINTF(SWM_D_SPAWN, "\"%s\" ", real_args[i]);
5889 DNPRINTF(SWM_D_SPAWN, "\n");
5891 *ret_args = real_args;
5892 return (prog->argc);
5896 spawn_custom(struct swm_region *r, union arg *args, const char *spawn_name)
5902 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
5906 spawn(r->ws->idx, &a, 1);
5908 for (i = 0; i < spawn_argc; i++)
5914 spawn_select(struct swm_region *r, union arg *args, const char *spawn_name,
5921 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
5925 if (pipe(select_list_pipe) == -1)
5926 err(1, "pipe error");
5927 if (pipe(select_resp_pipe) == -1)
5928 err(1, "pipe error");
5930 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
5931 err(1, "could not disable SIGPIPE");
5932 switch (*pid = fork()) {
5934 err(1, "cannot fork");
5937 if (dup2(select_list_pipe[0], STDIN_FILENO) == -1)
5939 if (dup2(select_resp_pipe[1], STDOUT_FILENO) == -1)
5941 close(select_list_pipe[1]);
5942 close(select_resp_pipe[0]);
5943 spawn(r->ws->idx, &a, 0);
5945 default: /* parent */
5946 close(select_list_pipe[0]);
5947 close(select_resp_pipe[1]);
5951 for (i = 0; i < spawn_argc; i++)
5957 spawn_insert(const char *name, const char *args, int flags)
5959 char *arg, *cp, *ptr;
5960 struct spawn_prog *sp;
5962 DNPRINTF(SWM_D_SPAWN, "spawn_insert: %s\n", name);
5964 if ((sp = calloc(1, sizeof *sp)) == NULL)
5965 err(1, "spawn_insert: calloc");
5966 if ((sp->name = strdup(name)) == NULL)
5967 err(1, "spawn_insert: strdup");
5969 /* convert the arguments to an argument list */
5970 if ((ptr = cp = strdup(args)) == NULL)
5971 err(1, "spawn_insert: strdup");
5972 while ((arg = strsep(&ptr, " \t")) != NULL) {
5973 /* empty field; skip it */
5978 if ((sp->argv = realloc(sp->argv, sp->argc *
5979 sizeof *sp->argv)) == NULL)
5980 err(1, "spawn_insert: realloc");
5981 if ((sp->argv[sp->argc - 1] = strdup(arg)) == NULL)
5982 err(1, "spawn_insert: strdup");
5988 TAILQ_INSERT_TAIL(&spawns, sp, entry);
5989 DNPRINTF(SWM_D_SPAWN, "spawn_insert: leave\n");
5993 spawn_remove(struct spawn_prog *sp)
5997 DNPRINTF(SWM_D_SPAWN, "spawn_remove: %s\n", sp->name);
5999 TAILQ_REMOVE(&spawns, sp, entry);
6000 for (i = 0; i < sp->argc; i++)
6006 DNPRINTF(SWM_D_SPAWN, "spawn_remove: leave\n");
6010 setspawn(const char *name, const char *args, int flags)
6012 struct spawn_prog *sp;
6014 DNPRINTF(SWM_D_SPAWN, "setspawn: %s\n", name);
6019 /* Remove any old spawn under the same name. */
6020 TAILQ_FOREACH(sp, &spawns, entry)
6021 if (!strcmp(sp->name, name)) {
6027 spawn_insert(name, args, flags);
6029 warnx("error: setspawn: cannot find program: %s", name);
6031 DNPRINTF(SWM_D_SPAWN, "setspawn: leave\n");
6035 setconfspawn(char *selector, char *value, int flags)
6039 args = expand_tilde(value);
6041 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, args);
6043 setspawn(selector, args, flags);
6046 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done.\n");
6051 validate_spawns(void)
6053 struct spawn_prog *sp;
6054 char which[PATH_MAX];
6059 RB_FOREACH(kp, key_tree, &keys) {
6060 if (kp->funcid != KF_SPAWN_CUSTOM)
6064 TAILQ_FOREACH(sp, &spawns, entry) {
6065 if (!strcasecmp(kp->spawn_name, sp->name))
6069 if (sp == NULL || sp->flags & SWM_SPAWN_OPTIONAL)
6072 /* verify we have the goods */
6073 snprintf(which, sizeof which, "which %s", sp->argv[0]);
6074 DNPRINTF(SWM_D_CONF, "validate_spawns: which %s\n",
6076 for (i = strlen("which "); i < strlen(which); i++)
6077 if (which[i] == ' ') {
6081 if (system(which) != 0)
6082 add_startup_exception("could not find %s",
6083 &which[strlen("which ")]);
6090 setconfspawn("lock", "xlock", 0);
6092 setconfspawn("term", "xterm", 0);
6093 setconfspawn("spawn_term", "xterm", 0);
6095 setconfspawn("menu", "dmenu_run"
6098 " -nf $bar_font_color"
6100 " -sf $bar_color", 0);
6102 setconfspawn("search", "dmenu"
6106 " -nf $bar_font_color"
6108 " -sf $bar_color", 0);
6110 setconfspawn("name_workspace", "dmenu"
6114 " -nf $bar_font_color"
6116 " -sf $bar_color", 0);
6118 /* These are not verified for existence, even with a binding set. */
6119 setconfspawn("screenshot_all", "screenshot.sh full", SWM_SPAWN_OPTIONAL);
6120 setconfspawn("screenshot_wind", "screenshot.sh window", SWM_SPAWN_OPTIONAL);
6121 setconfspawn("initscr", "initscreen.sh", SWM_SPAWN_OPTIONAL);
6125 #define SWM_MODNAME_SIZE 32
6126 #define SWM_KEY_WS "\n+ \t"
6128 parsekeys(char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
6132 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
6133 if (mod == NULL || ks == NULL) {
6134 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
6137 if (keystr == NULL || strlen(keystr) == 0) {
6138 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
6142 *ks = XCB_NO_SYMBOL;
6144 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
6145 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
6147 cp += (long)strspn(cp, SWM_KEY_WS);
6148 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
6150 else if (!strncasecmp(name, "Mod1", SWM_MODNAME_SIZE))
6151 *mod |= XCB_MOD_MASK_1;
6152 else if (!strncasecmp(name, "Mod2", SWM_MODNAME_SIZE))
6153 *mod += XCB_MOD_MASK_2;
6154 else if (!strncmp(name, "Mod3", SWM_MODNAME_SIZE))
6155 *mod |= XCB_MOD_MASK_3;
6156 else if (!strncmp(name, "Mod4", SWM_MODNAME_SIZE))
6157 *mod |= XCB_MOD_MASK_4;
6158 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
6159 *mod |= XCB_MOD_MASK_SHIFT;
6160 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
6161 *mod |= XCB_MOD_MASK_CONTROL;
6163 *ks = XStringToKeysym(name);
6164 XConvertCase(*ks, ks, &uks);
6165 if (ks == XCB_NO_SYMBOL) {
6167 "parsekeys: invalid key %s\n",
6173 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
6178 strdupsafe(const char *str)
6183 return (strdup(str));
6187 key_insert(unsigned int mod, KeySym ks, enum keyfuncid kfid,
6188 const char *spawn_name)
6192 DNPRINTF(SWM_D_KEY, "key_insert: enter %s [%s]\n",
6193 keyfuncs[kfid].name, spawn_name);
6195 if ((kp = malloc(sizeof *kp)) == NULL)
6196 err(1, "key_insert: malloc");
6201 kp->spawn_name = strdupsafe(spawn_name);
6202 RB_INSERT(key_tree, &keys, kp);
6204 DNPRINTF(SWM_D_KEY, "key_insert: leave\n");
6208 key_lookup(unsigned int mod, KeySym ks)
6215 return (RB_FIND(key_tree, &keys, &kp));
6219 key_remove(struct key *kp)
6221 DNPRINTF(SWM_D_KEY, "key_remove: %s\n", keyfuncs[kp->funcid].name);
6223 RB_REMOVE(key_tree, &keys, kp);
6224 free(kp->spawn_name);
6227 DNPRINTF(SWM_D_KEY, "key_remove: leave\n");
6231 key_replace(struct key *kp, unsigned int mod, KeySym ks, enum keyfuncid kfid,
6232 const char *spawn_name)
6234 DNPRINTF(SWM_D_KEY, "key_replace: %s [%s]\n", keyfuncs[kp->funcid].name,
6238 key_insert(mod, ks, kfid, spawn_name);
6240 DNPRINTF(SWM_D_KEY, "key_replace: leave\n");
6244 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid,
6245 const char *spawn_name)
6249 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
6250 keyfuncs[kfid].name, spawn_name);
6252 if ((kp = key_lookup(mod, ks)) != NULL) {
6253 if (kfid == KF_INVALID)
6256 key_replace(kp, mod, ks, kfid, spawn_name);
6257 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
6260 if (kfid == KF_INVALID) {
6261 warnx("bind: Key combination already unbound.");
6262 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
6266 key_insert(mod, ks, kfid, spawn_name);
6267 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
6271 setconfbinding(char *selector, char *value, int flags)
6273 enum keyfuncid kfid;
6276 struct spawn_prog *sp;
6278 /* suppress unused warning since var is needed */
6281 DNPRINTF(SWM_D_KEY, "setconfbinding: enter\n");
6282 if (selector == NULL) {
6283 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
6284 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
6286 setkeybinding(mod, ks, kfid, NULL);
6291 /* search by key function name */
6292 for (kfid = 0; kfid < KF_INVALID; (kfid)++) {
6293 if (strncasecmp(selector, keyfuncs[kfid].name,
6294 SWM_FUNCNAME_LEN) == 0) {
6295 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
6297 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
6298 setkeybinding(mod, ks, kfid, NULL);
6304 /* search by custom spawn name */
6305 TAILQ_FOREACH(sp, &spawns, entry) {
6306 if (strcasecmp(selector, sp->name) == 0) {
6307 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
6309 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
6310 setkeybinding(mod, ks, KF_SPAWN_CUSTOM,
6312 /* Custom binding; validate spawn. */
6313 sp->flags ^= SWM_SPAWN_OPTIONAL;
6319 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
6326 #define MODKEY_SHIFT MODKEY | XCB_MOD_MASK_SHIFT
6327 setkeybinding(MODKEY, XK_b, KF_BAR_TOGGLE, NULL);
6328 setkeybinding(MODKEY_SHIFT, XK_b, KF_BAR_TOGGLE_WS,NULL);
6329 setkeybinding(MODKEY, XK_v, KF_BUTTON2, NULL);
6330 setkeybinding(MODKEY, XK_space, KF_CYCLE_LAYOUT,NULL);
6331 setkeybinding(MODKEY_SHIFT, XK_backslash, KF_FLIP_LAYOUT, NULL);
6332 setkeybinding(MODKEY, XK_t, KF_FLOAT_TOGGLE,NULL);
6333 setkeybinding(MODKEY, XK_m, KF_FOCUS_MAIN, NULL);
6334 setkeybinding(MODKEY, XK_j, KF_FOCUS_NEXT, NULL);
6335 setkeybinding(MODKEY, XK_Tab, KF_FOCUS_NEXT, NULL);
6336 setkeybinding(MODKEY, XK_k, KF_FOCUS_PREV, NULL);
6337 setkeybinding(MODKEY_SHIFT, XK_Tab, KF_FOCUS_PREV, NULL);
6338 setkeybinding(MODKEY_SHIFT, XK_equal, KF_HEIGHT_GROW,NULL);
6339 setkeybinding(MODKEY_SHIFT, XK_minus, KF_HEIGHT_SHRINK,NULL);
6340 setkeybinding(MODKEY, XK_w, KF_ICONIFY, NULL);
6341 setkeybinding(MODKEY, XK_h, KF_MASTER_SHRINK, NULL);
6342 setkeybinding(MODKEY, XK_l, KF_MASTER_GROW, NULL);
6343 setkeybinding(MODKEY, XK_comma, KF_MASTER_ADD, NULL);
6344 setkeybinding(MODKEY, XK_period, KF_MASTER_DEL, NULL);
6345 setkeybinding(MODKEY_SHIFT, XK_bracketright,KF_MOVE_DOWN,NULL);
6346 setkeybinding(MODKEY, XK_bracketleft, KF_MOVE_LEFT,NULL);
6347 setkeybinding(MODKEY, XK_bracketright,KF_MOVE_RIGHT,NULL);
6348 setkeybinding(MODKEY_SHIFT, XK_bracketleft, KF_MOVE_UP, NULL);
6349 setkeybinding(MODKEY_SHIFT, XK_KP_End, KF_MVRG_1, NULL);
6350 setkeybinding(MODKEY_SHIFT, XK_KP_Down, KF_MVRG_2, NULL);
6351 setkeybinding(MODKEY_SHIFT, XK_KP_Next, KF_MVRG_3, NULL);
6352 setkeybinding(MODKEY_SHIFT, XK_KP_Left, KF_MVRG_4, NULL);
6353 setkeybinding(MODKEY_SHIFT, XK_KP_Begin, KF_MVRG_5, NULL);
6354 setkeybinding(MODKEY_SHIFT, XK_KP_Right, KF_MVRG_6, NULL);
6355 setkeybinding(MODKEY_SHIFT, XK_KP_Home, KF_MVRG_7, NULL);
6356 setkeybinding(MODKEY_SHIFT, XK_KP_Up, KF_MVRG_8, NULL);
6357 setkeybinding(MODKEY_SHIFT, XK_KP_Prior, KF_MVRG_9, NULL);
6358 setkeybinding(MODKEY_SHIFT, XK_1, KF_MVWS_1, NULL);
6359 setkeybinding(MODKEY_SHIFT, XK_2, KF_MVWS_2, NULL);
6360 setkeybinding(MODKEY_SHIFT, XK_3, KF_MVWS_3, NULL);
6361 setkeybinding(MODKEY_SHIFT, XK_4, KF_MVWS_4, NULL);
6362 setkeybinding(MODKEY_SHIFT, XK_5, KF_MVWS_5, NULL);
6363 setkeybinding(MODKEY_SHIFT, XK_6, KF_MVWS_6, NULL);
6364 setkeybinding(MODKEY_SHIFT, XK_7, KF_MVWS_7, NULL);
6365 setkeybinding(MODKEY_SHIFT, XK_8, KF_MVWS_8, NULL);
6366 setkeybinding(MODKEY_SHIFT, XK_9, KF_MVWS_9, NULL);
6367 setkeybinding(MODKEY_SHIFT, XK_0, KF_MVWS_10, NULL);
6368 setkeybinding(MODKEY_SHIFT, XK_F1, KF_MVWS_11, NULL);
6369 setkeybinding(MODKEY_SHIFT, XK_F2, KF_MVWS_12, NULL);
6370 setkeybinding(MODKEY_SHIFT, XK_F3, KF_MVWS_13, NULL);
6371 setkeybinding(MODKEY_SHIFT, XK_F4, KF_MVWS_14, NULL);
6372 setkeybinding(MODKEY_SHIFT, XK_F5, KF_MVWS_15, NULL);
6373 setkeybinding(MODKEY_SHIFT, XK_F6, KF_MVWS_16, NULL);
6374 setkeybinding(MODKEY_SHIFT, XK_F7, KF_MVWS_17, NULL);
6375 setkeybinding(MODKEY_SHIFT, XK_F8, KF_MVWS_18, NULL);
6376 setkeybinding(MODKEY_SHIFT, XK_F9, KF_MVWS_19, NULL);
6377 setkeybinding(MODKEY_SHIFT, XK_F10, KF_MVWS_20, NULL);
6378 setkeybinding(MODKEY_SHIFT, XK_F11, KF_MVWS_21, NULL);
6379 setkeybinding(MODKEY_SHIFT, XK_F12, KF_MVWS_22, NULL);
6380 setkeybinding(MODKEY_SHIFT, XK_slash, KF_NAME_WORKSPACE,NULL);
6381 setkeybinding(MODKEY_SHIFT, XK_q, KF_QUIT, NULL);
6382 setkeybinding(MODKEY_SHIFT, XK_r, KF_RAISE_TOGGLE,NULL);
6383 setkeybinding(MODKEY, XK_q, KF_RESTART, NULL);
6384 setkeybinding(MODKEY, XK_KP_End, KF_RG_1, NULL);
6385 setkeybinding(MODKEY, XK_KP_Down, KF_RG_2, NULL);
6386 setkeybinding(MODKEY, XK_KP_Next, KF_RG_3, NULL);
6387 setkeybinding(MODKEY, XK_KP_Left, KF_RG_4, NULL);
6388 setkeybinding(MODKEY, XK_KP_Begin, KF_RG_5, NULL);
6389 setkeybinding(MODKEY, XK_KP_Right, KF_RG_6, NULL);
6390 setkeybinding(MODKEY, XK_KP_Home, KF_RG_7, NULL);
6391 setkeybinding(MODKEY, XK_KP_Up, KF_RG_8, NULL);
6392 setkeybinding(MODKEY, XK_KP_Prior, KF_RG_9, NULL);
6393 setkeybinding(MODKEY_SHIFT, XK_Right, KF_RG_NEXT, NULL);
6394 setkeybinding(MODKEY_SHIFT, XK_Left, KF_RG_PREV, NULL);
6395 setkeybinding(MODKEY, XK_f, KF_SEARCH_WIN, NULL);
6396 setkeybinding(MODKEY, XK_slash, KF_SEARCH_WORKSPACE,NULL);
6397 setkeybinding(MODKEY_SHIFT, XK_i, KF_SPAWN_CUSTOM,"initscr");
6398 setkeybinding(MODKEY_SHIFT, XK_Delete, KF_SPAWN_CUSTOM,"lock");
6399 setkeybinding(MODKEY, XK_p, KF_SPAWN_CUSTOM,"menu");
6400 setkeybinding(MODKEY, XK_s, KF_SPAWN_CUSTOM,"screenshot_all");
6401 setkeybinding(MODKEY_SHIFT, XK_s, KF_SPAWN_CUSTOM,"screenshot_wind");
6402 setkeybinding(MODKEY_SHIFT, XK_Return, KF_SPAWN_CUSTOM,"term");
6403 setkeybinding(MODKEY_SHIFT, XK_comma, KF_STACK_INC, NULL);
6404 setkeybinding(MODKEY_SHIFT, XK_period, KF_STACK_DEC, NULL);
6405 setkeybinding(MODKEY_SHIFT, XK_space, KF_STACK_RESET, NULL);
6406 setkeybinding(MODKEY, XK_Return, KF_SWAP_MAIN, NULL);
6407 setkeybinding(MODKEY_SHIFT, XK_j, KF_SWAP_NEXT, NULL);
6408 setkeybinding(MODKEY_SHIFT, XK_k, KF_SWAP_PREV, NULL);
6409 setkeybinding(MODKEY_SHIFT, XK_w, KF_UNICONIFY, NULL);
6410 setkeybinding(MODKEY_SHIFT, XK_v, KF_VERSION, NULL);
6411 setkeybinding(MODKEY, XK_equal, KF_WIDTH_GROW, NULL);
6412 setkeybinding(MODKEY, XK_minus, KF_WIDTH_SHRINK,NULL);
6413 setkeybinding(MODKEY, XK_x, KF_WIND_DEL, NULL);
6414 setkeybinding(MODKEY_SHIFT, XK_x, KF_WIND_KILL, NULL);
6415 setkeybinding(MODKEY, XK_1, KF_WS_1, NULL);
6416 setkeybinding(MODKEY, XK_2, KF_WS_2, NULL);
6417 setkeybinding(MODKEY, XK_3, KF_WS_3, NULL);
6418 setkeybinding(MODKEY, XK_4, KF_WS_4, NULL);
6419 setkeybinding(MODKEY, XK_5, KF_WS_5, NULL);
6420 setkeybinding(MODKEY, XK_6, KF_WS_6, NULL);
6421 setkeybinding(MODKEY, XK_7, KF_WS_7, NULL);
6422 setkeybinding(MODKEY, XK_8, KF_WS_8, NULL);
6423 setkeybinding(MODKEY, XK_9, KF_WS_9, NULL);
6424 setkeybinding(MODKEY, XK_0, KF_WS_10, NULL);
6425 setkeybinding(MODKEY, XK_F1, KF_WS_11, NULL);
6426 setkeybinding(MODKEY, XK_F2, KF_WS_12, NULL);
6427 setkeybinding(MODKEY, XK_F3, KF_WS_13, NULL);
6428 setkeybinding(MODKEY, XK_F4, KF_WS_14, NULL);
6429 setkeybinding(MODKEY, XK_F5, KF_WS_15, NULL);
6430 setkeybinding(MODKEY, XK_F6, KF_WS_16, NULL);
6431 setkeybinding(MODKEY, XK_F7, KF_WS_17, NULL);
6432 setkeybinding(MODKEY, XK_F8, KF_WS_18, NULL);
6433 setkeybinding(MODKEY, XK_F9, KF_WS_19, NULL);
6434 setkeybinding(MODKEY, XK_F10, KF_WS_20, NULL);
6435 setkeybinding(MODKEY, XK_F11, KF_WS_21, NULL);
6436 setkeybinding(MODKEY, XK_F12, KF_WS_22, NULL);
6437 setkeybinding(MODKEY, XK_Right, KF_WS_NEXT, NULL);
6438 setkeybinding(MODKEY, XK_Left, KF_WS_PREV, NULL);
6439 setkeybinding(MODKEY, XK_Up, KF_WS_NEXT_ALL, NULL);
6440 setkeybinding(MODKEY, XK_Down, KF_WS_PREV_ALL, NULL);
6441 setkeybinding(MODKEY, XK_a, KF_WS_PRIOR, NULL);
6443 setkeybinding(MODKEY_SHIFT, XK_d, KF_DUMPWINS, NULL);
6453 while (RB_EMPTY(&keys) == 0) {
6454 kp = RB_ROOT(&keys);
6460 setkeymapping(char *selector, char *value, int flags)
6462 char keymapping_file[PATH_MAX];
6464 /* suppress unused warnings since vars are needed */
6468 DNPRINTF(SWM_D_KEY, "setkeymapping: enter\n");
6469 if (value[0] == '~')
6470 snprintf(keymapping_file, sizeof keymapping_file, "%s/%s",
6471 pwd->pw_dir, &value[1]);
6473 strlcpy(keymapping_file, value, sizeof keymapping_file);
6475 /* load new key bindings; if it fails, revert to default bindings */
6476 if (conf_load(keymapping_file, SWM_CONF_KEYMAPPING)) {
6480 DNPRINTF(SWM_D_KEY, "setkeymapping: leave\n");
6485 updatenumlockmask(void)
6488 xcb_get_modifier_mapping_reply_t *modmap_r;
6489 xcb_keycode_t *modmap, kc, *keycode;
6493 modmap_r = xcb_get_modifier_mapping_reply(conn,
6494 xcb_get_modifier_mapping(conn),
6497 modmap = xcb_get_modifier_mapping_keycodes(modmap_r);
6498 for (i = 0; i < 8; i++) {
6499 for (j = 0; j < modmap_r->keycodes_per_modifier; j++) {
6500 kc = modmap[i * modmap_r->keycodes_per_modifier
6502 keycode = xcb_key_symbols_get_keycode(syms,
6505 numlockmask = (1 << i);
6511 DNPRINTF(SWM_D_MISC, "updatenumlockmask: %d\n", numlockmask);
6518 int num_screens, k, j;
6519 unsigned int modifiers[4];
6520 xcb_keycode_t *code;
6522 DNPRINTF(SWM_D_MISC, "grabkeys\n");
6523 updatenumlockmask();
6526 modifiers[1] = numlockmask;
6527 modifiers[2] = XCB_MOD_MASK_LOCK;
6528 modifiers[3] = numlockmask | XCB_MOD_MASK_LOCK;
6530 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
6531 for (k = 0; k < num_screens; k++) {
6532 if (TAILQ_EMPTY(&screens[k].rl))
6534 xcb_ungrab_key(conn, XCB_GRAB_ANY, screens[k].root,
6536 RB_FOREACH(kp, key_tree, &keys) {
6537 if ((code = xcb_key_symbols_get_keycode(syms,
6539 for (j = 0; j < LENGTH(modifiers); j++)
6540 xcb_grab_key(conn, 1,
6542 kp->mod | modifiers[j],
6543 *code, XCB_GRAB_MODE_SYNC,
6544 XCB_GRAB_MODE_SYNC);
6552 grabbuttons(struct ws_win *win)
6554 unsigned int modifiers[4];
6557 DNPRINTF(SWM_D_MOUSE, "grabbuttons: win 0x%x\n", win->id);
6558 updatenumlockmask();
6561 modifiers[1] = numlockmask;
6562 modifiers[2] = XCB_MOD_MASK_LOCK;
6563 modifiers[3] = numlockmask | XCB_MOD_MASK_LOCK;
6565 for (i = 0; i < LENGTH(buttons); i++)
6566 if (buttons[i].action == client_click)
6567 for (j = 0; j < LENGTH(modifiers); ++j)
6568 xcb_grab_button(conn, 0, win->id, BUTTONMASK,
6569 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC,
6570 XCB_WINDOW_NONE, XCB_CURSOR_NONE,
6571 buttons[i].button, buttons[i].mask |
6575 const char *quirkname[] = {
6576 "NONE", /* config string for "no value" */
6585 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
6586 #define SWM_Q_WS "\n|+ \t"
6588 parsequirks(char *qstr, unsigned long *quirk)
6598 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
6600 cp += (long)strspn(cp, SWM_Q_WS);
6601 for (i = 0; i < LENGTH(quirkname); i++) {
6602 if (!strncasecmp(name, quirkname[i], SWM_QUIRK_LEN)) {
6603 DNPRINTF(SWM_D_QUIRK,
6604 "parsequirks: %s\n", name);
6609 *quirk |= 1 << (i-1);
6613 if (i >= LENGTH(quirkname)) {
6614 DNPRINTF(SWM_D_QUIRK,
6615 "parsequirks: invalid quirk [%s]\n", name);
6623 quirk_insert(const char *class, const char *name, unsigned long quirk)
6627 DNPRINTF(SWM_D_QUIRK, "quirk_insert: %s:%s [%lu]\n", class, name,
6630 if ((qp = malloc(sizeof *qp)) == NULL)
6631 err(1, "quirk_insert: malloc");
6632 if ((qp->class = strdup(class)) == NULL)
6633 err(1, "quirk_insert: strdup");
6634 if ((qp->name = strdup(name)) == NULL)
6635 err(1, "quirk_insert: strdup");
6638 TAILQ_INSERT_TAIL(&quirks, qp, entry);
6640 DNPRINTF(SWM_D_QUIRK, "quirk_insert: leave\n");
6644 quirk_remove(struct quirk *qp)
6646 DNPRINTF(SWM_D_QUIRK, "quirk_remove: %s:%s [%lu]\n", qp->class,
6647 qp->name, qp->quirk);
6649 TAILQ_REMOVE(&quirks, qp, entry);
6654 DNPRINTF(SWM_D_QUIRK, "quirk_remove: leave\n");
6658 quirk_replace(struct quirk *qp, const char *class, const char *name,
6659 unsigned long quirk)
6661 DNPRINTF(SWM_D_QUIRK, "quirk_replace: %s:%s [%lu]\n", qp->class,
6662 qp->name, qp->quirk);
6665 quirk_insert(class, name, quirk);
6667 DNPRINTF(SWM_D_QUIRK, "quirk_replace: leave\n");
6671 setquirk(const char *class, const char *name, unsigned long quirk)
6675 DNPRINTF(SWM_D_QUIRK, "setquirk: enter %s:%s [%lu]\n", class, name,
6678 /* Remove/replace existing quirk. */
6679 TAILQ_FOREACH(qp, &quirks, entry) {
6680 if (!strcmp(qp->class, class) && !strcmp(qp->name, name)) {
6684 quirk_replace(qp, class, name, quirk);
6685 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
6690 /* Only insert if quirk is not NONE. */
6692 quirk_insert(class, name, quirk);
6694 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
6698 setconfquirk(char *selector, char *value, int flags)
6700 char *cp, *class, *name;
6704 /* suppress unused warning since var is needed */
6707 if (selector == NULL)
6709 if ((cp = strchr(selector, ':')) == NULL)
6714 if ((retval = parsequirks(value, &qrks)) == 0)
6715 setquirk(class, name, qrks);
6722 setquirk("MPlayer", "xv", SWM_Q_FLOAT | SWM_Q_FULLSCREEN | SWM_Q_FOCUSPREV);
6723 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT);
6724 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ);
6725 setquirk("Firefox", "Dialog", SWM_Q_FLOAT);
6726 setquirk("Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
6727 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ);
6728 setquirk("xine", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
6729 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
6730 setquirk("xine", "xine Panel", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
6731 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
6732 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
6733 setquirk("pcb", "pcb", SWM_Q_FLOAT);
6734 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
6737 /* conf file stuff */
6738 #define SWM_CONF_FILE "spectrwm.conf"
6739 #define SWM_CONF_FILE_OLD "scrotwm.conf"
6743 SWM_S_BAR_AT_BOTTOM,
6744 SWM_S_BAR_BORDER_WIDTH,
6747 SWM_S_BAR_ENABLED_WS,
6752 SWM_S_CLOCK_ENABLED,
6755 SWM_S_CYCLE_VISIBLE,
6757 SWM_S_DISABLE_BORDER,
6759 SWM_S_FOCUS_CLOSE_WRAP,
6760 SWM_S_FOCUS_DEFAULT,
6762 SWM_S_REGION_PADDING,
6767 SWM_S_STACK_ENABLED,
6770 SWM_S_TITLE_CLASS_ENABLED,
6771 SWM_S_TITLE_NAME_ENABLED,
6772 SWM_S_URGENT_ENABLED,
6773 SWM_S_VERBOSE_LAYOUT,
6774 SWM_S_WINDOW_NAME_ENABLED,
6775 SWM_S_WORKSPACE_LIMIT
6779 setconfvalue(char *selector, char *value, int flags)
6781 struct workspace *ws;
6782 int i, ws_id, num_screens;
6785 /* suppress unused warning since var is needed */
6789 case SWM_S_BAR_ACTION:
6791 if ((bar_argv[0] = expand_tilde(value)) == NULL)
6792 err(1, "setconfvalue: bar_action");
6794 case SWM_S_BAR_AT_BOTTOM:
6795 bar_at_bottom = atoi(value);
6797 case SWM_S_BAR_BORDER_WIDTH:
6798 bar_border_width = atoi(value);
6799 if (bar_border_width < 0)
6800 bar_border_width = 0;
6802 case SWM_S_BAR_DELAY:
6803 /* No longer needed; leave to not break old conf files. */
6805 case SWM_S_BAR_ENABLED:
6806 bar_enabled = atoi(value);
6808 case SWM_S_BAR_ENABLED_WS:
6809 ws_id = atoi(selector) - 1;
6810 if (ws_id < 0 || ws_id >= workspace_limit)
6811 errx(1, "setconfvalue: bar_enabled_ws: invalid "
6812 "workspace %d.", ws_id + 1);
6814 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
6815 for (i = 0; i < num_screens; i++) {
6816 ws = (struct workspace *)&screens[i].ws;
6817 ws[ws_id].bar_enabled = atoi(value);
6820 case SWM_S_BAR_FONT:
6822 if (asprintf(&bar_fonts, "%s,%s", value, bar_fonts) == -1)
6823 err(1, "setconfvalue: asprintf: failed to allocate "
6824 "memory for bar_fonts.");
6827 /* If already in xft mode, then we are done. */
6828 if (!bar_font_legacy)
6831 /* If there are any non-XLFD entries, switch to Xft mode. */
6832 while ((b = strsep(&value, ",")) != NULL) {
6836 bar_font_legacy = 0;
6841 case SWM_S_BAR_FORMAT:
6843 if ((bar_format = strdup(value)) == NULL)
6844 err(1, "setconfvalue: bar_format");
6846 case SWM_S_BAR_JUSTIFY:
6847 if (!strcmp(value, "left"))
6848 bar_justify = SWM_BAR_JUSTIFY_LEFT;
6849 else if (!strcmp(value, "center"))
6850 bar_justify = SWM_BAR_JUSTIFY_CENTER;
6851 else if (!strcmp(value, "right"))
6852 bar_justify = SWM_BAR_JUSTIFY_RIGHT;
6854 errx(1, "invalid bar_justify");
6856 case SWM_S_BORDER_WIDTH:
6857 border_width = atoi(value);
6858 if (border_width < 0)
6861 case SWM_S_CLOCK_ENABLED:
6862 clock_enabled = atoi(value);
6864 case SWM_S_CLOCK_FORMAT:
6865 #ifndef SWM_DENY_CLOCK_FORMAT
6867 if ((clock_format = strdup(value)) == NULL)
6868 err(1, "setconfvalue: clock_format");
6871 case SWM_S_CYCLE_EMPTY:
6872 cycle_empty = atoi(value);
6874 case SWM_S_CYCLE_VISIBLE:
6875 cycle_visible = atoi(value);
6877 case SWM_S_DIALOG_RATIO:
6878 dialog_ratio = atof(value);
6879 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
6882 case SWM_S_DISABLE_BORDER:
6883 disable_border = atoi(value);
6885 case SWM_S_FOCUS_CLOSE:
6886 if (!strcmp(value, "first"))
6887 focus_close = SWM_STACK_BOTTOM;
6888 else if (!strcmp(value, "last"))
6889 focus_close = SWM_STACK_TOP;
6890 else if (!strcmp(value, "next"))
6891 focus_close = SWM_STACK_ABOVE;
6892 else if (!strcmp(value, "previous"))
6893 focus_close = SWM_STACK_BELOW;
6895 errx(1, "focus_close");
6897 case SWM_S_FOCUS_CLOSE_WRAP:
6898 focus_close_wrap = atoi(value);
6900 case SWM_S_FOCUS_DEFAULT:
6901 if (!strcmp(value, "last"))
6902 focus_default = SWM_STACK_TOP;
6903 else if (!strcmp(value, "first"))
6904 focus_default = SWM_STACK_BOTTOM;
6906 errx(1, "focus_default");
6908 case SWM_S_FOCUS_MODE:
6909 if (!strcmp(value, "default"))
6910 focus_mode = SWM_FOCUS_DEFAULT;
6911 else if (!strcmp(value, "follow") ||
6912 !strcmp(value, "follow_cursor"))
6913 focus_mode = SWM_FOCUS_FOLLOW;
6914 else if (!strcmp(value, "manual"))
6915 focus_mode = SWM_FOCUS_MANUAL;
6917 errx(1, "focus_mode");
6919 case SWM_S_REGION_PADDING:
6920 region_padding = atoi(value);
6921 if (region_padding < 0)
6924 case SWM_S_SPAWN_ORDER:
6925 if (!strcmp(value, "first"))
6926 spawn_position = SWM_STACK_BOTTOM;
6927 else if (!strcmp(value, "last"))
6928 spawn_position = SWM_STACK_TOP;
6929 else if (!strcmp(value, "next"))
6930 spawn_position = SWM_STACK_ABOVE;
6931 else if (!strcmp(value, "previous"))
6932 spawn_position = SWM_STACK_BELOW;
6934 errx(1, "spawn_position");
6936 case SWM_S_SPAWN_TERM:
6937 setconfspawn("term", value, 0);
6938 setconfspawn("spawn_term", value, 0);
6941 /* No longer needed; leave to not break old conf files. */
6943 case SWM_S_SS_ENABLED:
6944 /* No longer needed; leave to not break old conf files. */
6946 case SWM_S_STACK_ENABLED:
6947 stack_enabled = atoi(value);
6949 case SWM_S_TERM_WIDTH:
6950 term_width = atoi(value);
6954 case SWM_S_TILE_GAP:
6955 tile_gap = atoi(value);
6959 case SWM_S_TITLE_CLASS_ENABLED:
6960 title_class_enabled = atoi(value);
6962 case SWM_S_TITLE_NAME_ENABLED:
6963 title_name_enabled = atoi(value);
6965 case SWM_S_URGENT_ENABLED:
6966 urgent_enabled = atoi(value);
6968 case SWM_S_VERBOSE_LAYOUT:
6969 verbose_layout = atoi(value);
6970 for (i = 0; layouts[i].l_stack != NULL; i++) {
6972 layouts[i].l_string = fancy_stacker;
6974 layouts[i].l_string = plain_stacker;
6977 case SWM_S_WINDOW_NAME_ENABLED:
6978 window_name_enabled = atoi(value);
6980 case SWM_S_WORKSPACE_LIMIT:
6981 workspace_limit = atoi(value);
6982 if (workspace_limit > SWM_WS_MAX)
6983 workspace_limit = SWM_WS_MAX;
6984 else if (workspace_limit < 1)
6985 workspace_limit = 1;
6994 setconfmodkey(char *selector, char *value, int flags)
6996 /* suppress unused warnings since vars are needed */
7000 if (!strncasecmp(value, "Mod1", strlen("Mod1")))
7001 update_modkey(XCB_MOD_MASK_1);
7002 else if (!strncasecmp(value, "Mod2", strlen("Mod2")))
7003 update_modkey(XCB_MOD_MASK_2);
7004 else if (!strncasecmp(value, "Mod3", strlen("Mod3")))
7005 update_modkey(XCB_MOD_MASK_3);
7006 else if (!strncasecmp(value, "Mod4", strlen("Mod4")))
7007 update_modkey(XCB_MOD_MASK_4);
7014 setconfcolor(char *selector, char *value, int flags)
7016 setscreencolor(value, ((selector == NULL)?-1:atoi(selector)), flags);
7021 setconfregion(char *selector, char *value, int flags)
7023 /* suppress unused warnings since vars are needed */
7027 custom_region(value);
7032 setautorun(char *selector, char *value, int flags)
7042 /* suppress unused warnings since vars are needed */
7046 if (getenv("SWM_STARTED"))
7050 if (sscanf(value, "ws[%d]:%1023c", &ws_id, s) != 2)
7051 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
7053 if (ws_id < 0 || ws_id >= workspace_limit)
7054 errx(1, "autorun: invalid workspace %d", ws_id + 1);
7056 sp = expand_tilde((char *)&s);
7059 * This is a little intricate
7061 * If the pid already exists we simply reuse it because it means it was
7062 * used before AND not claimed by manage_window. We get away with
7063 * altering it in the parent after INSERT because this can not be a race
7066 while ((ap = strsep(&sp, " \t")) != NULL) {
7069 DNPRINTF(SWM_D_SPAWN, "setautorun: arg [%s]\n", ap);
7071 if ((a.argv = realloc(a.argv, argc * sizeof(char *))) == NULL)
7072 err(1, "setautorun: realloc");
7073 a.argv[argc - 1] = ap;
7077 if ((a.argv = realloc(a.argv, (argc + 1) * sizeof(char *))) == NULL)
7078 err(1, "setautorun: realloc");
7079 a.argv[argc] = NULL;
7081 if ((pid = fork()) == 0) {
7082 spawn(ws_id, &a, 1);
7091 p = calloc(1, sizeof *p);
7094 TAILQ_INSERT_TAIL(&pidlist, p, entry);
7104 setlayout(char *selector, char *value, int flags)
7106 int ws_id, i, x, mg, ma, si, ar, f = 0;
7107 int st = SWM_V_STACK, num_screens;
7109 struct workspace *ws;
7111 /* suppress unused warnings since vars are needed */
7115 if (getenv("SWM_STARTED"))
7119 if (sscanf(value, "ws[%d]:%d:%d:%d:%d:%1023c",
7120 &ws_id, &mg, &ma, &si, &ar, s) != 6)
7121 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
7122 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
7125 if (ws_id < 0 || ws_id >= workspace_limit)
7126 errx(1, "layout: invalid workspace %d", ws_id + 1);
7128 if (!strcasecmp(s, "vertical"))
7130 else if (!strcasecmp(s, "vertical_flip")) {
7133 } else if (!strcasecmp(s, "horizontal"))
7135 else if (!strcasecmp(s, "horizontal_flip")) {
7138 } else if (!strcasecmp(s, "fullscreen"))
7141 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
7142 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
7145 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7146 for (i = 0; i < num_screens; i++) {
7147 ws = (struct workspace *)&screens[i].ws;
7148 ws[ws_id].cur_layout = &layouts[st];
7150 ws[ws_id].always_raise = ar;
7151 if (st == SWM_MAX_STACK)
7155 for (x = 0; x < abs(mg); x++) {
7156 ws[ws_id].cur_layout->l_config(&ws[ws_id],
7157 mg >= 0 ? SWM_ARG_ID_MASTERGROW :
7158 SWM_ARG_ID_MASTERSHRINK);
7162 for (x = 0; x < abs(ma); x++) {
7163 ws[ws_id].cur_layout->l_config(&ws[ws_id],
7164 ma >= 0 ? SWM_ARG_ID_MASTERADD :
7165 SWM_ARG_ID_MASTERDEL);
7169 for (x = 0; x < abs(si); x++) {
7170 ws[ws_id].cur_layout->l_config(&ws[ws_id],
7171 si >= 0 ? SWM_ARG_ID_STACKINC :
7172 SWM_ARG_ID_STACKDEC);
7177 ws[ws_id].cur_layout->l_config(&ws[ws_id],
7178 SWM_ARG_ID_FLIPLAYOUT);
7188 /* config options */
7189 struct config_option {
7191 int (*func)(char*, char*, int);
7194 struct config_option configopt[] = {
7195 { "autorun", setautorun, 0 },
7196 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
7197 { "bar_at_bottom", setconfvalue, SWM_S_BAR_AT_BOTTOM },
7198 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
7199 { "bar_border_unfocus", setconfcolor, SWM_S_COLOR_BAR_BORDER_UNFOCUS },
7200 { "bar_border_width", setconfvalue, SWM_S_BAR_BORDER_WIDTH },
7201 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
7202 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
7203 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
7204 { "bar_enabled_ws", setconfvalue, SWM_S_BAR_ENABLED_WS },
7205 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
7206 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
7207 { "bar_format", setconfvalue, SWM_S_BAR_FORMAT },
7208 { "bar_justify", setconfvalue, SWM_S_BAR_JUSTIFY },
7209 { "bind", setconfbinding, 0 },
7210 { "border_width", setconfvalue, SWM_S_BORDER_WIDTH },
7211 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
7212 { "clock_format", setconfvalue, SWM_S_CLOCK_FORMAT },
7213 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
7214 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
7215 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
7216 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
7217 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
7218 { "disable_border", setconfvalue, SWM_S_DISABLE_BORDER },
7219 { "focus_close", setconfvalue, SWM_S_FOCUS_CLOSE },
7220 { "focus_close_wrap", setconfvalue, SWM_S_FOCUS_CLOSE_WRAP },
7221 { "focus_default", setconfvalue, SWM_S_FOCUS_DEFAULT },
7222 { "focus_mode", setconfvalue, SWM_S_FOCUS_MODE },
7223 { "keyboard_mapping", setkeymapping, 0 },
7224 { "layout", setlayout, 0 },
7225 { "modkey", setconfmodkey, 0 },
7226 { "program", setconfspawn, 0 },
7227 { "quirk", setconfquirk, 0 },
7228 { "region", setconfregion, 0 },
7229 { "region_padding", setconfvalue, SWM_S_REGION_PADDING },
7230 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
7231 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
7232 { "spawn_position", setconfvalue, SWM_S_SPAWN_ORDER },
7233 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
7234 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
7235 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
7236 { "tile_gap", setconfvalue, SWM_S_TILE_GAP },
7237 { "title_class_enabled", setconfvalue, SWM_S_TITLE_CLASS_ENABLED },
7238 { "title_name_enabled", setconfvalue, SWM_S_TITLE_NAME_ENABLED },
7239 { "urgent_enabled", setconfvalue, SWM_S_URGENT_ENABLED },
7240 { "verbose_layout", setconfvalue, SWM_S_VERBOSE_LAYOUT },
7241 { "window_name_enabled", setconfvalue, SWM_S_WINDOW_NAME_ENABLED },
7242 { "workspace_limit", setconfvalue, SWM_S_WORKSPACE_LIMIT },
7246 _add_startup_exception(const char *fmt, va_list ap)
7248 if (vasprintf(&startup_exception, fmt, ap) == -1)
7249 warn("%s: asprintf", __func__);
7253 add_startup_exception(const char *fmt, ...)
7259 if (startup_exception)
7262 /* force bar to be enabled due to exception */
7266 _add_startup_exception(fmt, ap);
7271 conf_load(const char *filename, int keymapping)
7274 char *line = NULL, *cp, *optsub, *optval = NULL;
7275 size_t linelen, lineno = 0;
7276 int wordlen, i, optidx;
7277 struct config_option *opt = NULL;
7279 DNPRINTF(SWM_D_CONF, "conf_load: begin\n");
7281 if (filename == NULL) {
7282 warnx("conf_load: no filename");
7285 if ((config = fopen(filename, "r")) == NULL) {
7286 warn("conf_load: fopen: %s", filename);
7290 while (!feof(config)) {
7294 if ((line = fparseln(config, &linelen, &lineno, NULL,
7295 FPARSELN_UNESCCOMM | FPARSELN_UNESCCONT)) == NULL) {
7297 err(1, "%s", filename);
7302 cp += strspn(cp, " \t\n"); /* eat whitespace */
7303 if (cp[0] == '\0') {
7307 /* get config option */
7308 wordlen = strcspn(cp, "=[ \t\n");
7310 add_startup_exception("%s: line %zd: no option found",
7315 for (i = 0; i < LENGTH(configopt); i++) {
7316 opt = &configopt[i];
7317 if (!strncasecmp(cp, opt->optname, wordlen) &&
7318 (int)strlen(opt->optname) == wordlen) {
7324 add_startup_exception("%s: line %zd: unknown option "
7325 "%.*s", filename, lineno, wordlen, cp);
7328 if (keymapping && opt && strcmp(opt->optname, "bind")) {
7329 add_startup_exception("%s: line %zd: invalid option "
7330 "%.*s", filename, lineno, wordlen, cp);
7334 cp += strspn(cp, " \t\n"); /* eat whitespace */
7336 /* from here on out we call goto invalid to continue */
7338 /* get [selector] if any */
7342 wordlen = strcspn(cp, "]");
7345 add_startup_exception("%s: line %zd: "
7346 "syntax error", filename, lineno);
7350 if (asprintf(&optsub, "%.*s", wordlen, cp) ==
7352 add_startup_exception("%s: line %zd: "
7353 "unable to allocatememory for "
7354 "selector", filename, lineno);
7359 cp += strspn(cp, "] \t\n"); /* eat trailing */
7361 cp += strspn(cp, "= \t\n"); /* eat trailing */
7363 optval = strdup(cp);
7364 if (strlen(optval) == 0) {
7365 add_startup_exception("%s: line %zd: must supply value "
7366 "to %s", filename, lineno,
7367 configopt[optidx].optname);
7370 /* call function to deal with it all */
7371 if (configopt[optidx].func(optsub, optval,
7372 configopt[optidx].funcflags) != 0) {
7373 add_startup_exception("%s: line %zd: invalid data for "
7374 "%s", filename, lineno, configopt[optidx].optname);
7391 DNPRINTF(SWM_D_CONF, "conf_load: end\n");
7397 set_child_transient(struct ws_win *win, xcb_window_t *trans)
7399 struct ws_win *parent, *w;
7400 struct swm_region *r;
7401 struct workspace *ws;
7402 xcb_icccm_wm_hints_t wmh;
7404 parent = find_window(win->transient);
7406 parent->focus_child = win;
7408 DNPRINTF(SWM_D_MISC, "set_child_transient: parent doesn't exist"
7409 " for 0x%x trans 0x%x\n", win->id, win->transient);
7411 r = root_to_region(win->wa->root, SWM_CK_ALL);
7413 /* parent doen't exist in our window list */
7414 TAILQ_FOREACH(w, &ws->winlist, entry) {
7415 if (xcb_icccm_get_wm_hints_reply(conn,
7416 xcb_icccm_get_wm_hints(conn, w->id),
7418 warnx("can't get hints for 0x%x", w->id);
7422 if (win->hints.window_group != wmh.window_group)
7425 w->focus_child = win;
7426 win->transient = w->id;
7428 DNPRINTF(SWM_D_MISC, "set_child_transient: adjusting "
7429 "transient to 0x%x\n", win->transient);
7436 window_get_pid(xcb_window_t win)
7441 xcb_get_property_cookie_t pc;
7442 xcb_get_property_reply_t *pr;
7444 apid = get_atom_from_string("_NET_WM_PID");
7445 if (apid == XCB_ATOM_NONE)
7448 pc = xcb_get_property(conn, 0, win, apid, XCB_ATOM_CARDINAL, 0, 1);
7449 pr = xcb_get_property_reply(conn, pc, NULL);
7452 if (pr->type != XCB_ATOM_CARDINAL) {
7457 if (pr->type == apid && pr->format == 32)
7458 ret = *((pid_t *)xcb_get_property_value(pr));
7464 apid = get_atom_from_string("_SWM_PID");
7465 pc = xcb_get_property(conn, 0, win, apid, XCB_ATOM_STRING,
7467 pr = xcb_get_property_reply(conn, pc, NULL);
7470 if (pr->type != apid) {
7475 ret = (pid_t)strtonum(xcb_get_property_value(pr), 0, INT_MAX, &errstr);
7482 get_ws_idx(xcb_window_t id)
7488 xcb_get_property_reply_t *gpr;
7490 gpr = xcb_get_property_reply(conn,
7491 xcb_get_property(conn, 0, id, a_swm_ws,
7492 XCB_ATOM_STRING, 0, SWM_PROPLEN),
7497 proplen = xcb_get_property_value_length(gpr);
7499 prop = malloc(proplen + 1);
7502 xcb_get_property_value(gpr),
7504 prop[proplen] = '\0';
7511 DNPRINTF(SWM_D_PROP, "get_ws_idx: _SWM_WS: %s\n", prop);
7512 ws_idx = (int)strtonum(prop, 0, workspace_limit - 1, &errstr);
7514 DNPRINTF(SWM_D_PROP, "get_ws_idx: window: #%s: %s",
7524 manage_window(xcb_window_t id, uint16_t mapped)
7526 xcb_window_t trans = XCB_WINDOW_NONE;
7527 struct ws_win *win, *ww;
7529 char ws_idx_str[SWM_PROPLEN];
7530 struct swm_region *r;
7534 xcb_icccm_get_wm_protocols_reply_t wpr;
7536 if ((win = find_window(id)) != NULL) {
7537 DNPRINTF(SWM_D_MISC, "manage_window: win 0x%x already "
7538 "managed; skipping.)\n", id);
7539 return (win); /* Already managed. */
7542 /* See if window is on the unmanaged list. */
7543 if ((win = find_unmanaged_window(id)) != NULL) {
7544 DNPRINTF(SWM_D_MISC, "manage_window: win 0x%x found on "
7545 "unmanaged list.\n", id);
7546 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
7549 set_child_transient(win, &trans);
7553 DNPRINTF(SWM_D_MISC, "manage_window: win 0x%x is new.\n", id);
7556 /* Create and initialize ws_win object. */
7557 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
7558 err(1, "manage_window: calloc: failed to allocate memory for "
7563 /* Get window geometry. */
7564 win->wa = xcb_get_geometry_reply(conn,
7565 xcb_get_geometry(conn, win->id),
7568 /* Figure out which region the window belongs to. */
7569 r = root_to_region(win->wa->root, SWM_CK_ALL);
7571 /* Ignore window border if there is one. */
7572 WIDTH(win) = win->wa->width;
7573 HEIGHT(win) = win->wa->height;
7574 X(win) = win->wa->x + win->wa->border_width - border_width;
7575 Y(win) = win->wa->y + win->wa->border_width - border_width;
7577 win->mapped = mapped;
7578 win->floatmaxed = 0;
7579 win->ewmh_flags = 0;
7580 win->s = r->s; /* this never changes */
7582 store_float_geom(win, r);
7584 /* Get WM_SIZE_HINTS. */
7585 xcb_icccm_get_wm_normal_hints_reply(conn,
7586 xcb_icccm_get_wm_normal_hints(conn, win->id),
7590 xcb_icccm_get_wm_hints_reply(conn,
7591 xcb_icccm_get_wm_hints(conn, win->id),
7594 /* Get WM_TRANSIENT_FOR; see if window is a transient. */
7595 xcb_icccm_get_wm_transient_for_reply(conn,
7596 xcb_icccm_get_wm_transient_for(conn, win->id),
7599 win->transient = trans;
7600 set_child_transient(win, &win->transient);
7603 /* Get supported protocols. */
7604 if (xcb_icccm_get_wm_protocols_reply(conn,
7605 xcb_icccm_get_wm_protocols(conn, win->id, a_prot),
7607 for (i = 0; i < wpr.atoms_len; i++) {
7608 if (wpr.atoms[i] == a_takefocus)
7609 win->take_focus = 1;
7610 if (wpr.atoms[i] == a_delete)
7611 win->can_delete = 1;
7613 xcb_icccm_get_wm_protocols_reply_wipe(&wpr);
7616 win->iconic = get_swm_iconic(win);
7618 /* Figure out which workspace the window belongs to. */
7619 if ((p = find_pid(window_get_pid(win->id))) != NULL) {
7620 win->ws = &r->s->ws[p->ws];
7621 TAILQ_REMOVE(&pidlist, p, entry);
7624 } else if ((ws_idx = get_ws_idx(win->id)) != -1 &&
7626 /* _SWM_WS is set; use that. */
7627 win->ws = &r->s->ws[ws_idx];
7628 } else if (trans && (ww = find_window(trans)) != NULL) {
7629 /* Launch transients in the same ws as parent. */
7635 /* Set the _SWM_WS atom so we can remember this after reincarnation. */
7636 if (snprintf(ws_idx_str, SWM_PROPLEN, "%d", win->ws->idx) <
7638 DNPRINTF(SWM_D_PROP, "manage_window: set _SWM_WS: %s\n",
7640 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
7641 a_swm_ws, XCB_ATOM_STRING, 8, strlen(ws_idx_str),
7646 ewmh_autoquirk(win);
7648 /* Determine initial quirks. */
7649 if (xcb_icccm_get_wm_class_reply(conn,
7650 xcb_icccm_get_wm_class(conn, win->id),
7652 DNPRINTF(SWM_D_CLASS, "manage_window: class: %s, name: %s\n",
7653 win->ch.class_name, win->ch.instance_name);
7655 /* java is retarded so treat it special */
7656 if (strstr(win->ch.instance_name, "sun-awt")) {
7657 DNPRINTF(SWM_D_CLASS, "manage_window: java window "
7662 TAILQ_FOREACH(qp, &quirks, entry) {
7663 if (!strcmp(win->ch.class_name, qp->class) &&
7664 !strcmp(win->ch.instance_name, qp->name)) {
7665 DNPRINTF(SWM_D_CLASS, "manage_window: on quirks"
7666 "list; mask: 0x%lx\n", qp->quirk);
7667 if (qp->quirk & SWM_Q_FLOAT)
7669 win->quirks = qp->quirk;
7674 /* Alter window position if quirky */
7675 if (win->quirks & SWM_Q_ANYWHERE)
7678 /* Reset font sizes (the bruteforce way; no default keybinding). */
7679 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
7680 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
7681 fake_keypress(win, XK_KP_Subtract, XCB_MOD_MASK_SHIFT);
7682 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
7683 fake_keypress(win, XK_KP_Add, XCB_MOD_MASK_SHIFT);
7686 /* Make sure window is positioned inside its region, if its active. */
7688 constrain_window(win, win->ws->r, 0);
7692 /* Select which X events to monitor and set border pixel color. */
7693 wa[0] = win->s->c[SWM_S_COLOR_UNFOCUS].pixel;
7694 wa[1] = XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_PROPERTY_CHANGE |
7695 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
7697 wa[1] |= XCB_EVENT_MASK_LEAVE_WINDOW | XCB_EVENT_MASK_FOCUS_CHANGE;
7700 xcb_change_window_attributes(conn, win->id, XCB_CW_BORDER_PIXEL |
7701 XCB_CW_EVENT_MASK, wa);
7704 /* Figure out where to stack the window in the workspace. */
7705 if (trans && (ww = find_window(trans)))
7706 TAILQ_INSERT_AFTER(&win->ws->winlist, ww, win, entry);
7707 else if (win->ws->focus && spawn_position == SWM_STACK_ABOVE)
7708 TAILQ_INSERT_AFTER(&win->ws->winlist, win->ws->focus, win,
7710 else if (win->ws->focus && spawn_position == SWM_STACK_BELOW)
7711 TAILQ_INSERT_BEFORE(win->ws->focus, win, entry);
7712 else switch (spawn_position) {
7715 case SWM_STACK_ABOVE:
7716 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
7718 case SWM_STACK_BOTTOM:
7719 case SWM_STACK_BELOW:
7720 TAILQ_INSERT_HEAD(&win->ws->winlist, win, entry);
7723 /* Get initial _NET_WM_STATE */
7724 ewmh_get_win_state(win);
7725 /* Set initial _NET_WM_ALLOWED_ACTIONS */
7726 ewmh_update_actions(win);
7730 DNPRINTF(SWM_D_MISC, "manage_window: done. window: 0x%x, (x,y) w x h: "
7731 "(%d,%d) %d x %d, ws: %d, iconic: %s, transient: 0x%x\n", win->id,
7732 X(win), Y(win), WIDTH(win), HEIGHT(win), win->ws->idx,
7733 YESNO(win->iconic), win->transient);
7739 free_window(struct ws_win *win)
7741 DNPRINTF(SWM_D_MISC, "free_window: window: 0x%x\n", win->id);
7746 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
7751 xcb_icccm_get_wm_class_reply_wipe(&win->ch);
7756 memset(win, 0xff, sizeof *win); /* XXX kill later */
7759 DNPRINTF(SWM_D_MISC, "free_window: done.\n");
7763 unmanage_window(struct ws_win *win)
7765 struct ws_win *parent;
7770 DNPRINTF(SWM_D_MISC, "unmanage_window: window: 0x%x\n", win->id);
7772 if (win->transient) {
7773 parent = find_window(win->transient);
7775 parent->focus_child = NULL;
7778 TAILQ_REMOVE(&win->ws->winlist, win, entry);
7779 TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
7783 expose(xcb_expose_event_t *e)
7786 struct swm_region *r;
7788 DNPRINTF(SWM_D_EVENT, "expose: window: 0x%x\n", e->window);
7790 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7791 for (i = 0; i < num_screens; i++)
7792 TAILQ_FOREACH(r, &screens[i].rl, entry)
7793 if (e->window == WINID(r->bar))
7801 focusin(xcb_focus_in_event_t *e)
7803 DNPRINTF(SWM_D_EVENT, "focusin: window: 0x%x, mode: %s(%u), "
7804 "detail: %s(%u)\n", e->event, get_notify_mode_label(e->mode),
7805 e->mode, get_notify_detail_label(e->detail), e->detail);
7809 focusout(xcb_focus_out_event_t *e)
7811 DNPRINTF(SWM_D_EVENT, "focusout: window: 0x%x, mode: %s(%u), "
7812 "detail: %s(%u)\n", e->event, get_notify_mode_label(e->mode),
7813 e->mode, get_notify_detail_label(e->detail), e->detail);
7818 keypress(xcb_key_press_event_t *e)
7820 xcb_keysym_t keysym;
7823 keysym = xcb_key_press_lookup_keysym(syms, e, 0);
7825 DNPRINTF(SWM_D_EVENT, "keypress: keysym: %u, win (x,y): 0x%x (%d,%d), "
7826 "detail: %u, time: %u, root (x,y): 0x%x (%d,%d), child: 0x%x, "
7827 "state: %u, same_screen: %s\n", keysym, e->event, e->event_x,
7828 e->event_y, e->detail, e->time, e->root, e->root_x, e->root_y,
7829 e->child, e->state, YESNO(e->same_screen));
7831 if ((kp = key_lookup(CLEANMASK(e->state), keysym)) == NULL)
7834 last_event_time = e->time;
7836 if (kp->funcid == KF_SPAWN_CUSTOM)
7837 spawn_custom(root_to_region(e->root, SWM_CK_ALL),
7838 &(keyfuncs[kp->funcid].args), kp->spawn_name);
7839 else if (keyfuncs[kp->funcid].func)
7840 keyfuncs[kp->funcid].func(root_to_region(e->root, SWM_CK_ALL),
7841 &(keyfuncs[kp->funcid].args));
7844 /* Unfreeze grab events. */
7845 xcb_allow_events(conn, XCB_ALLOW_ASYNC_KEYBOARD, e->time);
7848 DNPRINTF(SWM_D_EVENT, "keypress: done.\n");
7852 buttonpress(xcb_button_press_event_t *e)
7854 struct ws_win *win = NULL;
7855 struct swm_region *r, *old_r;
7859 DNPRINTF(SWM_D_EVENT, "buttonpress: win (x,y): 0x%x (%d,%d), "
7860 "detail: %u, time: %u, root (x,y): 0x%x (%d,%d), child: 0x%x, "
7861 "state: %u, same_screen: %s\n", e->event, e->event_x, e->event_y,
7862 e->detail, e->time, e->root, e->root_x, e->root_y, e->child,
7863 e->state, YESNO(e->same_screen));
7865 if (e->event == e->root) {
7866 if (e->child != 0) {
7867 win = find_window(e->child);
7868 /* Pass ButtonPress to window if it isn't managed. */
7872 /* Focus on empty region */
7873 /* If no windows on region if its empty. */
7874 r = root_to_region(e->root, SWM_CK_POINTER);
7876 DNPRINTF(SWM_D_EVENT, "buttonpress: "
7877 "NULL region; ignoring.\n");
7881 if (TAILQ_EMPTY(&r->ws->winlist)) {
7882 old_r = root_to_region(e->root, SWM_CK_FOCUS);
7883 if (old_r && old_r != r)
7884 unfocus_win(old_r->ws->focus);
7886 xcb_set_input_focus(conn,
7887 XCB_INPUT_FOCUS_PARENT, e->root, e->time);
7889 /* Clear bar since empty. */
7897 win = find_window(e->event);
7903 last_event_time = e->time;
7905 focus_win(get_focus_magic(win));
7907 for (i = 0; i < LENGTH(buttons); i++)
7908 if (client_click == buttons[i].action && buttons[i].func &&
7909 buttons[i].button == e->detail &&
7910 CLEANMASK(buttons[i].mask) == CLEANMASK(e->state)) {
7911 buttons[i].func(win, &buttons[i].args);
7917 DNPRINTF(SWM_D_EVENT, "buttonpress: passing to window.\n");
7918 /* Replay event to event window */
7919 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, e->time);
7921 DNPRINTF(SWM_D_EVENT, "buttonpress: handled.\n");
7922 /* Unfreeze grab events. */
7923 xcb_allow_events(conn, XCB_ALLOW_SYNC_POINTER, e->time);
7931 print_win_geom(xcb_window_t w)
7933 xcb_get_geometry_reply_t *wa;
7935 wa = xcb_get_geometry_reply(conn, xcb_get_geometry(conn, w), NULL);
7938 DNPRINTF(SWM_D_MISC, "print_win_geom: window not found: 0x%x\n",
7943 DNPRINTF(SWM_D_MISC, "print_win_geom: window: 0x%x, root: 0x%x, "
7944 "depth: %u, (x,y) w x h: (%d,%d) %d x %d, border: %d\n",
7945 w, wa->root, wa->depth, wa->x, wa->y, wa->width, wa->height,
7954 get_stack_mode_name(uint8_t mode)
7959 case XCB_STACK_MODE_ABOVE:
7962 case XCB_STACK_MODE_BELOW:
7965 case XCB_STACK_MODE_TOP_IF:
7968 case XCB_STACK_MODE_BOTTOM_IF:
7971 case XCB_STACK_MODE_OPPOSITE:
7983 configurerequest(xcb_configure_request_event_t *e)
7986 struct swm_region *r = NULL;
7989 uint32_t wc[7] = {0};
7991 if ((win = find_window(e->window)) == NULL)
7992 if ((win = find_unmanaged_window(e->window)) == NULL)
7996 if (swm_debug & SWM_D_EVENT) {
7997 print_win_geom(e->window);
7999 DNPRINTF(SWM_D_EVENT, "configurerequest: window: 0x%x, "
8000 "parent: 0x%x, new: %s, value_mask: %u { ", e->window,
8001 e->parent, YESNO(new), e->value_mask);
8002 if (e->value_mask & XCB_CONFIG_WINDOW_X)
8003 DPRINTF("X: %d ", e->x);
8004 if (e->value_mask & XCB_CONFIG_WINDOW_Y)
8005 DPRINTF("Y: %d ", e->y);
8006 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH)
8007 DPRINTF("W: %u ", e->width);
8008 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
8009 DPRINTF("H: %u ", e->height);
8010 if (e->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH)
8011 DPRINTF("Border: %u ", e->border_width);
8012 if (e->value_mask & XCB_CONFIG_WINDOW_SIBLING)
8013 DPRINTF("Sibling: 0x%x ", e->sibling);
8014 if (e->value_mask & XCB_CONFIG_WINDOW_STACK_MODE)
8015 DPRINTF("StackMode: %s(%u) ",
8016 get_stack_mode_name(e->stack_mode), e->stack_mode);
8022 if (e->value_mask & XCB_CONFIG_WINDOW_X) {
8023 mask |= XCB_CONFIG_WINDOW_X;
8026 if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
8027 mask |= XCB_CONFIG_WINDOW_Y;
8030 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
8031 mask |= XCB_CONFIG_WINDOW_WIDTH;
8034 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
8035 mask |= XCB_CONFIG_WINDOW_HEIGHT;
8036 wc[i++] = e->height;
8038 if (e->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH) {
8039 mask |= XCB_CONFIG_WINDOW_BORDER_WIDTH;
8040 wc[i++] = e->border_width;
8042 if (e->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
8043 mask |= XCB_CONFIG_WINDOW_SIBLING;
8044 wc[i++] = e->sibling;
8046 if (e->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
8047 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
8048 wc[i++] = e->stack_mode;
8052 xcb_configure_window(conn, e->window, mask, wc);
8055 } else if ((!win->manual || win->quirks & SWM_Q_ANYWHERE) &&
8056 !(win->ewmh_flags & EWMH_F_FULLSCREEN)) {
8059 else if (win->ws->old_r)
8062 /* windows are centered unless ANYWHERE quirk is set. */
8063 if (win->quirks & SWM_Q_ANYWHERE) {
8064 if (e->value_mask & XCB_CONFIG_WINDOW_X) {
8065 win->g_float.x = e->x;
8067 win->g_float.x -= X(r);
8070 if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
8071 win->g_float.y = e->y;
8073 win->g_float.y -= Y(r);
8077 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH)
8078 win->g_float.w = e->width;
8080 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
8081 win->g_float.h = e->height;
8083 win->g_floatvalid = 1;
8085 if (win->floating && r && (win->transient ||
8086 win->ws->cur_layout != &layouts[SWM_MAX_STACK])) {
8087 WIDTH(win) = win->g_float.w;
8088 HEIGHT(win) = win->g_float.h;
8091 stack_floater(win, r);
8103 DNPRINTF(SWM_D_EVENT, "configurerequest: done.\n");
8107 configurenotify(xcb_configure_notify_event_t *e)
8111 DNPRINTF(SWM_D_EVENT, "configurenotify: win 0x%x, event win: 0x%x, "
8112 "(x,y) WxH: (%d,%d) %ux%u, border: %u, above_sibling: 0x%x, "
8113 "override_redirect: %s\n", e->window, e->event, e->x, e->y,
8114 e->width, e->height, e->border_width, e->above_sibling,
8115 YESNO(e->override_redirect));
8117 win = find_window(e->window);
8119 xcb_icccm_get_wm_normal_hints_reply(conn,
8120 xcb_icccm_get_wm_normal_hints(conn, win->id),
8123 if (font_adjusted) {
8131 destroynotify(xcb_destroy_notify_event_t *e)
8135 DNPRINTF(SWM_D_EVENT, "destroynotify: window: 0x%x\n", e->window);
8137 if ((win = find_window(e->window)) == NULL) {
8138 if ((win = find_unmanaged_window(e->window)) == NULL)
8144 if (focus_mode != SWM_FOCUS_FOLLOW) {
8145 /* If we were focused, make sure we focus on something else. */
8146 if (win == win->ws->focus)
8147 win->ws->focus_pending = get_focus_prev(win);
8150 unmanage_window(win);
8153 if (focus_mode != SWM_FOCUS_FOLLOW) {
8154 if (win->ws->focus_pending) {
8155 focus_win(win->ws->focus_pending);
8156 win->ws->focus_pending = NULL;
8167 get_notify_detail_label(uint8_t detail)
8172 case XCB_NOTIFY_DETAIL_ANCESTOR:
8175 case XCB_NOTIFY_DETAIL_VIRTUAL:
8178 case XCB_NOTIFY_DETAIL_INFERIOR:
8181 case XCB_NOTIFY_DETAIL_NONLINEAR:
8182 label = "Nonlinear";
8184 case XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL:
8185 label = "NonlinearVirtual";
8187 case XCB_NOTIFY_DETAIL_POINTER:
8190 case XCB_NOTIFY_DETAIL_POINTER_ROOT:
8191 label = "PointerRoot";
8193 case XCB_NOTIFY_DETAIL_NONE:
8204 get_notify_mode_label(uint8_t mode)
8209 case XCB_NOTIFY_MODE_NORMAL:
8212 case XCB_NOTIFY_MODE_GRAB:
8215 case XCB_NOTIFY_MODE_UNGRAB:
8218 case XCB_NOTIFY_MODE_WHILE_GRABBED:
8219 label = "WhileGrabbed";
8230 enternotify(xcb_enter_notify_event_t *e)
8233 struct swm_region *r;
8235 DNPRINTF(SWM_D_FOCUS, "enternotify: time: %u, win (x,y): 0x%x "
8236 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): 0x%x (%d,%d), "
8237 "child: 0x%x, same_screen_focus: %s, state: %d\n",
8238 e->time, e->event, e->event_x, e->event_y,
8239 get_notify_mode_label(e->mode), e->mode,
8240 get_notify_detail_label(e->detail), e->detail,
8241 e->root, e->root_x, e->root_y, e->child,
8242 YESNO(e->same_screen_focus), e->state);
8244 if (focus_mode == SWM_FOCUS_MANUAL &&
8245 e->mode == XCB_NOTIFY_MODE_NORMAL) {
8246 DNPRINTF(SWM_D_EVENT, "enternotify: manual focus; ignoring.\n");
8250 last_event_time = e->time;
8252 if ((win = find_window(e->event)) == NULL) {
8253 if (e->event == e->root) {
8254 /* If no windows on pointer region, then focus root. */
8255 r = root_to_region(e->root, SWM_CK_POINTER);
8257 DNPRINTF(SWM_D_EVENT, "enternotify: "
8258 "NULL region; ignoring.\n");
8264 DNPRINTF(SWM_D_EVENT, "enternotify: window is NULL; "
8269 focus_win(get_focus_magic(win));
8277 leavenotify(xcb_leave_notify_event_t *e)
8279 DNPRINTF(SWM_D_FOCUS, "leavenotify: time: %u, win (x,y): 0x%x "
8280 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): 0x%x (%d,%d), "
8281 "child: 0x%x, same_screen_focus: %s, state: %d\n",
8282 e->time, e->event, e->event_x, e->event_y,
8283 get_notify_mode_label(e->mode), e->mode,
8284 get_notify_detail_label(e->detail), e->detail,
8285 e->root, e->root_x, e->root_y, e->child,
8286 YESNO(e->same_screen_focus), e->state);
8291 mapnotify(xcb_map_notify_event_t *e)
8295 DNPRINTF(SWM_D_EVENT, "mapnotify: window: 0x%x\n", e->window);
8297 if ((win = find_window(e->window)) == NULL)
8298 win = manage_window(e->window, 1);
8301 set_win_state(win, XCB_ICCCM_WM_STATE_NORMAL);
8303 if (focus_mode != SWM_FOCUS_FOLLOW) {
8304 if (win->ws->focus_pending == win) {
8306 win->ws->focus_pending = NULL;
8315 mappingnotify(xcb_mapping_notify_event_t *e)
8317 xcb_refresh_keyboard_mapping(syms, e);
8319 if (e->request == XCB_MAPPING_KEYBOARD)
8324 maprequest(xcb_map_request_event_t *e)
8327 xcb_get_window_attributes_reply_t *war;
8329 DNPRINTF(SWM_D_EVENT, "maprequest: win 0x%x\n",
8332 war = xcb_get_window_attributes_reply(conn,
8333 xcb_get_window_attributes(conn, e->window),
8336 DNPRINTF(SWM_D_EVENT, "maprequest: window lost.\n");
8340 if (war->override_redirect) {
8341 DNPRINTF(SWM_D_EVENT, "maprequest: override_redirect; "
8346 win = manage_window(e->window,
8347 (war->map_state == XCB_MAP_STATE_VIEWABLE));
8349 /* The new window should get focus; prepare. */
8350 if (focus_mode != SWM_FOCUS_FOLLOW)
8351 win->ws->focus_pending = get_focus_magic(win);
8353 /* All windows need to be mapped if they are in the current workspace.*/
8357 /* Ignore EnterNotify to handle the mapnotify without interference. */
8358 if (focus_mode == SWM_FOCUS_DEFAULT)
8359 event_drain(XCB_ENTER_NOTIFY);
8362 DNPRINTF(SWM_D_EVENT, "maprequest: done.\n");
8366 motionnotify(xcb_motion_notify_event_t *e)
8368 struct swm_region *r;
8371 DNPRINTF(SWM_D_FOCUS, "motionnotify: time: %u, win (x,y): 0x%x "
8372 "(%d,%d), detail: %s(%d), root (x,y): 0x%x (%d,%d), "
8373 "child: 0x%x, same_screen_focus: %s, state: %d\n",
8374 e->time, e->event, e->event_x, e->event_y,
8375 get_notify_detail_label(e->detail), e->detail,
8376 e->root, e->root_x, e->root_y, e->child,
8377 YESNO(e->same_screen), e->state);
8379 last_event_time = e->time;
8381 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
8382 for (i = 0; i < num_screens; i++)
8383 if (screens[i].root == e->root)
8386 TAILQ_FOREACH(r, &screens[i].rl, entry)
8387 if (X(r) <= e->root_x && e->root_x < MAX_X(r) &&
8388 Y(r) <= e->root_y && e->root_y < MAX_Y(r))
8396 get_atom_name(xcb_atom_t atom)
8401 * This should be disabled during most debugging since
8402 * xcb_get_* causes an xcb_flush.
8405 xcb_get_atom_name_reply_t *r;
8407 r = xcb_get_atom_name_reply(conn,
8408 xcb_get_atom_name(conn, atom),
8411 len = xcb_get_atom_name_name_length(r);
8413 name = malloc(len + 1);
8415 memcpy(name, xcb_get_atom_name_name(r), len);
8429 propertynotify(xcb_property_notify_event_t *e)
8435 name = get_atom_name(e->atom);
8436 DNPRINTF(SWM_D_EVENT, "propertynotify: window: 0x%x, atom: %s(%u), "
8437 "time: %#x, state: %u\n", e->window, name, e->atom, e->time,
8441 win = find_window(e->window);
8445 last_event_time = e->time;
8447 if (e->atom == a_swm_iconic) {
8448 if (e->state == XCB_PROPERTY_NEW_VALUE) {
8449 if (focus_mode != SWM_FOCUS_FOLLOW)
8450 win->ws->focus_pending = get_focus_prev(win);
8457 if (focus_mode != SWM_FOCUS_FOLLOW) {
8458 focus_win(win->ws->focus_pending);
8459 win->ws->focus_pending = NULL;
8463 } else if (e->state == XCB_PROPERTY_DELETE) {
8464 /* The window is no longer iconic, restack ws. */
8465 if (focus_mode != SWM_FOCUS_FOLLOW)
8466 win->ws->focus_pending = get_focus_magic(win);
8470 /* Flush EnterNotify for mapnotify, if needed. */
8473 } else if (e->atom == a_state) {
8474 /* State just changed, make sure it gets focused if mapped. */
8475 if (e->state == XCB_PROPERTY_NEW_VALUE) {
8476 if (focus_mode != SWM_FOCUS_FOLLOW) {
8478 win->ws->focus_pending == win) {
8479 focus_win(win->ws->focus_pending);
8480 win->ws->focus_pending = NULL;
8484 } else if (e->atom == XCB_ATOM_WM_CLASS ||
8485 e->atom == XCB_ATOM_WM_NAME) {
8493 unmapnotify(xcb_unmap_notify_event_t *e)
8496 struct workspace *ws;
8498 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: 0x%x\n", e->window);
8500 /* If we aren't managing the window, then ignore. */
8501 win = find_window(e->window);
8502 if (win == NULL || win->id != e->window)
8507 if (getstate(e->window) != XCB_ICCCM_WM_STATE_ICONIC)
8508 set_win_state(win, XCB_ICCCM_WM_STATE_ICONIC);
8511 /* window unmapped itself */
8512 /* do unmap/unfocus/restack and unmanage */
8515 /* If win was focused, make sure to focus on something else. */
8516 if (win == ws->focus) {
8517 if (focus_mode != SWM_FOCUS_FOLLOW) {
8518 ws->focus_pending = get_focus_prev(win);
8519 DNPRINTF(SWM_D_EVENT, "unmapnotify: "
8520 "focus_pending: 0x%x\n",
8521 WINID(ws->focus_pending));
8527 unmanage_window(win);
8532 if (focus_mode == SWM_FOCUS_FOLLOW) {
8534 focus_win(get_pointer_win(ws->r->s->root));
8536 if (ws->focus_pending) {
8537 focus_win(ws->focus_pending);
8538 ws->focus_pending = NULL;
8543 if (getstate(e->window) == XCB_ICCCM_WM_STATE_NORMAL)
8544 set_win_state(win, XCB_ICCCM_WM_STATE_ICONIC);
8551 visibilitynotify(xcb_visibility_notify_event_t *e)
8553 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: 0x%x\n",
8559 clientmessage(xcb_client_message_event_t *e)
8562 xcb_map_request_event_t mre;
8566 name = get_atom_name(e->type);
8567 DNPRINTF(SWM_D_EVENT, "clientmessage: window: 0x%x, atom: %s(%u)\n",
8568 e->window, name, e->type);
8571 win = find_window(e->window);
8574 if (e->type == ewmh[_NET_ACTIVE_WINDOW].atom) {
8575 /* Manage the window with maprequest. */
8576 DNPRINTF(SWM_D_EVENT, "clientmessage: request focus on "
8577 "unmanaged window.\n");
8578 mre.window = e->window;
8584 if (e->type == ewmh[_NET_ACTIVE_WINDOW].atom) {
8585 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_ACTIVE_WINDOW\n");
8588 if (e->type == ewmh[_NET_CLOSE_WINDOW].atom) {
8589 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_CLOSE_WINDOW\n");
8590 if (win->can_delete)
8591 client_msg(win, a_delete, 0);
8593 xcb_kill_client(conn, win->id);
8595 if (e->type == ewmh[_NET_MOVERESIZE_WINDOW].atom) {
8596 DNPRINTF(SWM_D_EVENT,
8597 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
8598 if (win->floating) {
8599 if (e->data.data32[0] & (1<<8)) /* x */
8600 X(win) = e->data.data32[1];
8601 if (e->data.data32[0] & (1<<9)) /* y */
8602 Y(win) = e->data.data32[2];
8603 if (e->data.data32[0] & (1<<10)) /* width */
8604 WIDTH(win) = e->data.data32[3];
8605 if (e->data.data32[0] & (1<<11)) /* height */
8606 HEIGHT(win) = e->data.data32[4];
8611 /* TODO: Change stack sizes */
8612 /* notify no change was made. */
8613 config_win(win, NULL);
8616 if (e->type == ewmh[_NET_WM_STATE].atom) {
8617 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_WM_STATE\n");
8618 ewmh_update_win_state(win, e->data.data32[1], e->data.data32[0]);
8619 if (e->data.data32[2])
8620 ewmh_update_win_state(win, e->data.data32[2],
8632 int errcode = xcb_connection_has_error(conn);
8633 #ifdef XCB_CONN_ERROR
8636 case XCB_CONN_ERROR:
8637 s = "Socket error, pipe error or other stream error.";
8639 case XCB_CONN_CLOSED_EXT_NOTSUPPORTED:
8640 s = "Extension not supported.";
8642 case XCB_CONN_CLOSED_MEM_INSUFFICIENT:
8643 s = "Insufficient memory.";
8645 case XCB_CONN_CLOSED_REQ_LEN_EXCEED:
8646 s = "Request length was exceeded.";
8648 case XCB_CONN_CLOSED_PARSE_ERR:
8649 s = "Error parsing display string.";
8652 s = "Unknown error.";
8655 errx(errcode, "X CONNECTION ERROR: %s", s);
8658 errx(errcode, "X CONNECTION ERROR");
8666 const uint32_t val = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
8667 XCB_EVENT_MASK_ENTER_WINDOW;
8669 xcb_void_cookie_t wac;
8670 xcb_generic_error_t *error;
8672 /* this causes an error if some other window manager is running */
8673 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
8674 for (i = 0; i < num_screens; i++) {
8675 if ((sc = get_screen(i)) == NULL)
8676 errx(1, "ERROR: can't get screen %d.", i);
8677 DNPRINTF(SWM_D_INIT, "enable_wm: screen %d, root: 0x%x\n",
8679 wac = xcb_change_window_attributes_checked(conn, sc->root,
8680 XCB_CW_EVENT_MASK, &val);
8681 if ((error = xcb_request_check(conn, wac))) {
8682 DNPRINTF(SWM_D_INIT, "enable_wm: error_code: %u\n",
8688 /* click to focus on empty region */
8689 xcb_grab_button(conn, 1, sc->root, BUTTONMASK,
8690 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC, XCB_WINDOW_NONE,
8691 XCB_CURSOR_NONE, XCB_BUTTON_INDEX_1, XCB_BUTTON_MASK_ANY);
8698 new_region(struct swm_screen *s, int x, int y, int w, int h)
8700 struct swm_region *r, *n;
8701 struct workspace *ws = NULL;
8705 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
8706 s->idx, w, h, x, y);
8708 /* remove any conflicting regions */
8709 n = TAILQ_FIRST(&s->rl);
8712 n = TAILQ_NEXT(r, entry);
8713 if (X(r) < (x + w) && (X(r) + WIDTH(r)) > x &&
8714 Y(r) < (y + h) && (Y(r) + HEIGHT(r)) > y) {
8715 if (r->ws->r != NULL)
8716 r->ws->old_r = r->ws->r;
8719 xcb_destroy_window(conn, r->id);
8720 TAILQ_REMOVE(&s->rl, r, entry);
8721 TAILQ_INSERT_TAIL(&s->orl, r, entry);
8725 /* search old regions for one to reuse */
8727 /* size + location match */
8728 TAILQ_FOREACH(r, &s->orl, entry)
8729 if (X(r) == x && Y(r) == y &&
8730 HEIGHT(r) == h && WIDTH(r) == w)
8734 TAILQ_FOREACH(r, &s->orl, entry)
8735 if (HEIGHT(r) == h && WIDTH(r) == w)
8739 TAILQ_REMOVE(&s->orl, r, entry);
8740 /* try to use old region's workspace */
8741 if (r->ws->r == NULL)
8744 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
8745 err(1, "new_region: calloc: failed to allocate memory "
8748 /* if we don't have a workspace already, find one */
8750 for (i = 0; i < workspace_limit; i++)
8751 if (s->ws[i].r == NULL) {
8758 errx(1, "new_region: no free workspaces");
8769 TAILQ_INSERT_TAIL(&s->rl, r, entry);
8771 /* Invisible region window to detect pointer events on empty regions. */
8772 r->id = xcb_generate_id(conn);
8773 wa[0] = XCB_EVENT_MASK_POINTER_MOTION |
8774 XCB_EVENT_MASK_POINTER_MOTION_HINT;
8776 xcb_create_window(conn, XCB_COPY_FROM_PARENT, r->id, r->s->root,
8777 X(r), Y(r), WIDTH(r), HEIGHT(r), 0, XCB_WINDOW_CLASS_INPUT_ONLY,
8778 XCB_COPY_FROM_PARENT, XCB_CW_EVENT_MASK, wa);
8780 xcb_map_window(conn, r->id);
8786 #ifdef SWM_XRR_HAS_CRTC
8789 #endif /* SWM_XRR_HAS_CRTC */
8790 struct swm_region *r;
8793 xcb_randr_get_screen_resources_current_cookie_t src;
8794 xcb_randr_get_screen_resources_current_reply_t *srr;
8795 xcb_randr_get_crtc_info_cookie_t cic;
8796 xcb_randr_get_crtc_info_reply_t *cir = NULL;
8797 xcb_randr_crtc_t *crtc;
8798 xcb_screen_t *screen;
8800 DNPRINTF(SWM_D_MISC, "scan_xrandr: screen: %d\n", i);
8802 if ((screen = get_screen(i)) == NULL)
8803 errx(1, "ERROR: can't get screen %d.", i);
8805 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
8806 if (i >= num_screens)
8807 errx(1, "scan_xrandr: invalid screen");
8809 /* remove any old regions */
8810 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
8811 r->ws->old_r = r->ws->r = NULL;
8813 xcb_destroy_window(conn, r->id);
8814 TAILQ_REMOVE(&screens[i].rl, r, entry);
8815 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
8819 /* map virtual screens onto physical screens */
8820 #ifdef SWM_XRR_HAS_CRTC
8821 if (xrandr_support) {
8822 src = xcb_randr_get_screen_resources_current(conn,
8824 srr = xcb_randr_get_screen_resources_current_reply(conn, src,
8827 new_region(&screens[i], 0, 0,
8828 screen->width_in_pixels,
8829 screen->height_in_pixels);
8832 ncrtc = srr->num_crtcs;
8834 crtc = xcb_randr_get_screen_resources_current_crtcs(srr);
8835 for (c = 0; c < ncrtc; c++) {
8836 cic = xcb_randr_get_crtc_info(conn, crtc[c],
8838 cir = xcb_randr_get_crtc_info_reply(conn, cic, NULL);
8841 if (cir->num_outputs == 0) {
8847 new_region(&screens[i], 0, 0,
8848 screen->width_in_pixels,
8849 screen->height_in_pixels);
8851 new_region(&screens[i],
8852 cir->x, cir->y, cir->width, cir->height);
8857 #endif /* SWM_XRR_HAS_CRTC */
8859 /* If detection failed, create a single region that spans the screen. */
8860 if (TAILQ_EMPTY(&screens[i].rl))
8861 new_region(&screens[i], 0, 0, screen->width_in_pixels,
8862 screen->height_in_pixels);
8865 /* Cleanup unused previously visible workspaces. */
8866 TAILQ_FOREACH(r, &screens[i].orl, entry) {
8867 TAILQ_FOREACH(win, &r->ws->winlist, entry)
8870 /* The screen shouldn't focus on an unused region. */
8871 if (screens[i].r_focus == r)
8872 screens[i].r_focus = NULL;
8875 DNPRINTF(SWM_D_MISC, "scan_xrandr: done.\n");
8879 screenchange(xcb_randr_screen_change_notify_event_t *e)
8881 struct swm_region *r;
8884 DNPRINTF(SWM_D_EVENT, "screenchange: root: 0x%x\n", e->root);
8886 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
8887 /* silly event doesn't include the screen index */
8888 for (i = 0; i < num_screens; i++)
8889 if (screens[i].root == e->root)
8891 if (i >= num_screens)
8892 errx(1, "screenchange: screen not found");
8894 /* brute force for now, just re-enumerate the regions */
8898 print_win_geom(e->root);
8900 /* add bars to all regions */
8901 for (i = 0; i < num_screens; i++) {
8902 TAILQ_FOREACH(r, &screens[i].rl, entry)
8908 /* Make sure a region has focus on each screen. */
8909 for (i = 0; i < num_screens; i++) {
8910 if (screens[i].r_focus == NULL) {
8911 r = TAILQ_FIRST(&screens[i].rl);
8924 struct swm_region *r = NULL;
8925 xcb_window_t *wins = NULL, trans;
8927 int i, j, num_screens;
8928 uint16_t state, manage, mapped;
8930 xcb_query_tree_cookie_t qtc;
8931 xcb_query_tree_reply_t *qtr;
8932 xcb_get_window_attributes_cookie_t gac;
8933 xcb_get_window_attributes_reply_t *gar;
8934 xcb_get_property_cookie_t pc;
8936 DNPRINTF(SWM_D_INIT, "grab_windows: begin\n");
8937 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
8938 for (i = 0; i < num_screens; i++) {
8939 qtc = xcb_query_tree(conn, screens[i].root);
8940 qtr = xcb_query_tree_reply(conn, qtc, NULL);
8943 wins = xcb_query_tree_children(qtr);
8944 no = xcb_query_tree_children_length(qtr);
8945 /* attach windows to a region */
8946 /* normal windows */
8947 DNPRINTF(SWM_D_INIT, "grab_windows: grab top level windows.\n");
8948 for (j = 0; j < no; j++) {
8949 TAILQ_FOREACH(r, &screens[i].rl, entry) {
8950 if (r->id == wins[j]) {
8951 DNPRINTF(SWM_D_INIT, "grab_windows: "
8952 "skip %#x; region input window.\n",
8961 gac = xcb_get_window_attributes(conn, wins[j]);
8962 gar = xcb_get_window_attributes_reply(conn, gac, NULL);
8964 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
8965 "doesn't exist.\n", wins[j]);
8969 if (gar->override_redirect) {
8970 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
8971 "override_redirect set.\n", wins[j]);
8976 pc = xcb_icccm_get_wm_transient_for(conn, wins[j]);
8977 if (xcb_icccm_get_wm_transient_for_reply(conn, pc,
8979 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
8980 "is transient for %#x.\n", wins[j], trans);
8985 state = getstate(wins[j]);
8986 manage = state != XCB_ICCCM_WM_STATE_WITHDRAWN;
8987 mapped = gar->map_state != XCB_MAP_STATE_UNMAPPED;
8988 if (mapped || manage)
8989 manage_window(wins[j], mapped);
8992 /* transient windows */
8993 DNPRINTF(SWM_D_INIT, "grab_windows: grab transient windows.\n");
8994 for (j = 0; j < no; j++) {
8995 gac = xcb_get_window_attributes(conn, wins[j]);
8996 gar = xcb_get_window_attributes_reply(conn, gac, NULL);
8998 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
8999 "doesn't exist.\n", wins[j]);
9003 if (gar->override_redirect) {
9004 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
9005 "override_redirect set.\n", wins[j]);
9010 state = getstate(wins[j]);
9011 manage = state != XCB_ICCCM_WM_STATE_WITHDRAWN;
9012 mapped = gar->map_state != XCB_MAP_STATE_UNMAPPED;
9013 pc = xcb_icccm_get_wm_transient_for(conn, wins[j]);
9014 if (xcb_icccm_get_wm_transient_for_reply(conn, pc,
9015 &trans, NULL) && manage)
9016 manage_window(wins[j], mapped);
9021 DNPRINTF(SWM_D_INIT, "grab_windows: done.\n");
9027 int i, j, k, num_screens;
9028 struct workspace *ws;
9029 uint32_t gcv[1], wa[1];
9030 const xcb_query_extension_reply_t *qep;
9031 xcb_screen_t *screen;
9032 xcb_randr_query_version_cookie_t c;
9033 xcb_randr_query_version_reply_t *r;
9035 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
9036 if ((screens = calloc(num_screens,
9037 sizeof(struct swm_screen))) == NULL)
9038 err(1, "setup_screens: calloc: failed to allocate memory for "
9041 /* initial Xrandr setup */
9043 qep = xcb_get_extension_data(conn, &xcb_randr_id);
9045 c = xcb_randr_query_version(conn, 1, 1);
9046 r = xcb_randr_query_version_reply(conn, c, NULL);
9048 if (r->major_version >= 1) {
9050 xrandr_eventbase = qep->first_event;
9056 wa[0] = cursors[XC_LEFT_PTR].cid;
9058 /* map physical screens */
9059 for (i = 0; i < num_screens; i++) {
9060 DNPRINTF(SWM_D_WS, "setup_screens: init screen: %d\n", i);
9062 screens[i].r_focus = NULL;
9064 TAILQ_INIT(&screens[i].rl);
9065 TAILQ_INIT(&screens[i].orl);
9066 if ((screen = get_screen(i)) == NULL)
9067 errx(1, "ERROR: can't get screen %d.", i);
9068 screens[i].root = screen->root;
9070 /* set default colors */
9071 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
9072 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
9073 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
9074 setscreencolor("rgb:00/40/40", i + 1,
9075 SWM_S_COLOR_BAR_BORDER_UNFOCUS);
9076 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
9077 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
9079 /* create graphics context on screen */
9080 screens[i].bar_gc = xcb_generate_id(conn);
9082 xcb_create_gc(conn, screens[i].bar_gc, screens[i].root,
9083 XCB_GC_GRAPHICS_EXPOSURES, gcv);
9085 /* set default cursor */
9086 xcb_change_window_attributes(conn, screens[i].root,
9089 /* init all workspaces */
9090 /* XXX these should be dynamically allocated too */
9091 for (j = 0; j < SWM_WS_MAX; j++) {
9092 ws = &screens[i].ws[j];
9095 ws->bar_enabled = 1;
9097 ws->focus_prev = NULL;
9098 ws->focus_pending = NULL;
9101 TAILQ_INIT(&ws->winlist);
9102 TAILQ_INIT(&ws->unmanagedlist);
9104 for (k = 0; layouts[k].l_stack != NULL; k++)
9105 if (layouts[k].l_config != NULL)
9106 layouts[k].l_config(ws,
9107 SWM_ARG_ID_STACKINIT);
9108 ws->cur_layout = &layouts[0];
9109 ws->cur_layout->l_string(ws);
9115 xcb_randr_select_input(conn, screens[i].root,
9116 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE);
9123 if ((bar_fonts = strdup(SWM_BAR_FONTS)) == NULL)
9124 err(1, "setup_globals: strdup: failed to allocate memory.");
9126 if ((clock_format = strdup("%a %b %d %R %Z %Y")) == NULL)
9127 err(1, "setup_globals: strdup: failed to allocate memory.");
9129 if ((syms = xcb_key_symbols_alloc(conn)) == NULL)
9130 errx(1, "unable to allocate key symbols");
9132 a_state = get_atom_from_string("WM_STATE");
9133 a_prot = get_atom_from_string("WM_PROTOCOLS");
9134 a_delete = get_atom_from_string("WM_DELETE_WINDOW");
9135 a_takefocus = get_atom_from_string("WM_TAKE_FOCUS");
9136 a_wmname = get_atom_from_string("WM_NAME");
9137 a_netwmname = get_atom_from_string("_NET_WM_NAME");
9138 a_utf8_string = get_atom_from_string("UTF8_STRING");
9139 a_string = get_atom_from_string("STRING");
9140 a_swm_iconic = get_atom_from_string("_SWM_ICONIC");
9141 a_swm_ws = get_atom_from_string("_SWM_WS");
9148 xcb_atom_t netwmcheck;
9149 xcb_window_t root, win;
9151 /* work around sun jdk bugs, code from wmname */
9152 netwmcheck = get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
9154 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
9155 for (i = 0; i < num_screens; i++) {
9156 root = screens[i].root;
9158 win = xcb_generate_id(conn);
9159 xcb_create_window(conn, XCB_COPY_FROM_PARENT, win, root,
9160 0, 0, 1, 1, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT,
9161 XCB_COPY_FROM_PARENT, 0, NULL);
9163 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root,
9164 netwmcheck, XCB_ATOM_WINDOW, 32, 1, &win);
9165 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win,
9166 netwmcheck, XCB_ATOM_WINDOW, 32, 1, &win);
9167 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win,
9168 a_netwmname, a_utf8_string, 8, strlen("LG3D"), "LG3D");
9173 shutdown_cleanup(void)
9177 /* disable alarm because the following code may not be interrupted */
9179 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
9180 err(1, "can't disable alarm");
9189 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
9190 for (i = 0; i < num_screens; ++i) {
9191 if (screens[i].bar_gc != 0)
9192 xcb_free_gc(conn, screens[i].bar_gc);
9193 if (!bar_font_legacy)
9194 XftColorFree(display, DefaultVisual(display, i),
9195 DefaultColormap(display, i), &bar_font_color);
9198 if (bar_font_legacy)
9199 XFreeFontSet(display, bar_fs);
9201 XftFontClose(display, bar_font);
9204 xcb_key_symbols_free(syms);
9206 xcb_disconnect(conn);
9210 event_error(xcb_generic_error_t *e)
9214 DNPRINTF(SWM_D_EVENT, "event_error: %s(%u) from %s(%u), sequence: %u, "
9215 "resource_id: %u, minor_code: %u\n",
9216 xcb_event_get_error_label(e->error_code), e->error_code,
9217 xcb_event_get_request_label(e->major_code), e->major_code,
9218 e->sequence, e->resource_id, e->minor_code);
9222 event_handle(xcb_generic_event_t *evt)
9224 uint8_t type = XCB_EVENT_RESPONSE_TYPE(evt);
9226 DNPRINTF(SWM_D_EVENT, "XCB Event: %s(%d), seq %u\n",
9227 xcb_event_get_label(XCB_EVENT_RESPONSE_TYPE(evt)),
9228 XCB_EVENT_RESPONSE_TYPE(evt), evt->sequence);
9231 #define EVENT(type, callback) case type: callback((void *)evt); return
9232 EVENT(0, event_error);
9233 EVENT(XCB_BUTTON_PRESS, buttonpress);
9234 /*EVENT(XCB_BUTTON_RELEASE, buttonpress);*/
9235 /*EVENT(XCB_CIRCULATE_NOTIFY, );*/
9236 /*EVENT(XCB_CIRCULATE_REQUEST, );*/
9237 EVENT(XCB_CLIENT_MESSAGE, clientmessage);
9238 /*EVENT(XCB_COLORMAP_NOTIFY, );*/
9239 EVENT(XCB_CONFIGURE_NOTIFY, configurenotify);
9240 EVENT(XCB_CONFIGURE_REQUEST, configurerequest);
9241 /*EVENT(XCB_CREATE_NOTIFY, );*/
9242 EVENT(XCB_DESTROY_NOTIFY, destroynotify);
9243 EVENT(XCB_ENTER_NOTIFY, enternotify);
9244 EVENT(XCB_EXPOSE, expose);
9246 EVENT(XCB_FOCUS_IN, focusin);
9247 EVENT(XCB_FOCUS_OUT, focusout);
9249 /*EVENT(XCB_GRAPHICS_EXPOSURE, );*/
9250 /*EVENT(XCB_GRAVITY_NOTIFY, );*/
9251 EVENT(XCB_KEY_PRESS, keypress);
9252 /*EVENT(XCB_KEY_RELEASE, keypress);*/
9253 /*EVENT(XCB_KEYMAP_NOTIFY, );*/
9255 EVENT(XCB_LEAVE_NOTIFY, leavenotify);
9257 EVENT(XCB_MAP_NOTIFY, mapnotify);
9258 EVENT(XCB_MAP_REQUEST, maprequest);
9259 EVENT(XCB_MAPPING_NOTIFY, mappingnotify);
9260 EVENT(XCB_MOTION_NOTIFY, motionnotify);
9261 /*EVENT(XCB_NO_EXPOSURE, );*/
9262 EVENT(XCB_PROPERTY_NOTIFY, propertynotify);
9263 /*EVENT(XCB_REPARENT_NOTIFY, );*/
9264 /*EVENT(XCB_RESIZE_REQUEST, );*/
9265 /*EVENT(XCB_SELECTION_CLEAR, );*/
9266 /*EVENT(XCB_SELECTION_NOTIFY, );*/
9267 /*EVENT(XCB_SELECTION_REQUEST, );*/
9268 EVENT(XCB_UNMAP_NOTIFY, unmapnotify);
9269 /*EVENT(XCB_VISIBILITY_NOTIFY, visibilitynotify);*/
9272 if (type - xrandr_eventbase == XCB_RANDR_SCREEN_CHANGE_NOTIFY)
9273 screenchange((void *)evt);
9277 main(int argc, char *argv[])
9279 struct swm_region *r;
9280 char conf[PATH_MAX], *cfile = NULL;
9282 int xfd, i, num_screens, startup = 1;
9283 struct sigaction sact;
9284 xcb_generic_event_t *evt;
9288 int stdin_ready = 0;
9291 /* suppress unused warning since var is needed */
9294 time_started = time(NULL);
9297 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION, buildstr);
9298 if (!setlocale(LC_CTYPE, "") || !setlocale(LC_TIME, ""))
9299 warnx("no locale support");
9301 /* handle some signals */
9302 bzero(&sact, sizeof(sact));
9303 sigemptyset(&sact.sa_mask);
9305 sact.sa_handler = sighdlr;
9306 sigaction(SIGINT, &sact, NULL);
9307 sigaction(SIGQUIT, &sact, NULL);
9308 sigaction(SIGTERM, &sact, NULL);
9309 sigaction(SIGHUP, &sact, NULL);
9311 sact.sa_handler = sighdlr;
9312 sact.sa_flags = SA_NOCLDSTOP;
9313 sigaction(SIGCHLD, &sact, NULL);
9315 if (!(display = XOpenDisplay(0)))
9316 errx(1, "can not open display");
9318 conn = XGetXCBConnection(display);
9319 if (xcb_connection_has_error(conn))
9320 errx(1, "can not get XCB connection");
9322 XSetEventQueueOwner(display, XCBOwnsEventQueue);
9324 xcb_prefetch_extension_data(conn, &xcb_randr_id);
9325 xfd = xcb_get_file_descriptor(conn);
9327 /* look for local and global conf file */
9328 pwd = getpwuid(getuid());
9330 errx(1, "invalid user: %d", getuid());
9332 xcb_grab_server(conn);
9335 /* flush all events */
9336 while ((evt = xcb_poll_for_event(conn))) {
9337 if (XCB_EVENT_RESPONSE_TYPE(evt) == 0)
9342 if (enable_wm() != 0)
9343 errx(1, "another window manager is currently running");
9345 /* Load Xcursors and/or cursorfont glyph cursors. */
9357 for (i = 0; ; i++) {
9362 snprintf(conf, sizeof conf, "%s/.%s",
9363 pwd->pw_dir, SWM_CONF_FILE);
9367 snprintf(conf, sizeof conf, "/etc/%s",
9372 snprintf(conf, sizeof conf, "%s/.%s",
9373 pwd->pw_dir, SWM_CONF_FILE_OLD);
9377 snprintf(conf, sizeof conf, "/etc/%s",
9384 if (strlen(conf) && stat(conf, &sb) != -1)
9385 if (S_ISREG(sb.st_mode)) {
9392 /* load conf (if any) */
9394 conf_load(cfile, SWM_CONF_DEFAULT);
9399 /* set some values to work around bad programs */
9401 /* grab existing windows (before we build the bars) */
9404 if (getenv("SWM_STARTED") == NULL)
9405 setenv("SWM_STARTED", "YES", 1);
9407 /* setup all bars */
9408 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
9409 for (i = 0; i < num_screens; i++)
9410 TAILQ_FOREACH(r, &screens[i].rl, entry)
9417 xcb_ungrab_server(conn);
9420 rd_max = xfd > STDIN_FILENO ? xfd : STDIN_FILENO;
9423 while ((evt = xcb_poll_for_event(conn))) {
9430 /* If just (re)started, set default focus if needed. */
9434 if (focus_mode != SWM_FOCUS_FOLLOW) {
9435 r = TAILQ_FIRST(&screens[0].rl);
9447 FD_SET(STDIN_FILENO, &rd);
9452 num_readable = select(rd_max + 1, &rd, NULL, NULL, &tv);
9453 if (num_readable == -1 && errno != EINTR) {
9454 DNPRINTF(SWM_D_MISC, "select failed");
9455 } else if (num_readable > 0 && FD_ISSET(STDIN_FILENO, &rd)) {
9460 restart(NULL, NULL);