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-2014 Reginald Kennedy <rk@rejii.com>
9 * Copyright (c) 2011-2012 Lawrence Teo <lteo@lteo.net>
10 * Copyright (c) 2011-2012 Tiago Cunha <tcunha@gmx.com>
11 * Copyright (c) 2012-2013 David Hill <dhill@mindcry.org>
13 * Permission to use, copy, modify, and distribute this software for any
14 * purpose with or without fee is hereby granted, provided that the above
15 * copyright notice and this permission notice appear in all copies.
17 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
18 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
20 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
21 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
22 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
23 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 * Much code and ideas taken from dwm under the following license:
27 * MIT/X Consortium License
29 * 2006-2008 Anselm R Garbe <garbeam at gmail dot com>
30 * 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
31 * 2006-2007 Jukka Salmi <jukka at salmi dot ch>
32 * 2007 Premysl Hruby <dfenze at gmail dot com>
33 * 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
34 * 2007 Christof Musik <christof at sendfax dot de>
35 * 2007-2008 Enno Gottox Boland <gottox at s01 dot de>
36 * 2007-2008 Peter Hartlich <sgkkr at hartlich dot com>
37 * 2008 Martin Hurton <martin dot hurton at gmail dot com>
39 * Permission is hereby granted, free of charge, to any person obtaining a
40 * copy of this software and associated documentation files (the "Software"),
41 * to deal in the Software without restriction, including without limitation
42 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
43 * and/or sell copies of the Software, and to permit persons to whom the
44 * Software is furnished to do so, subject to the following conditions:
46 * The above copyright notice and this permission notice shall be included in
47 * all copies or substantial portions of the Software.
49 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
50 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
51 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
52 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
53 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
54 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
55 * DEALINGS IN THE SOFTWARE.
59 #include <sys/types.h>
63 #include <sys/queue.h>
64 #include <sys/param.h>
65 #include <sys/select.h>
66 #if defined(__linux__)
68 #elif defined(__OpenBSD__)
70 #elif defined(__FreeBSD__)
92 #include <X11/cursorfont.h>
93 #include <X11/extensions/Xrandr.h>
94 #include <X11/Xcursor/Xcursor.h>
95 #include <X11/Xft/Xft.h>
96 #include <X11/Xlib-xcb.h>
97 #include <xcb/xcb_atom.h>
98 #include <xcb/xcb_aux.h>
99 #include <xcb/xcb_event.h>
100 #include <xcb/xcb_icccm.h>
101 #include <xcb/xcb_keysyms.h>
102 #include <xcb/xtest.h>
103 #include <xcb/randr.h>
111 #ifdef SPECTRWM_BUILDSTR
112 static const char *buildstr = SPECTRWM_BUILDSTR;
114 static const char *buildstr = SPECTRWM_VERSION;
117 #if !defined(__CYGWIN__) /* cygwin chokes on xrandr stuff */
119 # error XRandR versions less than 1.0 are not supported
122 # if RANDR_MAJOR >= 1
123 # if RANDR_MINOR >= 2
124 # define SWM_XRR_HAS_CRTC
127 #endif /* __CYGWIN__ */
129 #ifndef XCB_ICCCM_NUM_WM_HINTS_ELEMENTS
130 #define XCB_ICCCM_SIZE_HINT_P_MIN_SIZE XCB_SIZE_HINT_P_MIN_SIZE
131 #define XCB_ICCCM_SIZE_HINT_P_MAX_SIZE XCB_SIZE_HINT_P_MAX_SIZE
132 #define XCB_ICCCM_SIZE_HINT_P_RESIZE_INC XCB_SIZE_HINT_P_RESIZE_INC
133 #define XCB_ICCCM_WM_HINT_INPUT XCB_WM_HINT_INPUT
134 #define XCB_ICCCM_WM_HINT_X_URGENCY XCB_WM_HINT_X_URGENCY
135 #define XCB_ICCCM_WM_STATE_ICONIC XCB_WM_STATE_ICONIC
136 #define XCB_ICCCM_WM_STATE_WITHDRAWN XCB_WM_STATE_WITHDRAWN
137 #define XCB_ICCCM_WM_STATE_NORMAL XCB_WM_STATE_NORMAL
138 #define xcb_icccm_get_text_property_reply_t xcb_get_text_property_reply_t
139 #define xcb_icccm_get_text_property_reply_wipe xcb_get_text_property_reply_wipe
140 #define xcb_icccm_get_wm_class xcb_get_wm_class
141 #define xcb_icccm_get_wm_class_reply xcb_get_wm_class_reply
142 #define xcb_icccm_get_wm_class_reply_t xcb_get_wm_class_reply_t
143 #define xcb_icccm_get_wm_class_reply_wipe xcb_get_wm_class_reply_wipe
144 #define xcb_icccm_get_wm_hints xcb_get_wm_hints
145 #define xcb_icccm_wm_hints_get_urgency xcb_wm_hints_get_urgency
146 #define xcb_icccm_get_wm_hints_reply xcb_get_wm_hints_reply
147 #define xcb_icccm_get_wm_name xcb_get_wm_name
148 #define xcb_icccm_get_wm_name_reply xcb_get_wm_name_reply
149 #define xcb_icccm_get_wm_normal_hints xcb_get_wm_normal_hints
150 #define xcb_icccm_get_wm_normal_hints_reply xcb_get_wm_normal_hints_reply
151 #define xcb_icccm_get_wm_protocols xcb_get_wm_protocols
152 #define xcb_icccm_get_wm_protocols_reply xcb_get_wm_protocols_reply
153 #define xcb_icccm_get_wm_protocols_reply_t xcb_get_wm_protocols_reply_t
154 #define xcb_icccm_get_wm_protocols_reply_wipe xcb_get_wm_protocols_reply_wipe
155 #define xcb_icccm_get_wm_transient_for xcb_get_wm_transient_for
156 #define xcb_icccm_get_wm_transient_for_reply xcb_get_wm_transient_for_reply
157 #define xcb_icccm_wm_hints_t xcb_wm_hints_t
160 /*#define SWM_DEBUG*/
162 #define DPRINTF(x...) do { \
164 fprintf(stderr, x); \
166 #define DNPRINTF(n,x...) do { \
167 if (swm_debug & n) { \
168 fprintf(stderr, "%ld ", (long)(time(NULL) - time_started)); \
169 fprintf(stderr, x); \
172 #define SWM_D_MISC 0x0001
173 #define SWM_D_EVENT 0x0002
174 #define SWM_D_WS 0x0004
175 #define SWM_D_FOCUS 0x0008
176 #define SWM_D_MOVE 0x0010
177 #define SWM_D_STACK 0x0020
178 #define SWM_D_MOUSE 0x0040
179 #define SWM_D_PROP 0x0080
180 #define SWM_D_CLASS 0x0100
181 #define SWM_D_KEY 0x0200
182 #define SWM_D_QUIRK 0x0400
183 #define SWM_D_SPAWN 0x0800
184 #define SWM_D_EVENTQ 0x1000
185 #define SWM_D_CONF 0x2000
186 #define SWM_D_BAR 0x4000
187 #define SWM_D_INIT 0x8000
189 uint32_t swm_debug = 0
208 #define DPRINTF(x...)
209 #define DNPRINTF(n,x...)
212 #define SWM_EWMH_ACTION_COUNT_MAX (8)
213 #define EWMH_F_FULLSCREEN (0x001)
214 #define EWMH_F_ABOVE (0x002)
215 #define EWMH_F_HIDDEN (0x004)
216 #define EWMH_F_MAXIMIZED_VERT (0x008)
217 #define EWMH_F_MAXIMIZED_HORZ (0x010)
218 #define EWMH_F_SKIP_PAGER (0x020)
219 #define EWMH_F_SKIP_TASKBAR (0x040)
220 #define SWM_F_MANUAL (0x080)
222 #define EWMH_F_MAXIMIZED (EWMH_F_MAXIMIZED_VERT | EWMH_F_MAXIMIZED_HORZ)
224 /* convert 8-bit to 16-bit */
225 #define RGB_8_TO_16(col) (((col) << 8) + (col))
227 #define PIXEL_TO_XRENDERCOLOR(px, xrc) \
228 xrc.red = RGB_8_TO_16((px) >> 16 & 0xff); \
229 xrc.green = RGB_8_TO_16((px) >> 8 & 0xff); \
230 xrc.blue = RGB_8_TO_16((px) & 0xff); \
233 #define LENGTH(x) (int)(sizeof (x) / sizeof (x)[0])
234 #define MODKEY XCB_MOD_MASK_1
235 #define CLEANMASK(mask) ((mask) & ~(numlockmask | XCB_MOD_MASK_LOCK))
236 #define BUTTONMASK (XCB_EVENT_MASK_BUTTON_PRESS | \
237 XCB_EVENT_MASK_BUTTON_RELEASE)
238 #define MOUSEMASK (BUTTONMASK|XCB_EVENT_MASK_POINTER_MOTION)
239 #define SWM_PROPLEN (16)
240 #define SWM_FUNCNAME_LEN (32)
241 #define SWM_KEYS_LEN (255)
242 #define SWM_QUIRK_LEN (64)
243 #define X(r) ((r)->g.x)
244 #define Y(r) ((r)->g.y)
245 #define WIDTH(r) ((r)->g.w)
246 #define HEIGHT(r) ((r)->g.h)
247 #define BORDER(w) ((w)->bordered ? border_width : 0)
248 #define MAX_X(r) ((r)->g.x + (r)->g.w)
249 #define MAX_Y(r) ((r)->g.y + (r)->g.h)
250 #define SH_MIN(w) ((w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE)
251 #define SH_MIN_W(w) ((w)->sh.min_width)
252 #define SH_MIN_H(w) ((w)->sh.min_height)
253 #define SH_MAX(w) ((w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MAX_SIZE)
254 #define SH_MAX_W(w) ((w)->sh.max_width)
255 #define SH_MAX_H(w) ((w)->sh.max_height)
256 #define SH_INC(w) ((w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_RESIZE_INC)
257 #define SH_INC_W(w) ((w)->sh.width_inc)
258 #define SH_INC_H(w) ((w)->sh.height_inc)
259 #define SWM_MAX_FONT_STEPS (3)
260 #define WINID(w) ((w) ? (w)->id : XCB_WINDOW_NONE)
261 #define WS_FOCUSED(ws) ((ws)->r && (ws)->r->s->r_focus == (ws)->r)
262 #define YESNO(x) ((x) ? "yes" : "no")
263 #define ICONIC(w) ((w)->ewmh_flags & EWMH_F_HIDDEN)
264 #define ABOVE(w) ((w)->ewmh_flags & EWMH_F_ABOVE)
265 #define FULLSCREEN(w) ((w)->ewmh_flags & EWMH_F_FULLSCREEN)
266 #define MAXIMIZED_VERT(w) ((w)->ewmh_flags & EWMH_F_MAXIMIZED_VERT)
267 #define MAXIMIZED_HORZ(w) ((w)->ewmh_flags & EWMH_F_MAXIMIZED_HORZ)
268 #define MAXIMIZED(w) (MAXIMIZED_VERT(w) || MAXIMIZED_HORZ(w))
269 #define MANUAL(w) ((w)->ewmh_flags & SWM_F_MANUAL)
270 #define TRANS(w) ((w)->transient != XCB_WINDOW_NONE)
271 #define FLOATING(w) (ABOVE(w) || TRANS(w) || FULLSCREEN(w) || \
274 /* Constrain Window flags */
275 #define SWM_CW_RESIZABLE (0x01)
276 #define SWM_CW_SOFTBOUNDARY (0x02)
277 #define SWM_CW_HARDBOUNDARY (0x04)
278 #define SWM_CW_RIGHT (0x10)
279 #define SWM_CW_LEFT (0x20)
280 #define SWM_CW_BOTTOM (0x40)
281 #define SWM_CW_TOP (0x80)
282 #define SWM_CW_ALLSIDES (0xf0)
284 #define SWM_FOCUS_DEFAULT (0)
285 #define SWM_FOCUS_FOLLOW (1)
286 #define SWM_FOCUS_MANUAL (2)
288 #define SWM_CK_NONE (0)
289 #define SWM_CK_ALL (0xf)
290 #define SWM_CK_FOCUS (0x1)
291 #define SWM_CK_POINTER (0x2)
292 #define SWM_CK_FALLBACK (0x4)
293 #define SWM_CK_REGION (0x8)
295 #define SWM_G_ALL (0xf)
296 #define SWM_G_SIZE (0x1)
297 #define SWM_G_POS (0x2)
299 #define SWM_CONF_DEFAULT (0)
300 #define SWM_CONF_KEYMAPPING (1)
303 #define SWM_LIB "/usr/local/lib/libswmhack.so"
310 xcb_atom_t a_net_wm_check;
311 xcb_atom_t a_net_supported;
312 xcb_atom_t a_takefocus;
313 xcb_atom_t a_utf8_string;
315 volatile sig_atomic_t running = 1;
316 volatile sig_atomic_t restart_wm = 0;
317 xcb_timestamp_t last_event_time = 0;
321 int xrandr_eventbase;
322 unsigned int numlockmask = 0;
325 xcb_connection_t *conn;
326 xcb_key_symbols_t *syms;
328 int boundary_width = 50;
330 int cycle_visible = 0;
332 int font_adjusted = 0;
333 unsigned int mod_key = MODKEY;
336 struct swm_region *search_r;
337 int select_list_pipe[2];
338 int select_resp_pipe[2];
340 volatile sig_atomic_t search_resp;
341 int search_resp_action;
343 struct search_window {
344 TAILQ_ENTRY(search_window) entry;
348 xcb_window_t indicator;
350 TAILQ_HEAD(search_winlist, search_window);
351 struct search_winlist search_wl;
356 SWM_SEARCH_UNICONIFY,
357 SWM_SEARCH_NAME_WORKSPACE,
358 SWM_SEARCH_SEARCH_WORKSPACE,
359 SWM_SEARCH_SEARCH_WINDOW
362 #define SWM_STACK_TOP (0)
363 #define SWM_STACK_BOTTOM (1)
364 #define SWM_STACK_ABOVE (2)
365 #define SWM_STACK_BELOW (3)
368 double dialog_ratio = 0.6;
370 #define SWM_BAR_MAX (256)
371 #define SWM_BAR_JUSTIFY_LEFT (0)
372 #define SWM_BAR_JUSTIFY_CENTER (1)
373 #define SWM_BAR_JUSTIFY_RIGHT (2)
374 #define SWM_BAR_OFFSET (4)
375 #define SWM_BAR_FONTS "-*-terminus-medium-*-*-*-12-*-*-*-*-*-*-*," \
376 "-*-profont-*-*-*-*-12-*-*-*-*-*-*-*," \
377 "-*-times-medium-r-*-*-12-*-*-*-*-*-*-*," \
378 "-misc-fixed-medium-r-*-*-12-*-*-*-*-*-*-*," \
379 "-*-*-*-r-*-*-*-*-*-*-*-*-*-*"
381 #ifdef X_HAVE_UTF8_STRING
382 #define DRAWSTRING(x...) Xutf8DrawString(x)
384 #define DRAWSTRING(x...) XmbDrawString(x)
387 char *bar_argv[] = { NULL, NULL };
389 char bar_ext[SWM_BAR_MAX];
390 char bar_ext_buf[SWM_BAR_MAX];
391 char bar_vertext[SWM_BAR_MAX];
394 int bar_border_width = 1;
395 int bar_at_bottom = 0;
399 int bar_justify = SWM_BAR_JUSTIFY_LEFT;
400 char *bar_format = NULL;
401 int stack_enabled = 1;
402 int clock_enabled = 1;
403 int iconic_enabled = 0;
404 int urgent_enabled = 0;
405 char *clock_format = NULL;
406 int window_class_enabled = 0;
407 int window_instance_enabled = 0;
408 int window_name_enabled = 0;
409 int focus_mode = SWM_FOCUS_DEFAULT;
410 int focus_close = SWM_STACK_BELOW;
411 int focus_close_wrap = 1;
412 int focus_default = SWM_STACK_TOP;
413 int spawn_position = SWM_STACK_TOP;
414 int disable_border = 0;
415 int border_width = 1;
416 int region_padding = 0;
418 int java_workaround = 1;
419 int verbose_layout = 0;
423 XFontSetExtents *bar_fs_extents;
425 int bar_font_legacy = 1;
427 XftColor bar_font_color;
428 XftColor search_font_color;
430 char *startup_exception;
431 unsigned int nr_exceptions = 0;
433 /* layout manager data */
434 struct swm_geometry {
447 struct swm_geometry g;
450 /* virtual "screens" */
452 TAILQ_ENTRY(swm_region) entry;
454 struct swm_geometry g;
455 struct workspace *ws; /* current workspace on this region */
456 struct workspace *ws_prior; /* prior workspace on this region */
457 struct swm_screen *s; /* screen idx */
460 TAILQ_HEAD(swm_region_list, swm_region);
463 TAILQ_ENTRY(ws_win) entry;
464 TAILQ_ENTRY(ws_win) stack_entry;
466 xcb_window_t transient;
467 struct ws_win *focus_child; /* focus on child transient */
468 struct swm_geometry g; /* current geometry */
469 struct swm_geometry g_float; /* region coordinates */
470 int g_floatvalid; /* g_float geometry validity */
474 int font_size_boundary[SWM_MAX_FONT_STEPS];
480 unsigned long quirks;
481 struct workspace *ws; /* always valid */
482 struct swm_screen *s; /* always valid, never changes */
484 xcb_icccm_get_wm_class_reply_t ch;
485 xcb_icccm_wm_hints_t hints;
487 TAILQ_HEAD(ws_win_list, ws_win);
488 TAILQ_HEAD(ws_win_stack, ws_win);
492 TAILQ_ENTRY(pid_e) entry;
496 TAILQ_HEAD(pid_list, pid_e);
497 struct pid_list pidlist = TAILQ_HEAD_INITIALIZER(pidlist);
499 /* layout handlers */
501 void vertical_config(struct workspace *, int);
502 void vertical_stack(struct workspace *, struct swm_geometry *);
503 void horizontal_config(struct workspace *, int);
504 void horizontal_stack(struct workspace *, struct swm_geometry *);
505 void max_stack(struct workspace *, struct swm_geometry *);
506 void plain_stacker(struct workspace *);
507 void fancy_stacker(struct workspace *);
510 void (*l_stack)(struct workspace *, struct swm_geometry *);
511 void (*l_config)(struct workspace *, int);
513 #define SWM_L_FOCUSPREV (1<<0)
514 #define SWM_L_MAPONFOCUS (1<<1)
515 void (*l_string)(struct workspace *);
517 /* stack, configure */
518 { vertical_stack, vertical_config, 0, plain_stacker },
519 { horizontal_stack, horizontal_config, 0, plain_stacker },
521 SWM_L_MAPONFOCUS | SWM_L_FOCUSPREV, plain_stacker },
522 { NULL, NULL, 0, NULL },
525 /* position of max_stack mode in the layouts array, index into layouts! */
526 #define SWM_V_STACK (0)
527 #define SWM_H_STACK (1)
528 #define SWM_MAX_STACK (2)
530 #define SWM_H_SLICE (32)
531 #define SWM_V_SLICE (32)
533 /* define work spaces */
535 int idx; /* workspace index */
536 char *name; /* workspace name */
537 int always_raise; /* raise windows on focus */
538 int bar_enabled; /* bar visibility */
539 struct layout *cur_layout; /* current layout handlers */
540 struct ws_win *focus; /* may be NULL */
541 struct ws_win *focus_prev; /* may be NULL */
542 struct ws_win *focus_pending; /* may be NULL */
543 struct swm_region *r; /* may be NULL */
544 struct swm_region *old_r; /* may be NULL */
545 struct ws_win_list winlist; /* list of windows in ws */
546 struct ws_win_list unmanagedlist; /* list of dead windows in ws */
547 struct ws_win_stack stack; /* stacking order */
548 int state; /* mapping state */
549 char stacker[10]; /* display stacker and layout */
553 int horizontal_msize;
555 int horizontal_stacks;
566 SWM_WS_STATE_MAPPING,
572 SWM_S_COLOR_BAR_BORDER,
573 SWM_S_COLOR_BAR_BORDER_UNFOCUS,
574 SWM_S_COLOR_BAR_FONT,
576 SWM_S_COLOR_FOCUS_MAXIMIZED,
578 SWM_S_COLOR_UNFOCUS_MAXIMIZED,
582 /* physical screen mapping */
583 #define SWM_WS_MAX (22) /* hard limit */
584 int workspace_limit = 10; /* soft limit */
587 int idx; /* screen index */
588 struct swm_region_list rl; /* list of regions on this screen */
589 struct swm_region_list orl; /* list of old regions */
591 struct workspace ws[SWM_WS_MAX];
592 struct swm_region *r_focus;
599 } c[SWM_S_COLOR_MAX];
601 xcb_gcontext_t bar_gc;
604 struct swm_screen *screens;
606 /* args to functions */
609 #define SWM_ARG_ID_FOCUSNEXT (0)
610 #define SWM_ARG_ID_FOCUSPREV (1)
611 #define SWM_ARG_ID_FOCUSMAIN (2)
612 #define SWM_ARG_ID_FOCUSURGENT (3)
613 #define SWM_ARG_ID_SWAPNEXT (10)
614 #define SWM_ARG_ID_SWAPPREV (11)
615 #define SWM_ARG_ID_SWAPMAIN (12)
616 #define SWM_ARG_ID_MOVELAST (13)
617 #define SWM_ARG_ID_MASTERSHRINK (20)
618 #define SWM_ARG_ID_MASTERGROW (21)
619 #define SWM_ARG_ID_MASTERADD (22)
620 #define SWM_ARG_ID_MASTERDEL (23)
621 #define SWM_ARG_ID_FLIPLAYOUT (24)
622 #define SWM_ARG_ID_STACKRESET (30)
623 #define SWM_ARG_ID_STACKINIT (31)
624 #define SWM_ARG_ID_CYCLEWS_UP (40)
625 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
626 #define SWM_ARG_ID_CYCLERG_UP (42)
627 #define SWM_ARG_ID_CYCLERG_DOWN (43)
628 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
629 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
630 #define SWM_ARG_ID_CYCLEWS_MOVE_UP (46)
631 #define SWM_ARG_ID_CYCLEWS_MOVE_DOWN (47)
632 #define SWM_ARG_ID_STACKINC (50)
633 #define SWM_ARG_ID_STACKDEC (51)
634 #define SWM_ARG_ID_SS_ALL (60)
635 #define SWM_ARG_ID_SS_WINDOW (61)
636 #define SWM_ARG_ID_DONTCENTER (70)
637 #define SWM_ARG_ID_CENTER (71)
638 #define SWM_ARG_ID_KILLWINDOW (80)
639 #define SWM_ARG_ID_DELETEWINDOW (81)
640 #define SWM_ARG_ID_WIDTHGROW (90)
641 #define SWM_ARG_ID_WIDTHSHRINK (91)
642 #define SWM_ARG_ID_HEIGHTGROW (92)
643 #define SWM_ARG_ID_HEIGHTSHRINK (93)
644 #define SWM_ARG_ID_MOVEUP (100)
645 #define SWM_ARG_ID_MOVEDOWN (101)
646 #define SWM_ARG_ID_MOVELEFT (102)
647 #define SWM_ARG_ID_MOVERIGHT (103)
648 #define SWM_ARG_ID_RAISE (105)
649 #define SWM_ARG_ID_LOWER (106)
650 #define SWM_ARG_ID_BAR_TOGGLE (110)
651 #define SWM_ARG_ID_BAR_TOGGLE_WS (111)
657 TAILQ_ENTRY(quirk) entry;
658 char *class; /* WM_CLASS:class */
659 char *instance; /* WM_CLASS:instance */
660 char *name; /* WM_NAME */
662 regex_t regex_instance;
665 #define SWM_Q_FLOAT (1<<0) /* float this window */
666 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
667 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
668 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
669 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
670 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
671 #define SWM_Q_NOFOCUSONMAP (1<<6) /* Don't focus on window when mapped. */
672 #define SWM_Q_FOCUSONMAP_SINGLE (1<<7) /* Only focus if single win of type. */
673 #define SWM_Q_OBEYAPPFOCUSREQ (1<<8) /* Focus when applications ask. */
674 #define SWM_Q_IGNOREPID (1<<9) /* Ignore PID when determining ws. */
676 TAILQ_HEAD(quirk_list, quirk);
677 struct quirk_list quirks = TAILQ_HEAD_INITIALIZER(quirks);
680 * Supported EWMH hints should be added to
681 * both the enum and the ewmh array
687 _NET_CURRENT_DESKTOP,
688 _NET_DESKTOP_GEOMETRY,
690 _NET_DESKTOP_VIEWPORT,
691 _NET_MOVERESIZE_WINDOW,
692 _NET_NUMBER_OF_DESKTOPS,
694 _NET_WM_ACTION_ABOVE,
695 _NET_WM_ACTION_CLOSE,
696 _NET_WM_ACTION_FULLSCREEN,
698 _NET_WM_ACTION_RESIZE,
699 _NET_WM_ALLOWED_ACTIONS,
701 _NET_WM_FULL_PLACEMENT,
705 _NET_WM_STATE_FULLSCREEN,
706 _NET_WM_STATE_HIDDEN,
707 _NET_WM_STATE_MAXIMIZED_VERT,
708 _NET_WM_STATE_MAXIMIZED_HORZ,
709 _NET_WM_STATE_SKIP_PAGER,
710 _NET_WM_STATE_SKIP_TASKBAR,
712 _NET_WM_WINDOW_TYPE_DIALOG,
713 _NET_WM_WINDOW_TYPE_DOCK,
714 _NET_WM_WINDOW_TYPE_NORMAL,
715 _NET_WM_WINDOW_TYPE_SPLASH,
716 _NET_WM_WINDOW_TYPE_TOOLBAR,
717 _NET_WM_WINDOW_TYPE_UTILITY,
718 _SWM_WM_STATE_MANUAL,
725 } ewmh[SWM_EWMH_HINT_MAX] = {
726 /* must be in same order as in the enum */
727 {"_NET_ACTIVE_WINDOW", XCB_ATOM_NONE},
728 {"_NET_CLIENT_LIST", XCB_ATOM_NONE},
729 {"_NET_CLOSE_WINDOW", XCB_ATOM_NONE},
730 {"_NET_CURRENT_DESKTOP", XCB_ATOM_NONE},
731 {"_NET_DESKTOP_GEOMETRY", XCB_ATOM_NONE},
732 {"_NET_DESKTOP_NAMES", XCB_ATOM_NONE},
733 {"_NET_DESKTOP_VIEWPORT", XCB_ATOM_NONE},
734 {"_NET_MOVERESIZE_WINDOW", XCB_ATOM_NONE},
735 {"_NET_NUMBER_OF_DESKTOPS", XCB_ATOM_NONE},
736 {"_NET_RESTACK_WINDOW", XCB_ATOM_NONE},
737 {"_NET_WM_ACTION_ABOVE", XCB_ATOM_NONE},
738 {"_NET_WM_ACTION_CLOSE", XCB_ATOM_NONE},
739 {"_NET_WM_ACTION_FULLSCREEN", XCB_ATOM_NONE},
740 {"_NET_WM_ACTION_MOVE", XCB_ATOM_NONE},
741 {"_NET_WM_ACTION_RESIZE", XCB_ATOM_NONE},
742 {"_NET_WM_ALLOWED_ACTIONS", XCB_ATOM_NONE},
743 {"_NET_WM_DESKTOP", XCB_ATOM_NONE},
744 {"_NET_WM_FULL_PLACEMENT", XCB_ATOM_NONE},
745 {"_NET_WM_NAME", XCB_ATOM_NONE},
746 {"_NET_WM_STATE", XCB_ATOM_NONE},
747 {"_NET_WM_STATE_ABOVE", XCB_ATOM_NONE},
748 {"_NET_WM_STATE_FULLSCREEN", XCB_ATOM_NONE},
749 {"_NET_WM_STATE_HIDDEN", XCB_ATOM_NONE},
750 {"_NET_WM_STATE_MAXIMIZED_VERT", XCB_ATOM_NONE},
751 {"_NET_WM_STATE_MAXIMIZED_HORZ", XCB_ATOM_NONE},
752 {"_NET_WM_STATE_SKIP_PAGER", XCB_ATOM_NONE},
753 {"_NET_WM_STATE_SKIP_TASKBAR", XCB_ATOM_NONE},
754 {"_NET_WM_WINDOW_TYPE", XCB_ATOM_NONE},
755 {"_NET_WM_WINDOW_TYPE_DIALOG", XCB_ATOM_NONE},
756 {"_NET_WM_WINDOW_TYPE_DOCK", XCB_ATOM_NONE},
757 {"_NET_WM_WINDOW_TYPE_NORMAL", XCB_ATOM_NONE},
758 {"_NET_WM_WINDOW_TYPE_SPLASH", XCB_ATOM_NONE},
759 {"_NET_WM_WINDOW_TYPE_TOOLBAR", XCB_ATOM_NONE},
760 {"_NET_WM_WINDOW_TYPE_UTILITY", XCB_ATOM_NONE},
761 {"_SWM_WM_STATE_MANUAL", XCB_ATOM_NONE},
764 /* EWMH source type */
766 EWMH_SOURCE_TYPE_NONE = 0,
767 EWMH_SOURCE_TYPE_NORMAL = 1,
768 EWMH_SOURCE_TYPE_OTHER = 2,
775 XC_BOTTOM_LEFT_CORNER,
776 XC_BOTTOM_RIGHT_CORNER,
784 char *name; /* Name used by Xcursor .*/
785 uint8_t cf_char; /* cursorfont index. */
787 } cursors[XC_MAX] = {
788 {"fleur", XC_fleur, XCB_CURSOR_NONE},
789 {"left_ptr", XC_left_ptr, XCB_CURSOR_NONE},
790 {"bottom_left_corner", XC_bottom_left_corner, XCB_CURSOR_NONE},
791 {"bottom_right_corner", XC_bottom_right_corner, XCB_CURSOR_NONE},
792 {"sizing", XC_sizing, XCB_CURSOR_NONE},
793 {"top_left_corner", XC_top_left_corner, XCB_CURSOR_NONE},
794 {"top_right_corner", XC_top_right_corner, XCB_CURSOR_NONE},
797 #define SWM_SPAWN_OPTIONAL 0x1
801 TAILQ_ENTRY(spawn_prog) entry;
807 TAILQ_HEAD(spawn_list, spawn_prog);
808 struct spawn_list spawns = TAILQ_HEAD_INITIALIZER(spawns);
810 /* user/key callable function IDs */
926 KF_DUMPWINS, /* MUST BE LAST */
934 enum keyfuncid funcid;
937 RB_HEAD(key_tree, key);
939 /* function prototypes */
940 void adjust_font(struct ws_win *);
941 char *argsep(char **);
942 void bar_cleanup(struct swm_region *);
943 void bar_extra_setup(void);
944 void bar_extra_stop(void);
945 int bar_extra_update(void);
946 void bar_fmt(const char *, char *, struct swm_region *, size_t);
947 void bar_fmt_expand(char *, size_t);
949 void bar_print(struct swm_region *, const char *);
950 void bar_print_legacy(struct swm_region *, const char *);
951 void bar_replace(char *, char *, struct swm_region *, size_t);
952 void bar_replace_pad(char *, int *, size_t);
953 char *bar_replace_seq(char *, char *, struct swm_region *, size_t *, size_t);
954 void bar_setup(struct swm_region *);
955 void bar_toggle(struct swm_region *, union arg *);
956 void bar_urgent(char *, size_t);
957 void bar_window_class(char *, size_t, struct swm_region *);
958 void bar_window_class_instance(char *, size_t, struct swm_region *);
959 void bar_window_float(char *, size_t, struct swm_region *);
960 void bar_window_instance(char *, size_t, struct swm_region *);
961 void bar_window_name(char *, size_t, struct swm_region *);
962 void bar_window_state(char *, size_t, struct swm_region *);
963 void bar_workspace_name(char *, size_t, struct swm_region *);
964 void buttonpress(xcb_button_press_event_t *);
965 void check_conn(void);
966 void clear_keys(void);
967 int clear_maximized(struct workspace *);
968 void clientmessage(xcb_client_message_event_t *);
969 void client_msg(struct ws_win *, xcb_atom_t, xcb_timestamp_t);
970 int conf_load(const char *, int);
971 void configurenotify(xcb_configure_notify_event_t *);
972 void configurerequest(xcb_configure_request_event_t *);
973 void config_win(struct ws_win *, xcb_configure_request_event_t *);
974 void constrain_window(struct ws_win *, struct swm_geometry *, int *);
975 int count_win(struct workspace *, int);
976 void cursors_cleanup(void);
977 void cursors_load(void);
978 void custom_region(const char *);
979 void cyclerg(struct swm_region *, union arg *);
980 void cyclews(struct swm_region *, union arg *);
981 void cycle_layout(struct swm_region *, union arg *);
982 void destroynotify(xcb_destroy_notify_event_t *);
983 void dumpwins(struct swm_region *, union arg *);
985 void enternotify(xcb_enter_notify_event_t *);
986 void event_drain(uint8_t);
987 void event_error(xcb_generic_error_t *);
988 void event_handle(xcb_generic_event_t *);
989 void ewmh_apply_flags(struct ws_win *, uint32_t);
990 void ewmh_autoquirk(struct ws_win *);
991 void ewmh_get_desktop_names(void);
992 void ewmh_get_wm_state(struct ws_win *);
993 void ewmh_update_actions(struct ws_win *);
994 void ewmh_update_client_list(void);
995 void ewmh_update_current_desktop(void);
996 void ewmh_update_desktop_names(void);
997 void ewmh_update_desktops(void);
998 void ewmh_change_wm_state(struct ws_win *, xcb_atom_t, long);
999 void ewmh_update_wm_state(struct ws_win *);
1000 char *expand_tilde(const char *);
1001 void expose(xcb_expose_event_t *);
1002 void fake_keypress(struct ws_win *, xcb_keysym_t, uint16_t);
1003 struct pid_e *find_pid(pid_t);
1004 struct ws_win *find_unmanaged_window(xcb_window_t);
1005 struct ws_win *find_window(xcb_window_t);
1006 void floating_toggle(struct swm_region *, union arg *);
1007 void focus(struct swm_region *, union arg *);
1009 void focusin(xcb_focus_in_event_t *);
1010 void focusout(xcb_focus_out_event_t *);
1012 void focus_flush(void);
1013 void focus_region(struct swm_region *);
1014 void focusrg(struct swm_region *, union arg *);
1015 void focus_win(struct ws_win *);
1016 void fontset_init(void);
1017 void free_window(struct ws_win *);
1018 xcb_atom_t get_atom_from_string(const char *);
1020 char *get_atom_name(xcb_atom_t);
1022 struct ws_win *get_focus_magic(struct ws_win *);
1023 struct ws_win *get_focus_prev(struct ws_win *);
1025 char *get_notify_detail_label(uint8_t);
1026 char *get_notify_mode_label(uint8_t);
1028 struct ws_win *get_pointer_win(xcb_window_t);
1029 struct ws_win *get_region_focus(struct swm_region *);
1030 int get_region_index(struct swm_region *);
1031 xcb_screen_t *get_screen(int);
1032 int get_screen_count(void);
1034 char *get_source_type_label(uint32_t);
1035 char *get_stack_mode_name(uint8_t);
1037 int32_t get_swm_ws(xcb_window_t);
1038 char *get_win_name(xcb_window_t);
1039 uint8_t get_win_state(xcb_window_t);
1040 void get_wm_protocols(struct ws_win *);
1041 int get_ws_idx(xcb_window_t);
1042 void grabbuttons(struct ws_win *);
1043 void grabkeys(void);
1044 void grab_windows(void);
1045 void iconify(struct swm_region *, union arg *);
1047 void keypress(xcb_key_press_event_t *);
1048 int key_cmp(struct key *, struct key *);
1049 void key_insert(unsigned int, KeySym, enum keyfuncid, const char *);
1050 struct key *key_lookup(unsigned int, KeySym);
1051 void key_remove(struct key *);
1052 void key_replace(struct key *, unsigned int, KeySym, enum keyfuncid,
1054 void kill_bar_extra_atexit(void);
1055 void kill_refs(struct ws_win *);
1057 void leavenotify(xcb_leave_notify_event_t *);
1059 void load_float_geom(struct ws_win *);
1060 struct ws_win *manage_window(xcb_window_t, int);
1061 void map_window(struct ws_win *);
1062 void mapnotify(xcb_map_notify_event_t *);
1063 void mappingnotify(xcb_mapping_notify_event_t *);
1064 void maprequest(xcb_map_request_event_t *);
1065 void maximize_toggle(struct swm_region *, union arg *);
1066 void motionnotify(xcb_motion_notify_event_t *);
1067 void move(struct ws_win *, union arg *);
1068 void move_step(struct swm_region *, union arg *);
1069 uint32_t name_to_pixel(int, const char *);
1070 void name_workspace(struct swm_region *, union arg *);
1071 void new_region(struct swm_screen *, int, int, int, int);
1072 int parsekeys(const char *, unsigned int, unsigned int *, KeySym *);
1073 int parsequirks(const char *, unsigned long *);
1074 int parse_rgb(const char *, uint16_t *, uint16_t *, uint16_t *);
1075 void pressbutton(struct swm_region *, union arg *);
1076 void priorws(struct swm_region *, union arg *);
1078 void print_win_geom(xcb_window_t);
1080 void propertynotify(xcb_property_notify_event_t *);
1081 void quirk_free(struct quirk *);
1082 void quirk_insert(const char *, const char *, const char *,unsigned long);
1083 void quirk_remove(struct quirk *);
1084 void quirk_replace(struct quirk *, const char *, const char *, const char *,
1086 void quit(struct swm_region *, union arg *);
1087 void raise_toggle(struct swm_region *, union arg *);
1088 void raise_window(struct ws_win *);
1089 void region_containment(struct ws_win *, struct swm_region *, int);
1090 struct swm_region *region_under(struct swm_screen *, int, int);
1091 void regionize(struct ws_win *, int, int);
1092 void resize(struct ws_win *, union arg *);
1093 void resize_step(struct swm_region *, union arg *);
1094 void restart(struct swm_region *, union arg *);
1095 struct swm_region *root_to_region(xcb_window_t, int);
1096 void screenchange(xcb_randr_screen_change_notify_event_t *);
1097 void scan_xrandr(int);
1098 void search_do_resp(void);
1099 void search_resp_name_workspace(const char *, size_t);
1100 void search_resp_search_window(const char *);
1101 void search_resp_search_workspace(const char *);
1102 void search_resp_uniconify(const char *, size_t);
1103 void search_win(struct swm_region *, union arg *);
1104 void search_win_cleanup(void);
1105 void search_workspace(struct swm_region *, union arg *);
1106 void send_to_rg(struct swm_region *, union arg *);
1107 void send_to_ws(struct swm_region *, union arg *);
1108 void set_region(struct swm_region *);
1109 int setautorun(const char *, const char *, int);
1110 int setconfbinding(const char *, const char *, int);
1111 int setconfcolor(const char *, const char *, int);
1112 int setconfmodkey(const char *, const char *, int);
1113 int setconfquirk(const char *, const char *, int);
1114 int setconfregion(const char *, const char *, int);
1115 int setconfspawn(const char *, const char *, int);
1116 int setconfvalue(const char *, const char *, int);
1117 void setkeybinding(unsigned int, KeySym, enum keyfuncid, const char *);
1118 int setkeymapping(const char *, const char *, int);
1119 int setlayout(const char *, const char *, int);
1120 void setquirk(const char *, const char *, const char *,unsigned long);
1121 void setscreencolor(const char *, int, int);
1122 void setspawn(const char *, const char *, int);
1123 void setup_ewmh(void);
1124 void setup_globals(void);
1125 void setup_keys(void);
1126 void setup_quirks(void);
1127 void setup_screens(void);
1128 void setup_spawn(void);
1129 void set_child_transient(struct ws_win *, xcb_window_t *);
1130 void set_win_state(struct ws_win *, uint8_t);
1131 void shutdown_cleanup(void);
1133 void socket_setnonblock(int);
1134 void sort_windows(struct ws_win_list *);
1135 void spawn(int, union arg *, int);
1136 void spawn_custom(struct swm_region *, union arg *, const char *);
1137 int spawn_expand(struct swm_region *, union arg *, const char *, char ***);
1138 void spawn_insert(const char *, const char *, int);
1139 struct spawn_prog *spawn_find(const char *);
1140 void spawn_remove(struct spawn_prog *);
1141 void spawn_replace(struct spawn_prog *, const char *, const char *, int);
1142 void spawn_select(struct swm_region *, union arg *, const char *, int *);
1143 void stack_config(struct swm_region *, union arg *);
1144 void stack_master(struct workspace *, struct swm_geometry *, int, int);
1145 void store_float_geom(struct ws_win *);
1146 char *strdupsafe(const char *);
1147 void swapwin(struct swm_region *, union arg *);
1148 void switchws(struct swm_region *, union arg *);
1149 void teardown_ewmh(void);
1150 void unescape_selector(char *);
1151 void unfocus_win(struct ws_win *);
1152 void uniconify(struct swm_region *, union arg *);
1153 void unmanage_window(struct ws_win *);
1154 void unmapnotify(xcb_unmap_notify_event_t *);
1155 void unmap_all(void);
1156 void unmap_window(struct ws_win *);
1157 void updatenumlockmask(void);
1158 void update_floater(struct ws_win *);
1159 void update_modkey(unsigned int);
1160 void update_win_stacking(struct ws_win *);
1161 void update_window(struct ws_win *);
1162 void update_window_color(struct ws_win *);
1163 void update_wm_state(struct ws_win *win);
1164 void validate_spawns(void);
1165 int validate_win(struct ws_win *);
1166 int validate_ws(struct workspace *);
1167 void version(struct swm_region *, union arg *);
1168 void win_to_ws(struct ws_win *, int, int);
1169 pid_t window_get_pid(xcb_window_t);
1170 void wkill(struct swm_region *, union arg *);
1171 void update_ws_stack(struct workspace *);
1172 void xft_init(struct swm_region *);
1173 void _add_startup_exception(const char *, va_list);
1174 void add_startup_exception(const char *, ...);
1176 RB_PROTOTYPE(key_tree, key, entry, key_cmp);
1177 RB_GENERATE(key_tree, key, entry, key_cmp);
1178 struct key_tree keys;
1183 xcb_font_t cf = XCB_NONE;
1186 for (i = 0; i < LENGTH(cursors); ++i) {
1187 /* try to load Xcursor first. */
1188 cursors[i].cid = XcursorLibraryLoadCursor(display,
1191 /* fallback to cursorfont. */
1192 if (cursors[i].cid == XCB_CURSOR_NONE) {
1193 if (cf == XCB_NONE) {
1194 cf = xcb_generate_id(conn);
1195 xcb_open_font(conn, cf, strlen("cursor"),
1199 cursors[i].cid = xcb_generate_id(conn);
1200 xcb_create_glyph_cursor(conn, cursors[i].cid, cf, cf,
1201 cursors[i].cf_char, cursors[i].cf_char + 1, 0, 0, 0,
1202 0xffff, 0xffff, 0xffff);
1208 xcb_close_font(conn, cf);
1212 cursors_cleanup(void)
1215 for (i = 0; i < LENGTH(cursors); ++i)
1216 xcb_free_cursor(conn, cursors[i].cid);
1220 expand_tilde(const char *s)
1222 struct passwd *ppwd;
1230 errx(1, "expand_tilde: NULL string.");
1233 result = strdup(sc);
1239 if ((max = sysconf(_SC_LOGIN_NAME_MAX)) == -1)
1240 errx(1, "expand_tilde: sysconf");
1242 if ((user = calloc(1, max + 1)) == NULL)
1243 errx(1, "expand_tilde: calloc");
1245 for (i = 0; s[i] != '/' && s[i] != '\0'; ++i)
1250 ppwd = strlen(user) == 0 ? getpwuid(getuid()) : getpwnam(user);
1254 result = strdup(sc);
1256 if (asprintf(&result, "%s%s", ppwd->pw_dir, s) == -1)
1260 errx(1, "expand_tilde: failed to allocate memory.");
1266 parse_rgb(const char *rgb, uint16_t *rr, uint16_t *gg, uint16_t *bb)
1268 unsigned int tmpr, tmpg, tmpb;
1270 if (sscanf(rgb, "rgb:%x/%x/%x", &tmpr, &tmpg, &tmpb) != 3)
1273 *rr = RGB_8_TO_16(tmpr);
1274 *gg = RGB_8_TO_16(tmpg);
1275 *bb = RGB_8_TO_16(tmpb);
1281 get_screen(int screen)
1283 const xcb_setup_t *r;
1284 xcb_screen_iterator_t iter;
1286 if ((r = xcb_get_setup(conn)) == NULL) {
1287 DNPRINTF(SWM_D_MISC, "get_screen: xcb_get_setup\n");
1291 iter = xcb_setup_roots_iterator(r);
1292 for (; iter.rem; --screen, xcb_screen_next(&iter))
1300 get_screen_count(void)
1302 const xcb_setup_t *r;
1304 if ((r = xcb_get_setup(conn)) == NULL) {
1305 DNPRINTF(SWM_D_MISC, "get_screen_count: xcb_get_setup\n");
1309 return xcb_setup_roots_length(r);
1313 get_region_index(struct swm_region *r)
1315 struct swm_region *rr;
1321 TAILQ_FOREACH(rr, &r->s->rl, entry) {
1336 if (focus_mode == SWM_FOCUS_DEFAULT)
1337 event_drain(XCB_ENTER_NOTIFY);
1343 get_atom_from_string(const char *str)
1345 xcb_intern_atom_cookie_t c;
1346 xcb_intern_atom_reply_t *r;
1349 c = xcb_intern_atom(conn, 0, strlen(str), str);
1350 r = xcb_intern_atom_reply(conn, c, NULL);
1358 return (XCB_ATOM_NONE);
1362 get_wm_protocols(struct ws_win *win) {
1364 xcb_icccm_get_wm_protocols_reply_t wpr;
1366 if (xcb_icccm_get_wm_protocols_reply(conn,
1367 xcb_icccm_get_wm_protocols(conn, win->id, a_prot),
1369 for (i = 0; i < (int)wpr.atoms_len; i++) {
1370 if (wpr.atoms[i] == a_takefocus)
1371 win->take_focus = 1;
1372 if (wpr.atoms[i] == a_delete)
1373 win->can_delete = 1;
1375 xcb_icccm_get_wm_protocols_reply_wipe(&wpr);
1382 xcb_window_t root, win;
1383 int i, j, num_screens;
1385 for (i = 0; i < LENGTH(ewmh); i++)
1386 ewmh[i].atom = get_atom_from_string(ewmh[i].name);
1388 num_screens = get_screen_count();
1389 for (i = 0; i < num_screens; i++) {
1390 root = screens[i].root;
1392 /* Set up _NET_SUPPORTING_WM_CHECK. */
1393 win = xcb_generate_id(conn);
1394 xcb_create_window(conn, XCB_COPY_FROM_PARENT, win, root,
1395 0, 0, 1, 1, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT,
1396 XCB_COPY_FROM_PARENT, 0, NULL);
1398 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root,
1399 a_net_wm_check, XCB_ATOM_WINDOW, 32, 1, &win);
1400 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win,
1401 a_net_wm_check, XCB_ATOM_WINDOW, 32, 1, &win);
1404 * Impersonate LG3D non-reparenting WM, written by Sun, to
1405 * workaround a Java GUI rendering issue.
1407 if (java_workaround)
1408 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win,
1409 ewmh[_NET_WM_NAME].atom, a_utf8_string,
1410 8, strlen("LG3D"), "LG3D");
1412 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win,
1413 ewmh[_NET_WM_NAME].atom, a_utf8_string,
1414 8, strlen("spectrwm"), "spectrwm");
1416 /* Report supported atoms */
1417 xcb_delete_property(conn, root, a_net_supported);
1418 for (j = 0; j < LENGTH(ewmh); j++)
1419 xcb_change_property(conn, XCB_PROP_MODE_APPEND, root,
1420 a_net_supported, XCB_ATOM_ATOM, 32, 1,
1425 ewmh_update_desktops();
1426 ewmh_get_desktop_names();
1434 xcb_get_property_cookie_t pc;
1435 xcb_get_property_reply_t *pr;
1437 num_screens = get_screen_count();
1439 for (i = 0; i < num_screens; i++) {
1440 /* Get the support check window and destroy it */
1441 pc = xcb_get_property(conn, 0, screens[i].root, a_net_wm_check,
1442 XCB_ATOM_WINDOW, 0, 1);
1443 pr = xcb_get_property_reply(conn, pc, NULL);
1446 if (pr->format == a_net_wm_check) {
1447 id = *((xcb_window_t *)xcb_get_property_value(pr));
1449 xcb_destroy_window(conn, id);
1450 xcb_delete_property(conn, screens[i].root,
1452 xcb_delete_property(conn, screens[i].root,
1460 ewmh_autoquirk(struct ws_win *win)
1462 xcb_get_property_reply_t *r;
1463 xcb_get_property_cookie_t c;
1467 c = xcb_get_property(conn, 0, win->id,
1468 ewmh[_NET_WM_WINDOW_TYPE].atom, XCB_ATOM_ATOM, 0, UINT32_MAX);
1469 r = xcb_get_property_reply(conn, c, NULL);
1473 type = xcb_get_property_value(r);
1474 n = xcb_get_property_value_length(r) / sizeof(xcb_atom_t);
1476 for (i = 0; i < n; i++) {
1477 if (type[i] == ewmh[_NET_WM_WINDOW_TYPE_NORMAL].atom)
1479 if (type[i] == ewmh[_NET_WM_WINDOW_TYPE_DOCK].atom ||
1480 type[i] == ewmh[_NET_WM_WINDOW_TYPE_TOOLBAR].atom ||
1481 type[i] == ewmh[_NET_WM_WINDOW_TYPE_UTILITY].atom) {
1482 win->quirks = SWM_Q_FLOAT | SWM_Q_ANYWHERE;
1485 if (type[i] == ewmh[_NET_WM_WINDOW_TYPE_SPLASH].atom ||
1486 type[i] == ewmh[_NET_WM_WINDOW_TYPE_DIALOG].atom) {
1487 win->quirks = SWM_Q_FLOAT;
1495 ewmh_update_actions(struct ws_win *win)
1497 xcb_atom_t actions[SWM_EWMH_ACTION_COUNT_MAX];
1503 actions[n++] = ewmh[_NET_WM_ACTION_CLOSE].atom;
1506 actions[n++] = ewmh[_NET_WM_ACTION_MOVE].atom;
1507 actions[n++] = ewmh[_NET_WM_ACTION_RESIZE].atom;
1508 actions[n++] = ewmh[_NET_WM_ACTION_ABOVE].atom;
1511 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
1512 ewmh[_NET_WM_ALLOWED_ACTIONS].atom, XCB_ATOM_ATOM, 32, 1, actions);
1515 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
1516 #define _NET_WM_STATE_ADD 1 /* add/set property */
1517 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
1520 ewmh_change_wm_state(struct ws_win *win, xcb_atom_t state, long action)
1527 name = get_atom_name(state);
1528 DNPRINTF(SWM_D_PROP, "ewmh_change_wm_state: win %#x, state: %s, "
1529 "action: %ld\n", WINID(win), name, action);
1535 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
1536 flag = EWMH_F_FULLSCREEN;
1537 else if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
1538 flag = EWMH_F_ABOVE;
1539 else if (state == ewmh[_NET_WM_STATE_HIDDEN].atom)
1540 flag = EWMH_F_HIDDEN;
1541 else if (state == ewmh[_NET_WM_STATE_MAXIMIZED_VERT].atom ||
1542 state == ewmh[_NET_WM_STATE_MAXIMIZED_HORZ].atom)
1543 flag = EWMH_F_MAXIMIZED;
1544 else if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
1545 flag = SWM_F_MANUAL;
1546 else if (state == ewmh[_NET_WM_STATE_SKIP_PAGER].atom)
1547 flag = EWMH_F_SKIP_PAGER;
1548 else if (state == ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom)
1549 flag = EWMH_F_SKIP_TASKBAR;
1551 /* Disallow unfloating transients. */
1552 if (TRANS(win) && flag == EWMH_F_ABOVE)
1555 new_flags = win->ewmh_flags;
1558 case _NET_WM_STATE_REMOVE:
1561 case _NET_WM_STATE_ADD:
1564 case _NET_WM_STATE_TOGGLE:
1569 ewmh_apply_flags(win, new_flags);
1572 DNPRINTF(SWM_D_PROP, "ewmh_change_wm_state: done.\n");
1576 ewmh_apply_flags(struct ws_win *win, uint32_t pending)
1578 struct workspace *ws;
1581 changed = win->ewmh_flags ^ pending;
1585 DNPRINTF(SWM_D_PROP, "ewmh_apply_flags: pending: %d\n", pending);
1587 win->ewmh_flags = pending;
1590 if (changed & EWMH_F_HIDDEN) {
1592 if (focus_mode != SWM_FOCUS_FOLLOW)
1593 ws->focus_pending = get_focus_prev(win);
1598 /* Reload floating geometry in case region changed. */
1600 load_float_geom(win);
1602 /* The window is no longer iconic, prepare focus. */
1603 if (focus_mode != SWM_FOCUS_FOLLOW)
1604 ws->focus_pending = get_focus_magic(win);
1609 if (changed & EWMH_F_ABOVE) {
1610 if (ws->cur_layout != &layouts[SWM_MAX_STACK]) {
1612 load_float_geom(win);
1613 else if (!MAXIMIZED(win))
1614 store_float_geom(win);
1616 win->ewmh_flags &= ~EWMH_F_MAXIMIZED;
1617 changed &= ~EWMH_F_MAXIMIZED;
1621 win->ewmh_flags ^= EWMH_F_ABOVE & pending;
1625 if (changed & EWMH_F_MAXIMIZED) {
1626 /* VERT and/or HORZ changed. */
1628 if (!MAXIMIZED(win))
1629 load_float_geom(win);
1631 store_float_geom(win);
1634 if (MAXIMIZED(win)) {
1635 if (focus_mode != SWM_FOCUS_FOLLOW &&
1636 ws->cur_layout != &layouts[SWM_MAX_STACK]) {
1640 ws->focus_pending = win;
1644 update_window_color(win);
1648 if (changed & EWMH_F_FULLSCREEN) {
1649 if (FULLSCREEN(win)) {
1650 if (focus_mode != SWM_FOCUS_FOLLOW) {
1654 ws->focus_pending = win;
1657 load_float_geom(win);
1660 win->ewmh_flags &= ~EWMH_F_MAXIMIZED;
1664 DNPRINTF(SWM_D_PROP, "ewmh_apply_flags: done.\n");
1668 ewmh_update_wm_state(struct ws_win *win) {
1669 xcb_atom_t vals[SWM_EWMH_ACTION_COUNT_MAX];
1673 vals[n++] = ewmh[_NET_WM_STATE_HIDDEN].atom;
1674 if (FULLSCREEN(win))
1675 vals[n++] = ewmh[_NET_WM_STATE_FULLSCREEN].atom;
1676 if (MAXIMIZED_VERT(win))
1677 vals[n++] = ewmh[_NET_WM_STATE_MAXIMIZED_VERT].atom;
1678 if (MAXIMIZED_HORZ(win))
1679 vals[n++] = ewmh[_NET_WM_STATE_MAXIMIZED_HORZ].atom;
1680 if (win->ewmh_flags & EWMH_F_SKIP_PAGER)
1681 vals[n++] = ewmh[_NET_WM_STATE_SKIP_PAGER].atom;
1682 if (win->ewmh_flags & EWMH_F_SKIP_TASKBAR)
1683 vals[n++] = ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom;
1684 if (win->ewmh_flags & EWMH_F_ABOVE)
1685 vals[n++] = ewmh[_NET_WM_STATE_ABOVE].atom;
1686 if (win->ewmh_flags & SWM_F_MANUAL)
1687 vals[n++] = ewmh[_SWM_WM_STATE_MANUAL].atom;
1690 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
1691 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, n, vals);
1693 xcb_delete_property(conn, win->id, ewmh[_NET_WM_STATE].atom);
1697 ewmh_get_wm_state(struct ws_win *win)
1700 xcb_get_property_cookie_t c;
1701 xcb_get_property_reply_t *r;
1707 win->ewmh_flags = 0;
1709 c = xcb_get_property(conn, 0, win->id, ewmh[_NET_WM_STATE].atom,
1710 XCB_ATOM_ATOM, 0, UINT32_MAX);
1711 r = xcb_get_property_reply(conn, c, NULL);
1715 states = xcb_get_property_value(r);
1716 n = xcb_get_property_value_length(r) / sizeof(xcb_atom_t);
1718 for (i = 0; i < n; i++)
1719 ewmh_change_wm_state(win, states[i], _NET_WM_STATE_ADD);
1727 dumpwins(struct swm_region *r, union arg *args)
1731 xcb_get_window_attributes_cookie_t c;
1732 xcb_get_window_attributes_reply_t *wa;
1734 /* suppress unused warning since var is needed */
1737 if (r->ws == NULL) {
1738 DPRINTF("dumpwins: invalid workspace\n");
1742 DPRINTF("=== managed window list ws %02d ===\n", r->ws->idx);
1743 TAILQ_FOREACH(w, &r->ws->winlist, entry) {
1744 state = get_win_state(w->id);
1745 c = xcb_get_window_attributes(conn, w->id);
1746 wa = xcb_get_window_attributes_reply(conn, c, NULL);
1748 DPRINTF("win %#x, map_state: %d, state: %u, "
1749 "transient: %#x\n", w->id, wa->map_state,
1750 state, w->transient);
1753 DPRINTF("win %#x, failed xcb_get_window_attributes\n",
1757 DPRINTF("=== stacking order (top down) === \n");
1758 TAILQ_FOREACH(w, &r->ws->stack, stack_entry) {
1759 DPRINTF("win %#x, fs: %s, maximized: %s, above: %s, "
1760 "iconic: %s\n", w->id, YESNO(FULLSCREEN(w)),
1761 YESNO(MAXIMIZED(w)), YESNO(ABOVE(w)), YESNO(ICONIC(w)));
1764 DPRINTF("===== unmanaged window list =====\n");
1765 TAILQ_FOREACH(w, &r->ws->unmanagedlist, entry) {
1766 state = get_win_state(w->id);
1767 c = xcb_get_window_attributes(conn, w->id);
1768 wa = xcb_get_window_attributes_reply(conn, c, NULL);
1770 DPRINTF("win %#x, map_state: %d, state: %u, "
1771 "transient: %#x\n", w->id, wa->map_state,
1772 state, w->transient);
1775 DPRINTF("win %#x, failed xcb_get_window_attributes\n",
1779 DPRINTF("=================================\n");
1783 dumpwins(struct swm_region *r, union arg *s)
1788 #endif /* SWM_DEBUG */
1793 int saved_errno, status;
1796 saved_errno = errno;
1800 while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) != 0) {
1805 if (errno != ECHILD)
1806 warn("sighdlr: waitpid");
1807 #endif /* SWM_DEBUG */
1810 if (pid == searchpid)
1814 if (WIFEXITED(status)) {
1815 if (WEXITSTATUS(status) != 0)
1816 warnx("sighdlr: child exit status: %d",
1817 WEXITSTATUS(status));
1819 warnx("sighdlr: child is terminated "
1821 #endif /* SWM_DEBUG */
1835 errno = saved_errno;
1841 struct pid_e *p = NULL;
1843 DNPRINTF(SWM_D_MISC, "find_pid: %d\n", pid);
1848 TAILQ_FOREACH(p, &pidlist, entry) {
1857 name_to_pixel(int sidx, const char *colorname)
1859 uint32_t result = 0;
1860 char cname[32] = "#";
1861 xcb_screen_t *screen;
1862 xcb_colormap_t cmap;
1863 xcb_alloc_color_reply_t *cr;
1864 xcb_alloc_named_color_reply_t *nr;
1865 uint16_t rr, gg, bb;
1867 screen = get_screen(sidx);
1868 cmap = screen->default_colormap;
1870 /* color is in format rgb://rr/gg/bb */
1871 if (strncmp(colorname, "rgb:", 4) == 0) {
1872 if (parse_rgb(colorname, &rr, &gg, &bb) == -1)
1873 warnx("could not parse rgb %s", colorname);
1875 cr = xcb_alloc_color_reply(conn,
1876 xcb_alloc_color(conn, cmap, rr, gg, bb),
1882 warnx("color '%s' not found", colorname);
1885 nr = xcb_alloc_named_color_reply(conn,
1886 xcb_alloc_named_color(conn, cmap, strlen(colorname),
1889 strlcat(cname, colorname + 2, sizeof cname - 1);
1890 nr = xcb_alloc_named_color_reply(conn,
1891 xcb_alloc_named_color(conn, cmap, strlen(cname),
1898 warnx("color '%s' not found", colorname);
1905 setscreencolor(const char *val, int i, int c)
1907 if (i < 0 || i >= get_screen_count())
1910 screens[i].c[c].pixel = name_to_pixel(i, val);
1911 free(screens[i].c[c].name);
1912 if ((screens[i].c[c].name = strdup(val)) == NULL)
1917 fancy_stacker(struct workspace *ws)
1919 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1920 if (ws->cur_layout->l_stack == vertical_stack)
1921 snprintf(ws->stacker, sizeof ws->stacker,
1922 ws->l_state.vertical_flip ? "[%d>%d]" : "[%d|%d]",
1923 ws->l_state.vertical_mwin, ws->l_state.vertical_stacks);
1924 else if (ws->cur_layout->l_stack == horizontal_stack)
1925 snprintf(ws->stacker, sizeof ws->stacker,
1926 ws->l_state.horizontal_flip ? "[%dv%d]" : "[%d-%d]",
1927 ws->l_state.horizontal_mwin, ws->l_state.horizontal_stacks);
1931 plain_stacker(struct workspace *ws)
1933 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1934 if (ws->cur_layout->l_stack == vertical_stack)
1935 strlcpy(ws->stacker, ws->l_state.vertical_flip ? "[>]" : "[|]",
1936 sizeof ws->stacker);
1937 else if (ws->cur_layout->l_stack == horizontal_stack)
1938 strlcpy(ws->stacker, ws->l_state.horizontal_flip ? "[v]" : "[-]",
1939 sizeof ws->stacker);
1943 custom_region(const char *val)
1945 unsigned int x, y, w, h;
1946 int sidx, num_screens;
1947 xcb_screen_t *screen;
1949 num_screens = get_screen_count();
1950 if (sscanf(val, "screen[%d]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
1951 errx(1, "invalid custom region, "
1952 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1953 if (sidx < 1 || sidx > num_screens)
1954 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1958 if ((screen = get_screen(sidx)) == NULL)
1959 errx(1, "ERROR: can't get screen %d.", sidx);
1962 errx(1, "region %ux%u+%u+%u too small", w, h, x, y);
1964 if (x > screen->width_in_pixels ||
1965 y > screen->height_in_pixels ||
1966 w + x > screen->width_in_pixels ||
1967 h + y > screen->height_in_pixels) {
1968 warnx("ignoring region %ux%u+%u+%u - not within screen "
1969 "boundaries (%ux%u)", w, h, x, y,
1970 screen->width_in_pixels, screen->height_in_pixels);
1974 new_region(&screens[sidx], x, y, w, h);
1978 socket_setnonblock(int fd)
1982 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
1983 err(1, "fcntl F_GETFL");
1984 flags |= O_NONBLOCK;
1985 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
1986 err(1, "fcntl F_SETFL");
1990 bar_print_legacy(struct swm_region *r, const char *s)
1992 xcb_rectangle_t rect;
1997 XRectangle ibox, lbox;
2001 XmbTextExtents(bar_fs, s, len, &ibox, &lbox);
2003 switch (bar_justify) {
2004 case SWM_BAR_JUSTIFY_LEFT:
2007 case SWM_BAR_JUSTIFY_CENTER:
2008 x = (WIDTH(r) - lbox.width) / 2;
2010 case SWM_BAR_JUSTIFY_RIGHT:
2011 x = WIDTH(r) - lbox.width - SWM_BAR_OFFSET;
2015 if (x < SWM_BAR_OFFSET)
2020 rect.width = WIDTH(r->bar);
2021 rect.height = HEIGHT(r->bar);
2023 /* clear back buffer */
2024 gcv[0] = r->s->c[SWM_S_COLOR_BAR].pixel;
2025 xcb_change_gc(conn, r->s->bar_gc, XCB_GC_FOREGROUND, gcv);
2026 xcb_poly_fill_rectangle(conn, r->bar->buffer, r->s->bar_gc, 1, &rect);
2028 /* draw back buffer */
2029 gcvd.graphics_exposures = 0;
2030 draw = XCreateGC(display, r->bar->buffer, GCGraphicsExposures, &gcvd);
2031 XSetForeground(display, draw, r->s->c[SWM_S_COLOR_BAR_FONT].pixel);
2032 DRAWSTRING(display, r->bar->buffer, bar_fs, draw,
2033 x, (bar_fs_extents->max_logical_extent.height - lbox.height) / 2 -
2035 XFreeGC(display, draw);
2038 xcb_copy_area(conn, r->bar->buffer, r->bar->id, r->s->bar_gc, 0, 0,
2039 0, 0, WIDTH(r->bar), HEIGHT(r->bar));
2043 bar_print(struct swm_region *r, const char *s)
2046 xcb_rectangle_t rect;
2054 XftTextExtentsUtf8(display, bar_font, (FcChar8 *)s, len, &info);
2056 switch (bar_justify) {
2057 case SWM_BAR_JUSTIFY_LEFT:
2060 case SWM_BAR_JUSTIFY_CENTER:
2061 x = (WIDTH(r) - info.width) / 2;
2063 case SWM_BAR_JUSTIFY_RIGHT:
2064 x = WIDTH(r) - info.width - SWM_BAR_OFFSET;
2068 if (x < SWM_BAR_OFFSET)
2073 rect.width = WIDTH(r->bar);
2074 rect.height = HEIGHT(r->bar);
2076 /* clear back buffer */
2077 gcv[0] = r->s->c[SWM_S_COLOR_BAR].pixel;
2078 xcb_change_gc(conn, r->s->bar_gc, XCB_GC_FOREGROUND, gcv);
2079 xcb_poly_fill_rectangle(conn, r->bar->buffer, r->s->bar_gc, 1, &rect);
2081 /* draw back buffer */
2082 draw = XftDrawCreate(display, r->bar->buffer,
2083 DefaultVisual(display, r->s->idx),
2084 DefaultColormap(display, r->s->idx));
2086 XftDrawStringUtf8(draw, &bar_font_color, bar_font, x,
2087 (HEIGHT(r->bar) + bar_font->height) / 2 - bar_font->descent,
2090 XftDrawDestroy(draw);
2093 xcb_copy_area(conn, r->bar->buffer, r->bar->id, r->s->bar_gc, 0, 0,
2094 0, 0, WIDTH(r->bar), HEIGHT(r->bar));
2098 bar_extra_stop(void)
2102 bzero(bar_pipe, sizeof bar_pipe);
2105 kill(bar_pid, SIGTERM);
2108 strlcpy(bar_ext, "", sizeof bar_ext);
2113 bar_window_class(char *s, size_t sz, struct swm_region *r)
2115 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
2117 if (r->ws->focus->ch.class_name != NULL)
2118 strlcat(s, r->ws->focus->ch.class_name, sz);
2122 bar_window_instance(char *s, size_t sz, struct swm_region *r)
2124 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
2126 if (r->ws->focus->ch.instance_name != NULL)
2127 strlcat(s, r->ws->focus->ch.instance_name, sz);
2131 bar_window_class_instance(char *s, size_t sz, struct swm_region *r)
2133 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
2136 bar_window_class(s, sz, r);
2137 strlcat(s, ":", sz);
2138 bar_window_instance(s, sz, r);
2142 bar_window_state(char *s, size_t sz, struct swm_region *r)
2144 if (r == NULL || r ->ws == NULL || r->ws->focus == NULL)
2146 if (MAXIMIZED(r->ws->focus))
2147 strlcat(s, "(m)", sz);
2148 else if (ABOVE(r->ws->focus))
2149 strlcat(s, "(f)", sz);
2153 bar_window_name(char *s, size_t sz, struct swm_region *r)
2157 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
2160 title = get_win_name(r->ws->focus->id);
2161 strlcat(s, title, sz);
2166 bar_urgent(char *s, size_t sz)
2169 int i, j, num_screens;
2170 int urgent[SWM_WS_MAX];
2172 xcb_get_property_cookie_t c;
2173 xcb_icccm_wm_hints_t hints;
2175 for (i = 0; i < workspace_limit; i++)
2178 num_screens = get_screen_count();
2179 for (i = 0; i < num_screens; i++)
2180 for (j = 0; j < workspace_limit; j++)
2181 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry) {
2182 c = xcb_icccm_get_wm_hints(conn, win->id);
2183 if (xcb_icccm_get_wm_hints_reply(conn, c,
2186 if (hints.flags & XCB_ICCCM_WM_HINT_X_URGENCY)
2190 for (i = 0; i < workspace_limit; i++) {
2192 snprintf(b, sizeof b, "%d ", i + 1);
2194 snprintf(b, sizeof b, "- ");
2200 bar_workspace_name(char *s, size_t sz, struct swm_region *r)
2202 if (r == NULL || r->ws == NULL)
2204 if (r->ws->name != NULL)
2205 strlcat(s, r->ws->name, sz);
2208 /* build the default bar format according to the defined enabled options */
2210 bar_fmt(const char *fmtexp, char *fmtnew, struct swm_region *r, size_t sz)
2214 /* if format provided, just copy the buffers */
2215 if (bar_format != NULL) {
2216 strlcpy(fmtnew, fmtexp, sz);
2220 /* reset the output buffer */
2223 strlcat(fmtnew, "+N:+I ", sz);
2225 strlcat(fmtnew, "+S", sz);
2226 strlcat(fmtnew, " ", sz);
2228 /* only show the workspace name if there's actually one */
2229 if (r != NULL && r->ws != NULL && r->ws->name != NULL)
2230 strlcat(fmtnew, "<+D>", sz);
2232 /* If enabled, only show the iconic count if there are iconic wins. */
2233 if (iconic_enabled && r != NULL && r->ws != NULL)
2234 TAILQ_FOREACH(w, &r->ws->winlist, entry)
2236 strlcat(fmtnew, "{+M}", sz);
2240 strlcat(fmtnew, "+3<", sz);
2242 if (clock_enabled) {
2243 strlcat(fmtnew, fmtexp, sz);
2244 strlcat(fmtnew, "+4<", sz);
2247 /* bar_urgent already adds the space before the last asterisk */
2249 strlcat(fmtnew, "* +U*+4<", sz);
2251 if (window_class_enabled) {
2252 strlcat(fmtnew, "+C", sz);
2253 if (!window_instance_enabled)
2254 strlcat(fmtnew, "+4<", sz);
2257 /* checks needed by the colon and floating strlcat(3) calls below */
2258 if (r != NULL && r->ws != NULL && r->ws->focus != NULL) {
2259 if (window_instance_enabled) {
2260 if (window_class_enabled)
2261 strlcat(fmtnew, ":", sz);
2262 strlcat(fmtnew, "+T+4<", sz);
2264 if (window_name_enabled) {
2265 if (ABOVE(r->ws->focus) || MAXIMIZED(r->ws->focus))
2266 strlcat(fmtnew, "+F ", sz);
2267 strlcat(fmtnew, "+64W ", sz);
2271 /* finally add the action script output and the version */
2272 strlcat(fmtnew, "+4<+A+4<+V", sz);
2276 bar_replace_pad(char *tmp, int *limit, size_t sz)
2278 /* special case; no limit given, pad one space, instead */
2279 if (*limit == (int)sz - 1)
2281 snprintf(tmp, sz, "%*s", *limit, " ");
2284 /* replaces the bar format character sequences (like in tmux(1)) */
2286 bar_replace_seq(char *fmt, char *fmtrep, struct swm_region *r, size_t *offrep,
2291 char tmp[SWM_BAR_MAX];
2292 int limit, size, count;
2295 /* reset strlcat(3) buffer */
2298 /* get number, if any */
2301 if (sscanf(fmt, "%d%n", &limit, &size) != 1)
2302 limit = sizeof tmp - 1;
2303 if (limit <= 0 || limit >= (int)sizeof tmp)
2304 limit = sizeof tmp - 1;
2306 /* there is nothing to replace (ie EOL) */
2313 bar_replace_pad(tmp, &limit, sizeof tmp);
2316 snprintf(tmp, sizeof tmp, "%s", bar_ext);
2319 bar_window_class(tmp, sizeof tmp, r);
2322 bar_workspace_name(tmp, sizeof tmp, r);
2325 bar_window_state(tmp, sizeof tmp, r);
2328 snprintf(tmp, sizeof tmp, "%d", r->ws->idx + 1);
2332 TAILQ_FOREACH(w, &r->ws->winlist, entry)
2336 snprintf(tmp, sizeof tmp, "%d", count);
2339 snprintf(tmp, sizeof tmp, "%d", r->s->idx + 1);
2342 bar_window_class_instance(tmp, sizeof tmp, r);
2345 snprintf(tmp, sizeof tmp, "%s", r->ws->stacker);
2348 bar_window_instance(tmp, sizeof tmp, r);
2351 bar_urgent(tmp, sizeof tmp);
2354 snprintf(tmp, sizeof tmp, "%s", bar_vertext);
2357 bar_window_name(tmp, sizeof tmp, r);
2360 /* unknown character sequence; copy as-is */
2361 snprintf(tmp, sizeof tmp, "+%c", *fmt);
2367 if ((int)len < limit)
2369 while (limit-- > 0) {
2370 if (*offrep >= sz - 1)
2372 fmtrep[(*offrep)++] = *ptr++;
2380 bar_replace(char *fmt, char *fmtrep, struct swm_region *r, size_t sz)
2385 while (*fmt != '\0') {
2387 /* skip ordinary characters */
2390 fmtrep[off++] = *fmt++;
2394 /* character sequence found; replace it */
2395 fmt = bar_replace_seq(fmt, fmtrep, r, &off, sz);
2404 bar_fmt_expand(char *fmtexp, size_t sz)
2411 /* start by grabbing the current time and date */
2413 localtime_r(&tmt, &tm);
2415 /* figure out what to expand */
2416 if (bar_format != NULL)
2418 else if (bar_format == NULL && clock_enabled)
2420 /* if nothing to expand bail out */
2426 /* copy as-is, just in case the format shouldn't be expanded below */
2427 strlcpy(fmtexp, fmt, sz);
2428 /* finally pass the string through strftime(3) */
2429 #ifndef SWM_DENY_CLOCK_FORMAT
2430 if ((len = strftime(fmtexp, sz, fmt, &tm)) == 0)
2431 warnx("format too long");
2436 /* Redraws the bar; need to follow with xcb_flush() or focus_flush(). */
2440 char fmtexp[SWM_BAR_MAX], fmtnew[SWM_BAR_MAX];
2441 char fmtrep[SWM_BAR_MAX];
2443 struct swm_region *r;
2445 /* expand the format by first passing it through strftime(3) */
2446 bar_fmt_expand(fmtexp, sizeof fmtexp);
2448 num_screens = get_screen_count();
2449 for (i = 0; i < num_screens; i++) {
2450 TAILQ_FOREACH(r, &screens[i].rl, entry) {
2454 if (bar_enabled && r->ws->bar_enabled)
2455 xcb_map_window(conn, r->bar->id);
2457 xcb_unmap_window(conn, r->bar->id);
2461 if (startup_exception)
2462 snprintf(fmtrep, sizeof fmtrep, "total "
2463 "exceptions: %d, first exception: %s",
2467 bar_fmt(fmtexp, fmtnew, r, sizeof fmtnew);
2468 bar_replace(fmtnew, fmtrep, r, sizeof fmtrep);
2470 if (bar_font_legacy)
2471 bar_print_legacy(r, fmtrep);
2473 bar_print(r, fmtrep);
2479 * Reads external script output; call when stdin is readable.
2480 * Returns 1 if bar_ext was updated; otherwise 0.
2483 bar_extra_update(void)
2486 char b[SWM_BAR_MAX];
2492 while (fgets(b, sizeof(b), stdin) != NULL) {
2495 if (b[len - 1] == '\n') {
2496 /* Remove newline. */
2499 /* "Clear" bar_ext. */
2502 /* Flush buffered output. */
2503 strlcpy(bar_ext, bar_ext_buf, sizeof(bar_ext));
2504 bar_ext_buf[0] = '\0';
2506 /* Append new output to bar. */
2507 strlcat(bar_ext, b, sizeof(bar_ext));
2511 /* Buffer output. */
2512 strlcat(bar_ext_buf, b, sizeof(bar_ext_buf));
2517 if (errno != EAGAIN) {
2518 warn("bar_action failed");
2527 bar_toggle(struct swm_region *r, union arg *args)
2529 struct swm_region *tmpr;
2532 /* suppress unused warnings since vars are needed */
2536 DNPRINTF(SWM_D_BAR, "bar_toggle\n");
2539 case SWM_ARG_ID_BAR_TOGGLE_WS:
2540 /* Only change if master switch is enabled. */
2542 r->ws->bar_enabled = !r->ws->bar_enabled;
2544 bar_enabled = r->ws->bar_enabled = 1;
2546 case SWM_ARG_ID_BAR_TOGGLE:
2547 bar_enabled = !bar_enabled;
2551 /* update bars as necessary */
2552 num_screens = get_screen_count();
2553 for (i = 0; i < num_screens; i++)
2554 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
2556 if (bar_enabled && tmpr->ws->bar_enabled)
2557 xcb_map_window(conn, tmpr->bar->id);
2559 xcb_unmap_window(conn, tmpr->bar->id);
2564 /* must be after stack */
2571 bar_extra_setup(void)
2573 /* do this here because the conf file is in memory */
2574 if (!bar_extra && bar_argv[0]) {
2575 /* launch external status app */
2577 if (pipe(bar_pipe) == -1)
2578 err(1, "pipe error");
2579 socket_setnonblock(bar_pipe[0]);
2580 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
2582 /* Set stdin to read from the pipe. */
2583 if (dup2(bar_pipe[0], STDIN_FILENO) == -1)
2586 /* Set stdout to write to the pipe. */
2587 if (dup2(bar_pipe[1], STDOUT_FILENO) == -1)
2590 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
2591 err(1, "could not disable SIGPIPE");
2592 switch (bar_pid = fork()) {
2594 err(1, "cannot fork");
2598 execvp(bar_argv[0], bar_argv);
2599 err(1, "%s external app failed", bar_argv[0]);
2601 default: /* parent */
2606 atexit(kill_bar_extra_atexit);
2611 kill_bar_extra_atexit(void)
2614 kill(bar_pid, SIGTERM);
2622 while ((s = index(s, '-'))) {
2627 return (count == 14);
2633 char *default_string;
2634 char **missing_charsets;
2635 int num_missing_charsets = 0;
2639 XFreeFontSet(display, bar_fs);
2643 DNPRINTF(SWM_D_INIT, "fontset_init: loading bar_fonts: %s\n", bar_fonts);
2645 bar_fs = XCreateFontSet(display, bar_fonts, &missing_charsets,
2646 &num_missing_charsets, &default_string);
2648 if (num_missing_charsets > 0) {
2649 warnx("Unable to load charset(s):");
2651 for (i = 0; i < num_missing_charsets; ++i)
2652 warnx("%s", missing_charsets[i]);
2654 XFreeStringList(missing_charsets);
2656 if (strcmp(default_string, ""))
2657 warnx("Glyphs from those sets will be replaced "
2658 "by '%s'.", default_string);
2660 warnx("Glyphs from those sets won't be drawn.");
2664 errx(1, "Error creating font set structure.");
2666 bar_fs_extents = XExtentsOfFontSet(bar_fs);
2668 bar_height = bar_fs_extents->max_logical_extent.height +
2669 2 * bar_border_width;
2676 xft_init(struct swm_region *r)
2678 char *font, *d, *search;
2681 if (bar_font == NULL) {
2682 if ((d = strdup(bar_fonts)) == NULL)
2683 errx(1, "insufficient memory.");
2685 while ((font = strsep(&search, ",")) != NULL) {
2689 DNPRINTF(SWM_D_INIT, "xft_init: try font %s\n", font);
2692 bar_font = XftFontOpenXlfd(display, r->s->idx,
2695 bar_font = XftFontOpenName(display, r->s->idx,
2699 if (bar_font == NULL) {
2700 warnx("unable to load font %s", font);
2703 DNPRINTF(SWM_D_INIT, "successfully opened "
2711 if (bar_font == NULL)
2712 errx(1, "unable to open a font");
2714 PIXEL_TO_XRENDERCOLOR(r->s->c[SWM_S_COLOR_BAR_FONT].pixel, color);
2716 if (!XftColorAllocValue(display, DefaultVisual(display, r->s->idx),
2717 DefaultColormap(display, r->s->idx), &color, &bar_font_color))
2718 warn("Xft error: unable to allocate color.");
2720 PIXEL_TO_XRENDERCOLOR(r->s->c[SWM_S_COLOR_BAR].pixel, color);
2722 if (!XftColorAllocValue(display, DefaultVisual(display, r->s->idx),
2723 DefaultColormap(display, r->s->idx), &color, &search_font_color))
2724 warn("Xft error: unable to allocate color.");
2726 bar_height = bar_font->height + 2 * bar_border_width;
2733 bar_setup(struct swm_region *r)
2735 xcb_screen_t *screen;
2738 DNPRINTF(SWM_D_BAR, "bar_setup: screen %d.\n",
2741 if ((screen = get_screen(r->s->idx)) == NULL)
2742 errx(1, "ERROR: can't get screen %d.", r->s->idx);
2747 if ((r->bar = calloc(1, sizeof(struct swm_bar))) == NULL)
2748 err(1, "bar_setup: calloc: failed to allocate memory.");
2750 if (bar_font_legacy)
2756 Y(r->bar) = bar_at_bottom ? (Y(r) + HEIGHT(r) - bar_height) : Y(r);
2757 WIDTH(r->bar) = WIDTH(r) - 2 * bar_border_width;
2758 HEIGHT(r->bar) = bar_height - 2 * bar_border_width;
2760 /* Assume region is unfocused when we create the bar. */
2761 r->bar->id = xcb_generate_id(conn);
2762 wa[0] = r->s->c[SWM_S_COLOR_BAR].pixel;
2763 wa[1] = r->s->c[SWM_S_COLOR_BAR_BORDER_UNFOCUS].pixel;
2764 wa[2] = XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_POINTER_MOTION |
2765 XCB_EVENT_MASK_POINTER_MOTION_HINT;
2767 xcb_create_window(conn, XCB_COPY_FROM_PARENT, r->bar->id, r->s->root,
2768 X(r->bar), Y(r->bar), WIDTH(r->bar), HEIGHT(r->bar),
2769 bar_border_width, XCB_WINDOW_CLASS_INPUT_OUTPUT,
2770 XCB_COPY_FROM_PARENT, XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL
2771 | XCB_CW_EVENT_MASK, wa);
2773 /* Stack bar window above region window to start. */
2775 wa[1] = XCB_STACK_MODE_ABOVE;
2777 xcb_configure_window(conn, r->bar->id, XCB_CONFIG_WINDOW_SIBLING |
2778 XCB_CONFIG_WINDOW_STACK_MODE, wa);
2780 r->bar->buffer = xcb_generate_id(conn);
2781 xcb_create_pixmap(conn, screen->root_depth, r->bar->buffer, r->bar->id,
2782 WIDTH(r->bar), HEIGHT(r->bar));
2785 xcb_randr_select_input(conn, r->bar->id,
2786 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE);
2789 xcb_map_window(conn, r->bar->id);
2791 DNPRINTF(SWM_D_BAR, "bar_setup: win %#x, (x,y) w x h: (%d,%d) "
2792 "%d x %d\n", WINID(r->bar), X(r->bar), Y(r->bar), WIDTH(r->bar),
2799 bar_cleanup(struct swm_region *r)
2803 xcb_destroy_window(conn, r->bar->id);
2804 xcb_free_pixmap(conn, r->bar->buffer);
2810 set_win_state(struct ws_win *win, uint8_t state)
2812 uint16_t data[2] = { state, XCB_ATOM_NONE };
2814 DNPRINTF(SWM_D_EVENT, "set_win_state: win %#x, state: %u\n",
2820 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id, a_state,
2821 a_state, 32, 2, data);
2825 get_win_state(xcb_window_t w)
2827 xcb_get_property_reply_t *r;
2828 xcb_get_property_cookie_t c;
2829 uint32_t result = 0;
2831 c = xcb_get_property(conn, 0, w, a_state, a_state, 0L, 2L);
2832 r = xcb_get_property_reply(conn, c, NULL);
2834 if (r->type == a_state && r->format == 32 && r->length == 2)
2835 result = *((uint32_t *)xcb_get_property_value(r));
2839 DNPRINTF(SWM_D_MISC, "get_win_state property: win %#x state %u\n", w,
2845 version(struct swm_region *r, union arg *args)
2847 /* suppress unused warnings since vars are needed */
2851 bar_version = !bar_version;
2853 snprintf(bar_vertext, sizeof bar_vertext,
2854 "Version: %s Build: %s", SPECTRWM_VERSION, buildstr);
2856 strlcpy(bar_vertext, "", sizeof bar_vertext);
2863 client_msg(struct ws_win *win, xcb_atom_t a, xcb_timestamp_t t)
2865 xcb_client_message_event_t ev;
2873 name = get_atom_name(a);
2874 DNPRINTF(SWM_D_EVENT, "client_msg: win %#x, atom: %s(%u), "
2876 win->id, name, a, t);
2880 bzero(&ev, sizeof ev);
2881 ev.response_type = XCB_CLIENT_MESSAGE;
2882 ev.window = win->id;
2885 ev.data.data32[0] = a;
2886 ev.data.data32[1] = t;
2888 xcb_send_event(conn, 0, win->id,
2889 XCB_EVENT_MASK_NO_EVENT, (const char *)&ev);
2892 /* synthetic response to a ConfigureRequest when not making a change */
2894 config_win(struct ws_win *win, xcb_configure_request_event_t *ev)
2896 xcb_configure_notify_event_t ce;
2901 /* send notification of unchanged state. */
2902 bzero(&ce, sizeof(ce));
2903 ce.response_type = XCB_CONFIGURE_NOTIFY;
2906 ce.width = WIDTH(win);
2907 ce.height = HEIGHT(win);
2908 ce.override_redirect = 0;
2913 ce.window = win->id;
2914 ce.border_width = BORDER(win);
2915 ce.above_sibling = XCB_WINDOW_NONE;
2918 ce.event = ev->window;
2919 ce.window = ev->window;
2921 /* make response appear more WM_SIZE_HINTS-compliant */
2922 if (win->sh.flags) {
2923 DNPRINTF(SWM_D_MISC, "config_win: hints: win %#x,"
2924 " sh.flags: %u, min: %d x %d, max: %d x %d, inc: "
2925 "%d x %d\n", win->id, win->sh.flags, SH_MIN_W(win),
2926 SH_MIN_H(win), SH_MAX_W(win), SH_MAX_H(win),
2927 SH_INC_W(win), SH_INC_H(win));
2932 /* the hint may be set... to 0! */
2933 if (SH_MIN_W(win) > 0 && ce.width < SH_MIN_W(win))
2934 ce.width = SH_MIN_W(win);
2935 if (SH_MIN_H(win) > 0 && ce.height < SH_MIN_H(win))
2936 ce.height = SH_MIN_H(win);
2941 /* may also be advertized as 0 */
2942 if (SH_MAX_W(win) > 0 && ce.width > SH_MAX_W(win))
2943 ce.width = SH_MAX_W(win);
2944 if (SH_MAX_H(win) > 0 && ce.height > SH_MAX_H(win))
2945 ce.height = SH_MAX_H(win);
2948 /* resize increment. */
2950 if (SH_INC_W(win) > 1 && ce.width > SH_INC_W(win))
2951 ce.width -= (ce.width - SH_MIN_W(win)) %
2953 if (SH_INC_H(win) > 1 && ce.height > SH_INC_H(win))
2954 ce.height -= (ce.height - SH_MIN_H(win)) %
2958 /* adjust x and y for requested border_width. */
2959 ce.x += BORDER(win) - ev->border_width;
2960 ce.y += BORDER(win) - ev->border_width;
2961 ce.border_width = ev->border_width;
2962 ce.above_sibling = ev->sibling;
2965 DNPRINTF(SWM_D_MISC, "config_win: ewmh: %s, win %#x, (x,y) w x h: "
2966 "(%d,%d) %d x %d, border: %d\n", YESNO(ev == NULL), win->id, ce.x,
2967 ce.y, ce.width, ce.height, ce.border_width);
2969 xcb_send_event(conn, 0, win->id, XCB_EVENT_MASK_STRUCTURE_NOTIFY,
2974 count_win(struct workspace *ws, int count_transient)
2979 TAILQ_FOREACH(win, &ws->winlist, entry) {
2980 if (!count_transient && FLOATING(win))
2986 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
2992 quit(struct swm_region *r, union arg *args)
2994 /* suppress unused warnings since vars are needed */
2998 DNPRINTF(SWM_D_MISC, "quit\n");
3003 raise_window(struct ws_win *win)
3005 struct ws_win *target = NULL;
3006 struct swm_region *r;
3007 struct workspace *ws;
3009 if (win == NULL || (r = win->ws->r) == NULL)
3013 DNPRINTF(SWM_D_EVENT, "raise_window: win %#x\n", win->id);
3015 TAILQ_FOREACH(target, &ws->stack, stack_entry) {
3016 if (target == win || ICONIC(target))
3018 if (ws->cur_layout == &layouts[SWM_MAX_STACK])
3020 if (TRANS(win) && (win->transient == target->transient ||
3021 win->transient == target->id))
3023 if (FULLSCREEN(win))
3025 if (FULLSCREEN(target))
3029 if (MAXIMIZED(target))
3031 if (ABOVE(win) || TRANS(win))
3033 if (!ABOVE(target) && !TRANS(target))
3037 if (target != NULL) {
3038 /* Change stack position. */
3039 TAILQ_REMOVE(&ws->stack, win, stack_entry);
3040 TAILQ_INSERT_BEFORE(target, win, stack_entry);
3041 update_win_stacking(win);
3045 if (swm_debug & SWM_D_STACK) {
3046 DPRINTF("=== stacking order (top down) === \n");
3047 TAILQ_FOREACH(target, &r->ws->stack, stack_entry) {
3048 DPRINTF("win %#x, fs: %s, maximized: %s, above: %s, "
3049 "iconic: %s\n", target->id, YESNO(FULLSCREEN(target)),
3050 YESNO(MAXIMIZED(target)), YESNO(ABOVE(target)),
3051 YESNO(ICONIC(target)));
3055 DNPRINTF(SWM_D_EVENT, "raise_window: done\n");
3059 update_win_stacking(struct ws_win *win)
3061 struct ws_win *sibling;
3062 struct swm_region *r;
3065 if (win == NULL || (r = win->ws->r) == NULL)
3068 sibling = TAILQ_NEXT(win, stack_entry);
3069 if (sibling != NULL && FLOATING(win) == FLOATING(sibling))
3070 val[0] = sibling->id;
3072 val[0] = FLOATING(win) ? r->bar->id : r->id;
3074 DNPRINTF(SWM_D_EVENT, "update_win_stacking: %#x, sibling %#x\n",
3077 val[1] = XCB_STACK_MODE_ABOVE;
3079 xcb_configure_window(conn, win->id, XCB_CONFIG_WINDOW_SIBLING |
3080 XCB_CONFIG_WINDOW_STACK_MODE, val);
3084 map_window(struct ws_win *win)
3089 DNPRINTF(SWM_D_EVENT, "map_window: win %#x, mapped: %s\n",
3090 win->id, YESNO(win->mapped));
3095 xcb_map_window(conn, win->id);
3096 set_win_state(win, XCB_ICCCM_WM_STATE_NORMAL);
3101 unmap_window(struct ws_win *win)
3106 DNPRINTF(SWM_D_EVENT, "unmap_window: win %#x, mapped: %s\n", win->id,
3107 YESNO(win->mapped));
3112 xcb_unmap_window(conn, win->id);
3113 set_win_state(win, XCB_ICCCM_WM_STATE_ICONIC);
3121 int i, j, num_screens;
3123 num_screens = get_screen_count();
3124 for (i = 0; i < num_screens; i++)
3125 for (j = 0; j < workspace_limit; j++)
3126 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
3131 fake_keypress(struct ws_win *win, xcb_keysym_t keysym, uint16_t modifiers)
3133 xcb_key_press_event_t event;
3134 xcb_keycode_t *keycode;
3139 keycode = xcb_key_symbols_get_keycode(syms, keysym);
3141 DNPRINTF(SWM_D_MISC, "fake_keypress: win %#x, keycode %u\n",
3144 bzero(&event, sizeof(event));
3145 event.event = win->id;
3146 event.root = win->s->root;
3147 event.child = XCB_WINDOW_NONE;
3148 event.time = XCB_CURRENT_TIME;
3149 event.event_x = X(win);
3150 event.event_y = Y(win);
3153 event.same_screen = 1;
3154 event.detail = *keycode;
3155 event.state = modifiers;
3157 event.response_type = XCB_KEY_PRESS;
3158 xcb_send_event(conn, 1, win->id,
3159 XCB_EVENT_MASK_KEY_PRESS, (const char *)&event);
3161 event.response_type = XCB_KEY_RELEASE;
3162 xcb_send_event(conn, 1, win->id,
3163 XCB_EVENT_MASK_KEY_RELEASE, (const char *)&event);
3169 restart(struct swm_region *r, union arg *args)
3171 /* suppress unused warning since var is needed */
3175 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
3179 execvp(start_argv[0], start_argv);
3180 warn("execvp failed");
3185 get_pointer_win(xcb_window_t root)
3187 struct ws_win *win = NULL;
3188 xcb_query_pointer_reply_t *r;
3190 DNPRINTF(SWM_D_EVENT, "get_pointer_win: root: %#x.\n", root);
3192 r = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, root), NULL);
3194 win = find_window(r->child);
3196 DNPRINTF(SWM_D_EVENT, "get_pointer_win: %#x.\n",
3199 DNPRINTF(SWM_D_EVENT, "get_pointer_win: none.\n");
3208 root_to_region(xcb_window_t root, int check)
3211 struct swm_region *r = NULL;
3213 xcb_query_pointer_reply_t *qpr;
3214 xcb_get_input_focus_reply_t *gifr;
3216 DNPRINTF(SWM_D_MISC, "root_to_region: win %#x\n", root);
3218 num_screens = get_screen_count();
3219 for (i = 0; i < num_screens; i++)
3220 if (screens[i].root == root)
3223 if (check & SWM_CK_REGION)
3224 r = screens[i].r_focus;
3226 if (r == NULL && check & SWM_CK_FOCUS) {
3227 /* Try to find an actively focused window */
3228 gifr = xcb_get_input_focus_reply(conn,
3229 xcb_get_input_focus(conn), NULL);
3231 cfw = find_window(gifr->focus);
3232 if (cfw && cfw->ws->r)
3239 if (r == NULL && check & SWM_CK_POINTER) {
3240 /* No region with an active focus; try to use pointer. */
3241 qpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn,
3242 screens[i].root), NULL);
3244 DNPRINTF(SWM_D_MISC, "root_to_region: pointer: "
3245 "(%d,%d)\n", qpr->root_x, qpr->root_y);
3246 TAILQ_FOREACH(r, &screens[i].rl, entry)
3247 if (X(r) <= qpr->root_x &&
3248 qpr->root_x < MAX_X(r) &&
3249 Y(r) <= qpr->root_y &&
3250 qpr->root_y < MAX_Y(r))
3257 if (r == NULL && check & SWM_CK_FALLBACK)
3258 r = TAILQ_FIRST(&screens[i].rl);
3260 DNPRINTF(SWM_D_MISC, "root_to_region: idx: %d\n", get_region_index(r));
3266 find_unmanaged_window(xcb_window_t id)
3269 int i, j, num_screens;
3271 num_screens = get_screen_count();
3272 for (i = 0; i < num_screens; i++)
3273 for (j = 0; j < workspace_limit; j++)
3274 TAILQ_FOREACH(win, &screens[i].ws[j].unmanagedlist,
3282 find_window(xcb_window_t id)
3285 int i, j, num_screens;
3286 xcb_query_tree_reply_t *r;
3288 num_screens = get_screen_count();
3289 for (i = 0; i < num_screens; i++)
3290 for (j = 0; j < workspace_limit; j++)
3291 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
3295 r = xcb_query_tree_reply(conn, xcb_query_tree(conn, id), NULL);
3299 /* if we were looking for the parent return that window instead */
3300 if (r->parent == 0 || r->root == r->parent) {
3305 /* look for parent */
3306 for (i = 0; i < num_screens; i++)
3307 for (j = 0; j < workspace_limit; j++)
3308 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
3309 if (r->parent == win->id) {
3319 spawn(int ws_idx, union arg *args, int close_fd)
3324 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
3326 close(xcb_get_file_descriptor(conn));
3328 setenv("LD_PRELOAD", SWM_LIB, 1);
3330 if (asprintf(&ret, "%d", ws_idx) == -1) {
3331 warn("spawn: asprintf SWM_WS");
3334 setenv("_SWM_WS", ret, 1);
3338 if (asprintf(&ret, "%d", getpid()) == -1) {
3339 warn("spawn: asprintf _SWM_PID");
3342 setenv("_SWM_PID", ret, 1);
3346 if (setsid() == -1) {
3347 warn("spawn: setsid");
3353 * close stdin and stdout to prevent interaction between apps
3354 * and the baraction script
3355 * leave stderr open to record errors
3357 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) == -1) {
3358 warn("spawn: open");
3361 dup2(fd, STDIN_FILENO);
3362 dup2(fd, STDOUT_FILENO);
3367 execvp(args->argv[0], args->argv);
3369 warn("spawn: execvp");
3374 kill_refs(struct ws_win *win)
3376 int i, x, num_screens;
3377 struct swm_region *r;
3378 struct workspace *ws;
3383 num_screens = get_screen_count();
3384 for (i = 0; i < num_screens; i++)
3385 TAILQ_FOREACH(r, &screens[i].rl, entry)
3386 for (x = 0; x < workspace_limit; x++) {
3388 if (win == ws->focus)
3390 if (win == ws->focus_prev)
3391 ws->focus_prev = NULL;
3396 validate_win(struct ws_win *testwin)
3399 struct workspace *ws;
3400 struct swm_region *r;
3401 int i, x, num_screens;
3403 if (testwin == NULL)
3406 num_screens = get_screen_count();
3407 for (i = 0; i < num_screens; i++)
3408 TAILQ_FOREACH(r, &screens[i].rl, entry)
3409 for (x = 0; x < workspace_limit; x++) {
3411 TAILQ_FOREACH(win, &ws->winlist, entry)
3419 validate_ws(struct workspace *testws)
3421 struct swm_region *r;
3422 struct workspace *ws;
3423 int i, x, num_screens;
3425 /* validate all ws */
3426 num_screens = get_screen_count();
3427 for (i = 0; i < num_screens; i++)
3428 TAILQ_FOREACH(r, &screens[i].rl, entry)
3429 for (x = 0; x < workspace_limit; x++) {
3438 unfocus_win(struct ws_win *win)
3440 xcb_window_t none = XCB_WINDOW_NONE;
3442 DNPRINTF(SWM_D_FOCUS, "unfocus_win: win %#x\n", WINID(win));
3447 if (win->ws == NULL) {
3448 DNPRINTF(SWM_D_FOCUS, "unfocus_win: NULL ws.\n");
3452 if (validate_ws(win->ws)) {
3453 DNPRINTF(SWM_D_FOCUS, "unfocus_win: invalid ws.\n");
3457 if (win->ws->r == NULL) {
3458 DNPRINTF(SWM_D_FOCUS, "unfocus_win: NULL region.\n");
3462 if (validate_win(win)) {
3463 DNPRINTF(SWM_D_FOCUS, "unfocus_win: invalid win.\n");
3468 if (win->ws->focus == win) {
3469 win->ws->focus = NULL;
3470 win->ws->focus_prev = win;
3473 if (validate_win(win->ws->focus)) {
3474 kill_refs(win->ws->focus);
3475 win->ws->focus = NULL;
3478 if (validate_win(win->ws->focus_prev)) {
3479 kill_refs(win->ws->focus_prev);
3480 win->ws->focus_prev = NULL;
3483 update_window_color(win);
3485 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
3486 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1, &none);
3488 DNPRINTF(SWM_D_FOCUS, "unfocus_win: done.\n");
3492 focus_win(struct ws_win *win)
3494 struct ws_win *cfw = NULL, *parent = NULL, *w, *tmpw;
3495 struct workspace *ws;
3496 xcb_get_input_focus_reply_t *gifr;
3498 DNPRINTF(SWM_D_FOCUS, "focus_win: win %#x\n", WINID(win));
3500 if (win == NULL || win->ws == NULL || !win->mapped)
3505 if (validate_ws(ws))
3508 if (validate_win(win)) {
3513 gifr = xcb_get_input_focus_reply(conn, xcb_get_input_focus(conn), NULL);
3515 cfw = find_window(gifr->focus);
3516 if (cfw != NULL && cfw != win) {
3517 if (cfw->ws != ws && cfw->ws->r != NULL) {
3518 /* Change border to unfocused color. */
3519 xcb_change_window_attributes(conn, cfw->id,
3520 XCB_CW_BORDER_PIXEL,
3521 &cfw->s->c[(MAXIMIZED(cfw) ?
3522 SWM_S_COLOR_UNFOCUS_MAXIMIZED :
3523 SWM_S_COLOR_UNFOCUS)].pixel);
3531 if (ws->focus != win) {
3532 if (ws->focus && ws->focus != cfw)
3533 unfocus_win(ws->focus);
3537 /* If this window directs focus to a child window, then clear. */
3538 if (win->focus_child)
3539 win->focus_child = NULL;
3541 /* If transient, adjust parent's focus child for focus_magic. */
3543 parent = find_window(win->transient);
3544 if (parent && parent->focus_child != win)
3545 parent->focus_child = win;
3548 if (cfw != win && ws->r != NULL) {
3549 /* Set input focus if no input hint, or indicated by hint. */
3550 if (!(win->hints.flags & XCB_ICCCM_WM_HINT_INPUT) ||
3551 (win->hints.flags & XCB_ICCCM_WM_HINT_INPUT &&
3553 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT,
3554 win->id, last_event_time);
3556 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT,
3557 ws->r->id, XCB_CURRENT_TIME);
3559 /* Tell app it can adjust focus to a specific window. */
3560 if (win->take_focus) {
3561 /* java is special; always tell parent */
3562 if (TRANS(win) && win->java)
3563 client_msg(parent, a_takefocus,
3566 client_msg(win, a_takefocus, last_event_time);
3569 if (ws->cur_layout->flags & SWM_L_MAPONFOCUS ||
3571 /* If a parent exists, map it first. */
3573 raise_window(parent);
3576 /* Map siblings next. */
3577 TAILQ_FOREACH_SAFE(w, &ws->stack, stack_entry,
3579 if (w != win && !ICONIC(w) &&
3580 w->transient == parent->id) {
3586 /* Map focused window. */
3590 /* Stack any children of focus window. */
3591 TAILQ_FOREACH_SAFE(w, &ws->stack, stack_entry, tmpw)
3592 if (w->transient == win->id && !ICONIC(w)) {
3596 } else if (tile_gap < 0 && !ABOVE(win)) {
3598 * Windows overlap in the layout.
3599 * Raise focused win above all tiled wins.
3607 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
3608 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1,
3613 /* Update window border even if workspace is hidden. */
3614 update_window_color(win);
3619 DNPRINTF(SWM_D_FOCUS, "focus_win: done.\n");
3622 /* If a child window should have focus instead, return it. */
3624 get_focus_magic(struct ws_win *win)
3626 struct ws_win *parent = NULL;
3627 struct ws_win *child = NULL;
3629 DNPRINTF(SWM_D_FOCUS, "get_focus_magic: win %#x\n", WINID(win));
3634 parent = find_window(win->transient);
3636 /* If parent prefers focus elsewhere, then try to do so. */
3637 if (parent && (child = parent->focus_child)) {
3638 if (validate_win(child) == 0 && child->mapped)
3641 parent->focus_child = NULL;
3645 /* If this window prefers focus elsewhere, then try to do so. */
3646 if ((child = win->focus_child)) {
3647 if (validate_win(child) == 0 && child->mapped)
3650 win->focus_child = NULL;
3657 event_drain(uint8_t rt)
3659 xcb_generic_event_t *evt;
3661 /* ensure all pending requests have been processed before filtering. */
3663 while ((evt = xcb_poll_for_event(conn))) {
3664 if (XCB_EVENT_RESPONSE_TYPE(evt) != rt)
3672 set_region(struct swm_region *r)
3674 struct swm_region *rf;
3681 /* Unfocus old region bar. */
3686 xcb_change_window_attributes(conn, rf->bar->id,
3687 XCB_CW_BORDER_PIXEL,
3688 &r->s->c[SWM_S_COLOR_BAR_BORDER_UNFOCUS].pixel);
3691 if (rf != NULL && rf != r && (X(rf) != X(r) || Y(rf) != Y(r) ||
3692 WIDTH(rf) != WIDTH(r) || HEIGHT(rf) != HEIGHT(r))) {
3693 /* Set _NET_DESKTOP_GEOMETRY. */
3695 vals[1] = HEIGHT(r);
3696 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, r->s->root,
3697 ewmh[_NET_DESKTOP_GEOMETRY].atom, XCB_ATOM_CARDINAL, 32, 2,
3701 /* Set region bar border to focus_color. */
3702 xcb_change_window_attributes(conn, r->bar->id,
3703 XCB_CW_BORDER_PIXEL, &r->s->c[SWM_S_COLOR_BAR_BORDER].pixel);
3707 ewmh_update_current_desktop();
3711 focus_region(struct swm_region *r)
3714 struct swm_region *old_r;
3719 old_r = r->s->r_focus;
3722 nfw = get_region_focus(r);
3726 /* New region is empty; need to manually unfocus win. */
3728 unfocus_win(old_r->ws->focus);
3730 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT, r->id,
3733 /* Clear bar since empty. */
3739 switchws(struct swm_region *r, union arg *args)
3741 struct swm_region *this_r, *other_r;
3743 struct workspace *new_ws, *old_ws;
3744 xcb_window_t none = XCB_WINDOW_NONE;
3745 int wsid = args->id, unmap_old = 0;
3750 if (wsid >= workspace_limit)
3754 old_ws = this_r->ws;
3755 new_ws = &this_r->s->ws[wsid];
3757 DNPRINTF(SWM_D_WS, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
3758 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), old_ws->idx, wsid);
3760 if (new_ws == NULL || old_ws == NULL)
3762 if (new_ws == old_ws)
3765 if ((win = old_ws->focus) != NULL) {
3766 update_window_color(win);
3768 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
3769 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1,
3773 other_r = new_ws->r;
3774 if (other_r == NULL) {
3775 /* the other workspace is hidden, hide this one */
3779 /* the other ws is visible in another region, exchange them */
3780 other_r->ws_prior = new_ws;
3781 other_r->ws = old_ws;
3782 old_ws->r = other_r;
3784 this_r->ws_prior = old_ws;
3785 this_r->ws = new_ws;
3788 /* Set focus_pending before stacking, if needed. */
3789 if (focus_mode != SWM_FOCUS_FOLLOW && (!new_ws->focus_pending ||
3790 validate_win(new_ws->focus_pending))) {
3791 new_ws->focus_pending = get_region_focus(new_ws->r);
3792 new_ws->focus = new_ws->focus_prev;
3793 new_ws->focus_prev = NULL;
3796 new_ws->state = SWM_WS_STATE_MAPPING;
3799 /* unmap old windows */
3801 TAILQ_FOREACH(win, &old_ws->winlist, entry)
3803 old_ws->state = SWM_WS_STATE_HIDDEN;
3806 /* if workspaces were swapped, then don't wait to set focus */
3807 if (old_ws->r && focus_mode != SWM_FOCUS_FOLLOW) {
3808 if (new_ws->focus_pending) {
3809 focus_win(new_ws->focus_pending);
3810 new_ws->focus_pending = NULL;
3814 /* Clear bar and set focus on region input win if new ws is empty. */
3815 if (new_ws->focus_pending == NULL && new_ws->focus == NULL) {
3816 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT, r->id,
3821 ewmh_update_current_desktop();
3824 new_ws->state = SWM_WS_STATE_MAPPED;
3826 DNPRINTF(SWM_D_WS, "switchws: done.\n");
3830 cyclews(struct swm_region *r, union arg *args)
3833 struct swm_screen *s = r->s;
3837 DNPRINTF(SWM_D_WS, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3838 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
3844 case SWM_ARG_ID_CYCLEWS_MOVE_UP:
3847 case SWM_ARG_ID_CYCLEWS_UP_ALL:
3850 case SWM_ARG_ID_CYCLEWS_UP:
3851 a.id = (a.id < workspace_limit - 1) ? a.id + 1 : 0;
3853 case SWM_ARG_ID_CYCLEWS_MOVE_DOWN:
3856 case SWM_ARG_ID_CYCLEWS_DOWN_ALL:
3859 case SWM_ARG_ID_CYCLEWS_DOWN:
3860 a.id = (a.id > 0) ? a.id - 1 : workspace_limit - 1;
3867 (!cycle_empty && TAILQ_EMPTY(&s->ws[a.id].winlist)))
3869 if (!cycle_visible && s->ws[a.id].r != NULL)
3876 } while (a.id != r->ws->idx);
3878 DNPRINTF(SWM_D_FOCUS, "cyclews: done\n");
3882 priorws(struct swm_region *r, union arg *args)
3888 DNPRINTF(SWM_D_WS, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3889 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
3891 if (r->ws_prior == NULL)
3894 a.id = r->ws_prior->idx;
3896 DNPRINTF(SWM_D_FOCUS, "priorws: done\n");
3900 focusrg(struct swm_region *r, union arg *args)
3902 int ridx = args->id, i, num_screens;
3903 struct swm_region *rr = NULL;
3905 num_screens = get_screen_count();
3906 /* do nothing if we don't have more than one screen */
3907 if (!(num_screens > 1 || outputs > 1))
3910 DNPRINTF(SWM_D_FOCUS, "focusrg: id: %d\n", ridx);
3912 rr = TAILQ_FIRST(&r->s->rl);
3913 for (i = 0; i < ridx && rr != NULL; ++i)
3914 rr = TAILQ_NEXT(rr, entry);
3921 DNPRINTF(SWM_D_FOCUS, "focusrg: done\n");
3925 cyclerg(struct swm_region *r, union arg *args)
3927 struct swm_region *rr = NULL;
3930 num_screens = get_screen_count();
3931 /* do nothing if we don't have more than one screen */
3932 if (!(num_screens > 1 || outputs > 1))
3936 DNPRINTF(SWM_D_FOCUS, "cyclerg: id: %d, region: %d\n", args->id, i);
3939 case SWM_ARG_ID_CYCLERG_UP:
3940 rr = TAILQ_NEXT(r, entry);
3942 rr = TAILQ_FIRST(&screens[i].rl);
3944 case SWM_ARG_ID_CYCLERG_DOWN:
3945 rr = TAILQ_PREV(r, swm_region_list, entry);
3947 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
3957 DNPRINTF(SWM_D_FOCUS, "cyclerg: done\n");
3960 /* Sorts transients after parent. */
3962 sort_windows(struct ws_win_list *wl)
3964 struct ws_win *win, *parent, *nxt;
3969 for (win = TAILQ_FIRST(wl); win != TAILQ_END(wl); win = nxt) {
3970 nxt = TAILQ_NEXT(win, entry);
3972 parent = find_window(win->transient);
3973 if (parent == NULL) {
3974 warnx("not possible bug");
3977 TAILQ_REMOVE(wl, win, entry);
3978 TAILQ_INSERT_AFTER(wl, parent, win, entry);
3984 swapwin(struct swm_region *r, union arg *args)
3986 struct ws_win *target, *source;
3987 struct ws_win *cur_focus;
3988 struct ws_win_list *wl;
3990 DNPRINTF(SWM_D_WS, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3991 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
3993 cur_focus = r->ws->focus;
3994 if (cur_focus == NULL || FULLSCREEN(cur_focus))
3997 /* Adjust stacking in floating layer. */
3998 if (ABOVE(cur_focus)) {
4000 case SWM_ARG_ID_SWAPPREV:
4001 target = TAILQ_PREV(cur_focus, ws_win_stack,
4003 if (target != NULL && FLOATING(target)) {
4004 TAILQ_REMOVE(&cur_focus->ws->stack, cur_focus,
4006 TAILQ_INSERT_BEFORE(target, cur_focus,
4008 update_win_stacking(cur_focus);
4012 case SWM_ARG_ID_SWAPNEXT:
4013 target = TAILQ_NEXT(cur_focus, stack_entry);
4014 if (target != NULL && FLOATING(target)) {
4015 TAILQ_REMOVE(&cur_focus->ws->stack, cur_focus,
4017 TAILQ_INSERT_AFTER(&cur_focus->ws->stack,
4018 target, cur_focus, stack_entry);
4019 update_win_stacking(cur_focus);
4027 if (r->ws->cur_layout == &layouts[SWM_MAX_STACK])
4030 clear_maximized(r->ws);
4033 wl = &source->ws->winlist;
4036 case SWM_ARG_ID_SWAPPREV:
4038 source = find_window(source->transient);
4039 target = TAILQ_PREV(source, ws_win_list, entry);
4040 if (target && target->transient)
4041 target = find_window(target->transient);
4042 TAILQ_REMOVE(wl, source, entry);
4044 TAILQ_INSERT_TAIL(wl, source, entry);
4046 TAILQ_INSERT_BEFORE(target, source, entry);
4048 case SWM_ARG_ID_SWAPNEXT:
4049 target = TAILQ_NEXT(source, entry);
4050 /* move the parent and let the sort handle the move */
4052 source = find_window(source->transient);
4053 TAILQ_REMOVE(wl, source, entry);
4055 TAILQ_INSERT_HEAD(wl, source, entry);
4057 TAILQ_INSERT_AFTER(wl, target, source, entry);
4059 case SWM_ARG_ID_SWAPMAIN:
4060 target = TAILQ_FIRST(wl);
4061 if (target == source) {
4062 if (source->ws->focus_prev != NULL &&
4063 source->ws->focus_prev != target)
4064 source = source->ws->focus_prev;
4068 if (target == NULL || source == NULL)
4070 source->ws->focus_prev = target;
4071 TAILQ_REMOVE(wl, target, entry);
4072 TAILQ_INSERT_BEFORE(source, target, entry);
4073 TAILQ_REMOVE(wl, source, entry);
4074 TAILQ_INSERT_HEAD(wl, source, entry);
4076 case SWM_ARG_ID_MOVELAST:
4077 TAILQ_REMOVE(wl, source, entry);
4078 TAILQ_INSERT_TAIL(wl, source, entry);
4081 DNPRINTF(SWM_D_MOVE, "swapwin: invalid id: %d\n", args->id);
4086 ewmh_update_client_list();
4091 DNPRINTF(SWM_D_MOVE, "swapwin: done\n");
4095 get_focus_prev(struct ws_win *win)
4097 struct ws_win *winfocus = NULL;
4098 struct ws_win *cur_focus = NULL;
4099 struct ws_win_list *wl = NULL;
4100 struct workspace *ws = NULL;
4102 if (!(win && win->ws))
4107 cur_focus = ws->focus;
4109 DNPRINTF(SWM_D_FOCUS, "get_focus_prev: win %#x, cur_focus: %#x, "
4110 "focus_prev: %#x\n", WINID(win), WINID(cur_focus),
4111 WINID(ws->focus_prev));
4113 /* pickle, just focus on whatever */
4114 if (cur_focus == NULL) {
4115 /* use prev_focus if valid */
4116 if (ws->focus_prev && find_window(ws->focus_prev->id))
4117 winfocus = ws->focus_prev;
4121 /* if transient focus on parent */
4122 if (TRANS(cur_focus)) {
4123 winfocus = find_window(cur_focus->transient);
4127 /* if in max_stack try harder */
4128 if ((win->quirks & SWM_Q_FOCUSPREV) ||
4129 (ws->cur_layout->flags & SWM_L_FOCUSPREV)) {
4130 if (cur_focus != ws->focus_prev)
4131 winfocus = ws->focus_prev;
4133 winfocus = TAILQ_PREV(win, ws_win_list, entry);
4138 DNPRINTF(SWM_D_FOCUS, "get_focus_prev: focus_close: %d\n", focus_close);
4140 if (winfocus == NULL || winfocus == win) {
4141 switch (focus_close) {
4142 case SWM_STACK_BOTTOM:
4143 TAILQ_FOREACH(winfocus, wl, entry)
4144 if (!ICONIC(winfocus) && winfocus != cur_focus)
4148 TAILQ_FOREACH_REVERSE(winfocus, wl, ws_win_list, entry)
4149 if (!ICONIC(winfocus) && winfocus != cur_focus)
4152 case SWM_STACK_ABOVE:
4153 winfocus = TAILQ_NEXT(cur_focus, entry);
4154 while (winfocus && ICONIC(winfocus))
4155 winfocus = TAILQ_NEXT(winfocus, entry);
4157 if (winfocus == NULL) {
4158 if (focus_close_wrap) {
4159 TAILQ_FOREACH(winfocus, wl, entry)
4160 if (!ICONIC(winfocus) &&
4161 winfocus != cur_focus)
4164 TAILQ_FOREACH_REVERSE(winfocus, wl,
4166 if (!ICONIC(winfocus) &&
4167 winfocus != cur_focus)
4172 case SWM_STACK_BELOW:
4173 winfocus = TAILQ_PREV(cur_focus, ws_win_list, entry);
4174 while (winfocus && ICONIC(winfocus))
4175 winfocus = TAILQ_PREV(winfocus, ws_win_list,
4178 if (winfocus == NULL) {
4179 if (focus_close_wrap) {
4180 TAILQ_FOREACH_REVERSE(winfocus, wl,
4182 if (!ICONIC(winfocus) &&
4183 winfocus != cur_focus)
4186 TAILQ_FOREACH(winfocus, wl, entry)
4187 if (!ICONIC(winfocus) &&
4188 winfocus != cur_focus)
4196 if (winfocus == NULL ||
4197 (winfocus && (ICONIC(winfocus) || winfocus == cur_focus))) {
4198 if (focus_default == SWM_STACK_TOP) {
4199 TAILQ_FOREACH_REVERSE(winfocus, wl, ws_win_list, entry)
4200 if (!ICONIC(winfocus) && winfocus != cur_focus)
4203 TAILQ_FOREACH(winfocus, wl, entry)
4204 if (!ICONIC(winfocus) && winfocus != cur_focus)
4211 return get_focus_magic(winfocus);
4215 get_region_focus(struct swm_region *r)
4217 struct ws_win *winfocus = NULL;
4222 if (r->ws->focus && !ICONIC(r->ws->focus))
4223 winfocus = r->ws->focus;
4224 else if (r->ws->focus_prev && !ICONIC(r->ws->focus_prev))
4225 winfocus = r->ws->focus_prev;
4227 TAILQ_FOREACH(winfocus, &r->ws->winlist, entry)
4228 if (!ICONIC(winfocus))
4231 return get_focus_magic(winfocus);
4235 focus(struct swm_region *r, union arg *args)
4237 struct ws_win *head, *cur_focus = NULL, *winfocus = NULL;
4238 struct ws_win_list *wl = NULL;
4239 struct workspace *ws = NULL;
4242 xcb_icccm_wm_hints_t hints;
4247 cur_focus = r->ws->focus;
4251 DNPRINTF(SWM_D_FOCUS, "focus: id: %d, cur_focus: %#x\n", args->id,
4254 /* Make sure an uniconified window has focus, if one exists. */
4255 if (cur_focus == NULL) {
4256 cur_focus = TAILQ_FIRST(wl);
4257 while (cur_focus != NULL && ICONIC(cur_focus))
4258 cur_focus = TAILQ_NEXT(cur_focus, entry);
4260 DNPRINTF(SWM_D_FOCUS, "focus: new cur_focus: %#x\n",
4265 case SWM_ARG_ID_FOCUSPREV:
4266 if (cur_focus == NULL)
4269 winfocus = cur_focus;
4271 winfocus = TAILQ_PREV(winfocus, ws_win_list, entry);
4272 if (winfocus == NULL)
4273 winfocus = TAILQ_LAST(wl, ws_win_list);
4274 if (winfocus == cur_focus)
4276 } while (winfocus && (ICONIC(winfocus) ||
4277 winfocus->id == cur_focus->transient ||
4278 (cur_focus->transient != XCB_WINDOW_NONE &&
4279 winfocus->transient == cur_focus->transient)));
4281 case SWM_ARG_ID_FOCUSNEXT:
4282 if (cur_focus == NULL)
4285 winfocus = cur_focus;
4287 winfocus = TAILQ_NEXT(winfocus, entry);
4288 if (winfocus == NULL)
4289 winfocus = TAILQ_FIRST(wl);
4290 if (winfocus == cur_focus)
4292 } while (winfocus && (ICONIC(winfocus) ||
4293 winfocus->id == cur_focus->transient ||
4294 (cur_focus->transient != XCB_WINDOW_NONE &&
4295 winfocus->transient == cur_focus->transient)));
4297 case SWM_ARG_ID_FOCUSMAIN:
4298 if (cur_focus == NULL)
4301 winfocus = TAILQ_FIRST(wl);
4302 if (winfocus == cur_focus)
4303 winfocus = cur_focus->ws->focus_prev;
4305 case SWM_ARG_ID_FOCUSURGENT:
4306 /* Search forward for the next urgent window. */
4310 for (i = 0; i <= workspace_limit; ++i) {
4312 head = TAILQ_FIRST(&r->s->ws[(ws->idx + i) %
4313 workspace_limit].winlist);
4315 while (head != NULL &&
4316 (head = TAILQ_NEXT(head, entry)) != NULL) {
4317 if (head == cur_focus) {
4318 winfocus = cur_focus;
4321 if (xcb_icccm_get_wm_hints_reply(conn,
4322 xcb_icccm_get_wm_hints(conn, head->id),
4323 &hints, NULL) != 0 &&
4324 xcb_icccm_wm_hints_get_urgency(&hints)) {
4330 if (winfocus != NULL)
4334 /* Switch ws if new focus is on a different ws. */
4335 if (winfocus != NULL && winfocus->ws != ws) {
4336 a.id = winfocus->ws->idx;
4344 if (clear_maximized(ws) > 0)
4347 focus_win(get_focus_magic(winfocus));
4351 DNPRINTF(SWM_D_FOCUS, "focus: done\n");
4355 cycle_layout(struct swm_region *r, union arg *args)
4357 struct workspace *ws = r->ws;
4359 /* suppress unused warning since var is needed */
4362 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
4365 if (ws->cur_layout->l_stack == NULL)
4366 ws->cur_layout = &layouts[0];
4368 clear_maximized(ws);
4373 focus_win(get_region_focus(r));
4379 stack_config(struct swm_region *r, union arg *args)
4381 struct workspace *ws = r->ws;
4383 DNPRINTF(SWM_D_STACK, "stack_config: id: %d workspace: %d\n",
4386 if (clear_maximized(ws) > 0)
4389 if (ws->cur_layout->l_config != NULL)
4390 ws->cur_layout->l_config(ws, args->id);
4392 if (args->id != SWM_ARG_ID_STACKINIT)
4401 struct swm_geometry g;
4402 struct swm_region *r, *r_prev = NULL;
4409 DNPRINTF(SWM_D_STACK, "stack: begin\n");
4411 num_screens = get_screen_count();
4412 for (i = 0; i < num_screens; i++) {
4416 TAILQ_FOREACH(r, &screens[i].rl, entry) {
4417 /* Adjust stack area for region bar and padding. */
4419 g.x += region_padding;
4420 g.y += region_padding;
4421 g.w -= 2 * border_width + 2 * region_padding;
4422 g.h -= 2 * border_width + 2 * region_padding;
4423 if (bar_enabled && r->ws->bar_enabled) {
4429 DNPRINTF(SWM_D_STACK, "stack: workspace: %d (screen: "
4430 "%d, region: %d), (x,y) WxH: (%d,%d) %d x %d\n",
4431 r->ws->idx, i, j++, g.x, g.y, g.w, g.h);
4434 /* Stack bar/input relative to prev. region. */
4435 val[1] = XCB_STACK_MODE_ABOVE;
4437 val[0] = r_prev->id;
4438 DNPRINTF(SWM_D_STACK, "stack: region input %#x "
4439 "relative to %#x.\n", r->id, val[0]);
4440 xcb_configure_window(conn, r->id,
4441 XCB_CONFIG_WINDOW_SIBLING |
4442 XCB_CONFIG_WINDOW_STACK_MODE, val);
4444 val[0] = r_prev->bar->id;
4445 DNPRINTF(SWM_D_STACK, "stack: region bar %#x "
4446 "relative to %#x.\n", r->bar->id, val[0]);
4447 xcb_configure_window(conn, r->bar->id,
4448 XCB_CONFIG_WINDOW_SIBLING |
4449 XCB_CONFIG_WINDOW_STACK_MODE, val);
4452 r->ws->cur_layout->l_stack(r->ws, &g);
4453 r->ws->cur_layout->l_string(r->ws);
4454 /* save r so we can track region changes */
4462 DNPRINTF(SWM_D_STACK, "stack: end\n");
4466 store_float_geom(struct ws_win *win)
4468 if (win == NULL || win->ws->r == NULL)
4471 /* retain window geom and region geom */
4472 win->g_float = win->g;
4473 win->g_float.x -= X(win->ws->r);
4474 win->g_float.y -= Y(win->ws->r);
4475 win->g_floatvalid = 1;
4476 DNPRINTF(SWM_D_MISC, "store_float_geom: win %#x, g: (%d,%d)"
4477 " %d x %d, g_float: (%d,%d) %d x %d\n", win->id, X(win), Y(win),
4478 WIDTH(win), HEIGHT(win), win->g_float.x, win->g_float.y,
4479 win->g_float.w, win->g_float.h);
4483 load_float_geom(struct ws_win *win)
4485 if (win == NULL || win->ws->r == NULL)
4488 if (win->g_floatvalid) {
4489 win->g = win->g_float;
4490 X(win) += X(win->ws->r);
4491 Y(win) += Y(win->ws->r);
4492 DNPRINTF(SWM_D_MISC, "load_float_geom: win %#x, g: (%d,%d)"
4493 "%d x %d\n", win->id, X(win), Y(win), WIDTH(win),
4496 DNPRINTF(SWM_D_MISC, "load_float_geom: win %#x, g_float "
4497 "is not set.\n", win->id);
4502 update_floater(struct ws_win *win)
4504 struct workspace *ws;
4505 struct swm_region *r;
4512 if ((r = ws->r) == NULL)
4515 DNPRINTF(SWM_D_MISC, "update_floater: win %#x\n", win->id);
4519 if (FULLSCREEN(win)) {
4520 /* _NET_WM_FULLSCREEN: fullscreen without border. */
4521 if (!win->g_floatvalid)
4522 store_float_geom(win);
4526 } else if (MAXIMIZED(win)) {
4527 /* Maximize: like a single stacked window. */
4528 if (!win->g_floatvalid)
4529 store_float_geom(win);
4533 if (bar_enabled && ws->bar_enabled) {
4535 Y(win) += bar_height;
4536 HEIGHT(win) -= bar_height;
4537 } else if (disable_border) {
4541 if (win->bordered) {
4542 HEIGHT(win) -= 2 * border_width;
4543 WIDTH(win) -= 2 * border_width;
4546 /* Normal floating window. */
4547 /* Update geometry if new region. */
4549 load_float_geom(win);
4551 if ((win->quirks & SWM_Q_FULLSCREEN) &&
4552 WIDTH(win) >= WIDTH(r) && HEIGHT(win) >= HEIGHT(r)) {
4553 /* Remove border for FULLSCREEN quirk. */
4555 } else if (!MANUAL(win)) {
4556 if (TRANS(win) && (win->quirks & SWM_Q_TRANSSZ)) {
4557 /* Adjust size on TRANSSZ quirk. */
4558 WIDTH(win) = (double)WIDTH(r) * dialog_ratio;
4559 HEIGHT(win) = (double)HEIGHT(r) * dialog_ratio;
4562 if (!(win->quirks & SWM_Q_ANYWHERE)) {
4564 * Floaters and transients are auto-centred
4565 * unless manually moved, resized or ANYWHERE
4568 X(win) = X(r) + (WIDTH(r) - WIDTH(win)) / 2 -
4570 Y(win) = Y(r) + (HEIGHT(r) - HEIGHT(win)) / 2 -
4572 store_float_geom(win);
4577 /* Ensure at least 1 pixel of the window is in the region. */
4578 region_containment(win, r, SWM_CW_ALLSIDES);
4584 * Send keystrokes to terminal to decrease/increase the font size as the
4585 * window size changes.
4588 adjust_font(struct ws_win *win)
4590 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
4591 ABOVE(win) || TRANS(win))
4594 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
4595 WIDTH(win) / win->sh.width_inc < term_width &&
4596 win->font_steps < SWM_MAX_FONT_STEPS) {
4597 win->font_size_boundary[win->font_steps] =
4598 (win->sh.width_inc * term_width) + win->sh.base_width;
4601 win->last_inc = win->sh.width_inc;
4602 fake_keypress(win, XK_KP_Subtract, XCB_MOD_MASK_SHIFT);
4603 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
4604 WIDTH(win) > win->font_size_boundary[win->font_steps - 1]) {
4607 win->last_inc = win->sh.width_inc;
4608 fake_keypress(win, XK_KP_Add, XCB_MOD_MASK_SHIFT);
4612 #define SWAPXY(g) do { \
4614 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
4615 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
4618 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
4620 struct swm_geometry win_g, r_g = *g;
4622 int i, j, s, stacks;
4623 int w_inc = 1, h_inc, w_base = 1, h_base;
4624 int hrh, extra = 0, h_slice, last_h = 0;
4625 int split, colno, winno, mwin, msize, mscale;
4626 int remain, missing, v_slice, reconfigure = 0;
4629 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d, rot: %s, "
4630 "flip: %s\n", ws->idx, YESNO(rot), YESNO(flip));
4632 /* Prepare tiling variables, if needed. */
4633 if ((winno = count_win(ws, 0)) > 0) {
4634 /* Find first tiled window. */
4635 TAILQ_FOREACH(win, &ws->winlist, entry)
4636 if (!FLOATING(win) && !ICONIC(win))
4639 /* Take into account size hints of first tiled window. */
4641 w_inc = win->sh.width_inc;
4642 w_base = win->sh.base_width;
4643 mwin = ws->l_state.horizontal_mwin;
4644 mscale = ws->l_state.horizontal_msize;
4645 stacks = ws->l_state.horizontal_stacks;
4648 w_inc = win->sh.height_inc;
4649 w_base = win->sh.base_height;
4650 mwin = ws->l_state.vertical_mwin;
4651 mscale = ws->l_state.vertical_msize;
4652 stacks = ws->l_state.vertical_stacks;
4656 if (stacks > winno - mwin)
4657 stacks = winno - mwin;
4661 h_slice = r_g.h / SWM_H_SLICE;
4662 if (mwin && winno > mwin) {
4663 v_slice = r_g.w / SWM_V_SLICE;
4667 win_g.w = v_slice * mscale;
4669 if (w_inc > 1 && w_inc < v_slice) {
4670 /* Adjust for requested size increment. */
4671 remain = (win_g.w - w_base) % w_inc;
4677 win_g.x += r_g.w - msize;
4680 colno = split = winno / stacks;
4681 win_g.w = ((r_g.w - (stacks * 2 * border_width) +
4682 2 * border_width) / stacks);
4684 hrh = r_g.h / colno;
4685 extra = r_g.h - (colno * hrh);
4686 win_g.h = hrh - 2 * border_width;
4687 i = j = 0, s = stacks;
4690 /* Update window geometry. */
4691 TAILQ_FOREACH(win, &ws->winlist, entry) {
4695 if (FLOATING(win)) {
4696 update_floater(win);
4701 if (split && i == split) {
4702 colno = (winno - mwin) / stacks;
4703 if (s <= (winno - mwin) % stacks)
4706 hrh = r_g.h / colno;
4707 extra = r_g.h - (colno * hrh);
4710 win_g.x += win_g.w + 2 * border_width +
4713 win_g.w = (r_g.w - msize -
4714 (stacks * (2 * border_width + tile_gap))) / stacks;
4716 win_g.w += (r_g.w - msize -
4717 (stacks * (2 * border_width + tile_gap))) %
4721 win_g.x -= win_g.w + 2 * border_width +
4727 win_g.h = hrh - 2 * border_width - tile_gap;
4730 h_inc = win->sh.width_inc;
4731 h_base = win->sh.base_width;
4733 h_inc = win->sh.height_inc;
4734 h_base = win->sh.base_height;
4737 if (j == colno - 1) {
4738 win_g.h = hrh + extra;
4739 } else if (h_inc > 1 && h_inc < h_slice) {
4740 /* adjust for window's requested size increment */
4741 remain = (win_g.h - h_base) % h_inc;
4742 missing = h_inc - remain;
4744 if (missing <= extra || j == 0) {
4756 win_g.y += last_h + 2 * border_width + tile_gap;
4758 if (disable_border && !(bar_enabled && ws->bar_enabled) &&
4761 win_g.w += 2 * border_width;
4762 win_g.h += 2 * border_width;
4768 if (X(win) != win_g.y || Y(win) != win_g.x ||
4769 WIDTH(win) != win_g.h || HEIGHT(win) != win_g.w) {
4773 WIDTH(win) = win_g.h;
4774 HEIGHT(win) = win_g.w;
4777 if (X(win) != win_g.x || Y(win) != win_g.y ||
4778 WIDTH(win) != win_g.w || HEIGHT(win) != win_g.h) {
4782 WIDTH(win) = win_g.w;
4783 HEIGHT(win) = win_g.h;
4787 if (bordered != win->bordered) {
4789 win->bordered = bordered;
4802 /* Stack all windows from bottom up. */
4803 TAILQ_FOREACH_REVERSE(win, &ws->stack, ws_win_stack, stack_entry)
4805 update_win_stacking(win);
4807 /* Map all windows from top down. */
4808 TAILQ_FOREACH(win, &ws->stack, stack_entry)
4812 DNPRINTF(SWM_D_STACK, "stack_master: done\n");
4816 vertical_config(struct workspace *ws, int id)
4818 DNPRINTF(SWM_D_STACK, "vertical_config: id: %d, workspace: %d\n",
4822 case SWM_ARG_ID_STACKRESET:
4823 case SWM_ARG_ID_STACKINIT:
4824 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
4825 ws->l_state.vertical_mwin = 1;
4826 ws->l_state.vertical_stacks = 1;
4828 case SWM_ARG_ID_MASTERSHRINK:
4829 if (ws->l_state.vertical_msize > 1)
4830 ws->l_state.vertical_msize--;
4832 case SWM_ARG_ID_MASTERGROW:
4833 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
4834 ws->l_state.vertical_msize++;
4836 case SWM_ARG_ID_MASTERADD:
4837 ws->l_state.vertical_mwin++;
4839 case SWM_ARG_ID_MASTERDEL:
4840 if (ws->l_state.vertical_mwin > 0)
4841 ws->l_state.vertical_mwin--;
4843 case SWM_ARG_ID_STACKINC:
4844 ws->l_state.vertical_stacks++;
4846 case SWM_ARG_ID_STACKDEC:
4847 if (ws->l_state.vertical_stacks > 1)
4848 ws->l_state.vertical_stacks--;
4850 case SWM_ARG_ID_FLIPLAYOUT:
4851 ws->l_state.vertical_flip = !ws->l_state.vertical_flip;
4859 vertical_stack(struct workspace *ws, struct swm_geometry *g)
4861 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
4863 stack_master(ws, g, 0, ws->l_state.vertical_flip);
4867 horizontal_config(struct workspace *ws, int id)
4869 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
4872 case SWM_ARG_ID_STACKRESET:
4873 case SWM_ARG_ID_STACKINIT:
4874 ws->l_state.horizontal_mwin = 1;
4875 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
4876 ws->l_state.horizontal_stacks = 1;
4878 case SWM_ARG_ID_MASTERSHRINK:
4879 if (ws->l_state.horizontal_msize > 1)
4880 ws->l_state.horizontal_msize--;
4882 case SWM_ARG_ID_MASTERGROW:
4883 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
4884 ws->l_state.horizontal_msize++;
4886 case SWM_ARG_ID_MASTERADD:
4887 ws->l_state.horizontal_mwin++;
4889 case SWM_ARG_ID_MASTERDEL:
4890 if (ws->l_state.horizontal_mwin > 0)
4891 ws->l_state.horizontal_mwin--;
4893 case SWM_ARG_ID_STACKINC:
4894 ws->l_state.horizontal_stacks++;
4896 case SWM_ARG_ID_STACKDEC:
4897 if (ws->l_state.horizontal_stacks > 1)
4898 ws->l_state.horizontal_stacks--;
4900 case SWM_ARG_ID_FLIPLAYOUT:
4901 ws->l_state.horizontal_flip = !ws->l_state.horizontal_flip;
4909 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
4911 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
4913 stack_master(ws, g, 1, ws->l_state.horizontal_flip);
4916 /* fullscreen view */
4918 max_stack(struct workspace *ws, struct swm_geometry *g)
4920 struct swm_geometry gg = *g;
4921 struct ws_win *w, *win = NULL, *parent = NULL, *tmpw;
4924 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
4929 winno = count_win(ws, 0);
4930 if (winno == 0 && count_win(ws, 1) == 0)
4933 /* Figure out which top level window should be visible. */
4934 if (ws->focus_pending)
4935 win = ws->focus_pending;
4938 else if (ws->focus_prev)
4939 win = ws->focus_prev;
4941 win = TAILQ_FIRST(&ws->winlist);
4943 DNPRINTF(SWM_D_STACK, "max_stack: focus_pending: %#x, focus: %#x, "
4944 "focus_prev: %#x, first: %#x, win: %#x\n", WINID(ws->focus_pending),
4945 WINID(ws->focus), WINID(ws->focus_prev),
4946 WINID(TAILQ_FIRST(&ws->winlist)), win->id);
4948 /* Update window geometry. */
4949 TAILQ_FOREACH(w, &ws->winlist, entry) {
4958 /* Set maximized flag for all maxed windows. */
4959 if (!MAXIMIZED(w)) {
4960 /* Preserve floating geometry. */
4962 store_float_geom(w);
4964 ewmh_apply_flags(w, w->ewmh_flags | EWMH_F_MAXIMIZED);
4965 ewmh_update_wm_state(w);
4968 /* Only reconfigure if necessary. */
4969 if (X(w) != gg.x || Y(w) != gg.y || WIDTH(w) != gg.w ||
4970 HEIGHT(w) != gg.h) {
4972 if (bar_enabled && ws->bar_enabled){
4976 WIDTH(w) += 2 * border_width;
4977 HEIGHT(w) += 2 * border_width;
4984 /* If transient, stack parent and its children. */
4985 if (TRANS(win) && (parent = find_window(win->transient))) {
4986 raise_window(parent);
4988 TAILQ_FOREACH_SAFE(w, &ws->stack, stack_entry, tmpw)
4989 if (w->transient == parent->id)
4993 /* Make sure focus window is on top. */
4996 /* Stack any children of focus window. */
4997 TAILQ_FOREACH_SAFE(w, &ws->stack, stack_entry, tmpw)
4998 if (w->transient == win->id)
5001 /* Map all windows. */
5002 TAILQ_FOREACH(w, &ws->stack, stack_entry)
5008 send_to_rg(struct swm_region *r, union arg *args)
5010 int ridx = args->id, i, num_screens;
5011 struct swm_region *rr = NULL;
5014 num_screens = get_screen_count();
5015 /* do nothing if we don't have more than one screen */
5016 if (!(num_screens > 1 || outputs > 1))
5019 DNPRINTF(SWM_D_FOCUS, "send_to_rg: id: %d\n", ridx);
5021 rr = TAILQ_FIRST(&r->s->rl);
5022 for (i = 0; i < ridx && rr != NULL; ++i)
5023 rr = TAILQ_NEXT(rr, entry);
5034 region_under(struct swm_screen *s, int x, int y)
5036 struct swm_region *r = NULL;
5038 TAILQ_FOREACH(r, &s->rl, entry) {
5039 DNPRINTF(SWM_D_MISC, "region_under: ws: %d, region g: (%d,%d) "
5040 "%d x %d, coords: (%d,%d)\n", r->ws->idx, X(r), Y(r),
5041 WIDTH(r), HEIGHT(r), x, y);
5042 if (X(r) <= x && x < MAX_X(r))
5043 if (Y(r) <= y && y < MAX_Y(r))
5050 /* Transfer focused window to target workspace and focus. */
5052 send_to_ws(struct swm_region *r, union arg *args)
5054 int wsid = args->id;
5055 struct ws_win *win = NULL;
5057 if (r && r->ws && r->ws->focus)
5062 DNPRINTF(SWM_D_MOVE, "send_to_ws: win %#x, ws %d\n", win->id, wsid);
5064 if (wsid < 0 || wsid >= workspace_limit)
5067 if (win->ws->idx == wsid)
5070 win_to_ws(win, wsid, 1);
5072 /* Set new focus on target ws. */
5073 if (focus_mode != SWM_FOCUS_FOLLOW) {
5074 win->ws->focus_prev = win->ws->focus;
5075 win->ws->focus = win;
5076 win->ws->focus_pending = NULL;
5078 if (win->ws->focus_prev)
5079 update_window_color(win->ws->focus_prev);
5082 DNPRINTF(SWM_D_STACK, "send_to_ws: focus_pending: %#x, focus: %#x, "
5083 "focus_prev: %#x, first: %#x, win: %#x\n",
5084 WINID(r->ws->focus_pending), WINID(r->ws->focus),
5085 WINID(r->ws->focus_prev), WINID(TAILQ_FIRST(&r->ws->winlist)),
5088 ewmh_apply_flags(win, win->ewmh_flags & ~EWMH_F_MAXIMIZED);
5089 ewmh_update_wm_state(win);
5091 /* Restack and set new focus on current ws. */
5093 load_float_geom(win);
5097 if (focus_mode != SWM_FOCUS_FOLLOW) {
5098 if (r->ws->focus != NULL) {
5099 focus_win(r->ws->focus);
5101 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT, r->id,
5111 win_to_ws(struct ws_win *win, int wsid, int unfocus)
5113 struct ws_win *parent;
5114 struct workspace *ws, *nws, *pws;
5116 if (wsid < 0 || wsid >= workspace_limit)
5119 if (win->ws->idx == wsid)
5123 nws = &win->s->ws[wsid];
5125 DNPRINTF(SWM_D_MOVE, "win_to_ws: win %#x, ws %d -> %d\n", win->id,
5128 /* Cleanup focus on source ws. */
5129 if (focus_mode != SWM_FOCUS_FOLLOW &&
5130 (ws->focus == win || ws->focus_pending == win))
5131 ws->focus_pending = get_focus_prev(win);
5133 /* Move the parent if this is a transient window. */
5135 parent = find_window(win->transient);
5138 /* Set new focus in parent's ws if needed. */
5139 if (pws->focus == parent) {
5140 if (focus_mode != SWM_FOCUS_FOLLOW)
5141 pws->focus_pending =
5142 get_focus_prev(parent);
5144 unfocus_win(parent);
5146 if (focus_mode != SWM_FOCUS_FOLLOW) {
5147 pws->focus = pws->focus_pending;
5148 pws->focus_pending = NULL;
5152 /* Don't unmap parent if new ws is visible */
5154 unmap_window(parent);
5157 TAILQ_REMOVE(&ws->winlist, parent, entry);
5158 TAILQ_REMOVE(&ws->stack, parent, stack_entry);
5159 TAILQ_INSERT_TAIL(&nws->winlist, parent, entry);
5160 TAILQ_INSERT_TAIL(&nws->stack, parent, stack_entry);
5163 DNPRINTF(SWM_D_PROP, "win_to_ws: set property: "
5164 "_NET_WM_DESKTOP: %d\n", wsid);
5165 xcb_change_property(conn, XCB_PROP_MODE_REPLACE,
5166 parent->id, ewmh[_NET_WM_DESKTOP].atom,
5167 XCB_ATOM_CARDINAL, 32, 1, &wsid);
5174 if (ws->focus_prev == win)
5175 ws->focus_prev = NULL;
5177 if (focus_mode != SWM_FOCUS_FOLLOW && ws->focus_pending != NULL) {
5178 ws->focus = ws->focus_pending;
5179 ws->focus_pending = NULL;
5182 /* Don't unmap if new ws is visible */
5187 TAILQ_REMOVE(&ws->winlist, win, entry);
5188 TAILQ_REMOVE(&ws->stack, win, stack_entry);
5189 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
5190 TAILQ_INSERT_TAIL(&nws->stack, win, stack_entry);
5193 /* Update the window's workspace property: _NET_WM_DESKTOP */
5194 DNPRINTF(SWM_D_PROP, "win_to_ws: set property: "
5195 "_NET_WM_DESKTOP: %d\n", wsid);
5196 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
5197 ewmh[_NET_WM_DESKTOP].atom, XCB_ATOM_CARDINAL, 32, 1, &wsid);
5199 ewmh_update_client_list();
5201 DNPRINTF(SWM_D_MOVE, "win_to_ws: done.\n");
5205 pressbutton(struct swm_region *r, union arg *args)
5207 /* suppress unused warning since var is needed */
5210 xcb_test_fake_input(conn, XCB_BUTTON_PRESS, args->id,
5211 XCB_CURRENT_TIME, XCB_WINDOW_NONE, 0, 0, 0);
5212 xcb_test_fake_input(conn, XCB_BUTTON_RELEASE, args->id,
5213 XCB_CURRENT_TIME, XCB_WINDOW_NONE, 0, 0, 0);
5217 raise_toggle(struct swm_region *r, union arg *args)
5219 /* suppress unused warning since var is needed */
5222 if (r == NULL || r->ws == NULL)
5225 if (r->ws->focus && MAXIMIZED(r->ws->focus))
5228 r->ws->always_raise = !r->ws->always_raise;
5230 /* bring floaters back to top */
5231 if (!r->ws->always_raise)
5238 iconify(struct swm_region *r, union arg *args)
5241 /* suppress unused warning since var is needed */
5244 if ((w = r->ws->focus) == NULL)
5247 ewmh_apply_flags(w, w->ewmh_flags | EWMH_F_HIDDEN);
5248 ewmh_update_wm_state(w);
5256 get_win_name(xcb_window_t win)
5259 xcb_get_property_cookie_t c;
5260 xcb_get_property_reply_t *r;
5262 /* First try _NET_WM_NAME for UTF-8. */
5263 c = xcb_get_property(conn, 0, win, ewmh[_NET_WM_NAME].atom,
5264 XCB_GET_PROPERTY_TYPE_ANY, 0, UINT_MAX);
5265 r = xcb_get_property_reply(conn, c, NULL);
5266 if (r && r->type == XCB_NONE) {
5268 /* Use WM_NAME instead; no UTF-8. */
5269 c = xcb_get_property(conn, 0, win, XCB_ATOM_WM_NAME,
5270 XCB_GET_PROPERTY_TYPE_ANY, 0, UINT_MAX);
5271 r = xcb_get_property_reply(conn, c, NULL);
5274 if (r && r->type != XCB_NONE && r->length > 0)
5275 name = strndup(xcb_get_property_value(r),
5276 xcb_get_property_value_length(r));
5281 err(1, "get_win_name: failed to allocate memory.");
5289 uniconify(struct swm_region *r, union arg *args)
5296 DNPRINTF(SWM_D_MISC, "uniconify\n");
5298 if (r == NULL || r->ws == NULL)
5301 /* make sure we have anything to uniconify */
5302 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
5303 if (win->ws == NULL)
5304 continue; /* should never happen */
5310 DNPRINTF(SWM_D_MISC, "uniconify: count: %d\n", count);
5316 search_resp_action = SWM_SEARCH_UNICONIFY;
5318 spawn_select(r, args, "search", &searchpid);
5320 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
5323 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
5324 if (win->ws == NULL)
5325 continue; /* should never happen */
5329 name = get_win_name(win->id);
5330 fprintf(lfile, "%s.%u\n", name, win->id);
5338 name_workspace(struct swm_region *r, union arg *args)
5342 DNPRINTF(SWM_D_MISC, "name_workspace\n");
5348 search_resp_action = SWM_SEARCH_NAME_WORKSPACE;
5350 spawn_select(r, args, "name_workspace", &searchpid);
5352 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
5355 fprintf(lfile, "%s", "");
5360 search_workspace(struct swm_region *r, union arg *args)
5363 struct workspace *ws;
5366 DNPRINTF(SWM_D_MISC, "search_workspace\n");
5372 search_resp_action = SWM_SEARCH_SEARCH_WORKSPACE;
5374 spawn_select(r, args, "search", &searchpid);
5376 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
5379 for (i = 0; i < workspace_limit; i++) {
5383 fprintf(lfile, "%d%s%s\n", ws->idx + 1,
5384 (ws->name ? ":" : ""), (ws->name ? ws->name : ""));
5391 search_win_cleanup(void)
5393 struct search_window *sw = NULL;
5395 while ((sw = TAILQ_FIRST(&search_wl)) != NULL) {
5396 xcb_destroy_window(conn, sw->indicator);
5397 TAILQ_REMOVE(&search_wl, sw, entry);
5403 search_win(struct swm_region *r, union arg *args)
5405 struct ws_win *win = NULL;
5406 struct search_window *sw = NULL;
5409 xcb_screen_t *screen;
5410 int i, width, height;
5418 XRectangle l_ibox, l_lbox;
5420 DNPRINTF(SWM_D_MISC, "search_win\n");
5423 search_resp_action = SWM_SEARCH_SEARCH_WINDOW;
5425 spawn_select(r, args, "search", &searchpid);
5427 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
5430 if ((screen = get_screen(r->s->idx)) == NULL)
5431 errx(1, "ERROR: can't get screen %d.", r->s->idx);
5433 TAILQ_INIT(&search_wl);
5436 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
5440 sw = calloc(1, sizeof(struct search_window));
5442 warn("search_win: calloc");
5444 search_win_cleanup();
5450 snprintf(s, sizeof s, "%d", i);
5453 w = xcb_generate_id(conn);
5454 wa[0] = r->s->c[SWM_S_COLOR_FOCUS].pixel;
5455 wa[1] = r->s->c[SWM_S_COLOR_UNFOCUS].pixel;
5456 wa[2] = screen->default_colormap;
5458 if (bar_font_legacy) {
5459 XmbTextExtents(bar_fs, s, len, &l_ibox, &l_lbox);
5460 width = l_lbox.width + 4;
5461 height = bar_fs_extents->max_logical_extent.height + 4;
5463 XftTextExtentsUtf8(display, bar_font, (FcChar8 *)s, len,
5465 width = info.width + 4;
5466 height = bar_font->height + 4;
5469 xcb_create_window(conn, screen->root_depth, w, win->id, 0, 0,
5470 width, height, 1, XCB_WINDOW_CLASS_INPUT_OUTPUT,
5471 screen->root_visual, XCB_CW_BACK_PIXEL |
5472 XCB_CW_BORDER_PIXEL | XCB_CW_COLORMAP, wa);
5474 xcb_map_window(conn, w);
5477 TAILQ_INSERT_TAIL(&search_wl, sw, entry);
5479 if (bar_font_legacy) {
5480 l_gcv.graphics_exposures = 0;
5481 l_draw = XCreateGC(display, w, 0, &l_gcv);
5483 XSetForeground(display, l_draw,
5484 r->s->c[SWM_S_COLOR_BAR].pixel);
5486 DRAWSTRING(display, w, bar_fs, l_draw, 2,
5487 (bar_fs_extents->max_logical_extent.height -
5488 l_lbox.height) / 2 - l_lbox.y, s, len);
5490 XFreeGC(display, l_draw);
5493 draw = XftDrawCreate(display, w,
5494 DefaultVisual(display, r->s->idx),
5495 DefaultColormap(display, r->s->idx));
5497 XftDrawStringUtf8(draw, &search_font_color, bar_font, 2,
5498 (HEIGHT(r->bar) + bar_font->height) / 2 -
5499 bar_font->descent, (FcChar8 *)s, len);
5501 XftDrawDestroy(draw);
5504 DNPRINTF(SWM_D_MISC, "search_win: mapped win %#x\n", w);
5506 fprintf(lfile, "%d\n", i);
5516 search_resp_uniconify(const char *resp, size_t len)
5522 DNPRINTF(SWM_D_MISC, "search_resp_uniconify: resp: %s\n", resp);
5524 TAILQ_FOREACH(win, &search_r->ws->winlist, entry) {
5527 name = get_win_name(win->id);
5528 if (asprintf(&s, "%s.%u", name, win->id) == -1) {
5533 if (strncmp(s, resp, len) == 0) {
5534 /* XXX this should be a callback to generalize */
5535 ewmh_apply_flags(win, win->ewmh_flags & ~EWMH_F_HIDDEN);
5536 ewmh_update_wm_state(win);
5546 search_resp_name_workspace(const char *resp, size_t len)
5548 struct workspace *ws;
5550 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: resp: %s\n", resp);
5552 if (search_r->ws == NULL)
5557 free(search_r->ws->name);
5558 search_r->ws->name = NULL;
5562 ws->name = strdup(resp);
5563 if (ws->name == NULL) {
5564 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: "
5565 "strdup: %s", strerror(errno));
5569 ewmh_update_desktop_names();
5570 ewmh_get_desktop_names();
5575 ewmh_update_desktop_names(void)
5577 char *name_list = NULL, *p;
5578 int num_screens, i, j, len = 0, tot = 0;
5580 num_screens = get_screen_count();
5581 for (i = 0; i < num_screens; ++i) {
5582 for (j = 0; j < workspace_limit; ++j) {
5583 if (screens[i].ws[j].name != NULL)
5584 len += strlen(screens[i].ws[j].name);
5588 if((name_list = calloc(len, sizeof(char))) == NULL)
5589 err(1, "update_desktop_names: calloc: failed to "
5590 "allocate memory.");
5593 for (j = 0; j < workspace_limit; ++j) {
5594 if (screens[i].ws[j].name != NULL) {
5595 len = strlen(screens[i].ws[j].name);
5596 memcpy(p, screens[i].ws[j].name, len);
5605 xcb_change_property(conn, XCB_PROP_MODE_REPLACE,
5606 screens[i].root, ewmh[_NET_DESKTOP_NAMES].atom,
5607 a_utf8_string, 8, tot, name_list);
5617 ewmh_get_desktop_names(void)
5620 xcb_get_property_cookie_t c;
5621 xcb_get_property_reply_t *r;
5622 int num_screens, i, j, n, k;
5624 num_screens = get_screen_count();
5625 for (i = 0; i < num_screens; ++i) {
5626 for (j = 0; j < workspace_limit; ++j) {
5627 free(screens[i].ws[j].name);
5628 screens[i].ws[j].name = NULL;
5631 c = xcb_get_property(conn, 0, screens[i].root,
5632 ewmh[_NET_DESKTOP_NAMES].atom,
5633 a_utf8_string, 0, UINT32_MAX);
5634 r = xcb_get_property_reply(conn, c, NULL);
5638 names = xcb_get_property_value(r);
5639 n = xcb_get_property_value_length(r);
5641 for (j = 0, k = 0; j < n; ++j) {
5642 if (*(names + j) != '\0') {
5643 screens[i].ws[k].name = strdup(names + j);
5644 j += strlen(names + j);
5653 ewmh_update_client_list(void)
5656 int num_screens, i, j, k = 0, count = 0;
5659 num_screens = get_screen_count();
5660 for (i = 0; i < num_screens; ++i) {
5661 for (j = 0; j < workspace_limit; ++j)
5662 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
5665 DNPRINTF(SWM_D_PROP, "ewmh_update_client_list: win count: %d\n",
5671 wins = calloc(count, sizeof(xcb_window_t));
5673 err(1, "ewmh_update_client_list: calloc: failed to "
5674 "allocate memory.");
5676 for (j = 0, k = 0; j < workspace_limit; ++j)
5677 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
5678 wins[k++] = win->id;
5680 xcb_change_property(conn, XCB_PROP_MODE_REPLACE,
5681 screens[i].root, ewmh[_NET_CLIENT_LIST].atom,
5682 XCB_ATOM_WINDOW, 32, count, wins);
5689 ewmh_update_current_desktop(void)
5693 num_screens = get_screen_count();
5694 for (i = 0; i < num_screens; ++i)
5695 xcb_change_property(conn, XCB_PROP_MODE_REPLACE,
5696 screens[i].root, ewmh[_NET_CURRENT_DESKTOP].atom,
5697 XCB_ATOM_CARDINAL, 32, 1, &screens[i].r_focus->ws->idx);
5701 ewmh_update_desktops(void)
5703 int num_screens, i, j;
5706 vals = calloc(workspace_limit * 2, sizeof(uint32_t));
5708 err(1, "ewmh_update_desktops: calloc: failed to allocate "
5711 num_screens = get_screen_count();
5712 for (i = 0; i < num_screens; i++) {
5713 xcb_change_property(conn, XCB_PROP_MODE_REPLACE,
5714 screens[i].root, ewmh[_NET_NUMBER_OF_DESKTOPS].atom,
5715 XCB_ATOM_CARDINAL, 32, 1, &workspace_limit);
5717 for (j = 0; j < workspace_limit; ++j) {
5718 if (screens[i].ws[j].r != NULL) {
5719 vals[j * 2] = X(screens[i].ws[j].r);
5720 vals[j * 2 + 1] = Y(screens[i].ws[j].r);
5721 } else if (screens[i].ws[j].old_r != NULL) {
5722 vals[j * 2] = X(screens[i].ws[j].old_r);
5723 vals[j * 2 + 1] = Y(screens[i].ws[j].old_r);
5725 vals[j * 2] = vals[j * 2 + 1] = 0;
5729 xcb_change_property(conn, XCB_PROP_MODE_REPLACE,
5730 screens[i].root, ewmh[_NET_DESKTOP_VIEWPORT].atom,
5731 XCB_ATOM_CARDINAL, 32, workspace_limit * 2, vals);
5738 search_resp_search_workspace(const char *resp)
5745 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: resp: %s\n", resp);
5749 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: strdup: %s",
5756 ws_idx = (int)strtonum(q, 1, workspace_limit, &errstr);
5758 DNPRINTF(SWM_D_MISC, "workspace idx is %s: %s",
5765 switchws(search_r, &a);
5769 search_resp_search_window(const char *resp)
5774 struct search_window *sw;
5776 DNPRINTF(SWM_D_MISC, "search_resp_search_window: resp: %s\n", resp);
5780 DNPRINTF(SWM_D_MISC, "search_resp_search_window: strdup: %s",
5785 idx = (int)strtonum(s, 1, INT_MAX, &errstr);
5787 DNPRINTF(SWM_D_MISC, "window idx is %s: %s",
5794 TAILQ_FOREACH(sw, &search_wl, entry)
5795 if (idx == sw->idx) {
5801 #define MAX_RESP_LEN 1024
5804 search_do_resp(void)
5810 DNPRINTF(SWM_D_MISC, "search_do_resp:\n");
5815 if ((resp = calloc(1, MAX_RESP_LEN + 1)) == NULL) {
5816 warn("search: calloc");
5820 rbytes = read(select_resp_pipe[0], resp, MAX_RESP_LEN);
5822 warn("search: read error");
5825 resp[rbytes] = '\0';
5828 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
5829 * newline, so work around that by sanitizing the resp now.
5831 resp[strcspn(resp, "\n")] = '\0';
5834 switch (search_resp_action) {
5835 case SWM_SEARCH_UNICONIFY:
5836 search_resp_uniconify(resp, len);
5838 case SWM_SEARCH_NAME_WORKSPACE:
5839 search_resp_name_workspace(resp, len);
5841 case SWM_SEARCH_SEARCH_WORKSPACE:
5842 search_resp_search_workspace(resp);
5844 case SWM_SEARCH_SEARCH_WINDOW:
5845 search_resp_search_window(resp);
5850 if (search_resp_action == SWM_SEARCH_SEARCH_WINDOW)
5851 search_win_cleanup();
5853 search_resp_action = SWM_SEARCH_NONE;
5854 close(select_resp_pipe[0]);
5861 wkill(struct swm_region *r, union arg *args)
5863 DNPRINTF(SWM_D_MISC, "wkill: win %#x, id: %d\n", WINID(r->ws->focus),
5866 if (r->ws->focus == NULL)
5869 if (args->id == SWM_ARG_ID_KILLWINDOW)
5870 xcb_kill_client(conn, r->ws->focus->id);
5872 if (r->ws->focus->can_delete)
5873 client_msg(r->ws->focus, a_delete, 0);
5879 clear_maximized(struct workspace *ws)
5884 /* Clear any maximized win(s) on ws, from bottom up. */
5885 TAILQ_FOREACH_REVERSE(w, &ws->stack, ws_win_stack, stack_entry)
5887 ewmh_apply_flags(w, w->ewmh_flags & ~EWMH_F_MAXIMIZED);
5888 ewmh_update_wm_state(w);
5896 maximize_toggle(struct swm_region *r, union arg *args)
5898 struct ws_win *w = r->ws->focus;
5900 /* suppress unused warning since var is needed */
5906 DNPRINTF(SWM_D_MISC, "maximize_toggle: win %#x\n", w->id);
5911 if (w->ws->cur_layout == &layouts[SWM_MAX_STACK])
5914 ewmh_apply_flags(w, w->ewmh_flags ^ EWMH_F_MAXIMIZED);
5915 ewmh_update_wm_state(w);
5919 if (w == w->ws->focus)
5923 DNPRINTF(SWM_D_MISC, "maximize_toggle: done\n");
5927 floating_toggle(struct swm_region *r, union arg *args)
5929 struct ws_win *w = r->ws->focus;
5931 /* suppress unused warning since var is needed */
5937 DNPRINTF(SWM_D_MISC, "floating_toggle: win %#x\n", w->id);
5939 if (FULLSCREEN(w) || TRANS(w))
5942 if (w->ws->cur_layout == &layouts[SWM_MAX_STACK])
5945 ewmh_apply_flags(w, w->ewmh_flags ^ EWMH_F_ABOVE);
5946 ewmh_update_wm_state(w);
5950 if (w == w->ws->focus)
5954 DNPRINTF(SWM_D_MISC, "floating_toggle: done\n");
5958 region_containment(struct ws_win *win, struct swm_region *r, int opts)
5960 struct swm_geometry g = r->g;
5961 int rt, lt, tp, bm, bw;
5963 bw = (opts & SWM_CW_SOFTBOUNDARY) ? boundary_width : 0;
5966 * Perpendicular distance of each side of the window to the respective
5967 * side of the region boundary. Positive values indicate the side of
5968 * the window has passed beyond the region boundary.
5970 rt = opts & SWM_CW_RIGHT ? MAX_X(win) + BORDER(win) - MAX_X(r) : bw;
5971 lt = opts & SWM_CW_LEFT ? X(r) - X(win) + BORDER(win) : bw;
5972 bm = opts & SWM_CW_BOTTOM ? MAX_Y(win) + BORDER(win) - MAX_Y(r) : bw;
5973 tp = opts & SWM_CW_TOP ? Y(r) - Y(win) + BORDER(win) : bw;
5975 DNPRINTF(SWM_D_MISC, "region_containment: win %#x, rt: %d, lt: %d, "
5976 "bm: %d, tp: %d, SOFTBOUNDARY: %s, HARDBOUNDARY: %s\n", win->id, rt,
5977 lt, bm, tp, YESNO(opts & SWM_CW_SOFTBOUNDARY),
5978 YESNO(opts & SWM_CW_HARDBOUNDARY));
5981 * Disable containment if any of the flagged sides went beyond the
5982 * containment boundary, or if containment is disabled.
5984 if (!(opts & SWM_CW_HARDBOUNDARY || opts & SWM_CW_SOFTBOUNDARY) ||
5985 (bw != 0 && ((rt > bw) || (lt > bw) || (bm > bw) || (tp > bw)))) {
5986 /* Make sure window has at least 1 pixel in the region */
5987 g.x += 1 - WIDTH(win);
5988 g.y += 1 - HEIGHT(win);
5989 g.w += 2 * WIDTH(win) - 2;
5990 g.h += 2 * HEIGHT(win) - 2;
5993 constrain_window(win, &g, &opts);
5996 /* Move or resize a window so that flagged side(s) fit into the supplied box. */
5998 constrain_window(struct ws_win *win, struct swm_geometry *b, int *opts)
6000 DNPRINTF(SWM_D_MISC, "constrain_window: win %#x, (x,y) w x h: "
6001 "(%d,%d) %d x %d, box: (x,y) w x h: (%d,%d) %d x %d, rt: %s, "
6002 "lt: %s, bt: %s, tp: %s, allow resize: %s\n", win->id, X(win),
6003 Y(win), WIDTH(win), HEIGHT(win), b->x, b->y, b->w, b->h,
6004 YESNO(*opts & SWM_CW_RIGHT), YESNO(*opts & SWM_CW_LEFT),
6005 YESNO(*opts & SWM_CW_BOTTOM), YESNO(*opts & SWM_CW_TOP),
6006 YESNO(*opts & SWM_CW_RESIZABLE));
6008 if ((*opts & SWM_CW_RIGHT) && MAX_X(win) + BORDER(win) > b->x + b->w) {
6009 if (*opts & SWM_CW_RESIZABLE)
6010 WIDTH(win) = b->x + b->w - X(win) - BORDER(win);
6012 X(win) = b->x + b->w - WIDTH(win) - BORDER(win);
6015 if ((*opts & SWM_CW_LEFT) && X(win) + BORDER(win) < b->x) {
6016 if (*opts & SWM_CW_RESIZABLE)
6017 WIDTH(win) -= b->x - X(win) - BORDER(win);
6019 X(win) = b->x - BORDER(win);
6022 if ((*opts & SWM_CW_BOTTOM) && MAX_Y(win) + BORDER(win) > b->y + b->h) {
6023 if (*opts & SWM_CW_RESIZABLE)
6024 HEIGHT(win) = b->y + b->h - Y(win) - BORDER(win);
6026 Y(win) = b->y + b->h - HEIGHT(win) - BORDER(win);
6029 if ((*opts & SWM_CW_TOP) && Y(win) + BORDER(win) < b->y) {
6030 if (*opts & SWM_CW_RESIZABLE)
6031 HEIGHT(win) -= b->y - Y(win) - BORDER(win);
6033 Y(win) = b->y - BORDER(win);
6036 if (*opts & SWM_CW_RESIZABLE) {
6039 if (HEIGHT(win) < 1)
6045 update_window_color(struct ws_win *win)
6049 if (WS_FOCUSED(win->ws) && win->ws->focus == win)
6050 pixel = MAXIMIZED(win) ?
6051 &win->s->c[SWM_S_COLOR_FOCUS_MAXIMIZED].pixel :
6052 &win->s->c[SWM_S_COLOR_FOCUS].pixel;
6054 pixel = MAXIMIZED(win) ?
6055 &win->s->c[SWM_S_COLOR_UNFOCUS_MAXIMIZED].pixel :
6056 &win->s->c[SWM_S_COLOR_UNFOCUS].pixel;
6058 xcb_change_window_attributes(conn, win->id,
6059 XCB_CW_BORDER_PIXEL, pixel);
6063 update_window(struct ws_win *win)
6068 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
6069 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
6070 XCB_CONFIG_WINDOW_BORDER_WIDTH;
6074 wc[3] = HEIGHT(win);
6075 wc[4] = BORDER(win);
6077 DNPRINTF(SWM_D_EVENT, "update_window: win %#x, (x,y) w x h: "
6078 "(%d,%d) %d x %d, bordered: %s\n", win->id, wc[0], wc[1], wc[2],
6079 wc[3], YESNO(win->bordered));
6081 xcb_configure_window(conn, win->id, mask, wc);
6084 #define SWM_RESIZE_STEPS (50)
6087 resize(struct ws_win *win, union arg *args)
6089 xcb_timestamp_t timestamp = 0;
6090 struct swm_region *r = NULL;
6091 struct swm_geometry g;
6093 int top = 0, left = 0, resizing;
6095 xcb_cursor_t cursor;
6096 xcb_query_pointer_reply_t *xpr = NULL;
6097 xcb_generic_event_t *evt;
6098 xcb_motion_notify_event_t *mne;
6104 if (FULLSCREEN(win))
6107 /* In max_stack mode, should only resize transients. */
6108 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !TRANS(win))
6111 DNPRINTF(SWM_D_EVENT, "resize: win %#x, floating: %s, "
6112 "transient: %#x\n", win->id, YESNO(ABOVE(win)),
6116 store_float_geom(win);
6117 else if (!(TRANS(win) || ABOVE(win)))
6120 ewmh_apply_flags(win, (win->ewmh_flags | SWM_F_MANUAL | EWMH_F_ABOVE) &
6122 ewmh_update_wm_state(win);
6128 /* It's possible for win to have been freed during focus_flush(). */
6129 if (validate_win(win)) {
6130 DNPRINTF(SWM_D_EVENT, "resize: invalid win.\n");
6135 case SWM_ARG_ID_WIDTHSHRINK:
6136 WIDTH(win) -= SWM_RESIZE_STEPS;
6139 case SWM_ARG_ID_WIDTHGROW:
6140 WIDTH(win) += SWM_RESIZE_STEPS;
6143 case SWM_ARG_ID_HEIGHTSHRINK:
6144 HEIGHT(win) -= SWM_RESIZE_STEPS;
6147 case SWM_ARG_ID_HEIGHTGROW:
6148 HEIGHT(win) += SWM_RESIZE_STEPS;
6155 region_containment(win, r, SWM_CW_ALLSIDES | SWM_CW_RESIZABLE |
6156 SWM_CW_HARDBOUNDARY);
6158 store_float_geom(win);
6162 region_containment(win, r, SWM_CW_ALLSIDES | SWM_CW_RESIZABLE |
6163 SWM_CW_SOFTBOUNDARY);
6166 /* get cursor offset from window root */
6167 xpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, win->id),
6174 if (xpr->win_x < WIDTH(win) / 2)
6177 if (xpr->win_y < HEIGHT(win) / 2)
6180 if (args->id == SWM_ARG_ID_CENTER)
6181 cursor = cursors[XC_SIZING].cid;
6183 cursor = cursors[left ? XC_TOP_LEFT_CORNER :
6184 XC_TOP_RIGHT_CORNER].cid;
6186 cursor = cursors[left ? XC_BOTTOM_LEFT_CORNER :
6187 XC_BOTTOM_RIGHT_CORNER].cid;
6189 xcb_grab_pointer(conn, 0, win->id, MOUSEMASK,
6190 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, XCB_WINDOW_NONE, cursor,
6195 while (resizing && (evt = xcb_wait_for_event(conn))) {
6196 switch (XCB_EVENT_RESPONSE_TYPE(evt)) {
6197 case XCB_BUTTON_RELEASE:
6198 DNPRINTF(SWM_D_EVENT, "resize: BUTTON_RELEASE\n");
6201 case XCB_MOTION_NOTIFY:
6202 mne = (xcb_motion_notify_event_t *)evt;
6203 /* cursor offset/delta from start of the operation */
6204 dx = mne->root_x - xpr->root_x;
6205 dy = mne->root_y - xpr->root_y;
6211 if (args->id == SWM_ARG_ID_CENTER) {
6212 if (g.h / 2 + dy < 1)
6216 HEIGHT(win) = g.h + 2 * dy;
6224 HEIGHT(win) = g.h + dy;
6231 if (args->id == SWM_ARG_ID_CENTER) {
6232 if (g.w / 2 + dx < 1)
6236 WIDTH(win) = g.w + 2 * dx;
6244 WIDTH(win) = g.w + dx;
6247 /* not free, don't sync more than 120 times / second */
6248 if ((mne->time - timestamp) > (1000 / 120) ) {
6249 timestamp = mne->time;
6250 regionize(win, mne->root_x, mne->root_y);
6251 region_containment(win, r, SWM_CW_ALLSIDES |
6252 SWM_CW_RESIZABLE | SWM_CW_HARDBOUNDARY |
6253 SWM_CW_SOFTBOUNDARY);
6260 xcb_allow_events(conn, XCB_ALLOW_ASYNC_KEYBOARD,
6261 ((xcb_key_press_event_t *)evt)->time);
6267 /* It's possible for win to have been freed above. */
6268 if (validate_win(win)) {
6269 DNPRINTF(SWM_D_EVENT, "resize: invalid win.\n");
6277 region_containment(win, r, SWM_CW_ALLSIDES | SWM_CW_RESIZABLE |
6278 SWM_CW_HARDBOUNDARY | SWM_CW_SOFTBOUNDARY);
6282 store_float_geom(win);
6284 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
6286 DNPRINTF(SWM_D_EVENT, "resize: done.\n");
6290 resize_step(struct swm_region *r, union arg *args)
6292 struct ws_win *win = NULL;
6294 if (r && r->ws && r->ws->focus)
6303 /* Try to set window region based on supplied coordinates or window center. */
6305 regionize(struct ws_win *win, int x, int y)
6307 struct swm_region *r = NULL;
6309 r = region_under(win->s, x, y);
6311 r = region_under(win->s, X(win) + WIDTH(win) / 2,
6312 Y(win) + HEIGHT(win) / 2);
6314 if (r != NULL && r != win->ws->r) {
6315 if (clear_maximized(r->ws) > 0)
6318 win_to_ws(win, r->ws->idx, 0);
6320 /* Set focus on new ws. */
6321 unfocus_win(r->ws->focus);
6329 #define SWM_MOVE_STEPS (50)
6332 move(struct ws_win *win, union arg *args)
6334 struct swm_region *r;
6335 xcb_timestamp_t timestamp = 0;
6336 int move_stp = 0, moving, restack = 0;
6337 xcb_query_pointer_reply_t *qpr = NULL;
6338 xcb_generic_event_t *evt;
6339 xcb_motion_notify_event_t *mne;
6344 if ((r = win->ws->r) == NULL)
6347 if (FULLSCREEN(win))
6350 DNPRINTF(SWM_D_EVENT, "move: win %#x, floating: %s, transient: "
6351 "%#x\n", win->id, YESNO(ABOVE(win)), win->transient);
6353 /* in max_stack mode should only move transients */
6354 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !TRANS(win))
6357 if (!(ABOVE(win) || TRANS(win)) || MAXIMIZED(win)) {
6358 store_float_geom(win);
6362 ewmh_apply_flags(win, (win->ewmh_flags | SWM_F_MANUAL | EWMH_F_ABOVE) &
6364 ewmh_update_wm_state(win);
6371 /* It's possible for win to have been freed during focus_flush(). */
6372 if (validate_win(win)) {
6373 DNPRINTF(SWM_D_EVENT, "move: invalid win.\n");
6379 case SWM_ARG_ID_MOVELEFT:
6380 X(win) -= (SWM_MOVE_STEPS - border_width);
6383 case SWM_ARG_ID_MOVERIGHT:
6384 X(win) += (SWM_MOVE_STEPS - border_width);
6387 case SWM_ARG_ID_MOVEUP:
6388 Y(win) -= (SWM_MOVE_STEPS - border_width);
6391 case SWM_ARG_ID_MOVEDOWN:
6392 Y(win) += (SWM_MOVE_STEPS - border_width);
6399 regionize(win, -1, -1);
6400 region_containment(win, win->ws->r, SWM_CW_ALLSIDES);
6402 store_float_geom(win);
6406 xcb_grab_pointer(conn, 0, win->id, MOUSEMASK,
6407 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC,
6408 XCB_WINDOW_NONE, cursors[XC_FLEUR].cid, XCB_CURRENT_TIME);
6410 /* get cursor offset from window root */
6411 qpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, win->id),
6414 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
6418 regionize(win, qpr->root_x, qpr->root_y);
6419 region_containment(win, win->ws->r, SWM_CW_ALLSIDES |
6420 SWM_CW_SOFTBOUNDARY);
6424 while (moving && (evt = xcb_wait_for_event(conn))) {
6425 switch (XCB_EVENT_RESPONSE_TYPE(evt)) {
6426 case XCB_BUTTON_RELEASE:
6427 DNPRINTF(SWM_D_EVENT, "move: BUTTON_RELEASE\n");
6430 case XCB_MOTION_NOTIFY:
6431 mne = (xcb_motion_notify_event_t *)evt;
6432 DNPRINTF(SWM_D_EVENT, "motion: root: %#x\n", mne->root);
6433 X(win) = mne->root_x - qpr->win_x - border_width;
6434 Y(win) = mne->root_y - qpr->win_y - border_width;
6436 /* not free, don't sync more than 120 times / second */
6437 if ((mne->time - timestamp) > (1000 / 120) ) {
6438 timestamp = mne->time;
6439 regionize(win, mne->root_x, mne->root_y);
6440 region_containment(win, win->ws->r,
6441 SWM_CW_ALLSIDES | SWM_CW_SOFTBOUNDARY);
6448 xcb_allow_events(conn, XCB_ALLOW_ASYNC_KEYBOARD,
6449 ((xcb_key_press_event_t *)evt)->time);
6455 /* It's possible for win to have been freed above. */
6456 if (validate_win(win)) {
6457 DNPRINTF(SWM_D_EVENT, "move: invalid win.\n");
6465 region_containment(win, win->ws->r, SWM_CW_ALLSIDES |
6466 SWM_CW_SOFTBOUNDARY);
6470 store_float_geom(win);
6472 /* New region set to fullscreen layout. */
6473 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK]) {
6480 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
6481 DNPRINTF(SWM_D_EVENT, "move: done.\n");
6485 move_step(struct swm_region *r, union arg *args)
6487 struct ws_win *win = NULL;
6489 if (r && r->ws && r->ws->focus)
6494 if (!TRANS(win) && !ABOVE(win))
6501 /* key definitions */
6503 char name[SWM_FUNCNAME_LEN];
6504 void (*func)(struct swm_region *r, union arg *);
6506 } keyfuncs[KF_INVALID + 1] = {
6507 /* name function argument */
6508 { "bar_toggle", bar_toggle, {.id = SWM_ARG_ID_BAR_TOGGLE} },
6509 { "bar_toggle_ws", bar_toggle, {.id = SWM_ARG_ID_BAR_TOGGLE_WS} },
6510 { "button2", pressbutton, {2} },
6511 { "cycle_layout", cycle_layout, {0} },
6512 { "flip_layout", stack_config, {.id = SWM_ARG_ID_FLIPLAYOUT} },
6513 { "float_toggle", floating_toggle,{0} },
6514 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
6515 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
6516 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
6517 { "focus_urgent", focus, {.id = SWM_ARG_ID_FOCUSURGENT} },
6518 { "maximize_toggle", maximize_toggle,{0} },
6519 { "height_grow", resize_step, {.id = SWM_ARG_ID_HEIGHTGROW} },
6520 { "height_shrink", resize_step, {.id = SWM_ARG_ID_HEIGHTSHRINK} },
6521 { "iconify", iconify, {0} },
6522 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
6523 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
6524 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
6525 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
6526 { "move_down", move_step, {.id = SWM_ARG_ID_MOVEDOWN} },
6527 { "move_left", move_step, {.id = SWM_ARG_ID_MOVELEFT} },
6528 { "move_right", move_step, {.id = SWM_ARG_ID_MOVERIGHT} },
6529 { "move_up", move_step, {.id = SWM_ARG_ID_MOVEUP} },
6530 { "mvrg_1", send_to_rg, {.id = 0} },
6531 { "mvrg_2", send_to_rg, {.id = 1} },
6532 { "mvrg_3", send_to_rg, {.id = 2} },
6533 { "mvrg_4", send_to_rg, {.id = 3} },
6534 { "mvrg_5", send_to_rg, {.id = 4} },
6535 { "mvrg_6", send_to_rg, {.id = 5} },
6536 { "mvrg_7", send_to_rg, {.id = 6} },
6537 { "mvrg_8", send_to_rg, {.id = 7} },
6538 { "mvrg_9", send_to_rg, {.id = 8} },
6539 { "mvws_1", send_to_ws, {.id = 0} },
6540 { "mvws_2", send_to_ws, {.id = 1} },
6541 { "mvws_3", send_to_ws, {.id = 2} },
6542 { "mvws_4", send_to_ws, {.id = 3} },
6543 { "mvws_5", send_to_ws, {.id = 4} },
6544 { "mvws_6", send_to_ws, {.id = 5} },
6545 { "mvws_7", send_to_ws, {.id = 6} },
6546 { "mvws_8", send_to_ws, {.id = 7} },
6547 { "mvws_9", send_to_ws, {.id = 8} },
6548 { "mvws_10", send_to_ws, {.id = 9} },
6549 { "mvws_11", send_to_ws, {.id = 10} },
6550 { "mvws_12", send_to_ws, {.id = 11} },
6551 { "mvws_13", send_to_ws, {.id = 12} },
6552 { "mvws_14", send_to_ws, {.id = 13} },
6553 { "mvws_15", send_to_ws, {.id = 14} },
6554 { "mvws_16", send_to_ws, {.id = 15} },
6555 { "mvws_17", send_to_ws, {.id = 16} },
6556 { "mvws_18", send_to_ws, {.id = 17} },
6557 { "mvws_19", send_to_ws, {.id = 18} },
6558 { "mvws_20", send_to_ws, {.id = 19} },
6559 { "mvws_21", send_to_ws, {.id = 20} },
6560 { "mvws_22", send_to_ws, {.id = 21} },
6561 { "name_workspace", name_workspace, {0} },
6562 { "quit", quit, {0} },
6563 { "raise_toggle", raise_toggle, {0} },
6564 { "restart", restart, {0} },
6565 { "rg_1", focusrg, {.id = 0} },
6566 { "rg_2", focusrg, {.id = 1} },
6567 { "rg_3", focusrg, {.id = 2} },
6568 { "rg_4", focusrg, {.id = 3} },
6569 { "rg_5", focusrg, {.id = 4} },
6570 { "rg_6", focusrg, {.id = 5} },
6571 { "rg_7", focusrg, {.id = 6} },
6572 { "rg_8", focusrg, {.id = 7} },
6573 { "rg_9", focusrg, {.id = 8} },
6574 { "rg_next", cyclerg, {.id = SWM_ARG_ID_CYCLERG_UP} },
6575 { "rg_prev", cyclerg, {.id = SWM_ARG_ID_CYCLERG_DOWN} },
6576 { "screen_next", cyclerg, {.id = SWM_ARG_ID_CYCLERG_UP} },
6577 { "screen_prev", cyclerg, {.id = SWM_ARG_ID_CYCLERG_DOWN} },
6578 { "search_win", search_win, {0} },
6579 { "search_workspace", search_workspace, {0} },
6580 { "spawn_custom", NULL, {0} },
6581 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
6582 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
6583 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
6584 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
6585 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
6586 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
6587 { "uniconify", uniconify, {0} },
6588 { "version", version, {0} },
6589 { "width_grow", resize_step, {.id = SWM_ARG_ID_WIDTHGROW} },
6590 { "width_shrink", resize_step, {.id = SWM_ARG_ID_WIDTHSHRINK} },
6591 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
6592 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
6593 { "ws_1", switchws, {.id = 0} },
6594 { "ws_2", switchws, {.id = 1} },
6595 { "ws_3", switchws, {.id = 2} },
6596 { "ws_4", switchws, {.id = 3} },
6597 { "ws_5", switchws, {.id = 4} },
6598 { "ws_6", switchws, {.id = 5} },
6599 { "ws_7", switchws, {.id = 6} },
6600 { "ws_8", switchws, {.id = 7} },
6601 { "ws_9", switchws, {.id = 8} },
6602 { "ws_10", switchws, {.id = 9} },
6603 { "ws_11", switchws, {.id = 10} },
6604 { "ws_12", switchws, {.id = 11} },
6605 { "ws_13", switchws, {.id = 12} },
6606 { "ws_14", switchws, {.id = 13} },
6607 { "ws_15", switchws, {.id = 14} },
6608 { "ws_16", switchws, {.id = 15} },
6609 { "ws_17", switchws, {.id = 16} },
6610 { "ws_18", switchws, {.id = 17} },
6611 { "ws_19", switchws, {.id = 18} },
6612 { "ws_20", switchws, {.id = 19} },
6613 { "ws_21", switchws, {.id = 20} },
6614 { "ws_22", switchws, {.id = 21} },
6615 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
6616 { "ws_next_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP_ALL} },
6617 { "ws_next_move", cyclews, {.id = SWM_ARG_ID_CYCLEWS_MOVE_UP} },
6618 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
6619 { "ws_prev_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN_ALL} },
6620 { "ws_prev_move", cyclews, {.id = SWM_ARG_ID_CYCLEWS_MOVE_DOWN} },
6621 { "ws_prior", priorws, {0} },
6622 { "dumpwins", dumpwins, {0} }, /* MUST BE LAST */
6623 { "invalid key func", NULL, {0} },
6627 key_cmp(struct key *kp1, struct key *kp2)
6629 if (kp1->keysym < kp2->keysym)
6631 if (kp1->keysym > kp2->keysym)
6634 if (kp1->mod < kp2->mod)
6636 if (kp1->mod > kp2->mod)
6643 enum { client_click, root_click };
6645 unsigned int action;
6647 unsigned int button;
6648 void (*func)(struct ws_win *, union arg *);
6651 #define MODKEY_SHIFT MODKEY | XCB_MOD_MASK_SHIFT
6652 /* action key mouse button func args */
6653 { client_click, MODKEY, XCB_BUTTON_INDEX_3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
6654 { client_click, MODKEY_SHIFT, XCB_BUTTON_INDEX_3, resize, {.id = SWM_ARG_ID_CENTER} },
6655 { client_click, MODKEY, XCB_BUTTON_INDEX_1, move, {0} },
6660 update_modkey(unsigned int mod)
6666 RB_FOREACH(kp, key_tree, &keys)
6667 if (kp->mod & XCB_MOD_MASK_SHIFT)
6668 kp->mod = mod | XCB_MOD_MASK_SHIFT;
6672 for (i = 0; i < LENGTH(buttons); i++)
6673 if (buttons[i].mask & XCB_MOD_MASK_SHIFT)
6674 buttons[i].mask = mod | XCB_MOD_MASK_SHIFT;
6676 buttons[i].mask = mod;
6680 spawn_expand(struct swm_region *r, union arg *args, const char *spawn_name,
6683 struct spawn_prog *prog = NULL;
6685 char *ap, **real_args;
6687 /* suppress unused warning since var is needed */
6690 DNPRINTF(SWM_D_SPAWN, "spawn_expand: %s\n", spawn_name);
6693 TAILQ_FOREACH(prog, &spawns, entry) {
6694 if (strcasecmp(spawn_name, prog->name) == 0)
6698 warnx("spawn_custom: program %s not found", spawn_name);
6702 /* make room for expanded args */
6703 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
6704 err(1, "spawn_custom: calloc real_args");
6706 /* expand spawn_args into real_args */
6707 for (i = c = 0; i < prog->argc; i++) {
6709 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg: %s\n", ap);
6710 if (strcasecmp(ap, "$bar_border") == 0) {
6712 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
6714 err(1, "spawn_custom border color");
6715 } else if (strcasecmp(ap, "$bar_color") == 0) {
6717 strdup(r->s->c[SWM_S_COLOR_BAR].name))
6719 err(1, "spawn_custom bar color");
6720 } else if (strcasecmp(ap, "$bar_font") == 0) {
6721 if ((real_args[c] = strdup(bar_fonts))
6723 err(1, "spawn_custom bar fonts");
6724 } else if (strcasecmp(ap, "$bar_font_color") == 0) {
6726 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
6728 err(1, "spawn_custom color font");
6729 } else if (strcasecmp(ap, "$color_focus") == 0) {
6731 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
6733 err(1, "spawn_custom color focus");
6734 } else if (strcasecmp(ap, "$color_focus_maximized") == 0) {
6736 strdup(r->s->c[SWM_S_COLOR_FOCUS_MAXIMIZED].name))
6738 err(1, "spawn_custom color focus maximized");
6739 } else if (strcasecmp(ap, "$color_unfocus") == 0) {
6741 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
6743 err(1, "spawn_custom color unfocus");
6744 } else if (strcasecmp(ap, "$color_unfocus_maximized") == 0) {
6746 strdup(r->s->c[SWM_S_COLOR_UNFOCUS_MAXIMIZED].name))
6748 err(1, "spawn_custom color unfocus maximized");
6749 } else if (strcasecmp(ap, "$region_index") == 0) {
6750 if (asprintf(&real_args[c], "%d",
6751 get_region_index(r) + 1) < 1)
6752 err(1, "spawn_custom region index");
6753 } else if (strcasecmp(ap, "$workspace_index") == 0) {
6754 if (asprintf(&real_args[c], "%d", r->ws->idx + 1) < 1)
6755 err(1, "spawn_custom workspace index");
6756 } else if (strcasecmp(ap, "$dmenu_bottom") == 0) {
6759 if ((real_args[c] = strdup("-b")) == NULL)
6760 err(1, "spawn_custom workspace index");
6762 /* no match --> copy as is */
6763 if ((real_args[c] = strdup(ap)) == NULL)
6764 err(1, "spawn_custom strdup(ap)");
6766 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg: %s\n",
6772 DNPRINTF(SWM_D_SPAWN, "spawn_custom: result: ");
6773 for (i = 0; i < c; ++i)
6774 DPRINTF("\"%s\" ", real_args[i]);
6777 *ret_args = real_args;
6782 spawn_custom(struct swm_region *r, union arg *args, const char *spawn_name)
6788 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
6792 spawn(r->ws->idx, &a, 1);
6794 for (i = 0; i < spawn_argc; i++)
6800 spawn_select(struct swm_region *r, union arg *args, const char *spawn_name,
6807 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
6811 if (pipe(select_list_pipe) == -1)
6812 err(1, "pipe error");
6813 if (pipe(select_resp_pipe) == -1)
6814 err(1, "pipe error");
6816 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
6817 err(1, "could not disable SIGPIPE");
6818 switch (*pid = fork()) {
6820 err(1, "cannot fork");
6823 if (dup2(select_list_pipe[0], STDIN_FILENO) == -1)
6825 if (dup2(select_resp_pipe[1], STDOUT_FILENO) == -1)
6827 close(select_list_pipe[1]);
6828 close(select_resp_pipe[0]);
6829 spawn(r->ws->idx, &a, 0);
6831 default: /* parent */
6832 close(select_list_pipe[0]);
6833 close(select_resp_pipe[1]);
6837 for (i = 0; i < spawn_argc; i++)
6842 /* Argument tokenizer. */
6845 int single_quoted = 0, double_quoted = 0;
6846 char *arg, *cp, *next;
6851 /* Eat and move characters until end of argument is found. */
6852 for (arg = next = cp = *sp; *cp != '\0'; ++cp) {
6853 if (!double_quoted && *cp == '\'') {
6854 /* Eat single-quote. */
6855 single_quoted = !single_quoted;
6856 } else if (!single_quoted && *cp == '"') {
6857 /* Eat double-quote. */
6858 double_quoted = !double_quoted;
6859 } else if (!single_quoted && *cp == '\\' && *(cp + 1) == '"') {
6860 /* Eat backslash; copy escaped character to arg. */
6862 } else if (!single_quoted && !double_quoted && *cp == '\\' &&
6863 (*(cp + 1) == '\'' || *(cp + 1) == ' ')) {
6864 /* Eat backslash; move escaped character. */
6866 } else if (!single_quoted && !double_quoted &&
6867 (*cp == ' ' || *cp == '\t')) {
6868 /* Terminate argument. */
6870 /* Point sp to beginning of next argument. */
6874 /* Move regular character. */
6879 /* Terminate argument if end of string. */
6889 spawn_insert(const char *name, const char *args, int flags)
6891 struct spawn_prog *sp;
6892 char *arg, *cp, *ptr;
6894 DNPRINTF(SWM_D_SPAWN, "spawn_insert: %s[%s]\n", name, args);
6896 if (args == NULL || *args == '\0')
6899 if ((sp = calloc(1, sizeof *sp)) == NULL)
6900 err(1, "spawn_insert: calloc");
6901 if ((sp->name = strdup(name)) == NULL)
6902 err(1, "spawn_insert: strdup");
6904 /* Convert the arguments to an argument list. */
6905 if ((ptr = cp = strdup(args)) == NULL)
6906 err(1, "spawn_insert: strdup");
6907 while ((arg = argsep(&ptr)) != NULL) {
6908 /* Null argument; skip it. */
6913 if ((sp->argv = realloc(sp->argv, sp->argc *
6914 sizeof *sp->argv)) == NULL)
6915 err(1, "spawn_insert: realloc");
6916 if ((sp->argv[sp->argc - 1] = strdup(arg)) == NULL)
6917 err(1, "spawn_insert: strdup");
6923 DNPRINTF(SWM_D_SPAWN, "arg %d: [%s]\n", sp->argc, sp->argv[sp->argc-1]);
6924 TAILQ_INSERT_TAIL(&spawns, sp, entry);
6925 DNPRINTF(SWM_D_SPAWN, "spawn_insert: leave\n");
6929 spawn_remove(struct spawn_prog *sp)
6933 DNPRINTF(SWM_D_SPAWN, "spawn_remove: %s\n", sp->name);
6935 TAILQ_REMOVE(&spawns, sp, entry);
6936 for (i = 0; i < sp->argc; i++)
6942 DNPRINTF(SWM_D_SPAWN, "spawn_remove: leave\n");
6946 spawn_find(const char *name)
6948 struct spawn_prog *sp;
6950 TAILQ_FOREACH(sp, &spawns, entry)
6951 if (strcasecmp(sp->name, name) == 0)
6958 setspawn(const char *name, const char *args, int flags)
6960 struct spawn_prog *sp;
6962 DNPRINTF(SWM_D_SPAWN, "setspawn: %s\n", name);
6967 /* Remove any old spawn under the same name. */
6968 if ((sp = spawn_find(name)) != NULL)
6972 spawn_insert(name, args, flags);
6974 warnx("error: setspawn: cannot find program: %s", name);
6976 DNPRINTF(SWM_D_SPAWN, "setspawn: leave\n");
6980 setconfspawn(const char *selector, const char *value, int flags)
6984 if (selector == NULL || strlen(selector) == 0)
6987 args = expand_tilde(value);
6989 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, args);
6991 setspawn(selector, args, flags);
6994 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done.\n");
6999 validate_spawns(void)
7001 struct spawn_prog *sp;
7002 char which[PATH_MAX];
7007 RB_FOREACH(kp, key_tree, &keys) {
7008 if (kp->funcid != KF_SPAWN_CUSTOM)
7012 sp = spawn_find(kp->spawn_name);
7013 if (sp == NULL || sp->flags & SWM_SPAWN_OPTIONAL)
7016 /* verify we have the goods */
7017 snprintf(which, sizeof which, "which %s", sp->argv[0]);
7018 DNPRINTF(SWM_D_CONF, "validate_spawns: which %s\n",
7020 for (i = strlen("which "); i < strlen(which); i++)
7021 if (which[i] == ' ') {
7025 if (system(which) != 0)
7026 add_startup_exception("could not find %s",
7027 &which[strlen("which ")]);
7034 setconfspawn("lock", "xlock", 0);
7036 setconfspawn("term", "xterm", 0);
7037 setconfspawn("spawn_term", "xterm", 0);
7039 setconfspawn("menu", "dmenu_run"
7043 " -nf $bar_font_color"
7045 " -sf $bar_color", 0);
7047 setconfspawn("search", "dmenu"
7052 " -nf $bar_font_color"
7054 " -sf $bar_color", 0);
7056 setconfspawn("name_workspace", "dmenu"
7061 " -nf $bar_font_color"
7063 " -sf $bar_color", 0);
7065 /* These are not verified for existence, even with a binding set. */
7066 setconfspawn("screenshot_all", "screenshot.sh full", SWM_SPAWN_OPTIONAL);
7067 setconfspawn("screenshot_wind", "screenshot.sh window", SWM_SPAWN_OPTIONAL);
7068 setconfspawn("initscr", "initscreen.sh", SWM_SPAWN_OPTIONAL);
7072 #define SWM_MODNAME_SIZE 32
7073 #define SWM_KEY_WS "\n+ \t"
7075 parsekeys(const char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
7077 char *str, *cp, *name;
7080 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
7081 if (mod == NULL || ks == NULL) {
7082 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
7085 if (keystr == NULL || strlen(keystr) == 0) {
7086 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
7090 if ((cp = str = strdup(keystr)) == NULL)
7091 err(1, "parsekeys: strdup");
7093 *ks = XCB_NO_SYMBOL;
7095 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
7096 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
7098 cp += (long)strspn(cp, SWM_KEY_WS);
7099 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
7101 else if (strncasecmp(name, "Mod1", SWM_MODNAME_SIZE) == 0)
7102 *mod |= XCB_MOD_MASK_1;
7103 else if (strncasecmp(name, "Mod2", SWM_MODNAME_SIZE) == 0)
7104 *mod += XCB_MOD_MASK_2;
7105 else if (strncmp(name, "Mod3", SWM_MODNAME_SIZE) == 0)
7106 *mod |= XCB_MOD_MASK_3;
7107 else if (strncmp(name, "Mod4", SWM_MODNAME_SIZE) == 0)
7108 *mod |= XCB_MOD_MASK_4;
7109 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
7110 *mod |= XCB_MOD_MASK_SHIFT;
7111 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
7112 *mod |= XCB_MOD_MASK_CONTROL;
7114 *ks = XStringToKeysym(name);
7115 XConvertCase(*ks, ks, &uks);
7116 if (ks == XCB_NO_SYMBOL) {
7118 "parsekeys: invalid key %s\n",
7127 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
7132 strdupsafe(const char *str)
7137 return (strdup(str));
7141 key_insert(unsigned int mod, KeySym ks, enum keyfuncid kfid,
7142 const char *spawn_name)
7146 DNPRINTF(SWM_D_KEY, "key_insert: enter %s [%s]\n",
7147 keyfuncs[kfid].name, spawn_name);
7149 if ((kp = malloc(sizeof *kp)) == NULL)
7150 err(1, "key_insert: malloc");
7155 kp->spawn_name = strdupsafe(spawn_name);
7156 RB_INSERT(key_tree, &keys, kp);
7158 DNPRINTF(SWM_D_KEY, "key_insert: leave\n");
7162 key_lookup(unsigned int mod, KeySym ks)
7169 return (RB_FIND(key_tree, &keys, &kp));
7173 key_remove(struct key *kp)
7175 DNPRINTF(SWM_D_KEY, "key_remove: %s\n", keyfuncs[kp->funcid].name);
7177 RB_REMOVE(key_tree, &keys, kp);
7178 free(kp->spawn_name);
7181 DNPRINTF(SWM_D_KEY, "key_remove: leave\n");
7185 key_replace(struct key *kp, unsigned int mod, KeySym ks, enum keyfuncid kfid,
7186 const char *spawn_name)
7188 DNPRINTF(SWM_D_KEY, "key_replace: %s [%s]\n", keyfuncs[kp->funcid].name,
7192 key_insert(mod, ks, kfid, spawn_name);
7194 DNPRINTF(SWM_D_KEY, "key_replace: leave\n");
7198 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid,
7199 const char *spawn_name)
7203 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
7204 keyfuncs[kfid].name, spawn_name);
7206 if ((kp = key_lookup(mod, ks)) != NULL) {
7207 if (kfid == KF_INVALID)
7210 key_replace(kp, mod, ks, kfid, spawn_name);
7211 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
7214 if (kfid == KF_INVALID) {
7215 warnx("bind: Key combination already unbound.");
7216 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
7220 key_insert(mod, ks, kfid, spawn_name);
7221 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
7225 setconfbinding(const char *selector, const char *value, int flags)
7227 enum keyfuncid kfid;
7230 struct spawn_prog *sp;
7232 /* suppress unused warning since var is needed */
7235 DNPRINTF(SWM_D_KEY, "setconfbinding: enter selector: [%s], "
7236 "value: [%s]\n", selector, value);
7237 if (selector == NULL || strlen(selector) == 0) {
7238 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
7239 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
7241 setkeybinding(mod, ks, kfid, NULL);
7246 /* search by key function name */
7247 for (kfid = 0; kfid < KF_INVALID; (kfid)++) {
7248 if (strncasecmp(selector, keyfuncs[kfid].name,
7249 SWM_FUNCNAME_LEN) == 0) {
7250 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match "
7251 "keyfunc\n", selector);
7252 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
7253 setkeybinding(mod, ks, kfid, NULL);
7259 /* search by custom spawn name */
7260 if ((sp = spawn_find(selector)) != NULL) {
7261 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match "
7262 "spawn\n", selector);
7263 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
7264 setkeybinding(mod, ks, KF_SPAWN_CUSTOM,
7270 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
7277 #define MODKEY_SHIFT MODKEY | XCB_MOD_MASK_SHIFT
7278 setkeybinding(MODKEY, XK_b, KF_BAR_TOGGLE, NULL);
7279 setkeybinding(MODKEY_SHIFT, XK_b, KF_BAR_TOGGLE_WS,NULL);
7280 setkeybinding(MODKEY, XK_v, KF_BUTTON2, NULL);
7281 setkeybinding(MODKEY, XK_space, KF_CYCLE_LAYOUT,NULL);
7282 setkeybinding(MODKEY_SHIFT, XK_backslash, KF_FLIP_LAYOUT, NULL);
7283 setkeybinding(MODKEY, XK_t, KF_FLOAT_TOGGLE,NULL);
7284 setkeybinding(MODKEY, XK_m, KF_FOCUS_MAIN, NULL);
7285 setkeybinding(MODKEY, XK_j, KF_FOCUS_NEXT, NULL);
7286 setkeybinding(MODKEY, XK_Tab, KF_FOCUS_NEXT, NULL);
7287 setkeybinding(MODKEY, XK_k, KF_FOCUS_PREV, NULL);
7288 setkeybinding(MODKEY_SHIFT, XK_Tab, KF_FOCUS_PREV, NULL);
7289 setkeybinding(MODKEY, XK_u, KF_FOCUS_URGENT,NULL);
7290 setkeybinding(MODKEY, XK_e, KF_MAXIMIZE_TOGGLE,NULL);
7291 setkeybinding(MODKEY_SHIFT, XK_equal, KF_HEIGHT_GROW,NULL);
7292 setkeybinding(MODKEY_SHIFT, XK_minus, KF_HEIGHT_SHRINK,NULL);
7293 setkeybinding(MODKEY, XK_w, KF_ICONIFY, NULL);
7294 setkeybinding(MODKEY, XK_h, KF_MASTER_SHRINK, NULL);
7295 setkeybinding(MODKEY, XK_l, KF_MASTER_GROW, NULL);
7296 setkeybinding(MODKEY, XK_comma, KF_MASTER_ADD, NULL);
7297 setkeybinding(MODKEY, XK_period, KF_MASTER_DEL, NULL);
7298 setkeybinding(MODKEY_SHIFT, XK_bracketright,KF_MOVE_DOWN,NULL);
7299 setkeybinding(MODKEY, XK_bracketleft, KF_MOVE_LEFT,NULL);
7300 setkeybinding(MODKEY, XK_bracketright,KF_MOVE_RIGHT,NULL);
7301 setkeybinding(MODKEY_SHIFT, XK_bracketleft, KF_MOVE_UP, NULL);
7302 setkeybinding(MODKEY_SHIFT, XK_KP_End, KF_MVRG_1, NULL);
7303 setkeybinding(MODKEY_SHIFT, XK_KP_Down, KF_MVRG_2, NULL);
7304 setkeybinding(MODKEY_SHIFT, XK_KP_Next, KF_MVRG_3, NULL);
7305 setkeybinding(MODKEY_SHIFT, XK_KP_Left, KF_MVRG_4, NULL);
7306 setkeybinding(MODKEY_SHIFT, XK_KP_Begin, KF_MVRG_5, NULL);
7307 setkeybinding(MODKEY_SHIFT, XK_KP_Right, KF_MVRG_6, NULL);
7308 setkeybinding(MODKEY_SHIFT, XK_KP_Home, KF_MVRG_7, NULL);
7309 setkeybinding(MODKEY_SHIFT, XK_KP_Up, KF_MVRG_8, NULL);
7310 setkeybinding(MODKEY_SHIFT, XK_KP_Prior, KF_MVRG_9, NULL);
7311 setkeybinding(MODKEY_SHIFT, XK_1, KF_MVWS_1, NULL);
7312 setkeybinding(MODKEY_SHIFT, XK_2, KF_MVWS_2, NULL);
7313 setkeybinding(MODKEY_SHIFT, XK_3, KF_MVWS_3, NULL);
7314 setkeybinding(MODKEY_SHIFT, XK_4, KF_MVWS_4, NULL);
7315 setkeybinding(MODKEY_SHIFT, XK_5, KF_MVWS_5, NULL);
7316 setkeybinding(MODKEY_SHIFT, XK_6, KF_MVWS_6, NULL);
7317 setkeybinding(MODKEY_SHIFT, XK_7, KF_MVWS_7, NULL);
7318 setkeybinding(MODKEY_SHIFT, XK_8, KF_MVWS_8, NULL);
7319 setkeybinding(MODKEY_SHIFT, XK_9, KF_MVWS_9, NULL);
7320 setkeybinding(MODKEY_SHIFT, XK_0, KF_MVWS_10, NULL);
7321 setkeybinding(MODKEY_SHIFT, XK_F1, KF_MVWS_11, NULL);
7322 setkeybinding(MODKEY_SHIFT, XK_F2, KF_MVWS_12, NULL);
7323 setkeybinding(MODKEY_SHIFT, XK_F3, KF_MVWS_13, NULL);
7324 setkeybinding(MODKEY_SHIFT, XK_F4, KF_MVWS_14, NULL);
7325 setkeybinding(MODKEY_SHIFT, XK_F5, KF_MVWS_15, NULL);
7326 setkeybinding(MODKEY_SHIFT, XK_F6, KF_MVWS_16, NULL);
7327 setkeybinding(MODKEY_SHIFT, XK_F7, KF_MVWS_17, NULL);
7328 setkeybinding(MODKEY_SHIFT, XK_F8, KF_MVWS_18, NULL);
7329 setkeybinding(MODKEY_SHIFT, XK_F9, KF_MVWS_19, NULL);
7330 setkeybinding(MODKEY_SHIFT, XK_F10, KF_MVWS_20, NULL);
7331 setkeybinding(MODKEY_SHIFT, XK_F11, KF_MVWS_21, NULL);
7332 setkeybinding(MODKEY_SHIFT, XK_F12, KF_MVWS_22, NULL);
7333 setkeybinding(MODKEY_SHIFT, XK_slash, KF_NAME_WORKSPACE,NULL);
7334 setkeybinding(MODKEY_SHIFT, XK_q, KF_QUIT, NULL);
7335 setkeybinding(MODKEY_SHIFT, XK_r, KF_RAISE_TOGGLE,NULL);
7336 setkeybinding(MODKEY, XK_q, KF_RESTART, NULL);
7337 setkeybinding(MODKEY, XK_KP_End, KF_RG_1, NULL);
7338 setkeybinding(MODKEY, XK_KP_Down, KF_RG_2, NULL);
7339 setkeybinding(MODKEY, XK_KP_Next, KF_RG_3, NULL);
7340 setkeybinding(MODKEY, XK_KP_Left, KF_RG_4, NULL);
7341 setkeybinding(MODKEY, XK_KP_Begin, KF_RG_5, NULL);
7342 setkeybinding(MODKEY, XK_KP_Right, KF_RG_6, NULL);
7343 setkeybinding(MODKEY, XK_KP_Home, KF_RG_7, NULL);
7344 setkeybinding(MODKEY, XK_KP_Up, KF_RG_8, NULL);
7345 setkeybinding(MODKEY, XK_KP_Prior, KF_RG_9, NULL);
7346 setkeybinding(MODKEY_SHIFT, XK_Right, KF_RG_NEXT, NULL);
7347 setkeybinding(MODKEY_SHIFT, XK_Left, KF_RG_PREV, NULL);
7348 setkeybinding(MODKEY, XK_f, KF_SEARCH_WIN, NULL);
7349 setkeybinding(MODKEY, XK_slash, KF_SEARCH_WORKSPACE,NULL);
7350 setkeybinding(MODKEY_SHIFT, XK_i, KF_SPAWN_CUSTOM,"initscr");
7351 setkeybinding(MODKEY_SHIFT, XK_Delete, KF_SPAWN_CUSTOM,"lock");
7352 setkeybinding(MODKEY, XK_p, KF_SPAWN_CUSTOM,"menu");
7353 setkeybinding(MODKEY, XK_s, KF_SPAWN_CUSTOM,"screenshot_all");
7354 setkeybinding(MODKEY_SHIFT, XK_s, KF_SPAWN_CUSTOM,"screenshot_wind");
7355 setkeybinding(MODKEY_SHIFT, XK_Return, KF_SPAWN_CUSTOM,"term");
7356 setkeybinding(MODKEY_SHIFT, XK_comma, KF_STACK_INC, NULL);
7357 setkeybinding(MODKEY_SHIFT, XK_period, KF_STACK_DEC, NULL);
7358 setkeybinding(MODKEY_SHIFT, XK_space, KF_STACK_RESET, NULL);
7359 setkeybinding(MODKEY, XK_Return, KF_SWAP_MAIN, NULL);
7360 setkeybinding(MODKEY_SHIFT, XK_j, KF_SWAP_NEXT, NULL);
7361 setkeybinding(MODKEY_SHIFT, XK_k, KF_SWAP_PREV, NULL);
7362 setkeybinding(MODKEY_SHIFT, XK_w, KF_UNICONIFY, NULL);
7363 setkeybinding(MODKEY_SHIFT, XK_v, KF_VERSION, NULL);
7364 setkeybinding(MODKEY, XK_equal, KF_WIDTH_GROW, NULL);
7365 setkeybinding(MODKEY, XK_minus, KF_WIDTH_SHRINK,NULL);
7366 setkeybinding(MODKEY, XK_x, KF_WIND_DEL, NULL);
7367 setkeybinding(MODKEY_SHIFT, XK_x, KF_WIND_KILL, NULL);
7368 setkeybinding(MODKEY, XK_1, KF_WS_1, NULL);
7369 setkeybinding(MODKEY, XK_2, KF_WS_2, NULL);
7370 setkeybinding(MODKEY, XK_3, KF_WS_3, NULL);
7371 setkeybinding(MODKEY, XK_4, KF_WS_4, NULL);
7372 setkeybinding(MODKEY, XK_5, KF_WS_5, NULL);
7373 setkeybinding(MODKEY, XK_6, KF_WS_6, NULL);
7374 setkeybinding(MODKEY, XK_7, KF_WS_7, NULL);
7375 setkeybinding(MODKEY, XK_8, KF_WS_8, NULL);
7376 setkeybinding(MODKEY, XK_9, KF_WS_9, NULL);
7377 setkeybinding(MODKEY, XK_0, KF_WS_10, NULL);
7378 setkeybinding(MODKEY, XK_F1, KF_WS_11, NULL);
7379 setkeybinding(MODKEY, XK_F2, KF_WS_12, NULL);
7380 setkeybinding(MODKEY, XK_F3, KF_WS_13, NULL);
7381 setkeybinding(MODKEY, XK_F4, KF_WS_14, NULL);
7382 setkeybinding(MODKEY, XK_F5, KF_WS_15, NULL);
7383 setkeybinding(MODKEY, XK_F6, KF_WS_16, NULL);
7384 setkeybinding(MODKEY, XK_F7, KF_WS_17, NULL);
7385 setkeybinding(MODKEY, XK_F8, KF_WS_18, NULL);
7386 setkeybinding(MODKEY, XK_F9, KF_WS_19, NULL);
7387 setkeybinding(MODKEY, XK_F10, KF_WS_20, NULL);
7388 setkeybinding(MODKEY, XK_F11, KF_WS_21, NULL);
7389 setkeybinding(MODKEY, XK_F12, KF_WS_22, NULL);
7390 setkeybinding(MODKEY, XK_Right, KF_WS_NEXT, NULL);
7391 setkeybinding(MODKEY, XK_Left, KF_WS_PREV, NULL);
7392 setkeybinding(MODKEY, XK_Up, KF_WS_NEXT_ALL, NULL);
7393 setkeybinding(MODKEY, XK_Down, KF_WS_PREV_ALL, NULL);
7394 setkeybinding(MODKEY_SHIFT, XK_Up, KF_WS_NEXT_MOVE,NULL);
7395 setkeybinding(MODKEY_SHIFT, XK_Down, KF_WS_PREV_MOVE,NULL);
7396 setkeybinding(MODKEY, XK_a, KF_WS_PRIOR, NULL);
7398 setkeybinding(MODKEY_SHIFT, XK_d, KF_DUMPWINS, NULL);
7408 while (RB_EMPTY(&keys) == 0) {
7409 kp = RB_ROOT(&keys);
7415 setkeymapping(const char *selector, const char *value, int flags)
7417 char *keymapping_file;
7419 /* suppress unused warnings since vars are needed */
7423 DNPRINTF(SWM_D_KEY, "setkeymapping: enter\n");
7425 keymapping_file = expand_tilde(value);
7428 /* load new key bindings; if it fails, revert to default bindings */
7429 if (conf_load(keymapping_file, SWM_CONF_KEYMAPPING)) {
7434 free(keymapping_file);
7436 DNPRINTF(SWM_D_KEY, "setkeymapping: leave\n");
7441 updatenumlockmask(void)
7444 xcb_get_modifier_mapping_reply_t *modmap_r;
7445 xcb_keycode_t *modmap, kc, *keycode;
7449 modmap_r = xcb_get_modifier_mapping_reply(conn,
7450 xcb_get_modifier_mapping(conn),
7453 modmap = xcb_get_modifier_mapping_keycodes(modmap_r);
7454 for (i = 0; i < 8; i++) {
7455 for (j = 0; j < modmap_r->keycodes_per_modifier; j++) {
7456 kc = modmap[i * modmap_r->keycodes_per_modifier
7458 keycode = xcb_key_symbols_get_keycode(syms,
7462 numlockmask = (1 << i);
7469 DNPRINTF(SWM_D_MISC, "updatenumlockmask: %d\n", numlockmask);
7476 int num_screens, k, j;
7477 unsigned int modifiers[4];
7478 xcb_keycode_t *code;
7480 DNPRINTF(SWM_D_MISC, "grabkeys\n");
7481 updatenumlockmask();
7484 modifiers[1] = numlockmask;
7485 modifiers[2] = XCB_MOD_MASK_LOCK;
7486 modifiers[3] = numlockmask | XCB_MOD_MASK_LOCK;
7488 num_screens = get_screen_count();
7489 for (k = 0; k < num_screens; k++) {
7490 if (TAILQ_EMPTY(&screens[k].rl))
7492 xcb_ungrab_key(conn, XCB_GRAB_ANY, screens[k].root,
7494 RB_FOREACH(kp, key_tree, &keys) {
7495 /* Skip unused ws binds. */
7496 if ((int)kp->funcid > KF_WS_1 + workspace_limit - 1 &&
7497 kp->funcid <= KF_WS_22)
7500 /* Skip unused mvws binds. */
7501 if ((int)kp->funcid > KF_MVWS_1 + workspace_limit - 1 &&
7502 kp->funcid <= KF_MVWS_22)
7505 if ((code = xcb_key_symbols_get_keycode(syms,
7507 for (j = 0; j < LENGTH(modifiers); j++)
7508 xcb_grab_key(conn, 1,
7510 kp->mod | modifiers[j],
7511 *code, XCB_GRAB_MODE_SYNC,
7512 XCB_GRAB_MODE_SYNC);
7520 grabbuttons(struct ws_win *win)
7522 unsigned int modifiers[4];
7525 DNPRINTF(SWM_D_MOUSE, "grabbuttons: win %#x\n", win->id);
7526 updatenumlockmask();
7529 modifiers[1] = numlockmask;
7530 modifiers[2] = XCB_MOD_MASK_LOCK;
7531 modifiers[3] = numlockmask | XCB_MOD_MASK_LOCK;
7533 for (i = 0; i < LENGTH(buttons); i++)
7534 if (buttons[i].action == client_click)
7535 for (j = 0; j < LENGTH(modifiers); ++j)
7536 xcb_grab_button(conn, 0, win->id, BUTTONMASK,
7537 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC,
7538 XCB_WINDOW_NONE, XCB_CURSOR_NONE,
7539 buttons[i].button, buttons[i].mask |
7543 const char *quirkname[] = {
7544 "NONE", /* config string for "no value" */
7552 "FOCUSONMAP_SINGLE",
7557 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
7558 #define SWM_Q_WS "\n|+ \t"
7560 parsequirks(const char *qstr, unsigned long *quirk)
7562 char *str, *cp, *name;
7565 if (quirk == NULL || qstr == NULL)
7568 if ((str = strdup(qstr)) == NULL)
7569 err(1, "parsequirks: strdup");
7573 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
7575 cp += (long)strspn(cp, SWM_Q_WS);
7576 for (i = 0; i < LENGTH(quirkname); i++) {
7577 if (strncasecmp(name, quirkname[i],
7578 SWM_QUIRK_LEN) == 0) {
7579 DNPRINTF(SWM_D_QUIRK,
7580 "parsequirks: %s\n", name);
7586 *quirk |= 1 << (i-1);
7590 if (i >= LENGTH(quirkname)) {
7591 DNPRINTF(SWM_D_QUIRK,
7592 "parsequirks: invalid quirk [%s]\n", name);
7603 quirk_insert(const char *class, const char *instance, const char *name,
7604 unsigned long quirk)
7610 DNPRINTF(SWM_D_QUIRK, "quirk_insert: class: %s, instance: %s, name: %s,"
7611 " value: %lu\n", class, instance, name, quirk);
7613 if ((qp = malloc(sizeof *qp)) == NULL)
7614 err(1, "quirk_insert: malloc");
7616 if ((qp->class = strdup(class)) == NULL)
7617 err(1, "quirk_insert: strdup");
7618 if ((qp->instance = strdup(instance)) == NULL)
7619 err(1, "quirk_insert: strdup");
7620 if ((qp->name = strdup(name)) == NULL)
7621 err(1, "quirk_insert: strdup");
7623 if (asprintf(&str, "^%s$", class) == -1)
7624 err(1, "quirk_insert: asprintf");
7625 if (regcomp(&qp->regex_class, str, REG_EXTENDED | REG_NOSUB)) {
7626 add_startup_exception("regex failed to compile quirk 'class' "
7627 "field: %s", class);
7630 DNPRINTF(SWM_D_QUIRK, "quirk_insert: compiled: %s\n", str);
7633 if (asprintf(&str, "^%s$", instance) == -1)
7634 err(1, "quirk_insert: asprintf");
7635 if (regcomp(&qp->regex_instance, str, REG_EXTENDED | REG_NOSUB)) {
7636 add_startup_exception("regex failed to compile quirk 'instance'"
7637 " field: %s", instance);
7640 DNPRINTF(SWM_D_QUIRK, "quirk_insert: compiled: %s\n", str);
7643 if (asprintf(&str, "^%s$", name) == -1)
7644 err(1, "quirk_insert: asprintf");
7645 if (regcomp(&qp->regex_name, str, REG_EXTENDED | REG_NOSUB)) {
7646 add_startup_exception("regex failed to compile quirk 'name' "
7650 DNPRINTF(SWM_D_QUIRK, "quirk_insert: compiled: %s\n", str);
7654 DNPRINTF(SWM_D_QUIRK, "quirk_insert: regex error; skipping.\n");
7658 TAILQ_INSERT_TAIL(&quirks, qp, entry);
7660 DNPRINTF(SWM_D_QUIRK, "quirk_insert: leave\n");
7664 quirk_remove(struct quirk *qp)
7666 DNPRINTF(SWM_D_QUIRK, "quirk_remove: %s:%s [%lu]\n", qp->class,
7667 qp->name, qp->quirk);
7669 TAILQ_REMOVE(&quirks, qp, entry);
7672 DNPRINTF(SWM_D_QUIRK, "quirk_remove: leave\n");
7676 quirk_free(struct quirk *qp)
7678 regfree(&qp->regex_class);
7679 regfree(&qp->regex_instance);
7680 regfree(&qp->regex_name);
7688 quirk_replace(struct quirk *qp, const char *class, const char *instance,
7689 const char *name, unsigned long quirk)
7691 DNPRINTF(SWM_D_QUIRK, "quirk_replace: %s:%s:%s [%lu]\n", qp->class,
7692 qp->instance, qp->name, qp->quirk);
7695 quirk_insert(class, instance, name, quirk);
7697 DNPRINTF(SWM_D_QUIRK, "quirk_replace: leave\n");
7701 setquirk(const char *class, const char *instance, const char *name,
7702 unsigned long quirk)
7706 DNPRINTF(SWM_D_QUIRK, "setquirk: enter %s:%s:%s [%lu]\n", class,
7707 instance, name, quirk);
7709 /* Remove/replace existing quirk. */
7710 TAILQ_FOREACH(qp, &quirks, entry) {
7711 if (strcmp(qp->class, class) == 0 &&
7712 strcmp(qp->instance, instance) == 0 &&
7713 strcmp(qp->name, name) == 0) {
7717 quirk_replace(qp, class, instance, name, quirk);
7718 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
7723 /* Only insert if quirk is not NONE. */
7725 quirk_insert(class, instance, name, quirk);
7727 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
7730 /* Eat '\' in str used to escape square brackets and colon. */
7732 unescape_selector(char *str)
7736 for (cp = str; *str != '\0'; ++str, ++cp) {
7737 if (*str == '\\' && (*(str + 1) == ':' || *(str + 1) == ']' ||
7747 setconfquirk(const char *selector, const char *value, int flags)
7749 char *str, *cp, *class;
7750 char *instance = NULL, *name = NULL;
7751 int retval, count = 0;
7754 /* suppress unused warning since var is needed */
7757 if (selector == NULL || strlen(selector) == 0)
7760 if ((str = strdup(selector)) == NULL)
7761 err(1, "setconfquirk: strdup");
7763 /* Find non-escaped colon. */
7770 for (++cp; *cp != '\0'; ++cp) {
7771 if (*cp == ':' && *(cp - 1) != '\\') {
7777 unescape_selector(class);
7779 instance = class + strlen(class) + 1;
7780 unescape_selector(instance);
7786 name = instance + strlen(instance) + 1;
7787 unescape_selector(name);
7792 DNPRINTF(SWM_D_CONF, "setconfquirk: class: %s, instance: %s, "
7793 "name: %s\n", class, instance, name);
7795 if ((retval = parsequirks(value, &qrks)) == 0)
7796 setquirk(class, instance, name, qrks);
7805 setquirk("MPlayer", "xv", ".*", SWM_Q_FLOAT | SWM_Q_FULLSCREEN | SWM_Q_FOCUSPREV);
7806 setquirk("OpenOffice.org 3.2", "VCLSalFrame", ".*", SWM_Q_FLOAT);
7807 setquirk("Firefox-bin", "firefox-bin", ".*", SWM_Q_TRANSSZ);
7808 setquirk("Firefox", "Dialog", ".*", SWM_Q_FLOAT);
7809 setquirk("Gimp", "gimp", ".*", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
7810 setquirk("XTerm", "xterm", ".*", SWM_Q_XTERM_FONTADJ);
7811 setquirk("xine", "Xine Window", ".*", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
7812 setquirk("Xitk", "Xitk Combo", ".*", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
7813 setquirk("xine", "xine Panel", ".*", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
7814 setquirk("Xitk", "Xine Window", ".*", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
7815 setquirk("xine", "xine Video Fullscreen Window", ".*", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
7816 setquirk("pcb", "pcb", ".*", SWM_Q_FLOAT);
7817 setquirk("SDL_App", "SDL_App", ".*", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
7820 /* conf file stuff */
7821 #define SWM_CONF_FILE "spectrwm.conf"
7822 #define SWM_CONF_FILE_OLD "scrotwm.conf"
7826 SWM_S_BAR_AT_BOTTOM,
7827 SWM_S_BAR_BORDER_WIDTH,
7830 SWM_S_BAR_ENABLED_WS,
7835 SWM_S_BOUNDARY_WIDTH,
7836 SWM_S_CLOCK_ENABLED,
7839 SWM_S_CYCLE_VISIBLE,
7841 SWM_S_DISABLE_BORDER,
7843 SWM_S_FOCUS_CLOSE_WRAP,
7844 SWM_S_FOCUS_DEFAULT,
7846 SWM_S_ICONIC_ENABLED,
7847 SWM_S_REGION_PADDING,
7852 SWM_S_STACK_ENABLED,
7855 SWM_S_URGENT_ENABLED,
7856 SWM_S_VERBOSE_LAYOUT,
7857 SWM_S_WINDOW_CLASS_ENABLED,
7858 SWM_S_WINDOW_INSTANCE_ENABLED,
7859 SWM_S_WINDOW_NAME_ENABLED,
7860 SWM_S_WORKSPACE_LIMIT,
7861 SWM_S_WORKSPACE_NAME,
7865 setconfvalue(const char *selector, const char *value, int flags)
7867 struct workspace *ws;
7868 int i, ws_id, num_screens;
7869 char *b, *str, s[1024];
7872 case SWM_S_BAR_ACTION:
7874 if ((bar_argv[0] = expand_tilde(value)) == NULL)
7875 err(1, "setconfvalue: bar_action");
7877 case SWM_S_BAR_AT_BOTTOM:
7878 bar_at_bottom = atoi(value);
7880 case SWM_S_BAR_BORDER_WIDTH:
7881 bar_border_width = atoi(value);
7882 if (bar_border_width < 0)
7883 bar_border_width = 0;
7885 case SWM_S_BAR_DELAY:
7886 /* No longer needed; leave to not break old conf files. */
7888 case SWM_S_BAR_ENABLED:
7889 bar_enabled = atoi(value);
7891 case SWM_S_BAR_ENABLED_WS:
7892 ws_id = atoi(selector) - 1;
7893 if (ws_id < 0 || ws_id >= workspace_limit)
7894 errx(1, "setconfvalue: bar_enabled_ws: invalid "
7895 "workspace %d.", ws_id + 1);
7897 num_screens = get_screen_count();
7898 for (i = 0; i < num_screens; i++) {
7899 ws = (struct workspace *)&screens[i].ws;
7900 ws[ws_id].bar_enabled = atoi(value);
7903 case SWM_S_BAR_FONT:
7905 if (asprintf(&bar_fonts, "%s,%s", value, bar_fonts) == -1)
7906 err(1, "setconfvalue: asprintf: failed to allocate "
7907 "memory for bar_fonts.");
7910 /* If already in xft mode, then we are done. */
7911 if (!bar_font_legacy)
7914 if ((str = strdup(value)) == NULL)
7915 err(1, "setconfvalue: strdup");
7917 /* If there are any non-XLFD entries, switch to Xft mode. */
7918 while ((b = strsep(&str, ",")) != NULL) {
7922 bar_font_legacy = 0;
7929 case SWM_S_BAR_FORMAT:
7931 if ((bar_format = strdup(value)) == NULL)
7932 err(1, "setconfvalue: bar_format");
7934 case SWM_S_BAR_JUSTIFY:
7935 if (strcmp(value, "left") == 0)
7936 bar_justify = SWM_BAR_JUSTIFY_LEFT;
7937 else if (strcmp(value, "center") == 0)
7938 bar_justify = SWM_BAR_JUSTIFY_CENTER;
7939 else if (strcmp(value, "right") == 0)
7940 bar_justify = SWM_BAR_JUSTIFY_RIGHT;
7942 errx(1, "invalid bar_justify");
7944 case SWM_S_BORDER_WIDTH:
7945 border_width = atoi(value);
7946 if (border_width < 0)
7949 case SWM_S_BOUNDARY_WIDTH:
7950 boundary_width = atoi(value);
7951 if (boundary_width < 0)
7954 case SWM_S_CLOCK_ENABLED:
7955 clock_enabled = atoi(value);
7957 case SWM_S_CLOCK_FORMAT:
7958 #ifndef SWM_DENY_CLOCK_FORMAT
7960 if ((clock_format = strdup(value)) == NULL)
7961 err(1, "setconfvalue: clock_format");
7964 case SWM_S_CYCLE_EMPTY:
7965 cycle_empty = atoi(value);
7967 case SWM_S_CYCLE_VISIBLE:
7968 cycle_visible = atoi(value);
7970 case SWM_S_DIALOG_RATIO:
7971 dialog_ratio = atof(value);
7972 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
7975 case SWM_S_DISABLE_BORDER:
7976 disable_border = atoi(value);
7978 case SWM_S_FOCUS_CLOSE:
7979 if (strcmp(value, "first") == 0)
7980 focus_close = SWM_STACK_BOTTOM;
7981 else if (strcmp(value, "last") == 0)
7982 focus_close = SWM_STACK_TOP;
7983 else if (strcmp(value, "next") == 0)
7984 focus_close = SWM_STACK_ABOVE;
7985 else if (strcmp(value, "previous") == 0)
7986 focus_close = SWM_STACK_BELOW;
7988 errx(1, "focus_close");
7990 case SWM_S_FOCUS_CLOSE_WRAP:
7991 focus_close_wrap = atoi(value);
7993 case SWM_S_FOCUS_DEFAULT:
7994 if (strcmp(value, "last") == 0)
7995 focus_default = SWM_STACK_TOP;
7996 else if (strcmp(value, "first") == 0)
7997 focus_default = SWM_STACK_BOTTOM;
7999 errx(1, "focus_default");
8001 case SWM_S_FOCUS_MODE:
8002 if (strcmp(value, "default") == 0)
8003 focus_mode = SWM_FOCUS_DEFAULT;
8004 else if (strcmp(value, "follow") == 0 ||
8005 strcmp(value, "follow_cursor") == 0)
8006 focus_mode = SWM_FOCUS_FOLLOW;
8007 else if (strcmp(value, "manual") == 0)
8008 focus_mode = SWM_FOCUS_MANUAL;
8010 errx(1, "focus_mode");
8012 case SWM_S_ICONIC_ENABLED:
8013 iconic_enabled = atoi(value);
8015 case SWM_S_REGION_PADDING:
8016 region_padding = atoi(value);
8017 if (region_padding < 0)
8020 case SWM_S_SPAWN_ORDER:
8021 if (strcmp(value, "first") == 0)
8022 spawn_position = SWM_STACK_BOTTOM;
8023 else if (strcmp(value, "last") == 0)
8024 spawn_position = SWM_STACK_TOP;
8025 else if (strcmp(value, "next") == 0)
8026 spawn_position = SWM_STACK_ABOVE;
8027 else if (strcmp(value, "previous") == 0)
8028 spawn_position = SWM_STACK_BELOW;
8030 errx(1, "spawn_position");
8032 case SWM_S_SPAWN_TERM:
8033 setconfspawn("term", value, 0);
8034 setconfspawn("spawn_term", value, 0);
8037 /* No longer needed; leave to not break old conf files. */
8039 case SWM_S_SS_ENABLED:
8040 /* No longer needed; leave to not break old conf files. */
8042 case SWM_S_STACK_ENABLED:
8043 stack_enabled = atoi(value);
8045 case SWM_S_TERM_WIDTH:
8046 term_width = atoi(value);
8050 case SWM_S_TILE_GAP:
8051 tile_gap = atoi(value);
8053 case SWM_S_URGENT_ENABLED:
8054 urgent_enabled = atoi(value);
8056 case SWM_S_VERBOSE_LAYOUT:
8057 verbose_layout = atoi(value);
8058 for (i = 0; layouts[i].l_stack != NULL; i++) {
8060 layouts[i].l_string = fancy_stacker;
8062 layouts[i].l_string = plain_stacker;
8065 case SWM_S_WINDOW_CLASS_ENABLED:
8066 window_class_enabled = atoi(value);
8068 case SWM_S_WINDOW_INSTANCE_ENABLED:
8069 window_instance_enabled = atoi(value);
8071 case SWM_S_WINDOW_NAME_ENABLED:
8072 window_name_enabled = atoi(value);
8074 case SWM_S_WORKSPACE_LIMIT:
8075 workspace_limit = atoi(value);
8076 if (workspace_limit > SWM_WS_MAX)
8077 workspace_limit = SWM_WS_MAX;
8078 else if (workspace_limit < 1)
8079 workspace_limit = 1;
8081 ewmh_update_desktops();
8083 case SWM_S_WORKSPACE_NAME:
8084 if (getenv("SWM_STARTED") != NULL)
8088 if (sscanf(value, "ws[%d]:%1023c", &ws_id, s) != 2)
8089 errx(1, "invalid entry, should be 'ws[<idx>]:name'");
8091 if (ws_id < 0 || ws_id >= workspace_limit)
8092 errx(1, "setconfvalue: workspace_name: invalid "
8093 "workspace %d.", ws_id + 1);
8095 num_screens = get_screen_count();
8096 for (i = 0; i < num_screens; ++i) {
8097 ws = (struct workspace *)&screens[i].ws;
8099 if (strlen(s) > 0) {
8100 free(ws[ws_id].name);
8101 if ((ws[ws_id].name = strdup(s)) == NULL)
8102 err(1, "setconfvalue: workspace_name.");
8104 ewmh_update_desktop_names();
8105 ewmh_get_desktop_names();
8116 setconfmodkey(const char *selector, const char *value, int flags)
8118 /* suppress unused warnings since vars are needed */
8122 if (strncasecmp(value, "Mod1", strlen("Mod1")) == 0)
8123 update_modkey(XCB_MOD_MASK_1);
8124 else if (strncasecmp(value, "Mod2", strlen("Mod2")) == 0)
8125 update_modkey(XCB_MOD_MASK_2);
8126 else if (strncasecmp(value, "Mod3", strlen("Mod3")) == 0)
8127 update_modkey(XCB_MOD_MASK_3);
8128 else if (strncasecmp(value, "Mod4", strlen("Mod4")) == 0)
8129 update_modkey(XCB_MOD_MASK_4);
8136 setconfcolor(const char *selector, const char *value, int flags)
8138 int first, last, i = 0, num_screens;
8140 num_screens = get_screen_count();
8142 /* conf screen indices begin at 1; treat vals <= 0 as 'all screens.' */
8143 if (selector == NULL || strlen(selector) == 0 ||
8144 (last = atoi(selector) - 1) < 0) {
8146 last = num_screens - 1;
8151 if (last >= num_screens) {
8152 add_startup_exception("invalid screen index: %d out of bounds "
8153 "(maximum %d)", last + 1, num_screens);
8157 for (i = first; i <= last; ++i) {
8158 setscreencolor(value, i, flags);
8161 * When setting focus/unfocus colors, we need to also
8162 * set maximize colors to match if they haven't been customized.
8164 if (flags == SWM_S_COLOR_FOCUS &&
8165 !screens[i].c[SWM_S_COLOR_FOCUS_MAXIMIZED].manual)
8166 setscreencolor(value, i, SWM_S_COLOR_FOCUS_MAXIMIZED);
8167 else if (flags == SWM_S_COLOR_UNFOCUS &&
8168 !screens[i].c[SWM_S_COLOR_UNFOCUS_MAXIMIZED].manual)
8169 setscreencolor(value, i, SWM_S_COLOR_UNFOCUS_MAXIMIZED);
8171 screens[i].c[flags].manual = 1;
8178 setconfregion(const char *selector, const char *value, int flags)
8180 /* suppress unused warnings since vars are needed */
8184 custom_region(value);
8189 setautorun(const char *selector, const char *value, int flags)
8199 /* suppress unused warnings since vars are needed */
8203 if (getenv("SWM_STARTED"))
8207 if (sscanf(value, "ws[%d]:%1023c", &ws_id, s) != 2)
8208 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
8210 if (ws_id < 0 || ws_id >= workspace_limit)
8211 errx(1, "autorun: invalid workspace %d", ws_id + 1);
8213 sp = expand_tilde((char *)&s);
8216 * This is a little intricate
8218 * If the pid already exists we simply reuse it because it means it was
8219 * used before AND not claimed by manage_window. We get away with
8220 * altering it in the parent after INSERT because this can not be a race
8223 while ((ap = strsep(&sp, " \t")) != NULL) {
8226 DNPRINTF(SWM_D_SPAWN, "setautorun: arg [%s]\n", ap);
8228 if ((a.argv = realloc(a.argv, argc * sizeof(char *))) == NULL)
8229 err(1, "setautorun: realloc");
8230 a.argv[argc - 1] = ap;
8234 if ((a.argv = realloc(a.argv, (argc + 1) * sizeof(char *))) == NULL)
8235 err(1, "setautorun: realloc");
8236 a.argv[argc] = NULL;
8238 if ((pid = fork()) == 0) {
8239 spawn(ws_id, &a, 1);
8248 p = calloc(1, sizeof *p);
8251 TAILQ_INSERT_TAIL(&pidlist, p, entry);
8261 setlayout(const char *selector, const char *value, int flags)
8263 int ws_id, i, x, mg, ma, si, ar, f = 0;
8264 int st = SWM_V_STACK, num_screens;
8266 struct workspace *ws;
8268 /* suppress unused warnings since vars are needed */
8272 if (getenv("SWM_STARTED"))
8276 if (sscanf(value, "ws[%d]:%d:%d:%d:%d:%1023c",
8277 &ws_id, &mg, &ma, &si, &ar, s) != 6)
8278 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
8279 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
8282 if (ws_id < 0 || ws_id >= workspace_limit)
8283 errx(1, "layout: invalid workspace %d", ws_id + 1);
8285 if (strcasecmp(s, "vertical") == 0)
8287 else if (strcasecmp(s, "vertical_flip") == 0) {
8290 } else if (strcasecmp(s, "horizontal") == 0)
8292 else if (strcasecmp(s, "horizontal_flip") == 0) {
8295 } else if (strcasecmp(s, "fullscreen") == 0)
8298 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
8299 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
8302 num_screens = get_screen_count();
8303 for (i = 0; i < num_screens; i++) {
8304 ws = (struct workspace *)&screens[i].ws;
8305 ws[ws_id].cur_layout = &layouts[st];
8307 ws[ws_id].always_raise = ar;
8308 if (st == SWM_MAX_STACK)
8312 for (x = 0; x < abs(mg); x++) {
8313 ws[ws_id].cur_layout->l_config(&ws[ws_id],
8314 mg >= 0 ? SWM_ARG_ID_MASTERGROW :
8315 SWM_ARG_ID_MASTERSHRINK);
8318 for (x = 0; x < abs(ma); x++) {
8319 ws[ws_id].cur_layout->l_config(&ws[ws_id],
8320 ma >= 0 ? SWM_ARG_ID_MASTERADD :
8321 SWM_ARG_ID_MASTERDEL);
8324 for (x = 0; x < abs(si); x++) {
8325 ws[ws_id].cur_layout->l_config(&ws[ws_id],
8326 si >= 0 ? SWM_ARG_ID_STACKINC :
8327 SWM_ARG_ID_STACKDEC);
8331 ws[ws_id].cur_layout->l_config(&ws[ws_id],
8332 SWM_ARG_ID_FLIPLAYOUT);
8339 /* config options */
8340 struct config_option {
8342 int (*func)(const char*, const char*, int);
8345 struct config_option configopt[] = {
8346 { "autorun", setautorun, 0 },
8347 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
8348 { "bar_at_bottom", setconfvalue, SWM_S_BAR_AT_BOTTOM },
8349 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
8350 { "bar_border_unfocus", setconfcolor, SWM_S_COLOR_BAR_BORDER_UNFOCUS },
8351 { "bar_border_width", setconfvalue, SWM_S_BAR_BORDER_WIDTH },
8352 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
8353 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
8354 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
8355 { "bar_enabled_ws", setconfvalue, SWM_S_BAR_ENABLED_WS },
8356 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
8357 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
8358 { "bar_format", setconfvalue, SWM_S_BAR_FORMAT },
8359 { "bar_justify", setconfvalue, SWM_S_BAR_JUSTIFY },
8360 { "bind", setconfbinding, 0 },
8361 { "border_width", setconfvalue, SWM_S_BORDER_WIDTH },
8362 { "boundary_width", setconfvalue, SWM_S_BOUNDARY_WIDTH },
8363 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
8364 { "clock_format", setconfvalue, SWM_S_CLOCK_FORMAT },
8365 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
8366 { "color_focus_maximized", setconfcolor, SWM_S_COLOR_FOCUS_MAXIMIZED },
8367 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
8368 { "color_unfocus_maximized", setconfcolor, SWM_S_COLOR_UNFOCUS_MAXIMIZED },
8369 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
8370 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
8371 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
8372 { "disable_border", setconfvalue, SWM_S_DISABLE_BORDER },
8373 { "focus_close", setconfvalue, SWM_S_FOCUS_CLOSE },
8374 { "focus_close_wrap", setconfvalue, SWM_S_FOCUS_CLOSE_WRAP },
8375 { "focus_default", setconfvalue, SWM_S_FOCUS_DEFAULT },
8376 { "focus_mode", setconfvalue, SWM_S_FOCUS_MODE },
8377 { "iconic_enabled", setconfvalue, SWM_S_ICONIC_ENABLED },
8378 { "keyboard_mapping", setkeymapping, 0 },
8379 { "layout", setlayout, 0 },
8380 { "modkey", setconfmodkey, 0 },
8381 { "program", setconfspawn, 0 },
8382 { "quirk", setconfquirk, 0 },
8383 { "region", setconfregion, 0 },
8384 { "region_padding", setconfvalue, SWM_S_REGION_PADDING },
8385 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
8386 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
8387 { "spawn_position", setconfvalue, SWM_S_SPAWN_ORDER },
8388 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
8389 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
8390 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
8391 { "tile_gap", setconfvalue, SWM_S_TILE_GAP },
8392 { "title_class_enabled", setconfvalue, SWM_S_WINDOW_CLASS_ENABLED }, /* For backwards compat. */
8393 { "title_name_enabled", setconfvalue, SWM_S_WINDOW_INSTANCE_ENABLED }, /* For backwards compat. */
8394 { "urgent_enabled", setconfvalue, SWM_S_URGENT_ENABLED },
8395 { "verbose_layout", setconfvalue, SWM_S_VERBOSE_LAYOUT },
8396 { "window_class_enabled", setconfvalue, SWM_S_WINDOW_CLASS_ENABLED },
8397 { "window_instance_enabled", setconfvalue, SWM_S_WINDOW_INSTANCE_ENABLED },
8398 { "window_name_enabled", setconfvalue, SWM_S_WINDOW_NAME_ENABLED },
8399 { "workspace_limit", setconfvalue, SWM_S_WORKSPACE_LIMIT },
8400 { "name", setconfvalue, SWM_S_WORKSPACE_NAME },
8404 _add_startup_exception(const char *fmt, va_list ap)
8406 if (vasprintf(&startup_exception, fmt, ap) == -1)
8407 warn("%s: asprintf", __func__);
8411 add_startup_exception(const char *fmt, ...)
8417 if (startup_exception)
8420 /* force bar to be enabled due to exception */
8424 _add_startup_exception(fmt, ap);
8429 conf_load(const char *filename, int keymapping)
8432 char *line = NULL, *cp, *ce, *optsub, *optval = NULL;
8433 size_t linelen, lineno = 0;
8434 int wordlen, i, optidx, count;
8435 struct config_option *opt = NULL;
8437 DNPRINTF(SWM_D_CONF, "conf_load: begin\n");
8439 if (filename == NULL) {
8440 warnx("conf_load: no filename");
8444 DNPRINTF(SWM_D_CONF, "conf_load: open %s\n", filename);
8446 if ((config = fopen(filename, "r")) == NULL) {
8447 warn("conf_load: fopen: %s", filename);
8451 while (!feof(config)) {
8455 if ((line = fparseln(config, &linelen, &lineno, NULL,
8456 FPARSELN_UNESCCOMM | FPARSELN_UNESCCONT)) == NULL) {
8458 err(1, "%s", filename);
8463 cp += strspn(cp, " \t\n"); /* eat whitespace */
8464 if (cp[0] == '\0') {
8468 /* get config option */
8469 wordlen = strcspn(cp, "=[ \t\n");
8471 add_startup_exception("%s: line %zd: no option found",
8476 for (i = 0; i < LENGTH(configopt); i++) {
8477 opt = &configopt[i];
8478 if (strncasecmp(cp, opt->optname, wordlen) == 0 &&
8479 (int)strlen(opt->optname) == wordlen) {
8485 add_startup_exception("%s: line %zd: unknown option "
8486 "%.*s", filename, lineno, wordlen, cp);
8489 if (keymapping && opt && strcmp(opt->optname, "bind")) {
8490 add_startup_exception("%s: line %zd: invalid option "
8491 "%.*s", filename, lineno, wordlen, cp);
8495 cp += strspn(cp, " \t\n"); /* eat whitespace */
8497 /* get [selector] if any */
8502 /* Get length of selector. */
8503 for (ce = ++cp; *ce != '\0'; ++ce) {
8504 /* Find matching (not escaped) bracket. */
8505 if (*ce == ']' && *(ce - 1) != '\\') {
8512 add_startup_exception("%s: line %zd: syntax "
8513 "error: unterminated selector", filename,
8518 /* ce points at ']'; terminate optsub. */
8523 cp += strspn(cp, "= \t\n"); /* eat trailing */
8526 if (strlen(optval) == 0) {
8527 add_startup_exception("%s: line %zd: must supply value "
8528 "to %s", filename, lineno,
8529 configopt[optidx].optname);
8532 /* call function to deal with it all */
8533 if (configopt[optidx].func(optsub, optval,
8534 configopt[optidx].funcflags) != 0) {
8535 add_startup_exception("%s: line %zd: invalid data for "
8536 "%s", filename, lineno, configopt[optidx].optname);
8544 DNPRINTF(SWM_D_CONF, "conf_load: end\n");
8550 set_child_transient(struct ws_win *win, xcb_window_t *trans)
8552 struct ws_win *parent, *w;
8553 struct swm_region *r;
8554 struct workspace *ws;
8555 xcb_icccm_wm_hints_t wmh;
8557 parent = find_window(win->transient);
8559 parent->focus_child = win;
8561 DNPRINTF(SWM_D_MISC, "set_child_transient: parent doesn't exist"
8562 " for %#x trans %#x\n", win->id, win->transient);
8564 r = root_to_region(win->s->root, SWM_CK_ALL);
8566 /* parent doen't exist in our window list */
8567 TAILQ_FOREACH(w, &ws->winlist, entry) {
8568 if (xcb_icccm_get_wm_hints_reply(conn,
8569 xcb_icccm_get_wm_hints(conn, w->id),
8571 warnx("can't get hints for %#x", w->id);
8575 if (win->hints.window_group != wmh.window_group)
8578 w->focus_child = win;
8579 win->transient = w->id;
8581 DNPRINTF(SWM_D_MISC, "set_child_transient: adjusting "
8582 "transient to %#x\n", win->transient);
8589 window_get_pid(xcb_window_t win)
8594 xcb_get_property_cookie_t pc;
8595 xcb_get_property_reply_t *pr;
8597 apid = get_atom_from_string("_NET_WM_PID");
8598 if (apid == XCB_ATOM_NONE)
8601 pc = xcb_get_property(conn, 0, win, apid, XCB_ATOM_CARDINAL, 0, 1);
8602 pr = xcb_get_property_reply(conn, pc, NULL);
8605 if (pr->type != XCB_ATOM_CARDINAL) {
8610 if (pr->type == apid && pr->format == 32)
8611 ret = *((pid_t *)xcb_get_property_value(pr));
8617 apid = get_atom_from_string("_SWM_PID");
8618 pc = xcb_get_property(conn, 0, win, apid, XCB_ATOM_STRING,
8620 pr = xcb_get_property_reply(conn, pc, NULL);
8623 if (pr->type != apid) {
8628 ret = (pid_t)strtonum(xcb_get_property_value(pr), 0, INT_MAX, &errstr);
8635 get_swm_ws(xcb_window_t id)
8641 xcb_get_property_reply_t *gpr;
8643 gpr = xcb_get_property_reply(conn,
8644 xcb_get_property(conn, 0, id, a_swm_ws,
8645 XCB_ATOM_STRING, 0, SWM_PROPLEN),
8650 proplen = xcb_get_property_value_length(gpr);
8652 prop = malloc(proplen + 1);
8655 xcb_get_property_value(gpr),
8657 prop[proplen] = '\0';
8664 DNPRINTF(SWM_D_PROP, "get_swm_ws: _SWM_WS: %s\n", prop);
8665 ws_idx = (int)strtonum(prop, 0, workspace_limit - 1, &errstr);
8667 DNPRINTF(SWM_D_PROP, "get_swm_ws: win #%s: %s",
8677 get_ws_idx(xcb_window_t id)
8679 xcb_get_property_reply_t *gpr;
8682 gpr = xcb_get_property_reply(conn,
8683 xcb_get_property(conn, 0, id, ewmh[_NET_WM_DESKTOP].atom,
8684 XCB_ATOM_CARDINAL, 0, 1),
8687 if (gpr->type == XCB_ATOM_CARDINAL && gpr->format == 32)
8688 ws_idx = *((int *)xcb_get_property_value(gpr));
8693 if ((ws_idx = get_swm_ws(id)) != -1)
8694 xcb_delete_property(conn, id, a_swm_ws);
8696 if (ws_idx > workspace_limit - 1 || ws_idx < -1)
8699 DNPRINTF(SWM_D_PROP, "get_ws_idx: win %#x, ws_idx: %d\n", id, ws_idx);
8705 manage_window(xcb_window_t id, int mapped)
8707 struct ws_win *win, *ww;
8708 struct swm_region *r;
8711 xcb_get_geometry_reply_t *gr;
8712 xcb_window_t trans = XCB_WINDOW_NONE;
8713 uint32_t i, wa[2], new_flags;
8715 char *class, *instance, *name;
8717 if ((win = find_window(id)) != NULL) {
8718 DNPRINTF(SWM_D_MISC, "manage_window: win %#x already "
8719 "managed; skipping.)\n", id);
8720 return (win); /* Already managed. */
8723 /* See if window is on the unmanaged list. */
8724 if ((win = find_unmanaged_window(id)) != NULL) {
8725 DNPRINTF(SWM_D_MISC, "manage_window: win %#x found on "
8726 "unmanaged list.\n", id);
8727 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
8730 set_child_transient(win, &trans);
8734 DNPRINTF(SWM_D_MISC, "manage_window: win %#x is new.\n", id);
8737 /* Try to get initial window geometry. */
8738 gr = xcb_get_geometry_reply(conn, xcb_get_geometry(conn, id), NULL);
8740 DNPRINTF(SWM_D_MISC, "manage_window: get geometry failed.\n");
8744 /* Create and initialize ws_win object. */
8745 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
8746 err(1, "manage_window: calloc: failed to allocate memory for "
8751 /* Figureout which region the window belongs to. */
8752 r = root_to_region(gr->root, SWM_CK_ALL);
8754 /* Ignore window border if there is one. */
8755 WIDTH(win) = gr->width;
8756 HEIGHT(win) = gr->height;
8757 X(win) = gr->x + gr->border_width - border_width;
8758 Y(win) = gr->y + gr->border_width - border_width;
8760 win->mapped = mapped;
8761 win->s = r->s; /* this never changes */
8765 /* Select which X events to monitor and set border pixel color. */
8766 wa[0] = win->s->c[SWM_S_COLOR_UNFOCUS].pixel;
8767 wa[1] = XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_PROPERTY_CHANGE |
8768 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
8770 wa[1] |= XCB_EVENT_MASK_LEAVE_WINDOW | XCB_EVENT_MASK_FOCUS_CHANGE;
8773 xcb_change_window_attributes(conn, win->id, XCB_CW_BORDER_PIXEL |
8774 XCB_CW_EVENT_MASK, wa);
8776 /* Get WM_SIZE_HINTS. */
8777 xcb_icccm_get_wm_normal_hints_reply(conn,
8778 xcb_icccm_get_wm_normal_hints(conn, win->id),
8782 xcb_icccm_get_wm_hints_reply(conn,
8783 xcb_icccm_get_wm_hints(conn, win->id),
8786 /* Get WM_TRANSIENT_FOR; see if window is a transient. */
8787 xcb_icccm_get_wm_transient_for_reply(conn,
8788 xcb_icccm_get_wm_transient_for(conn, win->id),
8791 win->transient = trans;
8792 set_child_transient(win, &win->transient);
8795 /* Get WM_PROTOCOLS. */
8796 get_wm_protocols(win);
8798 /* Set initial quirks based on EWMH. */
8799 ewmh_autoquirk(win);
8801 /* Determine initial quirks. */
8802 xcb_icccm_get_wm_class_reply(conn,
8803 xcb_icccm_get_wm_class(conn, win->id),
8806 class = win->ch.class_name ? win->ch.class_name : "";
8807 instance = win->ch.instance_name ? win->ch.instance_name : "";
8808 name = get_win_name(win->id);
8810 DNPRINTF(SWM_D_CLASS, "manage_window: class: %s, instance: %s, "
8811 "name: %s\n", class, instance, name);
8813 /* java is retarded so treat it special */
8814 if (strstr(instance, "sun-awt")) {
8815 DNPRINTF(SWM_D_CLASS, "manage_window: java window detected.\n");
8819 TAILQ_FOREACH(qp, &quirks, entry) {
8820 if (regexec(&qp->regex_class, class, 0, NULL, 0) == 0 &&
8821 regexec(&qp->regex_instance, instance, 0, NULL, 0) == 0 &&
8822 regexec(&qp->regex_name, name, 0, NULL, 0) == 0) {
8823 DNPRINTF(SWM_D_CLASS, "manage_window: matched "
8824 "quirk: %s:%s:%s mask: %#lx\n", qp->class,
8825 qp->instance, qp->name, qp->quirk);
8826 win->quirks = qp->quirk;
8832 /* Reset font sizes (the bruteforce way; no default keybinding). */
8833 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
8834 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
8835 fake_keypress(win, XK_KP_Subtract, XCB_MOD_MASK_SHIFT);
8836 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
8837 fake_keypress(win, XK_KP_Add, XCB_MOD_MASK_SHIFT);
8840 /* Figure out which workspace the window belongs to. */
8841 if (!(win->quirks & SWM_Q_IGNOREPID) &&
8842 (p = find_pid(window_get_pid(win->id))) != NULL) {
8843 win->ws = &r->s->ws[p->ws];
8844 TAILQ_REMOVE(&pidlist, p, entry);
8847 } else if ((ws_idx = get_ws_idx(win->id)) != -1 &&
8849 /* _SWM_WS is set; use that. */
8850 win->ws = &r->s->ws[ws_idx];
8851 } else if (trans && (ww = find_window(trans)) != NULL) {
8852 /* Launch transients in the same ws as parent. */
8858 /* Set the _NET_WM_DESKTOP atom. */
8859 DNPRINTF(SWM_D_PROP, "manage_window: set _NET_WM_DESKTOP: %d\n",
8861 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
8862 ewmh[_NET_WM_DESKTOP].atom, XCB_ATOM_CARDINAL, 32, 1, &win->ws->idx);
8864 /* WS must already be set for this to work. */
8865 store_float_geom(win);
8867 /* Make sure window is positioned inside its region, if its active. */
8869 region_containment(win, r, SWM_CW_ALLSIDES |
8870 SWM_CW_HARDBOUNDARY);
8875 /* Figure out where to stack the window in the workspace. */
8876 if (trans && (ww = find_window(trans)))
8877 TAILQ_INSERT_AFTER(&win->ws->winlist, ww, win, entry);
8878 else if (win->ws->focus && spawn_position == SWM_STACK_ABOVE)
8879 TAILQ_INSERT_AFTER(&win->ws->winlist, win->ws->focus, win,
8881 else if (win->ws->focus && spawn_position == SWM_STACK_BELOW)
8882 TAILQ_INSERT_BEFORE(win->ws->focus, win, entry);
8883 else switch (spawn_position) {
8886 case SWM_STACK_ABOVE:
8887 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
8889 case SWM_STACK_BOTTOM:
8890 case SWM_STACK_BELOW:
8891 TAILQ_INSERT_HEAD(&win->ws->winlist, win, entry);
8894 ewmh_update_client_list();
8896 TAILQ_INSERT_TAIL(&win->ws->stack, win, stack_entry);
8898 /* Get/apply initial _NET_WM_STATE */
8899 ewmh_get_wm_state(win);
8902 new_flags = win->ewmh_flags;
8904 if (win->quirks & SWM_Q_FLOAT)
8905 new_flags |= EWMH_F_ABOVE;
8907 if (win->quirks & SWM_Q_ANYWHERE)
8908 new_flags |= SWM_F_MANUAL;
8910 ewmh_apply_flags(win, new_flags);
8911 ewmh_update_wm_state(win);
8913 /* Set initial _NET_WM_ALLOWED_ACTIONS */
8914 ewmh_update_actions(win);
8918 DNPRINTF(SWM_D_MISC, "manage_window: done. win %#x, (x,y) w x h: "
8919 "(%d,%d) %d x %d, ws: %d, iconic: %s, transient: %#x\n", win->id,
8920 X(win), Y(win), WIDTH(win), HEIGHT(win), win->ws->idx,
8921 YESNO(ICONIC(win)), win->transient);
8927 free_window(struct ws_win *win)
8929 DNPRINTF(SWM_D_MISC, "free_window: win %#x\n", win->id);
8934 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
8936 xcb_icccm_get_wm_class_reply_wipe(&win->ch);
8941 memset(win, 0xff, sizeof *win); /* XXX kill later */
8944 DNPRINTF(SWM_D_MISC, "free_window: done.\n");
8948 unmanage_window(struct ws_win *win)
8950 struct ws_win *parent;
8955 DNPRINTF(SWM_D_MISC, "unmanage_window: win %#x\n", win->id);
8958 parent = find_window(win->transient);
8960 parent->focus_child = NULL;
8963 TAILQ_REMOVE(&win->ws->stack, win, stack_entry);
8964 TAILQ_REMOVE(&win->ws->winlist, win, entry);
8965 TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
8967 ewmh_update_client_list();
8971 expose(xcb_expose_event_t *e)
8974 struct swm_region *r;
8976 DNPRINTF(SWM_D_EVENT, "expose: win %#x\n", e->window);
8978 num_screens = get_screen_count();
8979 for (i = 0; i < num_screens; i++)
8980 TAILQ_FOREACH(r, &screens[i].rl, entry)
8981 if (e->window == WINID(r->bar))
8989 focusin(xcb_focus_in_event_t *e)
8991 DNPRINTF(SWM_D_EVENT, "focusin: win %#x, mode: %s(%u), "
8992 "detail: %s(%u)\n", e->event, get_notify_mode_label(e->mode),
8993 e->mode, get_notify_detail_label(e->detail), e->detail);
8997 focusout(xcb_focus_out_event_t *e)
8999 DNPRINTF(SWM_D_EVENT, "focusout: win %#x, mode: %s(%u), "
9000 "detail: %s(%u)\n", e->event, get_notify_mode_label(e->mode),
9001 e->mode, get_notify_detail_label(e->detail), e->detail);
9006 keypress(xcb_key_press_event_t *e)
9008 xcb_keysym_t keysym;
9011 keysym = xcb_key_press_lookup_keysym(syms, e, 0);
9013 DNPRINTF(SWM_D_EVENT, "keypress: keysym: %u, win (x,y): %#x (%d,%d), "
9014 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
9015 "state: %u, same_screen: %s\n", keysym, e->event, e->event_x,
9016 e->event_y, e->detail, e->time, e->root, e->root_x, e->root_y,
9017 e->child, e->state, YESNO(e->same_screen));
9019 if ((kp = key_lookup(CLEANMASK(e->state), keysym)) == NULL)
9022 last_event_time = e->time;
9024 if (kp->funcid == KF_SPAWN_CUSTOM)
9025 spawn_custom(root_to_region(e->root, SWM_CK_ALL),
9026 &(keyfuncs[kp->funcid].args), kp->spawn_name);
9027 else if (keyfuncs[kp->funcid].func)
9028 keyfuncs[kp->funcid].func(root_to_region(e->root, SWM_CK_ALL),
9029 &(keyfuncs[kp->funcid].args));
9032 /* Unfreeze grab events. */
9033 xcb_allow_events(conn, XCB_ALLOW_ASYNC_KEYBOARD, e->time);
9036 DNPRINTF(SWM_D_EVENT, "keypress: done.\n");
9040 buttonpress(xcb_button_press_event_t *e)
9042 struct ws_win *win = NULL;
9043 struct swm_region *r, *old_r;
9047 DNPRINTF(SWM_D_EVENT, "buttonpress: win (x,y): %#x (%d,%d), "
9048 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
9049 "state: %u, same_screen: %s\n", e->event, e->event_x, e->event_y,
9050 e->detail, e->time, e->root, e->root_x, e->root_y, e->child,
9051 e->state, YESNO(e->same_screen));
9053 if (e->event == e->root) {
9054 if (e->child != 0) {
9055 win = find_window(e->child);
9056 /* Pass ButtonPress to window if it isn't managed. */
9060 /* Focus on empty region */
9061 /* If no windows on region if its empty. */
9062 r = root_to_region(e->root, SWM_CK_POINTER);
9064 DNPRINTF(SWM_D_EVENT, "buttonpress: "
9065 "NULL region; ignoring.\n");
9069 if (TAILQ_EMPTY(&r->ws->winlist)) {
9070 old_r = root_to_region(e->root, SWM_CK_FOCUS);
9071 if (old_r && old_r != r)
9072 unfocus_win(old_r->ws->focus);
9074 xcb_set_input_focus(conn,
9075 XCB_INPUT_FOCUS_PARENT, e->root, e->time);
9077 /* Clear bar since empty. */
9085 win = find_window(e->event);
9091 last_event_time = e->time;
9093 focus_win(get_focus_magic(win));
9095 for (i = 0; i < LENGTH(buttons); i++)
9096 if (client_click == buttons[i].action && buttons[i].func &&
9097 buttons[i].button == e->detail &&
9098 CLEANMASK(buttons[i].mask) == CLEANMASK(e->state)) {
9099 buttons[i].func(win, &buttons[i].args);
9105 DNPRINTF(SWM_D_EVENT, "buttonpress: passing to window.\n");
9106 /* Replay event to event window */
9107 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, e->time);
9109 DNPRINTF(SWM_D_EVENT, "buttonpress: handled.\n");
9110 /* Unfreeze grab events. */
9111 xcb_allow_events(conn, XCB_ALLOW_SYNC_POINTER, e->time);
9119 print_win_geom(xcb_window_t w)
9121 xcb_get_geometry_reply_t *wa;
9123 wa = xcb_get_geometry_reply(conn, xcb_get_geometry(conn, w), NULL);
9125 DNPRINTF(SWM_D_MISC, "print_win_geom: window not found: %#x\n",
9130 DNPRINTF(SWM_D_MISC, "print_win_geom: win %#x, root: %#x, "
9131 "depth: %u, (x,y) w x h: (%d,%d) %d x %d, border: %d\n",
9132 w, wa->root, wa->depth, wa->x, wa->y, wa->width, wa->height,
9141 get_stack_mode_name(uint8_t mode)
9146 case XCB_STACK_MODE_ABOVE:
9149 case XCB_STACK_MODE_BELOW:
9152 case XCB_STACK_MODE_TOP_IF:
9155 case XCB_STACK_MODE_BOTTOM_IF:
9158 case XCB_STACK_MODE_OPPOSITE:
9170 configurerequest(xcb_configure_request_event_t *e)
9173 struct swm_region *r = NULL;
9176 uint32_t wc[7] = {0};
9178 if ((win = find_window(e->window)) == NULL)
9179 if ((win = find_unmanaged_window(e->window)) == NULL)
9183 if (swm_debug & SWM_D_EVENT) {
9184 print_win_geom(e->window);
9186 DNPRINTF(SWM_D_EVENT, "configurerequest: win %#x, "
9187 "parent: %#x, new: %s, value_mask: %u { ", e->window,
9188 e->parent, YESNO(new), e->value_mask);
9189 if (e->value_mask & XCB_CONFIG_WINDOW_X)
9190 DPRINTF("X: %d ", e->x);
9191 if (e->value_mask & XCB_CONFIG_WINDOW_Y)
9192 DPRINTF("Y: %d ", e->y);
9193 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH)
9194 DPRINTF("W: %u ", e->width);
9195 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
9196 DPRINTF("H: %u ", e->height);
9197 if (e->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH)
9198 DPRINTF("Border: %u ", e->border_width);
9199 if (e->value_mask & XCB_CONFIG_WINDOW_SIBLING)
9200 DPRINTF("Sibling: %#x ", e->sibling);
9201 if (e->value_mask & XCB_CONFIG_WINDOW_STACK_MODE)
9202 DPRINTF("StackMode: %s(%u) ",
9203 get_stack_mode_name(e->stack_mode), e->stack_mode);
9209 if (e->value_mask & XCB_CONFIG_WINDOW_X) {
9210 mask |= XCB_CONFIG_WINDOW_X;
9213 if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
9214 mask |= XCB_CONFIG_WINDOW_Y;
9217 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
9218 mask |= XCB_CONFIG_WINDOW_WIDTH;
9221 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
9222 mask |= XCB_CONFIG_WINDOW_HEIGHT;
9223 wc[i++] = e->height;
9225 if (e->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH) {
9226 mask |= XCB_CONFIG_WINDOW_BORDER_WIDTH;
9227 wc[i++] = e->border_width;
9229 if (e->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
9230 mask |= XCB_CONFIG_WINDOW_SIBLING;
9231 wc[i++] = e->sibling;
9233 if (e->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
9234 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
9235 wc[i++] = e->stack_mode;
9239 xcb_configure_window(conn, e->window, mask, wc);
9242 } else if ((!MANUAL(win) || win->quirks & SWM_Q_ANYWHERE) &&
9243 !FULLSCREEN(win) && !MAXIMIZED(win)) {
9246 else if (win->ws->old_r)
9249 /* windows are centered unless ANYWHERE quirk is set. */
9250 if (win->quirks & SWM_Q_ANYWHERE) {
9251 if (e->value_mask & XCB_CONFIG_WINDOW_X) {
9252 win->g_float.x = e->x;
9254 win->g_float.x -= X(r);
9257 if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
9258 win->g_float.y = e->y;
9260 win->g_float.y -= Y(r);
9264 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH)
9265 win->g_float.w = e->width;
9267 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
9268 win->g_float.h = e->height;
9270 win->g_floatvalid = 1;
9272 if (!MAXIMIZED(win) && !FULLSCREEN(win) &&
9273 (TRANS(win) || (ABOVE(win) &&
9274 win->ws->cur_layout != &layouts[SWM_MAX_STACK]))) {
9275 WIDTH(win) = win->g_float.w;
9276 HEIGHT(win) = win->g_float.h;
9279 update_floater(win);
9294 DNPRINTF(SWM_D_EVENT, "configurerequest: done.\n");
9298 configurenotify(xcb_configure_notify_event_t *e)
9302 DNPRINTF(SWM_D_EVENT, "configurenotify: win %#x, event win: %#x, "
9303 "(x,y) WxH: (%d,%d) %ux%u, border: %u, above_sibling: %#x, "
9304 "override_redirect: %s\n", e->window, e->event, e->x, e->y,
9305 e->width, e->height, e->border_width, e->above_sibling,
9306 YESNO(e->override_redirect));
9308 win = find_window(e->window);
9310 xcb_icccm_get_wm_normal_hints_reply(conn,
9311 xcb_icccm_get_wm_normal_hints(conn, win->id),
9314 if (font_adjusted) {
9322 destroynotify(xcb_destroy_notify_event_t *e)
9326 DNPRINTF(SWM_D_EVENT, "destroynotify: win %#x\n", e->window);
9328 if ((win = find_window(e->window)) == NULL) {
9329 if ((win = find_unmanaged_window(e->window)) == NULL)
9335 if (focus_mode != SWM_FOCUS_FOLLOW) {
9336 /* If we were focused, make sure we focus on something else. */
9337 if (win == win->ws->focus)
9338 win->ws->focus_pending = get_focus_prev(win);
9341 unmanage_window(win);
9344 if (focus_mode != SWM_FOCUS_FOLLOW && WS_FOCUSED(win->ws)) {
9345 if (win->ws->focus_pending) {
9346 focus_win(win->ws->focus_pending);
9347 win->ws->focus_pending = NULL;
9348 } else if (win == win->ws->focus) {
9349 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT,
9350 win->ws->r->id, XCB_CURRENT_TIME);
9361 get_notify_detail_label(uint8_t detail)
9366 case XCB_NOTIFY_DETAIL_ANCESTOR:
9369 case XCB_NOTIFY_DETAIL_VIRTUAL:
9372 case XCB_NOTIFY_DETAIL_INFERIOR:
9375 case XCB_NOTIFY_DETAIL_NONLINEAR:
9376 label = "Nonlinear";
9378 case XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL:
9379 label = "NonlinearVirtual";
9381 case XCB_NOTIFY_DETAIL_POINTER:
9384 case XCB_NOTIFY_DETAIL_POINTER_ROOT:
9385 label = "PointerRoot";
9387 case XCB_NOTIFY_DETAIL_NONE:
9398 get_notify_mode_label(uint8_t mode)
9403 case XCB_NOTIFY_MODE_NORMAL:
9406 case XCB_NOTIFY_MODE_GRAB:
9409 case XCB_NOTIFY_MODE_UNGRAB:
9412 case XCB_NOTIFY_MODE_WHILE_GRABBED:
9413 label = "WhileGrabbed";
9424 enternotify(xcb_enter_notify_event_t *e)
9427 struct swm_region *r;
9429 DNPRINTF(SWM_D_FOCUS, "enternotify: time: %u, win (x,y): %#x "
9430 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
9431 "child: %#x, same_screen_focus: %s, state: %d\n",
9432 e->time, e->event, e->event_x, e->event_y,
9433 get_notify_mode_label(e->mode), e->mode,
9434 get_notify_detail_label(e->detail), e->detail,
9435 e->root, e->root_x, e->root_y, e->child,
9436 YESNO(e->same_screen_focus), e->state);
9438 if (focus_mode == SWM_FOCUS_MANUAL &&
9439 e->mode == XCB_NOTIFY_MODE_NORMAL) {
9440 DNPRINTF(SWM_D_EVENT, "enternotify: manual focus; ignoring.\n");
9444 last_event_time = e->time;
9446 if ((win = find_window(e->event)) == NULL) {
9447 if (e->event == e->root) {
9448 /* If no windows on pointer region, then focus root. */
9449 r = root_to_region(e->root, SWM_CK_POINTER);
9451 DNPRINTF(SWM_D_EVENT, "enternotify: "
9452 "NULL region; ignoring.\n");
9458 DNPRINTF(SWM_D_EVENT, "enternotify: window is NULL; "
9463 if (e->mode == XCB_NOTIFY_MODE_NORMAL &&
9464 e->detail == XCB_NOTIFY_DETAIL_INFERIOR) {
9465 DNPRINTF(SWM_D_EVENT, "enternotify: entering from "
9466 "inferior; ignoring\n");
9470 focus_win(get_focus_magic(win));
9478 leavenotify(xcb_leave_notify_event_t *e)
9480 DNPRINTF(SWM_D_FOCUS, "leavenotify: time: %u, win (x,y): %#x "
9481 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
9482 "child: %#x, same_screen_focus: %s, state: %d\n",
9483 e->time, e->event, e->event_x, e->event_y,
9484 get_notify_mode_label(e->mode), e->mode,
9485 get_notify_detail_label(e->detail), e->detail,
9486 e->root, e->root_x, e->root_y, e->child,
9487 YESNO(e->same_screen_focus), e->state);
9492 mapnotify(xcb_map_notify_event_t *e)
9494 struct ws_win *win, *parent = NULL;
9495 struct workspace *ws;
9497 DNPRINTF(SWM_D_EVENT, "mapnotify: win %#x\n", e->window);
9499 if ((win = manage_window(e->window, 1)) == NULL)
9503 /* Need to know if win was mapped due to ws switch. */
9504 if (ws->state == SWM_WS_STATE_MAPPED) {
9505 if (ws->focus_pending && TRANS(ws->focus_pending))
9506 parent = find_window(win->transient);
9508 /* If window's parent is maximized, don't clear it. */
9509 if ((parent == NULL) || !MAXIMIZED(parent))
9510 if (clear_maximized(ws) > 0)
9515 set_win_state(win, XCB_ICCCM_WM_STATE_NORMAL);
9517 if (focus_mode != SWM_FOCUS_FOLLOW && WS_FOCUSED(win->ws)) {
9518 if (ws->focus_pending == win) {
9520 ws->focus_pending = NULL;
9529 mappingnotify(xcb_mapping_notify_event_t *e)
9532 int i, j, num_screens;
9534 xcb_refresh_keyboard_mapping(syms, e);
9536 if (e->request == XCB_MAPPING_KEYBOARD) {
9539 /* Regrab buttons on all managed windows. */
9540 num_screens = get_screen_count();
9541 for (i = 0; i < num_screens; i++)
9542 for (j = 0; j < workspace_limit; j++)
9543 TAILQ_FOREACH(w, &screens[i].ws[j].winlist,
9550 maprequest(xcb_map_request_event_t *e)
9552 struct ws_win *win, *w = NULL;
9553 xcb_get_window_attributes_reply_t *war;
9555 DNPRINTF(SWM_D_EVENT, "maprequest: win %#x\n",
9558 war = xcb_get_window_attributes_reply(conn,
9559 xcb_get_window_attributes(conn, e->window),
9562 DNPRINTF(SWM_D_EVENT, "maprequest: window lost.\n");
9566 if (war->override_redirect) {
9567 DNPRINTF(SWM_D_EVENT, "maprequest: override_redirect; "
9572 win = manage_window(e->window,
9573 (war->map_state == XCB_MAP_STATE_VIEWABLE));
9577 /* The new window should get focus; prepare. */
9578 if (focus_mode != SWM_FOCUS_FOLLOW &&
9579 !(win->quirks & SWM_Q_NOFOCUSONMAP) &&
9580 (!(win->hints.flags & XCB_ICCCM_WM_HINT_INPUT) ||
9581 (win->hints.flags & XCB_ICCCM_WM_HINT_INPUT &&
9582 win->hints.input))) {
9583 if (win->quirks & SWM_Q_FOCUSONMAP_SINGLE) {
9584 /* See if other wins of same type are already mapped. */
9585 TAILQ_FOREACH(w, &win->ws->winlist, entry) {
9586 if (w == win || !w->mapped)
9589 if (w->ch.class_name &&
9590 win->ch.class_name &&
9591 strcmp(w->ch.class_name,
9592 win->ch.class_name) == 0 &&
9593 w->ch.instance_name &&
9594 win->ch.instance_name &&
9595 strcmp(w->ch.instance_name,
9596 win->ch.instance_name) == 0)
9602 win->ws->focus_pending = get_focus_magic(win);
9605 /* All windows need to be mapped if they are in the current workspace.*/
9609 /* Ignore EnterNotify to handle the mapnotify without interference. */
9610 if (focus_mode == SWM_FOCUS_DEFAULT)
9611 event_drain(XCB_ENTER_NOTIFY);
9614 DNPRINTF(SWM_D_EVENT, "maprequest: done.\n");
9618 motionnotify(xcb_motion_notify_event_t *e)
9620 struct swm_region *r = NULL;
9623 DNPRINTF(SWM_D_FOCUS, "motionnotify: time: %u, win (x,y): %#x "
9624 "(%d,%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
9625 "child: %#x, same_screen_focus: %s, state: %d\n",
9626 e->time, e->event, e->event_x, e->event_y,
9627 get_notify_detail_label(e->detail), e->detail,
9628 e->root, e->root_x, e->root_y, e->child,
9629 YESNO(e->same_screen), e->state);
9631 last_event_time = e->time;
9633 if (focus_mode == SWM_FOCUS_MANUAL)
9636 num_screens = get_screen_count();
9637 for (i = 0; i < num_screens; i++)
9638 if (screens[i].root == e->root)
9641 TAILQ_FOREACH(r, &screens[i].rl, entry)
9642 if (X(r) <= e->root_x && e->root_x < MAX_X(r) &&
9643 Y(r) <= e->root_y && e->root_y < MAX_Y(r))
9651 get_atom_name(xcb_atom_t atom)
9654 #ifdef SWM_DEBUG_ATOM_NAMES
9656 * This should be disabled during most debugging since
9657 * xcb_get_* causes an xcb_flush.
9660 xcb_get_atom_name_reply_t *r;
9662 r = xcb_get_atom_name_reply(conn,
9663 xcb_get_atom_name(conn, atom),
9666 len = xcb_get_atom_name_name_length(r);
9668 name = malloc(len + 1);
9670 memcpy(name, xcb_get_atom_name_name(r), len);
9684 propertynotify(xcb_property_notify_event_t *e)
9687 struct workspace *ws;
9691 name = get_atom_name(e->atom);
9692 DNPRINTF(SWM_D_EVENT, "propertynotify: win %#x, atom: %s(%u), "
9693 "time: %#x, state: %u\n", e->window, name, e->atom, e->time,
9697 win = find_window(e->window);
9703 last_event_time = e->time;
9705 if (e->atom == a_state) {
9706 /* State just changed, make sure it gets focused if mapped. */
9707 if (e->state == XCB_PROPERTY_NEW_VALUE) {
9708 if (focus_mode != SWM_FOCUS_FOLLOW && WS_FOCUSED(ws)) {
9710 ws->focus_pending == win) {
9711 focus_win(ws->focus_pending);
9712 ws->focus_pending = NULL;
9716 } else if (e->atom == XCB_ATOM_WM_CLASS ||
9717 e->atom == XCB_ATOM_WM_NAME) {
9719 } else if (e->atom == a_prot) {
9720 get_wm_protocols(win);
9727 unmapnotify(xcb_unmap_notify_event_t *e)
9730 struct workspace *ws;
9732 DNPRINTF(SWM_D_EVENT, "unmapnotify: win %#x\n", e->window);
9734 /* If we aren't managing the window, then ignore. */
9735 win = find_window(e->window);
9736 if (win == NULL || win->id != e->window)
9739 /* Do nothing if already withdrawn. */
9740 if (!win->mapped && !ICONIC(win))
9746 /* If win was focused, make sure to focus on something else. */
9747 if (win == ws->focus) {
9748 if (focus_mode != SWM_FOCUS_FOLLOW) {
9749 ws->focus_pending = get_focus_prev(win);
9750 DNPRINTF(SWM_D_EVENT, "unmapnotify: "
9751 "focus_pending: %#x\n",
9752 WINID(ws->focus_pending));
9760 set_win_state(win, XCB_ICCCM_WM_STATE_ICONIC);
9763 set_win_state(win, XCB_ICCCM_WM_STATE_WITHDRAWN);
9764 unmanage_window(win);
9770 /* Update focus if ws is active. */
9771 if (WS_FOCUSED(ws)) {
9772 if (focus_mode == SWM_FOCUS_FOLLOW) {
9773 focus_win(get_pointer_win(ws->r->s->root));
9774 } else if (ws->focus_pending) {
9775 focus_win(ws->focus_pending);
9776 ws->focus_pending = NULL;
9777 } else if (ws->focus == NULL) {
9778 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT,
9779 ws->r->id, XCB_CURRENT_TIME);
9788 get_source_type_label(uint32_t type)
9793 case EWMH_SOURCE_TYPE_NONE:
9796 case EWMH_SOURCE_TYPE_NORMAL:
9799 case EWMH_SOURCE_TYPE_OTHER:
9811 clientmessage(xcb_client_message_event_t *e)
9814 struct swm_region *r = NULL;
9818 xcb_map_request_event_t mre;
9822 name = get_atom_name(e->type);
9823 DNPRINTF(SWM_D_EVENT, "clientmessage: win %#x, atom: %s(%u)\n",
9824 e->window, name, e->type);
9828 if (e->type == ewmh[_NET_CURRENT_DESKTOP].atom) {
9829 num_screens = get_screen_count();
9830 for (i = 0; i < num_screens; i++)
9831 if (screens[i].root == e->window) {
9832 r = screens[i].r_focus;
9836 if (r && e->data.data32[0] < (uint32_t)workspace_limit) {
9837 a.id = e->data.data32[0];
9845 win = find_window(e->window);
9847 if (e->type == ewmh[_NET_ACTIVE_WINDOW].atom) {
9848 /* Manage the window with maprequest. */
9849 DNPRINTF(SWM_D_EVENT, "clientmessage: request focus on "
9850 "unmanaged window.\n");
9851 mre.window = e->window;
9857 if (e->type == ewmh[_NET_ACTIVE_WINDOW].atom) {
9858 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_ACTIVE_WINDOW, "
9859 "source_type: %s(%d)\n",
9860 get_source_type_label(e->data.data32[0]),
9864 * Allow focus changes that are a result of direct user
9865 * action and from applications that use the old EWMH spec.
9867 if (e->data.data32[0] != EWMH_SOURCE_TYPE_NORMAL ||
9868 win->quirks & SWM_Q_OBEYAPPFOCUSREQ) {
9869 if (WS_FOCUSED(win->ws))
9872 win->ws->focus_pending = win;
9874 } else if (e->type == ewmh[_NET_CLOSE_WINDOW].atom) {
9875 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_CLOSE_WINDOW\n");
9876 if (win->can_delete)
9877 client_msg(win, a_delete, 0);
9879 xcb_kill_client(conn, win->id);
9880 } else if (e->type == ewmh[_NET_MOVERESIZE_WINDOW].atom) {
9881 DNPRINTF(SWM_D_EVENT,
9882 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
9884 if (e->data.data32[0] & (1<<8)) /* x */
9885 X(win) = e->data.data32[1];
9886 if (e->data.data32[0] & (1<<9)) /* y */
9887 Y(win) = e->data.data32[2];
9888 if (e->data.data32[0] & (1<<10)) /* width */
9889 WIDTH(win) = e->data.data32[3];
9890 if (e->data.data32[0] & (1<<11)) /* height */
9891 HEIGHT(win) = e->data.data32[4];
9895 /* Notify no change was made. */
9896 config_win(win, NULL);
9897 /* TODO: Change stack sizes */
9899 } else if (e->type == ewmh[_NET_RESTACK_WINDOW].atom) {
9900 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_RESTACK_WINDOW\n");
9901 val[0] = e->data.data32[1]; /* Sibling window. */
9902 val[1] = e->data.data32[2]; /* Stack mode detail. */
9904 xcb_configure_window(conn, win->id, XCB_CONFIG_WINDOW_SIBLING |
9905 XCB_CONFIG_WINDOW_STACK_MODE, val);
9906 } else if (e->type == ewmh[_NET_WM_STATE].atom) {
9907 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_WM_STATE\n");
9908 ewmh_change_wm_state(win, e->data.data32[1], e->data.data32[0]);
9909 if (e->data.data32[2])
9910 ewmh_change_wm_state(win, e->data.data32[2],
9913 ewmh_update_wm_state(win);
9915 } else if (e->type == ewmh[_NET_WM_DESKTOP].atom) {
9916 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_WM_DESKTOP\n");
9919 win_to_ws(win, e->data.data32[0], 1);
9921 /* Restack if either the source or destination ws is mapped. */
9922 if (r != NULL || win->ws->r != NULL) {
9924 load_float_geom(win);
9936 int errcode = xcb_connection_has_error(conn);
9937 #ifdef XCB_CONN_ERROR
9940 case XCB_CONN_ERROR:
9941 s = "Socket error, pipe error or other stream error.";
9943 case XCB_CONN_CLOSED_EXT_NOTSUPPORTED:
9944 s = "Extension not supported.";
9946 case XCB_CONN_CLOSED_MEM_INSUFFICIENT:
9947 s = "Insufficient memory.";
9949 case XCB_CONN_CLOSED_REQ_LEN_EXCEED:
9950 s = "Request length was exceeded.";
9952 case XCB_CONN_CLOSED_PARSE_ERR:
9953 s = "Error parsing display string.";
9956 s = "Unknown error.";
9959 errx(errcode, "X CONNECTION ERROR: %s", s);
9962 errx(errcode, "X CONNECTION ERROR");
9970 const uint32_t val = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
9971 XCB_EVENT_MASK_ENTER_WINDOW;
9973 xcb_void_cookie_t wac;
9974 xcb_generic_error_t *error;
9976 /* this causes an error if some other window manager is running */
9977 num_screens = get_screen_count();
9978 for (i = 0; i < num_screens; i++) {
9979 if ((sc = get_screen(i)) == NULL)
9980 errx(1, "ERROR: can't get screen %d.", i);
9981 DNPRINTF(SWM_D_INIT, "enable_wm: screen %d, root: %#x\n",
9983 wac = xcb_change_window_attributes_checked(conn, sc->root,
9984 XCB_CW_EVENT_MASK, &val);
9985 if ((error = xcb_request_check(conn, wac))) {
9986 DNPRINTF(SWM_D_INIT, "enable_wm: error_code: %u\n",
9992 /* click to focus on empty region */
9993 xcb_grab_button(conn, 1, sc->root, BUTTONMASK,
9994 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC, XCB_WINDOW_NONE,
9995 XCB_CURSOR_NONE, XCB_BUTTON_INDEX_1, XCB_BUTTON_MASK_ANY);
10002 new_region(struct swm_screen *s, int x, int y, int w, int h)
10004 struct swm_region *r = NULL, *n;
10005 struct workspace *ws = NULL;
10009 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
10010 s->idx, w, h, x, y);
10012 /* remove any conflicting regions */
10013 n = TAILQ_FIRST(&s->rl);
10016 n = TAILQ_NEXT(r, entry);
10017 if (X(r) < (x + w) && (X(r) + WIDTH(r)) > x &&
10018 Y(r) < (y + h) && (Y(r) + HEIGHT(r)) > y) {
10019 if (r->ws->r != NULL)
10020 r->ws->old_r = r->ws->r;
10023 xcb_destroy_window(conn, r->id);
10024 TAILQ_REMOVE(&s->rl, r, entry);
10025 TAILQ_INSERT_TAIL(&s->orl, r, entry);
10029 /* search old regions for one to reuse */
10031 /* size + location match */
10032 TAILQ_FOREACH(r, &s->orl, entry)
10033 if (X(r) == x && Y(r) == y &&
10034 HEIGHT(r) == h && WIDTH(r) == w)
10038 TAILQ_FOREACH(r, &s->orl, entry)
10039 if (HEIGHT(r) == h && WIDTH(r) == w)
10043 TAILQ_REMOVE(&s->orl, r, entry);
10044 /* try to use old region's workspace */
10045 if (r->ws->r == NULL)
10048 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
10049 err(1, "new_region: calloc: failed to allocate memory "
10052 /* if we don't have a workspace already, find one */
10054 for (i = 0; i < workspace_limit; i++)
10055 if (s->ws[i].r == NULL) {
10062 errx(1, "new_region: no free workspaces");
10064 if (ws->state == SWM_WS_STATE_HIDDEN)
10065 ws->state = SWM_WS_STATE_MAPPING;
10073 r->ws_prior = NULL;
10076 TAILQ_INSERT_TAIL(&s->rl, r, entry);
10078 /* Invisible region window to detect pointer events on empty regions. */
10079 r->id = xcb_generate_id(conn);
10080 wa[0] = XCB_EVENT_MASK_POINTER_MOTION |
10081 XCB_EVENT_MASK_POINTER_MOTION_HINT;
10083 xcb_create_window(conn, XCB_COPY_FROM_PARENT, r->id, r->s->root,
10084 X(r), Y(r), WIDTH(r), HEIGHT(r), 0, XCB_WINDOW_CLASS_INPUT_ONLY,
10085 XCB_COPY_FROM_PARENT, XCB_CW_EVENT_MASK, wa);
10087 /* Make sure region input is at the bottom. */
10088 wa[0] = XCB_STACK_MODE_BELOW;
10089 xcb_configure_window(conn, r->id, XCB_CONFIG_WINDOW_STACK_MODE, wa);
10091 xcb_map_window(conn, r->id);
10097 #ifdef SWM_XRR_HAS_CRTC
10100 #endif /* SWM_XRR_HAS_CRTC */
10101 struct swm_region *r;
10102 struct ws_win *win;
10104 xcb_randr_get_screen_resources_current_cookie_t src;
10105 xcb_randr_get_screen_resources_current_reply_t *srr;
10106 xcb_randr_get_crtc_info_cookie_t cic;
10107 xcb_randr_get_crtc_info_reply_t *cir = NULL;
10108 xcb_randr_crtc_t *crtc;
10109 xcb_screen_t *screen;
10111 DNPRINTF(SWM_D_MISC, "scan_xrandr: screen: %d\n", i);
10113 if ((screen = get_screen(i)) == NULL)
10114 errx(1, "ERROR: can't get screen %d.", i);
10116 num_screens = get_screen_count();
10117 if (i >= num_screens)
10118 errx(1, "scan_xrandr: invalid screen");
10120 /* remove any old regions */
10121 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
10122 r->ws->old_r = r->ws->r = NULL;
10124 xcb_destroy_window(conn, r->id);
10125 TAILQ_REMOVE(&screens[i].rl, r, entry);
10126 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
10130 /* map virtual screens onto physical screens */
10131 #ifdef SWM_XRR_HAS_CRTC
10132 if (xrandr_support) {
10133 src = xcb_randr_get_screen_resources_current(conn,
10135 srr = xcb_randr_get_screen_resources_current_reply(conn, src,
10138 new_region(&screens[i], 0, 0,
10139 screen->width_in_pixels,
10140 screen->height_in_pixels);
10143 ncrtc = srr->num_crtcs;
10145 crtc = xcb_randr_get_screen_resources_current_crtcs(srr);
10146 for (c = 0; c < ncrtc; c++) {
10147 cic = xcb_randr_get_crtc_info(conn, crtc[c],
10149 cir = xcb_randr_get_crtc_info_reply(conn, cic, NULL);
10152 if (cir->num_outputs == 0) {
10157 if (cir->mode == 0)
10158 new_region(&screens[i], 0, 0,
10159 screen->width_in_pixels,
10160 screen->height_in_pixels);
10162 new_region(&screens[i],
10163 cir->x, cir->y, cir->width, cir->height);
10168 #endif /* SWM_XRR_HAS_CRTC */
10170 /* If detection failed, create a single region that spans the screen. */
10171 if (TAILQ_EMPTY(&screens[i].rl))
10172 new_region(&screens[i], 0, 0, screen->width_in_pixels,
10173 screen->height_in_pixels);
10176 /* Cleanup unused previously visible workspaces. */
10177 TAILQ_FOREACH(r, &screens[i].orl, entry) {
10178 TAILQ_FOREACH(win, &r->ws->winlist, entry)
10180 r->ws->state = SWM_WS_STATE_HIDDEN;
10182 /* The screen shouldn't focus on an unused region. */
10183 if (screens[i].r_focus == r)
10184 screens[i].r_focus = NULL;
10187 DNPRINTF(SWM_D_MISC, "scan_xrandr: done.\n");
10191 screenchange(xcb_randr_screen_change_notify_event_t *e)
10193 struct swm_region *r;
10194 int i, num_screens;
10196 DNPRINTF(SWM_D_EVENT, "screenchange: root: %#x\n", e->root);
10198 num_screens = get_screen_count();
10199 /* silly event doesn't include the screen index */
10200 for (i = 0; i < num_screens; i++)
10201 if (screens[i].root == e->root)
10203 if (i >= num_screens)
10204 errx(1, "screenchange: screen not found");
10206 /* brute force for now, just re-enumerate the regions */
10210 print_win_geom(e->root);
10212 /* add bars to all regions */
10213 for (i = 0; i < num_screens; i++) {
10214 TAILQ_FOREACH(r, &screens[i].rl, entry)
10220 /* Make sure a region has focus on each screen. */
10221 for (i = 0; i < num_screens; i++) {
10222 if (screens[i].r_focus == NULL) {
10223 r = TAILQ_FIRST(&screens[i].rl);
10232 /* Update workspace state on all regions. */
10233 for (i = 0; i < num_screens; i++)
10234 TAILQ_FOREACH(r, &screens[i].rl, entry)
10235 r->ws->state = SWM_WS_STATE_MAPPED;
10241 struct swm_region *r = NULL;
10242 xcb_window_t *wins = NULL, trans, *cwins = NULL;
10243 int i, j, k, n, no, num_screens, manage, mapped;
10246 xcb_query_tree_cookie_t qtc;
10247 xcb_query_tree_reply_t *qtr;
10248 xcb_get_window_attributes_cookie_t gac;
10249 xcb_get_window_attributes_reply_t *gar;
10250 xcb_get_property_cookie_t pc;
10251 xcb_get_property_reply_t *pr;
10253 DNPRINTF(SWM_D_INIT, "grab_windows: begin\n");
10254 num_screens = get_screen_count();
10255 for (i = 0; i < num_screens; i++) {
10256 qtc = xcb_query_tree(conn, screens[i].root);
10257 qtr = xcb_query_tree_reply(conn, qtc, NULL);
10260 wins = xcb_query_tree_children(qtr);
10261 no = xcb_query_tree_children_length(qtr);
10263 /* Try to sort windows according to _NET_CLIENT_LIST. */
10264 pr = xcb_get_property_reply(conn, xcb_get_property(conn, 0,
10265 screens[i].root, ewmh[_NET_CLIENT_LIST].atom,
10266 XCB_ATOM_WINDOW, 0, UINT32_MAX), NULL);
10268 cwins = xcb_get_property_value(pr);
10269 n = xcb_get_property_value_length(pr) /
10270 sizeof(xcb_atom_t);
10272 for (j = 0; j < n; ++j) {
10273 for (k = j; k < no; ++k) {
10274 if (wins[k] == cwins[j]) {
10275 /* Swap wins j and k. */
10277 wins[j] = cwins[j];
10285 /* attach windows to a region */
10286 /* normal windows */
10287 DNPRINTF(SWM_D_INIT, "grab_windows: grab top level windows.\n");
10288 for (j = 0; j < no; j++) {
10289 TAILQ_FOREACH(r, &screens[i].rl, entry) {
10290 if (r->id == wins[j]) {
10291 DNPRINTF(SWM_D_INIT, "grab_windows: "
10292 "skip %#x; region input window.\n",
10295 } else if (r->bar->id == wins[j]) {
10296 DNPRINTF(SWM_D_INIT, "grab_windows: "
10297 "skip %#x; region bar.\n",
10306 gac = xcb_get_window_attributes(conn, wins[j]);
10307 gar = xcb_get_window_attributes_reply(conn, gac, NULL);
10309 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
10310 "doesn't exist.\n", wins[j]);
10314 if (gar->override_redirect) {
10315 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
10316 "override_redirect set.\n", wins[j]);
10321 pc = xcb_icccm_get_wm_transient_for(conn, wins[j]);
10322 if (xcb_icccm_get_wm_transient_for_reply(conn, pc,
10324 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
10325 "is transient for %#x.\n", wins[j], trans);
10330 state = get_win_state(wins[j]);
10331 manage = state != XCB_ICCCM_WM_STATE_WITHDRAWN;
10332 mapped = gar->map_state == XCB_MAP_STATE_VIEWABLE;
10333 if (mapped || manage)
10334 manage_window(wins[j], mapped);
10337 /* transient windows */
10338 DNPRINTF(SWM_D_INIT, "grab_windows: grab transient windows.\n");
10339 for (j = 0; j < no; j++) {
10340 gac = xcb_get_window_attributes(conn, wins[j]);
10341 gar = xcb_get_window_attributes_reply(conn, gac, NULL);
10343 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
10344 "doesn't exist.\n", wins[j]);
10348 if (gar->override_redirect) {
10349 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
10350 "override_redirect set.\n", wins[j]);
10355 state = get_win_state(wins[j]);
10356 manage = state != XCB_ICCCM_WM_STATE_WITHDRAWN;
10357 mapped = gar->map_state == XCB_MAP_STATE_VIEWABLE;
10358 pc = xcb_icccm_get_wm_transient_for(conn, wins[j]);
10359 if (xcb_icccm_get_wm_transient_for_reply(conn, pc,
10360 &trans, NULL) && manage)
10361 manage_window(wins[j], mapped);
10366 DNPRINTF(SWM_D_INIT, "grab_windows: done.\n");
10370 setup_screens(void)
10372 int i, j, k, num_screens;
10373 struct workspace *ws;
10374 uint32_t gcv[1], wa[1];
10375 const xcb_query_extension_reply_t *qep;
10376 xcb_screen_t *screen;
10377 xcb_randr_query_version_cookie_t c;
10378 xcb_randr_query_version_reply_t *r;
10380 num_screens = get_screen_count();
10381 if ((screens = calloc(num_screens,
10382 sizeof(struct swm_screen))) == NULL)
10383 err(1, "setup_screens: calloc: failed to allocate memory for "
10386 /* initial Xrandr setup */
10387 xrandr_support = 0;
10388 qep = xcb_get_extension_data(conn, &xcb_randr_id);
10389 if (qep->present) {
10390 c = xcb_randr_query_version(conn, 1, 1);
10391 r = xcb_randr_query_version_reply(conn, c, NULL);
10393 if (r->major_version >= 1) {
10394 xrandr_support = 1;
10395 xrandr_eventbase = qep->first_event;
10401 wa[0] = cursors[XC_LEFT_PTR].cid;
10403 /* map physical screens */
10404 for (i = 0; i < num_screens; i++) {
10405 DNPRINTF(SWM_D_WS, "setup_screens: init screen: %d\n", i);
10406 screens[i].idx = i;
10407 screens[i].r_focus = NULL;
10409 TAILQ_INIT(&screens[i].rl);
10410 TAILQ_INIT(&screens[i].orl);
10411 if ((screen = get_screen(i)) == NULL)
10412 errx(1, "ERROR: can't get screen %d.", i);
10413 screens[i].root = screen->root;
10415 /* set default colors */
10416 setscreencolor("red", i, SWM_S_COLOR_FOCUS);
10417 setscreencolor("rgb:88/88/88", i, SWM_S_COLOR_UNFOCUS);
10418 setscreencolor("rgb:00/80/80", i, SWM_S_COLOR_BAR_BORDER);
10419 setscreencolor("rgb:00/40/40", i,
10420 SWM_S_COLOR_BAR_BORDER_UNFOCUS);
10421 setscreencolor("black", i, SWM_S_COLOR_BAR);
10422 setscreencolor("rgb:a0/a0/a0", i, SWM_S_COLOR_BAR_FONT);
10423 setscreencolor("red", i, SWM_S_COLOR_FOCUS_MAXIMIZED);
10424 setscreencolor("rgb:88/88/88", i,
10425 SWM_S_COLOR_UNFOCUS_MAXIMIZED);
10427 /* create graphics context on screen */
10428 screens[i].bar_gc = xcb_generate_id(conn);
10430 xcb_create_gc(conn, screens[i].bar_gc, screens[i].root,
10431 XCB_GC_GRAPHICS_EXPOSURES, gcv);
10433 /* set default cursor */
10434 xcb_change_window_attributes(conn, screens[i].root,
10435 XCB_CW_CURSOR, wa);
10437 /* init all workspaces */
10438 /* XXX these should be dynamically allocated too */
10439 for (j = 0; j < SWM_WS_MAX; j++) {
10440 ws = &screens[i].ws[j];
10443 ws->bar_enabled = 1;
10445 ws->focus_prev = NULL;
10446 ws->focus_pending = NULL;
10449 ws->state = SWM_WS_STATE_HIDDEN;
10450 TAILQ_INIT(&ws->stack);
10451 TAILQ_INIT(&ws->winlist);
10452 TAILQ_INIT(&ws->unmanagedlist);
10454 for (k = 0; layouts[k].l_stack != NULL; k++)
10455 if (layouts[k].l_config != NULL)
10456 layouts[k].l_config(ws,
10457 SWM_ARG_ID_STACKINIT);
10458 ws->cur_layout = &layouts[0];
10459 ws->cur_layout->l_string(ws);
10464 if (xrandr_support)
10465 xcb_randr_select_input(conn, screens[i].root,
10466 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE);
10471 setup_globals(void)
10473 if ((bar_fonts = strdup(SWM_BAR_FONTS)) == NULL)
10474 err(1, "setup_globals: strdup: failed to allocate memory.");
10476 if ((clock_format = strdup("%a %b %d %R %Z %Y")) == NULL)
10477 err(1, "setup_globals: strdup: failed to allocate memory.");
10479 if ((syms = xcb_key_symbols_alloc(conn)) == NULL)
10480 errx(1, "unable to allocate key symbols");
10482 a_state = get_atom_from_string("WM_STATE");
10483 a_prot = get_atom_from_string("WM_PROTOCOLS");
10484 a_delete = get_atom_from_string("WM_DELETE_WINDOW");
10485 a_net_supported = get_atom_from_string("_NET_SUPPORTED");
10486 a_net_wm_check = get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
10487 a_takefocus = get_atom_from_string("WM_TAKE_FOCUS");
10488 a_utf8_string = get_atom_from_string("UTF8_STRING");
10489 a_swm_ws = get_atom_from_string("_SWM_WS");
10493 shutdown_cleanup(void)
10495 int i, num_screens;
10497 /* disable alarm because the following code may not be interrupted */
10499 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
10500 err(1, "can't disable alarm");
10509 num_screens = get_screen_count();
10510 for (i = 0; i < num_screens; ++i) {
10511 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT,
10512 screens[i].root, XCB_CURRENT_TIME);
10514 if (screens[i].bar_gc != XCB_NONE)
10515 xcb_free_gc(conn, screens[i].bar_gc);
10516 if (!bar_font_legacy) {
10517 XftColorFree(display, DefaultVisual(display, i),
10518 DefaultColormap(display, i), &bar_font_color);
10519 XftColorFree(display, DefaultVisual(display, i),
10520 DefaultColormap(display, i), &search_font_color);
10524 if (bar_font_legacy)
10525 XFreeFontSet(display, bar_fs);
10527 XftFontClose(display, bar_font);
10530 xcb_key_symbols_free(syms);
10532 xcb_disconnect(conn);
10536 event_error(xcb_generic_error_t *e)
10540 DNPRINTF(SWM_D_EVENT, "event_error: %s(%u) from %s(%u), sequence: %u, "
10541 "resource_id: %u, minor_code: %u\n",
10542 xcb_event_get_error_label(e->error_code), e->error_code,
10543 xcb_event_get_request_label(e->major_code), e->major_code,
10544 e->sequence, e->resource_id, e->minor_code);
10548 event_handle(xcb_generic_event_t *evt)
10550 uint8_t type = XCB_EVENT_RESPONSE_TYPE(evt);
10552 DNPRINTF(SWM_D_EVENT, "XCB Event: %s(%d), seq %u\n",
10553 xcb_event_get_label(XCB_EVENT_RESPONSE_TYPE(evt)),
10554 XCB_EVENT_RESPONSE_TYPE(evt), evt->sequence);
10557 #define EVENT(type, callback) case type: callback((void *)evt); return
10558 EVENT(0, event_error);
10559 EVENT(XCB_BUTTON_PRESS, buttonpress);
10560 /*EVENT(XCB_BUTTON_RELEASE, buttonpress);*/
10561 /*EVENT(XCB_CIRCULATE_NOTIFY, );*/
10562 /*EVENT(XCB_CIRCULATE_REQUEST, );*/
10563 EVENT(XCB_CLIENT_MESSAGE, clientmessage);
10564 /*EVENT(XCB_COLORMAP_NOTIFY, );*/
10565 EVENT(XCB_CONFIGURE_NOTIFY, configurenotify);
10566 EVENT(XCB_CONFIGURE_REQUEST, configurerequest);
10567 /*EVENT(XCB_CREATE_NOTIFY, );*/
10568 EVENT(XCB_DESTROY_NOTIFY, destroynotify);
10569 EVENT(XCB_ENTER_NOTIFY, enternotify);
10570 EVENT(XCB_EXPOSE, expose);
10572 EVENT(XCB_FOCUS_IN, focusin);
10573 EVENT(XCB_FOCUS_OUT, focusout);
10575 /*EVENT(XCB_GRAPHICS_EXPOSURE, );*/
10576 /*EVENT(XCB_GRAVITY_NOTIFY, );*/
10577 EVENT(XCB_KEY_PRESS, keypress);
10578 /*EVENT(XCB_KEY_RELEASE, keypress);*/
10579 /*EVENT(XCB_KEYMAP_NOTIFY, );*/
10581 EVENT(XCB_LEAVE_NOTIFY, leavenotify);
10583 EVENT(XCB_MAP_NOTIFY, mapnotify);
10584 EVENT(XCB_MAP_REQUEST, maprequest);
10585 EVENT(XCB_MAPPING_NOTIFY, mappingnotify);
10586 EVENT(XCB_MOTION_NOTIFY, motionnotify);
10587 /*EVENT(XCB_NO_EXPOSURE, );*/
10588 EVENT(XCB_PROPERTY_NOTIFY, propertynotify);
10589 /*EVENT(XCB_REPARENT_NOTIFY, );*/
10590 /*EVENT(XCB_RESIZE_REQUEST, );*/
10591 /*EVENT(XCB_SELECTION_CLEAR, );*/
10592 /*EVENT(XCB_SELECTION_NOTIFY, );*/
10593 /*EVENT(XCB_SELECTION_REQUEST, );*/
10594 EVENT(XCB_UNMAP_NOTIFY, unmapnotify);
10595 /*EVENT(XCB_VISIBILITY_NOTIFY, );*/
10598 if (type - xrandr_eventbase == XCB_RANDR_SCREEN_CHANGE_NOTIFY)
10599 screenchange((void *)evt);
10603 main(int argc, char *argv[])
10605 struct swm_region *r;
10606 char conf[PATH_MAX], *cfile = NULL;
10608 int xfd, i, num_screens, startup = 1;
10609 struct sigaction sact;
10610 xcb_generic_event_t *evt;
10614 int stdin_ready = 0;
10617 /* suppress unused warning since var is needed */
10620 time_started = time(NULL);
10623 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION, buildstr);
10624 if (setlocale(LC_CTYPE, "") == NULL || setlocale(LC_TIME, "") == NULL)
10625 warnx("no locale support");
10627 /* handle some signals */
10628 bzero(&sact, sizeof(sact));
10629 sigemptyset(&sact.sa_mask);
10631 sact.sa_handler = sighdlr;
10632 sigaction(SIGINT, &sact, NULL);
10633 sigaction(SIGQUIT, &sact, NULL);
10634 sigaction(SIGTERM, &sact, NULL);
10635 sigaction(SIGHUP, &sact, NULL);
10637 sact.sa_handler = sighdlr;
10638 sact.sa_flags = SA_NOCLDSTOP;
10639 sigaction(SIGCHLD, &sact, NULL);
10641 if ((display = XOpenDisplay(0)) == NULL)
10642 errx(1, "can not open display");
10644 conn = XGetXCBConnection(display);
10645 if (xcb_connection_has_error(conn))
10646 errx(1, "can not get XCB connection");
10648 XSetEventQueueOwner(display, XCBOwnsEventQueue);
10650 xcb_prefetch_extension_data(conn, &xcb_randr_id);
10651 xfd = xcb_get_file_descriptor(conn);
10653 /* look for local and global conf file */
10654 pwd = getpwuid(getuid());
10656 errx(1, "invalid user: %d", getuid());
10658 xcb_grab_server(conn);
10659 xcb_aux_sync(conn);
10661 /* flush all events */
10662 while ((evt = xcb_poll_for_event(conn))) {
10663 if (XCB_EVENT_RESPONSE_TYPE(evt) == 0)
10669 errx(1, "another window manager is currently running");
10671 /* Load Xcursors and/or cursorfont glyph cursors. */
10674 xcb_aux_sync(conn);
10684 for (i = 0; ; i++) {
10689 snprintf(conf, sizeof conf, "%s/.%s",
10690 pwd->pw_dir, SWM_CONF_FILE);
10694 snprintf(conf, sizeof conf, "/etc/%s",
10699 snprintf(conf, sizeof conf, "%s/.%s",
10700 pwd->pw_dir, SWM_CONF_FILE_OLD);
10703 /* global compat */
10704 snprintf(conf, sizeof conf, "/etc/%s",
10705 SWM_CONF_FILE_OLD);
10711 if (strlen(conf) && stat(conf, &sb) != -1)
10712 if (S_ISREG(sb.st_mode)) {
10719 /* load conf (if any) */
10721 conf_load(cfile, SWM_CONF_DEFAULT);
10725 if (getenv("SWM_STARTED") == NULL)
10726 setenv("SWM_STARTED", "YES", 1);
10728 /* setup all bars */
10729 num_screens = get_screen_count();
10730 for (i = 0; i < num_screens; i++)
10731 TAILQ_FOREACH(r, &screens[i].rl, entry)
10734 /* Manage existing windows. */
10741 xcb_ungrab_server(conn);
10744 /* Update state of each newly mapped workspace. */
10745 for (i = 0; i < num_screens; i++)
10746 TAILQ_FOREACH(r, &screens[i].rl, entry)
10747 r->ws->state = SWM_WS_STATE_MAPPED;
10749 rd_max = xfd > STDIN_FILENO ? xfd : STDIN_FILENO;
10752 while ((evt = xcb_poll_for_event(conn))) {
10759 /* If just (re)started, set default focus if needed. */
10763 if (focus_mode != SWM_FOCUS_FOLLOW) {
10764 r = TAILQ_FIRST(&screens[0].rl);
10776 FD_SET(STDIN_FILENO, &rd);
10781 num_readable = select(rd_max + 1, &rd, NULL, NULL, &tv);
10782 if (num_readable == -1 && errno != EINTR) {
10783 DNPRINTF(SWM_D_MISC, "select failed");
10784 } else if (num_readable > 0 && FD_ISSET(STDIN_FILENO, &rd)) {
10789 restart(NULL, NULL);
10799 bar_extra_update();
10806 shutdown_cleanup();