2 * Copyright (c) 2009-2012 Marco Peereboom <marco@peereboom.us>
3 * Copyright (c) 2009-2011 Ryan McBride <mcbride@countersiege.com>
4 * Copyright (c) 2009 Darrin Chandler <dwchandler@stilyagin.com>
5 * Copyright (c) 2009 Pierre-Yves Ritschard <pyr@spootnik.org>
6 * Copyright (c) 2010 Tuukka Kataja <stuge@xor.fi>
7 * Copyright (c) 2011 Jason L. Wright <jason@thought.net>
8 * Copyright (c) 2011-2012 Reginald Kennedy <rk@rejii.com>
9 * Copyright (c) 2011-2012 Lawrence Teo <lteo@lteo.net>
10 * Copyright (c) 2011-2012 Tiago Cunha <tcunha@gmx.com>
11 * Copyright (c) 2012 David Hill <dhill@mindcry.org>
13 * Permission to use, copy, modify, and distribute this software for any
14 * purpose with or without fee is hereby granted, provided that the above
15 * copyright notice and this permission notice appear in all copies.
17 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
18 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
20 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
21 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
22 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
23 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 * Much code and ideas taken from dwm under the following license:
27 * MIT/X Consortium License
29 * 2006-2008 Anselm R Garbe <garbeam at gmail dot com>
30 * 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
31 * 2006-2007 Jukka Salmi <jukka at salmi dot ch>
32 * 2007 Premysl Hruby <dfenze at gmail dot com>
33 * 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
34 * 2007 Christof Musik <christof at sendfax dot de>
35 * 2007-2008 Enno Gottox Boland <gottox at s01 dot de>
36 * 2007-2008 Peter Hartlich <sgkkr at hartlich dot com>
37 * 2008 Martin Hurton <martin dot hurton at gmail dot com>
39 * Permission is hereby granted, free of charge, to any person obtaining a
40 * copy of this software and associated documentation files (the "Software"),
41 * to deal in the Software without restriction, including without limitation
42 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
43 * and/or sell copies of the Software, and to permit persons to whom the
44 * Software is furnished to do so, subject to the following conditions:
46 * The above copyright notice and this permission notice shall be included in
47 * all copies or substantial portions of the Software.
49 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
50 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
51 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
52 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
53 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
54 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
55 * DEALINGS IN THE SOFTWARE.
59 #include <sys/types.h>
63 #include <sys/queue.h>
64 #include <sys/param.h>
65 #include <sys/select.h>
66 #if defined(__linux__)
68 #elif defined(__OpenBSD__)
70 #elif defined(__FreeBSD__)
91 #include <X11/cursorfont.h>
92 #include <X11/extensions/Xrandr.h>
93 #include <X11/Xcursor/Xcursor.h>
94 #include <X11/Xft/Xft.h>
95 #include <X11/Xlib-xcb.h>
96 #include <xcb/xcb_atom.h>
97 #include <xcb/xcb_aux.h>
98 #include <xcb/xcb_event.h>
99 #include <xcb/xcb_icccm.h>
100 #include <xcb/xcb_keysyms.h>
101 #include <xcb/xtest.h>
102 #include <xcb/randr.h>
110 #ifdef SPECTRWM_BUILDSTR
111 static const char *buildstr = SPECTRWM_BUILDSTR;
113 static const char *buildstr = SPECTRWM_VERSION;
116 #if !defined(__CYGWIN__) /* cygwin chokes on xrandr stuff */
118 # error XRandR versions less than 1.0 are not supported
121 # if RANDR_MAJOR >= 1
122 # if RANDR_MINOR >= 2
123 # define SWM_XRR_HAS_CRTC
126 #endif /* __CYGWIN__ */
128 #ifndef XCB_ICCCM_NUM_WM_HINTS_ELEMENTS
129 #define XCB_ICCCM_SIZE_HINT_P_MIN_SIZE XCB_SIZE_HINT_P_MIN_SIZE
130 #define XCB_ICCCM_SIZE_HINT_P_MAX_SIZE XCB_SIZE_HINT_P_MAX_SIZE
131 #define XCB_ICCCM_SIZE_HINT_P_RESIZE_INC XCB_SIZE_HINT_P_RESIZE_INC
132 #define XCB_ICCCM_WM_HINT_INPUT XCB_WM_HINT_INPUT
133 #define XCB_ICCCM_WM_HINT_X_URGENCY XCB_WM_HINT_X_URGENCY
134 #define XCB_ICCCM_WM_STATE_ICONIC XCB_WM_STATE_ICONIC
135 #define XCB_ICCCM_WM_STATE_WITHDRAWN XCB_WM_STATE_WITHDRAWN
136 #define XCB_ICCCM_WM_STATE_NORMAL XCB_WM_STATE_NORMAL
137 #define xcb_icccm_get_text_property_reply_t xcb_get_text_property_reply_t
138 #define xcb_icccm_get_text_property_reply_wipe xcb_get_text_property_reply_wipe
139 #define xcb_icccm_get_wm_class xcb_get_wm_class
140 #define xcb_icccm_get_wm_class_reply xcb_get_wm_class_reply
141 #define xcb_icccm_get_wm_class_reply_t xcb_get_wm_class_reply_t
142 #define xcb_icccm_get_wm_class_reply_wipe xcb_get_wm_class_reply_wipe
143 #define xcb_icccm_get_wm_hints xcb_get_wm_hints
144 #define xcb_icccm_get_wm_hints_reply xcb_get_wm_hints_reply
145 #define xcb_icccm_get_wm_name xcb_get_wm_name
146 #define xcb_icccm_get_wm_name_reply xcb_get_wm_name_reply
147 #define xcb_icccm_get_wm_normal_hints xcb_get_wm_normal_hints
148 #define xcb_icccm_get_wm_normal_hints_reply xcb_get_wm_normal_hints_reply
149 #define xcb_icccm_get_wm_protocols xcb_get_wm_protocols
150 #define xcb_icccm_get_wm_protocols_reply xcb_get_wm_protocols_reply
151 #define xcb_icccm_get_wm_protocols_reply_t xcb_get_wm_protocols_reply_t
152 #define xcb_icccm_get_wm_protocols_reply_wipe xcb_get_wm_protocols_reply_wipe
153 #define xcb_icccm_get_wm_transient_for xcb_get_wm_transient_for
154 #define xcb_icccm_get_wm_transient_for_reply xcb_get_wm_transient_for_reply
155 #define xcb_icccm_wm_hints_t xcb_wm_hints_t
158 /*#define SWM_DEBUG*/
160 #define DPRINTF(x...) do { \
162 fprintf(stderr, x); \
164 #define DNPRINTF(n,x...) do { \
165 if (swm_debug & n) { \
166 fprintf(stderr, "%ld ", (long)(time(NULL) - time_started)); \
167 fprintf(stderr, x); \
170 #define SWM_D_MISC 0x0001
171 #define SWM_D_EVENT 0x0002
172 #define SWM_D_WS 0x0004
173 #define SWM_D_FOCUS 0x0008
174 #define SWM_D_MOVE 0x0010
175 #define SWM_D_STACK 0x0020
176 #define SWM_D_MOUSE 0x0040
177 #define SWM_D_PROP 0x0080
178 #define SWM_D_CLASS 0x0100
179 #define SWM_D_KEY 0x0200
180 #define SWM_D_QUIRK 0x0400
181 #define SWM_D_SPAWN 0x0800
182 #define SWM_D_EVENTQ 0x1000
183 #define SWM_D_CONF 0x2000
184 #define SWM_D_BAR 0x4000
185 #define SWM_D_INIT 0x8000
187 u_int32_t swm_debug = 0
206 #define DPRINTF(x...)
207 #define DNPRINTF(n,x...)
210 /* convert 8-bit to 16-bit */
211 #define RGB_8_TO_16(col) ((col) << 8) + (col)
213 #define PIXEL_TO_XRENDERCOLOR(px, xrc) \
214 xrc.red = RGB_8_TO_16((px) >> 16 & 0xff); \
215 xrc.green = RGB_8_TO_16((px) >> 8 & 0xff); \
216 xrc.blue = RGB_8_TO_16((px) & 0xff); \
219 #define LENGTH(x) (int)(sizeof (x) / sizeof (x)[0])
220 #define MODKEY XCB_MOD_MASK_1
221 #define CLEANMASK(mask) ((mask) & ~(numlockmask | XCB_MOD_MASK_LOCK))
222 #define BUTTONMASK (XCB_EVENT_MASK_BUTTON_PRESS | \
223 XCB_EVENT_MASK_BUTTON_RELEASE)
224 #define MOUSEMASK (BUTTONMASK|XCB_EVENT_MASK_POINTER_MOTION)
225 #define SWM_PROPLEN (16)
226 #define SWM_FUNCNAME_LEN (32)
227 #define SWM_KEYS_LEN (255)
228 #define SWM_QUIRK_LEN (64)
229 #define X(r) (r)->g.x
230 #define Y(r) (r)->g.y
231 #define WIDTH(r) (r)->g.w
232 #define HEIGHT(r) (r)->g.h
233 #define BORDER(w) ((w)->bordered ? border_width : 0)
234 #define MAX_X(r) ((r)->g.x + (r)->g.w)
235 #define MAX_Y(r) ((r)->g.y + (r)->g.h)
236 #define SH_MIN(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE
237 #define SH_MIN_W(w) (w)->sh.min_width
238 #define SH_MIN_H(w) (w)->sh.min_height
239 #define SH_MAX(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MAX_SIZE
240 #define SH_MAX_W(w) (w)->sh.max_width
241 #define SH_MAX_H(w) (w)->sh.max_height
242 #define SH_INC(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_RESIZE_INC
243 #define SH_INC_W(w) (w)->sh.width_inc
244 #define SH_INC_H(w) (w)->sh.height_inc
245 #define SWM_MAX_FONT_STEPS (3)
246 #define WINID(w) ((w) ? (w)->id : 0)
247 #define YESNO(x) ((x) ? "yes" : "no")
249 #define SWM_FOCUS_DEFAULT (0)
250 #define SWM_FOCUS_FOLLOW (1)
251 #define SWM_FOCUS_MANUAL (2)
253 #define SWM_CK_NONE 0
254 #define SWM_CK_ALL 0x7
255 #define SWM_CK_FOCUS 0x1
256 #define SWM_CK_POINTER 0x2
257 #define SWM_CK_FALLBACK 0x4
259 #define SWM_CONF_DEFAULT (0)
260 #define SWM_CONF_KEYMAPPING (1)
263 #define SWM_LIB "/usr/local/lib/libswmhack.so"
270 xcb_atom_t a_takefocus;
272 xcb_atom_t a_netwmname;
273 xcb_atom_t a_utf8_string;
275 xcb_atom_t a_swm_iconic;
277 volatile sig_atomic_t running = 1;
278 volatile sig_atomic_t restart_wm = 0;
279 xcb_timestamp_t last_event_time = 0;
283 int xrandr_eventbase;
284 unsigned int numlockmask = 0;
287 xcb_connection_t *conn;
288 xcb_key_symbols_t *syms;
291 int cycle_visible = 0;
293 int font_adjusted = 0;
294 unsigned int mod_key = MODKEY;
297 struct swm_region *search_r;
298 int select_list_pipe[2];
299 int select_resp_pipe[2];
301 volatile sig_atomic_t search_resp;
302 int search_resp_action;
304 struct search_window {
305 TAILQ_ENTRY(search_window) entry;
309 xcb_window_t indicator;
311 TAILQ_HEAD(search_winlist, search_window);
312 struct search_winlist search_wl;
317 SWM_SEARCH_UNICONIFY,
318 SWM_SEARCH_NAME_WORKSPACE,
319 SWM_SEARCH_SEARCH_WORKSPACE,
320 SWM_SEARCH_SEARCH_WINDOW
323 #define SWM_STACK_TOP (0)
324 #define SWM_STACK_BOTTOM (1)
325 #define SWM_STACK_ABOVE (2)
326 #define SWM_STACK_BELOW (3)
329 double dialog_ratio = 0.6;
331 #define SWM_BAR_MAX (256)
332 #define SWM_BAR_JUSTIFY_LEFT (0)
333 #define SWM_BAR_JUSTIFY_CENTER (1)
334 #define SWM_BAR_JUSTIFY_RIGHT (2)
335 #define SWM_BAR_OFFSET (4)
336 #define SWM_BAR_FONTS "-*-terminus-medium-*-*-*-12-*-*-*-*-*-*-*," \
337 "-*-profont-*-*-*-*-12-*-*-*-*-*-*-*," \
338 "-*-times-medium-r-*-*-12-*-*-*-*-*-*-*," \
339 "-misc-fixed-medium-r-*-*-12-*-*-*-*-*-*-*," \
340 "-*-*-*-r-*-*-*-*-*-*-*-*-*-*"
342 #ifdef X_HAVE_UTF8_STRING
343 #define DRAWSTRING(x...) Xutf8DrawString(x)
345 #define DRAWSTRING(x...) XmbDrawString(x)
348 char *bar_argv[] = { NULL, NULL };
350 char bar_ext[SWM_BAR_MAX];
351 char bar_ext_buf[SWM_BAR_MAX];
352 char bar_vertext[SWM_BAR_MAX];
355 int bar_border_width = 1;
356 int bar_at_bottom = 0;
358 int bar_extra_running = 0;
361 int bar_justify = SWM_BAR_JUSTIFY_LEFT;
362 char *bar_format = NULL;
363 int stack_enabled = 1;
364 int clock_enabled = 1;
365 int urgent_enabled = 0;
366 char *clock_format = NULL;
367 int title_name_enabled = 0;
368 int title_class_enabled = 0;
369 int window_name_enabled = 0;
370 int focus_mode = SWM_FOCUS_DEFAULT;
371 int focus_close = SWM_STACK_BELOW;
372 int focus_close_wrap = 1;
373 int focus_default = SWM_STACK_TOP;
374 int spawn_position = SWM_STACK_TOP;
375 int disable_border = 0;
376 int border_width = 1;
377 int verbose_layout = 0;
381 XFontSetExtents *bar_fs_extents;
383 int bar_font_legacy = 1;
385 XftColor bar_font_color;
388 /* layout manager data */
389 struct swm_geometry {
402 struct swm_geometry g;
405 /* virtual "screens" */
407 TAILQ_ENTRY(swm_region) entry;
408 struct swm_geometry g;
409 struct workspace *ws; /* current workspace on this region */
410 struct workspace *ws_prior; /* prior workspace on this region */
411 struct swm_screen *s; /* screen idx */
414 TAILQ_HEAD(swm_region_list, swm_region);
417 TAILQ_ENTRY(ws_win) entry;
419 xcb_window_t transient;
420 struct ws_win *focus_child; /* focus on child transient */
421 struct swm_geometry g; /* current geometry */
422 struct swm_geometry g_float; /* region coordinates */
423 int g_floatvalid; /* g_float geometry validity */
424 int floatmaxed; /* whether maxed by max_stack */
430 unsigned int ewmh_flags;
431 int font_size_boundary[SWM_MAX_FONT_STEPS];
437 unsigned long quirks;
438 struct workspace *ws; /* always valid */
439 struct swm_screen *s; /* always valid, never changes */
440 xcb_get_geometry_reply_t *wa;
442 xcb_icccm_get_wm_class_reply_t ch;
443 xcb_icccm_wm_hints_t hints;
445 TAILQ_HEAD(ws_win_list, ws_win);
449 TAILQ_ENTRY(pid_e) entry;
453 TAILQ_HEAD(pid_list, pid_e);
454 struct pid_list pidlist = TAILQ_HEAD_INITIALIZER(pidlist);
456 /* layout handlers */
458 void vertical_config(struct workspace *, int);
459 void vertical_stack(struct workspace *, struct swm_geometry *);
460 void horizontal_config(struct workspace *, int);
461 void horizontal_stack(struct workspace *, struct swm_geometry *);
462 void max_stack(struct workspace *, struct swm_geometry *);
463 void plain_stacker(struct workspace *);
464 void fancy_stacker(struct workspace *);
467 void (*l_stack)(struct workspace *, struct swm_geometry *);
468 void (*l_config)(struct workspace *, int);
470 #define SWM_L_FOCUSPREV (1<<0)
471 #define SWM_L_MAPONFOCUS (1<<1)
472 void (*l_string)(struct workspace *);
474 /* stack, configure */
475 { vertical_stack, vertical_config, 0, plain_stacker },
476 { horizontal_stack, horizontal_config, 0, plain_stacker },
478 SWM_L_MAPONFOCUS | SWM_L_FOCUSPREV, plain_stacker },
479 { NULL, NULL, 0, NULL },
482 /* position of max_stack mode in the layouts array, index into layouts! */
483 #define SWM_V_STACK (0)
484 #define SWM_H_STACK (1)
485 #define SWM_MAX_STACK (2)
487 #define SWM_H_SLICE (32)
488 #define SWM_V_SLICE (32)
490 /* define work spaces */
492 int idx; /* workspace index */
493 char *name; /* workspace name */
494 int always_raise; /* raise windows on focus */
495 int bar_enabled; /* bar visibility */
496 struct layout *cur_layout; /* current layout handlers */
497 struct ws_win *focus; /* may be NULL */
498 struct ws_win *focus_prev; /* may be NULL */
499 struct ws_win *focus_pending; /* may be NULL */
500 struct swm_region *r; /* may be NULL */
501 struct swm_region *old_r; /* may be NULL */
502 struct ws_win_list winlist; /* list of windows in ws */
503 struct ws_win_list unmanagedlist; /* list of dead windows in ws */
504 char stacker[10]; /* display stacker and layout */
508 int horizontal_msize;
510 int horizontal_stacks;
521 SWM_S_COLOR_BAR_BORDER,
522 SWM_S_COLOR_BAR_FONT,
528 /* physical screen mapping */
529 #define SWM_WS_MAX (22) /* hard limit */
530 int workspace_limit = 10; /* soft limit */
533 int idx; /* screen index */
534 struct swm_region_list rl; /* list of regions on this screen */
535 struct swm_region_list orl; /* list of old regions */
537 struct workspace ws[SWM_WS_MAX];
543 } c[SWM_S_COLOR_MAX];
545 xcb_gcontext_t bar_gc;
548 struct swm_screen *screens;
550 /* args to functions */
553 #define SWM_ARG_ID_FOCUSNEXT (0)
554 #define SWM_ARG_ID_FOCUSPREV (1)
555 #define SWM_ARG_ID_FOCUSMAIN (2)
556 #define SWM_ARG_ID_SWAPNEXT (10)
557 #define SWM_ARG_ID_SWAPPREV (11)
558 #define SWM_ARG_ID_SWAPMAIN (12)
559 #define SWM_ARG_ID_MOVELAST (13)
560 #define SWM_ARG_ID_MASTERSHRINK (20)
561 #define SWM_ARG_ID_MASTERGROW (21)
562 #define SWM_ARG_ID_MASTERADD (22)
563 #define SWM_ARG_ID_MASTERDEL (23)
564 #define SWM_ARG_ID_FLIPLAYOUT (24)
565 #define SWM_ARG_ID_STACKRESET (30)
566 #define SWM_ARG_ID_STACKINIT (31)
567 #define SWM_ARG_ID_CYCLEWS_UP (40)
568 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
569 #define SWM_ARG_ID_CYCLESC_UP (42)
570 #define SWM_ARG_ID_CYCLESC_DOWN (43)
571 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
572 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
573 #define SWM_ARG_ID_STACKINC (50)
574 #define SWM_ARG_ID_STACKDEC (51)
575 #define SWM_ARG_ID_SS_ALL (60)
576 #define SWM_ARG_ID_SS_WINDOW (61)
577 #define SWM_ARG_ID_DONTCENTER (70)
578 #define SWM_ARG_ID_CENTER (71)
579 #define SWM_ARG_ID_KILLWINDOW (80)
580 #define SWM_ARG_ID_DELETEWINDOW (81)
581 #define SWM_ARG_ID_WIDTHGROW (90)
582 #define SWM_ARG_ID_WIDTHSHRINK (91)
583 #define SWM_ARG_ID_HEIGHTGROW (92)
584 #define SWM_ARG_ID_HEIGHTSHRINK (93)
585 #define SWM_ARG_ID_MOVEUP (100)
586 #define SWM_ARG_ID_MOVEDOWN (101)
587 #define SWM_ARG_ID_MOVELEFT (102)
588 #define SWM_ARG_ID_MOVERIGHT (103)
589 #define SWM_ARG_ID_BAR_TOGGLE (110)
590 #define SWM_ARG_ID_BAR_TOGGLE_WS (111)
596 TAILQ_ENTRY(quirk) entry;
600 #define SWM_Q_FLOAT (1<<0) /* float this window */
601 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
602 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
603 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
604 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
605 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
607 TAILQ_HEAD(quirk_list, quirk);
608 struct quirk_list quirks = TAILQ_HEAD_INITIALIZER(quirks);
611 * Supported EWMH hints should be added to
612 * both the enum and the ewmh array
617 _NET_MOVERESIZE_WINDOW,
618 _NET_WM_ACTION_ABOVE,
619 _NET_WM_ACTION_CLOSE,
620 _NET_WM_ACTION_FULLSCREEN,
622 _NET_WM_ACTION_RESIZE,
623 _NET_WM_ALLOWED_ACTIONS,
627 _NET_WM_STATE_FULLSCREEN,
628 _NET_WM_STATE_HIDDEN,
629 _NET_WM_STATE_MAXIMIZED_HORZ,
630 _NET_WM_STATE_MAXIMIZED_VERT,
631 _NET_WM_STATE_SKIP_PAGER,
632 _NET_WM_STATE_SKIP_TASKBAR,
634 _NET_WM_WINDOW_TYPE_DIALOG,
635 _NET_WM_WINDOW_TYPE_DOCK,
636 _NET_WM_WINDOW_TYPE_NORMAL,
637 _NET_WM_WINDOW_TYPE_SPLASH,
638 _NET_WM_WINDOW_TYPE_TOOLBAR,
639 _NET_WM_WINDOW_TYPE_UTILITY,
640 _SWM_WM_STATE_MANUAL,
647 } ewmh[SWM_EWMH_HINT_MAX] = {
648 /* must be in same order as in the enum */
649 {"_NET_ACTIVE_WINDOW", XCB_ATOM_NONE},
650 {"_NET_CLOSE_WINDOW", XCB_ATOM_NONE},
651 {"_NET_MOVERESIZE_WINDOW", XCB_ATOM_NONE},
652 {"_NET_WM_ACTION_ABOVE", XCB_ATOM_NONE},
653 {"_NET_WM_ACTION_CLOSE", XCB_ATOM_NONE},
654 {"_NET_WM_ACTION_FULLSCREEN", XCB_ATOM_NONE},
655 {"_NET_WM_ACTION_MOVE", XCB_ATOM_NONE},
656 {"_NET_WM_ACTION_RESIZE", XCB_ATOM_NONE},
657 {"_NET_WM_ALLOWED_ACTIONS", XCB_ATOM_NONE},
658 {"_NET_WM_NAME", XCB_ATOM_NONE},
659 {"_NET_WM_STATE", XCB_ATOM_NONE},
660 {"_NET_WM_STATE_ABOVE", XCB_ATOM_NONE},
661 {"_NET_WM_STATE_FULLSCREEN", XCB_ATOM_NONE},
662 {"_NET_WM_STATE_HIDDEN", XCB_ATOM_NONE},
663 {"_NET_WM_STATE_MAXIMIZED_HORZ", XCB_ATOM_NONE},
664 {"_NET_WM_STATE_MAXIMIZED_VERT", XCB_ATOM_NONE},
665 {"_NET_WM_STATE_SKIP_PAGER", XCB_ATOM_NONE},
666 {"_NET_WM_STATE_SKIP_TASKBAR", XCB_ATOM_NONE},
667 {"_NET_WM_WINDOW_TYPE", XCB_ATOM_NONE},
668 {"_NET_WM_WINDOW_TYPE_DIALOG", XCB_ATOM_NONE},
669 {"_NET_WM_WINDOW_TYPE_DOCK", XCB_ATOM_NONE},
670 {"_NET_WM_WINDOW_TYPE_NORMAL", XCB_ATOM_NONE},
671 {"_NET_WM_WINDOW_TYPE_SPLASH", XCB_ATOM_NONE},
672 {"_NET_WM_WINDOW_TYPE_TOOLBAR", XCB_ATOM_NONE},
673 {"_NET_WM_WINDOW_TYPE_UTILITY", XCB_ATOM_NONE},
674 {"_SWM_WM_STATE_MANUAL", XCB_ATOM_NONE},
681 XC_BOTTOM_LEFT_CORNER,
682 XC_BOTTOM_RIGHT_CORNER,
690 char *name; /* Name used by Xcursor .*/
691 uint8_t cf_char; /* cursorfont index. */
693 } cursors[XC_MAX] = {
694 {"fleur", XC_fleur, XCB_CURSOR_NONE},
695 {"left_ptr", XC_left_ptr, XCB_CURSOR_NONE},
696 {"bottom_left_corner", XC_bottom_left_corner, XCB_CURSOR_NONE},
697 {"bottom_right_corner", XC_bottom_right_corner, XCB_CURSOR_NONE},
698 {"sizing", XC_sizing, XCB_CURSOR_NONE},
699 {"top_left_corner", XC_top_left_corner, XCB_CURSOR_NONE},
700 {"top_right_corner", XC_top_right_corner, XCB_CURSOR_NONE},
705 TAILQ_ENTRY(spawn_prog) entry;
710 TAILQ_HEAD(spawn_list, spawn_prog);
711 struct spawn_list spawns = TAILQ_HEAD_INITIALIZER(spawns);
713 /* user/key callable function IDs */
805 KF_DUMPWINS, /* MUST BE LAST */
813 enum keyfuncid funcid;
816 RB_HEAD(key_tree, key);
818 /* function prototypes */
819 void adjust_font(struct ws_win *);
820 void bar_class_name(char *, size_t, struct swm_region *);
821 void bar_class_title_name(char *, size_t, struct swm_region *);
822 void bar_cleanup(struct swm_region *);
823 void bar_extra_setup(void);
824 void bar_extra_stop(void);
825 void bar_extra_update(void);
826 void bar_fmt(const char *, char *, struct swm_region *, size_t);
827 void bar_fmt_expand(char *, size_t);
829 void bar_print(struct swm_region *, const char *);
830 void bar_print_legacy(struct swm_region *, const char *);
831 void bar_replace(char *, char *, struct swm_region *, size_t);
832 void bar_replace_pad(char *, int *, size_t);
833 char * bar_replace_seq(char *, char *, struct swm_region *, size_t *,
835 void bar_setup(struct swm_region *);
836 void bar_title_name(char *, size_t, struct swm_region *);
837 void bar_toggle(struct swm_region *, union arg *);
838 void bar_urgent(char *, size_t);
839 void bar_window_float(char *, size_t, struct swm_region *);
840 void bar_window_name(char *, size_t, struct swm_region *);
841 void bar_workspace_name(char *, size_t, struct swm_region *);
842 void buttonpress(xcb_button_press_event_t *);
843 void check_conn(void);
844 void clear_keys(void);
845 void clientmessage(xcb_client_message_event_t *);
846 void client_msg(struct ws_win *, xcb_atom_t, xcb_timestamp_t);
847 int conf_load(const char *, int);
848 void configurenotify(xcb_configure_notify_event_t *);
849 void configurerequest(xcb_configure_request_event_t *);
850 void config_win(struct ws_win *, xcb_configure_request_event_t *);
851 void constrain_window(struct ws_win *, struct swm_region *, int);
852 int count_win(struct workspace *, int);
853 void cursors_cleanup(void);
854 void cursors_load(void);
855 void custom_region(char *);
856 void cyclescr(struct swm_region *, union arg *);
857 void cyclews(struct swm_region *, union arg *);
858 void cycle_layout(struct swm_region *, union arg *);
859 void destroynotify(xcb_destroy_notify_event_t *);
860 void dumpwins(struct swm_region *, union arg *);
862 void enternotify(xcb_enter_notify_event_t *);
863 void event_drain(uint8_t);
864 void event_error(xcb_generic_error_t *);
865 void event_handle(xcb_generic_event_t *);
866 void ewmh_autoquirk(struct ws_win *);
867 void ewmh_get_win_state(struct ws_win *);
868 int ewmh_set_win_fullscreen(struct ws_win *, int);
869 void ewmh_update_actions(struct ws_win *);
870 void ewmh_update_win_state(struct ws_win *, xcb_atom_t, long);
871 char *expand_tilde(const char *);
872 void expose(xcb_expose_event_t *);
873 void fake_keypress(struct ws_win *, xcb_keysym_t, uint16_t);
874 struct pid_e *find_pid(pid_t);
875 struct ws_win *find_unmanaged_window(xcb_window_t);
876 struct ws_win *find_window(xcb_window_t);
877 void floating_toggle(struct swm_region *, union arg *);
878 int floating_toggle_win(struct ws_win *);
879 void focus(struct swm_region *, union arg *);
881 void focusin(xcb_focus_in_event_t *);
882 void focusout(xcb_focus_out_event_t *);
884 void focus_flush(void);
885 void focus_win(struct ws_win *);
886 void fontset_init(void);
887 void free_window(struct ws_win *);
888 xcb_atom_t get_atom_from_string(const char *);
890 char *get_atom_name(xcb_atom_t);
892 struct ws_win *get_focus_magic(struct ws_win *);
893 struct ws_win *get_focus_prev(struct ws_win *);
895 char *get_notify_detail_label(uint8_t);
896 char *get_notify_mode_label(uint8_t);
898 struct ws_win *get_pointer_win(xcb_window_t);
899 struct ws_win *get_region_focus(struct swm_region *);
900 xcb_screen_t *get_screen(int);
902 char *get_stack_mode_name(uint8_t);
904 int32_t get_swm_iconic(struct ws_win *);
905 char *get_win_name(xcb_window_t);
906 int get_ws_idx(xcb_window_t);
907 uint32_t getstate(xcb_window_t);
908 void grabbuttons(struct ws_win *);
910 void grab_windows(void);
911 void iconify(struct swm_region *, union arg *);
913 void keypress(xcb_key_press_event_t *);
914 int key_cmp(struct key *, struct key *);
915 void key_insert(unsigned int, KeySym, enum keyfuncid, const char *);
916 struct key *key_lookup(unsigned int, KeySym);
917 void key_remove(struct key *);
918 void key_replace(struct key *, unsigned int, KeySym, enum keyfuncid,
920 void kill_refs(struct ws_win *);
922 void leavenotify(xcb_leave_notify_event_t *);
924 void load_float_geom(struct ws_win *, struct swm_region *);
925 struct ws_win *manage_window(xcb_window_t, uint16_t);
926 void map_window(struct ws_win *);
927 void mapnotify(xcb_map_notify_event_t *);
928 void mappingnotify(xcb_mapping_notify_event_t *);
929 void maprequest(xcb_map_request_event_t *);
930 void move(struct ws_win *, union arg *);
931 void move_step(struct swm_region *, union arg *);
932 uint32_t name_to_pixel(const char *);
933 void name_workspace(struct swm_region *, union arg *);
934 void new_region(struct swm_screen *, int, int, int, int);
935 int parsekeys(char *, unsigned int, unsigned int *, KeySym *);
936 int parsequirks(char *, unsigned long *);
937 int parse_rgb(const char *, uint16_t *, uint16_t *, uint16_t *);
938 void pressbutton(struct swm_region *, union arg *);
939 void priorws(struct swm_region *, union arg *);
941 void print_win_geom(xcb_window_t);
943 void propertynotify(xcb_property_notify_event_t *);
944 void quirk_insert(const char *, const char *, unsigned long);
945 void quirk_remove(struct quirk *);
946 void quirk_replace(struct quirk *, const char *, const char *,
948 void quit(struct swm_region *, union arg *);
949 void raise_toggle(struct swm_region *, union arg *);
950 void resize(struct ws_win *, union arg *);
951 void resize_step(struct swm_region *, union arg *);
952 void restart(struct swm_region *, union arg *);
953 struct swm_region *root_to_region(xcb_window_t, int);
954 void screenchange(xcb_randr_screen_change_notify_event_t *);
955 void scan_xrandr(int);
956 void search_do_resp(void);
957 void search_resp_name_workspace(const char *, unsigned long);
958 void search_resp_search_window(const char *);
959 void search_resp_search_workspace(const char *);
960 void search_resp_uniconify(const char *, unsigned long);
961 void search_win(struct swm_region *, union arg *);
962 void search_win_cleanup(void);
963 void search_workspace(struct swm_region *, union arg *);
964 void send_to_ws(struct swm_region *, union arg *);
965 int setautorun(char *, char *, int);
966 int setconfbinding(char *, char *, int);
967 int setconfcolor(char *, char *, int);
968 int setconfmodkey(char *, char *, int);
969 int setconfquirk(char *, char *, int);
970 int setconfregion(char *, char *, int);
971 int setconfspawn(char *, char *, int);
972 int setconfvalue(char *, char *, int);
973 void setkeybinding(unsigned int, KeySym, enum keyfuncid, const char *);
974 int setkeymapping(char *, char *, int);
975 int setlayout(char *, char *, int);
976 void setquirk(const char *, const char *, unsigned long);
977 void setscreencolor(char *, int, int);
978 void setspawn(const char *, const char *);
979 void setup_ewmh(void);
980 void setup_globals(void);
981 void setup_keys(void);
982 void setup_quirks(void);
983 void setup_screens(void);
984 void setup_spawn(void);
985 void set_child_transient(struct ws_win *, xcb_window_t *);
986 void set_swm_iconic(struct ws_win *, int);
987 void set_win_state(struct ws_win *, uint16_t);
988 void shutdown_cleanup(void);
990 void socket_setnonblock(int);
991 void sort_windows(struct ws_win_list *);
992 void spawn(int, union arg *, int);
993 void spawn_custom(struct swm_region *, union arg *, const char *);
994 int spawn_expand(struct swm_region *, union arg *, const char *, char ***);
995 void spawn_insert(const char *, const char *);
996 void spawn_remove(struct spawn_prog *);
997 void spawn_replace(struct spawn_prog *, const char *, const char *);
998 void spawn_select(struct swm_region *, union arg *, const char *, int *);
999 void stack_config(struct swm_region *, union arg *);
1000 void stack_floater(struct ws_win *, struct swm_region *);
1001 void stack_master(struct workspace *, struct swm_geometry *, int, int);
1002 void store_float_geom(struct ws_win *, struct swm_region *);
1003 char * strdupsafe(const char *);
1004 void swapwin(struct swm_region *, union arg *);
1005 void switchws(struct swm_region *, union arg *);
1006 void teardown_ewmh(void);
1007 void unfocus_win(struct ws_win *);
1008 void uniconify(struct swm_region *, union arg *);
1009 void unmanage_window(struct ws_win *);
1010 void unmapnotify(xcb_unmap_notify_event_t *);
1011 void unmap_all(void);
1012 void unmap_window(struct ws_win *);
1013 void updatenumlockmask(void);
1014 void update_modkey(unsigned int);
1015 void update_window(struct ws_win *);
1016 int validate_win(struct ws_win *);
1017 int validate_ws(struct workspace *);
1018 /*void visibilitynotify(xcb_visibility_notify_event_t *);*/
1019 void version(struct swm_region *, union arg *);
1020 pid_t window_get_pid(xcb_window_t);
1021 void wkill(struct swm_region *, union arg *);
1022 void workaround(void);
1023 void xft_init(struct swm_region *);
1025 RB_PROTOTYPE(key_tree, key, entry, key_cmp);
1026 RB_GENERATE(key_tree, key, entry, key_cmp);
1027 struct key_tree keys;
1032 xcb_font_t cf = XCB_NONE;
1035 for (i = 0; i < LENGTH(cursors); ++i) {
1036 /* try to load Xcursor first. */
1037 cursors[i].cid = XcursorLibraryLoadCursor(display,
1040 /* fallback to cursorfont. */
1041 if (cursors[i].cid == XCB_CURSOR_NONE) {
1042 if (cf == XCB_NONE) {
1043 cf = xcb_generate_id(conn);
1044 xcb_open_font(conn, cf, strlen("cursor"),
1048 cursors[i].cid = xcb_generate_id(conn);
1049 xcb_create_glyph_cursor(conn, cursors[i].cid, cf, cf,
1050 cursors[i].cf_char, cursors[i].cf_char + 1, 0, 0, 0,
1051 0xffff, 0xffff, 0xffff);
1057 xcb_close_font(conn, cf);
1061 cursors_cleanup(void)
1064 for (i = 0; i < LENGTH(cursors); ++i)
1065 xcb_free_cursor(conn, cursors[i].cid);
1069 expand_tilde(const char *s)
1071 struct passwd *ppwd;
1079 errx(1, "expand_tilde: NULL string.");
1082 result = strdup(sc);
1088 if ((max = sysconf(_SC_LOGIN_NAME_MAX)) == -1)
1089 errx(1, "expand_tilde: sysconf");
1091 if ((user = calloc(1, max + 1)) == NULL)
1092 errx(1, "expand_tilde: calloc");
1094 for (i = 0; s[i] != '/' && s[i] != '\0'; ++i)
1099 ppwd = strlen(user) == 0 ? getpwuid(getuid()) : getpwnam(user);
1103 result = strdup(sc);
1105 if (asprintf(&result, "%s%s", ppwd->pw_dir, s) == -1)
1109 errx(1, "expand_tilde: failed to allocate memory.");
1115 parse_rgb(const char *rgb, uint16_t *rr, uint16_t *gg, uint16_t *bb)
1117 unsigned int tmpr, tmpg, tmpb;
1119 if (sscanf(rgb, "rgb:%x/%x/%x", &tmpr, &tmpg, &tmpb) != 3)
1122 *rr = RGB_8_TO_16(tmpr);
1123 *gg = RGB_8_TO_16(tmpg);
1124 *bb = RGB_8_TO_16(tmpb);
1130 get_screen(int screen)
1132 const xcb_setup_t *r;
1133 xcb_screen_iterator_t iter;
1135 if ((r = xcb_get_setup(conn)) == NULL) {
1136 DNPRINTF(SWM_D_MISC, "get_screen: xcb_get_setup\n");
1140 iter = xcb_setup_roots_iterator(r);
1141 for (; iter.rem; --screen, xcb_screen_next(&iter))
1151 if (focus_mode == SWM_FOCUS_DEFAULT)
1152 event_drain(XCB_ENTER_NOTIFY);
1158 get_atom_from_string(const char *str)
1160 xcb_intern_atom_cookie_t c;
1161 xcb_intern_atom_reply_t *r;
1164 c = xcb_intern_atom(conn, 0, strlen(str), str);
1165 r = xcb_intern_atom_reply(conn, c, NULL);
1173 return (XCB_ATOM_NONE);
1177 set_swm_iconic(struct ws_win *win, int newv)
1184 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
1185 a_swm_iconic, XCB_ATOM_INTEGER, 32, 1, &v);
1187 xcb_delete_property(conn, win->id, a_swm_iconic);
1191 get_swm_iconic(struct ws_win *win)
1194 xcb_get_property_reply_t *pr = NULL;
1196 pr = xcb_get_property_reply(conn,
1197 xcb_get_property(conn, 0, win->id, a_swm_iconic,
1198 XCB_ATOM_INTEGER, 0, 1), NULL);
1201 if (pr->type != XCB_ATOM_INTEGER || pr->format != 32)
1203 v = *((int32_t *)xcb_get_property_value(pr));
1213 xcb_atom_t sup_list;
1214 int i, j, num_screens;
1216 sup_list = get_atom_from_string("_NET_SUPPORTED");
1218 for (i = 0; i < LENGTH(ewmh); i++)
1219 ewmh[i].atom = get_atom_from_string(ewmh[i].name);
1221 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1222 for (i = 0; i < num_screens; i++) {
1223 /* Support check window will be created by workaround(). */
1225 /* Report supported atoms */
1226 xcb_delete_property(conn, screens[i].root, sup_list);
1227 for (j = 0; j < LENGTH(ewmh); j++)
1228 xcb_change_property(conn, XCB_PROP_MODE_APPEND,
1229 screens[i].root, sup_list, XCB_ATOM_ATOM, 32, 1,
1238 xcb_atom_t sup_check, sup_list;
1240 xcb_get_property_cookie_t pc;
1241 xcb_get_property_reply_t *pr;
1243 sup_check = get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
1244 sup_list = get_atom_from_string("_NET_SUPPORTED");
1245 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1247 for (i = 0; i < num_screens; i++) {
1248 /* Get the support check window and destroy it */
1249 pc = xcb_get_property(conn, 0, screens[i].root, sup_check,
1250 XCB_ATOM_WINDOW, 0, 1);
1251 pr = xcb_get_property_reply(conn, pc, NULL);
1254 if (pr->format == sup_check) {
1255 id = *((xcb_window_t *)xcb_get_property_value(pr));
1257 xcb_destroy_window(conn, id);
1258 xcb_delete_property(conn, screens[i].root, sup_check);
1259 xcb_delete_property(conn, screens[i].root, sup_list);
1266 ewmh_autoquirk(struct ws_win *win)
1270 xcb_get_property_cookie_t c;
1271 xcb_get_property_reply_t *r;
1273 c = xcb_get_property(conn, 0, win->id,
1274 ewmh[_NET_WM_WINDOW_TYPE].atom, XCB_ATOM_ATOM, 0, UINT32_MAX);
1275 r = xcb_get_property_reply(conn, c, NULL);
1279 n = xcb_get_property_value_length(r);
1280 type = xcb_get_property_value(r);
1282 for (i = 0; i < n; i++) {
1283 if (type[i] == ewmh[_NET_WM_WINDOW_TYPE_NORMAL].atom)
1285 if (type[i] == ewmh[_NET_WM_WINDOW_TYPE_DOCK].atom ||
1286 type[i] == ewmh[_NET_WM_WINDOW_TYPE_TOOLBAR].atom ||
1287 type[i] == ewmh[_NET_WM_WINDOW_TYPE_UTILITY].atom) {
1289 win->quirks = SWM_Q_FLOAT | SWM_Q_ANYWHERE;
1292 if (type[i] == ewmh[_NET_WM_WINDOW_TYPE_SPLASH].atom ||
1293 type[i] == ewmh[_NET_WM_WINDOW_TYPE_DIALOG].atom) {
1295 win->quirks = SWM_Q_FLOAT;
1302 #define SWM_EWMH_ACTION_COUNT_MAX (6)
1303 #define EWMH_F_FULLSCREEN (1<<0)
1304 #define EWMH_F_ABOVE (1<<1)
1305 #define EWMH_F_HIDDEN (1<<2)
1306 #define EWMH_F_SKIP_PAGER (1<<3)
1307 #define EWMH_F_SKIP_TASKBAR (1<<4)
1308 #define SWM_F_MANUAL (1<<5)
1311 ewmh_set_win_fullscreen(struct ws_win *win, int fs)
1319 DNPRINTF(SWM_D_MISC, "ewmh_set_win_fullscreen: window: 0x%x, "
1320 "fullscreen %s\n", win->id, YESNO(fs));
1323 if (!win->g_floatvalid)
1324 store_float_geom(win, win->ws->r);
1326 win->g = win->ws->r->g;
1329 load_float_geom(win, win->ws->r);
1336 ewmh_update_actions(struct ws_win *win)
1338 xcb_atom_t actions[SWM_EWMH_ACTION_COUNT_MAX];
1344 actions[n++] = ewmh[_NET_WM_ACTION_CLOSE].atom;
1346 if (win->floating) {
1347 actions[n++] = ewmh[_NET_WM_ACTION_MOVE].atom;
1348 actions[n++] = ewmh[_NET_WM_ACTION_RESIZE].atom;
1349 actions[n++] = ewmh[_NET_WM_ACTION_ABOVE].atom;
1352 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
1353 ewmh[_NET_WM_ALLOWED_ACTIONS].atom, XCB_ATOM_ATOM, 32, 1, actions);
1356 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
1357 #define _NET_WM_STATE_ADD 1 /* add/set property */
1358 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
1361 ewmh_update_win_state(struct ws_win *win, xcb_atom_t state, long action)
1363 unsigned int mask = 0;
1364 unsigned int changed = 0;
1365 unsigned int orig_flags;
1370 DNPRINTF(SWM_D_PROP, "ewmh_update_win_state: window: 0x%x, state: %ld, "
1371 "action: %ld\n", win->id, (unsigned long)state, action);
1373 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
1374 mask = EWMH_F_FULLSCREEN;
1375 else if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
1376 mask = EWMH_F_ABOVE;
1377 else if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
1378 mask = SWM_F_MANUAL;
1379 else if (state == ewmh[_NET_WM_STATE_SKIP_PAGER].atom)
1380 mask = EWMH_F_SKIP_PAGER;
1381 else if (state == ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom)
1382 mask = EWMH_F_SKIP_TASKBAR;
1384 orig_flags = win->ewmh_flags;
1387 case _NET_WM_STATE_REMOVE:
1388 win->ewmh_flags &= ~mask;
1390 case _NET_WM_STATE_ADD:
1391 win->ewmh_flags |= mask;
1393 case _NET_WM_STATE_TOGGLE:
1394 win->ewmh_flags ^= mask;
1398 changed = (win->ewmh_flags & mask) ^ (orig_flags & mask) ? 1 : 0;
1400 if (state == ewmh[_NET_WM_STATE_ABOVE].atom) {
1401 if (changed && !floating_toggle_win(win))
1402 win->ewmh_flags = orig_flags; /* revert */
1403 } else if (state == ewmh[_SWM_WM_STATE_MANUAL].atom) {
1405 win->manual = (win->ewmh_flags & SWM_F_MANUAL) != 0;
1406 } else if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom) {
1407 if (changed && !ewmh_set_win_fullscreen(win,
1408 win->ewmh_flags & EWMH_F_FULLSCREEN))
1409 win->ewmh_flags = orig_flags; /* revert */
1412 xcb_delete_property(conn, win->id, ewmh[_NET_WM_STATE].atom);
1414 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
1415 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1416 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1417 &ewmh[_NET_WM_STATE_FULLSCREEN].atom);
1418 else if (win->ewmh_flags & EWMH_F_SKIP_PAGER)
1419 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1420 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1421 &ewmh[_NET_WM_STATE_SKIP_PAGER].atom);
1422 else if (win->ewmh_flags & EWMH_F_SKIP_TASKBAR)
1423 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1424 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1425 &ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom);
1426 else if (win->ewmh_flags & EWMH_F_ABOVE)
1427 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1428 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1429 &ewmh[_NET_WM_STATE_ABOVE].atom);
1430 else if (win->ewmh_flags & SWM_F_MANUAL)
1431 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1432 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1433 &ewmh[_SWM_WM_STATE_MANUAL].atom);
1437 ewmh_get_win_state(struct ws_win *win)
1440 xcb_get_property_cookie_t c;
1441 xcb_get_property_reply_t *r;
1447 win->ewmh_flags = 0;
1449 win->ewmh_flags |= EWMH_F_ABOVE;
1451 win->ewmh_flags |= SWM_F_MANUAL;
1453 c = xcb_get_property(conn, 0, win->id, ewmh[_NET_WM_STATE].atom,
1454 XCB_ATOM_ATOM, 0, UINT32_MAX);
1455 r = xcb_get_property_reply(conn, c, NULL);
1459 states = xcb_get_property_value(r);
1460 n = xcb_get_property_value_length(r);
1462 for (i = 0; i < n; i++)
1463 ewmh_update_win_state(win, states[i], _NET_WM_STATE_ADD);
1471 dumpwins(struct swm_region *r, union arg *args)
1475 xcb_get_window_attributes_cookie_t c;
1476 xcb_get_window_attributes_reply_t *wa;
1478 /* suppress unused warning since var is needed */
1481 if (r->ws == NULL) {
1482 warnx("dumpwins: invalid workspace");
1486 warnx("=== managed window list ws %02d ===", r->ws->idx);
1487 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
1488 state = getstate(win->id);
1489 c = xcb_get_window_attributes(conn, win->id);
1490 wa = xcb_get_window_attributes_reply(conn, c, NULL);
1492 warnx("window: 0x%x, map_state: %d, state: %u, "
1493 "transient: 0x%x", win->id, wa->map_state,
1494 state, win->transient);
1497 warnx("window: 0x%x, failed xcb_get_window_attributes",
1501 warnx("===== unmanaged window list =====");
1502 TAILQ_FOREACH(win, &r->ws->unmanagedlist, entry) {
1503 state = getstate(win->id);
1504 c = xcb_get_window_attributes(conn, win->id);
1505 wa = xcb_get_window_attributes_reply(conn, c, NULL);
1507 warnx("window: 0x%x, map_state: %d, state: %u, "
1508 "transient: 0x%x", win->id, wa->map_state,
1509 state, win->transient);
1512 warnx("window: 0x%x, failed xcb_get_window_attributes",
1516 warnx("=================================");
1520 dumpwins(struct swm_region *r, union arg *s)
1525 #endif /* SWM_DEBUG */
1530 int saved_errno, status;
1533 saved_errno = errno;
1537 while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) != 0) {
1542 if (errno != ECHILD)
1543 warn("sighdlr: waitpid");
1544 #endif /* SWM_DEBUG */
1547 if (pid == searchpid)
1551 if (WIFEXITED(status)) {
1552 if (WEXITSTATUS(status) != 0)
1553 warnx("sighdlr: child exit status: %d",
1554 WEXITSTATUS(status));
1556 warnx("sighdlr: child is terminated "
1558 #endif /* SWM_DEBUG */
1572 errno = saved_errno;
1578 struct pid_e *p = NULL;
1580 DNPRINTF(SWM_D_MISC, "find_pid: %d\n", pid);
1585 TAILQ_FOREACH(p, &pidlist, entry) {
1594 name_to_pixel(const char *colorname)
1596 uint32_t result = 0;
1597 char cname[32] = "#";
1598 xcb_screen_t *screen;
1599 xcb_colormap_t cmap;
1600 xcb_alloc_color_reply_t *cr;
1601 xcb_alloc_named_color_reply_t *nr;
1602 uint16_t rr, gg, bb;
1604 screen = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
1605 cmap = screen->default_colormap;
1607 /* color is in format rgb://rr/gg/bb */
1608 if (strncmp(colorname, "rgb:", 4) == 0) {
1609 if (parse_rgb(colorname, &rr, &gg, &bb) == -1)
1610 warnx("could not parse rgb %s", colorname);
1612 cr = xcb_alloc_color_reply(conn,
1613 xcb_alloc_color(conn, cmap, rr, gg, bb),
1619 warnx("color '%s' not found", colorname);
1622 nr = xcb_alloc_named_color_reply(conn,
1623 xcb_alloc_named_color(conn, cmap, strlen(colorname),
1626 strlcat(cname, colorname + 2, sizeof cname - 1);
1627 nr = xcb_alloc_named_color_reply(conn,
1628 xcb_alloc_named_color(conn, cmap, strlen(cname),
1635 warnx("color '%s' not found", colorname);
1642 setscreencolor(char *val, int i, int c)
1646 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1647 if (i > 0 && i <= num_screens) {
1648 screens[i - 1].c[c].pixel = name_to_pixel(val);
1649 free(screens[i - 1].c[c].name);
1650 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
1652 } else if (i == -1) {
1653 for (i = 0; i < num_screens; i++) {
1654 screens[i].c[c].pixel = name_to_pixel(val);
1655 free(screens[i].c[c].name);
1656 if ((screens[i].c[c].name = strdup(val)) == NULL)
1660 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1665 fancy_stacker(struct workspace *ws)
1667 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1668 if (ws->cur_layout->l_stack == vertical_stack)
1669 snprintf(ws->stacker, sizeof ws->stacker,
1670 ws->l_state.vertical_flip ? "[%d>%d]" : "[%d|%d]",
1671 ws->l_state.vertical_mwin, ws->l_state.vertical_stacks);
1672 if (ws->cur_layout->l_stack == horizontal_stack)
1673 snprintf(ws->stacker, sizeof ws->stacker,
1674 ws->l_state.horizontal_flip ? "[%dv%d]" : "[%d-%d]",
1675 ws->l_state.horizontal_mwin, ws->l_state.horizontal_stacks);
1679 plain_stacker(struct workspace *ws)
1681 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1682 if (ws->cur_layout->l_stack == vertical_stack)
1683 strlcpy(ws->stacker, ws->l_state.vertical_flip ? "[>]" : "[|]",
1684 sizeof ws->stacker);
1685 if (ws->cur_layout->l_stack == horizontal_stack)
1686 strlcpy(ws->stacker, ws->l_state.horizontal_flip ? "[v]" : "[-]",
1687 sizeof ws->stacker);
1691 custom_region(char *val)
1693 unsigned int x, y, w, h;
1694 int sidx, num_screens;
1695 xcb_screen_t *screen;
1697 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1698 if (sscanf(val, "screen[%d]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
1699 errx(1, "invalid custom region, "
1700 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1701 if (sidx < 1 || sidx > num_screens)
1702 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1706 if ((screen = get_screen(sidx)) == NULL)
1707 errx(1, "ERROR: can't get screen %d.", sidx);
1710 errx(1, "region %ux%u+%u+%u too small", w, h, x, y);
1712 if (x > screen->width_in_pixels ||
1713 y > screen->height_in_pixels ||
1714 w + x > screen->width_in_pixels ||
1715 h + y > screen->height_in_pixels) {
1716 warnx("ignoring region %ux%u+%u+%u - not within screen "
1717 "boundaries (%ux%u)", w, h, x, y,
1718 screen->width_in_pixels, screen->height_in_pixels);
1722 new_region(&screens[sidx], x, y, w, h);
1726 socket_setnonblock(int fd)
1730 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
1731 err(1, "fcntl F_GETFL");
1732 flags |= O_NONBLOCK;
1733 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
1734 err(1, "fcntl F_SETFL");
1738 bar_print_legacy(struct swm_region *r, const char *s)
1740 xcb_rectangle_t rect;
1745 XRectangle ibox, lbox;
1749 XmbTextExtents(bar_fs, s, len, &ibox, &lbox);
1751 switch (bar_justify) {
1752 case SWM_BAR_JUSTIFY_LEFT:
1755 case SWM_BAR_JUSTIFY_CENTER:
1756 x = (WIDTH(r) - lbox.width) / 2;
1758 case SWM_BAR_JUSTIFY_RIGHT:
1759 x = WIDTH(r) - lbox.width - SWM_BAR_OFFSET;
1763 if (x < SWM_BAR_OFFSET)
1768 rect.width = WIDTH(r->bar);
1769 rect.height = HEIGHT(r->bar);
1771 /* clear back buffer */
1772 gcv[0] = r->s->c[SWM_S_COLOR_BAR].pixel;
1773 xcb_change_gc(conn, r->s->bar_gc, XCB_GC_FOREGROUND, gcv);
1774 xcb_poly_fill_rectangle(conn, r->bar->buffer, r->s->bar_gc, 1, &rect);
1776 /* draw back buffer */
1777 gcvd.graphics_exposures = 0;
1778 draw = XCreateGC(display, r->bar->buffer, GCGraphicsExposures, &gcvd);
1779 XSetForeground(display, draw, r->s->c[SWM_S_COLOR_BAR_FONT].pixel);
1780 DRAWSTRING(display, r->bar->buffer, bar_fs, draw,
1781 x, (bar_fs_extents->max_logical_extent.height - lbox.height) / 2 -
1783 XFreeGC(display, draw);
1786 xcb_copy_area(conn, r->bar->buffer, r->bar->id, r->s->bar_gc, 0, 0,
1787 0, 0, WIDTH(r->bar), HEIGHT(r->bar));
1791 bar_print(struct swm_region *r, const char *s)
1794 xcb_rectangle_t rect;
1802 XftTextExtentsUtf8(display, bar_font, (FcChar8 *)s, len, &info);
1804 switch (bar_justify) {
1805 case SWM_BAR_JUSTIFY_LEFT:
1808 case SWM_BAR_JUSTIFY_CENTER:
1809 x = (WIDTH(r) - info.width) / 2;
1811 case SWM_BAR_JUSTIFY_RIGHT:
1812 x = WIDTH(r) - info.width - SWM_BAR_OFFSET;
1816 if (x < SWM_BAR_OFFSET)
1821 rect.width = WIDTH(r->bar);
1822 rect.height = HEIGHT(r->bar);
1824 /* clear back buffer */
1825 gcv[0] = r->s->c[SWM_S_COLOR_BAR].pixel;
1826 xcb_change_gc(conn, r->s->bar_gc, XCB_GC_FOREGROUND, gcv);
1827 xcb_poly_fill_rectangle(conn, r->bar->buffer, r->s->bar_gc, 1, &rect);
1829 /* draw back buffer */
1830 draw = XftDrawCreate(display, r->bar->buffer,
1831 DefaultVisual(display, r->s->idx),
1832 DefaultColormap(display, r->s->idx));
1834 XftDrawStringUtf8(draw, &bar_font_color, bar_font, x,
1835 (HEIGHT(r->bar) + bar_font->height) / 2 - bar_font->descent,
1838 XftDrawDestroy(draw);
1841 xcb_copy_area(conn, r->bar->buffer, r->bar->id, r->s->bar_gc, 0, 0,
1842 0, 0, WIDTH(r->bar), HEIGHT(r->bar));
1846 bar_extra_stop(void)
1850 bzero(bar_pipe, sizeof bar_pipe);
1853 kill(bar_pid, SIGTERM);
1856 strlcpy(bar_ext, "", sizeof bar_ext);
1861 bar_class_name(char *s, size_t sz, struct swm_region *r)
1863 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1865 if (r->ws->focus->ch.class_name != NULL)
1866 strlcat(s, r->ws->focus->ch.class_name, sz);
1870 bar_title_name(char *s, size_t sz, struct swm_region *r)
1872 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1874 if (r->ws->focus->ch.instance_name != NULL)
1875 strlcat(s, r->ws->focus->ch.instance_name, sz);
1879 bar_class_title_name(char *s, size_t sz, struct swm_region *r)
1881 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1884 bar_class_name(s, sz, r);
1885 strlcat(s, ":", sz);
1886 bar_title_name(s, sz, r);
1890 bar_window_float(char *s, size_t sz, struct swm_region *r)
1892 if (r == NULL || r ->ws == NULL || r->ws->focus == NULL)
1894 if (r->ws->focus->floating)
1895 strlcat(s, "(f)", sz);
1899 bar_window_name(char *s, size_t sz, struct swm_region *r)
1903 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1905 if ((title = get_win_name(r->ws->focus->id)) == NULL)
1908 strlcat(s, title, sz);
1912 int urgent[SWM_WS_MAX];
1914 bar_urgent(char *s, size_t sz)
1917 int i, j, num_screens;
1919 xcb_get_property_cookie_t c;
1920 xcb_icccm_wm_hints_t hints;
1922 for (i = 0; i < workspace_limit; i++)
1925 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1926 for (i = 0; i < num_screens; i++)
1927 for (j = 0; j < workspace_limit; j++)
1928 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry) {
1929 c = xcb_icccm_get_wm_hints(conn, win->id);
1930 if (xcb_icccm_get_wm_hints_reply(conn, c,
1933 if (hints.flags & XCB_ICCCM_WM_HINT_X_URGENCY)
1937 for (i = 0; i < workspace_limit; i++) {
1939 snprintf(b, sizeof b, "%d ", i + 1);
1941 snprintf(b, sizeof b, "- ");
1947 bar_workspace_name(char *s, size_t sz, struct swm_region *r)
1949 if (r == NULL || r->ws == NULL)
1951 if (r->ws->name != NULL)
1952 strlcat(s, r->ws->name, sz);
1955 /* build the default bar format according to the defined enabled options */
1957 bar_fmt(const char *fmtexp, char *fmtnew, struct swm_region *r, size_t sz)
1959 /* if format provided, just copy the buffers */
1960 if (bar_format != NULL) {
1961 strlcpy(fmtnew, fmtexp, sz);
1965 /* reset the output buffer */
1968 strlcat(fmtnew, "+N:+I ", sz);
1970 strlcat(fmtnew, "+S", sz);
1971 strlcat(fmtnew, " ", sz);
1973 /* only show the workspace name if there's actually one */
1974 if (r != NULL && r->ws != NULL && r->ws->name != NULL)
1975 strlcat(fmtnew, "<+D>", sz);
1976 strlcat(fmtnew, "+3<", sz);
1978 if (clock_enabled) {
1979 strlcat(fmtnew, fmtexp, sz);
1980 strlcat(fmtnew, "+4<", sz);
1983 /* bar_urgent already adds the space before the last asterisk */
1985 strlcat(fmtnew, "* +U*+4<", sz);
1987 if (title_class_enabled) {
1988 strlcat(fmtnew, "+C", sz);
1989 if (!title_name_enabled)
1990 strlcat(fmtnew, "+4<", sz);
1993 /* checks needed by the colon and floating strlcat(3) calls below */
1994 if (r != NULL && r->ws != NULL && r->ws->focus != NULL) {
1995 if (title_name_enabled) {
1996 if (title_class_enabled)
1997 strlcat(fmtnew, ":", sz);
1998 strlcat(fmtnew, "+T+4<", sz);
2000 if (window_name_enabled) {
2001 if (r->ws->focus->floating)
2002 strlcat(fmtnew, "+F ", sz);
2003 strlcat(fmtnew, "+64W ", sz);
2007 /* finally add the action script output and the version */
2008 strlcat(fmtnew, "+4<+A+4<+V", sz);
2012 bar_replace_pad(char *tmp, int *limit, size_t sz)
2014 /* special case; no limit given, pad one space, instead */
2015 if (*limit == (int)sz - 1)
2017 snprintf(tmp, sz, "%*s", *limit, " ");
2020 /* replaces the bar format character sequences (like in tmux(1)) */
2022 bar_replace_seq(char *fmt, char *fmtrep, struct swm_region *r, size_t *offrep,
2026 char tmp[SWM_BAR_MAX];
2030 /* reset strlcat(3) buffer */
2033 /* get number, if any */
2036 if (sscanf(fmt, "%d%n", &limit, &size) != 1)
2037 limit = sizeof tmp - 1;
2038 if (limit <= 0 || limit >= (int)sizeof tmp)
2039 limit = sizeof tmp - 1;
2041 /* there is nothing to replace (ie EOL) */
2048 bar_replace_pad(tmp, &limit, sizeof tmp);
2051 snprintf(tmp, sizeof tmp, "%s", bar_ext);
2054 bar_class_name(tmp, sizeof tmp, r);
2057 bar_workspace_name(tmp, sizeof tmp, r);
2060 bar_window_float(tmp, sizeof tmp, r);
2063 snprintf(tmp, sizeof tmp, "%d", r->ws->idx + 1);
2066 snprintf(tmp, sizeof tmp, "%d", r->s->idx + 1);
2069 bar_class_title_name(tmp, sizeof tmp, r);
2072 snprintf(tmp, sizeof tmp, "%s", r->ws->stacker);
2075 bar_title_name(tmp, sizeof tmp, r);
2078 bar_urgent(tmp, sizeof tmp);
2081 snprintf(tmp, sizeof tmp, "%s", bar_vertext);
2084 bar_window_name(tmp, sizeof tmp, r);
2087 /* unknown character sequence; copy as-is */
2088 snprintf(tmp, sizeof tmp, "+%c", *fmt);
2094 if ((int)len < limit)
2096 while (limit-- > 0) {
2097 if (*offrep >= sz - 1)
2099 fmtrep[(*offrep)++] = *ptr++;
2107 bar_replace(char *fmt, char *fmtrep, struct swm_region *r, size_t sz)
2112 while (*fmt != '\0') {
2114 /* skip ordinary characters */
2117 fmtrep[off++] = *fmt++;
2121 /* character sequence found; replace it */
2122 fmt = bar_replace_seq(fmt, fmtrep, r, &off, sz);
2131 bar_fmt_expand(char *fmtexp, size_t sz)
2138 /* start by grabbing the current time and date */
2140 localtime_r(&tmt, &tm);
2142 /* figure out what to expand */
2143 if (bar_format != NULL)
2145 else if (bar_format == NULL && clock_enabled)
2147 /* if nothing to expand bail out */
2153 /* copy as-is, just in case the format shouldn't be expanded below */
2154 strlcpy(fmtexp, fmt, sz);
2155 /* finally pass the string through strftime(3) */
2156 #ifndef SWM_DENY_CLOCK_FORMAT
2157 if ((len = strftime(fmtexp, sz, fmt, &tm)) == 0)
2158 warnx("format too long");
2163 /* Redraws the bar; need to follow with xcb_flush() or focus_flush(). */
2167 char fmtexp[SWM_BAR_MAX], fmtnew[SWM_BAR_MAX];
2168 char fmtrep[SWM_BAR_MAX];
2170 struct swm_region *r;
2172 /* expand the format by first passing it through strftime(3) */
2173 bar_fmt_expand(fmtexp, sizeof fmtexp);
2175 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2176 for (i = 0; i < num_screens; i++) {
2177 TAILQ_FOREACH(r, &screens[i].rl, entry) {
2181 if (r->ws->bar_enabled)
2182 xcb_map_window(conn, r->bar->id);
2184 xcb_unmap_window(conn, r->bar->id);
2188 bar_fmt(fmtexp, fmtnew, r, sizeof fmtnew);
2189 bar_replace(fmtnew, fmtrep, r, sizeof fmtrep);
2190 if (bar_font_legacy)
2191 bar_print_legacy(r, fmtrep);
2193 bar_print(r, fmtrep);
2198 /* Reads external script output; call when stdin is readable. */
2200 bar_extra_update(void)
2203 char b[SWM_BAR_MAX];
2206 if (bar_enabled && bar_extra && bar_extra_running) {
2207 while (fgets(b, sizeof(b), stdin) != NULL) {
2209 if (b[len - 1] == '\n') {
2210 /* Remove newline. */
2213 /* "Clear" bar_ext. */
2216 /* Flush buffered output. */
2217 strlcpy(bar_ext, bar_ext_buf, sizeof(bar_ext));
2218 bar_ext_buf[0] = '\0';
2220 /* Append new output to bar. */
2221 strlcat(bar_ext, b, sizeof(bar_ext));
2225 /* Buffer output. */
2226 strlcat(bar_ext_buf, b, sizeof(bar_ext_buf));
2230 if (errno != EAGAIN) {
2231 warn("bar_action failed");
2236 * Attempt to drain stdin, so it doesn't cause the main loop to
2237 * call us as fast as it can.
2239 while (fgets(b, sizeof(b), stdin) != NULL);
2244 /* Clear bar script output if bar script is not running. */
2245 if (bar_ext[0] != '\0') {
2258 bar_toggle(struct swm_region *r, union arg *args)
2260 struct swm_region *tmpr;
2263 /* suppress unused warnings since vars are needed */
2267 DNPRINTF(SWM_D_BAR, "bar_toggle\n");
2270 case SWM_ARG_ID_BAR_TOGGLE_WS:
2271 /* Only change if master switch is enabled. */
2273 r->ws->bar_enabled = !r->ws->bar_enabled;
2275 bar_enabled = r->ws->bar_enabled = 1;
2277 case SWM_ARG_ID_BAR_TOGGLE:
2278 bar_enabled = !bar_enabled;
2282 /* update bars as necessary */
2283 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2284 for (i = 0; i < num_screens; i++)
2285 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
2287 if (bar_enabled && tmpr->ws->bar_enabled)
2288 xcb_map_window(conn, tmpr->bar->id);
2290 xcb_unmap_window(conn, tmpr->bar->id);
2295 /* must be after stack */
2302 bar_extra_setup(void)
2304 struct swm_region *r;
2308 /* do this here because the conf file is in memory */
2309 if (bar_extra && !bar_extra_running && bar_argv[0]) {
2310 /* launch external status app */
2311 bar_extra_running = 1;
2312 if (pipe(bar_pipe) == -1)
2313 err(1, "pipe error");
2314 socket_setnonblock(bar_pipe[0]);
2315 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
2317 /* Set stdin to read from the pipe. */
2318 if (dup2(bar_pipe[0], 0) == -1)
2321 /* Set stdout to write to the pipe. */
2322 if (dup2(bar_pipe[1], 1) == -1)
2325 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
2326 err(1, "could not disable SIGPIPE");
2327 switch (bar_pid = fork()) {
2329 err(1, "cannot fork");
2333 execvp(bar_argv[0], bar_argv);
2334 err(1, "%s external app failed", bar_argv[0]);
2336 default: /* parent */
2342 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2343 for (i = 0; i < num_screens; i++)
2344 TAILQ_FOREACH(r, &screens[i].rl, entry) {
2347 wa[0] = screens[i].c[SWM_S_COLOR_BAR].pixel;
2348 wa[1] = screens[i].c[SWM_S_COLOR_BAR_BORDER].pixel;
2349 xcb_change_window_attributes(conn, r->bar->id,
2350 XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL, wa);
2362 while ((s = index(s, '-'))) {
2367 return (count == 14);
2373 char *default_string;
2374 char **missing_charsets;
2375 int num_missing_charsets = 0;
2379 XFreeFontSet(display, bar_fs);
2383 DNPRINTF(SWM_D_INIT, "fontset_init: loading bar_fonts: %s\n", bar_fonts);
2385 bar_fs = XCreateFontSet(display, bar_fonts, &missing_charsets,
2386 &num_missing_charsets, &default_string);
2388 if (num_missing_charsets > 0) {
2389 warnx("Unable to load charset(s):");
2391 for (i = 0; i < num_missing_charsets; ++i)
2392 warnx("%s", missing_charsets[i]);
2394 XFreeStringList(missing_charsets);
2396 if (strcmp(default_string, ""))
2397 warnx("Glyphs from those sets will be replaced "
2398 "by '%s'.", default_string);
2400 warnx("Glyphs from those sets won't be drawn.");
2404 errx(1, "Error creating font set structure.");
2406 bar_fs_extents = XExtentsOfFontSet(bar_fs);
2408 bar_height = bar_fs_extents->max_logical_extent.height +
2409 2 * bar_border_width;
2416 xft_init(struct swm_region *r)
2418 char *font, *d, *search;
2421 if (bar_font == NULL) {
2422 if ((d = strdup(bar_fonts)) == NULL)
2423 errx(1, "insufficient memory.");
2425 while ((font = strsep(&search, ",")) != NULL) {
2429 DNPRINTF(SWM_D_INIT, "xft_init: try font %s\n", font);
2432 bar_font = XftFontOpenXlfd(display, r->s->idx,
2435 bar_font = XftFontOpenName(display, r->s->idx,
2440 warnx("unable to load font %s", font);
2443 DNPRINTF(SWM_D_INIT, "successfully opened "
2451 if (bar_font == NULL)
2452 errx(1, "unable to open a font");
2454 PIXEL_TO_XRENDERCOLOR(r->s->c[SWM_S_COLOR_BAR_FONT].pixel, color);
2456 if (!XftColorAllocValue(display, DefaultVisual(display, r->s->idx),
2457 DefaultColormap(display, r->s->idx), &color, &bar_font_color))
2458 warn("Xft error: unable to allocate color.");
2460 bar_height = bar_font->height + 2 * bar_border_width;
2467 bar_setup(struct swm_region *r)
2469 xcb_screen_t *screen;
2472 DNPRINTF(SWM_D_BAR, "bar_setup: screen %d.\n",
2475 if ((screen = get_screen(r->s->idx)) == NULL)
2476 errx(1, "ERROR: can't get screen %d.", r->s->idx);
2481 if ((r->bar = calloc(1, sizeof(struct swm_bar))) == NULL)
2482 err(1, "bar_setup: calloc: failed to allocate memory.");
2484 if (bar_font_legacy)
2490 Y(r->bar) = bar_at_bottom ? (Y(r) + HEIGHT(r) - bar_height) : Y(r);
2491 WIDTH(r->bar) = WIDTH(r) - 2 * bar_border_width;
2492 HEIGHT(r->bar) = bar_height - 2 * bar_border_width;
2494 r->bar->id = xcb_generate_id(conn);
2495 wa[0] = r->s->c[SWM_S_COLOR_BAR].pixel;
2496 wa[1] = r->s->c[SWM_S_COLOR_BAR_BORDER].pixel;
2497 wa[2] = XCB_EVENT_MASK_EXPOSURE;
2499 xcb_create_window(conn, XCB_COPY_FROM_PARENT, r->bar->id, r->s->root,
2500 X(r->bar), Y(r->bar), WIDTH(r->bar), HEIGHT(r->bar),
2501 bar_border_width, XCB_WINDOW_CLASS_INPUT_OUTPUT,
2502 XCB_COPY_FROM_PARENT, XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL
2503 | XCB_CW_EVENT_MASK, wa);
2505 r->bar->buffer = xcb_generate_id(conn);
2506 xcb_create_pixmap(conn, screen->root_depth, r->bar->buffer, r->bar->id,
2507 WIDTH(r->bar), HEIGHT(r->bar));
2510 xcb_randr_select_input(conn, r->bar->id,
2511 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE);
2514 xcb_map_window(conn, r->bar->id);
2516 DNPRINTF(SWM_D_BAR, "bar_setup: window: 0x%x, (x,y) w x h: (%d,%d) "
2517 "%d x %d\n", WINID(r->bar), X(r->bar), Y(r->bar), WIDTH(r->bar),
2524 bar_cleanup(struct swm_region *r)
2528 xcb_destroy_window(conn, r->bar->id);
2529 xcb_free_pixmap(conn, r->bar->buffer);
2535 set_win_state(struct ws_win *win, uint16_t state)
2537 uint16_t data[2] = { state, XCB_ATOM_NONE };
2539 DNPRINTF(SWM_D_EVENT, "set_win_state: window: 0x%x, state: %u\n",
2545 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id, a_state,
2546 a_state, 32, 2, data);
2550 getstate(xcb_window_t w)
2552 uint32_t result = 0;
2553 xcb_get_property_cookie_t c;
2554 xcb_get_property_reply_t *r;
2556 c = xcb_get_property(conn, 0, w, a_state, a_state, 0L, 2L);
2557 r = xcb_get_property_reply(conn, c, NULL);
2560 if (r->type == a_state && r->format == 32 && r->length == 2)
2561 result = *((uint32_t *)xcb_get_property_value(r));
2565 DNPRINTF(SWM_D_MISC, "getstate property: win 0x%x state %u\n", w,
2571 version(struct swm_region *r, union arg *args)
2573 /* suppress unused warnings since vars are needed */
2577 bar_version = !bar_version;
2579 snprintf(bar_vertext, sizeof bar_vertext,
2580 "Version: %s Build: %s", SPECTRWM_VERSION, buildstr);
2582 strlcpy(bar_vertext, "", sizeof bar_vertext);
2589 client_msg(struct ws_win *win, xcb_atom_t a, xcb_timestamp_t t)
2591 xcb_client_message_event_t ev;
2599 name = get_atom_name(a);
2600 DNPRINTF(SWM_D_EVENT, "client_msg: window: 0x%x, atom: %s(%u), "
2602 win->id, name, a, t);
2606 bzero(&ev, sizeof ev);
2607 ev.response_type = XCB_CLIENT_MESSAGE;
2608 ev.window = win->id;
2611 ev.data.data32[0] = a;
2612 ev.data.data32[1] = t;
2614 xcb_send_event(conn, 0, win->id,
2615 XCB_EVENT_MASK_NO_EVENT, (const char *)&ev);
2618 /* synthetic response to a ConfigureRequest when not making a change */
2620 config_win(struct ws_win *win, xcb_configure_request_event_t *ev)
2622 xcb_configure_notify_event_t ce;
2627 /* send notification of unchanged state. */
2628 bzero(&ce, sizeof(ce));
2629 ce.response_type = XCB_CONFIGURE_NOTIFY;
2632 ce.width = WIDTH(win);
2633 ce.height = HEIGHT(win);
2634 ce.override_redirect = 0;
2639 ce.window = win->id;
2640 ce.border_width = BORDER(win);
2641 ce.above_sibling = XCB_WINDOW_NONE;
2644 ce.event = ev->window;
2645 ce.window = ev->window;
2647 /* make response appear more WM_SIZE_HINTS-compliant */
2648 if (win->sh.flags) {
2649 DNPRINTF(SWM_D_MISC, "config_win: hints: window: 0x%x,"
2650 " sh.flags: %u, min: %d x %d, max: %d x %d, inc: "
2651 "%d x %d\n", win->id, win->sh.flags, SH_MIN_W(win),
2652 SH_MIN_H(win), SH_MAX_W(win), SH_MAX_H(win),
2653 SH_INC_W(win), SH_INC_H(win));
2658 /* the hint may be set... to 0! */
2659 if (SH_MIN_W(win) > 0 && ce.width < SH_MIN_W(win))
2660 ce.width = SH_MIN_W(win);
2661 if (SH_MIN_H(win) > 0 && ce.height < SH_MIN_H(win))
2662 ce.height = SH_MIN_H(win);
2667 /* may also be advertized as 0 */
2668 if (SH_MAX_W(win) > 0 && ce.width > SH_MAX_W(win))
2669 ce.width = SH_MAX_W(win);
2670 if (SH_MAX_H(win) > 0 && ce.height > SH_MAX_H(win))
2671 ce.height = SH_MAX_H(win);
2674 /* resize increment. */
2676 if (SH_INC_W(win) > 1 && ce.width > SH_INC_W(win))
2677 ce.width -= (ce.width - SH_MIN_W(win)) %
2679 if (SH_INC_H(win) > 1 && ce.height > SH_INC_H(win))
2680 ce.height -= (ce.height - SH_MIN_H(win)) %
2684 /* adjust x and y for requested border_width. */
2685 ce.x += BORDER(win) - ev->border_width;
2686 ce.y += BORDER(win) - ev->border_width;
2687 ce.border_width = ev->border_width;
2688 ce.above_sibling = ev->sibling;
2691 DNPRINTF(SWM_D_MISC, "config_win: ewmh: %s, window: 0x%x, (x,y) w x h: "
2692 "(%d,%d) %d x %d, border: %d\n", YESNO(ev == NULL), win->id, ce.x,
2693 ce.y, ce.width, ce.height, ce.border_width);
2695 xcb_send_event(conn, 0, win->id, XCB_EVENT_MASK_STRUCTURE_NOTIFY,
2700 count_win(struct workspace *ws, int count_transient)
2705 TAILQ_FOREACH(win, &ws->winlist, entry) {
2706 if (!count_transient && win->floating)
2708 if (!count_transient && win->transient)
2714 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
2720 quit(struct swm_region *r, union arg *args)
2722 /* suppress unused warnings since vars are needed */
2726 DNPRINTF(SWM_D_MISC, "quit\n");
2731 map_window(struct ws_win *win)
2733 uint32_t val = XCB_STACK_MODE_ABOVE;
2738 DNPRINTF(SWM_D_EVENT, "map_window: win 0x%x, mapped: %s\n", win->id,
2739 YESNO(win->mapped));
2741 xcb_configure_window(conn, win->id,
2742 XCB_CONFIG_WINDOW_STACK_MODE, &val);
2747 xcb_map_window(conn, win->id);
2748 set_win_state(win, XCB_ICCCM_WM_STATE_NORMAL);
2753 unmap_window(struct ws_win *win)
2758 DNPRINTF(SWM_D_EVENT, "unmap_window: win 0x%x, mapped: %s\n", win->id,
2759 YESNO(win->mapped));
2764 xcb_unmap_window(conn, win->id);
2765 set_win_state(win, XCB_ICCCM_WM_STATE_ICONIC);
2773 int i, j, num_screens;
2775 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2776 for (i = 0; i < num_screens; i++)
2777 for (j = 0; j < workspace_limit; j++)
2778 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2783 fake_keypress(struct ws_win *win, xcb_keysym_t keysym, uint16_t modifiers)
2785 xcb_key_press_event_t event;
2786 xcb_keycode_t *keycode;
2791 keycode = xcb_key_symbols_get_keycode(syms, keysym);
2793 DNPRINTF(SWM_D_MISC, "fake_keypress: win 0x%x keycode %u\n",
2796 bzero(&event, sizeof(event));
2797 event.event = win->id;
2798 event.root = win->s->root;
2799 event.child = XCB_WINDOW_NONE;
2800 event.time = XCB_CURRENT_TIME;
2801 event.event_x = X(win);
2802 event.event_y = Y(win);
2805 event.same_screen = 1;
2806 event.detail = *keycode;
2807 event.state = modifiers;
2809 event.response_type = XCB_KEY_PRESS;
2810 xcb_send_event(conn, 1, win->id,
2811 XCB_EVENT_MASK_KEY_PRESS, (const char *)&event);
2813 event.response_type = XCB_KEY_RELEASE;
2814 xcb_send_event(conn, 1, win->id,
2815 XCB_EVENT_MASK_KEY_RELEASE, (const char *)&event);
2821 restart(struct swm_region *r, union arg *args)
2823 /* suppress unused warning since var is needed */
2827 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
2831 execvp(start_argv[0], start_argv);
2832 warn("execvp failed");
2837 get_pointer_win(xcb_window_t root)
2839 struct ws_win *win = NULL;
2840 xcb_query_pointer_reply_t *r;
2842 DNPRINTF(SWM_D_EVENT, "get_pointer_win: root: 0x%x.\n", root);
2844 r = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, root), NULL);
2846 win = find_window(r->child);
2848 DNPRINTF(SWM_D_EVENT, "get_pointer_win: 0x%x.\n",
2851 DNPRINTF(SWM_D_EVENT, "get_pointer_win: none.\n");
2860 root_to_region(xcb_window_t root, int check)
2863 struct swm_region *r = NULL;
2865 xcb_query_pointer_reply_t *qpr;
2866 xcb_get_input_focus_reply_t *gifr;
2868 DNPRINTF(SWM_D_MISC, "root_to_region: window: 0x%x\n", root);
2870 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2871 for (i = 0; i < num_screens; i++)
2872 if (screens[i].root == root)
2875 if (check & SWM_CK_FOCUS) {
2876 /* Try to find an actively focused window */
2877 gifr = xcb_get_input_focus_reply(conn,
2878 xcb_get_input_focus(conn), NULL);
2880 cfw = find_window(gifr->focus);
2881 if (cfw && cfw->ws->r)
2888 if (r == NULL && check & SWM_CK_POINTER) {
2889 /* No region with an active focus; try to use pointer. */
2890 qpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn,
2891 screens[i].root), NULL);
2894 DNPRINTF(SWM_D_MISC, "root_to_region: pointer: "
2895 "(%d,%d)\n", qpr->root_x, qpr->root_y);
2896 TAILQ_FOREACH(r, &screens[i].rl, entry)
2897 if (X(r) <= qpr->root_x &&
2898 qpr->root_x < MAX_X(r) &&
2899 Y(r) <= qpr->root_y &&
2900 qpr->root_y < MAX_Y(r))
2907 if (r == NULL && check & SWM_CK_FALLBACK)
2908 r = TAILQ_FIRST(&screens[i].rl);
2914 find_unmanaged_window(xcb_window_t id)
2917 int i, j, num_screens;
2919 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2920 for (i = 0; i < num_screens; i++)
2921 for (j = 0; j < workspace_limit; j++)
2922 TAILQ_FOREACH(win, &screens[i].ws[j].unmanagedlist,
2930 find_window(xcb_window_t id)
2933 int i, j, num_screens;
2934 xcb_query_tree_reply_t *r;
2936 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2937 for (i = 0; i < num_screens; i++)
2938 for (j = 0; j < workspace_limit; j++)
2939 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2943 r = xcb_query_tree_reply(conn, xcb_query_tree(conn, id), NULL);
2947 /* if we were looking for the parent return that window instead */
2948 if (r->parent == 0 || r->root == r->parent) {
2953 /* look for parent */
2954 for (i = 0; i < num_screens; i++)
2955 for (j = 0; j < workspace_limit; j++)
2956 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2957 if (r->parent == win->id) {
2967 spawn(int ws_idx, union arg *args, int close_fd)
2972 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
2974 close(xcb_get_file_descriptor(conn));
2976 setenv("LD_PRELOAD", SWM_LIB, 1);
2978 if (asprintf(&ret, "%d", ws_idx) == -1) {
2979 warn("spawn: asprintf SWM_WS");
2982 setenv("_SWM_WS", ret, 1);
2986 if (asprintf(&ret, "%d", getpid()) == -1) {
2987 warn("spawn: asprintf _SWM_PID");
2990 setenv("_SWM_PID", ret, 1);
2994 if (setsid() == -1) {
2995 warn("spawn: setsid");
3001 * close stdin and stdout to prevent interaction between apps
3002 * and the baraction script
3003 * leave stderr open to record errors
3005 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) == -1) {
3006 warn("spawn: open");
3009 dup2(fd, STDIN_FILENO);
3010 dup2(fd, STDOUT_FILENO);
3015 execvp(args->argv[0], args->argv);
3017 warn("spawn: execvp");
3022 kill_refs(struct ws_win *win)
3024 int i, x, num_screens;
3025 struct swm_region *r;
3026 struct workspace *ws;
3031 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
3032 for (i = 0; i < num_screens; i++)
3033 TAILQ_FOREACH(r, &screens[i].rl, entry)
3034 for (x = 0; x < workspace_limit; x++) {
3036 if (win == ws->focus)
3038 if (win == ws->focus_prev)
3039 ws->focus_prev = NULL;
3044 validate_win(struct ws_win *testwin)
3047 struct workspace *ws;
3048 struct swm_region *r;
3049 int i, x, num_screens;
3051 if (testwin == NULL)
3054 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
3055 for (i = 0; i < num_screens; i++)
3056 TAILQ_FOREACH(r, &screens[i].rl, entry)
3057 for (x = 0; x < workspace_limit; x++) {
3059 TAILQ_FOREACH(win, &ws->winlist, entry)
3067 validate_ws(struct workspace *testws)
3069 struct swm_region *r;
3070 struct workspace *ws;
3071 int i, x, num_screens;
3073 /* validate all ws */
3074 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
3075 for (i = 0; i < num_screens; i++)
3076 TAILQ_FOREACH(r, &screens[i].rl, entry)
3077 for (x = 0; x < workspace_limit; x++) {
3086 unfocus_win(struct ws_win *win)
3088 xcb_window_t none = XCB_WINDOW_NONE;
3090 DNPRINTF(SWM_D_FOCUS, "unfocus_win: window: 0x%x\n", WINID(win));
3094 if (win->ws == NULL) {
3095 DNPRINTF(SWM_D_FOCUS, "unfocus_win: NULL ws.\n");
3099 if (validate_ws(win->ws)) {
3100 DNPRINTF(SWM_D_FOCUS, "unfocus_win: invalid ws.\n");
3104 if (win->ws->r == NULL) {
3105 DNPRINTF(SWM_D_FOCUS, "unfocus_win: NULL region.\n");
3109 if (validate_win(win)) {
3110 DNPRINTF(SWM_D_FOCUS, "unfocus_win: invalid win.\n");
3115 if (win->ws->focus == win) {
3116 win->ws->focus = NULL;
3117 win->ws->focus_prev = win;
3120 if (validate_win(win->ws->focus)) {
3121 kill_refs(win->ws->focus);
3122 win->ws->focus = NULL;
3125 if (validate_win(win->ws->focus_prev)) {
3126 kill_refs(win->ws->focus_prev);
3127 win->ws->focus_prev = NULL;
3130 xcb_change_window_attributes(conn, win->id, XCB_CW_BORDER_PIXEL,
3131 &win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].pixel);
3133 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
3134 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1, &none);
3136 DNPRINTF(SWM_D_FOCUS, "unfocus_win: done.\n");
3140 focus_win(struct ws_win *win)
3142 struct ws_win *cfw = NULL, *parent = NULL, *w;
3143 struct workspace *ws;
3144 xcb_get_input_focus_reply_t *r;
3146 DNPRINTF(SWM_D_FOCUS, "focus_win: window: 0x%x\n", WINID(win));
3151 if (win->ws == NULL)
3159 if (validate_ws(ws))
3162 if (validate_win(win)) {
3167 r = xcb_get_input_focus_reply(conn, xcb_get_input_focus(conn), NULL);
3169 cfw = find_window(r->focus);
3175 if (ws->focus != win) {
3176 if (ws->focus && ws->focus != cfw)
3177 unfocus_win(ws->focus);
3181 /* If this window directs focus to a child window, then clear. */
3182 if (win->focus_child)
3183 win->focus_child = NULL;
3185 /* If transient, adjust parent's focus child for focus_magic. */
3186 if (win->transient) {
3187 parent = find_window(win->transient);
3188 if (parent && parent->focus_child != win)
3189 parent->focus_child = win;
3192 if (cfw != win && ws->r != NULL) {
3193 /* Set input focus if no input hint, or indicated by hint. */
3194 if (!(win->hints.flags & XCB_ICCCM_WM_HINT_INPUT) ||
3195 (win->hints.flags & XCB_ICCCM_WM_HINT_INPUT &&
3197 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT,
3198 win->id, last_event_time);
3200 /* Tell app it can adjust focus to a specific window. */
3201 if (win->take_focus) {
3202 /* java is special; always tell parent */
3203 if (win->transient && win->java)
3204 client_msg(parent, a_takefocus,
3207 client_msg(win, a_takefocus, last_event_time);
3210 xcb_change_window_attributes(conn, win->id, XCB_CW_BORDER_PIXEL,
3211 &ws->r->s->c[SWM_S_COLOR_FOCUS].pixel);
3213 if (ws->cur_layout->flags & SWM_L_MAPONFOCUS ||
3215 /* If a parent exists, map it first. */
3219 /* Map siblings next. */
3220 TAILQ_FOREACH(w, &ws->winlist, entry)
3221 if (w != win && !w->iconic &&
3222 w->transient == parent->id)
3226 /* Map focused window. */
3229 /* Finally, map children of focus window. */
3230 TAILQ_FOREACH(w, &ws->winlist, entry)
3231 if (w->transient == win->id && !w->iconic)
3236 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
3237 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1,
3244 DNPRINTF(SWM_D_FOCUS, "focus_win: done.\n");
3247 /* If a child window should have focus instead, return it. */
3249 get_focus_magic(struct ws_win *win)
3251 struct ws_win *parent = NULL;
3252 struct ws_win *child = NULL;
3254 DNPRINTF(SWM_D_FOCUS, "get_focus_magic: window: 0x%x\n", WINID(win));
3258 if (win->transient) {
3259 parent = find_window(win->transient);
3261 /* If parent prefers focus elsewhere, then try to do so. */
3262 if (parent && (child = parent->focus_child)) {
3263 if (validate_win(child) == 0 && child->mapped)
3266 parent->focus_child = NULL;
3270 /* If this window prefers focus elsewhere, then try to do so. */
3271 if ((child = win->focus_child)) {
3272 if (validate_win(child) == 0 && child->mapped)
3275 win->focus_child = NULL;
3282 event_drain(uint8_t rt)
3284 xcb_generic_event_t *evt;
3286 /* ensure all pending requests have been processed before filtering. */
3288 while ((evt = xcb_poll_for_event(conn))) {
3289 if (XCB_EVENT_RESPONSE_TYPE(evt) != rt)
3297 switchws(struct swm_region *r, union arg *args)
3299 int wsid = args->id, unmap_old = 0;
3300 struct swm_region *this_r, *other_r;
3302 struct workspace *new_ws, *old_ws;
3307 if (wsid >= workspace_limit)
3311 old_ws = this_r->ws;
3312 new_ws = &this_r->s->ws[wsid];
3314 DNPRINTF(SWM_D_WS, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
3315 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), old_ws->idx, wsid);
3317 if (new_ws == NULL || old_ws == NULL)
3319 if (new_ws == old_ws)
3322 unfocus_win(old_ws->focus);
3324 other_r = new_ws->r;
3325 if (other_r == NULL) {
3326 /* the other workspace is hidden, hide this one */
3330 /* the other ws is visible in another region, exchange them */
3331 other_r->ws_prior = new_ws;
3332 other_r->ws = old_ws;
3333 old_ws->r = other_r;
3335 this_r->ws_prior = old_ws;
3336 this_r->ws = new_ws;
3339 /* Set focus_pending before stacking. */
3340 if (focus_mode != SWM_FOCUS_FOLLOW)
3341 new_ws->focus_pending = get_region_focus(new_ws->r);
3345 /* unmap old windows */
3347 TAILQ_FOREACH(win, &old_ws->winlist, entry)
3350 /* if workspaces were swapped, then don't wait to set focus */
3351 if (old_ws->r && focus_mode != SWM_FOCUS_FOLLOW) {
3352 if (new_ws->focus_pending) {
3353 focus_win(new_ws->focus_pending);
3355 /* Empty region, focus on root. */
3356 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT,
3357 new_ws->r->s[new_ws->r->s->idx].root,
3362 /* Clear bar if new ws is empty. */
3363 if (new_ws->focus_pending == NULL)
3368 DNPRINTF(SWM_D_WS, "switchws: done.\n");
3372 cyclews(struct swm_region *r, union arg *args)
3375 struct swm_screen *s = r->s;
3378 DNPRINTF(SWM_D_WS, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3379 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
3384 case SWM_ARG_ID_CYCLEWS_UP_ALL:
3387 case SWM_ARG_ID_CYCLEWS_UP:
3388 if (a.id < workspace_limit - 1)
3393 case SWM_ARG_ID_CYCLEWS_DOWN_ALL:
3396 case SWM_ARG_ID_CYCLEWS_DOWN:
3400 a.id = workspace_limit - 1;
3407 (!cycle_empty && TAILQ_EMPTY(&s->ws[a.id].winlist)))
3409 if (!cycle_visible && s->ws[a.id].r != NULL)
3413 } while (a.id != r->ws->idx);
3417 priorws(struct swm_region *r, union arg *args)
3423 DNPRINTF(SWM_D_WS, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3424 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
3426 if (r->ws_prior == NULL)
3429 a.id = r->ws_prior->idx;
3434 cyclescr(struct swm_region *r, union arg *args)
3437 struct swm_region *rr = NULL;
3440 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
3441 /* do nothing if we don't have more than one screen */
3442 if (!(num_screens > 1 || outputs > 1))
3447 case SWM_ARG_ID_CYCLESC_UP:
3448 rr = TAILQ_NEXT(r, entry);
3450 rr = TAILQ_FIRST(&screens[i].rl);
3452 case SWM_ARG_ID_CYCLESC_DOWN:
3453 rr = TAILQ_PREV(r, swm_region_list, entry);
3455 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
3463 nfw = get_region_focus(rr);
3467 /* New region is empty; unfocus old region and warp pointer. */
3468 unfocus_win(r->ws->focus);
3469 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT,
3470 rr->s[i].root, XCB_CURRENT_TIME);
3472 /* Clear bar since empty. */
3480 sort_windows(struct ws_win_list *wl)
3482 struct ws_win *win, *parent, *nxt;
3487 for (win = TAILQ_FIRST(wl); win != TAILQ_END(wl); win = nxt) {
3488 nxt = TAILQ_NEXT(win, entry);
3489 if (win->transient) {
3490 parent = find_window(win->transient);
3491 if (parent == NULL) {
3492 warnx("not possible bug");
3495 TAILQ_REMOVE(wl, win, entry);
3496 TAILQ_INSERT_AFTER(wl, parent, win, entry);
3503 swapwin(struct swm_region *r, union arg *args)
3505 struct ws_win *target, *source;
3506 struct ws_win *cur_focus;
3507 struct ws_win_list *wl;
3509 DNPRINTF(SWM_D_WS, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3510 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
3512 cur_focus = r->ws->focus;
3513 if (cur_focus == NULL)
3517 wl = &source->ws->winlist;
3520 case SWM_ARG_ID_SWAPPREV:
3521 if (source->transient)
3522 source = find_window(source->transient);
3523 target = TAILQ_PREV(source, ws_win_list, entry);
3524 if (target && target->transient)
3525 target = find_window(target->transient);
3526 TAILQ_REMOVE(wl, source, entry);
3528 TAILQ_INSERT_TAIL(wl, source, entry);
3530 TAILQ_INSERT_BEFORE(target, source, entry);
3532 case SWM_ARG_ID_SWAPNEXT:
3533 target = TAILQ_NEXT(source, entry);
3534 /* move the parent and let the sort handle the move */
3535 if (source->transient)
3536 source = find_window(source->transient);
3537 TAILQ_REMOVE(wl, source, entry);
3539 TAILQ_INSERT_HEAD(wl, source, entry);
3541 TAILQ_INSERT_AFTER(wl, target, source, entry);
3543 case SWM_ARG_ID_SWAPMAIN:
3544 target = TAILQ_FIRST(wl);
3545 if (target == source) {
3546 if (source->ws->focus_prev != NULL &&
3547 source->ws->focus_prev != target)
3548 source = source->ws->focus_prev;
3552 if (target == NULL || source == NULL)
3554 source->ws->focus_prev = target;
3555 TAILQ_REMOVE(wl, target, entry);
3556 TAILQ_INSERT_BEFORE(source, target, entry);
3557 TAILQ_REMOVE(wl, source, entry);
3558 TAILQ_INSERT_HEAD(wl, source, entry);
3560 case SWM_ARG_ID_MOVELAST:
3561 TAILQ_REMOVE(wl, source, entry);
3562 TAILQ_INSERT_TAIL(wl, source, entry);
3565 DNPRINTF(SWM_D_MOVE, "swapwin: invalid id: %d\n", args->id);
3577 get_focus_prev(struct ws_win *win)
3579 struct ws_win *winfocus = NULL;
3580 struct ws_win *cur_focus = NULL;
3581 struct ws_win_list *wl = NULL;
3582 struct workspace *ws = NULL;
3584 if (!(win && win->ws))
3589 cur_focus = ws->focus;
3591 DNPRINTF(SWM_D_FOCUS, "get_focus_prev: window: 0x%x, cur_focus: 0x%x\n",
3592 WINID(win), WINID(cur_focus));
3594 /* pickle, just focus on whatever */
3595 if (cur_focus == NULL) {
3596 /* use prev_focus if valid */
3597 if (ws->focus_prev && ws->focus_prev != cur_focus &&
3598 find_window(WINID(ws->focus_prev)))
3599 winfocus = ws->focus_prev;
3603 /* if transient focus on parent */
3604 if (cur_focus->transient) {
3605 winfocus = find_window(cur_focus->transient);
3609 /* if in max_stack try harder */
3610 if ((win->quirks & SWM_Q_FOCUSPREV) ||
3611 (ws->cur_layout->flags & SWM_L_FOCUSPREV)) {
3612 if (cur_focus != ws->focus_prev)
3613 winfocus = ws->focus_prev;
3615 winfocus = TAILQ_PREV(win, ws_win_list, entry);
3620 DNPRINTF(SWM_D_FOCUS, "get_focus_prev: focus_close: %d\n", focus_close);
3622 if (winfocus == NULL || winfocus == win) {
3623 switch (focus_close) {
3624 case SWM_STACK_BOTTOM:
3625 TAILQ_FOREACH(winfocus, wl, entry)
3626 if (!winfocus->iconic && winfocus != cur_focus)
3630 TAILQ_FOREACH_REVERSE(winfocus, wl, ws_win_list, entry)
3631 if (!winfocus->iconic && winfocus != cur_focus)
3634 case SWM_STACK_ABOVE:
3635 winfocus = TAILQ_NEXT(cur_focus, entry);
3636 while (winfocus && winfocus->iconic)
3637 winfocus = TAILQ_NEXT(winfocus, entry);
3639 if (winfocus == NULL) {
3640 if (focus_close_wrap) {
3641 TAILQ_FOREACH(winfocus, wl, entry)
3642 if (!winfocus->iconic &&
3643 winfocus != cur_focus)
3646 TAILQ_FOREACH_REVERSE(winfocus, wl,
3648 if (!winfocus->iconic &&
3649 winfocus != cur_focus)
3654 case SWM_STACK_BELOW:
3655 winfocus = TAILQ_PREV(cur_focus, ws_win_list, entry);
3656 while (winfocus && winfocus->iconic)
3657 winfocus = TAILQ_PREV(winfocus, ws_win_list,
3660 if (winfocus == NULL) {
3661 if (focus_close_wrap) {
3662 TAILQ_FOREACH_REVERSE(winfocus, wl,
3664 if (!winfocus->iconic &&
3665 winfocus != cur_focus)
3668 TAILQ_FOREACH(winfocus, wl, entry)
3669 if (!winfocus->iconic &&
3670 winfocus != cur_focus)
3678 if (winfocus == NULL ||
3679 (winfocus && (winfocus->iconic || winfocus == cur_focus))) {
3680 if (focus_default == SWM_STACK_TOP) {
3681 TAILQ_FOREACH_REVERSE(winfocus, wl, ws_win_list, entry)
3682 if (!winfocus->iconic && winfocus != cur_focus)
3685 TAILQ_FOREACH(winfocus, wl, entry)
3686 if (!winfocus->iconic && winfocus != cur_focus)
3693 return get_focus_magic(winfocus);
3697 get_region_focus(struct swm_region *r)
3699 struct ws_win *winfocus = NULL;
3704 if (r->ws->focus && !r->ws->focus->iconic)
3705 winfocus = r->ws->focus;
3706 else if (r->ws->focus_prev && !r->ws->focus_prev->iconic)
3707 winfocus = r->ws->focus_prev;
3709 TAILQ_FOREACH(winfocus, &r->ws->winlist, entry)
3710 if (!winfocus->iconic)
3713 return get_focus_magic(winfocus);
3717 focus(struct swm_region *r, union arg *args)
3719 struct ws_win *head, *cur_focus = NULL, *winfocus = NULL;
3720 struct ws_win_list *wl = NULL;
3721 struct workspace *ws = NULL;
3727 DNPRINTF(SWM_D_FOCUS, "focus: id: %d\n", args->id);
3729 if ((cur_focus = r->ws->focus) == NULL)
3733 if (TAILQ_EMPTY(wl))
3735 /* make sure there is at least one uniconified window */
3737 TAILQ_FOREACH(winfocus, wl, entry)
3738 if (!winfocus->iconic) {
3746 case SWM_ARG_ID_FOCUSPREV:
3747 head = TAILQ_PREV(cur_focus, ws_win_list, entry);
3749 head = TAILQ_LAST(wl, ws_win_list);
3751 if (WINID(winfocus) == cur_focus->transient) {
3752 head = TAILQ_PREV(winfocus, ws_win_list, entry);
3754 head = TAILQ_LAST(wl, ws_win_list);
3759 if (winfocus && winfocus->iconic) {
3760 while (winfocus != cur_focus) {
3761 if (winfocus == NULL)
3762 winfocus = TAILQ_LAST(wl, ws_win_list);
3763 if (!winfocus->iconic)
3765 winfocus = TAILQ_PREV(winfocus, ws_win_list,
3771 case SWM_ARG_ID_FOCUSNEXT:
3772 head = TAILQ_NEXT(cur_focus, entry);
3774 head = TAILQ_FIRST(wl);
3778 if (winfocus && winfocus->iconic) {
3779 while (winfocus != cur_focus) {
3780 if (winfocus == NULL)
3781 winfocus = TAILQ_FIRST(wl);
3782 if (!winfocus->iconic)
3784 winfocus = TAILQ_NEXT(winfocus, entry);
3789 case SWM_ARG_ID_FOCUSMAIN:
3790 winfocus = TAILQ_FIRST(wl);
3791 if (winfocus == cur_focus)
3792 winfocus = cur_focus->ws->focus_prev;
3799 focus_win(get_focus_magic(winfocus));
3805 cycle_layout(struct swm_region *r, union arg *args)
3807 struct workspace *ws = r->ws;
3809 /* suppress unused warning since var is needed */
3812 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
3815 if (ws->cur_layout->l_stack == NULL)
3816 ws->cur_layout = &layouts[0];
3821 focus_win(get_region_focus(r));
3827 stack_config(struct swm_region *r, union arg *args)
3829 struct workspace *ws = r->ws;
3831 DNPRINTF(SWM_D_STACK, "stack_config: id: %d workspace: %d\n",
3834 if (ws->cur_layout->l_config != NULL)
3835 ws->cur_layout->l_config(ws, args->id);
3837 if (args->id != SWM_ARG_ID_STACKINIT)
3846 struct swm_geometry g;
3847 struct swm_region *r;
3853 DNPRINTF(SWM_D_STACK, "stack: begin\n");
3855 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
3856 for (i = 0; i < num_screens; i++) {
3860 TAILQ_FOREACH(r, &screens[i].rl, entry) {
3861 DNPRINTF(SWM_D_STACK, "stack: workspace: %d "
3862 "(screen: %d, region: %d)\n", r->ws->idx, i, j++);
3864 /* start with screen geometry, adjust for bar */
3866 g.w -= 2 * border_width;
3867 g.h -= 2 * border_width;
3868 if (bar_enabled && r->ws->bar_enabled) {
3873 r->ws->cur_layout->l_stack(r->ws, &g);
3874 r->ws->cur_layout->l_string(r->ws);
3875 /* save r so we can track region changes */
3882 DNPRINTF(SWM_D_STACK, "stack: end\n");
3886 store_float_geom(struct ws_win *win, struct swm_region *r)
3888 if (win == NULL || r == NULL)
3891 /* retain window geom and region geom */
3892 win->g_float = win->g;
3893 win->g_float.x -= X(r);
3894 win->g_float.y -= Y(r);
3895 win->g_floatvalid = 1;
3896 DNPRINTF(SWM_D_MISC, "store_float_geom: window: 0x%x, g: (%d,%d)"
3897 " %d x %d, g_float: (%d,%d) %d x %d\n", win->id, X(win), Y(win),
3898 WIDTH(win), HEIGHT(win), win->g_float.x, win->g_float.y,
3899 win->g_float.w, win->g_float.h);
3903 load_float_geom(struct ws_win *win, struct swm_region *r)
3905 if (win == NULL || r == NULL)
3908 if (win->g_floatvalid) {
3909 win->g = win->g_float;
3912 DNPRINTF(SWM_D_MISC, "load_float_geom: window: 0x%x, g: (%d,%d)"
3913 "%d x %d\n", win->id, X(win), Y(win), WIDTH(win),
3916 DNPRINTF(SWM_D_MISC, "load_float_geom: window: 0x%x, g_float "
3917 "is not set.\n", win->id);
3922 stack_floater(struct ws_win *win, struct swm_region *r)
3927 DNPRINTF(SWM_D_MISC, "stack_floater: window: 0x%x\n", win->id);
3930 * to allow windows to change their size (e.g. mplayer fs) only retrieve
3931 * geom on ws switches or return from max mode
3933 if (win->floatmaxed || (r != r->ws->old_r &&
3934 !(win->ewmh_flags & EWMH_F_FULLSCREEN))) {
3935 /* update geometry for the new region */
3936 load_float_geom(win, r);
3939 win->floatmaxed = 0;
3942 * if set to fullscreen mode, configure window to maximum size.
3944 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
3945 if (!win->g_floatvalid)
3946 store_float_geom(win, win->ws->r);
3952 * remove border on fullscreen floater when in fullscreen mode or when
3953 * the quirk is present.
3955 if ((win->ewmh_flags & EWMH_F_FULLSCREEN) ||
3956 ((win->quirks & SWM_Q_FULLSCREEN) &&
3957 (WIDTH(win) >= WIDTH(r)) && (HEIGHT(win) >= HEIGHT(r)))) {
3958 if (win->bordered) {
3960 X(win) += border_width;
3961 Y(win) += border_width;
3963 } else if (!win->bordered) {
3965 X(win) -= border_width;
3966 Y(win) -= border_width;
3969 if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
3970 WIDTH(win) = (double)WIDTH(r) * dialog_ratio;
3971 HEIGHT(win) = (double)HEIGHT(r) * dialog_ratio;
3974 if (!win->manual && !(win->ewmh_flags & EWMH_F_FULLSCREEN) &&
3975 !(win->quirks & SWM_Q_ANYWHERE)) {
3977 * floaters and transients are auto-centred unless moved,
3978 * resized or ANYWHERE quirk is set.
3980 X(win) = X(r) + (WIDTH(r) - WIDTH(win)) / 2 - BORDER(win);
3981 Y(win) = Y(r) + (HEIGHT(r) - HEIGHT(win)) / 2 - BORDER(win);
3983 store_float_geom(win, r);
3986 /* keep window within region bounds */
3987 constrain_window(win, r, 0);
3993 * Send keystrokes to terminal to decrease/increase the font size as the
3994 * window size changes.
3997 adjust_font(struct ws_win *win)
3999 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
4000 win->floating || win->transient)
4003 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
4004 WIDTH(win) / win->sh.width_inc < term_width &&
4005 win->font_steps < SWM_MAX_FONT_STEPS) {
4006 win->font_size_boundary[win->font_steps] =
4007 (win->sh.width_inc * term_width) + win->sh.base_width;
4010 win->last_inc = win->sh.width_inc;
4011 fake_keypress(win, XK_KP_Subtract, XCB_MOD_MASK_SHIFT);
4012 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
4013 WIDTH(win) > win->font_size_boundary[win->font_steps - 1]) {
4016 win->last_inc = win->sh.width_inc;
4017 fake_keypress(win, XK_KP_Add, XCB_MOD_MASK_SHIFT);
4021 #define SWAPXY(g) do { \
4023 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
4024 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
4027 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
4029 struct swm_geometry win_g, r_g = *g;
4030 struct ws_win *win, *fs_win = NULL;
4031 int i, j, s, stacks;
4032 int w_inc = 1, h_inc, w_base = 1, h_base;
4033 int hrh, extra = 0, h_slice, last_h = 0;
4034 int split, colno, winno, mwin, msize, mscale;
4035 int remain, missing, v_slice, reconfigure = 0;
4038 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d, rot: %s, "
4039 "flip: %s\n", ws->idx, YESNO(rot), YESNO(flip));
4041 winno = count_win(ws, 0);
4042 if (winno == 0 && count_win(ws, 1) == 0)
4045 TAILQ_FOREACH(win, &ws->winlist, entry)
4046 if (!win->transient && !win->floating && !win->iconic)
4053 w_inc = win->sh.width_inc;
4054 w_base = win->sh.base_width;
4055 mwin = ws->l_state.horizontal_mwin;
4056 mscale = ws->l_state.horizontal_msize;
4057 stacks = ws->l_state.horizontal_stacks;
4060 w_inc = win->sh.height_inc;
4061 w_base = win->sh.base_height;
4062 mwin = ws->l_state.vertical_mwin;
4063 mscale = ws->l_state.vertical_msize;
4064 stacks = ws->l_state.vertical_stacks;
4068 if (stacks > winno - mwin)
4069 stacks = winno - mwin;
4073 h_slice = r_g.h / SWM_H_SLICE;
4074 if (mwin && winno > mwin) {
4075 v_slice = r_g.w / SWM_V_SLICE;
4079 win_g.w = v_slice * mscale;
4081 if (w_inc > 1 && w_inc < v_slice) {
4082 /* adjust for window's requested size increment */
4083 remain = (win_g.w - w_base) % w_inc;
4089 win_g.x += r_g.w - msize;
4092 colno = split = winno / stacks;
4093 win_g.w = ((r_g.w - (stacks * 2 * border_width) +
4094 2 * border_width) / stacks);
4096 hrh = r_g.h / colno;
4097 extra = r_g.h - (colno * hrh);
4098 win_g.h = hrh - 2 * border_width;
4100 /* stack all the tiled windows */
4101 i = j = 0, s = stacks;
4102 TAILQ_FOREACH(win, &ws->winlist, entry) {
4103 if (win->transient || win->floating)
4108 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
4113 if (split && i == split) {
4114 colno = (winno - mwin) / stacks;
4115 if (s <= (winno - mwin) % stacks)
4117 split = split + colno;
4118 hrh = (r_g.h / colno);
4119 extra = r_g.h - (colno * hrh);
4123 win_g.x += win_g.w + 2 * border_width;
4124 win_g.w = (r_g.w - msize -
4125 (stacks * 2 * border_width)) / stacks;
4127 win_g.w += (r_g.w - msize -
4128 (stacks * 2 * border_width)) % stacks;
4132 win_g.h = hrh - 2 * border_width;
4134 h_inc = win->sh.width_inc;
4135 h_base = win->sh.base_width;
4137 h_inc = win->sh.height_inc;
4138 h_base = win->sh.base_height;
4140 if (j == colno - 1) {
4141 win_g.h = hrh + extra;
4142 } else if (h_inc > 1 && h_inc < h_slice) {
4143 /* adjust for window's requested size increment */
4144 remain = (win_g.h - h_base) % h_inc;
4145 missing = h_inc - remain;
4147 if (missing <= extra || j == 0) {
4159 win_g.y += last_h + 2 * border_width;
4161 if (disable_border && !(bar_enabled && ws->bar_enabled) &&
4164 win_g.w += 2 * border_width;
4165 win_g.h += 2 * border_width;
4170 if (X(win) != win_g.y || Y(win) != win_g.x ||
4171 WIDTH(win) != win_g.h || HEIGHT(win) != win_g.w) {
4175 WIDTH(win) = win_g.h;
4176 HEIGHT(win) = win_g.w;
4179 if (X(win) != win_g.x || Y(win) != win_g.y ||
4180 WIDTH(win) != win_g.w || HEIGHT(win) != win_g.h) {
4184 WIDTH(win) = win_g.w;
4185 HEIGHT(win) = win_g.h;
4189 if (bordered != win->bordered) {
4191 win->bordered = bordered;
4207 /* now, stack all the floaters and transients */
4208 TAILQ_FOREACH(win, &ws->winlist, entry) {
4209 if (!win->transient && !win->floating)
4213 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
4218 stack_floater(win, ws->r);
4222 /* Make sure fs_win is stacked last so it's on top. */
4224 stack_floater(fs_win, ws->r);
4230 vertical_config(struct workspace *ws, int id)
4232 DNPRINTF(SWM_D_STACK, "vertical_config: id: %d, workspace: %d\n",
4236 case SWM_ARG_ID_STACKRESET:
4237 case SWM_ARG_ID_STACKINIT:
4238 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
4239 ws->l_state.vertical_mwin = 1;
4240 ws->l_state.vertical_stacks = 1;
4242 case SWM_ARG_ID_MASTERSHRINK:
4243 if (ws->l_state.vertical_msize > 1)
4244 ws->l_state.vertical_msize--;
4246 case SWM_ARG_ID_MASTERGROW:
4247 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
4248 ws->l_state.vertical_msize++;
4250 case SWM_ARG_ID_MASTERADD:
4251 ws->l_state.vertical_mwin++;
4253 case SWM_ARG_ID_MASTERDEL:
4254 if (ws->l_state.vertical_mwin > 0)
4255 ws->l_state.vertical_mwin--;
4257 case SWM_ARG_ID_STACKINC:
4258 ws->l_state.vertical_stacks++;
4260 case SWM_ARG_ID_STACKDEC:
4261 if (ws->l_state.vertical_stacks > 1)
4262 ws->l_state.vertical_stacks--;
4264 case SWM_ARG_ID_FLIPLAYOUT:
4265 ws->l_state.vertical_flip = !ws->l_state.vertical_flip;
4273 vertical_stack(struct workspace *ws, struct swm_geometry *g)
4275 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
4277 stack_master(ws, g, 0, ws->l_state.vertical_flip);
4281 horizontal_config(struct workspace *ws, int id)
4283 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
4286 case SWM_ARG_ID_STACKRESET:
4287 case SWM_ARG_ID_STACKINIT:
4288 ws->l_state.horizontal_mwin = 1;
4289 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
4290 ws->l_state.horizontal_stacks = 1;
4292 case SWM_ARG_ID_MASTERSHRINK:
4293 if (ws->l_state.horizontal_msize > 1)
4294 ws->l_state.horizontal_msize--;
4296 case SWM_ARG_ID_MASTERGROW:
4297 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
4298 ws->l_state.horizontal_msize++;
4300 case SWM_ARG_ID_MASTERADD:
4301 ws->l_state.horizontal_mwin++;
4303 case SWM_ARG_ID_MASTERDEL:
4304 if (ws->l_state.horizontal_mwin > 0)
4305 ws->l_state.horizontal_mwin--;
4307 case SWM_ARG_ID_STACKINC:
4308 ws->l_state.horizontal_stacks++;
4310 case SWM_ARG_ID_STACKDEC:
4311 if (ws->l_state.horizontal_stacks > 1)
4312 ws->l_state.horizontal_stacks--;
4314 case SWM_ARG_ID_FLIPLAYOUT:
4315 ws->l_state.horizontal_flip = !ws->l_state.horizontal_flip;
4323 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
4325 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
4327 stack_master(ws, g, 1, ws->l_state.horizontal_flip);
4330 /* fullscreen view */
4332 max_stack(struct workspace *ws, struct swm_geometry *g)
4334 struct swm_geometry gg = *g;
4335 struct ws_win *w, *win = NULL, *parent = NULL;
4336 int winno, num_screens;
4338 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
4343 winno = count_win(ws, 0);
4344 if (winno == 0 && count_win(ws, 1) == 0)
4347 /* Figure out which top level window should be visible. */
4348 if (ws->focus_pending)
4349 win = ws->focus_pending;
4353 win = TAILQ_FIRST(&ws->winlist);
4356 parent = find_window(win->transient);
4358 DNPRINTF(SWM_D_STACK, "max_stack: win: 0x%x\n", win->id);
4360 /* maximize all top level windows */
4361 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
4362 TAILQ_FOREACH(w, &ws->winlist, entry) {
4363 if (w->transient || w->iconic)
4366 if (w->floating && !w->floatmaxed) {
4368 * retain geometry for retrieval on exit from
4371 store_float_geom(w, ws->r);
4375 /* only reconfigure if necessary */
4376 if (X(w) != gg.x || Y(w) != gg.y || WIDTH(w) != gg.w ||
4377 HEIGHT(w) != gg.h) {
4379 if (bar_enabled && ws->bar_enabled){
4383 WIDTH(w) += 2 * border_width;
4384 HEIGHT(w) += 2 * border_width;
4390 /* Unmap unwanted windows if not multi-screen. */
4391 if (num_screens <= 1 && outputs <= 1 && w != win &&
4392 w != parent && w->transient != win->id)
4396 /* If a parent exists, map it first. */
4400 /* Map siblings next. */
4401 TAILQ_FOREACH(w, &ws->winlist, entry)
4402 if (w != win && !w->iconic &&
4403 w->transient == parent->id) {
4404 stack_floater(w, ws->r);
4409 /* Map focused window. */
4412 /* Finally, map children of focus window. */
4413 TAILQ_FOREACH(w, &ws->winlist, entry)
4414 if (w->transient == win->id && !w->iconic) {
4415 stack_floater(w, ws->r);
4421 send_to_ws(struct swm_region *r, union arg *args)
4423 int wsid = args->id;
4424 struct ws_win *win = NULL, *parent;
4425 struct workspace *ws, *nws, *pws;
4426 char ws_idx_str[SWM_PROPLEN];
4428 if (wsid >= workspace_limit)
4431 if (r && r->ws && r->ws->focus)
4436 if (win->ws->idx == wsid)
4439 DNPRINTF(SWM_D_MOVE, "send_to_ws: win 0x%x, ws %d -> %d\n", win->id,
4440 win->ws->idx, wsid);
4443 nws = &win->s->ws[wsid];
4445 /* Update the window's workspace property: _SWM_WS */
4446 if (snprintf(ws_idx_str, SWM_PROPLEN, "%d", nws->idx) < SWM_PROPLEN) {
4447 if (focus_mode != SWM_FOCUS_FOLLOW)
4448 ws->focus_pending = get_focus_prev(win);
4450 /* Move the parent if this is a transient window. */
4451 if (win->transient) {
4452 parent = find_window(win->transient);
4455 /* Set new focus in parent's ws if needed. */
4456 if (pws->focus == parent) {
4457 if (focus_mode != SWM_FOCUS_FOLLOW)
4458 pws->focus_pending =
4459 get_focus_prev(parent);
4461 unfocus_win(parent);
4463 if (focus_mode != SWM_FOCUS_FOLLOW)
4464 pws->focus = pws->focus_pending;
4466 if (focus_mode != SWM_FOCUS_FOLLOW)
4467 pws->focus_pending = NULL;
4470 /* Don't unmap parent if new ws is visible */
4472 unmap_window(parent);
4475 TAILQ_REMOVE(&ws->winlist, parent, entry);
4476 TAILQ_INSERT_TAIL(&nws->winlist, parent, entry);
4479 DNPRINTF(SWM_D_PROP, "send_to_ws: set "
4480 "property: _SWM_WS: %s\n", ws_idx_str);
4481 xcb_change_property(conn, XCB_PROP_MODE_REPLACE,
4482 parent->id, a_swm_ws, XCB_ATOM_STRING, 8,
4483 strlen(ws_idx_str), ws_idx_str);
4489 /* Don't unmap if new ws is visible */
4494 TAILQ_REMOVE(&ws->winlist, win, entry);
4495 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
4498 /* Set focus on new ws. */
4499 unfocus_win(nws->focus);
4502 DNPRINTF(SWM_D_PROP, "send_to_ws: set property: _SWM_WS: %s\n",
4504 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
4505 a_swm_ws, XCB_ATOM_STRING, 8, strlen(ws_idx_str),
4508 /* Restack and set new focus. */
4511 if (focus_mode != SWM_FOCUS_FOLLOW) {
4512 focus_win(ws->focus_pending);
4513 ws->focus_pending = NULL;
4519 DNPRINTF(SWM_D_MOVE, "send_to_ws: done.\n");
4523 pressbutton(struct swm_region *r, union arg *args)
4525 /* suppress unused warning since var is needed */
4528 xcb_test_fake_input(conn, XCB_BUTTON_PRESS, args->id,
4529 XCB_CURRENT_TIME, XCB_WINDOW_NONE, 0, 0, 0);
4530 xcb_test_fake_input(conn, XCB_BUTTON_RELEASE, args->id,
4531 XCB_CURRENT_TIME, XCB_WINDOW_NONE, 0, 0, 0);
4535 raise_toggle(struct swm_region *r, union arg *args)
4537 /* suppress unused warning since var is needed */
4540 if (r == NULL || r->ws == NULL)
4543 r->ws->always_raise = !r->ws->always_raise;
4545 /* bring floaters back to top */
4546 if (!r->ws->always_raise)
4553 iconify(struct swm_region *r, union arg *args)
4555 /* suppress unused warning since var is needed */
4558 if (r->ws->focus == NULL)
4561 set_swm_iconic(r->ws->focus, 1);
4567 get_win_name(xcb_window_t win)
4570 xcb_get_property_cookie_t c;
4571 xcb_get_property_reply_t *r;
4573 /* First try _NET_WM_NAME for UTF-8. */
4574 c = xcb_get_property(conn, 0, win, a_netwmname,
4575 XCB_GET_PROPERTY_TYPE_ANY, 0, UINT_MAX);
4576 r = xcb_get_property_reply(conn, c, NULL);
4579 if (r->type == XCB_NONE) {
4581 /* Use WM_NAME instead; no UTF-8. */
4582 c = xcb_get_property(conn, 0, win, XCB_ATOM_WM_NAME,
4583 XCB_GET_PROPERTY_TYPE_ANY, 0, UINT_MAX);
4584 r = xcb_get_property_reply(conn, c, NULL);
4588 if (r->type == XCB_NONE) {
4594 name = strndup(xcb_get_property_value(r),
4595 xcb_get_property_value_length(r));
4604 uniconify(struct swm_region *r, union arg *args)
4611 DNPRINTF(SWM_D_MISC, "uniconify\n");
4613 if (r == NULL || r->ws == NULL)
4616 /* make sure we have anything to uniconify */
4617 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
4618 if (win->ws == NULL)
4619 continue; /* should never happen */
4628 search_resp_action = SWM_SEARCH_UNICONIFY;
4630 spawn_select(r, args, "search", &searchpid);
4632 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
4635 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
4636 if (win->ws == NULL)
4637 continue; /* should never happen */
4641 name = get_win_name(win->id);
4644 fprintf(lfile, "%s.%u\n", name, win->id);
4652 name_workspace(struct swm_region *r, union arg *args)
4656 DNPRINTF(SWM_D_MISC, "name_workspace\n");
4662 search_resp_action = SWM_SEARCH_NAME_WORKSPACE;
4664 spawn_select(r, args, "name_workspace", &searchpid);
4666 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
4669 fprintf(lfile, "%s", "");
4674 search_workspace(struct swm_region *r, union arg *args)
4677 struct workspace *ws;
4680 DNPRINTF(SWM_D_MISC, "search_workspace\n");
4686 search_resp_action = SWM_SEARCH_SEARCH_WORKSPACE;
4688 spawn_select(r, args, "search", &searchpid);
4690 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
4693 for (i = 0; i < workspace_limit; i++) {
4697 fprintf(lfile, "%d%s%s\n", ws->idx + 1,
4698 (ws->name ? ":" : ""), (ws->name ? ws->name : ""));
4705 search_win_cleanup(void)
4707 struct search_window *sw = NULL;
4709 while ((sw = TAILQ_FIRST(&search_wl)) != NULL) {
4710 xcb_destroy_window(conn, sw->indicator);
4711 TAILQ_REMOVE(&search_wl, sw, entry);
4717 search_win(struct swm_region *r, union arg *args)
4719 struct ws_win *win = NULL;
4720 struct search_window *sw = NULL;
4723 int i, width, height;
4731 XRectangle l_ibox, l_lbox;
4733 DNPRINTF(SWM_D_MISC, "search_win\n");
4736 search_resp_action = SWM_SEARCH_SEARCH_WINDOW;
4738 spawn_select(r, args, "search", &searchpid);
4740 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
4743 TAILQ_INIT(&search_wl);
4746 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
4750 sw = calloc(1, sizeof(struct search_window));
4752 warn("search_win: calloc");
4754 search_win_cleanup();
4760 snprintf(s, sizeof s, "%d", i);
4763 w = xcb_generate_id(conn);
4764 wa[0] = r->s->c[SWM_S_COLOR_FOCUS].pixel;
4765 wa[1] = r->s->c[SWM_S_COLOR_UNFOCUS].pixel;
4767 if (bar_font_legacy) {
4768 XmbTextExtents(bar_fs, s, len, &l_ibox, &l_lbox);
4769 width = l_lbox.width + 4;
4770 height = bar_fs_extents->max_logical_extent.height + 4;
4772 XftTextExtentsUtf8(display, bar_font, (FcChar8 *)s, len,
4774 width = info.width + 4;
4775 height = bar_font->height + 4;
4778 xcb_create_window(conn, XCB_COPY_FROM_PARENT, w, win->id, 0, 0,
4779 width, height, 1, XCB_WINDOW_CLASS_INPUT_OUTPUT,
4780 XCB_COPY_FROM_PARENT, XCB_CW_BACK_PIXEL |
4781 XCB_CW_BORDER_PIXEL, wa);
4783 xcb_map_window(conn, w);
4786 TAILQ_INSERT_TAIL(&search_wl, sw, entry);
4788 if (bar_font_legacy) {
4789 l_gcv.graphics_exposures = 0;
4790 l_draw = XCreateGC(display, w, 0, &l_gcv);
4792 XSetForeground(display, l_draw,
4793 r->s->c[SWM_S_COLOR_BAR].pixel);
4795 DRAWSTRING(display, w, bar_fs, l_draw, 2,
4796 (bar_fs_extents->max_logical_extent.height -
4797 l_lbox.height) / 2 - l_lbox.y, s, len);
4799 XFreeGC(display, l_draw);
4802 draw = XftDrawCreate(display, w,
4803 DefaultVisual(display, r->s->idx),
4804 DefaultColormap(display, r->s->idx));
4806 XftDrawStringUtf8(draw, &bar_font_color, bar_font, 2,
4807 (HEIGHT(r->bar) + bar_font->height) / 2 -
4808 bar_font->descent, (FcChar8 *)s, len);
4810 XftDrawDestroy(draw);
4813 DNPRINTF(SWM_D_MISC, "search_win: mapped window: 0x%x\n", w);
4815 fprintf(lfile, "%d\n", i);
4825 search_resp_uniconify(const char *resp, unsigned long len)
4831 DNPRINTF(SWM_D_MISC, "search_resp_uniconify: resp: %s\n", resp);
4833 TAILQ_FOREACH(win, &search_r->ws->winlist, entry) {
4836 name = get_win_name(win->id);
4839 if (asprintf(&s, "%s.%u", name, win->id) == -1) {
4844 if (strncmp(s, resp, len) == 0) {
4845 /* XXX this should be a callback to generalize */
4846 set_swm_iconic(win, 0);
4855 search_resp_name_workspace(const char *resp, unsigned long len)
4857 struct workspace *ws;
4859 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: resp: %s\n", resp);
4861 if (search_r->ws == NULL)
4866 free(search_r->ws->name);
4867 search_r->ws->name = NULL;
4871 ws->name = strdup(resp);
4872 if (ws->name == NULL) {
4873 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: "
4874 "strdup: %s", strerror(errno));
4881 search_resp_search_workspace(const char *resp)
4888 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: resp: %s\n", resp);
4892 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: strdup: %s",
4899 ws_idx = (int)strtonum(q, 1, workspace_limit, &errstr);
4901 DNPRINTF(SWM_D_MISC, "workspace idx is %s: %s",
4908 switchws(search_r, &a);
4912 search_resp_search_window(const char *resp)
4917 struct search_window *sw;
4919 DNPRINTF(SWM_D_MISC, "search_resp_search_window: resp: %s\n", resp);
4923 DNPRINTF(SWM_D_MISC, "search_resp_search_window: strdup: %s",
4928 idx = (int)strtonum(s, 1, INT_MAX, &errstr);
4930 DNPRINTF(SWM_D_MISC, "window idx is %s: %s",
4937 TAILQ_FOREACH(sw, &search_wl, entry)
4938 if (idx == sw->idx) {
4944 #define MAX_RESP_LEN 1024
4947 search_do_resp(void)
4953 DNPRINTF(SWM_D_MISC, "search_do_resp:\n");
4958 if ((resp = calloc(1, MAX_RESP_LEN + 1)) == NULL) {
4959 warn("search: calloc");
4963 rbytes = read(select_resp_pipe[0], resp, MAX_RESP_LEN);
4965 warn("search: read error");
4968 resp[rbytes] = '\0';
4971 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
4972 * newline, so work around that by sanitizing the resp now.
4974 resp[strcspn(resp, "\n")] = '\0';
4977 switch (search_resp_action) {
4978 case SWM_SEARCH_UNICONIFY:
4979 search_resp_uniconify(resp, len);
4981 case SWM_SEARCH_NAME_WORKSPACE:
4982 search_resp_name_workspace(resp, len);
4984 case SWM_SEARCH_SEARCH_WORKSPACE:
4985 search_resp_search_workspace(resp);
4987 case SWM_SEARCH_SEARCH_WINDOW:
4988 search_resp_search_window(resp);
4993 if (search_resp_action == SWM_SEARCH_SEARCH_WINDOW)
4994 search_win_cleanup();
4996 search_resp_action = SWM_SEARCH_NONE;
4997 close(select_resp_pipe[0]);
5004 wkill(struct swm_region *r, union arg *args)
5006 DNPRINTF(SWM_D_MISC, "wkill: id: %d\n", args->id);
5008 if (r->ws->focus == NULL)
5011 if (args->id == SWM_ARG_ID_KILLWINDOW)
5012 xcb_kill_client(conn, r->ws->focus->id);
5014 if (r->ws->focus->can_delete)
5015 client_msg(r->ws->focus, a_delete, 0);
5021 floating_toggle_win(struct ws_win *win)
5023 struct swm_region *r;
5033 /* reject floating toggles in max stack mode */
5034 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK])
5037 if (win->floating) {
5038 if (!win->floatmaxed) {
5039 /* retain position for refloat */
5040 store_float_geom(win, r);
5044 load_float_geom(win, r);
5048 ewmh_update_actions(win);
5054 floating_toggle(struct swm_region *r, union arg *args)
5056 struct ws_win *win = r->ws->focus;
5058 /* suppress unused warning since var is needed */
5064 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
5067 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
5068 _NET_WM_STATE_TOGGLE);
5072 if (win == win->ws->focus)
5079 constrain_window(struct ws_win *win, struct swm_region *r, int resizable)
5081 if (MAX_X(win) + BORDER(win) > MAX_X(r)) {
5083 WIDTH(win) = MAX_X(r) - X(win) - BORDER(win);
5085 X(win) = MAX_X(r)- WIDTH(win) - BORDER(win);
5088 if (X(win) + BORDER(win) < X(r)) {
5090 WIDTH(win) -= X(r) - X(win) - BORDER(win);
5092 X(win) = X(r) - BORDER(win);
5095 if (MAX_Y(win) + BORDER(win) > MAX_Y(r)) {
5097 HEIGHT(win) = MAX_Y(r) - Y(win) - BORDER(win);
5099 Y(win) = MAX_Y(r) - HEIGHT(win) - BORDER(win);
5102 if (Y(win) + BORDER(win) < Y(r)) {
5104 HEIGHT(win) -= Y(r) - Y(win) - BORDER(win);
5106 Y(win) = Y(r) - BORDER(win);
5112 if (HEIGHT(win) < 1)
5118 update_window(struct ws_win *win)
5123 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
5124 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
5125 XCB_CONFIG_WINDOW_BORDER_WIDTH;
5129 wc[3] = HEIGHT(win);
5130 wc[4] = BORDER(win);
5132 DNPRINTF(SWM_D_EVENT, "update_window: window: 0x%x, (x,y) w x h: "
5133 "(%d,%d) %d x %d, bordered: %s\n", win->id, wc[0], wc[1], wc[2],
5134 wc[3], YESNO(win->bordered));
5136 xcb_configure_window(conn, win->id, mask, wc);
5139 #define SWM_RESIZE_STEPS (50)
5142 resize(struct ws_win *win, union arg *args)
5144 xcb_timestamp_t timestamp = 0;
5145 struct swm_region *r = NULL;
5147 struct swm_geometry g;
5148 int top = 0, left = 0, resizing;
5150 xcb_cursor_t cursor;
5151 xcb_query_pointer_reply_t *xpr;
5152 xcb_generic_event_t *evt;
5153 xcb_motion_notify_event_t *mne;
5159 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
5162 DNPRINTF(SWM_D_EVENT, "resize: window: 0x%x, floating: %s, "
5163 "transient: 0x%x\n", win->id, YESNO(win->floating),
5166 if (!win->transient && !win->floating)
5169 /* reject resizes in max mode for floaters (transient ok) */
5170 if (win->floatmaxed)
5174 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
5182 case SWM_ARG_ID_WIDTHSHRINK:
5183 WIDTH(win) -= SWM_RESIZE_STEPS;
5186 case SWM_ARG_ID_WIDTHGROW:
5187 WIDTH(win) += SWM_RESIZE_STEPS;
5190 case SWM_ARG_ID_HEIGHTSHRINK:
5191 HEIGHT(win) -= SWM_RESIZE_STEPS;
5194 case SWM_ARG_ID_HEIGHTGROW:
5195 HEIGHT(win) += SWM_RESIZE_STEPS;
5202 constrain_window(win, r, 1);
5204 store_float_geom(win,r);
5208 /* get cursor offset from window root */
5209 xpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, win->id),
5216 if (xpr->win_x < WIDTH(win) / 2)
5219 if (xpr->win_y < HEIGHT(win) / 2)
5222 if (args->id == SWM_ARG_ID_CENTER)
5223 cursor = cursors[XC_SIZING].cid;
5225 cursor = cursors[left ? XC_TOP_LEFT_CORNER :
5226 XC_TOP_RIGHT_CORNER].cid;
5228 cursor = cursors[left ? XC_BOTTOM_LEFT_CORNER :
5229 XC_BOTTOM_RIGHT_CORNER].cid;
5231 xcb_grab_pointer(conn, 0, win->id, MOUSEMASK,
5232 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, XCB_WINDOW_NONE, cursor,
5237 while ((evt = xcb_wait_for_event(conn)) && resizing) {
5238 switch (XCB_EVENT_RESPONSE_TYPE(evt)) {
5239 case XCB_BUTTON_RELEASE:
5240 DNPRINTF(SWM_D_EVENT, "resize: BUTTON_RELEASE\n");
5243 case XCB_MOTION_NOTIFY:
5244 mne = (xcb_motion_notify_event_t *)evt;
5245 /* cursor offset/delta from start of the operation */
5246 dx = mne->root_x - xpr->root_x;
5247 dy = mne->root_y - xpr->root_y;
5253 if (args->id == SWM_ARG_ID_CENTER) {
5254 if (g.h / 2 + dy < 1)
5258 HEIGHT(win) = g.h + 2 * dy;
5266 HEIGHT(win) = g.h + dy;
5273 if (args->id == SWM_ARG_ID_CENTER) {
5274 if (g.w / 2 + dx < 1)
5278 WIDTH(win) = g.w + 2 * dx;
5286 WIDTH(win) = g.w + dx;
5289 constrain_window(win, r, 1);
5291 /* not free, don't sync more than 120 times / second */
5292 if ((mne->time - timestamp) > (1000 / 120) ) {
5293 timestamp = mne->time;
5308 store_float_geom(win,r);
5310 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
5312 DNPRINTF(SWM_D_EVENT, "resize: done.\n");
5316 resize_step(struct swm_region *r, union arg *args)
5318 struct ws_win *win = NULL;
5320 if (r && r->ws && r->ws->focus)
5329 #define SWM_MOVE_STEPS (50)
5332 move(struct ws_win *win, union arg *args)
5334 xcb_timestamp_t timestamp = 0;
5335 int move_stp = 0, moving;
5336 struct swm_region *r = NULL;
5337 xcb_query_pointer_reply_t *qpr;
5338 xcb_generic_event_t *evt;
5339 xcb_motion_notify_event_t *mne;
5345 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
5348 DNPRINTF(SWM_D_EVENT, "move: window: 0x%x, floating: %s, transient: "
5349 "0x%x\n", win->id, YESNO(win->floating), win->transient);
5351 /* in max_stack mode should only move transients */
5352 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !win->transient)
5356 if (!win->floating && !win->transient) {
5357 store_float_geom(win, r);
5358 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
5361 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
5370 case SWM_ARG_ID_MOVELEFT:
5371 X(win) -= (SWM_MOVE_STEPS - border_width);
5374 case SWM_ARG_ID_MOVERIGHT:
5375 X(win) += (SWM_MOVE_STEPS - border_width);
5378 case SWM_ARG_ID_MOVEUP:
5379 Y(win) -= (SWM_MOVE_STEPS - border_width);
5382 case SWM_ARG_ID_MOVEDOWN:
5383 Y(win) += (SWM_MOVE_STEPS - border_width);
5390 constrain_window(win, r, 0);
5392 store_float_geom(win, r);
5396 xcb_grab_pointer(conn, 0, win->id, MOUSEMASK,
5397 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC,
5398 XCB_WINDOW_NONE, cursors[XC_FLEUR].cid, XCB_CURRENT_TIME);
5400 /* get cursor offset from window root */
5401 qpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, win->id),
5404 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
5410 while ((evt = xcb_wait_for_event(conn)) && moving) {
5411 switch (XCB_EVENT_RESPONSE_TYPE(evt)) {
5412 case XCB_BUTTON_RELEASE:
5413 DNPRINTF(SWM_D_EVENT, "move: BUTTON_RELEASE\n");
5416 case XCB_MOTION_NOTIFY:
5417 mne = (xcb_motion_notify_event_t *)evt;
5418 X(win) = mne->root_x - qpr->win_x - border_width;
5419 Y(win) = mne->root_y - qpr->win_y - border_width;
5421 constrain_window(win, r, 0);
5423 /* not free, don't sync more than 120 times / second */
5424 if ((mne->time - timestamp) > (1000 / 120) ) {
5425 timestamp = mne->time;
5440 store_float_geom(win, r);
5442 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
5443 DNPRINTF(SWM_D_EVENT, "move: done.\n");
5447 move_step(struct swm_region *r, union arg *args)
5449 struct ws_win *win = NULL;
5451 if (r && r->ws && r->ws->focus)
5456 if (!win->transient && !win->floating)
5463 /* key definitions */
5465 char name[SWM_FUNCNAME_LEN];
5466 void (*func)(struct swm_region *r, union arg *);
5468 } keyfuncs[KF_INVALID + 1] = {
5469 /* name function argument */
5470 { "bar_toggle", bar_toggle, {.id = SWM_ARG_ID_BAR_TOGGLE} },
5471 { "bar_toggle_ws", bar_toggle, {.id = SWM_ARG_ID_BAR_TOGGLE_WS} },
5472 { "button2", pressbutton, {2} },
5473 { "cycle_layout", cycle_layout, {0} },
5474 { "flip_layout", stack_config, {.id = SWM_ARG_ID_FLIPLAYOUT} },
5475 { "float_toggle", floating_toggle,{0} },
5476 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
5477 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
5478 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
5479 { "height_grow", resize_step, {.id = SWM_ARG_ID_HEIGHTGROW} },
5480 { "height_shrink", resize_step, {.id = SWM_ARG_ID_HEIGHTSHRINK} },
5481 { "iconify", iconify, {0} },
5482 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
5483 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
5484 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
5485 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
5486 { "move_down", move_step, {.id = SWM_ARG_ID_MOVEDOWN} },
5487 { "move_left", move_step, {.id = SWM_ARG_ID_MOVELEFT} },
5488 { "move_right", move_step, {.id = SWM_ARG_ID_MOVERIGHT} },
5489 { "move_up", move_step, {.id = SWM_ARG_ID_MOVEUP} },
5490 { "mvws_1", send_to_ws, {.id = 0} },
5491 { "mvws_2", send_to_ws, {.id = 1} },
5492 { "mvws_3", send_to_ws, {.id = 2} },
5493 { "mvws_4", send_to_ws, {.id = 3} },
5494 { "mvws_5", send_to_ws, {.id = 4} },
5495 { "mvws_6", send_to_ws, {.id = 5} },
5496 { "mvws_7", send_to_ws, {.id = 6} },
5497 { "mvws_8", send_to_ws, {.id = 7} },
5498 { "mvws_9", send_to_ws, {.id = 8} },
5499 { "mvws_10", send_to_ws, {.id = 9} },
5500 { "mvws_11", send_to_ws, {.id = 10} },
5501 { "mvws_12", send_to_ws, {.id = 11} },
5502 { "mvws_13", send_to_ws, {.id = 12} },
5503 { "mvws_14", send_to_ws, {.id = 13} },
5504 { "mvws_15", send_to_ws, {.id = 14} },
5505 { "mvws_16", send_to_ws, {.id = 15} },
5506 { "mvws_17", send_to_ws, {.id = 16} },
5507 { "mvws_18", send_to_ws, {.id = 17} },
5508 { "mvws_19", send_to_ws, {.id = 18} },
5509 { "mvws_20", send_to_ws, {.id = 19} },
5510 { "mvws_21", send_to_ws, {.id = 20} },
5511 { "mvws_22", send_to_ws, {.id = 21} },
5512 { "name_workspace", name_workspace, {0} },
5513 { "quit", quit, {0} },
5514 { "raise_toggle", raise_toggle, {0} },
5515 { "restart", restart, {0} },
5516 { "screen_next", cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
5517 { "screen_prev", cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
5518 { "search_win", search_win, {0} },
5519 { "search_workspace", search_workspace, {0} },
5520 { "spawn_custom", NULL, {0} },
5521 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
5522 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
5523 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
5524 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
5525 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
5526 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
5527 { "uniconify", uniconify, {0} },
5528 { "version", version, {0} },
5529 { "width_grow", resize_step, {.id = SWM_ARG_ID_WIDTHGROW} },
5530 { "width_shrink", resize_step, {.id = SWM_ARG_ID_WIDTHSHRINK} },
5531 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
5532 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
5533 { "ws_1", switchws, {.id = 0} },
5534 { "ws_2", switchws, {.id = 1} },
5535 { "ws_3", switchws, {.id = 2} },
5536 { "ws_4", switchws, {.id = 3} },
5537 { "ws_5", switchws, {.id = 4} },
5538 { "ws_6", switchws, {.id = 5} },
5539 { "ws_7", switchws, {.id = 6} },
5540 { "ws_8", switchws, {.id = 7} },
5541 { "ws_9", switchws, {.id = 8} },
5542 { "ws_10", switchws, {.id = 9} },
5543 { "ws_11", switchws, {.id = 10} },
5544 { "ws_12", switchws, {.id = 11} },
5545 { "ws_13", switchws, {.id = 12} },
5546 { "ws_14", switchws, {.id = 13} },
5547 { "ws_15", switchws, {.id = 14} },
5548 { "ws_16", switchws, {.id = 15} },
5549 { "ws_17", switchws, {.id = 16} },
5550 { "ws_18", switchws, {.id = 17} },
5551 { "ws_19", switchws, {.id = 18} },
5552 { "ws_20", switchws, {.id = 19} },
5553 { "ws_21", switchws, {.id = 20} },
5554 { "ws_22", switchws, {.id = 21} },
5555 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
5556 { "ws_next_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP_ALL} },
5557 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
5558 { "ws_prev_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN_ALL} },
5559 { "ws_prior", priorws, {0} },
5560 { "dumpwins", dumpwins, {0} }, /* MUST BE LAST */
5561 { "invalid key func", NULL, {0} },
5565 key_cmp(struct key *kp1, struct key *kp2)
5567 if (kp1->keysym < kp2->keysym)
5569 if (kp1->keysym > kp2->keysym)
5572 if (kp1->mod < kp2->mod)
5574 if (kp1->mod > kp2->mod)
5581 enum { client_click, root_click };
5583 unsigned int action;
5585 unsigned int button;
5586 void (*func)(struct ws_win *, union arg *);
5589 #define MODKEY_SHIFT MODKEY | XCB_MOD_MASK_SHIFT
5590 /* action key mouse button func args */
5591 { client_click, MODKEY, XCB_BUTTON_INDEX_3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
5592 { client_click, MODKEY_SHIFT, XCB_BUTTON_INDEX_3, resize, {.id = SWM_ARG_ID_CENTER} },
5593 { client_click, MODKEY, XCB_BUTTON_INDEX_1, move, {0} },
5598 update_modkey(unsigned int mod)
5604 RB_FOREACH(kp, key_tree, &keys)
5605 if (kp->mod & XCB_MOD_MASK_SHIFT)
5606 kp->mod = mod | XCB_MOD_MASK_SHIFT;
5610 for (i = 0; i < LENGTH(buttons); i++)
5611 if (buttons[i].mask & XCB_MOD_MASK_SHIFT)
5612 buttons[i].mask = mod | XCB_MOD_MASK_SHIFT;
5614 buttons[i].mask = mod;
5618 spawn_expand(struct swm_region *r, union arg *args, const char *spawn_name,
5621 struct spawn_prog *prog = NULL;
5623 char *ap, **real_args;
5625 /* suppress unused warning since var is needed */
5628 DNPRINTF(SWM_D_SPAWN, "spawn_expand: %s\n", spawn_name);
5631 TAILQ_FOREACH(prog, &spawns, entry) {
5632 if (!strcasecmp(spawn_name, prog->name))
5636 warnx("spawn_custom: program %s not found", spawn_name);
5640 /* make room for expanded args */
5641 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
5642 err(1, "spawn_custom: calloc real_args");
5644 /* expand spawn_args into real_args */
5645 for (i = 0; i < prog->argc; i++) {
5647 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg: %s\n", ap);
5648 if (!strcasecmp(ap, "$bar_border")) {
5650 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
5652 err(1, "spawn_custom border color");
5653 } else if (!strcasecmp(ap, "$bar_color")) {
5655 strdup(r->s->c[SWM_S_COLOR_BAR].name))
5657 err(1, "spawn_custom bar color");
5658 } else if (!strcasecmp(ap, "$bar_font")) {
5659 if ((real_args[i] = strdup(bar_fonts))
5661 err(1, "spawn_custom bar fonts");
5662 } else if (!strcasecmp(ap, "$bar_font_color")) {
5664 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
5666 err(1, "spawn_custom color font");
5667 } else if (!strcasecmp(ap, "$color_focus")) {
5669 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
5671 err(1, "spawn_custom color focus");
5672 } else if (!strcasecmp(ap, "$color_unfocus")) {
5674 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
5676 err(1, "spawn_custom color unfocus");
5678 /* no match --> copy as is */
5679 if ((real_args[i] = strdup(ap)) == NULL)
5680 err(1, "spawn_custom strdup(ap)");
5682 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg: %s\n",
5687 DNPRINTF(SWM_D_SPAWN, "spawn_custom: result: ");
5688 for (i = 0; i < prog->argc; i++)
5689 DNPRINTF(SWM_D_SPAWN, "\"%s\" ", real_args[i]);
5690 DNPRINTF(SWM_D_SPAWN, "\n");
5692 *ret_args = real_args;
5693 return (prog->argc);
5697 spawn_custom(struct swm_region *r, union arg *args, const char *spawn_name)
5703 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
5707 spawn(r->ws->idx, &a, 1);
5709 for (i = 0; i < spawn_argc; i++)
5715 spawn_select(struct swm_region *r, union arg *args, const char *spawn_name,
5722 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
5726 if (pipe(select_list_pipe) == -1)
5727 err(1, "pipe error");
5728 if (pipe(select_resp_pipe) == -1)
5729 err(1, "pipe error");
5731 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
5732 err(1, "could not disable SIGPIPE");
5733 switch (*pid = fork()) {
5735 err(1, "cannot fork");
5738 if (dup2(select_list_pipe[0], 0) == -1)
5740 if (dup2(select_resp_pipe[1], 1) == -1)
5742 close(select_list_pipe[1]);
5743 close(select_resp_pipe[0]);
5744 spawn(r->ws->idx, &a, 0);
5746 default: /* parent */
5747 close(select_list_pipe[0]);
5748 close(select_resp_pipe[1]);
5752 for (i = 0; i < spawn_argc; i++)
5758 spawn_insert(const char *name, const char *args)
5760 char *arg, *cp, *ptr;
5761 struct spawn_prog *sp;
5763 DNPRINTF(SWM_D_SPAWN, "spawn_insert: %s\n", name);
5765 if ((sp = calloc(1, sizeof *sp)) == NULL)
5766 err(1, "spawn_insert: calloc");
5767 if ((sp->name = strdup(name)) == NULL)
5768 err(1, "spawn_insert: strdup");
5770 /* convert the arguments to an argument list */
5771 if ((ptr = cp = strdup(args)) == NULL)
5772 err(1, "spawn_insert: strdup");
5773 while ((arg = strsep(&ptr, " \t")) != NULL) {
5774 /* empty field; skip it */
5779 if ((sp->argv = realloc(sp->argv, sp->argc *
5780 sizeof *sp->argv)) == NULL)
5781 err(1, "spawn_insert: realloc");
5782 if ((sp->argv[sp->argc - 1] = strdup(arg)) == NULL)
5783 err(1, "spawn_insert: strdup");
5787 TAILQ_INSERT_TAIL(&spawns, sp, entry);
5788 DNPRINTF(SWM_D_SPAWN, "spawn_insert: leave\n");
5792 spawn_remove(struct spawn_prog *sp)
5796 DNPRINTF(SWM_D_SPAWN, "spawn_remove: %s\n", sp->name);
5798 TAILQ_REMOVE(&spawns, sp, entry);
5799 for (i = 0; i < sp->argc; i++)
5805 DNPRINTF(SWM_D_SPAWN, "spawn_remove: leave\n");
5809 spawn_replace(struct spawn_prog *sp, const char *name, const char *args)
5811 DNPRINTF(SWM_D_SPAWN, "spawn_replace: %s [%s]\n", sp->name, name);
5814 spawn_insert(name, args);
5816 DNPRINTF(SWM_D_SPAWN, "spawn_replace: leave\n");
5820 setspawn(const char *name, const char *args)
5822 struct spawn_prog *sp;
5824 DNPRINTF(SWM_D_SPAWN, "setspawn: %s\n", name);
5829 TAILQ_FOREACH(sp, &spawns, entry) {
5830 if (!strcmp(sp->name, name)) {
5834 spawn_replace(sp, name, args);
5835 DNPRINTF(SWM_D_SPAWN, "setspawn: leave\n");
5839 if (*args == '\0') {
5840 warnx("error: setspawn: cannot find program: %s", name);
5844 spawn_insert(name, args);
5845 DNPRINTF(SWM_D_SPAWN, "setspawn: leave\n");
5849 setconfspawn(char *selector, char *value, int flags)
5853 /* suppress unused warning since var is needed */
5856 args = expand_tilde(value);
5858 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, args);
5860 setspawn(selector, args);
5863 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done.\n");
5870 setconfspawn("term", "xterm", 0);
5871 setconfspawn("spawn_term", "xterm", 0);
5872 setconfspawn("screenshot_all", "screenshot.sh full", 0);
5873 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
5874 setconfspawn("lock", "xlock", 0);
5875 setconfspawn("initscr", "initscreen.sh", 0);
5876 setconfspawn("menu", "dmenu_run"
5879 " -nf $bar_font_color"
5881 " -sf $bar_color", 0);
5882 setconfspawn("search", "dmenu"
5886 " -nf $bar_font_color"
5888 " -sf $bar_color", 0);
5889 setconfspawn("name_workspace", "dmenu"
5893 " -nf $bar_font_color"
5895 " -sf $bar_color", 0);
5899 #define SWM_MODNAME_SIZE 32
5900 #define SWM_KEY_WS "\n+ \t"
5902 parsekeys(char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
5906 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
5907 if (mod == NULL || ks == NULL) {
5908 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
5911 if (keystr == NULL || strlen(keystr) == 0) {
5912 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
5916 *ks = XCB_NO_SYMBOL;
5918 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
5919 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
5921 cp += (long)strspn(cp, SWM_KEY_WS);
5922 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
5924 else if (!strncasecmp(name, "Mod1", SWM_MODNAME_SIZE))
5925 *mod |= XCB_MOD_MASK_1;
5926 else if (!strncasecmp(name, "Mod2", SWM_MODNAME_SIZE))
5927 *mod += XCB_MOD_MASK_2;
5928 else if (!strncmp(name, "Mod3", SWM_MODNAME_SIZE))
5929 *mod |= XCB_MOD_MASK_3;
5930 else if (!strncmp(name, "Mod4", SWM_MODNAME_SIZE))
5931 *mod |= XCB_MOD_MASK_4;
5932 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
5933 *mod |= XCB_MOD_MASK_SHIFT;
5934 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
5935 *mod |= XCB_MOD_MASK_CONTROL;
5937 *ks = XStringToKeysym(name);
5938 XConvertCase(*ks, ks, &uks);
5939 if (ks == XCB_NO_SYMBOL) {
5941 "parsekeys: invalid key %s\n",
5947 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
5952 strdupsafe(const char *str)
5957 return (strdup(str));
5961 key_insert(unsigned int mod, KeySym ks, enum keyfuncid kfid,
5962 const char *spawn_name)
5966 DNPRINTF(SWM_D_KEY, "key_insert: enter %s [%s]\n",
5967 keyfuncs[kfid].name, spawn_name);
5969 if ((kp = malloc(sizeof *kp)) == NULL)
5970 err(1, "key_insert: malloc");
5975 kp->spawn_name = strdupsafe(spawn_name);
5976 RB_INSERT(key_tree, &keys, kp);
5978 DNPRINTF(SWM_D_KEY, "key_insert: leave\n");
5982 key_lookup(unsigned int mod, KeySym ks)
5989 return (RB_FIND(key_tree, &keys, &kp));
5993 key_remove(struct key *kp)
5995 DNPRINTF(SWM_D_KEY, "key_remove: %s\n", keyfuncs[kp->funcid].name);
5997 RB_REMOVE(key_tree, &keys, kp);
5998 free(kp->spawn_name);
6001 DNPRINTF(SWM_D_KEY, "key_remove: leave\n");
6005 key_replace(struct key *kp, unsigned int mod, KeySym ks, enum keyfuncid kfid,
6006 const char *spawn_name)
6008 DNPRINTF(SWM_D_KEY, "key_replace: %s [%s]\n", keyfuncs[kp->funcid].name,
6012 key_insert(mod, ks, kfid, spawn_name);
6014 DNPRINTF(SWM_D_KEY, "key_replace: leave\n");
6018 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid,
6019 const char *spawn_name)
6023 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
6024 keyfuncs[kfid].name, spawn_name);
6026 if ((kp = key_lookup(mod, ks)) != NULL) {
6027 if (kfid == KF_INVALID)
6030 key_replace(kp, mod, ks, kfid, spawn_name);
6031 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
6034 if (kfid == KF_INVALID) {
6035 warnx("bind: Key combination already unbound.");
6036 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
6040 key_insert(mod, ks, kfid, spawn_name);
6041 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
6045 setconfbinding(char *selector, char *value, int flags)
6047 enum keyfuncid kfid;
6050 struct spawn_prog *sp;
6052 /* suppress unused warning since var is needed */
6055 DNPRINTF(SWM_D_KEY, "setconfbinding: enter\n");
6056 if (selector == NULL) {
6057 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
6058 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
6060 setkeybinding(mod, ks, kfid, NULL);
6065 /* search by key function name */
6066 for (kfid = 0; kfid < KF_INVALID; (kfid)++) {
6067 if (strncasecmp(selector, keyfuncs[kfid].name,
6068 SWM_FUNCNAME_LEN) == 0) {
6069 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
6071 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
6072 setkeybinding(mod, ks, kfid, NULL);
6078 /* search by custom spawn name */
6079 TAILQ_FOREACH(sp, &spawns, entry) {
6080 if (strcasecmp(selector, sp->name) == 0) {
6081 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
6083 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
6084 setkeybinding(mod, ks, KF_SPAWN_CUSTOM,
6091 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
6098 #define MODKEY_SHIFT MODKEY | XCB_MOD_MASK_SHIFT
6099 setkeybinding(MODKEY, XK_space, KF_CYCLE_LAYOUT,NULL);
6100 setkeybinding(MODKEY_SHIFT, XK_backslash, KF_FLIP_LAYOUT, NULL);
6101 setkeybinding(MODKEY_SHIFT, XK_space, KF_STACK_RESET, NULL);
6102 setkeybinding(MODKEY, XK_h, KF_MASTER_SHRINK, NULL);
6103 setkeybinding(MODKEY, XK_l, KF_MASTER_GROW, NULL);
6104 setkeybinding(MODKEY, XK_comma, KF_MASTER_ADD, NULL);
6105 setkeybinding(MODKEY, XK_period, KF_MASTER_DEL, NULL);
6106 setkeybinding(MODKEY_SHIFT, XK_comma, KF_STACK_INC, NULL);
6107 setkeybinding(MODKEY_SHIFT, XK_period, KF_STACK_DEC, NULL);
6108 setkeybinding(MODKEY, XK_Return, KF_SWAP_MAIN, NULL);
6109 setkeybinding(MODKEY, XK_j, KF_FOCUS_NEXT, NULL);
6110 setkeybinding(MODKEY, XK_k, KF_FOCUS_PREV, NULL);
6111 setkeybinding(MODKEY_SHIFT, XK_j, KF_SWAP_NEXT, NULL);
6112 setkeybinding(MODKEY_SHIFT, XK_k, KF_SWAP_PREV, NULL);
6113 setkeybinding(MODKEY_SHIFT, XK_Return, KF_SPAWN_CUSTOM,"term");
6114 setkeybinding(MODKEY, XK_p, KF_SPAWN_CUSTOM,"menu");
6115 setkeybinding(MODKEY_SHIFT, XK_q, KF_QUIT, NULL);
6116 setkeybinding(MODKEY, XK_q, KF_RESTART, NULL);
6117 setkeybinding(MODKEY, XK_m, KF_FOCUS_MAIN, NULL);
6118 setkeybinding(MODKEY, XK_1, KF_WS_1, NULL);
6119 setkeybinding(MODKEY, XK_2, KF_WS_2, NULL);
6120 setkeybinding(MODKEY, XK_3, KF_WS_3, NULL);
6121 setkeybinding(MODKEY, XK_4, KF_WS_4, NULL);
6122 setkeybinding(MODKEY, XK_5, KF_WS_5, NULL);
6123 setkeybinding(MODKEY, XK_6, KF_WS_6, NULL);
6124 setkeybinding(MODKEY, XK_7, KF_WS_7, NULL);
6125 setkeybinding(MODKEY, XK_8, KF_WS_8, NULL);
6126 setkeybinding(MODKEY, XK_9, KF_WS_9, NULL);
6127 setkeybinding(MODKEY, XK_0, KF_WS_10, NULL);
6128 setkeybinding(MODKEY, XK_F1, KF_WS_11, NULL);
6129 setkeybinding(MODKEY, XK_F2, KF_WS_12, NULL);
6130 setkeybinding(MODKEY, XK_F3, KF_WS_13, NULL);
6131 setkeybinding(MODKEY, XK_F4, KF_WS_14, NULL);
6132 setkeybinding(MODKEY, XK_F5, KF_WS_15, NULL);
6133 setkeybinding(MODKEY, XK_F6, KF_WS_16, NULL);
6134 setkeybinding(MODKEY, XK_F7, KF_WS_17, NULL);
6135 setkeybinding(MODKEY, XK_F8, KF_WS_18, NULL);
6136 setkeybinding(MODKEY, XK_F9, KF_WS_19, NULL);
6137 setkeybinding(MODKEY, XK_F10, KF_WS_20, NULL);
6138 setkeybinding(MODKEY, XK_F11, KF_WS_21, NULL);
6139 setkeybinding(MODKEY, XK_F12, KF_WS_22, NULL);
6140 setkeybinding(MODKEY, XK_Right, KF_WS_NEXT, NULL);
6141 setkeybinding(MODKEY, XK_Left, KF_WS_PREV, NULL);
6142 setkeybinding(MODKEY, XK_Up, KF_WS_NEXT_ALL, NULL);
6143 setkeybinding(MODKEY, XK_Down, KF_WS_PREV_ALL, NULL);
6144 setkeybinding(MODKEY, XK_a, KF_WS_PRIOR, NULL);
6145 setkeybinding(MODKEY_SHIFT, XK_Right, KF_SCREEN_NEXT, NULL);
6146 setkeybinding(MODKEY_SHIFT, XK_Left, KF_SCREEN_PREV, NULL);
6147 setkeybinding(MODKEY_SHIFT, XK_1, KF_MVWS_1, NULL);
6148 setkeybinding(MODKEY_SHIFT, XK_2, KF_MVWS_2, NULL);
6149 setkeybinding(MODKEY_SHIFT, XK_3, KF_MVWS_3, NULL);
6150 setkeybinding(MODKEY_SHIFT, XK_4, KF_MVWS_4, NULL);
6151 setkeybinding(MODKEY_SHIFT, XK_5, KF_MVWS_5, NULL);
6152 setkeybinding(MODKEY_SHIFT, XK_6, KF_MVWS_6, NULL);
6153 setkeybinding(MODKEY_SHIFT, XK_7, KF_MVWS_7, NULL);
6154 setkeybinding(MODKEY_SHIFT, XK_8, KF_MVWS_8, NULL);
6155 setkeybinding(MODKEY_SHIFT, XK_9, KF_MVWS_9, NULL);
6156 setkeybinding(MODKEY_SHIFT, XK_0, KF_MVWS_10, NULL);
6157 setkeybinding(MODKEY_SHIFT, XK_F1, KF_MVWS_11, NULL);
6158 setkeybinding(MODKEY_SHIFT, XK_F2, KF_MVWS_12, NULL);
6159 setkeybinding(MODKEY_SHIFT, XK_F3, KF_MVWS_13, NULL);
6160 setkeybinding(MODKEY_SHIFT, XK_F4, KF_MVWS_14, NULL);
6161 setkeybinding(MODKEY_SHIFT, XK_F5, KF_MVWS_15, NULL);
6162 setkeybinding(MODKEY_SHIFT, XK_F6, KF_MVWS_16, NULL);
6163 setkeybinding(MODKEY_SHIFT, XK_F7, KF_MVWS_17, NULL);
6164 setkeybinding(MODKEY_SHIFT, XK_F8, KF_MVWS_18, NULL);
6165 setkeybinding(MODKEY_SHIFT, XK_F9, KF_MVWS_19, NULL);
6166 setkeybinding(MODKEY_SHIFT, XK_F10, KF_MVWS_20, NULL);
6167 setkeybinding(MODKEY_SHIFT, XK_F11, KF_MVWS_21, NULL);
6168 setkeybinding(MODKEY_SHIFT, XK_F12, KF_MVWS_22, NULL);
6169 setkeybinding(MODKEY, XK_b, KF_BAR_TOGGLE, NULL);
6170 setkeybinding(MODKEY_SHIFT, XK_b, KF_BAR_TOGGLE_WS,NULL);
6171 setkeybinding(MODKEY, XK_Tab, KF_FOCUS_NEXT, NULL);
6172 setkeybinding(MODKEY_SHIFT, XK_Tab, KF_FOCUS_PREV, NULL);
6173 setkeybinding(MODKEY_SHIFT, XK_x, KF_WIND_KILL, NULL);
6174 setkeybinding(MODKEY, XK_x, KF_WIND_DEL, NULL);
6175 setkeybinding(MODKEY, XK_s, KF_SPAWN_CUSTOM,"screenshot_all");
6176 setkeybinding(MODKEY_SHIFT, XK_s, KF_SPAWN_CUSTOM,"screenshot_wind");
6177 setkeybinding(MODKEY, XK_t, KF_FLOAT_TOGGLE,NULL);
6178 setkeybinding(MODKEY_SHIFT, XK_v, KF_VERSION, NULL);
6179 setkeybinding(MODKEY_SHIFT, XK_Delete, KF_SPAWN_CUSTOM,"lock");
6180 setkeybinding(MODKEY_SHIFT, XK_i, KF_SPAWN_CUSTOM,"initscr");
6181 setkeybinding(MODKEY, XK_w, KF_ICONIFY, NULL);
6182 setkeybinding(MODKEY_SHIFT, XK_w, KF_UNICONIFY, NULL);
6183 setkeybinding(MODKEY_SHIFT, XK_r, KF_RAISE_TOGGLE,NULL);
6184 setkeybinding(MODKEY, XK_v, KF_BUTTON2, NULL);
6185 setkeybinding(MODKEY, XK_equal, KF_WIDTH_GROW, NULL);
6186 setkeybinding(MODKEY, XK_minus, KF_WIDTH_SHRINK,NULL);
6187 setkeybinding(MODKEY_SHIFT, XK_equal, KF_HEIGHT_GROW,NULL);
6188 setkeybinding(MODKEY_SHIFT, XK_minus, KF_HEIGHT_SHRINK,NULL);
6189 setkeybinding(MODKEY, XK_bracketleft, KF_MOVE_LEFT,NULL);
6190 setkeybinding(MODKEY, XK_bracketright,KF_MOVE_RIGHT,NULL);
6191 setkeybinding(MODKEY_SHIFT, XK_bracketleft, KF_MOVE_UP, NULL);
6192 setkeybinding(MODKEY_SHIFT, XK_bracketright,KF_MOVE_DOWN,NULL);
6193 setkeybinding(MODKEY_SHIFT, XK_slash, KF_NAME_WORKSPACE,NULL);
6194 setkeybinding(MODKEY, XK_slash, KF_SEARCH_WORKSPACE,NULL);
6195 setkeybinding(MODKEY, XK_f, KF_SEARCH_WIN, NULL);
6197 setkeybinding(MODKEY_SHIFT, XK_d, KF_DUMPWINS, NULL);
6207 while (RB_EMPTY(&keys) == 0) {
6208 kp = RB_ROOT(&keys);
6214 setkeymapping(char *selector, char *value, int flags)
6216 char keymapping_file[PATH_MAX];
6218 /* suppress unused warnings since vars are needed */
6222 DNPRINTF(SWM_D_KEY, "setkeymapping: enter\n");
6223 if (value[0] == '~')
6224 snprintf(keymapping_file, sizeof keymapping_file, "%s/%s",
6225 pwd->pw_dir, &value[1]);
6227 strlcpy(keymapping_file, value, sizeof keymapping_file);
6229 /* load new key bindings; if it fails, revert to default bindings */
6230 if (conf_load(keymapping_file, SWM_CONF_KEYMAPPING)) {
6234 DNPRINTF(SWM_D_KEY, "setkeymapping: leave\n");
6239 updatenumlockmask(void)
6242 xcb_get_modifier_mapping_reply_t *modmap_r;
6243 xcb_keycode_t *modmap, kc, *keycode;
6247 modmap_r = xcb_get_modifier_mapping_reply(conn,
6248 xcb_get_modifier_mapping(conn),
6251 modmap = xcb_get_modifier_mapping_keycodes(modmap_r);
6252 for (i = 0; i < 8; i++) {
6253 for (j = 0; j < modmap_r->keycodes_per_modifier; j++) {
6254 kc = modmap[i * modmap_r->keycodes_per_modifier
6256 keycode = xcb_key_symbols_get_keycode(syms,
6259 numlockmask = (1 << i);
6265 DNPRINTF(SWM_D_MISC, "updatenumlockmask: %d\n", numlockmask);
6272 int num_screens, k, j;
6273 unsigned int modifiers[4];
6274 xcb_keycode_t *code;
6276 DNPRINTF(SWM_D_MISC, "grabkeys\n");
6277 updatenumlockmask();
6280 modifiers[1] = numlockmask;
6281 modifiers[2] = XCB_MOD_MASK_LOCK;
6282 modifiers[3] = numlockmask | XCB_MOD_MASK_LOCK;
6284 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
6285 for (k = 0; k < num_screens; k++) {
6286 if (TAILQ_EMPTY(&screens[k].rl))
6288 xcb_ungrab_key(conn, XCB_GRAB_ANY, screens[k].root,
6290 RB_FOREACH(kp, key_tree, &keys) {
6291 if ((code = xcb_key_symbols_get_keycode(syms,
6293 for (j = 0; j < LENGTH(modifiers); j++)
6294 xcb_grab_key(conn, 1,
6296 kp->mod | modifiers[j],
6297 *code, XCB_GRAB_MODE_SYNC,
6298 XCB_GRAB_MODE_ASYNC);
6306 grabbuttons(struct ws_win *win)
6308 unsigned int modifiers[4];
6311 DNPRINTF(SWM_D_MOUSE, "grabbuttons: win 0x%x\n", win->id);
6312 updatenumlockmask();
6315 modifiers[1] = numlockmask;
6316 modifiers[2] = XCB_MOD_MASK_LOCK;
6317 modifiers[3] = numlockmask | XCB_MOD_MASK_LOCK;
6319 for (i = 0; i < LENGTH(buttons); i++)
6320 if (buttons[i].action == client_click)
6321 for (j = 0; j < LENGTH(modifiers); ++j)
6322 xcb_grab_button(conn, 0, win->id, BUTTONMASK,
6323 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC,
6324 XCB_WINDOW_NONE, XCB_CURSOR_NONE,
6325 buttons[i].button, buttons[i].mask |
6329 const char *quirkname[] = {
6330 "NONE", /* config string for "no value" */
6339 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
6340 #define SWM_Q_WS "\n|+ \t"
6342 parsequirks(char *qstr, unsigned long *quirk)
6352 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
6354 cp += (long)strspn(cp, SWM_Q_WS);
6355 for (i = 0; i < LENGTH(quirkname); i++) {
6356 if (!strncasecmp(name, quirkname[i], SWM_QUIRK_LEN)) {
6357 DNPRINTF(SWM_D_QUIRK,
6358 "parsequirks: %s\n", name);
6363 *quirk |= 1 << (i-1);
6367 if (i >= LENGTH(quirkname)) {
6368 DNPRINTF(SWM_D_QUIRK,
6369 "parsequirks: invalid quirk [%s]\n", name);
6377 quirk_insert(const char *class, const char *name, unsigned long quirk)
6381 DNPRINTF(SWM_D_QUIRK, "quirk_insert: %s:%s [%lu]\n", class, name,
6384 if ((qp = malloc(sizeof *qp)) == NULL)
6385 err(1, "quirk_insert: malloc");
6386 if ((qp->class = strdup(class)) == NULL)
6387 err(1, "quirk_insert: strdup");
6388 if ((qp->name = strdup(name)) == NULL)
6389 err(1, "quirk_insert: strdup");
6392 TAILQ_INSERT_TAIL(&quirks, qp, entry);
6394 DNPRINTF(SWM_D_QUIRK, "quirk_insert: leave\n");
6398 quirk_remove(struct quirk *qp)
6400 DNPRINTF(SWM_D_QUIRK, "quirk_remove: %s:%s [%lu]\n", qp->class,
6401 qp->name, qp->quirk);
6403 TAILQ_REMOVE(&quirks, qp, entry);
6408 DNPRINTF(SWM_D_QUIRK, "quirk_remove: leave\n");
6412 quirk_replace(struct quirk *qp, const char *class, const char *name,
6413 unsigned long quirk)
6415 DNPRINTF(SWM_D_QUIRK, "quirk_replace: %s:%s [%lu]\n", qp->class,
6416 qp->name, qp->quirk);
6419 quirk_insert(class, name, quirk);
6421 DNPRINTF(SWM_D_QUIRK, "quirk_replace: leave\n");
6425 setquirk(const char *class, const char *name, unsigned long quirk)
6429 DNPRINTF(SWM_D_QUIRK, "setquirk: enter %s:%s [%lu]\n", class, name,
6432 /* Remove/replace existing quirk. */
6433 TAILQ_FOREACH(qp, &quirks, entry) {
6434 if (!strcmp(qp->class, class) && !strcmp(qp->name, name)) {
6438 quirk_replace(qp, class, name, quirk);
6439 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
6444 /* Only insert if quirk is not NONE. */
6446 quirk_insert(class, name, quirk);
6448 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
6452 setconfquirk(char *selector, char *value, int flags)
6454 char *cp, *class, *name;
6458 /* suppress unused warning since var is needed */
6461 if (selector == NULL)
6463 if ((cp = strchr(selector, ':')) == NULL)
6468 if ((retval = parsequirks(value, &qrks)) == 0)
6469 setquirk(class, name, qrks);
6476 setquirk("MPlayer", "xv", SWM_Q_FLOAT | SWM_Q_FULLSCREEN | SWM_Q_FOCUSPREV);
6477 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT);
6478 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ);
6479 setquirk("Firefox", "Dialog", SWM_Q_FLOAT);
6480 setquirk("Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
6481 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ);
6482 setquirk("xine", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
6483 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
6484 setquirk("xine", "xine Panel", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
6485 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
6486 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
6487 setquirk("pcb", "pcb", SWM_Q_FLOAT);
6488 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
6491 /* conf file stuff */
6492 #define SWM_CONF_FILE "spectrwm.conf"
6493 #define SWM_CONF_FILE_OLD "scrotwm.conf"
6497 SWM_S_BAR_AT_BOTTOM,
6498 SWM_S_BAR_BORDER_WIDTH,
6501 SWM_S_BAR_ENABLED_WS,
6506 SWM_S_CLOCK_ENABLED,
6509 SWM_S_CYCLE_VISIBLE,
6511 SWM_S_DISABLE_BORDER,
6513 SWM_S_FOCUS_CLOSE_WRAP,
6514 SWM_S_FOCUS_DEFAULT,
6520 SWM_S_STACK_ENABLED,
6522 SWM_S_TITLE_CLASS_ENABLED,
6523 SWM_S_TITLE_NAME_ENABLED,
6524 SWM_S_URGENT_ENABLED,
6525 SWM_S_VERBOSE_LAYOUT,
6526 SWM_S_WINDOW_NAME_ENABLED,
6527 SWM_S_WORKSPACE_LIMIT
6531 setconfvalue(char *selector, char *value, int flags)
6533 struct workspace *ws;
6534 int i, ws_id, num_screens;
6537 /* suppress unused warning since var is needed */
6541 case SWM_S_BAR_ACTION:
6543 if ((bar_argv[0] = expand_tilde(value)) == NULL)
6544 err(1, "setconfvalue: bar_action");
6546 case SWM_S_BAR_AT_BOTTOM:
6547 bar_at_bottom = atoi(value);
6549 case SWM_S_BAR_BORDER_WIDTH:
6550 bar_border_width = atoi(value);
6551 if (bar_border_width < 0)
6552 bar_border_width = 0;
6554 case SWM_S_BAR_DELAY:
6555 /* No longer needed; leave to not break old conf files. */
6557 case SWM_S_BAR_ENABLED:
6558 bar_enabled = atoi(value);
6560 case SWM_S_BAR_ENABLED_WS:
6561 ws_id = atoi(selector) - 1;
6562 if (ws_id < 0 || ws_id >= workspace_limit)
6563 errx(1, "setconfvalue: bar_enabled_ws: invalid "
6564 "workspace %d.", ws_id + 1);
6566 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
6567 for (i = 0; i < num_screens; i++) {
6568 ws = (struct workspace *)&screens[i].ws;
6569 ws[ws_id].bar_enabled = atoi(value);
6572 case SWM_S_BAR_FONT:
6574 if (asprintf(&bar_fonts, "%s,%s", value, bar_fonts) == -1)
6575 err(1, "setconfvalue: asprintf: failed to allocate "
6576 "memory for bar_fonts.");
6579 /* If already in xft mode, then we are done. */
6580 if (!bar_font_legacy)
6583 /* If there are any non-XLFD entries, switch to Xft mode. */
6584 while ((b = strsep(&value, ",")) != NULL) {
6588 bar_font_legacy = 0;
6593 case SWM_S_BAR_FORMAT:
6595 if ((bar_format = strdup(value)) == NULL)
6596 err(1, "setconfvalue: bar_format");
6598 case SWM_S_BAR_JUSTIFY:
6599 if (!strcmp(value, "left"))
6600 bar_justify = SWM_BAR_JUSTIFY_LEFT;
6601 else if (!strcmp(value, "center"))
6602 bar_justify = SWM_BAR_JUSTIFY_CENTER;
6603 else if (!strcmp(value, "right"))
6604 bar_justify = SWM_BAR_JUSTIFY_RIGHT;
6606 errx(1, "invalid bar_justify");
6608 case SWM_S_BORDER_WIDTH:
6609 border_width = atoi(value);
6610 if (border_width < 0)
6613 case SWM_S_CLOCK_ENABLED:
6614 clock_enabled = atoi(value);
6616 case SWM_S_CLOCK_FORMAT:
6617 #ifndef SWM_DENY_CLOCK_FORMAT
6619 if ((clock_format = strdup(value)) == NULL)
6620 err(1, "setconfvalue: clock_format");
6623 case SWM_S_CYCLE_EMPTY:
6624 cycle_empty = atoi(value);
6626 case SWM_S_CYCLE_VISIBLE:
6627 cycle_visible = atoi(value);
6629 case SWM_S_DIALOG_RATIO:
6630 dialog_ratio = atof(value);
6631 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
6634 case SWM_S_DISABLE_BORDER:
6635 disable_border = atoi(value);
6637 case SWM_S_FOCUS_CLOSE:
6638 if (!strcmp(value, "first"))
6639 focus_close = SWM_STACK_BOTTOM;
6640 else if (!strcmp(value, "last"))
6641 focus_close = SWM_STACK_TOP;
6642 else if (!strcmp(value, "next"))
6643 focus_close = SWM_STACK_ABOVE;
6644 else if (!strcmp(value, "previous"))
6645 focus_close = SWM_STACK_BELOW;
6647 errx(1, "focus_close");
6649 case SWM_S_FOCUS_CLOSE_WRAP:
6650 focus_close_wrap = atoi(value);
6652 case SWM_S_FOCUS_DEFAULT:
6653 if (!strcmp(value, "last"))
6654 focus_default = SWM_STACK_TOP;
6655 else if (!strcmp(value, "first"))
6656 focus_default = SWM_STACK_BOTTOM;
6658 errx(1, "focus_default");
6660 case SWM_S_FOCUS_MODE:
6661 if (!strcmp(value, "default"))
6662 focus_mode = SWM_FOCUS_DEFAULT;
6663 else if (!strcmp(value, "follow") ||
6664 !strcmp(value, "follow_cursor"))
6665 focus_mode = SWM_FOCUS_FOLLOW;
6666 else if (!strcmp(value, "manual"))
6667 focus_mode = SWM_FOCUS_MANUAL;
6669 errx(1, "focus_mode");
6671 case SWM_S_SPAWN_ORDER:
6672 if (!strcmp(value, "first"))
6673 spawn_position = SWM_STACK_BOTTOM;
6674 else if (!strcmp(value, "last"))
6675 spawn_position = SWM_STACK_TOP;
6676 else if (!strcmp(value, "next"))
6677 spawn_position = SWM_STACK_ABOVE;
6678 else if (!strcmp(value, "previous"))
6679 spawn_position = SWM_STACK_BELOW;
6681 errx(1, "spawn_position");
6683 case SWM_S_SPAWN_TERM:
6684 setconfspawn("term", value, 0);
6685 setconfspawn("spawn_term", value, 0);
6688 /* No longer needed; leave to not break old conf files. */
6690 case SWM_S_SS_ENABLED:
6691 /* No longer needed; leave to not break old conf files. */
6693 case SWM_S_STACK_ENABLED:
6694 stack_enabled = atoi(value);
6696 case SWM_S_TERM_WIDTH:
6697 term_width = atoi(value);
6701 case SWM_S_TITLE_CLASS_ENABLED:
6702 title_class_enabled = atoi(value);
6704 case SWM_S_TITLE_NAME_ENABLED:
6705 title_name_enabled = atoi(value);
6707 case SWM_S_URGENT_ENABLED:
6708 urgent_enabled = atoi(value);
6710 case SWM_S_VERBOSE_LAYOUT:
6711 verbose_layout = atoi(value);
6712 for (i = 0; layouts[i].l_stack != NULL; i++) {
6714 layouts[i].l_string = fancy_stacker;
6716 layouts[i].l_string = plain_stacker;
6719 case SWM_S_WINDOW_NAME_ENABLED:
6720 window_name_enabled = atoi(value);
6722 case SWM_S_WORKSPACE_LIMIT:
6723 workspace_limit = atoi(value);
6724 if (workspace_limit > SWM_WS_MAX)
6725 workspace_limit = SWM_WS_MAX;
6726 else if (workspace_limit < 1)
6727 workspace_limit = 1;
6736 setconfmodkey(char *selector, char *value, int flags)
6738 /* suppress unused warnings since vars are needed */
6742 if (!strncasecmp(value, "Mod1", strlen("Mod1")))
6743 update_modkey(XCB_MOD_MASK_1);
6744 else if (!strncasecmp(value, "Mod2", strlen("Mod2")))
6745 update_modkey(XCB_MOD_MASK_2);
6746 else if (!strncasecmp(value, "Mod3", strlen("Mod3")))
6747 update_modkey(XCB_MOD_MASK_3);
6748 else if (!strncasecmp(value, "Mod4", strlen("Mod4")))
6749 update_modkey(XCB_MOD_MASK_4);
6756 setconfcolor(char *selector, char *value, int flags)
6758 setscreencolor(value, ((selector == NULL)?-1:atoi(selector)), flags);
6763 setconfregion(char *selector, char *value, int flags)
6765 /* suppress unused warnings since vars are needed */
6769 custom_region(value);
6774 setautorun(char *selector, char *value, int flags)
6784 /* suppress unused warnings since vars are needed */
6788 if (getenv("SWM_STARTED"))
6792 if (sscanf(value, "ws[%d]:%1023c", &ws_id, s) != 2)
6793 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
6795 if (ws_id < 0 || ws_id >= workspace_limit)
6796 errx(1, "autorun: invalid workspace %d", ws_id + 1);
6798 sp = expand_tilde((char *)&s);
6801 * This is a little intricate
6803 * If the pid already exists we simply reuse it because it means it was
6804 * used before AND not claimed by manage_window. We get away with
6805 * altering it in the parent after INSERT because this can not be a race
6808 while ((ap = strsep(&sp, " \t")) != NULL) {
6811 DNPRINTF(SWM_D_SPAWN, "setautorun: arg [%s]\n", ap);
6813 if ((a.argv = realloc(a.argv, argc * sizeof(char *))) == NULL)
6814 err(1, "setautorun: realloc");
6815 a.argv[argc - 1] = ap;
6819 if ((a.argv = realloc(a.argv, (argc + 1) * sizeof(char *))) == NULL)
6820 err(1, "setautorun: realloc");
6821 a.argv[argc] = NULL;
6823 if ((pid = fork()) == 0) {
6824 spawn(ws_id, &a, 1);
6833 p = calloc(1, sizeof *p);
6836 TAILQ_INSERT_TAIL(&pidlist, p, entry);
6846 setlayout(char *selector, char *value, int flags)
6848 int ws_id, i, x, mg, ma, si, ar, f = 0;
6849 int st = SWM_V_STACK, num_screens;
6851 struct workspace *ws;
6853 /* suppress unused warnings since vars are needed */
6857 if (getenv("SWM_STARTED"))
6861 if (sscanf(value, "ws[%d]:%d:%d:%d:%d:%1023c",
6862 &ws_id, &mg, &ma, &si, &ar, s) != 6)
6863 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
6864 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
6867 if (ws_id < 0 || ws_id >= workspace_limit)
6868 errx(1, "layout: invalid workspace %d", ws_id + 1);
6870 if (!strcasecmp(s, "vertical"))
6872 else if (!strcasecmp(s, "vertical_flip")) {
6875 } else if (!strcasecmp(s, "horizontal"))
6877 else if (!strcasecmp(s, "horizontal_flip")) {
6880 } else if (!strcasecmp(s, "fullscreen"))
6883 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
6884 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
6887 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
6888 for (i = 0; i < num_screens; i++) {
6889 ws = (struct workspace *)&screens[i].ws;
6890 ws[ws_id].cur_layout = &layouts[st];
6892 ws[ws_id].always_raise = ar;
6893 if (st == SWM_MAX_STACK)
6897 for (x = 0; x < abs(mg); x++) {
6898 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6899 mg >= 0 ? SWM_ARG_ID_MASTERGROW :
6900 SWM_ARG_ID_MASTERSHRINK);
6904 for (x = 0; x < abs(ma); x++) {
6905 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6906 ma >= 0 ? SWM_ARG_ID_MASTERADD :
6907 SWM_ARG_ID_MASTERDEL);
6911 for (x = 0; x < abs(si); x++) {
6912 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6913 si >= 0 ? SWM_ARG_ID_STACKINC :
6914 SWM_ARG_ID_STACKDEC);
6919 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6920 SWM_ARG_ID_FLIPLAYOUT);
6930 /* config options */
6931 struct config_option {
6933 int (*func)(char*, char*, int);
6936 struct config_option configopt[] = {
6937 { "autorun", setautorun, 0 },
6938 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
6939 { "bar_at_bottom", setconfvalue, SWM_S_BAR_AT_BOTTOM },
6940 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
6941 { "bar_border_width", setconfvalue, SWM_S_BAR_BORDER_WIDTH },
6942 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
6943 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
6944 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
6945 { "bar_enabled_ws", setconfvalue, SWM_S_BAR_ENABLED_WS },
6946 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
6947 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
6948 { "bar_format", setconfvalue, SWM_S_BAR_FORMAT },
6949 { "bar_justify", setconfvalue, SWM_S_BAR_JUSTIFY },
6950 { "bind", setconfbinding, 0 },
6951 { "border_width", setconfvalue, SWM_S_BORDER_WIDTH },
6952 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
6953 { "clock_format", setconfvalue, SWM_S_CLOCK_FORMAT },
6954 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
6955 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
6956 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
6957 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
6958 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
6959 { "disable_border", setconfvalue, SWM_S_DISABLE_BORDER },
6960 { "focus_close", setconfvalue, SWM_S_FOCUS_CLOSE },
6961 { "focus_close_wrap", setconfvalue, SWM_S_FOCUS_CLOSE_WRAP },
6962 { "focus_default", setconfvalue, SWM_S_FOCUS_DEFAULT },
6963 { "focus_mode", setconfvalue, SWM_S_FOCUS_MODE },
6964 { "keyboard_mapping", setkeymapping, 0 },
6965 { "layout", setlayout, 0 },
6966 { "modkey", setconfmodkey, 0 },
6967 { "program", setconfspawn, 0 },
6968 { "quirk", setconfquirk, 0 },
6969 { "region", setconfregion, 0 },
6970 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
6971 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
6972 { "spawn_position", setconfvalue, SWM_S_SPAWN_ORDER },
6973 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
6974 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
6975 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
6976 { "title_class_enabled", setconfvalue, SWM_S_TITLE_CLASS_ENABLED },
6977 { "title_name_enabled", setconfvalue, SWM_S_TITLE_NAME_ENABLED },
6978 { "urgent_enabled", setconfvalue, SWM_S_URGENT_ENABLED },
6979 { "verbose_layout", setconfvalue, SWM_S_VERBOSE_LAYOUT },
6980 { "window_name_enabled", setconfvalue, SWM_S_WINDOW_NAME_ENABLED },
6981 { "workspace_limit", setconfvalue, SWM_S_WORKSPACE_LIMIT },
6985 conf_load(const char *filename, int keymapping)
6988 char *line, *cp, *optsub, *optval;
6989 size_t linelen, lineno = 0;
6990 int wordlen, i, optidx;
6991 struct config_option *opt = NULL;
6993 DNPRINTF(SWM_D_CONF, "conf_load: begin\n");
6995 if (filename == NULL) {
6996 warnx("conf_load: no filename");
6999 if ((config = fopen(filename, "r")) == NULL) {
7000 warn("conf_load: fopen: %s", filename);
7004 while (!feof(config)) {
7005 if ((line = fparseln(config, &linelen, &lineno, NULL, 0))
7008 err(1, "%s", filename);
7013 cp += strspn(cp, " \t\n"); /* eat whitespace */
7014 if (cp[0] == '\0') {
7019 /* get config option */
7020 wordlen = strcspn(cp, "=[ \t\n");
7022 warnx("%s: line %zd: no option found",
7027 for (i = 0; i < LENGTH(configopt); i++) {
7028 opt = &configopt[i];
7029 if (!strncasecmp(cp, opt->optname, wordlen) &&
7030 (int)strlen(opt->optname) == wordlen) {
7036 warnx("%s: line %zd: unknown option %.*s",
7037 filename, lineno, wordlen, cp);
7040 if (keymapping && opt && strcmp(opt->optname, "bind")) {
7041 warnx("%s: line %zd: invalid option %.*s",
7042 filename, lineno, wordlen, cp);
7046 cp += strspn(cp, " \t\n"); /* eat whitespace */
7047 /* get [selector] if any */
7051 wordlen = strcspn(cp, "]");
7054 warnx("%s: line %zd: syntax error",
7059 if (asprintf(&optsub, "%.*s", wordlen, cp) ==
7061 warnx("%s: line %zd: unable to allocate"
7062 "memory for selector", filename,
7068 cp += strspn(cp, "] \t\n"); /* eat trailing */
7070 cp += strspn(cp, "= \t\n"); /* eat trailing */
7072 optval = strdup(cp);
7073 /* call function to deal with it all */
7074 if (configopt[optidx].func(optsub, optval,
7075 configopt[optidx].funcflags) != 0)
7076 errx(1, "%s: line %zd: invalid data for %s",
7077 filename, lineno, configopt[optidx].optname);
7084 DNPRINTF(SWM_D_CONF, "conf_load: end\n");
7091 DNPRINTF(SWM_D_CONF, "conf_load: end with error.\n");
7097 set_child_transient(struct ws_win *win, xcb_window_t *trans)
7099 struct ws_win *parent, *w;
7100 struct swm_region *r;
7101 struct workspace *ws;
7102 xcb_icccm_wm_hints_t wmh;
7104 parent = find_window(win->transient);
7106 parent->focus_child = win;
7108 DNPRINTF(SWM_D_MISC, "set_child_transient: parent doesn't exist"
7109 " for 0x%x trans 0x%x\n", win->id, win->transient);
7111 r = root_to_region(win->wa->root, SWM_CK_ALL);
7113 /* parent doen't exist in our window list */
7114 TAILQ_FOREACH(w, &ws->winlist, entry) {
7115 if (xcb_icccm_get_wm_hints_reply(conn,
7116 xcb_icccm_get_wm_hints(conn, w->id),
7118 warnx("can't get hints for 0x%x", w->id);
7122 if (win->hints.window_group != wmh.window_group)
7125 w->focus_child = win;
7126 win->transient = w->id;
7128 DNPRINTF(SWM_D_MISC, "set_child_transient: adjusting "
7129 "transient to 0x%x\n", win->transient);
7136 window_get_pid(xcb_window_t win)
7141 xcb_get_property_cookie_t pc;
7142 xcb_get_property_reply_t *pr;
7144 apid = get_atom_from_string("_NET_WM_PID");
7145 if (apid == XCB_ATOM_NONE)
7148 pc = xcb_get_property(conn, 0, win, apid, XCB_ATOM_CARDINAL, 0, 1);
7149 pr = xcb_get_property_reply(conn, pc, NULL);
7152 if (pr->type != XCB_ATOM_CARDINAL) {
7157 if (pr->type == apid && pr->format == 32)
7158 ret = *((pid_t *)xcb_get_property_value(pr));
7164 apid = get_atom_from_string("_SWM_PID");
7165 pc = xcb_get_property(conn, 0, win, apid, XCB_ATOM_STRING,
7167 pr = xcb_get_property_reply(conn, pc, NULL);
7170 if (pr->type != apid) {
7175 ret = (pid_t)strtonum(xcb_get_property_value(pr), 0, INT_MAX, &errstr);
7182 get_ws_idx(xcb_window_t id)
7188 xcb_get_property_reply_t *gpr;
7190 gpr = xcb_get_property_reply(conn,
7191 xcb_get_property(conn, 0, id, a_swm_ws,
7192 XCB_ATOM_STRING, 0, SWM_PROPLEN),
7197 proplen = xcb_get_property_value_length(gpr);
7199 prop = malloc(proplen + 1);
7202 xcb_get_property_value(gpr),
7204 prop[proplen] = '\0';
7211 DNPRINTF(SWM_D_PROP, "get_ws_idx: _SWM_WS: %s\n", prop);
7212 ws_idx = (int)strtonum(prop, 0, workspace_limit - 1, &errstr);
7214 DNPRINTF(SWM_D_PROP, "get_ws_idx: window: #%s: %s",
7224 manage_window(xcb_window_t id, uint16_t mapped)
7226 xcb_window_t trans = XCB_WINDOW_NONE;
7227 struct ws_win *win, *ww;
7229 char ws_idx_str[SWM_PROPLEN];
7230 struct swm_region *r;
7234 xcb_icccm_get_wm_protocols_reply_t wpr;
7236 if ((win = find_window(id)) != NULL) {
7237 DNPRINTF(SWM_D_MISC, "manage_window: win 0x%x already "
7238 "managed; skipping.)\n", id);
7239 return (win); /* Already managed. */
7242 /* See if window is on the unmanaged list. */
7243 if ((win = find_unmanaged_window(id)) != NULL) {
7244 DNPRINTF(SWM_D_MISC, "manage_window: win 0x%x found on "
7245 "unmanaged list.\n", id);
7246 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
7249 set_child_transient(win, &trans);
7253 DNPRINTF(SWM_D_MISC, "manage_window: win 0x%x is new.\n", id);
7256 /* Create and initialize ws_win object. */
7257 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
7258 err(1, "manage_window: calloc: failed to allocate memory for "
7263 /* Get window geometry. */
7264 win->wa = xcb_get_geometry_reply(conn,
7265 xcb_get_geometry(conn, win->id),
7268 /* Figure out which region the window belongs to. */
7269 r = root_to_region(win->wa->root, SWM_CK_ALL);
7271 /* Ignore window border if there is one. */
7272 WIDTH(win) = win->wa->width;
7273 HEIGHT(win) = win->wa->height;
7274 X(win) = win->wa->x + win->wa->border_width - border_width;
7275 Y(win) = win->wa->y + win->wa->border_width - border_width;
7277 win->mapped = mapped;
7278 win->floatmaxed = 0;
7279 win->ewmh_flags = 0;
7280 win->s = r->s; /* this never changes */
7282 store_float_geom(win, r);
7284 /* Get WM_SIZE_HINTS. */
7285 xcb_icccm_get_wm_normal_hints_reply(conn,
7286 xcb_icccm_get_wm_normal_hints(conn, win->id),
7290 xcb_icccm_get_wm_hints_reply(conn,
7291 xcb_icccm_get_wm_hints(conn, win->id),
7294 /* Get WM_TRANSIENT_FOR; see if window is a transient. */
7295 xcb_icccm_get_wm_transient_for_reply(conn,
7296 xcb_icccm_get_wm_transient_for(conn, win->id),
7299 win->transient = trans;
7300 set_child_transient(win, &win->transient);
7303 /* Get supported protocols. */
7304 if (xcb_icccm_get_wm_protocols_reply(conn,
7305 xcb_icccm_get_wm_protocols(conn, win->id, a_prot),
7307 for (i = 0; i < wpr.atoms_len; i++) {
7308 if (wpr.atoms[i] == a_takefocus)
7309 win->take_focus = 1;
7310 if (wpr.atoms[i] == a_delete)
7311 win->can_delete = 1;
7313 xcb_icccm_get_wm_protocols_reply_wipe(&wpr);
7316 win->iconic = get_swm_iconic(win);
7318 /* Figure out which workspace the window belongs to. */
7319 if ((p = find_pid(window_get_pid(win->id))) != NULL) {
7320 win->ws = &r->s->ws[p->ws];
7321 TAILQ_REMOVE(&pidlist, p, entry);
7324 } else if ((ws_idx = get_ws_idx(win->id)) != -1 &&
7326 /* _SWM_WS is set; use that. */
7327 win->ws = &r->s->ws[ws_idx];
7328 } else if (trans && (ww = find_window(trans)) != NULL) {
7329 /* Launch transients in the same ws as parent. */
7335 /* Set the _SWM_WS atom so we can remember this after reincarnation. */
7336 if (snprintf(ws_idx_str, SWM_PROPLEN, "%d", win->ws->idx) <
7338 DNPRINTF(SWM_D_PROP, "manage_window: set _SWM_WS: %s\n",
7340 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
7341 a_swm_ws, XCB_ATOM_STRING, 8, strlen(ws_idx_str),
7346 ewmh_autoquirk(win);
7348 /* Determine initial quirks. */
7349 if (xcb_icccm_get_wm_class_reply(conn,
7350 xcb_icccm_get_wm_class(conn, win->id),
7352 DNPRINTF(SWM_D_CLASS, "manage_window: class: %s, name: %s\n",
7353 win->ch.class_name, win->ch.instance_name);
7355 /* java is retarded so treat it special */
7356 if (strstr(win->ch.instance_name, "sun-awt")) {
7357 DNPRINTF(SWM_D_CLASS, "manage_window: java window "
7362 TAILQ_FOREACH(qp, &quirks, entry) {
7363 if (!strcmp(win->ch.class_name, qp->class) &&
7364 !strcmp(win->ch.instance_name, qp->name)) {
7365 DNPRINTF(SWM_D_CLASS, "manage_window: on quirks"
7366 "list; mask: 0x%lx\n", qp->quirk);
7367 if (qp->quirk & SWM_Q_FLOAT)
7369 win->quirks = qp->quirk;
7374 /* Alter window position if quirky */
7375 if (win->quirks & SWM_Q_ANYWHERE)
7378 /* Reset font sizes (the bruteforce way; no default keybinding). */
7379 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
7380 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
7381 fake_keypress(win, XK_KP_Subtract, XCB_MOD_MASK_SHIFT);
7382 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
7383 fake_keypress(win, XK_KP_Add, XCB_MOD_MASK_SHIFT);
7386 /* Make sure window is positioned inside its region, if its active. */
7388 constrain_window(win, win->ws->r, 0);
7392 /* Select which X events to monitor and set border pixel color. */
7393 wa[0] = win->s->c[SWM_S_COLOR_UNFOCUS].pixel;
7394 wa[1] = XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_PROPERTY_CHANGE |
7395 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
7397 wa[1] |= XCB_EVENT_MASK_LEAVE_WINDOW | XCB_EVENT_MASK_FOCUS_CHANGE;
7400 xcb_change_window_attributes(conn, win->id, XCB_CW_BORDER_PIXEL |
7401 XCB_CW_EVENT_MASK, wa);
7404 /* Figure out where to stack the window in the workspace. */
7405 if (trans && (ww = find_window(trans)))
7406 TAILQ_INSERT_AFTER(&win->ws->winlist, ww, win, entry);
7407 else if (win->ws->focus && spawn_position == SWM_STACK_ABOVE)
7408 TAILQ_INSERT_AFTER(&win->ws->winlist, win->ws->focus, win,
7410 else if (win->ws->focus && spawn_position == SWM_STACK_BELOW)
7411 TAILQ_INSERT_BEFORE(win->ws->focus, win, entry);
7412 else switch (spawn_position) {
7415 case SWM_STACK_ABOVE:
7416 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
7418 case SWM_STACK_BOTTOM:
7419 case SWM_STACK_BELOW:
7420 TAILQ_INSERT_HEAD(&win->ws->winlist, win, entry);
7423 /* Get initial _NET_WM_STATE */
7424 ewmh_get_win_state(win);
7425 /* Set initial _NET_WM_ALLOWED_ACTIONS */
7426 ewmh_update_actions(win);
7430 DNPRINTF(SWM_D_MISC, "manage_window: done. window: 0x%x, (x,y) w x h: "
7431 "(%d,%d) %d x %d, ws: %d, iconic: %s, transient: 0x%x\n", win->id,
7432 X(win), Y(win), WIDTH(win), HEIGHT(win), win->ws->idx,
7433 YESNO(win->iconic), win->transient);
7439 free_window(struct ws_win *win)
7441 DNPRINTF(SWM_D_MISC, "free_window: window: 0x%x\n", win->id);
7446 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
7451 xcb_icccm_get_wm_class_reply_wipe(&win->ch);
7456 memset(win, 0xff, sizeof *win); /* XXX kill later */
7459 DNPRINTF(SWM_D_MISC, "free_window: done.\n");
7463 unmanage_window(struct ws_win *win)
7465 struct ws_win *parent;
7470 DNPRINTF(SWM_D_MISC, "unmanage_window: window: 0x%x\n", win->id);
7472 if (win->transient) {
7473 parent = find_window(win->transient);
7475 parent->focus_child = NULL;
7478 TAILQ_REMOVE(&win->ws->winlist, win, entry);
7479 TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
7483 expose(xcb_expose_event_t *e)
7486 struct swm_region *r;
7488 DNPRINTF(SWM_D_EVENT, "expose: window: 0x%x\n", e->window);
7490 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7491 for (i = 0; i < num_screens; i++)
7492 TAILQ_FOREACH(r, &screens[i].rl, entry)
7493 if (e->window == WINID(r->bar))
7501 focusin(xcb_focus_in_event_t *e)
7503 DNPRINTF(SWM_D_EVENT, "focusin: window: 0x%x, mode: %s(%u), "
7504 "detail: %s(%u)\n", e->event, get_notify_mode_label(e->mode),
7505 e->mode, get_notify_detail_label(e->detail), e->detail);
7509 focusout(xcb_focus_out_event_t *e)
7511 DNPRINTF(SWM_D_EVENT, "focusout: window: 0x%x, mode: %s(%u), "
7512 "detail: %s(%u)\n", e->event, get_notify_mode_label(e->mode),
7513 e->mode, get_notify_detail_label(e->detail), e->detail);
7518 keypress(xcb_key_press_event_t *e)
7520 xcb_keysym_t keysym;
7523 keysym = xcb_key_press_lookup_keysym(syms, e, 0);
7525 DNPRINTF(SWM_D_EVENT, "keypress: %u %u\n", e->detail, keysym);
7527 if ((kp = key_lookup(CLEANMASK(e->state), keysym)) == NULL)
7530 last_event_time = e->time;
7532 if (kp->funcid == KF_SPAWN_CUSTOM)
7533 spawn_custom(root_to_region(e->root, SWM_CK_ALL),
7534 &(keyfuncs[kp->funcid].args), kp->spawn_name);
7535 else if (keyfuncs[kp->funcid].func)
7536 keyfuncs[kp->funcid].func(root_to_region(e->root, SWM_CK_ALL),
7537 &(keyfuncs[kp->funcid].args));
7541 buttonpress(xcb_button_press_event_t *e)
7543 struct ws_win *win = NULL;
7544 struct swm_region *r, *old_r;
7548 DNPRINTF(SWM_D_EVENT, "buttonpress: win (x,y): 0x%x (%d,%d), "
7549 "detail: %u, time: %u, root (x,y): 0x%x (%d,%d), child: 0x%x, "
7550 "state: %u, same_screen: %s\n", e->event, e->event_x, e->event_y,
7551 e->detail, e->time, e->root, e->root_x, e->root_y, e->child,
7552 e->state, YESNO(e->same_screen));
7554 if (e->event == e->root) {
7555 if (e->child != 0) {
7556 win = find_window(e->child);
7557 /* Pass ButtonPress to window if it isn't managed. */
7561 /* Focus on empty region */
7562 /* If no windows on region if its empty. */
7563 r = root_to_region(e->root, SWM_CK_POINTER);
7565 DNPRINTF(SWM_D_EVENT, "buttonpress: "
7566 "NULL region; ignoring.\n");
7570 if (TAILQ_EMPTY(&r->ws->winlist)) {
7571 old_r = root_to_region(e->root, SWM_CK_FOCUS);
7572 if (old_r && old_r != r)
7573 unfocus_win(old_r->ws->focus);
7575 xcb_set_input_focus(conn,
7576 XCB_INPUT_FOCUS_PARENT, e->root, e->time);
7578 /* Clear bar since empty. */
7586 win = find_window(e->event);
7592 last_event_time = e->time;
7594 focus_win(get_focus_magic(win));
7596 for (i = 0; i < LENGTH(buttons); i++)
7597 if (client_click == buttons[i].action && buttons[i].func &&
7598 buttons[i].button == e->detail &&
7599 CLEANMASK(buttons[i].mask) == CLEANMASK(e->state)) {
7600 buttons[i].func(win, &buttons[i].args);
7606 DNPRINTF(SWM_D_EVENT, "buttonpress: passing to window.\n");
7607 /* Replay event to event window */
7608 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, e->time);
7610 DNPRINTF(SWM_D_EVENT, "buttonpress: handled.\n");
7611 /* Unfreeze grab events. */
7612 xcb_allow_events(conn, XCB_ALLOW_SYNC_POINTER, e->time);
7620 print_win_geom(xcb_window_t w)
7622 xcb_get_geometry_reply_t *wa;
7624 wa = xcb_get_geometry_reply(conn, xcb_get_geometry(conn, w), NULL);
7627 DNPRINTF(SWM_D_MISC, "print_win_geom: window not found: 0x%x\n",
7632 DNPRINTF(SWM_D_MISC, "print_win_geom: window: 0x%x, root: 0x%x, "
7633 "depth: %u, (x,y) w x h: (%d,%d) %d x %d, border: %d\n",
7634 w, wa->root, wa->depth, wa->x, wa->y, wa->width, wa->height,
7643 get_stack_mode_name(uint8_t mode)
7648 case XCB_STACK_MODE_ABOVE:
7651 case XCB_STACK_MODE_BELOW:
7654 case XCB_STACK_MODE_TOP_IF:
7657 case XCB_STACK_MODE_BOTTOM_IF:
7660 case XCB_STACK_MODE_OPPOSITE:
7672 configurerequest(xcb_configure_request_event_t *e)
7675 struct swm_region *r = NULL;
7678 uint32_t wc[7] = {0};
7680 if ((win = find_window(e->window)) == NULL)
7681 if ((win = find_unmanaged_window(e->window)) == NULL)
7685 if (swm_debug & SWM_D_EVENT) {
7686 print_win_geom(e->window);
7688 DNPRINTF(SWM_D_EVENT, "configurerequest: window: 0x%x, "
7689 "parent: 0x%x, new: %s, value_mask: %u { ", e->window,
7690 e->parent, YESNO(new), e->value_mask);
7691 if (e->value_mask & XCB_CONFIG_WINDOW_X)
7692 DPRINTF("X: %d ", e->x);
7693 if (e->value_mask & XCB_CONFIG_WINDOW_Y)
7694 DPRINTF("Y: %d ", e->y);
7695 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH)
7696 DPRINTF("W: %u ", e->width);
7697 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
7698 DPRINTF("H: %u ", e->height);
7699 if (e->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH)
7700 DPRINTF("Border: %u ", e->border_width);
7701 if (e->value_mask & XCB_CONFIG_WINDOW_SIBLING)
7702 DPRINTF("Sibling: 0x%x ", e->sibling);
7703 if (e->value_mask & XCB_CONFIG_WINDOW_STACK_MODE)
7704 DPRINTF("StackMode: %s(%u) ",
7705 get_stack_mode_name(e->stack_mode), e->stack_mode);
7711 if (e->value_mask & XCB_CONFIG_WINDOW_X) {
7712 mask |= XCB_CONFIG_WINDOW_X;
7715 if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
7716 mask |= XCB_CONFIG_WINDOW_Y;
7719 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
7720 mask |= XCB_CONFIG_WINDOW_WIDTH;
7723 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
7724 mask |= XCB_CONFIG_WINDOW_HEIGHT;
7725 wc[i++] = e->height;
7727 if (e->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH) {
7728 mask |= XCB_CONFIG_WINDOW_BORDER_WIDTH;
7729 wc[i++] = e->border_width;
7731 if (e->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
7732 mask |= XCB_CONFIG_WINDOW_SIBLING;
7733 wc[i++] = e->sibling;
7735 if (e->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
7736 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
7737 wc[i++] = e->stack_mode;
7741 xcb_configure_window(conn, e->window, mask, wc);
7744 } else if ((!win->manual || win->quirks & SWM_Q_ANYWHERE) &&
7745 !(win->ewmh_flags & EWMH_F_FULLSCREEN)) {
7748 else if (win->ws->old_r)
7751 /* windows are centered unless ANYWHERE quirk is set. */
7752 if (win->quirks & SWM_Q_ANYWHERE) {
7753 if (e->value_mask & XCB_CONFIG_WINDOW_X) {
7754 win->g_float.x = e->x;
7756 win->g_float.x -= X(r);
7759 if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
7760 win->g_float.y = e->y;
7762 win->g_float.y -= Y(r);
7766 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH)
7767 win->g_float.w = e->width;
7769 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
7770 win->g_float.h = e->height;
7772 win->g_floatvalid = 1;
7774 if (win->floating && r && (win->transient ||
7775 win->ws->cur_layout != &layouts[SWM_MAX_STACK])) {
7776 WIDTH(win) = win->g_float.w;
7777 HEIGHT(win) = win->g_float.h;
7780 stack_floater(win, r);
7792 DNPRINTF(SWM_D_EVENT, "configurerequest: done.\n");
7796 configurenotify(xcb_configure_notify_event_t *e)
7800 DNPRINTF(SWM_D_EVENT, "configurenotify: win 0x%x, event win: 0x%x, "
7801 "(x,y) WxH: (%d,%d) %ux%u, border: %u, above_sibling: 0x%x, "
7802 "override_redirect: %s\n", e->window, e->event, e->x, e->y,
7803 e->width, e->height, e->border_width, e->above_sibling,
7804 YESNO(e->override_redirect));
7806 win = find_window(e->window);
7808 xcb_icccm_get_wm_normal_hints_reply(conn,
7809 xcb_icccm_get_wm_normal_hints(conn, win->id),
7812 if (font_adjusted) {
7820 destroynotify(xcb_destroy_notify_event_t *e)
7824 DNPRINTF(SWM_D_EVENT, "destroynotify: window: 0x%x\n", e->window);
7826 if ((win = find_window(e->window)) == NULL) {
7827 if ((win = find_unmanaged_window(e->window)) == NULL)
7833 if (focus_mode != SWM_FOCUS_FOLLOW) {
7834 /* If we were focused, make sure we focus on something else. */
7835 if (win == win->ws->focus)
7836 win->ws->focus_pending = get_focus_prev(win);
7839 unmanage_window(win);
7842 if (focus_mode != SWM_FOCUS_FOLLOW) {
7843 if (win->ws->focus_pending) {
7844 focus_win(win->ws->focus_pending);
7845 win->ws->focus_pending = NULL;
7856 get_notify_detail_label(uint8_t detail)
7861 case XCB_NOTIFY_DETAIL_ANCESTOR:
7864 case XCB_NOTIFY_DETAIL_VIRTUAL:
7867 case XCB_NOTIFY_DETAIL_INFERIOR:
7870 case XCB_NOTIFY_DETAIL_NONLINEAR:
7871 label = "Nonlinear";
7873 case XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL:
7874 label = "NonlinearVirtual";
7876 case XCB_NOTIFY_DETAIL_POINTER:
7879 case XCB_NOTIFY_DETAIL_POINTER_ROOT:
7880 label = "PointerRoot";
7882 case XCB_NOTIFY_DETAIL_NONE:
7893 get_notify_mode_label(uint8_t mode)
7898 case XCB_NOTIFY_MODE_NORMAL:
7901 case XCB_NOTIFY_MODE_GRAB:
7904 case XCB_NOTIFY_MODE_UNGRAB:
7907 case XCB_NOTIFY_MODE_WHILE_GRABBED:
7908 label = "WhileGrabbed";
7919 enternotify(xcb_enter_notify_event_t *e)
7922 struct swm_region *old_r, *r;
7924 DNPRINTF(SWM_D_FOCUS, "enternotify: time: %u, win (x,y): 0x%x "
7925 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): 0x%x (%d,%d), "
7926 "child: 0x%x, same_screen_focus: %s, state: %d\n",
7927 e->time, e->event, e->event_x, e->event_y,
7928 get_notify_mode_label(e->mode), e->mode,
7929 get_notify_detail_label(e->detail), e->detail,
7930 e->root, e->root_x, e->root_y, e->child,
7931 YESNO(e->same_screen_focus), e->state);
7933 if (focus_mode == SWM_FOCUS_MANUAL &&
7934 e->mode == XCB_NOTIFY_MODE_NORMAL) {
7935 DNPRINTF(SWM_D_EVENT, "enternotify: manual focus; ignoring.\n");
7939 last_event_time = e->time;
7941 if ((win = find_window(e->event)) == NULL) {
7942 if (e->event == e->root) {
7943 /* If no windows on pointer region, then focus root. */
7944 r = root_to_region(e->root, SWM_CK_POINTER);
7946 DNPRINTF(SWM_D_EVENT, "enterntoify: "
7947 "NULL region; ignoring.\n");
7951 if (TAILQ_EMPTY(&r->ws->winlist)) {
7952 old_r = root_to_region(e->root, SWM_CK_FOCUS);
7953 if (old_r && old_r != r)
7954 unfocus_win(old_r->ws->focus);
7956 xcb_set_input_focus(conn,
7957 XCB_INPUT_FOCUS_PARENT, e->root, e->time);
7959 /* Clear bar since empty. */
7965 DNPRINTF(SWM_D_EVENT, "enternotify: window is NULL; "
7971 focus_win(get_focus_magic(win));
7978 leavenotify(xcb_leave_notify_event_t *e)
7980 DNPRINTF(SWM_D_FOCUS, "leavenotify: time: %u, win (x,y): 0x%x "
7981 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): 0x%x (%d,%d), "
7982 "child: 0x%x, same_screen_focus: %s, state: %d\n",
7983 e->time, e->event, e->event_x, e->event_y,
7984 get_notify_mode_label(e->mode), e->mode,
7985 get_notify_detail_label(e->detail), e->detail,
7986 e->root, e->root_x, e->root_y, e->child,
7987 YESNO(e->same_screen_focus), e->state);
7992 mapnotify(xcb_map_notify_event_t *e)
7996 DNPRINTF(SWM_D_EVENT, "mapnotify: window: 0x%x\n", e->window);
7998 if ((win = find_window(e->window)) == NULL)
7999 win = manage_window(e->window, 1);
8002 set_win_state(win, XCB_ICCCM_WM_STATE_NORMAL);
8004 if (focus_mode != SWM_FOCUS_FOLLOW) {
8005 if (win->ws->focus_pending == win) {
8007 win->ws->focus_pending = NULL;
8016 mappingnotify(xcb_mapping_notify_event_t *e)
8018 xcb_refresh_keyboard_mapping(syms, e);
8020 if (e->request == XCB_MAPPING_KEYBOARD)
8025 maprequest(xcb_map_request_event_t *e)
8028 xcb_get_window_attributes_reply_t *war;
8030 DNPRINTF(SWM_D_EVENT, "maprequest: win 0x%x\n",
8033 war = xcb_get_window_attributes_reply(conn,
8034 xcb_get_window_attributes(conn, e->window),
8037 DNPRINTF(SWM_D_EVENT, "maprequest: window lost.\n");
8041 if (war->override_redirect) {
8042 DNPRINTF(SWM_D_EVENT, "maprequest: override_redirect; "
8047 win = manage_window(e->window,
8048 (war->map_state == XCB_MAP_STATE_VIEWABLE));
8050 /* The new window should get focus; prepare. */
8051 if (focus_mode != SWM_FOCUS_FOLLOW)
8052 win->ws->focus_pending = get_focus_magic(win);
8054 /* All windows need to be mapped if they are in the current workspace.*/
8058 /* Ignore EnterNotify to handle the mapnotify without interference. */
8059 if (focus_mode == SWM_FOCUS_DEFAULT)
8060 event_drain(XCB_ENTER_NOTIFY);
8063 DNPRINTF(SWM_D_EVENT, "maprequest: done.\n");
8068 get_atom_name(xcb_atom_t atom)
8073 * This should be disabled during most debugging since
8074 * xcb_get_* causes an xcb_flush.
8077 xcb_get_atom_name_reply_t *r;
8079 r = xcb_get_atom_name_reply(conn,
8080 xcb_get_atom_name(conn, atom),
8083 len = xcb_get_atom_name_name_length(r);
8085 name = malloc(len + 1);
8087 memcpy(name, xcb_get_atom_name_name(r), len);
8101 propertynotify(xcb_property_notify_event_t *e)
8107 name = get_atom_name(e->atom);
8108 DNPRINTF(SWM_D_EVENT, "propertynotify: window: 0x%x, atom: %s(%u), "
8109 "time: %#x, state: %u\n", e->window, name, e->atom, e->time,
8113 win = find_window(e->window);
8117 last_event_time = e->time;
8119 if (e->atom == a_swm_iconic) {
8120 if (e->state == XCB_PROPERTY_NEW_VALUE) {
8121 if (focus_mode != SWM_FOCUS_FOLLOW)
8122 win->ws->focus_pending = get_focus_prev(win);
8129 if (focus_mode != SWM_FOCUS_FOLLOW) {
8130 focus_win(win->ws->focus_pending);
8131 win->ws->focus_pending = NULL;
8135 } else if (e->state == XCB_PROPERTY_DELETE) {
8136 /* The window is no longer iconic, restack ws. */
8137 if (focus_mode != SWM_FOCUS_FOLLOW)
8138 win->ws->focus_pending = get_focus_magic(win);
8142 /* Flush EnterNotify for mapnotify, if needed. */
8145 } else if (e->atom == a_state) {
8146 /* State just changed, make sure it gets focused if mapped. */
8147 if (e->state == XCB_PROPERTY_NEW_VALUE) {
8148 if (focus_mode != SWM_FOCUS_FOLLOW) {
8150 win->ws->focus_pending == win) {
8151 focus_win(win->ws->focus_pending);
8152 win->ws->focus_pending = NULL;
8156 } else if (e->atom == XCB_ATOM_WM_CLASS ||
8157 e->atom == XCB_ATOM_WM_NAME) {
8165 unmapnotify(xcb_unmap_notify_event_t *e)
8168 struct workspace *ws;
8170 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: 0x%x\n", e->window);
8172 /* If we aren't managing the window, then ignore. */
8173 win = find_window(e->window);
8179 if (getstate(e->window) != XCB_ICCCM_WM_STATE_ICONIC)
8180 set_win_state(win, XCB_ICCCM_WM_STATE_ICONIC);
8183 /* window unmapped itself */
8184 /* do unmap/unfocus/restack and unmanage */
8187 /* If win was focused, make sure to focus on something else. */
8188 if (win == ws->focus) {
8189 if (focus_mode != SWM_FOCUS_FOLLOW) {
8190 ws->focus_pending = get_focus_prev(win);
8191 DNPRINTF(SWM_D_EVENT, "unmapnotify: "
8192 "focus_pending: 0x%x\n",
8193 WINID(ws->focus_pending));
8199 unmanage_window(win);
8204 if (focus_mode == SWM_FOCUS_FOLLOW) {
8206 focus_win(get_pointer_win(ws->r->s->root));
8208 if (ws->focus_pending) {
8209 focus_win(ws->focus_pending);
8210 ws->focus_pending = NULL;
8215 if (getstate(e->window) == XCB_ICCCM_WM_STATE_NORMAL)
8216 set_win_state(win, XCB_ICCCM_WM_STATE_ICONIC);
8223 visibilitynotify(xcb_visibility_notify_event_t *e)
8225 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: 0x%x\n",
8231 clientmessage(xcb_client_message_event_t *e)
8234 xcb_map_request_event_t mre;
8238 name = get_atom_name(e->type);
8239 DNPRINTF(SWM_D_EVENT, "clientmessage: window: 0x%x, atom: %s(%u)\n",
8240 e->window, name, e->type);
8243 win = find_window(e->window);
8246 if (e->type == ewmh[_NET_ACTIVE_WINDOW].atom) {
8247 /* Manage the window with maprequest. */
8248 DNPRINTF(SWM_D_EVENT, "clientmessage: request focus on "
8249 "unmanaged window.\n");
8250 mre.window = e->window;
8256 if (e->type == ewmh[_NET_ACTIVE_WINDOW].atom) {
8257 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_ACTIVE_WINDOW\n");
8260 if (e->type == ewmh[_NET_CLOSE_WINDOW].atom) {
8261 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_CLOSE_WINDOW\n");
8262 if (win->can_delete)
8263 client_msg(win, a_delete, 0);
8265 xcb_kill_client(conn, win->id);
8267 if (e->type == ewmh[_NET_MOVERESIZE_WINDOW].atom) {
8268 DNPRINTF(SWM_D_EVENT,
8269 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
8270 if (win->floating) {
8271 if (e->data.data32[0] & (1<<8)) /* x */
8272 X(win) = e->data.data32[1];
8273 if (e->data.data32[0] & (1<<9)) /* y */
8274 Y(win) = e->data.data32[2];
8275 if (e->data.data32[0] & (1<<10)) /* width */
8276 WIDTH(win) = e->data.data32[3];
8277 if (e->data.data32[0] & (1<<11)) /* height */
8278 HEIGHT(win) = e->data.data32[4];
8283 /* TODO: Change stack sizes */
8284 /* notify no change was made. */
8285 config_win(win, NULL);
8288 if (e->type == ewmh[_NET_WM_STATE].atom) {
8289 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_WM_STATE\n");
8290 ewmh_update_win_state(win, e->data.data32[1], e->data.data32[0]);
8291 if (e->data.data32[2])
8292 ewmh_update_win_state(win, e->data.data32[2],
8304 int errcode = xcb_connection_has_error(conn);
8305 #ifdef XCB_CONN_ERROR
8308 case XCB_CONN_ERROR:
8309 s = "Socket error, pipe error or other stream error.";
8311 case XCB_CONN_CLOSED_EXT_NOTSUPPORTED:
8312 s = "Extension not supported.";
8314 case XCB_CONN_CLOSED_MEM_INSUFFICIENT:
8315 s = "Insufficient memory.";
8317 case XCB_CONN_CLOSED_REQ_LEN_EXCEED:
8318 s = "Request length was exceeded.";
8320 case XCB_CONN_CLOSED_PARSE_ERR:
8321 s = "Error parsing display string.";
8324 s = "Unknown error.";
8327 errx(errcode, "X CONNECTION ERROR: %s", s);
8330 errx(errcode, "X CONNECTION ERROR");
8338 const uint32_t val = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
8339 XCB_EVENT_MASK_ENTER_WINDOW;
8341 xcb_void_cookie_t wac;
8342 xcb_generic_error_t *error;
8344 /* this causes an error if some other window manager is running */
8345 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
8346 for (i = 0; i < num_screens; i++) {
8347 if ((sc = get_screen(i)) == NULL)
8348 errx(1, "ERROR: can't get screen %d.", i);
8349 DNPRINTF(SWM_D_INIT, "enable_wm: screen %d, root: 0x%x\n",
8351 wac = xcb_change_window_attributes_checked(conn, sc->root,
8352 XCB_CW_EVENT_MASK, &val);
8353 if ((error = xcb_request_check(conn, wac))) {
8354 DNPRINTF(SWM_D_INIT, "enable_wm: error_code: %u\n",
8360 /* click to focus on empty region */
8361 xcb_grab_button(conn, 1, sc->root, BUTTONMASK,
8362 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC, XCB_WINDOW_NONE,
8363 XCB_CURSOR_NONE, XCB_BUTTON_INDEX_1, XCB_BUTTON_MASK_ANY);
8370 new_region(struct swm_screen *s, int x, int y, int w, int h)
8372 struct swm_region *r, *n;
8373 struct workspace *ws = NULL;
8376 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
8377 s->idx, w, h, x, y);
8379 /* remove any conflicting regions */
8380 n = TAILQ_FIRST(&s->rl);
8383 n = TAILQ_NEXT(r, entry);
8384 if (X(r) < (x + w) &&
8385 (X(r) + WIDTH(r)) > x &&
8387 (Y(r) + HEIGHT(r)) > y) {
8388 if (r->ws->r != NULL)
8389 r->ws->old_r = r->ws->r;
8392 TAILQ_REMOVE(&s->rl, r, entry);
8393 TAILQ_INSERT_TAIL(&s->orl, r, entry);
8397 /* search old regions for one to reuse */
8399 /* size + location match */
8400 TAILQ_FOREACH(r, &s->orl, entry)
8401 if (X(r) == x && Y(r) == y &&
8402 HEIGHT(r) == h && WIDTH(r) == w)
8406 TAILQ_FOREACH(r, &s->orl, entry)
8407 if (HEIGHT(r) == h && WIDTH(r) == w)
8411 TAILQ_REMOVE(&s->orl, r, entry);
8412 /* try to use old region's workspace */
8413 if (r->ws->r == NULL)
8416 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
8417 err(1, "new_region: calloc: failed to allocate memory "
8420 /* if we don't have a workspace already, find one */
8422 for (i = 0; i < workspace_limit; i++)
8423 if (s->ws[i].r == NULL) {
8430 errx(1, "new_region: no free workspaces");
8441 TAILQ_INSERT_TAIL(&s->rl, r, entry);
8447 #ifdef SWM_XRR_HAS_CRTC
8450 #endif /* SWM_XRR_HAS_CRTC */
8451 struct swm_region *r;
8453 xcb_randr_get_screen_resources_current_cookie_t src;
8454 xcb_randr_get_screen_resources_current_reply_t *srr;
8455 xcb_randr_get_crtc_info_cookie_t cic;
8456 xcb_randr_get_crtc_info_reply_t *cir = NULL;
8457 xcb_randr_crtc_t *crtc;
8458 xcb_screen_t *screen;
8460 DNPRINTF(SWM_D_MISC, "scan_xrandr: screen: %d\n", i);
8462 if ((screen = get_screen(i)) == NULL)
8463 errx(1, "ERROR: can't get screen %d.", i);
8465 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
8466 if (i >= num_screens)
8467 errx(1, "scan_xrandr: invalid screen");
8469 /* remove any old regions */
8470 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
8471 r->ws->old_r = r->ws->r = NULL;
8473 TAILQ_REMOVE(&screens[i].rl, r, entry);
8474 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
8478 /* map virtual screens onto physical screens */
8479 #ifdef SWM_XRR_HAS_CRTC
8480 if (xrandr_support) {
8481 src = xcb_randr_get_screen_resources_current(conn,
8483 srr = xcb_randr_get_screen_resources_current_reply(conn, src,
8486 new_region(&screens[i], 0, 0,
8487 screen->width_in_pixels,
8488 screen->height_in_pixels);
8491 ncrtc = srr->num_crtcs;
8493 crtc = xcb_randr_get_screen_resources_current_crtcs(srr);
8494 for (c = 0; c < ncrtc; c++) {
8495 cic = xcb_randr_get_crtc_info(conn, crtc[c],
8497 cir = xcb_randr_get_crtc_info_reply(conn, cic, NULL);
8500 if (cir->num_outputs == 0) {
8506 new_region(&screens[i], 0, 0,
8507 screen->width_in_pixels,
8508 screen->height_in_pixels);
8510 new_region(&screens[i],
8511 cir->x, cir->y, cir->width, cir->height);
8516 #endif /* SWM_XRR_HAS_CRTC */
8518 /* If detection failed, create a single region that spans the screen. */
8519 if (TAILQ_EMPTY(&screens[i].rl))
8520 new_region(&screens[i], 0, 0, screen->width_in_pixels,
8521 screen->height_in_pixels);
8524 DNPRINTF(SWM_D_MISC, "scan_xrandr: done.\n");
8528 screenchange(xcb_randr_screen_change_notify_event_t *e)
8530 struct swm_region *r;
8533 DNPRINTF(SWM_D_EVENT, "screenchange: root: 0x%x\n", e->root);
8535 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
8536 /* silly event doesn't include the screen index */
8537 for (i = 0; i < num_screens; i++)
8538 if (screens[i].root == e->root)
8540 if (i >= num_screens)
8541 errx(1, "screenchange: screen not found");
8543 /* brute force for now, just re-enumerate the regions */
8547 print_win_geom(e->root);
8549 /* add bars to all regions */
8550 for (i = 0; i < num_screens; i++)
8551 TAILQ_FOREACH(r, &screens[i].rl, entry)
8559 xcb_window_t *wins = NULL, trans;
8561 int i, j, num_screens;
8562 uint16_t state, manage, mapped;
8564 xcb_query_tree_cookie_t qtc;
8565 xcb_query_tree_reply_t *qtr;
8566 xcb_get_window_attributes_cookie_t c;
8567 xcb_get_window_attributes_reply_t *r;
8568 xcb_get_property_cookie_t pc;
8570 DNPRINTF(SWM_D_INIT, "grab_windows: begin\n");
8571 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
8572 for (i = 0; i < num_screens; i++) {
8573 qtc = xcb_query_tree(conn, screens[i].root);
8574 qtr = xcb_query_tree_reply(conn, qtc, NULL);
8577 wins = xcb_query_tree_children(qtr);
8578 no = xcb_query_tree_children_length(qtr);
8579 /* attach windows to a region */
8580 /* normal windows */
8581 DNPRINTF(SWM_D_INIT, "grab_windows: grab top level windows.\n");
8582 for (j = 0; j < no; j++) {
8583 c = xcb_get_window_attributes(conn, wins[j]);
8584 r = xcb_get_window_attributes_reply(conn, c, NULL);
8586 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
8587 "doesn't exist.\n", wins[j]);
8590 if (r->override_redirect) {
8591 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
8592 "override_redirect set.\n", wins[j]);
8597 pc = xcb_icccm_get_wm_transient_for(conn, wins[j]);
8598 if (xcb_icccm_get_wm_transient_for_reply(conn, pc,
8600 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
8601 "is transient for %#x.\n", wins[j], trans);
8606 state = getstate(wins[j]);
8607 manage = state != XCB_ICCCM_WM_STATE_WITHDRAWN;
8608 mapped = r->map_state != XCB_MAP_STATE_UNMAPPED;
8609 if (mapped || manage)
8610 manage_window(wins[j], mapped);
8613 /* transient windows */
8614 DNPRINTF(SWM_D_INIT, "grab_windows: grab transient windows.\n");
8615 for (j = 0; j < no; j++) {
8616 c = xcb_get_window_attributes(conn, wins[j]);
8617 r = xcb_get_window_attributes_reply(conn, c, NULL);
8619 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
8620 "doesn't exist.\n", wins[j]);
8623 if (r->override_redirect) {
8624 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
8625 "override_redirect set.\n", wins[j]);
8630 state = getstate(wins[j]);
8631 manage = state != XCB_ICCCM_WM_STATE_WITHDRAWN;
8632 mapped = r->map_state != XCB_MAP_STATE_UNMAPPED;
8633 pc = xcb_icccm_get_wm_transient_for(conn, wins[j]);
8634 if (xcb_icccm_get_wm_transient_for_reply(conn, pc,
8635 &trans, NULL) && manage)
8636 manage_window(wins[j], mapped);
8641 DNPRINTF(SWM_D_INIT, "grab_windows: done.\n");
8647 int i, j, k, num_screens;
8648 struct workspace *ws;
8649 uint32_t gcv[1], wa[1];
8650 const xcb_query_extension_reply_t *qep;
8651 xcb_screen_t *screen;
8652 xcb_randr_query_version_cookie_t c;
8653 xcb_randr_query_version_reply_t *r;
8655 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
8656 if ((screens = calloc(num_screens,
8657 sizeof(struct swm_screen))) == NULL)
8658 err(1, "setup_screens: calloc: failed to allocate memory for "
8661 /* initial Xrandr setup */
8663 qep = xcb_get_extension_data(conn, &xcb_randr_id);
8665 c = xcb_randr_query_version(conn, 1, 1);
8666 r = xcb_randr_query_version_reply(conn, c, NULL);
8668 if (r->major_version >= 1) {
8670 xrandr_eventbase = qep->first_event;
8676 wa[0] = cursors[XC_LEFT_PTR].cid;
8678 /* map physical screens */
8679 for (i = 0; i < num_screens; i++) {
8680 DNPRINTF(SWM_D_WS, "setup_screens: init screen: %d\n", i);
8682 TAILQ_INIT(&screens[i].rl);
8683 TAILQ_INIT(&screens[i].orl);
8684 if ((screen = get_screen(i)) == NULL)
8685 errx(1, "ERROR: can't get screen %d.", i);
8686 screens[i].root = screen->root;
8688 /* set default colors */
8689 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
8690 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
8691 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
8692 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
8693 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
8695 /* create graphics context on screen */
8696 screens[i].bar_gc = xcb_generate_id(conn);
8698 xcb_create_gc(conn, screens[i].bar_gc, screens[i].root,
8699 XCB_GC_GRAPHICS_EXPOSURES, gcv);
8701 /* set default cursor */
8702 xcb_change_window_attributes(conn, screens[i].root,
8705 /* init all workspaces */
8706 /* XXX these should be dynamically allocated too */
8707 for (j = 0; j < SWM_WS_MAX; j++) {
8708 ws = &screens[i].ws[j];
8711 ws->bar_enabled = 1;
8713 ws->focus_prev = NULL;
8714 ws->focus_pending = NULL;
8717 TAILQ_INIT(&ws->winlist);
8718 TAILQ_INIT(&ws->unmanagedlist);
8720 for (k = 0; layouts[k].l_stack != NULL; k++)
8721 if (layouts[k].l_config != NULL)
8722 layouts[k].l_config(ws,
8723 SWM_ARG_ID_STACKINIT);
8724 ws->cur_layout = &layouts[0];
8725 ws->cur_layout->l_string(ws);
8731 xcb_randr_select_input(conn, screens[i].root,
8732 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE);
8739 if ((bar_fonts = strdup(SWM_BAR_FONTS)) == NULL)
8740 err(1, "setup_globals: strdup: failed to allocate memory.");
8742 if ((clock_format = strdup("%a %b %d %R %Z %Y")) == NULL)
8743 err(1, "setup_globals: strdup: failed to allocate memory.");
8745 if ((syms = xcb_key_symbols_alloc(conn)) == NULL)
8746 errx(1, "unable to allocate key symbols");
8748 a_state = get_atom_from_string("WM_STATE");
8749 a_prot = get_atom_from_string("WM_PROTOCOLS");
8750 a_delete = get_atom_from_string("WM_DELETE_WINDOW");
8751 a_takefocus = get_atom_from_string("WM_TAKE_FOCUS");
8752 a_wmname = get_atom_from_string("WM_NAME");
8753 a_netwmname = get_atom_from_string("_NET_WM_NAME");
8754 a_utf8_string = get_atom_from_string("UTF8_STRING");
8755 a_string = get_atom_from_string("STRING");
8756 a_swm_iconic = get_atom_from_string("_SWM_ICONIC");
8757 a_swm_ws = get_atom_from_string("_SWM_WS");
8764 xcb_atom_t netwmcheck;
8765 xcb_window_t root, win;
8767 /* work around sun jdk bugs, code from wmname */
8768 netwmcheck = get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
8770 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
8771 for (i = 0; i < num_screens; i++) {
8772 root = screens[i].root;
8774 win = xcb_generate_id(conn);
8775 xcb_create_window(conn, XCB_COPY_FROM_PARENT, win, root,
8776 0, 0, 1, 1, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT,
8777 XCB_COPY_FROM_PARENT, 0, NULL);
8779 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root,
8780 netwmcheck, XCB_ATOM_WINDOW, 32, 1, &win);
8781 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win,
8782 netwmcheck, XCB_ATOM_WINDOW, 32, 1, &win);
8783 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win,
8784 a_netwmname, a_utf8_string, 8, strlen("LG3D"), "LG3D");
8789 shutdown_cleanup(void)
8793 /* disable alarm because the following code may not be interrupted */
8795 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
8796 err(1, "can't disable alarm");
8806 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
8807 for (i = 0; i < num_screens; ++i) {
8808 if (screens[i].bar_gc != 0)
8809 xcb_free_gc(conn, screens[i].bar_gc);
8810 if (!bar_font_legacy)
8811 XftColorFree(display, DefaultVisual(display, i),
8812 DefaultColormap(display, i), &bar_font_color);
8815 if (bar_font_legacy)
8816 XFreeFontSet(display, bar_fs);
8818 XftFontClose(display, bar_font);
8821 xcb_key_symbols_free(syms);
8823 xcb_disconnect(conn);
8827 event_error(xcb_generic_error_t *e)
8831 DNPRINTF(SWM_D_EVENT, "event_error: %s(%u) from %s(%u), sequence: %u, "
8832 "resource_id: %u, minor_code: %u\n",
8833 xcb_event_get_error_label(e->error_code), e->error_code,
8834 xcb_event_get_request_label(e->major_code), e->major_code,
8835 e->sequence, e->resource_id, e->minor_code);
8839 event_handle(xcb_generic_event_t *evt)
8841 uint8_t type = XCB_EVENT_RESPONSE_TYPE(evt);
8843 DNPRINTF(SWM_D_EVENT, "XCB Event: %s(%d), seq %u\n",
8844 xcb_event_get_label(XCB_EVENT_RESPONSE_TYPE(evt)),
8845 XCB_EVENT_RESPONSE_TYPE(evt), evt->sequence);
8848 #define EVENT(type, callback) case type: callback((void *)evt); return
8849 EVENT(0, event_error);
8850 EVENT(XCB_BUTTON_PRESS, buttonpress);
8851 /*EVENT(XCB_BUTTON_RELEASE, buttonpress);*/
8852 /*EVENT(XCB_CIRCULATE_NOTIFY, );*/
8853 /*EVENT(XCB_CIRCULATE_REQUEST, );*/
8854 EVENT(XCB_CLIENT_MESSAGE, clientmessage);
8855 /*EVENT(XCB_COLORMAP_NOTIFY, );*/
8856 EVENT(XCB_CONFIGURE_NOTIFY, configurenotify);
8857 EVENT(XCB_CONFIGURE_REQUEST, configurerequest);
8858 /*EVENT(XCB_CREATE_NOTIFY, );*/
8859 EVENT(XCB_DESTROY_NOTIFY, destroynotify);
8860 EVENT(XCB_ENTER_NOTIFY, enternotify);
8861 EVENT(XCB_EXPOSE, expose);
8863 EVENT(XCB_FOCUS_IN, focusin);
8864 EVENT(XCB_FOCUS_OUT, focusout);
8866 /*EVENT(XCB_GRAPHICS_EXPOSURE, );*/
8867 /*EVENT(XCB_GRAVITY_NOTIFY, );*/
8868 EVENT(XCB_KEY_PRESS, keypress);
8869 /*EVENT(XCB_KEY_RELEASE, keypress);*/
8870 /*EVENT(XCB_KEYMAP_NOTIFY, );*/
8872 EVENT(XCB_LEAVE_NOTIFY, leavenotify);
8874 EVENT(XCB_MAP_NOTIFY, mapnotify);
8875 EVENT(XCB_MAP_REQUEST, maprequest);
8876 EVENT(XCB_MAPPING_NOTIFY, mappingnotify);
8877 /*EVENT(XCB_MOTION_NOTIFY, );*/
8878 /*EVENT(XCB_NO_EXPOSURE, );*/
8879 EVENT(XCB_PROPERTY_NOTIFY, propertynotify);
8880 /*EVENT(XCB_REPARENT_NOTIFY, );*/
8881 /*EVENT(XCB_RESIZE_REQUEST, );*/
8882 /*EVENT(XCB_SELECTION_CLEAR, );*/
8883 /*EVENT(XCB_SELECTION_NOTIFY, );*/
8884 /*EVENT(XCB_SELECTION_REQUEST, );*/
8885 EVENT(XCB_UNMAP_NOTIFY, unmapnotify);
8886 /*EVENT(XCB_VISIBILITY_NOTIFY, visibilitynotify);*/
8889 if (type - xrandr_eventbase == XCB_RANDR_SCREEN_CHANGE_NOTIFY)
8890 screenchange((void *)evt);
8894 main(int argc, char *argv[])
8896 struct swm_region *r, *rr;
8897 struct ws_win *winfocus = NULL;
8898 char conf[PATH_MAX], *cfile = NULL;
8900 int xfd, i, num_screens;
8901 struct sigaction sact;
8902 xcb_generic_event_t *evt;
8906 int stdin_ready = 0;
8909 /* suppress unused warning since var is needed */
8912 time_started = time(NULL);
8915 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION, buildstr);
8916 if (!setlocale(LC_CTYPE, "") || !setlocale(LC_TIME, ""))
8917 warnx("no locale support");
8919 /* handle some signals */
8920 bzero(&sact, sizeof(sact));
8921 sigemptyset(&sact.sa_mask);
8923 sact.sa_handler = sighdlr;
8924 sigaction(SIGINT, &sact, NULL);
8925 sigaction(SIGQUIT, &sact, NULL);
8926 sigaction(SIGTERM, &sact, NULL);
8927 sigaction(SIGHUP, &sact, NULL);
8929 sact.sa_handler = sighdlr;
8930 sact.sa_flags = SA_NOCLDSTOP;
8931 sigaction(SIGCHLD, &sact, NULL);
8933 if (!(display = XOpenDisplay(0)))
8934 errx(1, "can not open display");
8936 conn = XGetXCBConnection(display);
8937 if (xcb_connection_has_error(conn))
8938 errx(1, "can not get XCB connection");
8940 XSetEventQueueOwner(display, XCBOwnsEventQueue);
8942 xcb_prefetch_extension_data(conn, &xcb_randr_id);
8943 xfd = xcb_get_file_descriptor(conn);
8945 /* look for local and global conf file */
8946 pwd = getpwuid(getuid());
8948 errx(1, "invalid user: %d", getuid());
8950 xcb_grab_server(conn);
8953 /* flush all events */
8954 while ((evt = xcb_poll_for_event(conn))) {
8955 if (XCB_EVENT_RESPONSE_TYPE(evt) == 0)
8960 if (enable_wm() != 0)
8961 errx(1, "another window manager is currently running");
8963 /* Load Xcursors and/or cursorfont glyph cursors. */
8975 for (i = 0; ; i++) {
8980 snprintf(conf, sizeof conf, "%s/.%s",
8981 pwd->pw_dir, SWM_CONF_FILE);
8985 snprintf(conf, sizeof conf, "/etc/%s",
8990 snprintf(conf, sizeof conf, "%s/.%s",
8991 pwd->pw_dir, SWM_CONF_FILE_OLD);
8995 snprintf(conf, sizeof conf, "/etc/%s",
9002 if (strlen(conf) && stat(conf, &sb) != -1)
9003 if (S_ISREG(sb.st_mode)) {
9010 /* load conf (if any) */
9012 conf_load(cfile, SWM_CONF_DEFAULT);
9015 /* set some values to work around bad programs */
9017 /* grab existing windows (before we build the bars) */
9020 if (getenv("SWM_STARTED") == NULL)
9021 setenv("SWM_STARTED", "YES", 1);
9023 /* setup all bars */
9024 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
9025 for (i = 0; i < num_screens; i++)
9026 TAILQ_FOREACH(r, &screens[i].rl, entry) {
9027 if (winfocus == NULL)
9028 winfocus = TAILQ_FIRST(&r->ws->winlist);
9035 xcb_ungrab_server(conn);
9038 rd_max = xfd > STDIN_FILENO ? xfd : STDIN_FILENO;
9041 while ((evt = xcb_poll_for_event(conn))) {
9048 /* If just (re)started, set default focus if needed. */
9049 if (winfocus && focus_mode != SWM_FOCUS_FOLLOW) {
9050 rr = winfocus->ws->r;
9052 /* not a visible window */
9057 focus_win(get_region_focus(rr));
9064 FD_SET(STDIN_FILENO, &rd);
9068 num_readable = select(rd_max + 1, &rd, NULL, NULL, &tv);
9069 if (num_readable == -1 && errno != EINTR) {
9070 DNPRINTF(SWM_D_MISC, "select failed");
9071 } else if (num_readable > 0 && FD_ISSET(STDIN_FILENO, &rd)) {
9076 restart(NULL, NULL);