2 * Copyright (c) 2009-2012 Marco Peereboom <marco@peereboom.us>
3 * Copyright (c) 2009-2011 Ryan McBride <mcbride@countersiege.com>
4 * Copyright (c) 2009 Darrin Chandler <dwchandler@stilyagin.com>
5 * Copyright (c) 2009 Pierre-Yves Ritschard <pyr@spootnik.org>
6 * Copyright (c) 2010 Tuukka Kataja <stuge@xor.fi>
7 * Copyright (c) 2011 Jason L. Wright <jason@thought.net>
8 * Copyright (c) 2011-2012 Reginald Kennedy <rk@rejii.com>
9 * Copyright (c) 2011-2012 Lawrence Teo <lteo@lteo.net>
10 * Copyright (c) 2011-2012 Tiago Cunha <tcunha@gmx.com>
11 * Copyright (c) 2012 David Hill <dhill@mindcry.org>
13 * Permission to use, copy, modify, and distribute this software for any
14 * purpose with or without fee is hereby granted, provided that the above
15 * copyright notice and this permission notice appear in all copies.
17 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
18 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
20 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
21 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
22 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
23 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 * Much code and ideas taken from dwm under the following license:
27 * MIT/X Consortium License
29 * 2006-2008 Anselm R Garbe <garbeam at gmail dot com>
30 * 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
31 * 2006-2007 Jukka Salmi <jukka at salmi dot ch>
32 * 2007 Premysl Hruby <dfenze at gmail dot com>
33 * 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
34 * 2007 Christof Musik <christof at sendfax dot de>
35 * 2007-2008 Enno Gottox Boland <gottox at s01 dot de>
36 * 2007-2008 Peter Hartlich <sgkkr at hartlich dot com>
37 * 2008 Martin Hurton <martin dot hurton at gmail dot com>
39 * Permission is hereby granted, free of charge, to any person obtaining a
40 * copy of this software and associated documentation files (the "Software"),
41 * to deal in the Software without restriction, including without limitation
42 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
43 * and/or sell copies of the Software, and to permit persons to whom the
44 * Software is furnished to do so, subject to the following conditions:
46 * The above copyright notice and this permission notice shall be included in
47 * all copies or substantial portions of the Software.
49 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
50 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
51 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
52 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
53 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
54 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
55 * DEALINGS IN THE SOFTWARE.
59 #include <sys/types.h>
63 #include <sys/queue.h>
64 #include <sys/param.h>
65 #include <sys/select.h>
66 #if defined(__linux__)
68 #elif defined(__OpenBSD__)
70 #elif defined(__FreeBSD__)
91 #include <X11/cursorfont.h>
92 #include <X11/extensions/Xrandr.h>
93 #include <X11/Xft/Xft.h>
94 #include <X11/Xlib-xcb.h>
95 #include <xcb/xcb_atom.h>
96 #include <xcb/xcb_aux.h>
97 #include <xcb/xcb_event.h>
98 #include <xcb/xcb_icccm.h>
99 #include <xcb/xcb_keysyms.h>
100 #include <xcb/xtest.h>
101 #include <xcb/randr.h>
109 #ifdef SPECTRWM_BUILDSTR
110 static const char *buildstr = SPECTRWM_BUILDSTR;
112 static const char *buildstr = SPECTRWM_VERSION;
115 #if !defined(__CYGWIN__) /* cygwin chokes on xrandr stuff */
117 # error XRandR versions less than 1.0 are not supported
120 # if RANDR_MAJOR >= 1
121 # if RANDR_MINOR >= 2
122 # define SWM_XRR_HAS_CRTC
125 #endif /* __CYGWIN__ */
127 #ifndef XCB_ICCCM_NUM_WM_HINTS_ELEMENTS
128 #define XCB_ICCCM_SIZE_HINT_P_MIN_SIZE XCB_SIZE_HINT_P_MIN_SIZE
129 #define XCB_ICCCM_SIZE_HINT_P_MAX_SIZE XCB_SIZE_HINT_P_MAX_SIZE
130 #define XCB_ICCCM_SIZE_HINT_P_RESIZE_INC XCB_SIZE_HINT_P_RESIZE_INC
131 #define XCB_ICCCM_WM_HINT_INPUT XCB_WM_HINT_INPUT
132 #define XCB_ICCCM_WM_HINT_X_URGENCY XCB_WM_HINT_X_URGENCY
133 #define XCB_ICCCM_WM_STATE_ICONIC XCB_WM_STATE_ICONIC
134 #define XCB_ICCCM_WM_STATE_WITHDRAWN XCB_WM_STATE_WITHDRAWN
135 #define XCB_ICCCM_WM_STATE_NORMAL XCB_WM_STATE_NORMAL
136 #define xcb_icccm_get_text_property_reply_t xcb_get_text_property_reply_t
137 #define xcb_icccm_get_text_property_reply_wipe xcb_get_text_property_reply_wipe
138 #define xcb_icccm_get_wm_class xcb_get_wm_class
139 #define xcb_icccm_get_wm_class_reply xcb_get_wm_class_reply
140 #define xcb_icccm_get_wm_class_reply_t xcb_get_wm_class_reply_t
141 #define xcb_icccm_get_wm_class_reply_wipe xcb_get_wm_class_reply_wipe
142 #define xcb_icccm_get_wm_hints xcb_get_wm_hints
143 #define xcb_icccm_get_wm_hints_reply xcb_get_wm_hints_reply
144 #define xcb_icccm_get_wm_name xcb_get_wm_name
145 #define xcb_icccm_get_wm_name_reply xcb_get_wm_name_reply
146 #define xcb_icccm_get_wm_normal_hints xcb_get_wm_normal_hints
147 #define xcb_icccm_get_wm_normal_hints_reply xcb_get_wm_normal_hints_reply
148 #define xcb_icccm_get_wm_protocols xcb_get_wm_protocols
149 #define xcb_icccm_get_wm_protocols_reply xcb_get_wm_protocols_reply
150 #define xcb_icccm_get_wm_protocols_reply_t xcb_get_wm_protocols_reply_t
151 #define xcb_icccm_get_wm_protocols_reply_wipe xcb_get_wm_protocols_reply_wipe
152 #define xcb_icccm_get_wm_transient_for xcb_get_wm_transient_for
153 #define xcb_icccm_get_wm_transient_for_reply xcb_get_wm_transient_for_reply
154 #define xcb_icccm_wm_hints_t xcb_wm_hints_t
157 /*#define SWM_DEBUG*/
159 #define DPRINTF(x...) do { \
161 fprintf(stderr, x); \
163 #define DNPRINTF(n,x...) do { \
164 if (swm_debug & n) { \
165 fprintf(stderr, "%ld ", (long)(time(NULL) - time_started)); \
166 fprintf(stderr, x); \
169 #define SWM_D_MISC 0x0001
170 #define SWM_D_EVENT 0x0002
171 #define SWM_D_WS 0x0004
172 #define SWM_D_FOCUS 0x0008
173 #define SWM_D_MOVE 0x0010
174 #define SWM_D_STACK 0x0020
175 #define SWM_D_MOUSE 0x0040
176 #define SWM_D_PROP 0x0080
177 #define SWM_D_CLASS 0x0100
178 #define SWM_D_KEY 0x0200
179 #define SWM_D_QUIRK 0x0400
180 #define SWM_D_SPAWN 0x0800
181 #define SWM_D_EVENTQ 0x1000
182 #define SWM_D_CONF 0x2000
183 #define SWM_D_BAR 0x4000
184 #define SWM_D_INIT 0x8000
186 u_int32_t swm_debug = 0
205 #define DPRINTF(x...)
206 #define DNPRINTF(n,x...)
209 /* convert 8-bit to 16-bit */
210 #define RGB_8_TO_16(col) ((col) << 8) + (col)
212 #define PIXEL_TO_XRENDERCOLOR(px, xrc) \
213 xrc.red = RGB_8_TO_16((px) >> 16 & 0xff); \
214 xrc.green = RGB_8_TO_16((px) >> 8 & 0xff); \
215 xrc.blue = RGB_8_TO_16((px) & 0xff); \
218 #define LENGTH(x) (int)(sizeof (x) / sizeof (x)[0])
219 #define MODKEY XCB_MOD_MASK_1
220 #define CLEANMASK(mask) ((mask) & ~(numlockmask | XCB_MOD_MASK_LOCK))
221 #define BUTTONMASK (XCB_EVENT_MASK_BUTTON_PRESS | \
222 XCB_EVENT_MASK_BUTTON_RELEASE)
223 #define MOUSEMASK (BUTTONMASK|XCB_EVENT_MASK_POINTER_MOTION)
224 #define SWM_PROPLEN (16)
225 #define SWM_FUNCNAME_LEN (32)
226 #define SWM_KEYS_LEN (255)
227 #define SWM_QUIRK_LEN (64)
228 #define X(r) (r)->g.x
229 #define Y(r) (r)->g.y
230 #define WIDTH(r) (r)->g.w
231 #define HEIGHT(r) (r)->g.h
232 #define BORDER(w) ((w)->bordered ? border_width : 0)
233 #define MAX_X(r) ((r)->g.x + (r)->g.w)
234 #define MAX_Y(r) ((r)->g.y + (r)->g.h)
235 #define SH_MIN(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE
236 #define SH_MIN_W(w) (w)->sh.min_width
237 #define SH_MIN_H(w) (w)->sh.min_height
238 #define SH_MAX(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MAX_SIZE
239 #define SH_MAX_W(w) (w)->sh.max_width
240 #define SH_MAX_H(w) (w)->sh.max_height
241 #define SH_INC(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_RESIZE_INC
242 #define SH_INC_W(w) (w)->sh.width_inc
243 #define SH_INC_H(w) (w)->sh.height_inc
244 #define SWM_MAX_FONT_STEPS (3)
245 #define WINID(w) ((w) ? (w)->id : 0)
246 #define YESNO(x) ((x) ? "yes" : "no")
248 #define SWM_FOCUS_DEFAULT (0)
249 #define SWM_FOCUS_FOLLOW (1)
250 #define SWM_FOCUS_MANUAL (2)
252 #define SWM_CK_NONE 0
253 #define SWM_CK_ALL 0x7
254 #define SWM_CK_FOCUS 0x1
255 #define SWM_CK_POINTER 0x2
256 #define SWM_CK_FALLBACK 0x4
258 #define SWM_CONF_DEFAULT (0)
259 #define SWM_CONF_KEYMAPPING (1)
262 #define SWM_LIB "/usr/local/lib/libswmhack.so"
269 xcb_atom_t a_takefocus;
271 xcb_atom_t a_netwmname;
272 xcb_atom_t a_utf8_string;
274 xcb_atom_t a_swm_iconic;
276 volatile sig_atomic_t running = 1;
277 volatile sig_atomic_t restart_wm = 0;
278 xcb_timestamp_t last_event_time = 0;
283 int xrandr_eventbase;
284 unsigned int numlockmask = 0;
287 xcb_connection_t *conn;
288 xcb_key_symbols_t *syms;
291 int cycle_visible = 0;
293 int font_adjusted = 0;
294 unsigned int mod_key = MODKEY;
297 struct swm_region *search_r;
298 int select_list_pipe[2];
299 int select_resp_pipe[2];
301 volatile sig_atomic_t search_resp;
302 int search_resp_action;
304 struct search_window {
305 TAILQ_ENTRY(search_window) entry;
309 xcb_window_t indicator;
311 TAILQ_HEAD(search_winlist, search_window);
312 struct search_winlist search_wl;
317 SWM_SEARCH_UNICONIFY,
318 SWM_SEARCH_NAME_WORKSPACE,
319 SWM_SEARCH_SEARCH_WORKSPACE,
320 SWM_SEARCH_SEARCH_WINDOW
323 #define SWM_STACK_TOP (0)
324 #define SWM_STACK_BOTTOM (1)
325 #define SWM_STACK_ABOVE (2)
326 #define SWM_STACK_BELOW (3)
329 double dialog_ratio = 0.6;
331 #define SWM_BAR_MAX (256)
332 #define SWM_BAR_JUSTIFY_LEFT (0)
333 #define SWM_BAR_JUSTIFY_CENTER (1)
334 #define SWM_BAR_JUSTIFY_RIGHT (2)
335 #define SWM_BAR_OFFSET (4)
336 #define SWM_BAR_FONTS "-*-terminus-medium-*-*-*-12-*-*-*-*-*-*-*," \
337 "-*-profont-*-*-*-*-12-*-*-*-*-*-*-*," \
338 "-*-times-medium-r-*-*-12-*-*-*-*-*-*-*," \
339 "-misc-fixed-medium-r-*-*-12-*-*-*-*-*-*-*," \
340 "-*-*-*-r-*-*-*-*-*-*-*-*-*-*"
342 #ifdef X_HAVE_UTF8_STRING
343 #define DRAWSTRING(x...) Xutf8DrawString(x)
345 #define DRAWSTRING(x...) XmbDrawString(x)
348 char *bar_argv[] = { NULL, NULL };
350 unsigned char bar_ext[SWM_BAR_MAX];
351 char bar_vertext[SWM_BAR_MAX];
353 sig_atomic_t bar_alarm = 0;
356 int bar_border_width = 1;
357 int bar_at_bottom = 0;
359 int bar_extra_running = 0;
362 int bar_justify = SWM_BAR_JUSTIFY_LEFT;
363 char *bar_format = NULL;
364 int stack_enabled = 1;
365 int clock_enabled = 1;
366 int urgent_enabled = 0;
367 char *clock_format = NULL;
368 int title_name_enabled = 0;
369 int title_class_enabled = 0;
370 int window_name_enabled = 0;
371 int focus_mode = SWM_FOCUS_DEFAULT;
372 int focus_close = SWM_STACK_BELOW;
373 int focus_close_wrap = 1;
374 int focus_default = SWM_STACK_TOP;
375 int spawn_position = SWM_STACK_TOP;
376 int disable_border = 0;
377 int border_width = 1;
378 int verbose_layout = 0;
382 XFontSetExtents *bar_fs_extents;
384 int bar_font_legacy = 1;
386 XftColor bar_font_color;
389 /* layout manager data */
390 struct swm_geometry {
403 struct swm_geometry g;
406 /* virtual "screens" */
408 TAILQ_ENTRY(swm_region) entry;
409 struct swm_geometry g;
410 struct workspace *ws; /* current workspace on this region */
411 struct workspace *ws_prior; /* prior workspace on this region */
412 struct swm_screen *s; /* screen idx */
415 TAILQ_HEAD(swm_region_list, swm_region);
418 TAILQ_ENTRY(ws_win) entry;
420 xcb_window_t transient;
421 struct ws_win *focus_child; /* focus on child transient */
422 struct swm_geometry g; /* current geometry */
423 struct swm_geometry g_float; /* region coordinates */
424 int g_floatvalid; /* g_float geometry validity */
425 int floatmaxed; /* whether maxed by max_stack */
431 unsigned int ewmh_flags;
432 int font_size_boundary[SWM_MAX_FONT_STEPS];
438 unsigned long quirks;
439 struct workspace *ws; /* always valid */
440 struct swm_screen *s; /* always valid, never changes */
441 xcb_get_geometry_reply_t *wa;
443 xcb_icccm_get_wm_class_reply_t ch;
444 xcb_icccm_wm_hints_t hints;
446 TAILQ_HEAD(ws_win_list, ws_win);
450 TAILQ_ENTRY(pid_e) entry;
454 TAILQ_HEAD(pid_list, pid_e);
455 struct pid_list pidlist = TAILQ_HEAD_INITIALIZER(pidlist);
457 /* layout handlers */
459 void vertical_config(struct workspace *, int);
460 void vertical_stack(struct workspace *, struct swm_geometry *);
461 void horizontal_config(struct workspace *, int);
462 void horizontal_stack(struct workspace *, struct swm_geometry *);
463 void max_stack(struct workspace *, struct swm_geometry *);
464 void plain_stacker(struct workspace *);
465 void fancy_stacker(struct workspace *);
468 void (*l_stack)(struct workspace *, struct swm_geometry *);
469 void (*l_config)(struct workspace *, int);
471 #define SWM_L_FOCUSPREV (1<<0)
472 #define SWM_L_MAPONFOCUS (1<<1)
473 void (*l_string)(struct workspace *);
475 /* stack, configure */
476 { vertical_stack, vertical_config, 0, plain_stacker },
477 { horizontal_stack, horizontal_config, 0, plain_stacker },
479 SWM_L_MAPONFOCUS | SWM_L_FOCUSPREV, plain_stacker },
480 { NULL, NULL, 0, NULL },
483 /* position of max_stack mode in the layouts array, index into layouts! */
484 #define SWM_V_STACK (0)
485 #define SWM_H_STACK (1)
486 #define SWM_MAX_STACK (2)
488 #define SWM_H_SLICE (32)
489 #define SWM_V_SLICE (32)
491 /* define work spaces */
493 int idx; /* workspace index */
494 char *name; /* workspace name */
495 int always_raise; /* raise windows on focus */
496 struct layout *cur_layout; /* current layout handlers */
497 struct ws_win *focus; /* may be NULL */
498 struct ws_win *focus_prev; /* may be NULL */
499 struct ws_win *focus_pending; /* may be NULL */
500 struct swm_region *r; /* may be NULL */
501 struct swm_region *old_r; /* may be NULL */
502 struct ws_win_list winlist; /* list of windows in ws */
503 struct ws_win_list unmanagedlist; /* list of dead windows in ws */
504 char stacker[10]; /* display stacker and layout */
508 int horizontal_msize;
510 int horizontal_stacks;
521 SWM_S_COLOR_BAR_BORDER,
522 SWM_S_COLOR_BAR_FONT,
528 /* physical screen mapping */
529 #define SWM_WS_MAX (22) /* hard limit */
530 int workspace_limit = 10; /* soft limit */
533 int idx; /* screen index */
534 struct swm_region_list rl; /* list of regions on this screen */
535 struct swm_region_list orl; /* list of old regions */
537 struct workspace ws[SWM_WS_MAX];
543 } c[SWM_S_COLOR_MAX];
545 xcb_gcontext_t bar_gc;
548 struct swm_screen *screens;
550 /* args to functions */
553 #define SWM_ARG_ID_FOCUSNEXT (0)
554 #define SWM_ARG_ID_FOCUSPREV (1)
555 #define SWM_ARG_ID_FOCUSMAIN (2)
556 #define SWM_ARG_ID_SWAPNEXT (10)
557 #define SWM_ARG_ID_SWAPPREV (11)
558 #define SWM_ARG_ID_SWAPMAIN (12)
559 #define SWM_ARG_ID_MOVELAST (13)
560 #define SWM_ARG_ID_MASTERSHRINK (20)
561 #define SWM_ARG_ID_MASTERGROW (21)
562 #define SWM_ARG_ID_MASTERADD (22)
563 #define SWM_ARG_ID_MASTERDEL (23)
564 #define SWM_ARG_ID_FLIPLAYOUT (24)
565 #define SWM_ARG_ID_STACKRESET (30)
566 #define SWM_ARG_ID_STACKINIT (31)
567 #define SWM_ARG_ID_CYCLEWS_UP (40)
568 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
569 #define SWM_ARG_ID_CYCLESC_UP (42)
570 #define SWM_ARG_ID_CYCLESC_DOWN (43)
571 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
572 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
573 #define SWM_ARG_ID_STACKINC (50)
574 #define SWM_ARG_ID_STACKDEC (51)
575 #define SWM_ARG_ID_SS_ALL (60)
576 #define SWM_ARG_ID_SS_WINDOW (61)
577 #define SWM_ARG_ID_DONTCENTER (70)
578 #define SWM_ARG_ID_CENTER (71)
579 #define SWM_ARG_ID_KILLWINDOW (80)
580 #define SWM_ARG_ID_DELETEWINDOW (81)
581 #define SWM_ARG_ID_WIDTHGROW (90)
582 #define SWM_ARG_ID_WIDTHSHRINK (91)
583 #define SWM_ARG_ID_HEIGHTGROW (92)
584 #define SWM_ARG_ID_HEIGHTSHRINK (93)
585 #define SWM_ARG_ID_MOVEUP (100)
586 #define SWM_ARG_ID_MOVEDOWN (101)
587 #define SWM_ARG_ID_MOVELEFT (102)
588 #define SWM_ARG_ID_MOVERIGHT (103)
594 TAILQ_ENTRY(quirk) entry;
598 #define SWM_Q_FLOAT (1<<0) /* float this window */
599 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
600 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
601 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
602 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
603 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
605 TAILQ_HEAD(quirk_list, quirk);
606 struct quirk_list quirks = TAILQ_HEAD_INITIALIZER(quirks);
609 * Supported EWMH hints should be added to
610 * both the enum and the ewmh array
615 _NET_MOVERESIZE_WINDOW,
616 _NET_WM_ACTION_ABOVE,
617 _NET_WM_ACTION_CLOSE,
618 _NET_WM_ACTION_FULLSCREEN,
620 _NET_WM_ACTION_RESIZE,
621 _NET_WM_ALLOWED_ACTIONS,
625 _NET_WM_STATE_FULLSCREEN,
626 _NET_WM_STATE_HIDDEN,
627 _NET_WM_STATE_MAXIMIZED_HORZ,
628 _NET_WM_STATE_MAXIMIZED_VERT,
629 _NET_WM_STATE_SKIP_PAGER,
630 _NET_WM_STATE_SKIP_TASKBAR,
632 _NET_WM_WINDOW_TYPE_DIALOG,
633 _NET_WM_WINDOW_TYPE_DOCK,
634 _NET_WM_WINDOW_TYPE_NORMAL,
635 _NET_WM_WINDOW_TYPE_SPLASH,
636 _NET_WM_WINDOW_TYPE_TOOLBAR,
637 _NET_WM_WINDOW_TYPE_UTILITY,
638 _SWM_WM_STATE_MANUAL,
645 } ewmh[SWM_EWMH_HINT_MAX] = {
646 /* must be in same order as in the enum */
647 {"_NET_ACTIVE_WINDOW", XCB_ATOM_NONE},
648 {"_NET_CLOSE_WINDOW", XCB_ATOM_NONE},
649 {"_NET_MOVERESIZE_WINDOW", XCB_ATOM_NONE},
650 {"_NET_WM_ACTION_ABOVE", XCB_ATOM_NONE},
651 {"_NET_WM_ACTION_CLOSE", XCB_ATOM_NONE},
652 {"_NET_WM_ACTION_FULLSCREEN", XCB_ATOM_NONE},
653 {"_NET_WM_ACTION_MOVE", XCB_ATOM_NONE},
654 {"_NET_WM_ACTION_RESIZE", XCB_ATOM_NONE},
655 {"_NET_WM_ALLOWED_ACTIONS", XCB_ATOM_NONE},
656 {"_NET_WM_NAME", XCB_ATOM_NONE},
657 {"_NET_WM_STATE", XCB_ATOM_NONE},
658 {"_NET_WM_STATE_ABOVE", XCB_ATOM_NONE},
659 {"_NET_WM_STATE_FULLSCREEN", XCB_ATOM_NONE},
660 {"_NET_WM_STATE_HIDDEN", XCB_ATOM_NONE},
661 {"_NET_WM_STATE_MAXIMIZED_HORZ", XCB_ATOM_NONE},
662 {"_NET_WM_STATE_MAXIMIZED_VERT", XCB_ATOM_NONE},
663 {"_NET_WM_STATE_SKIP_PAGER", XCB_ATOM_NONE},
664 {"_NET_WM_STATE_SKIP_TASKBAR", XCB_ATOM_NONE},
665 {"_NET_WM_WINDOW_TYPE", XCB_ATOM_NONE},
666 {"_NET_WM_WINDOW_TYPE_DIALOG", XCB_ATOM_NONE},
667 {"_NET_WM_WINDOW_TYPE_DOCK", XCB_ATOM_NONE},
668 {"_NET_WM_WINDOW_TYPE_NORMAL", XCB_ATOM_NONE},
669 {"_NET_WM_WINDOW_TYPE_SPLASH", XCB_ATOM_NONE},
670 {"_NET_WM_WINDOW_TYPE_TOOLBAR", XCB_ATOM_NONE},
671 {"_NET_WM_WINDOW_TYPE_UTILITY", XCB_ATOM_NONE},
672 {"_SWM_WM_STATE_MANUAL", XCB_ATOM_NONE},
675 /* function prototypes */
676 void buttonpress(xcb_button_press_event_t *);
677 void check_conn(void);
678 void clientmessage(xcb_client_message_event_t *);
679 int conf_load(char *, int);
680 void configurenotify(xcb_configure_notify_event_t *);
681 void configurerequest(xcb_configure_request_event_t *);
682 void constrain_window(struct ws_win *, struct swm_region *, int);
683 void destroynotify(xcb_destroy_notify_event_t *);
684 void enternotify(xcb_enter_notify_event_t *);
685 void event_drain(uint8_t);
686 void event_error(xcb_generic_error_t *);
687 void event_handle(xcb_generic_event_t *);
688 char *expand_tilde(char *);
689 void expose(xcb_expose_event_t *);
690 struct ws_win *find_window(xcb_window_t);
691 int floating_toggle_win(struct ws_win *);
692 void focus(struct swm_region *, union arg *);
693 void focus_flush(void);
694 struct ws_win *get_focus_magic(struct ws_win *);
695 struct ws_win *get_focus_prev(struct ws_win *);
696 void focus_win(struct ws_win *);
698 void focusin(xcb_focus_in_event_t *);
700 xcb_atom_t get_atom_from_string(const char *);
702 char *get_atom_name(xcb_atom_t);
703 char *get_notify_detail_label(uint8_t);
704 char *get_notify_mode_label(uint8_t);
706 struct ws_win *get_pointer_win(xcb_window_t);
707 struct ws_win *get_region_focus(struct swm_region *);
708 xcb_screen_t *get_screen(int);
709 char *get_win_name(xcb_window_t);
710 uint32_t getstate(xcb_window_t);
711 void grabbuttons(struct ws_win *);
712 void keypress(xcb_key_press_event_t *);
714 void leavenotify(xcb_leave_notify_event_t *);
716 void load_float_geom(struct ws_win *, struct swm_region *);
717 struct ws_win *manage_window(xcb_window_t, uint16_t);
718 void map_window(struct ws_win *);
719 void mapnotify(xcb_map_notify_event_t *);
720 void mappingnotify(xcb_mapping_notify_event_t *);
721 void maprequest(xcb_map_request_event_t *);
722 void new_region(struct swm_screen *, int, int, int, int);
723 int parse_rgb(const char *, uint16_t *, uint16_t *, uint16_t *);
724 void propertynotify(xcb_property_notify_event_t *);
725 void spawn_select(struct swm_region *, union arg *, char *, int *);
726 void screenchange(xcb_randr_screen_change_notify_event_t *);
727 void shutdown_cleanup(void);
728 void store_float_geom(struct ws_win *, struct swm_region *);
729 void unmanage_window(struct ws_win *);
730 void unmapnotify(xcb_unmap_notify_event_t *);
731 void unfocus_win(struct ws_win *);
732 void update_window(struct ws_win *);
733 /*void visibilitynotify(xcb_visibility_notify_event_t *);*/
736 expand_tilde(char *s)
745 errx(1, "expand_tilde: NULL string.");
754 if ((max = sysconf(_SC_LOGIN_NAME_MAX)) == -1)
755 errx(1, "expand_tilde: sysconf");
757 if ((user = calloc(1, max + 1)) == NULL)
758 errx(1, "expand_tilde: calloc");
760 for (i = 0; s[i] != '/' && s[i] != '\0'; ++i)
765 ppwd = strlen(user) == 0 ? getpwuid(getuid()) : getpwnam(user);
769 if (asprintf(&result, "%s%s", ppwd->pw_dir, s) == -1)
773 errx(1, "expand_tilde: failed to allocate memory.");
779 parse_rgb(const char *rgb, uint16_t *rr, uint16_t *gg, uint16_t *bb)
781 unsigned int tmpr, tmpg, tmpb;
783 if (sscanf(rgb, "rgb:%x/%x/%x", &tmpr, &tmpg, &tmpb) != 3)
786 *rr = RGB_8_TO_16(tmpr);
787 *gg = RGB_8_TO_16(tmpg);
788 *bb = RGB_8_TO_16(tmpb);
794 get_screen(int screen)
796 const xcb_setup_t *r;
797 xcb_screen_iterator_t iter;
799 if ((r = xcb_get_setup(conn)) == NULL) {
800 DNPRINTF(SWM_D_MISC, "get_screen: xcb_get_setup\n");
804 iter = xcb_setup_roots_iterator(r);
805 for (; iter.rem; --screen, xcb_screen_next(&iter))
815 if (focus_mode == SWM_FOCUS_DEFAULT)
816 event_drain(XCB_ENTER_NOTIFY);
822 get_atom_from_string(const char *str)
824 xcb_intern_atom_cookie_t c;
825 xcb_intern_atom_reply_t *r;
828 c = xcb_intern_atom(conn, 0, strlen(str), str);
829 r = xcb_intern_atom_reply(conn, c, NULL);
837 return (XCB_ATOM_NONE);
841 set_swm_iconic(struct ws_win *win, int newv)
848 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
849 a_swm_iconic, XCB_ATOM_INTEGER, 32, 1, &v);
851 xcb_delete_property(conn, win->id, a_swm_iconic);
855 get_swm_iconic(struct ws_win *win)
858 xcb_get_property_reply_t *pr = NULL;
860 pr = xcb_get_property_reply(conn,
861 xcb_get_property(conn, 0, win->id, a_swm_iconic,
862 XCB_ATOM_INTEGER, 0, 1), NULL);
865 if (pr->type != XCB_ATOM_INTEGER || pr->format != 32)
867 v = *((int32_t *)xcb_get_property_value(pr));
878 int i, j, num_screens;
880 sup_list = get_atom_from_string("_NET_SUPPORTED");
882 for (i = 0; i < LENGTH(ewmh); i++)
883 ewmh[i].atom = get_atom_from_string(ewmh[i].name);
885 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
886 for (i = 0; i < num_screens; i++) {
887 /* Support check window will be created by workaround(). */
889 /* Report supported atoms */
890 xcb_delete_property(conn, screens[i].root, sup_list);
891 for (j = 0; j < LENGTH(ewmh); j++)
892 xcb_change_property(conn, XCB_PROP_MODE_APPEND,
893 screens[i].root, sup_list, XCB_ATOM_ATOM, 32, 1,
902 xcb_atom_t sup_check, sup_list;
904 xcb_get_property_cookie_t pc;
905 xcb_get_property_reply_t *pr;
907 sup_check = get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
908 sup_list = get_atom_from_string("_NET_SUPPORTED");
909 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
911 for (i = 0; i < num_screens; i++) {
912 /* Get the support check window and destroy it */
913 pc = xcb_get_property(conn, 0, screens[i].root, sup_check,
914 XCB_ATOM_WINDOW, 0, 1);
915 pr = xcb_get_property_reply(conn, pc, NULL);
918 if (pr->format == sup_check) {
919 id = *((xcb_window_t *)xcb_get_property_value(pr));
921 xcb_destroy_window(conn, id);
922 xcb_delete_property(conn, screens[i].root, sup_check);
923 xcb_delete_property(conn, screens[i].root, sup_list);
930 ewmh_autoquirk(struct ws_win *win)
934 xcb_get_property_cookie_t c;
935 xcb_get_property_reply_t *r;
937 c = xcb_get_property(conn, 0, win->id,
938 ewmh[_NET_WM_WINDOW_TYPE].atom, XCB_ATOM_ATOM, 0, UINT32_MAX);
939 r = xcb_get_property_reply(conn, c, NULL);
943 n = xcb_get_property_value_length(r);
944 type = xcb_get_property_value(r);
946 for (i = 0; i < n; i++) {
947 if (type[i] == ewmh[_NET_WM_WINDOW_TYPE_NORMAL].atom)
949 if (type[i] == ewmh[_NET_WM_WINDOW_TYPE_DOCK].atom ||
950 type[i] == ewmh[_NET_WM_WINDOW_TYPE_TOOLBAR].atom ||
951 type[i] == ewmh[_NET_WM_WINDOW_TYPE_UTILITY].atom) {
953 win->quirks = SWM_Q_FLOAT | SWM_Q_ANYWHERE;
956 if (type[i] == ewmh[_NET_WM_WINDOW_TYPE_SPLASH].atom ||
957 type[i] == ewmh[_NET_WM_WINDOW_TYPE_DIALOG].atom) {
959 win->quirks = SWM_Q_FLOAT;
966 #define SWM_EWMH_ACTION_COUNT_MAX (6)
967 #define EWMH_F_FULLSCREEN (1<<0)
968 #define EWMH_F_ABOVE (1<<1)
969 #define EWMH_F_HIDDEN (1<<2)
970 #define EWMH_F_SKIP_PAGER (1<<3)
971 #define EWMH_F_SKIP_TASKBAR (1<<4)
972 #define SWM_F_MANUAL (1<<5)
975 ewmh_set_win_fullscreen(struct ws_win *win, int fs)
983 DNPRINTF(SWM_D_MISC, "ewmh_set_win_fullscreen: window: 0x%x, "
984 "fullscreen %s\n", win->id, YESNO(fs));
987 if (!win->g_floatvalid)
988 store_float_geom(win, win->ws->r);
990 win->g = win->ws->r->g;
993 load_float_geom(win, win->ws->r);
1000 ewmh_update_actions(struct ws_win *win)
1002 xcb_atom_t actions[SWM_EWMH_ACTION_COUNT_MAX];
1008 actions[n++] = ewmh[_NET_WM_ACTION_CLOSE].atom;
1010 if (win->floating) {
1011 actions[n++] = ewmh[_NET_WM_ACTION_MOVE].atom;
1012 actions[n++] = ewmh[_NET_WM_ACTION_RESIZE].atom;
1013 actions[n++] = ewmh[_NET_WM_ACTION_ABOVE].atom;
1016 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
1017 ewmh[_NET_WM_ALLOWED_ACTIONS].atom, XCB_ATOM_ATOM, 32, 1, actions);
1020 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
1021 #define _NET_WM_STATE_ADD 1 /* add/set property */
1022 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
1025 ewmh_update_win_state(struct ws_win *win, xcb_atom_t state, long action)
1027 unsigned int mask = 0;
1028 unsigned int changed = 0;
1029 unsigned int orig_flags;
1034 DNPRINTF(SWM_D_PROP, "ewmh_update_win_state: window: 0x%x, state: %ld, "
1035 "action: %ld\n", win->id, (unsigned long)state, action);
1037 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
1038 mask = EWMH_F_FULLSCREEN;
1039 else if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
1040 mask = EWMH_F_ABOVE;
1041 else if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
1042 mask = SWM_F_MANUAL;
1043 else if (state == ewmh[_NET_WM_STATE_SKIP_PAGER].atom)
1044 mask = EWMH_F_SKIP_PAGER;
1045 else if (state == ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom)
1046 mask = EWMH_F_SKIP_TASKBAR;
1048 orig_flags = win->ewmh_flags;
1051 case _NET_WM_STATE_REMOVE:
1052 win->ewmh_flags &= ~mask;
1054 case _NET_WM_STATE_ADD:
1055 win->ewmh_flags |= mask;
1057 case _NET_WM_STATE_TOGGLE:
1058 win->ewmh_flags ^= mask;
1062 changed = (win->ewmh_flags & mask) ^ (orig_flags & mask) ? 1 : 0;
1064 if (state == ewmh[_NET_WM_STATE_ABOVE].atom) {
1065 if (changed && !floating_toggle_win(win))
1066 win->ewmh_flags = orig_flags; /* revert */
1067 } else if (state == ewmh[_SWM_WM_STATE_MANUAL].atom) {
1069 win->manual = (win->ewmh_flags & SWM_F_MANUAL) != 0;
1070 } else if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom) {
1071 if (changed && !ewmh_set_win_fullscreen(win,
1072 win->ewmh_flags & EWMH_F_FULLSCREEN))
1073 win->ewmh_flags = orig_flags; /* revert */
1076 xcb_delete_property(conn, win->id, ewmh[_NET_WM_STATE].atom);
1078 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
1079 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1080 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1081 &ewmh[_NET_WM_STATE_FULLSCREEN].atom);
1082 else if (win->ewmh_flags & EWMH_F_SKIP_PAGER)
1083 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1084 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1085 &ewmh[_NET_WM_STATE_SKIP_PAGER].atom);
1086 else if (win->ewmh_flags & EWMH_F_SKIP_TASKBAR)
1087 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1088 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1089 &ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom);
1090 else if (win->ewmh_flags & EWMH_F_ABOVE)
1091 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1092 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1093 &ewmh[_NET_WM_STATE_ABOVE].atom);
1094 else if (win->ewmh_flags & SWM_F_MANUAL)
1095 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1096 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1097 &ewmh[_SWM_WM_STATE_MANUAL].atom);
1101 ewmh_get_win_state(struct ws_win *win)
1104 xcb_get_property_cookie_t c;
1105 xcb_get_property_reply_t *r;
1111 win->ewmh_flags = 0;
1113 win->ewmh_flags |= EWMH_F_ABOVE;
1115 win->ewmh_flags |= SWM_F_MANUAL;
1117 c = xcb_get_property(conn, 0, win->id, ewmh[_NET_WM_STATE].atom,
1118 XCB_ATOM_ATOM, 0, UINT32_MAX);
1119 r = xcb_get_property_reply(conn, c, NULL);
1123 states = xcb_get_property_value(r);
1124 n = xcb_get_property_value_length(r);
1126 for (i = 0; i < n; i++)
1127 ewmh_update_win_state(win, states[i], _NET_WM_STATE_ADD);
1135 dumpwins(struct swm_region *r, union arg *args)
1139 xcb_get_window_attributes_cookie_t c;
1140 xcb_get_window_attributes_reply_t *wa;
1142 /* suppress unused warning since var is needed */
1145 if (r->ws == NULL) {
1146 warnx("dumpwins: invalid workspace");
1150 warnx("=== managed window list ws %02d ===", r->ws->idx);
1151 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
1152 state = getstate(win->id);
1153 c = xcb_get_window_attributes(conn, win->id);
1154 wa = xcb_get_window_attributes_reply(conn, c, NULL);
1156 warnx("window: 0x%x, map_state: %d, state: %u, "
1157 "transient: 0x%x", win->id, wa->map_state,
1158 state, win->transient);
1161 warnx("window: 0x%x, failed xcb_get_window_attributes",
1165 warnx("===== unmanaged window list =====");
1166 TAILQ_FOREACH(win, &r->ws->unmanagedlist, entry) {
1167 state = getstate(win->id);
1168 c = xcb_get_window_attributes(conn, win->id);
1169 wa = xcb_get_window_attributes_reply(conn, c, NULL);
1171 warnx("window: 0x%x, map_state: %d, state: %u, "
1172 "transient: 0x%x", win->id, wa->map_state,
1173 state, win->transient);
1176 warnx("window: 0x%x, failed xcb_get_window_attributes",
1180 warnx("=================================");
1184 dumpwins(struct swm_region *r, union arg *s)
1189 #endif /* SWM_DEBUG */
1194 int saved_errno, status;
1197 saved_errno = errno;
1201 while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) != 0) {
1206 if (errno != ECHILD)
1207 warn("sighdlr: waitpid");
1208 #endif /* SWM_DEBUG */
1211 if (pid == searchpid)
1215 if (WIFEXITED(status)) {
1216 if (WEXITSTATUS(status) != 0)
1217 warnx("sighdlr: child exit status: %d",
1218 WEXITSTATUS(status));
1220 warnx("sighdlr: child is terminated "
1222 #endif /* SWM_DEBUG */
1236 errno = saved_errno;
1242 struct pid_e *p = NULL;
1244 DNPRINTF(SWM_D_MISC, "find_pid: %d\n", pid);
1249 TAILQ_FOREACH(p, &pidlist, entry) {
1258 name_to_pixel(const char *colorname)
1260 uint32_t result = 0;
1261 char cname[32] = "#";
1262 xcb_screen_t *screen;
1263 xcb_colormap_t cmap;
1264 xcb_alloc_color_reply_t *cr;
1265 xcb_alloc_named_color_reply_t *nr;
1266 uint16_t rr, gg, bb;
1268 screen = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
1269 cmap = screen->default_colormap;
1271 /* color is in format rgb://rr/gg/bb */
1272 if (strncmp(colorname, "rgb:", 4) == 0) {
1273 if (parse_rgb(colorname, &rr, &gg, &bb) == -1)
1274 warnx("could not parse rgb %s", colorname);
1276 cr = xcb_alloc_color_reply(conn,
1277 xcb_alloc_color(conn, cmap, rr, gg, bb),
1283 warnx("color '%s' not found", colorname);
1286 nr = xcb_alloc_named_color_reply(conn,
1287 xcb_alloc_named_color(conn, cmap, strlen(colorname),
1290 strlcat(cname, colorname + 2, sizeof cname - 1);
1291 nr = xcb_alloc_named_color_reply(conn,
1292 xcb_alloc_named_color(conn, cmap, strlen(cname),
1299 warnx("color '%s' not found", colorname);
1306 setscreencolor(char *val, int i, int c)
1310 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1311 if (i > 0 && i <= num_screens) {
1312 screens[i - 1].c[c].pixel = name_to_pixel(val);
1313 free(screens[i - 1].c[c].name);
1314 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
1316 } else if (i == -1) {
1317 for (i = 0; i < num_screens; i++) {
1318 screens[i].c[c].pixel = name_to_pixel(val);
1319 free(screens[i].c[c].name);
1320 if ((screens[i].c[c].name = strdup(val)) == NULL)
1324 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1329 fancy_stacker(struct workspace *ws)
1331 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1332 if (ws->cur_layout->l_stack == vertical_stack)
1333 snprintf(ws->stacker, sizeof ws->stacker,
1334 ws->l_state.vertical_flip ? "[%d>%d]" : "[%d|%d]",
1335 ws->l_state.vertical_mwin, ws->l_state.vertical_stacks);
1336 if (ws->cur_layout->l_stack == horizontal_stack)
1337 snprintf(ws->stacker, sizeof ws->stacker,
1338 ws->l_state.horizontal_flip ? "[%dv%d]" : "[%d-%d]",
1339 ws->l_state.horizontal_mwin, ws->l_state.horizontal_stacks);
1343 plain_stacker(struct workspace *ws)
1345 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1346 if (ws->cur_layout->l_stack == vertical_stack)
1347 strlcpy(ws->stacker, ws->l_state.vertical_flip ? "[>]" : "[|]",
1348 sizeof ws->stacker);
1349 if (ws->cur_layout->l_stack == horizontal_stack)
1350 strlcpy(ws->stacker, ws->l_state.horizontal_flip ? "[v]" : "[-]",
1351 sizeof ws->stacker);
1355 custom_region(char *val)
1357 unsigned int x, y, w, h;
1358 int sidx, num_screens;
1359 xcb_screen_t *screen;
1361 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1362 if (sscanf(val, "screen[%d]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
1363 errx(1, "invalid custom region, "
1364 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1365 if (sidx < 1 || sidx > num_screens)
1366 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1370 if ((screen = get_screen(sidx)) == NULL)
1371 errx(1, "ERROR: can't get screen %d.", sidx);
1374 errx(1, "region %ux%u+%u+%u too small", w, h, x, y);
1376 if (x > screen->width_in_pixels ||
1377 y > screen->height_in_pixels ||
1378 w + x > screen->width_in_pixels ||
1379 h + y > screen->height_in_pixels) {
1380 warnx("ignoring region %ux%u+%u+%u - not within screen "
1381 "boundaries (%ux%u)", w, h, x, y,
1382 screen->width_in_pixels, screen->height_in_pixels);
1386 new_region(&screens[sidx], x, y, w, h);
1390 socket_setnonblock(int fd)
1394 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
1395 err(1, "fcntl F_GETFL");
1396 flags |= O_NONBLOCK;
1397 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
1398 err(1, "fcntl F_SETFL");
1402 bar_print_legacy(struct swm_region *r, const char *s)
1404 xcb_rectangle_t rect;
1409 XRectangle ibox, lbox;
1413 XmbTextExtents(bar_fs, s, len, &ibox, &lbox);
1415 switch (bar_justify) {
1416 case SWM_BAR_JUSTIFY_LEFT:
1419 case SWM_BAR_JUSTIFY_CENTER:
1420 x = (WIDTH(r) - lbox.width) / 2;
1422 case SWM_BAR_JUSTIFY_RIGHT:
1423 x = WIDTH(r) - lbox.width - SWM_BAR_OFFSET;
1427 if (x < SWM_BAR_OFFSET)
1432 rect.width = WIDTH(r->bar);
1433 rect.height = HEIGHT(r->bar);
1435 /* clear back buffer */
1436 gcv[0] = r->s->c[SWM_S_COLOR_BAR].pixel;
1437 xcb_change_gc(conn, r->s->bar_gc, XCB_GC_FOREGROUND, gcv);
1438 xcb_poly_fill_rectangle(conn, r->bar->buffer, r->s->bar_gc, 1, &rect);
1440 /* draw back buffer */
1441 gcvd.graphics_exposures = 0;
1442 draw = XCreateGC(display, r->bar->buffer, GCGraphicsExposures, &gcvd);
1443 XSetForeground(display, draw, r->s->c[SWM_S_COLOR_BAR_FONT].pixel);
1444 DRAWSTRING(display, r->bar->buffer, bar_fs, draw,
1445 x, (bar_fs_extents->max_logical_extent.height - lbox.height) / 2 -
1447 XFreeGC(display, draw);
1450 xcb_copy_area(conn, r->bar->buffer, r->bar->id, r->s->bar_gc, 0, 0,
1451 0, 0, WIDTH(r->bar), HEIGHT(r->bar));
1455 bar_print(struct swm_region *r, const char *s)
1458 xcb_rectangle_t rect;
1466 XftTextExtentsUtf8(display, bar_font, (FcChar8 *)s, len, &info);
1468 switch (bar_justify) {
1469 case SWM_BAR_JUSTIFY_LEFT:
1472 case SWM_BAR_JUSTIFY_CENTER:
1473 x = (WIDTH(r) - info.width) / 2;
1475 case SWM_BAR_JUSTIFY_RIGHT:
1476 x = WIDTH(r) - info.width - SWM_BAR_OFFSET;
1480 if (x < SWM_BAR_OFFSET)
1485 rect.width = WIDTH(r->bar);
1486 rect.height = HEIGHT(r->bar);
1488 /* clear back buffer */
1489 gcv[0] = r->s->c[SWM_S_COLOR_BAR].pixel;
1490 xcb_change_gc(conn, r->s->bar_gc, XCB_GC_FOREGROUND, gcv);
1491 xcb_poly_fill_rectangle(conn, r->bar->buffer, r->s->bar_gc, 1, &rect);
1493 /* draw back buffer */
1494 draw = XftDrawCreate(display, r->bar->buffer,
1495 DefaultVisual(display, r->s->idx),
1496 DefaultColormap(display, r->s->idx));
1498 XftDrawStringUtf8(draw, &bar_font_color, bar_font, x,
1499 (HEIGHT(r->bar) + bar_font->height) / 2 - bar_font->descent,
1502 XftDrawDestroy(draw);
1505 xcb_copy_area(conn, r->bar->buffer, r->bar->id, r->s->bar_gc, 0, 0,
1506 0, 0, WIDTH(r->bar), HEIGHT(r->bar));
1510 bar_extra_stop(void)
1514 bzero(bar_pipe, sizeof bar_pipe);
1517 kill(bar_pid, SIGTERM);
1520 strlcpy((char *)bar_ext, "", sizeof bar_ext);
1525 bar_class_name(char *s, size_t sz, struct swm_region *r)
1527 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1529 if (r->ws->focus->ch.class_name != NULL)
1530 strlcat(s, r->ws->focus->ch.class_name, sz);
1534 bar_title_name(char *s, size_t sz, struct swm_region *r)
1536 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1538 if (r->ws->focus->ch.instance_name != NULL)
1539 strlcat(s, r->ws->focus->ch.instance_name, sz);
1543 bar_class_title_name(char *s, size_t sz, struct swm_region *r)
1545 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1548 bar_class_name(s, sz, r);
1549 strlcat(s, ":", sz);
1550 bar_title_name(s, sz, r);
1554 bar_window_float(char *s, size_t sz, struct swm_region *r)
1556 if (r == NULL || r ->ws == NULL || r->ws->focus == NULL)
1558 if (r->ws->focus->floating)
1559 strlcat(s, "(f)", sz);
1563 bar_window_name(char *s, size_t sz, struct swm_region *r)
1567 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1569 if ((title = get_win_name(r->ws->focus->id)) == NULL)
1572 strlcat(s, title, sz);
1576 int urgent[SWM_WS_MAX];
1578 bar_urgent(char *s, size_t sz)
1581 int i, j, num_screens;
1583 xcb_get_property_cookie_t c;
1584 xcb_icccm_wm_hints_t hints;
1586 for (i = 0; i < workspace_limit; i++)
1589 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1590 for (i = 0; i < num_screens; i++)
1591 for (j = 0; j < workspace_limit; j++)
1592 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry) {
1593 c = xcb_icccm_get_wm_hints(conn, win->id);
1594 if (xcb_icccm_get_wm_hints_reply(conn, c,
1597 if (hints.flags & XCB_ICCCM_WM_HINT_X_URGENCY)
1601 for (i = 0; i < workspace_limit; i++) {
1603 snprintf(b, sizeof b, "%d ", i + 1);
1605 snprintf(b, sizeof b, "- ");
1611 bar_workspace_name(char *s, size_t sz, struct swm_region *r)
1613 if (r == NULL || r->ws == NULL)
1615 if (r->ws->name != NULL)
1616 strlcat(s, r->ws->name, sz);
1619 /* build the default bar format according to the defined enabled options */
1621 bar_fmt(const char *fmtexp, char *fmtnew, struct swm_region *r, size_t sz)
1623 /* if format provided, just copy the buffers */
1624 if (bar_format != NULL) {
1625 strlcpy(fmtnew, fmtexp, sz);
1629 /* reset the output buffer */
1632 strlcat(fmtnew, "+N:+I ", sz);
1634 strlcat(fmtnew, "+S", sz);
1635 strlcat(fmtnew, " ", sz);
1637 /* only show the workspace name if there's actually one */
1638 if (r != NULL && r->ws != NULL && r->ws->name != NULL)
1639 strlcat(fmtnew, "<+D>", sz);
1640 strlcat(fmtnew, "+3<", sz);
1642 if (clock_enabled) {
1643 strlcat(fmtnew, fmtexp, sz);
1644 strlcat(fmtnew, "+4<", sz);
1647 /* bar_urgent already adds the space before the last asterisk */
1649 strlcat(fmtnew, "* +U*+4<", sz);
1651 if (title_class_enabled) {
1652 strlcat(fmtnew, "+C", sz);
1653 if (!title_name_enabled)
1654 strlcat(fmtnew, "+4<", sz);
1657 /* checks needed by the colon and floating strlcat(3) calls below */
1658 if (r != NULL && r->ws != NULL && r->ws->focus != NULL) {
1659 if (title_name_enabled) {
1660 if (title_class_enabled)
1661 strlcat(fmtnew, ":", sz);
1662 strlcat(fmtnew, "+T+4<", sz);
1664 if (window_name_enabled) {
1665 if (r->ws->focus->floating)
1666 strlcat(fmtnew, "+F ", sz);
1667 strlcat(fmtnew, "+64W ", sz);
1671 /* finally add the action script output and the version */
1672 strlcat(fmtnew, "+4<+A+4<+V", sz);
1676 bar_replace_pad(char *tmp, int *limit, size_t sz)
1678 /* special case; no limit given, pad one space, instead */
1679 if (*limit == (int)sz - 1)
1681 snprintf(tmp, sz, "%*s", *limit, " ");
1684 /* replaces the bar format character sequences (like in tmux(1)) */
1686 bar_replace_seq(char *fmt, char *fmtrep, struct swm_region *r, size_t *offrep,
1690 char tmp[SWM_BAR_MAX];
1694 /* reset strlcat(3) buffer */
1697 /* get number, if any */
1700 if (sscanf(fmt, "%d%n", &limit, &size) != 1)
1701 limit = sizeof tmp - 1;
1702 if (limit <= 0 || limit >= (int)sizeof tmp)
1703 limit = sizeof tmp - 1;
1705 /* there is nothing to replace (ie EOL) */
1712 bar_replace_pad(tmp, &limit, sizeof tmp);
1715 snprintf(tmp, sizeof tmp, "%s", bar_ext);
1718 bar_class_name(tmp, sizeof tmp, r);
1721 bar_workspace_name(tmp, sizeof tmp, r);
1724 bar_window_float(tmp, sizeof tmp, r);
1727 snprintf(tmp, sizeof tmp, "%d", r->ws->idx + 1);
1730 snprintf(tmp, sizeof tmp, "%d", r->s->idx + 1);
1733 bar_class_title_name(tmp, sizeof tmp, r);
1736 snprintf(tmp, sizeof tmp, "%s", r->ws->stacker);
1739 bar_title_name(tmp, sizeof tmp, r);
1742 bar_urgent(tmp, sizeof tmp);
1745 snprintf(tmp, sizeof tmp, "%s", bar_vertext);
1748 bar_window_name(tmp, sizeof tmp, r);
1751 /* unknown character sequence; copy as-is */
1752 snprintf(tmp, sizeof tmp, "+%c", *fmt);
1758 if ((int)len < limit)
1760 while (limit-- > 0) {
1761 if (*offrep >= sz - 1)
1763 fmtrep[(*offrep)++] = *ptr++;
1771 bar_replace(char *fmt, char *fmtrep, struct swm_region *r, size_t sz)
1776 while (*fmt != '\0') {
1778 /* skip ordinary characters */
1781 fmtrep[off++] = *fmt++;
1785 /* character sequence found; replace it */
1786 fmt = bar_replace_seq(fmt, fmtrep, r, &off, sz);
1795 bar_fmt_expand(char *fmtexp, size_t sz)
1802 /* start by grabbing the current time and date */
1804 localtime_r(&tmt, &tm);
1806 /* figure out what to expand */
1807 if (bar_format != NULL)
1809 else if (bar_format == NULL && clock_enabled)
1811 /* if nothing to expand bail out */
1817 /* copy as-is, just in case the format shouldn't be expanded below */
1818 strlcpy(fmtexp, fmt, sz);
1819 /* finally pass the string through strftime(3) */
1820 #ifndef SWM_DENY_CLOCK_FORMAT
1821 if ((len = strftime(fmtexp, sz, fmt, &tm)) == 0)
1822 warnx("format too long");
1830 char fmtexp[SWM_BAR_MAX], fmtnew[SWM_BAR_MAX];
1831 char fmtrep[SWM_BAR_MAX];
1833 struct swm_region *r;
1835 /* expand the format by first passing it through strftime(3) */
1836 bar_fmt_expand(fmtexp, sizeof fmtexp);
1838 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1839 for (i = 0; i < num_screens; i++) {
1840 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1843 bar_fmt(fmtexp, fmtnew, r, sizeof fmtnew);
1844 bar_replace(fmtnew, fmtrep, r, sizeof fmtrep);
1845 if (bar_font_legacy)
1846 bar_print_legacy(r, fmtrep);
1848 bar_print(r, fmtrep);
1861 if (bar_extra && bar_extra_running) {
1862 /* ignore short reads; it'll correct itself */
1863 while ((b = fgetln(stdin, &len)) != NULL)
1864 if (b && b[len - 1] == '\n') {
1866 strlcpy((char *)bar_ext, b, sizeof bar_ext);
1868 if (b == NULL && errno != EAGAIN) {
1869 warn("bar_update: bar_extra failed");
1873 strlcpy((char *)bar_ext, "", sizeof bar_ext);
1882 /* suppress unused warning since var is needed */
1889 bar_toggle(struct swm_region *r, union arg *args)
1891 struct swm_region *tmpr;
1894 /* suppress unused warnings since vars are needed */
1898 DNPRINTF(SWM_D_BAR, "bar_toggle\n");
1900 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1902 for (i = 0; i < num_screens; i++)
1903 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1905 xcb_unmap_window(conn, tmpr->bar->id);
1907 for (i = 0; i < num_screens; i++)
1908 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1910 xcb_map_window(conn, tmpr->bar->id);
1913 bar_enabled = !bar_enabled;
1917 /* must be after stack */
1926 struct swm_region *r;
1930 /* do this here because the conf file is in memory */
1931 if (bar_extra && !bar_extra_running && bar_argv[0]) {
1932 /* launch external status app */
1933 bar_extra_running = 1;
1934 if (pipe(bar_pipe) == -1)
1935 err(1, "pipe error");
1936 socket_setnonblock(bar_pipe[0]);
1937 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
1938 if (dup2(bar_pipe[0], 0) == -1)
1940 if (dup2(bar_pipe[1], 1) == -1)
1942 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
1943 err(1, "could not disable SIGPIPE");
1944 switch (bar_pid = fork()) {
1946 err(1, "cannot fork");
1950 execvp(bar_argv[0], bar_argv);
1951 err(1, "%s external app failed", bar_argv[0]);
1953 default: /* parent */
1959 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1960 for (i = 0; i < num_screens; i++)
1961 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1964 wa[0] = screens[i].c[SWM_S_COLOR_BAR].pixel;
1965 wa[1] = screens[i].c[SWM_S_COLOR_BAR_BORDER].pixel;
1966 xcb_change_window_attributes(conn, r->bar->id,
1967 XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL, wa);
1977 while ((s = index(s, '-'))) {
1982 return (count == 14);
1988 char *default_string;
1989 char **missing_charsets;
1990 int num_missing_charsets = 0;
1994 XFreeFontSet(display, bar_fs);
1998 DNPRINTF(SWM_D_INIT, "fontset_init: loading bar_fonts: %s\n", bar_fonts);
2000 bar_fs = XCreateFontSet(display, bar_fonts, &missing_charsets,
2001 &num_missing_charsets, &default_string);
2003 if (num_missing_charsets > 0) {
2004 warnx("Unable to load charset(s):");
2006 for (i = 0; i < num_missing_charsets; ++i)
2007 warnx("%s", missing_charsets[i]);
2009 XFreeStringList(missing_charsets);
2011 if (strcmp(default_string, ""))
2012 warnx("Glyphs from those sets will be replaced "
2013 "by '%s'.", default_string);
2015 warnx("Glyphs from those sets won't be drawn.");
2019 errx(1, "Error creating font set structure.");
2021 bar_fs_extents = XExtentsOfFontSet(bar_fs);
2023 bar_height = bar_fs_extents->max_logical_extent.height +
2024 2 * bar_border_width;
2031 xft_init(struct swm_region *r)
2033 char *font, *d, *search;
2036 if (bar_font == NULL) {
2037 if ((d = strdup(bar_fonts)) == NULL)
2038 errx(1, "insufficient memory.");
2040 while ((font = strsep(&search, ",")) != NULL) {
2044 DNPRINTF(SWM_D_INIT, "xft_init: try font %s\n", font);
2047 bar_font = XftFontOpenXlfd(display, r->s->idx,
2050 bar_font = XftFontOpenName(display, r->s->idx,
2055 warnx("unable to load font %s", font);
2058 DNPRINTF(SWM_D_INIT, "successfully opened "
2066 if (bar_font == NULL)
2067 errx(1, "unable to open a font");
2069 PIXEL_TO_XRENDERCOLOR(r->s->c[SWM_S_COLOR_BAR_FONT].pixel, color);
2071 if (!XftColorAllocValue(display, DefaultVisual(display, r->s->idx),
2072 DefaultColormap(display, r->s->idx), &color, &bar_font_color))
2073 warn("unable to allocate Xft color");
2075 bar_height = bar_font->height + 2 * bar_border_width;
2082 bar_setup(struct swm_region *r)
2084 xcb_screen_t *screen;
2087 DNPRINTF(SWM_D_BAR, "bar_setup: screen %d.\n",
2090 if ((screen = get_screen(r->s->idx)) == NULL)
2091 errx(1, "ERROR: can't get screen %d.", r->s->idx);
2096 if ((r->bar = calloc(1, sizeof(struct swm_bar))) == NULL)
2097 err(1, "bar_setup: calloc: failed to allocate memory.");
2099 if (bar_font_legacy)
2105 Y(r->bar) = bar_at_bottom ? (Y(r) + HEIGHT(r) - bar_height) : Y(r);
2106 WIDTH(r->bar) = WIDTH(r) - 2 * bar_border_width;
2107 HEIGHT(r->bar) = bar_height - 2 * bar_border_width;
2109 r->bar->id = xcb_generate_id(conn);
2110 wa[0] = r->s->c[SWM_S_COLOR_BAR].pixel;
2111 wa[1] = r->s->c[SWM_S_COLOR_BAR_BORDER].pixel;
2112 wa[2] = XCB_EVENT_MASK_EXPOSURE;
2114 xcb_create_window(conn, XCB_COPY_FROM_PARENT, r->bar->id, r->s->root,
2115 X(r->bar), Y(r->bar), WIDTH(r->bar), HEIGHT(r->bar),
2116 bar_border_width, XCB_WINDOW_CLASS_INPUT_OUTPUT,
2117 XCB_COPY_FROM_PARENT, XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL
2118 | XCB_CW_EVENT_MASK, wa);
2120 r->bar->buffer = xcb_generate_id(conn);
2121 xcb_create_pixmap(conn, screen->root_depth, r->bar->buffer, r->bar->id,
2122 WIDTH(r->bar), HEIGHT(r->bar));
2125 xcb_randr_select_input(conn, r->bar->id,
2126 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE);
2129 xcb_map_window(conn, r->bar->id);
2131 DNPRINTF(SWM_D_BAR, "bar_setup: window: 0x%x, (x,y) w x h: (%d,%d) "
2132 "%d x %d\n", WINID(r->bar), X(r->bar), Y(r->bar), WIDTH(r->bar),
2135 if (signal(SIGALRM, bar_signal) == SIG_ERR)
2136 err(1, "could not install bar_signal");
2141 bar_cleanup(struct swm_region *r)
2145 xcb_destroy_window(conn, r->bar->id);
2146 xcb_free_pixmap(conn, r->bar->buffer);
2152 set_win_state(struct ws_win *win, uint16_t state)
2154 uint16_t data[2] = { state, XCB_ATOM_NONE };
2156 DNPRINTF(SWM_D_EVENT, "set_win_state: window: 0x%x, state: %u\n",
2162 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id, a_state,
2163 a_state, 32, 2, data);
2167 getstate(xcb_window_t w)
2169 uint32_t result = 0;
2170 xcb_get_property_cookie_t c;
2171 xcb_get_property_reply_t *r;
2173 c = xcb_get_property(conn, 0, w, a_state, a_state, 0L, 2L);
2174 r = xcb_get_property_reply(conn, c, NULL);
2177 if (r->type == a_state && r->format == 32 && r->length == 2)
2178 result = *((uint32_t *)xcb_get_property_value(r));
2182 DNPRINTF(SWM_D_MISC, "getstate property: win 0x%x state %u\n", w,
2188 version(struct swm_region *r, union arg *args)
2190 /* suppress unused warnings since vars are needed */
2194 bar_version = !bar_version;
2196 snprintf(bar_vertext, sizeof bar_vertext,
2197 "Version: %s Build: %s", SPECTRWM_VERSION, buildstr);
2199 strlcpy(bar_vertext, "", sizeof bar_vertext);
2206 client_msg(struct ws_win *win, xcb_atom_t a, xcb_timestamp_t t)
2208 xcb_client_message_event_t ev;
2216 name = get_atom_name(a);
2217 DNPRINTF(SWM_D_EVENT, "client_msg: window: 0x%x, atom: %s(%u), "
2219 win->id, name, a, t);
2223 bzero(&ev, sizeof ev);
2224 ev.response_type = XCB_CLIENT_MESSAGE;
2225 ev.window = win->id;
2228 ev.data.data32[0] = a;
2229 ev.data.data32[1] = t;
2231 xcb_send_event(conn, 0, win->id,
2232 XCB_EVENT_MASK_NO_EVENT, (const char *)&ev);
2235 /* synthetic response to a ConfigureRequest when not making a change */
2237 config_win(struct ws_win *win, xcb_configure_request_event_t *ev)
2239 xcb_configure_notify_event_t ce;
2244 /* send notification of unchanged state. */
2245 bzero(&ce, sizeof(ce));
2246 ce.response_type = XCB_CONFIGURE_NOTIFY;
2249 ce.width = WIDTH(win);
2250 ce.height = HEIGHT(win);
2251 ce.override_redirect = 0;
2256 ce.window = win->id;
2257 ce.border_width = BORDER(win);
2258 ce.above_sibling = XCB_WINDOW_NONE;
2261 ce.event = ev->window;
2262 ce.window = ev->window;
2264 /* make response appear more WM_SIZE_HINTS-compliant */
2265 if (win->sh.flags) {
2266 DNPRINTF(SWM_D_MISC, "config_win: hints: window: 0x%x,"
2267 " sh.flags: %u, min: %d x %d, max: %d x %d, inc: "
2268 "%d x %d\n", win->id, win->sh.flags, SH_MIN_W(win),
2269 SH_MIN_H(win), SH_MAX_W(win), SH_MAX_H(win),
2270 SH_INC_W(win), SH_INC_H(win));
2275 /* the hint may be set... to 0! */
2276 if (SH_MIN_W(win) > 0 && ce.width < SH_MIN_W(win))
2277 ce.width = SH_MIN_W(win);
2278 if (SH_MIN_H(win) > 0 && ce.height < SH_MIN_H(win))
2279 ce.height = SH_MIN_H(win);
2284 /* may also be advertized as 0 */
2285 if (SH_MAX_W(win) > 0 && ce.width > SH_MAX_W(win))
2286 ce.width = SH_MAX_W(win);
2287 if (SH_MAX_H(win) > 0 && ce.height > SH_MAX_H(win))
2288 ce.height = SH_MAX_H(win);
2291 /* resize increment. */
2293 if (SH_INC_W(win) > 1 && ce.width > SH_INC_W(win))
2294 ce.width -= (ce.width - SH_MIN_W(win)) %
2296 if (SH_INC_H(win) > 1 && ce.height > SH_INC_H(win))
2297 ce.height -= (ce.height - SH_MIN_H(win)) %
2301 /* adjust x and y for requested border_width. */
2302 ce.x += BORDER(win) - ev->border_width;
2303 ce.y += BORDER(win) - ev->border_width;
2304 ce.border_width = ev->border_width;
2305 ce.above_sibling = ev->sibling;
2308 DNPRINTF(SWM_D_MISC, "config_win: ewmh: %s, window: 0x%x, (x,y) w x h: "
2309 "(%d,%d) %d x %d, border: %d\n", YESNO(ev == NULL), win->id, ce.x,
2310 ce.y, ce.width, ce.height, ce.border_width);
2312 xcb_send_event(conn, 0, win->id, XCB_EVENT_MASK_STRUCTURE_NOTIFY,
2317 count_win(struct workspace *ws, int count_transient)
2322 TAILQ_FOREACH(win, &ws->winlist, entry) {
2323 if (!count_transient && win->floating)
2325 if (!count_transient && win->transient)
2331 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
2337 quit(struct swm_region *r, union arg *args)
2339 /* suppress unused warnings since vars are needed */
2343 DNPRINTF(SWM_D_MISC, "quit\n");
2348 map_window(struct ws_win *win)
2350 uint32_t val = XCB_STACK_MODE_ABOVE;
2355 DNPRINTF(SWM_D_EVENT, "map_window: win 0x%x, mapped: %s\n", win->id,
2356 YESNO(win->mapped));
2358 xcb_configure_window(conn, win->id,
2359 XCB_CONFIG_WINDOW_STACK_MODE, &val);
2364 xcb_map_window(conn, win->id);
2365 set_win_state(win, XCB_ICCCM_WM_STATE_NORMAL);
2370 unmap_window(struct ws_win *win)
2375 DNPRINTF(SWM_D_EVENT, "unmap_window: win 0x%x, mapped: %s\n", win->id,
2376 YESNO(win->mapped));
2381 xcb_unmap_window(conn, win->id);
2382 set_win_state(win, XCB_ICCCM_WM_STATE_ICONIC);
2390 int i, j, num_screens;
2392 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2393 for (i = 0; i < num_screens; i++)
2394 for (j = 0; j < workspace_limit; j++)
2395 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2400 fake_keypress(struct ws_win *win, xcb_keysym_t keysym, uint16_t modifiers)
2402 xcb_key_press_event_t event;
2403 xcb_keycode_t *keycode;
2408 keycode = xcb_key_symbols_get_keycode(syms, keysym);
2410 DNPRINTF(SWM_D_MISC, "fake_keypress: win 0x%x keycode %u\n",
2413 bzero(&event, sizeof(event));
2414 event.event = win->id;
2415 event.root = win->s->root;
2416 event.child = XCB_WINDOW_NONE;
2417 event.time = XCB_CURRENT_TIME;
2418 event.event_x = X(win);
2419 event.event_y = Y(win);
2422 event.same_screen = 1;
2423 event.detail = *keycode;
2424 event.state = modifiers;
2426 event.response_type = XCB_KEY_PRESS;
2427 xcb_send_event(conn, 1, win->id,
2428 XCB_EVENT_MASK_KEY_PRESS, (const char *)&event);
2430 event.response_type = XCB_KEY_RELEASE;
2431 xcb_send_event(conn, 1, win->id,
2432 XCB_EVENT_MASK_KEY_RELEASE, (const char *)&event);
2438 restart(struct swm_region *r, union arg *args)
2440 /* suppress unused warning since var is needed */
2444 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
2448 execvp(start_argv[0], start_argv);
2449 warn("execvp failed");
2454 get_pointer_win(xcb_window_t root)
2456 struct ws_win *win = NULL;
2457 xcb_query_pointer_reply_t *r;
2459 DNPRINTF(SWM_D_EVENT, "get_pointer_win: root: 0x%x.\n", root);
2461 r = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, root), NULL);
2463 win = find_window(r->child);
2465 DNPRINTF(SWM_D_EVENT, "get_pointer_win: 0x%x.\n",
2468 DNPRINTF(SWM_D_EVENT, "get_pointer_win: none.\n");
2476 root_to_region(xcb_window_t root, int check)
2479 struct swm_region *r = NULL;
2481 xcb_query_pointer_reply_t *qpr;
2482 xcb_get_input_focus_reply_t *gifr;
2484 DNPRINTF(SWM_D_MISC, "root_to_region: window: 0x%x\n", root);
2486 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2487 for (i = 0; i < num_screens; i++)
2488 if (screens[i].root == root)
2491 if (check & SWM_CK_FOCUS) {
2492 /* Try to find an actively focused window */
2493 gifr = xcb_get_input_focus_reply(conn,
2494 xcb_get_input_focus(conn), NULL);
2496 cfw = find_window(gifr->focus);
2497 if (cfw && cfw->ws->r)
2504 if (r == NULL && check & SWM_CK_POINTER) {
2505 /* No region with an active focus; try to use pointer. */
2506 qpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn,
2507 screens[i].root), NULL);
2510 DNPRINTF(SWM_D_MISC, "root_to_region: pointer: "
2511 "(%d,%d)\n", qpr->root_x, qpr->root_y);
2512 TAILQ_FOREACH(r, &screens[i].rl, entry)
2513 if (X(r) <= qpr->root_x &&
2514 qpr->root_x < MAX_X(r) &&
2515 Y(r) <= qpr->root_y &&
2516 qpr->root_y < MAX_Y(r))
2523 if (r == NULL && check & SWM_CK_FALLBACK)
2524 r = TAILQ_FIRST(&screens[i].rl);
2530 find_unmanaged_window(xcb_window_t id)
2533 int i, j, num_screens;
2535 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2536 for (i = 0; i < num_screens; i++)
2537 for (j = 0; j < workspace_limit; j++)
2538 TAILQ_FOREACH(win, &screens[i].ws[j].unmanagedlist,
2546 find_window(xcb_window_t id)
2549 int i, j, num_screens;
2550 xcb_query_tree_reply_t *r;
2552 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2553 for (i = 0; i < num_screens; i++)
2554 for (j = 0; j < workspace_limit; j++)
2555 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2559 r = xcb_query_tree_reply(conn, xcb_query_tree(conn, id), NULL);
2563 /* if we were looking for the parent return that window instead */
2564 if (r->parent == 0 || r->root == r->parent) {
2569 /* look for parent */
2570 for (i = 0; i < num_screens; i++)
2571 for (j = 0; j < workspace_limit; j++)
2572 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2573 if (r->parent == win->id) {
2583 spawn(int ws_idx, union arg *args, int close_fd)
2588 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
2590 close(xcb_get_file_descriptor(conn));
2592 setenv("LD_PRELOAD", SWM_LIB, 1);
2594 if (asprintf(&ret, "%d", ws_idx) == -1) {
2595 warn("spawn: asprintf SWM_WS");
2598 setenv("_SWM_WS", ret, 1);
2602 if (asprintf(&ret, "%d", getpid()) == -1) {
2603 warn("spawn: asprintf _SWM_PID");
2606 setenv("_SWM_PID", ret, 1);
2610 if (setsid() == -1) {
2611 warn("spawn: setsid");
2617 * close stdin and stdout to prevent interaction between apps
2618 * and the baraction script
2619 * leave stderr open to record errors
2621 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) == -1) {
2622 warn("spawn: open");
2625 dup2(fd, STDIN_FILENO);
2626 dup2(fd, STDOUT_FILENO);
2631 execvp(args->argv[0], args->argv);
2633 warn("spawn: execvp");
2638 kill_refs(struct ws_win *win)
2640 int i, x, num_screens;
2641 struct swm_region *r;
2642 struct workspace *ws;
2647 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2648 for (i = 0; i < num_screens; i++)
2649 TAILQ_FOREACH(r, &screens[i].rl, entry)
2650 for (x = 0; x < workspace_limit; x++) {
2652 if (win == ws->focus)
2654 if (win == ws->focus_prev)
2655 ws->focus_prev = NULL;
2660 validate_win(struct ws_win *testwin)
2663 struct workspace *ws;
2664 struct swm_region *r;
2665 int i, x, num_screens;
2667 if (testwin == NULL)
2670 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2671 for (i = 0; i < num_screens; i++)
2672 TAILQ_FOREACH(r, &screens[i].rl, entry)
2673 for (x = 0; x < workspace_limit; x++) {
2675 TAILQ_FOREACH(win, &ws->winlist, entry)
2683 validate_ws(struct workspace *testws)
2685 struct swm_region *r;
2686 struct workspace *ws;
2687 int i, x, num_screens;
2689 /* validate all ws */
2690 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2691 for (i = 0; i < num_screens; i++)
2692 TAILQ_FOREACH(r, &screens[i].rl, entry)
2693 for (x = 0; x < workspace_limit; x++) {
2702 unfocus_win(struct ws_win *win)
2704 xcb_window_t none = XCB_WINDOW_NONE;
2706 DNPRINTF(SWM_D_FOCUS, "unfocus_win: window: 0x%x\n", WINID(win));
2710 if (win->ws == NULL) {
2711 DNPRINTF(SWM_D_FOCUS, "unfocus_win: NULL ws.\n");
2715 if (validate_ws(win->ws)) {
2716 DNPRINTF(SWM_D_FOCUS, "unfocus_win: invalid ws.\n");
2720 if (win->ws->r == NULL) {
2721 DNPRINTF(SWM_D_FOCUS, "unfocus_win: NULL region.\n");
2725 if (validate_win(win)) {
2726 DNPRINTF(SWM_D_FOCUS, "unfocus_win: invalid win.\n");
2731 if (win->ws->focus == win) {
2732 win->ws->focus = NULL;
2733 win->ws->focus_prev = win;
2736 if (validate_win(win->ws->focus)) {
2737 kill_refs(win->ws->focus);
2738 win->ws->focus = NULL;
2740 if (validate_win(win->ws->focus_prev)) {
2741 kill_refs(win->ws->focus_prev);
2742 win->ws->focus_prev = NULL;
2745 xcb_change_window_attributes(conn, win->id, XCB_CW_BORDER_PIXEL,
2746 &win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].pixel);
2748 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
2749 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1, &none);
2751 DNPRINTF(SWM_D_FOCUS, "unfocus_win: done.\n");
2755 focus_win(struct ws_win *win)
2757 struct ws_win *cfw = NULL, *parent = NULL, *w;
2758 struct workspace *ws;
2759 xcb_get_input_focus_reply_t *r;
2761 DNPRINTF(SWM_D_FOCUS, "focus_win: window: 0x%x\n", WINID(win));
2766 if (win->ws == NULL)
2771 if (validate_ws(ws))
2774 if (validate_win(win)) {
2779 r = xcb_get_input_focus_reply(conn, xcb_get_input_focus(conn), NULL);
2781 cfw = find_window(r->focus);
2787 if (ws->focus != win) {
2788 if (ws->focus && ws->focus != cfw)
2789 unfocus_win(ws->focus);
2793 /* If this window directs focus to a child window, then clear. */
2794 if (win->focus_child)
2795 win->focus_child = NULL;
2797 /* If transient, adjust parent's focus child for focus_magic. */
2798 if (win->transient) {
2799 parent = find_window(win->transient);
2800 if (parent && parent->focus_child != win)
2801 parent->focus_child = win;
2804 if (cfw != win && ws->r != NULL) {
2805 /* Set input focus if no input hint, or indicated by hint. */
2806 if (!(win->hints.flags & XCB_ICCCM_WM_HINT_INPUT) ||
2807 (win->hints.flags & XCB_ICCCM_WM_HINT_INPUT &&
2809 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT,
2810 win->id, last_event_time);
2812 /* Tell app it can adjust focus to a specific window. */
2813 if (win->take_focus) {
2814 /* java is special; always tell parent */
2815 if (win->transient && win->java)
2816 client_msg(parent, a_takefocus,
2819 client_msg(win, a_takefocus, last_event_time);
2822 xcb_change_window_attributes(conn, win->id, XCB_CW_BORDER_PIXEL,
2823 &ws->r->s->c[SWM_S_COLOR_FOCUS].pixel);
2825 if (ws->cur_layout->flags & SWM_L_MAPONFOCUS ||
2827 /* If a parent exists, map it first. */
2831 /* Map siblings next. */
2832 TAILQ_FOREACH(w, &ws->winlist, entry)
2833 if (w != win && !w->iconic &&
2834 w->transient == parent->id)
2838 /* Map focused window. */
2841 /* Finally, map children of focus window. */
2842 TAILQ_FOREACH(w, &ws->winlist, entry)
2843 if (w->transient == win->id && !w->iconic)
2848 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
2849 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1,
2856 DNPRINTF(SWM_D_FOCUS, "focus_win: done.\n");
2859 /* If a child window should have focus instead, return it. */
2861 get_focus_magic(struct ws_win *win)
2863 struct ws_win *parent = NULL;
2864 struct ws_win *child = NULL;
2866 DNPRINTF(SWM_D_FOCUS, "get_focus_magic: window: 0x%x\n", WINID(win));
2870 if (win->transient) {
2871 parent = find_window(win->transient);
2873 /* If parent prefers focus elsewhere, then try to do so. */
2874 if (parent && (child = parent->focus_child)) {
2875 if (validate_win(child) == 0 && child->mapped)
2878 parent->focus_child = NULL;
2882 /* If this window prefers focus elsewhere, then try to do so. */
2883 if ((child = win->focus_child)) {
2884 if (validate_win(child) == 0 && child->mapped)
2887 win->focus_child = NULL;
2894 event_drain(uint8_t rt)
2896 xcb_generic_event_t *evt;
2898 /* ensure all pending requests have been processed before filtering. */
2900 while ((evt = xcb_poll_for_event(conn))) {
2901 if (XCB_EVENT_RESPONSE_TYPE(evt) != rt)
2909 switchws(struct swm_region *r, union arg *args)
2911 int wsid = args->id, unmap_old = 0;
2912 struct swm_region *this_r, *other_r;
2914 struct workspace *new_ws, *old_ws;
2919 if (wsid >= workspace_limit)
2923 old_ws = this_r->ws;
2924 new_ws = &this_r->s->ws[wsid];
2926 DNPRINTF(SWM_D_WS, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
2927 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), old_ws->idx, wsid);
2929 if (new_ws == NULL || old_ws == NULL)
2931 if (new_ws == old_ws)
2934 unfocus_win(old_ws->focus);
2936 other_r = new_ws->r;
2937 if (other_r == NULL) {
2938 /* the other workspace is hidden, hide this one */
2942 /* the other ws is visible in another region, exchange them */
2943 other_r->ws_prior = new_ws;
2944 other_r->ws = old_ws;
2945 old_ws->r = other_r;
2947 this_r->ws_prior = old_ws;
2948 this_r->ws = new_ws;
2953 /* unmap old windows */
2955 TAILQ_FOREACH(win, &old_ws->winlist, entry)
2958 if (focus_mode != SWM_FOCUS_FOLLOW) {
2959 new_ws->focus_pending = get_region_focus(new_ws->r);
2961 /* if workspaces were swapped, then don't wait to set focus */
2963 if (new_ws->focus_pending) {
2964 focus_win(new_ws->focus_pending);
2966 /* Empty region, focus on root. */
2967 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT,
2968 new_ws->r->s[new_ws->r->s->idx].root,
2974 /* Clear bar if new ws is empty. */
2975 if (new_ws->focus_pending == NULL)
2980 DNPRINTF(SWM_D_WS, "switchws: done.\n");
2984 cyclews(struct swm_region *r, union arg *args)
2987 struct swm_screen *s = r->s;
2990 DNPRINTF(SWM_D_WS, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2991 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2996 case SWM_ARG_ID_CYCLEWS_UP_ALL:
2999 case SWM_ARG_ID_CYCLEWS_UP:
3000 if (a.id < workspace_limit - 1)
3005 case SWM_ARG_ID_CYCLEWS_DOWN_ALL:
3008 case SWM_ARG_ID_CYCLEWS_DOWN:
3012 a.id = workspace_limit - 1;
3019 (!cycle_empty && TAILQ_EMPTY(&s->ws[a.id].winlist)))
3021 if (!cycle_visible && s->ws[a.id].r != NULL)
3025 } while (a.id != r->ws->idx);
3029 priorws(struct swm_region *r, union arg *args)
3035 DNPRINTF(SWM_D_WS, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3036 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
3038 if (r->ws_prior == NULL)
3041 a.id = r->ws_prior->idx;
3046 cyclescr(struct swm_region *r, union arg *args)
3049 struct swm_region *rr = NULL;
3052 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
3053 /* do nothing if we don't have more than one screen */
3054 if (!(num_screens > 1 || outputs > 1))
3059 case SWM_ARG_ID_CYCLESC_UP:
3060 rr = TAILQ_NEXT(r, entry);
3062 rr = TAILQ_FIRST(&screens[i].rl);
3064 case SWM_ARG_ID_CYCLESC_DOWN:
3065 rr = TAILQ_PREV(r, swm_region_list, entry);
3067 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
3075 nfw = get_region_focus(rr);
3079 /* New region is empty; unfocus old region and warp pointer. */
3080 unfocus_win(r->ws->focus);
3081 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT,
3082 rr->s[i].root, XCB_CURRENT_TIME);
3084 /* Clear bar since empty. */
3092 sort_windows(struct ws_win_list *wl)
3094 struct ws_win *win, *parent, *nxt;
3099 for (win = TAILQ_FIRST(wl); win != TAILQ_END(wl); win = nxt) {
3100 nxt = TAILQ_NEXT(win, entry);
3101 if (win->transient) {
3102 parent = find_window(win->transient);
3103 if (parent == NULL) {
3104 warnx("not possible bug");
3107 TAILQ_REMOVE(wl, win, entry);
3108 TAILQ_INSERT_AFTER(wl, parent, win, entry);
3115 swapwin(struct swm_region *r, union arg *args)
3117 struct ws_win *target, *source;
3118 struct ws_win *cur_focus;
3119 struct ws_win_list *wl;
3121 DNPRINTF(SWM_D_WS, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3122 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
3124 cur_focus = r->ws->focus;
3125 if (cur_focus == NULL)
3129 wl = &source->ws->winlist;
3132 case SWM_ARG_ID_SWAPPREV:
3133 if (source->transient)
3134 source = find_window(source->transient);
3135 target = TAILQ_PREV(source, ws_win_list, entry);
3136 if (target && target->transient)
3137 target = find_window(target->transient);
3138 TAILQ_REMOVE(wl, source, entry);
3140 TAILQ_INSERT_TAIL(wl, source, entry);
3142 TAILQ_INSERT_BEFORE(target, source, entry);
3144 case SWM_ARG_ID_SWAPNEXT:
3145 target = TAILQ_NEXT(source, entry);
3146 /* move the parent and let the sort handle the move */
3147 if (source->transient)
3148 source = find_window(source->transient);
3149 TAILQ_REMOVE(wl, source, entry);
3151 TAILQ_INSERT_HEAD(wl, source, entry);
3153 TAILQ_INSERT_AFTER(wl, target, source, entry);
3155 case SWM_ARG_ID_SWAPMAIN:
3156 target = TAILQ_FIRST(wl);
3157 if (target == source) {
3158 if (source->ws->focus_prev != NULL &&
3159 source->ws->focus_prev != target)
3160 source = source->ws->focus_prev;
3164 if (target == NULL || source == NULL)
3166 source->ws->focus_prev = target;
3167 TAILQ_REMOVE(wl, target, entry);
3168 TAILQ_INSERT_BEFORE(source, target, entry);
3169 TAILQ_REMOVE(wl, source, entry);
3170 TAILQ_INSERT_HEAD(wl, source, entry);
3172 case SWM_ARG_ID_MOVELAST:
3173 TAILQ_REMOVE(wl, source, entry);
3174 TAILQ_INSERT_TAIL(wl, source, entry);
3177 DNPRINTF(SWM_D_MOVE, "swapwin: invalid id: %d\n", args->id);
3189 get_focus_prev(struct ws_win *win)
3191 struct ws_win *winfocus = NULL;
3192 struct ws_win *cur_focus = NULL;
3193 struct ws_win_list *wl = NULL;
3194 struct workspace *ws = NULL;
3196 if (!(win && win->ws))
3201 cur_focus = ws->focus;
3203 DNPRINTF(SWM_D_FOCUS, "get_focus_prev: window: 0x%x, cur_focus: 0x%x\n",
3204 WINID(win), WINID(cur_focus));
3206 /* pickle, just focus on whatever */
3207 if (cur_focus == NULL) {
3208 /* use prev_focus if valid */
3209 if (ws->focus_prev && ws->focus_prev != cur_focus &&
3210 find_window(WINID(ws->focus_prev)))
3211 winfocus = ws->focus_prev;
3215 /* if transient focus on parent */
3216 if (cur_focus->transient) {
3217 winfocus = find_window(cur_focus->transient);
3221 /* if in max_stack try harder */
3222 if ((win->quirks & SWM_Q_FOCUSPREV) ||
3223 (ws->cur_layout->flags & SWM_L_FOCUSPREV)) {
3224 if (cur_focus != ws->focus_prev)
3225 winfocus = ws->focus_prev;
3227 winfocus = TAILQ_PREV(win, ws_win_list, entry);
3232 DNPRINTF(SWM_D_FOCUS, "get_focus_prev: focus_close: %d\n", focus_close);
3234 if (winfocus == NULL || winfocus == win) {
3235 switch (focus_close) {
3236 case SWM_STACK_BOTTOM:
3237 winfocus = TAILQ_FIRST(wl);
3240 winfocus = TAILQ_LAST(wl, ws_win_list);
3242 case SWM_STACK_ABOVE:
3243 if ((winfocus = TAILQ_NEXT(cur_focus, entry)) == NULL) {
3244 if (focus_close_wrap)
3245 winfocus = TAILQ_FIRST(wl);
3247 winfocus = TAILQ_PREV(cur_focus,
3248 ws_win_list, entry);
3251 case SWM_STACK_BELOW:
3252 if ((winfocus = TAILQ_PREV(cur_focus, ws_win_list,
3254 if (focus_close_wrap)
3255 winfocus = TAILQ_LAST(wl, ws_win_list);
3257 winfocus = TAILQ_NEXT(cur_focus, entry);
3263 if (winfocus == NULL) {
3264 if (focus_default == SWM_STACK_TOP)
3265 winfocus = TAILQ_LAST(wl, ws_win_list);
3267 winfocus = TAILQ_FIRST(wl);
3272 return get_focus_magic(winfocus);
3276 get_region_focus(struct swm_region *r)
3278 struct ws_win *winfocus = NULL;
3283 if (r->ws->focus && !r->ws->focus->iconic)
3284 winfocus = r->ws->focus;
3285 else if (r->ws->focus_prev && !r->ws->focus_prev->iconic)
3286 winfocus = r->ws->focus_prev;
3288 TAILQ_FOREACH(winfocus, &r->ws->winlist, entry)
3289 if (!winfocus->iconic)
3292 return get_focus_magic(winfocus);
3296 focus(struct swm_region *r, union arg *args)
3298 struct ws_win *head, *cur_focus = NULL, *winfocus = NULL;
3299 struct ws_win_list *wl = NULL;
3300 struct workspace *ws = NULL;
3306 DNPRINTF(SWM_D_FOCUS, "focus: id: %d\n", args->id);
3308 if ((cur_focus = r->ws->focus) == NULL)
3312 if (TAILQ_EMPTY(wl))
3314 /* make sure there is at least one uniconified window */
3316 TAILQ_FOREACH(winfocus, wl, entry)
3317 if (!winfocus->iconic) {
3325 case SWM_ARG_ID_FOCUSPREV:
3326 head = TAILQ_PREV(cur_focus, ws_win_list, entry);
3328 head = TAILQ_LAST(wl, ws_win_list);
3330 if (WINID(winfocus) == cur_focus->transient) {
3331 head = TAILQ_PREV(winfocus, ws_win_list, entry);
3333 head = TAILQ_LAST(wl, ws_win_list);
3338 if (winfocus && winfocus->iconic) {
3339 while (winfocus != cur_focus) {
3340 if (winfocus == NULL)
3341 winfocus = TAILQ_LAST(wl, ws_win_list);
3342 if (!winfocus->iconic)
3344 winfocus = TAILQ_PREV(winfocus, ws_win_list,
3350 case SWM_ARG_ID_FOCUSNEXT:
3351 head = TAILQ_NEXT(cur_focus, entry);
3353 head = TAILQ_FIRST(wl);
3357 if (winfocus && winfocus->iconic) {
3358 while (winfocus != cur_focus) {
3359 if (winfocus == NULL)
3360 winfocus = TAILQ_FIRST(wl);
3361 if (!winfocus->iconic)
3363 winfocus = TAILQ_NEXT(winfocus, entry);
3368 case SWM_ARG_ID_FOCUSMAIN:
3369 winfocus = TAILQ_FIRST(wl);
3370 if (winfocus == cur_focus)
3371 winfocus = cur_focus->ws->focus_prev;
3378 focus_win(get_focus_magic(winfocus));
3384 cycle_layout(struct swm_region *r, union arg *args)
3386 struct workspace *ws = r->ws;
3388 /* suppress unused warning since var is needed */
3391 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
3394 if (ws->cur_layout->l_stack == NULL)
3395 ws->cur_layout = &layouts[0];
3400 focus_win(get_region_focus(r));
3406 stack_config(struct swm_region *r, union arg *args)
3408 struct workspace *ws = r->ws;
3410 DNPRINTF(SWM_D_STACK, "stack_config: id: %d workspace: %d\n",
3413 if (ws->cur_layout->l_config != NULL)
3414 ws->cur_layout->l_config(ws, args->id);
3416 if (args->id != SWM_ARG_ID_STACKINIT)
3425 struct swm_geometry g;
3426 struct swm_region *r;
3432 DNPRINTF(SWM_D_STACK, "stack: begin\n");
3434 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
3435 for (i = 0; i < num_screens; i++) {
3439 TAILQ_FOREACH(r, &screens[i].rl, entry) {
3440 DNPRINTF(SWM_D_STACK, "stack: workspace: %d "
3441 "(screen: %d, region: %d)\n", r->ws->idx, i, j++);
3443 /* start with screen geometry, adjust for bar */
3445 g.w -= 2 * border_width;
3446 g.h -= 2 * border_width;
3452 r->ws->cur_layout->l_stack(r->ws, &g);
3453 r->ws->cur_layout->l_string(r->ws);
3454 /* save r so we can track region changes */
3461 DNPRINTF(SWM_D_STACK, "stack: end\n");
3465 store_float_geom(struct ws_win *win, struct swm_region *r)
3467 if (win == NULL || r == NULL)
3470 /* retain window geom and region geom */
3471 win->g_float = win->g;
3472 win->g_float.x -= X(r);
3473 win->g_float.y -= Y(r);
3474 win->g_floatvalid = 1;
3475 DNPRINTF(SWM_D_MISC, "store_float_geom: window: 0x%x, g: (%d,%d)"
3476 " %d x %d, g_float: (%d,%d) %d x %d\n", win->id, X(win), Y(win),
3477 WIDTH(win), HEIGHT(win), win->g_float.x, win->g_float.y,
3478 win->g_float.w, win->g_float.h);
3482 load_float_geom(struct ws_win *win, struct swm_region *r)
3484 if (win == NULL || r == NULL)
3487 if (win->g_floatvalid) {
3488 win->g = win->g_float;
3491 DNPRINTF(SWM_D_MISC, "load_float_geom: window: 0x%x, g: (%d,%d)"
3492 "%d x %d\n", win->id, X(win), Y(win), WIDTH(win),
3495 DNPRINTF(SWM_D_MISC, "load_float_geom: window: 0x%x, g_float "
3496 "is not set.\n", win->id);
3501 stack_floater(struct ws_win *win, struct swm_region *r)
3506 DNPRINTF(SWM_D_MISC, "stack_floater: window: 0x%x\n", win->id);
3509 * to allow windows to change their size (e.g. mplayer fs) only retrieve
3510 * geom on ws switches or return from max mode
3512 if (win->floatmaxed || (r != r->ws->old_r &&
3513 !(win->ewmh_flags & EWMH_F_FULLSCREEN))) {
3514 /* update geometry for the new region */
3515 load_float_geom(win, r);
3518 win->floatmaxed = 0;
3521 * if set to fullscreen mode, configure window to maximum size.
3523 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
3524 if (!win->g_floatvalid)
3525 store_float_geom(win, win->ws->r);
3531 * remove border on fullscreen floater when in fullscreen mode or when
3532 * the quirk is present.
3534 if ((win->ewmh_flags & EWMH_F_FULLSCREEN) ||
3535 ((win->quirks & SWM_Q_FULLSCREEN) &&
3536 (WIDTH(win) >= WIDTH(r)) && (HEIGHT(win) >= HEIGHT(r)))) {
3537 if (win->bordered) {
3539 X(win) += border_width;
3540 Y(win) += border_width;
3542 } else if (!win->bordered) {
3544 X(win) -= border_width;
3545 Y(win) -= border_width;
3548 if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
3549 WIDTH(win) = (double)WIDTH(r) * dialog_ratio;
3550 HEIGHT(win) = (double)HEIGHT(r) * dialog_ratio;
3553 if (!win->manual && !(win->ewmh_flags & EWMH_F_FULLSCREEN) &&
3554 !(win->quirks & SWM_Q_ANYWHERE)) {
3556 * floaters and transients are auto-centred unless moved,
3557 * resized or ANYWHERE quirk is set.
3559 X(win) = X(r) + (WIDTH(r) - WIDTH(win)) / 2 - BORDER(win);
3560 Y(win) = Y(r) + (HEIGHT(r) - HEIGHT(win)) / 2 - BORDER(win);
3562 store_float_geom(win, r);
3565 /* keep window within region bounds */
3566 constrain_window(win, r, 0);
3572 * Send keystrokes to terminal to decrease/increase the font size as the
3573 * window size changes.
3576 adjust_font(struct ws_win *win)
3578 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
3579 win->floating || win->transient)
3582 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
3583 WIDTH(win) / win->sh.width_inc < term_width &&
3584 win->font_steps < SWM_MAX_FONT_STEPS) {
3585 win->font_size_boundary[win->font_steps] =
3586 (win->sh.width_inc * term_width) + win->sh.base_width;
3589 win->last_inc = win->sh.width_inc;
3590 fake_keypress(win, XK_KP_Subtract, XCB_MOD_MASK_SHIFT);
3591 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
3592 WIDTH(win) > win->font_size_boundary[win->font_steps - 1]) {
3595 win->last_inc = win->sh.width_inc;
3596 fake_keypress(win, XK_KP_Add, XCB_MOD_MASK_SHIFT);
3600 #define SWAPXY(g) do { \
3602 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
3603 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
3606 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
3608 struct swm_geometry win_g, r_g = *g;
3609 struct ws_win *win, *fs_win = NULL;
3610 int i, j, s, stacks;
3611 int w_inc = 1, h_inc, w_base = 1, h_base;
3612 int hrh, extra = 0, h_slice, last_h = 0;
3613 int split, colno, winno, mwin, msize, mscale;
3614 int remain, missing, v_slice, reconfigure = 0;
3617 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d, rot: %s, "
3618 "flip: %s\n", ws->idx, YESNO(rot), YESNO(flip));
3620 winno = count_win(ws, 0);
3621 if (winno == 0 && count_win(ws, 1) == 0)
3624 TAILQ_FOREACH(win, &ws->winlist, entry)
3625 if (!win->transient && !win->floating && !win->iconic)
3632 w_inc = win->sh.width_inc;
3633 w_base = win->sh.base_width;
3634 mwin = ws->l_state.horizontal_mwin;
3635 mscale = ws->l_state.horizontal_msize;
3636 stacks = ws->l_state.horizontal_stacks;
3639 w_inc = win->sh.height_inc;
3640 w_base = win->sh.base_height;
3641 mwin = ws->l_state.vertical_mwin;
3642 mscale = ws->l_state.vertical_msize;
3643 stacks = ws->l_state.vertical_stacks;
3647 if (stacks > winno - mwin)
3648 stacks = winno - mwin;
3652 h_slice = r_g.h / SWM_H_SLICE;
3653 if (mwin && winno > mwin) {
3654 v_slice = r_g.w / SWM_V_SLICE;
3658 win_g.w = v_slice * mscale;
3660 if (w_inc > 1 && w_inc < v_slice) {
3661 /* adjust for window's requested size increment */
3662 remain = (win_g.w - w_base) % w_inc;
3668 win_g.x += r_g.w - msize;
3671 colno = split = winno / stacks;
3672 win_g.w = ((r_g.w - (stacks * 2 * border_width) +
3673 2 * border_width) / stacks);
3675 hrh = r_g.h / colno;
3676 extra = r_g.h - (colno * hrh);
3677 win_g.h = hrh - 2 * border_width;
3679 /* stack all the tiled windows */
3680 i = j = 0, s = stacks;
3681 TAILQ_FOREACH(win, &ws->winlist, entry) {
3682 if (win->transient || win->floating)
3687 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
3692 if (split && i == split) {
3693 colno = (winno - mwin) / stacks;
3694 if (s <= (winno - mwin) % stacks)
3696 split = split + colno;
3697 hrh = (r_g.h / colno);
3698 extra = r_g.h - (colno * hrh);
3702 win_g.x += win_g.w + 2 * border_width;
3703 win_g.w = (r_g.w - msize -
3704 (stacks * 2 * border_width)) / stacks;
3706 win_g.w += (r_g.w - msize -
3707 (stacks * 2 * border_width)) % stacks;
3711 win_g.h = hrh - 2 * border_width;
3713 h_inc = win->sh.width_inc;
3714 h_base = win->sh.base_width;
3716 h_inc = win->sh.height_inc;
3717 h_base = win->sh.base_height;
3719 if (j == colno - 1) {
3720 win_g.h = hrh + extra;
3721 } else if (h_inc > 1 && h_inc < h_slice) {
3722 /* adjust for window's requested size increment */
3723 remain = (win_g.h - h_base) % h_inc;
3724 missing = h_inc - remain;
3726 if (missing <= extra || j == 0) {
3738 win_g.y += last_h + 2 * border_width;
3740 if (disable_border && !bar_enabled && winno == 1){
3742 win_g.w += 2 * border_width;
3743 win_g.h += 2 * border_width;
3748 if (X(win) != win_g.y || Y(win) != win_g.x ||
3749 WIDTH(win) != win_g.h || HEIGHT(win) != win_g.w) {
3753 WIDTH(win) = win_g.h;
3754 HEIGHT(win) = win_g.w;
3757 if (X(win) != win_g.x || Y(win) != win_g.y ||
3758 WIDTH(win) != win_g.w || HEIGHT(win) != win_g.h) {
3762 WIDTH(win) = win_g.w;
3763 HEIGHT(win) = win_g.h;
3767 if (bordered != win->bordered) {
3769 win->bordered = bordered;
3785 /* now, stack all the floaters and transients */
3786 TAILQ_FOREACH(win, &ws->winlist, entry) {
3787 if (!win->transient && !win->floating)
3791 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
3796 stack_floater(win, ws->r);
3800 /* Make sure fs_win is stacked last so it's on top. */
3802 stack_floater(fs_win, ws->r);
3808 vertical_config(struct workspace *ws, int id)
3810 DNPRINTF(SWM_D_STACK, "vertical_config: id: %d, workspace: %d\n",
3814 case SWM_ARG_ID_STACKRESET:
3815 case SWM_ARG_ID_STACKINIT:
3816 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
3817 ws->l_state.vertical_mwin = 1;
3818 ws->l_state.vertical_stacks = 1;
3820 case SWM_ARG_ID_MASTERSHRINK:
3821 if (ws->l_state.vertical_msize > 1)
3822 ws->l_state.vertical_msize--;
3824 case SWM_ARG_ID_MASTERGROW:
3825 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
3826 ws->l_state.vertical_msize++;
3828 case SWM_ARG_ID_MASTERADD:
3829 ws->l_state.vertical_mwin++;
3831 case SWM_ARG_ID_MASTERDEL:
3832 if (ws->l_state.vertical_mwin > 0)
3833 ws->l_state.vertical_mwin--;
3835 case SWM_ARG_ID_STACKINC:
3836 ws->l_state.vertical_stacks++;
3838 case SWM_ARG_ID_STACKDEC:
3839 if (ws->l_state.vertical_stacks > 1)
3840 ws->l_state.vertical_stacks--;
3842 case SWM_ARG_ID_FLIPLAYOUT:
3843 ws->l_state.vertical_flip = !ws->l_state.vertical_flip;
3851 vertical_stack(struct workspace *ws, struct swm_geometry *g)
3853 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
3855 stack_master(ws, g, 0, ws->l_state.vertical_flip);
3859 horizontal_config(struct workspace *ws, int id)
3861 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
3864 case SWM_ARG_ID_STACKRESET:
3865 case SWM_ARG_ID_STACKINIT:
3866 ws->l_state.horizontal_mwin = 1;
3867 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
3868 ws->l_state.horizontal_stacks = 1;
3870 case SWM_ARG_ID_MASTERSHRINK:
3871 if (ws->l_state.horizontal_msize > 1)
3872 ws->l_state.horizontal_msize--;
3874 case SWM_ARG_ID_MASTERGROW:
3875 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
3876 ws->l_state.horizontal_msize++;
3878 case SWM_ARG_ID_MASTERADD:
3879 ws->l_state.horizontal_mwin++;
3881 case SWM_ARG_ID_MASTERDEL:
3882 if (ws->l_state.horizontal_mwin > 0)
3883 ws->l_state.horizontal_mwin--;
3885 case SWM_ARG_ID_STACKINC:
3886 ws->l_state.horizontal_stacks++;
3888 case SWM_ARG_ID_STACKDEC:
3889 if (ws->l_state.horizontal_stacks > 1)
3890 ws->l_state.horizontal_stacks--;
3892 case SWM_ARG_ID_FLIPLAYOUT:
3893 ws->l_state.horizontal_flip = !ws->l_state.horizontal_flip;
3901 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
3903 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
3905 stack_master(ws, g, 1, ws->l_state.horizontal_flip);
3908 /* fullscreen view */
3910 max_stack(struct workspace *ws, struct swm_geometry *g)
3912 struct swm_geometry gg = *g;
3913 struct ws_win *w, *win = NULL, *parent = NULL;
3914 int winno, num_screens;
3916 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
3921 winno = count_win(ws, 0);
3922 if (winno == 0 && count_win(ws, 1) == 0)
3925 /* Figure out which top level window should be visible. */
3926 if (ws->focus_pending)
3927 win = ws->focus_pending;
3931 win = TAILQ_FIRST(&ws->winlist);
3934 parent = find_window(win->transient);
3936 DNPRINTF(SWM_D_STACK, "max_stack: win: 0x%x\n", win->id);
3938 /* maximize all top level windows */
3939 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
3940 TAILQ_FOREACH(w, &ws->winlist, entry) {
3941 if (w->transient || w->iconic)
3944 if (w->floating && !w->floatmaxed) {
3946 * retain geometry for retrieval on exit from
3949 store_float_geom(w, ws->r);
3953 /* only reconfigure if necessary */
3954 if (X(w) != gg.x || Y(w) != gg.y || WIDTH(w) != gg.w ||
3955 HEIGHT(w) != gg.h) {
3961 WIDTH(w) += 2 * border_width;
3962 HEIGHT(w) += 2 * border_width;
3968 /* Unmap unwanted windows if not multi-screen. */
3969 if (!(num_screens > 1 || outputs > 1) && (w != win ||
3970 w != parent || w->transient != win->id))
3974 /* If a parent exists, map it first. */
3978 /* Map siblings next. */
3979 TAILQ_FOREACH(w, &ws->winlist, entry)
3980 if (w != win && !w->iconic &&
3981 w->transient == parent->id) {
3982 stack_floater(w, ws->r);
3987 /* Map focused window. */
3990 /* Finally, map children of focus window. */
3991 TAILQ_FOREACH(w, &ws->winlist, entry)
3992 if (w->transient == win->id && !w->iconic) {
3993 stack_floater(w, ws->r);
3999 send_to_ws(struct swm_region *r, union arg *args)
4001 int wsid = args->id;
4002 struct ws_win *win = NULL, *parent;
4003 struct workspace *ws, *nws, *pws;
4004 char ws_idx_str[SWM_PROPLEN];
4006 if (wsid >= workspace_limit)
4009 if (r && r->ws && r->ws->focus)
4014 if (win->ws->idx == wsid)
4017 DNPRINTF(SWM_D_MOVE, "send_to_ws: win 0x%x, ws %d -> %d\n", win->id,
4018 win->ws->idx, wsid);
4021 nws = &win->s->ws[wsid];
4023 /* Update the window's workspace property: _SWM_WS */
4024 if (snprintf(ws_idx_str, SWM_PROPLEN, "%d", nws->idx) < SWM_PROPLEN) {
4025 if (focus_mode != SWM_FOCUS_FOLLOW)
4026 ws->focus_pending = get_focus_prev(win);
4028 /* Move the parent if this is a transient window. */
4029 if (win->transient) {
4030 parent = find_window(win->transient);
4033 /* Set new focus in parent's ws if needed. */
4034 if (pws->focus == parent) {
4035 if (focus_mode != SWM_FOCUS_FOLLOW)
4036 pws->focus_pending =
4037 get_focus_prev(parent);
4039 unfocus_win(parent);
4041 if (focus_mode != SWM_FOCUS_FOLLOW)
4042 pws->focus = pws->focus_pending;
4044 if (focus_mode != SWM_FOCUS_FOLLOW)
4045 pws->focus_pending = NULL;
4048 /* Don't unmap parent if new ws is visible */
4050 unmap_window(parent);
4053 TAILQ_REMOVE(&ws->winlist, parent, entry);
4054 TAILQ_INSERT_TAIL(&nws->winlist, parent, entry);
4057 DNPRINTF(SWM_D_PROP, "send_to_ws: set "
4058 "property: _SWM_WS: %s\n", ws_idx_str);
4059 xcb_change_property(conn, XCB_PROP_MODE_REPLACE,
4060 parent->id, a_swm_ws, XCB_ATOM_STRING, 8,
4061 strlen(ws_idx_str), ws_idx_str);
4067 /* Don't unmap if new ws is visible */
4072 TAILQ_REMOVE(&ws->winlist, win, entry);
4073 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
4076 /* Set focus on new ws. */
4077 unfocus_win(nws->focus);
4080 DNPRINTF(SWM_D_PROP, "send_to_ws: set property: _SWM_WS: %s\n",
4082 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
4083 a_swm_ws, XCB_ATOM_STRING, 8, strlen(ws_idx_str),
4086 /* Restack and set new focus. */
4089 if (focus_mode != SWM_FOCUS_FOLLOW) {
4090 focus_win(ws->focus_pending);
4091 ws->focus_pending = NULL;
4097 DNPRINTF(SWM_D_MOVE, "send_to_ws: done.\n");
4101 pressbutton(struct swm_region *r, union arg *args)
4103 /* suppress unused warning since var is needed */
4106 xcb_test_fake_input(conn, XCB_BUTTON_PRESS, args->id,
4107 XCB_CURRENT_TIME, XCB_WINDOW_NONE, 0, 0, 0);
4108 xcb_test_fake_input(conn, XCB_BUTTON_RELEASE, args->id,
4109 XCB_CURRENT_TIME, XCB_WINDOW_NONE, 0, 0, 0);
4113 raise_toggle(struct swm_region *r, union arg *args)
4115 /* suppress unused warning since var is needed */
4118 if (r == NULL || r->ws == NULL)
4121 r->ws->always_raise = !r->ws->always_raise;
4123 /* bring floaters back to top */
4124 if (!r->ws->always_raise)
4131 iconify(struct swm_region *r, union arg *args)
4133 /* suppress unused warning since var is needed */
4136 if (r->ws->focus == NULL)
4139 set_swm_iconic(r->ws->focus, 1);
4145 get_win_name(xcb_window_t win)
4148 xcb_get_property_cookie_t c;
4149 xcb_get_property_reply_t *r;
4151 /* First try _NET_WM_NAME for UTF-8. */
4152 c = xcb_get_property(conn, 0, win, a_netwmname,
4153 XCB_GET_PROPERTY_TYPE_ANY, 0, UINT_MAX);
4154 r = xcb_get_property_reply(conn, c, NULL);
4156 if (!r || r->type == XCB_NONE) {
4158 /* Use WM_NAME instead; no UTF-8. */
4159 c = xcb_get_property(conn, 0, win, XCB_ATOM_WM_NAME,
4160 XCB_GET_PROPERTY_TYPE_ANY, 0, UINT_MAX);
4161 r = xcb_get_property_reply(conn, c, NULL);
4163 if(!r || r->type == XCB_NONE) {
4170 name = strndup(xcb_get_property_value(r),
4171 xcb_get_property_value_length(r));
4178 uniconify(struct swm_region *r, union arg *args)
4185 DNPRINTF(SWM_D_MISC, "uniconify\n");
4187 if (r == NULL || r->ws == NULL)
4190 /* make sure we have anything to uniconify */
4191 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
4192 if (win->ws == NULL)
4193 continue; /* should never happen */
4202 search_resp_action = SWM_SEARCH_UNICONIFY;
4204 spawn_select(r, args, "search", &searchpid);
4206 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
4209 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
4210 if (win->ws == NULL)
4211 continue; /* should never happen */
4215 name = get_win_name(win->id);
4218 fprintf(lfile, "%s.%u\n", name, win->id);
4226 name_workspace(struct swm_region *r, union arg *args)
4230 DNPRINTF(SWM_D_MISC, "name_workspace\n");
4236 search_resp_action = SWM_SEARCH_NAME_WORKSPACE;
4238 spawn_select(r, args, "name_workspace", &searchpid);
4240 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
4243 fprintf(lfile, "%s", "");
4248 search_workspace(struct swm_region *r, union arg *args)
4251 struct workspace *ws;
4254 DNPRINTF(SWM_D_MISC, "search_workspace\n");
4260 search_resp_action = SWM_SEARCH_SEARCH_WORKSPACE;
4262 spawn_select(r, args, "search", &searchpid);
4264 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
4267 for (i = 0; i < workspace_limit; i++) {
4271 fprintf(lfile, "%d%s%s\n", ws->idx + 1,
4272 (ws->name ? ":" : ""), (ws->name ? ws->name : ""));
4279 search_win_cleanup(void)
4281 struct search_window *sw = NULL;
4283 while ((sw = TAILQ_FIRST(&search_wl)) != NULL) {
4284 xcb_destroy_window(conn, sw->indicator);
4285 TAILQ_REMOVE(&search_wl, sw, entry);
4291 search_win(struct swm_region *r, union arg *args)
4293 struct ws_win *win = NULL;
4294 struct search_window *sw = NULL;
4297 int i, width, height;
4305 XRectangle l_ibox, l_lbox;
4307 DNPRINTF(SWM_D_MISC, "search_win\n");
4310 search_resp_action = SWM_SEARCH_SEARCH_WINDOW;
4312 spawn_select(r, args, "search", &searchpid);
4314 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
4317 TAILQ_INIT(&search_wl);
4320 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
4324 sw = calloc(1, sizeof(struct search_window));
4326 warn("search_win: calloc");
4328 search_win_cleanup();
4334 snprintf(s, sizeof s, "%d", i);
4337 w = xcb_generate_id(conn);
4338 wa[0] = r->s->c[SWM_S_COLOR_FOCUS].pixel;
4339 wa[1] = r->s->c[SWM_S_COLOR_UNFOCUS].pixel;
4341 if (bar_font_legacy) {
4342 XmbTextExtents(bar_fs, s, len, &l_ibox, &l_lbox);
4343 width = l_lbox.width + 4;
4344 height = bar_fs_extents->max_logical_extent.height + 4;
4346 XftTextExtentsUtf8(display, bar_font, (FcChar8 *)s, len,
4348 width = info.width + 4;
4349 height = bar_font->height + 4;
4352 xcb_create_window(conn, XCB_COPY_FROM_PARENT, w, win->id, 0, 0,
4353 width, height, 1, XCB_WINDOW_CLASS_INPUT_OUTPUT,
4354 XCB_COPY_FROM_PARENT, XCB_CW_BACK_PIXEL |
4355 XCB_CW_BORDER_PIXEL, wa);
4357 xcb_map_window(conn, w);
4360 TAILQ_INSERT_TAIL(&search_wl, sw, entry);
4362 if (bar_font_legacy) {
4363 l_gcv.graphics_exposures = 0;
4364 l_draw = XCreateGC(display, w, 0, &l_gcv);
4366 XSetForeground(display, l_draw,
4367 r->s->c[SWM_S_COLOR_BAR].pixel);
4369 DRAWSTRING(display, w, bar_fs, l_draw, 2,
4370 (bar_fs_extents->max_logical_extent.height -
4371 l_lbox.height) / 2 - l_lbox.y, s, len);
4373 XFreeGC(display, l_draw);
4376 draw = XftDrawCreate(display, w,
4377 DefaultVisual(display, r->s->idx),
4378 DefaultColormap(display, r->s->idx));
4380 XftDrawStringUtf8(draw, &bar_font_color, bar_font, 2,
4381 (HEIGHT(r->bar) + bar_font->height) / 2 -
4382 bar_font->descent, (FcChar8 *)s, len);
4384 XftDrawDestroy(draw);
4387 DNPRINTF(SWM_D_MISC, "search_win: mapped window: 0x%x\n", w);
4389 fprintf(lfile, "%d\n", i);
4399 search_resp_uniconify(char *resp, unsigned long len)
4405 DNPRINTF(SWM_D_MISC, "search_resp_uniconify: resp: %s\n", resp);
4407 TAILQ_FOREACH(win, &search_r->ws->winlist, entry) {
4410 name = get_win_name(win->id);
4413 if (asprintf(&s, "%s.%u", name, win->id) == -1) {
4418 if (strncmp(s, resp, len) == 0) {
4419 /* XXX this should be a callback to generalize */
4420 set_swm_iconic(win, 0);
4429 search_resp_name_workspace(char *resp, unsigned long len)
4431 struct workspace *ws;
4433 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: resp: %s\n", resp);
4435 if (search_r->ws == NULL)
4440 free(search_r->ws->name);
4441 search_r->ws->name = NULL;
4445 ws->name = strdup(resp);
4446 if (ws->name == NULL) {
4447 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: "
4448 "strdup: %s", strerror(errno));
4455 search_resp_search_workspace(char *resp)
4462 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: resp: %s\n", resp);
4466 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: strdup: %s",
4473 ws_idx = (int)strtonum(q, 1, workspace_limit, &errstr);
4475 DNPRINTF(SWM_D_MISC, "workspace idx is %s: %s",
4482 switchws(search_r, &a);
4486 search_resp_search_window(char *resp)
4491 struct search_window *sw;
4493 DNPRINTF(SWM_D_MISC, "search_resp_search_window: resp: %s\n", resp);
4497 DNPRINTF(SWM_D_MISC, "search_resp_search_window: strdup: %s",
4502 idx = (int)strtonum(s, 1, INT_MAX, &errstr);
4504 DNPRINTF(SWM_D_MISC, "window idx is %s: %s",
4511 TAILQ_FOREACH(sw, &search_wl, entry)
4512 if (idx == sw->idx) {
4518 #define MAX_RESP_LEN 1024
4521 search_do_resp(void)
4527 DNPRINTF(SWM_D_MISC, "search_do_resp:\n");
4532 if ((resp = calloc(1, MAX_RESP_LEN + 1)) == NULL) {
4533 warn("search: calloc");
4537 rbytes = read(select_resp_pipe[0], resp, MAX_RESP_LEN);
4539 warn("search: read error");
4542 resp[rbytes] = '\0';
4545 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
4546 * newline, so work around that by sanitizing the resp now.
4548 resp[strcspn(resp, "\n")] = '\0';
4551 switch (search_resp_action) {
4552 case SWM_SEARCH_UNICONIFY:
4553 search_resp_uniconify(resp, len);
4555 case SWM_SEARCH_NAME_WORKSPACE:
4556 search_resp_name_workspace(resp, len);
4558 case SWM_SEARCH_SEARCH_WORKSPACE:
4559 search_resp_search_workspace(resp);
4561 case SWM_SEARCH_SEARCH_WINDOW:
4562 search_resp_search_window(resp);
4567 if (search_resp_action == SWM_SEARCH_SEARCH_WINDOW)
4568 search_win_cleanup();
4570 search_resp_action = SWM_SEARCH_NONE;
4571 close(select_resp_pipe[0]);
4578 wkill(struct swm_region *r, union arg *args)
4580 DNPRINTF(SWM_D_MISC, "wkill: id: %d\n", args->id);
4582 if (r->ws->focus == NULL)
4585 if (args->id == SWM_ARG_ID_KILLWINDOW)
4586 xcb_kill_client(conn, r->ws->focus->id);
4588 if (r->ws->focus->can_delete)
4589 client_msg(r->ws->focus, a_delete, 0);
4595 floating_toggle_win(struct ws_win *win)
4597 struct swm_region *r;
4607 /* reject floating toggles in max stack mode */
4608 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK])
4611 if (win->floating) {
4612 if (!win->floatmaxed) {
4613 /* retain position for refloat */
4614 store_float_geom(win, r);
4618 load_float_geom(win, r);
4622 ewmh_update_actions(win);
4628 floating_toggle(struct swm_region *r, union arg *args)
4630 struct ws_win *win = r->ws->focus;
4632 /* suppress unused warning since var is needed */
4638 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
4641 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
4642 _NET_WM_STATE_TOGGLE);
4646 if (win == win->ws->focus)
4653 constrain_window(struct ws_win *win, struct swm_region *r, int resizable)
4655 if (MAX_X(win) + BORDER(win) > MAX_X(r)) {
4657 WIDTH(win) = MAX_X(r) - X(win) - BORDER(win);
4659 X(win) = MAX_X(r)- WIDTH(win) - BORDER(win);
4662 if (X(win) + BORDER(win) < X(r)) {
4664 WIDTH(win) -= X(r) - X(win) - BORDER(win);
4666 X(win) = X(r) - BORDER(win);
4669 if (MAX_Y(win) + BORDER(win) > MAX_Y(r)) {
4671 HEIGHT(win) = MAX_Y(r) - Y(win) - BORDER(win);
4673 Y(win) = MAX_Y(r) - HEIGHT(win) - BORDER(win);
4676 if (Y(win) + BORDER(win) < Y(r)) {
4678 HEIGHT(win) -= Y(r) - Y(win) - BORDER(win);
4680 Y(win) = Y(r) - BORDER(win);
4686 if (HEIGHT(win) < 1)
4692 update_window(struct ws_win *win)
4697 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
4698 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
4699 XCB_CONFIG_WINDOW_BORDER_WIDTH;
4703 wc[3] = HEIGHT(win);
4704 wc[4] = BORDER(win);
4706 DNPRINTF(SWM_D_EVENT, "update_window: window: 0x%x, (x,y) w x h: "
4707 "(%d,%d) %d x %d, bordered: %s\n", win->id, wc[0], wc[1], wc[2],
4708 wc[3], YESNO(win->bordered));
4710 xcb_configure_window(conn, win->id, mask, wc);
4713 #define SWM_RESIZE_STEPS (50)
4716 resize(struct ws_win *win, union arg *args)
4718 xcb_timestamp_t timestamp = 0;
4719 struct swm_region *r = NULL;
4720 int resize_step = 0;
4721 struct swm_geometry g;
4722 int top = 0, left = 0, resizing;
4724 unsigned int shape; /* cursor style */
4725 xcb_cursor_t cursor;
4726 xcb_font_t cursor_font;
4727 xcb_query_pointer_reply_t *xpr;
4728 xcb_generic_event_t *evt;
4729 xcb_motion_notify_event_t *mne;
4735 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
4738 DNPRINTF(SWM_D_EVENT, "resize: window: 0x%x, floating: %s, "
4739 "transient: 0x%x\n", win->id, YESNO(win->floating),
4742 if (!win->transient && !win->floating)
4745 /* reject resizes in max mode for floaters (transient ok) */
4746 if (win->floatmaxed)
4750 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
4758 case SWM_ARG_ID_WIDTHSHRINK:
4759 WIDTH(win) -= SWM_RESIZE_STEPS;
4762 case SWM_ARG_ID_WIDTHGROW:
4763 WIDTH(win) += SWM_RESIZE_STEPS;
4766 case SWM_ARG_ID_HEIGHTSHRINK:
4767 HEIGHT(win) -= SWM_RESIZE_STEPS;
4770 case SWM_ARG_ID_HEIGHTGROW:
4771 HEIGHT(win) += SWM_RESIZE_STEPS;
4778 constrain_window(win, r, 1);
4780 store_float_geom(win,r);
4784 /* get cursor offset from window root */
4785 xpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, win->id),
4792 if (xpr->win_x < WIDTH(win) / 2)
4795 if (xpr->win_y < HEIGHT(win) / 2)
4798 if (args->id == SWM_ARG_ID_CENTER)
4801 shape = (left) ? XC_top_left_corner : XC_top_right_corner;
4803 shape = (left) ? XC_bottom_left_corner : XC_bottom_right_corner;
4805 cursor_font = xcb_generate_id(conn);
4806 xcb_open_font(conn, cursor_font, strlen("cursor"), "cursor");
4808 cursor = xcb_generate_id(conn);
4809 xcb_create_glyph_cursor(conn, cursor, cursor_font, cursor_font,
4810 shape, shape + 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
4812 xcb_grab_pointer(conn, 0, win->id, MOUSEMASK,
4813 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, XCB_WINDOW_NONE, cursor,
4818 while ((evt = xcb_wait_for_event(conn)) && resizing) {
4819 switch (XCB_EVENT_RESPONSE_TYPE(evt)) {
4820 case XCB_BUTTON_RELEASE:
4821 DNPRINTF(SWM_D_EVENT, "resize: BUTTON_RELEASE\n");
4824 case XCB_MOTION_NOTIFY:
4825 mne = (xcb_motion_notify_event_t *)evt;
4826 /* cursor offset/delta from start of the operation */
4827 dx = mne->root_x - xpr->root_x;
4828 dy = mne->root_y - xpr->root_y;
4834 if (args->id == SWM_ARG_ID_CENTER) {
4835 if (g.h / 2 + dy < 1)
4839 HEIGHT(win) = g.h + 2 * dy;
4847 HEIGHT(win) = g.h + dy;
4854 if (args->id == SWM_ARG_ID_CENTER) {
4855 if (g.w / 2 + dx < 1)
4859 WIDTH(win) = g.w + 2 * dx;
4867 WIDTH(win) = g.w + dx;
4870 constrain_window(win, r, 1);
4872 /* not free, don't sync more than 120 times / second */
4873 if ((mne->time - timestamp) > (1000 / 120) ) {
4874 timestamp = mne->time;
4889 store_float_geom(win,r);
4891 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
4892 xcb_free_cursor(conn, cursor);
4893 xcb_close_font(conn, cursor_font);
4895 DNPRINTF(SWM_D_EVENT, "resize: done.\n");
4899 resize_step(struct swm_region *r, union arg *args)
4901 struct ws_win *win = NULL;
4903 if (r && r->ws && r->ws->focus)
4912 #define SWM_MOVE_STEPS (50)
4915 move(struct ws_win *win, union arg *args)
4917 xcb_timestamp_t timestamp = 0;
4918 int move_step = 0, moving;
4919 struct swm_region *r = NULL;
4920 xcb_font_t cursor_font;
4921 xcb_cursor_t cursor;
4922 xcb_query_pointer_reply_t *qpr;
4923 xcb_generic_event_t *evt;
4924 xcb_motion_notify_event_t *mne;
4930 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
4933 DNPRINTF(SWM_D_EVENT, "move: window: 0x%x, floating: %s, transient: "
4934 "0x%x\n", win->id, YESNO(win->floating), win->transient);
4936 /* in max_stack mode should only move transients */
4937 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !win->transient)
4941 if (!win->floating && !win->transient) {
4942 store_float_geom(win, r);
4943 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
4946 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
4955 case SWM_ARG_ID_MOVELEFT:
4956 X(win) -= (SWM_MOVE_STEPS - border_width);
4959 case SWM_ARG_ID_MOVERIGHT:
4960 X(win) += (SWM_MOVE_STEPS - border_width);
4963 case SWM_ARG_ID_MOVEUP:
4964 Y(win) -= (SWM_MOVE_STEPS - border_width);
4967 case SWM_ARG_ID_MOVEDOWN:
4968 Y(win) += (SWM_MOVE_STEPS - border_width);
4975 constrain_window(win, r, 0);
4977 store_float_geom(win, r);
4981 cursor_font = xcb_generate_id(conn);
4982 xcb_open_font(conn, cursor_font, strlen("cursor"), "cursor");
4984 cursor = xcb_generate_id(conn);
4985 xcb_create_glyph_cursor(conn, cursor, cursor_font, cursor_font,
4986 XC_fleur, XC_fleur + 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
4988 xcb_grab_pointer(conn, 0, win->id, MOUSEMASK,
4989 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC,
4990 XCB_WINDOW_NONE, cursor, XCB_CURRENT_TIME);
4992 /* get cursor offset from window root */
4993 qpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, win->id),
4996 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
4997 xcb_free_cursor(conn, cursor);
4998 xcb_close_font(conn, cursor_font);
5004 while ((evt = xcb_wait_for_event(conn)) && moving) {
5005 switch (XCB_EVENT_RESPONSE_TYPE(evt)) {
5006 case XCB_BUTTON_RELEASE:
5007 DNPRINTF(SWM_D_EVENT, "move: BUTTON_RELEASE\n");
5010 case XCB_MOTION_NOTIFY:
5011 mne = (xcb_motion_notify_event_t *)evt;
5012 X(win) = mne->root_x - qpr->win_x - border_width;
5013 Y(win) = mne->root_y - qpr->win_y - border_width;
5015 constrain_window(win, r, 0);
5017 /* not free, don't sync more than 120 times / second */
5018 if ((mne->time - timestamp) > (1000 / 120) ) {
5019 timestamp = mne->time;
5034 store_float_geom(win, r);
5036 xcb_free_cursor(conn, cursor);
5037 xcb_close_font(conn, cursor_font);
5038 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
5039 DNPRINTF(SWM_D_EVENT, "move: done.\n");
5043 move_step(struct swm_region *r, union arg *args)
5045 struct ws_win *win = NULL;
5047 if (r && r->ws && r->ws->focus)
5052 if (!win->transient && !win->floating)
5059 /* user/key callable function IDs */
5109 KF_SEARCH_WORKSPACE,
5150 KF_DUMPWINS, /* MUST BE LAST */
5154 /* key definitions */
5156 char name[SWM_FUNCNAME_LEN];
5157 void (*func)(struct swm_region *r, union arg *);
5159 } keyfuncs[KF_INVALID + 1] = {
5160 /* name function argument */
5161 { "bar_toggle", bar_toggle, {0} },
5162 { "button2", pressbutton, {2} },
5163 { "cycle_layout", cycle_layout, {0} },
5164 { "flip_layout", stack_config, {.id = SWM_ARG_ID_FLIPLAYOUT} },
5165 { "float_toggle", floating_toggle,{0} },
5166 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
5167 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
5168 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
5169 { "height_grow", resize_step, {.id = SWM_ARG_ID_HEIGHTGROW} },
5170 { "height_shrink", resize_step, {.id = SWM_ARG_ID_HEIGHTSHRINK} },
5171 { "iconify", iconify, {0} },
5172 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
5173 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
5174 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
5175 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
5176 { "move_down", move_step, {.id = SWM_ARG_ID_MOVEDOWN} },
5177 { "move_left", move_step, {.id = SWM_ARG_ID_MOVELEFT} },
5178 { "move_right", move_step, {.id = SWM_ARG_ID_MOVERIGHT} },
5179 { "move_up", move_step, {.id = SWM_ARG_ID_MOVEUP} },
5180 { "mvws_1", send_to_ws, {.id = 0} },
5181 { "mvws_2", send_to_ws, {.id = 1} },
5182 { "mvws_3", send_to_ws, {.id = 2} },
5183 { "mvws_4", send_to_ws, {.id = 3} },
5184 { "mvws_5", send_to_ws, {.id = 4} },
5185 { "mvws_6", send_to_ws, {.id = 5} },
5186 { "mvws_7", send_to_ws, {.id = 6} },
5187 { "mvws_8", send_to_ws, {.id = 7} },
5188 { "mvws_9", send_to_ws, {.id = 8} },
5189 { "mvws_10", send_to_ws, {.id = 9} },
5190 { "mvws_11", send_to_ws, {.id = 10} },
5191 { "mvws_12", send_to_ws, {.id = 11} },
5192 { "mvws_13", send_to_ws, {.id = 12} },
5193 { "mvws_14", send_to_ws, {.id = 13} },
5194 { "mvws_15", send_to_ws, {.id = 14} },
5195 { "mvws_16", send_to_ws, {.id = 15} },
5196 { "mvws_17", send_to_ws, {.id = 16} },
5197 { "mvws_18", send_to_ws, {.id = 17} },
5198 { "mvws_19", send_to_ws, {.id = 18} },
5199 { "mvws_20", send_to_ws, {.id = 19} },
5200 { "mvws_21", send_to_ws, {.id = 20} },
5201 { "mvws_22", send_to_ws, {.id = 21} },
5202 { "name_workspace", name_workspace, {0} },
5203 { "quit", quit, {0} },
5204 { "raise_toggle", raise_toggle, {0} },
5205 { "restart", restart, {0} },
5206 { "screen_next", cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
5207 { "screen_prev", cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
5208 { "search_win", search_win, {0} },
5209 { "search_workspace", search_workspace, {0} },
5210 { "spawn_custom", NULL, {0} },
5211 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
5212 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
5213 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
5214 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
5215 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
5216 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
5217 { "uniconify", uniconify, {0} },
5218 { "version", version, {0} },
5219 { "width_grow", resize_step, {.id = SWM_ARG_ID_WIDTHGROW} },
5220 { "width_shrink", resize_step, {.id = SWM_ARG_ID_WIDTHSHRINK} },
5221 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
5222 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
5223 { "ws_1", switchws, {.id = 0} },
5224 { "ws_2", switchws, {.id = 1} },
5225 { "ws_3", switchws, {.id = 2} },
5226 { "ws_4", switchws, {.id = 3} },
5227 { "ws_5", switchws, {.id = 4} },
5228 { "ws_6", switchws, {.id = 5} },
5229 { "ws_7", switchws, {.id = 6} },
5230 { "ws_8", switchws, {.id = 7} },
5231 { "ws_9", switchws, {.id = 8} },
5232 { "ws_10", switchws, {.id = 9} },
5233 { "ws_11", switchws, {.id = 10} },
5234 { "ws_12", switchws, {.id = 11} },
5235 { "ws_13", switchws, {.id = 12} },
5236 { "ws_14", switchws, {.id = 13} },
5237 { "ws_15", switchws, {.id = 14} },
5238 { "ws_16", switchws, {.id = 15} },
5239 { "ws_17", switchws, {.id = 16} },
5240 { "ws_18", switchws, {.id = 17} },
5241 { "ws_19", switchws, {.id = 18} },
5242 { "ws_20", switchws, {.id = 19} },
5243 { "ws_21", switchws, {.id = 20} },
5244 { "ws_22", switchws, {.id = 21} },
5245 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
5246 { "ws_next_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP_ALL} },
5247 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
5248 { "ws_prev_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN_ALL} },
5249 { "ws_prior", priorws, {0} },
5250 { "dumpwins", dumpwins, {0} }, /* MUST BE LAST */
5251 { "invalid key func", NULL, {0} },
5254 RB_ENTRY(key) entry;
5257 enum keyfuncid funcid;
5260 RB_HEAD(key_tree, key);
5263 key_cmp(struct key *kp1, struct key *kp2)
5265 if (kp1->keysym < kp2->keysym)
5267 if (kp1->keysym > kp2->keysym)
5270 if (kp1->mod < kp2->mod)
5272 if (kp1->mod > kp2->mod)
5278 RB_GENERATE(key_tree, key, entry, key_cmp);
5279 struct key_tree keys;
5282 enum { client_click, root_click };
5284 unsigned int action;
5286 unsigned int button;
5287 void (*func)(struct ws_win *, union arg *);
5290 #define MODKEY_SHIFT MODKEY | XCB_MOD_MASK_SHIFT
5291 /* action key mouse button func args */
5292 { client_click, MODKEY, XCB_BUTTON_INDEX_3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
5293 { client_click, MODKEY_SHIFT, XCB_BUTTON_INDEX_3, resize, {.id = SWM_ARG_ID_CENTER} },
5294 { client_click, MODKEY, XCB_BUTTON_INDEX_1, move, {0} },
5299 update_modkey(unsigned int mod)
5305 RB_FOREACH(kp, key_tree, &keys)
5306 if (kp->mod & XCB_MOD_MASK_SHIFT)
5307 kp->mod = mod | XCB_MOD_MASK_SHIFT;
5311 for (i = 0; i < LENGTH(buttons); i++)
5312 if (buttons[i].mask & XCB_MOD_MASK_SHIFT)
5313 buttons[i].mask = mod | XCB_MOD_MASK_SHIFT;
5315 buttons[i].mask = mod;
5320 TAILQ_ENTRY(spawn_prog) entry;
5325 TAILQ_HEAD(spawn_list, spawn_prog);
5326 struct spawn_list spawns = TAILQ_HEAD_INITIALIZER(spawns);
5329 spawn_expand(struct swm_region *r, union arg *args, char *spawn_name,
5332 struct spawn_prog *prog = NULL;
5334 char *ap, **real_args;
5336 /* suppress unused warning since var is needed */
5339 DNPRINTF(SWM_D_SPAWN, "spawn_expand: %s\n", spawn_name);
5342 TAILQ_FOREACH(prog, &spawns, entry) {
5343 if (!strcasecmp(spawn_name, prog->name))
5347 warnx("spawn_custom: program %s not found", spawn_name);
5351 /* make room for expanded args */
5352 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
5353 err(1, "spawn_custom: calloc real_args");
5355 /* expand spawn_args into real_args */
5356 for (i = 0; i < prog->argc; i++) {
5358 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg: %s\n", ap);
5359 if (!strcasecmp(ap, "$bar_border")) {
5361 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
5363 err(1, "spawn_custom border color");
5364 } else if (!strcasecmp(ap, "$bar_color")) {
5366 strdup(r->s->c[SWM_S_COLOR_BAR].name))
5368 err(1, "spawn_custom bar color");
5369 } else if (!strcasecmp(ap, "$bar_font")) {
5370 if ((real_args[i] = strdup(bar_fonts))
5372 err(1, "spawn_custom bar fonts");
5373 } else if (!strcasecmp(ap, "$bar_font_color")) {
5375 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
5377 err(1, "spawn_custom color font");
5378 } else if (!strcasecmp(ap, "$color_focus")) {
5380 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
5382 err(1, "spawn_custom color focus");
5383 } else if (!strcasecmp(ap, "$color_unfocus")) {
5385 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
5387 err(1, "spawn_custom color unfocus");
5389 /* no match --> copy as is */
5390 if ((real_args[i] = strdup(ap)) == NULL)
5391 err(1, "spawn_custom strdup(ap)");
5393 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg: %s\n",
5398 DNPRINTF(SWM_D_SPAWN, "spawn_custom: result: ");
5399 for (i = 0; i < prog->argc; i++)
5400 DNPRINTF(SWM_D_SPAWN, "\"%s\" ", real_args[i]);
5401 DNPRINTF(SWM_D_SPAWN, "\n");
5403 *ret_args = real_args;
5404 return (prog->argc);
5408 spawn_custom(struct swm_region *r, union arg *args, char *spawn_name)
5414 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
5418 spawn(r->ws->idx, &a, 1);
5420 for (i = 0; i < spawn_argc; i++)
5426 spawn_select(struct swm_region *r, union arg *args, char *spawn_name, int *pid)
5432 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
5436 if (pipe(select_list_pipe) == -1)
5437 err(1, "pipe error");
5438 if (pipe(select_resp_pipe) == -1)
5439 err(1, "pipe error");
5441 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
5442 err(1, "could not disable SIGPIPE");
5443 switch (*pid = fork()) {
5445 err(1, "cannot fork");
5448 if (dup2(select_list_pipe[0], 0) == -1)
5450 if (dup2(select_resp_pipe[1], 1) == -1)
5452 close(select_list_pipe[1]);
5453 close(select_resp_pipe[0]);
5454 spawn(r->ws->idx, &a, 0);
5456 default: /* parent */
5457 close(select_list_pipe[0]);
5458 close(select_resp_pipe[1]);
5462 for (i = 0; i < spawn_argc; i++)
5468 spawn_insert(char *name, char *args)
5470 char *arg, *cp, *ptr;
5471 struct spawn_prog *sp;
5473 DNPRINTF(SWM_D_SPAWN, "spawn_insert: %s\n", name);
5475 if ((sp = calloc(1, sizeof *sp)) == NULL)
5476 err(1, "spawn_insert: calloc");
5477 if ((sp->name = strdup(name)) == NULL)
5478 err(1, "spawn_insert: strdup");
5480 /* convert the arguments to an argument list */
5481 if ((ptr = cp = strdup(args)) == NULL)
5482 err(1, "spawn_insert: strdup");
5483 while ((arg = strsep(&ptr, " \t")) != NULL) {
5484 /* empty field; skip it */
5489 if ((sp->argv = realloc(sp->argv, sp->argc *
5490 sizeof *sp->argv)) == NULL)
5491 err(1, "spawn_insert: realloc");
5492 if ((sp->argv[sp->argc - 1] = strdup(arg)) == NULL)
5493 err(1, "spawn_insert: strdup");
5497 TAILQ_INSERT_TAIL(&spawns, sp, entry);
5498 DNPRINTF(SWM_D_SPAWN, "spawn_insert: leave\n");
5502 spawn_remove(struct spawn_prog *sp)
5506 DNPRINTF(SWM_D_SPAWN, "spawn_remove: %s\n", sp->name);
5508 TAILQ_REMOVE(&spawns, sp, entry);
5509 for (i = 0; i < sp->argc; i++)
5515 DNPRINTF(SWM_D_SPAWN, "spawn_remove: leave\n");
5519 spawn_replace(struct spawn_prog *sp, char *name, char *args)
5521 DNPRINTF(SWM_D_SPAWN, "spawn_replace: %s [%s]\n", sp->name, name);
5524 spawn_insert(name, args);
5526 DNPRINTF(SWM_D_SPAWN, "spawn_replace: leave\n");
5530 setspawn(char *name, char *args)
5532 struct spawn_prog *sp;
5534 DNPRINTF(SWM_D_SPAWN, "setspawn: %s\n", name);
5539 TAILQ_FOREACH(sp, &spawns, entry) {
5540 if (!strcmp(sp->name, name)) {
5544 spawn_replace(sp, name, args);
5545 DNPRINTF(SWM_D_SPAWN, "setspawn: leave\n");
5549 if (*args == '\0') {
5550 warnx("error: setspawn: cannot find program: %s", name);
5554 spawn_insert(name, args);
5555 DNPRINTF(SWM_D_SPAWN, "setspawn: leave\n");
5559 setconfspawn(char *selector, char *value, int flags)
5563 /* suppress unused warning since var is needed */
5566 args = expand_tilde(value);
5568 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, args);
5570 setspawn(selector, args);
5573 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done.\n");
5580 setconfspawn("term", "xterm", 0);
5581 setconfspawn("spawn_term", "xterm", 0);
5582 setconfspawn("screenshot_all", "screenshot.sh full", 0);
5583 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
5584 setconfspawn("lock", "xlock", 0);
5585 setconfspawn("initscr", "initscreen.sh", 0);
5586 setconfspawn("menu", "dmenu_run"
5589 " -nf $bar_font_color"
5591 " -sf $bar_color", 0);
5592 setconfspawn("search", "dmenu"
5596 " -nf $bar_font_color"
5598 " -sf $bar_color", 0);
5599 setconfspawn("name_workspace", "dmenu"
5603 " -nf $bar_font_color"
5605 " -sf $bar_color", 0);
5609 #define SWM_MODNAME_SIZE 32
5610 #define SWM_KEY_WS "\n+ \t"
5612 parsekeys(char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
5616 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
5617 if (mod == NULL || ks == NULL) {
5618 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
5621 if (keystr == NULL || strlen(keystr) == 0) {
5622 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
5626 *ks = XCB_NO_SYMBOL;
5628 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
5629 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
5631 cp += (long)strspn(cp, SWM_KEY_WS);
5632 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
5634 else if (!strncasecmp(name, "Mod1", SWM_MODNAME_SIZE))
5635 *mod |= XCB_MOD_MASK_1;
5636 else if (!strncasecmp(name, "Mod2", SWM_MODNAME_SIZE))
5637 *mod += XCB_MOD_MASK_2;
5638 else if (!strncmp(name, "Mod3", SWM_MODNAME_SIZE))
5639 *mod |= XCB_MOD_MASK_3;
5640 else if (!strncmp(name, "Mod4", SWM_MODNAME_SIZE))
5641 *mod |= XCB_MOD_MASK_4;
5642 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
5643 *mod |= XCB_MOD_MASK_SHIFT;
5644 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
5645 *mod |= XCB_MOD_MASK_CONTROL;
5647 *ks = XStringToKeysym(name);
5648 XConvertCase(*ks, ks, &uks);
5649 if (ks == XCB_NO_SYMBOL) {
5651 "parsekeys: invalid key %s\n",
5657 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
5662 strdupsafe(char *str)
5667 return (strdup(str));
5671 key_insert(unsigned int mod, KeySym ks, enum keyfuncid kfid, char *spawn_name)
5675 DNPRINTF(SWM_D_KEY, "key_insert: enter %s [%s]\n",
5676 keyfuncs[kfid].name, spawn_name);
5678 if ((kp = malloc(sizeof *kp)) == NULL)
5679 err(1, "key_insert: malloc");
5684 kp->spawn_name = strdupsafe(spawn_name);
5685 RB_INSERT(key_tree, &keys, kp);
5687 DNPRINTF(SWM_D_KEY, "key_insert: leave\n");
5691 key_lookup(unsigned int mod, KeySym ks)
5698 return (RB_FIND(key_tree, &keys, &kp));
5702 key_remove(struct key *kp)
5704 DNPRINTF(SWM_D_KEY, "key_remove: %s\n", keyfuncs[kp->funcid].name);
5706 RB_REMOVE(key_tree, &keys, kp);
5707 free(kp->spawn_name);
5710 DNPRINTF(SWM_D_KEY, "key_remove: leave\n");
5714 key_replace(struct key *kp, unsigned int mod, KeySym ks, enum keyfuncid kfid,
5717 DNPRINTF(SWM_D_KEY, "key_replace: %s [%s]\n", keyfuncs[kp->funcid].name,
5721 key_insert(mod, ks, kfid, spawn_name);
5723 DNPRINTF(SWM_D_KEY, "key_replace: leave\n");
5727 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid,
5732 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
5733 keyfuncs[kfid].name, spawn_name);
5735 if ((kp = key_lookup(mod, ks)) != NULL) {
5736 if (kfid == KF_INVALID)
5739 key_replace(kp, mod, ks, kfid, spawn_name);
5740 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
5743 if (kfid == KF_INVALID) {
5744 warnx("error: setkeybinding: cannot find mod/key combination");
5745 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
5749 key_insert(mod, ks, kfid, spawn_name);
5750 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
5754 setconfbinding(char *selector, char *value, int flags)
5756 enum keyfuncid kfid;
5759 struct spawn_prog *sp;
5761 /* suppress unused warning since var is needed */
5764 DNPRINTF(SWM_D_KEY, "setconfbinding: enter\n");
5765 if (selector == NULL) {
5766 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
5767 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
5769 setkeybinding(mod, ks, kfid, NULL);
5774 /* search by key function name */
5775 for (kfid = 0; kfid < KF_INVALID; (kfid)++) {
5776 if (strncasecmp(selector, keyfuncs[kfid].name,
5777 SWM_FUNCNAME_LEN) == 0) {
5778 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
5780 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
5781 setkeybinding(mod, ks, kfid, NULL);
5787 /* search by custom spawn name */
5788 TAILQ_FOREACH(sp, &spawns, entry) {
5789 if (strcasecmp(selector, sp->name) == 0) {
5790 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
5792 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
5793 setkeybinding(mod, ks, KF_SPAWN_CUSTOM,
5800 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
5807 #define MODKEY_SHIFT MODKEY | XCB_MOD_MASK_SHIFT
5808 setkeybinding(MODKEY, XK_space, KF_CYCLE_LAYOUT,NULL);
5809 setkeybinding(MODKEY_SHIFT, XK_backslash, KF_FLIP_LAYOUT, NULL);
5810 setkeybinding(MODKEY_SHIFT, XK_space, KF_STACK_RESET, NULL);
5811 setkeybinding(MODKEY, XK_h, KF_MASTER_SHRINK, NULL);
5812 setkeybinding(MODKEY, XK_l, KF_MASTER_GROW, NULL);
5813 setkeybinding(MODKEY, XK_comma, KF_MASTER_ADD, NULL);
5814 setkeybinding(MODKEY, XK_period, KF_MASTER_DEL, NULL);
5815 setkeybinding(MODKEY_SHIFT, XK_comma, KF_STACK_INC, NULL);
5816 setkeybinding(MODKEY_SHIFT, XK_period, KF_STACK_DEC, NULL);
5817 setkeybinding(MODKEY, XK_Return, KF_SWAP_MAIN, NULL);
5818 setkeybinding(MODKEY, XK_j, KF_FOCUS_NEXT, NULL);
5819 setkeybinding(MODKEY, XK_k, KF_FOCUS_PREV, NULL);
5820 setkeybinding(MODKEY_SHIFT, XK_j, KF_SWAP_NEXT, NULL);
5821 setkeybinding(MODKEY_SHIFT, XK_k, KF_SWAP_PREV, NULL);
5822 setkeybinding(MODKEY_SHIFT, XK_Return, KF_SPAWN_CUSTOM,"term");
5823 setkeybinding(MODKEY, XK_p, KF_SPAWN_CUSTOM,"menu");
5824 setkeybinding(MODKEY_SHIFT, XK_q, KF_QUIT, NULL);
5825 setkeybinding(MODKEY, XK_q, KF_RESTART, NULL);
5826 setkeybinding(MODKEY, XK_m, KF_FOCUS_MAIN, NULL);
5827 setkeybinding(MODKEY, XK_1, KF_WS_1, NULL);
5828 setkeybinding(MODKEY, XK_2, KF_WS_2, NULL);
5829 setkeybinding(MODKEY, XK_3, KF_WS_3, NULL);
5830 setkeybinding(MODKEY, XK_4, KF_WS_4, NULL);
5831 setkeybinding(MODKEY, XK_5, KF_WS_5, NULL);
5832 setkeybinding(MODKEY, XK_6, KF_WS_6, NULL);
5833 setkeybinding(MODKEY, XK_7, KF_WS_7, NULL);
5834 setkeybinding(MODKEY, XK_8, KF_WS_8, NULL);
5835 setkeybinding(MODKEY, XK_9, KF_WS_9, NULL);
5836 setkeybinding(MODKEY, XK_0, KF_WS_10, NULL);
5837 setkeybinding(MODKEY, XK_F1, KF_WS_11, NULL);
5838 setkeybinding(MODKEY, XK_F2, KF_WS_12, NULL);
5839 setkeybinding(MODKEY, XK_F3, KF_WS_13, NULL);
5840 setkeybinding(MODKEY, XK_F4, KF_WS_14, NULL);
5841 setkeybinding(MODKEY, XK_F5, KF_WS_15, NULL);
5842 setkeybinding(MODKEY, XK_F6, KF_WS_16, NULL);
5843 setkeybinding(MODKEY, XK_F7, KF_WS_17, NULL);
5844 setkeybinding(MODKEY, XK_F8, KF_WS_18, NULL);
5845 setkeybinding(MODKEY, XK_F9, KF_WS_19, NULL);
5846 setkeybinding(MODKEY, XK_F10, KF_WS_20, NULL);
5847 setkeybinding(MODKEY, XK_F11, KF_WS_21, NULL);
5848 setkeybinding(MODKEY, XK_F12, KF_WS_22, NULL);
5849 setkeybinding(MODKEY, XK_Right, KF_WS_NEXT, NULL);
5850 setkeybinding(MODKEY, XK_Left, KF_WS_PREV, NULL);
5851 setkeybinding(MODKEY, XK_Up, KF_WS_NEXT_ALL, NULL);
5852 setkeybinding(MODKEY, XK_Down, KF_WS_PREV_ALL, NULL);
5853 setkeybinding(MODKEY, XK_a, KF_WS_PRIOR, NULL);
5854 setkeybinding(MODKEY_SHIFT, XK_Right, KF_SCREEN_NEXT, NULL);
5855 setkeybinding(MODKEY_SHIFT, XK_Left, KF_SCREEN_PREV, NULL);
5856 setkeybinding(MODKEY_SHIFT, XK_1, KF_MVWS_1, NULL);
5857 setkeybinding(MODKEY_SHIFT, XK_2, KF_MVWS_2, NULL);
5858 setkeybinding(MODKEY_SHIFT, XK_3, KF_MVWS_3, NULL);
5859 setkeybinding(MODKEY_SHIFT, XK_4, KF_MVWS_4, NULL);
5860 setkeybinding(MODKEY_SHIFT, XK_5, KF_MVWS_5, NULL);
5861 setkeybinding(MODKEY_SHIFT, XK_6, KF_MVWS_6, NULL);
5862 setkeybinding(MODKEY_SHIFT, XK_7, KF_MVWS_7, NULL);
5863 setkeybinding(MODKEY_SHIFT, XK_8, KF_MVWS_8, NULL);
5864 setkeybinding(MODKEY_SHIFT, XK_9, KF_MVWS_9, NULL);
5865 setkeybinding(MODKEY_SHIFT, XK_0, KF_MVWS_10, NULL);
5866 setkeybinding(MODKEY_SHIFT, XK_F1, KF_MVWS_11, NULL);
5867 setkeybinding(MODKEY_SHIFT, XK_F2, KF_MVWS_12, NULL);
5868 setkeybinding(MODKEY_SHIFT, XK_F3, KF_MVWS_13, NULL);
5869 setkeybinding(MODKEY_SHIFT, XK_F4, KF_MVWS_14, NULL);
5870 setkeybinding(MODKEY_SHIFT, XK_F5, KF_MVWS_15, NULL);
5871 setkeybinding(MODKEY_SHIFT, XK_F6, KF_MVWS_16, NULL);
5872 setkeybinding(MODKEY_SHIFT, XK_F7, KF_MVWS_17, NULL);
5873 setkeybinding(MODKEY_SHIFT, XK_F8, KF_MVWS_18, NULL);
5874 setkeybinding(MODKEY_SHIFT, XK_F9, KF_MVWS_19, NULL);
5875 setkeybinding(MODKEY_SHIFT, XK_F10, KF_MVWS_20, NULL);
5876 setkeybinding(MODKEY_SHIFT, XK_F11, KF_MVWS_21, NULL);
5877 setkeybinding(MODKEY_SHIFT, XK_F12, KF_MVWS_22, NULL);
5878 setkeybinding(MODKEY, XK_b, KF_BAR_TOGGLE, NULL);
5879 setkeybinding(MODKEY, XK_Tab, KF_FOCUS_NEXT, NULL);
5880 setkeybinding(MODKEY_SHIFT, XK_Tab, KF_FOCUS_PREV, NULL);
5881 setkeybinding(MODKEY_SHIFT, XK_x, KF_WIND_KILL, NULL);
5882 setkeybinding(MODKEY, XK_x, KF_WIND_DEL, NULL);
5883 setkeybinding(MODKEY, XK_s, KF_SPAWN_CUSTOM,"screenshot_all");
5884 setkeybinding(MODKEY_SHIFT, XK_s, KF_SPAWN_CUSTOM,"screenshot_wind");
5885 setkeybinding(MODKEY, XK_t, KF_FLOAT_TOGGLE,NULL);
5886 setkeybinding(MODKEY_SHIFT, XK_v, KF_VERSION, NULL);
5887 setkeybinding(MODKEY_SHIFT, XK_Delete, KF_SPAWN_CUSTOM,"lock");
5888 setkeybinding(MODKEY_SHIFT, XK_i, KF_SPAWN_CUSTOM,"initscr");
5889 setkeybinding(MODKEY, XK_w, KF_ICONIFY, NULL);
5890 setkeybinding(MODKEY_SHIFT, XK_w, KF_UNICONIFY, NULL);
5891 setkeybinding(MODKEY_SHIFT, XK_r, KF_RAISE_TOGGLE,NULL);
5892 setkeybinding(MODKEY, XK_v, KF_BUTTON2, NULL);
5893 setkeybinding(MODKEY, XK_equal, KF_WIDTH_GROW, NULL);
5894 setkeybinding(MODKEY, XK_minus, KF_WIDTH_SHRINK,NULL);
5895 setkeybinding(MODKEY_SHIFT, XK_equal, KF_HEIGHT_GROW,NULL);
5896 setkeybinding(MODKEY_SHIFT, XK_minus, KF_HEIGHT_SHRINK,NULL);
5897 setkeybinding(MODKEY, XK_bracketleft, KF_MOVE_LEFT,NULL);
5898 setkeybinding(MODKEY, XK_bracketright,KF_MOVE_RIGHT,NULL);
5899 setkeybinding(MODKEY_SHIFT, XK_bracketleft, KF_MOVE_UP, NULL);
5900 setkeybinding(MODKEY_SHIFT, XK_bracketright,KF_MOVE_DOWN,NULL);
5901 setkeybinding(MODKEY_SHIFT, XK_slash, KF_NAME_WORKSPACE,NULL);
5902 setkeybinding(MODKEY, XK_slash, KF_SEARCH_WORKSPACE,NULL);
5903 setkeybinding(MODKEY, XK_f, KF_SEARCH_WIN, NULL);
5905 setkeybinding(MODKEY_SHIFT, XK_d, KF_DUMPWINS, NULL);
5915 while (RB_EMPTY(&keys) == 0) {
5916 kp = RB_ROOT(&keys);
5922 setkeymapping(char *selector, char *value, int flags)
5924 char keymapping_file[PATH_MAX];
5926 /* suppress unused warnings since vars are needed */
5930 DNPRINTF(SWM_D_KEY, "setkeymapping: enter\n");
5931 if (value[0] == '~')
5932 snprintf(keymapping_file, sizeof keymapping_file, "%s/%s",
5933 pwd->pw_dir, &value[1]);
5935 strlcpy(keymapping_file, value, sizeof keymapping_file);
5937 /* load new key bindings; if it fails, revert to default bindings */
5938 if (conf_load(keymapping_file, SWM_CONF_KEYMAPPING)) {
5942 DNPRINTF(SWM_D_KEY, "setkeymapping: leave\n");
5947 updatenumlockmask(void)
5950 xcb_get_modifier_mapping_reply_t *modmap_r;
5951 xcb_keycode_t *modmap, kc, *keycode;
5955 modmap_r = xcb_get_modifier_mapping_reply(conn,
5956 xcb_get_modifier_mapping(conn),
5959 modmap = xcb_get_modifier_mapping_keycodes(modmap_r);
5960 for (i = 0; i < 8; i++) {
5961 for (j = 0; j < modmap_r->keycodes_per_modifier; j++) {
5962 kc = modmap[i * modmap_r->keycodes_per_modifier
5964 keycode = xcb_key_symbols_get_keycode(syms,
5967 numlockmask = (1 << i);
5973 DNPRINTF(SWM_D_MISC, "updatenumlockmask: %d\n", numlockmask);
5980 int num_screens, k, j;
5981 unsigned int modifiers[3];
5982 xcb_keycode_t *code;
5984 DNPRINTF(SWM_D_MISC, "grabkeys\n");
5985 updatenumlockmask();
5988 modifiers[1] = numlockmask;
5989 modifiers[2] = numlockmask | XCB_MOD_MASK_LOCK;
5991 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
5992 for (k = 0; k < num_screens; k++) {
5993 if (TAILQ_EMPTY(&screens[k].rl))
5995 xcb_ungrab_key(conn, XCB_GRAB_ANY, screens[k].root,
5997 RB_FOREACH(kp, key_tree, &keys) {
5998 if ((code = xcb_key_symbols_get_keycode(syms,
6000 for (j = 0; j < LENGTH(modifiers); j++)
6001 xcb_grab_key(conn, 1,
6003 kp->mod | modifiers[j],
6004 *code, XCB_GRAB_MODE_SYNC,
6005 XCB_GRAB_MODE_ASYNC);
6013 grabbuttons(struct ws_win *win)
6017 DNPRINTF(SWM_D_MOUSE, "grabbuttons: win 0x%x\n", win->id);
6019 for (i = 0; i < LENGTH(buttons); i++)
6020 if (buttons[i].action == client_click)
6021 xcb_grab_button(conn, 0, win->id, BUTTONMASK,
6022 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC,
6023 XCB_WINDOW_NONE, XCB_CURSOR_NONE,
6024 buttons[i].button, buttons[i].mask);
6027 const char *quirkname[] = {
6028 "NONE", /* config string for "no value" */
6037 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
6038 #define SWM_Q_WS "\n|+ \t"
6040 parsequirks(char *qstr, unsigned long *quirk)
6050 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
6052 cp += (long)strspn(cp, SWM_Q_WS);
6053 for (i = 0; i < LENGTH(quirkname); i++) {
6054 if (!strncasecmp(name, quirkname[i], SWM_QUIRK_LEN)) {
6055 DNPRINTF(SWM_D_QUIRK,
6056 "parsequirks: %s\n", name);
6061 *quirk |= 1 << (i-1);
6065 if (i >= LENGTH(quirkname)) {
6066 DNPRINTF(SWM_D_QUIRK,
6067 "parsequirks: invalid quirk [%s]\n", name);
6075 quirk_insert(const char *class, const char *name, unsigned long quirk)
6079 DNPRINTF(SWM_D_QUIRK, "quirk_insert: %s:%s [%lu]\n", class, name,
6082 if ((qp = malloc(sizeof *qp)) == NULL)
6083 err(1, "quirk_insert: malloc");
6084 if ((qp->class = strdup(class)) == NULL)
6085 err(1, "quirk_insert: strdup");
6086 if ((qp->name = strdup(name)) == NULL)
6087 err(1, "quirk_insert: strdup");
6090 TAILQ_INSERT_TAIL(&quirks, qp, entry);
6092 DNPRINTF(SWM_D_QUIRK, "quirk_insert: leave\n");
6096 quirk_remove(struct quirk *qp)
6098 DNPRINTF(SWM_D_QUIRK, "quirk_remove: %s:%s [%lu]\n", qp->class,
6099 qp->name, qp->quirk);
6101 TAILQ_REMOVE(&quirks, qp, entry);
6106 DNPRINTF(SWM_D_QUIRK, "quirk_remove: leave\n");
6110 quirk_replace(struct quirk *qp, const char *class, const char *name,
6111 unsigned long quirk)
6113 DNPRINTF(SWM_D_QUIRK, "quirk_replace: %s:%s [%lu]\n", qp->class,
6114 qp->name, qp->quirk);
6117 quirk_insert(class, name, quirk);
6119 DNPRINTF(SWM_D_QUIRK, "quirk_replace: leave\n");
6123 setquirk(const char *class, const char *name, unsigned long quirk)
6127 DNPRINTF(SWM_D_QUIRK, "setquirk: enter %s:%s [%lu]\n", class, name,
6130 TAILQ_FOREACH(qp, &quirks, entry) {
6131 if (!strcmp(qp->class, class) && !strcmp(qp->name, name)) {
6135 quirk_replace(qp, class, name, quirk);
6136 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
6141 warnx("error: setquirk: cannot find class/name combination");
6145 quirk_insert(class, name, quirk);
6146 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
6150 setconfquirk(char *selector, char *value, int flags)
6152 char *cp, *class, *name;
6156 /* suppress unused warning since var is needed */
6159 if (selector == NULL)
6161 if ((cp = strchr(selector, ':')) == NULL)
6166 if ((retval = parsequirks(value, &qrks)) == 0)
6167 setquirk(class, name, qrks);
6174 setquirk("MPlayer", "xv", SWM_Q_FLOAT | SWM_Q_FULLSCREEN | SWM_Q_FOCUSPREV);
6175 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT);
6176 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ);
6177 setquirk("Firefox", "Dialog", SWM_Q_FLOAT);
6178 setquirk("Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
6179 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ);
6180 setquirk("xine", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
6181 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
6182 setquirk("xine", "xine Panel", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
6183 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
6184 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
6185 setquirk("pcb", "pcb", SWM_Q_FLOAT);
6186 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
6189 /* conf file stuff */
6190 #define SWM_CONF_FILE "spectrwm.conf"
6191 #define SWM_CONF_FILE_OLD "scrotwm.conf"
6195 SWM_S_BAR_AT_BOTTOM,
6196 SWM_S_BAR_BORDER_WIDTH,
6203 SWM_S_CLOCK_ENABLED,
6206 SWM_S_CYCLE_VISIBLE,
6208 SWM_S_DISABLE_BORDER,
6210 SWM_S_FOCUS_CLOSE_WRAP,
6211 SWM_S_FOCUS_DEFAULT,
6217 SWM_S_STACK_ENABLED,
6219 SWM_S_TITLE_CLASS_ENABLED,
6220 SWM_S_TITLE_NAME_ENABLED,
6221 SWM_S_URGENT_ENABLED,
6222 SWM_S_VERBOSE_LAYOUT,
6223 SWM_S_WINDOW_NAME_ENABLED,
6224 SWM_S_WORKSPACE_LIMIT
6228 setconfvalue(char *selector, char *value, int flags)
6233 /* suppress unused warning since var is needed */
6237 case SWM_S_BAR_ACTION:
6239 if ((bar_argv[0] = expand_tilde(value)) == NULL)
6240 err(1, "setconfvalue: bar_action");
6242 case SWM_S_BAR_AT_BOTTOM:
6243 bar_at_bottom = atoi(value);
6245 case SWM_S_BAR_BORDER_WIDTH:
6246 bar_border_width = atoi(value);
6247 if (bar_border_width < 0)
6248 bar_border_width = 0;
6250 case SWM_S_BAR_DELAY:
6251 bar_delay = atoi(value);
6253 case SWM_S_BAR_ENABLED:
6254 bar_enabled = atoi(value);
6256 case SWM_S_BAR_FONT:
6258 if (asprintf(&bar_fonts, "%s,%s", value, bar_fonts) == -1)
6259 err(1, "setconfvalue: asprintf: failed to allocate "
6260 "memory for bar_fonts.");
6263 /* If already in xft mode, then we are done. */
6264 if (!bar_font_legacy)
6267 /* If there are any non-XLFD entries, switch to Xft mode. */
6268 while ((b = strsep(&value, ",")) != NULL) {
6272 bar_font_legacy = 0;
6277 case SWM_S_BAR_FORMAT:
6279 if ((bar_format = strdup(value)) == NULL)
6280 err(1, "setconfvalue: bar_format");
6282 case SWM_S_BAR_JUSTIFY:
6283 if (!strcmp(value, "left"))
6284 bar_justify = SWM_BAR_JUSTIFY_LEFT;
6285 else if (!strcmp(value, "center"))
6286 bar_justify = SWM_BAR_JUSTIFY_CENTER;
6287 else if (!strcmp(value, "right"))
6288 bar_justify = SWM_BAR_JUSTIFY_RIGHT;
6290 errx(1, "invalid bar_justify");
6292 case SWM_S_BORDER_WIDTH:
6293 border_width = atoi(value);
6294 if (border_width < 0)
6297 case SWM_S_CLOCK_ENABLED:
6298 clock_enabled = atoi(value);
6300 case SWM_S_CLOCK_FORMAT:
6301 #ifndef SWM_DENY_CLOCK_FORMAT
6303 if ((clock_format = strdup(value)) == NULL)
6304 err(1, "setconfvalue: clock_format");
6307 case SWM_S_CYCLE_EMPTY:
6308 cycle_empty = atoi(value);
6310 case SWM_S_CYCLE_VISIBLE:
6311 cycle_visible = atoi(value);
6313 case SWM_S_DIALOG_RATIO:
6314 dialog_ratio = atof(value);
6315 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
6318 case SWM_S_DISABLE_BORDER:
6319 disable_border = atoi(value);
6321 case SWM_S_FOCUS_CLOSE:
6322 if (!strcmp(value, "first"))
6323 focus_close = SWM_STACK_BOTTOM;
6324 else if (!strcmp(value, "last"))
6325 focus_close = SWM_STACK_TOP;
6326 else if (!strcmp(value, "next"))
6327 focus_close = SWM_STACK_ABOVE;
6328 else if (!strcmp(value, "previous"))
6329 focus_close = SWM_STACK_BELOW;
6331 errx(1, "focus_close");
6333 case SWM_S_FOCUS_CLOSE_WRAP:
6334 focus_close_wrap = atoi(value);
6336 case SWM_S_FOCUS_DEFAULT:
6337 if (!strcmp(value, "last"))
6338 focus_default = SWM_STACK_TOP;
6339 else if (!strcmp(value, "first"))
6340 focus_default = SWM_STACK_BOTTOM;
6342 errx(1, "focus_default");
6344 case SWM_S_FOCUS_MODE:
6345 if (!strcmp(value, "default"))
6346 focus_mode = SWM_FOCUS_DEFAULT;
6347 else if (!strcmp(value, "follow") ||
6348 !strcmp(value, "follow_cursor"))
6349 focus_mode = SWM_FOCUS_FOLLOW;
6350 else if (!strcmp(value, "manual"))
6351 focus_mode = SWM_FOCUS_MANUAL;
6353 errx(1, "focus_mode");
6355 case SWM_S_SPAWN_ORDER:
6356 if (!strcmp(value, "first"))
6357 spawn_position = SWM_STACK_BOTTOM;
6358 else if (!strcmp(value, "last"))
6359 spawn_position = SWM_STACK_TOP;
6360 else if (!strcmp(value, "next"))
6361 spawn_position = SWM_STACK_ABOVE;
6362 else if (!strcmp(value, "previous"))
6363 spawn_position = SWM_STACK_BELOW;
6365 errx(1, "spawn_position");
6367 case SWM_S_SPAWN_TERM:
6368 setconfspawn("term", value, 0);
6369 setconfspawn("spawn_term", value, 0);
6373 case SWM_S_SS_ENABLED:
6374 ss_enabled = atoi(value);
6376 case SWM_S_STACK_ENABLED:
6377 stack_enabled = atoi(value);
6379 case SWM_S_TERM_WIDTH:
6380 term_width = atoi(value);
6384 case SWM_S_TITLE_CLASS_ENABLED:
6385 title_class_enabled = atoi(value);
6387 case SWM_S_TITLE_NAME_ENABLED:
6388 title_name_enabled = atoi(value);
6390 case SWM_S_URGENT_ENABLED:
6391 urgent_enabled = atoi(value);
6393 case SWM_S_VERBOSE_LAYOUT:
6394 verbose_layout = atoi(value);
6395 for (i = 0; layouts[i].l_stack != NULL; i++) {
6397 layouts[i].l_string = fancy_stacker;
6399 layouts[i].l_string = plain_stacker;
6402 case SWM_S_WINDOW_NAME_ENABLED:
6403 window_name_enabled = atoi(value);
6405 case SWM_S_WORKSPACE_LIMIT:
6406 workspace_limit = atoi(value);
6407 if (workspace_limit > SWM_WS_MAX)
6408 workspace_limit = SWM_WS_MAX;
6409 else if (workspace_limit < 1)
6410 workspace_limit = 1;
6419 setconfmodkey(char *selector, char *value, int flags)
6421 /* suppress unused warnings since vars are needed */
6425 if (!strncasecmp(value, "Mod1", strlen("Mod1")))
6426 update_modkey(XCB_MOD_MASK_1);
6427 else if (!strncasecmp(value, "Mod2", strlen("Mod2")))
6428 update_modkey(XCB_MOD_MASK_2);
6429 else if (!strncasecmp(value, "Mod3", strlen("Mod3")))
6430 update_modkey(XCB_MOD_MASK_3);
6431 else if (!strncasecmp(value, "Mod4", strlen("Mod4")))
6432 update_modkey(XCB_MOD_MASK_4);
6439 setconfcolor(char *selector, char *value, int flags)
6441 setscreencolor(value, ((selector == NULL)?-1:atoi(selector)), flags);
6446 setconfregion(char *selector, char *value, int flags)
6448 /* suppress unused warnings since vars are needed */
6452 custom_region(value);
6457 setautorun(char *selector, char *value, int flags)
6467 /* suppress unused warnings since vars are needed */
6471 if (getenv("SWM_STARTED"))
6475 if (sscanf(value, "ws[%d]:%1023c", &ws_id, s) != 2)
6476 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
6478 if (ws_id < 0 || ws_id >= workspace_limit)
6479 errx(1, "autorun: invalid workspace %d", ws_id + 1);
6482 * This is a little intricate
6484 * If the pid already exists we simply reuse it because it means it was
6485 * used before AND not claimed by manage_window. We get away with
6486 * altering it in the parent after INSERT because this can not be a race
6489 while ((ap = strsep(&sp, " \t")) != NULL) {
6492 DNPRINTF(SWM_D_SPAWN, "setautorun: arg [%s]\n", ap);
6494 if ((a.argv = realloc(a.argv, argc * sizeof(char *))) == NULL)
6495 err(1, "setautorun: realloc");
6496 a.argv[argc - 1] = ap;
6499 if ((a.argv = realloc(a.argv, (argc + 1) * sizeof(char *))) == NULL)
6500 err(1, "setautorun: realloc");
6501 a.argv[argc] = NULL;
6503 if ((pid = fork()) == 0) {
6504 spawn(ws_id, &a, 1);
6513 p = calloc(1, sizeof *p);
6516 TAILQ_INSERT_TAIL(&pidlist, p, entry);
6526 setlayout(char *selector, char *value, int flags)
6528 int ws_id, i, x, mg, ma, si, ar, f = 0;
6529 int st = SWM_V_STACK, num_screens;
6531 struct workspace *ws;
6533 /* suppress unused warnings since vars are needed */
6537 if (getenv("SWM_STARTED"))
6541 if (sscanf(value, "ws[%d]:%d:%d:%d:%d:%1023c",
6542 &ws_id, &mg, &ma, &si, &ar, s) != 6)
6543 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
6544 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
6547 if (ws_id < 0 || ws_id >= workspace_limit)
6548 errx(1, "layout: invalid workspace %d", ws_id + 1);
6550 if (!strcasecmp(s, "vertical"))
6552 else if (!strcasecmp(s, "vertical_flip")) {
6555 } else if (!strcasecmp(s, "horizontal"))
6557 else if (!strcasecmp(s, "horizontal_flip")) {
6560 } else if (!strcasecmp(s, "fullscreen"))
6563 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
6564 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
6567 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
6568 for (i = 0; i < num_screens; i++) {
6569 ws = (struct workspace *)&screens[i].ws;
6570 ws[ws_id].cur_layout = &layouts[st];
6572 ws[ws_id].always_raise = ar;
6573 if (st == SWM_MAX_STACK)
6577 for (x = 0; x < abs(mg); x++) {
6578 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6579 mg >= 0 ? SWM_ARG_ID_MASTERGROW :
6580 SWM_ARG_ID_MASTERSHRINK);
6584 for (x = 0; x < abs(ma); x++) {
6585 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6586 ma >= 0 ? SWM_ARG_ID_MASTERADD :
6587 SWM_ARG_ID_MASTERDEL);
6591 for (x = 0; x < abs(si); x++) {
6592 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6593 si >= 0 ? SWM_ARG_ID_STACKINC :
6594 SWM_ARG_ID_STACKDEC);
6599 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6600 SWM_ARG_ID_FLIPLAYOUT);
6610 /* config options */
6611 struct config_option {
6613 int (*func)(char*, char*, int);
6616 struct config_option configopt[] = {
6617 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
6618 { "bar_at_bottom", setconfvalue, SWM_S_BAR_AT_BOTTOM },
6619 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
6620 { "bar_border_width", setconfvalue, SWM_S_BAR_BORDER_WIDTH },
6621 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
6622 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
6623 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
6624 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
6625 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
6626 { "bar_justify", setconfvalue, SWM_S_BAR_JUSTIFY },
6627 { "bar_format", setconfvalue, SWM_S_BAR_FORMAT },
6628 { "keyboard_mapping", setkeymapping, 0 },
6629 { "bind", setconfbinding, 0 },
6630 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
6631 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
6632 { "clock_format", setconfvalue, SWM_S_CLOCK_FORMAT },
6633 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
6634 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
6635 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
6636 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
6637 { "workspace_limit", setconfvalue, SWM_S_WORKSPACE_LIMIT },
6638 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
6639 { "verbose_layout", setconfvalue, SWM_S_VERBOSE_LAYOUT },
6640 { "modkey", setconfmodkey, 0 },
6641 { "program", setconfspawn, 0 },
6642 { "quirk", setconfquirk, 0 },
6643 { "region", setconfregion, 0 },
6644 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
6645 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
6646 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
6647 { "window_name_enabled", setconfvalue, SWM_S_WINDOW_NAME_ENABLED },
6648 { "urgent_enabled", setconfvalue, SWM_S_URGENT_ENABLED },
6649 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
6650 { "title_class_enabled", setconfvalue, SWM_S_TITLE_CLASS_ENABLED },
6651 { "title_name_enabled", setconfvalue, SWM_S_TITLE_NAME_ENABLED },
6652 { "focus_mode", setconfvalue, SWM_S_FOCUS_MODE },
6653 { "focus_close", setconfvalue, SWM_S_FOCUS_CLOSE },
6654 { "focus_close_wrap", setconfvalue, SWM_S_FOCUS_CLOSE_WRAP },
6655 { "focus_default", setconfvalue, SWM_S_FOCUS_DEFAULT },
6656 { "spawn_position", setconfvalue, SWM_S_SPAWN_ORDER },
6657 { "disable_border", setconfvalue, SWM_S_DISABLE_BORDER },
6658 { "border_width", setconfvalue, SWM_S_BORDER_WIDTH },
6659 { "autorun", setautorun, 0 },
6660 { "layout", setlayout, 0 },
6664 conf_load(char *filename, int keymapping)
6667 char *line, *cp, *optsub, *optval;
6668 size_t linelen, lineno = 0;
6669 int wordlen, i, optidx;
6670 struct config_option *opt = NULL;
6672 DNPRINTF(SWM_D_CONF, "conf_load: begin\n");
6674 if (filename == NULL) {
6675 warnx("conf_load: no filename");
6678 if ((config = fopen(filename, "r")) == NULL) {
6679 warn("conf_load: fopen: %s", filename);
6683 while (!feof(config)) {
6684 if ((line = fparseln(config, &linelen, &lineno, NULL, 0))
6687 err(1, "%s", filename);
6692 cp += strspn(cp, " \t\n"); /* eat whitespace */
6693 if (cp[0] == '\0') {
6698 /* get config option */
6699 wordlen = strcspn(cp, "=[ \t\n");
6701 warnx("%s: line %zd: no option found",
6706 for (i = 0; i < LENGTH(configopt); i++) {
6707 opt = &configopt[i];
6708 if (!strncasecmp(cp, opt->optname, wordlen) &&
6709 (int)strlen(opt->optname) == wordlen) {
6715 warnx("%s: line %zd: unknown option %.*s",
6716 filename, lineno, wordlen, cp);
6719 if (keymapping && opt && strcmp(opt->optname, "bind")) {
6720 warnx("%s: line %zd: invalid option %.*s",
6721 filename, lineno, wordlen, cp);
6725 cp += strspn(cp, " \t\n"); /* eat whitespace */
6726 /* get [selector] if any */
6730 wordlen = strcspn(cp, "]");
6733 warnx("%s: line %zd: syntax error",
6738 if (asprintf(&optsub, "%.*s", wordlen, cp) ==
6740 warnx("%s: line %zd: unable to allocate"
6741 "memory for selector", filename,
6747 cp += strspn(cp, "] \t\n"); /* eat trailing */
6749 cp += strspn(cp, "= \t\n"); /* eat trailing */
6751 optval = strdup(cp);
6752 /* call function to deal with it all */
6753 if (configopt[optidx].func(optsub, optval,
6754 configopt[optidx].funcflags) != 0)
6755 errx(1, "%s: line %zd: invalid data for %s",
6756 filename, lineno, configopt[optidx].optname);
6763 DNPRINTF(SWM_D_CONF, "conf_load: end\n");
6770 DNPRINTF(SWM_D_CONF, "conf_load: end with error.\n");
6776 set_child_transient(struct ws_win *win, xcb_window_t *trans)
6778 struct ws_win *parent, *w;
6779 struct swm_region *r;
6780 struct workspace *ws;
6781 xcb_icccm_wm_hints_t wmh;
6783 parent = find_window(win->transient);
6785 parent->focus_child = win;
6787 DNPRINTF(SWM_D_MISC, "set_child_transient: parent doesn't exist"
6788 " for 0x%x trans 0x%x\n", win->id, win->transient);
6790 r = root_to_region(win->wa->root, SWM_CK_ALL);
6792 /* parent doen't exist in our window list */
6793 TAILQ_FOREACH(w, &ws->winlist, entry) {
6794 if (xcb_icccm_get_wm_hints_reply(conn,
6795 xcb_icccm_get_wm_hints(conn, w->id),
6797 warnx("can't get hints for 0x%x", w->id);
6801 if (win->hints.window_group != wmh.window_group)
6804 w->focus_child = win;
6805 win->transient = w->id;
6807 DNPRINTF(SWM_D_MISC, "set_child_transient: adjusting "
6808 "transient to 0x%x\n", win->transient);
6815 window_get_pid(xcb_window_t win)
6820 xcb_get_property_cookie_t pc;
6821 xcb_get_property_reply_t *pr;
6823 apid = get_atom_from_string("_NET_WM_PID");
6824 if (apid == XCB_ATOM_NONE)
6827 pc = xcb_get_property(conn, 0, win, apid, XCB_ATOM_CARDINAL, 0, 1);
6828 pr = xcb_get_property_reply(conn, pc, NULL);
6831 if (pr->type != XCB_ATOM_CARDINAL) {
6836 if (pr->type == apid && pr->format == 32)
6837 ret = *((pid_t *)xcb_get_property_value(pr));
6843 apid = get_atom_from_string("_SWM_PID");
6844 pc = xcb_get_property(conn, 0, win, apid, XCB_ATOM_STRING,
6846 pr = xcb_get_property_reply(conn, pc, NULL);
6849 if (pr->type != apid) {
6854 ret = (pid_t)strtonum(xcb_get_property_value(pr), 0, INT_MAX, &errstr);
6861 get_ws_idx(xcb_window_t id)
6867 xcb_get_property_reply_t *gpr;
6869 gpr = xcb_get_property_reply(conn,
6870 xcb_get_property(conn, 0, id, a_swm_ws,
6871 XCB_ATOM_STRING, 0, SWM_PROPLEN),
6876 proplen = xcb_get_property_value_length(gpr);
6878 prop = malloc(proplen + 1);
6881 xcb_get_property_value(gpr),
6883 prop[proplen] = '\0';
6890 DNPRINTF(SWM_D_PROP, "get_ws_idx: _SWM_WS: %s\n", prop);
6891 ws_idx = (int)strtonum(prop, 0, workspace_limit - 1, &errstr);
6893 DNPRINTF(SWM_D_PROP, "get_ws_idx: window: #%s: %s",
6903 manage_window(xcb_window_t id, uint16_t mapped)
6905 xcb_window_t trans = XCB_WINDOW_NONE;
6906 struct ws_win *win, *ww;
6908 char ws_idx_str[SWM_PROPLEN];
6909 struct swm_region *r;
6913 xcb_icccm_get_wm_protocols_reply_t wpr;
6915 if ((win = find_window(id)) != NULL) {
6916 DNPRINTF(SWM_D_MISC, "manage_window: win 0x%x already "
6917 "managed; skipping.)\n", id);
6918 return (win); /* Already managed. */
6921 /* See if window is on the unmanaged list. */
6922 if ((win = find_unmanaged_window(id)) != NULL) {
6923 DNPRINTF(SWM_D_MISC, "manage_window: win 0x%x found on "
6924 "unmanaged list.\n", id);
6925 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
6928 set_child_transient(win, &trans);
6932 DNPRINTF(SWM_D_MISC, "manage_window: win 0x%x is new.\n", id);
6935 /* Create and initialize ws_win object. */
6936 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
6937 err(1, "manage_window: calloc: failed to allocate memory for "
6942 /* Get window geometry. */
6943 win->wa = xcb_get_geometry_reply(conn,
6944 xcb_get_geometry(conn, win->id),
6947 /* Figure out which region the window belongs to. */
6948 r = root_to_region(win->wa->root, SWM_CK_ALL);
6950 /* Ignore window border if there is one. */
6951 WIDTH(win) = win->wa->width;
6952 HEIGHT(win) = win->wa->height;
6953 X(win) = win->wa->x + win->wa->border_width - border_width;
6954 Y(win) = win->wa->y + win->wa->border_width - border_width;
6956 win->mapped = mapped;
6957 win->floatmaxed = 0;
6958 win->ewmh_flags = 0;
6959 win->s = r->s; /* this never changes */
6961 store_float_geom(win, r);
6963 /* Get WM_SIZE_HINTS. */
6964 xcb_icccm_get_wm_normal_hints_reply(conn,
6965 xcb_icccm_get_wm_normal_hints(conn, win->id),
6969 xcb_icccm_get_wm_hints_reply(conn,
6970 xcb_icccm_get_wm_hints(conn, win->id),
6973 /* Get WM_TRANSIENT_FOR; see if window is a transient. */
6974 xcb_icccm_get_wm_transient_for_reply(conn,
6975 xcb_icccm_get_wm_transient_for(conn, win->id),
6978 win->transient = trans;
6979 set_child_transient(win, &win->transient);
6982 /* Get supported protocols. */
6983 if (xcb_icccm_get_wm_protocols_reply(conn,
6984 xcb_icccm_get_wm_protocols(conn, win->id, a_prot),
6986 for (i = 0; i < wpr.atoms_len; i++) {
6987 if (wpr.atoms[i] == a_takefocus)
6988 win->take_focus = 1;
6989 if (wpr.atoms[i] == a_delete)
6990 win->can_delete = 1;
6992 xcb_icccm_get_wm_protocols_reply_wipe(&wpr);
6995 win->iconic = get_swm_iconic(win);
6997 /* Figure out which workspace the window belongs to. */
6998 if ((p = find_pid(window_get_pid(win->id))) != NULL) {
6999 win->ws = &r->s->ws[p->ws];
7000 TAILQ_REMOVE(&pidlist, p, entry);
7003 } else if ((ws_idx = get_ws_idx(win->id)) != -1 &&
7005 /* _SWM_WS is set; use that. */
7006 win->ws = &r->s->ws[ws_idx];
7007 } else if (trans && (ww = find_window(trans)) != NULL) {
7008 /* Launch transients in the same ws as parent. */
7014 /* Set the _SWM_WS atom so we can remember this after reincarnation. */
7015 if (snprintf(ws_idx_str, SWM_PROPLEN, "%d", win->ws->idx) <
7017 DNPRINTF(SWM_D_PROP, "manage_window: set _SWM_WS: %s\n",
7019 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
7020 a_swm_ws, XCB_ATOM_STRING, 8, strlen(ws_idx_str),
7025 ewmh_autoquirk(win);
7027 /* Determine initial quirks. */
7028 if (xcb_icccm_get_wm_class_reply(conn,
7029 xcb_icccm_get_wm_class(conn, win->id),
7031 DNPRINTF(SWM_D_CLASS, "manage_window: class: %s, name: %s\n",
7032 win->ch.class_name, win->ch.instance_name);
7034 /* java is retarded so treat it special */
7035 if (strstr(win->ch.instance_name, "sun-awt")) {
7036 DNPRINTF(SWM_D_CLASS, "manage_window: java window "
7041 TAILQ_FOREACH(qp, &quirks, entry) {
7042 if (!strcmp(win->ch.class_name, qp->class) &&
7043 !strcmp(win->ch.instance_name, qp->name)) {
7044 DNPRINTF(SWM_D_CLASS, "manage_window: on quirks"
7045 "list; mask: 0x%lx\n", qp->quirk);
7046 if (qp->quirk & SWM_Q_FLOAT)
7048 win->quirks = qp->quirk;
7053 /* Alter window position if quirky */
7054 if (win->quirks & SWM_Q_ANYWHERE)
7057 /* Reset font sizes (the bruteforce way; no default keybinding). */
7058 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
7059 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
7060 fake_keypress(win, XK_KP_Subtract, XCB_MOD_MASK_SHIFT);
7061 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
7062 fake_keypress(win, XK_KP_Add, XCB_MOD_MASK_SHIFT);
7065 /* Make sure window is positioned inside its region, if its active. */
7067 constrain_window(win, win->ws->r, 0);
7071 /* Select which X events to monitor and set border pixel color. */
7072 wa[0] = win->s->c[SWM_S_COLOR_UNFOCUS].pixel;
7073 wa[1] = XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_PROPERTY_CHANGE |
7074 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
7076 wa[1] |= XCB_EVENT_MASK_LEAVE_WINDOW | XCB_EVENT_MASK_FOCUS_CHANGE;
7079 xcb_change_window_attributes(conn, win->id, XCB_CW_BORDER_PIXEL |
7080 XCB_CW_EVENT_MASK, wa);
7083 /* Figure out where to stack the window in the workspace. */
7084 if (trans && (ww = find_window(trans)))
7085 TAILQ_INSERT_AFTER(&win->ws->winlist, ww, win, entry);
7086 else if (win->ws->focus && spawn_position == SWM_STACK_ABOVE)
7087 TAILQ_INSERT_AFTER(&win->ws->winlist, win->ws->focus, win,
7089 else if (win->ws->focus && spawn_position == SWM_STACK_BELOW)
7090 TAILQ_INSERT_BEFORE(win->ws->focus, win, entry);
7091 else switch (spawn_position) {
7094 case SWM_STACK_ABOVE:
7095 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
7097 case SWM_STACK_BOTTOM:
7098 case SWM_STACK_BELOW:
7099 TAILQ_INSERT_HEAD(&win->ws->winlist, win, entry);
7102 /* Get initial _NET_WM_STATE */
7103 ewmh_get_win_state(win);
7104 /* Set initial _NET_WM_ALLOWED_ACTIONS */
7105 ewmh_update_actions(win);
7109 DNPRINTF(SWM_D_MISC, "manage_window: done. window: 0x%x, (x,y) w x h: "
7110 "(%d,%d) %d x %d, ws: %d, iconic: %s, transient: 0x%x\n", win->id,
7111 X(win), Y(win), WIDTH(win), HEIGHT(win), win->ws->idx,
7112 YESNO(win->iconic), win->transient);
7118 free_window(struct ws_win *win)
7120 DNPRINTF(SWM_D_MISC, "free_window: window: 0x%x\n", win->id);
7125 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
7130 xcb_icccm_get_wm_class_reply_wipe(&win->ch);
7135 memset(win, 0xff, sizeof *win); /* XXX kill later */
7138 DNPRINTF(SWM_D_MISC, "free_window: done.\n");
7142 unmanage_window(struct ws_win *win)
7144 struct ws_win *parent;
7149 DNPRINTF(SWM_D_MISC, "unmanage_window: window: 0x%x\n", win->id);
7151 if (win->transient) {
7152 parent = find_window(win->transient);
7154 parent->focus_child = NULL;
7157 TAILQ_REMOVE(&win->ws->winlist, win, entry);
7158 TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
7162 expose(xcb_expose_event_t *e)
7165 struct swm_region *r;
7167 DNPRINTF(SWM_D_EVENT, "expose: window: 0x%x\n", e->window);
7169 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7170 for (i = 0; i < num_screens; i++)
7171 TAILQ_FOREACH(r, &screens[i].rl, entry)
7172 if (e->window == WINID(r->bar))
7180 focusin(xcb_focus_in_event_t *e)
7182 DNPRINTF(SWM_D_EVENT, "focusin: window: 0x%x, mode: %s(%u), "
7183 "detail: %s(%u)\n", e->event, get_notify_mode_label(e->mode),
7184 e->mode, get_notify_detail_label(e->detail), e->detail);
7188 focusout(xcb_focus_out_event_t *e)
7190 DNPRINTF(SWM_D_EVENT, "focusout: window: 0x%x, mode: %s(%u), "
7191 "detail: %s(%u)\n", e->event, get_notify_mode_label(e->mode),
7192 e->mode, get_notify_detail_label(e->detail), e->detail);
7197 keypress(xcb_key_press_event_t *e)
7199 xcb_keysym_t keysym;
7202 keysym = xcb_key_press_lookup_keysym(syms, e, 0);
7204 DNPRINTF(SWM_D_EVENT, "keypress: %u %u\n", e->detail, keysym);
7206 if ((kp = key_lookup(CLEANMASK(e->state), keysym)) == NULL)
7209 last_event_time = e->time;
7211 if (kp->funcid == KF_SPAWN_CUSTOM)
7212 spawn_custom(root_to_region(e->root, SWM_CK_ALL),
7213 &(keyfuncs[kp->funcid].args), kp->spawn_name);
7214 else if (keyfuncs[kp->funcid].func)
7215 keyfuncs[kp->funcid].func(root_to_region(e->root, SWM_CK_ALL),
7216 &(keyfuncs[kp->funcid].args));
7220 buttonpress(xcb_button_press_event_t *e)
7222 struct ws_win *win = NULL;
7223 struct swm_region *r, *old_r;
7227 DNPRINTF(SWM_D_EVENT, "buttonpress: win (x,y): 0x%x (%d,%d), "
7228 "detail: %u, time: %u, root (x,y): 0x%x (%d,%d), child: 0x%x, "
7229 "state: %u, same_screen: %s\n", e->event, e->event_x, e->event_y,
7230 e->detail, e->time, e->root, e->root_x, e->root_y, e->child,
7231 e->state, YESNO(e->same_screen));
7233 if (e->event == e->root) {
7234 if (e->child != 0) {
7235 win = find_window(e->child);
7236 /* Pass ButtonPress to window if it isn't managed. */
7240 /* Focus on empty region */
7241 /* If no windows on region if its empty. */
7242 r = root_to_region(e->root, SWM_CK_POINTER);
7243 if (r && TAILQ_EMPTY(&r->ws->winlist)) {
7244 old_r = root_to_region(e->root, SWM_CK_FOCUS);
7245 if (old_r && old_r != r)
7246 unfocus_win(old_r->ws->focus);
7248 xcb_set_input_focus(conn,
7249 XCB_INPUT_FOCUS_PARENT, e->root, e->time);
7251 /* Clear bar since empty. */
7259 win = find_window(e->event);
7265 last_event_time = e->time;
7267 focus_win(get_focus_magic(win));
7269 for (i = 0; i < LENGTH(buttons); i++)
7270 if (client_click == buttons[i].action && buttons[i].func &&
7271 buttons[i].button == e->detail &&
7272 CLEANMASK(buttons[i].mask) == CLEANMASK(e->state)) {
7273 buttons[i].func(win, &buttons[i].args);
7279 DNPRINTF(SWM_D_EVENT, "buttonpress: passing to window.\n");
7280 /* Replay event to event window */
7281 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, e->time);
7283 DNPRINTF(SWM_D_EVENT, "buttonpress: handled.\n");
7284 /* Unfreeze grab events. */
7285 xcb_allow_events(conn, XCB_ALLOW_SYNC_POINTER, e->time);
7293 print_win_geom(xcb_window_t w)
7295 xcb_get_geometry_reply_t *wa;
7297 wa = xcb_get_geometry_reply(conn, xcb_get_geometry(conn, w), NULL);
7300 DNPRINTF(SWM_D_MISC, "print_win_geom: window not found: 0x%x\n",
7305 DNPRINTF(SWM_D_MISC, "print_win_geom: window: 0x%x, root: 0x%x, "
7306 "depth: %u, (x,y) w x h: (%d,%d) %d x %d, border: %d\n",
7307 w, wa->root, wa->depth, wa->x, wa->y, wa->width, wa->height,
7316 get_stack_mode_name(uint8_t mode)
7321 case XCB_STACK_MODE_ABOVE:
7324 case XCB_STACK_MODE_BELOW:
7327 case XCB_STACK_MODE_TOP_IF:
7330 case XCB_STACK_MODE_BOTTOM_IF:
7333 case XCB_STACK_MODE_OPPOSITE:
7345 configurerequest(xcb_configure_request_event_t *e)
7348 struct swm_region *r = NULL;
7351 uint32_t wc[7] = {0};
7353 if ((win = find_window(e->window)) == NULL)
7354 if ((win = find_unmanaged_window(e->window)) == NULL)
7358 if (swm_debug & SWM_D_EVENT) {
7359 print_win_geom(e->window);
7361 DNPRINTF(SWM_D_EVENT, "configurerequest: window: 0x%x, "
7362 "parent: 0x%x, new: %s, value_mask: %u { ", e->window,
7363 e->parent, YESNO(new), e->value_mask);
7364 if (e->value_mask & XCB_CONFIG_WINDOW_X)
7365 DPRINTF("X: %d ", e->x);
7366 if (e->value_mask & XCB_CONFIG_WINDOW_Y)
7367 DPRINTF("Y: %d ", e->y);
7368 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH)
7369 DPRINTF("W: %u ", e->width);
7370 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
7371 DPRINTF("H: %u ", e->height);
7372 if (e->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH)
7373 DPRINTF("Border: %u ", e->border_width);
7374 if (e->value_mask & XCB_CONFIG_WINDOW_SIBLING)
7375 DPRINTF("Sibling: 0x%x ", e->sibling);
7376 if (e->value_mask & XCB_CONFIG_WINDOW_STACK_MODE)
7377 DPRINTF("StackMode: %s(%u) ",
7378 get_stack_mode_name(e->stack_mode), e->stack_mode);
7384 if (e->value_mask & XCB_CONFIG_WINDOW_X) {
7385 mask |= XCB_CONFIG_WINDOW_X;
7388 if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
7389 mask |= XCB_CONFIG_WINDOW_Y;
7392 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
7393 mask |= XCB_CONFIG_WINDOW_WIDTH;
7396 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
7397 mask |= XCB_CONFIG_WINDOW_HEIGHT;
7398 wc[i++] = e->height;
7400 if (e->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH) {
7401 mask |= XCB_CONFIG_WINDOW_BORDER_WIDTH;
7402 wc[i++] = e->border_width;
7404 if (e->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
7405 mask |= XCB_CONFIG_WINDOW_SIBLING;
7406 wc[i++] = e->sibling;
7408 if (e->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
7409 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
7410 wc[i++] = e->stack_mode;
7414 xcb_configure_window(conn, e->window, mask, wc);
7417 } else if ((!win->manual || win->quirks & SWM_Q_ANYWHERE) &&
7418 !(win->ewmh_flags & EWMH_F_FULLSCREEN)) {
7421 else if (win->ws->old_r)
7424 /* windows are centered unless ANYWHERE quirk is set. */
7425 if (win->quirks & SWM_Q_ANYWHERE) {
7426 if (e->value_mask & XCB_CONFIG_WINDOW_X) {
7427 win->g_float.x = e->x;
7429 win->g_float.x -= X(r);
7432 if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
7433 win->g_float.y = e->y;
7435 win->g_float.y -= Y(r);
7439 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH)
7440 win->g_float.w = e->width;
7442 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
7443 win->g_float.h = e->height;
7445 win->g_floatvalid = 1;
7447 if (win->floating && r && (win->transient ||
7448 win->ws->cur_layout != &layouts[SWM_MAX_STACK])) {
7449 WIDTH(win) = win->g_float.w;
7450 HEIGHT(win) = win->g_float.h;
7452 stack_floater(win, win->ws->r);
7464 DNPRINTF(SWM_D_EVENT, "configurerequest: done.\n");
7468 configurenotify(xcb_configure_notify_event_t *e)
7472 DNPRINTF(SWM_D_EVENT, "configurenotify: win 0x%x, event win: 0x%x, "
7473 "(x,y) WxH: (%d,%d) %ux%u, border: %u, above_sibling: 0x%x, "
7474 "override_redirect: %s\n", e->window, e->event, e->x, e->y,
7475 e->width, e->height, e->border_width, e->above_sibling,
7476 YESNO(e->override_redirect));
7478 win = find_window(e->window);
7480 xcb_icccm_get_wm_normal_hints_reply(conn,
7481 xcb_icccm_get_wm_normal_hints(conn, win->id),
7484 if (font_adjusted) {
7492 destroynotify(xcb_destroy_notify_event_t *e)
7496 DNPRINTF(SWM_D_EVENT, "destroynotify: window: 0x%x\n", e->window);
7498 if ((win = find_window(e->window)) == NULL) {
7499 if ((win = find_unmanaged_window(e->window)) == NULL)
7505 if (focus_mode != SWM_FOCUS_FOLLOW) {
7506 /* If we were focused, make sure we focus on something else. */
7507 if (win == win->ws->focus)
7508 win->ws->focus_pending = get_focus_prev(win);
7511 unmanage_window(win);
7514 if (focus_mode != SWM_FOCUS_FOLLOW) {
7515 if (win->ws->focus_pending) {
7516 focus_win(win->ws->focus_pending);
7517 win->ws->focus_pending = NULL;
7528 get_notify_detail_label(uint8_t detail)
7533 case XCB_NOTIFY_DETAIL_ANCESTOR:
7536 case XCB_NOTIFY_DETAIL_VIRTUAL:
7539 case XCB_NOTIFY_DETAIL_INFERIOR:
7542 case XCB_NOTIFY_DETAIL_NONLINEAR:
7543 label = "Nonlinear";
7545 case XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL:
7546 label = "NonlinearVirtual";
7548 case XCB_NOTIFY_DETAIL_POINTER:
7551 case XCB_NOTIFY_DETAIL_POINTER_ROOT:
7552 label = "PointerRoot";
7554 case XCB_NOTIFY_DETAIL_NONE:
7565 get_notify_mode_label(uint8_t mode)
7570 case XCB_NOTIFY_MODE_NORMAL:
7573 case XCB_NOTIFY_MODE_GRAB:
7576 case XCB_NOTIFY_MODE_UNGRAB:
7579 case XCB_NOTIFY_MODE_WHILE_GRABBED:
7580 label = "WhileGrabbed";
7591 enternotify(xcb_enter_notify_event_t *e)
7594 struct swm_region *old_r, *r;
7596 DNPRINTF(SWM_D_FOCUS, "enternotify: time: %u, win (x,y): 0x%x "
7597 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): 0x%x (%d,%d), "
7598 "child: 0x%x, same_screen_focus: %s, state: %d\n",
7599 e->time, e->event, e->event_x, e->event_y,
7600 get_notify_mode_label(e->mode), e->mode,
7601 get_notify_detail_label(e->detail), e->detail,
7602 e->root, e->root_x, e->root_y, e->child,
7603 YESNO(e->same_screen_focus), e->state);
7605 if (focus_mode == SWM_FOCUS_MANUAL &&
7606 e->mode == XCB_NOTIFY_MODE_NORMAL) {
7607 DNPRINTF(SWM_D_EVENT, "enternotify: manual focus; ignoring.\n");
7611 last_event_time = e->time;
7613 if ((win = find_window(e->event)) == NULL) {
7614 if (e->event == e->root) {
7615 /* If no windows on pointer region, then focus root. */
7616 r = root_to_region(e->root, SWM_CK_POINTER);
7617 if (TAILQ_EMPTY(&r->ws->winlist)) {
7618 old_r = root_to_region(e->root, SWM_CK_FOCUS);
7619 if (old_r && old_r != r)
7620 unfocus_win(old_r->ws->focus);
7622 xcb_set_input_focus(conn,
7623 XCB_INPUT_FOCUS_PARENT, e->root, e->time);
7625 /* Clear bar since empty. */
7631 DNPRINTF(SWM_D_EVENT, "enternotify: window is NULL; "
7637 focus_win(get_focus_magic(win));
7644 leavenotify(xcb_leave_notify_event_t *e)
7646 DNPRINTF(SWM_D_FOCUS, "leavenotify: time: %u, win (x,y): 0x%x "
7647 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): 0x%x (%d,%d), "
7648 "child: 0x%x, same_screen_focus: %s, state: %d\n",
7649 e->time, e->event, e->event_x, e->event_y,
7650 get_notify_mode_label(e->mode), e->mode,
7651 get_notify_detail_label(e->detail), e->detail,
7652 e->root, e->root_x, e->root_y, e->child,
7653 YESNO(e->same_screen_focus), e->state);
7657 /* lets us use one switch statement for arbitrary mode/detail combinations */
7658 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
7661 mapnotify(xcb_map_notify_event_t *e)
7665 DNPRINTF(SWM_D_EVENT, "mapnotify: window: 0x%x\n", e->window);
7667 if ((win = find_window(e->window)) == NULL)
7668 win = manage_window(e->window, 1);
7671 set_win_state(win, XCB_ICCCM_WM_STATE_NORMAL);
7673 if (focus_mode != SWM_FOCUS_FOLLOW) {
7674 if (win->ws->focus_pending == win) {
7676 win->ws->focus_pending = NULL;
7685 mappingnotify(xcb_mapping_notify_event_t *e)
7687 xcb_refresh_keyboard_mapping(syms, e);
7689 if (e->request == XCB_MAPPING_KEYBOARD)
7694 maprequest(xcb_map_request_event_t *e)
7697 xcb_get_window_attributes_reply_t *war;
7699 DNPRINTF(SWM_D_EVENT, "maprequest: win 0x%x\n",
7702 war = xcb_get_window_attributes_reply(conn,
7703 xcb_get_window_attributes(conn, e->window),
7706 DNPRINTF(SWM_D_EVENT, "maprequest: window lost.\n");
7710 if (war->override_redirect) {
7711 DNPRINTF(SWM_D_EVENT, "maprequest: override_redirect; "
7716 win = manage_window(e->window,
7717 (war->map_state == XCB_MAP_STATE_VIEWABLE));
7719 /* The new window should get focus; prepare. */
7720 if (focus_mode != SWM_FOCUS_FOLLOW)
7721 win->ws->focus_pending = get_focus_magic(win);
7723 /* All windows need to be mapped if they are in the current workspace.*/
7727 /* Ignore EnterNotify to handle the mapnotify without interference. */
7728 if (focus_mode == SWM_FOCUS_DEFAULT)
7729 event_drain(XCB_ENTER_NOTIFY);
7732 DNPRINTF(SWM_D_EVENT, "maprequest: done.\n");
7737 get_atom_name(xcb_atom_t atom)
7742 * This should be disabled during most debugging since
7743 * xcb_get_* causes an xcb_flush.
7746 xcb_get_atom_name_reply_t *r;
7748 r = xcb_get_atom_name_reply(conn,
7749 xcb_get_atom_name(conn, atom),
7752 len = xcb_get_atom_name_name_length(r);
7754 name = malloc(len + 1);
7756 memcpy(name, xcb_get_atom_name_name(r), len);
7770 propertynotify(xcb_property_notify_event_t *e)
7776 name = get_atom_name(e->atom);
7777 DNPRINTF(SWM_D_EVENT, "propertynotify: window: 0x%x, atom: %s(%u), "
7778 "time: %#x, state: %u\n", e->window, name, e->atom, e->time,
7782 win = find_window(e->window);
7786 last_event_time = e->time;
7788 if (e->atom == a_swm_iconic) {
7789 if (e->state == XCB_PROPERTY_NEW_VALUE) {
7790 if (focus_mode != SWM_FOCUS_FOLLOW)
7791 win->ws->focus_pending = get_focus_prev(win);
7798 if (focus_mode != SWM_FOCUS_FOLLOW) {
7799 focus_win(win->ws->focus_pending);
7800 win->ws->focus_pending = NULL;
7804 } else if (e->state == XCB_PROPERTY_DELETE) {
7805 /* The window is no longer iconic, restack ws. */
7806 if (focus_mode != SWM_FOCUS_FOLLOW)
7807 win->ws->focus_pending = get_focus_magic(win);
7811 /* Flush EnterNotify for mapnotify, if needed. */
7814 } else if (e->atom == a_state) {
7815 /* State just changed, make sure it gets focused if mapped. */
7816 if (e->state == XCB_PROPERTY_NEW_VALUE) {
7817 if (focus_mode != SWM_FOCUS_FOLLOW) {
7819 win->ws->focus_pending == win) {
7820 focus_win(win->ws->focus_pending);
7821 win->ws->focus_pending = NULL;
7825 } else if (e->atom == XCB_ATOM_WM_CLASS ||
7826 e->atom == XCB_ATOM_WM_NAME) {
7834 unmapnotify(xcb_unmap_notify_event_t *e)
7837 struct workspace *ws;
7839 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: 0x%x\n", e->window);
7841 /* If we aren't managing the window, then ignore. */
7842 win = find_window(e->window);
7848 if (getstate(e->window) != XCB_ICCCM_WM_STATE_ICONIC)
7849 set_win_state(win, XCB_ICCCM_WM_STATE_ICONIC);
7852 /* window unmapped itself */
7853 /* do unmap/unfocus/restack and unmanage */
7856 /* If win was focused, make sure to focus on something else. */
7857 if (win == ws->focus) {
7858 if (focus_mode != SWM_FOCUS_FOLLOW) {
7859 ws->focus_pending = get_focus_prev(win);
7860 DNPRINTF(SWM_D_EVENT, "unmapnotify: "
7861 "focus_pending: 0x%x\n",
7862 WINID(ws->focus_pending));
7868 unmanage_window(win);
7873 if (focus_mode == SWM_FOCUS_FOLLOW) {
7875 focus_win(get_pointer_win(ws->r->s->root));
7877 if (ws->focus_pending) {
7878 focus_win(ws->focus_pending);
7879 ws->focus_pending = NULL;
7884 if (getstate(e->window) == XCB_ICCCM_WM_STATE_NORMAL)
7885 set_win_state(win, XCB_ICCCM_WM_STATE_ICONIC);
7892 visibilitynotify(xcb_visibility_notify_event_t *e)
7894 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: 0x%x\n",
7900 clientmessage(xcb_client_message_event_t *e)
7903 xcb_map_request_event_t mre;
7907 name = get_atom_name(e->type);
7908 DNPRINTF(SWM_D_EVENT, "clientmessage: window: 0x%x, atom: %s(%u)\n",
7909 e->window, name, e->type);
7912 win = find_window(e->window);
7915 if (e->type == ewmh[_NET_ACTIVE_WINDOW].atom) {
7916 /* Manage the window with maprequest. */
7917 DNPRINTF(SWM_D_EVENT, "clientmessage: request focus on "
7918 "unmanaged window.\n");
7919 mre.window = e->window;
7925 if (e->type == ewmh[_NET_ACTIVE_WINDOW].atom) {
7926 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_ACTIVE_WINDOW\n");
7929 if (e->type == ewmh[_NET_CLOSE_WINDOW].atom) {
7930 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_CLOSE_WINDOW\n");
7931 if (win->can_delete)
7932 client_msg(win, a_delete, 0);
7934 xcb_kill_client(conn, win->id);
7936 if (e->type == ewmh[_NET_MOVERESIZE_WINDOW].atom) {
7937 DNPRINTF(SWM_D_EVENT,
7938 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
7939 if (win->floating) {
7940 if (e->data.data32[0] & (1<<8)) /* x */
7941 X(win) = e->data.data32[1];
7942 if (e->data.data32[0] & (1<<9)) /* y */
7943 Y(win) = e->data.data32[2];
7944 if (e->data.data32[0] & (1<<10)) /* width */
7945 WIDTH(win) = e->data.data32[3];
7946 if (e->data.data32[0] & (1<<11)) /* height */
7947 HEIGHT(win) = e->data.data32[4];
7952 /* TODO: Change stack sizes */
7953 /* notify no change was made. */
7954 config_win(win, NULL);
7957 if (e->type == ewmh[_NET_WM_STATE].atom) {
7958 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_WM_STATE\n");
7959 ewmh_update_win_state(win, e->data.data32[1], e->data.data32[0]);
7960 if (e->data.data32[2])
7961 ewmh_update_win_state(win, e->data.data32[2],
7973 int errcode = xcb_connection_has_error(conn);
7974 #ifdef XCB_CONN_ERROR
7977 case XCB_CONN_ERROR:
7978 s = "Socket error, pipe error or other stream error.";
7980 case XCB_CONN_CLOSED_EXT_NOTSUPPORTED:
7981 s = "Extension not supported.";
7983 case XCB_CONN_CLOSED_MEM_INSUFFICIENT:
7984 s = "Insufficient memory.";
7986 case XCB_CONN_CLOSED_REQ_LEN_EXCEED:
7987 s = "Request length was exceeded.";
7989 case XCB_CONN_CLOSED_PARSE_ERR:
7990 s = "Error parsing display string.";
7993 s = "Unknown error.";
7996 errx(errcode, "X CONNECTION ERROR: %s", s);
7999 errx(errcode, "X CONNECTION ERROR");
8007 const uint32_t val = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
8008 XCB_EVENT_MASK_ENTER_WINDOW;
8010 xcb_void_cookie_t wac;
8011 xcb_generic_error_t *error;
8013 /* this causes an error if some other window manager is running */
8014 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
8015 for (i = 0; i < num_screens; i++) {
8016 if ((sc = get_screen(i)) == NULL)
8017 errx(1, "ERROR: can't get screen %d.", i);
8018 DNPRINTF(SWM_D_INIT, "enable_wm: screen %d, root: 0x%x\n",
8020 wac = xcb_change_window_attributes_checked(conn, sc->root,
8021 XCB_CW_EVENT_MASK, &val);
8022 if ((error = xcb_request_check(conn, wac))) {
8023 DNPRINTF(SWM_D_INIT, "enable_wm: error_code: %u\n",
8029 /* click to focus on empty region */
8030 xcb_grab_button(conn, 1, sc->root, BUTTONMASK,
8031 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC, XCB_WINDOW_NONE,
8032 XCB_CURSOR_NONE, XCB_BUTTON_INDEX_1, XCB_BUTTON_MASK_ANY);
8039 new_region(struct swm_screen *s, int x, int y, int w, int h)
8041 struct swm_region *r, *n;
8042 struct workspace *ws = NULL;
8045 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
8046 s->idx, w, h, x, y);
8048 /* remove any conflicting regions */
8049 n = TAILQ_FIRST(&s->rl);
8052 n = TAILQ_NEXT(r, entry);
8053 if (X(r) < (x + w) &&
8054 (X(r) + WIDTH(r)) > x &&
8056 (Y(r) + HEIGHT(r)) > y) {
8057 if (r->ws->r != NULL)
8058 r->ws->old_r = r->ws->r;
8061 TAILQ_REMOVE(&s->rl, r, entry);
8062 TAILQ_INSERT_TAIL(&s->orl, r, entry);
8066 /* search old regions for one to reuse */
8068 /* size + location match */
8069 TAILQ_FOREACH(r, &s->orl, entry)
8070 if (X(r) == x && Y(r) == y &&
8071 HEIGHT(r) == h && WIDTH(r) == w)
8075 TAILQ_FOREACH(r, &s->orl, entry)
8076 if (HEIGHT(r) == h && WIDTH(r) == w)
8080 TAILQ_REMOVE(&s->orl, r, entry);
8081 /* try to use old region's workspace */
8082 if (r->ws->r == NULL)
8085 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
8086 err(1, "new_region: calloc: failed to allocate memory "
8089 /* if we don't have a workspace already, find one */
8091 for (i = 0; i < workspace_limit; i++)
8092 if (s->ws[i].r == NULL) {
8099 errx(1, "new_region: no free workspaces");
8110 TAILQ_INSERT_TAIL(&s->rl, r, entry);
8116 #ifdef SWM_XRR_HAS_CRTC
8119 #endif /* SWM_XRR_HAS_CRTC */
8120 struct swm_region *r;
8122 xcb_randr_get_screen_resources_current_cookie_t src;
8123 xcb_randr_get_screen_resources_current_reply_t *srr;
8124 xcb_randr_get_crtc_info_cookie_t cic;
8125 xcb_randr_get_crtc_info_reply_t *cir = NULL;
8126 xcb_randr_crtc_t *crtc;
8127 xcb_screen_t *screen;
8129 if ((screen = get_screen(i)) == NULL)
8130 errx(1, "ERROR: can't get screen %d.", i);
8132 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
8133 if (i >= num_screens)
8134 errx(1, "scan_xrandr: invalid screen");
8136 /* remove any old regions */
8137 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
8138 r->ws->old_r = r->ws->r = NULL;
8140 TAILQ_REMOVE(&screens[i].rl, r, entry);
8141 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
8145 /* map virtual screens onto physical screens */
8146 #ifdef SWM_XRR_HAS_CRTC
8147 if (xrandr_support) {
8148 src = xcb_randr_get_screen_resources_current(conn,
8150 srr = xcb_randr_get_screen_resources_current_reply(conn, src,
8153 new_region(&screens[i], 0, 0,
8154 screen->width_in_pixels,
8155 screen->height_in_pixels);
8158 ncrtc = srr->num_crtcs;
8160 crtc = xcb_randr_get_screen_resources_current_crtcs(srr);
8161 for (c = 0; c < ncrtc; c++) {
8162 cic = xcb_randr_get_crtc_info(conn, crtc[c],
8164 cir = xcb_randr_get_crtc_info_reply(conn, cic, NULL);
8167 if (cir->num_outputs == 0) {
8173 new_region(&screens[i], 0, 0,
8174 screen->width_in_pixels,
8175 screen->height_in_pixels);
8177 new_region(&screens[i],
8178 cir->x, cir->y, cir->width, cir->height);
8183 #endif /* SWM_XRR_HAS_CRTC */
8185 new_region(&screens[i], 0, 0, screen->width_in_pixels,
8186 screen->height_in_pixels);
8191 screenchange(xcb_randr_screen_change_notify_event_t *e)
8193 struct swm_region *r;
8196 DNPRINTF(SWM_D_EVENT, "screenchange: root: 0x%x\n", e->root);
8198 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
8199 /* silly event doesn't include the screen index */
8200 for (i = 0; i < num_screens; i++)
8201 if (screens[i].root == e->root)
8203 if (i >= num_screens)
8204 errx(1, "screenchange: screen not found");
8206 /* brute force for now, just re-enumerate the regions */
8210 print_win_geom(e->root);
8212 /* add bars to all regions */
8213 for (i = 0; i < num_screens; i++)
8214 TAILQ_FOREACH(r, &screens[i].rl, entry)
8222 xcb_window_t *wins = NULL, trans;
8224 int i, j, num_screens;
8225 uint16_t state, manage, mapped;
8227 xcb_query_tree_cookie_t qtc;
8228 xcb_query_tree_reply_t *qtr;
8229 xcb_get_window_attributes_cookie_t c;
8230 xcb_get_window_attributes_reply_t *r;
8231 xcb_get_property_cookie_t pc;
8233 DNPRINTF(SWM_D_INIT, "grab_windows: begin\n");
8234 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
8235 for (i = 0; i < num_screens; i++) {
8236 qtc = xcb_query_tree(conn, screens[i].root);
8237 qtr = xcb_query_tree_reply(conn, qtc, NULL);
8240 wins = xcb_query_tree_children(qtr);
8241 no = xcb_query_tree_children_length(qtr);
8242 /* attach windows to a region */
8243 /* normal windows */
8244 DNPRINTF(SWM_D_INIT, "grab_windows: grab top level windows.\n");
8245 for (j = 0; j < no; j++) {
8246 c = xcb_get_window_attributes(conn, wins[j]);
8247 r = xcb_get_window_attributes_reply(conn, c, NULL);
8249 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
8250 "doesn't exist.\n", wins[j]);
8253 if (r->override_redirect) {
8254 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
8255 "override_redirect set.\n", wins[j]);
8260 pc = xcb_icccm_get_wm_transient_for(conn, wins[j]);
8261 if (xcb_icccm_get_wm_transient_for_reply(conn, pc,
8263 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
8264 "is transient for %#x.\n", wins[j], trans);
8269 state = getstate(wins[j]);
8270 manage = state != XCB_ICCCM_WM_STATE_WITHDRAWN;
8271 mapped = r->map_state != XCB_MAP_STATE_UNMAPPED;
8272 if (mapped || manage)
8273 manage_window(wins[j], mapped);
8276 /* transient windows */
8277 DNPRINTF(SWM_D_INIT, "grab_windows: grab transient windows.\n");
8278 for (j = 0; j < no; j++) {
8279 c = xcb_get_window_attributes(conn, wins[j]);
8280 r = xcb_get_window_attributes_reply(conn, c, NULL);
8282 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
8283 "doesn't exist.\n", wins[j]);
8286 if (r->override_redirect) {
8287 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
8288 "override_redirect set.\n", wins[j]);
8294 state = getstate(wins[j]);
8295 manage = state != XCB_ICCCM_WM_STATE_WITHDRAWN;
8296 mapped = r->map_state != XCB_MAP_STATE_UNMAPPED;
8297 pc = xcb_icccm_get_wm_transient_for(conn, wins[j]);
8298 if (xcb_icccm_get_wm_transient_for_reply(conn, pc,
8299 &trans, NULL) && manage)
8300 manage_window(wins[j], mapped);
8304 DNPRINTF(SWM_D_INIT, "grab_windows: done.\n");
8310 int i, j, k, num_screens;
8311 struct workspace *ws;
8312 uint32_t gcv[1], wa[1];
8313 const xcb_query_extension_reply_t *qep;
8314 xcb_screen_t *screen;
8315 xcb_cursor_t cursor;
8316 xcb_font_t cursor_font;
8317 xcb_randr_query_version_cookie_t c;
8318 xcb_randr_query_version_reply_t *r;
8320 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
8321 if ((screens = calloc(num_screens,
8322 sizeof(struct swm_screen))) == NULL)
8323 err(1, "setup_screens: calloc: failed to allocate memory for "
8326 /* initial Xrandr setup */
8328 qep = xcb_get_extension_data(conn, &xcb_randr_id);
8330 c = xcb_randr_query_version(conn, 1, 1);
8331 r = xcb_randr_query_version_reply(conn, c, NULL);
8333 if (r->major_version >= 1) {
8335 xrandr_eventbase = qep->first_event;
8341 cursor_font = xcb_generate_id(conn);
8342 xcb_open_font(conn, cursor_font, strlen("cursor"), "cursor");
8344 cursor = xcb_generate_id(conn);
8345 xcb_create_glyph_cursor(conn, cursor, cursor_font, cursor_font,
8346 XC_left_ptr, XC_left_ptr + 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
8349 /* map physical screens */
8350 for (i = 0; i < num_screens; i++) {
8351 DNPRINTF(SWM_D_WS, "setup_screens: init screen: %d\n", i);
8353 TAILQ_INIT(&screens[i].rl);
8354 TAILQ_INIT(&screens[i].orl);
8355 if ((screen = get_screen(i)) == NULL)
8356 errx(1, "ERROR: can't get screen %d.", i);
8357 screens[i].root = screen->root;
8359 /* set default colors */
8360 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
8361 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
8362 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
8363 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
8364 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
8366 /* create graphics context on screen */
8367 screens[i].bar_gc = xcb_generate_id(conn);
8369 xcb_create_gc(conn, screens[i].bar_gc, screens[i].root,
8370 XCB_GC_GRAPHICS_EXPOSURES, gcv);
8372 /* set default cursor */
8373 xcb_change_window_attributes(conn, screens[i].root,
8376 /* init all workspaces */
8377 /* XXX these should be dynamically allocated too */
8378 for (j = 0; j < SWM_WS_MAX; j++) {
8379 ws = &screens[i].ws[j];
8383 ws->focus_prev = NULL;
8384 ws->focus_pending = NULL;
8387 TAILQ_INIT(&ws->winlist);
8388 TAILQ_INIT(&ws->unmanagedlist);
8390 for (k = 0; layouts[k].l_stack != NULL; k++)
8391 if (layouts[k].l_config != NULL)
8392 layouts[k].l_config(ws,
8393 SWM_ARG_ID_STACKINIT);
8394 ws->cur_layout = &layouts[0];
8395 ws->cur_layout->l_string(ws);
8401 xcb_randr_select_input(conn, screens[i].root,
8402 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE);
8404 xcb_free_cursor(conn, cursor);
8405 xcb_close_font(conn, cursor_font);
8411 if ((bar_fonts = strdup(SWM_BAR_FONTS)) == NULL)
8412 err(1, "setup_globals: strdup: failed to allocate memory.");
8414 if ((clock_format = strdup("%a %b %d %R %Z %Y")) == NULL)
8415 err(1, "setup_globals: strdup: failed to allocate memory.");
8417 if ((syms = xcb_key_symbols_alloc(conn)) == NULL)
8418 errx(1, "unable to allocate key symbols");
8420 a_state = get_atom_from_string("WM_STATE");
8421 a_prot = get_atom_from_string("WM_PROTOCOLS");
8422 a_delete = get_atom_from_string("WM_DELETE_WINDOW");
8423 a_takefocus = get_atom_from_string("WM_TAKE_FOCUS");
8424 a_wmname = get_atom_from_string("WM_NAME");
8425 a_netwmname = get_atom_from_string("_NET_WM_NAME");
8426 a_utf8_string = get_atom_from_string("UTF8_STRING");
8427 a_string = get_atom_from_string("STRING");
8428 a_swm_iconic = get_atom_from_string("_SWM_ICONIC");
8429 a_swm_ws = get_atom_from_string("_SWM_WS");
8436 xcb_atom_t netwmcheck;
8437 xcb_window_t root, win;
8439 /* work around sun jdk bugs, code from wmname */
8440 netwmcheck = get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
8442 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
8443 for (i = 0; i < num_screens; i++) {
8444 root = screens[i].root;
8446 win = xcb_generate_id(conn);
8447 xcb_create_window(conn, XCB_COPY_FROM_PARENT, win, root,
8448 0, 0, 1, 1, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT,
8449 XCB_COPY_FROM_PARENT, 0, NULL);
8451 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root,
8452 netwmcheck, XCB_ATOM_WINDOW, 32, 1, &win);
8453 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win,
8454 netwmcheck, XCB_ATOM_WINDOW, 32, 1, &win);
8455 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win,
8456 a_netwmname, a_utf8_string, 8, strlen("LG3D"), "LG3D");
8461 shutdown_cleanup(void)
8465 /* disable alarm because the following code may not be interrupted */
8467 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
8468 err(1, "can't disable alarm");
8476 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
8477 for (i = 0; i < num_screens; ++i) {
8478 if (screens[i].bar_gc != 0)
8479 xcb_free_gc(conn, screens[i].bar_gc);
8480 if (!bar_font_legacy)
8481 XftColorFree(display, DefaultVisual(display, i),
8482 DefaultColormap(display, i), &bar_font_color);
8485 if (bar_font_legacy)
8486 XFreeFontSet(display, bar_fs);
8488 XftFontClose(display, bar_font);
8491 xcb_key_symbols_free(syms);
8493 xcb_disconnect(conn);
8497 event_error(xcb_generic_error_t *e)
8501 DNPRINTF(SWM_D_EVENT, "event_error: %s(%u) from %s(%u), sequence: %u, "
8502 "resource_id: %u, minor_code: %u\n",
8503 xcb_event_get_error_label(e->error_code), e->error_code,
8504 xcb_event_get_request_label(e->major_code), e->major_code,
8505 e->sequence, e->resource_id, e->minor_code);
8509 event_handle(xcb_generic_event_t *evt)
8511 uint8_t type = XCB_EVENT_RESPONSE_TYPE(evt);
8513 DNPRINTF(SWM_D_EVENT, "XCB Event: %s(%d), seq %u\n",
8514 xcb_event_get_label(XCB_EVENT_RESPONSE_TYPE(evt)),
8515 XCB_EVENT_RESPONSE_TYPE(evt), evt->sequence);
8518 #define EVENT(type, callback) case type: callback((void *)evt); return
8519 EVENT(0, event_error);
8520 EVENT(XCB_BUTTON_PRESS, buttonpress);
8521 /*EVENT(XCB_BUTTON_RELEASE, buttonpress);*/
8522 /*EVENT(XCB_CIRCULATE_NOTIFY, );*/
8523 /*EVENT(XCB_CIRCULATE_REQUEST, );*/
8524 EVENT(XCB_CLIENT_MESSAGE, clientmessage);
8525 /*EVENT(XCB_COLORMAP_NOTIFY, );*/
8526 EVENT(XCB_CONFIGURE_NOTIFY, configurenotify);
8527 EVENT(XCB_CONFIGURE_REQUEST, configurerequest);
8528 /*EVENT(XCB_CREATE_NOTIFY, );*/
8529 EVENT(XCB_DESTROY_NOTIFY, destroynotify);
8530 EVENT(XCB_ENTER_NOTIFY, enternotify);
8531 EVENT(XCB_EXPOSE, expose);
8533 EVENT(XCB_FOCUS_IN, focusin);
8534 EVENT(XCB_FOCUS_OUT, focusout);
8536 /*EVENT(XCB_GRAPHICS_EXPOSURE, );*/
8537 /*EVENT(XCB_GRAVITY_NOTIFY, );*/
8538 EVENT(XCB_KEY_PRESS, keypress);
8539 /*EVENT(XCB_KEY_RELEASE, keypress);*/
8540 /*EVENT(XCB_KEYMAP_NOTIFY, );*/
8542 EVENT(XCB_LEAVE_NOTIFY, leavenotify);
8544 EVENT(XCB_MAP_NOTIFY, mapnotify);
8545 EVENT(XCB_MAP_REQUEST, maprequest);
8546 EVENT(XCB_MAPPING_NOTIFY, mappingnotify);
8547 /*EVENT(XCB_MOTION_NOTIFY, );*/
8548 /*EVENT(XCB_NO_EXPOSURE, );*/
8549 EVENT(XCB_PROPERTY_NOTIFY, propertynotify);
8550 /*EVENT(XCB_REPARENT_NOTIFY, );*/
8551 /*EVENT(XCB_RESIZE_REQUEST, );*/
8552 /*EVENT(XCB_SELECTION_CLEAR, );*/
8553 /*EVENT(XCB_SELECTION_NOTIFY, );*/
8554 /*EVENT(XCB_SELECTION_REQUEST, );*/
8555 EVENT(XCB_UNMAP_NOTIFY, unmapnotify);
8556 /*EVENT(XCB_VISIBILITY_NOTIFY, visibilitynotify);*/
8559 if (type - xrandr_eventbase == XCB_RANDR_SCREEN_CHANGE_NOTIFY)
8560 screenchange((void *)evt);
8564 main(int argc, char *argv[])
8566 struct swm_region *r, *rr;
8567 struct ws_win *winfocus = NULL;
8568 char conf[PATH_MAX], *cfile = NULL;
8570 int xfd, i, num_screens;
8571 struct sigaction sact;
8572 xcb_generic_event_t *evt;
8576 /* suppress unused warning since var is needed */
8579 time_started = time(NULL);
8582 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION, buildstr);
8583 if (!setlocale(LC_CTYPE, "") || !setlocale(LC_TIME, ""))
8584 warnx("no locale support");
8586 /* handle some signals */
8587 bzero(&sact, sizeof(sact));
8588 sigemptyset(&sact.sa_mask);
8590 sact.sa_handler = sighdlr;
8591 sigaction(SIGINT, &sact, NULL);
8592 sigaction(SIGQUIT, &sact, NULL);
8593 sigaction(SIGTERM, &sact, NULL);
8594 sigaction(SIGHUP, &sact, NULL);
8596 sact.sa_handler = sighdlr;
8597 sact.sa_flags = SA_NOCLDSTOP;
8598 sigaction(SIGCHLD, &sact, NULL);
8600 if (!(display = XOpenDisplay(0)))
8601 errx(1, "can not open display");
8603 conn = XGetXCBConnection(display);
8604 if (xcb_connection_has_error(conn))
8605 errx(1, "can not get XCB connection");
8607 XSetEventQueueOwner(display, XCBOwnsEventQueue);
8609 xcb_prefetch_extension_data(conn, &xcb_randr_id);
8610 xfd = xcb_get_file_descriptor(conn);
8612 /* look for local and global conf file */
8613 pwd = getpwuid(getuid());
8615 errx(1, "invalid user: %d", getuid());
8617 xcb_grab_server(conn);
8620 /* flush all events */
8621 while ((evt = xcb_poll_for_event(conn))) {
8622 if (XCB_EVENT_RESPONSE_TYPE(evt) == 0)
8627 if (enable_wm() != 0)
8628 errx(1, "another window manager is currently running");
8639 for (i = 0; ; i++) {
8644 snprintf(conf, sizeof conf, "%s/.%s",
8645 pwd->pw_dir, SWM_CONF_FILE);
8649 snprintf(conf, sizeof conf, "/etc/%s",
8654 snprintf(conf, sizeof conf, "%s/.%s",
8655 pwd->pw_dir, SWM_CONF_FILE_OLD);
8659 snprintf(conf, sizeof conf, "/etc/%s",
8666 if (strlen(conf) && stat(conf, &sb) != -1)
8667 if (S_ISREG(sb.st_mode)) {
8674 /* load conf (if any) */
8676 conf_load(cfile, SWM_CONF_DEFAULT);
8679 /* set some values to work around bad programs */
8681 /* grab existing windows (before we build the bars) */
8684 if (getenv("SWM_STARTED") == NULL)
8685 setenv("SWM_STARTED", "YES", 1);
8687 /* setup all bars */
8688 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
8689 for (i = 0; i < num_screens; i++)
8690 TAILQ_FOREACH(r, &screens[i].rl, entry) {
8691 if (winfocus == NULL)
8692 winfocus = TAILQ_FIRST(&r->ws->winlist);
8699 xcb_ungrab_server(conn);
8703 while ((evt = xcb_poll_for_event(conn))) {
8710 /* if we are being restarted go focus on first window */
8712 rr = winfocus->ws->r;
8714 /* not a visible window */
8719 focus_win(get_region_focus(rr));
8729 if (select(xfd + 1, &rd, NULL, NULL, &tv) == -1)
8730 if (errno != EINTR) {
8731 DNPRINTF(SWM_D_MISC, "select failed");
8734 restart(NULL, NULL);