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;
390 /* layout manager data */
391 struct swm_geometry {
404 struct swm_geometry g;
407 /* virtual "screens" */
409 TAILQ_ENTRY(swm_region) entry;
411 struct swm_geometry g;
412 struct workspace *ws; /* current workspace on this region */
413 struct workspace *ws_prior; /* prior workspace on this region */
414 struct swm_screen *s; /* screen idx */
417 TAILQ_HEAD(swm_region_list, swm_region);
420 TAILQ_ENTRY(ws_win) entry;
422 xcb_window_t transient;
423 struct ws_win *focus_child; /* focus on child transient */
424 struct swm_geometry g; /* current geometry */
425 struct swm_geometry g_float; /* region coordinates */
426 int g_floatvalid; /* g_float geometry validity */
427 int floatmaxed; /* whether maxed by max_stack */
433 unsigned int ewmh_flags;
434 int font_size_boundary[SWM_MAX_FONT_STEPS];
440 unsigned long quirks;
441 struct workspace *ws; /* always valid */
442 struct swm_screen *s; /* always valid, never changes */
443 xcb_get_geometry_reply_t *wa;
445 xcb_icccm_get_wm_class_reply_t ch;
446 xcb_icccm_wm_hints_t hints;
448 TAILQ_HEAD(ws_win_list, ws_win);
452 TAILQ_ENTRY(pid_e) entry;
456 TAILQ_HEAD(pid_list, pid_e);
457 struct pid_list pidlist = TAILQ_HEAD_INITIALIZER(pidlist);
459 /* layout handlers */
461 void vertical_config(struct workspace *, int);
462 void vertical_stack(struct workspace *, struct swm_geometry *);
463 void horizontal_config(struct workspace *, int);
464 void horizontal_stack(struct workspace *, struct swm_geometry *);
465 void max_stack(struct workspace *, struct swm_geometry *);
466 void plain_stacker(struct workspace *);
467 void fancy_stacker(struct workspace *);
470 void (*l_stack)(struct workspace *, struct swm_geometry *);
471 void (*l_config)(struct workspace *, int);
473 #define SWM_L_FOCUSPREV (1<<0)
474 #define SWM_L_MAPONFOCUS (1<<1)
475 void (*l_string)(struct workspace *);
477 /* stack, configure */
478 { vertical_stack, vertical_config, 0, plain_stacker },
479 { horizontal_stack, horizontal_config, 0, plain_stacker },
481 SWM_L_MAPONFOCUS | SWM_L_FOCUSPREV, plain_stacker },
482 { NULL, NULL, 0, NULL },
485 /* position of max_stack mode in the layouts array, index into layouts! */
486 #define SWM_V_STACK (0)
487 #define SWM_H_STACK (1)
488 #define SWM_MAX_STACK (2)
490 #define SWM_H_SLICE (32)
491 #define SWM_V_SLICE (32)
493 /* define work spaces */
495 int idx; /* workspace index */
496 char *name; /* workspace name */
497 int always_raise; /* raise windows on focus */
498 int bar_enabled; /* bar visibility */
499 struct layout *cur_layout; /* current layout handlers */
500 struct ws_win *focus; /* may be NULL */
501 struct ws_win *focus_prev; /* may be NULL */
502 struct ws_win *focus_pending; /* may be NULL */
503 struct swm_region *r; /* may be NULL */
504 struct swm_region *old_r; /* may be NULL */
505 struct ws_win_list winlist; /* list of windows in ws */
506 struct ws_win_list unmanagedlist; /* list of dead windows in ws */
507 char stacker[10]; /* display stacker and layout */
511 int horizontal_msize;
513 int horizontal_stacks;
524 SWM_S_COLOR_BAR_BORDER,
525 SWM_S_COLOR_BAR_BORDER_UNFOCUS,
526 SWM_S_COLOR_BAR_FONT,
532 /* physical screen mapping */
533 #define SWM_WS_MAX (22) /* hard limit */
534 int workspace_limit = 10; /* soft limit */
537 int idx; /* screen index */
538 struct swm_region_list rl; /* list of regions on this screen */
539 struct swm_region_list orl; /* list of old regions */
541 struct workspace ws[SWM_WS_MAX];
542 struct swm_region *r_focus;
548 } c[SWM_S_COLOR_MAX];
550 xcb_gcontext_t bar_gc;
553 struct swm_screen *screens;
555 /* args to functions */
558 #define SWM_ARG_ID_FOCUSNEXT (0)
559 #define SWM_ARG_ID_FOCUSPREV (1)
560 #define SWM_ARG_ID_FOCUSMAIN (2)
561 #define SWM_ARG_ID_SWAPNEXT (10)
562 #define SWM_ARG_ID_SWAPPREV (11)
563 #define SWM_ARG_ID_SWAPMAIN (12)
564 #define SWM_ARG_ID_MOVELAST (13)
565 #define SWM_ARG_ID_MASTERSHRINK (20)
566 #define SWM_ARG_ID_MASTERGROW (21)
567 #define SWM_ARG_ID_MASTERADD (22)
568 #define SWM_ARG_ID_MASTERDEL (23)
569 #define SWM_ARG_ID_FLIPLAYOUT (24)
570 #define SWM_ARG_ID_STACKRESET (30)
571 #define SWM_ARG_ID_STACKINIT (31)
572 #define SWM_ARG_ID_CYCLEWS_UP (40)
573 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
574 #define SWM_ARG_ID_CYCLERG_UP (42)
575 #define SWM_ARG_ID_CYCLERG_DOWN (43)
576 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
577 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
578 #define SWM_ARG_ID_STACKINC (50)
579 #define SWM_ARG_ID_STACKDEC (51)
580 #define SWM_ARG_ID_SS_ALL (60)
581 #define SWM_ARG_ID_SS_WINDOW (61)
582 #define SWM_ARG_ID_DONTCENTER (70)
583 #define SWM_ARG_ID_CENTER (71)
584 #define SWM_ARG_ID_KILLWINDOW (80)
585 #define SWM_ARG_ID_DELETEWINDOW (81)
586 #define SWM_ARG_ID_WIDTHGROW (90)
587 #define SWM_ARG_ID_WIDTHSHRINK (91)
588 #define SWM_ARG_ID_HEIGHTGROW (92)
589 #define SWM_ARG_ID_HEIGHTSHRINK (93)
590 #define SWM_ARG_ID_MOVEUP (100)
591 #define SWM_ARG_ID_MOVEDOWN (101)
592 #define SWM_ARG_ID_MOVELEFT (102)
593 #define SWM_ARG_ID_MOVERIGHT (103)
594 #define SWM_ARG_ID_BAR_TOGGLE (110)
595 #define SWM_ARG_ID_BAR_TOGGLE_WS (111)
601 TAILQ_ENTRY(quirk) entry;
605 #define SWM_Q_FLOAT (1<<0) /* float this window */
606 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
607 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
608 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
609 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
610 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
612 TAILQ_HEAD(quirk_list, quirk);
613 struct quirk_list quirks = TAILQ_HEAD_INITIALIZER(quirks);
616 * Supported EWMH hints should be added to
617 * both the enum and the ewmh array
622 _NET_MOVERESIZE_WINDOW,
623 _NET_WM_ACTION_ABOVE,
624 _NET_WM_ACTION_CLOSE,
625 _NET_WM_ACTION_FULLSCREEN,
627 _NET_WM_ACTION_RESIZE,
628 _NET_WM_ALLOWED_ACTIONS,
632 _NET_WM_STATE_FULLSCREEN,
633 _NET_WM_STATE_HIDDEN,
634 _NET_WM_STATE_MAXIMIZED_HORZ,
635 _NET_WM_STATE_MAXIMIZED_VERT,
636 _NET_WM_STATE_SKIP_PAGER,
637 _NET_WM_STATE_SKIP_TASKBAR,
639 _NET_WM_WINDOW_TYPE_DIALOG,
640 _NET_WM_WINDOW_TYPE_DOCK,
641 _NET_WM_WINDOW_TYPE_NORMAL,
642 _NET_WM_WINDOW_TYPE_SPLASH,
643 _NET_WM_WINDOW_TYPE_TOOLBAR,
644 _NET_WM_WINDOW_TYPE_UTILITY,
645 _SWM_WM_STATE_MANUAL,
652 } ewmh[SWM_EWMH_HINT_MAX] = {
653 /* must be in same order as in the enum */
654 {"_NET_ACTIVE_WINDOW", XCB_ATOM_NONE},
655 {"_NET_CLOSE_WINDOW", XCB_ATOM_NONE},
656 {"_NET_MOVERESIZE_WINDOW", XCB_ATOM_NONE},
657 {"_NET_WM_ACTION_ABOVE", XCB_ATOM_NONE},
658 {"_NET_WM_ACTION_CLOSE", XCB_ATOM_NONE},
659 {"_NET_WM_ACTION_FULLSCREEN", XCB_ATOM_NONE},
660 {"_NET_WM_ACTION_MOVE", XCB_ATOM_NONE},
661 {"_NET_WM_ACTION_RESIZE", XCB_ATOM_NONE},
662 {"_NET_WM_ALLOWED_ACTIONS", XCB_ATOM_NONE},
663 {"_NET_WM_NAME", XCB_ATOM_NONE},
664 {"_NET_WM_STATE", XCB_ATOM_NONE},
665 {"_NET_WM_STATE_ABOVE", XCB_ATOM_NONE},
666 {"_NET_WM_STATE_FULLSCREEN", XCB_ATOM_NONE},
667 {"_NET_WM_STATE_HIDDEN", XCB_ATOM_NONE},
668 {"_NET_WM_STATE_MAXIMIZED_HORZ", XCB_ATOM_NONE},
669 {"_NET_WM_STATE_MAXIMIZED_VERT", XCB_ATOM_NONE},
670 {"_NET_WM_STATE_SKIP_PAGER", XCB_ATOM_NONE},
671 {"_NET_WM_STATE_SKIP_TASKBAR", XCB_ATOM_NONE},
672 {"_NET_WM_WINDOW_TYPE", XCB_ATOM_NONE},
673 {"_NET_WM_WINDOW_TYPE_DIALOG", XCB_ATOM_NONE},
674 {"_NET_WM_WINDOW_TYPE_DOCK", XCB_ATOM_NONE},
675 {"_NET_WM_WINDOW_TYPE_NORMAL", XCB_ATOM_NONE},
676 {"_NET_WM_WINDOW_TYPE_SPLASH", XCB_ATOM_NONE},
677 {"_NET_WM_WINDOW_TYPE_TOOLBAR", XCB_ATOM_NONE},
678 {"_NET_WM_WINDOW_TYPE_UTILITY", XCB_ATOM_NONE},
679 {"_SWM_WM_STATE_MANUAL", XCB_ATOM_NONE},
686 XC_BOTTOM_LEFT_CORNER,
687 XC_BOTTOM_RIGHT_CORNER,
695 char *name; /* Name used by Xcursor .*/
696 uint8_t cf_char; /* cursorfont index. */
698 } cursors[XC_MAX] = {
699 {"fleur", XC_fleur, XCB_CURSOR_NONE},
700 {"left_ptr", XC_left_ptr, XCB_CURSOR_NONE},
701 {"bottom_left_corner", XC_bottom_left_corner, XCB_CURSOR_NONE},
702 {"bottom_right_corner", XC_bottom_right_corner, XCB_CURSOR_NONE},
703 {"sizing", XC_sizing, XCB_CURSOR_NONE},
704 {"top_left_corner", XC_top_left_corner, XCB_CURSOR_NONE},
705 {"top_right_corner", XC_top_right_corner, XCB_CURSOR_NONE},
710 TAILQ_ENTRY(spawn_prog) entry;
715 TAILQ_HEAD(spawn_list, spawn_prog);
716 struct spawn_list spawns = TAILQ_HEAD_INITIALIZER(spawns);
718 /* user/key callable function IDs */
830 KF_DUMPWINS, /* MUST BE LAST */
838 enum keyfuncid funcid;
841 RB_HEAD(key_tree, key);
843 /* function prototypes */
844 void adjust_font(struct ws_win *);
845 void bar_class_name(char *, size_t, struct swm_region *);
846 void bar_class_title_name(char *, size_t, struct swm_region *);
847 void bar_cleanup(struct swm_region *);
848 void bar_extra_setup(void);
849 void bar_extra_stop(void);
850 int bar_extra_update(void);
851 void bar_fmt(const char *, char *, struct swm_region *, size_t);
852 void bar_fmt_expand(char *, size_t);
854 void bar_print(struct swm_region *, const char *);
855 void bar_print_legacy(struct swm_region *, const char *);
856 void bar_replace(char *, char *, struct swm_region *, size_t);
857 void bar_replace_pad(char *, int *, size_t);
858 char * bar_replace_seq(char *, char *, struct swm_region *, size_t *,
860 void bar_setup(struct swm_region *);
861 void bar_title_name(char *, size_t, struct swm_region *);
862 void bar_toggle(struct swm_region *, union arg *);
863 void bar_urgent(char *, size_t);
864 void bar_window_float(char *, size_t, struct swm_region *);
865 void bar_window_name(char *, size_t, struct swm_region *);
866 void bar_workspace_name(char *, size_t, struct swm_region *);
867 void buttonpress(xcb_button_press_event_t *);
868 void check_conn(void);
869 void clear_keys(void);
870 void clientmessage(xcb_client_message_event_t *);
871 void client_msg(struct ws_win *, xcb_atom_t, xcb_timestamp_t);
872 int conf_load(const char *, int);
873 void configurenotify(xcb_configure_notify_event_t *);
874 void configurerequest(xcb_configure_request_event_t *);
875 void config_win(struct ws_win *, xcb_configure_request_event_t *);
876 void constrain_window(struct ws_win *, struct swm_region *, int);
877 int count_win(struct workspace *, int);
878 void cursors_cleanup(void);
879 void cursors_load(void);
880 void custom_region(char *);
881 void cyclerg(struct swm_region *, union arg *);
882 void cyclews(struct swm_region *, union arg *);
883 void cycle_layout(struct swm_region *, union arg *);
884 void destroynotify(xcb_destroy_notify_event_t *);
885 void dumpwins(struct swm_region *, union arg *);
887 void enternotify(xcb_enter_notify_event_t *);
888 void event_drain(uint8_t);
889 void event_error(xcb_generic_error_t *);
890 void event_handle(xcb_generic_event_t *);
891 void ewmh_autoquirk(struct ws_win *);
892 void ewmh_get_win_state(struct ws_win *);
893 int ewmh_set_win_fullscreen(struct ws_win *, int);
894 void ewmh_update_actions(struct ws_win *);
895 void ewmh_update_win_state(struct ws_win *, xcb_atom_t, long);
896 char *expand_tilde(const char *);
897 void expose(xcb_expose_event_t *);
898 void fake_keypress(struct ws_win *, xcb_keysym_t, uint16_t);
899 struct pid_e *find_pid(pid_t);
900 struct ws_win *find_unmanaged_window(xcb_window_t);
901 struct ws_win *find_window(xcb_window_t);
902 void floating_toggle(struct swm_region *, union arg *);
903 int floating_toggle_win(struct ws_win *);
904 void focus(struct swm_region *, union arg *);
906 void focusin(xcb_focus_in_event_t *);
907 void focusout(xcb_focus_out_event_t *);
909 void focus_flush(void);
910 void focus_region(struct swm_region *);
911 void focusrg(struct swm_region *, union arg *);
912 void focus_win(struct ws_win *);
913 void fontset_init(void);
914 void free_window(struct ws_win *);
915 xcb_atom_t get_atom_from_string(const char *);
917 char *get_atom_name(xcb_atom_t);
919 struct ws_win *get_focus_magic(struct ws_win *);
920 struct ws_win *get_focus_prev(struct ws_win *);
922 char *get_notify_detail_label(uint8_t);
923 char *get_notify_mode_label(uint8_t);
925 struct ws_win *get_pointer_win(xcb_window_t);
926 struct ws_win *get_region_focus(struct swm_region *);
927 int get_region_index(struct swm_region *);
928 xcb_screen_t *get_screen(int);
930 char *get_stack_mode_name(uint8_t);
932 int32_t get_swm_iconic(struct ws_win *);
933 char *get_win_name(xcb_window_t);
934 int get_ws_idx(xcb_window_t);
935 uint32_t getstate(xcb_window_t);
936 void grabbuttons(struct ws_win *);
938 void grab_windows(void);
939 void iconify(struct swm_region *, union arg *);
941 void keypress(xcb_key_press_event_t *);
942 int key_cmp(struct key *, struct key *);
943 void key_insert(unsigned int, KeySym, enum keyfuncid, const char *);
944 struct key *key_lookup(unsigned int, KeySym);
945 void key_remove(struct key *);
946 void key_replace(struct key *, unsigned int, KeySym, enum keyfuncid,
948 void kill_bar_extra_atexit(void);
949 void kill_refs(struct ws_win *);
951 void leavenotify(xcb_leave_notify_event_t *);
953 void load_float_geom(struct ws_win *, struct swm_region *);
954 struct ws_win *manage_window(xcb_window_t, uint16_t);
955 void map_window(struct ws_win *);
956 void mapnotify(xcb_map_notify_event_t *);
957 void mappingnotify(xcb_mapping_notify_event_t *);
958 void maprequest(xcb_map_request_event_t *);
959 void motionnotify(xcb_motion_notify_event_t *);
960 void move(struct ws_win *, union arg *);
961 void move_step(struct swm_region *, union arg *);
962 uint32_t name_to_pixel(const char *);
963 void name_workspace(struct swm_region *, union arg *);
964 void new_region(struct swm_screen *, int, int, int, int);
965 int parsekeys(char *, unsigned int, unsigned int *, KeySym *);
966 int parsequirks(char *, unsigned long *);
967 int parse_rgb(const char *, uint16_t *, uint16_t *, uint16_t *);
968 void pressbutton(struct swm_region *, union arg *);
969 void priorws(struct swm_region *, union arg *);
971 void print_win_geom(xcb_window_t);
973 void propertynotify(xcb_property_notify_event_t *);
974 void quirk_insert(const char *, const char *, unsigned long);
975 void quirk_remove(struct quirk *);
976 void quirk_replace(struct quirk *, const char *, const char *,
978 void quit(struct swm_region *, union arg *);
979 void raise_toggle(struct swm_region *, union arg *);
980 void resize(struct ws_win *, union arg *);
981 void resize_step(struct swm_region *, union arg *);
982 void restart(struct swm_region *, union arg *);
983 struct swm_region *root_to_region(xcb_window_t, int);
984 void screenchange(xcb_randr_screen_change_notify_event_t *);
985 void scan_xrandr(int);
986 void search_do_resp(void);
987 void search_resp_name_workspace(const char *, unsigned long);
988 void search_resp_search_window(const char *);
989 void search_resp_search_workspace(const char *);
990 void search_resp_uniconify(const char *, unsigned long);
991 void search_win(struct swm_region *, union arg *);
992 void search_win_cleanup(void);
993 void search_workspace(struct swm_region *, union arg *);
994 void send_to_rg(struct swm_region *, union arg *);
995 void send_to_ws(struct swm_region *, union arg *);
996 void set_region(struct swm_region *);
997 int setautorun(char *, char *, int);
998 int setconfbinding(char *, char *, int);
999 int setconfcolor(char *, char *, int);
1000 int setconfmodkey(char *, char *, int);
1001 int setconfquirk(char *, char *, int);
1002 int setconfregion(char *, char *, int);
1003 int setconfspawn(char *, char *, int);
1004 int setconfvalue(char *, char *, int);
1005 void setkeybinding(unsigned int, KeySym, enum keyfuncid, const char *);
1006 int setkeymapping(char *, char *, int);
1007 int setlayout(char *, char *, int);
1008 void setquirk(const char *, const char *, unsigned long);
1009 void setscreencolor(char *, int, int);
1010 void setspawn(const char *, const char *);
1011 void setup_ewmh(void);
1012 void setup_globals(void);
1013 void setup_keys(void);
1014 void setup_quirks(void);
1015 void setup_screens(void);
1016 void setup_spawn(void);
1017 void set_child_transient(struct ws_win *, xcb_window_t *);
1018 void set_swm_iconic(struct ws_win *, int);
1019 void set_win_state(struct ws_win *, uint16_t);
1020 void shutdown_cleanup(void);
1022 void socket_setnonblock(int);
1023 void sort_windows(struct ws_win_list *);
1024 void spawn(int, union arg *, int);
1025 void spawn_custom(struct swm_region *, union arg *, const char *);
1026 int spawn_expand(struct swm_region *, union arg *, const char *, char ***);
1027 void spawn_insert(const char *, const char *);
1028 void spawn_remove(struct spawn_prog *);
1029 void spawn_replace(struct spawn_prog *, const char *, const char *);
1030 void spawn_select(struct swm_region *, union arg *, const char *, int *);
1031 void stack_config(struct swm_region *, union arg *);
1032 void stack_floater(struct ws_win *, struct swm_region *);
1033 void stack_master(struct workspace *, struct swm_geometry *, int, int);
1034 void store_float_geom(struct ws_win *, struct swm_region *);
1035 char * strdupsafe(const char *);
1036 void swapwin(struct swm_region *, union arg *);
1037 void switchws(struct swm_region *, union arg *);
1038 void teardown_ewmh(void);
1039 void unfocus_win(struct ws_win *);
1040 void uniconify(struct swm_region *, union arg *);
1041 void unmanage_window(struct ws_win *);
1042 void unmapnotify(xcb_unmap_notify_event_t *);
1043 void unmap_all(void);
1044 void unmap_window(struct ws_win *);
1045 void updatenumlockmask(void);
1046 void update_modkey(unsigned int);
1047 void update_window(struct ws_win *);
1048 int validate_win(struct ws_win *);
1049 int validate_ws(struct workspace *);
1050 /*void visibilitynotify(xcb_visibility_notify_event_t *);*/
1051 void version(struct swm_region *, union arg *);
1052 pid_t window_get_pid(xcb_window_t);
1053 void wkill(struct swm_region *, union arg *);
1054 void workaround(void);
1055 void xft_init(struct swm_region *);
1057 RB_PROTOTYPE(key_tree, key, entry, key_cmp);
1058 RB_GENERATE(key_tree, key, entry, key_cmp);
1059 struct key_tree keys;
1064 xcb_font_t cf = XCB_NONE;
1067 for (i = 0; i < LENGTH(cursors); ++i) {
1068 /* try to load Xcursor first. */
1069 cursors[i].cid = XcursorLibraryLoadCursor(display,
1072 /* fallback to cursorfont. */
1073 if (cursors[i].cid == XCB_CURSOR_NONE) {
1074 if (cf == XCB_NONE) {
1075 cf = xcb_generate_id(conn);
1076 xcb_open_font(conn, cf, strlen("cursor"),
1080 cursors[i].cid = xcb_generate_id(conn);
1081 xcb_create_glyph_cursor(conn, cursors[i].cid, cf, cf,
1082 cursors[i].cf_char, cursors[i].cf_char + 1, 0, 0, 0,
1083 0xffff, 0xffff, 0xffff);
1089 xcb_close_font(conn, cf);
1093 cursors_cleanup(void)
1096 for (i = 0; i < LENGTH(cursors); ++i)
1097 xcb_free_cursor(conn, cursors[i].cid);
1101 expand_tilde(const char *s)
1103 struct passwd *ppwd;
1111 errx(1, "expand_tilde: NULL string.");
1114 result = strdup(sc);
1120 if ((max = sysconf(_SC_LOGIN_NAME_MAX)) == -1)
1121 errx(1, "expand_tilde: sysconf");
1123 if ((user = calloc(1, max + 1)) == NULL)
1124 errx(1, "expand_tilde: calloc");
1126 for (i = 0; s[i] != '/' && s[i] != '\0'; ++i)
1131 ppwd = strlen(user) == 0 ? getpwuid(getuid()) : getpwnam(user);
1135 result = strdup(sc);
1137 if (asprintf(&result, "%s%s", ppwd->pw_dir, s) == -1)
1141 errx(1, "expand_tilde: failed to allocate memory.");
1147 parse_rgb(const char *rgb, uint16_t *rr, uint16_t *gg, uint16_t *bb)
1149 unsigned int tmpr, tmpg, tmpb;
1151 if (sscanf(rgb, "rgb:%x/%x/%x", &tmpr, &tmpg, &tmpb) != 3)
1154 *rr = RGB_8_TO_16(tmpr);
1155 *gg = RGB_8_TO_16(tmpg);
1156 *bb = RGB_8_TO_16(tmpb);
1162 get_screen(int screen)
1164 const xcb_setup_t *r;
1165 xcb_screen_iterator_t iter;
1167 if ((r = xcb_get_setup(conn)) == NULL) {
1168 DNPRINTF(SWM_D_MISC, "get_screen: xcb_get_setup\n");
1172 iter = xcb_setup_roots_iterator(r);
1173 for (; iter.rem; --screen, xcb_screen_next(&iter))
1181 get_region_index(struct swm_region *r)
1183 struct swm_region *rr;
1189 TAILQ_FOREACH(rr, &r->s->rl, entry) {
1204 if (focus_mode == SWM_FOCUS_DEFAULT)
1205 event_drain(XCB_ENTER_NOTIFY);
1211 get_atom_from_string(const char *str)
1213 xcb_intern_atom_cookie_t c;
1214 xcb_intern_atom_reply_t *r;
1217 c = xcb_intern_atom(conn, 0, strlen(str), str);
1218 r = xcb_intern_atom_reply(conn, c, NULL);
1226 return (XCB_ATOM_NONE);
1230 set_swm_iconic(struct ws_win *win, int newv)
1237 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
1238 a_swm_iconic, XCB_ATOM_INTEGER, 32, 1, &v);
1240 xcb_delete_property(conn, win->id, a_swm_iconic);
1244 get_swm_iconic(struct ws_win *win)
1247 xcb_get_property_reply_t *pr = NULL;
1249 pr = xcb_get_property_reply(conn,
1250 xcb_get_property(conn, 0, win->id, a_swm_iconic,
1251 XCB_ATOM_INTEGER, 0, 1), NULL);
1254 if (pr->type != XCB_ATOM_INTEGER || pr->format != 32)
1256 v = *((int32_t *)xcb_get_property_value(pr));
1266 xcb_atom_t sup_list;
1267 int i, j, num_screens;
1269 sup_list = get_atom_from_string("_NET_SUPPORTED");
1271 for (i = 0; i < LENGTH(ewmh); i++)
1272 ewmh[i].atom = get_atom_from_string(ewmh[i].name);
1274 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1275 for (i = 0; i < num_screens; i++) {
1276 /* Support check window will be created by workaround(). */
1278 /* Report supported atoms */
1279 xcb_delete_property(conn, screens[i].root, sup_list);
1280 for (j = 0; j < LENGTH(ewmh); j++)
1281 xcb_change_property(conn, XCB_PROP_MODE_APPEND,
1282 screens[i].root, sup_list, XCB_ATOM_ATOM, 32, 1,
1291 xcb_atom_t sup_check, sup_list;
1293 xcb_get_property_cookie_t pc;
1294 xcb_get_property_reply_t *pr;
1296 sup_check = get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
1297 sup_list = get_atom_from_string("_NET_SUPPORTED");
1298 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1300 for (i = 0; i < num_screens; i++) {
1301 /* Get the support check window and destroy it */
1302 pc = xcb_get_property(conn, 0, screens[i].root, sup_check,
1303 XCB_ATOM_WINDOW, 0, 1);
1304 pr = xcb_get_property_reply(conn, pc, NULL);
1307 if (pr->format == sup_check) {
1308 id = *((xcb_window_t *)xcb_get_property_value(pr));
1310 xcb_destroy_window(conn, id);
1311 xcb_delete_property(conn, screens[i].root, sup_check);
1312 xcb_delete_property(conn, screens[i].root, sup_list);
1319 ewmh_autoquirk(struct ws_win *win)
1323 xcb_get_property_cookie_t c;
1324 xcb_get_property_reply_t *r;
1326 c = xcb_get_property(conn, 0, win->id,
1327 ewmh[_NET_WM_WINDOW_TYPE].atom, XCB_ATOM_ATOM, 0, UINT32_MAX);
1328 r = xcb_get_property_reply(conn, c, NULL);
1332 n = xcb_get_property_value_length(r);
1333 type = xcb_get_property_value(r);
1335 for (i = 0; i < n; i++) {
1336 if (type[i] == ewmh[_NET_WM_WINDOW_TYPE_NORMAL].atom)
1338 if (type[i] == ewmh[_NET_WM_WINDOW_TYPE_DOCK].atom ||
1339 type[i] == ewmh[_NET_WM_WINDOW_TYPE_TOOLBAR].atom ||
1340 type[i] == ewmh[_NET_WM_WINDOW_TYPE_UTILITY].atom) {
1342 win->quirks = SWM_Q_FLOAT | SWM_Q_ANYWHERE;
1345 if (type[i] == ewmh[_NET_WM_WINDOW_TYPE_SPLASH].atom ||
1346 type[i] == ewmh[_NET_WM_WINDOW_TYPE_DIALOG].atom) {
1348 win->quirks = SWM_Q_FLOAT;
1355 #define SWM_EWMH_ACTION_COUNT_MAX (6)
1356 #define EWMH_F_FULLSCREEN (1<<0)
1357 #define EWMH_F_ABOVE (1<<1)
1358 #define EWMH_F_HIDDEN (1<<2)
1359 #define EWMH_F_SKIP_PAGER (1<<3)
1360 #define EWMH_F_SKIP_TASKBAR (1<<4)
1361 #define SWM_F_MANUAL (1<<5)
1364 ewmh_set_win_fullscreen(struct ws_win *win, int fs)
1372 DNPRINTF(SWM_D_MISC, "ewmh_set_win_fullscreen: window: 0x%x, "
1373 "fullscreen %s\n", win->id, YESNO(fs));
1376 if (!win->g_floatvalid)
1377 store_float_geom(win, win->ws->r);
1379 win->g = win->ws->r->g;
1382 load_float_geom(win, win->ws->r);
1389 ewmh_update_actions(struct ws_win *win)
1391 xcb_atom_t actions[SWM_EWMH_ACTION_COUNT_MAX];
1397 actions[n++] = ewmh[_NET_WM_ACTION_CLOSE].atom;
1399 if (win->floating) {
1400 actions[n++] = ewmh[_NET_WM_ACTION_MOVE].atom;
1401 actions[n++] = ewmh[_NET_WM_ACTION_RESIZE].atom;
1402 actions[n++] = ewmh[_NET_WM_ACTION_ABOVE].atom;
1405 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
1406 ewmh[_NET_WM_ALLOWED_ACTIONS].atom, XCB_ATOM_ATOM, 32, 1, actions);
1409 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
1410 #define _NET_WM_STATE_ADD 1 /* add/set property */
1411 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
1414 ewmh_update_win_state(struct ws_win *win, xcb_atom_t state, long action)
1416 unsigned int mask = 0;
1417 unsigned int changed = 0;
1418 unsigned int orig_flags;
1423 DNPRINTF(SWM_D_PROP, "ewmh_update_win_state: window: 0x%x, state: %ld, "
1424 "action: %ld\n", win->id, (unsigned long)state, action);
1426 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
1427 mask = EWMH_F_FULLSCREEN;
1428 else if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
1429 mask = EWMH_F_ABOVE;
1430 else if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
1431 mask = SWM_F_MANUAL;
1432 else if (state == ewmh[_NET_WM_STATE_SKIP_PAGER].atom)
1433 mask = EWMH_F_SKIP_PAGER;
1434 else if (state == ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom)
1435 mask = EWMH_F_SKIP_TASKBAR;
1437 orig_flags = win->ewmh_flags;
1440 case _NET_WM_STATE_REMOVE:
1441 win->ewmh_flags &= ~mask;
1443 case _NET_WM_STATE_ADD:
1444 win->ewmh_flags |= mask;
1446 case _NET_WM_STATE_TOGGLE:
1447 win->ewmh_flags ^= mask;
1451 changed = (win->ewmh_flags & mask) ^ (orig_flags & mask) ? 1 : 0;
1453 if (state == ewmh[_NET_WM_STATE_ABOVE].atom) {
1454 if (changed && !floating_toggle_win(win))
1455 win->ewmh_flags = orig_flags; /* revert */
1456 } else if (state == ewmh[_SWM_WM_STATE_MANUAL].atom) {
1458 win->manual = (win->ewmh_flags & SWM_F_MANUAL) != 0;
1459 } else if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom) {
1460 if (changed && !ewmh_set_win_fullscreen(win,
1461 win->ewmh_flags & EWMH_F_FULLSCREEN))
1462 win->ewmh_flags = orig_flags; /* revert */
1465 xcb_delete_property(conn, win->id, ewmh[_NET_WM_STATE].atom);
1467 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
1468 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1469 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1470 &ewmh[_NET_WM_STATE_FULLSCREEN].atom);
1471 else if (win->ewmh_flags & EWMH_F_SKIP_PAGER)
1472 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1473 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1474 &ewmh[_NET_WM_STATE_SKIP_PAGER].atom);
1475 else if (win->ewmh_flags & EWMH_F_SKIP_TASKBAR)
1476 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1477 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1478 &ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom);
1479 else if (win->ewmh_flags & EWMH_F_ABOVE)
1480 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1481 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1482 &ewmh[_NET_WM_STATE_ABOVE].atom);
1483 else if (win->ewmh_flags & SWM_F_MANUAL)
1484 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1485 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1486 &ewmh[_SWM_WM_STATE_MANUAL].atom);
1490 ewmh_get_win_state(struct ws_win *win)
1493 xcb_get_property_cookie_t c;
1494 xcb_get_property_reply_t *r;
1500 win->ewmh_flags = 0;
1502 win->ewmh_flags |= EWMH_F_ABOVE;
1504 win->ewmh_flags |= SWM_F_MANUAL;
1506 c = xcb_get_property(conn, 0, win->id, ewmh[_NET_WM_STATE].atom,
1507 XCB_ATOM_ATOM, 0, UINT32_MAX);
1508 r = xcb_get_property_reply(conn, c, NULL);
1512 states = xcb_get_property_value(r);
1513 n = xcb_get_property_value_length(r);
1515 for (i = 0; i < n; i++)
1516 ewmh_update_win_state(win, states[i], _NET_WM_STATE_ADD);
1524 dumpwins(struct swm_region *r, union arg *args)
1528 xcb_get_window_attributes_cookie_t c;
1529 xcb_get_window_attributes_reply_t *wa;
1531 /* suppress unused warning since var is needed */
1534 if (r->ws == NULL) {
1535 warnx("dumpwins: invalid workspace");
1539 warnx("=== managed window list ws %02d ===", r->ws->idx);
1540 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
1541 state = getstate(win->id);
1542 c = xcb_get_window_attributes(conn, win->id);
1543 wa = xcb_get_window_attributes_reply(conn, c, NULL);
1545 warnx("window: 0x%x, map_state: %d, state: %u, "
1546 "transient: 0x%x", win->id, wa->map_state,
1547 state, win->transient);
1550 warnx("window: 0x%x, failed xcb_get_window_attributes",
1554 warnx("===== unmanaged window list =====");
1555 TAILQ_FOREACH(win, &r->ws->unmanagedlist, entry) {
1556 state = getstate(win->id);
1557 c = xcb_get_window_attributes(conn, win->id);
1558 wa = xcb_get_window_attributes_reply(conn, c, NULL);
1560 warnx("window: 0x%x, map_state: %d, state: %u, "
1561 "transient: 0x%x", win->id, wa->map_state,
1562 state, win->transient);
1565 warnx("window: 0x%x, failed xcb_get_window_attributes",
1569 warnx("=================================");
1573 dumpwins(struct swm_region *r, union arg *s)
1578 #endif /* SWM_DEBUG */
1583 int saved_errno, status;
1586 saved_errno = errno;
1590 while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) != 0) {
1595 if (errno != ECHILD)
1596 warn("sighdlr: waitpid");
1597 #endif /* SWM_DEBUG */
1600 if (pid == searchpid)
1604 if (WIFEXITED(status)) {
1605 if (WEXITSTATUS(status) != 0)
1606 warnx("sighdlr: child exit status: %d",
1607 WEXITSTATUS(status));
1609 warnx("sighdlr: child is terminated "
1611 #endif /* SWM_DEBUG */
1625 errno = saved_errno;
1631 struct pid_e *p = NULL;
1633 DNPRINTF(SWM_D_MISC, "find_pid: %d\n", pid);
1638 TAILQ_FOREACH(p, &pidlist, entry) {
1647 name_to_pixel(const char *colorname)
1649 uint32_t result = 0;
1650 char cname[32] = "#";
1651 xcb_screen_t *screen;
1652 xcb_colormap_t cmap;
1653 xcb_alloc_color_reply_t *cr;
1654 xcb_alloc_named_color_reply_t *nr;
1655 uint16_t rr, gg, bb;
1657 screen = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
1658 cmap = screen->default_colormap;
1660 /* color is in format rgb://rr/gg/bb */
1661 if (strncmp(colorname, "rgb:", 4) == 0) {
1662 if (parse_rgb(colorname, &rr, &gg, &bb) == -1)
1663 warnx("could not parse rgb %s", colorname);
1665 cr = xcb_alloc_color_reply(conn,
1666 xcb_alloc_color(conn, cmap, rr, gg, bb),
1672 warnx("color '%s' not found", colorname);
1675 nr = xcb_alloc_named_color_reply(conn,
1676 xcb_alloc_named_color(conn, cmap, strlen(colorname),
1679 strlcat(cname, colorname + 2, sizeof cname - 1);
1680 nr = xcb_alloc_named_color_reply(conn,
1681 xcb_alloc_named_color(conn, cmap, strlen(cname),
1688 warnx("color '%s' not found", colorname);
1695 setscreencolor(char *val, int i, int c)
1699 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1700 if (i > 0 && i <= num_screens) {
1701 screens[i - 1].c[c].pixel = name_to_pixel(val);
1702 free(screens[i - 1].c[c].name);
1703 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
1705 } else if (i == -1) {
1706 for (i = 0; i < num_screens; i++) {
1707 screens[i].c[c].pixel = name_to_pixel(val);
1708 free(screens[i].c[c].name);
1709 if ((screens[i].c[c].name = strdup(val)) == NULL)
1713 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1718 fancy_stacker(struct workspace *ws)
1720 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1721 if (ws->cur_layout->l_stack == vertical_stack)
1722 snprintf(ws->stacker, sizeof ws->stacker,
1723 ws->l_state.vertical_flip ? "[%d>%d]" : "[%d|%d]",
1724 ws->l_state.vertical_mwin, ws->l_state.vertical_stacks);
1725 if (ws->cur_layout->l_stack == horizontal_stack)
1726 snprintf(ws->stacker, sizeof ws->stacker,
1727 ws->l_state.horizontal_flip ? "[%dv%d]" : "[%d-%d]",
1728 ws->l_state.horizontal_mwin, ws->l_state.horizontal_stacks);
1732 plain_stacker(struct workspace *ws)
1734 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1735 if (ws->cur_layout->l_stack == vertical_stack)
1736 strlcpy(ws->stacker, ws->l_state.vertical_flip ? "[>]" : "[|]",
1737 sizeof ws->stacker);
1738 if (ws->cur_layout->l_stack == horizontal_stack)
1739 strlcpy(ws->stacker, ws->l_state.horizontal_flip ? "[v]" : "[-]",
1740 sizeof ws->stacker);
1744 custom_region(char *val)
1746 unsigned int x, y, w, h;
1747 int sidx, num_screens;
1748 xcb_screen_t *screen;
1750 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1751 if (sscanf(val, "screen[%d]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
1752 errx(1, "invalid custom region, "
1753 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1754 if (sidx < 1 || sidx > num_screens)
1755 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1759 if ((screen = get_screen(sidx)) == NULL)
1760 errx(1, "ERROR: can't get screen %d.", sidx);
1763 errx(1, "region %ux%u+%u+%u too small", w, h, x, y);
1765 if (x > screen->width_in_pixels ||
1766 y > screen->height_in_pixels ||
1767 w + x > screen->width_in_pixels ||
1768 h + y > screen->height_in_pixels) {
1769 warnx("ignoring region %ux%u+%u+%u - not within screen "
1770 "boundaries (%ux%u)", w, h, x, y,
1771 screen->width_in_pixels, screen->height_in_pixels);
1775 new_region(&screens[sidx], x, y, w, h);
1779 socket_setnonblock(int fd)
1783 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
1784 err(1, "fcntl F_GETFL");
1785 flags |= O_NONBLOCK;
1786 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
1787 err(1, "fcntl F_SETFL");
1791 bar_print_legacy(struct swm_region *r, const char *s)
1793 xcb_rectangle_t rect;
1798 XRectangle ibox, lbox;
1802 XmbTextExtents(bar_fs, s, len, &ibox, &lbox);
1804 switch (bar_justify) {
1805 case SWM_BAR_JUSTIFY_LEFT:
1808 case SWM_BAR_JUSTIFY_CENTER:
1809 x = (WIDTH(r) - lbox.width) / 2;
1811 case SWM_BAR_JUSTIFY_RIGHT:
1812 x = WIDTH(r) - lbox.width - SWM_BAR_OFFSET;
1816 if (x < SWM_BAR_OFFSET)
1821 rect.width = WIDTH(r->bar);
1822 rect.height = HEIGHT(r->bar);
1824 /* clear back buffer */
1825 gcv[0] = r->s->c[SWM_S_COLOR_BAR].pixel;
1826 xcb_change_gc(conn, r->s->bar_gc, XCB_GC_FOREGROUND, gcv);
1827 xcb_poly_fill_rectangle(conn, r->bar->buffer, r->s->bar_gc, 1, &rect);
1829 /* draw back buffer */
1830 gcvd.graphics_exposures = 0;
1831 draw = XCreateGC(display, r->bar->buffer, GCGraphicsExposures, &gcvd);
1832 XSetForeground(display, draw, r->s->c[SWM_S_COLOR_BAR_FONT].pixel);
1833 DRAWSTRING(display, r->bar->buffer, bar_fs, draw,
1834 x, (bar_fs_extents->max_logical_extent.height - lbox.height) / 2 -
1836 XFreeGC(display, draw);
1839 xcb_copy_area(conn, r->bar->buffer, r->bar->id, r->s->bar_gc, 0, 0,
1840 0, 0, WIDTH(r->bar), HEIGHT(r->bar));
1844 bar_print(struct swm_region *r, const char *s)
1847 xcb_rectangle_t rect;
1855 XftTextExtentsUtf8(display, bar_font, (FcChar8 *)s, len, &info);
1857 switch (bar_justify) {
1858 case SWM_BAR_JUSTIFY_LEFT:
1861 case SWM_BAR_JUSTIFY_CENTER:
1862 x = (WIDTH(r) - info.width) / 2;
1864 case SWM_BAR_JUSTIFY_RIGHT:
1865 x = WIDTH(r) - info.width - SWM_BAR_OFFSET;
1869 if (x < SWM_BAR_OFFSET)
1874 rect.width = WIDTH(r->bar);
1875 rect.height = HEIGHT(r->bar);
1877 /* clear back buffer */
1878 gcv[0] = r->s->c[SWM_S_COLOR_BAR].pixel;
1879 xcb_change_gc(conn, r->s->bar_gc, XCB_GC_FOREGROUND, gcv);
1880 xcb_poly_fill_rectangle(conn, r->bar->buffer, r->s->bar_gc, 1, &rect);
1882 /* draw back buffer */
1883 draw = XftDrawCreate(display, r->bar->buffer,
1884 DefaultVisual(display, r->s->idx),
1885 DefaultColormap(display, r->s->idx));
1887 XftDrawStringUtf8(draw, &bar_font_color, bar_font, x,
1888 (HEIGHT(r->bar) + bar_font->height) / 2 - bar_font->descent,
1891 XftDrawDestroy(draw);
1894 xcb_copy_area(conn, r->bar->buffer, r->bar->id, r->s->bar_gc, 0, 0,
1895 0, 0, WIDTH(r->bar), HEIGHT(r->bar));
1899 bar_extra_stop(void)
1903 bzero(bar_pipe, sizeof bar_pipe);
1906 kill(bar_pid, SIGTERM);
1909 strlcpy(bar_ext, "", sizeof bar_ext);
1914 bar_class_name(char *s, size_t sz, struct swm_region *r)
1916 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1918 if (r->ws->focus->ch.class_name != NULL)
1919 strlcat(s, r->ws->focus->ch.class_name, sz);
1923 bar_title_name(char *s, size_t sz, struct swm_region *r)
1925 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1927 if (r->ws->focus->ch.instance_name != NULL)
1928 strlcat(s, r->ws->focus->ch.instance_name, sz);
1932 bar_class_title_name(char *s, size_t sz, struct swm_region *r)
1934 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1937 bar_class_name(s, sz, r);
1938 strlcat(s, ":", sz);
1939 bar_title_name(s, sz, r);
1943 bar_window_float(char *s, size_t sz, struct swm_region *r)
1945 if (r == NULL || r ->ws == NULL || r->ws->focus == NULL)
1947 if (r->ws->focus->floating)
1948 strlcat(s, "(f)", sz);
1952 bar_window_name(char *s, size_t sz, struct swm_region *r)
1956 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1958 if ((title = get_win_name(r->ws->focus->id)) == NULL)
1961 strlcat(s, title, sz);
1965 int urgent[SWM_WS_MAX];
1967 bar_urgent(char *s, size_t sz)
1970 int i, j, num_screens;
1972 xcb_get_property_cookie_t c;
1973 xcb_icccm_wm_hints_t hints;
1975 for (i = 0; i < workspace_limit; i++)
1978 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1979 for (i = 0; i < num_screens; i++)
1980 for (j = 0; j < workspace_limit; j++)
1981 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry) {
1982 c = xcb_icccm_get_wm_hints(conn, win->id);
1983 if (xcb_icccm_get_wm_hints_reply(conn, c,
1986 if (hints.flags & XCB_ICCCM_WM_HINT_X_URGENCY)
1990 for (i = 0; i < workspace_limit; i++) {
1992 snprintf(b, sizeof b, "%d ", i + 1);
1994 snprintf(b, sizeof b, "- ");
2000 bar_workspace_name(char *s, size_t sz, struct swm_region *r)
2002 if (r == NULL || r->ws == NULL)
2004 if (r->ws->name != NULL)
2005 strlcat(s, r->ws->name, sz);
2008 /* build the default bar format according to the defined enabled options */
2010 bar_fmt(const char *fmtexp, char *fmtnew, struct swm_region *r, size_t sz)
2012 /* if format provided, just copy the buffers */
2013 if (bar_format != NULL) {
2014 strlcpy(fmtnew, fmtexp, sz);
2018 /* reset the output buffer */
2021 strlcat(fmtnew, "+N:+I ", sz);
2023 strlcat(fmtnew, "+S", sz);
2024 strlcat(fmtnew, " ", sz);
2026 /* only show the workspace name if there's actually one */
2027 if (r != NULL && r->ws != NULL && r->ws->name != NULL)
2028 strlcat(fmtnew, "<+D>", sz);
2029 strlcat(fmtnew, "+3<", sz);
2031 if (clock_enabled) {
2032 strlcat(fmtnew, fmtexp, sz);
2033 strlcat(fmtnew, "+4<", sz);
2036 /* bar_urgent already adds the space before the last asterisk */
2038 strlcat(fmtnew, "* +U*+4<", sz);
2040 if (title_class_enabled) {
2041 strlcat(fmtnew, "+C", sz);
2042 if (!title_name_enabled)
2043 strlcat(fmtnew, "+4<", sz);
2046 /* checks needed by the colon and floating strlcat(3) calls below */
2047 if (r != NULL && r->ws != NULL && r->ws->focus != NULL) {
2048 if (title_name_enabled) {
2049 if (title_class_enabled)
2050 strlcat(fmtnew, ":", sz);
2051 strlcat(fmtnew, "+T+4<", sz);
2053 if (window_name_enabled) {
2054 if (r->ws->focus->floating)
2055 strlcat(fmtnew, "+F ", sz);
2056 strlcat(fmtnew, "+64W ", sz);
2060 /* finally add the action script output and the version */
2061 strlcat(fmtnew, "+4<+A+4<+V", sz);
2065 bar_replace_pad(char *tmp, int *limit, size_t sz)
2067 /* special case; no limit given, pad one space, instead */
2068 if (*limit == (int)sz - 1)
2070 snprintf(tmp, sz, "%*s", *limit, " ");
2073 /* replaces the bar format character sequences (like in tmux(1)) */
2075 bar_replace_seq(char *fmt, char *fmtrep, struct swm_region *r, size_t *offrep,
2079 char tmp[SWM_BAR_MAX];
2083 /* reset strlcat(3) buffer */
2086 /* get number, if any */
2089 if (sscanf(fmt, "%d%n", &limit, &size) != 1)
2090 limit = sizeof tmp - 1;
2091 if (limit <= 0 || limit >= (int)sizeof tmp)
2092 limit = sizeof tmp - 1;
2094 /* there is nothing to replace (ie EOL) */
2101 bar_replace_pad(tmp, &limit, sizeof tmp);
2104 snprintf(tmp, sizeof tmp, "%s", bar_ext);
2107 bar_class_name(tmp, sizeof tmp, r);
2110 bar_workspace_name(tmp, sizeof tmp, r);
2113 bar_window_float(tmp, sizeof tmp, r);
2116 snprintf(tmp, sizeof tmp, "%d", r->ws->idx + 1);
2119 snprintf(tmp, sizeof tmp, "%d", r->s->idx + 1);
2122 bar_class_title_name(tmp, sizeof tmp, r);
2125 snprintf(tmp, sizeof tmp, "%s", r->ws->stacker);
2128 bar_title_name(tmp, sizeof tmp, r);
2131 bar_urgent(tmp, sizeof tmp);
2134 snprintf(tmp, sizeof tmp, "%s", bar_vertext);
2137 bar_window_name(tmp, sizeof tmp, r);
2140 /* unknown character sequence; copy as-is */
2141 snprintf(tmp, sizeof tmp, "+%c", *fmt);
2147 if ((int)len < limit)
2149 while (limit-- > 0) {
2150 if (*offrep >= sz - 1)
2152 fmtrep[(*offrep)++] = *ptr++;
2160 bar_replace(char *fmt, char *fmtrep, struct swm_region *r, size_t sz)
2165 while (*fmt != '\0') {
2167 /* skip ordinary characters */
2170 fmtrep[off++] = *fmt++;
2174 /* character sequence found; replace it */
2175 fmt = bar_replace_seq(fmt, fmtrep, r, &off, sz);
2184 bar_fmt_expand(char *fmtexp, size_t sz)
2191 /* start by grabbing the current time and date */
2193 localtime_r(&tmt, &tm);
2195 /* figure out what to expand */
2196 if (bar_format != NULL)
2198 else if (bar_format == NULL && clock_enabled)
2200 /* if nothing to expand bail out */
2206 /* copy as-is, just in case the format shouldn't be expanded below */
2207 strlcpy(fmtexp, fmt, sz);
2208 /* finally pass the string through strftime(3) */
2209 #ifndef SWM_DENY_CLOCK_FORMAT
2210 if ((len = strftime(fmtexp, sz, fmt, &tm)) == 0)
2211 warnx("format too long");
2216 /* Redraws the bar; need to follow with xcb_flush() or focus_flush(). */
2220 char fmtexp[SWM_BAR_MAX], fmtnew[SWM_BAR_MAX];
2221 char fmtrep[SWM_BAR_MAX];
2223 struct swm_region *r;
2225 /* expand the format by first passing it through strftime(3) */
2226 bar_fmt_expand(fmtexp, sizeof fmtexp);
2228 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2229 for (i = 0; i < num_screens; i++) {
2230 TAILQ_FOREACH(r, &screens[i].rl, entry) {
2234 if (bar_enabled && r->ws->bar_enabled)
2235 xcb_map_window(conn, r->bar->id);
2237 xcb_unmap_window(conn, r->bar->id);
2241 bar_fmt(fmtexp, fmtnew, r, sizeof fmtnew);
2242 bar_replace(fmtnew, fmtrep, r, sizeof fmtrep);
2243 if (bar_font_legacy)
2244 bar_print_legacy(r, fmtrep);
2246 bar_print(r, fmtrep);
2252 * Reads external script output; call when stdin is readable.
2253 * Returns 1 if bar_ext was updated; otherwise 0.
2256 bar_extra_update(void)
2259 char b[SWM_BAR_MAX];
2265 while (fgets(b, sizeof(b), stdin) != NULL) {
2268 if (b[len - 1] == '\n') {
2269 /* Remove newline. */
2272 /* "Clear" bar_ext. */
2275 /* Flush buffered output. */
2276 strlcpy(bar_ext, bar_ext_buf, sizeof(bar_ext));
2277 bar_ext_buf[0] = '\0';
2279 /* Append new output to bar. */
2280 strlcat(bar_ext, b, sizeof(bar_ext));
2284 /* Buffer output. */
2285 strlcat(bar_ext_buf, b, sizeof(bar_ext_buf));
2290 if (errno != EAGAIN) {
2291 warn("bar_action failed");
2300 bar_toggle(struct swm_region *r, union arg *args)
2302 struct swm_region *tmpr;
2305 /* suppress unused warnings since vars are needed */
2309 DNPRINTF(SWM_D_BAR, "bar_toggle\n");
2312 case SWM_ARG_ID_BAR_TOGGLE_WS:
2313 /* Only change if master switch is enabled. */
2315 r->ws->bar_enabled = !r->ws->bar_enabled;
2317 bar_enabled = r->ws->bar_enabled = 1;
2319 case SWM_ARG_ID_BAR_TOGGLE:
2320 bar_enabled = !bar_enabled;
2324 /* update bars as necessary */
2325 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2326 for (i = 0; i < num_screens; i++)
2327 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
2329 if (bar_enabled && tmpr->ws->bar_enabled)
2330 xcb_map_window(conn, tmpr->bar->id);
2332 xcb_unmap_window(conn, tmpr->bar->id);
2337 /* must be after stack */
2344 bar_extra_setup(void)
2346 /* do this here because the conf file is in memory */
2347 if (!bar_extra && bar_argv[0]) {
2348 /* launch external status app */
2350 if (pipe(bar_pipe) == -1)
2351 err(1, "pipe error");
2352 socket_setnonblock(bar_pipe[0]);
2353 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
2355 /* Set stdin to read from the pipe. */
2356 if (dup2(bar_pipe[0], STDIN_FILENO) == -1)
2359 /* Set stdout to write to the pipe. */
2360 if (dup2(bar_pipe[1], STDOUT_FILENO) == -1)
2363 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
2364 err(1, "could not disable SIGPIPE");
2365 switch (bar_pid = fork()) {
2367 err(1, "cannot fork");
2371 execvp(bar_argv[0], bar_argv);
2372 err(1, "%s external app failed", bar_argv[0]);
2374 default: /* parent */
2379 atexit(kill_bar_extra_atexit);
2384 kill_bar_extra_atexit(void)
2387 kill(bar_pid, SIGTERM);
2395 while ((s = index(s, '-'))) {
2400 return (count == 14);
2406 char *default_string;
2407 char **missing_charsets;
2408 int num_missing_charsets = 0;
2412 XFreeFontSet(display, bar_fs);
2416 DNPRINTF(SWM_D_INIT, "fontset_init: loading bar_fonts: %s\n", bar_fonts);
2418 bar_fs = XCreateFontSet(display, bar_fonts, &missing_charsets,
2419 &num_missing_charsets, &default_string);
2421 if (num_missing_charsets > 0) {
2422 warnx("Unable to load charset(s):");
2424 for (i = 0; i < num_missing_charsets; ++i)
2425 warnx("%s", missing_charsets[i]);
2427 XFreeStringList(missing_charsets);
2429 if (strcmp(default_string, ""))
2430 warnx("Glyphs from those sets will be replaced "
2431 "by '%s'.", default_string);
2433 warnx("Glyphs from those sets won't be drawn.");
2437 errx(1, "Error creating font set structure.");
2439 bar_fs_extents = XExtentsOfFontSet(bar_fs);
2441 bar_height = bar_fs_extents->max_logical_extent.height +
2442 2 * bar_border_width;
2449 xft_init(struct swm_region *r)
2451 char *font, *d, *search;
2454 if (bar_font == NULL) {
2455 if ((d = strdup(bar_fonts)) == NULL)
2456 errx(1, "insufficient memory.");
2458 while ((font = strsep(&search, ",")) != NULL) {
2462 DNPRINTF(SWM_D_INIT, "xft_init: try font %s\n", font);
2465 bar_font = XftFontOpenXlfd(display, r->s->idx,
2468 bar_font = XftFontOpenName(display, r->s->idx,
2473 warnx("unable to load font %s", font);
2476 DNPRINTF(SWM_D_INIT, "successfully opened "
2484 if (bar_font == NULL)
2485 errx(1, "unable to open a font");
2487 PIXEL_TO_XRENDERCOLOR(r->s->c[SWM_S_COLOR_BAR_FONT].pixel, color);
2489 if (!XftColorAllocValue(display, DefaultVisual(display, r->s->idx),
2490 DefaultColormap(display, r->s->idx), &color, &bar_font_color))
2491 warn("Xft error: unable to allocate color.");
2493 bar_height = bar_font->height + 2 * bar_border_width;
2500 bar_setup(struct swm_region *r)
2502 xcb_screen_t *screen;
2505 DNPRINTF(SWM_D_BAR, "bar_setup: screen %d.\n",
2508 if ((screen = get_screen(r->s->idx)) == NULL)
2509 errx(1, "ERROR: can't get screen %d.", r->s->idx);
2514 if ((r->bar = calloc(1, sizeof(struct swm_bar))) == NULL)
2515 err(1, "bar_setup: calloc: failed to allocate memory.");
2517 if (bar_font_legacy)
2523 Y(r->bar) = bar_at_bottom ? (Y(r) + HEIGHT(r) - bar_height) : Y(r);
2524 WIDTH(r->bar) = WIDTH(r) - 2 * bar_border_width;
2525 HEIGHT(r->bar) = bar_height - 2 * bar_border_width;
2527 /* Assume region is unfocused when we create the bar. */
2528 r->bar->id = xcb_generate_id(conn);
2529 wa[0] = r->s->c[SWM_S_COLOR_BAR].pixel;
2530 wa[1] = r->s->c[SWM_S_COLOR_BAR_BORDER_UNFOCUS].pixel;
2531 wa[2] = XCB_EVENT_MASK_EXPOSURE;
2533 xcb_create_window(conn, XCB_COPY_FROM_PARENT, r->bar->id, r->s->root,
2534 X(r->bar), Y(r->bar), WIDTH(r->bar), HEIGHT(r->bar),
2535 bar_border_width, XCB_WINDOW_CLASS_INPUT_OUTPUT,
2536 XCB_COPY_FROM_PARENT, XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL
2537 | XCB_CW_EVENT_MASK, wa);
2539 r->bar->buffer = xcb_generate_id(conn);
2540 xcb_create_pixmap(conn, screen->root_depth, r->bar->buffer, r->bar->id,
2541 WIDTH(r->bar), HEIGHT(r->bar));
2544 xcb_randr_select_input(conn, r->bar->id,
2545 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE);
2548 xcb_map_window(conn, r->bar->id);
2550 DNPRINTF(SWM_D_BAR, "bar_setup: window: 0x%x, (x,y) w x h: (%d,%d) "
2551 "%d x %d\n", WINID(r->bar), X(r->bar), Y(r->bar), WIDTH(r->bar),
2558 bar_cleanup(struct swm_region *r)
2562 xcb_destroy_window(conn, r->bar->id);
2563 xcb_free_pixmap(conn, r->bar->buffer);
2569 set_win_state(struct ws_win *win, uint16_t state)
2571 uint16_t data[2] = { state, XCB_ATOM_NONE };
2573 DNPRINTF(SWM_D_EVENT, "set_win_state: window: 0x%x, state: %u\n",
2579 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id, a_state,
2580 a_state, 32, 2, data);
2584 getstate(xcb_window_t w)
2586 uint32_t result = 0;
2587 xcb_get_property_cookie_t c;
2588 xcb_get_property_reply_t *r;
2590 c = xcb_get_property(conn, 0, w, a_state, a_state, 0L, 2L);
2591 r = xcb_get_property_reply(conn, c, NULL);
2594 if (r->type == a_state && r->format == 32 && r->length == 2)
2595 result = *((uint32_t *)xcb_get_property_value(r));
2599 DNPRINTF(SWM_D_MISC, "getstate property: win 0x%x state %u\n", w,
2605 version(struct swm_region *r, union arg *args)
2607 /* suppress unused warnings since vars are needed */
2611 bar_version = !bar_version;
2613 snprintf(bar_vertext, sizeof bar_vertext,
2614 "Version: %s Build: %s", SPECTRWM_VERSION, buildstr);
2616 strlcpy(bar_vertext, "", sizeof bar_vertext);
2623 client_msg(struct ws_win *win, xcb_atom_t a, xcb_timestamp_t t)
2625 xcb_client_message_event_t ev;
2633 name = get_atom_name(a);
2634 DNPRINTF(SWM_D_EVENT, "client_msg: window: 0x%x, atom: %s(%u), "
2636 win->id, name, a, t);
2640 bzero(&ev, sizeof ev);
2641 ev.response_type = XCB_CLIENT_MESSAGE;
2642 ev.window = win->id;
2645 ev.data.data32[0] = a;
2646 ev.data.data32[1] = t;
2648 xcb_send_event(conn, 0, win->id,
2649 XCB_EVENT_MASK_NO_EVENT, (const char *)&ev);
2652 /* synthetic response to a ConfigureRequest when not making a change */
2654 config_win(struct ws_win *win, xcb_configure_request_event_t *ev)
2656 xcb_configure_notify_event_t ce;
2661 /* send notification of unchanged state. */
2662 bzero(&ce, sizeof(ce));
2663 ce.response_type = XCB_CONFIGURE_NOTIFY;
2666 ce.width = WIDTH(win);
2667 ce.height = HEIGHT(win);
2668 ce.override_redirect = 0;
2673 ce.window = win->id;
2674 ce.border_width = BORDER(win);
2675 ce.above_sibling = XCB_WINDOW_NONE;
2678 ce.event = ev->window;
2679 ce.window = ev->window;
2681 /* make response appear more WM_SIZE_HINTS-compliant */
2682 if (win->sh.flags) {
2683 DNPRINTF(SWM_D_MISC, "config_win: hints: window: 0x%x,"
2684 " sh.flags: %u, min: %d x %d, max: %d x %d, inc: "
2685 "%d x %d\n", win->id, win->sh.flags, SH_MIN_W(win),
2686 SH_MIN_H(win), SH_MAX_W(win), SH_MAX_H(win),
2687 SH_INC_W(win), SH_INC_H(win));
2692 /* the hint may be set... to 0! */
2693 if (SH_MIN_W(win) > 0 && ce.width < SH_MIN_W(win))
2694 ce.width = SH_MIN_W(win);
2695 if (SH_MIN_H(win) > 0 && ce.height < SH_MIN_H(win))
2696 ce.height = SH_MIN_H(win);
2701 /* may also be advertized as 0 */
2702 if (SH_MAX_W(win) > 0 && ce.width > SH_MAX_W(win))
2703 ce.width = SH_MAX_W(win);
2704 if (SH_MAX_H(win) > 0 && ce.height > SH_MAX_H(win))
2705 ce.height = SH_MAX_H(win);
2708 /* resize increment. */
2710 if (SH_INC_W(win) > 1 && ce.width > SH_INC_W(win))
2711 ce.width -= (ce.width - SH_MIN_W(win)) %
2713 if (SH_INC_H(win) > 1 && ce.height > SH_INC_H(win))
2714 ce.height -= (ce.height - SH_MIN_H(win)) %
2718 /* adjust x and y for requested border_width. */
2719 ce.x += BORDER(win) - ev->border_width;
2720 ce.y += BORDER(win) - ev->border_width;
2721 ce.border_width = ev->border_width;
2722 ce.above_sibling = ev->sibling;
2725 DNPRINTF(SWM_D_MISC, "config_win: ewmh: %s, window: 0x%x, (x,y) w x h: "
2726 "(%d,%d) %d x %d, border: %d\n", YESNO(ev == NULL), win->id, ce.x,
2727 ce.y, ce.width, ce.height, ce.border_width);
2729 xcb_send_event(conn, 0, win->id, XCB_EVENT_MASK_STRUCTURE_NOTIFY,
2734 count_win(struct workspace *ws, int count_transient)
2739 TAILQ_FOREACH(win, &ws->winlist, entry) {
2740 if (!count_transient && win->floating)
2742 if (!count_transient && win->transient)
2748 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
2754 quit(struct swm_region *r, union arg *args)
2756 /* suppress unused warnings since vars are needed */
2760 DNPRINTF(SWM_D_MISC, "quit\n");
2765 map_window(struct ws_win *win)
2767 uint32_t val = XCB_STACK_MODE_ABOVE;
2772 DNPRINTF(SWM_D_EVENT, "map_window: win 0x%x, mapped: %s\n", win->id,
2773 YESNO(win->mapped));
2775 xcb_configure_window(conn, win->id,
2776 XCB_CONFIG_WINDOW_STACK_MODE, &val);
2781 xcb_map_window(conn, win->id);
2782 set_win_state(win, XCB_ICCCM_WM_STATE_NORMAL);
2787 unmap_window(struct ws_win *win)
2792 DNPRINTF(SWM_D_EVENT, "unmap_window: win 0x%x, mapped: %s\n", win->id,
2793 YESNO(win->mapped));
2798 xcb_unmap_window(conn, win->id);
2799 set_win_state(win, XCB_ICCCM_WM_STATE_ICONIC);
2807 int i, j, num_screens;
2809 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2810 for (i = 0; i < num_screens; i++)
2811 for (j = 0; j < workspace_limit; j++)
2812 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2817 fake_keypress(struct ws_win *win, xcb_keysym_t keysym, uint16_t modifiers)
2819 xcb_key_press_event_t event;
2820 xcb_keycode_t *keycode;
2825 keycode = xcb_key_symbols_get_keycode(syms, keysym);
2827 DNPRINTF(SWM_D_MISC, "fake_keypress: win 0x%x keycode %u\n",
2830 bzero(&event, sizeof(event));
2831 event.event = win->id;
2832 event.root = win->s->root;
2833 event.child = XCB_WINDOW_NONE;
2834 event.time = XCB_CURRENT_TIME;
2835 event.event_x = X(win);
2836 event.event_y = Y(win);
2839 event.same_screen = 1;
2840 event.detail = *keycode;
2841 event.state = modifiers;
2843 event.response_type = XCB_KEY_PRESS;
2844 xcb_send_event(conn, 1, win->id,
2845 XCB_EVENT_MASK_KEY_PRESS, (const char *)&event);
2847 event.response_type = XCB_KEY_RELEASE;
2848 xcb_send_event(conn, 1, win->id,
2849 XCB_EVENT_MASK_KEY_RELEASE, (const char *)&event);
2855 restart(struct swm_region *r, union arg *args)
2857 /* suppress unused warning since var is needed */
2861 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
2865 execvp(start_argv[0], start_argv);
2866 warn("execvp failed");
2871 get_pointer_win(xcb_window_t root)
2873 struct ws_win *win = NULL;
2874 xcb_query_pointer_reply_t *r;
2876 DNPRINTF(SWM_D_EVENT, "get_pointer_win: root: 0x%x.\n", root);
2878 r = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, root), NULL);
2880 win = find_window(r->child);
2882 DNPRINTF(SWM_D_EVENT, "get_pointer_win: 0x%x.\n",
2885 DNPRINTF(SWM_D_EVENT, "get_pointer_win: none.\n");
2894 root_to_region(xcb_window_t root, int check)
2897 struct swm_region *r = NULL;
2899 xcb_query_pointer_reply_t *qpr;
2900 xcb_get_input_focus_reply_t *gifr;
2902 DNPRINTF(SWM_D_MISC, "root_to_region: window: 0x%x\n", root);
2904 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2905 for (i = 0; i < num_screens; i++)
2906 if (screens[i].root == root)
2909 if (check & SWM_CK_REGION)
2910 r = screens[i].r_focus;
2912 if (r == NULL && check & SWM_CK_FOCUS) {
2913 /* Try to find an actively focused window */
2914 gifr = xcb_get_input_focus_reply(conn,
2915 xcb_get_input_focus(conn), NULL);
2917 cfw = find_window(gifr->focus);
2918 if (cfw && cfw->ws->r)
2925 if (r == NULL && check & SWM_CK_POINTER) {
2926 /* No region with an active focus; try to use pointer. */
2927 qpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn,
2928 screens[i].root), NULL);
2931 DNPRINTF(SWM_D_MISC, "root_to_region: pointer: "
2932 "(%d,%d)\n", qpr->root_x, qpr->root_y);
2933 TAILQ_FOREACH(r, &screens[i].rl, entry)
2934 if (X(r) <= qpr->root_x &&
2935 qpr->root_x < MAX_X(r) &&
2936 Y(r) <= qpr->root_y &&
2937 qpr->root_y < MAX_Y(r))
2944 if (r == NULL && check & SWM_CK_FALLBACK)
2945 r = TAILQ_FIRST(&screens[i].rl);
2951 find_unmanaged_window(xcb_window_t id)
2954 int i, j, num_screens;
2956 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2957 for (i = 0; i < num_screens; i++)
2958 for (j = 0; j < workspace_limit; j++)
2959 TAILQ_FOREACH(win, &screens[i].ws[j].unmanagedlist,
2967 find_window(xcb_window_t id)
2970 int i, j, num_screens;
2971 xcb_query_tree_reply_t *r;
2973 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2974 for (i = 0; i < num_screens; i++)
2975 for (j = 0; j < workspace_limit; j++)
2976 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2980 r = xcb_query_tree_reply(conn, xcb_query_tree(conn, id), NULL);
2984 /* if we were looking for the parent return that window instead */
2985 if (r->parent == 0 || r->root == r->parent) {
2990 /* look for parent */
2991 for (i = 0; i < num_screens; i++)
2992 for (j = 0; j < workspace_limit; j++)
2993 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2994 if (r->parent == win->id) {
3004 spawn(int ws_idx, union arg *args, int close_fd)
3009 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
3011 close(xcb_get_file_descriptor(conn));
3013 setenv("LD_PRELOAD", SWM_LIB, 1);
3015 if (asprintf(&ret, "%d", ws_idx) == -1) {
3016 warn("spawn: asprintf SWM_WS");
3019 setenv("_SWM_WS", ret, 1);
3023 if (asprintf(&ret, "%d", getpid()) == -1) {
3024 warn("spawn: asprintf _SWM_PID");
3027 setenv("_SWM_PID", ret, 1);
3031 if (setsid() == -1) {
3032 warn("spawn: setsid");
3038 * close stdin and stdout to prevent interaction between apps
3039 * and the baraction script
3040 * leave stderr open to record errors
3042 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) == -1) {
3043 warn("spawn: open");
3046 dup2(fd, STDIN_FILENO);
3047 dup2(fd, STDOUT_FILENO);
3052 execvp(args->argv[0], args->argv);
3054 warn("spawn: execvp");
3059 kill_refs(struct ws_win *win)
3061 int i, x, num_screens;
3062 struct swm_region *r;
3063 struct workspace *ws;
3068 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
3069 for (i = 0; i < num_screens; i++)
3070 TAILQ_FOREACH(r, &screens[i].rl, entry)
3071 for (x = 0; x < workspace_limit; x++) {
3073 if (win == ws->focus)
3075 if (win == ws->focus_prev)
3076 ws->focus_prev = NULL;
3081 validate_win(struct ws_win *testwin)
3084 struct workspace *ws;
3085 struct swm_region *r;
3086 int i, x, num_screens;
3088 if (testwin == NULL)
3091 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
3092 for (i = 0; i < num_screens; i++)
3093 TAILQ_FOREACH(r, &screens[i].rl, entry)
3094 for (x = 0; x < workspace_limit; x++) {
3096 TAILQ_FOREACH(win, &ws->winlist, entry)
3104 validate_ws(struct workspace *testws)
3106 struct swm_region *r;
3107 struct workspace *ws;
3108 int i, x, num_screens;
3110 /* validate all ws */
3111 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
3112 for (i = 0; i < num_screens; i++)
3113 TAILQ_FOREACH(r, &screens[i].rl, entry)
3114 for (x = 0; x < workspace_limit; x++) {
3123 unfocus_win(struct ws_win *win)
3125 xcb_window_t none = XCB_WINDOW_NONE;
3127 DNPRINTF(SWM_D_FOCUS, "unfocus_win: window: 0x%x\n", WINID(win));
3131 if (win->ws == NULL) {
3132 DNPRINTF(SWM_D_FOCUS, "unfocus_win: NULL ws.\n");
3136 if (validate_ws(win->ws)) {
3137 DNPRINTF(SWM_D_FOCUS, "unfocus_win: invalid ws.\n");
3141 if (win->ws->r == NULL) {
3142 DNPRINTF(SWM_D_FOCUS, "unfocus_win: NULL region.\n");
3146 if (validate_win(win)) {
3147 DNPRINTF(SWM_D_FOCUS, "unfocus_win: invalid win.\n");
3152 if (win->ws->focus == win) {
3153 win->ws->focus = NULL;
3154 win->ws->focus_prev = win;
3157 if (validate_win(win->ws->focus)) {
3158 kill_refs(win->ws->focus);
3159 win->ws->focus = NULL;
3162 if (validate_win(win->ws->focus_prev)) {
3163 kill_refs(win->ws->focus_prev);
3164 win->ws->focus_prev = NULL;
3167 xcb_change_window_attributes(conn, win->id, XCB_CW_BORDER_PIXEL,
3168 &win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].pixel);
3170 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
3171 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1, &none);
3173 DNPRINTF(SWM_D_FOCUS, "unfocus_win: done.\n");
3177 focus_win(struct ws_win *win)
3179 struct ws_win *cfw = NULL, *parent = NULL, *w;
3180 struct workspace *ws;
3181 xcb_get_input_focus_reply_t *r;
3183 DNPRINTF(SWM_D_FOCUS, "focus_win: window: 0x%x\n", WINID(win));
3188 if (win->ws == NULL)
3196 if (validate_ws(ws))
3199 if (validate_win(win)) {
3204 r = xcb_get_input_focus_reply(conn, xcb_get_input_focus(conn), NULL);
3206 cfw = find_window(r->focus);
3212 if (ws->focus != win) {
3213 if (ws->focus && ws->focus != cfw)
3214 unfocus_win(ws->focus);
3218 /* If this window directs focus to a child window, then clear. */
3219 if (win->focus_child)
3220 win->focus_child = NULL;
3222 /* If transient, adjust parent's focus child for focus_magic. */
3223 if (win->transient) {
3224 parent = find_window(win->transient);
3225 if (parent && parent->focus_child != win)
3226 parent->focus_child = win;
3229 if (cfw != win && ws->r != NULL) {
3230 /* Set input focus if no input hint, or indicated by hint. */
3231 if (!(win->hints.flags & XCB_ICCCM_WM_HINT_INPUT) ||
3232 (win->hints.flags & XCB_ICCCM_WM_HINT_INPUT &&
3234 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT,
3235 win->id, last_event_time);
3237 /* Tell app it can adjust focus to a specific window. */
3238 if (win->take_focus) {
3239 /* java is special; always tell parent */
3240 if (win->transient && win->java)
3241 client_msg(parent, a_takefocus,
3244 client_msg(win, a_takefocus, last_event_time);
3247 xcb_change_window_attributes(conn, win->id, XCB_CW_BORDER_PIXEL,
3248 &ws->r->s->c[SWM_S_COLOR_FOCUS].pixel);
3250 if (ws->cur_layout->flags & SWM_L_MAPONFOCUS ||
3252 /* If a parent exists, map it first. */
3256 /* Map siblings next. */
3257 TAILQ_FOREACH(w, &ws->winlist, entry)
3258 if (w != win && !w->iconic &&
3259 w->transient == parent->id)
3263 /* Map focused window. */
3266 /* Finally, map children of focus window. */
3267 TAILQ_FOREACH(w, &ws->winlist, entry)
3268 if (w->transient == win->id && !w->iconic)
3274 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
3275 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1,
3282 DNPRINTF(SWM_D_FOCUS, "focus_win: done.\n");
3285 /* If a child window should have focus instead, return it. */
3287 get_focus_magic(struct ws_win *win)
3289 struct ws_win *parent = NULL;
3290 struct ws_win *child = NULL;
3292 DNPRINTF(SWM_D_FOCUS, "get_focus_magic: window: 0x%x\n", WINID(win));
3296 if (win->transient) {
3297 parent = find_window(win->transient);
3299 /* If parent prefers focus elsewhere, then try to do so. */
3300 if (parent && (child = parent->focus_child)) {
3301 if (validate_win(child) == 0 && child->mapped)
3304 parent->focus_child = NULL;
3308 /* If this window prefers focus elsewhere, then try to do so. */
3309 if ((child = win->focus_child)) {
3310 if (validate_win(child) == 0 && child->mapped)
3313 win->focus_child = NULL;
3320 event_drain(uint8_t rt)
3322 xcb_generic_event_t *evt;
3324 /* ensure all pending requests have been processed before filtering. */
3326 while ((evt = xcb_poll_for_event(conn))) {
3327 if (XCB_EVENT_RESPONSE_TYPE(evt) != rt)
3335 set_region(struct swm_region *r)
3337 struct swm_region *rf;
3343 /* Unfocus old region bar. */
3348 xcb_change_window_attributes(conn, rf->bar->id,
3349 XCB_CW_BORDER_PIXEL,
3350 &r->s->c[SWM_S_COLOR_BAR_BORDER_UNFOCUS].pixel);
3353 /* Set region bar border to focus_color. */
3354 xcb_change_window_attributes(conn, r->bar->id,
3355 XCB_CW_BORDER_PIXEL, &r->s->c[SWM_S_COLOR_BAR_BORDER].pixel);
3361 focus_region(struct swm_region *r)
3364 struct swm_region *old_r;
3369 old_r = r->s->r_focus;
3372 nfw = get_region_focus(r);
3376 /* New region is empty; need to manually unfocus win. */
3378 unfocus_win(old_r->ws->focus);
3380 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT, r->s->root,
3383 /* Clear bar since empty. */
3389 switchws(struct swm_region *r, union arg *args)
3391 int wsid = args->id, unmap_old = 0;
3392 struct swm_region *this_r, *other_r;
3394 struct workspace *new_ws, *old_ws;
3399 if (wsid >= workspace_limit)
3403 old_ws = this_r->ws;
3404 new_ws = &this_r->s->ws[wsid];
3406 DNPRINTF(SWM_D_WS, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
3407 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), old_ws->idx, wsid);
3409 if (new_ws == NULL || old_ws == NULL)
3411 if (new_ws == old_ws)
3414 unfocus_win(old_ws->focus);
3416 other_r = new_ws->r;
3417 if (other_r == NULL) {
3418 /* the other workspace is hidden, hide this one */
3422 /* the other ws is visible in another region, exchange them */
3423 other_r->ws_prior = new_ws;
3424 other_r->ws = old_ws;
3425 old_ws->r = other_r;
3427 this_r->ws_prior = old_ws;
3428 this_r->ws = new_ws;
3431 /* Set focus_pending before stacking. */
3432 if (focus_mode != SWM_FOCUS_FOLLOW)
3433 new_ws->focus_pending = get_region_focus(new_ws->r);
3437 /* unmap old windows */
3439 TAILQ_FOREACH(win, &old_ws->winlist, entry)
3442 /* if workspaces were swapped, then don't wait to set focus */
3443 if (old_ws->r && focus_mode != SWM_FOCUS_FOLLOW) {
3444 if (new_ws->focus_pending) {
3445 focus_win(new_ws->focus_pending);
3447 /* Empty region, focus on root. */
3448 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT,
3449 new_ws->r->s[new_ws->r->s->idx].root,
3454 /* Clear bar if new ws is empty. */
3455 if (new_ws->focus_pending == NULL)
3460 DNPRINTF(SWM_D_WS, "switchws: done.\n");
3464 cyclews(struct swm_region *r, union arg *args)
3467 struct swm_screen *s = r->s;
3470 DNPRINTF(SWM_D_WS, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3471 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
3476 case SWM_ARG_ID_CYCLEWS_UP_ALL:
3479 case SWM_ARG_ID_CYCLEWS_UP:
3480 if (a.id < workspace_limit - 1)
3485 case SWM_ARG_ID_CYCLEWS_DOWN_ALL:
3488 case SWM_ARG_ID_CYCLEWS_DOWN:
3492 a.id = workspace_limit - 1;
3499 (!cycle_empty && TAILQ_EMPTY(&s->ws[a.id].winlist)))
3501 if (!cycle_visible && s->ws[a.id].r != NULL)
3505 } while (a.id != r->ws->idx);
3509 priorws(struct swm_region *r, union arg *args)
3515 DNPRINTF(SWM_D_WS, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3516 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
3518 if (r->ws_prior == NULL)
3521 a.id = r->ws_prior->idx;
3526 focusrg(struct swm_region *r, union arg *args)
3528 int ridx = args->id, i, num_screens;
3529 struct swm_region *rr = NULL;
3531 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
3532 /* do nothing if we don't have more than one screen */
3533 if (!(num_screens > 1 || outputs > 1))
3536 DNPRINTF(SWM_D_FOCUS, "focusrg: id: %d\n", ridx);
3538 rr = TAILQ_FIRST(&r->s->rl);
3539 for (i = 0; i < ridx; ++i)
3540 rr = TAILQ_NEXT(rr, entry);
3550 cyclerg(struct swm_region *r, union arg *args)
3552 struct swm_region *rr = NULL;
3555 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
3556 /* do nothing if we don't have more than one screen */
3557 if (!(num_screens > 1 || outputs > 1))
3562 case SWM_ARG_ID_CYCLERG_UP:
3563 rr = TAILQ_NEXT(r, entry);
3565 rr = TAILQ_FIRST(&screens[i].rl);
3567 case SWM_ARG_ID_CYCLERG_DOWN:
3568 rr = TAILQ_PREV(r, swm_region_list, entry);
3570 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
3583 sort_windows(struct ws_win_list *wl)
3585 struct ws_win *win, *parent, *nxt;
3590 for (win = TAILQ_FIRST(wl); win != TAILQ_END(wl); win = nxt) {
3591 nxt = TAILQ_NEXT(win, entry);
3592 if (win->transient) {
3593 parent = find_window(win->transient);
3594 if (parent == NULL) {
3595 warnx("not possible bug");
3598 TAILQ_REMOVE(wl, win, entry);
3599 TAILQ_INSERT_AFTER(wl, parent, win, entry);
3606 swapwin(struct swm_region *r, union arg *args)
3608 struct ws_win *target, *source;
3609 struct ws_win *cur_focus;
3610 struct ws_win_list *wl;
3612 DNPRINTF(SWM_D_WS, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3613 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
3615 cur_focus = r->ws->focus;
3616 if (cur_focus == NULL)
3620 wl = &source->ws->winlist;
3623 case SWM_ARG_ID_SWAPPREV:
3624 if (source->transient)
3625 source = find_window(source->transient);
3626 target = TAILQ_PREV(source, ws_win_list, entry);
3627 if (target && target->transient)
3628 target = find_window(target->transient);
3629 TAILQ_REMOVE(wl, source, entry);
3631 TAILQ_INSERT_TAIL(wl, source, entry);
3633 TAILQ_INSERT_BEFORE(target, source, entry);
3635 case SWM_ARG_ID_SWAPNEXT:
3636 target = TAILQ_NEXT(source, entry);
3637 /* move the parent and let the sort handle the move */
3638 if (source->transient)
3639 source = find_window(source->transient);
3640 TAILQ_REMOVE(wl, source, entry);
3642 TAILQ_INSERT_HEAD(wl, source, entry);
3644 TAILQ_INSERT_AFTER(wl, target, source, entry);
3646 case SWM_ARG_ID_SWAPMAIN:
3647 target = TAILQ_FIRST(wl);
3648 if (target == source) {
3649 if (source->ws->focus_prev != NULL &&
3650 source->ws->focus_prev != target)
3651 source = source->ws->focus_prev;
3655 if (target == NULL || source == NULL)
3657 source->ws->focus_prev = target;
3658 TAILQ_REMOVE(wl, target, entry);
3659 TAILQ_INSERT_BEFORE(source, target, entry);
3660 TAILQ_REMOVE(wl, source, entry);
3661 TAILQ_INSERT_HEAD(wl, source, entry);
3663 case SWM_ARG_ID_MOVELAST:
3664 TAILQ_REMOVE(wl, source, entry);
3665 TAILQ_INSERT_TAIL(wl, source, entry);
3668 DNPRINTF(SWM_D_MOVE, "swapwin: invalid id: %d\n", args->id);
3680 get_focus_prev(struct ws_win *win)
3682 struct ws_win *winfocus = NULL;
3683 struct ws_win *cur_focus = NULL;
3684 struct ws_win_list *wl = NULL;
3685 struct workspace *ws = NULL;
3687 if (!(win && win->ws))
3692 cur_focus = ws->focus;
3694 DNPRINTF(SWM_D_FOCUS, "get_focus_prev: window: 0x%x, cur_focus: 0x%x\n",
3695 WINID(win), WINID(cur_focus));
3697 /* pickle, just focus on whatever */
3698 if (cur_focus == NULL) {
3699 /* use prev_focus if valid */
3700 if (ws->focus_prev && ws->focus_prev != cur_focus &&
3701 find_window(WINID(ws->focus_prev)))
3702 winfocus = ws->focus_prev;
3706 /* if transient focus on parent */
3707 if (cur_focus->transient) {
3708 winfocus = find_window(cur_focus->transient);
3712 /* if in max_stack try harder */
3713 if ((win->quirks & SWM_Q_FOCUSPREV) ||
3714 (ws->cur_layout->flags & SWM_L_FOCUSPREV)) {
3715 if (cur_focus != ws->focus_prev)
3716 winfocus = ws->focus_prev;
3718 winfocus = TAILQ_PREV(win, ws_win_list, entry);
3723 DNPRINTF(SWM_D_FOCUS, "get_focus_prev: focus_close: %d\n", focus_close);
3725 if (winfocus == NULL || winfocus == win) {
3726 switch (focus_close) {
3727 case SWM_STACK_BOTTOM:
3728 TAILQ_FOREACH(winfocus, wl, entry)
3729 if (!winfocus->iconic && winfocus != cur_focus)
3733 TAILQ_FOREACH_REVERSE(winfocus, wl, ws_win_list, entry)
3734 if (!winfocus->iconic && winfocus != cur_focus)
3737 case SWM_STACK_ABOVE:
3738 winfocus = TAILQ_NEXT(cur_focus, entry);
3739 while (winfocus && winfocus->iconic)
3740 winfocus = TAILQ_NEXT(winfocus, entry);
3742 if (winfocus == NULL) {
3743 if (focus_close_wrap) {
3744 TAILQ_FOREACH(winfocus, wl, entry)
3745 if (!winfocus->iconic &&
3746 winfocus != cur_focus)
3749 TAILQ_FOREACH_REVERSE(winfocus, wl,
3751 if (!winfocus->iconic &&
3752 winfocus != cur_focus)
3757 case SWM_STACK_BELOW:
3758 winfocus = TAILQ_PREV(cur_focus, ws_win_list, entry);
3759 while (winfocus && winfocus->iconic)
3760 winfocus = TAILQ_PREV(winfocus, ws_win_list,
3763 if (winfocus == NULL) {
3764 if (focus_close_wrap) {
3765 TAILQ_FOREACH_REVERSE(winfocus, wl,
3767 if (!winfocus->iconic &&
3768 winfocus != cur_focus)
3771 TAILQ_FOREACH(winfocus, wl, entry)
3772 if (!winfocus->iconic &&
3773 winfocus != cur_focus)
3781 if (winfocus == NULL ||
3782 (winfocus && (winfocus->iconic || winfocus == cur_focus))) {
3783 if (focus_default == SWM_STACK_TOP) {
3784 TAILQ_FOREACH_REVERSE(winfocus, wl, ws_win_list, entry)
3785 if (!winfocus->iconic && winfocus != cur_focus)
3788 TAILQ_FOREACH(winfocus, wl, entry)
3789 if (!winfocus->iconic && winfocus != cur_focus)
3796 return get_focus_magic(winfocus);
3800 get_region_focus(struct swm_region *r)
3802 struct ws_win *winfocus = NULL;
3807 if (r->ws->focus && !r->ws->focus->iconic)
3808 winfocus = r->ws->focus;
3809 else if (r->ws->focus_prev && !r->ws->focus_prev->iconic)
3810 winfocus = r->ws->focus_prev;
3812 TAILQ_FOREACH(winfocus, &r->ws->winlist, entry)
3813 if (!winfocus->iconic)
3816 return get_focus_magic(winfocus);
3820 focus(struct swm_region *r, union arg *args)
3822 struct ws_win *head, *cur_focus = NULL, *winfocus = NULL;
3823 struct ws_win_list *wl = NULL;
3824 struct workspace *ws = NULL;
3830 DNPRINTF(SWM_D_FOCUS, "focus: id: %d\n", args->id);
3832 if ((cur_focus = r->ws->focus) == NULL)
3836 if (TAILQ_EMPTY(wl))
3838 /* make sure there is at least one uniconified window */
3840 TAILQ_FOREACH(winfocus, wl, entry)
3841 if (!winfocus->iconic) {
3849 case SWM_ARG_ID_FOCUSPREV:
3850 head = TAILQ_PREV(cur_focus, ws_win_list, entry);
3852 head = TAILQ_LAST(wl, ws_win_list);
3854 if (WINID(winfocus) == cur_focus->transient) {
3855 head = TAILQ_PREV(winfocus, ws_win_list, entry);
3857 head = TAILQ_LAST(wl, ws_win_list);
3862 if (winfocus && winfocus->iconic) {
3863 while (winfocus != cur_focus) {
3864 if (winfocus == NULL)
3865 winfocus = TAILQ_LAST(wl, ws_win_list);
3866 if (!winfocus->iconic)
3868 winfocus = TAILQ_PREV(winfocus, ws_win_list,
3874 case SWM_ARG_ID_FOCUSNEXT:
3875 head = TAILQ_NEXT(cur_focus, entry);
3877 head = TAILQ_FIRST(wl);
3881 if (winfocus && winfocus->iconic) {
3882 while (winfocus != cur_focus) {
3883 if (winfocus == NULL)
3884 winfocus = TAILQ_FIRST(wl);
3885 if (!winfocus->iconic)
3887 winfocus = TAILQ_NEXT(winfocus, entry);
3892 case SWM_ARG_ID_FOCUSMAIN:
3893 winfocus = TAILQ_FIRST(wl);
3894 if (winfocus == cur_focus)
3895 winfocus = cur_focus->ws->focus_prev;
3902 focus_win(get_focus_magic(winfocus));
3908 cycle_layout(struct swm_region *r, union arg *args)
3910 struct workspace *ws = r->ws;
3912 /* suppress unused warning since var is needed */
3915 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
3918 if (ws->cur_layout->l_stack == NULL)
3919 ws->cur_layout = &layouts[0];
3924 focus_win(get_region_focus(r));
3930 stack_config(struct swm_region *r, union arg *args)
3932 struct workspace *ws = r->ws;
3934 DNPRINTF(SWM_D_STACK, "stack_config: id: %d workspace: %d\n",
3937 if (ws->cur_layout->l_config != NULL)
3938 ws->cur_layout->l_config(ws, args->id);
3940 if (args->id != SWM_ARG_ID_STACKINIT)
3949 struct swm_geometry g;
3950 struct swm_region *r;
3956 DNPRINTF(SWM_D_STACK, "stack: begin\n");
3958 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
3959 for (i = 0; i < num_screens; i++) {
3963 TAILQ_FOREACH(r, &screens[i].rl, entry) {
3964 DNPRINTF(SWM_D_STACK, "stack: workspace: %d "
3965 "(screen: %d, region: %d)\n", r->ws->idx, i, j++);
3967 /* Adjust stack area for region bar and padding. */
3969 g.x += region_padding;
3970 g.y += region_padding;
3971 g.w -= 2 * border_width + 2 * region_padding;
3972 g.h -= 2 * border_width + 2 * region_padding;
3973 if (bar_enabled && r->ws->bar_enabled) {
3978 r->ws->cur_layout->l_stack(r->ws, &g);
3979 r->ws->cur_layout->l_string(r->ws);
3980 /* save r so we can track region changes */
3987 DNPRINTF(SWM_D_STACK, "stack: end\n");
3991 store_float_geom(struct ws_win *win, struct swm_region *r)
3993 if (win == NULL || r == NULL)
3996 /* retain window geom and region geom */
3997 win->g_float = win->g;
3998 win->g_float.x -= X(r);
3999 win->g_float.y -= Y(r);
4000 win->g_floatvalid = 1;
4001 DNPRINTF(SWM_D_MISC, "store_float_geom: window: 0x%x, g: (%d,%d)"
4002 " %d x %d, g_float: (%d,%d) %d x %d\n", win->id, X(win), Y(win),
4003 WIDTH(win), HEIGHT(win), win->g_float.x, win->g_float.y,
4004 win->g_float.w, win->g_float.h);
4008 load_float_geom(struct ws_win *win, struct swm_region *r)
4010 if (win == NULL || r == NULL)
4013 if (win->g_floatvalid) {
4014 win->g = win->g_float;
4017 DNPRINTF(SWM_D_MISC, "load_float_geom: window: 0x%x, g: (%d,%d)"
4018 "%d x %d\n", win->id, X(win), Y(win), WIDTH(win),
4021 DNPRINTF(SWM_D_MISC, "load_float_geom: window: 0x%x, g_float "
4022 "is not set.\n", win->id);
4027 stack_floater(struct ws_win *win, struct swm_region *r)
4032 DNPRINTF(SWM_D_MISC, "stack_floater: window: 0x%x\n", win->id);
4035 * to allow windows to change their size (e.g. mplayer fs) only retrieve
4036 * geom on ws switches or return from max mode
4038 if (win->floatmaxed || (r != r->ws->old_r &&
4039 !(win->ewmh_flags & EWMH_F_FULLSCREEN))) {
4040 /* update geometry for the new region */
4041 load_float_geom(win, r);
4044 win->floatmaxed = 0;
4047 * if set to fullscreen mode, configure window to maximum size.
4049 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
4050 if (!win->g_floatvalid)
4051 store_float_geom(win, win->ws->r);
4057 * remove border on fullscreen floater when in fullscreen mode or when
4058 * the quirk is present.
4060 if ((win->ewmh_flags & EWMH_F_FULLSCREEN) ||
4061 ((win->quirks & SWM_Q_FULLSCREEN) &&
4062 (WIDTH(win) >= WIDTH(r)) && (HEIGHT(win) >= HEIGHT(r)))) {
4063 if (win->bordered) {
4065 X(win) += border_width;
4066 Y(win) += border_width;
4068 } else if (!win->bordered) {
4070 X(win) -= border_width;
4071 Y(win) -= border_width;
4074 if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
4075 WIDTH(win) = (double)WIDTH(r) * dialog_ratio;
4076 HEIGHT(win) = (double)HEIGHT(r) * dialog_ratio;
4079 if (!win->manual && !(win->ewmh_flags & EWMH_F_FULLSCREEN) &&
4080 !(win->quirks & SWM_Q_ANYWHERE)) {
4082 * floaters and transients are auto-centred unless moved,
4083 * resized or ANYWHERE quirk is set.
4085 X(win) = X(r) + (WIDTH(r) - WIDTH(win)) / 2 - BORDER(win);
4086 Y(win) = Y(r) + (HEIGHT(r) - HEIGHT(win)) / 2 - BORDER(win);
4088 store_float_geom(win, r);
4091 /* keep window within region bounds */
4092 constrain_window(win, r, 0);
4098 * Send keystrokes to terminal to decrease/increase the font size as the
4099 * window size changes.
4102 adjust_font(struct ws_win *win)
4104 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
4105 win->floating || win->transient)
4108 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
4109 WIDTH(win) / win->sh.width_inc < term_width &&
4110 win->font_steps < SWM_MAX_FONT_STEPS) {
4111 win->font_size_boundary[win->font_steps] =
4112 (win->sh.width_inc * term_width) + win->sh.base_width;
4115 win->last_inc = win->sh.width_inc;
4116 fake_keypress(win, XK_KP_Subtract, XCB_MOD_MASK_SHIFT);
4117 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
4118 WIDTH(win) > win->font_size_boundary[win->font_steps - 1]) {
4121 win->last_inc = win->sh.width_inc;
4122 fake_keypress(win, XK_KP_Add, XCB_MOD_MASK_SHIFT);
4126 #define SWAPXY(g) do { \
4128 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
4129 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
4132 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
4134 struct swm_geometry win_g, r_g = *g;
4135 struct ws_win *win, *fs_win = NULL;
4136 int i, j, s, stacks;
4137 int w_inc = 1, h_inc, w_base = 1, h_base;
4138 int hrh, extra = 0, h_slice, last_h = 0;
4139 int split, colno, winno, mwin, msize, mscale;
4140 int remain, missing, v_slice, reconfigure = 0;
4143 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d, rot: %s, "
4144 "flip: %s\n", ws->idx, YESNO(rot), YESNO(flip));
4146 winno = count_win(ws, 0);
4147 if (winno == 0 && count_win(ws, 1) == 0)
4150 TAILQ_FOREACH(win, &ws->winlist, entry)
4151 if (!win->transient && !win->floating && !win->iconic)
4158 w_inc = win->sh.width_inc;
4159 w_base = win->sh.base_width;
4160 mwin = ws->l_state.horizontal_mwin;
4161 mscale = ws->l_state.horizontal_msize;
4162 stacks = ws->l_state.horizontal_stacks;
4165 w_inc = win->sh.height_inc;
4166 w_base = win->sh.base_height;
4167 mwin = ws->l_state.vertical_mwin;
4168 mscale = ws->l_state.vertical_msize;
4169 stacks = ws->l_state.vertical_stacks;
4173 if (stacks > winno - mwin)
4174 stacks = winno - mwin;
4178 h_slice = r_g.h / SWM_H_SLICE;
4179 if (mwin && winno > mwin) {
4180 v_slice = r_g.w / SWM_V_SLICE;
4184 win_g.w = v_slice * mscale;
4186 if (w_inc > 1 && w_inc < v_slice) {
4187 /* adjust for window's requested size increment */
4188 remain = (win_g.w - w_base) % w_inc;
4194 win_g.x += r_g.w - msize;
4197 colno = split = winno / stacks;
4198 win_g.w = ((r_g.w - (stacks * 2 * border_width) +
4199 2 * border_width) / stacks);
4201 hrh = r_g.h / colno;
4202 extra = r_g.h - (colno * hrh);
4203 win_g.h = hrh - 2 * border_width;
4205 /* stack all the tiled windows */
4206 i = j = 0, s = stacks;
4207 TAILQ_FOREACH(win, &ws->winlist, entry) {
4208 if (win->transient || win->floating || win->iconic)
4211 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
4216 if (split && i == split) {
4217 colno = (winno - mwin) / stacks;
4218 if (s <= (winno - mwin) % stacks)
4220 split = split + colno;
4221 hrh = (r_g.h / colno);
4222 extra = r_g.h - (colno * hrh);
4226 win_g.x += win_g.w + 2 * border_width +
4228 win_g.w = (r_g.w - msize -
4229 (stacks * (2 * border_width + tile_gap))) / stacks;
4231 win_g.w += (r_g.w - msize -
4232 (stacks * (2 * border_width + tile_gap))) %
4238 win_g.h = hrh - 2 * border_width - tile_gap;
4241 h_inc = win->sh.width_inc;
4242 h_base = win->sh.base_width;
4244 h_inc = win->sh.height_inc;
4245 h_base = win->sh.base_height;
4248 if (j == colno - 1) {
4249 win_g.h = hrh + extra;
4250 } else if (h_inc > 1 && h_inc < h_slice) {
4251 /* adjust for window's requested size increment */
4252 remain = (win_g.h - h_base) % h_inc;
4253 missing = h_inc - remain;
4255 if (missing <= extra || j == 0) {
4267 win_g.y += last_h + 2 * border_width + tile_gap;
4269 if (disable_border && !(bar_enabled && ws->bar_enabled) &&
4272 win_g.w += 2 * border_width;
4273 win_g.h += 2 * border_width;
4279 if (X(win) != win_g.y || Y(win) != win_g.x ||
4280 WIDTH(win) != win_g.h || HEIGHT(win) != win_g.w) {
4284 WIDTH(win) = win_g.h;
4285 HEIGHT(win) = win_g.w;
4288 if (X(win) != win_g.x || Y(win) != win_g.y ||
4289 WIDTH(win) != win_g.w || HEIGHT(win) != win_g.h) {
4293 WIDTH(win) = win_g.w;
4294 HEIGHT(win) = win_g.h;
4298 if (bordered != win->bordered) {
4300 win->bordered = bordered;
4316 /* now, stack all the floaters and transients */
4317 TAILQ_FOREACH(win, &ws->winlist, entry) {
4318 if (!win->transient && !win->floating)
4322 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
4327 stack_floater(win, ws->r);
4331 /* Make sure fs_win is stacked last so it's on top. */
4333 stack_floater(fs_win, ws->r);
4339 vertical_config(struct workspace *ws, int id)
4341 DNPRINTF(SWM_D_STACK, "vertical_config: id: %d, workspace: %d\n",
4345 case SWM_ARG_ID_STACKRESET:
4346 case SWM_ARG_ID_STACKINIT:
4347 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
4348 ws->l_state.vertical_mwin = 1;
4349 ws->l_state.vertical_stacks = 1;
4351 case SWM_ARG_ID_MASTERSHRINK:
4352 if (ws->l_state.vertical_msize > 1)
4353 ws->l_state.vertical_msize--;
4355 case SWM_ARG_ID_MASTERGROW:
4356 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
4357 ws->l_state.vertical_msize++;
4359 case SWM_ARG_ID_MASTERADD:
4360 ws->l_state.vertical_mwin++;
4362 case SWM_ARG_ID_MASTERDEL:
4363 if (ws->l_state.vertical_mwin > 0)
4364 ws->l_state.vertical_mwin--;
4366 case SWM_ARG_ID_STACKINC:
4367 ws->l_state.vertical_stacks++;
4369 case SWM_ARG_ID_STACKDEC:
4370 if (ws->l_state.vertical_stacks > 1)
4371 ws->l_state.vertical_stacks--;
4373 case SWM_ARG_ID_FLIPLAYOUT:
4374 ws->l_state.vertical_flip = !ws->l_state.vertical_flip;
4382 vertical_stack(struct workspace *ws, struct swm_geometry *g)
4384 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
4386 stack_master(ws, g, 0, ws->l_state.vertical_flip);
4390 horizontal_config(struct workspace *ws, int id)
4392 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
4395 case SWM_ARG_ID_STACKRESET:
4396 case SWM_ARG_ID_STACKINIT:
4397 ws->l_state.horizontal_mwin = 1;
4398 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
4399 ws->l_state.horizontal_stacks = 1;
4401 case SWM_ARG_ID_MASTERSHRINK:
4402 if (ws->l_state.horizontal_msize > 1)
4403 ws->l_state.horizontal_msize--;
4405 case SWM_ARG_ID_MASTERGROW:
4406 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
4407 ws->l_state.horizontal_msize++;
4409 case SWM_ARG_ID_MASTERADD:
4410 ws->l_state.horizontal_mwin++;
4412 case SWM_ARG_ID_MASTERDEL:
4413 if (ws->l_state.horizontal_mwin > 0)
4414 ws->l_state.horizontal_mwin--;
4416 case SWM_ARG_ID_STACKINC:
4417 ws->l_state.horizontal_stacks++;
4419 case SWM_ARG_ID_STACKDEC:
4420 if (ws->l_state.horizontal_stacks > 1)
4421 ws->l_state.horizontal_stacks--;
4423 case SWM_ARG_ID_FLIPLAYOUT:
4424 ws->l_state.horizontal_flip = !ws->l_state.horizontal_flip;
4432 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
4434 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
4436 stack_master(ws, g, 1, ws->l_state.horizontal_flip);
4439 /* fullscreen view */
4441 max_stack(struct workspace *ws, struct swm_geometry *g)
4443 struct swm_geometry gg = *g;
4444 struct ws_win *w, *win = NULL, *parent = NULL;
4445 int winno, num_screens;
4447 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
4452 winno = count_win(ws, 0);
4453 if (winno == 0 && count_win(ws, 1) == 0)
4456 /* Figure out which top level window should be visible. */
4457 if (ws->focus_pending)
4458 win = ws->focus_pending;
4462 win = TAILQ_FIRST(&ws->winlist);
4465 parent = find_window(win->transient);
4467 DNPRINTF(SWM_D_STACK, "max_stack: win: 0x%x\n", win->id);
4469 /* maximize all top level windows */
4470 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
4471 TAILQ_FOREACH(w, &ws->winlist, entry) {
4472 if (w->transient || w->iconic)
4475 if (w->floating && !w->floatmaxed) {
4477 * retain geometry for retrieval on exit from
4480 store_float_geom(w, ws->r);
4484 /* only reconfigure if necessary */
4485 if (X(w) != gg.x || Y(w) != gg.y || WIDTH(w) != gg.w ||
4486 HEIGHT(w) != gg.h) {
4488 if (bar_enabled && ws->bar_enabled){
4492 WIDTH(w) += 2 * border_width;
4493 HEIGHT(w) += 2 * border_width;
4500 /* If a parent exists, map/raise it first. */
4504 /* Map siblings next. */
4505 TAILQ_FOREACH(w, &ws->winlist, entry)
4506 if (w != win && !w->iconic &&
4507 w->transient == parent->id) {
4508 stack_floater(w, ws->r);
4513 /* Map/raise focused window. */
4516 /* Finally, map/raise children of focus window. */
4517 TAILQ_FOREACH(w, &ws->winlist, entry)
4518 if (w->transient == win->id && !w->iconic) {
4519 stack_floater(w, ws->r);
4525 send_to_rg(struct swm_region *r, union arg *args)
4527 int ridx = args->id, i, num_screens;
4528 struct swm_region *rr = NULL;
4531 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
4532 /* do nothing if we don't have more than one screen */
4533 if (!(num_screens > 1 || outputs > 1))
4536 DNPRINTF(SWM_D_FOCUS, "send_to_rg: id: %d\n", ridx);
4538 rr = TAILQ_FIRST(&r->s->rl);
4539 for (i = 0; i < ridx; ++i)
4540 rr = TAILQ_NEXT(rr, entry);
4551 send_to_ws(struct swm_region *r, union arg *args)
4553 int wsid = args->id;
4554 struct ws_win *win = NULL, *parent;
4555 struct workspace *ws, *nws, *pws;
4556 char ws_idx_str[SWM_PROPLEN];
4558 if (wsid >= workspace_limit)
4561 if (r && r->ws && r->ws->focus)
4566 if (win->ws->idx == wsid)
4569 DNPRINTF(SWM_D_MOVE, "send_to_ws: win 0x%x, ws %d -> %d\n", win->id,
4570 win->ws->idx, wsid);
4573 nws = &win->s->ws[wsid];
4575 /* Update the window's workspace property: _SWM_WS */
4576 if (snprintf(ws_idx_str, SWM_PROPLEN, "%d", nws->idx) < SWM_PROPLEN) {
4577 if (focus_mode != SWM_FOCUS_FOLLOW)
4578 ws->focus_pending = get_focus_prev(win);
4580 /* Move the parent if this is a transient window. */
4581 if (win->transient) {
4582 parent = find_window(win->transient);
4585 /* Set new focus in parent's ws if needed. */
4586 if (pws->focus == parent) {
4587 if (focus_mode != SWM_FOCUS_FOLLOW)
4588 pws->focus_pending =
4589 get_focus_prev(parent);
4591 unfocus_win(parent);
4593 if (focus_mode != SWM_FOCUS_FOLLOW)
4594 pws->focus = pws->focus_pending;
4596 if (focus_mode != SWM_FOCUS_FOLLOW)
4597 pws->focus_pending = NULL;
4600 /* Don't unmap parent if new ws is visible */
4602 unmap_window(parent);
4605 TAILQ_REMOVE(&ws->winlist, parent, entry);
4606 TAILQ_INSERT_TAIL(&nws->winlist, parent, entry);
4609 DNPRINTF(SWM_D_PROP, "send_to_ws: set "
4610 "property: _SWM_WS: %s\n", ws_idx_str);
4611 xcb_change_property(conn, XCB_PROP_MODE_REPLACE,
4612 parent->id, a_swm_ws, XCB_ATOM_STRING, 8,
4613 strlen(ws_idx_str), ws_idx_str);
4619 /* Don't unmap if new ws is visible */
4624 TAILQ_REMOVE(&ws->winlist, win, entry);
4625 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
4628 /* Set focus on new ws. */
4629 unfocus_win(nws->focus);
4632 DNPRINTF(SWM_D_PROP, "send_to_ws: set property: _SWM_WS: %s\n",
4634 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
4635 a_swm_ws, XCB_ATOM_STRING, 8, strlen(ws_idx_str),
4638 /* Restack and set new focus. */
4641 if (focus_mode != SWM_FOCUS_FOLLOW) {
4642 focus_win(ws->focus_pending);
4643 ws->focus_pending = NULL;
4649 DNPRINTF(SWM_D_MOVE, "send_to_ws: done.\n");
4653 pressbutton(struct swm_region *r, union arg *args)
4655 /* suppress unused warning since var is needed */
4658 xcb_test_fake_input(conn, XCB_BUTTON_PRESS, args->id,
4659 XCB_CURRENT_TIME, XCB_WINDOW_NONE, 0, 0, 0);
4660 xcb_test_fake_input(conn, XCB_BUTTON_RELEASE, args->id,
4661 XCB_CURRENT_TIME, XCB_WINDOW_NONE, 0, 0, 0);
4665 raise_toggle(struct swm_region *r, union arg *args)
4667 /* suppress unused warning since var is needed */
4670 if (r == NULL || r->ws == NULL)
4673 r->ws->always_raise = !r->ws->always_raise;
4675 /* bring floaters back to top */
4676 if (!r->ws->always_raise)
4683 iconify(struct swm_region *r, union arg *args)
4685 /* suppress unused warning since var is needed */
4688 if (r->ws->focus == NULL)
4691 set_swm_iconic(r->ws->focus, 1);
4697 get_win_name(xcb_window_t win)
4700 xcb_get_property_cookie_t c;
4701 xcb_get_property_reply_t *r;
4703 /* First try _NET_WM_NAME for UTF-8. */
4704 c = xcb_get_property(conn, 0, win, a_netwmname,
4705 XCB_GET_PROPERTY_TYPE_ANY, 0, UINT_MAX);
4706 r = xcb_get_property_reply(conn, c, NULL);
4709 if (r->type == XCB_NONE) {
4711 /* Use WM_NAME instead; no UTF-8. */
4712 c = xcb_get_property(conn, 0, win, XCB_ATOM_WM_NAME,
4713 XCB_GET_PROPERTY_TYPE_ANY, 0, UINT_MAX);
4714 r = xcb_get_property_reply(conn, c, NULL);
4718 if (r->type == XCB_NONE) {
4724 name = strndup(xcb_get_property_value(r),
4725 xcb_get_property_value_length(r));
4734 uniconify(struct swm_region *r, union arg *args)
4741 DNPRINTF(SWM_D_MISC, "uniconify\n");
4743 if (r == NULL || r->ws == NULL)
4746 /* make sure we have anything to uniconify */
4747 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
4748 if (win->ws == NULL)
4749 continue; /* should never happen */
4758 search_resp_action = SWM_SEARCH_UNICONIFY;
4760 spawn_select(r, args, "search", &searchpid);
4762 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
4765 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
4766 if (win->ws == NULL)
4767 continue; /* should never happen */
4771 name = get_win_name(win->id);
4774 fprintf(lfile, "%s.%u\n", name, win->id);
4782 name_workspace(struct swm_region *r, union arg *args)
4786 DNPRINTF(SWM_D_MISC, "name_workspace\n");
4792 search_resp_action = SWM_SEARCH_NAME_WORKSPACE;
4794 spawn_select(r, args, "name_workspace", &searchpid);
4796 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
4799 fprintf(lfile, "%s", "");
4804 search_workspace(struct swm_region *r, union arg *args)
4807 struct workspace *ws;
4810 DNPRINTF(SWM_D_MISC, "search_workspace\n");
4816 search_resp_action = SWM_SEARCH_SEARCH_WORKSPACE;
4818 spawn_select(r, args, "search", &searchpid);
4820 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
4823 for (i = 0; i < workspace_limit; i++) {
4827 fprintf(lfile, "%d%s%s\n", ws->idx + 1,
4828 (ws->name ? ":" : ""), (ws->name ? ws->name : ""));
4835 search_win_cleanup(void)
4837 struct search_window *sw = NULL;
4839 while ((sw = TAILQ_FIRST(&search_wl)) != NULL) {
4840 xcb_destroy_window(conn, sw->indicator);
4841 TAILQ_REMOVE(&search_wl, sw, entry);
4847 search_win(struct swm_region *r, union arg *args)
4849 struct ws_win *win = NULL;
4850 struct search_window *sw = NULL;
4853 int i, width, height;
4861 XRectangle l_ibox, l_lbox;
4863 DNPRINTF(SWM_D_MISC, "search_win\n");
4866 search_resp_action = SWM_SEARCH_SEARCH_WINDOW;
4868 spawn_select(r, args, "search", &searchpid);
4870 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
4873 TAILQ_INIT(&search_wl);
4876 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
4880 sw = calloc(1, sizeof(struct search_window));
4882 warn("search_win: calloc");
4884 search_win_cleanup();
4890 snprintf(s, sizeof s, "%d", i);
4893 w = xcb_generate_id(conn);
4894 wa[0] = r->s->c[SWM_S_COLOR_FOCUS].pixel;
4895 wa[1] = r->s->c[SWM_S_COLOR_UNFOCUS].pixel;
4897 if (bar_font_legacy) {
4898 XmbTextExtents(bar_fs, s, len, &l_ibox, &l_lbox);
4899 width = l_lbox.width + 4;
4900 height = bar_fs_extents->max_logical_extent.height + 4;
4902 XftTextExtentsUtf8(display, bar_font, (FcChar8 *)s, len,
4904 width = info.width + 4;
4905 height = bar_font->height + 4;
4908 xcb_create_window(conn, XCB_COPY_FROM_PARENT, w, win->id, 0, 0,
4909 width, height, 1, XCB_WINDOW_CLASS_INPUT_OUTPUT,
4910 XCB_COPY_FROM_PARENT, XCB_CW_BACK_PIXEL |
4911 XCB_CW_BORDER_PIXEL, wa);
4913 xcb_map_window(conn, w);
4916 TAILQ_INSERT_TAIL(&search_wl, sw, entry);
4918 if (bar_font_legacy) {
4919 l_gcv.graphics_exposures = 0;
4920 l_draw = XCreateGC(display, w, 0, &l_gcv);
4922 XSetForeground(display, l_draw,
4923 r->s->c[SWM_S_COLOR_BAR].pixel);
4925 DRAWSTRING(display, w, bar_fs, l_draw, 2,
4926 (bar_fs_extents->max_logical_extent.height -
4927 l_lbox.height) / 2 - l_lbox.y, s, len);
4929 XFreeGC(display, l_draw);
4932 draw = XftDrawCreate(display, w,
4933 DefaultVisual(display, r->s->idx),
4934 DefaultColormap(display, r->s->idx));
4936 XftDrawStringUtf8(draw, &bar_font_color, bar_font, 2,
4937 (HEIGHT(r->bar) + bar_font->height) / 2 -
4938 bar_font->descent, (FcChar8 *)s, len);
4940 XftDrawDestroy(draw);
4943 DNPRINTF(SWM_D_MISC, "search_win: mapped window: 0x%x\n", w);
4945 fprintf(lfile, "%d\n", i);
4955 search_resp_uniconify(const char *resp, unsigned long len)
4961 DNPRINTF(SWM_D_MISC, "search_resp_uniconify: resp: %s\n", resp);
4963 TAILQ_FOREACH(win, &search_r->ws->winlist, entry) {
4966 name = get_win_name(win->id);
4969 if (asprintf(&s, "%s.%u", name, win->id) == -1) {
4974 if (strncmp(s, resp, len) == 0) {
4975 /* XXX this should be a callback to generalize */
4976 set_swm_iconic(win, 0);
4985 search_resp_name_workspace(const char *resp, unsigned long len)
4987 struct workspace *ws;
4989 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: resp: %s\n", resp);
4991 if (search_r->ws == NULL)
4996 free(search_r->ws->name);
4997 search_r->ws->name = NULL;
5001 ws->name = strdup(resp);
5002 if (ws->name == NULL) {
5003 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: "
5004 "strdup: %s", strerror(errno));
5011 search_resp_search_workspace(const char *resp)
5018 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: resp: %s\n", resp);
5022 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: strdup: %s",
5029 ws_idx = (int)strtonum(q, 1, workspace_limit, &errstr);
5031 DNPRINTF(SWM_D_MISC, "workspace idx is %s: %s",
5038 switchws(search_r, &a);
5042 search_resp_search_window(const char *resp)
5047 struct search_window *sw;
5049 DNPRINTF(SWM_D_MISC, "search_resp_search_window: resp: %s\n", resp);
5053 DNPRINTF(SWM_D_MISC, "search_resp_search_window: strdup: %s",
5058 idx = (int)strtonum(s, 1, INT_MAX, &errstr);
5060 DNPRINTF(SWM_D_MISC, "window idx is %s: %s",
5067 TAILQ_FOREACH(sw, &search_wl, entry)
5068 if (idx == sw->idx) {
5074 #define MAX_RESP_LEN 1024
5077 search_do_resp(void)
5083 DNPRINTF(SWM_D_MISC, "search_do_resp:\n");
5088 if ((resp = calloc(1, MAX_RESP_LEN + 1)) == NULL) {
5089 warn("search: calloc");
5093 rbytes = read(select_resp_pipe[0], resp, MAX_RESP_LEN);
5095 warn("search: read error");
5098 resp[rbytes] = '\0';
5101 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
5102 * newline, so work around that by sanitizing the resp now.
5104 resp[strcspn(resp, "\n")] = '\0';
5107 switch (search_resp_action) {
5108 case SWM_SEARCH_UNICONIFY:
5109 search_resp_uniconify(resp, len);
5111 case SWM_SEARCH_NAME_WORKSPACE:
5112 search_resp_name_workspace(resp, len);
5114 case SWM_SEARCH_SEARCH_WORKSPACE:
5115 search_resp_search_workspace(resp);
5117 case SWM_SEARCH_SEARCH_WINDOW:
5118 search_resp_search_window(resp);
5123 if (search_resp_action == SWM_SEARCH_SEARCH_WINDOW)
5124 search_win_cleanup();
5126 search_resp_action = SWM_SEARCH_NONE;
5127 close(select_resp_pipe[0]);
5134 wkill(struct swm_region *r, union arg *args)
5136 DNPRINTF(SWM_D_MISC, "wkill: id: %d\n", args->id);
5138 if (r->ws->focus == NULL)
5141 if (args->id == SWM_ARG_ID_KILLWINDOW)
5142 xcb_kill_client(conn, r->ws->focus->id);
5144 if (r->ws->focus->can_delete)
5145 client_msg(r->ws->focus, a_delete, 0);
5151 floating_toggle_win(struct ws_win *win)
5153 struct swm_region *r;
5163 /* reject floating toggles in max stack mode */
5164 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK])
5167 if (win->floating) {
5168 if (!win->floatmaxed) {
5169 /* retain position for refloat */
5170 store_float_geom(win, r);
5174 load_float_geom(win, r);
5178 ewmh_update_actions(win);
5184 floating_toggle(struct swm_region *r, union arg *args)
5186 struct ws_win *win = r->ws->focus;
5188 /* suppress unused warning since var is needed */
5194 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
5197 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
5198 _NET_WM_STATE_TOGGLE);
5202 if (win == win->ws->focus)
5209 constrain_window(struct ws_win *win, struct swm_region *r, int resizable)
5211 if (MAX_X(win) + BORDER(win) > MAX_X(r)) {
5213 WIDTH(win) = MAX_X(r) - X(win) - BORDER(win);
5215 X(win) = MAX_X(r)- WIDTH(win) - BORDER(win);
5218 if (X(win) + BORDER(win) < X(r)) {
5220 WIDTH(win) -= X(r) - X(win) - BORDER(win);
5222 X(win) = X(r) - BORDER(win);
5225 if (MAX_Y(win) + BORDER(win) > MAX_Y(r)) {
5227 HEIGHT(win) = MAX_Y(r) - Y(win) - BORDER(win);
5229 Y(win) = MAX_Y(r) - HEIGHT(win) - BORDER(win);
5232 if (Y(win) + BORDER(win) < Y(r)) {
5234 HEIGHT(win) -= Y(r) - Y(win) - BORDER(win);
5236 Y(win) = Y(r) - BORDER(win);
5242 if (HEIGHT(win) < 1)
5248 update_window(struct ws_win *win)
5253 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
5254 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
5255 XCB_CONFIG_WINDOW_BORDER_WIDTH;
5259 wc[3] = HEIGHT(win);
5260 wc[4] = BORDER(win);
5262 DNPRINTF(SWM_D_EVENT, "update_window: window: 0x%x, (x,y) w x h: "
5263 "(%d,%d) %d x %d, bordered: %s\n", win->id, wc[0], wc[1], wc[2],
5264 wc[3], YESNO(win->bordered));
5266 xcb_configure_window(conn, win->id, mask, wc);
5269 #define SWM_RESIZE_STEPS (50)
5272 resize(struct ws_win *win, union arg *args)
5274 xcb_timestamp_t timestamp = 0;
5275 struct swm_region *r = NULL;
5277 struct swm_geometry g;
5278 int top = 0, left = 0, resizing;
5280 xcb_cursor_t cursor;
5281 xcb_query_pointer_reply_t *xpr;
5282 xcb_generic_event_t *evt;
5283 xcb_motion_notify_event_t *mne;
5289 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
5292 DNPRINTF(SWM_D_EVENT, "resize: window: 0x%x, floating: %s, "
5293 "transient: 0x%x\n", win->id, YESNO(win->floating),
5296 if (!win->transient && !win->floating)
5299 /* reject resizes in max mode for floaters (transient ok) */
5300 if (win->floatmaxed)
5304 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
5312 case SWM_ARG_ID_WIDTHSHRINK:
5313 WIDTH(win) -= SWM_RESIZE_STEPS;
5316 case SWM_ARG_ID_WIDTHGROW:
5317 WIDTH(win) += SWM_RESIZE_STEPS;
5320 case SWM_ARG_ID_HEIGHTSHRINK:
5321 HEIGHT(win) -= SWM_RESIZE_STEPS;
5324 case SWM_ARG_ID_HEIGHTGROW:
5325 HEIGHT(win) += SWM_RESIZE_STEPS;
5332 constrain_window(win, r, 1);
5334 store_float_geom(win,r);
5338 /* get cursor offset from window root */
5339 xpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, win->id),
5346 if (xpr->win_x < WIDTH(win) / 2)
5349 if (xpr->win_y < HEIGHT(win) / 2)
5352 if (args->id == SWM_ARG_ID_CENTER)
5353 cursor = cursors[XC_SIZING].cid;
5355 cursor = cursors[left ? XC_TOP_LEFT_CORNER :
5356 XC_TOP_RIGHT_CORNER].cid;
5358 cursor = cursors[left ? XC_BOTTOM_LEFT_CORNER :
5359 XC_BOTTOM_RIGHT_CORNER].cid;
5361 xcb_grab_pointer(conn, 0, win->id, MOUSEMASK,
5362 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, XCB_WINDOW_NONE, cursor,
5367 while ((evt = xcb_wait_for_event(conn)) && resizing) {
5368 switch (XCB_EVENT_RESPONSE_TYPE(evt)) {
5369 case XCB_BUTTON_RELEASE:
5370 DNPRINTF(SWM_D_EVENT, "resize: BUTTON_RELEASE\n");
5373 case XCB_MOTION_NOTIFY:
5374 mne = (xcb_motion_notify_event_t *)evt;
5375 /* cursor offset/delta from start of the operation */
5376 dx = mne->root_x - xpr->root_x;
5377 dy = mne->root_y - xpr->root_y;
5383 if (args->id == SWM_ARG_ID_CENTER) {
5384 if (g.h / 2 + dy < 1)
5388 HEIGHT(win) = g.h + 2 * dy;
5396 HEIGHT(win) = g.h + dy;
5403 if (args->id == SWM_ARG_ID_CENTER) {
5404 if (g.w / 2 + dx < 1)
5408 WIDTH(win) = g.w + 2 * dx;
5416 WIDTH(win) = g.w + dx;
5419 constrain_window(win, r, 1);
5421 /* not free, don't sync more than 120 times / second */
5422 if ((mne->time - timestamp) > (1000 / 120) ) {
5423 timestamp = mne->time;
5438 store_float_geom(win,r);
5440 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
5442 DNPRINTF(SWM_D_EVENT, "resize: done.\n");
5446 resize_step(struct swm_region *r, union arg *args)
5448 struct ws_win *win = NULL;
5450 if (r && r->ws && r->ws->focus)
5459 #define SWM_MOVE_STEPS (50)
5462 move(struct ws_win *win, union arg *args)
5464 xcb_timestamp_t timestamp = 0;
5465 int move_stp = 0, moving;
5466 struct swm_region *r = NULL;
5467 xcb_query_pointer_reply_t *qpr;
5468 xcb_generic_event_t *evt;
5469 xcb_motion_notify_event_t *mne;
5475 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
5478 DNPRINTF(SWM_D_EVENT, "move: window: 0x%x, floating: %s, transient: "
5479 "0x%x\n", win->id, YESNO(win->floating), win->transient);
5481 /* in max_stack mode should only move transients */
5482 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !win->transient)
5486 if (!win->floating && !win->transient) {
5487 store_float_geom(win, r);
5488 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
5491 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
5500 case SWM_ARG_ID_MOVELEFT:
5501 X(win) -= (SWM_MOVE_STEPS - border_width);
5504 case SWM_ARG_ID_MOVERIGHT:
5505 X(win) += (SWM_MOVE_STEPS - border_width);
5508 case SWM_ARG_ID_MOVEUP:
5509 Y(win) -= (SWM_MOVE_STEPS - border_width);
5512 case SWM_ARG_ID_MOVEDOWN:
5513 Y(win) += (SWM_MOVE_STEPS - border_width);
5520 constrain_window(win, r, 0);
5522 store_float_geom(win, r);
5526 xcb_grab_pointer(conn, 0, win->id, MOUSEMASK,
5527 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC,
5528 XCB_WINDOW_NONE, cursors[XC_FLEUR].cid, XCB_CURRENT_TIME);
5530 /* get cursor offset from window root */
5531 qpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, win->id),
5534 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
5540 while ((evt = xcb_wait_for_event(conn)) && moving) {
5541 switch (XCB_EVENT_RESPONSE_TYPE(evt)) {
5542 case XCB_BUTTON_RELEASE:
5543 DNPRINTF(SWM_D_EVENT, "move: BUTTON_RELEASE\n");
5546 case XCB_MOTION_NOTIFY:
5547 mne = (xcb_motion_notify_event_t *)evt;
5548 X(win) = mne->root_x - qpr->win_x - border_width;
5549 Y(win) = mne->root_y - qpr->win_y - border_width;
5551 constrain_window(win, r, 0);
5553 /* not free, don't sync more than 120 times / second */
5554 if ((mne->time - timestamp) > (1000 / 120) ) {
5555 timestamp = mne->time;
5570 store_float_geom(win, r);
5572 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
5573 DNPRINTF(SWM_D_EVENT, "move: done.\n");
5577 move_step(struct swm_region *r, union arg *args)
5579 struct ws_win *win = NULL;
5581 if (r && r->ws && r->ws->focus)
5586 if (!win->transient && !win->floating)
5593 /* key definitions */
5595 char name[SWM_FUNCNAME_LEN];
5596 void (*func)(struct swm_region *r, union arg *);
5598 } keyfuncs[KF_INVALID + 1] = {
5599 /* name function argument */
5600 { "bar_toggle", bar_toggle, {.id = SWM_ARG_ID_BAR_TOGGLE} },
5601 { "bar_toggle_ws", bar_toggle, {.id = SWM_ARG_ID_BAR_TOGGLE_WS} },
5602 { "button2", pressbutton, {2} },
5603 { "cycle_layout", cycle_layout, {0} },
5604 { "flip_layout", stack_config, {.id = SWM_ARG_ID_FLIPLAYOUT} },
5605 { "float_toggle", floating_toggle,{0} },
5606 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
5607 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
5608 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
5609 { "height_grow", resize_step, {.id = SWM_ARG_ID_HEIGHTGROW} },
5610 { "height_shrink", resize_step, {.id = SWM_ARG_ID_HEIGHTSHRINK} },
5611 { "iconify", iconify, {0} },
5612 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
5613 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
5614 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
5615 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
5616 { "move_down", move_step, {.id = SWM_ARG_ID_MOVEDOWN} },
5617 { "move_left", move_step, {.id = SWM_ARG_ID_MOVELEFT} },
5618 { "move_right", move_step, {.id = SWM_ARG_ID_MOVERIGHT} },
5619 { "move_up", move_step, {.id = SWM_ARG_ID_MOVEUP} },
5620 { "mvrg_1", send_to_rg, {.id = 0} },
5621 { "mvrg_2", send_to_rg, {.id = 1} },
5622 { "mvrg_3", send_to_rg, {.id = 2} },
5623 { "mvrg_4", send_to_rg, {.id = 3} },
5624 { "mvrg_5", send_to_rg, {.id = 4} },
5625 { "mvrg_6", send_to_rg, {.id = 5} },
5626 { "mvrg_7", send_to_rg, {.id = 6} },
5627 { "mvrg_8", send_to_rg, {.id = 7} },
5628 { "mvrg_9", send_to_rg, {.id = 8} },
5629 { "mvws_1", send_to_ws, {.id = 0} },
5630 { "mvws_2", send_to_ws, {.id = 1} },
5631 { "mvws_3", send_to_ws, {.id = 2} },
5632 { "mvws_4", send_to_ws, {.id = 3} },
5633 { "mvws_5", send_to_ws, {.id = 4} },
5634 { "mvws_6", send_to_ws, {.id = 5} },
5635 { "mvws_7", send_to_ws, {.id = 6} },
5636 { "mvws_8", send_to_ws, {.id = 7} },
5637 { "mvws_9", send_to_ws, {.id = 8} },
5638 { "mvws_10", send_to_ws, {.id = 9} },
5639 { "mvws_11", send_to_ws, {.id = 10} },
5640 { "mvws_12", send_to_ws, {.id = 11} },
5641 { "mvws_13", send_to_ws, {.id = 12} },
5642 { "mvws_14", send_to_ws, {.id = 13} },
5643 { "mvws_15", send_to_ws, {.id = 14} },
5644 { "mvws_16", send_to_ws, {.id = 15} },
5645 { "mvws_17", send_to_ws, {.id = 16} },
5646 { "mvws_18", send_to_ws, {.id = 17} },
5647 { "mvws_19", send_to_ws, {.id = 18} },
5648 { "mvws_20", send_to_ws, {.id = 19} },
5649 { "mvws_21", send_to_ws, {.id = 20} },
5650 { "mvws_22", send_to_ws, {.id = 21} },
5651 { "name_workspace", name_workspace, {0} },
5652 { "quit", quit, {0} },
5653 { "raise_toggle", raise_toggle, {0} },
5654 { "restart", restart, {0} },
5655 { "rg_1", focusrg, {.id = 0} },
5656 { "rg_2", focusrg, {.id = 1} },
5657 { "rg_3", focusrg, {.id = 2} },
5658 { "rg_4", focusrg, {.id = 3} },
5659 { "rg_5", focusrg, {.id = 4} },
5660 { "rg_6", focusrg, {.id = 5} },
5661 { "rg_7", focusrg, {.id = 6} },
5662 { "rg_8", focusrg, {.id = 7} },
5663 { "rg_9", focusrg, {.id = 8} },
5664 { "rg_next", cyclerg, {.id = SWM_ARG_ID_CYCLERG_UP} },
5665 { "rg_prev", cyclerg, {.id = SWM_ARG_ID_CYCLERG_DOWN} },
5666 { "screen_next", cyclerg, {.id = SWM_ARG_ID_CYCLERG_UP} },
5667 { "screen_prev", cyclerg, {.id = SWM_ARG_ID_CYCLERG_DOWN} },
5668 { "search_win", search_win, {0} },
5669 { "search_workspace", search_workspace, {0} },
5670 { "spawn_custom", NULL, {0} },
5671 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
5672 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
5673 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
5674 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
5675 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
5676 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
5677 { "uniconify", uniconify, {0} },
5678 { "version", version, {0} },
5679 { "width_grow", resize_step, {.id = SWM_ARG_ID_WIDTHGROW} },
5680 { "width_shrink", resize_step, {.id = SWM_ARG_ID_WIDTHSHRINK} },
5681 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
5682 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
5683 { "ws_1", switchws, {.id = 0} },
5684 { "ws_2", switchws, {.id = 1} },
5685 { "ws_3", switchws, {.id = 2} },
5686 { "ws_4", switchws, {.id = 3} },
5687 { "ws_5", switchws, {.id = 4} },
5688 { "ws_6", switchws, {.id = 5} },
5689 { "ws_7", switchws, {.id = 6} },
5690 { "ws_8", switchws, {.id = 7} },
5691 { "ws_9", switchws, {.id = 8} },
5692 { "ws_10", switchws, {.id = 9} },
5693 { "ws_11", switchws, {.id = 10} },
5694 { "ws_12", switchws, {.id = 11} },
5695 { "ws_13", switchws, {.id = 12} },
5696 { "ws_14", switchws, {.id = 13} },
5697 { "ws_15", switchws, {.id = 14} },
5698 { "ws_16", switchws, {.id = 15} },
5699 { "ws_17", switchws, {.id = 16} },
5700 { "ws_18", switchws, {.id = 17} },
5701 { "ws_19", switchws, {.id = 18} },
5702 { "ws_20", switchws, {.id = 19} },
5703 { "ws_21", switchws, {.id = 20} },
5704 { "ws_22", switchws, {.id = 21} },
5705 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
5706 { "ws_next_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP_ALL} },
5707 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
5708 { "ws_prev_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN_ALL} },
5709 { "ws_prior", priorws, {0} },
5710 { "dumpwins", dumpwins, {0} }, /* MUST BE LAST */
5711 { "invalid key func", NULL, {0} },
5715 key_cmp(struct key *kp1, struct key *kp2)
5717 if (kp1->keysym < kp2->keysym)
5719 if (kp1->keysym > kp2->keysym)
5722 if (kp1->mod < kp2->mod)
5724 if (kp1->mod > kp2->mod)
5731 enum { client_click, root_click };
5733 unsigned int action;
5735 unsigned int button;
5736 void (*func)(struct ws_win *, union arg *);
5739 #define MODKEY_SHIFT MODKEY | XCB_MOD_MASK_SHIFT
5740 /* action key mouse button func args */
5741 { client_click, MODKEY, XCB_BUTTON_INDEX_3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
5742 { client_click, MODKEY_SHIFT, XCB_BUTTON_INDEX_3, resize, {.id = SWM_ARG_ID_CENTER} },
5743 { client_click, MODKEY, XCB_BUTTON_INDEX_1, move, {0} },
5748 update_modkey(unsigned int mod)
5754 RB_FOREACH(kp, key_tree, &keys)
5755 if (kp->mod & XCB_MOD_MASK_SHIFT)
5756 kp->mod = mod | XCB_MOD_MASK_SHIFT;
5760 for (i = 0; i < LENGTH(buttons); i++)
5761 if (buttons[i].mask & XCB_MOD_MASK_SHIFT)
5762 buttons[i].mask = mod | XCB_MOD_MASK_SHIFT;
5764 buttons[i].mask = mod;
5768 spawn_expand(struct swm_region *r, union arg *args, const char *spawn_name,
5771 struct spawn_prog *prog = NULL;
5773 char *ap, **real_args;
5775 /* suppress unused warning since var is needed */
5778 DNPRINTF(SWM_D_SPAWN, "spawn_expand: %s\n", spawn_name);
5781 TAILQ_FOREACH(prog, &spawns, entry) {
5782 if (!strcasecmp(spawn_name, prog->name))
5786 warnx("spawn_custom: program %s not found", spawn_name);
5790 /* make room for expanded args */
5791 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
5792 err(1, "spawn_custom: calloc real_args");
5794 /* expand spawn_args into real_args */
5795 for (i = 0; i < prog->argc; i++) {
5797 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg: %s\n", ap);
5798 if (!strcasecmp(ap, "$bar_border")) {
5800 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
5802 err(1, "spawn_custom border color");
5803 } else if (!strcasecmp(ap, "$bar_color")) {
5805 strdup(r->s->c[SWM_S_COLOR_BAR].name))
5807 err(1, "spawn_custom bar color");
5808 } else if (!strcasecmp(ap, "$bar_font")) {
5809 if ((real_args[i] = strdup(bar_fonts))
5811 err(1, "spawn_custom bar fonts");
5812 } else if (!strcasecmp(ap, "$bar_font_color")) {
5814 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
5816 err(1, "spawn_custom color font");
5817 } else if (!strcasecmp(ap, "$color_focus")) {
5819 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
5821 err(1, "spawn_custom color focus");
5822 } else if (!strcasecmp(ap, "$color_unfocus")) {
5824 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
5826 err(1, "spawn_custom color unfocus");
5827 } else if (!strcasecmp(ap, "$region_index")) {
5828 if (asprintf(&real_args[i], "%d",
5829 get_region_index(r) + 1) < 1)
5830 err(1, "spawn_custom region index");
5831 } else if (!strcasecmp(ap, "$workspace_index")) {
5832 if (asprintf(&real_args[i], "%d", r->ws->idx + 1) < 1)
5833 err(1, "spawn_custom workspace index");
5835 /* no match --> copy as is */
5836 if ((real_args[i] = strdup(ap)) == NULL)
5837 err(1, "spawn_custom strdup(ap)");
5839 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg: %s\n",
5844 DNPRINTF(SWM_D_SPAWN, "spawn_custom: result: ");
5845 for (i = 0; i < prog->argc; i++)
5846 DNPRINTF(SWM_D_SPAWN, "\"%s\" ", real_args[i]);
5847 DNPRINTF(SWM_D_SPAWN, "\n");
5849 *ret_args = real_args;
5850 return (prog->argc);
5854 spawn_custom(struct swm_region *r, union arg *args, const char *spawn_name)
5860 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
5864 spawn(r->ws->idx, &a, 1);
5866 for (i = 0; i < spawn_argc; i++)
5872 spawn_select(struct swm_region *r, union arg *args, const char *spawn_name,
5879 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
5883 if (pipe(select_list_pipe) == -1)
5884 err(1, "pipe error");
5885 if (pipe(select_resp_pipe) == -1)
5886 err(1, "pipe error");
5888 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
5889 err(1, "could not disable SIGPIPE");
5890 switch (*pid = fork()) {
5892 err(1, "cannot fork");
5895 if (dup2(select_list_pipe[0], STDIN_FILENO) == -1)
5897 if (dup2(select_resp_pipe[1], STDOUT_FILENO) == -1)
5899 close(select_list_pipe[1]);
5900 close(select_resp_pipe[0]);
5901 spawn(r->ws->idx, &a, 0);
5903 default: /* parent */
5904 close(select_list_pipe[0]);
5905 close(select_resp_pipe[1]);
5909 for (i = 0; i < spawn_argc; i++)
5915 spawn_insert(const char *name, const char *args)
5917 char *arg, *cp, *ptr;
5918 struct spawn_prog *sp;
5920 DNPRINTF(SWM_D_SPAWN, "spawn_insert: %s\n", name);
5922 if ((sp = calloc(1, sizeof *sp)) == NULL)
5923 err(1, "spawn_insert: calloc");
5924 if ((sp->name = strdup(name)) == NULL)
5925 err(1, "spawn_insert: strdup");
5927 /* convert the arguments to an argument list */
5928 if ((ptr = cp = strdup(args)) == NULL)
5929 err(1, "spawn_insert: strdup");
5930 while ((arg = strsep(&ptr, " \t")) != NULL) {
5931 /* empty field; skip it */
5936 if ((sp->argv = realloc(sp->argv, sp->argc *
5937 sizeof *sp->argv)) == NULL)
5938 err(1, "spawn_insert: realloc");
5939 if ((sp->argv[sp->argc - 1] = strdup(arg)) == NULL)
5940 err(1, "spawn_insert: strdup");
5944 TAILQ_INSERT_TAIL(&spawns, sp, entry);
5945 DNPRINTF(SWM_D_SPAWN, "spawn_insert: leave\n");
5949 spawn_remove(struct spawn_prog *sp)
5953 DNPRINTF(SWM_D_SPAWN, "spawn_remove: %s\n", sp->name);
5955 TAILQ_REMOVE(&spawns, sp, entry);
5956 for (i = 0; i < sp->argc; i++)
5962 DNPRINTF(SWM_D_SPAWN, "spawn_remove: leave\n");
5966 spawn_replace(struct spawn_prog *sp, const char *name, const char *args)
5968 DNPRINTF(SWM_D_SPAWN, "spawn_replace: %s [%s]\n", sp->name, name);
5971 spawn_insert(name, args);
5973 DNPRINTF(SWM_D_SPAWN, "spawn_replace: leave\n");
5977 setspawn(const char *name, const char *args)
5979 struct spawn_prog *sp;
5981 DNPRINTF(SWM_D_SPAWN, "setspawn: %s\n", name);
5986 TAILQ_FOREACH(sp, &spawns, entry) {
5987 if (!strcmp(sp->name, name)) {
5991 spawn_replace(sp, name, args);
5992 DNPRINTF(SWM_D_SPAWN, "setspawn: leave\n");
5996 if (*args == '\0') {
5997 warnx("error: setspawn: cannot find program: %s", name);
6001 spawn_insert(name, args);
6002 DNPRINTF(SWM_D_SPAWN, "setspawn: leave\n");
6006 setconfspawn(char *selector, char *value, int flags)
6010 /* suppress unused warning since var is needed */
6013 args = expand_tilde(value);
6015 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, args);
6017 setspawn(selector, args);
6020 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done.\n");
6027 setconfspawn("term", "xterm", 0);
6028 setconfspawn("spawn_term", "xterm", 0);
6029 setconfspawn("screenshot_all", "screenshot.sh full", 0);
6030 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
6031 setconfspawn("lock", "xlock", 0);
6032 setconfspawn("initscr", "initscreen.sh", 0);
6033 setconfspawn("menu", "dmenu_run"
6036 " -nf $bar_font_color"
6038 " -sf $bar_color", 0);
6039 setconfspawn("search", "dmenu"
6043 " -nf $bar_font_color"
6045 " -sf $bar_color", 0);
6046 setconfspawn("name_workspace", "dmenu"
6050 " -nf $bar_font_color"
6052 " -sf $bar_color", 0);
6056 #define SWM_MODNAME_SIZE 32
6057 #define SWM_KEY_WS "\n+ \t"
6059 parsekeys(char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
6063 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
6064 if (mod == NULL || ks == NULL) {
6065 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
6068 if (keystr == NULL || strlen(keystr) == 0) {
6069 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
6073 *ks = XCB_NO_SYMBOL;
6075 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
6076 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
6078 cp += (long)strspn(cp, SWM_KEY_WS);
6079 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
6081 else if (!strncasecmp(name, "Mod1", SWM_MODNAME_SIZE))
6082 *mod |= XCB_MOD_MASK_1;
6083 else if (!strncasecmp(name, "Mod2", SWM_MODNAME_SIZE))
6084 *mod += XCB_MOD_MASK_2;
6085 else if (!strncmp(name, "Mod3", SWM_MODNAME_SIZE))
6086 *mod |= XCB_MOD_MASK_3;
6087 else if (!strncmp(name, "Mod4", SWM_MODNAME_SIZE))
6088 *mod |= XCB_MOD_MASK_4;
6089 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
6090 *mod |= XCB_MOD_MASK_SHIFT;
6091 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
6092 *mod |= XCB_MOD_MASK_CONTROL;
6094 *ks = XStringToKeysym(name);
6095 XConvertCase(*ks, ks, &uks);
6096 if (ks == XCB_NO_SYMBOL) {
6098 "parsekeys: invalid key %s\n",
6104 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
6109 strdupsafe(const char *str)
6114 return (strdup(str));
6118 key_insert(unsigned int mod, KeySym ks, enum keyfuncid kfid,
6119 const char *spawn_name)
6123 DNPRINTF(SWM_D_KEY, "key_insert: enter %s [%s]\n",
6124 keyfuncs[kfid].name, spawn_name);
6126 if ((kp = malloc(sizeof *kp)) == NULL)
6127 err(1, "key_insert: malloc");
6132 kp->spawn_name = strdupsafe(spawn_name);
6133 RB_INSERT(key_tree, &keys, kp);
6135 DNPRINTF(SWM_D_KEY, "key_insert: leave\n");
6139 key_lookup(unsigned int mod, KeySym ks)
6146 return (RB_FIND(key_tree, &keys, &kp));
6150 key_remove(struct key *kp)
6152 DNPRINTF(SWM_D_KEY, "key_remove: %s\n", keyfuncs[kp->funcid].name);
6154 RB_REMOVE(key_tree, &keys, kp);
6155 free(kp->spawn_name);
6158 DNPRINTF(SWM_D_KEY, "key_remove: leave\n");
6162 key_replace(struct key *kp, unsigned int mod, KeySym ks, enum keyfuncid kfid,
6163 const char *spawn_name)
6165 DNPRINTF(SWM_D_KEY, "key_replace: %s [%s]\n", keyfuncs[kp->funcid].name,
6169 key_insert(mod, ks, kfid, spawn_name);
6171 DNPRINTF(SWM_D_KEY, "key_replace: leave\n");
6175 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid,
6176 const char *spawn_name)
6180 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
6181 keyfuncs[kfid].name, spawn_name);
6183 if ((kp = key_lookup(mod, ks)) != NULL) {
6184 if (kfid == KF_INVALID)
6187 key_replace(kp, mod, ks, kfid, spawn_name);
6188 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
6191 if (kfid == KF_INVALID) {
6192 warnx("bind: Key combination already unbound.");
6193 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
6197 key_insert(mod, ks, kfid, spawn_name);
6198 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
6202 setconfbinding(char *selector, char *value, int flags)
6204 enum keyfuncid kfid;
6207 struct spawn_prog *sp;
6209 /* suppress unused warning since var is needed */
6212 DNPRINTF(SWM_D_KEY, "setconfbinding: enter\n");
6213 if (selector == NULL) {
6214 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
6215 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
6217 setkeybinding(mod, ks, kfid, NULL);
6222 /* search by key function name */
6223 for (kfid = 0; kfid < KF_INVALID; (kfid)++) {
6224 if (strncasecmp(selector, keyfuncs[kfid].name,
6225 SWM_FUNCNAME_LEN) == 0) {
6226 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
6228 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
6229 setkeybinding(mod, ks, kfid, NULL);
6235 /* search by custom spawn name */
6236 TAILQ_FOREACH(sp, &spawns, entry) {
6237 if (strcasecmp(selector, sp->name) == 0) {
6238 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
6240 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
6241 setkeybinding(mod, ks, KF_SPAWN_CUSTOM,
6248 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
6255 #define MODKEY_SHIFT MODKEY | XCB_MOD_MASK_SHIFT
6256 setkeybinding(MODKEY, XK_b, KF_BAR_TOGGLE, NULL);
6257 setkeybinding(MODKEY_SHIFT, XK_b, KF_BAR_TOGGLE_WS,NULL);
6258 setkeybinding(MODKEY, XK_v, KF_BUTTON2, NULL);
6259 setkeybinding(MODKEY, XK_space, KF_CYCLE_LAYOUT,NULL);
6260 setkeybinding(MODKEY_SHIFT, XK_backslash, KF_FLIP_LAYOUT, NULL);
6261 setkeybinding(MODKEY, XK_t, KF_FLOAT_TOGGLE,NULL);
6262 setkeybinding(MODKEY, XK_m, KF_FOCUS_MAIN, NULL);
6263 setkeybinding(MODKEY, XK_j, KF_FOCUS_NEXT, NULL);
6264 setkeybinding(MODKEY, XK_Tab, KF_FOCUS_NEXT, NULL);
6265 setkeybinding(MODKEY, XK_k, KF_FOCUS_PREV, NULL);
6266 setkeybinding(MODKEY_SHIFT, XK_Tab, KF_FOCUS_PREV, NULL);
6267 setkeybinding(MODKEY_SHIFT, XK_equal, KF_HEIGHT_GROW,NULL);
6268 setkeybinding(MODKEY_SHIFT, XK_minus, KF_HEIGHT_SHRINK,NULL);
6269 setkeybinding(MODKEY, XK_w, KF_ICONIFY, NULL);
6270 setkeybinding(MODKEY, XK_h, KF_MASTER_SHRINK, NULL);
6271 setkeybinding(MODKEY, XK_l, KF_MASTER_GROW, NULL);
6272 setkeybinding(MODKEY, XK_comma, KF_MASTER_ADD, NULL);
6273 setkeybinding(MODKEY, XK_period, KF_MASTER_DEL, NULL);
6274 setkeybinding(MODKEY_SHIFT, XK_bracketright,KF_MOVE_DOWN,NULL);
6275 setkeybinding(MODKEY, XK_bracketleft, KF_MOVE_LEFT,NULL);
6276 setkeybinding(MODKEY, XK_bracketright,KF_MOVE_RIGHT,NULL);
6277 setkeybinding(MODKEY_SHIFT, XK_bracketleft, KF_MOVE_UP, NULL);
6278 setkeybinding(MODKEY_SHIFT, XK_KP_End, KF_MVRG_1, NULL);
6279 setkeybinding(MODKEY_SHIFT, XK_KP_Down, KF_MVRG_2, NULL);
6280 setkeybinding(MODKEY_SHIFT, XK_KP_Next, KF_MVRG_3, NULL);
6281 setkeybinding(MODKEY_SHIFT, XK_KP_Left, KF_MVRG_4, NULL);
6282 setkeybinding(MODKEY_SHIFT, XK_KP_Begin, KF_MVRG_5, NULL);
6283 setkeybinding(MODKEY_SHIFT, XK_KP_Right, KF_MVRG_6, NULL);
6284 setkeybinding(MODKEY_SHIFT, XK_KP_Home, KF_MVRG_7, NULL);
6285 setkeybinding(MODKEY_SHIFT, XK_KP_Up, KF_MVRG_8, NULL);
6286 setkeybinding(MODKEY_SHIFT, XK_KP_Prior, KF_MVRG_9, NULL);
6287 setkeybinding(MODKEY_SHIFT, XK_1, KF_MVWS_1, NULL);
6288 setkeybinding(MODKEY_SHIFT, XK_2, KF_MVWS_2, NULL);
6289 setkeybinding(MODKEY_SHIFT, XK_3, KF_MVWS_3, NULL);
6290 setkeybinding(MODKEY_SHIFT, XK_4, KF_MVWS_4, NULL);
6291 setkeybinding(MODKEY_SHIFT, XK_5, KF_MVWS_5, NULL);
6292 setkeybinding(MODKEY_SHIFT, XK_6, KF_MVWS_6, NULL);
6293 setkeybinding(MODKEY_SHIFT, XK_7, KF_MVWS_7, NULL);
6294 setkeybinding(MODKEY_SHIFT, XK_8, KF_MVWS_8, NULL);
6295 setkeybinding(MODKEY_SHIFT, XK_9, KF_MVWS_9, NULL);
6296 setkeybinding(MODKEY_SHIFT, XK_0, KF_MVWS_10, NULL);
6297 setkeybinding(MODKEY_SHIFT, XK_F1, KF_MVWS_11, NULL);
6298 setkeybinding(MODKEY_SHIFT, XK_F2, KF_MVWS_12, NULL);
6299 setkeybinding(MODKEY_SHIFT, XK_F3, KF_MVWS_13, NULL);
6300 setkeybinding(MODKEY_SHIFT, XK_F4, KF_MVWS_14, NULL);
6301 setkeybinding(MODKEY_SHIFT, XK_F5, KF_MVWS_15, NULL);
6302 setkeybinding(MODKEY_SHIFT, XK_F6, KF_MVWS_16, NULL);
6303 setkeybinding(MODKEY_SHIFT, XK_F7, KF_MVWS_17, NULL);
6304 setkeybinding(MODKEY_SHIFT, XK_F8, KF_MVWS_18, NULL);
6305 setkeybinding(MODKEY_SHIFT, XK_F9, KF_MVWS_19, NULL);
6306 setkeybinding(MODKEY_SHIFT, XK_F10, KF_MVWS_20, NULL);
6307 setkeybinding(MODKEY_SHIFT, XK_F11, KF_MVWS_21, NULL);
6308 setkeybinding(MODKEY_SHIFT, XK_F12, KF_MVWS_22, NULL);
6309 setkeybinding(MODKEY_SHIFT, XK_slash, KF_NAME_WORKSPACE,NULL);
6310 setkeybinding(MODKEY_SHIFT, XK_q, KF_QUIT, NULL);
6311 setkeybinding(MODKEY_SHIFT, XK_r, KF_RAISE_TOGGLE,NULL);
6312 setkeybinding(MODKEY, XK_q, KF_RESTART, NULL);
6313 setkeybinding(MODKEY, XK_KP_End, KF_RG_1, NULL);
6314 setkeybinding(MODKEY, XK_KP_Down, KF_RG_2, NULL);
6315 setkeybinding(MODKEY, XK_KP_Next, KF_RG_3, NULL);
6316 setkeybinding(MODKEY, XK_KP_Left, KF_RG_4, NULL);
6317 setkeybinding(MODKEY, XK_KP_Begin, KF_RG_5, NULL);
6318 setkeybinding(MODKEY, XK_KP_Right, KF_RG_6, NULL);
6319 setkeybinding(MODKEY, XK_KP_Home, KF_RG_7, NULL);
6320 setkeybinding(MODKEY, XK_KP_Up, KF_RG_8, NULL);
6321 setkeybinding(MODKEY, XK_KP_Prior, KF_RG_9, NULL);
6322 setkeybinding(MODKEY_SHIFT, XK_Right, KF_RG_NEXT, NULL);
6323 setkeybinding(MODKEY_SHIFT, XK_Left, KF_RG_PREV, NULL);
6324 setkeybinding(MODKEY, XK_f, KF_SEARCH_WIN, NULL);
6325 setkeybinding(MODKEY, XK_slash, KF_SEARCH_WORKSPACE,NULL);
6326 setkeybinding(MODKEY_SHIFT, XK_i, KF_SPAWN_CUSTOM,"initscr");
6327 setkeybinding(MODKEY_SHIFT, XK_Delete, KF_SPAWN_CUSTOM,"lock");
6328 setkeybinding(MODKEY, XK_p, KF_SPAWN_CUSTOM,"menu");
6329 setkeybinding(MODKEY, XK_s, KF_SPAWN_CUSTOM,"screenshot_all");
6330 setkeybinding(MODKEY_SHIFT, XK_s, KF_SPAWN_CUSTOM,"screenshot_wind");
6331 setkeybinding(MODKEY_SHIFT, XK_Return, KF_SPAWN_CUSTOM,"term");
6332 setkeybinding(MODKEY_SHIFT, XK_comma, KF_STACK_INC, NULL);
6333 setkeybinding(MODKEY_SHIFT, XK_period, KF_STACK_DEC, NULL);
6334 setkeybinding(MODKEY_SHIFT, XK_space, KF_STACK_RESET, NULL);
6335 setkeybinding(MODKEY, XK_Return, KF_SWAP_MAIN, NULL);
6336 setkeybinding(MODKEY_SHIFT, XK_j, KF_SWAP_NEXT, NULL);
6337 setkeybinding(MODKEY_SHIFT, XK_k, KF_SWAP_PREV, NULL);
6338 setkeybinding(MODKEY_SHIFT, XK_w, KF_UNICONIFY, NULL);
6339 setkeybinding(MODKEY_SHIFT, XK_v, KF_VERSION, NULL);
6340 setkeybinding(MODKEY, XK_equal, KF_WIDTH_GROW, NULL);
6341 setkeybinding(MODKEY, XK_minus, KF_WIDTH_SHRINK,NULL);
6342 setkeybinding(MODKEY, XK_x, KF_WIND_DEL, NULL);
6343 setkeybinding(MODKEY_SHIFT, XK_x, KF_WIND_KILL, NULL);
6344 setkeybinding(MODKEY, XK_1, KF_WS_1, NULL);
6345 setkeybinding(MODKEY, XK_2, KF_WS_2, NULL);
6346 setkeybinding(MODKEY, XK_3, KF_WS_3, NULL);
6347 setkeybinding(MODKEY, XK_4, KF_WS_4, NULL);
6348 setkeybinding(MODKEY, XK_5, KF_WS_5, NULL);
6349 setkeybinding(MODKEY, XK_6, KF_WS_6, NULL);
6350 setkeybinding(MODKEY, XK_7, KF_WS_7, NULL);
6351 setkeybinding(MODKEY, XK_8, KF_WS_8, NULL);
6352 setkeybinding(MODKEY, XK_9, KF_WS_9, NULL);
6353 setkeybinding(MODKEY, XK_0, KF_WS_10, NULL);
6354 setkeybinding(MODKEY, XK_F1, KF_WS_11, NULL);
6355 setkeybinding(MODKEY, XK_F2, KF_WS_12, NULL);
6356 setkeybinding(MODKEY, XK_F3, KF_WS_13, NULL);
6357 setkeybinding(MODKEY, XK_F4, KF_WS_14, NULL);
6358 setkeybinding(MODKEY, XK_F5, KF_WS_15, NULL);
6359 setkeybinding(MODKEY, XK_F6, KF_WS_16, NULL);
6360 setkeybinding(MODKEY, XK_F7, KF_WS_17, NULL);
6361 setkeybinding(MODKEY, XK_F8, KF_WS_18, NULL);
6362 setkeybinding(MODKEY, XK_F9, KF_WS_19, NULL);
6363 setkeybinding(MODKEY, XK_F10, KF_WS_20, NULL);
6364 setkeybinding(MODKEY, XK_F11, KF_WS_21, NULL);
6365 setkeybinding(MODKEY, XK_F12, KF_WS_22, NULL);
6366 setkeybinding(MODKEY, XK_Right, KF_WS_NEXT, NULL);
6367 setkeybinding(MODKEY, XK_Left, KF_WS_PREV, NULL);
6368 setkeybinding(MODKEY, XK_Up, KF_WS_NEXT_ALL, NULL);
6369 setkeybinding(MODKEY, XK_Down, KF_WS_PREV_ALL, NULL);
6370 setkeybinding(MODKEY, XK_a, KF_WS_PRIOR, NULL);
6372 setkeybinding(MODKEY_SHIFT, XK_d, KF_DUMPWINS, NULL);
6382 while (RB_EMPTY(&keys) == 0) {
6383 kp = RB_ROOT(&keys);
6389 setkeymapping(char *selector, char *value, int flags)
6391 char keymapping_file[PATH_MAX];
6393 /* suppress unused warnings since vars are needed */
6397 DNPRINTF(SWM_D_KEY, "setkeymapping: enter\n");
6398 if (value[0] == '~')
6399 snprintf(keymapping_file, sizeof keymapping_file, "%s/%s",
6400 pwd->pw_dir, &value[1]);
6402 strlcpy(keymapping_file, value, sizeof keymapping_file);
6404 /* load new key bindings; if it fails, revert to default bindings */
6405 if (conf_load(keymapping_file, SWM_CONF_KEYMAPPING)) {
6409 DNPRINTF(SWM_D_KEY, "setkeymapping: leave\n");
6414 updatenumlockmask(void)
6417 xcb_get_modifier_mapping_reply_t *modmap_r;
6418 xcb_keycode_t *modmap, kc, *keycode;
6422 modmap_r = xcb_get_modifier_mapping_reply(conn,
6423 xcb_get_modifier_mapping(conn),
6426 modmap = xcb_get_modifier_mapping_keycodes(modmap_r);
6427 for (i = 0; i < 8; i++) {
6428 for (j = 0; j < modmap_r->keycodes_per_modifier; j++) {
6429 kc = modmap[i * modmap_r->keycodes_per_modifier
6431 keycode = xcb_key_symbols_get_keycode(syms,
6434 numlockmask = (1 << i);
6440 DNPRINTF(SWM_D_MISC, "updatenumlockmask: %d\n", numlockmask);
6447 int num_screens, k, j;
6448 unsigned int modifiers[4];
6449 xcb_keycode_t *code;
6451 DNPRINTF(SWM_D_MISC, "grabkeys\n");
6452 updatenumlockmask();
6455 modifiers[1] = numlockmask;
6456 modifiers[2] = XCB_MOD_MASK_LOCK;
6457 modifiers[3] = numlockmask | XCB_MOD_MASK_LOCK;
6459 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
6460 for (k = 0; k < num_screens; k++) {
6461 if (TAILQ_EMPTY(&screens[k].rl))
6463 xcb_ungrab_key(conn, XCB_GRAB_ANY, screens[k].root,
6465 RB_FOREACH(kp, key_tree, &keys) {
6466 if ((code = xcb_key_symbols_get_keycode(syms,
6468 for (j = 0; j < LENGTH(modifiers); j++)
6469 xcb_grab_key(conn, 1,
6471 kp->mod | modifiers[j],
6472 *code, XCB_GRAB_MODE_SYNC,
6473 XCB_GRAB_MODE_SYNC);
6481 grabbuttons(struct ws_win *win)
6483 unsigned int modifiers[4];
6486 DNPRINTF(SWM_D_MOUSE, "grabbuttons: win 0x%x\n", win->id);
6487 updatenumlockmask();
6490 modifiers[1] = numlockmask;
6491 modifiers[2] = XCB_MOD_MASK_LOCK;
6492 modifiers[3] = numlockmask | XCB_MOD_MASK_LOCK;
6494 for (i = 0; i < LENGTH(buttons); i++)
6495 if (buttons[i].action == client_click)
6496 for (j = 0; j < LENGTH(modifiers); ++j)
6497 xcb_grab_button(conn, 0, win->id, BUTTONMASK,
6498 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC,
6499 XCB_WINDOW_NONE, XCB_CURSOR_NONE,
6500 buttons[i].button, buttons[i].mask |
6504 const char *quirkname[] = {
6505 "NONE", /* config string for "no value" */
6514 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
6515 #define SWM_Q_WS "\n|+ \t"
6517 parsequirks(char *qstr, unsigned long *quirk)
6527 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
6529 cp += (long)strspn(cp, SWM_Q_WS);
6530 for (i = 0; i < LENGTH(quirkname); i++) {
6531 if (!strncasecmp(name, quirkname[i], SWM_QUIRK_LEN)) {
6532 DNPRINTF(SWM_D_QUIRK,
6533 "parsequirks: %s\n", name);
6538 *quirk |= 1 << (i-1);
6542 if (i >= LENGTH(quirkname)) {
6543 DNPRINTF(SWM_D_QUIRK,
6544 "parsequirks: invalid quirk [%s]\n", name);
6552 quirk_insert(const char *class, const char *name, unsigned long quirk)
6556 DNPRINTF(SWM_D_QUIRK, "quirk_insert: %s:%s [%lu]\n", class, name,
6559 if ((qp = malloc(sizeof *qp)) == NULL)
6560 err(1, "quirk_insert: malloc");
6561 if ((qp->class = strdup(class)) == NULL)
6562 err(1, "quirk_insert: strdup");
6563 if ((qp->name = strdup(name)) == NULL)
6564 err(1, "quirk_insert: strdup");
6567 TAILQ_INSERT_TAIL(&quirks, qp, entry);
6569 DNPRINTF(SWM_D_QUIRK, "quirk_insert: leave\n");
6573 quirk_remove(struct quirk *qp)
6575 DNPRINTF(SWM_D_QUIRK, "quirk_remove: %s:%s [%lu]\n", qp->class,
6576 qp->name, qp->quirk);
6578 TAILQ_REMOVE(&quirks, qp, entry);
6583 DNPRINTF(SWM_D_QUIRK, "quirk_remove: leave\n");
6587 quirk_replace(struct quirk *qp, const char *class, const char *name,
6588 unsigned long quirk)
6590 DNPRINTF(SWM_D_QUIRK, "quirk_replace: %s:%s [%lu]\n", qp->class,
6591 qp->name, qp->quirk);
6594 quirk_insert(class, name, quirk);
6596 DNPRINTF(SWM_D_QUIRK, "quirk_replace: leave\n");
6600 setquirk(const char *class, const char *name, unsigned long quirk)
6604 DNPRINTF(SWM_D_QUIRK, "setquirk: enter %s:%s [%lu]\n", class, name,
6607 /* Remove/replace existing quirk. */
6608 TAILQ_FOREACH(qp, &quirks, entry) {
6609 if (!strcmp(qp->class, class) && !strcmp(qp->name, name)) {
6613 quirk_replace(qp, class, name, quirk);
6614 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
6619 /* Only insert if quirk is not NONE. */
6621 quirk_insert(class, name, quirk);
6623 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
6627 setconfquirk(char *selector, char *value, int flags)
6629 char *cp, *class, *name;
6633 /* suppress unused warning since var is needed */
6636 if (selector == NULL)
6638 if ((cp = strchr(selector, ':')) == NULL)
6643 if ((retval = parsequirks(value, &qrks)) == 0)
6644 setquirk(class, name, qrks);
6651 setquirk("MPlayer", "xv", SWM_Q_FLOAT | SWM_Q_FULLSCREEN | SWM_Q_FOCUSPREV);
6652 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT);
6653 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ);
6654 setquirk("Firefox", "Dialog", SWM_Q_FLOAT);
6655 setquirk("Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
6656 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ);
6657 setquirk("xine", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
6658 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
6659 setquirk("xine", "xine Panel", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
6660 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
6661 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
6662 setquirk("pcb", "pcb", SWM_Q_FLOAT);
6663 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
6666 /* conf file stuff */
6667 #define SWM_CONF_FILE "spectrwm.conf"
6668 #define SWM_CONF_FILE_OLD "scrotwm.conf"
6672 SWM_S_BAR_AT_BOTTOM,
6673 SWM_S_BAR_BORDER_WIDTH,
6676 SWM_S_BAR_ENABLED_WS,
6681 SWM_S_CLOCK_ENABLED,
6684 SWM_S_CYCLE_VISIBLE,
6686 SWM_S_DISABLE_BORDER,
6688 SWM_S_FOCUS_CLOSE_WRAP,
6689 SWM_S_FOCUS_DEFAULT,
6691 SWM_S_REGION_PADDING,
6696 SWM_S_STACK_ENABLED,
6699 SWM_S_TITLE_CLASS_ENABLED,
6700 SWM_S_TITLE_NAME_ENABLED,
6701 SWM_S_URGENT_ENABLED,
6702 SWM_S_VERBOSE_LAYOUT,
6703 SWM_S_WINDOW_NAME_ENABLED,
6704 SWM_S_WORKSPACE_LIMIT
6708 setconfvalue(char *selector, char *value, int flags)
6710 struct workspace *ws;
6711 int i, ws_id, num_screens;
6714 /* suppress unused warning since var is needed */
6718 case SWM_S_BAR_ACTION:
6720 if ((bar_argv[0] = expand_tilde(value)) == NULL)
6721 err(1, "setconfvalue: bar_action");
6723 case SWM_S_BAR_AT_BOTTOM:
6724 bar_at_bottom = atoi(value);
6726 case SWM_S_BAR_BORDER_WIDTH:
6727 bar_border_width = atoi(value);
6728 if (bar_border_width < 0)
6729 bar_border_width = 0;
6731 case SWM_S_BAR_DELAY:
6732 /* No longer needed; leave to not break old conf files. */
6734 case SWM_S_BAR_ENABLED:
6735 bar_enabled = atoi(value);
6737 case SWM_S_BAR_ENABLED_WS:
6738 ws_id = atoi(selector) - 1;
6739 if (ws_id < 0 || ws_id >= workspace_limit)
6740 errx(1, "setconfvalue: bar_enabled_ws: invalid "
6741 "workspace %d.", ws_id + 1);
6743 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
6744 for (i = 0; i < num_screens; i++) {
6745 ws = (struct workspace *)&screens[i].ws;
6746 ws[ws_id].bar_enabled = atoi(value);
6749 case SWM_S_BAR_FONT:
6751 if (asprintf(&bar_fonts, "%s,%s", value, bar_fonts) == -1)
6752 err(1, "setconfvalue: asprintf: failed to allocate "
6753 "memory for bar_fonts.");
6756 /* If already in xft mode, then we are done. */
6757 if (!bar_font_legacy)
6760 /* If there are any non-XLFD entries, switch to Xft mode. */
6761 while ((b = strsep(&value, ",")) != NULL) {
6765 bar_font_legacy = 0;
6770 case SWM_S_BAR_FORMAT:
6772 if ((bar_format = strdup(value)) == NULL)
6773 err(1, "setconfvalue: bar_format");
6775 case SWM_S_BAR_JUSTIFY:
6776 if (!strcmp(value, "left"))
6777 bar_justify = SWM_BAR_JUSTIFY_LEFT;
6778 else if (!strcmp(value, "center"))
6779 bar_justify = SWM_BAR_JUSTIFY_CENTER;
6780 else if (!strcmp(value, "right"))
6781 bar_justify = SWM_BAR_JUSTIFY_RIGHT;
6783 errx(1, "invalid bar_justify");
6785 case SWM_S_BORDER_WIDTH:
6786 border_width = atoi(value);
6787 if (border_width < 0)
6790 case SWM_S_CLOCK_ENABLED:
6791 clock_enabled = atoi(value);
6793 case SWM_S_CLOCK_FORMAT:
6794 #ifndef SWM_DENY_CLOCK_FORMAT
6796 if ((clock_format = strdup(value)) == NULL)
6797 err(1, "setconfvalue: clock_format");
6800 case SWM_S_CYCLE_EMPTY:
6801 cycle_empty = atoi(value);
6803 case SWM_S_CYCLE_VISIBLE:
6804 cycle_visible = atoi(value);
6806 case SWM_S_DIALOG_RATIO:
6807 dialog_ratio = atof(value);
6808 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
6811 case SWM_S_DISABLE_BORDER:
6812 disable_border = atoi(value);
6814 case SWM_S_FOCUS_CLOSE:
6815 if (!strcmp(value, "first"))
6816 focus_close = SWM_STACK_BOTTOM;
6817 else if (!strcmp(value, "last"))
6818 focus_close = SWM_STACK_TOP;
6819 else if (!strcmp(value, "next"))
6820 focus_close = SWM_STACK_ABOVE;
6821 else if (!strcmp(value, "previous"))
6822 focus_close = SWM_STACK_BELOW;
6824 errx(1, "focus_close");
6826 case SWM_S_FOCUS_CLOSE_WRAP:
6827 focus_close_wrap = atoi(value);
6829 case SWM_S_FOCUS_DEFAULT:
6830 if (!strcmp(value, "last"))
6831 focus_default = SWM_STACK_TOP;
6832 else if (!strcmp(value, "first"))
6833 focus_default = SWM_STACK_BOTTOM;
6835 errx(1, "focus_default");
6837 case SWM_S_FOCUS_MODE:
6838 if (!strcmp(value, "default"))
6839 focus_mode = SWM_FOCUS_DEFAULT;
6840 else if (!strcmp(value, "follow") ||
6841 !strcmp(value, "follow_cursor"))
6842 focus_mode = SWM_FOCUS_FOLLOW;
6843 else if (!strcmp(value, "manual"))
6844 focus_mode = SWM_FOCUS_MANUAL;
6846 errx(1, "focus_mode");
6848 case SWM_S_REGION_PADDING:
6849 region_padding = atoi(value);
6850 if (region_padding < 0)
6853 case SWM_S_SPAWN_ORDER:
6854 if (!strcmp(value, "first"))
6855 spawn_position = SWM_STACK_BOTTOM;
6856 else if (!strcmp(value, "last"))
6857 spawn_position = SWM_STACK_TOP;
6858 else if (!strcmp(value, "next"))
6859 spawn_position = SWM_STACK_ABOVE;
6860 else if (!strcmp(value, "previous"))
6861 spawn_position = SWM_STACK_BELOW;
6863 errx(1, "spawn_position");
6865 case SWM_S_SPAWN_TERM:
6866 setconfspawn("term", value, 0);
6867 setconfspawn("spawn_term", value, 0);
6870 /* No longer needed; leave to not break old conf files. */
6872 case SWM_S_SS_ENABLED:
6873 /* No longer needed; leave to not break old conf files. */
6875 case SWM_S_STACK_ENABLED:
6876 stack_enabled = atoi(value);
6878 case SWM_S_TERM_WIDTH:
6879 term_width = atoi(value);
6883 case SWM_S_TILE_GAP:
6884 tile_gap = atoi(value);
6888 case SWM_S_TITLE_CLASS_ENABLED:
6889 title_class_enabled = atoi(value);
6891 case SWM_S_TITLE_NAME_ENABLED:
6892 title_name_enabled = atoi(value);
6894 case SWM_S_URGENT_ENABLED:
6895 urgent_enabled = atoi(value);
6897 case SWM_S_VERBOSE_LAYOUT:
6898 verbose_layout = atoi(value);
6899 for (i = 0; layouts[i].l_stack != NULL; i++) {
6901 layouts[i].l_string = fancy_stacker;
6903 layouts[i].l_string = plain_stacker;
6906 case SWM_S_WINDOW_NAME_ENABLED:
6907 window_name_enabled = atoi(value);
6909 case SWM_S_WORKSPACE_LIMIT:
6910 workspace_limit = atoi(value);
6911 if (workspace_limit > SWM_WS_MAX)
6912 workspace_limit = SWM_WS_MAX;
6913 else if (workspace_limit < 1)
6914 workspace_limit = 1;
6923 setconfmodkey(char *selector, char *value, int flags)
6925 /* suppress unused warnings since vars are needed */
6929 if (!strncasecmp(value, "Mod1", strlen("Mod1")))
6930 update_modkey(XCB_MOD_MASK_1);
6931 else if (!strncasecmp(value, "Mod2", strlen("Mod2")))
6932 update_modkey(XCB_MOD_MASK_2);
6933 else if (!strncasecmp(value, "Mod3", strlen("Mod3")))
6934 update_modkey(XCB_MOD_MASK_3);
6935 else if (!strncasecmp(value, "Mod4", strlen("Mod4")))
6936 update_modkey(XCB_MOD_MASK_4);
6943 setconfcolor(char *selector, char *value, int flags)
6945 setscreencolor(value, ((selector == NULL)?-1:atoi(selector)), flags);
6950 setconfregion(char *selector, char *value, int flags)
6952 /* suppress unused warnings since vars are needed */
6956 custom_region(value);
6961 setautorun(char *selector, char *value, int flags)
6971 /* suppress unused warnings since vars are needed */
6975 if (getenv("SWM_STARTED"))
6979 if (sscanf(value, "ws[%d]:%1023c", &ws_id, s) != 2)
6980 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
6982 if (ws_id < 0 || ws_id >= workspace_limit)
6983 errx(1, "autorun: invalid workspace %d", ws_id + 1);
6985 sp = expand_tilde((char *)&s);
6988 * This is a little intricate
6990 * If the pid already exists we simply reuse it because it means it was
6991 * used before AND not claimed by manage_window. We get away with
6992 * altering it in the parent after INSERT because this can not be a race
6995 while ((ap = strsep(&sp, " \t")) != NULL) {
6998 DNPRINTF(SWM_D_SPAWN, "setautorun: arg [%s]\n", ap);
7000 if ((a.argv = realloc(a.argv, argc * sizeof(char *))) == NULL)
7001 err(1, "setautorun: realloc");
7002 a.argv[argc - 1] = ap;
7006 if ((a.argv = realloc(a.argv, (argc + 1) * sizeof(char *))) == NULL)
7007 err(1, "setautorun: realloc");
7008 a.argv[argc] = NULL;
7010 if ((pid = fork()) == 0) {
7011 spawn(ws_id, &a, 1);
7020 p = calloc(1, sizeof *p);
7023 TAILQ_INSERT_TAIL(&pidlist, p, entry);
7033 setlayout(char *selector, char *value, int flags)
7035 int ws_id, i, x, mg, ma, si, ar, f = 0;
7036 int st = SWM_V_STACK, num_screens;
7038 struct workspace *ws;
7040 /* suppress unused warnings since vars are needed */
7044 if (getenv("SWM_STARTED"))
7048 if (sscanf(value, "ws[%d]:%d:%d:%d:%d:%1023c",
7049 &ws_id, &mg, &ma, &si, &ar, s) != 6)
7050 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
7051 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
7054 if (ws_id < 0 || ws_id >= workspace_limit)
7055 errx(1, "layout: invalid workspace %d", ws_id + 1);
7057 if (!strcasecmp(s, "vertical"))
7059 else if (!strcasecmp(s, "vertical_flip")) {
7062 } else if (!strcasecmp(s, "horizontal"))
7064 else if (!strcasecmp(s, "horizontal_flip")) {
7067 } else if (!strcasecmp(s, "fullscreen"))
7070 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
7071 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
7074 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7075 for (i = 0; i < num_screens; i++) {
7076 ws = (struct workspace *)&screens[i].ws;
7077 ws[ws_id].cur_layout = &layouts[st];
7079 ws[ws_id].always_raise = ar;
7080 if (st == SWM_MAX_STACK)
7084 for (x = 0; x < abs(mg); x++) {
7085 ws[ws_id].cur_layout->l_config(&ws[ws_id],
7086 mg >= 0 ? SWM_ARG_ID_MASTERGROW :
7087 SWM_ARG_ID_MASTERSHRINK);
7091 for (x = 0; x < abs(ma); x++) {
7092 ws[ws_id].cur_layout->l_config(&ws[ws_id],
7093 ma >= 0 ? SWM_ARG_ID_MASTERADD :
7094 SWM_ARG_ID_MASTERDEL);
7098 for (x = 0; x < abs(si); x++) {
7099 ws[ws_id].cur_layout->l_config(&ws[ws_id],
7100 si >= 0 ? SWM_ARG_ID_STACKINC :
7101 SWM_ARG_ID_STACKDEC);
7106 ws[ws_id].cur_layout->l_config(&ws[ws_id],
7107 SWM_ARG_ID_FLIPLAYOUT);
7117 /* config options */
7118 struct config_option {
7120 int (*func)(char*, char*, int);
7123 struct config_option configopt[] = {
7124 { "autorun", setautorun, 0 },
7125 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
7126 { "bar_at_bottom", setconfvalue, SWM_S_BAR_AT_BOTTOM },
7127 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
7128 { "bar_border_unfocus", setconfcolor, SWM_S_COLOR_BAR_BORDER_UNFOCUS },
7129 { "bar_border_width", setconfvalue, SWM_S_BAR_BORDER_WIDTH },
7130 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
7131 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
7132 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
7133 { "bar_enabled_ws", setconfvalue, SWM_S_BAR_ENABLED_WS },
7134 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
7135 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
7136 { "bar_format", setconfvalue, SWM_S_BAR_FORMAT },
7137 { "bar_justify", setconfvalue, SWM_S_BAR_JUSTIFY },
7138 { "bind", setconfbinding, 0 },
7139 { "border_width", setconfvalue, SWM_S_BORDER_WIDTH },
7140 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
7141 { "clock_format", setconfvalue, SWM_S_CLOCK_FORMAT },
7142 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
7143 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
7144 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
7145 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
7146 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
7147 { "disable_border", setconfvalue, SWM_S_DISABLE_BORDER },
7148 { "focus_close", setconfvalue, SWM_S_FOCUS_CLOSE },
7149 { "focus_close_wrap", setconfvalue, SWM_S_FOCUS_CLOSE_WRAP },
7150 { "focus_default", setconfvalue, SWM_S_FOCUS_DEFAULT },
7151 { "focus_mode", setconfvalue, SWM_S_FOCUS_MODE },
7152 { "keyboard_mapping", setkeymapping, 0 },
7153 { "layout", setlayout, 0 },
7154 { "modkey", setconfmodkey, 0 },
7155 { "program", setconfspawn, 0 },
7156 { "quirk", setconfquirk, 0 },
7157 { "region", setconfregion, 0 },
7158 { "region_padding", setconfvalue, SWM_S_REGION_PADDING },
7159 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
7160 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
7161 { "spawn_position", setconfvalue, SWM_S_SPAWN_ORDER },
7162 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
7163 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
7164 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
7165 { "tile_gap", setconfvalue, SWM_S_TILE_GAP },
7166 { "title_class_enabled", setconfvalue, SWM_S_TITLE_CLASS_ENABLED },
7167 { "title_name_enabled", setconfvalue, SWM_S_TITLE_NAME_ENABLED },
7168 { "urgent_enabled", setconfvalue, SWM_S_URGENT_ENABLED },
7169 { "verbose_layout", setconfvalue, SWM_S_VERBOSE_LAYOUT },
7170 { "window_name_enabled", setconfvalue, SWM_S_WINDOW_NAME_ENABLED },
7171 { "workspace_limit", setconfvalue, SWM_S_WORKSPACE_LIMIT },
7175 conf_load(const char *filename, int keymapping)
7178 char *line, *cp, *optsub, *optval;
7179 size_t linelen, lineno = 0;
7180 int wordlen, i, optidx;
7181 struct config_option *opt = NULL;
7183 DNPRINTF(SWM_D_CONF, "conf_load: begin\n");
7185 if (filename == NULL) {
7186 warnx("conf_load: no filename");
7189 if ((config = fopen(filename, "r")) == NULL) {
7190 warn("conf_load: fopen: %s", filename);
7194 while (!feof(config)) {
7195 if ((line = fparseln(config, &linelen, &lineno, NULL, 0))
7198 err(1, "%s", filename);
7203 cp += strspn(cp, " \t\n"); /* eat whitespace */
7204 if (cp[0] == '\0') {
7209 /* get config option */
7210 wordlen = strcspn(cp, "=[ \t\n");
7212 warnx("%s: line %zd: no option found",
7217 for (i = 0; i < LENGTH(configopt); i++) {
7218 opt = &configopt[i];
7219 if (!strncasecmp(cp, opt->optname, wordlen) &&
7220 (int)strlen(opt->optname) == wordlen) {
7226 warnx("%s: line %zd: unknown option %.*s",
7227 filename, lineno, wordlen, cp);
7230 if (keymapping && opt && strcmp(opt->optname, "bind")) {
7231 warnx("%s: line %zd: invalid option %.*s",
7232 filename, lineno, wordlen, cp);
7236 cp += strspn(cp, " \t\n"); /* eat whitespace */
7237 /* get [selector] if any */
7241 wordlen = strcspn(cp, "]");
7244 warnx("%s: line %zd: syntax error",
7249 if (asprintf(&optsub, "%.*s", wordlen, cp) ==
7251 warnx("%s: line %zd: unable to allocate"
7252 "memory for selector", filename,
7258 cp += strspn(cp, "] \t\n"); /* eat trailing */
7260 cp += strspn(cp, "= \t\n"); /* eat trailing */
7262 optval = strdup(cp);
7263 /* call function to deal with it all */
7264 if (configopt[optidx].func(optsub, optval,
7265 configopt[optidx].funcflags) != 0)
7266 errx(1, "%s: line %zd: invalid data for %s",
7267 filename, lineno, configopt[optidx].optname);
7274 DNPRINTF(SWM_D_CONF, "conf_load: end\n");
7281 DNPRINTF(SWM_D_CONF, "conf_load: end with error.\n");
7287 set_child_transient(struct ws_win *win, xcb_window_t *trans)
7289 struct ws_win *parent, *w;
7290 struct swm_region *r;
7291 struct workspace *ws;
7292 xcb_icccm_wm_hints_t wmh;
7294 parent = find_window(win->transient);
7296 parent->focus_child = win;
7298 DNPRINTF(SWM_D_MISC, "set_child_transient: parent doesn't exist"
7299 " for 0x%x trans 0x%x\n", win->id, win->transient);
7301 r = root_to_region(win->wa->root, SWM_CK_ALL);
7303 /* parent doen't exist in our window list */
7304 TAILQ_FOREACH(w, &ws->winlist, entry) {
7305 if (xcb_icccm_get_wm_hints_reply(conn,
7306 xcb_icccm_get_wm_hints(conn, w->id),
7308 warnx("can't get hints for 0x%x", w->id);
7312 if (win->hints.window_group != wmh.window_group)
7315 w->focus_child = win;
7316 win->transient = w->id;
7318 DNPRINTF(SWM_D_MISC, "set_child_transient: adjusting "
7319 "transient to 0x%x\n", win->transient);
7326 window_get_pid(xcb_window_t win)
7331 xcb_get_property_cookie_t pc;
7332 xcb_get_property_reply_t *pr;
7334 apid = get_atom_from_string("_NET_WM_PID");
7335 if (apid == XCB_ATOM_NONE)
7338 pc = xcb_get_property(conn, 0, win, apid, XCB_ATOM_CARDINAL, 0, 1);
7339 pr = xcb_get_property_reply(conn, pc, NULL);
7342 if (pr->type != XCB_ATOM_CARDINAL) {
7347 if (pr->type == apid && pr->format == 32)
7348 ret = *((pid_t *)xcb_get_property_value(pr));
7354 apid = get_atom_from_string("_SWM_PID");
7355 pc = xcb_get_property(conn, 0, win, apid, XCB_ATOM_STRING,
7357 pr = xcb_get_property_reply(conn, pc, NULL);
7360 if (pr->type != apid) {
7365 ret = (pid_t)strtonum(xcb_get_property_value(pr), 0, INT_MAX, &errstr);
7372 get_ws_idx(xcb_window_t id)
7378 xcb_get_property_reply_t *gpr;
7380 gpr = xcb_get_property_reply(conn,
7381 xcb_get_property(conn, 0, id, a_swm_ws,
7382 XCB_ATOM_STRING, 0, SWM_PROPLEN),
7387 proplen = xcb_get_property_value_length(gpr);
7389 prop = malloc(proplen + 1);
7392 xcb_get_property_value(gpr),
7394 prop[proplen] = '\0';
7401 DNPRINTF(SWM_D_PROP, "get_ws_idx: _SWM_WS: %s\n", prop);
7402 ws_idx = (int)strtonum(prop, 0, workspace_limit - 1, &errstr);
7404 DNPRINTF(SWM_D_PROP, "get_ws_idx: window: #%s: %s",
7414 manage_window(xcb_window_t id, uint16_t mapped)
7416 xcb_window_t trans = XCB_WINDOW_NONE;
7417 struct ws_win *win, *ww;
7419 char ws_idx_str[SWM_PROPLEN];
7420 struct swm_region *r;
7424 xcb_icccm_get_wm_protocols_reply_t wpr;
7426 if ((win = find_window(id)) != NULL) {
7427 DNPRINTF(SWM_D_MISC, "manage_window: win 0x%x already "
7428 "managed; skipping.)\n", id);
7429 return (win); /* Already managed. */
7432 /* See if window is on the unmanaged list. */
7433 if ((win = find_unmanaged_window(id)) != NULL) {
7434 DNPRINTF(SWM_D_MISC, "manage_window: win 0x%x found on "
7435 "unmanaged list.\n", id);
7436 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
7439 set_child_transient(win, &trans);
7443 DNPRINTF(SWM_D_MISC, "manage_window: win 0x%x is new.\n", id);
7446 /* Create and initialize ws_win object. */
7447 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
7448 err(1, "manage_window: calloc: failed to allocate memory for "
7453 /* Get window geometry. */
7454 win->wa = xcb_get_geometry_reply(conn,
7455 xcb_get_geometry(conn, win->id),
7458 /* Figure out which region the window belongs to. */
7459 r = root_to_region(win->wa->root, SWM_CK_ALL);
7461 /* Ignore window border if there is one. */
7462 WIDTH(win) = win->wa->width;
7463 HEIGHT(win) = win->wa->height;
7464 X(win) = win->wa->x + win->wa->border_width - border_width;
7465 Y(win) = win->wa->y + win->wa->border_width - border_width;
7467 win->mapped = mapped;
7468 win->floatmaxed = 0;
7469 win->ewmh_flags = 0;
7470 win->s = r->s; /* this never changes */
7472 store_float_geom(win, r);
7474 /* Get WM_SIZE_HINTS. */
7475 xcb_icccm_get_wm_normal_hints_reply(conn,
7476 xcb_icccm_get_wm_normal_hints(conn, win->id),
7480 xcb_icccm_get_wm_hints_reply(conn,
7481 xcb_icccm_get_wm_hints(conn, win->id),
7484 /* Get WM_TRANSIENT_FOR; see if window is a transient. */
7485 xcb_icccm_get_wm_transient_for_reply(conn,
7486 xcb_icccm_get_wm_transient_for(conn, win->id),
7489 win->transient = trans;
7490 set_child_transient(win, &win->transient);
7493 /* Get supported protocols. */
7494 if (xcb_icccm_get_wm_protocols_reply(conn,
7495 xcb_icccm_get_wm_protocols(conn, win->id, a_prot),
7497 for (i = 0; i < wpr.atoms_len; i++) {
7498 if (wpr.atoms[i] == a_takefocus)
7499 win->take_focus = 1;
7500 if (wpr.atoms[i] == a_delete)
7501 win->can_delete = 1;
7503 xcb_icccm_get_wm_protocols_reply_wipe(&wpr);
7506 win->iconic = get_swm_iconic(win);
7508 /* Figure out which workspace the window belongs to. */
7509 if ((p = find_pid(window_get_pid(win->id))) != NULL) {
7510 win->ws = &r->s->ws[p->ws];
7511 TAILQ_REMOVE(&pidlist, p, entry);
7514 } else if ((ws_idx = get_ws_idx(win->id)) != -1 &&
7516 /* _SWM_WS is set; use that. */
7517 win->ws = &r->s->ws[ws_idx];
7518 } else if (trans && (ww = find_window(trans)) != NULL) {
7519 /* Launch transients in the same ws as parent. */
7525 /* Set the _SWM_WS atom so we can remember this after reincarnation. */
7526 if (snprintf(ws_idx_str, SWM_PROPLEN, "%d", win->ws->idx) <
7528 DNPRINTF(SWM_D_PROP, "manage_window: set _SWM_WS: %s\n",
7530 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
7531 a_swm_ws, XCB_ATOM_STRING, 8, strlen(ws_idx_str),
7536 ewmh_autoquirk(win);
7538 /* Determine initial quirks. */
7539 if (xcb_icccm_get_wm_class_reply(conn,
7540 xcb_icccm_get_wm_class(conn, win->id),
7542 DNPRINTF(SWM_D_CLASS, "manage_window: class: %s, name: %s\n",
7543 win->ch.class_name, win->ch.instance_name);
7545 /* java is retarded so treat it special */
7546 if (strstr(win->ch.instance_name, "sun-awt")) {
7547 DNPRINTF(SWM_D_CLASS, "manage_window: java window "
7552 TAILQ_FOREACH(qp, &quirks, entry) {
7553 if (!strcmp(win->ch.class_name, qp->class) &&
7554 !strcmp(win->ch.instance_name, qp->name)) {
7555 DNPRINTF(SWM_D_CLASS, "manage_window: on quirks"
7556 "list; mask: 0x%lx\n", qp->quirk);
7557 if (qp->quirk & SWM_Q_FLOAT)
7559 win->quirks = qp->quirk;
7564 /* Alter window position if quirky */
7565 if (win->quirks & SWM_Q_ANYWHERE)
7568 /* Reset font sizes (the bruteforce way; no default keybinding). */
7569 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
7570 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
7571 fake_keypress(win, XK_KP_Subtract, XCB_MOD_MASK_SHIFT);
7572 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
7573 fake_keypress(win, XK_KP_Add, XCB_MOD_MASK_SHIFT);
7576 /* Make sure window is positioned inside its region, if its active. */
7578 constrain_window(win, win->ws->r, 0);
7582 /* Select which X events to monitor and set border pixel color. */
7583 wa[0] = win->s->c[SWM_S_COLOR_UNFOCUS].pixel;
7584 wa[1] = XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_PROPERTY_CHANGE |
7585 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
7587 wa[1] |= XCB_EVENT_MASK_LEAVE_WINDOW | XCB_EVENT_MASK_FOCUS_CHANGE;
7590 xcb_change_window_attributes(conn, win->id, XCB_CW_BORDER_PIXEL |
7591 XCB_CW_EVENT_MASK, wa);
7594 /* Figure out where to stack the window in the workspace. */
7595 if (trans && (ww = find_window(trans)))
7596 TAILQ_INSERT_AFTER(&win->ws->winlist, ww, win, entry);
7597 else if (win->ws->focus && spawn_position == SWM_STACK_ABOVE)
7598 TAILQ_INSERT_AFTER(&win->ws->winlist, win->ws->focus, win,
7600 else if (win->ws->focus && spawn_position == SWM_STACK_BELOW)
7601 TAILQ_INSERT_BEFORE(win->ws->focus, win, entry);
7602 else switch (spawn_position) {
7605 case SWM_STACK_ABOVE:
7606 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
7608 case SWM_STACK_BOTTOM:
7609 case SWM_STACK_BELOW:
7610 TAILQ_INSERT_HEAD(&win->ws->winlist, win, entry);
7613 /* Get initial _NET_WM_STATE */
7614 ewmh_get_win_state(win);
7615 /* Set initial _NET_WM_ALLOWED_ACTIONS */
7616 ewmh_update_actions(win);
7620 DNPRINTF(SWM_D_MISC, "manage_window: done. window: 0x%x, (x,y) w x h: "
7621 "(%d,%d) %d x %d, ws: %d, iconic: %s, transient: 0x%x\n", win->id,
7622 X(win), Y(win), WIDTH(win), HEIGHT(win), win->ws->idx,
7623 YESNO(win->iconic), win->transient);
7629 free_window(struct ws_win *win)
7631 DNPRINTF(SWM_D_MISC, "free_window: window: 0x%x\n", win->id);
7636 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
7641 xcb_icccm_get_wm_class_reply_wipe(&win->ch);
7646 memset(win, 0xff, sizeof *win); /* XXX kill later */
7649 DNPRINTF(SWM_D_MISC, "free_window: done.\n");
7653 unmanage_window(struct ws_win *win)
7655 struct ws_win *parent;
7660 DNPRINTF(SWM_D_MISC, "unmanage_window: window: 0x%x\n", win->id);
7662 if (win->transient) {
7663 parent = find_window(win->transient);
7665 parent->focus_child = NULL;
7668 TAILQ_REMOVE(&win->ws->winlist, win, entry);
7669 TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
7673 expose(xcb_expose_event_t *e)
7676 struct swm_region *r;
7678 DNPRINTF(SWM_D_EVENT, "expose: window: 0x%x\n", e->window);
7680 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7681 for (i = 0; i < num_screens; i++)
7682 TAILQ_FOREACH(r, &screens[i].rl, entry)
7683 if (e->window == WINID(r->bar))
7691 focusin(xcb_focus_in_event_t *e)
7693 DNPRINTF(SWM_D_EVENT, "focusin: window: 0x%x, mode: %s(%u), "
7694 "detail: %s(%u)\n", e->event, get_notify_mode_label(e->mode),
7695 e->mode, get_notify_detail_label(e->detail), e->detail);
7699 focusout(xcb_focus_out_event_t *e)
7701 DNPRINTF(SWM_D_EVENT, "focusout: window: 0x%x, mode: %s(%u), "
7702 "detail: %s(%u)\n", e->event, get_notify_mode_label(e->mode),
7703 e->mode, get_notify_detail_label(e->detail), e->detail);
7708 keypress(xcb_key_press_event_t *e)
7710 xcb_keysym_t keysym;
7713 keysym = xcb_key_press_lookup_keysym(syms, e, 0);
7715 DNPRINTF(SWM_D_EVENT, "keypress: keysym: %u, win (x,y): 0x%x (%d,%d), "
7716 "detail: %u, time: %u, root (x,y): 0x%x (%d,%d), child: 0x%x, "
7717 "state: %u, same_screen: %s\n", keysym, e->event, e->event_x,
7718 e->event_y, e->detail, e->time, e->root, e->root_x, e->root_y,
7719 e->child, e->state, YESNO(e->same_screen));
7721 if ((kp = key_lookup(CLEANMASK(e->state), keysym)) == NULL)
7724 last_event_time = e->time;
7726 if (kp->funcid == KF_SPAWN_CUSTOM)
7727 spawn_custom(root_to_region(e->root, SWM_CK_ALL),
7728 &(keyfuncs[kp->funcid].args), kp->spawn_name);
7729 else if (keyfuncs[kp->funcid].func)
7730 keyfuncs[kp->funcid].func(root_to_region(e->root, SWM_CK_ALL),
7731 &(keyfuncs[kp->funcid].args));
7734 /* Unfreeze grab events. */
7735 xcb_allow_events(conn, XCB_ALLOW_ASYNC_KEYBOARD, e->time);
7738 DNPRINTF(SWM_D_EVENT, "keypress: done.\n");
7742 buttonpress(xcb_button_press_event_t *e)
7744 struct ws_win *win = NULL;
7745 struct swm_region *r, *old_r;
7749 DNPRINTF(SWM_D_EVENT, "buttonpress: win (x,y): 0x%x (%d,%d), "
7750 "detail: %u, time: %u, root (x,y): 0x%x (%d,%d), child: 0x%x, "
7751 "state: %u, same_screen: %s\n", e->event, e->event_x, e->event_y,
7752 e->detail, e->time, e->root, e->root_x, e->root_y, e->child,
7753 e->state, YESNO(e->same_screen));
7755 if (e->event == e->root) {
7756 if (e->child != 0) {
7757 win = find_window(e->child);
7758 /* Pass ButtonPress to window if it isn't managed. */
7762 /* Focus on empty region */
7763 /* If no windows on region if its empty. */
7764 r = root_to_region(e->root, SWM_CK_POINTER);
7766 DNPRINTF(SWM_D_EVENT, "buttonpress: "
7767 "NULL region; ignoring.\n");
7771 if (TAILQ_EMPTY(&r->ws->winlist)) {
7772 old_r = root_to_region(e->root, SWM_CK_FOCUS);
7773 if (old_r && old_r != r)
7774 unfocus_win(old_r->ws->focus);
7776 xcb_set_input_focus(conn,
7777 XCB_INPUT_FOCUS_PARENT, e->root, e->time);
7779 /* Clear bar since empty. */
7787 win = find_window(e->event);
7793 last_event_time = e->time;
7795 focus_win(get_focus_magic(win));
7797 for (i = 0; i < LENGTH(buttons); i++)
7798 if (client_click == buttons[i].action && buttons[i].func &&
7799 buttons[i].button == e->detail &&
7800 CLEANMASK(buttons[i].mask) == CLEANMASK(e->state)) {
7801 buttons[i].func(win, &buttons[i].args);
7807 DNPRINTF(SWM_D_EVENT, "buttonpress: passing to window.\n");
7808 /* Replay event to event window */
7809 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, e->time);
7811 DNPRINTF(SWM_D_EVENT, "buttonpress: handled.\n");
7812 /* Unfreeze grab events. */
7813 xcb_allow_events(conn, XCB_ALLOW_SYNC_POINTER, e->time);
7821 print_win_geom(xcb_window_t w)
7823 xcb_get_geometry_reply_t *wa;
7825 wa = xcb_get_geometry_reply(conn, xcb_get_geometry(conn, w), NULL);
7828 DNPRINTF(SWM_D_MISC, "print_win_geom: window not found: 0x%x\n",
7833 DNPRINTF(SWM_D_MISC, "print_win_geom: window: 0x%x, root: 0x%x, "
7834 "depth: %u, (x,y) w x h: (%d,%d) %d x %d, border: %d\n",
7835 w, wa->root, wa->depth, wa->x, wa->y, wa->width, wa->height,
7844 get_stack_mode_name(uint8_t mode)
7849 case XCB_STACK_MODE_ABOVE:
7852 case XCB_STACK_MODE_BELOW:
7855 case XCB_STACK_MODE_TOP_IF:
7858 case XCB_STACK_MODE_BOTTOM_IF:
7861 case XCB_STACK_MODE_OPPOSITE:
7873 configurerequest(xcb_configure_request_event_t *e)
7876 struct swm_region *r = NULL;
7879 uint32_t wc[7] = {0};
7881 if ((win = find_window(e->window)) == NULL)
7882 if ((win = find_unmanaged_window(e->window)) == NULL)
7886 if (swm_debug & SWM_D_EVENT) {
7887 print_win_geom(e->window);
7889 DNPRINTF(SWM_D_EVENT, "configurerequest: window: 0x%x, "
7890 "parent: 0x%x, new: %s, value_mask: %u { ", e->window,
7891 e->parent, YESNO(new), e->value_mask);
7892 if (e->value_mask & XCB_CONFIG_WINDOW_X)
7893 DPRINTF("X: %d ", e->x);
7894 if (e->value_mask & XCB_CONFIG_WINDOW_Y)
7895 DPRINTF("Y: %d ", e->y);
7896 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH)
7897 DPRINTF("W: %u ", e->width);
7898 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
7899 DPRINTF("H: %u ", e->height);
7900 if (e->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH)
7901 DPRINTF("Border: %u ", e->border_width);
7902 if (e->value_mask & XCB_CONFIG_WINDOW_SIBLING)
7903 DPRINTF("Sibling: 0x%x ", e->sibling);
7904 if (e->value_mask & XCB_CONFIG_WINDOW_STACK_MODE)
7905 DPRINTF("StackMode: %s(%u) ",
7906 get_stack_mode_name(e->stack_mode), e->stack_mode);
7912 if (e->value_mask & XCB_CONFIG_WINDOW_X) {
7913 mask |= XCB_CONFIG_WINDOW_X;
7916 if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
7917 mask |= XCB_CONFIG_WINDOW_Y;
7920 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
7921 mask |= XCB_CONFIG_WINDOW_WIDTH;
7924 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
7925 mask |= XCB_CONFIG_WINDOW_HEIGHT;
7926 wc[i++] = e->height;
7928 if (e->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH) {
7929 mask |= XCB_CONFIG_WINDOW_BORDER_WIDTH;
7930 wc[i++] = e->border_width;
7932 if (e->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
7933 mask |= XCB_CONFIG_WINDOW_SIBLING;
7934 wc[i++] = e->sibling;
7936 if (e->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
7937 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
7938 wc[i++] = e->stack_mode;
7942 xcb_configure_window(conn, e->window, mask, wc);
7945 } else if ((!win->manual || win->quirks & SWM_Q_ANYWHERE) &&
7946 !(win->ewmh_flags & EWMH_F_FULLSCREEN)) {
7949 else if (win->ws->old_r)
7952 /* windows are centered unless ANYWHERE quirk is set. */
7953 if (win->quirks & SWM_Q_ANYWHERE) {
7954 if (e->value_mask & XCB_CONFIG_WINDOW_X) {
7955 win->g_float.x = e->x;
7957 win->g_float.x -= X(r);
7960 if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
7961 win->g_float.y = e->y;
7963 win->g_float.y -= Y(r);
7967 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH)
7968 win->g_float.w = e->width;
7970 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
7971 win->g_float.h = e->height;
7973 win->g_floatvalid = 1;
7975 if (win->floating && r && (win->transient ||
7976 win->ws->cur_layout != &layouts[SWM_MAX_STACK])) {
7977 WIDTH(win) = win->g_float.w;
7978 HEIGHT(win) = win->g_float.h;
7981 stack_floater(win, r);
7993 DNPRINTF(SWM_D_EVENT, "configurerequest: done.\n");
7997 configurenotify(xcb_configure_notify_event_t *e)
8001 DNPRINTF(SWM_D_EVENT, "configurenotify: win 0x%x, event win: 0x%x, "
8002 "(x,y) WxH: (%d,%d) %ux%u, border: %u, above_sibling: 0x%x, "
8003 "override_redirect: %s\n", e->window, e->event, e->x, e->y,
8004 e->width, e->height, e->border_width, e->above_sibling,
8005 YESNO(e->override_redirect));
8007 win = find_window(e->window);
8009 xcb_icccm_get_wm_normal_hints_reply(conn,
8010 xcb_icccm_get_wm_normal_hints(conn, win->id),
8013 if (font_adjusted) {
8021 destroynotify(xcb_destroy_notify_event_t *e)
8025 DNPRINTF(SWM_D_EVENT, "destroynotify: window: 0x%x\n", e->window);
8027 if ((win = find_window(e->window)) == NULL) {
8028 if ((win = find_unmanaged_window(e->window)) == NULL)
8034 if (focus_mode != SWM_FOCUS_FOLLOW) {
8035 /* If we were focused, make sure we focus on something else. */
8036 if (win == win->ws->focus)
8037 win->ws->focus_pending = get_focus_prev(win);
8040 unmanage_window(win);
8043 if (focus_mode != SWM_FOCUS_FOLLOW) {
8044 if (win->ws->focus_pending) {
8045 focus_win(win->ws->focus_pending);
8046 win->ws->focus_pending = NULL;
8057 get_notify_detail_label(uint8_t detail)
8062 case XCB_NOTIFY_DETAIL_ANCESTOR:
8065 case XCB_NOTIFY_DETAIL_VIRTUAL:
8068 case XCB_NOTIFY_DETAIL_INFERIOR:
8071 case XCB_NOTIFY_DETAIL_NONLINEAR:
8072 label = "Nonlinear";
8074 case XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL:
8075 label = "NonlinearVirtual";
8077 case XCB_NOTIFY_DETAIL_POINTER:
8080 case XCB_NOTIFY_DETAIL_POINTER_ROOT:
8081 label = "PointerRoot";
8083 case XCB_NOTIFY_DETAIL_NONE:
8094 get_notify_mode_label(uint8_t mode)
8099 case XCB_NOTIFY_MODE_NORMAL:
8102 case XCB_NOTIFY_MODE_GRAB:
8105 case XCB_NOTIFY_MODE_UNGRAB:
8108 case XCB_NOTIFY_MODE_WHILE_GRABBED:
8109 label = "WhileGrabbed";
8120 enternotify(xcb_enter_notify_event_t *e)
8123 struct swm_region *r;
8125 DNPRINTF(SWM_D_FOCUS, "enternotify: time: %u, win (x,y): 0x%x "
8126 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): 0x%x (%d,%d), "
8127 "child: 0x%x, same_screen_focus: %s, state: %d\n",
8128 e->time, e->event, e->event_x, e->event_y,
8129 get_notify_mode_label(e->mode), e->mode,
8130 get_notify_detail_label(e->detail), e->detail,
8131 e->root, e->root_x, e->root_y, e->child,
8132 YESNO(e->same_screen_focus), e->state);
8134 if (focus_mode == SWM_FOCUS_MANUAL &&
8135 e->mode == XCB_NOTIFY_MODE_NORMAL) {
8136 DNPRINTF(SWM_D_EVENT, "enternotify: manual focus; ignoring.\n");
8140 last_event_time = e->time;
8142 if ((win = find_window(e->event)) == NULL) {
8143 if (e->event == e->root) {
8144 /* If no windows on pointer region, then focus root. */
8145 r = root_to_region(e->root, SWM_CK_POINTER);
8147 DNPRINTF(SWM_D_EVENT, "enterntoify: "
8148 "NULL region; ignoring.\n");
8154 DNPRINTF(SWM_D_EVENT, "enternotify: window is NULL; "
8159 focus_win(get_focus_magic(win));
8167 leavenotify(xcb_leave_notify_event_t *e)
8169 DNPRINTF(SWM_D_FOCUS, "leavenotify: time: %u, win (x,y): 0x%x "
8170 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): 0x%x (%d,%d), "
8171 "child: 0x%x, same_screen_focus: %s, state: %d\n",
8172 e->time, e->event, e->event_x, e->event_y,
8173 get_notify_mode_label(e->mode), e->mode,
8174 get_notify_detail_label(e->detail), e->detail,
8175 e->root, e->root_x, e->root_y, e->child,
8176 YESNO(e->same_screen_focus), e->state);
8181 mapnotify(xcb_map_notify_event_t *e)
8185 DNPRINTF(SWM_D_EVENT, "mapnotify: window: 0x%x\n", e->window);
8187 if ((win = find_window(e->window)) == NULL)
8188 win = manage_window(e->window, 1);
8191 set_win_state(win, XCB_ICCCM_WM_STATE_NORMAL);
8193 if (focus_mode != SWM_FOCUS_FOLLOW) {
8194 if (win->ws->focus_pending == win) {
8196 win->ws->focus_pending = NULL;
8205 mappingnotify(xcb_mapping_notify_event_t *e)
8207 xcb_refresh_keyboard_mapping(syms, e);
8209 if (e->request == XCB_MAPPING_KEYBOARD)
8214 maprequest(xcb_map_request_event_t *e)
8217 xcb_get_window_attributes_reply_t *war;
8219 DNPRINTF(SWM_D_EVENT, "maprequest: win 0x%x\n",
8222 war = xcb_get_window_attributes_reply(conn,
8223 xcb_get_window_attributes(conn, e->window),
8226 DNPRINTF(SWM_D_EVENT, "maprequest: window lost.\n");
8230 if (war->override_redirect) {
8231 DNPRINTF(SWM_D_EVENT, "maprequest: override_redirect; "
8236 win = manage_window(e->window,
8237 (war->map_state == XCB_MAP_STATE_VIEWABLE));
8239 /* The new window should get focus; prepare. */
8240 if (focus_mode != SWM_FOCUS_FOLLOW)
8241 win->ws->focus_pending = get_focus_magic(win);
8243 /* All windows need to be mapped if they are in the current workspace.*/
8247 /* Ignore EnterNotify to handle the mapnotify without interference. */
8248 if (focus_mode == SWM_FOCUS_DEFAULT)
8249 event_drain(XCB_ENTER_NOTIFY);
8252 DNPRINTF(SWM_D_EVENT, "maprequest: done.\n");
8256 motionnotify(xcb_motion_notify_event_t *e)
8258 struct swm_region *r;
8261 DNPRINTF(SWM_D_FOCUS, "motionnotify: time: %u, win (x,y): 0x%x "
8262 "(%d,%d), detail: %s(%d), root (x,y): 0x%x (%d,%d), "
8263 "child: 0x%x, same_screen_focus: %s, state: %d\n",
8264 e->time, e->event, e->event_x, e->event_y,
8265 get_notify_detail_label(e->detail), e->detail,
8266 e->root, e->root_x, e->root_y, e->child,
8267 YESNO(e->same_screen), e->state);
8269 last_event_time = e->time;
8271 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
8272 for (i = 0; i < num_screens; i++)
8273 if (screens[i].root == e->root)
8276 TAILQ_FOREACH(r, &screens[i].rl, entry)
8277 if (X(r) <= e->root_x && e->root_x < MAX_X(r) &&
8278 Y(r) <= e->root_y && e->root_y < MAX_Y(r))
8286 get_atom_name(xcb_atom_t atom)
8291 * This should be disabled during most debugging since
8292 * xcb_get_* causes an xcb_flush.
8295 xcb_get_atom_name_reply_t *r;
8297 r = xcb_get_atom_name_reply(conn,
8298 xcb_get_atom_name(conn, atom),
8301 len = xcb_get_atom_name_name_length(r);
8303 name = malloc(len + 1);
8305 memcpy(name, xcb_get_atom_name_name(r), len);
8319 propertynotify(xcb_property_notify_event_t *e)
8325 name = get_atom_name(e->atom);
8326 DNPRINTF(SWM_D_EVENT, "propertynotify: window: 0x%x, atom: %s(%u), "
8327 "time: %#x, state: %u\n", e->window, name, e->atom, e->time,
8331 win = find_window(e->window);
8335 last_event_time = e->time;
8337 if (e->atom == a_swm_iconic) {
8338 if (e->state == XCB_PROPERTY_NEW_VALUE) {
8339 if (focus_mode != SWM_FOCUS_FOLLOW)
8340 win->ws->focus_pending = get_focus_prev(win);
8347 if (focus_mode != SWM_FOCUS_FOLLOW) {
8348 focus_win(win->ws->focus_pending);
8349 win->ws->focus_pending = NULL;
8353 } else if (e->state == XCB_PROPERTY_DELETE) {
8354 /* The window is no longer iconic, restack ws. */
8355 if (focus_mode != SWM_FOCUS_FOLLOW)
8356 win->ws->focus_pending = get_focus_magic(win);
8360 /* Flush EnterNotify for mapnotify, if needed. */
8363 } else if (e->atom == a_state) {
8364 /* State just changed, make sure it gets focused if mapped. */
8365 if (e->state == XCB_PROPERTY_NEW_VALUE) {
8366 if (focus_mode != SWM_FOCUS_FOLLOW) {
8368 win->ws->focus_pending == win) {
8369 focus_win(win->ws->focus_pending);
8370 win->ws->focus_pending = NULL;
8374 } else if (e->atom == XCB_ATOM_WM_CLASS ||
8375 e->atom == XCB_ATOM_WM_NAME) {
8383 unmapnotify(xcb_unmap_notify_event_t *e)
8386 struct workspace *ws;
8388 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: 0x%x\n", e->window);
8390 /* If we aren't managing the window, then ignore. */
8391 win = find_window(e->window);
8392 if (win == NULL || win->id != e->window)
8397 if (getstate(e->window) != XCB_ICCCM_WM_STATE_ICONIC)
8398 set_win_state(win, XCB_ICCCM_WM_STATE_ICONIC);
8401 /* window unmapped itself */
8402 /* do unmap/unfocus/restack and unmanage */
8405 /* If win was focused, make sure to focus on something else. */
8406 if (win == ws->focus) {
8407 if (focus_mode != SWM_FOCUS_FOLLOW) {
8408 ws->focus_pending = get_focus_prev(win);
8409 DNPRINTF(SWM_D_EVENT, "unmapnotify: "
8410 "focus_pending: 0x%x\n",
8411 WINID(ws->focus_pending));
8417 unmanage_window(win);
8422 if (focus_mode == SWM_FOCUS_FOLLOW) {
8424 focus_win(get_pointer_win(ws->r->s->root));
8426 if (ws->focus_pending) {
8427 focus_win(ws->focus_pending);
8428 ws->focus_pending = NULL;
8433 if (getstate(e->window) == XCB_ICCCM_WM_STATE_NORMAL)
8434 set_win_state(win, XCB_ICCCM_WM_STATE_ICONIC);
8441 visibilitynotify(xcb_visibility_notify_event_t *e)
8443 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: 0x%x\n",
8449 clientmessage(xcb_client_message_event_t *e)
8452 xcb_map_request_event_t mre;
8456 name = get_atom_name(e->type);
8457 DNPRINTF(SWM_D_EVENT, "clientmessage: window: 0x%x, atom: %s(%u)\n",
8458 e->window, name, e->type);
8461 win = find_window(e->window);
8464 if (e->type == ewmh[_NET_ACTIVE_WINDOW].atom) {
8465 /* Manage the window with maprequest. */
8466 DNPRINTF(SWM_D_EVENT, "clientmessage: request focus on "
8467 "unmanaged window.\n");
8468 mre.window = e->window;
8474 if (e->type == ewmh[_NET_ACTIVE_WINDOW].atom) {
8475 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_ACTIVE_WINDOW\n");
8478 if (e->type == ewmh[_NET_CLOSE_WINDOW].atom) {
8479 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_CLOSE_WINDOW\n");
8480 if (win->can_delete)
8481 client_msg(win, a_delete, 0);
8483 xcb_kill_client(conn, win->id);
8485 if (e->type == ewmh[_NET_MOVERESIZE_WINDOW].atom) {
8486 DNPRINTF(SWM_D_EVENT,
8487 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
8488 if (win->floating) {
8489 if (e->data.data32[0] & (1<<8)) /* x */
8490 X(win) = e->data.data32[1];
8491 if (e->data.data32[0] & (1<<9)) /* y */
8492 Y(win) = e->data.data32[2];
8493 if (e->data.data32[0] & (1<<10)) /* width */
8494 WIDTH(win) = e->data.data32[3];
8495 if (e->data.data32[0] & (1<<11)) /* height */
8496 HEIGHT(win) = e->data.data32[4];
8501 /* TODO: Change stack sizes */
8502 /* notify no change was made. */
8503 config_win(win, NULL);
8506 if (e->type == ewmh[_NET_WM_STATE].atom) {
8507 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_WM_STATE\n");
8508 ewmh_update_win_state(win, e->data.data32[1], e->data.data32[0]);
8509 if (e->data.data32[2])
8510 ewmh_update_win_state(win, e->data.data32[2],
8522 int errcode = xcb_connection_has_error(conn);
8523 #ifdef XCB_CONN_ERROR
8526 case XCB_CONN_ERROR:
8527 s = "Socket error, pipe error or other stream error.";
8529 case XCB_CONN_CLOSED_EXT_NOTSUPPORTED:
8530 s = "Extension not supported.";
8532 case XCB_CONN_CLOSED_MEM_INSUFFICIENT:
8533 s = "Insufficient memory.";
8535 case XCB_CONN_CLOSED_REQ_LEN_EXCEED:
8536 s = "Request length was exceeded.";
8538 case XCB_CONN_CLOSED_PARSE_ERR:
8539 s = "Error parsing display string.";
8542 s = "Unknown error.";
8545 errx(errcode, "X CONNECTION ERROR: %s", s);
8548 errx(errcode, "X CONNECTION ERROR");
8556 const uint32_t val = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
8557 XCB_EVENT_MASK_ENTER_WINDOW;
8559 xcb_void_cookie_t wac;
8560 xcb_generic_error_t *error;
8562 /* this causes an error if some other window manager is running */
8563 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
8564 for (i = 0; i < num_screens; i++) {
8565 if ((sc = get_screen(i)) == NULL)
8566 errx(1, "ERROR: can't get screen %d.", i);
8567 DNPRINTF(SWM_D_INIT, "enable_wm: screen %d, root: 0x%x\n",
8569 wac = xcb_change_window_attributes_checked(conn, sc->root,
8570 XCB_CW_EVENT_MASK, &val);
8571 if ((error = xcb_request_check(conn, wac))) {
8572 DNPRINTF(SWM_D_INIT, "enable_wm: error_code: %u\n",
8578 /* click to focus on empty region */
8579 xcb_grab_button(conn, 1, sc->root, BUTTONMASK,
8580 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC, XCB_WINDOW_NONE,
8581 XCB_CURSOR_NONE, XCB_BUTTON_INDEX_1, XCB_BUTTON_MASK_ANY);
8588 new_region(struct swm_screen *s, int x, int y, int w, int h)
8590 struct swm_region *r, *n;
8591 struct workspace *ws = NULL;
8595 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
8596 s->idx, w, h, x, y);
8598 /* remove any conflicting regions */
8599 n = TAILQ_FIRST(&s->rl);
8602 n = TAILQ_NEXT(r, entry);
8603 if (X(r) < (x + w) && (X(r) + WIDTH(r)) > x &&
8604 Y(r) < (y + h) && (Y(r) + HEIGHT(r)) > y) {
8605 if (r->ws->r != NULL)
8606 r->ws->old_r = r->ws->r;
8609 xcb_destroy_window(conn, r->id);
8610 TAILQ_REMOVE(&s->rl, r, entry);
8611 TAILQ_INSERT_TAIL(&s->orl, r, entry);
8615 /* search old regions for one to reuse */
8617 /* size + location match */
8618 TAILQ_FOREACH(r, &s->orl, entry)
8619 if (X(r) == x && Y(r) == y &&
8620 HEIGHT(r) == h && WIDTH(r) == w)
8624 TAILQ_FOREACH(r, &s->orl, entry)
8625 if (HEIGHT(r) == h && WIDTH(r) == w)
8629 TAILQ_REMOVE(&s->orl, r, entry);
8630 /* try to use old region's workspace */
8631 if (r->ws->r == NULL)
8634 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
8635 err(1, "new_region: calloc: failed to allocate memory "
8638 /* if we don't have a workspace already, find one */
8640 for (i = 0; i < workspace_limit; i++)
8641 if (s->ws[i].r == NULL) {
8648 errx(1, "new_region: no free workspaces");
8659 TAILQ_INSERT_TAIL(&s->rl, r, entry);
8661 /* Invisible region window to detect pointer events on empty regions. */
8662 r->id = xcb_generate_id(conn);
8663 wa[0] = XCB_EVENT_MASK_POINTER_MOTION |
8664 XCB_EVENT_MASK_POINTER_MOTION_HINT;
8666 xcb_create_window(conn, XCB_COPY_FROM_PARENT, r->id, r->s->root,
8667 X(r), Y(r), WIDTH(r), HEIGHT(r), 0, XCB_WINDOW_CLASS_INPUT_ONLY,
8668 XCB_COPY_FROM_PARENT, XCB_CW_EVENT_MASK, wa);
8670 xcb_map_window(conn, r->id);
8676 #ifdef SWM_XRR_HAS_CRTC
8679 #endif /* SWM_XRR_HAS_CRTC */
8680 struct swm_region *r;
8682 xcb_randr_get_screen_resources_current_cookie_t src;
8683 xcb_randr_get_screen_resources_current_reply_t *srr;
8684 xcb_randr_get_crtc_info_cookie_t cic;
8685 xcb_randr_get_crtc_info_reply_t *cir = NULL;
8686 xcb_randr_crtc_t *crtc;
8687 xcb_screen_t *screen;
8689 DNPRINTF(SWM_D_MISC, "scan_xrandr: screen: %d\n", i);
8691 if ((screen = get_screen(i)) == NULL)
8692 errx(1, "ERROR: can't get screen %d.", i);
8694 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
8695 if (i >= num_screens)
8696 errx(1, "scan_xrandr: invalid screen");
8698 /* remove any old regions */
8699 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
8700 r->ws->old_r = r->ws->r = NULL;
8702 xcb_destroy_window(conn, r->id);
8703 TAILQ_REMOVE(&screens[i].rl, r, entry);
8704 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
8708 /* map virtual screens onto physical screens */
8709 #ifdef SWM_XRR_HAS_CRTC
8710 if (xrandr_support) {
8711 src = xcb_randr_get_screen_resources_current(conn,
8713 srr = xcb_randr_get_screen_resources_current_reply(conn, src,
8716 new_region(&screens[i], 0, 0,
8717 screen->width_in_pixels,
8718 screen->height_in_pixels);
8721 ncrtc = srr->num_crtcs;
8723 crtc = xcb_randr_get_screen_resources_current_crtcs(srr);
8724 for (c = 0; c < ncrtc; c++) {
8725 cic = xcb_randr_get_crtc_info(conn, crtc[c],
8727 cir = xcb_randr_get_crtc_info_reply(conn, cic, NULL);
8730 if (cir->num_outputs == 0) {
8736 new_region(&screens[i], 0, 0,
8737 screen->width_in_pixels,
8738 screen->height_in_pixels);
8740 new_region(&screens[i],
8741 cir->x, cir->y, cir->width, cir->height);
8746 #endif /* SWM_XRR_HAS_CRTC */
8748 /* If detection failed, create a single region that spans the screen. */
8749 if (TAILQ_EMPTY(&screens[i].rl))
8750 new_region(&screens[i], 0, 0, screen->width_in_pixels,
8751 screen->height_in_pixels);
8754 DNPRINTF(SWM_D_MISC, "scan_xrandr: done.\n");
8758 screenchange(xcb_randr_screen_change_notify_event_t *e)
8760 struct swm_region *r;
8763 DNPRINTF(SWM_D_EVENT, "screenchange: root: 0x%x\n", e->root);
8765 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
8766 /* silly event doesn't include the screen index */
8767 for (i = 0; i < num_screens; i++)
8768 if (screens[i].root == e->root)
8770 if (i >= num_screens)
8771 errx(1, "screenchange: screen not found");
8773 /* brute force for now, just re-enumerate the regions */
8777 print_win_geom(e->root);
8779 /* add bars to all regions */
8780 for (i = 0; i < num_screens; i++)
8781 TAILQ_FOREACH(r, &screens[i].rl, entry)
8791 struct swm_region *r = NULL;
8792 xcb_window_t *wins = NULL, trans;
8794 int i, j, num_screens;
8795 uint16_t state, manage, mapped;
8797 xcb_query_tree_cookie_t qtc;
8798 xcb_query_tree_reply_t *qtr;
8799 xcb_get_window_attributes_cookie_t gac;
8800 xcb_get_window_attributes_reply_t *gar;
8801 xcb_get_property_cookie_t pc;
8803 DNPRINTF(SWM_D_INIT, "grab_windows: begin\n");
8804 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
8805 for (i = 0; i < num_screens; i++) {
8806 qtc = xcb_query_tree(conn, screens[i].root);
8807 qtr = xcb_query_tree_reply(conn, qtc, NULL);
8810 wins = xcb_query_tree_children(qtr);
8811 no = xcb_query_tree_children_length(qtr);
8812 /* attach windows to a region */
8813 /* normal windows */
8814 DNPRINTF(SWM_D_INIT, "grab_windows: grab top level windows.\n");
8815 for (j = 0; j < no; j++) {
8816 TAILQ_FOREACH(r, &screens[i].rl, entry) {
8817 if (r->id == wins[j]) {
8818 DNPRINTF(SWM_D_INIT, "grab_windows: "
8819 "skip %#x; region input window.\n",
8828 gac = xcb_get_window_attributes(conn, wins[j]);
8829 gar = xcb_get_window_attributes_reply(conn, gac, NULL);
8831 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
8832 "doesn't exist.\n", wins[j]);
8836 if (gar->override_redirect) {
8837 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
8838 "override_redirect set.\n", wins[j]);
8843 pc = xcb_icccm_get_wm_transient_for(conn, wins[j]);
8844 if (xcb_icccm_get_wm_transient_for_reply(conn, pc,
8846 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
8847 "is transient for %#x.\n", wins[j], trans);
8852 state = getstate(wins[j]);
8853 manage = state != XCB_ICCCM_WM_STATE_WITHDRAWN;
8854 mapped = gar->map_state != XCB_MAP_STATE_UNMAPPED;
8855 if (mapped || manage)
8856 manage_window(wins[j], mapped);
8859 /* transient windows */
8860 DNPRINTF(SWM_D_INIT, "grab_windows: grab transient windows.\n");
8861 for (j = 0; j < no; j++) {
8862 gac = xcb_get_window_attributes(conn, wins[j]);
8863 gar = xcb_get_window_attributes_reply(conn, gac, NULL);
8865 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
8866 "doesn't exist.\n", wins[j]);
8870 if (gar->override_redirect) {
8871 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
8872 "override_redirect set.\n", wins[j]);
8877 state = getstate(wins[j]);
8878 manage = state != XCB_ICCCM_WM_STATE_WITHDRAWN;
8879 mapped = gar->map_state != XCB_MAP_STATE_UNMAPPED;
8880 pc = xcb_icccm_get_wm_transient_for(conn, wins[j]);
8881 if (xcb_icccm_get_wm_transient_for_reply(conn, pc,
8882 &trans, NULL) && manage)
8883 manage_window(wins[j], mapped);
8888 DNPRINTF(SWM_D_INIT, "grab_windows: done.\n");
8894 int i, j, k, num_screens;
8895 struct workspace *ws;
8896 uint32_t gcv[1], wa[1];
8897 const xcb_query_extension_reply_t *qep;
8898 xcb_screen_t *screen;
8899 xcb_randr_query_version_cookie_t c;
8900 xcb_randr_query_version_reply_t *r;
8902 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
8903 if ((screens = calloc(num_screens,
8904 sizeof(struct swm_screen))) == NULL)
8905 err(1, "setup_screens: calloc: failed to allocate memory for "
8908 /* initial Xrandr setup */
8910 qep = xcb_get_extension_data(conn, &xcb_randr_id);
8912 c = xcb_randr_query_version(conn, 1, 1);
8913 r = xcb_randr_query_version_reply(conn, c, NULL);
8915 if (r->major_version >= 1) {
8917 xrandr_eventbase = qep->first_event;
8923 wa[0] = cursors[XC_LEFT_PTR].cid;
8925 /* map physical screens */
8926 for (i = 0; i < num_screens; i++) {
8927 DNPRINTF(SWM_D_WS, "setup_screens: init screen: %d\n", i);
8929 screens[i].r_focus = NULL;
8931 TAILQ_INIT(&screens[i].rl);
8932 TAILQ_INIT(&screens[i].orl);
8933 if ((screen = get_screen(i)) == NULL)
8934 errx(1, "ERROR: can't get screen %d.", i);
8935 screens[i].root = screen->root;
8937 /* set default colors */
8938 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
8939 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
8940 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
8941 setscreencolor("rgb:00/40/40", i + 1,
8942 SWM_S_COLOR_BAR_BORDER_UNFOCUS);
8943 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
8944 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
8946 /* create graphics context on screen */
8947 screens[i].bar_gc = xcb_generate_id(conn);
8949 xcb_create_gc(conn, screens[i].bar_gc, screens[i].root,
8950 XCB_GC_GRAPHICS_EXPOSURES, gcv);
8952 /* set default cursor */
8953 xcb_change_window_attributes(conn, screens[i].root,
8956 /* init all workspaces */
8957 /* XXX these should be dynamically allocated too */
8958 for (j = 0; j < SWM_WS_MAX; j++) {
8959 ws = &screens[i].ws[j];
8962 ws->bar_enabled = 1;
8964 ws->focus_prev = NULL;
8965 ws->focus_pending = NULL;
8968 TAILQ_INIT(&ws->winlist);
8969 TAILQ_INIT(&ws->unmanagedlist);
8971 for (k = 0; layouts[k].l_stack != NULL; k++)
8972 if (layouts[k].l_config != NULL)
8973 layouts[k].l_config(ws,
8974 SWM_ARG_ID_STACKINIT);
8975 ws->cur_layout = &layouts[0];
8976 ws->cur_layout->l_string(ws);
8982 xcb_randr_select_input(conn, screens[i].root,
8983 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE);
8990 if ((bar_fonts = strdup(SWM_BAR_FONTS)) == NULL)
8991 err(1, "setup_globals: strdup: failed to allocate memory.");
8993 if ((clock_format = strdup("%a %b %d %R %Z %Y")) == NULL)
8994 err(1, "setup_globals: strdup: failed to allocate memory.");
8996 if ((syms = xcb_key_symbols_alloc(conn)) == NULL)
8997 errx(1, "unable to allocate key symbols");
8999 a_state = get_atom_from_string("WM_STATE");
9000 a_prot = get_atom_from_string("WM_PROTOCOLS");
9001 a_delete = get_atom_from_string("WM_DELETE_WINDOW");
9002 a_takefocus = get_atom_from_string("WM_TAKE_FOCUS");
9003 a_wmname = get_atom_from_string("WM_NAME");
9004 a_netwmname = get_atom_from_string("_NET_WM_NAME");
9005 a_utf8_string = get_atom_from_string("UTF8_STRING");
9006 a_string = get_atom_from_string("STRING");
9007 a_swm_iconic = get_atom_from_string("_SWM_ICONIC");
9008 a_swm_ws = get_atom_from_string("_SWM_WS");
9015 xcb_atom_t netwmcheck;
9016 xcb_window_t root, win;
9018 /* work around sun jdk bugs, code from wmname */
9019 netwmcheck = get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
9021 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
9022 for (i = 0; i < num_screens; i++) {
9023 root = screens[i].root;
9025 win = xcb_generate_id(conn);
9026 xcb_create_window(conn, XCB_COPY_FROM_PARENT, win, root,
9027 0, 0, 1, 1, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT,
9028 XCB_COPY_FROM_PARENT, 0, NULL);
9030 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root,
9031 netwmcheck, XCB_ATOM_WINDOW, 32, 1, &win);
9032 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win,
9033 netwmcheck, XCB_ATOM_WINDOW, 32, 1, &win);
9034 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win,
9035 a_netwmname, a_utf8_string, 8, strlen("LG3D"), "LG3D");
9040 shutdown_cleanup(void)
9044 /* disable alarm because the following code may not be interrupted */
9046 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
9047 err(1, "can't disable alarm");
9056 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
9057 for (i = 0; i < num_screens; ++i) {
9058 if (screens[i].bar_gc != 0)
9059 xcb_free_gc(conn, screens[i].bar_gc);
9060 if (!bar_font_legacy)
9061 XftColorFree(display, DefaultVisual(display, i),
9062 DefaultColormap(display, i), &bar_font_color);
9065 if (bar_font_legacy)
9066 XFreeFontSet(display, bar_fs);
9068 XftFontClose(display, bar_font);
9071 xcb_key_symbols_free(syms);
9073 xcb_disconnect(conn);
9077 event_error(xcb_generic_error_t *e)
9081 DNPRINTF(SWM_D_EVENT, "event_error: %s(%u) from %s(%u), sequence: %u, "
9082 "resource_id: %u, minor_code: %u\n",
9083 xcb_event_get_error_label(e->error_code), e->error_code,
9084 xcb_event_get_request_label(e->major_code), e->major_code,
9085 e->sequence, e->resource_id, e->minor_code);
9089 event_handle(xcb_generic_event_t *evt)
9091 uint8_t type = XCB_EVENT_RESPONSE_TYPE(evt);
9093 DNPRINTF(SWM_D_EVENT, "XCB Event: %s(%d), seq %u\n",
9094 xcb_event_get_label(XCB_EVENT_RESPONSE_TYPE(evt)),
9095 XCB_EVENT_RESPONSE_TYPE(evt), evt->sequence);
9098 #define EVENT(type, callback) case type: callback((void *)evt); return
9099 EVENT(0, event_error);
9100 EVENT(XCB_BUTTON_PRESS, buttonpress);
9101 /*EVENT(XCB_BUTTON_RELEASE, buttonpress);*/
9102 /*EVENT(XCB_CIRCULATE_NOTIFY, );*/
9103 /*EVENT(XCB_CIRCULATE_REQUEST, );*/
9104 EVENT(XCB_CLIENT_MESSAGE, clientmessage);
9105 /*EVENT(XCB_COLORMAP_NOTIFY, );*/
9106 EVENT(XCB_CONFIGURE_NOTIFY, configurenotify);
9107 EVENT(XCB_CONFIGURE_REQUEST, configurerequest);
9108 /*EVENT(XCB_CREATE_NOTIFY, );*/
9109 EVENT(XCB_DESTROY_NOTIFY, destroynotify);
9110 EVENT(XCB_ENTER_NOTIFY, enternotify);
9111 EVENT(XCB_EXPOSE, expose);
9113 EVENT(XCB_FOCUS_IN, focusin);
9114 EVENT(XCB_FOCUS_OUT, focusout);
9116 /*EVENT(XCB_GRAPHICS_EXPOSURE, );*/
9117 /*EVENT(XCB_GRAVITY_NOTIFY, );*/
9118 EVENT(XCB_KEY_PRESS, keypress);
9119 /*EVENT(XCB_KEY_RELEASE, keypress);*/
9120 /*EVENT(XCB_KEYMAP_NOTIFY, );*/
9122 EVENT(XCB_LEAVE_NOTIFY, leavenotify);
9124 EVENT(XCB_MAP_NOTIFY, mapnotify);
9125 EVENT(XCB_MAP_REQUEST, maprequest);
9126 EVENT(XCB_MAPPING_NOTIFY, mappingnotify);
9127 EVENT(XCB_MOTION_NOTIFY, motionnotify);
9128 /*EVENT(XCB_NO_EXPOSURE, );*/
9129 EVENT(XCB_PROPERTY_NOTIFY, propertynotify);
9130 /*EVENT(XCB_REPARENT_NOTIFY, );*/
9131 /*EVENT(XCB_RESIZE_REQUEST, );*/
9132 /*EVENT(XCB_SELECTION_CLEAR, );*/
9133 /*EVENT(XCB_SELECTION_NOTIFY, );*/
9134 /*EVENT(XCB_SELECTION_REQUEST, );*/
9135 EVENT(XCB_UNMAP_NOTIFY, unmapnotify);
9136 /*EVENT(XCB_VISIBILITY_NOTIFY, visibilitynotify);*/
9139 if (type - xrandr_eventbase == XCB_RANDR_SCREEN_CHANGE_NOTIFY)
9140 screenchange((void *)evt);
9144 main(int argc, char *argv[])
9146 struct swm_region *r;
9147 char conf[PATH_MAX], *cfile = NULL;
9149 int xfd, i, num_screens, startup = 1;
9150 struct sigaction sact;
9151 xcb_generic_event_t *evt;
9155 int stdin_ready = 0;
9158 /* suppress unused warning since var is needed */
9161 time_started = time(NULL);
9164 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION, buildstr);
9165 if (!setlocale(LC_CTYPE, "") || !setlocale(LC_TIME, ""))
9166 warnx("no locale support");
9168 /* handle some signals */
9169 bzero(&sact, sizeof(sact));
9170 sigemptyset(&sact.sa_mask);
9172 sact.sa_handler = sighdlr;
9173 sigaction(SIGINT, &sact, NULL);
9174 sigaction(SIGQUIT, &sact, NULL);
9175 sigaction(SIGTERM, &sact, NULL);
9176 sigaction(SIGHUP, &sact, NULL);
9178 sact.sa_handler = sighdlr;
9179 sact.sa_flags = SA_NOCLDSTOP;
9180 sigaction(SIGCHLD, &sact, NULL);
9182 if (!(display = XOpenDisplay(0)))
9183 errx(1, "can not open display");
9185 conn = XGetXCBConnection(display);
9186 if (xcb_connection_has_error(conn))
9187 errx(1, "can not get XCB connection");
9189 XSetEventQueueOwner(display, XCBOwnsEventQueue);
9191 xcb_prefetch_extension_data(conn, &xcb_randr_id);
9192 xfd = xcb_get_file_descriptor(conn);
9194 /* look for local and global conf file */
9195 pwd = getpwuid(getuid());
9197 errx(1, "invalid user: %d", getuid());
9199 xcb_grab_server(conn);
9202 /* flush all events */
9203 while ((evt = xcb_poll_for_event(conn))) {
9204 if (XCB_EVENT_RESPONSE_TYPE(evt) == 0)
9209 if (enable_wm() != 0)
9210 errx(1, "another window manager is currently running");
9212 /* Load Xcursors and/or cursorfont glyph cursors. */
9224 for (i = 0; ; i++) {
9229 snprintf(conf, sizeof conf, "%s/.%s",
9230 pwd->pw_dir, SWM_CONF_FILE);
9234 snprintf(conf, sizeof conf, "/etc/%s",
9239 snprintf(conf, sizeof conf, "%s/.%s",
9240 pwd->pw_dir, SWM_CONF_FILE_OLD);
9244 snprintf(conf, sizeof conf, "/etc/%s",
9251 if (strlen(conf) && stat(conf, &sb) != -1)
9252 if (S_ISREG(sb.st_mode)) {
9259 /* load conf (if any) */
9261 conf_load(cfile, SWM_CONF_DEFAULT);
9264 /* set some values to work around bad programs */
9266 /* grab existing windows (before we build the bars) */
9269 if (getenv("SWM_STARTED") == NULL)
9270 setenv("SWM_STARTED", "YES", 1);
9272 /* setup all bars */
9273 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
9274 for (i = 0; i < num_screens; i++)
9275 TAILQ_FOREACH(r, &screens[i].rl, entry)
9282 xcb_ungrab_server(conn);
9285 rd_max = xfd > STDIN_FILENO ? xfd : STDIN_FILENO;
9288 while ((evt = xcb_poll_for_event(conn))) {
9295 /* If just (re)started, set default focus if needed. */
9299 if (focus_mode != SWM_FOCUS_FOLLOW) {
9300 r = TAILQ_FIRST(&screens[0].rl);
9312 FD_SET(STDIN_FILENO, &rd);
9317 num_readable = select(rd_max + 1, &rd, NULL, NULL, &tv);
9318 if (num_readable == -1 && errno != EINTR) {
9319 DNPRINTF(SWM_D_MISC, "select failed");
9320 } else if (num_readable > 0 && FD_ISSET(STDIN_FILENO, &rd)) {
9325 restart(NULL, NULL);
9335 if (bar_extra_update()) {