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 0x7
255 #define SWM_CK_FOCUS 0x1
256 #define SWM_CK_POINTER 0x2
257 #define SWM_CK_FALLBACK 0x4
259 #define SWM_CONF_DEFAULT (0)
260 #define SWM_CONF_KEYMAPPING (1)
263 #define SWM_LIB "/usr/local/lib/libswmhack.so"
270 xcb_atom_t a_takefocus;
272 xcb_atom_t a_netwmname;
273 xcb_atom_t a_utf8_string;
275 xcb_atom_t a_swm_iconic;
277 volatile sig_atomic_t running = 1;
278 volatile sig_atomic_t restart_wm = 0;
279 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 unsigned char bar_ext[SWM_BAR_MAX];
352 char bar_vertext[SWM_BAR_MAX];
354 sig_atomic_t bar_alarm = 0;
357 int bar_border_width = 1;
358 int bar_at_bottom = 0;
360 int bar_extra_running = 0;
363 int bar_justify = SWM_BAR_JUSTIFY_LEFT;
364 char *bar_format = NULL;
365 int stack_enabled = 1;
366 int clock_enabled = 1;
367 int urgent_enabled = 0;
368 char *clock_format = NULL;
369 int title_name_enabled = 0;
370 int title_class_enabled = 0;
371 int window_name_enabled = 0;
372 int focus_mode = SWM_FOCUS_DEFAULT;
373 int focus_close = SWM_STACK_BELOW;
374 int focus_close_wrap = 1;
375 int focus_default = SWM_STACK_TOP;
376 int spawn_position = SWM_STACK_TOP;
377 int disable_border = 0;
378 int border_width = 1;
379 int verbose_layout = 0;
383 XFontSetExtents *bar_fs_extents;
385 int bar_font_legacy = 1;
387 XftColor bar_font_color;
390 /* layout manager data */
391 struct swm_geometry {
404 struct swm_geometry g;
407 /* virtual "screens" */
409 TAILQ_ENTRY(swm_region) entry;
410 struct swm_geometry g;
411 struct workspace *ws; /* current workspace on this region */
412 struct workspace *ws_prior; /* prior workspace on this region */
413 struct swm_screen *s; /* screen idx */
416 TAILQ_HEAD(swm_region_list, swm_region);
419 TAILQ_ENTRY(ws_win) entry;
421 xcb_window_t transient;
422 struct ws_win *focus_child; /* focus on child transient */
423 struct swm_geometry g; /* current geometry */
424 struct swm_geometry g_float; /* region coordinates */
425 int g_floatvalid; /* g_float geometry validity */
426 int floatmaxed; /* whether maxed by max_stack */
432 unsigned int ewmh_flags;
433 int font_size_boundary[SWM_MAX_FONT_STEPS];
439 unsigned long quirks;
440 struct workspace *ws; /* always valid */
441 struct swm_screen *s; /* always valid, never changes */
442 xcb_get_geometry_reply_t *wa;
444 xcb_icccm_get_wm_class_reply_t ch;
445 xcb_icccm_wm_hints_t hints;
447 TAILQ_HEAD(ws_win_list, ws_win);
451 TAILQ_ENTRY(pid_e) entry;
455 TAILQ_HEAD(pid_list, pid_e);
456 struct pid_list pidlist = TAILQ_HEAD_INITIALIZER(pidlist);
458 /* layout handlers */
460 void vertical_config(struct workspace *, int);
461 void vertical_stack(struct workspace *, struct swm_geometry *);
462 void horizontal_config(struct workspace *, int);
463 void horizontal_stack(struct workspace *, struct swm_geometry *);
464 void max_stack(struct workspace *, struct swm_geometry *);
465 void plain_stacker(struct workspace *);
466 void fancy_stacker(struct workspace *);
469 void (*l_stack)(struct workspace *, struct swm_geometry *);
470 void (*l_config)(struct workspace *, int);
472 #define SWM_L_FOCUSPREV (1<<0)
473 #define SWM_L_MAPONFOCUS (1<<1)
474 void (*l_string)(struct workspace *);
476 /* stack, configure */
477 { vertical_stack, vertical_config, 0, plain_stacker },
478 { horizontal_stack, horizontal_config, 0, plain_stacker },
480 SWM_L_MAPONFOCUS | SWM_L_FOCUSPREV, plain_stacker },
481 { NULL, NULL, 0, NULL },
484 /* position of max_stack mode in the layouts array, index into layouts! */
485 #define SWM_V_STACK (0)
486 #define SWM_H_STACK (1)
487 #define SWM_MAX_STACK (2)
489 #define SWM_H_SLICE (32)
490 #define SWM_V_SLICE (32)
492 /* define work spaces */
494 int idx; /* workspace index */
495 char *name; /* workspace name */
496 int always_raise; /* raise windows on focus */
497 struct layout *cur_layout; /* current layout handlers */
498 struct ws_win *focus; /* may be NULL */
499 struct ws_win *focus_prev; /* may be NULL */
500 struct ws_win *focus_pending; /* may be NULL */
501 struct swm_region *r; /* may be NULL */
502 struct swm_region *old_r; /* may be NULL */
503 struct ws_win_list winlist; /* list of windows in ws */
504 struct ws_win_list unmanagedlist; /* list of dead windows in ws */
505 char stacker[10]; /* display stacker and layout */
509 int horizontal_msize;
511 int horizontal_stacks;
522 SWM_S_COLOR_BAR_BORDER,
523 SWM_S_COLOR_BAR_FONT,
529 /* physical screen mapping */
530 #define SWM_WS_MAX (22) /* hard limit */
531 int workspace_limit = 10; /* soft limit */
534 int idx; /* screen index */
535 struct swm_region_list rl; /* list of regions on this screen */
536 struct swm_region_list orl; /* list of old regions */
538 struct workspace ws[SWM_WS_MAX];
544 } c[SWM_S_COLOR_MAX];
546 xcb_gcontext_t bar_gc;
549 struct swm_screen *screens;
551 /* args to functions */
554 #define SWM_ARG_ID_FOCUSNEXT (0)
555 #define SWM_ARG_ID_FOCUSPREV (1)
556 #define SWM_ARG_ID_FOCUSMAIN (2)
557 #define SWM_ARG_ID_SWAPNEXT (10)
558 #define SWM_ARG_ID_SWAPPREV (11)
559 #define SWM_ARG_ID_SWAPMAIN (12)
560 #define SWM_ARG_ID_MOVELAST (13)
561 #define SWM_ARG_ID_MASTERSHRINK (20)
562 #define SWM_ARG_ID_MASTERGROW (21)
563 #define SWM_ARG_ID_MASTERADD (22)
564 #define SWM_ARG_ID_MASTERDEL (23)
565 #define SWM_ARG_ID_FLIPLAYOUT (24)
566 #define SWM_ARG_ID_STACKRESET (30)
567 #define SWM_ARG_ID_STACKINIT (31)
568 #define SWM_ARG_ID_CYCLEWS_UP (40)
569 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
570 #define SWM_ARG_ID_CYCLESC_UP (42)
571 #define SWM_ARG_ID_CYCLESC_DOWN (43)
572 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
573 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
574 #define SWM_ARG_ID_STACKINC (50)
575 #define SWM_ARG_ID_STACKDEC (51)
576 #define SWM_ARG_ID_SS_ALL (60)
577 #define SWM_ARG_ID_SS_WINDOW (61)
578 #define SWM_ARG_ID_DONTCENTER (70)
579 #define SWM_ARG_ID_CENTER (71)
580 #define SWM_ARG_ID_KILLWINDOW (80)
581 #define SWM_ARG_ID_DELETEWINDOW (81)
582 #define SWM_ARG_ID_WIDTHGROW (90)
583 #define SWM_ARG_ID_WIDTHSHRINK (91)
584 #define SWM_ARG_ID_HEIGHTGROW (92)
585 #define SWM_ARG_ID_HEIGHTSHRINK (93)
586 #define SWM_ARG_ID_MOVEUP (100)
587 #define SWM_ARG_ID_MOVEDOWN (101)
588 #define SWM_ARG_ID_MOVELEFT (102)
589 #define SWM_ARG_ID_MOVERIGHT (103)
595 TAILQ_ENTRY(quirk) entry;
599 #define SWM_Q_FLOAT (1<<0) /* float this window */
600 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
601 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
602 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
603 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
604 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
606 TAILQ_HEAD(quirk_list, quirk);
607 struct quirk_list quirks = TAILQ_HEAD_INITIALIZER(quirks);
610 * Supported EWMH hints should be added to
611 * both the enum and the ewmh array
616 _NET_MOVERESIZE_WINDOW,
617 _NET_WM_ACTION_ABOVE,
618 _NET_WM_ACTION_CLOSE,
619 _NET_WM_ACTION_FULLSCREEN,
621 _NET_WM_ACTION_RESIZE,
622 _NET_WM_ALLOWED_ACTIONS,
626 _NET_WM_STATE_FULLSCREEN,
627 _NET_WM_STATE_HIDDEN,
628 _NET_WM_STATE_MAXIMIZED_HORZ,
629 _NET_WM_STATE_MAXIMIZED_VERT,
630 _NET_WM_STATE_SKIP_PAGER,
631 _NET_WM_STATE_SKIP_TASKBAR,
633 _NET_WM_WINDOW_TYPE_DIALOG,
634 _NET_WM_WINDOW_TYPE_DOCK,
635 _NET_WM_WINDOW_TYPE_NORMAL,
636 _NET_WM_WINDOW_TYPE_SPLASH,
637 _NET_WM_WINDOW_TYPE_TOOLBAR,
638 _NET_WM_WINDOW_TYPE_UTILITY,
639 _SWM_WM_STATE_MANUAL,
646 } ewmh[SWM_EWMH_HINT_MAX] = {
647 /* must be in same order as in the enum */
648 {"_NET_ACTIVE_WINDOW", XCB_ATOM_NONE},
649 {"_NET_CLOSE_WINDOW", XCB_ATOM_NONE},
650 {"_NET_MOVERESIZE_WINDOW", XCB_ATOM_NONE},
651 {"_NET_WM_ACTION_ABOVE", XCB_ATOM_NONE},
652 {"_NET_WM_ACTION_CLOSE", XCB_ATOM_NONE},
653 {"_NET_WM_ACTION_FULLSCREEN", XCB_ATOM_NONE},
654 {"_NET_WM_ACTION_MOVE", XCB_ATOM_NONE},
655 {"_NET_WM_ACTION_RESIZE", XCB_ATOM_NONE},
656 {"_NET_WM_ALLOWED_ACTIONS", XCB_ATOM_NONE},
657 {"_NET_WM_NAME", XCB_ATOM_NONE},
658 {"_NET_WM_STATE", XCB_ATOM_NONE},
659 {"_NET_WM_STATE_ABOVE", XCB_ATOM_NONE},
660 {"_NET_WM_STATE_FULLSCREEN", XCB_ATOM_NONE},
661 {"_NET_WM_STATE_HIDDEN", XCB_ATOM_NONE},
662 {"_NET_WM_STATE_MAXIMIZED_HORZ", XCB_ATOM_NONE},
663 {"_NET_WM_STATE_MAXIMIZED_VERT", XCB_ATOM_NONE},
664 {"_NET_WM_STATE_SKIP_PAGER", XCB_ATOM_NONE},
665 {"_NET_WM_STATE_SKIP_TASKBAR", XCB_ATOM_NONE},
666 {"_NET_WM_WINDOW_TYPE", XCB_ATOM_NONE},
667 {"_NET_WM_WINDOW_TYPE_DIALOG", XCB_ATOM_NONE},
668 {"_NET_WM_WINDOW_TYPE_DOCK", XCB_ATOM_NONE},
669 {"_NET_WM_WINDOW_TYPE_NORMAL", XCB_ATOM_NONE},
670 {"_NET_WM_WINDOW_TYPE_SPLASH", XCB_ATOM_NONE},
671 {"_NET_WM_WINDOW_TYPE_TOOLBAR", XCB_ATOM_NONE},
672 {"_NET_WM_WINDOW_TYPE_UTILITY", XCB_ATOM_NONE},
673 {"_SWM_WM_STATE_MANUAL", XCB_ATOM_NONE},
680 XC_BOTTOM_LEFT_CORNER,
681 XC_BOTTOM_RIGHT_CORNER,
689 char *name; /* Name used by Xcursor .*/
690 uint8_t cf_char; /* cursorfont index. */
692 } cursors[XC_MAX] = {
693 {"fleur", XC_fleur, XCB_CURSOR_NONE},
694 {"left_ptr", XC_left_ptr, XCB_CURSOR_NONE},
695 {"bottom_left_corner", XC_bottom_left_corner, XCB_CURSOR_NONE},
696 {"bottom_right_corner", XC_bottom_right_corner, XCB_CURSOR_NONE},
697 {"sizing", XC_sizing, XCB_CURSOR_NONE},
698 {"top_left_corner", XC_top_left_corner, XCB_CURSOR_NONE},
699 {"top_right_corner", XC_top_right_corner, XCB_CURSOR_NONE},
702 /* function prototypes */
703 void buttonpress(xcb_button_press_event_t *);
704 void check_conn(void);
705 void clientmessage(xcb_client_message_event_t *);
706 int conf_load(const char *, int);
707 void configurenotify(xcb_configure_notify_event_t *);
708 void configurerequest(xcb_configure_request_event_t *);
709 void constrain_window(struct ws_win *, struct swm_region *, int);
710 void destroynotify(xcb_destroy_notify_event_t *);
711 void enternotify(xcb_enter_notify_event_t *);
712 void event_drain(uint8_t);
713 void event_error(xcb_generic_error_t *);
714 void event_handle(xcb_generic_event_t *);
715 char *expand_tilde(const char *);
716 void expose(xcb_expose_event_t *);
717 struct ws_win *find_window(xcb_window_t);
718 int floating_toggle_win(struct ws_win *);
719 void focus(struct swm_region *, union arg *);
720 void focus_flush(void);
721 struct ws_win *get_focus_magic(struct ws_win *);
722 struct ws_win *get_focus_prev(struct ws_win *);
723 void focus_win(struct ws_win *);
725 void focusin(xcb_focus_in_event_t *);
727 xcb_atom_t get_atom_from_string(const char *);
729 char *get_atom_name(xcb_atom_t);
730 char *get_notify_detail_label(uint8_t);
731 char *get_notify_mode_label(uint8_t);
733 struct ws_win *get_pointer_win(xcb_window_t);
734 struct ws_win *get_region_focus(struct swm_region *);
735 xcb_screen_t *get_screen(int);
736 char *get_win_name(xcb_window_t);
737 uint32_t getstate(xcb_window_t);
738 void grabbuttons(struct ws_win *);
739 void keypress(xcb_key_press_event_t *);
741 void leavenotify(xcb_leave_notify_event_t *);
743 void load_float_geom(struct ws_win *, struct swm_region *);
744 struct ws_win *manage_window(xcb_window_t, uint16_t);
745 void map_window(struct ws_win *);
746 void mapnotify(xcb_map_notify_event_t *);
747 void mappingnotify(xcb_mapping_notify_event_t *);
748 void maprequest(xcb_map_request_event_t *);
749 void new_region(struct swm_screen *, int, int, int, int);
750 int parse_rgb(const char *, uint16_t *, uint16_t *, uint16_t *);
751 void propertynotify(xcb_property_notify_event_t *);
752 void spawn_select(struct swm_region *, union arg *, const char *, int *);
753 void screenchange(xcb_randr_screen_change_notify_event_t *);
754 void shutdown_cleanup(void);
755 void store_float_geom(struct ws_win *, struct swm_region *);
756 void unmanage_window(struct ws_win *);
757 void unmapnotify(xcb_unmap_notify_event_t *);
758 void unfocus_win(struct ws_win *);
759 void update_window(struct ws_win *);
760 /*void visibilitynotify(xcb_visibility_notify_event_t *);*/
765 xcb_font_t cf = XCB_NONE;
768 for (i = 0; i < LENGTH(cursors); ++i) {
769 /* try to load Xcursor first. */
770 cursors[i].cid = XcursorLibraryLoadCursor(display,
773 /* fallback to cursorfont. */
774 if (cursors[i].cid == XCB_CURSOR_NONE) {
775 if (cf == XCB_NONE) {
776 cf = xcb_generate_id(conn);
777 xcb_open_font(conn, cf, strlen("cursor"),
781 cursors[i].cid = xcb_generate_id(conn);
782 xcb_create_glyph_cursor(conn, cursors[i].cid, cf, cf,
783 cursors[i].cf_char, cursors[i].cf_char + 1, 0, 0, 0,
784 0xffff, 0xffff, 0xffff);
790 xcb_close_font(conn, cf);
794 cursors_cleanup(void)
797 for (i = 0; i < LENGTH(cursors); ++i)
798 xcb_free_cursor(conn, cursors[i].cid);
802 expand_tilde(const char *s)
811 errx(1, "expand_tilde: NULL string.");
820 if ((max = sysconf(_SC_LOGIN_NAME_MAX)) == -1)
821 errx(1, "expand_tilde: sysconf");
823 if ((user = calloc(1, max + 1)) == NULL)
824 errx(1, "expand_tilde: calloc");
826 for (i = 0; s[i] != '/' && s[i] != '\0'; ++i)
831 ppwd = strlen(user) == 0 ? getpwuid(getuid()) : getpwnam(user);
837 if (asprintf(&result, "%s%s", ppwd->pw_dir, s) == -1)
841 errx(1, "expand_tilde: failed to allocate memory.");
847 parse_rgb(const char *rgb, uint16_t *rr, uint16_t *gg, uint16_t *bb)
849 unsigned int tmpr, tmpg, tmpb;
851 if (sscanf(rgb, "rgb:%x/%x/%x", &tmpr, &tmpg, &tmpb) != 3)
854 *rr = RGB_8_TO_16(tmpr);
855 *gg = RGB_8_TO_16(tmpg);
856 *bb = RGB_8_TO_16(tmpb);
862 get_screen(int screen)
864 const xcb_setup_t *r;
865 xcb_screen_iterator_t iter;
867 if ((r = xcb_get_setup(conn)) == NULL) {
868 DNPRINTF(SWM_D_MISC, "get_screen: xcb_get_setup\n");
872 iter = xcb_setup_roots_iterator(r);
873 for (; iter.rem; --screen, xcb_screen_next(&iter))
883 if (focus_mode == SWM_FOCUS_DEFAULT)
884 event_drain(XCB_ENTER_NOTIFY);
890 get_atom_from_string(const char *str)
892 xcb_intern_atom_cookie_t c;
893 xcb_intern_atom_reply_t *r;
896 c = xcb_intern_atom(conn, 0, strlen(str), str);
897 r = xcb_intern_atom_reply(conn, c, NULL);
905 return (XCB_ATOM_NONE);
909 set_swm_iconic(struct ws_win *win, int newv)
916 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
917 a_swm_iconic, XCB_ATOM_INTEGER, 32, 1, &v);
919 xcb_delete_property(conn, win->id, a_swm_iconic);
923 get_swm_iconic(struct ws_win *win)
926 xcb_get_property_reply_t *pr = NULL;
928 pr = xcb_get_property_reply(conn,
929 xcb_get_property(conn, 0, win->id, a_swm_iconic,
930 XCB_ATOM_INTEGER, 0, 1), NULL);
933 if (pr->type != XCB_ATOM_INTEGER || pr->format != 32)
935 v = *((int32_t *)xcb_get_property_value(pr));
946 int i, j, num_screens;
948 sup_list = get_atom_from_string("_NET_SUPPORTED");
950 for (i = 0; i < LENGTH(ewmh); i++)
951 ewmh[i].atom = get_atom_from_string(ewmh[i].name);
953 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
954 for (i = 0; i < num_screens; i++) {
955 /* Support check window will be created by workaround(). */
957 /* Report supported atoms */
958 xcb_delete_property(conn, screens[i].root, sup_list);
959 for (j = 0; j < LENGTH(ewmh); j++)
960 xcb_change_property(conn, XCB_PROP_MODE_APPEND,
961 screens[i].root, sup_list, XCB_ATOM_ATOM, 32, 1,
970 xcb_atom_t sup_check, sup_list;
972 xcb_get_property_cookie_t pc;
973 xcb_get_property_reply_t *pr;
975 sup_check = get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
976 sup_list = get_atom_from_string("_NET_SUPPORTED");
977 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
979 for (i = 0; i < num_screens; i++) {
980 /* Get the support check window and destroy it */
981 pc = xcb_get_property(conn, 0, screens[i].root, sup_check,
982 XCB_ATOM_WINDOW, 0, 1);
983 pr = xcb_get_property_reply(conn, pc, NULL);
986 if (pr->format == sup_check) {
987 id = *((xcb_window_t *)xcb_get_property_value(pr));
989 xcb_destroy_window(conn, id);
990 xcb_delete_property(conn, screens[i].root, sup_check);
991 xcb_delete_property(conn, screens[i].root, sup_list);
998 ewmh_autoquirk(struct ws_win *win)
1002 xcb_get_property_cookie_t c;
1003 xcb_get_property_reply_t *r;
1005 c = xcb_get_property(conn, 0, win->id,
1006 ewmh[_NET_WM_WINDOW_TYPE].atom, XCB_ATOM_ATOM, 0, UINT32_MAX);
1007 r = xcb_get_property_reply(conn, c, NULL);
1011 n = xcb_get_property_value_length(r);
1012 type = xcb_get_property_value(r);
1014 for (i = 0; i < n; i++) {
1015 if (type[i] == ewmh[_NET_WM_WINDOW_TYPE_NORMAL].atom)
1017 if (type[i] == ewmh[_NET_WM_WINDOW_TYPE_DOCK].atom ||
1018 type[i] == ewmh[_NET_WM_WINDOW_TYPE_TOOLBAR].atom ||
1019 type[i] == ewmh[_NET_WM_WINDOW_TYPE_UTILITY].atom) {
1021 win->quirks = SWM_Q_FLOAT | SWM_Q_ANYWHERE;
1024 if (type[i] == ewmh[_NET_WM_WINDOW_TYPE_SPLASH].atom ||
1025 type[i] == ewmh[_NET_WM_WINDOW_TYPE_DIALOG].atom) {
1027 win->quirks = SWM_Q_FLOAT;
1034 #define SWM_EWMH_ACTION_COUNT_MAX (6)
1035 #define EWMH_F_FULLSCREEN (1<<0)
1036 #define EWMH_F_ABOVE (1<<1)
1037 #define EWMH_F_HIDDEN (1<<2)
1038 #define EWMH_F_SKIP_PAGER (1<<3)
1039 #define EWMH_F_SKIP_TASKBAR (1<<4)
1040 #define SWM_F_MANUAL (1<<5)
1043 ewmh_set_win_fullscreen(struct ws_win *win, int fs)
1051 DNPRINTF(SWM_D_MISC, "ewmh_set_win_fullscreen: window: 0x%x, "
1052 "fullscreen %s\n", win->id, YESNO(fs));
1055 if (!win->g_floatvalid)
1056 store_float_geom(win, win->ws->r);
1058 win->g = win->ws->r->g;
1061 load_float_geom(win, win->ws->r);
1068 ewmh_update_actions(struct ws_win *win)
1070 xcb_atom_t actions[SWM_EWMH_ACTION_COUNT_MAX];
1076 actions[n++] = ewmh[_NET_WM_ACTION_CLOSE].atom;
1078 if (win->floating) {
1079 actions[n++] = ewmh[_NET_WM_ACTION_MOVE].atom;
1080 actions[n++] = ewmh[_NET_WM_ACTION_RESIZE].atom;
1081 actions[n++] = ewmh[_NET_WM_ACTION_ABOVE].atom;
1084 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
1085 ewmh[_NET_WM_ALLOWED_ACTIONS].atom, XCB_ATOM_ATOM, 32, 1, actions);
1088 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
1089 #define _NET_WM_STATE_ADD 1 /* add/set property */
1090 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
1093 ewmh_update_win_state(struct ws_win *win, xcb_atom_t state, long action)
1095 unsigned int mask = 0;
1096 unsigned int changed = 0;
1097 unsigned int orig_flags;
1102 DNPRINTF(SWM_D_PROP, "ewmh_update_win_state: window: 0x%x, state: %ld, "
1103 "action: %ld\n", win->id, (unsigned long)state, action);
1105 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
1106 mask = EWMH_F_FULLSCREEN;
1107 else if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
1108 mask = EWMH_F_ABOVE;
1109 else if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
1110 mask = SWM_F_MANUAL;
1111 else if (state == ewmh[_NET_WM_STATE_SKIP_PAGER].atom)
1112 mask = EWMH_F_SKIP_PAGER;
1113 else if (state == ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom)
1114 mask = EWMH_F_SKIP_TASKBAR;
1116 orig_flags = win->ewmh_flags;
1119 case _NET_WM_STATE_REMOVE:
1120 win->ewmh_flags &= ~mask;
1122 case _NET_WM_STATE_ADD:
1123 win->ewmh_flags |= mask;
1125 case _NET_WM_STATE_TOGGLE:
1126 win->ewmh_flags ^= mask;
1130 changed = (win->ewmh_flags & mask) ^ (orig_flags & mask) ? 1 : 0;
1132 if (state == ewmh[_NET_WM_STATE_ABOVE].atom) {
1133 if (changed && !floating_toggle_win(win))
1134 win->ewmh_flags = orig_flags; /* revert */
1135 } else if (state == ewmh[_SWM_WM_STATE_MANUAL].atom) {
1137 win->manual = (win->ewmh_flags & SWM_F_MANUAL) != 0;
1138 } else if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom) {
1139 if (changed && !ewmh_set_win_fullscreen(win,
1140 win->ewmh_flags & EWMH_F_FULLSCREEN))
1141 win->ewmh_flags = orig_flags; /* revert */
1144 xcb_delete_property(conn, win->id, ewmh[_NET_WM_STATE].atom);
1146 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
1147 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1148 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1149 &ewmh[_NET_WM_STATE_FULLSCREEN].atom);
1150 else if (win->ewmh_flags & EWMH_F_SKIP_PAGER)
1151 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1152 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1153 &ewmh[_NET_WM_STATE_SKIP_PAGER].atom);
1154 else if (win->ewmh_flags & EWMH_F_SKIP_TASKBAR)
1155 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1156 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1157 &ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom);
1158 else if (win->ewmh_flags & EWMH_F_ABOVE)
1159 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1160 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1161 &ewmh[_NET_WM_STATE_ABOVE].atom);
1162 else if (win->ewmh_flags & SWM_F_MANUAL)
1163 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1164 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1165 &ewmh[_SWM_WM_STATE_MANUAL].atom);
1169 ewmh_get_win_state(struct ws_win *win)
1172 xcb_get_property_cookie_t c;
1173 xcb_get_property_reply_t *r;
1179 win->ewmh_flags = 0;
1181 win->ewmh_flags |= EWMH_F_ABOVE;
1183 win->ewmh_flags |= SWM_F_MANUAL;
1185 c = xcb_get_property(conn, 0, win->id, ewmh[_NET_WM_STATE].atom,
1186 XCB_ATOM_ATOM, 0, UINT32_MAX);
1187 r = xcb_get_property_reply(conn, c, NULL);
1191 states = xcb_get_property_value(r);
1192 n = xcb_get_property_value_length(r);
1194 for (i = 0; i < n; i++)
1195 ewmh_update_win_state(win, states[i], _NET_WM_STATE_ADD);
1203 dumpwins(struct swm_region *r, union arg *args)
1207 xcb_get_window_attributes_cookie_t c;
1208 xcb_get_window_attributes_reply_t *wa;
1210 /* suppress unused warning since var is needed */
1213 if (r->ws == NULL) {
1214 warnx("dumpwins: invalid workspace");
1218 warnx("=== managed window list ws %02d ===", r->ws->idx);
1219 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
1220 state = getstate(win->id);
1221 c = xcb_get_window_attributes(conn, win->id);
1222 wa = xcb_get_window_attributes_reply(conn, c, NULL);
1224 warnx("window: 0x%x, map_state: %d, state: %u, "
1225 "transient: 0x%x", win->id, wa->map_state,
1226 state, win->transient);
1229 warnx("window: 0x%x, failed xcb_get_window_attributes",
1233 warnx("===== unmanaged window list =====");
1234 TAILQ_FOREACH(win, &r->ws->unmanagedlist, entry) {
1235 state = getstate(win->id);
1236 c = xcb_get_window_attributes(conn, win->id);
1237 wa = xcb_get_window_attributes_reply(conn, c, NULL);
1239 warnx("window: 0x%x, map_state: %d, state: %u, "
1240 "transient: 0x%x", win->id, wa->map_state,
1241 state, win->transient);
1244 warnx("window: 0x%x, failed xcb_get_window_attributes",
1248 warnx("=================================");
1252 dumpwins(struct swm_region *r, union arg *s)
1257 #endif /* SWM_DEBUG */
1262 int saved_errno, status;
1265 saved_errno = errno;
1269 while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) != 0) {
1274 if (errno != ECHILD)
1275 warn("sighdlr: waitpid");
1276 #endif /* SWM_DEBUG */
1279 if (pid == searchpid)
1283 if (WIFEXITED(status)) {
1284 if (WEXITSTATUS(status) != 0)
1285 warnx("sighdlr: child exit status: %d",
1286 WEXITSTATUS(status));
1288 warnx("sighdlr: child is terminated "
1290 #endif /* SWM_DEBUG */
1304 errno = saved_errno;
1310 struct pid_e *p = NULL;
1312 DNPRINTF(SWM_D_MISC, "find_pid: %d\n", pid);
1317 TAILQ_FOREACH(p, &pidlist, entry) {
1326 name_to_pixel(const char *colorname)
1328 uint32_t result = 0;
1329 char cname[32] = "#";
1330 xcb_screen_t *screen;
1331 xcb_colormap_t cmap;
1332 xcb_alloc_color_reply_t *cr;
1333 xcb_alloc_named_color_reply_t *nr;
1334 uint16_t rr, gg, bb;
1336 screen = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
1337 cmap = screen->default_colormap;
1339 /* color is in format rgb://rr/gg/bb */
1340 if (strncmp(colorname, "rgb:", 4) == 0) {
1341 if (parse_rgb(colorname, &rr, &gg, &bb) == -1)
1342 warnx("could not parse rgb %s", colorname);
1344 cr = xcb_alloc_color_reply(conn,
1345 xcb_alloc_color(conn, cmap, rr, gg, bb),
1351 warnx("color '%s' not found", colorname);
1354 nr = xcb_alloc_named_color_reply(conn,
1355 xcb_alloc_named_color(conn, cmap, strlen(colorname),
1358 strlcat(cname, colorname + 2, sizeof cname - 1);
1359 nr = xcb_alloc_named_color_reply(conn,
1360 xcb_alloc_named_color(conn, cmap, strlen(cname),
1367 warnx("color '%s' not found", colorname);
1374 setscreencolor(char *val, int i, int c)
1378 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1379 if (i > 0 && i <= num_screens) {
1380 screens[i - 1].c[c].pixel = name_to_pixel(val);
1381 free(screens[i - 1].c[c].name);
1382 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
1384 } else if (i == -1) {
1385 for (i = 0; i < num_screens; i++) {
1386 screens[i].c[c].pixel = name_to_pixel(val);
1387 free(screens[i].c[c].name);
1388 if ((screens[i].c[c].name = strdup(val)) == NULL)
1392 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1397 fancy_stacker(struct workspace *ws)
1399 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1400 if (ws->cur_layout->l_stack == vertical_stack)
1401 snprintf(ws->stacker, sizeof ws->stacker,
1402 ws->l_state.vertical_flip ? "[%d>%d]" : "[%d|%d]",
1403 ws->l_state.vertical_mwin, ws->l_state.vertical_stacks);
1404 if (ws->cur_layout->l_stack == horizontal_stack)
1405 snprintf(ws->stacker, sizeof ws->stacker,
1406 ws->l_state.horizontal_flip ? "[%dv%d]" : "[%d-%d]",
1407 ws->l_state.horizontal_mwin, ws->l_state.horizontal_stacks);
1411 plain_stacker(struct workspace *ws)
1413 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1414 if (ws->cur_layout->l_stack == vertical_stack)
1415 strlcpy(ws->stacker, ws->l_state.vertical_flip ? "[>]" : "[|]",
1416 sizeof ws->stacker);
1417 if (ws->cur_layout->l_stack == horizontal_stack)
1418 strlcpy(ws->stacker, ws->l_state.horizontal_flip ? "[v]" : "[-]",
1419 sizeof ws->stacker);
1423 custom_region(char *val)
1425 unsigned int x, y, w, h;
1426 int sidx, num_screens;
1427 xcb_screen_t *screen;
1429 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1430 if (sscanf(val, "screen[%d]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
1431 errx(1, "invalid custom region, "
1432 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1433 if (sidx < 1 || sidx > num_screens)
1434 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1438 if ((screen = get_screen(sidx)) == NULL)
1439 errx(1, "ERROR: can't get screen %d.", sidx);
1442 errx(1, "region %ux%u+%u+%u too small", w, h, x, y);
1444 if (x > screen->width_in_pixels ||
1445 y > screen->height_in_pixels ||
1446 w + x > screen->width_in_pixels ||
1447 h + y > screen->height_in_pixels) {
1448 warnx("ignoring region %ux%u+%u+%u - not within screen "
1449 "boundaries (%ux%u)", w, h, x, y,
1450 screen->width_in_pixels, screen->height_in_pixels);
1454 new_region(&screens[sidx], x, y, w, h);
1458 socket_setnonblock(int fd)
1462 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
1463 err(1, "fcntl F_GETFL");
1464 flags |= O_NONBLOCK;
1465 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
1466 err(1, "fcntl F_SETFL");
1470 bar_print_legacy(struct swm_region *r, const char *s)
1472 xcb_rectangle_t rect;
1477 XRectangle ibox, lbox;
1481 XmbTextExtents(bar_fs, s, len, &ibox, &lbox);
1483 switch (bar_justify) {
1484 case SWM_BAR_JUSTIFY_LEFT:
1487 case SWM_BAR_JUSTIFY_CENTER:
1488 x = (WIDTH(r) - lbox.width) / 2;
1490 case SWM_BAR_JUSTIFY_RIGHT:
1491 x = WIDTH(r) - lbox.width - SWM_BAR_OFFSET;
1495 if (x < SWM_BAR_OFFSET)
1500 rect.width = WIDTH(r->bar);
1501 rect.height = HEIGHT(r->bar);
1503 /* clear back buffer */
1504 gcv[0] = r->s->c[SWM_S_COLOR_BAR].pixel;
1505 xcb_change_gc(conn, r->s->bar_gc, XCB_GC_FOREGROUND, gcv);
1506 xcb_poly_fill_rectangle(conn, r->bar->buffer, r->s->bar_gc, 1, &rect);
1508 /* draw back buffer */
1509 gcvd.graphics_exposures = 0;
1510 draw = XCreateGC(display, r->bar->buffer, GCGraphicsExposures, &gcvd);
1511 XSetForeground(display, draw, r->s->c[SWM_S_COLOR_BAR_FONT].pixel);
1512 DRAWSTRING(display, r->bar->buffer, bar_fs, draw,
1513 x, (bar_fs_extents->max_logical_extent.height - lbox.height) / 2 -
1515 XFreeGC(display, draw);
1518 xcb_copy_area(conn, r->bar->buffer, r->bar->id, r->s->bar_gc, 0, 0,
1519 0, 0, WIDTH(r->bar), HEIGHT(r->bar));
1523 bar_print(struct swm_region *r, const char *s)
1526 xcb_rectangle_t rect;
1534 XftTextExtentsUtf8(display, bar_font, (FcChar8 *)s, len, &info);
1536 switch (bar_justify) {
1537 case SWM_BAR_JUSTIFY_LEFT:
1540 case SWM_BAR_JUSTIFY_CENTER:
1541 x = (WIDTH(r) - info.width) / 2;
1543 case SWM_BAR_JUSTIFY_RIGHT:
1544 x = WIDTH(r) - info.width - SWM_BAR_OFFSET;
1548 if (x < SWM_BAR_OFFSET)
1553 rect.width = WIDTH(r->bar);
1554 rect.height = HEIGHT(r->bar);
1556 /* clear back buffer */
1557 gcv[0] = r->s->c[SWM_S_COLOR_BAR].pixel;
1558 xcb_change_gc(conn, r->s->bar_gc, XCB_GC_FOREGROUND, gcv);
1559 xcb_poly_fill_rectangle(conn, r->bar->buffer, r->s->bar_gc, 1, &rect);
1561 /* draw back buffer */
1562 draw = XftDrawCreate(display, r->bar->buffer,
1563 DefaultVisual(display, r->s->idx),
1564 DefaultColormap(display, r->s->idx));
1566 XftDrawStringUtf8(draw, &bar_font_color, bar_font, x,
1567 (HEIGHT(r->bar) + bar_font->height) / 2 - bar_font->descent,
1570 XftDrawDestroy(draw);
1573 xcb_copy_area(conn, r->bar->buffer, r->bar->id, r->s->bar_gc, 0, 0,
1574 0, 0, WIDTH(r->bar), HEIGHT(r->bar));
1578 bar_extra_stop(void)
1582 bzero(bar_pipe, sizeof bar_pipe);
1585 kill(bar_pid, SIGTERM);
1588 strlcpy((char *)bar_ext, "", sizeof bar_ext);
1593 bar_class_name(char *s, size_t sz, struct swm_region *r)
1595 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1597 if (r->ws->focus->ch.class_name != NULL)
1598 strlcat(s, r->ws->focus->ch.class_name, sz);
1602 bar_title_name(char *s, size_t sz, struct swm_region *r)
1604 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1606 if (r->ws->focus->ch.instance_name != NULL)
1607 strlcat(s, r->ws->focus->ch.instance_name, sz);
1611 bar_class_title_name(char *s, size_t sz, struct swm_region *r)
1613 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1616 bar_class_name(s, sz, r);
1617 strlcat(s, ":", sz);
1618 bar_title_name(s, sz, r);
1622 bar_window_float(char *s, size_t sz, struct swm_region *r)
1624 if (r == NULL || r ->ws == NULL || r->ws->focus == NULL)
1626 if (r->ws->focus->floating)
1627 strlcat(s, "(f)", sz);
1631 bar_window_name(char *s, size_t sz, struct swm_region *r)
1635 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1637 if ((title = get_win_name(r->ws->focus->id)) == NULL)
1640 strlcat(s, title, sz);
1644 int urgent[SWM_WS_MAX];
1646 bar_urgent(char *s, size_t sz)
1649 int i, j, num_screens;
1651 xcb_get_property_cookie_t c;
1652 xcb_icccm_wm_hints_t hints;
1654 for (i = 0; i < workspace_limit; i++)
1657 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1658 for (i = 0; i < num_screens; i++)
1659 for (j = 0; j < workspace_limit; j++)
1660 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry) {
1661 c = xcb_icccm_get_wm_hints(conn, win->id);
1662 if (xcb_icccm_get_wm_hints_reply(conn, c,
1665 if (hints.flags & XCB_ICCCM_WM_HINT_X_URGENCY)
1669 for (i = 0; i < workspace_limit; i++) {
1671 snprintf(b, sizeof b, "%d ", i + 1);
1673 snprintf(b, sizeof b, "- ");
1679 bar_workspace_name(char *s, size_t sz, struct swm_region *r)
1681 if (r == NULL || r->ws == NULL)
1683 if (r->ws->name != NULL)
1684 strlcat(s, r->ws->name, sz);
1687 /* build the default bar format according to the defined enabled options */
1689 bar_fmt(const char *fmtexp, char *fmtnew, struct swm_region *r, size_t sz)
1691 /* if format provided, just copy the buffers */
1692 if (bar_format != NULL) {
1693 strlcpy(fmtnew, fmtexp, sz);
1697 /* reset the output buffer */
1700 strlcat(fmtnew, "+N:+I ", sz);
1702 strlcat(fmtnew, "+S", sz);
1703 strlcat(fmtnew, " ", sz);
1705 /* only show the workspace name if there's actually one */
1706 if (r != NULL && r->ws != NULL && r->ws->name != NULL)
1707 strlcat(fmtnew, "<+D>", sz);
1708 strlcat(fmtnew, "+3<", sz);
1710 if (clock_enabled) {
1711 strlcat(fmtnew, fmtexp, sz);
1712 strlcat(fmtnew, "+4<", sz);
1715 /* bar_urgent already adds the space before the last asterisk */
1717 strlcat(fmtnew, "* +U*+4<", sz);
1719 if (title_class_enabled) {
1720 strlcat(fmtnew, "+C", sz);
1721 if (!title_name_enabled)
1722 strlcat(fmtnew, "+4<", sz);
1725 /* checks needed by the colon and floating strlcat(3) calls below */
1726 if (r != NULL && r->ws != NULL && r->ws->focus != NULL) {
1727 if (title_name_enabled) {
1728 if (title_class_enabled)
1729 strlcat(fmtnew, ":", sz);
1730 strlcat(fmtnew, "+T+4<", sz);
1732 if (window_name_enabled) {
1733 if (r->ws->focus->floating)
1734 strlcat(fmtnew, "+F ", sz);
1735 strlcat(fmtnew, "+64W ", sz);
1739 /* finally add the action script output and the version */
1740 strlcat(fmtnew, "+4<+A+4<+V", sz);
1744 bar_replace_pad(char *tmp, int *limit, size_t sz)
1746 /* special case; no limit given, pad one space, instead */
1747 if (*limit == (int)sz - 1)
1749 snprintf(tmp, sz, "%*s", *limit, " ");
1752 /* replaces the bar format character sequences (like in tmux(1)) */
1754 bar_replace_seq(char *fmt, char *fmtrep, struct swm_region *r, size_t *offrep,
1758 char tmp[SWM_BAR_MAX];
1762 /* reset strlcat(3) buffer */
1765 /* get number, if any */
1768 if (sscanf(fmt, "%d%n", &limit, &size) != 1)
1769 limit = sizeof tmp - 1;
1770 if (limit <= 0 || limit >= (int)sizeof tmp)
1771 limit = sizeof tmp - 1;
1773 /* there is nothing to replace (ie EOL) */
1780 bar_replace_pad(tmp, &limit, sizeof tmp);
1783 snprintf(tmp, sizeof tmp, "%s", bar_ext);
1786 bar_class_name(tmp, sizeof tmp, r);
1789 bar_workspace_name(tmp, sizeof tmp, r);
1792 bar_window_float(tmp, sizeof tmp, r);
1795 snprintf(tmp, sizeof tmp, "%d", r->ws->idx + 1);
1798 snprintf(tmp, sizeof tmp, "%d", r->s->idx + 1);
1801 bar_class_title_name(tmp, sizeof tmp, r);
1804 snprintf(tmp, sizeof tmp, "%s", r->ws->stacker);
1807 bar_title_name(tmp, sizeof tmp, r);
1810 bar_urgent(tmp, sizeof tmp);
1813 snprintf(tmp, sizeof tmp, "%s", bar_vertext);
1816 bar_window_name(tmp, sizeof tmp, r);
1819 /* unknown character sequence; copy as-is */
1820 snprintf(tmp, sizeof tmp, "+%c", *fmt);
1826 if ((int)len < limit)
1828 while (limit-- > 0) {
1829 if (*offrep >= sz - 1)
1831 fmtrep[(*offrep)++] = *ptr++;
1839 bar_replace(char *fmt, char *fmtrep, struct swm_region *r, size_t sz)
1844 while (*fmt != '\0') {
1846 /* skip ordinary characters */
1849 fmtrep[off++] = *fmt++;
1853 /* character sequence found; replace it */
1854 fmt = bar_replace_seq(fmt, fmtrep, r, &off, sz);
1863 bar_fmt_expand(char *fmtexp, size_t sz)
1870 /* start by grabbing the current time and date */
1872 localtime_r(&tmt, &tm);
1874 /* figure out what to expand */
1875 if (bar_format != NULL)
1877 else if (bar_format == NULL && clock_enabled)
1879 /* if nothing to expand bail out */
1885 /* copy as-is, just in case the format shouldn't be expanded below */
1886 strlcpy(fmtexp, fmt, sz);
1887 /* finally pass the string through strftime(3) */
1888 #ifndef SWM_DENY_CLOCK_FORMAT
1889 if ((len = strftime(fmtexp, sz, fmt, &tm)) == 0)
1890 warnx("format too long");
1898 char fmtexp[SWM_BAR_MAX], fmtnew[SWM_BAR_MAX];
1899 char fmtrep[SWM_BAR_MAX];
1901 struct swm_region *r;
1903 /* expand the format by first passing it through strftime(3) */
1904 bar_fmt_expand(fmtexp, sizeof fmtexp);
1906 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1907 for (i = 0; i < num_screens; i++) {
1908 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1911 bar_fmt(fmtexp, fmtnew, r, sizeof fmtnew);
1912 bar_replace(fmtnew, fmtrep, r, sizeof fmtrep);
1913 if (bar_font_legacy)
1914 bar_print_legacy(r, fmtrep);
1916 bar_print(r, fmtrep);
1929 if (bar_extra && bar_extra_running) {
1930 /* ignore short reads; it'll correct itself */
1931 while ((b = fgetln(stdin, &len)) != NULL)
1932 if (b && b[len - 1] == '\n') {
1934 strlcpy((char *)bar_ext, b, sizeof bar_ext);
1936 if (b == NULL && errno != EAGAIN) {
1937 warn("bar_update: bar_extra failed");
1941 strlcpy((char *)bar_ext, "", sizeof bar_ext);
1950 /* suppress unused warning since var is needed */
1957 bar_toggle(struct swm_region *r, union arg *args)
1959 struct swm_region *tmpr;
1962 /* suppress unused warnings since vars are needed */
1966 DNPRINTF(SWM_D_BAR, "bar_toggle\n");
1968 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1970 for (i = 0; i < num_screens; i++)
1971 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1973 xcb_unmap_window(conn, tmpr->bar->id);
1975 for (i = 0; i < num_screens; i++)
1976 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1978 xcb_map_window(conn, tmpr->bar->id);
1981 bar_enabled = !bar_enabled;
1985 /* must be after stack */
1994 struct swm_region *r;
1998 /* do this here because the conf file is in memory */
1999 if (bar_extra && !bar_extra_running && bar_argv[0]) {
2000 /* launch external status app */
2001 bar_extra_running = 1;
2002 if (pipe(bar_pipe) == -1)
2003 err(1, "pipe error");
2004 socket_setnonblock(bar_pipe[0]);
2005 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
2006 if (dup2(bar_pipe[0], 0) == -1)
2008 if (dup2(bar_pipe[1], 1) == -1)
2010 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
2011 err(1, "could not disable SIGPIPE");
2012 switch (bar_pid = fork()) {
2014 err(1, "cannot fork");
2018 execvp(bar_argv[0], bar_argv);
2019 err(1, "%s external app failed", bar_argv[0]);
2021 default: /* parent */
2027 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2028 for (i = 0; i < num_screens; i++)
2029 TAILQ_FOREACH(r, &screens[i].rl, entry) {
2032 wa[0] = screens[i].c[SWM_S_COLOR_BAR].pixel;
2033 wa[1] = screens[i].c[SWM_S_COLOR_BAR_BORDER].pixel;
2034 xcb_change_window_attributes(conn, r->bar->id,
2035 XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL, wa);
2045 while ((s = index(s, '-'))) {
2050 return (count == 14);
2056 char *default_string;
2057 char **missing_charsets;
2058 int num_missing_charsets = 0;
2062 XFreeFontSet(display, bar_fs);
2066 DNPRINTF(SWM_D_INIT, "fontset_init: loading bar_fonts: %s\n", bar_fonts);
2068 bar_fs = XCreateFontSet(display, bar_fonts, &missing_charsets,
2069 &num_missing_charsets, &default_string);
2071 if (num_missing_charsets > 0) {
2072 warnx("Unable to load charset(s):");
2074 for (i = 0; i < num_missing_charsets; ++i)
2075 warnx("%s", missing_charsets[i]);
2077 XFreeStringList(missing_charsets);
2079 if (strcmp(default_string, ""))
2080 warnx("Glyphs from those sets will be replaced "
2081 "by '%s'.", default_string);
2083 warnx("Glyphs from those sets won't be drawn.");
2087 errx(1, "Error creating font set structure.");
2089 bar_fs_extents = XExtentsOfFontSet(bar_fs);
2091 bar_height = bar_fs_extents->max_logical_extent.height +
2092 2 * bar_border_width;
2099 xft_init(struct swm_region *r)
2101 char *font, *d, *search;
2104 if (bar_font == NULL) {
2105 if ((d = strdup(bar_fonts)) == NULL)
2106 errx(1, "insufficient memory.");
2108 while ((font = strsep(&search, ",")) != NULL) {
2112 DNPRINTF(SWM_D_INIT, "xft_init: try font %s\n", font);
2115 bar_font = XftFontOpenXlfd(display, r->s->idx,
2118 bar_font = XftFontOpenName(display, r->s->idx,
2123 warnx("unable to load font %s", font);
2126 DNPRINTF(SWM_D_INIT, "successfully opened "
2134 if (bar_font == NULL)
2135 errx(1, "unable to open a font");
2137 PIXEL_TO_XRENDERCOLOR(r->s->c[SWM_S_COLOR_BAR_FONT].pixel, color);
2139 if (!XftColorAllocValue(display, DefaultVisual(display, r->s->idx),
2140 DefaultColormap(display, r->s->idx), &color, &bar_font_color))
2141 warn("unable to allocate Xft color");
2143 bar_height = bar_font->height + 2 * bar_border_width;
2150 bar_setup(struct swm_region *r)
2152 xcb_screen_t *screen;
2155 DNPRINTF(SWM_D_BAR, "bar_setup: screen %d.\n",
2158 if ((screen = get_screen(r->s->idx)) == NULL)
2159 errx(1, "ERROR: can't get screen %d.", r->s->idx);
2164 if ((r->bar = calloc(1, sizeof(struct swm_bar))) == NULL)
2165 err(1, "bar_setup: calloc: failed to allocate memory.");
2167 if (bar_font_legacy)
2173 Y(r->bar) = bar_at_bottom ? (Y(r) + HEIGHT(r) - bar_height) : Y(r);
2174 WIDTH(r->bar) = WIDTH(r) - 2 * bar_border_width;
2175 HEIGHT(r->bar) = bar_height - 2 * bar_border_width;
2177 r->bar->id = xcb_generate_id(conn);
2178 wa[0] = r->s->c[SWM_S_COLOR_BAR].pixel;
2179 wa[1] = r->s->c[SWM_S_COLOR_BAR_BORDER].pixel;
2180 wa[2] = XCB_EVENT_MASK_EXPOSURE;
2182 xcb_create_window(conn, XCB_COPY_FROM_PARENT, r->bar->id, r->s->root,
2183 X(r->bar), Y(r->bar), WIDTH(r->bar), HEIGHT(r->bar),
2184 bar_border_width, XCB_WINDOW_CLASS_INPUT_OUTPUT,
2185 XCB_COPY_FROM_PARENT, XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL
2186 | XCB_CW_EVENT_MASK, wa);
2188 r->bar->buffer = xcb_generate_id(conn);
2189 xcb_create_pixmap(conn, screen->root_depth, r->bar->buffer, r->bar->id,
2190 WIDTH(r->bar), HEIGHT(r->bar));
2193 xcb_randr_select_input(conn, r->bar->id,
2194 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE);
2197 xcb_map_window(conn, r->bar->id);
2199 DNPRINTF(SWM_D_BAR, "bar_setup: window: 0x%x, (x,y) w x h: (%d,%d) "
2200 "%d x %d\n", WINID(r->bar), X(r->bar), Y(r->bar), WIDTH(r->bar),
2203 if (signal(SIGALRM, bar_signal) == SIG_ERR)
2204 err(1, "could not install bar_signal");
2209 bar_cleanup(struct swm_region *r)
2213 xcb_destroy_window(conn, r->bar->id);
2214 xcb_free_pixmap(conn, r->bar->buffer);
2220 set_win_state(struct ws_win *win, uint16_t state)
2222 uint16_t data[2] = { state, XCB_ATOM_NONE };
2224 DNPRINTF(SWM_D_EVENT, "set_win_state: window: 0x%x, state: %u\n",
2230 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id, a_state,
2231 a_state, 32, 2, data);
2235 getstate(xcb_window_t w)
2237 uint32_t result = 0;
2238 xcb_get_property_cookie_t c;
2239 xcb_get_property_reply_t *r;
2241 c = xcb_get_property(conn, 0, w, a_state, a_state, 0L, 2L);
2242 r = xcb_get_property_reply(conn, c, NULL);
2245 if (r->type == a_state && r->format == 32 && r->length == 2)
2246 result = *((uint32_t *)xcb_get_property_value(r));
2250 DNPRINTF(SWM_D_MISC, "getstate property: win 0x%x state %u\n", w,
2256 version(struct swm_region *r, union arg *args)
2258 /* suppress unused warnings since vars are needed */
2262 bar_version = !bar_version;
2264 snprintf(bar_vertext, sizeof bar_vertext,
2265 "Version: %s Build: %s", SPECTRWM_VERSION, buildstr);
2267 strlcpy(bar_vertext, "", sizeof bar_vertext);
2274 client_msg(struct ws_win *win, xcb_atom_t a, xcb_timestamp_t t)
2276 xcb_client_message_event_t ev;
2284 name = get_atom_name(a);
2285 DNPRINTF(SWM_D_EVENT, "client_msg: window: 0x%x, atom: %s(%u), "
2287 win->id, name, a, t);
2291 bzero(&ev, sizeof ev);
2292 ev.response_type = XCB_CLIENT_MESSAGE;
2293 ev.window = win->id;
2296 ev.data.data32[0] = a;
2297 ev.data.data32[1] = t;
2299 xcb_send_event(conn, 0, win->id,
2300 XCB_EVENT_MASK_NO_EVENT, (const char *)&ev);
2303 /* synthetic response to a ConfigureRequest when not making a change */
2305 config_win(struct ws_win *win, xcb_configure_request_event_t *ev)
2307 xcb_configure_notify_event_t ce;
2312 /* send notification of unchanged state. */
2313 bzero(&ce, sizeof(ce));
2314 ce.response_type = XCB_CONFIGURE_NOTIFY;
2317 ce.width = WIDTH(win);
2318 ce.height = HEIGHT(win);
2319 ce.override_redirect = 0;
2324 ce.window = win->id;
2325 ce.border_width = BORDER(win);
2326 ce.above_sibling = XCB_WINDOW_NONE;
2329 ce.event = ev->window;
2330 ce.window = ev->window;
2332 /* make response appear more WM_SIZE_HINTS-compliant */
2333 if (win->sh.flags) {
2334 DNPRINTF(SWM_D_MISC, "config_win: hints: window: 0x%x,"
2335 " sh.flags: %u, min: %d x %d, max: %d x %d, inc: "
2336 "%d x %d\n", win->id, win->sh.flags, SH_MIN_W(win),
2337 SH_MIN_H(win), SH_MAX_W(win), SH_MAX_H(win),
2338 SH_INC_W(win), SH_INC_H(win));
2343 /* the hint may be set... to 0! */
2344 if (SH_MIN_W(win) > 0 && ce.width < SH_MIN_W(win))
2345 ce.width = SH_MIN_W(win);
2346 if (SH_MIN_H(win) > 0 && ce.height < SH_MIN_H(win))
2347 ce.height = SH_MIN_H(win);
2352 /* may also be advertized as 0 */
2353 if (SH_MAX_W(win) > 0 && ce.width > SH_MAX_W(win))
2354 ce.width = SH_MAX_W(win);
2355 if (SH_MAX_H(win) > 0 && ce.height > SH_MAX_H(win))
2356 ce.height = SH_MAX_H(win);
2359 /* resize increment. */
2361 if (SH_INC_W(win) > 1 && ce.width > SH_INC_W(win))
2362 ce.width -= (ce.width - SH_MIN_W(win)) %
2364 if (SH_INC_H(win) > 1 && ce.height > SH_INC_H(win))
2365 ce.height -= (ce.height - SH_MIN_H(win)) %
2369 /* adjust x and y for requested border_width. */
2370 ce.x += BORDER(win) - ev->border_width;
2371 ce.y += BORDER(win) - ev->border_width;
2372 ce.border_width = ev->border_width;
2373 ce.above_sibling = ev->sibling;
2376 DNPRINTF(SWM_D_MISC, "config_win: ewmh: %s, window: 0x%x, (x,y) w x h: "
2377 "(%d,%d) %d x %d, border: %d\n", YESNO(ev == NULL), win->id, ce.x,
2378 ce.y, ce.width, ce.height, ce.border_width);
2380 xcb_send_event(conn, 0, win->id, XCB_EVENT_MASK_STRUCTURE_NOTIFY,
2385 count_win(struct workspace *ws, int count_transient)
2390 TAILQ_FOREACH(win, &ws->winlist, entry) {
2391 if (!count_transient && win->floating)
2393 if (!count_transient && win->transient)
2399 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
2405 quit(struct swm_region *r, union arg *args)
2407 /* suppress unused warnings since vars are needed */
2411 DNPRINTF(SWM_D_MISC, "quit\n");
2416 map_window(struct ws_win *win)
2418 uint32_t val = XCB_STACK_MODE_ABOVE;
2423 DNPRINTF(SWM_D_EVENT, "map_window: win 0x%x, mapped: %s\n", win->id,
2424 YESNO(win->mapped));
2426 xcb_configure_window(conn, win->id,
2427 XCB_CONFIG_WINDOW_STACK_MODE, &val);
2432 xcb_map_window(conn, win->id);
2433 set_win_state(win, XCB_ICCCM_WM_STATE_NORMAL);
2438 unmap_window(struct ws_win *win)
2443 DNPRINTF(SWM_D_EVENT, "unmap_window: win 0x%x, mapped: %s\n", win->id,
2444 YESNO(win->mapped));
2449 xcb_unmap_window(conn, win->id);
2450 set_win_state(win, XCB_ICCCM_WM_STATE_ICONIC);
2458 int i, j, num_screens;
2460 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2461 for (i = 0; i < num_screens; i++)
2462 for (j = 0; j < workspace_limit; j++)
2463 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2468 fake_keypress(struct ws_win *win, xcb_keysym_t keysym, uint16_t modifiers)
2470 xcb_key_press_event_t event;
2471 xcb_keycode_t *keycode;
2476 keycode = xcb_key_symbols_get_keycode(syms, keysym);
2478 DNPRINTF(SWM_D_MISC, "fake_keypress: win 0x%x keycode %u\n",
2481 bzero(&event, sizeof(event));
2482 event.event = win->id;
2483 event.root = win->s->root;
2484 event.child = XCB_WINDOW_NONE;
2485 event.time = XCB_CURRENT_TIME;
2486 event.event_x = X(win);
2487 event.event_y = Y(win);
2490 event.same_screen = 1;
2491 event.detail = *keycode;
2492 event.state = modifiers;
2494 event.response_type = XCB_KEY_PRESS;
2495 xcb_send_event(conn, 1, win->id,
2496 XCB_EVENT_MASK_KEY_PRESS, (const char *)&event);
2498 event.response_type = XCB_KEY_RELEASE;
2499 xcb_send_event(conn, 1, win->id,
2500 XCB_EVENT_MASK_KEY_RELEASE, (const char *)&event);
2506 restart(struct swm_region *r, union arg *args)
2508 /* suppress unused warning since var is needed */
2512 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
2516 execvp(start_argv[0], start_argv);
2517 warn("execvp failed");
2522 get_pointer_win(xcb_window_t root)
2524 struct ws_win *win = NULL;
2525 xcb_query_pointer_reply_t *r;
2527 DNPRINTF(SWM_D_EVENT, "get_pointer_win: root: 0x%x.\n", root);
2529 r = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, root), NULL);
2531 win = find_window(r->child);
2533 DNPRINTF(SWM_D_EVENT, "get_pointer_win: 0x%x.\n",
2536 DNPRINTF(SWM_D_EVENT, "get_pointer_win: none.\n");
2544 root_to_region(xcb_window_t root, int check)
2547 struct swm_region *r = NULL;
2549 xcb_query_pointer_reply_t *qpr;
2550 xcb_get_input_focus_reply_t *gifr;
2552 DNPRINTF(SWM_D_MISC, "root_to_region: window: 0x%x\n", root);
2554 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2555 for (i = 0; i < num_screens; i++)
2556 if (screens[i].root == root)
2559 if (check & SWM_CK_FOCUS) {
2560 /* Try to find an actively focused window */
2561 gifr = xcb_get_input_focus_reply(conn,
2562 xcb_get_input_focus(conn), NULL);
2564 cfw = find_window(gifr->focus);
2565 if (cfw && cfw->ws->r)
2572 if (r == NULL && check & SWM_CK_POINTER) {
2573 /* No region with an active focus; try to use pointer. */
2574 qpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn,
2575 screens[i].root), NULL);
2578 DNPRINTF(SWM_D_MISC, "root_to_region: pointer: "
2579 "(%d,%d)\n", qpr->root_x, qpr->root_y);
2580 TAILQ_FOREACH(r, &screens[i].rl, entry)
2581 if (X(r) <= qpr->root_x &&
2582 qpr->root_x < MAX_X(r) &&
2583 Y(r) <= qpr->root_y &&
2584 qpr->root_y < MAX_Y(r))
2591 if (r == NULL && check & SWM_CK_FALLBACK)
2592 r = TAILQ_FIRST(&screens[i].rl);
2598 find_unmanaged_window(xcb_window_t id)
2601 int i, j, num_screens;
2603 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2604 for (i = 0; i < num_screens; i++)
2605 for (j = 0; j < workspace_limit; j++)
2606 TAILQ_FOREACH(win, &screens[i].ws[j].unmanagedlist,
2614 find_window(xcb_window_t id)
2617 int i, j, num_screens;
2618 xcb_query_tree_reply_t *r;
2620 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2621 for (i = 0; i < num_screens; i++)
2622 for (j = 0; j < workspace_limit; j++)
2623 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2627 r = xcb_query_tree_reply(conn, xcb_query_tree(conn, id), NULL);
2631 /* if we were looking for the parent return that window instead */
2632 if (r->parent == 0 || r->root == r->parent) {
2637 /* look for parent */
2638 for (i = 0; i < num_screens; i++)
2639 for (j = 0; j < workspace_limit; j++)
2640 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2641 if (r->parent == win->id) {
2651 spawn(int ws_idx, union arg *args, int close_fd)
2656 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
2658 close(xcb_get_file_descriptor(conn));
2660 setenv("LD_PRELOAD", SWM_LIB, 1);
2662 if (asprintf(&ret, "%d", ws_idx) == -1) {
2663 warn("spawn: asprintf SWM_WS");
2666 setenv("_SWM_WS", ret, 1);
2670 if (asprintf(&ret, "%d", getpid()) == -1) {
2671 warn("spawn: asprintf _SWM_PID");
2674 setenv("_SWM_PID", ret, 1);
2678 if (setsid() == -1) {
2679 warn("spawn: setsid");
2685 * close stdin and stdout to prevent interaction between apps
2686 * and the baraction script
2687 * leave stderr open to record errors
2689 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) == -1) {
2690 warn("spawn: open");
2693 dup2(fd, STDIN_FILENO);
2694 dup2(fd, STDOUT_FILENO);
2699 execvp(args->argv[0], args->argv);
2701 warn("spawn: execvp");
2706 kill_refs(struct ws_win *win)
2708 int i, x, num_screens;
2709 struct swm_region *r;
2710 struct workspace *ws;
2715 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2716 for (i = 0; i < num_screens; i++)
2717 TAILQ_FOREACH(r, &screens[i].rl, entry)
2718 for (x = 0; x < workspace_limit; x++) {
2720 if (win == ws->focus)
2722 if (win == ws->focus_prev)
2723 ws->focus_prev = NULL;
2728 validate_win(struct ws_win *testwin)
2731 struct workspace *ws;
2732 struct swm_region *r;
2733 int i, x, num_screens;
2735 if (testwin == NULL)
2738 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2739 for (i = 0; i < num_screens; i++)
2740 TAILQ_FOREACH(r, &screens[i].rl, entry)
2741 for (x = 0; x < workspace_limit; x++) {
2743 TAILQ_FOREACH(win, &ws->winlist, entry)
2751 validate_ws(struct workspace *testws)
2753 struct swm_region *r;
2754 struct workspace *ws;
2755 int i, x, num_screens;
2757 /* validate all ws */
2758 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2759 for (i = 0; i < num_screens; i++)
2760 TAILQ_FOREACH(r, &screens[i].rl, entry)
2761 for (x = 0; x < workspace_limit; x++) {
2770 unfocus_win(struct ws_win *win)
2772 xcb_window_t none = XCB_WINDOW_NONE;
2774 DNPRINTF(SWM_D_FOCUS, "unfocus_win: window: 0x%x\n", WINID(win));
2778 if (win->ws == NULL) {
2779 DNPRINTF(SWM_D_FOCUS, "unfocus_win: NULL ws.\n");
2783 if (validate_ws(win->ws)) {
2784 DNPRINTF(SWM_D_FOCUS, "unfocus_win: invalid ws.\n");
2788 if (win->ws->r == NULL) {
2789 DNPRINTF(SWM_D_FOCUS, "unfocus_win: NULL region.\n");
2793 if (validate_win(win)) {
2794 DNPRINTF(SWM_D_FOCUS, "unfocus_win: invalid win.\n");
2799 if (win->ws->focus == win) {
2800 win->ws->focus = NULL;
2801 win->ws->focus_prev = win;
2804 if (validate_win(win->ws->focus)) {
2805 kill_refs(win->ws->focus);
2806 win->ws->focus = NULL;
2808 if (validate_win(win->ws->focus_prev)) {
2809 kill_refs(win->ws->focus_prev);
2810 win->ws->focus_prev = NULL;
2813 xcb_change_window_attributes(conn, win->id, XCB_CW_BORDER_PIXEL,
2814 &win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].pixel);
2816 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
2817 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1, &none);
2819 DNPRINTF(SWM_D_FOCUS, "unfocus_win: done.\n");
2823 focus_win(struct ws_win *win)
2825 struct ws_win *cfw = NULL, *parent = NULL, *w;
2826 struct workspace *ws;
2827 xcb_get_input_focus_reply_t *r;
2829 DNPRINTF(SWM_D_FOCUS, "focus_win: window: 0x%x\n", WINID(win));
2834 if (win->ws == NULL)
2839 if (validate_ws(ws))
2842 if (validate_win(win)) {
2847 r = xcb_get_input_focus_reply(conn, xcb_get_input_focus(conn), NULL);
2849 cfw = find_window(r->focus);
2855 if (ws->focus != win) {
2856 if (ws->focus && ws->focus != cfw)
2857 unfocus_win(ws->focus);
2861 /* If this window directs focus to a child window, then clear. */
2862 if (win->focus_child)
2863 win->focus_child = NULL;
2865 /* If transient, adjust parent's focus child for focus_magic. */
2866 if (win->transient) {
2867 parent = find_window(win->transient);
2868 if (parent && parent->focus_child != win)
2869 parent->focus_child = win;
2872 if (cfw != win && ws->r != NULL) {
2873 /* Set input focus if no input hint, or indicated by hint. */
2874 if (!(win->hints.flags & XCB_ICCCM_WM_HINT_INPUT) ||
2875 (win->hints.flags & XCB_ICCCM_WM_HINT_INPUT &&
2877 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT,
2878 win->id, last_event_time);
2880 /* Tell app it can adjust focus to a specific window. */
2881 if (win->take_focus) {
2882 /* java is special; always tell parent */
2883 if (win->transient && win->java)
2884 client_msg(parent, a_takefocus,
2887 client_msg(win, a_takefocus, last_event_time);
2890 xcb_change_window_attributes(conn, win->id, XCB_CW_BORDER_PIXEL,
2891 &ws->r->s->c[SWM_S_COLOR_FOCUS].pixel);
2893 if (ws->cur_layout->flags & SWM_L_MAPONFOCUS ||
2895 /* If a parent exists, map it first. */
2899 /* Map siblings next. */
2900 TAILQ_FOREACH(w, &ws->winlist, entry)
2901 if (w != win && !w->iconic &&
2902 w->transient == parent->id)
2906 /* Map focused window. */
2909 /* Finally, map children of focus window. */
2910 TAILQ_FOREACH(w, &ws->winlist, entry)
2911 if (w->transient == win->id && !w->iconic)
2916 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
2917 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1,
2924 DNPRINTF(SWM_D_FOCUS, "focus_win: done.\n");
2927 /* If a child window should have focus instead, return it. */
2929 get_focus_magic(struct ws_win *win)
2931 struct ws_win *parent = NULL;
2932 struct ws_win *child = NULL;
2934 DNPRINTF(SWM_D_FOCUS, "get_focus_magic: window: 0x%x\n", WINID(win));
2938 if (win->transient) {
2939 parent = find_window(win->transient);
2941 /* If parent prefers focus elsewhere, then try to do so. */
2942 if (parent && (child = parent->focus_child)) {
2943 if (validate_win(child) == 0 && child->mapped)
2946 parent->focus_child = NULL;
2950 /* If this window prefers focus elsewhere, then try to do so. */
2951 if ((child = win->focus_child)) {
2952 if (validate_win(child) == 0 && child->mapped)
2955 win->focus_child = NULL;
2962 event_drain(uint8_t rt)
2964 xcb_generic_event_t *evt;
2966 /* ensure all pending requests have been processed before filtering. */
2968 while ((evt = xcb_poll_for_event(conn))) {
2969 if (XCB_EVENT_RESPONSE_TYPE(evt) != rt)
2977 switchws(struct swm_region *r, union arg *args)
2979 int wsid = args->id, unmap_old = 0;
2980 struct swm_region *this_r, *other_r;
2982 struct workspace *new_ws, *old_ws;
2987 if (wsid >= workspace_limit)
2991 old_ws = this_r->ws;
2992 new_ws = &this_r->s->ws[wsid];
2994 DNPRINTF(SWM_D_WS, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
2995 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), old_ws->idx, wsid);
2997 if (new_ws == NULL || old_ws == NULL)
2999 if (new_ws == old_ws)
3002 unfocus_win(old_ws->focus);
3004 other_r = new_ws->r;
3005 if (other_r == NULL) {
3006 /* the other workspace is hidden, hide this one */
3010 /* the other ws is visible in another region, exchange them */
3011 other_r->ws_prior = new_ws;
3012 other_r->ws = old_ws;
3013 old_ws->r = other_r;
3015 this_r->ws_prior = old_ws;
3016 this_r->ws = new_ws;
3019 /* Set focus_pending before stacking. */
3020 if (focus_mode != SWM_FOCUS_FOLLOW)
3021 new_ws->focus_pending = get_region_focus(new_ws->r);
3025 /* unmap old windows */
3027 TAILQ_FOREACH(win, &old_ws->winlist, entry)
3030 /* if workspaces were swapped, then don't wait to set focus */
3031 if (old_ws->r && focus_mode != SWM_FOCUS_FOLLOW) {
3032 if (new_ws->focus_pending) {
3033 focus_win(new_ws->focus_pending);
3035 /* Empty region, focus on root. */
3036 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT,
3037 new_ws->r->s[new_ws->r->s->idx].root,
3042 /* Clear bar if new ws is empty. */
3043 if (new_ws->focus_pending == NULL)
3048 DNPRINTF(SWM_D_WS, "switchws: done.\n");
3052 cyclews(struct swm_region *r, union arg *args)
3055 struct swm_screen *s = r->s;
3058 DNPRINTF(SWM_D_WS, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3059 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
3064 case SWM_ARG_ID_CYCLEWS_UP_ALL:
3067 case SWM_ARG_ID_CYCLEWS_UP:
3068 if (a.id < workspace_limit - 1)
3073 case SWM_ARG_ID_CYCLEWS_DOWN_ALL:
3076 case SWM_ARG_ID_CYCLEWS_DOWN:
3080 a.id = workspace_limit - 1;
3087 (!cycle_empty && TAILQ_EMPTY(&s->ws[a.id].winlist)))
3089 if (!cycle_visible && s->ws[a.id].r != NULL)
3093 } while (a.id != r->ws->idx);
3097 priorws(struct swm_region *r, union arg *args)
3103 DNPRINTF(SWM_D_WS, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3104 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
3106 if (r->ws_prior == NULL)
3109 a.id = r->ws_prior->idx;
3114 cyclescr(struct swm_region *r, union arg *args)
3117 struct swm_region *rr = NULL;
3120 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
3121 /* do nothing if we don't have more than one screen */
3122 if (!(num_screens > 1 || outputs > 1))
3127 case SWM_ARG_ID_CYCLESC_UP:
3128 rr = TAILQ_NEXT(r, entry);
3130 rr = TAILQ_FIRST(&screens[i].rl);
3132 case SWM_ARG_ID_CYCLESC_DOWN:
3133 rr = TAILQ_PREV(r, swm_region_list, entry);
3135 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
3143 nfw = get_region_focus(rr);
3147 /* New region is empty; unfocus old region and warp pointer. */
3148 unfocus_win(r->ws->focus);
3149 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT,
3150 rr->s[i].root, XCB_CURRENT_TIME);
3152 /* Clear bar since empty. */
3160 sort_windows(struct ws_win_list *wl)
3162 struct ws_win *win, *parent, *nxt;
3167 for (win = TAILQ_FIRST(wl); win != TAILQ_END(wl); win = nxt) {
3168 nxt = TAILQ_NEXT(win, entry);
3169 if (win->transient) {
3170 parent = find_window(win->transient);
3171 if (parent == NULL) {
3172 warnx("not possible bug");
3175 TAILQ_REMOVE(wl, win, entry);
3176 TAILQ_INSERT_AFTER(wl, parent, win, entry);
3183 swapwin(struct swm_region *r, union arg *args)
3185 struct ws_win *target, *source;
3186 struct ws_win *cur_focus;
3187 struct ws_win_list *wl;
3189 DNPRINTF(SWM_D_WS, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3190 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
3192 cur_focus = r->ws->focus;
3193 if (cur_focus == NULL)
3197 wl = &source->ws->winlist;
3200 case SWM_ARG_ID_SWAPPREV:
3201 if (source->transient)
3202 source = find_window(source->transient);
3203 target = TAILQ_PREV(source, ws_win_list, entry);
3204 if (target && target->transient)
3205 target = find_window(target->transient);
3206 TAILQ_REMOVE(wl, source, entry);
3208 TAILQ_INSERT_TAIL(wl, source, entry);
3210 TAILQ_INSERT_BEFORE(target, source, entry);
3212 case SWM_ARG_ID_SWAPNEXT:
3213 target = TAILQ_NEXT(source, entry);
3214 /* move the parent and let the sort handle the move */
3215 if (source->transient)
3216 source = find_window(source->transient);
3217 TAILQ_REMOVE(wl, source, entry);
3219 TAILQ_INSERT_HEAD(wl, source, entry);
3221 TAILQ_INSERT_AFTER(wl, target, source, entry);
3223 case SWM_ARG_ID_SWAPMAIN:
3224 target = TAILQ_FIRST(wl);
3225 if (target == source) {
3226 if (source->ws->focus_prev != NULL &&
3227 source->ws->focus_prev != target)
3228 source = source->ws->focus_prev;
3232 if (target == NULL || source == NULL)
3234 source->ws->focus_prev = target;
3235 TAILQ_REMOVE(wl, target, entry);
3236 TAILQ_INSERT_BEFORE(source, target, entry);
3237 TAILQ_REMOVE(wl, source, entry);
3238 TAILQ_INSERT_HEAD(wl, source, entry);
3240 case SWM_ARG_ID_MOVELAST:
3241 TAILQ_REMOVE(wl, source, entry);
3242 TAILQ_INSERT_TAIL(wl, source, entry);
3245 DNPRINTF(SWM_D_MOVE, "swapwin: invalid id: %d\n", args->id);
3257 get_focus_prev(struct ws_win *win)
3259 struct ws_win *winfocus = NULL;
3260 struct ws_win *cur_focus = NULL;
3261 struct ws_win_list *wl = NULL;
3262 struct workspace *ws = NULL;
3264 if (!(win && win->ws))
3269 cur_focus = ws->focus;
3271 DNPRINTF(SWM_D_FOCUS, "get_focus_prev: window: 0x%x, cur_focus: 0x%x\n",
3272 WINID(win), WINID(cur_focus));
3274 /* pickle, just focus on whatever */
3275 if (cur_focus == NULL) {
3276 /* use prev_focus if valid */
3277 if (ws->focus_prev && ws->focus_prev != cur_focus &&
3278 find_window(WINID(ws->focus_prev)))
3279 winfocus = ws->focus_prev;
3283 /* if transient focus on parent */
3284 if (cur_focus->transient) {
3285 winfocus = find_window(cur_focus->transient);
3289 /* if in max_stack try harder */
3290 if ((win->quirks & SWM_Q_FOCUSPREV) ||
3291 (ws->cur_layout->flags & SWM_L_FOCUSPREV)) {
3292 if (cur_focus != ws->focus_prev)
3293 winfocus = ws->focus_prev;
3295 winfocus = TAILQ_PREV(win, ws_win_list, entry);
3300 DNPRINTF(SWM_D_FOCUS, "get_focus_prev: focus_close: %d\n", focus_close);
3302 if (winfocus == NULL || winfocus == win) {
3303 switch (focus_close) {
3304 case SWM_STACK_BOTTOM:
3305 winfocus = TAILQ_FIRST(wl);
3308 winfocus = TAILQ_LAST(wl, ws_win_list);
3310 case SWM_STACK_ABOVE:
3311 if ((winfocus = TAILQ_NEXT(cur_focus, entry)) == NULL) {
3312 if (focus_close_wrap)
3313 winfocus = TAILQ_FIRST(wl);
3315 winfocus = TAILQ_PREV(cur_focus,
3316 ws_win_list, entry);
3319 case SWM_STACK_BELOW:
3320 if ((winfocus = TAILQ_PREV(cur_focus, ws_win_list,
3322 if (focus_close_wrap)
3323 winfocus = TAILQ_LAST(wl, ws_win_list);
3325 winfocus = TAILQ_NEXT(cur_focus, entry);
3331 if (winfocus == NULL) {
3332 if (focus_default == SWM_STACK_TOP)
3333 winfocus = TAILQ_LAST(wl, ws_win_list);
3335 winfocus = TAILQ_FIRST(wl);
3340 return get_focus_magic(winfocus);
3344 get_region_focus(struct swm_region *r)
3346 struct ws_win *winfocus = NULL;
3351 if (r->ws->focus && !r->ws->focus->iconic)
3352 winfocus = r->ws->focus;
3353 else if (r->ws->focus_prev && !r->ws->focus_prev->iconic)
3354 winfocus = r->ws->focus_prev;
3356 TAILQ_FOREACH(winfocus, &r->ws->winlist, entry)
3357 if (!winfocus->iconic)
3360 return get_focus_magic(winfocus);
3364 focus(struct swm_region *r, union arg *args)
3366 struct ws_win *head, *cur_focus = NULL, *winfocus = NULL;
3367 struct ws_win_list *wl = NULL;
3368 struct workspace *ws = NULL;
3374 DNPRINTF(SWM_D_FOCUS, "focus: id: %d\n", args->id);
3376 if ((cur_focus = r->ws->focus) == NULL)
3380 if (TAILQ_EMPTY(wl))
3382 /* make sure there is at least one uniconified window */
3384 TAILQ_FOREACH(winfocus, wl, entry)
3385 if (!winfocus->iconic) {
3393 case SWM_ARG_ID_FOCUSPREV:
3394 head = TAILQ_PREV(cur_focus, ws_win_list, entry);
3396 head = TAILQ_LAST(wl, ws_win_list);
3398 if (WINID(winfocus) == cur_focus->transient) {
3399 head = TAILQ_PREV(winfocus, ws_win_list, entry);
3401 head = TAILQ_LAST(wl, ws_win_list);
3406 if (winfocus && winfocus->iconic) {
3407 while (winfocus != cur_focus) {
3408 if (winfocus == NULL)
3409 winfocus = TAILQ_LAST(wl, ws_win_list);
3410 if (!winfocus->iconic)
3412 winfocus = TAILQ_PREV(winfocus, ws_win_list,
3418 case SWM_ARG_ID_FOCUSNEXT:
3419 head = TAILQ_NEXT(cur_focus, entry);
3421 head = TAILQ_FIRST(wl);
3425 if (winfocus && winfocus->iconic) {
3426 while (winfocus != cur_focus) {
3427 if (winfocus == NULL)
3428 winfocus = TAILQ_FIRST(wl);
3429 if (!winfocus->iconic)
3431 winfocus = TAILQ_NEXT(winfocus, entry);
3436 case SWM_ARG_ID_FOCUSMAIN:
3437 winfocus = TAILQ_FIRST(wl);
3438 if (winfocus == cur_focus)
3439 winfocus = cur_focus->ws->focus_prev;
3446 focus_win(get_focus_magic(winfocus));
3452 cycle_layout(struct swm_region *r, union arg *args)
3454 struct workspace *ws = r->ws;
3456 /* suppress unused warning since var is needed */
3459 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
3462 if (ws->cur_layout->l_stack == NULL)
3463 ws->cur_layout = &layouts[0];
3468 focus_win(get_region_focus(r));
3474 stack_config(struct swm_region *r, union arg *args)
3476 struct workspace *ws = r->ws;
3478 DNPRINTF(SWM_D_STACK, "stack_config: id: %d workspace: %d\n",
3481 if (ws->cur_layout->l_config != NULL)
3482 ws->cur_layout->l_config(ws, args->id);
3484 if (args->id != SWM_ARG_ID_STACKINIT)
3493 struct swm_geometry g;
3494 struct swm_region *r;
3500 DNPRINTF(SWM_D_STACK, "stack: begin\n");
3502 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
3503 for (i = 0; i < num_screens; i++) {
3507 TAILQ_FOREACH(r, &screens[i].rl, entry) {
3508 DNPRINTF(SWM_D_STACK, "stack: workspace: %d "
3509 "(screen: %d, region: %d)\n", r->ws->idx, i, j++);
3511 /* start with screen geometry, adjust for bar */
3513 g.w -= 2 * border_width;
3514 g.h -= 2 * border_width;
3520 r->ws->cur_layout->l_stack(r->ws, &g);
3521 r->ws->cur_layout->l_string(r->ws);
3522 /* save r so we can track region changes */
3529 DNPRINTF(SWM_D_STACK, "stack: end\n");
3533 store_float_geom(struct ws_win *win, struct swm_region *r)
3535 if (win == NULL || r == NULL)
3538 /* retain window geom and region geom */
3539 win->g_float = win->g;
3540 win->g_float.x -= X(r);
3541 win->g_float.y -= Y(r);
3542 win->g_floatvalid = 1;
3543 DNPRINTF(SWM_D_MISC, "store_float_geom: window: 0x%x, g: (%d,%d)"
3544 " %d x %d, g_float: (%d,%d) %d x %d\n", win->id, X(win), Y(win),
3545 WIDTH(win), HEIGHT(win), win->g_float.x, win->g_float.y,
3546 win->g_float.w, win->g_float.h);
3550 load_float_geom(struct ws_win *win, struct swm_region *r)
3552 if (win == NULL || r == NULL)
3555 if (win->g_floatvalid) {
3556 win->g = win->g_float;
3559 DNPRINTF(SWM_D_MISC, "load_float_geom: window: 0x%x, g: (%d,%d)"
3560 "%d x %d\n", win->id, X(win), Y(win), WIDTH(win),
3563 DNPRINTF(SWM_D_MISC, "load_float_geom: window: 0x%x, g_float "
3564 "is not set.\n", win->id);
3569 stack_floater(struct ws_win *win, struct swm_region *r)
3574 DNPRINTF(SWM_D_MISC, "stack_floater: window: 0x%x\n", win->id);
3577 * to allow windows to change their size (e.g. mplayer fs) only retrieve
3578 * geom on ws switches or return from max mode
3580 if (win->floatmaxed || (r != r->ws->old_r &&
3581 !(win->ewmh_flags & EWMH_F_FULLSCREEN))) {
3582 /* update geometry for the new region */
3583 load_float_geom(win, r);
3586 win->floatmaxed = 0;
3589 * if set to fullscreen mode, configure window to maximum size.
3591 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
3592 if (!win->g_floatvalid)
3593 store_float_geom(win, win->ws->r);
3599 * remove border on fullscreen floater when in fullscreen mode or when
3600 * the quirk is present.
3602 if ((win->ewmh_flags & EWMH_F_FULLSCREEN) ||
3603 ((win->quirks & SWM_Q_FULLSCREEN) &&
3604 (WIDTH(win) >= WIDTH(r)) && (HEIGHT(win) >= HEIGHT(r)))) {
3605 if (win->bordered) {
3607 X(win) += border_width;
3608 Y(win) += border_width;
3610 } else if (!win->bordered) {
3612 X(win) -= border_width;
3613 Y(win) -= border_width;
3616 if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
3617 WIDTH(win) = (double)WIDTH(r) * dialog_ratio;
3618 HEIGHT(win) = (double)HEIGHT(r) * dialog_ratio;
3621 if (!win->manual && !(win->ewmh_flags & EWMH_F_FULLSCREEN) &&
3622 !(win->quirks & SWM_Q_ANYWHERE)) {
3624 * floaters and transients are auto-centred unless moved,
3625 * resized or ANYWHERE quirk is set.
3627 X(win) = X(r) + (WIDTH(r) - WIDTH(win)) / 2 - BORDER(win);
3628 Y(win) = Y(r) + (HEIGHT(r) - HEIGHT(win)) / 2 - BORDER(win);
3630 store_float_geom(win, r);
3633 /* keep window within region bounds */
3634 constrain_window(win, r, 0);
3640 * Send keystrokes to terminal to decrease/increase the font size as the
3641 * window size changes.
3644 adjust_font(struct ws_win *win)
3646 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
3647 win->floating || win->transient)
3650 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
3651 WIDTH(win) / win->sh.width_inc < term_width &&
3652 win->font_steps < SWM_MAX_FONT_STEPS) {
3653 win->font_size_boundary[win->font_steps] =
3654 (win->sh.width_inc * term_width) + win->sh.base_width;
3657 win->last_inc = win->sh.width_inc;
3658 fake_keypress(win, XK_KP_Subtract, XCB_MOD_MASK_SHIFT);
3659 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
3660 WIDTH(win) > win->font_size_boundary[win->font_steps - 1]) {
3663 win->last_inc = win->sh.width_inc;
3664 fake_keypress(win, XK_KP_Add, XCB_MOD_MASK_SHIFT);
3668 #define SWAPXY(g) do { \
3670 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
3671 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
3674 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
3676 struct swm_geometry win_g, r_g = *g;
3677 struct ws_win *win, *fs_win = NULL;
3678 int i, j, s, stacks;
3679 int w_inc = 1, h_inc, w_base = 1, h_base;
3680 int hrh, extra = 0, h_slice, last_h = 0;
3681 int split, colno, winno, mwin, msize, mscale;
3682 int remain, missing, v_slice, reconfigure = 0;
3685 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d, rot: %s, "
3686 "flip: %s\n", ws->idx, YESNO(rot), YESNO(flip));
3688 winno = count_win(ws, 0);
3689 if (winno == 0 && count_win(ws, 1) == 0)
3692 TAILQ_FOREACH(win, &ws->winlist, entry)
3693 if (!win->transient && !win->floating && !win->iconic)
3700 w_inc = win->sh.width_inc;
3701 w_base = win->sh.base_width;
3702 mwin = ws->l_state.horizontal_mwin;
3703 mscale = ws->l_state.horizontal_msize;
3704 stacks = ws->l_state.horizontal_stacks;
3707 w_inc = win->sh.height_inc;
3708 w_base = win->sh.base_height;
3709 mwin = ws->l_state.vertical_mwin;
3710 mscale = ws->l_state.vertical_msize;
3711 stacks = ws->l_state.vertical_stacks;
3715 if (stacks > winno - mwin)
3716 stacks = winno - mwin;
3720 h_slice = r_g.h / SWM_H_SLICE;
3721 if (mwin && winno > mwin) {
3722 v_slice = r_g.w / SWM_V_SLICE;
3726 win_g.w = v_slice * mscale;
3728 if (w_inc > 1 && w_inc < v_slice) {
3729 /* adjust for window's requested size increment */
3730 remain = (win_g.w - w_base) % w_inc;
3736 win_g.x += r_g.w - msize;
3739 colno = split = winno / stacks;
3740 win_g.w = ((r_g.w - (stacks * 2 * border_width) +
3741 2 * border_width) / stacks);
3743 hrh = r_g.h / colno;
3744 extra = r_g.h - (colno * hrh);
3745 win_g.h = hrh - 2 * border_width;
3747 /* stack all the tiled windows */
3748 i = j = 0, s = stacks;
3749 TAILQ_FOREACH(win, &ws->winlist, entry) {
3750 if (win->transient || win->floating)
3755 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
3760 if (split && i == split) {
3761 colno = (winno - mwin) / stacks;
3762 if (s <= (winno - mwin) % stacks)
3764 split = split + colno;
3765 hrh = (r_g.h / colno);
3766 extra = r_g.h - (colno * hrh);
3770 win_g.x += win_g.w + 2 * border_width;
3771 win_g.w = (r_g.w - msize -
3772 (stacks * 2 * border_width)) / stacks;
3774 win_g.w += (r_g.w - msize -
3775 (stacks * 2 * border_width)) % stacks;
3779 win_g.h = hrh - 2 * border_width;
3781 h_inc = win->sh.width_inc;
3782 h_base = win->sh.base_width;
3784 h_inc = win->sh.height_inc;
3785 h_base = win->sh.base_height;
3787 if (j == colno - 1) {
3788 win_g.h = hrh + extra;
3789 } else if (h_inc > 1 && h_inc < h_slice) {
3790 /* adjust for window's requested size increment */
3791 remain = (win_g.h - h_base) % h_inc;
3792 missing = h_inc - remain;
3794 if (missing <= extra || j == 0) {
3806 win_g.y += last_h + 2 * border_width;
3808 if (disable_border && !bar_enabled && winno == 1){
3810 win_g.w += 2 * border_width;
3811 win_g.h += 2 * border_width;
3816 if (X(win) != win_g.y || Y(win) != win_g.x ||
3817 WIDTH(win) != win_g.h || HEIGHT(win) != win_g.w) {
3821 WIDTH(win) = win_g.h;
3822 HEIGHT(win) = win_g.w;
3825 if (X(win) != win_g.x || Y(win) != win_g.y ||
3826 WIDTH(win) != win_g.w || HEIGHT(win) != win_g.h) {
3830 WIDTH(win) = win_g.w;
3831 HEIGHT(win) = win_g.h;
3835 if (bordered != win->bordered) {
3837 win->bordered = bordered;
3853 /* now, stack all the floaters and transients */
3854 TAILQ_FOREACH(win, &ws->winlist, entry) {
3855 if (!win->transient && !win->floating)
3859 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
3864 stack_floater(win, ws->r);
3868 /* Make sure fs_win is stacked last so it's on top. */
3870 stack_floater(fs_win, ws->r);
3876 vertical_config(struct workspace *ws, int id)
3878 DNPRINTF(SWM_D_STACK, "vertical_config: id: %d, workspace: %d\n",
3882 case SWM_ARG_ID_STACKRESET:
3883 case SWM_ARG_ID_STACKINIT:
3884 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
3885 ws->l_state.vertical_mwin = 1;
3886 ws->l_state.vertical_stacks = 1;
3888 case SWM_ARG_ID_MASTERSHRINK:
3889 if (ws->l_state.vertical_msize > 1)
3890 ws->l_state.vertical_msize--;
3892 case SWM_ARG_ID_MASTERGROW:
3893 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
3894 ws->l_state.vertical_msize++;
3896 case SWM_ARG_ID_MASTERADD:
3897 ws->l_state.vertical_mwin++;
3899 case SWM_ARG_ID_MASTERDEL:
3900 if (ws->l_state.vertical_mwin > 0)
3901 ws->l_state.vertical_mwin--;
3903 case SWM_ARG_ID_STACKINC:
3904 ws->l_state.vertical_stacks++;
3906 case SWM_ARG_ID_STACKDEC:
3907 if (ws->l_state.vertical_stacks > 1)
3908 ws->l_state.vertical_stacks--;
3910 case SWM_ARG_ID_FLIPLAYOUT:
3911 ws->l_state.vertical_flip = !ws->l_state.vertical_flip;
3919 vertical_stack(struct workspace *ws, struct swm_geometry *g)
3921 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
3923 stack_master(ws, g, 0, ws->l_state.vertical_flip);
3927 horizontal_config(struct workspace *ws, int id)
3929 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
3932 case SWM_ARG_ID_STACKRESET:
3933 case SWM_ARG_ID_STACKINIT:
3934 ws->l_state.horizontal_mwin = 1;
3935 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
3936 ws->l_state.horizontal_stacks = 1;
3938 case SWM_ARG_ID_MASTERSHRINK:
3939 if (ws->l_state.horizontal_msize > 1)
3940 ws->l_state.horizontal_msize--;
3942 case SWM_ARG_ID_MASTERGROW:
3943 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
3944 ws->l_state.horizontal_msize++;
3946 case SWM_ARG_ID_MASTERADD:
3947 ws->l_state.horizontal_mwin++;
3949 case SWM_ARG_ID_MASTERDEL:
3950 if (ws->l_state.horizontal_mwin > 0)
3951 ws->l_state.horizontal_mwin--;
3953 case SWM_ARG_ID_STACKINC:
3954 ws->l_state.horizontal_stacks++;
3956 case SWM_ARG_ID_STACKDEC:
3957 if (ws->l_state.horizontal_stacks > 1)
3958 ws->l_state.horizontal_stacks--;
3960 case SWM_ARG_ID_FLIPLAYOUT:
3961 ws->l_state.horizontal_flip = !ws->l_state.horizontal_flip;
3969 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
3971 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
3973 stack_master(ws, g, 1, ws->l_state.horizontal_flip);
3976 /* fullscreen view */
3978 max_stack(struct workspace *ws, struct swm_geometry *g)
3980 struct swm_geometry gg = *g;
3981 struct ws_win *w, *win = NULL, *parent = NULL;
3982 int winno, num_screens;
3984 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
3989 winno = count_win(ws, 0);
3990 if (winno == 0 && count_win(ws, 1) == 0)
3993 /* Figure out which top level window should be visible. */
3994 if (ws->focus_pending)
3995 win = ws->focus_pending;
3999 win = TAILQ_FIRST(&ws->winlist);
4002 parent = find_window(win->transient);
4004 DNPRINTF(SWM_D_STACK, "max_stack: win: 0x%x\n", win->id);
4006 /* maximize all top level windows */
4007 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
4008 TAILQ_FOREACH(w, &ws->winlist, entry) {
4009 if (w->transient || w->iconic)
4012 if (w->floating && !w->floatmaxed) {
4014 * retain geometry for retrieval on exit from
4017 store_float_geom(w, ws->r);
4021 /* only reconfigure if necessary */
4022 if (X(w) != gg.x || Y(w) != gg.y || WIDTH(w) != gg.w ||
4023 HEIGHT(w) != gg.h) {
4029 WIDTH(w) += 2 * border_width;
4030 HEIGHT(w) += 2 * border_width;
4036 /* Unmap unwanted windows if not multi-screen. */
4037 if (!(num_screens > 1 || outputs > 1) && (w != win ||
4038 w != parent || w->transient != win->id))
4042 /* If a parent exists, map it first. */
4046 /* Map siblings next. */
4047 TAILQ_FOREACH(w, &ws->winlist, entry)
4048 if (w != win && !w->iconic &&
4049 w->transient == parent->id) {
4050 stack_floater(w, ws->r);
4055 /* Map focused window. */
4058 /* Finally, map children of focus window. */
4059 TAILQ_FOREACH(w, &ws->winlist, entry)
4060 if (w->transient == win->id && !w->iconic) {
4061 stack_floater(w, ws->r);
4067 send_to_ws(struct swm_region *r, union arg *args)
4069 int wsid = args->id;
4070 struct ws_win *win = NULL, *parent;
4071 struct workspace *ws, *nws, *pws;
4072 char ws_idx_str[SWM_PROPLEN];
4074 if (wsid >= workspace_limit)
4077 if (r && r->ws && r->ws->focus)
4082 if (win->ws->idx == wsid)
4085 DNPRINTF(SWM_D_MOVE, "send_to_ws: win 0x%x, ws %d -> %d\n", win->id,
4086 win->ws->idx, wsid);
4089 nws = &win->s->ws[wsid];
4091 /* Update the window's workspace property: _SWM_WS */
4092 if (snprintf(ws_idx_str, SWM_PROPLEN, "%d", nws->idx) < SWM_PROPLEN) {
4093 if (focus_mode != SWM_FOCUS_FOLLOW)
4094 ws->focus_pending = get_focus_prev(win);
4096 /* Move the parent if this is a transient window. */
4097 if (win->transient) {
4098 parent = find_window(win->transient);
4101 /* Set new focus in parent's ws if needed. */
4102 if (pws->focus == parent) {
4103 if (focus_mode != SWM_FOCUS_FOLLOW)
4104 pws->focus_pending =
4105 get_focus_prev(parent);
4107 unfocus_win(parent);
4109 if (focus_mode != SWM_FOCUS_FOLLOW)
4110 pws->focus = pws->focus_pending;
4112 if (focus_mode != SWM_FOCUS_FOLLOW)
4113 pws->focus_pending = NULL;
4116 /* Don't unmap parent if new ws is visible */
4118 unmap_window(parent);
4121 TAILQ_REMOVE(&ws->winlist, parent, entry);
4122 TAILQ_INSERT_TAIL(&nws->winlist, parent, entry);
4125 DNPRINTF(SWM_D_PROP, "send_to_ws: set "
4126 "property: _SWM_WS: %s\n", ws_idx_str);
4127 xcb_change_property(conn, XCB_PROP_MODE_REPLACE,
4128 parent->id, a_swm_ws, XCB_ATOM_STRING, 8,
4129 strlen(ws_idx_str), ws_idx_str);
4135 /* Don't unmap if new ws is visible */
4140 TAILQ_REMOVE(&ws->winlist, win, entry);
4141 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
4144 /* Set focus on new ws. */
4145 unfocus_win(nws->focus);
4148 DNPRINTF(SWM_D_PROP, "send_to_ws: set property: _SWM_WS: %s\n",
4150 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
4151 a_swm_ws, XCB_ATOM_STRING, 8, strlen(ws_idx_str),
4154 /* Restack and set new focus. */
4157 if (focus_mode != SWM_FOCUS_FOLLOW) {
4158 focus_win(ws->focus_pending);
4159 ws->focus_pending = NULL;
4165 DNPRINTF(SWM_D_MOVE, "send_to_ws: done.\n");
4169 pressbutton(struct swm_region *r, union arg *args)
4171 /* suppress unused warning since var is needed */
4174 xcb_test_fake_input(conn, XCB_BUTTON_PRESS, args->id,
4175 XCB_CURRENT_TIME, XCB_WINDOW_NONE, 0, 0, 0);
4176 xcb_test_fake_input(conn, XCB_BUTTON_RELEASE, args->id,
4177 XCB_CURRENT_TIME, XCB_WINDOW_NONE, 0, 0, 0);
4181 raise_toggle(struct swm_region *r, union arg *args)
4183 /* suppress unused warning since var is needed */
4186 if (r == NULL || r->ws == NULL)
4189 r->ws->always_raise = !r->ws->always_raise;
4191 /* bring floaters back to top */
4192 if (!r->ws->always_raise)
4199 iconify(struct swm_region *r, union arg *args)
4201 /* suppress unused warning since var is needed */
4204 if (r->ws->focus == NULL)
4207 set_swm_iconic(r->ws->focus, 1);
4213 get_win_name(xcb_window_t win)
4216 xcb_get_property_cookie_t c;
4217 xcb_get_property_reply_t *r;
4219 /* First try _NET_WM_NAME for UTF-8. */
4220 c = xcb_get_property(conn, 0, win, a_netwmname,
4221 XCB_GET_PROPERTY_TYPE_ANY, 0, UINT_MAX);
4222 r = xcb_get_property_reply(conn, c, NULL);
4224 if (!r || r->type == XCB_NONE) {
4226 /* Use WM_NAME instead; no UTF-8. */
4227 c = xcb_get_property(conn, 0, win, XCB_ATOM_WM_NAME,
4228 XCB_GET_PROPERTY_TYPE_ANY, 0, UINT_MAX);
4229 r = xcb_get_property_reply(conn, c, NULL);
4231 if(!r || r->type == XCB_NONE) {
4238 name = strndup(xcb_get_property_value(r),
4239 xcb_get_property_value_length(r));
4246 uniconify(struct swm_region *r, union arg *args)
4253 DNPRINTF(SWM_D_MISC, "uniconify\n");
4255 if (r == NULL || r->ws == NULL)
4258 /* make sure we have anything to uniconify */
4259 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
4260 if (win->ws == NULL)
4261 continue; /* should never happen */
4270 search_resp_action = SWM_SEARCH_UNICONIFY;
4272 spawn_select(r, args, "search", &searchpid);
4274 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
4277 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
4278 if (win->ws == NULL)
4279 continue; /* should never happen */
4283 name = get_win_name(win->id);
4286 fprintf(lfile, "%s.%u\n", name, win->id);
4294 name_workspace(struct swm_region *r, union arg *args)
4298 DNPRINTF(SWM_D_MISC, "name_workspace\n");
4304 search_resp_action = SWM_SEARCH_NAME_WORKSPACE;
4306 spawn_select(r, args, "name_workspace", &searchpid);
4308 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
4311 fprintf(lfile, "%s", "");
4316 search_workspace(struct swm_region *r, union arg *args)
4319 struct workspace *ws;
4322 DNPRINTF(SWM_D_MISC, "search_workspace\n");
4328 search_resp_action = SWM_SEARCH_SEARCH_WORKSPACE;
4330 spawn_select(r, args, "search", &searchpid);
4332 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
4335 for (i = 0; i < workspace_limit; i++) {
4339 fprintf(lfile, "%d%s%s\n", ws->idx + 1,
4340 (ws->name ? ":" : ""), (ws->name ? ws->name : ""));
4347 search_win_cleanup(void)
4349 struct search_window *sw = NULL;
4351 while ((sw = TAILQ_FIRST(&search_wl)) != NULL) {
4352 xcb_destroy_window(conn, sw->indicator);
4353 TAILQ_REMOVE(&search_wl, sw, entry);
4359 search_win(struct swm_region *r, union arg *args)
4361 struct ws_win *win = NULL;
4362 struct search_window *sw = NULL;
4365 int i, width, height;
4373 XRectangle l_ibox, l_lbox;
4375 DNPRINTF(SWM_D_MISC, "search_win\n");
4378 search_resp_action = SWM_SEARCH_SEARCH_WINDOW;
4380 spawn_select(r, args, "search", &searchpid);
4382 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
4385 TAILQ_INIT(&search_wl);
4388 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
4392 sw = calloc(1, sizeof(struct search_window));
4394 warn("search_win: calloc");
4396 search_win_cleanup();
4402 snprintf(s, sizeof s, "%d", i);
4405 w = xcb_generate_id(conn);
4406 wa[0] = r->s->c[SWM_S_COLOR_FOCUS].pixel;
4407 wa[1] = r->s->c[SWM_S_COLOR_UNFOCUS].pixel;
4409 if (bar_font_legacy) {
4410 XmbTextExtents(bar_fs, s, len, &l_ibox, &l_lbox);
4411 width = l_lbox.width + 4;
4412 height = bar_fs_extents->max_logical_extent.height + 4;
4414 XftTextExtentsUtf8(display, bar_font, (FcChar8 *)s, len,
4416 width = info.width + 4;
4417 height = bar_font->height + 4;
4420 xcb_create_window(conn, XCB_COPY_FROM_PARENT, w, win->id, 0, 0,
4421 width, height, 1, XCB_WINDOW_CLASS_INPUT_OUTPUT,
4422 XCB_COPY_FROM_PARENT, XCB_CW_BACK_PIXEL |
4423 XCB_CW_BORDER_PIXEL, wa);
4425 xcb_map_window(conn, w);
4428 TAILQ_INSERT_TAIL(&search_wl, sw, entry);
4430 if (bar_font_legacy) {
4431 l_gcv.graphics_exposures = 0;
4432 l_draw = XCreateGC(display, w, 0, &l_gcv);
4434 XSetForeground(display, l_draw,
4435 r->s->c[SWM_S_COLOR_BAR].pixel);
4437 DRAWSTRING(display, w, bar_fs, l_draw, 2,
4438 (bar_fs_extents->max_logical_extent.height -
4439 l_lbox.height) / 2 - l_lbox.y, s, len);
4441 XFreeGC(display, l_draw);
4444 draw = XftDrawCreate(display, w,
4445 DefaultVisual(display, r->s->idx),
4446 DefaultColormap(display, r->s->idx));
4448 XftDrawStringUtf8(draw, &bar_font_color, bar_font, 2,
4449 (HEIGHT(r->bar) + bar_font->height) / 2 -
4450 bar_font->descent, (FcChar8 *)s, len);
4452 XftDrawDestroy(draw);
4455 DNPRINTF(SWM_D_MISC, "search_win: mapped window: 0x%x\n", w);
4457 fprintf(lfile, "%d\n", i);
4467 search_resp_uniconify(const char *resp, unsigned long len)
4473 DNPRINTF(SWM_D_MISC, "search_resp_uniconify: resp: %s\n", resp);
4475 TAILQ_FOREACH(win, &search_r->ws->winlist, entry) {
4478 name = get_win_name(win->id);
4481 if (asprintf(&s, "%s.%u", name, win->id) == -1) {
4486 if (strncmp(s, resp, len) == 0) {
4487 /* XXX this should be a callback to generalize */
4488 set_swm_iconic(win, 0);
4497 search_resp_name_workspace(const char *resp, unsigned long len)
4499 struct workspace *ws;
4501 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: resp: %s\n", resp);
4503 if (search_r->ws == NULL)
4508 free(search_r->ws->name);
4509 search_r->ws->name = NULL;
4513 ws->name = strdup(resp);
4514 if (ws->name == NULL) {
4515 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: "
4516 "strdup: %s", strerror(errno));
4523 search_resp_search_workspace(const char *resp)
4530 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: resp: %s\n", resp);
4534 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: strdup: %s",
4541 ws_idx = (int)strtonum(q, 1, workspace_limit, &errstr);
4543 DNPRINTF(SWM_D_MISC, "workspace idx is %s: %s",
4550 switchws(search_r, &a);
4554 search_resp_search_window(const char *resp)
4559 struct search_window *sw;
4561 DNPRINTF(SWM_D_MISC, "search_resp_search_window: resp: %s\n", resp);
4565 DNPRINTF(SWM_D_MISC, "search_resp_search_window: strdup: %s",
4570 idx = (int)strtonum(s, 1, INT_MAX, &errstr);
4572 DNPRINTF(SWM_D_MISC, "window idx is %s: %s",
4579 TAILQ_FOREACH(sw, &search_wl, entry)
4580 if (idx == sw->idx) {
4586 #define MAX_RESP_LEN 1024
4589 search_do_resp(void)
4595 DNPRINTF(SWM_D_MISC, "search_do_resp:\n");
4600 if ((resp = calloc(1, MAX_RESP_LEN + 1)) == NULL) {
4601 warn("search: calloc");
4605 rbytes = read(select_resp_pipe[0], resp, MAX_RESP_LEN);
4607 warn("search: read error");
4610 resp[rbytes] = '\0';
4613 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
4614 * newline, so work around that by sanitizing the resp now.
4616 resp[strcspn(resp, "\n")] = '\0';
4619 switch (search_resp_action) {
4620 case SWM_SEARCH_UNICONIFY:
4621 search_resp_uniconify(resp, len);
4623 case SWM_SEARCH_NAME_WORKSPACE:
4624 search_resp_name_workspace(resp, len);
4626 case SWM_SEARCH_SEARCH_WORKSPACE:
4627 search_resp_search_workspace(resp);
4629 case SWM_SEARCH_SEARCH_WINDOW:
4630 search_resp_search_window(resp);
4635 if (search_resp_action == SWM_SEARCH_SEARCH_WINDOW)
4636 search_win_cleanup();
4638 search_resp_action = SWM_SEARCH_NONE;
4639 close(select_resp_pipe[0]);
4646 wkill(struct swm_region *r, union arg *args)
4648 DNPRINTF(SWM_D_MISC, "wkill: id: %d\n", args->id);
4650 if (r->ws->focus == NULL)
4653 if (args->id == SWM_ARG_ID_KILLWINDOW)
4654 xcb_kill_client(conn, r->ws->focus->id);
4656 if (r->ws->focus->can_delete)
4657 client_msg(r->ws->focus, a_delete, 0);
4663 floating_toggle_win(struct ws_win *win)
4665 struct swm_region *r;
4675 /* reject floating toggles in max stack mode */
4676 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK])
4679 if (win->floating) {
4680 if (!win->floatmaxed) {
4681 /* retain position for refloat */
4682 store_float_geom(win, r);
4686 load_float_geom(win, r);
4690 ewmh_update_actions(win);
4696 floating_toggle(struct swm_region *r, union arg *args)
4698 struct ws_win *win = r->ws->focus;
4700 /* suppress unused warning since var is needed */
4706 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
4709 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
4710 _NET_WM_STATE_TOGGLE);
4714 if (win == win->ws->focus)
4721 constrain_window(struct ws_win *win, struct swm_region *r, int resizable)
4723 if (MAX_X(win) + BORDER(win) > MAX_X(r)) {
4725 WIDTH(win) = MAX_X(r) - X(win) - BORDER(win);
4727 X(win) = MAX_X(r)- WIDTH(win) - BORDER(win);
4730 if (X(win) + BORDER(win) < X(r)) {
4732 WIDTH(win) -= X(r) - X(win) - BORDER(win);
4734 X(win) = X(r) - BORDER(win);
4737 if (MAX_Y(win) + BORDER(win) > MAX_Y(r)) {
4739 HEIGHT(win) = MAX_Y(r) - Y(win) - BORDER(win);
4741 Y(win) = MAX_Y(r) - HEIGHT(win) - BORDER(win);
4744 if (Y(win) + BORDER(win) < Y(r)) {
4746 HEIGHT(win) -= Y(r) - Y(win) - BORDER(win);
4748 Y(win) = Y(r) - BORDER(win);
4754 if (HEIGHT(win) < 1)
4760 update_window(struct ws_win *win)
4765 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
4766 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
4767 XCB_CONFIG_WINDOW_BORDER_WIDTH;
4771 wc[3] = HEIGHT(win);
4772 wc[4] = BORDER(win);
4774 DNPRINTF(SWM_D_EVENT, "update_window: window: 0x%x, (x,y) w x h: "
4775 "(%d,%d) %d x %d, bordered: %s\n", win->id, wc[0], wc[1], wc[2],
4776 wc[3], YESNO(win->bordered));
4778 xcb_configure_window(conn, win->id, mask, wc);
4781 #define SWM_RESIZE_STEPS (50)
4784 resize(struct ws_win *win, union arg *args)
4786 xcb_timestamp_t timestamp = 0;
4787 struct swm_region *r = NULL;
4788 int resize_step = 0;
4789 struct swm_geometry g;
4790 int top = 0, left = 0, resizing;
4792 xcb_cursor_t cursor;
4793 xcb_query_pointer_reply_t *xpr;
4794 xcb_generic_event_t *evt;
4795 xcb_motion_notify_event_t *mne;
4801 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
4804 DNPRINTF(SWM_D_EVENT, "resize: window: 0x%x, floating: %s, "
4805 "transient: 0x%x\n", win->id, YESNO(win->floating),
4808 if (!win->transient && !win->floating)
4811 /* reject resizes in max mode for floaters (transient ok) */
4812 if (win->floatmaxed)
4816 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
4824 case SWM_ARG_ID_WIDTHSHRINK:
4825 WIDTH(win) -= SWM_RESIZE_STEPS;
4828 case SWM_ARG_ID_WIDTHGROW:
4829 WIDTH(win) += SWM_RESIZE_STEPS;
4832 case SWM_ARG_ID_HEIGHTSHRINK:
4833 HEIGHT(win) -= SWM_RESIZE_STEPS;
4836 case SWM_ARG_ID_HEIGHTGROW:
4837 HEIGHT(win) += SWM_RESIZE_STEPS;
4844 constrain_window(win, r, 1);
4846 store_float_geom(win,r);
4850 /* get cursor offset from window root */
4851 xpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, win->id),
4858 if (xpr->win_x < WIDTH(win) / 2)
4861 if (xpr->win_y < HEIGHT(win) / 2)
4864 if (args->id == SWM_ARG_ID_CENTER)
4865 cursor = cursors[XC_SIZING].cid;
4867 cursor = cursors[left ? XC_TOP_LEFT_CORNER :
4868 XC_TOP_RIGHT_CORNER].cid;
4870 cursor = cursors[left ? XC_BOTTOM_LEFT_CORNER :
4871 XC_BOTTOM_RIGHT_CORNER].cid;
4873 xcb_grab_pointer(conn, 0, win->id, MOUSEMASK,
4874 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, XCB_WINDOW_NONE, cursor,
4879 while ((evt = xcb_wait_for_event(conn)) && resizing) {
4880 switch (XCB_EVENT_RESPONSE_TYPE(evt)) {
4881 case XCB_BUTTON_RELEASE:
4882 DNPRINTF(SWM_D_EVENT, "resize: BUTTON_RELEASE\n");
4885 case XCB_MOTION_NOTIFY:
4886 mne = (xcb_motion_notify_event_t *)evt;
4887 /* cursor offset/delta from start of the operation */
4888 dx = mne->root_x - xpr->root_x;
4889 dy = mne->root_y - xpr->root_y;
4895 if (args->id == SWM_ARG_ID_CENTER) {
4896 if (g.h / 2 + dy < 1)
4900 HEIGHT(win) = g.h + 2 * dy;
4908 HEIGHT(win) = g.h + dy;
4915 if (args->id == SWM_ARG_ID_CENTER) {
4916 if (g.w / 2 + dx < 1)
4920 WIDTH(win) = g.w + 2 * dx;
4928 WIDTH(win) = g.w + dx;
4931 constrain_window(win, r, 1);
4933 /* not free, don't sync more than 120 times / second */
4934 if ((mne->time - timestamp) > (1000 / 120) ) {
4935 timestamp = mne->time;
4950 store_float_geom(win,r);
4952 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
4954 DNPRINTF(SWM_D_EVENT, "resize: done.\n");
4958 resize_step(struct swm_region *r, union arg *args)
4960 struct ws_win *win = NULL;
4962 if (r && r->ws && r->ws->focus)
4971 #define SWM_MOVE_STEPS (50)
4974 move(struct ws_win *win, union arg *args)
4976 xcb_timestamp_t timestamp = 0;
4977 int move_step = 0, moving;
4978 struct swm_region *r = NULL;
4979 xcb_query_pointer_reply_t *qpr;
4980 xcb_generic_event_t *evt;
4981 xcb_motion_notify_event_t *mne;
4987 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
4990 DNPRINTF(SWM_D_EVENT, "move: window: 0x%x, floating: %s, transient: "
4991 "0x%x\n", win->id, YESNO(win->floating), win->transient);
4993 /* in max_stack mode should only move transients */
4994 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !win->transient)
4998 if (!win->floating && !win->transient) {
4999 store_float_geom(win, r);
5000 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
5003 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
5012 case SWM_ARG_ID_MOVELEFT:
5013 X(win) -= (SWM_MOVE_STEPS - border_width);
5016 case SWM_ARG_ID_MOVERIGHT:
5017 X(win) += (SWM_MOVE_STEPS - border_width);
5020 case SWM_ARG_ID_MOVEUP:
5021 Y(win) -= (SWM_MOVE_STEPS - border_width);
5024 case SWM_ARG_ID_MOVEDOWN:
5025 Y(win) += (SWM_MOVE_STEPS - border_width);
5032 constrain_window(win, r, 0);
5034 store_float_geom(win, r);
5038 xcb_grab_pointer(conn, 0, win->id, MOUSEMASK,
5039 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC,
5040 XCB_WINDOW_NONE, cursors[XC_FLEUR].cid, XCB_CURRENT_TIME);
5042 /* get cursor offset from window root */
5043 qpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, win->id),
5046 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
5052 while ((evt = xcb_wait_for_event(conn)) && moving) {
5053 switch (XCB_EVENT_RESPONSE_TYPE(evt)) {
5054 case XCB_BUTTON_RELEASE:
5055 DNPRINTF(SWM_D_EVENT, "move: BUTTON_RELEASE\n");
5058 case XCB_MOTION_NOTIFY:
5059 mne = (xcb_motion_notify_event_t *)evt;
5060 X(win) = mne->root_x - qpr->win_x - border_width;
5061 Y(win) = mne->root_y - qpr->win_y - border_width;
5063 constrain_window(win, r, 0);
5065 /* not free, don't sync more than 120 times / second */
5066 if ((mne->time - timestamp) > (1000 / 120) ) {
5067 timestamp = mne->time;
5082 store_float_geom(win, r);
5084 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
5085 DNPRINTF(SWM_D_EVENT, "move: done.\n");
5089 move_step(struct swm_region *r, union arg *args)
5091 struct ws_win *win = NULL;
5093 if (r && r->ws && r->ws->focus)
5098 if (!win->transient && !win->floating)
5105 /* user/key callable function IDs */
5155 KF_SEARCH_WORKSPACE,
5196 KF_DUMPWINS, /* MUST BE LAST */
5200 /* key definitions */
5202 char name[SWM_FUNCNAME_LEN];
5203 void (*func)(struct swm_region *r, union arg *);
5205 } keyfuncs[KF_INVALID + 1] = {
5206 /* name function argument */
5207 { "bar_toggle", bar_toggle, {0} },
5208 { "button2", pressbutton, {2} },
5209 { "cycle_layout", cycle_layout, {0} },
5210 { "flip_layout", stack_config, {.id = SWM_ARG_ID_FLIPLAYOUT} },
5211 { "float_toggle", floating_toggle,{0} },
5212 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
5213 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
5214 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
5215 { "height_grow", resize_step, {.id = SWM_ARG_ID_HEIGHTGROW} },
5216 { "height_shrink", resize_step, {.id = SWM_ARG_ID_HEIGHTSHRINK} },
5217 { "iconify", iconify, {0} },
5218 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
5219 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
5220 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
5221 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
5222 { "move_down", move_step, {.id = SWM_ARG_ID_MOVEDOWN} },
5223 { "move_left", move_step, {.id = SWM_ARG_ID_MOVELEFT} },
5224 { "move_right", move_step, {.id = SWM_ARG_ID_MOVERIGHT} },
5225 { "move_up", move_step, {.id = SWM_ARG_ID_MOVEUP} },
5226 { "mvws_1", send_to_ws, {.id = 0} },
5227 { "mvws_2", send_to_ws, {.id = 1} },
5228 { "mvws_3", send_to_ws, {.id = 2} },
5229 { "mvws_4", send_to_ws, {.id = 3} },
5230 { "mvws_5", send_to_ws, {.id = 4} },
5231 { "mvws_6", send_to_ws, {.id = 5} },
5232 { "mvws_7", send_to_ws, {.id = 6} },
5233 { "mvws_8", send_to_ws, {.id = 7} },
5234 { "mvws_9", send_to_ws, {.id = 8} },
5235 { "mvws_10", send_to_ws, {.id = 9} },
5236 { "mvws_11", send_to_ws, {.id = 10} },
5237 { "mvws_12", send_to_ws, {.id = 11} },
5238 { "mvws_13", send_to_ws, {.id = 12} },
5239 { "mvws_14", send_to_ws, {.id = 13} },
5240 { "mvws_15", send_to_ws, {.id = 14} },
5241 { "mvws_16", send_to_ws, {.id = 15} },
5242 { "mvws_17", send_to_ws, {.id = 16} },
5243 { "mvws_18", send_to_ws, {.id = 17} },
5244 { "mvws_19", send_to_ws, {.id = 18} },
5245 { "mvws_20", send_to_ws, {.id = 19} },
5246 { "mvws_21", send_to_ws, {.id = 20} },
5247 { "mvws_22", send_to_ws, {.id = 21} },
5248 { "name_workspace", name_workspace, {0} },
5249 { "quit", quit, {0} },
5250 { "raise_toggle", raise_toggle, {0} },
5251 { "restart", restart, {0} },
5252 { "screen_next", cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
5253 { "screen_prev", cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
5254 { "search_win", search_win, {0} },
5255 { "search_workspace", search_workspace, {0} },
5256 { "spawn_custom", NULL, {0} },
5257 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
5258 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
5259 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
5260 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
5261 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
5262 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
5263 { "uniconify", uniconify, {0} },
5264 { "version", version, {0} },
5265 { "width_grow", resize_step, {.id = SWM_ARG_ID_WIDTHGROW} },
5266 { "width_shrink", resize_step, {.id = SWM_ARG_ID_WIDTHSHRINK} },
5267 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
5268 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
5269 { "ws_1", switchws, {.id = 0} },
5270 { "ws_2", switchws, {.id = 1} },
5271 { "ws_3", switchws, {.id = 2} },
5272 { "ws_4", switchws, {.id = 3} },
5273 { "ws_5", switchws, {.id = 4} },
5274 { "ws_6", switchws, {.id = 5} },
5275 { "ws_7", switchws, {.id = 6} },
5276 { "ws_8", switchws, {.id = 7} },
5277 { "ws_9", switchws, {.id = 8} },
5278 { "ws_10", switchws, {.id = 9} },
5279 { "ws_11", switchws, {.id = 10} },
5280 { "ws_12", switchws, {.id = 11} },
5281 { "ws_13", switchws, {.id = 12} },
5282 { "ws_14", switchws, {.id = 13} },
5283 { "ws_15", switchws, {.id = 14} },
5284 { "ws_16", switchws, {.id = 15} },
5285 { "ws_17", switchws, {.id = 16} },
5286 { "ws_18", switchws, {.id = 17} },
5287 { "ws_19", switchws, {.id = 18} },
5288 { "ws_20", switchws, {.id = 19} },
5289 { "ws_21", switchws, {.id = 20} },
5290 { "ws_22", switchws, {.id = 21} },
5291 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
5292 { "ws_next_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP_ALL} },
5293 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
5294 { "ws_prev_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN_ALL} },
5295 { "ws_prior", priorws, {0} },
5296 { "dumpwins", dumpwins, {0} }, /* MUST BE LAST */
5297 { "invalid key func", NULL, {0} },
5300 RB_ENTRY(key) entry;
5303 enum keyfuncid funcid;
5306 RB_HEAD(key_tree, key);
5309 key_cmp(struct key *kp1, struct key *kp2)
5311 if (kp1->keysym < kp2->keysym)
5313 if (kp1->keysym > kp2->keysym)
5316 if (kp1->mod < kp2->mod)
5318 if (kp1->mod > kp2->mod)
5324 RB_GENERATE(key_tree, key, entry, key_cmp);
5325 struct key_tree keys;
5328 enum { client_click, root_click };
5330 unsigned int action;
5332 unsigned int button;
5333 void (*func)(struct ws_win *, union arg *);
5336 #define MODKEY_SHIFT MODKEY | XCB_MOD_MASK_SHIFT
5337 /* action key mouse button func args */
5338 { client_click, MODKEY, XCB_BUTTON_INDEX_3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
5339 { client_click, MODKEY_SHIFT, XCB_BUTTON_INDEX_3, resize, {.id = SWM_ARG_ID_CENTER} },
5340 { client_click, MODKEY, XCB_BUTTON_INDEX_1, move, {0} },
5345 update_modkey(unsigned int mod)
5351 RB_FOREACH(kp, key_tree, &keys)
5352 if (kp->mod & XCB_MOD_MASK_SHIFT)
5353 kp->mod = mod | XCB_MOD_MASK_SHIFT;
5357 for (i = 0; i < LENGTH(buttons); i++)
5358 if (buttons[i].mask & XCB_MOD_MASK_SHIFT)
5359 buttons[i].mask = mod | XCB_MOD_MASK_SHIFT;
5361 buttons[i].mask = mod;
5366 TAILQ_ENTRY(spawn_prog) entry;
5371 TAILQ_HEAD(spawn_list, spawn_prog);
5372 struct spawn_list spawns = TAILQ_HEAD_INITIALIZER(spawns);
5375 spawn_expand(struct swm_region *r, union arg *args, const char *spawn_name,
5378 struct spawn_prog *prog = NULL;
5380 char *ap, **real_args;
5382 /* suppress unused warning since var is needed */
5385 DNPRINTF(SWM_D_SPAWN, "spawn_expand: %s\n", spawn_name);
5388 TAILQ_FOREACH(prog, &spawns, entry) {
5389 if (!strcasecmp(spawn_name, prog->name))
5393 warnx("spawn_custom: program %s not found", spawn_name);
5397 /* make room for expanded args */
5398 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
5399 err(1, "spawn_custom: calloc real_args");
5401 /* expand spawn_args into real_args */
5402 for (i = 0; i < prog->argc; i++) {
5404 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg: %s\n", ap);
5405 if (!strcasecmp(ap, "$bar_border")) {
5407 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
5409 err(1, "spawn_custom border color");
5410 } else if (!strcasecmp(ap, "$bar_color")) {
5412 strdup(r->s->c[SWM_S_COLOR_BAR].name))
5414 err(1, "spawn_custom bar color");
5415 } else if (!strcasecmp(ap, "$bar_font")) {
5416 if ((real_args[i] = strdup(bar_fonts))
5418 err(1, "spawn_custom bar fonts");
5419 } else if (!strcasecmp(ap, "$bar_font_color")) {
5421 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
5423 err(1, "spawn_custom color font");
5424 } else if (!strcasecmp(ap, "$color_focus")) {
5426 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
5428 err(1, "spawn_custom color focus");
5429 } else if (!strcasecmp(ap, "$color_unfocus")) {
5431 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
5433 err(1, "spawn_custom color unfocus");
5435 /* no match --> copy as is */
5436 if ((real_args[i] = strdup(ap)) == NULL)
5437 err(1, "spawn_custom strdup(ap)");
5439 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg: %s\n",
5444 DNPRINTF(SWM_D_SPAWN, "spawn_custom: result: ");
5445 for (i = 0; i < prog->argc; i++)
5446 DNPRINTF(SWM_D_SPAWN, "\"%s\" ", real_args[i]);
5447 DNPRINTF(SWM_D_SPAWN, "\n");
5449 *ret_args = real_args;
5450 return (prog->argc);
5454 spawn_custom(struct swm_region *r, union arg *args, const char *spawn_name)
5460 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
5464 spawn(r->ws->idx, &a, 1);
5466 for (i = 0; i < spawn_argc; i++)
5472 spawn_select(struct swm_region *r, union arg *args, const char *spawn_name,
5479 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
5483 if (pipe(select_list_pipe) == -1)
5484 err(1, "pipe error");
5485 if (pipe(select_resp_pipe) == -1)
5486 err(1, "pipe error");
5488 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
5489 err(1, "could not disable SIGPIPE");
5490 switch (*pid = fork()) {
5492 err(1, "cannot fork");
5495 if (dup2(select_list_pipe[0], 0) == -1)
5497 if (dup2(select_resp_pipe[1], 1) == -1)
5499 close(select_list_pipe[1]);
5500 close(select_resp_pipe[0]);
5501 spawn(r->ws->idx, &a, 0);
5503 default: /* parent */
5504 close(select_list_pipe[0]);
5505 close(select_resp_pipe[1]);
5509 for (i = 0; i < spawn_argc; i++)
5515 spawn_insert(const char *name, const char *args)
5517 char *arg, *cp, *ptr;
5518 struct spawn_prog *sp;
5520 DNPRINTF(SWM_D_SPAWN, "spawn_insert: %s\n", name);
5522 if ((sp = calloc(1, sizeof *sp)) == NULL)
5523 err(1, "spawn_insert: calloc");
5524 if ((sp->name = strdup(name)) == NULL)
5525 err(1, "spawn_insert: strdup");
5527 /* convert the arguments to an argument list */
5528 if ((ptr = cp = strdup(args)) == NULL)
5529 err(1, "spawn_insert: strdup");
5530 while ((arg = strsep(&ptr, " \t")) != NULL) {
5531 /* empty field; skip it */
5536 if ((sp->argv = realloc(sp->argv, sp->argc *
5537 sizeof *sp->argv)) == NULL)
5538 err(1, "spawn_insert: realloc");
5539 if ((sp->argv[sp->argc - 1] = strdup(arg)) == NULL)
5540 err(1, "spawn_insert: strdup");
5544 TAILQ_INSERT_TAIL(&spawns, sp, entry);
5545 DNPRINTF(SWM_D_SPAWN, "spawn_insert: leave\n");
5549 spawn_remove(struct spawn_prog *sp)
5553 DNPRINTF(SWM_D_SPAWN, "spawn_remove: %s\n", sp->name);
5555 TAILQ_REMOVE(&spawns, sp, entry);
5556 for (i = 0; i < sp->argc; i++)
5562 DNPRINTF(SWM_D_SPAWN, "spawn_remove: leave\n");
5566 spawn_replace(struct spawn_prog *sp, const char *name, const char *args)
5568 DNPRINTF(SWM_D_SPAWN, "spawn_replace: %s [%s]\n", sp->name, name);
5571 spawn_insert(name, args);
5573 DNPRINTF(SWM_D_SPAWN, "spawn_replace: leave\n");
5577 setspawn(const char *name, const char *args)
5579 struct spawn_prog *sp;
5581 DNPRINTF(SWM_D_SPAWN, "setspawn: %s\n", name);
5586 TAILQ_FOREACH(sp, &spawns, entry) {
5587 if (!strcmp(sp->name, name)) {
5591 spawn_replace(sp, name, args);
5592 DNPRINTF(SWM_D_SPAWN, "setspawn: leave\n");
5596 if (*args == '\0') {
5597 warnx("error: setspawn: cannot find program: %s", name);
5601 spawn_insert(name, args);
5602 DNPRINTF(SWM_D_SPAWN, "setspawn: leave\n");
5606 setconfspawn(char *selector, char *value, int flags)
5610 /* suppress unused warning since var is needed */
5613 args = expand_tilde(value);
5615 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, args);
5617 setspawn(selector, args);
5620 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done.\n");
5627 setconfspawn("term", "xterm", 0);
5628 setconfspawn("spawn_term", "xterm", 0);
5629 setconfspawn("screenshot_all", "screenshot.sh full", 0);
5630 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
5631 setconfspawn("lock", "xlock", 0);
5632 setconfspawn("initscr", "initscreen.sh", 0);
5633 setconfspawn("menu", "dmenu_run"
5636 " -nf $bar_font_color"
5638 " -sf $bar_color", 0);
5639 setconfspawn("search", "dmenu"
5643 " -nf $bar_font_color"
5645 " -sf $bar_color", 0);
5646 setconfspawn("name_workspace", "dmenu"
5650 " -nf $bar_font_color"
5652 " -sf $bar_color", 0);
5656 #define SWM_MODNAME_SIZE 32
5657 #define SWM_KEY_WS "\n+ \t"
5659 parsekeys(char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
5663 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
5664 if (mod == NULL || ks == NULL) {
5665 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
5668 if (keystr == NULL || strlen(keystr) == 0) {
5669 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
5673 *ks = XCB_NO_SYMBOL;
5675 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
5676 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
5678 cp += (long)strspn(cp, SWM_KEY_WS);
5679 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
5681 else if (!strncasecmp(name, "Mod1", SWM_MODNAME_SIZE))
5682 *mod |= XCB_MOD_MASK_1;
5683 else if (!strncasecmp(name, "Mod2", SWM_MODNAME_SIZE))
5684 *mod += XCB_MOD_MASK_2;
5685 else if (!strncmp(name, "Mod3", SWM_MODNAME_SIZE))
5686 *mod |= XCB_MOD_MASK_3;
5687 else if (!strncmp(name, "Mod4", SWM_MODNAME_SIZE))
5688 *mod |= XCB_MOD_MASK_4;
5689 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
5690 *mod |= XCB_MOD_MASK_SHIFT;
5691 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
5692 *mod |= XCB_MOD_MASK_CONTROL;
5694 *ks = XStringToKeysym(name);
5695 XConvertCase(*ks, ks, &uks);
5696 if (ks == XCB_NO_SYMBOL) {
5698 "parsekeys: invalid key %s\n",
5704 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
5709 strdupsafe(const char *str)
5714 return (strdup(str));
5718 key_insert(unsigned int mod, KeySym ks, enum keyfuncid kfid,
5719 const char *spawn_name)
5723 DNPRINTF(SWM_D_KEY, "key_insert: enter %s [%s]\n",
5724 keyfuncs[kfid].name, spawn_name);
5726 if ((kp = malloc(sizeof *kp)) == NULL)
5727 err(1, "key_insert: malloc");
5732 kp->spawn_name = strdupsafe(spawn_name);
5733 RB_INSERT(key_tree, &keys, kp);
5735 DNPRINTF(SWM_D_KEY, "key_insert: leave\n");
5739 key_lookup(unsigned int mod, KeySym ks)
5746 return (RB_FIND(key_tree, &keys, &kp));
5750 key_remove(struct key *kp)
5752 DNPRINTF(SWM_D_KEY, "key_remove: %s\n", keyfuncs[kp->funcid].name);
5754 RB_REMOVE(key_tree, &keys, kp);
5755 free(kp->spawn_name);
5758 DNPRINTF(SWM_D_KEY, "key_remove: leave\n");
5762 key_replace(struct key *kp, unsigned int mod, KeySym ks, enum keyfuncid kfid,
5763 const char *spawn_name)
5765 DNPRINTF(SWM_D_KEY, "key_replace: %s [%s]\n", keyfuncs[kp->funcid].name,
5769 key_insert(mod, ks, kfid, spawn_name);
5771 DNPRINTF(SWM_D_KEY, "key_replace: leave\n");
5775 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid,
5776 const char *spawn_name)
5780 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
5781 keyfuncs[kfid].name, spawn_name);
5783 if ((kp = key_lookup(mod, ks)) != NULL) {
5784 if (kfid == KF_INVALID)
5787 key_replace(kp, mod, ks, kfid, spawn_name);
5788 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
5791 if (kfid == KF_INVALID) {
5792 warnx("error: setkeybinding: cannot find mod/key combination");
5793 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
5797 key_insert(mod, ks, kfid, spawn_name);
5798 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
5802 setconfbinding(char *selector, char *value, int flags)
5804 enum keyfuncid kfid;
5807 struct spawn_prog *sp;
5809 /* suppress unused warning since var is needed */
5812 DNPRINTF(SWM_D_KEY, "setconfbinding: enter\n");
5813 if (selector == NULL) {
5814 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
5815 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
5817 setkeybinding(mod, ks, kfid, NULL);
5822 /* search by key function name */
5823 for (kfid = 0; kfid < KF_INVALID; (kfid)++) {
5824 if (strncasecmp(selector, keyfuncs[kfid].name,
5825 SWM_FUNCNAME_LEN) == 0) {
5826 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
5828 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
5829 setkeybinding(mod, ks, kfid, NULL);
5835 /* search by custom spawn name */
5836 TAILQ_FOREACH(sp, &spawns, entry) {
5837 if (strcasecmp(selector, sp->name) == 0) {
5838 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
5840 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
5841 setkeybinding(mod, ks, KF_SPAWN_CUSTOM,
5848 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
5855 #define MODKEY_SHIFT MODKEY | XCB_MOD_MASK_SHIFT
5856 setkeybinding(MODKEY, XK_space, KF_CYCLE_LAYOUT,NULL);
5857 setkeybinding(MODKEY_SHIFT, XK_backslash, KF_FLIP_LAYOUT, NULL);
5858 setkeybinding(MODKEY_SHIFT, XK_space, KF_STACK_RESET, NULL);
5859 setkeybinding(MODKEY, XK_h, KF_MASTER_SHRINK, NULL);
5860 setkeybinding(MODKEY, XK_l, KF_MASTER_GROW, NULL);
5861 setkeybinding(MODKEY, XK_comma, KF_MASTER_ADD, NULL);
5862 setkeybinding(MODKEY, XK_period, KF_MASTER_DEL, NULL);
5863 setkeybinding(MODKEY_SHIFT, XK_comma, KF_STACK_INC, NULL);
5864 setkeybinding(MODKEY_SHIFT, XK_period, KF_STACK_DEC, NULL);
5865 setkeybinding(MODKEY, XK_Return, KF_SWAP_MAIN, NULL);
5866 setkeybinding(MODKEY, XK_j, KF_FOCUS_NEXT, NULL);
5867 setkeybinding(MODKEY, XK_k, KF_FOCUS_PREV, NULL);
5868 setkeybinding(MODKEY_SHIFT, XK_j, KF_SWAP_NEXT, NULL);
5869 setkeybinding(MODKEY_SHIFT, XK_k, KF_SWAP_PREV, NULL);
5870 setkeybinding(MODKEY_SHIFT, XK_Return, KF_SPAWN_CUSTOM,"term");
5871 setkeybinding(MODKEY, XK_p, KF_SPAWN_CUSTOM,"menu");
5872 setkeybinding(MODKEY_SHIFT, XK_q, KF_QUIT, NULL);
5873 setkeybinding(MODKEY, XK_q, KF_RESTART, NULL);
5874 setkeybinding(MODKEY, XK_m, KF_FOCUS_MAIN, NULL);
5875 setkeybinding(MODKEY, XK_1, KF_WS_1, NULL);
5876 setkeybinding(MODKEY, XK_2, KF_WS_2, NULL);
5877 setkeybinding(MODKEY, XK_3, KF_WS_3, NULL);
5878 setkeybinding(MODKEY, XK_4, KF_WS_4, NULL);
5879 setkeybinding(MODKEY, XK_5, KF_WS_5, NULL);
5880 setkeybinding(MODKEY, XK_6, KF_WS_6, NULL);
5881 setkeybinding(MODKEY, XK_7, KF_WS_7, NULL);
5882 setkeybinding(MODKEY, XK_8, KF_WS_8, NULL);
5883 setkeybinding(MODKEY, XK_9, KF_WS_9, NULL);
5884 setkeybinding(MODKEY, XK_0, KF_WS_10, NULL);
5885 setkeybinding(MODKEY, XK_F1, KF_WS_11, NULL);
5886 setkeybinding(MODKEY, XK_F2, KF_WS_12, NULL);
5887 setkeybinding(MODKEY, XK_F3, KF_WS_13, NULL);
5888 setkeybinding(MODKEY, XK_F4, KF_WS_14, NULL);
5889 setkeybinding(MODKEY, XK_F5, KF_WS_15, NULL);
5890 setkeybinding(MODKEY, XK_F6, KF_WS_16, NULL);
5891 setkeybinding(MODKEY, XK_F7, KF_WS_17, NULL);
5892 setkeybinding(MODKEY, XK_F8, KF_WS_18, NULL);
5893 setkeybinding(MODKEY, XK_F9, KF_WS_19, NULL);
5894 setkeybinding(MODKEY, XK_F10, KF_WS_20, NULL);
5895 setkeybinding(MODKEY, XK_F11, KF_WS_21, NULL);
5896 setkeybinding(MODKEY, XK_F12, KF_WS_22, NULL);
5897 setkeybinding(MODKEY, XK_Right, KF_WS_NEXT, NULL);
5898 setkeybinding(MODKEY, XK_Left, KF_WS_PREV, NULL);
5899 setkeybinding(MODKEY, XK_Up, KF_WS_NEXT_ALL, NULL);
5900 setkeybinding(MODKEY, XK_Down, KF_WS_PREV_ALL, NULL);
5901 setkeybinding(MODKEY, XK_a, KF_WS_PRIOR, NULL);
5902 setkeybinding(MODKEY_SHIFT, XK_Right, KF_SCREEN_NEXT, NULL);
5903 setkeybinding(MODKEY_SHIFT, XK_Left, KF_SCREEN_PREV, NULL);
5904 setkeybinding(MODKEY_SHIFT, XK_1, KF_MVWS_1, NULL);
5905 setkeybinding(MODKEY_SHIFT, XK_2, KF_MVWS_2, NULL);
5906 setkeybinding(MODKEY_SHIFT, XK_3, KF_MVWS_3, NULL);
5907 setkeybinding(MODKEY_SHIFT, XK_4, KF_MVWS_4, NULL);
5908 setkeybinding(MODKEY_SHIFT, XK_5, KF_MVWS_5, NULL);
5909 setkeybinding(MODKEY_SHIFT, XK_6, KF_MVWS_6, NULL);
5910 setkeybinding(MODKEY_SHIFT, XK_7, KF_MVWS_7, NULL);
5911 setkeybinding(MODKEY_SHIFT, XK_8, KF_MVWS_8, NULL);
5912 setkeybinding(MODKEY_SHIFT, XK_9, KF_MVWS_9, NULL);
5913 setkeybinding(MODKEY_SHIFT, XK_0, KF_MVWS_10, NULL);
5914 setkeybinding(MODKEY_SHIFT, XK_F1, KF_MVWS_11, NULL);
5915 setkeybinding(MODKEY_SHIFT, XK_F2, KF_MVWS_12, NULL);
5916 setkeybinding(MODKEY_SHIFT, XK_F3, KF_MVWS_13, NULL);
5917 setkeybinding(MODKEY_SHIFT, XK_F4, KF_MVWS_14, NULL);
5918 setkeybinding(MODKEY_SHIFT, XK_F5, KF_MVWS_15, NULL);
5919 setkeybinding(MODKEY_SHIFT, XK_F6, KF_MVWS_16, NULL);
5920 setkeybinding(MODKEY_SHIFT, XK_F7, KF_MVWS_17, NULL);
5921 setkeybinding(MODKEY_SHIFT, XK_F8, KF_MVWS_18, NULL);
5922 setkeybinding(MODKEY_SHIFT, XK_F9, KF_MVWS_19, NULL);
5923 setkeybinding(MODKEY_SHIFT, XK_F10, KF_MVWS_20, NULL);
5924 setkeybinding(MODKEY_SHIFT, XK_F11, KF_MVWS_21, NULL);
5925 setkeybinding(MODKEY_SHIFT, XK_F12, KF_MVWS_22, NULL);
5926 setkeybinding(MODKEY, XK_b, KF_BAR_TOGGLE, NULL);
5927 setkeybinding(MODKEY, XK_Tab, KF_FOCUS_NEXT, NULL);
5928 setkeybinding(MODKEY_SHIFT, XK_Tab, KF_FOCUS_PREV, NULL);
5929 setkeybinding(MODKEY_SHIFT, XK_x, KF_WIND_KILL, NULL);
5930 setkeybinding(MODKEY, XK_x, KF_WIND_DEL, NULL);
5931 setkeybinding(MODKEY, XK_s, KF_SPAWN_CUSTOM,"screenshot_all");
5932 setkeybinding(MODKEY_SHIFT, XK_s, KF_SPAWN_CUSTOM,"screenshot_wind");
5933 setkeybinding(MODKEY, XK_t, KF_FLOAT_TOGGLE,NULL);
5934 setkeybinding(MODKEY_SHIFT, XK_v, KF_VERSION, NULL);
5935 setkeybinding(MODKEY_SHIFT, XK_Delete, KF_SPAWN_CUSTOM,"lock");
5936 setkeybinding(MODKEY_SHIFT, XK_i, KF_SPAWN_CUSTOM,"initscr");
5937 setkeybinding(MODKEY, XK_w, KF_ICONIFY, NULL);
5938 setkeybinding(MODKEY_SHIFT, XK_w, KF_UNICONIFY, NULL);
5939 setkeybinding(MODKEY_SHIFT, XK_r, KF_RAISE_TOGGLE,NULL);
5940 setkeybinding(MODKEY, XK_v, KF_BUTTON2, NULL);
5941 setkeybinding(MODKEY, XK_equal, KF_WIDTH_GROW, NULL);
5942 setkeybinding(MODKEY, XK_minus, KF_WIDTH_SHRINK,NULL);
5943 setkeybinding(MODKEY_SHIFT, XK_equal, KF_HEIGHT_GROW,NULL);
5944 setkeybinding(MODKEY_SHIFT, XK_minus, KF_HEIGHT_SHRINK,NULL);
5945 setkeybinding(MODKEY, XK_bracketleft, KF_MOVE_LEFT,NULL);
5946 setkeybinding(MODKEY, XK_bracketright,KF_MOVE_RIGHT,NULL);
5947 setkeybinding(MODKEY_SHIFT, XK_bracketleft, KF_MOVE_UP, NULL);
5948 setkeybinding(MODKEY_SHIFT, XK_bracketright,KF_MOVE_DOWN,NULL);
5949 setkeybinding(MODKEY_SHIFT, XK_slash, KF_NAME_WORKSPACE,NULL);
5950 setkeybinding(MODKEY, XK_slash, KF_SEARCH_WORKSPACE,NULL);
5951 setkeybinding(MODKEY, XK_f, KF_SEARCH_WIN, NULL);
5953 setkeybinding(MODKEY_SHIFT, XK_d, KF_DUMPWINS, NULL);
5963 while (RB_EMPTY(&keys) == 0) {
5964 kp = RB_ROOT(&keys);
5970 setkeymapping(char *selector, char *value, int flags)
5972 char keymapping_file[PATH_MAX];
5974 /* suppress unused warnings since vars are needed */
5978 DNPRINTF(SWM_D_KEY, "setkeymapping: enter\n");
5979 if (value[0] == '~')
5980 snprintf(keymapping_file, sizeof keymapping_file, "%s/%s",
5981 pwd->pw_dir, &value[1]);
5983 strlcpy(keymapping_file, value, sizeof keymapping_file);
5985 /* load new key bindings; if it fails, revert to default bindings */
5986 if (conf_load(keymapping_file, SWM_CONF_KEYMAPPING)) {
5990 DNPRINTF(SWM_D_KEY, "setkeymapping: leave\n");
5995 updatenumlockmask(void)
5998 xcb_get_modifier_mapping_reply_t *modmap_r;
5999 xcb_keycode_t *modmap, kc, *keycode;
6003 modmap_r = xcb_get_modifier_mapping_reply(conn,
6004 xcb_get_modifier_mapping(conn),
6007 modmap = xcb_get_modifier_mapping_keycodes(modmap_r);
6008 for (i = 0; i < 8; i++) {
6009 for (j = 0; j < modmap_r->keycodes_per_modifier; j++) {
6010 kc = modmap[i * modmap_r->keycodes_per_modifier
6012 keycode = xcb_key_symbols_get_keycode(syms,
6015 numlockmask = (1 << i);
6021 DNPRINTF(SWM_D_MISC, "updatenumlockmask: %d\n", numlockmask);
6028 int num_screens, k, j;
6029 unsigned int modifiers[3];
6030 xcb_keycode_t *code;
6032 DNPRINTF(SWM_D_MISC, "grabkeys\n");
6033 updatenumlockmask();
6036 modifiers[1] = numlockmask;
6037 modifiers[2] = numlockmask | XCB_MOD_MASK_LOCK;
6039 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
6040 for (k = 0; k < num_screens; k++) {
6041 if (TAILQ_EMPTY(&screens[k].rl))
6043 xcb_ungrab_key(conn, XCB_GRAB_ANY, screens[k].root,
6045 RB_FOREACH(kp, key_tree, &keys) {
6046 if ((code = xcb_key_symbols_get_keycode(syms,
6048 for (j = 0; j < LENGTH(modifiers); j++)
6049 xcb_grab_key(conn, 1,
6051 kp->mod | modifiers[j],
6052 *code, XCB_GRAB_MODE_SYNC,
6053 XCB_GRAB_MODE_ASYNC);
6061 grabbuttons(struct ws_win *win)
6065 DNPRINTF(SWM_D_MOUSE, "grabbuttons: win 0x%x\n", win->id);
6067 for (i = 0; i < LENGTH(buttons); i++)
6068 if (buttons[i].action == client_click)
6069 xcb_grab_button(conn, 0, win->id, BUTTONMASK,
6070 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC,
6071 XCB_WINDOW_NONE, XCB_CURSOR_NONE,
6072 buttons[i].button, buttons[i].mask);
6075 const char *quirkname[] = {
6076 "NONE", /* config string for "no value" */
6085 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
6086 #define SWM_Q_WS "\n|+ \t"
6088 parsequirks(char *qstr, unsigned long *quirk)
6098 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
6100 cp += (long)strspn(cp, SWM_Q_WS);
6101 for (i = 0; i < LENGTH(quirkname); i++) {
6102 if (!strncasecmp(name, quirkname[i], SWM_QUIRK_LEN)) {
6103 DNPRINTF(SWM_D_QUIRK,
6104 "parsequirks: %s\n", name);
6109 *quirk |= 1 << (i-1);
6113 if (i >= LENGTH(quirkname)) {
6114 DNPRINTF(SWM_D_QUIRK,
6115 "parsequirks: invalid quirk [%s]\n", name);
6123 quirk_insert(const char *class, const char *name, unsigned long quirk)
6127 DNPRINTF(SWM_D_QUIRK, "quirk_insert: %s:%s [%lu]\n", class, name,
6130 if ((qp = malloc(sizeof *qp)) == NULL)
6131 err(1, "quirk_insert: malloc");
6132 if ((qp->class = strdup(class)) == NULL)
6133 err(1, "quirk_insert: strdup");
6134 if ((qp->name = strdup(name)) == NULL)
6135 err(1, "quirk_insert: strdup");
6138 TAILQ_INSERT_TAIL(&quirks, qp, entry);
6140 DNPRINTF(SWM_D_QUIRK, "quirk_insert: leave\n");
6144 quirk_remove(struct quirk *qp)
6146 DNPRINTF(SWM_D_QUIRK, "quirk_remove: %s:%s [%lu]\n", qp->class,
6147 qp->name, qp->quirk);
6149 TAILQ_REMOVE(&quirks, qp, entry);
6154 DNPRINTF(SWM_D_QUIRK, "quirk_remove: leave\n");
6158 quirk_replace(struct quirk *qp, const char *class, const char *name,
6159 unsigned long quirk)
6161 DNPRINTF(SWM_D_QUIRK, "quirk_replace: %s:%s [%lu]\n", qp->class,
6162 qp->name, qp->quirk);
6165 quirk_insert(class, name, quirk);
6167 DNPRINTF(SWM_D_QUIRK, "quirk_replace: leave\n");
6171 setquirk(const char *class, const char *name, unsigned long quirk)
6175 DNPRINTF(SWM_D_QUIRK, "setquirk: enter %s:%s [%lu]\n", class, name,
6178 /* Remove/replace existing quirk. */
6179 TAILQ_FOREACH(qp, &quirks, entry) {
6180 if (!strcmp(qp->class, class) && !strcmp(qp->name, name)) {
6184 quirk_replace(qp, class, name, quirk);
6185 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
6190 /* Only insert if quirk is not NONE. */
6192 quirk_insert(class, name, quirk);
6194 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
6198 setconfquirk(char *selector, char *value, int flags)
6200 char *cp, *class, *name;
6204 /* suppress unused warning since var is needed */
6207 if (selector == NULL)
6209 if ((cp = strchr(selector, ':')) == NULL)
6214 if ((retval = parsequirks(value, &qrks)) == 0)
6215 setquirk(class, name, qrks);
6222 setquirk("MPlayer", "xv", SWM_Q_FLOAT | SWM_Q_FULLSCREEN | SWM_Q_FOCUSPREV);
6223 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT);
6224 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ);
6225 setquirk("Firefox", "Dialog", SWM_Q_FLOAT);
6226 setquirk("Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
6227 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ);
6228 setquirk("xine", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
6229 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
6230 setquirk("xine", "xine Panel", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
6231 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
6232 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
6233 setquirk("pcb", "pcb", SWM_Q_FLOAT);
6234 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
6237 /* conf file stuff */
6238 #define SWM_CONF_FILE "spectrwm.conf"
6239 #define SWM_CONF_FILE_OLD "scrotwm.conf"
6243 SWM_S_BAR_AT_BOTTOM,
6244 SWM_S_BAR_BORDER_WIDTH,
6251 SWM_S_CLOCK_ENABLED,
6254 SWM_S_CYCLE_VISIBLE,
6256 SWM_S_DISABLE_BORDER,
6258 SWM_S_FOCUS_CLOSE_WRAP,
6259 SWM_S_FOCUS_DEFAULT,
6265 SWM_S_STACK_ENABLED,
6267 SWM_S_TITLE_CLASS_ENABLED,
6268 SWM_S_TITLE_NAME_ENABLED,
6269 SWM_S_URGENT_ENABLED,
6270 SWM_S_VERBOSE_LAYOUT,
6271 SWM_S_WINDOW_NAME_ENABLED,
6272 SWM_S_WORKSPACE_LIMIT
6276 setconfvalue(char *selector, char *value, int flags)
6281 /* suppress unused warning since var is needed */
6285 case SWM_S_BAR_ACTION:
6287 if ((bar_argv[0] = expand_tilde(value)) == NULL)
6288 err(1, "setconfvalue: bar_action");
6290 case SWM_S_BAR_AT_BOTTOM:
6291 bar_at_bottom = atoi(value);
6293 case SWM_S_BAR_BORDER_WIDTH:
6294 bar_border_width = atoi(value);
6295 if (bar_border_width < 0)
6296 bar_border_width = 0;
6298 case SWM_S_BAR_DELAY:
6299 bar_delay = atoi(value);
6301 case SWM_S_BAR_ENABLED:
6302 bar_enabled = atoi(value);
6304 case SWM_S_BAR_FONT:
6306 if (asprintf(&bar_fonts, "%s,%s", value, bar_fonts) == -1)
6307 err(1, "setconfvalue: asprintf: failed to allocate "
6308 "memory for bar_fonts.");
6311 /* If already in xft mode, then we are done. */
6312 if (!bar_font_legacy)
6315 /* If there are any non-XLFD entries, switch to Xft mode. */
6316 while ((b = strsep(&value, ",")) != NULL) {
6320 bar_font_legacy = 0;
6325 case SWM_S_BAR_FORMAT:
6327 if ((bar_format = strdup(value)) == NULL)
6328 err(1, "setconfvalue: bar_format");
6330 case SWM_S_BAR_JUSTIFY:
6331 if (!strcmp(value, "left"))
6332 bar_justify = SWM_BAR_JUSTIFY_LEFT;
6333 else if (!strcmp(value, "center"))
6334 bar_justify = SWM_BAR_JUSTIFY_CENTER;
6335 else if (!strcmp(value, "right"))
6336 bar_justify = SWM_BAR_JUSTIFY_RIGHT;
6338 errx(1, "invalid bar_justify");
6340 case SWM_S_BORDER_WIDTH:
6341 border_width = atoi(value);
6342 if (border_width < 0)
6345 case SWM_S_CLOCK_ENABLED:
6346 clock_enabled = atoi(value);
6348 case SWM_S_CLOCK_FORMAT:
6349 #ifndef SWM_DENY_CLOCK_FORMAT
6351 if ((clock_format = strdup(value)) == NULL)
6352 err(1, "setconfvalue: clock_format");
6355 case SWM_S_CYCLE_EMPTY:
6356 cycle_empty = atoi(value);
6358 case SWM_S_CYCLE_VISIBLE:
6359 cycle_visible = atoi(value);
6361 case SWM_S_DIALOG_RATIO:
6362 dialog_ratio = atof(value);
6363 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
6366 case SWM_S_DISABLE_BORDER:
6367 disable_border = atoi(value);
6369 case SWM_S_FOCUS_CLOSE:
6370 if (!strcmp(value, "first"))
6371 focus_close = SWM_STACK_BOTTOM;
6372 else if (!strcmp(value, "last"))
6373 focus_close = SWM_STACK_TOP;
6374 else if (!strcmp(value, "next"))
6375 focus_close = SWM_STACK_ABOVE;
6376 else if (!strcmp(value, "previous"))
6377 focus_close = SWM_STACK_BELOW;
6379 errx(1, "focus_close");
6381 case SWM_S_FOCUS_CLOSE_WRAP:
6382 focus_close_wrap = atoi(value);
6384 case SWM_S_FOCUS_DEFAULT:
6385 if (!strcmp(value, "last"))
6386 focus_default = SWM_STACK_TOP;
6387 else if (!strcmp(value, "first"))
6388 focus_default = SWM_STACK_BOTTOM;
6390 errx(1, "focus_default");
6392 case SWM_S_FOCUS_MODE:
6393 if (!strcmp(value, "default"))
6394 focus_mode = SWM_FOCUS_DEFAULT;
6395 else if (!strcmp(value, "follow") ||
6396 !strcmp(value, "follow_cursor"))
6397 focus_mode = SWM_FOCUS_FOLLOW;
6398 else if (!strcmp(value, "manual"))
6399 focus_mode = SWM_FOCUS_MANUAL;
6401 errx(1, "focus_mode");
6403 case SWM_S_SPAWN_ORDER:
6404 if (!strcmp(value, "first"))
6405 spawn_position = SWM_STACK_BOTTOM;
6406 else if (!strcmp(value, "last"))
6407 spawn_position = SWM_STACK_TOP;
6408 else if (!strcmp(value, "next"))
6409 spawn_position = SWM_STACK_ABOVE;
6410 else if (!strcmp(value, "previous"))
6411 spawn_position = SWM_STACK_BELOW;
6413 errx(1, "spawn_position");
6415 case SWM_S_SPAWN_TERM:
6416 setconfspawn("term", value, 0);
6417 setconfspawn("spawn_term", value, 0);
6421 case SWM_S_SS_ENABLED:
6422 ss_enabled = atoi(value);
6424 case SWM_S_STACK_ENABLED:
6425 stack_enabled = atoi(value);
6427 case SWM_S_TERM_WIDTH:
6428 term_width = atoi(value);
6432 case SWM_S_TITLE_CLASS_ENABLED:
6433 title_class_enabled = atoi(value);
6435 case SWM_S_TITLE_NAME_ENABLED:
6436 title_name_enabled = atoi(value);
6438 case SWM_S_URGENT_ENABLED:
6439 urgent_enabled = atoi(value);
6441 case SWM_S_VERBOSE_LAYOUT:
6442 verbose_layout = atoi(value);
6443 for (i = 0; layouts[i].l_stack != NULL; i++) {
6445 layouts[i].l_string = fancy_stacker;
6447 layouts[i].l_string = plain_stacker;
6450 case SWM_S_WINDOW_NAME_ENABLED:
6451 window_name_enabled = atoi(value);
6453 case SWM_S_WORKSPACE_LIMIT:
6454 workspace_limit = atoi(value);
6455 if (workspace_limit > SWM_WS_MAX)
6456 workspace_limit = SWM_WS_MAX;
6457 else if (workspace_limit < 1)
6458 workspace_limit = 1;
6467 setconfmodkey(char *selector, char *value, int flags)
6469 /* suppress unused warnings since vars are needed */
6473 if (!strncasecmp(value, "Mod1", strlen("Mod1")))
6474 update_modkey(XCB_MOD_MASK_1);
6475 else if (!strncasecmp(value, "Mod2", strlen("Mod2")))
6476 update_modkey(XCB_MOD_MASK_2);
6477 else if (!strncasecmp(value, "Mod3", strlen("Mod3")))
6478 update_modkey(XCB_MOD_MASK_3);
6479 else if (!strncasecmp(value, "Mod4", strlen("Mod4")))
6480 update_modkey(XCB_MOD_MASK_4);
6487 setconfcolor(char *selector, char *value, int flags)
6489 setscreencolor(value, ((selector == NULL)?-1:atoi(selector)), flags);
6494 setconfregion(char *selector, char *value, int flags)
6496 /* suppress unused warnings since vars are needed */
6500 custom_region(value);
6505 setautorun(char *selector, char *value, int flags)
6515 /* suppress unused warnings since vars are needed */
6519 if (getenv("SWM_STARTED"))
6523 if (sscanf(value, "ws[%d]:%1023c", &ws_id, s) != 2)
6524 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
6526 if (ws_id < 0 || ws_id >= workspace_limit)
6527 errx(1, "autorun: invalid workspace %d", ws_id + 1);
6529 sp = expand_tilde((char *)&s);
6532 * This is a little intricate
6534 * If the pid already exists we simply reuse it because it means it was
6535 * used before AND not claimed by manage_window. We get away with
6536 * altering it in the parent after INSERT because this can not be a race
6539 while ((ap = strsep(&sp, " \t")) != NULL) {
6542 DNPRINTF(SWM_D_SPAWN, "setautorun: arg [%s]\n", ap);
6544 if ((a.argv = realloc(a.argv, argc * sizeof(char *))) == NULL)
6545 err(1, "setautorun: realloc");
6546 a.argv[argc - 1] = ap;
6550 if ((a.argv = realloc(a.argv, (argc + 1) * sizeof(char *))) == NULL)
6551 err(1, "setautorun: realloc");
6552 a.argv[argc] = NULL;
6554 if ((pid = fork()) == 0) {
6555 spawn(ws_id, &a, 1);
6564 p = calloc(1, sizeof *p);
6567 TAILQ_INSERT_TAIL(&pidlist, p, entry);
6577 setlayout(char *selector, char *value, int flags)
6579 int ws_id, i, x, mg, ma, si, ar, f = 0;
6580 int st = SWM_V_STACK, num_screens;
6582 struct workspace *ws;
6584 /* suppress unused warnings since vars are needed */
6588 if (getenv("SWM_STARTED"))
6592 if (sscanf(value, "ws[%d]:%d:%d:%d:%d:%1023c",
6593 &ws_id, &mg, &ma, &si, &ar, s) != 6)
6594 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
6595 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
6598 if (ws_id < 0 || ws_id >= workspace_limit)
6599 errx(1, "layout: invalid workspace %d", ws_id + 1);
6601 if (!strcasecmp(s, "vertical"))
6603 else if (!strcasecmp(s, "vertical_flip")) {
6606 } else if (!strcasecmp(s, "horizontal"))
6608 else if (!strcasecmp(s, "horizontal_flip")) {
6611 } else if (!strcasecmp(s, "fullscreen"))
6614 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
6615 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
6618 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
6619 for (i = 0; i < num_screens; i++) {
6620 ws = (struct workspace *)&screens[i].ws;
6621 ws[ws_id].cur_layout = &layouts[st];
6623 ws[ws_id].always_raise = ar;
6624 if (st == SWM_MAX_STACK)
6628 for (x = 0; x < abs(mg); x++) {
6629 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6630 mg >= 0 ? SWM_ARG_ID_MASTERGROW :
6631 SWM_ARG_ID_MASTERSHRINK);
6635 for (x = 0; x < abs(ma); x++) {
6636 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6637 ma >= 0 ? SWM_ARG_ID_MASTERADD :
6638 SWM_ARG_ID_MASTERDEL);
6642 for (x = 0; x < abs(si); x++) {
6643 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6644 si >= 0 ? SWM_ARG_ID_STACKINC :
6645 SWM_ARG_ID_STACKDEC);
6650 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6651 SWM_ARG_ID_FLIPLAYOUT);
6661 /* config options */
6662 struct config_option {
6664 int (*func)(char*, char*, int);
6667 struct config_option configopt[] = {
6668 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
6669 { "bar_at_bottom", setconfvalue, SWM_S_BAR_AT_BOTTOM },
6670 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
6671 { "bar_border_width", setconfvalue, SWM_S_BAR_BORDER_WIDTH },
6672 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
6673 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
6674 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
6675 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
6676 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
6677 { "bar_justify", setconfvalue, SWM_S_BAR_JUSTIFY },
6678 { "bar_format", setconfvalue, SWM_S_BAR_FORMAT },
6679 { "keyboard_mapping", setkeymapping, 0 },
6680 { "bind", setconfbinding, 0 },
6681 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
6682 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
6683 { "clock_format", setconfvalue, SWM_S_CLOCK_FORMAT },
6684 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
6685 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
6686 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
6687 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
6688 { "workspace_limit", setconfvalue, SWM_S_WORKSPACE_LIMIT },
6689 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
6690 { "verbose_layout", setconfvalue, SWM_S_VERBOSE_LAYOUT },
6691 { "modkey", setconfmodkey, 0 },
6692 { "program", setconfspawn, 0 },
6693 { "quirk", setconfquirk, 0 },
6694 { "region", setconfregion, 0 },
6695 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
6696 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
6697 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
6698 { "window_name_enabled", setconfvalue, SWM_S_WINDOW_NAME_ENABLED },
6699 { "urgent_enabled", setconfvalue, SWM_S_URGENT_ENABLED },
6700 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
6701 { "title_class_enabled", setconfvalue, SWM_S_TITLE_CLASS_ENABLED },
6702 { "title_name_enabled", setconfvalue, SWM_S_TITLE_NAME_ENABLED },
6703 { "focus_mode", setconfvalue, SWM_S_FOCUS_MODE },
6704 { "focus_close", setconfvalue, SWM_S_FOCUS_CLOSE },
6705 { "focus_close_wrap", setconfvalue, SWM_S_FOCUS_CLOSE_WRAP },
6706 { "focus_default", setconfvalue, SWM_S_FOCUS_DEFAULT },
6707 { "spawn_position", setconfvalue, SWM_S_SPAWN_ORDER },
6708 { "disable_border", setconfvalue, SWM_S_DISABLE_BORDER },
6709 { "border_width", setconfvalue, SWM_S_BORDER_WIDTH },
6710 { "autorun", setautorun, 0 },
6711 { "layout", setlayout, 0 },
6715 conf_load(const char *filename, int keymapping)
6718 char *line, *cp, *optsub, *optval;
6719 size_t linelen, lineno = 0;
6720 int wordlen, i, optidx;
6721 struct config_option *opt = NULL;
6723 DNPRINTF(SWM_D_CONF, "conf_load: begin\n");
6725 if (filename == NULL) {
6726 warnx("conf_load: no filename");
6729 if ((config = fopen(filename, "r")) == NULL) {
6730 warn("conf_load: fopen: %s", filename);
6734 while (!feof(config)) {
6735 if ((line = fparseln(config, &linelen, &lineno, NULL, 0))
6738 err(1, "%s", filename);
6743 cp += strspn(cp, " \t\n"); /* eat whitespace */
6744 if (cp[0] == '\0') {
6749 /* get config option */
6750 wordlen = strcspn(cp, "=[ \t\n");
6752 warnx("%s: line %zd: no option found",
6757 for (i = 0; i < LENGTH(configopt); i++) {
6758 opt = &configopt[i];
6759 if (!strncasecmp(cp, opt->optname, wordlen) &&
6760 (int)strlen(opt->optname) == wordlen) {
6766 warnx("%s: line %zd: unknown option %.*s",
6767 filename, lineno, wordlen, cp);
6770 if (keymapping && opt && strcmp(opt->optname, "bind")) {
6771 warnx("%s: line %zd: invalid option %.*s",
6772 filename, lineno, wordlen, cp);
6776 cp += strspn(cp, " \t\n"); /* eat whitespace */
6777 /* get [selector] if any */
6781 wordlen = strcspn(cp, "]");
6784 warnx("%s: line %zd: syntax error",
6789 if (asprintf(&optsub, "%.*s", wordlen, cp) ==
6791 warnx("%s: line %zd: unable to allocate"
6792 "memory for selector", filename,
6798 cp += strspn(cp, "] \t\n"); /* eat trailing */
6800 cp += strspn(cp, "= \t\n"); /* eat trailing */
6802 optval = strdup(cp);
6803 /* call function to deal with it all */
6804 if (configopt[optidx].func(optsub, optval,
6805 configopt[optidx].funcflags) != 0)
6806 errx(1, "%s: line %zd: invalid data for %s",
6807 filename, lineno, configopt[optidx].optname);
6814 DNPRINTF(SWM_D_CONF, "conf_load: end\n");
6821 DNPRINTF(SWM_D_CONF, "conf_load: end with error.\n");
6827 set_child_transient(struct ws_win *win, xcb_window_t *trans)
6829 struct ws_win *parent, *w;
6830 struct swm_region *r;
6831 struct workspace *ws;
6832 xcb_icccm_wm_hints_t wmh;
6834 parent = find_window(win->transient);
6836 parent->focus_child = win;
6838 DNPRINTF(SWM_D_MISC, "set_child_transient: parent doesn't exist"
6839 " for 0x%x trans 0x%x\n", win->id, win->transient);
6841 r = root_to_region(win->wa->root, SWM_CK_ALL);
6843 /* parent doen't exist in our window list */
6844 TAILQ_FOREACH(w, &ws->winlist, entry) {
6845 if (xcb_icccm_get_wm_hints_reply(conn,
6846 xcb_icccm_get_wm_hints(conn, w->id),
6848 warnx("can't get hints for 0x%x", w->id);
6852 if (win->hints.window_group != wmh.window_group)
6855 w->focus_child = win;
6856 win->transient = w->id;
6858 DNPRINTF(SWM_D_MISC, "set_child_transient: adjusting "
6859 "transient to 0x%x\n", win->transient);
6866 window_get_pid(xcb_window_t win)
6871 xcb_get_property_cookie_t pc;
6872 xcb_get_property_reply_t *pr;
6874 apid = get_atom_from_string("_NET_WM_PID");
6875 if (apid == XCB_ATOM_NONE)
6878 pc = xcb_get_property(conn, 0, win, apid, XCB_ATOM_CARDINAL, 0, 1);
6879 pr = xcb_get_property_reply(conn, pc, NULL);
6882 if (pr->type != XCB_ATOM_CARDINAL) {
6887 if (pr->type == apid && pr->format == 32)
6888 ret = *((pid_t *)xcb_get_property_value(pr));
6894 apid = get_atom_from_string("_SWM_PID");
6895 pc = xcb_get_property(conn, 0, win, apid, XCB_ATOM_STRING,
6897 pr = xcb_get_property_reply(conn, pc, NULL);
6900 if (pr->type != apid) {
6905 ret = (pid_t)strtonum(xcb_get_property_value(pr), 0, INT_MAX, &errstr);
6912 get_ws_idx(xcb_window_t id)
6918 xcb_get_property_reply_t *gpr;
6920 gpr = xcb_get_property_reply(conn,
6921 xcb_get_property(conn, 0, id, a_swm_ws,
6922 XCB_ATOM_STRING, 0, SWM_PROPLEN),
6927 proplen = xcb_get_property_value_length(gpr);
6929 prop = malloc(proplen + 1);
6932 xcb_get_property_value(gpr),
6934 prop[proplen] = '\0';
6941 DNPRINTF(SWM_D_PROP, "get_ws_idx: _SWM_WS: %s\n", prop);
6942 ws_idx = (int)strtonum(prop, 0, workspace_limit - 1, &errstr);
6944 DNPRINTF(SWM_D_PROP, "get_ws_idx: window: #%s: %s",
6954 manage_window(xcb_window_t id, uint16_t mapped)
6956 xcb_window_t trans = XCB_WINDOW_NONE;
6957 struct ws_win *win, *ww;
6959 char ws_idx_str[SWM_PROPLEN];
6960 struct swm_region *r;
6964 xcb_icccm_get_wm_protocols_reply_t wpr;
6966 if ((win = find_window(id)) != NULL) {
6967 DNPRINTF(SWM_D_MISC, "manage_window: win 0x%x already "
6968 "managed; skipping.)\n", id);
6969 return (win); /* Already managed. */
6972 /* See if window is on the unmanaged list. */
6973 if ((win = find_unmanaged_window(id)) != NULL) {
6974 DNPRINTF(SWM_D_MISC, "manage_window: win 0x%x found on "
6975 "unmanaged list.\n", id);
6976 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
6979 set_child_transient(win, &trans);
6983 DNPRINTF(SWM_D_MISC, "manage_window: win 0x%x is new.\n", id);
6986 /* Create and initialize ws_win object. */
6987 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
6988 err(1, "manage_window: calloc: failed to allocate memory for "
6993 /* Get window geometry. */
6994 win->wa = xcb_get_geometry_reply(conn,
6995 xcb_get_geometry(conn, win->id),
6998 /* Figure out which region the window belongs to. */
6999 r = root_to_region(win->wa->root, SWM_CK_ALL);
7001 /* Ignore window border if there is one. */
7002 WIDTH(win) = win->wa->width;
7003 HEIGHT(win) = win->wa->height;
7004 X(win) = win->wa->x + win->wa->border_width - border_width;
7005 Y(win) = win->wa->y + win->wa->border_width - border_width;
7007 win->mapped = mapped;
7008 win->floatmaxed = 0;
7009 win->ewmh_flags = 0;
7010 win->s = r->s; /* this never changes */
7012 store_float_geom(win, r);
7014 /* Get WM_SIZE_HINTS. */
7015 xcb_icccm_get_wm_normal_hints_reply(conn,
7016 xcb_icccm_get_wm_normal_hints(conn, win->id),
7020 xcb_icccm_get_wm_hints_reply(conn,
7021 xcb_icccm_get_wm_hints(conn, win->id),
7024 /* Get WM_TRANSIENT_FOR; see if window is a transient. */
7025 xcb_icccm_get_wm_transient_for_reply(conn,
7026 xcb_icccm_get_wm_transient_for(conn, win->id),
7029 win->transient = trans;
7030 set_child_transient(win, &win->transient);
7033 /* Get supported protocols. */
7034 if (xcb_icccm_get_wm_protocols_reply(conn,
7035 xcb_icccm_get_wm_protocols(conn, win->id, a_prot),
7037 for (i = 0; i < wpr.atoms_len; i++) {
7038 if (wpr.atoms[i] == a_takefocus)
7039 win->take_focus = 1;
7040 if (wpr.atoms[i] == a_delete)
7041 win->can_delete = 1;
7043 xcb_icccm_get_wm_protocols_reply_wipe(&wpr);
7046 win->iconic = get_swm_iconic(win);
7048 /* Figure out which workspace the window belongs to. */
7049 if ((p = find_pid(window_get_pid(win->id))) != NULL) {
7050 win->ws = &r->s->ws[p->ws];
7051 TAILQ_REMOVE(&pidlist, p, entry);
7054 } else if ((ws_idx = get_ws_idx(win->id)) != -1 &&
7056 /* _SWM_WS is set; use that. */
7057 win->ws = &r->s->ws[ws_idx];
7058 } else if (trans && (ww = find_window(trans)) != NULL) {
7059 /* Launch transients in the same ws as parent. */
7065 /* Set the _SWM_WS atom so we can remember this after reincarnation. */
7066 if (snprintf(ws_idx_str, SWM_PROPLEN, "%d", win->ws->idx) <
7068 DNPRINTF(SWM_D_PROP, "manage_window: set _SWM_WS: %s\n",
7070 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
7071 a_swm_ws, XCB_ATOM_STRING, 8, strlen(ws_idx_str),
7076 ewmh_autoquirk(win);
7078 /* Determine initial quirks. */
7079 if (xcb_icccm_get_wm_class_reply(conn,
7080 xcb_icccm_get_wm_class(conn, win->id),
7082 DNPRINTF(SWM_D_CLASS, "manage_window: class: %s, name: %s\n",
7083 win->ch.class_name, win->ch.instance_name);
7085 /* java is retarded so treat it special */
7086 if (strstr(win->ch.instance_name, "sun-awt")) {
7087 DNPRINTF(SWM_D_CLASS, "manage_window: java window "
7092 TAILQ_FOREACH(qp, &quirks, entry) {
7093 if (!strcmp(win->ch.class_name, qp->class) &&
7094 !strcmp(win->ch.instance_name, qp->name)) {
7095 DNPRINTF(SWM_D_CLASS, "manage_window: on quirks"
7096 "list; mask: 0x%lx\n", qp->quirk);
7097 if (qp->quirk & SWM_Q_FLOAT)
7099 win->quirks = qp->quirk;
7104 /* Alter window position if quirky */
7105 if (win->quirks & SWM_Q_ANYWHERE)
7108 /* Reset font sizes (the bruteforce way; no default keybinding). */
7109 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
7110 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
7111 fake_keypress(win, XK_KP_Subtract, XCB_MOD_MASK_SHIFT);
7112 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
7113 fake_keypress(win, XK_KP_Add, XCB_MOD_MASK_SHIFT);
7116 /* Make sure window is positioned inside its region, if its active. */
7118 constrain_window(win, win->ws->r, 0);
7122 /* Select which X events to monitor and set border pixel color. */
7123 wa[0] = win->s->c[SWM_S_COLOR_UNFOCUS].pixel;
7124 wa[1] = XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_PROPERTY_CHANGE |
7125 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
7127 wa[1] |= XCB_EVENT_MASK_LEAVE_WINDOW | XCB_EVENT_MASK_FOCUS_CHANGE;
7130 xcb_change_window_attributes(conn, win->id, XCB_CW_BORDER_PIXEL |
7131 XCB_CW_EVENT_MASK, wa);
7134 /* Figure out where to stack the window in the workspace. */
7135 if (trans && (ww = find_window(trans)))
7136 TAILQ_INSERT_AFTER(&win->ws->winlist, ww, win, entry);
7137 else if (win->ws->focus && spawn_position == SWM_STACK_ABOVE)
7138 TAILQ_INSERT_AFTER(&win->ws->winlist, win->ws->focus, win,
7140 else if (win->ws->focus && spawn_position == SWM_STACK_BELOW)
7141 TAILQ_INSERT_BEFORE(win->ws->focus, win, entry);
7142 else switch (spawn_position) {
7145 case SWM_STACK_ABOVE:
7146 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
7148 case SWM_STACK_BOTTOM:
7149 case SWM_STACK_BELOW:
7150 TAILQ_INSERT_HEAD(&win->ws->winlist, win, entry);
7153 /* Get initial _NET_WM_STATE */
7154 ewmh_get_win_state(win);
7155 /* Set initial _NET_WM_ALLOWED_ACTIONS */
7156 ewmh_update_actions(win);
7160 DNPRINTF(SWM_D_MISC, "manage_window: done. window: 0x%x, (x,y) w x h: "
7161 "(%d,%d) %d x %d, ws: %d, iconic: %s, transient: 0x%x\n", win->id,
7162 X(win), Y(win), WIDTH(win), HEIGHT(win), win->ws->idx,
7163 YESNO(win->iconic), win->transient);
7169 free_window(struct ws_win *win)
7171 DNPRINTF(SWM_D_MISC, "free_window: window: 0x%x\n", win->id);
7176 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
7181 xcb_icccm_get_wm_class_reply_wipe(&win->ch);
7186 memset(win, 0xff, sizeof *win); /* XXX kill later */
7189 DNPRINTF(SWM_D_MISC, "free_window: done.\n");
7193 unmanage_window(struct ws_win *win)
7195 struct ws_win *parent;
7200 DNPRINTF(SWM_D_MISC, "unmanage_window: window: 0x%x\n", win->id);
7202 if (win->transient) {
7203 parent = find_window(win->transient);
7205 parent->focus_child = NULL;
7208 TAILQ_REMOVE(&win->ws->winlist, win, entry);
7209 TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
7213 expose(xcb_expose_event_t *e)
7216 struct swm_region *r;
7218 DNPRINTF(SWM_D_EVENT, "expose: window: 0x%x\n", e->window);
7220 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7221 for (i = 0; i < num_screens; i++)
7222 TAILQ_FOREACH(r, &screens[i].rl, entry)
7223 if (e->window == WINID(r->bar))
7231 focusin(xcb_focus_in_event_t *e)
7233 DNPRINTF(SWM_D_EVENT, "focusin: window: 0x%x, mode: %s(%u), "
7234 "detail: %s(%u)\n", e->event, get_notify_mode_label(e->mode),
7235 e->mode, get_notify_detail_label(e->detail), e->detail);
7239 focusout(xcb_focus_out_event_t *e)
7241 DNPRINTF(SWM_D_EVENT, "focusout: window: 0x%x, mode: %s(%u), "
7242 "detail: %s(%u)\n", e->event, get_notify_mode_label(e->mode),
7243 e->mode, get_notify_detail_label(e->detail), e->detail);
7248 keypress(xcb_key_press_event_t *e)
7250 xcb_keysym_t keysym;
7253 keysym = xcb_key_press_lookup_keysym(syms, e, 0);
7255 DNPRINTF(SWM_D_EVENT, "keypress: %u %u\n", e->detail, keysym);
7257 if ((kp = key_lookup(CLEANMASK(e->state), keysym)) == NULL)
7260 last_event_time = e->time;
7262 if (kp->funcid == KF_SPAWN_CUSTOM)
7263 spawn_custom(root_to_region(e->root, SWM_CK_ALL),
7264 &(keyfuncs[kp->funcid].args), kp->spawn_name);
7265 else if (keyfuncs[kp->funcid].func)
7266 keyfuncs[kp->funcid].func(root_to_region(e->root, SWM_CK_ALL),
7267 &(keyfuncs[kp->funcid].args));
7271 buttonpress(xcb_button_press_event_t *e)
7273 struct ws_win *win = NULL;
7274 struct swm_region *r, *old_r;
7278 DNPRINTF(SWM_D_EVENT, "buttonpress: win (x,y): 0x%x (%d,%d), "
7279 "detail: %u, time: %u, root (x,y): 0x%x (%d,%d), child: 0x%x, "
7280 "state: %u, same_screen: %s\n", e->event, e->event_x, e->event_y,
7281 e->detail, e->time, e->root, e->root_x, e->root_y, e->child,
7282 e->state, YESNO(e->same_screen));
7284 if (e->event == e->root) {
7285 if (e->child != 0) {
7286 win = find_window(e->child);
7287 /* Pass ButtonPress to window if it isn't managed. */
7291 /* Focus on empty region */
7292 /* If no windows on region if its empty. */
7293 r = root_to_region(e->root, SWM_CK_POINTER);
7294 if (r && TAILQ_EMPTY(&r->ws->winlist)) {
7295 old_r = root_to_region(e->root, SWM_CK_FOCUS);
7296 if (old_r && old_r != r)
7297 unfocus_win(old_r->ws->focus);
7299 xcb_set_input_focus(conn,
7300 XCB_INPUT_FOCUS_PARENT, e->root, e->time);
7302 /* Clear bar since empty. */
7310 win = find_window(e->event);
7316 last_event_time = e->time;
7318 focus_win(get_focus_magic(win));
7320 for (i = 0; i < LENGTH(buttons); i++)
7321 if (client_click == buttons[i].action && buttons[i].func &&
7322 buttons[i].button == e->detail &&
7323 CLEANMASK(buttons[i].mask) == CLEANMASK(e->state)) {
7324 buttons[i].func(win, &buttons[i].args);
7330 DNPRINTF(SWM_D_EVENT, "buttonpress: passing to window.\n");
7331 /* Replay event to event window */
7332 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, e->time);
7334 DNPRINTF(SWM_D_EVENT, "buttonpress: handled.\n");
7335 /* Unfreeze grab events. */
7336 xcb_allow_events(conn, XCB_ALLOW_SYNC_POINTER, e->time);
7344 print_win_geom(xcb_window_t w)
7346 xcb_get_geometry_reply_t *wa;
7348 wa = xcb_get_geometry_reply(conn, xcb_get_geometry(conn, w), NULL);
7351 DNPRINTF(SWM_D_MISC, "print_win_geom: window not found: 0x%x\n",
7356 DNPRINTF(SWM_D_MISC, "print_win_geom: window: 0x%x, root: 0x%x, "
7357 "depth: %u, (x,y) w x h: (%d,%d) %d x %d, border: %d\n",
7358 w, wa->root, wa->depth, wa->x, wa->y, wa->width, wa->height,
7367 get_stack_mode_name(uint8_t mode)
7372 case XCB_STACK_MODE_ABOVE:
7375 case XCB_STACK_MODE_BELOW:
7378 case XCB_STACK_MODE_TOP_IF:
7381 case XCB_STACK_MODE_BOTTOM_IF:
7384 case XCB_STACK_MODE_OPPOSITE:
7396 configurerequest(xcb_configure_request_event_t *e)
7399 struct swm_region *r = NULL;
7402 uint32_t wc[7] = {0};
7404 if ((win = find_window(e->window)) == NULL)
7405 if ((win = find_unmanaged_window(e->window)) == NULL)
7409 if (swm_debug & SWM_D_EVENT) {
7410 print_win_geom(e->window);
7412 DNPRINTF(SWM_D_EVENT, "configurerequest: window: 0x%x, "
7413 "parent: 0x%x, new: %s, value_mask: %u { ", e->window,
7414 e->parent, YESNO(new), e->value_mask);
7415 if (e->value_mask & XCB_CONFIG_WINDOW_X)
7416 DPRINTF("X: %d ", e->x);
7417 if (e->value_mask & XCB_CONFIG_WINDOW_Y)
7418 DPRINTF("Y: %d ", e->y);
7419 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH)
7420 DPRINTF("W: %u ", e->width);
7421 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
7422 DPRINTF("H: %u ", e->height);
7423 if (e->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH)
7424 DPRINTF("Border: %u ", e->border_width);
7425 if (e->value_mask & XCB_CONFIG_WINDOW_SIBLING)
7426 DPRINTF("Sibling: 0x%x ", e->sibling);
7427 if (e->value_mask & XCB_CONFIG_WINDOW_STACK_MODE)
7428 DPRINTF("StackMode: %s(%u) ",
7429 get_stack_mode_name(e->stack_mode), e->stack_mode);
7435 if (e->value_mask & XCB_CONFIG_WINDOW_X) {
7436 mask |= XCB_CONFIG_WINDOW_X;
7439 if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
7440 mask |= XCB_CONFIG_WINDOW_Y;
7443 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
7444 mask |= XCB_CONFIG_WINDOW_WIDTH;
7447 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
7448 mask |= XCB_CONFIG_WINDOW_HEIGHT;
7449 wc[i++] = e->height;
7451 if (e->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH) {
7452 mask |= XCB_CONFIG_WINDOW_BORDER_WIDTH;
7453 wc[i++] = e->border_width;
7455 if (e->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
7456 mask |= XCB_CONFIG_WINDOW_SIBLING;
7457 wc[i++] = e->sibling;
7459 if (e->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
7460 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
7461 wc[i++] = e->stack_mode;
7465 xcb_configure_window(conn, e->window, mask, wc);
7468 } else if ((!win->manual || win->quirks & SWM_Q_ANYWHERE) &&
7469 !(win->ewmh_flags & EWMH_F_FULLSCREEN)) {
7472 else if (win->ws->old_r)
7475 /* windows are centered unless ANYWHERE quirk is set. */
7476 if (win->quirks & SWM_Q_ANYWHERE) {
7477 if (e->value_mask & XCB_CONFIG_WINDOW_X) {
7478 win->g_float.x = e->x;
7480 win->g_float.x -= X(r);
7483 if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
7484 win->g_float.y = e->y;
7486 win->g_float.y -= Y(r);
7490 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH)
7491 win->g_float.w = e->width;
7493 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
7494 win->g_float.h = e->height;
7496 win->g_floatvalid = 1;
7498 if (win->floating && r && (win->transient ||
7499 win->ws->cur_layout != &layouts[SWM_MAX_STACK])) {
7500 WIDTH(win) = win->g_float.w;
7501 HEIGHT(win) = win->g_float.h;
7503 stack_floater(win, win->ws->r);
7515 DNPRINTF(SWM_D_EVENT, "configurerequest: done.\n");
7519 configurenotify(xcb_configure_notify_event_t *e)
7523 DNPRINTF(SWM_D_EVENT, "configurenotify: win 0x%x, event win: 0x%x, "
7524 "(x,y) WxH: (%d,%d) %ux%u, border: %u, above_sibling: 0x%x, "
7525 "override_redirect: %s\n", e->window, e->event, e->x, e->y,
7526 e->width, e->height, e->border_width, e->above_sibling,
7527 YESNO(e->override_redirect));
7529 win = find_window(e->window);
7531 xcb_icccm_get_wm_normal_hints_reply(conn,
7532 xcb_icccm_get_wm_normal_hints(conn, win->id),
7535 if (font_adjusted) {
7543 destroynotify(xcb_destroy_notify_event_t *e)
7547 DNPRINTF(SWM_D_EVENT, "destroynotify: window: 0x%x\n", e->window);
7549 if ((win = find_window(e->window)) == NULL) {
7550 if ((win = find_unmanaged_window(e->window)) == NULL)
7556 if (focus_mode != SWM_FOCUS_FOLLOW) {
7557 /* If we were focused, make sure we focus on something else. */
7558 if (win == win->ws->focus)
7559 win->ws->focus_pending = get_focus_prev(win);
7562 unmanage_window(win);
7565 if (focus_mode != SWM_FOCUS_FOLLOW) {
7566 if (win->ws->focus_pending) {
7567 focus_win(win->ws->focus_pending);
7568 win->ws->focus_pending = NULL;
7579 get_notify_detail_label(uint8_t detail)
7584 case XCB_NOTIFY_DETAIL_ANCESTOR:
7587 case XCB_NOTIFY_DETAIL_VIRTUAL:
7590 case XCB_NOTIFY_DETAIL_INFERIOR:
7593 case XCB_NOTIFY_DETAIL_NONLINEAR:
7594 label = "Nonlinear";
7596 case XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL:
7597 label = "NonlinearVirtual";
7599 case XCB_NOTIFY_DETAIL_POINTER:
7602 case XCB_NOTIFY_DETAIL_POINTER_ROOT:
7603 label = "PointerRoot";
7605 case XCB_NOTIFY_DETAIL_NONE:
7616 get_notify_mode_label(uint8_t mode)
7621 case XCB_NOTIFY_MODE_NORMAL:
7624 case XCB_NOTIFY_MODE_GRAB:
7627 case XCB_NOTIFY_MODE_UNGRAB:
7630 case XCB_NOTIFY_MODE_WHILE_GRABBED:
7631 label = "WhileGrabbed";
7642 enternotify(xcb_enter_notify_event_t *e)
7645 struct swm_region *old_r, *r;
7647 DNPRINTF(SWM_D_FOCUS, "enternotify: time: %u, win (x,y): 0x%x "
7648 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): 0x%x (%d,%d), "
7649 "child: 0x%x, same_screen_focus: %s, state: %d\n",
7650 e->time, e->event, e->event_x, e->event_y,
7651 get_notify_mode_label(e->mode), e->mode,
7652 get_notify_detail_label(e->detail), e->detail,
7653 e->root, e->root_x, e->root_y, e->child,
7654 YESNO(e->same_screen_focus), e->state);
7656 if (focus_mode == SWM_FOCUS_MANUAL &&
7657 e->mode == XCB_NOTIFY_MODE_NORMAL) {
7658 DNPRINTF(SWM_D_EVENT, "enternotify: manual focus; ignoring.\n");
7662 last_event_time = e->time;
7664 if ((win = find_window(e->event)) == NULL) {
7665 if (e->event == e->root) {
7666 /* If no windows on pointer region, then focus root. */
7667 r = root_to_region(e->root, SWM_CK_POINTER);
7668 if (TAILQ_EMPTY(&r->ws->winlist)) {
7669 old_r = root_to_region(e->root, SWM_CK_FOCUS);
7670 if (old_r && old_r != r)
7671 unfocus_win(old_r->ws->focus);
7673 xcb_set_input_focus(conn,
7674 XCB_INPUT_FOCUS_PARENT, e->root, e->time);
7676 /* Clear bar since empty. */
7682 DNPRINTF(SWM_D_EVENT, "enternotify: window is NULL; "
7688 focus_win(get_focus_magic(win));
7695 leavenotify(xcb_leave_notify_event_t *e)
7697 DNPRINTF(SWM_D_FOCUS, "leavenotify: time: %u, win (x,y): 0x%x "
7698 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): 0x%x (%d,%d), "
7699 "child: 0x%x, same_screen_focus: %s, state: %d\n",
7700 e->time, e->event, e->event_x, e->event_y,
7701 get_notify_mode_label(e->mode), e->mode,
7702 get_notify_detail_label(e->detail), e->detail,
7703 e->root, e->root_x, e->root_y, e->child,
7704 YESNO(e->same_screen_focus), e->state);
7708 /* lets us use one switch statement for arbitrary mode/detail combinations */
7709 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
7712 mapnotify(xcb_map_notify_event_t *e)
7716 DNPRINTF(SWM_D_EVENT, "mapnotify: window: 0x%x\n", e->window);
7718 if ((win = find_window(e->window)) == NULL)
7719 win = manage_window(e->window, 1);
7722 set_win_state(win, XCB_ICCCM_WM_STATE_NORMAL);
7724 if (focus_mode != SWM_FOCUS_FOLLOW) {
7725 if (win->ws->focus_pending == win) {
7727 win->ws->focus_pending = NULL;
7736 mappingnotify(xcb_mapping_notify_event_t *e)
7738 xcb_refresh_keyboard_mapping(syms, e);
7740 if (e->request == XCB_MAPPING_KEYBOARD)
7745 maprequest(xcb_map_request_event_t *e)
7748 xcb_get_window_attributes_reply_t *war;
7750 DNPRINTF(SWM_D_EVENT, "maprequest: win 0x%x\n",
7753 war = xcb_get_window_attributes_reply(conn,
7754 xcb_get_window_attributes(conn, e->window),
7757 DNPRINTF(SWM_D_EVENT, "maprequest: window lost.\n");
7761 if (war->override_redirect) {
7762 DNPRINTF(SWM_D_EVENT, "maprequest: override_redirect; "
7767 win = manage_window(e->window,
7768 (war->map_state == XCB_MAP_STATE_VIEWABLE));
7770 /* The new window should get focus; prepare. */
7771 if (focus_mode != SWM_FOCUS_FOLLOW)
7772 win->ws->focus_pending = get_focus_magic(win);
7774 /* All windows need to be mapped if they are in the current workspace.*/
7778 /* Ignore EnterNotify to handle the mapnotify without interference. */
7779 if (focus_mode == SWM_FOCUS_DEFAULT)
7780 event_drain(XCB_ENTER_NOTIFY);
7783 DNPRINTF(SWM_D_EVENT, "maprequest: done.\n");
7788 get_atom_name(xcb_atom_t atom)
7793 * This should be disabled during most debugging since
7794 * xcb_get_* causes an xcb_flush.
7797 xcb_get_atom_name_reply_t *r;
7799 r = xcb_get_atom_name_reply(conn,
7800 xcb_get_atom_name(conn, atom),
7803 len = xcb_get_atom_name_name_length(r);
7805 name = malloc(len + 1);
7807 memcpy(name, xcb_get_atom_name_name(r), len);
7821 propertynotify(xcb_property_notify_event_t *e)
7827 name = get_atom_name(e->atom);
7828 DNPRINTF(SWM_D_EVENT, "propertynotify: window: 0x%x, atom: %s(%u), "
7829 "time: %#x, state: %u\n", e->window, name, e->atom, e->time,
7833 win = find_window(e->window);
7837 last_event_time = e->time;
7839 if (e->atom == a_swm_iconic) {
7840 if (e->state == XCB_PROPERTY_NEW_VALUE) {
7841 if (focus_mode != SWM_FOCUS_FOLLOW)
7842 win->ws->focus_pending = get_focus_prev(win);
7849 if (focus_mode != SWM_FOCUS_FOLLOW) {
7850 focus_win(win->ws->focus_pending);
7851 win->ws->focus_pending = NULL;
7855 } else if (e->state == XCB_PROPERTY_DELETE) {
7856 /* The window is no longer iconic, restack ws. */
7857 if (focus_mode != SWM_FOCUS_FOLLOW)
7858 win->ws->focus_pending = get_focus_magic(win);
7862 /* Flush EnterNotify for mapnotify, if needed. */
7865 } else if (e->atom == a_state) {
7866 /* State just changed, make sure it gets focused if mapped. */
7867 if (e->state == XCB_PROPERTY_NEW_VALUE) {
7868 if (focus_mode != SWM_FOCUS_FOLLOW) {
7870 win->ws->focus_pending == win) {
7871 focus_win(win->ws->focus_pending);
7872 win->ws->focus_pending = NULL;
7876 } else if (e->atom == XCB_ATOM_WM_CLASS ||
7877 e->atom == XCB_ATOM_WM_NAME) {
7885 unmapnotify(xcb_unmap_notify_event_t *e)
7888 struct workspace *ws;
7890 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: 0x%x\n", e->window);
7892 /* If we aren't managing the window, then ignore. */
7893 win = find_window(e->window);
7899 if (getstate(e->window) != XCB_ICCCM_WM_STATE_ICONIC)
7900 set_win_state(win, XCB_ICCCM_WM_STATE_ICONIC);
7903 /* window unmapped itself */
7904 /* do unmap/unfocus/restack and unmanage */
7907 /* If win was focused, make sure to focus on something else. */
7908 if (win == ws->focus) {
7909 if (focus_mode != SWM_FOCUS_FOLLOW) {
7910 ws->focus_pending = get_focus_prev(win);
7911 DNPRINTF(SWM_D_EVENT, "unmapnotify: "
7912 "focus_pending: 0x%x\n",
7913 WINID(ws->focus_pending));
7919 unmanage_window(win);
7924 if (focus_mode == SWM_FOCUS_FOLLOW) {
7926 focus_win(get_pointer_win(ws->r->s->root));
7928 if (ws->focus_pending) {
7929 focus_win(ws->focus_pending);
7930 ws->focus_pending = NULL;
7935 if (getstate(e->window) == XCB_ICCCM_WM_STATE_NORMAL)
7936 set_win_state(win, XCB_ICCCM_WM_STATE_ICONIC);
7943 visibilitynotify(xcb_visibility_notify_event_t *e)
7945 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: 0x%x\n",
7951 clientmessage(xcb_client_message_event_t *e)
7954 xcb_map_request_event_t mre;
7958 name = get_atom_name(e->type);
7959 DNPRINTF(SWM_D_EVENT, "clientmessage: window: 0x%x, atom: %s(%u)\n",
7960 e->window, name, e->type);
7963 win = find_window(e->window);
7966 if (e->type == ewmh[_NET_ACTIVE_WINDOW].atom) {
7967 /* Manage the window with maprequest. */
7968 DNPRINTF(SWM_D_EVENT, "clientmessage: request focus on "
7969 "unmanaged window.\n");
7970 mre.window = e->window;
7976 if (e->type == ewmh[_NET_ACTIVE_WINDOW].atom) {
7977 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_ACTIVE_WINDOW\n");
7980 if (e->type == ewmh[_NET_CLOSE_WINDOW].atom) {
7981 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_CLOSE_WINDOW\n");
7982 if (win->can_delete)
7983 client_msg(win, a_delete, 0);
7985 xcb_kill_client(conn, win->id);
7987 if (e->type == ewmh[_NET_MOVERESIZE_WINDOW].atom) {
7988 DNPRINTF(SWM_D_EVENT,
7989 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
7990 if (win->floating) {
7991 if (e->data.data32[0] & (1<<8)) /* x */
7992 X(win) = e->data.data32[1];
7993 if (e->data.data32[0] & (1<<9)) /* y */
7994 Y(win) = e->data.data32[2];
7995 if (e->data.data32[0] & (1<<10)) /* width */
7996 WIDTH(win) = e->data.data32[3];
7997 if (e->data.data32[0] & (1<<11)) /* height */
7998 HEIGHT(win) = e->data.data32[4];
8003 /* TODO: Change stack sizes */
8004 /* notify no change was made. */
8005 config_win(win, NULL);
8008 if (e->type == ewmh[_NET_WM_STATE].atom) {
8009 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_WM_STATE\n");
8010 ewmh_update_win_state(win, e->data.data32[1], e->data.data32[0]);
8011 if (e->data.data32[2])
8012 ewmh_update_win_state(win, e->data.data32[2],
8024 int errcode = xcb_connection_has_error(conn);
8025 #ifdef XCB_CONN_ERROR
8028 case XCB_CONN_ERROR:
8029 s = "Socket error, pipe error or other stream error.";
8031 case XCB_CONN_CLOSED_EXT_NOTSUPPORTED:
8032 s = "Extension not supported.";
8034 case XCB_CONN_CLOSED_MEM_INSUFFICIENT:
8035 s = "Insufficient memory.";
8037 case XCB_CONN_CLOSED_REQ_LEN_EXCEED:
8038 s = "Request length was exceeded.";
8040 case XCB_CONN_CLOSED_PARSE_ERR:
8041 s = "Error parsing display string.";
8044 s = "Unknown error.";
8047 errx(errcode, "X CONNECTION ERROR: %s", s);
8050 errx(errcode, "X CONNECTION ERROR");
8058 const uint32_t val = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
8059 XCB_EVENT_MASK_ENTER_WINDOW;
8061 xcb_void_cookie_t wac;
8062 xcb_generic_error_t *error;
8064 /* this causes an error if some other window manager is running */
8065 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
8066 for (i = 0; i < num_screens; i++) {
8067 if ((sc = get_screen(i)) == NULL)
8068 errx(1, "ERROR: can't get screen %d.", i);
8069 DNPRINTF(SWM_D_INIT, "enable_wm: screen %d, root: 0x%x\n",
8071 wac = xcb_change_window_attributes_checked(conn, sc->root,
8072 XCB_CW_EVENT_MASK, &val);
8073 if ((error = xcb_request_check(conn, wac))) {
8074 DNPRINTF(SWM_D_INIT, "enable_wm: error_code: %u\n",
8080 /* click to focus on empty region */
8081 xcb_grab_button(conn, 1, sc->root, BUTTONMASK,
8082 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC, XCB_WINDOW_NONE,
8083 XCB_CURSOR_NONE, XCB_BUTTON_INDEX_1, XCB_BUTTON_MASK_ANY);
8090 new_region(struct swm_screen *s, int x, int y, int w, int h)
8092 struct swm_region *r, *n;
8093 struct workspace *ws = NULL;
8096 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
8097 s->idx, w, h, x, y);
8099 /* remove any conflicting regions */
8100 n = TAILQ_FIRST(&s->rl);
8103 n = TAILQ_NEXT(r, entry);
8104 if (X(r) < (x + w) &&
8105 (X(r) + WIDTH(r)) > x &&
8107 (Y(r) + HEIGHT(r)) > y) {
8108 if (r->ws->r != NULL)
8109 r->ws->old_r = r->ws->r;
8112 TAILQ_REMOVE(&s->rl, r, entry);
8113 TAILQ_INSERT_TAIL(&s->orl, r, entry);
8117 /* search old regions for one to reuse */
8119 /* size + location match */
8120 TAILQ_FOREACH(r, &s->orl, entry)
8121 if (X(r) == x && Y(r) == y &&
8122 HEIGHT(r) == h && WIDTH(r) == w)
8126 TAILQ_FOREACH(r, &s->orl, entry)
8127 if (HEIGHT(r) == h && WIDTH(r) == w)
8131 TAILQ_REMOVE(&s->orl, r, entry);
8132 /* try to use old region's workspace */
8133 if (r->ws->r == NULL)
8136 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
8137 err(1, "new_region: calloc: failed to allocate memory "
8140 /* if we don't have a workspace already, find one */
8142 for (i = 0; i < workspace_limit; i++)
8143 if (s->ws[i].r == NULL) {
8150 errx(1, "new_region: no free workspaces");
8161 TAILQ_INSERT_TAIL(&s->rl, r, entry);
8167 #ifdef SWM_XRR_HAS_CRTC
8170 #endif /* SWM_XRR_HAS_CRTC */
8171 struct swm_region *r;
8173 xcb_randr_get_screen_resources_current_cookie_t src;
8174 xcb_randr_get_screen_resources_current_reply_t *srr;
8175 xcb_randr_get_crtc_info_cookie_t cic;
8176 xcb_randr_get_crtc_info_reply_t *cir = NULL;
8177 xcb_randr_crtc_t *crtc;
8178 xcb_screen_t *screen;
8180 DNPRINTF(SWM_D_MISC, "scan_xrandr: screen: %d\n", i);
8182 if ((screen = get_screen(i)) == NULL)
8183 errx(1, "ERROR: can't get screen %d.", i);
8185 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
8186 if (i >= num_screens)
8187 errx(1, "scan_xrandr: invalid screen");
8189 /* remove any old regions */
8190 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
8191 r->ws->old_r = r->ws->r = NULL;
8193 TAILQ_REMOVE(&screens[i].rl, r, entry);
8194 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
8198 /* map virtual screens onto physical screens */
8199 #ifdef SWM_XRR_HAS_CRTC
8200 if (xrandr_support) {
8201 src = xcb_randr_get_screen_resources_current(conn,
8203 srr = xcb_randr_get_screen_resources_current_reply(conn, src,
8206 new_region(&screens[i], 0, 0,
8207 screen->width_in_pixels,
8208 screen->height_in_pixels);
8211 ncrtc = srr->num_crtcs;
8213 crtc = xcb_randr_get_screen_resources_current_crtcs(srr);
8214 for (c = 0; c < ncrtc; c++) {
8215 cic = xcb_randr_get_crtc_info(conn, crtc[c],
8217 cir = xcb_randr_get_crtc_info_reply(conn, cic, NULL);
8220 if (cir->num_outputs == 0) {
8226 new_region(&screens[i], 0, 0,
8227 screen->width_in_pixels,
8228 screen->height_in_pixels);
8230 new_region(&screens[i],
8231 cir->x, cir->y, cir->width, cir->height);
8236 #endif /* SWM_XRR_HAS_CRTC */
8238 /* If detection failed, create a single region that spans the screen. */
8239 if (TAILQ_EMPTY(&screens[i].rl))
8240 new_region(&screens[i], 0, 0, screen->width_in_pixels,
8241 screen->height_in_pixels);
8244 DNPRINTF(SWM_D_MISC, "scan_xrandr: done.\n");
8248 screenchange(xcb_randr_screen_change_notify_event_t *e)
8250 struct swm_region *r;
8253 DNPRINTF(SWM_D_EVENT, "screenchange: root: 0x%x\n", e->root);
8255 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
8256 /* silly event doesn't include the screen index */
8257 for (i = 0; i < num_screens; i++)
8258 if (screens[i].root == e->root)
8260 if (i >= num_screens)
8261 errx(1, "screenchange: screen not found");
8263 /* brute force for now, just re-enumerate the regions */
8267 print_win_geom(e->root);
8269 /* add bars to all regions */
8270 for (i = 0; i < num_screens; i++)
8271 TAILQ_FOREACH(r, &screens[i].rl, entry)
8279 xcb_window_t *wins = NULL, trans;
8281 int i, j, num_screens;
8282 uint16_t state, manage, mapped;
8284 xcb_query_tree_cookie_t qtc;
8285 xcb_query_tree_reply_t *qtr;
8286 xcb_get_window_attributes_cookie_t c;
8287 xcb_get_window_attributes_reply_t *r;
8288 xcb_get_property_cookie_t pc;
8290 DNPRINTF(SWM_D_INIT, "grab_windows: begin\n");
8291 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
8292 for (i = 0; i < num_screens; i++) {
8293 qtc = xcb_query_tree(conn, screens[i].root);
8294 qtr = xcb_query_tree_reply(conn, qtc, NULL);
8297 wins = xcb_query_tree_children(qtr);
8298 no = xcb_query_tree_children_length(qtr);
8299 /* attach windows to a region */
8300 /* normal windows */
8301 DNPRINTF(SWM_D_INIT, "grab_windows: grab top level windows.\n");
8302 for (j = 0; j < no; j++) {
8303 c = xcb_get_window_attributes(conn, wins[j]);
8304 r = xcb_get_window_attributes_reply(conn, c, NULL);
8306 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
8307 "doesn't exist.\n", wins[j]);
8310 if (r->override_redirect) {
8311 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
8312 "override_redirect set.\n", wins[j]);
8317 pc = xcb_icccm_get_wm_transient_for(conn, wins[j]);
8318 if (xcb_icccm_get_wm_transient_for_reply(conn, pc,
8320 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
8321 "is transient for %#x.\n", wins[j], trans);
8326 state = getstate(wins[j]);
8327 manage = state != XCB_ICCCM_WM_STATE_WITHDRAWN;
8328 mapped = r->map_state != XCB_MAP_STATE_UNMAPPED;
8329 if (mapped || manage)
8330 manage_window(wins[j], mapped);
8333 /* transient windows */
8334 DNPRINTF(SWM_D_INIT, "grab_windows: grab transient windows.\n");
8335 for (j = 0; j < no; j++) {
8336 c = xcb_get_window_attributes(conn, wins[j]);
8337 r = xcb_get_window_attributes_reply(conn, c, NULL);
8339 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
8340 "doesn't exist.\n", wins[j]);
8343 if (r->override_redirect) {
8344 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
8345 "override_redirect set.\n", wins[j]);
8350 state = getstate(wins[j]);
8351 manage = state != XCB_ICCCM_WM_STATE_WITHDRAWN;
8352 mapped = r->map_state != XCB_MAP_STATE_UNMAPPED;
8353 pc = xcb_icccm_get_wm_transient_for(conn, wins[j]);
8354 if (xcb_icccm_get_wm_transient_for_reply(conn, pc,
8355 &trans, NULL) && manage)
8356 manage_window(wins[j], mapped);
8361 DNPRINTF(SWM_D_INIT, "grab_windows: done.\n");
8367 int i, j, k, num_screens;
8368 struct workspace *ws;
8369 uint32_t gcv[1], wa[1];
8370 const xcb_query_extension_reply_t *qep;
8371 xcb_screen_t *screen;
8372 xcb_randr_query_version_cookie_t c;
8373 xcb_randr_query_version_reply_t *r;
8375 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
8376 if ((screens = calloc(num_screens,
8377 sizeof(struct swm_screen))) == NULL)
8378 err(1, "setup_screens: calloc: failed to allocate memory for "
8381 /* initial Xrandr setup */
8383 qep = xcb_get_extension_data(conn, &xcb_randr_id);
8385 c = xcb_randr_query_version(conn, 1, 1);
8386 r = xcb_randr_query_version_reply(conn, c, NULL);
8388 if (r->major_version >= 1) {
8390 xrandr_eventbase = qep->first_event;
8396 wa[0] = cursors[XC_LEFT_PTR].cid;
8398 /* map physical screens */
8399 for (i = 0; i < num_screens; i++) {
8400 DNPRINTF(SWM_D_WS, "setup_screens: init screen: %d\n", i);
8402 TAILQ_INIT(&screens[i].rl);
8403 TAILQ_INIT(&screens[i].orl);
8404 if ((screen = get_screen(i)) == NULL)
8405 errx(1, "ERROR: can't get screen %d.", i);
8406 screens[i].root = screen->root;
8408 /* set default colors */
8409 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
8410 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
8411 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
8412 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
8413 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
8415 /* create graphics context on screen */
8416 screens[i].bar_gc = xcb_generate_id(conn);
8418 xcb_create_gc(conn, screens[i].bar_gc, screens[i].root,
8419 XCB_GC_GRAPHICS_EXPOSURES, gcv);
8421 /* set default cursor */
8422 xcb_change_window_attributes(conn, screens[i].root,
8425 /* init all workspaces */
8426 /* XXX these should be dynamically allocated too */
8427 for (j = 0; j < SWM_WS_MAX; j++) {
8428 ws = &screens[i].ws[j];
8432 ws->focus_prev = NULL;
8433 ws->focus_pending = NULL;
8436 TAILQ_INIT(&ws->winlist);
8437 TAILQ_INIT(&ws->unmanagedlist);
8439 for (k = 0; layouts[k].l_stack != NULL; k++)
8440 if (layouts[k].l_config != NULL)
8441 layouts[k].l_config(ws,
8442 SWM_ARG_ID_STACKINIT);
8443 ws->cur_layout = &layouts[0];
8444 ws->cur_layout->l_string(ws);
8450 xcb_randr_select_input(conn, screens[i].root,
8451 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE);
8458 if ((bar_fonts = strdup(SWM_BAR_FONTS)) == NULL)
8459 err(1, "setup_globals: strdup: failed to allocate memory.");
8461 if ((clock_format = strdup("%a %b %d %R %Z %Y")) == NULL)
8462 err(1, "setup_globals: strdup: failed to allocate memory.");
8464 if ((syms = xcb_key_symbols_alloc(conn)) == NULL)
8465 errx(1, "unable to allocate key symbols");
8467 a_state = get_atom_from_string("WM_STATE");
8468 a_prot = get_atom_from_string("WM_PROTOCOLS");
8469 a_delete = get_atom_from_string("WM_DELETE_WINDOW");
8470 a_takefocus = get_atom_from_string("WM_TAKE_FOCUS");
8471 a_wmname = get_atom_from_string("WM_NAME");
8472 a_netwmname = get_atom_from_string("_NET_WM_NAME");
8473 a_utf8_string = get_atom_from_string("UTF8_STRING");
8474 a_string = get_atom_from_string("STRING");
8475 a_swm_iconic = get_atom_from_string("_SWM_ICONIC");
8476 a_swm_ws = get_atom_from_string("_SWM_WS");
8483 xcb_atom_t netwmcheck;
8484 xcb_window_t root, win;
8486 /* work around sun jdk bugs, code from wmname */
8487 netwmcheck = get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
8489 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
8490 for (i = 0; i < num_screens; i++) {
8491 root = screens[i].root;
8493 win = xcb_generate_id(conn);
8494 xcb_create_window(conn, XCB_COPY_FROM_PARENT, win, root,
8495 0, 0, 1, 1, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT,
8496 XCB_COPY_FROM_PARENT, 0, NULL);
8498 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root,
8499 netwmcheck, XCB_ATOM_WINDOW, 32, 1, &win);
8500 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win,
8501 netwmcheck, XCB_ATOM_WINDOW, 32, 1, &win);
8502 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win,
8503 a_netwmname, a_utf8_string, 8, strlen("LG3D"), "LG3D");
8508 shutdown_cleanup(void)
8512 /* disable alarm because the following code may not be interrupted */
8514 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
8515 err(1, "can't disable alarm");
8525 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
8526 for (i = 0; i < num_screens; ++i) {
8527 if (screens[i].bar_gc != 0)
8528 xcb_free_gc(conn, screens[i].bar_gc);
8529 if (!bar_font_legacy)
8530 XftColorFree(display, DefaultVisual(display, i),
8531 DefaultColormap(display, i), &bar_font_color);
8534 if (bar_font_legacy)
8535 XFreeFontSet(display, bar_fs);
8537 XftFontClose(display, bar_font);
8540 xcb_key_symbols_free(syms);
8542 xcb_disconnect(conn);
8546 event_error(xcb_generic_error_t *e)
8550 DNPRINTF(SWM_D_EVENT, "event_error: %s(%u) from %s(%u), sequence: %u, "
8551 "resource_id: %u, minor_code: %u\n",
8552 xcb_event_get_error_label(e->error_code), e->error_code,
8553 xcb_event_get_request_label(e->major_code), e->major_code,
8554 e->sequence, e->resource_id, e->minor_code);
8558 event_handle(xcb_generic_event_t *evt)
8560 uint8_t type = XCB_EVENT_RESPONSE_TYPE(evt);
8562 DNPRINTF(SWM_D_EVENT, "XCB Event: %s(%d), seq %u\n",
8563 xcb_event_get_label(XCB_EVENT_RESPONSE_TYPE(evt)),
8564 XCB_EVENT_RESPONSE_TYPE(evt), evt->sequence);
8567 #define EVENT(type, callback) case type: callback((void *)evt); return
8568 EVENT(0, event_error);
8569 EVENT(XCB_BUTTON_PRESS, buttonpress);
8570 /*EVENT(XCB_BUTTON_RELEASE, buttonpress);*/
8571 /*EVENT(XCB_CIRCULATE_NOTIFY, );*/
8572 /*EVENT(XCB_CIRCULATE_REQUEST, );*/
8573 EVENT(XCB_CLIENT_MESSAGE, clientmessage);
8574 /*EVENT(XCB_COLORMAP_NOTIFY, );*/
8575 EVENT(XCB_CONFIGURE_NOTIFY, configurenotify);
8576 EVENT(XCB_CONFIGURE_REQUEST, configurerequest);
8577 /*EVENT(XCB_CREATE_NOTIFY, );*/
8578 EVENT(XCB_DESTROY_NOTIFY, destroynotify);
8579 EVENT(XCB_ENTER_NOTIFY, enternotify);
8580 EVENT(XCB_EXPOSE, expose);
8582 EVENT(XCB_FOCUS_IN, focusin);
8583 EVENT(XCB_FOCUS_OUT, focusout);
8585 /*EVENT(XCB_GRAPHICS_EXPOSURE, );*/
8586 /*EVENT(XCB_GRAVITY_NOTIFY, );*/
8587 EVENT(XCB_KEY_PRESS, keypress);
8588 /*EVENT(XCB_KEY_RELEASE, keypress);*/
8589 /*EVENT(XCB_KEYMAP_NOTIFY, );*/
8591 EVENT(XCB_LEAVE_NOTIFY, leavenotify);
8593 EVENT(XCB_MAP_NOTIFY, mapnotify);
8594 EVENT(XCB_MAP_REQUEST, maprequest);
8595 EVENT(XCB_MAPPING_NOTIFY, mappingnotify);
8596 /*EVENT(XCB_MOTION_NOTIFY, );*/
8597 /*EVENT(XCB_NO_EXPOSURE, );*/
8598 EVENT(XCB_PROPERTY_NOTIFY, propertynotify);
8599 /*EVENT(XCB_REPARENT_NOTIFY, );*/
8600 /*EVENT(XCB_RESIZE_REQUEST, );*/
8601 /*EVENT(XCB_SELECTION_CLEAR, );*/
8602 /*EVENT(XCB_SELECTION_NOTIFY, );*/
8603 /*EVENT(XCB_SELECTION_REQUEST, );*/
8604 EVENT(XCB_UNMAP_NOTIFY, unmapnotify);
8605 /*EVENT(XCB_VISIBILITY_NOTIFY, visibilitynotify);*/
8608 if (type - xrandr_eventbase == XCB_RANDR_SCREEN_CHANGE_NOTIFY)
8609 screenchange((void *)evt);
8613 main(int argc, char *argv[])
8615 struct swm_region *r, *rr;
8616 struct ws_win *winfocus = NULL;
8617 char conf[PATH_MAX], *cfile = NULL;
8619 int xfd, i, num_screens;
8620 struct sigaction sact;
8621 xcb_generic_event_t *evt;
8625 /* suppress unused warning since var is needed */
8628 time_started = time(NULL);
8631 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION, buildstr);
8632 if (!setlocale(LC_CTYPE, "") || !setlocale(LC_TIME, ""))
8633 warnx("no locale support");
8635 /* handle some signals */
8636 bzero(&sact, sizeof(sact));
8637 sigemptyset(&sact.sa_mask);
8639 sact.sa_handler = sighdlr;
8640 sigaction(SIGINT, &sact, NULL);
8641 sigaction(SIGQUIT, &sact, NULL);
8642 sigaction(SIGTERM, &sact, NULL);
8643 sigaction(SIGHUP, &sact, NULL);
8645 sact.sa_handler = sighdlr;
8646 sact.sa_flags = SA_NOCLDSTOP;
8647 sigaction(SIGCHLD, &sact, NULL);
8649 if (!(display = XOpenDisplay(0)))
8650 errx(1, "can not open display");
8652 conn = XGetXCBConnection(display);
8653 if (xcb_connection_has_error(conn))
8654 errx(1, "can not get XCB connection");
8656 XSetEventQueueOwner(display, XCBOwnsEventQueue);
8658 xcb_prefetch_extension_data(conn, &xcb_randr_id);
8659 xfd = xcb_get_file_descriptor(conn);
8661 /* look for local and global conf file */
8662 pwd = getpwuid(getuid());
8664 errx(1, "invalid user: %d", getuid());
8666 xcb_grab_server(conn);
8669 /* flush all events */
8670 while ((evt = xcb_poll_for_event(conn))) {
8671 if (XCB_EVENT_RESPONSE_TYPE(evt) == 0)
8676 if (enable_wm() != 0)
8677 errx(1, "another window manager is currently running");
8679 /* Load Xcursors and/or cursorfont glyph cursors. */
8691 for (i = 0; ; i++) {
8696 snprintf(conf, sizeof conf, "%s/.%s",
8697 pwd->pw_dir, SWM_CONF_FILE);
8701 snprintf(conf, sizeof conf, "/etc/%s",
8706 snprintf(conf, sizeof conf, "%s/.%s",
8707 pwd->pw_dir, SWM_CONF_FILE_OLD);
8711 snprintf(conf, sizeof conf, "/etc/%s",
8718 if (strlen(conf) && stat(conf, &sb) != -1)
8719 if (S_ISREG(sb.st_mode)) {
8726 /* load conf (if any) */
8728 conf_load(cfile, SWM_CONF_DEFAULT);
8731 /* set some values to work around bad programs */
8733 /* grab existing windows (before we build the bars) */
8736 if (getenv("SWM_STARTED") == NULL)
8737 setenv("SWM_STARTED", "YES", 1);
8739 /* setup all bars */
8740 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
8741 for (i = 0; i < num_screens; i++)
8742 TAILQ_FOREACH(r, &screens[i].rl, entry) {
8743 if (winfocus == NULL)
8744 winfocus = TAILQ_FIRST(&r->ws->winlist);
8751 xcb_ungrab_server(conn);
8755 while ((evt = xcb_poll_for_event(conn))) {
8762 /* If just (re)started, set default focus if needed. */
8763 if (winfocus && focus_mode != SWM_FOCUS_FOLLOW) {
8764 rr = winfocus->ws->r;
8766 /* not a visible window */
8771 focus_win(get_region_focus(rr));
8781 if (select(xfd + 1, &rd, NULL, NULL, &tv) == -1)
8782 if (errno != EINTR) {
8783 DNPRINTF(SWM_D_MISC, "select failed");
8786 restart(NULL, NULL);