2 * Copyright (c) 2009-2012 Marco Peereboom <marco@peereboom.us>
3 * Copyright (c) 2009-2011 Ryan McBride <mcbride@countersiege.com>
4 * Copyright (c) 2009 Darrin Chandler <dwchandler@stilyagin.com>
5 * Copyright (c) 2009 Pierre-Yves Ritschard <pyr@spootnik.org>
6 * Copyright (c) 2010 Tuukka Kataja <stuge@xor.fi>
7 * Copyright (c) 2011 Jason L. Wright <jason@thought.net>
8 * Copyright (c) 2011-2012 Reginald Kennedy <rk@rejii.com>
9 * Copyright (c) 2011-2012 Lawrence Teo <lteo@lteo.net>
10 * Copyright (c) 2011-2012 Tiago Cunha <tcunha@gmx.com>
11 * Copyright (c) 2012 David Hill <dhill@mindcry.org>
13 * Permission to use, copy, modify, and distribute this software for any
14 * purpose with or without fee is hereby granted, provided that the above
15 * copyright notice and this permission notice appear in all copies.
17 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
18 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
20 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
21 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
22 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
23 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 * Much code and ideas taken from dwm under the following license:
27 * MIT/X Consortium License
29 * 2006-2008 Anselm R Garbe <garbeam at gmail dot com>
30 * 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
31 * 2006-2007 Jukka Salmi <jukka at salmi dot ch>
32 * 2007 Premysl Hruby <dfenze at gmail dot com>
33 * 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
34 * 2007 Christof Musik <christof at sendfax dot de>
35 * 2007-2008 Enno Gottox Boland <gottox at s01 dot de>
36 * 2007-2008 Peter Hartlich <sgkkr at hartlich dot com>
37 * 2008 Martin Hurton <martin dot hurton at gmail dot com>
39 * Permission is hereby granted, free of charge, to any person obtaining a
40 * copy of this software and associated documentation files (the "Software"),
41 * to deal in the Software without restriction, including without limitation
42 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
43 * and/or sell copies of the Software, and to permit persons to whom the
44 * Software is furnished to do so, subject to the following conditions:
46 * The above copyright notice and this permission notice shall be included in
47 * all copies or substantial portions of the Software.
49 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
50 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
51 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
52 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
53 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
54 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
55 * DEALINGS IN THE SOFTWARE.
59 #include <sys/types.h>
63 #include <sys/queue.h>
64 #include <sys/param.h>
65 #include <sys/select.h>
66 #if defined(__linux__)
68 #elif defined(__OpenBSD__)
70 #elif defined(__FreeBSD__)
91 #include <X11/cursorfont.h>
92 #include <X11/extensions/Xrandr.h>
93 #include <X11/Xcursor/Xcursor.h>
94 #include <X11/Xft/Xft.h>
95 #include <X11/Xlib-xcb.h>
96 #include <xcb/xcb_atom.h>
97 #include <xcb/xcb_aux.h>
98 #include <xcb/xcb_event.h>
99 #include <xcb/xcb_icccm.h>
100 #include <xcb/xcb_keysyms.h>
101 #include <xcb/xtest.h>
102 #include <xcb/randr.h>
110 #ifdef SPECTRWM_BUILDSTR
111 static const char *buildstr = SPECTRWM_BUILDSTR;
113 static const char *buildstr = SPECTRWM_VERSION;
116 #if !defined(__CYGWIN__) /* cygwin chokes on xrandr stuff */
118 # error XRandR versions less than 1.0 are not supported
121 # if RANDR_MAJOR >= 1
122 # if RANDR_MINOR >= 2
123 # define SWM_XRR_HAS_CRTC
126 #endif /* __CYGWIN__ */
128 #ifndef XCB_ICCCM_NUM_WM_HINTS_ELEMENTS
129 #define XCB_ICCCM_SIZE_HINT_P_MIN_SIZE XCB_SIZE_HINT_P_MIN_SIZE
130 #define XCB_ICCCM_SIZE_HINT_P_MAX_SIZE XCB_SIZE_HINT_P_MAX_SIZE
131 #define XCB_ICCCM_SIZE_HINT_P_RESIZE_INC XCB_SIZE_HINT_P_RESIZE_INC
132 #define XCB_ICCCM_WM_HINT_INPUT XCB_WM_HINT_INPUT
133 #define XCB_ICCCM_WM_HINT_X_URGENCY XCB_WM_HINT_X_URGENCY
134 #define XCB_ICCCM_WM_STATE_ICONIC XCB_WM_STATE_ICONIC
135 #define XCB_ICCCM_WM_STATE_WITHDRAWN XCB_WM_STATE_WITHDRAWN
136 #define XCB_ICCCM_WM_STATE_NORMAL XCB_WM_STATE_NORMAL
137 #define xcb_icccm_get_text_property_reply_t xcb_get_text_property_reply_t
138 #define xcb_icccm_get_text_property_reply_wipe xcb_get_text_property_reply_wipe
139 #define xcb_icccm_get_wm_class xcb_get_wm_class
140 #define xcb_icccm_get_wm_class_reply xcb_get_wm_class_reply
141 #define xcb_icccm_get_wm_class_reply_t xcb_get_wm_class_reply_t
142 #define xcb_icccm_get_wm_class_reply_wipe xcb_get_wm_class_reply_wipe
143 #define xcb_icccm_get_wm_hints xcb_get_wm_hints
144 #define xcb_icccm_get_wm_hints_reply xcb_get_wm_hints_reply
145 #define xcb_icccm_get_wm_name xcb_get_wm_name
146 #define xcb_icccm_get_wm_name_reply xcb_get_wm_name_reply
147 #define xcb_icccm_get_wm_normal_hints xcb_get_wm_normal_hints
148 #define xcb_icccm_get_wm_normal_hints_reply xcb_get_wm_normal_hints_reply
149 #define xcb_icccm_get_wm_protocols xcb_get_wm_protocols
150 #define xcb_icccm_get_wm_protocols_reply xcb_get_wm_protocols_reply
151 #define xcb_icccm_get_wm_protocols_reply_t xcb_get_wm_protocols_reply_t
152 #define xcb_icccm_get_wm_protocols_reply_wipe xcb_get_wm_protocols_reply_wipe
153 #define xcb_icccm_get_wm_transient_for xcb_get_wm_transient_for
154 #define xcb_icccm_get_wm_transient_for_reply xcb_get_wm_transient_for_reply
155 #define xcb_icccm_wm_hints_t xcb_wm_hints_t
158 /*#define SWM_DEBUG*/
160 #define DPRINTF(x...) do { \
162 fprintf(stderr, x); \
164 #define DNPRINTF(n,x...) do { \
165 if (swm_debug & n) { \
166 fprintf(stderr, "%ld ", (long)(time(NULL) - time_started)); \
167 fprintf(stderr, x); \
170 #define SWM_D_MISC 0x0001
171 #define SWM_D_EVENT 0x0002
172 #define SWM_D_WS 0x0004
173 #define SWM_D_FOCUS 0x0008
174 #define SWM_D_MOVE 0x0010
175 #define SWM_D_STACK 0x0020
176 #define SWM_D_MOUSE 0x0040
177 #define SWM_D_PROP 0x0080
178 #define SWM_D_CLASS 0x0100
179 #define SWM_D_KEY 0x0200
180 #define SWM_D_QUIRK 0x0400
181 #define SWM_D_SPAWN 0x0800
182 #define SWM_D_EVENTQ 0x1000
183 #define SWM_D_CONF 0x2000
184 #define SWM_D_BAR 0x4000
185 #define SWM_D_INIT 0x8000
187 u_int32_t swm_debug = 0
206 #define DPRINTF(x...)
207 #define DNPRINTF(n,x...)
210 /* convert 8-bit to 16-bit */
211 #define RGB_8_TO_16(col) ((col) << 8) + (col)
213 #define PIXEL_TO_XRENDERCOLOR(px, xrc) \
214 xrc.red = RGB_8_TO_16((px) >> 16 & 0xff); \
215 xrc.green = RGB_8_TO_16((px) >> 8 & 0xff); \
216 xrc.blue = RGB_8_TO_16((px) & 0xff); \
219 #define LENGTH(x) (int)(sizeof (x) / sizeof (x)[0])
220 #define MODKEY XCB_MOD_MASK_1
221 #define CLEANMASK(mask) ((mask) & ~(numlockmask | XCB_MOD_MASK_LOCK))
222 #define BUTTONMASK (XCB_EVENT_MASK_BUTTON_PRESS | \
223 XCB_EVENT_MASK_BUTTON_RELEASE)
224 #define MOUSEMASK (BUTTONMASK|XCB_EVENT_MASK_POINTER_MOTION)
225 #define SWM_PROPLEN (16)
226 #define SWM_FUNCNAME_LEN (32)
227 #define SWM_KEYS_LEN (255)
228 #define SWM_QUIRK_LEN (64)
229 #define X(r) (r)->g.x
230 #define Y(r) (r)->g.y
231 #define WIDTH(r) (r)->g.w
232 #define HEIGHT(r) (r)->g.h
233 #define BORDER(w) ((w)->bordered ? border_width : 0)
234 #define MAX_X(r) ((r)->g.x + (r)->g.w)
235 #define MAX_Y(r) ((r)->g.y + (r)->g.h)
236 #define SH_MIN(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE
237 #define SH_MIN_W(w) (w)->sh.min_width
238 #define SH_MIN_H(w) (w)->sh.min_height
239 #define SH_MAX(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MAX_SIZE
240 #define SH_MAX_W(w) (w)->sh.max_width
241 #define SH_MAX_H(w) (w)->sh.max_height
242 #define SH_INC(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_RESIZE_INC
243 #define SH_INC_W(w) (w)->sh.width_inc
244 #define SH_INC_H(w) (w)->sh.height_inc
245 #define SWM_MAX_FONT_STEPS (3)
246 #define WINID(w) ((w) ? (w)->id : 0)
247 #define YESNO(x) ((x) ? "yes" : "no")
249 #define SWM_FOCUS_DEFAULT (0)
250 #define SWM_FOCUS_FOLLOW (1)
251 #define SWM_FOCUS_MANUAL (2)
253 #define SWM_CK_NONE 0
254 #define SWM_CK_ALL 0xf
255 #define SWM_CK_FOCUS 0x1
256 #define SWM_CK_POINTER 0x2
257 #define SWM_CK_FALLBACK 0x4
258 #define SWM_CK_REGION 0x8
260 #define SWM_CONF_DEFAULT (0)
261 #define SWM_CONF_KEYMAPPING (1)
264 #define SWM_LIB "/usr/local/lib/libswmhack.so"
271 xcb_atom_t a_takefocus;
273 xcb_atom_t a_netwmname;
274 xcb_atom_t a_utf8_string;
276 xcb_atom_t a_swm_iconic;
278 volatile sig_atomic_t running = 1;
279 volatile sig_atomic_t restart_wm = 0;
280 xcb_timestamp_t last_event_time = 0;
284 int xrandr_eventbase;
285 unsigned int numlockmask = 0;
288 xcb_connection_t *conn;
289 xcb_key_symbols_t *syms;
292 int cycle_visible = 0;
294 int font_adjusted = 0;
295 unsigned int mod_key = MODKEY;
298 struct swm_region *search_r;
299 int select_list_pipe[2];
300 int select_resp_pipe[2];
302 volatile sig_atomic_t search_resp;
303 int search_resp_action;
305 struct search_window {
306 TAILQ_ENTRY(search_window) entry;
310 xcb_window_t indicator;
312 TAILQ_HEAD(search_winlist, search_window);
313 struct search_winlist search_wl;
318 SWM_SEARCH_UNICONIFY,
319 SWM_SEARCH_NAME_WORKSPACE,
320 SWM_SEARCH_SEARCH_WORKSPACE,
321 SWM_SEARCH_SEARCH_WINDOW
324 #define SWM_STACK_TOP (0)
325 #define SWM_STACK_BOTTOM (1)
326 #define SWM_STACK_ABOVE (2)
327 #define SWM_STACK_BELOW (3)
330 double dialog_ratio = 0.6;
332 #define SWM_BAR_MAX (256)
333 #define SWM_BAR_JUSTIFY_LEFT (0)
334 #define SWM_BAR_JUSTIFY_CENTER (1)
335 #define SWM_BAR_JUSTIFY_RIGHT (2)
336 #define SWM_BAR_OFFSET (4)
337 #define SWM_BAR_FONTS "-*-terminus-medium-*-*-*-12-*-*-*-*-*-*-*," \
338 "-*-profont-*-*-*-*-12-*-*-*-*-*-*-*," \
339 "-*-times-medium-r-*-*-12-*-*-*-*-*-*-*," \
340 "-misc-fixed-medium-r-*-*-12-*-*-*-*-*-*-*," \
341 "-*-*-*-r-*-*-*-*-*-*-*-*-*-*"
343 #ifdef X_HAVE_UTF8_STRING
344 #define DRAWSTRING(x...) Xutf8DrawString(x)
346 #define DRAWSTRING(x...) XmbDrawString(x)
349 char *bar_argv[] = { NULL, NULL };
351 char bar_ext[SWM_BAR_MAX];
352 char bar_ext_buf[SWM_BAR_MAX];
353 char bar_vertext[SWM_BAR_MAX];
356 int bar_border_width = 1;
357 int bar_at_bottom = 0;
361 int bar_justify = SWM_BAR_JUSTIFY_LEFT;
362 char *bar_format = NULL;
363 int stack_enabled = 1;
364 int clock_enabled = 1;
365 int urgent_enabled = 0;
366 char *clock_format = NULL;
367 int title_name_enabled = 0;
368 int title_class_enabled = 0;
369 int window_name_enabled = 0;
370 int focus_mode = SWM_FOCUS_DEFAULT;
371 int focus_close = SWM_STACK_BELOW;
372 int focus_close_wrap = 1;
373 int focus_default = SWM_STACK_TOP;
374 int spawn_position = SWM_STACK_TOP;
375 int disable_border = 0;
376 int border_width = 1;
377 int region_padding = 0;
379 int verbose_layout = 0;
383 XFontSetExtents *bar_fs_extents;
385 int bar_font_legacy = 1;
387 XftColor bar_font_color;
389 char *startup_exception;
390 unsigned int nr_exceptions = 0;
392 /* layout manager data */
393 struct swm_geometry {
406 struct swm_geometry g;
409 /* virtual "screens" */
411 TAILQ_ENTRY(swm_region) entry;
413 struct swm_geometry g;
414 struct workspace *ws; /* current workspace on this region */
415 struct workspace *ws_prior; /* prior workspace on this region */
416 struct swm_screen *s; /* screen idx */
419 TAILQ_HEAD(swm_region_list, swm_region);
422 TAILQ_ENTRY(ws_win) entry;
424 xcb_window_t transient;
425 struct ws_win *focus_child; /* focus on child transient */
426 struct swm_geometry g; /* current geometry */
427 struct swm_geometry g_float; /* region coordinates */
428 int g_floatvalid; /* g_float geometry validity */
429 int floatmaxed; /* whether maxed by max_stack */
435 unsigned int ewmh_flags;
436 int font_size_boundary[SWM_MAX_FONT_STEPS];
442 unsigned long quirks;
443 struct workspace *ws; /* always valid */
444 struct swm_screen *s; /* always valid, never changes */
445 xcb_get_geometry_reply_t *wa;
447 xcb_icccm_get_wm_class_reply_t ch;
448 xcb_icccm_wm_hints_t hints;
450 TAILQ_HEAD(ws_win_list, ws_win);
454 TAILQ_ENTRY(pid_e) entry;
458 TAILQ_HEAD(pid_list, pid_e);
459 struct pid_list pidlist = TAILQ_HEAD_INITIALIZER(pidlist);
461 /* layout handlers */
463 void vertical_config(struct workspace *, int);
464 void vertical_stack(struct workspace *, struct swm_geometry *);
465 void horizontal_config(struct workspace *, int);
466 void horizontal_stack(struct workspace *, struct swm_geometry *);
467 void max_stack(struct workspace *, struct swm_geometry *);
468 void plain_stacker(struct workspace *);
469 void fancy_stacker(struct workspace *);
472 void (*l_stack)(struct workspace *, struct swm_geometry *);
473 void (*l_config)(struct workspace *, int);
475 #define SWM_L_FOCUSPREV (1<<0)
476 #define SWM_L_MAPONFOCUS (1<<1)
477 void (*l_string)(struct workspace *);
479 /* stack, configure */
480 { vertical_stack, vertical_config, 0, plain_stacker },
481 { horizontal_stack, horizontal_config, 0, plain_stacker },
483 SWM_L_MAPONFOCUS | SWM_L_FOCUSPREV, plain_stacker },
484 { NULL, NULL, 0, NULL },
487 /* position of max_stack mode in the layouts array, index into layouts! */
488 #define SWM_V_STACK (0)
489 #define SWM_H_STACK (1)
490 #define SWM_MAX_STACK (2)
492 #define SWM_H_SLICE (32)
493 #define SWM_V_SLICE (32)
495 /* define work spaces */
497 int idx; /* workspace index */
498 char *name; /* workspace name */
499 int always_raise; /* raise windows on focus */
500 int bar_enabled; /* bar visibility */
501 struct layout *cur_layout; /* current layout handlers */
502 struct ws_win *focus; /* may be NULL */
503 struct ws_win *focus_prev; /* may be NULL */
504 struct ws_win *focus_pending; /* may be NULL */
505 struct swm_region *r; /* may be NULL */
506 struct swm_region *old_r; /* may be NULL */
507 struct ws_win_list winlist; /* list of windows in ws */
508 struct ws_win_list unmanagedlist; /* list of dead windows in ws */
509 char stacker[10]; /* display stacker and layout */
513 int horizontal_msize;
515 int horizontal_stacks;
526 SWM_S_COLOR_BAR_BORDER,
527 SWM_S_COLOR_BAR_BORDER_UNFOCUS,
528 SWM_S_COLOR_BAR_FONT,
534 /* physical screen mapping */
535 #define SWM_WS_MAX (22) /* hard limit */
536 int workspace_limit = 10; /* soft limit */
539 int idx; /* screen index */
540 struct swm_region_list rl; /* list of regions on this screen */
541 struct swm_region_list orl; /* list of old regions */
543 struct workspace ws[SWM_WS_MAX];
544 struct swm_region *r_focus;
550 } c[SWM_S_COLOR_MAX];
552 xcb_gcontext_t bar_gc;
555 struct swm_screen *screens;
557 /* args to functions */
560 #define SWM_ARG_ID_FOCUSNEXT (0)
561 #define SWM_ARG_ID_FOCUSPREV (1)
562 #define SWM_ARG_ID_FOCUSMAIN (2)
563 #define SWM_ARG_ID_SWAPNEXT (10)
564 #define SWM_ARG_ID_SWAPPREV (11)
565 #define SWM_ARG_ID_SWAPMAIN (12)
566 #define SWM_ARG_ID_MOVELAST (13)
567 #define SWM_ARG_ID_MASTERSHRINK (20)
568 #define SWM_ARG_ID_MASTERGROW (21)
569 #define SWM_ARG_ID_MASTERADD (22)
570 #define SWM_ARG_ID_MASTERDEL (23)
571 #define SWM_ARG_ID_FLIPLAYOUT (24)
572 #define SWM_ARG_ID_STACKRESET (30)
573 #define SWM_ARG_ID_STACKINIT (31)
574 #define SWM_ARG_ID_CYCLEWS_UP (40)
575 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
576 #define SWM_ARG_ID_CYCLERG_UP (42)
577 #define SWM_ARG_ID_CYCLERG_DOWN (43)
578 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
579 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
580 #define SWM_ARG_ID_CYCLEWS_MOVE_UP (46)
581 #define SWM_ARG_ID_CYCLEWS_MOVE_DOWN (47)
582 #define SWM_ARG_ID_STACKINC (50)
583 #define SWM_ARG_ID_STACKDEC (51)
584 #define SWM_ARG_ID_SS_ALL (60)
585 #define SWM_ARG_ID_SS_WINDOW (61)
586 #define SWM_ARG_ID_DONTCENTER (70)
587 #define SWM_ARG_ID_CENTER (71)
588 #define SWM_ARG_ID_KILLWINDOW (80)
589 #define SWM_ARG_ID_DELETEWINDOW (81)
590 #define SWM_ARG_ID_WIDTHGROW (90)
591 #define SWM_ARG_ID_WIDTHSHRINK (91)
592 #define SWM_ARG_ID_HEIGHTGROW (92)
593 #define SWM_ARG_ID_HEIGHTSHRINK (93)
594 #define SWM_ARG_ID_MOVEUP (100)
595 #define SWM_ARG_ID_MOVEDOWN (101)
596 #define SWM_ARG_ID_MOVELEFT (102)
597 #define SWM_ARG_ID_MOVERIGHT (103)
598 #define SWM_ARG_ID_BAR_TOGGLE (110)
599 #define SWM_ARG_ID_BAR_TOGGLE_WS (111)
605 TAILQ_ENTRY(quirk) entry;
609 #define SWM_Q_FLOAT (1<<0) /* float this window */
610 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
611 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
612 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
613 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
614 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
615 #define SWM_Q_NOFOCUSONMAP (1<<6) /* Don't focus on window when mapped. */
616 #define SWM_Q_FOCUSONMAP_SINGLE (1<<7) /* Only focus if single win of type. */
618 TAILQ_HEAD(quirk_list, quirk);
619 struct quirk_list quirks = TAILQ_HEAD_INITIALIZER(quirks);
622 * Supported EWMH hints should be added to
623 * both the enum and the ewmh array
628 _NET_MOVERESIZE_WINDOW,
629 _NET_WM_ACTION_ABOVE,
630 _NET_WM_ACTION_CLOSE,
631 _NET_WM_ACTION_FULLSCREEN,
633 _NET_WM_ACTION_RESIZE,
634 _NET_WM_ALLOWED_ACTIONS,
638 _NET_WM_STATE_FULLSCREEN,
639 _NET_WM_STATE_HIDDEN,
640 _NET_WM_STATE_MAXIMIZED_HORZ,
641 _NET_WM_STATE_MAXIMIZED_VERT,
642 _NET_WM_STATE_SKIP_PAGER,
643 _NET_WM_STATE_SKIP_TASKBAR,
645 _NET_WM_WINDOW_TYPE_DIALOG,
646 _NET_WM_WINDOW_TYPE_DOCK,
647 _NET_WM_WINDOW_TYPE_NORMAL,
648 _NET_WM_WINDOW_TYPE_SPLASH,
649 _NET_WM_WINDOW_TYPE_TOOLBAR,
650 _NET_WM_WINDOW_TYPE_UTILITY,
651 _SWM_WM_STATE_MANUAL,
658 } ewmh[SWM_EWMH_HINT_MAX] = {
659 /* must be in same order as in the enum */
660 {"_NET_ACTIVE_WINDOW", XCB_ATOM_NONE},
661 {"_NET_CLOSE_WINDOW", XCB_ATOM_NONE},
662 {"_NET_MOVERESIZE_WINDOW", XCB_ATOM_NONE},
663 {"_NET_WM_ACTION_ABOVE", XCB_ATOM_NONE},
664 {"_NET_WM_ACTION_CLOSE", XCB_ATOM_NONE},
665 {"_NET_WM_ACTION_FULLSCREEN", XCB_ATOM_NONE},
666 {"_NET_WM_ACTION_MOVE", XCB_ATOM_NONE},
667 {"_NET_WM_ACTION_RESIZE", XCB_ATOM_NONE},
668 {"_NET_WM_ALLOWED_ACTIONS", XCB_ATOM_NONE},
669 {"_NET_WM_NAME", XCB_ATOM_NONE},
670 {"_NET_WM_STATE", XCB_ATOM_NONE},
671 {"_NET_WM_STATE_ABOVE", XCB_ATOM_NONE},
672 {"_NET_WM_STATE_FULLSCREEN", XCB_ATOM_NONE},
673 {"_NET_WM_STATE_HIDDEN", XCB_ATOM_NONE},
674 {"_NET_WM_STATE_MAXIMIZED_HORZ", XCB_ATOM_NONE},
675 {"_NET_WM_STATE_MAXIMIZED_VERT", XCB_ATOM_NONE},
676 {"_NET_WM_STATE_SKIP_PAGER", XCB_ATOM_NONE},
677 {"_NET_WM_STATE_SKIP_TASKBAR", XCB_ATOM_NONE},
678 {"_NET_WM_WINDOW_TYPE", XCB_ATOM_NONE},
679 {"_NET_WM_WINDOW_TYPE_DIALOG", XCB_ATOM_NONE},
680 {"_NET_WM_WINDOW_TYPE_DOCK", XCB_ATOM_NONE},
681 {"_NET_WM_WINDOW_TYPE_NORMAL", XCB_ATOM_NONE},
682 {"_NET_WM_WINDOW_TYPE_SPLASH", XCB_ATOM_NONE},
683 {"_NET_WM_WINDOW_TYPE_TOOLBAR", XCB_ATOM_NONE},
684 {"_NET_WM_WINDOW_TYPE_UTILITY", XCB_ATOM_NONE},
685 {"_SWM_WM_STATE_MANUAL", XCB_ATOM_NONE},
692 XC_BOTTOM_LEFT_CORNER,
693 XC_BOTTOM_RIGHT_CORNER,
701 char *name; /* Name used by Xcursor .*/
702 uint8_t cf_char; /* cursorfont index. */
704 } cursors[XC_MAX] = {
705 {"fleur", XC_fleur, XCB_CURSOR_NONE},
706 {"left_ptr", XC_left_ptr, XCB_CURSOR_NONE},
707 {"bottom_left_corner", XC_bottom_left_corner, XCB_CURSOR_NONE},
708 {"bottom_right_corner", XC_bottom_right_corner, XCB_CURSOR_NONE},
709 {"sizing", XC_sizing, XCB_CURSOR_NONE},
710 {"top_left_corner", XC_top_left_corner, XCB_CURSOR_NONE},
711 {"top_right_corner", XC_top_right_corner, XCB_CURSOR_NONE},
714 #define SWM_SPAWN_OPTIONAL 0x1
718 TAILQ_ENTRY(spawn_prog) entry;
724 TAILQ_HEAD(spawn_list, spawn_prog);
725 struct spawn_list spawns = TAILQ_HEAD_INITIALIZER(spawns);
727 /* user/key callable function IDs */
841 KF_DUMPWINS, /* MUST BE LAST */
849 enum keyfuncid funcid;
852 RB_HEAD(key_tree, key);
854 /* function prototypes */
855 void adjust_font(struct ws_win *);
856 void bar_class_name(char *, size_t, struct swm_region *);
857 void bar_class_title_name(char *, size_t, struct swm_region *);
858 void bar_cleanup(struct swm_region *);
859 void bar_extra_setup(void);
860 void bar_extra_stop(void);
861 int bar_extra_update(void);
862 void bar_fmt(const char *, char *, struct swm_region *, size_t);
863 void bar_fmt_expand(char *, size_t);
865 void bar_print(struct swm_region *, const char *);
866 void bar_print_legacy(struct swm_region *, const char *);
867 void bar_replace(char *, char *, struct swm_region *, size_t);
868 void bar_replace_pad(char *, int *, size_t);
869 char *bar_replace_seq(char *, char *, struct swm_region *, size_t *, size_t);
870 void bar_setup(struct swm_region *);
871 void bar_title_name(char *, size_t, struct swm_region *);
872 void bar_toggle(struct swm_region *, union arg *);
873 void bar_urgent(char *, size_t);
874 void bar_window_float(char *, size_t, struct swm_region *);
875 void bar_window_name(char *, size_t, struct swm_region *);
876 void bar_workspace_name(char *, size_t, struct swm_region *);
877 void buttonpress(xcb_button_press_event_t *);
878 void check_conn(void);
879 void clear_keys(void);
880 void clientmessage(xcb_client_message_event_t *);
881 void client_msg(struct ws_win *, xcb_atom_t, xcb_timestamp_t);
882 int conf_load(const char *, int);
883 void configurenotify(xcb_configure_notify_event_t *);
884 void configurerequest(xcb_configure_request_event_t *);
885 void config_win(struct ws_win *, xcb_configure_request_event_t *);
886 void constrain_window(struct ws_win *, struct swm_region *, int);
887 int count_win(struct workspace *, int);
888 void cursors_cleanup(void);
889 void cursors_load(void);
890 void custom_region(char *);
891 void cyclerg(struct swm_region *, union arg *);
892 void cyclews(struct swm_region *, union arg *);
893 void cycle_layout(struct swm_region *, union arg *);
894 void destroynotify(xcb_destroy_notify_event_t *);
895 void dumpwins(struct swm_region *, union arg *);
897 void enternotify(xcb_enter_notify_event_t *);
898 void event_drain(uint8_t);
899 void event_error(xcb_generic_error_t *);
900 void event_handle(xcb_generic_event_t *);
901 void ewmh_autoquirk(struct ws_win *);
902 void ewmh_get_win_state(struct ws_win *);
903 int ewmh_set_win_fullscreen(struct ws_win *, int);
904 void ewmh_update_actions(struct ws_win *);
905 void ewmh_update_win_state(struct ws_win *, xcb_atom_t, long);
906 char *expand_tilde(const char *);
907 void expose(xcb_expose_event_t *);
908 void fake_keypress(struct ws_win *, xcb_keysym_t, uint16_t);
909 struct pid_e *find_pid(pid_t);
910 struct ws_win *find_unmanaged_window(xcb_window_t);
911 struct ws_win *find_window(xcb_window_t);
912 void floating_toggle(struct swm_region *, union arg *);
913 int floating_toggle_win(struct ws_win *);
914 void focus(struct swm_region *, union arg *);
916 void focusin(xcb_focus_in_event_t *);
917 void focusout(xcb_focus_out_event_t *);
919 void focus_flush(void);
920 void focus_region(struct swm_region *);
921 void focusrg(struct swm_region *, union arg *);
922 void focus_win(struct ws_win *);
923 void fontset_init(void);
924 void free_window(struct ws_win *);
925 xcb_atom_t get_atom_from_string(const char *);
927 char *get_atom_name(xcb_atom_t);
929 struct ws_win *get_focus_magic(struct ws_win *);
930 struct ws_win *get_focus_prev(struct ws_win *);
932 char *get_notify_detail_label(uint8_t);
933 char *get_notify_mode_label(uint8_t);
935 struct ws_win *get_pointer_win(xcb_window_t);
936 struct ws_win *get_region_focus(struct swm_region *);
937 int get_region_index(struct swm_region *);
938 xcb_screen_t *get_screen(int);
939 int get_screen_count(void);
941 char *get_stack_mode_name(uint8_t);
943 int32_t get_swm_iconic(struct ws_win *);
944 char *get_win_name(xcb_window_t);
945 int get_ws_idx(xcb_window_t);
946 uint32_t getstate(xcb_window_t);
947 void grabbuttons(struct ws_win *);
949 void grab_windows(void);
950 void iconify(struct swm_region *, union arg *);
952 void keypress(xcb_key_press_event_t *);
953 int key_cmp(struct key *, struct key *);
954 void key_insert(unsigned int, KeySym, enum keyfuncid, const char *);
955 struct key *key_lookup(unsigned int, KeySym);
956 void key_remove(struct key *);
957 void key_replace(struct key *, unsigned int, KeySym, enum keyfuncid,
959 void kill_bar_extra_atexit(void);
960 void kill_refs(struct ws_win *);
962 void leavenotify(xcb_leave_notify_event_t *);
964 void load_float_geom(struct ws_win *, struct swm_region *);
965 struct ws_win *manage_window(xcb_window_t, uint16_t);
966 void map_window(struct ws_win *);
967 void mapnotify(xcb_map_notify_event_t *);
968 void mappingnotify(xcb_mapping_notify_event_t *);
969 void maprequest(xcb_map_request_event_t *);
970 void motionnotify(xcb_motion_notify_event_t *);
971 void move(struct ws_win *, union arg *);
972 void move_step(struct swm_region *, union arg *);
973 uint32_t name_to_pixel(int, const char *);
974 void name_workspace(struct swm_region *, union arg *);
975 void new_region(struct swm_screen *, int, int, int, int);
976 int parsekeys(char *, unsigned int, unsigned int *, KeySym *);
977 int parsequirks(char *, unsigned long *);
978 int parse_rgb(const char *, uint16_t *, uint16_t *, uint16_t *);
979 void pressbutton(struct swm_region *, union arg *);
980 void priorws(struct swm_region *, union arg *);
982 void print_win_geom(xcb_window_t);
984 void propertynotify(xcb_property_notify_event_t *);
985 void quirk_insert(const char *, const char *, unsigned long);
986 void quirk_remove(struct quirk *);
987 void quirk_replace(struct quirk *, const char *, const char *,
989 void quit(struct swm_region *, union arg *);
990 void raise_toggle(struct swm_region *, union arg *);
991 void resize(struct ws_win *, union arg *);
992 void resize_step(struct swm_region *, union arg *);
993 void restart(struct swm_region *, union arg *);
994 struct swm_region *root_to_region(xcb_window_t, int);
995 void screenchange(xcb_randr_screen_change_notify_event_t *);
996 void scan_xrandr(int);
997 void search_do_resp(void);
998 void search_resp_name_workspace(const char *, unsigned long);
999 void search_resp_search_window(const char *);
1000 void search_resp_search_workspace(const char *);
1001 void search_resp_uniconify(const char *, unsigned long);
1002 void search_win(struct swm_region *, union arg *);
1003 void search_win_cleanup(void);
1004 void search_workspace(struct swm_region *, union arg *);
1005 void send_to_rg(struct swm_region *, union arg *);
1006 void send_to_ws(struct swm_region *, union arg *);
1007 void set_region(struct swm_region *);
1008 int setautorun(char *, char *, int);
1009 int setconfbinding(char *, char *, int);
1010 int setconfcolor(char *, char *, int);
1011 int setconfmodkey(char *, char *, int);
1012 int setconfquirk(char *, char *, int);
1013 int setconfregion(char *, char *, int);
1014 int setconfspawn(char *, char *, int);
1015 int setconfvalue(char *, char *, int);
1016 void setkeybinding(unsigned int, KeySym, enum keyfuncid, const char *);
1017 int setkeymapping(char *, char *, int);
1018 int setlayout(char *, char *, int);
1019 void setquirk(const char *, const char *, unsigned long);
1020 void setscreencolor(char *, int, int);
1021 void setspawn(const char *, const char *, int);
1022 void setup_ewmh(void);
1023 void setup_globals(void);
1024 void setup_keys(void);
1025 void setup_quirks(void);
1026 void setup_screens(void);
1027 void setup_spawn(void);
1028 void set_child_transient(struct ws_win *, xcb_window_t *);
1029 void set_swm_iconic(struct ws_win *, int);
1030 void set_win_state(struct ws_win *, uint16_t);
1031 void shutdown_cleanup(void);
1033 void socket_setnonblock(int);
1034 void sort_windows(struct ws_win_list *);
1035 void spawn(int, union arg *, int);
1036 void spawn_custom(struct swm_region *, union arg *, const char *);
1037 int spawn_expand(struct swm_region *, union arg *, const char *, char ***);
1038 void spawn_insert(const char *, const char *, int);
1039 void spawn_remove(struct spawn_prog *);
1040 void spawn_replace(struct spawn_prog *, const char *, const char *, int);
1041 void spawn_select(struct swm_region *, union arg *, const char *, int *);
1042 void stack_config(struct swm_region *, union arg *);
1043 void stack_floater(struct ws_win *, struct swm_region *);
1044 void stack_master(struct workspace *, struct swm_geometry *, int, int);
1045 void store_float_geom(struct ws_win *, struct swm_region *);
1046 char *strdupsafe(const char *);
1047 void swapwin(struct swm_region *, union arg *);
1048 void switchws(struct swm_region *, union arg *);
1049 void teardown_ewmh(void);
1050 void unfocus_win(struct ws_win *);
1051 void uniconify(struct swm_region *, union arg *);
1052 void unmanage_window(struct ws_win *);
1053 void unmapnotify(xcb_unmap_notify_event_t *);
1054 void unmap_all(void);
1055 void unmap_window(struct ws_win *);
1056 void updatenumlockmask(void);
1057 void update_modkey(unsigned int);
1058 void update_window(struct ws_win *);
1059 void validate_spawns(void);
1060 int validate_win(struct ws_win *);
1061 int validate_ws(struct workspace *);
1062 /*void visibilitynotify(xcb_visibility_notify_event_t *);*/
1063 void version(struct swm_region *, union arg *);
1064 pid_t window_get_pid(xcb_window_t);
1065 void wkill(struct swm_region *, union arg *);
1066 void workaround(void);
1067 void xft_init(struct swm_region *);
1068 void _add_startup_exception(const char *, va_list);
1069 void add_startup_exception(const char *, ...);
1071 RB_PROTOTYPE(key_tree, key, entry, key_cmp);
1072 RB_GENERATE(key_tree, key, entry, key_cmp);
1073 struct key_tree keys;
1078 xcb_font_t cf = XCB_NONE;
1081 for (i = 0; i < LENGTH(cursors); ++i) {
1082 /* try to load Xcursor first. */
1083 cursors[i].cid = XcursorLibraryLoadCursor(display,
1086 /* fallback to cursorfont. */
1087 if (cursors[i].cid == XCB_CURSOR_NONE) {
1088 if (cf == XCB_NONE) {
1089 cf = xcb_generate_id(conn);
1090 xcb_open_font(conn, cf, strlen("cursor"),
1094 cursors[i].cid = xcb_generate_id(conn);
1095 xcb_create_glyph_cursor(conn, cursors[i].cid, cf, cf,
1096 cursors[i].cf_char, cursors[i].cf_char + 1, 0, 0, 0,
1097 0xffff, 0xffff, 0xffff);
1103 xcb_close_font(conn, cf);
1107 cursors_cleanup(void)
1110 for (i = 0; i < LENGTH(cursors); ++i)
1111 xcb_free_cursor(conn, cursors[i].cid);
1115 expand_tilde(const char *s)
1117 struct passwd *ppwd;
1125 errx(1, "expand_tilde: NULL string.");
1128 result = strdup(sc);
1134 if ((max = sysconf(_SC_LOGIN_NAME_MAX)) == -1)
1135 errx(1, "expand_tilde: sysconf");
1137 if ((user = calloc(1, max + 1)) == NULL)
1138 errx(1, "expand_tilde: calloc");
1140 for (i = 0; s[i] != '/' && s[i] != '\0'; ++i)
1145 ppwd = strlen(user) == 0 ? getpwuid(getuid()) : getpwnam(user);
1149 result = strdup(sc);
1151 if (asprintf(&result, "%s%s", ppwd->pw_dir, s) == -1)
1155 errx(1, "expand_tilde: failed to allocate memory.");
1161 parse_rgb(const char *rgb, uint16_t *rr, uint16_t *gg, uint16_t *bb)
1163 unsigned int tmpr, tmpg, tmpb;
1165 if (sscanf(rgb, "rgb:%x/%x/%x", &tmpr, &tmpg, &tmpb) != 3)
1168 *rr = RGB_8_TO_16(tmpr);
1169 *gg = RGB_8_TO_16(tmpg);
1170 *bb = RGB_8_TO_16(tmpb);
1176 get_screen(int screen)
1178 const xcb_setup_t *r;
1179 xcb_screen_iterator_t iter;
1181 if ((r = xcb_get_setup(conn)) == NULL) {
1182 DNPRINTF(SWM_D_MISC, "get_screen: xcb_get_setup\n");
1186 iter = xcb_setup_roots_iterator(r);
1187 for (; iter.rem; --screen, xcb_screen_next(&iter))
1195 get_screen_count(void)
1197 const xcb_setup_t *r;
1199 if ((r = xcb_get_setup(conn)) == NULL) {
1200 DNPRINTF(SWM_D_MISC, "get_screen_count: xcb_get_setup\n");
1204 return xcb_setup_roots_length(r);
1208 get_region_index(struct swm_region *r)
1210 struct swm_region *rr;
1216 TAILQ_FOREACH(rr, &r->s->rl, entry) {
1231 if (focus_mode == SWM_FOCUS_DEFAULT)
1232 event_drain(XCB_ENTER_NOTIFY);
1238 get_atom_from_string(const char *str)
1240 xcb_intern_atom_cookie_t c;
1241 xcb_intern_atom_reply_t *r;
1244 c = xcb_intern_atom(conn, 0, strlen(str), str);
1245 r = xcb_intern_atom_reply(conn, c, NULL);
1253 return (XCB_ATOM_NONE);
1257 set_swm_iconic(struct ws_win *win, int newv)
1264 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
1265 a_swm_iconic, XCB_ATOM_INTEGER, 32, 1, &v);
1267 xcb_delete_property(conn, win->id, a_swm_iconic);
1271 get_swm_iconic(struct ws_win *win)
1274 xcb_get_property_reply_t *pr = NULL;
1276 pr = xcb_get_property_reply(conn,
1277 xcb_get_property(conn, 0, win->id, a_swm_iconic,
1278 XCB_ATOM_INTEGER, 0, 1), NULL);
1281 if (pr->type != XCB_ATOM_INTEGER || pr->format != 32)
1283 v = *((int32_t *)xcb_get_property_value(pr));
1293 xcb_atom_t sup_list;
1294 int i, j, num_screens;
1296 sup_list = get_atom_from_string("_NET_SUPPORTED");
1298 for (i = 0; i < LENGTH(ewmh); i++)
1299 ewmh[i].atom = get_atom_from_string(ewmh[i].name);
1301 num_screens = get_screen_count();
1302 for (i = 0; i < num_screens; i++) {
1303 /* Support check window will be created by workaround(). */
1305 /* Report supported atoms */
1306 xcb_delete_property(conn, screens[i].root, sup_list);
1307 for (j = 0; j < LENGTH(ewmh); j++)
1308 xcb_change_property(conn, XCB_PROP_MODE_APPEND,
1309 screens[i].root, sup_list, XCB_ATOM_ATOM, 32, 1,
1318 xcb_atom_t sup_check, sup_list;
1320 xcb_get_property_cookie_t pc;
1321 xcb_get_property_reply_t *pr;
1323 sup_check = get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
1324 sup_list = get_atom_from_string("_NET_SUPPORTED");
1325 num_screens = get_screen_count();
1327 for (i = 0; i < num_screens; i++) {
1328 /* Get the support check window and destroy it */
1329 pc = xcb_get_property(conn, 0, screens[i].root, sup_check,
1330 XCB_ATOM_WINDOW, 0, 1);
1331 pr = xcb_get_property_reply(conn, pc, NULL);
1334 if (pr->format == sup_check) {
1335 id = *((xcb_window_t *)xcb_get_property_value(pr));
1337 xcb_destroy_window(conn, id);
1338 xcb_delete_property(conn, screens[i].root, sup_check);
1339 xcb_delete_property(conn, screens[i].root, sup_list);
1346 ewmh_autoquirk(struct ws_win *win)
1350 xcb_get_property_cookie_t c;
1351 xcb_get_property_reply_t *r;
1353 c = xcb_get_property(conn, 0, win->id,
1354 ewmh[_NET_WM_WINDOW_TYPE].atom, XCB_ATOM_ATOM, 0, UINT32_MAX);
1355 r = xcb_get_property_reply(conn, c, NULL);
1359 n = xcb_get_property_value_length(r);
1360 type = xcb_get_property_value(r);
1362 for (i = 0; i < n; i++) {
1363 if (type[i] == ewmh[_NET_WM_WINDOW_TYPE_NORMAL].atom)
1365 if (type[i] == ewmh[_NET_WM_WINDOW_TYPE_DOCK].atom ||
1366 type[i] == ewmh[_NET_WM_WINDOW_TYPE_TOOLBAR].atom ||
1367 type[i] == ewmh[_NET_WM_WINDOW_TYPE_UTILITY].atom) {
1369 win->quirks = SWM_Q_FLOAT | SWM_Q_ANYWHERE;
1372 if (type[i] == ewmh[_NET_WM_WINDOW_TYPE_SPLASH].atom ||
1373 type[i] == ewmh[_NET_WM_WINDOW_TYPE_DIALOG].atom) {
1375 win->quirks = SWM_Q_FLOAT;
1382 #define SWM_EWMH_ACTION_COUNT_MAX (6)
1383 #define EWMH_F_FULLSCREEN (1<<0)
1384 #define EWMH_F_ABOVE (1<<1)
1385 #define EWMH_F_HIDDEN (1<<2)
1386 #define EWMH_F_SKIP_PAGER (1<<3)
1387 #define EWMH_F_SKIP_TASKBAR (1<<4)
1388 #define SWM_F_MANUAL (1<<5)
1391 ewmh_set_win_fullscreen(struct ws_win *win, int fs)
1399 DNPRINTF(SWM_D_MISC, "ewmh_set_win_fullscreen: window: 0x%x, "
1400 "fullscreen %s\n", win->id, YESNO(fs));
1403 if (!win->g_floatvalid)
1404 store_float_geom(win, win->ws->r);
1406 win->g = win->ws->r->g;
1409 load_float_geom(win, win->ws->r);
1416 ewmh_update_actions(struct ws_win *win)
1418 xcb_atom_t actions[SWM_EWMH_ACTION_COUNT_MAX];
1424 actions[n++] = ewmh[_NET_WM_ACTION_CLOSE].atom;
1426 if (win->floating) {
1427 actions[n++] = ewmh[_NET_WM_ACTION_MOVE].atom;
1428 actions[n++] = ewmh[_NET_WM_ACTION_RESIZE].atom;
1429 actions[n++] = ewmh[_NET_WM_ACTION_ABOVE].atom;
1432 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
1433 ewmh[_NET_WM_ALLOWED_ACTIONS].atom, XCB_ATOM_ATOM, 32, 1, actions);
1436 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
1437 #define _NET_WM_STATE_ADD 1 /* add/set property */
1438 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
1441 ewmh_update_win_state(struct ws_win *win, xcb_atom_t state, long action)
1443 unsigned int mask = 0;
1444 unsigned int changed = 0;
1445 unsigned int orig_flags;
1450 DNPRINTF(SWM_D_PROP, "ewmh_update_win_state: window: 0x%x, state: %ld, "
1451 "action: %ld\n", win->id, (unsigned long)state, action);
1453 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
1454 mask = EWMH_F_FULLSCREEN;
1455 else if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
1456 mask = EWMH_F_ABOVE;
1457 else if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
1458 mask = SWM_F_MANUAL;
1459 else if (state == ewmh[_NET_WM_STATE_SKIP_PAGER].atom)
1460 mask = EWMH_F_SKIP_PAGER;
1461 else if (state == ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom)
1462 mask = EWMH_F_SKIP_TASKBAR;
1464 orig_flags = win->ewmh_flags;
1467 case _NET_WM_STATE_REMOVE:
1468 win->ewmh_flags &= ~mask;
1470 case _NET_WM_STATE_ADD:
1471 win->ewmh_flags |= mask;
1473 case _NET_WM_STATE_TOGGLE:
1474 win->ewmh_flags ^= mask;
1478 changed = (win->ewmh_flags & mask) ^ (orig_flags & mask) ? 1 : 0;
1480 if (state == ewmh[_NET_WM_STATE_ABOVE].atom) {
1481 if (changed && !floating_toggle_win(win))
1482 win->ewmh_flags = orig_flags; /* revert */
1483 } else if (state == ewmh[_SWM_WM_STATE_MANUAL].atom) {
1485 win->manual = (win->ewmh_flags & SWM_F_MANUAL) != 0;
1486 } else if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom) {
1487 if (changed && !ewmh_set_win_fullscreen(win,
1488 win->ewmh_flags & EWMH_F_FULLSCREEN))
1489 win->ewmh_flags = orig_flags; /* revert */
1492 xcb_delete_property(conn, win->id, ewmh[_NET_WM_STATE].atom);
1494 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
1495 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1496 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1497 &ewmh[_NET_WM_STATE_FULLSCREEN].atom);
1498 else if (win->ewmh_flags & EWMH_F_SKIP_PAGER)
1499 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1500 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1501 &ewmh[_NET_WM_STATE_SKIP_PAGER].atom);
1502 else if (win->ewmh_flags & EWMH_F_SKIP_TASKBAR)
1503 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1504 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1505 &ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom);
1506 else if (win->ewmh_flags & EWMH_F_ABOVE)
1507 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1508 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1509 &ewmh[_NET_WM_STATE_ABOVE].atom);
1510 else if (win->ewmh_flags & SWM_F_MANUAL)
1511 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1512 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1513 &ewmh[_SWM_WM_STATE_MANUAL].atom);
1517 ewmh_get_win_state(struct ws_win *win)
1520 xcb_get_property_cookie_t c;
1521 xcb_get_property_reply_t *r;
1527 win->ewmh_flags = 0;
1529 win->ewmh_flags |= EWMH_F_ABOVE;
1531 win->ewmh_flags |= SWM_F_MANUAL;
1533 c = xcb_get_property(conn, 0, win->id, ewmh[_NET_WM_STATE].atom,
1534 XCB_ATOM_ATOM, 0, UINT32_MAX);
1535 r = xcb_get_property_reply(conn, c, NULL);
1539 states = xcb_get_property_value(r);
1540 n = xcb_get_property_value_length(r);
1542 for (i = 0; i < n; i++)
1543 ewmh_update_win_state(win, states[i], _NET_WM_STATE_ADD);
1551 dumpwins(struct swm_region *r, union arg *args)
1555 xcb_get_window_attributes_cookie_t c;
1556 xcb_get_window_attributes_reply_t *wa;
1558 /* suppress unused warning since var is needed */
1561 if (r->ws == NULL) {
1562 warnx("dumpwins: invalid workspace");
1566 warnx("=== managed window list ws %02d ===", r->ws->idx);
1567 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
1568 state = getstate(win->id);
1569 c = xcb_get_window_attributes(conn, win->id);
1570 wa = xcb_get_window_attributes_reply(conn, c, NULL);
1572 warnx("window: 0x%x, map_state: %d, state: %u, "
1573 "transient: 0x%x", win->id, wa->map_state,
1574 state, win->transient);
1577 warnx("window: 0x%x, failed xcb_get_window_attributes",
1581 warnx("===== unmanaged window list =====");
1582 TAILQ_FOREACH(win, &r->ws->unmanagedlist, entry) {
1583 state = getstate(win->id);
1584 c = xcb_get_window_attributes(conn, win->id);
1585 wa = xcb_get_window_attributes_reply(conn, c, NULL);
1587 warnx("window: 0x%x, map_state: %d, state: %u, "
1588 "transient: 0x%x", win->id, wa->map_state,
1589 state, win->transient);
1592 warnx("window: 0x%x, failed xcb_get_window_attributes",
1596 warnx("=================================");
1600 dumpwins(struct swm_region *r, union arg *s)
1605 #endif /* SWM_DEBUG */
1610 int saved_errno, status;
1613 saved_errno = errno;
1617 while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) != 0) {
1622 if (errno != ECHILD)
1623 warn("sighdlr: waitpid");
1624 #endif /* SWM_DEBUG */
1627 if (pid == searchpid)
1631 if (WIFEXITED(status)) {
1632 if (WEXITSTATUS(status) != 0)
1633 warnx("sighdlr: child exit status: %d",
1634 WEXITSTATUS(status));
1636 warnx("sighdlr: child is terminated "
1638 #endif /* SWM_DEBUG */
1652 errno = saved_errno;
1658 struct pid_e *p = NULL;
1660 DNPRINTF(SWM_D_MISC, "find_pid: %d\n", pid);
1665 TAILQ_FOREACH(p, &pidlist, entry) {
1674 name_to_pixel(int sidx, const char *colorname)
1676 uint32_t result = 0;
1677 char cname[32] = "#";
1678 xcb_screen_t *screen;
1679 xcb_colormap_t cmap;
1680 xcb_alloc_color_reply_t *cr;
1681 xcb_alloc_named_color_reply_t *nr;
1682 uint16_t rr, gg, bb;
1684 screen = get_screen(sidx);
1685 cmap = screen->default_colormap;
1687 /* color is in format rgb://rr/gg/bb */
1688 if (strncmp(colorname, "rgb:", 4) == 0) {
1689 if (parse_rgb(colorname, &rr, &gg, &bb) == -1)
1690 warnx("could not parse rgb %s", colorname);
1692 cr = xcb_alloc_color_reply(conn,
1693 xcb_alloc_color(conn, cmap, rr, gg, bb),
1699 warnx("color '%s' not found", colorname);
1702 nr = xcb_alloc_named_color_reply(conn,
1703 xcb_alloc_named_color(conn, cmap, strlen(colorname),
1706 strlcat(cname, colorname + 2, sizeof cname - 1);
1707 nr = xcb_alloc_named_color_reply(conn,
1708 xcb_alloc_named_color(conn, cmap, strlen(cname),
1715 warnx("color '%s' not found", colorname);
1722 setscreencolor(char *val, int i, int c)
1726 num_screens = get_screen_count();
1727 if (i > 0 && i <= num_screens) {
1728 screens[i - 1].c[c].pixel = name_to_pixel(i - 1, val);
1729 free(screens[i - 1].c[c].name);
1730 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
1732 } else if (i == -1) {
1733 for (i = 0; i < num_screens; i++) {
1734 screens[i].c[c].pixel = name_to_pixel(0, val);
1735 free(screens[i].c[c].name);
1736 if ((screens[i].c[c].name = strdup(val)) == NULL)
1740 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1745 fancy_stacker(struct workspace *ws)
1747 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1748 if (ws->cur_layout->l_stack == vertical_stack)
1749 snprintf(ws->stacker, sizeof ws->stacker,
1750 ws->l_state.vertical_flip ? "[%d>%d]" : "[%d|%d]",
1751 ws->l_state.vertical_mwin, ws->l_state.vertical_stacks);
1752 if (ws->cur_layout->l_stack == horizontal_stack)
1753 snprintf(ws->stacker, sizeof ws->stacker,
1754 ws->l_state.horizontal_flip ? "[%dv%d]" : "[%d-%d]",
1755 ws->l_state.horizontal_mwin, ws->l_state.horizontal_stacks);
1759 plain_stacker(struct workspace *ws)
1761 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1762 if (ws->cur_layout->l_stack == vertical_stack)
1763 strlcpy(ws->stacker, ws->l_state.vertical_flip ? "[>]" : "[|]",
1764 sizeof ws->stacker);
1765 if (ws->cur_layout->l_stack == horizontal_stack)
1766 strlcpy(ws->stacker, ws->l_state.horizontal_flip ? "[v]" : "[-]",
1767 sizeof ws->stacker);
1771 custom_region(char *val)
1773 unsigned int x, y, w, h;
1774 int sidx, num_screens;
1775 xcb_screen_t *screen;
1777 num_screens = get_screen_count();
1778 if (sscanf(val, "screen[%d]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
1779 errx(1, "invalid custom region, "
1780 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1781 if (sidx < 1 || sidx > num_screens)
1782 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1786 if ((screen = get_screen(sidx)) == NULL)
1787 errx(1, "ERROR: can't get screen %d.", sidx);
1790 errx(1, "region %ux%u+%u+%u too small", w, h, x, y);
1792 if (x > screen->width_in_pixels ||
1793 y > screen->height_in_pixels ||
1794 w + x > screen->width_in_pixels ||
1795 h + y > screen->height_in_pixels) {
1796 warnx("ignoring region %ux%u+%u+%u - not within screen "
1797 "boundaries (%ux%u)", w, h, x, y,
1798 screen->width_in_pixels, screen->height_in_pixels);
1802 new_region(&screens[sidx], x, y, w, h);
1806 socket_setnonblock(int fd)
1810 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
1811 err(1, "fcntl F_GETFL");
1812 flags |= O_NONBLOCK;
1813 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
1814 err(1, "fcntl F_SETFL");
1818 bar_print_legacy(struct swm_region *r, const char *s)
1820 xcb_rectangle_t rect;
1825 XRectangle ibox, lbox;
1829 XmbTextExtents(bar_fs, s, len, &ibox, &lbox);
1831 switch (bar_justify) {
1832 case SWM_BAR_JUSTIFY_LEFT:
1835 case SWM_BAR_JUSTIFY_CENTER:
1836 x = (WIDTH(r) - lbox.width) / 2;
1838 case SWM_BAR_JUSTIFY_RIGHT:
1839 x = WIDTH(r) - lbox.width - SWM_BAR_OFFSET;
1843 if (x < SWM_BAR_OFFSET)
1848 rect.width = WIDTH(r->bar);
1849 rect.height = HEIGHT(r->bar);
1851 /* clear back buffer */
1852 gcv[0] = r->s->c[SWM_S_COLOR_BAR].pixel;
1853 xcb_change_gc(conn, r->s->bar_gc, XCB_GC_FOREGROUND, gcv);
1854 xcb_poly_fill_rectangle(conn, r->bar->buffer, r->s->bar_gc, 1, &rect);
1856 /* draw back buffer */
1857 gcvd.graphics_exposures = 0;
1858 draw = XCreateGC(display, r->bar->buffer, GCGraphicsExposures, &gcvd);
1859 XSetForeground(display, draw, r->s->c[SWM_S_COLOR_BAR_FONT].pixel);
1860 DRAWSTRING(display, r->bar->buffer, bar_fs, draw,
1861 x, (bar_fs_extents->max_logical_extent.height - lbox.height) / 2 -
1863 XFreeGC(display, draw);
1866 xcb_copy_area(conn, r->bar->buffer, r->bar->id, r->s->bar_gc, 0, 0,
1867 0, 0, WIDTH(r->bar), HEIGHT(r->bar));
1871 bar_print(struct swm_region *r, const char *s)
1874 xcb_rectangle_t rect;
1882 XftTextExtentsUtf8(display, bar_font, (FcChar8 *)s, len, &info);
1884 switch (bar_justify) {
1885 case SWM_BAR_JUSTIFY_LEFT:
1888 case SWM_BAR_JUSTIFY_CENTER:
1889 x = (WIDTH(r) - info.width) / 2;
1891 case SWM_BAR_JUSTIFY_RIGHT:
1892 x = WIDTH(r) - info.width - SWM_BAR_OFFSET;
1896 if (x < SWM_BAR_OFFSET)
1901 rect.width = WIDTH(r->bar);
1902 rect.height = HEIGHT(r->bar);
1904 /* clear back buffer */
1905 gcv[0] = r->s->c[SWM_S_COLOR_BAR].pixel;
1906 xcb_change_gc(conn, r->s->bar_gc, XCB_GC_FOREGROUND, gcv);
1907 xcb_poly_fill_rectangle(conn, r->bar->buffer, r->s->bar_gc, 1, &rect);
1909 /* draw back buffer */
1910 draw = XftDrawCreate(display, r->bar->buffer,
1911 DefaultVisual(display, r->s->idx),
1912 DefaultColormap(display, r->s->idx));
1914 XftDrawStringUtf8(draw, &bar_font_color, bar_font, x,
1915 (HEIGHT(r->bar) + bar_font->height) / 2 - bar_font->descent,
1918 XftDrawDestroy(draw);
1921 xcb_copy_area(conn, r->bar->buffer, r->bar->id, r->s->bar_gc, 0, 0,
1922 0, 0, WIDTH(r->bar), HEIGHT(r->bar));
1926 bar_extra_stop(void)
1930 bzero(bar_pipe, sizeof bar_pipe);
1933 kill(bar_pid, SIGTERM);
1936 strlcpy(bar_ext, "", sizeof bar_ext);
1941 bar_class_name(char *s, size_t sz, struct swm_region *r)
1943 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1945 if (r->ws->focus->ch.class_name != NULL)
1946 strlcat(s, r->ws->focus->ch.class_name, sz);
1950 bar_title_name(char *s, size_t sz, struct swm_region *r)
1952 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1954 if (r->ws->focus->ch.instance_name != NULL)
1955 strlcat(s, r->ws->focus->ch.instance_name, sz);
1959 bar_class_title_name(char *s, size_t sz, struct swm_region *r)
1961 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1964 bar_class_name(s, sz, r);
1965 strlcat(s, ":", sz);
1966 bar_title_name(s, sz, r);
1970 bar_window_float(char *s, size_t sz, struct swm_region *r)
1972 if (r == NULL || r ->ws == NULL || r->ws->focus == NULL)
1974 if (r->ws->focus->floating)
1975 strlcat(s, "(f)", sz);
1979 bar_window_name(char *s, size_t sz, struct swm_region *r)
1983 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1985 if ((title = get_win_name(r->ws->focus->id)) == NULL)
1988 strlcat(s, title, sz);
1992 int urgent[SWM_WS_MAX];
1994 bar_urgent(char *s, size_t sz)
1997 int i, j, num_screens;
1999 xcb_get_property_cookie_t c;
2000 xcb_icccm_wm_hints_t hints;
2002 for (i = 0; i < workspace_limit; i++)
2005 num_screens = get_screen_count();
2006 for (i = 0; i < num_screens; i++)
2007 for (j = 0; j < workspace_limit; j++)
2008 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry) {
2009 c = xcb_icccm_get_wm_hints(conn, win->id);
2010 if (xcb_icccm_get_wm_hints_reply(conn, c,
2013 if (hints.flags & XCB_ICCCM_WM_HINT_X_URGENCY)
2017 for (i = 0; i < workspace_limit; i++) {
2019 snprintf(b, sizeof b, "%d ", i + 1);
2021 snprintf(b, sizeof b, "- ");
2027 bar_workspace_name(char *s, size_t sz, struct swm_region *r)
2029 if (r == NULL || r->ws == NULL)
2031 if (r->ws->name != NULL)
2032 strlcat(s, r->ws->name, sz);
2035 /* build the default bar format according to the defined enabled options */
2037 bar_fmt(const char *fmtexp, char *fmtnew, struct swm_region *r, size_t sz)
2039 /* if format provided, just copy the buffers */
2040 if (bar_format != NULL) {
2041 strlcpy(fmtnew, fmtexp, sz);
2045 /* reset the output buffer */
2048 strlcat(fmtnew, "+N:+I ", sz);
2050 strlcat(fmtnew, "+S", sz);
2051 strlcat(fmtnew, " ", sz);
2053 /* only show the workspace name if there's actually one */
2054 if (r != NULL && r->ws != NULL && r->ws->name != NULL)
2055 strlcat(fmtnew, "<+D>", sz);
2056 strlcat(fmtnew, "+3<", sz);
2058 if (clock_enabled) {
2059 strlcat(fmtnew, fmtexp, sz);
2060 strlcat(fmtnew, "+4<", sz);
2063 /* bar_urgent already adds the space before the last asterisk */
2065 strlcat(fmtnew, "* +U*+4<", sz);
2067 if (title_class_enabled) {
2068 strlcat(fmtnew, "+C", sz);
2069 if (!title_name_enabled)
2070 strlcat(fmtnew, "+4<", sz);
2073 /* checks needed by the colon and floating strlcat(3) calls below */
2074 if (r != NULL && r->ws != NULL && r->ws->focus != NULL) {
2075 if (title_name_enabled) {
2076 if (title_class_enabled)
2077 strlcat(fmtnew, ":", sz);
2078 strlcat(fmtnew, "+T+4<", sz);
2080 if (window_name_enabled) {
2081 if (r->ws->focus->floating)
2082 strlcat(fmtnew, "+F ", sz);
2083 strlcat(fmtnew, "+64W ", sz);
2087 /* finally add the action script output and the version */
2088 strlcat(fmtnew, "+4<+A+4<+V", sz);
2092 bar_replace_pad(char *tmp, int *limit, size_t sz)
2094 /* special case; no limit given, pad one space, instead */
2095 if (*limit == (int)sz - 1)
2097 snprintf(tmp, sz, "%*s", *limit, " ");
2100 /* replaces the bar format character sequences (like in tmux(1)) */
2102 bar_replace_seq(char *fmt, char *fmtrep, struct swm_region *r, size_t *offrep,
2106 char tmp[SWM_BAR_MAX];
2110 /* reset strlcat(3) buffer */
2113 /* get number, if any */
2116 if (sscanf(fmt, "%d%n", &limit, &size) != 1)
2117 limit = sizeof tmp - 1;
2118 if (limit <= 0 || limit >= (int)sizeof tmp)
2119 limit = sizeof tmp - 1;
2121 /* there is nothing to replace (ie EOL) */
2128 bar_replace_pad(tmp, &limit, sizeof tmp);
2131 snprintf(tmp, sizeof tmp, "%s", bar_ext);
2134 bar_class_name(tmp, sizeof tmp, r);
2137 bar_workspace_name(tmp, sizeof tmp, r);
2140 bar_window_float(tmp, sizeof tmp, r);
2143 snprintf(tmp, sizeof tmp, "%d", r->ws->idx + 1);
2146 snprintf(tmp, sizeof tmp, "%d", r->s->idx + 1);
2149 bar_class_title_name(tmp, sizeof tmp, r);
2152 snprintf(tmp, sizeof tmp, "%s", r->ws->stacker);
2155 bar_title_name(tmp, sizeof tmp, r);
2158 bar_urgent(tmp, sizeof tmp);
2161 snprintf(tmp, sizeof tmp, "%s", bar_vertext);
2164 bar_window_name(tmp, sizeof tmp, r);
2167 /* unknown character sequence; copy as-is */
2168 snprintf(tmp, sizeof tmp, "+%c", *fmt);
2174 if ((int)len < limit)
2176 while (limit-- > 0) {
2177 if (*offrep >= sz - 1)
2179 fmtrep[(*offrep)++] = *ptr++;
2187 bar_replace(char *fmt, char *fmtrep, struct swm_region *r, size_t sz)
2192 while (*fmt != '\0') {
2194 /* skip ordinary characters */
2197 fmtrep[off++] = *fmt++;
2201 /* character sequence found; replace it */
2202 fmt = bar_replace_seq(fmt, fmtrep, r, &off, sz);
2211 bar_fmt_expand(char *fmtexp, size_t sz)
2218 /* start by grabbing the current time and date */
2220 localtime_r(&tmt, &tm);
2222 /* figure out what to expand */
2223 if (bar_format != NULL)
2225 else if (bar_format == NULL && clock_enabled)
2227 /* if nothing to expand bail out */
2233 /* copy as-is, just in case the format shouldn't be expanded below */
2234 strlcpy(fmtexp, fmt, sz);
2235 /* finally pass the string through strftime(3) */
2236 #ifndef SWM_DENY_CLOCK_FORMAT
2237 if ((len = strftime(fmtexp, sz, fmt, &tm)) == 0)
2238 warnx("format too long");
2243 /* Redraws the bar; need to follow with xcb_flush() or focus_flush(). */
2247 char fmtexp[SWM_BAR_MAX], fmtnew[SWM_BAR_MAX];
2248 char fmtrep[SWM_BAR_MAX];
2250 struct swm_region *r;
2252 /* expand the format by first passing it through strftime(3) */
2253 bar_fmt_expand(fmtexp, sizeof fmtexp);
2255 num_screens = get_screen_count();
2256 for (i = 0; i < num_screens; i++) {
2257 TAILQ_FOREACH(r, &screens[i].rl, entry) {
2261 if (bar_enabled && r->ws->bar_enabled)
2262 xcb_map_window(conn, r->bar->id);
2264 xcb_unmap_window(conn, r->bar->id);
2268 if (startup_exception)
2269 snprintf(fmtrep, sizeof fmtrep, "total "
2270 "exceptions: %d, first exception: %s",
2274 bar_fmt(fmtexp, fmtnew, r, sizeof fmtnew);
2275 bar_replace(fmtnew, fmtrep, r, sizeof fmtrep);
2277 if (bar_font_legacy)
2278 bar_print_legacy(r, fmtrep);
2280 bar_print(r, fmtrep);
2286 * Reads external script output; call when stdin is readable.
2287 * Returns 1 if bar_ext was updated; otherwise 0.
2290 bar_extra_update(void)
2293 char b[SWM_BAR_MAX];
2299 while (fgets(b, sizeof(b), stdin) != NULL) {
2302 if (b[len - 1] == '\n') {
2303 /* Remove newline. */
2306 /* "Clear" bar_ext. */
2309 /* Flush buffered output. */
2310 strlcpy(bar_ext, bar_ext_buf, sizeof(bar_ext));
2311 bar_ext_buf[0] = '\0';
2313 /* Append new output to bar. */
2314 strlcat(bar_ext, b, sizeof(bar_ext));
2318 /* Buffer output. */
2319 strlcat(bar_ext_buf, b, sizeof(bar_ext_buf));
2324 if (errno != EAGAIN) {
2325 warn("bar_action failed");
2334 bar_toggle(struct swm_region *r, union arg *args)
2336 struct swm_region *tmpr;
2339 /* suppress unused warnings since vars are needed */
2343 DNPRINTF(SWM_D_BAR, "bar_toggle\n");
2346 case SWM_ARG_ID_BAR_TOGGLE_WS:
2347 /* Only change if master switch is enabled. */
2349 r->ws->bar_enabled = !r->ws->bar_enabled;
2351 bar_enabled = r->ws->bar_enabled = 1;
2353 case SWM_ARG_ID_BAR_TOGGLE:
2354 bar_enabled = !bar_enabled;
2358 /* update bars as necessary */
2359 num_screens = get_screen_count();
2360 for (i = 0; i < num_screens; i++)
2361 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
2363 if (bar_enabled && tmpr->ws->bar_enabled)
2364 xcb_map_window(conn, tmpr->bar->id);
2366 xcb_unmap_window(conn, tmpr->bar->id);
2371 /* must be after stack */
2378 bar_extra_setup(void)
2380 /* do this here because the conf file is in memory */
2381 if (!bar_extra && bar_argv[0]) {
2382 /* launch external status app */
2384 if (pipe(bar_pipe) == -1)
2385 err(1, "pipe error");
2386 socket_setnonblock(bar_pipe[0]);
2387 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
2389 /* Set stdin to read from the pipe. */
2390 if (dup2(bar_pipe[0], STDIN_FILENO) == -1)
2393 /* Set stdout to write to the pipe. */
2394 if (dup2(bar_pipe[1], STDOUT_FILENO) == -1)
2397 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
2398 err(1, "could not disable SIGPIPE");
2399 switch (bar_pid = fork()) {
2401 err(1, "cannot fork");
2405 execvp(bar_argv[0], bar_argv);
2406 err(1, "%s external app failed", bar_argv[0]);
2408 default: /* parent */
2413 atexit(kill_bar_extra_atexit);
2418 kill_bar_extra_atexit(void)
2421 kill(bar_pid, SIGTERM);
2429 while ((s = index(s, '-'))) {
2434 return (count == 14);
2440 char *default_string;
2441 char **missing_charsets;
2442 int num_missing_charsets = 0;
2446 XFreeFontSet(display, bar_fs);
2450 DNPRINTF(SWM_D_INIT, "fontset_init: loading bar_fonts: %s\n", bar_fonts);
2452 bar_fs = XCreateFontSet(display, bar_fonts, &missing_charsets,
2453 &num_missing_charsets, &default_string);
2455 if (num_missing_charsets > 0) {
2456 warnx("Unable to load charset(s):");
2458 for (i = 0; i < num_missing_charsets; ++i)
2459 warnx("%s", missing_charsets[i]);
2461 XFreeStringList(missing_charsets);
2463 if (strcmp(default_string, ""))
2464 warnx("Glyphs from those sets will be replaced "
2465 "by '%s'.", default_string);
2467 warnx("Glyphs from those sets won't be drawn.");
2471 errx(1, "Error creating font set structure.");
2473 bar_fs_extents = XExtentsOfFontSet(bar_fs);
2475 bar_height = bar_fs_extents->max_logical_extent.height +
2476 2 * bar_border_width;
2483 xft_init(struct swm_region *r)
2485 char *font, *d, *search;
2488 if (bar_font == NULL) {
2489 if ((d = strdup(bar_fonts)) == NULL)
2490 errx(1, "insufficient memory.");
2492 while ((font = strsep(&search, ",")) != NULL) {
2496 DNPRINTF(SWM_D_INIT, "xft_init: try font %s\n", font);
2499 bar_font = XftFontOpenXlfd(display, r->s->idx,
2502 bar_font = XftFontOpenName(display, r->s->idx,
2507 warnx("unable to load font %s", font);
2510 DNPRINTF(SWM_D_INIT, "successfully opened "
2518 if (bar_font == NULL)
2519 errx(1, "unable to open a font");
2521 PIXEL_TO_XRENDERCOLOR(r->s->c[SWM_S_COLOR_BAR_FONT].pixel, color);
2523 if (!XftColorAllocValue(display, DefaultVisual(display, r->s->idx),
2524 DefaultColormap(display, r->s->idx), &color, &bar_font_color))
2525 warn("Xft error: unable to allocate color.");
2527 bar_height = bar_font->height + 2 * bar_border_width;
2534 bar_setup(struct swm_region *r)
2536 xcb_screen_t *screen;
2539 DNPRINTF(SWM_D_BAR, "bar_setup: screen %d.\n",
2542 if ((screen = get_screen(r->s->idx)) == NULL)
2543 errx(1, "ERROR: can't get screen %d.", r->s->idx);
2548 if ((r->bar = calloc(1, sizeof(struct swm_bar))) == NULL)
2549 err(1, "bar_setup: calloc: failed to allocate memory.");
2551 if (bar_font_legacy)
2557 Y(r->bar) = bar_at_bottom ? (Y(r) + HEIGHT(r) - bar_height) : Y(r);
2558 WIDTH(r->bar) = WIDTH(r) - 2 * bar_border_width;
2559 HEIGHT(r->bar) = bar_height - 2 * bar_border_width;
2561 /* Assume region is unfocused when we create the bar. */
2562 r->bar->id = xcb_generate_id(conn);
2563 wa[0] = r->s->c[SWM_S_COLOR_BAR].pixel;
2564 wa[1] = r->s->c[SWM_S_COLOR_BAR_BORDER_UNFOCUS].pixel;
2565 wa[2] = XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_POINTER_MOTION |
2566 XCB_EVENT_MASK_POINTER_MOTION_HINT;
2568 xcb_create_window(conn, XCB_COPY_FROM_PARENT, r->bar->id, r->s->root,
2569 X(r->bar), Y(r->bar), WIDTH(r->bar), HEIGHT(r->bar),
2570 bar_border_width, XCB_WINDOW_CLASS_INPUT_OUTPUT,
2571 XCB_COPY_FROM_PARENT, XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL
2572 | XCB_CW_EVENT_MASK, wa);
2574 r->bar->buffer = xcb_generate_id(conn);
2575 xcb_create_pixmap(conn, screen->root_depth, r->bar->buffer, r->bar->id,
2576 WIDTH(r->bar), HEIGHT(r->bar));
2579 xcb_randr_select_input(conn, r->bar->id,
2580 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE);
2583 xcb_map_window(conn, r->bar->id);
2585 DNPRINTF(SWM_D_BAR, "bar_setup: window: 0x%x, (x,y) w x h: (%d,%d) "
2586 "%d x %d\n", WINID(r->bar), X(r->bar), Y(r->bar), WIDTH(r->bar),
2593 bar_cleanup(struct swm_region *r)
2597 xcb_destroy_window(conn, r->bar->id);
2598 xcb_free_pixmap(conn, r->bar->buffer);
2604 set_win_state(struct ws_win *win, uint16_t state)
2606 uint16_t data[2] = { state, XCB_ATOM_NONE };
2608 DNPRINTF(SWM_D_EVENT, "set_win_state: window: 0x%x, state: %u\n",
2614 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id, a_state,
2615 a_state, 32, 2, data);
2619 getstate(xcb_window_t w)
2621 uint32_t result = 0;
2622 xcb_get_property_cookie_t c;
2623 xcb_get_property_reply_t *r;
2625 c = xcb_get_property(conn, 0, w, a_state, a_state, 0L, 2L);
2626 r = xcb_get_property_reply(conn, c, NULL);
2629 if (r->type == a_state && r->format == 32 && r->length == 2)
2630 result = *((uint32_t *)xcb_get_property_value(r));
2634 DNPRINTF(SWM_D_MISC, "getstate property: win 0x%x state %u\n", w,
2640 version(struct swm_region *r, union arg *args)
2642 /* suppress unused warnings since vars are needed */
2646 bar_version = !bar_version;
2648 snprintf(bar_vertext, sizeof bar_vertext,
2649 "Version: %s Build: %s", SPECTRWM_VERSION, buildstr);
2651 strlcpy(bar_vertext, "", sizeof bar_vertext);
2658 client_msg(struct ws_win *win, xcb_atom_t a, xcb_timestamp_t t)
2660 xcb_client_message_event_t ev;
2668 name = get_atom_name(a);
2669 DNPRINTF(SWM_D_EVENT, "client_msg: window: 0x%x, atom: %s(%u), "
2671 win->id, name, a, t);
2675 bzero(&ev, sizeof ev);
2676 ev.response_type = XCB_CLIENT_MESSAGE;
2677 ev.window = win->id;
2680 ev.data.data32[0] = a;
2681 ev.data.data32[1] = t;
2683 xcb_send_event(conn, 0, win->id,
2684 XCB_EVENT_MASK_NO_EVENT, (const char *)&ev);
2687 /* synthetic response to a ConfigureRequest when not making a change */
2689 config_win(struct ws_win *win, xcb_configure_request_event_t *ev)
2691 xcb_configure_notify_event_t ce;
2696 /* send notification of unchanged state. */
2697 bzero(&ce, sizeof(ce));
2698 ce.response_type = XCB_CONFIGURE_NOTIFY;
2701 ce.width = WIDTH(win);
2702 ce.height = HEIGHT(win);
2703 ce.override_redirect = 0;
2708 ce.window = win->id;
2709 ce.border_width = BORDER(win);
2710 ce.above_sibling = XCB_WINDOW_NONE;
2713 ce.event = ev->window;
2714 ce.window = ev->window;
2716 /* make response appear more WM_SIZE_HINTS-compliant */
2717 if (win->sh.flags) {
2718 DNPRINTF(SWM_D_MISC, "config_win: hints: window: 0x%x,"
2719 " sh.flags: %u, min: %d x %d, max: %d x %d, inc: "
2720 "%d x %d\n", win->id, win->sh.flags, SH_MIN_W(win),
2721 SH_MIN_H(win), SH_MAX_W(win), SH_MAX_H(win),
2722 SH_INC_W(win), SH_INC_H(win));
2727 /* the hint may be set... to 0! */
2728 if (SH_MIN_W(win) > 0 && ce.width < SH_MIN_W(win))
2729 ce.width = SH_MIN_W(win);
2730 if (SH_MIN_H(win) > 0 && ce.height < SH_MIN_H(win))
2731 ce.height = SH_MIN_H(win);
2736 /* may also be advertized as 0 */
2737 if (SH_MAX_W(win) > 0 && ce.width > SH_MAX_W(win))
2738 ce.width = SH_MAX_W(win);
2739 if (SH_MAX_H(win) > 0 && ce.height > SH_MAX_H(win))
2740 ce.height = SH_MAX_H(win);
2743 /* resize increment. */
2745 if (SH_INC_W(win) > 1 && ce.width > SH_INC_W(win))
2746 ce.width -= (ce.width - SH_MIN_W(win)) %
2748 if (SH_INC_H(win) > 1 && ce.height > SH_INC_H(win))
2749 ce.height -= (ce.height - SH_MIN_H(win)) %
2753 /* adjust x and y for requested border_width. */
2754 ce.x += BORDER(win) - ev->border_width;
2755 ce.y += BORDER(win) - ev->border_width;
2756 ce.border_width = ev->border_width;
2757 ce.above_sibling = ev->sibling;
2760 DNPRINTF(SWM_D_MISC, "config_win: ewmh: %s, window: 0x%x, (x,y) w x h: "
2761 "(%d,%d) %d x %d, border: %d\n", YESNO(ev == NULL), win->id, ce.x,
2762 ce.y, ce.width, ce.height, ce.border_width);
2764 xcb_send_event(conn, 0, win->id, XCB_EVENT_MASK_STRUCTURE_NOTIFY,
2769 count_win(struct workspace *ws, int count_transient)
2774 TAILQ_FOREACH(win, &ws->winlist, entry) {
2775 if (!count_transient && win->floating)
2777 if (!count_transient && win->transient)
2783 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
2789 quit(struct swm_region *r, union arg *args)
2791 /* suppress unused warnings since vars are needed */
2795 DNPRINTF(SWM_D_MISC, "quit\n");
2800 map_window(struct ws_win *win)
2802 uint32_t val = XCB_STACK_MODE_ABOVE;
2807 DNPRINTF(SWM_D_EVENT, "map_window: win 0x%x, mapped: %s\n", win->id,
2808 YESNO(win->mapped));
2810 xcb_configure_window(conn, win->id,
2811 XCB_CONFIG_WINDOW_STACK_MODE, &val);
2816 xcb_map_window(conn, win->id);
2817 set_win_state(win, XCB_ICCCM_WM_STATE_NORMAL);
2822 unmap_window(struct ws_win *win)
2827 DNPRINTF(SWM_D_EVENT, "unmap_window: win 0x%x, mapped: %s\n", win->id,
2828 YESNO(win->mapped));
2833 xcb_unmap_window(conn, win->id);
2834 set_win_state(win, XCB_ICCCM_WM_STATE_ICONIC);
2842 int i, j, num_screens;
2844 num_screens = get_screen_count();
2845 for (i = 0; i < num_screens; i++)
2846 for (j = 0; j < workspace_limit; j++)
2847 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2852 fake_keypress(struct ws_win *win, xcb_keysym_t keysym, uint16_t modifiers)
2854 xcb_key_press_event_t event;
2855 xcb_keycode_t *keycode;
2860 keycode = xcb_key_symbols_get_keycode(syms, keysym);
2862 DNPRINTF(SWM_D_MISC, "fake_keypress: win 0x%x keycode %u\n",
2865 bzero(&event, sizeof(event));
2866 event.event = win->id;
2867 event.root = win->s->root;
2868 event.child = XCB_WINDOW_NONE;
2869 event.time = XCB_CURRENT_TIME;
2870 event.event_x = X(win);
2871 event.event_y = Y(win);
2874 event.same_screen = 1;
2875 event.detail = *keycode;
2876 event.state = modifiers;
2878 event.response_type = XCB_KEY_PRESS;
2879 xcb_send_event(conn, 1, win->id,
2880 XCB_EVENT_MASK_KEY_PRESS, (const char *)&event);
2882 event.response_type = XCB_KEY_RELEASE;
2883 xcb_send_event(conn, 1, win->id,
2884 XCB_EVENT_MASK_KEY_RELEASE, (const char *)&event);
2890 restart(struct swm_region *r, union arg *args)
2892 /* suppress unused warning since var is needed */
2896 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
2900 execvp(start_argv[0], start_argv);
2901 warn("execvp failed");
2906 get_pointer_win(xcb_window_t root)
2908 struct ws_win *win = NULL;
2909 xcb_query_pointer_reply_t *r;
2911 DNPRINTF(SWM_D_EVENT, "get_pointer_win: root: 0x%x.\n", root);
2913 r = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, root), NULL);
2915 win = find_window(r->child);
2917 DNPRINTF(SWM_D_EVENT, "get_pointer_win: 0x%x.\n",
2920 DNPRINTF(SWM_D_EVENT, "get_pointer_win: none.\n");
2929 root_to_region(xcb_window_t root, int check)
2932 struct swm_region *r = NULL;
2934 xcb_query_pointer_reply_t *qpr;
2935 xcb_get_input_focus_reply_t *gifr;
2937 DNPRINTF(SWM_D_MISC, "root_to_region: window: 0x%x\n", root);
2939 num_screens = get_screen_count();
2940 for (i = 0; i < num_screens; i++)
2941 if (screens[i].root == root)
2944 if (check & SWM_CK_REGION)
2945 r = screens[i].r_focus;
2947 if (r == NULL && check & SWM_CK_FOCUS) {
2948 /* Try to find an actively focused window */
2949 gifr = xcb_get_input_focus_reply(conn,
2950 xcb_get_input_focus(conn), NULL);
2952 cfw = find_window(gifr->focus);
2953 if (cfw && cfw->ws->r)
2960 if (r == NULL && check & SWM_CK_POINTER) {
2961 /* No region with an active focus; try to use pointer. */
2962 qpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn,
2963 screens[i].root), NULL);
2966 DNPRINTF(SWM_D_MISC, "root_to_region: pointer: "
2967 "(%d,%d)\n", qpr->root_x, qpr->root_y);
2968 TAILQ_FOREACH(r, &screens[i].rl, entry)
2969 if (X(r) <= qpr->root_x &&
2970 qpr->root_x < MAX_X(r) &&
2971 Y(r) <= qpr->root_y &&
2972 qpr->root_y < MAX_Y(r))
2979 if (r == NULL && check & SWM_CK_FALLBACK)
2980 r = TAILQ_FIRST(&screens[i].rl);
2986 find_unmanaged_window(xcb_window_t id)
2989 int i, j, num_screens;
2991 num_screens = get_screen_count();
2992 for (i = 0; i < num_screens; i++)
2993 for (j = 0; j < workspace_limit; j++)
2994 TAILQ_FOREACH(win, &screens[i].ws[j].unmanagedlist,
3002 find_window(xcb_window_t id)
3005 int i, j, num_screens;
3006 xcb_query_tree_reply_t *r;
3008 num_screens = get_screen_count();
3009 for (i = 0; i < num_screens; i++)
3010 for (j = 0; j < workspace_limit; j++)
3011 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
3015 r = xcb_query_tree_reply(conn, xcb_query_tree(conn, id), NULL);
3019 /* if we were looking for the parent return that window instead */
3020 if (r->parent == 0 || r->root == r->parent) {
3025 /* look for parent */
3026 for (i = 0; i < num_screens; i++)
3027 for (j = 0; j < workspace_limit; j++)
3028 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
3029 if (r->parent == win->id) {
3039 spawn(int ws_idx, union arg *args, int close_fd)
3044 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
3046 close(xcb_get_file_descriptor(conn));
3048 setenv("LD_PRELOAD", SWM_LIB, 1);
3050 if (asprintf(&ret, "%d", ws_idx) == -1) {
3051 warn("spawn: asprintf SWM_WS");
3054 setenv("_SWM_WS", ret, 1);
3058 if (asprintf(&ret, "%d", getpid()) == -1) {
3059 warn("spawn: asprintf _SWM_PID");
3062 setenv("_SWM_PID", ret, 1);
3066 if (setsid() == -1) {
3067 warn("spawn: setsid");
3073 * close stdin and stdout to prevent interaction between apps
3074 * and the baraction script
3075 * leave stderr open to record errors
3077 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) == -1) {
3078 warn("spawn: open");
3081 dup2(fd, STDIN_FILENO);
3082 dup2(fd, STDOUT_FILENO);
3087 execvp(args->argv[0], args->argv);
3089 warn("spawn: execvp");
3094 kill_refs(struct ws_win *win)
3096 int i, x, num_screens;
3097 struct swm_region *r;
3098 struct workspace *ws;
3103 num_screens = get_screen_count();
3104 for (i = 0; i < num_screens; i++)
3105 TAILQ_FOREACH(r, &screens[i].rl, entry)
3106 for (x = 0; x < workspace_limit; x++) {
3108 if (win == ws->focus)
3110 if (win == ws->focus_prev)
3111 ws->focus_prev = NULL;
3116 validate_win(struct ws_win *testwin)
3119 struct workspace *ws;
3120 struct swm_region *r;
3121 int i, x, num_screens;
3123 if (testwin == NULL)
3126 num_screens = get_screen_count();
3127 for (i = 0; i < num_screens; i++)
3128 TAILQ_FOREACH(r, &screens[i].rl, entry)
3129 for (x = 0; x < workspace_limit; x++) {
3131 TAILQ_FOREACH(win, &ws->winlist, entry)
3139 validate_ws(struct workspace *testws)
3141 struct swm_region *r;
3142 struct workspace *ws;
3143 int i, x, num_screens;
3145 /* validate all ws */
3146 num_screens = get_screen_count();
3147 for (i = 0; i < num_screens; i++)
3148 TAILQ_FOREACH(r, &screens[i].rl, entry)
3149 for (x = 0; x < workspace_limit; x++) {
3158 unfocus_win(struct ws_win *win)
3160 xcb_window_t none = XCB_WINDOW_NONE;
3162 DNPRINTF(SWM_D_FOCUS, "unfocus_win: window: 0x%x\n", WINID(win));
3166 if (win->ws == NULL) {
3167 DNPRINTF(SWM_D_FOCUS, "unfocus_win: NULL ws.\n");
3171 if (validate_ws(win->ws)) {
3172 DNPRINTF(SWM_D_FOCUS, "unfocus_win: invalid ws.\n");
3176 if (win->ws->r == NULL) {
3177 DNPRINTF(SWM_D_FOCUS, "unfocus_win: NULL region.\n");
3181 if (validate_win(win)) {
3182 DNPRINTF(SWM_D_FOCUS, "unfocus_win: invalid win.\n");
3187 if (win->ws->focus == win) {
3188 win->ws->focus = NULL;
3189 win->ws->focus_prev = win;
3192 if (validate_win(win->ws->focus)) {
3193 kill_refs(win->ws->focus);
3194 win->ws->focus = NULL;
3197 if (validate_win(win->ws->focus_prev)) {
3198 kill_refs(win->ws->focus_prev);
3199 win->ws->focus_prev = NULL;
3202 xcb_change_window_attributes(conn, win->id, XCB_CW_BORDER_PIXEL,
3203 &win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].pixel);
3205 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
3206 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1, &none);
3208 DNPRINTF(SWM_D_FOCUS, "unfocus_win: done.\n");
3212 focus_win(struct ws_win *win)
3214 struct ws_win *cfw = NULL, *parent = NULL, *w;
3215 struct workspace *ws;
3216 xcb_get_input_focus_reply_t *r;
3218 DNPRINTF(SWM_D_FOCUS, "focus_win: window: 0x%x\n", WINID(win));
3223 if (win->ws == NULL)
3231 if (validate_ws(ws))
3234 if (validate_win(win)) {
3239 r = xcb_get_input_focus_reply(conn, xcb_get_input_focus(conn), NULL);
3241 cfw = find_window(r->focus);
3247 if (ws->focus != win) {
3248 if (ws->focus && ws->focus != cfw)
3249 unfocus_win(ws->focus);
3253 /* If this window directs focus to a child window, then clear. */
3254 if (win->focus_child)
3255 win->focus_child = NULL;
3257 /* If transient, adjust parent's focus child for focus_magic. */
3258 if (win->transient) {
3259 parent = find_window(win->transient);
3260 if (parent && parent->focus_child != win)
3261 parent->focus_child = win;
3264 if (cfw != win && ws->r != NULL) {
3265 /* Set input focus if no input hint, or indicated by hint. */
3266 if (!(win->hints.flags & XCB_ICCCM_WM_HINT_INPUT) ||
3267 (win->hints.flags & XCB_ICCCM_WM_HINT_INPUT &&
3269 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT,
3270 win->id, last_event_time);
3272 /* Tell app it can adjust focus to a specific window. */
3273 if (win->take_focus) {
3274 /* java is special; always tell parent */
3275 if (win->transient && win->java)
3276 client_msg(parent, a_takefocus,
3279 client_msg(win, a_takefocus, last_event_time);
3282 xcb_change_window_attributes(conn, win->id, XCB_CW_BORDER_PIXEL,
3283 &ws->r->s->c[SWM_S_COLOR_FOCUS].pixel);
3285 if (ws->cur_layout->flags & SWM_L_MAPONFOCUS ||
3287 /* If a parent exists, map it first. */
3291 /* Map siblings next. */
3292 TAILQ_FOREACH(w, &ws->winlist, entry)
3293 if (w != win && !w->iconic &&
3294 w->transient == parent->id)
3298 /* Map focused window. */
3301 /* Finally, map children of focus window. */
3302 TAILQ_FOREACH(w, &ws->winlist, entry)
3303 if (w->transient == win->id && !w->iconic)
3309 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
3310 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1,
3317 DNPRINTF(SWM_D_FOCUS, "focus_win: done.\n");
3320 /* If a child window should have focus instead, return it. */
3322 get_focus_magic(struct ws_win *win)
3324 struct ws_win *parent = NULL;
3325 struct ws_win *child = NULL;
3327 DNPRINTF(SWM_D_FOCUS, "get_focus_magic: window: 0x%x\n", WINID(win));
3331 if (win->transient) {
3332 parent = find_window(win->transient);
3334 /* If parent prefers focus elsewhere, then try to do so. */
3335 if (parent && (child = parent->focus_child)) {
3336 if (validate_win(child) == 0 && child->mapped)
3339 parent->focus_child = NULL;
3343 /* If this window prefers focus elsewhere, then try to do so. */
3344 if ((child = win->focus_child)) {
3345 if (validate_win(child) == 0 && child->mapped)
3348 win->focus_child = NULL;
3355 event_drain(uint8_t rt)
3357 xcb_generic_event_t *evt;
3359 /* ensure all pending requests have been processed before filtering. */
3361 while ((evt = xcb_poll_for_event(conn))) {
3362 if (XCB_EVENT_RESPONSE_TYPE(evt) != rt)
3370 set_region(struct swm_region *r)
3372 struct swm_region *rf;
3378 /* Unfocus old region bar. */
3383 xcb_change_window_attributes(conn, rf->bar->id,
3384 XCB_CW_BORDER_PIXEL,
3385 &r->s->c[SWM_S_COLOR_BAR_BORDER_UNFOCUS].pixel);
3388 /* Set region bar border to focus_color. */
3389 xcb_change_window_attributes(conn, r->bar->id,
3390 XCB_CW_BORDER_PIXEL, &r->s->c[SWM_S_COLOR_BAR_BORDER].pixel);
3396 focus_region(struct swm_region *r)
3399 struct swm_region *old_r;
3404 old_r = r->s->r_focus;
3407 nfw = get_region_focus(r);
3411 /* New region is empty; need to manually unfocus win. */
3413 unfocus_win(old_r->ws->focus);
3415 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT, r->s->root,
3418 /* Clear bar since empty. */
3424 switchws(struct swm_region *r, union arg *args)
3426 int wsid = args->id, unmap_old = 0;
3427 struct swm_region *this_r, *other_r;
3429 struct workspace *new_ws, *old_ws;
3434 if (wsid >= workspace_limit)
3438 old_ws = this_r->ws;
3439 new_ws = &this_r->s->ws[wsid];
3441 DNPRINTF(SWM_D_WS, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
3442 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), old_ws->idx, wsid);
3444 if (new_ws == NULL || old_ws == NULL)
3446 if (new_ws == old_ws)
3449 unfocus_win(old_ws->focus);
3451 other_r = new_ws->r;
3452 if (other_r == NULL) {
3453 /* the other workspace is hidden, hide this one */
3457 /* the other ws is visible in another region, exchange them */
3458 other_r->ws_prior = new_ws;
3459 other_r->ws = old_ws;
3460 old_ws->r = other_r;
3462 this_r->ws_prior = old_ws;
3463 this_r->ws = new_ws;
3466 /* Set focus_pending before stacking. */
3467 if (focus_mode != SWM_FOCUS_FOLLOW)
3468 new_ws->focus_pending = get_region_focus(new_ws->r);
3472 /* unmap old windows */
3474 TAILQ_FOREACH(win, &old_ws->winlist, entry)
3477 /* if workspaces were swapped, then don't wait to set focus */
3478 if (old_ws->r && focus_mode != SWM_FOCUS_FOLLOW) {
3479 if (new_ws->focus_pending) {
3480 focus_win(new_ws->focus_pending);
3482 /* Empty region, focus on root. */
3483 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT,
3484 new_ws->r->s[new_ws->r->s->idx].root,
3489 /* Clear bar if new ws is empty. */
3490 if (new_ws->focus_pending == NULL)
3495 DNPRINTF(SWM_D_WS, "switchws: done.\n");
3499 cyclews(struct swm_region *r, union arg *args)
3502 struct swm_screen *s = r->s;
3506 DNPRINTF(SWM_D_WS, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3507 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
3513 case SWM_ARG_ID_CYCLEWS_MOVE_UP:
3516 case SWM_ARG_ID_CYCLEWS_UP_ALL:
3519 case SWM_ARG_ID_CYCLEWS_UP:
3520 a.id = (a.id < workspace_limit - 1) ? a.id + 1 : 0;
3522 case SWM_ARG_ID_CYCLEWS_MOVE_DOWN:
3525 case SWM_ARG_ID_CYCLEWS_DOWN_ALL:
3528 case SWM_ARG_ID_CYCLEWS_DOWN:
3529 a.id = (a.id > 0) ? a.id - 1 : workspace_limit - 1;
3536 (!cycle_empty && TAILQ_EMPTY(&s->ws[a.id].winlist)))
3538 if (!cycle_visible && s->ws[a.id].r != NULL)
3545 } while (a.id != r->ws->idx);
3549 priorws(struct swm_region *r, union arg *args)
3555 DNPRINTF(SWM_D_WS, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3556 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
3558 if (r->ws_prior == NULL)
3561 a.id = r->ws_prior->idx;
3566 focusrg(struct swm_region *r, union arg *args)
3568 int ridx = args->id, i, num_screens;
3569 struct swm_region *rr = NULL;
3571 num_screens = get_screen_count();
3572 /* do nothing if we don't have more than one screen */
3573 if (!(num_screens > 1 || outputs > 1))
3576 DNPRINTF(SWM_D_FOCUS, "focusrg: id: %d\n", ridx);
3578 rr = TAILQ_FIRST(&r->s->rl);
3579 for (i = 0; i < ridx; ++i)
3580 rr = TAILQ_NEXT(rr, entry);
3590 cyclerg(struct swm_region *r, union arg *args)
3592 struct swm_region *rr = NULL;
3595 num_screens = get_screen_count();
3596 /* do nothing if we don't have more than one screen */
3597 if (!(num_screens > 1 || outputs > 1))
3602 case SWM_ARG_ID_CYCLERG_UP:
3603 rr = TAILQ_NEXT(r, entry);
3605 rr = TAILQ_FIRST(&screens[i].rl);
3607 case SWM_ARG_ID_CYCLERG_DOWN:
3608 rr = TAILQ_PREV(r, swm_region_list, entry);
3610 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
3623 sort_windows(struct ws_win_list *wl)
3625 struct ws_win *win, *parent, *nxt;
3630 for (win = TAILQ_FIRST(wl); win != TAILQ_END(wl); win = nxt) {
3631 nxt = TAILQ_NEXT(win, entry);
3632 if (win->transient) {
3633 parent = find_window(win->transient);
3634 if (parent == NULL) {
3635 warnx("not possible bug");
3638 TAILQ_REMOVE(wl, win, entry);
3639 TAILQ_INSERT_AFTER(wl, parent, win, entry);
3646 swapwin(struct swm_region *r, union arg *args)
3648 struct ws_win *target, *source;
3649 struct ws_win *cur_focus;
3650 struct ws_win_list *wl;
3652 DNPRINTF(SWM_D_WS, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3653 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
3655 cur_focus = r->ws->focus;
3656 if (cur_focus == NULL)
3660 wl = &source->ws->winlist;
3663 case SWM_ARG_ID_SWAPPREV:
3664 if (source->transient)
3665 source = find_window(source->transient);
3666 target = TAILQ_PREV(source, ws_win_list, entry);
3667 if (target && target->transient)
3668 target = find_window(target->transient);
3669 TAILQ_REMOVE(wl, source, entry);
3671 TAILQ_INSERT_TAIL(wl, source, entry);
3673 TAILQ_INSERT_BEFORE(target, source, entry);
3675 case SWM_ARG_ID_SWAPNEXT:
3676 target = TAILQ_NEXT(source, entry);
3677 /* move the parent and let the sort handle the move */
3678 if (source->transient)
3679 source = find_window(source->transient);
3680 TAILQ_REMOVE(wl, source, entry);
3682 TAILQ_INSERT_HEAD(wl, source, entry);
3684 TAILQ_INSERT_AFTER(wl, target, source, entry);
3686 case SWM_ARG_ID_SWAPMAIN:
3687 target = TAILQ_FIRST(wl);
3688 if (target == source) {
3689 if (source->ws->focus_prev != NULL &&
3690 source->ws->focus_prev != target)
3691 source = source->ws->focus_prev;
3695 if (target == NULL || source == NULL)
3697 source->ws->focus_prev = target;
3698 TAILQ_REMOVE(wl, target, entry);
3699 TAILQ_INSERT_BEFORE(source, target, entry);
3700 TAILQ_REMOVE(wl, source, entry);
3701 TAILQ_INSERT_HEAD(wl, source, entry);
3703 case SWM_ARG_ID_MOVELAST:
3704 TAILQ_REMOVE(wl, source, entry);
3705 TAILQ_INSERT_TAIL(wl, source, entry);
3708 DNPRINTF(SWM_D_MOVE, "swapwin: invalid id: %d\n", args->id);
3720 get_focus_prev(struct ws_win *win)
3722 struct ws_win *winfocus = NULL;
3723 struct ws_win *cur_focus = NULL;
3724 struct ws_win_list *wl = NULL;
3725 struct workspace *ws = NULL;
3727 if (!(win && win->ws))
3732 cur_focus = ws->focus;
3734 DNPRINTF(SWM_D_FOCUS, "get_focus_prev: window: 0x%x, cur_focus: 0x%x\n",
3735 WINID(win), WINID(cur_focus));
3737 /* pickle, just focus on whatever */
3738 if (cur_focus == NULL) {
3739 /* use prev_focus if valid */
3740 if (ws->focus_prev && ws->focus_prev != cur_focus &&
3741 find_window(WINID(ws->focus_prev)))
3742 winfocus = ws->focus_prev;
3746 /* if transient focus on parent */
3747 if (cur_focus->transient) {
3748 winfocus = find_window(cur_focus->transient);
3752 /* if in max_stack try harder */
3753 if ((win->quirks & SWM_Q_FOCUSPREV) ||
3754 (ws->cur_layout->flags & SWM_L_FOCUSPREV)) {
3755 if (cur_focus != ws->focus_prev)
3756 winfocus = ws->focus_prev;
3758 winfocus = TAILQ_PREV(win, ws_win_list, entry);
3763 DNPRINTF(SWM_D_FOCUS, "get_focus_prev: focus_close: %d\n", focus_close);
3765 if (winfocus == NULL || winfocus == win) {
3766 switch (focus_close) {
3767 case SWM_STACK_BOTTOM:
3768 TAILQ_FOREACH(winfocus, wl, entry)
3769 if (!winfocus->iconic && winfocus != cur_focus)
3773 TAILQ_FOREACH_REVERSE(winfocus, wl, ws_win_list, entry)
3774 if (!winfocus->iconic && winfocus != cur_focus)
3777 case SWM_STACK_ABOVE:
3778 winfocus = TAILQ_NEXT(cur_focus, entry);
3779 while (winfocus && winfocus->iconic)
3780 winfocus = TAILQ_NEXT(winfocus, entry);
3782 if (winfocus == NULL) {
3783 if (focus_close_wrap) {
3784 TAILQ_FOREACH(winfocus, wl, entry)
3785 if (!winfocus->iconic &&
3786 winfocus != cur_focus)
3789 TAILQ_FOREACH_REVERSE(winfocus, wl,
3791 if (!winfocus->iconic &&
3792 winfocus != cur_focus)
3797 case SWM_STACK_BELOW:
3798 winfocus = TAILQ_PREV(cur_focus, ws_win_list, entry);
3799 while (winfocus && winfocus->iconic)
3800 winfocus = TAILQ_PREV(winfocus, ws_win_list,
3803 if (winfocus == NULL) {
3804 if (focus_close_wrap) {
3805 TAILQ_FOREACH_REVERSE(winfocus, wl,
3807 if (!winfocus->iconic &&
3808 winfocus != cur_focus)
3811 TAILQ_FOREACH(winfocus, wl, entry)
3812 if (!winfocus->iconic &&
3813 winfocus != cur_focus)
3821 if (winfocus == NULL ||
3822 (winfocus && (winfocus->iconic || winfocus == cur_focus))) {
3823 if (focus_default == SWM_STACK_TOP) {
3824 TAILQ_FOREACH_REVERSE(winfocus, wl, ws_win_list, entry)
3825 if (!winfocus->iconic && winfocus != cur_focus)
3828 TAILQ_FOREACH(winfocus, wl, entry)
3829 if (!winfocus->iconic && winfocus != cur_focus)
3836 return get_focus_magic(winfocus);
3840 get_region_focus(struct swm_region *r)
3842 struct ws_win *winfocus = NULL;
3847 if (r->ws->focus && !r->ws->focus->iconic)
3848 winfocus = r->ws->focus;
3849 else if (r->ws->focus_prev && !r->ws->focus_prev->iconic)
3850 winfocus = r->ws->focus_prev;
3852 TAILQ_FOREACH(winfocus, &r->ws->winlist, entry)
3853 if (!winfocus->iconic)
3856 return get_focus_magic(winfocus);
3860 focus(struct swm_region *r, union arg *args)
3862 struct ws_win *head, *cur_focus = NULL, *winfocus = NULL;
3863 struct ws_win_list *wl = NULL;
3864 struct workspace *ws = NULL;
3870 DNPRINTF(SWM_D_FOCUS, "focus: id: %d\n", args->id);
3872 if ((cur_focus = r->ws->focus) == NULL)
3876 if (TAILQ_EMPTY(wl))
3878 /* make sure there is at least one uniconified window */
3880 TAILQ_FOREACH(winfocus, wl, entry)
3881 if (!winfocus->iconic) {
3889 case SWM_ARG_ID_FOCUSPREV:
3890 head = TAILQ_PREV(cur_focus, ws_win_list, entry);
3892 head = TAILQ_LAST(wl, ws_win_list);
3894 if (WINID(winfocus) == cur_focus->transient) {
3895 head = TAILQ_PREV(winfocus, ws_win_list, entry);
3897 head = TAILQ_LAST(wl, ws_win_list);
3902 if (winfocus && winfocus->iconic) {
3903 while (winfocus != cur_focus) {
3904 if (winfocus == NULL)
3905 winfocus = TAILQ_LAST(wl, ws_win_list);
3906 if (!winfocus->iconic)
3908 winfocus = TAILQ_PREV(winfocus, ws_win_list,
3914 case SWM_ARG_ID_FOCUSNEXT:
3915 head = TAILQ_NEXT(cur_focus, entry);
3917 head = TAILQ_FIRST(wl);
3921 if (winfocus && winfocus->iconic) {
3922 while (winfocus != cur_focus) {
3923 if (winfocus == NULL)
3924 winfocus = TAILQ_FIRST(wl);
3925 if (!winfocus->iconic)
3927 winfocus = TAILQ_NEXT(winfocus, entry);
3932 case SWM_ARG_ID_FOCUSMAIN:
3933 winfocus = TAILQ_FIRST(wl);
3934 if (winfocus == cur_focus)
3935 winfocus = cur_focus->ws->focus_prev;
3942 focus_win(get_focus_magic(winfocus));
3948 cycle_layout(struct swm_region *r, union arg *args)
3950 struct workspace *ws = r->ws;
3952 /* suppress unused warning since var is needed */
3955 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
3958 if (ws->cur_layout->l_stack == NULL)
3959 ws->cur_layout = &layouts[0];
3964 focus_win(get_region_focus(r));
3970 stack_config(struct swm_region *r, union arg *args)
3972 struct workspace *ws = r->ws;
3974 DNPRINTF(SWM_D_STACK, "stack_config: id: %d workspace: %d\n",
3977 if (ws->cur_layout->l_config != NULL)
3978 ws->cur_layout->l_config(ws, args->id);
3980 if (args->id != SWM_ARG_ID_STACKINIT)
3989 struct swm_geometry g;
3990 struct swm_region *r;
3996 DNPRINTF(SWM_D_STACK, "stack: begin\n");
3998 num_screens = get_screen_count();
3999 for (i = 0; i < num_screens; i++) {
4003 TAILQ_FOREACH(r, &screens[i].rl, entry) {
4004 DNPRINTF(SWM_D_STACK, "stack: workspace: %d "
4005 "(screen: %d, region: %d)\n", r->ws->idx, i, j++);
4007 /* Adjust stack area for region bar and padding. */
4009 g.x += region_padding;
4010 g.y += region_padding;
4011 g.w -= 2 * border_width + 2 * region_padding;
4012 g.h -= 2 * border_width + 2 * region_padding;
4013 if (bar_enabled && r->ws->bar_enabled) {
4018 r->ws->cur_layout->l_stack(r->ws, &g);
4019 r->ws->cur_layout->l_string(r->ws);
4020 /* save r so we can track region changes */
4027 DNPRINTF(SWM_D_STACK, "stack: end\n");
4031 store_float_geom(struct ws_win *win, struct swm_region *r)
4033 if (win == NULL || r == NULL)
4036 /* retain window geom and region geom */
4037 win->g_float = win->g;
4038 win->g_float.x -= X(r);
4039 win->g_float.y -= Y(r);
4040 win->g_floatvalid = 1;
4041 DNPRINTF(SWM_D_MISC, "store_float_geom: window: 0x%x, g: (%d,%d)"
4042 " %d x %d, g_float: (%d,%d) %d x %d\n", win->id, X(win), Y(win),
4043 WIDTH(win), HEIGHT(win), win->g_float.x, win->g_float.y,
4044 win->g_float.w, win->g_float.h);
4048 load_float_geom(struct ws_win *win, struct swm_region *r)
4050 if (win == NULL || r == NULL)
4053 if (win->g_floatvalid) {
4054 win->g = win->g_float;
4057 DNPRINTF(SWM_D_MISC, "load_float_geom: window: 0x%x, g: (%d,%d)"
4058 "%d x %d\n", win->id, X(win), Y(win), WIDTH(win),
4061 DNPRINTF(SWM_D_MISC, "load_float_geom: window: 0x%x, g_float "
4062 "is not set.\n", win->id);
4067 stack_floater(struct ws_win *win, struct swm_region *r)
4072 DNPRINTF(SWM_D_MISC, "stack_floater: window: 0x%x\n", win->id);
4075 * to allow windows to change their size (e.g. mplayer fs) only retrieve
4076 * geom on ws switches or return from max mode
4078 if (win->floatmaxed || (r != r->ws->old_r &&
4079 !(win->ewmh_flags & EWMH_F_FULLSCREEN))) {
4080 /* update geometry for the new region */
4081 load_float_geom(win, r);
4084 win->floatmaxed = 0;
4087 * if set to fullscreen mode, configure window to maximum size.
4089 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
4090 if (!win->g_floatvalid)
4091 store_float_geom(win, win->ws->r);
4097 * remove border on fullscreen floater when in fullscreen mode or when
4098 * the quirk is present.
4100 if ((win->ewmh_flags & EWMH_F_FULLSCREEN) ||
4101 ((win->quirks & SWM_Q_FULLSCREEN) &&
4102 (WIDTH(win) >= WIDTH(r)) && (HEIGHT(win) >= HEIGHT(r)))) {
4103 if (win->bordered) {
4105 X(win) += border_width;
4106 Y(win) += border_width;
4108 } else if (!win->bordered) {
4110 X(win) -= border_width;
4111 Y(win) -= border_width;
4114 if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
4115 WIDTH(win) = (double)WIDTH(r) * dialog_ratio;
4116 HEIGHT(win) = (double)HEIGHT(r) * dialog_ratio;
4119 if (!win->manual && !(win->ewmh_flags & EWMH_F_FULLSCREEN) &&
4120 !(win->quirks & SWM_Q_ANYWHERE)) {
4122 * floaters and transients are auto-centred unless moved,
4123 * resized or ANYWHERE quirk is set.
4125 X(win) = X(r) + (WIDTH(r) - WIDTH(win)) / 2 - BORDER(win);
4126 Y(win) = Y(r) + (HEIGHT(r) - HEIGHT(win)) / 2 - BORDER(win);
4128 store_float_geom(win, r);
4131 /* keep window within region bounds */
4132 constrain_window(win, r, 0);
4138 * Send keystrokes to terminal to decrease/increase the font size as the
4139 * window size changes.
4142 adjust_font(struct ws_win *win)
4144 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
4145 win->floating || win->transient)
4148 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
4149 WIDTH(win) / win->sh.width_inc < term_width &&
4150 win->font_steps < SWM_MAX_FONT_STEPS) {
4151 win->font_size_boundary[win->font_steps] =
4152 (win->sh.width_inc * term_width) + win->sh.base_width;
4155 win->last_inc = win->sh.width_inc;
4156 fake_keypress(win, XK_KP_Subtract, XCB_MOD_MASK_SHIFT);
4157 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
4158 WIDTH(win) > win->font_size_boundary[win->font_steps - 1]) {
4161 win->last_inc = win->sh.width_inc;
4162 fake_keypress(win, XK_KP_Add, XCB_MOD_MASK_SHIFT);
4166 #define SWAPXY(g) do { \
4168 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
4169 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
4172 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
4174 struct swm_geometry win_g, r_g = *g;
4175 struct ws_win *win, *fs_win = NULL;
4176 int i, j, s, stacks;
4177 int w_inc = 1, h_inc, w_base = 1, h_base;
4178 int hrh, extra = 0, h_slice, last_h = 0;
4179 int split, colno, winno, mwin, msize, mscale;
4180 int remain, missing, v_slice, reconfigure = 0;
4183 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d, rot: %s, "
4184 "flip: %s\n", ws->idx, YESNO(rot), YESNO(flip));
4186 winno = count_win(ws, 0);
4187 if (winno == 0 && count_win(ws, 1) == 0)
4190 TAILQ_FOREACH(win, &ws->winlist, entry)
4191 if (!win->transient && !win->floating && !win->iconic)
4198 w_inc = win->sh.width_inc;
4199 w_base = win->sh.base_width;
4200 mwin = ws->l_state.horizontal_mwin;
4201 mscale = ws->l_state.horizontal_msize;
4202 stacks = ws->l_state.horizontal_stacks;
4205 w_inc = win->sh.height_inc;
4206 w_base = win->sh.base_height;
4207 mwin = ws->l_state.vertical_mwin;
4208 mscale = ws->l_state.vertical_msize;
4209 stacks = ws->l_state.vertical_stacks;
4213 if (stacks > winno - mwin)
4214 stacks = winno - mwin;
4218 h_slice = r_g.h / SWM_H_SLICE;
4219 if (mwin && winno > mwin) {
4220 v_slice = r_g.w / SWM_V_SLICE;
4224 win_g.w = v_slice * mscale;
4226 if (w_inc > 1 && w_inc < v_slice) {
4227 /* adjust for window's requested size increment */
4228 remain = (win_g.w - w_base) % w_inc;
4234 win_g.x += r_g.w - msize;
4237 colno = split = winno / stacks;
4238 win_g.w = ((r_g.w - (stacks * 2 * border_width) +
4239 2 * border_width) / stacks);
4241 hrh = r_g.h / colno;
4242 extra = r_g.h - (colno * hrh);
4243 win_g.h = hrh - 2 * border_width;
4245 /* stack all the tiled windows */
4246 i = j = 0, s = stacks;
4247 TAILQ_FOREACH(win, &ws->winlist, entry) {
4248 if (win->transient || win->floating || win->iconic)
4251 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
4256 if (split && i == split) {
4257 colno = (winno - mwin) / stacks;
4258 if (s <= (winno - mwin) % stacks)
4260 split = split + colno;
4261 hrh = (r_g.h / colno);
4262 extra = r_g.h - (colno * hrh);
4266 win_g.x += win_g.w + 2 * border_width +
4268 win_g.w = (r_g.w - msize -
4269 (stacks * (2 * border_width + tile_gap))) / stacks;
4271 win_g.w += (r_g.w - msize -
4272 (stacks * (2 * border_width + tile_gap))) %
4278 win_g.h = hrh - 2 * border_width - tile_gap;
4281 h_inc = win->sh.width_inc;
4282 h_base = win->sh.base_width;
4284 h_inc = win->sh.height_inc;
4285 h_base = win->sh.base_height;
4288 if (j == colno - 1) {
4289 win_g.h = hrh + extra;
4290 } else if (h_inc > 1 && h_inc < h_slice) {
4291 /* adjust for window's requested size increment */
4292 remain = (win_g.h - h_base) % h_inc;
4293 missing = h_inc - remain;
4295 if (missing <= extra || j == 0) {
4307 win_g.y += last_h + 2 * border_width + tile_gap;
4309 if (disable_border && !(bar_enabled && ws->bar_enabled) &&
4312 win_g.w += 2 * border_width;
4313 win_g.h += 2 * border_width;
4319 if (X(win) != win_g.y || Y(win) != win_g.x ||
4320 WIDTH(win) != win_g.h || HEIGHT(win) != win_g.w) {
4324 WIDTH(win) = win_g.h;
4325 HEIGHT(win) = win_g.w;
4328 if (X(win) != win_g.x || Y(win) != win_g.y ||
4329 WIDTH(win) != win_g.w || HEIGHT(win) != win_g.h) {
4333 WIDTH(win) = win_g.w;
4334 HEIGHT(win) = win_g.h;
4338 if (bordered != win->bordered) {
4340 win->bordered = bordered;
4356 /* now, stack all the floaters and transients */
4357 TAILQ_FOREACH(win, &ws->winlist, entry) {
4358 if (!win->transient && !win->floating)
4362 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
4367 stack_floater(win, ws->r);
4371 /* Make sure fs_win is stacked last so it's on top. */
4373 stack_floater(fs_win, ws->r);
4379 vertical_config(struct workspace *ws, int id)
4381 DNPRINTF(SWM_D_STACK, "vertical_config: id: %d, workspace: %d\n",
4385 case SWM_ARG_ID_STACKRESET:
4386 case SWM_ARG_ID_STACKINIT:
4387 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
4388 ws->l_state.vertical_mwin = 1;
4389 ws->l_state.vertical_stacks = 1;
4391 case SWM_ARG_ID_MASTERSHRINK:
4392 if (ws->l_state.vertical_msize > 1)
4393 ws->l_state.vertical_msize--;
4395 case SWM_ARG_ID_MASTERGROW:
4396 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
4397 ws->l_state.vertical_msize++;
4399 case SWM_ARG_ID_MASTERADD:
4400 ws->l_state.vertical_mwin++;
4402 case SWM_ARG_ID_MASTERDEL:
4403 if (ws->l_state.vertical_mwin > 0)
4404 ws->l_state.vertical_mwin--;
4406 case SWM_ARG_ID_STACKINC:
4407 ws->l_state.vertical_stacks++;
4409 case SWM_ARG_ID_STACKDEC:
4410 if (ws->l_state.vertical_stacks > 1)
4411 ws->l_state.vertical_stacks--;
4413 case SWM_ARG_ID_FLIPLAYOUT:
4414 ws->l_state.vertical_flip = !ws->l_state.vertical_flip;
4422 vertical_stack(struct workspace *ws, struct swm_geometry *g)
4424 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
4426 stack_master(ws, g, 0, ws->l_state.vertical_flip);
4430 horizontal_config(struct workspace *ws, int id)
4432 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
4435 case SWM_ARG_ID_STACKRESET:
4436 case SWM_ARG_ID_STACKINIT:
4437 ws->l_state.horizontal_mwin = 1;
4438 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
4439 ws->l_state.horizontal_stacks = 1;
4441 case SWM_ARG_ID_MASTERSHRINK:
4442 if (ws->l_state.horizontal_msize > 1)
4443 ws->l_state.horizontal_msize--;
4445 case SWM_ARG_ID_MASTERGROW:
4446 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
4447 ws->l_state.horizontal_msize++;
4449 case SWM_ARG_ID_MASTERADD:
4450 ws->l_state.horizontal_mwin++;
4452 case SWM_ARG_ID_MASTERDEL:
4453 if (ws->l_state.horizontal_mwin > 0)
4454 ws->l_state.horizontal_mwin--;
4456 case SWM_ARG_ID_STACKINC:
4457 ws->l_state.horizontal_stacks++;
4459 case SWM_ARG_ID_STACKDEC:
4460 if (ws->l_state.horizontal_stacks > 1)
4461 ws->l_state.horizontal_stacks--;
4463 case SWM_ARG_ID_FLIPLAYOUT:
4464 ws->l_state.horizontal_flip = !ws->l_state.horizontal_flip;
4472 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
4474 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
4476 stack_master(ws, g, 1, ws->l_state.horizontal_flip);
4479 /* fullscreen view */
4481 max_stack(struct workspace *ws, struct swm_geometry *g)
4483 struct swm_geometry gg = *g;
4484 struct ws_win *w, *win = NULL, *parent = NULL;
4487 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
4492 winno = count_win(ws, 0);
4493 if (winno == 0 && count_win(ws, 1) == 0)
4496 /* Figure out which top level window should be visible. */
4497 if (ws->focus_pending)
4498 win = ws->focus_pending;
4502 win = TAILQ_FIRST(&ws->winlist);
4505 parent = find_window(win->transient);
4507 DNPRINTF(SWM_D_STACK, "max_stack: win: 0x%x\n", win->id);
4509 /* maximize all top level windows */
4510 TAILQ_FOREACH(w, &ws->winlist, entry) {
4511 if (w->transient || w->iconic)
4514 if (!w->mapped && w != win)
4517 if (w->floating && !w->floatmaxed) {
4519 * retain geometry for retrieval on exit from
4522 store_float_geom(w, ws->r);
4526 /* only reconfigure if necessary */
4527 if (X(w) != gg.x || Y(w) != gg.y || WIDTH(w) != gg.w ||
4528 HEIGHT(w) != gg.h) {
4530 if (bar_enabled && ws->bar_enabled){
4534 WIDTH(w) += 2 * border_width;
4535 HEIGHT(w) += 2 * border_width;
4542 /* If a parent exists, map/raise it first. */
4546 /* Map siblings next. */
4547 TAILQ_FOREACH(w, &ws->winlist, entry)
4548 if (w != win && !w->iconic &&
4549 w->transient == parent->id) {
4550 stack_floater(w, ws->r);
4555 /* Map/raise focused window. */
4558 /* Finally, map/raise children of focus window. */
4559 TAILQ_FOREACH(w, &ws->winlist, entry)
4560 if (w->transient == win->id && !w->iconic) {
4561 stack_floater(w, ws->r);
4567 send_to_rg(struct swm_region *r, union arg *args)
4569 int ridx = args->id, i, num_screens;
4570 struct swm_region *rr = NULL;
4573 num_screens = get_screen_count();
4574 /* do nothing if we don't have more than one screen */
4575 if (!(num_screens > 1 || outputs > 1))
4578 DNPRINTF(SWM_D_FOCUS, "send_to_rg: id: %d\n", ridx);
4580 rr = TAILQ_FIRST(&r->s->rl);
4581 for (i = 0; i < ridx; ++i)
4582 rr = TAILQ_NEXT(rr, entry);
4593 send_to_ws(struct swm_region *r, union arg *args)
4595 int wsid = args->id;
4596 struct ws_win *win = NULL, *parent;
4597 struct workspace *ws, *nws, *pws;
4598 char ws_idx_str[SWM_PROPLEN];
4600 if (wsid >= workspace_limit)
4603 if (r && r->ws && r->ws->focus)
4608 if (win->ws->idx == wsid)
4611 DNPRINTF(SWM_D_MOVE, "send_to_ws: win 0x%x, ws %d -> %d\n", win->id,
4612 win->ws->idx, wsid);
4615 nws = &win->s->ws[wsid];
4617 /* Update the window's workspace property: _SWM_WS */
4618 if (snprintf(ws_idx_str, SWM_PROPLEN, "%d", nws->idx) < SWM_PROPLEN) {
4619 if (focus_mode != SWM_FOCUS_FOLLOW)
4620 ws->focus_pending = get_focus_prev(win);
4622 /* Move the parent if this is a transient window. */
4623 if (win->transient) {
4624 parent = find_window(win->transient);
4627 /* Set new focus in parent's ws if needed. */
4628 if (pws->focus == parent) {
4629 if (focus_mode != SWM_FOCUS_FOLLOW)
4630 pws->focus_pending =
4631 get_focus_prev(parent);
4633 unfocus_win(parent);
4635 if (focus_mode != SWM_FOCUS_FOLLOW)
4636 pws->focus = pws->focus_pending;
4638 if (focus_mode != SWM_FOCUS_FOLLOW)
4639 pws->focus_pending = NULL;
4642 /* Don't unmap parent if new ws is visible */
4644 unmap_window(parent);
4647 TAILQ_REMOVE(&ws->winlist, parent, entry);
4648 TAILQ_INSERT_TAIL(&nws->winlist, parent, entry);
4651 DNPRINTF(SWM_D_PROP, "send_to_ws: set "
4652 "property: _SWM_WS: %s\n", ws_idx_str);
4653 xcb_change_property(conn, XCB_PROP_MODE_REPLACE,
4654 parent->id, a_swm_ws, XCB_ATOM_STRING, 8,
4655 strlen(ws_idx_str), ws_idx_str);
4661 /* Don't unmap if new ws is visible */
4666 TAILQ_REMOVE(&ws->winlist, win, entry);
4667 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
4670 /* Set focus on new ws. */
4671 unfocus_win(nws->focus);
4674 DNPRINTF(SWM_D_PROP, "send_to_ws: set property: _SWM_WS: %s\n",
4676 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
4677 a_swm_ws, XCB_ATOM_STRING, 8, strlen(ws_idx_str),
4680 /* Restack and set new focus. */
4683 if (focus_mode != SWM_FOCUS_FOLLOW) {
4684 focus_win(ws->focus_pending);
4685 ws->focus_pending = NULL;
4691 DNPRINTF(SWM_D_MOVE, "send_to_ws: done.\n");
4695 pressbutton(struct swm_region *r, union arg *args)
4697 /* suppress unused warning since var is needed */
4700 xcb_test_fake_input(conn, XCB_BUTTON_PRESS, args->id,
4701 XCB_CURRENT_TIME, XCB_WINDOW_NONE, 0, 0, 0);
4702 xcb_test_fake_input(conn, XCB_BUTTON_RELEASE, args->id,
4703 XCB_CURRENT_TIME, XCB_WINDOW_NONE, 0, 0, 0);
4707 raise_toggle(struct swm_region *r, union arg *args)
4709 /* suppress unused warning since var is needed */
4712 if (r == NULL || r->ws == NULL)
4715 r->ws->always_raise = !r->ws->always_raise;
4717 /* bring floaters back to top */
4718 if (!r->ws->always_raise)
4725 iconify(struct swm_region *r, union arg *args)
4727 /* suppress unused warning since var is needed */
4730 if (r->ws->focus == NULL)
4733 set_swm_iconic(r->ws->focus, 1);
4739 get_win_name(xcb_window_t win)
4742 xcb_get_property_cookie_t c;
4743 xcb_get_property_reply_t *r;
4745 /* First try _NET_WM_NAME for UTF-8. */
4746 c = xcb_get_property(conn, 0, win, a_netwmname,
4747 XCB_GET_PROPERTY_TYPE_ANY, 0, UINT_MAX);
4748 r = xcb_get_property_reply(conn, c, NULL);
4751 if (r->type == XCB_NONE) {
4753 /* Use WM_NAME instead; no UTF-8. */
4754 c = xcb_get_property(conn, 0, win, XCB_ATOM_WM_NAME,
4755 XCB_GET_PROPERTY_TYPE_ANY, 0, UINT_MAX);
4756 r = xcb_get_property_reply(conn, c, NULL);
4760 if (r->type == XCB_NONE) {
4766 name = strndup(xcb_get_property_value(r),
4767 xcb_get_property_value_length(r));
4776 uniconify(struct swm_region *r, union arg *args)
4783 DNPRINTF(SWM_D_MISC, "uniconify\n");
4785 if (r == NULL || r->ws == NULL)
4788 /* make sure we have anything to uniconify */
4789 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
4790 if (win->ws == NULL)
4791 continue; /* should never happen */
4800 search_resp_action = SWM_SEARCH_UNICONIFY;
4802 spawn_select(r, args, "search", &searchpid);
4804 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
4807 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
4808 if (win->ws == NULL)
4809 continue; /* should never happen */
4813 name = get_win_name(win->id);
4816 fprintf(lfile, "%s.%u\n", name, win->id);
4824 name_workspace(struct swm_region *r, union arg *args)
4828 DNPRINTF(SWM_D_MISC, "name_workspace\n");
4834 search_resp_action = SWM_SEARCH_NAME_WORKSPACE;
4836 spawn_select(r, args, "name_workspace", &searchpid);
4838 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
4841 fprintf(lfile, "%s", "");
4846 search_workspace(struct swm_region *r, union arg *args)
4849 struct workspace *ws;
4852 DNPRINTF(SWM_D_MISC, "search_workspace\n");
4858 search_resp_action = SWM_SEARCH_SEARCH_WORKSPACE;
4860 spawn_select(r, args, "search", &searchpid);
4862 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
4865 for (i = 0; i < workspace_limit; i++) {
4869 fprintf(lfile, "%d%s%s\n", ws->idx + 1,
4870 (ws->name ? ":" : ""), (ws->name ? ws->name : ""));
4877 search_win_cleanup(void)
4879 struct search_window *sw = NULL;
4881 while ((sw = TAILQ_FIRST(&search_wl)) != NULL) {
4882 xcb_destroy_window(conn, sw->indicator);
4883 TAILQ_REMOVE(&search_wl, sw, entry);
4889 search_win(struct swm_region *r, union arg *args)
4891 struct ws_win *win = NULL;
4892 struct search_window *sw = NULL;
4895 int i, width, height;
4903 XRectangle l_ibox, l_lbox;
4905 DNPRINTF(SWM_D_MISC, "search_win\n");
4908 search_resp_action = SWM_SEARCH_SEARCH_WINDOW;
4910 spawn_select(r, args, "search", &searchpid);
4912 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
4915 TAILQ_INIT(&search_wl);
4918 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
4922 sw = calloc(1, sizeof(struct search_window));
4924 warn("search_win: calloc");
4926 search_win_cleanup();
4932 snprintf(s, sizeof s, "%d", i);
4935 w = xcb_generate_id(conn);
4936 wa[0] = r->s->c[SWM_S_COLOR_FOCUS].pixel;
4937 wa[1] = r->s->c[SWM_S_COLOR_UNFOCUS].pixel;
4939 if (bar_font_legacy) {
4940 XmbTextExtents(bar_fs, s, len, &l_ibox, &l_lbox);
4941 width = l_lbox.width + 4;
4942 height = bar_fs_extents->max_logical_extent.height + 4;
4944 XftTextExtentsUtf8(display, bar_font, (FcChar8 *)s, len,
4946 width = info.width + 4;
4947 height = bar_font->height + 4;
4950 xcb_create_window(conn, XCB_COPY_FROM_PARENT, w, win->id, 0, 0,
4951 width, height, 1, XCB_WINDOW_CLASS_INPUT_OUTPUT,
4952 XCB_COPY_FROM_PARENT, XCB_CW_BACK_PIXEL |
4953 XCB_CW_BORDER_PIXEL, wa);
4955 xcb_map_window(conn, w);
4958 TAILQ_INSERT_TAIL(&search_wl, sw, entry);
4960 if (bar_font_legacy) {
4961 l_gcv.graphics_exposures = 0;
4962 l_draw = XCreateGC(display, w, 0, &l_gcv);
4964 XSetForeground(display, l_draw,
4965 r->s->c[SWM_S_COLOR_BAR].pixel);
4967 DRAWSTRING(display, w, bar_fs, l_draw, 2,
4968 (bar_fs_extents->max_logical_extent.height -
4969 l_lbox.height) / 2 - l_lbox.y, s, len);
4971 XFreeGC(display, l_draw);
4974 draw = XftDrawCreate(display, w,
4975 DefaultVisual(display, r->s->idx),
4976 DefaultColormap(display, r->s->idx));
4978 XftDrawStringUtf8(draw, &bar_font_color, bar_font, 2,
4979 (HEIGHT(r->bar) + bar_font->height) / 2 -
4980 bar_font->descent, (FcChar8 *)s, len);
4982 XftDrawDestroy(draw);
4985 DNPRINTF(SWM_D_MISC, "search_win: mapped window: 0x%x\n", w);
4987 fprintf(lfile, "%d\n", i);
4997 search_resp_uniconify(const char *resp, unsigned long len)
5003 DNPRINTF(SWM_D_MISC, "search_resp_uniconify: resp: %s\n", resp);
5005 TAILQ_FOREACH(win, &search_r->ws->winlist, entry) {
5008 name = get_win_name(win->id);
5011 if (asprintf(&s, "%s.%u", name, win->id) == -1) {
5016 if (strncmp(s, resp, len) == 0) {
5017 /* XXX this should be a callback to generalize */
5018 set_swm_iconic(win, 0);
5027 search_resp_name_workspace(const char *resp, unsigned long len)
5029 struct workspace *ws;
5031 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: resp: %s\n", resp);
5033 if (search_r->ws == NULL)
5038 free(search_r->ws->name);
5039 search_r->ws->name = NULL;
5043 ws->name = strdup(resp);
5044 if (ws->name == NULL) {
5045 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: "
5046 "strdup: %s", strerror(errno));
5053 search_resp_search_workspace(const char *resp)
5060 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: resp: %s\n", resp);
5064 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: strdup: %s",
5071 ws_idx = (int)strtonum(q, 1, workspace_limit, &errstr);
5073 DNPRINTF(SWM_D_MISC, "workspace idx is %s: %s",
5080 switchws(search_r, &a);
5084 search_resp_search_window(const char *resp)
5089 struct search_window *sw;
5091 DNPRINTF(SWM_D_MISC, "search_resp_search_window: resp: %s\n", resp);
5095 DNPRINTF(SWM_D_MISC, "search_resp_search_window: strdup: %s",
5100 idx = (int)strtonum(s, 1, INT_MAX, &errstr);
5102 DNPRINTF(SWM_D_MISC, "window idx is %s: %s",
5109 TAILQ_FOREACH(sw, &search_wl, entry)
5110 if (idx == sw->idx) {
5116 #define MAX_RESP_LEN 1024
5119 search_do_resp(void)
5125 DNPRINTF(SWM_D_MISC, "search_do_resp:\n");
5130 if ((resp = calloc(1, MAX_RESP_LEN + 1)) == NULL) {
5131 warn("search: calloc");
5135 rbytes = read(select_resp_pipe[0], resp, MAX_RESP_LEN);
5137 warn("search: read error");
5140 resp[rbytes] = '\0';
5143 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
5144 * newline, so work around that by sanitizing the resp now.
5146 resp[strcspn(resp, "\n")] = '\0';
5149 switch (search_resp_action) {
5150 case SWM_SEARCH_UNICONIFY:
5151 search_resp_uniconify(resp, len);
5153 case SWM_SEARCH_NAME_WORKSPACE:
5154 search_resp_name_workspace(resp, len);
5156 case SWM_SEARCH_SEARCH_WORKSPACE:
5157 search_resp_search_workspace(resp);
5159 case SWM_SEARCH_SEARCH_WINDOW:
5160 search_resp_search_window(resp);
5165 if (search_resp_action == SWM_SEARCH_SEARCH_WINDOW)
5166 search_win_cleanup();
5168 search_resp_action = SWM_SEARCH_NONE;
5169 close(select_resp_pipe[0]);
5176 wkill(struct swm_region *r, union arg *args)
5178 DNPRINTF(SWM_D_MISC, "wkill: id: %d\n", args->id);
5180 if (r->ws->focus == NULL)
5183 if (args->id == SWM_ARG_ID_KILLWINDOW)
5184 xcb_kill_client(conn, r->ws->focus->id);
5186 if (r->ws->focus->can_delete)
5187 client_msg(r->ws->focus, a_delete, 0);
5193 floating_toggle_win(struct ws_win *win)
5195 struct swm_region *r;
5205 /* reject floating toggles in max stack mode */
5206 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK])
5209 if (win->floating) {
5210 if (!win->floatmaxed) {
5211 /* retain position for refloat */
5212 store_float_geom(win, r);
5216 load_float_geom(win, r);
5220 ewmh_update_actions(win);
5226 floating_toggle(struct swm_region *r, union arg *args)
5228 struct ws_win *win = r->ws->focus;
5230 /* suppress unused warning since var is needed */
5236 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
5239 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
5240 _NET_WM_STATE_TOGGLE);
5244 if (win == win->ws->focus)
5251 constrain_window(struct ws_win *win, struct swm_region *r, int resizable)
5253 if (MAX_X(win) + BORDER(win) > MAX_X(r)) {
5255 WIDTH(win) = MAX_X(r) - X(win) - BORDER(win);
5257 X(win) = MAX_X(r)- WIDTH(win) - BORDER(win);
5260 if (X(win) + BORDER(win) < X(r)) {
5262 WIDTH(win) -= X(r) - X(win) - BORDER(win);
5264 X(win) = X(r) - BORDER(win);
5267 if (MAX_Y(win) + BORDER(win) > MAX_Y(r)) {
5269 HEIGHT(win) = MAX_Y(r) - Y(win) - BORDER(win);
5271 Y(win) = MAX_Y(r) - HEIGHT(win) - BORDER(win);
5274 if (Y(win) + BORDER(win) < Y(r)) {
5276 HEIGHT(win) -= Y(r) - Y(win) - BORDER(win);
5278 Y(win) = Y(r) - BORDER(win);
5284 if (HEIGHT(win) < 1)
5290 update_window(struct ws_win *win)
5295 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
5296 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
5297 XCB_CONFIG_WINDOW_BORDER_WIDTH;
5301 wc[3] = HEIGHT(win);
5302 wc[4] = BORDER(win);
5304 DNPRINTF(SWM_D_EVENT, "update_window: window: 0x%x, (x,y) w x h: "
5305 "(%d,%d) %d x %d, bordered: %s\n", win->id, wc[0], wc[1], wc[2],
5306 wc[3], YESNO(win->bordered));
5308 xcb_configure_window(conn, win->id, mask, wc);
5311 #define SWM_RESIZE_STEPS (50)
5314 resize(struct ws_win *win, union arg *args)
5316 xcb_timestamp_t timestamp = 0;
5317 struct swm_region *r = NULL;
5319 struct swm_geometry g;
5320 int top = 0, left = 0, resizing;
5322 xcb_cursor_t cursor;
5323 xcb_query_pointer_reply_t *xpr = NULL;
5324 xcb_generic_event_t *evt;
5325 xcb_motion_notify_event_t *mne;
5331 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
5334 DNPRINTF(SWM_D_EVENT, "resize: window: 0x%x, floating: %s, "
5335 "transient: 0x%x\n", win->id, YESNO(win->floating),
5338 if (!win->transient && !win->floating)
5341 /* reject resizes in max mode for floaters (transient ok) */
5342 if (win->floatmaxed)
5346 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
5353 /* It's possible for win to have been freed during focus_flush(). */
5354 if (validate_win(win)) {
5355 DNPRINTF(SWM_D_EVENT, "move: invalid win.\n");
5360 case SWM_ARG_ID_WIDTHSHRINK:
5361 WIDTH(win) -= SWM_RESIZE_STEPS;
5364 case SWM_ARG_ID_WIDTHGROW:
5365 WIDTH(win) += SWM_RESIZE_STEPS;
5368 case SWM_ARG_ID_HEIGHTSHRINK:
5369 HEIGHT(win) -= SWM_RESIZE_STEPS;
5372 case SWM_ARG_ID_HEIGHTGROW:
5373 HEIGHT(win) += SWM_RESIZE_STEPS;
5380 constrain_window(win, r, 1);
5382 store_float_geom(win,r);
5386 /* get cursor offset from window root */
5387 xpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, win->id),
5394 if (xpr->win_x < WIDTH(win) / 2)
5397 if (xpr->win_y < HEIGHT(win) / 2)
5400 if (args->id == SWM_ARG_ID_CENTER)
5401 cursor = cursors[XC_SIZING].cid;
5403 cursor = cursors[left ? XC_TOP_LEFT_CORNER :
5404 XC_TOP_RIGHT_CORNER].cid;
5406 cursor = cursors[left ? XC_BOTTOM_LEFT_CORNER :
5407 XC_BOTTOM_RIGHT_CORNER].cid;
5409 xcb_grab_pointer(conn, 0, win->id, MOUSEMASK,
5410 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, XCB_WINDOW_NONE, cursor,
5415 while (resizing && (evt = xcb_wait_for_event(conn))) {
5416 switch (XCB_EVENT_RESPONSE_TYPE(evt)) {
5417 case XCB_BUTTON_RELEASE:
5418 DNPRINTF(SWM_D_EVENT, "resize: BUTTON_RELEASE\n");
5421 case XCB_MOTION_NOTIFY:
5422 mne = (xcb_motion_notify_event_t *)evt;
5423 /* cursor offset/delta from start of the operation */
5424 dx = mne->root_x - xpr->root_x;
5425 dy = mne->root_y - xpr->root_y;
5431 if (args->id == SWM_ARG_ID_CENTER) {
5432 if (g.h / 2 + dy < 1)
5436 HEIGHT(win) = g.h + 2 * dy;
5444 HEIGHT(win) = g.h + dy;
5451 if (args->id == SWM_ARG_ID_CENTER) {
5452 if (g.w / 2 + dx < 1)
5456 WIDTH(win) = g.w + 2 * dx;
5464 WIDTH(win) = g.w + dx;
5467 constrain_window(win, r, 1);
5469 /* not free, don't sync more than 120 times / second */
5470 if ((mne->time - timestamp) > (1000 / 120) ) {
5471 timestamp = mne->time;
5479 /* It's possible for win to have been freed above. */
5480 if (validate_win(win)) {
5481 DNPRINTF(SWM_D_EVENT, "move: invalid win.\n");
5492 store_float_geom(win,r);
5494 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
5496 DNPRINTF(SWM_D_EVENT, "resize: done.\n");
5500 resize_step(struct swm_region *r, union arg *args)
5502 struct ws_win *win = NULL;
5504 if (r && r->ws && r->ws->focus)
5513 #define SWM_MOVE_STEPS (50)
5516 move(struct ws_win *win, union arg *args)
5518 xcb_timestamp_t timestamp = 0;
5519 int move_stp = 0, moving;
5520 struct swm_region *r = NULL;
5521 xcb_query_pointer_reply_t *qpr = NULL;
5522 xcb_generic_event_t *evt;
5523 xcb_motion_notify_event_t *mne;
5529 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
5532 DNPRINTF(SWM_D_EVENT, "move: window: 0x%x, floating: %s, transient: "
5533 "0x%x\n", win->id, YESNO(win->floating), win->transient);
5535 /* in max_stack mode should only move transients */
5536 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !win->transient)
5540 if (!win->floating && !win->transient) {
5541 store_float_geom(win, r);
5542 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
5545 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
5552 /* It's possible for win to have been freed during focus_flush(). */
5553 if (validate_win(win)) {
5554 DNPRINTF(SWM_D_EVENT, "move: invalid win.\n");
5560 case SWM_ARG_ID_MOVELEFT:
5561 X(win) -= (SWM_MOVE_STEPS - border_width);
5564 case SWM_ARG_ID_MOVERIGHT:
5565 X(win) += (SWM_MOVE_STEPS - border_width);
5568 case SWM_ARG_ID_MOVEUP:
5569 Y(win) -= (SWM_MOVE_STEPS - border_width);
5572 case SWM_ARG_ID_MOVEDOWN:
5573 Y(win) += (SWM_MOVE_STEPS - border_width);
5580 constrain_window(win, r, 0);
5582 store_float_geom(win, r);
5586 xcb_grab_pointer(conn, 0, win->id, MOUSEMASK,
5587 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC,
5588 XCB_WINDOW_NONE, cursors[XC_FLEUR].cid, XCB_CURRENT_TIME);
5590 /* get cursor offset from window root */
5591 qpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, win->id),
5594 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
5600 while (moving && (evt = xcb_wait_for_event(conn))) {
5601 switch (XCB_EVENT_RESPONSE_TYPE(evt)) {
5602 case XCB_BUTTON_RELEASE:
5603 DNPRINTF(SWM_D_EVENT, "move: BUTTON_RELEASE\n");
5606 case XCB_MOTION_NOTIFY:
5607 mne = (xcb_motion_notify_event_t *)evt;
5608 X(win) = mne->root_x - qpr->win_x - border_width;
5609 Y(win) = mne->root_y - qpr->win_y - border_width;
5611 constrain_window(win, r, 0);
5613 /* not free, don't sync more than 120 times / second */
5614 if ((mne->time - timestamp) > (1000 / 120) ) {
5615 timestamp = mne->time;
5623 /* It's possible for win to have been freed above. */
5624 if (validate_win(win)) {
5625 DNPRINTF(SWM_D_EVENT, "move: invalid win.\n");
5636 store_float_geom(win, r);
5639 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
5640 DNPRINTF(SWM_D_EVENT, "move: done.\n");
5644 move_step(struct swm_region *r, union arg *args)
5646 struct ws_win *win = NULL;
5648 if (r && r->ws && r->ws->focus)
5653 if (!win->transient && !win->floating)
5660 /* key definitions */
5662 char name[SWM_FUNCNAME_LEN];
5663 void (*func)(struct swm_region *r, union arg *);
5665 } keyfuncs[KF_INVALID + 1] = {
5666 /* name function argument */
5667 { "bar_toggle", bar_toggle, {.id = SWM_ARG_ID_BAR_TOGGLE} },
5668 { "bar_toggle_ws", bar_toggle, {.id = SWM_ARG_ID_BAR_TOGGLE_WS} },
5669 { "button2", pressbutton, {2} },
5670 { "cycle_layout", cycle_layout, {0} },
5671 { "flip_layout", stack_config, {.id = SWM_ARG_ID_FLIPLAYOUT} },
5672 { "float_toggle", floating_toggle,{0} },
5673 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
5674 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
5675 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
5676 { "height_grow", resize_step, {.id = SWM_ARG_ID_HEIGHTGROW} },
5677 { "height_shrink", resize_step, {.id = SWM_ARG_ID_HEIGHTSHRINK} },
5678 { "iconify", iconify, {0} },
5679 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
5680 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
5681 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
5682 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
5683 { "move_down", move_step, {.id = SWM_ARG_ID_MOVEDOWN} },
5684 { "move_left", move_step, {.id = SWM_ARG_ID_MOVELEFT} },
5685 { "move_right", move_step, {.id = SWM_ARG_ID_MOVERIGHT} },
5686 { "move_up", move_step, {.id = SWM_ARG_ID_MOVEUP} },
5687 { "mvrg_1", send_to_rg, {.id = 0} },
5688 { "mvrg_2", send_to_rg, {.id = 1} },
5689 { "mvrg_3", send_to_rg, {.id = 2} },
5690 { "mvrg_4", send_to_rg, {.id = 3} },
5691 { "mvrg_5", send_to_rg, {.id = 4} },
5692 { "mvrg_6", send_to_rg, {.id = 5} },
5693 { "mvrg_7", send_to_rg, {.id = 6} },
5694 { "mvrg_8", send_to_rg, {.id = 7} },
5695 { "mvrg_9", send_to_rg, {.id = 8} },
5696 { "mvws_1", send_to_ws, {.id = 0} },
5697 { "mvws_2", send_to_ws, {.id = 1} },
5698 { "mvws_3", send_to_ws, {.id = 2} },
5699 { "mvws_4", send_to_ws, {.id = 3} },
5700 { "mvws_5", send_to_ws, {.id = 4} },
5701 { "mvws_6", send_to_ws, {.id = 5} },
5702 { "mvws_7", send_to_ws, {.id = 6} },
5703 { "mvws_8", send_to_ws, {.id = 7} },
5704 { "mvws_9", send_to_ws, {.id = 8} },
5705 { "mvws_10", send_to_ws, {.id = 9} },
5706 { "mvws_11", send_to_ws, {.id = 10} },
5707 { "mvws_12", send_to_ws, {.id = 11} },
5708 { "mvws_13", send_to_ws, {.id = 12} },
5709 { "mvws_14", send_to_ws, {.id = 13} },
5710 { "mvws_15", send_to_ws, {.id = 14} },
5711 { "mvws_16", send_to_ws, {.id = 15} },
5712 { "mvws_17", send_to_ws, {.id = 16} },
5713 { "mvws_18", send_to_ws, {.id = 17} },
5714 { "mvws_19", send_to_ws, {.id = 18} },
5715 { "mvws_20", send_to_ws, {.id = 19} },
5716 { "mvws_21", send_to_ws, {.id = 20} },
5717 { "mvws_22", send_to_ws, {.id = 21} },
5718 { "name_workspace", name_workspace, {0} },
5719 { "quit", quit, {0} },
5720 { "raise_toggle", raise_toggle, {0} },
5721 { "restart", restart, {0} },
5722 { "rg_1", focusrg, {.id = 0} },
5723 { "rg_2", focusrg, {.id = 1} },
5724 { "rg_3", focusrg, {.id = 2} },
5725 { "rg_4", focusrg, {.id = 3} },
5726 { "rg_5", focusrg, {.id = 4} },
5727 { "rg_6", focusrg, {.id = 5} },
5728 { "rg_7", focusrg, {.id = 6} },
5729 { "rg_8", focusrg, {.id = 7} },
5730 { "rg_9", focusrg, {.id = 8} },
5731 { "rg_next", cyclerg, {.id = SWM_ARG_ID_CYCLERG_UP} },
5732 { "rg_prev", cyclerg, {.id = SWM_ARG_ID_CYCLERG_DOWN} },
5733 { "screen_next", cyclerg, {.id = SWM_ARG_ID_CYCLERG_UP} },
5734 { "screen_prev", cyclerg, {.id = SWM_ARG_ID_CYCLERG_DOWN} },
5735 { "search_win", search_win, {0} },
5736 { "search_workspace", search_workspace, {0} },
5737 { "spawn_custom", NULL, {0} },
5738 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
5739 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
5740 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
5741 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
5742 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
5743 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
5744 { "uniconify", uniconify, {0} },
5745 { "version", version, {0} },
5746 { "width_grow", resize_step, {.id = SWM_ARG_ID_WIDTHGROW} },
5747 { "width_shrink", resize_step, {.id = SWM_ARG_ID_WIDTHSHRINK} },
5748 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
5749 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
5750 { "ws_1", switchws, {.id = 0} },
5751 { "ws_2", switchws, {.id = 1} },
5752 { "ws_3", switchws, {.id = 2} },
5753 { "ws_4", switchws, {.id = 3} },
5754 { "ws_5", switchws, {.id = 4} },
5755 { "ws_6", switchws, {.id = 5} },
5756 { "ws_7", switchws, {.id = 6} },
5757 { "ws_8", switchws, {.id = 7} },
5758 { "ws_9", switchws, {.id = 8} },
5759 { "ws_10", switchws, {.id = 9} },
5760 { "ws_11", switchws, {.id = 10} },
5761 { "ws_12", switchws, {.id = 11} },
5762 { "ws_13", switchws, {.id = 12} },
5763 { "ws_14", switchws, {.id = 13} },
5764 { "ws_15", switchws, {.id = 14} },
5765 { "ws_16", switchws, {.id = 15} },
5766 { "ws_17", switchws, {.id = 16} },
5767 { "ws_18", switchws, {.id = 17} },
5768 { "ws_19", switchws, {.id = 18} },
5769 { "ws_20", switchws, {.id = 19} },
5770 { "ws_21", switchws, {.id = 20} },
5771 { "ws_22", switchws, {.id = 21} },
5772 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
5773 { "ws_next_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP_ALL} },
5774 { "ws_next_move", cyclews, {.id = SWM_ARG_ID_CYCLEWS_MOVE_UP} },
5775 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
5776 { "ws_prev_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN_ALL} },
5777 { "ws_prev_move", cyclews, {.id = SWM_ARG_ID_CYCLEWS_MOVE_DOWN} },
5778 { "ws_prior", priorws, {0} },
5779 { "dumpwins", dumpwins, {0} }, /* MUST BE LAST */
5780 { "invalid key func", NULL, {0} },
5784 key_cmp(struct key *kp1, struct key *kp2)
5786 if (kp1->keysym < kp2->keysym)
5788 if (kp1->keysym > kp2->keysym)
5791 if (kp1->mod < kp2->mod)
5793 if (kp1->mod > kp2->mod)
5800 enum { client_click, root_click };
5802 unsigned int action;
5804 unsigned int button;
5805 void (*func)(struct ws_win *, union arg *);
5808 #define MODKEY_SHIFT MODKEY | XCB_MOD_MASK_SHIFT
5809 /* action key mouse button func args */
5810 { client_click, MODKEY, XCB_BUTTON_INDEX_3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
5811 { client_click, MODKEY_SHIFT, XCB_BUTTON_INDEX_3, resize, {.id = SWM_ARG_ID_CENTER} },
5812 { client_click, MODKEY, XCB_BUTTON_INDEX_1, move, {0} },
5817 update_modkey(unsigned int mod)
5823 RB_FOREACH(kp, key_tree, &keys)
5824 if (kp->mod & XCB_MOD_MASK_SHIFT)
5825 kp->mod = mod | XCB_MOD_MASK_SHIFT;
5829 for (i = 0; i < LENGTH(buttons); i++)
5830 if (buttons[i].mask & XCB_MOD_MASK_SHIFT)
5831 buttons[i].mask = mod | XCB_MOD_MASK_SHIFT;
5833 buttons[i].mask = mod;
5837 spawn_expand(struct swm_region *r, union arg *args, const char *spawn_name,
5840 struct spawn_prog *prog = NULL;
5842 char *ap, **real_args;
5844 /* suppress unused warning since var is needed */
5847 DNPRINTF(SWM_D_SPAWN, "spawn_expand: %s\n", spawn_name);
5850 TAILQ_FOREACH(prog, &spawns, entry) {
5851 if (!strcasecmp(spawn_name, prog->name))
5855 warnx("spawn_custom: program %s not found", spawn_name);
5859 /* make room for expanded args */
5860 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
5861 err(1, "spawn_custom: calloc real_args");
5863 /* expand spawn_args into real_args */
5864 for (i = 0; i < prog->argc; i++) {
5866 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg: %s\n", ap);
5867 if (!strcasecmp(ap, "$bar_border")) {
5869 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
5871 err(1, "spawn_custom border color");
5872 } else if (!strcasecmp(ap, "$bar_color")) {
5874 strdup(r->s->c[SWM_S_COLOR_BAR].name))
5876 err(1, "spawn_custom bar color");
5877 } else if (!strcasecmp(ap, "$bar_font")) {
5878 if ((real_args[i] = strdup(bar_fonts))
5880 err(1, "spawn_custom bar fonts");
5881 } else if (!strcasecmp(ap, "$bar_font_color")) {
5883 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
5885 err(1, "spawn_custom color font");
5886 } else if (!strcasecmp(ap, "$color_focus")) {
5888 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
5890 err(1, "spawn_custom color focus");
5891 } else if (!strcasecmp(ap, "$color_unfocus")) {
5893 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
5895 err(1, "spawn_custom color unfocus");
5896 } else if (!strcasecmp(ap, "$region_index")) {
5897 if (asprintf(&real_args[i], "%d",
5898 get_region_index(r) + 1) < 1)
5899 err(1, "spawn_custom region index");
5900 } else if (!strcasecmp(ap, "$workspace_index")) {
5901 if (asprintf(&real_args[i], "%d", r->ws->idx + 1) < 1)
5902 err(1, "spawn_custom workspace index");
5904 /* no match --> copy as is */
5905 if ((real_args[i] = strdup(ap)) == NULL)
5906 err(1, "spawn_custom strdup(ap)");
5908 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg: %s\n",
5913 DNPRINTF(SWM_D_SPAWN, "spawn_custom: result: ");
5914 for (i = 0; i < prog->argc; i++)
5915 DNPRINTF(SWM_D_SPAWN, "\"%s\" ", real_args[i]);
5916 DNPRINTF(SWM_D_SPAWN, "\n");
5918 *ret_args = real_args;
5919 return (prog->argc);
5923 spawn_custom(struct swm_region *r, union arg *args, const char *spawn_name)
5929 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
5933 spawn(r->ws->idx, &a, 1);
5935 for (i = 0; i < spawn_argc; i++)
5941 spawn_select(struct swm_region *r, union arg *args, const char *spawn_name,
5948 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
5952 if (pipe(select_list_pipe) == -1)
5953 err(1, "pipe error");
5954 if (pipe(select_resp_pipe) == -1)
5955 err(1, "pipe error");
5957 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
5958 err(1, "could not disable SIGPIPE");
5959 switch (*pid = fork()) {
5961 err(1, "cannot fork");
5964 if (dup2(select_list_pipe[0], STDIN_FILENO) == -1)
5966 if (dup2(select_resp_pipe[1], STDOUT_FILENO) == -1)
5968 close(select_list_pipe[1]);
5969 close(select_resp_pipe[0]);
5970 spawn(r->ws->idx, &a, 0);
5972 default: /* parent */
5973 close(select_list_pipe[0]);
5974 close(select_resp_pipe[1]);
5978 for (i = 0; i < spawn_argc; i++)
5984 spawn_insert(const char *name, const char *args, int flags)
5986 char *arg, *cp, *ptr;
5987 struct spawn_prog *sp;
5989 DNPRINTF(SWM_D_SPAWN, "spawn_insert: %s\n", name);
5991 if ((sp = calloc(1, sizeof *sp)) == NULL)
5992 err(1, "spawn_insert: calloc");
5993 if ((sp->name = strdup(name)) == NULL)
5994 err(1, "spawn_insert: strdup");
5996 /* convert the arguments to an argument list */
5997 if ((ptr = cp = strdup(args)) == NULL)
5998 err(1, "spawn_insert: strdup");
5999 while ((arg = strsep(&ptr, " \t")) != NULL) {
6000 /* empty field; skip it */
6005 if ((sp->argv = realloc(sp->argv, sp->argc *
6006 sizeof *sp->argv)) == NULL)
6007 err(1, "spawn_insert: realloc");
6008 if ((sp->argv[sp->argc - 1] = strdup(arg)) == NULL)
6009 err(1, "spawn_insert: strdup");
6015 TAILQ_INSERT_TAIL(&spawns, sp, entry);
6016 DNPRINTF(SWM_D_SPAWN, "spawn_insert: leave\n");
6020 spawn_remove(struct spawn_prog *sp)
6024 DNPRINTF(SWM_D_SPAWN, "spawn_remove: %s\n", sp->name);
6026 TAILQ_REMOVE(&spawns, sp, entry);
6027 for (i = 0; i < sp->argc; i++)
6033 DNPRINTF(SWM_D_SPAWN, "spawn_remove: leave\n");
6037 setspawn(const char *name, const char *args, int flags)
6039 struct spawn_prog *sp;
6041 DNPRINTF(SWM_D_SPAWN, "setspawn: %s\n", name);
6046 /* Remove any old spawn under the same name. */
6047 TAILQ_FOREACH(sp, &spawns, entry)
6048 if (!strcmp(sp->name, name)) {
6054 spawn_insert(name, args, flags);
6056 warnx("error: setspawn: cannot find program: %s", name);
6058 DNPRINTF(SWM_D_SPAWN, "setspawn: leave\n");
6062 setconfspawn(char *selector, char *value, int flags)
6066 args = expand_tilde(value);
6068 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, args);
6070 setspawn(selector, args, flags);
6073 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done.\n");
6078 validate_spawns(void)
6080 struct spawn_prog *sp;
6081 char which[PATH_MAX];
6086 RB_FOREACH(kp, key_tree, &keys) {
6087 if (kp->funcid != KF_SPAWN_CUSTOM)
6091 TAILQ_FOREACH(sp, &spawns, entry) {
6092 if (!strcasecmp(kp->spawn_name, sp->name))
6096 if (sp == NULL || sp->flags & SWM_SPAWN_OPTIONAL)
6099 /* verify we have the goods */
6100 snprintf(which, sizeof which, "which %s", sp->argv[0]);
6101 DNPRINTF(SWM_D_CONF, "validate_spawns: which %s\n",
6103 for (i = strlen("which "); i < strlen(which); i++)
6104 if (which[i] == ' ') {
6108 if (system(which) != 0)
6109 add_startup_exception("could not find %s",
6110 &which[strlen("which ")]);
6117 setconfspawn("lock", "xlock", 0);
6119 setconfspawn("term", "xterm", 0);
6120 setconfspawn("spawn_term", "xterm", 0);
6122 setconfspawn("menu", "dmenu_run"
6125 " -nf $bar_font_color"
6127 " -sf $bar_color", 0);
6129 setconfspawn("search", "dmenu"
6133 " -nf $bar_font_color"
6135 " -sf $bar_color", 0);
6137 setconfspawn("name_workspace", "dmenu"
6141 " -nf $bar_font_color"
6143 " -sf $bar_color", 0);
6145 /* These are not verified for existence, even with a binding set. */
6146 setconfspawn("screenshot_all", "screenshot.sh full", SWM_SPAWN_OPTIONAL);
6147 setconfspawn("screenshot_wind", "screenshot.sh window", SWM_SPAWN_OPTIONAL);
6148 setconfspawn("initscr", "initscreen.sh", SWM_SPAWN_OPTIONAL);
6152 #define SWM_MODNAME_SIZE 32
6153 #define SWM_KEY_WS "\n+ \t"
6155 parsekeys(char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
6159 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
6160 if (mod == NULL || ks == NULL) {
6161 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
6164 if (keystr == NULL || strlen(keystr) == 0) {
6165 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
6169 *ks = XCB_NO_SYMBOL;
6171 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
6172 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
6174 cp += (long)strspn(cp, SWM_KEY_WS);
6175 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
6177 else if (!strncasecmp(name, "Mod1", SWM_MODNAME_SIZE))
6178 *mod |= XCB_MOD_MASK_1;
6179 else if (!strncasecmp(name, "Mod2", SWM_MODNAME_SIZE))
6180 *mod += XCB_MOD_MASK_2;
6181 else if (!strncmp(name, "Mod3", SWM_MODNAME_SIZE))
6182 *mod |= XCB_MOD_MASK_3;
6183 else if (!strncmp(name, "Mod4", SWM_MODNAME_SIZE))
6184 *mod |= XCB_MOD_MASK_4;
6185 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
6186 *mod |= XCB_MOD_MASK_SHIFT;
6187 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
6188 *mod |= XCB_MOD_MASK_CONTROL;
6190 *ks = XStringToKeysym(name);
6191 XConvertCase(*ks, ks, &uks);
6192 if (ks == XCB_NO_SYMBOL) {
6194 "parsekeys: invalid key %s\n",
6200 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
6205 strdupsafe(const char *str)
6210 return (strdup(str));
6214 key_insert(unsigned int mod, KeySym ks, enum keyfuncid kfid,
6215 const char *spawn_name)
6219 DNPRINTF(SWM_D_KEY, "key_insert: enter %s [%s]\n",
6220 keyfuncs[kfid].name, spawn_name);
6222 if ((kp = malloc(sizeof *kp)) == NULL)
6223 err(1, "key_insert: malloc");
6228 kp->spawn_name = strdupsafe(spawn_name);
6229 RB_INSERT(key_tree, &keys, kp);
6231 DNPRINTF(SWM_D_KEY, "key_insert: leave\n");
6235 key_lookup(unsigned int mod, KeySym ks)
6242 return (RB_FIND(key_tree, &keys, &kp));
6246 key_remove(struct key *kp)
6248 DNPRINTF(SWM_D_KEY, "key_remove: %s\n", keyfuncs[kp->funcid].name);
6250 RB_REMOVE(key_tree, &keys, kp);
6251 free(kp->spawn_name);
6254 DNPRINTF(SWM_D_KEY, "key_remove: leave\n");
6258 key_replace(struct key *kp, unsigned int mod, KeySym ks, enum keyfuncid kfid,
6259 const char *spawn_name)
6261 DNPRINTF(SWM_D_KEY, "key_replace: %s [%s]\n", keyfuncs[kp->funcid].name,
6265 key_insert(mod, ks, kfid, spawn_name);
6267 DNPRINTF(SWM_D_KEY, "key_replace: leave\n");
6271 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid,
6272 const char *spawn_name)
6276 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
6277 keyfuncs[kfid].name, spawn_name);
6279 if ((kp = key_lookup(mod, ks)) != NULL) {
6280 if (kfid == KF_INVALID)
6283 key_replace(kp, mod, ks, kfid, spawn_name);
6284 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
6287 if (kfid == KF_INVALID) {
6288 warnx("bind: Key combination already unbound.");
6289 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
6293 key_insert(mod, ks, kfid, spawn_name);
6294 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
6298 setconfbinding(char *selector, char *value, int flags)
6300 enum keyfuncid kfid;
6303 struct spawn_prog *sp;
6305 /* suppress unused warning since var is needed */
6308 DNPRINTF(SWM_D_KEY, "setconfbinding: enter\n");
6309 if (selector == NULL) {
6310 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
6311 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
6313 setkeybinding(mod, ks, kfid, NULL);
6318 /* search by key function name */
6319 for (kfid = 0; kfid < KF_INVALID; (kfid)++) {
6320 if (strncasecmp(selector, keyfuncs[kfid].name,
6321 SWM_FUNCNAME_LEN) == 0) {
6322 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match "
6323 "keyfunc\n", selector);
6324 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
6325 setkeybinding(mod, ks, kfid, NULL);
6331 /* search by custom spawn name */
6332 TAILQ_FOREACH(sp, &spawns, entry) {
6333 if (strcasecmp(selector, sp->name) == 0) {
6334 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match "
6335 "spawn\n", selector);
6336 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
6337 setkeybinding(mod, ks, KF_SPAWN_CUSTOM,
6344 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
6351 #define MODKEY_SHIFT MODKEY | XCB_MOD_MASK_SHIFT
6352 setkeybinding(MODKEY, XK_b, KF_BAR_TOGGLE, NULL);
6353 setkeybinding(MODKEY_SHIFT, XK_b, KF_BAR_TOGGLE_WS,NULL);
6354 setkeybinding(MODKEY, XK_v, KF_BUTTON2, NULL);
6355 setkeybinding(MODKEY, XK_space, KF_CYCLE_LAYOUT,NULL);
6356 setkeybinding(MODKEY_SHIFT, XK_backslash, KF_FLIP_LAYOUT, NULL);
6357 setkeybinding(MODKEY, XK_t, KF_FLOAT_TOGGLE,NULL);
6358 setkeybinding(MODKEY, XK_m, KF_FOCUS_MAIN, NULL);
6359 setkeybinding(MODKEY, XK_j, KF_FOCUS_NEXT, NULL);
6360 setkeybinding(MODKEY, XK_Tab, KF_FOCUS_NEXT, NULL);
6361 setkeybinding(MODKEY, XK_k, KF_FOCUS_PREV, NULL);
6362 setkeybinding(MODKEY_SHIFT, XK_Tab, KF_FOCUS_PREV, NULL);
6363 setkeybinding(MODKEY_SHIFT, XK_equal, KF_HEIGHT_GROW,NULL);
6364 setkeybinding(MODKEY_SHIFT, XK_minus, KF_HEIGHT_SHRINK,NULL);
6365 setkeybinding(MODKEY, XK_w, KF_ICONIFY, NULL);
6366 setkeybinding(MODKEY, XK_h, KF_MASTER_SHRINK, NULL);
6367 setkeybinding(MODKEY, XK_l, KF_MASTER_GROW, NULL);
6368 setkeybinding(MODKEY, XK_comma, KF_MASTER_ADD, NULL);
6369 setkeybinding(MODKEY, XK_period, KF_MASTER_DEL, NULL);
6370 setkeybinding(MODKEY_SHIFT, XK_bracketright,KF_MOVE_DOWN,NULL);
6371 setkeybinding(MODKEY, XK_bracketleft, KF_MOVE_LEFT,NULL);
6372 setkeybinding(MODKEY, XK_bracketright,KF_MOVE_RIGHT,NULL);
6373 setkeybinding(MODKEY_SHIFT, XK_bracketleft, KF_MOVE_UP, NULL);
6374 setkeybinding(MODKEY_SHIFT, XK_KP_End, KF_MVRG_1, NULL);
6375 setkeybinding(MODKEY_SHIFT, XK_KP_Down, KF_MVRG_2, NULL);
6376 setkeybinding(MODKEY_SHIFT, XK_KP_Next, KF_MVRG_3, NULL);
6377 setkeybinding(MODKEY_SHIFT, XK_KP_Left, KF_MVRG_4, NULL);
6378 setkeybinding(MODKEY_SHIFT, XK_KP_Begin, KF_MVRG_5, NULL);
6379 setkeybinding(MODKEY_SHIFT, XK_KP_Right, KF_MVRG_6, NULL);
6380 setkeybinding(MODKEY_SHIFT, XK_KP_Home, KF_MVRG_7, NULL);
6381 setkeybinding(MODKEY_SHIFT, XK_KP_Up, KF_MVRG_8, NULL);
6382 setkeybinding(MODKEY_SHIFT, XK_KP_Prior, KF_MVRG_9, NULL);
6383 setkeybinding(MODKEY_SHIFT, XK_1, KF_MVWS_1, NULL);
6384 setkeybinding(MODKEY_SHIFT, XK_2, KF_MVWS_2, NULL);
6385 setkeybinding(MODKEY_SHIFT, XK_3, KF_MVWS_3, NULL);
6386 setkeybinding(MODKEY_SHIFT, XK_4, KF_MVWS_4, NULL);
6387 setkeybinding(MODKEY_SHIFT, XK_5, KF_MVWS_5, NULL);
6388 setkeybinding(MODKEY_SHIFT, XK_6, KF_MVWS_6, NULL);
6389 setkeybinding(MODKEY_SHIFT, XK_7, KF_MVWS_7, NULL);
6390 setkeybinding(MODKEY_SHIFT, XK_8, KF_MVWS_8, NULL);
6391 setkeybinding(MODKEY_SHIFT, XK_9, KF_MVWS_9, NULL);
6392 setkeybinding(MODKEY_SHIFT, XK_0, KF_MVWS_10, NULL);
6393 setkeybinding(MODKEY_SHIFT, XK_F1, KF_MVWS_11, NULL);
6394 setkeybinding(MODKEY_SHIFT, XK_F2, KF_MVWS_12, NULL);
6395 setkeybinding(MODKEY_SHIFT, XK_F3, KF_MVWS_13, NULL);
6396 setkeybinding(MODKEY_SHIFT, XK_F4, KF_MVWS_14, NULL);
6397 setkeybinding(MODKEY_SHIFT, XK_F5, KF_MVWS_15, NULL);
6398 setkeybinding(MODKEY_SHIFT, XK_F6, KF_MVWS_16, NULL);
6399 setkeybinding(MODKEY_SHIFT, XK_F7, KF_MVWS_17, NULL);
6400 setkeybinding(MODKEY_SHIFT, XK_F8, KF_MVWS_18, NULL);
6401 setkeybinding(MODKEY_SHIFT, XK_F9, KF_MVWS_19, NULL);
6402 setkeybinding(MODKEY_SHIFT, XK_F10, KF_MVWS_20, NULL);
6403 setkeybinding(MODKEY_SHIFT, XK_F11, KF_MVWS_21, NULL);
6404 setkeybinding(MODKEY_SHIFT, XK_F12, KF_MVWS_22, NULL);
6405 setkeybinding(MODKEY_SHIFT, XK_slash, KF_NAME_WORKSPACE,NULL);
6406 setkeybinding(MODKEY_SHIFT, XK_q, KF_QUIT, NULL);
6407 setkeybinding(MODKEY_SHIFT, XK_r, KF_RAISE_TOGGLE,NULL);
6408 setkeybinding(MODKEY, XK_q, KF_RESTART, NULL);
6409 setkeybinding(MODKEY, XK_KP_End, KF_RG_1, NULL);
6410 setkeybinding(MODKEY, XK_KP_Down, KF_RG_2, NULL);
6411 setkeybinding(MODKEY, XK_KP_Next, KF_RG_3, NULL);
6412 setkeybinding(MODKEY, XK_KP_Left, KF_RG_4, NULL);
6413 setkeybinding(MODKEY, XK_KP_Begin, KF_RG_5, NULL);
6414 setkeybinding(MODKEY, XK_KP_Right, KF_RG_6, NULL);
6415 setkeybinding(MODKEY, XK_KP_Home, KF_RG_7, NULL);
6416 setkeybinding(MODKEY, XK_KP_Up, KF_RG_8, NULL);
6417 setkeybinding(MODKEY, XK_KP_Prior, KF_RG_9, NULL);
6418 setkeybinding(MODKEY_SHIFT, XK_Right, KF_RG_NEXT, NULL);
6419 setkeybinding(MODKEY_SHIFT, XK_Left, KF_RG_PREV, NULL);
6420 setkeybinding(MODKEY, XK_f, KF_SEARCH_WIN, NULL);
6421 setkeybinding(MODKEY, XK_slash, KF_SEARCH_WORKSPACE,NULL);
6422 setkeybinding(MODKEY_SHIFT, XK_i, KF_SPAWN_CUSTOM,"initscr");
6423 setkeybinding(MODKEY_SHIFT, XK_Delete, KF_SPAWN_CUSTOM,"lock");
6424 setkeybinding(MODKEY, XK_p, KF_SPAWN_CUSTOM,"menu");
6425 setkeybinding(MODKEY, XK_s, KF_SPAWN_CUSTOM,"screenshot_all");
6426 setkeybinding(MODKEY_SHIFT, XK_s, KF_SPAWN_CUSTOM,"screenshot_wind");
6427 setkeybinding(MODKEY_SHIFT, XK_Return, KF_SPAWN_CUSTOM,"term");
6428 setkeybinding(MODKEY_SHIFT, XK_comma, KF_STACK_INC, NULL);
6429 setkeybinding(MODKEY_SHIFT, XK_period, KF_STACK_DEC, NULL);
6430 setkeybinding(MODKEY_SHIFT, XK_space, KF_STACK_RESET, NULL);
6431 setkeybinding(MODKEY, XK_Return, KF_SWAP_MAIN, NULL);
6432 setkeybinding(MODKEY_SHIFT, XK_j, KF_SWAP_NEXT, NULL);
6433 setkeybinding(MODKEY_SHIFT, XK_k, KF_SWAP_PREV, NULL);
6434 setkeybinding(MODKEY_SHIFT, XK_w, KF_UNICONIFY, NULL);
6435 setkeybinding(MODKEY_SHIFT, XK_v, KF_VERSION, NULL);
6436 setkeybinding(MODKEY, XK_equal, KF_WIDTH_GROW, NULL);
6437 setkeybinding(MODKEY, XK_minus, KF_WIDTH_SHRINK,NULL);
6438 setkeybinding(MODKEY, XK_x, KF_WIND_DEL, NULL);
6439 setkeybinding(MODKEY_SHIFT, XK_x, KF_WIND_KILL, NULL);
6440 setkeybinding(MODKEY, XK_1, KF_WS_1, NULL);
6441 setkeybinding(MODKEY, XK_2, KF_WS_2, NULL);
6442 setkeybinding(MODKEY, XK_3, KF_WS_3, NULL);
6443 setkeybinding(MODKEY, XK_4, KF_WS_4, NULL);
6444 setkeybinding(MODKEY, XK_5, KF_WS_5, NULL);
6445 setkeybinding(MODKEY, XK_6, KF_WS_6, NULL);
6446 setkeybinding(MODKEY, XK_7, KF_WS_7, NULL);
6447 setkeybinding(MODKEY, XK_8, KF_WS_8, NULL);
6448 setkeybinding(MODKEY, XK_9, KF_WS_9, NULL);
6449 setkeybinding(MODKEY, XK_0, KF_WS_10, NULL);
6450 setkeybinding(MODKEY, XK_F1, KF_WS_11, NULL);
6451 setkeybinding(MODKEY, XK_F2, KF_WS_12, NULL);
6452 setkeybinding(MODKEY, XK_F3, KF_WS_13, NULL);
6453 setkeybinding(MODKEY, XK_F4, KF_WS_14, NULL);
6454 setkeybinding(MODKEY, XK_F5, KF_WS_15, NULL);
6455 setkeybinding(MODKEY, XK_F6, KF_WS_16, NULL);
6456 setkeybinding(MODKEY, XK_F7, KF_WS_17, NULL);
6457 setkeybinding(MODKEY, XK_F8, KF_WS_18, NULL);
6458 setkeybinding(MODKEY, XK_F9, KF_WS_19, NULL);
6459 setkeybinding(MODKEY, XK_F10, KF_WS_20, NULL);
6460 setkeybinding(MODKEY, XK_F11, KF_WS_21, NULL);
6461 setkeybinding(MODKEY, XK_F12, KF_WS_22, NULL);
6462 setkeybinding(MODKEY, XK_Right, KF_WS_NEXT, NULL);
6463 setkeybinding(MODKEY, XK_Left, KF_WS_PREV, NULL);
6464 setkeybinding(MODKEY, XK_Up, KF_WS_NEXT_ALL, NULL);
6465 setkeybinding(MODKEY, XK_Down, KF_WS_PREV_ALL, NULL);
6466 setkeybinding(MODKEY_SHIFT, XK_Up, KF_WS_NEXT_MOVE,NULL);
6467 setkeybinding(MODKEY_SHIFT, XK_Down, KF_WS_PREV_MOVE,NULL);
6468 setkeybinding(MODKEY, XK_a, KF_WS_PRIOR, NULL);
6470 setkeybinding(MODKEY_SHIFT, XK_d, KF_DUMPWINS, NULL);
6480 while (RB_EMPTY(&keys) == 0) {
6481 kp = RB_ROOT(&keys);
6487 setkeymapping(char *selector, char *value, int flags)
6489 char *keymapping_file;
6491 /* suppress unused warnings since vars are needed */
6495 DNPRINTF(SWM_D_KEY, "setkeymapping: enter\n");
6497 keymapping_file = expand_tilde(value);
6500 /* load new key bindings; if it fails, revert to default bindings */
6501 if (conf_load(keymapping_file, SWM_CONF_KEYMAPPING)) {
6506 free(keymapping_file);
6508 DNPRINTF(SWM_D_KEY, "setkeymapping: leave\n");
6513 updatenumlockmask(void)
6516 xcb_get_modifier_mapping_reply_t *modmap_r;
6517 xcb_keycode_t *modmap, kc, *keycode;
6521 modmap_r = xcb_get_modifier_mapping_reply(conn,
6522 xcb_get_modifier_mapping(conn),
6525 modmap = xcb_get_modifier_mapping_keycodes(modmap_r);
6526 for (i = 0; i < 8; i++) {
6527 for (j = 0; j < modmap_r->keycodes_per_modifier; j++) {
6528 kc = modmap[i * modmap_r->keycodes_per_modifier
6530 keycode = xcb_key_symbols_get_keycode(syms,
6533 numlockmask = (1 << i);
6539 DNPRINTF(SWM_D_MISC, "updatenumlockmask: %d\n", numlockmask);
6546 int num_screens, k, j;
6547 unsigned int modifiers[4];
6548 xcb_keycode_t *code;
6550 DNPRINTF(SWM_D_MISC, "grabkeys\n");
6551 updatenumlockmask();
6554 modifiers[1] = numlockmask;
6555 modifiers[2] = XCB_MOD_MASK_LOCK;
6556 modifiers[3] = numlockmask | XCB_MOD_MASK_LOCK;
6558 num_screens = get_screen_count();
6559 for (k = 0; k < num_screens; k++) {
6560 if (TAILQ_EMPTY(&screens[k].rl))
6562 xcb_ungrab_key(conn, XCB_GRAB_ANY, screens[k].root,
6564 RB_FOREACH(kp, key_tree, &keys) {
6565 /* Skip unused ws binds. */
6566 if ((int)kp->funcid > KF_WS_1 + workspace_limit - 1 &&
6567 kp->funcid <= KF_WS_22)
6570 /* Skip unused mvws binds. */
6571 if ((int)kp->funcid > KF_MVWS_1 + workspace_limit - 1 &&
6572 kp->funcid <= KF_MVWS_22)
6575 if ((code = xcb_key_symbols_get_keycode(syms,
6577 for (j = 0; j < LENGTH(modifiers); j++)
6578 xcb_grab_key(conn, 1,
6580 kp->mod | modifiers[j],
6581 *code, XCB_GRAB_MODE_SYNC,
6582 XCB_GRAB_MODE_SYNC);
6590 grabbuttons(struct ws_win *win)
6592 unsigned int modifiers[4];
6595 DNPRINTF(SWM_D_MOUSE, "grabbuttons: win 0x%x\n", win->id);
6596 updatenumlockmask();
6599 modifiers[1] = numlockmask;
6600 modifiers[2] = XCB_MOD_MASK_LOCK;
6601 modifiers[3] = numlockmask | XCB_MOD_MASK_LOCK;
6603 for (i = 0; i < LENGTH(buttons); i++)
6604 if (buttons[i].action == client_click)
6605 for (j = 0; j < LENGTH(modifiers); ++j)
6606 xcb_grab_button(conn, 0, win->id, BUTTONMASK,
6607 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC,
6608 XCB_WINDOW_NONE, XCB_CURSOR_NONE,
6609 buttons[i].button, buttons[i].mask |
6613 const char *quirkname[] = {
6614 "NONE", /* config string for "no value" */
6622 "FOCUSONMAP_SINGLE",
6625 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
6626 #define SWM_Q_WS "\n|+ \t"
6628 parsequirks(char *qstr, unsigned long *quirk)
6638 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
6640 cp += (long)strspn(cp, SWM_Q_WS);
6641 for (i = 0; i < LENGTH(quirkname); i++) {
6642 if (!strncasecmp(name, quirkname[i], SWM_QUIRK_LEN)) {
6643 DNPRINTF(SWM_D_QUIRK,
6644 "parsequirks: %s\n", name);
6649 *quirk |= 1 << (i-1);
6653 if (i >= LENGTH(quirkname)) {
6654 DNPRINTF(SWM_D_QUIRK,
6655 "parsequirks: invalid quirk [%s]\n", name);
6663 quirk_insert(const char *class, const char *name, unsigned long quirk)
6667 DNPRINTF(SWM_D_QUIRK, "quirk_insert: %s:%s [%lu]\n", class, name,
6670 if ((qp = malloc(sizeof *qp)) == NULL)
6671 err(1, "quirk_insert: malloc");
6672 if ((qp->class = strdup(class)) == NULL)
6673 err(1, "quirk_insert: strdup");
6674 if ((qp->name = strdup(name)) == NULL)
6675 err(1, "quirk_insert: strdup");
6678 TAILQ_INSERT_TAIL(&quirks, qp, entry);
6680 DNPRINTF(SWM_D_QUIRK, "quirk_insert: leave\n");
6684 quirk_remove(struct quirk *qp)
6686 DNPRINTF(SWM_D_QUIRK, "quirk_remove: %s:%s [%lu]\n", qp->class,
6687 qp->name, qp->quirk);
6689 TAILQ_REMOVE(&quirks, qp, entry);
6694 DNPRINTF(SWM_D_QUIRK, "quirk_remove: leave\n");
6698 quirk_replace(struct quirk *qp, const char *class, const char *name,
6699 unsigned long quirk)
6701 DNPRINTF(SWM_D_QUIRK, "quirk_replace: %s:%s [%lu]\n", qp->class,
6702 qp->name, qp->quirk);
6705 quirk_insert(class, name, quirk);
6707 DNPRINTF(SWM_D_QUIRK, "quirk_replace: leave\n");
6711 setquirk(const char *class, const char *name, unsigned long quirk)
6715 DNPRINTF(SWM_D_QUIRK, "setquirk: enter %s:%s [%lu]\n", class, name,
6718 /* Remove/replace existing quirk. */
6719 TAILQ_FOREACH(qp, &quirks, entry) {
6720 if (!strcmp(qp->class, class) && !strcmp(qp->name, name)) {
6724 quirk_replace(qp, class, name, quirk);
6725 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
6730 /* Only insert if quirk is not NONE. */
6732 quirk_insert(class, name, quirk);
6734 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
6738 setconfquirk(char *selector, char *value, int flags)
6740 char *cp, *class, *name;
6744 /* suppress unused warning since var is needed */
6747 if (selector == NULL)
6749 if ((cp = strchr(selector, ':')) == NULL)
6754 if ((retval = parsequirks(value, &qrks)) == 0)
6755 setquirk(class, name, qrks);
6762 setquirk("MPlayer", "xv", SWM_Q_FLOAT | SWM_Q_FULLSCREEN | SWM_Q_FOCUSPREV);
6763 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT);
6764 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ);
6765 setquirk("Firefox", "Dialog", SWM_Q_FLOAT);
6766 setquirk("Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
6767 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ);
6768 setquirk("xine", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
6769 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
6770 setquirk("xine", "xine Panel", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
6771 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
6772 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
6773 setquirk("pcb", "pcb", SWM_Q_FLOAT);
6774 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
6777 /* conf file stuff */
6778 #define SWM_CONF_FILE "spectrwm.conf"
6779 #define SWM_CONF_FILE_OLD "scrotwm.conf"
6783 SWM_S_BAR_AT_BOTTOM,
6784 SWM_S_BAR_BORDER_WIDTH,
6787 SWM_S_BAR_ENABLED_WS,
6792 SWM_S_CLOCK_ENABLED,
6795 SWM_S_CYCLE_VISIBLE,
6797 SWM_S_DISABLE_BORDER,
6799 SWM_S_FOCUS_CLOSE_WRAP,
6800 SWM_S_FOCUS_DEFAULT,
6802 SWM_S_REGION_PADDING,
6807 SWM_S_STACK_ENABLED,
6810 SWM_S_TITLE_CLASS_ENABLED,
6811 SWM_S_TITLE_NAME_ENABLED,
6812 SWM_S_URGENT_ENABLED,
6813 SWM_S_VERBOSE_LAYOUT,
6814 SWM_S_WINDOW_NAME_ENABLED,
6815 SWM_S_WORKSPACE_LIMIT
6819 setconfvalue(char *selector, char *value, int flags)
6821 struct workspace *ws;
6822 int i, ws_id, num_screens;
6825 /* suppress unused warning since var is needed */
6829 case SWM_S_BAR_ACTION:
6831 if ((bar_argv[0] = expand_tilde(value)) == NULL)
6832 err(1, "setconfvalue: bar_action");
6834 case SWM_S_BAR_AT_BOTTOM:
6835 bar_at_bottom = atoi(value);
6837 case SWM_S_BAR_BORDER_WIDTH:
6838 bar_border_width = atoi(value);
6839 if (bar_border_width < 0)
6840 bar_border_width = 0;
6842 case SWM_S_BAR_DELAY:
6843 /* No longer needed; leave to not break old conf files. */
6845 case SWM_S_BAR_ENABLED:
6846 bar_enabled = atoi(value);
6848 case SWM_S_BAR_ENABLED_WS:
6849 ws_id = atoi(selector) - 1;
6850 if (ws_id < 0 || ws_id >= workspace_limit)
6851 errx(1, "setconfvalue: bar_enabled_ws: invalid "
6852 "workspace %d.", ws_id + 1);
6854 num_screens = get_screen_count();
6855 for (i = 0; i < num_screens; i++) {
6856 ws = (struct workspace *)&screens[i].ws;
6857 ws[ws_id].bar_enabled = atoi(value);
6860 case SWM_S_BAR_FONT:
6862 if (asprintf(&bar_fonts, "%s,%s", value, bar_fonts) == -1)
6863 err(1, "setconfvalue: asprintf: failed to allocate "
6864 "memory for bar_fonts.");
6867 /* If already in xft mode, then we are done. */
6868 if (!bar_font_legacy)
6871 /* If there are any non-XLFD entries, switch to Xft mode. */
6872 while ((b = strsep(&value, ",")) != NULL) {
6876 bar_font_legacy = 0;
6881 case SWM_S_BAR_FORMAT:
6883 if ((bar_format = strdup(value)) == NULL)
6884 err(1, "setconfvalue: bar_format");
6886 case SWM_S_BAR_JUSTIFY:
6887 if (!strcmp(value, "left"))
6888 bar_justify = SWM_BAR_JUSTIFY_LEFT;
6889 else if (!strcmp(value, "center"))
6890 bar_justify = SWM_BAR_JUSTIFY_CENTER;
6891 else if (!strcmp(value, "right"))
6892 bar_justify = SWM_BAR_JUSTIFY_RIGHT;
6894 errx(1, "invalid bar_justify");
6896 case SWM_S_BORDER_WIDTH:
6897 border_width = atoi(value);
6898 if (border_width < 0)
6901 case SWM_S_CLOCK_ENABLED:
6902 clock_enabled = atoi(value);
6904 case SWM_S_CLOCK_FORMAT:
6905 #ifndef SWM_DENY_CLOCK_FORMAT
6907 if ((clock_format = strdup(value)) == NULL)
6908 err(1, "setconfvalue: clock_format");
6911 case SWM_S_CYCLE_EMPTY:
6912 cycle_empty = atoi(value);
6914 case SWM_S_CYCLE_VISIBLE:
6915 cycle_visible = atoi(value);
6917 case SWM_S_DIALOG_RATIO:
6918 dialog_ratio = atof(value);
6919 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
6922 case SWM_S_DISABLE_BORDER:
6923 disable_border = atoi(value);
6925 case SWM_S_FOCUS_CLOSE:
6926 if (!strcmp(value, "first"))
6927 focus_close = SWM_STACK_BOTTOM;
6928 else if (!strcmp(value, "last"))
6929 focus_close = SWM_STACK_TOP;
6930 else if (!strcmp(value, "next"))
6931 focus_close = SWM_STACK_ABOVE;
6932 else if (!strcmp(value, "previous"))
6933 focus_close = SWM_STACK_BELOW;
6935 errx(1, "focus_close");
6937 case SWM_S_FOCUS_CLOSE_WRAP:
6938 focus_close_wrap = atoi(value);
6940 case SWM_S_FOCUS_DEFAULT:
6941 if (!strcmp(value, "last"))
6942 focus_default = SWM_STACK_TOP;
6943 else if (!strcmp(value, "first"))
6944 focus_default = SWM_STACK_BOTTOM;
6946 errx(1, "focus_default");
6948 case SWM_S_FOCUS_MODE:
6949 if (!strcmp(value, "default"))
6950 focus_mode = SWM_FOCUS_DEFAULT;
6951 else if (!strcmp(value, "follow") ||
6952 !strcmp(value, "follow_cursor"))
6953 focus_mode = SWM_FOCUS_FOLLOW;
6954 else if (!strcmp(value, "manual"))
6955 focus_mode = SWM_FOCUS_MANUAL;
6957 errx(1, "focus_mode");
6959 case SWM_S_REGION_PADDING:
6960 region_padding = atoi(value);
6961 if (region_padding < 0)
6964 case SWM_S_SPAWN_ORDER:
6965 if (!strcmp(value, "first"))
6966 spawn_position = SWM_STACK_BOTTOM;
6967 else if (!strcmp(value, "last"))
6968 spawn_position = SWM_STACK_TOP;
6969 else if (!strcmp(value, "next"))
6970 spawn_position = SWM_STACK_ABOVE;
6971 else if (!strcmp(value, "previous"))
6972 spawn_position = SWM_STACK_BELOW;
6974 errx(1, "spawn_position");
6976 case SWM_S_SPAWN_TERM:
6977 setconfspawn("term", value, 0);
6978 setconfspawn("spawn_term", value, 0);
6981 /* No longer needed; leave to not break old conf files. */
6983 case SWM_S_SS_ENABLED:
6984 /* No longer needed; leave to not break old conf files. */
6986 case SWM_S_STACK_ENABLED:
6987 stack_enabled = atoi(value);
6989 case SWM_S_TERM_WIDTH:
6990 term_width = atoi(value);
6994 case SWM_S_TILE_GAP:
6995 tile_gap = atoi(value);
6999 case SWM_S_TITLE_CLASS_ENABLED:
7000 title_class_enabled = atoi(value);
7002 case SWM_S_TITLE_NAME_ENABLED:
7003 title_name_enabled = atoi(value);
7005 case SWM_S_URGENT_ENABLED:
7006 urgent_enabled = atoi(value);
7008 case SWM_S_VERBOSE_LAYOUT:
7009 verbose_layout = atoi(value);
7010 for (i = 0; layouts[i].l_stack != NULL; i++) {
7012 layouts[i].l_string = fancy_stacker;
7014 layouts[i].l_string = plain_stacker;
7017 case SWM_S_WINDOW_NAME_ENABLED:
7018 window_name_enabled = atoi(value);
7020 case SWM_S_WORKSPACE_LIMIT:
7021 workspace_limit = atoi(value);
7022 if (workspace_limit > SWM_WS_MAX)
7023 workspace_limit = SWM_WS_MAX;
7024 else if (workspace_limit < 1)
7025 workspace_limit = 1;
7034 setconfmodkey(char *selector, char *value, int flags)
7036 /* suppress unused warnings since vars are needed */
7040 if (!strncasecmp(value, "Mod1", strlen("Mod1")))
7041 update_modkey(XCB_MOD_MASK_1);
7042 else if (!strncasecmp(value, "Mod2", strlen("Mod2")))
7043 update_modkey(XCB_MOD_MASK_2);
7044 else if (!strncasecmp(value, "Mod3", strlen("Mod3")))
7045 update_modkey(XCB_MOD_MASK_3);
7046 else if (!strncasecmp(value, "Mod4", strlen("Mod4")))
7047 update_modkey(XCB_MOD_MASK_4);
7054 setconfcolor(char *selector, char *value, int flags)
7056 setscreencolor(value, ((selector == NULL)?-1:atoi(selector)), flags);
7061 setconfregion(char *selector, char *value, int flags)
7063 /* suppress unused warnings since vars are needed */
7067 custom_region(value);
7072 setautorun(char *selector, char *value, int flags)
7082 /* suppress unused warnings since vars are needed */
7086 if (getenv("SWM_STARTED"))
7090 if (sscanf(value, "ws[%d]:%1023c", &ws_id, s) != 2)
7091 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
7093 if (ws_id < 0 || ws_id >= workspace_limit)
7094 errx(1, "autorun: invalid workspace %d", ws_id + 1);
7096 sp = expand_tilde((char *)&s);
7099 * This is a little intricate
7101 * If the pid already exists we simply reuse it because it means it was
7102 * used before AND not claimed by manage_window. We get away with
7103 * altering it in the parent after INSERT because this can not be a race
7106 while ((ap = strsep(&sp, " \t")) != NULL) {
7109 DNPRINTF(SWM_D_SPAWN, "setautorun: arg [%s]\n", ap);
7111 if ((a.argv = realloc(a.argv, argc * sizeof(char *))) == NULL)
7112 err(1, "setautorun: realloc");
7113 a.argv[argc - 1] = ap;
7117 if ((a.argv = realloc(a.argv, (argc + 1) * sizeof(char *))) == NULL)
7118 err(1, "setautorun: realloc");
7119 a.argv[argc] = NULL;
7121 if ((pid = fork()) == 0) {
7122 spawn(ws_id, &a, 1);
7131 p = calloc(1, sizeof *p);
7134 TAILQ_INSERT_TAIL(&pidlist, p, entry);
7144 setlayout(char *selector, char *value, int flags)
7146 int ws_id, i, x, mg, ma, si, ar, f = 0;
7147 int st = SWM_V_STACK, num_screens;
7149 struct workspace *ws;
7151 /* suppress unused warnings since vars are needed */
7155 if (getenv("SWM_STARTED"))
7159 if (sscanf(value, "ws[%d]:%d:%d:%d:%d:%1023c",
7160 &ws_id, &mg, &ma, &si, &ar, s) != 6)
7161 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
7162 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
7165 if (ws_id < 0 || ws_id >= workspace_limit)
7166 errx(1, "layout: invalid workspace %d", ws_id + 1);
7168 if (!strcasecmp(s, "vertical"))
7170 else if (!strcasecmp(s, "vertical_flip")) {
7173 } else if (!strcasecmp(s, "horizontal"))
7175 else if (!strcasecmp(s, "horizontal_flip")) {
7178 } else if (!strcasecmp(s, "fullscreen"))
7181 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
7182 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
7185 num_screens = get_screen_count();
7186 for (i = 0; i < num_screens; i++) {
7187 ws = (struct workspace *)&screens[i].ws;
7188 ws[ws_id].cur_layout = &layouts[st];
7190 ws[ws_id].always_raise = ar;
7191 if (st == SWM_MAX_STACK)
7195 for (x = 0; x < abs(mg); x++) {
7196 ws[ws_id].cur_layout->l_config(&ws[ws_id],
7197 mg >= 0 ? SWM_ARG_ID_MASTERGROW :
7198 SWM_ARG_ID_MASTERSHRINK);
7202 for (x = 0; x < abs(ma); x++) {
7203 ws[ws_id].cur_layout->l_config(&ws[ws_id],
7204 ma >= 0 ? SWM_ARG_ID_MASTERADD :
7205 SWM_ARG_ID_MASTERDEL);
7209 for (x = 0; x < abs(si); x++) {
7210 ws[ws_id].cur_layout->l_config(&ws[ws_id],
7211 si >= 0 ? SWM_ARG_ID_STACKINC :
7212 SWM_ARG_ID_STACKDEC);
7217 ws[ws_id].cur_layout->l_config(&ws[ws_id],
7218 SWM_ARG_ID_FLIPLAYOUT);
7228 /* config options */
7229 struct config_option {
7231 int (*func)(char*, char*, int);
7234 struct config_option configopt[] = {
7235 { "autorun", setautorun, 0 },
7236 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
7237 { "bar_at_bottom", setconfvalue, SWM_S_BAR_AT_BOTTOM },
7238 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
7239 { "bar_border_unfocus", setconfcolor, SWM_S_COLOR_BAR_BORDER_UNFOCUS },
7240 { "bar_border_width", setconfvalue, SWM_S_BAR_BORDER_WIDTH },
7241 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
7242 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
7243 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
7244 { "bar_enabled_ws", setconfvalue, SWM_S_BAR_ENABLED_WS },
7245 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
7246 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
7247 { "bar_format", setconfvalue, SWM_S_BAR_FORMAT },
7248 { "bar_justify", setconfvalue, SWM_S_BAR_JUSTIFY },
7249 { "bind", setconfbinding, 0 },
7250 { "border_width", setconfvalue, SWM_S_BORDER_WIDTH },
7251 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
7252 { "clock_format", setconfvalue, SWM_S_CLOCK_FORMAT },
7253 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
7254 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
7255 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
7256 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
7257 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
7258 { "disable_border", setconfvalue, SWM_S_DISABLE_BORDER },
7259 { "focus_close", setconfvalue, SWM_S_FOCUS_CLOSE },
7260 { "focus_close_wrap", setconfvalue, SWM_S_FOCUS_CLOSE_WRAP },
7261 { "focus_default", setconfvalue, SWM_S_FOCUS_DEFAULT },
7262 { "focus_mode", setconfvalue, SWM_S_FOCUS_MODE },
7263 { "keyboard_mapping", setkeymapping, 0 },
7264 { "layout", setlayout, 0 },
7265 { "modkey", setconfmodkey, 0 },
7266 { "program", setconfspawn, 0 },
7267 { "quirk", setconfquirk, 0 },
7268 { "region", setconfregion, 0 },
7269 { "region_padding", setconfvalue, SWM_S_REGION_PADDING },
7270 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
7271 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
7272 { "spawn_position", setconfvalue, SWM_S_SPAWN_ORDER },
7273 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
7274 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
7275 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
7276 { "tile_gap", setconfvalue, SWM_S_TILE_GAP },
7277 { "title_class_enabled", setconfvalue, SWM_S_TITLE_CLASS_ENABLED },
7278 { "title_name_enabled", setconfvalue, SWM_S_TITLE_NAME_ENABLED },
7279 { "urgent_enabled", setconfvalue, SWM_S_URGENT_ENABLED },
7280 { "verbose_layout", setconfvalue, SWM_S_VERBOSE_LAYOUT },
7281 { "window_name_enabled", setconfvalue, SWM_S_WINDOW_NAME_ENABLED },
7282 { "workspace_limit", setconfvalue, SWM_S_WORKSPACE_LIMIT },
7286 _add_startup_exception(const char *fmt, va_list ap)
7288 if (vasprintf(&startup_exception, fmt, ap) == -1)
7289 warn("%s: asprintf", __func__);
7293 add_startup_exception(const char *fmt, ...)
7299 if (startup_exception)
7302 /* force bar to be enabled due to exception */
7306 _add_startup_exception(fmt, ap);
7311 conf_load(const char *filename, int keymapping)
7314 char *line = NULL, *cp, *optsub, *optval = NULL;
7315 size_t linelen, lineno = 0;
7316 int wordlen, i, optidx;
7317 struct config_option *opt = NULL;
7319 DNPRINTF(SWM_D_CONF, "conf_load: begin\n");
7321 if (filename == NULL) {
7322 warnx("conf_load: no filename");
7326 DNPRINTF(SWM_D_CONF, "conf_load: open %s\n", filename);
7328 if ((config = fopen(filename, "r")) == NULL) {
7329 warn("conf_load: fopen: %s", filename);
7333 while (!feof(config)) {
7337 if ((line = fparseln(config, &linelen, &lineno, NULL,
7338 FPARSELN_UNESCCOMM | FPARSELN_UNESCCONT)) == NULL) {
7340 err(1, "%s", filename);
7345 cp += strspn(cp, " \t\n"); /* eat whitespace */
7346 if (cp[0] == '\0') {
7350 /* get config option */
7351 wordlen = strcspn(cp, "=[ \t\n");
7353 add_startup_exception("%s: line %zd: no option found",
7358 for (i = 0; i < LENGTH(configopt); i++) {
7359 opt = &configopt[i];
7360 if (!strncasecmp(cp, opt->optname, wordlen) &&
7361 (int)strlen(opt->optname) == wordlen) {
7367 add_startup_exception("%s: line %zd: unknown option "
7368 "%.*s", filename, lineno, wordlen, cp);
7371 if (keymapping && opt && strcmp(opt->optname, "bind")) {
7372 add_startup_exception("%s: line %zd: invalid option "
7373 "%.*s", filename, lineno, wordlen, cp);
7377 cp += strspn(cp, " \t\n"); /* eat whitespace */
7379 /* from here on out we call goto invalid to continue */
7381 /* get [selector] if any */
7385 wordlen = strcspn(cp, "]");
7388 add_startup_exception("%s: line %zd: "
7389 "syntax error", filename, lineno);
7393 if (asprintf(&optsub, "%.*s", wordlen, cp) ==
7395 add_startup_exception("%s: line %zd: "
7396 "unable to allocatememory for "
7397 "selector", filename, lineno);
7402 cp += strspn(cp, "] \t\n"); /* eat trailing */
7404 cp += strspn(cp, "= \t\n"); /* eat trailing */
7406 optval = strdup(cp);
7407 if (strlen(optval) == 0) {
7408 add_startup_exception("%s: line %zd: must supply value "
7409 "to %s", filename, lineno,
7410 configopt[optidx].optname);
7413 /* call function to deal with it all */
7414 if (configopt[optidx].func(optsub, optval,
7415 configopt[optidx].funcflags) != 0) {
7416 add_startup_exception("%s: line %zd: invalid data for "
7417 "%s", filename, lineno, configopt[optidx].optname);
7434 DNPRINTF(SWM_D_CONF, "conf_load: end\n");
7440 set_child_transient(struct ws_win *win, xcb_window_t *trans)
7442 struct ws_win *parent, *w;
7443 struct swm_region *r;
7444 struct workspace *ws;
7445 xcb_icccm_wm_hints_t wmh;
7447 parent = find_window(win->transient);
7449 parent->focus_child = win;
7451 DNPRINTF(SWM_D_MISC, "set_child_transient: parent doesn't exist"
7452 " for 0x%x trans 0x%x\n", win->id, win->transient);
7454 r = root_to_region(win->wa->root, SWM_CK_ALL);
7456 /* parent doen't exist in our window list */
7457 TAILQ_FOREACH(w, &ws->winlist, entry) {
7458 if (xcb_icccm_get_wm_hints_reply(conn,
7459 xcb_icccm_get_wm_hints(conn, w->id),
7461 warnx("can't get hints for 0x%x", w->id);
7465 if (win->hints.window_group != wmh.window_group)
7468 w->focus_child = win;
7469 win->transient = w->id;
7471 DNPRINTF(SWM_D_MISC, "set_child_transient: adjusting "
7472 "transient to 0x%x\n", win->transient);
7479 window_get_pid(xcb_window_t win)
7484 xcb_get_property_cookie_t pc;
7485 xcb_get_property_reply_t *pr;
7487 apid = get_atom_from_string("_NET_WM_PID");
7488 if (apid == XCB_ATOM_NONE)
7491 pc = xcb_get_property(conn, 0, win, apid, XCB_ATOM_CARDINAL, 0, 1);
7492 pr = xcb_get_property_reply(conn, pc, NULL);
7495 if (pr->type != XCB_ATOM_CARDINAL) {
7500 if (pr->type == apid && pr->format == 32)
7501 ret = *((pid_t *)xcb_get_property_value(pr));
7507 apid = get_atom_from_string("_SWM_PID");
7508 pc = xcb_get_property(conn, 0, win, apid, XCB_ATOM_STRING,
7510 pr = xcb_get_property_reply(conn, pc, NULL);
7513 if (pr->type != apid) {
7518 ret = (pid_t)strtonum(xcb_get_property_value(pr), 0, INT_MAX, &errstr);
7525 get_ws_idx(xcb_window_t id)
7531 xcb_get_property_reply_t *gpr;
7533 gpr = xcb_get_property_reply(conn,
7534 xcb_get_property(conn, 0, id, a_swm_ws,
7535 XCB_ATOM_STRING, 0, SWM_PROPLEN),
7540 proplen = xcb_get_property_value_length(gpr);
7542 prop = malloc(proplen + 1);
7545 xcb_get_property_value(gpr),
7547 prop[proplen] = '\0';
7554 DNPRINTF(SWM_D_PROP, "get_ws_idx: _SWM_WS: %s\n", prop);
7555 ws_idx = (int)strtonum(prop, 0, workspace_limit - 1, &errstr);
7557 DNPRINTF(SWM_D_PROP, "get_ws_idx: window: #%s: %s",
7567 manage_window(xcb_window_t id, uint16_t mapped)
7569 xcb_window_t trans = XCB_WINDOW_NONE;
7570 struct ws_win *win, *ww;
7572 char ws_idx_str[SWM_PROPLEN];
7573 struct swm_region *r;
7577 xcb_icccm_get_wm_protocols_reply_t wpr;
7579 if ((win = find_window(id)) != NULL) {
7580 DNPRINTF(SWM_D_MISC, "manage_window: win 0x%x already "
7581 "managed; skipping.)\n", id);
7582 return (win); /* Already managed. */
7585 /* See if window is on the unmanaged list. */
7586 if ((win = find_unmanaged_window(id)) != NULL) {
7587 DNPRINTF(SWM_D_MISC, "manage_window: win 0x%x found on "
7588 "unmanaged list.\n", id);
7589 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
7592 set_child_transient(win, &trans);
7596 DNPRINTF(SWM_D_MISC, "manage_window: win 0x%x is new.\n", id);
7599 /* Create and initialize ws_win object. */
7600 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
7601 err(1, "manage_window: calloc: failed to allocate memory for "
7606 /* Get window geometry. */
7607 win->wa = xcb_get_geometry_reply(conn,
7608 xcb_get_geometry(conn, win->id),
7611 /* Figure out which region the window belongs to. */
7612 r = root_to_region(win->wa->root, SWM_CK_ALL);
7614 /* Ignore window border if there is one. */
7615 WIDTH(win) = win->wa->width;
7616 HEIGHT(win) = win->wa->height;
7617 X(win) = win->wa->x + win->wa->border_width - border_width;
7618 Y(win) = win->wa->y + win->wa->border_width - border_width;
7620 win->mapped = mapped;
7621 win->floatmaxed = 0;
7622 win->ewmh_flags = 0;
7623 win->s = r->s; /* this never changes */
7625 store_float_geom(win, r);
7627 /* Get WM_SIZE_HINTS. */
7628 xcb_icccm_get_wm_normal_hints_reply(conn,
7629 xcb_icccm_get_wm_normal_hints(conn, win->id),
7633 xcb_icccm_get_wm_hints_reply(conn,
7634 xcb_icccm_get_wm_hints(conn, win->id),
7637 /* Get WM_TRANSIENT_FOR; see if window is a transient. */
7638 xcb_icccm_get_wm_transient_for_reply(conn,
7639 xcb_icccm_get_wm_transient_for(conn, win->id),
7642 win->transient = trans;
7643 set_child_transient(win, &win->transient);
7646 /* Get supported protocols. */
7647 if (xcb_icccm_get_wm_protocols_reply(conn,
7648 xcb_icccm_get_wm_protocols(conn, win->id, a_prot),
7650 for (i = 0; i < wpr.atoms_len; i++) {
7651 if (wpr.atoms[i] == a_takefocus)
7652 win->take_focus = 1;
7653 if (wpr.atoms[i] == a_delete)
7654 win->can_delete = 1;
7656 xcb_icccm_get_wm_protocols_reply_wipe(&wpr);
7659 win->iconic = get_swm_iconic(win);
7661 /* Figure out which workspace the window belongs to. */
7662 if ((p = find_pid(window_get_pid(win->id))) != NULL) {
7663 win->ws = &r->s->ws[p->ws];
7664 TAILQ_REMOVE(&pidlist, p, entry);
7667 } else if ((ws_idx = get_ws_idx(win->id)) != -1 &&
7669 /* _SWM_WS is set; use that. */
7670 win->ws = &r->s->ws[ws_idx];
7671 } else if (trans && (ww = find_window(trans)) != NULL) {
7672 /* Launch transients in the same ws as parent. */
7678 /* Set the _SWM_WS atom so we can remember this after reincarnation. */
7679 if (snprintf(ws_idx_str, SWM_PROPLEN, "%d", win->ws->idx) <
7681 DNPRINTF(SWM_D_PROP, "manage_window: set _SWM_WS: %s\n",
7683 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
7684 a_swm_ws, XCB_ATOM_STRING, 8, strlen(ws_idx_str),
7689 ewmh_autoquirk(win);
7691 /* Determine initial quirks. */
7692 if (xcb_icccm_get_wm_class_reply(conn,
7693 xcb_icccm_get_wm_class(conn, win->id),
7695 DNPRINTF(SWM_D_CLASS, "manage_window: class: %s, name: %s\n",
7696 win->ch.class_name, win->ch.instance_name);
7698 /* java is retarded so treat it special */
7699 if (strstr(win->ch.instance_name, "sun-awt")) {
7700 DNPRINTF(SWM_D_CLASS, "manage_window: java window "
7705 TAILQ_FOREACH(qp, &quirks, entry) {
7706 if (!strcmp(win->ch.class_name, qp->class) &&
7707 !strcmp(win->ch.instance_name, qp->name)) {
7708 DNPRINTF(SWM_D_CLASS, "manage_window: on quirks"
7709 "list; mask: 0x%lx\n", qp->quirk);
7710 if (qp->quirk & SWM_Q_FLOAT)
7712 win->quirks = qp->quirk;
7717 /* Alter window position if quirky */
7718 if (win->quirks & SWM_Q_ANYWHERE)
7721 /* Reset font sizes (the bruteforce way; no default keybinding). */
7722 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
7723 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
7724 fake_keypress(win, XK_KP_Subtract, XCB_MOD_MASK_SHIFT);
7725 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
7726 fake_keypress(win, XK_KP_Add, XCB_MOD_MASK_SHIFT);
7729 /* Make sure window is positioned inside its region, if its active. */
7731 constrain_window(win, win->ws->r, 0);
7735 /* Select which X events to monitor and set border pixel color. */
7736 wa[0] = win->s->c[SWM_S_COLOR_UNFOCUS].pixel;
7737 wa[1] = XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_PROPERTY_CHANGE |
7738 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
7740 wa[1] |= XCB_EVENT_MASK_LEAVE_WINDOW | XCB_EVENT_MASK_FOCUS_CHANGE;
7743 xcb_change_window_attributes(conn, win->id, XCB_CW_BORDER_PIXEL |
7744 XCB_CW_EVENT_MASK, wa);
7747 /* Figure out where to stack the window in the workspace. */
7748 if (trans && (ww = find_window(trans)))
7749 TAILQ_INSERT_AFTER(&win->ws->winlist, ww, win, entry);
7750 else if (win->ws->focus && spawn_position == SWM_STACK_ABOVE)
7751 TAILQ_INSERT_AFTER(&win->ws->winlist, win->ws->focus, win,
7753 else if (win->ws->focus && spawn_position == SWM_STACK_BELOW)
7754 TAILQ_INSERT_BEFORE(win->ws->focus, win, entry);
7755 else switch (spawn_position) {
7758 case SWM_STACK_ABOVE:
7759 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
7761 case SWM_STACK_BOTTOM:
7762 case SWM_STACK_BELOW:
7763 TAILQ_INSERT_HEAD(&win->ws->winlist, win, entry);
7766 /* Get initial _NET_WM_STATE */
7767 ewmh_get_win_state(win);
7768 /* Set initial _NET_WM_ALLOWED_ACTIONS */
7769 ewmh_update_actions(win);
7773 DNPRINTF(SWM_D_MISC, "manage_window: done. window: 0x%x, (x,y) w x h: "
7774 "(%d,%d) %d x %d, ws: %d, iconic: %s, transient: 0x%x\n", win->id,
7775 X(win), Y(win), WIDTH(win), HEIGHT(win), win->ws->idx,
7776 YESNO(win->iconic), win->transient);
7782 free_window(struct ws_win *win)
7784 DNPRINTF(SWM_D_MISC, "free_window: window: 0x%x\n", win->id);
7789 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
7794 xcb_icccm_get_wm_class_reply_wipe(&win->ch);
7799 memset(win, 0xff, sizeof *win); /* XXX kill later */
7802 DNPRINTF(SWM_D_MISC, "free_window: done.\n");
7806 unmanage_window(struct ws_win *win)
7808 struct ws_win *parent;
7813 DNPRINTF(SWM_D_MISC, "unmanage_window: window: 0x%x\n", win->id);
7815 if (win->transient) {
7816 parent = find_window(win->transient);
7818 parent->focus_child = NULL;
7821 TAILQ_REMOVE(&win->ws->winlist, win, entry);
7822 TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
7826 expose(xcb_expose_event_t *e)
7829 struct swm_region *r;
7831 DNPRINTF(SWM_D_EVENT, "expose: window: 0x%x\n", e->window);
7833 num_screens = get_screen_count();
7834 for (i = 0; i < num_screens; i++)
7835 TAILQ_FOREACH(r, &screens[i].rl, entry)
7836 if (e->window == WINID(r->bar))
7844 focusin(xcb_focus_in_event_t *e)
7846 DNPRINTF(SWM_D_EVENT, "focusin: window: 0x%x, mode: %s(%u), "
7847 "detail: %s(%u)\n", e->event, get_notify_mode_label(e->mode),
7848 e->mode, get_notify_detail_label(e->detail), e->detail);
7852 focusout(xcb_focus_out_event_t *e)
7854 DNPRINTF(SWM_D_EVENT, "focusout: window: 0x%x, mode: %s(%u), "
7855 "detail: %s(%u)\n", e->event, get_notify_mode_label(e->mode),
7856 e->mode, get_notify_detail_label(e->detail), e->detail);
7861 keypress(xcb_key_press_event_t *e)
7863 xcb_keysym_t keysym;
7866 keysym = xcb_key_press_lookup_keysym(syms, e, 0);
7868 DNPRINTF(SWM_D_EVENT, "keypress: keysym: %u, win (x,y): 0x%x (%d,%d), "
7869 "detail: %u, time: %u, root (x,y): 0x%x (%d,%d), child: 0x%x, "
7870 "state: %u, same_screen: %s\n", keysym, e->event, e->event_x,
7871 e->event_y, e->detail, e->time, e->root, e->root_x, e->root_y,
7872 e->child, e->state, YESNO(e->same_screen));
7874 if ((kp = key_lookup(CLEANMASK(e->state), keysym)) == NULL)
7877 last_event_time = e->time;
7879 if (kp->funcid == KF_SPAWN_CUSTOM)
7880 spawn_custom(root_to_region(e->root, SWM_CK_ALL),
7881 &(keyfuncs[kp->funcid].args), kp->spawn_name);
7882 else if (keyfuncs[kp->funcid].func)
7883 keyfuncs[kp->funcid].func(root_to_region(e->root, SWM_CK_ALL),
7884 &(keyfuncs[kp->funcid].args));
7887 /* Unfreeze grab events. */
7888 xcb_allow_events(conn, XCB_ALLOW_ASYNC_KEYBOARD, e->time);
7891 DNPRINTF(SWM_D_EVENT, "keypress: done.\n");
7895 buttonpress(xcb_button_press_event_t *e)
7897 struct ws_win *win = NULL;
7898 struct swm_region *r, *old_r;
7902 DNPRINTF(SWM_D_EVENT, "buttonpress: win (x,y): 0x%x (%d,%d), "
7903 "detail: %u, time: %u, root (x,y): 0x%x (%d,%d), child: 0x%x, "
7904 "state: %u, same_screen: %s\n", e->event, e->event_x, e->event_y,
7905 e->detail, e->time, e->root, e->root_x, e->root_y, e->child,
7906 e->state, YESNO(e->same_screen));
7908 if (e->event == e->root) {
7909 if (e->child != 0) {
7910 win = find_window(e->child);
7911 /* Pass ButtonPress to window if it isn't managed. */
7915 /* Focus on empty region */
7916 /* If no windows on region if its empty. */
7917 r = root_to_region(e->root, SWM_CK_POINTER);
7919 DNPRINTF(SWM_D_EVENT, "buttonpress: "
7920 "NULL region; ignoring.\n");
7924 if (TAILQ_EMPTY(&r->ws->winlist)) {
7925 old_r = root_to_region(e->root, SWM_CK_FOCUS);
7926 if (old_r && old_r != r)
7927 unfocus_win(old_r->ws->focus);
7929 xcb_set_input_focus(conn,
7930 XCB_INPUT_FOCUS_PARENT, e->root, e->time);
7932 /* Clear bar since empty. */
7940 win = find_window(e->event);
7946 last_event_time = e->time;
7948 focus_win(get_focus_magic(win));
7950 for (i = 0; i < LENGTH(buttons); i++)
7951 if (client_click == buttons[i].action && buttons[i].func &&
7952 buttons[i].button == e->detail &&
7953 CLEANMASK(buttons[i].mask) == CLEANMASK(e->state)) {
7954 buttons[i].func(win, &buttons[i].args);
7960 DNPRINTF(SWM_D_EVENT, "buttonpress: passing to window.\n");
7961 /* Replay event to event window */
7962 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, e->time);
7964 DNPRINTF(SWM_D_EVENT, "buttonpress: handled.\n");
7965 /* Unfreeze grab events. */
7966 xcb_allow_events(conn, XCB_ALLOW_SYNC_POINTER, e->time);
7974 print_win_geom(xcb_window_t w)
7976 xcb_get_geometry_reply_t *wa;
7978 wa = xcb_get_geometry_reply(conn, xcb_get_geometry(conn, w), NULL);
7981 DNPRINTF(SWM_D_MISC, "print_win_geom: window not found: 0x%x\n",
7986 DNPRINTF(SWM_D_MISC, "print_win_geom: window: 0x%x, root: 0x%x, "
7987 "depth: %u, (x,y) w x h: (%d,%d) %d x %d, border: %d\n",
7988 w, wa->root, wa->depth, wa->x, wa->y, wa->width, wa->height,
7997 get_stack_mode_name(uint8_t mode)
8002 case XCB_STACK_MODE_ABOVE:
8005 case XCB_STACK_MODE_BELOW:
8008 case XCB_STACK_MODE_TOP_IF:
8011 case XCB_STACK_MODE_BOTTOM_IF:
8014 case XCB_STACK_MODE_OPPOSITE:
8026 configurerequest(xcb_configure_request_event_t *e)
8029 struct swm_region *r = NULL;
8032 uint32_t wc[7] = {0};
8034 if ((win = find_window(e->window)) == NULL)
8035 if ((win = find_unmanaged_window(e->window)) == NULL)
8039 if (swm_debug & SWM_D_EVENT) {
8040 print_win_geom(e->window);
8042 DNPRINTF(SWM_D_EVENT, "configurerequest: window: 0x%x, "
8043 "parent: 0x%x, new: %s, value_mask: %u { ", e->window,
8044 e->parent, YESNO(new), e->value_mask);
8045 if (e->value_mask & XCB_CONFIG_WINDOW_X)
8046 DPRINTF("X: %d ", e->x);
8047 if (e->value_mask & XCB_CONFIG_WINDOW_Y)
8048 DPRINTF("Y: %d ", e->y);
8049 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH)
8050 DPRINTF("W: %u ", e->width);
8051 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
8052 DPRINTF("H: %u ", e->height);
8053 if (e->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH)
8054 DPRINTF("Border: %u ", e->border_width);
8055 if (e->value_mask & XCB_CONFIG_WINDOW_SIBLING)
8056 DPRINTF("Sibling: 0x%x ", e->sibling);
8057 if (e->value_mask & XCB_CONFIG_WINDOW_STACK_MODE)
8058 DPRINTF("StackMode: %s(%u) ",
8059 get_stack_mode_name(e->stack_mode), e->stack_mode);
8065 if (e->value_mask & XCB_CONFIG_WINDOW_X) {
8066 mask |= XCB_CONFIG_WINDOW_X;
8069 if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
8070 mask |= XCB_CONFIG_WINDOW_Y;
8073 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
8074 mask |= XCB_CONFIG_WINDOW_WIDTH;
8077 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
8078 mask |= XCB_CONFIG_WINDOW_HEIGHT;
8079 wc[i++] = e->height;
8081 if (e->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH) {
8082 mask |= XCB_CONFIG_WINDOW_BORDER_WIDTH;
8083 wc[i++] = e->border_width;
8085 if (e->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
8086 mask |= XCB_CONFIG_WINDOW_SIBLING;
8087 wc[i++] = e->sibling;
8089 if (e->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
8090 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
8091 wc[i++] = e->stack_mode;
8095 xcb_configure_window(conn, e->window, mask, wc);
8098 } else if ((!win->manual || win->quirks & SWM_Q_ANYWHERE) &&
8099 !(win->ewmh_flags & EWMH_F_FULLSCREEN)) {
8102 else if (win->ws->old_r)
8105 /* windows are centered unless ANYWHERE quirk is set. */
8106 if (win->quirks & SWM_Q_ANYWHERE) {
8107 if (e->value_mask & XCB_CONFIG_WINDOW_X) {
8108 win->g_float.x = e->x;
8110 win->g_float.x -= X(r);
8113 if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
8114 win->g_float.y = e->y;
8116 win->g_float.y -= Y(r);
8120 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH)
8121 win->g_float.w = e->width;
8123 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
8124 win->g_float.h = e->height;
8126 win->g_floatvalid = 1;
8128 if (win->floating && r && (win->transient ||
8129 win->ws->cur_layout != &layouts[SWM_MAX_STACK])) {
8130 WIDTH(win) = win->g_float.w;
8131 HEIGHT(win) = win->g_float.h;
8134 stack_floater(win, r);
8146 DNPRINTF(SWM_D_EVENT, "configurerequest: done.\n");
8150 configurenotify(xcb_configure_notify_event_t *e)
8154 DNPRINTF(SWM_D_EVENT, "configurenotify: win 0x%x, event win: 0x%x, "
8155 "(x,y) WxH: (%d,%d) %ux%u, border: %u, above_sibling: 0x%x, "
8156 "override_redirect: %s\n", e->window, e->event, e->x, e->y,
8157 e->width, e->height, e->border_width, e->above_sibling,
8158 YESNO(e->override_redirect));
8160 win = find_window(e->window);
8162 xcb_icccm_get_wm_normal_hints_reply(conn,
8163 xcb_icccm_get_wm_normal_hints(conn, win->id),
8166 if (font_adjusted) {
8174 destroynotify(xcb_destroy_notify_event_t *e)
8178 DNPRINTF(SWM_D_EVENT, "destroynotify: window: 0x%x\n", e->window);
8180 if ((win = find_window(e->window)) == NULL) {
8181 if ((win = find_unmanaged_window(e->window)) == NULL)
8187 if (focus_mode != SWM_FOCUS_FOLLOW) {
8188 /* If we were focused, make sure we focus on something else. */
8189 if (win == win->ws->focus)
8190 win->ws->focus_pending = get_focus_prev(win);
8193 unmanage_window(win);
8196 if (focus_mode != SWM_FOCUS_FOLLOW) {
8197 if (win->ws->focus_pending) {
8198 focus_win(win->ws->focus_pending);
8199 win->ws->focus_pending = NULL;
8210 get_notify_detail_label(uint8_t detail)
8215 case XCB_NOTIFY_DETAIL_ANCESTOR:
8218 case XCB_NOTIFY_DETAIL_VIRTUAL:
8221 case XCB_NOTIFY_DETAIL_INFERIOR:
8224 case XCB_NOTIFY_DETAIL_NONLINEAR:
8225 label = "Nonlinear";
8227 case XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL:
8228 label = "NonlinearVirtual";
8230 case XCB_NOTIFY_DETAIL_POINTER:
8233 case XCB_NOTIFY_DETAIL_POINTER_ROOT:
8234 label = "PointerRoot";
8236 case XCB_NOTIFY_DETAIL_NONE:
8247 get_notify_mode_label(uint8_t mode)
8252 case XCB_NOTIFY_MODE_NORMAL:
8255 case XCB_NOTIFY_MODE_GRAB:
8258 case XCB_NOTIFY_MODE_UNGRAB:
8261 case XCB_NOTIFY_MODE_WHILE_GRABBED:
8262 label = "WhileGrabbed";
8273 enternotify(xcb_enter_notify_event_t *e)
8276 struct swm_region *r;
8278 DNPRINTF(SWM_D_FOCUS, "enternotify: time: %u, win (x,y): 0x%x "
8279 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): 0x%x (%d,%d), "
8280 "child: 0x%x, same_screen_focus: %s, state: %d\n",
8281 e->time, e->event, e->event_x, e->event_y,
8282 get_notify_mode_label(e->mode), e->mode,
8283 get_notify_detail_label(e->detail), e->detail,
8284 e->root, e->root_x, e->root_y, e->child,
8285 YESNO(e->same_screen_focus), e->state);
8287 if (focus_mode == SWM_FOCUS_MANUAL &&
8288 e->mode == XCB_NOTIFY_MODE_NORMAL) {
8289 DNPRINTF(SWM_D_EVENT, "enternotify: manual focus; ignoring.\n");
8293 last_event_time = e->time;
8295 if ((win = find_window(e->event)) == NULL) {
8296 if (e->event == e->root) {
8297 /* If no windows on pointer region, then focus root. */
8298 r = root_to_region(e->root, SWM_CK_POINTER);
8300 DNPRINTF(SWM_D_EVENT, "enternotify: "
8301 "NULL region; ignoring.\n");
8307 DNPRINTF(SWM_D_EVENT, "enternotify: window is NULL; "
8312 focus_win(get_focus_magic(win));
8320 leavenotify(xcb_leave_notify_event_t *e)
8322 DNPRINTF(SWM_D_FOCUS, "leavenotify: time: %u, win (x,y): 0x%x "
8323 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): 0x%x (%d,%d), "
8324 "child: 0x%x, same_screen_focus: %s, state: %d\n",
8325 e->time, e->event, e->event_x, e->event_y,
8326 get_notify_mode_label(e->mode), e->mode,
8327 get_notify_detail_label(e->detail), e->detail,
8328 e->root, e->root_x, e->root_y, e->child,
8329 YESNO(e->same_screen_focus), e->state);
8334 mapnotify(xcb_map_notify_event_t *e)
8338 DNPRINTF(SWM_D_EVENT, "mapnotify: window: 0x%x\n", e->window);
8340 if ((win = find_window(e->window)) == NULL)
8341 win = manage_window(e->window, 1);
8344 set_win_state(win, XCB_ICCCM_WM_STATE_NORMAL);
8346 if (focus_mode != SWM_FOCUS_FOLLOW) {
8347 if (win->ws->focus_pending == win) {
8349 win->ws->focus_pending = NULL;
8358 mappingnotify(xcb_mapping_notify_event_t *e)
8360 xcb_refresh_keyboard_mapping(syms, e);
8362 if (e->request == XCB_MAPPING_KEYBOARD)
8367 maprequest(xcb_map_request_event_t *e)
8369 struct ws_win *win, *w = NULL;
8370 xcb_get_window_attributes_reply_t *war;
8372 DNPRINTF(SWM_D_EVENT, "maprequest: win 0x%x\n",
8375 war = xcb_get_window_attributes_reply(conn,
8376 xcb_get_window_attributes(conn, e->window),
8379 DNPRINTF(SWM_D_EVENT, "maprequest: window lost.\n");
8383 if (war->override_redirect) {
8384 DNPRINTF(SWM_D_EVENT, "maprequest: override_redirect; "
8389 win = manage_window(e->window,
8390 (war->map_state == XCB_MAP_STATE_VIEWABLE));
8392 /* The new window should get focus; prepare. */
8393 if (focus_mode != SWM_FOCUS_FOLLOW &&
8394 !(win->quirks & SWM_Q_NOFOCUSONMAP)) {
8395 if (win->quirks & SWM_Q_FOCUSONMAP_SINGLE) {
8396 /* See if other wins of same type are already mapped. */
8397 TAILQ_FOREACH(w, &win->ws->winlist, entry) {
8398 if (w == win || !w->mapped)
8401 if (!strcmp(w->ch.class_name,
8402 win->ch.class_name) &&
8403 !strcmp(w->ch.instance_name,
8404 win->ch.instance_name))
8410 win->ws->focus_pending = get_focus_magic(win);
8413 /* All windows need to be mapped if they are in the current workspace.*/
8417 /* Ignore EnterNotify to handle the mapnotify without interference. */
8418 if (focus_mode == SWM_FOCUS_DEFAULT)
8419 event_drain(XCB_ENTER_NOTIFY);
8422 DNPRINTF(SWM_D_EVENT, "maprequest: done.\n");
8426 motionnotify(xcb_motion_notify_event_t *e)
8428 struct swm_region *r;
8431 DNPRINTF(SWM_D_FOCUS, "motionnotify: time: %u, win (x,y): 0x%x "
8432 "(%d,%d), detail: %s(%d), root (x,y): 0x%x (%d,%d), "
8433 "child: 0x%x, same_screen_focus: %s, state: %d\n",
8434 e->time, e->event, e->event_x, e->event_y,
8435 get_notify_detail_label(e->detail), e->detail,
8436 e->root, e->root_x, e->root_y, e->child,
8437 YESNO(e->same_screen), e->state);
8439 last_event_time = e->time;
8441 num_screens = get_screen_count();
8442 for (i = 0; i < num_screens; i++)
8443 if (screens[i].root == e->root)
8446 TAILQ_FOREACH(r, &screens[i].rl, entry)
8447 if (X(r) <= e->root_x && e->root_x < MAX_X(r) &&
8448 Y(r) <= e->root_y && e->root_y < MAX_Y(r))
8456 get_atom_name(xcb_atom_t atom)
8461 * This should be disabled during most debugging since
8462 * xcb_get_* causes an xcb_flush.
8465 xcb_get_atom_name_reply_t *r;
8467 r = xcb_get_atom_name_reply(conn,
8468 xcb_get_atom_name(conn, atom),
8471 len = xcb_get_atom_name_name_length(r);
8473 name = malloc(len + 1);
8475 memcpy(name, xcb_get_atom_name_name(r), len);
8489 propertynotify(xcb_property_notify_event_t *e)
8495 name = get_atom_name(e->atom);
8496 DNPRINTF(SWM_D_EVENT, "propertynotify: window: 0x%x, atom: %s(%u), "
8497 "time: %#x, state: %u\n", e->window, name, e->atom, e->time,
8501 win = find_window(e->window);
8505 last_event_time = e->time;
8507 if (e->atom == a_swm_iconic) {
8508 if (e->state == XCB_PROPERTY_NEW_VALUE) {
8509 if (focus_mode != SWM_FOCUS_FOLLOW)
8510 win->ws->focus_pending = get_focus_prev(win);
8517 if (focus_mode != SWM_FOCUS_FOLLOW) {
8518 focus_win(win->ws->focus_pending);
8519 win->ws->focus_pending = NULL;
8523 } else if (e->state == XCB_PROPERTY_DELETE) {
8524 /* The window is no longer iconic, restack ws. */
8525 if (focus_mode != SWM_FOCUS_FOLLOW)
8526 win->ws->focus_pending = get_focus_magic(win);
8530 /* Flush EnterNotify for mapnotify, if needed. */
8533 } else if (e->atom == a_state) {
8534 /* State just changed, make sure it gets focused if mapped. */
8535 if (e->state == XCB_PROPERTY_NEW_VALUE) {
8536 if (focus_mode != SWM_FOCUS_FOLLOW) {
8538 win->ws->focus_pending == win) {
8539 focus_win(win->ws->focus_pending);
8540 win->ws->focus_pending = NULL;
8544 } else if (e->atom == XCB_ATOM_WM_CLASS ||
8545 e->atom == XCB_ATOM_WM_NAME) {
8553 unmapnotify(xcb_unmap_notify_event_t *e)
8556 struct workspace *ws;
8558 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: 0x%x\n", e->window);
8560 /* If we aren't managing the window, then ignore. */
8561 win = find_window(e->window);
8562 if (win == NULL || win->id != e->window)
8567 if (getstate(e->window) != XCB_ICCCM_WM_STATE_ICONIC)
8568 set_win_state(win, XCB_ICCCM_WM_STATE_ICONIC);
8571 /* window unmapped itself */
8572 /* do unmap/unfocus/restack and unmanage */
8575 /* If win was focused, make sure to focus on something else. */
8576 if (win == ws->focus) {
8577 if (focus_mode != SWM_FOCUS_FOLLOW) {
8578 ws->focus_pending = get_focus_prev(win);
8579 DNPRINTF(SWM_D_EVENT, "unmapnotify: "
8580 "focus_pending: 0x%x\n",
8581 WINID(ws->focus_pending));
8587 unmanage_window(win);
8592 if (focus_mode == SWM_FOCUS_FOLLOW) {
8594 focus_win(get_pointer_win(ws->r->s->root));
8596 if (ws->focus_pending) {
8597 focus_win(ws->focus_pending);
8598 ws->focus_pending = NULL;
8603 if (getstate(e->window) == XCB_ICCCM_WM_STATE_NORMAL)
8604 set_win_state(win, XCB_ICCCM_WM_STATE_ICONIC);
8611 visibilitynotify(xcb_visibility_notify_event_t *e)
8613 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: 0x%x\n",
8619 clientmessage(xcb_client_message_event_t *e)
8622 xcb_map_request_event_t mre;
8626 name = get_atom_name(e->type);
8627 DNPRINTF(SWM_D_EVENT, "clientmessage: window: 0x%x, atom: %s(%u)\n",
8628 e->window, name, e->type);
8631 win = find_window(e->window);
8634 if (e->type == ewmh[_NET_ACTIVE_WINDOW].atom) {
8635 /* Manage the window with maprequest. */
8636 DNPRINTF(SWM_D_EVENT, "clientmessage: request focus on "
8637 "unmanaged window.\n");
8638 mre.window = e->window;
8644 if (e->type == ewmh[_NET_ACTIVE_WINDOW].atom) {
8645 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_ACTIVE_WINDOW\n");
8648 if (e->type == ewmh[_NET_CLOSE_WINDOW].atom) {
8649 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_CLOSE_WINDOW\n");
8650 if (win->can_delete)
8651 client_msg(win, a_delete, 0);
8653 xcb_kill_client(conn, win->id);
8655 if (e->type == ewmh[_NET_MOVERESIZE_WINDOW].atom) {
8656 DNPRINTF(SWM_D_EVENT,
8657 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
8658 if (win->floating) {
8659 if (e->data.data32[0] & (1<<8)) /* x */
8660 X(win) = e->data.data32[1];
8661 if (e->data.data32[0] & (1<<9)) /* y */
8662 Y(win) = e->data.data32[2];
8663 if (e->data.data32[0] & (1<<10)) /* width */
8664 WIDTH(win) = e->data.data32[3];
8665 if (e->data.data32[0] & (1<<11)) /* height */
8666 HEIGHT(win) = e->data.data32[4];
8671 /* TODO: Change stack sizes */
8672 /* notify no change was made. */
8673 config_win(win, NULL);
8676 if (e->type == ewmh[_NET_WM_STATE].atom) {
8677 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_WM_STATE\n");
8678 ewmh_update_win_state(win, e->data.data32[1], e->data.data32[0]);
8679 if (e->data.data32[2])
8680 ewmh_update_win_state(win, e->data.data32[2],
8692 int errcode = xcb_connection_has_error(conn);
8693 #ifdef XCB_CONN_ERROR
8696 case XCB_CONN_ERROR:
8697 s = "Socket error, pipe error or other stream error.";
8699 case XCB_CONN_CLOSED_EXT_NOTSUPPORTED:
8700 s = "Extension not supported.";
8702 case XCB_CONN_CLOSED_MEM_INSUFFICIENT:
8703 s = "Insufficient memory.";
8705 case XCB_CONN_CLOSED_REQ_LEN_EXCEED:
8706 s = "Request length was exceeded.";
8708 case XCB_CONN_CLOSED_PARSE_ERR:
8709 s = "Error parsing display string.";
8712 s = "Unknown error.";
8715 errx(errcode, "X CONNECTION ERROR: %s", s);
8718 errx(errcode, "X CONNECTION ERROR");
8726 const uint32_t val = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
8727 XCB_EVENT_MASK_ENTER_WINDOW;
8729 xcb_void_cookie_t wac;
8730 xcb_generic_error_t *error;
8732 /* this causes an error if some other window manager is running */
8733 num_screens = get_screen_count();
8734 for (i = 0; i < num_screens; i++) {
8735 if ((sc = get_screen(i)) == NULL)
8736 errx(1, "ERROR: can't get screen %d.", i);
8737 DNPRINTF(SWM_D_INIT, "enable_wm: screen %d, root: 0x%x\n",
8739 wac = xcb_change_window_attributes_checked(conn, sc->root,
8740 XCB_CW_EVENT_MASK, &val);
8741 if ((error = xcb_request_check(conn, wac))) {
8742 DNPRINTF(SWM_D_INIT, "enable_wm: error_code: %u\n",
8748 /* click to focus on empty region */
8749 xcb_grab_button(conn, 1, sc->root, BUTTONMASK,
8750 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC, XCB_WINDOW_NONE,
8751 XCB_CURSOR_NONE, XCB_BUTTON_INDEX_1, XCB_BUTTON_MASK_ANY);
8758 new_region(struct swm_screen *s, int x, int y, int w, int h)
8760 struct swm_region *r, *n;
8761 struct workspace *ws = NULL;
8765 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
8766 s->idx, w, h, x, y);
8768 /* remove any conflicting regions */
8769 n = TAILQ_FIRST(&s->rl);
8772 n = TAILQ_NEXT(r, entry);
8773 if (X(r) < (x + w) && (X(r) + WIDTH(r)) > x &&
8774 Y(r) < (y + h) && (Y(r) + HEIGHT(r)) > y) {
8775 if (r->ws->r != NULL)
8776 r->ws->old_r = r->ws->r;
8779 xcb_destroy_window(conn, r->id);
8780 TAILQ_REMOVE(&s->rl, r, entry);
8781 TAILQ_INSERT_TAIL(&s->orl, r, entry);
8785 /* search old regions for one to reuse */
8787 /* size + location match */
8788 TAILQ_FOREACH(r, &s->orl, entry)
8789 if (X(r) == x && Y(r) == y &&
8790 HEIGHT(r) == h && WIDTH(r) == w)
8794 TAILQ_FOREACH(r, &s->orl, entry)
8795 if (HEIGHT(r) == h && WIDTH(r) == w)
8799 TAILQ_REMOVE(&s->orl, r, entry);
8800 /* try to use old region's workspace */
8801 if (r->ws->r == NULL)
8804 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
8805 err(1, "new_region: calloc: failed to allocate memory "
8808 /* if we don't have a workspace already, find one */
8810 for (i = 0; i < workspace_limit; i++)
8811 if (s->ws[i].r == NULL) {
8818 errx(1, "new_region: no free workspaces");
8829 TAILQ_INSERT_TAIL(&s->rl, r, entry);
8831 /* Invisible region window to detect pointer events on empty regions. */
8832 r->id = xcb_generate_id(conn);
8833 wa[0] = XCB_EVENT_MASK_POINTER_MOTION |
8834 XCB_EVENT_MASK_POINTER_MOTION_HINT;
8836 xcb_create_window(conn, XCB_COPY_FROM_PARENT, r->id, r->s->root,
8837 X(r), Y(r), WIDTH(r), HEIGHT(r), 0, XCB_WINDOW_CLASS_INPUT_ONLY,
8838 XCB_COPY_FROM_PARENT, XCB_CW_EVENT_MASK, wa);
8840 xcb_map_window(conn, r->id);
8846 #ifdef SWM_XRR_HAS_CRTC
8849 #endif /* SWM_XRR_HAS_CRTC */
8850 struct swm_region *r;
8853 xcb_randr_get_screen_resources_current_cookie_t src;
8854 xcb_randr_get_screen_resources_current_reply_t *srr;
8855 xcb_randr_get_crtc_info_cookie_t cic;
8856 xcb_randr_get_crtc_info_reply_t *cir = NULL;
8857 xcb_randr_crtc_t *crtc;
8858 xcb_screen_t *screen;
8860 DNPRINTF(SWM_D_MISC, "scan_xrandr: screen: %d\n", i);
8862 if ((screen = get_screen(i)) == NULL)
8863 errx(1, "ERROR: can't get screen %d.", i);
8865 num_screens = get_screen_count();
8866 if (i >= num_screens)
8867 errx(1, "scan_xrandr: invalid screen");
8869 /* remove any old regions */
8870 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
8871 r->ws->old_r = r->ws->r = NULL;
8873 xcb_destroy_window(conn, r->id);
8874 TAILQ_REMOVE(&screens[i].rl, r, entry);
8875 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
8879 /* map virtual screens onto physical screens */
8880 #ifdef SWM_XRR_HAS_CRTC
8881 if (xrandr_support) {
8882 src = xcb_randr_get_screen_resources_current(conn,
8884 srr = xcb_randr_get_screen_resources_current_reply(conn, src,
8887 new_region(&screens[i], 0, 0,
8888 screen->width_in_pixels,
8889 screen->height_in_pixels);
8892 ncrtc = srr->num_crtcs;
8894 crtc = xcb_randr_get_screen_resources_current_crtcs(srr);
8895 for (c = 0; c < ncrtc; c++) {
8896 cic = xcb_randr_get_crtc_info(conn, crtc[c],
8898 cir = xcb_randr_get_crtc_info_reply(conn, cic, NULL);
8901 if (cir->num_outputs == 0) {
8907 new_region(&screens[i], 0, 0,
8908 screen->width_in_pixels,
8909 screen->height_in_pixels);
8911 new_region(&screens[i],
8912 cir->x, cir->y, cir->width, cir->height);
8917 #endif /* SWM_XRR_HAS_CRTC */
8919 /* If detection failed, create a single region that spans the screen. */
8920 if (TAILQ_EMPTY(&screens[i].rl))
8921 new_region(&screens[i], 0, 0, screen->width_in_pixels,
8922 screen->height_in_pixels);
8925 /* Cleanup unused previously visible workspaces. */
8926 TAILQ_FOREACH(r, &screens[i].orl, entry) {
8927 TAILQ_FOREACH(win, &r->ws->winlist, entry)
8930 /* The screen shouldn't focus on an unused region. */
8931 if (screens[i].r_focus == r)
8932 screens[i].r_focus = NULL;
8935 DNPRINTF(SWM_D_MISC, "scan_xrandr: done.\n");
8939 screenchange(xcb_randr_screen_change_notify_event_t *e)
8941 struct swm_region *r;
8944 DNPRINTF(SWM_D_EVENT, "screenchange: root: 0x%x\n", e->root);
8946 num_screens = get_screen_count();
8947 /* silly event doesn't include the screen index */
8948 for (i = 0; i < num_screens; i++)
8949 if (screens[i].root == e->root)
8951 if (i >= num_screens)
8952 errx(1, "screenchange: screen not found");
8954 /* brute force for now, just re-enumerate the regions */
8958 print_win_geom(e->root);
8960 /* add bars to all regions */
8961 for (i = 0; i < num_screens; i++) {
8962 TAILQ_FOREACH(r, &screens[i].rl, entry)
8968 /* Make sure a region has focus on each screen. */
8969 for (i = 0; i < num_screens; i++) {
8970 if (screens[i].r_focus == NULL) {
8971 r = TAILQ_FIRST(&screens[i].rl);
8984 struct swm_region *r = NULL;
8985 xcb_window_t *wins = NULL, trans;
8987 int i, j, num_screens;
8988 uint16_t state, manage, mapped;
8990 xcb_query_tree_cookie_t qtc;
8991 xcb_query_tree_reply_t *qtr;
8992 xcb_get_window_attributes_cookie_t gac;
8993 xcb_get_window_attributes_reply_t *gar;
8994 xcb_get_property_cookie_t pc;
8996 DNPRINTF(SWM_D_INIT, "grab_windows: begin\n");
8997 num_screens = get_screen_count();
8998 for (i = 0; i < num_screens; i++) {
8999 qtc = xcb_query_tree(conn, screens[i].root);
9000 qtr = xcb_query_tree_reply(conn, qtc, NULL);
9003 wins = xcb_query_tree_children(qtr);
9004 no = xcb_query_tree_children_length(qtr);
9005 /* attach windows to a region */
9006 /* normal windows */
9007 DNPRINTF(SWM_D_INIT, "grab_windows: grab top level windows.\n");
9008 for (j = 0; j < no; j++) {
9009 TAILQ_FOREACH(r, &screens[i].rl, entry) {
9010 if (r->id == wins[j]) {
9011 DNPRINTF(SWM_D_INIT, "grab_windows: "
9012 "skip %#x; region input window.\n",
9021 gac = xcb_get_window_attributes(conn, wins[j]);
9022 gar = xcb_get_window_attributes_reply(conn, gac, NULL);
9024 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
9025 "doesn't exist.\n", wins[j]);
9029 if (gar->override_redirect) {
9030 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
9031 "override_redirect set.\n", wins[j]);
9036 pc = xcb_icccm_get_wm_transient_for(conn, wins[j]);
9037 if (xcb_icccm_get_wm_transient_for_reply(conn, pc,
9039 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
9040 "is transient for %#x.\n", wins[j], trans);
9045 state = getstate(wins[j]);
9046 manage = state != XCB_ICCCM_WM_STATE_WITHDRAWN;
9047 mapped = gar->map_state != XCB_MAP_STATE_UNMAPPED;
9048 if (mapped || manage)
9049 manage_window(wins[j], mapped);
9052 /* transient windows */
9053 DNPRINTF(SWM_D_INIT, "grab_windows: grab transient windows.\n");
9054 for (j = 0; j < no; j++) {
9055 gac = xcb_get_window_attributes(conn, wins[j]);
9056 gar = xcb_get_window_attributes_reply(conn, gac, NULL);
9058 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
9059 "doesn't exist.\n", wins[j]);
9063 if (gar->override_redirect) {
9064 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
9065 "override_redirect set.\n", wins[j]);
9070 state = getstate(wins[j]);
9071 manage = state != XCB_ICCCM_WM_STATE_WITHDRAWN;
9072 mapped = gar->map_state != XCB_MAP_STATE_UNMAPPED;
9073 pc = xcb_icccm_get_wm_transient_for(conn, wins[j]);
9074 if (xcb_icccm_get_wm_transient_for_reply(conn, pc,
9075 &trans, NULL) && manage)
9076 manage_window(wins[j], mapped);
9081 DNPRINTF(SWM_D_INIT, "grab_windows: done.\n");
9087 int i, j, k, num_screens;
9088 struct workspace *ws;
9089 uint32_t gcv[1], wa[1];
9090 const xcb_query_extension_reply_t *qep;
9091 xcb_screen_t *screen;
9092 xcb_randr_query_version_cookie_t c;
9093 xcb_randr_query_version_reply_t *r;
9095 num_screens = get_screen_count();
9096 if ((screens = calloc(num_screens,
9097 sizeof(struct swm_screen))) == NULL)
9098 err(1, "setup_screens: calloc: failed to allocate memory for "
9101 /* initial Xrandr setup */
9103 qep = xcb_get_extension_data(conn, &xcb_randr_id);
9105 c = xcb_randr_query_version(conn, 1, 1);
9106 r = xcb_randr_query_version_reply(conn, c, NULL);
9108 if (r->major_version >= 1) {
9110 xrandr_eventbase = qep->first_event;
9116 wa[0] = cursors[XC_LEFT_PTR].cid;
9118 /* map physical screens */
9119 for (i = 0; i < num_screens; i++) {
9120 DNPRINTF(SWM_D_WS, "setup_screens: init screen: %d\n", i);
9122 screens[i].r_focus = NULL;
9124 TAILQ_INIT(&screens[i].rl);
9125 TAILQ_INIT(&screens[i].orl);
9126 if ((screen = get_screen(i)) == NULL)
9127 errx(1, "ERROR: can't get screen %d.", i);
9128 screens[i].root = screen->root;
9130 /* set default colors */
9131 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
9132 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
9133 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
9134 setscreencolor("rgb:00/40/40", i + 1,
9135 SWM_S_COLOR_BAR_BORDER_UNFOCUS);
9136 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
9137 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
9139 /* create graphics context on screen */
9140 screens[i].bar_gc = xcb_generate_id(conn);
9142 xcb_create_gc(conn, screens[i].bar_gc, screens[i].root,
9143 XCB_GC_GRAPHICS_EXPOSURES, gcv);
9145 /* set default cursor */
9146 xcb_change_window_attributes(conn, screens[i].root,
9149 /* init all workspaces */
9150 /* XXX these should be dynamically allocated too */
9151 for (j = 0; j < SWM_WS_MAX; j++) {
9152 ws = &screens[i].ws[j];
9155 ws->bar_enabled = 1;
9157 ws->focus_prev = NULL;
9158 ws->focus_pending = NULL;
9161 TAILQ_INIT(&ws->winlist);
9162 TAILQ_INIT(&ws->unmanagedlist);
9164 for (k = 0; layouts[k].l_stack != NULL; k++)
9165 if (layouts[k].l_config != NULL)
9166 layouts[k].l_config(ws,
9167 SWM_ARG_ID_STACKINIT);
9168 ws->cur_layout = &layouts[0];
9169 ws->cur_layout->l_string(ws);
9175 xcb_randr_select_input(conn, screens[i].root,
9176 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE);
9183 if ((bar_fonts = strdup(SWM_BAR_FONTS)) == NULL)
9184 err(1, "setup_globals: strdup: failed to allocate memory.");
9186 if ((clock_format = strdup("%a %b %d %R %Z %Y")) == NULL)
9187 err(1, "setup_globals: strdup: failed to allocate memory.");
9189 if ((syms = xcb_key_symbols_alloc(conn)) == NULL)
9190 errx(1, "unable to allocate key symbols");
9192 a_state = get_atom_from_string("WM_STATE");
9193 a_prot = get_atom_from_string("WM_PROTOCOLS");
9194 a_delete = get_atom_from_string("WM_DELETE_WINDOW");
9195 a_takefocus = get_atom_from_string("WM_TAKE_FOCUS");
9196 a_wmname = get_atom_from_string("WM_NAME");
9197 a_netwmname = get_atom_from_string("_NET_WM_NAME");
9198 a_utf8_string = get_atom_from_string("UTF8_STRING");
9199 a_string = get_atom_from_string("STRING");
9200 a_swm_iconic = get_atom_from_string("_SWM_ICONIC");
9201 a_swm_ws = get_atom_from_string("_SWM_WS");
9208 xcb_atom_t netwmcheck;
9209 xcb_window_t root, win;
9211 /* work around sun jdk bugs, code from wmname */
9212 netwmcheck = get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
9214 num_screens = get_screen_count();
9215 for (i = 0; i < num_screens; i++) {
9216 root = screens[i].root;
9218 win = xcb_generate_id(conn);
9219 xcb_create_window(conn, XCB_COPY_FROM_PARENT, win, root,
9220 0, 0, 1, 1, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT,
9221 XCB_COPY_FROM_PARENT, 0, NULL);
9223 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root,
9224 netwmcheck, XCB_ATOM_WINDOW, 32, 1, &win);
9225 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win,
9226 netwmcheck, XCB_ATOM_WINDOW, 32, 1, &win);
9227 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win,
9228 a_netwmname, a_utf8_string, 8, strlen("LG3D"), "LG3D");
9233 shutdown_cleanup(void)
9237 /* disable alarm because the following code may not be interrupted */
9239 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
9240 err(1, "can't disable alarm");
9249 num_screens = get_screen_count();
9250 for (i = 0; i < num_screens; ++i) {
9251 if (screens[i].bar_gc != 0)
9252 xcb_free_gc(conn, screens[i].bar_gc);
9253 if (!bar_font_legacy)
9254 XftColorFree(display, DefaultVisual(display, i),
9255 DefaultColormap(display, i), &bar_font_color);
9258 if (bar_font_legacy)
9259 XFreeFontSet(display, bar_fs);
9261 XftFontClose(display, bar_font);
9264 xcb_key_symbols_free(syms);
9266 xcb_disconnect(conn);
9270 event_error(xcb_generic_error_t *e)
9274 DNPRINTF(SWM_D_EVENT, "event_error: %s(%u) from %s(%u), sequence: %u, "
9275 "resource_id: %u, minor_code: %u\n",
9276 xcb_event_get_error_label(e->error_code), e->error_code,
9277 xcb_event_get_request_label(e->major_code), e->major_code,
9278 e->sequence, e->resource_id, e->minor_code);
9282 event_handle(xcb_generic_event_t *evt)
9284 uint8_t type = XCB_EVENT_RESPONSE_TYPE(evt);
9286 DNPRINTF(SWM_D_EVENT, "XCB Event: %s(%d), seq %u\n",
9287 xcb_event_get_label(XCB_EVENT_RESPONSE_TYPE(evt)),
9288 XCB_EVENT_RESPONSE_TYPE(evt), evt->sequence);
9291 #define EVENT(type, callback) case type: callback((void *)evt); return
9292 EVENT(0, event_error);
9293 EVENT(XCB_BUTTON_PRESS, buttonpress);
9294 /*EVENT(XCB_BUTTON_RELEASE, buttonpress);*/
9295 /*EVENT(XCB_CIRCULATE_NOTIFY, );*/
9296 /*EVENT(XCB_CIRCULATE_REQUEST, );*/
9297 EVENT(XCB_CLIENT_MESSAGE, clientmessage);
9298 /*EVENT(XCB_COLORMAP_NOTIFY, );*/
9299 EVENT(XCB_CONFIGURE_NOTIFY, configurenotify);
9300 EVENT(XCB_CONFIGURE_REQUEST, configurerequest);
9301 /*EVENT(XCB_CREATE_NOTIFY, );*/
9302 EVENT(XCB_DESTROY_NOTIFY, destroynotify);
9303 EVENT(XCB_ENTER_NOTIFY, enternotify);
9304 EVENT(XCB_EXPOSE, expose);
9306 EVENT(XCB_FOCUS_IN, focusin);
9307 EVENT(XCB_FOCUS_OUT, focusout);
9309 /*EVENT(XCB_GRAPHICS_EXPOSURE, );*/
9310 /*EVENT(XCB_GRAVITY_NOTIFY, );*/
9311 EVENT(XCB_KEY_PRESS, keypress);
9312 /*EVENT(XCB_KEY_RELEASE, keypress);*/
9313 /*EVENT(XCB_KEYMAP_NOTIFY, );*/
9315 EVENT(XCB_LEAVE_NOTIFY, leavenotify);
9317 EVENT(XCB_MAP_NOTIFY, mapnotify);
9318 EVENT(XCB_MAP_REQUEST, maprequest);
9319 EVENT(XCB_MAPPING_NOTIFY, mappingnotify);
9320 EVENT(XCB_MOTION_NOTIFY, motionnotify);
9321 /*EVENT(XCB_NO_EXPOSURE, );*/
9322 EVENT(XCB_PROPERTY_NOTIFY, propertynotify);
9323 /*EVENT(XCB_REPARENT_NOTIFY, );*/
9324 /*EVENT(XCB_RESIZE_REQUEST, );*/
9325 /*EVENT(XCB_SELECTION_CLEAR, );*/
9326 /*EVENT(XCB_SELECTION_NOTIFY, );*/
9327 /*EVENT(XCB_SELECTION_REQUEST, );*/
9328 EVENT(XCB_UNMAP_NOTIFY, unmapnotify);
9329 /*EVENT(XCB_VISIBILITY_NOTIFY, visibilitynotify);*/
9332 if (type - xrandr_eventbase == XCB_RANDR_SCREEN_CHANGE_NOTIFY)
9333 screenchange((void *)evt);
9337 main(int argc, char *argv[])
9339 struct swm_region *r;
9340 char conf[PATH_MAX], *cfile = NULL;
9342 int xfd, i, num_screens, startup = 1;
9343 struct sigaction sact;
9344 xcb_generic_event_t *evt;
9348 int stdin_ready = 0;
9351 /* suppress unused warning since var is needed */
9354 time_started = time(NULL);
9357 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION, buildstr);
9358 if (!setlocale(LC_CTYPE, "") || !setlocale(LC_TIME, ""))
9359 warnx("no locale support");
9361 /* handle some signals */
9362 bzero(&sact, sizeof(sact));
9363 sigemptyset(&sact.sa_mask);
9365 sact.sa_handler = sighdlr;
9366 sigaction(SIGINT, &sact, NULL);
9367 sigaction(SIGQUIT, &sact, NULL);
9368 sigaction(SIGTERM, &sact, NULL);
9369 sigaction(SIGHUP, &sact, NULL);
9371 sact.sa_handler = sighdlr;
9372 sact.sa_flags = SA_NOCLDSTOP;
9373 sigaction(SIGCHLD, &sact, NULL);
9375 if (!(display = XOpenDisplay(0)))
9376 errx(1, "can not open display");
9378 conn = XGetXCBConnection(display);
9379 if (xcb_connection_has_error(conn))
9380 errx(1, "can not get XCB connection");
9382 XSetEventQueueOwner(display, XCBOwnsEventQueue);
9384 xcb_prefetch_extension_data(conn, &xcb_randr_id);
9385 xfd = xcb_get_file_descriptor(conn);
9387 /* look for local and global conf file */
9388 pwd = getpwuid(getuid());
9390 errx(1, "invalid user: %d", getuid());
9392 xcb_grab_server(conn);
9395 /* flush all events */
9396 while ((evt = xcb_poll_for_event(conn))) {
9397 if (XCB_EVENT_RESPONSE_TYPE(evt) == 0)
9402 if (enable_wm() != 0)
9403 errx(1, "another window manager is currently running");
9405 /* Load Xcursors and/or cursorfont glyph cursors. */
9417 for (i = 0; ; i++) {
9422 snprintf(conf, sizeof conf, "%s/.%s",
9423 pwd->pw_dir, SWM_CONF_FILE);
9427 snprintf(conf, sizeof conf, "/etc/%s",
9432 snprintf(conf, sizeof conf, "%s/.%s",
9433 pwd->pw_dir, SWM_CONF_FILE_OLD);
9437 snprintf(conf, sizeof conf, "/etc/%s",
9444 if (strlen(conf) && stat(conf, &sb) != -1)
9445 if (S_ISREG(sb.st_mode)) {
9452 /* load conf (if any) */
9454 conf_load(cfile, SWM_CONF_DEFAULT);
9459 /* set some values to work around bad programs */
9461 /* grab existing windows (before we build the bars) */
9464 if (getenv("SWM_STARTED") == NULL)
9465 setenv("SWM_STARTED", "YES", 1);
9467 /* setup all bars */
9468 num_screens = get_screen_count();
9469 for (i = 0; i < num_screens; i++)
9470 TAILQ_FOREACH(r, &screens[i].rl, entry)
9477 xcb_ungrab_server(conn);
9480 rd_max = xfd > STDIN_FILENO ? xfd : STDIN_FILENO;
9483 while ((evt = xcb_poll_for_event(conn))) {
9490 /* If just (re)started, set default focus if needed. */
9494 if (focus_mode != SWM_FOCUS_FOLLOW) {
9495 r = TAILQ_FIRST(&screens[0].rl);
9507 FD_SET(STDIN_FILENO, &rd);
9512 num_readable = select(rd_max + 1, &rd, NULL, NULL, &tv);
9513 if (num_readable == -1 && errno != EINTR) {
9514 DNPRINTF(SWM_D_MISC, "select failed");
9515 } else if (num_readable > 0 && FD_ISSET(STDIN_FILENO, &rd)) {
9520 restart(NULL, NULL);