2 * Copyright (c) 2009-2012 Marco Peereboom <marco@peereboom.us>
3 * Copyright (c) 2009-2011 Ryan McBride <mcbride@countersiege.com>
4 * Copyright (c) 2009 Darrin Chandler <dwchandler@stilyagin.com>
5 * Copyright (c) 2009 Pierre-Yves Ritschard <pyr@spootnik.org>
6 * Copyright (c) 2010 Tuukka Kataja <stuge@xor.fi>
7 * Copyright (c) 2011 Jason L. Wright <jason@thought.net>
8 * Copyright (c) 2011-2013 Reginald Kennedy <rk@rejii.com>
9 * Copyright (c) 2011-2012 Lawrence Teo <lteo@lteo.net>
10 * Copyright (c) 2011-2012 Tiago Cunha <tcunha@gmx.com>
11 * Copyright (c) 2012-2013 David Hill <dhill@mindcry.org>
13 * Permission to use, copy, modify, and distribute this software for any
14 * purpose with or without fee is hereby granted, provided that the above
15 * copyright notice and this permission notice appear in all copies.
17 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
18 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
20 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
21 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
22 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
23 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 * Much code and ideas taken from dwm under the following license:
27 * MIT/X Consortium License
29 * 2006-2008 Anselm R Garbe <garbeam at gmail dot com>
30 * 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
31 * 2006-2007 Jukka Salmi <jukka at salmi dot ch>
32 * 2007 Premysl Hruby <dfenze at gmail dot com>
33 * 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
34 * 2007 Christof Musik <christof at sendfax dot de>
35 * 2007-2008 Enno Gottox Boland <gottox at s01 dot de>
36 * 2007-2008 Peter Hartlich <sgkkr at hartlich dot com>
37 * 2008 Martin Hurton <martin dot hurton at gmail dot com>
39 * Permission is hereby granted, free of charge, to any person obtaining a
40 * copy of this software and associated documentation files (the "Software"),
41 * to deal in the Software without restriction, including without limitation
42 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
43 * and/or sell copies of the Software, and to permit persons to whom the
44 * Software is furnished to do so, subject to the following conditions:
46 * The above copyright notice and this permission notice shall be included in
47 * all copies or substantial portions of the Software.
49 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
50 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
51 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
52 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
53 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
54 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
55 * DEALINGS IN THE SOFTWARE.
59 #include <sys/types.h>
63 #include <sys/queue.h>
64 #include <sys/param.h>
65 #include <sys/select.h>
66 #if defined(__linux__)
68 #elif defined(__OpenBSD__)
70 #elif defined(__FreeBSD__)
91 #include <X11/cursorfont.h>
92 #include <X11/extensions/Xrandr.h>
93 #include <X11/Xcursor/Xcursor.h>
94 #include <X11/Xft/Xft.h>
95 #include <X11/Xlib-xcb.h>
96 #include <xcb/xcb_atom.h>
97 #include <xcb/xcb_aux.h>
98 #include <xcb/xcb_event.h>
99 #include <xcb/xcb_icccm.h>
100 #include <xcb/xcb_keysyms.h>
101 #include <xcb/xtest.h>
102 #include <xcb/randr.h>
110 #ifdef SPECTRWM_BUILDSTR
111 static const char *buildstr = SPECTRWM_BUILDSTR;
113 static const char *buildstr = SPECTRWM_VERSION;
116 #if !defined(__CYGWIN__) /* cygwin chokes on xrandr stuff */
118 # error XRandR versions less than 1.0 are not supported
121 # if RANDR_MAJOR >= 1
122 # if RANDR_MINOR >= 2
123 # define SWM_XRR_HAS_CRTC
126 #endif /* __CYGWIN__ */
128 #ifndef XCB_ICCCM_NUM_WM_HINTS_ELEMENTS
129 #define XCB_ICCCM_SIZE_HINT_P_MIN_SIZE XCB_SIZE_HINT_P_MIN_SIZE
130 #define XCB_ICCCM_SIZE_HINT_P_MAX_SIZE XCB_SIZE_HINT_P_MAX_SIZE
131 #define XCB_ICCCM_SIZE_HINT_P_RESIZE_INC XCB_SIZE_HINT_P_RESIZE_INC
132 #define XCB_ICCCM_WM_HINT_INPUT XCB_WM_HINT_INPUT
133 #define XCB_ICCCM_WM_HINT_X_URGENCY XCB_WM_HINT_X_URGENCY
134 #define XCB_ICCCM_WM_STATE_ICONIC XCB_WM_STATE_ICONIC
135 #define XCB_ICCCM_WM_STATE_WITHDRAWN XCB_WM_STATE_WITHDRAWN
136 #define XCB_ICCCM_WM_STATE_NORMAL XCB_WM_STATE_NORMAL
137 #define xcb_icccm_get_text_property_reply_t xcb_get_text_property_reply_t
138 #define xcb_icccm_get_text_property_reply_wipe xcb_get_text_property_reply_wipe
139 #define xcb_icccm_get_wm_class xcb_get_wm_class
140 #define xcb_icccm_get_wm_class_reply xcb_get_wm_class_reply
141 #define xcb_icccm_get_wm_class_reply_t xcb_get_wm_class_reply_t
142 #define xcb_icccm_get_wm_class_reply_wipe xcb_get_wm_class_reply_wipe
143 #define xcb_icccm_get_wm_hints xcb_get_wm_hints
144 #define xcb_icccm_get_wm_hints_reply xcb_get_wm_hints_reply
145 #define xcb_icccm_get_wm_name xcb_get_wm_name
146 #define xcb_icccm_get_wm_name_reply xcb_get_wm_name_reply
147 #define xcb_icccm_get_wm_normal_hints xcb_get_wm_normal_hints
148 #define xcb_icccm_get_wm_normal_hints_reply xcb_get_wm_normal_hints_reply
149 #define xcb_icccm_get_wm_protocols xcb_get_wm_protocols
150 #define xcb_icccm_get_wm_protocols_reply xcb_get_wm_protocols_reply
151 #define xcb_icccm_get_wm_protocols_reply_t xcb_get_wm_protocols_reply_t
152 #define xcb_icccm_get_wm_protocols_reply_wipe xcb_get_wm_protocols_reply_wipe
153 #define xcb_icccm_get_wm_transient_for xcb_get_wm_transient_for
154 #define xcb_icccm_get_wm_transient_for_reply xcb_get_wm_transient_for_reply
155 #define xcb_icccm_wm_hints_t xcb_wm_hints_t
158 /*#define SWM_DEBUG*/
160 #define DPRINTF(x...) do { \
162 fprintf(stderr, x); \
164 #define DNPRINTF(n,x...) do { \
165 if (swm_debug & n) { \
166 fprintf(stderr, "%ld ", (long)(time(NULL) - time_started)); \
167 fprintf(stderr, x); \
170 #define SWM_D_MISC 0x0001
171 #define SWM_D_EVENT 0x0002
172 #define SWM_D_WS 0x0004
173 #define SWM_D_FOCUS 0x0008
174 #define SWM_D_MOVE 0x0010
175 #define SWM_D_STACK 0x0020
176 #define SWM_D_MOUSE 0x0040
177 #define SWM_D_PROP 0x0080
178 #define SWM_D_CLASS 0x0100
179 #define SWM_D_KEY 0x0200
180 #define SWM_D_QUIRK 0x0400
181 #define SWM_D_SPAWN 0x0800
182 #define SWM_D_EVENTQ 0x1000
183 #define SWM_D_CONF 0x2000
184 #define SWM_D_BAR 0x4000
185 #define SWM_D_INIT 0x8000
187 u_int32_t swm_debug = 0
206 #define DPRINTF(x...)
207 #define DNPRINTF(n,x...)
210 /* convert 8-bit to 16-bit */
211 #define RGB_8_TO_16(col) ((col) << 8) + (col)
213 #define PIXEL_TO_XRENDERCOLOR(px, xrc) \
214 xrc.red = RGB_8_TO_16((px) >> 16 & 0xff); \
215 xrc.green = RGB_8_TO_16((px) >> 8 & 0xff); \
216 xrc.blue = RGB_8_TO_16((px) & 0xff); \
219 #define LENGTH(x) (int)(sizeof (x) / sizeof (x)[0])
220 #define MODKEY XCB_MOD_MASK_1
221 #define CLEANMASK(mask) ((mask) & ~(numlockmask | XCB_MOD_MASK_LOCK))
222 #define BUTTONMASK (XCB_EVENT_MASK_BUTTON_PRESS | \
223 XCB_EVENT_MASK_BUTTON_RELEASE)
224 #define MOUSEMASK (BUTTONMASK|XCB_EVENT_MASK_POINTER_MOTION)
225 #define SWM_PROPLEN (16)
226 #define SWM_FUNCNAME_LEN (32)
227 #define SWM_KEYS_LEN (255)
228 #define SWM_QUIRK_LEN (64)
229 #define X(r) (r)->g.x
230 #define Y(r) (r)->g.y
231 #define WIDTH(r) (r)->g.w
232 #define HEIGHT(r) (r)->g.h
233 #define BORDER(w) ((w)->bordered ? border_width : 0)
234 #define MAX_X(r) ((r)->g.x + (r)->g.w)
235 #define MAX_Y(r) ((r)->g.y + (r)->g.h)
236 #define SH_MIN(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE
237 #define SH_MIN_W(w) (w)->sh.min_width
238 #define SH_MIN_H(w) (w)->sh.min_height
239 #define SH_MAX(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MAX_SIZE
240 #define SH_MAX_W(w) (w)->sh.max_width
241 #define SH_MAX_H(w) (w)->sh.max_height
242 #define SH_INC(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_RESIZE_INC
243 #define SH_INC_W(w) (w)->sh.width_inc
244 #define SH_INC_H(w) (w)->sh.height_inc
245 #define SWM_MAX_FONT_STEPS (3)
246 #define WINID(w) ((w) ? (w)->id : XCB_WINDOW_NONE)
247 #define YESNO(x) ((x) ? "yes" : "no")
249 /* Constrain Window flags */
250 #define SWM_CW_RESIZABLE (0x01)
251 #define SWM_CW_SOFTBOUNDARY (0x02)
252 #define SWM_CW_HARDBOUNDARY (0x04)
253 #define SWM_CW_RIGHT (0x10)
254 #define SWM_CW_LEFT (0x20)
255 #define SWM_CW_BOTTOM (0x40)
256 #define SWM_CW_TOP (0x80)
257 #define SWM_CW_ALLSIDES (0xf0)
259 #define SWM_FOCUS_DEFAULT (0)
260 #define SWM_FOCUS_FOLLOW (1)
261 #define SWM_FOCUS_MANUAL (2)
263 #define SWM_CK_NONE 0
264 #define SWM_CK_ALL 0xf
265 #define SWM_CK_FOCUS 0x1
266 #define SWM_CK_POINTER 0x2
267 #define SWM_CK_FALLBACK 0x4
268 #define SWM_CK_REGION 0x8
270 #define SWM_CONF_DEFAULT (0)
271 #define SWM_CONF_KEYMAPPING (1)
274 #define SWM_LIB "/usr/local/lib/libswmhack.so"
281 xcb_atom_t a_takefocus;
282 xcb_atom_t a_utf8_string;
283 xcb_atom_t a_swm_iconic;
285 volatile sig_atomic_t running = 1;
286 volatile sig_atomic_t restart_wm = 0;
287 xcb_timestamp_t last_event_time = 0;
291 int xrandr_eventbase;
292 unsigned int numlockmask = 0;
295 xcb_connection_t *conn;
296 xcb_key_symbols_t *syms;
298 int boundary_width = 50;
300 int cycle_visible = 0;
302 int font_adjusted = 0;
303 unsigned int mod_key = MODKEY;
306 struct swm_region *search_r;
307 int select_list_pipe[2];
308 int select_resp_pipe[2];
310 volatile sig_atomic_t search_resp;
311 int search_resp_action;
313 struct search_window {
314 TAILQ_ENTRY(search_window) entry;
318 xcb_window_t indicator;
320 TAILQ_HEAD(search_winlist, search_window);
321 struct search_winlist search_wl;
326 SWM_SEARCH_UNICONIFY,
327 SWM_SEARCH_NAME_WORKSPACE,
328 SWM_SEARCH_SEARCH_WORKSPACE,
329 SWM_SEARCH_SEARCH_WINDOW
332 #define SWM_STACK_TOP (0)
333 #define SWM_STACK_BOTTOM (1)
334 #define SWM_STACK_ABOVE (2)
335 #define SWM_STACK_BELOW (3)
338 double dialog_ratio = 0.6;
340 #define SWM_BAR_MAX (256)
341 #define SWM_BAR_JUSTIFY_LEFT (0)
342 #define SWM_BAR_JUSTIFY_CENTER (1)
343 #define SWM_BAR_JUSTIFY_RIGHT (2)
344 #define SWM_BAR_OFFSET (4)
345 #define SWM_BAR_FONTS "-*-terminus-medium-*-*-*-12-*-*-*-*-*-*-*," \
346 "-*-profont-*-*-*-*-12-*-*-*-*-*-*-*," \
347 "-*-times-medium-r-*-*-12-*-*-*-*-*-*-*," \
348 "-misc-fixed-medium-r-*-*-12-*-*-*-*-*-*-*," \
349 "-*-*-*-r-*-*-*-*-*-*-*-*-*-*"
351 #ifdef X_HAVE_UTF8_STRING
352 #define DRAWSTRING(x...) Xutf8DrawString(x)
354 #define DRAWSTRING(x...) XmbDrawString(x)
357 char *bar_argv[] = { NULL, NULL };
359 char bar_ext[SWM_BAR_MAX];
360 char bar_ext_buf[SWM_BAR_MAX];
361 char bar_vertext[SWM_BAR_MAX];
364 int bar_border_width = 1;
365 int bar_at_bottom = 0;
369 int bar_justify = SWM_BAR_JUSTIFY_LEFT;
370 char *bar_format = NULL;
371 int stack_enabled = 1;
372 int clock_enabled = 1;
373 int urgent_enabled = 0;
374 char *clock_format = NULL;
375 int title_name_enabled = 0;
376 int title_class_enabled = 0;
377 int window_name_enabled = 0;
378 int focus_mode = SWM_FOCUS_DEFAULT;
379 int focus_close = SWM_STACK_BELOW;
380 int focus_close_wrap = 1;
381 int focus_default = SWM_STACK_TOP;
382 int spawn_position = SWM_STACK_TOP;
383 int disable_border = 0;
384 int border_width = 1;
385 int region_padding = 0;
387 int verbose_layout = 0;
391 XFontSetExtents *bar_fs_extents;
393 int bar_font_legacy = 1;
395 XftColor bar_font_color;
397 char *startup_exception;
398 unsigned int nr_exceptions = 0;
400 /* layout manager data */
401 struct swm_geometry {
414 struct swm_geometry g;
417 /* virtual "screens" */
419 TAILQ_ENTRY(swm_region) entry;
421 struct swm_geometry g;
422 struct workspace *ws; /* current workspace on this region */
423 struct workspace *ws_prior; /* prior workspace on this region */
424 struct swm_screen *s; /* screen idx */
427 TAILQ_HEAD(swm_region_list, swm_region);
430 TAILQ_ENTRY(ws_win) entry;
432 xcb_window_t transient;
433 struct ws_win *focus_child; /* focus on child transient */
434 struct swm_geometry g; /* current geometry */
435 struct swm_geometry g_float; /* region coordinates */
436 int g_floatvalid; /* g_float geometry validity */
437 int floatmaxed; /* whether maxed by max_stack */
443 unsigned int ewmh_flags;
444 int font_size_boundary[SWM_MAX_FONT_STEPS];
450 unsigned long quirks;
451 struct workspace *ws; /* always valid */
452 struct swm_screen *s; /* always valid, never changes */
453 xcb_get_geometry_reply_t *wa;
455 xcb_icccm_get_wm_class_reply_t ch;
456 xcb_icccm_wm_hints_t hints;
458 TAILQ_HEAD(ws_win_list, ws_win);
462 TAILQ_ENTRY(pid_e) entry;
466 TAILQ_HEAD(pid_list, pid_e);
467 struct pid_list pidlist = TAILQ_HEAD_INITIALIZER(pidlist);
469 /* layout handlers */
471 void vertical_config(struct workspace *, int);
472 void vertical_stack(struct workspace *, struct swm_geometry *);
473 void horizontal_config(struct workspace *, int);
474 void horizontal_stack(struct workspace *, struct swm_geometry *);
475 void max_stack(struct workspace *, struct swm_geometry *);
476 void plain_stacker(struct workspace *);
477 void fancy_stacker(struct workspace *);
480 void (*l_stack)(struct workspace *, struct swm_geometry *);
481 void (*l_config)(struct workspace *, int);
483 #define SWM_L_FOCUSPREV (1<<0)
484 #define SWM_L_MAPONFOCUS (1<<1)
485 void (*l_string)(struct workspace *);
487 /* stack, configure */
488 { vertical_stack, vertical_config, 0, plain_stacker },
489 { horizontal_stack, horizontal_config, 0, plain_stacker },
491 SWM_L_MAPONFOCUS | SWM_L_FOCUSPREV, plain_stacker },
492 { NULL, NULL, 0, NULL },
495 /* position of max_stack mode in the layouts array, index into layouts! */
496 #define SWM_V_STACK (0)
497 #define SWM_H_STACK (1)
498 #define SWM_MAX_STACK (2)
500 #define SWM_H_SLICE (32)
501 #define SWM_V_SLICE (32)
503 /* define work spaces */
505 int idx; /* workspace index */
506 char *name; /* workspace name */
507 int always_raise; /* raise windows on focus */
508 int bar_enabled; /* bar visibility */
509 struct layout *cur_layout; /* current layout handlers */
510 struct ws_win *focus; /* may be NULL */
511 struct ws_win *focus_prev; /* may be NULL */
512 struct ws_win *focus_pending; /* may be NULL */
513 struct swm_region *r; /* may be NULL */
514 struct swm_region *old_r; /* may be NULL */
515 struct ws_win_list winlist; /* list of windows in ws */
516 struct ws_win_list unmanagedlist; /* list of dead windows in ws */
517 char stacker[10]; /* display stacker and layout */
521 int horizontal_msize;
523 int horizontal_stacks;
534 SWM_S_COLOR_BAR_BORDER,
535 SWM_S_COLOR_BAR_BORDER_UNFOCUS,
536 SWM_S_COLOR_BAR_FONT,
542 /* physical screen mapping */
543 #define SWM_WS_MAX (22) /* hard limit */
544 int workspace_limit = 10; /* soft limit */
547 int idx; /* screen index */
548 struct swm_region_list rl; /* list of regions on this screen */
549 struct swm_region_list orl; /* list of old regions */
551 struct workspace ws[SWM_WS_MAX];
552 struct swm_region *r_focus;
558 } c[SWM_S_COLOR_MAX];
560 xcb_gcontext_t bar_gc;
563 struct swm_screen *screens;
565 /* args to functions */
568 #define SWM_ARG_ID_FOCUSNEXT (0)
569 #define SWM_ARG_ID_FOCUSPREV (1)
570 #define SWM_ARG_ID_FOCUSMAIN (2)
571 #define SWM_ARG_ID_SWAPNEXT (10)
572 #define SWM_ARG_ID_SWAPPREV (11)
573 #define SWM_ARG_ID_SWAPMAIN (12)
574 #define SWM_ARG_ID_MOVELAST (13)
575 #define SWM_ARG_ID_MASTERSHRINK (20)
576 #define SWM_ARG_ID_MASTERGROW (21)
577 #define SWM_ARG_ID_MASTERADD (22)
578 #define SWM_ARG_ID_MASTERDEL (23)
579 #define SWM_ARG_ID_FLIPLAYOUT (24)
580 #define SWM_ARG_ID_STACKRESET (30)
581 #define SWM_ARG_ID_STACKINIT (31)
582 #define SWM_ARG_ID_CYCLEWS_UP (40)
583 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
584 #define SWM_ARG_ID_CYCLERG_UP (42)
585 #define SWM_ARG_ID_CYCLERG_DOWN (43)
586 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
587 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
588 #define SWM_ARG_ID_CYCLEWS_MOVE_UP (46)
589 #define SWM_ARG_ID_CYCLEWS_MOVE_DOWN (47)
590 #define SWM_ARG_ID_STACKINC (50)
591 #define SWM_ARG_ID_STACKDEC (51)
592 #define SWM_ARG_ID_SS_ALL (60)
593 #define SWM_ARG_ID_SS_WINDOW (61)
594 #define SWM_ARG_ID_DONTCENTER (70)
595 #define SWM_ARG_ID_CENTER (71)
596 #define SWM_ARG_ID_KILLWINDOW (80)
597 #define SWM_ARG_ID_DELETEWINDOW (81)
598 #define SWM_ARG_ID_WIDTHGROW (90)
599 #define SWM_ARG_ID_WIDTHSHRINK (91)
600 #define SWM_ARG_ID_HEIGHTGROW (92)
601 #define SWM_ARG_ID_HEIGHTSHRINK (93)
602 #define SWM_ARG_ID_MOVEUP (100)
603 #define SWM_ARG_ID_MOVEDOWN (101)
604 #define SWM_ARG_ID_MOVELEFT (102)
605 #define SWM_ARG_ID_MOVERIGHT (103)
606 #define SWM_ARG_ID_BAR_TOGGLE (110)
607 #define SWM_ARG_ID_BAR_TOGGLE_WS (111)
613 TAILQ_ENTRY(quirk) entry;
617 #define SWM_Q_FLOAT (1<<0) /* float this window */
618 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
619 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
620 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
621 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
622 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
623 #define SWM_Q_NOFOCUSONMAP (1<<6) /* Don't focus on window when mapped. */
624 #define SWM_Q_FOCUSONMAP_SINGLE (1<<7) /* Only focus if single win of type. */
626 TAILQ_HEAD(quirk_list, quirk);
627 struct quirk_list quirks = TAILQ_HEAD_INITIALIZER(quirks);
630 * Supported EWMH hints should be added to
631 * both the enum and the ewmh array
636 _NET_MOVERESIZE_WINDOW,
637 _NET_WM_ACTION_ABOVE,
638 _NET_WM_ACTION_CLOSE,
639 _NET_WM_ACTION_FULLSCREEN,
641 _NET_WM_ACTION_RESIZE,
642 _NET_WM_ALLOWED_ACTIONS,
646 _NET_WM_STATE_FULLSCREEN,
647 _NET_WM_STATE_HIDDEN,
648 _NET_WM_STATE_MAXIMIZED_HORZ,
649 _NET_WM_STATE_MAXIMIZED_VERT,
650 _NET_WM_STATE_SKIP_PAGER,
651 _NET_WM_STATE_SKIP_TASKBAR,
653 _NET_WM_WINDOW_TYPE_DIALOG,
654 _NET_WM_WINDOW_TYPE_DOCK,
655 _NET_WM_WINDOW_TYPE_NORMAL,
656 _NET_WM_WINDOW_TYPE_SPLASH,
657 _NET_WM_WINDOW_TYPE_TOOLBAR,
658 _NET_WM_WINDOW_TYPE_UTILITY,
659 _SWM_WM_STATE_MANUAL,
666 } ewmh[SWM_EWMH_HINT_MAX] = {
667 /* must be in same order as in the enum */
668 {"_NET_ACTIVE_WINDOW", XCB_ATOM_NONE},
669 {"_NET_CLOSE_WINDOW", XCB_ATOM_NONE},
670 {"_NET_MOVERESIZE_WINDOW", XCB_ATOM_NONE},
671 {"_NET_WM_ACTION_ABOVE", XCB_ATOM_NONE},
672 {"_NET_WM_ACTION_CLOSE", XCB_ATOM_NONE},
673 {"_NET_WM_ACTION_FULLSCREEN", XCB_ATOM_NONE},
674 {"_NET_WM_ACTION_MOVE", XCB_ATOM_NONE},
675 {"_NET_WM_ACTION_RESIZE", XCB_ATOM_NONE},
676 {"_NET_WM_ALLOWED_ACTIONS", XCB_ATOM_NONE},
677 {"_NET_WM_NAME", XCB_ATOM_NONE},
678 {"_NET_WM_STATE", XCB_ATOM_NONE},
679 {"_NET_WM_STATE_ABOVE", XCB_ATOM_NONE},
680 {"_NET_WM_STATE_FULLSCREEN", XCB_ATOM_NONE},
681 {"_NET_WM_STATE_HIDDEN", XCB_ATOM_NONE},
682 {"_NET_WM_STATE_MAXIMIZED_HORZ", XCB_ATOM_NONE},
683 {"_NET_WM_STATE_MAXIMIZED_VERT", XCB_ATOM_NONE},
684 {"_NET_WM_STATE_SKIP_PAGER", XCB_ATOM_NONE},
685 {"_NET_WM_STATE_SKIP_TASKBAR", XCB_ATOM_NONE},
686 {"_NET_WM_WINDOW_TYPE", XCB_ATOM_NONE},
687 {"_NET_WM_WINDOW_TYPE_DIALOG", XCB_ATOM_NONE},
688 {"_NET_WM_WINDOW_TYPE_DOCK", XCB_ATOM_NONE},
689 {"_NET_WM_WINDOW_TYPE_NORMAL", XCB_ATOM_NONE},
690 {"_NET_WM_WINDOW_TYPE_SPLASH", XCB_ATOM_NONE},
691 {"_NET_WM_WINDOW_TYPE_TOOLBAR", XCB_ATOM_NONE},
692 {"_NET_WM_WINDOW_TYPE_UTILITY", XCB_ATOM_NONE},
693 {"_SWM_WM_STATE_MANUAL", XCB_ATOM_NONE},
700 XC_BOTTOM_LEFT_CORNER,
701 XC_BOTTOM_RIGHT_CORNER,
709 char *name; /* Name used by Xcursor .*/
710 uint8_t cf_char; /* cursorfont index. */
712 } cursors[XC_MAX] = {
713 {"fleur", XC_fleur, XCB_CURSOR_NONE},
714 {"left_ptr", XC_left_ptr, XCB_CURSOR_NONE},
715 {"bottom_left_corner", XC_bottom_left_corner, XCB_CURSOR_NONE},
716 {"bottom_right_corner", XC_bottom_right_corner, XCB_CURSOR_NONE},
717 {"sizing", XC_sizing, XCB_CURSOR_NONE},
718 {"top_left_corner", XC_top_left_corner, XCB_CURSOR_NONE},
719 {"top_right_corner", XC_top_right_corner, XCB_CURSOR_NONE},
722 #define SWM_SPAWN_OPTIONAL 0x1
726 TAILQ_ENTRY(spawn_prog) entry;
732 TAILQ_HEAD(spawn_list, spawn_prog);
733 struct spawn_list spawns = TAILQ_HEAD_INITIALIZER(spawns);
735 /* user/key callable function IDs */
849 KF_DUMPWINS, /* MUST BE LAST */
857 enum keyfuncid funcid;
860 RB_HEAD(key_tree, key);
862 /* function prototypes */
863 void adjust_font(struct ws_win *);
864 void bar_class_name(char *, size_t, struct swm_region *);
865 void bar_class_title_name(char *, size_t, struct swm_region *);
866 void bar_cleanup(struct swm_region *);
867 void bar_extra_setup(void);
868 void bar_extra_stop(void);
869 int bar_extra_update(void);
870 void bar_fmt(const char *, char *, struct swm_region *, size_t);
871 void bar_fmt_expand(char *, size_t);
873 void bar_print(struct swm_region *, const char *);
874 void bar_print_legacy(struct swm_region *, const char *);
875 void bar_replace(char *, char *, struct swm_region *, size_t);
876 void bar_replace_pad(char *, int *, size_t);
877 char *bar_replace_seq(char *, char *, struct swm_region *, size_t *, size_t);
878 void bar_setup(struct swm_region *);
879 void bar_title_name(char *, size_t, struct swm_region *);
880 void bar_toggle(struct swm_region *, union arg *);
881 void bar_urgent(char *, size_t);
882 void bar_window_float(char *, size_t, struct swm_region *);
883 void bar_window_name(char *, size_t, struct swm_region *);
884 void bar_workspace_name(char *, size_t, struct swm_region *);
885 void buttonpress(xcb_button_press_event_t *);
886 void check_conn(void);
887 void clear_keys(void);
888 void clientmessage(xcb_client_message_event_t *);
889 void client_msg(struct ws_win *, xcb_atom_t, xcb_timestamp_t);
890 int conf_load(const char *, int);
891 void configurenotify(xcb_configure_notify_event_t *);
892 void configurerequest(xcb_configure_request_event_t *);
893 void config_win(struct ws_win *, xcb_configure_request_event_t *);
894 void constrain_window(struct ws_win *, struct swm_geometry *, int *);
895 int count_win(struct workspace *, int);
896 void cursors_cleanup(void);
897 void cursors_load(void);
898 void custom_region(char *);
899 void cyclerg(struct swm_region *, union arg *);
900 void cyclews(struct swm_region *, union arg *);
901 void cycle_layout(struct swm_region *, union arg *);
902 void destroynotify(xcb_destroy_notify_event_t *);
903 void dumpwins(struct swm_region *, union arg *);
905 void enternotify(xcb_enter_notify_event_t *);
906 void event_drain(uint8_t);
907 void event_error(xcb_generic_error_t *);
908 void event_handle(xcb_generic_event_t *);
909 void ewmh_autoquirk(struct ws_win *);
910 void ewmh_get_win_state(struct ws_win *);
911 int ewmh_set_win_fullscreen(struct ws_win *, int);
912 void ewmh_update_actions(struct ws_win *);
913 void ewmh_update_win_state(struct ws_win *, xcb_atom_t, long);
914 char *expand_tilde(const char *);
915 void expose(xcb_expose_event_t *);
916 void fake_keypress(struct ws_win *, xcb_keysym_t, uint16_t);
917 struct pid_e *find_pid(pid_t);
918 struct ws_win *find_unmanaged_window(xcb_window_t);
919 struct ws_win *find_window(xcb_window_t);
920 void floating_toggle(struct swm_region *, union arg *);
921 int floating_toggle_win(struct ws_win *);
922 void focus(struct swm_region *, union arg *);
924 void focusin(xcb_focus_in_event_t *);
925 void focusout(xcb_focus_out_event_t *);
927 void focus_flush(void);
928 void focus_region(struct swm_region *);
929 void focusrg(struct swm_region *, union arg *);
930 void focus_win(struct ws_win *);
931 void fontset_init(void);
932 void free_window(struct ws_win *);
933 xcb_atom_t get_atom_from_string(const char *);
935 char *get_atom_name(xcb_atom_t);
937 struct ws_win *get_focus_magic(struct ws_win *);
938 struct ws_win *get_focus_prev(struct ws_win *);
940 char *get_notify_detail_label(uint8_t);
941 char *get_notify_mode_label(uint8_t);
943 struct ws_win *get_pointer_win(xcb_window_t);
944 struct ws_win *get_region_focus(struct swm_region *);
945 int get_region_index(struct swm_region *);
946 xcb_screen_t *get_screen(int);
947 xcb_window_t get_sibling(struct ws_win *, int);
948 int get_screen_count(void);
950 char *get_stack_mode_name(uint8_t);
952 int32_t get_swm_iconic(struct ws_win *);
953 char *get_win_name(xcb_window_t);
954 int get_ws_idx(xcb_window_t);
955 uint32_t getstate(xcb_window_t);
956 void grabbuttons(struct ws_win *);
958 void grab_windows(void);
959 void iconify(struct swm_region *, union arg *);
961 void keypress(xcb_key_press_event_t *);
962 int key_cmp(struct key *, struct key *);
963 void key_insert(unsigned int, KeySym, enum keyfuncid, const char *);
964 struct key *key_lookup(unsigned int, KeySym);
965 void key_remove(struct key *);
966 void key_replace(struct key *, unsigned int, KeySym, enum keyfuncid,
968 void kill_bar_extra_atexit(void);
969 void kill_refs(struct ws_win *);
971 void leavenotify(xcb_leave_notify_event_t *);
973 void load_float_geom(struct ws_win *);
974 struct ws_win *manage_window(xcb_window_t, uint16_t);
975 void map_window(struct ws_win *, xcb_window_t);
976 void mapnotify(xcb_map_notify_event_t *);
977 void mappingnotify(xcb_mapping_notify_event_t *);
978 void maprequest(xcb_map_request_event_t *);
979 void motionnotify(xcb_motion_notify_event_t *);
980 void move(struct ws_win *, union arg *);
981 void move_step(struct swm_region *, union arg *);
982 uint32_t name_to_pixel(int, const char *);
983 void name_workspace(struct swm_region *, union arg *);
984 void new_region(struct swm_screen *, int, int, int, int);
985 int parsekeys(char *, unsigned int, unsigned int *, KeySym *);
986 int parsequirks(char *, unsigned long *);
987 int parse_rgb(const char *, uint16_t *, uint16_t *, uint16_t *);
988 void pressbutton(struct swm_region *, union arg *);
989 void priorws(struct swm_region *, union arg *);
991 void print_win_geom(xcb_window_t);
993 void propertynotify(xcb_property_notify_event_t *);
994 void quirk_insert(const char *, const char *, unsigned long);
995 void quirk_remove(struct quirk *);
996 void quirk_replace(struct quirk *, const char *, const char *,
998 void quit(struct swm_region *, union arg *);
999 void raise_toggle(struct swm_region *, union arg *);
1000 void region_containment(struct ws_win *, struct swm_region *, int);
1001 struct swm_region *region_under(struct swm_screen *, int, int);
1002 void regionize(struct ws_win *, int, int);
1003 void resize(struct ws_win *, union arg *);
1004 void resize_step(struct swm_region *, union arg *);
1005 void restart(struct swm_region *, union arg *);
1006 struct swm_region *root_to_region(xcb_window_t, int);
1007 void screenchange(xcb_randr_screen_change_notify_event_t *);
1008 void scan_xrandr(int);
1009 void search_do_resp(void);
1010 void search_resp_name_workspace(const char *, size_t);
1011 void search_resp_search_window(const char *);
1012 void search_resp_search_workspace(const char *);
1013 void search_resp_uniconify(const char *, size_t);
1014 void search_win(struct swm_region *, union arg *);
1015 void search_win_cleanup(void);
1016 void search_workspace(struct swm_region *, union arg *);
1017 void send_to_rg(struct swm_region *, union arg *);
1018 void send_to_ws(struct swm_region *, union arg *);
1019 void set_region(struct swm_region *);
1020 int setautorun(char *, char *, int);
1021 int setconfbinding(char *, char *, int);
1022 int setconfcolor(char *, char *, int);
1023 int setconfmodkey(char *, char *, int);
1024 int setconfquirk(char *, char *, int);
1025 int setconfregion(char *, char *, int);
1026 int setconfspawn(char *, char *, int);
1027 int setconfvalue(char *, char *, int);
1028 void setkeybinding(unsigned int, KeySym, enum keyfuncid, const char *);
1029 int setkeymapping(char *, char *, int);
1030 int setlayout(char *, char *, int);
1031 void setquirk(const char *, const char *, unsigned long);
1032 void setscreencolor(char *, int, int);
1033 void setspawn(const char *, const char *, int);
1034 void setup_ewmh(void);
1035 void setup_globals(void);
1036 void setup_keys(void);
1037 void setup_quirks(void);
1038 void setup_screens(void);
1039 void setup_spawn(void);
1040 void set_child_transient(struct ws_win *, xcb_window_t *);
1041 void set_swm_iconic(struct ws_win *, int);
1042 void set_win_state(struct ws_win *, uint16_t);
1043 void shutdown_cleanup(void);
1045 void socket_setnonblock(int);
1046 void sort_windows(struct ws_win_list *);
1047 void spawn(int, union arg *, int);
1048 void spawn_custom(struct swm_region *, union arg *, const char *);
1049 int spawn_expand(struct swm_region *, union arg *, const char *, char ***);
1050 void spawn_insert(const char *, const char *, int);
1051 struct spawn_prog *spawn_find(const char *);
1052 void spawn_remove(struct spawn_prog *);
1053 void spawn_replace(struct spawn_prog *, const char *, const char *, int);
1054 void spawn_select(struct swm_region *, union arg *, const char *, int *);
1055 void stack_config(struct swm_region *, union arg *);
1056 void stack_floater(struct ws_win *, struct swm_region *);
1057 void stack_master(struct workspace *, struct swm_geometry *, int, int);
1058 void store_float_geom(struct ws_win *);
1059 char *strdupsafe(const char *);
1060 void swapwin(struct swm_region *, union arg *);
1061 void switchws(struct swm_region *, union arg *);
1062 void teardown_ewmh(void);
1063 void unfocus_win(struct ws_win *);
1064 void uniconify(struct swm_region *, union arg *);
1065 void unmanage_window(struct ws_win *);
1066 void unmapnotify(xcb_unmap_notify_event_t *);
1067 void unmap_all(void);
1068 void unmap_window(struct ws_win *);
1069 void updatenumlockmask(void);
1070 void update_modkey(unsigned int);
1071 void update_window(struct ws_win *);
1072 void validate_spawns(void);
1073 int validate_win(struct ws_win *);
1074 int validate_ws(struct workspace *);
1075 /*void visibilitynotify(xcb_visibility_notify_event_t *);*/
1076 void version(struct swm_region *, union arg *);
1077 void win_to_ws(struct ws_win *, int, int);
1078 pid_t window_get_pid(xcb_window_t);
1079 void wkill(struct swm_region *, union arg *);
1080 void workaround(void);
1081 void xft_init(struct swm_region *);
1082 void _add_startup_exception(const char *, va_list);
1083 void add_startup_exception(const char *, ...);
1085 RB_PROTOTYPE(key_tree, key, entry, key_cmp);
1086 RB_GENERATE(key_tree, key, entry, key_cmp);
1087 struct key_tree keys;
1092 xcb_font_t cf = XCB_NONE;
1095 for (i = 0; i < LENGTH(cursors); ++i) {
1096 /* try to load Xcursor first. */
1097 cursors[i].cid = XcursorLibraryLoadCursor(display,
1100 /* fallback to cursorfont. */
1101 if (cursors[i].cid == XCB_CURSOR_NONE) {
1102 if (cf == XCB_NONE) {
1103 cf = xcb_generate_id(conn);
1104 xcb_open_font(conn, cf, strlen("cursor"),
1108 cursors[i].cid = xcb_generate_id(conn);
1109 xcb_create_glyph_cursor(conn, cursors[i].cid, cf, cf,
1110 cursors[i].cf_char, cursors[i].cf_char + 1, 0, 0, 0,
1111 0xffff, 0xffff, 0xffff);
1117 xcb_close_font(conn, cf);
1121 cursors_cleanup(void)
1124 for (i = 0; i < LENGTH(cursors); ++i)
1125 xcb_free_cursor(conn, cursors[i].cid);
1129 expand_tilde(const char *s)
1131 struct passwd *ppwd;
1139 errx(1, "expand_tilde: NULL string.");
1142 result = strdup(sc);
1148 if ((max = sysconf(_SC_LOGIN_NAME_MAX)) == -1)
1149 errx(1, "expand_tilde: sysconf");
1151 if ((user = calloc(1, max + 1)) == NULL)
1152 errx(1, "expand_tilde: calloc");
1154 for (i = 0; s[i] != '/' && s[i] != '\0'; ++i)
1159 ppwd = strlen(user) == 0 ? getpwuid(getuid()) : getpwnam(user);
1163 result = strdup(sc);
1165 if (asprintf(&result, "%s%s", ppwd->pw_dir, s) == -1)
1169 errx(1, "expand_tilde: failed to allocate memory.");
1175 parse_rgb(const char *rgb, uint16_t *rr, uint16_t *gg, uint16_t *bb)
1177 unsigned int tmpr, tmpg, tmpb;
1179 if (sscanf(rgb, "rgb:%x/%x/%x", &tmpr, &tmpg, &tmpb) != 3)
1182 *rr = RGB_8_TO_16(tmpr);
1183 *gg = RGB_8_TO_16(tmpg);
1184 *bb = RGB_8_TO_16(tmpb);
1190 get_screen(int screen)
1192 const xcb_setup_t *r;
1193 xcb_screen_iterator_t iter;
1195 if ((r = xcb_get_setup(conn)) == NULL) {
1196 DNPRINTF(SWM_D_MISC, "get_screen: xcb_get_setup\n");
1200 iter = xcb_setup_roots_iterator(r);
1201 for (; iter.rem; --screen, xcb_screen_next(&iter))
1209 get_screen_count(void)
1211 const xcb_setup_t *r;
1213 if ((r = xcb_get_setup(conn)) == NULL) {
1214 DNPRINTF(SWM_D_MISC, "get_screen_count: xcb_get_setup\n");
1218 return xcb_setup_roots_length(r);
1222 get_region_index(struct swm_region *r)
1224 struct swm_region *rr;
1230 TAILQ_FOREACH(rr, &r->s->rl, entry) {
1245 if (focus_mode == SWM_FOCUS_DEFAULT)
1246 event_drain(XCB_ENTER_NOTIFY);
1252 get_atom_from_string(const char *str)
1254 xcb_intern_atom_cookie_t c;
1255 xcb_intern_atom_reply_t *r;
1258 c = xcb_intern_atom(conn, 0, strlen(str), str);
1259 r = xcb_intern_atom_reply(conn, c, NULL);
1267 return (XCB_ATOM_NONE);
1271 set_swm_iconic(struct ws_win *win, int newv)
1278 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
1279 a_swm_iconic, XCB_ATOM_INTEGER, 32, 1, &v);
1281 xcb_delete_property(conn, win->id, a_swm_iconic);
1285 get_swm_iconic(struct ws_win *win)
1288 xcb_get_property_reply_t *pr = NULL;
1290 pr = xcb_get_property_reply(conn,
1291 xcb_get_property(conn, 0, win->id, a_swm_iconic,
1292 XCB_ATOM_INTEGER, 0, 1), NULL);
1295 if (pr->type != XCB_ATOM_INTEGER || pr->format != 32)
1297 v = *((int32_t *)xcb_get_property_value(pr));
1307 xcb_atom_t sup_list;
1308 int i, j, num_screens;
1310 sup_list = get_atom_from_string("_NET_SUPPORTED");
1312 for (i = 0; i < LENGTH(ewmh); i++)
1313 ewmh[i].atom = get_atom_from_string(ewmh[i].name);
1315 num_screens = get_screen_count();
1316 for (i = 0; i < num_screens; i++) {
1317 /* Support check window will be created by workaround(). */
1319 /* Report supported atoms */
1320 xcb_delete_property(conn, screens[i].root, sup_list);
1321 for (j = 0; j < LENGTH(ewmh); j++)
1322 xcb_change_property(conn, XCB_PROP_MODE_APPEND,
1323 screens[i].root, sup_list, XCB_ATOM_ATOM, 32, 1,
1332 xcb_atom_t sup_check, sup_list;
1334 xcb_get_property_cookie_t pc;
1335 xcb_get_property_reply_t *pr;
1337 sup_check = get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
1338 sup_list = get_atom_from_string("_NET_SUPPORTED");
1339 num_screens = get_screen_count();
1341 for (i = 0; i < num_screens; i++) {
1342 /* Get the support check window and destroy it */
1343 pc = xcb_get_property(conn, 0, screens[i].root, sup_check,
1344 XCB_ATOM_WINDOW, 0, 1);
1345 pr = xcb_get_property_reply(conn, pc, NULL);
1348 if (pr->format == sup_check) {
1349 id = *((xcb_window_t *)xcb_get_property_value(pr));
1351 xcb_destroy_window(conn, id);
1352 xcb_delete_property(conn, screens[i].root, sup_check);
1353 xcb_delete_property(conn, screens[i].root, sup_list);
1360 ewmh_autoquirk(struct ws_win *win)
1364 xcb_get_property_cookie_t c;
1365 xcb_get_property_reply_t *r;
1367 c = xcb_get_property(conn, 0, win->id,
1368 ewmh[_NET_WM_WINDOW_TYPE].atom, XCB_ATOM_ATOM, 0, UINT32_MAX);
1369 r = xcb_get_property_reply(conn, c, NULL);
1373 n = xcb_get_property_value_length(r);
1374 type = xcb_get_property_value(r);
1376 for (i = 0; i < n; i++) {
1377 if (type[i] == ewmh[_NET_WM_WINDOW_TYPE_NORMAL].atom)
1379 if (type[i] == ewmh[_NET_WM_WINDOW_TYPE_DOCK].atom ||
1380 type[i] == ewmh[_NET_WM_WINDOW_TYPE_TOOLBAR].atom ||
1381 type[i] == ewmh[_NET_WM_WINDOW_TYPE_UTILITY].atom) {
1383 win->quirks = SWM_Q_FLOAT | SWM_Q_ANYWHERE;
1386 if (type[i] == ewmh[_NET_WM_WINDOW_TYPE_SPLASH].atom ||
1387 type[i] == ewmh[_NET_WM_WINDOW_TYPE_DIALOG].atom) {
1389 win->quirks = SWM_Q_FLOAT;
1396 #define SWM_EWMH_ACTION_COUNT_MAX (6)
1397 #define EWMH_F_FULLSCREEN (1<<0)
1398 #define EWMH_F_ABOVE (1<<1)
1399 #define EWMH_F_HIDDEN (1<<2)
1400 #define EWMH_F_SKIP_PAGER (1<<3)
1401 #define EWMH_F_SKIP_TASKBAR (1<<4)
1402 #define SWM_F_MANUAL (1<<5)
1405 ewmh_set_win_fullscreen(struct ws_win *win, int fs)
1413 DNPRINTF(SWM_D_MISC, "ewmh_set_win_fullscreen: window: 0x%x, "
1414 "fullscreen %s\n", win->id, YESNO(fs));
1417 if (!win->g_floatvalid)
1418 store_float_geom(win);
1420 win->g = win->ws->r->g;
1423 load_float_geom(win);
1430 ewmh_update_actions(struct ws_win *win)
1432 xcb_atom_t actions[SWM_EWMH_ACTION_COUNT_MAX];
1438 actions[n++] = ewmh[_NET_WM_ACTION_CLOSE].atom;
1440 if (win->floating) {
1441 actions[n++] = ewmh[_NET_WM_ACTION_MOVE].atom;
1442 actions[n++] = ewmh[_NET_WM_ACTION_RESIZE].atom;
1443 actions[n++] = ewmh[_NET_WM_ACTION_ABOVE].atom;
1446 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
1447 ewmh[_NET_WM_ALLOWED_ACTIONS].atom, XCB_ATOM_ATOM, 32, 1, actions);
1450 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
1451 #define _NET_WM_STATE_ADD 1 /* add/set property */
1452 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
1455 ewmh_update_win_state(struct ws_win *win, xcb_atom_t state, long action)
1457 unsigned int mask = 0;
1458 unsigned int changed = 0;
1459 unsigned int orig_flags;
1464 DNPRINTF(SWM_D_PROP, "ewmh_update_win_state: window: 0x%x, state: %ld, "
1465 "action: %ld\n", win->id, (unsigned long)state, action);
1467 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
1468 mask = EWMH_F_FULLSCREEN;
1469 else if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
1470 mask = EWMH_F_ABOVE;
1471 else if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
1472 mask = SWM_F_MANUAL;
1473 else if (state == ewmh[_NET_WM_STATE_SKIP_PAGER].atom)
1474 mask = EWMH_F_SKIP_PAGER;
1475 else if (state == ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom)
1476 mask = EWMH_F_SKIP_TASKBAR;
1478 orig_flags = win->ewmh_flags;
1481 case _NET_WM_STATE_REMOVE:
1482 win->ewmh_flags &= ~mask;
1484 case _NET_WM_STATE_ADD:
1485 win->ewmh_flags |= mask;
1487 case _NET_WM_STATE_TOGGLE:
1488 win->ewmh_flags ^= mask;
1492 changed = (win->ewmh_flags & mask) ^ (orig_flags & mask) ? 1 : 0;
1494 if (state == ewmh[_NET_WM_STATE_ABOVE].atom) {
1495 if (changed && !floating_toggle_win(win))
1496 win->ewmh_flags = orig_flags; /* revert */
1497 } else if (state == ewmh[_SWM_WM_STATE_MANUAL].atom) {
1499 win->manual = (win->ewmh_flags & SWM_F_MANUAL) != 0;
1500 } else if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom) {
1501 if (changed && !ewmh_set_win_fullscreen(win,
1502 win->ewmh_flags & EWMH_F_FULLSCREEN))
1503 win->ewmh_flags = orig_flags; /* revert */
1506 xcb_delete_property(conn, win->id, ewmh[_NET_WM_STATE].atom);
1508 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
1509 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1510 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1511 &ewmh[_NET_WM_STATE_FULLSCREEN].atom);
1512 else if (win->ewmh_flags & EWMH_F_SKIP_PAGER)
1513 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1514 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1515 &ewmh[_NET_WM_STATE_SKIP_PAGER].atom);
1516 else if (win->ewmh_flags & EWMH_F_SKIP_TASKBAR)
1517 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1518 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1519 &ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom);
1520 else if (win->ewmh_flags & EWMH_F_ABOVE)
1521 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1522 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1523 &ewmh[_NET_WM_STATE_ABOVE].atom);
1524 else if (win->ewmh_flags & SWM_F_MANUAL)
1525 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1526 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1527 &ewmh[_SWM_WM_STATE_MANUAL].atom);
1531 ewmh_get_win_state(struct ws_win *win)
1534 xcb_get_property_cookie_t c;
1535 xcb_get_property_reply_t *r;
1541 win->ewmh_flags = 0;
1543 win->ewmh_flags |= EWMH_F_ABOVE;
1545 win->ewmh_flags |= SWM_F_MANUAL;
1547 c = xcb_get_property(conn, 0, win->id, ewmh[_NET_WM_STATE].atom,
1548 XCB_ATOM_ATOM, 0, UINT32_MAX);
1549 r = xcb_get_property_reply(conn, c, NULL);
1553 states = xcb_get_property_value(r);
1554 n = xcb_get_property_value_length(r);
1556 for (i = 0; i < n; i++)
1557 ewmh_update_win_state(win, states[i], _NET_WM_STATE_ADD);
1565 dumpwins(struct swm_region *r, union arg *args)
1569 xcb_get_window_attributes_cookie_t c;
1570 xcb_get_window_attributes_reply_t *wa;
1572 /* suppress unused warning since var is needed */
1575 if (r->ws == NULL) {
1576 warnx("dumpwins: invalid workspace");
1580 warnx("=== managed window list ws %02d ===", r->ws->idx);
1581 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
1582 state = getstate(win->id);
1583 c = xcb_get_window_attributes(conn, win->id);
1584 wa = xcb_get_window_attributes_reply(conn, c, NULL);
1586 warnx("window: 0x%x, map_state: %d, state: %u, "
1587 "transient: 0x%x", win->id, wa->map_state,
1588 state, win->transient);
1591 warnx("window: 0x%x, failed xcb_get_window_attributes",
1595 warnx("===== unmanaged window list =====");
1596 TAILQ_FOREACH(win, &r->ws->unmanagedlist, entry) {
1597 state = getstate(win->id);
1598 c = xcb_get_window_attributes(conn, win->id);
1599 wa = xcb_get_window_attributes_reply(conn, c, NULL);
1601 warnx("window: 0x%x, map_state: %d, state: %u, "
1602 "transient: 0x%x", win->id, wa->map_state,
1603 state, win->transient);
1606 warnx("window: 0x%x, failed xcb_get_window_attributes",
1610 warnx("=================================");
1614 dumpwins(struct swm_region *r, union arg *s)
1619 #endif /* SWM_DEBUG */
1624 int saved_errno, status;
1627 saved_errno = errno;
1631 while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) != 0) {
1636 if (errno != ECHILD)
1637 warn("sighdlr: waitpid");
1638 #endif /* SWM_DEBUG */
1641 if (pid == searchpid)
1645 if (WIFEXITED(status)) {
1646 if (WEXITSTATUS(status) != 0)
1647 warnx("sighdlr: child exit status: %d",
1648 WEXITSTATUS(status));
1650 warnx("sighdlr: child is terminated "
1652 #endif /* SWM_DEBUG */
1666 errno = saved_errno;
1672 struct pid_e *p = NULL;
1674 DNPRINTF(SWM_D_MISC, "find_pid: %d\n", pid);
1679 TAILQ_FOREACH(p, &pidlist, entry) {
1688 name_to_pixel(int sidx, const char *colorname)
1690 uint32_t result = 0;
1691 char cname[32] = "#";
1692 xcb_screen_t *screen;
1693 xcb_colormap_t cmap;
1694 xcb_alloc_color_reply_t *cr;
1695 xcb_alloc_named_color_reply_t *nr;
1696 uint16_t rr, gg, bb;
1698 screen = get_screen(sidx);
1699 cmap = screen->default_colormap;
1701 /* color is in format rgb://rr/gg/bb */
1702 if (strncmp(colorname, "rgb:", 4) == 0) {
1703 if (parse_rgb(colorname, &rr, &gg, &bb) == -1)
1704 warnx("could not parse rgb %s", colorname);
1706 cr = xcb_alloc_color_reply(conn,
1707 xcb_alloc_color(conn, cmap, rr, gg, bb),
1713 warnx("color '%s' not found", colorname);
1716 nr = xcb_alloc_named_color_reply(conn,
1717 xcb_alloc_named_color(conn, cmap, strlen(colorname),
1720 strlcat(cname, colorname + 2, sizeof cname - 1);
1721 nr = xcb_alloc_named_color_reply(conn,
1722 xcb_alloc_named_color(conn, cmap, strlen(cname),
1729 warnx("color '%s' not found", colorname);
1736 setscreencolor(char *val, int i, int c)
1740 num_screens = get_screen_count();
1741 if (i > 0 && i <= num_screens) {
1742 screens[i - 1].c[c].pixel = name_to_pixel(i - 1, val);
1743 free(screens[i - 1].c[c].name);
1744 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
1746 } else if (i == -1) {
1747 for (i = 0; i < num_screens; i++) {
1748 screens[i].c[c].pixel = name_to_pixel(0, val);
1749 free(screens[i].c[c].name);
1750 if ((screens[i].c[c].name = strdup(val)) == NULL)
1754 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1759 fancy_stacker(struct workspace *ws)
1761 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1762 if (ws->cur_layout->l_stack == vertical_stack)
1763 snprintf(ws->stacker, sizeof ws->stacker,
1764 ws->l_state.vertical_flip ? "[%d>%d]" : "[%d|%d]",
1765 ws->l_state.vertical_mwin, ws->l_state.vertical_stacks);
1766 if (ws->cur_layout->l_stack == horizontal_stack)
1767 snprintf(ws->stacker, sizeof ws->stacker,
1768 ws->l_state.horizontal_flip ? "[%dv%d]" : "[%d-%d]",
1769 ws->l_state.horizontal_mwin, ws->l_state.horizontal_stacks);
1773 plain_stacker(struct workspace *ws)
1775 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1776 if (ws->cur_layout->l_stack == vertical_stack)
1777 strlcpy(ws->stacker, ws->l_state.vertical_flip ? "[>]" : "[|]",
1778 sizeof ws->stacker);
1779 if (ws->cur_layout->l_stack == horizontal_stack)
1780 strlcpy(ws->stacker, ws->l_state.horizontal_flip ? "[v]" : "[-]",
1781 sizeof ws->stacker);
1785 custom_region(char *val)
1787 unsigned int x, y, w, h;
1788 int sidx, num_screens;
1789 xcb_screen_t *screen;
1791 num_screens = get_screen_count();
1792 if (sscanf(val, "screen[%d]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
1793 errx(1, "invalid custom region, "
1794 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1795 if (sidx < 1 || sidx > num_screens)
1796 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1800 if ((screen = get_screen(sidx)) == NULL)
1801 errx(1, "ERROR: can't get screen %d.", sidx);
1804 errx(1, "region %ux%u+%u+%u too small", w, h, x, y);
1806 if (x > screen->width_in_pixels ||
1807 y > screen->height_in_pixels ||
1808 w + x > screen->width_in_pixels ||
1809 h + y > screen->height_in_pixels) {
1810 warnx("ignoring region %ux%u+%u+%u - not within screen "
1811 "boundaries (%ux%u)", w, h, x, y,
1812 screen->width_in_pixels, screen->height_in_pixels);
1816 new_region(&screens[sidx], x, y, w, h);
1820 socket_setnonblock(int fd)
1824 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
1825 err(1, "fcntl F_GETFL");
1826 flags |= O_NONBLOCK;
1827 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
1828 err(1, "fcntl F_SETFL");
1832 bar_print_legacy(struct swm_region *r, const char *s)
1834 xcb_rectangle_t rect;
1839 XRectangle ibox, lbox;
1843 XmbTextExtents(bar_fs, s, len, &ibox, &lbox);
1845 switch (bar_justify) {
1846 case SWM_BAR_JUSTIFY_LEFT:
1849 case SWM_BAR_JUSTIFY_CENTER:
1850 x = (WIDTH(r) - lbox.width) / 2;
1852 case SWM_BAR_JUSTIFY_RIGHT:
1853 x = WIDTH(r) - lbox.width - SWM_BAR_OFFSET;
1857 if (x < SWM_BAR_OFFSET)
1862 rect.width = WIDTH(r->bar);
1863 rect.height = HEIGHT(r->bar);
1865 /* clear back buffer */
1866 gcv[0] = r->s->c[SWM_S_COLOR_BAR].pixel;
1867 xcb_change_gc(conn, r->s->bar_gc, XCB_GC_FOREGROUND, gcv);
1868 xcb_poly_fill_rectangle(conn, r->bar->buffer, r->s->bar_gc, 1, &rect);
1870 /* draw back buffer */
1871 gcvd.graphics_exposures = 0;
1872 draw = XCreateGC(display, r->bar->buffer, GCGraphicsExposures, &gcvd);
1873 XSetForeground(display, draw, r->s->c[SWM_S_COLOR_BAR_FONT].pixel);
1874 DRAWSTRING(display, r->bar->buffer, bar_fs, draw,
1875 x, (bar_fs_extents->max_logical_extent.height - lbox.height) / 2 -
1877 XFreeGC(display, draw);
1880 xcb_copy_area(conn, r->bar->buffer, r->bar->id, r->s->bar_gc, 0, 0,
1881 0, 0, WIDTH(r->bar), HEIGHT(r->bar));
1885 bar_print(struct swm_region *r, const char *s)
1888 xcb_rectangle_t rect;
1896 XftTextExtentsUtf8(display, bar_font, (FcChar8 *)s, len, &info);
1898 switch (bar_justify) {
1899 case SWM_BAR_JUSTIFY_LEFT:
1902 case SWM_BAR_JUSTIFY_CENTER:
1903 x = (WIDTH(r) - info.width) / 2;
1905 case SWM_BAR_JUSTIFY_RIGHT:
1906 x = WIDTH(r) - info.width - SWM_BAR_OFFSET;
1910 if (x < SWM_BAR_OFFSET)
1915 rect.width = WIDTH(r->bar);
1916 rect.height = HEIGHT(r->bar);
1918 /* clear back buffer */
1919 gcv[0] = r->s->c[SWM_S_COLOR_BAR].pixel;
1920 xcb_change_gc(conn, r->s->bar_gc, XCB_GC_FOREGROUND, gcv);
1921 xcb_poly_fill_rectangle(conn, r->bar->buffer, r->s->bar_gc, 1, &rect);
1923 /* draw back buffer */
1924 draw = XftDrawCreate(display, r->bar->buffer,
1925 DefaultVisual(display, r->s->idx),
1926 DefaultColormap(display, r->s->idx));
1928 XftDrawStringUtf8(draw, &bar_font_color, bar_font, x,
1929 (HEIGHT(r->bar) + bar_font->height) / 2 - bar_font->descent,
1932 XftDrawDestroy(draw);
1935 xcb_copy_area(conn, r->bar->buffer, r->bar->id, r->s->bar_gc, 0, 0,
1936 0, 0, WIDTH(r->bar), HEIGHT(r->bar));
1940 bar_extra_stop(void)
1944 bzero(bar_pipe, sizeof bar_pipe);
1947 kill(bar_pid, SIGTERM);
1950 strlcpy(bar_ext, "", sizeof bar_ext);
1955 bar_class_name(char *s, size_t sz, struct swm_region *r)
1957 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1959 if (r->ws->focus->ch.class_name != NULL)
1960 strlcat(s, r->ws->focus->ch.class_name, sz);
1964 bar_title_name(char *s, size_t sz, struct swm_region *r)
1966 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1968 if (r->ws->focus->ch.instance_name != NULL)
1969 strlcat(s, r->ws->focus->ch.instance_name, sz);
1973 bar_class_title_name(char *s, size_t sz, struct swm_region *r)
1975 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1978 bar_class_name(s, sz, r);
1979 strlcat(s, ":", sz);
1980 bar_title_name(s, sz, r);
1984 bar_window_float(char *s, size_t sz, struct swm_region *r)
1986 if (r == NULL || r ->ws == NULL || r->ws->focus == NULL)
1988 if (r->ws->focus->floating)
1989 strlcat(s, "(f)", sz);
1993 bar_window_name(char *s, size_t sz, struct swm_region *r)
1997 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1999 if ((title = get_win_name(r->ws->focus->id)) == NULL)
2002 strlcat(s, title, sz);
2007 bar_urgent(char *s, size_t sz)
2010 int i, j, num_screens;
2011 int urgent[SWM_WS_MAX];
2013 xcb_get_property_cookie_t c;
2014 xcb_icccm_wm_hints_t hints;
2016 for (i = 0; i < workspace_limit; i++)
2019 num_screens = get_screen_count();
2020 for (i = 0; i < num_screens; i++)
2021 for (j = 0; j < workspace_limit; j++)
2022 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry) {
2023 c = xcb_icccm_get_wm_hints(conn, win->id);
2024 if (xcb_icccm_get_wm_hints_reply(conn, c,
2027 if (hints.flags & XCB_ICCCM_WM_HINT_X_URGENCY)
2031 for (i = 0; i < workspace_limit; i++) {
2033 snprintf(b, sizeof b, "%d ", i + 1);
2035 snprintf(b, sizeof b, "- ");
2041 bar_workspace_name(char *s, size_t sz, struct swm_region *r)
2043 if (r == NULL || r->ws == NULL)
2045 if (r->ws->name != NULL)
2046 strlcat(s, r->ws->name, sz);
2049 /* build the default bar format according to the defined enabled options */
2051 bar_fmt(const char *fmtexp, char *fmtnew, struct swm_region *r, size_t sz)
2053 /* if format provided, just copy the buffers */
2054 if (bar_format != NULL) {
2055 strlcpy(fmtnew, fmtexp, sz);
2059 /* reset the output buffer */
2062 strlcat(fmtnew, "+N:+I ", sz);
2064 strlcat(fmtnew, "+S", sz);
2065 strlcat(fmtnew, " ", sz);
2067 /* only show the workspace name if there's actually one */
2068 if (r != NULL && r->ws != NULL && r->ws->name != NULL)
2069 strlcat(fmtnew, "<+D>", sz);
2070 strlcat(fmtnew, "+3<", sz);
2072 if (clock_enabled) {
2073 strlcat(fmtnew, fmtexp, sz);
2074 strlcat(fmtnew, "+4<", sz);
2077 /* bar_urgent already adds the space before the last asterisk */
2079 strlcat(fmtnew, "* +U*+4<", sz);
2081 if (title_class_enabled) {
2082 strlcat(fmtnew, "+C", sz);
2083 if (!title_name_enabled)
2084 strlcat(fmtnew, "+4<", sz);
2087 /* checks needed by the colon and floating strlcat(3) calls below */
2088 if (r != NULL && r->ws != NULL && r->ws->focus != NULL) {
2089 if (title_name_enabled) {
2090 if (title_class_enabled)
2091 strlcat(fmtnew, ":", sz);
2092 strlcat(fmtnew, "+T+4<", sz);
2094 if (window_name_enabled) {
2095 if (r->ws->focus->floating)
2096 strlcat(fmtnew, "+F ", sz);
2097 strlcat(fmtnew, "+64W ", sz);
2101 /* finally add the action script output and the version */
2102 strlcat(fmtnew, "+4<+A+4<+V", sz);
2106 bar_replace_pad(char *tmp, int *limit, size_t sz)
2108 /* special case; no limit given, pad one space, instead */
2109 if (*limit == (int)sz - 1)
2111 snprintf(tmp, sz, "%*s", *limit, " ");
2114 /* replaces the bar format character sequences (like in tmux(1)) */
2116 bar_replace_seq(char *fmt, char *fmtrep, struct swm_region *r, size_t *offrep,
2120 char tmp[SWM_BAR_MAX];
2124 /* reset strlcat(3) buffer */
2127 /* get number, if any */
2130 if (sscanf(fmt, "%d%n", &limit, &size) != 1)
2131 limit = sizeof tmp - 1;
2132 if (limit <= 0 || limit >= (int)sizeof tmp)
2133 limit = sizeof tmp - 1;
2135 /* there is nothing to replace (ie EOL) */
2142 bar_replace_pad(tmp, &limit, sizeof tmp);
2145 snprintf(tmp, sizeof tmp, "%s", bar_ext);
2148 bar_class_name(tmp, sizeof tmp, r);
2151 bar_workspace_name(tmp, sizeof tmp, r);
2154 bar_window_float(tmp, sizeof tmp, r);
2157 snprintf(tmp, sizeof tmp, "%d", r->ws->idx + 1);
2160 snprintf(tmp, sizeof tmp, "%d", r->s->idx + 1);
2163 bar_class_title_name(tmp, sizeof tmp, r);
2166 snprintf(tmp, sizeof tmp, "%s", r->ws->stacker);
2169 bar_title_name(tmp, sizeof tmp, r);
2172 bar_urgent(tmp, sizeof tmp);
2175 snprintf(tmp, sizeof tmp, "%s", bar_vertext);
2178 bar_window_name(tmp, sizeof tmp, r);
2181 /* unknown character sequence; copy as-is */
2182 snprintf(tmp, sizeof tmp, "+%c", *fmt);
2188 if ((int)len < limit)
2190 while (limit-- > 0) {
2191 if (*offrep >= sz - 1)
2193 fmtrep[(*offrep)++] = *ptr++;
2201 bar_replace(char *fmt, char *fmtrep, struct swm_region *r, size_t sz)
2206 while (*fmt != '\0') {
2208 /* skip ordinary characters */
2211 fmtrep[off++] = *fmt++;
2215 /* character sequence found; replace it */
2216 fmt = bar_replace_seq(fmt, fmtrep, r, &off, sz);
2225 bar_fmt_expand(char *fmtexp, size_t sz)
2232 /* start by grabbing the current time and date */
2234 localtime_r(&tmt, &tm);
2236 /* figure out what to expand */
2237 if (bar_format != NULL)
2239 else if (bar_format == NULL && clock_enabled)
2241 /* if nothing to expand bail out */
2247 /* copy as-is, just in case the format shouldn't be expanded below */
2248 strlcpy(fmtexp, fmt, sz);
2249 /* finally pass the string through strftime(3) */
2250 #ifndef SWM_DENY_CLOCK_FORMAT
2251 if ((len = strftime(fmtexp, sz, fmt, &tm)) == 0)
2252 warnx("format too long");
2257 /* Redraws the bar; need to follow with xcb_flush() or focus_flush(). */
2261 char fmtexp[SWM_BAR_MAX], fmtnew[SWM_BAR_MAX];
2262 char fmtrep[SWM_BAR_MAX];
2264 struct swm_region *r;
2266 /* expand the format by first passing it through strftime(3) */
2267 bar_fmt_expand(fmtexp, sizeof fmtexp);
2269 num_screens = get_screen_count();
2270 for (i = 0; i < num_screens; i++) {
2271 TAILQ_FOREACH(r, &screens[i].rl, entry) {
2275 if (bar_enabled && r->ws->bar_enabled)
2276 xcb_map_window(conn, r->bar->id);
2278 xcb_unmap_window(conn, r->bar->id);
2282 if (startup_exception)
2283 snprintf(fmtrep, sizeof fmtrep, "total "
2284 "exceptions: %d, first exception: %s",
2288 bar_fmt(fmtexp, fmtnew, r, sizeof fmtnew);
2289 bar_replace(fmtnew, fmtrep, r, sizeof fmtrep);
2291 if (bar_font_legacy)
2292 bar_print_legacy(r, fmtrep);
2294 bar_print(r, fmtrep);
2300 * Reads external script output; call when stdin is readable.
2301 * Returns 1 if bar_ext was updated; otherwise 0.
2304 bar_extra_update(void)
2307 char b[SWM_BAR_MAX];
2313 while (fgets(b, sizeof(b), stdin) != NULL) {
2316 if (b[len - 1] == '\n') {
2317 /* Remove newline. */
2320 /* "Clear" bar_ext. */
2323 /* Flush buffered output. */
2324 strlcpy(bar_ext, bar_ext_buf, sizeof(bar_ext));
2325 bar_ext_buf[0] = '\0';
2327 /* Append new output to bar. */
2328 strlcat(bar_ext, b, sizeof(bar_ext));
2332 /* Buffer output. */
2333 strlcat(bar_ext_buf, b, sizeof(bar_ext_buf));
2338 if (errno != EAGAIN) {
2339 warn("bar_action failed");
2348 bar_toggle(struct swm_region *r, union arg *args)
2350 struct swm_region *tmpr;
2353 /* suppress unused warnings since vars are needed */
2357 DNPRINTF(SWM_D_BAR, "bar_toggle\n");
2360 case SWM_ARG_ID_BAR_TOGGLE_WS:
2361 /* Only change if master switch is enabled. */
2363 r->ws->bar_enabled = !r->ws->bar_enabled;
2365 bar_enabled = r->ws->bar_enabled = 1;
2367 case SWM_ARG_ID_BAR_TOGGLE:
2368 bar_enabled = !bar_enabled;
2372 /* update bars as necessary */
2373 num_screens = get_screen_count();
2374 for (i = 0; i < num_screens; i++)
2375 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
2377 if (bar_enabled && tmpr->ws->bar_enabled)
2378 xcb_map_window(conn, tmpr->bar->id);
2380 xcb_unmap_window(conn, tmpr->bar->id);
2385 /* must be after stack */
2392 bar_extra_setup(void)
2394 /* do this here because the conf file is in memory */
2395 if (!bar_extra && bar_argv[0]) {
2396 /* launch external status app */
2398 if (pipe(bar_pipe) == -1)
2399 err(1, "pipe error");
2400 socket_setnonblock(bar_pipe[0]);
2401 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
2403 /* Set stdin to read from the pipe. */
2404 if (dup2(bar_pipe[0], STDIN_FILENO) == -1)
2407 /* Set stdout to write to the pipe. */
2408 if (dup2(bar_pipe[1], STDOUT_FILENO) == -1)
2411 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
2412 err(1, "could not disable SIGPIPE");
2413 switch (bar_pid = fork()) {
2415 err(1, "cannot fork");
2419 execvp(bar_argv[0], bar_argv);
2420 err(1, "%s external app failed", bar_argv[0]);
2422 default: /* parent */
2427 atexit(kill_bar_extra_atexit);
2432 kill_bar_extra_atexit(void)
2435 kill(bar_pid, SIGTERM);
2443 while ((s = index(s, '-'))) {
2448 return (count == 14);
2454 char *default_string;
2455 char **missing_charsets;
2456 int num_missing_charsets = 0;
2460 XFreeFontSet(display, bar_fs);
2464 DNPRINTF(SWM_D_INIT, "fontset_init: loading bar_fonts: %s\n", bar_fonts);
2466 bar_fs = XCreateFontSet(display, bar_fonts, &missing_charsets,
2467 &num_missing_charsets, &default_string);
2469 if (num_missing_charsets > 0) {
2470 warnx("Unable to load charset(s):");
2472 for (i = 0; i < num_missing_charsets; ++i)
2473 warnx("%s", missing_charsets[i]);
2475 XFreeStringList(missing_charsets);
2477 if (strcmp(default_string, ""))
2478 warnx("Glyphs from those sets will be replaced "
2479 "by '%s'.", default_string);
2481 warnx("Glyphs from those sets won't be drawn.");
2485 errx(1, "Error creating font set structure.");
2487 bar_fs_extents = XExtentsOfFontSet(bar_fs);
2489 bar_height = bar_fs_extents->max_logical_extent.height +
2490 2 * bar_border_width;
2497 xft_init(struct swm_region *r)
2499 char *font, *d, *search;
2502 if (bar_font == NULL) {
2503 if ((d = strdup(bar_fonts)) == NULL)
2504 errx(1, "insufficient memory.");
2506 while ((font = strsep(&search, ",")) != NULL) {
2510 DNPRINTF(SWM_D_INIT, "xft_init: try font %s\n", font);
2513 bar_font = XftFontOpenXlfd(display, r->s->idx,
2516 bar_font = XftFontOpenName(display, r->s->idx,
2521 warnx("unable to load font %s", font);
2524 DNPRINTF(SWM_D_INIT, "successfully opened "
2532 if (bar_font == NULL)
2533 errx(1, "unable to open a font");
2535 PIXEL_TO_XRENDERCOLOR(r->s->c[SWM_S_COLOR_BAR_FONT].pixel, color);
2537 if (!XftColorAllocValue(display, DefaultVisual(display, r->s->idx),
2538 DefaultColormap(display, r->s->idx), &color, &bar_font_color))
2539 warn("Xft error: unable to allocate color.");
2541 bar_height = bar_font->height + 2 * bar_border_width;
2548 bar_setup(struct swm_region *r)
2550 xcb_screen_t *screen;
2553 DNPRINTF(SWM_D_BAR, "bar_setup: screen %d.\n",
2556 if ((screen = get_screen(r->s->idx)) == NULL)
2557 errx(1, "ERROR: can't get screen %d.", r->s->idx);
2562 if ((r->bar = calloc(1, sizeof(struct swm_bar))) == NULL)
2563 err(1, "bar_setup: calloc: failed to allocate memory.");
2565 if (bar_font_legacy)
2571 Y(r->bar) = bar_at_bottom ? (Y(r) + HEIGHT(r) - bar_height) : Y(r);
2572 WIDTH(r->bar) = WIDTH(r) - 2 * bar_border_width;
2573 HEIGHT(r->bar) = bar_height - 2 * bar_border_width;
2575 /* Assume region is unfocused when we create the bar. */
2576 r->bar->id = xcb_generate_id(conn);
2577 wa[0] = r->s->c[SWM_S_COLOR_BAR].pixel;
2578 wa[1] = r->s->c[SWM_S_COLOR_BAR_BORDER_UNFOCUS].pixel;
2579 wa[2] = XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_POINTER_MOTION |
2580 XCB_EVENT_MASK_POINTER_MOTION_HINT;
2582 xcb_create_window(conn, XCB_COPY_FROM_PARENT, r->bar->id, r->s->root,
2583 X(r->bar), Y(r->bar), WIDTH(r->bar), HEIGHT(r->bar),
2584 bar_border_width, XCB_WINDOW_CLASS_INPUT_OUTPUT,
2585 XCB_COPY_FROM_PARENT, XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL
2586 | XCB_CW_EVENT_MASK, wa);
2588 r->bar->buffer = xcb_generate_id(conn);
2589 xcb_create_pixmap(conn, screen->root_depth, r->bar->buffer, r->bar->id,
2590 WIDTH(r->bar), HEIGHT(r->bar));
2593 xcb_randr_select_input(conn, r->bar->id,
2594 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE);
2597 xcb_map_window(conn, r->bar->id);
2599 DNPRINTF(SWM_D_BAR, "bar_setup: window: 0x%x, (x,y) w x h: (%d,%d) "
2600 "%d x %d\n", WINID(r->bar), X(r->bar), Y(r->bar), WIDTH(r->bar),
2607 bar_cleanup(struct swm_region *r)
2611 xcb_destroy_window(conn, r->bar->id);
2612 xcb_free_pixmap(conn, r->bar->buffer);
2618 set_win_state(struct ws_win *win, uint16_t state)
2620 uint16_t data[2] = { state, XCB_ATOM_NONE };
2622 DNPRINTF(SWM_D_EVENT, "set_win_state: window: 0x%x, state: %u\n",
2628 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id, a_state,
2629 a_state, 32, 2, data);
2633 getstate(xcb_window_t w)
2635 uint32_t result = 0;
2636 xcb_get_property_cookie_t c;
2637 xcb_get_property_reply_t *r;
2639 c = xcb_get_property(conn, 0, w, a_state, a_state, 0L, 2L);
2640 r = xcb_get_property_reply(conn, c, NULL);
2643 if (r->type == a_state && r->format == 32 && r->length == 2)
2644 result = *((uint32_t *)xcb_get_property_value(r));
2648 DNPRINTF(SWM_D_MISC, "getstate property: win 0x%x state %u\n", w,
2654 version(struct swm_region *r, union arg *args)
2656 /* suppress unused warnings since vars are needed */
2660 bar_version = !bar_version;
2662 snprintf(bar_vertext, sizeof bar_vertext,
2663 "Version: %s Build: %s", SPECTRWM_VERSION, buildstr);
2665 strlcpy(bar_vertext, "", sizeof bar_vertext);
2672 client_msg(struct ws_win *win, xcb_atom_t a, xcb_timestamp_t t)
2674 xcb_client_message_event_t ev;
2682 name = get_atom_name(a);
2683 DNPRINTF(SWM_D_EVENT, "client_msg: window: 0x%x, atom: %s(%u), "
2685 win->id, name, a, t);
2689 bzero(&ev, sizeof ev);
2690 ev.response_type = XCB_CLIENT_MESSAGE;
2691 ev.window = win->id;
2694 ev.data.data32[0] = a;
2695 ev.data.data32[1] = t;
2697 xcb_send_event(conn, 0, win->id,
2698 XCB_EVENT_MASK_NO_EVENT, (const char *)&ev);
2701 /* synthetic response to a ConfigureRequest when not making a change */
2703 config_win(struct ws_win *win, xcb_configure_request_event_t *ev)
2705 xcb_configure_notify_event_t ce;
2710 /* send notification of unchanged state. */
2711 bzero(&ce, sizeof(ce));
2712 ce.response_type = XCB_CONFIGURE_NOTIFY;
2715 ce.width = WIDTH(win);
2716 ce.height = HEIGHT(win);
2717 ce.override_redirect = 0;
2722 ce.window = win->id;
2723 ce.border_width = BORDER(win);
2724 ce.above_sibling = XCB_WINDOW_NONE;
2727 ce.event = ev->window;
2728 ce.window = ev->window;
2730 /* make response appear more WM_SIZE_HINTS-compliant */
2731 if (win->sh.flags) {
2732 DNPRINTF(SWM_D_MISC, "config_win: hints: window: 0x%x,"
2733 " sh.flags: %u, min: %d x %d, max: %d x %d, inc: "
2734 "%d x %d\n", win->id, win->sh.flags, SH_MIN_W(win),
2735 SH_MIN_H(win), SH_MAX_W(win), SH_MAX_H(win),
2736 SH_INC_W(win), SH_INC_H(win));
2741 /* the hint may be set... to 0! */
2742 if (SH_MIN_W(win) > 0 && ce.width < SH_MIN_W(win))
2743 ce.width = SH_MIN_W(win);
2744 if (SH_MIN_H(win) > 0 && ce.height < SH_MIN_H(win))
2745 ce.height = SH_MIN_H(win);
2750 /* may also be advertized as 0 */
2751 if (SH_MAX_W(win) > 0 && ce.width > SH_MAX_W(win))
2752 ce.width = SH_MAX_W(win);
2753 if (SH_MAX_H(win) > 0 && ce.height > SH_MAX_H(win))
2754 ce.height = SH_MAX_H(win);
2757 /* resize increment. */
2759 if (SH_INC_W(win) > 1 && ce.width > SH_INC_W(win))
2760 ce.width -= (ce.width - SH_MIN_W(win)) %
2762 if (SH_INC_H(win) > 1 && ce.height > SH_INC_H(win))
2763 ce.height -= (ce.height - SH_MIN_H(win)) %
2767 /* adjust x and y for requested border_width. */
2768 ce.x += BORDER(win) - ev->border_width;
2769 ce.y += BORDER(win) - ev->border_width;
2770 ce.border_width = ev->border_width;
2771 ce.above_sibling = ev->sibling;
2774 DNPRINTF(SWM_D_MISC, "config_win: ewmh: %s, window: 0x%x, (x,y) w x h: "
2775 "(%d,%d) %d x %d, border: %d\n", YESNO(ev == NULL), win->id, ce.x,
2776 ce.y, ce.width, ce.height, ce.border_width);
2778 xcb_send_event(conn, 0, win->id, XCB_EVENT_MASK_STRUCTURE_NOTIFY,
2783 count_win(struct workspace *ws, int count_transient)
2788 TAILQ_FOREACH(win, &ws->winlist, entry) {
2789 if (!count_transient && win->floating)
2791 if (!count_transient && win->transient)
2797 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
2803 quit(struct swm_region *r, union arg *args)
2805 /* suppress unused warnings since vars are needed */
2809 DNPRINTF(SWM_D_MISC, "quit\n");
2814 map_window(struct ws_win *win, xcb_window_t sibling)
2816 uint16_t mode = XCB_CONFIG_WINDOW_STACK_MODE;
2820 /* If sibling is specified, stack right above it. */
2821 if (sibling != XCB_WINDOW_NONE) {
2822 mode |= XCB_CONFIG_WINDOW_SIBLING;
2826 val[i] = XCB_STACK_MODE_ABOVE;
2831 DNPRINTF(SWM_D_EVENT, "map_window: win 0x%x, mapped: %s, "
2832 "sibling: 0x%x\n", win->id, YESNO(win->mapped), sibling);
2834 xcb_configure_window(conn, win->id, mode, val);
2839 xcb_map_window(conn, win->id);
2840 set_win_state(win, XCB_ICCCM_WM_STATE_NORMAL);
2845 unmap_window(struct ws_win *win)
2850 DNPRINTF(SWM_D_EVENT, "unmap_window: win 0x%x, mapped: %s\n", win->id,
2851 YESNO(win->mapped));
2856 xcb_unmap_window(conn, win->id);
2857 set_win_state(win, XCB_ICCCM_WM_STATE_ICONIC);
2865 int i, j, num_screens;
2867 num_screens = get_screen_count();
2868 for (i = 0; i < num_screens; i++)
2869 for (j = 0; j < workspace_limit; j++)
2870 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2875 fake_keypress(struct ws_win *win, xcb_keysym_t keysym, uint16_t modifiers)
2877 xcb_key_press_event_t event;
2878 xcb_keycode_t *keycode;
2883 keycode = xcb_key_symbols_get_keycode(syms, keysym);
2885 DNPRINTF(SWM_D_MISC, "fake_keypress: win 0x%x keycode %u\n",
2888 bzero(&event, sizeof(event));
2889 event.event = win->id;
2890 event.root = win->s->root;
2891 event.child = XCB_WINDOW_NONE;
2892 event.time = XCB_CURRENT_TIME;
2893 event.event_x = X(win);
2894 event.event_y = Y(win);
2897 event.same_screen = 1;
2898 event.detail = *keycode;
2899 event.state = modifiers;
2901 event.response_type = XCB_KEY_PRESS;
2902 xcb_send_event(conn, 1, win->id,
2903 XCB_EVENT_MASK_KEY_PRESS, (const char *)&event);
2905 event.response_type = XCB_KEY_RELEASE;
2906 xcb_send_event(conn, 1, win->id,
2907 XCB_EVENT_MASK_KEY_RELEASE, (const char *)&event);
2913 restart(struct swm_region *r, union arg *args)
2915 /* suppress unused warning since var is needed */
2919 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
2923 execvp(start_argv[0], start_argv);
2924 warn("execvp failed");
2929 get_pointer_win(xcb_window_t root)
2931 struct ws_win *win = NULL;
2932 xcb_query_pointer_reply_t *r;
2934 DNPRINTF(SWM_D_EVENT, "get_pointer_win: root: 0x%x.\n", root);
2936 r = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, root), NULL);
2938 win = find_window(r->child);
2940 DNPRINTF(SWM_D_EVENT, "get_pointer_win: 0x%x.\n",
2943 DNPRINTF(SWM_D_EVENT, "get_pointer_win: none.\n");
2952 root_to_region(xcb_window_t root, int check)
2955 struct swm_region *r = NULL;
2957 xcb_query_pointer_reply_t *qpr;
2958 xcb_get_input_focus_reply_t *gifr;
2960 DNPRINTF(SWM_D_MISC, "root_to_region: window: 0x%x\n", root);
2962 num_screens = get_screen_count();
2963 for (i = 0; i < num_screens; i++)
2964 if (screens[i].root == root)
2967 if (check & SWM_CK_REGION)
2968 r = screens[i].r_focus;
2970 if (r == NULL && check & SWM_CK_FOCUS) {
2971 /* Try to find an actively focused window */
2972 gifr = xcb_get_input_focus_reply(conn,
2973 xcb_get_input_focus(conn), NULL);
2975 cfw = find_window(gifr->focus);
2976 if (cfw && cfw->ws->r)
2983 if (r == NULL && check & SWM_CK_POINTER) {
2984 /* No region with an active focus; try to use pointer. */
2985 qpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn,
2986 screens[i].root), NULL);
2989 DNPRINTF(SWM_D_MISC, "root_to_region: pointer: "
2990 "(%d,%d)\n", qpr->root_x, qpr->root_y);
2991 TAILQ_FOREACH(r, &screens[i].rl, entry)
2992 if (X(r) <= qpr->root_x &&
2993 qpr->root_x < MAX_X(r) &&
2994 Y(r) <= qpr->root_y &&
2995 qpr->root_y < MAX_Y(r))
3002 if (r == NULL && check & SWM_CK_FALLBACK)
3003 r = TAILQ_FIRST(&screens[i].rl);
3005 DNPRINTF(SWM_D_MISC, "root_to_region: idx: %d\n", get_region_index(r));
3011 find_unmanaged_window(xcb_window_t id)
3014 int i, j, num_screens;
3016 num_screens = get_screen_count();
3017 for (i = 0; i < num_screens; i++)
3018 for (j = 0; j < workspace_limit; j++)
3019 TAILQ_FOREACH(win, &screens[i].ws[j].unmanagedlist,
3027 find_window(xcb_window_t id)
3030 int i, j, num_screens;
3031 xcb_query_tree_reply_t *r;
3033 num_screens = get_screen_count();
3034 for (i = 0; i < num_screens; i++)
3035 for (j = 0; j < workspace_limit; j++)
3036 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
3040 r = xcb_query_tree_reply(conn, xcb_query_tree(conn, id), NULL);
3044 /* if we were looking for the parent return that window instead */
3045 if (r->parent == 0 || r->root == r->parent) {
3050 /* look for parent */
3051 for (i = 0; i < num_screens; i++)
3052 for (j = 0; j < workspace_limit; j++)
3053 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
3054 if (r->parent == win->id) {
3064 spawn(int ws_idx, union arg *args, int close_fd)
3069 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
3071 close(xcb_get_file_descriptor(conn));
3073 setenv("LD_PRELOAD", SWM_LIB, 1);
3075 if (asprintf(&ret, "%d", ws_idx) == -1) {
3076 warn("spawn: asprintf SWM_WS");
3079 setenv("_SWM_WS", ret, 1);
3083 if (asprintf(&ret, "%d", getpid()) == -1) {
3084 warn("spawn: asprintf _SWM_PID");
3087 setenv("_SWM_PID", ret, 1);
3091 if (setsid() == -1) {
3092 warn("spawn: setsid");
3098 * close stdin and stdout to prevent interaction between apps
3099 * and the baraction script
3100 * leave stderr open to record errors
3102 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) == -1) {
3103 warn("spawn: open");
3106 dup2(fd, STDIN_FILENO);
3107 dup2(fd, STDOUT_FILENO);
3112 execvp(args->argv[0], args->argv);
3114 warn("spawn: execvp");
3119 kill_refs(struct ws_win *win)
3121 int i, x, num_screens;
3122 struct swm_region *r;
3123 struct workspace *ws;
3128 num_screens = get_screen_count();
3129 for (i = 0; i < num_screens; i++)
3130 TAILQ_FOREACH(r, &screens[i].rl, entry)
3131 for (x = 0; x < workspace_limit; x++) {
3133 if (win == ws->focus)
3135 if (win == ws->focus_prev)
3136 ws->focus_prev = NULL;
3141 validate_win(struct ws_win *testwin)
3144 struct workspace *ws;
3145 struct swm_region *r;
3146 int i, x, num_screens;
3148 if (testwin == NULL)
3151 num_screens = get_screen_count();
3152 for (i = 0; i < num_screens; i++)
3153 TAILQ_FOREACH(r, &screens[i].rl, entry)
3154 for (x = 0; x < workspace_limit; x++) {
3156 TAILQ_FOREACH(win, &ws->winlist, entry)
3164 validate_ws(struct workspace *testws)
3166 struct swm_region *r;
3167 struct workspace *ws;
3168 int i, x, num_screens;
3170 /* validate all ws */
3171 num_screens = get_screen_count();
3172 for (i = 0; i < num_screens; i++)
3173 TAILQ_FOREACH(r, &screens[i].rl, entry)
3174 for (x = 0; x < workspace_limit; x++) {
3183 get_sibling(struct ws_win *win, int mode)
3185 struct ws_win *w = win;
3189 TAILQ_FOREACH_REVERSE(w, &w->ws->winlist, ws_win_list, entry)
3190 if (w != win && !w->floating && !w->iconic)
3193 case SWM_STACK_ABOVE:
3195 w = TAILQ_NEXT(w, entry);
3196 } while (w != NULL && (w == win || w->floating || w->iconic));
3198 case SWM_STACK_BELOW:
3200 w = TAILQ_PREV(w, ws_win_list, entry);
3201 } while (w != NULL && (w == win || w->floating || w->iconic));
3203 case SWM_STACK_BOTTOM:
3204 TAILQ_FOREACH(w, &w->ws->winlist, entry)
3205 if (w != win && !w->floating && !w->iconic)
3213 return (win->ws->r->id);
3219 unfocus_win(struct ws_win *win)
3221 xcb_window_t none = XCB_WINDOW_NONE;
3223 DNPRINTF(SWM_D_FOCUS, "unfocus_win: window: 0x%x\n", WINID(win));
3227 if (win->ws == NULL) {
3228 DNPRINTF(SWM_D_FOCUS, "unfocus_win: NULL ws.\n");
3232 if (validate_ws(win->ws)) {
3233 DNPRINTF(SWM_D_FOCUS, "unfocus_win: invalid ws.\n");
3237 if (win->ws->r == NULL) {
3238 DNPRINTF(SWM_D_FOCUS, "unfocus_win: NULL region.\n");
3242 if (validate_win(win)) {
3243 DNPRINTF(SWM_D_FOCUS, "unfocus_win: invalid win.\n");
3248 if (win->ws->focus == win) {
3249 if (tile_gap < 0 && !win->floating)
3250 map_window(win, get_sibling(win, SWM_STACK_BELOW));
3251 win->ws->focus = NULL;
3252 win->ws->focus_prev = win;
3255 if (validate_win(win->ws->focus)) {
3256 kill_refs(win->ws->focus);
3257 win->ws->focus = NULL;
3260 if (validate_win(win->ws->focus_prev)) {
3261 kill_refs(win->ws->focus_prev);
3262 win->ws->focus_prev = NULL;
3265 xcb_change_window_attributes(conn, win->id, XCB_CW_BORDER_PIXEL,
3266 &win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].pixel);
3268 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
3269 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1, &none);
3271 DNPRINTF(SWM_D_FOCUS, "unfocus_win: done.\n");
3275 focus_win(struct ws_win *win)
3277 struct ws_win *cfw = NULL, *parent = NULL, *w;
3278 struct workspace *ws;
3279 xcb_get_input_focus_reply_t *r;
3281 DNPRINTF(SWM_D_FOCUS, "focus_win: window: 0x%x\n", WINID(win));
3286 if (win->ws == NULL)
3294 if (validate_ws(ws))
3297 if (validate_win(win)) {
3302 r = xcb_get_input_focus_reply(conn, xcb_get_input_focus(conn), NULL);
3304 cfw = find_window(r->focus);
3310 if (ws->focus != win) {
3311 if (ws->focus && ws->focus != cfw)
3312 unfocus_win(ws->focus);
3316 /* If this window directs focus to a child window, then clear. */
3317 if (win->focus_child)
3318 win->focus_child = NULL;
3320 /* If transient, adjust parent's focus child for focus_magic. */
3321 if (win->transient) {
3322 parent = find_window(win->transient);
3323 if (parent && parent->focus_child != win)
3324 parent->focus_child = win;
3327 if (cfw != win && ws->r != NULL) {
3328 /* Set input focus if no input hint, or indicated by hint. */
3329 if (!(win->hints.flags & XCB_ICCCM_WM_HINT_INPUT) ||
3330 (win->hints.flags & XCB_ICCCM_WM_HINT_INPUT &&
3332 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT,
3333 win->id, last_event_time);
3335 /* Tell app it can adjust focus to a specific window. */
3336 if (win->take_focus) {
3337 /* java is special; always tell parent */
3338 if (win->transient && win->java)
3339 client_msg(parent, a_takefocus,
3342 client_msg(win, a_takefocus, last_event_time);
3345 xcb_change_window_attributes(conn, win->id, XCB_CW_BORDER_PIXEL,
3346 &ws->r->s->c[SWM_S_COLOR_FOCUS].pixel);
3348 if (ws->cur_layout->flags & SWM_L_MAPONFOCUS ||
3350 /* If a parent exists, map it first. */
3352 map_window(parent, XCB_WINDOW_NONE);
3354 /* Map siblings next. */
3355 TAILQ_FOREACH(w, &ws->winlist, entry)
3356 if (w != win && !w->iconic &&
3357 w->transient == parent->id)
3358 map_window(w, XCB_WINDOW_NONE);
3361 /* Map focused window. */
3362 map_window(win, XCB_WINDOW_NONE);
3364 /* Finally, map children of focus window. */
3365 TAILQ_FOREACH(w, &ws->winlist, entry)
3366 if (w->transient == win->id && !w->iconic)
3367 map_window(w, XCB_WINDOW_NONE);
3368 } else if (tile_gap < 0 && !win->floating) {
3370 * Windows overlap in the layout.
3371 * Raise focused win above all tiled wins.
3373 if (tile_gap < 0 && !win->floating)
3375 get_sibling(win, SWM_STACK_TOP));
3380 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
3381 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1,
3388 DNPRINTF(SWM_D_FOCUS, "focus_win: done.\n");
3391 /* If a child window should have focus instead, return it. */
3393 get_focus_magic(struct ws_win *win)
3395 struct ws_win *parent = NULL;
3396 struct ws_win *child = NULL;
3398 DNPRINTF(SWM_D_FOCUS, "get_focus_magic: window: 0x%x\n", WINID(win));
3402 if (win->transient) {
3403 parent = find_window(win->transient);
3405 /* If parent prefers focus elsewhere, then try to do so. */
3406 if (parent && (child = parent->focus_child)) {
3407 if (validate_win(child) == 0 && child->mapped)
3410 parent->focus_child = NULL;
3414 /* If this window prefers focus elsewhere, then try to do so. */
3415 if ((child = win->focus_child)) {
3416 if (validate_win(child) == 0 && child->mapped)
3419 win->focus_child = NULL;
3426 event_drain(uint8_t rt)
3428 xcb_generic_event_t *evt;
3430 /* ensure all pending requests have been processed before filtering. */
3432 while ((evt = xcb_poll_for_event(conn))) {
3433 if (XCB_EVENT_RESPONSE_TYPE(evt) != rt)
3441 set_region(struct swm_region *r)
3443 struct swm_region *rf;
3449 /* Unfocus old region bar. */
3454 xcb_change_window_attributes(conn, rf->bar->id,
3455 XCB_CW_BORDER_PIXEL,
3456 &r->s->c[SWM_S_COLOR_BAR_BORDER_UNFOCUS].pixel);
3459 /* Set region bar border to focus_color. */
3460 xcb_change_window_attributes(conn, r->bar->id,
3461 XCB_CW_BORDER_PIXEL, &r->s->c[SWM_S_COLOR_BAR_BORDER].pixel);
3467 focus_region(struct swm_region *r)
3470 struct swm_region *old_r;
3475 old_r = r->s->r_focus;
3478 nfw = get_region_focus(r);
3482 /* New region is empty; need to manually unfocus win. */
3484 unfocus_win(old_r->ws->focus);
3486 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT, r->id,
3489 /* Clear bar since empty. */
3495 switchws(struct swm_region *r, union arg *args)
3497 int wsid = args->id, unmap_old = 0;
3498 struct swm_region *this_r, *other_r;
3500 struct workspace *new_ws, *old_ws;
3505 if (wsid >= workspace_limit)
3509 old_ws = this_r->ws;
3510 new_ws = &this_r->s->ws[wsid];
3512 DNPRINTF(SWM_D_WS, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
3513 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), old_ws->idx, wsid);
3515 if (new_ws == NULL || old_ws == NULL)
3517 if (new_ws == old_ws)
3520 unfocus_win(old_ws->focus);
3522 other_r = new_ws->r;
3523 if (other_r == NULL) {
3524 /* the other workspace is hidden, hide this one */
3528 /* the other ws is visible in another region, exchange them */
3529 other_r->ws_prior = new_ws;
3530 other_r->ws = old_ws;
3531 old_ws->r = other_r;
3533 this_r->ws_prior = old_ws;
3534 this_r->ws = new_ws;
3537 /* Set focus_pending before stacking, if needed. */
3538 if (focus_mode != SWM_FOCUS_FOLLOW && (!new_ws->focus_pending ||
3539 validate_win(new_ws->focus_pending)))
3540 new_ws->focus_pending = get_region_focus(new_ws->r);
3544 /* unmap old windows */
3546 TAILQ_FOREACH(win, &old_ws->winlist, entry)
3549 /* if workspaces were swapped, then don't wait to set focus */
3550 if (old_ws->r && focus_mode != SWM_FOCUS_FOLLOW) {
3551 if (new_ws->focus_pending) {
3552 focus_win(new_ws->focus_pending);
3553 new_ws->focus_pending = NULL;
3557 /* Clear bar and set focus on region input win if new ws is empty. */
3558 if (new_ws->focus_pending == NULL && new_ws->focus == NULL) {
3559 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT, r->id,
3566 DNPRINTF(SWM_D_WS, "switchws: done.\n");
3570 cyclews(struct swm_region *r, union arg *args)
3573 struct swm_screen *s = r->s;
3577 DNPRINTF(SWM_D_WS, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3578 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
3584 case SWM_ARG_ID_CYCLEWS_MOVE_UP:
3587 case SWM_ARG_ID_CYCLEWS_UP_ALL:
3590 case SWM_ARG_ID_CYCLEWS_UP:
3591 a.id = (a.id < workspace_limit - 1) ? a.id + 1 : 0;
3593 case SWM_ARG_ID_CYCLEWS_MOVE_DOWN:
3596 case SWM_ARG_ID_CYCLEWS_DOWN_ALL:
3599 case SWM_ARG_ID_CYCLEWS_DOWN:
3600 a.id = (a.id > 0) ? a.id - 1 : workspace_limit - 1;
3607 (!cycle_empty && TAILQ_EMPTY(&s->ws[a.id].winlist)))
3609 if (!cycle_visible && s->ws[a.id].r != NULL)
3616 } while (a.id != r->ws->idx);
3620 priorws(struct swm_region *r, union arg *args)
3626 DNPRINTF(SWM_D_WS, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3627 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
3629 if (r->ws_prior == NULL)
3632 a.id = r->ws_prior->idx;
3637 focusrg(struct swm_region *r, union arg *args)
3639 int ridx = args->id, i, num_screens;
3640 struct swm_region *rr = NULL;
3642 num_screens = get_screen_count();
3643 /* do nothing if we don't have more than one screen */
3644 if (!(num_screens > 1 || outputs > 1))
3647 DNPRINTF(SWM_D_FOCUS, "focusrg: id: %d\n", ridx);
3649 rr = TAILQ_FIRST(&r->s->rl);
3650 for (i = 0; i < ridx; ++i)
3651 rr = TAILQ_NEXT(rr, entry);
3661 cyclerg(struct swm_region *r, union arg *args)
3663 struct swm_region *rr = NULL;
3666 num_screens = get_screen_count();
3667 /* do nothing if we don't have more than one screen */
3668 if (!(num_screens > 1 || outputs > 1))
3673 case SWM_ARG_ID_CYCLERG_UP:
3674 rr = TAILQ_NEXT(r, entry);
3676 rr = TAILQ_FIRST(&screens[i].rl);
3678 case SWM_ARG_ID_CYCLERG_DOWN:
3679 rr = TAILQ_PREV(r, swm_region_list, entry);
3681 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
3694 sort_windows(struct ws_win_list *wl)
3696 struct ws_win *win, *parent, *nxt;
3701 for (win = TAILQ_FIRST(wl); win != TAILQ_END(wl); win = nxt) {
3702 nxt = TAILQ_NEXT(win, entry);
3703 if (win->transient) {
3704 parent = find_window(win->transient);
3705 if (parent == NULL) {
3706 warnx("not possible bug");
3709 TAILQ_REMOVE(wl, win, entry);
3710 TAILQ_INSERT_AFTER(wl, parent, win, entry);
3717 swapwin(struct swm_region *r, union arg *args)
3719 struct ws_win *target, *source;
3720 struct ws_win *cur_focus;
3721 struct ws_win_list *wl;
3723 DNPRINTF(SWM_D_WS, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3724 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
3726 cur_focus = r->ws->focus;
3727 if (cur_focus == NULL)
3731 wl = &source->ws->winlist;
3734 case SWM_ARG_ID_SWAPPREV:
3735 if (source->transient)
3736 source = find_window(source->transient);
3737 target = TAILQ_PREV(source, ws_win_list, entry);
3738 if (target && target->transient)
3739 target = find_window(target->transient);
3740 TAILQ_REMOVE(wl, source, entry);
3742 TAILQ_INSERT_TAIL(wl, source, entry);
3744 TAILQ_INSERT_BEFORE(target, source, entry);
3746 case SWM_ARG_ID_SWAPNEXT:
3747 target = TAILQ_NEXT(source, entry);
3748 /* move the parent and let the sort handle the move */
3749 if (source->transient)
3750 source = find_window(source->transient);
3751 TAILQ_REMOVE(wl, source, entry);
3753 TAILQ_INSERT_HEAD(wl, source, entry);
3755 TAILQ_INSERT_AFTER(wl, target, source, entry);
3757 case SWM_ARG_ID_SWAPMAIN:
3758 target = TAILQ_FIRST(wl);
3759 if (target == source) {
3760 if (source->ws->focus_prev != NULL &&
3761 source->ws->focus_prev != target)
3762 source = source->ws->focus_prev;
3766 if (target == NULL || source == NULL)
3768 source->ws->focus_prev = target;
3769 TAILQ_REMOVE(wl, target, entry);
3770 TAILQ_INSERT_BEFORE(source, target, entry);
3771 TAILQ_REMOVE(wl, source, entry);
3772 TAILQ_INSERT_HEAD(wl, source, entry);
3774 case SWM_ARG_ID_MOVELAST:
3775 TAILQ_REMOVE(wl, source, entry);
3776 TAILQ_INSERT_TAIL(wl, source, entry);
3779 DNPRINTF(SWM_D_MOVE, "swapwin: invalid id: %d\n", args->id);
3791 get_focus_prev(struct ws_win *win)
3793 struct ws_win *winfocus = NULL;
3794 struct ws_win *cur_focus = NULL;
3795 struct ws_win_list *wl = NULL;
3796 struct workspace *ws = NULL;
3798 if (!(win && win->ws))
3803 cur_focus = ws->focus;
3805 DNPRINTF(SWM_D_FOCUS, "get_focus_prev: window: 0x%x, cur_focus: 0x%x\n",
3806 WINID(win), WINID(cur_focus));
3808 /* pickle, just focus on whatever */
3809 if (cur_focus == NULL) {
3810 /* use prev_focus if valid */
3811 if (ws->focus_prev && ws->focus_prev != cur_focus &&
3812 find_window(WINID(ws->focus_prev)))
3813 winfocus = ws->focus_prev;
3817 /* if transient focus on parent */
3818 if (cur_focus->transient) {
3819 winfocus = find_window(cur_focus->transient);
3823 /* if in max_stack try harder */
3824 if ((win->quirks & SWM_Q_FOCUSPREV) ||
3825 (ws->cur_layout->flags & SWM_L_FOCUSPREV)) {
3826 if (cur_focus != ws->focus_prev)
3827 winfocus = ws->focus_prev;
3829 winfocus = TAILQ_PREV(win, ws_win_list, entry);
3834 DNPRINTF(SWM_D_FOCUS, "get_focus_prev: focus_close: %d\n", focus_close);
3836 if (winfocus == NULL || winfocus == win) {
3837 switch (focus_close) {
3838 case SWM_STACK_BOTTOM:
3839 TAILQ_FOREACH(winfocus, wl, entry)
3840 if (!winfocus->iconic && winfocus != cur_focus)
3844 TAILQ_FOREACH_REVERSE(winfocus, wl, ws_win_list, entry)
3845 if (!winfocus->iconic && winfocus != cur_focus)
3848 case SWM_STACK_ABOVE:
3849 winfocus = TAILQ_NEXT(cur_focus, entry);
3850 while (winfocus && winfocus->iconic)
3851 winfocus = TAILQ_NEXT(winfocus, entry);
3853 if (winfocus == NULL) {
3854 if (focus_close_wrap) {
3855 TAILQ_FOREACH(winfocus, wl, entry)
3856 if (!winfocus->iconic &&
3857 winfocus != cur_focus)
3860 TAILQ_FOREACH_REVERSE(winfocus, wl,
3862 if (!winfocus->iconic &&
3863 winfocus != cur_focus)
3868 case SWM_STACK_BELOW:
3869 winfocus = TAILQ_PREV(cur_focus, ws_win_list, entry);
3870 while (winfocus && winfocus->iconic)
3871 winfocus = TAILQ_PREV(winfocus, ws_win_list,
3874 if (winfocus == NULL) {
3875 if (focus_close_wrap) {
3876 TAILQ_FOREACH_REVERSE(winfocus, wl,
3878 if (!winfocus->iconic &&
3879 winfocus != cur_focus)
3882 TAILQ_FOREACH(winfocus, wl, entry)
3883 if (!winfocus->iconic &&
3884 winfocus != cur_focus)
3892 if (winfocus == NULL ||
3893 (winfocus && (winfocus->iconic || winfocus == cur_focus))) {
3894 if (focus_default == SWM_STACK_TOP) {
3895 TAILQ_FOREACH_REVERSE(winfocus, wl, ws_win_list, entry)
3896 if (!winfocus->iconic && winfocus != cur_focus)
3899 TAILQ_FOREACH(winfocus, wl, entry)
3900 if (!winfocus->iconic && winfocus != cur_focus)
3907 return get_focus_magic(winfocus);
3911 get_region_focus(struct swm_region *r)
3913 struct ws_win *winfocus = NULL;
3918 if (r->ws->focus && !r->ws->focus->iconic)
3919 winfocus = r->ws->focus;
3920 else if (r->ws->focus_prev && !r->ws->focus_prev->iconic)
3921 winfocus = r->ws->focus_prev;
3923 TAILQ_FOREACH(winfocus, &r->ws->winlist, entry)
3924 if (!winfocus->iconic)
3927 return get_focus_magic(winfocus);
3931 focus(struct swm_region *r, union arg *args)
3933 struct ws_win *head, *cur_focus = NULL, *winfocus = NULL;
3934 struct ws_win_list *wl = NULL;
3935 struct workspace *ws = NULL;
3941 DNPRINTF(SWM_D_FOCUS, "focus: id: %d\n", args->id);
3943 if ((cur_focus = r->ws->focus) == NULL)
3947 if (TAILQ_EMPTY(wl))
3949 /* make sure there is at least one uniconified window */
3951 TAILQ_FOREACH(winfocus, wl, entry)
3952 if (!winfocus->iconic) {
3960 case SWM_ARG_ID_FOCUSPREV:
3961 head = TAILQ_PREV(cur_focus, ws_win_list, entry);
3963 head = TAILQ_LAST(wl, ws_win_list);
3965 if (WINID(winfocus) == cur_focus->transient) {
3966 head = TAILQ_PREV(winfocus, ws_win_list, entry);
3968 head = TAILQ_LAST(wl, ws_win_list);
3973 if (winfocus && winfocus->iconic) {
3974 while (winfocus != cur_focus) {
3975 if (winfocus == NULL)
3976 winfocus = TAILQ_LAST(wl, ws_win_list);
3977 if (!winfocus->iconic)
3979 winfocus = TAILQ_PREV(winfocus, ws_win_list,
3985 case SWM_ARG_ID_FOCUSNEXT:
3986 head = TAILQ_NEXT(cur_focus, entry);
3988 head = TAILQ_FIRST(wl);
3992 if (winfocus && winfocus->iconic) {
3993 while (winfocus != cur_focus) {
3994 if (winfocus == NULL)
3995 winfocus = TAILQ_FIRST(wl);
3996 if (!winfocus->iconic)
3998 winfocus = TAILQ_NEXT(winfocus, entry);
4003 case SWM_ARG_ID_FOCUSMAIN:
4004 winfocus = TAILQ_FIRST(wl);
4005 if (winfocus == cur_focus)
4006 winfocus = cur_focus->ws->focus_prev;
4013 focus_win(get_focus_magic(winfocus));
4019 cycle_layout(struct swm_region *r, union arg *args)
4021 struct workspace *ws = r->ws;
4023 /* suppress unused warning since var is needed */
4026 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
4029 if (ws->cur_layout->l_stack == NULL)
4030 ws->cur_layout = &layouts[0];
4035 focus_win(get_region_focus(r));
4041 stack_config(struct swm_region *r, union arg *args)
4043 struct workspace *ws = r->ws;
4045 DNPRINTF(SWM_D_STACK, "stack_config: id: %d workspace: %d\n",
4048 if (ws->cur_layout->l_config != NULL)
4049 ws->cur_layout->l_config(ws, args->id);
4051 if (args->id != SWM_ARG_ID_STACKINIT)
4060 struct swm_geometry g;
4061 struct swm_region *r;
4067 DNPRINTF(SWM_D_STACK, "stack: begin\n");
4069 num_screens = get_screen_count();
4070 for (i = 0; i < num_screens; i++) {
4074 TAILQ_FOREACH(r, &screens[i].rl, entry) {
4075 DNPRINTF(SWM_D_STACK, "stack: workspace: %d "
4076 "(screen: %d, region: %d)\n", r->ws->idx, i, j++);
4078 /* Adjust stack area for region bar and padding. */
4080 g.x += region_padding;
4081 g.y += region_padding;
4082 g.w -= 2 * border_width + 2 * region_padding;
4083 g.h -= 2 * border_width + 2 * region_padding;
4084 if (bar_enabled && r->ws->bar_enabled) {
4089 r->ws->cur_layout->l_stack(r->ws, &g);
4090 r->ws->cur_layout->l_string(r->ws);
4091 /* save r so we can track region changes */
4098 DNPRINTF(SWM_D_STACK, "stack: end\n");
4102 store_float_geom(struct ws_win *win)
4104 if (win == NULL || win->ws->r == NULL)
4107 /* retain window geom and region geom */
4108 win->g_float = win->g;
4109 win->g_float.x -= X(win->ws->r);
4110 win->g_float.y -= Y(win->ws->r);
4111 win->g_floatvalid = 1;
4112 DNPRINTF(SWM_D_MISC, "store_float_geom: window: 0x%x, g: (%d,%d)"
4113 " %d x %d, g_float: (%d,%d) %d x %d\n", win->id, X(win), Y(win),
4114 WIDTH(win), HEIGHT(win), win->g_float.x, win->g_float.y,
4115 win->g_float.w, win->g_float.h);
4119 load_float_geom(struct ws_win *win)
4121 if (win == NULL || win->ws->r == NULL)
4124 if (win->g_floatvalid) {
4125 win->g = win->g_float;
4126 X(win) += X(win->ws->r);
4127 Y(win) += Y(win->ws->r);
4128 DNPRINTF(SWM_D_MISC, "load_float_geom: window: 0x%x, g: (%d,%d)"
4129 "%d x %d\n", win->id, X(win), Y(win), WIDTH(win),
4132 DNPRINTF(SWM_D_MISC, "load_float_geom: window: 0x%x, g_float "
4133 "is not set.\n", win->id);
4138 stack_floater(struct ws_win *win, struct swm_region *r)
4143 DNPRINTF(SWM_D_MISC, "stack_floater: window: 0x%x\n", win->id);
4146 * to allow windows to change their size (e.g. mplayer fs) only retrieve
4147 * geom on ws switches or return from max mode
4149 if (win->floatmaxed || (r != r->ws->old_r &&
4150 !(win->ewmh_flags & EWMH_F_FULLSCREEN))) {
4151 /* update geometry for the new region */
4152 load_float_geom(win);
4155 win->floatmaxed = 0;
4158 * if set to fullscreen mode, configure window to maximum size.
4160 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
4161 if (!win->g_floatvalid)
4162 store_float_geom(win);
4168 * remove border on fullscreen floater when in fullscreen mode or when
4169 * the quirk is present.
4171 if ((win->ewmh_flags & EWMH_F_FULLSCREEN) ||
4172 ((win->quirks & SWM_Q_FULLSCREEN) &&
4173 (WIDTH(win) >= WIDTH(r)) && (HEIGHT(win) >= HEIGHT(r)))) {
4174 if (win->bordered) {
4176 X(win) += border_width;
4177 Y(win) += border_width;
4179 } else if (!win->bordered) {
4181 X(win) -= border_width;
4182 Y(win) -= border_width;
4185 if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
4186 WIDTH(win) = (double)WIDTH(r) * dialog_ratio;
4187 HEIGHT(win) = (double)HEIGHT(r) * dialog_ratio;
4190 if (!win->manual && !(win->ewmh_flags & EWMH_F_FULLSCREEN) &&
4191 !(win->quirks & SWM_Q_ANYWHERE)) {
4193 * floaters and transients are auto-centred unless moved,
4194 * resized or ANYWHERE quirk is set.
4196 X(win) = X(r) + (WIDTH(r) - WIDTH(win)) / 2 - BORDER(win);
4197 Y(win) = Y(r) + (HEIGHT(r) - HEIGHT(win)) / 2 - BORDER(win);
4199 store_float_geom(win);
4202 /* Ensure at least 1 pixel of the window is in the region. */
4203 region_containment(win, r, SWM_CW_ALLSIDES);
4209 * Send keystrokes to terminal to decrease/increase the font size as the
4210 * window size changes.
4213 adjust_font(struct ws_win *win)
4215 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
4216 win->floating || win->transient)
4219 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
4220 WIDTH(win) / win->sh.width_inc < term_width &&
4221 win->font_steps < SWM_MAX_FONT_STEPS) {
4222 win->font_size_boundary[win->font_steps] =
4223 (win->sh.width_inc * term_width) + win->sh.base_width;
4226 win->last_inc = win->sh.width_inc;
4227 fake_keypress(win, XK_KP_Subtract, XCB_MOD_MASK_SHIFT);
4228 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
4229 WIDTH(win) > win->font_size_boundary[win->font_steps - 1]) {
4232 win->last_inc = win->sh.width_inc;
4233 fake_keypress(win, XK_KP_Add, XCB_MOD_MASK_SHIFT);
4237 #define SWAPXY(g) do { \
4239 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
4240 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
4243 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
4245 struct swm_geometry win_g, r_g = *g;
4246 struct ws_win *win, *fs_win = NULL;
4247 int i, j, s, stacks;
4248 int w_inc = 1, h_inc, w_base = 1, h_base;
4249 int hrh, extra = 0, h_slice, last_h = 0;
4250 int split, colno, winno, mwin, msize, mscale;
4251 int remain, missing, v_slice, reconfigure = 0;
4254 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d, rot: %s, "
4255 "flip: %s\n", ws->idx, YESNO(rot), YESNO(flip));
4257 winno = count_win(ws, 0);
4258 if (winno == 0 && count_win(ws, 1) == 0)
4261 TAILQ_FOREACH(win, &ws->winlist, entry)
4262 if (!win->transient && !win->floating && !win->iconic)
4269 w_inc = win->sh.width_inc;
4270 w_base = win->sh.base_width;
4271 mwin = ws->l_state.horizontal_mwin;
4272 mscale = ws->l_state.horizontal_msize;
4273 stacks = ws->l_state.horizontal_stacks;
4276 w_inc = win->sh.height_inc;
4277 w_base = win->sh.base_height;
4278 mwin = ws->l_state.vertical_mwin;
4279 mscale = ws->l_state.vertical_msize;
4280 stacks = ws->l_state.vertical_stacks;
4284 if (stacks > winno - mwin)
4285 stacks = winno - mwin;
4289 h_slice = r_g.h / SWM_H_SLICE;
4290 if (mwin && winno > mwin) {
4291 v_slice = r_g.w / SWM_V_SLICE;
4295 win_g.w = v_slice * mscale;
4297 if (w_inc > 1 && w_inc < v_slice) {
4298 /* adjust for window's requested size increment */
4299 remain = (win_g.w - w_base) % w_inc;
4305 win_g.x += r_g.w - msize;
4308 colno = split = winno / stacks;
4309 win_g.w = ((r_g.w - (stacks * 2 * border_width) +
4310 2 * border_width) / stacks);
4312 hrh = r_g.h / colno;
4313 extra = r_g.h - (colno * hrh);
4314 win_g.h = hrh - 2 * border_width;
4316 /* stack all the tiled windows */
4317 i = j = 0, s = stacks;
4318 TAILQ_FOREACH(win, &ws->winlist, entry) {
4319 if (win->transient || win->floating || win->iconic)
4322 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
4327 if (split && i == split) {
4328 colno = (winno - mwin) / stacks;
4329 if (s <= (winno - mwin) % stacks)
4332 hrh = r_g.h / colno;
4333 extra = r_g.h - (colno * hrh);
4336 win_g.x += win_g.w + 2 * border_width +
4339 win_g.w = (r_g.w - msize -
4340 (stacks * (2 * border_width + tile_gap))) / stacks;
4342 win_g.w += (r_g.w - msize -
4343 (stacks * (2 * border_width + tile_gap))) %
4347 win_g.x -= win_g.w + 2 * border_width +
4353 win_g.h = hrh - 2 * border_width - tile_gap;
4356 h_inc = win->sh.width_inc;
4357 h_base = win->sh.base_width;
4359 h_inc = win->sh.height_inc;
4360 h_base = win->sh.base_height;
4363 if (j == colno - 1) {
4364 win_g.h = hrh + extra;
4365 } else if (h_inc > 1 && h_inc < h_slice) {
4366 /* adjust for window's requested size increment */
4367 remain = (win_g.h - h_base) % h_inc;
4368 missing = h_inc - remain;
4370 if (missing <= extra || j == 0) {
4382 win_g.y += last_h + 2 * border_width + tile_gap;
4384 if (disable_border && !(bar_enabled && ws->bar_enabled) &&
4387 win_g.w += 2 * border_width;
4388 win_g.h += 2 * border_width;
4394 if (X(win) != win_g.y || Y(win) != win_g.x ||
4395 WIDTH(win) != win_g.h || HEIGHT(win) != win_g.w) {
4399 WIDTH(win) = win_g.h;
4400 HEIGHT(win) = win_g.w;
4403 if (X(win) != win_g.x || Y(win) != win_g.y ||
4404 WIDTH(win) != win_g.w || HEIGHT(win) != win_g.h) {
4408 WIDTH(win) = win_g.w;
4409 HEIGHT(win) = win_g.h;
4413 if (bordered != win->bordered) {
4415 win->bordered = bordered;
4423 map_window(win, get_sibling(win, SWM_STACK_BELOW));
4430 /* Map/raise focused tiled window to top if windows could overlap. */
4431 if (tile_gap < 0 && ws->focus != NULL && !ws->focus->floating)
4432 map_window(ws->focus, get_sibling(ws->focus, SWM_STACK_TOP));
4435 /* now, stack all the floaters and transients */
4436 TAILQ_FOREACH(win, &ws->winlist, entry) {
4437 if (!win->transient && !win->floating)
4441 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
4446 stack_floater(win, ws->r);
4447 map_window(win, XCB_WINDOW_NONE);
4450 /* Make sure fs_win is stacked last so it's on top. */
4452 stack_floater(fs_win, ws->r);
4453 map_window(fs_win, XCB_WINDOW_NONE);
4458 vertical_config(struct workspace *ws, int id)
4460 DNPRINTF(SWM_D_STACK, "vertical_config: id: %d, workspace: %d\n",
4464 case SWM_ARG_ID_STACKRESET:
4465 case SWM_ARG_ID_STACKINIT:
4466 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
4467 ws->l_state.vertical_mwin = 1;
4468 ws->l_state.vertical_stacks = 1;
4470 case SWM_ARG_ID_MASTERSHRINK:
4471 if (ws->l_state.vertical_msize > 1)
4472 ws->l_state.vertical_msize--;
4474 case SWM_ARG_ID_MASTERGROW:
4475 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
4476 ws->l_state.vertical_msize++;
4478 case SWM_ARG_ID_MASTERADD:
4479 ws->l_state.vertical_mwin++;
4481 case SWM_ARG_ID_MASTERDEL:
4482 if (ws->l_state.vertical_mwin > 0)
4483 ws->l_state.vertical_mwin--;
4485 case SWM_ARG_ID_STACKINC:
4486 ws->l_state.vertical_stacks++;
4488 case SWM_ARG_ID_STACKDEC:
4489 if (ws->l_state.vertical_stacks > 1)
4490 ws->l_state.vertical_stacks--;
4492 case SWM_ARG_ID_FLIPLAYOUT:
4493 ws->l_state.vertical_flip = !ws->l_state.vertical_flip;
4501 vertical_stack(struct workspace *ws, struct swm_geometry *g)
4503 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
4505 stack_master(ws, g, 0, ws->l_state.vertical_flip);
4509 horizontal_config(struct workspace *ws, int id)
4511 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
4514 case SWM_ARG_ID_STACKRESET:
4515 case SWM_ARG_ID_STACKINIT:
4516 ws->l_state.horizontal_mwin = 1;
4517 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
4518 ws->l_state.horizontal_stacks = 1;
4520 case SWM_ARG_ID_MASTERSHRINK:
4521 if (ws->l_state.horizontal_msize > 1)
4522 ws->l_state.horizontal_msize--;
4524 case SWM_ARG_ID_MASTERGROW:
4525 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
4526 ws->l_state.horizontal_msize++;
4528 case SWM_ARG_ID_MASTERADD:
4529 ws->l_state.horizontal_mwin++;
4531 case SWM_ARG_ID_MASTERDEL:
4532 if (ws->l_state.horizontal_mwin > 0)
4533 ws->l_state.horizontal_mwin--;
4535 case SWM_ARG_ID_STACKINC:
4536 ws->l_state.horizontal_stacks++;
4538 case SWM_ARG_ID_STACKDEC:
4539 if (ws->l_state.horizontal_stacks > 1)
4540 ws->l_state.horizontal_stacks--;
4542 case SWM_ARG_ID_FLIPLAYOUT:
4543 ws->l_state.horizontal_flip = !ws->l_state.horizontal_flip;
4551 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
4553 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
4555 stack_master(ws, g, 1, ws->l_state.horizontal_flip);
4558 /* fullscreen view */
4560 max_stack(struct workspace *ws, struct swm_geometry *g)
4562 struct swm_geometry gg = *g;
4563 struct ws_win *w, *win = NULL, *parent = NULL;
4566 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
4571 winno = count_win(ws, 0);
4572 if (winno == 0 && count_win(ws, 1) == 0)
4575 /* Figure out which top level window should be visible. */
4576 if (ws->focus_pending)
4577 win = ws->focus_pending;
4580 else if (ws->focus_prev)
4581 win = ws->focus_prev;
4583 win = TAILQ_FIRST(&ws->winlist);
4586 parent = find_window(win->transient);
4588 DNPRINTF(SWM_D_STACK, "max_stack: win: 0x%x\n", win->id);
4590 /* maximize all top level windows */
4591 TAILQ_FOREACH(w, &ws->winlist, entry) {
4592 if (w->transient || w->iconic)
4595 if (!w->mapped && w != win)
4596 map_window(w, XCB_WINDOW_NONE);
4598 if (w->floating && !w->floatmaxed) {
4600 * retain geometry for retrieval on exit from
4603 store_float_geom(w);
4607 /* only reconfigure if necessary */
4608 if (X(w) != gg.x || Y(w) != gg.y || WIDTH(w) != gg.w ||
4609 HEIGHT(w) != gg.h) {
4611 if (bar_enabled && ws->bar_enabled){
4615 WIDTH(w) += 2 * border_width;
4616 HEIGHT(w) += 2 * border_width;
4623 /* If a parent exists, map/raise it first. */
4625 map_window(parent, XCB_WINDOW_NONE);
4627 /* Map siblings next. */
4628 TAILQ_FOREACH(w, &ws->winlist, entry)
4629 if (w != win && !w->iconic &&
4630 w->transient == parent->id) {
4631 stack_floater(w, ws->r);
4632 map_window(w, XCB_WINDOW_NONE);
4636 /* Map/raise focused window. */
4637 map_window(win, XCB_WINDOW_NONE);
4639 /* Finally, map/raise children of focus window. */
4640 TAILQ_FOREACH(w, &ws->winlist, entry)
4641 if (w->transient == win->id && !w->iconic) {
4642 stack_floater(w, ws->r);
4643 map_window(w, XCB_WINDOW_NONE);
4648 send_to_rg(struct swm_region *r, union arg *args)
4650 int ridx = args->id, i, num_screens;
4651 struct swm_region *rr = NULL;
4654 num_screens = get_screen_count();
4655 /* do nothing if we don't have more than one screen */
4656 if (!(num_screens > 1 || outputs > 1))
4659 DNPRINTF(SWM_D_FOCUS, "send_to_rg: id: %d\n", ridx);
4661 rr = TAILQ_FIRST(&r->s->rl);
4662 for (i = 0; i < ridx; ++i)
4663 rr = TAILQ_NEXT(rr, entry);
4674 region_under(struct swm_screen *s, int x, int y)
4676 struct swm_region *r = NULL;
4678 TAILQ_FOREACH(r, &s->rl, entry) {
4679 DNPRINTF(SWM_D_MISC, "region_under: ws: %d, region g: (%d,%d) "
4680 "%d x %d, coords: (%d,%d)\n", r->ws->idx, X(r), Y(r),
4681 WIDTH(r), HEIGHT(r), x, y);
4682 if (X(r) <= x && x < MAX_X(r))
4683 if (Y(r) <= y && y < MAX_Y(r))
4691 send_to_ws(struct swm_region *r, union arg *args)
4693 int wsid = args->id;
4694 struct ws_win *win = NULL;
4696 if (r && r->ws && r->ws->focus)
4701 DNPRINTF(SWM_D_MOVE, "send_to_ws: win 0x%x, ws %d\n", win->id, wsid);
4703 win_to_ws(win, wsid, 1);
4705 /* Restack and set new focus. */
4708 if (focus_mode != SWM_FOCUS_FOLLOW) {
4709 if (r->ws->focus_pending) {
4710 focus_win(r->ws->focus_pending);
4711 r->ws->focus_pending = NULL;
4713 xcb_set_input_focus(conn,
4714 XCB_INPUT_FOCUS_PARENT, r->id,
4723 win_to_ws(struct ws_win *win, int wsid, int unfocus)
4725 struct ws_win *parent;
4726 struct workspace *ws, *nws, *pws;
4727 char ws_idx_str[SWM_PROPLEN];
4729 if (wsid >= workspace_limit)
4732 if (win->ws->idx == wsid)
4736 nws = &win->s->ws[wsid];
4738 DNPRINTF(SWM_D_MOVE, "win_to_ws: win 0x%x, ws %d -> %d\n", win->id,
4741 /* Update the window's workspace property: _SWM_WS */
4742 if (snprintf(ws_idx_str, SWM_PROPLEN, "%d", nws->idx) < SWM_PROPLEN) {
4743 if (focus_mode != SWM_FOCUS_FOLLOW)
4744 ws->focus_pending = get_focus_prev(win);
4746 /* Move the parent if this is a transient window. */
4747 if (win->transient) {
4748 parent = find_window(win->transient);
4751 /* Set new focus in parent's ws if needed. */
4752 if (pws->focus == parent) {
4753 if (focus_mode != SWM_FOCUS_FOLLOW)
4754 pws->focus_pending =
4755 get_focus_prev(parent);
4757 unfocus_win(parent);
4759 if (focus_mode != SWM_FOCUS_FOLLOW) {
4760 pws->focus = pws->focus_pending;
4761 pws->focus_pending = NULL;
4765 /* Don't unmap parent if new ws is visible */
4767 unmap_window(parent);
4770 TAILQ_REMOVE(&ws->winlist, parent, entry);
4771 TAILQ_INSERT_TAIL(&nws->winlist, parent, entry);
4774 DNPRINTF(SWM_D_PROP, "win_to_ws: set "
4775 "property: _SWM_WS: %s\n", ws_idx_str);
4776 xcb_change_property(conn, XCB_PROP_MODE_REPLACE,
4777 parent->id, a_swm_ws, XCB_ATOM_STRING, 8,
4778 strlen(ws_idx_str), ws_idx_str);
4785 /* Don't unmap if new ws is visible */
4790 TAILQ_REMOVE(&ws->winlist, win, entry);
4791 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
4794 /* Set focus on new ws. */
4795 unfocus_win(nws->focus);
4798 DNPRINTF(SWM_D_PROP, "win_to_ws: set property: _SWM_WS: %s\n",
4800 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
4801 a_swm_ws, XCB_ATOM_STRING, 8, strlen(ws_idx_str),
4805 DNPRINTF(SWM_D_MOVE, "win_to_ws: done.\n");
4809 pressbutton(struct swm_region *r, union arg *args)
4811 /* suppress unused warning since var is needed */
4814 xcb_test_fake_input(conn, XCB_BUTTON_PRESS, args->id,
4815 XCB_CURRENT_TIME, XCB_WINDOW_NONE, 0, 0, 0);
4816 xcb_test_fake_input(conn, XCB_BUTTON_RELEASE, args->id,
4817 XCB_CURRENT_TIME, XCB_WINDOW_NONE, 0, 0, 0);
4821 raise_toggle(struct swm_region *r, union arg *args)
4823 /* suppress unused warning since var is needed */
4826 if (r == NULL || r->ws == NULL)
4829 r->ws->always_raise = !r->ws->always_raise;
4831 /* bring floaters back to top */
4832 if (!r->ws->always_raise)
4839 iconify(struct swm_region *r, union arg *args)
4841 /* suppress unused warning since var is needed */
4844 if (r->ws->focus == NULL)
4847 set_swm_iconic(r->ws->focus, 1);
4853 get_win_name(xcb_window_t win)
4856 xcb_get_property_cookie_t c;
4857 xcb_get_property_reply_t *r;
4859 /* First try _NET_WM_NAME for UTF-8. */
4860 c = xcb_get_property(conn, 0, win, ewmh[_NET_WM_NAME].atom,
4861 XCB_GET_PROPERTY_TYPE_ANY, 0, UINT_MAX);
4862 r = xcb_get_property_reply(conn, c, NULL);
4865 if (r->type == XCB_NONE) {
4867 /* Use WM_NAME instead; no UTF-8. */
4868 c = xcb_get_property(conn, 0, win, XCB_ATOM_WM_NAME,
4869 XCB_GET_PROPERTY_TYPE_ANY, 0, UINT_MAX);
4870 r = xcb_get_property_reply(conn, c, NULL);
4874 if (r->type == XCB_NONE) {
4880 name = strndup(xcb_get_property_value(r),
4881 xcb_get_property_value_length(r));
4890 uniconify(struct swm_region *r, union arg *args)
4897 DNPRINTF(SWM_D_MISC, "uniconify\n");
4899 if (r == NULL || r->ws == NULL)
4902 /* make sure we have anything to uniconify */
4903 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
4904 if (win->ws == NULL)
4905 continue; /* should never happen */
4914 search_resp_action = SWM_SEARCH_UNICONIFY;
4916 spawn_select(r, args, "search", &searchpid);
4918 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
4921 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
4922 if (win->ws == NULL)
4923 continue; /* should never happen */
4927 name = get_win_name(win->id);
4930 fprintf(lfile, "%s.%u\n", name, win->id);
4938 name_workspace(struct swm_region *r, union arg *args)
4942 DNPRINTF(SWM_D_MISC, "name_workspace\n");
4948 search_resp_action = SWM_SEARCH_NAME_WORKSPACE;
4950 spawn_select(r, args, "name_workspace", &searchpid);
4952 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
4955 fprintf(lfile, "%s", "");
4960 search_workspace(struct swm_region *r, union arg *args)
4963 struct workspace *ws;
4966 DNPRINTF(SWM_D_MISC, "search_workspace\n");
4972 search_resp_action = SWM_SEARCH_SEARCH_WORKSPACE;
4974 spawn_select(r, args, "search", &searchpid);
4976 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
4979 for (i = 0; i < workspace_limit; i++) {
4983 fprintf(lfile, "%d%s%s\n", ws->idx + 1,
4984 (ws->name ? ":" : ""), (ws->name ? ws->name : ""));
4991 search_win_cleanup(void)
4993 struct search_window *sw = NULL;
4995 while ((sw = TAILQ_FIRST(&search_wl)) != NULL) {
4996 xcb_destroy_window(conn, sw->indicator);
4997 TAILQ_REMOVE(&search_wl, sw, entry);
5003 search_win(struct swm_region *r, union arg *args)
5005 struct ws_win *win = NULL;
5006 struct search_window *sw = NULL;
5009 int i, width, height;
5017 XRectangle l_ibox, l_lbox;
5019 DNPRINTF(SWM_D_MISC, "search_win\n");
5022 search_resp_action = SWM_SEARCH_SEARCH_WINDOW;
5024 spawn_select(r, args, "search", &searchpid);
5026 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
5029 TAILQ_INIT(&search_wl);
5032 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
5036 sw = calloc(1, sizeof(struct search_window));
5038 warn("search_win: calloc");
5040 search_win_cleanup();
5046 snprintf(s, sizeof s, "%d", i);
5049 w = xcb_generate_id(conn);
5050 wa[0] = r->s->c[SWM_S_COLOR_FOCUS].pixel;
5051 wa[1] = r->s->c[SWM_S_COLOR_UNFOCUS].pixel;
5053 if (bar_font_legacy) {
5054 XmbTextExtents(bar_fs, s, len, &l_ibox, &l_lbox);
5055 width = l_lbox.width + 4;
5056 height = bar_fs_extents->max_logical_extent.height + 4;
5058 XftTextExtentsUtf8(display, bar_font, (FcChar8 *)s, len,
5060 width = info.width + 4;
5061 height = bar_font->height + 4;
5064 xcb_create_window(conn, XCB_COPY_FROM_PARENT, w, win->id, 0, 0,
5065 width, height, 1, XCB_WINDOW_CLASS_INPUT_OUTPUT,
5066 XCB_COPY_FROM_PARENT, XCB_CW_BACK_PIXEL |
5067 XCB_CW_BORDER_PIXEL, wa);
5069 xcb_map_window(conn, w);
5072 TAILQ_INSERT_TAIL(&search_wl, sw, entry);
5074 if (bar_font_legacy) {
5075 l_gcv.graphics_exposures = 0;
5076 l_draw = XCreateGC(display, w, 0, &l_gcv);
5078 XSetForeground(display, l_draw,
5079 r->s->c[SWM_S_COLOR_BAR].pixel);
5081 DRAWSTRING(display, w, bar_fs, l_draw, 2,
5082 (bar_fs_extents->max_logical_extent.height -
5083 l_lbox.height) / 2 - l_lbox.y, s, len);
5085 XFreeGC(display, l_draw);
5088 draw = XftDrawCreate(display, w,
5089 DefaultVisual(display, r->s->idx),
5090 DefaultColormap(display, r->s->idx));
5092 XftDrawStringUtf8(draw, &bar_font_color, bar_font, 2,
5093 (HEIGHT(r->bar) + bar_font->height) / 2 -
5094 bar_font->descent, (FcChar8 *)s, len);
5096 XftDrawDestroy(draw);
5099 DNPRINTF(SWM_D_MISC, "search_win: mapped window: 0x%x\n", w);
5101 fprintf(lfile, "%d\n", i);
5111 search_resp_uniconify(const char *resp, size_t len)
5117 DNPRINTF(SWM_D_MISC, "search_resp_uniconify: resp: %s\n", resp);
5119 TAILQ_FOREACH(win, &search_r->ws->winlist, entry) {
5122 name = get_win_name(win->id);
5125 if (asprintf(&s, "%s.%u", name, win->id) == -1) {
5130 if (strncmp(s, resp, len) == 0) {
5131 /* XXX this should be a callback to generalize */
5132 set_swm_iconic(win, 0);
5141 search_resp_name_workspace(const char *resp, size_t len)
5143 struct workspace *ws;
5145 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: resp: %s\n", resp);
5147 if (search_r->ws == NULL)
5152 free(search_r->ws->name);
5153 search_r->ws->name = NULL;
5157 ws->name = strdup(resp);
5158 if (ws->name == NULL) {
5159 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: "
5160 "strdup: %s", strerror(errno));
5167 search_resp_search_workspace(const char *resp)
5174 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: resp: %s\n", resp);
5178 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: strdup: %s",
5185 ws_idx = (int)strtonum(q, 1, workspace_limit, &errstr);
5187 DNPRINTF(SWM_D_MISC, "workspace idx is %s: %s",
5194 switchws(search_r, &a);
5198 search_resp_search_window(const char *resp)
5203 struct search_window *sw;
5205 DNPRINTF(SWM_D_MISC, "search_resp_search_window: resp: %s\n", resp);
5209 DNPRINTF(SWM_D_MISC, "search_resp_search_window: strdup: %s",
5214 idx = (int)strtonum(s, 1, INT_MAX, &errstr);
5216 DNPRINTF(SWM_D_MISC, "window idx is %s: %s",
5223 TAILQ_FOREACH(sw, &search_wl, entry)
5224 if (idx == sw->idx) {
5230 #define MAX_RESP_LEN 1024
5233 search_do_resp(void)
5239 DNPRINTF(SWM_D_MISC, "search_do_resp:\n");
5244 if ((resp = calloc(1, MAX_RESP_LEN + 1)) == NULL) {
5245 warn("search: calloc");
5249 rbytes = read(select_resp_pipe[0], resp, MAX_RESP_LEN);
5251 warn("search: read error");
5254 resp[rbytes] = '\0';
5257 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
5258 * newline, so work around that by sanitizing the resp now.
5260 resp[strcspn(resp, "\n")] = '\0';
5263 switch (search_resp_action) {
5264 case SWM_SEARCH_UNICONIFY:
5265 search_resp_uniconify(resp, len);
5267 case SWM_SEARCH_NAME_WORKSPACE:
5268 search_resp_name_workspace(resp, len);
5270 case SWM_SEARCH_SEARCH_WORKSPACE:
5271 search_resp_search_workspace(resp);
5273 case SWM_SEARCH_SEARCH_WINDOW:
5274 search_resp_search_window(resp);
5279 if (search_resp_action == SWM_SEARCH_SEARCH_WINDOW)
5280 search_win_cleanup();
5282 search_resp_action = SWM_SEARCH_NONE;
5283 close(select_resp_pipe[0]);
5290 wkill(struct swm_region *r, union arg *args)
5292 DNPRINTF(SWM_D_MISC, "wkill: id: %d\n", args->id);
5294 if (r->ws->focus == NULL)
5297 if (args->id == SWM_ARG_ID_KILLWINDOW)
5298 xcb_kill_client(conn, r->ws->focus->id);
5300 if (r->ws->focus->can_delete)
5301 client_msg(r->ws->focus, a_delete, 0);
5307 floating_toggle_win(struct ws_win *win)
5309 struct swm_region *r;
5319 /* reject floating toggles in max stack mode */
5320 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK])
5323 if (win->floating) {
5324 if (!win->floatmaxed) {
5325 /* retain position for refloat */
5326 store_float_geom(win);
5330 load_float_geom(win);
5334 ewmh_update_actions(win);
5340 floating_toggle(struct swm_region *r, union arg *args)
5342 struct ws_win *win = r->ws->focus;
5344 /* suppress unused warning since var is needed */
5350 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
5353 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
5354 _NET_WM_STATE_TOGGLE);
5358 if (win == win->ws->focus)
5365 region_containment(struct ws_win *win, struct swm_region *r, int opts)
5367 struct swm_geometry g = r->g;
5368 int rt, lt, tp, bm, bw;
5370 bw = (opts & SWM_CW_SOFTBOUNDARY) ? boundary_width : 0;
5373 * Perpendicular distance of each side of the window to the respective
5374 * side of the region boundary. Positive values indicate the side of
5375 * the window has passed beyond the region boundary.
5377 rt = opts & SWM_CW_RIGHT ? MAX_X(win) + BORDER(win) - MAX_X(r) : bw;
5378 lt = opts & SWM_CW_LEFT ? X(r) - X(win) + BORDER(win) : bw;
5379 bm = opts & SWM_CW_BOTTOM ? MAX_Y(win) + BORDER(win) - MAX_Y(r) : bw;
5380 tp = opts & SWM_CW_TOP ? Y(r) - Y(win) + BORDER(win) : bw;
5382 DNPRINTF(SWM_D_MISC, "region_containment: win 0x%x, rt: %d, lt: %d, "
5383 "bm: %d, tp: %d, SOFTBOUNDARY: %s, HARDBOUNDARY: %s\n", win->id, rt,
5384 lt, bm, tp, YESNO(opts & SWM_CW_SOFTBOUNDARY),
5385 YESNO(opts & SWM_CW_HARDBOUNDARY));
5388 * Disable containment if any of the flagged sides went beyond the
5389 * containment boundary, or if containment is disabled.
5391 if (!(opts & SWM_CW_HARDBOUNDARY || opts & SWM_CW_SOFTBOUNDARY) ||
5392 (bw != 0 && ((rt > bw) || (lt > bw) || (bm > bw) || (tp > bw)))) {
5393 /* Make sure window has at least 1 pixel in the region */
5394 g.x += 1 - WIDTH(win);
5395 g.y += 1 - HEIGHT(win);
5396 g.w += 2 * WIDTH(win) - 2;
5397 g.h += 2 * HEIGHT(win) - 2;
5400 constrain_window(win, &g, &opts);
5403 /* Move or resize a window so that flagged side(s) fit into the supplied box. */
5405 constrain_window(struct ws_win *win, struct swm_geometry *b, int *opts)
5407 DNPRINTF(SWM_D_MISC, "constrain_window: window: 0x%x, (x,y) w x h: "
5408 "(%d,%d) %d x %d, box: (x,y) w x h: (%d,%d) %d x %d, rt: %s, "
5409 "lt: %s, bt: %s, tp: %s, allow resize: %s\n", win->id, X(win),
5410 Y(win), WIDTH(win), HEIGHT(win), b->x, b->y, b->w, b->h,
5411 YESNO(*opts & SWM_CW_RIGHT), YESNO(*opts & SWM_CW_LEFT),
5412 YESNO(*opts & SWM_CW_BOTTOM), YESNO(*opts & SWM_CW_TOP),
5413 YESNO(*opts & SWM_CW_RESIZABLE));
5415 if ((*opts & SWM_CW_RIGHT) && MAX_X(win) + BORDER(win) > b->x + b->w) {
5416 if (*opts & SWM_CW_RESIZABLE)
5417 WIDTH(win) = b->x + b->w - X(win) - BORDER(win);
5419 X(win) = b->x + b->w - WIDTH(win) - BORDER(win);
5422 if ((*opts & SWM_CW_LEFT) && X(win) + BORDER(win) < b->x) {
5423 if (*opts & SWM_CW_RESIZABLE)
5424 WIDTH(win) -= b->x - X(win) - BORDER(win);
5426 X(win) = b->x - BORDER(win);
5429 if ((*opts & SWM_CW_BOTTOM) && MAX_Y(win) + BORDER(win) > b->y + b->h) {
5430 if (*opts & SWM_CW_RESIZABLE)
5431 HEIGHT(win) = b->y + b->h - Y(win) - BORDER(win);
5433 Y(win) = b->y + b->h - HEIGHT(win) - BORDER(win);
5436 if ((*opts & SWM_CW_TOP) && Y(win) + BORDER(win) < b->y) {
5437 if (*opts & SWM_CW_RESIZABLE)
5438 HEIGHT(win) -= b->y - Y(win) - BORDER(win);
5440 Y(win) = b->y - BORDER(win);
5443 if (*opts & SWM_CW_RESIZABLE) {
5446 if (HEIGHT(win) < 1)
5452 update_window(struct ws_win *win)
5457 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
5458 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
5459 XCB_CONFIG_WINDOW_BORDER_WIDTH;
5463 wc[3] = HEIGHT(win);
5464 wc[4] = BORDER(win);
5466 DNPRINTF(SWM_D_EVENT, "update_window: window: 0x%x, (x,y) w x h: "
5467 "(%d,%d) %d x %d, bordered: %s\n", win->id, wc[0], wc[1], wc[2],
5468 wc[3], YESNO(win->bordered));
5470 xcb_configure_window(conn, win->id, mask, wc);
5473 #define SWM_RESIZE_STEPS (50)
5476 resize(struct ws_win *win, union arg *args)
5478 xcb_timestamp_t timestamp = 0;
5479 struct swm_region *r = NULL;
5481 struct swm_geometry g;
5482 int top = 0, left = 0, resizing;
5484 xcb_cursor_t cursor;
5485 xcb_query_pointer_reply_t *xpr = NULL;
5486 xcb_generic_event_t *evt;
5487 xcb_motion_notify_event_t *mne;
5493 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
5496 DNPRINTF(SWM_D_EVENT, "resize: window: 0x%x, floating: %s, "
5497 "transient: 0x%x\n", win->id, YESNO(win->floating),
5500 if (!win->transient && !win->floating)
5503 /* reject resizes in max mode for floaters (transient ok) */
5504 if (win->floatmaxed)
5508 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
5515 /* It's possible for win to have been freed during focus_flush(). */
5516 if (validate_win(win)) {
5517 DNPRINTF(SWM_D_EVENT, "resize: invalid win.\n");
5522 case SWM_ARG_ID_WIDTHSHRINK:
5523 WIDTH(win) -= SWM_RESIZE_STEPS;
5526 case SWM_ARG_ID_WIDTHGROW:
5527 WIDTH(win) += SWM_RESIZE_STEPS;
5530 case SWM_ARG_ID_HEIGHTSHRINK:
5531 HEIGHT(win) -= SWM_RESIZE_STEPS;
5534 case SWM_ARG_ID_HEIGHTGROW:
5535 HEIGHT(win) += SWM_RESIZE_STEPS;
5542 region_containment(win, r, SWM_CW_ALLSIDES | SWM_CW_RESIZABLE |
5543 SWM_CW_HARDBOUNDARY);
5545 store_float_geom(win);
5549 region_containment(win, r, SWM_CW_ALLSIDES | SWM_CW_RESIZABLE |
5550 SWM_CW_SOFTBOUNDARY);
5553 /* get cursor offset from window root */
5554 xpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, win->id),
5561 if (xpr->win_x < WIDTH(win) / 2)
5564 if (xpr->win_y < HEIGHT(win) / 2)
5567 if (args->id == SWM_ARG_ID_CENTER)
5568 cursor = cursors[XC_SIZING].cid;
5570 cursor = cursors[left ? XC_TOP_LEFT_CORNER :
5571 XC_TOP_RIGHT_CORNER].cid;
5573 cursor = cursors[left ? XC_BOTTOM_LEFT_CORNER :
5574 XC_BOTTOM_RIGHT_CORNER].cid;
5576 xcb_grab_pointer(conn, 0, win->id, MOUSEMASK,
5577 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, XCB_WINDOW_NONE, cursor,
5582 while (resizing && (evt = xcb_wait_for_event(conn))) {
5583 switch (XCB_EVENT_RESPONSE_TYPE(evt)) {
5584 case XCB_BUTTON_RELEASE:
5585 DNPRINTF(SWM_D_EVENT, "resize: BUTTON_RELEASE\n");
5588 case XCB_MOTION_NOTIFY:
5589 mne = (xcb_motion_notify_event_t *)evt;
5590 /* cursor offset/delta from start of the operation */
5591 dx = mne->root_x - xpr->root_x;
5592 dy = mne->root_y - xpr->root_y;
5598 if (args->id == SWM_ARG_ID_CENTER) {
5599 if (g.h / 2 + dy < 1)
5603 HEIGHT(win) = g.h + 2 * dy;
5611 HEIGHT(win) = g.h + dy;
5618 if (args->id == SWM_ARG_ID_CENTER) {
5619 if (g.w / 2 + dx < 1)
5623 WIDTH(win) = g.w + 2 * dx;
5631 WIDTH(win) = g.w + dx;
5634 /* not free, don't sync more than 120 times / second */
5635 if ((mne->time - timestamp) > (1000 / 120) ) {
5636 timestamp = mne->time;
5637 regionize(win, mne->root_x, mne->root_y);
5638 region_containment(win, r, SWM_CW_ALLSIDES |
5639 SWM_CW_RESIZABLE | SWM_CW_HARDBOUNDARY |
5640 SWM_CW_SOFTBOUNDARY);
5648 /* It's possible for win to have been freed above. */
5649 if (validate_win(win)) {
5650 DNPRINTF(SWM_D_EVENT, "resize: invalid win.\n");
5658 region_containment(win, r, SWM_CW_ALLSIDES | SWM_CW_RESIZABLE |
5659 SWM_CW_HARDBOUNDARY | SWM_CW_SOFTBOUNDARY);
5663 store_float_geom(win);
5665 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
5667 DNPRINTF(SWM_D_EVENT, "resize: done.\n");
5671 resize_step(struct swm_region *r, union arg *args)
5673 struct ws_win *win = NULL;
5675 if (r && r->ws && r->ws->focus)
5684 /* Try to set window region based on supplied coordinates or window center. */
5686 regionize(struct ws_win *win, int x, int y)
5688 struct swm_region *r = NULL;
5690 r = region_under(win->s, x, y);
5692 r = region_under(win->s, X(win) + WIDTH(win) / 2,
5693 Y(win) + HEIGHT(win) / 2);
5695 if (r && r != win->ws->r) {
5696 win_to_ws(win, r->ws->idx, 0);
5701 #define SWM_MOVE_STEPS (50)
5704 move(struct ws_win *win, union arg *args)
5706 xcb_timestamp_t timestamp = 0;
5707 int move_stp = 0, moving;
5708 struct swm_region *r = NULL;
5709 xcb_query_pointer_reply_t *qpr = NULL;
5710 xcb_generic_event_t *evt;
5711 xcb_motion_notify_event_t *mne;
5717 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
5720 DNPRINTF(SWM_D_EVENT, "move: window: 0x%x, floating: %s, transient: "
5721 "0x%x\n", win->id, YESNO(win->floating), win->transient);
5723 /* in max_stack mode should only move transients */
5724 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !win->transient)
5729 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
5733 /* When a stacked win is moved, float it and restack. */
5734 if (!win->floating && !win->transient) {
5735 store_float_geom(win);
5736 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
5743 /* It's possible for win to have been freed during focus_flush(). */
5744 if (validate_win(win)) {
5745 DNPRINTF(SWM_D_EVENT, "move: invalid win.\n");
5751 case SWM_ARG_ID_MOVELEFT:
5752 X(win) -= (SWM_MOVE_STEPS - border_width);
5755 case SWM_ARG_ID_MOVERIGHT:
5756 X(win) += (SWM_MOVE_STEPS - border_width);
5759 case SWM_ARG_ID_MOVEUP:
5760 Y(win) -= (SWM_MOVE_STEPS - border_width);
5763 case SWM_ARG_ID_MOVEDOWN:
5764 Y(win) += (SWM_MOVE_STEPS - border_width);
5771 regionize(win, -1, -1);
5772 region_containment(win, win->ws->r, SWM_CW_ALLSIDES);
5774 store_float_geom(win);
5778 xcb_grab_pointer(conn, 0, win->id, MOUSEMASK,
5779 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC,
5780 XCB_WINDOW_NONE, cursors[XC_FLEUR].cid, XCB_CURRENT_TIME);
5782 /* get cursor offset from window root */
5783 qpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, win->id),
5786 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
5790 regionize(win, qpr->root_x, qpr->root_y);
5791 region_containment(win, win->ws->r, SWM_CW_ALLSIDES |
5792 SWM_CW_SOFTBOUNDARY);
5796 while (moving && (evt = xcb_wait_for_event(conn))) {
5797 switch (XCB_EVENT_RESPONSE_TYPE(evt)) {
5798 case XCB_BUTTON_RELEASE:
5799 DNPRINTF(SWM_D_EVENT, "move: BUTTON_RELEASE\n");
5802 case XCB_MOTION_NOTIFY:
5803 mne = (xcb_motion_notify_event_t *)evt;
5804 DNPRINTF(SWM_D_EVENT, "motion: root: 0x%x\n", mne->root);
5805 X(win) = mne->root_x - qpr->win_x - border_width;
5806 Y(win) = mne->root_y - qpr->win_y - border_width;
5808 /* not free, don't sync more than 120 times / second */
5809 if ((mne->time - timestamp) > (1000 / 120) ) {
5810 timestamp = mne->time;
5811 regionize(win, mne->root_x, mne->root_y);
5812 region_containment(win, win->ws->r,
5813 SWM_CW_ALLSIDES | SWM_CW_SOFTBOUNDARY);
5821 /* It's possible for win to have been freed above. */
5822 if (validate_win(win)) {
5823 DNPRINTF(SWM_D_EVENT, "move: invalid win.\n");
5831 region_containment(win, win->ws->r, SWM_CW_ALLSIDES |
5832 SWM_CW_SOFTBOUNDARY);
5836 store_float_geom(win);
5839 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
5840 DNPRINTF(SWM_D_EVENT, "move: done.\n");
5844 move_step(struct swm_region *r, union arg *args)
5846 struct ws_win *win = NULL;
5848 if (r && r->ws && r->ws->focus)
5853 if (!win->transient && !win->floating)
5860 /* key definitions */
5862 char name[SWM_FUNCNAME_LEN];
5863 void (*func)(struct swm_region *r, union arg *);
5865 } keyfuncs[KF_INVALID + 1] = {
5866 /* name function argument */
5867 { "bar_toggle", bar_toggle, {.id = SWM_ARG_ID_BAR_TOGGLE} },
5868 { "bar_toggle_ws", bar_toggle, {.id = SWM_ARG_ID_BAR_TOGGLE_WS} },
5869 { "button2", pressbutton, {2} },
5870 { "cycle_layout", cycle_layout, {0} },
5871 { "flip_layout", stack_config, {.id = SWM_ARG_ID_FLIPLAYOUT} },
5872 { "float_toggle", floating_toggle,{0} },
5873 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
5874 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
5875 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
5876 { "height_grow", resize_step, {.id = SWM_ARG_ID_HEIGHTGROW} },
5877 { "height_shrink", resize_step, {.id = SWM_ARG_ID_HEIGHTSHRINK} },
5878 { "iconify", iconify, {0} },
5879 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
5880 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
5881 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
5882 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
5883 { "move_down", move_step, {.id = SWM_ARG_ID_MOVEDOWN} },
5884 { "move_left", move_step, {.id = SWM_ARG_ID_MOVELEFT} },
5885 { "move_right", move_step, {.id = SWM_ARG_ID_MOVERIGHT} },
5886 { "move_up", move_step, {.id = SWM_ARG_ID_MOVEUP} },
5887 { "mvrg_1", send_to_rg, {.id = 0} },
5888 { "mvrg_2", send_to_rg, {.id = 1} },
5889 { "mvrg_3", send_to_rg, {.id = 2} },
5890 { "mvrg_4", send_to_rg, {.id = 3} },
5891 { "mvrg_5", send_to_rg, {.id = 4} },
5892 { "mvrg_6", send_to_rg, {.id = 5} },
5893 { "mvrg_7", send_to_rg, {.id = 6} },
5894 { "mvrg_8", send_to_rg, {.id = 7} },
5895 { "mvrg_9", send_to_rg, {.id = 8} },
5896 { "mvws_1", send_to_ws, {.id = 0} },
5897 { "mvws_2", send_to_ws, {.id = 1} },
5898 { "mvws_3", send_to_ws, {.id = 2} },
5899 { "mvws_4", send_to_ws, {.id = 3} },
5900 { "mvws_5", send_to_ws, {.id = 4} },
5901 { "mvws_6", send_to_ws, {.id = 5} },
5902 { "mvws_7", send_to_ws, {.id = 6} },
5903 { "mvws_8", send_to_ws, {.id = 7} },
5904 { "mvws_9", send_to_ws, {.id = 8} },
5905 { "mvws_10", send_to_ws, {.id = 9} },
5906 { "mvws_11", send_to_ws, {.id = 10} },
5907 { "mvws_12", send_to_ws, {.id = 11} },
5908 { "mvws_13", send_to_ws, {.id = 12} },
5909 { "mvws_14", send_to_ws, {.id = 13} },
5910 { "mvws_15", send_to_ws, {.id = 14} },
5911 { "mvws_16", send_to_ws, {.id = 15} },
5912 { "mvws_17", send_to_ws, {.id = 16} },
5913 { "mvws_18", send_to_ws, {.id = 17} },
5914 { "mvws_19", send_to_ws, {.id = 18} },
5915 { "mvws_20", send_to_ws, {.id = 19} },
5916 { "mvws_21", send_to_ws, {.id = 20} },
5917 { "mvws_22", send_to_ws, {.id = 21} },
5918 { "name_workspace", name_workspace, {0} },
5919 { "quit", quit, {0} },
5920 { "raise_toggle", raise_toggle, {0} },
5921 { "restart", restart, {0} },
5922 { "rg_1", focusrg, {.id = 0} },
5923 { "rg_2", focusrg, {.id = 1} },
5924 { "rg_3", focusrg, {.id = 2} },
5925 { "rg_4", focusrg, {.id = 3} },
5926 { "rg_5", focusrg, {.id = 4} },
5927 { "rg_6", focusrg, {.id = 5} },
5928 { "rg_7", focusrg, {.id = 6} },
5929 { "rg_8", focusrg, {.id = 7} },
5930 { "rg_9", focusrg, {.id = 8} },
5931 { "rg_next", cyclerg, {.id = SWM_ARG_ID_CYCLERG_UP} },
5932 { "rg_prev", cyclerg, {.id = SWM_ARG_ID_CYCLERG_DOWN} },
5933 { "screen_next", cyclerg, {.id = SWM_ARG_ID_CYCLERG_UP} },
5934 { "screen_prev", cyclerg, {.id = SWM_ARG_ID_CYCLERG_DOWN} },
5935 { "search_win", search_win, {0} },
5936 { "search_workspace", search_workspace, {0} },
5937 { "spawn_custom", NULL, {0} },
5938 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
5939 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
5940 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
5941 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
5942 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
5943 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
5944 { "uniconify", uniconify, {0} },
5945 { "version", version, {0} },
5946 { "width_grow", resize_step, {.id = SWM_ARG_ID_WIDTHGROW} },
5947 { "width_shrink", resize_step, {.id = SWM_ARG_ID_WIDTHSHRINK} },
5948 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
5949 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
5950 { "ws_1", switchws, {.id = 0} },
5951 { "ws_2", switchws, {.id = 1} },
5952 { "ws_3", switchws, {.id = 2} },
5953 { "ws_4", switchws, {.id = 3} },
5954 { "ws_5", switchws, {.id = 4} },
5955 { "ws_6", switchws, {.id = 5} },
5956 { "ws_7", switchws, {.id = 6} },
5957 { "ws_8", switchws, {.id = 7} },
5958 { "ws_9", switchws, {.id = 8} },
5959 { "ws_10", switchws, {.id = 9} },
5960 { "ws_11", switchws, {.id = 10} },
5961 { "ws_12", switchws, {.id = 11} },
5962 { "ws_13", switchws, {.id = 12} },
5963 { "ws_14", switchws, {.id = 13} },
5964 { "ws_15", switchws, {.id = 14} },
5965 { "ws_16", switchws, {.id = 15} },
5966 { "ws_17", switchws, {.id = 16} },
5967 { "ws_18", switchws, {.id = 17} },
5968 { "ws_19", switchws, {.id = 18} },
5969 { "ws_20", switchws, {.id = 19} },
5970 { "ws_21", switchws, {.id = 20} },
5971 { "ws_22", switchws, {.id = 21} },
5972 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
5973 { "ws_next_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP_ALL} },
5974 { "ws_next_move", cyclews, {.id = SWM_ARG_ID_CYCLEWS_MOVE_UP} },
5975 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
5976 { "ws_prev_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN_ALL} },
5977 { "ws_prev_move", cyclews, {.id = SWM_ARG_ID_CYCLEWS_MOVE_DOWN} },
5978 { "ws_prior", priorws, {0} },
5979 { "dumpwins", dumpwins, {0} }, /* MUST BE LAST */
5980 { "invalid key func", NULL, {0} },
5984 key_cmp(struct key *kp1, struct key *kp2)
5986 if (kp1->keysym < kp2->keysym)
5988 if (kp1->keysym > kp2->keysym)
5991 if (kp1->mod < kp2->mod)
5993 if (kp1->mod > kp2->mod)
6000 enum { client_click, root_click };
6002 unsigned int action;
6004 unsigned int button;
6005 void (*func)(struct ws_win *, union arg *);
6008 #define MODKEY_SHIFT MODKEY | XCB_MOD_MASK_SHIFT
6009 /* action key mouse button func args */
6010 { client_click, MODKEY, XCB_BUTTON_INDEX_3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
6011 { client_click, MODKEY_SHIFT, XCB_BUTTON_INDEX_3, resize, {.id = SWM_ARG_ID_CENTER} },
6012 { client_click, MODKEY, XCB_BUTTON_INDEX_1, move, {0} },
6017 update_modkey(unsigned int mod)
6023 RB_FOREACH(kp, key_tree, &keys)
6024 if (kp->mod & XCB_MOD_MASK_SHIFT)
6025 kp->mod = mod | XCB_MOD_MASK_SHIFT;
6029 for (i = 0; i < LENGTH(buttons); i++)
6030 if (buttons[i].mask & XCB_MOD_MASK_SHIFT)
6031 buttons[i].mask = mod | XCB_MOD_MASK_SHIFT;
6033 buttons[i].mask = mod;
6037 spawn_expand(struct swm_region *r, union arg *args, const char *spawn_name,
6040 struct spawn_prog *prog = NULL;
6042 char *ap, **real_args;
6044 /* suppress unused warning since var is needed */
6047 DNPRINTF(SWM_D_SPAWN, "spawn_expand: %s\n", spawn_name);
6050 TAILQ_FOREACH(prog, &spawns, entry) {
6051 if (!strcasecmp(spawn_name, prog->name))
6055 warnx("spawn_custom: program %s not found", spawn_name);
6059 /* make room for expanded args */
6060 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
6061 err(1, "spawn_custom: calloc real_args");
6063 /* expand spawn_args into real_args */
6064 for (i = c = 0; i < prog->argc; i++) {
6066 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg: %s\n", ap);
6067 if (!strcasecmp(ap, "$bar_border")) {
6069 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
6071 err(1, "spawn_custom border color");
6072 } else if (!strcasecmp(ap, "$bar_color")) {
6074 strdup(r->s->c[SWM_S_COLOR_BAR].name))
6076 err(1, "spawn_custom bar color");
6077 } else if (!strcasecmp(ap, "$bar_font")) {
6078 if ((real_args[c] = strdup(bar_fonts))
6080 err(1, "spawn_custom bar fonts");
6081 } else if (!strcasecmp(ap, "$bar_font_color")) {
6083 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
6085 err(1, "spawn_custom color font");
6086 } else if (!strcasecmp(ap, "$color_focus")) {
6088 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
6090 err(1, "spawn_custom color focus");
6091 } else if (!strcasecmp(ap, "$color_unfocus")) {
6093 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
6095 err(1, "spawn_custom color unfocus");
6096 } else if (!strcasecmp(ap, "$region_index")) {
6097 if (asprintf(&real_args[c], "%d",
6098 get_region_index(r) + 1) < 1)
6099 err(1, "spawn_custom region index");
6100 } else if (!strcasecmp(ap, "$workspace_index")) {
6101 if (asprintf(&real_args[c], "%d", r->ws->idx + 1) < 1)
6102 err(1, "spawn_custom workspace index");
6103 } else if (!strcasecmp(ap, "$dmenu_bottom")) {
6106 if ((real_args[c] = strdup("-b")) == NULL)
6107 err(1, "spawn_custom workspace index");
6109 /* no match --> copy as is */
6110 if ((real_args[c] = strdup(ap)) == NULL)
6111 err(1, "spawn_custom strdup(ap)");
6113 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg: %s\n",
6119 DNPRINTF(SWM_D_SPAWN, "spawn_custom: result: ");
6120 for (i = 0; i < c; ++i)
6121 DPRINTF("\"%s\" ", real_args[i]);
6124 *ret_args = real_args;
6129 spawn_custom(struct swm_region *r, union arg *args, const char *spawn_name)
6135 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
6139 spawn(r->ws->idx, &a, 1);
6141 for (i = 0; i < spawn_argc; i++)
6147 spawn_select(struct swm_region *r, union arg *args, const char *spawn_name,
6154 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
6158 if (pipe(select_list_pipe) == -1)
6159 err(1, "pipe error");
6160 if (pipe(select_resp_pipe) == -1)
6161 err(1, "pipe error");
6163 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
6164 err(1, "could not disable SIGPIPE");
6165 switch (*pid = fork()) {
6167 err(1, "cannot fork");
6170 if (dup2(select_list_pipe[0], STDIN_FILENO) == -1)
6172 if (dup2(select_resp_pipe[1], STDOUT_FILENO) == -1)
6174 close(select_list_pipe[1]);
6175 close(select_resp_pipe[0]);
6176 spawn(r->ws->idx, &a, 0);
6178 default: /* parent */
6179 close(select_list_pipe[0]);
6180 close(select_resp_pipe[1]);
6184 for (i = 0; i < spawn_argc; i++)
6190 spawn_insert(const char *name, const char *args, int flags)
6192 char *arg, *cp, *ptr;
6193 struct spawn_prog *sp;
6195 DNPRINTF(SWM_D_SPAWN, "spawn_insert: %s\n", name);
6197 if ((sp = calloc(1, sizeof *sp)) == NULL)
6198 err(1, "spawn_insert: calloc");
6199 if ((sp->name = strdup(name)) == NULL)
6200 err(1, "spawn_insert: strdup");
6202 /* convert the arguments to an argument list */
6203 if ((ptr = cp = strdup(args)) == NULL)
6204 err(1, "spawn_insert: strdup");
6205 while ((arg = strsep(&ptr, " \t")) != NULL) {
6206 /* empty field; skip it */
6211 if ((sp->argv = realloc(sp->argv, sp->argc *
6212 sizeof *sp->argv)) == NULL)
6213 err(1, "spawn_insert: realloc");
6214 if ((sp->argv[sp->argc - 1] = strdup(arg)) == NULL)
6215 err(1, "spawn_insert: strdup");
6221 TAILQ_INSERT_TAIL(&spawns, sp, entry);
6222 DNPRINTF(SWM_D_SPAWN, "spawn_insert: leave\n");
6226 spawn_remove(struct spawn_prog *sp)
6230 DNPRINTF(SWM_D_SPAWN, "spawn_remove: %s\n", sp->name);
6232 TAILQ_REMOVE(&spawns, sp, entry);
6233 for (i = 0; i < sp->argc; i++)
6239 DNPRINTF(SWM_D_SPAWN, "spawn_remove: leave\n");
6243 spawn_find(const char *name)
6245 struct spawn_prog *sp;
6247 TAILQ_FOREACH(sp, &spawns, entry)
6248 if (!strcasecmp(sp->name, name))
6255 setspawn(const char *name, const char *args, int flags)
6257 struct spawn_prog *sp;
6259 DNPRINTF(SWM_D_SPAWN, "setspawn: %s\n", name);
6264 /* Remove any old spawn under the same name. */
6265 if ((sp = spawn_find(name)) != NULL)
6269 spawn_insert(name, args, flags);
6271 warnx("error: setspawn: cannot find program: %s", name);
6273 DNPRINTF(SWM_D_SPAWN, "setspawn: leave\n");
6277 setconfspawn(char *selector, char *value, int flags)
6281 args = expand_tilde(value);
6283 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, args);
6285 setspawn(selector, args, flags);
6288 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done.\n");
6293 validate_spawns(void)
6295 struct spawn_prog *sp;
6296 char which[PATH_MAX];
6301 RB_FOREACH(kp, key_tree, &keys) {
6302 if (kp->funcid != KF_SPAWN_CUSTOM)
6306 sp = spawn_find(kp->spawn_name);
6307 if (sp == NULL || sp->flags & SWM_SPAWN_OPTIONAL)
6310 /* verify we have the goods */
6311 snprintf(which, sizeof which, "which %s", sp->argv[0]);
6312 DNPRINTF(SWM_D_CONF, "validate_spawns: which %s\n",
6314 for (i = strlen("which "); i < strlen(which); i++)
6315 if (which[i] == ' ') {
6319 if (system(which) != 0)
6320 add_startup_exception("could not find %s",
6321 &which[strlen("which ")]);
6328 setconfspawn("lock", "xlock", 0);
6330 setconfspawn("term", "xterm", 0);
6331 setconfspawn("spawn_term", "xterm", 0);
6333 setconfspawn("menu", "dmenu_run"
6337 " -nf $bar_font_color"
6339 " -sf $bar_color", 0);
6341 setconfspawn("search", "dmenu"
6346 " -nf $bar_font_color"
6348 " -sf $bar_color", 0);
6350 setconfspawn("name_workspace", "dmenu"
6355 " -nf $bar_font_color"
6357 " -sf $bar_color", 0);
6359 /* These are not verified for existence, even with a binding set. */
6360 setconfspawn("screenshot_all", "screenshot.sh full", SWM_SPAWN_OPTIONAL);
6361 setconfspawn("screenshot_wind", "screenshot.sh window", SWM_SPAWN_OPTIONAL);
6362 setconfspawn("initscr", "initscreen.sh", SWM_SPAWN_OPTIONAL);
6366 #define SWM_MODNAME_SIZE 32
6367 #define SWM_KEY_WS "\n+ \t"
6369 parsekeys(char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
6373 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
6374 if (mod == NULL || ks == NULL) {
6375 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
6378 if (keystr == NULL || strlen(keystr) == 0) {
6379 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
6383 *ks = XCB_NO_SYMBOL;
6385 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
6386 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
6388 cp += (long)strspn(cp, SWM_KEY_WS);
6389 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
6391 else if (!strncasecmp(name, "Mod1", SWM_MODNAME_SIZE))
6392 *mod |= XCB_MOD_MASK_1;
6393 else if (!strncasecmp(name, "Mod2", SWM_MODNAME_SIZE))
6394 *mod += XCB_MOD_MASK_2;
6395 else if (!strncmp(name, "Mod3", SWM_MODNAME_SIZE))
6396 *mod |= XCB_MOD_MASK_3;
6397 else if (!strncmp(name, "Mod4", SWM_MODNAME_SIZE))
6398 *mod |= XCB_MOD_MASK_4;
6399 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
6400 *mod |= XCB_MOD_MASK_SHIFT;
6401 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
6402 *mod |= XCB_MOD_MASK_CONTROL;
6404 *ks = XStringToKeysym(name);
6405 XConvertCase(*ks, ks, &uks);
6406 if (ks == XCB_NO_SYMBOL) {
6408 "parsekeys: invalid key %s\n",
6414 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
6419 strdupsafe(const char *str)
6424 return (strdup(str));
6428 key_insert(unsigned int mod, KeySym ks, enum keyfuncid kfid,
6429 const char *spawn_name)
6433 DNPRINTF(SWM_D_KEY, "key_insert: enter %s [%s]\n",
6434 keyfuncs[kfid].name, spawn_name);
6436 if ((kp = malloc(sizeof *kp)) == NULL)
6437 err(1, "key_insert: malloc");
6442 kp->spawn_name = strdupsafe(spawn_name);
6443 RB_INSERT(key_tree, &keys, kp);
6445 DNPRINTF(SWM_D_KEY, "key_insert: leave\n");
6449 key_lookup(unsigned int mod, KeySym ks)
6456 return (RB_FIND(key_tree, &keys, &kp));
6460 key_remove(struct key *kp)
6462 DNPRINTF(SWM_D_KEY, "key_remove: %s\n", keyfuncs[kp->funcid].name);
6464 RB_REMOVE(key_tree, &keys, kp);
6465 free(kp->spawn_name);
6468 DNPRINTF(SWM_D_KEY, "key_remove: leave\n");
6472 key_replace(struct key *kp, unsigned int mod, KeySym ks, enum keyfuncid kfid,
6473 const char *spawn_name)
6475 DNPRINTF(SWM_D_KEY, "key_replace: %s [%s]\n", keyfuncs[kp->funcid].name,
6479 key_insert(mod, ks, kfid, spawn_name);
6481 DNPRINTF(SWM_D_KEY, "key_replace: leave\n");
6485 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid,
6486 const char *spawn_name)
6490 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
6491 keyfuncs[kfid].name, spawn_name);
6493 if ((kp = key_lookup(mod, ks)) != NULL) {
6494 if (kfid == KF_INVALID)
6497 key_replace(kp, mod, ks, kfid, spawn_name);
6498 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
6501 if (kfid == KF_INVALID) {
6502 warnx("bind: Key combination already unbound.");
6503 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
6507 key_insert(mod, ks, kfid, spawn_name);
6508 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
6512 setconfbinding(char *selector, char *value, int flags)
6514 enum keyfuncid kfid;
6517 struct spawn_prog *sp;
6519 /* suppress unused warning since var is needed */
6522 DNPRINTF(SWM_D_KEY, "setconfbinding: enter\n");
6523 if (selector == NULL) {
6524 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
6525 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
6527 setkeybinding(mod, ks, kfid, NULL);
6532 /* search by key function name */
6533 for (kfid = 0; kfid < KF_INVALID; (kfid)++) {
6534 if (strncasecmp(selector, keyfuncs[kfid].name,
6535 SWM_FUNCNAME_LEN) == 0) {
6536 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match "
6537 "keyfunc\n", selector);
6538 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
6539 setkeybinding(mod, ks, kfid, NULL);
6545 /* search by custom spawn name */
6546 if ((sp = spawn_find(selector)) != NULL) {
6547 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match "
6548 "spawn\n", selector);
6549 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
6550 setkeybinding(mod, ks, KF_SPAWN_CUSTOM,
6556 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
6563 #define MODKEY_SHIFT MODKEY | XCB_MOD_MASK_SHIFT
6564 setkeybinding(MODKEY, XK_b, KF_BAR_TOGGLE, NULL);
6565 setkeybinding(MODKEY_SHIFT, XK_b, KF_BAR_TOGGLE_WS,NULL);
6566 setkeybinding(MODKEY, XK_v, KF_BUTTON2, NULL);
6567 setkeybinding(MODKEY, XK_space, KF_CYCLE_LAYOUT,NULL);
6568 setkeybinding(MODKEY_SHIFT, XK_backslash, KF_FLIP_LAYOUT, NULL);
6569 setkeybinding(MODKEY, XK_t, KF_FLOAT_TOGGLE,NULL);
6570 setkeybinding(MODKEY, XK_m, KF_FOCUS_MAIN, NULL);
6571 setkeybinding(MODKEY, XK_j, KF_FOCUS_NEXT, NULL);
6572 setkeybinding(MODKEY, XK_Tab, KF_FOCUS_NEXT, NULL);
6573 setkeybinding(MODKEY, XK_k, KF_FOCUS_PREV, NULL);
6574 setkeybinding(MODKEY_SHIFT, XK_Tab, KF_FOCUS_PREV, NULL);
6575 setkeybinding(MODKEY_SHIFT, XK_equal, KF_HEIGHT_GROW,NULL);
6576 setkeybinding(MODKEY_SHIFT, XK_minus, KF_HEIGHT_SHRINK,NULL);
6577 setkeybinding(MODKEY, XK_w, KF_ICONIFY, NULL);
6578 setkeybinding(MODKEY, XK_h, KF_MASTER_SHRINK, NULL);
6579 setkeybinding(MODKEY, XK_l, KF_MASTER_GROW, NULL);
6580 setkeybinding(MODKEY, XK_comma, KF_MASTER_ADD, NULL);
6581 setkeybinding(MODKEY, XK_period, KF_MASTER_DEL, NULL);
6582 setkeybinding(MODKEY_SHIFT, XK_bracketright,KF_MOVE_DOWN,NULL);
6583 setkeybinding(MODKEY, XK_bracketleft, KF_MOVE_LEFT,NULL);
6584 setkeybinding(MODKEY, XK_bracketright,KF_MOVE_RIGHT,NULL);
6585 setkeybinding(MODKEY_SHIFT, XK_bracketleft, KF_MOVE_UP, NULL);
6586 setkeybinding(MODKEY_SHIFT, XK_KP_End, KF_MVRG_1, NULL);
6587 setkeybinding(MODKEY_SHIFT, XK_KP_Down, KF_MVRG_2, NULL);
6588 setkeybinding(MODKEY_SHIFT, XK_KP_Next, KF_MVRG_3, NULL);
6589 setkeybinding(MODKEY_SHIFT, XK_KP_Left, KF_MVRG_4, NULL);
6590 setkeybinding(MODKEY_SHIFT, XK_KP_Begin, KF_MVRG_5, NULL);
6591 setkeybinding(MODKEY_SHIFT, XK_KP_Right, KF_MVRG_6, NULL);
6592 setkeybinding(MODKEY_SHIFT, XK_KP_Home, KF_MVRG_7, NULL);
6593 setkeybinding(MODKEY_SHIFT, XK_KP_Up, KF_MVRG_8, NULL);
6594 setkeybinding(MODKEY_SHIFT, XK_KP_Prior, KF_MVRG_9, NULL);
6595 setkeybinding(MODKEY_SHIFT, XK_1, KF_MVWS_1, NULL);
6596 setkeybinding(MODKEY_SHIFT, XK_2, KF_MVWS_2, NULL);
6597 setkeybinding(MODKEY_SHIFT, XK_3, KF_MVWS_3, NULL);
6598 setkeybinding(MODKEY_SHIFT, XK_4, KF_MVWS_4, NULL);
6599 setkeybinding(MODKEY_SHIFT, XK_5, KF_MVWS_5, NULL);
6600 setkeybinding(MODKEY_SHIFT, XK_6, KF_MVWS_6, NULL);
6601 setkeybinding(MODKEY_SHIFT, XK_7, KF_MVWS_7, NULL);
6602 setkeybinding(MODKEY_SHIFT, XK_8, KF_MVWS_8, NULL);
6603 setkeybinding(MODKEY_SHIFT, XK_9, KF_MVWS_9, NULL);
6604 setkeybinding(MODKEY_SHIFT, XK_0, KF_MVWS_10, NULL);
6605 setkeybinding(MODKEY_SHIFT, XK_F1, KF_MVWS_11, NULL);
6606 setkeybinding(MODKEY_SHIFT, XK_F2, KF_MVWS_12, NULL);
6607 setkeybinding(MODKEY_SHIFT, XK_F3, KF_MVWS_13, NULL);
6608 setkeybinding(MODKEY_SHIFT, XK_F4, KF_MVWS_14, NULL);
6609 setkeybinding(MODKEY_SHIFT, XK_F5, KF_MVWS_15, NULL);
6610 setkeybinding(MODKEY_SHIFT, XK_F6, KF_MVWS_16, NULL);
6611 setkeybinding(MODKEY_SHIFT, XK_F7, KF_MVWS_17, NULL);
6612 setkeybinding(MODKEY_SHIFT, XK_F8, KF_MVWS_18, NULL);
6613 setkeybinding(MODKEY_SHIFT, XK_F9, KF_MVWS_19, NULL);
6614 setkeybinding(MODKEY_SHIFT, XK_F10, KF_MVWS_20, NULL);
6615 setkeybinding(MODKEY_SHIFT, XK_F11, KF_MVWS_21, NULL);
6616 setkeybinding(MODKEY_SHIFT, XK_F12, KF_MVWS_22, NULL);
6617 setkeybinding(MODKEY_SHIFT, XK_slash, KF_NAME_WORKSPACE,NULL);
6618 setkeybinding(MODKEY_SHIFT, XK_q, KF_QUIT, NULL);
6619 setkeybinding(MODKEY_SHIFT, XK_r, KF_RAISE_TOGGLE,NULL);
6620 setkeybinding(MODKEY, XK_q, KF_RESTART, NULL);
6621 setkeybinding(MODKEY, XK_KP_End, KF_RG_1, NULL);
6622 setkeybinding(MODKEY, XK_KP_Down, KF_RG_2, NULL);
6623 setkeybinding(MODKEY, XK_KP_Next, KF_RG_3, NULL);
6624 setkeybinding(MODKEY, XK_KP_Left, KF_RG_4, NULL);
6625 setkeybinding(MODKEY, XK_KP_Begin, KF_RG_5, NULL);
6626 setkeybinding(MODKEY, XK_KP_Right, KF_RG_6, NULL);
6627 setkeybinding(MODKEY, XK_KP_Home, KF_RG_7, NULL);
6628 setkeybinding(MODKEY, XK_KP_Up, KF_RG_8, NULL);
6629 setkeybinding(MODKEY, XK_KP_Prior, KF_RG_9, NULL);
6630 setkeybinding(MODKEY_SHIFT, XK_Right, KF_RG_NEXT, NULL);
6631 setkeybinding(MODKEY_SHIFT, XK_Left, KF_RG_PREV, NULL);
6632 setkeybinding(MODKEY, XK_f, KF_SEARCH_WIN, NULL);
6633 setkeybinding(MODKEY, XK_slash, KF_SEARCH_WORKSPACE,NULL);
6634 setkeybinding(MODKEY_SHIFT, XK_i, KF_SPAWN_CUSTOM,"initscr");
6635 setkeybinding(MODKEY_SHIFT, XK_Delete, KF_SPAWN_CUSTOM,"lock");
6636 setkeybinding(MODKEY, XK_p, KF_SPAWN_CUSTOM,"menu");
6637 setkeybinding(MODKEY, XK_s, KF_SPAWN_CUSTOM,"screenshot_all");
6638 setkeybinding(MODKEY_SHIFT, XK_s, KF_SPAWN_CUSTOM,"screenshot_wind");
6639 setkeybinding(MODKEY_SHIFT, XK_Return, KF_SPAWN_CUSTOM,"term");
6640 setkeybinding(MODKEY_SHIFT, XK_comma, KF_STACK_INC, NULL);
6641 setkeybinding(MODKEY_SHIFT, XK_period, KF_STACK_DEC, NULL);
6642 setkeybinding(MODKEY_SHIFT, XK_space, KF_STACK_RESET, NULL);
6643 setkeybinding(MODKEY, XK_Return, KF_SWAP_MAIN, NULL);
6644 setkeybinding(MODKEY_SHIFT, XK_j, KF_SWAP_NEXT, NULL);
6645 setkeybinding(MODKEY_SHIFT, XK_k, KF_SWAP_PREV, NULL);
6646 setkeybinding(MODKEY_SHIFT, XK_w, KF_UNICONIFY, NULL);
6647 setkeybinding(MODKEY_SHIFT, XK_v, KF_VERSION, NULL);
6648 setkeybinding(MODKEY, XK_equal, KF_WIDTH_GROW, NULL);
6649 setkeybinding(MODKEY, XK_minus, KF_WIDTH_SHRINK,NULL);
6650 setkeybinding(MODKEY, XK_x, KF_WIND_DEL, NULL);
6651 setkeybinding(MODKEY_SHIFT, XK_x, KF_WIND_KILL, NULL);
6652 setkeybinding(MODKEY, XK_1, KF_WS_1, NULL);
6653 setkeybinding(MODKEY, XK_2, KF_WS_2, NULL);
6654 setkeybinding(MODKEY, XK_3, KF_WS_3, NULL);
6655 setkeybinding(MODKEY, XK_4, KF_WS_4, NULL);
6656 setkeybinding(MODKEY, XK_5, KF_WS_5, NULL);
6657 setkeybinding(MODKEY, XK_6, KF_WS_6, NULL);
6658 setkeybinding(MODKEY, XK_7, KF_WS_7, NULL);
6659 setkeybinding(MODKEY, XK_8, KF_WS_8, NULL);
6660 setkeybinding(MODKEY, XK_9, KF_WS_9, NULL);
6661 setkeybinding(MODKEY, XK_0, KF_WS_10, NULL);
6662 setkeybinding(MODKEY, XK_F1, KF_WS_11, NULL);
6663 setkeybinding(MODKEY, XK_F2, KF_WS_12, NULL);
6664 setkeybinding(MODKEY, XK_F3, KF_WS_13, NULL);
6665 setkeybinding(MODKEY, XK_F4, KF_WS_14, NULL);
6666 setkeybinding(MODKEY, XK_F5, KF_WS_15, NULL);
6667 setkeybinding(MODKEY, XK_F6, KF_WS_16, NULL);
6668 setkeybinding(MODKEY, XK_F7, KF_WS_17, NULL);
6669 setkeybinding(MODKEY, XK_F8, KF_WS_18, NULL);
6670 setkeybinding(MODKEY, XK_F9, KF_WS_19, NULL);
6671 setkeybinding(MODKEY, XK_F10, KF_WS_20, NULL);
6672 setkeybinding(MODKEY, XK_F11, KF_WS_21, NULL);
6673 setkeybinding(MODKEY, XK_F12, KF_WS_22, NULL);
6674 setkeybinding(MODKEY, XK_Right, KF_WS_NEXT, NULL);
6675 setkeybinding(MODKEY, XK_Left, KF_WS_PREV, NULL);
6676 setkeybinding(MODKEY, XK_Up, KF_WS_NEXT_ALL, NULL);
6677 setkeybinding(MODKEY, XK_Down, KF_WS_PREV_ALL, NULL);
6678 setkeybinding(MODKEY_SHIFT, XK_Up, KF_WS_NEXT_MOVE,NULL);
6679 setkeybinding(MODKEY_SHIFT, XK_Down, KF_WS_PREV_MOVE,NULL);
6680 setkeybinding(MODKEY, XK_a, KF_WS_PRIOR, NULL);
6682 setkeybinding(MODKEY_SHIFT, XK_d, KF_DUMPWINS, NULL);
6692 while (RB_EMPTY(&keys) == 0) {
6693 kp = RB_ROOT(&keys);
6699 setkeymapping(char *selector, char *value, int flags)
6701 char *keymapping_file;
6703 /* suppress unused warnings since vars are needed */
6707 DNPRINTF(SWM_D_KEY, "setkeymapping: enter\n");
6709 keymapping_file = expand_tilde(value);
6712 /* load new key bindings; if it fails, revert to default bindings */
6713 if (conf_load(keymapping_file, SWM_CONF_KEYMAPPING)) {
6718 free(keymapping_file);
6720 DNPRINTF(SWM_D_KEY, "setkeymapping: leave\n");
6725 updatenumlockmask(void)
6728 xcb_get_modifier_mapping_reply_t *modmap_r;
6729 xcb_keycode_t *modmap, kc, *keycode;
6733 modmap_r = xcb_get_modifier_mapping_reply(conn,
6734 xcb_get_modifier_mapping(conn),
6737 modmap = xcb_get_modifier_mapping_keycodes(modmap_r);
6738 for (i = 0; i < 8; i++) {
6739 for (j = 0; j < modmap_r->keycodes_per_modifier; j++) {
6740 kc = modmap[i * modmap_r->keycodes_per_modifier
6742 keycode = xcb_key_symbols_get_keycode(syms,
6745 numlockmask = (1 << i);
6751 DNPRINTF(SWM_D_MISC, "updatenumlockmask: %d\n", numlockmask);
6758 int num_screens, k, j;
6759 unsigned int modifiers[4];
6760 xcb_keycode_t *code;
6762 DNPRINTF(SWM_D_MISC, "grabkeys\n");
6763 updatenumlockmask();
6766 modifiers[1] = numlockmask;
6767 modifiers[2] = XCB_MOD_MASK_LOCK;
6768 modifiers[3] = numlockmask | XCB_MOD_MASK_LOCK;
6770 num_screens = get_screen_count();
6771 for (k = 0; k < num_screens; k++) {
6772 if (TAILQ_EMPTY(&screens[k].rl))
6774 xcb_ungrab_key(conn, XCB_GRAB_ANY, screens[k].root,
6776 RB_FOREACH(kp, key_tree, &keys) {
6777 /* Skip unused ws binds. */
6778 if ((int)kp->funcid > KF_WS_1 + workspace_limit - 1 &&
6779 kp->funcid <= KF_WS_22)
6782 /* Skip unused mvws binds. */
6783 if ((int)kp->funcid > KF_MVWS_1 + workspace_limit - 1 &&
6784 kp->funcid <= KF_MVWS_22)
6787 if ((code = xcb_key_symbols_get_keycode(syms,
6789 for (j = 0; j < LENGTH(modifiers); j++)
6790 xcb_grab_key(conn, 1,
6792 kp->mod | modifiers[j],
6793 *code, XCB_GRAB_MODE_SYNC,
6794 XCB_GRAB_MODE_SYNC);
6802 grabbuttons(struct ws_win *win)
6804 unsigned int modifiers[4];
6807 DNPRINTF(SWM_D_MOUSE, "grabbuttons: win 0x%x\n", win->id);
6808 updatenumlockmask();
6811 modifiers[1] = numlockmask;
6812 modifiers[2] = XCB_MOD_MASK_LOCK;
6813 modifiers[3] = numlockmask | XCB_MOD_MASK_LOCK;
6815 for (i = 0; i < LENGTH(buttons); i++)
6816 if (buttons[i].action == client_click)
6817 for (j = 0; j < LENGTH(modifiers); ++j)
6818 xcb_grab_button(conn, 0, win->id, BUTTONMASK,
6819 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC,
6820 XCB_WINDOW_NONE, XCB_CURSOR_NONE,
6821 buttons[i].button, buttons[i].mask |
6825 const char *quirkname[] = {
6826 "NONE", /* config string for "no value" */
6834 "FOCUSONMAP_SINGLE",
6837 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
6838 #define SWM_Q_WS "\n|+ \t"
6840 parsequirks(char *qstr, unsigned long *quirk)
6850 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
6852 cp += (long)strspn(cp, SWM_Q_WS);
6853 for (i = 0; i < LENGTH(quirkname); i++) {
6854 if (!strncasecmp(name, quirkname[i], SWM_QUIRK_LEN)) {
6855 DNPRINTF(SWM_D_QUIRK,
6856 "parsequirks: %s\n", name);
6861 *quirk |= 1 << (i-1);
6865 if (i >= LENGTH(quirkname)) {
6866 DNPRINTF(SWM_D_QUIRK,
6867 "parsequirks: invalid quirk [%s]\n", name);
6875 quirk_insert(const char *class, const char *name, unsigned long quirk)
6879 DNPRINTF(SWM_D_QUIRK, "quirk_insert: %s:%s [%lu]\n", class, name,
6882 if ((qp = malloc(sizeof *qp)) == NULL)
6883 err(1, "quirk_insert: malloc");
6884 if ((qp->class = strdup(class)) == NULL)
6885 err(1, "quirk_insert: strdup");
6886 if ((qp->name = strdup(name)) == NULL)
6887 err(1, "quirk_insert: strdup");
6890 TAILQ_INSERT_TAIL(&quirks, qp, entry);
6892 DNPRINTF(SWM_D_QUIRK, "quirk_insert: leave\n");
6896 quirk_remove(struct quirk *qp)
6898 DNPRINTF(SWM_D_QUIRK, "quirk_remove: %s:%s [%lu]\n", qp->class,
6899 qp->name, qp->quirk);
6901 TAILQ_REMOVE(&quirks, qp, entry);
6906 DNPRINTF(SWM_D_QUIRK, "quirk_remove: leave\n");
6910 quirk_replace(struct quirk *qp, const char *class, const char *name,
6911 unsigned long quirk)
6913 DNPRINTF(SWM_D_QUIRK, "quirk_replace: %s:%s [%lu]\n", qp->class,
6914 qp->name, qp->quirk);
6917 quirk_insert(class, name, quirk);
6919 DNPRINTF(SWM_D_QUIRK, "quirk_replace: leave\n");
6923 setquirk(const char *class, const char *name, unsigned long quirk)
6927 DNPRINTF(SWM_D_QUIRK, "setquirk: enter %s:%s [%lu]\n", class, name,
6930 /* Remove/replace existing quirk. */
6931 TAILQ_FOREACH(qp, &quirks, entry) {
6932 if (!strcmp(qp->class, class) && !strcmp(qp->name, name)) {
6936 quirk_replace(qp, class, name, quirk);
6937 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
6942 /* Only insert if quirk is not NONE. */
6944 quirk_insert(class, name, quirk);
6946 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
6950 setconfquirk(char *selector, char *value, int flags)
6952 char *cp, *class, *name;
6956 /* suppress unused warning since var is needed */
6959 if (selector == NULL)
6961 if ((cp = strchr(selector, ':')) == NULL)
6966 if ((retval = parsequirks(value, &qrks)) == 0)
6967 setquirk(class, name, qrks);
6974 setquirk("MPlayer", "xv", SWM_Q_FLOAT | SWM_Q_FULLSCREEN | SWM_Q_FOCUSPREV);
6975 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT);
6976 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ);
6977 setquirk("Firefox", "Dialog", SWM_Q_FLOAT);
6978 setquirk("Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
6979 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ);
6980 setquirk("xine", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
6981 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
6982 setquirk("xine", "xine Panel", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
6983 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
6984 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
6985 setquirk("pcb", "pcb", SWM_Q_FLOAT);
6986 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
6989 /* conf file stuff */
6990 #define SWM_CONF_FILE "spectrwm.conf"
6991 #define SWM_CONF_FILE_OLD "scrotwm.conf"
6995 SWM_S_BAR_AT_BOTTOM,
6996 SWM_S_BAR_BORDER_WIDTH,
6999 SWM_S_BAR_ENABLED_WS,
7004 SWM_S_BOUNDARY_WIDTH,
7005 SWM_S_CLOCK_ENABLED,
7008 SWM_S_CYCLE_VISIBLE,
7010 SWM_S_DISABLE_BORDER,
7012 SWM_S_FOCUS_CLOSE_WRAP,
7013 SWM_S_FOCUS_DEFAULT,
7015 SWM_S_REGION_PADDING,
7020 SWM_S_STACK_ENABLED,
7023 SWM_S_TITLE_CLASS_ENABLED,
7024 SWM_S_TITLE_NAME_ENABLED,
7025 SWM_S_URGENT_ENABLED,
7026 SWM_S_VERBOSE_LAYOUT,
7027 SWM_S_WINDOW_NAME_ENABLED,
7028 SWM_S_WORKSPACE_LIMIT
7032 setconfvalue(char *selector, char *value, int flags)
7034 struct workspace *ws;
7035 int i, ws_id, num_screens;
7038 /* suppress unused warning since var is needed */
7042 case SWM_S_BAR_ACTION:
7044 if ((bar_argv[0] = expand_tilde(value)) == NULL)
7045 err(1, "setconfvalue: bar_action");
7047 case SWM_S_BAR_AT_BOTTOM:
7048 bar_at_bottom = atoi(value);
7050 case SWM_S_BAR_BORDER_WIDTH:
7051 bar_border_width = atoi(value);
7052 if (bar_border_width < 0)
7053 bar_border_width = 0;
7055 case SWM_S_BAR_DELAY:
7056 /* No longer needed; leave to not break old conf files. */
7058 case SWM_S_BAR_ENABLED:
7059 bar_enabled = atoi(value);
7061 case SWM_S_BAR_ENABLED_WS:
7062 ws_id = atoi(selector) - 1;
7063 if (ws_id < 0 || ws_id >= workspace_limit)
7064 errx(1, "setconfvalue: bar_enabled_ws: invalid "
7065 "workspace %d.", ws_id + 1);
7067 num_screens = get_screen_count();
7068 for (i = 0; i < num_screens; i++) {
7069 ws = (struct workspace *)&screens[i].ws;
7070 ws[ws_id].bar_enabled = atoi(value);
7073 case SWM_S_BAR_FONT:
7075 if (asprintf(&bar_fonts, "%s,%s", value, bar_fonts) == -1)
7076 err(1, "setconfvalue: asprintf: failed to allocate "
7077 "memory for bar_fonts.");
7080 /* If already in xft mode, then we are done. */
7081 if (!bar_font_legacy)
7084 /* If there are any non-XLFD entries, switch to Xft mode. */
7085 while ((b = strsep(&value, ",")) != NULL) {
7089 bar_font_legacy = 0;
7094 case SWM_S_BAR_FORMAT:
7096 if ((bar_format = strdup(value)) == NULL)
7097 err(1, "setconfvalue: bar_format");
7099 case SWM_S_BAR_JUSTIFY:
7100 if (!strcmp(value, "left"))
7101 bar_justify = SWM_BAR_JUSTIFY_LEFT;
7102 else if (!strcmp(value, "center"))
7103 bar_justify = SWM_BAR_JUSTIFY_CENTER;
7104 else if (!strcmp(value, "right"))
7105 bar_justify = SWM_BAR_JUSTIFY_RIGHT;
7107 errx(1, "invalid bar_justify");
7109 case SWM_S_BORDER_WIDTH:
7110 border_width = atoi(value);
7111 if (border_width < 0)
7114 case SWM_S_BOUNDARY_WIDTH:
7115 boundary_width = atoi(value);
7116 if (boundary_width < 0)
7119 case SWM_S_CLOCK_ENABLED:
7120 clock_enabled = atoi(value);
7122 case SWM_S_CLOCK_FORMAT:
7123 #ifndef SWM_DENY_CLOCK_FORMAT
7125 if ((clock_format = strdup(value)) == NULL)
7126 err(1, "setconfvalue: clock_format");
7129 case SWM_S_CYCLE_EMPTY:
7130 cycle_empty = atoi(value);
7132 case SWM_S_CYCLE_VISIBLE:
7133 cycle_visible = atoi(value);
7135 case SWM_S_DIALOG_RATIO:
7136 dialog_ratio = atof(value);
7137 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
7140 case SWM_S_DISABLE_BORDER:
7141 disable_border = atoi(value);
7143 case SWM_S_FOCUS_CLOSE:
7144 if (!strcmp(value, "first"))
7145 focus_close = SWM_STACK_BOTTOM;
7146 else if (!strcmp(value, "last"))
7147 focus_close = SWM_STACK_TOP;
7148 else if (!strcmp(value, "next"))
7149 focus_close = SWM_STACK_ABOVE;
7150 else if (!strcmp(value, "previous"))
7151 focus_close = SWM_STACK_BELOW;
7153 errx(1, "focus_close");
7155 case SWM_S_FOCUS_CLOSE_WRAP:
7156 focus_close_wrap = atoi(value);
7158 case SWM_S_FOCUS_DEFAULT:
7159 if (!strcmp(value, "last"))
7160 focus_default = SWM_STACK_TOP;
7161 else if (!strcmp(value, "first"))
7162 focus_default = SWM_STACK_BOTTOM;
7164 errx(1, "focus_default");
7166 case SWM_S_FOCUS_MODE:
7167 if (!strcmp(value, "default"))
7168 focus_mode = SWM_FOCUS_DEFAULT;
7169 else if (!strcmp(value, "follow") ||
7170 !strcmp(value, "follow_cursor"))
7171 focus_mode = SWM_FOCUS_FOLLOW;
7172 else if (!strcmp(value, "manual"))
7173 focus_mode = SWM_FOCUS_MANUAL;
7175 errx(1, "focus_mode");
7177 case SWM_S_REGION_PADDING:
7178 region_padding = atoi(value);
7179 if (region_padding < 0)
7182 case SWM_S_SPAWN_ORDER:
7183 if (!strcmp(value, "first"))
7184 spawn_position = SWM_STACK_BOTTOM;
7185 else if (!strcmp(value, "last"))
7186 spawn_position = SWM_STACK_TOP;
7187 else if (!strcmp(value, "next"))
7188 spawn_position = SWM_STACK_ABOVE;
7189 else if (!strcmp(value, "previous"))
7190 spawn_position = SWM_STACK_BELOW;
7192 errx(1, "spawn_position");
7194 case SWM_S_SPAWN_TERM:
7195 setconfspawn("term", value, 0);
7196 setconfspawn("spawn_term", value, 0);
7199 /* No longer needed; leave to not break old conf files. */
7201 case SWM_S_SS_ENABLED:
7202 /* No longer needed; leave to not break old conf files. */
7204 case SWM_S_STACK_ENABLED:
7205 stack_enabled = atoi(value);
7207 case SWM_S_TERM_WIDTH:
7208 term_width = atoi(value);
7212 case SWM_S_TILE_GAP:
7213 tile_gap = atoi(value);
7215 case SWM_S_TITLE_CLASS_ENABLED:
7216 title_class_enabled = atoi(value);
7218 case SWM_S_TITLE_NAME_ENABLED:
7219 title_name_enabled = atoi(value);
7221 case SWM_S_URGENT_ENABLED:
7222 urgent_enabled = atoi(value);
7224 case SWM_S_VERBOSE_LAYOUT:
7225 verbose_layout = atoi(value);
7226 for (i = 0; layouts[i].l_stack != NULL; i++) {
7228 layouts[i].l_string = fancy_stacker;
7230 layouts[i].l_string = plain_stacker;
7233 case SWM_S_WINDOW_NAME_ENABLED:
7234 window_name_enabled = atoi(value);
7236 case SWM_S_WORKSPACE_LIMIT:
7237 workspace_limit = atoi(value);
7238 if (workspace_limit > SWM_WS_MAX)
7239 workspace_limit = SWM_WS_MAX;
7240 else if (workspace_limit < 1)
7241 workspace_limit = 1;
7250 setconfmodkey(char *selector, char *value, int flags)
7252 /* suppress unused warnings since vars are needed */
7256 if (!strncasecmp(value, "Mod1", strlen("Mod1")))
7257 update_modkey(XCB_MOD_MASK_1);
7258 else if (!strncasecmp(value, "Mod2", strlen("Mod2")))
7259 update_modkey(XCB_MOD_MASK_2);
7260 else if (!strncasecmp(value, "Mod3", strlen("Mod3")))
7261 update_modkey(XCB_MOD_MASK_3);
7262 else if (!strncasecmp(value, "Mod4", strlen("Mod4")))
7263 update_modkey(XCB_MOD_MASK_4);
7270 setconfcolor(char *selector, char *value, int flags)
7272 setscreencolor(value, ((selector == NULL)?-1:atoi(selector)), flags);
7277 setconfregion(char *selector, char *value, int flags)
7279 /* suppress unused warnings since vars are needed */
7283 custom_region(value);
7288 setautorun(char *selector, char *value, int flags)
7298 /* suppress unused warnings since vars are needed */
7302 if (getenv("SWM_STARTED"))
7306 if (sscanf(value, "ws[%d]:%1023c", &ws_id, s) != 2)
7307 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
7309 if (ws_id < 0 || ws_id >= workspace_limit)
7310 errx(1, "autorun: invalid workspace %d", ws_id + 1);
7312 sp = expand_tilde((char *)&s);
7315 * This is a little intricate
7317 * If the pid already exists we simply reuse it because it means it was
7318 * used before AND not claimed by manage_window. We get away with
7319 * altering it in the parent after INSERT because this can not be a race
7322 while ((ap = strsep(&sp, " \t")) != NULL) {
7325 DNPRINTF(SWM_D_SPAWN, "setautorun: arg [%s]\n", ap);
7327 if ((a.argv = realloc(a.argv, argc * sizeof(char *))) == NULL)
7328 err(1, "setautorun: realloc");
7329 a.argv[argc - 1] = ap;
7333 if ((a.argv = realloc(a.argv, (argc + 1) * sizeof(char *))) == NULL)
7334 err(1, "setautorun: realloc");
7335 a.argv[argc] = NULL;
7337 if ((pid = fork()) == 0) {
7338 spawn(ws_id, &a, 1);
7347 p = calloc(1, sizeof *p);
7350 TAILQ_INSERT_TAIL(&pidlist, p, entry);
7360 setlayout(char *selector, char *value, int flags)
7362 int ws_id, i, x, mg, ma, si, ar, f = 0;
7363 int st = SWM_V_STACK, num_screens;
7365 struct workspace *ws;
7367 /* suppress unused warnings since vars are needed */
7371 if (getenv("SWM_STARTED"))
7375 if (sscanf(value, "ws[%d]:%d:%d:%d:%d:%1023c",
7376 &ws_id, &mg, &ma, &si, &ar, s) != 6)
7377 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
7378 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
7381 if (ws_id < 0 || ws_id >= workspace_limit)
7382 errx(1, "layout: invalid workspace %d", ws_id + 1);
7384 if (!strcasecmp(s, "vertical"))
7386 else if (!strcasecmp(s, "vertical_flip")) {
7389 } else if (!strcasecmp(s, "horizontal"))
7391 else if (!strcasecmp(s, "horizontal_flip")) {
7394 } else if (!strcasecmp(s, "fullscreen"))
7397 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
7398 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
7401 num_screens = get_screen_count();
7402 for (i = 0; i < num_screens; i++) {
7403 ws = (struct workspace *)&screens[i].ws;
7404 ws[ws_id].cur_layout = &layouts[st];
7406 ws[ws_id].always_raise = ar;
7407 if (st == SWM_MAX_STACK)
7411 for (x = 0; x < abs(mg); x++) {
7412 ws[ws_id].cur_layout->l_config(&ws[ws_id],
7413 mg >= 0 ? SWM_ARG_ID_MASTERGROW :
7414 SWM_ARG_ID_MASTERSHRINK);
7418 for (x = 0; x < abs(ma); x++) {
7419 ws[ws_id].cur_layout->l_config(&ws[ws_id],
7420 ma >= 0 ? SWM_ARG_ID_MASTERADD :
7421 SWM_ARG_ID_MASTERDEL);
7425 for (x = 0; x < abs(si); x++) {
7426 ws[ws_id].cur_layout->l_config(&ws[ws_id],
7427 si >= 0 ? SWM_ARG_ID_STACKINC :
7428 SWM_ARG_ID_STACKDEC);
7433 ws[ws_id].cur_layout->l_config(&ws[ws_id],
7434 SWM_ARG_ID_FLIPLAYOUT);
7444 /* config options */
7445 struct config_option {
7447 int (*func)(char*, char*, int);
7450 struct config_option configopt[] = {
7451 { "autorun", setautorun, 0 },
7452 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
7453 { "bar_at_bottom", setconfvalue, SWM_S_BAR_AT_BOTTOM },
7454 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
7455 { "bar_border_unfocus", setconfcolor, SWM_S_COLOR_BAR_BORDER_UNFOCUS },
7456 { "bar_border_width", setconfvalue, SWM_S_BAR_BORDER_WIDTH },
7457 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
7458 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
7459 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
7460 { "bar_enabled_ws", setconfvalue, SWM_S_BAR_ENABLED_WS },
7461 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
7462 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
7463 { "bar_format", setconfvalue, SWM_S_BAR_FORMAT },
7464 { "bar_justify", setconfvalue, SWM_S_BAR_JUSTIFY },
7465 { "bind", setconfbinding, 0 },
7466 { "border_width", setconfvalue, SWM_S_BORDER_WIDTH },
7467 { "boundary_width", setconfvalue, SWM_S_BOUNDARY_WIDTH },
7468 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
7469 { "clock_format", setconfvalue, SWM_S_CLOCK_FORMAT },
7470 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
7471 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
7472 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
7473 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
7474 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
7475 { "disable_border", setconfvalue, SWM_S_DISABLE_BORDER },
7476 { "focus_close", setconfvalue, SWM_S_FOCUS_CLOSE },
7477 { "focus_close_wrap", setconfvalue, SWM_S_FOCUS_CLOSE_WRAP },
7478 { "focus_default", setconfvalue, SWM_S_FOCUS_DEFAULT },
7479 { "focus_mode", setconfvalue, SWM_S_FOCUS_MODE },
7480 { "keyboard_mapping", setkeymapping, 0 },
7481 { "layout", setlayout, 0 },
7482 { "modkey", setconfmodkey, 0 },
7483 { "program", setconfspawn, 0 },
7484 { "quirk", setconfquirk, 0 },
7485 { "region", setconfregion, 0 },
7486 { "region_padding", setconfvalue, SWM_S_REGION_PADDING },
7487 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
7488 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
7489 { "spawn_position", setconfvalue, SWM_S_SPAWN_ORDER },
7490 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
7491 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
7492 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
7493 { "tile_gap", setconfvalue, SWM_S_TILE_GAP },
7494 { "title_class_enabled", setconfvalue, SWM_S_TITLE_CLASS_ENABLED },
7495 { "title_name_enabled", setconfvalue, SWM_S_TITLE_NAME_ENABLED },
7496 { "urgent_enabled", setconfvalue, SWM_S_URGENT_ENABLED },
7497 { "verbose_layout", setconfvalue, SWM_S_VERBOSE_LAYOUT },
7498 { "window_name_enabled", setconfvalue, SWM_S_WINDOW_NAME_ENABLED },
7499 { "workspace_limit", setconfvalue, SWM_S_WORKSPACE_LIMIT },
7503 _add_startup_exception(const char *fmt, va_list ap)
7505 if (vasprintf(&startup_exception, fmt, ap) == -1)
7506 warn("%s: asprintf", __func__);
7510 add_startup_exception(const char *fmt, ...)
7516 if (startup_exception)
7519 /* force bar to be enabled due to exception */
7523 _add_startup_exception(fmt, ap);
7528 conf_load(const char *filename, int keymapping)
7531 char *line = NULL, *cp, *optsub, *optval = NULL;
7532 size_t linelen, lineno = 0;
7533 int wordlen, i, optidx;
7534 struct config_option *opt = NULL;
7536 DNPRINTF(SWM_D_CONF, "conf_load: begin\n");
7538 if (filename == NULL) {
7539 warnx("conf_load: no filename");
7543 DNPRINTF(SWM_D_CONF, "conf_load: open %s\n", filename);
7545 if ((config = fopen(filename, "r")) == NULL) {
7546 warn("conf_load: fopen: %s", filename);
7550 while (!feof(config)) {
7554 if ((line = fparseln(config, &linelen, &lineno, NULL,
7555 FPARSELN_UNESCCOMM | FPARSELN_UNESCCONT)) == NULL) {
7557 err(1, "%s", filename);
7562 cp += strspn(cp, " \t\n"); /* eat whitespace */
7563 if (cp[0] == '\0') {
7567 /* get config option */
7568 wordlen = strcspn(cp, "=[ \t\n");
7570 add_startup_exception("%s: line %zd: no option found",
7575 for (i = 0; i < LENGTH(configopt); i++) {
7576 opt = &configopt[i];
7577 if (!strncasecmp(cp, opt->optname, wordlen) &&
7578 (int)strlen(opt->optname) == wordlen) {
7584 add_startup_exception("%s: line %zd: unknown option "
7585 "%.*s", filename, lineno, wordlen, cp);
7588 if (keymapping && opt && strcmp(opt->optname, "bind")) {
7589 add_startup_exception("%s: line %zd: invalid option "
7590 "%.*s", filename, lineno, wordlen, cp);
7594 cp += strspn(cp, " \t\n"); /* eat whitespace */
7596 /* from here on out we call goto invalid to continue */
7598 /* get [selector] if any */
7602 wordlen = strcspn(cp, "]");
7605 add_startup_exception("%s: line %zd: "
7606 "syntax error", filename, lineno);
7610 if (asprintf(&optsub, "%.*s", wordlen, cp) ==
7612 add_startup_exception("%s: line %zd: "
7613 "unable to allocatememory for "
7614 "selector", filename, lineno);
7619 cp += strspn(cp, "] \t\n"); /* eat trailing */
7621 cp += strspn(cp, "= \t\n"); /* eat trailing */
7623 optval = strdup(cp);
7624 if (strlen(optval) == 0) {
7625 add_startup_exception("%s: line %zd: must supply value "
7626 "to %s", filename, lineno,
7627 configopt[optidx].optname);
7630 /* call function to deal with it all */
7631 if (configopt[optidx].func(optsub, optval,
7632 configopt[optidx].funcflags) != 0) {
7633 add_startup_exception("%s: line %zd: invalid data for "
7634 "%s", filename, lineno, configopt[optidx].optname);
7651 DNPRINTF(SWM_D_CONF, "conf_load: end\n");
7657 set_child_transient(struct ws_win *win, xcb_window_t *trans)
7659 struct ws_win *parent, *w;
7660 struct swm_region *r;
7661 struct workspace *ws;
7662 xcb_icccm_wm_hints_t wmh;
7664 parent = find_window(win->transient);
7666 parent->focus_child = win;
7668 DNPRINTF(SWM_D_MISC, "set_child_transient: parent doesn't exist"
7669 " for 0x%x trans 0x%x\n", win->id, win->transient);
7671 r = root_to_region(win->wa->root, SWM_CK_ALL);
7673 /* parent doen't exist in our window list */
7674 TAILQ_FOREACH(w, &ws->winlist, entry) {
7675 if (xcb_icccm_get_wm_hints_reply(conn,
7676 xcb_icccm_get_wm_hints(conn, w->id),
7678 warnx("can't get hints for 0x%x", w->id);
7682 if (win->hints.window_group != wmh.window_group)
7685 w->focus_child = win;
7686 win->transient = w->id;
7688 DNPRINTF(SWM_D_MISC, "set_child_transient: adjusting "
7689 "transient to 0x%x\n", win->transient);
7696 window_get_pid(xcb_window_t win)
7701 xcb_get_property_cookie_t pc;
7702 xcb_get_property_reply_t *pr;
7704 apid = get_atom_from_string("_NET_WM_PID");
7705 if (apid == XCB_ATOM_NONE)
7708 pc = xcb_get_property(conn, 0, win, apid, XCB_ATOM_CARDINAL, 0, 1);
7709 pr = xcb_get_property_reply(conn, pc, NULL);
7712 if (pr->type != XCB_ATOM_CARDINAL) {
7717 if (pr->type == apid && pr->format == 32)
7718 ret = *((pid_t *)xcb_get_property_value(pr));
7724 apid = get_atom_from_string("_SWM_PID");
7725 pc = xcb_get_property(conn, 0, win, apid, XCB_ATOM_STRING,
7727 pr = xcb_get_property_reply(conn, pc, NULL);
7730 if (pr->type != apid) {
7735 ret = (pid_t)strtonum(xcb_get_property_value(pr), 0, INT_MAX, &errstr);
7742 get_ws_idx(xcb_window_t id)
7748 xcb_get_property_reply_t *gpr;
7750 gpr = xcb_get_property_reply(conn,
7751 xcb_get_property(conn, 0, id, a_swm_ws,
7752 XCB_ATOM_STRING, 0, SWM_PROPLEN),
7757 proplen = xcb_get_property_value_length(gpr);
7759 prop = malloc(proplen + 1);
7762 xcb_get_property_value(gpr),
7764 prop[proplen] = '\0';
7771 DNPRINTF(SWM_D_PROP, "get_ws_idx: _SWM_WS: %s\n", prop);
7772 ws_idx = (int)strtonum(prop, 0, workspace_limit - 1, &errstr);
7774 DNPRINTF(SWM_D_PROP, "get_ws_idx: window: #%s: %s",
7784 manage_window(xcb_window_t id, uint16_t mapped)
7786 xcb_window_t trans = XCB_WINDOW_NONE;
7787 struct ws_win *win, *ww;
7789 char ws_idx_str[SWM_PROPLEN];
7790 struct swm_region *r;
7794 xcb_icccm_get_wm_protocols_reply_t wpr;
7796 if ((win = find_window(id)) != NULL) {
7797 DNPRINTF(SWM_D_MISC, "manage_window: win 0x%x already "
7798 "managed; skipping.)\n", id);
7799 return (win); /* Already managed. */
7802 /* See if window is on the unmanaged list. */
7803 if ((win = find_unmanaged_window(id)) != NULL) {
7804 DNPRINTF(SWM_D_MISC, "manage_window: win 0x%x found on "
7805 "unmanaged list.\n", id);
7806 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
7809 set_child_transient(win, &trans);
7813 DNPRINTF(SWM_D_MISC, "manage_window: win 0x%x is new.\n", id);
7816 /* Create and initialize ws_win object. */
7817 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
7818 err(1, "manage_window: calloc: failed to allocate memory for "
7823 /* Get window geometry. */
7824 win->wa = xcb_get_geometry_reply(conn,
7825 xcb_get_geometry(conn, win->id),
7828 /* Figure out which region the window belongs to. */
7829 r = root_to_region(win->wa->root, SWM_CK_ALL);
7831 /* Ignore window border if there is one. */
7832 WIDTH(win) = win->wa->width;
7833 HEIGHT(win) = win->wa->height;
7834 X(win) = win->wa->x + win->wa->border_width - border_width;
7835 Y(win) = win->wa->y + win->wa->border_width - border_width;
7837 win->mapped = mapped;
7838 win->floatmaxed = 0;
7839 win->ewmh_flags = 0;
7840 win->s = r->s; /* this never changes */
7842 /* Get WM_SIZE_HINTS. */
7843 xcb_icccm_get_wm_normal_hints_reply(conn,
7844 xcb_icccm_get_wm_normal_hints(conn, win->id),
7848 xcb_icccm_get_wm_hints_reply(conn,
7849 xcb_icccm_get_wm_hints(conn, win->id),
7852 /* Get WM_TRANSIENT_FOR; see if window is a transient. */
7853 xcb_icccm_get_wm_transient_for_reply(conn,
7854 xcb_icccm_get_wm_transient_for(conn, win->id),
7857 win->transient = trans;
7858 set_child_transient(win, &win->transient);
7861 /* Get supported protocols. */
7862 if (xcb_icccm_get_wm_protocols_reply(conn,
7863 xcb_icccm_get_wm_protocols(conn, win->id, a_prot),
7865 for (i = 0; i < wpr.atoms_len; i++) {
7866 if (wpr.atoms[i] == a_takefocus)
7867 win->take_focus = 1;
7868 if (wpr.atoms[i] == a_delete)
7869 win->can_delete = 1;
7871 xcb_icccm_get_wm_protocols_reply_wipe(&wpr);
7874 win->iconic = get_swm_iconic(win);
7876 /* Figure out which workspace the window belongs to. */
7877 if ((p = find_pid(window_get_pid(win->id))) != NULL) {
7878 win->ws = &r->s->ws[p->ws];
7879 TAILQ_REMOVE(&pidlist, p, entry);
7882 } else if ((ws_idx = get_ws_idx(win->id)) != -1 &&
7884 /* _SWM_WS is set; use that. */
7885 win->ws = &r->s->ws[ws_idx];
7886 } else if (trans && (ww = find_window(trans)) != NULL) {
7887 /* Launch transients in the same ws as parent. */
7893 /* Set the _SWM_WS atom so we can remember this after reincarnation. */
7894 if (snprintf(ws_idx_str, SWM_PROPLEN, "%d", win->ws->idx) <
7896 DNPRINTF(SWM_D_PROP, "manage_window: set _SWM_WS: %s\n",
7898 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
7899 a_swm_ws, XCB_ATOM_STRING, 8, strlen(ws_idx_str),
7903 /* WS must already be set for this to work. */
7904 store_float_geom(win);
7907 ewmh_autoquirk(win);
7909 /* Determine initial quirks. */
7910 if (xcb_icccm_get_wm_class_reply(conn,
7911 xcb_icccm_get_wm_class(conn, win->id),
7913 DNPRINTF(SWM_D_CLASS, "manage_window: class: %s, name: %s\n",
7914 win->ch.class_name, win->ch.instance_name);
7916 /* java is retarded so treat it special */
7917 if (strstr(win->ch.instance_name, "sun-awt")) {
7918 DNPRINTF(SWM_D_CLASS, "manage_window: java window "
7923 TAILQ_FOREACH(qp, &quirks, entry) {
7924 if (!strcmp(win->ch.class_name, qp->class) &&
7925 !strcmp(win->ch.instance_name, qp->name)) {
7926 DNPRINTF(SWM_D_CLASS, "manage_window: on quirks"
7927 "list; mask: 0x%lx\n", qp->quirk);
7928 if (qp->quirk & SWM_Q_FLOAT)
7930 win->quirks = qp->quirk;
7935 /* Alter window position if quirky */
7936 if (win->quirks & SWM_Q_ANYWHERE)
7939 /* Reset font sizes (the bruteforce way; no default keybinding). */
7940 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
7941 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
7942 fake_keypress(win, XK_KP_Subtract, XCB_MOD_MASK_SHIFT);
7943 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
7944 fake_keypress(win, XK_KP_Add, XCB_MOD_MASK_SHIFT);
7947 /* Make sure window is positioned inside its region, if its active. */
7949 region_containment(win, r, SWM_CW_ALLSIDES |
7950 SWM_CW_HARDBOUNDARY);
7954 /* Select which X events to monitor and set border pixel color. */
7955 wa[0] = win->s->c[SWM_S_COLOR_UNFOCUS].pixel;
7956 wa[1] = XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_PROPERTY_CHANGE |
7957 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
7959 wa[1] |= XCB_EVENT_MASK_LEAVE_WINDOW | XCB_EVENT_MASK_FOCUS_CHANGE;
7962 xcb_change_window_attributes(conn, win->id, XCB_CW_BORDER_PIXEL |
7963 XCB_CW_EVENT_MASK, wa);
7966 /* Figure out where to stack the window in the workspace. */
7967 if (trans && (ww = find_window(trans)))
7968 TAILQ_INSERT_AFTER(&win->ws->winlist, ww, win, entry);
7969 else if (win->ws->focus && spawn_position == SWM_STACK_ABOVE)
7970 TAILQ_INSERT_AFTER(&win->ws->winlist, win->ws->focus, win,
7972 else if (win->ws->focus && spawn_position == SWM_STACK_BELOW)
7973 TAILQ_INSERT_BEFORE(win->ws->focus, win, entry);
7974 else switch (spawn_position) {
7977 case SWM_STACK_ABOVE:
7978 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
7980 case SWM_STACK_BOTTOM:
7981 case SWM_STACK_BELOW:
7982 TAILQ_INSERT_HEAD(&win->ws->winlist, win, entry);
7985 /* Get initial _NET_WM_STATE */
7986 ewmh_get_win_state(win);
7987 /* Set initial _NET_WM_ALLOWED_ACTIONS */
7988 ewmh_update_actions(win);
7992 DNPRINTF(SWM_D_MISC, "manage_window: done. window: 0x%x, (x,y) w x h: "
7993 "(%d,%d) %d x %d, ws: %d, iconic: %s, transient: 0x%x\n", win->id,
7994 X(win), Y(win), WIDTH(win), HEIGHT(win), win->ws->idx,
7995 YESNO(win->iconic), win->transient);
8001 free_window(struct ws_win *win)
8003 DNPRINTF(SWM_D_MISC, "free_window: window: 0x%x\n", win->id);
8008 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
8013 xcb_icccm_get_wm_class_reply_wipe(&win->ch);
8018 memset(win, 0xff, sizeof *win); /* XXX kill later */
8021 DNPRINTF(SWM_D_MISC, "free_window: done.\n");
8025 unmanage_window(struct ws_win *win)
8027 struct ws_win *parent;
8032 DNPRINTF(SWM_D_MISC, "unmanage_window: window: 0x%x\n", win->id);
8034 if (win->transient) {
8035 parent = find_window(win->transient);
8037 parent->focus_child = NULL;
8040 TAILQ_REMOVE(&win->ws->winlist, win, entry);
8041 TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
8045 expose(xcb_expose_event_t *e)
8048 struct swm_region *r;
8050 DNPRINTF(SWM_D_EVENT, "expose: window: 0x%x\n", e->window);
8052 num_screens = get_screen_count();
8053 for (i = 0; i < num_screens; i++)
8054 TAILQ_FOREACH(r, &screens[i].rl, entry)
8055 if (e->window == WINID(r->bar))
8063 focusin(xcb_focus_in_event_t *e)
8065 DNPRINTF(SWM_D_EVENT, "focusin: window: 0x%x, mode: %s(%u), "
8066 "detail: %s(%u)\n", e->event, get_notify_mode_label(e->mode),
8067 e->mode, get_notify_detail_label(e->detail), e->detail);
8071 focusout(xcb_focus_out_event_t *e)
8073 DNPRINTF(SWM_D_EVENT, "focusout: window: 0x%x, mode: %s(%u), "
8074 "detail: %s(%u)\n", e->event, get_notify_mode_label(e->mode),
8075 e->mode, get_notify_detail_label(e->detail), e->detail);
8080 keypress(xcb_key_press_event_t *e)
8082 xcb_keysym_t keysym;
8085 keysym = xcb_key_press_lookup_keysym(syms, e, 0);
8087 DNPRINTF(SWM_D_EVENT, "keypress: keysym: %u, win (x,y): 0x%x (%d,%d), "
8088 "detail: %u, time: %u, root (x,y): 0x%x (%d,%d), child: 0x%x, "
8089 "state: %u, same_screen: %s\n", keysym, e->event, e->event_x,
8090 e->event_y, e->detail, e->time, e->root, e->root_x, e->root_y,
8091 e->child, e->state, YESNO(e->same_screen));
8093 if ((kp = key_lookup(CLEANMASK(e->state), keysym)) == NULL)
8096 last_event_time = e->time;
8098 if (kp->funcid == KF_SPAWN_CUSTOM)
8099 spawn_custom(root_to_region(e->root, SWM_CK_ALL),
8100 &(keyfuncs[kp->funcid].args), kp->spawn_name);
8101 else if (keyfuncs[kp->funcid].func)
8102 keyfuncs[kp->funcid].func(root_to_region(e->root, SWM_CK_ALL),
8103 &(keyfuncs[kp->funcid].args));
8106 /* Unfreeze grab events. */
8107 xcb_allow_events(conn, XCB_ALLOW_ASYNC_KEYBOARD, e->time);
8110 DNPRINTF(SWM_D_EVENT, "keypress: done.\n");
8114 buttonpress(xcb_button_press_event_t *e)
8116 struct ws_win *win = NULL;
8117 struct swm_region *r, *old_r;
8121 DNPRINTF(SWM_D_EVENT, "buttonpress: win (x,y): 0x%x (%d,%d), "
8122 "detail: %u, time: %u, root (x,y): 0x%x (%d,%d), child: 0x%x, "
8123 "state: %u, same_screen: %s\n", e->event, e->event_x, e->event_y,
8124 e->detail, e->time, e->root, e->root_x, e->root_y, e->child,
8125 e->state, YESNO(e->same_screen));
8127 if (e->event == e->root) {
8128 if (e->child != 0) {
8129 win = find_window(e->child);
8130 /* Pass ButtonPress to window if it isn't managed. */
8134 /* Focus on empty region */
8135 /* If no windows on region if its empty. */
8136 r = root_to_region(e->root, SWM_CK_POINTER);
8138 DNPRINTF(SWM_D_EVENT, "buttonpress: "
8139 "NULL region; ignoring.\n");
8143 if (TAILQ_EMPTY(&r->ws->winlist)) {
8144 old_r = root_to_region(e->root, SWM_CK_FOCUS);
8145 if (old_r && old_r != r)
8146 unfocus_win(old_r->ws->focus);
8148 xcb_set_input_focus(conn,
8149 XCB_INPUT_FOCUS_PARENT, e->root, e->time);
8151 /* Clear bar since empty. */
8159 win = find_window(e->event);
8165 last_event_time = e->time;
8167 focus_win(get_focus_magic(win));
8169 for (i = 0; i < LENGTH(buttons); i++)
8170 if (client_click == buttons[i].action && buttons[i].func &&
8171 buttons[i].button == e->detail &&
8172 CLEANMASK(buttons[i].mask) == CLEANMASK(e->state)) {
8173 buttons[i].func(win, &buttons[i].args);
8179 DNPRINTF(SWM_D_EVENT, "buttonpress: passing to window.\n");
8180 /* Replay event to event window */
8181 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, e->time);
8183 DNPRINTF(SWM_D_EVENT, "buttonpress: handled.\n");
8184 /* Unfreeze grab events. */
8185 xcb_allow_events(conn, XCB_ALLOW_SYNC_POINTER, e->time);
8193 print_win_geom(xcb_window_t w)
8195 xcb_get_geometry_reply_t *wa;
8197 wa = xcb_get_geometry_reply(conn, xcb_get_geometry(conn, w), NULL);
8200 DNPRINTF(SWM_D_MISC, "print_win_geom: window not found: 0x%x\n",
8205 DNPRINTF(SWM_D_MISC, "print_win_geom: window: 0x%x, root: 0x%x, "
8206 "depth: %u, (x,y) w x h: (%d,%d) %d x %d, border: %d\n",
8207 w, wa->root, wa->depth, wa->x, wa->y, wa->width, wa->height,
8216 get_stack_mode_name(uint8_t mode)
8221 case XCB_STACK_MODE_ABOVE:
8224 case XCB_STACK_MODE_BELOW:
8227 case XCB_STACK_MODE_TOP_IF:
8230 case XCB_STACK_MODE_BOTTOM_IF:
8233 case XCB_STACK_MODE_OPPOSITE:
8245 configurerequest(xcb_configure_request_event_t *e)
8248 struct swm_region *r = NULL;
8251 uint32_t wc[7] = {0};
8253 if ((win = find_window(e->window)) == NULL)
8254 if ((win = find_unmanaged_window(e->window)) == NULL)
8258 if (swm_debug & SWM_D_EVENT) {
8259 print_win_geom(e->window);
8261 DNPRINTF(SWM_D_EVENT, "configurerequest: window: 0x%x, "
8262 "parent: 0x%x, new: %s, value_mask: %u { ", e->window,
8263 e->parent, YESNO(new), e->value_mask);
8264 if (e->value_mask & XCB_CONFIG_WINDOW_X)
8265 DPRINTF("X: %d ", e->x);
8266 if (e->value_mask & XCB_CONFIG_WINDOW_Y)
8267 DPRINTF("Y: %d ", e->y);
8268 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH)
8269 DPRINTF("W: %u ", e->width);
8270 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
8271 DPRINTF("H: %u ", e->height);
8272 if (e->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH)
8273 DPRINTF("Border: %u ", e->border_width);
8274 if (e->value_mask & XCB_CONFIG_WINDOW_SIBLING)
8275 DPRINTF("Sibling: 0x%x ", e->sibling);
8276 if (e->value_mask & XCB_CONFIG_WINDOW_STACK_MODE)
8277 DPRINTF("StackMode: %s(%u) ",
8278 get_stack_mode_name(e->stack_mode), e->stack_mode);
8284 if (e->value_mask & XCB_CONFIG_WINDOW_X) {
8285 mask |= XCB_CONFIG_WINDOW_X;
8288 if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
8289 mask |= XCB_CONFIG_WINDOW_Y;
8292 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
8293 mask |= XCB_CONFIG_WINDOW_WIDTH;
8296 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
8297 mask |= XCB_CONFIG_WINDOW_HEIGHT;
8298 wc[i++] = e->height;
8300 if (e->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH) {
8301 mask |= XCB_CONFIG_WINDOW_BORDER_WIDTH;
8302 wc[i++] = e->border_width;
8304 if (e->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
8305 mask |= XCB_CONFIG_WINDOW_SIBLING;
8306 wc[i++] = e->sibling;
8308 if (e->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
8309 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
8310 wc[i++] = e->stack_mode;
8314 xcb_configure_window(conn, e->window, mask, wc);
8317 } else if ((!win->manual || win->quirks & SWM_Q_ANYWHERE) &&
8318 !(win->ewmh_flags & EWMH_F_FULLSCREEN)) {
8321 else if (win->ws->old_r)
8324 /* windows are centered unless ANYWHERE quirk is set. */
8325 if (win->quirks & SWM_Q_ANYWHERE) {
8326 if (e->value_mask & XCB_CONFIG_WINDOW_X) {
8327 win->g_float.x = e->x;
8329 win->g_float.x -= X(r);
8332 if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
8333 win->g_float.y = e->y;
8335 win->g_float.y -= Y(r);
8339 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH)
8340 win->g_float.w = e->width;
8342 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
8343 win->g_float.h = e->height;
8345 win->g_floatvalid = 1;
8347 if (win->floating && r && (win->transient ||
8348 win->ws->cur_layout != &layouts[SWM_MAX_STACK])) {
8349 WIDTH(win) = win->g_float.w;
8350 HEIGHT(win) = win->g_float.h;
8353 stack_floater(win, r);
8365 DNPRINTF(SWM_D_EVENT, "configurerequest: done.\n");
8369 configurenotify(xcb_configure_notify_event_t *e)
8373 DNPRINTF(SWM_D_EVENT, "configurenotify: win 0x%x, event win: 0x%x, "
8374 "(x,y) WxH: (%d,%d) %ux%u, border: %u, above_sibling: 0x%x, "
8375 "override_redirect: %s\n", e->window, e->event, e->x, e->y,
8376 e->width, e->height, e->border_width, e->above_sibling,
8377 YESNO(e->override_redirect));
8379 win = find_window(e->window);
8381 xcb_icccm_get_wm_normal_hints_reply(conn,
8382 xcb_icccm_get_wm_normal_hints(conn, win->id),
8385 if (font_adjusted) {
8393 destroynotify(xcb_destroy_notify_event_t *e)
8397 DNPRINTF(SWM_D_EVENT, "destroynotify: window: 0x%x\n", e->window);
8399 if ((win = find_window(e->window)) == NULL) {
8400 if ((win = find_unmanaged_window(e->window)) == NULL)
8406 if (focus_mode != SWM_FOCUS_FOLLOW) {
8407 /* If we were focused, make sure we focus on something else. */
8408 if (win == win->ws->focus)
8409 win->ws->focus_pending = get_focus_prev(win);
8412 unmanage_window(win);
8415 if (focus_mode != SWM_FOCUS_FOLLOW) {
8416 if (win->ws->focus_pending) {
8417 focus_win(win->ws->focus_pending);
8418 win->ws->focus_pending = NULL;
8419 } else if (win == win->ws->focus && win->ws->r) {
8420 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT,
8421 win->ws->r->id, XCB_CURRENT_TIME);
8432 get_notify_detail_label(uint8_t detail)
8437 case XCB_NOTIFY_DETAIL_ANCESTOR:
8440 case XCB_NOTIFY_DETAIL_VIRTUAL:
8443 case XCB_NOTIFY_DETAIL_INFERIOR:
8446 case XCB_NOTIFY_DETAIL_NONLINEAR:
8447 label = "Nonlinear";
8449 case XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL:
8450 label = "NonlinearVirtual";
8452 case XCB_NOTIFY_DETAIL_POINTER:
8455 case XCB_NOTIFY_DETAIL_POINTER_ROOT:
8456 label = "PointerRoot";
8458 case XCB_NOTIFY_DETAIL_NONE:
8469 get_notify_mode_label(uint8_t mode)
8474 case XCB_NOTIFY_MODE_NORMAL:
8477 case XCB_NOTIFY_MODE_GRAB:
8480 case XCB_NOTIFY_MODE_UNGRAB:
8483 case XCB_NOTIFY_MODE_WHILE_GRABBED:
8484 label = "WhileGrabbed";
8495 enternotify(xcb_enter_notify_event_t *e)
8498 struct swm_region *r;
8500 DNPRINTF(SWM_D_FOCUS, "enternotify: time: %u, win (x,y): 0x%x "
8501 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): 0x%x (%d,%d), "
8502 "child: 0x%x, same_screen_focus: %s, state: %d\n",
8503 e->time, e->event, e->event_x, e->event_y,
8504 get_notify_mode_label(e->mode), e->mode,
8505 get_notify_detail_label(e->detail), e->detail,
8506 e->root, e->root_x, e->root_y, e->child,
8507 YESNO(e->same_screen_focus), e->state);
8509 if (focus_mode == SWM_FOCUS_MANUAL &&
8510 e->mode == XCB_NOTIFY_MODE_NORMAL) {
8511 DNPRINTF(SWM_D_EVENT, "enternotify: manual focus; ignoring.\n");
8515 last_event_time = e->time;
8517 if ((win = find_window(e->event)) == NULL) {
8518 if (e->event == e->root) {
8519 /* If no windows on pointer region, then focus root. */
8520 r = root_to_region(e->root, SWM_CK_POINTER);
8522 DNPRINTF(SWM_D_EVENT, "enternotify: "
8523 "NULL region; ignoring.\n");
8529 DNPRINTF(SWM_D_EVENT, "enternotify: window is NULL; "
8534 focus_win(get_focus_magic(win));
8542 leavenotify(xcb_leave_notify_event_t *e)
8544 DNPRINTF(SWM_D_FOCUS, "leavenotify: time: %u, win (x,y): 0x%x "
8545 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): 0x%x (%d,%d), "
8546 "child: 0x%x, same_screen_focus: %s, state: %d\n",
8547 e->time, e->event, e->event_x, e->event_y,
8548 get_notify_mode_label(e->mode), e->mode,
8549 get_notify_detail_label(e->detail), e->detail,
8550 e->root, e->root_x, e->root_y, e->child,
8551 YESNO(e->same_screen_focus), e->state);
8556 mapnotify(xcb_map_notify_event_t *e)
8560 DNPRINTF(SWM_D_EVENT, "mapnotify: window: 0x%x\n", e->window);
8562 if ((win = find_window(e->window)) == NULL)
8563 win = manage_window(e->window, 1);
8566 set_win_state(win, XCB_ICCCM_WM_STATE_NORMAL);
8568 if (focus_mode != SWM_FOCUS_FOLLOW) {
8569 if (win->ws->focus_pending == win) {
8571 win->ws->focus_pending = NULL;
8580 mappingnotify(xcb_mapping_notify_event_t *e)
8582 xcb_refresh_keyboard_mapping(syms, e);
8584 if (e->request == XCB_MAPPING_KEYBOARD)
8589 maprequest(xcb_map_request_event_t *e)
8591 struct ws_win *win, *w = NULL;
8592 xcb_get_window_attributes_reply_t *war;
8594 DNPRINTF(SWM_D_EVENT, "maprequest: win 0x%x\n",
8597 war = xcb_get_window_attributes_reply(conn,
8598 xcb_get_window_attributes(conn, e->window),
8601 DNPRINTF(SWM_D_EVENT, "maprequest: window lost.\n");
8605 if (war->override_redirect) {
8606 DNPRINTF(SWM_D_EVENT, "maprequest: override_redirect; "
8611 win = manage_window(e->window,
8612 (war->map_state == XCB_MAP_STATE_VIEWABLE));
8614 /* The new window should get focus; prepare. */
8615 if (focus_mode != SWM_FOCUS_FOLLOW &&
8616 !(win->quirks & SWM_Q_NOFOCUSONMAP)) {
8617 if (win->quirks & SWM_Q_FOCUSONMAP_SINGLE) {
8618 /* See if other wins of same type are already mapped. */
8619 TAILQ_FOREACH(w, &win->ws->winlist, entry) {
8620 if (w == win || !w->mapped)
8623 if (w->ch.class_name &&
8624 win->ch.class_name &&
8625 !strcmp(w->ch.class_name,
8626 win->ch.class_name) &&
8627 w->ch.instance_name &&
8628 win->ch.instance_name &&
8629 !strcmp(w->ch.instance_name,
8630 win->ch.instance_name))
8636 win->ws->focus_pending = get_focus_magic(win);
8639 /* All windows need to be mapped if they are in the current workspace.*/
8643 /* Ignore EnterNotify to handle the mapnotify without interference. */
8644 if (focus_mode == SWM_FOCUS_DEFAULT)
8645 event_drain(XCB_ENTER_NOTIFY);
8648 DNPRINTF(SWM_D_EVENT, "maprequest: done.\n");
8652 motionnotify(xcb_motion_notify_event_t *e)
8654 struct swm_region *r;
8657 DNPRINTF(SWM_D_FOCUS, "motionnotify: time: %u, win (x,y): 0x%x "
8658 "(%d,%d), detail: %s(%d), root (x,y): 0x%x (%d,%d), "
8659 "child: 0x%x, same_screen_focus: %s, state: %d\n",
8660 e->time, e->event, e->event_x, e->event_y,
8661 get_notify_detail_label(e->detail), e->detail,
8662 e->root, e->root_x, e->root_y, e->child,
8663 YESNO(e->same_screen), e->state);
8665 last_event_time = e->time;
8667 if (focus_mode == SWM_FOCUS_MANUAL)
8670 num_screens = get_screen_count();
8671 for (i = 0; i < num_screens; i++)
8672 if (screens[i].root == e->root)
8675 TAILQ_FOREACH(r, &screens[i].rl, entry)
8676 if (X(r) <= e->root_x && e->root_x < MAX_X(r) &&
8677 Y(r) <= e->root_y && e->root_y < MAX_Y(r))
8685 get_atom_name(xcb_atom_t atom)
8690 * This should be disabled during most debugging since
8691 * xcb_get_* causes an xcb_flush.
8694 xcb_get_atom_name_reply_t *r;
8696 r = xcb_get_atom_name_reply(conn,
8697 xcb_get_atom_name(conn, atom),
8700 len = xcb_get_atom_name_name_length(r);
8702 name = malloc(len + 1);
8704 memcpy(name, xcb_get_atom_name_name(r), len);
8718 propertynotify(xcb_property_notify_event_t *e)
8721 struct workspace *ws;
8725 name = get_atom_name(e->atom);
8726 DNPRINTF(SWM_D_EVENT, "propertynotify: window: 0x%x, atom: %s(%u), "
8727 "time: %#x, state: %u\n", e->window, name, e->atom, e->time,
8731 win = find_window(e->window);
8737 last_event_time = e->time;
8739 if (e->atom == a_swm_iconic) {
8740 if (e->state == XCB_PROPERTY_NEW_VALUE) {
8741 if (focus_mode != SWM_FOCUS_FOLLOW)
8742 ws->focus_pending = get_focus_prev(win);
8750 if (focus_mode != SWM_FOCUS_FOLLOW) {
8751 if (ws->focus_pending) {
8752 focus_win(ws->focus_pending);
8753 ws->focus_pending = NULL;
8755 xcb_set_input_focus(conn,
8756 XCB_INPUT_FOCUS_PARENT,
8764 } else if (e->state == XCB_PROPERTY_DELETE) {
8765 /* The window is no longer iconic, restack ws. */
8766 if (focus_mode != SWM_FOCUS_FOLLOW)
8767 ws->focus_pending = get_focus_magic(win);
8771 /* Flush EnterNotify for mapnotify, if needed. */
8774 } else if (e->atom == a_state) {
8775 /* State just changed, make sure it gets focused if mapped. */
8776 if (e->state == XCB_PROPERTY_NEW_VALUE) {
8777 if (focus_mode != SWM_FOCUS_FOLLOW) {
8779 ws->focus_pending == win) {
8780 focus_win(ws->focus_pending);
8781 ws->focus_pending = NULL;
8785 } else if (e->atom == XCB_ATOM_WM_CLASS ||
8786 e->atom == XCB_ATOM_WM_NAME) {
8794 unmapnotify(xcb_unmap_notify_event_t *e)
8797 struct workspace *ws;
8799 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: 0x%x\n", e->window);
8801 /* If we aren't managing the window, then ignore. */
8802 win = find_window(e->window);
8803 if (win == NULL || win->id != e->window)
8808 if (getstate(e->window) != XCB_ICCCM_WM_STATE_ICONIC)
8809 set_win_state(win, XCB_ICCCM_WM_STATE_ICONIC);
8812 /* window unmapped itself */
8813 /* do unmap/unfocus/restack and unmanage */
8816 /* If win was focused, make sure to focus on something else. */
8817 if (win == ws->focus) {
8818 if (focus_mode != SWM_FOCUS_FOLLOW) {
8819 ws->focus_pending = get_focus_prev(win);
8820 DNPRINTF(SWM_D_EVENT, "unmapnotify: "
8821 "focus_pending: 0x%x\n",
8822 WINID(ws->focus_pending));
8828 unmanage_window(win);
8833 if (focus_mode == SWM_FOCUS_FOLLOW) {
8835 focus_win(get_pointer_win(ws->r->s->root));
8836 } else if (ws->focus_pending) {
8837 focus_win(ws->focus_pending);
8838 ws->focus_pending = NULL;
8839 } else if (ws->focus == NULL && ws->r) {
8840 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT,
8841 ws->r->id, XCB_CURRENT_TIME);
8845 if (getstate(e->window) == XCB_ICCCM_WM_STATE_NORMAL)
8846 set_win_state(win, XCB_ICCCM_WM_STATE_ICONIC);
8853 visibilitynotify(xcb_visibility_notify_event_t *e)
8855 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: 0x%x\n",
8861 clientmessage(xcb_client_message_event_t *e)
8864 xcb_map_request_event_t mre;
8868 name = get_atom_name(e->type);
8869 DNPRINTF(SWM_D_EVENT, "clientmessage: window: 0x%x, atom: %s(%u)\n",
8870 e->window, name, e->type);
8873 win = find_window(e->window);
8876 if (e->type == ewmh[_NET_ACTIVE_WINDOW].atom) {
8877 /* Manage the window with maprequest. */
8878 DNPRINTF(SWM_D_EVENT, "clientmessage: request focus on "
8879 "unmanaged window.\n");
8880 mre.window = e->window;
8886 if (e->type == ewmh[_NET_ACTIVE_WINDOW].atom) {
8887 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_ACTIVE_WINDOW\n");
8890 if (e->type == ewmh[_NET_CLOSE_WINDOW].atom) {
8891 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_CLOSE_WINDOW\n");
8892 if (win->can_delete)
8893 client_msg(win, a_delete, 0);
8895 xcb_kill_client(conn, win->id);
8897 if (e->type == ewmh[_NET_MOVERESIZE_WINDOW].atom) {
8898 DNPRINTF(SWM_D_EVENT,
8899 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
8900 if (win->floating) {
8901 if (e->data.data32[0] & (1<<8)) /* x */
8902 X(win) = e->data.data32[1];
8903 if (e->data.data32[0] & (1<<9)) /* y */
8904 Y(win) = e->data.data32[2];
8905 if (e->data.data32[0] & (1<<10)) /* width */
8906 WIDTH(win) = e->data.data32[3];
8907 if (e->data.data32[0] & (1<<11)) /* height */
8908 HEIGHT(win) = e->data.data32[4];
8913 /* TODO: Change stack sizes */
8914 /* notify no change was made. */
8915 config_win(win, NULL);
8918 if (e->type == ewmh[_NET_WM_STATE].atom) {
8919 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_WM_STATE\n");
8920 ewmh_update_win_state(win, e->data.data32[1], e->data.data32[0]);
8921 if (e->data.data32[2])
8922 ewmh_update_win_state(win, e->data.data32[2],
8934 int errcode = xcb_connection_has_error(conn);
8935 #ifdef XCB_CONN_ERROR
8938 case XCB_CONN_ERROR:
8939 s = "Socket error, pipe error or other stream error.";
8941 case XCB_CONN_CLOSED_EXT_NOTSUPPORTED:
8942 s = "Extension not supported.";
8944 case XCB_CONN_CLOSED_MEM_INSUFFICIENT:
8945 s = "Insufficient memory.";
8947 case XCB_CONN_CLOSED_REQ_LEN_EXCEED:
8948 s = "Request length was exceeded.";
8950 case XCB_CONN_CLOSED_PARSE_ERR:
8951 s = "Error parsing display string.";
8954 s = "Unknown error.";
8957 errx(errcode, "X CONNECTION ERROR: %s", s);
8960 errx(errcode, "X CONNECTION ERROR");
8968 const uint32_t val = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
8969 XCB_EVENT_MASK_ENTER_WINDOW;
8971 xcb_void_cookie_t wac;
8972 xcb_generic_error_t *error;
8974 /* this causes an error if some other window manager is running */
8975 num_screens = get_screen_count();
8976 for (i = 0; i < num_screens; i++) {
8977 if ((sc = get_screen(i)) == NULL)
8978 errx(1, "ERROR: can't get screen %d.", i);
8979 DNPRINTF(SWM_D_INIT, "enable_wm: screen %d, root: 0x%x\n",
8981 wac = xcb_change_window_attributes_checked(conn, sc->root,
8982 XCB_CW_EVENT_MASK, &val);
8983 if ((error = xcb_request_check(conn, wac))) {
8984 DNPRINTF(SWM_D_INIT, "enable_wm: error_code: %u\n",
8990 /* click to focus on empty region */
8991 xcb_grab_button(conn, 1, sc->root, BUTTONMASK,
8992 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC, XCB_WINDOW_NONE,
8993 XCB_CURSOR_NONE, XCB_BUTTON_INDEX_1, XCB_BUTTON_MASK_ANY);
9000 new_region(struct swm_screen *s, int x, int y, int w, int h)
9002 struct swm_region *r, *n;
9003 struct workspace *ws = NULL;
9007 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
9008 s->idx, w, h, x, y);
9010 /* remove any conflicting regions */
9011 n = TAILQ_FIRST(&s->rl);
9014 n = TAILQ_NEXT(r, entry);
9015 if (X(r) < (x + w) && (X(r) + WIDTH(r)) > x &&
9016 Y(r) < (y + h) && (Y(r) + HEIGHT(r)) > y) {
9017 if (r->ws->r != NULL)
9018 r->ws->old_r = r->ws->r;
9021 xcb_destroy_window(conn, r->id);
9022 TAILQ_REMOVE(&s->rl, r, entry);
9023 TAILQ_INSERT_TAIL(&s->orl, r, entry);
9027 /* search old regions for one to reuse */
9029 /* size + location match */
9030 TAILQ_FOREACH(r, &s->orl, entry)
9031 if (X(r) == x && Y(r) == y &&
9032 HEIGHT(r) == h && WIDTH(r) == w)
9036 TAILQ_FOREACH(r, &s->orl, entry)
9037 if (HEIGHT(r) == h && WIDTH(r) == w)
9041 TAILQ_REMOVE(&s->orl, r, entry);
9042 /* try to use old region's workspace */
9043 if (r->ws->r == NULL)
9046 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
9047 err(1, "new_region: calloc: failed to allocate memory "
9050 /* if we don't have a workspace already, find one */
9052 for (i = 0; i < workspace_limit; i++)
9053 if (s->ws[i].r == NULL) {
9060 errx(1, "new_region: no free workspaces");
9071 TAILQ_INSERT_TAIL(&s->rl, r, entry);
9073 /* Invisible region window to detect pointer events on empty regions. */
9074 r->id = xcb_generate_id(conn);
9075 wa[0] = XCB_EVENT_MASK_POINTER_MOTION |
9076 XCB_EVENT_MASK_POINTER_MOTION_HINT;
9078 xcb_create_window(conn, XCB_COPY_FROM_PARENT, r->id, r->s->root,
9079 X(r), Y(r), WIDTH(r), HEIGHT(r), 0, XCB_WINDOW_CLASS_INPUT_ONLY,
9080 XCB_COPY_FROM_PARENT, XCB_CW_EVENT_MASK, wa);
9082 xcb_map_window(conn, r->id);
9088 #ifdef SWM_XRR_HAS_CRTC
9091 #endif /* SWM_XRR_HAS_CRTC */
9092 struct swm_region *r;
9095 xcb_randr_get_screen_resources_current_cookie_t src;
9096 xcb_randr_get_screen_resources_current_reply_t *srr;
9097 xcb_randr_get_crtc_info_cookie_t cic;
9098 xcb_randr_get_crtc_info_reply_t *cir = NULL;
9099 xcb_randr_crtc_t *crtc;
9100 xcb_screen_t *screen;
9102 DNPRINTF(SWM_D_MISC, "scan_xrandr: screen: %d\n", i);
9104 if ((screen = get_screen(i)) == NULL)
9105 errx(1, "ERROR: can't get screen %d.", i);
9107 num_screens = get_screen_count();
9108 if (i >= num_screens)
9109 errx(1, "scan_xrandr: invalid screen");
9111 /* remove any old regions */
9112 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
9113 r->ws->old_r = r->ws->r = NULL;
9115 xcb_destroy_window(conn, r->id);
9116 TAILQ_REMOVE(&screens[i].rl, r, entry);
9117 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
9121 /* map virtual screens onto physical screens */
9122 #ifdef SWM_XRR_HAS_CRTC
9123 if (xrandr_support) {
9124 src = xcb_randr_get_screen_resources_current(conn,
9126 srr = xcb_randr_get_screen_resources_current_reply(conn, src,
9129 new_region(&screens[i], 0, 0,
9130 screen->width_in_pixels,
9131 screen->height_in_pixels);
9134 ncrtc = srr->num_crtcs;
9136 crtc = xcb_randr_get_screen_resources_current_crtcs(srr);
9137 for (c = 0; c < ncrtc; c++) {
9138 cic = xcb_randr_get_crtc_info(conn, crtc[c],
9140 cir = xcb_randr_get_crtc_info_reply(conn, cic, NULL);
9143 if (cir->num_outputs == 0) {
9149 new_region(&screens[i], 0, 0,
9150 screen->width_in_pixels,
9151 screen->height_in_pixels);
9153 new_region(&screens[i],
9154 cir->x, cir->y, cir->width, cir->height);
9159 #endif /* SWM_XRR_HAS_CRTC */
9161 /* If detection failed, create a single region that spans the screen. */
9162 if (TAILQ_EMPTY(&screens[i].rl))
9163 new_region(&screens[i], 0, 0, screen->width_in_pixels,
9164 screen->height_in_pixels);
9167 /* Cleanup unused previously visible workspaces. */
9168 TAILQ_FOREACH(r, &screens[i].orl, entry) {
9169 TAILQ_FOREACH(win, &r->ws->winlist, entry)
9172 /* The screen shouldn't focus on an unused region. */
9173 if (screens[i].r_focus == r)
9174 screens[i].r_focus = NULL;
9177 DNPRINTF(SWM_D_MISC, "scan_xrandr: done.\n");
9181 screenchange(xcb_randr_screen_change_notify_event_t *e)
9183 struct swm_region *r;
9186 DNPRINTF(SWM_D_EVENT, "screenchange: root: 0x%x\n", e->root);
9188 num_screens = get_screen_count();
9189 /* silly event doesn't include the screen index */
9190 for (i = 0; i < num_screens; i++)
9191 if (screens[i].root == e->root)
9193 if (i >= num_screens)
9194 errx(1, "screenchange: screen not found");
9196 /* brute force for now, just re-enumerate the regions */
9200 print_win_geom(e->root);
9202 /* add bars to all regions */
9203 for (i = 0; i < num_screens; i++) {
9204 TAILQ_FOREACH(r, &screens[i].rl, entry)
9210 /* Make sure a region has focus on each screen. */
9211 for (i = 0; i < num_screens; i++) {
9212 if (screens[i].r_focus == NULL) {
9213 r = TAILQ_FIRST(&screens[i].rl);
9226 struct swm_region *r = NULL;
9227 xcb_window_t *wins = NULL, trans;
9229 int i, j, num_screens;
9230 uint16_t state, manage, mapped;
9232 xcb_query_tree_cookie_t qtc;
9233 xcb_query_tree_reply_t *qtr;
9234 xcb_get_window_attributes_cookie_t gac;
9235 xcb_get_window_attributes_reply_t *gar;
9236 xcb_get_property_cookie_t pc;
9238 DNPRINTF(SWM_D_INIT, "grab_windows: begin\n");
9239 num_screens = get_screen_count();
9240 for (i = 0; i < num_screens; i++) {
9241 qtc = xcb_query_tree(conn, screens[i].root);
9242 qtr = xcb_query_tree_reply(conn, qtc, NULL);
9245 wins = xcb_query_tree_children(qtr);
9246 no = xcb_query_tree_children_length(qtr);
9247 /* attach windows to a region */
9248 /* normal windows */
9249 DNPRINTF(SWM_D_INIT, "grab_windows: grab top level windows.\n");
9250 for (j = 0; j < no; j++) {
9251 TAILQ_FOREACH(r, &screens[i].rl, entry) {
9252 if (r->id == wins[j]) {
9253 DNPRINTF(SWM_D_INIT, "grab_windows: "
9254 "skip %#x; region input window.\n",
9263 gac = xcb_get_window_attributes(conn, wins[j]);
9264 gar = xcb_get_window_attributes_reply(conn, gac, NULL);
9266 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
9267 "doesn't exist.\n", wins[j]);
9271 if (gar->override_redirect) {
9272 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
9273 "override_redirect set.\n", wins[j]);
9278 pc = xcb_icccm_get_wm_transient_for(conn, wins[j]);
9279 if (xcb_icccm_get_wm_transient_for_reply(conn, pc,
9281 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
9282 "is transient for %#x.\n", wins[j], trans);
9287 state = getstate(wins[j]);
9288 manage = state != XCB_ICCCM_WM_STATE_WITHDRAWN;
9289 mapped = gar->map_state != XCB_MAP_STATE_UNMAPPED;
9290 if (mapped || manage)
9291 manage_window(wins[j], mapped);
9294 /* transient windows */
9295 DNPRINTF(SWM_D_INIT, "grab_windows: grab transient windows.\n");
9296 for (j = 0; j < no; j++) {
9297 gac = xcb_get_window_attributes(conn, wins[j]);
9298 gar = xcb_get_window_attributes_reply(conn, gac, NULL);
9300 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
9301 "doesn't exist.\n", wins[j]);
9305 if (gar->override_redirect) {
9306 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
9307 "override_redirect set.\n", wins[j]);
9312 state = getstate(wins[j]);
9313 manage = state != XCB_ICCCM_WM_STATE_WITHDRAWN;
9314 mapped = gar->map_state != XCB_MAP_STATE_UNMAPPED;
9315 pc = xcb_icccm_get_wm_transient_for(conn, wins[j]);
9316 if (xcb_icccm_get_wm_transient_for_reply(conn, pc,
9317 &trans, NULL) && manage)
9318 manage_window(wins[j], mapped);
9323 DNPRINTF(SWM_D_INIT, "grab_windows: done.\n");
9329 int i, j, k, num_screens;
9330 struct workspace *ws;
9331 uint32_t gcv[1], wa[1];
9332 const xcb_query_extension_reply_t *qep;
9333 xcb_screen_t *screen;
9334 xcb_randr_query_version_cookie_t c;
9335 xcb_randr_query_version_reply_t *r;
9337 num_screens = get_screen_count();
9338 if ((screens = calloc(num_screens,
9339 sizeof(struct swm_screen))) == NULL)
9340 err(1, "setup_screens: calloc: failed to allocate memory for "
9343 /* initial Xrandr setup */
9345 qep = xcb_get_extension_data(conn, &xcb_randr_id);
9347 c = xcb_randr_query_version(conn, 1, 1);
9348 r = xcb_randr_query_version_reply(conn, c, NULL);
9350 if (r->major_version >= 1) {
9352 xrandr_eventbase = qep->first_event;
9358 wa[0] = cursors[XC_LEFT_PTR].cid;
9360 /* map physical screens */
9361 for (i = 0; i < num_screens; i++) {
9362 DNPRINTF(SWM_D_WS, "setup_screens: init screen: %d\n", i);
9364 screens[i].r_focus = NULL;
9366 TAILQ_INIT(&screens[i].rl);
9367 TAILQ_INIT(&screens[i].orl);
9368 if ((screen = get_screen(i)) == NULL)
9369 errx(1, "ERROR: can't get screen %d.", i);
9370 screens[i].root = screen->root;
9372 /* set default colors */
9373 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
9374 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
9375 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
9376 setscreencolor("rgb:00/40/40", i + 1,
9377 SWM_S_COLOR_BAR_BORDER_UNFOCUS);
9378 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
9379 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
9381 /* create graphics context on screen */
9382 screens[i].bar_gc = xcb_generate_id(conn);
9384 xcb_create_gc(conn, screens[i].bar_gc, screens[i].root,
9385 XCB_GC_GRAPHICS_EXPOSURES, gcv);
9387 /* set default cursor */
9388 xcb_change_window_attributes(conn, screens[i].root,
9391 /* init all workspaces */
9392 /* XXX these should be dynamically allocated too */
9393 for (j = 0; j < SWM_WS_MAX; j++) {
9394 ws = &screens[i].ws[j];
9397 ws->bar_enabled = 1;
9399 ws->focus_prev = NULL;
9400 ws->focus_pending = NULL;
9403 TAILQ_INIT(&ws->winlist);
9404 TAILQ_INIT(&ws->unmanagedlist);
9406 for (k = 0; layouts[k].l_stack != NULL; k++)
9407 if (layouts[k].l_config != NULL)
9408 layouts[k].l_config(ws,
9409 SWM_ARG_ID_STACKINIT);
9410 ws->cur_layout = &layouts[0];
9411 ws->cur_layout->l_string(ws);
9417 xcb_randr_select_input(conn, screens[i].root,
9418 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE);
9425 if ((bar_fonts = strdup(SWM_BAR_FONTS)) == NULL)
9426 err(1, "setup_globals: strdup: failed to allocate memory.");
9428 if ((clock_format = strdup("%a %b %d %R %Z %Y")) == NULL)
9429 err(1, "setup_globals: strdup: failed to allocate memory.");
9431 if ((syms = xcb_key_symbols_alloc(conn)) == NULL)
9432 errx(1, "unable to allocate key symbols");
9434 a_state = get_atom_from_string("WM_STATE");
9435 a_prot = get_atom_from_string("WM_PROTOCOLS");
9436 a_delete = get_atom_from_string("WM_DELETE_WINDOW");
9437 a_takefocus = get_atom_from_string("WM_TAKE_FOCUS");
9438 a_utf8_string = get_atom_from_string("UTF8_STRING");
9439 a_swm_iconic = get_atom_from_string("_SWM_ICONIC");
9440 a_swm_ws = get_atom_from_string("_SWM_WS");
9447 xcb_atom_t netwmcheck;
9448 xcb_window_t root, win;
9450 /* work around sun jdk bugs, code from wmname */
9451 netwmcheck = get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
9453 num_screens = get_screen_count();
9454 for (i = 0; i < num_screens; i++) {
9455 root = screens[i].root;
9457 win = xcb_generate_id(conn);
9458 xcb_create_window(conn, XCB_COPY_FROM_PARENT, win, root,
9459 0, 0, 1, 1, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT,
9460 XCB_COPY_FROM_PARENT, 0, NULL);
9462 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root,
9463 netwmcheck, XCB_ATOM_WINDOW, 32, 1, &win);
9464 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win,
9465 netwmcheck, XCB_ATOM_WINDOW, 32, 1, &win);
9466 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win,
9467 ewmh[_NET_WM_NAME].atom, a_utf8_string, 8, strlen("LG3D"),
9473 shutdown_cleanup(void)
9477 /* disable alarm because the following code may not be interrupted */
9479 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
9480 err(1, "can't disable alarm");
9489 num_screens = get_screen_count();
9490 for (i = 0; i < num_screens; ++i) {
9491 if (screens[i].bar_gc != 0)
9492 xcb_free_gc(conn, screens[i].bar_gc);
9493 if (!bar_font_legacy)
9494 XftColorFree(display, DefaultVisual(display, i),
9495 DefaultColormap(display, i), &bar_font_color);
9498 if (bar_font_legacy)
9499 XFreeFontSet(display, bar_fs);
9501 XftFontClose(display, bar_font);
9504 xcb_key_symbols_free(syms);
9506 xcb_disconnect(conn);
9510 event_error(xcb_generic_error_t *e)
9514 DNPRINTF(SWM_D_EVENT, "event_error: %s(%u) from %s(%u), sequence: %u, "
9515 "resource_id: %u, minor_code: %u\n",
9516 xcb_event_get_error_label(e->error_code), e->error_code,
9517 xcb_event_get_request_label(e->major_code), e->major_code,
9518 e->sequence, e->resource_id, e->minor_code);
9522 event_handle(xcb_generic_event_t *evt)
9524 uint8_t type = XCB_EVENT_RESPONSE_TYPE(evt);
9526 DNPRINTF(SWM_D_EVENT, "XCB Event: %s(%d), seq %u\n",
9527 xcb_event_get_label(XCB_EVENT_RESPONSE_TYPE(evt)),
9528 XCB_EVENT_RESPONSE_TYPE(evt), evt->sequence);
9531 #define EVENT(type, callback) case type: callback((void *)evt); return
9532 EVENT(0, event_error);
9533 EVENT(XCB_BUTTON_PRESS, buttonpress);
9534 /*EVENT(XCB_BUTTON_RELEASE, buttonpress);*/
9535 /*EVENT(XCB_CIRCULATE_NOTIFY, );*/
9536 /*EVENT(XCB_CIRCULATE_REQUEST, );*/
9537 EVENT(XCB_CLIENT_MESSAGE, clientmessage);
9538 /*EVENT(XCB_COLORMAP_NOTIFY, );*/
9539 EVENT(XCB_CONFIGURE_NOTIFY, configurenotify);
9540 EVENT(XCB_CONFIGURE_REQUEST, configurerequest);
9541 /*EVENT(XCB_CREATE_NOTIFY, );*/
9542 EVENT(XCB_DESTROY_NOTIFY, destroynotify);
9543 EVENT(XCB_ENTER_NOTIFY, enternotify);
9544 EVENT(XCB_EXPOSE, expose);
9546 EVENT(XCB_FOCUS_IN, focusin);
9547 EVENT(XCB_FOCUS_OUT, focusout);
9549 /*EVENT(XCB_GRAPHICS_EXPOSURE, );*/
9550 /*EVENT(XCB_GRAVITY_NOTIFY, );*/
9551 EVENT(XCB_KEY_PRESS, keypress);
9552 /*EVENT(XCB_KEY_RELEASE, keypress);*/
9553 /*EVENT(XCB_KEYMAP_NOTIFY, );*/
9555 EVENT(XCB_LEAVE_NOTIFY, leavenotify);
9557 EVENT(XCB_MAP_NOTIFY, mapnotify);
9558 EVENT(XCB_MAP_REQUEST, maprequest);
9559 EVENT(XCB_MAPPING_NOTIFY, mappingnotify);
9560 EVENT(XCB_MOTION_NOTIFY, motionnotify);
9561 /*EVENT(XCB_NO_EXPOSURE, );*/
9562 EVENT(XCB_PROPERTY_NOTIFY, propertynotify);
9563 /*EVENT(XCB_REPARENT_NOTIFY, );*/
9564 /*EVENT(XCB_RESIZE_REQUEST, );*/
9565 /*EVENT(XCB_SELECTION_CLEAR, );*/
9566 /*EVENT(XCB_SELECTION_NOTIFY, );*/
9567 /*EVENT(XCB_SELECTION_REQUEST, );*/
9568 EVENT(XCB_UNMAP_NOTIFY, unmapnotify);
9569 /*EVENT(XCB_VISIBILITY_NOTIFY, visibilitynotify);*/
9572 if (type - xrandr_eventbase == XCB_RANDR_SCREEN_CHANGE_NOTIFY)
9573 screenchange((void *)evt);
9577 main(int argc, char *argv[])
9579 struct swm_region *r;
9580 char conf[PATH_MAX], *cfile = NULL;
9582 int xfd, i, num_screens, startup = 1;
9583 struct sigaction sact;
9584 xcb_generic_event_t *evt;
9588 int stdin_ready = 0;
9591 /* suppress unused warning since var is needed */
9594 time_started = time(NULL);
9597 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION, buildstr);
9598 if (!setlocale(LC_CTYPE, "") || !setlocale(LC_TIME, ""))
9599 warnx("no locale support");
9601 /* handle some signals */
9602 bzero(&sact, sizeof(sact));
9603 sigemptyset(&sact.sa_mask);
9605 sact.sa_handler = sighdlr;
9606 sigaction(SIGINT, &sact, NULL);
9607 sigaction(SIGQUIT, &sact, NULL);
9608 sigaction(SIGTERM, &sact, NULL);
9609 sigaction(SIGHUP, &sact, NULL);
9611 sact.sa_handler = sighdlr;
9612 sact.sa_flags = SA_NOCLDSTOP;
9613 sigaction(SIGCHLD, &sact, NULL);
9615 if (!(display = XOpenDisplay(0)))
9616 errx(1, "can not open display");
9618 conn = XGetXCBConnection(display);
9619 if (xcb_connection_has_error(conn))
9620 errx(1, "can not get XCB connection");
9622 XSetEventQueueOwner(display, XCBOwnsEventQueue);
9624 xcb_prefetch_extension_data(conn, &xcb_randr_id);
9625 xfd = xcb_get_file_descriptor(conn);
9627 /* look for local and global conf file */
9628 pwd = getpwuid(getuid());
9630 errx(1, "invalid user: %d", getuid());
9632 xcb_grab_server(conn);
9635 /* flush all events */
9636 while ((evt = xcb_poll_for_event(conn))) {
9637 if (XCB_EVENT_RESPONSE_TYPE(evt) == 0)
9642 if (enable_wm() != 0)
9643 errx(1, "another window manager is currently running");
9645 /* Load Xcursors and/or cursorfont glyph cursors. */
9657 for (i = 0; ; i++) {
9662 snprintf(conf, sizeof conf, "%s/.%s",
9663 pwd->pw_dir, SWM_CONF_FILE);
9667 snprintf(conf, sizeof conf, "/etc/%s",
9672 snprintf(conf, sizeof conf, "%s/.%s",
9673 pwd->pw_dir, SWM_CONF_FILE_OLD);
9677 snprintf(conf, sizeof conf, "/etc/%s",
9684 if (strlen(conf) && stat(conf, &sb) != -1)
9685 if (S_ISREG(sb.st_mode)) {
9692 /* load conf (if any) */
9694 conf_load(cfile, SWM_CONF_DEFAULT);
9699 /* set some values to work around bad programs */
9701 /* grab existing windows (before we build the bars) */
9704 if (getenv("SWM_STARTED") == NULL)
9705 setenv("SWM_STARTED", "YES", 1);
9707 /* setup all bars */
9708 num_screens = get_screen_count();
9709 for (i = 0; i < num_screens; i++)
9710 TAILQ_FOREACH(r, &screens[i].rl, entry)
9717 xcb_ungrab_server(conn);
9720 rd_max = xfd > STDIN_FILENO ? xfd : STDIN_FILENO;
9723 while ((evt = xcb_poll_for_event(conn))) {
9730 /* If just (re)started, set default focus if needed. */
9734 if (focus_mode != SWM_FOCUS_FOLLOW) {
9735 r = TAILQ_FIRST(&screens[0].rl);
9747 FD_SET(STDIN_FILENO, &rd);
9752 num_readable = select(rd_max + 1, &rd, NULL, NULL, &tv);
9753 if (num_readable == -1 && errno != EINTR) {
9754 DNPRINTF(SWM_D_MISC, "select failed");
9755 } else if (num_readable > 0 && FD_ISSET(STDIN_FILENO, &rd)) {
9760 restart(NULL, NULL);