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. */
675 TAILQ_HEAD(quirk_list
, quirk
);
676 struct quirk_list quirks
= TAILQ_HEAD_INITIALIZER(quirks
);
679 * Supported EWMH hints should be added to
680 * both the enum and the ewmh array
686 _NET_CURRENT_DESKTOP
,
687 _NET_DESKTOP_GEOMETRY
,
689 _NET_DESKTOP_VIEWPORT
,
690 _NET_MOVERESIZE_WINDOW
,
691 _NET_NUMBER_OF_DESKTOPS
,
693 _NET_WM_ACTION_ABOVE
,
694 _NET_WM_ACTION_CLOSE
,
695 _NET_WM_ACTION_FULLSCREEN
,
697 _NET_WM_ACTION_RESIZE
,
698 _NET_WM_ALLOWED_ACTIONS
,
700 _NET_WM_FULL_PLACEMENT
,
704 _NET_WM_STATE_FULLSCREEN
,
705 _NET_WM_STATE_HIDDEN
,
706 _NET_WM_STATE_MAXIMIZED_VERT
,
707 _NET_WM_STATE_MAXIMIZED_HORZ
,
708 _NET_WM_STATE_SKIP_PAGER
,
709 _NET_WM_STATE_SKIP_TASKBAR
,
711 _NET_WM_WINDOW_TYPE_DIALOG
,
712 _NET_WM_WINDOW_TYPE_DOCK
,
713 _NET_WM_WINDOW_TYPE_NORMAL
,
714 _NET_WM_WINDOW_TYPE_SPLASH
,
715 _NET_WM_WINDOW_TYPE_TOOLBAR
,
716 _NET_WM_WINDOW_TYPE_UTILITY
,
717 _SWM_WM_STATE_MANUAL
,
724 } ewmh
[SWM_EWMH_HINT_MAX
] = {
725 /* must be in same order as in the enum */
726 {"_NET_ACTIVE_WINDOW", XCB_ATOM_NONE
},
727 {"_NET_CLIENT_LIST", XCB_ATOM_NONE
},
728 {"_NET_CLOSE_WINDOW", XCB_ATOM_NONE
},
729 {"_NET_CURRENT_DESKTOP", XCB_ATOM_NONE
},
730 {"_NET_DESKTOP_GEOMETRY", XCB_ATOM_NONE
},
731 {"_NET_DESKTOP_NAMES", XCB_ATOM_NONE
},
732 {"_NET_DESKTOP_VIEWPORT", XCB_ATOM_NONE
},
733 {"_NET_MOVERESIZE_WINDOW", XCB_ATOM_NONE
},
734 {"_NET_NUMBER_OF_DESKTOPS", XCB_ATOM_NONE
},
735 {"_NET_RESTACK_WINDOW", XCB_ATOM_NONE
},
736 {"_NET_WM_ACTION_ABOVE", XCB_ATOM_NONE
},
737 {"_NET_WM_ACTION_CLOSE", XCB_ATOM_NONE
},
738 {"_NET_WM_ACTION_FULLSCREEN", XCB_ATOM_NONE
},
739 {"_NET_WM_ACTION_MOVE", XCB_ATOM_NONE
},
740 {"_NET_WM_ACTION_RESIZE", XCB_ATOM_NONE
},
741 {"_NET_WM_ALLOWED_ACTIONS", XCB_ATOM_NONE
},
742 {"_NET_WM_DESKTOP", XCB_ATOM_NONE
},
743 {"_NET_WM_FULL_PLACEMENT", XCB_ATOM_NONE
},
744 {"_NET_WM_NAME", XCB_ATOM_NONE
},
745 {"_NET_WM_STATE", XCB_ATOM_NONE
},
746 {"_NET_WM_STATE_ABOVE", XCB_ATOM_NONE
},
747 {"_NET_WM_STATE_FULLSCREEN", XCB_ATOM_NONE
},
748 {"_NET_WM_STATE_HIDDEN", XCB_ATOM_NONE
},
749 {"_NET_WM_STATE_MAXIMIZED_VERT", XCB_ATOM_NONE
},
750 {"_NET_WM_STATE_MAXIMIZED_HORZ", XCB_ATOM_NONE
},
751 {"_NET_WM_STATE_SKIP_PAGER", XCB_ATOM_NONE
},
752 {"_NET_WM_STATE_SKIP_TASKBAR", XCB_ATOM_NONE
},
753 {"_NET_WM_WINDOW_TYPE", XCB_ATOM_NONE
},
754 {"_NET_WM_WINDOW_TYPE_DIALOG", XCB_ATOM_NONE
},
755 {"_NET_WM_WINDOW_TYPE_DOCK", XCB_ATOM_NONE
},
756 {"_NET_WM_WINDOW_TYPE_NORMAL", XCB_ATOM_NONE
},
757 {"_NET_WM_WINDOW_TYPE_SPLASH", XCB_ATOM_NONE
},
758 {"_NET_WM_WINDOW_TYPE_TOOLBAR", XCB_ATOM_NONE
},
759 {"_NET_WM_WINDOW_TYPE_UTILITY", XCB_ATOM_NONE
},
760 {"_SWM_WM_STATE_MANUAL", XCB_ATOM_NONE
},
763 /* EWMH source type */
765 EWMH_SOURCE_TYPE_NONE
= 0,
766 EWMH_SOURCE_TYPE_NORMAL
= 1,
767 EWMH_SOURCE_TYPE_OTHER
= 2,
774 XC_BOTTOM_LEFT_CORNER
,
775 XC_BOTTOM_RIGHT_CORNER
,
783 char *name
; /* Name used by Xcursor .*/
784 uint8_t cf_char
; /* cursorfont index. */
786 } cursors
[XC_MAX
] = {
787 {"fleur", XC_fleur
, XCB_CURSOR_NONE
},
788 {"left_ptr", XC_left_ptr
, XCB_CURSOR_NONE
},
789 {"bottom_left_corner", XC_bottom_left_corner
, XCB_CURSOR_NONE
},
790 {"bottom_right_corner", XC_bottom_right_corner
, XCB_CURSOR_NONE
},
791 {"sizing", XC_sizing
, XCB_CURSOR_NONE
},
792 {"top_left_corner", XC_top_left_corner
, XCB_CURSOR_NONE
},
793 {"top_right_corner", XC_top_right_corner
, XCB_CURSOR_NONE
},
796 #define SWM_SPAWN_OPTIONAL 0x1
800 TAILQ_ENTRY(spawn_prog
) entry
;
806 TAILQ_HEAD(spawn_list
, spawn_prog
);
807 struct spawn_list spawns
= TAILQ_HEAD_INITIALIZER(spawns
);
809 /* user/key callable function IDs */
925 KF_DUMPWINS
, /* MUST BE LAST */
933 enum keyfuncid funcid
;
936 RB_HEAD(key_tree
, key
);
938 /* function prototypes */
939 void adjust_font(struct ws_win
*);
940 char *argsep(char **);
941 void bar_cleanup(struct swm_region
*);
942 void bar_extra_setup(void);
943 void bar_extra_stop(void);
944 int bar_extra_update(void);
945 void bar_fmt(const char *, char *, struct swm_region
*, size_t);
946 void bar_fmt_expand(char *, size_t);
948 void bar_print(struct swm_region
*, const char *);
949 void bar_print_legacy(struct swm_region
*, const char *);
950 void bar_replace(char *, char *, struct swm_region
*, size_t);
951 void bar_replace_pad(char *, int *, size_t);
952 char *bar_replace_seq(char *, char *, struct swm_region
*, size_t *, size_t);
953 void bar_setup(struct swm_region
*);
954 void bar_toggle(struct swm_region
*, union arg
*);
955 void bar_urgent(char *, size_t);
956 void bar_window_class(char *, size_t, struct swm_region
*);
957 void bar_window_class_instance(char *, size_t, struct swm_region
*);
958 void bar_window_float(char *, size_t, struct swm_region
*);
959 void bar_window_instance(char *, size_t, struct swm_region
*);
960 void bar_window_name(char *, size_t, struct swm_region
*);
961 void bar_window_state(char *, size_t, struct swm_region
*);
962 void bar_workspace_name(char *, size_t, struct swm_region
*);
963 void buttonpress(xcb_button_press_event_t
*);
964 void check_conn(void);
965 void clear_keys(void);
966 int clear_maximized(struct workspace
*);
967 void clientmessage(xcb_client_message_event_t
*);
968 void client_msg(struct ws_win
*, xcb_atom_t
, xcb_timestamp_t
);
969 int conf_load(const char *, int);
970 void configurenotify(xcb_configure_notify_event_t
*);
971 void configurerequest(xcb_configure_request_event_t
*);
972 void config_win(struct ws_win
*, xcb_configure_request_event_t
*);
973 void constrain_window(struct ws_win
*, struct swm_geometry
*, int *);
974 int count_win(struct workspace
*, int);
975 void cursors_cleanup(void);
976 void cursors_load(void);
977 void custom_region(const char *);
978 void cyclerg(struct swm_region
*, union arg
*);
979 void cyclews(struct swm_region
*, union arg
*);
980 void cycle_layout(struct swm_region
*, union arg
*);
981 void destroynotify(xcb_destroy_notify_event_t
*);
982 void dumpwins(struct swm_region
*, union arg
*);
984 void enternotify(xcb_enter_notify_event_t
*);
985 void event_drain(uint8_t);
986 void event_error(xcb_generic_error_t
*);
987 void event_handle(xcb_generic_event_t
*);
988 void ewmh_apply_flags(struct ws_win
*, uint32_t);
989 void ewmh_autoquirk(struct ws_win
*);
990 void ewmh_get_desktop_names(void);
991 void ewmh_get_wm_state(struct ws_win
*);
992 void ewmh_update_actions(struct ws_win
*);
993 void ewmh_update_client_list(void);
994 void ewmh_update_current_desktop(void);
995 void ewmh_update_desktop_names(void);
996 void ewmh_update_desktops(void);
997 void ewmh_change_wm_state(struct ws_win
*, xcb_atom_t
, long);
998 void ewmh_update_wm_state(struct ws_win
*);
999 char *expand_tilde(const char *);
1000 void expose(xcb_expose_event_t
*);
1001 void fake_keypress(struct ws_win
*, xcb_keysym_t
, uint16_t);
1002 struct pid_e
*find_pid(pid_t
);
1003 struct ws_win
*find_unmanaged_window(xcb_window_t
);
1004 struct ws_win
*find_window(xcb_window_t
);
1005 void floating_toggle(struct swm_region
*, union arg
*);
1006 void focus(struct swm_region
*, union arg
*);
1008 void focusin(xcb_focus_in_event_t
*);
1009 void focusout(xcb_focus_out_event_t
*);
1011 void focus_flush(void);
1012 void focus_region(struct swm_region
*);
1013 void focusrg(struct swm_region
*, union arg
*);
1014 void focus_win(struct ws_win
*);
1015 void fontset_init(void);
1016 void free_window(struct ws_win
*);
1017 xcb_atom_t
get_atom_from_string(const char *);
1019 char *get_atom_name(xcb_atom_t
);
1021 struct ws_win
*get_focus_magic(struct ws_win
*);
1022 struct ws_win
*get_focus_prev(struct ws_win
*);
1024 char *get_notify_detail_label(uint8_t);
1025 char *get_notify_mode_label(uint8_t);
1027 struct ws_win
*get_pointer_win(xcb_window_t
);
1028 struct ws_win
*get_region_focus(struct swm_region
*);
1029 int get_region_index(struct swm_region
*);
1030 xcb_screen_t
*get_screen(int);
1031 int get_screen_count(void);
1033 char *get_source_type_label(uint32_t);
1034 char *get_stack_mode_name(uint8_t);
1036 int32_t get_swm_ws(xcb_window_t
);
1037 char *get_win_name(xcb_window_t
);
1038 uint8_t get_win_state(xcb_window_t
);
1039 void get_wm_protocols(struct ws_win
*);
1040 int get_ws_idx(xcb_window_t
);
1041 void grabbuttons(struct ws_win
*);
1042 void grabkeys(void);
1043 void grab_windows(void);
1044 void iconify(struct swm_region
*, union arg
*);
1046 void keypress(xcb_key_press_event_t
*);
1047 int key_cmp(struct key
*, struct key
*);
1048 void key_insert(unsigned int, KeySym
, enum keyfuncid
, const char *);
1049 struct key
*key_lookup(unsigned int, KeySym
);
1050 void key_remove(struct key
*);
1051 void key_replace(struct key
*, unsigned int, KeySym
, enum keyfuncid
,
1053 void kill_bar_extra_atexit(void);
1054 void kill_refs(struct ws_win
*);
1056 void leavenotify(xcb_leave_notify_event_t
*);
1058 void load_float_geom(struct ws_win
*);
1059 struct ws_win
*manage_window(xcb_window_t
, int);
1060 void map_window(struct ws_win
*);
1061 void mapnotify(xcb_map_notify_event_t
*);
1062 void mappingnotify(xcb_mapping_notify_event_t
*);
1063 void maprequest(xcb_map_request_event_t
*);
1064 void maximize_toggle(struct swm_region
*, union arg
*);
1065 void motionnotify(xcb_motion_notify_event_t
*);
1066 void move(struct ws_win
*, union arg
*);
1067 void move_step(struct swm_region
*, union arg
*);
1068 uint32_t name_to_pixel(int, const char *);
1069 void name_workspace(struct swm_region
*, union arg
*);
1070 void new_region(struct swm_screen
*, int, int, int, int);
1071 int parsekeys(const char *, unsigned int, unsigned int *, KeySym
*);
1072 int parsequirks(const char *, unsigned long *);
1073 int parse_rgb(const char *, uint16_t *, uint16_t *, uint16_t *);
1074 void pressbutton(struct swm_region
*, union arg
*);
1075 void priorws(struct swm_region
*, union arg
*);
1077 void print_win_geom(xcb_window_t
);
1079 void propertynotify(xcb_property_notify_event_t
*);
1080 void quirk_free(struct quirk
*);
1081 void quirk_insert(const char *, const char *, const char *,unsigned long);
1082 void quirk_remove(struct quirk
*);
1083 void quirk_replace(struct quirk
*, const char *, const char *, const char *,
1085 void quit(struct swm_region
*, union arg
*);
1086 void raise_toggle(struct swm_region
*, union arg
*);
1087 void raise_window(struct ws_win
*);
1088 void region_containment(struct ws_win
*, struct swm_region
*, int);
1089 struct swm_region
*region_under(struct swm_screen
*, int, int);
1090 void regionize(struct ws_win
*, int, int);
1091 void resize(struct ws_win
*, union arg
*);
1092 void resize_step(struct swm_region
*, union arg
*);
1093 void restart(struct swm_region
*, union arg
*);
1094 struct swm_region
*root_to_region(xcb_window_t
, int);
1095 void screenchange(xcb_randr_screen_change_notify_event_t
*);
1096 void scan_xrandr(int);
1097 void search_do_resp(void);
1098 void search_resp_name_workspace(const char *, size_t);
1099 void search_resp_search_window(const char *);
1100 void search_resp_search_workspace(const char *);
1101 void search_resp_uniconify(const char *, size_t);
1102 void search_win(struct swm_region
*, union arg
*);
1103 void search_win_cleanup(void);
1104 void search_workspace(struct swm_region
*, union arg
*);
1105 void send_to_rg(struct swm_region
*, union arg
*);
1106 void send_to_ws(struct swm_region
*, union arg
*);
1107 void set_region(struct swm_region
*);
1108 int setautorun(const char *, const char *, int);
1109 int setconfbinding(const char *, const char *, int);
1110 int setconfcolor(const char *, const char *, int);
1111 int setconfmodkey(const char *, const char *, int);
1112 int setconfquirk(const char *, const char *, int);
1113 int setconfregion(const char *, const char *, int);
1114 int setconfspawn(const char *, const char *, int);
1115 int setconfvalue(const char *, const char *, int);
1116 void setkeybinding(unsigned int, KeySym
, enum keyfuncid
, const char *);
1117 int setkeymapping(const char *, const char *, int);
1118 int setlayout(const char *, const char *, int);
1119 void setquirk(const char *, const char *, const char *,unsigned long);
1120 void setscreencolor(const char *, int, int);
1121 void setspawn(const char *, const char *, int);
1122 void setup_ewmh(void);
1123 void setup_globals(void);
1124 void setup_keys(void);
1125 void setup_quirks(void);
1126 void setup_screens(void);
1127 void setup_spawn(void);
1128 void set_child_transient(struct ws_win
*, xcb_window_t
*);
1129 void set_win_state(struct ws_win
*, uint8_t);
1130 void shutdown_cleanup(void);
1132 void socket_setnonblock(int);
1133 void sort_windows(struct ws_win_list
*);
1134 void spawn(int, union arg
*, int);
1135 void spawn_custom(struct swm_region
*, union arg
*, const char *);
1136 int spawn_expand(struct swm_region
*, union arg
*, const char *, char ***);
1137 void spawn_insert(const char *, const char *, int);
1138 struct spawn_prog
*spawn_find(const char *);
1139 void spawn_remove(struct spawn_prog
*);
1140 void spawn_replace(struct spawn_prog
*, const char *, const char *, int);
1141 void spawn_select(struct swm_region
*, union arg
*, const char *, int *);
1142 void stack_config(struct swm_region
*, union arg
*);
1143 void stack_master(struct workspace
*, struct swm_geometry
*, int, int);
1144 void store_float_geom(struct ws_win
*);
1145 char *strdupsafe(const char *);
1146 void swapwin(struct swm_region
*, union arg
*);
1147 void switchws(struct swm_region
*, union arg
*);
1148 void teardown_ewmh(void);
1149 void unescape_selector(char *);
1150 void unfocus_win(struct ws_win
*);
1151 void uniconify(struct swm_region
*, union arg
*);
1152 void unmanage_window(struct ws_win
*);
1153 void unmapnotify(xcb_unmap_notify_event_t
*);
1154 void unmap_all(void);
1155 void unmap_window(struct ws_win
*);
1156 void updatenumlockmask(void);
1157 void update_floater(struct ws_win
*);
1158 void update_modkey(unsigned int);
1159 void update_win_stacking(struct ws_win
*);
1160 void update_window(struct ws_win
*);
1161 void update_window_color(struct ws_win
*);
1162 void update_wm_state(struct ws_win
*win
);
1163 void validate_spawns(void);
1164 int validate_win(struct ws_win
*);
1165 int validate_ws(struct workspace
*);
1166 void version(struct swm_region
*, union arg
*);
1167 void win_to_ws(struct ws_win
*, int, int);
1168 pid_t
window_get_pid(xcb_window_t
);
1169 void wkill(struct swm_region
*, union arg
*);
1170 void update_ws_stack(struct workspace
*);
1171 void xft_init(struct swm_region
*);
1172 void _add_startup_exception(const char *, va_list);
1173 void add_startup_exception(const char *, ...);
1175 RB_PROTOTYPE(key_tree
, key
, entry
, key_cmp
);
1176 RB_GENERATE(key_tree
, key
, entry
, key_cmp
);
1177 struct key_tree keys
;
1182 xcb_font_t cf
= XCB_NONE
;
1185 for (i
= 0; i
< LENGTH(cursors
); ++i
) {
1186 /* try to load Xcursor first. */
1187 cursors
[i
].cid
= XcursorLibraryLoadCursor(display
,
1190 /* fallback to cursorfont. */
1191 if (cursors
[i
].cid
== XCB_CURSOR_NONE
) {
1192 if (cf
== XCB_NONE
) {
1193 cf
= xcb_generate_id(conn
);
1194 xcb_open_font(conn
, cf
, strlen("cursor"),
1198 cursors
[i
].cid
= xcb_generate_id(conn
);
1199 xcb_create_glyph_cursor(conn
, cursors
[i
].cid
, cf
, cf
,
1200 cursors
[i
].cf_char
, cursors
[i
].cf_char
+ 1, 0, 0, 0,
1201 0xffff, 0xffff, 0xffff);
1207 xcb_close_font(conn
, cf
);
1211 cursors_cleanup(void)
1214 for (i
= 0; i
< LENGTH(cursors
); ++i
)
1215 xcb_free_cursor(conn
, cursors
[i
].cid
);
1219 expand_tilde(const char *s
)
1221 struct passwd
*ppwd
;
1229 errx(1, "expand_tilde: NULL string.");
1232 result
= strdup(sc
);
1238 if ((max
= sysconf(_SC_LOGIN_NAME_MAX
)) == -1)
1239 errx(1, "expand_tilde: sysconf");
1241 if ((user
= calloc(1, max
+ 1)) == NULL
)
1242 errx(1, "expand_tilde: calloc");
1244 for (i
= 0; s
[i
] != '/' && s
[i
] != '\0'; ++i
)
1249 ppwd
= strlen(user
) == 0 ? getpwuid(getuid()) : getpwnam(user
);
1253 result
= strdup(sc
);
1255 if (asprintf(&result
, "%s%s", ppwd
->pw_dir
, s
) == -1)
1259 errx(1, "expand_tilde: failed to allocate memory.");
1265 parse_rgb(const char *rgb
, uint16_t *rr
, uint16_t *gg
, uint16_t *bb
)
1267 unsigned int tmpr
, tmpg
, tmpb
;
1269 if (sscanf(rgb
, "rgb:%x/%x/%x", &tmpr
, &tmpg
, &tmpb
) != 3)
1272 *rr
= RGB_8_TO_16(tmpr
);
1273 *gg
= RGB_8_TO_16(tmpg
);
1274 *bb
= RGB_8_TO_16(tmpb
);
1280 get_screen(int screen
)
1282 const xcb_setup_t
*r
;
1283 xcb_screen_iterator_t iter
;
1285 if ((r
= xcb_get_setup(conn
)) == NULL
) {
1286 DNPRINTF(SWM_D_MISC
, "get_screen: xcb_get_setup\n");
1290 iter
= xcb_setup_roots_iterator(r
);
1291 for (; iter
.rem
; --screen
, xcb_screen_next(&iter
))
1299 get_screen_count(void)
1301 const xcb_setup_t
*r
;
1303 if ((r
= xcb_get_setup(conn
)) == NULL
) {
1304 DNPRINTF(SWM_D_MISC
, "get_screen_count: xcb_get_setup\n");
1308 return xcb_setup_roots_length(r
);
1312 get_region_index(struct swm_region
*r
)
1314 struct swm_region
*rr
;
1320 TAILQ_FOREACH(rr
, &r
->s
->rl
, entry
) {
1335 if (focus_mode
== SWM_FOCUS_DEFAULT
)
1336 event_drain(XCB_ENTER_NOTIFY
);
1342 get_atom_from_string(const char *str
)
1344 xcb_intern_atom_cookie_t c
;
1345 xcb_intern_atom_reply_t
*r
;
1348 c
= xcb_intern_atom(conn
, 0, strlen(str
), str
);
1349 r
= xcb_intern_atom_reply(conn
, c
, NULL
);
1357 return (XCB_ATOM_NONE
);
1361 get_wm_protocols(struct ws_win
*win
) {
1363 xcb_icccm_get_wm_protocols_reply_t wpr
;
1365 if (xcb_icccm_get_wm_protocols_reply(conn
,
1366 xcb_icccm_get_wm_protocols(conn
, win
->id
, a_prot
),
1368 for (i
= 0; i
< (int)wpr
.atoms_len
; i
++) {
1369 if (wpr
.atoms
[i
] == a_takefocus
)
1370 win
->take_focus
= 1;
1371 if (wpr
.atoms
[i
] == a_delete
)
1372 win
->can_delete
= 1;
1374 xcb_icccm_get_wm_protocols_reply_wipe(&wpr
);
1381 xcb_window_t root
, win
;
1382 int i
, j
, num_screens
;
1384 for (i
= 0; i
< LENGTH(ewmh
); i
++)
1385 ewmh
[i
].atom
= get_atom_from_string(ewmh
[i
].name
);
1387 num_screens
= get_screen_count();
1388 for (i
= 0; i
< num_screens
; i
++) {
1389 root
= screens
[i
].root
;
1391 /* Set up _NET_SUPPORTING_WM_CHECK. */
1392 win
= xcb_generate_id(conn
);
1393 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, win
, root
,
1394 0, 0, 1, 1, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
1395 XCB_COPY_FROM_PARENT
, 0, NULL
);
1397 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, root
,
1398 a_net_wm_check
, XCB_ATOM_WINDOW
, 32, 1, &win
);
1399 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
1400 a_net_wm_check
, XCB_ATOM_WINDOW
, 32, 1, &win
);
1403 * Impersonate LG3D non-reparenting WM, written by Sun, to
1404 * workaround a Java GUI rendering issue.
1406 if (java_workaround
)
1407 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
1408 ewmh
[_NET_WM_NAME
].atom
, a_utf8_string
,
1409 8, strlen("LG3D"), "LG3D");
1411 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
1412 ewmh
[_NET_WM_NAME
].atom
, a_utf8_string
,
1413 8, strlen("spectrwm"), "spectrwm");
1415 /* Report supported atoms */
1416 xcb_delete_property(conn
, root
, a_net_supported
);
1417 for (j
= 0; j
< LENGTH(ewmh
); j
++)
1418 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, root
,
1419 a_net_supported
, XCB_ATOM_ATOM
, 32, 1,
1424 ewmh_update_desktops();
1425 ewmh_get_desktop_names();
1433 xcb_get_property_cookie_t pc
;
1434 xcb_get_property_reply_t
*pr
;
1436 num_screens
= get_screen_count();
1438 for (i
= 0; i
< num_screens
; i
++) {
1439 /* Get the support check window and destroy it */
1440 pc
= xcb_get_property(conn
, 0, screens
[i
].root
, a_net_wm_check
,
1441 XCB_ATOM_WINDOW
, 0, 1);
1442 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
1445 if (pr
->format
== a_net_wm_check
) {
1446 id
= *((xcb_window_t
*)xcb_get_property_value(pr
));
1448 xcb_destroy_window(conn
, id
);
1449 xcb_delete_property(conn
, screens
[i
].root
,
1451 xcb_delete_property(conn
, screens
[i
].root
,
1459 ewmh_autoquirk(struct ws_win
*win
)
1461 xcb_get_property_reply_t
*r
;
1462 xcb_get_property_cookie_t c
;
1466 c
= xcb_get_property(conn
, 0, win
->id
,
1467 ewmh
[_NET_WM_WINDOW_TYPE
].atom
, XCB_ATOM_ATOM
, 0, UINT32_MAX
);
1468 r
= xcb_get_property_reply(conn
, c
, NULL
);
1472 type
= xcb_get_property_value(r
);
1473 n
= xcb_get_property_value_length(r
) / sizeof(xcb_atom_t
);
1475 for (i
= 0; i
< n
; i
++) {
1476 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_NORMAL
].atom
)
1478 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_DOCK
].atom
||
1479 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_TOOLBAR
].atom
||
1480 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_UTILITY
].atom
) {
1481 win
->quirks
= SWM_Q_FLOAT
| SWM_Q_ANYWHERE
;
1484 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_SPLASH
].atom
||
1485 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_DIALOG
].atom
) {
1486 win
->quirks
= SWM_Q_FLOAT
;
1494 ewmh_update_actions(struct ws_win
*win
)
1496 xcb_atom_t actions
[SWM_EWMH_ACTION_COUNT_MAX
];
1502 actions
[n
++] = ewmh
[_NET_WM_ACTION_CLOSE
].atom
;
1505 actions
[n
++] = ewmh
[_NET_WM_ACTION_MOVE
].atom
;
1506 actions
[n
++] = ewmh
[_NET_WM_ACTION_RESIZE
].atom
;
1507 actions
[n
++] = ewmh
[_NET_WM_ACTION_ABOVE
].atom
;
1510 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
1511 ewmh
[_NET_WM_ALLOWED_ACTIONS
].atom
, XCB_ATOM_ATOM
, 32, 1, actions
);
1514 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
1515 #define _NET_WM_STATE_ADD 1 /* add/set property */
1516 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
1519 ewmh_change_wm_state(struct ws_win
*win
, xcb_atom_t state
, long action
)
1526 name
= get_atom_name(state
);
1527 DNPRINTF(SWM_D_PROP
, "ewmh_change_wm_state: win %#x, state: %s, "
1528 "action: %ld\n", WINID(win
), name
, action
);
1534 if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
)
1535 flag
= EWMH_F_FULLSCREEN
;
1536 else if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
)
1537 flag
= EWMH_F_ABOVE
;
1538 else if (state
== ewmh
[_NET_WM_STATE_HIDDEN
].atom
)
1539 flag
= EWMH_F_HIDDEN
;
1540 else if (state
== ewmh
[_NET_WM_STATE_MAXIMIZED_VERT
].atom
||
1541 state
== ewmh
[_NET_WM_STATE_MAXIMIZED_HORZ
].atom
)
1542 flag
= EWMH_F_MAXIMIZED
;
1543 else if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
)
1544 flag
= SWM_F_MANUAL
;
1545 else if (state
== ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
)
1546 flag
= EWMH_F_SKIP_PAGER
;
1547 else if (state
== ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
)
1548 flag
= EWMH_F_SKIP_TASKBAR
;
1550 /* Disallow unfloating transients. */
1551 if (TRANS(win
) && flag
== EWMH_F_ABOVE
)
1554 new_flags
= win
->ewmh_flags
;
1557 case _NET_WM_STATE_REMOVE
:
1560 case _NET_WM_STATE_ADD
:
1563 case _NET_WM_STATE_TOGGLE
:
1568 ewmh_apply_flags(win
, new_flags
);
1571 DNPRINTF(SWM_D_PROP
, "ewmh_change_wm_state: done.\n");
1575 ewmh_apply_flags(struct ws_win
*win
, uint32_t pending
)
1577 struct workspace
*ws
;
1580 changed
= win
->ewmh_flags
^ pending
;
1584 DNPRINTF(SWM_D_PROP
, "ewmh_apply_flags: pending: %d\n", pending
);
1586 win
->ewmh_flags
= pending
;
1589 if (changed
& EWMH_F_HIDDEN
) {
1591 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
1592 ws
->focus_pending
= get_focus_prev(win
);
1597 /* Reload floating geometry in case region changed. */
1599 load_float_geom(win
);
1601 /* The window is no longer iconic, prepare focus. */
1602 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
1603 ws
->focus_pending
= get_focus_magic(win
);
1608 if (changed
& EWMH_F_ABOVE
) {
1609 if (ws
->cur_layout
!= &layouts
[SWM_MAX_STACK
]) {
1611 load_float_geom(win
);
1612 else if (!MAXIMIZED(win
))
1613 store_float_geom(win
);
1615 win
->ewmh_flags
&= ~EWMH_F_MAXIMIZED
;
1616 changed
&= ~EWMH_F_MAXIMIZED
;
1620 win
->ewmh_flags
^= EWMH_F_ABOVE
& pending
;
1624 if (changed
& EWMH_F_MAXIMIZED
) {
1625 /* VERT and/or HORZ changed. */
1627 if (!MAXIMIZED(win
))
1628 load_float_geom(win
);
1630 store_float_geom(win
);
1633 if (MAXIMIZED(win
)) {
1634 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
1635 ws
->cur_layout
!= &layouts
[SWM_MAX_STACK
]) {
1639 ws
->focus_pending
= win
;
1643 update_window_color(win
);
1647 if (changed
& EWMH_F_FULLSCREEN
) {
1648 if (FULLSCREEN(win
)) {
1649 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
1653 ws
->focus_pending
= win
;
1656 load_float_geom(win
);
1659 win
->ewmh_flags
&= ~EWMH_F_MAXIMIZED
;
1663 DNPRINTF(SWM_D_PROP
, "ewmh_apply_flags: done.\n");
1667 ewmh_update_wm_state(struct ws_win
*win
) {
1668 xcb_atom_t vals
[SWM_EWMH_ACTION_COUNT_MAX
];
1672 vals
[n
++] = ewmh
[_NET_WM_STATE_HIDDEN
].atom
;
1673 if (FULLSCREEN(win
))
1674 vals
[n
++] = ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
;
1675 if (MAXIMIZED_VERT(win
))
1676 vals
[n
++] = ewmh
[_NET_WM_STATE_MAXIMIZED_VERT
].atom
;
1677 if (MAXIMIZED_HORZ(win
))
1678 vals
[n
++] = ewmh
[_NET_WM_STATE_MAXIMIZED_HORZ
].atom
;
1679 if (win
->ewmh_flags
& EWMH_F_SKIP_PAGER
)
1680 vals
[n
++] = ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
;
1681 if (win
->ewmh_flags
& EWMH_F_SKIP_TASKBAR
)
1682 vals
[n
++] = ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
;
1683 if (win
->ewmh_flags
& EWMH_F_ABOVE
)
1684 vals
[n
++] = ewmh
[_NET_WM_STATE_ABOVE
].atom
;
1685 if (win
->ewmh_flags
& SWM_F_MANUAL
)
1686 vals
[n
++] = ewmh
[_SWM_WM_STATE_MANUAL
].atom
;
1689 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
1690 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, n
, vals
);
1692 xcb_delete_property(conn
, win
->id
, ewmh
[_NET_WM_STATE
].atom
);
1696 ewmh_get_wm_state(struct ws_win
*win
)
1699 xcb_get_property_cookie_t c
;
1700 xcb_get_property_reply_t
*r
;
1706 win
->ewmh_flags
= 0;
1708 c
= xcb_get_property(conn
, 0, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
1709 XCB_ATOM_ATOM
, 0, UINT32_MAX
);
1710 r
= xcb_get_property_reply(conn
, c
, NULL
);
1714 states
= xcb_get_property_value(r
);
1715 n
= xcb_get_property_value_length(r
) / sizeof(xcb_atom_t
);
1717 for (i
= 0; i
< n
; i
++)
1718 ewmh_change_wm_state(win
, states
[i
], _NET_WM_STATE_ADD
);
1726 dumpwins(struct swm_region
*r
, union arg
*args
)
1730 xcb_get_window_attributes_cookie_t c
;
1731 xcb_get_window_attributes_reply_t
*wa
;
1733 /* suppress unused warning since var is needed */
1736 if (r
->ws
== NULL
) {
1737 DPRINTF("dumpwins: invalid workspace\n");
1741 DPRINTF("=== managed window list ws %02d ===\n", r
->ws
->idx
);
1742 TAILQ_FOREACH(w
, &r
->ws
->winlist
, entry
) {
1743 state
= get_win_state(w
->id
);
1744 c
= xcb_get_window_attributes(conn
, w
->id
);
1745 wa
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
1747 DPRINTF("win %#x, map_state: %d, state: %u, "
1748 "transient: %#x\n", w
->id
, wa
->map_state
,
1749 state
, w
->transient
);
1752 DPRINTF("win %#x, failed xcb_get_window_attributes\n",
1756 DPRINTF("=== stacking order (top down) === \n");
1757 TAILQ_FOREACH(w
, &r
->ws
->stack
, stack_entry
) {
1758 DPRINTF("win %#x, fs: %s, maximized: %s, above: %s, "
1759 "iconic: %s\n", w
->id
, YESNO(FULLSCREEN(w
)),
1760 YESNO(MAXIMIZED(w
)), YESNO(ABOVE(w
)), YESNO(ICONIC(w
)));
1763 DPRINTF("===== unmanaged window list =====\n");
1764 TAILQ_FOREACH(w
, &r
->ws
->unmanagedlist
, entry
) {
1765 state
= get_win_state(w
->id
);
1766 c
= xcb_get_window_attributes(conn
, w
->id
);
1767 wa
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
1769 DPRINTF("win %#x, map_state: %d, state: %u, "
1770 "transient: %#x\n", w
->id
, wa
->map_state
,
1771 state
, w
->transient
);
1774 DPRINTF("win %#x, failed xcb_get_window_attributes\n",
1778 DPRINTF("=================================\n");
1782 dumpwins(struct swm_region
*r
, union arg
*s
)
1787 #endif /* SWM_DEBUG */
1792 int saved_errno
, status
;
1795 saved_errno
= errno
;
1799 while ((pid
= waitpid(WAIT_ANY
, &status
, WNOHANG
)) != 0) {
1804 if (errno
!= ECHILD
)
1805 warn("sighdlr: waitpid");
1806 #endif /* SWM_DEBUG */
1809 if (pid
== searchpid
)
1813 if (WIFEXITED(status
)) {
1814 if (WEXITSTATUS(status
) != 0)
1815 warnx("sighdlr: child exit status: %d",
1816 WEXITSTATUS(status
));
1818 warnx("sighdlr: child is terminated "
1820 #endif /* SWM_DEBUG */
1834 errno
= saved_errno
;
1840 struct pid_e
*p
= NULL
;
1842 DNPRINTF(SWM_D_MISC
, "find_pid: %d\n", pid
);
1847 TAILQ_FOREACH(p
, &pidlist
, entry
) {
1856 name_to_pixel(int sidx
, const char *colorname
)
1858 uint32_t result
= 0;
1859 char cname
[32] = "#";
1860 xcb_screen_t
*screen
;
1861 xcb_colormap_t cmap
;
1862 xcb_alloc_color_reply_t
*cr
;
1863 xcb_alloc_named_color_reply_t
*nr
;
1864 uint16_t rr
, gg
, bb
;
1866 screen
= get_screen(sidx
);
1867 cmap
= screen
->default_colormap
;
1869 /* color is in format rgb://rr/gg/bb */
1870 if (strncmp(colorname
, "rgb:", 4) == 0) {
1871 if (parse_rgb(colorname
, &rr
, &gg
, &bb
) == -1)
1872 warnx("could not parse rgb %s", colorname
);
1874 cr
= xcb_alloc_color_reply(conn
,
1875 xcb_alloc_color(conn
, cmap
, rr
, gg
, bb
),
1881 warnx("color '%s' not found", colorname
);
1884 nr
= xcb_alloc_named_color_reply(conn
,
1885 xcb_alloc_named_color(conn
, cmap
, strlen(colorname
),
1888 strlcat(cname
, colorname
+ 2, sizeof cname
- 1);
1889 nr
= xcb_alloc_named_color_reply(conn
,
1890 xcb_alloc_named_color(conn
, cmap
, strlen(cname
),
1897 warnx("color '%s' not found", colorname
);
1904 setscreencolor(const char *val
, int i
, int c
)
1906 if (i
< 0 || i
>= get_screen_count())
1909 screens
[i
].c
[c
].pixel
= name_to_pixel(i
, val
);
1910 free(screens
[i
].c
[c
].name
);
1911 if ((screens
[i
].c
[c
].name
= strdup(val
)) == NULL
)
1916 fancy_stacker(struct workspace
*ws
)
1918 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1919 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1920 snprintf(ws
->stacker
, sizeof ws
->stacker
,
1921 ws
->l_state
.vertical_flip
? "[%d>%d]" : "[%d|%d]",
1922 ws
->l_state
.vertical_mwin
, ws
->l_state
.vertical_stacks
);
1923 else if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1924 snprintf(ws
->stacker
, sizeof ws
->stacker
,
1925 ws
->l_state
.horizontal_flip
? "[%dv%d]" : "[%d-%d]",
1926 ws
->l_state
.horizontal_mwin
, ws
->l_state
.horizontal_stacks
);
1930 plain_stacker(struct workspace
*ws
)
1932 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1933 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1934 strlcpy(ws
->stacker
, ws
->l_state
.vertical_flip
? "[>]" : "[|]",
1935 sizeof ws
->stacker
);
1936 else if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1937 strlcpy(ws
->stacker
, ws
->l_state
.horizontal_flip
? "[v]" : "[-]",
1938 sizeof ws
->stacker
);
1942 custom_region(const char *val
)
1944 unsigned int x
, y
, w
, h
;
1945 int sidx
, num_screens
;
1946 xcb_screen_t
*screen
;
1948 num_screens
= get_screen_count();
1949 if (sscanf(val
, "screen[%d]:%ux%u+%u+%u", &sidx
, &w
, &h
, &x
, &y
) != 5)
1950 errx(1, "invalid custom region, "
1951 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1952 if (sidx
< 1 || sidx
> num_screens
)
1953 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1957 if ((screen
= get_screen(sidx
)) == NULL
)
1958 errx(1, "ERROR: can't get screen %d.", sidx
);
1961 errx(1, "region %ux%u+%u+%u too small", w
, h
, x
, y
);
1963 if (x
> screen
->width_in_pixels
||
1964 y
> screen
->height_in_pixels
||
1965 w
+ x
> screen
->width_in_pixels
||
1966 h
+ y
> screen
->height_in_pixels
) {
1967 warnx("ignoring region %ux%u+%u+%u - not within screen "
1968 "boundaries (%ux%u)", w
, h
, x
, y
,
1969 screen
->width_in_pixels
, screen
->height_in_pixels
);
1973 new_region(&screens
[sidx
], x
, y
, w
, h
);
1977 socket_setnonblock(int fd
)
1981 if ((flags
= fcntl(fd
, F_GETFL
, 0)) == -1)
1982 err(1, "fcntl F_GETFL");
1983 flags
|= O_NONBLOCK
;
1984 if ((flags
= fcntl(fd
, F_SETFL
, flags
)) == -1)
1985 err(1, "fcntl F_SETFL");
1989 bar_print_legacy(struct swm_region
*r
, const char *s
)
1991 xcb_rectangle_t rect
;
1996 XRectangle ibox
, lbox
;
2000 XmbTextExtents(bar_fs
, s
, len
, &ibox
, &lbox
);
2002 switch (bar_justify
) {
2003 case SWM_BAR_JUSTIFY_LEFT
:
2006 case SWM_BAR_JUSTIFY_CENTER
:
2007 x
= (WIDTH(r
) - lbox
.width
) / 2;
2009 case SWM_BAR_JUSTIFY_RIGHT
:
2010 x
= WIDTH(r
) - lbox
.width
- SWM_BAR_OFFSET
;
2014 if (x
< SWM_BAR_OFFSET
)
2019 rect
.width
= WIDTH(r
->bar
);
2020 rect
.height
= HEIGHT(r
->bar
);
2022 /* clear back buffer */
2023 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
2024 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
2025 xcb_poly_fill_rectangle(conn
, r
->bar
->buffer
, r
->s
->bar_gc
, 1, &rect
);
2027 /* draw back buffer */
2028 gcvd
.graphics_exposures
= 0;
2029 draw
= XCreateGC(display
, r
->bar
->buffer
, GCGraphicsExposures
, &gcvd
);
2030 XSetForeground(display
, draw
, r
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
);
2031 DRAWSTRING(display
, r
->bar
->buffer
, bar_fs
, draw
,
2032 x
, (bar_fs_extents
->max_logical_extent
.height
- lbox
.height
) / 2 -
2034 XFreeGC(display
, draw
);
2037 xcb_copy_area(conn
, r
->bar
->buffer
, r
->bar
->id
, r
->s
->bar_gc
, 0, 0,
2038 0, 0, WIDTH(r
->bar
), HEIGHT(r
->bar
));
2042 bar_print(struct swm_region
*r
, const char *s
)
2045 xcb_rectangle_t rect
;
2053 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
, &info
);
2055 switch (bar_justify
) {
2056 case SWM_BAR_JUSTIFY_LEFT
:
2059 case SWM_BAR_JUSTIFY_CENTER
:
2060 x
= (WIDTH(r
) - info
.width
) / 2;
2062 case SWM_BAR_JUSTIFY_RIGHT
:
2063 x
= WIDTH(r
) - info
.width
- SWM_BAR_OFFSET
;
2067 if (x
< SWM_BAR_OFFSET
)
2072 rect
.width
= WIDTH(r
->bar
);
2073 rect
.height
= HEIGHT(r
->bar
);
2075 /* clear back buffer */
2076 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
2077 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
2078 xcb_poly_fill_rectangle(conn
, r
->bar
->buffer
, r
->s
->bar_gc
, 1, &rect
);
2080 /* draw back buffer */
2081 draw
= XftDrawCreate(display
, r
->bar
->buffer
,
2082 DefaultVisual(display
, r
->s
->idx
),
2083 DefaultColormap(display
, r
->s
->idx
));
2085 XftDrawStringUtf8(draw
, &bar_font_color
, bar_font
, x
,
2086 (HEIGHT(r
->bar
) + bar_font
->height
) / 2 - bar_font
->descent
,
2089 XftDrawDestroy(draw
);
2092 xcb_copy_area(conn
, r
->bar
->buffer
, r
->bar
->id
, r
->s
->bar_gc
, 0, 0,
2093 0, 0, WIDTH(r
->bar
), HEIGHT(r
->bar
));
2097 bar_extra_stop(void)
2101 bzero(bar_pipe
, sizeof bar_pipe
);
2104 kill(bar_pid
, SIGTERM
);
2107 strlcpy(bar_ext
, "", sizeof bar_ext
);
2112 bar_window_class(char *s
, size_t sz
, struct swm_region
*r
)
2114 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2116 if (r
->ws
->focus
->ch
.class_name
!= NULL
)
2117 strlcat(s
, r
->ws
->focus
->ch
.class_name
, sz
);
2121 bar_window_instance(char *s
, size_t sz
, struct swm_region
*r
)
2123 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2125 if (r
->ws
->focus
->ch
.instance_name
!= NULL
)
2126 strlcat(s
, r
->ws
->focus
->ch
.instance_name
, sz
);
2130 bar_window_class_instance(char *s
, size_t sz
, struct swm_region
*r
)
2132 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2135 bar_window_class(s
, sz
, r
);
2136 strlcat(s
, ":", sz
);
2137 bar_window_instance(s
, sz
, r
);
2141 bar_window_state(char *s
, size_t sz
, struct swm_region
*r
)
2143 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2145 if (MAXIMIZED(r
->ws
->focus
))
2146 strlcat(s
, "(m)", sz
);
2147 else if (ABOVE(r
->ws
->focus
))
2148 strlcat(s
, "(f)", sz
);
2152 bar_window_name(char *s
, size_t sz
, struct swm_region
*r
)
2156 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2159 title
= get_win_name(r
->ws
->focus
->id
);
2160 strlcat(s
, title
, sz
);
2165 bar_urgent(char *s
, size_t sz
)
2168 int i
, j
, num_screens
;
2169 int urgent
[SWM_WS_MAX
];
2171 xcb_get_property_cookie_t c
;
2172 xcb_icccm_wm_hints_t hints
;
2174 for (i
= 0; i
< workspace_limit
; i
++)
2177 num_screens
= get_screen_count();
2178 for (i
= 0; i
< num_screens
; i
++)
2179 for (j
= 0; j
< workspace_limit
; j
++)
2180 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
) {
2181 c
= xcb_icccm_get_wm_hints(conn
, win
->id
);
2182 if (xcb_icccm_get_wm_hints_reply(conn
, c
,
2185 if (hints
.flags
& XCB_ICCCM_WM_HINT_X_URGENCY
)
2189 for (i
= 0; i
< workspace_limit
; i
++) {
2191 snprintf(b
, sizeof b
, "%d ", i
+ 1);
2193 snprintf(b
, sizeof b
, "- ");
2199 bar_workspace_name(char *s
, size_t sz
, struct swm_region
*r
)
2201 if (r
== NULL
|| r
->ws
== NULL
)
2203 if (r
->ws
->name
!= NULL
)
2204 strlcat(s
, r
->ws
->name
, sz
);
2207 /* build the default bar format according to the defined enabled options */
2209 bar_fmt(const char *fmtexp
, char *fmtnew
, struct swm_region
*r
, size_t sz
)
2213 /* if format provided, just copy the buffers */
2214 if (bar_format
!= NULL
) {
2215 strlcpy(fmtnew
, fmtexp
, sz
);
2219 /* reset the output buffer */
2222 strlcat(fmtnew
, "+N:+I ", sz
);
2224 strlcat(fmtnew
, "+S", sz
);
2225 strlcat(fmtnew
, " ", sz
);
2227 /* only show the workspace name if there's actually one */
2228 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->name
!= NULL
)
2229 strlcat(fmtnew
, "<+D>", sz
);
2231 /* If enabled, only show the iconic count if there are iconic wins. */
2232 if (iconic_enabled
&& r
!= NULL
&& r
->ws
!= NULL
)
2233 TAILQ_FOREACH(w
, &r
->ws
->winlist
, entry
)
2235 strlcat(fmtnew
, "{+M}", sz
);
2239 strlcat(fmtnew
, "+3<", sz
);
2241 if (clock_enabled
) {
2242 strlcat(fmtnew
, fmtexp
, sz
);
2243 strlcat(fmtnew
, "+4<", sz
);
2246 /* bar_urgent already adds the space before the last asterisk */
2248 strlcat(fmtnew
, "* +U*+4<", sz
);
2250 if (window_class_enabled
) {
2251 strlcat(fmtnew
, "+C", sz
);
2252 if (!window_instance_enabled
)
2253 strlcat(fmtnew
, "+4<", sz
);
2256 /* checks needed by the colon and floating strlcat(3) calls below */
2257 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->focus
!= NULL
) {
2258 if (window_instance_enabled
) {
2259 if (window_class_enabled
)
2260 strlcat(fmtnew
, ":", sz
);
2261 strlcat(fmtnew
, "+T+4<", sz
);
2263 if (window_name_enabled
) {
2264 if (ABOVE(r
->ws
->focus
) || MAXIMIZED(r
->ws
->focus
))
2265 strlcat(fmtnew
, "+F ", sz
);
2266 strlcat(fmtnew
, "+64W ", sz
);
2270 /* finally add the action script output and the version */
2271 strlcat(fmtnew
, "+4<+A+4<+V", sz
);
2275 bar_replace_pad(char *tmp
, int *limit
, size_t sz
)
2277 /* special case; no limit given, pad one space, instead */
2278 if (*limit
== (int)sz
- 1)
2280 snprintf(tmp
, sz
, "%*s", *limit
, " ");
2283 /* replaces the bar format character sequences (like in tmux(1)) */
2285 bar_replace_seq(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t *offrep
,
2290 char tmp
[SWM_BAR_MAX
];
2291 int limit
, size
, count
;
2294 /* reset strlcat(3) buffer */
2297 /* get number, if any */
2300 if (sscanf(fmt
, "%d%n", &limit
, &size
) != 1)
2301 limit
= sizeof tmp
- 1;
2302 if (limit
<= 0 || limit
>= (int)sizeof tmp
)
2303 limit
= sizeof tmp
- 1;
2305 /* there is nothing to replace (ie EOL) */
2312 bar_replace_pad(tmp
, &limit
, sizeof tmp
);
2315 snprintf(tmp
, sizeof tmp
, "%s", bar_ext
);
2318 bar_window_class(tmp
, sizeof tmp
, r
);
2321 bar_workspace_name(tmp
, sizeof tmp
, r
);
2324 bar_window_state(tmp
, sizeof tmp
, r
);
2327 snprintf(tmp
, sizeof tmp
, "%d", r
->ws
->idx
+ 1);
2331 TAILQ_FOREACH(w
, &r
->ws
->winlist
, entry
)
2335 snprintf(tmp
, sizeof tmp
, "%d", count
);
2338 snprintf(tmp
, sizeof tmp
, "%d", r
->s
->idx
+ 1);
2341 bar_window_class_instance(tmp
, sizeof tmp
, r
);
2344 snprintf(tmp
, sizeof tmp
, "%s", r
->ws
->stacker
);
2347 bar_window_instance(tmp
, sizeof tmp
, r
);
2350 bar_urgent(tmp
, sizeof tmp
);
2353 snprintf(tmp
, sizeof tmp
, "%s", bar_vertext
);
2356 bar_window_name(tmp
, sizeof tmp
, r
);
2359 /* unknown character sequence; copy as-is */
2360 snprintf(tmp
, sizeof tmp
, "+%c", *fmt
);
2366 if ((int)len
< limit
)
2368 while (limit
-- > 0) {
2369 if (*offrep
>= sz
- 1)
2371 fmtrep
[(*offrep
)++] = *ptr
++;
2379 bar_replace(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t sz
)
2384 while (*fmt
!= '\0') {
2386 /* skip ordinary characters */
2389 fmtrep
[off
++] = *fmt
++;
2393 /* character sequence found; replace it */
2394 fmt
= bar_replace_seq(fmt
, fmtrep
, r
, &off
, sz
);
2403 bar_fmt_expand(char *fmtexp
, size_t sz
)
2410 /* start by grabbing the current time and date */
2412 localtime_r(&tmt
, &tm
);
2414 /* figure out what to expand */
2415 if (bar_format
!= NULL
)
2417 else if (bar_format
== NULL
&& clock_enabled
)
2419 /* if nothing to expand bail out */
2425 /* copy as-is, just in case the format shouldn't be expanded below */
2426 strlcpy(fmtexp
, fmt
, sz
);
2427 /* finally pass the string through strftime(3) */
2428 #ifndef SWM_DENY_CLOCK_FORMAT
2429 if ((len
= strftime(fmtexp
, sz
, fmt
, &tm
)) == 0)
2430 warnx("format too long");
2435 /* Redraws the bar; need to follow with xcb_flush() or focus_flush(). */
2439 char fmtexp
[SWM_BAR_MAX
], fmtnew
[SWM_BAR_MAX
];
2440 char fmtrep
[SWM_BAR_MAX
];
2442 struct swm_region
*r
;
2444 /* expand the format by first passing it through strftime(3) */
2445 bar_fmt_expand(fmtexp
, sizeof fmtexp
);
2447 num_screens
= get_screen_count();
2448 for (i
= 0; i
< num_screens
; i
++) {
2449 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
2453 if (bar_enabled
&& r
->ws
->bar_enabled
)
2454 xcb_map_window(conn
, r
->bar
->id
);
2456 xcb_unmap_window(conn
, r
->bar
->id
);
2460 if (startup_exception
)
2461 snprintf(fmtrep
, sizeof fmtrep
, "total "
2462 "exceptions: %d, first exception: %s",
2466 bar_fmt(fmtexp
, fmtnew
, r
, sizeof fmtnew
);
2467 bar_replace(fmtnew
, fmtrep
, r
, sizeof fmtrep
);
2469 if (bar_font_legacy
)
2470 bar_print_legacy(r
, fmtrep
);
2472 bar_print(r
, fmtrep
);
2478 * Reads external script output; call when stdin is readable.
2479 * Returns 1 if bar_ext was updated; otherwise 0.
2482 bar_extra_update(void)
2485 char b
[SWM_BAR_MAX
];
2491 while (fgets(b
, sizeof(b
), stdin
) != NULL
) {
2494 if (b
[len
- 1] == '\n') {
2495 /* Remove newline. */
2498 /* "Clear" bar_ext. */
2501 /* Flush buffered output. */
2502 strlcpy(bar_ext
, bar_ext_buf
, sizeof(bar_ext
));
2503 bar_ext_buf
[0] = '\0';
2505 /* Append new output to bar. */
2506 strlcat(bar_ext
, b
, sizeof(bar_ext
));
2510 /* Buffer output. */
2511 strlcat(bar_ext_buf
, b
, sizeof(bar_ext_buf
));
2516 if (errno
!= EAGAIN
) {
2517 warn("bar_action failed");
2526 bar_toggle(struct swm_region
*r
, union arg
*args
)
2528 struct swm_region
*tmpr
;
2531 /* suppress unused warnings since vars are needed */
2535 DNPRINTF(SWM_D_BAR
, "bar_toggle\n");
2538 case SWM_ARG_ID_BAR_TOGGLE_WS
:
2539 /* Only change if master switch is enabled. */
2541 r
->ws
->bar_enabled
= !r
->ws
->bar_enabled
;
2543 bar_enabled
= r
->ws
->bar_enabled
= 1;
2545 case SWM_ARG_ID_BAR_TOGGLE
:
2546 bar_enabled
= !bar_enabled
;
2550 /* update bars as necessary */
2551 num_screens
= get_screen_count();
2552 for (i
= 0; i
< num_screens
; i
++)
2553 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
2555 if (bar_enabled
&& tmpr
->ws
->bar_enabled
)
2556 xcb_map_window(conn
, tmpr
->bar
->id
);
2558 xcb_unmap_window(conn
, tmpr
->bar
->id
);
2563 /* must be after stack */
2570 bar_extra_setup(void)
2572 /* do this here because the conf file is in memory */
2573 if (!bar_extra
&& bar_argv
[0]) {
2574 /* launch external status app */
2576 if (pipe(bar_pipe
) == -1)
2577 err(1, "pipe error");
2578 socket_setnonblock(bar_pipe
[0]);
2579 socket_setnonblock(bar_pipe
[1]); /* XXX hmmm, really? */
2581 /* Set stdin to read from the pipe. */
2582 if (dup2(bar_pipe
[0], STDIN_FILENO
) == -1)
2585 /* Set stdout to write to the pipe. */
2586 if (dup2(bar_pipe
[1], STDOUT_FILENO
) == -1)
2589 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
2590 err(1, "could not disable SIGPIPE");
2591 switch (bar_pid
= fork()) {
2593 err(1, "cannot fork");
2597 execvp(bar_argv
[0], bar_argv
);
2598 err(1, "%s external app failed", bar_argv
[0]);
2600 default: /* parent */
2605 atexit(kill_bar_extra_atexit
);
2610 kill_bar_extra_atexit(void)
2613 kill(bar_pid
, SIGTERM
);
2621 while ((s
= index(s
, '-'))) {
2626 return (count
== 14);
2632 char *default_string
;
2633 char **missing_charsets
;
2634 int num_missing_charsets
= 0;
2638 XFreeFontSet(display
, bar_fs
);
2642 DNPRINTF(SWM_D_INIT
, "fontset_init: loading bar_fonts: %s\n", bar_fonts
);
2644 bar_fs
= XCreateFontSet(display
, bar_fonts
, &missing_charsets
,
2645 &num_missing_charsets
, &default_string
);
2647 if (num_missing_charsets
> 0) {
2648 warnx("Unable to load charset(s):");
2650 for (i
= 0; i
< num_missing_charsets
; ++i
)
2651 warnx("%s", missing_charsets
[i
]);
2653 XFreeStringList(missing_charsets
);
2655 if (strcmp(default_string
, ""))
2656 warnx("Glyphs from those sets will be replaced "
2657 "by '%s'.", default_string
);
2659 warnx("Glyphs from those sets won't be drawn.");
2663 errx(1, "Error creating font set structure.");
2665 bar_fs_extents
= XExtentsOfFontSet(bar_fs
);
2667 bar_height
= bar_fs_extents
->max_logical_extent
.height
+
2668 2 * bar_border_width
;
2675 xft_init(struct swm_region
*r
)
2677 char *font
, *d
, *search
;
2680 if (bar_font
== NULL
) {
2681 if ((d
= strdup(bar_fonts
)) == NULL
)
2682 errx(1, "insufficient memory.");
2684 while ((font
= strsep(&search
, ",")) != NULL
) {
2688 DNPRINTF(SWM_D_INIT
, "xft_init: try font %s\n", font
);
2691 bar_font
= XftFontOpenXlfd(display
, r
->s
->idx
,
2694 bar_font
= XftFontOpenName(display
, r
->s
->idx
,
2698 if (bar_font
== NULL
) {
2699 warnx("unable to load font %s", font
);
2702 DNPRINTF(SWM_D_INIT
, "successfully opened "
2710 if (bar_font
== NULL
)
2711 errx(1, "unable to open a font");
2713 PIXEL_TO_XRENDERCOLOR(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
, color
);
2715 if (!XftColorAllocValue(display
, DefaultVisual(display
, r
->s
->idx
),
2716 DefaultColormap(display
, r
->s
->idx
), &color
, &bar_font_color
))
2717 warn("Xft error: unable to allocate color.");
2719 PIXEL_TO_XRENDERCOLOR(r
->s
->c
[SWM_S_COLOR_BAR
].pixel
, color
);
2721 if (!XftColorAllocValue(display
, DefaultVisual(display
, r
->s
->idx
),
2722 DefaultColormap(display
, r
->s
->idx
), &color
, &search_font_color
))
2723 warn("Xft error: unable to allocate color.");
2725 bar_height
= bar_font
->height
+ 2 * bar_border_width
;
2732 bar_setup(struct swm_region
*r
)
2734 xcb_screen_t
*screen
;
2737 DNPRINTF(SWM_D_BAR
, "bar_setup: screen %d.\n",
2740 if ((screen
= get_screen(r
->s
->idx
)) == NULL
)
2741 errx(1, "ERROR: can't get screen %d.", r
->s
->idx
);
2746 if ((r
->bar
= calloc(1, sizeof(struct swm_bar
))) == NULL
)
2747 err(1, "bar_setup: calloc: failed to allocate memory.");
2749 if (bar_font_legacy
)
2755 Y(r
->bar
) = bar_at_bottom
? (Y(r
) + HEIGHT(r
) - bar_height
) : Y(r
);
2756 WIDTH(r
->bar
) = WIDTH(r
) - 2 * bar_border_width
;
2757 HEIGHT(r
->bar
) = bar_height
- 2 * bar_border_width
;
2759 /* Assume region is unfocused when we create the bar. */
2760 r
->bar
->id
= xcb_generate_id(conn
);
2761 wa
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
2762 wa
[1] = r
->s
->c
[SWM_S_COLOR_BAR_BORDER_UNFOCUS
].pixel
;
2763 wa
[2] = XCB_EVENT_MASK_EXPOSURE
| XCB_EVENT_MASK_POINTER_MOTION
|
2764 XCB_EVENT_MASK_POINTER_MOTION_HINT
;
2766 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, r
->bar
->id
, r
->s
->root
,
2767 X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
), HEIGHT(r
->bar
),
2768 bar_border_width
, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
2769 XCB_COPY_FROM_PARENT
, XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
2770 | XCB_CW_EVENT_MASK
, wa
);
2772 /* Stack bar window above region window to start. */
2774 wa
[1] = XCB_STACK_MODE_ABOVE
;
2776 xcb_configure_window(conn
, r
->bar
->id
, XCB_CONFIG_WINDOW_SIBLING
|
2777 XCB_CONFIG_WINDOW_STACK_MODE
, wa
);
2779 r
->bar
->buffer
= xcb_generate_id(conn
);
2780 xcb_create_pixmap(conn
, screen
->root_depth
, r
->bar
->buffer
, r
->bar
->id
,
2781 WIDTH(r
->bar
), HEIGHT(r
->bar
));
2784 xcb_randr_select_input(conn
, r
->bar
->id
,
2785 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE
);
2788 xcb_map_window(conn
, r
->bar
->id
);
2790 DNPRINTF(SWM_D_BAR
, "bar_setup: win %#x, (x,y) w x h: (%d,%d) "
2791 "%d x %d\n", WINID(r
->bar
), X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
),
2798 bar_cleanup(struct swm_region
*r
)
2802 xcb_destroy_window(conn
, r
->bar
->id
);
2803 xcb_free_pixmap(conn
, r
->bar
->buffer
);
2809 set_win_state(struct ws_win
*win
, uint8_t state
)
2811 uint16_t data
[2] = { state
, XCB_ATOM_NONE
};
2813 DNPRINTF(SWM_D_EVENT
, "set_win_state: win %#x, state: %u\n",
2819 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
, a_state
,
2820 a_state
, 32, 2, data
);
2824 get_win_state(xcb_window_t w
)
2826 xcb_get_property_reply_t
*r
;
2827 xcb_get_property_cookie_t c
;
2828 uint32_t result
= 0;
2830 c
= xcb_get_property(conn
, 0, w
, a_state
, a_state
, 0L, 2L);
2831 r
= xcb_get_property_reply(conn
, c
, NULL
);
2833 if (r
->type
== a_state
&& r
->format
== 32 && r
->length
== 2)
2834 result
= *((uint32_t *)xcb_get_property_value(r
));
2838 DNPRINTF(SWM_D_MISC
, "get_win_state property: win %#x state %u\n", w
,
2844 version(struct swm_region
*r
, union arg
*args
)
2846 /* suppress unused warnings since vars are needed */
2850 bar_version
= !bar_version
;
2852 snprintf(bar_vertext
, sizeof bar_vertext
,
2853 "Version: %s Build: %s", SPECTRWM_VERSION
, buildstr
);
2855 strlcpy(bar_vertext
, "", sizeof bar_vertext
);
2862 client_msg(struct ws_win
*win
, xcb_atom_t a
, xcb_timestamp_t t
)
2864 xcb_client_message_event_t ev
;
2872 name
= get_atom_name(a
);
2873 DNPRINTF(SWM_D_EVENT
, "client_msg: win %#x, atom: %s(%u), "
2875 win
->id
, name
, a
, t
);
2879 bzero(&ev
, sizeof ev
);
2880 ev
.response_type
= XCB_CLIENT_MESSAGE
;
2881 ev
.window
= win
->id
;
2884 ev
.data
.data32
[0] = a
;
2885 ev
.data
.data32
[1] = t
;
2887 xcb_send_event(conn
, 0, win
->id
,
2888 XCB_EVENT_MASK_NO_EVENT
, (const char *)&ev
);
2891 /* synthetic response to a ConfigureRequest when not making a change */
2893 config_win(struct ws_win
*win
, xcb_configure_request_event_t
*ev
)
2895 xcb_configure_notify_event_t ce
;
2900 /* send notification of unchanged state. */
2901 bzero(&ce
, sizeof(ce
));
2902 ce
.response_type
= XCB_CONFIGURE_NOTIFY
;
2905 ce
.width
= WIDTH(win
);
2906 ce
.height
= HEIGHT(win
);
2907 ce
.override_redirect
= 0;
2912 ce
.window
= win
->id
;
2913 ce
.border_width
= BORDER(win
);
2914 ce
.above_sibling
= XCB_WINDOW_NONE
;
2917 ce
.event
= ev
->window
;
2918 ce
.window
= ev
->window
;
2920 /* make response appear more WM_SIZE_HINTS-compliant */
2921 if (win
->sh
.flags
) {
2922 DNPRINTF(SWM_D_MISC
, "config_win: hints: win %#x,"
2923 " sh.flags: %u, min: %d x %d, max: %d x %d, inc: "
2924 "%d x %d\n", win
->id
, win
->sh
.flags
, SH_MIN_W(win
),
2925 SH_MIN_H(win
), SH_MAX_W(win
), SH_MAX_H(win
),
2926 SH_INC_W(win
), SH_INC_H(win
));
2931 /* the hint may be set... to 0! */
2932 if (SH_MIN_W(win
) > 0 && ce
.width
< SH_MIN_W(win
))
2933 ce
.width
= SH_MIN_W(win
);
2934 if (SH_MIN_H(win
) > 0 && ce
.height
< SH_MIN_H(win
))
2935 ce
.height
= SH_MIN_H(win
);
2940 /* may also be advertized as 0 */
2941 if (SH_MAX_W(win
) > 0 && ce
.width
> SH_MAX_W(win
))
2942 ce
.width
= SH_MAX_W(win
);
2943 if (SH_MAX_H(win
) > 0 && ce
.height
> SH_MAX_H(win
))
2944 ce
.height
= SH_MAX_H(win
);
2947 /* resize increment. */
2949 if (SH_INC_W(win
) > 1 && ce
.width
> SH_INC_W(win
))
2950 ce
.width
-= (ce
.width
- SH_MIN_W(win
)) %
2952 if (SH_INC_H(win
) > 1 && ce
.height
> SH_INC_H(win
))
2953 ce
.height
-= (ce
.height
- SH_MIN_H(win
)) %
2957 /* adjust x and y for requested border_width. */
2958 ce
.x
+= BORDER(win
) - ev
->border_width
;
2959 ce
.y
+= BORDER(win
) - ev
->border_width
;
2960 ce
.border_width
= ev
->border_width
;
2961 ce
.above_sibling
= ev
->sibling
;
2964 DNPRINTF(SWM_D_MISC
, "config_win: ewmh: %s, win %#x, (x,y) w x h: "
2965 "(%d,%d) %d x %d, border: %d\n", YESNO(ev
== NULL
), win
->id
, ce
.x
,
2966 ce
.y
, ce
.width
, ce
.height
, ce
.border_width
);
2968 xcb_send_event(conn
, 0, win
->id
, XCB_EVENT_MASK_STRUCTURE_NOTIFY
,
2973 count_win(struct workspace
*ws
, int count_transient
)
2978 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
2979 if (!count_transient
&& FLOATING(win
))
2985 DNPRINTF(SWM_D_MISC
, "count_win: %d\n", count
);
2991 quit(struct swm_region
*r
, union arg
*args
)
2993 /* suppress unused warnings since vars are needed */
2997 DNPRINTF(SWM_D_MISC
, "quit\n");
3002 raise_window(struct ws_win
*win
)
3004 struct ws_win
*target
= NULL
;
3005 struct swm_region
*r
;
3006 struct workspace
*ws
;
3008 if (win
== NULL
|| (r
= win
->ws
->r
) == NULL
)
3012 DNPRINTF(SWM_D_EVENT
, "raise_window: win %#x\n", win
->id
);
3014 TAILQ_FOREACH(target
, &ws
->stack
, stack_entry
) {
3015 if (target
== win
|| ICONIC(target
))
3017 if (ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
3019 if (TRANS(win
) && (win
->transient
== target
->transient
||
3020 win
->transient
== target
->id
))
3022 if (FULLSCREEN(win
))
3024 if (FULLSCREEN(target
))
3028 if (MAXIMIZED(target
))
3030 if (ABOVE(win
) || TRANS(win
))
3032 if (!ABOVE(target
) && !TRANS(target
))
3036 if (target
!= NULL
) {
3037 /* Change stack position. */
3038 TAILQ_REMOVE(&ws
->stack
, win
, stack_entry
);
3039 TAILQ_INSERT_BEFORE(target
, win
, stack_entry
);
3040 update_win_stacking(win
);
3044 if (swm_debug
& SWM_D_STACK
) {
3045 DPRINTF("=== stacking order (top down) === \n");
3046 TAILQ_FOREACH(target
, &r
->ws
->stack
, stack_entry
) {
3047 DPRINTF("win %#x, fs: %s, maximized: %s, above: %s, "
3048 "iconic: %s\n", target
->id
, YESNO(FULLSCREEN(target
)),
3049 YESNO(MAXIMIZED(target
)), YESNO(ABOVE(target
)),
3050 YESNO(ICONIC(target
)));
3054 DNPRINTF(SWM_D_EVENT
, "raise_window: done\n");
3058 update_win_stacking(struct ws_win
*win
)
3060 struct ws_win
*sibling
;
3061 struct swm_region
*r
;
3064 if (win
== NULL
|| (r
= win
->ws
->r
) == NULL
)
3067 sibling
= TAILQ_NEXT(win
, stack_entry
);
3068 if (sibling
!= NULL
&& FLOATING(win
) == FLOATING(sibling
))
3069 val
[0] = sibling
->id
;
3071 val
[0] = FLOATING(win
) ? r
->bar
->id
: r
->id
;
3073 DNPRINTF(SWM_D_EVENT
, "update_win_stacking: %#x, sibling %#x\n",
3076 val
[1] = XCB_STACK_MODE_ABOVE
;
3078 xcb_configure_window(conn
, win
->id
, XCB_CONFIG_WINDOW_SIBLING
|
3079 XCB_CONFIG_WINDOW_STACK_MODE
, val
);
3083 map_window(struct ws_win
*win
)
3088 DNPRINTF(SWM_D_EVENT
, "map_window: win %#x, mapped: %s\n",
3089 win
->id
, YESNO(win
->mapped
));
3094 xcb_map_window(conn
, win
->id
);
3095 set_win_state(win
, XCB_ICCCM_WM_STATE_NORMAL
);
3100 unmap_window(struct ws_win
*win
)
3105 DNPRINTF(SWM_D_EVENT
, "unmap_window: win %#x, mapped: %s\n", win
->id
,
3106 YESNO(win
->mapped
));
3111 xcb_unmap_window(conn
, win
->id
);
3112 set_win_state(win
, XCB_ICCCM_WM_STATE_ICONIC
);
3120 int i
, j
, num_screens
;
3122 num_screens
= get_screen_count();
3123 for (i
= 0; i
< num_screens
; i
++)
3124 for (j
= 0; j
< workspace_limit
; j
++)
3125 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
3130 fake_keypress(struct ws_win
*win
, xcb_keysym_t keysym
, uint16_t modifiers
)
3132 xcb_key_press_event_t event
;
3133 xcb_keycode_t
*keycode
;
3138 keycode
= xcb_key_symbols_get_keycode(syms
, keysym
);
3140 DNPRINTF(SWM_D_MISC
, "fake_keypress: win %#x, keycode %u\n",
3143 bzero(&event
, sizeof(event
));
3144 event
.event
= win
->id
;
3145 event
.root
= win
->s
->root
;
3146 event
.child
= XCB_WINDOW_NONE
;
3147 event
.time
= XCB_CURRENT_TIME
;
3148 event
.event_x
= X(win
);
3149 event
.event_y
= Y(win
);
3152 event
.same_screen
= 1;
3153 event
.detail
= *keycode
;
3154 event
.state
= modifiers
;
3156 event
.response_type
= XCB_KEY_PRESS
;
3157 xcb_send_event(conn
, 1, win
->id
,
3158 XCB_EVENT_MASK_KEY_PRESS
, (const char *)&event
);
3160 event
.response_type
= XCB_KEY_RELEASE
;
3161 xcb_send_event(conn
, 1, win
->id
,
3162 XCB_EVENT_MASK_KEY_RELEASE
, (const char *)&event
);
3168 restart(struct swm_region
*r
, union arg
*args
)
3170 /* suppress unused warning since var is needed */
3174 DNPRINTF(SWM_D_MISC
, "restart: %s\n", start_argv
[0]);
3178 execvp(start_argv
[0], start_argv
);
3179 warn("execvp failed");
3184 get_pointer_win(xcb_window_t root
)
3186 struct ws_win
*win
= NULL
;
3187 xcb_query_pointer_reply_t
*r
;
3189 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: root: %#x.\n", root
);
3191 r
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, root
), NULL
);
3193 win
= find_window(r
->child
);
3195 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: %#x.\n",
3198 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: none.\n");
3207 root_to_region(xcb_window_t root
, int check
)
3210 struct swm_region
*r
= NULL
;
3212 xcb_query_pointer_reply_t
*qpr
;
3213 xcb_get_input_focus_reply_t
*gifr
;
3215 DNPRINTF(SWM_D_MISC
, "root_to_region: win %#x\n", root
);
3217 num_screens
= get_screen_count();
3218 for (i
= 0; i
< num_screens
; i
++)
3219 if (screens
[i
].root
== root
)
3222 if (check
& SWM_CK_REGION
)
3223 r
= screens
[i
].r_focus
;
3225 if (r
== NULL
&& check
& SWM_CK_FOCUS
) {
3226 /* Try to find an actively focused window */
3227 gifr
= xcb_get_input_focus_reply(conn
,
3228 xcb_get_input_focus(conn
), NULL
);
3230 cfw
= find_window(gifr
->focus
);
3231 if (cfw
&& cfw
->ws
->r
)
3238 if (r
== NULL
&& check
& SWM_CK_POINTER
) {
3239 /* No region with an active focus; try to use pointer. */
3240 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
,
3241 screens
[i
].root
), NULL
);
3243 DNPRINTF(SWM_D_MISC
, "root_to_region: pointer: "
3244 "(%d,%d)\n", qpr
->root_x
, qpr
->root_y
);
3245 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3246 if (X(r
) <= qpr
->root_x
&&
3247 qpr
->root_x
< MAX_X(r
) &&
3248 Y(r
) <= qpr
->root_y
&&
3249 qpr
->root_y
< MAX_Y(r
))
3256 if (r
== NULL
&& check
& SWM_CK_FALLBACK
)
3257 r
= TAILQ_FIRST(&screens
[i
].rl
);
3259 DNPRINTF(SWM_D_MISC
, "root_to_region: idx: %d\n", get_region_index(r
));
3265 find_unmanaged_window(xcb_window_t id
)
3268 int i
, j
, num_screens
;
3270 num_screens
= get_screen_count();
3271 for (i
= 0; i
< num_screens
; i
++)
3272 for (j
= 0; j
< workspace_limit
; j
++)
3273 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].unmanagedlist
,
3281 find_window(xcb_window_t id
)
3284 int i
, j
, num_screens
;
3285 xcb_query_tree_reply_t
*r
;
3287 num_screens
= get_screen_count();
3288 for (i
= 0; i
< num_screens
; i
++)
3289 for (j
= 0; j
< workspace_limit
; j
++)
3290 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
3294 r
= xcb_query_tree_reply(conn
, xcb_query_tree(conn
, id
), NULL
);
3298 /* if we were looking for the parent return that window instead */
3299 if (r
->parent
== 0 || r
->root
== r
->parent
) {
3304 /* look for parent */
3305 for (i
= 0; i
< num_screens
; i
++)
3306 for (j
= 0; j
< workspace_limit
; j
++)
3307 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
3308 if (r
->parent
== win
->id
) {
3318 spawn(int ws_idx
, union arg
*args
, int close_fd
)
3323 DNPRINTF(SWM_D_MISC
, "spawn: %s\n", args
->argv
[0]);
3325 close(xcb_get_file_descriptor(conn
));
3327 setenv("LD_PRELOAD", SWM_LIB
, 1);
3329 if (asprintf(&ret
, "%d", ws_idx
) == -1) {
3330 warn("spawn: asprintf SWM_WS");
3333 setenv("_SWM_WS", ret
, 1);
3337 if (asprintf(&ret
, "%d", getpid()) == -1) {
3338 warn("spawn: asprintf _SWM_PID");
3341 setenv("_SWM_PID", ret
, 1);
3345 if (setsid() == -1) {
3346 warn("spawn: setsid");
3352 * close stdin and stdout to prevent interaction between apps
3353 * and the baraction script
3354 * leave stderr open to record errors
3356 if ((fd
= open(_PATH_DEVNULL
, O_RDWR
, 0)) == -1) {
3357 warn("spawn: open");
3360 dup2(fd
, STDIN_FILENO
);
3361 dup2(fd
, STDOUT_FILENO
);
3366 execvp(args
->argv
[0], args
->argv
);
3368 warn("spawn: execvp");
3373 kill_refs(struct ws_win
*win
)
3375 int i
, x
, num_screens
;
3376 struct swm_region
*r
;
3377 struct workspace
*ws
;
3382 num_screens
= get_screen_count();
3383 for (i
= 0; i
< num_screens
; i
++)
3384 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3385 for (x
= 0; x
< workspace_limit
; x
++) {
3387 if (win
== ws
->focus
)
3389 if (win
== ws
->focus_prev
)
3390 ws
->focus_prev
= NULL
;
3395 validate_win(struct ws_win
*testwin
)
3398 struct workspace
*ws
;
3399 struct swm_region
*r
;
3400 int i
, x
, num_screens
;
3402 if (testwin
== NULL
)
3405 num_screens
= get_screen_count();
3406 for (i
= 0; i
< num_screens
; i
++)
3407 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3408 for (x
= 0; x
< workspace_limit
; x
++) {
3410 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
3418 validate_ws(struct workspace
*testws
)
3420 struct swm_region
*r
;
3421 struct workspace
*ws
;
3422 int i
, x
, num_screens
;
3424 /* validate all ws */
3425 num_screens
= get_screen_count();
3426 for (i
= 0; i
< num_screens
; i
++)
3427 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3428 for (x
= 0; x
< workspace_limit
; x
++) {
3437 unfocus_win(struct ws_win
*win
)
3439 xcb_window_t none
= XCB_WINDOW_NONE
;
3441 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: win %#x\n", WINID(win
));
3446 if (win
->ws
== NULL
) {
3447 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: NULL ws.\n");
3451 if (validate_ws(win
->ws
)) {
3452 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: invalid ws.\n");
3456 if (win
->ws
->r
== NULL
) {
3457 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: NULL region.\n");
3461 if (validate_win(win
)) {
3462 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: invalid win.\n");
3467 if (win
->ws
->focus
== win
) {
3468 win
->ws
->focus
= NULL
;
3469 win
->ws
->focus_prev
= win
;
3472 if (validate_win(win
->ws
->focus
)) {
3473 kill_refs(win
->ws
->focus
);
3474 win
->ws
->focus
= NULL
;
3477 if (validate_win(win
->ws
->focus_prev
)) {
3478 kill_refs(win
->ws
->focus_prev
);
3479 win
->ws
->focus_prev
= NULL
;
3482 update_window_color(win
);
3484 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
3485 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1, &none
);
3487 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: done.\n");
3491 focus_win(struct ws_win
*win
)
3493 struct ws_win
*cfw
= NULL
, *parent
= NULL
, *w
, *tmpw
;
3494 struct workspace
*ws
;
3495 xcb_get_input_focus_reply_t
*gifr
;
3497 DNPRINTF(SWM_D_FOCUS
, "focus_win: win %#x\n", WINID(win
));
3499 if (win
== NULL
|| win
->ws
== NULL
|| !win
->mapped
)
3504 if (validate_ws(ws
))
3507 if (validate_win(win
)) {
3512 gifr
= xcb_get_input_focus_reply(conn
, xcb_get_input_focus(conn
), NULL
);
3514 cfw
= find_window(gifr
->focus
);
3515 if (cfw
!= NULL
&& cfw
!= win
) {
3516 if (cfw
->ws
!= ws
&& cfw
->ws
->r
!= NULL
) {
3517 /* Change border to unfocused color. */
3518 xcb_change_window_attributes(conn
, cfw
->id
,
3519 XCB_CW_BORDER_PIXEL
,
3520 &cfw
->s
->c
[(MAXIMIZED(cfw
) ?
3521 SWM_S_COLOR_UNFOCUS_MAXIMIZED
:
3522 SWM_S_COLOR_UNFOCUS
)].pixel
);
3530 if (ws
->focus
!= win
) {
3531 if (ws
->focus
&& ws
->focus
!= cfw
)
3532 unfocus_win(ws
->focus
);
3536 /* If this window directs focus to a child window, then clear. */
3537 if (win
->focus_child
)
3538 win
->focus_child
= NULL
;
3540 /* If transient, adjust parent's focus child for focus_magic. */
3542 parent
= find_window(win
->transient
);
3543 if (parent
&& parent
->focus_child
!= win
)
3544 parent
->focus_child
= win
;
3547 if (cfw
!= win
&& ws
->r
!= NULL
) {
3548 /* Set input focus if no input hint, or indicated by hint. */
3549 if (!(win
->hints
.flags
& XCB_ICCCM_WM_HINT_INPUT
) ||
3550 (win
->hints
.flags
& XCB_ICCCM_WM_HINT_INPUT
&&
3552 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
,
3553 win
->id
, last_event_time
);
3555 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
,
3556 ws
->r
->id
, XCB_CURRENT_TIME
);
3558 /* Tell app it can adjust focus to a specific window. */
3559 if (win
->take_focus
) {
3560 /* java is special; always tell parent */
3561 if (TRANS(win
) && win
->java
)
3562 client_msg(parent
, a_takefocus
,
3565 client_msg(win
, a_takefocus
, last_event_time
);
3568 if (ws
->cur_layout
->flags
& SWM_L_MAPONFOCUS
||
3570 /* If a parent exists, map it first. */
3572 raise_window(parent
);
3575 /* Map siblings next. */
3576 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
,
3578 if (w
!= win
&& !ICONIC(w
) &&
3579 w
->transient
== parent
->id
) {
3585 /* Map focused window. */
3589 /* Stack any children of focus window. */
3590 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
, tmpw
)
3591 if (w
->transient
== win
->id
&& !ICONIC(w
)) {
3595 } else if (tile_gap
< 0 && !ABOVE(win
)) {
3597 * Windows overlap in the layout.
3598 * Raise focused win above all tiled wins.
3606 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
3607 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1,
3612 /* Update window border even if workspace is hidden. */
3613 update_window_color(win
);
3618 DNPRINTF(SWM_D_FOCUS
, "focus_win: done.\n");
3621 /* If a child window should have focus instead, return it. */
3623 get_focus_magic(struct ws_win
*win
)
3625 struct ws_win
*parent
= NULL
;
3626 struct ws_win
*child
= NULL
;
3628 DNPRINTF(SWM_D_FOCUS
, "get_focus_magic: win %#x\n", WINID(win
));
3633 parent
= find_window(win
->transient
);
3635 /* If parent prefers focus elsewhere, then try to do so. */
3636 if (parent
&& (child
= parent
->focus_child
)) {
3637 if (validate_win(child
) == 0 && child
->mapped
)
3640 parent
->focus_child
= NULL
;
3644 /* If this window prefers focus elsewhere, then try to do so. */
3645 if ((child
= win
->focus_child
)) {
3646 if (validate_win(child
) == 0 && child
->mapped
)
3649 win
->focus_child
= NULL
;
3656 event_drain(uint8_t rt
)
3658 xcb_generic_event_t
*evt
;
3660 /* ensure all pending requests have been processed before filtering. */
3662 while ((evt
= xcb_poll_for_event(conn
))) {
3663 if (XCB_EVENT_RESPONSE_TYPE(evt
) != rt
)
3671 set_region(struct swm_region
*r
)
3673 struct swm_region
*rf
;
3680 /* Unfocus old region bar. */
3685 xcb_change_window_attributes(conn
, rf
->bar
->id
,
3686 XCB_CW_BORDER_PIXEL
,
3687 &r
->s
->c
[SWM_S_COLOR_BAR_BORDER_UNFOCUS
].pixel
);
3690 if (rf
!= NULL
&& rf
!= r
&& (X(rf
) != X(r
) || Y(rf
) != Y(r
) ||
3691 WIDTH(rf
) != WIDTH(r
) || HEIGHT(rf
) != HEIGHT(r
))) {
3692 /* Set _NET_DESKTOP_GEOMETRY. */
3694 vals
[1] = HEIGHT(r
);
3695 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, r
->s
->root
,
3696 ewmh
[_NET_DESKTOP_GEOMETRY
].atom
, XCB_ATOM_CARDINAL
, 32, 2,
3700 /* Set region bar border to focus_color. */
3701 xcb_change_window_attributes(conn
, r
->bar
->id
,
3702 XCB_CW_BORDER_PIXEL
, &r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].pixel
);
3706 ewmh_update_current_desktop();
3710 focus_region(struct swm_region
*r
)
3713 struct swm_region
*old_r
;
3718 old_r
= r
->s
->r_focus
;
3721 nfw
= get_region_focus(r
);
3725 /* New region is empty; need to manually unfocus win. */
3727 unfocus_win(old_r
->ws
->focus
);
3729 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
, r
->id
,
3732 /* Clear bar since empty. */
3738 switchws(struct swm_region
*r
, union arg
*args
)
3740 struct swm_region
*this_r
, *other_r
;
3742 struct workspace
*new_ws
, *old_ws
;
3743 xcb_window_t none
= XCB_WINDOW_NONE
;
3744 int wsid
= args
->id
, unmap_old
= 0;
3749 if (wsid
>= workspace_limit
)
3753 old_ws
= this_r
->ws
;
3754 new_ws
= &this_r
->s
->ws
[wsid
];
3756 DNPRINTF(SWM_D_WS
, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
3757 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), old_ws
->idx
, wsid
);
3759 if (new_ws
== NULL
|| old_ws
== NULL
)
3761 if (new_ws
== old_ws
)
3764 if ((win
= old_ws
->focus
) != NULL
) {
3765 update_window_color(win
);
3767 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
3768 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1,
3772 other_r
= new_ws
->r
;
3773 if (other_r
== NULL
) {
3774 /* the other workspace is hidden, hide this one */
3778 /* the other ws is visible in another region, exchange them */
3779 other_r
->ws_prior
= new_ws
;
3780 other_r
->ws
= old_ws
;
3781 old_ws
->r
= other_r
;
3783 this_r
->ws_prior
= old_ws
;
3784 this_r
->ws
= new_ws
;
3787 /* Set focus_pending before stacking, if needed. */
3788 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& (!new_ws
->focus_pending
||
3789 validate_win(new_ws
->focus_pending
))) {
3790 new_ws
->focus_pending
= get_region_focus(new_ws
->r
);
3791 new_ws
->focus
= new_ws
->focus_prev
;
3792 new_ws
->focus_prev
= NULL
;
3795 new_ws
->state
= SWM_WS_STATE_MAPPING
;
3798 /* unmap old windows */
3800 TAILQ_FOREACH(win
, &old_ws
->winlist
, entry
)
3802 old_ws
->state
= SWM_WS_STATE_HIDDEN
;
3805 /* if workspaces were swapped, then don't wait to set focus */
3806 if (old_ws
->r
&& focus_mode
!= SWM_FOCUS_FOLLOW
) {
3807 if (new_ws
->focus_pending
) {
3808 focus_win(new_ws
->focus_pending
);
3809 new_ws
->focus_pending
= NULL
;
3813 /* Clear bar and set focus on region input win if new ws is empty. */
3814 if (new_ws
->focus_pending
== NULL
&& new_ws
->focus
== NULL
) {
3815 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
, r
->id
,
3820 ewmh_update_current_desktop();
3823 new_ws
->state
= SWM_WS_STATE_MAPPED
;
3825 DNPRINTF(SWM_D_WS
, "switchws: done.\n");
3829 cyclews(struct swm_region
*r
, union arg
*args
)
3832 struct swm_screen
*s
= r
->s
;
3836 DNPRINTF(SWM_D_WS
, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3837 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
3843 case SWM_ARG_ID_CYCLEWS_MOVE_UP
:
3846 case SWM_ARG_ID_CYCLEWS_UP_ALL
:
3849 case SWM_ARG_ID_CYCLEWS_UP
:
3850 a
.id
= (a
.id
< workspace_limit
- 1) ? a
.id
+ 1 : 0;
3852 case SWM_ARG_ID_CYCLEWS_MOVE_DOWN
:
3855 case SWM_ARG_ID_CYCLEWS_DOWN_ALL
:
3858 case SWM_ARG_ID_CYCLEWS_DOWN
:
3859 a
.id
= (a
.id
> 0) ? a
.id
- 1 : workspace_limit
- 1;
3866 (!cycle_empty
&& TAILQ_EMPTY(&s
->ws
[a
.id
].winlist
)))
3868 if (!cycle_visible
&& s
->ws
[a
.id
].r
!= NULL
)
3875 } while (a
.id
!= r
->ws
->idx
);
3877 DNPRINTF(SWM_D_FOCUS
, "cyclews: done\n");
3881 priorws(struct swm_region
*r
, union arg
*args
)
3887 DNPRINTF(SWM_D_WS
, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3888 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
3890 if (r
->ws_prior
== NULL
)
3893 a
.id
= r
->ws_prior
->idx
;
3895 DNPRINTF(SWM_D_FOCUS
, "priorws: done\n");
3899 focusrg(struct swm_region
*r
, union arg
*args
)
3901 int ridx
= args
->id
, i
, num_screens
;
3902 struct swm_region
*rr
= NULL
;
3904 num_screens
= get_screen_count();
3905 /* do nothing if we don't have more than one screen */
3906 if (!(num_screens
> 1 || outputs
> 1))
3909 DNPRINTF(SWM_D_FOCUS
, "focusrg: id: %d\n", ridx
);
3911 rr
= TAILQ_FIRST(&r
->s
->rl
);
3912 for (i
= 0; i
< ridx
&& rr
!= NULL
; ++i
)
3913 rr
= TAILQ_NEXT(rr
, entry
);
3920 DNPRINTF(SWM_D_FOCUS
, "focusrg: done\n");
3924 cyclerg(struct swm_region
*r
, union arg
*args
)
3926 struct swm_region
*rr
= NULL
;
3929 num_screens
= get_screen_count();
3930 /* do nothing if we don't have more than one screen */
3931 if (!(num_screens
> 1 || outputs
> 1))
3935 DNPRINTF(SWM_D_FOCUS
, "cyclerg: id: %d, region: %d\n", args
->id
, i
);
3938 case SWM_ARG_ID_CYCLERG_UP
:
3939 rr
= TAILQ_NEXT(r
, entry
);
3941 rr
= TAILQ_FIRST(&screens
[i
].rl
);
3943 case SWM_ARG_ID_CYCLERG_DOWN
:
3944 rr
= TAILQ_PREV(r
, swm_region_list
, entry
);
3946 rr
= TAILQ_LAST(&screens
[i
].rl
, swm_region_list
);
3956 DNPRINTF(SWM_D_FOCUS
, "cyclerg: done\n");
3959 /* Sorts transients after parent. */
3961 sort_windows(struct ws_win_list
*wl
)
3963 struct ws_win
*win
, *parent
, *nxt
;
3968 for (win
= TAILQ_FIRST(wl
); win
!= TAILQ_END(wl
); win
= nxt
) {
3969 nxt
= TAILQ_NEXT(win
, entry
);
3971 parent
= find_window(win
->transient
);
3972 if (parent
== NULL
) {
3973 warnx("not possible bug");
3976 TAILQ_REMOVE(wl
, win
, entry
);
3977 TAILQ_INSERT_AFTER(wl
, parent
, win
, entry
);
3983 swapwin(struct swm_region
*r
, union arg
*args
)
3985 struct ws_win
*target
, *source
;
3986 struct ws_win
*cur_focus
;
3987 struct ws_win_list
*wl
;
3989 DNPRINTF(SWM_D_WS
, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3990 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
3992 cur_focus
= r
->ws
->focus
;
3993 if (cur_focus
== NULL
|| FULLSCREEN(cur_focus
))
3996 /* Adjust stacking in floating layer. */
3997 if (ABOVE(cur_focus
)) {
3999 case SWM_ARG_ID_SWAPPREV
:
4000 target
= TAILQ_PREV(cur_focus
, ws_win_stack
,
4002 if (target
!= NULL
&& FLOATING(target
)) {
4003 TAILQ_REMOVE(&cur_focus
->ws
->stack
, cur_focus
,
4005 TAILQ_INSERT_BEFORE(target
, cur_focus
,
4007 update_win_stacking(cur_focus
);
4011 case SWM_ARG_ID_SWAPNEXT
:
4012 target
= TAILQ_NEXT(cur_focus
, stack_entry
);
4013 if (target
!= NULL
&& FLOATING(target
)) {
4014 TAILQ_REMOVE(&cur_focus
->ws
->stack
, cur_focus
,
4016 TAILQ_INSERT_AFTER(&cur_focus
->ws
->stack
,
4017 target
, cur_focus
, stack_entry
);
4018 update_win_stacking(cur_focus
);
4026 if (r
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
4029 clear_maximized(r
->ws
);
4032 wl
= &source
->ws
->winlist
;
4035 case SWM_ARG_ID_SWAPPREV
:
4037 source
= find_window(source
->transient
);
4038 target
= TAILQ_PREV(source
, ws_win_list
, entry
);
4039 if (target
&& target
->transient
)
4040 target
= find_window(target
->transient
);
4041 TAILQ_REMOVE(wl
, source
, entry
);
4043 TAILQ_INSERT_TAIL(wl
, source
, entry
);
4045 TAILQ_INSERT_BEFORE(target
, source
, entry
);
4047 case SWM_ARG_ID_SWAPNEXT
:
4048 target
= TAILQ_NEXT(source
, entry
);
4049 /* move the parent and let the sort handle the move */
4051 source
= find_window(source
->transient
);
4052 TAILQ_REMOVE(wl
, source
, entry
);
4054 TAILQ_INSERT_HEAD(wl
, source
, entry
);
4056 TAILQ_INSERT_AFTER(wl
, target
, source
, entry
);
4058 case SWM_ARG_ID_SWAPMAIN
:
4059 target
= TAILQ_FIRST(wl
);
4060 if (target
== source
) {
4061 if (source
->ws
->focus_prev
!= NULL
&&
4062 source
->ws
->focus_prev
!= target
)
4063 source
= source
->ws
->focus_prev
;
4067 if (target
== NULL
|| source
== NULL
)
4069 source
->ws
->focus_prev
= target
;
4070 TAILQ_REMOVE(wl
, target
, entry
);
4071 TAILQ_INSERT_BEFORE(source
, target
, entry
);
4072 TAILQ_REMOVE(wl
, source
, entry
);
4073 TAILQ_INSERT_HEAD(wl
, source
, entry
);
4075 case SWM_ARG_ID_MOVELAST
:
4076 TAILQ_REMOVE(wl
, source
, entry
);
4077 TAILQ_INSERT_TAIL(wl
, source
, entry
);
4080 DNPRINTF(SWM_D_MOVE
, "swapwin: invalid id: %d\n", args
->id
);
4085 ewmh_update_client_list();
4090 DNPRINTF(SWM_D_MOVE
, "swapwin: done\n");
4094 get_focus_prev(struct ws_win
*win
)
4096 struct ws_win
*winfocus
= NULL
;
4097 struct ws_win
*cur_focus
= NULL
;
4098 struct ws_win_list
*wl
= NULL
;
4099 struct workspace
*ws
= NULL
;
4101 if (!(win
&& win
->ws
))
4106 cur_focus
= ws
->focus
;
4108 DNPRINTF(SWM_D_FOCUS
, "get_focus_prev: win %#x, cur_focus: %#x, "
4109 "focus_prev: %#x\n", WINID(win
), WINID(cur_focus
),
4110 WINID(ws
->focus_prev
));
4112 /* pickle, just focus on whatever */
4113 if (cur_focus
== NULL
) {
4114 /* use prev_focus if valid */
4115 if (ws
->focus_prev
&& find_window(ws
->focus_prev
->id
))
4116 winfocus
= ws
->focus_prev
;
4120 /* if transient focus on parent */
4121 if (TRANS(cur_focus
)) {
4122 winfocus
= find_window(cur_focus
->transient
);
4126 /* if in max_stack try harder */
4127 if ((win
->quirks
& SWM_Q_FOCUSPREV
) ||
4128 (ws
->cur_layout
->flags
& SWM_L_FOCUSPREV
)) {
4129 if (cur_focus
!= ws
->focus_prev
)
4130 winfocus
= ws
->focus_prev
;
4132 winfocus
= TAILQ_PREV(win
, ws_win_list
, entry
);
4137 DNPRINTF(SWM_D_FOCUS
, "get_focus_prev: focus_close: %d\n", focus_close
);
4139 if (winfocus
== NULL
|| winfocus
== win
) {
4140 switch (focus_close
) {
4141 case SWM_STACK_BOTTOM
:
4142 TAILQ_FOREACH(winfocus
, wl
, entry
)
4143 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4147 TAILQ_FOREACH_REVERSE(winfocus
, wl
, ws_win_list
, entry
)
4148 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4151 case SWM_STACK_ABOVE
:
4152 winfocus
= TAILQ_NEXT(cur_focus
, entry
);
4153 while (winfocus
&& ICONIC(winfocus
))
4154 winfocus
= TAILQ_NEXT(winfocus
, entry
);
4156 if (winfocus
== NULL
) {
4157 if (focus_close_wrap
) {
4158 TAILQ_FOREACH(winfocus
, wl
, entry
)
4159 if (!ICONIC(winfocus
) &&
4160 winfocus
!= cur_focus
)
4163 TAILQ_FOREACH_REVERSE(winfocus
, wl
,
4165 if (!ICONIC(winfocus
) &&
4166 winfocus
!= cur_focus
)
4171 case SWM_STACK_BELOW
:
4172 winfocus
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
4173 while (winfocus
&& ICONIC(winfocus
))
4174 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
,
4177 if (winfocus
== NULL
) {
4178 if (focus_close_wrap
) {
4179 TAILQ_FOREACH_REVERSE(winfocus
, wl
,
4181 if (!ICONIC(winfocus
) &&
4182 winfocus
!= cur_focus
)
4185 TAILQ_FOREACH(winfocus
, wl
, entry
)
4186 if (!ICONIC(winfocus
) &&
4187 winfocus
!= cur_focus
)
4195 if (winfocus
== NULL
||
4196 (winfocus
&& (ICONIC(winfocus
) || winfocus
== cur_focus
))) {
4197 if (focus_default
== SWM_STACK_TOP
) {
4198 TAILQ_FOREACH_REVERSE(winfocus
, wl
, ws_win_list
, entry
)
4199 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4202 TAILQ_FOREACH(winfocus
, wl
, entry
)
4203 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4210 return get_focus_magic(winfocus
);
4214 get_region_focus(struct swm_region
*r
)
4216 struct ws_win
*winfocus
= NULL
;
4221 if (r
->ws
->focus
&& !ICONIC(r
->ws
->focus
))
4222 winfocus
= r
->ws
->focus
;
4223 else if (r
->ws
->focus_prev
&& !ICONIC(r
->ws
->focus_prev
))
4224 winfocus
= r
->ws
->focus_prev
;
4226 TAILQ_FOREACH(winfocus
, &r
->ws
->winlist
, entry
)
4227 if (!ICONIC(winfocus
))
4230 return get_focus_magic(winfocus
);
4234 focus(struct swm_region
*r
, union arg
*args
)
4236 struct ws_win
*head
, *cur_focus
= NULL
, *winfocus
= NULL
;
4237 struct ws_win_list
*wl
= NULL
;
4238 struct workspace
*ws
= NULL
;
4241 xcb_icccm_wm_hints_t hints
;
4246 cur_focus
= r
->ws
->focus
;
4250 DNPRINTF(SWM_D_FOCUS
, "focus: id: %d, cur_focus: %#x\n", args
->id
,
4253 /* Make sure an uniconified window has focus, if one exists. */
4254 if (cur_focus
== NULL
) {
4255 cur_focus
= TAILQ_FIRST(wl
);
4256 while (cur_focus
!= NULL
&& ICONIC(cur_focus
))
4257 cur_focus
= TAILQ_NEXT(cur_focus
, entry
);
4259 DNPRINTF(SWM_D_FOCUS
, "focus: new cur_focus: %#x\n",
4264 case SWM_ARG_ID_FOCUSPREV
:
4265 if (cur_focus
== NULL
)
4268 winfocus
= cur_focus
;
4270 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
, entry
);
4271 if (winfocus
== NULL
)
4272 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
4273 if (winfocus
== cur_focus
)
4275 } while (winfocus
&& (ICONIC(winfocus
) ||
4276 winfocus
->id
== cur_focus
->transient
||
4277 (cur_focus
->transient
!= XCB_WINDOW_NONE
&&
4278 winfocus
->transient
== cur_focus
->transient
)));
4280 case SWM_ARG_ID_FOCUSNEXT
:
4281 if (cur_focus
== NULL
)
4284 winfocus
= cur_focus
;
4286 winfocus
= TAILQ_NEXT(winfocus
, entry
);
4287 if (winfocus
== NULL
)
4288 winfocus
= TAILQ_FIRST(wl
);
4289 if (winfocus
== cur_focus
)
4291 } while (winfocus
&& (ICONIC(winfocus
) ||
4292 winfocus
->id
== cur_focus
->transient
||
4293 (cur_focus
->transient
!= XCB_WINDOW_NONE
&&
4294 winfocus
->transient
== cur_focus
->transient
)));
4296 case SWM_ARG_ID_FOCUSMAIN
:
4297 if (cur_focus
== NULL
)
4300 winfocus
= TAILQ_FIRST(wl
);
4301 if (winfocus
== cur_focus
)
4302 winfocus
= cur_focus
->ws
->focus_prev
;
4304 case SWM_ARG_ID_FOCUSURGENT
:
4305 /* Search forward for the next urgent window. */
4309 for (i
= 0; i
<= workspace_limit
; ++i
) {
4311 head
= TAILQ_FIRST(&r
->s
->ws
[(ws
->idx
+ i
) %
4312 workspace_limit
].winlist
);
4314 while (head
!= NULL
&&
4315 (head
= TAILQ_NEXT(head
, entry
)) != NULL
) {
4316 if (head
== cur_focus
) {
4317 winfocus
= cur_focus
;
4320 if (xcb_icccm_get_wm_hints_reply(conn
,
4321 xcb_icccm_get_wm_hints(conn
, head
->id
),
4322 &hints
, NULL
) != 0 &&
4323 xcb_icccm_wm_hints_get_urgency(&hints
)) {
4329 if (winfocus
!= NULL
)
4333 /* Switch ws if new focus is on a different ws. */
4334 if (winfocus
!= NULL
&& winfocus
->ws
!= ws
) {
4335 a
.id
= winfocus
->ws
->idx
;
4343 if (clear_maximized(ws
) > 0)
4346 focus_win(get_focus_magic(winfocus
));
4350 DNPRINTF(SWM_D_FOCUS
, "focus: done\n");
4354 cycle_layout(struct swm_region
*r
, union arg
*args
)
4356 struct workspace
*ws
= r
->ws
;
4358 /* suppress unused warning since var is needed */
4361 DNPRINTF(SWM_D_EVENT
, "cycle_layout: workspace: %d\n", ws
->idx
);
4364 if (ws
->cur_layout
->l_stack
== NULL
)
4365 ws
->cur_layout
= &layouts
[0];
4367 clear_maximized(ws
);
4372 focus_win(get_region_focus(r
));
4378 stack_config(struct swm_region
*r
, union arg
*args
)
4380 struct workspace
*ws
= r
->ws
;
4382 DNPRINTF(SWM_D_STACK
, "stack_config: id: %d workspace: %d\n",
4385 if (clear_maximized(ws
) > 0)
4388 if (ws
->cur_layout
->l_config
!= NULL
)
4389 ws
->cur_layout
->l_config(ws
, args
->id
);
4391 if (args
->id
!= SWM_ARG_ID_STACKINIT
)
4400 struct swm_geometry g
;
4401 struct swm_region
*r
, *r_prev
= NULL
;
4408 DNPRINTF(SWM_D_STACK
, "stack: begin\n");
4410 num_screens
= get_screen_count();
4411 for (i
= 0; i
< num_screens
; i
++) {
4415 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
4416 /* Adjust stack area for region bar and padding. */
4418 g
.x
+= region_padding
;
4419 g
.y
+= region_padding
;
4420 g
.w
-= 2 * border_width
+ 2 * region_padding
;
4421 g
.h
-= 2 * border_width
+ 2 * region_padding
;
4422 if (bar_enabled
&& r
->ws
->bar_enabled
) {
4428 DNPRINTF(SWM_D_STACK
, "stack: workspace: %d (screen: "
4429 "%d, region: %d), (x,y) WxH: (%d,%d) %d x %d\n",
4430 r
->ws
->idx
, i
, j
++, g
.x
, g
.y
, g
.w
, g
.h
);
4433 /* Stack bar/input relative to prev. region. */
4434 val
[1] = XCB_STACK_MODE_ABOVE
;
4436 val
[0] = r_prev
->id
;
4437 DNPRINTF(SWM_D_STACK
, "stack: region input %#x "
4438 "relative to %#x.\n", r
->id
, val
[0]);
4439 xcb_configure_window(conn
, r
->id
,
4440 XCB_CONFIG_WINDOW_SIBLING
|
4441 XCB_CONFIG_WINDOW_STACK_MODE
, val
);
4443 val
[0] = r_prev
->bar
->id
;
4444 DNPRINTF(SWM_D_STACK
, "stack: region bar %#x "
4445 "relative to %#x.\n", r
->bar
->id
, val
[0]);
4446 xcb_configure_window(conn
, r
->bar
->id
,
4447 XCB_CONFIG_WINDOW_SIBLING
|
4448 XCB_CONFIG_WINDOW_STACK_MODE
, val
);
4451 r
->ws
->cur_layout
->l_stack(r
->ws
, &g
);
4452 r
->ws
->cur_layout
->l_string(r
->ws
);
4453 /* save r so we can track region changes */
4461 DNPRINTF(SWM_D_STACK
, "stack: end\n");
4465 store_float_geom(struct ws_win
*win
)
4467 if (win
== NULL
|| win
->ws
->r
== NULL
)
4470 /* retain window geom and region geom */
4471 win
->g_float
= win
->g
;
4472 win
->g_float
.x
-= X(win
->ws
->r
);
4473 win
->g_float
.y
-= Y(win
->ws
->r
);
4474 win
->g_floatvalid
= 1;
4475 DNPRINTF(SWM_D_MISC
, "store_float_geom: win %#x, g: (%d,%d)"
4476 " %d x %d, g_float: (%d,%d) %d x %d\n", win
->id
, X(win
), Y(win
),
4477 WIDTH(win
), HEIGHT(win
), win
->g_float
.x
, win
->g_float
.y
,
4478 win
->g_float
.w
, win
->g_float
.h
);
4482 load_float_geom(struct ws_win
*win
)
4484 if (win
== NULL
|| win
->ws
->r
== NULL
)
4487 if (win
->g_floatvalid
) {
4488 win
->g
= win
->g_float
;
4489 X(win
) += X(win
->ws
->r
);
4490 Y(win
) += Y(win
->ws
->r
);
4491 DNPRINTF(SWM_D_MISC
, "load_float_geom: win %#x, g: (%d,%d)"
4492 "%d x %d\n", win
->id
, X(win
), Y(win
), WIDTH(win
),
4495 DNPRINTF(SWM_D_MISC
, "load_float_geom: win %#x, g_float "
4496 "is not set.\n", win
->id
);
4501 update_floater(struct ws_win
*win
)
4503 struct workspace
*ws
;
4504 struct swm_region
*r
;
4511 if ((r
= ws
->r
) == NULL
)
4514 DNPRINTF(SWM_D_MISC
, "update_floater: win %#x\n", win
->id
);
4518 if (FULLSCREEN(win
)) {
4519 /* _NET_WM_FULLSCREEN: fullscreen without border. */
4520 if (!win
->g_floatvalid
)
4521 store_float_geom(win
);
4525 } else if (MAXIMIZED(win
)) {
4526 /* Maximize: like a single stacked window. */
4527 if (!win
->g_floatvalid
)
4528 store_float_geom(win
);
4532 if (bar_enabled
&& ws
->bar_enabled
) {
4534 Y(win
) += bar_height
;
4535 HEIGHT(win
) -= bar_height
;
4536 } else if (disable_border
) {
4540 if (win
->bordered
) {
4541 HEIGHT(win
) -= 2 * border_width
;
4542 WIDTH(win
) -= 2 * border_width
;
4545 /* Normal floating window. */
4546 /* Update geometry if new region. */
4548 load_float_geom(win
);
4550 if ((win
->quirks
& SWM_Q_FULLSCREEN
) &&
4551 WIDTH(win
) >= WIDTH(r
) && HEIGHT(win
) >= HEIGHT(r
)) {
4552 /* Remove border for FULLSCREEN quirk. */
4554 } else if (!MANUAL(win
)) {
4555 if (TRANS(win
) && (win
->quirks
& SWM_Q_TRANSSZ
)) {
4556 /* Adjust size on TRANSSZ quirk. */
4557 WIDTH(win
) = (double)WIDTH(r
) * dialog_ratio
;
4558 HEIGHT(win
) = (double)HEIGHT(r
) * dialog_ratio
;
4561 if (!(win
->quirks
& SWM_Q_ANYWHERE
)) {
4563 * Floaters and transients are auto-centred
4564 * unless manually moved, resized or ANYWHERE
4567 X(win
) = X(r
) + (WIDTH(r
) - WIDTH(win
)) / 2 -
4569 Y(win
) = Y(r
) + (HEIGHT(r
) - HEIGHT(win
)) / 2 -
4571 store_float_geom(win
);
4576 /* Ensure at least 1 pixel of the window is in the region. */
4577 region_containment(win
, r
, SWM_CW_ALLSIDES
);
4583 * Send keystrokes to terminal to decrease/increase the font size as the
4584 * window size changes.
4587 adjust_font(struct ws_win
*win
)
4589 if (!(win
->quirks
& SWM_Q_XTERM_FONTADJ
) ||
4590 ABOVE(win
) || TRANS(win
))
4593 if (win
->sh
.width_inc
&& win
->last_inc
!= win
->sh
.width_inc
&&
4594 WIDTH(win
) / win
->sh
.width_inc
< term_width
&&
4595 win
->font_steps
< SWM_MAX_FONT_STEPS
) {
4596 win
->font_size_boundary
[win
->font_steps
] =
4597 (win
->sh
.width_inc
* term_width
) + win
->sh
.base_width
;
4600 win
->last_inc
= win
->sh
.width_inc
;
4601 fake_keypress(win
, XK_KP_Subtract
, XCB_MOD_MASK_SHIFT
);
4602 } else if (win
->font_steps
&& win
->last_inc
!= win
->sh
.width_inc
&&
4603 WIDTH(win
) > win
->font_size_boundary
[win
->font_steps
- 1]) {
4606 win
->last_inc
= win
->sh
.width_inc
;
4607 fake_keypress(win
, XK_KP_Add
, XCB_MOD_MASK_SHIFT
);
4611 #define SWAPXY(g) do { \
4613 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
4614 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
4617 stack_master(struct workspace
*ws
, struct swm_geometry
*g
, int rot
, int flip
)
4619 struct swm_geometry win_g
, r_g
= *g
;
4621 int i
, j
, s
, stacks
;
4622 int w_inc
= 1, h_inc
, w_base
= 1, h_base
;
4623 int hrh
, extra
= 0, h_slice
, last_h
= 0;
4624 int split
, colno
, winno
, mwin
, msize
, mscale
;
4625 int remain
, missing
, v_slice
, reconfigure
= 0;
4628 DNPRINTF(SWM_D_STACK
, "stack_master: workspace: %d, rot: %s, "
4629 "flip: %s\n", ws
->idx
, YESNO(rot
), YESNO(flip
));
4631 /* Prepare tiling variables, if needed. */
4632 if ((winno
= count_win(ws
, 0)) > 0) {
4633 /* Find first tiled window. */
4634 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
4635 if (!FLOATING(win
) && !ICONIC(win
))
4638 /* Take into account size hints of first tiled window. */
4640 w_inc
= win
->sh
.width_inc
;
4641 w_base
= win
->sh
.base_width
;
4642 mwin
= ws
->l_state
.horizontal_mwin
;
4643 mscale
= ws
->l_state
.horizontal_msize
;
4644 stacks
= ws
->l_state
.horizontal_stacks
;
4647 w_inc
= win
->sh
.height_inc
;
4648 w_base
= win
->sh
.base_height
;
4649 mwin
= ws
->l_state
.vertical_mwin
;
4650 mscale
= ws
->l_state
.vertical_msize
;
4651 stacks
= ws
->l_state
.vertical_stacks
;
4655 if (stacks
> winno
- mwin
)
4656 stacks
= winno
- mwin
;
4660 h_slice
= r_g
.h
/ SWM_H_SLICE
;
4661 if (mwin
&& winno
> mwin
) {
4662 v_slice
= r_g
.w
/ SWM_V_SLICE
;
4666 win_g
.w
= v_slice
* mscale
;
4668 if (w_inc
> 1 && w_inc
< v_slice
) {
4669 /* Adjust for requested size increment. */
4670 remain
= (win_g
.w
- w_base
) % w_inc
;
4676 win_g
.x
+= r_g
.w
- msize
;
4679 colno
= split
= winno
/ stacks
;
4680 win_g
.w
= ((r_g
.w
- (stacks
* 2 * border_width
) +
4681 2 * border_width
) / stacks
);
4683 hrh
= r_g
.h
/ colno
;
4684 extra
= r_g
.h
- (colno
* hrh
);
4685 win_g
.h
= hrh
- 2 * border_width
;
4686 i
= j
= 0, s
= stacks
;
4689 /* Update window geometry. */
4690 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
4694 if (FLOATING(win
)) {
4695 update_floater(win
);
4700 if (split
&& i
== split
) {
4701 colno
= (winno
- mwin
) / stacks
;
4702 if (s
<= (winno
- mwin
) % stacks
)
4705 hrh
= r_g
.h
/ colno
;
4706 extra
= r_g
.h
- (colno
* hrh
);
4709 win_g
.x
+= win_g
.w
+ 2 * border_width
+
4712 win_g
.w
= (r_g
.w
- msize
-
4713 (stacks
* (2 * border_width
+ tile_gap
))) / stacks
;
4715 win_g
.w
+= (r_g
.w
- msize
-
4716 (stacks
* (2 * border_width
+ tile_gap
))) %
4720 win_g
.x
-= win_g
.w
+ 2 * border_width
+
4726 win_g
.h
= hrh
- 2 * border_width
- tile_gap
;
4729 h_inc
= win
->sh
.width_inc
;
4730 h_base
= win
->sh
.base_width
;
4732 h_inc
= win
->sh
.height_inc
;
4733 h_base
= win
->sh
.base_height
;
4736 if (j
== colno
- 1) {
4737 win_g
.h
= hrh
+ extra
;
4738 } else if (h_inc
> 1 && h_inc
< h_slice
) {
4739 /* adjust for window's requested size increment */
4740 remain
= (win_g
.h
- h_base
) % h_inc
;
4741 missing
= h_inc
- remain
;
4743 if (missing
<= extra
|| j
== 0) {
4755 win_g
.y
+= last_h
+ 2 * border_width
+ tile_gap
;
4757 if (disable_border
&& !(bar_enabled
&& ws
->bar_enabled
) &&
4760 win_g
.w
+= 2 * border_width
;
4761 win_g
.h
+= 2 * border_width
;
4767 if (X(win
) != win_g
.y
|| Y(win
) != win_g
.x
||
4768 WIDTH(win
) != win_g
.h
|| HEIGHT(win
) != win_g
.w
) {
4772 WIDTH(win
) = win_g
.h
;
4773 HEIGHT(win
) = win_g
.w
;
4776 if (X(win
) != win_g
.x
|| Y(win
) != win_g
.y
||
4777 WIDTH(win
) != win_g
.w
|| HEIGHT(win
) != win_g
.h
) {
4781 WIDTH(win
) = win_g
.w
;
4782 HEIGHT(win
) = win_g
.h
;
4786 if (bordered
!= win
->bordered
) {
4788 win
->bordered
= bordered
;
4801 /* Stack all windows from bottom up. */
4802 TAILQ_FOREACH_REVERSE(win
, &ws
->stack
, ws_win_stack
, stack_entry
)
4804 update_win_stacking(win
);
4806 /* Map all windows from top down. */
4807 TAILQ_FOREACH(win
, &ws
->stack
, stack_entry
)
4811 DNPRINTF(SWM_D_STACK
, "stack_master: done\n");
4815 vertical_config(struct workspace
*ws
, int id
)
4817 DNPRINTF(SWM_D_STACK
, "vertical_config: id: %d, workspace: %d\n",
4821 case SWM_ARG_ID_STACKRESET
:
4822 case SWM_ARG_ID_STACKINIT
:
4823 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ 2;
4824 ws
->l_state
.vertical_mwin
= 1;
4825 ws
->l_state
.vertical_stacks
= 1;
4827 case SWM_ARG_ID_MASTERSHRINK
:
4828 if (ws
->l_state
.vertical_msize
> 1)
4829 ws
->l_state
.vertical_msize
--;
4831 case SWM_ARG_ID_MASTERGROW
:
4832 if (ws
->l_state
.vertical_msize
< SWM_V_SLICE
- 1)
4833 ws
->l_state
.vertical_msize
++;
4835 case SWM_ARG_ID_MASTERADD
:
4836 ws
->l_state
.vertical_mwin
++;
4838 case SWM_ARG_ID_MASTERDEL
:
4839 if (ws
->l_state
.vertical_mwin
> 0)
4840 ws
->l_state
.vertical_mwin
--;
4842 case SWM_ARG_ID_STACKINC
:
4843 ws
->l_state
.vertical_stacks
++;
4845 case SWM_ARG_ID_STACKDEC
:
4846 if (ws
->l_state
.vertical_stacks
> 1)
4847 ws
->l_state
.vertical_stacks
--;
4849 case SWM_ARG_ID_FLIPLAYOUT
:
4850 ws
->l_state
.vertical_flip
= !ws
->l_state
.vertical_flip
;
4858 vertical_stack(struct workspace
*ws
, struct swm_geometry
*g
)
4860 DNPRINTF(SWM_D_STACK
, "vertical_stack: workspace: %d\n", ws
->idx
);
4862 stack_master(ws
, g
, 0, ws
->l_state
.vertical_flip
);
4866 horizontal_config(struct workspace
*ws
, int id
)
4868 DNPRINTF(SWM_D_STACK
, "horizontal_config: workspace: %d\n", ws
->idx
);
4871 case SWM_ARG_ID_STACKRESET
:
4872 case SWM_ARG_ID_STACKINIT
:
4873 ws
->l_state
.horizontal_mwin
= 1;
4874 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ 2;
4875 ws
->l_state
.horizontal_stacks
= 1;
4877 case SWM_ARG_ID_MASTERSHRINK
:
4878 if (ws
->l_state
.horizontal_msize
> 1)
4879 ws
->l_state
.horizontal_msize
--;
4881 case SWM_ARG_ID_MASTERGROW
:
4882 if (ws
->l_state
.horizontal_msize
< SWM_H_SLICE
- 1)
4883 ws
->l_state
.horizontal_msize
++;
4885 case SWM_ARG_ID_MASTERADD
:
4886 ws
->l_state
.horizontal_mwin
++;
4888 case SWM_ARG_ID_MASTERDEL
:
4889 if (ws
->l_state
.horizontal_mwin
> 0)
4890 ws
->l_state
.horizontal_mwin
--;
4892 case SWM_ARG_ID_STACKINC
:
4893 ws
->l_state
.horizontal_stacks
++;
4895 case SWM_ARG_ID_STACKDEC
:
4896 if (ws
->l_state
.horizontal_stacks
> 1)
4897 ws
->l_state
.horizontal_stacks
--;
4899 case SWM_ARG_ID_FLIPLAYOUT
:
4900 ws
->l_state
.horizontal_flip
= !ws
->l_state
.horizontal_flip
;
4908 horizontal_stack(struct workspace
*ws
, struct swm_geometry
*g
)
4910 DNPRINTF(SWM_D_STACK
, "horizontal_stack: workspace: %d\n", ws
->idx
);
4912 stack_master(ws
, g
, 1, ws
->l_state
.horizontal_flip
);
4915 /* fullscreen view */
4917 max_stack(struct workspace
*ws
, struct swm_geometry
*g
)
4919 struct swm_geometry gg
= *g
;
4920 struct ws_win
*w
, *win
= NULL
, *parent
= NULL
, *tmpw
;
4923 DNPRINTF(SWM_D_STACK
, "max_stack: workspace: %d\n", ws
->idx
);
4928 winno
= count_win(ws
, 0);
4929 if (winno
== 0 && count_win(ws
, 1) == 0)
4932 /* Figure out which top level window should be visible. */
4933 if (ws
->focus_pending
)
4934 win
= ws
->focus_pending
;
4937 else if (ws
->focus_prev
)
4938 win
= ws
->focus_prev
;
4940 win
= TAILQ_FIRST(&ws
->winlist
);
4942 DNPRINTF(SWM_D_STACK
, "max_stack: focus_pending: %#x, focus: %#x, "
4943 "focus_prev: %#x, first: %#x, win: %#x\n", WINID(ws
->focus_pending
),
4944 WINID(ws
->focus
), WINID(ws
->focus_prev
),
4945 WINID(TAILQ_FIRST(&ws
->winlist
)), win
->id
);
4947 /* Update window geometry. */
4948 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
4957 /* Set maximized flag for all maxed windows. */
4958 if (!MAXIMIZED(w
)) {
4959 /* Preserve floating geometry. */
4961 store_float_geom(w
);
4963 ewmh_apply_flags(w
, w
->ewmh_flags
| EWMH_F_MAXIMIZED
);
4964 ewmh_update_wm_state(w
);
4967 /* Only reconfigure if necessary. */
4968 if (X(w
) != gg
.x
|| Y(w
) != gg
.y
|| WIDTH(w
) != gg
.w
||
4969 HEIGHT(w
) != gg
.h
) {
4971 if (bar_enabled
&& ws
->bar_enabled
){
4975 WIDTH(w
) += 2 * border_width
;
4976 HEIGHT(w
) += 2 * border_width
;
4983 /* If transient, stack parent and its children. */
4984 if (TRANS(win
) && (parent
= find_window(win
->transient
))) {
4985 raise_window(parent
);
4987 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
, tmpw
)
4988 if (w
->transient
== parent
->id
)
4992 /* Make sure focus window is on top. */
4995 /* Stack any children of focus window. */
4996 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
, tmpw
)
4997 if (w
->transient
== win
->id
)
5000 /* Map all windows. */
5001 TAILQ_FOREACH(w
, &ws
->stack
, stack_entry
)
5007 send_to_rg(struct swm_region
*r
, union arg
*args
)
5009 int ridx
= args
->id
, i
, num_screens
;
5010 struct swm_region
*rr
= NULL
;
5013 num_screens
= get_screen_count();
5014 /* do nothing if we don't have more than one screen */
5015 if (!(num_screens
> 1 || outputs
> 1))
5018 DNPRINTF(SWM_D_FOCUS
, "send_to_rg: id: %d\n", ridx
);
5020 rr
= TAILQ_FIRST(&r
->s
->rl
);
5021 for (i
= 0; i
< ridx
&& rr
!= NULL
; ++i
)
5022 rr
= TAILQ_NEXT(rr
, entry
);
5033 region_under(struct swm_screen
*s
, int x
, int y
)
5035 struct swm_region
*r
= NULL
;
5037 TAILQ_FOREACH(r
, &s
->rl
, entry
) {
5038 DNPRINTF(SWM_D_MISC
, "region_under: ws: %d, region g: (%d,%d) "
5039 "%d x %d, coords: (%d,%d)\n", r
->ws
->idx
, X(r
), Y(r
),
5040 WIDTH(r
), HEIGHT(r
), x
, y
);
5041 if (X(r
) <= x
&& x
< MAX_X(r
))
5042 if (Y(r
) <= y
&& y
< MAX_Y(r
))
5049 /* Transfer focused window to target workspace and focus. */
5051 send_to_ws(struct swm_region
*r
, union arg
*args
)
5053 int wsid
= args
->id
;
5054 struct ws_win
*win
= NULL
;
5056 if (r
&& r
->ws
&& r
->ws
->focus
)
5061 DNPRINTF(SWM_D_MOVE
, "send_to_ws: win %#x, ws %d\n", win
->id
, wsid
);
5063 if (wsid
< 0 || wsid
>= workspace_limit
)
5066 if (win
->ws
->idx
== wsid
)
5069 win_to_ws(win
, wsid
, 1);
5071 /* Set new focus on target ws. */
5072 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
5073 win
->ws
->focus_prev
= win
->ws
->focus
;
5074 win
->ws
->focus
= win
;
5075 win
->ws
->focus_pending
= NULL
;
5077 if (win
->ws
->focus_prev
)
5078 update_window_color(win
->ws
->focus_prev
);
5081 DNPRINTF(SWM_D_STACK
, "send_to_ws: focus_pending: %#x, focus: %#x, "
5082 "focus_prev: %#x, first: %#x, win: %#x\n",
5083 WINID(r
->ws
->focus_pending
), WINID(r
->ws
->focus
),
5084 WINID(r
->ws
->focus_prev
), WINID(TAILQ_FIRST(&r
->ws
->winlist
)),
5087 ewmh_apply_flags(win
, win
->ewmh_flags
& ~EWMH_F_MAXIMIZED
);
5088 ewmh_update_wm_state(win
);
5090 /* Restack and set new focus on current ws. */
5092 load_float_geom(win
);
5096 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
5097 if (r
->ws
->focus
!= NULL
) {
5098 focus_win(r
->ws
->focus
);
5100 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
, r
->id
,
5110 win_to_ws(struct ws_win
*win
, int wsid
, int unfocus
)
5112 struct ws_win
*parent
;
5113 struct workspace
*ws
, *nws
, *pws
;
5115 if (wsid
< 0 || wsid
>= workspace_limit
)
5118 if (win
->ws
->idx
== wsid
)
5122 nws
= &win
->s
->ws
[wsid
];
5124 DNPRINTF(SWM_D_MOVE
, "win_to_ws: win %#x, ws %d -> %d\n", win
->id
,
5127 /* Cleanup focus on source ws. */
5128 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
5129 (ws
->focus
== win
|| ws
->focus_pending
== win
))
5130 ws
->focus_pending
= get_focus_prev(win
);
5132 /* Move the parent if this is a transient window. */
5134 parent
= find_window(win
->transient
);
5137 /* Set new focus in parent's ws if needed. */
5138 if (pws
->focus
== parent
) {
5139 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
5140 pws
->focus_pending
=
5141 get_focus_prev(parent
);
5143 unfocus_win(parent
);
5145 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
5146 pws
->focus
= pws
->focus_pending
;
5147 pws
->focus_pending
= NULL
;
5151 /* Don't unmap parent if new ws is visible */
5153 unmap_window(parent
);
5156 TAILQ_REMOVE(&ws
->winlist
, parent
, entry
);
5157 TAILQ_REMOVE(&ws
->stack
, parent
, stack_entry
);
5158 TAILQ_INSERT_TAIL(&nws
->winlist
, parent
, entry
);
5159 TAILQ_INSERT_TAIL(&nws
->stack
, parent
, stack_entry
);
5162 DNPRINTF(SWM_D_PROP
, "win_to_ws: set property: "
5163 "_NET_WM_DESKTOP: %d\n", wsid
);
5164 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
5165 parent
->id
, ewmh
[_NET_WM_DESKTOP
].atom
,
5166 XCB_ATOM_CARDINAL
, 32, 1, &wsid
);
5173 if (ws
->focus_prev
== win
)
5174 ws
->focus_prev
= NULL
;
5176 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& ws
->focus_pending
!= NULL
) {
5177 ws
->focus
= ws
->focus_pending
;
5178 ws
->focus_pending
= NULL
;
5181 /* Don't unmap if new ws is visible */
5186 TAILQ_REMOVE(&ws
->winlist
, win
, entry
);
5187 TAILQ_REMOVE(&ws
->stack
, win
, stack_entry
);
5188 TAILQ_INSERT_TAIL(&nws
->winlist
, win
, entry
);
5189 TAILQ_INSERT_TAIL(&nws
->stack
, win
, stack_entry
);
5192 /* Update the window's workspace property: _NET_WM_DESKTOP */
5193 DNPRINTF(SWM_D_PROP
, "win_to_ws: set property: "
5194 "_NET_WM_DESKTOP: %d\n", wsid
);
5195 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
5196 ewmh
[_NET_WM_DESKTOP
].atom
, XCB_ATOM_CARDINAL
, 32, 1, &wsid
);
5198 ewmh_update_client_list();
5200 DNPRINTF(SWM_D_MOVE
, "win_to_ws: done.\n");
5204 pressbutton(struct swm_region
*r
, union arg
*args
)
5206 /* suppress unused warning since var is needed */
5209 xcb_test_fake_input(conn
, XCB_BUTTON_PRESS
, args
->id
,
5210 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
5211 xcb_test_fake_input(conn
, XCB_BUTTON_RELEASE
, args
->id
,
5212 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
5216 raise_toggle(struct swm_region
*r
, union arg
*args
)
5218 /* suppress unused warning since var is needed */
5221 if (r
== NULL
|| r
->ws
== NULL
)
5224 if (r
->ws
->focus
&& MAXIMIZED(r
->ws
->focus
))
5227 r
->ws
->always_raise
= !r
->ws
->always_raise
;
5229 /* bring floaters back to top */
5230 if (!r
->ws
->always_raise
)
5237 iconify(struct swm_region
*r
, union arg
*args
)
5240 /* suppress unused warning since var is needed */
5243 if ((w
= r
->ws
->focus
) == NULL
)
5246 ewmh_apply_flags(w
, w
->ewmh_flags
| EWMH_F_HIDDEN
);
5247 ewmh_update_wm_state(w
);
5255 get_win_name(xcb_window_t win
)
5258 xcb_get_property_cookie_t c
;
5259 xcb_get_property_reply_t
*r
;
5261 /* First try _NET_WM_NAME for UTF-8. */
5262 c
= xcb_get_property(conn
, 0, win
, ewmh
[_NET_WM_NAME
].atom
,
5263 XCB_GET_PROPERTY_TYPE_ANY
, 0, UINT_MAX
);
5264 r
= xcb_get_property_reply(conn
, c
, NULL
);
5265 if (r
&& r
->type
== XCB_NONE
) {
5267 /* Use WM_NAME instead; no UTF-8. */
5268 c
= xcb_get_property(conn
, 0, win
, XCB_ATOM_WM_NAME
,
5269 XCB_GET_PROPERTY_TYPE_ANY
, 0, UINT_MAX
);
5270 r
= xcb_get_property_reply(conn
, c
, NULL
);
5273 if (r
&& r
->type
!= XCB_NONE
&& r
->length
> 0)
5274 name
= strndup(xcb_get_property_value(r
),
5275 xcb_get_property_value_length(r
));
5280 err(1, "get_win_name: failed to allocate memory.");
5288 uniconify(struct swm_region
*r
, union arg
*args
)
5295 DNPRINTF(SWM_D_MISC
, "uniconify\n");
5297 if (r
== NULL
|| r
->ws
== NULL
)
5300 /* make sure we have anything to uniconify */
5301 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
5302 if (win
->ws
== NULL
)
5303 continue; /* should never happen */
5309 DNPRINTF(SWM_D_MISC
, "uniconify: count: %d\n", count
);
5315 search_resp_action
= SWM_SEARCH_UNICONIFY
;
5317 spawn_select(r
, args
, "search", &searchpid
);
5319 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
5322 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
5323 if (win
->ws
== NULL
)
5324 continue; /* should never happen */
5328 name
= get_win_name(win
->id
);
5329 fprintf(lfile
, "%s.%u\n", name
, win
->id
);
5337 name_workspace(struct swm_region
*r
, union arg
*args
)
5341 DNPRINTF(SWM_D_MISC
, "name_workspace\n");
5347 search_resp_action
= SWM_SEARCH_NAME_WORKSPACE
;
5349 spawn_select(r
, args
, "name_workspace", &searchpid
);
5351 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
5354 fprintf(lfile
, "%s", "");
5359 search_workspace(struct swm_region
*r
, union arg
*args
)
5362 struct workspace
*ws
;
5365 DNPRINTF(SWM_D_MISC
, "search_workspace\n");
5371 search_resp_action
= SWM_SEARCH_SEARCH_WORKSPACE
;
5373 spawn_select(r
, args
, "search", &searchpid
);
5375 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
5378 for (i
= 0; i
< workspace_limit
; i
++) {
5382 fprintf(lfile
, "%d%s%s\n", ws
->idx
+ 1,
5383 (ws
->name
? ":" : ""), (ws
->name
? ws
->name
: ""));
5390 search_win_cleanup(void)
5392 struct search_window
*sw
= NULL
;
5394 while ((sw
= TAILQ_FIRST(&search_wl
)) != NULL
) {
5395 xcb_destroy_window(conn
, sw
->indicator
);
5396 TAILQ_REMOVE(&search_wl
, sw
, entry
);
5402 search_win(struct swm_region
*r
, union arg
*args
)
5404 struct ws_win
*win
= NULL
;
5405 struct search_window
*sw
= NULL
;
5408 xcb_screen_t
*screen
;
5409 int i
, width
, height
;
5417 XRectangle l_ibox
, l_lbox
;
5419 DNPRINTF(SWM_D_MISC
, "search_win\n");
5422 search_resp_action
= SWM_SEARCH_SEARCH_WINDOW
;
5424 spawn_select(r
, args
, "search", &searchpid
);
5426 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
5429 if ((screen
= get_screen(r
->s
->idx
)) == NULL
)
5430 errx(1, "ERROR: can't get screen %d.", r
->s
->idx
);
5432 TAILQ_INIT(&search_wl
);
5435 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
5439 sw
= calloc(1, sizeof(struct search_window
));
5441 warn("search_win: calloc");
5443 search_win_cleanup();
5449 snprintf(s
, sizeof s
, "%d", i
);
5452 w
= xcb_generate_id(conn
);
5453 wa
[0] = r
->s
->c
[SWM_S_COLOR_FOCUS
].pixel
;
5454 wa
[1] = r
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
;
5455 wa
[2] = screen
->default_colormap
;
5457 if (bar_font_legacy
) {
5458 XmbTextExtents(bar_fs
, s
, len
, &l_ibox
, &l_lbox
);
5459 width
= l_lbox
.width
+ 4;
5460 height
= bar_fs_extents
->max_logical_extent
.height
+ 4;
5462 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
,
5464 width
= info
.width
+ 4;
5465 height
= bar_font
->height
+ 4;
5468 xcb_create_window(conn
, screen
->root_depth
, w
, win
->id
, 0, 0,
5469 width
, height
, 1, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
5470 screen
->root_visual
, XCB_CW_BACK_PIXEL
|
5471 XCB_CW_BORDER_PIXEL
| XCB_CW_COLORMAP
, wa
);
5473 xcb_map_window(conn
, w
);
5476 TAILQ_INSERT_TAIL(&search_wl
, sw
, entry
);
5478 if (bar_font_legacy
) {
5479 l_gcv
.graphics_exposures
= 0;
5480 l_draw
= XCreateGC(display
, w
, 0, &l_gcv
);
5482 XSetForeground(display
, l_draw
,
5483 r
->s
->c
[SWM_S_COLOR_BAR
].pixel
);
5485 DRAWSTRING(display
, w
, bar_fs
, l_draw
, 2,
5486 (bar_fs_extents
->max_logical_extent
.height
-
5487 l_lbox
.height
) / 2 - l_lbox
.y
, s
, len
);
5489 XFreeGC(display
, l_draw
);
5492 draw
= XftDrawCreate(display
, w
,
5493 DefaultVisual(display
, r
->s
->idx
),
5494 DefaultColormap(display
, r
->s
->idx
));
5496 XftDrawStringUtf8(draw
, &search_font_color
, bar_font
, 2,
5497 (HEIGHT(r
->bar
) + bar_font
->height
) / 2 -
5498 bar_font
->descent
, (FcChar8
*)s
, len
);
5500 XftDrawDestroy(draw
);
5503 DNPRINTF(SWM_D_MISC
, "search_win: mapped win %#x\n", w
);
5505 fprintf(lfile
, "%d\n", i
);
5515 search_resp_uniconify(const char *resp
, size_t len
)
5521 DNPRINTF(SWM_D_MISC
, "search_resp_uniconify: resp: %s\n", resp
);
5523 TAILQ_FOREACH(win
, &search_r
->ws
->winlist
, entry
) {
5526 name
= get_win_name(win
->id
);
5527 if (asprintf(&s
, "%s.%u", name
, win
->id
) == -1) {
5532 if (strncmp(s
, resp
, len
) == 0) {
5533 /* XXX this should be a callback to generalize */
5534 ewmh_apply_flags(win
, win
->ewmh_flags
& ~EWMH_F_HIDDEN
);
5535 ewmh_update_wm_state(win
);
5545 search_resp_name_workspace(const char *resp
, size_t len
)
5547 struct workspace
*ws
;
5549 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: resp: %s\n", resp
);
5551 if (search_r
->ws
== NULL
)
5556 free(search_r
->ws
->name
);
5557 search_r
->ws
->name
= NULL
;
5561 ws
->name
= strdup(resp
);
5562 if (ws
->name
== NULL
) {
5563 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: "
5564 "strdup: %s", strerror(errno
));
5568 ewmh_update_desktop_names();
5569 ewmh_get_desktop_names();
5574 ewmh_update_desktop_names(void)
5576 char *name_list
= NULL
, *p
;
5577 int num_screens
, i
, j
, len
= 0, tot
= 0;
5579 num_screens
= get_screen_count();
5580 for (i
= 0; i
< num_screens
; ++i
) {
5581 for (j
= 0; j
< workspace_limit
; ++j
) {
5582 if (screens
[i
].ws
[j
].name
!= NULL
)
5583 len
+= strlen(screens
[i
].ws
[j
].name
);
5587 if((name_list
= calloc(len
, sizeof(char))) == NULL
)
5588 err(1, "update_desktop_names: calloc: failed to "
5589 "allocate memory.");
5592 for (j
= 0; j
< workspace_limit
; ++j
) {
5593 if (screens
[i
].ws
[j
].name
!= NULL
) {
5594 len
= strlen(screens
[i
].ws
[j
].name
);
5595 memcpy(p
, screens
[i
].ws
[j
].name
, len
);
5604 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
5605 screens
[i
].root
, ewmh
[_NET_DESKTOP_NAMES
].atom
,
5606 a_utf8_string
, 8, tot
, name_list
);
5616 ewmh_get_desktop_names(void)
5619 xcb_get_property_cookie_t c
;
5620 xcb_get_property_reply_t
*r
;
5621 int num_screens
, i
, j
, n
, k
;
5623 num_screens
= get_screen_count();
5624 for (i
= 0; i
< num_screens
; ++i
) {
5625 for (j
= 0; j
< workspace_limit
; ++j
) {
5626 free(screens
[i
].ws
[j
].name
);
5627 screens
[i
].ws
[j
].name
= NULL
;
5630 c
= xcb_get_property(conn
, 0, screens
[i
].root
,
5631 ewmh
[_NET_DESKTOP_NAMES
].atom
,
5632 a_utf8_string
, 0, UINT32_MAX
);
5633 r
= xcb_get_property_reply(conn
, c
, NULL
);
5637 names
= xcb_get_property_value(r
);
5638 n
= xcb_get_property_value_length(r
);
5640 for (j
= 0, k
= 0; j
< n
; ++j
) {
5641 if (*(names
+ j
) != '\0') {
5642 screens
[i
].ws
[k
].name
= strdup(names
+ j
);
5643 j
+= strlen(names
+ j
);
5652 ewmh_update_client_list(void)
5655 int num_screens
, i
, j
, k
= 0, count
= 0;
5658 num_screens
= get_screen_count();
5659 for (i
= 0; i
< num_screens
; ++i
) {
5660 for (j
= 0; j
< workspace_limit
; ++j
)
5661 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
5664 DNPRINTF(SWM_D_PROP
, "ewmh_update_client_list: win count: %d\n",
5670 wins
= calloc(count
, sizeof(xcb_window_t
));
5672 err(1, "ewmh_update_client_list: calloc: failed to "
5673 "allocate memory.");
5675 for (j
= 0, k
= 0; j
< workspace_limit
; ++j
)
5676 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
5677 wins
[k
++] = win
->id
;
5679 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
5680 screens
[i
].root
, ewmh
[_NET_CLIENT_LIST
].atom
,
5681 XCB_ATOM_WINDOW
, 32, count
, wins
);
5688 ewmh_update_current_desktop(void)
5692 num_screens
= get_screen_count();
5693 for (i
= 0; i
< num_screens
; ++i
)
5694 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
5695 screens
[i
].root
, ewmh
[_NET_CURRENT_DESKTOP
].atom
,
5696 XCB_ATOM_CARDINAL
, 32, 1, &screens
[i
].r_focus
->ws
->idx
);
5700 ewmh_update_desktops(void)
5702 int num_screens
, i
, j
;
5705 vals
= calloc(workspace_limit
* 2, sizeof(uint32_t));
5707 err(1, "ewmh_update_desktops: calloc: failed to allocate "
5710 num_screens
= get_screen_count();
5711 for (i
= 0; i
< num_screens
; i
++) {
5712 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
5713 screens
[i
].root
, ewmh
[_NET_NUMBER_OF_DESKTOPS
].atom
,
5714 XCB_ATOM_CARDINAL
, 32, 1, &workspace_limit
);
5716 for (j
= 0; j
< workspace_limit
; ++j
) {
5717 if (screens
[i
].ws
[j
].r
!= NULL
) {
5718 vals
[j
* 2] = X(screens
[i
].ws
[j
].r
);
5719 vals
[j
* 2 + 1] = Y(screens
[i
].ws
[j
].r
);
5720 } else if (screens
[i
].ws
[j
].old_r
!= NULL
) {
5721 vals
[j
* 2] = X(screens
[i
].ws
[j
].old_r
);
5722 vals
[j
* 2 + 1] = Y(screens
[i
].ws
[j
].old_r
);
5724 vals
[j
* 2] = vals
[j
* 2 + 1] = 0;
5728 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
5729 screens
[i
].root
, ewmh
[_NET_DESKTOP_VIEWPORT
].atom
,
5730 XCB_ATOM_CARDINAL
, 32, workspace_limit
* 2, vals
);
5737 search_resp_search_workspace(const char *resp
)
5744 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: resp: %s\n", resp
);
5748 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: strdup: %s",
5755 ws_idx
= (int)strtonum(q
, 1, workspace_limit
, &errstr
);
5757 DNPRINTF(SWM_D_MISC
, "workspace idx is %s: %s",
5764 switchws(search_r
, &a
);
5768 search_resp_search_window(const char *resp
)
5773 struct search_window
*sw
;
5775 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: resp: %s\n", resp
);
5779 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: strdup: %s",
5784 idx
= (int)strtonum(s
, 1, INT_MAX
, &errstr
);
5786 DNPRINTF(SWM_D_MISC
, "window idx is %s: %s",
5793 TAILQ_FOREACH(sw
, &search_wl
, entry
)
5794 if (idx
== sw
->idx
) {
5800 #define MAX_RESP_LEN 1024
5803 search_do_resp(void)
5809 DNPRINTF(SWM_D_MISC
, "search_do_resp:\n");
5814 if ((resp
= calloc(1, MAX_RESP_LEN
+ 1)) == NULL
) {
5815 warn("search: calloc");
5819 rbytes
= read(select_resp_pipe
[0], resp
, MAX_RESP_LEN
);
5821 warn("search: read error");
5824 resp
[rbytes
] = '\0';
5827 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
5828 * newline, so work around that by sanitizing the resp now.
5830 resp
[strcspn(resp
, "\n")] = '\0';
5833 switch (search_resp_action
) {
5834 case SWM_SEARCH_UNICONIFY
:
5835 search_resp_uniconify(resp
, len
);
5837 case SWM_SEARCH_NAME_WORKSPACE
:
5838 search_resp_name_workspace(resp
, len
);
5840 case SWM_SEARCH_SEARCH_WORKSPACE
:
5841 search_resp_search_workspace(resp
);
5843 case SWM_SEARCH_SEARCH_WINDOW
:
5844 search_resp_search_window(resp
);
5849 if (search_resp_action
== SWM_SEARCH_SEARCH_WINDOW
)
5850 search_win_cleanup();
5852 search_resp_action
= SWM_SEARCH_NONE
;
5853 close(select_resp_pipe
[0]);
5860 wkill(struct swm_region
*r
, union arg
*args
)
5862 DNPRINTF(SWM_D_MISC
, "wkill: win %#x, id: %d\n", WINID(r
->ws
->focus
),
5865 if (r
->ws
->focus
== NULL
)
5868 if (args
->id
== SWM_ARG_ID_KILLWINDOW
)
5869 xcb_kill_client(conn
, r
->ws
->focus
->id
);
5871 if (r
->ws
->focus
->can_delete
)
5872 client_msg(r
->ws
->focus
, a_delete
, 0);
5878 clear_maximized(struct workspace
*ws
)
5883 /* Clear any maximized win(s) on ws, from bottom up. */
5884 TAILQ_FOREACH_REVERSE(w
, &ws
->stack
, ws_win_stack
, stack_entry
)
5886 ewmh_apply_flags(w
, w
->ewmh_flags
& ~EWMH_F_MAXIMIZED
);
5887 ewmh_update_wm_state(w
);
5895 maximize_toggle(struct swm_region
*r
, union arg
*args
)
5897 struct ws_win
*w
= r
->ws
->focus
;
5899 /* suppress unused warning since var is needed */
5905 DNPRINTF(SWM_D_MISC
, "maximize_toggle: win %#x\n", w
->id
);
5910 if (w
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
5913 ewmh_apply_flags(w
, w
->ewmh_flags
^ EWMH_F_MAXIMIZED
);
5914 ewmh_update_wm_state(w
);
5918 if (w
== w
->ws
->focus
)
5922 DNPRINTF(SWM_D_MISC
, "maximize_toggle: done\n");
5926 floating_toggle(struct swm_region
*r
, union arg
*args
)
5928 struct ws_win
*w
= r
->ws
->focus
;
5930 /* suppress unused warning since var is needed */
5936 DNPRINTF(SWM_D_MISC
, "floating_toggle: win %#x\n", w
->id
);
5938 if (FULLSCREEN(w
) || TRANS(w
))
5941 if (w
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
5944 ewmh_apply_flags(w
, w
->ewmh_flags
^ EWMH_F_ABOVE
);
5945 ewmh_update_wm_state(w
);
5949 if (w
== w
->ws
->focus
)
5953 DNPRINTF(SWM_D_MISC
, "floating_toggle: done\n");
5957 region_containment(struct ws_win
*win
, struct swm_region
*r
, int opts
)
5959 struct swm_geometry g
= r
->g
;
5960 int rt
, lt
, tp
, bm
, bw
;
5962 bw
= (opts
& SWM_CW_SOFTBOUNDARY
) ? boundary_width
: 0;
5965 * Perpendicular distance of each side of the window to the respective
5966 * side of the region boundary. Positive values indicate the side of
5967 * the window has passed beyond the region boundary.
5969 rt
= opts
& SWM_CW_RIGHT
? MAX_X(win
) + BORDER(win
) - MAX_X(r
) : bw
;
5970 lt
= opts
& SWM_CW_LEFT
? X(r
) - X(win
) + BORDER(win
) : bw
;
5971 bm
= opts
& SWM_CW_BOTTOM
? MAX_Y(win
) + BORDER(win
) - MAX_Y(r
) : bw
;
5972 tp
= opts
& SWM_CW_TOP
? Y(r
) - Y(win
) + BORDER(win
) : bw
;
5974 DNPRINTF(SWM_D_MISC
, "region_containment: win %#x, rt: %d, lt: %d, "
5975 "bm: %d, tp: %d, SOFTBOUNDARY: %s, HARDBOUNDARY: %s\n", win
->id
, rt
,
5976 lt
, bm
, tp
, YESNO(opts
& SWM_CW_SOFTBOUNDARY
),
5977 YESNO(opts
& SWM_CW_HARDBOUNDARY
));
5980 * Disable containment if any of the flagged sides went beyond the
5981 * containment boundary, or if containment is disabled.
5983 if (!(opts
& SWM_CW_HARDBOUNDARY
|| opts
& SWM_CW_SOFTBOUNDARY
) ||
5984 (bw
!= 0 && ((rt
> bw
) || (lt
> bw
) || (bm
> bw
) || (tp
> bw
)))) {
5985 /* Make sure window has at least 1 pixel in the region */
5986 g
.x
+= 1 - WIDTH(win
);
5987 g
.y
+= 1 - HEIGHT(win
);
5988 g
.w
+= 2 * WIDTH(win
) - 2;
5989 g
.h
+= 2 * HEIGHT(win
) - 2;
5992 constrain_window(win
, &g
, &opts
);
5995 /* Move or resize a window so that flagged side(s) fit into the supplied box. */
5997 constrain_window(struct ws_win
*win
, struct swm_geometry
*b
, int *opts
)
5999 DNPRINTF(SWM_D_MISC
, "constrain_window: win %#x, (x,y) w x h: "
6000 "(%d,%d) %d x %d, box: (x,y) w x h: (%d,%d) %d x %d, rt: %s, "
6001 "lt: %s, bt: %s, tp: %s, allow resize: %s\n", win
->id
, X(win
),
6002 Y(win
), WIDTH(win
), HEIGHT(win
), b
->x
, b
->y
, b
->w
, b
->h
,
6003 YESNO(*opts
& SWM_CW_RIGHT
), YESNO(*opts
& SWM_CW_LEFT
),
6004 YESNO(*opts
& SWM_CW_BOTTOM
), YESNO(*opts
& SWM_CW_TOP
),
6005 YESNO(*opts
& SWM_CW_RESIZABLE
));
6007 if ((*opts
& SWM_CW_RIGHT
) && MAX_X(win
) + BORDER(win
) > b
->x
+ b
->w
) {
6008 if (*opts
& SWM_CW_RESIZABLE
)
6009 WIDTH(win
) = b
->x
+ b
->w
- X(win
) - BORDER(win
);
6011 X(win
) = b
->x
+ b
->w
- WIDTH(win
) - BORDER(win
);
6014 if ((*opts
& SWM_CW_LEFT
) && X(win
) + BORDER(win
) < b
->x
) {
6015 if (*opts
& SWM_CW_RESIZABLE
)
6016 WIDTH(win
) -= b
->x
- X(win
) - BORDER(win
);
6018 X(win
) = b
->x
- BORDER(win
);
6021 if ((*opts
& SWM_CW_BOTTOM
) && MAX_Y(win
) + BORDER(win
) > b
->y
+ b
->h
) {
6022 if (*opts
& SWM_CW_RESIZABLE
)
6023 HEIGHT(win
) = b
->y
+ b
->h
- Y(win
) - BORDER(win
);
6025 Y(win
) = b
->y
+ b
->h
- HEIGHT(win
) - BORDER(win
);
6028 if ((*opts
& SWM_CW_TOP
) && Y(win
) + BORDER(win
) < b
->y
) {
6029 if (*opts
& SWM_CW_RESIZABLE
)
6030 HEIGHT(win
) -= b
->y
- Y(win
) - BORDER(win
);
6032 Y(win
) = b
->y
- BORDER(win
);
6035 if (*opts
& SWM_CW_RESIZABLE
) {
6038 if (HEIGHT(win
) < 1)
6044 update_window_color(struct ws_win
*win
)
6048 if (WS_FOCUSED(win
->ws
) && win
->ws
->focus
== win
)
6049 pixel
= MAXIMIZED(win
) ?
6050 &win
->s
->c
[SWM_S_COLOR_FOCUS_MAXIMIZED
].pixel
:
6051 &win
->s
->c
[SWM_S_COLOR_FOCUS
].pixel
;
6053 pixel
= MAXIMIZED(win
) ?
6054 &win
->s
->c
[SWM_S_COLOR_UNFOCUS_MAXIMIZED
].pixel
:
6055 &win
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
;
6057 xcb_change_window_attributes(conn
, win
->id
,
6058 XCB_CW_BORDER_PIXEL
, pixel
);
6062 update_window(struct ws_win
*win
)
6067 mask
= XCB_CONFIG_WINDOW_X
| XCB_CONFIG_WINDOW_Y
|
6068 XCB_CONFIG_WINDOW_WIDTH
| XCB_CONFIG_WINDOW_HEIGHT
|
6069 XCB_CONFIG_WINDOW_BORDER_WIDTH
;
6073 wc
[3] = HEIGHT(win
);
6074 wc
[4] = BORDER(win
);
6076 DNPRINTF(SWM_D_EVENT
, "update_window: win %#x, (x,y) w x h: "
6077 "(%d,%d) %d x %d, bordered: %s\n", win
->id
, wc
[0], wc
[1], wc
[2],
6078 wc
[3], YESNO(win
->bordered
));
6080 xcb_configure_window(conn
, win
->id
, mask
, wc
);
6083 #define SWM_RESIZE_STEPS (50)
6086 resize(struct ws_win
*win
, union arg
*args
)
6088 xcb_timestamp_t timestamp
= 0;
6089 struct swm_region
*r
= NULL
;
6090 struct swm_geometry g
;
6092 int top
= 0, left
= 0, resizing
;
6094 xcb_cursor_t cursor
;
6095 xcb_query_pointer_reply_t
*xpr
= NULL
;
6096 xcb_generic_event_t
*evt
;
6097 xcb_motion_notify_event_t
*mne
;
6103 if (FULLSCREEN(win
))
6106 /* In max_stack mode, should only resize transients. */
6107 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
] && !TRANS(win
))
6110 DNPRINTF(SWM_D_EVENT
, "resize: win %#x, floating: %s, "
6111 "transient: %#x\n", win
->id
, YESNO(ABOVE(win
)),
6115 store_float_geom(win
);
6116 else if (!(TRANS(win
) || ABOVE(win
)))
6119 ewmh_apply_flags(win
, (win
->ewmh_flags
| SWM_F_MANUAL
| EWMH_F_ABOVE
) &
6121 ewmh_update_wm_state(win
);
6127 /* It's possible for win to have been freed during focus_flush(). */
6128 if (validate_win(win
)) {
6129 DNPRINTF(SWM_D_EVENT
, "resize: invalid win.\n");
6134 case SWM_ARG_ID_WIDTHSHRINK
:
6135 WIDTH(win
) -= SWM_RESIZE_STEPS
;
6138 case SWM_ARG_ID_WIDTHGROW
:
6139 WIDTH(win
) += SWM_RESIZE_STEPS
;
6142 case SWM_ARG_ID_HEIGHTSHRINK
:
6143 HEIGHT(win
) -= SWM_RESIZE_STEPS
;
6146 case SWM_ARG_ID_HEIGHTGROW
:
6147 HEIGHT(win
) += SWM_RESIZE_STEPS
;
6154 region_containment(win
, r
, SWM_CW_ALLSIDES
| SWM_CW_RESIZABLE
|
6155 SWM_CW_HARDBOUNDARY
);
6157 store_float_geom(win
);
6161 region_containment(win
, r
, SWM_CW_ALLSIDES
| SWM_CW_RESIZABLE
|
6162 SWM_CW_SOFTBOUNDARY
);
6165 /* get cursor offset from window root */
6166 xpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
6173 if (xpr
->win_x
< WIDTH(win
) / 2)
6176 if (xpr
->win_y
< HEIGHT(win
) / 2)
6179 if (args
->id
== SWM_ARG_ID_CENTER
)
6180 cursor
= cursors
[XC_SIZING
].cid
;
6182 cursor
= cursors
[left
? XC_TOP_LEFT_CORNER
:
6183 XC_TOP_RIGHT_CORNER
].cid
;
6185 cursor
= cursors
[left
? XC_BOTTOM_LEFT_CORNER
:
6186 XC_BOTTOM_RIGHT_CORNER
].cid
;
6188 xcb_grab_pointer(conn
, 0, win
->id
, MOUSEMASK
,
6189 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
, XCB_WINDOW_NONE
, cursor
,
6194 while (resizing
&& (evt
= xcb_wait_for_event(conn
))) {
6195 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
6196 case XCB_BUTTON_RELEASE
:
6197 DNPRINTF(SWM_D_EVENT
, "resize: BUTTON_RELEASE\n");
6200 case XCB_MOTION_NOTIFY
:
6201 mne
= (xcb_motion_notify_event_t
*)evt
;
6202 /* cursor offset/delta from start of the operation */
6203 dx
= mne
->root_x
- xpr
->root_x
;
6204 dy
= mne
->root_y
- xpr
->root_y
;
6210 if (args
->id
== SWM_ARG_ID_CENTER
) {
6211 if (g
.h
/ 2 + dy
< 1)
6215 HEIGHT(win
) = g
.h
+ 2 * dy
;
6223 HEIGHT(win
) = g
.h
+ dy
;
6230 if (args
->id
== SWM_ARG_ID_CENTER
) {
6231 if (g
.w
/ 2 + dx
< 1)
6235 WIDTH(win
) = g
.w
+ 2 * dx
;
6243 WIDTH(win
) = g
.w
+ dx
;
6246 /* not free, don't sync more than 120 times / second */
6247 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
6248 timestamp
= mne
->time
;
6249 regionize(win
, mne
->root_x
, mne
->root_y
);
6250 region_containment(win
, r
, SWM_CW_ALLSIDES
|
6251 SWM_CW_RESIZABLE
| SWM_CW_HARDBOUNDARY
|
6252 SWM_CW_SOFTBOUNDARY
);
6259 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
6260 ((xcb_key_press_event_t
*)evt
)->time
);
6266 /* It's possible for win to have been freed above. */
6267 if (validate_win(win
)) {
6268 DNPRINTF(SWM_D_EVENT
, "resize: invalid win.\n");
6276 region_containment(win
, r
, SWM_CW_ALLSIDES
| SWM_CW_RESIZABLE
|
6277 SWM_CW_HARDBOUNDARY
| SWM_CW_SOFTBOUNDARY
);
6281 store_float_geom(win
);
6283 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
6285 DNPRINTF(SWM_D_EVENT
, "resize: done.\n");
6289 resize_step(struct swm_region
*r
, union arg
*args
)
6291 struct ws_win
*win
= NULL
;
6293 if (r
&& r
->ws
&& r
->ws
->focus
)
6302 /* Try to set window region based on supplied coordinates or window center. */
6304 regionize(struct ws_win
*win
, int x
, int y
)
6306 struct swm_region
*r
= NULL
;
6308 r
= region_under(win
->s
, x
, y
);
6310 r
= region_under(win
->s
, X(win
) + WIDTH(win
) / 2,
6311 Y(win
) + HEIGHT(win
) / 2);
6313 if (r
!= NULL
&& r
!= win
->ws
->r
) {
6314 if (clear_maximized(r
->ws
) > 0)
6317 win_to_ws(win
, r
->ws
->idx
, 0);
6319 /* Set focus on new ws. */
6320 unfocus_win(r
->ws
->focus
);
6328 #define SWM_MOVE_STEPS (50)
6331 move(struct ws_win
*win
, union arg
*args
)
6333 struct swm_region
*r
;
6334 xcb_timestamp_t timestamp
= 0;
6335 int move_stp
= 0, moving
, restack
= 0;
6336 xcb_query_pointer_reply_t
*qpr
= NULL
;
6337 xcb_generic_event_t
*evt
;
6338 xcb_motion_notify_event_t
*mne
;
6343 if ((r
= win
->ws
->r
) == NULL
)
6346 if (FULLSCREEN(win
))
6349 DNPRINTF(SWM_D_EVENT
, "move: win %#x, floating: %s, transient: "
6350 "%#x\n", win
->id
, YESNO(ABOVE(win
)), win
->transient
);
6352 /* in max_stack mode should only move transients */
6353 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
] && !TRANS(win
))
6356 if (!(ABOVE(win
) || TRANS(win
)) || MAXIMIZED(win
)) {
6357 store_float_geom(win
);
6361 ewmh_apply_flags(win
, (win
->ewmh_flags
| SWM_F_MANUAL
| EWMH_F_ABOVE
) &
6363 ewmh_update_wm_state(win
);
6370 /* It's possible for win to have been freed during focus_flush(). */
6371 if (validate_win(win
)) {
6372 DNPRINTF(SWM_D_EVENT
, "move: invalid win.\n");
6378 case SWM_ARG_ID_MOVELEFT
:
6379 X(win
) -= (SWM_MOVE_STEPS
- border_width
);
6382 case SWM_ARG_ID_MOVERIGHT
:
6383 X(win
) += (SWM_MOVE_STEPS
- border_width
);
6386 case SWM_ARG_ID_MOVEUP
:
6387 Y(win
) -= (SWM_MOVE_STEPS
- border_width
);
6390 case SWM_ARG_ID_MOVEDOWN
:
6391 Y(win
) += (SWM_MOVE_STEPS
- border_width
);
6398 regionize(win
, -1, -1);
6399 region_containment(win
, win
->ws
->r
, SWM_CW_ALLSIDES
);
6401 store_float_geom(win
);
6405 xcb_grab_pointer(conn
, 0, win
->id
, MOUSEMASK
,
6406 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
,
6407 XCB_WINDOW_NONE
, cursors
[XC_FLEUR
].cid
, XCB_CURRENT_TIME
);
6409 /* get cursor offset from window root */
6410 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
6413 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
6417 regionize(win
, qpr
->root_x
, qpr
->root_y
);
6418 region_containment(win
, win
->ws
->r
, SWM_CW_ALLSIDES
|
6419 SWM_CW_SOFTBOUNDARY
);
6423 while (moving
&& (evt
= xcb_wait_for_event(conn
))) {
6424 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
6425 case XCB_BUTTON_RELEASE
:
6426 DNPRINTF(SWM_D_EVENT
, "move: BUTTON_RELEASE\n");
6429 case XCB_MOTION_NOTIFY
:
6430 mne
= (xcb_motion_notify_event_t
*)evt
;
6431 DNPRINTF(SWM_D_EVENT
, "motion: root: %#x\n", mne
->root
);
6432 X(win
) = mne
->root_x
- qpr
->win_x
- border_width
;
6433 Y(win
) = mne
->root_y
- qpr
->win_y
- border_width
;
6435 /* not free, don't sync more than 120 times / second */
6436 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
6437 timestamp
= mne
->time
;
6438 regionize(win
, mne
->root_x
, mne
->root_y
);
6439 region_containment(win
, win
->ws
->r
,
6440 SWM_CW_ALLSIDES
| SWM_CW_SOFTBOUNDARY
);
6447 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
6448 ((xcb_key_press_event_t
*)evt
)->time
);
6454 /* It's possible for win to have been freed above. */
6455 if (validate_win(win
)) {
6456 DNPRINTF(SWM_D_EVENT
, "move: invalid win.\n");
6464 region_containment(win
, win
->ws
->r
, SWM_CW_ALLSIDES
|
6465 SWM_CW_SOFTBOUNDARY
);
6469 store_float_geom(win
);
6471 /* New region set to fullscreen layout. */
6472 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
]) {
6479 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
6480 DNPRINTF(SWM_D_EVENT
, "move: done.\n");
6484 move_step(struct swm_region
*r
, union arg
*args
)
6486 struct ws_win
*win
= NULL
;
6488 if (r
&& r
->ws
&& r
->ws
->focus
)
6493 if (!TRANS(win
) && !ABOVE(win
))
6500 /* key definitions */
6502 char name
[SWM_FUNCNAME_LEN
];
6503 void (*func
)(struct swm_region
*r
, union arg
*);
6505 } keyfuncs
[KF_INVALID
+ 1] = {
6506 /* name function argument */
6507 { "bar_toggle", bar_toggle
, {.id
= SWM_ARG_ID_BAR_TOGGLE
} },
6508 { "bar_toggle_ws", bar_toggle
, {.id
= SWM_ARG_ID_BAR_TOGGLE_WS
} },
6509 { "button2", pressbutton
, {2} },
6510 { "cycle_layout", cycle_layout
, {0} },
6511 { "flip_layout", stack_config
, {.id
= SWM_ARG_ID_FLIPLAYOUT
} },
6512 { "float_toggle", floating_toggle
,{0} },
6513 { "focus_main", focus
, {.id
= SWM_ARG_ID_FOCUSMAIN
} },
6514 { "focus_next", focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
6515 { "focus_prev", focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
6516 { "focus_urgent", focus
, {.id
= SWM_ARG_ID_FOCUSURGENT
} },
6517 { "maximize_toggle", maximize_toggle
,{0} },
6518 { "height_grow", resize_step
, {.id
= SWM_ARG_ID_HEIGHTGROW
} },
6519 { "height_shrink", resize_step
, {.id
= SWM_ARG_ID_HEIGHTSHRINK
} },
6520 { "iconify", iconify
, {0} },
6521 { "master_shrink", stack_config
, {.id
= SWM_ARG_ID_MASTERSHRINK
} },
6522 { "master_grow", stack_config
, {.id
= SWM_ARG_ID_MASTERGROW
} },
6523 { "master_add", stack_config
, {.id
= SWM_ARG_ID_MASTERADD
} },
6524 { "master_del", stack_config
, {.id
= SWM_ARG_ID_MASTERDEL
} },
6525 { "move_down", move_step
, {.id
= SWM_ARG_ID_MOVEDOWN
} },
6526 { "move_left", move_step
, {.id
= SWM_ARG_ID_MOVELEFT
} },
6527 { "move_right", move_step
, {.id
= SWM_ARG_ID_MOVERIGHT
} },
6528 { "move_up", move_step
, {.id
= SWM_ARG_ID_MOVEUP
} },
6529 { "mvrg_1", send_to_rg
, {.id
= 0} },
6530 { "mvrg_2", send_to_rg
, {.id
= 1} },
6531 { "mvrg_3", send_to_rg
, {.id
= 2} },
6532 { "mvrg_4", send_to_rg
, {.id
= 3} },
6533 { "mvrg_5", send_to_rg
, {.id
= 4} },
6534 { "mvrg_6", send_to_rg
, {.id
= 5} },
6535 { "mvrg_7", send_to_rg
, {.id
= 6} },
6536 { "mvrg_8", send_to_rg
, {.id
= 7} },
6537 { "mvrg_9", send_to_rg
, {.id
= 8} },
6538 { "mvws_1", send_to_ws
, {.id
= 0} },
6539 { "mvws_2", send_to_ws
, {.id
= 1} },
6540 { "mvws_3", send_to_ws
, {.id
= 2} },
6541 { "mvws_4", send_to_ws
, {.id
= 3} },
6542 { "mvws_5", send_to_ws
, {.id
= 4} },
6543 { "mvws_6", send_to_ws
, {.id
= 5} },
6544 { "mvws_7", send_to_ws
, {.id
= 6} },
6545 { "mvws_8", send_to_ws
, {.id
= 7} },
6546 { "mvws_9", send_to_ws
, {.id
= 8} },
6547 { "mvws_10", send_to_ws
, {.id
= 9} },
6548 { "mvws_11", send_to_ws
, {.id
= 10} },
6549 { "mvws_12", send_to_ws
, {.id
= 11} },
6550 { "mvws_13", send_to_ws
, {.id
= 12} },
6551 { "mvws_14", send_to_ws
, {.id
= 13} },
6552 { "mvws_15", send_to_ws
, {.id
= 14} },
6553 { "mvws_16", send_to_ws
, {.id
= 15} },
6554 { "mvws_17", send_to_ws
, {.id
= 16} },
6555 { "mvws_18", send_to_ws
, {.id
= 17} },
6556 { "mvws_19", send_to_ws
, {.id
= 18} },
6557 { "mvws_20", send_to_ws
, {.id
= 19} },
6558 { "mvws_21", send_to_ws
, {.id
= 20} },
6559 { "mvws_22", send_to_ws
, {.id
= 21} },
6560 { "name_workspace", name_workspace
, {0} },
6561 { "quit", quit
, {0} },
6562 { "raise_toggle", raise_toggle
, {0} },
6563 { "restart", restart
, {0} },
6564 { "rg_1", focusrg
, {.id
= 0} },
6565 { "rg_2", focusrg
, {.id
= 1} },
6566 { "rg_3", focusrg
, {.id
= 2} },
6567 { "rg_4", focusrg
, {.id
= 3} },
6568 { "rg_5", focusrg
, {.id
= 4} },
6569 { "rg_6", focusrg
, {.id
= 5} },
6570 { "rg_7", focusrg
, {.id
= 6} },
6571 { "rg_8", focusrg
, {.id
= 7} },
6572 { "rg_9", focusrg
, {.id
= 8} },
6573 { "rg_next", cyclerg
, {.id
= SWM_ARG_ID_CYCLERG_UP
} },
6574 { "rg_prev", cyclerg
, {.id
= SWM_ARG_ID_CYCLERG_DOWN
} },
6575 { "screen_next", cyclerg
, {.id
= SWM_ARG_ID_CYCLERG_UP
} },
6576 { "screen_prev", cyclerg
, {.id
= SWM_ARG_ID_CYCLERG_DOWN
} },
6577 { "search_win", search_win
, {0} },
6578 { "search_workspace", search_workspace
, {0} },
6579 { "spawn_custom", NULL
, {0} },
6580 { "stack_inc", stack_config
, {.id
= SWM_ARG_ID_STACKINC
} },
6581 { "stack_dec", stack_config
, {.id
= SWM_ARG_ID_STACKDEC
} },
6582 { "stack_reset", stack_config
, {.id
= SWM_ARG_ID_STACKRESET
} },
6583 { "swap_main", swapwin
, {.id
= SWM_ARG_ID_SWAPMAIN
} },
6584 { "swap_next", swapwin
, {.id
= SWM_ARG_ID_SWAPNEXT
} },
6585 { "swap_prev", swapwin
, {.id
= SWM_ARG_ID_SWAPPREV
} },
6586 { "uniconify", uniconify
, {0} },
6587 { "version", version
, {0} },
6588 { "width_grow", resize_step
, {.id
= SWM_ARG_ID_WIDTHGROW
} },
6589 { "width_shrink", resize_step
, {.id
= SWM_ARG_ID_WIDTHSHRINK
} },
6590 { "wind_del", wkill
, {.id
= SWM_ARG_ID_DELETEWINDOW
} },
6591 { "wind_kill", wkill
, {.id
= SWM_ARG_ID_KILLWINDOW
} },
6592 { "ws_1", switchws
, {.id
= 0} },
6593 { "ws_2", switchws
, {.id
= 1} },
6594 { "ws_3", switchws
, {.id
= 2} },
6595 { "ws_4", switchws
, {.id
= 3} },
6596 { "ws_5", switchws
, {.id
= 4} },
6597 { "ws_6", switchws
, {.id
= 5} },
6598 { "ws_7", switchws
, {.id
= 6} },
6599 { "ws_8", switchws
, {.id
= 7} },
6600 { "ws_9", switchws
, {.id
= 8} },
6601 { "ws_10", switchws
, {.id
= 9} },
6602 { "ws_11", switchws
, {.id
= 10} },
6603 { "ws_12", switchws
, {.id
= 11} },
6604 { "ws_13", switchws
, {.id
= 12} },
6605 { "ws_14", switchws
, {.id
= 13} },
6606 { "ws_15", switchws
, {.id
= 14} },
6607 { "ws_16", switchws
, {.id
= 15} },
6608 { "ws_17", switchws
, {.id
= 16} },
6609 { "ws_18", switchws
, {.id
= 17} },
6610 { "ws_19", switchws
, {.id
= 18} },
6611 { "ws_20", switchws
, {.id
= 19} },
6612 { "ws_21", switchws
, {.id
= 20} },
6613 { "ws_22", switchws
, {.id
= 21} },
6614 { "ws_next", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP
} },
6615 { "ws_next_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP_ALL
} },
6616 { "ws_next_move", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_MOVE_UP
} },
6617 { "ws_prev", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN
} },
6618 { "ws_prev_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN_ALL
} },
6619 { "ws_prev_move", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_MOVE_DOWN
} },
6620 { "ws_prior", priorws
, {0} },
6621 { "dumpwins", dumpwins
, {0} }, /* MUST BE LAST */
6622 { "invalid key func", NULL
, {0} },
6626 key_cmp(struct key
*kp1
, struct key
*kp2
)
6628 if (kp1
->keysym
< kp2
->keysym
)
6630 if (kp1
->keysym
> kp2
->keysym
)
6633 if (kp1
->mod
< kp2
->mod
)
6635 if (kp1
->mod
> kp2
->mod
)
6642 enum { client_click
, root_click
};
6644 unsigned int action
;
6646 unsigned int button
;
6647 void (*func
)(struct ws_win
*, union arg
*);
6650 #define MODKEY_SHIFT MODKEY | XCB_MOD_MASK_SHIFT
6651 /* action key mouse button func args */
6652 { client_click
, MODKEY
, XCB_BUTTON_INDEX_3
, resize
, {.id
= SWM_ARG_ID_DONTCENTER
} },
6653 { client_click
, MODKEY_SHIFT
, XCB_BUTTON_INDEX_3
, resize
, {.id
= SWM_ARG_ID_CENTER
} },
6654 { client_click
, MODKEY
, XCB_BUTTON_INDEX_1
, move
, {0} },
6659 update_modkey(unsigned int mod
)
6665 RB_FOREACH(kp
, key_tree
, &keys
)
6666 if (kp
->mod
& XCB_MOD_MASK_SHIFT
)
6667 kp
->mod
= mod
| XCB_MOD_MASK_SHIFT
;
6671 for (i
= 0; i
< LENGTH(buttons
); i
++)
6672 if (buttons
[i
].mask
& XCB_MOD_MASK_SHIFT
)
6673 buttons
[i
].mask
= mod
| XCB_MOD_MASK_SHIFT
;
6675 buttons
[i
].mask
= mod
;
6679 spawn_expand(struct swm_region
*r
, union arg
*args
, const char *spawn_name
,
6682 struct spawn_prog
*prog
= NULL
;
6684 char *ap
, **real_args
;
6686 /* suppress unused warning since var is needed */
6689 DNPRINTF(SWM_D_SPAWN
, "spawn_expand: %s\n", spawn_name
);
6692 TAILQ_FOREACH(prog
, &spawns
, entry
) {
6693 if (strcasecmp(spawn_name
, prog
->name
) == 0)
6697 warnx("spawn_custom: program %s not found", spawn_name
);
6701 /* make room for expanded args */
6702 if ((real_args
= calloc(prog
->argc
+ 1, sizeof(char *))) == NULL
)
6703 err(1, "spawn_custom: calloc real_args");
6705 /* expand spawn_args into real_args */
6706 for (i
= c
= 0; i
< prog
->argc
; i
++) {
6708 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: raw arg: %s\n", ap
);
6709 if (strcasecmp(ap
, "$bar_border") == 0) {
6711 strdup(r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].name
))
6713 err(1, "spawn_custom border color");
6714 } else if (strcasecmp(ap
, "$bar_color") == 0) {
6716 strdup(r
->s
->c
[SWM_S_COLOR_BAR
].name
))
6718 err(1, "spawn_custom bar color");
6719 } else if (strcasecmp(ap
, "$bar_font") == 0) {
6720 if ((real_args
[c
] = strdup(bar_fonts
))
6722 err(1, "spawn_custom bar fonts");
6723 } else if (strcasecmp(ap
, "$bar_font_color") == 0) {
6725 strdup(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].name
))
6727 err(1, "spawn_custom color font");
6728 } else if (strcasecmp(ap
, "$color_focus") == 0) {
6730 strdup(r
->s
->c
[SWM_S_COLOR_FOCUS
].name
))
6732 err(1, "spawn_custom color focus");
6733 } else if (strcasecmp(ap
, "$color_focus_maximized") == 0) {
6735 strdup(r
->s
->c
[SWM_S_COLOR_FOCUS_MAXIMIZED
].name
))
6737 err(1, "spawn_custom color focus maximized");
6738 } else if (strcasecmp(ap
, "$color_unfocus") == 0) {
6740 strdup(r
->s
->c
[SWM_S_COLOR_UNFOCUS
].name
))
6742 err(1, "spawn_custom color unfocus");
6743 } else if (strcasecmp(ap
, "$color_unfocus_maximized") == 0) {
6745 strdup(r
->s
->c
[SWM_S_COLOR_UNFOCUS_MAXIMIZED
].name
))
6747 err(1, "spawn_custom color unfocus maximized");
6748 } else if (strcasecmp(ap
, "$region_index") == 0) {
6749 if (asprintf(&real_args
[c
], "%d",
6750 get_region_index(r
) + 1) < 1)
6751 err(1, "spawn_custom region index");
6752 } else if (strcasecmp(ap
, "$workspace_index") == 0) {
6753 if (asprintf(&real_args
[c
], "%d", r
->ws
->idx
+ 1) < 1)
6754 err(1, "spawn_custom workspace index");
6755 } else if (strcasecmp(ap
, "$dmenu_bottom") == 0) {
6758 if ((real_args
[c
] = strdup("-b")) == NULL
)
6759 err(1, "spawn_custom workspace index");
6761 /* no match --> copy as is */
6762 if ((real_args
[c
] = strdup(ap
)) == NULL
)
6763 err(1, "spawn_custom strdup(ap)");
6765 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: cooked arg: %s\n",
6771 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: result: ");
6772 for (i
= 0; i
< c
; ++i
)
6773 DPRINTF("\"%s\" ", real_args
[i
]);
6776 *ret_args
= real_args
;
6781 spawn_custom(struct swm_region
*r
, union arg
*args
, const char *spawn_name
)
6787 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
6791 spawn(r
->ws
->idx
, &a
, 1);
6793 for (i
= 0; i
< spawn_argc
; i
++)
6799 spawn_select(struct swm_region
*r
, union arg
*args
, const char *spawn_name
,
6806 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
6810 if (pipe(select_list_pipe
) == -1)
6811 err(1, "pipe error");
6812 if (pipe(select_resp_pipe
) == -1)
6813 err(1, "pipe error");
6815 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
6816 err(1, "could not disable SIGPIPE");
6817 switch (*pid
= fork()) {
6819 err(1, "cannot fork");
6822 if (dup2(select_list_pipe
[0], STDIN_FILENO
) == -1)
6824 if (dup2(select_resp_pipe
[1], STDOUT_FILENO
) == -1)
6826 close(select_list_pipe
[1]);
6827 close(select_resp_pipe
[0]);
6828 spawn(r
->ws
->idx
, &a
, 0);
6830 default: /* parent */
6831 close(select_list_pipe
[0]);
6832 close(select_resp_pipe
[1]);
6836 for (i
= 0; i
< spawn_argc
; i
++)
6841 /* Argument tokenizer. */
6844 int single_quoted
= 0, double_quoted
= 0;
6845 char *arg
, *cp
, *next
;
6850 /* Eat and move characters until end of argument is found. */
6851 for (arg
= next
= cp
= *sp
; *cp
!= '\0'; ++cp
) {
6852 if (!double_quoted
&& *cp
== '\'') {
6853 /* Eat single-quote. */
6854 single_quoted
= !single_quoted
;
6855 } else if (!single_quoted
&& *cp
== '"') {
6856 /* Eat double-quote. */
6857 double_quoted
= !double_quoted
;
6858 } else if (!single_quoted
&& *cp
== '\\' && *(cp
+ 1) == '"') {
6859 /* Eat backslash; copy escaped character to arg. */
6861 } else if (!single_quoted
&& !double_quoted
&& *cp
== '\\' &&
6862 (*(cp
+ 1) == '\'' || *(cp
+ 1) == ' ')) {
6863 /* Eat backslash; move escaped character. */
6865 } else if (!single_quoted
&& !double_quoted
&&
6866 (*cp
== ' ' || *cp
== '\t')) {
6867 /* Terminate argument. */
6869 /* Point sp to beginning of next argument. */
6873 /* Move regular character. */
6878 /* Terminate argument if end of string. */
6888 spawn_insert(const char *name
, const char *args
, int flags
)
6890 struct spawn_prog
*sp
;
6891 char *arg
, *cp
, *ptr
;
6893 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: %s[%s]\n", name
, args
);
6895 if (args
== NULL
|| *args
== '\0')
6898 if ((sp
= calloc(1, sizeof *sp
)) == NULL
)
6899 err(1, "spawn_insert: calloc");
6900 if ((sp
->name
= strdup(name
)) == NULL
)
6901 err(1, "spawn_insert: strdup");
6903 /* Convert the arguments to an argument list. */
6904 if ((ptr
= cp
= strdup(args
)) == NULL
)
6905 err(1, "spawn_insert: strdup");
6906 while ((arg
= argsep(&ptr
)) != NULL
) {
6907 /* Null argument; skip it. */
6912 if ((sp
->argv
= realloc(sp
->argv
, sp
->argc
*
6913 sizeof *sp
->argv
)) == NULL
)
6914 err(1, "spawn_insert: realloc");
6915 if ((sp
->argv
[sp
->argc
- 1] = strdup(arg
)) == NULL
)
6916 err(1, "spawn_insert: strdup");
6922 DNPRINTF(SWM_D_SPAWN
, "arg %d: [%s]\n", sp
->argc
, sp
->argv
[sp
->argc
-1]);
6923 TAILQ_INSERT_TAIL(&spawns
, sp
, entry
);
6924 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: leave\n");
6928 spawn_remove(struct spawn_prog
*sp
)
6932 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: %s\n", sp
->name
);
6934 TAILQ_REMOVE(&spawns
, sp
, entry
);
6935 for (i
= 0; i
< sp
->argc
; i
++)
6941 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: leave\n");
6945 spawn_find(const char *name
)
6947 struct spawn_prog
*sp
;
6949 TAILQ_FOREACH(sp
, &spawns
, entry
)
6950 if (strcasecmp(sp
->name
, name
) == 0)
6957 setspawn(const char *name
, const char *args
, int flags
)
6959 struct spawn_prog
*sp
;
6961 DNPRINTF(SWM_D_SPAWN
, "setspawn: %s\n", name
);
6966 /* Remove any old spawn under the same name. */
6967 if ((sp
= spawn_find(name
)) != NULL
)
6971 spawn_insert(name
, args
, flags
);
6973 warnx("error: setspawn: cannot find program: %s", name
);
6975 DNPRINTF(SWM_D_SPAWN
, "setspawn: leave\n");
6979 setconfspawn(const char *selector
, const char *value
, int flags
)
6983 if (selector
== NULL
|| strlen(selector
) == 0)
6986 args
= expand_tilde(value
);
6988 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: [%s] [%s]\n", selector
, args
);
6990 setspawn(selector
, args
, flags
);
6993 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: done.\n");
6998 validate_spawns(void)
7000 struct spawn_prog
*sp
;
7001 char which
[PATH_MAX
];
7006 RB_FOREACH(kp
, key_tree
, &keys
) {
7007 if (kp
->funcid
!= KF_SPAWN_CUSTOM
)
7011 sp
= spawn_find(kp
->spawn_name
);
7012 if (sp
== NULL
|| sp
->flags
& SWM_SPAWN_OPTIONAL
)
7015 /* verify we have the goods */
7016 snprintf(which
, sizeof which
, "which %s", sp
->argv
[0]);
7017 DNPRINTF(SWM_D_CONF
, "validate_spawns: which %s\n",
7019 for (i
= strlen("which "); i
< strlen(which
); i
++)
7020 if (which
[i
] == ' ') {
7024 if (system(which
) != 0)
7025 add_startup_exception("could not find %s",
7026 &which
[strlen("which ")]);
7033 setconfspawn("lock", "xlock", 0);
7035 setconfspawn("term", "xterm", 0);
7036 setconfspawn("spawn_term", "xterm", 0);
7038 setconfspawn("menu", "dmenu_run"
7042 " -nf $bar_font_color"
7044 " -sf $bar_color", 0);
7046 setconfspawn("search", "dmenu"
7051 " -nf $bar_font_color"
7053 " -sf $bar_color", 0);
7055 setconfspawn("name_workspace", "dmenu"
7060 " -nf $bar_font_color"
7062 " -sf $bar_color", 0);
7064 /* These are not verified for existence, even with a binding set. */
7065 setconfspawn("screenshot_all", "screenshot.sh full", SWM_SPAWN_OPTIONAL
);
7066 setconfspawn("screenshot_wind", "screenshot.sh window", SWM_SPAWN_OPTIONAL
);
7067 setconfspawn("initscr", "initscreen.sh", SWM_SPAWN_OPTIONAL
);
7071 #define SWM_MODNAME_SIZE 32
7072 #define SWM_KEY_WS "\n+ \t"
7074 parsekeys(const char *keystr
, unsigned int currmod
, unsigned int *mod
, KeySym
*ks
)
7076 char *str
, *cp
, *name
;
7079 DNPRINTF(SWM_D_KEY
, "parsekeys: enter [%s]\n", keystr
);
7080 if (mod
== NULL
|| ks
== NULL
) {
7081 DNPRINTF(SWM_D_KEY
, "parsekeys: no mod or key vars\n");
7084 if (keystr
== NULL
|| strlen(keystr
) == 0) {
7085 DNPRINTF(SWM_D_KEY
, "parsekeys: no keystr\n");
7089 if ((cp
= str
= strdup(keystr
)) == NULL
)
7090 err(1, "parsekeys: strdup");
7092 *ks
= XCB_NO_SYMBOL
;
7094 while ((name
= strsep(&cp
, SWM_KEY_WS
)) != NULL
) {
7095 DNPRINTF(SWM_D_KEY
, "parsekeys: key [%s]\n", name
);
7097 cp
+= (long)strspn(cp
, SWM_KEY_WS
);
7098 if (strncasecmp(name
, "MOD", SWM_MODNAME_SIZE
) == 0)
7100 else if (strncasecmp(name
, "Mod1", SWM_MODNAME_SIZE
) == 0)
7101 *mod
|= XCB_MOD_MASK_1
;
7102 else if (strncasecmp(name
, "Mod2", SWM_MODNAME_SIZE
) == 0)
7103 *mod
+= XCB_MOD_MASK_2
;
7104 else if (strncmp(name
, "Mod3", SWM_MODNAME_SIZE
) == 0)
7105 *mod
|= XCB_MOD_MASK_3
;
7106 else if (strncmp(name
, "Mod4", SWM_MODNAME_SIZE
) == 0)
7107 *mod
|= XCB_MOD_MASK_4
;
7108 else if (strncasecmp(name
, "SHIFT", SWM_MODNAME_SIZE
) == 0)
7109 *mod
|= XCB_MOD_MASK_SHIFT
;
7110 else if (strncasecmp(name
, "CONTROL", SWM_MODNAME_SIZE
) == 0)
7111 *mod
|= XCB_MOD_MASK_CONTROL
;
7113 *ks
= XStringToKeysym(name
);
7114 XConvertCase(*ks
, ks
, &uks
);
7115 if (ks
== XCB_NO_SYMBOL
) {
7117 "parsekeys: invalid key %s\n",
7126 DNPRINTF(SWM_D_KEY
, "parsekeys: leave ok\n");
7131 strdupsafe(const char *str
)
7136 return (strdup(str
));
7140 key_insert(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
7141 const char *spawn_name
)
7145 DNPRINTF(SWM_D_KEY
, "key_insert: enter %s [%s]\n",
7146 keyfuncs
[kfid
].name
, spawn_name
);
7148 if ((kp
= malloc(sizeof *kp
)) == NULL
)
7149 err(1, "key_insert: malloc");
7154 kp
->spawn_name
= strdupsafe(spawn_name
);
7155 RB_INSERT(key_tree
, &keys
, kp
);
7157 DNPRINTF(SWM_D_KEY
, "key_insert: leave\n");
7161 key_lookup(unsigned int mod
, KeySym ks
)
7168 return (RB_FIND(key_tree
, &keys
, &kp
));
7172 key_remove(struct key
*kp
)
7174 DNPRINTF(SWM_D_KEY
, "key_remove: %s\n", keyfuncs
[kp
->funcid
].name
);
7176 RB_REMOVE(key_tree
, &keys
, kp
);
7177 free(kp
->spawn_name
);
7180 DNPRINTF(SWM_D_KEY
, "key_remove: leave\n");
7184 key_replace(struct key
*kp
, unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
7185 const char *spawn_name
)
7187 DNPRINTF(SWM_D_KEY
, "key_replace: %s [%s]\n", keyfuncs
[kp
->funcid
].name
,
7191 key_insert(mod
, ks
, kfid
, spawn_name
);
7193 DNPRINTF(SWM_D_KEY
, "key_replace: leave\n");
7197 setkeybinding(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
7198 const char *spawn_name
)
7202 DNPRINTF(SWM_D_KEY
, "setkeybinding: enter %s [%s]\n",
7203 keyfuncs
[kfid
].name
, spawn_name
);
7205 if ((kp
= key_lookup(mod
, ks
)) != NULL
) {
7206 if (kfid
== KF_INVALID
)
7209 key_replace(kp
, mod
, ks
, kfid
, spawn_name
);
7210 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
7213 if (kfid
== KF_INVALID
) {
7214 warnx("bind: Key combination already unbound.");
7215 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
7219 key_insert(mod
, ks
, kfid
, spawn_name
);
7220 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
7224 setconfbinding(const char *selector
, const char *value
, int flags
)
7226 enum keyfuncid kfid
;
7229 struct spawn_prog
*sp
;
7231 /* suppress unused warning since var is needed */
7234 DNPRINTF(SWM_D_KEY
, "setconfbinding: enter selector: [%s], "
7235 "value: [%s]\n", selector
, value
);
7236 if (selector
== NULL
|| strlen(selector
) == 0) {
7237 DNPRINTF(SWM_D_KEY
, "setconfbinding: unbind %s\n", value
);
7238 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
7240 setkeybinding(mod
, ks
, kfid
, NULL
);
7245 /* search by key function name */
7246 for (kfid
= 0; kfid
< KF_INVALID
; (kfid
)++) {
7247 if (strncasecmp(selector
, keyfuncs
[kfid
].name
,
7248 SWM_FUNCNAME_LEN
) == 0) {
7249 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match "
7250 "keyfunc\n", selector
);
7251 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
7252 setkeybinding(mod
, ks
, kfid
, NULL
);
7258 /* search by custom spawn name */
7259 if ((sp
= spawn_find(selector
)) != NULL
) {
7260 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match "
7261 "spawn\n", selector
);
7262 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
7263 setkeybinding(mod
, ks
, KF_SPAWN_CUSTOM
,
7269 DNPRINTF(SWM_D_KEY
, "setconfbinding: no match\n");
7276 #define MODKEY_SHIFT MODKEY | XCB_MOD_MASK_SHIFT
7277 setkeybinding(MODKEY
, XK_b
, KF_BAR_TOGGLE
, NULL
);
7278 setkeybinding(MODKEY_SHIFT
, XK_b
, KF_BAR_TOGGLE_WS
,NULL
);
7279 setkeybinding(MODKEY
, XK_v
, KF_BUTTON2
, NULL
);
7280 setkeybinding(MODKEY
, XK_space
, KF_CYCLE_LAYOUT
,NULL
);
7281 setkeybinding(MODKEY_SHIFT
, XK_backslash
, KF_FLIP_LAYOUT
, NULL
);
7282 setkeybinding(MODKEY
, XK_t
, KF_FLOAT_TOGGLE
,NULL
);
7283 setkeybinding(MODKEY
, XK_m
, KF_FOCUS_MAIN
, NULL
);
7284 setkeybinding(MODKEY
, XK_j
, KF_FOCUS_NEXT
, NULL
);
7285 setkeybinding(MODKEY
, XK_Tab
, KF_FOCUS_NEXT
, NULL
);
7286 setkeybinding(MODKEY
, XK_k
, KF_FOCUS_PREV
, NULL
);
7287 setkeybinding(MODKEY_SHIFT
, XK_Tab
, KF_FOCUS_PREV
, NULL
);
7288 setkeybinding(MODKEY
, XK_u
, KF_FOCUS_URGENT
,NULL
);
7289 setkeybinding(MODKEY
, XK_e
, KF_MAXIMIZE_TOGGLE
,NULL
);
7290 setkeybinding(MODKEY_SHIFT
, XK_equal
, KF_HEIGHT_GROW
,NULL
);
7291 setkeybinding(MODKEY_SHIFT
, XK_minus
, KF_HEIGHT_SHRINK
,NULL
);
7292 setkeybinding(MODKEY
, XK_w
, KF_ICONIFY
, NULL
);
7293 setkeybinding(MODKEY
, XK_h
, KF_MASTER_SHRINK
, NULL
);
7294 setkeybinding(MODKEY
, XK_l
, KF_MASTER_GROW
, NULL
);
7295 setkeybinding(MODKEY
, XK_comma
, KF_MASTER_ADD
, NULL
);
7296 setkeybinding(MODKEY
, XK_period
, KF_MASTER_DEL
, NULL
);
7297 setkeybinding(MODKEY_SHIFT
, XK_bracketright
,KF_MOVE_DOWN
,NULL
);
7298 setkeybinding(MODKEY
, XK_bracketleft
, KF_MOVE_LEFT
,NULL
);
7299 setkeybinding(MODKEY
, XK_bracketright
,KF_MOVE_RIGHT
,NULL
);
7300 setkeybinding(MODKEY_SHIFT
, XK_bracketleft
, KF_MOVE_UP
, NULL
);
7301 setkeybinding(MODKEY_SHIFT
, XK_KP_End
, KF_MVRG_1
, NULL
);
7302 setkeybinding(MODKEY_SHIFT
, XK_KP_Down
, KF_MVRG_2
, NULL
);
7303 setkeybinding(MODKEY_SHIFT
, XK_KP_Next
, KF_MVRG_3
, NULL
);
7304 setkeybinding(MODKEY_SHIFT
, XK_KP_Left
, KF_MVRG_4
, NULL
);
7305 setkeybinding(MODKEY_SHIFT
, XK_KP_Begin
, KF_MVRG_5
, NULL
);
7306 setkeybinding(MODKEY_SHIFT
, XK_KP_Right
, KF_MVRG_6
, NULL
);
7307 setkeybinding(MODKEY_SHIFT
, XK_KP_Home
, KF_MVRG_7
, NULL
);
7308 setkeybinding(MODKEY_SHIFT
, XK_KP_Up
, KF_MVRG_8
, NULL
);
7309 setkeybinding(MODKEY_SHIFT
, XK_KP_Prior
, KF_MVRG_9
, NULL
);
7310 setkeybinding(MODKEY_SHIFT
, XK_1
, KF_MVWS_1
, NULL
);
7311 setkeybinding(MODKEY_SHIFT
, XK_2
, KF_MVWS_2
, NULL
);
7312 setkeybinding(MODKEY_SHIFT
, XK_3
, KF_MVWS_3
, NULL
);
7313 setkeybinding(MODKEY_SHIFT
, XK_4
, KF_MVWS_4
, NULL
);
7314 setkeybinding(MODKEY_SHIFT
, XK_5
, KF_MVWS_5
, NULL
);
7315 setkeybinding(MODKEY_SHIFT
, XK_6
, KF_MVWS_6
, NULL
);
7316 setkeybinding(MODKEY_SHIFT
, XK_7
, KF_MVWS_7
, NULL
);
7317 setkeybinding(MODKEY_SHIFT
, XK_8
, KF_MVWS_8
, NULL
);
7318 setkeybinding(MODKEY_SHIFT
, XK_9
, KF_MVWS_9
, NULL
);
7319 setkeybinding(MODKEY_SHIFT
, XK_0
, KF_MVWS_10
, NULL
);
7320 setkeybinding(MODKEY_SHIFT
, XK_F1
, KF_MVWS_11
, NULL
);
7321 setkeybinding(MODKEY_SHIFT
, XK_F2
, KF_MVWS_12
, NULL
);
7322 setkeybinding(MODKEY_SHIFT
, XK_F3
, KF_MVWS_13
, NULL
);
7323 setkeybinding(MODKEY_SHIFT
, XK_F4
, KF_MVWS_14
, NULL
);
7324 setkeybinding(MODKEY_SHIFT
, XK_F5
, KF_MVWS_15
, NULL
);
7325 setkeybinding(MODKEY_SHIFT
, XK_F6
, KF_MVWS_16
, NULL
);
7326 setkeybinding(MODKEY_SHIFT
, XK_F7
, KF_MVWS_17
, NULL
);
7327 setkeybinding(MODKEY_SHIFT
, XK_F8
, KF_MVWS_18
, NULL
);
7328 setkeybinding(MODKEY_SHIFT
, XK_F9
, KF_MVWS_19
, NULL
);
7329 setkeybinding(MODKEY_SHIFT
, XK_F10
, KF_MVWS_20
, NULL
);
7330 setkeybinding(MODKEY_SHIFT
, XK_F11
, KF_MVWS_21
, NULL
);
7331 setkeybinding(MODKEY_SHIFT
, XK_F12
, KF_MVWS_22
, NULL
);
7332 setkeybinding(MODKEY_SHIFT
, XK_slash
, KF_NAME_WORKSPACE
,NULL
);
7333 setkeybinding(MODKEY_SHIFT
, XK_q
, KF_QUIT
, NULL
);
7334 setkeybinding(MODKEY_SHIFT
, XK_r
, KF_RAISE_TOGGLE
,NULL
);
7335 setkeybinding(MODKEY
, XK_q
, KF_RESTART
, NULL
);
7336 setkeybinding(MODKEY
, XK_KP_End
, KF_RG_1
, NULL
);
7337 setkeybinding(MODKEY
, XK_KP_Down
, KF_RG_2
, NULL
);
7338 setkeybinding(MODKEY
, XK_KP_Next
, KF_RG_3
, NULL
);
7339 setkeybinding(MODKEY
, XK_KP_Left
, KF_RG_4
, NULL
);
7340 setkeybinding(MODKEY
, XK_KP_Begin
, KF_RG_5
, NULL
);
7341 setkeybinding(MODKEY
, XK_KP_Right
, KF_RG_6
, NULL
);
7342 setkeybinding(MODKEY
, XK_KP_Home
, KF_RG_7
, NULL
);
7343 setkeybinding(MODKEY
, XK_KP_Up
, KF_RG_8
, NULL
);
7344 setkeybinding(MODKEY
, XK_KP_Prior
, KF_RG_9
, NULL
);
7345 setkeybinding(MODKEY_SHIFT
, XK_Right
, KF_RG_NEXT
, NULL
);
7346 setkeybinding(MODKEY_SHIFT
, XK_Left
, KF_RG_PREV
, NULL
);
7347 setkeybinding(MODKEY
, XK_f
, KF_SEARCH_WIN
, NULL
);
7348 setkeybinding(MODKEY
, XK_slash
, KF_SEARCH_WORKSPACE
,NULL
);
7349 setkeybinding(MODKEY_SHIFT
, XK_i
, KF_SPAWN_CUSTOM
,"initscr");
7350 setkeybinding(MODKEY_SHIFT
, XK_Delete
, KF_SPAWN_CUSTOM
,"lock");
7351 setkeybinding(MODKEY
, XK_p
, KF_SPAWN_CUSTOM
,"menu");
7352 setkeybinding(MODKEY
, XK_s
, KF_SPAWN_CUSTOM
,"screenshot_all");
7353 setkeybinding(MODKEY_SHIFT
, XK_s
, KF_SPAWN_CUSTOM
,"screenshot_wind");
7354 setkeybinding(MODKEY_SHIFT
, XK_Return
, KF_SPAWN_CUSTOM
,"term");
7355 setkeybinding(MODKEY_SHIFT
, XK_comma
, KF_STACK_INC
, NULL
);
7356 setkeybinding(MODKEY_SHIFT
, XK_period
, KF_STACK_DEC
, NULL
);
7357 setkeybinding(MODKEY_SHIFT
, XK_space
, KF_STACK_RESET
, NULL
);
7358 setkeybinding(MODKEY
, XK_Return
, KF_SWAP_MAIN
, NULL
);
7359 setkeybinding(MODKEY_SHIFT
, XK_j
, KF_SWAP_NEXT
, NULL
);
7360 setkeybinding(MODKEY_SHIFT
, XK_k
, KF_SWAP_PREV
, NULL
);
7361 setkeybinding(MODKEY_SHIFT
, XK_w
, KF_UNICONIFY
, NULL
);
7362 setkeybinding(MODKEY_SHIFT
, XK_v
, KF_VERSION
, NULL
);
7363 setkeybinding(MODKEY
, XK_equal
, KF_WIDTH_GROW
, NULL
);
7364 setkeybinding(MODKEY
, XK_minus
, KF_WIDTH_SHRINK
,NULL
);
7365 setkeybinding(MODKEY
, XK_x
, KF_WIND_DEL
, NULL
);
7366 setkeybinding(MODKEY_SHIFT
, XK_x
, KF_WIND_KILL
, NULL
);
7367 setkeybinding(MODKEY
, XK_1
, KF_WS_1
, NULL
);
7368 setkeybinding(MODKEY
, XK_2
, KF_WS_2
, NULL
);
7369 setkeybinding(MODKEY
, XK_3
, KF_WS_3
, NULL
);
7370 setkeybinding(MODKEY
, XK_4
, KF_WS_4
, NULL
);
7371 setkeybinding(MODKEY
, XK_5
, KF_WS_5
, NULL
);
7372 setkeybinding(MODKEY
, XK_6
, KF_WS_6
, NULL
);
7373 setkeybinding(MODKEY
, XK_7
, KF_WS_7
, NULL
);
7374 setkeybinding(MODKEY
, XK_8
, KF_WS_8
, NULL
);
7375 setkeybinding(MODKEY
, XK_9
, KF_WS_9
, NULL
);
7376 setkeybinding(MODKEY
, XK_0
, KF_WS_10
, NULL
);
7377 setkeybinding(MODKEY
, XK_F1
, KF_WS_11
, NULL
);
7378 setkeybinding(MODKEY
, XK_F2
, KF_WS_12
, NULL
);
7379 setkeybinding(MODKEY
, XK_F3
, KF_WS_13
, NULL
);
7380 setkeybinding(MODKEY
, XK_F4
, KF_WS_14
, NULL
);
7381 setkeybinding(MODKEY
, XK_F5
, KF_WS_15
, NULL
);
7382 setkeybinding(MODKEY
, XK_F6
, KF_WS_16
, NULL
);
7383 setkeybinding(MODKEY
, XK_F7
, KF_WS_17
, NULL
);
7384 setkeybinding(MODKEY
, XK_F8
, KF_WS_18
, NULL
);
7385 setkeybinding(MODKEY
, XK_F9
, KF_WS_19
, NULL
);
7386 setkeybinding(MODKEY
, XK_F10
, KF_WS_20
, NULL
);
7387 setkeybinding(MODKEY
, XK_F11
, KF_WS_21
, NULL
);
7388 setkeybinding(MODKEY
, XK_F12
, KF_WS_22
, NULL
);
7389 setkeybinding(MODKEY
, XK_Right
, KF_WS_NEXT
, NULL
);
7390 setkeybinding(MODKEY
, XK_Left
, KF_WS_PREV
, NULL
);
7391 setkeybinding(MODKEY
, XK_Up
, KF_WS_NEXT_ALL
, NULL
);
7392 setkeybinding(MODKEY
, XK_Down
, KF_WS_PREV_ALL
, NULL
);
7393 setkeybinding(MODKEY_SHIFT
, XK_Up
, KF_WS_NEXT_MOVE
,NULL
);
7394 setkeybinding(MODKEY_SHIFT
, XK_Down
, KF_WS_PREV_MOVE
,NULL
);
7395 setkeybinding(MODKEY
, XK_a
, KF_WS_PRIOR
, NULL
);
7397 setkeybinding(MODKEY_SHIFT
, XK_d
, KF_DUMPWINS
, NULL
);
7407 while (RB_EMPTY(&keys
) == 0) {
7408 kp
= RB_ROOT(&keys
);
7414 setkeymapping(const char *selector
, const char *value
, int flags
)
7416 char *keymapping_file
;
7418 /* suppress unused warnings since vars are needed */
7422 DNPRINTF(SWM_D_KEY
, "setkeymapping: enter\n");
7424 keymapping_file
= expand_tilde(value
);
7427 /* load new key bindings; if it fails, revert to default bindings */
7428 if (conf_load(keymapping_file
, SWM_CONF_KEYMAPPING
)) {
7433 free(keymapping_file
);
7435 DNPRINTF(SWM_D_KEY
, "setkeymapping: leave\n");
7440 updatenumlockmask(void)
7443 xcb_get_modifier_mapping_reply_t
*modmap_r
;
7444 xcb_keycode_t
*modmap
, kc
, *keycode
;
7448 modmap_r
= xcb_get_modifier_mapping_reply(conn
,
7449 xcb_get_modifier_mapping(conn
),
7452 modmap
= xcb_get_modifier_mapping_keycodes(modmap_r
);
7453 for (i
= 0; i
< 8; i
++) {
7454 for (j
= 0; j
< modmap_r
->keycodes_per_modifier
; j
++) {
7455 kc
= modmap
[i
* modmap_r
->keycodes_per_modifier
7457 keycode
= xcb_key_symbols_get_keycode(syms
,
7461 numlockmask
= (1 << i
);
7468 DNPRINTF(SWM_D_MISC
, "updatenumlockmask: %d\n", numlockmask
);
7475 int num_screens
, k
, j
;
7476 unsigned int modifiers
[4];
7477 xcb_keycode_t
*code
;
7479 DNPRINTF(SWM_D_MISC
, "grabkeys\n");
7480 updatenumlockmask();
7483 modifiers
[1] = numlockmask
;
7484 modifiers
[2] = XCB_MOD_MASK_LOCK
;
7485 modifiers
[3] = numlockmask
| XCB_MOD_MASK_LOCK
;
7487 num_screens
= get_screen_count();
7488 for (k
= 0; k
< num_screens
; k
++) {
7489 if (TAILQ_EMPTY(&screens
[k
].rl
))
7491 xcb_ungrab_key(conn
, XCB_GRAB_ANY
, screens
[k
].root
,
7493 RB_FOREACH(kp
, key_tree
, &keys
) {
7494 /* Skip unused ws binds. */
7495 if ((int)kp
->funcid
> KF_WS_1
+ workspace_limit
- 1 &&
7496 kp
->funcid
<= KF_WS_22
)
7499 /* Skip unused mvws binds. */
7500 if ((int)kp
->funcid
> KF_MVWS_1
+ workspace_limit
- 1 &&
7501 kp
->funcid
<= KF_MVWS_22
)
7504 if ((code
= xcb_key_symbols_get_keycode(syms
,
7506 for (j
= 0; j
< LENGTH(modifiers
); j
++)
7507 xcb_grab_key(conn
, 1,
7509 kp
->mod
| modifiers
[j
],
7510 *code
, XCB_GRAB_MODE_SYNC
,
7511 XCB_GRAB_MODE_SYNC
);
7519 grabbuttons(struct ws_win
*win
)
7521 unsigned int modifiers
[4];
7524 DNPRINTF(SWM_D_MOUSE
, "grabbuttons: win %#x\n", win
->id
);
7525 updatenumlockmask();
7528 modifiers
[1] = numlockmask
;
7529 modifiers
[2] = XCB_MOD_MASK_LOCK
;
7530 modifiers
[3] = numlockmask
| XCB_MOD_MASK_LOCK
;
7532 for (i
= 0; i
< LENGTH(buttons
); i
++)
7533 if (buttons
[i
].action
== client_click
)
7534 for (j
= 0; j
< LENGTH(modifiers
); ++j
)
7535 xcb_grab_button(conn
, 0, win
->id
, BUTTONMASK
,
7536 XCB_GRAB_MODE_SYNC
, XCB_GRAB_MODE_ASYNC
,
7537 XCB_WINDOW_NONE
, XCB_CURSOR_NONE
,
7538 buttons
[i
].button
, buttons
[i
].mask
|
7542 const char *quirkname
[] = {
7543 "NONE", /* config string for "no value" */
7551 "FOCUSONMAP_SINGLE",
7555 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
7556 #define SWM_Q_WS "\n|+ \t"
7558 parsequirks(const char *qstr
, unsigned long *quirk
)
7560 char *str
, *cp
, *name
;
7563 if (quirk
== NULL
|| qstr
== NULL
)
7566 if ((str
= strdup(qstr
)) == NULL
)
7567 err(1, "parsequirks: strdup");
7571 while ((name
= strsep(&cp
, SWM_Q_WS
)) != NULL
) {
7573 cp
+= (long)strspn(cp
, SWM_Q_WS
);
7574 for (i
= 0; i
< LENGTH(quirkname
); i
++) {
7575 if (strncasecmp(name
, quirkname
[i
],
7576 SWM_QUIRK_LEN
) == 0) {
7577 DNPRINTF(SWM_D_QUIRK
,
7578 "parsequirks: %s\n", name
);
7584 *quirk
|= 1 << (i
-1);
7588 if (i
>= LENGTH(quirkname
)) {
7589 DNPRINTF(SWM_D_QUIRK
,
7590 "parsequirks: invalid quirk [%s]\n", name
);
7601 quirk_insert(const char *class, const char *instance
, const char *name
,
7602 unsigned long quirk
)
7608 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: class: %s, instance: %s, name: %s,"
7609 " value: %lu\n", class, instance
, name
, quirk
);
7611 if ((qp
= malloc(sizeof *qp
)) == NULL
)
7612 err(1, "quirk_insert: malloc");
7614 if ((qp
->class = strdup(class)) == NULL
)
7615 err(1, "quirk_insert: strdup");
7616 if ((qp
->instance
= strdup(instance
)) == NULL
)
7617 err(1, "quirk_insert: strdup");
7618 if ((qp
->name
= strdup(name
)) == NULL
)
7619 err(1, "quirk_insert: strdup");
7621 if (asprintf(&str
, "^%s$", class) == -1)
7622 err(1, "quirk_insert: asprintf");
7623 if (regcomp(&qp
->regex_class
, str
, REG_EXTENDED
| REG_NOSUB
)) {
7624 add_startup_exception("regex failed to compile quirk 'class' "
7625 "field: %s", class);
7628 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: compiled: %s\n", str
);
7631 if (asprintf(&str
, "^%s$", instance
) == -1)
7632 err(1, "quirk_insert: asprintf");
7633 if (regcomp(&qp
->regex_instance
, str
, REG_EXTENDED
| REG_NOSUB
)) {
7634 add_startup_exception("regex failed to compile quirk 'instance'"
7635 " field: %s", instance
);
7638 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: compiled: %s\n", str
);
7641 if (asprintf(&str
, "^%s$", name
) == -1)
7642 err(1, "quirk_insert: asprintf");
7643 if (regcomp(&qp
->regex_name
, str
, REG_EXTENDED
| REG_NOSUB
)) {
7644 add_startup_exception("regex failed to compile quirk 'name' "
7648 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: compiled: %s\n", str
);
7652 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: regex error; skipping.\n");
7656 TAILQ_INSERT_TAIL(&quirks
, qp
, entry
);
7658 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: leave\n");
7662 quirk_remove(struct quirk
*qp
)
7664 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: %s:%s [%lu]\n", qp
->class,
7665 qp
->name
, qp
->quirk
);
7667 TAILQ_REMOVE(&quirks
, qp
, entry
);
7670 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: leave\n");
7674 quirk_free(struct quirk
*qp
)
7676 regfree(&qp
->regex_class
);
7677 regfree(&qp
->regex_instance
);
7678 regfree(&qp
->regex_name
);
7686 quirk_replace(struct quirk
*qp
, const char *class, const char *instance
,
7687 const char *name
, unsigned long quirk
)
7689 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: %s:%s:%s [%lu]\n", qp
->class,
7690 qp
->instance
, qp
->name
, qp
->quirk
);
7693 quirk_insert(class, instance
, name
, quirk
);
7695 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: leave\n");
7699 setquirk(const char *class, const char *instance
, const char *name
,
7700 unsigned long quirk
)
7704 DNPRINTF(SWM_D_QUIRK
, "setquirk: enter %s:%s:%s [%lu]\n", class,
7705 instance
, name
, quirk
);
7707 /* Remove/replace existing quirk. */
7708 TAILQ_FOREACH(qp
, &quirks
, entry
) {
7709 if (strcmp(qp
->class, class) == 0 &&
7710 strcmp(qp
->instance
, instance
) == 0 &&
7711 strcmp(qp
->name
, name
) == 0) {
7715 quirk_replace(qp
, class, instance
, name
, quirk
);
7716 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
7721 /* Only insert if quirk is not NONE. */
7723 quirk_insert(class, instance
, name
, quirk
);
7725 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
7728 /* Eat '\' in str used to escape square brackets and colon. */
7730 unescape_selector(char *str
)
7734 for (cp
= str
; *str
!= '\0'; ++str
, ++cp
) {
7735 if (*str
== '\\' && (*(str
+ 1) == ':' || *(str
+ 1) == ']' ||
7745 setconfquirk(const char *selector
, const char *value
, int flags
)
7747 char *str
, *cp
, *class;
7748 char *instance
= NULL
, *name
= NULL
;
7749 int retval
, count
= 0;
7752 /* suppress unused warning since var is needed */
7755 if (selector
== NULL
|| strlen(selector
) == 0)
7758 if ((str
= strdup(selector
)) == NULL
)
7759 err(1, "setconfquirk: strdup");
7761 /* Find non-escaped colon. */
7768 for (++cp
; *cp
!= '\0'; ++cp
) {
7769 if (*cp
== ':' && *(cp
- 1) != '\\') {
7775 unescape_selector(class);
7777 instance
= class + strlen(class) + 1;
7778 unescape_selector(instance
);
7784 name
= instance
+ strlen(instance
) + 1;
7785 unescape_selector(name
);
7790 DNPRINTF(SWM_D_CONF
, "setconfquirk: class: %s, instance: %s, "
7791 "name: %s\n", class, instance
, name
);
7793 if ((retval
= parsequirks(value
, &qrks
)) == 0)
7794 setquirk(class, instance
, name
, qrks
);
7803 setquirk("MPlayer", "xv", ".*", SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
| SWM_Q_FOCUSPREV
);
7804 setquirk("OpenOffice.org 3.2", "VCLSalFrame", ".*", SWM_Q_FLOAT
);
7805 setquirk("Firefox-bin", "firefox-bin", ".*", SWM_Q_TRANSSZ
);
7806 setquirk("Firefox", "Dialog", ".*", SWM_Q_FLOAT
);
7807 setquirk("Gimp", "gimp", ".*", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
7808 setquirk("XTerm", "xterm", ".*", SWM_Q_XTERM_FONTADJ
);
7809 setquirk("xine", "Xine Window", ".*", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
7810 setquirk("Xitk", "Xitk Combo", ".*", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
7811 setquirk("xine", "xine Panel", ".*", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
7812 setquirk("Xitk", "Xine Window", ".*", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
7813 setquirk("xine", "xine Video Fullscreen Window", ".*", SWM_Q_FULLSCREEN
| SWM_Q_FLOAT
);
7814 setquirk("pcb", "pcb", ".*", SWM_Q_FLOAT
);
7815 setquirk("SDL_App", "SDL_App", ".*", SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
);
7818 /* conf file stuff */
7819 #define SWM_CONF_FILE "spectrwm.conf"
7820 #define SWM_CONF_FILE_OLD "scrotwm.conf"
7824 SWM_S_BAR_AT_BOTTOM
,
7825 SWM_S_BAR_BORDER_WIDTH
,
7828 SWM_S_BAR_ENABLED_WS
,
7833 SWM_S_BOUNDARY_WIDTH
,
7834 SWM_S_CLOCK_ENABLED
,
7837 SWM_S_CYCLE_VISIBLE
,
7839 SWM_S_DISABLE_BORDER
,
7841 SWM_S_FOCUS_CLOSE_WRAP
,
7842 SWM_S_FOCUS_DEFAULT
,
7844 SWM_S_ICONIC_ENABLED
,
7845 SWM_S_REGION_PADDING
,
7850 SWM_S_STACK_ENABLED
,
7853 SWM_S_URGENT_ENABLED
,
7854 SWM_S_VERBOSE_LAYOUT
,
7855 SWM_S_WINDOW_CLASS_ENABLED
,
7856 SWM_S_WINDOW_INSTANCE_ENABLED
,
7857 SWM_S_WINDOW_NAME_ENABLED
,
7858 SWM_S_WORKSPACE_LIMIT
,
7859 SWM_S_WORKSPACE_NAME
,
7863 setconfvalue(const char *selector
, const char *value
, int flags
)
7865 struct workspace
*ws
;
7866 int i
, ws_id
, num_screens
;
7867 char *b
, *str
, s
[1024];
7870 case SWM_S_BAR_ACTION
:
7872 if ((bar_argv
[0] = expand_tilde(value
)) == NULL
)
7873 err(1, "setconfvalue: bar_action");
7875 case SWM_S_BAR_AT_BOTTOM
:
7876 bar_at_bottom
= atoi(value
);
7878 case SWM_S_BAR_BORDER_WIDTH
:
7879 bar_border_width
= atoi(value
);
7880 if (bar_border_width
< 0)
7881 bar_border_width
= 0;
7883 case SWM_S_BAR_DELAY
:
7884 /* No longer needed; leave to not break old conf files. */
7886 case SWM_S_BAR_ENABLED
:
7887 bar_enabled
= atoi(value
);
7889 case SWM_S_BAR_ENABLED_WS
:
7890 ws_id
= atoi(selector
) - 1;
7891 if (ws_id
< 0 || ws_id
>= workspace_limit
)
7892 errx(1, "setconfvalue: bar_enabled_ws: invalid "
7893 "workspace %d.", ws_id
+ 1);
7895 num_screens
= get_screen_count();
7896 for (i
= 0; i
< num_screens
; i
++) {
7897 ws
= (struct workspace
*)&screens
[i
].ws
;
7898 ws
[ws_id
].bar_enabled
= atoi(value
);
7901 case SWM_S_BAR_FONT
:
7903 if (asprintf(&bar_fonts
, "%s,%s", value
, bar_fonts
) == -1)
7904 err(1, "setconfvalue: asprintf: failed to allocate "
7905 "memory for bar_fonts.");
7908 /* If already in xft mode, then we are done. */
7909 if (!bar_font_legacy
)
7912 if ((str
= strdup(value
)) == NULL
)
7913 err(1, "setconfvalue: strdup");
7915 /* If there are any non-XLFD entries, switch to Xft mode. */
7916 while ((b
= strsep(&str
, ",")) != NULL
) {
7920 bar_font_legacy
= 0;
7927 case SWM_S_BAR_FORMAT
:
7929 if ((bar_format
= strdup(value
)) == NULL
)
7930 err(1, "setconfvalue: bar_format");
7932 case SWM_S_BAR_JUSTIFY
:
7933 if (strcmp(value
, "left") == 0)
7934 bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
7935 else if (strcmp(value
, "center") == 0)
7936 bar_justify
= SWM_BAR_JUSTIFY_CENTER
;
7937 else if (strcmp(value
, "right") == 0)
7938 bar_justify
= SWM_BAR_JUSTIFY_RIGHT
;
7940 errx(1, "invalid bar_justify");
7942 case SWM_S_BORDER_WIDTH
:
7943 border_width
= atoi(value
);
7944 if (border_width
< 0)
7947 case SWM_S_BOUNDARY_WIDTH
:
7948 boundary_width
= atoi(value
);
7949 if (boundary_width
< 0)
7952 case SWM_S_CLOCK_ENABLED
:
7953 clock_enabled
= atoi(value
);
7955 case SWM_S_CLOCK_FORMAT
:
7956 #ifndef SWM_DENY_CLOCK_FORMAT
7958 if ((clock_format
= strdup(value
)) == NULL
)
7959 err(1, "setconfvalue: clock_format");
7962 case SWM_S_CYCLE_EMPTY
:
7963 cycle_empty
= atoi(value
);
7965 case SWM_S_CYCLE_VISIBLE
:
7966 cycle_visible
= atoi(value
);
7968 case SWM_S_DIALOG_RATIO
:
7969 dialog_ratio
= atof(value
);
7970 if (dialog_ratio
> 1.0 || dialog_ratio
<= .3)
7973 case SWM_S_DISABLE_BORDER
:
7974 disable_border
= atoi(value
);
7976 case SWM_S_FOCUS_CLOSE
:
7977 if (strcmp(value
, "first") == 0)
7978 focus_close
= SWM_STACK_BOTTOM
;
7979 else if (strcmp(value
, "last") == 0)
7980 focus_close
= SWM_STACK_TOP
;
7981 else if (strcmp(value
, "next") == 0)
7982 focus_close
= SWM_STACK_ABOVE
;
7983 else if (strcmp(value
, "previous") == 0)
7984 focus_close
= SWM_STACK_BELOW
;
7986 errx(1, "focus_close");
7988 case SWM_S_FOCUS_CLOSE_WRAP
:
7989 focus_close_wrap
= atoi(value
);
7991 case SWM_S_FOCUS_DEFAULT
:
7992 if (strcmp(value
, "last") == 0)
7993 focus_default
= SWM_STACK_TOP
;
7994 else if (strcmp(value
, "first") == 0)
7995 focus_default
= SWM_STACK_BOTTOM
;
7997 errx(1, "focus_default");
7999 case SWM_S_FOCUS_MODE
:
8000 if (strcmp(value
, "default") == 0)
8001 focus_mode
= SWM_FOCUS_DEFAULT
;
8002 else if (strcmp(value
, "follow") == 0 ||
8003 strcmp(value
, "follow_cursor") == 0)
8004 focus_mode
= SWM_FOCUS_FOLLOW
;
8005 else if (strcmp(value
, "manual") == 0)
8006 focus_mode
= SWM_FOCUS_MANUAL
;
8008 errx(1, "focus_mode");
8010 case SWM_S_ICONIC_ENABLED
:
8011 iconic_enabled
= atoi(value
);
8013 case SWM_S_REGION_PADDING
:
8014 region_padding
= atoi(value
);
8015 if (region_padding
< 0)
8018 case SWM_S_SPAWN_ORDER
:
8019 if (strcmp(value
, "first") == 0)
8020 spawn_position
= SWM_STACK_BOTTOM
;
8021 else if (strcmp(value
, "last") == 0)
8022 spawn_position
= SWM_STACK_TOP
;
8023 else if (strcmp(value
, "next") == 0)
8024 spawn_position
= SWM_STACK_ABOVE
;
8025 else if (strcmp(value
, "previous") == 0)
8026 spawn_position
= SWM_STACK_BELOW
;
8028 errx(1, "spawn_position");
8030 case SWM_S_SPAWN_TERM
:
8031 setconfspawn("term", value
, 0);
8032 setconfspawn("spawn_term", value
, 0);
8035 /* No longer needed; leave to not break old conf files. */
8037 case SWM_S_SS_ENABLED
:
8038 /* No longer needed; leave to not break old conf files. */
8040 case SWM_S_STACK_ENABLED
:
8041 stack_enabled
= atoi(value
);
8043 case SWM_S_TERM_WIDTH
:
8044 term_width
= atoi(value
);
8048 case SWM_S_TILE_GAP
:
8049 tile_gap
= atoi(value
);
8051 case SWM_S_URGENT_ENABLED
:
8052 urgent_enabled
= atoi(value
);
8054 case SWM_S_VERBOSE_LAYOUT
:
8055 verbose_layout
= atoi(value
);
8056 for (i
= 0; layouts
[i
].l_stack
!= NULL
; i
++) {
8058 layouts
[i
].l_string
= fancy_stacker
;
8060 layouts
[i
].l_string
= plain_stacker
;
8063 case SWM_S_WINDOW_CLASS_ENABLED
:
8064 window_class_enabled
= atoi(value
);
8066 case SWM_S_WINDOW_INSTANCE_ENABLED
:
8067 window_instance_enabled
= atoi(value
);
8069 case SWM_S_WINDOW_NAME_ENABLED
:
8070 window_name_enabled
= atoi(value
);
8072 case SWM_S_WORKSPACE_LIMIT
:
8073 workspace_limit
= atoi(value
);
8074 if (workspace_limit
> SWM_WS_MAX
)
8075 workspace_limit
= SWM_WS_MAX
;
8076 else if (workspace_limit
< 1)
8077 workspace_limit
= 1;
8079 ewmh_update_desktops();
8081 case SWM_S_WORKSPACE_NAME
:
8082 if (getenv("SWM_STARTED") != NULL
)
8086 if (sscanf(value
, "ws[%d]:%1023c", &ws_id
, s
) != 2)
8087 errx(1, "invalid entry, should be 'ws[<idx>]:name'");
8089 if (ws_id
< 0 || ws_id
>= workspace_limit
)
8090 errx(1, "setconfvalue: workspace_name: invalid "
8091 "workspace %d.", ws_id
+ 1);
8093 num_screens
= get_screen_count();
8094 for (i
= 0; i
< num_screens
; ++i
) {
8095 ws
= (struct workspace
*)&screens
[i
].ws
;
8097 if (strlen(s
) > 0) {
8098 free(ws
[ws_id
].name
);
8099 if ((ws
[ws_id
].name
= strdup(s
)) == NULL
)
8100 err(1, "setconfvalue: workspace_name.");
8102 ewmh_update_desktop_names();
8103 ewmh_get_desktop_names();
8114 setconfmodkey(const char *selector
, const char *value
, int flags
)
8116 /* suppress unused warnings since vars are needed */
8120 if (strncasecmp(value
, "Mod1", strlen("Mod1")) == 0)
8121 update_modkey(XCB_MOD_MASK_1
);
8122 else if (strncasecmp(value
, "Mod2", strlen("Mod2")) == 0)
8123 update_modkey(XCB_MOD_MASK_2
);
8124 else if (strncasecmp(value
, "Mod3", strlen("Mod3")) == 0)
8125 update_modkey(XCB_MOD_MASK_3
);
8126 else if (strncasecmp(value
, "Mod4", strlen("Mod4")) == 0)
8127 update_modkey(XCB_MOD_MASK_4
);
8134 setconfcolor(const char *selector
, const char *value
, int flags
)
8136 int first
, last
, i
= 0, num_screens
;
8138 num_screens
= get_screen_count();
8140 /* conf screen indices begin at 1; treat vals <= 0 as 'all screens.' */
8141 if (selector
== NULL
|| strlen(selector
) == 0 ||
8142 (last
= atoi(selector
) - 1) < 0) {
8144 last
= num_screens
- 1;
8149 if (last
>= num_screens
) {
8150 add_startup_exception("invalid screen index: %d out of bounds "
8151 "(maximum %d)", last
+ 1, num_screens
);
8155 for (i
= first
; i
<= last
; ++i
) {
8156 setscreencolor(value
, i
, flags
);
8159 * When setting focus/unfocus colors, we need to also
8160 * set maximize colors to match if they haven't been customized.
8162 if (flags
== SWM_S_COLOR_FOCUS
&&
8163 !screens
[i
].c
[SWM_S_COLOR_FOCUS_MAXIMIZED
].manual
)
8164 setscreencolor(value
, i
, SWM_S_COLOR_FOCUS_MAXIMIZED
);
8165 else if (flags
== SWM_S_COLOR_UNFOCUS
&&
8166 !screens
[i
].c
[SWM_S_COLOR_UNFOCUS_MAXIMIZED
].manual
)
8167 setscreencolor(value
, i
, SWM_S_COLOR_UNFOCUS_MAXIMIZED
);
8169 screens
[i
].c
[flags
].manual
= 1;
8176 setconfregion(const char *selector
, const char *value
, int flags
)
8178 /* suppress unused warnings since vars are needed */
8182 custom_region(value
);
8187 setautorun(const char *selector
, const char *value
, int flags
)
8197 /* suppress unused warnings since vars are needed */
8201 if (getenv("SWM_STARTED"))
8205 if (sscanf(value
, "ws[%d]:%1023c", &ws_id
, s
) != 2)
8206 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
8208 if (ws_id
< 0 || ws_id
>= workspace_limit
)
8209 errx(1, "autorun: invalid workspace %d", ws_id
+ 1);
8211 sp
= expand_tilde((char *)&s
);
8214 * This is a little intricate
8216 * If the pid already exists we simply reuse it because it means it was
8217 * used before AND not claimed by manage_window. We get away with
8218 * altering it in the parent after INSERT because this can not be a race
8221 while ((ap
= strsep(&sp
, " \t")) != NULL
) {
8224 DNPRINTF(SWM_D_SPAWN
, "setautorun: arg [%s]\n", ap
);
8226 if ((a
.argv
= realloc(a
.argv
, argc
* sizeof(char *))) == NULL
)
8227 err(1, "setautorun: realloc");
8228 a
.argv
[argc
- 1] = ap
;
8232 if ((a
.argv
= realloc(a
.argv
, (argc
+ 1) * sizeof(char *))) == NULL
)
8233 err(1, "setautorun: realloc");
8234 a
.argv
[argc
] = NULL
;
8236 if ((pid
= fork()) == 0) {
8237 spawn(ws_id
, &a
, 1);
8246 p
= calloc(1, sizeof *p
);
8249 TAILQ_INSERT_TAIL(&pidlist
, p
, entry
);
8259 setlayout(const char *selector
, const char *value
, int flags
)
8261 int ws_id
, i
, x
, mg
, ma
, si
, ar
, f
= 0;
8262 int st
= SWM_V_STACK
, num_screens
;
8264 struct workspace
*ws
;
8266 /* suppress unused warnings since vars are needed */
8270 if (getenv("SWM_STARTED"))
8274 if (sscanf(value
, "ws[%d]:%d:%d:%d:%d:%1023c",
8275 &ws_id
, &mg
, &ma
, &si
, &ar
, s
) != 6)
8276 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
8277 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
8280 if (ws_id
< 0 || ws_id
>= workspace_limit
)
8281 errx(1, "layout: invalid workspace %d", ws_id
+ 1);
8283 if (strcasecmp(s
, "vertical") == 0)
8285 else if (strcasecmp(s
, "vertical_flip") == 0) {
8288 } else if (strcasecmp(s
, "horizontal") == 0)
8290 else if (strcasecmp(s
, "horizontal_flip") == 0) {
8293 } else if (strcasecmp(s
, "fullscreen") == 0)
8296 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
8297 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
8300 num_screens
= get_screen_count();
8301 for (i
= 0; i
< num_screens
; i
++) {
8302 ws
= (struct workspace
*)&screens
[i
].ws
;
8303 ws
[ws_id
].cur_layout
= &layouts
[st
];
8305 ws
[ws_id
].always_raise
= ar
;
8306 if (st
== SWM_MAX_STACK
)
8310 for (x
= 0; x
< abs(mg
); x
++) {
8311 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
8312 mg
>= 0 ? SWM_ARG_ID_MASTERGROW
:
8313 SWM_ARG_ID_MASTERSHRINK
);
8316 for (x
= 0; x
< abs(ma
); x
++) {
8317 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
8318 ma
>= 0 ? SWM_ARG_ID_MASTERADD
:
8319 SWM_ARG_ID_MASTERDEL
);
8322 for (x
= 0; x
< abs(si
); x
++) {
8323 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
8324 si
>= 0 ? SWM_ARG_ID_STACKINC
:
8325 SWM_ARG_ID_STACKDEC
);
8329 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
8330 SWM_ARG_ID_FLIPLAYOUT
);
8337 /* config options */
8338 struct config_option
{
8340 int (*func
)(const char*, const char*, int);
8343 struct config_option configopt
[] = {
8344 { "autorun", setautorun
, 0 },
8345 { "bar_action", setconfvalue
, SWM_S_BAR_ACTION
},
8346 { "bar_at_bottom", setconfvalue
, SWM_S_BAR_AT_BOTTOM
},
8347 { "bar_border", setconfcolor
, SWM_S_COLOR_BAR_BORDER
},
8348 { "bar_border_unfocus", setconfcolor
, SWM_S_COLOR_BAR_BORDER_UNFOCUS
},
8349 { "bar_border_width", setconfvalue
, SWM_S_BAR_BORDER_WIDTH
},
8350 { "bar_color", setconfcolor
, SWM_S_COLOR_BAR
},
8351 { "bar_delay", setconfvalue
, SWM_S_BAR_DELAY
},
8352 { "bar_enabled", setconfvalue
, SWM_S_BAR_ENABLED
},
8353 { "bar_enabled_ws", setconfvalue
, SWM_S_BAR_ENABLED_WS
},
8354 { "bar_font", setconfvalue
, SWM_S_BAR_FONT
},
8355 { "bar_font_color", setconfcolor
, SWM_S_COLOR_BAR_FONT
},
8356 { "bar_format", setconfvalue
, SWM_S_BAR_FORMAT
},
8357 { "bar_justify", setconfvalue
, SWM_S_BAR_JUSTIFY
},
8358 { "bind", setconfbinding
, 0 },
8359 { "border_width", setconfvalue
, SWM_S_BORDER_WIDTH
},
8360 { "boundary_width", setconfvalue
, SWM_S_BOUNDARY_WIDTH
},
8361 { "clock_enabled", setconfvalue
, SWM_S_CLOCK_ENABLED
},
8362 { "clock_format", setconfvalue
, SWM_S_CLOCK_FORMAT
},
8363 { "color_focus", setconfcolor
, SWM_S_COLOR_FOCUS
},
8364 { "color_focus_maximized", setconfcolor
, SWM_S_COLOR_FOCUS_MAXIMIZED
},
8365 { "color_unfocus", setconfcolor
, SWM_S_COLOR_UNFOCUS
},
8366 { "color_unfocus_maximized", setconfcolor
, SWM_S_COLOR_UNFOCUS_MAXIMIZED
},
8367 { "cycle_empty", setconfvalue
, SWM_S_CYCLE_EMPTY
},
8368 { "cycle_visible", setconfvalue
, SWM_S_CYCLE_VISIBLE
},
8369 { "dialog_ratio", setconfvalue
, SWM_S_DIALOG_RATIO
},
8370 { "disable_border", setconfvalue
, SWM_S_DISABLE_BORDER
},
8371 { "focus_close", setconfvalue
, SWM_S_FOCUS_CLOSE
},
8372 { "focus_close_wrap", setconfvalue
, SWM_S_FOCUS_CLOSE_WRAP
},
8373 { "focus_default", setconfvalue
, SWM_S_FOCUS_DEFAULT
},
8374 { "focus_mode", setconfvalue
, SWM_S_FOCUS_MODE
},
8375 { "iconic_enabled", setconfvalue
, SWM_S_ICONIC_ENABLED
},
8376 { "keyboard_mapping", setkeymapping
, 0 },
8377 { "layout", setlayout
, 0 },
8378 { "modkey", setconfmodkey
, 0 },
8379 { "program", setconfspawn
, 0 },
8380 { "quirk", setconfquirk
, 0 },
8381 { "region", setconfregion
, 0 },
8382 { "region_padding", setconfvalue
, SWM_S_REGION_PADDING
},
8383 { "screenshot_app", setconfvalue
, SWM_S_SS_APP
},
8384 { "screenshot_enabled", setconfvalue
, SWM_S_SS_ENABLED
},
8385 { "spawn_position", setconfvalue
, SWM_S_SPAWN_ORDER
},
8386 { "spawn_term", setconfvalue
, SWM_S_SPAWN_TERM
},
8387 { "stack_enabled", setconfvalue
, SWM_S_STACK_ENABLED
},
8388 { "term_width", setconfvalue
, SWM_S_TERM_WIDTH
},
8389 { "tile_gap", setconfvalue
, SWM_S_TILE_GAP
},
8390 { "title_class_enabled", setconfvalue
, SWM_S_WINDOW_CLASS_ENABLED
}, /* For backwards compat. */
8391 { "title_name_enabled", setconfvalue
, SWM_S_WINDOW_INSTANCE_ENABLED
}, /* For backwards compat. */
8392 { "urgent_enabled", setconfvalue
, SWM_S_URGENT_ENABLED
},
8393 { "verbose_layout", setconfvalue
, SWM_S_VERBOSE_LAYOUT
},
8394 { "window_class_enabled", setconfvalue
, SWM_S_WINDOW_CLASS_ENABLED
},
8395 { "window_instance_enabled", setconfvalue
, SWM_S_WINDOW_INSTANCE_ENABLED
},
8396 { "window_name_enabled", setconfvalue
, SWM_S_WINDOW_NAME_ENABLED
},
8397 { "workspace_limit", setconfvalue
, SWM_S_WORKSPACE_LIMIT
},
8398 { "name", setconfvalue
, SWM_S_WORKSPACE_NAME
},
8402 _add_startup_exception(const char *fmt
, va_list ap
)
8404 if (vasprintf(&startup_exception
, fmt
, ap
) == -1)
8405 warn("%s: asprintf", __func__
);
8409 add_startup_exception(const char *fmt
, ...)
8415 if (startup_exception
)
8418 /* force bar to be enabled due to exception */
8422 _add_startup_exception(fmt
, ap
);
8427 conf_load(const char *filename
, int keymapping
)
8430 char *line
= NULL
, *cp
, *ce
, *optsub
, *optval
= NULL
;
8431 size_t linelen
, lineno
= 0;
8432 int wordlen
, i
, optidx
, count
;
8433 struct config_option
*opt
= NULL
;
8435 DNPRINTF(SWM_D_CONF
, "conf_load: begin\n");
8437 if (filename
== NULL
) {
8438 warnx("conf_load: no filename");
8442 DNPRINTF(SWM_D_CONF
, "conf_load: open %s\n", filename
);
8444 if ((config
= fopen(filename
, "r")) == NULL
) {
8445 warn("conf_load: fopen: %s", filename
);
8449 while (!feof(config
)) {
8453 if ((line
= fparseln(config
, &linelen
, &lineno
, NULL
,
8454 FPARSELN_UNESCCOMM
| FPARSELN_UNESCCONT
)) == NULL
) {
8456 err(1, "%s", filename
);
8461 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
8462 if (cp
[0] == '\0') {
8466 /* get config option */
8467 wordlen
= strcspn(cp
, "=[ \t\n");
8469 add_startup_exception("%s: line %zd: no option found",
8474 for (i
= 0; i
< LENGTH(configopt
); i
++) {
8475 opt
= &configopt
[i
];
8476 if (strncasecmp(cp
, opt
->optname
, wordlen
) == 0 &&
8477 (int)strlen(opt
->optname
) == wordlen
) {
8483 add_startup_exception("%s: line %zd: unknown option "
8484 "%.*s", filename
, lineno
, wordlen
, cp
);
8487 if (keymapping
&& opt
&& strcmp(opt
->optname
, "bind")) {
8488 add_startup_exception("%s: line %zd: invalid option "
8489 "%.*s", filename
, lineno
, wordlen
, cp
);
8493 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
8495 /* get [selector] if any */
8500 /* Get length of selector. */
8501 for (ce
= ++cp
; *ce
!= '\0'; ++ce
) {
8502 /* Find matching (not escaped) bracket. */
8503 if (*ce
== ']' && *(ce
- 1) != '\\') {
8510 add_startup_exception("%s: line %zd: syntax "
8511 "error: unterminated selector", filename
,
8516 /* ce points at ']'; terminate optsub. */
8521 cp
+= strspn(cp
, "= \t\n"); /* eat trailing */
8524 if (strlen(optval
) == 0) {
8525 add_startup_exception("%s: line %zd: must supply value "
8526 "to %s", filename
, lineno
,
8527 configopt
[optidx
].optname
);
8530 /* call function to deal with it all */
8531 if (configopt
[optidx
].func(optsub
, optval
,
8532 configopt
[optidx
].funcflags
) != 0) {
8533 add_startup_exception("%s: line %zd: invalid data for "
8534 "%s", filename
, lineno
, configopt
[optidx
].optname
);
8542 DNPRINTF(SWM_D_CONF
, "conf_load: end\n");
8548 set_child_transient(struct ws_win
*win
, xcb_window_t
*trans
)
8550 struct ws_win
*parent
, *w
;
8551 struct swm_region
*r
;
8552 struct workspace
*ws
;
8553 xcb_icccm_wm_hints_t wmh
;
8555 parent
= find_window(win
->transient
);
8557 parent
->focus_child
= win
;
8559 DNPRINTF(SWM_D_MISC
, "set_child_transient: parent doesn't exist"
8560 " for %#x trans %#x\n", win
->id
, win
->transient
);
8562 r
= root_to_region(win
->s
->root
, SWM_CK_ALL
);
8564 /* parent doen't exist in our window list */
8565 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
8566 if (xcb_icccm_get_wm_hints_reply(conn
,
8567 xcb_icccm_get_wm_hints(conn
, w
->id
),
8569 warnx("can't get hints for %#x", w
->id
);
8573 if (win
->hints
.window_group
!= wmh
.window_group
)
8576 w
->focus_child
= win
;
8577 win
->transient
= w
->id
;
8579 DNPRINTF(SWM_D_MISC
, "set_child_transient: adjusting "
8580 "transient to %#x\n", win
->transient
);
8587 window_get_pid(xcb_window_t win
)
8592 xcb_get_property_cookie_t pc
;
8593 xcb_get_property_reply_t
*pr
;
8595 apid
= get_atom_from_string("_NET_WM_PID");
8596 if (apid
== XCB_ATOM_NONE
)
8599 pc
= xcb_get_property(conn
, 0, win
, apid
, XCB_ATOM_CARDINAL
, 0, 1);
8600 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
8603 if (pr
->type
!= XCB_ATOM_CARDINAL
) {
8608 if (pr
->type
== apid
&& pr
->format
== 32)
8609 ret
= *((pid_t
*)xcb_get_property_value(pr
));
8615 apid
= get_atom_from_string("_SWM_PID");
8616 pc
= xcb_get_property(conn
, 0, win
, apid
, XCB_ATOM_STRING
,
8618 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
8621 if (pr
->type
!= apid
) {
8626 ret
= (pid_t
)strtonum(xcb_get_property_value(pr
), 0, INT_MAX
, &errstr
);
8633 get_swm_ws(xcb_window_t id
)
8639 xcb_get_property_reply_t
*gpr
;
8641 gpr
= xcb_get_property_reply(conn
,
8642 xcb_get_property(conn
, 0, id
, a_swm_ws
,
8643 XCB_ATOM_STRING
, 0, SWM_PROPLEN
),
8648 proplen
= xcb_get_property_value_length(gpr
);
8650 prop
= malloc(proplen
+ 1);
8653 xcb_get_property_value(gpr
),
8655 prop
[proplen
] = '\0';
8662 DNPRINTF(SWM_D_PROP
, "get_swm_ws: _SWM_WS: %s\n", prop
);
8663 ws_idx
= (int)strtonum(prop
, 0, workspace_limit
- 1, &errstr
);
8665 DNPRINTF(SWM_D_PROP
, "get_swm_ws: win #%s: %s",
8675 get_ws_idx(xcb_window_t id
)
8677 xcb_get_property_reply_t
*gpr
;
8680 gpr
= xcb_get_property_reply(conn
,
8681 xcb_get_property(conn
, 0, id
, ewmh
[_NET_WM_DESKTOP
].atom
,
8682 XCB_ATOM_CARDINAL
, 0, 1),
8685 if (gpr
->type
== XCB_ATOM_CARDINAL
&& gpr
->format
== 32)
8686 ws_idx
= *((int *)xcb_get_property_value(gpr
));
8691 if ((ws_idx
= get_swm_ws(id
)) != -1)
8692 xcb_delete_property(conn
, id
, a_swm_ws
);
8694 if (ws_idx
> workspace_limit
- 1 || ws_idx
< -1)
8697 DNPRINTF(SWM_D_PROP
, "get_ws_idx: win %#x, ws_idx: %d\n", id
, ws_idx
);
8703 manage_window(xcb_window_t id
, int mapped
)
8705 struct ws_win
*win
, *ww
;
8706 struct swm_region
*r
;
8709 xcb_get_geometry_reply_t
*gr
;
8710 xcb_window_t trans
= XCB_WINDOW_NONE
;
8711 uint32_t i
, wa
[2], new_flags
;
8713 char *class, *instance
, *name
;
8715 if ((win
= find_window(id
)) != NULL
) {
8716 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x already "
8717 "managed; skipping.)\n", id
);
8718 return (win
); /* Already managed. */
8721 /* See if window is on the unmanaged list. */
8722 if ((win
= find_unmanaged_window(id
)) != NULL
) {
8723 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x found on "
8724 "unmanaged list.\n", id
);
8725 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
8728 set_child_transient(win
, &trans
);
8732 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x is new.\n", id
);
8735 /* Try to get initial window geometry. */
8736 gr
= xcb_get_geometry_reply(conn
, xcb_get_geometry(conn
, id
), NULL
);
8738 DNPRINTF(SWM_D_MISC
, "manage_window: get geometry failed.\n");
8742 /* Create and initialize ws_win object. */
8743 if ((win
= calloc(1, sizeof(struct ws_win
))) == NULL
)
8744 err(1, "manage_window: calloc: failed to allocate memory for "
8749 /* Figureout which region the window belongs to. */
8750 r
= root_to_region(gr
->root
, SWM_CK_ALL
);
8752 /* Ignore window border if there is one. */
8753 WIDTH(win
) = gr
->width
;
8754 HEIGHT(win
) = gr
->height
;
8755 X(win
) = gr
->x
+ gr
->border_width
- border_width
;
8756 Y(win
) = gr
->y
+ gr
->border_width
- border_width
;
8758 win
->mapped
= mapped
;
8759 win
->s
= r
->s
; /* this never changes */
8763 /* Select which X events to monitor and set border pixel color. */
8764 wa
[0] = win
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
;
8765 wa
[1] = XCB_EVENT_MASK_ENTER_WINDOW
| XCB_EVENT_MASK_PROPERTY_CHANGE
|
8766 XCB_EVENT_MASK_STRUCTURE_NOTIFY
;
8768 wa
[1] |= XCB_EVENT_MASK_LEAVE_WINDOW
| XCB_EVENT_MASK_FOCUS_CHANGE
;
8771 xcb_change_window_attributes(conn
, win
->id
, XCB_CW_BORDER_PIXEL
|
8772 XCB_CW_EVENT_MASK
, wa
);
8774 /* Get WM_SIZE_HINTS. */
8775 xcb_icccm_get_wm_normal_hints_reply(conn
,
8776 xcb_icccm_get_wm_normal_hints(conn
, win
->id
),
8780 xcb_icccm_get_wm_hints_reply(conn
,
8781 xcb_icccm_get_wm_hints(conn
, win
->id
),
8784 /* Get WM_TRANSIENT_FOR; see if window is a transient. */
8785 xcb_icccm_get_wm_transient_for_reply(conn
,
8786 xcb_icccm_get_wm_transient_for(conn
, win
->id
),
8789 win
->transient
= trans
;
8790 set_child_transient(win
, &win
->transient
);
8793 /* Get WM_PROTOCOLS. */
8794 get_wm_protocols(win
);
8796 /* Figure out which workspace the window belongs to. */
8797 if ((p
= find_pid(window_get_pid(win
->id
))) != NULL
) {
8798 win
->ws
= &r
->s
->ws
[p
->ws
];
8799 TAILQ_REMOVE(&pidlist
, p
, entry
);
8802 } else if ((ws_idx
= get_ws_idx(win
->id
)) != -1 &&
8804 /* _SWM_WS is set; use that. */
8805 win
->ws
= &r
->s
->ws
[ws_idx
];
8806 } else if (trans
&& (ww
= find_window(trans
)) != NULL
) {
8807 /* Launch transients in the same ws as parent. */
8813 /* Set the _NET_WM_DESKTOP atom. */
8814 DNPRINTF(SWM_D_PROP
, "manage_window: set _NET_WM_DESKTOP: %d\n",
8816 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
8817 ewmh
[_NET_WM_DESKTOP
].atom
, XCB_ATOM_CARDINAL
, 32, 1, &win
->ws
->idx
);
8819 /* WS must already be set for this to work. */
8820 store_float_geom(win
);
8822 /* Set initial quirks based on EWMH. */
8823 ewmh_autoquirk(win
);
8825 /* Determine initial quirks. */
8826 xcb_icccm_get_wm_class_reply(conn
,
8827 xcb_icccm_get_wm_class(conn
, win
->id
),
8830 class = win
->ch
.class_name
? win
->ch
.class_name
: "";
8831 instance
= win
->ch
.instance_name
? win
->ch
.instance_name
: "";
8832 name
= get_win_name(win
->id
);
8834 DNPRINTF(SWM_D_CLASS
, "manage_window: class: %s, instance: %s, "
8835 "name: %s\n", class, instance
, name
);
8837 /* java is retarded so treat it special */
8838 if (strstr(instance
, "sun-awt")) {
8839 DNPRINTF(SWM_D_CLASS
, "manage_window: java window detected.\n");
8843 TAILQ_FOREACH(qp
, &quirks
, entry
) {
8844 if (regexec(&qp
->regex_class
, class, 0, NULL
, 0) == 0 &&
8845 regexec(&qp
->regex_instance
, instance
, 0, NULL
, 0) == 0 &&
8846 regexec(&qp
->regex_name
, name
, 0, NULL
, 0) == 0) {
8847 DNPRINTF(SWM_D_CLASS
, "manage_window: matched "
8848 "quirk: %s:%s:%s mask: %#lx\n", qp
->class,
8849 qp
->instance
, qp
->name
, qp
->quirk
);
8850 win
->quirks
= qp
->quirk
;
8856 /* Reset font sizes (the bruteforce way; no default keybinding). */
8857 if (win
->quirks
& SWM_Q_XTERM_FONTADJ
) {
8858 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
8859 fake_keypress(win
, XK_KP_Subtract
, XCB_MOD_MASK_SHIFT
);
8860 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
8861 fake_keypress(win
, XK_KP_Add
, XCB_MOD_MASK_SHIFT
);
8864 /* Make sure window is positioned inside its region, if its active. */
8866 region_containment(win
, r
, SWM_CW_ALLSIDES
|
8867 SWM_CW_HARDBOUNDARY
);
8872 /* Figure out where to stack the window in the workspace. */
8873 if (trans
&& (ww
= find_window(trans
)))
8874 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, ww
, win
, entry
);
8875 else if (win
->ws
->focus
&& spawn_position
== SWM_STACK_ABOVE
)
8876 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, win
->ws
->focus
, win
,
8878 else if (win
->ws
->focus
&& spawn_position
== SWM_STACK_BELOW
)
8879 TAILQ_INSERT_BEFORE(win
->ws
->focus
, win
, entry
);
8880 else switch (spawn_position
) {
8883 case SWM_STACK_ABOVE
:
8884 TAILQ_INSERT_TAIL(&win
->ws
->winlist
, win
, entry
);
8886 case SWM_STACK_BOTTOM
:
8887 case SWM_STACK_BELOW
:
8888 TAILQ_INSERT_HEAD(&win
->ws
->winlist
, win
, entry
);
8891 ewmh_update_client_list();
8893 TAILQ_INSERT_TAIL(&win
->ws
->stack
, win
, stack_entry
);
8895 /* Get/apply initial _NET_WM_STATE */
8896 ewmh_get_wm_state(win
);
8899 new_flags
= win
->ewmh_flags
;
8901 if (win
->quirks
& SWM_Q_FLOAT
)
8902 new_flags
|= EWMH_F_ABOVE
;
8904 if (win
->quirks
& SWM_Q_ANYWHERE
)
8905 new_flags
|= SWM_F_MANUAL
;
8907 ewmh_apply_flags(win
, new_flags
);
8908 ewmh_update_wm_state(win
);
8910 /* Set initial _NET_WM_ALLOWED_ACTIONS */
8911 ewmh_update_actions(win
);
8915 DNPRINTF(SWM_D_MISC
, "manage_window: done. win %#x, (x,y) w x h: "
8916 "(%d,%d) %d x %d, ws: %d, iconic: %s, transient: %#x\n", win
->id
,
8917 X(win
), Y(win
), WIDTH(win
), HEIGHT(win
), win
->ws
->idx
,
8918 YESNO(ICONIC(win
)), win
->transient
);
8924 free_window(struct ws_win
*win
)
8926 DNPRINTF(SWM_D_MISC
, "free_window: win %#x\n", win
->id
);
8931 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
8933 xcb_icccm_get_wm_class_reply_wipe(&win
->ch
);
8938 memset(win
, 0xff, sizeof *win
); /* XXX kill later */
8941 DNPRINTF(SWM_D_MISC
, "free_window: done.\n");
8945 unmanage_window(struct ws_win
*win
)
8947 struct ws_win
*parent
;
8952 DNPRINTF(SWM_D_MISC
, "unmanage_window: win %#x\n", win
->id
);
8955 parent
= find_window(win
->transient
);
8957 parent
->focus_child
= NULL
;
8960 TAILQ_REMOVE(&win
->ws
->stack
, win
, stack_entry
);
8961 TAILQ_REMOVE(&win
->ws
->winlist
, win
, entry
);
8962 TAILQ_INSERT_TAIL(&win
->ws
->unmanagedlist
, win
, entry
);
8964 ewmh_update_client_list();
8968 expose(xcb_expose_event_t
*e
)
8971 struct swm_region
*r
;
8973 DNPRINTF(SWM_D_EVENT
, "expose: win %#x\n", e
->window
);
8975 num_screens
= get_screen_count();
8976 for (i
= 0; i
< num_screens
; i
++)
8977 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
8978 if (e
->window
== WINID(r
->bar
))
8986 focusin(xcb_focus_in_event_t
*e
)
8988 DNPRINTF(SWM_D_EVENT
, "focusin: win %#x, mode: %s(%u), "
8989 "detail: %s(%u)\n", e
->event
, get_notify_mode_label(e
->mode
),
8990 e
->mode
, get_notify_detail_label(e
->detail
), e
->detail
);
8994 focusout(xcb_focus_out_event_t
*e
)
8996 DNPRINTF(SWM_D_EVENT
, "focusout: win %#x, mode: %s(%u), "
8997 "detail: %s(%u)\n", e
->event
, get_notify_mode_label(e
->mode
),
8998 e
->mode
, get_notify_detail_label(e
->detail
), e
->detail
);
9003 keypress(xcb_key_press_event_t
*e
)
9005 xcb_keysym_t keysym
;
9008 keysym
= xcb_key_press_lookup_keysym(syms
, e
, 0);
9010 DNPRINTF(SWM_D_EVENT
, "keypress: keysym: %u, win (x,y): %#x (%d,%d), "
9011 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
9012 "state: %u, same_screen: %s\n", keysym
, e
->event
, e
->event_x
,
9013 e
->event_y
, e
->detail
, e
->time
, e
->root
, e
->root_x
, e
->root_y
,
9014 e
->child
, e
->state
, YESNO(e
->same_screen
));
9016 if ((kp
= key_lookup(CLEANMASK(e
->state
), keysym
)) == NULL
)
9019 last_event_time
= e
->time
;
9021 if (kp
->funcid
== KF_SPAWN_CUSTOM
)
9022 spawn_custom(root_to_region(e
->root
, SWM_CK_ALL
),
9023 &(keyfuncs
[kp
->funcid
].args
), kp
->spawn_name
);
9024 else if (keyfuncs
[kp
->funcid
].func
)
9025 keyfuncs
[kp
->funcid
].func(root_to_region(e
->root
, SWM_CK_ALL
),
9026 &(keyfuncs
[kp
->funcid
].args
));
9029 /* Unfreeze grab events. */
9030 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
, e
->time
);
9033 DNPRINTF(SWM_D_EVENT
, "keypress: done.\n");
9037 buttonpress(xcb_button_press_event_t
*e
)
9039 struct ws_win
*win
= NULL
;
9040 struct swm_region
*r
, *old_r
;
9044 DNPRINTF(SWM_D_EVENT
, "buttonpress: win (x,y): %#x (%d,%d), "
9045 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
9046 "state: %u, same_screen: %s\n", e
->event
, e
->event_x
, e
->event_y
,
9047 e
->detail
, e
->time
, e
->root
, e
->root_x
, e
->root_y
, e
->child
,
9048 e
->state
, YESNO(e
->same_screen
));
9050 if (e
->event
== e
->root
) {
9051 if (e
->child
!= 0) {
9052 win
= find_window(e
->child
);
9053 /* Pass ButtonPress to window if it isn't managed. */
9057 /* Focus on empty region */
9058 /* If no windows on region if its empty. */
9059 r
= root_to_region(e
->root
, SWM_CK_POINTER
);
9061 DNPRINTF(SWM_D_EVENT
, "buttonpress: "
9062 "NULL region; ignoring.\n");
9066 if (TAILQ_EMPTY(&r
->ws
->winlist
)) {
9067 old_r
= root_to_region(e
->root
, SWM_CK_FOCUS
);
9068 if (old_r
&& old_r
!= r
)
9069 unfocus_win(old_r
->ws
->focus
);
9071 xcb_set_input_focus(conn
,
9072 XCB_INPUT_FOCUS_PARENT
, e
->root
, e
->time
);
9074 /* Clear bar since empty. */
9082 win
= find_window(e
->event
);
9088 last_event_time
= e
->time
;
9090 focus_win(get_focus_magic(win
));
9092 for (i
= 0; i
< LENGTH(buttons
); i
++)
9093 if (client_click
== buttons
[i
].action
&& buttons
[i
].func
&&
9094 buttons
[i
].button
== e
->detail
&&
9095 CLEANMASK(buttons
[i
].mask
) == CLEANMASK(e
->state
)) {
9096 buttons
[i
].func(win
, &buttons
[i
].args
);
9102 DNPRINTF(SWM_D_EVENT
, "buttonpress: passing to window.\n");
9103 /* Replay event to event window */
9104 xcb_allow_events(conn
, XCB_ALLOW_REPLAY_POINTER
, e
->time
);
9106 DNPRINTF(SWM_D_EVENT
, "buttonpress: handled.\n");
9107 /* Unfreeze grab events. */
9108 xcb_allow_events(conn
, XCB_ALLOW_SYNC_POINTER
, e
->time
);
9116 print_win_geom(xcb_window_t w
)
9118 xcb_get_geometry_reply_t
*wa
;
9120 wa
= xcb_get_geometry_reply(conn
, xcb_get_geometry(conn
, w
), NULL
);
9122 DNPRINTF(SWM_D_MISC
, "print_win_geom: window not found: %#x\n",
9127 DNPRINTF(SWM_D_MISC
, "print_win_geom: win %#x, root: %#x, "
9128 "depth: %u, (x,y) w x h: (%d,%d) %d x %d, border: %d\n",
9129 w
, wa
->root
, wa
->depth
, wa
->x
, wa
->y
, wa
->width
, wa
->height
,
9138 get_stack_mode_name(uint8_t mode
)
9143 case XCB_STACK_MODE_ABOVE
:
9146 case XCB_STACK_MODE_BELOW
:
9149 case XCB_STACK_MODE_TOP_IF
:
9152 case XCB_STACK_MODE_BOTTOM_IF
:
9155 case XCB_STACK_MODE_OPPOSITE
:
9167 configurerequest(xcb_configure_request_event_t
*e
)
9170 struct swm_region
*r
= NULL
;
9173 uint32_t wc
[7] = {0};
9175 if ((win
= find_window(e
->window
)) == NULL
)
9176 if ((win
= find_unmanaged_window(e
->window
)) == NULL
)
9180 if (swm_debug
& SWM_D_EVENT
) {
9181 print_win_geom(e
->window
);
9183 DNPRINTF(SWM_D_EVENT
, "configurerequest: win %#x, "
9184 "parent: %#x, new: %s, value_mask: %u { ", e
->window
,
9185 e
->parent
, YESNO(new), e
->value_mask
);
9186 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
)
9187 DPRINTF("X: %d ", e
->x
);
9188 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
)
9189 DPRINTF("Y: %d ", e
->y
);
9190 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
)
9191 DPRINTF("W: %u ", e
->width
);
9192 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
)
9193 DPRINTF("H: %u ", e
->height
);
9194 if (e
->value_mask
& XCB_CONFIG_WINDOW_BORDER_WIDTH
)
9195 DPRINTF("Border: %u ", e
->border_width
);
9196 if (e
->value_mask
& XCB_CONFIG_WINDOW_SIBLING
)
9197 DPRINTF("Sibling: %#x ", e
->sibling
);
9198 if (e
->value_mask
& XCB_CONFIG_WINDOW_STACK_MODE
)
9199 DPRINTF("StackMode: %s(%u) ",
9200 get_stack_mode_name(e
->stack_mode
), e
->stack_mode
);
9206 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
) {
9207 mask
|= XCB_CONFIG_WINDOW_X
;
9210 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
) {
9211 mask
|= XCB_CONFIG_WINDOW_Y
;
9214 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
) {
9215 mask
|= XCB_CONFIG_WINDOW_WIDTH
;
9218 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
) {
9219 mask
|= XCB_CONFIG_WINDOW_HEIGHT
;
9220 wc
[i
++] = e
->height
;
9222 if (e
->value_mask
& XCB_CONFIG_WINDOW_BORDER_WIDTH
) {
9223 mask
|= XCB_CONFIG_WINDOW_BORDER_WIDTH
;
9224 wc
[i
++] = e
->border_width
;
9226 if (e
->value_mask
& XCB_CONFIG_WINDOW_SIBLING
) {
9227 mask
|= XCB_CONFIG_WINDOW_SIBLING
;
9228 wc
[i
++] = e
->sibling
;
9230 if (e
->value_mask
& XCB_CONFIG_WINDOW_STACK_MODE
) {
9231 mask
|= XCB_CONFIG_WINDOW_STACK_MODE
;
9232 wc
[i
++] = e
->stack_mode
;
9236 xcb_configure_window(conn
, e
->window
, mask
, wc
);
9239 } else if ((!MANUAL(win
) || win
->quirks
& SWM_Q_ANYWHERE
) &&
9240 !FULLSCREEN(win
) && !MAXIMIZED(win
)) {
9243 else if (win
->ws
->old_r
)
9246 /* windows are centered unless ANYWHERE quirk is set. */
9247 if (win
->quirks
& SWM_Q_ANYWHERE
) {
9248 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
) {
9249 win
->g_float
.x
= e
->x
;
9251 win
->g_float
.x
-= X(r
);
9254 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
) {
9255 win
->g_float
.y
= e
->y
;
9257 win
->g_float
.y
-= Y(r
);
9261 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
)
9262 win
->g_float
.w
= e
->width
;
9264 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
)
9265 win
->g_float
.h
= e
->height
;
9267 win
->g_floatvalid
= 1;
9269 if (!MAXIMIZED(win
) && !FULLSCREEN(win
) &&
9270 (TRANS(win
) || (ABOVE(win
) &&
9271 win
->ws
->cur_layout
!= &layouts
[SWM_MAX_STACK
]))) {
9272 WIDTH(win
) = win
->g_float
.w
;
9273 HEIGHT(win
) = win
->g_float
.h
;
9276 update_floater(win
);
9291 DNPRINTF(SWM_D_EVENT
, "configurerequest: done.\n");
9295 configurenotify(xcb_configure_notify_event_t
*e
)
9299 DNPRINTF(SWM_D_EVENT
, "configurenotify: win %#x, event win: %#x, "
9300 "(x,y) WxH: (%d,%d) %ux%u, border: %u, above_sibling: %#x, "
9301 "override_redirect: %s\n", e
->window
, e
->event
, e
->x
, e
->y
,
9302 e
->width
, e
->height
, e
->border_width
, e
->above_sibling
,
9303 YESNO(e
->override_redirect
));
9305 win
= find_window(e
->window
);
9307 xcb_icccm_get_wm_normal_hints_reply(conn
,
9308 xcb_icccm_get_wm_normal_hints(conn
, win
->id
),
9311 if (font_adjusted
) {
9319 destroynotify(xcb_destroy_notify_event_t
*e
)
9323 DNPRINTF(SWM_D_EVENT
, "destroynotify: win %#x\n", e
->window
);
9325 if ((win
= find_window(e
->window
)) == NULL
) {
9326 if ((win
= find_unmanaged_window(e
->window
)) == NULL
)
9332 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
9333 /* If we were focused, make sure we focus on something else. */
9334 if (win
== win
->ws
->focus
)
9335 win
->ws
->focus_pending
= get_focus_prev(win
);
9338 unmanage_window(win
);
9341 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& WS_FOCUSED(win
->ws
)) {
9342 if (win
->ws
->focus_pending
) {
9343 focus_win(win
->ws
->focus_pending
);
9344 win
->ws
->focus_pending
= NULL
;
9345 } else if (win
== win
->ws
->focus
) {
9346 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
,
9347 win
->ws
->r
->id
, XCB_CURRENT_TIME
);
9358 get_notify_detail_label(uint8_t detail
)
9363 case XCB_NOTIFY_DETAIL_ANCESTOR
:
9366 case XCB_NOTIFY_DETAIL_VIRTUAL
:
9369 case XCB_NOTIFY_DETAIL_INFERIOR
:
9372 case XCB_NOTIFY_DETAIL_NONLINEAR
:
9373 label
= "Nonlinear";
9375 case XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL
:
9376 label
= "NonlinearVirtual";
9378 case XCB_NOTIFY_DETAIL_POINTER
:
9381 case XCB_NOTIFY_DETAIL_POINTER_ROOT
:
9382 label
= "PointerRoot";
9384 case XCB_NOTIFY_DETAIL_NONE
:
9395 get_notify_mode_label(uint8_t mode
)
9400 case XCB_NOTIFY_MODE_NORMAL
:
9403 case XCB_NOTIFY_MODE_GRAB
:
9406 case XCB_NOTIFY_MODE_UNGRAB
:
9409 case XCB_NOTIFY_MODE_WHILE_GRABBED
:
9410 label
= "WhileGrabbed";
9421 enternotify(xcb_enter_notify_event_t
*e
)
9424 struct swm_region
*r
;
9426 DNPRINTF(SWM_D_FOCUS
, "enternotify: time: %u, win (x,y): %#x "
9427 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
9428 "child: %#x, same_screen_focus: %s, state: %d\n",
9429 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
9430 get_notify_mode_label(e
->mode
), e
->mode
,
9431 get_notify_detail_label(e
->detail
), e
->detail
,
9432 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
9433 YESNO(e
->same_screen_focus
), e
->state
);
9435 if (focus_mode
== SWM_FOCUS_MANUAL
&&
9436 e
->mode
== XCB_NOTIFY_MODE_NORMAL
) {
9437 DNPRINTF(SWM_D_EVENT
, "enternotify: manual focus; ignoring.\n");
9441 last_event_time
= e
->time
;
9443 if ((win
= find_window(e
->event
)) == NULL
) {
9444 if (e
->event
== e
->root
) {
9445 /* If no windows on pointer region, then focus root. */
9446 r
= root_to_region(e
->root
, SWM_CK_POINTER
);
9448 DNPRINTF(SWM_D_EVENT
, "enternotify: "
9449 "NULL region; ignoring.\n");
9455 DNPRINTF(SWM_D_EVENT
, "enternotify: window is NULL; "
9460 if (e
->mode
== XCB_NOTIFY_MODE_NORMAL
&&
9461 e
->detail
== XCB_NOTIFY_DETAIL_INFERIOR
) {
9462 DNPRINTF(SWM_D_EVENT
, "enternotify: entering from "
9463 "inferior; ignoring\n");
9467 focus_win(get_focus_magic(win
));
9475 leavenotify(xcb_leave_notify_event_t
*e
)
9477 DNPRINTF(SWM_D_FOCUS
, "leavenotify: time: %u, win (x,y): %#x "
9478 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
9479 "child: %#x, same_screen_focus: %s, state: %d\n",
9480 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
9481 get_notify_mode_label(e
->mode
), e
->mode
,
9482 get_notify_detail_label(e
->detail
), e
->detail
,
9483 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
9484 YESNO(e
->same_screen_focus
), e
->state
);
9489 mapnotify(xcb_map_notify_event_t
*e
)
9491 struct ws_win
*win
, *parent
= NULL
;
9492 struct workspace
*ws
;
9494 DNPRINTF(SWM_D_EVENT
, "mapnotify: win %#x\n", e
->window
);
9496 if ((win
= manage_window(e
->window
, 1)) == NULL
)
9500 /* Need to know if win was mapped due to ws switch. */
9501 if (ws
->state
== SWM_WS_STATE_MAPPED
) {
9502 if (ws
->focus_pending
&& TRANS(ws
->focus_pending
))
9503 parent
= find_window(win
->transient
);
9505 /* If window's parent is maximized, don't clear it. */
9506 if ((parent
== NULL
) || !MAXIMIZED(parent
))
9507 if (clear_maximized(ws
) > 0)
9512 set_win_state(win
, XCB_ICCCM_WM_STATE_NORMAL
);
9514 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& WS_FOCUSED(win
->ws
)) {
9515 if (ws
->focus_pending
== win
) {
9517 ws
->focus_pending
= NULL
;
9526 mappingnotify(xcb_mapping_notify_event_t
*e
)
9529 int i
, j
, num_screens
;
9531 xcb_refresh_keyboard_mapping(syms
, e
);
9533 if (e
->request
== XCB_MAPPING_KEYBOARD
) {
9536 /* Regrab buttons on all managed windows. */
9537 num_screens
= get_screen_count();
9538 for (i
= 0; i
< num_screens
; i
++)
9539 for (j
= 0; j
< workspace_limit
; j
++)
9540 TAILQ_FOREACH(w
, &screens
[i
].ws
[j
].winlist
,
9547 maprequest(xcb_map_request_event_t
*e
)
9549 struct ws_win
*win
, *w
= NULL
;
9550 xcb_get_window_attributes_reply_t
*war
;
9552 DNPRINTF(SWM_D_EVENT
, "maprequest: win %#x\n",
9555 war
= xcb_get_window_attributes_reply(conn
,
9556 xcb_get_window_attributes(conn
, e
->window
),
9559 DNPRINTF(SWM_D_EVENT
, "maprequest: window lost.\n");
9563 if (war
->override_redirect
) {
9564 DNPRINTF(SWM_D_EVENT
, "maprequest: override_redirect; "
9569 win
= manage_window(e
->window
,
9570 (war
->map_state
== XCB_MAP_STATE_VIEWABLE
));
9574 /* The new window should get focus; prepare. */
9575 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
9576 !(win
->quirks
& SWM_Q_NOFOCUSONMAP
) &&
9577 (!(win
->hints
.flags
& XCB_ICCCM_WM_HINT_INPUT
) ||
9578 (win
->hints
.flags
& XCB_ICCCM_WM_HINT_INPUT
&&
9579 win
->hints
.input
))) {
9580 if (win
->quirks
& SWM_Q_FOCUSONMAP_SINGLE
) {
9581 /* See if other wins of same type are already mapped. */
9582 TAILQ_FOREACH(w
, &win
->ws
->winlist
, entry
) {
9583 if (w
== win
|| !w
->mapped
)
9586 if (w
->ch
.class_name
&&
9587 win
->ch
.class_name
&&
9588 strcmp(w
->ch
.class_name
,
9589 win
->ch
.class_name
) == 0 &&
9590 w
->ch
.instance_name
&&
9591 win
->ch
.instance_name
&&
9592 strcmp(w
->ch
.instance_name
,
9593 win
->ch
.instance_name
) == 0)
9599 win
->ws
->focus_pending
= get_focus_magic(win
);
9602 /* All windows need to be mapped if they are in the current workspace.*/
9606 /* Ignore EnterNotify to handle the mapnotify without interference. */
9607 if (focus_mode
== SWM_FOCUS_DEFAULT
)
9608 event_drain(XCB_ENTER_NOTIFY
);
9611 DNPRINTF(SWM_D_EVENT
, "maprequest: done.\n");
9615 motionnotify(xcb_motion_notify_event_t
*e
)
9617 struct swm_region
*r
= NULL
;
9620 DNPRINTF(SWM_D_FOCUS
, "motionnotify: time: %u, win (x,y): %#x "
9621 "(%d,%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
9622 "child: %#x, same_screen_focus: %s, state: %d\n",
9623 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
9624 get_notify_detail_label(e
->detail
), e
->detail
,
9625 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
9626 YESNO(e
->same_screen
), e
->state
);
9628 last_event_time
= e
->time
;
9630 if (focus_mode
== SWM_FOCUS_MANUAL
)
9633 num_screens
= get_screen_count();
9634 for (i
= 0; i
< num_screens
; i
++)
9635 if (screens
[i
].root
== e
->root
)
9638 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
9639 if (X(r
) <= e
->root_x
&& e
->root_x
< MAX_X(r
) &&
9640 Y(r
) <= e
->root_y
&& e
->root_y
< MAX_Y(r
))
9648 get_atom_name(xcb_atom_t atom
)
9651 #ifdef SWM_DEBUG_ATOM_NAMES
9653 * This should be disabled during most debugging since
9654 * xcb_get_* causes an xcb_flush.
9657 xcb_get_atom_name_reply_t
*r
;
9659 r
= xcb_get_atom_name_reply(conn
,
9660 xcb_get_atom_name(conn
, atom
),
9663 len
= xcb_get_atom_name_name_length(r
);
9665 name
= malloc(len
+ 1);
9667 memcpy(name
, xcb_get_atom_name_name(r
), len
);
9681 propertynotify(xcb_property_notify_event_t
*e
)
9684 struct workspace
*ws
;
9688 name
= get_atom_name(e
->atom
);
9689 DNPRINTF(SWM_D_EVENT
, "propertynotify: win %#x, atom: %s(%u), "
9690 "time: %#x, state: %u\n", e
->window
, name
, e
->atom
, e
->time
,
9694 win
= find_window(e
->window
);
9700 last_event_time
= e
->time
;
9702 if (e
->atom
== a_state
) {
9703 /* State just changed, make sure it gets focused if mapped. */
9704 if (e
->state
== XCB_PROPERTY_NEW_VALUE
) {
9705 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& WS_FOCUSED(ws
)) {
9707 ws
->focus_pending
== win
) {
9708 focus_win(ws
->focus_pending
);
9709 ws
->focus_pending
= NULL
;
9713 } else if (e
->atom
== XCB_ATOM_WM_CLASS
||
9714 e
->atom
== XCB_ATOM_WM_NAME
) {
9716 } else if (e
->atom
== a_prot
) {
9717 get_wm_protocols(win
);
9724 unmapnotify(xcb_unmap_notify_event_t
*e
)
9727 struct workspace
*ws
;
9729 DNPRINTF(SWM_D_EVENT
, "unmapnotify: win %#x\n", e
->window
);
9731 /* If we aren't managing the window, then ignore. */
9732 win
= find_window(e
->window
);
9733 if (win
== NULL
|| win
->id
!= e
->window
)
9736 /* Do nothing if already withdrawn. */
9737 if (!win
->mapped
&& !ICONIC(win
))
9743 /* If win was focused, make sure to focus on something else. */
9744 if (win
== ws
->focus
) {
9745 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
9746 ws
->focus_pending
= get_focus_prev(win
);
9747 DNPRINTF(SWM_D_EVENT
, "unmapnotify: "
9748 "focus_pending: %#x\n",
9749 WINID(ws
->focus_pending
));
9757 set_win_state(win
, XCB_ICCCM_WM_STATE_ICONIC
);
9760 set_win_state(win
, XCB_ICCCM_WM_STATE_WITHDRAWN
);
9761 unmanage_window(win
);
9767 /* Update focus if ws is active. */
9768 if (WS_FOCUSED(ws
)) {
9769 if (focus_mode
== SWM_FOCUS_FOLLOW
) {
9770 focus_win(get_pointer_win(ws
->r
->s
->root
));
9771 } else if (ws
->focus_pending
) {
9772 focus_win(ws
->focus_pending
);
9773 ws
->focus_pending
= NULL
;
9774 } else if (ws
->focus
== NULL
) {
9775 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
,
9776 ws
->r
->id
, XCB_CURRENT_TIME
);
9785 get_source_type_label(uint32_t type
)
9790 case EWMH_SOURCE_TYPE_NONE
:
9793 case EWMH_SOURCE_TYPE_NORMAL
:
9796 case EWMH_SOURCE_TYPE_OTHER
:
9808 clientmessage(xcb_client_message_event_t
*e
)
9811 struct swm_region
*r
= NULL
;
9815 xcb_map_request_event_t mre
;
9819 name
= get_atom_name(e
->type
);
9820 DNPRINTF(SWM_D_EVENT
, "clientmessage: win %#x, atom: %s(%u)\n",
9821 e
->window
, name
, e
->type
);
9825 if (e
->type
== ewmh
[_NET_CURRENT_DESKTOP
].atom
) {
9826 num_screens
= get_screen_count();
9827 for (i
= 0; i
< num_screens
; i
++)
9828 if (screens
[i
].root
== e
->window
) {
9829 r
= screens
[i
].r_focus
;
9833 if (r
&& e
->data
.data32
[0] < (uint32_t)workspace_limit
) {
9834 a
.id
= e
->data
.data32
[0];
9842 win
= find_window(e
->window
);
9844 if (e
->type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
9845 /* Manage the window with maprequest. */
9846 DNPRINTF(SWM_D_EVENT
, "clientmessage: request focus on "
9847 "unmanaged window.\n");
9848 mre
.window
= e
->window
;
9854 if (e
->type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
9855 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_ACTIVE_WINDOW, "
9856 "source_type: %s(%d)\n",
9857 get_source_type_label(e
->data
.data32
[0]),
9861 * Allow focus changes that are a result of direct user
9862 * action and from applications that use the old EWMH spec.
9864 if (e
->data
.data32
[0] != EWMH_SOURCE_TYPE_NORMAL
||
9865 win
->quirks
& SWM_Q_OBEYAPPFOCUSREQ
) {
9866 if (WS_FOCUSED(win
->ws
))
9869 win
->ws
->focus_pending
= win
;
9871 } else if (e
->type
== ewmh
[_NET_CLOSE_WINDOW
].atom
) {
9872 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_CLOSE_WINDOW\n");
9873 if (win
->can_delete
)
9874 client_msg(win
, a_delete
, 0);
9876 xcb_kill_client(conn
, win
->id
);
9877 } else if (e
->type
== ewmh
[_NET_MOVERESIZE_WINDOW
].atom
) {
9878 DNPRINTF(SWM_D_EVENT
,
9879 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
9881 if (e
->data
.data32
[0] & (1<<8)) /* x */
9882 X(win
) = e
->data
.data32
[1];
9883 if (e
->data
.data32
[0] & (1<<9)) /* y */
9884 Y(win
) = e
->data
.data32
[2];
9885 if (e
->data
.data32
[0] & (1<<10)) /* width */
9886 WIDTH(win
) = e
->data
.data32
[3];
9887 if (e
->data
.data32
[0] & (1<<11)) /* height */
9888 HEIGHT(win
) = e
->data
.data32
[4];
9892 /* Notify no change was made. */
9893 config_win(win
, NULL
);
9894 /* TODO: Change stack sizes */
9896 } else if (e
->type
== ewmh
[_NET_RESTACK_WINDOW
].atom
) {
9897 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_RESTACK_WINDOW\n");
9898 val
[0] = e
->data
.data32
[1]; /* Sibling window. */
9899 val
[1] = e
->data
.data32
[2]; /* Stack mode detail. */
9901 xcb_configure_window(conn
, win
->id
, XCB_CONFIG_WINDOW_SIBLING
|
9902 XCB_CONFIG_WINDOW_STACK_MODE
, val
);
9903 } else if (e
->type
== ewmh
[_NET_WM_STATE
].atom
) {
9904 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_WM_STATE\n");
9905 ewmh_change_wm_state(win
, e
->data
.data32
[1], e
->data
.data32
[0]);
9906 if (e
->data
.data32
[2])
9907 ewmh_change_wm_state(win
, e
->data
.data32
[2],
9910 ewmh_update_wm_state(win
);
9912 } else if (e
->type
== ewmh
[_NET_WM_DESKTOP
].atom
) {
9913 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_WM_DESKTOP\n");
9916 win_to_ws(win
, e
->data
.data32
[0], 1);
9918 /* Restack if either the source or destination ws is mapped. */
9919 if (r
!= NULL
|| win
->ws
->r
!= NULL
) {
9921 load_float_geom(win
);
9933 int errcode
= xcb_connection_has_error(conn
);
9934 #ifdef XCB_CONN_ERROR
9937 case XCB_CONN_ERROR
:
9938 s
= "Socket error, pipe error or other stream error.";
9940 case XCB_CONN_CLOSED_EXT_NOTSUPPORTED
:
9941 s
= "Extension not supported.";
9943 case XCB_CONN_CLOSED_MEM_INSUFFICIENT
:
9944 s
= "Insufficient memory.";
9946 case XCB_CONN_CLOSED_REQ_LEN_EXCEED
:
9947 s
= "Request length was exceeded.";
9949 case XCB_CONN_CLOSED_PARSE_ERR
:
9950 s
= "Error parsing display string.";
9953 s
= "Unknown error.";
9956 errx(errcode
, "X CONNECTION ERROR: %s", s
);
9959 errx(errcode
, "X CONNECTION ERROR");
9967 const uint32_t val
= XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT
|
9968 XCB_EVENT_MASK_ENTER_WINDOW
;
9970 xcb_void_cookie_t wac
;
9971 xcb_generic_error_t
*error
;
9973 /* this causes an error if some other window manager is running */
9974 num_screens
= get_screen_count();
9975 for (i
= 0; i
< num_screens
; i
++) {
9976 if ((sc
= get_screen(i
)) == NULL
)
9977 errx(1, "ERROR: can't get screen %d.", i
);
9978 DNPRINTF(SWM_D_INIT
, "enable_wm: screen %d, root: %#x\n",
9980 wac
= xcb_change_window_attributes_checked(conn
, sc
->root
,
9981 XCB_CW_EVENT_MASK
, &val
);
9982 if ((error
= xcb_request_check(conn
, wac
))) {
9983 DNPRINTF(SWM_D_INIT
, "enable_wm: error_code: %u\n",
9989 /* click to focus on empty region */
9990 xcb_grab_button(conn
, 1, sc
->root
, BUTTONMASK
,
9991 XCB_GRAB_MODE_SYNC
, XCB_GRAB_MODE_ASYNC
, XCB_WINDOW_NONE
,
9992 XCB_CURSOR_NONE
, XCB_BUTTON_INDEX_1
, XCB_BUTTON_MASK_ANY
);
9999 new_region(struct swm_screen
*s
, int x
, int y
, int w
, int h
)
10001 struct swm_region
*r
= NULL
, *n
;
10002 struct workspace
*ws
= NULL
;
10006 DNPRINTF(SWM_D_MISC
, "new region: screen[%d]:%dx%d+%d+%d\n",
10007 s
->idx
, w
, h
, x
, y
);
10009 /* remove any conflicting regions */
10010 n
= TAILQ_FIRST(&s
->rl
);
10013 n
= TAILQ_NEXT(r
, entry
);
10014 if (X(r
) < (x
+ w
) && (X(r
) + WIDTH(r
)) > x
&&
10015 Y(r
) < (y
+ h
) && (Y(r
) + HEIGHT(r
)) > y
) {
10016 if (r
->ws
->r
!= NULL
)
10017 r
->ws
->old_r
= r
->ws
->r
;
10020 xcb_destroy_window(conn
, r
->id
);
10021 TAILQ_REMOVE(&s
->rl
, r
, entry
);
10022 TAILQ_INSERT_TAIL(&s
->orl
, r
, entry
);
10026 /* search old regions for one to reuse */
10028 /* size + location match */
10029 TAILQ_FOREACH(r
, &s
->orl
, entry
)
10030 if (X(r
) == x
&& Y(r
) == y
&&
10031 HEIGHT(r
) == h
&& WIDTH(r
) == w
)
10035 TAILQ_FOREACH(r
, &s
->orl
, entry
)
10036 if (HEIGHT(r
) == h
&& WIDTH(r
) == w
)
10040 TAILQ_REMOVE(&s
->orl
, r
, entry
);
10041 /* try to use old region's workspace */
10042 if (r
->ws
->r
== NULL
)
10045 if ((r
= calloc(1, sizeof(struct swm_region
))) == NULL
)
10046 err(1, "new_region: calloc: failed to allocate memory "
10049 /* if we don't have a workspace already, find one */
10051 for (i
= 0; i
< workspace_limit
; i
++)
10052 if (s
->ws
[i
].r
== NULL
) {
10059 errx(1, "new_region: no free workspaces");
10061 if (ws
->state
== SWM_WS_STATE_HIDDEN
)
10062 ws
->state
= SWM_WS_STATE_MAPPING
;
10070 r
->ws_prior
= NULL
;
10073 TAILQ_INSERT_TAIL(&s
->rl
, r
, entry
);
10075 /* Invisible region window to detect pointer events on empty regions. */
10076 r
->id
= xcb_generate_id(conn
);
10077 wa
[0] = XCB_EVENT_MASK_POINTER_MOTION
|
10078 XCB_EVENT_MASK_POINTER_MOTION_HINT
;
10080 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, r
->id
, r
->s
->root
,
10081 X(r
), Y(r
), WIDTH(r
), HEIGHT(r
), 0, XCB_WINDOW_CLASS_INPUT_ONLY
,
10082 XCB_COPY_FROM_PARENT
, XCB_CW_EVENT_MASK
, wa
);
10084 /* Make sure region input is at the bottom. */
10085 wa
[0] = XCB_STACK_MODE_BELOW
;
10086 xcb_configure_window(conn
, r
->id
, XCB_CONFIG_WINDOW_STACK_MODE
, wa
);
10088 xcb_map_window(conn
, r
->id
);
10094 #ifdef SWM_XRR_HAS_CRTC
10097 #endif /* SWM_XRR_HAS_CRTC */
10098 struct swm_region
*r
;
10099 struct ws_win
*win
;
10101 xcb_randr_get_screen_resources_current_cookie_t src
;
10102 xcb_randr_get_screen_resources_current_reply_t
*srr
;
10103 xcb_randr_get_crtc_info_cookie_t cic
;
10104 xcb_randr_get_crtc_info_reply_t
*cir
= NULL
;
10105 xcb_randr_crtc_t
*crtc
;
10106 xcb_screen_t
*screen
;
10108 DNPRINTF(SWM_D_MISC
, "scan_xrandr: screen: %d\n", i
);
10110 if ((screen
= get_screen(i
)) == NULL
)
10111 errx(1, "ERROR: can't get screen %d.", i
);
10113 num_screens
= get_screen_count();
10114 if (i
>= num_screens
)
10115 errx(1, "scan_xrandr: invalid screen");
10117 /* remove any old regions */
10118 while ((r
= TAILQ_FIRST(&screens
[i
].rl
)) != NULL
) {
10119 r
->ws
->old_r
= r
->ws
->r
= NULL
;
10121 xcb_destroy_window(conn
, r
->id
);
10122 TAILQ_REMOVE(&screens
[i
].rl
, r
, entry
);
10123 TAILQ_INSERT_TAIL(&screens
[i
].orl
, r
, entry
);
10127 /* map virtual screens onto physical screens */
10128 #ifdef SWM_XRR_HAS_CRTC
10129 if (xrandr_support
) {
10130 src
= xcb_randr_get_screen_resources_current(conn
,
10132 srr
= xcb_randr_get_screen_resources_current_reply(conn
, src
,
10135 new_region(&screens
[i
], 0, 0,
10136 screen
->width_in_pixels
,
10137 screen
->height_in_pixels
);
10140 ncrtc
= srr
->num_crtcs
;
10142 crtc
= xcb_randr_get_screen_resources_current_crtcs(srr
);
10143 for (c
= 0; c
< ncrtc
; c
++) {
10144 cic
= xcb_randr_get_crtc_info(conn
, crtc
[c
],
10146 cir
= xcb_randr_get_crtc_info_reply(conn
, cic
, NULL
);
10149 if (cir
->num_outputs
== 0) {
10154 if (cir
->mode
== 0)
10155 new_region(&screens
[i
], 0, 0,
10156 screen
->width_in_pixels
,
10157 screen
->height_in_pixels
);
10159 new_region(&screens
[i
],
10160 cir
->x
, cir
->y
, cir
->width
, cir
->height
);
10165 #endif /* SWM_XRR_HAS_CRTC */
10167 /* If detection failed, create a single region that spans the screen. */
10168 if (TAILQ_EMPTY(&screens
[i
].rl
))
10169 new_region(&screens
[i
], 0, 0, screen
->width_in_pixels
,
10170 screen
->height_in_pixels
);
10173 /* Cleanup unused previously visible workspaces. */
10174 TAILQ_FOREACH(r
, &screens
[i
].orl
, entry
) {
10175 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
)
10177 r
->ws
->state
= SWM_WS_STATE_HIDDEN
;
10179 /* The screen shouldn't focus on an unused region. */
10180 if (screens
[i
].r_focus
== r
)
10181 screens
[i
].r_focus
= NULL
;
10184 DNPRINTF(SWM_D_MISC
, "scan_xrandr: done.\n");
10188 screenchange(xcb_randr_screen_change_notify_event_t
*e
)
10190 struct swm_region
*r
;
10191 int i
, num_screens
;
10193 DNPRINTF(SWM_D_EVENT
, "screenchange: root: %#x\n", e
->root
);
10195 num_screens
= get_screen_count();
10196 /* silly event doesn't include the screen index */
10197 for (i
= 0; i
< num_screens
; i
++)
10198 if (screens
[i
].root
== e
->root
)
10200 if (i
>= num_screens
)
10201 errx(1, "screenchange: screen not found");
10203 /* brute force for now, just re-enumerate the regions */
10207 print_win_geom(e
->root
);
10209 /* add bars to all regions */
10210 for (i
= 0; i
< num_screens
; i
++) {
10211 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
10217 /* Make sure a region has focus on each screen. */
10218 for (i
= 0; i
< num_screens
; i
++) {
10219 if (screens
[i
].r_focus
== NULL
) {
10220 r
= TAILQ_FIRST(&screens
[i
].rl
);
10229 /* Update workspace state on all regions. */
10230 for (i
= 0; i
< num_screens
; i
++)
10231 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
10232 r
->ws
->state
= SWM_WS_STATE_MAPPED
;
10238 struct swm_region
*r
= NULL
;
10239 xcb_window_t
*wins
= NULL
, trans
, *cwins
= NULL
;
10240 int i
, j
, k
, n
, no
, num_screens
, manage
, mapped
;
10243 xcb_query_tree_cookie_t qtc
;
10244 xcb_query_tree_reply_t
*qtr
;
10245 xcb_get_window_attributes_cookie_t gac
;
10246 xcb_get_window_attributes_reply_t
*gar
;
10247 xcb_get_property_cookie_t pc
;
10248 xcb_get_property_reply_t
*pr
;
10250 DNPRINTF(SWM_D_INIT
, "grab_windows: begin\n");
10251 num_screens
= get_screen_count();
10252 for (i
= 0; i
< num_screens
; i
++) {
10253 qtc
= xcb_query_tree(conn
, screens
[i
].root
);
10254 qtr
= xcb_query_tree_reply(conn
, qtc
, NULL
);
10257 wins
= xcb_query_tree_children(qtr
);
10258 no
= xcb_query_tree_children_length(qtr
);
10260 /* Try to sort windows according to _NET_CLIENT_LIST. */
10261 pr
= xcb_get_property_reply(conn
, xcb_get_property(conn
, 0,
10262 screens
[i
].root
, ewmh
[_NET_CLIENT_LIST
].atom
,
10263 XCB_ATOM_WINDOW
, 0, UINT32_MAX
), NULL
);
10265 cwins
= xcb_get_property_value(pr
);
10266 n
= xcb_get_property_value_length(pr
) /
10267 sizeof(xcb_atom_t
);
10269 for (j
= 0; j
< n
; ++j
) {
10270 for (k
= j
; k
< no
; ++k
) {
10271 if (wins
[k
] == cwins
[j
]) {
10272 /* Swap wins j and k. */
10274 wins
[j
] = cwins
[j
];
10282 /* attach windows to a region */
10283 /* normal windows */
10284 DNPRINTF(SWM_D_INIT
, "grab_windows: grab top level windows.\n");
10285 for (j
= 0; j
< no
; j
++) {
10286 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
10287 if (r
->id
== wins
[j
]) {
10288 DNPRINTF(SWM_D_INIT
, "grab_windows: "
10289 "skip %#x; region input window.\n",
10292 } else if (r
->bar
->id
== wins
[j
]) {
10293 DNPRINTF(SWM_D_INIT
, "grab_windows: "
10294 "skip %#x; region bar.\n",
10303 gac
= xcb_get_window_attributes(conn
, wins
[j
]);
10304 gar
= xcb_get_window_attributes_reply(conn
, gac
, NULL
);
10306 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
10307 "doesn't exist.\n", wins
[j
]);
10311 if (gar
->override_redirect
) {
10312 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
10313 "override_redirect set.\n", wins
[j
]);
10318 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
10319 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
10321 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
10322 "is transient for %#x.\n", wins
[j
], trans
);
10327 state
= get_win_state(wins
[j
]);
10328 manage
= state
!= XCB_ICCCM_WM_STATE_WITHDRAWN
;
10329 mapped
= gar
->map_state
== XCB_MAP_STATE_VIEWABLE
;
10330 if (mapped
|| manage
)
10331 manage_window(wins
[j
], mapped
);
10334 /* transient windows */
10335 DNPRINTF(SWM_D_INIT
, "grab_windows: grab transient windows.\n");
10336 for (j
= 0; j
< no
; j
++) {
10337 gac
= xcb_get_window_attributes(conn
, wins
[j
]);
10338 gar
= xcb_get_window_attributes_reply(conn
, gac
, NULL
);
10340 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
10341 "doesn't exist.\n", wins
[j
]);
10345 if (gar
->override_redirect
) {
10346 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
10347 "override_redirect set.\n", wins
[j
]);
10352 state
= get_win_state(wins
[j
]);
10353 manage
= state
!= XCB_ICCCM_WM_STATE_WITHDRAWN
;
10354 mapped
= gar
->map_state
== XCB_MAP_STATE_VIEWABLE
;
10355 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
10356 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
10357 &trans
, NULL
) && manage
)
10358 manage_window(wins
[j
], mapped
);
10363 DNPRINTF(SWM_D_INIT
, "grab_windows: done.\n");
10367 setup_screens(void)
10369 int i
, j
, k
, num_screens
;
10370 struct workspace
*ws
;
10371 uint32_t gcv
[1], wa
[1];
10372 const xcb_query_extension_reply_t
*qep
;
10373 xcb_screen_t
*screen
;
10374 xcb_randr_query_version_cookie_t c
;
10375 xcb_randr_query_version_reply_t
*r
;
10377 num_screens
= get_screen_count();
10378 if ((screens
= calloc(num_screens
,
10379 sizeof(struct swm_screen
))) == NULL
)
10380 err(1, "setup_screens: calloc: failed to allocate memory for "
10383 /* initial Xrandr setup */
10384 xrandr_support
= 0;
10385 qep
= xcb_get_extension_data(conn
, &xcb_randr_id
);
10386 if (qep
->present
) {
10387 c
= xcb_randr_query_version(conn
, 1, 1);
10388 r
= xcb_randr_query_version_reply(conn
, c
, NULL
);
10390 if (r
->major_version
>= 1) {
10391 xrandr_support
= 1;
10392 xrandr_eventbase
= qep
->first_event
;
10398 wa
[0] = cursors
[XC_LEFT_PTR
].cid
;
10400 /* map physical screens */
10401 for (i
= 0; i
< num_screens
; i
++) {
10402 DNPRINTF(SWM_D_WS
, "setup_screens: init screen: %d\n", i
);
10403 screens
[i
].idx
= i
;
10404 screens
[i
].r_focus
= NULL
;
10406 TAILQ_INIT(&screens
[i
].rl
);
10407 TAILQ_INIT(&screens
[i
].orl
);
10408 if ((screen
= get_screen(i
)) == NULL
)
10409 errx(1, "ERROR: can't get screen %d.", i
);
10410 screens
[i
].root
= screen
->root
;
10412 /* set default colors */
10413 setscreencolor("red", i
, SWM_S_COLOR_FOCUS
);
10414 setscreencolor("rgb:88/88/88", i
, SWM_S_COLOR_UNFOCUS
);
10415 setscreencolor("rgb:00/80/80", i
, SWM_S_COLOR_BAR_BORDER
);
10416 setscreencolor("rgb:00/40/40", i
,
10417 SWM_S_COLOR_BAR_BORDER_UNFOCUS
);
10418 setscreencolor("black", i
, SWM_S_COLOR_BAR
);
10419 setscreencolor("rgb:a0/a0/a0", i
, SWM_S_COLOR_BAR_FONT
);
10420 setscreencolor("red", i
, SWM_S_COLOR_FOCUS_MAXIMIZED
);
10421 setscreencolor("rgb:88/88/88", i
,
10422 SWM_S_COLOR_UNFOCUS_MAXIMIZED
);
10424 /* create graphics context on screen */
10425 screens
[i
].bar_gc
= xcb_generate_id(conn
);
10427 xcb_create_gc(conn
, screens
[i
].bar_gc
, screens
[i
].root
,
10428 XCB_GC_GRAPHICS_EXPOSURES
, gcv
);
10430 /* set default cursor */
10431 xcb_change_window_attributes(conn
, screens
[i
].root
,
10432 XCB_CW_CURSOR
, wa
);
10434 /* init all workspaces */
10435 /* XXX these should be dynamically allocated too */
10436 for (j
= 0; j
< SWM_WS_MAX
; j
++) {
10437 ws
= &screens
[i
].ws
[j
];
10440 ws
->bar_enabled
= 1;
10442 ws
->focus_prev
= NULL
;
10443 ws
->focus_pending
= NULL
;
10446 ws
->state
= SWM_WS_STATE_HIDDEN
;
10447 TAILQ_INIT(&ws
->stack
);
10448 TAILQ_INIT(&ws
->winlist
);
10449 TAILQ_INIT(&ws
->unmanagedlist
);
10451 for (k
= 0; layouts
[k
].l_stack
!= NULL
; k
++)
10452 if (layouts
[k
].l_config
!= NULL
)
10453 layouts
[k
].l_config(ws
,
10454 SWM_ARG_ID_STACKINIT
);
10455 ws
->cur_layout
= &layouts
[0];
10456 ws
->cur_layout
->l_string(ws
);
10461 if (xrandr_support
)
10462 xcb_randr_select_input(conn
, screens
[i
].root
,
10463 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE
);
10468 setup_globals(void)
10470 if ((bar_fonts
= strdup(SWM_BAR_FONTS
)) == NULL
)
10471 err(1, "setup_globals: strdup: failed to allocate memory.");
10473 if ((clock_format
= strdup("%a %b %d %R %Z %Y")) == NULL
)
10474 err(1, "setup_globals: strdup: failed to allocate memory.");
10476 if ((syms
= xcb_key_symbols_alloc(conn
)) == NULL
)
10477 errx(1, "unable to allocate key symbols");
10479 a_state
= get_atom_from_string("WM_STATE");
10480 a_prot
= get_atom_from_string("WM_PROTOCOLS");
10481 a_delete
= get_atom_from_string("WM_DELETE_WINDOW");
10482 a_net_supported
= get_atom_from_string("_NET_SUPPORTED");
10483 a_net_wm_check
= get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
10484 a_takefocus
= get_atom_from_string("WM_TAKE_FOCUS");
10485 a_utf8_string
= get_atom_from_string("UTF8_STRING");
10486 a_swm_ws
= get_atom_from_string("_SWM_WS");
10490 shutdown_cleanup(void)
10492 int i
, num_screens
;
10494 /* disable alarm because the following code may not be interrupted */
10496 if (signal(SIGALRM
, SIG_IGN
) == SIG_ERR
)
10497 err(1, "can't disable alarm");
10506 num_screens
= get_screen_count();
10507 for (i
= 0; i
< num_screens
; ++i
) {
10508 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
10509 screens
[i
].root
, XCB_CURRENT_TIME
);
10511 if (screens
[i
].bar_gc
!= XCB_NONE
)
10512 xcb_free_gc(conn
, screens
[i
].bar_gc
);
10513 if (!bar_font_legacy
) {
10514 XftColorFree(display
, DefaultVisual(display
, i
),
10515 DefaultColormap(display
, i
), &bar_font_color
);
10516 XftColorFree(display
, DefaultVisual(display
, i
),
10517 DefaultColormap(display
, i
), &search_font_color
);
10521 if (bar_font_legacy
)
10522 XFreeFontSet(display
, bar_fs
);
10524 XftFontClose(display
, bar_font
);
10527 xcb_key_symbols_free(syms
);
10529 xcb_disconnect(conn
);
10533 event_error(xcb_generic_error_t
*e
)
10537 DNPRINTF(SWM_D_EVENT
, "event_error: %s(%u) from %s(%u), sequence: %u, "
10538 "resource_id: %u, minor_code: %u\n",
10539 xcb_event_get_error_label(e
->error_code
), e
->error_code
,
10540 xcb_event_get_request_label(e
->major_code
), e
->major_code
,
10541 e
->sequence
, e
->resource_id
, e
->minor_code
);
10545 event_handle(xcb_generic_event_t
*evt
)
10547 uint8_t type
= XCB_EVENT_RESPONSE_TYPE(evt
);
10549 DNPRINTF(SWM_D_EVENT
, "XCB Event: %s(%d), seq %u\n",
10550 xcb_event_get_label(XCB_EVENT_RESPONSE_TYPE(evt
)),
10551 XCB_EVENT_RESPONSE_TYPE(evt
), evt
->sequence
);
10554 #define EVENT(type, callback) case type: callback((void *)evt); return
10555 EVENT(0, event_error
);
10556 EVENT(XCB_BUTTON_PRESS
, buttonpress
);
10557 /*EVENT(XCB_BUTTON_RELEASE, buttonpress);*/
10558 /*EVENT(XCB_CIRCULATE_NOTIFY, );*/
10559 /*EVENT(XCB_CIRCULATE_REQUEST, );*/
10560 EVENT(XCB_CLIENT_MESSAGE
, clientmessage
);
10561 /*EVENT(XCB_COLORMAP_NOTIFY, );*/
10562 EVENT(XCB_CONFIGURE_NOTIFY
, configurenotify
);
10563 EVENT(XCB_CONFIGURE_REQUEST
, configurerequest
);
10564 /*EVENT(XCB_CREATE_NOTIFY, );*/
10565 EVENT(XCB_DESTROY_NOTIFY
, destroynotify
);
10566 EVENT(XCB_ENTER_NOTIFY
, enternotify
);
10567 EVENT(XCB_EXPOSE
, expose
);
10569 EVENT(XCB_FOCUS_IN
, focusin
);
10570 EVENT(XCB_FOCUS_OUT
, focusout
);
10572 /*EVENT(XCB_GRAPHICS_EXPOSURE, );*/
10573 /*EVENT(XCB_GRAVITY_NOTIFY, );*/
10574 EVENT(XCB_KEY_PRESS
, keypress
);
10575 /*EVENT(XCB_KEY_RELEASE, keypress);*/
10576 /*EVENT(XCB_KEYMAP_NOTIFY, );*/
10578 EVENT(XCB_LEAVE_NOTIFY
, leavenotify
);
10580 EVENT(XCB_MAP_NOTIFY
, mapnotify
);
10581 EVENT(XCB_MAP_REQUEST
, maprequest
);
10582 EVENT(XCB_MAPPING_NOTIFY
, mappingnotify
);
10583 EVENT(XCB_MOTION_NOTIFY
, motionnotify
);
10584 /*EVENT(XCB_NO_EXPOSURE, );*/
10585 EVENT(XCB_PROPERTY_NOTIFY
, propertynotify
);
10586 /*EVENT(XCB_REPARENT_NOTIFY, );*/
10587 /*EVENT(XCB_RESIZE_REQUEST, );*/
10588 /*EVENT(XCB_SELECTION_CLEAR, );*/
10589 /*EVENT(XCB_SELECTION_NOTIFY, );*/
10590 /*EVENT(XCB_SELECTION_REQUEST, );*/
10591 EVENT(XCB_UNMAP_NOTIFY
, unmapnotify
);
10592 /*EVENT(XCB_VISIBILITY_NOTIFY, );*/
10595 if (type
- xrandr_eventbase
== XCB_RANDR_SCREEN_CHANGE_NOTIFY
)
10596 screenchange((void *)evt
);
10600 main(int argc
, char *argv
[])
10602 struct swm_region
*r
;
10603 char conf
[PATH_MAX
], *cfile
= NULL
;
10605 int xfd
, i
, num_screens
, startup
= 1;
10606 struct sigaction sact
;
10607 xcb_generic_event_t
*evt
;
10611 int stdin_ready
= 0;
10614 /* suppress unused warning since var is needed */
10617 time_started
= time(NULL
);
10620 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION
, buildstr
);
10621 if (setlocale(LC_CTYPE
, "") == NULL
|| setlocale(LC_TIME
, "") == NULL
)
10622 warnx("no locale support");
10624 /* handle some signals */
10625 bzero(&sact
, sizeof(sact
));
10626 sigemptyset(&sact
.sa_mask
);
10628 sact
.sa_handler
= sighdlr
;
10629 sigaction(SIGINT
, &sact
, NULL
);
10630 sigaction(SIGQUIT
, &sact
, NULL
);
10631 sigaction(SIGTERM
, &sact
, NULL
);
10632 sigaction(SIGHUP
, &sact
, NULL
);
10634 sact
.sa_handler
= sighdlr
;
10635 sact
.sa_flags
= SA_NOCLDSTOP
;
10636 sigaction(SIGCHLD
, &sact
, NULL
);
10638 if ((display
= XOpenDisplay(0)) == NULL
)
10639 errx(1, "can not open display");
10641 conn
= XGetXCBConnection(display
);
10642 if (xcb_connection_has_error(conn
))
10643 errx(1, "can not get XCB connection");
10645 XSetEventQueueOwner(display
, XCBOwnsEventQueue
);
10647 xcb_prefetch_extension_data(conn
, &xcb_randr_id
);
10648 xfd
= xcb_get_file_descriptor(conn
);
10650 /* look for local and global conf file */
10651 pwd
= getpwuid(getuid());
10653 errx(1, "invalid user: %d", getuid());
10655 xcb_grab_server(conn
);
10656 xcb_aux_sync(conn
);
10658 /* flush all events */
10659 while ((evt
= xcb_poll_for_event(conn
))) {
10660 if (XCB_EVENT_RESPONSE_TYPE(evt
) == 0)
10666 errx(1, "another window manager is currently running");
10668 /* Load Xcursors and/or cursorfont glyph cursors. */
10671 xcb_aux_sync(conn
);
10681 for (i
= 0; ; i
++) {
10686 snprintf(conf
, sizeof conf
, "%s/.%s",
10687 pwd
->pw_dir
, SWM_CONF_FILE
);
10691 snprintf(conf
, sizeof conf
, "/etc/%s",
10696 snprintf(conf
, sizeof conf
, "%s/.%s",
10697 pwd
->pw_dir
, SWM_CONF_FILE_OLD
);
10700 /* global compat */
10701 snprintf(conf
, sizeof conf
, "/etc/%s",
10702 SWM_CONF_FILE_OLD
);
10708 if (strlen(conf
) && stat(conf
, &sb
) != -1)
10709 if (S_ISREG(sb
.st_mode
)) {
10716 /* load conf (if any) */
10718 conf_load(cfile
, SWM_CONF_DEFAULT
);
10722 if (getenv("SWM_STARTED") == NULL
)
10723 setenv("SWM_STARTED", "YES", 1);
10725 /* setup all bars */
10726 num_screens
= get_screen_count();
10727 for (i
= 0; i
< num_screens
; i
++)
10728 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
10731 /* Manage existing windows. */
10738 xcb_ungrab_server(conn
);
10741 /* Update state of each newly mapped workspace. */
10742 for (i
= 0; i
< num_screens
; i
++)
10743 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
10744 r
->ws
->state
= SWM_WS_STATE_MAPPED
;
10746 rd_max
= xfd
> STDIN_FILENO
? xfd
: STDIN_FILENO
;
10749 while ((evt
= xcb_poll_for_event(conn
))) {
10756 /* If just (re)started, set default focus if needed. */
10760 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
10761 r
= TAILQ_FIRST(&screens
[0].rl
);
10773 FD_SET(STDIN_FILENO
, &rd
);
10778 num_readable
= select(rd_max
+ 1, &rd
, NULL
, NULL
, &tv
);
10779 if (num_readable
== -1 && errno
!= EINTR
) {
10780 DNPRINTF(SWM_D_MISC
, "select failed");
10781 } else if (num_readable
> 0 && FD_ISSET(STDIN_FILENO
, &rd
)) {
10786 restart(NULL
, NULL
);
10796 bar_extra_update();
10803 shutdown_cleanup();