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_CONF_DEFAULT (0)
253 #define SWM_CONF_KEYMAPPING (1)
256 #define SWM_LIB "/usr/local/lib/libswmhack.so"
263 xcb_atom_t a_takefocus;
265 xcb_atom_t a_netwmname;
266 xcb_atom_t a_utf8_string;
268 xcb_atom_t a_swm_iconic;
270 volatile sig_atomic_t running = 1;
271 volatile sig_atomic_t restart_wm = 0;
273 /*int last_focus_event = FocusOut;*/
277 int xrandr_eventbase;
278 unsigned int numlockmask = 0;
281 xcb_connection_t *conn;
282 xcb_key_symbols_t *syms;
283 xcb_timestamp_t last_event_time;
286 int cycle_visible = 0;
288 int font_adjusted = 0;
289 unsigned int mod_key = MODKEY;
292 struct swm_region *search_r;
293 int select_list_pipe[2];
294 int select_resp_pipe[2];
296 volatile sig_atomic_t search_resp;
297 int search_resp_action;
299 struct search_window {
300 TAILQ_ENTRY(search_window) entry;
304 xcb_window_t indicator;
306 TAILQ_HEAD(search_winlist, search_window);
307 struct search_winlist search_wl;
312 SWM_SEARCH_UNICONIFY,
313 SWM_SEARCH_NAME_WORKSPACE,
314 SWM_SEARCH_SEARCH_WORKSPACE,
315 SWM_SEARCH_SEARCH_WINDOW
318 #define SWM_STACK_TOP (0)
319 #define SWM_STACK_BOTTOM (1)
320 #define SWM_STACK_ABOVE (2)
321 #define SWM_STACK_BELOW (3)
324 double dialog_ratio = 0.6;
326 #define SWM_BAR_MAX (256)
327 #define SWM_BAR_JUSTIFY_LEFT (0)
328 #define SWM_BAR_JUSTIFY_CENTER (1)
329 #define SWM_BAR_JUSTIFY_RIGHT (2)
330 #define SWM_BAR_OFFSET (4)
331 #define SWM_BAR_FONTS "-*-terminus-medium-*-*-*-12-*-*-*-*-*-*-*," \
332 "-*-profont-*-*-*-*-12-*-*-*-*-*-*-*," \
333 "-*-times-medium-r-*-*-12-*-*-*-*-*-*-*," \
334 "-misc-fixed-medium-r-*-*-12-*-*-*-*-*-*-*," \
335 "-*-*-*-r-*-*-*-*-*-*-*-*-*-*"
337 #ifdef X_HAVE_UTF8_STRING
338 #define DRAWSTRING(x...) Xutf8DrawString(x)
340 #define DRAWSTRING(x...) XmbDrawString(x)
343 char *bar_argv[] = { NULL, NULL };
345 unsigned char bar_ext[SWM_BAR_MAX];
346 char bar_vertext[SWM_BAR_MAX];
348 sig_atomic_t bar_alarm = 0;
351 int bar_border_width = 1;
352 int bar_at_bottom = 0;
354 int bar_extra_running = 0;
357 int bar_justify = SWM_BAR_JUSTIFY_LEFT;
358 char *bar_format = NULL;
359 int stack_enabled = 1;
360 int clock_enabled = 1;
361 int urgent_enabled = 0;
362 char *clock_format = NULL;
363 int title_name_enabled = 0;
364 int title_class_enabled = 0;
365 int window_name_enabled = 0;
366 int focus_mode = SWM_FOCUS_DEFAULT;
367 int focus_close = SWM_STACK_BELOW;
368 int focus_close_wrap = 1;
369 int focus_default = SWM_STACK_TOP;
370 int spawn_position = SWM_STACK_TOP;
371 int disable_border = 0;
372 int border_width = 1;
373 int verbose_layout = 0;
377 XFontSetExtents *bar_fs_extents;
379 int bar_font_legacy = 1;
381 XftColor bar_font_color;
384 /* layout manager data */
385 struct swm_geometry {
398 struct swm_geometry g;
401 /* virtual "screens" */
403 TAILQ_ENTRY(swm_region) entry;
404 struct swm_geometry g;
405 struct workspace *ws; /* current workspace on this region */
406 struct workspace *ws_prior; /* prior workspace on this region */
407 struct swm_screen *s; /* screen idx */
410 TAILQ_HEAD(swm_region_list, swm_region);
413 TAILQ_ENTRY(ws_win) entry;
415 xcb_window_t transient;
416 struct ws_win *focus_child; /* focus on child transient */
417 struct swm_geometry g; /* current geometry */
418 struct swm_geometry g_float; /* region coordinates */
419 int g_floatvalid; /* g_float geometry validity */
420 int floatmaxed; /* whether maxed by max_stack */
426 unsigned int ewmh_flags;
427 int font_size_boundary[SWM_MAX_FONT_STEPS];
433 unsigned long quirks;
434 struct workspace *ws; /* always valid */
435 struct swm_screen *s; /* always valid, never changes */
436 xcb_get_geometry_reply_t *wa;
438 xcb_icccm_get_wm_class_reply_t ch;
439 xcb_icccm_wm_hints_t hints;
441 TAILQ_HEAD(ws_win_list, ws_win);
445 TAILQ_ENTRY(pid_e) entry;
449 TAILQ_HEAD(pid_list, pid_e);
450 struct pid_list pidlist = TAILQ_HEAD_INITIALIZER(pidlist);
452 /* layout handlers */
454 void vertical_config(struct workspace *, int);
455 void vertical_stack(struct workspace *, struct swm_geometry *);
456 void horizontal_config(struct workspace *, int);
457 void horizontal_stack(struct workspace *, struct swm_geometry *);
458 void max_stack(struct workspace *, struct swm_geometry *);
459 void plain_stacker(struct workspace *);
460 void fancy_stacker(struct workspace *);
463 void (*l_stack)(struct workspace *, struct swm_geometry *);
464 void (*l_config)(struct workspace *, int);
466 #define SWM_L_FOCUSPREV (1<<0)
467 #define SWM_L_MAPONFOCUS (1<<1)
468 void (*l_string)(struct workspace *);
470 /* stack, configure */
471 { vertical_stack, vertical_config, 0, plain_stacker },
472 { horizontal_stack, horizontal_config, 0, plain_stacker },
474 SWM_L_MAPONFOCUS | SWM_L_FOCUSPREV, plain_stacker },
475 { NULL, NULL, 0, NULL },
478 /* position of max_stack mode in the layouts array, index into layouts! */
479 #define SWM_V_STACK (0)
480 #define SWM_H_STACK (1)
481 #define SWM_MAX_STACK (2)
483 #define SWM_H_SLICE (32)
484 #define SWM_V_SLICE (32)
486 /* define work spaces */
488 int idx; /* workspace index */
489 char *name; /* workspace name */
490 int always_raise; /* raise windows on focus */
491 struct layout *cur_layout; /* current layout handlers */
492 struct ws_win *focus; /* may be NULL */
493 struct ws_win *focus_prev; /* may be NULL */
494 struct swm_region *r; /* may be NULL */
495 struct swm_region *old_r; /* may be NULL */
496 struct ws_win_list winlist; /* list of windows in ws */
497 struct ws_win_list unmanagedlist; /* list of dead windows in ws */
498 char stacker[10]; /* display stacker and layout */
502 int horizontal_msize;
504 int horizontal_stacks;
515 SWM_S_COLOR_BAR_BORDER,
516 SWM_S_COLOR_BAR_FONT,
522 /* physical screen mapping */
523 #define SWM_WS_MAX (22) /* hard limit */
524 int workspace_limit = 10; /* soft limit */
527 int idx; /* screen index */
528 struct swm_region_list rl; /* list of regions on this screen */
529 struct swm_region_list orl; /* list of old regions */
531 struct workspace ws[SWM_WS_MAX];
537 } c[SWM_S_COLOR_MAX];
539 xcb_gcontext_t bar_gc;
542 struct swm_screen *screens;
544 /* args to functions */
547 #define SWM_ARG_ID_FOCUSNEXT (0)
548 #define SWM_ARG_ID_FOCUSPREV (1)
549 #define SWM_ARG_ID_FOCUSMAIN (2)
550 #define SWM_ARG_ID_SWAPNEXT (10)
551 #define SWM_ARG_ID_SWAPPREV (11)
552 #define SWM_ARG_ID_SWAPMAIN (12)
553 #define SWM_ARG_ID_MOVELAST (13)
554 #define SWM_ARG_ID_MASTERSHRINK (20)
555 #define SWM_ARG_ID_MASTERGROW (21)
556 #define SWM_ARG_ID_MASTERADD (22)
557 #define SWM_ARG_ID_MASTERDEL (23)
558 #define SWM_ARG_ID_FLIPLAYOUT (24)
559 #define SWM_ARG_ID_STACKRESET (30)
560 #define SWM_ARG_ID_STACKINIT (31)
561 #define SWM_ARG_ID_CYCLEWS_UP (40)
562 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
563 #define SWM_ARG_ID_CYCLESC_UP (42)
564 #define SWM_ARG_ID_CYCLESC_DOWN (43)
565 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
566 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
567 #define SWM_ARG_ID_STACKINC (50)
568 #define SWM_ARG_ID_STACKDEC (51)
569 #define SWM_ARG_ID_SS_ALL (60)
570 #define SWM_ARG_ID_SS_WINDOW (61)
571 #define SWM_ARG_ID_DONTCENTER (70)
572 #define SWM_ARG_ID_CENTER (71)
573 #define SWM_ARG_ID_KILLWINDOW (80)
574 #define SWM_ARG_ID_DELETEWINDOW (81)
575 #define SWM_ARG_ID_WIDTHGROW (90)
576 #define SWM_ARG_ID_WIDTHSHRINK (91)
577 #define SWM_ARG_ID_HEIGHTGROW (92)
578 #define SWM_ARG_ID_HEIGHTSHRINK (93)
579 #define SWM_ARG_ID_MOVEUP (100)
580 #define SWM_ARG_ID_MOVEDOWN (101)
581 #define SWM_ARG_ID_MOVELEFT (102)
582 #define SWM_ARG_ID_MOVERIGHT (103)
588 TAILQ_ENTRY(quirk) entry;
592 #define SWM_Q_FLOAT (1<<0) /* float this window */
593 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
594 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
595 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
596 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
597 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
599 TAILQ_HEAD(quirk_list, quirk);
600 struct quirk_list quirks = TAILQ_HEAD_INITIALIZER(quirks);
603 * Supported EWMH hints should be added to
604 * both the enum and the ewmh array
609 _NET_MOVERESIZE_WINDOW,
610 _NET_WM_ACTION_ABOVE,
611 _NET_WM_ACTION_CLOSE,
612 _NET_WM_ACTION_FULLSCREEN,
614 _NET_WM_ACTION_RESIZE,
615 _NET_WM_ALLOWED_ACTIONS,
619 _NET_WM_STATE_FULLSCREEN,
620 _NET_WM_STATE_HIDDEN,
621 _NET_WM_STATE_MAXIMIZED_HORZ,
622 _NET_WM_STATE_MAXIMIZED_VERT,
623 _NET_WM_STATE_SKIP_PAGER,
624 _NET_WM_STATE_SKIP_TASKBAR,
626 _NET_WM_WINDOW_TYPE_DIALOG,
627 _NET_WM_WINDOW_TYPE_DOCK,
628 _NET_WM_WINDOW_TYPE_NORMAL,
629 _NET_WM_WINDOW_TYPE_SPLASH,
630 _NET_WM_WINDOW_TYPE_TOOLBAR,
631 _NET_WM_WINDOW_TYPE_UTILITY,
632 _SWM_WM_STATE_MANUAL,
639 } ewmh[SWM_EWMH_HINT_MAX] = {
640 /* must be in same order as in the enum */
641 {"_NET_ACTIVE_WINDOW", XCB_ATOM_NONE},
642 {"_NET_CLOSE_WINDOW", XCB_ATOM_NONE},
643 {"_NET_MOVERESIZE_WINDOW", XCB_ATOM_NONE},
644 {"_NET_WM_ACTION_ABOVE", XCB_ATOM_NONE},
645 {"_NET_WM_ACTION_CLOSE", XCB_ATOM_NONE},
646 {"_NET_WM_ACTION_FULLSCREEN", XCB_ATOM_NONE},
647 {"_NET_WM_ACTION_MOVE", XCB_ATOM_NONE},
648 {"_NET_WM_ACTION_RESIZE", XCB_ATOM_NONE},
649 {"_NET_WM_ALLOWED_ACTIONS", XCB_ATOM_NONE},
650 {"_NET_WM_NAME", XCB_ATOM_NONE},
651 {"_NET_WM_STATE", XCB_ATOM_NONE},
652 {"_NET_WM_STATE_ABOVE", XCB_ATOM_NONE},
653 {"_NET_WM_STATE_FULLSCREEN", XCB_ATOM_NONE},
654 {"_NET_WM_STATE_HIDDEN", XCB_ATOM_NONE},
655 {"_NET_WM_STATE_MAXIMIZED_HORZ", XCB_ATOM_NONE},
656 {"_NET_WM_STATE_MAXIMIZED_VERT", XCB_ATOM_NONE},
657 {"_NET_WM_STATE_SKIP_PAGER", XCB_ATOM_NONE},
658 {"_NET_WM_STATE_SKIP_TASKBAR", XCB_ATOM_NONE},
659 {"_NET_WM_WINDOW_TYPE", XCB_ATOM_NONE},
660 {"_NET_WM_WINDOW_TYPE_DIALOG", XCB_ATOM_NONE},
661 {"_NET_WM_WINDOW_TYPE_DOCK", XCB_ATOM_NONE},
662 {"_NET_WM_WINDOW_TYPE_NORMAL", XCB_ATOM_NONE},
663 {"_NET_WM_WINDOW_TYPE_SPLASH", XCB_ATOM_NONE},
664 {"_NET_WM_WINDOW_TYPE_TOOLBAR", XCB_ATOM_NONE},
665 {"_NET_WM_WINDOW_TYPE_UTILITY", XCB_ATOM_NONE},
666 {"_SWM_WM_STATE_MANUAL", XCB_ATOM_NONE},
669 /* function prototypes */
670 void buttonpress(xcb_button_press_event_t *);
671 void check_conn(void);
672 void clientmessage(xcb_client_message_event_t *);
673 int conf_load(char *, int);
674 void configurenotify(xcb_configure_notify_event_t *);
675 void configurerequest(xcb_configure_request_event_t *);
676 void constrain_window(struct ws_win *, struct swm_region *, int);
677 void destroynotify(xcb_destroy_notify_event_t *);
678 void enternotify(xcb_enter_notify_event_t *);
679 void event_drain(uint8_t);
680 void event_error(xcb_generic_error_t *);
681 void event_handle(xcb_generic_event_t *);
682 char *expand_tilde(char *);
683 void expose(xcb_expose_event_t *);
684 struct ws_win *find_window(xcb_window_t);
685 int floating_toggle_win(struct ws_win *);
686 void focus(struct swm_region *, union arg *);
687 void focus_flush(void);
688 struct ws_win *get_focus_magic(struct ws_win *);
689 struct ws_win *get_focus_prev(struct ws_win *);
690 void focus_win(struct ws_win *);
692 void focusin(xcb_focus_in_event_t *);
694 xcb_atom_t get_atom_from_string(const char *);
696 char *get_atom_name(xcb_atom_t);
697 char *get_notify_detail_label(uint8_t);
698 char *get_notify_mode_label(uint8_t);
700 struct ws_win *get_region_focus(struct swm_region *);
701 xcb_screen_t *get_screen(int);
702 char *get_win_name(xcb_window_t);
703 uint32_t getstate(xcb_window_t);
704 void grabbuttons(struct ws_win *);
705 void keypress(xcb_key_press_event_t *);
707 void leavenotify(xcb_leave_notify_event_t *);
709 void load_float_geom(struct ws_win *, struct swm_region *);
710 struct ws_win *manage_window(xcb_window_t, uint16_t);
711 void map_window(struct ws_win *);
712 void mapnotify(xcb_map_notify_event_t *);
713 void mappingnotify(xcb_mapping_notify_event_t *);
714 void maprequest(xcb_map_request_event_t *);
715 void new_region(struct swm_screen *, int, int, int, int);
716 int parse_rgb(const char *, uint16_t *, uint16_t *, uint16_t *);
717 void propertynotify(xcb_property_notify_event_t *);
718 void spawn_select(struct swm_region *, union arg *, char *, int *);
719 void screenchange(xcb_randr_screen_change_notify_event_t *);
720 void shutdown_cleanup(void);
721 void store_float_geom(struct ws_win *, struct swm_region *);
722 void unmanage_window(struct ws_win *);
723 void unmapnotify(xcb_unmap_notify_event_t *);
724 void unfocus_win(struct ws_win *);
725 void update_window(struct ws_win *);
726 /*void visibilitynotify(xcb_visibility_notify_event_t *);*/
729 expand_tilde(char *s)
738 errx(1, "expand_tilde: NULL string.");
747 if ((max = sysconf(_SC_LOGIN_NAME_MAX)) == -1)
748 errx(1, "expand_tilde: sysconf");
750 if ((user = calloc(1, max + 1)) == NULL)
751 errx(1, "expand_tilde: calloc");
753 for (i = 0; s[i] != '/' && s[i] != '\0'; ++i)
758 ppwd = strlen(user) == 0 ? getpwuid(getuid()) : getpwnam(user);
762 if (asprintf(&result, "%s%s", ppwd->pw_dir, s) == -1)
766 errx(1, "expand_tilde: failed to allocate memory.");
772 parse_rgb(const char *rgb, uint16_t *rr, uint16_t *gg, uint16_t *bb)
774 unsigned int tmpr, tmpg, tmpb;
776 if (sscanf(rgb, "rgb:%x/%x/%x", &tmpr, &tmpg, &tmpb) != 3)
779 *rr = RGB_8_TO_16(tmpr);
780 *gg = RGB_8_TO_16(tmpg);
781 *bb = RGB_8_TO_16(tmpb);
787 get_screen(int screen)
789 const xcb_setup_t *r;
790 xcb_screen_iterator_t iter;
792 if ((r = xcb_get_setup(conn)) == NULL) {
793 DNPRINTF(SWM_D_MISC, "get_screen: xcb_get_setup\n");
797 iter = xcb_setup_roots_iterator(r);
798 for (; iter.rem; --screen, xcb_screen_next(&iter))
808 if (focus_mode == SWM_FOCUS_DEFAULT)
809 event_drain(XCB_ENTER_NOTIFY);
815 get_atom_from_string(const char *str)
817 xcb_intern_atom_cookie_t c;
818 xcb_intern_atom_reply_t *r;
821 c = xcb_intern_atom(conn, 0, strlen(str), str);
822 r = xcb_intern_atom_reply(conn, c, NULL);
830 return (XCB_ATOM_NONE);
834 update_iconic(struct ws_win *win, int newv)
841 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
842 a_swm_iconic, XCB_ATOM_INTEGER, 32, 1, &v);
844 xcb_delete_property(conn, win->id, a_swm_iconic);
848 get_swm_iconic(struct ws_win *win)
851 xcb_get_property_reply_t *pr = NULL;
854 pr = xcb_get_property_reply(conn,
855 xcb_get_property(conn, 0, win->id, a_swm_iconic,
856 XCB_ATOM_INTEGER, 0, 1), NULL);
859 if (pr->type != XCB_ATOM_INTEGER || pr->format != 32)
861 v = *((int32_t *)xcb_get_property_value(pr));
872 int i, j, num_screens;
874 sup_list = get_atom_from_string("_NET_SUPPORTED");
876 for (i = 0; i < LENGTH(ewmh); i++)
877 ewmh[i].atom = get_atom_from_string(ewmh[i].name);
879 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
880 for (i = 0; i < num_screens; i++) {
881 /* Support check window will be created by workaround(). */
883 /* Report supported atoms */
884 xcb_delete_property(conn, screens[i].root, sup_list);
885 for (j = 0; j < LENGTH(ewmh); j++)
886 xcb_change_property(conn, XCB_PROP_MODE_APPEND,
887 screens[i].root, sup_list, XCB_ATOM_ATOM, 32, 1,
896 xcb_atom_t sup_check, sup_list;
898 xcb_get_property_cookie_t pc;
899 xcb_get_property_reply_t *pr;
901 sup_check = get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
902 sup_list = get_atom_from_string("_NET_SUPPORTED");
903 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
905 for (i = 0; i < num_screens; i++) {
906 /* Get the support check window and destroy it */
907 pc = xcb_get_property(conn, 0, screens[i].root, sup_check,
908 XCB_ATOM_WINDOW, 0, 1);
909 pr = xcb_get_property_reply(conn, pc, NULL);
912 if (pr->format == sup_check) {
913 id = *((xcb_window_t *)xcb_get_property_value(pr));
915 xcb_destroy_window(conn, id);
916 xcb_delete_property(conn, screens[i].root, sup_check);
917 xcb_delete_property(conn, screens[i].root, sup_list);
924 ewmh_autoquirk(struct ws_win *win)
928 xcb_get_property_cookie_t c;
929 xcb_get_property_reply_t *r;
931 c = xcb_get_property(conn, 0, win->id,
932 ewmh[_NET_WM_WINDOW_TYPE].atom, XCB_ATOM_ATOM, 0, UINT32_MAX);
933 r = xcb_get_property_reply(conn, c, NULL);
937 n = xcb_get_property_value_length(r);
938 type = xcb_get_property_value(r);
940 for (i = 0; i < n; i++) {
941 if (type[i] == ewmh[_NET_WM_WINDOW_TYPE_NORMAL].atom)
943 if (type[i] == ewmh[_NET_WM_WINDOW_TYPE_DOCK].atom ||
944 type[i] == ewmh[_NET_WM_WINDOW_TYPE_TOOLBAR].atom ||
945 type[i] == ewmh[_NET_WM_WINDOW_TYPE_UTILITY].atom) {
947 win->quirks = SWM_Q_FLOAT | SWM_Q_ANYWHERE;
950 if (type[i] == ewmh[_NET_WM_WINDOW_TYPE_SPLASH].atom ||
951 type[i] == ewmh[_NET_WM_WINDOW_TYPE_DIALOG].atom) {
953 win->quirks = SWM_Q_FLOAT;
960 #define SWM_EWMH_ACTION_COUNT_MAX (6)
961 #define EWMH_F_FULLSCREEN (1<<0)
962 #define EWMH_F_ABOVE (1<<1)
963 #define EWMH_F_HIDDEN (1<<2)
964 #define EWMH_F_SKIP_PAGER (1<<3)
965 #define EWMH_F_SKIP_TASKBAR (1<<4)
966 #define SWM_F_MANUAL (1<<5)
969 ewmh_set_win_fullscreen(struct ws_win *win, int fs)
977 DNPRINTF(SWM_D_MISC, "ewmh_set_win_fullscreen: window: 0x%x, "
978 "fullscreen %s\n", win->id, YESNO(fs));
981 if (!win->g_floatvalid)
982 store_float_geom(win, win->ws->r);
984 win->g = win->ws->r->g;
987 load_float_geom(win, win->ws->r);
994 ewmh_update_actions(struct ws_win *win)
996 xcb_atom_t actions[SWM_EWMH_ACTION_COUNT_MAX];
1002 actions[n++] = ewmh[_NET_WM_ACTION_CLOSE].atom;
1004 if (win->floating) {
1005 actions[n++] = ewmh[_NET_WM_ACTION_MOVE].atom;
1006 actions[n++] = ewmh[_NET_WM_ACTION_RESIZE].atom;
1007 actions[n++] = ewmh[_NET_WM_ACTION_ABOVE].atom;
1010 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
1011 ewmh[_NET_WM_ALLOWED_ACTIONS].atom, XCB_ATOM_ATOM, 32, 1, actions);
1014 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
1015 #define _NET_WM_STATE_ADD 1 /* add/set property */
1016 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
1019 ewmh_update_win_state(struct ws_win *win, xcb_atom_t state, long action)
1021 unsigned int mask = 0;
1022 unsigned int changed = 0;
1023 unsigned int orig_flags;
1028 DNPRINTF(SWM_D_PROP, "ewmh_update_win_state: window: 0x%x, state: %ld, "
1029 "action: %ld\n", win->id, (unsigned long)state, action);
1031 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
1032 mask = EWMH_F_FULLSCREEN;
1033 else if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
1034 mask = EWMH_F_ABOVE;
1035 else if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
1036 mask = SWM_F_MANUAL;
1037 else if (state == ewmh[_NET_WM_STATE_SKIP_PAGER].atom)
1038 mask = EWMH_F_SKIP_PAGER;
1039 else if (state == ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom)
1040 mask = EWMH_F_SKIP_TASKBAR;
1042 orig_flags = win->ewmh_flags;
1045 case _NET_WM_STATE_REMOVE:
1046 win->ewmh_flags &= ~mask;
1048 case _NET_WM_STATE_ADD:
1049 win->ewmh_flags |= mask;
1051 case _NET_WM_STATE_TOGGLE:
1052 win->ewmh_flags ^= mask;
1056 changed = (win->ewmh_flags & mask) ^ (orig_flags & mask) ? 1 : 0;
1058 if (state == ewmh[_NET_WM_STATE_ABOVE].atom) {
1059 if (changed && !floating_toggle_win(win))
1060 win->ewmh_flags = orig_flags; /* revert */
1061 } else if (state == ewmh[_SWM_WM_STATE_MANUAL].atom) {
1063 win->manual = (win->ewmh_flags & SWM_F_MANUAL) != 0;
1064 } else if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom) {
1065 if (changed && !ewmh_set_win_fullscreen(win,
1066 win->ewmh_flags & EWMH_F_FULLSCREEN))
1067 win->ewmh_flags = orig_flags; /* revert */
1070 xcb_delete_property(conn, win->id, ewmh[_NET_WM_STATE].atom);
1072 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
1073 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1074 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1075 &ewmh[_NET_WM_STATE_FULLSCREEN].atom);
1076 else if (win->ewmh_flags & EWMH_F_SKIP_PAGER)
1077 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1078 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1079 &ewmh[_NET_WM_STATE_SKIP_PAGER].atom);
1080 else if (win->ewmh_flags & EWMH_F_SKIP_TASKBAR)
1081 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1082 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1083 &ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom);
1084 else if (win->ewmh_flags & EWMH_F_ABOVE)
1085 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1086 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1087 &ewmh[_NET_WM_STATE_ABOVE].atom);
1088 else if (win->ewmh_flags & SWM_F_MANUAL)
1089 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1090 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1091 &ewmh[_SWM_WM_STATE_MANUAL].atom);
1095 ewmh_get_win_state(struct ws_win *win)
1098 xcb_get_property_cookie_t c;
1099 xcb_get_property_reply_t *r;
1105 win->ewmh_flags = 0;
1107 win->ewmh_flags |= EWMH_F_ABOVE;
1109 win->ewmh_flags |= SWM_F_MANUAL;
1111 c = xcb_get_property(conn, 0, win->id, ewmh[_NET_WM_STATE].atom,
1112 XCB_ATOM_ATOM, 0, UINT32_MAX);
1113 r = xcb_get_property_reply(conn, c, NULL);
1117 states = xcb_get_property_value(r);
1118 n = xcb_get_property_value_length(r);
1120 for (i = 0; i < n; i++)
1121 ewmh_update_win_state(win, states[i], _NET_WM_STATE_ADD);
1129 dumpwins(struct swm_region *r, union arg *args)
1133 xcb_get_window_attributes_cookie_t c;
1134 xcb_get_window_attributes_reply_t *wa;
1136 /* suppress unused warning since var is needed */
1139 if (r->ws == NULL) {
1140 warnx("dumpwins: invalid workspace");
1144 warnx("=== managed window list ws %02d ===", r->ws->idx);
1145 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
1146 state = getstate(win->id);
1147 c = xcb_get_window_attributes(conn, win->id);
1148 wa = xcb_get_window_attributes_reply(conn, c, NULL);
1150 warnx("window: 0x%x, map_state: %d, state: %u, "
1151 "transient: 0x%x", win->id, wa->map_state,
1152 state, win->transient);
1155 warnx("window: 0x%x, failed xcb_get_window_attributes",
1159 warnx("===== unmanaged window list =====");
1160 TAILQ_FOREACH(win, &r->ws->unmanagedlist, entry) {
1161 state = getstate(win->id);
1162 c = xcb_get_window_attributes(conn, win->id);
1163 wa = xcb_get_window_attributes_reply(conn, c, NULL);
1165 warnx("window: 0x%x, map_state: %d, state: %u, "
1166 "transient: 0x%x", win->id, wa->map_state,
1167 state, win->transient);
1170 warnx("window: 0x%x, failed xcb_get_window_attributes",
1174 warnx("=================================");
1178 dumpwins(struct swm_region *r, union arg *s)
1183 #endif /* SWM_DEBUG */
1188 int saved_errno, status;
1191 saved_errno = errno;
1195 while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) != 0) {
1200 if (errno != ECHILD)
1201 warn("sighdlr: waitpid");
1202 #endif /* SWM_DEBUG */
1205 if (pid == searchpid)
1209 if (WIFEXITED(status)) {
1210 if (WEXITSTATUS(status) != 0)
1211 warnx("sighdlr: child exit status: %d",
1212 WEXITSTATUS(status));
1214 warnx("sighdlr: child is terminated "
1216 #endif /* SWM_DEBUG */
1230 errno = saved_errno;
1236 struct pid_e *p = NULL;
1238 DNPRINTF(SWM_D_MISC, "find_pid: %d\n", pid);
1243 TAILQ_FOREACH(p, &pidlist, entry) {
1252 name_to_pixel(const char *colorname)
1254 uint32_t result = 0;
1255 char cname[32] = "#";
1256 xcb_screen_t *screen;
1257 xcb_colormap_t cmap;
1258 xcb_alloc_color_reply_t *cr;
1259 xcb_alloc_named_color_reply_t *nr;
1260 uint16_t rr, gg, bb;
1262 screen = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
1263 cmap = screen->default_colormap;
1265 /* color is in format rgb://rr/gg/bb */
1266 if (strncmp(colorname, "rgb:", 4) == 0) {
1267 if (parse_rgb(colorname, &rr, &gg, &bb) == -1)
1268 warnx("could not parse rgb %s", colorname);
1270 cr = xcb_alloc_color_reply(conn,
1271 xcb_alloc_color(conn, cmap, rr, gg, bb),
1277 warnx("color '%s' not found", colorname);
1280 nr = xcb_alloc_named_color_reply(conn,
1281 xcb_alloc_named_color(conn, cmap, strlen(colorname),
1284 strlcat(cname, colorname + 2, sizeof cname - 1);
1285 nr = xcb_alloc_named_color_reply(conn,
1286 xcb_alloc_named_color(conn, cmap, strlen(cname),
1293 warnx("color '%s' not found", colorname);
1300 setscreencolor(char *val, int i, int c)
1304 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1305 if (i > 0 && i <= num_screens) {
1306 screens[i - 1].c[c].pixel = name_to_pixel(val);
1307 free(screens[i - 1].c[c].name);
1308 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
1310 } else if (i == -1) {
1311 for (i = 0; i < num_screens; i++) {
1312 screens[i].c[c].pixel = name_to_pixel(val);
1313 free(screens[i].c[c].name);
1314 if ((screens[i].c[c].name = strdup(val)) == NULL)
1318 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1323 fancy_stacker(struct workspace *ws)
1325 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1326 if (ws->cur_layout->l_stack == vertical_stack)
1327 snprintf(ws->stacker, sizeof ws->stacker,
1328 ws->l_state.vertical_flip ? "[%d>%d]" : "[%d|%d]",
1329 ws->l_state.vertical_mwin, ws->l_state.vertical_stacks);
1330 if (ws->cur_layout->l_stack == horizontal_stack)
1331 snprintf(ws->stacker, sizeof ws->stacker,
1332 ws->l_state.horizontal_flip ? "[%dv%d]" : "[%d-%d]",
1333 ws->l_state.horizontal_mwin, ws->l_state.horizontal_stacks);
1337 plain_stacker(struct workspace *ws)
1339 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1340 if (ws->cur_layout->l_stack == vertical_stack)
1341 strlcpy(ws->stacker, ws->l_state.vertical_flip ? "[>]" : "[|]",
1342 sizeof ws->stacker);
1343 if (ws->cur_layout->l_stack == horizontal_stack)
1344 strlcpy(ws->stacker, ws->l_state.horizontal_flip ? "[v]" : "[-]",
1345 sizeof ws->stacker);
1349 custom_region(char *val)
1351 unsigned int x, y, w, h;
1352 int sidx, num_screens;
1353 xcb_screen_t *screen;
1355 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1356 if (sscanf(val, "screen[%d]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
1357 errx(1, "invalid custom region, "
1358 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1359 if (sidx < 1 || sidx > num_screens)
1360 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1364 if ((screen = get_screen(sidx)) == NULL)
1365 errx(1, "ERROR: can't get screen %d.", sidx);
1368 errx(1, "region %ux%u+%u+%u too small", w, h, x, y);
1370 if (x > screen->width_in_pixels ||
1371 y > screen->height_in_pixels ||
1372 w + x > screen->width_in_pixels ||
1373 h + y > screen->height_in_pixels) {
1374 warnx("ignoring region %ux%u+%u+%u - not within screen "
1375 "boundaries (%ux%u)", w, h, x, y,
1376 screen->width_in_pixels, screen->height_in_pixels);
1380 new_region(&screens[sidx], x, y, w, h);
1384 socket_setnonblock(int fd)
1388 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
1389 err(1, "fcntl F_GETFL");
1390 flags |= O_NONBLOCK;
1391 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
1392 err(1, "fcntl F_SETFL");
1396 bar_print_legacy(struct swm_region *r, const char *s)
1398 xcb_rectangle_t rect;
1403 XRectangle ibox, lbox;
1407 XmbTextExtents(bar_fs, s, len, &ibox, &lbox);
1409 switch (bar_justify) {
1410 case SWM_BAR_JUSTIFY_LEFT:
1413 case SWM_BAR_JUSTIFY_CENTER:
1414 x = (WIDTH(r) - lbox.width) / 2;
1416 case SWM_BAR_JUSTIFY_RIGHT:
1417 x = WIDTH(r) - lbox.width - SWM_BAR_OFFSET;
1421 if (x < SWM_BAR_OFFSET)
1426 rect.width = WIDTH(r->bar);
1427 rect.height = HEIGHT(r->bar);
1429 /* clear back buffer */
1430 gcv[0] = r->s->c[SWM_S_COLOR_BAR].pixel;
1431 xcb_change_gc(conn, r->s->bar_gc, XCB_GC_FOREGROUND, gcv);
1432 xcb_poly_fill_rectangle(conn, r->bar->buffer, r->s->bar_gc, 1, &rect);
1434 /* draw back buffer */
1435 gcvd.graphics_exposures = 0;
1436 draw = XCreateGC(display, r->bar->buffer, GCGraphicsExposures, &gcvd);
1437 XSetForeground(display, draw, r->s->c[SWM_S_COLOR_BAR_FONT].pixel);
1438 DRAWSTRING(display, r->bar->buffer, bar_fs, draw,
1439 x, (bar_fs_extents->max_logical_extent.height - lbox.height) / 2 -
1441 XFreeGC(display, draw);
1444 xcb_copy_area(conn, r->bar->buffer, r->bar->id, r->s->bar_gc, 0, 0,
1445 0, 0, WIDTH(r->bar), HEIGHT(r->bar));
1449 bar_print(struct swm_region *r, const char *s)
1452 xcb_rectangle_t rect;
1460 XftTextExtentsUtf8(display, bar_font, (FcChar8 *)s, len, &info);
1462 switch (bar_justify) {
1463 case SWM_BAR_JUSTIFY_LEFT:
1466 case SWM_BAR_JUSTIFY_CENTER:
1467 x = (WIDTH(r) - info.width) / 2;
1469 case SWM_BAR_JUSTIFY_RIGHT:
1470 x = WIDTH(r) - info.width - SWM_BAR_OFFSET;
1474 if (x < SWM_BAR_OFFSET)
1479 rect.width = WIDTH(r->bar);
1480 rect.height = HEIGHT(r->bar);
1482 /* clear back buffer */
1483 gcv[0] = r->s->c[SWM_S_COLOR_BAR].pixel;
1484 xcb_change_gc(conn, r->s->bar_gc, XCB_GC_FOREGROUND, gcv);
1485 xcb_poly_fill_rectangle(conn, r->bar->buffer, r->s->bar_gc, 1, &rect);
1487 /* draw back buffer */
1488 draw = XftDrawCreate(display, r->bar->buffer,
1489 DefaultVisual(display, r->s->idx),
1490 DefaultColormap(display, r->s->idx));
1492 XftDrawStringUtf8(draw, &bar_font_color, bar_font, x,
1493 (HEIGHT(r->bar) + bar_font->height) / 2 - bar_font->descent,
1496 XftDrawDestroy(draw);
1499 xcb_copy_area(conn, r->bar->buffer, r->bar->id, r->s->bar_gc, 0, 0,
1500 0, 0, WIDTH(r->bar), HEIGHT(r->bar));
1504 bar_extra_stop(void)
1508 bzero(bar_pipe, sizeof bar_pipe);
1511 kill(bar_pid, SIGTERM);
1514 strlcpy((char *)bar_ext, "", sizeof bar_ext);
1519 bar_class_name(char *s, size_t sz, struct swm_region *r)
1521 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1523 if (r->ws->focus->ch.class_name != NULL)
1524 strlcat(s, r->ws->focus->ch.class_name, sz);
1528 bar_title_name(char *s, size_t sz, struct swm_region *r)
1530 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1532 if (r->ws->focus->ch.instance_name != NULL)
1533 strlcat(s, r->ws->focus->ch.instance_name, sz);
1537 bar_class_title_name(char *s, size_t sz, struct swm_region *r)
1539 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1542 bar_class_name(s, sz, r);
1543 strlcat(s, ":", sz);
1544 bar_title_name(s, sz, r);
1548 bar_window_float(char *s, size_t sz, struct swm_region *r)
1550 if (r == NULL || r ->ws == NULL || r->ws->focus == NULL)
1552 if (r->ws->focus->floating)
1553 strlcat(s, "(f)", sz);
1557 bar_window_name(char *s, size_t sz, struct swm_region *r)
1561 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1563 if ((title = get_win_name(r->ws->focus->id)) == NULL)
1566 strlcat(s, title, sz);
1570 int urgent[SWM_WS_MAX];
1572 bar_urgent(char *s, size_t sz)
1575 int i, j, num_screens;
1577 xcb_get_property_cookie_t c;
1578 xcb_icccm_wm_hints_t hints;
1580 for (i = 0; i < workspace_limit; i++)
1583 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1584 for (i = 0; i < num_screens; i++)
1585 for (j = 0; j < workspace_limit; j++)
1586 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry) {
1587 c = xcb_icccm_get_wm_hints(conn, win->id);
1588 if (xcb_icccm_get_wm_hints_reply(conn, c,
1591 if (hints.flags & XCB_ICCCM_WM_HINT_X_URGENCY)
1595 for (i = 0; i < workspace_limit; i++) {
1597 snprintf(b, sizeof b, "%d ", i + 1);
1599 snprintf(b, sizeof b, "- ");
1605 bar_workspace_name(char *s, size_t sz, struct swm_region *r)
1607 if (r == NULL || r->ws == NULL)
1609 if (r->ws->name != NULL)
1610 strlcat(s, r->ws->name, sz);
1613 /* build the default bar format according to the defined enabled options */
1615 bar_fmt(const char *fmtexp, char *fmtnew, struct swm_region *r, size_t sz)
1617 /* if format provided, just copy the buffers */
1618 if (bar_format != NULL) {
1619 strlcpy(fmtnew, fmtexp, sz);
1623 /* reset the output buffer */
1626 strlcat(fmtnew, "+N:+I ", sz);
1628 strlcat(fmtnew, "+S", sz);
1629 strlcat(fmtnew, " ", sz);
1631 /* only show the workspace name if there's actually one */
1632 if (r != NULL && r->ws != NULL && r->ws->name != NULL)
1633 strlcat(fmtnew, "<+D>", sz);
1634 strlcat(fmtnew, "+3<", sz);
1636 if (clock_enabled) {
1637 strlcat(fmtnew, fmtexp, sz);
1638 strlcat(fmtnew, "+4<", sz);
1641 /* bar_urgent already adds the space before the last asterisk */
1643 strlcat(fmtnew, "* +U*+4<", sz);
1645 if (title_class_enabled) {
1646 strlcat(fmtnew, "+C", sz);
1647 if (title_name_enabled == 0)
1648 strlcat(fmtnew, "+4<", sz);
1651 /* checks needed by the colon and floating strlcat(3) calls below */
1652 if (r != NULL && r->ws != NULL && r->ws->focus != NULL) {
1653 if (title_name_enabled) {
1654 if (title_class_enabled)
1655 strlcat(fmtnew, ":", sz);
1656 strlcat(fmtnew, "+T+4<", sz);
1658 if (window_name_enabled) {
1659 if (r->ws->focus->floating)
1660 strlcat(fmtnew, "+F ", sz);
1661 strlcat(fmtnew, "+64W ", sz);
1665 /* finally add the action script output and the version */
1666 strlcat(fmtnew, "+4<+A+4<+V", sz);
1670 bar_replace_pad(char *tmp, int *limit, size_t sz)
1672 /* special case; no limit given, pad one space, instead */
1673 if (*limit == (int)sz - 1)
1675 snprintf(tmp, sz, "%*s", *limit, " ");
1678 /* replaces the bar format character sequences (like in tmux(1)) */
1680 bar_replace_seq(char *fmt, char *fmtrep, struct swm_region *r, size_t *offrep,
1684 char tmp[SWM_BAR_MAX];
1688 /* reset strlcat(3) buffer */
1691 /* get number, if any */
1694 if (sscanf(fmt, "%d%n", &limit, &size) != 1)
1695 limit = sizeof tmp - 1;
1696 if (limit <= 0 || limit >= (int)sizeof tmp)
1697 limit = sizeof tmp - 1;
1699 /* there is nothing to replace (ie EOL) */
1706 bar_replace_pad(tmp, &limit, sizeof tmp);
1709 snprintf(tmp, sizeof tmp, "%s", bar_ext);
1712 bar_class_name(tmp, sizeof tmp, r);
1715 bar_workspace_name(tmp, sizeof tmp, r);
1718 bar_window_float(tmp, sizeof tmp, r);
1721 snprintf(tmp, sizeof tmp, "%d", r->ws->idx + 1);
1724 snprintf(tmp, sizeof tmp, "%d", r->s->idx + 1);
1727 bar_class_title_name(tmp, sizeof tmp, r);
1730 snprintf(tmp, sizeof tmp, "%s", r->ws->stacker);
1733 bar_title_name(tmp, sizeof tmp, r);
1736 bar_urgent(tmp, sizeof tmp);
1739 snprintf(tmp, sizeof tmp, "%s", bar_vertext);
1742 bar_window_name(tmp, sizeof tmp, r);
1745 /* unknown character sequence; copy as-is */
1746 snprintf(tmp, sizeof tmp, "+%c", *fmt);
1752 if ((int)len < limit)
1754 while (limit-- > 0) {
1755 if (*offrep >= sz - 1)
1757 fmtrep[(*offrep)++] = *ptr++;
1765 bar_replace(char *fmt, char *fmtrep, struct swm_region *r, size_t sz)
1770 while (*fmt != '\0') {
1772 /* skip ordinary characters */
1775 fmtrep[off++] = *fmt++;
1779 /* character sequence found; replace it */
1780 fmt = bar_replace_seq(fmt, fmtrep, r, &off, sz);
1789 bar_fmt_expand(char *fmtexp, size_t sz)
1796 /* start by grabbing the current time and date */
1798 localtime_r(&tmt, &tm);
1800 /* figure out what to expand */
1801 if (bar_format != NULL)
1803 else if (bar_format == NULL && clock_enabled)
1805 /* if nothing to expand bail out */
1811 /* copy as-is, just in case the format shouldn't be expanded below */
1812 strlcpy(fmtexp, fmt, sz);
1813 /* finally pass the string through strftime(3) */
1814 #ifndef SWM_DENY_CLOCK_FORMAT
1815 if ((len = strftime(fmtexp, sz, fmt, &tm)) == 0)
1816 warnx("format too long");
1824 char fmtexp[SWM_BAR_MAX], fmtnew[SWM_BAR_MAX];
1825 char fmtrep[SWM_BAR_MAX];
1827 struct swm_region *r;
1829 /* expand the format by first passing it through strftime(3) */
1830 bar_fmt_expand(fmtexp, sizeof fmtexp);
1832 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1833 for (i = 0; i < num_screens; i++) {
1834 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1837 bar_fmt(fmtexp, fmtnew, r, sizeof fmtnew);
1838 bar_replace(fmtnew, fmtrep, r, sizeof fmtrep);
1839 if (bar_font_legacy)
1840 bar_print_legacy(r, fmtrep);
1842 bar_print(r, fmtrep);
1853 if (bar_enabled == 0)
1855 if (bar_extra && bar_extra_running) {
1856 /* ignore short reads; it'll correct itself */
1857 while ((b = fgetln(stdin, &len)) != NULL)
1858 if (b && b[len - 1] == '\n') {
1860 strlcpy((char *)bar_ext, b, sizeof bar_ext);
1862 if (b == NULL && errno != EAGAIN) {
1863 warn("bar_update: bar_extra failed");
1867 strlcpy((char *)bar_ext, "", sizeof bar_ext);
1876 /* suppress unused warning since var is needed */
1883 bar_toggle(struct swm_region *r, union arg *args)
1885 struct swm_region *tmpr;
1888 /* suppress unused warnings since vars are needed */
1892 DNPRINTF(SWM_D_BAR, "bar_toggle\n");
1894 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1896 for (i = 0; i < num_screens; i++)
1897 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1899 xcb_unmap_window(conn, tmpr->bar->id);
1901 for (i = 0; i < num_screens; i++)
1902 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1904 xcb_map_window(conn, tmpr->bar->id);
1907 bar_enabled = !bar_enabled;
1911 /* must be after stack */
1920 struct swm_region *r;
1924 /* do this here because the conf file is in memory */
1925 if (bar_extra && bar_extra_running == 0 && bar_argv[0]) {
1926 /* launch external status app */
1927 bar_extra_running = 1;
1928 if (pipe(bar_pipe) == -1)
1929 err(1, "pipe error");
1930 socket_setnonblock(bar_pipe[0]);
1931 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
1932 if (dup2(bar_pipe[0], 0) == -1)
1934 if (dup2(bar_pipe[1], 1) == -1)
1936 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
1937 err(1, "could not disable SIGPIPE");
1938 switch (bar_pid = fork()) {
1940 err(1, "cannot fork");
1944 execvp(bar_argv[0], bar_argv);
1945 err(1, "%s external app failed", bar_argv[0]);
1947 default: /* parent */
1953 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1954 for (i = 0; i < num_screens; i++)
1955 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1958 wa[0] = screens[i].c[SWM_S_COLOR_BAR].pixel;
1959 wa[1] = screens[i].c[SWM_S_COLOR_BAR_BORDER].pixel;
1960 xcb_change_window_attributes(conn, r->bar->id,
1961 XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL, wa);
1971 while ((s = index(s, '-'))) {
1976 return (count == 14);
1982 char *default_string;
1983 char **missing_charsets;
1984 int num_missing_charsets = 0;
1988 XFreeFontSet(display, bar_fs);
1992 DNPRINTF(SWM_D_INIT, "fontset_init: loading bar_fonts: %s\n", bar_fonts);
1994 bar_fs = XCreateFontSet(display, bar_fonts, &missing_charsets,
1995 &num_missing_charsets, &default_string);
1997 if (num_missing_charsets > 0) {
1998 warnx("Unable to load charset(s):");
2000 for (i = 0; i < num_missing_charsets; ++i)
2001 warnx("%s", missing_charsets[i]);
2003 XFreeStringList(missing_charsets);
2005 if (strcmp(default_string, ""))
2006 warnx("Glyphs from those sets will be replaced "
2007 "by '%s'.", default_string);
2009 warnx("Glyphs from those sets won't be drawn.");
2013 errx(1, "Error creating font set structure.");
2015 bar_fs_extents = XExtentsOfFontSet(bar_fs);
2017 bar_height = bar_fs_extents->max_logical_extent.height +
2018 2 * bar_border_width;
2025 xft_init(struct swm_region *r)
2027 char *font, *d, *search;
2030 if (bar_font == NULL) {
2031 if ((d = strdup(bar_fonts)) == NULL)
2032 errx(1, "insufficient memory.");
2034 while ((font = strsep(&search, ",")) != NULL) {
2038 DNPRINTF(SWM_D_INIT, "xft_init: try font %s\n", font);
2041 bar_font = XftFontOpenXlfd(display, r->s->idx,
2044 bar_font = XftFontOpenName(display, r->s->idx,
2049 warnx("unable to load font %s", font);
2052 DNPRINTF(SWM_D_INIT, "successfully opened "
2060 if (bar_font == NULL)
2061 errx(1, "unable to open a font");
2063 PIXEL_TO_XRENDERCOLOR(r->s->c[SWM_S_COLOR_BAR_FONT].pixel, color);
2065 if (!XftColorAllocValue(display, DefaultVisual(display, r->s->idx),
2066 DefaultColormap(display, r->s->idx), &color, &bar_font_color))
2067 warn("unable to allocate Xft color");
2069 bar_height = bar_font->height + 2 * bar_border_width;
2076 bar_setup(struct swm_region *r)
2078 xcb_screen_t *screen;
2081 DNPRINTF(SWM_D_BAR, "bar_setup: screen %d.\n",
2084 if ((screen = get_screen(r->s->idx)) == NULL)
2085 errx(1, "ERROR: can't get screen %d.", r->s->idx);
2090 if ((r->bar = calloc(1, sizeof(struct swm_bar))) == NULL)
2091 err(1, "bar_setup: calloc: failed to allocate memory.");
2093 if (bar_font_legacy)
2099 Y(r->bar) = bar_at_bottom ? (Y(r) + HEIGHT(r) - bar_height) : Y(r);
2100 WIDTH(r->bar) = WIDTH(r) - 2 * bar_border_width;
2101 HEIGHT(r->bar) = bar_height - 2 * bar_border_width;
2103 r->bar->id = xcb_generate_id(conn);
2104 wa[0] = r->s->c[SWM_S_COLOR_BAR].pixel;
2105 wa[1] = r->s->c[SWM_S_COLOR_BAR_BORDER].pixel;
2106 wa[2] = XCB_EVENT_MASK_EXPOSURE;
2108 xcb_create_window(conn, XCB_COPY_FROM_PARENT, r->bar->id, r->s->root,
2109 X(r->bar), Y(r->bar), WIDTH(r->bar), HEIGHT(r->bar),
2110 bar_border_width, XCB_WINDOW_CLASS_INPUT_OUTPUT,
2111 XCB_COPY_FROM_PARENT, XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL
2112 | XCB_CW_EVENT_MASK, wa);
2114 r->bar->buffer = xcb_generate_id(conn);
2115 xcb_create_pixmap(conn, screen->root_depth, r->bar->buffer, r->bar->id,
2116 WIDTH(r->bar), HEIGHT(r->bar));
2119 xcb_randr_select_input(conn, r->bar->id,
2120 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE);
2123 xcb_map_window(conn, r->bar->id);
2125 DNPRINTF(SWM_D_BAR, "bar_setup: window: 0x%x, (x,y) w x h: (%d,%d) "
2126 "%d x %d\n", WINID(r->bar), X(r->bar), Y(r->bar), WIDTH(r->bar),
2129 if (signal(SIGALRM, bar_signal) == SIG_ERR)
2130 err(1, "could not install bar_signal");
2135 bar_cleanup(struct swm_region *r)
2139 xcb_destroy_window(conn, r->bar->id);
2140 xcb_free_pixmap(conn, r->bar->buffer);
2146 set_win_state(struct ws_win *win, uint16_t state)
2148 uint16_t data[2] = { state, XCB_ATOM_NONE };
2150 DNPRINTF(SWM_D_EVENT, "set_win_state: window: 0x%x\n", win->id);
2155 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id, a_state,
2156 a_state, 32, 2, data);
2160 getstate(xcb_window_t w)
2162 uint32_t result = 0;
2163 xcb_get_property_cookie_t c;
2164 xcb_get_property_reply_t *r;
2166 c = xcb_get_property(conn, 0, w, a_state, a_state, 0L, 2L);
2167 r = xcb_get_property_reply(conn, c, NULL);
2170 if (r->type == a_state && r->format == 32 && r->length == 2)
2171 result = *((uint32_t *)xcb_get_property_value(r));
2175 DNPRINTF(SWM_D_MISC, "getstate property: win 0x%x state %u\n", w,
2181 version(struct swm_region *r, union arg *args)
2183 /* suppress unused warnings since vars are needed */
2187 bar_version = !bar_version;
2189 snprintf(bar_vertext, sizeof bar_vertext,
2190 "Version: %s Build: %s", SPECTRWM_VERSION, buildstr);
2192 strlcpy(bar_vertext, "", sizeof bar_vertext);
2199 client_msg(struct ws_win *win, xcb_atom_t a, xcb_timestamp_t t)
2201 xcb_client_message_event_t ev;
2209 name = get_atom_name(a);
2210 DNPRINTF(SWM_D_EVENT, "client_msg: window: 0x%x, atom: %s(%u), "
2212 win->id, name, a, t);
2216 bzero(&ev, sizeof ev);
2217 ev.response_type = XCB_CLIENT_MESSAGE;
2218 ev.window = win->id;
2221 ev.data.data32[0] = a;
2222 ev.data.data32[1] = t;
2224 xcb_send_event(conn, 0, win->id,
2225 XCB_EVENT_MASK_NO_EVENT, (const char *)&ev);
2228 /* synthetic response to a ConfigureRequest when not making a change */
2230 config_win(struct ws_win *win, xcb_configure_request_event_t *ev)
2232 xcb_configure_notify_event_t ce;
2237 /* send notification of unchanged state. */
2238 bzero(&ce, sizeof(ce));
2239 ce.response_type = XCB_CONFIGURE_NOTIFY;
2242 ce.width = WIDTH(win);
2243 ce.height = HEIGHT(win);
2244 ce.override_redirect = 0;
2249 ce.window = win->id;
2250 ce.border_width = BORDER(win);
2251 ce.above_sibling = XCB_WINDOW_NONE;
2254 ce.event = ev->window;
2255 ce.window = ev->window;
2257 /* make response appear more WM_SIZE_HINTS-compliant */
2258 if (win->sh.flags) {
2259 DNPRINTF(SWM_D_MISC, "config_win: hints: window: 0x%x,"
2260 " sh.flags: %u, min: %d x %d, max: %d x %d, inc: "
2261 "%d x %d\n", win->id, win->sh.flags, SH_MIN_W(win),
2262 SH_MIN_H(win), SH_MAX_W(win), SH_MAX_H(win),
2263 SH_INC_W(win), SH_INC_H(win));
2268 /* the hint may be set... to 0! */
2269 if (SH_MIN_W(win) > 0 && ce.width < SH_MIN_W(win))
2270 ce.width = SH_MIN_W(win);
2271 if (SH_MIN_H(win) > 0 && ce.height < SH_MIN_H(win))
2272 ce.height = SH_MIN_H(win);
2277 /* may also be advertized as 0 */
2278 if (SH_MAX_W(win) > 0 && ce.width > SH_MAX_W(win))
2279 ce.width = SH_MAX_W(win);
2280 if (SH_MAX_H(win) > 0 && ce.height > SH_MAX_H(win))
2281 ce.height = SH_MAX_H(win);
2284 /* resize increment. */
2286 if (SH_INC_W(win) > 1 && ce.width > SH_INC_W(win))
2287 ce.width -= (ce.width - SH_MIN_W(win)) %
2289 if (SH_INC_H(win) > 1 && ce.height > SH_INC_H(win))
2290 ce.height -= (ce.height - SH_MIN_H(win)) %
2294 /* adjust x and y for requested border_width. */
2295 ce.x += BORDER(win) - ev->border_width;
2296 ce.y += BORDER(win) - ev->border_width;
2297 ce.border_width = ev->border_width;
2298 ce.above_sibling = ev->sibling;
2301 DNPRINTF(SWM_D_MISC, "config_win: ewmh: %s, window: 0x%x, (x,y) w x h: "
2302 "(%d,%d) %d x %d, border: %d\n", YESNO(ev == NULL), win->id, ce.x,
2303 ce.y, ce.width, ce.height, ce.border_width);
2305 xcb_send_event(conn, 0, win->id, XCB_EVENT_MASK_STRUCTURE_NOTIFY,
2310 count_win(struct workspace *ws, int count_transient)
2315 TAILQ_FOREACH(win, &ws->winlist, entry) {
2316 if (count_transient == 0 && win->floating)
2318 if (count_transient == 0 && win->transient)
2324 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
2330 quit(struct swm_region *r, union arg *args)
2332 /* suppress unused warnings since vars are needed */
2336 DNPRINTF(SWM_D_MISC, "quit\n");
2341 map_window(struct ws_win *win)
2343 uint32_t val = XCB_STACK_MODE_ABOVE;
2348 DNPRINTF(SWM_D_EVENT, "map_window: win 0x%x, mapped: %s\n", win->id,
2349 YESNO(win->mapped));
2351 xcb_configure_window(conn, win->id,
2352 XCB_CONFIG_WINDOW_STACK_MODE, &val);
2357 xcb_map_window(conn, win->id);
2358 set_win_state(win, XCB_ICCCM_WM_STATE_NORMAL);
2363 unmap_window(struct ws_win *win)
2368 DNPRINTF(SWM_D_EVENT, "unmap_window: win 0x%x, mapped: %s\n", win->id,
2369 YESNO(win->mapped));
2374 xcb_unmap_window(conn, win->id);
2375 set_win_state(win, XCB_ICCCM_WM_STATE_ICONIC);
2383 int i, j, num_screens;
2385 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2386 for (i = 0; i < num_screens; i++)
2387 for (j = 0; j < workspace_limit; j++)
2388 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2393 fake_keypress(struct ws_win *win, xcb_keysym_t keysym, uint16_t modifiers)
2395 xcb_key_press_event_t event;
2396 xcb_keycode_t *keycode;
2401 keycode = xcb_key_symbols_get_keycode(syms, keysym);
2403 DNPRINTF(SWM_D_MISC, "fake_keypress: win 0x%x keycode %u\n",
2406 bzero(&event, sizeof(event));
2407 event.event = win->id;
2408 event.root = win->s->root;
2409 event.child = XCB_WINDOW_NONE;
2410 event.time = XCB_CURRENT_TIME;
2411 event.event_x = X(win);
2412 event.event_y = Y(win);
2415 event.same_screen = 1;
2416 event.detail = *keycode;
2417 event.state = modifiers;
2419 event.response_type = XCB_KEY_PRESS;
2420 xcb_send_event(conn, 1, win->id,
2421 XCB_EVENT_MASK_KEY_PRESS, (const char *)&event);
2423 event.response_type = XCB_KEY_RELEASE;
2424 xcb_send_event(conn, 1, win->id,
2425 XCB_EVENT_MASK_KEY_RELEASE, (const char *)&event);
2431 restart(struct swm_region *r, union arg *args)
2433 /* suppress unused warning since var is needed */
2437 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
2441 execvp(start_argv[0], start_argv);
2442 warn("execvp failed");
2447 root_to_region(xcb_window_t root)
2449 struct swm_region *r = NULL;
2451 xcb_query_pointer_reply_t *qpr;
2453 DNPRINTF(SWM_D_MISC, "root_to_region: window: 0x%x\n", root);
2455 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2456 for (i = 0; i < num_screens; i++)
2457 if (screens[i].root == root)
2460 qpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn,
2461 screens[i].root), NULL);
2464 DNPRINTF(SWM_D_MISC, "root_to_region: pointer: (%d,%d)\n",
2465 qpr->root_x, qpr->root_y);
2466 /* choose a region based on pointer location */
2467 TAILQ_FOREACH(r, &screens[i].rl, entry)
2468 if (X(r) <= qpr->root_x && qpr->root_x < MAX_X(r) &&
2469 Y(r) <= qpr->root_y && qpr->root_y < MAX_Y(r))
2475 r = TAILQ_FIRST(&screens[i].rl);
2481 find_unmanaged_window(xcb_window_t id)
2484 int i, j, num_screens;
2486 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2487 for (i = 0; i < num_screens; i++)
2488 for (j = 0; j < workspace_limit; j++)
2489 TAILQ_FOREACH(win, &screens[i].ws[j].unmanagedlist,
2497 find_window(xcb_window_t id)
2500 int i, j, num_screens;
2501 xcb_query_tree_reply_t *r;
2503 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2504 for (i = 0; i < num_screens; i++)
2505 for (j = 0; j < workspace_limit; j++)
2506 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2510 r = xcb_query_tree_reply(conn, xcb_query_tree(conn, id), NULL);
2514 /* if we were looking for the parent return that window instead */
2515 if (r->parent == 0 || r->root == r->parent) {
2520 /* look for parent */
2521 for (i = 0; i < num_screens; i++)
2522 for (j = 0; j < workspace_limit; j++)
2523 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2524 if (r->parent == win->id) {
2534 spawn(int ws_idx, union arg *args, int close_fd)
2539 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
2541 close(xcb_get_file_descriptor(conn));
2543 setenv("LD_PRELOAD", SWM_LIB, 1);
2545 if (asprintf(&ret, "%d", ws_idx) == -1) {
2546 warn("spawn: asprintf SWM_WS");
2549 setenv("_SWM_WS", ret, 1);
2553 if (asprintf(&ret, "%d", getpid()) == -1) {
2554 warn("spawn: asprintf _SWM_PID");
2557 setenv("_SWM_PID", ret, 1);
2561 if (setsid() == -1) {
2562 warn("spawn: setsid");
2568 * close stdin and stdout to prevent interaction between apps
2569 * and the baraction script
2570 * leave stderr open to record errors
2572 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) == -1) {
2573 warn("spawn: open");
2576 dup2(fd, STDIN_FILENO);
2577 dup2(fd, STDOUT_FILENO);
2582 execvp(args->argv[0], args->argv);
2584 warn("spawn: execvp");
2589 kill_refs(struct ws_win *win)
2591 int i, x, num_screens;
2592 struct swm_region *r;
2593 struct workspace *ws;
2598 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2599 for (i = 0; i < num_screens; i++)
2600 TAILQ_FOREACH(r, &screens[i].rl, entry)
2601 for (x = 0; x < workspace_limit; x++) {
2603 if (win == ws->focus)
2605 if (win == ws->focus_prev)
2606 ws->focus_prev = NULL;
2611 validate_win(struct ws_win *testwin)
2614 struct workspace *ws;
2615 struct swm_region *r;
2616 int i, x, num_screens;
2618 if (testwin == NULL)
2621 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2622 for (i = 0; i < num_screens; i++)
2623 TAILQ_FOREACH(r, &screens[i].rl, entry)
2624 for (x = 0; x < workspace_limit; x++) {
2626 TAILQ_FOREACH(win, &ws->winlist, entry)
2634 validate_ws(struct workspace *testws)
2636 struct swm_region *r;
2637 struct workspace *ws;
2638 int i, x, num_screens;
2640 /* validate all ws */
2641 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2642 for (i = 0; i < num_screens; i++)
2643 TAILQ_FOREACH(r, &screens[i].rl, entry)
2644 for (x = 0; x < workspace_limit; x++) {
2653 unfocus_win(struct ws_win *win)
2655 xcb_window_t none = XCB_WINDOW_NONE;
2657 DNPRINTF(SWM_D_FOCUS, "unfocus_win: window: 0x%x\n", WINID(win));
2661 if (win->ws == NULL) {
2662 DNPRINTF(SWM_D_FOCUS, "unfocus_win: NULL ws.\n");
2666 if (validate_ws(win->ws)) {
2667 DNPRINTF(SWM_D_FOCUS, "unfocus_win: invalid ws.\n");
2671 if (win->ws->r == NULL) {
2672 DNPRINTF(SWM_D_FOCUS, "unfocus_win: NULL region.\n");
2676 if (validate_win(win)) {
2677 DNPRINTF(SWM_D_FOCUS, "unfocus_win: invalid win.\n");
2682 if (win->ws->focus == win) {
2683 win->ws->focus = NULL;
2684 win->ws->focus_prev = win;
2687 if (validate_win(win->ws->focus)) {
2688 kill_refs(win->ws->focus);
2689 win->ws->focus = NULL;
2691 if (validate_win(win->ws->focus_prev)) {
2692 kill_refs(win->ws->focus_prev);
2693 win->ws->focus_prev = NULL;
2696 xcb_change_window_attributes(conn, win->id, XCB_CW_BORDER_PIXEL,
2697 &win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].pixel);
2699 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
2700 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1, &none);
2702 DNPRINTF(SWM_D_FOCUS, "unfocus_win: done.\n");
2706 focus_win(struct ws_win *win)
2708 struct ws_win *cfw = NULL;
2709 xcb_get_input_focus_reply_t *r;
2711 DNPRINTF(SWM_D_FOCUS, "focus_win: window: 0x%x\n", WINID(win));
2716 if (win->ws == NULL)
2719 if (validate_ws(win->ws))
2722 if (validate_win(win)) {
2727 r = xcb_get_input_focus_reply(conn, xcb_get_input_focus(conn), NULL);
2729 cfw = find_window(r->focus);
2735 if (win->ws->focus != win) {
2736 if (win->ws->focus && win->ws->focus != cfw)
2737 unfocus_win(win->ws->focus);
2738 win->ws->focus = win;
2741 if (cfw != win && win->ws->r != NULL) {
2742 /* Set input focus if no input hint, or indicated by hint. */
2743 if (!(win->hints.flags & XCB_ICCCM_WM_HINT_INPUT) ||
2744 (win->hints.flags & XCB_ICCCM_WM_HINT_INPUT &&
2746 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT,
2747 win->id, last_event_time);
2749 /* Tell app it can adjust focus to a specific window. */
2750 if (win->take_focus) {
2751 /* java is special; always tell parent */
2752 if (win->transient && win->java)
2753 client_msg(find_window(win->transient),
2754 a_takefocus, last_event_time);
2756 client_msg(win, a_takefocus, last_event_time);
2759 xcb_change_window_attributes(conn, win->id, XCB_CW_BORDER_PIXEL,
2760 &win->ws->r->s->c[SWM_S_COLOR_FOCUS].pixel);
2762 if (win->ws->cur_layout->flags & SWM_L_MAPONFOCUS ||
2763 win->ws->always_raise)
2766 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
2767 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1,
2774 DNPRINTF(SWM_D_FOCUS, "focus_win: done.\n");
2777 /* If a child window should have focus instead, return it. */
2779 get_focus_magic(struct ws_win *win)
2781 struct ws_win *parent = NULL;
2783 DNPRINTF(SWM_D_FOCUS, "get_focus_magic: window: 0x%x\n", WINID(win));
2787 if (win->transient) {
2788 parent = find_window(win->transient);
2790 /* If parent prefers focus elsewhere, then do so. */
2791 if (parent && parent->focus_child) {
2792 if (validate_win(parent->focus_child) == 0)
2793 win = parent->focus_child;
2795 parent->focus_child = NULL;
2799 /* If this window prefers focus elsewhere, then do so. */
2800 if (win->focus_child) {
2801 if (validate_win(win->focus_child) == 0)
2802 win = win->focus_child;
2804 win->focus_child = NULL;
2811 event_drain(uint8_t rt)
2813 xcb_generic_event_t *evt;
2815 /* ensure all pending requests have been processed before filtering. */
2817 while ((evt = xcb_poll_for_event(conn))) {
2818 if (XCB_EVENT_RESPONSE_TYPE(evt) != rt)
2826 switchws(struct swm_region *r, union arg *args)
2828 int wsid = args->id, unmap_old = 0;
2829 struct swm_region *this_r, *other_r;
2831 struct workspace *new_ws, *old_ws;
2836 if (wsid >= workspace_limit)
2840 old_ws = this_r->ws;
2841 new_ws = &this_r->s->ws[wsid];
2843 DNPRINTF(SWM_D_WS, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
2844 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), old_ws->idx, wsid);
2846 if (new_ws == NULL || old_ws == NULL)
2848 if (new_ws == old_ws)
2851 other_r = new_ws->r;
2852 if (other_r == NULL) {
2853 /* the other workspace is hidden, hide this one */
2857 /* the other ws is visible in another region, exchange them */
2858 other_r->ws_prior = new_ws;
2859 other_r->ws = old_ws;
2860 old_ws->r = other_r;
2862 this_r->ws_prior = old_ws;
2863 this_r->ws = new_ws;
2866 unfocus_win(old_ws->focus);
2870 /* unmap old windows */
2872 TAILQ_FOREACH(win, &old_ws->winlist, entry)
2875 new_ws->focus = get_region_focus(new_ws->r);
2877 if (new_ws->focus) {
2878 /* if workspaces were swapped, then don't wait to set focus */
2880 focus_win(new_ws->focus);
2882 /* make sure bar gets updated if ws is empty */
2888 DNPRINTF(SWM_D_WS, "switchws: done.\n");
2892 cyclews(struct swm_region *r, union arg *args)
2895 struct swm_screen *s = r->s;
2898 DNPRINTF(SWM_D_WS, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2899 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2904 case SWM_ARG_ID_CYCLEWS_UP_ALL:
2907 case SWM_ARG_ID_CYCLEWS_UP:
2908 if (a.id < workspace_limit - 1)
2913 case SWM_ARG_ID_CYCLEWS_DOWN_ALL:
2916 case SWM_ARG_ID_CYCLEWS_DOWN:
2920 a.id = workspace_limit - 1;
2927 (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist)))
2929 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
2933 } while (a.id != r->ws->idx);
2937 priorws(struct swm_region *r, union arg *args)
2943 DNPRINTF(SWM_D_WS, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2944 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2946 if (r->ws_prior == NULL)
2949 a.id = r->ws_prior->idx;
2954 cyclescr(struct swm_region *r, union arg *args)
2956 struct swm_region *rr = NULL;
2957 int i, x, y, num_screens;
2959 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2960 /* do nothing if we don't have more than one screen */
2961 if (!(num_screens > 1 || outputs > 1))
2966 case SWM_ARG_ID_CYCLESC_UP:
2967 rr = TAILQ_NEXT(r, entry);
2969 rr = TAILQ_FIRST(&screens[i].rl);
2971 case SWM_ARG_ID_CYCLESC_DOWN:
2972 rr = TAILQ_PREV(r, swm_region_list, entry);
2974 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
2982 /* move mouse to region */
2984 y = Y(rr) + 1 + (bar_enabled ? bar_height : 0);
2985 xcb_warp_pointer(conn, XCB_WINDOW_NONE, rr->s[i].root, 0, 0, 0, 0,
2988 rr->ws->focus = get_region_focus(rr);
2990 if (rr->ws->focus) {
2991 /* move to focus window */
2992 x = X(rr->ws->focus) + 1;
2993 y = Y(rr->ws->focus) + 1;
2994 xcb_warp_pointer(conn, XCB_WINDOW_NONE, rr->s[i].root, 0, 0, 0,
3002 sort_windows(struct ws_win_list *wl)
3004 struct ws_win *win, *parent, *nxt;
3009 for (win = TAILQ_FIRST(wl); win != TAILQ_END(wl); win = nxt) {
3010 nxt = TAILQ_NEXT(win, entry);
3011 if (win->transient) {
3012 parent = find_window(win->transient);
3013 if (parent == NULL) {
3014 warnx("not possible bug");
3017 TAILQ_REMOVE(wl, win, entry);
3018 TAILQ_INSERT_AFTER(wl, parent, win, entry);
3025 swapwin(struct swm_region *r, union arg *args)
3027 struct ws_win *target, *source;
3028 struct ws_win *cur_focus;
3029 struct ws_win_list *wl;
3032 DNPRINTF(SWM_D_WS, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3033 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
3035 cur_focus = r->ws->focus;
3036 if (cur_focus == NULL)
3040 wl = &source->ws->winlist;
3043 case SWM_ARG_ID_SWAPPREV:
3044 if (source->transient)
3045 source = find_window(source->transient);
3046 target = TAILQ_PREV(source, ws_win_list, entry);
3047 if (target && target->transient)
3048 target = find_window(target->transient);
3049 TAILQ_REMOVE(wl, source, entry);
3051 TAILQ_INSERT_TAIL(wl, source, entry);
3053 TAILQ_INSERT_BEFORE(target, source, entry);
3055 case SWM_ARG_ID_SWAPNEXT:
3056 target = TAILQ_NEXT(source, entry);
3057 /* move the parent and let the sort handle the move */
3058 if (source->transient)
3059 source = find_window(source->transient);
3060 TAILQ_REMOVE(wl, source, entry);
3062 TAILQ_INSERT_HEAD(wl, source, entry);
3064 TAILQ_INSERT_AFTER(wl, target, source, entry);
3066 case SWM_ARG_ID_SWAPMAIN:
3067 target = TAILQ_FIRST(wl);
3068 if (target == source) {
3069 if (source->ws->focus_prev != NULL &&
3070 source->ws->focus_prev != target)
3071 source = source->ws->focus_prev;
3075 if (target == NULL || source == NULL)
3077 source->ws->focus_prev = target;
3078 TAILQ_REMOVE(wl, target, entry);
3079 TAILQ_INSERT_BEFORE(source, target, entry);
3080 TAILQ_REMOVE(wl, source, entry);
3081 TAILQ_INSERT_HEAD(wl, source, entry);
3083 case SWM_ARG_ID_MOVELAST:
3084 TAILQ_REMOVE(wl, source, entry);
3085 TAILQ_INSERT_TAIL(wl, source, entry);
3088 DNPRINTF(SWM_D_MOVE, "swapwin: invalid id: %d\n", args->id);
3100 get_focus_prev(struct ws_win *win)
3102 struct ws_win *winfocus = NULL;
3103 struct ws_win *cur_focus = NULL;
3104 struct ws_win_list *wl = NULL;
3105 struct workspace *ws = NULL;
3107 if (!(win && win->ws))
3112 cur_focus = ws->focus;
3114 DNPRINTF(SWM_D_FOCUS, "get_focus_prev: window: 0x%x, cur_focus: 0x%x\n",
3115 WINID(win), WINID(cur_focus));
3117 /* pickle, just focus on whatever */
3118 if (cur_focus == NULL) {
3119 /* use prev_focus if valid */
3120 if (ws->focus_prev && ws->focus_prev != cur_focus &&
3121 find_window(WINID(ws->focus_prev)))
3122 winfocus = ws->focus_prev;
3126 /* if transient focus on parent */
3127 if (cur_focus->transient) {
3128 winfocus = find_window(cur_focus->transient);
3132 /* if in max_stack try harder */
3133 if ((win->quirks & SWM_Q_FOCUSPREV) ||
3134 (ws->cur_layout->flags & SWM_L_FOCUSPREV)) {
3135 if (cur_focus != ws->focus_prev)
3136 winfocus = ws->focus_prev;
3138 winfocus = TAILQ_PREV(win, ws_win_list, entry);
3143 DNPRINTF(SWM_D_FOCUS, "get_focus_prev: focus_close: %d\n", focus_close);
3145 if (winfocus == NULL || winfocus == win) {
3146 switch (focus_close) {
3147 case SWM_STACK_BOTTOM:
3148 winfocus = TAILQ_FIRST(wl);
3151 winfocus = TAILQ_LAST(wl, ws_win_list);
3153 case SWM_STACK_ABOVE:
3154 if ((winfocus = TAILQ_NEXT(cur_focus, entry)) == NULL) {
3155 if (focus_close_wrap)
3156 winfocus = TAILQ_FIRST(wl);
3158 winfocus = TAILQ_PREV(cur_focus,
3159 ws_win_list, entry);
3162 case SWM_STACK_BELOW:
3163 if ((winfocus = TAILQ_PREV(cur_focus, ws_win_list,
3165 if (focus_close_wrap)
3166 winfocus = TAILQ_LAST(wl, ws_win_list);
3168 winfocus = TAILQ_NEXT(cur_focus, entry);
3174 if (winfocus == NULL) {
3175 if (focus_default == SWM_STACK_TOP)
3176 winfocus = TAILQ_LAST(wl, ws_win_list);
3178 winfocus = TAILQ_FIRST(wl);
3183 return get_focus_magic(winfocus);
3187 get_region_focus(struct swm_region *r)
3189 struct ws_win *winfocus = NULL;
3194 if (r->ws->focus && !r->ws->focus->iconic)
3195 winfocus = r->ws->focus;
3196 else if (r->ws->focus_prev && !r->ws->focus_prev->iconic)
3197 winfocus = r->ws->focus_prev;
3199 TAILQ_FOREACH(winfocus, &r->ws->winlist, entry)
3200 if (!winfocus->iconic)
3203 return get_focus_magic(winfocus);
3207 focus(struct swm_region *r, union arg *args)
3209 struct ws_win *head, *cur_focus = NULL, *winfocus = NULL;
3210 struct ws_win_list *wl = NULL;
3211 struct workspace *ws = NULL;
3217 DNPRINTF(SWM_D_FOCUS, "focus: id: %d\n", args->id);
3219 if ((cur_focus = r->ws->focus) == NULL)
3223 if (TAILQ_EMPTY(wl))
3225 /* make sure there is at least one uniconified window */
3227 TAILQ_FOREACH(winfocus, wl, entry)
3228 if (winfocus->iconic == 0) {
3236 case SWM_ARG_ID_FOCUSPREV:
3237 head = TAILQ_PREV(cur_focus, ws_win_list, entry);
3239 head = TAILQ_LAST(wl, ws_win_list);
3241 if (WINID(winfocus) == cur_focus->transient) {
3242 head = TAILQ_PREV(winfocus, ws_win_list, entry);
3244 head = TAILQ_LAST(wl, ws_win_list);
3249 if (winfocus && winfocus->iconic) {
3250 while (winfocus != cur_focus) {
3251 if (winfocus == NULL)
3252 winfocus = TAILQ_LAST(wl, ws_win_list);
3253 if (winfocus->iconic == 0)
3255 winfocus = TAILQ_PREV(winfocus, ws_win_list,
3261 case SWM_ARG_ID_FOCUSNEXT:
3262 head = TAILQ_NEXT(cur_focus, entry);
3264 head = TAILQ_FIRST(wl);
3268 if (winfocus && winfocus->iconic) {
3269 while (winfocus != cur_focus) {
3270 if (winfocus == NULL)
3271 winfocus = TAILQ_FIRST(wl);
3272 if (winfocus->iconic == 0)
3274 winfocus = TAILQ_NEXT(winfocus, entry);
3279 case SWM_ARG_ID_FOCUSMAIN:
3280 winfocus = TAILQ_FIRST(wl);
3281 if (winfocus == cur_focus)
3282 winfocus = cur_focus->ws->focus_prev;
3289 focus_win(get_focus_magic(winfocus));
3295 cycle_layout(struct swm_region *r, union arg *args)
3297 struct workspace *ws = r->ws;
3299 /* suppress unused warning since var is needed */
3302 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
3305 if (ws->cur_layout->l_stack == NULL)
3306 ws->cur_layout = &layouts[0];
3311 focus_win(get_region_focus(r));
3317 stack_config(struct swm_region *r, union arg *args)
3319 struct workspace *ws = r->ws;
3321 DNPRINTF(SWM_D_STACK, "stack_config: id: %d workspace: %d\n",
3324 if (ws->cur_layout->l_config != NULL)
3325 ws->cur_layout->l_config(ws, args->id);
3327 if (args->id != SWM_ARG_ID_STACKINIT)
3336 struct swm_geometry g;
3337 struct swm_region *r;
3343 DNPRINTF(SWM_D_STACK, "stack: begin\n");
3345 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
3346 for (i = 0; i < num_screens; i++) {
3350 TAILQ_FOREACH(r, &screens[i].rl, entry) {
3351 DNPRINTF(SWM_D_STACK, "stack: workspace: %d "
3352 "(screen: %d, region: %d)\n", r->ws->idx, i, j++);
3354 /* start with screen geometry, adjust for bar */
3356 g.w -= 2 * border_width;
3357 g.h -= 2 * border_width;
3363 r->ws->cur_layout->l_stack(r->ws, &g);
3364 r->ws->cur_layout->l_string(r->ws);
3365 /* save r so we can track region changes */
3372 DNPRINTF(SWM_D_STACK, "stack: end\n");
3376 store_float_geom(struct ws_win *win, struct swm_region *r)
3378 if (win == NULL || r == NULL)
3381 /* retain window geom and region geom */
3382 win->g_float = win->g;
3383 win->g_float.x -= X(r);
3384 win->g_float.y -= Y(r);
3385 win->g_floatvalid = 1;
3386 DNPRINTF(SWM_D_MISC, "store_float_geom: window: 0x%x, g: (%d,%d)"
3387 " %d x %d, g_float: (%d,%d) %d x %d\n", win->id, X(win), Y(win),
3388 WIDTH(win), HEIGHT(win), win->g_float.x, win->g_float.y,
3389 win->g_float.w, win->g_float.h);
3393 load_float_geom(struct ws_win *win, struct swm_region *r)
3395 if (win == NULL || r == NULL)
3398 if (win->g_floatvalid) {
3399 win->g = win->g_float;
3402 DNPRINTF(SWM_D_MISC, "load_float_geom: window: 0x%x, g: (%d,%d)"
3403 "%d x %d\n", win->id, X(win), Y(win), WIDTH(win),
3406 DNPRINTF(SWM_D_MISC, "load_float_geom: window: 0x%x, g_float "
3407 "is not set.\n", win->id);
3412 stack_floater(struct ws_win *win, struct swm_region *r)
3417 DNPRINTF(SWM_D_MISC, "stack_floater: window: 0x%x\n", win->id);
3420 * to allow windows to change their size (e.g. mplayer fs) only retrieve
3421 * geom on ws switches or return from max mode
3423 if (win->floatmaxed || (r != r->ws->old_r &&
3424 !(win->ewmh_flags & EWMH_F_FULLSCREEN))) {
3425 /* update geometry for the new region */
3426 load_float_geom(win, r);
3429 win->floatmaxed = 0;
3432 * if set to fullscreen mode, configure window to maximum size.
3434 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
3435 if (!win->g_floatvalid)
3436 store_float_geom(win, win->ws->r);
3442 * remove border on fullscreen floater when in fullscreen mode or when
3443 * the quirk is present.
3445 if ((win->ewmh_flags & EWMH_F_FULLSCREEN) ||
3446 ((win->quirks & SWM_Q_FULLSCREEN) &&
3447 (WIDTH(win) >= WIDTH(r)) && (HEIGHT(win) >= HEIGHT(r)))) {
3448 if (win->bordered) {
3450 X(win) += border_width;
3451 Y(win) += border_width;
3453 } else if (!win->bordered) {
3455 X(win) -= border_width;
3456 Y(win) -= border_width;
3459 if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
3460 WIDTH(win) = (double)WIDTH(r) * dialog_ratio;
3461 HEIGHT(win) = (double)HEIGHT(r) * dialog_ratio;
3464 if (!win->manual && !(win->ewmh_flags & EWMH_F_FULLSCREEN) &&
3465 !(win->quirks & SWM_Q_ANYWHERE)) {
3467 * floaters and transients are auto-centred unless moved,
3468 * resized or ANYWHERE quirk is set.
3470 X(win) = X(r) + (WIDTH(r) - WIDTH(win)) / 2 - BORDER(win);
3471 Y(win) = Y(r) + (HEIGHT(r) - HEIGHT(win)) / 2 - BORDER(win);
3473 store_float_geom(win, r);
3476 /* keep window within region bounds */
3477 constrain_window(win, r, 0);
3483 * Send keystrokes to terminal to decrease/increase the font size as the
3484 * window size changes.
3487 adjust_font(struct ws_win *win)
3489 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
3490 win->floating || win->transient)
3493 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
3494 WIDTH(win) / win->sh.width_inc < term_width &&
3495 win->font_steps < SWM_MAX_FONT_STEPS) {
3496 win->font_size_boundary[win->font_steps] =
3497 (win->sh.width_inc * term_width) + win->sh.base_width;
3500 win->last_inc = win->sh.width_inc;
3501 fake_keypress(win, XK_KP_Subtract, XCB_MOD_MASK_SHIFT);
3502 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
3503 WIDTH(win) > win->font_size_boundary[win->font_steps - 1]) {
3506 win->last_inc = win->sh.width_inc;
3507 fake_keypress(win, XK_KP_Add, XCB_MOD_MASK_SHIFT);
3511 #define SWAPXY(g) do { \
3513 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
3514 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
3517 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
3519 struct swm_geometry win_g, r_g = *g;
3520 struct ws_win *win, *fs_win = NULL;
3521 int i, j, s, stacks;
3522 int w_inc = 1, h_inc, w_base = 1, h_base;
3523 int hrh, extra = 0, h_slice, last_h = 0;
3524 int split, colno, winno, mwin, msize, mscale;
3525 int remain, missing, v_slice, reconfigure;
3528 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d, rot: %s, "
3529 "flip: %s\n", ws->idx, YESNO(rot), YESNO(flip));
3531 winno = count_win(ws, 0);
3532 if (winno == 0 && count_win(ws, 1) == 0)
3535 TAILQ_FOREACH(win, &ws->winlist, entry)
3536 if (win->transient == 0 && win->floating == 0
3537 && win->iconic == 0)
3544 w_inc = win->sh.width_inc;
3545 w_base = win->sh.base_width;
3546 mwin = ws->l_state.horizontal_mwin;
3547 mscale = ws->l_state.horizontal_msize;
3548 stacks = ws->l_state.horizontal_stacks;
3551 w_inc = win->sh.height_inc;
3552 w_base = win->sh.base_height;
3553 mwin = ws->l_state.vertical_mwin;
3554 mscale = ws->l_state.vertical_msize;
3555 stacks = ws->l_state.vertical_stacks;
3559 if (stacks > winno - mwin)
3560 stacks = winno - mwin;
3564 h_slice = r_g.h / SWM_H_SLICE;
3565 if (mwin && winno > mwin) {
3566 v_slice = r_g.w / SWM_V_SLICE;
3570 win_g.w = v_slice * mscale;
3572 if (w_inc > 1 && w_inc < v_slice) {
3573 /* adjust for window's requested size increment */
3574 remain = (win_g.w - w_base) % w_inc;
3580 win_g.x += r_g.w - msize;
3583 colno = split = winno / stacks;
3584 win_g.w = ((r_g.w - (stacks * 2 * border_width) +
3585 2 * border_width) / stacks);
3587 hrh = r_g.h / colno;
3588 extra = r_g.h - (colno * hrh);
3589 win_g.h = hrh - 2 * border_width;
3591 /* stack all the tiled windows */
3592 i = j = 0, s = stacks;
3593 TAILQ_FOREACH(win, &ws->winlist, entry) {
3594 if (win->transient != 0 || win->floating != 0)
3596 if (win->iconic != 0)
3599 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
3604 if (split && i == split) {
3605 colno = (winno - mwin) / stacks;
3606 if (s <= (winno - mwin) % stacks)
3608 split = split + colno;
3609 hrh = (r_g.h / colno);
3610 extra = r_g.h - (colno * hrh);
3614 win_g.x += win_g.w + 2 * border_width;
3615 win_g.w = (r_g.w - msize -
3616 (stacks * 2 * border_width)) / stacks;
3618 win_g.w += (r_g.w - msize -
3619 (stacks * 2 * border_width)) % stacks;
3623 win_g.h = hrh - 2 * border_width;
3625 h_inc = win->sh.width_inc;
3626 h_base = win->sh.base_width;
3628 h_inc = win->sh.height_inc;
3629 h_base = win->sh.base_height;
3631 if (j == colno - 1) {
3632 win_g.h = hrh + extra;
3633 } else if (h_inc > 1 && h_inc < h_slice) {
3634 /* adjust for window's requested size increment */
3635 remain = (win_g.h - h_base) % h_inc;
3636 missing = h_inc - remain;
3638 if (missing <= extra || j == 0) {
3650 win_g.y += last_h + 2 * border_width;
3652 if (disable_border && bar_enabled == 0 && winno == 1){
3654 win_g.w += 2 * border_width;
3655 win_g.h += 2 * border_width;
3660 if (X(win) != win_g.y || Y(win) != win_g.x ||
3661 WIDTH(win) != win_g.h || HEIGHT(win) != win_g.w) {
3665 WIDTH(win) = win_g.h;
3666 HEIGHT(win) = win_g.w;
3669 if (X(win) != win_g.x || Y(win) != win_g.y ||
3670 WIDTH(win) != win_g.w || HEIGHT(win) != win_g.h) {
3674 WIDTH(win) = win_g.w;
3675 HEIGHT(win) = win_g.h;
3679 if (bordered != win->bordered) {
3681 win->bordered = bordered;
3697 /* now, stack all the floaters and transients */
3698 TAILQ_FOREACH(win, &ws->winlist, entry) {
3699 if (win->transient == 0 && win->floating == 0)
3701 if (win->iconic == 1)
3703 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
3708 stack_floater(win, ws->r);
3713 stack_floater(fs_win, ws->r);
3719 vertical_config(struct workspace *ws, int id)
3721 DNPRINTF(SWM_D_STACK, "vertical_config: id: %d, workspace: %d\n",
3725 case SWM_ARG_ID_STACKRESET:
3726 case SWM_ARG_ID_STACKINIT:
3727 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
3728 ws->l_state.vertical_mwin = 1;
3729 ws->l_state.vertical_stacks = 1;
3731 case SWM_ARG_ID_MASTERSHRINK:
3732 if (ws->l_state.vertical_msize > 1)
3733 ws->l_state.vertical_msize--;
3735 case SWM_ARG_ID_MASTERGROW:
3736 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
3737 ws->l_state.vertical_msize++;
3739 case SWM_ARG_ID_MASTERADD:
3740 ws->l_state.vertical_mwin++;
3742 case SWM_ARG_ID_MASTERDEL:
3743 if (ws->l_state.vertical_mwin > 0)
3744 ws->l_state.vertical_mwin--;
3746 case SWM_ARG_ID_STACKINC:
3747 ws->l_state.vertical_stacks++;
3749 case SWM_ARG_ID_STACKDEC:
3750 if (ws->l_state.vertical_stacks > 1)
3751 ws->l_state.vertical_stacks--;
3753 case SWM_ARG_ID_FLIPLAYOUT:
3754 ws->l_state.vertical_flip = !ws->l_state.vertical_flip;
3762 vertical_stack(struct workspace *ws, struct swm_geometry *g)
3764 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
3766 stack_master(ws, g, 0, ws->l_state.vertical_flip);
3770 horizontal_config(struct workspace *ws, int id)
3772 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
3775 case SWM_ARG_ID_STACKRESET:
3776 case SWM_ARG_ID_STACKINIT:
3777 ws->l_state.horizontal_mwin = 1;
3778 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
3779 ws->l_state.horizontal_stacks = 1;
3781 case SWM_ARG_ID_MASTERSHRINK:
3782 if (ws->l_state.horizontal_msize > 1)
3783 ws->l_state.horizontal_msize--;
3785 case SWM_ARG_ID_MASTERGROW:
3786 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
3787 ws->l_state.horizontal_msize++;
3789 case SWM_ARG_ID_MASTERADD:
3790 ws->l_state.horizontal_mwin++;
3792 case SWM_ARG_ID_MASTERDEL:
3793 if (ws->l_state.horizontal_mwin > 0)
3794 ws->l_state.horizontal_mwin--;
3796 case SWM_ARG_ID_STACKINC:
3797 ws->l_state.horizontal_stacks++;
3799 case SWM_ARG_ID_STACKDEC:
3800 if (ws->l_state.horizontal_stacks > 1)
3801 ws->l_state.horizontal_stacks--;
3803 case SWM_ARG_ID_FLIPLAYOUT:
3804 ws->l_state.horizontal_flip = !ws->l_state.horizontal_flip;
3812 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
3814 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
3816 stack_master(ws, g, 1, ws->l_state.horizontal_flip);
3819 /* fullscreen view */
3821 max_stack(struct workspace *ws, struct swm_geometry *g)
3823 struct swm_geometry gg = *g;
3824 struct ws_win *win, *wintrans = NULL, *parent = NULL;
3825 int winno, num_screens;
3827 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
3832 winno = count_win(ws, 0);
3833 if (winno == 0 && count_win(ws, 1) == 0)
3836 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
3837 TAILQ_FOREACH(win, &ws->winlist, entry) {
3838 if (win->transient) {
3840 parent = find_window(win->transient);
3844 if (win->floating && win->floatmaxed == 0 ) {
3846 * retain geometry for retrieval on exit from
3849 store_float_geom(win, ws->r);
3850 win->floatmaxed = 1;
3853 /* only reconfigure if necessary */
3854 if (X(win) != gg.x || Y(win) != gg.y || WIDTH(win) != gg.w ||
3855 HEIGHT(win) != gg.h) {
3861 WIDTH(win) += 2 * border_width;
3862 HEIGHT(win) += 2 * border_width;
3867 /* unmap only if we don't have multi screen */
3868 if (win != ws->focus)
3869 if (!(num_screens > 1 || outputs > 1))
3873 /* put the last transient on top */
3877 stack_floater(wintrans, ws->r);
3878 ws->focus = get_focus_magic(wintrans);
3883 send_to_ws(struct swm_region *r, union arg *args)
3885 int wsid = args->id;
3886 struct ws_win *win = NULL, *parent;
3887 struct workspace *ws, *nws;
3888 char ws_idx_str[SWM_PROPLEN];
3891 if (wsid >= workspace_limit)
3894 if (r && r->ws && r->ws->focus)
3900 if (win->ws->idx == wsid)
3903 DNPRINTF(SWM_D_MOVE, "send_to_ws: window: 0x%x\n", win->id);
3906 nws = &win->s->ws[wsid];
3908 a.id = SWM_ARG_ID_FOCUSPREV;
3910 if (win->transient) {
3911 parent = find_window(win->transient);
3913 unmap_window(parent);
3914 TAILQ_REMOVE(&ws->winlist, parent, entry);
3915 TAILQ_INSERT_TAIL(&nws->winlist, parent, entry);
3920 TAILQ_REMOVE(&ws->winlist, win, entry);
3921 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
3922 if (TAILQ_EMPTY(&ws->winlist))
3923 r->ws->focus = NULL;
3926 /* Try to update the window's workspace property */
3927 if (snprintf(ws_idx_str, SWM_PROPLEN, "%d", nws->idx) < SWM_PROPLEN) {
3928 DNPRINTF(SWM_D_PROP, "send_to_ws: set property: _SWM_WS: %s\n",
3930 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
3931 a_swm_ws, XCB_ATOM_STRING, 8, strlen(ws_idx_str),
3942 pressbutton(struct swm_region *r, union arg *args)
3944 /* suppress unused warning since var is needed */
3947 xcb_test_fake_input(conn, XCB_BUTTON_PRESS, args->id,
3948 XCB_CURRENT_TIME, XCB_WINDOW_NONE, 0, 0, 0);
3949 xcb_test_fake_input(conn, XCB_BUTTON_RELEASE, args->id,
3950 XCB_CURRENT_TIME, XCB_WINDOW_NONE, 0, 0, 0);
3954 raise_toggle(struct swm_region *r, union arg *args)
3956 /* suppress unused warning since var is needed */
3959 if (r == NULL || r->ws == NULL)
3962 r->ws->always_raise = !r->ws->always_raise;
3964 /* bring floaters back to top */
3965 if (r->ws->always_raise == 0)
3972 iconify(struct swm_region *r, union arg *args)
3974 /* suppress unused warning since var is needed */
3977 if (r->ws->focus == NULL)
3980 update_iconic(r->ws->focus, 1);
3986 get_win_name(xcb_window_t win)
3989 xcb_get_property_cookie_t c;
3990 xcb_get_property_reply_t *r;
3992 /* First try _NET_WM_NAME for UTF-8. */
3993 c = xcb_get_property(conn, 0, win, a_netwmname,
3994 XCB_GET_PROPERTY_TYPE_ANY, 0, UINT_MAX);
3995 r = xcb_get_property_reply(conn, c, NULL);
3997 if (!r || r->type == XCB_NONE) {
3999 /* Use WM_NAME instead; no UTF-8. */
4000 c = xcb_get_property(conn, 0, win, XCB_ATOM_WM_NAME,
4001 XCB_GET_PROPERTY_TYPE_ANY, 0, UINT_MAX);
4002 r = xcb_get_property_reply(conn, c, NULL);
4004 if(!r || r->type == XCB_NONE) {
4011 name = strndup(xcb_get_property_value(r),
4012 xcb_get_property_value_length(r));
4019 uniconify(struct swm_region *r, union arg *args)
4026 DNPRINTF(SWM_D_MISC, "uniconify\n");
4028 if (r == NULL || r->ws == NULL)
4031 /* make sure we have anything to uniconify */
4032 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
4033 if (win->ws == NULL)
4034 continue; /* should never happen */
4035 if (win->iconic == 0)
4043 search_resp_action = SWM_SEARCH_UNICONIFY;
4045 spawn_select(r, args, "search", &searchpid);
4047 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
4050 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
4051 if (win->ws == NULL)
4052 continue; /* should never happen */
4053 if (win->iconic == 0)
4056 name = get_win_name(win->id);
4059 fprintf(lfile, "%s.%u\n", name, win->id);
4067 name_workspace(struct swm_region *r, union arg *args)
4071 DNPRINTF(SWM_D_MISC, "name_workspace\n");
4077 search_resp_action = SWM_SEARCH_NAME_WORKSPACE;
4079 spawn_select(r, args, "name_workspace", &searchpid);
4081 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
4084 fprintf(lfile, "%s", "");
4089 search_workspace(struct swm_region *r, union arg *args)
4092 struct workspace *ws;
4095 DNPRINTF(SWM_D_MISC, "search_workspace\n");
4101 search_resp_action = SWM_SEARCH_SEARCH_WORKSPACE;
4103 spawn_select(r, args, "search", &searchpid);
4105 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
4108 for (i = 0; i < workspace_limit; i++) {
4112 fprintf(lfile, "%d%s%s\n", ws->idx + 1,
4113 (ws->name ? ":" : ""), (ws->name ? ws->name : ""));
4120 search_win_cleanup(void)
4122 struct search_window *sw = NULL;
4124 while ((sw = TAILQ_FIRST(&search_wl)) != NULL) {
4125 xcb_destroy_window(conn, sw->indicator);
4126 TAILQ_REMOVE(&search_wl, sw, entry);
4132 search_win(struct swm_region *r, union arg *args)
4134 struct ws_win *win = NULL;
4135 struct search_window *sw = NULL;
4138 int i, width, height;
4146 XRectangle l_ibox, l_lbox;
4148 DNPRINTF(SWM_D_MISC, "search_win\n");
4151 search_resp_action = SWM_SEARCH_SEARCH_WINDOW;
4153 spawn_select(r, args, "search", &searchpid);
4155 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
4158 TAILQ_INIT(&search_wl);
4161 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
4162 if (win->iconic == 1)
4165 sw = calloc(1, sizeof(struct search_window));
4167 warn("search_win: calloc");
4169 search_win_cleanup();
4175 snprintf(s, sizeof s, "%d", i);
4178 w = xcb_generate_id(conn);
4179 wa[0] = r->s->c[SWM_S_COLOR_FOCUS].pixel;
4180 wa[1] = r->s->c[SWM_S_COLOR_UNFOCUS].pixel;
4182 if (bar_font_legacy) {
4183 XmbTextExtents(bar_fs, s, len, &l_ibox, &l_lbox);
4184 width = l_lbox.width + 4;
4185 height = bar_fs_extents->max_logical_extent.height + 4;
4187 XftTextExtentsUtf8(display, bar_font, (FcChar8 *)s, len,
4189 width = info.width + 4;
4190 height = bar_font->height + 4;
4193 xcb_create_window(conn, XCB_COPY_FROM_PARENT, w, win->id, 0, 0,
4194 width, height, 1, XCB_WINDOW_CLASS_INPUT_OUTPUT,
4195 XCB_COPY_FROM_PARENT, XCB_CW_BACK_PIXEL |
4196 XCB_CW_BORDER_PIXEL, wa);
4198 xcb_map_window(conn, w);
4201 TAILQ_INSERT_TAIL(&search_wl, sw, entry);
4203 if (bar_font_legacy) {
4204 l_gcv.graphics_exposures = 0;
4205 l_draw = XCreateGC(display, w, 0, &l_gcv);
4207 XSetForeground(display, l_draw,
4208 r->s->c[SWM_S_COLOR_BAR].pixel);
4210 DRAWSTRING(display, w, bar_fs, l_draw, 2,
4211 (bar_fs_extents->max_logical_extent.height -
4212 l_lbox.height) / 2 - l_lbox.y, s, len);
4214 XFreeGC(display, l_draw);
4217 draw = XftDrawCreate(display, w,
4218 DefaultVisual(display, r->s->idx),
4219 DefaultColormap(display, r->s->idx));
4221 XftDrawStringUtf8(draw, &bar_font_color, bar_font, 2,
4222 (HEIGHT(r->bar) + bar_font->height) / 2 -
4223 bar_font->descent, (FcChar8 *)s, len);
4225 XftDrawDestroy(draw);
4228 DNPRINTF(SWM_D_MISC, "search_win: mapped window: 0x%x\n", w);
4230 fprintf(lfile, "%d\n", i);
4240 search_resp_uniconify(char *resp, unsigned long len)
4246 DNPRINTF(SWM_D_MISC, "search_resp_uniconify: resp: %s\n", resp);
4248 TAILQ_FOREACH(win, &search_r->ws->winlist, entry) {
4249 if (win->iconic == 0)
4251 name = get_win_name(win->id);
4254 if (asprintf(&s, "%s.%u", name, win->id) == -1) {
4259 if (strncmp(s, resp, len) == 0) {
4260 /* XXX this should be a callback to generalize */
4261 update_iconic(win, 0);
4271 search_resp_name_workspace(char *resp, unsigned long len)
4273 struct workspace *ws;
4275 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: resp: %s\n", resp);
4277 if (search_r->ws == NULL)
4282 free(search_r->ws->name);
4283 search_r->ws->name = NULL;
4287 ws->name = strdup(resp);
4288 if (ws->name == NULL) {
4289 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: "
4290 "strdup: %s", strerror(errno));
4297 search_resp_search_workspace(char *resp)
4304 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: resp: %s\n", resp);
4308 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: strdup: %s",
4315 ws_idx = (int)strtonum(q, 1, workspace_limit, &errstr);
4317 DNPRINTF(SWM_D_MISC, "workspace idx is %s: %s",
4324 switchws(search_r, &a);
4328 search_resp_search_window(char *resp)
4333 struct search_window *sw;
4335 DNPRINTF(SWM_D_MISC, "search_resp_search_window: resp: %s\n", resp);
4339 DNPRINTF(SWM_D_MISC, "search_resp_search_window: strdup: %s",
4344 idx = (int)strtonum(s, 1, INT_MAX, &errstr);
4346 DNPRINTF(SWM_D_MISC, "window idx is %s: %s",
4353 TAILQ_FOREACH(sw, &search_wl, entry)
4354 if (idx == sw->idx) {
4360 #define MAX_RESP_LEN 1024
4363 search_do_resp(void)
4369 DNPRINTF(SWM_D_MISC, "search_do_resp:\n");
4374 if ((resp = calloc(1, MAX_RESP_LEN + 1)) == NULL) {
4375 warn("search: calloc");
4379 rbytes = read(select_resp_pipe[0], resp, MAX_RESP_LEN);
4381 warn("search: read error");
4384 resp[rbytes] = '\0';
4387 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
4388 * newline, so work around that by sanitizing the resp now.
4390 resp[strcspn(resp, "\n")] = '\0';
4393 switch (search_resp_action) {
4394 case SWM_SEARCH_UNICONIFY:
4395 search_resp_uniconify(resp, len);
4397 case SWM_SEARCH_NAME_WORKSPACE:
4398 search_resp_name_workspace(resp, len);
4400 case SWM_SEARCH_SEARCH_WORKSPACE:
4401 search_resp_search_workspace(resp);
4403 case SWM_SEARCH_SEARCH_WINDOW:
4404 search_resp_search_window(resp);
4409 if (search_resp_action == SWM_SEARCH_SEARCH_WINDOW)
4410 search_win_cleanup();
4412 search_resp_action = SWM_SEARCH_NONE;
4413 close(select_resp_pipe[0]);
4420 wkill(struct swm_region *r, union arg *args)
4422 DNPRINTF(SWM_D_MISC, "wkill: id: %d\n", args->id);
4424 if (r->ws->focus == NULL)
4427 if (args->id == SWM_ARG_ID_KILLWINDOW)
4428 xcb_kill_client(conn, r->ws->focus->id);
4430 if (r->ws->focus->can_delete)
4431 client_msg(r->ws->focus, a_delete, 0);
4438 floating_toggle_win(struct ws_win *win)
4440 struct swm_region *r;
4450 /* reject floating toggles in max stack mode */
4451 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK])
4454 if (win->floating) {
4455 if (!win->floatmaxed) {
4456 /* retain position for refloat */
4457 store_float_geom(win, r);
4461 load_float_geom(win, r);
4465 ewmh_update_actions(win);
4471 floating_toggle(struct swm_region *r, union arg *args)
4473 struct ws_win *win = r->ws->focus;
4475 /* suppress unused warning since var is needed */
4481 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
4484 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
4485 _NET_WM_STATE_TOGGLE);
4489 if (win == win->ws->focus)
4496 constrain_window(struct ws_win *win, struct swm_region *r, int resizable)
4498 if (MAX_X(win) + BORDER(win) > MAX_X(r)) {
4500 WIDTH(win) = MAX_X(r) - X(win) - BORDER(win);
4502 X(win) = MAX_X(r)- WIDTH(win) - BORDER(win);
4505 if (X(win) + BORDER(win) < X(r)) {
4507 WIDTH(win) -= X(r) - X(win) - BORDER(win);
4509 X(win) = X(r) - BORDER(win);
4512 if (MAX_Y(win) + BORDER(win) > MAX_Y(r)) {
4514 HEIGHT(win) = MAX_Y(r) - Y(win) - BORDER(win);
4516 Y(win) = MAX_Y(r) - HEIGHT(win) - BORDER(win);
4519 if (Y(win) + BORDER(win) < Y(r)) {
4521 HEIGHT(win) -= Y(r) - Y(win) - BORDER(win);
4523 Y(win) = Y(r) - BORDER(win);
4529 if (HEIGHT(win) < 1)
4535 update_window(struct ws_win *win)
4540 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
4541 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
4542 XCB_CONFIG_WINDOW_BORDER_WIDTH;
4546 wc[3] = HEIGHT(win);
4547 wc[4] = BORDER(win);
4549 DNPRINTF(SWM_D_EVENT, "update_window: window: 0x%x, (x,y) w x h: "
4550 "(%d,%d) %d x %d, bordered: %s\n", win->id, wc[0], wc[1], wc[2],
4551 wc[3], YESNO(win->bordered));
4553 xcb_configure_window(conn, win->id, mask, wc);
4556 #define SWM_RESIZE_STEPS (50)
4559 resize(struct ws_win *win, union arg *args)
4561 xcb_timestamp_t timestamp = 0;
4562 struct swm_region *r = NULL;
4563 int resize_step = 0;
4564 struct swm_geometry g;
4565 int top = 0, left = 0, resizing;
4567 unsigned int shape; /* cursor style */
4568 xcb_cursor_t cursor;
4569 xcb_font_t cursor_font;
4570 xcb_query_pointer_reply_t *xpr;
4571 xcb_generic_event_t *evt;
4572 xcb_motion_notify_event_t *mne;
4578 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
4581 DNPRINTF(SWM_D_EVENT, "resize: window: 0x%x, floating: %s, "
4582 "transient: 0x%x\n", win->id, YESNO(win->floating),
4585 if (!(win->transient != 0 || win->floating != 0))
4588 /* reject resizes in max mode for floaters (transient ok) */
4589 if (win->floatmaxed)
4593 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
4601 case SWM_ARG_ID_WIDTHSHRINK:
4602 WIDTH(win) -= SWM_RESIZE_STEPS;
4605 case SWM_ARG_ID_WIDTHGROW:
4606 WIDTH(win) += SWM_RESIZE_STEPS;
4609 case SWM_ARG_ID_HEIGHTSHRINK:
4610 HEIGHT(win) -= SWM_RESIZE_STEPS;
4613 case SWM_ARG_ID_HEIGHTGROW:
4614 HEIGHT(win) += SWM_RESIZE_STEPS;
4621 constrain_window(win, r, 1);
4623 store_float_geom(win,r);
4627 /* get cursor offset from window root */
4628 xpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, win->id),
4635 if (xpr->win_x < WIDTH(win) / 2)
4638 if (xpr->win_y < HEIGHT(win) / 2)
4641 if (args->id == SWM_ARG_ID_CENTER)
4644 shape = (left) ? XC_top_left_corner : XC_top_right_corner;
4646 shape = (left) ? XC_bottom_left_corner : XC_bottom_right_corner;
4648 cursor_font = xcb_generate_id(conn);
4649 xcb_open_font(conn, cursor_font, strlen("cursor"), "cursor");
4651 cursor = xcb_generate_id(conn);
4652 xcb_create_glyph_cursor(conn, cursor, cursor_font, cursor_font,
4653 shape, shape + 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
4655 xcb_grab_pointer(conn, 0, win->id, MOUSEMASK,
4656 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, XCB_WINDOW_NONE, cursor,
4661 while ((evt = xcb_wait_for_event(conn)) && resizing) {
4662 switch (XCB_EVENT_RESPONSE_TYPE(evt)) {
4663 case XCB_BUTTON_RELEASE:
4664 DNPRINTF(SWM_D_EVENT, "resize: BUTTON_RELEASE\n");
4667 case XCB_MOTION_NOTIFY:
4668 mne = (xcb_motion_notify_event_t *)evt;
4669 /* cursor offset/delta from start of the operation */
4670 dx = mne->root_x - xpr->root_x;
4671 dy = mne->root_y - xpr->root_y;
4677 if (args->id == SWM_ARG_ID_CENTER) {
4678 if (g.h / 2 + dy < 1)
4682 HEIGHT(win) = g.h + 2 * dy;
4690 HEIGHT(win) = g.h + dy;
4697 if (args->id == SWM_ARG_ID_CENTER) {
4698 if (g.w / 2 + dx < 1)
4702 WIDTH(win) = g.w + 2 * dx;
4710 WIDTH(win) = g.w + dx;
4713 constrain_window(win, r, 1);
4715 /* not free, don't sync more than 120 times / second */
4716 if ((mne->time - timestamp) > (1000 / 120) ) {
4717 timestamp = mne->time;
4732 store_float_geom(win,r);
4734 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
4735 xcb_free_cursor(conn, cursor);
4736 xcb_close_font(conn, cursor_font);
4738 DNPRINTF(SWM_D_EVENT, "resize: done.\n");
4742 resize_step(struct swm_region *r, union arg *args)
4744 struct ws_win *win = NULL;
4746 if (r && r->ws && r->ws->focus)
4754 #define SWM_MOVE_STEPS (50)
4757 move(struct ws_win *win, union arg *args)
4759 xcb_timestamp_t timestamp = 0;
4760 int move_step = 0, moving;
4761 struct swm_region *r = NULL;
4762 xcb_font_t cursor_font;
4763 xcb_cursor_t cursor;
4764 xcb_query_pointer_reply_t *qpr;
4765 xcb_generic_event_t *evt;
4766 xcb_motion_notify_event_t *mne;
4772 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
4775 DNPRINTF(SWM_D_EVENT, "move: window: 0x%x, floating: %s, transient: "
4776 "0x%x\n", win->id, YESNO(win->floating), win->transient);
4778 /* in max_stack mode should only move transients */
4779 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !win->transient)
4783 if (win->floating == 0 && !win->transient) {
4784 store_float_geom(win, r);
4785 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
4788 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
4797 case SWM_ARG_ID_MOVELEFT:
4798 X(win) -= (SWM_MOVE_STEPS - border_width);
4801 case SWM_ARG_ID_MOVERIGHT:
4802 X(win) += (SWM_MOVE_STEPS - border_width);
4805 case SWM_ARG_ID_MOVEUP:
4806 Y(win) -= (SWM_MOVE_STEPS - border_width);
4809 case SWM_ARG_ID_MOVEDOWN:
4810 Y(win) += (SWM_MOVE_STEPS - border_width);
4817 constrain_window(win, r, 0);
4819 store_float_geom(win, r);
4823 cursor_font = xcb_generate_id(conn);
4824 xcb_open_font(conn, cursor_font, strlen("cursor"), "cursor");
4826 cursor = xcb_generate_id(conn);
4827 xcb_create_glyph_cursor(conn, cursor, cursor_font, cursor_font,
4828 XC_fleur, XC_fleur + 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
4830 xcb_grab_pointer(conn, 0, win->id, MOUSEMASK,
4831 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC,
4832 XCB_WINDOW_NONE, cursor, XCB_CURRENT_TIME);
4834 /* get cursor offset from window root */
4835 qpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, win->id),
4838 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
4839 xcb_free_cursor(conn, cursor);
4840 xcb_close_font(conn, cursor_font);
4846 while ((evt = xcb_wait_for_event(conn)) && moving) {
4847 switch (XCB_EVENT_RESPONSE_TYPE(evt)) {
4848 case XCB_BUTTON_RELEASE:
4849 DNPRINTF(SWM_D_EVENT, "move: BUTTON_RELEASE\n");
4852 case XCB_MOTION_NOTIFY:
4853 mne = (xcb_motion_notify_event_t *)evt;
4854 X(win) = mne->root_x - qpr->win_x - border_width;
4855 Y(win) = mne->root_y - qpr->win_y - border_width;
4857 constrain_window(win, r, 0);
4859 /* not free, don't sync more than 120 times / second */
4860 if ((mne->time - timestamp) > (1000 / 120) ) {
4861 timestamp = mne->time;
4876 store_float_geom(win, r);
4878 xcb_free_cursor(conn, cursor);
4879 xcb_close_font(conn, cursor_font);
4880 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
4881 DNPRINTF(SWM_D_EVENT, "move: done.\n");
4885 move_step(struct swm_region *r, union arg *args)
4887 struct ws_win *win = NULL;
4889 if (r && r->ws && r->ws->focus)
4894 if (!(win->transient != 0 || win->floating != 0))
4901 /* user/key callable function IDs */
4951 KF_SEARCH_WORKSPACE,
4992 KF_DUMPWINS, /* MUST BE LAST */
4996 /* key definitions */
4998 char name[SWM_FUNCNAME_LEN];
4999 void (*func)(struct swm_region *r, union arg *);
5001 } keyfuncs[KF_INVALID + 1] = {
5002 /* name function argument */
5003 { "bar_toggle", bar_toggle, {0} },
5004 { "button2", pressbutton, {2} },
5005 { "cycle_layout", cycle_layout, {0} },
5006 { "flip_layout", stack_config, {.id = SWM_ARG_ID_FLIPLAYOUT} },
5007 { "float_toggle", floating_toggle,{0} },
5008 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
5009 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
5010 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
5011 { "height_grow", resize_step, {.id = SWM_ARG_ID_HEIGHTGROW} },
5012 { "height_shrink", resize_step, {.id = SWM_ARG_ID_HEIGHTSHRINK} },
5013 { "iconify", iconify, {0} },
5014 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
5015 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
5016 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
5017 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
5018 { "move_down", move_step, {.id = SWM_ARG_ID_MOVEDOWN} },
5019 { "move_left", move_step, {.id = SWM_ARG_ID_MOVELEFT} },
5020 { "move_right", move_step, {.id = SWM_ARG_ID_MOVERIGHT} },
5021 { "move_up", move_step, {.id = SWM_ARG_ID_MOVEUP} },
5022 { "mvws_1", send_to_ws, {.id = 0} },
5023 { "mvws_2", send_to_ws, {.id = 1} },
5024 { "mvws_3", send_to_ws, {.id = 2} },
5025 { "mvws_4", send_to_ws, {.id = 3} },
5026 { "mvws_5", send_to_ws, {.id = 4} },
5027 { "mvws_6", send_to_ws, {.id = 5} },
5028 { "mvws_7", send_to_ws, {.id = 6} },
5029 { "mvws_8", send_to_ws, {.id = 7} },
5030 { "mvws_9", send_to_ws, {.id = 8} },
5031 { "mvws_10", send_to_ws, {.id = 9} },
5032 { "mvws_11", send_to_ws, {.id = 10} },
5033 { "mvws_12", send_to_ws, {.id = 11} },
5034 { "mvws_13", send_to_ws, {.id = 12} },
5035 { "mvws_14", send_to_ws, {.id = 13} },
5036 { "mvws_15", send_to_ws, {.id = 14} },
5037 { "mvws_16", send_to_ws, {.id = 15} },
5038 { "mvws_17", send_to_ws, {.id = 16} },
5039 { "mvws_18", send_to_ws, {.id = 17} },
5040 { "mvws_19", send_to_ws, {.id = 18} },
5041 { "mvws_20", send_to_ws, {.id = 19} },
5042 { "mvws_21", send_to_ws, {.id = 20} },
5043 { "mvws_22", send_to_ws, {.id = 21} },
5044 { "name_workspace", name_workspace, {0} },
5045 { "quit", quit, {0} },
5046 { "raise_toggle", raise_toggle, {0} },
5047 { "restart", restart, {0} },
5048 { "screen_next", cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
5049 { "screen_prev", cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
5050 { "search_win", search_win, {0} },
5051 { "search_workspace", search_workspace, {0} },
5052 { "spawn_custom", NULL, {0} },
5053 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
5054 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
5055 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
5056 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
5057 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
5058 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
5059 { "uniconify", uniconify, {0} },
5060 { "version", version, {0} },
5061 { "width_grow", resize_step, {.id = SWM_ARG_ID_WIDTHGROW} },
5062 { "width_shrink", resize_step, {.id = SWM_ARG_ID_WIDTHSHRINK} },
5063 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
5064 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
5065 { "ws_1", switchws, {.id = 0} },
5066 { "ws_2", switchws, {.id = 1} },
5067 { "ws_3", switchws, {.id = 2} },
5068 { "ws_4", switchws, {.id = 3} },
5069 { "ws_5", switchws, {.id = 4} },
5070 { "ws_6", switchws, {.id = 5} },
5071 { "ws_7", switchws, {.id = 6} },
5072 { "ws_8", switchws, {.id = 7} },
5073 { "ws_9", switchws, {.id = 8} },
5074 { "ws_10", switchws, {.id = 9} },
5075 { "ws_11", switchws, {.id = 10} },
5076 { "ws_12", switchws, {.id = 11} },
5077 { "ws_13", switchws, {.id = 12} },
5078 { "ws_14", switchws, {.id = 13} },
5079 { "ws_15", switchws, {.id = 14} },
5080 { "ws_16", switchws, {.id = 15} },
5081 { "ws_17", switchws, {.id = 16} },
5082 { "ws_18", switchws, {.id = 17} },
5083 { "ws_19", switchws, {.id = 18} },
5084 { "ws_20", switchws, {.id = 19} },
5085 { "ws_21", switchws, {.id = 20} },
5086 { "ws_22", switchws, {.id = 21} },
5087 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
5088 { "ws_next_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP_ALL} },
5089 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
5090 { "ws_prev_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN_ALL} },
5091 { "ws_prior", priorws, {0} },
5092 { "dumpwins", dumpwins, {0} }, /* MUST BE LAST */
5093 { "invalid key func", NULL, {0} },
5096 RB_ENTRY(key) entry;
5099 enum keyfuncid funcid;
5102 RB_HEAD(key_tree, key);
5105 key_cmp(struct key *kp1, struct key *kp2)
5107 if (kp1->keysym < kp2->keysym)
5109 if (kp1->keysym > kp2->keysym)
5112 if (kp1->mod < kp2->mod)
5114 if (kp1->mod > kp2->mod)
5120 RB_GENERATE(key_tree, key, entry, key_cmp);
5121 struct key_tree keys;
5124 enum { client_click, root_click };
5126 unsigned int action;
5128 unsigned int button;
5129 void (*func)(struct ws_win *, union arg *);
5132 #define MODKEY_SHIFT MODKEY | XCB_MOD_MASK_SHIFT
5133 /* action key mouse button func args */
5134 { client_click, MODKEY, XCB_BUTTON_INDEX_3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
5135 { client_click, MODKEY_SHIFT, XCB_BUTTON_INDEX_3, resize, {.id = SWM_ARG_ID_CENTER} },
5136 { client_click, MODKEY, XCB_BUTTON_INDEX_1, move, {0} },
5141 update_modkey(unsigned int mod)
5147 RB_FOREACH(kp, key_tree, &keys)
5148 if (kp->mod & XCB_MOD_MASK_SHIFT)
5149 kp->mod = mod | XCB_MOD_MASK_SHIFT;
5153 for (i = 0; i < LENGTH(buttons); i++)
5154 if (buttons[i].mask & XCB_MOD_MASK_SHIFT)
5155 buttons[i].mask = mod | XCB_MOD_MASK_SHIFT;
5157 buttons[i].mask = mod;
5162 TAILQ_ENTRY(spawn_prog) entry;
5167 TAILQ_HEAD(spawn_list, spawn_prog);
5168 struct spawn_list spawns = TAILQ_HEAD_INITIALIZER(spawns);
5171 spawn_expand(struct swm_region *r, union arg *args, char *spawn_name,
5174 struct spawn_prog *prog = NULL;
5176 char *ap, **real_args;
5178 /* suppress unused warning since var is needed */
5181 DNPRINTF(SWM_D_SPAWN, "spawn_expand: %s\n", spawn_name);
5184 TAILQ_FOREACH(prog, &spawns, entry) {
5185 if (!strcasecmp(spawn_name, prog->name))
5189 warnx("spawn_custom: program %s not found", spawn_name);
5193 /* make room for expanded args */
5194 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
5195 err(1, "spawn_custom: calloc real_args");
5197 /* expand spawn_args into real_args */
5198 for (i = 0; i < prog->argc; i++) {
5200 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg: %s\n", ap);
5201 if (!strcasecmp(ap, "$bar_border")) {
5203 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
5205 err(1, "spawn_custom border color");
5206 } else if (!strcasecmp(ap, "$bar_color")) {
5208 strdup(r->s->c[SWM_S_COLOR_BAR].name))
5210 err(1, "spawn_custom bar color");
5211 } else if (!strcasecmp(ap, "$bar_font")) {
5212 if ((real_args[i] = strdup(bar_fonts))
5214 err(1, "spawn_custom bar fonts");
5215 } else if (!strcasecmp(ap, "$bar_font_color")) {
5217 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
5219 err(1, "spawn_custom color font");
5220 } else if (!strcasecmp(ap, "$color_focus")) {
5222 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
5224 err(1, "spawn_custom color focus");
5225 } else if (!strcasecmp(ap, "$color_unfocus")) {
5227 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
5229 err(1, "spawn_custom color unfocus");
5231 /* no match --> copy as is */
5232 if ((real_args[i] = strdup(ap)) == NULL)
5233 err(1, "spawn_custom strdup(ap)");
5235 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg: %s\n",
5240 DNPRINTF(SWM_D_SPAWN, "spawn_custom: result: ");
5241 for (i = 0; i < prog->argc; i++)
5242 DNPRINTF(SWM_D_SPAWN, "\"%s\" ", real_args[i]);
5243 DNPRINTF(SWM_D_SPAWN, "\n");
5245 *ret_args = real_args;
5246 return (prog->argc);
5250 spawn_custom(struct swm_region *r, union arg *args, char *spawn_name)
5256 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
5260 spawn(r->ws->idx, &a, 1);
5262 for (i = 0; i < spawn_argc; i++)
5268 spawn_select(struct swm_region *r, union arg *args, char *spawn_name, int *pid)
5274 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
5278 if (pipe(select_list_pipe) == -1)
5279 err(1, "pipe error");
5280 if (pipe(select_resp_pipe) == -1)
5281 err(1, "pipe error");
5283 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
5284 err(1, "could not disable SIGPIPE");
5285 switch (*pid = fork()) {
5287 err(1, "cannot fork");
5290 if (dup2(select_list_pipe[0], 0) == -1)
5292 if (dup2(select_resp_pipe[1], 1) == -1)
5294 close(select_list_pipe[1]);
5295 close(select_resp_pipe[0]);
5296 spawn(r->ws->idx, &a, 0);
5298 default: /* parent */
5299 close(select_list_pipe[0]);
5300 close(select_resp_pipe[1]);
5304 for (i = 0; i < spawn_argc; i++)
5310 spawn_insert(char *name, char *args)
5312 char *arg, *cp, *ptr;
5313 struct spawn_prog *sp;
5315 DNPRINTF(SWM_D_SPAWN, "spawn_insert: %s\n", name);
5317 if ((sp = calloc(1, sizeof *sp)) == NULL)
5318 err(1, "spawn_insert: calloc");
5319 if ((sp->name = strdup(name)) == NULL)
5320 err(1, "spawn_insert: strdup");
5322 /* convert the arguments to an argument list */
5323 if ((ptr = cp = strdup(args)) == NULL)
5324 err(1, "spawn_insert: strdup");
5325 while ((arg = strsep(&ptr, " \t")) != NULL) {
5326 /* empty field; skip it */
5331 if ((sp->argv = realloc(sp->argv, sp->argc *
5332 sizeof *sp->argv)) == NULL)
5333 err(1, "spawn_insert: realloc");
5334 if ((sp->argv[sp->argc - 1] = strdup(arg)) == NULL)
5335 err(1, "spawn_insert: strdup");
5339 TAILQ_INSERT_TAIL(&spawns, sp, entry);
5340 DNPRINTF(SWM_D_SPAWN, "spawn_insert: leave\n");
5344 spawn_remove(struct spawn_prog *sp)
5348 DNPRINTF(SWM_D_SPAWN, "spawn_remove: %s\n", sp->name);
5350 TAILQ_REMOVE(&spawns, sp, entry);
5351 for (i = 0; i < sp->argc; i++)
5357 DNPRINTF(SWM_D_SPAWN, "spawn_remove: leave\n");
5361 spawn_replace(struct spawn_prog *sp, char *name, char *args)
5363 DNPRINTF(SWM_D_SPAWN, "spawn_replace: %s [%s]\n", sp->name, name);
5366 spawn_insert(name, args);
5368 DNPRINTF(SWM_D_SPAWN, "spawn_replace: leave\n");
5372 setspawn(char *name, char *args)
5374 struct spawn_prog *sp;
5376 DNPRINTF(SWM_D_SPAWN, "setspawn: %s\n", name);
5381 TAILQ_FOREACH(sp, &spawns, entry) {
5382 if (!strcmp(sp->name, name)) {
5386 spawn_replace(sp, name, args);
5387 DNPRINTF(SWM_D_SPAWN, "setspawn: leave\n");
5391 if (*args == '\0') {
5392 warnx("error: setspawn: cannot find program: %s", name);
5396 spawn_insert(name, args);
5397 DNPRINTF(SWM_D_SPAWN, "setspawn: leave\n");
5401 setconfspawn(char *selector, char *value, int flags)
5405 /* suppress unused warning since var is needed */
5408 args = expand_tilde(value);
5410 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, args);
5412 setspawn(selector, args);
5415 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done.\n");
5422 setconfspawn("term", "xterm", 0);
5423 setconfspawn("spawn_term", "xterm", 0);
5424 setconfspawn("screenshot_all", "screenshot.sh full", 0);
5425 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
5426 setconfspawn("lock", "xlock", 0);
5427 setconfspawn("initscr", "initscreen.sh", 0);
5428 setconfspawn("menu", "dmenu_run"
5431 " -nf $bar_font_color"
5433 " -sf $bar_color", 0);
5434 setconfspawn("search", "dmenu"
5438 " -nf $bar_font_color"
5440 " -sf $bar_color", 0);
5441 setconfspawn("name_workspace", "dmenu"
5445 " -nf $bar_font_color"
5447 " -sf $bar_color", 0);
5451 #define SWM_MODNAME_SIZE 32
5452 #define SWM_KEY_WS "\n+ \t"
5454 parsekeys(char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
5458 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
5459 if (mod == NULL || ks == NULL) {
5460 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
5463 if (keystr == NULL || strlen(keystr) == 0) {
5464 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
5468 *ks = XCB_NO_SYMBOL;
5470 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
5471 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
5473 cp += (long)strspn(cp, SWM_KEY_WS);
5474 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
5476 else if (!strncasecmp(name, "Mod1", SWM_MODNAME_SIZE))
5477 *mod |= XCB_MOD_MASK_1;
5478 else if (!strncasecmp(name, "Mod2", SWM_MODNAME_SIZE))
5479 *mod += XCB_MOD_MASK_2;
5480 else if (!strncmp(name, "Mod3", SWM_MODNAME_SIZE))
5481 *mod |= XCB_MOD_MASK_3;
5482 else if (!strncmp(name, "Mod4", SWM_MODNAME_SIZE))
5483 *mod |= XCB_MOD_MASK_4;
5484 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
5485 *mod |= XCB_MOD_MASK_SHIFT;
5486 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
5487 *mod |= XCB_MOD_MASK_CONTROL;
5489 *ks = XStringToKeysym(name);
5490 XConvertCase(*ks, ks, &uks);
5491 if (ks == XCB_NO_SYMBOL) {
5493 "parsekeys: invalid key %s\n",
5499 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
5504 strdupsafe(char *str)
5509 return (strdup(str));
5513 key_insert(unsigned int mod, KeySym ks, enum keyfuncid kfid, char *spawn_name)
5517 DNPRINTF(SWM_D_KEY, "key_insert: enter %s [%s]\n",
5518 keyfuncs[kfid].name, spawn_name);
5520 if ((kp = malloc(sizeof *kp)) == NULL)
5521 err(1, "key_insert: malloc");
5526 kp->spawn_name = strdupsafe(spawn_name);
5527 RB_INSERT(key_tree, &keys, kp);
5529 DNPRINTF(SWM_D_KEY, "key_insert: leave\n");
5533 key_lookup(unsigned int mod, KeySym ks)
5540 return (RB_FIND(key_tree, &keys, &kp));
5544 key_remove(struct key *kp)
5546 DNPRINTF(SWM_D_KEY, "key_remove: %s\n", keyfuncs[kp->funcid].name);
5548 RB_REMOVE(key_tree, &keys, kp);
5549 free(kp->spawn_name);
5552 DNPRINTF(SWM_D_KEY, "key_remove: leave\n");
5556 key_replace(struct key *kp, unsigned int mod, KeySym ks, enum keyfuncid kfid,
5559 DNPRINTF(SWM_D_KEY, "key_replace: %s [%s]\n", keyfuncs[kp->funcid].name,
5563 key_insert(mod, ks, kfid, spawn_name);
5565 DNPRINTF(SWM_D_KEY, "key_replace: leave\n");
5569 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid,
5574 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
5575 keyfuncs[kfid].name, spawn_name);
5577 if ((kp = key_lookup(mod, ks)) != NULL) {
5578 if (kfid == KF_INVALID)
5581 key_replace(kp, mod, ks, kfid, spawn_name);
5582 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
5585 if (kfid == KF_INVALID) {
5586 warnx("error: setkeybinding: cannot find mod/key combination");
5587 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
5591 key_insert(mod, ks, kfid, spawn_name);
5592 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
5596 setconfbinding(char *selector, char *value, int flags)
5598 enum keyfuncid kfid;
5601 struct spawn_prog *sp;
5603 /* suppress unused warning since var is needed */
5606 DNPRINTF(SWM_D_KEY, "setconfbinding: enter\n");
5607 if (selector == NULL) {
5608 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
5609 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
5611 setkeybinding(mod, ks, kfid, NULL);
5616 /* search by key function name */
5617 for (kfid = 0; kfid < KF_INVALID; (kfid)++) {
5618 if (strncasecmp(selector, keyfuncs[kfid].name,
5619 SWM_FUNCNAME_LEN) == 0) {
5620 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
5622 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
5623 setkeybinding(mod, ks, kfid, NULL);
5629 /* search by custom spawn name */
5630 TAILQ_FOREACH(sp, &spawns, entry) {
5631 if (strcasecmp(selector, sp->name) == 0) {
5632 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
5634 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
5635 setkeybinding(mod, ks, KF_SPAWN_CUSTOM,
5642 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
5649 #define MODKEY_SHIFT MODKEY | XCB_MOD_MASK_SHIFT
5650 setkeybinding(MODKEY, XK_space, KF_CYCLE_LAYOUT,NULL);
5651 setkeybinding(MODKEY_SHIFT, XK_backslash, KF_FLIP_LAYOUT, NULL);
5652 setkeybinding(MODKEY_SHIFT, XK_space, KF_STACK_RESET, NULL);
5653 setkeybinding(MODKEY, XK_h, KF_MASTER_SHRINK, NULL);
5654 setkeybinding(MODKEY, XK_l, KF_MASTER_GROW, NULL);
5655 setkeybinding(MODKEY, XK_comma, KF_MASTER_ADD, NULL);
5656 setkeybinding(MODKEY, XK_period, KF_MASTER_DEL, NULL);
5657 setkeybinding(MODKEY_SHIFT, XK_comma, KF_STACK_INC, NULL);
5658 setkeybinding(MODKEY_SHIFT, XK_period, KF_STACK_DEC, NULL);
5659 setkeybinding(MODKEY, XK_Return, KF_SWAP_MAIN, NULL);
5660 setkeybinding(MODKEY, XK_j, KF_FOCUS_NEXT, NULL);
5661 setkeybinding(MODKEY, XK_k, KF_FOCUS_PREV, NULL);
5662 setkeybinding(MODKEY_SHIFT, XK_j, KF_SWAP_NEXT, NULL);
5663 setkeybinding(MODKEY_SHIFT, XK_k, KF_SWAP_PREV, NULL);
5664 setkeybinding(MODKEY_SHIFT, XK_Return, KF_SPAWN_CUSTOM,"term");
5665 setkeybinding(MODKEY, XK_p, KF_SPAWN_CUSTOM,"menu");
5666 setkeybinding(MODKEY_SHIFT, XK_q, KF_QUIT, NULL);
5667 setkeybinding(MODKEY, XK_q, KF_RESTART, NULL);
5668 setkeybinding(MODKEY, XK_m, KF_FOCUS_MAIN, NULL);
5669 setkeybinding(MODKEY, XK_1, KF_WS_1, NULL);
5670 setkeybinding(MODKEY, XK_2, KF_WS_2, NULL);
5671 setkeybinding(MODKEY, XK_3, KF_WS_3, NULL);
5672 setkeybinding(MODKEY, XK_4, KF_WS_4, NULL);
5673 setkeybinding(MODKEY, XK_5, KF_WS_5, NULL);
5674 setkeybinding(MODKEY, XK_6, KF_WS_6, NULL);
5675 setkeybinding(MODKEY, XK_7, KF_WS_7, NULL);
5676 setkeybinding(MODKEY, XK_8, KF_WS_8, NULL);
5677 setkeybinding(MODKEY, XK_9, KF_WS_9, NULL);
5678 setkeybinding(MODKEY, XK_0, KF_WS_10, NULL);
5679 setkeybinding(MODKEY, XK_F1, KF_WS_11, NULL);
5680 setkeybinding(MODKEY, XK_F2, KF_WS_12, NULL);
5681 setkeybinding(MODKEY, XK_F3, KF_WS_13, NULL);
5682 setkeybinding(MODKEY, XK_F4, KF_WS_14, NULL);
5683 setkeybinding(MODKEY, XK_F5, KF_WS_15, NULL);
5684 setkeybinding(MODKEY, XK_F6, KF_WS_16, NULL);
5685 setkeybinding(MODKEY, XK_F7, KF_WS_17, NULL);
5686 setkeybinding(MODKEY, XK_F8, KF_WS_18, NULL);
5687 setkeybinding(MODKEY, XK_F9, KF_WS_19, NULL);
5688 setkeybinding(MODKEY, XK_F10, KF_WS_20, NULL);
5689 setkeybinding(MODKEY, XK_F11, KF_WS_21, NULL);
5690 setkeybinding(MODKEY, XK_F12, KF_WS_22, NULL);
5691 setkeybinding(MODKEY, XK_Right, KF_WS_NEXT, NULL);
5692 setkeybinding(MODKEY, XK_Left, KF_WS_PREV, NULL);
5693 setkeybinding(MODKEY, XK_Up, KF_WS_NEXT_ALL, NULL);
5694 setkeybinding(MODKEY, XK_Down, KF_WS_PREV_ALL, NULL);
5695 setkeybinding(MODKEY, XK_a, KF_WS_PRIOR, NULL);
5696 setkeybinding(MODKEY_SHIFT, XK_Right, KF_SCREEN_NEXT, NULL);
5697 setkeybinding(MODKEY_SHIFT, XK_Left, KF_SCREEN_PREV, NULL);
5698 setkeybinding(MODKEY_SHIFT, XK_1, KF_MVWS_1, NULL);
5699 setkeybinding(MODKEY_SHIFT, XK_2, KF_MVWS_2, NULL);
5700 setkeybinding(MODKEY_SHIFT, XK_3, KF_MVWS_3, NULL);
5701 setkeybinding(MODKEY_SHIFT, XK_4, KF_MVWS_4, NULL);
5702 setkeybinding(MODKEY_SHIFT, XK_5, KF_MVWS_5, NULL);
5703 setkeybinding(MODKEY_SHIFT, XK_6, KF_MVWS_6, NULL);
5704 setkeybinding(MODKEY_SHIFT, XK_7, KF_MVWS_7, NULL);
5705 setkeybinding(MODKEY_SHIFT, XK_8, KF_MVWS_8, NULL);
5706 setkeybinding(MODKEY_SHIFT, XK_9, KF_MVWS_9, NULL);
5707 setkeybinding(MODKEY_SHIFT, XK_0, KF_MVWS_10, NULL);
5708 setkeybinding(MODKEY_SHIFT, XK_F1, KF_MVWS_11, NULL);
5709 setkeybinding(MODKEY_SHIFT, XK_F2, KF_MVWS_12, NULL);
5710 setkeybinding(MODKEY_SHIFT, XK_F3, KF_MVWS_13, NULL);
5711 setkeybinding(MODKEY_SHIFT, XK_F4, KF_MVWS_14, NULL);
5712 setkeybinding(MODKEY_SHIFT, XK_F5, KF_MVWS_15, NULL);
5713 setkeybinding(MODKEY_SHIFT, XK_F6, KF_MVWS_16, NULL);
5714 setkeybinding(MODKEY_SHIFT, XK_F7, KF_MVWS_17, NULL);
5715 setkeybinding(MODKEY_SHIFT, XK_F8, KF_MVWS_18, NULL);
5716 setkeybinding(MODKEY_SHIFT, XK_F9, KF_MVWS_19, NULL);
5717 setkeybinding(MODKEY_SHIFT, XK_F10, KF_MVWS_20, NULL);
5718 setkeybinding(MODKEY_SHIFT, XK_F11, KF_MVWS_21, NULL);
5719 setkeybinding(MODKEY_SHIFT, XK_F12, KF_MVWS_22, NULL);
5720 setkeybinding(MODKEY, XK_b, KF_BAR_TOGGLE, NULL);
5721 setkeybinding(MODKEY, XK_Tab, KF_FOCUS_NEXT, NULL);
5722 setkeybinding(MODKEY_SHIFT, XK_Tab, KF_FOCUS_PREV, NULL);
5723 setkeybinding(MODKEY_SHIFT, XK_x, KF_WIND_KILL, NULL);
5724 setkeybinding(MODKEY, XK_x, KF_WIND_DEL, NULL);
5725 setkeybinding(MODKEY, XK_s, KF_SPAWN_CUSTOM,"screenshot_all");
5726 setkeybinding(MODKEY_SHIFT, XK_s, KF_SPAWN_CUSTOM,"screenshot_wind");
5727 setkeybinding(MODKEY, XK_t, KF_FLOAT_TOGGLE,NULL);
5728 setkeybinding(MODKEY_SHIFT, XK_v, KF_VERSION, NULL);
5729 setkeybinding(MODKEY_SHIFT, XK_Delete, KF_SPAWN_CUSTOM,"lock");
5730 setkeybinding(MODKEY_SHIFT, XK_i, KF_SPAWN_CUSTOM,"initscr");
5731 setkeybinding(MODKEY, XK_w, KF_ICONIFY, NULL);
5732 setkeybinding(MODKEY_SHIFT, XK_w, KF_UNICONIFY, NULL);
5733 setkeybinding(MODKEY_SHIFT, XK_r, KF_RAISE_TOGGLE,NULL);
5734 setkeybinding(MODKEY, XK_v, KF_BUTTON2, NULL);
5735 setkeybinding(MODKEY, XK_equal, KF_WIDTH_GROW, NULL);
5736 setkeybinding(MODKEY, XK_minus, KF_WIDTH_SHRINK,NULL);
5737 setkeybinding(MODKEY_SHIFT, XK_equal, KF_HEIGHT_GROW,NULL);
5738 setkeybinding(MODKEY_SHIFT, XK_minus, KF_HEIGHT_SHRINK,NULL);
5739 setkeybinding(MODKEY, XK_bracketleft, KF_MOVE_LEFT,NULL);
5740 setkeybinding(MODKEY, XK_bracketright,KF_MOVE_RIGHT,NULL);
5741 setkeybinding(MODKEY_SHIFT, XK_bracketleft, KF_MOVE_UP, NULL);
5742 setkeybinding(MODKEY_SHIFT, XK_bracketright,KF_MOVE_DOWN,NULL);
5743 setkeybinding(MODKEY_SHIFT, XK_slash, KF_NAME_WORKSPACE,NULL);
5744 setkeybinding(MODKEY, XK_slash, KF_SEARCH_WORKSPACE,NULL);
5745 setkeybinding(MODKEY, XK_f, KF_SEARCH_WIN, NULL);
5747 setkeybinding(MODKEY_SHIFT, XK_d, KF_DUMPWINS, NULL);
5757 while (RB_EMPTY(&keys) == 0) {
5758 kp = RB_ROOT(&keys);
5764 setkeymapping(char *selector, char *value, int flags)
5766 char keymapping_file[PATH_MAX];
5768 /* suppress unused warnings since vars are needed */
5772 DNPRINTF(SWM_D_KEY, "setkeymapping: enter\n");
5773 if (value[0] == '~')
5774 snprintf(keymapping_file, sizeof keymapping_file, "%s/%s",
5775 pwd->pw_dir, &value[1]);
5777 strlcpy(keymapping_file, value, sizeof keymapping_file);
5779 /* load new key bindings; if it fails, revert to default bindings */
5780 if (conf_load(keymapping_file, SWM_CONF_KEYMAPPING)) {
5784 DNPRINTF(SWM_D_KEY, "setkeymapping: leave\n");
5789 updatenumlockmask(void)
5792 xcb_get_modifier_mapping_reply_t *modmap_r;
5793 xcb_keycode_t *modmap, kc, *keycode;
5797 modmap_r = xcb_get_modifier_mapping_reply(conn,
5798 xcb_get_modifier_mapping(conn),
5801 modmap = xcb_get_modifier_mapping_keycodes(modmap_r);
5802 for (i = 0; i < 8; i++) {
5803 for (j = 0; j < modmap_r->keycodes_per_modifier; j++) {
5804 kc = modmap[i * modmap_r->keycodes_per_modifier
5806 keycode = xcb_key_symbols_get_keycode(syms,
5809 numlockmask = (1 << i);
5815 DNPRINTF(SWM_D_MISC, "updatenumlockmask: %d\n", numlockmask);
5822 int num_screens, k, j;
5823 unsigned int modifiers[3];
5824 xcb_keycode_t *code;
5826 DNPRINTF(SWM_D_MISC, "grabkeys\n");
5827 updatenumlockmask();
5830 modifiers[1] = numlockmask;
5831 modifiers[2] = numlockmask | XCB_MOD_MASK_LOCK;
5833 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
5834 for (k = 0; k < num_screens; k++) {
5835 if (TAILQ_EMPTY(&screens[k].rl))
5837 xcb_ungrab_key(conn, XCB_GRAB_ANY, screens[k].root,
5839 RB_FOREACH(kp, key_tree, &keys) {
5840 if ((code = xcb_key_symbols_get_keycode(syms,
5842 for (j = 0; j < LENGTH(modifiers); j++)
5843 xcb_grab_key(conn, 1,
5845 kp->mod | modifiers[j],
5846 *code, XCB_GRAB_MODE_SYNC,
5847 XCB_GRAB_MODE_ASYNC);
5855 grabbuttons(struct ws_win *win)
5859 DNPRINTF(SWM_D_MOUSE, "grabbuttons: win 0x%x\n", win->id);
5861 for (i = 0; i < LENGTH(buttons); i++)
5862 if (buttons[i].action == client_click)
5863 xcb_grab_button(conn, 0, win->id, BUTTONMASK,
5864 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC,
5865 XCB_WINDOW_NONE, XCB_CURSOR_NONE,
5866 buttons[i].button, buttons[i].mask);
5868 /* click to focus */
5869 xcb_grab_button(conn, 0, win->id, BUTTONMASK, XCB_GRAB_MODE_SYNC,
5870 XCB_GRAB_MODE_ASYNC, XCB_WINDOW_NONE, XCB_CURSOR_NONE,
5871 XCB_BUTTON_INDEX_1, XCB_BUTTON_MASK_ANY);
5874 const char *quirkname[] = {
5875 "NONE", /* config string for "no value" */
5884 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
5885 #define SWM_Q_WS "\n|+ \t"
5887 parsequirks(char *qstr, unsigned long *quirk)
5897 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
5899 cp += (long)strspn(cp, SWM_Q_WS);
5900 for (i = 0; i < LENGTH(quirkname); i++) {
5901 if (!strncasecmp(name, quirkname[i], SWM_QUIRK_LEN)) {
5902 DNPRINTF(SWM_D_QUIRK,
5903 "parsequirks: %s\n", name);
5908 *quirk |= 1 << (i-1);
5912 if (i >= LENGTH(quirkname)) {
5913 DNPRINTF(SWM_D_QUIRK,
5914 "parsequirks: invalid quirk [%s]\n", name);
5922 quirk_insert(const char *class, const char *name, unsigned long quirk)
5926 DNPRINTF(SWM_D_QUIRK, "quirk_insert: %s:%s [%lu]\n", class, name,
5929 if ((qp = malloc(sizeof *qp)) == NULL)
5930 err(1, "quirk_insert: malloc");
5931 if ((qp->class = strdup(class)) == NULL)
5932 err(1, "quirk_insert: strdup");
5933 if ((qp->name = strdup(name)) == NULL)
5934 err(1, "quirk_insert: strdup");
5937 TAILQ_INSERT_TAIL(&quirks, qp, entry);
5939 DNPRINTF(SWM_D_QUIRK, "quirk_insert: leave\n");
5943 quirk_remove(struct quirk *qp)
5945 DNPRINTF(SWM_D_QUIRK, "quirk_remove: %s:%s [%lu]\n", qp->class,
5946 qp->name, qp->quirk);
5948 TAILQ_REMOVE(&quirks, qp, entry);
5953 DNPRINTF(SWM_D_QUIRK, "quirk_remove: leave\n");
5957 quirk_replace(struct quirk *qp, const char *class, const char *name,
5958 unsigned long quirk)
5960 DNPRINTF(SWM_D_QUIRK, "quirk_replace: %s:%s [%lu]\n", qp->class,
5961 qp->name, qp->quirk);
5964 quirk_insert(class, name, quirk);
5966 DNPRINTF(SWM_D_QUIRK, "quirk_replace: leave\n");
5970 setquirk(const char *class, const char *name, unsigned long quirk)
5974 DNPRINTF(SWM_D_QUIRK, "setquirk: enter %s:%s [%lu]\n", class, name,
5977 TAILQ_FOREACH(qp, &quirks, entry) {
5978 if (!strcmp(qp->class, class) && !strcmp(qp->name, name)) {
5982 quirk_replace(qp, class, name, quirk);
5983 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
5988 warnx("error: setquirk: cannot find class/name combination");
5992 quirk_insert(class, name, quirk);
5993 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
5997 setconfquirk(char *selector, char *value, int flags)
5999 char *cp, *class, *name;
6003 /* suppress unused warning since var is needed */
6006 if (selector == NULL)
6008 if ((cp = strchr(selector, ':')) == NULL)
6013 if ((retval = parsequirks(value, &qrks)) == 0)
6014 setquirk(class, name, qrks);
6021 setquirk("MPlayer", "xv", SWM_Q_FLOAT | SWM_Q_FULLSCREEN | SWM_Q_FOCUSPREV);
6022 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT);
6023 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ);
6024 setquirk("Firefox", "Dialog", SWM_Q_FLOAT);
6025 setquirk("Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
6026 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ);
6027 setquirk("xine", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
6028 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
6029 setquirk("xine", "xine Panel", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
6030 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
6031 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
6032 setquirk("pcb", "pcb", SWM_Q_FLOAT);
6033 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
6036 /* conf file stuff */
6037 #define SWM_CONF_FILE "spectrwm.conf"
6038 #define SWM_CONF_FILE_OLD "scrotwm.conf"
6042 SWM_S_BAR_AT_BOTTOM,
6043 SWM_S_BAR_BORDER_WIDTH,
6050 SWM_S_CLOCK_ENABLED,
6053 SWM_S_CYCLE_VISIBLE,
6055 SWM_S_DISABLE_BORDER,
6057 SWM_S_FOCUS_CLOSE_WRAP,
6058 SWM_S_FOCUS_DEFAULT,
6064 SWM_S_STACK_ENABLED,
6066 SWM_S_TITLE_CLASS_ENABLED,
6067 SWM_S_TITLE_NAME_ENABLED,
6068 SWM_S_URGENT_ENABLED,
6069 SWM_S_VERBOSE_LAYOUT,
6070 SWM_S_WINDOW_NAME_ENABLED,
6071 SWM_S_WORKSPACE_LIMIT
6075 setconfvalue(char *selector, char *value, int flags)
6080 /* suppress unused warning since var is needed */
6084 case SWM_S_BAR_ACTION:
6086 if ((bar_argv[0] = expand_tilde(value)) == NULL)
6087 err(1, "setconfvalue: bar_action");
6089 case SWM_S_BAR_AT_BOTTOM:
6090 bar_at_bottom = atoi(value);
6092 case SWM_S_BAR_BORDER_WIDTH:
6093 bar_border_width = atoi(value);
6094 if (bar_border_width < 0)
6095 bar_border_width = 0;
6097 case SWM_S_BAR_DELAY:
6098 bar_delay = atoi(value);
6100 case SWM_S_BAR_ENABLED:
6101 bar_enabled = atoi(value);
6103 case SWM_S_BAR_FONT:
6105 if (asprintf(&bar_fonts, "%s,%s", value, bar_fonts) == -1)
6106 err(1, "setconfvalue: asprintf: failed to allocate "
6107 "memory for bar_fonts.");
6110 /* If already in xft mode, then we are done. */
6111 if (!bar_font_legacy)
6114 /* If there are any non-XLFD entries, switch to Xft mode. */
6115 while ((b = strsep(&value, ",")) != NULL) {
6119 bar_font_legacy = 0;
6124 case SWM_S_BAR_FORMAT:
6126 if ((bar_format = strdup(value)) == NULL)
6127 err(1, "setconfvalue: bar_format");
6129 case SWM_S_BAR_JUSTIFY:
6130 if (!strcmp(value, "left"))
6131 bar_justify = SWM_BAR_JUSTIFY_LEFT;
6132 else if (!strcmp(value, "center"))
6133 bar_justify = SWM_BAR_JUSTIFY_CENTER;
6134 else if (!strcmp(value, "right"))
6135 bar_justify = SWM_BAR_JUSTIFY_RIGHT;
6137 errx(1, "invalid bar_justify");
6139 case SWM_S_BORDER_WIDTH:
6140 border_width = atoi(value);
6141 if (border_width < 0)
6144 case SWM_S_CLOCK_ENABLED:
6145 clock_enabled = atoi(value);
6147 case SWM_S_CLOCK_FORMAT:
6148 #ifndef SWM_DENY_CLOCK_FORMAT
6150 if ((clock_format = strdup(value)) == NULL)
6151 err(1, "setconfvalue: clock_format");
6154 case SWM_S_CYCLE_EMPTY:
6155 cycle_empty = atoi(value);
6157 case SWM_S_CYCLE_VISIBLE:
6158 cycle_visible = atoi(value);
6160 case SWM_S_DIALOG_RATIO:
6161 dialog_ratio = atof(value);
6162 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
6165 case SWM_S_DISABLE_BORDER:
6166 disable_border = atoi(value);
6168 case SWM_S_FOCUS_CLOSE:
6169 if (!strcmp(value, "first"))
6170 focus_close = SWM_STACK_BOTTOM;
6171 else if (!strcmp(value, "last"))
6172 focus_close = SWM_STACK_TOP;
6173 else if (!strcmp(value, "next"))
6174 focus_close = SWM_STACK_ABOVE;
6175 else if (!strcmp(value, "previous"))
6176 focus_close = SWM_STACK_BELOW;
6178 errx(1, "focus_close");
6180 case SWM_S_FOCUS_CLOSE_WRAP:
6181 focus_close_wrap = atoi(value);
6183 case SWM_S_FOCUS_DEFAULT:
6184 if (!strcmp(value, "last"))
6185 focus_default = SWM_STACK_TOP;
6186 else if (!strcmp(value, "first"))
6187 focus_default = SWM_STACK_BOTTOM;
6189 errx(1, "focus_default");
6191 case SWM_S_FOCUS_MODE:
6192 if (!strcmp(value, "default"))
6193 focus_mode = SWM_FOCUS_DEFAULT;
6194 else if (!strcmp(value, "follow_cursor"))
6195 focus_mode = SWM_FOCUS_FOLLOW;
6196 else if (!strcmp(value, "manual"))
6197 focus_mode = SWM_FOCUS_MANUAL;
6199 errx(1, "focus_mode");
6201 case SWM_S_SPAWN_ORDER:
6202 if (!strcmp(value, "first"))
6203 spawn_position = SWM_STACK_BOTTOM;
6204 else if (!strcmp(value, "last"))
6205 spawn_position = SWM_STACK_TOP;
6206 else if (!strcmp(value, "next"))
6207 spawn_position = SWM_STACK_ABOVE;
6208 else if (!strcmp(value, "previous"))
6209 spawn_position = SWM_STACK_BELOW;
6211 errx(1, "spawn_position");
6213 case SWM_S_SPAWN_TERM:
6214 setconfspawn("term", value, 0);
6215 setconfspawn("spawn_term", value, 0);
6219 case SWM_S_SS_ENABLED:
6220 ss_enabled = atoi(value);
6222 case SWM_S_STACK_ENABLED:
6223 stack_enabled = atoi(value);
6225 case SWM_S_TERM_WIDTH:
6226 term_width = atoi(value);
6230 case SWM_S_TITLE_CLASS_ENABLED:
6231 title_class_enabled = atoi(value);
6233 case SWM_S_TITLE_NAME_ENABLED:
6234 title_name_enabled = atoi(value);
6236 case SWM_S_URGENT_ENABLED:
6237 urgent_enabled = atoi(value);
6239 case SWM_S_VERBOSE_LAYOUT:
6240 verbose_layout = atoi(value);
6241 for (i = 0; layouts[i].l_stack != NULL; i++) {
6243 layouts[i].l_string = fancy_stacker;
6245 layouts[i].l_string = plain_stacker;
6248 case SWM_S_WINDOW_NAME_ENABLED:
6249 window_name_enabled = atoi(value);
6251 case SWM_S_WORKSPACE_LIMIT:
6252 workspace_limit = atoi(value);
6253 if (workspace_limit > SWM_WS_MAX)
6254 workspace_limit = SWM_WS_MAX;
6255 else if (workspace_limit < 1)
6256 workspace_limit = 1;
6265 setconfmodkey(char *selector, char *value, int flags)
6267 /* suppress unused warnings since vars are needed */
6271 if (!strncasecmp(value, "Mod1", strlen("Mod1")))
6272 update_modkey(XCB_MOD_MASK_1);
6273 else if (!strncasecmp(value, "Mod2", strlen("Mod2")))
6274 update_modkey(XCB_MOD_MASK_2);
6275 else if (!strncasecmp(value, "Mod3", strlen("Mod3")))
6276 update_modkey(XCB_MOD_MASK_3);
6277 else if (!strncasecmp(value, "Mod4", strlen("Mod4")))
6278 update_modkey(XCB_MOD_MASK_4);
6285 setconfcolor(char *selector, char *value, int flags)
6287 setscreencolor(value, ((selector == NULL)?-1:atoi(selector)), flags);
6292 setconfregion(char *selector, char *value, int flags)
6294 /* suppress unused warnings since vars are needed */
6298 custom_region(value);
6303 setautorun(char *selector, char *value, int flags)
6313 /* suppress unused warnings since vars are needed */
6317 if (getenv("SWM_STARTED"))
6321 if (sscanf(value, "ws[%d]:%1023c", &ws_id, s) != 2)
6322 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
6324 if (ws_id < 0 || ws_id >= workspace_limit)
6325 errx(1, "autorun: invalid workspace %d", ws_id + 1);
6328 * This is a little intricate
6330 * If the pid already exists we simply reuse it because it means it was
6331 * used before AND not claimed by manage_window. We get away with
6332 * altering it in the parent after INSERT because this can not be a race
6335 while ((ap = strsep(&sp, " \t")) != NULL) {
6338 DNPRINTF(SWM_D_SPAWN, "setautorun: arg [%s]\n", ap);
6340 if ((a.argv = realloc(a.argv, argc * sizeof(char *))) == NULL)
6341 err(1, "setautorun: realloc");
6342 a.argv[argc - 1] = ap;
6345 if ((a.argv = realloc(a.argv, (argc + 1) * sizeof(char *))) == NULL)
6346 err(1, "setautorun: realloc");
6347 a.argv[argc] = NULL;
6349 if ((pid = fork()) == 0) {
6350 spawn(ws_id, &a, 1);
6359 p = calloc(1, sizeof *p);
6362 TAILQ_INSERT_TAIL(&pidlist, p, entry);
6372 setlayout(char *selector, char *value, int flags)
6374 int ws_id, i, x, mg, ma, si, ar, f = 0;
6375 int st = SWM_V_STACK, num_screens;
6377 struct workspace *ws;
6379 /* suppress unused warnings since vars are needed */
6383 if (getenv("SWM_STARTED"))
6387 if (sscanf(value, "ws[%d]:%d:%d:%d:%d:%1023c",
6388 &ws_id, &mg, &ma, &si, &ar, s) != 6)
6389 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
6390 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
6393 if (ws_id < 0 || ws_id >= workspace_limit)
6394 errx(1, "layout: invalid workspace %d", ws_id + 1);
6396 if (!strcasecmp(s, "vertical"))
6398 else if (!strcasecmp(s, "vertical_flip")) {
6401 } else if (!strcasecmp(s, "horizontal"))
6403 else if (!strcasecmp(s, "horizontal_flip")) {
6406 } else if (!strcasecmp(s, "fullscreen"))
6409 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
6410 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
6413 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
6414 for (i = 0; i < num_screens; i++) {
6415 ws = (struct workspace *)&screens[i].ws;
6416 ws[ws_id].cur_layout = &layouts[st];
6418 ws[ws_id].always_raise = ar;
6419 if (st == SWM_MAX_STACK)
6423 for (x = 0; x < abs(mg); x++) {
6424 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6425 mg >= 0 ? SWM_ARG_ID_MASTERGROW :
6426 SWM_ARG_ID_MASTERSHRINK);
6430 for (x = 0; x < abs(ma); x++) {
6431 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6432 ma >= 0 ? SWM_ARG_ID_MASTERADD :
6433 SWM_ARG_ID_MASTERDEL);
6437 for (x = 0; x < abs(si); x++) {
6438 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6439 si >= 0 ? SWM_ARG_ID_STACKINC :
6440 SWM_ARG_ID_STACKDEC);
6445 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6446 SWM_ARG_ID_FLIPLAYOUT);
6456 /* config options */
6457 struct config_option {
6459 int (*func)(char*, char*, int);
6462 struct config_option configopt[] = {
6463 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
6464 { "bar_at_bottom", setconfvalue, SWM_S_BAR_AT_BOTTOM },
6465 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
6466 { "bar_border_width", setconfvalue, SWM_S_BAR_BORDER_WIDTH },
6467 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
6468 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
6469 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
6470 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
6471 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
6472 { "bar_justify", setconfvalue, SWM_S_BAR_JUSTIFY },
6473 { "bar_format", setconfvalue, SWM_S_BAR_FORMAT },
6474 { "keyboard_mapping", setkeymapping, 0 },
6475 { "bind", setconfbinding, 0 },
6476 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
6477 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
6478 { "clock_format", setconfvalue, SWM_S_CLOCK_FORMAT },
6479 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
6480 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
6481 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
6482 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
6483 { "workspace_limit", setconfvalue, SWM_S_WORKSPACE_LIMIT },
6484 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
6485 { "verbose_layout", setconfvalue, SWM_S_VERBOSE_LAYOUT },
6486 { "modkey", setconfmodkey, 0 },
6487 { "program", setconfspawn, 0 },
6488 { "quirk", setconfquirk, 0 },
6489 { "region", setconfregion, 0 },
6490 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
6491 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
6492 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
6493 { "window_name_enabled", setconfvalue, SWM_S_WINDOW_NAME_ENABLED },
6494 { "urgent_enabled", setconfvalue, SWM_S_URGENT_ENABLED },
6495 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
6496 { "title_class_enabled", setconfvalue, SWM_S_TITLE_CLASS_ENABLED },
6497 { "title_name_enabled", setconfvalue, SWM_S_TITLE_NAME_ENABLED },
6498 { "focus_mode", setconfvalue, SWM_S_FOCUS_MODE },
6499 { "focus_close", setconfvalue, SWM_S_FOCUS_CLOSE },
6500 { "focus_close_wrap", setconfvalue, SWM_S_FOCUS_CLOSE_WRAP },
6501 { "focus_default", setconfvalue, SWM_S_FOCUS_DEFAULT },
6502 { "spawn_position", setconfvalue, SWM_S_SPAWN_ORDER },
6503 { "disable_border", setconfvalue, SWM_S_DISABLE_BORDER },
6504 { "border_width", setconfvalue, SWM_S_BORDER_WIDTH },
6505 { "autorun", setautorun, 0 },
6506 { "layout", setlayout, 0 },
6511 conf_load(char *filename, int keymapping)
6514 char *line, *cp, *optsub, *optval;
6515 size_t linelen, lineno = 0;
6516 int wordlen, i, optidx;
6517 struct config_option *opt;
6519 DNPRINTF(SWM_D_CONF, "conf_load: begin\n");
6521 if (filename == NULL) {
6522 warnx("conf_load: no filename");
6525 if ((config = fopen(filename, "r")) == NULL) {
6526 warn("conf_load: fopen: %s", filename);
6530 while (!feof(config)) {
6531 if ((line = fparseln(config, &linelen, &lineno, NULL, 0))
6534 err(1, "%s", filename);
6539 cp += strspn(cp, " \t\n"); /* eat whitespace */
6540 if (cp[0] == '\0') {
6545 /* get config option */
6546 wordlen = strcspn(cp, "=[ \t\n");
6548 warnx("%s: line %zd: no option found",
6553 for (i = 0; i < LENGTH(configopt); i++) {
6554 opt = &configopt[i];
6555 if (!strncasecmp(cp, opt->optname, wordlen) &&
6556 (int)strlen(opt->optname) == wordlen) {
6562 warnx("%s: line %zd: unknown option %.*s",
6563 filename, lineno, wordlen, cp);
6566 if (keymapping && strcmp(opt->optname, "bind")) {
6567 warnx("%s: line %zd: invalid option %.*s",
6568 filename, lineno, wordlen, cp);
6572 cp += strspn(cp, " \t\n"); /* eat whitespace */
6573 /* get [selector] if any */
6577 wordlen = strcspn(cp, "]");
6580 warnx("%s: line %zd: syntax error",
6585 if (asprintf(&optsub, "%.*s", wordlen, cp) ==
6587 warnx("%s: line %zd: unable to allocate"
6588 "memory for selector", filename,
6594 cp += strspn(cp, "] \t\n"); /* eat trailing */
6596 cp += strspn(cp, "= \t\n"); /* eat trailing */
6598 optval = strdup(cp);
6599 /* call function to deal with it all */
6600 if (configopt[optidx].func(optsub, optval,
6601 configopt[optidx].funcflags) != 0)
6602 errx(1, "%s: line %zd: invalid data for %s",
6603 filename, lineno, configopt[optidx].optname);
6610 DNPRINTF(SWM_D_CONF, "conf_load: end\n");
6617 DNPRINTF(SWM_D_CONF, "conf_load: end with error.\n");
6623 set_child_transient(struct ws_win *win, xcb_window_t *trans)
6625 struct ws_win *parent, *w;
6626 struct swm_region *r;
6627 struct workspace *ws;
6628 xcb_icccm_wm_hints_t wmh;
6630 parent = find_window(win->transient);
6632 parent->focus_child = win;
6634 DNPRINTF(SWM_D_MISC, "set_child_transient: parent doesn't exist"
6635 " for 0x%x trans 0x%x\n", win->id, win->transient);
6637 r = root_to_region(win->wa->root);
6639 /* parent doen't exist in our window list */
6640 TAILQ_FOREACH(w, &ws->winlist, entry) {
6641 if (xcb_icccm_get_wm_hints_reply(conn,
6642 xcb_icccm_get_wm_hints(conn, w->id),
6644 warnx("can't get hints for 0x%x", w->id);
6648 if (win->hints.window_group != wmh.window_group)
6651 w->focus_child = win;
6652 win->transient = w->id;
6654 DNPRINTF(SWM_D_MISC, "set_child_transient: adjusting "
6655 "transient to 0x%x\n", win->transient);
6662 window_get_pid(xcb_window_t win)
6667 xcb_get_property_cookie_t pc;
6668 xcb_get_property_reply_t *pr;
6670 apid = get_atom_from_string("_NET_WM_PID");
6671 if (apid == XCB_ATOM_NONE)
6674 pc = xcb_get_property(conn, 0, win, apid, XCB_ATOM_CARDINAL, 0, 1);
6675 pr = xcb_get_property_reply(conn, pc, NULL);
6678 if (pr->type != XCB_ATOM_CARDINAL) {
6683 if (pr->type == apid && pr->format == 32)
6684 ret = *((pid_t *)xcb_get_property_value(pr));
6690 apid = get_atom_from_string("_SWM_PID");
6691 pc = xcb_get_property(conn, 0, win, apid, XCB_ATOM_STRING,
6693 pr = xcb_get_property_reply(conn, pc, NULL);
6696 if (pr->type != apid) {
6701 ret = (pid_t)strtonum(xcb_get_property_value(pr), 0, INT_MAX, &errstr);
6708 get_ws_idx(xcb_window_t id)
6714 xcb_get_property_reply_t *gpr;
6716 gpr = xcb_get_property_reply(conn,
6717 xcb_get_property(conn, 0, id, a_swm_ws,
6718 XCB_ATOM_STRING, 0, SWM_PROPLEN),
6723 proplen = xcb_get_property_value_length(gpr);
6725 prop = malloc(proplen + 1);
6728 xcb_get_property_value(gpr),
6730 prop[proplen] = '\0';
6737 DNPRINTF(SWM_D_PROP, "get_ws_idx: _SWM_WS: %s\n", prop);
6738 ws_idx = (int)strtonum(prop, 0, workspace_limit - 1, &errstr);
6740 DNPRINTF(SWM_D_PROP, "get_ws_idx: window: #%s: %s",
6750 manage_window(xcb_window_t id, uint16_t mapped)
6752 xcb_window_t trans = XCB_WINDOW_NONE;
6753 struct ws_win *win, *ww;
6755 char ws_idx_str[SWM_PROPLEN];
6756 struct swm_region *r;
6760 xcb_icccm_get_wm_protocols_reply_t wpr;
6762 if ((win = find_window(id)) != NULL) {
6763 DNPRINTF(SWM_D_MISC, "manage_window: win 0x%x already "
6764 "managed; skipping.)\n", id);
6765 return (win); /* Already managed. */
6768 /* See if window is on the unmanaged list. */
6769 if ((win = find_unmanaged_window(id)) != NULL) {
6770 DNPRINTF(SWM_D_MISC, "manage_window: win 0x%x found on "
6771 "unmanaged list.\n", id);
6772 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
6775 set_child_transient(win, &trans);
6779 DNPRINTF(SWM_D_MISC, "manage_window: win 0x%x is new.\n", id);
6782 /* Create and initialize ws_win object. */
6783 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
6784 err(1, "manage_window: calloc: failed to allocate memory for "
6789 /* Get window geometry. */
6790 win->wa = xcb_get_geometry_reply(conn,
6791 xcb_get_geometry(conn, win->id),
6794 /* Figure out which region the window belongs to. */
6795 r = root_to_region(win->wa->root);
6797 /* Ignore window border if there is one. */
6798 WIDTH(win) = win->wa->width;
6799 HEIGHT(win) = win->wa->height;
6800 X(win) = win->wa->x + win->wa->border_width - border_width;
6801 Y(win) = win->wa->y + win->wa->border_width - border_width;
6803 win->mapped = mapped;
6804 win->floatmaxed = 0;
6805 win->ewmh_flags = 0;
6806 win->s = r->s; /* this never changes */
6808 store_float_geom(win, r);
6810 /* Get WM_SIZE_HINTS. */
6811 xcb_icccm_get_wm_normal_hints_reply(conn,
6812 xcb_icccm_get_wm_normal_hints(conn, win->id),
6816 xcb_icccm_get_wm_hints_reply(conn,
6817 xcb_icccm_get_wm_hints(conn, win->id),
6820 /* Get WM_TRANSIENT_FOR; see if window is a transient. */
6821 xcb_icccm_get_wm_transient_for_reply(conn,
6822 xcb_icccm_get_wm_transient_for(conn, win->id),
6825 win->transient = trans;
6826 set_child_transient(win, &win->transient);
6829 /* Get supported protocols. */
6830 if (xcb_icccm_get_wm_protocols_reply(conn,
6831 xcb_icccm_get_wm_protocols(conn, win->id, a_prot),
6833 for (i = 0; i < wpr.atoms_len; i++) {
6834 if (wpr.atoms[i] == a_takefocus)
6835 win->take_focus = 1;
6836 if (wpr.atoms[i] == a_delete)
6837 win->can_delete = 1;
6839 xcb_icccm_get_wm_protocols_reply_wipe(&wpr);
6842 win->iconic = get_swm_iconic(win);
6844 /* Figure out which workspace the window belongs to. */
6845 if ((p = find_pid(window_get_pid(win->id))) != NULL) {
6846 win->ws = &r->s->ws[p->ws];
6847 TAILQ_REMOVE(&pidlist, p, entry);
6850 } else if ((ws_idx = get_ws_idx(win->id)) != -1 &&
6851 win->transient == 0) {
6852 /* _SWM_WS is set; use that. */
6853 win->ws = &r->s->ws[ws_idx];
6854 } else if (trans && (ww = find_window(trans)) != NULL) {
6855 /* Launch transients in the same ws as parent. */
6861 /* Set the _SWM_WS atom so we can remember this after reincarnation. */
6862 if (snprintf(ws_idx_str, SWM_PROPLEN, "%d", win->ws->idx) <
6864 DNPRINTF(SWM_D_PROP, "manage_window: set _SWM_WS: %s\n",
6866 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
6867 a_swm_ws, XCB_ATOM_STRING, 8, strlen(ws_idx_str),
6872 ewmh_autoquirk(win);
6874 /* Determine initial quirks. */
6875 if (xcb_icccm_get_wm_class_reply(conn,
6876 xcb_icccm_get_wm_class(conn, win->id),
6878 DNPRINTF(SWM_D_CLASS, "manage_window: class: %s, name: %s\n",
6879 win->ch.class_name, win->ch.instance_name);
6881 /* java is retarded so treat it special */
6882 if (strstr(win->ch.instance_name, "sun-awt")) {
6883 DNPRINTF(SWM_D_CLASS, "manage_window: java window "
6888 TAILQ_FOREACH(qp, &quirks, entry) {
6889 if (!strcmp(win->ch.class_name, qp->class) &&
6890 !strcmp(win->ch.instance_name, qp->name)) {
6891 DNPRINTF(SWM_D_CLASS, "manage_window: on quirks"
6892 "list; mask: 0x%lx\n", qp->quirk);
6893 if (qp->quirk & SWM_Q_FLOAT)
6895 win->quirks = qp->quirk;
6900 /* Alter window position if quirky */
6901 if (win->quirks & SWM_Q_ANYWHERE)
6904 /* Reset font sizes (the bruteforce way; no default keybinding). */
6905 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
6906 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
6907 fake_keypress(win, XK_KP_Subtract, XCB_MOD_MASK_SHIFT);
6908 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
6909 fake_keypress(win, XK_KP_Add, XCB_MOD_MASK_SHIFT);
6912 /* Make sure window is positioned inside its region, if its active. */
6914 constrain_window(win, win->ws->r, 0);
6919 /* Select which X events to monitor and set border pixel color. */
6920 wa[0] = win->s->c[SWM_S_COLOR_UNFOCUS].pixel;
6921 wa[1] = XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_PROPERTY_CHANGE |
6922 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
6924 wa[1] |= XCB_EVENT_MASK_LEAVE_WINDOW | XCB_EVENT_MASK_FOCUS_CHANGE;
6927 xcb_change_window_attributes(conn, win->id, XCB_CW_BORDER_PIXEL |
6928 XCB_CW_EVENT_MASK, wa);
6931 /* Figure out where to stack the window in the workspace. */
6932 if (trans && (ww = find_window(trans)))
6933 TAILQ_INSERT_AFTER(&win->ws->winlist, ww, win, entry);
6934 else if (win->ws->focus && spawn_position == SWM_STACK_ABOVE)
6935 TAILQ_INSERT_AFTER(&win->ws->winlist, win->ws->focus, win,
6937 else if (win->ws->focus && spawn_position == SWM_STACK_BELOW)
6938 TAILQ_INSERT_BEFORE(win->ws->focus, win, entry);
6939 else switch (spawn_position) {
6942 case SWM_STACK_ABOVE:
6943 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
6945 case SWM_STACK_BOTTOM:
6946 case SWM_STACK_BELOW:
6947 TAILQ_INSERT_HEAD(&win->ws->winlist, win, entry);
6950 /* Get initial _NET_WM_STATE */
6951 ewmh_get_win_state(win);
6952 /* Set initial _NET_WM_ALLOWED_ACTIONS */
6953 ewmh_update_actions(win);
6957 DNPRINTF(SWM_D_MISC, "manage_window: done. window: 0x%x, (x,y) w x h: "
6958 "(%d,%d) %d x %d, ws: %d, iconic: %s, transient: 0x%x\n", win->id,
6959 X(win), Y(win), WIDTH(win), HEIGHT(win), win->ws->idx,
6960 YESNO(win->iconic), win->transient);
6966 free_window(struct ws_win *win)
6968 DNPRINTF(SWM_D_MISC, "free_window: window: 0x%x\n", win->id);
6973 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
6978 xcb_icccm_get_wm_class_reply_wipe(&win->ch);
6983 memset(win, 0xff, sizeof *win); /* XXX kill later */
6986 DNPRINTF(SWM_D_MISC, "free_window: done.\n");
6990 unmanage_window(struct ws_win *win)
6992 struct ws_win *parent;
6997 DNPRINTF(SWM_D_MISC, "unmanage_window: window: 0x%x\n", win->id);
6999 if (win->transient) {
7000 parent = find_window(win->transient);
7002 parent->focus_child = NULL;
7005 focus_win(get_focus_prev(win));
7007 TAILQ_REMOVE(&win->ws->winlist, win, entry);
7008 TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
7012 expose(xcb_expose_event_t *e)
7015 struct swm_region *r;
7017 DNPRINTF(SWM_D_EVENT, "expose: window: 0x%x\n", e->window);
7019 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7020 for (i = 0; i < num_screens; i++)
7021 TAILQ_FOREACH(r, &screens[i].rl, entry)
7022 if (e->window == WINID(r->bar))
7030 focusin(xcb_focus_in_event_t *e)
7032 DNPRINTF(SWM_D_EVENT, "focusin: window: 0x%x, mode: %s(%u), "
7033 "detail: %s(%u)\n", e->event, get_notify_mode_label(e->mode),
7034 e->mode, get_notify_detail_label(e->detail), e->detail);
7038 focusout(xcb_focus_out_event_t *e)
7040 DNPRINTF(SWM_D_EVENT, "focusout: window: 0x%x, mode: %s(%u), "
7041 "detail: %s(%u)\n", e->event, get_notify_mode_label(e->mode),
7042 e->mode, get_notify_detail_label(e->detail), e->detail);
7047 keypress(xcb_key_press_event_t *e)
7049 xcb_keysym_t keysym;
7052 keysym = xcb_key_press_lookup_keysym(syms, e, 0);
7054 DNPRINTF(SWM_D_EVENT, "keypress: %u %u\n", e->detail, keysym);
7056 if ((kp = key_lookup(CLEANMASK(e->state), keysym)) == NULL)
7059 last_event_time = e->time;
7061 if (kp->funcid == KF_SPAWN_CUSTOM)
7062 spawn_custom(root_to_region(e->root),
7063 &(keyfuncs[kp->funcid].args), kp->spawn_name);
7064 else if (keyfuncs[kp->funcid].func)
7065 keyfuncs[kp->funcid].func(root_to_region(e->root),
7066 &(keyfuncs[kp->funcid].args));
7070 buttonpress(xcb_button_press_event_t *e)
7076 DNPRINTF(SWM_D_EVENT, "buttonpress: window 0x%x, detail: %u\n",
7077 e->event, e->detail);
7079 if ((win = find_window(e->event)) == NULL)
7082 last_event_time = e->time;
7084 focus_win(get_focus_magic(win));
7086 for (i = 0; i < LENGTH(buttons); i++)
7087 if (client_click == buttons[i].action && buttons[i].func &&
7088 buttons[i].button == e->detail &&
7089 CLEANMASK(buttons[i].mask) == CLEANMASK(e->state)) {
7090 buttons[i].func(win, &buttons[i].args);
7095 DNPRINTF(SWM_D_EVENT, "buttonpress: passing to window.\n");
7096 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, e->time);
7098 DNPRINTF(SWM_D_EVENT, "buttonpress: handled.\n");
7106 print_win_geom(xcb_window_t w)
7108 xcb_get_geometry_reply_t *wa;
7110 wa = xcb_get_geometry_reply(conn, xcb_get_geometry(conn, w), NULL);
7113 DNPRINTF(SWM_D_MISC, "print_win_geom: window not found: 0x%x\n",
7118 DNPRINTF(SWM_D_MISC, "print_win_geom: window: 0x%x, root: 0x%x, "
7119 "depth: %u, (x,y) w x h: (%d,%d) %d x %d, border: %d\n",
7120 w, wa->root, wa->depth, wa->x, wa->y, wa->width, wa->height,
7129 get_stack_mode_name(uint8_t mode)
7134 case XCB_STACK_MODE_ABOVE:
7137 case XCB_STACK_MODE_BELOW:
7140 case XCB_STACK_MODE_TOP_IF:
7143 case XCB_STACK_MODE_BOTTOM_IF:
7146 case XCB_STACK_MODE_OPPOSITE:
7158 configurerequest(xcb_configure_request_event_t *e)
7161 struct swm_region *r = NULL;
7164 uint32_t wc[7] = {0};
7166 if ((win = find_window(e->window)) == NULL)
7167 if ((win = find_unmanaged_window(e->window)) == NULL)
7171 if (swm_debug & SWM_D_EVENT) {
7172 print_win_geom(e->window);
7174 DNPRINTF(SWM_D_EVENT, "configurerequest: window: 0x%x, "
7175 "parent: 0x%x, new: %s, value_mask: %u { ", e->window,
7176 e->parent, YESNO(new), e->value_mask);
7177 if (e->value_mask & XCB_CONFIG_WINDOW_X)
7178 DPRINTF("X: %d ", e->x);
7179 if (e->value_mask & XCB_CONFIG_WINDOW_Y)
7180 DPRINTF("Y: %d ", e->y);
7181 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH)
7182 DPRINTF("W: %u ", e->width);
7183 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
7184 DPRINTF("H: %u ", e->height);
7185 if (e->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH)
7186 DPRINTF("Border: %u ", e->border_width);
7187 if (e->value_mask & XCB_CONFIG_WINDOW_SIBLING)
7188 DPRINTF("Sibling: 0x%x ", e->sibling);
7189 if (e->value_mask & XCB_CONFIG_WINDOW_STACK_MODE)
7190 DPRINTF("StackMode: %s(%u) ",
7191 get_stack_mode_name(e->stack_mode), e->stack_mode);
7197 if (e->value_mask & XCB_CONFIG_WINDOW_X) {
7198 mask |= XCB_CONFIG_WINDOW_X;
7201 if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
7202 mask |= XCB_CONFIG_WINDOW_Y;
7205 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
7206 mask |= XCB_CONFIG_WINDOW_WIDTH;
7209 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
7210 mask |= XCB_CONFIG_WINDOW_HEIGHT;
7211 wc[i++] = e->height;
7213 if (e->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH) {
7214 mask |= XCB_CONFIG_WINDOW_BORDER_WIDTH;
7215 wc[i++] = e->border_width;
7217 if (e->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
7218 mask |= XCB_CONFIG_WINDOW_SIBLING;
7219 wc[i++] = e->sibling;
7221 if (e->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
7222 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
7223 wc[i++] = e->stack_mode;
7227 xcb_configure_window(conn, e->window, mask, wc);
7228 } else if ((!win->manual || win->quirks & SWM_Q_ANYWHERE) &&
7229 !(win->ewmh_flags & EWMH_F_FULLSCREEN)) {
7232 else if (win->ws->old_r)
7235 /* windows are centered unless ANYWHERE quirk is set. */
7236 if (win->quirks & SWM_Q_ANYWHERE) {
7237 if (e->value_mask & XCB_CONFIG_WINDOW_X) {
7238 win->g_float.x = e->x;
7240 win->g_float.x -= X(r);
7243 if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
7244 win->g_float.y = e->y;
7246 win->g_float.y -= Y(r);
7250 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH)
7251 win->g_float.w = e->width;
7253 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
7254 win->g_float.h = e->height;
7256 win->g_floatvalid = 1;
7258 if (win->floating && r) {
7259 WIDTH(win) = win->g_float.w;
7260 HEIGHT(win) = win->g_float.h;
7262 stack_floater(win, win->ws->r);
7276 configurenotify(xcb_configure_notify_event_t *e)
7280 DNPRINTF(SWM_D_EVENT, "configurenotify: window: 0x%x\n",
7283 win = find_window(e->window);
7285 xcb_icccm_get_wm_normal_hints_reply(conn,
7286 xcb_icccm_get_wm_normal_hints(conn, win->id),
7289 if (font_adjusted) {
7297 destroynotify(xcb_destroy_notify_event_t *e)
7301 DNPRINTF(SWM_D_EVENT, "destroynotify: window: 0x%x\n", e->window);
7303 if ((win = find_window(e->window)) == NULL) {
7304 if ((win = find_unmanaged_window(e->window)) == NULL)
7310 /* make sure we focus on something */
7313 unmanage_window(win);
7323 get_notify_detail_label(uint8_t detail)
7328 case XCB_NOTIFY_DETAIL_ANCESTOR:
7331 case XCB_NOTIFY_DETAIL_VIRTUAL:
7334 case XCB_NOTIFY_DETAIL_INFERIOR:
7337 case XCB_NOTIFY_DETAIL_NONLINEAR:
7338 label = "Nonlinear";
7340 case XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL:
7341 label = "NonlinearVirtual";
7343 case XCB_NOTIFY_DETAIL_POINTER:
7346 case XCB_NOTIFY_DETAIL_POINTER_ROOT:
7347 label = "PointerRoot";
7349 case XCB_NOTIFY_DETAIL_NONE:
7360 get_notify_mode_label(uint8_t mode)
7365 case XCB_NOTIFY_MODE_NORMAL:
7368 case XCB_NOTIFY_MODE_GRAB:
7371 case XCB_NOTIFY_MODE_UNGRAB:
7374 case XCB_NOTIFY_MODE_WHILE_GRABBED:
7375 label = "WhileGrabbed";
7386 enternotify(xcb_enter_notify_event_t *e)
7390 DNPRINTF(SWM_D_FOCUS, "enternotify: time: %u, win (x,y): 0x%x "
7391 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): 0x%x (%d,%d), "
7392 "child: 0x%x, same_screen_focus: %s, state: %d\n",
7393 e->time, e->event, e->event_x, e->event_y,
7394 get_notify_mode_label(e->mode), e->mode,
7395 get_notify_detail_label(e->detail), e->detail,
7396 e->root, e->root_x, e->root_y, e->child,
7397 YESNO(e->same_screen_focus), e->state);
7399 if (focus_mode == SWM_FOCUS_MANUAL &&
7400 e->mode == XCB_NOTIFY_MODE_NORMAL) {
7401 DNPRINTF(SWM_D_EVENT, "enternotify: manual focus; ignoring.\n");
7405 if ((win = find_window(e->event)) == NULL) {
7406 DNPRINTF(SWM_D_EVENT, "enternotify: window is NULL; ignoring\n");
7410 last_event_time = e->time;
7412 focus_win(get_focus_magic(win));
7419 leavenotify(xcb_leave_notify_event_t *e)
7421 DNPRINTF(SWM_D_FOCUS, "leavenotify: time: %u, win (x,y): 0x%x "
7422 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): 0x%x (%d,%d), "
7423 "child: 0x%x, same_screen_focus: %s, state: %d\n",
7424 e->time, e->event, e->event_x, e->event_y,
7425 get_notify_mode_label(e->mode), e->mode,
7426 get_notify_detail_label(e->detail), e->detail,
7427 e->root, e->root_x, e->root_y, e->child,
7428 YESNO(e->same_screen_focus), e->state);
7432 /* lets us use one switch statement for arbitrary mode/detail combinations */
7433 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
7436 mapnotify(xcb_map_notify_event_t *e)
7440 DNPRINTF(SWM_D_EVENT, "mapnotify: window: 0x%x\n", e->window);
7442 if ((win = find_window(e->window)) == NULL)
7443 win = manage_window(e->window, 1);
7446 set_win_state(win, XCB_ICCCM_WM_STATE_NORMAL);
7448 /* Focus on window if it is selected. */
7449 if (win->ws->focus == win)
7456 mappingnotify(xcb_mapping_notify_event_t *e)
7458 xcb_refresh_keyboard_mapping(syms, e);
7460 if (e->request == XCB_MAPPING_KEYBOARD)
7465 maprequest(xcb_map_request_event_t *e)
7468 xcb_get_window_attributes_reply_t *war;
7470 DNPRINTF(SWM_D_EVENT, "maprequest: win 0x%x\n",
7473 war = xcb_get_window_attributes_reply(conn,
7474 xcb_get_window_attributes(conn, e->window),
7477 DNPRINTF(SWM_D_EVENT, "maprequest: window lost.\n");
7481 if (war->override_redirect) {
7482 DNPRINTF(SWM_D_EVENT, "maprequest: override_redirect; "
7487 win = manage_window(e->window,
7488 (war->map_state == XCB_MAP_STATE_VIEWABLE));
7490 /* All windows need to be mapped if they are in the current workspace.*/
7494 /* The new window should get focus. */
7495 win->ws->focus = get_focus_magic(win);
7497 /* Ignore EnterNotify to handle the mapnotify without interference. */
7498 if (focus_mode == SWM_FOCUS_DEFAULT)
7499 event_drain(XCB_ENTER_NOTIFY);
7502 DNPRINTF(SWM_D_EVENT, "maprequest: done.\n");
7507 get_atom_name(xcb_atom_t atom)
7512 * This should be disabled during most debugging since
7513 * xcb_get_* causes an xcb_flush.
7516 xcb_get_atom_name_reply_t *r;
7518 r = xcb_get_atom_name_reply(conn,
7519 xcb_get_atom_name(conn, atom),
7522 len = xcb_get_atom_name_name_length(r);
7524 name = malloc(len + 1);
7526 memcpy(name, xcb_get_atom_name_name(r), len);
7540 propertynotify(xcb_property_notify_event_t *e)
7546 name = get_atom_name(e->atom);
7547 DNPRINTF(SWM_D_EVENT, "propertynotify: window: 0x%x, atom: %s(%u), "
7548 "time: %#x\n", e->window, name, e->atom, e->time);
7551 win = find_window(e->window);
7555 last_event_time = e->time;
7557 if (e->atom == a_swm_iconic) {
7558 if (e->state == XCB_PROPERTY_DELETE) {
7559 /* The window is no longer iconic, restack ws. */
7562 /* The window should get focus. */
7563 win->ws->focus = get_focus_magic(win);
7565 /* Flush EnterNotify for mapnotify, if needed. */
7568 } else if (e->state == XCB_PROPERTY_NEW_VALUE) {
7569 win->ws->focus = NULL;
7575 focus_win(get_focus_prev(win));
7580 } else if (e->atom == a_state && e->state == XCB_PROPERTY_NEW_VALUE) {
7581 /* Focus on window if it is selected. */
7582 if (win->ws->focus == win)
7587 case XCB_ATOM_WM_CLASS:
7588 case XCB_ATOM_WM_NAME:
7599 unmapnotify(xcb_unmap_notify_event_t *e)
7603 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: 0x%x\n", e->window);
7605 /* determine if we need to help unmanage this window */
7606 win = find_window(e->window);
7610 if (getstate(e->window) == XCB_ICCCM_WM_STATE_NORMAL) {
7611 unmanage_window(win);
7619 visibilitynotify(xcb_visibility_notify_event_t *e)
7621 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: 0x%x\n",
7627 clientmessage(xcb_client_message_event_t *e)
7630 xcb_map_request_event_t mre;
7634 name = get_atom_name(e->type);
7635 DNPRINTF(SWM_D_EVENT, "clientmessage: window: 0x%x, atom: %s(%u)\n",
7636 e->window, name, e->type);
7639 win = find_window(e->window);
7642 if (e->type == ewmh[_NET_ACTIVE_WINDOW].atom) {
7643 DNPRINTF(SWM_D_EVENT, "clientmessage: request focus on "
7644 "unmanaged window.\n");
7645 mre.window = e->window;
7651 if (e->type == ewmh[_NET_ACTIVE_WINDOW].atom) {
7652 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_ACTIVE_WINDOW\n");
7655 if (e->type == ewmh[_NET_CLOSE_WINDOW].atom) {
7656 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_CLOSE_WINDOW\n");
7657 if (win->can_delete)
7658 client_msg(win, a_delete, 0);
7660 xcb_kill_client(conn, win->id);
7662 if (e->type == ewmh[_NET_MOVERESIZE_WINDOW].atom) {
7663 DNPRINTF(SWM_D_EVENT,
7664 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
7665 if (win->floating) {
7666 if (e->data.data32[0] & (1<<8)) /* x */
7667 X(win) = e->data.data32[1];
7668 if (e->data.data32[0] & (1<<9)) /* y */
7669 Y(win) = e->data.data32[2];
7670 if (e->data.data32[0] & (1<<10)) /* width */
7671 WIDTH(win) = e->data.data32[3];
7672 if (e->data.data32[0] & (1<<11)) /* height */
7673 HEIGHT(win) = e->data.data32[4];
7678 /* TODO: Change stack sizes */
7679 /* notify no change was made. */
7680 config_win(win, NULL);
7683 if (e->type == ewmh[_NET_WM_STATE].atom) {
7684 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_WM_STATE\n");
7685 ewmh_update_win_state(win, e->data.data32[1], e->data.data32[0]);
7686 if (e->data.data32[2])
7687 ewmh_update_win_state(win, e->data.data32[2],
7699 int errcode = xcb_connection_has_error(conn);
7700 #ifdef XCB_CONN_ERROR
7703 case XCB_CONN_ERROR:
7704 s = "Socket error, pipe error or other stream error.";
7706 case XCB_CONN_CLOSED_EXT_NOTSUPPORTED:
7707 s = "Extension not supported.";
7709 case XCB_CONN_CLOSED_MEM_INSUFFICIENT:
7710 s = "Insufficient memory.";
7712 case XCB_CONN_CLOSED_REQ_LEN_EXCEED:
7713 s = "Request length was exceeded.";
7715 case XCB_CONN_CLOSED_PARSE_ERR:
7716 s = "Error parsing display string.";
7719 s = "Unknown error.";
7722 errx(errcode, "X CONNECTION ERROR: %s", s);
7725 errx(errcode, "X CONNECTION ERROR");
7733 const uint32_t val = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
7735 xcb_void_cookie_t wac;
7736 xcb_generic_error_t *error;
7738 /* this causes an error if some other window manager is running */
7739 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7740 for (i = 0; i < num_screens; i++) {
7741 if ((sc = get_screen(i)) == NULL)
7742 errx(1, "ERROR: can't get screen %d.", i);
7743 DNPRINTF(SWM_D_INIT, "enable_wm: screen %d, root: 0x%x\n",
7745 wac = xcb_change_window_attributes_checked(conn, sc->root,
7746 XCB_CW_EVENT_MASK, &val);
7747 if ((error = xcb_request_check(conn, wac))) {
7748 DNPRINTF(SWM_D_INIT, "enable_wm: error_code: %u\n",
7759 new_region(struct swm_screen *s, int x, int y, int w, int h)
7761 struct swm_region *r, *n;
7762 struct workspace *ws = NULL;
7765 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
7766 s->idx, w, h, x, y);
7768 /* remove any conflicting regions */
7769 n = TAILQ_FIRST(&s->rl);
7772 n = TAILQ_NEXT(r, entry);
7773 if (X(r) < (x + w) &&
7774 (X(r) + WIDTH(r)) > x &&
7776 (Y(r) + HEIGHT(r)) > y) {
7777 if (r->ws->r != NULL)
7778 r->ws->old_r = r->ws->r;
7781 TAILQ_REMOVE(&s->rl, r, entry);
7782 TAILQ_INSERT_TAIL(&s->orl, r, entry);
7786 /* search old regions for one to reuse */
7788 /* size + location match */
7789 TAILQ_FOREACH(r, &s->orl, entry)
7790 if (X(r) == x && Y(r) == y &&
7791 HEIGHT(r) == h && WIDTH(r) == w)
7795 TAILQ_FOREACH(r, &s->orl, entry)
7796 if (HEIGHT(r) == h && WIDTH(r) == w)
7800 TAILQ_REMOVE(&s->orl, r, entry);
7801 /* try to use old region's workspace */
7802 if (r->ws->r == NULL)
7805 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
7806 err(1, "new_region: calloc: failed to allocate memory "
7809 /* if we don't have a workspace already, find one */
7811 for (i = 0; i < workspace_limit; i++)
7812 if (s->ws[i].r == NULL) {
7819 errx(1, "new_region: no free workspaces");
7830 TAILQ_INSERT_TAIL(&s->rl, r, entry);
7836 #ifdef SWM_XRR_HAS_CRTC
7839 #endif /* SWM_XRR_HAS_CRTC */
7840 struct swm_region *r;
7842 xcb_randr_get_screen_resources_current_cookie_t src;
7843 xcb_randr_get_screen_resources_current_reply_t *srr;
7844 xcb_randr_get_crtc_info_cookie_t cic;
7845 xcb_randr_get_crtc_info_reply_t *cir = NULL;
7846 xcb_randr_crtc_t *crtc;
7847 xcb_screen_t *screen;
7849 if ((screen = get_screen(i)) == NULL)
7850 errx(1, "ERROR: can't get screen %d.", i);
7852 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7853 if (i >= num_screens)
7854 errx(1, "scan_xrandr: invalid screen");
7856 /* remove any old regions */
7857 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
7858 r->ws->old_r = r->ws->r = NULL;
7860 TAILQ_REMOVE(&screens[i].rl, r, entry);
7861 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
7865 /* map virtual screens onto physical screens */
7866 #ifdef SWM_XRR_HAS_CRTC
7867 if (xrandr_support) {
7868 src = xcb_randr_get_screen_resources_current(conn,
7870 srr = xcb_randr_get_screen_resources_current_reply(conn, src,
7873 new_region(&screens[i], 0, 0,
7874 screen->width_in_pixels,
7875 screen->height_in_pixels);
7878 ncrtc = srr->num_crtcs;
7880 crtc = xcb_randr_get_screen_resources_current_crtcs(srr);
7881 for (c = 0; c < ncrtc; c++) {
7882 cic = xcb_randr_get_crtc_info(conn, crtc[c],
7884 cir = xcb_randr_get_crtc_info_reply(conn, cic, NULL);
7887 if (cir->num_outputs == 0) {
7893 new_region(&screens[i], 0, 0,
7894 screen->width_in_pixels,
7895 screen->height_in_pixels);
7897 new_region(&screens[i],
7898 cir->x, cir->y, cir->width, cir->height);
7903 #endif /* SWM_XRR_HAS_CRTC */
7905 new_region(&screens[i], 0, 0, screen->width_in_pixels,
7906 screen->height_in_pixels);
7911 screenchange(xcb_randr_screen_change_notify_event_t *e)
7913 struct swm_region *r;
7916 DNPRINTF(SWM_D_EVENT, "screenchange: root: 0x%x\n", e->root);
7918 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7919 /* silly event doesn't include the screen index */
7920 for (i = 0; i < num_screens; i++)
7921 if (screens[i].root == e->root)
7923 if (i >= num_screens)
7924 errx(1, "screenchange: screen not found");
7926 /* brute force for now, just re-enumerate the regions */
7930 print_win_geom(e->root);
7932 /* add bars to all regions */
7933 for (i = 0; i < num_screens; i++)
7934 TAILQ_FOREACH(r, &screens[i].rl, entry)
7942 xcb_window_t *wins = NULL;
7944 int i, j, num_screens;
7945 uint16_t state, manage, mapped;
7947 xcb_query_tree_cookie_t qtc;
7948 xcb_query_tree_reply_t *qtr;
7949 xcb_get_window_attributes_cookie_t c;
7950 xcb_get_window_attributes_reply_t *r;
7951 xcb_get_property_cookie_t pc;
7953 DNPRINTF(SWM_D_INIT, "grab_windows: begin\n");
7954 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7955 for (i = 0; i < num_screens; i++) {
7956 qtc = xcb_query_tree(conn, screens[i].root);
7957 qtr = xcb_query_tree_reply(conn, qtc, NULL);
7960 wins = xcb_query_tree_children(qtr);
7961 no = xcb_query_tree_children_length(qtr);
7962 /* attach windows to a region */
7963 /* normal windows */
7964 for (j = 0; j < no; j++) {
7965 c = xcb_get_window_attributes(conn, wins[j]);
7966 r = xcb_get_window_attributes_reply(conn, c, NULL);
7969 if (r->override_redirect) {
7974 pc = xcb_icccm_get_wm_transient_for(conn, wins[j]);
7975 if (xcb_icccm_get_wm_transient_for_reply(conn, pc,
7981 state = getstate(wins[j]);
7982 manage = state == XCB_ICCCM_WM_STATE_ICONIC;
7983 mapped = r->map_state != XCB_MAP_STATE_UNMAPPED;
7984 if (mapped || manage)
7985 manage_window(wins[j], mapped);
7988 /* transient windows */
7989 for (j = 0; j < no; j++) {
7990 c = xcb_get_window_attributes(conn, wins[j]);
7991 r = xcb_get_window_attributes_reply(conn, c, NULL);
7994 if (r->override_redirect) {
8000 state = getstate(wins[j]);
8001 manage = state == XCB_ICCCM_WM_STATE_ICONIC;
8002 mapped = r->map_state != XCB_MAP_STATE_UNMAPPED;
8003 pc = xcb_icccm_get_wm_transient_for(conn, wins[j]);
8004 if (xcb_icccm_get_wm_transient_for_reply(conn, pc,
8005 &wins[j], NULL) && manage)
8006 manage_window(wins[j], mapped);
8010 DNPRINTF(SWM_D_INIT, "grab_windows: done.\n");
8016 int i, j, k, num_screens;
8017 struct workspace *ws;
8018 uint32_t gcv[1], wa[1];
8019 const xcb_query_extension_reply_t *qep;
8020 xcb_screen_t *screen;
8021 xcb_cursor_t cursor;
8022 xcb_font_t cursor_font;
8023 xcb_randr_query_version_cookie_t c;
8024 xcb_randr_query_version_reply_t *r;
8026 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
8027 if ((screens = calloc(num_screens,
8028 sizeof(struct swm_screen))) == NULL)
8029 err(1, "setup_screens: calloc: failed to allocate memory for "
8032 /* initial Xrandr setup */
8034 qep = xcb_get_extension_data(conn, &xcb_randr_id);
8036 c = xcb_randr_query_version(conn, 1, 1);
8037 r = xcb_randr_query_version_reply(conn, c, NULL);
8039 if (r->major_version >= 1) {
8041 xrandr_eventbase = qep->first_event;
8047 cursor_font = xcb_generate_id(conn);
8048 xcb_open_font(conn, cursor_font, strlen("cursor"), "cursor");
8050 cursor = xcb_generate_id(conn);
8051 xcb_create_glyph_cursor(conn, cursor, cursor_font, cursor_font,
8052 XC_left_ptr, XC_left_ptr + 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
8055 /* map physical screens */
8056 for (i = 0; i < num_screens; i++) {
8057 DNPRINTF(SWM_D_WS, "setup_screens: init screen: %d\n", i);
8059 TAILQ_INIT(&screens[i].rl);
8060 TAILQ_INIT(&screens[i].orl);
8061 if ((screen = get_screen(i)) == NULL)
8062 errx(1, "ERROR: can't get screen %d.", i);
8063 screens[i].root = screen->root;
8065 /* set default colors */
8066 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
8067 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
8068 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
8069 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
8070 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
8072 /* create graphics context on screen */
8073 screens[i].bar_gc = xcb_generate_id(conn);
8075 xcb_create_gc(conn, screens[i].bar_gc, screens[i].root,
8076 XCB_GC_GRAPHICS_EXPOSURES, gcv);
8078 /* set default cursor */
8079 xcb_change_window_attributes(conn, screens[i].root,
8082 /* init all workspaces */
8083 /* XXX these should be dynamically allocated too */
8084 for (j = 0; j < SWM_WS_MAX; j++) {
8085 ws = &screens[i].ws[j];
8091 TAILQ_INIT(&ws->winlist);
8092 TAILQ_INIT(&ws->unmanagedlist);
8094 for (k = 0; layouts[k].l_stack != NULL; k++)
8095 if (layouts[k].l_config != NULL)
8096 layouts[k].l_config(ws,
8097 SWM_ARG_ID_STACKINIT);
8098 ws->cur_layout = &layouts[0];
8099 ws->cur_layout->l_string(ws);
8105 xcb_randr_select_input(conn, screens[i].root,
8106 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE);
8108 xcb_free_cursor(conn, cursor);
8109 xcb_close_font(conn, cursor_font);
8115 if ((bar_fonts = strdup(SWM_BAR_FONTS)) == NULL)
8116 err(1, "setup_globals: strdup: failed to allocate memory.");
8118 if ((clock_format = strdup("%a %b %d %R %Z %Y")) == NULL)
8119 err(1, "setup_globals: strdup: failed to allocate memory.");
8121 if ((syms = xcb_key_symbols_alloc(conn)) == NULL)
8122 errx(1, "unable to allocate key symbols");
8124 a_state = get_atom_from_string("WM_STATE");
8125 a_prot = get_atom_from_string("WM_PROTOCOLS");
8126 a_delete = get_atom_from_string("WM_DELETE_WINDOW");
8127 a_takefocus = get_atom_from_string("WM_TAKE_FOCUS");
8128 a_wmname = get_atom_from_string("WM_NAME");
8129 a_netwmname = get_atom_from_string("_NET_WM_NAME");
8130 a_utf8_string = get_atom_from_string("UTF8_STRING");
8131 a_string = get_atom_from_string("STRING");
8132 a_swm_iconic = get_atom_from_string("_SWM_ICONIC");
8133 a_swm_ws = get_atom_from_string("_SWM_WS");
8140 xcb_atom_t netwmcheck;
8141 xcb_window_t root, win;
8143 /* work around sun jdk bugs, code from wmname */
8144 netwmcheck = get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
8146 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
8147 for (i = 0; i < num_screens; i++) {
8148 root = screens[i].root;
8150 win = xcb_generate_id(conn);
8151 xcb_create_window(conn, XCB_COPY_FROM_PARENT, win, root,
8152 0, 0, 1, 1, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT,
8153 XCB_COPY_FROM_PARENT, 0, NULL);
8155 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root,
8156 netwmcheck, XCB_ATOM_WINDOW, 32, 1, &win);
8157 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win,
8158 netwmcheck, XCB_ATOM_WINDOW, 32, 1, &win);
8159 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win,
8160 a_netwmname, a_utf8_string, 8, strlen("LG3D"), "LG3D");
8165 shutdown_cleanup(void)
8169 /* disable alarm because the following code may not be interrupted */
8171 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
8172 err(1, "can't disable alarm");
8180 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
8181 for (i = 0; i < num_screens; ++i) {
8182 if (screens[i].bar_gc != 0)
8183 xcb_free_gc(conn, screens[i].bar_gc);
8184 if (!bar_font_legacy)
8185 XftColorFree(display, DefaultVisual(display, i),
8186 DefaultColormap(display, i), &bar_font_color);
8189 if (bar_font_legacy)
8190 XFreeFontSet(display, bar_fs);
8192 XftFontClose(display, bar_font);
8195 xcb_key_symbols_free(syms);
8197 xcb_disconnect(conn);
8201 event_error(xcb_generic_error_t *e)
8205 DNPRINTF(SWM_D_EVENT, "event_error: %s(%u) from %s(%u), sequence: %u, "
8206 "resource_id: %u, minor_code: %u\n",
8207 xcb_event_get_error_label(e->error_code), e->error_code,
8208 xcb_event_get_request_label(e->major_code), e->major_code,
8209 e->sequence, e->resource_id, e->minor_code);
8213 event_handle(xcb_generic_event_t *evt)
8215 uint8_t type = XCB_EVENT_RESPONSE_TYPE(evt);
8217 DNPRINTF(SWM_D_EVENT, "XCB Event: %s(%d), seq %u\n",
8218 xcb_event_get_label(XCB_EVENT_RESPONSE_TYPE(evt)),
8219 XCB_EVENT_RESPONSE_TYPE(evt), evt->sequence);
8222 #define EVENT(type, callback) case type: callback((void *)evt); return
8223 EVENT(0, event_error);
8224 EVENT(XCB_BUTTON_PRESS, buttonpress);
8225 /*EVENT(XCB_BUTTON_RELEASE, buttonpress);*/
8226 /*EVENT(XCB_CIRCULATE_NOTIFY, );*/
8227 /*EVENT(XCB_CIRCULATE_REQUEST, );*/
8228 EVENT(XCB_CLIENT_MESSAGE, clientmessage);
8229 /*EVENT(XCB_COLORMAP_NOTIFY, );*/
8230 EVENT(XCB_CONFIGURE_NOTIFY, configurenotify);
8231 EVENT(XCB_CONFIGURE_REQUEST, configurerequest);
8232 /*EVENT(XCB_CREATE_NOTIFY, );*/
8233 EVENT(XCB_DESTROY_NOTIFY, destroynotify);
8234 EVENT(XCB_ENTER_NOTIFY, enternotify);
8235 EVENT(XCB_EXPOSE, expose);
8237 EVENT(XCB_FOCUS_IN, focusin);
8238 EVENT(XCB_FOCUS_OUT, focusout);
8240 /*EVENT(XCB_GRAPHICS_EXPOSURE, );*/
8241 /*EVENT(XCB_GRAVITY_NOTIFY, );*/
8242 EVENT(XCB_KEY_PRESS, keypress);
8243 /*EVENT(XCB_KEY_RELEASE, keypress);*/
8244 /*EVENT(XCB_KEYMAP_NOTIFY, );*/
8246 EVENT(XCB_LEAVE_NOTIFY, leavenotify);
8248 EVENT(XCB_MAP_NOTIFY, mapnotify);
8249 EVENT(XCB_MAP_REQUEST, maprequest);
8250 EVENT(XCB_MAPPING_NOTIFY, mappingnotify);
8251 /*EVENT(XCB_MOTION_NOTIFY, );*/
8252 /*EVENT(XCB_NO_EXPOSURE, );*/
8253 EVENT(XCB_PROPERTY_NOTIFY, propertynotify);
8254 /*EVENT(XCB_REPARENT_NOTIFY, );*/
8255 /*EVENT(XCB_RESIZE_REQUEST, );*/
8256 /*EVENT(XCB_SELECTION_CLEAR, );*/
8257 /*EVENT(XCB_SELECTION_NOTIFY, );*/
8258 /*EVENT(XCB_SELECTION_REQUEST, );*/
8259 EVENT(XCB_UNMAP_NOTIFY, unmapnotify);
8260 /*EVENT(XCB_VISIBILITY_NOTIFY, visibilitynotify);*/
8263 if (type - xrandr_eventbase == XCB_RANDR_SCREEN_CHANGE_NOTIFY)
8264 screenchange((void *)evt);
8268 main(int argc, char *argv[])
8270 struct swm_region *r, *rr;
8271 struct ws_win *winfocus = NULL;
8272 char conf[PATH_MAX], *cfile = NULL;
8274 int xfd, i, num_screens;
8275 struct sigaction sact;
8276 xcb_generic_event_t *evt;
8280 /* suppress unused warning since var is needed */
8283 time_started = time(NULL);
8286 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION, buildstr);
8287 if (!setlocale(LC_CTYPE, "") || !setlocale(LC_TIME, ""))
8288 warnx("no locale support");
8290 /* handle some signals */
8291 bzero(&sact, sizeof(sact));
8292 sigemptyset(&sact.sa_mask);
8294 sact.sa_handler = sighdlr;
8295 sigaction(SIGINT, &sact, NULL);
8296 sigaction(SIGQUIT, &sact, NULL);
8297 sigaction(SIGTERM, &sact, NULL);
8298 sigaction(SIGHUP, &sact, NULL);
8300 sact.sa_handler = sighdlr;
8301 sact.sa_flags = SA_NOCLDSTOP;
8302 sigaction(SIGCHLD, &sact, NULL);
8304 if (!(display = XOpenDisplay(0)))
8305 errx(1, "can not open display");
8307 conn = XGetXCBConnection(display);
8308 if (xcb_connection_has_error(conn))
8309 errx(1, "can not get XCB connection");
8311 XSetEventQueueOwner(display, XCBOwnsEventQueue);
8313 xcb_prefetch_extension_data(conn, &xcb_randr_id);
8314 xfd = xcb_get_file_descriptor(conn);
8316 /* look for local and global conf file */
8317 pwd = getpwuid(getuid());
8319 errx(1, "invalid user: %d", getuid());
8321 xcb_grab_server(conn);
8324 /* flush all events */
8325 while ((evt = xcb_poll_for_event(conn))) {
8326 if (XCB_EVENT_RESPONSE_TYPE(evt) == 0)
8331 if (enable_wm() != 0)
8332 errx(1, "another window manager is currently running");
8343 for (i = 0; ; i++) {
8348 snprintf(conf, sizeof conf, "%s/.%s",
8349 pwd->pw_dir, SWM_CONF_FILE);
8353 snprintf(conf, sizeof conf, "/etc/%s",
8358 snprintf(conf, sizeof conf, "%s/.%s",
8359 pwd->pw_dir, SWM_CONF_FILE_OLD);
8363 snprintf(conf, sizeof conf, "/etc/%s",
8370 if (strlen(conf) && stat(conf, &sb) != -1)
8371 if (S_ISREG(sb.st_mode)) {
8378 /* load conf (if any) */
8380 conf_load(cfile, SWM_CONF_DEFAULT);
8383 /* set some values to work around bad programs */
8385 /* grab existing windows (before we build the bars) */
8388 if (getenv("SWM_STARTED") == NULL)
8389 setenv("SWM_STARTED", "YES", 1);
8391 /* setup all bars */
8392 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
8393 for (i = 0; i < num_screens; i++)
8394 TAILQ_FOREACH(r, &screens[i].rl, entry) {
8395 if (winfocus == NULL)
8396 winfocus = TAILQ_FIRST(&r->ws->winlist);
8403 xcb_ungrab_server(conn);
8407 while ((evt = xcb_poll_for_event(conn))) {
8414 /* if we are being restarted go focus on first window */
8416 rr = winfocus->ws->r;
8418 /* not a visible window */
8422 /* move pointer to first screen if multi screen */
8423 if (num_screens > 1 || outputs > 1)
8424 xcb_warp_pointer(conn, XCB_WINDOW_NONE,
8425 rr->s[0].root, 0, 0, 0, 0, X(rr),
8426 Y(rr) + (bar_enabled ? bar_height : 0));
8428 focus_win(get_region_focus(rr));
8438 if (select(xfd + 1, &rd, NULL, NULL, &tv) == -1)
8439 if (errno != EINTR) {
8440 DNPRINTF(SWM_D_MISC, "select failed");
8442 if (restart_wm == 1)
8443 restart(NULL, NULL);
8444 if (search_resp == 1)