2 * Copyright (c) 2009-2012 Marco Peereboom <marco@peereboom.us>
3 * Copyright (c) 2009-2011 Ryan McBride <mcbride@countersiege.com>
4 * Copyright (c) 2009 Darrin Chandler <dwchandler@stilyagin.com>
5 * Copyright (c) 2009 Pierre-Yves Ritschard <pyr@spootnik.org>
6 * Copyright (c) 2010 Tuukka Kataja <stuge@xor.fi>
7 * Copyright (c) 2011 Jason L. Wright <jason@thought.net>
8 * Copyright (c) 2011-2012 Reginald Kennedy <rk@rejii.com>
9 * Copyright (c) 2011-2012 Lawrence Teo <lteo@lteo.net>
10 * Copyright (c) 2011-2012 Tiago Cunha <tcunha@gmx.com>
11 * Copyright (c) 2012 David Hill <dhill@mindcry.org>
13 * Permission to use, copy, modify, and distribute this software for any
14 * purpose with or without fee is hereby granted, provided that the above
15 * copyright notice and this permission notice appear in all copies.
17 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
18 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
20 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
21 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
22 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
23 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 * Much code and ideas taken from dwm under the following license:
27 * MIT/X Consortium License
29 * 2006-2008 Anselm R Garbe <garbeam at gmail dot com>
30 * 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
31 * 2006-2007 Jukka Salmi <jukka at salmi dot ch>
32 * 2007 Premysl Hruby <dfenze at gmail dot com>
33 * 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
34 * 2007 Christof Musik <christof at sendfax dot de>
35 * 2007-2008 Enno Gottox Boland <gottox at s01 dot de>
36 * 2007-2008 Peter Hartlich <sgkkr at hartlich dot com>
37 * 2008 Martin Hurton <martin dot hurton at gmail dot com>
39 * Permission is hereby granted, free of charge, to any person obtaining a
40 * copy of this software and associated documentation files (the "Software"),
41 * to deal in the Software without restriction, including without limitation
42 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
43 * and/or sell copies of the Software, and to permit persons to whom the
44 * Software is furnished to do so, subject to the following conditions:
46 * The above copyright notice and this permission notice shall be included in
47 * all copies or substantial portions of the Software.
49 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
50 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
51 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
52 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
53 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
54 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
55 * DEALINGS IN THE SOFTWARE.
59 #include <sys/types.h>
63 #include <sys/queue.h>
64 #include <sys/param.h>
65 #include <sys/select.h>
66 #if defined(__linux__)
68 #elif defined(__OpenBSD__)
70 #elif defined(__FreeBSD__)
91 #include <X11/cursorfont.h>
92 #include <X11/extensions/Xrandr.h>
93 #include <X11/Xcursor/Xcursor.h>
94 #include <X11/Xft/Xft.h>
95 #include <X11/Xlib-xcb.h>
96 #include <xcb/xcb_atom.h>
97 #include <xcb/xcb_aux.h>
98 #include <xcb/xcb_event.h>
99 #include <xcb/xcb_icccm.h>
100 #include <xcb/xcb_keysyms.h>
101 #include <xcb/xtest.h>
102 #include <xcb/randr.h>
110 #ifdef SPECTRWM_BUILDSTR
111 static const char *buildstr = SPECTRWM_BUILDSTR;
113 static const char *buildstr = SPECTRWM_VERSION;
116 #if !defined(__CYGWIN__) /* cygwin chokes on xrandr stuff */
118 # error XRandR versions less than 1.0 are not supported
121 # if RANDR_MAJOR >= 1
122 # if RANDR_MINOR >= 2
123 # define SWM_XRR_HAS_CRTC
126 #endif /* __CYGWIN__ */
128 #ifndef XCB_ICCCM_NUM_WM_HINTS_ELEMENTS
129 #define XCB_ICCCM_SIZE_HINT_P_MIN_SIZE XCB_SIZE_HINT_P_MIN_SIZE
130 #define XCB_ICCCM_SIZE_HINT_P_MAX_SIZE XCB_SIZE_HINT_P_MAX_SIZE
131 #define XCB_ICCCM_SIZE_HINT_P_RESIZE_INC XCB_SIZE_HINT_P_RESIZE_INC
132 #define XCB_ICCCM_WM_HINT_INPUT XCB_WM_HINT_INPUT
133 #define XCB_ICCCM_WM_HINT_X_URGENCY XCB_WM_HINT_X_URGENCY
134 #define XCB_ICCCM_WM_STATE_ICONIC XCB_WM_STATE_ICONIC
135 #define XCB_ICCCM_WM_STATE_WITHDRAWN XCB_WM_STATE_WITHDRAWN
136 #define XCB_ICCCM_WM_STATE_NORMAL XCB_WM_STATE_NORMAL
137 #define xcb_icccm_get_text_property_reply_t xcb_get_text_property_reply_t
138 #define xcb_icccm_get_text_property_reply_wipe xcb_get_text_property_reply_wipe
139 #define xcb_icccm_get_wm_class xcb_get_wm_class
140 #define xcb_icccm_get_wm_class_reply xcb_get_wm_class_reply
141 #define xcb_icccm_get_wm_class_reply_t xcb_get_wm_class_reply_t
142 #define xcb_icccm_get_wm_class_reply_wipe xcb_get_wm_class_reply_wipe
143 #define xcb_icccm_get_wm_hints xcb_get_wm_hints
144 #define xcb_icccm_get_wm_hints_reply xcb_get_wm_hints_reply
145 #define xcb_icccm_get_wm_name xcb_get_wm_name
146 #define xcb_icccm_get_wm_name_reply xcb_get_wm_name_reply
147 #define xcb_icccm_get_wm_normal_hints xcb_get_wm_normal_hints
148 #define xcb_icccm_get_wm_normal_hints_reply xcb_get_wm_normal_hints_reply
149 #define xcb_icccm_get_wm_protocols xcb_get_wm_protocols
150 #define xcb_icccm_get_wm_protocols_reply xcb_get_wm_protocols_reply
151 #define xcb_icccm_get_wm_protocols_reply_t xcb_get_wm_protocols_reply_t
152 #define xcb_icccm_get_wm_protocols_reply_wipe xcb_get_wm_protocols_reply_wipe
153 #define xcb_icccm_get_wm_transient_for xcb_get_wm_transient_for
154 #define xcb_icccm_get_wm_transient_for_reply xcb_get_wm_transient_for_reply
155 #define xcb_icccm_wm_hints_t xcb_wm_hints_t
158 /*#define SWM_DEBUG*/
160 #define DPRINTF(x...) do { \
162 fprintf(stderr, x); \
164 #define DNPRINTF(n,x...) do { \
165 if (swm_debug & n) { \
166 fprintf(stderr, "%ld ", (long)(time(NULL) - time_started)); \
167 fprintf(stderr, x); \
170 #define SWM_D_MISC 0x0001
171 #define SWM_D_EVENT 0x0002
172 #define SWM_D_WS 0x0004
173 #define SWM_D_FOCUS 0x0008
174 #define SWM_D_MOVE 0x0010
175 #define SWM_D_STACK 0x0020
176 #define SWM_D_MOUSE 0x0040
177 #define SWM_D_PROP 0x0080
178 #define SWM_D_CLASS 0x0100
179 #define SWM_D_KEY 0x0200
180 #define SWM_D_QUIRK 0x0400
181 #define SWM_D_SPAWN 0x0800
182 #define SWM_D_EVENTQ 0x1000
183 #define SWM_D_CONF 0x2000
184 #define SWM_D_BAR 0x4000
185 #define SWM_D_INIT 0x8000
187 u_int32_t swm_debug = 0
206 #define DPRINTF(x...)
207 #define DNPRINTF(n,x...)
210 /* convert 8-bit to 16-bit */
211 #define RGB_8_TO_16(col) ((col) << 8) + (col)
213 #define PIXEL_TO_XRENDERCOLOR(px, xrc) \
214 xrc.red = RGB_8_TO_16((px) >> 16 & 0xff); \
215 xrc.green = RGB_8_TO_16((px) >> 8 & 0xff); \
216 xrc.blue = RGB_8_TO_16((px) & 0xff); \
219 #define LENGTH(x) (int)(sizeof (x) / sizeof (x)[0])
220 #define MODKEY XCB_MOD_MASK_1
221 #define CLEANMASK(mask) ((mask) & ~(numlockmask | XCB_MOD_MASK_LOCK))
222 #define BUTTONMASK (XCB_EVENT_MASK_BUTTON_PRESS | \
223 XCB_EVENT_MASK_BUTTON_RELEASE)
224 #define MOUSEMASK (BUTTONMASK|XCB_EVENT_MASK_POINTER_MOTION)
225 #define SWM_PROPLEN (16)
226 #define SWM_FUNCNAME_LEN (32)
227 #define SWM_KEYS_LEN (255)
228 #define SWM_QUIRK_LEN (64)
229 #define X(r) (r)->g.x
230 #define Y(r) (r)->g.y
231 #define WIDTH(r) (r)->g.w
232 #define HEIGHT(r) (r)->g.h
233 #define BORDER(w) ((w)->bordered ? border_width : 0)
234 #define MAX_X(r) ((r)->g.x + (r)->g.w)
235 #define MAX_Y(r) ((r)->g.y + (r)->g.h)
236 #define SH_MIN(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE
237 #define SH_MIN_W(w) (w)->sh.min_width
238 #define SH_MIN_H(w) (w)->sh.min_height
239 #define SH_MAX(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MAX_SIZE
240 #define SH_MAX_W(w) (w)->sh.max_width
241 #define SH_MAX_H(w) (w)->sh.max_height
242 #define SH_INC(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_RESIZE_INC
243 #define SH_INC_W(w) (w)->sh.width_inc
244 #define SH_INC_H(w) (w)->sh.height_inc
245 #define SWM_MAX_FONT_STEPS (3)
246 #define WINID(w) ((w) ? (w)->id : 0)
247 #define YESNO(x) ((x) ? "yes" : "no")
249 #define SWM_FOCUS_DEFAULT (0)
250 #define SWM_FOCUS_FOLLOW (1)
251 #define SWM_FOCUS_MANUAL (2)
253 #define SWM_CK_NONE 0
254 #define SWM_CK_ALL 0x7
255 #define SWM_CK_FOCUS 0x1
256 #define SWM_CK_POINTER 0x2
257 #define SWM_CK_FALLBACK 0x4
259 #define SWM_CONF_DEFAULT (0)
260 #define SWM_CONF_KEYMAPPING (1)
263 #define SWM_LIB "/usr/local/lib/libswmhack.so"
270 xcb_atom_t a_takefocus;
272 xcb_atom_t a_netwmname;
273 xcb_atom_t a_utf8_string;
275 xcb_atom_t a_swm_iconic;
277 volatile sig_atomic_t running = 1;
278 volatile sig_atomic_t restart_wm = 0;
279 xcb_timestamp_t last_event_time = 0;
284 int xrandr_eventbase;
285 unsigned int numlockmask = 0;
288 xcb_connection_t *conn;
289 xcb_key_symbols_t *syms;
292 int cycle_visible = 0;
294 int font_adjusted = 0;
295 unsigned int mod_key = MODKEY;
298 struct swm_region *search_r;
299 int select_list_pipe[2];
300 int select_resp_pipe[2];
302 volatile sig_atomic_t search_resp;
303 int search_resp_action;
305 struct search_window {
306 TAILQ_ENTRY(search_window) entry;
310 xcb_window_t indicator;
312 TAILQ_HEAD(search_winlist, search_window);
313 struct search_winlist search_wl;
318 SWM_SEARCH_UNICONIFY,
319 SWM_SEARCH_NAME_WORKSPACE,
320 SWM_SEARCH_SEARCH_WORKSPACE,
321 SWM_SEARCH_SEARCH_WINDOW
324 #define SWM_STACK_TOP (0)
325 #define SWM_STACK_BOTTOM (1)
326 #define SWM_STACK_ABOVE (2)
327 #define SWM_STACK_BELOW (3)
330 double dialog_ratio = 0.6;
332 #define SWM_BAR_MAX (256)
333 #define SWM_BAR_JUSTIFY_LEFT (0)
334 #define SWM_BAR_JUSTIFY_CENTER (1)
335 #define SWM_BAR_JUSTIFY_RIGHT (2)
336 #define SWM_BAR_OFFSET (4)
337 #define SWM_BAR_FONTS "-*-terminus-medium-*-*-*-12-*-*-*-*-*-*-*," \
338 "-*-profont-*-*-*-*-12-*-*-*-*-*-*-*," \
339 "-*-times-medium-r-*-*-12-*-*-*-*-*-*-*," \
340 "-misc-fixed-medium-r-*-*-12-*-*-*-*-*-*-*," \
341 "-*-*-*-r-*-*-*-*-*-*-*-*-*-*"
343 #ifdef X_HAVE_UTF8_STRING
344 #define DRAWSTRING(x...) Xutf8DrawString(x)
346 #define DRAWSTRING(x...) XmbDrawString(x)
349 char *bar_argv[] = { NULL, NULL };
351 char bar_ext[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_stop(void);
824 void bar_fmt(const char *, char *, struct swm_region *, size_t);
825 void bar_fmt_expand(char *, size_t);
826 void bar_fmt_print(void);
827 void bar_print(struct swm_region *, const char *);
828 void bar_print_legacy(struct swm_region *, const char *);
829 void bar_refresh(void);
830 void bar_replace(char *, char *, struct swm_region *, size_t);
831 void bar_replace_pad(char *, int *, size_t);
832 char * bar_replace_seq(char *, char *, struct swm_region *, size_t *,
834 void bar_setup(struct swm_region *);
835 void bar_title_name(char *, size_t, struct swm_region *);
836 void bar_toggle(struct swm_region *, union arg *);
837 void bar_update(void);
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");
2166 char fmtexp[SWM_BAR_MAX], fmtnew[SWM_BAR_MAX];
2167 char fmtrep[SWM_BAR_MAX];
2169 struct swm_region *r;
2171 /* expand the format by first passing it through strftime(3) */
2172 bar_fmt_expand(fmtexp, sizeof fmtexp);
2174 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2175 for (i = 0; i < num_screens; i++) {
2176 TAILQ_FOREACH(r, &screens[i].rl, entry) {
2180 if (r->ws->bar_enabled)
2181 xcb_map_window(conn, r->bar->id);
2183 xcb_unmap_window(conn, r->bar->id);
2187 bar_fmt(fmtexp, fmtnew, r, sizeof fmtnew);
2188 bar_replace(fmtnew, fmtrep, r, sizeof fmtrep);
2189 if (bar_font_legacy)
2190 bar_print_legacy(r, fmtrep);
2192 bar_print(r, fmtrep);
2203 if (bar_enabled && bar_extra && bar_extra_running) {
2204 /* Ignore short reads; it'll correct itself. */
2205 while ((b = fgetln(stdin, &len)) != NULL)
2206 if (b && b[len - 1] == '\n') {
2208 strlcpy(bar_ext, b, sizeof bar_ext);
2210 if (b == NULL && errno != EAGAIN) {
2211 warn("bar_update: bar_extra failed");
2216 * Attempt to drain stdin, so it doesn't cause the main loop to
2217 * call us as fast as it can.
2219 fgetln(stdin, &len);
2231 bar_toggle(struct swm_region *r, union arg *args)
2233 struct swm_region *tmpr;
2236 /* suppress unused warnings since vars are needed */
2240 DNPRINTF(SWM_D_BAR, "bar_toggle\n");
2243 case SWM_ARG_ID_BAR_TOGGLE_WS:
2244 /* Only change if master switch is enabled. */
2246 r->ws->bar_enabled = !r->ws->bar_enabled;
2248 bar_enabled = r->ws->bar_enabled = 1;
2250 case SWM_ARG_ID_BAR_TOGGLE:
2251 bar_enabled = !bar_enabled;
2255 /* update bars as necessary */
2256 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2257 for (i = 0; i < num_screens; i++)
2258 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
2260 if (bar_enabled && tmpr->ws->bar_enabled)
2261 xcb_map_window(conn, tmpr->bar->id);
2263 xcb_unmap_window(conn, tmpr->bar->id);
2268 /* must be after stack */
2277 struct swm_region *r;
2281 /* do this here because the conf file is in memory */
2282 if (bar_extra && !bar_extra_running && bar_argv[0]) {
2283 /* launch external status app */
2284 bar_extra_running = 1;
2285 if (pipe(bar_pipe) == -1)
2286 err(1, "pipe error");
2287 socket_setnonblock(bar_pipe[0]);
2288 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
2290 /* Set stdin to read from the pipe. */
2291 if (dup2(bar_pipe[0], 0) == -1)
2294 /* Set stdout to write to the pipe. */
2295 if (dup2(bar_pipe[1], 1) == -1)
2298 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
2299 err(1, "could not disable SIGPIPE");
2300 switch (bar_pid = fork()) {
2302 err(1, "cannot fork");
2306 execvp(bar_argv[0], bar_argv);
2307 err(1, "%s external app failed", bar_argv[0]);
2309 default: /* parent */
2315 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2316 for (i = 0; i < num_screens; i++)
2317 TAILQ_FOREACH(r, &screens[i].rl, entry) {
2320 wa[0] = screens[i].c[SWM_S_COLOR_BAR].pixel;
2321 wa[1] = screens[i].c[SWM_S_COLOR_BAR_BORDER].pixel;
2322 xcb_change_window_attributes(conn, r->bar->id,
2323 XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL, wa);
2335 while ((s = index(s, '-'))) {
2340 return (count == 14);
2346 char *default_string;
2347 char **missing_charsets;
2348 int num_missing_charsets = 0;
2352 XFreeFontSet(display, bar_fs);
2356 DNPRINTF(SWM_D_INIT, "fontset_init: loading bar_fonts: %s\n", bar_fonts);
2358 bar_fs = XCreateFontSet(display, bar_fonts, &missing_charsets,
2359 &num_missing_charsets, &default_string);
2361 if (num_missing_charsets > 0) {
2362 warnx("Unable to load charset(s):");
2364 for (i = 0; i < num_missing_charsets; ++i)
2365 warnx("%s", missing_charsets[i]);
2367 XFreeStringList(missing_charsets);
2369 if (strcmp(default_string, ""))
2370 warnx("Glyphs from those sets will be replaced "
2371 "by '%s'.", default_string);
2373 warnx("Glyphs from those sets won't be drawn.");
2377 errx(1, "Error creating font set structure.");
2379 bar_fs_extents = XExtentsOfFontSet(bar_fs);
2381 bar_height = bar_fs_extents->max_logical_extent.height +
2382 2 * bar_border_width;
2389 xft_init(struct swm_region *r)
2391 char *font, *d, *search;
2394 if (bar_font == NULL) {
2395 if ((d = strdup(bar_fonts)) == NULL)
2396 errx(1, "insufficient memory.");
2398 while ((font = strsep(&search, ",")) != NULL) {
2402 DNPRINTF(SWM_D_INIT, "xft_init: try font %s\n", font);
2405 bar_font = XftFontOpenXlfd(display, r->s->idx,
2408 bar_font = XftFontOpenName(display, r->s->idx,
2413 warnx("unable to load font %s", font);
2416 DNPRINTF(SWM_D_INIT, "successfully opened "
2424 if (bar_font == NULL)
2425 errx(1, "unable to open a font");
2427 PIXEL_TO_XRENDERCOLOR(r->s->c[SWM_S_COLOR_BAR_FONT].pixel, color);
2429 if (!XftColorAllocValue(display, DefaultVisual(display, r->s->idx),
2430 DefaultColormap(display, r->s->idx), &color, &bar_font_color))
2431 warn("Xft error: unable to allocate color.");
2433 bar_height = bar_font->height + 2 * bar_border_width;
2440 bar_setup(struct swm_region *r)
2442 xcb_screen_t *screen;
2445 DNPRINTF(SWM_D_BAR, "bar_setup: screen %d.\n",
2448 if ((screen = get_screen(r->s->idx)) == NULL)
2449 errx(1, "ERROR: can't get screen %d.", r->s->idx);
2454 if ((r->bar = calloc(1, sizeof(struct swm_bar))) == NULL)
2455 err(1, "bar_setup: calloc: failed to allocate memory.");
2457 if (bar_font_legacy)
2463 Y(r->bar) = bar_at_bottom ? (Y(r) + HEIGHT(r) - bar_height) : Y(r);
2464 WIDTH(r->bar) = WIDTH(r) - 2 * bar_border_width;
2465 HEIGHT(r->bar) = bar_height - 2 * bar_border_width;
2467 r->bar->id = xcb_generate_id(conn);
2468 wa[0] = r->s->c[SWM_S_COLOR_BAR].pixel;
2469 wa[1] = r->s->c[SWM_S_COLOR_BAR_BORDER].pixel;
2470 wa[2] = XCB_EVENT_MASK_EXPOSURE;
2472 xcb_create_window(conn, XCB_COPY_FROM_PARENT, r->bar->id, r->s->root,
2473 X(r->bar), Y(r->bar), WIDTH(r->bar), HEIGHT(r->bar),
2474 bar_border_width, XCB_WINDOW_CLASS_INPUT_OUTPUT,
2475 XCB_COPY_FROM_PARENT, XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL
2476 | XCB_CW_EVENT_MASK, wa);
2478 r->bar->buffer = xcb_generate_id(conn);
2479 xcb_create_pixmap(conn, screen->root_depth, r->bar->buffer, r->bar->id,
2480 WIDTH(r->bar), HEIGHT(r->bar));
2483 xcb_randr_select_input(conn, r->bar->id,
2484 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE);
2487 xcb_map_window(conn, r->bar->id);
2489 DNPRINTF(SWM_D_BAR, "bar_setup: window: 0x%x, (x,y) w x h: (%d,%d) "
2490 "%d x %d\n", WINID(r->bar), X(r->bar), Y(r->bar), WIDTH(r->bar),
2497 bar_cleanup(struct swm_region *r)
2501 xcb_destroy_window(conn, r->bar->id);
2502 xcb_free_pixmap(conn, r->bar->buffer);
2508 set_win_state(struct ws_win *win, uint16_t state)
2510 uint16_t data[2] = { state, XCB_ATOM_NONE };
2512 DNPRINTF(SWM_D_EVENT, "set_win_state: window: 0x%x, state: %u\n",
2518 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id, a_state,
2519 a_state, 32, 2, data);
2523 getstate(xcb_window_t w)
2525 uint32_t result = 0;
2526 xcb_get_property_cookie_t c;
2527 xcb_get_property_reply_t *r;
2529 c = xcb_get_property(conn, 0, w, a_state, a_state, 0L, 2L);
2530 r = xcb_get_property_reply(conn, c, NULL);
2533 if (r->type == a_state && r->format == 32 && r->length == 2)
2534 result = *((uint32_t *)xcb_get_property_value(r));
2538 DNPRINTF(SWM_D_MISC, "getstate property: win 0x%x state %u\n", w,
2544 version(struct swm_region *r, union arg *args)
2546 /* suppress unused warnings since vars are needed */
2550 bar_version = !bar_version;
2552 snprintf(bar_vertext, sizeof bar_vertext,
2553 "Version: %s Build: %s", SPECTRWM_VERSION, buildstr);
2555 strlcpy(bar_vertext, "", sizeof bar_vertext);
2562 client_msg(struct ws_win *win, xcb_atom_t a, xcb_timestamp_t t)
2564 xcb_client_message_event_t ev;
2572 name = get_atom_name(a);
2573 DNPRINTF(SWM_D_EVENT, "client_msg: window: 0x%x, atom: %s(%u), "
2575 win->id, name, a, t);
2579 bzero(&ev, sizeof ev);
2580 ev.response_type = XCB_CLIENT_MESSAGE;
2581 ev.window = win->id;
2584 ev.data.data32[0] = a;
2585 ev.data.data32[1] = t;
2587 xcb_send_event(conn, 0, win->id,
2588 XCB_EVENT_MASK_NO_EVENT, (const char *)&ev);
2591 /* synthetic response to a ConfigureRequest when not making a change */
2593 config_win(struct ws_win *win, xcb_configure_request_event_t *ev)
2595 xcb_configure_notify_event_t ce;
2600 /* send notification of unchanged state. */
2601 bzero(&ce, sizeof(ce));
2602 ce.response_type = XCB_CONFIGURE_NOTIFY;
2605 ce.width = WIDTH(win);
2606 ce.height = HEIGHT(win);
2607 ce.override_redirect = 0;
2612 ce.window = win->id;
2613 ce.border_width = BORDER(win);
2614 ce.above_sibling = XCB_WINDOW_NONE;
2617 ce.event = ev->window;
2618 ce.window = ev->window;
2620 /* make response appear more WM_SIZE_HINTS-compliant */
2621 if (win->sh.flags) {
2622 DNPRINTF(SWM_D_MISC, "config_win: hints: window: 0x%x,"
2623 " sh.flags: %u, min: %d x %d, max: %d x %d, inc: "
2624 "%d x %d\n", win->id, win->sh.flags, SH_MIN_W(win),
2625 SH_MIN_H(win), SH_MAX_W(win), SH_MAX_H(win),
2626 SH_INC_W(win), SH_INC_H(win));
2631 /* the hint may be set... to 0! */
2632 if (SH_MIN_W(win) > 0 && ce.width < SH_MIN_W(win))
2633 ce.width = SH_MIN_W(win);
2634 if (SH_MIN_H(win) > 0 && ce.height < SH_MIN_H(win))
2635 ce.height = SH_MIN_H(win);
2640 /* may also be advertized as 0 */
2641 if (SH_MAX_W(win) > 0 && ce.width > SH_MAX_W(win))
2642 ce.width = SH_MAX_W(win);
2643 if (SH_MAX_H(win) > 0 && ce.height > SH_MAX_H(win))
2644 ce.height = SH_MAX_H(win);
2647 /* resize increment. */
2649 if (SH_INC_W(win) > 1 && ce.width > SH_INC_W(win))
2650 ce.width -= (ce.width - SH_MIN_W(win)) %
2652 if (SH_INC_H(win) > 1 && ce.height > SH_INC_H(win))
2653 ce.height -= (ce.height - SH_MIN_H(win)) %
2657 /* adjust x and y for requested border_width. */
2658 ce.x += BORDER(win) - ev->border_width;
2659 ce.y += BORDER(win) - ev->border_width;
2660 ce.border_width = ev->border_width;
2661 ce.above_sibling = ev->sibling;
2664 DNPRINTF(SWM_D_MISC, "config_win: ewmh: %s, window: 0x%x, (x,y) w x h: "
2665 "(%d,%d) %d x %d, border: %d\n", YESNO(ev == NULL), win->id, ce.x,
2666 ce.y, ce.width, ce.height, ce.border_width);
2668 xcb_send_event(conn, 0, win->id, XCB_EVENT_MASK_STRUCTURE_NOTIFY,
2673 count_win(struct workspace *ws, int count_transient)
2678 TAILQ_FOREACH(win, &ws->winlist, entry) {
2679 if (!count_transient && win->floating)
2681 if (!count_transient && win->transient)
2687 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
2693 quit(struct swm_region *r, union arg *args)
2695 /* suppress unused warnings since vars are needed */
2699 DNPRINTF(SWM_D_MISC, "quit\n");
2704 map_window(struct ws_win *win)
2706 uint32_t val = XCB_STACK_MODE_ABOVE;
2711 DNPRINTF(SWM_D_EVENT, "map_window: win 0x%x, mapped: %s\n", win->id,
2712 YESNO(win->mapped));
2714 xcb_configure_window(conn, win->id,
2715 XCB_CONFIG_WINDOW_STACK_MODE, &val);
2720 xcb_map_window(conn, win->id);
2721 set_win_state(win, XCB_ICCCM_WM_STATE_NORMAL);
2726 unmap_window(struct ws_win *win)
2731 DNPRINTF(SWM_D_EVENT, "unmap_window: win 0x%x, mapped: %s\n", win->id,
2732 YESNO(win->mapped));
2737 xcb_unmap_window(conn, win->id);
2738 set_win_state(win, XCB_ICCCM_WM_STATE_ICONIC);
2746 int i, j, num_screens;
2748 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2749 for (i = 0; i < num_screens; i++)
2750 for (j = 0; j < workspace_limit; j++)
2751 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2756 fake_keypress(struct ws_win *win, xcb_keysym_t keysym, uint16_t modifiers)
2758 xcb_key_press_event_t event;
2759 xcb_keycode_t *keycode;
2764 keycode = xcb_key_symbols_get_keycode(syms, keysym);
2766 DNPRINTF(SWM_D_MISC, "fake_keypress: win 0x%x keycode %u\n",
2769 bzero(&event, sizeof(event));
2770 event.event = win->id;
2771 event.root = win->s->root;
2772 event.child = XCB_WINDOW_NONE;
2773 event.time = XCB_CURRENT_TIME;
2774 event.event_x = X(win);
2775 event.event_y = Y(win);
2778 event.same_screen = 1;
2779 event.detail = *keycode;
2780 event.state = modifiers;
2782 event.response_type = XCB_KEY_PRESS;
2783 xcb_send_event(conn, 1, win->id,
2784 XCB_EVENT_MASK_KEY_PRESS, (const char *)&event);
2786 event.response_type = XCB_KEY_RELEASE;
2787 xcb_send_event(conn, 1, win->id,
2788 XCB_EVENT_MASK_KEY_RELEASE, (const char *)&event);
2794 restart(struct swm_region *r, union arg *args)
2796 /* suppress unused warning since var is needed */
2800 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
2804 execvp(start_argv[0], start_argv);
2805 warn("execvp failed");
2810 get_pointer_win(xcb_window_t root)
2812 struct ws_win *win = NULL;
2813 xcb_query_pointer_reply_t *r;
2815 DNPRINTF(SWM_D_EVENT, "get_pointer_win: root: 0x%x.\n", root);
2817 r = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, root), NULL);
2819 win = find_window(r->child);
2821 DNPRINTF(SWM_D_EVENT, "get_pointer_win: 0x%x.\n",
2824 DNPRINTF(SWM_D_EVENT, "get_pointer_win: none.\n");
2833 root_to_region(xcb_window_t root, int check)
2836 struct swm_region *r = NULL;
2838 xcb_query_pointer_reply_t *qpr;
2839 xcb_get_input_focus_reply_t *gifr;
2841 DNPRINTF(SWM_D_MISC, "root_to_region: window: 0x%x\n", root);
2843 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2844 for (i = 0; i < num_screens; i++)
2845 if (screens[i].root == root)
2848 if (check & SWM_CK_FOCUS) {
2849 /* Try to find an actively focused window */
2850 gifr = xcb_get_input_focus_reply(conn,
2851 xcb_get_input_focus(conn), NULL);
2853 cfw = find_window(gifr->focus);
2854 if (cfw && cfw->ws->r)
2861 if (r == NULL && check & SWM_CK_POINTER) {
2862 /* No region with an active focus; try to use pointer. */
2863 qpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn,
2864 screens[i].root), NULL);
2867 DNPRINTF(SWM_D_MISC, "root_to_region: pointer: "
2868 "(%d,%d)\n", qpr->root_x, qpr->root_y);
2869 TAILQ_FOREACH(r, &screens[i].rl, entry)
2870 if (X(r) <= qpr->root_x &&
2871 qpr->root_x < MAX_X(r) &&
2872 Y(r) <= qpr->root_y &&
2873 qpr->root_y < MAX_Y(r))
2880 if (r == NULL && check & SWM_CK_FALLBACK)
2881 r = TAILQ_FIRST(&screens[i].rl);
2887 find_unmanaged_window(xcb_window_t id)
2890 int i, j, num_screens;
2892 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2893 for (i = 0; i < num_screens; i++)
2894 for (j = 0; j < workspace_limit; j++)
2895 TAILQ_FOREACH(win, &screens[i].ws[j].unmanagedlist,
2903 find_window(xcb_window_t id)
2906 int i, j, num_screens;
2907 xcb_query_tree_reply_t *r;
2909 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2910 for (i = 0; i < num_screens; i++)
2911 for (j = 0; j < workspace_limit; j++)
2912 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2916 r = xcb_query_tree_reply(conn, xcb_query_tree(conn, id), NULL);
2920 /* if we were looking for the parent return that window instead */
2921 if (r->parent == 0 || r->root == r->parent) {
2926 /* look for parent */
2927 for (i = 0; i < num_screens; i++)
2928 for (j = 0; j < workspace_limit; j++)
2929 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2930 if (r->parent == win->id) {
2940 spawn(int ws_idx, union arg *args, int close_fd)
2945 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
2947 close(xcb_get_file_descriptor(conn));
2949 setenv("LD_PRELOAD", SWM_LIB, 1);
2951 if (asprintf(&ret, "%d", ws_idx) == -1) {
2952 warn("spawn: asprintf SWM_WS");
2955 setenv("_SWM_WS", ret, 1);
2959 if (asprintf(&ret, "%d", getpid()) == -1) {
2960 warn("spawn: asprintf _SWM_PID");
2963 setenv("_SWM_PID", ret, 1);
2967 if (setsid() == -1) {
2968 warn("spawn: setsid");
2974 * close stdin and stdout to prevent interaction between apps
2975 * and the baraction script
2976 * leave stderr open to record errors
2978 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) == -1) {
2979 warn("spawn: open");
2982 dup2(fd, STDIN_FILENO);
2983 dup2(fd, STDOUT_FILENO);
2988 execvp(args->argv[0], args->argv);
2990 warn("spawn: execvp");
2995 kill_refs(struct ws_win *win)
2997 int i, x, num_screens;
2998 struct swm_region *r;
2999 struct workspace *ws;
3004 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
3005 for (i = 0; i < num_screens; i++)
3006 TAILQ_FOREACH(r, &screens[i].rl, entry)
3007 for (x = 0; x < workspace_limit; x++) {
3009 if (win == ws->focus)
3011 if (win == ws->focus_prev)
3012 ws->focus_prev = NULL;
3017 validate_win(struct ws_win *testwin)
3020 struct workspace *ws;
3021 struct swm_region *r;
3022 int i, x, num_screens;
3024 if (testwin == NULL)
3027 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
3028 for (i = 0; i < num_screens; i++)
3029 TAILQ_FOREACH(r, &screens[i].rl, entry)
3030 for (x = 0; x < workspace_limit; x++) {
3032 TAILQ_FOREACH(win, &ws->winlist, entry)
3040 validate_ws(struct workspace *testws)
3042 struct swm_region *r;
3043 struct workspace *ws;
3044 int i, x, num_screens;
3046 /* validate all ws */
3047 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
3048 for (i = 0; i < num_screens; i++)
3049 TAILQ_FOREACH(r, &screens[i].rl, entry)
3050 for (x = 0; x < workspace_limit; x++) {
3059 unfocus_win(struct ws_win *win)
3061 xcb_window_t none = XCB_WINDOW_NONE;
3063 DNPRINTF(SWM_D_FOCUS, "unfocus_win: window: 0x%x\n", WINID(win));
3067 if (win->ws == NULL) {
3068 DNPRINTF(SWM_D_FOCUS, "unfocus_win: NULL ws.\n");
3072 if (validate_ws(win->ws)) {
3073 DNPRINTF(SWM_D_FOCUS, "unfocus_win: invalid ws.\n");
3077 if (win->ws->r == NULL) {
3078 DNPRINTF(SWM_D_FOCUS, "unfocus_win: NULL region.\n");
3082 if (validate_win(win)) {
3083 DNPRINTF(SWM_D_FOCUS, "unfocus_win: invalid win.\n");
3088 if (win->ws->focus == win) {
3089 win->ws->focus = NULL;
3090 win->ws->focus_prev = win;
3093 if (validate_win(win->ws->focus)) {
3094 kill_refs(win->ws->focus);
3095 win->ws->focus = NULL;
3098 if (validate_win(win->ws->focus_prev)) {
3099 kill_refs(win->ws->focus_prev);
3100 win->ws->focus_prev = NULL;
3103 xcb_change_window_attributes(conn, win->id, XCB_CW_BORDER_PIXEL,
3104 &win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].pixel);
3106 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
3107 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1, &none);
3109 DNPRINTF(SWM_D_FOCUS, "unfocus_win: done.\n");
3113 focus_win(struct ws_win *win)
3115 struct ws_win *cfw = NULL, *parent = NULL, *w;
3116 struct workspace *ws;
3117 xcb_get_input_focus_reply_t *r;
3119 DNPRINTF(SWM_D_FOCUS, "focus_win: window: 0x%x\n", WINID(win));
3124 if (win->ws == NULL)
3129 if (validate_ws(ws))
3132 if (validate_win(win)) {
3137 r = xcb_get_input_focus_reply(conn, xcb_get_input_focus(conn), NULL);
3139 cfw = find_window(r->focus);
3145 if (ws->focus != win) {
3146 if (ws->focus && ws->focus != cfw)
3147 unfocus_win(ws->focus);
3151 /* If this window directs focus to a child window, then clear. */
3152 if (win->focus_child)
3153 win->focus_child = NULL;
3155 /* If transient, adjust parent's focus child for focus_magic. */
3156 if (win->transient) {
3157 parent = find_window(win->transient);
3158 if (parent && parent->focus_child != win)
3159 parent->focus_child = win;
3162 if (cfw != win && ws->r != NULL) {
3163 /* Set input focus if no input hint, or indicated by hint. */
3164 if (!(win->hints.flags & XCB_ICCCM_WM_HINT_INPUT) ||
3165 (win->hints.flags & XCB_ICCCM_WM_HINT_INPUT &&
3167 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT,
3168 win->id, last_event_time);
3170 /* Tell app it can adjust focus to a specific window. */
3171 if (win->take_focus) {
3172 /* java is special; always tell parent */
3173 if (win->transient && win->java)
3174 client_msg(parent, a_takefocus,
3177 client_msg(win, a_takefocus, last_event_time);
3180 xcb_change_window_attributes(conn, win->id, XCB_CW_BORDER_PIXEL,
3181 &ws->r->s->c[SWM_S_COLOR_FOCUS].pixel);
3183 if (ws->cur_layout->flags & SWM_L_MAPONFOCUS ||
3185 /* If a parent exists, map it first. */
3189 /* Map siblings next. */
3190 TAILQ_FOREACH(w, &ws->winlist, entry)
3191 if (w != win && !w->iconic &&
3192 w->transient == parent->id)
3196 /* Map focused window. */
3199 /* Finally, map children of focus window. */
3200 TAILQ_FOREACH(w, &ws->winlist, entry)
3201 if (w->transient == win->id && !w->iconic)
3206 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
3207 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1,
3214 DNPRINTF(SWM_D_FOCUS, "focus_win: done.\n");
3217 /* If a child window should have focus instead, return it. */
3219 get_focus_magic(struct ws_win *win)
3221 struct ws_win *parent = NULL;
3222 struct ws_win *child = NULL;
3224 DNPRINTF(SWM_D_FOCUS, "get_focus_magic: window: 0x%x\n", WINID(win));
3228 if (win->transient) {
3229 parent = find_window(win->transient);
3231 /* If parent prefers focus elsewhere, then try to do so. */
3232 if (parent && (child = parent->focus_child)) {
3233 if (validate_win(child) == 0 && child->mapped)
3236 parent->focus_child = NULL;
3240 /* If this window prefers focus elsewhere, then try to do so. */
3241 if ((child = win->focus_child)) {
3242 if (validate_win(child) == 0 && child->mapped)
3245 win->focus_child = NULL;
3252 event_drain(uint8_t rt)
3254 xcb_generic_event_t *evt;
3256 /* ensure all pending requests have been processed before filtering. */
3258 while ((evt = xcb_poll_for_event(conn))) {
3259 if (XCB_EVENT_RESPONSE_TYPE(evt) != rt)
3267 switchws(struct swm_region *r, union arg *args)
3269 int wsid = args->id, unmap_old = 0;
3270 struct swm_region *this_r, *other_r;
3272 struct workspace *new_ws, *old_ws;
3277 if (wsid >= workspace_limit)
3281 old_ws = this_r->ws;
3282 new_ws = &this_r->s->ws[wsid];
3284 DNPRINTF(SWM_D_WS, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
3285 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), old_ws->idx, wsid);
3287 if (new_ws == NULL || old_ws == NULL)
3289 if (new_ws == old_ws)
3292 unfocus_win(old_ws->focus);
3294 other_r = new_ws->r;
3295 if (other_r == NULL) {
3296 /* the other workspace is hidden, hide this one */
3300 /* the other ws is visible in another region, exchange them */
3301 other_r->ws_prior = new_ws;
3302 other_r->ws = old_ws;
3303 old_ws->r = other_r;
3305 this_r->ws_prior = old_ws;
3306 this_r->ws = new_ws;
3309 /* Set focus_pending before stacking. */
3310 if (focus_mode != SWM_FOCUS_FOLLOW)
3311 new_ws->focus_pending = get_region_focus(new_ws->r);
3315 /* unmap old windows */
3317 TAILQ_FOREACH(win, &old_ws->winlist, entry)
3320 /* if workspaces were swapped, then don't wait to set focus */
3321 if (old_ws->r && focus_mode != SWM_FOCUS_FOLLOW) {
3322 if (new_ws->focus_pending) {
3323 focus_win(new_ws->focus_pending);
3325 /* Empty region, focus on root. */
3326 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT,
3327 new_ws->r->s[new_ws->r->s->idx].root,
3332 /* Clear bar if new ws is empty. */
3333 if (new_ws->focus_pending == NULL)
3338 DNPRINTF(SWM_D_WS, "switchws: done.\n");
3342 cyclews(struct swm_region *r, union arg *args)
3345 struct swm_screen *s = r->s;
3348 DNPRINTF(SWM_D_WS, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3349 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
3354 case SWM_ARG_ID_CYCLEWS_UP_ALL:
3357 case SWM_ARG_ID_CYCLEWS_UP:
3358 if (a.id < workspace_limit - 1)
3363 case SWM_ARG_ID_CYCLEWS_DOWN_ALL:
3366 case SWM_ARG_ID_CYCLEWS_DOWN:
3370 a.id = workspace_limit - 1;
3377 (!cycle_empty && TAILQ_EMPTY(&s->ws[a.id].winlist)))
3379 if (!cycle_visible && s->ws[a.id].r != NULL)
3383 } while (a.id != r->ws->idx);
3387 priorws(struct swm_region *r, union arg *args)
3393 DNPRINTF(SWM_D_WS, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3394 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
3396 if (r->ws_prior == NULL)
3399 a.id = r->ws_prior->idx;
3404 cyclescr(struct swm_region *r, union arg *args)
3407 struct swm_region *rr = NULL;
3410 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
3411 /* do nothing if we don't have more than one screen */
3412 if (!(num_screens > 1 || outputs > 1))
3417 case SWM_ARG_ID_CYCLESC_UP:
3418 rr = TAILQ_NEXT(r, entry);
3420 rr = TAILQ_FIRST(&screens[i].rl);
3422 case SWM_ARG_ID_CYCLESC_DOWN:
3423 rr = TAILQ_PREV(r, swm_region_list, entry);
3425 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
3433 nfw = get_region_focus(rr);
3437 /* New region is empty; unfocus old region and warp pointer. */
3438 unfocus_win(r->ws->focus);
3439 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT,
3440 rr->s[i].root, XCB_CURRENT_TIME);
3442 /* Clear bar since empty. */
3450 sort_windows(struct ws_win_list *wl)
3452 struct ws_win *win, *parent, *nxt;
3457 for (win = TAILQ_FIRST(wl); win != TAILQ_END(wl); win = nxt) {
3458 nxt = TAILQ_NEXT(win, entry);
3459 if (win->transient) {
3460 parent = find_window(win->transient);
3461 if (parent == NULL) {
3462 warnx("not possible bug");
3465 TAILQ_REMOVE(wl, win, entry);
3466 TAILQ_INSERT_AFTER(wl, parent, win, entry);
3473 swapwin(struct swm_region *r, union arg *args)
3475 struct ws_win *target, *source;
3476 struct ws_win *cur_focus;
3477 struct ws_win_list *wl;
3479 DNPRINTF(SWM_D_WS, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3480 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
3482 cur_focus = r->ws->focus;
3483 if (cur_focus == NULL)
3487 wl = &source->ws->winlist;
3490 case SWM_ARG_ID_SWAPPREV:
3491 if (source->transient)
3492 source = find_window(source->transient);
3493 target = TAILQ_PREV(source, ws_win_list, entry);
3494 if (target && target->transient)
3495 target = find_window(target->transient);
3496 TAILQ_REMOVE(wl, source, entry);
3498 TAILQ_INSERT_TAIL(wl, source, entry);
3500 TAILQ_INSERT_BEFORE(target, source, entry);
3502 case SWM_ARG_ID_SWAPNEXT:
3503 target = TAILQ_NEXT(source, entry);
3504 /* move the parent and let the sort handle the move */
3505 if (source->transient)
3506 source = find_window(source->transient);
3507 TAILQ_REMOVE(wl, source, entry);
3509 TAILQ_INSERT_HEAD(wl, source, entry);
3511 TAILQ_INSERT_AFTER(wl, target, source, entry);
3513 case SWM_ARG_ID_SWAPMAIN:
3514 target = TAILQ_FIRST(wl);
3515 if (target == source) {
3516 if (source->ws->focus_prev != NULL &&
3517 source->ws->focus_prev != target)
3518 source = source->ws->focus_prev;
3522 if (target == NULL || source == NULL)
3524 source->ws->focus_prev = target;
3525 TAILQ_REMOVE(wl, target, entry);
3526 TAILQ_INSERT_BEFORE(source, target, entry);
3527 TAILQ_REMOVE(wl, source, entry);
3528 TAILQ_INSERT_HEAD(wl, source, entry);
3530 case SWM_ARG_ID_MOVELAST:
3531 TAILQ_REMOVE(wl, source, entry);
3532 TAILQ_INSERT_TAIL(wl, source, entry);
3535 DNPRINTF(SWM_D_MOVE, "swapwin: invalid id: %d\n", args->id);
3547 get_focus_prev(struct ws_win *win)
3549 struct ws_win *winfocus = NULL;
3550 struct ws_win *cur_focus = NULL;
3551 struct ws_win_list *wl = NULL;
3552 struct workspace *ws = NULL;
3554 if (!(win && win->ws))
3559 cur_focus = ws->focus;
3561 DNPRINTF(SWM_D_FOCUS, "get_focus_prev: window: 0x%x, cur_focus: 0x%x\n",
3562 WINID(win), WINID(cur_focus));
3564 /* pickle, just focus on whatever */
3565 if (cur_focus == NULL) {
3566 /* use prev_focus if valid */
3567 if (ws->focus_prev && ws->focus_prev != cur_focus &&
3568 find_window(WINID(ws->focus_prev)))
3569 winfocus = ws->focus_prev;
3573 /* if transient focus on parent */
3574 if (cur_focus->transient) {
3575 winfocus = find_window(cur_focus->transient);
3579 /* if in max_stack try harder */
3580 if ((win->quirks & SWM_Q_FOCUSPREV) ||
3581 (ws->cur_layout->flags & SWM_L_FOCUSPREV)) {
3582 if (cur_focus != ws->focus_prev)
3583 winfocus = ws->focus_prev;
3585 winfocus = TAILQ_PREV(win, ws_win_list, entry);
3590 DNPRINTF(SWM_D_FOCUS, "get_focus_prev: focus_close: %d\n", focus_close);
3592 if (winfocus == NULL || winfocus == win) {
3593 switch (focus_close) {
3594 case SWM_STACK_BOTTOM:
3595 winfocus = TAILQ_FIRST(wl);
3598 winfocus = TAILQ_LAST(wl, ws_win_list);
3600 case SWM_STACK_ABOVE:
3601 if ((winfocus = TAILQ_NEXT(cur_focus, entry)) == NULL) {
3602 if (focus_close_wrap)
3603 winfocus = TAILQ_FIRST(wl);
3605 winfocus = TAILQ_PREV(cur_focus,
3606 ws_win_list, entry);
3609 case SWM_STACK_BELOW:
3610 if ((winfocus = TAILQ_PREV(cur_focus, ws_win_list,
3612 if (focus_close_wrap)
3613 winfocus = TAILQ_LAST(wl, ws_win_list);
3615 winfocus = TAILQ_NEXT(cur_focus, entry);
3621 if (winfocus == NULL) {
3622 if (focus_default == SWM_STACK_TOP)
3623 winfocus = TAILQ_LAST(wl, ws_win_list);
3625 winfocus = TAILQ_FIRST(wl);
3630 return get_focus_magic(winfocus);
3634 get_region_focus(struct swm_region *r)
3636 struct ws_win *winfocus = NULL;
3641 if (r->ws->focus && !r->ws->focus->iconic)
3642 winfocus = r->ws->focus;
3643 else if (r->ws->focus_prev && !r->ws->focus_prev->iconic)
3644 winfocus = r->ws->focus_prev;
3646 TAILQ_FOREACH(winfocus, &r->ws->winlist, entry)
3647 if (!winfocus->iconic)
3650 return get_focus_magic(winfocus);
3654 focus(struct swm_region *r, union arg *args)
3656 struct ws_win *head, *cur_focus = NULL, *winfocus = NULL;
3657 struct ws_win_list *wl = NULL;
3658 struct workspace *ws = NULL;
3664 DNPRINTF(SWM_D_FOCUS, "focus: id: %d\n", args->id);
3666 if ((cur_focus = r->ws->focus) == NULL)
3670 if (TAILQ_EMPTY(wl))
3672 /* make sure there is at least one uniconified window */
3674 TAILQ_FOREACH(winfocus, wl, entry)
3675 if (!winfocus->iconic) {
3683 case SWM_ARG_ID_FOCUSPREV:
3684 head = TAILQ_PREV(cur_focus, ws_win_list, entry);
3686 head = TAILQ_LAST(wl, ws_win_list);
3688 if (WINID(winfocus) == cur_focus->transient) {
3689 head = TAILQ_PREV(winfocus, ws_win_list, entry);
3691 head = TAILQ_LAST(wl, ws_win_list);
3696 if (winfocus && winfocus->iconic) {
3697 while (winfocus != cur_focus) {
3698 if (winfocus == NULL)
3699 winfocus = TAILQ_LAST(wl, ws_win_list);
3700 if (!winfocus->iconic)
3702 winfocus = TAILQ_PREV(winfocus, ws_win_list,
3708 case SWM_ARG_ID_FOCUSNEXT:
3709 head = TAILQ_NEXT(cur_focus, entry);
3711 head = TAILQ_FIRST(wl);
3715 if (winfocus && winfocus->iconic) {
3716 while (winfocus != cur_focus) {
3717 if (winfocus == NULL)
3718 winfocus = TAILQ_FIRST(wl);
3719 if (!winfocus->iconic)
3721 winfocus = TAILQ_NEXT(winfocus, entry);
3726 case SWM_ARG_ID_FOCUSMAIN:
3727 winfocus = TAILQ_FIRST(wl);
3728 if (winfocus == cur_focus)
3729 winfocus = cur_focus->ws->focus_prev;
3736 focus_win(get_focus_magic(winfocus));
3742 cycle_layout(struct swm_region *r, union arg *args)
3744 struct workspace *ws = r->ws;
3746 /* suppress unused warning since var is needed */
3749 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
3752 if (ws->cur_layout->l_stack == NULL)
3753 ws->cur_layout = &layouts[0];
3758 focus_win(get_region_focus(r));
3764 stack_config(struct swm_region *r, union arg *args)
3766 struct workspace *ws = r->ws;
3768 DNPRINTF(SWM_D_STACK, "stack_config: id: %d workspace: %d\n",
3771 if (ws->cur_layout->l_config != NULL)
3772 ws->cur_layout->l_config(ws, args->id);
3774 if (args->id != SWM_ARG_ID_STACKINIT)
3783 struct swm_geometry g;
3784 struct swm_region *r;
3790 DNPRINTF(SWM_D_STACK, "stack: begin\n");
3792 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
3793 for (i = 0; i < num_screens; i++) {
3797 TAILQ_FOREACH(r, &screens[i].rl, entry) {
3798 DNPRINTF(SWM_D_STACK, "stack: workspace: %d "
3799 "(screen: %d, region: %d)\n", r->ws->idx, i, j++);
3801 /* start with screen geometry, adjust for bar */
3803 g.w -= 2 * border_width;
3804 g.h -= 2 * border_width;
3805 if (bar_enabled && r->ws->bar_enabled) {
3810 r->ws->cur_layout->l_stack(r->ws, &g);
3811 r->ws->cur_layout->l_string(r->ws);
3812 /* save r so we can track region changes */
3819 DNPRINTF(SWM_D_STACK, "stack: end\n");
3823 store_float_geom(struct ws_win *win, struct swm_region *r)
3825 if (win == NULL || r == NULL)
3828 /* retain window geom and region geom */
3829 win->g_float = win->g;
3830 win->g_float.x -= X(r);
3831 win->g_float.y -= Y(r);
3832 win->g_floatvalid = 1;
3833 DNPRINTF(SWM_D_MISC, "store_float_geom: window: 0x%x, g: (%d,%d)"
3834 " %d x %d, g_float: (%d,%d) %d x %d\n", win->id, X(win), Y(win),
3835 WIDTH(win), HEIGHT(win), win->g_float.x, win->g_float.y,
3836 win->g_float.w, win->g_float.h);
3840 load_float_geom(struct ws_win *win, struct swm_region *r)
3842 if (win == NULL || r == NULL)
3845 if (win->g_floatvalid) {
3846 win->g = win->g_float;
3849 DNPRINTF(SWM_D_MISC, "load_float_geom: window: 0x%x, g: (%d,%d)"
3850 "%d x %d\n", win->id, X(win), Y(win), WIDTH(win),
3853 DNPRINTF(SWM_D_MISC, "load_float_geom: window: 0x%x, g_float "
3854 "is not set.\n", win->id);
3859 stack_floater(struct ws_win *win, struct swm_region *r)
3864 DNPRINTF(SWM_D_MISC, "stack_floater: window: 0x%x\n", win->id);
3867 * to allow windows to change their size (e.g. mplayer fs) only retrieve
3868 * geom on ws switches or return from max mode
3870 if (win->floatmaxed || (r != r->ws->old_r &&
3871 !(win->ewmh_flags & EWMH_F_FULLSCREEN))) {
3872 /* update geometry for the new region */
3873 load_float_geom(win, r);
3876 win->floatmaxed = 0;
3879 * if set to fullscreen mode, configure window to maximum size.
3881 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
3882 if (!win->g_floatvalid)
3883 store_float_geom(win, win->ws->r);
3889 * remove border on fullscreen floater when in fullscreen mode or when
3890 * the quirk is present.
3892 if ((win->ewmh_flags & EWMH_F_FULLSCREEN) ||
3893 ((win->quirks & SWM_Q_FULLSCREEN) &&
3894 (WIDTH(win) >= WIDTH(r)) && (HEIGHT(win) >= HEIGHT(r)))) {
3895 if (win->bordered) {
3897 X(win) += border_width;
3898 Y(win) += border_width;
3900 } else if (!win->bordered) {
3902 X(win) -= border_width;
3903 Y(win) -= border_width;
3906 if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
3907 WIDTH(win) = (double)WIDTH(r) * dialog_ratio;
3908 HEIGHT(win) = (double)HEIGHT(r) * dialog_ratio;
3911 if (!win->manual && !(win->ewmh_flags & EWMH_F_FULLSCREEN) &&
3912 !(win->quirks & SWM_Q_ANYWHERE)) {
3914 * floaters and transients are auto-centred unless moved,
3915 * resized or ANYWHERE quirk is set.
3917 X(win) = X(r) + (WIDTH(r) - WIDTH(win)) / 2 - BORDER(win);
3918 Y(win) = Y(r) + (HEIGHT(r) - HEIGHT(win)) / 2 - BORDER(win);
3920 store_float_geom(win, r);
3923 /* keep window within region bounds */
3924 constrain_window(win, r, 0);
3930 * Send keystrokes to terminal to decrease/increase the font size as the
3931 * window size changes.
3934 adjust_font(struct ws_win *win)
3936 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
3937 win->floating || win->transient)
3940 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
3941 WIDTH(win) / win->sh.width_inc < term_width &&
3942 win->font_steps < SWM_MAX_FONT_STEPS) {
3943 win->font_size_boundary[win->font_steps] =
3944 (win->sh.width_inc * term_width) + win->sh.base_width;
3947 win->last_inc = win->sh.width_inc;
3948 fake_keypress(win, XK_KP_Subtract, XCB_MOD_MASK_SHIFT);
3949 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
3950 WIDTH(win) > win->font_size_boundary[win->font_steps - 1]) {
3953 win->last_inc = win->sh.width_inc;
3954 fake_keypress(win, XK_KP_Add, XCB_MOD_MASK_SHIFT);
3958 #define SWAPXY(g) do { \
3960 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
3961 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
3964 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
3966 struct swm_geometry win_g, r_g = *g;
3967 struct ws_win *win, *fs_win = NULL;
3968 int i, j, s, stacks;
3969 int w_inc = 1, h_inc, w_base = 1, h_base;
3970 int hrh, extra = 0, h_slice, last_h = 0;
3971 int split, colno, winno, mwin, msize, mscale;
3972 int remain, missing, v_slice, reconfigure = 0;
3975 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d, rot: %s, "
3976 "flip: %s\n", ws->idx, YESNO(rot), YESNO(flip));
3978 winno = count_win(ws, 0);
3979 if (winno == 0 && count_win(ws, 1) == 0)
3982 TAILQ_FOREACH(win, &ws->winlist, entry)
3983 if (!win->transient && !win->floating && !win->iconic)
3990 w_inc = win->sh.width_inc;
3991 w_base = win->sh.base_width;
3992 mwin = ws->l_state.horizontal_mwin;
3993 mscale = ws->l_state.horizontal_msize;
3994 stacks = ws->l_state.horizontal_stacks;
3997 w_inc = win->sh.height_inc;
3998 w_base = win->sh.base_height;
3999 mwin = ws->l_state.vertical_mwin;
4000 mscale = ws->l_state.vertical_msize;
4001 stacks = ws->l_state.vertical_stacks;
4005 if (stacks > winno - mwin)
4006 stacks = winno - mwin;
4010 h_slice = r_g.h / SWM_H_SLICE;
4011 if (mwin && winno > mwin) {
4012 v_slice = r_g.w / SWM_V_SLICE;
4016 win_g.w = v_slice * mscale;
4018 if (w_inc > 1 && w_inc < v_slice) {
4019 /* adjust for window's requested size increment */
4020 remain = (win_g.w - w_base) % w_inc;
4026 win_g.x += r_g.w - msize;
4029 colno = split = winno / stacks;
4030 win_g.w = ((r_g.w - (stacks * 2 * border_width) +
4031 2 * border_width) / stacks);
4033 hrh = r_g.h / colno;
4034 extra = r_g.h - (colno * hrh);
4035 win_g.h = hrh - 2 * border_width;
4037 /* stack all the tiled windows */
4038 i = j = 0, s = stacks;
4039 TAILQ_FOREACH(win, &ws->winlist, entry) {
4040 if (win->transient || win->floating)
4045 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
4050 if (split && i == split) {
4051 colno = (winno - mwin) / stacks;
4052 if (s <= (winno - mwin) % stacks)
4054 split = split + colno;
4055 hrh = (r_g.h / colno);
4056 extra = r_g.h - (colno * hrh);
4060 win_g.x += win_g.w + 2 * border_width;
4061 win_g.w = (r_g.w - msize -
4062 (stacks * 2 * border_width)) / stacks;
4064 win_g.w += (r_g.w - msize -
4065 (stacks * 2 * border_width)) % stacks;
4069 win_g.h = hrh - 2 * border_width;
4071 h_inc = win->sh.width_inc;
4072 h_base = win->sh.base_width;
4074 h_inc = win->sh.height_inc;
4075 h_base = win->sh.base_height;
4077 if (j == colno - 1) {
4078 win_g.h = hrh + extra;
4079 } else if (h_inc > 1 && h_inc < h_slice) {
4080 /* adjust for window's requested size increment */
4081 remain = (win_g.h - h_base) % h_inc;
4082 missing = h_inc - remain;
4084 if (missing <= extra || j == 0) {
4096 win_g.y += last_h + 2 * border_width;
4098 if (disable_border && !(bar_enabled && ws->bar_enabled) &&
4101 win_g.w += 2 * border_width;
4102 win_g.h += 2 * border_width;
4107 if (X(win) != win_g.y || Y(win) != win_g.x ||
4108 WIDTH(win) != win_g.h || HEIGHT(win) != win_g.w) {
4112 WIDTH(win) = win_g.h;
4113 HEIGHT(win) = win_g.w;
4116 if (X(win) != win_g.x || Y(win) != win_g.y ||
4117 WIDTH(win) != win_g.w || HEIGHT(win) != win_g.h) {
4121 WIDTH(win) = win_g.w;
4122 HEIGHT(win) = win_g.h;
4126 if (bordered != win->bordered) {
4128 win->bordered = bordered;
4144 /* now, stack all the floaters and transients */
4145 TAILQ_FOREACH(win, &ws->winlist, entry) {
4146 if (!win->transient && !win->floating)
4150 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
4155 stack_floater(win, ws->r);
4159 /* Make sure fs_win is stacked last so it's on top. */
4161 stack_floater(fs_win, ws->r);
4167 vertical_config(struct workspace *ws, int id)
4169 DNPRINTF(SWM_D_STACK, "vertical_config: id: %d, workspace: %d\n",
4173 case SWM_ARG_ID_STACKRESET:
4174 case SWM_ARG_ID_STACKINIT:
4175 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
4176 ws->l_state.vertical_mwin = 1;
4177 ws->l_state.vertical_stacks = 1;
4179 case SWM_ARG_ID_MASTERSHRINK:
4180 if (ws->l_state.vertical_msize > 1)
4181 ws->l_state.vertical_msize--;
4183 case SWM_ARG_ID_MASTERGROW:
4184 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
4185 ws->l_state.vertical_msize++;
4187 case SWM_ARG_ID_MASTERADD:
4188 ws->l_state.vertical_mwin++;
4190 case SWM_ARG_ID_MASTERDEL:
4191 if (ws->l_state.vertical_mwin > 0)
4192 ws->l_state.vertical_mwin--;
4194 case SWM_ARG_ID_STACKINC:
4195 ws->l_state.vertical_stacks++;
4197 case SWM_ARG_ID_STACKDEC:
4198 if (ws->l_state.vertical_stacks > 1)
4199 ws->l_state.vertical_stacks--;
4201 case SWM_ARG_ID_FLIPLAYOUT:
4202 ws->l_state.vertical_flip = !ws->l_state.vertical_flip;
4210 vertical_stack(struct workspace *ws, struct swm_geometry *g)
4212 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
4214 stack_master(ws, g, 0, ws->l_state.vertical_flip);
4218 horizontal_config(struct workspace *ws, int id)
4220 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
4223 case SWM_ARG_ID_STACKRESET:
4224 case SWM_ARG_ID_STACKINIT:
4225 ws->l_state.horizontal_mwin = 1;
4226 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
4227 ws->l_state.horizontal_stacks = 1;
4229 case SWM_ARG_ID_MASTERSHRINK:
4230 if (ws->l_state.horizontal_msize > 1)
4231 ws->l_state.horizontal_msize--;
4233 case SWM_ARG_ID_MASTERGROW:
4234 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
4235 ws->l_state.horizontal_msize++;
4237 case SWM_ARG_ID_MASTERADD:
4238 ws->l_state.horizontal_mwin++;
4240 case SWM_ARG_ID_MASTERDEL:
4241 if (ws->l_state.horizontal_mwin > 0)
4242 ws->l_state.horizontal_mwin--;
4244 case SWM_ARG_ID_STACKINC:
4245 ws->l_state.horizontal_stacks++;
4247 case SWM_ARG_ID_STACKDEC:
4248 if (ws->l_state.horizontal_stacks > 1)
4249 ws->l_state.horizontal_stacks--;
4251 case SWM_ARG_ID_FLIPLAYOUT:
4252 ws->l_state.horizontal_flip = !ws->l_state.horizontal_flip;
4260 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
4262 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
4264 stack_master(ws, g, 1, ws->l_state.horizontal_flip);
4267 /* fullscreen view */
4269 max_stack(struct workspace *ws, struct swm_geometry *g)
4271 struct swm_geometry gg = *g;
4272 struct ws_win *w, *win = NULL, *parent = NULL;
4273 int winno, num_screens;
4275 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
4280 winno = count_win(ws, 0);
4281 if (winno == 0 && count_win(ws, 1) == 0)
4284 /* Figure out which top level window should be visible. */
4285 if (ws->focus_pending)
4286 win = ws->focus_pending;
4290 win = TAILQ_FIRST(&ws->winlist);
4293 parent = find_window(win->transient);
4295 DNPRINTF(SWM_D_STACK, "max_stack: win: 0x%x\n", win->id);
4297 /* maximize all top level windows */
4298 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
4299 TAILQ_FOREACH(w, &ws->winlist, entry) {
4300 if (w->transient || w->iconic)
4303 if (w->floating && !w->floatmaxed) {
4305 * retain geometry for retrieval on exit from
4308 store_float_geom(w, ws->r);
4312 /* only reconfigure if necessary */
4313 if (X(w) != gg.x || Y(w) != gg.y || WIDTH(w) != gg.w ||
4314 HEIGHT(w) != gg.h) {
4316 if (bar_enabled && ws->bar_enabled){
4320 WIDTH(w) += 2 * border_width;
4321 HEIGHT(w) += 2 * border_width;
4327 /* Unmap unwanted windows if not multi-screen. */
4328 if (!(num_screens > 1 || outputs > 1) && (w != win ||
4329 w != parent || w->transient != win->id))
4333 /* If a parent exists, map it first. */
4337 /* Map siblings next. */
4338 TAILQ_FOREACH(w, &ws->winlist, entry)
4339 if (w != win && !w->iconic &&
4340 w->transient == parent->id) {
4341 stack_floater(w, ws->r);
4346 /* Map focused window. */
4349 /* Finally, map children of focus window. */
4350 TAILQ_FOREACH(w, &ws->winlist, entry)
4351 if (w->transient == win->id && !w->iconic) {
4352 stack_floater(w, ws->r);
4358 send_to_ws(struct swm_region *r, union arg *args)
4360 int wsid = args->id;
4361 struct ws_win *win = NULL, *parent;
4362 struct workspace *ws, *nws, *pws;
4363 char ws_idx_str[SWM_PROPLEN];
4365 if (wsid >= workspace_limit)
4368 if (r && r->ws && r->ws->focus)
4373 if (win->ws->idx == wsid)
4376 DNPRINTF(SWM_D_MOVE, "send_to_ws: win 0x%x, ws %d -> %d\n", win->id,
4377 win->ws->idx, wsid);
4380 nws = &win->s->ws[wsid];
4382 /* Update the window's workspace property: _SWM_WS */
4383 if (snprintf(ws_idx_str, SWM_PROPLEN, "%d", nws->idx) < SWM_PROPLEN) {
4384 if (focus_mode != SWM_FOCUS_FOLLOW)
4385 ws->focus_pending = get_focus_prev(win);
4387 /* Move the parent if this is a transient window. */
4388 if (win->transient) {
4389 parent = find_window(win->transient);
4392 /* Set new focus in parent's ws if needed. */
4393 if (pws->focus == parent) {
4394 if (focus_mode != SWM_FOCUS_FOLLOW)
4395 pws->focus_pending =
4396 get_focus_prev(parent);
4398 unfocus_win(parent);
4400 if (focus_mode != SWM_FOCUS_FOLLOW)
4401 pws->focus = pws->focus_pending;
4403 if (focus_mode != SWM_FOCUS_FOLLOW)
4404 pws->focus_pending = NULL;
4407 /* Don't unmap parent if new ws is visible */
4409 unmap_window(parent);
4412 TAILQ_REMOVE(&ws->winlist, parent, entry);
4413 TAILQ_INSERT_TAIL(&nws->winlist, parent, entry);
4416 DNPRINTF(SWM_D_PROP, "send_to_ws: set "
4417 "property: _SWM_WS: %s\n", ws_idx_str);
4418 xcb_change_property(conn, XCB_PROP_MODE_REPLACE,
4419 parent->id, a_swm_ws, XCB_ATOM_STRING, 8,
4420 strlen(ws_idx_str), ws_idx_str);
4426 /* Don't unmap if new ws is visible */
4431 TAILQ_REMOVE(&ws->winlist, win, entry);
4432 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
4435 /* Set focus on new ws. */
4436 unfocus_win(nws->focus);
4439 DNPRINTF(SWM_D_PROP, "send_to_ws: set property: _SWM_WS: %s\n",
4441 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
4442 a_swm_ws, XCB_ATOM_STRING, 8, strlen(ws_idx_str),
4445 /* Restack and set new focus. */
4448 if (focus_mode != SWM_FOCUS_FOLLOW) {
4449 focus_win(ws->focus_pending);
4450 ws->focus_pending = NULL;
4456 DNPRINTF(SWM_D_MOVE, "send_to_ws: done.\n");
4460 pressbutton(struct swm_region *r, union arg *args)
4462 /* suppress unused warning since var is needed */
4465 xcb_test_fake_input(conn, XCB_BUTTON_PRESS, args->id,
4466 XCB_CURRENT_TIME, XCB_WINDOW_NONE, 0, 0, 0);
4467 xcb_test_fake_input(conn, XCB_BUTTON_RELEASE, args->id,
4468 XCB_CURRENT_TIME, XCB_WINDOW_NONE, 0, 0, 0);
4472 raise_toggle(struct swm_region *r, union arg *args)
4474 /* suppress unused warning since var is needed */
4477 if (r == NULL || r->ws == NULL)
4480 r->ws->always_raise = !r->ws->always_raise;
4482 /* bring floaters back to top */
4483 if (!r->ws->always_raise)
4490 iconify(struct swm_region *r, union arg *args)
4492 /* suppress unused warning since var is needed */
4495 if (r->ws->focus == NULL)
4498 set_swm_iconic(r->ws->focus, 1);
4504 get_win_name(xcb_window_t win)
4507 xcb_get_property_cookie_t c;
4508 xcb_get_property_reply_t *r;
4510 /* First try _NET_WM_NAME for UTF-8. */
4511 c = xcb_get_property(conn, 0, win, a_netwmname,
4512 XCB_GET_PROPERTY_TYPE_ANY, 0, UINT_MAX);
4513 r = xcb_get_property_reply(conn, c, NULL);
4516 if (r->type == XCB_NONE) {
4518 /* Use WM_NAME instead; no UTF-8. */
4519 c = xcb_get_property(conn, 0, win, XCB_ATOM_WM_NAME,
4520 XCB_GET_PROPERTY_TYPE_ANY, 0, UINT_MAX);
4521 r = xcb_get_property_reply(conn, c, NULL);
4525 if (r->type == XCB_NONE) {
4531 name = strndup(xcb_get_property_value(r),
4532 xcb_get_property_value_length(r));
4541 uniconify(struct swm_region *r, union arg *args)
4548 DNPRINTF(SWM_D_MISC, "uniconify\n");
4550 if (r == NULL || r->ws == NULL)
4553 /* make sure we have anything to uniconify */
4554 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
4555 if (win->ws == NULL)
4556 continue; /* should never happen */
4565 search_resp_action = SWM_SEARCH_UNICONIFY;
4567 spawn_select(r, args, "search", &searchpid);
4569 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
4572 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
4573 if (win->ws == NULL)
4574 continue; /* should never happen */
4578 name = get_win_name(win->id);
4581 fprintf(lfile, "%s.%u\n", name, win->id);
4589 name_workspace(struct swm_region *r, union arg *args)
4593 DNPRINTF(SWM_D_MISC, "name_workspace\n");
4599 search_resp_action = SWM_SEARCH_NAME_WORKSPACE;
4601 spawn_select(r, args, "name_workspace", &searchpid);
4603 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
4606 fprintf(lfile, "%s", "");
4611 search_workspace(struct swm_region *r, union arg *args)
4614 struct workspace *ws;
4617 DNPRINTF(SWM_D_MISC, "search_workspace\n");
4623 search_resp_action = SWM_SEARCH_SEARCH_WORKSPACE;
4625 spawn_select(r, args, "search", &searchpid);
4627 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
4630 for (i = 0; i < workspace_limit; i++) {
4634 fprintf(lfile, "%d%s%s\n", ws->idx + 1,
4635 (ws->name ? ":" : ""), (ws->name ? ws->name : ""));
4642 search_win_cleanup(void)
4644 struct search_window *sw = NULL;
4646 while ((sw = TAILQ_FIRST(&search_wl)) != NULL) {
4647 xcb_destroy_window(conn, sw->indicator);
4648 TAILQ_REMOVE(&search_wl, sw, entry);
4654 search_win(struct swm_region *r, union arg *args)
4656 struct ws_win *win = NULL;
4657 struct search_window *sw = NULL;
4660 int i, width, height;
4668 XRectangle l_ibox, l_lbox;
4670 DNPRINTF(SWM_D_MISC, "search_win\n");
4673 search_resp_action = SWM_SEARCH_SEARCH_WINDOW;
4675 spawn_select(r, args, "search", &searchpid);
4677 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
4680 TAILQ_INIT(&search_wl);
4683 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
4687 sw = calloc(1, sizeof(struct search_window));
4689 warn("search_win: calloc");
4691 search_win_cleanup();
4697 snprintf(s, sizeof s, "%d", i);
4700 w = xcb_generate_id(conn);
4701 wa[0] = r->s->c[SWM_S_COLOR_FOCUS].pixel;
4702 wa[1] = r->s->c[SWM_S_COLOR_UNFOCUS].pixel;
4704 if (bar_font_legacy) {
4705 XmbTextExtents(bar_fs, s, len, &l_ibox, &l_lbox);
4706 width = l_lbox.width + 4;
4707 height = bar_fs_extents->max_logical_extent.height + 4;
4709 XftTextExtentsUtf8(display, bar_font, (FcChar8 *)s, len,
4711 width = info.width + 4;
4712 height = bar_font->height + 4;
4715 xcb_create_window(conn, XCB_COPY_FROM_PARENT, w, win->id, 0, 0,
4716 width, height, 1, XCB_WINDOW_CLASS_INPUT_OUTPUT,
4717 XCB_COPY_FROM_PARENT, XCB_CW_BACK_PIXEL |
4718 XCB_CW_BORDER_PIXEL, wa);
4720 xcb_map_window(conn, w);
4723 TAILQ_INSERT_TAIL(&search_wl, sw, entry);
4725 if (bar_font_legacy) {
4726 l_gcv.graphics_exposures = 0;
4727 l_draw = XCreateGC(display, w, 0, &l_gcv);
4729 XSetForeground(display, l_draw,
4730 r->s->c[SWM_S_COLOR_BAR].pixel);
4732 DRAWSTRING(display, w, bar_fs, l_draw, 2,
4733 (bar_fs_extents->max_logical_extent.height -
4734 l_lbox.height) / 2 - l_lbox.y, s, len);
4736 XFreeGC(display, l_draw);
4739 draw = XftDrawCreate(display, w,
4740 DefaultVisual(display, r->s->idx),
4741 DefaultColormap(display, r->s->idx));
4743 XftDrawStringUtf8(draw, &bar_font_color, bar_font, 2,
4744 (HEIGHT(r->bar) + bar_font->height) / 2 -
4745 bar_font->descent, (FcChar8 *)s, len);
4747 XftDrawDestroy(draw);
4750 DNPRINTF(SWM_D_MISC, "search_win: mapped window: 0x%x\n", w);
4752 fprintf(lfile, "%d\n", i);
4762 search_resp_uniconify(const char *resp, unsigned long len)
4768 DNPRINTF(SWM_D_MISC, "search_resp_uniconify: resp: %s\n", resp);
4770 TAILQ_FOREACH(win, &search_r->ws->winlist, entry) {
4773 name = get_win_name(win->id);
4776 if (asprintf(&s, "%s.%u", name, win->id) == -1) {
4781 if (strncmp(s, resp, len) == 0) {
4782 /* XXX this should be a callback to generalize */
4783 set_swm_iconic(win, 0);
4792 search_resp_name_workspace(const char *resp, unsigned long len)
4794 struct workspace *ws;
4796 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: resp: %s\n", resp);
4798 if (search_r->ws == NULL)
4803 free(search_r->ws->name);
4804 search_r->ws->name = NULL;
4808 ws->name = strdup(resp);
4809 if (ws->name == NULL) {
4810 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: "
4811 "strdup: %s", strerror(errno));
4818 search_resp_search_workspace(const char *resp)
4825 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: resp: %s\n", resp);
4829 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: strdup: %s",
4836 ws_idx = (int)strtonum(q, 1, workspace_limit, &errstr);
4838 DNPRINTF(SWM_D_MISC, "workspace idx is %s: %s",
4845 switchws(search_r, &a);
4849 search_resp_search_window(const char *resp)
4854 struct search_window *sw;
4856 DNPRINTF(SWM_D_MISC, "search_resp_search_window: resp: %s\n", resp);
4860 DNPRINTF(SWM_D_MISC, "search_resp_search_window: strdup: %s",
4865 idx = (int)strtonum(s, 1, INT_MAX, &errstr);
4867 DNPRINTF(SWM_D_MISC, "window idx is %s: %s",
4874 TAILQ_FOREACH(sw, &search_wl, entry)
4875 if (idx == sw->idx) {
4881 #define MAX_RESP_LEN 1024
4884 search_do_resp(void)
4890 DNPRINTF(SWM_D_MISC, "search_do_resp:\n");
4895 if ((resp = calloc(1, MAX_RESP_LEN + 1)) == NULL) {
4896 warn("search: calloc");
4900 rbytes = read(select_resp_pipe[0], resp, MAX_RESP_LEN);
4902 warn("search: read error");
4905 resp[rbytes] = '\0';
4908 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
4909 * newline, so work around that by sanitizing the resp now.
4911 resp[strcspn(resp, "\n")] = '\0';
4914 switch (search_resp_action) {
4915 case SWM_SEARCH_UNICONIFY:
4916 search_resp_uniconify(resp, len);
4918 case SWM_SEARCH_NAME_WORKSPACE:
4919 search_resp_name_workspace(resp, len);
4921 case SWM_SEARCH_SEARCH_WORKSPACE:
4922 search_resp_search_workspace(resp);
4924 case SWM_SEARCH_SEARCH_WINDOW:
4925 search_resp_search_window(resp);
4930 if (search_resp_action == SWM_SEARCH_SEARCH_WINDOW)
4931 search_win_cleanup();
4933 search_resp_action = SWM_SEARCH_NONE;
4934 close(select_resp_pipe[0]);
4941 wkill(struct swm_region *r, union arg *args)
4943 DNPRINTF(SWM_D_MISC, "wkill: id: %d\n", args->id);
4945 if (r->ws->focus == NULL)
4948 if (args->id == SWM_ARG_ID_KILLWINDOW)
4949 xcb_kill_client(conn, r->ws->focus->id);
4951 if (r->ws->focus->can_delete)
4952 client_msg(r->ws->focus, a_delete, 0);
4958 floating_toggle_win(struct ws_win *win)
4960 struct swm_region *r;
4970 /* reject floating toggles in max stack mode */
4971 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK])
4974 if (win->floating) {
4975 if (!win->floatmaxed) {
4976 /* retain position for refloat */
4977 store_float_geom(win, r);
4981 load_float_geom(win, r);
4985 ewmh_update_actions(win);
4991 floating_toggle(struct swm_region *r, union arg *args)
4993 struct ws_win *win = r->ws->focus;
4995 /* suppress unused warning since var is needed */
5001 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
5004 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
5005 _NET_WM_STATE_TOGGLE);
5009 if (win == win->ws->focus)
5016 constrain_window(struct ws_win *win, struct swm_region *r, int resizable)
5018 if (MAX_X(win) + BORDER(win) > MAX_X(r)) {
5020 WIDTH(win) = MAX_X(r) - X(win) - BORDER(win);
5022 X(win) = MAX_X(r)- WIDTH(win) - BORDER(win);
5025 if (X(win) + BORDER(win) < X(r)) {
5027 WIDTH(win) -= X(r) - X(win) - BORDER(win);
5029 X(win) = X(r) - BORDER(win);
5032 if (MAX_Y(win) + BORDER(win) > MAX_Y(r)) {
5034 HEIGHT(win) = MAX_Y(r) - Y(win) - BORDER(win);
5036 Y(win) = MAX_Y(r) - HEIGHT(win) - BORDER(win);
5039 if (Y(win) + BORDER(win) < Y(r)) {
5041 HEIGHT(win) -= Y(r) - Y(win) - BORDER(win);
5043 Y(win) = Y(r) - BORDER(win);
5049 if (HEIGHT(win) < 1)
5055 update_window(struct ws_win *win)
5060 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
5061 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
5062 XCB_CONFIG_WINDOW_BORDER_WIDTH;
5066 wc[3] = HEIGHT(win);
5067 wc[4] = BORDER(win);
5069 DNPRINTF(SWM_D_EVENT, "update_window: window: 0x%x, (x,y) w x h: "
5070 "(%d,%d) %d x %d, bordered: %s\n", win->id, wc[0], wc[1], wc[2],
5071 wc[3], YESNO(win->bordered));
5073 xcb_configure_window(conn, win->id, mask, wc);
5076 #define SWM_RESIZE_STEPS (50)
5079 resize(struct ws_win *win, union arg *args)
5081 xcb_timestamp_t timestamp = 0;
5082 struct swm_region *r = NULL;
5084 struct swm_geometry g;
5085 int top = 0, left = 0, resizing;
5087 xcb_cursor_t cursor;
5088 xcb_query_pointer_reply_t *xpr;
5089 xcb_generic_event_t *evt;
5090 xcb_motion_notify_event_t *mne;
5096 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
5099 DNPRINTF(SWM_D_EVENT, "resize: window: 0x%x, floating: %s, "
5100 "transient: 0x%x\n", win->id, YESNO(win->floating),
5103 if (!win->transient && !win->floating)
5106 /* reject resizes in max mode for floaters (transient ok) */
5107 if (win->floatmaxed)
5111 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
5119 case SWM_ARG_ID_WIDTHSHRINK:
5120 WIDTH(win) -= SWM_RESIZE_STEPS;
5123 case SWM_ARG_ID_WIDTHGROW:
5124 WIDTH(win) += SWM_RESIZE_STEPS;
5127 case SWM_ARG_ID_HEIGHTSHRINK:
5128 HEIGHT(win) -= SWM_RESIZE_STEPS;
5131 case SWM_ARG_ID_HEIGHTGROW:
5132 HEIGHT(win) += SWM_RESIZE_STEPS;
5139 constrain_window(win, r, 1);
5141 store_float_geom(win,r);
5145 /* get cursor offset from window root */
5146 xpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, win->id),
5153 if (xpr->win_x < WIDTH(win) / 2)
5156 if (xpr->win_y < HEIGHT(win) / 2)
5159 if (args->id == SWM_ARG_ID_CENTER)
5160 cursor = cursors[XC_SIZING].cid;
5162 cursor = cursors[left ? XC_TOP_LEFT_CORNER :
5163 XC_TOP_RIGHT_CORNER].cid;
5165 cursor = cursors[left ? XC_BOTTOM_LEFT_CORNER :
5166 XC_BOTTOM_RIGHT_CORNER].cid;
5168 xcb_grab_pointer(conn, 0, win->id, MOUSEMASK,
5169 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, XCB_WINDOW_NONE, cursor,
5174 while ((evt = xcb_wait_for_event(conn)) && resizing) {
5175 switch (XCB_EVENT_RESPONSE_TYPE(evt)) {
5176 case XCB_BUTTON_RELEASE:
5177 DNPRINTF(SWM_D_EVENT, "resize: BUTTON_RELEASE\n");
5180 case XCB_MOTION_NOTIFY:
5181 mne = (xcb_motion_notify_event_t *)evt;
5182 /* cursor offset/delta from start of the operation */
5183 dx = mne->root_x - xpr->root_x;
5184 dy = mne->root_y - xpr->root_y;
5190 if (args->id == SWM_ARG_ID_CENTER) {
5191 if (g.h / 2 + dy < 1)
5195 HEIGHT(win) = g.h + 2 * dy;
5203 HEIGHT(win) = g.h + dy;
5210 if (args->id == SWM_ARG_ID_CENTER) {
5211 if (g.w / 2 + dx < 1)
5215 WIDTH(win) = g.w + 2 * dx;
5223 WIDTH(win) = g.w + dx;
5226 constrain_window(win, r, 1);
5228 /* not free, don't sync more than 120 times / second */
5229 if ((mne->time - timestamp) > (1000 / 120) ) {
5230 timestamp = mne->time;
5245 store_float_geom(win,r);
5247 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
5249 DNPRINTF(SWM_D_EVENT, "resize: done.\n");
5253 resize_step(struct swm_region *r, union arg *args)
5255 struct ws_win *win = NULL;
5257 if (r && r->ws && r->ws->focus)
5266 #define SWM_MOVE_STEPS (50)
5269 move(struct ws_win *win, union arg *args)
5271 xcb_timestamp_t timestamp = 0;
5272 int move_stp = 0, moving;
5273 struct swm_region *r = NULL;
5274 xcb_query_pointer_reply_t *qpr;
5275 xcb_generic_event_t *evt;
5276 xcb_motion_notify_event_t *mne;
5282 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
5285 DNPRINTF(SWM_D_EVENT, "move: window: 0x%x, floating: %s, transient: "
5286 "0x%x\n", win->id, YESNO(win->floating), win->transient);
5288 /* in max_stack mode should only move transients */
5289 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !win->transient)
5293 if (!win->floating && !win->transient) {
5294 store_float_geom(win, r);
5295 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
5298 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
5307 case SWM_ARG_ID_MOVELEFT:
5308 X(win) -= (SWM_MOVE_STEPS - border_width);
5311 case SWM_ARG_ID_MOVERIGHT:
5312 X(win) += (SWM_MOVE_STEPS - border_width);
5315 case SWM_ARG_ID_MOVEUP:
5316 Y(win) -= (SWM_MOVE_STEPS - border_width);
5319 case SWM_ARG_ID_MOVEDOWN:
5320 Y(win) += (SWM_MOVE_STEPS - border_width);
5327 constrain_window(win, r, 0);
5329 store_float_geom(win, r);
5333 xcb_grab_pointer(conn, 0, win->id, MOUSEMASK,
5334 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC,
5335 XCB_WINDOW_NONE, cursors[XC_FLEUR].cid, XCB_CURRENT_TIME);
5337 /* get cursor offset from window root */
5338 qpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, win->id),
5341 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
5347 while ((evt = xcb_wait_for_event(conn)) && moving) {
5348 switch (XCB_EVENT_RESPONSE_TYPE(evt)) {
5349 case XCB_BUTTON_RELEASE:
5350 DNPRINTF(SWM_D_EVENT, "move: BUTTON_RELEASE\n");
5353 case XCB_MOTION_NOTIFY:
5354 mne = (xcb_motion_notify_event_t *)evt;
5355 X(win) = mne->root_x - qpr->win_x - border_width;
5356 Y(win) = mne->root_y - qpr->win_y - border_width;
5358 constrain_window(win, r, 0);
5360 /* not free, don't sync more than 120 times / second */
5361 if ((mne->time - timestamp) > (1000 / 120) ) {
5362 timestamp = mne->time;
5377 store_float_geom(win, r);
5379 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
5380 DNPRINTF(SWM_D_EVENT, "move: done.\n");
5384 move_step(struct swm_region *r, union arg *args)
5386 struct ws_win *win = NULL;
5388 if (r && r->ws && r->ws->focus)
5393 if (!win->transient && !win->floating)
5400 /* key definitions */
5402 char name[SWM_FUNCNAME_LEN];
5403 void (*func)(struct swm_region *r, union arg *);
5405 } keyfuncs[KF_INVALID + 1] = {
5406 /* name function argument */
5407 { "bar_toggle", bar_toggle, {.id = SWM_ARG_ID_BAR_TOGGLE} },
5408 { "bar_toggle_ws", bar_toggle, {.id = SWM_ARG_ID_BAR_TOGGLE_WS} },
5409 { "button2", pressbutton, {2} },
5410 { "cycle_layout", cycle_layout, {0} },
5411 { "flip_layout", stack_config, {.id = SWM_ARG_ID_FLIPLAYOUT} },
5412 { "float_toggle", floating_toggle,{0} },
5413 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
5414 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
5415 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
5416 { "height_grow", resize_step, {.id = SWM_ARG_ID_HEIGHTGROW} },
5417 { "height_shrink", resize_step, {.id = SWM_ARG_ID_HEIGHTSHRINK} },
5418 { "iconify", iconify, {0} },
5419 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
5420 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
5421 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
5422 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
5423 { "move_down", move_step, {.id = SWM_ARG_ID_MOVEDOWN} },
5424 { "move_left", move_step, {.id = SWM_ARG_ID_MOVELEFT} },
5425 { "move_right", move_step, {.id = SWM_ARG_ID_MOVERIGHT} },
5426 { "move_up", move_step, {.id = SWM_ARG_ID_MOVEUP} },
5427 { "mvws_1", send_to_ws, {.id = 0} },
5428 { "mvws_2", send_to_ws, {.id = 1} },
5429 { "mvws_3", send_to_ws, {.id = 2} },
5430 { "mvws_4", send_to_ws, {.id = 3} },
5431 { "mvws_5", send_to_ws, {.id = 4} },
5432 { "mvws_6", send_to_ws, {.id = 5} },
5433 { "mvws_7", send_to_ws, {.id = 6} },
5434 { "mvws_8", send_to_ws, {.id = 7} },
5435 { "mvws_9", send_to_ws, {.id = 8} },
5436 { "mvws_10", send_to_ws, {.id = 9} },
5437 { "mvws_11", send_to_ws, {.id = 10} },
5438 { "mvws_12", send_to_ws, {.id = 11} },
5439 { "mvws_13", send_to_ws, {.id = 12} },
5440 { "mvws_14", send_to_ws, {.id = 13} },
5441 { "mvws_15", send_to_ws, {.id = 14} },
5442 { "mvws_16", send_to_ws, {.id = 15} },
5443 { "mvws_17", send_to_ws, {.id = 16} },
5444 { "mvws_18", send_to_ws, {.id = 17} },
5445 { "mvws_19", send_to_ws, {.id = 18} },
5446 { "mvws_20", send_to_ws, {.id = 19} },
5447 { "mvws_21", send_to_ws, {.id = 20} },
5448 { "mvws_22", send_to_ws, {.id = 21} },
5449 { "name_workspace", name_workspace, {0} },
5450 { "quit", quit, {0} },
5451 { "raise_toggle", raise_toggle, {0} },
5452 { "restart", restart, {0} },
5453 { "screen_next", cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
5454 { "screen_prev", cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
5455 { "search_win", search_win, {0} },
5456 { "search_workspace", search_workspace, {0} },
5457 { "spawn_custom", NULL, {0} },
5458 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
5459 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
5460 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
5461 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
5462 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
5463 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
5464 { "uniconify", uniconify, {0} },
5465 { "version", version, {0} },
5466 { "width_grow", resize_step, {.id = SWM_ARG_ID_WIDTHGROW} },
5467 { "width_shrink", resize_step, {.id = SWM_ARG_ID_WIDTHSHRINK} },
5468 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
5469 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
5470 { "ws_1", switchws, {.id = 0} },
5471 { "ws_2", switchws, {.id = 1} },
5472 { "ws_3", switchws, {.id = 2} },
5473 { "ws_4", switchws, {.id = 3} },
5474 { "ws_5", switchws, {.id = 4} },
5475 { "ws_6", switchws, {.id = 5} },
5476 { "ws_7", switchws, {.id = 6} },
5477 { "ws_8", switchws, {.id = 7} },
5478 { "ws_9", switchws, {.id = 8} },
5479 { "ws_10", switchws, {.id = 9} },
5480 { "ws_11", switchws, {.id = 10} },
5481 { "ws_12", switchws, {.id = 11} },
5482 { "ws_13", switchws, {.id = 12} },
5483 { "ws_14", switchws, {.id = 13} },
5484 { "ws_15", switchws, {.id = 14} },
5485 { "ws_16", switchws, {.id = 15} },
5486 { "ws_17", switchws, {.id = 16} },
5487 { "ws_18", switchws, {.id = 17} },
5488 { "ws_19", switchws, {.id = 18} },
5489 { "ws_20", switchws, {.id = 19} },
5490 { "ws_21", switchws, {.id = 20} },
5491 { "ws_22", switchws, {.id = 21} },
5492 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
5493 { "ws_next_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP_ALL} },
5494 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
5495 { "ws_prev_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN_ALL} },
5496 { "ws_prior", priorws, {0} },
5497 { "dumpwins", dumpwins, {0} }, /* MUST BE LAST */
5498 { "invalid key func", NULL, {0} },
5502 key_cmp(struct key *kp1, struct key *kp2)
5504 if (kp1->keysym < kp2->keysym)
5506 if (kp1->keysym > kp2->keysym)
5509 if (kp1->mod < kp2->mod)
5511 if (kp1->mod > kp2->mod)
5518 enum { client_click, root_click };
5520 unsigned int action;
5522 unsigned int button;
5523 void (*func)(struct ws_win *, union arg *);
5526 #define MODKEY_SHIFT MODKEY | XCB_MOD_MASK_SHIFT
5527 /* action key mouse button func args */
5528 { client_click, MODKEY, XCB_BUTTON_INDEX_3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
5529 { client_click, MODKEY_SHIFT, XCB_BUTTON_INDEX_3, resize, {.id = SWM_ARG_ID_CENTER} },
5530 { client_click, MODKEY, XCB_BUTTON_INDEX_1, move, {0} },
5535 update_modkey(unsigned int mod)
5541 RB_FOREACH(kp, key_tree, &keys)
5542 if (kp->mod & XCB_MOD_MASK_SHIFT)
5543 kp->mod = mod | XCB_MOD_MASK_SHIFT;
5547 for (i = 0; i < LENGTH(buttons); i++)
5548 if (buttons[i].mask & XCB_MOD_MASK_SHIFT)
5549 buttons[i].mask = mod | XCB_MOD_MASK_SHIFT;
5551 buttons[i].mask = mod;
5555 spawn_expand(struct swm_region *r, union arg *args, const char *spawn_name,
5558 struct spawn_prog *prog = NULL;
5560 char *ap, **real_args;
5562 /* suppress unused warning since var is needed */
5565 DNPRINTF(SWM_D_SPAWN, "spawn_expand: %s\n", spawn_name);
5568 TAILQ_FOREACH(prog, &spawns, entry) {
5569 if (!strcasecmp(spawn_name, prog->name))
5573 warnx("spawn_custom: program %s not found", spawn_name);
5577 /* make room for expanded args */
5578 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
5579 err(1, "spawn_custom: calloc real_args");
5581 /* expand spawn_args into real_args */
5582 for (i = 0; i < prog->argc; i++) {
5584 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg: %s\n", ap);
5585 if (!strcasecmp(ap, "$bar_border")) {
5587 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
5589 err(1, "spawn_custom border color");
5590 } else if (!strcasecmp(ap, "$bar_color")) {
5592 strdup(r->s->c[SWM_S_COLOR_BAR].name))
5594 err(1, "spawn_custom bar color");
5595 } else if (!strcasecmp(ap, "$bar_font")) {
5596 if ((real_args[i] = strdup(bar_fonts))
5598 err(1, "spawn_custom bar fonts");
5599 } else if (!strcasecmp(ap, "$bar_font_color")) {
5601 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
5603 err(1, "spawn_custom color font");
5604 } else if (!strcasecmp(ap, "$color_focus")) {
5606 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
5608 err(1, "spawn_custom color focus");
5609 } else if (!strcasecmp(ap, "$color_unfocus")) {
5611 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
5613 err(1, "spawn_custom color unfocus");
5615 /* no match --> copy as is */
5616 if ((real_args[i] = strdup(ap)) == NULL)
5617 err(1, "spawn_custom strdup(ap)");
5619 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg: %s\n",
5624 DNPRINTF(SWM_D_SPAWN, "spawn_custom: result: ");
5625 for (i = 0; i < prog->argc; i++)
5626 DNPRINTF(SWM_D_SPAWN, "\"%s\" ", real_args[i]);
5627 DNPRINTF(SWM_D_SPAWN, "\n");
5629 *ret_args = real_args;
5630 return (prog->argc);
5634 spawn_custom(struct swm_region *r, union arg *args, const char *spawn_name)
5640 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
5644 spawn(r->ws->idx, &a, 1);
5646 for (i = 0; i < spawn_argc; i++)
5652 spawn_select(struct swm_region *r, union arg *args, const char *spawn_name,
5659 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
5663 if (pipe(select_list_pipe) == -1)
5664 err(1, "pipe error");
5665 if (pipe(select_resp_pipe) == -1)
5666 err(1, "pipe error");
5668 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
5669 err(1, "could not disable SIGPIPE");
5670 switch (*pid = fork()) {
5672 err(1, "cannot fork");
5675 if (dup2(select_list_pipe[0], 0) == -1)
5677 if (dup2(select_resp_pipe[1], 1) == -1)
5679 close(select_list_pipe[1]);
5680 close(select_resp_pipe[0]);
5681 spawn(r->ws->idx, &a, 0);
5683 default: /* parent */
5684 close(select_list_pipe[0]);
5685 close(select_resp_pipe[1]);
5689 for (i = 0; i < spawn_argc; i++)
5695 spawn_insert(const char *name, const char *args)
5697 char *arg, *cp, *ptr;
5698 struct spawn_prog *sp;
5700 DNPRINTF(SWM_D_SPAWN, "spawn_insert: %s\n", name);
5702 if ((sp = calloc(1, sizeof *sp)) == NULL)
5703 err(1, "spawn_insert: calloc");
5704 if ((sp->name = strdup(name)) == NULL)
5705 err(1, "spawn_insert: strdup");
5707 /* convert the arguments to an argument list */
5708 if ((ptr = cp = strdup(args)) == NULL)
5709 err(1, "spawn_insert: strdup");
5710 while ((arg = strsep(&ptr, " \t")) != NULL) {
5711 /* empty field; skip it */
5716 if ((sp->argv = realloc(sp->argv, sp->argc *
5717 sizeof *sp->argv)) == NULL)
5718 err(1, "spawn_insert: realloc");
5719 if ((sp->argv[sp->argc - 1] = strdup(arg)) == NULL)
5720 err(1, "spawn_insert: strdup");
5724 TAILQ_INSERT_TAIL(&spawns, sp, entry);
5725 DNPRINTF(SWM_D_SPAWN, "spawn_insert: leave\n");
5729 spawn_remove(struct spawn_prog *sp)
5733 DNPRINTF(SWM_D_SPAWN, "spawn_remove: %s\n", sp->name);
5735 TAILQ_REMOVE(&spawns, sp, entry);
5736 for (i = 0; i < sp->argc; i++)
5742 DNPRINTF(SWM_D_SPAWN, "spawn_remove: leave\n");
5746 spawn_replace(struct spawn_prog *sp, const char *name, const char *args)
5748 DNPRINTF(SWM_D_SPAWN, "spawn_replace: %s [%s]\n", sp->name, name);
5751 spawn_insert(name, args);
5753 DNPRINTF(SWM_D_SPAWN, "spawn_replace: leave\n");
5757 setspawn(const char *name, const char *args)
5759 struct spawn_prog *sp;
5761 DNPRINTF(SWM_D_SPAWN, "setspawn: %s\n", name);
5766 TAILQ_FOREACH(sp, &spawns, entry) {
5767 if (!strcmp(sp->name, name)) {
5771 spawn_replace(sp, name, args);
5772 DNPRINTF(SWM_D_SPAWN, "setspawn: leave\n");
5776 if (*args == '\0') {
5777 warnx("error: setspawn: cannot find program: %s", name);
5781 spawn_insert(name, args);
5782 DNPRINTF(SWM_D_SPAWN, "setspawn: leave\n");
5786 setconfspawn(char *selector, char *value, int flags)
5790 /* suppress unused warning since var is needed */
5793 args = expand_tilde(value);
5795 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, args);
5797 setspawn(selector, args);
5800 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done.\n");
5807 setconfspawn("term", "xterm", 0);
5808 setconfspawn("spawn_term", "xterm", 0);
5809 setconfspawn("screenshot_all", "screenshot.sh full", 0);
5810 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
5811 setconfspawn("lock", "xlock", 0);
5812 setconfspawn("initscr", "initscreen.sh", 0);
5813 setconfspawn("menu", "dmenu_run"
5816 " -nf $bar_font_color"
5818 " -sf $bar_color", 0);
5819 setconfspawn("search", "dmenu"
5823 " -nf $bar_font_color"
5825 " -sf $bar_color", 0);
5826 setconfspawn("name_workspace", "dmenu"
5830 " -nf $bar_font_color"
5832 " -sf $bar_color", 0);
5836 #define SWM_MODNAME_SIZE 32
5837 #define SWM_KEY_WS "\n+ \t"
5839 parsekeys(char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
5843 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
5844 if (mod == NULL || ks == NULL) {
5845 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
5848 if (keystr == NULL || strlen(keystr) == 0) {
5849 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
5853 *ks = XCB_NO_SYMBOL;
5855 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
5856 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
5858 cp += (long)strspn(cp, SWM_KEY_WS);
5859 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
5861 else if (!strncasecmp(name, "Mod1", SWM_MODNAME_SIZE))
5862 *mod |= XCB_MOD_MASK_1;
5863 else if (!strncasecmp(name, "Mod2", SWM_MODNAME_SIZE))
5864 *mod += XCB_MOD_MASK_2;
5865 else if (!strncmp(name, "Mod3", SWM_MODNAME_SIZE))
5866 *mod |= XCB_MOD_MASK_3;
5867 else if (!strncmp(name, "Mod4", SWM_MODNAME_SIZE))
5868 *mod |= XCB_MOD_MASK_4;
5869 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
5870 *mod |= XCB_MOD_MASK_SHIFT;
5871 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
5872 *mod |= XCB_MOD_MASK_CONTROL;
5874 *ks = XStringToKeysym(name);
5875 XConvertCase(*ks, ks, &uks);
5876 if (ks == XCB_NO_SYMBOL) {
5878 "parsekeys: invalid key %s\n",
5884 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
5889 strdupsafe(const char *str)
5894 return (strdup(str));
5898 key_insert(unsigned int mod, KeySym ks, enum keyfuncid kfid,
5899 const char *spawn_name)
5903 DNPRINTF(SWM_D_KEY, "key_insert: enter %s [%s]\n",
5904 keyfuncs[kfid].name, spawn_name);
5906 if ((kp = malloc(sizeof *kp)) == NULL)
5907 err(1, "key_insert: malloc");
5912 kp->spawn_name = strdupsafe(spawn_name);
5913 RB_INSERT(key_tree, &keys, kp);
5915 DNPRINTF(SWM_D_KEY, "key_insert: leave\n");
5919 key_lookup(unsigned int mod, KeySym ks)
5926 return (RB_FIND(key_tree, &keys, &kp));
5930 key_remove(struct key *kp)
5932 DNPRINTF(SWM_D_KEY, "key_remove: %s\n", keyfuncs[kp->funcid].name);
5934 RB_REMOVE(key_tree, &keys, kp);
5935 free(kp->spawn_name);
5938 DNPRINTF(SWM_D_KEY, "key_remove: leave\n");
5942 key_replace(struct key *kp, unsigned int mod, KeySym ks, enum keyfuncid kfid,
5943 const char *spawn_name)
5945 DNPRINTF(SWM_D_KEY, "key_replace: %s [%s]\n", keyfuncs[kp->funcid].name,
5949 key_insert(mod, ks, kfid, spawn_name);
5951 DNPRINTF(SWM_D_KEY, "key_replace: leave\n");
5955 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid,
5956 const char *spawn_name)
5960 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
5961 keyfuncs[kfid].name, spawn_name);
5963 if ((kp = key_lookup(mod, ks)) != NULL) {
5964 if (kfid == KF_INVALID)
5967 key_replace(kp, mod, ks, kfid, spawn_name);
5968 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
5971 if (kfid == KF_INVALID) {
5972 warnx("bind: Key combination already unbound.");
5973 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
5977 key_insert(mod, ks, kfid, spawn_name);
5978 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
5982 setconfbinding(char *selector, char *value, int flags)
5984 enum keyfuncid kfid;
5987 struct spawn_prog *sp;
5989 /* suppress unused warning since var is needed */
5992 DNPRINTF(SWM_D_KEY, "setconfbinding: enter\n");
5993 if (selector == NULL) {
5994 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
5995 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
5997 setkeybinding(mod, ks, kfid, NULL);
6002 /* search by key function name */
6003 for (kfid = 0; kfid < KF_INVALID; (kfid)++) {
6004 if (strncasecmp(selector, keyfuncs[kfid].name,
6005 SWM_FUNCNAME_LEN) == 0) {
6006 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
6008 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
6009 setkeybinding(mod, ks, kfid, NULL);
6015 /* search by custom spawn name */
6016 TAILQ_FOREACH(sp, &spawns, entry) {
6017 if (strcasecmp(selector, sp->name) == 0) {
6018 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
6020 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
6021 setkeybinding(mod, ks, KF_SPAWN_CUSTOM,
6028 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
6035 #define MODKEY_SHIFT MODKEY | XCB_MOD_MASK_SHIFT
6036 setkeybinding(MODKEY, XK_space, KF_CYCLE_LAYOUT,NULL);
6037 setkeybinding(MODKEY_SHIFT, XK_backslash, KF_FLIP_LAYOUT, NULL);
6038 setkeybinding(MODKEY_SHIFT, XK_space, KF_STACK_RESET, NULL);
6039 setkeybinding(MODKEY, XK_h, KF_MASTER_SHRINK, NULL);
6040 setkeybinding(MODKEY, XK_l, KF_MASTER_GROW, NULL);
6041 setkeybinding(MODKEY, XK_comma, KF_MASTER_ADD, NULL);
6042 setkeybinding(MODKEY, XK_period, KF_MASTER_DEL, NULL);
6043 setkeybinding(MODKEY_SHIFT, XK_comma, KF_STACK_INC, NULL);
6044 setkeybinding(MODKEY_SHIFT, XK_period, KF_STACK_DEC, NULL);
6045 setkeybinding(MODKEY, XK_Return, KF_SWAP_MAIN, NULL);
6046 setkeybinding(MODKEY, XK_j, KF_FOCUS_NEXT, NULL);
6047 setkeybinding(MODKEY, XK_k, KF_FOCUS_PREV, NULL);
6048 setkeybinding(MODKEY_SHIFT, XK_j, KF_SWAP_NEXT, NULL);
6049 setkeybinding(MODKEY_SHIFT, XK_k, KF_SWAP_PREV, NULL);
6050 setkeybinding(MODKEY_SHIFT, XK_Return, KF_SPAWN_CUSTOM,"term");
6051 setkeybinding(MODKEY, XK_p, KF_SPAWN_CUSTOM,"menu");
6052 setkeybinding(MODKEY_SHIFT, XK_q, KF_QUIT, NULL);
6053 setkeybinding(MODKEY, XK_q, KF_RESTART, NULL);
6054 setkeybinding(MODKEY, XK_m, KF_FOCUS_MAIN, NULL);
6055 setkeybinding(MODKEY, XK_1, KF_WS_1, NULL);
6056 setkeybinding(MODKEY, XK_2, KF_WS_2, NULL);
6057 setkeybinding(MODKEY, XK_3, KF_WS_3, NULL);
6058 setkeybinding(MODKEY, XK_4, KF_WS_4, NULL);
6059 setkeybinding(MODKEY, XK_5, KF_WS_5, NULL);
6060 setkeybinding(MODKEY, XK_6, KF_WS_6, NULL);
6061 setkeybinding(MODKEY, XK_7, KF_WS_7, NULL);
6062 setkeybinding(MODKEY, XK_8, KF_WS_8, NULL);
6063 setkeybinding(MODKEY, XK_9, KF_WS_9, NULL);
6064 setkeybinding(MODKEY, XK_0, KF_WS_10, NULL);
6065 setkeybinding(MODKEY, XK_F1, KF_WS_11, NULL);
6066 setkeybinding(MODKEY, XK_F2, KF_WS_12, NULL);
6067 setkeybinding(MODKEY, XK_F3, KF_WS_13, NULL);
6068 setkeybinding(MODKEY, XK_F4, KF_WS_14, NULL);
6069 setkeybinding(MODKEY, XK_F5, KF_WS_15, NULL);
6070 setkeybinding(MODKEY, XK_F6, KF_WS_16, NULL);
6071 setkeybinding(MODKEY, XK_F7, KF_WS_17, NULL);
6072 setkeybinding(MODKEY, XK_F8, KF_WS_18, NULL);
6073 setkeybinding(MODKEY, XK_F9, KF_WS_19, NULL);
6074 setkeybinding(MODKEY, XK_F10, KF_WS_20, NULL);
6075 setkeybinding(MODKEY, XK_F11, KF_WS_21, NULL);
6076 setkeybinding(MODKEY, XK_F12, KF_WS_22, NULL);
6077 setkeybinding(MODKEY, XK_Right, KF_WS_NEXT, NULL);
6078 setkeybinding(MODKEY, XK_Left, KF_WS_PREV, NULL);
6079 setkeybinding(MODKEY, XK_Up, KF_WS_NEXT_ALL, NULL);
6080 setkeybinding(MODKEY, XK_Down, KF_WS_PREV_ALL, NULL);
6081 setkeybinding(MODKEY, XK_a, KF_WS_PRIOR, NULL);
6082 setkeybinding(MODKEY_SHIFT, XK_Right, KF_SCREEN_NEXT, NULL);
6083 setkeybinding(MODKEY_SHIFT, XK_Left, KF_SCREEN_PREV, NULL);
6084 setkeybinding(MODKEY_SHIFT, XK_1, KF_MVWS_1, NULL);
6085 setkeybinding(MODKEY_SHIFT, XK_2, KF_MVWS_2, NULL);
6086 setkeybinding(MODKEY_SHIFT, XK_3, KF_MVWS_3, NULL);
6087 setkeybinding(MODKEY_SHIFT, XK_4, KF_MVWS_4, NULL);
6088 setkeybinding(MODKEY_SHIFT, XK_5, KF_MVWS_5, NULL);
6089 setkeybinding(MODKEY_SHIFT, XK_6, KF_MVWS_6, NULL);
6090 setkeybinding(MODKEY_SHIFT, XK_7, KF_MVWS_7, NULL);
6091 setkeybinding(MODKEY_SHIFT, XK_8, KF_MVWS_8, NULL);
6092 setkeybinding(MODKEY_SHIFT, XK_9, KF_MVWS_9, NULL);
6093 setkeybinding(MODKEY_SHIFT, XK_0, KF_MVWS_10, NULL);
6094 setkeybinding(MODKEY_SHIFT, XK_F1, KF_MVWS_11, NULL);
6095 setkeybinding(MODKEY_SHIFT, XK_F2, KF_MVWS_12, NULL);
6096 setkeybinding(MODKEY_SHIFT, XK_F3, KF_MVWS_13, NULL);
6097 setkeybinding(MODKEY_SHIFT, XK_F4, KF_MVWS_14, NULL);
6098 setkeybinding(MODKEY_SHIFT, XK_F5, KF_MVWS_15, NULL);
6099 setkeybinding(MODKEY_SHIFT, XK_F6, KF_MVWS_16, NULL);
6100 setkeybinding(MODKEY_SHIFT, XK_F7, KF_MVWS_17, NULL);
6101 setkeybinding(MODKEY_SHIFT, XK_F8, KF_MVWS_18, NULL);
6102 setkeybinding(MODKEY_SHIFT, XK_F9, KF_MVWS_19, NULL);
6103 setkeybinding(MODKEY_SHIFT, XK_F10, KF_MVWS_20, NULL);
6104 setkeybinding(MODKEY_SHIFT, XK_F11, KF_MVWS_21, NULL);
6105 setkeybinding(MODKEY_SHIFT, XK_F12, KF_MVWS_22, NULL);
6106 setkeybinding(MODKEY, XK_b, KF_BAR_TOGGLE, NULL);
6107 setkeybinding(MODKEY_SHIFT, XK_b, KF_BAR_TOGGLE_WS,NULL);
6108 setkeybinding(MODKEY, XK_Tab, KF_FOCUS_NEXT, NULL);
6109 setkeybinding(MODKEY_SHIFT, XK_Tab, KF_FOCUS_PREV, NULL);
6110 setkeybinding(MODKEY_SHIFT, XK_x, KF_WIND_KILL, NULL);
6111 setkeybinding(MODKEY, XK_x, KF_WIND_DEL, NULL);
6112 setkeybinding(MODKEY, XK_s, KF_SPAWN_CUSTOM,"screenshot_all");
6113 setkeybinding(MODKEY_SHIFT, XK_s, KF_SPAWN_CUSTOM,"screenshot_wind");
6114 setkeybinding(MODKEY, XK_t, KF_FLOAT_TOGGLE,NULL);
6115 setkeybinding(MODKEY_SHIFT, XK_v, KF_VERSION, NULL);
6116 setkeybinding(MODKEY_SHIFT, XK_Delete, KF_SPAWN_CUSTOM,"lock");
6117 setkeybinding(MODKEY_SHIFT, XK_i, KF_SPAWN_CUSTOM,"initscr");
6118 setkeybinding(MODKEY, XK_w, KF_ICONIFY, NULL);
6119 setkeybinding(MODKEY_SHIFT, XK_w, KF_UNICONIFY, NULL);
6120 setkeybinding(MODKEY_SHIFT, XK_r, KF_RAISE_TOGGLE,NULL);
6121 setkeybinding(MODKEY, XK_v, KF_BUTTON2, NULL);
6122 setkeybinding(MODKEY, XK_equal, KF_WIDTH_GROW, NULL);
6123 setkeybinding(MODKEY, XK_minus, KF_WIDTH_SHRINK,NULL);
6124 setkeybinding(MODKEY_SHIFT, XK_equal, KF_HEIGHT_GROW,NULL);
6125 setkeybinding(MODKEY_SHIFT, XK_minus, KF_HEIGHT_SHRINK,NULL);
6126 setkeybinding(MODKEY, XK_bracketleft, KF_MOVE_LEFT,NULL);
6127 setkeybinding(MODKEY, XK_bracketright,KF_MOVE_RIGHT,NULL);
6128 setkeybinding(MODKEY_SHIFT, XK_bracketleft, KF_MOVE_UP, NULL);
6129 setkeybinding(MODKEY_SHIFT, XK_bracketright,KF_MOVE_DOWN,NULL);
6130 setkeybinding(MODKEY_SHIFT, XK_slash, KF_NAME_WORKSPACE,NULL);
6131 setkeybinding(MODKEY, XK_slash, KF_SEARCH_WORKSPACE,NULL);
6132 setkeybinding(MODKEY, XK_f, KF_SEARCH_WIN, NULL);
6134 setkeybinding(MODKEY_SHIFT, XK_d, KF_DUMPWINS, NULL);
6144 while (RB_EMPTY(&keys) == 0) {
6145 kp = RB_ROOT(&keys);
6151 setkeymapping(char *selector, char *value, int flags)
6153 char keymapping_file[PATH_MAX];
6155 /* suppress unused warnings since vars are needed */
6159 DNPRINTF(SWM_D_KEY, "setkeymapping: enter\n");
6160 if (value[0] == '~')
6161 snprintf(keymapping_file, sizeof keymapping_file, "%s/%s",
6162 pwd->pw_dir, &value[1]);
6164 strlcpy(keymapping_file, value, sizeof keymapping_file);
6166 /* load new key bindings; if it fails, revert to default bindings */
6167 if (conf_load(keymapping_file, SWM_CONF_KEYMAPPING)) {
6171 DNPRINTF(SWM_D_KEY, "setkeymapping: leave\n");
6176 updatenumlockmask(void)
6179 xcb_get_modifier_mapping_reply_t *modmap_r;
6180 xcb_keycode_t *modmap, kc, *keycode;
6184 modmap_r = xcb_get_modifier_mapping_reply(conn,
6185 xcb_get_modifier_mapping(conn),
6188 modmap = xcb_get_modifier_mapping_keycodes(modmap_r);
6189 for (i = 0; i < 8; i++) {
6190 for (j = 0; j < modmap_r->keycodes_per_modifier; j++) {
6191 kc = modmap[i * modmap_r->keycodes_per_modifier
6193 keycode = xcb_key_symbols_get_keycode(syms,
6196 numlockmask = (1 << i);
6202 DNPRINTF(SWM_D_MISC, "updatenumlockmask: %d\n", numlockmask);
6209 int num_screens, k, j;
6210 unsigned int modifiers[4];
6211 xcb_keycode_t *code;
6213 DNPRINTF(SWM_D_MISC, "grabkeys\n");
6214 updatenumlockmask();
6217 modifiers[1] = numlockmask;
6218 modifiers[2] = XCB_MOD_MASK_LOCK;
6219 modifiers[3] = numlockmask | XCB_MOD_MASK_LOCK;
6221 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
6222 for (k = 0; k < num_screens; k++) {
6223 if (TAILQ_EMPTY(&screens[k].rl))
6225 xcb_ungrab_key(conn, XCB_GRAB_ANY, screens[k].root,
6227 RB_FOREACH(kp, key_tree, &keys) {
6228 if ((code = xcb_key_symbols_get_keycode(syms,
6230 for (j = 0; j < LENGTH(modifiers); j++)
6231 xcb_grab_key(conn, 1,
6233 kp->mod | modifiers[j],
6234 *code, XCB_GRAB_MODE_SYNC,
6235 XCB_GRAB_MODE_ASYNC);
6243 grabbuttons(struct ws_win *win)
6245 unsigned int modifiers[4];
6248 DNPRINTF(SWM_D_MOUSE, "grabbuttons: win 0x%x\n", win->id);
6249 updatenumlockmask();
6252 modifiers[1] = numlockmask;
6253 modifiers[2] = XCB_MOD_MASK_LOCK;
6254 modifiers[3] = numlockmask | XCB_MOD_MASK_LOCK;
6256 for (i = 0; i < LENGTH(buttons); i++)
6257 if (buttons[i].action == client_click)
6258 for (j = 0; j < LENGTH(modifiers); ++j)
6259 xcb_grab_button(conn, 0, win->id, BUTTONMASK,
6260 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC,
6261 XCB_WINDOW_NONE, XCB_CURSOR_NONE,
6262 buttons[i].button, buttons[i].mask |
6266 const char *quirkname[] = {
6267 "NONE", /* config string for "no value" */
6276 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
6277 #define SWM_Q_WS "\n|+ \t"
6279 parsequirks(char *qstr, unsigned long *quirk)
6289 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
6291 cp += (long)strspn(cp, SWM_Q_WS);
6292 for (i = 0; i < LENGTH(quirkname); i++) {
6293 if (!strncasecmp(name, quirkname[i], SWM_QUIRK_LEN)) {
6294 DNPRINTF(SWM_D_QUIRK,
6295 "parsequirks: %s\n", name);
6300 *quirk |= 1 << (i-1);
6304 if (i >= LENGTH(quirkname)) {
6305 DNPRINTF(SWM_D_QUIRK,
6306 "parsequirks: invalid quirk [%s]\n", name);
6314 quirk_insert(const char *class, const char *name, unsigned long quirk)
6318 DNPRINTF(SWM_D_QUIRK, "quirk_insert: %s:%s [%lu]\n", class, name,
6321 if ((qp = malloc(sizeof *qp)) == NULL)
6322 err(1, "quirk_insert: malloc");
6323 if ((qp->class = strdup(class)) == NULL)
6324 err(1, "quirk_insert: strdup");
6325 if ((qp->name = strdup(name)) == NULL)
6326 err(1, "quirk_insert: strdup");
6329 TAILQ_INSERT_TAIL(&quirks, qp, entry);
6331 DNPRINTF(SWM_D_QUIRK, "quirk_insert: leave\n");
6335 quirk_remove(struct quirk *qp)
6337 DNPRINTF(SWM_D_QUIRK, "quirk_remove: %s:%s [%lu]\n", qp->class,
6338 qp->name, qp->quirk);
6340 TAILQ_REMOVE(&quirks, qp, entry);
6345 DNPRINTF(SWM_D_QUIRK, "quirk_remove: leave\n");
6349 quirk_replace(struct quirk *qp, const char *class, const char *name,
6350 unsigned long quirk)
6352 DNPRINTF(SWM_D_QUIRK, "quirk_replace: %s:%s [%lu]\n", qp->class,
6353 qp->name, qp->quirk);
6356 quirk_insert(class, name, quirk);
6358 DNPRINTF(SWM_D_QUIRK, "quirk_replace: leave\n");
6362 setquirk(const char *class, const char *name, unsigned long quirk)
6366 DNPRINTF(SWM_D_QUIRK, "setquirk: enter %s:%s [%lu]\n", class, name,
6369 /* Remove/replace existing quirk. */
6370 TAILQ_FOREACH(qp, &quirks, entry) {
6371 if (!strcmp(qp->class, class) && !strcmp(qp->name, name)) {
6375 quirk_replace(qp, class, name, quirk);
6376 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
6381 /* Only insert if quirk is not NONE. */
6383 quirk_insert(class, name, quirk);
6385 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
6389 setconfquirk(char *selector, char *value, int flags)
6391 char *cp, *class, *name;
6395 /* suppress unused warning since var is needed */
6398 if (selector == NULL)
6400 if ((cp = strchr(selector, ':')) == NULL)
6405 if ((retval = parsequirks(value, &qrks)) == 0)
6406 setquirk(class, name, qrks);
6413 setquirk("MPlayer", "xv", SWM_Q_FLOAT | SWM_Q_FULLSCREEN | SWM_Q_FOCUSPREV);
6414 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT);
6415 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ);
6416 setquirk("Firefox", "Dialog", SWM_Q_FLOAT);
6417 setquirk("Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
6418 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ);
6419 setquirk("xine", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
6420 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
6421 setquirk("xine", "xine Panel", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
6422 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
6423 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
6424 setquirk("pcb", "pcb", SWM_Q_FLOAT);
6425 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
6428 /* conf file stuff */
6429 #define SWM_CONF_FILE "spectrwm.conf"
6430 #define SWM_CONF_FILE_OLD "scrotwm.conf"
6434 SWM_S_BAR_AT_BOTTOM,
6435 SWM_S_BAR_BORDER_WIDTH,
6438 SWM_S_BAR_ENABLED_WS,
6443 SWM_S_CLOCK_ENABLED,
6446 SWM_S_CYCLE_VISIBLE,
6448 SWM_S_DISABLE_BORDER,
6450 SWM_S_FOCUS_CLOSE_WRAP,
6451 SWM_S_FOCUS_DEFAULT,
6457 SWM_S_STACK_ENABLED,
6459 SWM_S_TITLE_CLASS_ENABLED,
6460 SWM_S_TITLE_NAME_ENABLED,
6461 SWM_S_URGENT_ENABLED,
6462 SWM_S_VERBOSE_LAYOUT,
6463 SWM_S_WINDOW_NAME_ENABLED,
6464 SWM_S_WORKSPACE_LIMIT
6468 setconfvalue(char *selector, char *value, int flags)
6470 struct workspace *ws;
6471 int i, ws_id, num_screens;
6474 /* suppress unused warning since var is needed */
6478 case SWM_S_BAR_ACTION:
6480 if ((bar_argv[0] = expand_tilde(value)) == NULL)
6481 err(1, "setconfvalue: bar_action");
6483 case SWM_S_BAR_AT_BOTTOM:
6484 bar_at_bottom = atoi(value);
6486 case SWM_S_BAR_BORDER_WIDTH:
6487 bar_border_width = atoi(value);
6488 if (bar_border_width < 0)
6489 bar_border_width = 0;
6491 case SWM_S_BAR_DELAY:
6492 /* No longer needed; leave to not break old conf files. */
6494 case SWM_S_BAR_ENABLED:
6495 bar_enabled = atoi(value);
6497 case SWM_S_BAR_ENABLED_WS:
6498 ws_id = atoi(selector) - 1;
6499 if (ws_id < 0 || ws_id >= workspace_limit)
6500 errx(1, "setconfvalue: bar_enabled_ws: invalid "
6501 "workspace %d.", ws_id + 1);
6503 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
6504 for (i = 0; i < num_screens; i++) {
6505 ws = (struct workspace *)&screens[i].ws;
6506 ws[ws_id].bar_enabled = atoi(value);
6509 case SWM_S_BAR_FONT:
6511 if (asprintf(&bar_fonts, "%s,%s", value, bar_fonts) == -1)
6512 err(1, "setconfvalue: asprintf: failed to allocate "
6513 "memory for bar_fonts.");
6516 /* If already in xft mode, then we are done. */
6517 if (!bar_font_legacy)
6520 /* If there are any non-XLFD entries, switch to Xft mode. */
6521 while ((b = strsep(&value, ",")) != NULL) {
6525 bar_font_legacy = 0;
6530 case SWM_S_BAR_FORMAT:
6532 if ((bar_format = strdup(value)) == NULL)
6533 err(1, "setconfvalue: bar_format");
6535 case SWM_S_BAR_JUSTIFY:
6536 if (!strcmp(value, "left"))
6537 bar_justify = SWM_BAR_JUSTIFY_LEFT;
6538 else if (!strcmp(value, "center"))
6539 bar_justify = SWM_BAR_JUSTIFY_CENTER;
6540 else if (!strcmp(value, "right"))
6541 bar_justify = SWM_BAR_JUSTIFY_RIGHT;
6543 errx(1, "invalid bar_justify");
6545 case SWM_S_BORDER_WIDTH:
6546 border_width = atoi(value);
6547 if (border_width < 0)
6550 case SWM_S_CLOCK_ENABLED:
6551 clock_enabled = atoi(value);
6553 case SWM_S_CLOCK_FORMAT:
6554 #ifndef SWM_DENY_CLOCK_FORMAT
6556 if ((clock_format = strdup(value)) == NULL)
6557 err(1, "setconfvalue: clock_format");
6560 case SWM_S_CYCLE_EMPTY:
6561 cycle_empty = atoi(value);
6563 case SWM_S_CYCLE_VISIBLE:
6564 cycle_visible = atoi(value);
6566 case SWM_S_DIALOG_RATIO:
6567 dialog_ratio = atof(value);
6568 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
6571 case SWM_S_DISABLE_BORDER:
6572 disable_border = atoi(value);
6574 case SWM_S_FOCUS_CLOSE:
6575 if (!strcmp(value, "first"))
6576 focus_close = SWM_STACK_BOTTOM;
6577 else if (!strcmp(value, "last"))
6578 focus_close = SWM_STACK_TOP;
6579 else if (!strcmp(value, "next"))
6580 focus_close = SWM_STACK_ABOVE;
6581 else if (!strcmp(value, "previous"))
6582 focus_close = SWM_STACK_BELOW;
6584 errx(1, "focus_close");
6586 case SWM_S_FOCUS_CLOSE_WRAP:
6587 focus_close_wrap = atoi(value);
6589 case SWM_S_FOCUS_DEFAULT:
6590 if (!strcmp(value, "last"))
6591 focus_default = SWM_STACK_TOP;
6592 else if (!strcmp(value, "first"))
6593 focus_default = SWM_STACK_BOTTOM;
6595 errx(1, "focus_default");
6597 case SWM_S_FOCUS_MODE:
6598 if (!strcmp(value, "default"))
6599 focus_mode = SWM_FOCUS_DEFAULT;
6600 else if (!strcmp(value, "follow") ||
6601 !strcmp(value, "follow_cursor"))
6602 focus_mode = SWM_FOCUS_FOLLOW;
6603 else if (!strcmp(value, "manual"))
6604 focus_mode = SWM_FOCUS_MANUAL;
6606 errx(1, "focus_mode");
6608 case SWM_S_SPAWN_ORDER:
6609 if (!strcmp(value, "first"))
6610 spawn_position = SWM_STACK_BOTTOM;
6611 else if (!strcmp(value, "last"))
6612 spawn_position = SWM_STACK_TOP;
6613 else if (!strcmp(value, "next"))
6614 spawn_position = SWM_STACK_ABOVE;
6615 else if (!strcmp(value, "previous"))
6616 spawn_position = SWM_STACK_BELOW;
6618 errx(1, "spawn_position");
6620 case SWM_S_SPAWN_TERM:
6621 setconfspawn("term", value, 0);
6622 setconfspawn("spawn_term", value, 0);
6626 case SWM_S_SS_ENABLED:
6627 ss_enabled = atoi(value);
6629 case SWM_S_STACK_ENABLED:
6630 stack_enabled = atoi(value);
6632 case SWM_S_TERM_WIDTH:
6633 term_width = atoi(value);
6637 case SWM_S_TITLE_CLASS_ENABLED:
6638 title_class_enabled = atoi(value);
6640 case SWM_S_TITLE_NAME_ENABLED:
6641 title_name_enabled = atoi(value);
6643 case SWM_S_URGENT_ENABLED:
6644 urgent_enabled = atoi(value);
6646 case SWM_S_VERBOSE_LAYOUT:
6647 verbose_layout = atoi(value);
6648 for (i = 0; layouts[i].l_stack != NULL; i++) {
6650 layouts[i].l_string = fancy_stacker;
6652 layouts[i].l_string = plain_stacker;
6655 case SWM_S_WINDOW_NAME_ENABLED:
6656 window_name_enabled = atoi(value);
6658 case SWM_S_WORKSPACE_LIMIT:
6659 workspace_limit = atoi(value);
6660 if (workspace_limit > SWM_WS_MAX)
6661 workspace_limit = SWM_WS_MAX;
6662 else if (workspace_limit < 1)
6663 workspace_limit = 1;
6672 setconfmodkey(char *selector, char *value, int flags)
6674 /* suppress unused warnings since vars are needed */
6678 if (!strncasecmp(value, "Mod1", strlen("Mod1")))
6679 update_modkey(XCB_MOD_MASK_1);
6680 else if (!strncasecmp(value, "Mod2", strlen("Mod2")))
6681 update_modkey(XCB_MOD_MASK_2);
6682 else if (!strncasecmp(value, "Mod3", strlen("Mod3")))
6683 update_modkey(XCB_MOD_MASK_3);
6684 else if (!strncasecmp(value, "Mod4", strlen("Mod4")))
6685 update_modkey(XCB_MOD_MASK_4);
6692 setconfcolor(char *selector, char *value, int flags)
6694 setscreencolor(value, ((selector == NULL)?-1:atoi(selector)), flags);
6699 setconfregion(char *selector, char *value, int flags)
6701 /* suppress unused warnings since vars are needed */
6705 custom_region(value);
6710 setautorun(char *selector, char *value, int flags)
6720 /* suppress unused warnings since vars are needed */
6724 if (getenv("SWM_STARTED"))
6728 if (sscanf(value, "ws[%d]:%1023c", &ws_id, s) != 2)
6729 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
6731 if (ws_id < 0 || ws_id >= workspace_limit)
6732 errx(1, "autorun: invalid workspace %d", ws_id + 1);
6734 sp = expand_tilde((char *)&s);
6737 * This is a little intricate
6739 * If the pid already exists we simply reuse it because it means it was
6740 * used before AND not claimed by manage_window. We get away with
6741 * altering it in the parent after INSERT because this can not be a race
6744 while ((ap = strsep(&sp, " \t")) != NULL) {
6747 DNPRINTF(SWM_D_SPAWN, "setautorun: arg [%s]\n", ap);
6749 if ((a.argv = realloc(a.argv, argc * sizeof(char *))) == NULL)
6750 err(1, "setautorun: realloc");
6751 a.argv[argc - 1] = ap;
6755 if ((a.argv = realloc(a.argv, (argc + 1) * sizeof(char *))) == NULL)
6756 err(1, "setautorun: realloc");
6757 a.argv[argc] = NULL;
6759 if ((pid = fork()) == 0) {
6760 spawn(ws_id, &a, 1);
6769 p = calloc(1, sizeof *p);
6772 TAILQ_INSERT_TAIL(&pidlist, p, entry);
6782 setlayout(char *selector, char *value, int flags)
6784 int ws_id, i, x, mg, ma, si, ar, f = 0;
6785 int st = SWM_V_STACK, num_screens;
6787 struct workspace *ws;
6789 /* suppress unused warnings since vars are needed */
6793 if (getenv("SWM_STARTED"))
6797 if (sscanf(value, "ws[%d]:%d:%d:%d:%d:%1023c",
6798 &ws_id, &mg, &ma, &si, &ar, s) != 6)
6799 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
6800 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
6803 if (ws_id < 0 || ws_id >= workspace_limit)
6804 errx(1, "layout: invalid workspace %d", ws_id + 1);
6806 if (!strcasecmp(s, "vertical"))
6808 else if (!strcasecmp(s, "vertical_flip")) {
6811 } else if (!strcasecmp(s, "horizontal"))
6813 else if (!strcasecmp(s, "horizontal_flip")) {
6816 } else if (!strcasecmp(s, "fullscreen"))
6819 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
6820 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
6823 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
6824 for (i = 0; i < num_screens; i++) {
6825 ws = (struct workspace *)&screens[i].ws;
6826 ws[ws_id].cur_layout = &layouts[st];
6828 ws[ws_id].always_raise = ar;
6829 if (st == SWM_MAX_STACK)
6833 for (x = 0; x < abs(mg); x++) {
6834 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6835 mg >= 0 ? SWM_ARG_ID_MASTERGROW :
6836 SWM_ARG_ID_MASTERSHRINK);
6840 for (x = 0; x < abs(ma); x++) {
6841 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6842 ma >= 0 ? SWM_ARG_ID_MASTERADD :
6843 SWM_ARG_ID_MASTERDEL);
6847 for (x = 0; x < abs(si); x++) {
6848 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6849 si >= 0 ? SWM_ARG_ID_STACKINC :
6850 SWM_ARG_ID_STACKDEC);
6855 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6856 SWM_ARG_ID_FLIPLAYOUT);
6866 /* config options */
6867 struct config_option {
6869 int (*func)(char*, char*, int);
6872 struct config_option configopt[] = {
6873 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
6874 { "bar_enabled_ws", setconfvalue, SWM_S_BAR_ENABLED_WS },
6875 { "bar_at_bottom", setconfvalue, SWM_S_BAR_AT_BOTTOM },
6876 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
6877 { "bar_border_width", setconfvalue, SWM_S_BAR_BORDER_WIDTH },
6878 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
6879 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
6880 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
6881 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
6882 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
6883 { "bar_justify", setconfvalue, SWM_S_BAR_JUSTIFY },
6884 { "bar_format", setconfvalue, SWM_S_BAR_FORMAT },
6885 { "keyboard_mapping", setkeymapping, 0 },
6886 { "bind", setconfbinding, 0 },
6887 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
6888 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
6889 { "clock_format", setconfvalue, SWM_S_CLOCK_FORMAT },
6890 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
6891 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
6892 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
6893 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
6894 { "workspace_limit", setconfvalue, SWM_S_WORKSPACE_LIMIT },
6895 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
6896 { "verbose_layout", setconfvalue, SWM_S_VERBOSE_LAYOUT },
6897 { "modkey", setconfmodkey, 0 },
6898 { "program", setconfspawn, 0 },
6899 { "quirk", setconfquirk, 0 },
6900 { "region", setconfregion, 0 },
6901 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
6902 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
6903 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
6904 { "window_name_enabled", setconfvalue, SWM_S_WINDOW_NAME_ENABLED },
6905 { "urgent_enabled", setconfvalue, SWM_S_URGENT_ENABLED },
6906 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
6907 { "title_class_enabled", setconfvalue, SWM_S_TITLE_CLASS_ENABLED },
6908 { "title_name_enabled", setconfvalue, SWM_S_TITLE_NAME_ENABLED },
6909 { "focus_mode", setconfvalue, SWM_S_FOCUS_MODE },
6910 { "focus_close", setconfvalue, SWM_S_FOCUS_CLOSE },
6911 { "focus_close_wrap", setconfvalue, SWM_S_FOCUS_CLOSE_WRAP },
6912 { "focus_default", setconfvalue, SWM_S_FOCUS_DEFAULT },
6913 { "spawn_position", setconfvalue, SWM_S_SPAWN_ORDER },
6914 { "disable_border", setconfvalue, SWM_S_DISABLE_BORDER },
6915 { "border_width", setconfvalue, SWM_S_BORDER_WIDTH },
6916 { "autorun", setautorun, 0 },
6917 { "layout", setlayout, 0 },
6921 conf_load(const char *filename, int keymapping)
6924 char *line, *cp, *optsub, *optval;
6925 size_t linelen, lineno = 0;
6926 int wordlen, i, optidx;
6927 struct config_option *opt = NULL;
6929 DNPRINTF(SWM_D_CONF, "conf_load: begin\n");
6931 if (filename == NULL) {
6932 warnx("conf_load: no filename");
6935 if ((config = fopen(filename, "r")) == NULL) {
6936 warn("conf_load: fopen: %s", filename);
6940 while (!feof(config)) {
6941 if ((line = fparseln(config, &linelen, &lineno, NULL, 0))
6944 err(1, "%s", filename);
6949 cp += strspn(cp, " \t\n"); /* eat whitespace */
6950 if (cp[0] == '\0') {
6955 /* get config option */
6956 wordlen = strcspn(cp, "=[ \t\n");
6958 warnx("%s: line %zd: no option found",
6963 for (i = 0; i < LENGTH(configopt); i++) {
6964 opt = &configopt[i];
6965 if (!strncasecmp(cp, opt->optname, wordlen) &&
6966 (int)strlen(opt->optname) == wordlen) {
6972 warnx("%s: line %zd: unknown option %.*s",
6973 filename, lineno, wordlen, cp);
6976 if (keymapping && opt && strcmp(opt->optname, "bind")) {
6977 warnx("%s: line %zd: invalid option %.*s",
6978 filename, lineno, wordlen, cp);
6982 cp += strspn(cp, " \t\n"); /* eat whitespace */
6983 /* get [selector] if any */
6987 wordlen = strcspn(cp, "]");
6990 warnx("%s: line %zd: syntax error",
6995 if (asprintf(&optsub, "%.*s", wordlen, cp) ==
6997 warnx("%s: line %zd: unable to allocate"
6998 "memory for selector", filename,
7004 cp += strspn(cp, "] \t\n"); /* eat trailing */
7006 cp += strspn(cp, "= \t\n"); /* eat trailing */
7008 optval = strdup(cp);
7009 /* call function to deal with it all */
7010 if (configopt[optidx].func(optsub, optval,
7011 configopt[optidx].funcflags) != 0)
7012 errx(1, "%s: line %zd: invalid data for %s",
7013 filename, lineno, configopt[optidx].optname);
7020 DNPRINTF(SWM_D_CONF, "conf_load: end\n");
7027 DNPRINTF(SWM_D_CONF, "conf_load: end with error.\n");
7033 set_child_transient(struct ws_win *win, xcb_window_t *trans)
7035 struct ws_win *parent, *w;
7036 struct swm_region *r;
7037 struct workspace *ws;
7038 xcb_icccm_wm_hints_t wmh;
7040 parent = find_window(win->transient);
7042 parent->focus_child = win;
7044 DNPRINTF(SWM_D_MISC, "set_child_transient: parent doesn't exist"
7045 " for 0x%x trans 0x%x\n", win->id, win->transient);
7047 r = root_to_region(win->wa->root, SWM_CK_ALL);
7049 /* parent doen't exist in our window list */
7050 TAILQ_FOREACH(w, &ws->winlist, entry) {
7051 if (xcb_icccm_get_wm_hints_reply(conn,
7052 xcb_icccm_get_wm_hints(conn, w->id),
7054 warnx("can't get hints for 0x%x", w->id);
7058 if (win->hints.window_group != wmh.window_group)
7061 w->focus_child = win;
7062 win->transient = w->id;
7064 DNPRINTF(SWM_D_MISC, "set_child_transient: adjusting "
7065 "transient to 0x%x\n", win->transient);
7072 window_get_pid(xcb_window_t win)
7077 xcb_get_property_cookie_t pc;
7078 xcb_get_property_reply_t *pr;
7080 apid = get_atom_from_string("_NET_WM_PID");
7081 if (apid == XCB_ATOM_NONE)
7084 pc = xcb_get_property(conn, 0, win, apid, XCB_ATOM_CARDINAL, 0, 1);
7085 pr = xcb_get_property_reply(conn, pc, NULL);
7088 if (pr->type != XCB_ATOM_CARDINAL) {
7093 if (pr->type == apid && pr->format == 32)
7094 ret = *((pid_t *)xcb_get_property_value(pr));
7100 apid = get_atom_from_string("_SWM_PID");
7101 pc = xcb_get_property(conn, 0, win, apid, XCB_ATOM_STRING,
7103 pr = xcb_get_property_reply(conn, pc, NULL);
7106 if (pr->type != apid) {
7111 ret = (pid_t)strtonum(xcb_get_property_value(pr), 0, INT_MAX, &errstr);
7118 get_ws_idx(xcb_window_t id)
7124 xcb_get_property_reply_t *gpr;
7126 gpr = xcb_get_property_reply(conn,
7127 xcb_get_property(conn, 0, id, a_swm_ws,
7128 XCB_ATOM_STRING, 0, SWM_PROPLEN),
7133 proplen = xcb_get_property_value_length(gpr);
7135 prop = malloc(proplen + 1);
7138 xcb_get_property_value(gpr),
7140 prop[proplen] = '\0';
7147 DNPRINTF(SWM_D_PROP, "get_ws_idx: _SWM_WS: %s\n", prop);
7148 ws_idx = (int)strtonum(prop, 0, workspace_limit - 1, &errstr);
7150 DNPRINTF(SWM_D_PROP, "get_ws_idx: window: #%s: %s",
7160 manage_window(xcb_window_t id, uint16_t mapped)
7162 xcb_window_t trans = XCB_WINDOW_NONE;
7163 struct ws_win *win, *ww;
7165 char ws_idx_str[SWM_PROPLEN];
7166 struct swm_region *r;
7170 xcb_icccm_get_wm_protocols_reply_t wpr;
7172 if ((win = find_window(id)) != NULL) {
7173 DNPRINTF(SWM_D_MISC, "manage_window: win 0x%x already "
7174 "managed; skipping.)\n", id);
7175 return (win); /* Already managed. */
7178 /* See if window is on the unmanaged list. */
7179 if ((win = find_unmanaged_window(id)) != NULL) {
7180 DNPRINTF(SWM_D_MISC, "manage_window: win 0x%x found on "
7181 "unmanaged list.\n", id);
7182 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
7185 set_child_transient(win, &trans);
7189 DNPRINTF(SWM_D_MISC, "manage_window: win 0x%x is new.\n", id);
7192 /* Create and initialize ws_win object. */
7193 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
7194 err(1, "manage_window: calloc: failed to allocate memory for "
7199 /* Get window geometry. */
7200 win->wa = xcb_get_geometry_reply(conn,
7201 xcb_get_geometry(conn, win->id),
7204 /* Figure out which region the window belongs to. */
7205 r = root_to_region(win->wa->root, SWM_CK_ALL);
7207 /* Ignore window border if there is one. */
7208 WIDTH(win) = win->wa->width;
7209 HEIGHT(win) = win->wa->height;
7210 X(win) = win->wa->x + win->wa->border_width - border_width;
7211 Y(win) = win->wa->y + win->wa->border_width - border_width;
7213 win->mapped = mapped;
7214 win->floatmaxed = 0;
7215 win->ewmh_flags = 0;
7216 win->s = r->s; /* this never changes */
7218 store_float_geom(win, r);
7220 /* Get WM_SIZE_HINTS. */
7221 xcb_icccm_get_wm_normal_hints_reply(conn,
7222 xcb_icccm_get_wm_normal_hints(conn, win->id),
7226 xcb_icccm_get_wm_hints_reply(conn,
7227 xcb_icccm_get_wm_hints(conn, win->id),
7230 /* Get WM_TRANSIENT_FOR; see if window is a transient. */
7231 xcb_icccm_get_wm_transient_for_reply(conn,
7232 xcb_icccm_get_wm_transient_for(conn, win->id),
7235 win->transient = trans;
7236 set_child_transient(win, &win->transient);
7239 /* Get supported protocols. */
7240 if (xcb_icccm_get_wm_protocols_reply(conn,
7241 xcb_icccm_get_wm_protocols(conn, win->id, a_prot),
7243 for (i = 0; i < wpr.atoms_len; i++) {
7244 if (wpr.atoms[i] == a_takefocus)
7245 win->take_focus = 1;
7246 if (wpr.atoms[i] == a_delete)
7247 win->can_delete = 1;
7249 xcb_icccm_get_wm_protocols_reply_wipe(&wpr);
7252 win->iconic = get_swm_iconic(win);
7254 /* Figure out which workspace the window belongs to. */
7255 if ((p = find_pid(window_get_pid(win->id))) != NULL) {
7256 win->ws = &r->s->ws[p->ws];
7257 TAILQ_REMOVE(&pidlist, p, entry);
7260 } else if ((ws_idx = get_ws_idx(win->id)) != -1 &&
7262 /* _SWM_WS is set; use that. */
7263 win->ws = &r->s->ws[ws_idx];
7264 } else if (trans && (ww = find_window(trans)) != NULL) {
7265 /* Launch transients in the same ws as parent. */
7271 /* Set the _SWM_WS atom so we can remember this after reincarnation. */
7272 if (snprintf(ws_idx_str, SWM_PROPLEN, "%d", win->ws->idx) <
7274 DNPRINTF(SWM_D_PROP, "manage_window: set _SWM_WS: %s\n",
7276 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
7277 a_swm_ws, XCB_ATOM_STRING, 8, strlen(ws_idx_str),
7282 ewmh_autoquirk(win);
7284 /* Determine initial quirks. */
7285 if (xcb_icccm_get_wm_class_reply(conn,
7286 xcb_icccm_get_wm_class(conn, win->id),
7288 DNPRINTF(SWM_D_CLASS, "manage_window: class: %s, name: %s\n",
7289 win->ch.class_name, win->ch.instance_name);
7291 /* java is retarded so treat it special */
7292 if (strstr(win->ch.instance_name, "sun-awt")) {
7293 DNPRINTF(SWM_D_CLASS, "manage_window: java window "
7298 TAILQ_FOREACH(qp, &quirks, entry) {
7299 if (!strcmp(win->ch.class_name, qp->class) &&
7300 !strcmp(win->ch.instance_name, qp->name)) {
7301 DNPRINTF(SWM_D_CLASS, "manage_window: on quirks"
7302 "list; mask: 0x%lx\n", qp->quirk);
7303 if (qp->quirk & SWM_Q_FLOAT)
7305 win->quirks = qp->quirk;
7310 /* Alter window position if quirky */
7311 if (win->quirks & SWM_Q_ANYWHERE)
7314 /* Reset font sizes (the bruteforce way; no default keybinding). */
7315 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
7316 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
7317 fake_keypress(win, XK_KP_Subtract, XCB_MOD_MASK_SHIFT);
7318 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
7319 fake_keypress(win, XK_KP_Add, XCB_MOD_MASK_SHIFT);
7322 /* Make sure window is positioned inside its region, if its active. */
7324 constrain_window(win, win->ws->r, 0);
7328 /* Select which X events to monitor and set border pixel color. */
7329 wa[0] = win->s->c[SWM_S_COLOR_UNFOCUS].pixel;
7330 wa[1] = XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_PROPERTY_CHANGE |
7331 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
7333 wa[1] |= XCB_EVENT_MASK_LEAVE_WINDOW | XCB_EVENT_MASK_FOCUS_CHANGE;
7336 xcb_change_window_attributes(conn, win->id, XCB_CW_BORDER_PIXEL |
7337 XCB_CW_EVENT_MASK, wa);
7340 /* Figure out where to stack the window in the workspace. */
7341 if (trans && (ww = find_window(trans)))
7342 TAILQ_INSERT_AFTER(&win->ws->winlist, ww, win, entry);
7343 else if (win->ws->focus && spawn_position == SWM_STACK_ABOVE)
7344 TAILQ_INSERT_AFTER(&win->ws->winlist, win->ws->focus, win,
7346 else if (win->ws->focus && spawn_position == SWM_STACK_BELOW)
7347 TAILQ_INSERT_BEFORE(win->ws->focus, win, entry);
7348 else switch (spawn_position) {
7351 case SWM_STACK_ABOVE:
7352 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
7354 case SWM_STACK_BOTTOM:
7355 case SWM_STACK_BELOW:
7356 TAILQ_INSERT_HEAD(&win->ws->winlist, win, entry);
7359 /* Get initial _NET_WM_STATE */
7360 ewmh_get_win_state(win);
7361 /* Set initial _NET_WM_ALLOWED_ACTIONS */
7362 ewmh_update_actions(win);
7366 DNPRINTF(SWM_D_MISC, "manage_window: done. window: 0x%x, (x,y) w x h: "
7367 "(%d,%d) %d x %d, ws: %d, iconic: %s, transient: 0x%x\n", win->id,
7368 X(win), Y(win), WIDTH(win), HEIGHT(win), win->ws->idx,
7369 YESNO(win->iconic), win->transient);
7375 free_window(struct ws_win *win)
7377 DNPRINTF(SWM_D_MISC, "free_window: window: 0x%x\n", win->id);
7382 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
7387 xcb_icccm_get_wm_class_reply_wipe(&win->ch);
7392 memset(win, 0xff, sizeof *win); /* XXX kill later */
7395 DNPRINTF(SWM_D_MISC, "free_window: done.\n");
7399 unmanage_window(struct ws_win *win)
7401 struct ws_win *parent;
7406 DNPRINTF(SWM_D_MISC, "unmanage_window: window: 0x%x\n", win->id);
7408 if (win->transient) {
7409 parent = find_window(win->transient);
7411 parent->focus_child = NULL;
7414 TAILQ_REMOVE(&win->ws->winlist, win, entry);
7415 TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
7419 expose(xcb_expose_event_t *e)
7422 struct swm_region *r;
7424 DNPRINTF(SWM_D_EVENT, "expose: window: 0x%x\n", e->window);
7426 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7427 for (i = 0; i < num_screens; i++)
7428 TAILQ_FOREACH(r, &screens[i].rl, entry)
7429 if (e->window == WINID(r->bar))
7437 focusin(xcb_focus_in_event_t *e)
7439 DNPRINTF(SWM_D_EVENT, "focusin: window: 0x%x, mode: %s(%u), "
7440 "detail: %s(%u)\n", e->event, get_notify_mode_label(e->mode),
7441 e->mode, get_notify_detail_label(e->detail), e->detail);
7445 focusout(xcb_focus_out_event_t *e)
7447 DNPRINTF(SWM_D_EVENT, "focusout: window: 0x%x, mode: %s(%u), "
7448 "detail: %s(%u)\n", e->event, get_notify_mode_label(e->mode),
7449 e->mode, get_notify_detail_label(e->detail), e->detail);
7454 keypress(xcb_key_press_event_t *e)
7456 xcb_keysym_t keysym;
7459 keysym = xcb_key_press_lookup_keysym(syms, e, 0);
7461 DNPRINTF(SWM_D_EVENT, "keypress: %u %u\n", e->detail, keysym);
7463 if ((kp = key_lookup(CLEANMASK(e->state), keysym)) == NULL)
7466 last_event_time = e->time;
7468 if (kp->funcid == KF_SPAWN_CUSTOM)
7469 spawn_custom(root_to_region(e->root, SWM_CK_ALL),
7470 &(keyfuncs[kp->funcid].args), kp->spawn_name);
7471 else if (keyfuncs[kp->funcid].func)
7472 keyfuncs[kp->funcid].func(root_to_region(e->root, SWM_CK_ALL),
7473 &(keyfuncs[kp->funcid].args));
7477 buttonpress(xcb_button_press_event_t *e)
7479 struct ws_win *win = NULL;
7480 struct swm_region *r, *old_r;
7484 DNPRINTF(SWM_D_EVENT, "buttonpress: win (x,y): 0x%x (%d,%d), "
7485 "detail: %u, time: %u, root (x,y): 0x%x (%d,%d), child: 0x%x, "
7486 "state: %u, same_screen: %s\n", e->event, e->event_x, e->event_y,
7487 e->detail, e->time, e->root, e->root_x, e->root_y, e->child,
7488 e->state, YESNO(e->same_screen));
7490 if (e->event == e->root) {
7491 if (e->child != 0) {
7492 win = find_window(e->child);
7493 /* Pass ButtonPress to window if it isn't managed. */
7497 /* Focus on empty region */
7498 /* If no windows on region if its empty. */
7499 r = root_to_region(e->root, SWM_CK_POINTER);
7501 DNPRINTF(SWM_D_EVENT, "buttonpress: "
7502 "NULL region; ignoring.\n");
7506 if (TAILQ_EMPTY(&r->ws->winlist)) {
7507 old_r = root_to_region(e->root, SWM_CK_FOCUS);
7508 if (old_r && old_r != r)
7509 unfocus_win(old_r->ws->focus);
7511 xcb_set_input_focus(conn,
7512 XCB_INPUT_FOCUS_PARENT, e->root, e->time);
7514 /* Clear bar since empty. */
7522 win = find_window(e->event);
7528 last_event_time = e->time;
7530 focus_win(get_focus_magic(win));
7532 for (i = 0; i < LENGTH(buttons); i++)
7533 if (client_click == buttons[i].action && buttons[i].func &&
7534 buttons[i].button == e->detail &&
7535 CLEANMASK(buttons[i].mask) == CLEANMASK(e->state)) {
7536 buttons[i].func(win, &buttons[i].args);
7542 DNPRINTF(SWM_D_EVENT, "buttonpress: passing to window.\n");
7543 /* Replay event to event window */
7544 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, e->time);
7546 DNPRINTF(SWM_D_EVENT, "buttonpress: handled.\n");
7547 /* Unfreeze grab events. */
7548 xcb_allow_events(conn, XCB_ALLOW_SYNC_POINTER, e->time);
7556 print_win_geom(xcb_window_t w)
7558 xcb_get_geometry_reply_t *wa;
7560 wa = xcb_get_geometry_reply(conn, xcb_get_geometry(conn, w), NULL);
7563 DNPRINTF(SWM_D_MISC, "print_win_geom: window not found: 0x%x\n",
7568 DNPRINTF(SWM_D_MISC, "print_win_geom: window: 0x%x, root: 0x%x, "
7569 "depth: %u, (x,y) w x h: (%d,%d) %d x %d, border: %d\n",
7570 w, wa->root, wa->depth, wa->x, wa->y, wa->width, wa->height,
7579 get_stack_mode_name(uint8_t mode)
7584 case XCB_STACK_MODE_ABOVE:
7587 case XCB_STACK_MODE_BELOW:
7590 case XCB_STACK_MODE_TOP_IF:
7593 case XCB_STACK_MODE_BOTTOM_IF:
7596 case XCB_STACK_MODE_OPPOSITE:
7608 configurerequest(xcb_configure_request_event_t *e)
7611 struct swm_region *r = NULL;
7614 uint32_t wc[7] = {0};
7616 if ((win = find_window(e->window)) == NULL)
7617 if ((win = find_unmanaged_window(e->window)) == NULL)
7621 if (swm_debug & SWM_D_EVENT) {
7622 print_win_geom(e->window);
7624 DNPRINTF(SWM_D_EVENT, "configurerequest: window: 0x%x, "
7625 "parent: 0x%x, new: %s, value_mask: %u { ", e->window,
7626 e->parent, YESNO(new), e->value_mask);
7627 if (e->value_mask & XCB_CONFIG_WINDOW_X)
7628 DPRINTF("X: %d ", e->x);
7629 if (e->value_mask & XCB_CONFIG_WINDOW_Y)
7630 DPRINTF("Y: %d ", e->y);
7631 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH)
7632 DPRINTF("W: %u ", e->width);
7633 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
7634 DPRINTF("H: %u ", e->height);
7635 if (e->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH)
7636 DPRINTF("Border: %u ", e->border_width);
7637 if (e->value_mask & XCB_CONFIG_WINDOW_SIBLING)
7638 DPRINTF("Sibling: 0x%x ", e->sibling);
7639 if (e->value_mask & XCB_CONFIG_WINDOW_STACK_MODE)
7640 DPRINTF("StackMode: %s(%u) ",
7641 get_stack_mode_name(e->stack_mode), e->stack_mode);
7647 if (e->value_mask & XCB_CONFIG_WINDOW_X) {
7648 mask |= XCB_CONFIG_WINDOW_X;
7651 if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
7652 mask |= XCB_CONFIG_WINDOW_Y;
7655 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
7656 mask |= XCB_CONFIG_WINDOW_WIDTH;
7659 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
7660 mask |= XCB_CONFIG_WINDOW_HEIGHT;
7661 wc[i++] = e->height;
7663 if (e->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH) {
7664 mask |= XCB_CONFIG_WINDOW_BORDER_WIDTH;
7665 wc[i++] = e->border_width;
7667 if (e->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
7668 mask |= XCB_CONFIG_WINDOW_SIBLING;
7669 wc[i++] = e->sibling;
7671 if (e->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
7672 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
7673 wc[i++] = e->stack_mode;
7677 xcb_configure_window(conn, e->window, mask, wc);
7680 } else if ((!win->manual || win->quirks & SWM_Q_ANYWHERE) &&
7681 !(win->ewmh_flags & EWMH_F_FULLSCREEN)) {
7684 else if (win->ws->old_r)
7687 /* windows are centered unless ANYWHERE quirk is set. */
7688 if (win->quirks & SWM_Q_ANYWHERE) {
7689 if (e->value_mask & XCB_CONFIG_WINDOW_X) {
7690 win->g_float.x = e->x;
7692 win->g_float.x -= X(r);
7695 if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
7696 win->g_float.y = e->y;
7698 win->g_float.y -= Y(r);
7702 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH)
7703 win->g_float.w = e->width;
7705 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
7706 win->g_float.h = e->height;
7708 win->g_floatvalid = 1;
7710 if (win->floating && r && (win->transient ||
7711 win->ws->cur_layout != &layouts[SWM_MAX_STACK])) {
7712 WIDTH(win) = win->g_float.w;
7713 HEIGHT(win) = win->g_float.h;
7716 stack_floater(win, r);
7728 DNPRINTF(SWM_D_EVENT, "configurerequest: done.\n");
7732 configurenotify(xcb_configure_notify_event_t *e)
7736 DNPRINTF(SWM_D_EVENT, "configurenotify: win 0x%x, event win: 0x%x, "
7737 "(x,y) WxH: (%d,%d) %ux%u, border: %u, above_sibling: 0x%x, "
7738 "override_redirect: %s\n", e->window, e->event, e->x, e->y,
7739 e->width, e->height, e->border_width, e->above_sibling,
7740 YESNO(e->override_redirect));
7742 win = find_window(e->window);
7744 xcb_icccm_get_wm_normal_hints_reply(conn,
7745 xcb_icccm_get_wm_normal_hints(conn, win->id),
7748 if (font_adjusted) {
7756 destroynotify(xcb_destroy_notify_event_t *e)
7760 DNPRINTF(SWM_D_EVENT, "destroynotify: window: 0x%x\n", e->window);
7762 if ((win = find_window(e->window)) == NULL) {
7763 if ((win = find_unmanaged_window(e->window)) == NULL)
7769 if (focus_mode != SWM_FOCUS_FOLLOW) {
7770 /* If we were focused, make sure we focus on something else. */
7771 if (win == win->ws->focus)
7772 win->ws->focus_pending = get_focus_prev(win);
7775 unmanage_window(win);
7778 if (focus_mode != SWM_FOCUS_FOLLOW) {
7779 if (win->ws->focus_pending) {
7780 focus_win(win->ws->focus_pending);
7781 win->ws->focus_pending = NULL;
7792 get_notify_detail_label(uint8_t detail)
7797 case XCB_NOTIFY_DETAIL_ANCESTOR:
7800 case XCB_NOTIFY_DETAIL_VIRTUAL:
7803 case XCB_NOTIFY_DETAIL_INFERIOR:
7806 case XCB_NOTIFY_DETAIL_NONLINEAR:
7807 label = "Nonlinear";
7809 case XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL:
7810 label = "NonlinearVirtual";
7812 case XCB_NOTIFY_DETAIL_POINTER:
7815 case XCB_NOTIFY_DETAIL_POINTER_ROOT:
7816 label = "PointerRoot";
7818 case XCB_NOTIFY_DETAIL_NONE:
7829 get_notify_mode_label(uint8_t mode)
7834 case XCB_NOTIFY_MODE_NORMAL:
7837 case XCB_NOTIFY_MODE_GRAB:
7840 case XCB_NOTIFY_MODE_UNGRAB:
7843 case XCB_NOTIFY_MODE_WHILE_GRABBED:
7844 label = "WhileGrabbed";
7855 enternotify(xcb_enter_notify_event_t *e)
7858 struct swm_region *old_r, *r;
7860 DNPRINTF(SWM_D_FOCUS, "enternotify: time: %u, win (x,y): 0x%x "
7861 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): 0x%x (%d,%d), "
7862 "child: 0x%x, same_screen_focus: %s, state: %d\n",
7863 e->time, e->event, e->event_x, e->event_y,
7864 get_notify_mode_label(e->mode), e->mode,
7865 get_notify_detail_label(e->detail), e->detail,
7866 e->root, e->root_x, e->root_y, e->child,
7867 YESNO(e->same_screen_focus), e->state);
7869 if (focus_mode == SWM_FOCUS_MANUAL &&
7870 e->mode == XCB_NOTIFY_MODE_NORMAL) {
7871 DNPRINTF(SWM_D_EVENT, "enternotify: manual focus; ignoring.\n");
7875 last_event_time = e->time;
7877 if ((win = find_window(e->event)) == NULL) {
7878 if (e->event == e->root) {
7879 /* If no windows on pointer region, then focus root. */
7880 r = root_to_region(e->root, SWM_CK_POINTER);
7882 DNPRINTF(SWM_D_EVENT, "enterntoify: "
7883 "NULL region; ignoring.\n");
7887 if (TAILQ_EMPTY(&r->ws->winlist)) {
7888 old_r = root_to_region(e->root, SWM_CK_FOCUS);
7889 if (old_r && old_r != r)
7890 unfocus_win(old_r->ws->focus);
7892 xcb_set_input_focus(conn,
7893 XCB_INPUT_FOCUS_PARENT, e->root, e->time);
7895 /* Clear bar since empty. */
7901 DNPRINTF(SWM_D_EVENT, "enternotify: window is NULL; "
7907 focus_win(get_focus_magic(win));
7914 leavenotify(xcb_leave_notify_event_t *e)
7916 DNPRINTF(SWM_D_FOCUS, "leavenotify: time: %u, win (x,y): 0x%x "
7917 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): 0x%x (%d,%d), "
7918 "child: 0x%x, same_screen_focus: %s, state: %d\n",
7919 e->time, e->event, e->event_x, e->event_y,
7920 get_notify_mode_label(e->mode), e->mode,
7921 get_notify_detail_label(e->detail), e->detail,
7922 e->root, e->root_x, e->root_y, e->child,
7923 YESNO(e->same_screen_focus), e->state);
7928 mapnotify(xcb_map_notify_event_t *e)
7932 DNPRINTF(SWM_D_EVENT, "mapnotify: window: 0x%x\n", e->window);
7934 if ((win = find_window(e->window)) == NULL)
7935 win = manage_window(e->window, 1);
7938 set_win_state(win, XCB_ICCCM_WM_STATE_NORMAL);
7940 if (focus_mode != SWM_FOCUS_FOLLOW) {
7941 if (win->ws->focus_pending == win) {
7943 win->ws->focus_pending = NULL;
7952 mappingnotify(xcb_mapping_notify_event_t *e)
7954 xcb_refresh_keyboard_mapping(syms, e);
7956 if (e->request == XCB_MAPPING_KEYBOARD)
7961 maprequest(xcb_map_request_event_t *e)
7964 xcb_get_window_attributes_reply_t *war;
7966 DNPRINTF(SWM_D_EVENT, "maprequest: win 0x%x\n",
7969 war = xcb_get_window_attributes_reply(conn,
7970 xcb_get_window_attributes(conn, e->window),
7973 DNPRINTF(SWM_D_EVENT, "maprequest: window lost.\n");
7977 if (war->override_redirect) {
7978 DNPRINTF(SWM_D_EVENT, "maprequest: override_redirect; "
7983 win = manage_window(e->window,
7984 (war->map_state == XCB_MAP_STATE_VIEWABLE));
7986 /* The new window should get focus; prepare. */
7987 if (focus_mode != SWM_FOCUS_FOLLOW)
7988 win->ws->focus_pending = get_focus_magic(win);
7990 /* All windows need to be mapped if they are in the current workspace.*/
7994 /* Ignore EnterNotify to handle the mapnotify without interference. */
7995 if (focus_mode == SWM_FOCUS_DEFAULT)
7996 event_drain(XCB_ENTER_NOTIFY);
7999 DNPRINTF(SWM_D_EVENT, "maprequest: done.\n");
8004 get_atom_name(xcb_atom_t atom)
8009 * This should be disabled during most debugging since
8010 * xcb_get_* causes an xcb_flush.
8013 xcb_get_atom_name_reply_t *r;
8015 r = xcb_get_atom_name_reply(conn,
8016 xcb_get_atom_name(conn, atom),
8019 len = xcb_get_atom_name_name_length(r);
8021 name = malloc(len + 1);
8023 memcpy(name, xcb_get_atom_name_name(r), len);
8037 propertynotify(xcb_property_notify_event_t *e)
8043 name = get_atom_name(e->atom);
8044 DNPRINTF(SWM_D_EVENT, "propertynotify: window: 0x%x, atom: %s(%u), "
8045 "time: %#x, state: %u\n", e->window, name, e->atom, e->time,
8049 win = find_window(e->window);
8053 last_event_time = e->time;
8055 if (e->atom == a_swm_iconic) {
8056 if (e->state == XCB_PROPERTY_NEW_VALUE) {
8057 if (focus_mode != SWM_FOCUS_FOLLOW)
8058 win->ws->focus_pending = get_focus_prev(win);
8065 if (focus_mode != SWM_FOCUS_FOLLOW) {
8066 focus_win(win->ws->focus_pending);
8067 win->ws->focus_pending = NULL;
8071 } else if (e->state == XCB_PROPERTY_DELETE) {
8072 /* The window is no longer iconic, restack ws. */
8073 if (focus_mode != SWM_FOCUS_FOLLOW)
8074 win->ws->focus_pending = get_focus_magic(win);
8078 /* Flush EnterNotify for mapnotify, if needed. */
8081 } else if (e->atom == a_state) {
8082 /* State just changed, make sure it gets focused if mapped. */
8083 if (e->state == XCB_PROPERTY_NEW_VALUE) {
8084 if (focus_mode != SWM_FOCUS_FOLLOW) {
8086 win->ws->focus_pending == win) {
8087 focus_win(win->ws->focus_pending);
8088 win->ws->focus_pending = NULL;
8092 } else if (e->atom == XCB_ATOM_WM_CLASS ||
8093 e->atom == XCB_ATOM_WM_NAME) {
8101 unmapnotify(xcb_unmap_notify_event_t *e)
8104 struct workspace *ws;
8106 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: 0x%x\n", e->window);
8108 /* If we aren't managing the window, then ignore. */
8109 win = find_window(e->window);
8115 if (getstate(e->window) != XCB_ICCCM_WM_STATE_ICONIC)
8116 set_win_state(win, XCB_ICCCM_WM_STATE_ICONIC);
8119 /* window unmapped itself */
8120 /* do unmap/unfocus/restack and unmanage */
8123 /* If win was focused, make sure to focus on something else. */
8124 if (win == ws->focus) {
8125 if (focus_mode != SWM_FOCUS_FOLLOW) {
8126 ws->focus_pending = get_focus_prev(win);
8127 DNPRINTF(SWM_D_EVENT, "unmapnotify: "
8128 "focus_pending: 0x%x\n",
8129 WINID(ws->focus_pending));
8135 unmanage_window(win);
8140 if (focus_mode == SWM_FOCUS_FOLLOW) {
8142 focus_win(get_pointer_win(ws->r->s->root));
8144 if (ws->focus_pending) {
8145 focus_win(ws->focus_pending);
8146 ws->focus_pending = NULL;
8151 if (getstate(e->window) == XCB_ICCCM_WM_STATE_NORMAL)
8152 set_win_state(win, XCB_ICCCM_WM_STATE_ICONIC);
8159 visibilitynotify(xcb_visibility_notify_event_t *e)
8161 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: 0x%x\n",
8167 clientmessage(xcb_client_message_event_t *e)
8170 xcb_map_request_event_t mre;
8174 name = get_atom_name(e->type);
8175 DNPRINTF(SWM_D_EVENT, "clientmessage: window: 0x%x, atom: %s(%u)\n",
8176 e->window, name, e->type);
8179 win = find_window(e->window);
8182 if (e->type == ewmh[_NET_ACTIVE_WINDOW].atom) {
8183 /* Manage the window with maprequest. */
8184 DNPRINTF(SWM_D_EVENT, "clientmessage: request focus on "
8185 "unmanaged window.\n");
8186 mre.window = e->window;
8192 if (e->type == ewmh[_NET_ACTIVE_WINDOW].atom) {
8193 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_ACTIVE_WINDOW\n");
8196 if (e->type == ewmh[_NET_CLOSE_WINDOW].atom) {
8197 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_CLOSE_WINDOW\n");
8198 if (win->can_delete)
8199 client_msg(win, a_delete, 0);
8201 xcb_kill_client(conn, win->id);
8203 if (e->type == ewmh[_NET_MOVERESIZE_WINDOW].atom) {
8204 DNPRINTF(SWM_D_EVENT,
8205 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
8206 if (win->floating) {
8207 if (e->data.data32[0] & (1<<8)) /* x */
8208 X(win) = e->data.data32[1];
8209 if (e->data.data32[0] & (1<<9)) /* y */
8210 Y(win) = e->data.data32[2];
8211 if (e->data.data32[0] & (1<<10)) /* width */
8212 WIDTH(win) = e->data.data32[3];
8213 if (e->data.data32[0] & (1<<11)) /* height */
8214 HEIGHT(win) = e->data.data32[4];
8219 /* TODO: Change stack sizes */
8220 /* notify no change was made. */
8221 config_win(win, NULL);
8224 if (e->type == ewmh[_NET_WM_STATE].atom) {
8225 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_WM_STATE\n");
8226 ewmh_update_win_state(win, e->data.data32[1], e->data.data32[0]);
8227 if (e->data.data32[2])
8228 ewmh_update_win_state(win, e->data.data32[2],
8240 int errcode = xcb_connection_has_error(conn);
8241 #ifdef XCB_CONN_ERROR
8244 case XCB_CONN_ERROR:
8245 s = "Socket error, pipe error or other stream error.";
8247 case XCB_CONN_CLOSED_EXT_NOTSUPPORTED:
8248 s = "Extension not supported.";
8250 case XCB_CONN_CLOSED_MEM_INSUFFICIENT:
8251 s = "Insufficient memory.";
8253 case XCB_CONN_CLOSED_REQ_LEN_EXCEED:
8254 s = "Request length was exceeded.";
8256 case XCB_CONN_CLOSED_PARSE_ERR:
8257 s = "Error parsing display string.";
8260 s = "Unknown error.";
8263 errx(errcode, "X CONNECTION ERROR: %s", s);
8266 errx(errcode, "X CONNECTION ERROR");
8274 const uint32_t val = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
8275 XCB_EVENT_MASK_ENTER_WINDOW;
8277 xcb_void_cookie_t wac;
8278 xcb_generic_error_t *error;
8280 /* this causes an error if some other window manager is running */
8281 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
8282 for (i = 0; i < num_screens; i++) {
8283 if ((sc = get_screen(i)) == NULL)
8284 errx(1, "ERROR: can't get screen %d.", i);
8285 DNPRINTF(SWM_D_INIT, "enable_wm: screen %d, root: 0x%x\n",
8287 wac = xcb_change_window_attributes_checked(conn, sc->root,
8288 XCB_CW_EVENT_MASK, &val);
8289 if ((error = xcb_request_check(conn, wac))) {
8290 DNPRINTF(SWM_D_INIT, "enable_wm: error_code: %u\n",
8296 /* click to focus on empty region */
8297 xcb_grab_button(conn, 1, sc->root, BUTTONMASK,
8298 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC, XCB_WINDOW_NONE,
8299 XCB_CURSOR_NONE, XCB_BUTTON_INDEX_1, XCB_BUTTON_MASK_ANY);
8306 new_region(struct swm_screen *s, int x, int y, int w, int h)
8308 struct swm_region *r, *n;
8309 struct workspace *ws = NULL;
8312 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
8313 s->idx, w, h, x, y);
8315 /* remove any conflicting regions */
8316 n = TAILQ_FIRST(&s->rl);
8319 n = TAILQ_NEXT(r, entry);
8320 if (X(r) < (x + w) &&
8321 (X(r) + WIDTH(r)) > x &&
8323 (Y(r) + HEIGHT(r)) > y) {
8324 if (r->ws->r != NULL)
8325 r->ws->old_r = r->ws->r;
8328 TAILQ_REMOVE(&s->rl, r, entry);
8329 TAILQ_INSERT_TAIL(&s->orl, r, entry);
8333 /* search old regions for one to reuse */
8335 /* size + location match */
8336 TAILQ_FOREACH(r, &s->orl, entry)
8337 if (X(r) == x && Y(r) == y &&
8338 HEIGHT(r) == h && WIDTH(r) == w)
8342 TAILQ_FOREACH(r, &s->orl, entry)
8343 if (HEIGHT(r) == h && WIDTH(r) == w)
8347 TAILQ_REMOVE(&s->orl, r, entry);
8348 /* try to use old region's workspace */
8349 if (r->ws->r == NULL)
8352 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
8353 err(1, "new_region: calloc: failed to allocate memory "
8356 /* if we don't have a workspace already, find one */
8358 for (i = 0; i < workspace_limit; i++)
8359 if (s->ws[i].r == NULL) {
8366 errx(1, "new_region: no free workspaces");
8377 TAILQ_INSERT_TAIL(&s->rl, r, entry);
8383 #ifdef SWM_XRR_HAS_CRTC
8386 #endif /* SWM_XRR_HAS_CRTC */
8387 struct swm_region *r;
8389 xcb_randr_get_screen_resources_current_cookie_t src;
8390 xcb_randr_get_screen_resources_current_reply_t *srr;
8391 xcb_randr_get_crtc_info_cookie_t cic;
8392 xcb_randr_get_crtc_info_reply_t *cir = NULL;
8393 xcb_randr_crtc_t *crtc;
8394 xcb_screen_t *screen;
8396 DNPRINTF(SWM_D_MISC, "scan_xrandr: screen: %d\n", i);
8398 if ((screen = get_screen(i)) == NULL)
8399 errx(1, "ERROR: can't get screen %d.", i);
8401 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
8402 if (i >= num_screens)
8403 errx(1, "scan_xrandr: invalid screen");
8405 /* remove any old regions */
8406 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
8407 r->ws->old_r = r->ws->r = NULL;
8409 TAILQ_REMOVE(&screens[i].rl, r, entry);
8410 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
8414 /* map virtual screens onto physical screens */
8415 #ifdef SWM_XRR_HAS_CRTC
8416 if (xrandr_support) {
8417 src = xcb_randr_get_screen_resources_current(conn,
8419 srr = xcb_randr_get_screen_resources_current_reply(conn, src,
8422 new_region(&screens[i], 0, 0,
8423 screen->width_in_pixels,
8424 screen->height_in_pixels);
8427 ncrtc = srr->num_crtcs;
8429 crtc = xcb_randr_get_screen_resources_current_crtcs(srr);
8430 for (c = 0; c < ncrtc; c++) {
8431 cic = xcb_randr_get_crtc_info(conn, crtc[c],
8433 cir = xcb_randr_get_crtc_info_reply(conn, cic, NULL);
8436 if (cir->num_outputs == 0) {
8442 new_region(&screens[i], 0, 0,
8443 screen->width_in_pixels,
8444 screen->height_in_pixels);
8446 new_region(&screens[i],
8447 cir->x, cir->y, cir->width, cir->height);
8452 #endif /* SWM_XRR_HAS_CRTC */
8454 /* If detection failed, create a single region that spans the screen. */
8455 if (TAILQ_EMPTY(&screens[i].rl))
8456 new_region(&screens[i], 0, 0, screen->width_in_pixels,
8457 screen->height_in_pixels);
8460 DNPRINTF(SWM_D_MISC, "scan_xrandr: done.\n");
8464 screenchange(xcb_randr_screen_change_notify_event_t *e)
8466 struct swm_region *r;
8469 DNPRINTF(SWM_D_EVENT, "screenchange: root: 0x%x\n", e->root);
8471 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
8472 /* silly event doesn't include the screen index */
8473 for (i = 0; i < num_screens; i++)
8474 if (screens[i].root == e->root)
8476 if (i >= num_screens)
8477 errx(1, "screenchange: screen not found");
8479 /* brute force for now, just re-enumerate the regions */
8483 print_win_geom(e->root);
8485 /* add bars to all regions */
8486 for (i = 0; i < num_screens; i++)
8487 TAILQ_FOREACH(r, &screens[i].rl, entry)
8495 xcb_window_t *wins = NULL, trans;
8497 int i, j, num_screens;
8498 uint16_t state, manage, mapped;
8500 xcb_query_tree_cookie_t qtc;
8501 xcb_query_tree_reply_t *qtr;
8502 xcb_get_window_attributes_cookie_t c;
8503 xcb_get_window_attributes_reply_t *r;
8504 xcb_get_property_cookie_t pc;
8506 DNPRINTF(SWM_D_INIT, "grab_windows: begin\n");
8507 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
8508 for (i = 0; i < num_screens; i++) {
8509 qtc = xcb_query_tree(conn, screens[i].root);
8510 qtr = xcb_query_tree_reply(conn, qtc, NULL);
8513 wins = xcb_query_tree_children(qtr);
8514 no = xcb_query_tree_children_length(qtr);
8515 /* attach windows to a region */
8516 /* normal windows */
8517 DNPRINTF(SWM_D_INIT, "grab_windows: grab top level windows.\n");
8518 for (j = 0; j < no; j++) {
8519 c = xcb_get_window_attributes(conn, wins[j]);
8520 r = xcb_get_window_attributes_reply(conn, c, NULL);
8522 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
8523 "doesn't exist.\n", wins[j]);
8526 if (r->override_redirect) {
8527 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
8528 "override_redirect set.\n", wins[j]);
8533 pc = xcb_icccm_get_wm_transient_for(conn, wins[j]);
8534 if (xcb_icccm_get_wm_transient_for_reply(conn, pc,
8536 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
8537 "is transient for %#x.\n", wins[j], trans);
8542 state = getstate(wins[j]);
8543 manage = state != XCB_ICCCM_WM_STATE_WITHDRAWN;
8544 mapped = r->map_state != XCB_MAP_STATE_UNMAPPED;
8545 if (mapped || manage)
8546 manage_window(wins[j], mapped);
8549 /* transient windows */
8550 DNPRINTF(SWM_D_INIT, "grab_windows: grab transient windows.\n");
8551 for (j = 0; j < no; j++) {
8552 c = xcb_get_window_attributes(conn, wins[j]);
8553 r = xcb_get_window_attributes_reply(conn, c, NULL);
8555 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
8556 "doesn't exist.\n", wins[j]);
8559 if (r->override_redirect) {
8560 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
8561 "override_redirect set.\n", wins[j]);
8566 state = getstate(wins[j]);
8567 manage = state != XCB_ICCCM_WM_STATE_WITHDRAWN;
8568 mapped = r->map_state != XCB_MAP_STATE_UNMAPPED;
8569 pc = xcb_icccm_get_wm_transient_for(conn, wins[j]);
8570 if (xcb_icccm_get_wm_transient_for_reply(conn, pc,
8571 &trans, NULL) && manage)
8572 manage_window(wins[j], mapped);
8577 DNPRINTF(SWM_D_INIT, "grab_windows: done.\n");
8583 int i, j, k, num_screens;
8584 struct workspace *ws;
8585 uint32_t gcv[1], wa[1];
8586 const xcb_query_extension_reply_t *qep;
8587 xcb_screen_t *screen;
8588 xcb_randr_query_version_cookie_t c;
8589 xcb_randr_query_version_reply_t *r;
8591 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
8592 if ((screens = calloc(num_screens,
8593 sizeof(struct swm_screen))) == NULL)
8594 err(1, "setup_screens: calloc: failed to allocate memory for "
8597 /* initial Xrandr setup */
8599 qep = xcb_get_extension_data(conn, &xcb_randr_id);
8601 c = xcb_randr_query_version(conn, 1, 1);
8602 r = xcb_randr_query_version_reply(conn, c, NULL);
8604 if (r->major_version >= 1) {
8606 xrandr_eventbase = qep->first_event;
8612 wa[0] = cursors[XC_LEFT_PTR].cid;
8614 /* map physical screens */
8615 for (i = 0; i < num_screens; i++) {
8616 DNPRINTF(SWM_D_WS, "setup_screens: init screen: %d\n", i);
8618 TAILQ_INIT(&screens[i].rl);
8619 TAILQ_INIT(&screens[i].orl);
8620 if ((screen = get_screen(i)) == NULL)
8621 errx(1, "ERROR: can't get screen %d.", i);
8622 screens[i].root = screen->root;
8624 /* set default colors */
8625 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
8626 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
8627 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
8628 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
8629 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
8631 /* create graphics context on screen */
8632 screens[i].bar_gc = xcb_generate_id(conn);
8634 xcb_create_gc(conn, screens[i].bar_gc, screens[i].root,
8635 XCB_GC_GRAPHICS_EXPOSURES, gcv);
8637 /* set default cursor */
8638 xcb_change_window_attributes(conn, screens[i].root,
8641 /* init all workspaces */
8642 /* XXX these should be dynamically allocated too */
8643 for (j = 0; j < SWM_WS_MAX; j++) {
8644 ws = &screens[i].ws[j];
8647 ws->bar_enabled = 1;
8649 ws->focus_prev = NULL;
8650 ws->focus_pending = NULL;
8653 TAILQ_INIT(&ws->winlist);
8654 TAILQ_INIT(&ws->unmanagedlist);
8656 for (k = 0; layouts[k].l_stack != NULL; k++)
8657 if (layouts[k].l_config != NULL)
8658 layouts[k].l_config(ws,
8659 SWM_ARG_ID_STACKINIT);
8660 ws->cur_layout = &layouts[0];
8661 ws->cur_layout->l_string(ws);
8667 xcb_randr_select_input(conn, screens[i].root,
8668 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE);
8675 if ((bar_fonts = strdup(SWM_BAR_FONTS)) == NULL)
8676 err(1, "setup_globals: strdup: failed to allocate memory.");
8678 if ((clock_format = strdup("%a %b %d %R %Z %Y")) == NULL)
8679 err(1, "setup_globals: strdup: failed to allocate memory.");
8681 if ((syms = xcb_key_symbols_alloc(conn)) == NULL)
8682 errx(1, "unable to allocate key symbols");
8684 a_state = get_atom_from_string("WM_STATE");
8685 a_prot = get_atom_from_string("WM_PROTOCOLS");
8686 a_delete = get_atom_from_string("WM_DELETE_WINDOW");
8687 a_takefocus = get_atom_from_string("WM_TAKE_FOCUS");
8688 a_wmname = get_atom_from_string("WM_NAME");
8689 a_netwmname = get_atom_from_string("_NET_WM_NAME");
8690 a_utf8_string = get_atom_from_string("UTF8_STRING");
8691 a_string = get_atom_from_string("STRING");
8692 a_swm_iconic = get_atom_from_string("_SWM_ICONIC");
8693 a_swm_ws = get_atom_from_string("_SWM_WS");
8700 xcb_atom_t netwmcheck;
8701 xcb_window_t root, win;
8703 /* work around sun jdk bugs, code from wmname */
8704 netwmcheck = get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
8706 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
8707 for (i = 0; i < num_screens; i++) {
8708 root = screens[i].root;
8710 win = xcb_generate_id(conn);
8711 xcb_create_window(conn, XCB_COPY_FROM_PARENT, win, root,
8712 0, 0, 1, 1, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT,
8713 XCB_COPY_FROM_PARENT, 0, NULL);
8715 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root,
8716 netwmcheck, XCB_ATOM_WINDOW, 32, 1, &win);
8717 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win,
8718 netwmcheck, XCB_ATOM_WINDOW, 32, 1, &win);
8719 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win,
8720 a_netwmname, a_utf8_string, 8, strlen("LG3D"), "LG3D");
8725 shutdown_cleanup(void)
8729 /* disable alarm because the following code may not be interrupted */
8731 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
8732 err(1, "can't disable alarm");
8742 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
8743 for (i = 0; i < num_screens; ++i) {
8744 if (screens[i].bar_gc != 0)
8745 xcb_free_gc(conn, screens[i].bar_gc);
8746 if (!bar_font_legacy)
8747 XftColorFree(display, DefaultVisual(display, i),
8748 DefaultColormap(display, i), &bar_font_color);
8751 if (bar_font_legacy)
8752 XFreeFontSet(display, bar_fs);
8754 XftFontClose(display, bar_font);
8757 xcb_key_symbols_free(syms);
8759 xcb_disconnect(conn);
8763 event_error(xcb_generic_error_t *e)
8767 DNPRINTF(SWM_D_EVENT, "event_error: %s(%u) from %s(%u), sequence: %u, "
8768 "resource_id: %u, minor_code: %u\n",
8769 xcb_event_get_error_label(e->error_code), e->error_code,
8770 xcb_event_get_request_label(e->major_code), e->major_code,
8771 e->sequence, e->resource_id, e->minor_code);
8775 event_handle(xcb_generic_event_t *evt)
8777 uint8_t type = XCB_EVENT_RESPONSE_TYPE(evt);
8779 DNPRINTF(SWM_D_EVENT, "XCB Event: %s(%d), seq %u\n",
8780 xcb_event_get_label(XCB_EVENT_RESPONSE_TYPE(evt)),
8781 XCB_EVENT_RESPONSE_TYPE(evt), evt->sequence);
8784 #define EVENT(type, callback) case type: callback((void *)evt); return
8785 EVENT(0, event_error);
8786 EVENT(XCB_BUTTON_PRESS, buttonpress);
8787 /*EVENT(XCB_BUTTON_RELEASE, buttonpress);*/
8788 /*EVENT(XCB_CIRCULATE_NOTIFY, );*/
8789 /*EVENT(XCB_CIRCULATE_REQUEST, );*/
8790 EVENT(XCB_CLIENT_MESSAGE, clientmessage);
8791 /*EVENT(XCB_COLORMAP_NOTIFY, );*/
8792 EVENT(XCB_CONFIGURE_NOTIFY, configurenotify);
8793 EVENT(XCB_CONFIGURE_REQUEST, configurerequest);
8794 /*EVENT(XCB_CREATE_NOTIFY, );*/
8795 EVENT(XCB_DESTROY_NOTIFY, destroynotify);
8796 EVENT(XCB_ENTER_NOTIFY, enternotify);
8797 EVENT(XCB_EXPOSE, expose);
8799 EVENT(XCB_FOCUS_IN, focusin);
8800 EVENT(XCB_FOCUS_OUT, focusout);
8802 /*EVENT(XCB_GRAPHICS_EXPOSURE, );*/
8803 /*EVENT(XCB_GRAVITY_NOTIFY, );*/
8804 EVENT(XCB_KEY_PRESS, keypress);
8805 /*EVENT(XCB_KEY_RELEASE, keypress);*/
8806 /*EVENT(XCB_KEYMAP_NOTIFY, );*/
8808 EVENT(XCB_LEAVE_NOTIFY, leavenotify);
8810 EVENT(XCB_MAP_NOTIFY, mapnotify);
8811 EVENT(XCB_MAP_REQUEST, maprequest);
8812 EVENT(XCB_MAPPING_NOTIFY, mappingnotify);
8813 /*EVENT(XCB_MOTION_NOTIFY, );*/
8814 /*EVENT(XCB_NO_EXPOSURE, );*/
8815 EVENT(XCB_PROPERTY_NOTIFY, propertynotify);
8816 /*EVENT(XCB_REPARENT_NOTIFY, );*/
8817 /*EVENT(XCB_RESIZE_REQUEST, );*/
8818 /*EVENT(XCB_SELECTION_CLEAR, );*/
8819 /*EVENT(XCB_SELECTION_NOTIFY, );*/
8820 /*EVENT(XCB_SELECTION_REQUEST, );*/
8821 EVENT(XCB_UNMAP_NOTIFY, unmapnotify);
8822 /*EVENT(XCB_VISIBILITY_NOTIFY, visibilitynotify);*/
8825 if (type - xrandr_eventbase == XCB_RANDR_SCREEN_CHANGE_NOTIFY)
8826 screenchange((void *)evt);
8830 main(int argc, char *argv[])
8832 struct swm_region *r, *rr;
8833 struct ws_win *winfocus = NULL;
8834 char conf[PATH_MAX], *cfile = NULL;
8836 int xfd, i, num_screens;
8837 struct sigaction sact;
8838 xcb_generic_event_t *evt;
8842 int do_bar_update = 0;
8845 /* suppress unused warning since var is needed */
8848 time_started = time(NULL);
8851 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION, buildstr);
8852 if (!setlocale(LC_CTYPE, "") || !setlocale(LC_TIME, ""))
8853 warnx("no locale support");
8855 /* handle some signals */
8856 bzero(&sact, sizeof(sact));
8857 sigemptyset(&sact.sa_mask);
8859 sact.sa_handler = sighdlr;
8860 sigaction(SIGINT, &sact, NULL);
8861 sigaction(SIGQUIT, &sact, NULL);
8862 sigaction(SIGTERM, &sact, NULL);
8863 sigaction(SIGHUP, &sact, NULL);
8865 sact.sa_handler = sighdlr;
8866 sact.sa_flags = SA_NOCLDSTOP;
8867 sigaction(SIGCHLD, &sact, NULL);
8869 if (!(display = XOpenDisplay(0)))
8870 errx(1, "can not open display");
8872 conn = XGetXCBConnection(display);
8873 if (xcb_connection_has_error(conn))
8874 errx(1, "can not get XCB connection");
8876 XSetEventQueueOwner(display, XCBOwnsEventQueue);
8878 xcb_prefetch_extension_data(conn, &xcb_randr_id);
8879 xfd = xcb_get_file_descriptor(conn);
8881 /* look for local and global conf file */
8882 pwd = getpwuid(getuid());
8884 errx(1, "invalid user: %d", getuid());
8886 xcb_grab_server(conn);
8889 /* flush all events */
8890 while ((evt = xcb_poll_for_event(conn))) {
8891 if (XCB_EVENT_RESPONSE_TYPE(evt) == 0)
8896 if (enable_wm() != 0)
8897 errx(1, "another window manager is currently running");
8899 /* Load Xcursors and/or cursorfont glyph cursors. */
8911 for (i = 0; ; i++) {
8916 snprintf(conf, sizeof conf, "%s/.%s",
8917 pwd->pw_dir, SWM_CONF_FILE);
8921 snprintf(conf, sizeof conf, "/etc/%s",
8926 snprintf(conf, sizeof conf, "%s/.%s",
8927 pwd->pw_dir, SWM_CONF_FILE_OLD);
8931 snprintf(conf, sizeof conf, "/etc/%s",
8938 if (strlen(conf) && stat(conf, &sb) != -1)
8939 if (S_ISREG(sb.st_mode)) {
8946 /* load conf (if any) */
8948 conf_load(cfile, SWM_CONF_DEFAULT);
8951 /* set some values to work around bad programs */
8953 /* grab existing windows (before we build the bars) */
8956 if (getenv("SWM_STARTED") == NULL)
8957 setenv("SWM_STARTED", "YES", 1);
8959 /* setup all bars */
8960 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
8961 for (i = 0; i < num_screens; i++)
8962 TAILQ_FOREACH(r, &screens[i].rl, entry) {
8963 if (winfocus == NULL)
8964 winfocus = TAILQ_FIRST(&r->ws->winlist);
8971 xcb_ungrab_server(conn);
8974 rd_max = xfd > STDIN_FILENO ? xfd : STDIN_FILENO;
8977 while ((evt = xcb_poll_for_event(conn))) {
8984 /* If just (re)started, set default focus if needed. */
8985 if (winfocus && focus_mode != SWM_FOCUS_FOLLOW) {
8986 rr = winfocus->ws->r;
8988 /* not a visible window */
8993 focus_win(get_region_focus(rr));
9000 FD_SET(STDIN_FILENO, &rd);
9004 num_readable = select(rd_max + 1, &rd, NULL, NULL, &tv);
9005 if (num_readable == -1 && errno != EINTR)
9006 DNPRINTF(SWM_D_MISC, "select failed");
9007 else if (num_readable > 0 && FD_ISSET(STDIN_FILENO, &rd))
9011 restart(NULL, NULL);
9019 if (do_bar_update) {