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-2013 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__)
91 #include <X11/cursorfont.h>
92 #include <X11/extensions/Xrandr.h>
93 #include <X11/Xcursor/Xcursor.h>
94 #include <X11/Xft/Xft.h>
95 #include <X11/Xlib-xcb.h>
96 #include <xcb/xcb_atom.h>
97 #include <xcb/xcb_aux.h>
98 #include <xcb/xcb_event.h>
99 #include <xcb/xcb_icccm.h>
100 #include <xcb/xcb_keysyms.h>
101 #include <xcb/xtest.h>
102 #include <xcb/randr.h>
110 #ifdef SPECTRWM_BUILDSTR
111 static const char *buildstr
= SPECTRWM_BUILDSTR
;
113 static const char *buildstr
= SPECTRWM_VERSION
;
116 #if !defined(__CYGWIN__) /* cygwin chokes on xrandr stuff */
118 # error XRandR versions less than 1.0 are not supported
121 # if RANDR_MAJOR >= 1
122 # if RANDR_MINOR >= 2
123 # define SWM_XRR_HAS_CRTC
126 #endif /* __CYGWIN__ */
128 #ifndef XCB_ICCCM_NUM_WM_HINTS_ELEMENTS
129 #define XCB_ICCCM_SIZE_HINT_P_MIN_SIZE XCB_SIZE_HINT_P_MIN_SIZE
130 #define XCB_ICCCM_SIZE_HINT_P_MAX_SIZE XCB_SIZE_HINT_P_MAX_SIZE
131 #define XCB_ICCCM_SIZE_HINT_P_RESIZE_INC XCB_SIZE_HINT_P_RESIZE_INC
132 #define XCB_ICCCM_WM_HINT_INPUT XCB_WM_HINT_INPUT
133 #define XCB_ICCCM_WM_HINT_X_URGENCY XCB_WM_HINT_X_URGENCY
134 #define XCB_ICCCM_WM_STATE_ICONIC XCB_WM_STATE_ICONIC
135 #define XCB_ICCCM_WM_STATE_WITHDRAWN XCB_WM_STATE_WITHDRAWN
136 #define XCB_ICCCM_WM_STATE_NORMAL XCB_WM_STATE_NORMAL
137 #define xcb_icccm_get_text_property_reply_t xcb_get_text_property_reply_t
138 #define xcb_icccm_get_text_property_reply_wipe xcb_get_text_property_reply_wipe
139 #define xcb_icccm_get_wm_class xcb_get_wm_class
140 #define xcb_icccm_get_wm_class_reply xcb_get_wm_class_reply
141 #define xcb_icccm_get_wm_class_reply_t xcb_get_wm_class_reply_t
142 #define xcb_icccm_get_wm_class_reply_wipe xcb_get_wm_class_reply_wipe
143 #define xcb_icccm_get_wm_hints xcb_get_wm_hints
144 #define xcb_icccm_get_wm_hints_reply xcb_get_wm_hints_reply
145 #define xcb_icccm_get_wm_name xcb_get_wm_name
146 #define xcb_icccm_get_wm_name_reply xcb_get_wm_name_reply
147 #define xcb_icccm_get_wm_normal_hints xcb_get_wm_normal_hints
148 #define xcb_icccm_get_wm_normal_hints_reply xcb_get_wm_normal_hints_reply
149 #define xcb_icccm_get_wm_protocols xcb_get_wm_protocols
150 #define xcb_icccm_get_wm_protocols_reply xcb_get_wm_protocols_reply
151 #define xcb_icccm_get_wm_protocols_reply_t xcb_get_wm_protocols_reply_t
152 #define xcb_icccm_get_wm_protocols_reply_wipe xcb_get_wm_protocols_reply_wipe
153 #define xcb_icccm_get_wm_transient_for xcb_get_wm_transient_for
154 #define xcb_icccm_get_wm_transient_for_reply xcb_get_wm_transient_for_reply
155 #define xcb_icccm_wm_hints_t xcb_wm_hints_t
158 /*#define SWM_DEBUG*/
160 #define DPRINTF(x...) do { \
162 fprintf(stderr, x); \
164 #define DNPRINTF(n,x...) do { \
165 if (swm_debug & n) { \
166 fprintf(stderr, "%ld ", (long)(time(NULL) - time_started)); \
167 fprintf(stderr, x); \
170 #define SWM_D_MISC 0x0001
171 #define SWM_D_EVENT 0x0002
172 #define SWM_D_WS 0x0004
173 #define SWM_D_FOCUS 0x0008
174 #define SWM_D_MOVE 0x0010
175 #define SWM_D_STACK 0x0020
176 #define SWM_D_MOUSE 0x0040
177 #define SWM_D_PROP 0x0080
178 #define SWM_D_CLASS 0x0100
179 #define SWM_D_KEY 0x0200
180 #define SWM_D_QUIRK 0x0400
181 #define SWM_D_SPAWN 0x0800
182 #define SWM_D_EVENTQ 0x1000
183 #define SWM_D_CONF 0x2000
184 #define SWM_D_BAR 0x4000
185 #define SWM_D_INIT 0x8000
187 u_int32_t swm_debug
= 0
206 #define DPRINTF(x...)
207 #define DNPRINTF(n,x...)
210 /* convert 8-bit to 16-bit */
211 #define RGB_8_TO_16(col) ((col) << 8) + (col)
213 #define PIXEL_TO_XRENDERCOLOR(px, xrc) \
214 xrc.red = RGB_8_TO_16((px) >> 16 & 0xff); \
215 xrc.green = RGB_8_TO_16((px) >> 8 & 0xff); \
216 xrc.blue = RGB_8_TO_16((px) & 0xff); \
219 #define LENGTH(x) (int)(sizeof (x) / sizeof (x)[0])
220 #define MODKEY XCB_MOD_MASK_1
221 #define CLEANMASK(mask) ((mask) & ~(numlockmask | XCB_MOD_MASK_LOCK))
222 #define BUTTONMASK (XCB_EVENT_MASK_BUTTON_PRESS | \
223 XCB_EVENT_MASK_BUTTON_RELEASE)
224 #define MOUSEMASK (BUTTONMASK|XCB_EVENT_MASK_POINTER_MOTION)
225 #define SWM_PROPLEN (16)
226 #define SWM_FUNCNAME_LEN (32)
227 #define SWM_KEYS_LEN (255)
228 #define SWM_QUIRK_LEN (64)
229 #define X(r) (r)->g.x
230 #define Y(r) (r)->g.y
231 #define WIDTH(r) (r)->g.w
232 #define HEIGHT(r) (r)->g.h
233 #define BORDER(w) ((w)->bordered ? border_width : 0)
234 #define MAX_X(r) ((r)->g.x + (r)->g.w)
235 #define MAX_Y(r) ((r)->g.y + (r)->g.h)
236 #define SH_MIN(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE
237 #define SH_MIN_W(w) (w)->sh.min_width
238 #define SH_MIN_H(w) (w)->sh.min_height
239 #define SH_MAX(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MAX_SIZE
240 #define SH_MAX_W(w) (w)->sh.max_width
241 #define SH_MAX_H(w) (w)->sh.max_height
242 #define SH_INC(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_RESIZE_INC
243 #define SH_INC_W(w) (w)->sh.width_inc
244 #define SH_INC_H(w) (w)->sh.height_inc
245 #define SWM_MAX_FONT_STEPS (3)
246 #define WINID(w) ((w) ? (w)->id : XCB_WINDOW_NONE)
247 #define YESNO(x) ((x) ? "yes" : "no")
249 /* Constrain Window flags */
250 #define SWM_CW_RESIZABLE (0x01)
251 #define SWM_CW_SOFTBOUNDARY (0x02)
252 #define SWM_CW_HARDBOUNDARY (0x04)
253 #define SWM_CW_RIGHT (0x10)
254 #define SWM_CW_LEFT (0x20)
255 #define SWM_CW_BOTTOM (0x40)
256 #define SWM_CW_TOP (0x80)
257 #define SWM_CW_ALLSIDES (0xf0)
259 #define SWM_FOCUS_DEFAULT (0)
260 #define SWM_FOCUS_FOLLOW (1)
261 #define SWM_FOCUS_MANUAL (2)
263 #define SWM_CK_NONE 0
264 #define SWM_CK_ALL 0xf
265 #define SWM_CK_FOCUS 0x1
266 #define SWM_CK_POINTER 0x2
267 #define SWM_CK_FALLBACK 0x4
268 #define SWM_CK_REGION 0x8
270 #define SWM_CONF_DEFAULT (0)
271 #define SWM_CONF_KEYMAPPING (1)
274 #define SWM_LIB "/usr/local/lib/libswmhack.so"
281 xcb_atom_t a_takefocus
;
283 xcb_atom_t a_netwmname
;
284 xcb_atom_t a_utf8_string
;
286 xcb_atom_t a_swm_iconic
;
288 volatile sig_atomic_t running
= 1;
289 volatile sig_atomic_t restart_wm
= 0;
290 xcb_timestamp_t last_event_time
= 0;
294 int xrandr_eventbase
;
295 unsigned int numlockmask
= 0;
298 xcb_connection_t
*conn
;
299 xcb_key_symbols_t
*syms
;
301 int boundary_width
= 50;
303 int cycle_visible
= 0;
305 int font_adjusted
= 0;
306 unsigned int mod_key
= MODKEY
;
309 struct swm_region
*search_r
;
310 int select_list_pipe
[2];
311 int select_resp_pipe
[2];
313 volatile sig_atomic_t search_resp
;
314 int search_resp_action
;
316 struct search_window
{
317 TAILQ_ENTRY(search_window
) entry
;
321 xcb_window_t indicator
;
323 TAILQ_HEAD(search_winlist
, search_window
);
324 struct search_winlist search_wl
;
329 SWM_SEARCH_UNICONIFY
,
330 SWM_SEARCH_NAME_WORKSPACE
,
331 SWM_SEARCH_SEARCH_WORKSPACE
,
332 SWM_SEARCH_SEARCH_WINDOW
335 #define SWM_STACK_TOP (0)
336 #define SWM_STACK_BOTTOM (1)
337 #define SWM_STACK_ABOVE (2)
338 #define SWM_STACK_BELOW (3)
341 double dialog_ratio
= 0.6;
343 #define SWM_BAR_MAX (256)
344 #define SWM_BAR_JUSTIFY_LEFT (0)
345 #define SWM_BAR_JUSTIFY_CENTER (1)
346 #define SWM_BAR_JUSTIFY_RIGHT (2)
347 #define SWM_BAR_OFFSET (4)
348 #define SWM_BAR_FONTS "-*-terminus-medium-*-*-*-12-*-*-*-*-*-*-*," \
349 "-*-profont-*-*-*-*-12-*-*-*-*-*-*-*," \
350 "-*-times-medium-r-*-*-12-*-*-*-*-*-*-*," \
351 "-misc-fixed-medium-r-*-*-12-*-*-*-*-*-*-*," \
352 "-*-*-*-r-*-*-*-*-*-*-*-*-*-*"
354 #ifdef X_HAVE_UTF8_STRING
355 #define DRAWSTRING(x...) Xutf8DrawString(x)
357 #define DRAWSTRING(x...) XmbDrawString(x)
360 char *bar_argv
[] = { NULL
, NULL
};
362 char bar_ext
[SWM_BAR_MAX
];
363 char bar_ext_buf
[SWM_BAR_MAX
];
364 char bar_vertext
[SWM_BAR_MAX
];
367 int bar_border_width
= 1;
368 int bar_at_bottom
= 0;
372 int bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
373 char *bar_format
= NULL
;
374 int stack_enabled
= 1;
375 int clock_enabled
= 1;
376 int urgent_enabled
= 0;
377 char *clock_format
= NULL
;
378 int title_name_enabled
= 0;
379 int title_class_enabled
= 0;
380 int window_name_enabled
= 0;
381 int focus_mode
= SWM_FOCUS_DEFAULT
;
382 int focus_close
= SWM_STACK_BELOW
;
383 int focus_close_wrap
= 1;
384 int focus_default
= SWM_STACK_TOP
;
385 int spawn_position
= SWM_STACK_TOP
;
386 int disable_border
= 0;
387 int border_width
= 1;
388 int region_padding
= 0;
390 int verbose_layout
= 0;
394 XFontSetExtents
*bar_fs_extents
;
396 int bar_font_legacy
= 1;
398 XftColor bar_font_color
;
400 char *startup_exception
;
401 unsigned int nr_exceptions
= 0;
403 /* layout manager data */
404 struct swm_geometry
{
417 struct swm_geometry g
;
420 /* virtual "screens" */
422 TAILQ_ENTRY(swm_region
) entry
;
424 struct swm_geometry g
;
425 struct workspace
*ws
; /* current workspace on this region */
426 struct workspace
*ws_prior
; /* prior workspace on this region */
427 struct swm_screen
*s
; /* screen idx */
430 TAILQ_HEAD(swm_region_list
, swm_region
);
433 TAILQ_ENTRY(ws_win
) entry
;
435 xcb_window_t transient
;
436 struct ws_win
*focus_child
; /* focus on child transient */
437 struct swm_geometry g
; /* current geometry */
438 struct swm_geometry g_float
; /* region coordinates */
439 int g_floatvalid
; /* g_float geometry validity */
440 int floatmaxed
; /* whether maxed by max_stack */
446 unsigned int ewmh_flags
;
447 int font_size_boundary
[SWM_MAX_FONT_STEPS
];
453 unsigned long quirks
;
454 struct workspace
*ws
; /* always valid */
455 struct swm_screen
*s
; /* always valid, never changes */
456 xcb_get_geometry_reply_t
*wa
;
458 xcb_icccm_get_wm_class_reply_t ch
;
459 xcb_icccm_wm_hints_t hints
;
461 TAILQ_HEAD(ws_win_list
, ws_win
);
465 TAILQ_ENTRY(pid_e
) entry
;
469 TAILQ_HEAD(pid_list
, pid_e
);
470 struct pid_list pidlist
= TAILQ_HEAD_INITIALIZER(pidlist
);
472 /* layout handlers */
474 void vertical_config(struct workspace
*, int);
475 void vertical_stack(struct workspace
*, struct swm_geometry
*);
476 void horizontal_config(struct workspace
*, int);
477 void horizontal_stack(struct workspace
*, struct swm_geometry
*);
478 void max_stack(struct workspace
*, struct swm_geometry
*);
479 void plain_stacker(struct workspace
*);
480 void fancy_stacker(struct workspace
*);
483 void (*l_stack
)(struct workspace
*, struct swm_geometry
*);
484 void (*l_config
)(struct workspace
*, int);
486 #define SWM_L_FOCUSPREV (1<<0)
487 #define SWM_L_MAPONFOCUS (1<<1)
488 void (*l_string
)(struct workspace
*);
490 /* stack, configure */
491 { vertical_stack
, vertical_config
, 0, plain_stacker
},
492 { horizontal_stack
, horizontal_config
, 0, plain_stacker
},
494 SWM_L_MAPONFOCUS
| SWM_L_FOCUSPREV
, plain_stacker
},
495 { NULL
, NULL
, 0, NULL
},
498 /* position of max_stack mode in the layouts array, index into layouts! */
499 #define SWM_V_STACK (0)
500 #define SWM_H_STACK (1)
501 #define SWM_MAX_STACK (2)
503 #define SWM_H_SLICE (32)
504 #define SWM_V_SLICE (32)
506 /* define work spaces */
508 int idx
; /* workspace index */
509 char *name
; /* workspace name */
510 int always_raise
; /* raise windows on focus */
511 int bar_enabled
; /* bar visibility */
512 struct layout
*cur_layout
; /* current layout handlers */
513 struct ws_win
*focus
; /* may be NULL */
514 struct ws_win
*focus_prev
; /* may be NULL */
515 struct ws_win
*focus_pending
; /* may be NULL */
516 struct swm_region
*r
; /* may be NULL */
517 struct swm_region
*old_r
; /* may be NULL */
518 struct ws_win_list winlist
; /* list of windows in ws */
519 struct ws_win_list unmanagedlist
; /* list of dead windows in ws */
520 char stacker
[10]; /* display stacker and layout */
524 int horizontal_msize
;
526 int horizontal_stacks
;
537 SWM_S_COLOR_BAR_BORDER
,
538 SWM_S_COLOR_BAR_BORDER_UNFOCUS
,
539 SWM_S_COLOR_BAR_FONT
,
545 /* physical screen mapping */
546 #define SWM_WS_MAX (22) /* hard limit */
547 int workspace_limit
= 10; /* soft limit */
550 int idx
; /* screen index */
551 struct swm_region_list rl
; /* list of regions on this screen */
552 struct swm_region_list orl
; /* list of old regions */
554 struct workspace ws
[SWM_WS_MAX
];
555 struct swm_region
*r_focus
;
561 } c
[SWM_S_COLOR_MAX
];
563 xcb_gcontext_t bar_gc
;
566 struct swm_screen
*screens
;
568 /* args to functions */
571 #define SWM_ARG_ID_FOCUSNEXT (0)
572 #define SWM_ARG_ID_FOCUSPREV (1)
573 #define SWM_ARG_ID_FOCUSMAIN (2)
574 #define SWM_ARG_ID_SWAPNEXT (10)
575 #define SWM_ARG_ID_SWAPPREV (11)
576 #define SWM_ARG_ID_SWAPMAIN (12)
577 #define SWM_ARG_ID_MOVELAST (13)
578 #define SWM_ARG_ID_MASTERSHRINK (20)
579 #define SWM_ARG_ID_MASTERGROW (21)
580 #define SWM_ARG_ID_MASTERADD (22)
581 #define SWM_ARG_ID_MASTERDEL (23)
582 #define SWM_ARG_ID_FLIPLAYOUT (24)
583 #define SWM_ARG_ID_STACKRESET (30)
584 #define SWM_ARG_ID_STACKINIT (31)
585 #define SWM_ARG_ID_CYCLEWS_UP (40)
586 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
587 #define SWM_ARG_ID_CYCLERG_UP (42)
588 #define SWM_ARG_ID_CYCLERG_DOWN (43)
589 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
590 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
591 #define SWM_ARG_ID_CYCLEWS_MOVE_UP (46)
592 #define SWM_ARG_ID_CYCLEWS_MOVE_DOWN (47)
593 #define SWM_ARG_ID_STACKINC (50)
594 #define SWM_ARG_ID_STACKDEC (51)
595 #define SWM_ARG_ID_SS_ALL (60)
596 #define SWM_ARG_ID_SS_WINDOW (61)
597 #define SWM_ARG_ID_DONTCENTER (70)
598 #define SWM_ARG_ID_CENTER (71)
599 #define SWM_ARG_ID_KILLWINDOW (80)
600 #define SWM_ARG_ID_DELETEWINDOW (81)
601 #define SWM_ARG_ID_WIDTHGROW (90)
602 #define SWM_ARG_ID_WIDTHSHRINK (91)
603 #define SWM_ARG_ID_HEIGHTGROW (92)
604 #define SWM_ARG_ID_HEIGHTSHRINK (93)
605 #define SWM_ARG_ID_MOVEUP (100)
606 #define SWM_ARG_ID_MOVEDOWN (101)
607 #define SWM_ARG_ID_MOVELEFT (102)
608 #define SWM_ARG_ID_MOVERIGHT (103)
609 #define SWM_ARG_ID_BAR_TOGGLE (110)
610 #define SWM_ARG_ID_BAR_TOGGLE_WS (111)
616 TAILQ_ENTRY(quirk
) entry
;
620 #define SWM_Q_FLOAT (1<<0) /* float this window */
621 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
622 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
623 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
624 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
625 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
626 #define SWM_Q_NOFOCUSONMAP (1<<6) /* Don't focus on window when mapped. */
627 #define SWM_Q_FOCUSONMAP_SINGLE (1<<7) /* Only focus if single win of type. */
629 TAILQ_HEAD(quirk_list
, quirk
);
630 struct quirk_list quirks
= TAILQ_HEAD_INITIALIZER(quirks
);
633 * Supported EWMH hints should be added to
634 * both the enum and the ewmh array
639 _NET_MOVERESIZE_WINDOW
,
640 _NET_WM_ACTION_ABOVE
,
641 _NET_WM_ACTION_CLOSE
,
642 _NET_WM_ACTION_FULLSCREEN
,
644 _NET_WM_ACTION_RESIZE
,
645 _NET_WM_ALLOWED_ACTIONS
,
649 _NET_WM_STATE_FULLSCREEN
,
650 _NET_WM_STATE_HIDDEN
,
651 _NET_WM_STATE_MAXIMIZED_HORZ
,
652 _NET_WM_STATE_MAXIMIZED_VERT
,
653 _NET_WM_STATE_SKIP_PAGER
,
654 _NET_WM_STATE_SKIP_TASKBAR
,
656 _NET_WM_WINDOW_TYPE_DIALOG
,
657 _NET_WM_WINDOW_TYPE_DOCK
,
658 _NET_WM_WINDOW_TYPE_NORMAL
,
659 _NET_WM_WINDOW_TYPE_SPLASH
,
660 _NET_WM_WINDOW_TYPE_TOOLBAR
,
661 _NET_WM_WINDOW_TYPE_UTILITY
,
662 _SWM_WM_STATE_MANUAL
,
669 } ewmh
[SWM_EWMH_HINT_MAX
] = {
670 /* must be in same order as in the enum */
671 {"_NET_ACTIVE_WINDOW", XCB_ATOM_NONE
},
672 {"_NET_CLOSE_WINDOW", XCB_ATOM_NONE
},
673 {"_NET_MOVERESIZE_WINDOW", XCB_ATOM_NONE
},
674 {"_NET_WM_ACTION_ABOVE", XCB_ATOM_NONE
},
675 {"_NET_WM_ACTION_CLOSE", XCB_ATOM_NONE
},
676 {"_NET_WM_ACTION_FULLSCREEN", XCB_ATOM_NONE
},
677 {"_NET_WM_ACTION_MOVE", XCB_ATOM_NONE
},
678 {"_NET_WM_ACTION_RESIZE", XCB_ATOM_NONE
},
679 {"_NET_WM_ALLOWED_ACTIONS", XCB_ATOM_NONE
},
680 {"_NET_WM_NAME", XCB_ATOM_NONE
},
681 {"_NET_WM_STATE", XCB_ATOM_NONE
},
682 {"_NET_WM_STATE_ABOVE", XCB_ATOM_NONE
},
683 {"_NET_WM_STATE_FULLSCREEN", XCB_ATOM_NONE
},
684 {"_NET_WM_STATE_HIDDEN", XCB_ATOM_NONE
},
685 {"_NET_WM_STATE_MAXIMIZED_HORZ", XCB_ATOM_NONE
},
686 {"_NET_WM_STATE_MAXIMIZED_VERT", XCB_ATOM_NONE
},
687 {"_NET_WM_STATE_SKIP_PAGER", XCB_ATOM_NONE
},
688 {"_NET_WM_STATE_SKIP_TASKBAR", XCB_ATOM_NONE
},
689 {"_NET_WM_WINDOW_TYPE", XCB_ATOM_NONE
},
690 {"_NET_WM_WINDOW_TYPE_DIALOG", XCB_ATOM_NONE
},
691 {"_NET_WM_WINDOW_TYPE_DOCK", XCB_ATOM_NONE
},
692 {"_NET_WM_WINDOW_TYPE_NORMAL", XCB_ATOM_NONE
},
693 {"_NET_WM_WINDOW_TYPE_SPLASH", XCB_ATOM_NONE
},
694 {"_NET_WM_WINDOW_TYPE_TOOLBAR", XCB_ATOM_NONE
},
695 {"_NET_WM_WINDOW_TYPE_UTILITY", XCB_ATOM_NONE
},
696 {"_SWM_WM_STATE_MANUAL", XCB_ATOM_NONE
},
703 XC_BOTTOM_LEFT_CORNER
,
704 XC_BOTTOM_RIGHT_CORNER
,
712 char *name
; /* Name used by Xcursor .*/
713 uint8_t cf_char
; /* cursorfont index. */
715 } cursors
[XC_MAX
] = {
716 {"fleur", XC_fleur
, XCB_CURSOR_NONE
},
717 {"left_ptr", XC_left_ptr
, XCB_CURSOR_NONE
},
718 {"bottom_left_corner", XC_bottom_left_corner
, XCB_CURSOR_NONE
},
719 {"bottom_right_corner", XC_bottom_right_corner
, XCB_CURSOR_NONE
},
720 {"sizing", XC_sizing
, XCB_CURSOR_NONE
},
721 {"top_left_corner", XC_top_left_corner
, XCB_CURSOR_NONE
},
722 {"top_right_corner", XC_top_right_corner
, XCB_CURSOR_NONE
},
725 #define SWM_SPAWN_OPTIONAL 0x1
729 TAILQ_ENTRY(spawn_prog
) entry
;
735 TAILQ_HEAD(spawn_list
, spawn_prog
);
736 struct spawn_list spawns
= TAILQ_HEAD_INITIALIZER(spawns
);
738 /* user/key callable function IDs */
852 KF_DUMPWINS
, /* MUST BE LAST */
860 enum keyfuncid funcid
;
863 RB_HEAD(key_tree
, key
);
865 /* function prototypes */
866 void adjust_font(struct ws_win
*);
867 void bar_class_name(char *, size_t, struct swm_region
*);
868 void bar_class_title_name(char *, size_t, struct swm_region
*);
869 void bar_cleanup(struct swm_region
*);
870 void bar_extra_setup(void);
871 void bar_extra_stop(void);
872 int bar_extra_update(void);
873 void bar_fmt(const char *, char *, struct swm_region
*, size_t);
874 void bar_fmt_expand(char *, size_t);
876 void bar_print(struct swm_region
*, const char *);
877 void bar_print_legacy(struct swm_region
*, const char *);
878 void bar_replace(char *, char *, struct swm_region
*, size_t);
879 void bar_replace_pad(char *, int *, size_t);
880 char *bar_replace_seq(char *, char *, struct swm_region
*, size_t *, size_t);
881 void bar_setup(struct swm_region
*);
882 void bar_title_name(char *, size_t, struct swm_region
*);
883 void bar_toggle(struct swm_region
*, union arg
*);
884 void bar_urgent(char *, size_t);
885 void bar_window_float(char *, size_t, struct swm_region
*);
886 void bar_window_name(char *, size_t, struct swm_region
*);
887 void bar_workspace_name(char *, size_t, struct swm_region
*);
888 void buttonpress(xcb_button_press_event_t
*);
889 void check_conn(void);
890 void clear_keys(void);
891 void clientmessage(xcb_client_message_event_t
*);
892 void client_msg(struct ws_win
*, xcb_atom_t
, xcb_timestamp_t
);
893 int conf_load(const char *, int);
894 void configurenotify(xcb_configure_notify_event_t
*);
895 void configurerequest(xcb_configure_request_event_t
*);
896 void config_win(struct ws_win
*, xcb_configure_request_event_t
*);
897 void constrain_window(struct ws_win
*, struct swm_geometry
*, int *);
898 int count_win(struct workspace
*, int);
899 void cursors_cleanup(void);
900 void cursors_load(void);
901 void custom_region(char *);
902 void cyclerg(struct swm_region
*, union arg
*);
903 void cyclews(struct swm_region
*, union arg
*);
904 void cycle_layout(struct swm_region
*, union arg
*);
905 void destroynotify(xcb_destroy_notify_event_t
*);
906 void dumpwins(struct swm_region
*, union arg
*);
908 void enternotify(xcb_enter_notify_event_t
*);
909 void event_drain(uint8_t);
910 void event_error(xcb_generic_error_t
*);
911 void event_handle(xcb_generic_event_t
*);
912 void ewmh_autoquirk(struct ws_win
*);
913 void ewmh_get_win_state(struct ws_win
*);
914 int ewmh_set_win_fullscreen(struct ws_win
*, int);
915 void ewmh_update_actions(struct ws_win
*);
916 void ewmh_update_win_state(struct ws_win
*, xcb_atom_t
, long);
917 char *expand_tilde(const char *);
918 void expose(xcb_expose_event_t
*);
919 void fake_keypress(struct ws_win
*, xcb_keysym_t
, uint16_t);
920 struct pid_e
*find_pid(pid_t
);
921 struct ws_win
*find_unmanaged_window(xcb_window_t
);
922 struct ws_win
*find_window(xcb_window_t
);
923 void floating_toggle(struct swm_region
*, union arg
*);
924 int floating_toggle_win(struct ws_win
*);
925 void focus(struct swm_region
*, union arg
*);
927 void focusin(xcb_focus_in_event_t
*);
928 void focusout(xcb_focus_out_event_t
*);
930 void focus_flush(void);
931 void focus_region(struct swm_region
*);
932 void focusrg(struct swm_region
*, union arg
*);
933 void focus_win(struct ws_win
*);
934 void fontset_init(void);
935 void free_window(struct ws_win
*);
936 xcb_atom_t
get_atom_from_string(const char *);
938 char *get_atom_name(xcb_atom_t
);
940 struct ws_win
*get_focus_magic(struct ws_win
*);
941 struct ws_win
*get_focus_prev(struct ws_win
*);
943 char *get_notify_detail_label(uint8_t);
944 char *get_notify_mode_label(uint8_t);
946 struct ws_win
*get_pointer_win(xcb_window_t
);
947 struct ws_win
*get_region_focus(struct swm_region
*);
948 int get_region_index(struct swm_region
*);
949 xcb_screen_t
*get_screen(int);
950 xcb_window_t
get_sibling(struct ws_win
*, int);
951 int get_screen_count(void);
953 char *get_stack_mode_name(uint8_t);
955 int32_t get_swm_iconic(struct ws_win
*);
956 char *get_win_name(xcb_window_t
);
957 int get_ws_idx(xcb_window_t
);
958 uint32_t getstate(xcb_window_t
);
959 void grabbuttons(struct ws_win
*);
961 void grab_windows(void);
962 void iconify(struct swm_region
*, union arg
*);
964 void keypress(xcb_key_press_event_t
*);
965 int key_cmp(struct key
*, struct key
*);
966 void key_insert(unsigned int, KeySym
, enum keyfuncid
, const char *);
967 struct key
*key_lookup(unsigned int, KeySym
);
968 void key_remove(struct key
*);
969 void key_replace(struct key
*, unsigned int, KeySym
, enum keyfuncid
,
971 void kill_bar_extra_atexit(void);
972 void kill_refs(struct ws_win
*);
974 void leavenotify(xcb_leave_notify_event_t
*);
976 void load_float_geom(struct ws_win
*);
977 struct ws_win
*manage_window(xcb_window_t
, uint16_t);
978 void map_window(struct ws_win
*, xcb_window_t
);
979 void mapnotify(xcb_map_notify_event_t
*);
980 void mappingnotify(xcb_mapping_notify_event_t
*);
981 void maprequest(xcb_map_request_event_t
*);
982 void motionnotify(xcb_motion_notify_event_t
*);
983 void move(struct ws_win
*, union arg
*);
984 void move_step(struct swm_region
*, union arg
*);
985 uint32_t name_to_pixel(int, const char *);
986 void name_workspace(struct swm_region
*, union arg
*);
987 void new_region(struct swm_screen
*, int, int, int, int);
988 int parsekeys(char *, unsigned int, unsigned int *, KeySym
*);
989 int parsequirks(char *, unsigned long *);
990 int parse_rgb(const char *, uint16_t *, uint16_t *, uint16_t *);
991 void pressbutton(struct swm_region
*, union arg
*);
992 void priorws(struct swm_region
*, union arg
*);
994 void print_win_geom(xcb_window_t
);
996 void propertynotify(xcb_property_notify_event_t
*);
997 void quirk_insert(const char *, const char *, unsigned long);
998 void quirk_remove(struct quirk
*);
999 void quirk_replace(struct quirk
*, const char *, const char *,
1001 void quit(struct swm_region
*, union arg
*);
1002 void raise_toggle(struct swm_region
*, union arg
*);
1003 void region_containment(struct ws_win
*, struct swm_region
*, int);
1004 struct swm_region
*region_under(struct swm_screen
*, int, int);
1005 void regionize(struct ws_win
*, int, int);
1006 void resize(struct ws_win
*, union arg
*);
1007 void resize_step(struct swm_region
*, union arg
*);
1008 void restart(struct swm_region
*, union arg
*);
1009 struct swm_region
*root_to_region(xcb_window_t
, int);
1010 void screenchange(xcb_randr_screen_change_notify_event_t
*);
1011 void scan_xrandr(int);
1012 void search_do_resp(void);
1013 void search_resp_name_workspace(const char *, size_t);
1014 void search_resp_search_window(const char *);
1015 void search_resp_search_workspace(const char *);
1016 void search_resp_uniconify(const char *, size_t);
1017 void search_win(struct swm_region
*, union arg
*);
1018 void search_win_cleanup(void);
1019 void search_workspace(struct swm_region
*, union arg
*);
1020 void send_to_rg(struct swm_region
*, union arg
*);
1021 void send_to_ws(struct swm_region
*, union arg
*);
1022 void set_region(struct swm_region
*);
1023 int setautorun(char *, char *, int);
1024 int setconfbinding(char *, char *, int);
1025 int setconfcolor(char *, char *, int);
1026 int setconfmodkey(char *, char *, int);
1027 int setconfquirk(char *, char *, int);
1028 int setconfregion(char *, char *, int);
1029 int setconfspawn(char *, char *, int);
1030 int setconfvalue(char *, char *, int);
1031 void setkeybinding(unsigned int, KeySym
, enum keyfuncid
, const char *);
1032 int setkeymapping(char *, char *, int);
1033 int setlayout(char *, char *, int);
1034 void setquirk(const char *, const char *, unsigned long);
1035 void setscreencolor(char *, int, int);
1036 void setspawn(const char *, const char *, int);
1037 void setup_ewmh(void);
1038 void setup_globals(void);
1039 void setup_keys(void);
1040 void setup_quirks(void);
1041 void setup_screens(void);
1042 void setup_spawn(void);
1043 void set_child_transient(struct ws_win
*, xcb_window_t
*);
1044 void set_swm_iconic(struct ws_win
*, int);
1045 void set_win_state(struct ws_win
*, uint16_t);
1046 void shutdown_cleanup(void);
1048 void socket_setnonblock(int);
1049 void sort_windows(struct ws_win_list
*);
1050 void spawn(int, union arg
*, int);
1051 void spawn_custom(struct swm_region
*, union arg
*, const char *);
1052 int spawn_expand(struct swm_region
*, union arg
*, const char *, char ***);
1053 void spawn_insert(const char *, const char *, int);
1054 void spawn_remove(struct spawn_prog
*);
1055 void spawn_replace(struct spawn_prog
*, const char *, const char *, int);
1056 void spawn_select(struct swm_region
*, union arg
*, const char *, int *);
1057 void stack_config(struct swm_region
*, union arg
*);
1058 void stack_floater(struct ws_win
*, struct swm_region
*);
1059 void stack_master(struct workspace
*, struct swm_geometry
*, int, int);
1060 void store_float_geom(struct ws_win
*);
1061 char *strdupsafe(const char *);
1062 void swapwin(struct swm_region
*, union arg
*);
1063 void switchws(struct swm_region
*, union arg
*);
1064 void teardown_ewmh(void);
1065 void unfocus_win(struct ws_win
*);
1066 void uniconify(struct swm_region
*, union arg
*);
1067 void unmanage_window(struct ws_win
*);
1068 void unmapnotify(xcb_unmap_notify_event_t
*);
1069 void unmap_all(void);
1070 void unmap_window(struct ws_win
*);
1071 void updatenumlockmask(void);
1072 void update_modkey(unsigned int);
1073 void update_window(struct ws_win
*);
1074 void validate_spawns(void);
1075 int validate_win(struct ws_win
*);
1076 int validate_ws(struct workspace
*);
1077 /*void visibilitynotify(xcb_visibility_notify_event_t *);*/
1078 void version(struct swm_region
*, union arg
*);
1079 void win_to_ws(struct ws_win
*, int, int);
1080 pid_t
window_get_pid(xcb_window_t
);
1081 void wkill(struct swm_region
*, union arg
*);
1082 void workaround(void);
1083 void xft_init(struct swm_region
*);
1084 void _add_startup_exception(const char *, va_list);
1085 void add_startup_exception(const char *, ...);
1087 RB_PROTOTYPE(key_tree
, key
, entry
, key_cmp
);
1088 RB_GENERATE(key_tree
, key
, entry
, key_cmp
);
1089 struct key_tree keys
;
1094 xcb_font_t cf
= XCB_NONE
;
1097 for (i
= 0; i
< LENGTH(cursors
); ++i
) {
1098 /* try to load Xcursor first. */
1099 cursors
[i
].cid
= XcursorLibraryLoadCursor(display
,
1102 /* fallback to cursorfont. */
1103 if (cursors
[i
].cid
== XCB_CURSOR_NONE
) {
1104 if (cf
== XCB_NONE
) {
1105 cf
= xcb_generate_id(conn
);
1106 xcb_open_font(conn
, cf
, strlen("cursor"),
1110 cursors
[i
].cid
= xcb_generate_id(conn
);
1111 xcb_create_glyph_cursor(conn
, cursors
[i
].cid
, cf
, cf
,
1112 cursors
[i
].cf_char
, cursors
[i
].cf_char
+ 1, 0, 0, 0,
1113 0xffff, 0xffff, 0xffff);
1119 xcb_close_font(conn
, cf
);
1123 cursors_cleanup(void)
1126 for (i
= 0; i
< LENGTH(cursors
); ++i
)
1127 xcb_free_cursor(conn
, cursors
[i
].cid
);
1131 expand_tilde(const char *s
)
1133 struct passwd
*ppwd
;
1141 errx(1, "expand_tilde: NULL string.");
1144 result
= strdup(sc
);
1150 if ((max
= sysconf(_SC_LOGIN_NAME_MAX
)) == -1)
1151 errx(1, "expand_tilde: sysconf");
1153 if ((user
= calloc(1, max
+ 1)) == NULL
)
1154 errx(1, "expand_tilde: calloc");
1156 for (i
= 0; s
[i
] != '/' && s
[i
] != '\0'; ++i
)
1161 ppwd
= strlen(user
) == 0 ? getpwuid(getuid()) : getpwnam(user
);
1165 result
= strdup(sc
);
1167 if (asprintf(&result
, "%s%s", ppwd
->pw_dir
, s
) == -1)
1171 errx(1, "expand_tilde: failed to allocate memory.");
1177 parse_rgb(const char *rgb
, uint16_t *rr
, uint16_t *gg
, uint16_t *bb
)
1179 unsigned int tmpr
, tmpg
, tmpb
;
1181 if (sscanf(rgb
, "rgb:%x/%x/%x", &tmpr
, &tmpg
, &tmpb
) != 3)
1184 *rr
= RGB_8_TO_16(tmpr
);
1185 *gg
= RGB_8_TO_16(tmpg
);
1186 *bb
= RGB_8_TO_16(tmpb
);
1192 get_screen(int screen
)
1194 const xcb_setup_t
*r
;
1195 xcb_screen_iterator_t iter
;
1197 if ((r
= xcb_get_setup(conn
)) == NULL
) {
1198 DNPRINTF(SWM_D_MISC
, "get_screen: xcb_get_setup\n");
1202 iter
= xcb_setup_roots_iterator(r
);
1203 for (; iter
.rem
; --screen
, xcb_screen_next(&iter
))
1211 get_screen_count(void)
1213 const xcb_setup_t
*r
;
1215 if ((r
= xcb_get_setup(conn
)) == NULL
) {
1216 DNPRINTF(SWM_D_MISC
, "get_screen_count: xcb_get_setup\n");
1220 return xcb_setup_roots_length(r
);
1224 get_region_index(struct swm_region
*r
)
1226 struct swm_region
*rr
;
1232 TAILQ_FOREACH(rr
, &r
->s
->rl
, entry
) {
1247 if (focus_mode
== SWM_FOCUS_DEFAULT
)
1248 event_drain(XCB_ENTER_NOTIFY
);
1254 get_atom_from_string(const char *str
)
1256 xcb_intern_atom_cookie_t c
;
1257 xcb_intern_atom_reply_t
*r
;
1260 c
= xcb_intern_atom(conn
, 0, strlen(str
), str
);
1261 r
= xcb_intern_atom_reply(conn
, c
, NULL
);
1269 return (XCB_ATOM_NONE
);
1273 set_swm_iconic(struct ws_win
*win
, int newv
)
1280 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
1281 a_swm_iconic
, XCB_ATOM_INTEGER
, 32, 1, &v
);
1283 xcb_delete_property(conn
, win
->id
, a_swm_iconic
);
1287 get_swm_iconic(struct ws_win
*win
)
1290 xcb_get_property_reply_t
*pr
= NULL
;
1292 pr
= xcb_get_property_reply(conn
,
1293 xcb_get_property(conn
, 0, win
->id
, a_swm_iconic
,
1294 XCB_ATOM_INTEGER
, 0, 1), NULL
);
1297 if (pr
->type
!= XCB_ATOM_INTEGER
|| pr
->format
!= 32)
1299 v
= *((int32_t *)xcb_get_property_value(pr
));
1309 xcb_atom_t sup_list
;
1310 int i
, j
, num_screens
;
1312 sup_list
= get_atom_from_string("_NET_SUPPORTED");
1314 for (i
= 0; i
< LENGTH(ewmh
); i
++)
1315 ewmh
[i
].atom
= get_atom_from_string(ewmh
[i
].name
);
1317 num_screens
= get_screen_count();
1318 for (i
= 0; i
< num_screens
; i
++) {
1319 /* Support check window will be created by workaround(). */
1321 /* Report supported atoms */
1322 xcb_delete_property(conn
, screens
[i
].root
, sup_list
);
1323 for (j
= 0; j
< LENGTH(ewmh
); j
++)
1324 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
,
1325 screens
[i
].root
, sup_list
, XCB_ATOM_ATOM
, 32, 1,
1334 xcb_atom_t sup_check
, sup_list
;
1336 xcb_get_property_cookie_t pc
;
1337 xcb_get_property_reply_t
*pr
;
1339 sup_check
= get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
1340 sup_list
= get_atom_from_string("_NET_SUPPORTED");
1341 num_screens
= get_screen_count();
1343 for (i
= 0; i
< num_screens
; i
++) {
1344 /* Get the support check window and destroy it */
1345 pc
= xcb_get_property(conn
, 0, screens
[i
].root
, sup_check
,
1346 XCB_ATOM_WINDOW
, 0, 1);
1347 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
1350 if (pr
->format
== sup_check
) {
1351 id
= *((xcb_window_t
*)xcb_get_property_value(pr
));
1353 xcb_destroy_window(conn
, id
);
1354 xcb_delete_property(conn
, screens
[i
].root
, sup_check
);
1355 xcb_delete_property(conn
, screens
[i
].root
, sup_list
);
1362 ewmh_autoquirk(struct ws_win
*win
)
1366 xcb_get_property_cookie_t c
;
1367 xcb_get_property_reply_t
*r
;
1369 c
= xcb_get_property(conn
, 0, win
->id
,
1370 ewmh
[_NET_WM_WINDOW_TYPE
].atom
, XCB_ATOM_ATOM
, 0, UINT32_MAX
);
1371 r
= xcb_get_property_reply(conn
, c
, NULL
);
1375 n
= xcb_get_property_value_length(r
);
1376 type
= xcb_get_property_value(r
);
1378 for (i
= 0; i
< n
; i
++) {
1379 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_NORMAL
].atom
)
1381 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_DOCK
].atom
||
1382 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_TOOLBAR
].atom
||
1383 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_UTILITY
].atom
) {
1385 win
->quirks
= SWM_Q_FLOAT
| SWM_Q_ANYWHERE
;
1388 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_SPLASH
].atom
||
1389 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_DIALOG
].atom
) {
1391 win
->quirks
= SWM_Q_FLOAT
;
1398 #define SWM_EWMH_ACTION_COUNT_MAX (6)
1399 #define EWMH_F_FULLSCREEN (1<<0)
1400 #define EWMH_F_ABOVE (1<<1)
1401 #define EWMH_F_HIDDEN (1<<2)
1402 #define EWMH_F_SKIP_PAGER (1<<3)
1403 #define EWMH_F_SKIP_TASKBAR (1<<4)
1404 #define SWM_F_MANUAL (1<<5)
1407 ewmh_set_win_fullscreen(struct ws_win
*win
, int fs
)
1415 DNPRINTF(SWM_D_MISC
, "ewmh_set_win_fullscreen: window: 0x%x, "
1416 "fullscreen %s\n", win
->id
, YESNO(fs
));
1419 if (!win
->g_floatvalid
)
1420 store_float_geom(win
);
1422 win
->g
= win
->ws
->r
->g
;
1425 load_float_geom(win
);
1432 ewmh_update_actions(struct ws_win
*win
)
1434 xcb_atom_t actions
[SWM_EWMH_ACTION_COUNT_MAX
];
1440 actions
[n
++] = ewmh
[_NET_WM_ACTION_CLOSE
].atom
;
1442 if (win
->floating
) {
1443 actions
[n
++] = ewmh
[_NET_WM_ACTION_MOVE
].atom
;
1444 actions
[n
++] = ewmh
[_NET_WM_ACTION_RESIZE
].atom
;
1445 actions
[n
++] = ewmh
[_NET_WM_ACTION_ABOVE
].atom
;
1448 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
1449 ewmh
[_NET_WM_ALLOWED_ACTIONS
].atom
, XCB_ATOM_ATOM
, 32, 1, actions
);
1452 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
1453 #define _NET_WM_STATE_ADD 1 /* add/set property */
1454 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
1457 ewmh_update_win_state(struct ws_win
*win
, xcb_atom_t state
, long action
)
1459 unsigned int mask
= 0;
1460 unsigned int changed
= 0;
1461 unsigned int orig_flags
;
1466 DNPRINTF(SWM_D_PROP
, "ewmh_update_win_state: window: 0x%x, state: %ld, "
1467 "action: %ld\n", win
->id
, (unsigned long)state
, action
);
1469 if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
)
1470 mask
= EWMH_F_FULLSCREEN
;
1471 else if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
)
1472 mask
= EWMH_F_ABOVE
;
1473 else if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
)
1474 mask
= SWM_F_MANUAL
;
1475 else if (state
== ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
)
1476 mask
= EWMH_F_SKIP_PAGER
;
1477 else if (state
== ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
)
1478 mask
= EWMH_F_SKIP_TASKBAR
;
1480 orig_flags
= win
->ewmh_flags
;
1483 case _NET_WM_STATE_REMOVE
:
1484 win
->ewmh_flags
&= ~mask
;
1486 case _NET_WM_STATE_ADD
:
1487 win
->ewmh_flags
|= mask
;
1489 case _NET_WM_STATE_TOGGLE
:
1490 win
->ewmh_flags
^= mask
;
1494 changed
= (win
->ewmh_flags
& mask
) ^ (orig_flags
& mask
) ? 1 : 0;
1496 if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
) {
1497 if (changed
&& !floating_toggle_win(win
))
1498 win
->ewmh_flags
= orig_flags
; /* revert */
1499 } else if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
) {
1501 win
->manual
= (win
->ewmh_flags
& SWM_F_MANUAL
) != 0;
1502 } else if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
) {
1503 if (changed
&& !ewmh_set_win_fullscreen(win
,
1504 win
->ewmh_flags
& EWMH_F_FULLSCREEN
))
1505 win
->ewmh_flags
= orig_flags
; /* revert */
1508 xcb_delete_property(conn
, win
->id
, ewmh
[_NET_WM_STATE
].atom
);
1510 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
1511 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1512 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1513 &ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
);
1514 else if (win
->ewmh_flags
& EWMH_F_SKIP_PAGER
)
1515 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1516 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1517 &ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
);
1518 else if (win
->ewmh_flags
& EWMH_F_SKIP_TASKBAR
)
1519 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1520 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1521 &ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
);
1522 else if (win
->ewmh_flags
& EWMH_F_ABOVE
)
1523 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1524 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1525 &ewmh
[_NET_WM_STATE_ABOVE
].atom
);
1526 else if (win
->ewmh_flags
& SWM_F_MANUAL
)
1527 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1528 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1529 &ewmh
[_SWM_WM_STATE_MANUAL
].atom
);
1533 ewmh_get_win_state(struct ws_win
*win
)
1536 xcb_get_property_cookie_t c
;
1537 xcb_get_property_reply_t
*r
;
1543 win
->ewmh_flags
= 0;
1545 win
->ewmh_flags
|= EWMH_F_ABOVE
;
1547 win
->ewmh_flags
|= SWM_F_MANUAL
;
1549 c
= xcb_get_property(conn
, 0, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
1550 XCB_ATOM_ATOM
, 0, UINT32_MAX
);
1551 r
= xcb_get_property_reply(conn
, c
, NULL
);
1555 states
= xcb_get_property_value(r
);
1556 n
= xcb_get_property_value_length(r
);
1558 for (i
= 0; i
< n
; i
++)
1559 ewmh_update_win_state(win
, states
[i
], _NET_WM_STATE_ADD
);
1567 dumpwins(struct swm_region
*r
, union arg
*args
)
1571 xcb_get_window_attributes_cookie_t c
;
1572 xcb_get_window_attributes_reply_t
*wa
;
1574 /* suppress unused warning since var is needed */
1577 if (r
->ws
== NULL
) {
1578 warnx("dumpwins: invalid workspace");
1582 warnx("=== managed window list ws %02d ===", r
->ws
->idx
);
1583 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
1584 state
= getstate(win
->id
);
1585 c
= xcb_get_window_attributes(conn
, win
->id
);
1586 wa
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
1588 warnx("window: 0x%x, map_state: %d, state: %u, "
1589 "transient: 0x%x", win
->id
, wa
->map_state
,
1590 state
, win
->transient
);
1593 warnx("window: 0x%x, failed xcb_get_window_attributes",
1597 warnx("===== unmanaged window list =====");
1598 TAILQ_FOREACH(win
, &r
->ws
->unmanagedlist
, entry
) {
1599 state
= getstate(win
->id
);
1600 c
= xcb_get_window_attributes(conn
, win
->id
);
1601 wa
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
1603 warnx("window: 0x%x, map_state: %d, state: %u, "
1604 "transient: 0x%x", win
->id
, wa
->map_state
,
1605 state
, win
->transient
);
1608 warnx("window: 0x%x, failed xcb_get_window_attributes",
1612 warnx("=================================");
1616 dumpwins(struct swm_region
*r
, union arg
*s
)
1621 #endif /* SWM_DEBUG */
1626 int saved_errno
, status
;
1629 saved_errno
= errno
;
1633 while ((pid
= waitpid(WAIT_ANY
, &status
, WNOHANG
)) != 0) {
1638 if (errno
!= ECHILD
)
1639 warn("sighdlr: waitpid");
1640 #endif /* SWM_DEBUG */
1643 if (pid
== searchpid
)
1647 if (WIFEXITED(status
)) {
1648 if (WEXITSTATUS(status
) != 0)
1649 warnx("sighdlr: child exit status: %d",
1650 WEXITSTATUS(status
));
1652 warnx("sighdlr: child is terminated "
1654 #endif /* SWM_DEBUG */
1668 errno
= saved_errno
;
1674 struct pid_e
*p
= NULL
;
1676 DNPRINTF(SWM_D_MISC
, "find_pid: %d\n", pid
);
1681 TAILQ_FOREACH(p
, &pidlist
, entry
) {
1690 name_to_pixel(int sidx
, const char *colorname
)
1692 uint32_t result
= 0;
1693 char cname
[32] = "#";
1694 xcb_screen_t
*screen
;
1695 xcb_colormap_t cmap
;
1696 xcb_alloc_color_reply_t
*cr
;
1697 xcb_alloc_named_color_reply_t
*nr
;
1698 uint16_t rr
, gg
, bb
;
1700 screen
= get_screen(sidx
);
1701 cmap
= screen
->default_colormap
;
1703 /* color is in format rgb://rr/gg/bb */
1704 if (strncmp(colorname
, "rgb:", 4) == 0) {
1705 if (parse_rgb(colorname
, &rr
, &gg
, &bb
) == -1)
1706 warnx("could not parse rgb %s", colorname
);
1708 cr
= xcb_alloc_color_reply(conn
,
1709 xcb_alloc_color(conn
, cmap
, rr
, gg
, bb
),
1715 warnx("color '%s' not found", colorname
);
1718 nr
= xcb_alloc_named_color_reply(conn
,
1719 xcb_alloc_named_color(conn
, cmap
, strlen(colorname
),
1722 strlcat(cname
, colorname
+ 2, sizeof cname
- 1);
1723 nr
= xcb_alloc_named_color_reply(conn
,
1724 xcb_alloc_named_color(conn
, cmap
, strlen(cname
),
1731 warnx("color '%s' not found", colorname
);
1738 setscreencolor(char *val
, int i
, int c
)
1742 num_screens
= get_screen_count();
1743 if (i
> 0 && i
<= num_screens
) {
1744 screens
[i
- 1].c
[c
].pixel
= name_to_pixel(i
- 1, val
);
1745 free(screens
[i
- 1].c
[c
].name
);
1746 if ((screens
[i
- 1].c
[c
].name
= strdup(val
)) == NULL
)
1748 } else if (i
== -1) {
1749 for (i
= 0; i
< num_screens
; i
++) {
1750 screens
[i
].c
[c
].pixel
= name_to_pixel(0, val
);
1751 free(screens
[i
].c
[c
].name
);
1752 if ((screens
[i
].c
[c
].name
= strdup(val
)) == NULL
)
1756 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1761 fancy_stacker(struct workspace
*ws
)
1763 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1764 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1765 snprintf(ws
->stacker
, sizeof ws
->stacker
,
1766 ws
->l_state
.vertical_flip
? "[%d>%d]" : "[%d|%d]",
1767 ws
->l_state
.vertical_mwin
, ws
->l_state
.vertical_stacks
);
1768 if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1769 snprintf(ws
->stacker
, sizeof ws
->stacker
,
1770 ws
->l_state
.horizontal_flip
? "[%dv%d]" : "[%d-%d]",
1771 ws
->l_state
.horizontal_mwin
, ws
->l_state
.horizontal_stacks
);
1775 plain_stacker(struct workspace
*ws
)
1777 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1778 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1779 strlcpy(ws
->stacker
, ws
->l_state
.vertical_flip
? "[>]" : "[|]",
1780 sizeof ws
->stacker
);
1781 if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1782 strlcpy(ws
->stacker
, ws
->l_state
.horizontal_flip
? "[v]" : "[-]",
1783 sizeof ws
->stacker
);
1787 custom_region(char *val
)
1789 unsigned int x
, y
, w
, h
;
1790 int sidx
, num_screens
;
1791 xcb_screen_t
*screen
;
1793 num_screens
= get_screen_count();
1794 if (sscanf(val
, "screen[%d]:%ux%u+%u+%u", &sidx
, &w
, &h
, &x
, &y
) != 5)
1795 errx(1, "invalid custom region, "
1796 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1797 if (sidx
< 1 || sidx
> num_screens
)
1798 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1802 if ((screen
= get_screen(sidx
)) == NULL
)
1803 errx(1, "ERROR: can't get screen %d.", sidx
);
1806 errx(1, "region %ux%u+%u+%u too small", w
, h
, x
, y
);
1808 if (x
> screen
->width_in_pixels
||
1809 y
> screen
->height_in_pixels
||
1810 w
+ x
> screen
->width_in_pixels
||
1811 h
+ y
> screen
->height_in_pixels
) {
1812 warnx("ignoring region %ux%u+%u+%u - not within screen "
1813 "boundaries (%ux%u)", w
, h
, x
, y
,
1814 screen
->width_in_pixels
, screen
->height_in_pixels
);
1818 new_region(&screens
[sidx
], x
, y
, w
, h
);
1822 socket_setnonblock(int fd
)
1826 if ((flags
= fcntl(fd
, F_GETFL
, 0)) == -1)
1827 err(1, "fcntl F_GETFL");
1828 flags
|= O_NONBLOCK
;
1829 if ((flags
= fcntl(fd
, F_SETFL
, flags
)) == -1)
1830 err(1, "fcntl F_SETFL");
1834 bar_print_legacy(struct swm_region
*r
, const char *s
)
1836 xcb_rectangle_t rect
;
1841 XRectangle ibox
, lbox
;
1845 XmbTextExtents(bar_fs
, s
, len
, &ibox
, &lbox
);
1847 switch (bar_justify
) {
1848 case SWM_BAR_JUSTIFY_LEFT
:
1851 case SWM_BAR_JUSTIFY_CENTER
:
1852 x
= (WIDTH(r
) - lbox
.width
) / 2;
1854 case SWM_BAR_JUSTIFY_RIGHT
:
1855 x
= WIDTH(r
) - lbox
.width
- SWM_BAR_OFFSET
;
1859 if (x
< SWM_BAR_OFFSET
)
1864 rect
.width
= WIDTH(r
->bar
);
1865 rect
.height
= HEIGHT(r
->bar
);
1867 /* clear back buffer */
1868 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
1869 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
1870 xcb_poly_fill_rectangle(conn
, r
->bar
->buffer
, r
->s
->bar_gc
, 1, &rect
);
1872 /* draw back buffer */
1873 gcvd
.graphics_exposures
= 0;
1874 draw
= XCreateGC(display
, r
->bar
->buffer
, GCGraphicsExposures
, &gcvd
);
1875 XSetForeground(display
, draw
, r
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
);
1876 DRAWSTRING(display
, r
->bar
->buffer
, bar_fs
, draw
,
1877 x
, (bar_fs_extents
->max_logical_extent
.height
- lbox
.height
) / 2 -
1879 XFreeGC(display
, draw
);
1882 xcb_copy_area(conn
, r
->bar
->buffer
, r
->bar
->id
, r
->s
->bar_gc
, 0, 0,
1883 0, 0, WIDTH(r
->bar
), HEIGHT(r
->bar
));
1887 bar_print(struct swm_region
*r
, const char *s
)
1890 xcb_rectangle_t rect
;
1898 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
, &info
);
1900 switch (bar_justify
) {
1901 case SWM_BAR_JUSTIFY_LEFT
:
1904 case SWM_BAR_JUSTIFY_CENTER
:
1905 x
= (WIDTH(r
) - info
.width
) / 2;
1907 case SWM_BAR_JUSTIFY_RIGHT
:
1908 x
= WIDTH(r
) - info
.width
- SWM_BAR_OFFSET
;
1912 if (x
< SWM_BAR_OFFSET
)
1917 rect
.width
= WIDTH(r
->bar
);
1918 rect
.height
= HEIGHT(r
->bar
);
1920 /* clear back buffer */
1921 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
1922 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
1923 xcb_poly_fill_rectangle(conn
, r
->bar
->buffer
, r
->s
->bar_gc
, 1, &rect
);
1925 /* draw back buffer */
1926 draw
= XftDrawCreate(display
, r
->bar
->buffer
,
1927 DefaultVisual(display
, r
->s
->idx
),
1928 DefaultColormap(display
, r
->s
->idx
));
1930 XftDrawStringUtf8(draw
, &bar_font_color
, bar_font
, x
,
1931 (HEIGHT(r
->bar
) + bar_font
->height
) / 2 - bar_font
->descent
,
1934 XftDrawDestroy(draw
);
1937 xcb_copy_area(conn
, r
->bar
->buffer
, r
->bar
->id
, r
->s
->bar_gc
, 0, 0,
1938 0, 0, WIDTH(r
->bar
), HEIGHT(r
->bar
));
1942 bar_extra_stop(void)
1946 bzero(bar_pipe
, sizeof bar_pipe
);
1949 kill(bar_pid
, SIGTERM
);
1952 strlcpy(bar_ext
, "", sizeof bar_ext
);
1957 bar_class_name(char *s
, size_t sz
, struct swm_region
*r
)
1959 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1961 if (r
->ws
->focus
->ch
.class_name
!= NULL
)
1962 strlcat(s
, r
->ws
->focus
->ch
.class_name
, sz
);
1966 bar_title_name(char *s
, size_t sz
, struct swm_region
*r
)
1968 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1970 if (r
->ws
->focus
->ch
.instance_name
!= NULL
)
1971 strlcat(s
, r
->ws
->focus
->ch
.instance_name
, sz
);
1975 bar_class_title_name(char *s
, size_t sz
, struct swm_region
*r
)
1977 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1980 bar_class_name(s
, sz
, r
);
1981 strlcat(s
, ":", sz
);
1982 bar_title_name(s
, sz
, r
);
1986 bar_window_float(char *s
, size_t sz
, struct swm_region
*r
)
1988 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1990 if (r
->ws
->focus
->floating
)
1991 strlcat(s
, "(f)", sz
);
1995 bar_window_name(char *s
, size_t sz
, struct swm_region
*r
)
1999 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2001 if ((title
= get_win_name(r
->ws
->focus
->id
)) == NULL
)
2004 strlcat(s
, title
, sz
);
2009 bar_urgent(char *s
, size_t sz
)
2012 int i
, j
, num_screens
;
2013 int urgent
[SWM_WS_MAX
];
2015 xcb_get_property_cookie_t c
;
2016 xcb_icccm_wm_hints_t hints
;
2018 for (i
= 0; i
< workspace_limit
; i
++)
2021 num_screens
= get_screen_count();
2022 for (i
= 0; i
< num_screens
; i
++)
2023 for (j
= 0; j
< workspace_limit
; j
++)
2024 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
) {
2025 c
= xcb_icccm_get_wm_hints(conn
, win
->id
);
2026 if (xcb_icccm_get_wm_hints_reply(conn
, c
,
2029 if (hints
.flags
& XCB_ICCCM_WM_HINT_X_URGENCY
)
2033 for (i
= 0; i
< workspace_limit
; i
++) {
2035 snprintf(b
, sizeof b
, "%d ", i
+ 1);
2037 snprintf(b
, sizeof b
, "- ");
2043 bar_workspace_name(char *s
, size_t sz
, struct swm_region
*r
)
2045 if (r
== NULL
|| r
->ws
== NULL
)
2047 if (r
->ws
->name
!= NULL
)
2048 strlcat(s
, r
->ws
->name
, sz
);
2051 /* build the default bar format according to the defined enabled options */
2053 bar_fmt(const char *fmtexp
, char *fmtnew
, struct swm_region
*r
, size_t sz
)
2055 /* if format provided, just copy the buffers */
2056 if (bar_format
!= NULL
) {
2057 strlcpy(fmtnew
, fmtexp
, sz
);
2061 /* reset the output buffer */
2064 strlcat(fmtnew
, "+N:+I ", sz
);
2066 strlcat(fmtnew
, "+S", sz
);
2067 strlcat(fmtnew
, " ", sz
);
2069 /* only show the workspace name if there's actually one */
2070 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->name
!= NULL
)
2071 strlcat(fmtnew
, "<+D>", sz
);
2072 strlcat(fmtnew
, "+3<", sz
);
2074 if (clock_enabled
) {
2075 strlcat(fmtnew
, fmtexp
, sz
);
2076 strlcat(fmtnew
, "+4<", sz
);
2079 /* bar_urgent already adds the space before the last asterisk */
2081 strlcat(fmtnew
, "* +U*+4<", sz
);
2083 if (title_class_enabled
) {
2084 strlcat(fmtnew
, "+C", sz
);
2085 if (!title_name_enabled
)
2086 strlcat(fmtnew
, "+4<", sz
);
2089 /* checks needed by the colon and floating strlcat(3) calls below */
2090 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->focus
!= NULL
) {
2091 if (title_name_enabled
) {
2092 if (title_class_enabled
)
2093 strlcat(fmtnew
, ":", sz
);
2094 strlcat(fmtnew
, "+T+4<", sz
);
2096 if (window_name_enabled
) {
2097 if (r
->ws
->focus
->floating
)
2098 strlcat(fmtnew
, "+F ", sz
);
2099 strlcat(fmtnew
, "+64W ", sz
);
2103 /* finally add the action script output and the version */
2104 strlcat(fmtnew
, "+4<+A+4<+V", sz
);
2108 bar_replace_pad(char *tmp
, int *limit
, size_t sz
)
2110 /* special case; no limit given, pad one space, instead */
2111 if (*limit
== (int)sz
- 1)
2113 snprintf(tmp
, sz
, "%*s", *limit
, " ");
2116 /* replaces the bar format character sequences (like in tmux(1)) */
2118 bar_replace_seq(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t *offrep
,
2122 char tmp
[SWM_BAR_MAX
];
2126 /* reset strlcat(3) buffer */
2129 /* get number, if any */
2132 if (sscanf(fmt
, "%d%n", &limit
, &size
) != 1)
2133 limit
= sizeof tmp
- 1;
2134 if (limit
<= 0 || limit
>= (int)sizeof tmp
)
2135 limit
= sizeof tmp
- 1;
2137 /* there is nothing to replace (ie EOL) */
2144 bar_replace_pad(tmp
, &limit
, sizeof tmp
);
2147 snprintf(tmp
, sizeof tmp
, "%s", bar_ext
);
2150 bar_class_name(tmp
, sizeof tmp
, r
);
2153 bar_workspace_name(tmp
, sizeof tmp
, r
);
2156 bar_window_float(tmp
, sizeof tmp
, r
);
2159 snprintf(tmp
, sizeof tmp
, "%d", r
->ws
->idx
+ 1);
2162 snprintf(tmp
, sizeof tmp
, "%d", r
->s
->idx
+ 1);
2165 bar_class_title_name(tmp
, sizeof tmp
, r
);
2168 snprintf(tmp
, sizeof tmp
, "%s", r
->ws
->stacker
);
2171 bar_title_name(tmp
, sizeof tmp
, r
);
2174 bar_urgent(tmp
, sizeof tmp
);
2177 snprintf(tmp
, sizeof tmp
, "%s", bar_vertext
);
2180 bar_window_name(tmp
, sizeof tmp
, r
);
2183 /* unknown character sequence; copy as-is */
2184 snprintf(tmp
, sizeof tmp
, "+%c", *fmt
);
2190 if ((int)len
< limit
)
2192 while (limit
-- > 0) {
2193 if (*offrep
>= sz
- 1)
2195 fmtrep
[(*offrep
)++] = *ptr
++;
2203 bar_replace(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t sz
)
2208 while (*fmt
!= '\0') {
2210 /* skip ordinary characters */
2213 fmtrep
[off
++] = *fmt
++;
2217 /* character sequence found; replace it */
2218 fmt
= bar_replace_seq(fmt
, fmtrep
, r
, &off
, sz
);
2227 bar_fmt_expand(char *fmtexp
, size_t sz
)
2234 /* start by grabbing the current time and date */
2236 localtime_r(&tmt
, &tm
);
2238 /* figure out what to expand */
2239 if (bar_format
!= NULL
)
2241 else if (bar_format
== NULL
&& clock_enabled
)
2243 /* if nothing to expand bail out */
2249 /* copy as-is, just in case the format shouldn't be expanded below */
2250 strlcpy(fmtexp
, fmt
, sz
);
2251 /* finally pass the string through strftime(3) */
2252 #ifndef SWM_DENY_CLOCK_FORMAT
2253 if ((len
= strftime(fmtexp
, sz
, fmt
, &tm
)) == 0)
2254 warnx("format too long");
2259 /* Redraws the bar; need to follow with xcb_flush() or focus_flush(). */
2263 char fmtexp
[SWM_BAR_MAX
], fmtnew
[SWM_BAR_MAX
];
2264 char fmtrep
[SWM_BAR_MAX
];
2266 struct swm_region
*r
;
2268 /* expand the format by first passing it through strftime(3) */
2269 bar_fmt_expand(fmtexp
, sizeof fmtexp
);
2271 num_screens
= get_screen_count();
2272 for (i
= 0; i
< num_screens
; i
++) {
2273 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
2277 if (bar_enabled
&& r
->ws
->bar_enabled
)
2278 xcb_map_window(conn
, r
->bar
->id
);
2280 xcb_unmap_window(conn
, r
->bar
->id
);
2284 if (startup_exception
)
2285 snprintf(fmtrep
, sizeof fmtrep
, "total "
2286 "exceptions: %d, first exception: %s",
2290 bar_fmt(fmtexp
, fmtnew
, r
, sizeof fmtnew
);
2291 bar_replace(fmtnew
, fmtrep
, r
, sizeof fmtrep
);
2293 if (bar_font_legacy
)
2294 bar_print_legacy(r
, fmtrep
);
2296 bar_print(r
, fmtrep
);
2302 * Reads external script output; call when stdin is readable.
2303 * Returns 1 if bar_ext was updated; otherwise 0.
2306 bar_extra_update(void)
2309 char b
[SWM_BAR_MAX
];
2315 while (fgets(b
, sizeof(b
), stdin
) != NULL
) {
2318 if (b
[len
- 1] == '\n') {
2319 /* Remove newline. */
2322 /* "Clear" bar_ext. */
2325 /* Flush buffered output. */
2326 strlcpy(bar_ext
, bar_ext_buf
, sizeof(bar_ext
));
2327 bar_ext_buf
[0] = '\0';
2329 /* Append new output to bar. */
2330 strlcat(bar_ext
, b
, sizeof(bar_ext
));
2334 /* Buffer output. */
2335 strlcat(bar_ext_buf
, b
, sizeof(bar_ext_buf
));
2340 if (errno
!= EAGAIN
) {
2341 warn("bar_action failed");
2350 bar_toggle(struct swm_region
*r
, union arg
*args
)
2352 struct swm_region
*tmpr
;
2355 /* suppress unused warnings since vars are needed */
2359 DNPRINTF(SWM_D_BAR
, "bar_toggle\n");
2362 case SWM_ARG_ID_BAR_TOGGLE_WS
:
2363 /* Only change if master switch is enabled. */
2365 r
->ws
->bar_enabled
= !r
->ws
->bar_enabled
;
2367 bar_enabled
= r
->ws
->bar_enabled
= 1;
2369 case SWM_ARG_ID_BAR_TOGGLE
:
2370 bar_enabled
= !bar_enabled
;
2374 /* update bars as necessary */
2375 num_screens
= get_screen_count();
2376 for (i
= 0; i
< num_screens
; i
++)
2377 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
2379 if (bar_enabled
&& tmpr
->ws
->bar_enabled
)
2380 xcb_map_window(conn
, tmpr
->bar
->id
);
2382 xcb_unmap_window(conn
, tmpr
->bar
->id
);
2387 /* must be after stack */
2394 bar_extra_setup(void)
2396 /* do this here because the conf file is in memory */
2397 if (!bar_extra
&& bar_argv
[0]) {
2398 /* launch external status app */
2400 if (pipe(bar_pipe
) == -1)
2401 err(1, "pipe error");
2402 socket_setnonblock(bar_pipe
[0]);
2403 socket_setnonblock(bar_pipe
[1]); /* XXX hmmm, really? */
2405 /* Set stdin to read from the pipe. */
2406 if (dup2(bar_pipe
[0], STDIN_FILENO
) == -1)
2409 /* Set stdout to write to the pipe. */
2410 if (dup2(bar_pipe
[1], STDOUT_FILENO
) == -1)
2413 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
2414 err(1, "could not disable SIGPIPE");
2415 switch (bar_pid
= fork()) {
2417 err(1, "cannot fork");
2421 execvp(bar_argv
[0], bar_argv
);
2422 err(1, "%s external app failed", bar_argv
[0]);
2424 default: /* parent */
2429 atexit(kill_bar_extra_atexit
);
2434 kill_bar_extra_atexit(void)
2437 kill(bar_pid
, SIGTERM
);
2445 while ((s
= index(s
, '-'))) {
2450 return (count
== 14);
2456 char *default_string
;
2457 char **missing_charsets
;
2458 int num_missing_charsets
= 0;
2462 XFreeFontSet(display
, bar_fs
);
2466 DNPRINTF(SWM_D_INIT
, "fontset_init: loading bar_fonts: %s\n", bar_fonts
);
2468 bar_fs
= XCreateFontSet(display
, bar_fonts
, &missing_charsets
,
2469 &num_missing_charsets
, &default_string
);
2471 if (num_missing_charsets
> 0) {
2472 warnx("Unable to load charset(s):");
2474 for (i
= 0; i
< num_missing_charsets
; ++i
)
2475 warnx("%s", missing_charsets
[i
]);
2477 XFreeStringList(missing_charsets
);
2479 if (strcmp(default_string
, ""))
2480 warnx("Glyphs from those sets will be replaced "
2481 "by '%s'.", default_string
);
2483 warnx("Glyphs from those sets won't be drawn.");
2487 errx(1, "Error creating font set structure.");
2489 bar_fs_extents
= XExtentsOfFontSet(bar_fs
);
2491 bar_height
= bar_fs_extents
->max_logical_extent
.height
+
2492 2 * bar_border_width
;
2499 xft_init(struct swm_region
*r
)
2501 char *font
, *d
, *search
;
2504 if (bar_font
== NULL
) {
2505 if ((d
= strdup(bar_fonts
)) == NULL
)
2506 errx(1, "insufficient memory.");
2508 while ((font
= strsep(&search
, ",")) != NULL
) {
2512 DNPRINTF(SWM_D_INIT
, "xft_init: try font %s\n", font
);
2515 bar_font
= XftFontOpenXlfd(display
, r
->s
->idx
,
2518 bar_font
= XftFontOpenName(display
, r
->s
->idx
,
2523 warnx("unable to load font %s", font
);
2526 DNPRINTF(SWM_D_INIT
, "successfully opened "
2534 if (bar_font
== NULL
)
2535 errx(1, "unable to open a font");
2537 PIXEL_TO_XRENDERCOLOR(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
, color
);
2539 if (!XftColorAllocValue(display
, DefaultVisual(display
, r
->s
->idx
),
2540 DefaultColormap(display
, r
->s
->idx
), &color
, &bar_font_color
))
2541 warn("Xft error: unable to allocate color.");
2543 bar_height
= bar_font
->height
+ 2 * bar_border_width
;
2550 bar_setup(struct swm_region
*r
)
2552 xcb_screen_t
*screen
;
2555 DNPRINTF(SWM_D_BAR
, "bar_setup: screen %d.\n",
2558 if ((screen
= get_screen(r
->s
->idx
)) == NULL
)
2559 errx(1, "ERROR: can't get screen %d.", r
->s
->idx
);
2564 if ((r
->bar
= calloc(1, sizeof(struct swm_bar
))) == NULL
)
2565 err(1, "bar_setup: calloc: failed to allocate memory.");
2567 if (bar_font_legacy
)
2573 Y(r
->bar
) = bar_at_bottom
? (Y(r
) + HEIGHT(r
) - bar_height
) : Y(r
);
2574 WIDTH(r
->bar
) = WIDTH(r
) - 2 * bar_border_width
;
2575 HEIGHT(r
->bar
) = bar_height
- 2 * bar_border_width
;
2577 /* Assume region is unfocused when we create the bar. */
2578 r
->bar
->id
= xcb_generate_id(conn
);
2579 wa
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
2580 wa
[1] = r
->s
->c
[SWM_S_COLOR_BAR_BORDER_UNFOCUS
].pixel
;
2581 wa
[2] = XCB_EVENT_MASK_EXPOSURE
| XCB_EVENT_MASK_POINTER_MOTION
|
2582 XCB_EVENT_MASK_POINTER_MOTION_HINT
;
2584 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, r
->bar
->id
, r
->s
->root
,
2585 X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
), HEIGHT(r
->bar
),
2586 bar_border_width
, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
2587 XCB_COPY_FROM_PARENT
, XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
2588 | XCB_CW_EVENT_MASK
, wa
);
2590 r
->bar
->buffer
= xcb_generate_id(conn
);
2591 xcb_create_pixmap(conn
, screen
->root_depth
, r
->bar
->buffer
, r
->bar
->id
,
2592 WIDTH(r
->bar
), HEIGHT(r
->bar
));
2595 xcb_randr_select_input(conn
, r
->bar
->id
,
2596 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE
);
2599 xcb_map_window(conn
, r
->bar
->id
);
2601 DNPRINTF(SWM_D_BAR
, "bar_setup: window: 0x%x, (x,y) w x h: (%d,%d) "
2602 "%d x %d\n", WINID(r
->bar
), X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
),
2609 bar_cleanup(struct swm_region
*r
)
2613 xcb_destroy_window(conn
, r
->bar
->id
);
2614 xcb_free_pixmap(conn
, r
->bar
->buffer
);
2620 set_win_state(struct ws_win
*win
, uint16_t state
)
2622 uint16_t data
[2] = { state
, XCB_ATOM_NONE
};
2624 DNPRINTF(SWM_D_EVENT
, "set_win_state: window: 0x%x, state: %u\n",
2630 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
, a_state
,
2631 a_state
, 32, 2, data
);
2635 getstate(xcb_window_t w
)
2637 uint32_t result
= 0;
2638 xcb_get_property_cookie_t c
;
2639 xcb_get_property_reply_t
*r
;
2641 c
= xcb_get_property(conn
, 0, w
, a_state
, a_state
, 0L, 2L);
2642 r
= xcb_get_property_reply(conn
, c
, NULL
);
2645 if (r
->type
== a_state
&& r
->format
== 32 && r
->length
== 2)
2646 result
= *((uint32_t *)xcb_get_property_value(r
));
2650 DNPRINTF(SWM_D_MISC
, "getstate property: win 0x%x state %u\n", w
,
2656 version(struct swm_region
*r
, union arg
*args
)
2658 /* suppress unused warnings since vars are needed */
2662 bar_version
= !bar_version
;
2664 snprintf(bar_vertext
, sizeof bar_vertext
,
2665 "Version: %s Build: %s", SPECTRWM_VERSION
, buildstr
);
2667 strlcpy(bar_vertext
, "", sizeof bar_vertext
);
2674 client_msg(struct ws_win
*win
, xcb_atom_t a
, xcb_timestamp_t t
)
2676 xcb_client_message_event_t ev
;
2684 name
= get_atom_name(a
);
2685 DNPRINTF(SWM_D_EVENT
, "client_msg: window: 0x%x, atom: %s(%u), "
2687 win
->id
, name
, a
, t
);
2691 bzero(&ev
, sizeof ev
);
2692 ev
.response_type
= XCB_CLIENT_MESSAGE
;
2693 ev
.window
= win
->id
;
2696 ev
.data
.data32
[0] = a
;
2697 ev
.data
.data32
[1] = t
;
2699 xcb_send_event(conn
, 0, win
->id
,
2700 XCB_EVENT_MASK_NO_EVENT
, (const char *)&ev
);
2703 /* synthetic response to a ConfigureRequest when not making a change */
2705 config_win(struct ws_win
*win
, xcb_configure_request_event_t
*ev
)
2707 xcb_configure_notify_event_t ce
;
2712 /* send notification of unchanged state. */
2713 bzero(&ce
, sizeof(ce
));
2714 ce
.response_type
= XCB_CONFIGURE_NOTIFY
;
2717 ce
.width
= WIDTH(win
);
2718 ce
.height
= HEIGHT(win
);
2719 ce
.override_redirect
= 0;
2724 ce
.window
= win
->id
;
2725 ce
.border_width
= BORDER(win
);
2726 ce
.above_sibling
= XCB_WINDOW_NONE
;
2729 ce
.event
= ev
->window
;
2730 ce
.window
= ev
->window
;
2732 /* make response appear more WM_SIZE_HINTS-compliant */
2733 if (win
->sh
.flags
) {
2734 DNPRINTF(SWM_D_MISC
, "config_win: hints: window: 0x%x,"
2735 " sh.flags: %u, min: %d x %d, max: %d x %d, inc: "
2736 "%d x %d\n", win
->id
, win
->sh
.flags
, SH_MIN_W(win
),
2737 SH_MIN_H(win
), SH_MAX_W(win
), SH_MAX_H(win
),
2738 SH_INC_W(win
), SH_INC_H(win
));
2743 /* the hint may be set... to 0! */
2744 if (SH_MIN_W(win
) > 0 && ce
.width
< SH_MIN_W(win
))
2745 ce
.width
= SH_MIN_W(win
);
2746 if (SH_MIN_H(win
) > 0 && ce
.height
< SH_MIN_H(win
))
2747 ce
.height
= SH_MIN_H(win
);
2752 /* may also be advertized as 0 */
2753 if (SH_MAX_W(win
) > 0 && ce
.width
> SH_MAX_W(win
))
2754 ce
.width
= SH_MAX_W(win
);
2755 if (SH_MAX_H(win
) > 0 && ce
.height
> SH_MAX_H(win
))
2756 ce
.height
= SH_MAX_H(win
);
2759 /* resize increment. */
2761 if (SH_INC_W(win
) > 1 && ce
.width
> SH_INC_W(win
))
2762 ce
.width
-= (ce
.width
- SH_MIN_W(win
)) %
2764 if (SH_INC_H(win
) > 1 && ce
.height
> SH_INC_H(win
))
2765 ce
.height
-= (ce
.height
- SH_MIN_H(win
)) %
2769 /* adjust x and y for requested border_width. */
2770 ce
.x
+= BORDER(win
) - ev
->border_width
;
2771 ce
.y
+= BORDER(win
) - ev
->border_width
;
2772 ce
.border_width
= ev
->border_width
;
2773 ce
.above_sibling
= ev
->sibling
;
2776 DNPRINTF(SWM_D_MISC
, "config_win: ewmh: %s, window: 0x%x, (x,y) w x h: "
2777 "(%d,%d) %d x %d, border: %d\n", YESNO(ev
== NULL
), win
->id
, ce
.x
,
2778 ce
.y
, ce
.width
, ce
.height
, ce
.border_width
);
2780 xcb_send_event(conn
, 0, win
->id
, XCB_EVENT_MASK_STRUCTURE_NOTIFY
,
2785 count_win(struct workspace
*ws
, int count_transient
)
2790 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
2791 if (!count_transient
&& win
->floating
)
2793 if (!count_transient
&& win
->transient
)
2799 DNPRINTF(SWM_D_MISC
, "count_win: %d\n", count
);
2805 quit(struct swm_region
*r
, union arg
*args
)
2807 /* suppress unused warnings since vars are needed */
2811 DNPRINTF(SWM_D_MISC
, "quit\n");
2816 map_window(struct ws_win
*win
, xcb_window_t sibling
)
2818 uint16_t mode
= XCB_CONFIG_WINDOW_STACK_MODE
;
2822 /* If sibling is specified, stack right above it. */
2823 if (sibling
!= XCB_WINDOW_NONE
) {
2824 mode
|= XCB_CONFIG_WINDOW_SIBLING
;
2828 val
[i
] = XCB_STACK_MODE_ABOVE
;
2833 DNPRINTF(SWM_D_EVENT
, "map_window: win 0x%x, mapped: %s, "
2834 "sibling: 0x%x\n", win
->id
, YESNO(win
->mapped
), sibling
);
2836 xcb_configure_window(conn
, win
->id
, mode
, val
);
2841 xcb_map_window(conn
, win
->id
);
2842 set_win_state(win
, XCB_ICCCM_WM_STATE_NORMAL
);
2847 unmap_window(struct ws_win
*win
)
2852 DNPRINTF(SWM_D_EVENT
, "unmap_window: win 0x%x, mapped: %s\n", win
->id
,
2853 YESNO(win
->mapped
));
2858 xcb_unmap_window(conn
, win
->id
);
2859 set_win_state(win
, XCB_ICCCM_WM_STATE_ICONIC
);
2867 int i
, j
, num_screens
;
2869 num_screens
= get_screen_count();
2870 for (i
= 0; i
< num_screens
; i
++)
2871 for (j
= 0; j
< workspace_limit
; j
++)
2872 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2877 fake_keypress(struct ws_win
*win
, xcb_keysym_t keysym
, uint16_t modifiers
)
2879 xcb_key_press_event_t event
;
2880 xcb_keycode_t
*keycode
;
2885 keycode
= xcb_key_symbols_get_keycode(syms
, keysym
);
2887 DNPRINTF(SWM_D_MISC
, "fake_keypress: win 0x%x keycode %u\n",
2890 bzero(&event
, sizeof(event
));
2891 event
.event
= win
->id
;
2892 event
.root
= win
->s
->root
;
2893 event
.child
= XCB_WINDOW_NONE
;
2894 event
.time
= XCB_CURRENT_TIME
;
2895 event
.event_x
= X(win
);
2896 event
.event_y
= Y(win
);
2899 event
.same_screen
= 1;
2900 event
.detail
= *keycode
;
2901 event
.state
= modifiers
;
2903 event
.response_type
= XCB_KEY_PRESS
;
2904 xcb_send_event(conn
, 1, win
->id
,
2905 XCB_EVENT_MASK_KEY_PRESS
, (const char *)&event
);
2907 event
.response_type
= XCB_KEY_RELEASE
;
2908 xcb_send_event(conn
, 1, win
->id
,
2909 XCB_EVENT_MASK_KEY_RELEASE
, (const char *)&event
);
2915 restart(struct swm_region
*r
, union arg
*args
)
2917 /* suppress unused warning since var is needed */
2921 DNPRINTF(SWM_D_MISC
, "restart: %s\n", start_argv
[0]);
2925 execvp(start_argv
[0], start_argv
);
2926 warn("execvp failed");
2931 get_pointer_win(xcb_window_t root
)
2933 struct ws_win
*win
= NULL
;
2934 xcb_query_pointer_reply_t
*r
;
2936 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: root: 0x%x.\n", root
);
2938 r
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, root
), NULL
);
2940 win
= find_window(r
->child
);
2942 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: 0x%x.\n",
2945 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: none.\n");
2954 root_to_region(xcb_window_t root
, int check
)
2957 struct swm_region
*r
= NULL
;
2959 xcb_query_pointer_reply_t
*qpr
;
2960 xcb_get_input_focus_reply_t
*gifr
;
2962 DNPRINTF(SWM_D_MISC
, "root_to_region: window: 0x%x\n", root
);
2964 num_screens
= get_screen_count();
2965 for (i
= 0; i
< num_screens
; i
++)
2966 if (screens
[i
].root
== root
)
2969 if (check
& SWM_CK_REGION
)
2970 r
= screens
[i
].r_focus
;
2972 if (r
== NULL
&& check
& SWM_CK_FOCUS
) {
2973 /* Try to find an actively focused window */
2974 gifr
= xcb_get_input_focus_reply(conn
,
2975 xcb_get_input_focus(conn
), NULL
);
2977 cfw
= find_window(gifr
->focus
);
2978 if (cfw
&& cfw
->ws
->r
)
2985 if (r
== NULL
&& check
& SWM_CK_POINTER
) {
2986 /* No region with an active focus; try to use pointer. */
2987 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
,
2988 screens
[i
].root
), NULL
);
2991 DNPRINTF(SWM_D_MISC
, "root_to_region: pointer: "
2992 "(%d,%d)\n", qpr
->root_x
, qpr
->root_y
);
2993 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2994 if (X(r
) <= qpr
->root_x
&&
2995 qpr
->root_x
< MAX_X(r
) &&
2996 Y(r
) <= qpr
->root_y
&&
2997 qpr
->root_y
< MAX_Y(r
))
3004 if (r
== NULL
&& check
& SWM_CK_FALLBACK
)
3005 r
= TAILQ_FIRST(&screens
[i
].rl
);
3007 DNPRINTF(SWM_D_MISC
, "root_to_region: idx: %d\n", get_region_index(r
));
3013 find_unmanaged_window(xcb_window_t id
)
3016 int i
, j
, num_screens
;
3018 num_screens
= get_screen_count();
3019 for (i
= 0; i
< num_screens
; i
++)
3020 for (j
= 0; j
< workspace_limit
; j
++)
3021 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].unmanagedlist
,
3029 find_window(xcb_window_t id
)
3032 int i
, j
, num_screens
;
3033 xcb_query_tree_reply_t
*r
;
3035 num_screens
= get_screen_count();
3036 for (i
= 0; i
< num_screens
; i
++)
3037 for (j
= 0; j
< workspace_limit
; j
++)
3038 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
3042 r
= xcb_query_tree_reply(conn
, xcb_query_tree(conn
, id
), NULL
);
3046 /* if we were looking for the parent return that window instead */
3047 if (r
->parent
== 0 || r
->root
== r
->parent
) {
3052 /* look for parent */
3053 for (i
= 0; i
< num_screens
; i
++)
3054 for (j
= 0; j
< workspace_limit
; j
++)
3055 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
3056 if (r
->parent
== win
->id
) {
3066 spawn(int ws_idx
, union arg
*args
, int close_fd
)
3071 DNPRINTF(SWM_D_MISC
, "spawn: %s\n", args
->argv
[0]);
3073 close(xcb_get_file_descriptor(conn
));
3075 setenv("LD_PRELOAD", SWM_LIB
, 1);
3077 if (asprintf(&ret
, "%d", ws_idx
) == -1) {
3078 warn("spawn: asprintf SWM_WS");
3081 setenv("_SWM_WS", ret
, 1);
3085 if (asprintf(&ret
, "%d", getpid()) == -1) {
3086 warn("spawn: asprintf _SWM_PID");
3089 setenv("_SWM_PID", ret
, 1);
3093 if (setsid() == -1) {
3094 warn("spawn: setsid");
3100 * close stdin and stdout to prevent interaction between apps
3101 * and the baraction script
3102 * leave stderr open to record errors
3104 if ((fd
= open(_PATH_DEVNULL
, O_RDWR
, 0)) == -1) {
3105 warn("spawn: open");
3108 dup2(fd
, STDIN_FILENO
);
3109 dup2(fd
, STDOUT_FILENO
);
3114 execvp(args
->argv
[0], args
->argv
);
3116 warn("spawn: execvp");
3121 kill_refs(struct ws_win
*win
)
3123 int i
, x
, num_screens
;
3124 struct swm_region
*r
;
3125 struct workspace
*ws
;
3130 num_screens
= get_screen_count();
3131 for (i
= 0; i
< num_screens
; i
++)
3132 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3133 for (x
= 0; x
< workspace_limit
; x
++) {
3135 if (win
== ws
->focus
)
3137 if (win
== ws
->focus_prev
)
3138 ws
->focus_prev
= NULL
;
3143 validate_win(struct ws_win
*testwin
)
3146 struct workspace
*ws
;
3147 struct swm_region
*r
;
3148 int i
, x
, num_screens
;
3150 if (testwin
== NULL
)
3153 num_screens
= get_screen_count();
3154 for (i
= 0; i
< num_screens
; i
++)
3155 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3156 for (x
= 0; x
< workspace_limit
; x
++) {
3158 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
3166 validate_ws(struct workspace
*testws
)
3168 struct swm_region
*r
;
3169 struct workspace
*ws
;
3170 int i
, x
, num_screens
;
3172 /* validate all ws */
3173 num_screens
= get_screen_count();
3174 for (i
= 0; i
< num_screens
; i
++)
3175 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3176 for (x
= 0; x
< workspace_limit
; x
++) {
3185 get_sibling(struct ws_win
*win
, int mode
)
3187 struct ws_win
*w
= win
;
3191 TAILQ_FOREACH_REVERSE(w
, &w
->ws
->winlist
, ws_win_list
, entry
)
3192 if (w
!= win
&& !w
->floating
&& !w
->iconic
)
3195 case SWM_STACK_ABOVE
:
3197 w
= TAILQ_NEXT(w
, entry
);
3198 } while (w
!= NULL
&& (w
== win
|| w
->floating
|| w
->iconic
));
3200 case SWM_STACK_BELOW
:
3202 w
= TAILQ_PREV(w
, ws_win_list
, entry
);
3203 } while (w
!= NULL
&& (w
== win
|| w
->floating
|| w
->iconic
));
3205 case SWM_STACK_BOTTOM
:
3206 TAILQ_FOREACH(w
, &w
->ws
->winlist
, entry
)
3207 if (w
!= win
&& !w
->floating
&& !w
->iconic
)
3215 return (win
->ws
->r
->id
);
3221 unfocus_win(struct ws_win
*win
)
3223 xcb_window_t none
= XCB_WINDOW_NONE
;
3225 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: window: 0x%x\n", WINID(win
));
3229 if (win
->ws
== NULL
) {
3230 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: NULL ws.\n");
3234 if (validate_ws(win
->ws
)) {
3235 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: invalid ws.\n");
3239 if (win
->ws
->r
== NULL
) {
3240 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: NULL region.\n");
3244 if (validate_win(win
)) {
3245 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: invalid win.\n");
3250 if (win
->ws
->focus
== win
) {
3251 if (tile_gap
< 0 && !win
->floating
)
3252 map_window(win
, get_sibling(win
, SWM_STACK_BELOW
));
3253 win
->ws
->focus
= NULL
;
3254 win
->ws
->focus_prev
= win
;
3257 if (validate_win(win
->ws
->focus
)) {
3258 kill_refs(win
->ws
->focus
);
3259 win
->ws
->focus
= NULL
;
3262 if (validate_win(win
->ws
->focus_prev
)) {
3263 kill_refs(win
->ws
->focus_prev
);
3264 win
->ws
->focus_prev
= NULL
;
3267 xcb_change_window_attributes(conn
, win
->id
, XCB_CW_BORDER_PIXEL
,
3268 &win
->ws
->r
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
);
3270 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
3271 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1, &none
);
3273 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: done.\n");
3277 focus_win(struct ws_win
*win
)
3279 struct ws_win
*cfw
= NULL
, *parent
= NULL
, *w
;
3280 struct workspace
*ws
;
3281 xcb_get_input_focus_reply_t
*r
;
3283 DNPRINTF(SWM_D_FOCUS
, "focus_win: window: 0x%x\n", WINID(win
));
3288 if (win
->ws
== NULL
)
3296 if (validate_ws(ws
))
3299 if (validate_win(win
)) {
3304 r
= xcb_get_input_focus_reply(conn
, xcb_get_input_focus(conn
), NULL
);
3306 cfw
= find_window(r
->focus
);
3312 if (ws
->focus
!= win
) {
3313 if (ws
->focus
&& ws
->focus
!= cfw
)
3314 unfocus_win(ws
->focus
);
3318 /* If this window directs focus to a child window, then clear. */
3319 if (win
->focus_child
)
3320 win
->focus_child
= NULL
;
3322 /* If transient, adjust parent's focus child for focus_magic. */
3323 if (win
->transient
) {
3324 parent
= find_window(win
->transient
);
3325 if (parent
&& parent
->focus_child
!= win
)
3326 parent
->focus_child
= win
;
3329 if (cfw
!= win
&& ws
->r
!= NULL
) {
3330 /* Set input focus if no input hint, or indicated by hint. */
3331 if (!(win
->hints
.flags
& XCB_ICCCM_WM_HINT_INPUT
) ||
3332 (win
->hints
.flags
& XCB_ICCCM_WM_HINT_INPUT
&&
3334 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
,
3335 win
->id
, last_event_time
);
3337 /* Tell app it can adjust focus to a specific window. */
3338 if (win
->take_focus
) {
3339 /* java is special; always tell parent */
3340 if (win
->transient
&& win
->java
)
3341 client_msg(parent
, a_takefocus
,
3344 client_msg(win
, a_takefocus
, last_event_time
);
3347 xcb_change_window_attributes(conn
, win
->id
, XCB_CW_BORDER_PIXEL
,
3348 &ws
->r
->s
->c
[SWM_S_COLOR_FOCUS
].pixel
);
3350 if (ws
->cur_layout
->flags
& SWM_L_MAPONFOCUS
||
3352 /* If a parent exists, map it first. */
3354 map_window(parent
, XCB_WINDOW_NONE
);
3356 /* Map siblings next. */
3357 TAILQ_FOREACH(w
, &ws
->winlist
, entry
)
3358 if (w
!= win
&& !w
->iconic
&&
3359 w
->transient
== parent
->id
)
3360 map_window(w
, XCB_WINDOW_NONE
);
3363 /* Map focused window. */
3364 map_window(win
, XCB_WINDOW_NONE
);
3366 /* Finally, map children of focus window. */
3367 TAILQ_FOREACH(w
, &ws
->winlist
, entry
)
3368 if (w
->transient
== win
->id
&& !w
->iconic
)
3369 map_window(w
, XCB_WINDOW_NONE
);
3370 } else if (tile_gap
< 0 && !win
->floating
) {
3372 * Windows overlap in the layout.
3373 * Raise focused win above all tiled wins.
3375 if (tile_gap
< 0 && !win
->floating
)
3377 get_sibling(win
, SWM_STACK_TOP
));
3382 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
3383 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1,
3390 DNPRINTF(SWM_D_FOCUS
, "focus_win: done.\n");
3393 /* If a child window should have focus instead, return it. */
3395 get_focus_magic(struct ws_win
*win
)
3397 struct ws_win
*parent
= NULL
;
3398 struct ws_win
*child
= NULL
;
3400 DNPRINTF(SWM_D_FOCUS
, "get_focus_magic: window: 0x%x\n", WINID(win
));
3404 if (win
->transient
) {
3405 parent
= find_window(win
->transient
);
3407 /* If parent prefers focus elsewhere, then try to do so. */
3408 if (parent
&& (child
= parent
->focus_child
)) {
3409 if (validate_win(child
) == 0 && child
->mapped
)
3412 parent
->focus_child
= NULL
;
3416 /* If this window prefers focus elsewhere, then try to do so. */
3417 if ((child
= win
->focus_child
)) {
3418 if (validate_win(child
) == 0 && child
->mapped
)
3421 win
->focus_child
= NULL
;
3428 event_drain(uint8_t rt
)
3430 xcb_generic_event_t
*evt
;
3432 /* ensure all pending requests have been processed before filtering. */
3434 while ((evt
= xcb_poll_for_event(conn
))) {
3435 if (XCB_EVENT_RESPONSE_TYPE(evt
) != rt
)
3443 set_region(struct swm_region
*r
)
3445 struct swm_region
*rf
;
3451 /* Unfocus old region bar. */
3456 xcb_change_window_attributes(conn
, rf
->bar
->id
,
3457 XCB_CW_BORDER_PIXEL
,
3458 &r
->s
->c
[SWM_S_COLOR_BAR_BORDER_UNFOCUS
].pixel
);
3461 /* Set region bar border to focus_color. */
3462 xcb_change_window_attributes(conn
, r
->bar
->id
,
3463 XCB_CW_BORDER_PIXEL
, &r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].pixel
);
3469 focus_region(struct swm_region
*r
)
3472 struct swm_region
*old_r
;
3477 old_r
= r
->s
->r_focus
;
3480 nfw
= get_region_focus(r
);
3484 /* New region is empty; need to manually unfocus win. */
3486 unfocus_win(old_r
->ws
->focus
);
3488 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
, r
->id
,
3491 /* Clear bar since empty. */
3497 switchws(struct swm_region
*r
, union arg
*args
)
3499 int wsid
= args
->id
, unmap_old
= 0;
3500 struct swm_region
*this_r
, *other_r
;
3502 struct workspace
*new_ws
, *old_ws
;
3507 if (wsid
>= workspace_limit
)
3511 old_ws
= this_r
->ws
;
3512 new_ws
= &this_r
->s
->ws
[wsid
];
3514 DNPRINTF(SWM_D_WS
, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
3515 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), old_ws
->idx
, wsid
);
3517 if (new_ws
== NULL
|| old_ws
== NULL
)
3519 if (new_ws
== old_ws
)
3522 unfocus_win(old_ws
->focus
);
3524 other_r
= new_ws
->r
;
3525 if (other_r
== NULL
) {
3526 /* the other workspace is hidden, hide this one */
3530 /* the other ws is visible in another region, exchange them */
3531 other_r
->ws_prior
= new_ws
;
3532 other_r
->ws
= old_ws
;
3533 old_ws
->r
= other_r
;
3535 this_r
->ws_prior
= old_ws
;
3536 this_r
->ws
= new_ws
;
3539 /* Set focus_pending before stacking, if needed. */
3540 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& (!new_ws
->focus_pending
||
3541 validate_win(new_ws
->focus_pending
)))
3542 new_ws
->focus_pending
= get_region_focus(new_ws
->r
);
3546 /* unmap old windows */
3548 TAILQ_FOREACH(win
, &old_ws
->winlist
, entry
)
3551 /* if workspaces were swapped, then don't wait to set focus */
3552 if (old_ws
->r
&& focus_mode
!= SWM_FOCUS_FOLLOW
) {
3553 if (new_ws
->focus_pending
) {
3554 focus_win(new_ws
->focus_pending
);
3555 new_ws
->focus_pending
= NULL
;
3559 /* Clear bar and set focus on region input win if new ws is empty. */
3560 if (new_ws
->focus_pending
== NULL
&& new_ws
->focus
== NULL
) {
3561 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
, r
->id
,
3568 DNPRINTF(SWM_D_WS
, "switchws: done.\n");
3572 cyclews(struct swm_region
*r
, union arg
*args
)
3575 struct swm_screen
*s
= r
->s
;
3579 DNPRINTF(SWM_D_WS
, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3580 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
3586 case SWM_ARG_ID_CYCLEWS_MOVE_UP
:
3589 case SWM_ARG_ID_CYCLEWS_UP_ALL
:
3592 case SWM_ARG_ID_CYCLEWS_UP
:
3593 a
.id
= (a
.id
< workspace_limit
- 1) ? a
.id
+ 1 : 0;
3595 case SWM_ARG_ID_CYCLEWS_MOVE_DOWN
:
3598 case SWM_ARG_ID_CYCLEWS_DOWN_ALL
:
3601 case SWM_ARG_ID_CYCLEWS_DOWN
:
3602 a
.id
= (a
.id
> 0) ? a
.id
- 1 : workspace_limit
- 1;
3609 (!cycle_empty
&& TAILQ_EMPTY(&s
->ws
[a
.id
].winlist
)))
3611 if (!cycle_visible
&& s
->ws
[a
.id
].r
!= NULL
)
3618 } while (a
.id
!= r
->ws
->idx
);
3622 priorws(struct swm_region
*r
, union arg
*args
)
3628 DNPRINTF(SWM_D_WS
, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3629 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
3631 if (r
->ws_prior
== NULL
)
3634 a
.id
= r
->ws_prior
->idx
;
3639 focusrg(struct swm_region
*r
, union arg
*args
)
3641 int ridx
= args
->id
, i
, num_screens
;
3642 struct swm_region
*rr
= NULL
;
3644 num_screens
= get_screen_count();
3645 /* do nothing if we don't have more than one screen */
3646 if (!(num_screens
> 1 || outputs
> 1))
3649 DNPRINTF(SWM_D_FOCUS
, "focusrg: id: %d\n", ridx
);
3651 rr
= TAILQ_FIRST(&r
->s
->rl
);
3652 for (i
= 0; i
< ridx
; ++i
)
3653 rr
= TAILQ_NEXT(rr
, entry
);
3663 cyclerg(struct swm_region
*r
, union arg
*args
)
3665 struct swm_region
*rr
= NULL
;
3668 num_screens
= get_screen_count();
3669 /* do nothing if we don't have more than one screen */
3670 if (!(num_screens
> 1 || outputs
> 1))
3675 case SWM_ARG_ID_CYCLERG_UP
:
3676 rr
= TAILQ_NEXT(r
, entry
);
3678 rr
= TAILQ_FIRST(&screens
[i
].rl
);
3680 case SWM_ARG_ID_CYCLERG_DOWN
:
3681 rr
= TAILQ_PREV(r
, swm_region_list
, entry
);
3683 rr
= TAILQ_LAST(&screens
[i
].rl
, swm_region_list
);
3696 sort_windows(struct ws_win_list
*wl
)
3698 struct ws_win
*win
, *parent
, *nxt
;
3703 for (win
= TAILQ_FIRST(wl
); win
!= TAILQ_END(wl
); win
= nxt
) {
3704 nxt
= TAILQ_NEXT(win
, entry
);
3705 if (win
->transient
) {
3706 parent
= find_window(win
->transient
);
3707 if (parent
== NULL
) {
3708 warnx("not possible bug");
3711 TAILQ_REMOVE(wl
, win
, entry
);
3712 TAILQ_INSERT_AFTER(wl
, parent
, win
, entry
);
3719 swapwin(struct swm_region
*r
, union arg
*args
)
3721 struct ws_win
*target
, *source
;
3722 struct ws_win
*cur_focus
;
3723 struct ws_win_list
*wl
;
3725 DNPRINTF(SWM_D_WS
, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3726 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
3728 cur_focus
= r
->ws
->focus
;
3729 if (cur_focus
== NULL
)
3733 wl
= &source
->ws
->winlist
;
3736 case SWM_ARG_ID_SWAPPREV
:
3737 if (source
->transient
)
3738 source
= find_window(source
->transient
);
3739 target
= TAILQ_PREV(source
, ws_win_list
, entry
);
3740 if (target
&& target
->transient
)
3741 target
= find_window(target
->transient
);
3742 TAILQ_REMOVE(wl
, source
, entry
);
3744 TAILQ_INSERT_TAIL(wl
, source
, entry
);
3746 TAILQ_INSERT_BEFORE(target
, source
, entry
);
3748 case SWM_ARG_ID_SWAPNEXT
:
3749 target
= TAILQ_NEXT(source
, entry
);
3750 /* move the parent and let the sort handle the move */
3751 if (source
->transient
)
3752 source
= find_window(source
->transient
);
3753 TAILQ_REMOVE(wl
, source
, entry
);
3755 TAILQ_INSERT_HEAD(wl
, source
, entry
);
3757 TAILQ_INSERT_AFTER(wl
, target
, source
, entry
);
3759 case SWM_ARG_ID_SWAPMAIN
:
3760 target
= TAILQ_FIRST(wl
);
3761 if (target
== source
) {
3762 if (source
->ws
->focus_prev
!= NULL
&&
3763 source
->ws
->focus_prev
!= target
)
3764 source
= source
->ws
->focus_prev
;
3768 if (target
== NULL
|| source
== NULL
)
3770 source
->ws
->focus_prev
= target
;
3771 TAILQ_REMOVE(wl
, target
, entry
);
3772 TAILQ_INSERT_BEFORE(source
, target
, entry
);
3773 TAILQ_REMOVE(wl
, source
, entry
);
3774 TAILQ_INSERT_HEAD(wl
, source
, entry
);
3776 case SWM_ARG_ID_MOVELAST
:
3777 TAILQ_REMOVE(wl
, source
, entry
);
3778 TAILQ_INSERT_TAIL(wl
, source
, entry
);
3781 DNPRINTF(SWM_D_MOVE
, "swapwin: invalid id: %d\n", args
->id
);
3793 get_focus_prev(struct ws_win
*win
)
3795 struct ws_win
*winfocus
= NULL
;
3796 struct ws_win
*cur_focus
= NULL
;
3797 struct ws_win_list
*wl
= NULL
;
3798 struct workspace
*ws
= NULL
;
3800 if (!(win
&& win
->ws
))
3805 cur_focus
= ws
->focus
;
3807 DNPRINTF(SWM_D_FOCUS
, "get_focus_prev: window: 0x%x, cur_focus: 0x%x\n",
3808 WINID(win
), WINID(cur_focus
));
3810 /* pickle, just focus on whatever */
3811 if (cur_focus
== NULL
) {
3812 /* use prev_focus if valid */
3813 if (ws
->focus_prev
&& ws
->focus_prev
!= cur_focus
&&
3814 find_window(WINID(ws
->focus_prev
)))
3815 winfocus
= ws
->focus_prev
;
3819 /* if transient focus on parent */
3820 if (cur_focus
->transient
) {
3821 winfocus
= find_window(cur_focus
->transient
);
3825 /* if in max_stack try harder */
3826 if ((win
->quirks
& SWM_Q_FOCUSPREV
) ||
3827 (ws
->cur_layout
->flags
& SWM_L_FOCUSPREV
)) {
3828 if (cur_focus
!= ws
->focus_prev
)
3829 winfocus
= ws
->focus_prev
;
3831 winfocus
= TAILQ_PREV(win
, ws_win_list
, entry
);
3836 DNPRINTF(SWM_D_FOCUS
, "get_focus_prev: focus_close: %d\n", focus_close
);
3838 if (winfocus
== NULL
|| winfocus
== win
) {
3839 switch (focus_close
) {
3840 case SWM_STACK_BOTTOM
:
3841 TAILQ_FOREACH(winfocus
, wl
, entry
)
3842 if (!winfocus
->iconic
&& winfocus
!= cur_focus
)
3846 TAILQ_FOREACH_REVERSE(winfocus
, wl
, ws_win_list
, entry
)
3847 if (!winfocus
->iconic
&& winfocus
!= cur_focus
)
3850 case SWM_STACK_ABOVE
:
3851 winfocus
= TAILQ_NEXT(cur_focus
, entry
);
3852 while (winfocus
&& winfocus
->iconic
)
3853 winfocus
= TAILQ_NEXT(winfocus
, entry
);
3855 if (winfocus
== NULL
) {
3856 if (focus_close_wrap
) {
3857 TAILQ_FOREACH(winfocus
, wl
, entry
)
3858 if (!winfocus
->iconic
&&
3859 winfocus
!= cur_focus
)
3862 TAILQ_FOREACH_REVERSE(winfocus
, wl
,
3864 if (!winfocus
->iconic
&&
3865 winfocus
!= cur_focus
)
3870 case SWM_STACK_BELOW
:
3871 winfocus
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
3872 while (winfocus
&& winfocus
->iconic
)
3873 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
,
3876 if (winfocus
== NULL
) {
3877 if (focus_close_wrap
) {
3878 TAILQ_FOREACH_REVERSE(winfocus
, wl
,
3880 if (!winfocus
->iconic
&&
3881 winfocus
!= cur_focus
)
3884 TAILQ_FOREACH(winfocus
, wl
, entry
)
3885 if (!winfocus
->iconic
&&
3886 winfocus
!= cur_focus
)
3894 if (winfocus
== NULL
||
3895 (winfocus
&& (winfocus
->iconic
|| winfocus
== cur_focus
))) {
3896 if (focus_default
== SWM_STACK_TOP
) {
3897 TAILQ_FOREACH_REVERSE(winfocus
, wl
, ws_win_list
, entry
)
3898 if (!winfocus
->iconic
&& winfocus
!= cur_focus
)
3901 TAILQ_FOREACH(winfocus
, wl
, entry
)
3902 if (!winfocus
->iconic
&& winfocus
!= cur_focus
)
3909 return get_focus_magic(winfocus
);
3913 get_region_focus(struct swm_region
*r
)
3915 struct ws_win
*winfocus
= NULL
;
3920 if (r
->ws
->focus
&& !r
->ws
->focus
->iconic
)
3921 winfocus
= r
->ws
->focus
;
3922 else if (r
->ws
->focus_prev
&& !r
->ws
->focus_prev
->iconic
)
3923 winfocus
= r
->ws
->focus_prev
;
3925 TAILQ_FOREACH(winfocus
, &r
->ws
->winlist
, entry
)
3926 if (!winfocus
->iconic
)
3929 return get_focus_magic(winfocus
);
3933 focus(struct swm_region
*r
, union arg
*args
)
3935 struct ws_win
*head
, *cur_focus
= NULL
, *winfocus
= NULL
;
3936 struct ws_win_list
*wl
= NULL
;
3937 struct workspace
*ws
= NULL
;
3943 DNPRINTF(SWM_D_FOCUS
, "focus: id: %d\n", args
->id
);
3945 if ((cur_focus
= r
->ws
->focus
) == NULL
)
3949 if (TAILQ_EMPTY(wl
))
3951 /* make sure there is at least one uniconified window */
3953 TAILQ_FOREACH(winfocus
, wl
, entry
)
3954 if (!winfocus
->iconic
) {
3962 case SWM_ARG_ID_FOCUSPREV
:
3963 head
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
3965 head
= TAILQ_LAST(wl
, ws_win_list
);
3967 if (WINID(winfocus
) == cur_focus
->transient
) {
3968 head
= TAILQ_PREV(winfocus
, ws_win_list
, entry
);
3970 head
= TAILQ_LAST(wl
, ws_win_list
);
3975 if (winfocus
&& winfocus
->iconic
) {
3976 while (winfocus
!= cur_focus
) {
3977 if (winfocus
== NULL
)
3978 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
3979 if (!winfocus
->iconic
)
3981 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
,
3987 case SWM_ARG_ID_FOCUSNEXT
:
3988 head
= TAILQ_NEXT(cur_focus
, entry
);
3990 head
= TAILQ_FIRST(wl
);
3994 if (winfocus
&& winfocus
->iconic
) {
3995 while (winfocus
!= cur_focus
) {
3996 if (winfocus
== NULL
)
3997 winfocus
= TAILQ_FIRST(wl
);
3998 if (!winfocus
->iconic
)
4000 winfocus
= TAILQ_NEXT(winfocus
, entry
);
4005 case SWM_ARG_ID_FOCUSMAIN
:
4006 winfocus
= TAILQ_FIRST(wl
);
4007 if (winfocus
== cur_focus
)
4008 winfocus
= cur_focus
->ws
->focus_prev
;
4015 focus_win(get_focus_magic(winfocus
));
4021 cycle_layout(struct swm_region
*r
, union arg
*args
)
4023 struct workspace
*ws
= r
->ws
;
4025 /* suppress unused warning since var is needed */
4028 DNPRINTF(SWM_D_EVENT
, "cycle_layout: workspace: %d\n", ws
->idx
);
4031 if (ws
->cur_layout
->l_stack
== NULL
)
4032 ws
->cur_layout
= &layouts
[0];
4037 focus_win(get_region_focus(r
));
4043 stack_config(struct swm_region
*r
, union arg
*args
)
4045 struct workspace
*ws
= r
->ws
;
4047 DNPRINTF(SWM_D_STACK
, "stack_config: id: %d workspace: %d\n",
4050 if (ws
->cur_layout
->l_config
!= NULL
)
4051 ws
->cur_layout
->l_config(ws
, args
->id
);
4053 if (args
->id
!= SWM_ARG_ID_STACKINIT
)
4062 struct swm_geometry g
;
4063 struct swm_region
*r
;
4069 DNPRINTF(SWM_D_STACK
, "stack: begin\n");
4071 num_screens
= get_screen_count();
4072 for (i
= 0; i
< num_screens
; i
++) {
4076 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
4077 DNPRINTF(SWM_D_STACK
, "stack: workspace: %d "
4078 "(screen: %d, region: %d)\n", r
->ws
->idx
, i
, j
++);
4080 /* Adjust stack area for region bar and padding. */
4082 g
.x
+= region_padding
;
4083 g
.y
+= region_padding
;
4084 g
.w
-= 2 * border_width
+ 2 * region_padding
;
4085 g
.h
-= 2 * border_width
+ 2 * region_padding
;
4086 if (bar_enabled
&& r
->ws
->bar_enabled
) {
4091 r
->ws
->cur_layout
->l_stack(r
->ws
, &g
);
4092 r
->ws
->cur_layout
->l_string(r
->ws
);
4093 /* save r so we can track region changes */
4100 DNPRINTF(SWM_D_STACK
, "stack: end\n");
4104 store_float_geom(struct ws_win
*win
)
4106 if (win
== NULL
|| win
->ws
->r
== NULL
)
4109 /* retain window geom and region geom */
4110 win
->g_float
= win
->g
;
4111 win
->g_float
.x
-= X(win
->ws
->r
);
4112 win
->g_float
.y
-= Y(win
->ws
->r
);
4113 win
->g_floatvalid
= 1;
4114 DNPRINTF(SWM_D_MISC
, "store_float_geom: window: 0x%x, g: (%d,%d)"
4115 " %d x %d, g_float: (%d,%d) %d x %d\n", win
->id
, X(win
), Y(win
),
4116 WIDTH(win
), HEIGHT(win
), win
->g_float
.x
, win
->g_float
.y
,
4117 win
->g_float
.w
, win
->g_float
.h
);
4121 load_float_geom(struct ws_win
*win
)
4123 if (win
== NULL
|| win
->ws
->r
== NULL
)
4126 if (win
->g_floatvalid
) {
4127 win
->g
= win
->g_float
;
4128 X(win
) += X(win
->ws
->r
);
4129 Y(win
) += Y(win
->ws
->r
);
4130 DNPRINTF(SWM_D_MISC
, "load_float_geom: window: 0x%x, g: (%d,%d)"
4131 "%d x %d\n", win
->id
, X(win
), Y(win
), WIDTH(win
),
4134 DNPRINTF(SWM_D_MISC
, "load_float_geom: window: 0x%x, g_float "
4135 "is not set.\n", win
->id
);
4140 stack_floater(struct ws_win
*win
, struct swm_region
*r
)
4145 DNPRINTF(SWM_D_MISC
, "stack_floater: window: 0x%x\n", win
->id
);
4148 * to allow windows to change their size (e.g. mplayer fs) only retrieve
4149 * geom on ws switches or return from max mode
4151 if (win
->floatmaxed
|| (r
!= r
->ws
->old_r
&&
4152 !(win
->ewmh_flags
& EWMH_F_FULLSCREEN
))) {
4153 /* update geometry for the new region */
4154 load_float_geom(win
);
4157 win
->floatmaxed
= 0;
4160 * if set to fullscreen mode, configure window to maximum size.
4162 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
4163 if (!win
->g_floatvalid
)
4164 store_float_geom(win
);
4170 * remove border on fullscreen floater when in fullscreen mode or when
4171 * the quirk is present.
4173 if ((win
->ewmh_flags
& EWMH_F_FULLSCREEN
) ||
4174 ((win
->quirks
& SWM_Q_FULLSCREEN
) &&
4175 (WIDTH(win
) >= WIDTH(r
)) && (HEIGHT(win
) >= HEIGHT(r
)))) {
4176 if (win
->bordered
) {
4178 X(win
) += border_width
;
4179 Y(win
) += border_width
;
4181 } else if (!win
->bordered
) {
4183 X(win
) -= border_width
;
4184 Y(win
) -= border_width
;
4187 if (win
->transient
&& (win
->quirks
& SWM_Q_TRANSSZ
)) {
4188 WIDTH(win
) = (double)WIDTH(r
) * dialog_ratio
;
4189 HEIGHT(win
) = (double)HEIGHT(r
) * dialog_ratio
;
4192 if (!win
->manual
&& !(win
->ewmh_flags
& EWMH_F_FULLSCREEN
) &&
4193 !(win
->quirks
& SWM_Q_ANYWHERE
)) {
4195 * floaters and transients are auto-centred unless moved,
4196 * resized or ANYWHERE quirk is set.
4198 X(win
) = X(r
) + (WIDTH(r
) - WIDTH(win
)) / 2 - BORDER(win
);
4199 Y(win
) = Y(r
) + (HEIGHT(r
) - HEIGHT(win
)) / 2 - BORDER(win
);
4201 store_float_geom(win
);
4204 /* Ensure at least 1 pixel of the window is in the region. */
4205 region_containment(win
, r
, SWM_CW_ALLSIDES
);
4211 * Send keystrokes to terminal to decrease/increase the font size as the
4212 * window size changes.
4215 adjust_font(struct ws_win
*win
)
4217 if (!(win
->quirks
& SWM_Q_XTERM_FONTADJ
) ||
4218 win
->floating
|| win
->transient
)
4221 if (win
->sh
.width_inc
&& win
->last_inc
!= win
->sh
.width_inc
&&
4222 WIDTH(win
) / win
->sh
.width_inc
< term_width
&&
4223 win
->font_steps
< SWM_MAX_FONT_STEPS
) {
4224 win
->font_size_boundary
[win
->font_steps
] =
4225 (win
->sh
.width_inc
* term_width
) + win
->sh
.base_width
;
4228 win
->last_inc
= win
->sh
.width_inc
;
4229 fake_keypress(win
, XK_KP_Subtract
, XCB_MOD_MASK_SHIFT
);
4230 } else if (win
->font_steps
&& win
->last_inc
!= win
->sh
.width_inc
&&
4231 WIDTH(win
) > win
->font_size_boundary
[win
->font_steps
- 1]) {
4234 win
->last_inc
= win
->sh
.width_inc
;
4235 fake_keypress(win
, XK_KP_Add
, XCB_MOD_MASK_SHIFT
);
4239 #define SWAPXY(g) do { \
4241 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
4242 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
4245 stack_master(struct workspace
*ws
, struct swm_geometry
*g
, int rot
, int flip
)
4247 struct swm_geometry win_g
, r_g
= *g
;
4248 struct ws_win
*win
, *fs_win
= NULL
;
4249 int i
, j
, s
, stacks
;
4250 int w_inc
= 1, h_inc
, w_base
= 1, h_base
;
4251 int hrh
, extra
= 0, h_slice
, last_h
= 0;
4252 int split
, colno
, winno
, mwin
, msize
, mscale
;
4253 int remain
, missing
, v_slice
, reconfigure
= 0;
4256 DNPRINTF(SWM_D_STACK
, "stack_master: workspace: %d, rot: %s, "
4257 "flip: %s\n", ws
->idx
, YESNO(rot
), YESNO(flip
));
4259 winno
= count_win(ws
, 0);
4260 if (winno
== 0 && count_win(ws
, 1) == 0)
4263 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
4264 if (!win
->transient
&& !win
->floating
&& !win
->iconic
)
4271 w_inc
= win
->sh
.width_inc
;
4272 w_base
= win
->sh
.base_width
;
4273 mwin
= ws
->l_state
.horizontal_mwin
;
4274 mscale
= ws
->l_state
.horizontal_msize
;
4275 stacks
= ws
->l_state
.horizontal_stacks
;
4278 w_inc
= win
->sh
.height_inc
;
4279 w_base
= win
->sh
.base_height
;
4280 mwin
= ws
->l_state
.vertical_mwin
;
4281 mscale
= ws
->l_state
.vertical_msize
;
4282 stacks
= ws
->l_state
.vertical_stacks
;
4286 if (stacks
> winno
- mwin
)
4287 stacks
= winno
- mwin
;
4291 h_slice
= r_g
.h
/ SWM_H_SLICE
;
4292 if (mwin
&& winno
> mwin
) {
4293 v_slice
= r_g
.w
/ SWM_V_SLICE
;
4297 win_g
.w
= v_slice
* mscale
;
4299 if (w_inc
> 1 && w_inc
< v_slice
) {
4300 /* adjust for window's requested size increment */
4301 remain
= (win_g
.w
- w_base
) % w_inc
;
4307 win_g
.x
+= r_g
.w
- msize
;
4310 colno
= split
= winno
/ stacks
;
4311 win_g
.w
= ((r_g
.w
- (stacks
* 2 * border_width
) +
4312 2 * border_width
) / stacks
);
4314 hrh
= r_g
.h
/ colno
;
4315 extra
= r_g
.h
- (colno
* hrh
);
4316 win_g
.h
= hrh
- 2 * border_width
;
4318 /* stack all the tiled windows */
4319 i
= j
= 0, s
= stacks
;
4320 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
4321 if (win
->transient
|| win
->floating
|| win
->iconic
)
4324 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
4329 if (split
&& i
== split
) {
4330 colno
= (winno
- mwin
) / stacks
;
4331 if (s
<= (winno
- mwin
) % stacks
)
4334 hrh
= r_g
.h
/ colno
;
4335 extra
= r_g
.h
- (colno
* hrh
);
4338 win_g
.x
+= win_g
.w
+ 2 * border_width
+
4341 win_g
.w
= (r_g
.w
- msize
-
4342 (stacks
* (2 * border_width
+ tile_gap
))) / stacks
;
4344 win_g
.w
+= (r_g
.w
- msize
-
4345 (stacks
* (2 * border_width
+ tile_gap
))) %
4349 win_g
.x
-= win_g
.w
+ 2 * border_width
+
4355 win_g
.h
= hrh
- 2 * border_width
- tile_gap
;
4358 h_inc
= win
->sh
.width_inc
;
4359 h_base
= win
->sh
.base_width
;
4361 h_inc
= win
->sh
.height_inc
;
4362 h_base
= win
->sh
.base_height
;
4365 if (j
== colno
- 1) {
4366 win_g
.h
= hrh
+ extra
;
4367 } else if (h_inc
> 1 && h_inc
< h_slice
) {
4368 /* adjust for window's requested size increment */
4369 remain
= (win_g
.h
- h_base
) % h_inc
;
4370 missing
= h_inc
- remain
;
4372 if (missing
<= extra
|| j
== 0) {
4384 win_g
.y
+= last_h
+ 2 * border_width
+ tile_gap
;
4386 if (disable_border
&& !(bar_enabled
&& ws
->bar_enabled
) &&
4389 win_g
.w
+= 2 * border_width
;
4390 win_g
.h
+= 2 * border_width
;
4396 if (X(win
) != win_g
.y
|| Y(win
) != win_g
.x
||
4397 WIDTH(win
) != win_g
.h
|| HEIGHT(win
) != win_g
.w
) {
4401 WIDTH(win
) = win_g
.h
;
4402 HEIGHT(win
) = win_g
.w
;
4405 if (X(win
) != win_g
.x
|| Y(win
) != win_g
.y
||
4406 WIDTH(win
) != win_g
.w
|| HEIGHT(win
) != win_g
.h
) {
4410 WIDTH(win
) = win_g
.w
;
4411 HEIGHT(win
) = win_g
.h
;
4415 if (bordered
!= win
->bordered
) {
4417 win
->bordered
= bordered
;
4425 map_window(win
, get_sibling(win
, SWM_STACK_BELOW
));
4432 /* Map/raise focused tiled window to top if windows could overlap. */
4433 if (tile_gap
< 0 && ws
->focus
!= NULL
&& !ws
->focus
->floating
)
4434 map_window(ws
->focus
, get_sibling(ws
->focus
, SWM_STACK_TOP
));
4437 /* now, stack all the floaters and transients */
4438 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
4439 if (!win
->transient
&& !win
->floating
)
4443 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
4448 stack_floater(win
, ws
->r
);
4449 map_window(win
, XCB_WINDOW_NONE
);
4452 /* Make sure fs_win is stacked last so it's on top. */
4454 stack_floater(fs_win
, ws
->r
);
4455 map_window(fs_win
, XCB_WINDOW_NONE
);
4460 vertical_config(struct workspace
*ws
, int id
)
4462 DNPRINTF(SWM_D_STACK
, "vertical_config: id: %d, workspace: %d\n",
4466 case SWM_ARG_ID_STACKRESET
:
4467 case SWM_ARG_ID_STACKINIT
:
4468 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ 2;
4469 ws
->l_state
.vertical_mwin
= 1;
4470 ws
->l_state
.vertical_stacks
= 1;
4472 case SWM_ARG_ID_MASTERSHRINK
:
4473 if (ws
->l_state
.vertical_msize
> 1)
4474 ws
->l_state
.vertical_msize
--;
4476 case SWM_ARG_ID_MASTERGROW
:
4477 if (ws
->l_state
.vertical_msize
< SWM_V_SLICE
- 1)
4478 ws
->l_state
.vertical_msize
++;
4480 case SWM_ARG_ID_MASTERADD
:
4481 ws
->l_state
.vertical_mwin
++;
4483 case SWM_ARG_ID_MASTERDEL
:
4484 if (ws
->l_state
.vertical_mwin
> 0)
4485 ws
->l_state
.vertical_mwin
--;
4487 case SWM_ARG_ID_STACKINC
:
4488 ws
->l_state
.vertical_stacks
++;
4490 case SWM_ARG_ID_STACKDEC
:
4491 if (ws
->l_state
.vertical_stacks
> 1)
4492 ws
->l_state
.vertical_stacks
--;
4494 case SWM_ARG_ID_FLIPLAYOUT
:
4495 ws
->l_state
.vertical_flip
= !ws
->l_state
.vertical_flip
;
4503 vertical_stack(struct workspace
*ws
, struct swm_geometry
*g
)
4505 DNPRINTF(SWM_D_STACK
, "vertical_stack: workspace: %d\n", ws
->idx
);
4507 stack_master(ws
, g
, 0, ws
->l_state
.vertical_flip
);
4511 horizontal_config(struct workspace
*ws
, int id
)
4513 DNPRINTF(SWM_D_STACK
, "horizontal_config: workspace: %d\n", ws
->idx
);
4516 case SWM_ARG_ID_STACKRESET
:
4517 case SWM_ARG_ID_STACKINIT
:
4518 ws
->l_state
.horizontal_mwin
= 1;
4519 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ 2;
4520 ws
->l_state
.horizontal_stacks
= 1;
4522 case SWM_ARG_ID_MASTERSHRINK
:
4523 if (ws
->l_state
.horizontal_msize
> 1)
4524 ws
->l_state
.horizontal_msize
--;
4526 case SWM_ARG_ID_MASTERGROW
:
4527 if (ws
->l_state
.horizontal_msize
< SWM_H_SLICE
- 1)
4528 ws
->l_state
.horizontal_msize
++;
4530 case SWM_ARG_ID_MASTERADD
:
4531 ws
->l_state
.horizontal_mwin
++;
4533 case SWM_ARG_ID_MASTERDEL
:
4534 if (ws
->l_state
.horizontal_mwin
> 0)
4535 ws
->l_state
.horizontal_mwin
--;
4537 case SWM_ARG_ID_STACKINC
:
4538 ws
->l_state
.horizontal_stacks
++;
4540 case SWM_ARG_ID_STACKDEC
:
4541 if (ws
->l_state
.horizontal_stacks
> 1)
4542 ws
->l_state
.horizontal_stacks
--;
4544 case SWM_ARG_ID_FLIPLAYOUT
:
4545 ws
->l_state
.horizontal_flip
= !ws
->l_state
.horizontal_flip
;
4553 horizontal_stack(struct workspace
*ws
, struct swm_geometry
*g
)
4555 DNPRINTF(SWM_D_STACK
, "horizontal_stack: workspace: %d\n", ws
->idx
);
4557 stack_master(ws
, g
, 1, ws
->l_state
.horizontal_flip
);
4560 /* fullscreen view */
4562 max_stack(struct workspace
*ws
, struct swm_geometry
*g
)
4564 struct swm_geometry gg
= *g
;
4565 struct ws_win
*w
, *win
= NULL
, *parent
= NULL
;
4568 DNPRINTF(SWM_D_STACK
, "max_stack: workspace: %d\n", ws
->idx
);
4573 winno
= count_win(ws
, 0);
4574 if (winno
== 0 && count_win(ws
, 1) == 0)
4577 /* Figure out which top level window should be visible. */
4578 if (ws
->focus_pending
)
4579 win
= ws
->focus_pending
;
4582 else if (ws
->focus_prev
)
4583 win
= ws
->focus_prev
;
4585 win
= TAILQ_FIRST(&ws
->winlist
);
4588 parent
= find_window(win
->transient
);
4590 DNPRINTF(SWM_D_STACK
, "max_stack: win: 0x%x\n", win
->id
);
4592 /* maximize all top level windows */
4593 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
4594 if (w
->transient
|| w
->iconic
)
4597 if (!w
->mapped
&& w
!= win
)
4598 map_window(w
, XCB_WINDOW_NONE
);
4600 if (w
->floating
&& !w
->floatmaxed
) {
4602 * retain geometry for retrieval on exit from
4605 store_float_geom(w
);
4609 /* only reconfigure if necessary */
4610 if (X(w
) != gg
.x
|| Y(w
) != gg
.y
|| WIDTH(w
) != gg
.w
||
4611 HEIGHT(w
) != gg
.h
) {
4613 if (bar_enabled
&& ws
->bar_enabled
){
4617 WIDTH(w
) += 2 * border_width
;
4618 HEIGHT(w
) += 2 * border_width
;
4625 /* If a parent exists, map/raise it first. */
4627 map_window(parent
, XCB_WINDOW_NONE
);
4629 /* Map siblings next. */
4630 TAILQ_FOREACH(w
, &ws
->winlist
, entry
)
4631 if (w
!= win
&& !w
->iconic
&&
4632 w
->transient
== parent
->id
) {
4633 stack_floater(w
, ws
->r
);
4634 map_window(w
, XCB_WINDOW_NONE
);
4638 /* Map/raise focused window. */
4639 map_window(win
, XCB_WINDOW_NONE
);
4641 /* Finally, map/raise children of focus window. */
4642 TAILQ_FOREACH(w
, &ws
->winlist
, entry
)
4643 if (w
->transient
== win
->id
&& !w
->iconic
) {
4644 stack_floater(w
, ws
->r
);
4645 map_window(w
, XCB_WINDOW_NONE
);
4650 send_to_rg(struct swm_region
*r
, union arg
*args
)
4652 int ridx
= args
->id
, i
, num_screens
;
4653 struct swm_region
*rr
= NULL
;
4656 num_screens
= get_screen_count();
4657 /* do nothing if we don't have more than one screen */
4658 if (!(num_screens
> 1 || outputs
> 1))
4661 DNPRINTF(SWM_D_FOCUS
, "send_to_rg: id: %d\n", ridx
);
4663 rr
= TAILQ_FIRST(&r
->s
->rl
);
4664 for (i
= 0; i
< ridx
; ++i
)
4665 rr
= TAILQ_NEXT(rr
, entry
);
4676 region_under(struct swm_screen
*s
, int x
, int y
)
4678 struct swm_region
*r
= NULL
;
4680 TAILQ_FOREACH(r
, &s
->rl
, entry
) {
4681 DNPRINTF(SWM_D_MISC
, "region_under: ws: %d, region g: (%d,%d) "
4682 "%d x %d, coords: (%d,%d)\n", r
->ws
->idx
, X(r
), Y(r
),
4683 WIDTH(r
), HEIGHT(r
), x
, y
);
4684 if (X(r
) <= x
&& x
< MAX_X(r
))
4685 if (Y(r
) <= y
&& y
< MAX_Y(r
))
4693 send_to_ws(struct swm_region
*r
, union arg
*args
)
4695 int wsid
= args
->id
;
4696 struct ws_win
*win
= NULL
;
4698 if (r
&& r
->ws
&& r
->ws
->focus
)
4703 DNPRINTF(SWM_D_MOVE
, "send_to_ws: win 0x%x, ws %d\n", win
->id
, wsid
);
4705 win_to_ws(win
, wsid
, 1);
4707 /* Restack and set new focus. */
4710 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
4711 if (r
->ws
->focus_pending
) {
4712 focus_win(r
->ws
->focus_pending
);
4713 r
->ws
->focus_pending
= NULL
;
4715 xcb_set_input_focus(conn
,
4716 XCB_INPUT_FOCUS_PARENT
, r
->id
,
4725 win_to_ws(struct ws_win
*win
, int wsid
, int unfocus
)
4727 struct ws_win
*parent
;
4728 struct workspace
*ws
, *nws
, *pws
;
4729 char ws_idx_str
[SWM_PROPLEN
];
4731 if (wsid
>= workspace_limit
)
4734 if (win
->ws
->idx
== wsid
)
4738 nws
= &win
->s
->ws
[wsid
];
4740 DNPRINTF(SWM_D_MOVE
, "win_to_ws: win 0x%x, ws %d -> %d\n", win
->id
,
4743 /* Update the window's workspace property: _SWM_WS */
4744 if (snprintf(ws_idx_str
, SWM_PROPLEN
, "%d", nws
->idx
) < SWM_PROPLEN
) {
4745 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
4746 ws
->focus_pending
= get_focus_prev(win
);
4748 /* Move the parent if this is a transient window. */
4749 if (win
->transient
) {
4750 parent
= find_window(win
->transient
);
4753 /* Set new focus in parent's ws if needed. */
4754 if (pws
->focus
== parent
) {
4755 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
4756 pws
->focus_pending
=
4757 get_focus_prev(parent
);
4759 unfocus_win(parent
);
4761 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
4762 pws
->focus
= pws
->focus_pending
;
4763 pws
->focus_pending
= NULL
;
4767 /* Don't unmap parent if new ws is visible */
4769 unmap_window(parent
);
4772 TAILQ_REMOVE(&ws
->winlist
, parent
, entry
);
4773 TAILQ_INSERT_TAIL(&nws
->winlist
, parent
, entry
);
4776 DNPRINTF(SWM_D_PROP
, "win_to_ws: set "
4777 "property: _SWM_WS: %s\n", ws_idx_str
);
4778 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
4779 parent
->id
, a_swm_ws
, XCB_ATOM_STRING
, 8,
4780 strlen(ws_idx_str
), ws_idx_str
);
4787 /* Don't unmap if new ws is visible */
4792 TAILQ_REMOVE(&ws
->winlist
, win
, entry
);
4793 TAILQ_INSERT_TAIL(&nws
->winlist
, win
, entry
);
4796 /* Set focus on new ws. */
4797 unfocus_win(nws
->focus
);
4800 DNPRINTF(SWM_D_PROP
, "win_to_ws: set property: _SWM_WS: %s\n",
4802 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
4803 a_swm_ws
, XCB_ATOM_STRING
, 8, strlen(ws_idx_str
),
4807 DNPRINTF(SWM_D_MOVE
, "win_to_ws: done.\n");
4811 pressbutton(struct swm_region
*r
, union arg
*args
)
4813 /* suppress unused warning since var is needed */
4816 xcb_test_fake_input(conn
, XCB_BUTTON_PRESS
, args
->id
,
4817 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
4818 xcb_test_fake_input(conn
, XCB_BUTTON_RELEASE
, args
->id
,
4819 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
4823 raise_toggle(struct swm_region
*r
, union arg
*args
)
4825 /* suppress unused warning since var is needed */
4828 if (r
== NULL
|| r
->ws
== NULL
)
4831 r
->ws
->always_raise
= !r
->ws
->always_raise
;
4833 /* bring floaters back to top */
4834 if (!r
->ws
->always_raise
)
4841 iconify(struct swm_region
*r
, union arg
*args
)
4843 /* suppress unused warning since var is needed */
4846 if (r
->ws
->focus
== NULL
)
4849 set_swm_iconic(r
->ws
->focus
, 1);
4855 get_win_name(xcb_window_t win
)
4858 xcb_get_property_cookie_t c
;
4859 xcb_get_property_reply_t
*r
;
4861 /* First try _NET_WM_NAME for UTF-8. */
4862 c
= xcb_get_property(conn
, 0, win
, a_netwmname
,
4863 XCB_GET_PROPERTY_TYPE_ANY
, 0, UINT_MAX
);
4864 r
= xcb_get_property_reply(conn
, c
, NULL
);
4867 if (r
->type
== XCB_NONE
) {
4869 /* Use WM_NAME instead; no UTF-8. */
4870 c
= xcb_get_property(conn
, 0, win
, XCB_ATOM_WM_NAME
,
4871 XCB_GET_PROPERTY_TYPE_ANY
, 0, UINT_MAX
);
4872 r
= xcb_get_property_reply(conn
, c
, NULL
);
4876 if (r
->type
== XCB_NONE
) {
4882 name
= strndup(xcb_get_property_value(r
),
4883 xcb_get_property_value_length(r
));
4892 uniconify(struct swm_region
*r
, union arg
*args
)
4899 DNPRINTF(SWM_D_MISC
, "uniconify\n");
4901 if (r
== NULL
|| r
->ws
== NULL
)
4904 /* make sure we have anything to uniconify */
4905 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
4906 if (win
->ws
== NULL
)
4907 continue; /* should never happen */
4916 search_resp_action
= SWM_SEARCH_UNICONIFY
;
4918 spawn_select(r
, args
, "search", &searchpid
);
4920 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
4923 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
4924 if (win
->ws
== NULL
)
4925 continue; /* should never happen */
4929 name
= get_win_name(win
->id
);
4932 fprintf(lfile
, "%s.%u\n", name
, win
->id
);
4940 name_workspace(struct swm_region
*r
, union arg
*args
)
4944 DNPRINTF(SWM_D_MISC
, "name_workspace\n");
4950 search_resp_action
= SWM_SEARCH_NAME_WORKSPACE
;
4952 spawn_select(r
, args
, "name_workspace", &searchpid
);
4954 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
4957 fprintf(lfile
, "%s", "");
4962 search_workspace(struct swm_region
*r
, union arg
*args
)
4965 struct workspace
*ws
;
4968 DNPRINTF(SWM_D_MISC
, "search_workspace\n");
4974 search_resp_action
= SWM_SEARCH_SEARCH_WORKSPACE
;
4976 spawn_select(r
, args
, "search", &searchpid
);
4978 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
4981 for (i
= 0; i
< workspace_limit
; i
++) {
4985 fprintf(lfile
, "%d%s%s\n", ws
->idx
+ 1,
4986 (ws
->name
? ":" : ""), (ws
->name
? ws
->name
: ""));
4993 search_win_cleanup(void)
4995 struct search_window
*sw
= NULL
;
4997 while ((sw
= TAILQ_FIRST(&search_wl
)) != NULL
) {
4998 xcb_destroy_window(conn
, sw
->indicator
);
4999 TAILQ_REMOVE(&search_wl
, sw
, entry
);
5005 search_win(struct swm_region
*r
, union arg
*args
)
5007 struct ws_win
*win
= NULL
;
5008 struct search_window
*sw
= NULL
;
5011 int i
, width
, height
;
5019 XRectangle l_ibox
, l_lbox
;
5021 DNPRINTF(SWM_D_MISC
, "search_win\n");
5024 search_resp_action
= SWM_SEARCH_SEARCH_WINDOW
;
5026 spawn_select(r
, args
, "search", &searchpid
);
5028 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
5031 TAILQ_INIT(&search_wl
);
5034 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
5038 sw
= calloc(1, sizeof(struct search_window
));
5040 warn("search_win: calloc");
5042 search_win_cleanup();
5048 snprintf(s
, sizeof s
, "%d", i
);
5051 w
= xcb_generate_id(conn
);
5052 wa
[0] = r
->s
->c
[SWM_S_COLOR_FOCUS
].pixel
;
5053 wa
[1] = r
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
;
5055 if (bar_font_legacy
) {
5056 XmbTextExtents(bar_fs
, s
, len
, &l_ibox
, &l_lbox
);
5057 width
= l_lbox
.width
+ 4;
5058 height
= bar_fs_extents
->max_logical_extent
.height
+ 4;
5060 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
,
5062 width
= info
.width
+ 4;
5063 height
= bar_font
->height
+ 4;
5066 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, w
, win
->id
, 0, 0,
5067 width
, height
, 1, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
5068 XCB_COPY_FROM_PARENT
, XCB_CW_BACK_PIXEL
|
5069 XCB_CW_BORDER_PIXEL
, wa
);
5071 xcb_map_window(conn
, w
);
5074 TAILQ_INSERT_TAIL(&search_wl
, sw
, entry
);
5076 if (bar_font_legacy
) {
5077 l_gcv
.graphics_exposures
= 0;
5078 l_draw
= XCreateGC(display
, w
, 0, &l_gcv
);
5080 XSetForeground(display
, l_draw
,
5081 r
->s
->c
[SWM_S_COLOR_BAR
].pixel
);
5083 DRAWSTRING(display
, w
, bar_fs
, l_draw
, 2,
5084 (bar_fs_extents
->max_logical_extent
.height
-
5085 l_lbox
.height
) / 2 - l_lbox
.y
, s
, len
);
5087 XFreeGC(display
, l_draw
);
5090 draw
= XftDrawCreate(display
, w
,
5091 DefaultVisual(display
, r
->s
->idx
),
5092 DefaultColormap(display
, r
->s
->idx
));
5094 XftDrawStringUtf8(draw
, &bar_font_color
, bar_font
, 2,
5095 (HEIGHT(r
->bar
) + bar_font
->height
) / 2 -
5096 bar_font
->descent
, (FcChar8
*)s
, len
);
5098 XftDrawDestroy(draw
);
5101 DNPRINTF(SWM_D_MISC
, "search_win: mapped window: 0x%x\n", w
);
5103 fprintf(lfile
, "%d\n", i
);
5113 search_resp_uniconify(const char *resp
, size_t len
)
5119 DNPRINTF(SWM_D_MISC
, "search_resp_uniconify: resp: %s\n", resp
);
5121 TAILQ_FOREACH(win
, &search_r
->ws
->winlist
, entry
) {
5124 name
= get_win_name(win
->id
);
5127 if (asprintf(&s
, "%s.%u", name
, win
->id
) == -1) {
5132 if (strncmp(s
, resp
, len
) == 0) {
5133 /* XXX this should be a callback to generalize */
5134 set_swm_iconic(win
, 0);
5143 search_resp_name_workspace(const char *resp
, size_t len
)
5145 struct workspace
*ws
;
5147 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: resp: %s\n", resp
);
5149 if (search_r
->ws
== NULL
)
5154 free(search_r
->ws
->name
);
5155 search_r
->ws
->name
= NULL
;
5159 ws
->name
= strdup(resp
);
5160 if (ws
->name
== NULL
) {
5161 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: "
5162 "strdup: %s", strerror(errno
));
5169 search_resp_search_workspace(const char *resp
)
5176 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: resp: %s\n", resp
);
5180 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: strdup: %s",
5187 ws_idx
= (int)strtonum(q
, 1, workspace_limit
, &errstr
);
5189 DNPRINTF(SWM_D_MISC
, "workspace idx is %s: %s",
5196 switchws(search_r
, &a
);
5200 search_resp_search_window(const char *resp
)
5205 struct search_window
*sw
;
5207 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: resp: %s\n", resp
);
5211 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: strdup: %s",
5216 idx
= (int)strtonum(s
, 1, INT_MAX
, &errstr
);
5218 DNPRINTF(SWM_D_MISC
, "window idx is %s: %s",
5225 TAILQ_FOREACH(sw
, &search_wl
, entry
)
5226 if (idx
== sw
->idx
) {
5232 #define MAX_RESP_LEN 1024
5235 search_do_resp(void)
5241 DNPRINTF(SWM_D_MISC
, "search_do_resp:\n");
5246 if ((resp
= calloc(1, MAX_RESP_LEN
+ 1)) == NULL
) {
5247 warn("search: calloc");
5251 rbytes
= read(select_resp_pipe
[0], resp
, MAX_RESP_LEN
);
5253 warn("search: read error");
5256 resp
[rbytes
] = '\0';
5259 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
5260 * newline, so work around that by sanitizing the resp now.
5262 resp
[strcspn(resp
, "\n")] = '\0';
5265 switch (search_resp_action
) {
5266 case SWM_SEARCH_UNICONIFY
:
5267 search_resp_uniconify(resp
, len
);
5269 case SWM_SEARCH_NAME_WORKSPACE
:
5270 search_resp_name_workspace(resp
, len
);
5272 case SWM_SEARCH_SEARCH_WORKSPACE
:
5273 search_resp_search_workspace(resp
);
5275 case SWM_SEARCH_SEARCH_WINDOW
:
5276 search_resp_search_window(resp
);
5281 if (search_resp_action
== SWM_SEARCH_SEARCH_WINDOW
)
5282 search_win_cleanup();
5284 search_resp_action
= SWM_SEARCH_NONE
;
5285 close(select_resp_pipe
[0]);
5292 wkill(struct swm_region
*r
, union arg
*args
)
5294 DNPRINTF(SWM_D_MISC
, "wkill: id: %d\n", args
->id
);
5296 if (r
->ws
->focus
== NULL
)
5299 if (args
->id
== SWM_ARG_ID_KILLWINDOW
)
5300 xcb_kill_client(conn
, r
->ws
->focus
->id
);
5302 if (r
->ws
->focus
->can_delete
)
5303 client_msg(r
->ws
->focus
, a_delete
, 0);
5309 floating_toggle_win(struct ws_win
*win
)
5311 struct swm_region
*r
;
5321 /* reject floating toggles in max stack mode */
5322 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
5325 if (win
->floating
) {
5326 if (!win
->floatmaxed
) {
5327 /* retain position for refloat */
5328 store_float_geom(win
);
5332 load_float_geom(win
);
5336 ewmh_update_actions(win
);
5342 floating_toggle(struct swm_region
*r
, union arg
*args
)
5344 struct ws_win
*win
= r
->ws
->focus
;
5346 /* suppress unused warning since var is needed */
5352 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
5355 ewmh_update_win_state(win
, ewmh
[_NET_WM_STATE_ABOVE
].atom
,
5356 _NET_WM_STATE_TOGGLE
);
5360 if (win
== win
->ws
->focus
)
5367 region_containment(struct ws_win
*win
, struct swm_region
*r
, int opts
)
5369 struct swm_geometry g
= r
->g
;
5370 int rt
, lt
, tp
, bm
, bw
;
5372 bw
= (opts
& SWM_CW_SOFTBOUNDARY
) ? boundary_width
: 0;
5375 * Perpendicular distance of each side of the window to the respective
5376 * side of the region boundary. Positive values indicate the side of
5377 * the window has passed beyond the region boundary.
5379 rt
= opts
& SWM_CW_RIGHT
? MAX_X(win
) + BORDER(win
) - MAX_X(r
) : bw
;
5380 lt
= opts
& SWM_CW_LEFT
? X(r
) - X(win
) + BORDER(win
) : bw
;
5381 bm
= opts
& SWM_CW_BOTTOM
? MAX_Y(win
) + BORDER(win
) - MAX_Y(r
) : bw
;
5382 tp
= opts
& SWM_CW_TOP
? Y(r
) - Y(win
) + BORDER(win
) : bw
;
5384 DNPRINTF(SWM_D_MISC
, "region_containment: win 0x%x, rt: %d, lt: %d, "
5385 "bm: %d, tp: %d, SOFTBOUNDARY: %s, HARDBOUNDARY: %s\n", win
->id
, rt
,
5386 lt
, bm
, tp
, YESNO(opts
& SWM_CW_SOFTBOUNDARY
),
5387 YESNO(opts
& SWM_CW_HARDBOUNDARY
));
5390 * Disable containment if any of the flagged sides went beyond the
5391 * containment boundary, or if containment is disabled.
5393 if (!(opts
& SWM_CW_HARDBOUNDARY
|| opts
& SWM_CW_SOFTBOUNDARY
) ||
5394 (bw
!= 0 && ((rt
> bw
) || (lt
> bw
) || (bm
> bw
) || (tp
> bw
)))) {
5395 /* Make sure window has at least 1 pixel in the region */
5396 g
.x
+= 1 - WIDTH(win
);
5397 g
.y
+= 1 - HEIGHT(win
);
5398 g
.w
+= 2 * WIDTH(win
) - 2;
5399 g
.h
+= 2 * HEIGHT(win
) - 2;
5402 constrain_window(win
, &g
, &opts
);
5405 /* Move or resize a window so that flagged side(s) fit into the supplied box. */
5407 constrain_window(struct ws_win
*win
, struct swm_geometry
*b
, int *opts
)
5409 DNPRINTF(SWM_D_MISC
, "constrain_window: window: 0x%x, (x,y) w x h: "
5410 "(%d,%d) %d x %d, box: (x,y) w x h: (%d,%d) %d x %d, rt: %s, "
5411 "lt: %s, bt: %s, tp: %s, allow resize: %s\n", win
->id
, X(win
),
5412 Y(win
), WIDTH(win
), HEIGHT(win
), b
->x
, b
->y
, b
->w
, b
->h
,
5413 YESNO(*opts
& SWM_CW_RIGHT
), YESNO(*opts
& SWM_CW_LEFT
),
5414 YESNO(*opts
& SWM_CW_BOTTOM
), YESNO(*opts
& SWM_CW_TOP
),
5415 YESNO(*opts
& SWM_CW_RESIZABLE
));
5417 if ((*opts
& SWM_CW_RIGHT
) && MAX_X(win
) + BORDER(win
) > b
->x
+ b
->w
) {
5418 if (*opts
& SWM_CW_RESIZABLE
)
5419 WIDTH(win
) = b
->x
+ b
->w
- X(win
) - BORDER(win
);
5421 X(win
) = b
->x
+ b
->w
- WIDTH(win
) - BORDER(win
);
5424 if ((*opts
& SWM_CW_LEFT
) && X(win
) + BORDER(win
) < b
->x
) {
5425 if (*opts
& SWM_CW_RESIZABLE
)
5426 WIDTH(win
) -= b
->x
- X(win
) - BORDER(win
);
5428 X(win
) = b
->x
- BORDER(win
);
5431 if ((*opts
& SWM_CW_BOTTOM
) && MAX_Y(win
) + BORDER(win
) > b
->y
+ b
->h
) {
5432 if (*opts
& SWM_CW_RESIZABLE
)
5433 HEIGHT(win
) = b
->y
+ b
->h
- Y(win
) - BORDER(win
);
5435 Y(win
) = b
->y
+ b
->h
- HEIGHT(win
) - BORDER(win
);
5438 if ((*opts
& SWM_CW_TOP
) && Y(win
) + BORDER(win
) < b
->y
) {
5439 if (*opts
& SWM_CW_RESIZABLE
)
5440 HEIGHT(win
) -= b
->y
- Y(win
) - BORDER(win
);
5442 Y(win
) = b
->y
- BORDER(win
);
5445 if (*opts
& SWM_CW_RESIZABLE
) {
5448 if (HEIGHT(win
) < 1)
5454 update_window(struct ws_win
*win
)
5459 mask
= XCB_CONFIG_WINDOW_X
| XCB_CONFIG_WINDOW_Y
|
5460 XCB_CONFIG_WINDOW_WIDTH
| XCB_CONFIG_WINDOW_HEIGHT
|
5461 XCB_CONFIG_WINDOW_BORDER_WIDTH
;
5465 wc
[3] = HEIGHT(win
);
5466 wc
[4] = BORDER(win
);
5468 DNPRINTF(SWM_D_EVENT
, "update_window: window: 0x%x, (x,y) w x h: "
5469 "(%d,%d) %d x %d, bordered: %s\n", win
->id
, wc
[0], wc
[1], wc
[2],
5470 wc
[3], YESNO(win
->bordered
));
5472 xcb_configure_window(conn
, win
->id
, mask
, wc
);
5475 #define SWM_RESIZE_STEPS (50)
5478 resize(struct ws_win
*win
, union arg
*args
)
5480 xcb_timestamp_t timestamp
= 0;
5481 struct swm_region
*r
= NULL
;
5483 struct swm_geometry g
;
5484 int top
= 0, left
= 0, resizing
;
5486 xcb_cursor_t cursor
;
5487 xcb_query_pointer_reply_t
*xpr
= NULL
;
5488 xcb_generic_event_t
*evt
;
5489 xcb_motion_notify_event_t
*mne
;
5495 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
5498 DNPRINTF(SWM_D_EVENT
, "resize: window: 0x%x, floating: %s, "
5499 "transient: 0x%x\n", win
->id
, YESNO(win
->floating
),
5502 if (!win
->transient
&& !win
->floating
)
5505 /* reject resizes in max mode for floaters (transient ok) */
5506 if (win
->floatmaxed
)
5510 ewmh_update_win_state(win
, ewmh
[_SWM_WM_STATE_MANUAL
].atom
,
5517 /* It's possible for win to have been freed during focus_flush(). */
5518 if (validate_win(win
)) {
5519 DNPRINTF(SWM_D_EVENT
, "resize: invalid win.\n");
5524 case SWM_ARG_ID_WIDTHSHRINK
:
5525 WIDTH(win
) -= SWM_RESIZE_STEPS
;
5528 case SWM_ARG_ID_WIDTHGROW
:
5529 WIDTH(win
) += SWM_RESIZE_STEPS
;
5532 case SWM_ARG_ID_HEIGHTSHRINK
:
5533 HEIGHT(win
) -= SWM_RESIZE_STEPS
;
5536 case SWM_ARG_ID_HEIGHTGROW
:
5537 HEIGHT(win
) += SWM_RESIZE_STEPS
;
5544 region_containment(win
, r
, SWM_CW_ALLSIDES
| SWM_CW_RESIZABLE
|
5545 SWM_CW_HARDBOUNDARY
);
5547 store_float_geom(win
);
5551 region_containment(win
, r
, SWM_CW_ALLSIDES
| SWM_CW_RESIZABLE
|
5552 SWM_CW_SOFTBOUNDARY
);
5555 /* get cursor offset from window root */
5556 xpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
5563 if (xpr
->win_x
< WIDTH(win
) / 2)
5566 if (xpr
->win_y
< HEIGHT(win
) / 2)
5569 if (args
->id
== SWM_ARG_ID_CENTER
)
5570 cursor
= cursors
[XC_SIZING
].cid
;
5572 cursor
= cursors
[left
? XC_TOP_LEFT_CORNER
:
5573 XC_TOP_RIGHT_CORNER
].cid
;
5575 cursor
= cursors
[left
? XC_BOTTOM_LEFT_CORNER
:
5576 XC_BOTTOM_RIGHT_CORNER
].cid
;
5578 xcb_grab_pointer(conn
, 0, win
->id
, MOUSEMASK
,
5579 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
, XCB_WINDOW_NONE
, cursor
,
5584 while (resizing
&& (evt
= xcb_wait_for_event(conn
))) {
5585 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
5586 case XCB_BUTTON_RELEASE
:
5587 DNPRINTF(SWM_D_EVENT
, "resize: BUTTON_RELEASE\n");
5590 case XCB_MOTION_NOTIFY
:
5591 mne
= (xcb_motion_notify_event_t
*)evt
;
5592 /* cursor offset/delta from start of the operation */
5593 dx
= mne
->root_x
- xpr
->root_x
;
5594 dy
= mne
->root_y
- xpr
->root_y
;
5600 if (args
->id
== SWM_ARG_ID_CENTER
) {
5601 if (g
.h
/ 2 + dy
< 1)
5605 HEIGHT(win
) = g
.h
+ 2 * dy
;
5613 HEIGHT(win
) = g
.h
+ dy
;
5620 if (args
->id
== SWM_ARG_ID_CENTER
) {
5621 if (g
.w
/ 2 + dx
< 1)
5625 WIDTH(win
) = g
.w
+ 2 * dx
;
5633 WIDTH(win
) = g
.w
+ dx
;
5636 /* not free, don't sync more than 120 times / second */
5637 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
5638 timestamp
= mne
->time
;
5639 regionize(win
, mne
->root_x
, mne
->root_y
);
5640 region_containment(win
, r
, SWM_CW_ALLSIDES
|
5641 SWM_CW_RESIZABLE
| SWM_CW_HARDBOUNDARY
|
5642 SWM_CW_SOFTBOUNDARY
);
5650 /* It's possible for win to have been freed above. */
5651 if (validate_win(win
)) {
5652 DNPRINTF(SWM_D_EVENT
, "resize: invalid win.\n");
5660 region_containment(win
, r
, SWM_CW_ALLSIDES
| SWM_CW_RESIZABLE
|
5661 SWM_CW_HARDBOUNDARY
| SWM_CW_SOFTBOUNDARY
);
5665 store_float_geom(win
);
5667 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
5669 DNPRINTF(SWM_D_EVENT
, "resize: done.\n");
5673 resize_step(struct swm_region
*r
, union arg
*args
)
5675 struct ws_win
*win
= NULL
;
5677 if (r
&& r
->ws
&& r
->ws
->focus
)
5686 /* Try to set window region based on supplied coordinates or window center. */
5688 regionize(struct ws_win
*win
, int x
, int y
)
5690 struct swm_region
*r
= NULL
;
5692 r
= region_under(win
->s
, x
, y
);
5694 r
= region_under(win
->s
, X(win
) + WIDTH(win
) / 2,
5695 Y(win
) + HEIGHT(win
) / 2);
5697 if (r
&& r
!= win
->ws
->r
) {
5698 win_to_ws(win
, r
->ws
->idx
, 0);
5703 #define SWM_MOVE_STEPS (50)
5706 move(struct ws_win
*win
, union arg
*args
)
5708 xcb_timestamp_t timestamp
= 0;
5709 int move_stp
= 0, moving
;
5710 struct swm_region
*r
= NULL
;
5711 xcb_query_pointer_reply_t
*qpr
= NULL
;
5712 xcb_generic_event_t
*evt
;
5713 xcb_motion_notify_event_t
*mne
;
5719 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
5722 DNPRINTF(SWM_D_EVENT
, "move: window: 0x%x, floating: %s, transient: "
5723 "0x%x\n", win
->id
, YESNO(win
->floating
), win
->transient
);
5725 /* in max_stack mode should only move transients */
5726 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
] && !win
->transient
)
5731 ewmh_update_win_state(win
, ewmh
[_SWM_WM_STATE_MANUAL
].atom
,
5735 /* When a stacked win is moved, float it and restack. */
5736 if (!win
->floating
&& !win
->transient
) {
5737 store_float_geom(win
);
5738 ewmh_update_win_state(win
, ewmh
[_NET_WM_STATE_ABOVE
].atom
,
5745 /* It's possible for win to have been freed during focus_flush(). */
5746 if (validate_win(win
)) {
5747 DNPRINTF(SWM_D_EVENT
, "move: invalid win.\n");
5753 case SWM_ARG_ID_MOVELEFT
:
5754 X(win
) -= (SWM_MOVE_STEPS
- border_width
);
5757 case SWM_ARG_ID_MOVERIGHT
:
5758 X(win
) += (SWM_MOVE_STEPS
- border_width
);
5761 case SWM_ARG_ID_MOVEUP
:
5762 Y(win
) -= (SWM_MOVE_STEPS
- border_width
);
5765 case SWM_ARG_ID_MOVEDOWN
:
5766 Y(win
) += (SWM_MOVE_STEPS
- border_width
);
5773 regionize(win
, -1, -1);
5774 region_containment(win
, win
->ws
->r
, SWM_CW_ALLSIDES
);
5776 store_float_geom(win
);
5780 xcb_grab_pointer(conn
, 0, win
->id
, MOUSEMASK
,
5781 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
,
5782 XCB_WINDOW_NONE
, cursors
[XC_FLEUR
].cid
, XCB_CURRENT_TIME
);
5784 /* get cursor offset from window root */
5785 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
5788 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
5792 regionize(win
, qpr
->root_x
, qpr
->root_y
);
5793 region_containment(win
, win
->ws
->r
, SWM_CW_ALLSIDES
|
5794 SWM_CW_SOFTBOUNDARY
);
5798 while (moving
&& (evt
= xcb_wait_for_event(conn
))) {
5799 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
5800 case XCB_BUTTON_RELEASE
:
5801 DNPRINTF(SWM_D_EVENT
, "move: BUTTON_RELEASE\n");
5804 case XCB_MOTION_NOTIFY
:
5805 mne
= (xcb_motion_notify_event_t
*)evt
;
5806 DNPRINTF(SWM_D_EVENT
, "motion: root: 0x%x\n", mne
->root
);
5807 X(win
) = mne
->root_x
- qpr
->win_x
- border_width
;
5808 Y(win
) = mne
->root_y
- qpr
->win_y
- border_width
;
5810 /* not free, don't sync more than 120 times / second */
5811 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
5812 timestamp
= mne
->time
;
5813 regionize(win
, mne
->root_x
, mne
->root_y
);
5814 region_containment(win
, win
->ws
->r
,
5815 SWM_CW_ALLSIDES
| SWM_CW_SOFTBOUNDARY
);
5823 /* It's possible for win to have been freed above. */
5824 if (validate_win(win
)) {
5825 DNPRINTF(SWM_D_EVENT
, "move: invalid win.\n");
5833 region_containment(win
, win
->ws
->r
, SWM_CW_ALLSIDES
|
5834 SWM_CW_SOFTBOUNDARY
);
5838 store_float_geom(win
);
5841 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
5842 DNPRINTF(SWM_D_EVENT
, "move: done.\n");
5846 move_step(struct swm_region
*r
, union arg
*args
)
5848 struct ws_win
*win
= NULL
;
5850 if (r
&& r
->ws
&& r
->ws
->focus
)
5855 if (!win
->transient
&& !win
->floating
)
5862 /* key definitions */
5864 char name
[SWM_FUNCNAME_LEN
];
5865 void (*func
)(struct swm_region
*r
, union arg
*);
5867 } keyfuncs
[KF_INVALID
+ 1] = {
5868 /* name function argument */
5869 { "bar_toggle", bar_toggle
, {.id
= SWM_ARG_ID_BAR_TOGGLE
} },
5870 { "bar_toggle_ws", bar_toggle
, {.id
= SWM_ARG_ID_BAR_TOGGLE_WS
} },
5871 { "button2", pressbutton
, {2} },
5872 { "cycle_layout", cycle_layout
, {0} },
5873 { "flip_layout", stack_config
, {.id
= SWM_ARG_ID_FLIPLAYOUT
} },
5874 { "float_toggle", floating_toggle
,{0} },
5875 { "focus_main", focus
, {.id
= SWM_ARG_ID_FOCUSMAIN
} },
5876 { "focus_next", focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
5877 { "focus_prev", focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
5878 { "height_grow", resize_step
, {.id
= SWM_ARG_ID_HEIGHTGROW
} },
5879 { "height_shrink", resize_step
, {.id
= SWM_ARG_ID_HEIGHTSHRINK
} },
5880 { "iconify", iconify
, {0} },
5881 { "master_shrink", stack_config
, {.id
= SWM_ARG_ID_MASTERSHRINK
} },
5882 { "master_grow", stack_config
, {.id
= SWM_ARG_ID_MASTERGROW
} },
5883 { "master_add", stack_config
, {.id
= SWM_ARG_ID_MASTERADD
} },
5884 { "master_del", stack_config
, {.id
= SWM_ARG_ID_MASTERDEL
} },
5885 { "move_down", move_step
, {.id
= SWM_ARG_ID_MOVEDOWN
} },
5886 { "move_left", move_step
, {.id
= SWM_ARG_ID_MOVELEFT
} },
5887 { "move_right", move_step
, {.id
= SWM_ARG_ID_MOVERIGHT
} },
5888 { "move_up", move_step
, {.id
= SWM_ARG_ID_MOVEUP
} },
5889 { "mvrg_1", send_to_rg
, {.id
= 0} },
5890 { "mvrg_2", send_to_rg
, {.id
= 1} },
5891 { "mvrg_3", send_to_rg
, {.id
= 2} },
5892 { "mvrg_4", send_to_rg
, {.id
= 3} },
5893 { "mvrg_5", send_to_rg
, {.id
= 4} },
5894 { "mvrg_6", send_to_rg
, {.id
= 5} },
5895 { "mvrg_7", send_to_rg
, {.id
= 6} },
5896 { "mvrg_8", send_to_rg
, {.id
= 7} },
5897 { "mvrg_9", send_to_rg
, {.id
= 8} },
5898 { "mvws_1", send_to_ws
, {.id
= 0} },
5899 { "mvws_2", send_to_ws
, {.id
= 1} },
5900 { "mvws_3", send_to_ws
, {.id
= 2} },
5901 { "mvws_4", send_to_ws
, {.id
= 3} },
5902 { "mvws_5", send_to_ws
, {.id
= 4} },
5903 { "mvws_6", send_to_ws
, {.id
= 5} },
5904 { "mvws_7", send_to_ws
, {.id
= 6} },
5905 { "mvws_8", send_to_ws
, {.id
= 7} },
5906 { "mvws_9", send_to_ws
, {.id
= 8} },
5907 { "mvws_10", send_to_ws
, {.id
= 9} },
5908 { "mvws_11", send_to_ws
, {.id
= 10} },
5909 { "mvws_12", send_to_ws
, {.id
= 11} },
5910 { "mvws_13", send_to_ws
, {.id
= 12} },
5911 { "mvws_14", send_to_ws
, {.id
= 13} },
5912 { "mvws_15", send_to_ws
, {.id
= 14} },
5913 { "mvws_16", send_to_ws
, {.id
= 15} },
5914 { "mvws_17", send_to_ws
, {.id
= 16} },
5915 { "mvws_18", send_to_ws
, {.id
= 17} },
5916 { "mvws_19", send_to_ws
, {.id
= 18} },
5917 { "mvws_20", send_to_ws
, {.id
= 19} },
5918 { "mvws_21", send_to_ws
, {.id
= 20} },
5919 { "mvws_22", send_to_ws
, {.id
= 21} },
5920 { "name_workspace", name_workspace
, {0} },
5921 { "quit", quit
, {0} },
5922 { "raise_toggle", raise_toggle
, {0} },
5923 { "restart", restart
, {0} },
5924 { "rg_1", focusrg
, {.id
= 0} },
5925 { "rg_2", focusrg
, {.id
= 1} },
5926 { "rg_3", focusrg
, {.id
= 2} },
5927 { "rg_4", focusrg
, {.id
= 3} },
5928 { "rg_5", focusrg
, {.id
= 4} },
5929 { "rg_6", focusrg
, {.id
= 5} },
5930 { "rg_7", focusrg
, {.id
= 6} },
5931 { "rg_8", focusrg
, {.id
= 7} },
5932 { "rg_9", focusrg
, {.id
= 8} },
5933 { "rg_next", cyclerg
, {.id
= SWM_ARG_ID_CYCLERG_UP
} },
5934 { "rg_prev", cyclerg
, {.id
= SWM_ARG_ID_CYCLERG_DOWN
} },
5935 { "screen_next", cyclerg
, {.id
= SWM_ARG_ID_CYCLERG_UP
} },
5936 { "screen_prev", cyclerg
, {.id
= SWM_ARG_ID_CYCLERG_DOWN
} },
5937 { "search_win", search_win
, {0} },
5938 { "search_workspace", search_workspace
, {0} },
5939 { "spawn_custom", NULL
, {0} },
5940 { "stack_inc", stack_config
, {.id
= SWM_ARG_ID_STACKINC
} },
5941 { "stack_dec", stack_config
, {.id
= SWM_ARG_ID_STACKDEC
} },
5942 { "stack_reset", stack_config
, {.id
= SWM_ARG_ID_STACKRESET
} },
5943 { "swap_main", swapwin
, {.id
= SWM_ARG_ID_SWAPMAIN
} },
5944 { "swap_next", swapwin
, {.id
= SWM_ARG_ID_SWAPNEXT
} },
5945 { "swap_prev", swapwin
, {.id
= SWM_ARG_ID_SWAPPREV
} },
5946 { "uniconify", uniconify
, {0} },
5947 { "version", version
, {0} },
5948 { "width_grow", resize_step
, {.id
= SWM_ARG_ID_WIDTHGROW
} },
5949 { "width_shrink", resize_step
, {.id
= SWM_ARG_ID_WIDTHSHRINK
} },
5950 { "wind_del", wkill
, {.id
= SWM_ARG_ID_DELETEWINDOW
} },
5951 { "wind_kill", wkill
, {.id
= SWM_ARG_ID_KILLWINDOW
} },
5952 { "ws_1", switchws
, {.id
= 0} },
5953 { "ws_2", switchws
, {.id
= 1} },
5954 { "ws_3", switchws
, {.id
= 2} },
5955 { "ws_4", switchws
, {.id
= 3} },
5956 { "ws_5", switchws
, {.id
= 4} },
5957 { "ws_6", switchws
, {.id
= 5} },
5958 { "ws_7", switchws
, {.id
= 6} },
5959 { "ws_8", switchws
, {.id
= 7} },
5960 { "ws_9", switchws
, {.id
= 8} },
5961 { "ws_10", switchws
, {.id
= 9} },
5962 { "ws_11", switchws
, {.id
= 10} },
5963 { "ws_12", switchws
, {.id
= 11} },
5964 { "ws_13", switchws
, {.id
= 12} },
5965 { "ws_14", switchws
, {.id
= 13} },
5966 { "ws_15", switchws
, {.id
= 14} },
5967 { "ws_16", switchws
, {.id
= 15} },
5968 { "ws_17", switchws
, {.id
= 16} },
5969 { "ws_18", switchws
, {.id
= 17} },
5970 { "ws_19", switchws
, {.id
= 18} },
5971 { "ws_20", switchws
, {.id
= 19} },
5972 { "ws_21", switchws
, {.id
= 20} },
5973 { "ws_22", switchws
, {.id
= 21} },
5974 { "ws_next", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP
} },
5975 { "ws_next_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP_ALL
} },
5976 { "ws_next_move", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_MOVE_UP
} },
5977 { "ws_prev", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN
} },
5978 { "ws_prev_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN_ALL
} },
5979 { "ws_prev_move", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_MOVE_DOWN
} },
5980 { "ws_prior", priorws
, {0} },
5981 { "dumpwins", dumpwins
, {0} }, /* MUST BE LAST */
5982 { "invalid key func", NULL
, {0} },
5986 key_cmp(struct key
*kp1
, struct key
*kp2
)
5988 if (kp1
->keysym
< kp2
->keysym
)
5990 if (kp1
->keysym
> kp2
->keysym
)
5993 if (kp1
->mod
< kp2
->mod
)
5995 if (kp1
->mod
> kp2
->mod
)
6002 enum { client_click
, root_click
};
6004 unsigned int action
;
6006 unsigned int button
;
6007 void (*func
)(struct ws_win
*, union arg
*);
6010 #define MODKEY_SHIFT MODKEY | XCB_MOD_MASK_SHIFT
6011 /* action key mouse button func args */
6012 { client_click
, MODKEY
, XCB_BUTTON_INDEX_3
, resize
, {.id
= SWM_ARG_ID_DONTCENTER
} },
6013 { client_click
, MODKEY_SHIFT
, XCB_BUTTON_INDEX_3
, resize
, {.id
= SWM_ARG_ID_CENTER
} },
6014 { client_click
, MODKEY
, XCB_BUTTON_INDEX_1
, move
, {0} },
6019 update_modkey(unsigned int mod
)
6025 RB_FOREACH(kp
, key_tree
, &keys
)
6026 if (kp
->mod
& XCB_MOD_MASK_SHIFT
)
6027 kp
->mod
= mod
| XCB_MOD_MASK_SHIFT
;
6031 for (i
= 0; i
< LENGTH(buttons
); i
++)
6032 if (buttons
[i
].mask
& XCB_MOD_MASK_SHIFT
)
6033 buttons
[i
].mask
= mod
| XCB_MOD_MASK_SHIFT
;
6035 buttons
[i
].mask
= mod
;
6039 spawn_expand(struct swm_region
*r
, union arg
*args
, const char *spawn_name
,
6042 struct spawn_prog
*prog
= NULL
;
6044 char *ap
, **real_args
;
6046 /* suppress unused warning since var is needed */
6049 DNPRINTF(SWM_D_SPAWN
, "spawn_expand: %s\n", spawn_name
);
6052 TAILQ_FOREACH(prog
, &spawns
, entry
) {
6053 if (!strcasecmp(spawn_name
, prog
->name
))
6057 warnx("spawn_custom: program %s not found", spawn_name
);
6061 /* make room for expanded args */
6062 if ((real_args
= calloc(prog
->argc
+ 1, sizeof(char *))) == NULL
)
6063 err(1, "spawn_custom: calloc real_args");
6065 /* expand spawn_args into real_args */
6066 for (i
= 0; i
< prog
->argc
; i
++) {
6068 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: raw arg: %s\n", ap
);
6069 if (!strcasecmp(ap
, "$bar_border")) {
6071 strdup(r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].name
))
6073 err(1, "spawn_custom border color");
6074 } else if (!strcasecmp(ap
, "$bar_color")) {
6076 strdup(r
->s
->c
[SWM_S_COLOR_BAR
].name
))
6078 err(1, "spawn_custom bar color");
6079 } else if (!strcasecmp(ap
, "$bar_font")) {
6080 if ((real_args
[i
] = strdup(bar_fonts
))
6082 err(1, "spawn_custom bar fonts");
6083 } else if (!strcasecmp(ap
, "$bar_font_color")) {
6085 strdup(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].name
))
6087 err(1, "spawn_custom color font");
6088 } else if (!strcasecmp(ap
, "$color_focus")) {
6090 strdup(r
->s
->c
[SWM_S_COLOR_FOCUS
].name
))
6092 err(1, "spawn_custom color focus");
6093 } else if (!strcasecmp(ap
, "$color_unfocus")) {
6095 strdup(r
->s
->c
[SWM_S_COLOR_UNFOCUS
].name
))
6097 err(1, "spawn_custom color unfocus");
6098 } else if (!strcasecmp(ap
, "$region_index")) {
6099 if (asprintf(&real_args
[i
], "%d",
6100 get_region_index(r
) + 1) < 1)
6101 err(1, "spawn_custom region index");
6102 } else if (!strcasecmp(ap
, "$workspace_index")) {
6103 if (asprintf(&real_args
[i
], "%d", r
->ws
->idx
+ 1) < 1)
6104 err(1, "spawn_custom workspace index");
6106 /* no match --> copy as is */
6107 if ((real_args
[i
] = strdup(ap
)) == NULL
)
6108 err(1, "spawn_custom strdup(ap)");
6110 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: cooked arg: %s\n",
6115 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: result: ");
6116 for (i
= 0; i
< prog
->argc
; i
++)
6117 DNPRINTF(SWM_D_SPAWN
, "\"%s\" ", real_args
[i
]);
6118 DNPRINTF(SWM_D_SPAWN
, "\n");
6120 *ret_args
= real_args
;
6121 return (prog
->argc
);
6125 spawn_custom(struct swm_region
*r
, union arg
*args
, const char *spawn_name
)
6131 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
6135 spawn(r
->ws
->idx
, &a
, 1);
6137 for (i
= 0; i
< spawn_argc
; i
++)
6143 spawn_select(struct swm_region
*r
, union arg
*args
, const char *spawn_name
,
6150 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
6154 if (pipe(select_list_pipe
) == -1)
6155 err(1, "pipe error");
6156 if (pipe(select_resp_pipe
) == -1)
6157 err(1, "pipe error");
6159 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
6160 err(1, "could not disable SIGPIPE");
6161 switch (*pid
= fork()) {
6163 err(1, "cannot fork");
6166 if (dup2(select_list_pipe
[0], STDIN_FILENO
) == -1)
6168 if (dup2(select_resp_pipe
[1], STDOUT_FILENO
) == -1)
6170 close(select_list_pipe
[1]);
6171 close(select_resp_pipe
[0]);
6172 spawn(r
->ws
->idx
, &a
, 0);
6174 default: /* parent */
6175 close(select_list_pipe
[0]);
6176 close(select_resp_pipe
[1]);
6180 for (i
= 0; i
< spawn_argc
; i
++)
6186 spawn_insert(const char *name
, const char *args
, int flags
)
6188 char *arg
, *cp
, *ptr
;
6189 struct spawn_prog
*sp
;
6191 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: %s\n", name
);
6193 if ((sp
= calloc(1, sizeof *sp
)) == NULL
)
6194 err(1, "spawn_insert: calloc");
6195 if ((sp
->name
= strdup(name
)) == NULL
)
6196 err(1, "spawn_insert: strdup");
6198 /* convert the arguments to an argument list */
6199 if ((ptr
= cp
= strdup(args
)) == NULL
)
6200 err(1, "spawn_insert: strdup");
6201 while ((arg
= strsep(&ptr
, " \t")) != NULL
) {
6202 /* empty field; skip it */
6207 if ((sp
->argv
= realloc(sp
->argv
, sp
->argc
*
6208 sizeof *sp
->argv
)) == NULL
)
6209 err(1, "spawn_insert: realloc");
6210 if ((sp
->argv
[sp
->argc
- 1] = strdup(arg
)) == NULL
)
6211 err(1, "spawn_insert: strdup");
6217 TAILQ_INSERT_TAIL(&spawns
, sp
, entry
);
6218 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: leave\n");
6222 spawn_remove(struct spawn_prog
*sp
)
6226 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: %s\n", sp
->name
);
6228 TAILQ_REMOVE(&spawns
, sp
, entry
);
6229 for (i
= 0; i
< sp
->argc
; i
++)
6235 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: leave\n");
6239 setspawn(const char *name
, const char *args
, int flags
)
6241 struct spawn_prog
*sp
;
6243 DNPRINTF(SWM_D_SPAWN
, "setspawn: %s\n", name
);
6248 /* Remove any old spawn under the same name. */
6249 TAILQ_FOREACH(sp
, &spawns
, entry
)
6250 if (!strcmp(sp
->name
, name
)) {
6256 spawn_insert(name
, args
, flags
);
6258 warnx("error: setspawn: cannot find program: %s", name
);
6260 DNPRINTF(SWM_D_SPAWN
, "setspawn: leave\n");
6264 setconfspawn(char *selector
, char *value
, int flags
)
6268 args
= expand_tilde(value
);
6270 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: [%s] [%s]\n", selector
, args
);
6272 setspawn(selector
, args
, flags
);
6275 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: done.\n");
6280 validate_spawns(void)
6282 struct spawn_prog
*sp
;
6283 char which
[PATH_MAX
];
6288 RB_FOREACH(kp
, key_tree
, &keys
) {
6289 if (kp
->funcid
!= KF_SPAWN_CUSTOM
)
6293 TAILQ_FOREACH(sp
, &spawns
, entry
) {
6294 if (!strcasecmp(kp
->spawn_name
, sp
->name
))
6298 if (sp
== NULL
|| sp
->flags
& SWM_SPAWN_OPTIONAL
)
6301 /* verify we have the goods */
6302 snprintf(which
, sizeof which
, "which %s", sp
->argv
[0]);
6303 DNPRINTF(SWM_D_CONF
, "validate_spawns: which %s\n",
6305 for (i
= strlen("which "); i
< strlen(which
); i
++)
6306 if (which
[i
] == ' ') {
6310 if (system(which
) != 0)
6311 add_startup_exception("could not find %s",
6312 &which
[strlen("which ")]);
6319 setconfspawn("lock", "xlock", 0);
6321 setconfspawn("term", "xterm", 0);
6322 setconfspawn("spawn_term", "xterm", 0);
6324 setconfspawn("menu", "dmenu_run"
6327 " -nf $bar_font_color"
6329 " -sf $bar_color", 0);
6331 setconfspawn("search", "dmenu"
6335 " -nf $bar_font_color"
6337 " -sf $bar_color", 0);
6339 setconfspawn("name_workspace", "dmenu"
6343 " -nf $bar_font_color"
6345 " -sf $bar_color", 0);
6347 /* These are not verified for existence, even with a binding set. */
6348 setconfspawn("screenshot_all", "screenshot.sh full", SWM_SPAWN_OPTIONAL
);
6349 setconfspawn("screenshot_wind", "screenshot.sh window", SWM_SPAWN_OPTIONAL
);
6350 setconfspawn("initscr", "initscreen.sh", SWM_SPAWN_OPTIONAL
);
6354 #define SWM_MODNAME_SIZE 32
6355 #define SWM_KEY_WS "\n+ \t"
6357 parsekeys(char *keystr
, unsigned int currmod
, unsigned int *mod
, KeySym
*ks
)
6361 DNPRINTF(SWM_D_KEY
, "parsekeys: enter [%s]\n", keystr
);
6362 if (mod
== NULL
|| ks
== NULL
) {
6363 DNPRINTF(SWM_D_KEY
, "parsekeys: no mod or key vars\n");
6366 if (keystr
== NULL
|| strlen(keystr
) == 0) {
6367 DNPRINTF(SWM_D_KEY
, "parsekeys: no keystr\n");
6371 *ks
= XCB_NO_SYMBOL
;
6373 while ((name
= strsep(&cp
, SWM_KEY_WS
)) != NULL
) {
6374 DNPRINTF(SWM_D_KEY
, "parsekeys: key [%s]\n", name
);
6376 cp
+= (long)strspn(cp
, SWM_KEY_WS
);
6377 if (strncasecmp(name
, "MOD", SWM_MODNAME_SIZE
) == 0)
6379 else if (!strncasecmp(name
, "Mod1", SWM_MODNAME_SIZE
))
6380 *mod
|= XCB_MOD_MASK_1
;
6381 else if (!strncasecmp(name
, "Mod2", SWM_MODNAME_SIZE
))
6382 *mod
+= XCB_MOD_MASK_2
;
6383 else if (!strncmp(name
, "Mod3", SWM_MODNAME_SIZE
))
6384 *mod
|= XCB_MOD_MASK_3
;
6385 else if (!strncmp(name
, "Mod4", SWM_MODNAME_SIZE
))
6386 *mod
|= XCB_MOD_MASK_4
;
6387 else if (strncasecmp(name
, "SHIFT", SWM_MODNAME_SIZE
) == 0)
6388 *mod
|= XCB_MOD_MASK_SHIFT
;
6389 else if (strncasecmp(name
, "CONTROL", SWM_MODNAME_SIZE
) == 0)
6390 *mod
|= XCB_MOD_MASK_CONTROL
;
6392 *ks
= XStringToKeysym(name
);
6393 XConvertCase(*ks
, ks
, &uks
);
6394 if (ks
== XCB_NO_SYMBOL
) {
6396 "parsekeys: invalid key %s\n",
6402 DNPRINTF(SWM_D_KEY
, "parsekeys: leave ok\n");
6407 strdupsafe(const char *str
)
6412 return (strdup(str
));
6416 key_insert(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
6417 const char *spawn_name
)
6421 DNPRINTF(SWM_D_KEY
, "key_insert: enter %s [%s]\n",
6422 keyfuncs
[kfid
].name
, spawn_name
);
6424 if ((kp
= malloc(sizeof *kp
)) == NULL
)
6425 err(1, "key_insert: malloc");
6430 kp
->spawn_name
= strdupsafe(spawn_name
);
6431 RB_INSERT(key_tree
, &keys
, kp
);
6433 DNPRINTF(SWM_D_KEY
, "key_insert: leave\n");
6437 key_lookup(unsigned int mod
, KeySym ks
)
6444 return (RB_FIND(key_tree
, &keys
, &kp
));
6448 key_remove(struct key
*kp
)
6450 DNPRINTF(SWM_D_KEY
, "key_remove: %s\n", keyfuncs
[kp
->funcid
].name
);
6452 RB_REMOVE(key_tree
, &keys
, kp
);
6453 free(kp
->spawn_name
);
6456 DNPRINTF(SWM_D_KEY
, "key_remove: leave\n");
6460 key_replace(struct key
*kp
, unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
6461 const char *spawn_name
)
6463 DNPRINTF(SWM_D_KEY
, "key_replace: %s [%s]\n", keyfuncs
[kp
->funcid
].name
,
6467 key_insert(mod
, ks
, kfid
, spawn_name
);
6469 DNPRINTF(SWM_D_KEY
, "key_replace: leave\n");
6473 setkeybinding(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
6474 const char *spawn_name
)
6478 DNPRINTF(SWM_D_KEY
, "setkeybinding: enter %s [%s]\n",
6479 keyfuncs
[kfid
].name
, spawn_name
);
6481 if ((kp
= key_lookup(mod
, ks
)) != NULL
) {
6482 if (kfid
== KF_INVALID
)
6485 key_replace(kp
, mod
, ks
, kfid
, spawn_name
);
6486 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
6489 if (kfid
== KF_INVALID
) {
6490 warnx("bind: Key combination already unbound.");
6491 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
6495 key_insert(mod
, ks
, kfid
, spawn_name
);
6496 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
6500 setconfbinding(char *selector
, char *value
, int flags
)
6502 enum keyfuncid kfid
;
6505 struct spawn_prog
*sp
;
6507 /* suppress unused warning since var is needed */
6510 DNPRINTF(SWM_D_KEY
, "setconfbinding: enter\n");
6511 if (selector
== NULL
) {
6512 DNPRINTF(SWM_D_KEY
, "setconfbinding: unbind %s\n", value
);
6513 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
6515 setkeybinding(mod
, ks
, kfid
, NULL
);
6520 /* search by key function name */
6521 for (kfid
= 0; kfid
< KF_INVALID
; (kfid
)++) {
6522 if (strncasecmp(selector
, keyfuncs
[kfid
].name
,
6523 SWM_FUNCNAME_LEN
) == 0) {
6524 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match "
6525 "keyfunc\n", selector
);
6526 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
6527 setkeybinding(mod
, ks
, kfid
, NULL
);
6533 /* search by custom spawn name */
6534 TAILQ_FOREACH(sp
, &spawns
, entry
) {
6535 if (strcasecmp(selector
, sp
->name
) == 0) {
6536 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match "
6537 "spawn\n", selector
);
6538 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
6539 setkeybinding(mod
, ks
, KF_SPAWN_CUSTOM
,
6546 DNPRINTF(SWM_D_KEY
, "setconfbinding: no match\n");
6553 #define MODKEY_SHIFT MODKEY | XCB_MOD_MASK_SHIFT
6554 setkeybinding(MODKEY
, XK_b
, KF_BAR_TOGGLE
, NULL
);
6555 setkeybinding(MODKEY_SHIFT
, XK_b
, KF_BAR_TOGGLE_WS
,NULL
);
6556 setkeybinding(MODKEY
, XK_v
, KF_BUTTON2
, NULL
);
6557 setkeybinding(MODKEY
, XK_space
, KF_CYCLE_LAYOUT
,NULL
);
6558 setkeybinding(MODKEY_SHIFT
, XK_backslash
, KF_FLIP_LAYOUT
, NULL
);
6559 setkeybinding(MODKEY
, XK_t
, KF_FLOAT_TOGGLE
,NULL
);
6560 setkeybinding(MODKEY
, XK_m
, KF_FOCUS_MAIN
, NULL
);
6561 setkeybinding(MODKEY
, XK_j
, KF_FOCUS_NEXT
, NULL
);
6562 setkeybinding(MODKEY
, XK_Tab
, KF_FOCUS_NEXT
, NULL
);
6563 setkeybinding(MODKEY
, XK_k
, KF_FOCUS_PREV
, NULL
);
6564 setkeybinding(MODKEY_SHIFT
, XK_Tab
, KF_FOCUS_PREV
, NULL
);
6565 setkeybinding(MODKEY_SHIFT
, XK_equal
, KF_HEIGHT_GROW
,NULL
);
6566 setkeybinding(MODKEY_SHIFT
, XK_minus
, KF_HEIGHT_SHRINK
,NULL
);
6567 setkeybinding(MODKEY
, XK_w
, KF_ICONIFY
, NULL
);
6568 setkeybinding(MODKEY
, XK_h
, KF_MASTER_SHRINK
, NULL
);
6569 setkeybinding(MODKEY
, XK_l
, KF_MASTER_GROW
, NULL
);
6570 setkeybinding(MODKEY
, XK_comma
, KF_MASTER_ADD
, NULL
);
6571 setkeybinding(MODKEY
, XK_period
, KF_MASTER_DEL
, NULL
);
6572 setkeybinding(MODKEY_SHIFT
, XK_bracketright
,KF_MOVE_DOWN
,NULL
);
6573 setkeybinding(MODKEY
, XK_bracketleft
, KF_MOVE_LEFT
,NULL
);
6574 setkeybinding(MODKEY
, XK_bracketright
,KF_MOVE_RIGHT
,NULL
);
6575 setkeybinding(MODKEY_SHIFT
, XK_bracketleft
, KF_MOVE_UP
, NULL
);
6576 setkeybinding(MODKEY_SHIFT
, XK_KP_End
, KF_MVRG_1
, NULL
);
6577 setkeybinding(MODKEY_SHIFT
, XK_KP_Down
, KF_MVRG_2
, NULL
);
6578 setkeybinding(MODKEY_SHIFT
, XK_KP_Next
, KF_MVRG_3
, NULL
);
6579 setkeybinding(MODKEY_SHIFT
, XK_KP_Left
, KF_MVRG_4
, NULL
);
6580 setkeybinding(MODKEY_SHIFT
, XK_KP_Begin
, KF_MVRG_5
, NULL
);
6581 setkeybinding(MODKEY_SHIFT
, XK_KP_Right
, KF_MVRG_6
, NULL
);
6582 setkeybinding(MODKEY_SHIFT
, XK_KP_Home
, KF_MVRG_7
, NULL
);
6583 setkeybinding(MODKEY_SHIFT
, XK_KP_Up
, KF_MVRG_8
, NULL
);
6584 setkeybinding(MODKEY_SHIFT
, XK_KP_Prior
, KF_MVRG_9
, NULL
);
6585 setkeybinding(MODKEY_SHIFT
, XK_1
, KF_MVWS_1
, NULL
);
6586 setkeybinding(MODKEY_SHIFT
, XK_2
, KF_MVWS_2
, NULL
);
6587 setkeybinding(MODKEY_SHIFT
, XK_3
, KF_MVWS_3
, NULL
);
6588 setkeybinding(MODKEY_SHIFT
, XK_4
, KF_MVWS_4
, NULL
);
6589 setkeybinding(MODKEY_SHIFT
, XK_5
, KF_MVWS_5
, NULL
);
6590 setkeybinding(MODKEY_SHIFT
, XK_6
, KF_MVWS_6
, NULL
);
6591 setkeybinding(MODKEY_SHIFT
, XK_7
, KF_MVWS_7
, NULL
);
6592 setkeybinding(MODKEY_SHIFT
, XK_8
, KF_MVWS_8
, NULL
);
6593 setkeybinding(MODKEY_SHIFT
, XK_9
, KF_MVWS_9
, NULL
);
6594 setkeybinding(MODKEY_SHIFT
, XK_0
, KF_MVWS_10
, NULL
);
6595 setkeybinding(MODKEY_SHIFT
, XK_F1
, KF_MVWS_11
, NULL
);
6596 setkeybinding(MODKEY_SHIFT
, XK_F2
, KF_MVWS_12
, NULL
);
6597 setkeybinding(MODKEY_SHIFT
, XK_F3
, KF_MVWS_13
, NULL
);
6598 setkeybinding(MODKEY_SHIFT
, XK_F4
, KF_MVWS_14
, NULL
);
6599 setkeybinding(MODKEY_SHIFT
, XK_F5
, KF_MVWS_15
, NULL
);
6600 setkeybinding(MODKEY_SHIFT
, XK_F6
, KF_MVWS_16
, NULL
);
6601 setkeybinding(MODKEY_SHIFT
, XK_F7
, KF_MVWS_17
, NULL
);
6602 setkeybinding(MODKEY_SHIFT
, XK_F8
, KF_MVWS_18
, NULL
);
6603 setkeybinding(MODKEY_SHIFT
, XK_F9
, KF_MVWS_19
, NULL
);
6604 setkeybinding(MODKEY_SHIFT
, XK_F10
, KF_MVWS_20
, NULL
);
6605 setkeybinding(MODKEY_SHIFT
, XK_F11
, KF_MVWS_21
, NULL
);
6606 setkeybinding(MODKEY_SHIFT
, XK_F12
, KF_MVWS_22
, NULL
);
6607 setkeybinding(MODKEY_SHIFT
, XK_slash
, KF_NAME_WORKSPACE
,NULL
);
6608 setkeybinding(MODKEY_SHIFT
, XK_q
, KF_QUIT
, NULL
);
6609 setkeybinding(MODKEY_SHIFT
, XK_r
, KF_RAISE_TOGGLE
,NULL
);
6610 setkeybinding(MODKEY
, XK_q
, KF_RESTART
, NULL
);
6611 setkeybinding(MODKEY
, XK_KP_End
, KF_RG_1
, NULL
);
6612 setkeybinding(MODKEY
, XK_KP_Down
, KF_RG_2
, NULL
);
6613 setkeybinding(MODKEY
, XK_KP_Next
, KF_RG_3
, NULL
);
6614 setkeybinding(MODKEY
, XK_KP_Left
, KF_RG_4
, NULL
);
6615 setkeybinding(MODKEY
, XK_KP_Begin
, KF_RG_5
, NULL
);
6616 setkeybinding(MODKEY
, XK_KP_Right
, KF_RG_6
, NULL
);
6617 setkeybinding(MODKEY
, XK_KP_Home
, KF_RG_7
, NULL
);
6618 setkeybinding(MODKEY
, XK_KP_Up
, KF_RG_8
, NULL
);
6619 setkeybinding(MODKEY
, XK_KP_Prior
, KF_RG_9
, NULL
);
6620 setkeybinding(MODKEY_SHIFT
, XK_Right
, KF_RG_NEXT
, NULL
);
6621 setkeybinding(MODKEY_SHIFT
, XK_Left
, KF_RG_PREV
, NULL
);
6622 setkeybinding(MODKEY
, XK_f
, KF_SEARCH_WIN
, NULL
);
6623 setkeybinding(MODKEY
, XK_slash
, KF_SEARCH_WORKSPACE
,NULL
);
6624 setkeybinding(MODKEY_SHIFT
, XK_i
, KF_SPAWN_CUSTOM
,"initscr");
6625 setkeybinding(MODKEY_SHIFT
, XK_Delete
, KF_SPAWN_CUSTOM
,"lock");
6626 setkeybinding(MODKEY
, XK_p
, KF_SPAWN_CUSTOM
,"menu");
6627 setkeybinding(MODKEY
, XK_s
, KF_SPAWN_CUSTOM
,"screenshot_all");
6628 setkeybinding(MODKEY_SHIFT
, XK_s
, KF_SPAWN_CUSTOM
,"screenshot_wind");
6629 setkeybinding(MODKEY_SHIFT
, XK_Return
, KF_SPAWN_CUSTOM
,"term");
6630 setkeybinding(MODKEY_SHIFT
, XK_comma
, KF_STACK_INC
, NULL
);
6631 setkeybinding(MODKEY_SHIFT
, XK_period
, KF_STACK_DEC
, NULL
);
6632 setkeybinding(MODKEY_SHIFT
, XK_space
, KF_STACK_RESET
, NULL
);
6633 setkeybinding(MODKEY
, XK_Return
, KF_SWAP_MAIN
, NULL
);
6634 setkeybinding(MODKEY_SHIFT
, XK_j
, KF_SWAP_NEXT
, NULL
);
6635 setkeybinding(MODKEY_SHIFT
, XK_k
, KF_SWAP_PREV
, NULL
);
6636 setkeybinding(MODKEY_SHIFT
, XK_w
, KF_UNICONIFY
, NULL
);
6637 setkeybinding(MODKEY_SHIFT
, XK_v
, KF_VERSION
, NULL
);
6638 setkeybinding(MODKEY
, XK_equal
, KF_WIDTH_GROW
, NULL
);
6639 setkeybinding(MODKEY
, XK_minus
, KF_WIDTH_SHRINK
,NULL
);
6640 setkeybinding(MODKEY
, XK_x
, KF_WIND_DEL
, NULL
);
6641 setkeybinding(MODKEY_SHIFT
, XK_x
, KF_WIND_KILL
, NULL
);
6642 setkeybinding(MODKEY
, XK_1
, KF_WS_1
, NULL
);
6643 setkeybinding(MODKEY
, XK_2
, KF_WS_2
, NULL
);
6644 setkeybinding(MODKEY
, XK_3
, KF_WS_3
, NULL
);
6645 setkeybinding(MODKEY
, XK_4
, KF_WS_4
, NULL
);
6646 setkeybinding(MODKEY
, XK_5
, KF_WS_5
, NULL
);
6647 setkeybinding(MODKEY
, XK_6
, KF_WS_6
, NULL
);
6648 setkeybinding(MODKEY
, XK_7
, KF_WS_7
, NULL
);
6649 setkeybinding(MODKEY
, XK_8
, KF_WS_8
, NULL
);
6650 setkeybinding(MODKEY
, XK_9
, KF_WS_9
, NULL
);
6651 setkeybinding(MODKEY
, XK_0
, KF_WS_10
, NULL
);
6652 setkeybinding(MODKEY
, XK_F1
, KF_WS_11
, NULL
);
6653 setkeybinding(MODKEY
, XK_F2
, KF_WS_12
, NULL
);
6654 setkeybinding(MODKEY
, XK_F3
, KF_WS_13
, NULL
);
6655 setkeybinding(MODKEY
, XK_F4
, KF_WS_14
, NULL
);
6656 setkeybinding(MODKEY
, XK_F5
, KF_WS_15
, NULL
);
6657 setkeybinding(MODKEY
, XK_F6
, KF_WS_16
, NULL
);
6658 setkeybinding(MODKEY
, XK_F7
, KF_WS_17
, NULL
);
6659 setkeybinding(MODKEY
, XK_F8
, KF_WS_18
, NULL
);
6660 setkeybinding(MODKEY
, XK_F9
, KF_WS_19
, NULL
);
6661 setkeybinding(MODKEY
, XK_F10
, KF_WS_20
, NULL
);
6662 setkeybinding(MODKEY
, XK_F11
, KF_WS_21
, NULL
);
6663 setkeybinding(MODKEY
, XK_F12
, KF_WS_22
, NULL
);
6664 setkeybinding(MODKEY
, XK_Right
, KF_WS_NEXT
, NULL
);
6665 setkeybinding(MODKEY
, XK_Left
, KF_WS_PREV
, NULL
);
6666 setkeybinding(MODKEY
, XK_Up
, KF_WS_NEXT_ALL
, NULL
);
6667 setkeybinding(MODKEY
, XK_Down
, KF_WS_PREV_ALL
, NULL
);
6668 setkeybinding(MODKEY_SHIFT
, XK_Up
, KF_WS_NEXT_MOVE
,NULL
);
6669 setkeybinding(MODKEY_SHIFT
, XK_Down
, KF_WS_PREV_MOVE
,NULL
);
6670 setkeybinding(MODKEY
, XK_a
, KF_WS_PRIOR
, NULL
);
6672 setkeybinding(MODKEY_SHIFT
, XK_d
, KF_DUMPWINS
, NULL
);
6682 while (RB_EMPTY(&keys
) == 0) {
6683 kp
= RB_ROOT(&keys
);
6689 setkeymapping(char *selector
, char *value
, int flags
)
6691 char *keymapping_file
;
6693 /* suppress unused warnings since vars are needed */
6697 DNPRINTF(SWM_D_KEY
, "setkeymapping: enter\n");
6699 keymapping_file
= expand_tilde(value
);
6702 /* load new key bindings; if it fails, revert to default bindings */
6703 if (conf_load(keymapping_file
, SWM_CONF_KEYMAPPING
)) {
6708 free(keymapping_file
);
6710 DNPRINTF(SWM_D_KEY
, "setkeymapping: leave\n");
6715 updatenumlockmask(void)
6718 xcb_get_modifier_mapping_reply_t
*modmap_r
;
6719 xcb_keycode_t
*modmap
, kc
, *keycode
;
6723 modmap_r
= xcb_get_modifier_mapping_reply(conn
,
6724 xcb_get_modifier_mapping(conn
),
6727 modmap
= xcb_get_modifier_mapping_keycodes(modmap_r
);
6728 for (i
= 0; i
< 8; i
++) {
6729 for (j
= 0; j
< modmap_r
->keycodes_per_modifier
; j
++) {
6730 kc
= modmap
[i
* modmap_r
->keycodes_per_modifier
6732 keycode
= xcb_key_symbols_get_keycode(syms
,
6735 numlockmask
= (1 << i
);
6741 DNPRINTF(SWM_D_MISC
, "updatenumlockmask: %d\n", numlockmask
);
6748 int num_screens
, k
, j
;
6749 unsigned int modifiers
[4];
6750 xcb_keycode_t
*code
;
6752 DNPRINTF(SWM_D_MISC
, "grabkeys\n");
6753 updatenumlockmask();
6756 modifiers
[1] = numlockmask
;
6757 modifiers
[2] = XCB_MOD_MASK_LOCK
;
6758 modifiers
[3] = numlockmask
| XCB_MOD_MASK_LOCK
;
6760 num_screens
= get_screen_count();
6761 for (k
= 0; k
< num_screens
; k
++) {
6762 if (TAILQ_EMPTY(&screens
[k
].rl
))
6764 xcb_ungrab_key(conn
, XCB_GRAB_ANY
, screens
[k
].root
,
6766 RB_FOREACH(kp
, key_tree
, &keys
) {
6767 /* Skip unused ws binds. */
6768 if ((int)kp
->funcid
> KF_WS_1
+ workspace_limit
- 1 &&
6769 kp
->funcid
<= KF_WS_22
)
6772 /* Skip unused mvws binds. */
6773 if ((int)kp
->funcid
> KF_MVWS_1
+ workspace_limit
- 1 &&
6774 kp
->funcid
<= KF_MVWS_22
)
6777 if ((code
= xcb_key_symbols_get_keycode(syms
,
6779 for (j
= 0; j
< LENGTH(modifiers
); j
++)
6780 xcb_grab_key(conn
, 1,
6782 kp
->mod
| modifiers
[j
],
6783 *code
, XCB_GRAB_MODE_SYNC
,
6784 XCB_GRAB_MODE_SYNC
);
6792 grabbuttons(struct ws_win
*win
)
6794 unsigned int modifiers
[4];
6797 DNPRINTF(SWM_D_MOUSE
, "grabbuttons: win 0x%x\n", win
->id
);
6798 updatenumlockmask();
6801 modifiers
[1] = numlockmask
;
6802 modifiers
[2] = XCB_MOD_MASK_LOCK
;
6803 modifiers
[3] = numlockmask
| XCB_MOD_MASK_LOCK
;
6805 for (i
= 0; i
< LENGTH(buttons
); i
++)
6806 if (buttons
[i
].action
== client_click
)
6807 for (j
= 0; j
< LENGTH(modifiers
); ++j
)
6808 xcb_grab_button(conn
, 0, win
->id
, BUTTONMASK
,
6809 XCB_GRAB_MODE_SYNC
, XCB_GRAB_MODE_ASYNC
,
6810 XCB_WINDOW_NONE
, XCB_CURSOR_NONE
,
6811 buttons
[i
].button
, buttons
[i
].mask
|
6815 const char *quirkname
[] = {
6816 "NONE", /* config string for "no value" */
6824 "FOCUSONMAP_SINGLE",
6827 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
6828 #define SWM_Q_WS "\n|+ \t"
6830 parsequirks(char *qstr
, unsigned long *quirk
)
6840 while ((name
= strsep(&cp
, SWM_Q_WS
)) != NULL
) {
6842 cp
+= (long)strspn(cp
, SWM_Q_WS
);
6843 for (i
= 0; i
< LENGTH(quirkname
); i
++) {
6844 if (!strncasecmp(name
, quirkname
[i
], SWM_QUIRK_LEN
)) {
6845 DNPRINTF(SWM_D_QUIRK
,
6846 "parsequirks: %s\n", name
);
6851 *quirk
|= 1 << (i
-1);
6855 if (i
>= LENGTH(quirkname
)) {
6856 DNPRINTF(SWM_D_QUIRK
,
6857 "parsequirks: invalid quirk [%s]\n", name
);
6865 quirk_insert(const char *class, const char *name
, unsigned long quirk
)
6869 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: %s:%s [%lu]\n", class, name
,
6872 if ((qp
= malloc(sizeof *qp
)) == NULL
)
6873 err(1, "quirk_insert: malloc");
6874 if ((qp
->class = strdup(class)) == NULL
)
6875 err(1, "quirk_insert: strdup");
6876 if ((qp
->name
= strdup(name
)) == NULL
)
6877 err(1, "quirk_insert: strdup");
6880 TAILQ_INSERT_TAIL(&quirks
, qp
, entry
);
6882 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: leave\n");
6886 quirk_remove(struct quirk
*qp
)
6888 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: %s:%s [%lu]\n", qp
->class,
6889 qp
->name
, qp
->quirk
);
6891 TAILQ_REMOVE(&quirks
, qp
, entry
);
6896 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: leave\n");
6900 quirk_replace(struct quirk
*qp
, const char *class, const char *name
,
6901 unsigned long quirk
)
6903 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: %s:%s [%lu]\n", qp
->class,
6904 qp
->name
, qp
->quirk
);
6907 quirk_insert(class, name
, quirk
);
6909 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: leave\n");
6913 setquirk(const char *class, const char *name
, unsigned long quirk
)
6917 DNPRINTF(SWM_D_QUIRK
, "setquirk: enter %s:%s [%lu]\n", class, name
,
6920 /* Remove/replace existing quirk. */
6921 TAILQ_FOREACH(qp
, &quirks
, entry
) {
6922 if (!strcmp(qp
->class, class) && !strcmp(qp
->name
, name
)) {
6926 quirk_replace(qp
, class, name
, quirk
);
6927 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
6932 /* Only insert if quirk is not NONE. */
6934 quirk_insert(class, name
, quirk
);
6936 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
6940 setconfquirk(char *selector
, char *value
, int flags
)
6942 char *cp
, *class, *name
;
6946 /* suppress unused warning since var is needed */
6949 if (selector
== NULL
)
6951 if ((cp
= strchr(selector
, ':')) == NULL
)
6956 if ((retval
= parsequirks(value
, &qrks
)) == 0)
6957 setquirk(class, name
, qrks
);
6964 setquirk("MPlayer", "xv", SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
| SWM_Q_FOCUSPREV
);
6965 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT
);
6966 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ
);
6967 setquirk("Firefox", "Dialog", SWM_Q_FLOAT
);
6968 setquirk("Gimp", "gimp", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
6969 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ
);
6970 setquirk("xine", "Xine Window", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
6971 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
6972 setquirk("xine", "xine Panel", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
6973 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
6974 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN
| SWM_Q_FLOAT
);
6975 setquirk("pcb", "pcb", SWM_Q_FLOAT
);
6976 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
);
6979 /* conf file stuff */
6980 #define SWM_CONF_FILE "spectrwm.conf"
6981 #define SWM_CONF_FILE_OLD "scrotwm.conf"
6985 SWM_S_BAR_AT_BOTTOM
,
6986 SWM_S_BAR_BORDER_WIDTH
,
6989 SWM_S_BAR_ENABLED_WS
,
6994 SWM_S_BOUNDARY_WIDTH
,
6995 SWM_S_CLOCK_ENABLED
,
6998 SWM_S_CYCLE_VISIBLE
,
7000 SWM_S_DISABLE_BORDER
,
7002 SWM_S_FOCUS_CLOSE_WRAP
,
7003 SWM_S_FOCUS_DEFAULT
,
7005 SWM_S_REGION_PADDING
,
7010 SWM_S_STACK_ENABLED
,
7013 SWM_S_TITLE_CLASS_ENABLED
,
7014 SWM_S_TITLE_NAME_ENABLED
,
7015 SWM_S_URGENT_ENABLED
,
7016 SWM_S_VERBOSE_LAYOUT
,
7017 SWM_S_WINDOW_NAME_ENABLED
,
7018 SWM_S_WORKSPACE_LIMIT
7022 setconfvalue(char *selector
, char *value
, int flags
)
7024 struct workspace
*ws
;
7025 int i
, ws_id
, num_screens
;
7028 /* suppress unused warning since var is needed */
7032 case SWM_S_BAR_ACTION
:
7034 if ((bar_argv
[0] = expand_tilde(value
)) == NULL
)
7035 err(1, "setconfvalue: bar_action");
7037 case SWM_S_BAR_AT_BOTTOM
:
7038 bar_at_bottom
= atoi(value
);
7040 case SWM_S_BAR_BORDER_WIDTH
:
7041 bar_border_width
= atoi(value
);
7042 if (bar_border_width
< 0)
7043 bar_border_width
= 0;
7045 case SWM_S_BAR_DELAY
:
7046 /* No longer needed; leave to not break old conf files. */
7048 case SWM_S_BAR_ENABLED
:
7049 bar_enabled
= atoi(value
);
7051 case SWM_S_BAR_ENABLED_WS
:
7052 ws_id
= atoi(selector
) - 1;
7053 if (ws_id
< 0 || ws_id
>= workspace_limit
)
7054 errx(1, "setconfvalue: bar_enabled_ws: invalid "
7055 "workspace %d.", ws_id
+ 1);
7057 num_screens
= get_screen_count();
7058 for (i
= 0; i
< num_screens
; i
++) {
7059 ws
= (struct workspace
*)&screens
[i
].ws
;
7060 ws
[ws_id
].bar_enabled
= atoi(value
);
7063 case SWM_S_BAR_FONT
:
7065 if (asprintf(&bar_fonts
, "%s,%s", value
, bar_fonts
) == -1)
7066 err(1, "setconfvalue: asprintf: failed to allocate "
7067 "memory for bar_fonts.");
7070 /* If already in xft mode, then we are done. */
7071 if (!bar_font_legacy
)
7074 /* If there are any non-XLFD entries, switch to Xft mode. */
7075 while ((b
= strsep(&value
, ",")) != NULL
) {
7079 bar_font_legacy
= 0;
7084 case SWM_S_BAR_FORMAT
:
7086 if ((bar_format
= strdup(value
)) == NULL
)
7087 err(1, "setconfvalue: bar_format");
7089 case SWM_S_BAR_JUSTIFY
:
7090 if (!strcmp(value
, "left"))
7091 bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
7092 else if (!strcmp(value
, "center"))
7093 bar_justify
= SWM_BAR_JUSTIFY_CENTER
;
7094 else if (!strcmp(value
, "right"))
7095 bar_justify
= SWM_BAR_JUSTIFY_RIGHT
;
7097 errx(1, "invalid bar_justify");
7099 case SWM_S_BORDER_WIDTH
:
7100 border_width
= atoi(value
);
7101 if (border_width
< 0)
7104 case SWM_S_BOUNDARY_WIDTH
:
7105 boundary_width
= atoi(value
);
7106 if (boundary_width
< 0)
7109 case SWM_S_CLOCK_ENABLED
:
7110 clock_enabled
= atoi(value
);
7112 case SWM_S_CLOCK_FORMAT
:
7113 #ifndef SWM_DENY_CLOCK_FORMAT
7115 if ((clock_format
= strdup(value
)) == NULL
)
7116 err(1, "setconfvalue: clock_format");
7119 case SWM_S_CYCLE_EMPTY
:
7120 cycle_empty
= atoi(value
);
7122 case SWM_S_CYCLE_VISIBLE
:
7123 cycle_visible
= atoi(value
);
7125 case SWM_S_DIALOG_RATIO
:
7126 dialog_ratio
= atof(value
);
7127 if (dialog_ratio
> 1.0 || dialog_ratio
<= .3)
7130 case SWM_S_DISABLE_BORDER
:
7131 disable_border
= atoi(value
);
7133 case SWM_S_FOCUS_CLOSE
:
7134 if (!strcmp(value
, "first"))
7135 focus_close
= SWM_STACK_BOTTOM
;
7136 else if (!strcmp(value
, "last"))
7137 focus_close
= SWM_STACK_TOP
;
7138 else if (!strcmp(value
, "next"))
7139 focus_close
= SWM_STACK_ABOVE
;
7140 else if (!strcmp(value
, "previous"))
7141 focus_close
= SWM_STACK_BELOW
;
7143 errx(1, "focus_close");
7145 case SWM_S_FOCUS_CLOSE_WRAP
:
7146 focus_close_wrap
= atoi(value
);
7148 case SWM_S_FOCUS_DEFAULT
:
7149 if (!strcmp(value
, "last"))
7150 focus_default
= SWM_STACK_TOP
;
7151 else if (!strcmp(value
, "first"))
7152 focus_default
= SWM_STACK_BOTTOM
;
7154 errx(1, "focus_default");
7156 case SWM_S_FOCUS_MODE
:
7157 if (!strcmp(value
, "default"))
7158 focus_mode
= SWM_FOCUS_DEFAULT
;
7159 else if (!strcmp(value
, "follow") ||
7160 !strcmp(value
, "follow_cursor"))
7161 focus_mode
= SWM_FOCUS_FOLLOW
;
7162 else if (!strcmp(value
, "manual"))
7163 focus_mode
= SWM_FOCUS_MANUAL
;
7165 errx(1, "focus_mode");
7167 case SWM_S_REGION_PADDING
:
7168 region_padding
= atoi(value
);
7169 if (region_padding
< 0)
7172 case SWM_S_SPAWN_ORDER
:
7173 if (!strcmp(value
, "first"))
7174 spawn_position
= SWM_STACK_BOTTOM
;
7175 else if (!strcmp(value
, "last"))
7176 spawn_position
= SWM_STACK_TOP
;
7177 else if (!strcmp(value
, "next"))
7178 spawn_position
= SWM_STACK_ABOVE
;
7179 else if (!strcmp(value
, "previous"))
7180 spawn_position
= SWM_STACK_BELOW
;
7182 errx(1, "spawn_position");
7184 case SWM_S_SPAWN_TERM
:
7185 setconfspawn("term", value
, 0);
7186 setconfspawn("spawn_term", value
, 0);
7189 /* No longer needed; leave to not break old conf files. */
7191 case SWM_S_SS_ENABLED
:
7192 /* No longer needed; leave to not break old conf files. */
7194 case SWM_S_STACK_ENABLED
:
7195 stack_enabled
= atoi(value
);
7197 case SWM_S_TERM_WIDTH
:
7198 term_width
= atoi(value
);
7202 case SWM_S_TILE_GAP
:
7203 tile_gap
= atoi(value
);
7205 case SWM_S_TITLE_CLASS_ENABLED
:
7206 title_class_enabled
= atoi(value
);
7208 case SWM_S_TITLE_NAME_ENABLED
:
7209 title_name_enabled
= atoi(value
);
7211 case SWM_S_URGENT_ENABLED
:
7212 urgent_enabled
= atoi(value
);
7214 case SWM_S_VERBOSE_LAYOUT
:
7215 verbose_layout
= atoi(value
);
7216 for (i
= 0; layouts
[i
].l_stack
!= NULL
; i
++) {
7218 layouts
[i
].l_string
= fancy_stacker
;
7220 layouts
[i
].l_string
= plain_stacker
;
7223 case SWM_S_WINDOW_NAME_ENABLED
:
7224 window_name_enabled
= atoi(value
);
7226 case SWM_S_WORKSPACE_LIMIT
:
7227 workspace_limit
= atoi(value
);
7228 if (workspace_limit
> SWM_WS_MAX
)
7229 workspace_limit
= SWM_WS_MAX
;
7230 else if (workspace_limit
< 1)
7231 workspace_limit
= 1;
7240 setconfmodkey(char *selector
, char *value
, int flags
)
7242 /* suppress unused warnings since vars are needed */
7246 if (!strncasecmp(value
, "Mod1", strlen("Mod1")))
7247 update_modkey(XCB_MOD_MASK_1
);
7248 else if (!strncasecmp(value
, "Mod2", strlen("Mod2")))
7249 update_modkey(XCB_MOD_MASK_2
);
7250 else if (!strncasecmp(value
, "Mod3", strlen("Mod3")))
7251 update_modkey(XCB_MOD_MASK_3
);
7252 else if (!strncasecmp(value
, "Mod4", strlen("Mod4")))
7253 update_modkey(XCB_MOD_MASK_4
);
7260 setconfcolor(char *selector
, char *value
, int flags
)
7262 setscreencolor(value
, ((selector
== NULL
)?-1:atoi(selector
)), flags
);
7267 setconfregion(char *selector
, char *value
, int flags
)
7269 /* suppress unused warnings since vars are needed */
7273 custom_region(value
);
7278 setautorun(char *selector
, char *value
, int flags
)
7288 /* suppress unused warnings since vars are needed */
7292 if (getenv("SWM_STARTED"))
7296 if (sscanf(value
, "ws[%d]:%1023c", &ws_id
, s
) != 2)
7297 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
7299 if (ws_id
< 0 || ws_id
>= workspace_limit
)
7300 errx(1, "autorun: invalid workspace %d", ws_id
+ 1);
7302 sp
= expand_tilde((char *)&s
);
7305 * This is a little intricate
7307 * If the pid already exists we simply reuse it because it means it was
7308 * used before AND not claimed by manage_window. We get away with
7309 * altering it in the parent after INSERT because this can not be a race
7312 while ((ap
= strsep(&sp
, " \t")) != NULL
) {
7315 DNPRINTF(SWM_D_SPAWN
, "setautorun: arg [%s]\n", ap
);
7317 if ((a
.argv
= realloc(a
.argv
, argc
* sizeof(char *))) == NULL
)
7318 err(1, "setautorun: realloc");
7319 a
.argv
[argc
- 1] = ap
;
7323 if ((a
.argv
= realloc(a
.argv
, (argc
+ 1) * sizeof(char *))) == NULL
)
7324 err(1, "setautorun: realloc");
7325 a
.argv
[argc
] = NULL
;
7327 if ((pid
= fork()) == 0) {
7328 spawn(ws_id
, &a
, 1);
7337 p
= calloc(1, sizeof *p
);
7340 TAILQ_INSERT_TAIL(&pidlist
, p
, entry
);
7350 setlayout(char *selector
, char *value
, int flags
)
7352 int ws_id
, i
, x
, mg
, ma
, si
, ar
, f
= 0;
7353 int st
= SWM_V_STACK
, num_screens
;
7355 struct workspace
*ws
;
7357 /* suppress unused warnings since vars are needed */
7361 if (getenv("SWM_STARTED"))
7365 if (sscanf(value
, "ws[%d]:%d:%d:%d:%d:%1023c",
7366 &ws_id
, &mg
, &ma
, &si
, &ar
, s
) != 6)
7367 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
7368 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
7371 if (ws_id
< 0 || ws_id
>= workspace_limit
)
7372 errx(1, "layout: invalid workspace %d", ws_id
+ 1);
7374 if (!strcasecmp(s
, "vertical"))
7376 else if (!strcasecmp(s
, "vertical_flip")) {
7379 } else if (!strcasecmp(s
, "horizontal"))
7381 else if (!strcasecmp(s
, "horizontal_flip")) {
7384 } else if (!strcasecmp(s
, "fullscreen"))
7387 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
7388 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
7391 num_screens
= get_screen_count();
7392 for (i
= 0; i
< num_screens
; i
++) {
7393 ws
= (struct workspace
*)&screens
[i
].ws
;
7394 ws
[ws_id
].cur_layout
= &layouts
[st
];
7396 ws
[ws_id
].always_raise
= ar
;
7397 if (st
== SWM_MAX_STACK
)
7401 for (x
= 0; x
< abs(mg
); x
++) {
7402 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
7403 mg
>= 0 ? SWM_ARG_ID_MASTERGROW
:
7404 SWM_ARG_ID_MASTERSHRINK
);
7408 for (x
= 0; x
< abs(ma
); x
++) {
7409 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
7410 ma
>= 0 ? SWM_ARG_ID_MASTERADD
:
7411 SWM_ARG_ID_MASTERDEL
);
7415 for (x
= 0; x
< abs(si
); x
++) {
7416 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
7417 si
>= 0 ? SWM_ARG_ID_STACKINC
:
7418 SWM_ARG_ID_STACKDEC
);
7423 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
7424 SWM_ARG_ID_FLIPLAYOUT
);
7434 /* config options */
7435 struct config_option
{
7437 int (*func
)(char*, char*, int);
7440 struct config_option configopt
[] = {
7441 { "autorun", setautorun
, 0 },
7442 { "bar_action", setconfvalue
, SWM_S_BAR_ACTION
},
7443 { "bar_at_bottom", setconfvalue
, SWM_S_BAR_AT_BOTTOM
},
7444 { "bar_border", setconfcolor
, SWM_S_COLOR_BAR_BORDER
},
7445 { "bar_border_unfocus", setconfcolor
, SWM_S_COLOR_BAR_BORDER_UNFOCUS
},
7446 { "bar_border_width", setconfvalue
, SWM_S_BAR_BORDER_WIDTH
},
7447 { "bar_color", setconfcolor
, SWM_S_COLOR_BAR
},
7448 { "bar_delay", setconfvalue
, SWM_S_BAR_DELAY
},
7449 { "bar_enabled", setconfvalue
, SWM_S_BAR_ENABLED
},
7450 { "bar_enabled_ws", setconfvalue
, SWM_S_BAR_ENABLED_WS
},
7451 { "bar_font", setconfvalue
, SWM_S_BAR_FONT
},
7452 { "bar_font_color", setconfcolor
, SWM_S_COLOR_BAR_FONT
},
7453 { "bar_format", setconfvalue
, SWM_S_BAR_FORMAT
},
7454 { "bar_justify", setconfvalue
, SWM_S_BAR_JUSTIFY
},
7455 { "bind", setconfbinding
, 0 },
7456 { "border_width", setconfvalue
, SWM_S_BORDER_WIDTH
},
7457 { "boundary_width", setconfvalue
, SWM_S_BOUNDARY_WIDTH
},
7458 { "clock_enabled", setconfvalue
, SWM_S_CLOCK_ENABLED
},
7459 { "clock_format", setconfvalue
, SWM_S_CLOCK_FORMAT
},
7460 { "color_focus", setconfcolor
, SWM_S_COLOR_FOCUS
},
7461 { "color_unfocus", setconfcolor
, SWM_S_COLOR_UNFOCUS
},
7462 { "cycle_empty", setconfvalue
, SWM_S_CYCLE_EMPTY
},
7463 { "cycle_visible", setconfvalue
, SWM_S_CYCLE_VISIBLE
},
7464 { "dialog_ratio", setconfvalue
, SWM_S_DIALOG_RATIO
},
7465 { "disable_border", setconfvalue
, SWM_S_DISABLE_BORDER
},
7466 { "focus_close", setconfvalue
, SWM_S_FOCUS_CLOSE
},
7467 { "focus_close_wrap", setconfvalue
, SWM_S_FOCUS_CLOSE_WRAP
},
7468 { "focus_default", setconfvalue
, SWM_S_FOCUS_DEFAULT
},
7469 { "focus_mode", setconfvalue
, SWM_S_FOCUS_MODE
},
7470 { "keyboard_mapping", setkeymapping
, 0 },
7471 { "layout", setlayout
, 0 },
7472 { "modkey", setconfmodkey
, 0 },
7473 { "program", setconfspawn
, 0 },
7474 { "quirk", setconfquirk
, 0 },
7475 { "region", setconfregion
, 0 },
7476 { "region_padding", setconfvalue
, SWM_S_REGION_PADDING
},
7477 { "screenshot_app", setconfvalue
, SWM_S_SS_APP
},
7478 { "screenshot_enabled", setconfvalue
, SWM_S_SS_ENABLED
},
7479 { "spawn_position", setconfvalue
, SWM_S_SPAWN_ORDER
},
7480 { "spawn_term", setconfvalue
, SWM_S_SPAWN_TERM
},
7481 { "stack_enabled", setconfvalue
, SWM_S_STACK_ENABLED
},
7482 { "term_width", setconfvalue
, SWM_S_TERM_WIDTH
},
7483 { "tile_gap", setconfvalue
, SWM_S_TILE_GAP
},
7484 { "title_class_enabled", setconfvalue
, SWM_S_TITLE_CLASS_ENABLED
},
7485 { "title_name_enabled", setconfvalue
, SWM_S_TITLE_NAME_ENABLED
},
7486 { "urgent_enabled", setconfvalue
, SWM_S_URGENT_ENABLED
},
7487 { "verbose_layout", setconfvalue
, SWM_S_VERBOSE_LAYOUT
},
7488 { "window_name_enabled", setconfvalue
, SWM_S_WINDOW_NAME_ENABLED
},
7489 { "workspace_limit", setconfvalue
, SWM_S_WORKSPACE_LIMIT
},
7493 _add_startup_exception(const char *fmt
, va_list ap
)
7495 if (vasprintf(&startup_exception
, fmt
, ap
) == -1)
7496 warn("%s: asprintf", __func__
);
7500 add_startup_exception(const char *fmt
, ...)
7506 if (startup_exception
)
7509 /* force bar to be enabled due to exception */
7513 _add_startup_exception(fmt
, ap
);
7518 conf_load(const char *filename
, int keymapping
)
7521 char *line
= NULL
, *cp
, *optsub
, *optval
= NULL
;
7522 size_t linelen
, lineno
= 0;
7523 int wordlen
, i
, optidx
;
7524 struct config_option
*opt
= NULL
;
7526 DNPRINTF(SWM_D_CONF
, "conf_load: begin\n");
7528 if (filename
== NULL
) {
7529 warnx("conf_load: no filename");
7533 DNPRINTF(SWM_D_CONF
, "conf_load: open %s\n", filename
);
7535 if ((config
= fopen(filename
, "r")) == NULL
) {
7536 warn("conf_load: fopen: %s", filename
);
7540 while (!feof(config
)) {
7544 if ((line
= fparseln(config
, &linelen
, &lineno
, NULL
,
7545 FPARSELN_UNESCCOMM
| FPARSELN_UNESCCONT
)) == NULL
) {
7547 err(1, "%s", filename
);
7552 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
7553 if (cp
[0] == '\0') {
7557 /* get config option */
7558 wordlen
= strcspn(cp
, "=[ \t\n");
7560 add_startup_exception("%s: line %zd: no option found",
7565 for (i
= 0; i
< LENGTH(configopt
); i
++) {
7566 opt
= &configopt
[i
];
7567 if (!strncasecmp(cp
, opt
->optname
, wordlen
) &&
7568 (int)strlen(opt
->optname
) == wordlen
) {
7574 add_startup_exception("%s: line %zd: unknown option "
7575 "%.*s", filename
, lineno
, wordlen
, cp
);
7578 if (keymapping
&& opt
&& strcmp(opt
->optname
, "bind")) {
7579 add_startup_exception("%s: line %zd: invalid option "
7580 "%.*s", filename
, lineno
, wordlen
, cp
);
7584 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
7586 /* from here on out we call goto invalid to continue */
7588 /* get [selector] if any */
7592 wordlen
= strcspn(cp
, "]");
7595 add_startup_exception("%s: line %zd: "
7596 "syntax error", filename
, lineno
);
7600 if (asprintf(&optsub
, "%.*s", wordlen
, cp
) ==
7602 add_startup_exception("%s: line %zd: "
7603 "unable to allocatememory for "
7604 "selector", filename
, lineno
);
7609 cp
+= strspn(cp
, "] \t\n"); /* eat trailing */
7611 cp
+= strspn(cp
, "= \t\n"); /* eat trailing */
7613 optval
= strdup(cp
);
7614 if (strlen(optval
) == 0) {
7615 add_startup_exception("%s: line %zd: must supply value "
7616 "to %s", filename
, lineno
,
7617 configopt
[optidx
].optname
);
7620 /* call function to deal with it all */
7621 if (configopt
[optidx
].func(optsub
, optval
,
7622 configopt
[optidx
].funcflags
) != 0) {
7623 add_startup_exception("%s: line %zd: invalid data for "
7624 "%s", filename
, lineno
, configopt
[optidx
].optname
);
7641 DNPRINTF(SWM_D_CONF
, "conf_load: end\n");
7647 set_child_transient(struct ws_win
*win
, xcb_window_t
*trans
)
7649 struct ws_win
*parent
, *w
;
7650 struct swm_region
*r
;
7651 struct workspace
*ws
;
7652 xcb_icccm_wm_hints_t wmh
;
7654 parent
= find_window(win
->transient
);
7656 parent
->focus_child
= win
;
7658 DNPRINTF(SWM_D_MISC
, "set_child_transient: parent doesn't exist"
7659 " for 0x%x trans 0x%x\n", win
->id
, win
->transient
);
7661 r
= root_to_region(win
->wa
->root
, SWM_CK_ALL
);
7663 /* parent doen't exist in our window list */
7664 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
7665 if (xcb_icccm_get_wm_hints_reply(conn
,
7666 xcb_icccm_get_wm_hints(conn
, w
->id
),
7668 warnx("can't get hints for 0x%x", w
->id
);
7672 if (win
->hints
.window_group
!= wmh
.window_group
)
7675 w
->focus_child
= win
;
7676 win
->transient
= w
->id
;
7678 DNPRINTF(SWM_D_MISC
, "set_child_transient: adjusting "
7679 "transient to 0x%x\n", win
->transient
);
7686 window_get_pid(xcb_window_t win
)
7691 xcb_get_property_cookie_t pc
;
7692 xcb_get_property_reply_t
*pr
;
7694 apid
= get_atom_from_string("_NET_WM_PID");
7695 if (apid
== XCB_ATOM_NONE
)
7698 pc
= xcb_get_property(conn
, 0, win
, apid
, XCB_ATOM_CARDINAL
, 0, 1);
7699 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
7702 if (pr
->type
!= XCB_ATOM_CARDINAL
) {
7707 if (pr
->type
== apid
&& pr
->format
== 32)
7708 ret
= *((pid_t
*)xcb_get_property_value(pr
));
7714 apid
= get_atom_from_string("_SWM_PID");
7715 pc
= xcb_get_property(conn
, 0, win
, apid
, XCB_ATOM_STRING
,
7717 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
7720 if (pr
->type
!= apid
) {
7725 ret
= (pid_t
)strtonum(xcb_get_property_value(pr
), 0, INT_MAX
, &errstr
);
7732 get_ws_idx(xcb_window_t id
)
7738 xcb_get_property_reply_t
*gpr
;
7740 gpr
= xcb_get_property_reply(conn
,
7741 xcb_get_property(conn
, 0, id
, a_swm_ws
,
7742 XCB_ATOM_STRING
, 0, SWM_PROPLEN
),
7747 proplen
= xcb_get_property_value_length(gpr
);
7749 prop
= malloc(proplen
+ 1);
7752 xcb_get_property_value(gpr
),
7754 prop
[proplen
] = '\0';
7761 DNPRINTF(SWM_D_PROP
, "get_ws_idx: _SWM_WS: %s\n", prop
);
7762 ws_idx
= (int)strtonum(prop
, 0, workspace_limit
- 1, &errstr
);
7764 DNPRINTF(SWM_D_PROP
, "get_ws_idx: window: #%s: %s",
7774 manage_window(xcb_window_t id
, uint16_t mapped
)
7776 xcb_window_t trans
= XCB_WINDOW_NONE
;
7777 struct ws_win
*win
, *ww
;
7779 char ws_idx_str
[SWM_PROPLEN
];
7780 struct swm_region
*r
;
7784 xcb_icccm_get_wm_protocols_reply_t wpr
;
7786 if ((win
= find_window(id
)) != NULL
) {
7787 DNPRINTF(SWM_D_MISC
, "manage_window: win 0x%x already "
7788 "managed; skipping.)\n", id
);
7789 return (win
); /* Already managed. */
7792 /* See if window is on the unmanaged list. */
7793 if ((win
= find_unmanaged_window(id
)) != NULL
) {
7794 DNPRINTF(SWM_D_MISC
, "manage_window: win 0x%x found on "
7795 "unmanaged list.\n", id
);
7796 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
7799 set_child_transient(win
, &trans
);
7803 DNPRINTF(SWM_D_MISC
, "manage_window: win 0x%x is new.\n", id
);
7806 /* Create and initialize ws_win object. */
7807 if ((win
= calloc(1, sizeof(struct ws_win
))) == NULL
)
7808 err(1, "manage_window: calloc: failed to allocate memory for "
7813 /* Get window geometry. */
7814 win
->wa
= xcb_get_geometry_reply(conn
,
7815 xcb_get_geometry(conn
, win
->id
),
7818 /* Figure out which region the window belongs to. */
7819 r
= root_to_region(win
->wa
->root
, SWM_CK_ALL
);
7821 /* Ignore window border if there is one. */
7822 WIDTH(win
) = win
->wa
->width
;
7823 HEIGHT(win
) = win
->wa
->height
;
7824 X(win
) = win
->wa
->x
+ win
->wa
->border_width
- border_width
;
7825 Y(win
) = win
->wa
->y
+ win
->wa
->border_width
- border_width
;
7827 win
->mapped
= mapped
;
7828 win
->floatmaxed
= 0;
7829 win
->ewmh_flags
= 0;
7830 win
->s
= r
->s
; /* this never changes */
7832 /* Get WM_SIZE_HINTS. */
7833 xcb_icccm_get_wm_normal_hints_reply(conn
,
7834 xcb_icccm_get_wm_normal_hints(conn
, win
->id
),
7838 xcb_icccm_get_wm_hints_reply(conn
,
7839 xcb_icccm_get_wm_hints(conn
, win
->id
),
7842 /* Get WM_TRANSIENT_FOR; see if window is a transient. */
7843 xcb_icccm_get_wm_transient_for_reply(conn
,
7844 xcb_icccm_get_wm_transient_for(conn
, win
->id
),
7847 win
->transient
= trans
;
7848 set_child_transient(win
, &win
->transient
);
7851 /* Get supported protocols. */
7852 if (xcb_icccm_get_wm_protocols_reply(conn
,
7853 xcb_icccm_get_wm_protocols(conn
, win
->id
, a_prot
),
7855 for (i
= 0; i
< wpr
.atoms_len
; i
++) {
7856 if (wpr
.atoms
[i
] == a_takefocus
)
7857 win
->take_focus
= 1;
7858 if (wpr
.atoms
[i
] == a_delete
)
7859 win
->can_delete
= 1;
7861 xcb_icccm_get_wm_protocols_reply_wipe(&wpr
);
7864 win
->iconic
= get_swm_iconic(win
);
7866 /* Figure out which workspace the window belongs to. */
7867 if ((p
= find_pid(window_get_pid(win
->id
))) != NULL
) {
7868 win
->ws
= &r
->s
->ws
[p
->ws
];
7869 TAILQ_REMOVE(&pidlist
, p
, entry
);
7872 } else if ((ws_idx
= get_ws_idx(win
->id
)) != -1 &&
7874 /* _SWM_WS is set; use that. */
7875 win
->ws
= &r
->s
->ws
[ws_idx
];
7876 } else if (trans
&& (ww
= find_window(trans
)) != NULL
) {
7877 /* Launch transients in the same ws as parent. */
7883 /* Set the _SWM_WS atom so we can remember this after reincarnation. */
7884 if (snprintf(ws_idx_str
, SWM_PROPLEN
, "%d", win
->ws
->idx
) <
7886 DNPRINTF(SWM_D_PROP
, "manage_window: set _SWM_WS: %s\n",
7888 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
7889 a_swm_ws
, XCB_ATOM_STRING
, 8, strlen(ws_idx_str
),
7893 /* WS must already be set for this to work. */
7894 store_float_geom(win
);
7897 ewmh_autoquirk(win
);
7899 /* Determine initial quirks. */
7900 if (xcb_icccm_get_wm_class_reply(conn
,
7901 xcb_icccm_get_wm_class(conn
, win
->id
),
7903 DNPRINTF(SWM_D_CLASS
, "manage_window: class: %s, name: %s\n",
7904 win
->ch
.class_name
, win
->ch
.instance_name
);
7906 /* java is retarded so treat it special */
7907 if (strstr(win
->ch
.instance_name
, "sun-awt")) {
7908 DNPRINTF(SWM_D_CLASS
, "manage_window: java window "
7913 TAILQ_FOREACH(qp
, &quirks
, entry
) {
7914 if (!strcmp(win
->ch
.class_name
, qp
->class) &&
7915 !strcmp(win
->ch
.instance_name
, qp
->name
)) {
7916 DNPRINTF(SWM_D_CLASS
, "manage_window: on quirks"
7917 "list; mask: 0x%lx\n", qp
->quirk
);
7918 if (qp
->quirk
& SWM_Q_FLOAT
)
7920 win
->quirks
= qp
->quirk
;
7925 /* Alter window position if quirky */
7926 if (win
->quirks
& SWM_Q_ANYWHERE
)
7929 /* Reset font sizes (the bruteforce way; no default keybinding). */
7930 if (win
->quirks
& SWM_Q_XTERM_FONTADJ
) {
7931 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
7932 fake_keypress(win
, XK_KP_Subtract
, XCB_MOD_MASK_SHIFT
);
7933 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
7934 fake_keypress(win
, XK_KP_Add
, XCB_MOD_MASK_SHIFT
);
7937 /* Make sure window is positioned inside its region, if its active. */
7939 region_containment(win
, r
, SWM_CW_ALLSIDES
|
7940 SWM_CW_HARDBOUNDARY
);
7944 /* Select which X events to monitor and set border pixel color. */
7945 wa
[0] = win
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
;
7946 wa
[1] = XCB_EVENT_MASK_ENTER_WINDOW
| XCB_EVENT_MASK_PROPERTY_CHANGE
|
7947 XCB_EVENT_MASK_STRUCTURE_NOTIFY
;
7949 wa
[1] |= XCB_EVENT_MASK_LEAVE_WINDOW
| XCB_EVENT_MASK_FOCUS_CHANGE
;
7952 xcb_change_window_attributes(conn
, win
->id
, XCB_CW_BORDER_PIXEL
|
7953 XCB_CW_EVENT_MASK
, wa
);
7956 /* Figure out where to stack the window in the workspace. */
7957 if (trans
&& (ww
= find_window(trans
)))
7958 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, ww
, win
, entry
);
7959 else if (win
->ws
->focus
&& spawn_position
== SWM_STACK_ABOVE
)
7960 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, win
->ws
->focus
, win
,
7962 else if (win
->ws
->focus
&& spawn_position
== SWM_STACK_BELOW
)
7963 TAILQ_INSERT_BEFORE(win
->ws
->focus
, win
, entry
);
7964 else switch (spawn_position
) {
7967 case SWM_STACK_ABOVE
:
7968 TAILQ_INSERT_TAIL(&win
->ws
->winlist
, win
, entry
);
7970 case SWM_STACK_BOTTOM
:
7971 case SWM_STACK_BELOW
:
7972 TAILQ_INSERT_HEAD(&win
->ws
->winlist
, win
, entry
);
7975 /* Get initial _NET_WM_STATE */
7976 ewmh_get_win_state(win
);
7977 /* Set initial _NET_WM_ALLOWED_ACTIONS */
7978 ewmh_update_actions(win
);
7982 DNPRINTF(SWM_D_MISC
, "manage_window: done. window: 0x%x, (x,y) w x h: "
7983 "(%d,%d) %d x %d, ws: %d, iconic: %s, transient: 0x%x\n", win
->id
,
7984 X(win
), Y(win
), WIDTH(win
), HEIGHT(win
), win
->ws
->idx
,
7985 YESNO(win
->iconic
), win
->transient
);
7991 free_window(struct ws_win
*win
)
7993 DNPRINTF(SWM_D_MISC
, "free_window: window: 0x%x\n", win
->id
);
7998 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
8003 xcb_icccm_get_wm_class_reply_wipe(&win
->ch
);
8008 memset(win
, 0xff, sizeof *win
); /* XXX kill later */
8011 DNPRINTF(SWM_D_MISC
, "free_window: done.\n");
8015 unmanage_window(struct ws_win
*win
)
8017 struct ws_win
*parent
;
8022 DNPRINTF(SWM_D_MISC
, "unmanage_window: window: 0x%x\n", win
->id
);
8024 if (win
->transient
) {
8025 parent
= find_window(win
->transient
);
8027 parent
->focus_child
= NULL
;
8030 TAILQ_REMOVE(&win
->ws
->winlist
, win
, entry
);
8031 TAILQ_INSERT_TAIL(&win
->ws
->unmanagedlist
, win
, entry
);
8035 expose(xcb_expose_event_t
*e
)
8038 struct swm_region
*r
;
8040 DNPRINTF(SWM_D_EVENT
, "expose: window: 0x%x\n", e
->window
);
8042 num_screens
= get_screen_count();
8043 for (i
= 0; i
< num_screens
; i
++)
8044 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
8045 if (e
->window
== WINID(r
->bar
))
8053 focusin(xcb_focus_in_event_t
*e
)
8055 DNPRINTF(SWM_D_EVENT
, "focusin: window: 0x%x, mode: %s(%u), "
8056 "detail: %s(%u)\n", e
->event
, get_notify_mode_label(e
->mode
),
8057 e
->mode
, get_notify_detail_label(e
->detail
), e
->detail
);
8061 focusout(xcb_focus_out_event_t
*e
)
8063 DNPRINTF(SWM_D_EVENT
, "focusout: window: 0x%x, mode: %s(%u), "
8064 "detail: %s(%u)\n", e
->event
, get_notify_mode_label(e
->mode
),
8065 e
->mode
, get_notify_detail_label(e
->detail
), e
->detail
);
8070 keypress(xcb_key_press_event_t
*e
)
8072 xcb_keysym_t keysym
;
8075 keysym
= xcb_key_press_lookup_keysym(syms
, e
, 0);
8077 DNPRINTF(SWM_D_EVENT
, "keypress: keysym: %u, win (x,y): 0x%x (%d,%d), "
8078 "detail: %u, time: %u, root (x,y): 0x%x (%d,%d), child: 0x%x, "
8079 "state: %u, same_screen: %s\n", keysym
, e
->event
, e
->event_x
,
8080 e
->event_y
, e
->detail
, e
->time
, e
->root
, e
->root_x
, e
->root_y
,
8081 e
->child
, e
->state
, YESNO(e
->same_screen
));
8083 if ((kp
= key_lookup(CLEANMASK(e
->state
), keysym
)) == NULL
)
8086 last_event_time
= e
->time
;
8088 if (kp
->funcid
== KF_SPAWN_CUSTOM
)
8089 spawn_custom(root_to_region(e
->root
, SWM_CK_ALL
),
8090 &(keyfuncs
[kp
->funcid
].args
), kp
->spawn_name
);
8091 else if (keyfuncs
[kp
->funcid
].func
)
8092 keyfuncs
[kp
->funcid
].func(root_to_region(e
->root
, SWM_CK_ALL
),
8093 &(keyfuncs
[kp
->funcid
].args
));
8096 /* Unfreeze grab events. */
8097 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
, e
->time
);
8100 DNPRINTF(SWM_D_EVENT
, "keypress: done.\n");
8104 buttonpress(xcb_button_press_event_t
*e
)
8106 struct ws_win
*win
= NULL
;
8107 struct swm_region
*r
, *old_r
;
8111 DNPRINTF(SWM_D_EVENT
, "buttonpress: win (x,y): 0x%x (%d,%d), "
8112 "detail: %u, time: %u, root (x,y): 0x%x (%d,%d), child: 0x%x, "
8113 "state: %u, same_screen: %s\n", e
->event
, e
->event_x
, e
->event_y
,
8114 e
->detail
, e
->time
, e
->root
, e
->root_x
, e
->root_y
, e
->child
,
8115 e
->state
, YESNO(e
->same_screen
));
8117 if (e
->event
== e
->root
) {
8118 if (e
->child
!= 0) {
8119 win
= find_window(e
->child
);
8120 /* Pass ButtonPress to window if it isn't managed. */
8124 /* Focus on empty region */
8125 /* If no windows on region if its empty. */
8126 r
= root_to_region(e
->root
, SWM_CK_POINTER
);
8128 DNPRINTF(SWM_D_EVENT
, "buttonpress: "
8129 "NULL region; ignoring.\n");
8133 if (TAILQ_EMPTY(&r
->ws
->winlist
)) {
8134 old_r
= root_to_region(e
->root
, SWM_CK_FOCUS
);
8135 if (old_r
&& old_r
!= r
)
8136 unfocus_win(old_r
->ws
->focus
);
8138 xcb_set_input_focus(conn
,
8139 XCB_INPUT_FOCUS_PARENT
, e
->root
, e
->time
);
8141 /* Clear bar since empty. */
8149 win
= find_window(e
->event
);
8155 last_event_time
= e
->time
;
8157 focus_win(get_focus_magic(win
));
8159 for (i
= 0; i
< LENGTH(buttons
); i
++)
8160 if (client_click
== buttons
[i
].action
&& buttons
[i
].func
&&
8161 buttons
[i
].button
== e
->detail
&&
8162 CLEANMASK(buttons
[i
].mask
) == CLEANMASK(e
->state
)) {
8163 buttons
[i
].func(win
, &buttons
[i
].args
);
8169 DNPRINTF(SWM_D_EVENT
, "buttonpress: passing to window.\n");
8170 /* Replay event to event window */
8171 xcb_allow_events(conn
, XCB_ALLOW_REPLAY_POINTER
, e
->time
);
8173 DNPRINTF(SWM_D_EVENT
, "buttonpress: handled.\n");
8174 /* Unfreeze grab events. */
8175 xcb_allow_events(conn
, XCB_ALLOW_SYNC_POINTER
, e
->time
);
8183 print_win_geom(xcb_window_t w
)
8185 xcb_get_geometry_reply_t
*wa
;
8187 wa
= xcb_get_geometry_reply(conn
, xcb_get_geometry(conn
, w
), NULL
);
8190 DNPRINTF(SWM_D_MISC
, "print_win_geom: window not found: 0x%x\n",
8195 DNPRINTF(SWM_D_MISC
, "print_win_geom: window: 0x%x, root: 0x%x, "
8196 "depth: %u, (x,y) w x h: (%d,%d) %d x %d, border: %d\n",
8197 w
, wa
->root
, wa
->depth
, wa
->x
, wa
->y
, wa
->width
, wa
->height
,
8206 get_stack_mode_name(uint8_t mode
)
8211 case XCB_STACK_MODE_ABOVE
:
8214 case XCB_STACK_MODE_BELOW
:
8217 case XCB_STACK_MODE_TOP_IF
:
8220 case XCB_STACK_MODE_BOTTOM_IF
:
8223 case XCB_STACK_MODE_OPPOSITE
:
8235 configurerequest(xcb_configure_request_event_t
*e
)
8238 struct swm_region
*r
= NULL
;
8241 uint32_t wc
[7] = {0};
8243 if ((win
= find_window(e
->window
)) == NULL
)
8244 if ((win
= find_unmanaged_window(e
->window
)) == NULL
)
8248 if (swm_debug
& SWM_D_EVENT
) {
8249 print_win_geom(e
->window
);
8251 DNPRINTF(SWM_D_EVENT
, "configurerequest: window: 0x%x, "
8252 "parent: 0x%x, new: %s, value_mask: %u { ", e
->window
,
8253 e
->parent
, YESNO(new), e
->value_mask
);
8254 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
)
8255 DPRINTF("X: %d ", e
->x
);
8256 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
)
8257 DPRINTF("Y: %d ", e
->y
);
8258 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
)
8259 DPRINTF("W: %u ", e
->width
);
8260 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
)
8261 DPRINTF("H: %u ", e
->height
);
8262 if (e
->value_mask
& XCB_CONFIG_WINDOW_BORDER_WIDTH
)
8263 DPRINTF("Border: %u ", e
->border_width
);
8264 if (e
->value_mask
& XCB_CONFIG_WINDOW_SIBLING
)
8265 DPRINTF("Sibling: 0x%x ", e
->sibling
);
8266 if (e
->value_mask
& XCB_CONFIG_WINDOW_STACK_MODE
)
8267 DPRINTF("StackMode: %s(%u) ",
8268 get_stack_mode_name(e
->stack_mode
), e
->stack_mode
);
8274 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
) {
8275 mask
|= XCB_CONFIG_WINDOW_X
;
8278 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
) {
8279 mask
|= XCB_CONFIG_WINDOW_Y
;
8282 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
) {
8283 mask
|= XCB_CONFIG_WINDOW_WIDTH
;
8286 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
) {
8287 mask
|= XCB_CONFIG_WINDOW_HEIGHT
;
8288 wc
[i
++] = e
->height
;
8290 if (e
->value_mask
& XCB_CONFIG_WINDOW_BORDER_WIDTH
) {
8291 mask
|= XCB_CONFIG_WINDOW_BORDER_WIDTH
;
8292 wc
[i
++] = e
->border_width
;
8294 if (e
->value_mask
& XCB_CONFIG_WINDOW_SIBLING
) {
8295 mask
|= XCB_CONFIG_WINDOW_SIBLING
;
8296 wc
[i
++] = e
->sibling
;
8298 if (e
->value_mask
& XCB_CONFIG_WINDOW_STACK_MODE
) {
8299 mask
|= XCB_CONFIG_WINDOW_STACK_MODE
;
8300 wc
[i
++] = e
->stack_mode
;
8304 xcb_configure_window(conn
, e
->window
, mask
, wc
);
8307 } else if ((!win
->manual
|| win
->quirks
& SWM_Q_ANYWHERE
) &&
8308 !(win
->ewmh_flags
& EWMH_F_FULLSCREEN
)) {
8311 else if (win
->ws
->old_r
)
8314 /* windows are centered unless ANYWHERE quirk is set. */
8315 if (win
->quirks
& SWM_Q_ANYWHERE
) {
8316 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
) {
8317 win
->g_float
.x
= e
->x
;
8319 win
->g_float
.x
-= X(r
);
8322 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
) {
8323 win
->g_float
.y
= e
->y
;
8325 win
->g_float
.y
-= Y(r
);
8329 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
)
8330 win
->g_float
.w
= e
->width
;
8332 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
)
8333 win
->g_float
.h
= e
->height
;
8335 win
->g_floatvalid
= 1;
8337 if (win
->floating
&& r
&& (win
->transient
||
8338 win
->ws
->cur_layout
!= &layouts
[SWM_MAX_STACK
])) {
8339 WIDTH(win
) = win
->g_float
.w
;
8340 HEIGHT(win
) = win
->g_float
.h
;
8343 stack_floater(win
, r
);
8355 DNPRINTF(SWM_D_EVENT
, "configurerequest: done.\n");
8359 configurenotify(xcb_configure_notify_event_t
*e
)
8363 DNPRINTF(SWM_D_EVENT
, "configurenotify: win 0x%x, event win: 0x%x, "
8364 "(x,y) WxH: (%d,%d) %ux%u, border: %u, above_sibling: 0x%x, "
8365 "override_redirect: %s\n", e
->window
, e
->event
, e
->x
, e
->y
,
8366 e
->width
, e
->height
, e
->border_width
, e
->above_sibling
,
8367 YESNO(e
->override_redirect
));
8369 win
= find_window(e
->window
);
8371 xcb_icccm_get_wm_normal_hints_reply(conn
,
8372 xcb_icccm_get_wm_normal_hints(conn
, win
->id
),
8375 if (font_adjusted
) {
8383 destroynotify(xcb_destroy_notify_event_t
*e
)
8387 DNPRINTF(SWM_D_EVENT
, "destroynotify: window: 0x%x\n", e
->window
);
8389 if ((win
= find_window(e
->window
)) == NULL
) {
8390 if ((win
= find_unmanaged_window(e
->window
)) == NULL
)
8396 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
8397 /* If we were focused, make sure we focus on something else. */
8398 if (win
== win
->ws
->focus
)
8399 win
->ws
->focus_pending
= get_focus_prev(win
);
8402 unmanage_window(win
);
8405 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
8406 if (win
->ws
->focus_pending
) {
8407 focus_win(win
->ws
->focus_pending
);
8408 win
->ws
->focus_pending
= NULL
;
8409 } else if (win
== win
->ws
->focus
&& win
->ws
->r
) {
8410 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
,
8411 win
->ws
->r
->id
, XCB_CURRENT_TIME
);
8422 get_notify_detail_label(uint8_t detail
)
8427 case XCB_NOTIFY_DETAIL_ANCESTOR
:
8430 case XCB_NOTIFY_DETAIL_VIRTUAL
:
8433 case XCB_NOTIFY_DETAIL_INFERIOR
:
8436 case XCB_NOTIFY_DETAIL_NONLINEAR
:
8437 label
= "Nonlinear";
8439 case XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL
:
8440 label
= "NonlinearVirtual";
8442 case XCB_NOTIFY_DETAIL_POINTER
:
8445 case XCB_NOTIFY_DETAIL_POINTER_ROOT
:
8446 label
= "PointerRoot";
8448 case XCB_NOTIFY_DETAIL_NONE
:
8459 get_notify_mode_label(uint8_t mode
)
8464 case XCB_NOTIFY_MODE_NORMAL
:
8467 case XCB_NOTIFY_MODE_GRAB
:
8470 case XCB_NOTIFY_MODE_UNGRAB
:
8473 case XCB_NOTIFY_MODE_WHILE_GRABBED
:
8474 label
= "WhileGrabbed";
8485 enternotify(xcb_enter_notify_event_t
*e
)
8488 struct swm_region
*r
;
8490 DNPRINTF(SWM_D_FOCUS
, "enternotify: time: %u, win (x,y): 0x%x "
8491 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): 0x%x (%d,%d), "
8492 "child: 0x%x, same_screen_focus: %s, state: %d\n",
8493 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
8494 get_notify_mode_label(e
->mode
), e
->mode
,
8495 get_notify_detail_label(e
->detail
), e
->detail
,
8496 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
8497 YESNO(e
->same_screen_focus
), e
->state
);
8499 if (focus_mode
== SWM_FOCUS_MANUAL
&&
8500 e
->mode
== XCB_NOTIFY_MODE_NORMAL
) {
8501 DNPRINTF(SWM_D_EVENT
, "enternotify: manual focus; ignoring.\n");
8505 last_event_time
= e
->time
;
8507 if ((win
= find_window(e
->event
)) == NULL
) {
8508 if (e
->event
== e
->root
) {
8509 /* If no windows on pointer region, then focus root. */
8510 r
= root_to_region(e
->root
, SWM_CK_POINTER
);
8512 DNPRINTF(SWM_D_EVENT
, "enternotify: "
8513 "NULL region; ignoring.\n");
8519 DNPRINTF(SWM_D_EVENT
, "enternotify: window is NULL; "
8524 focus_win(get_focus_magic(win
));
8532 leavenotify(xcb_leave_notify_event_t
*e
)
8534 DNPRINTF(SWM_D_FOCUS
, "leavenotify: time: %u, win (x,y): 0x%x "
8535 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): 0x%x (%d,%d), "
8536 "child: 0x%x, same_screen_focus: %s, state: %d\n",
8537 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
8538 get_notify_mode_label(e
->mode
), e
->mode
,
8539 get_notify_detail_label(e
->detail
), e
->detail
,
8540 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
8541 YESNO(e
->same_screen_focus
), e
->state
);
8546 mapnotify(xcb_map_notify_event_t
*e
)
8550 DNPRINTF(SWM_D_EVENT
, "mapnotify: window: 0x%x\n", e
->window
);
8552 if ((win
= find_window(e
->window
)) == NULL
)
8553 win
= manage_window(e
->window
, 1);
8556 set_win_state(win
, XCB_ICCCM_WM_STATE_NORMAL
);
8558 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
8559 if (win
->ws
->focus_pending
== win
) {
8561 win
->ws
->focus_pending
= NULL
;
8570 mappingnotify(xcb_mapping_notify_event_t
*e
)
8572 xcb_refresh_keyboard_mapping(syms
, e
);
8574 if (e
->request
== XCB_MAPPING_KEYBOARD
)
8579 maprequest(xcb_map_request_event_t
*e
)
8581 struct ws_win
*win
, *w
= NULL
;
8582 xcb_get_window_attributes_reply_t
*war
;
8584 DNPRINTF(SWM_D_EVENT
, "maprequest: win 0x%x\n",
8587 war
= xcb_get_window_attributes_reply(conn
,
8588 xcb_get_window_attributes(conn
, e
->window
),
8591 DNPRINTF(SWM_D_EVENT
, "maprequest: window lost.\n");
8595 if (war
->override_redirect
) {
8596 DNPRINTF(SWM_D_EVENT
, "maprequest: override_redirect; "
8601 win
= manage_window(e
->window
,
8602 (war
->map_state
== XCB_MAP_STATE_VIEWABLE
));
8604 /* The new window should get focus; prepare. */
8605 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
8606 !(win
->quirks
& SWM_Q_NOFOCUSONMAP
)) {
8607 if (win
->quirks
& SWM_Q_FOCUSONMAP_SINGLE
) {
8608 /* See if other wins of same type are already mapped. */
8609 TAILQ_FOREACH(w
, &win
->ws
->winlist
, entry
) {
8610 if (w
== win
|| !w
->mapped
)
8613 if (w
->ch
.class_name
&&
8614 win
->ch
.class_name
&&
8615 !strcmp(w
->ch
.class_name
,
8616 win
->ch
.class_name
) &&
8617 w
->ch
.instance_name
&&
8618 win
->ch
.instance_name
&&
8619 !strcmp(w
->ch
.instance_name
,
8620 win
->ch
.instance_name
))
8626 win
->ws
->focus_pending
= get_focus_magic(win
);
8629 /* All windows need to be mapped if they are in the current workspace.*/
8633 /* Ignore EnterNotify to handle the mapnotify without interference. */
8634 if (focus_mode
== SWM_FOCUS_DEFAULT
)
8635 event_drain(XCB_ENTER_NOTIFY
);
8638 DNPRINTF(SWM_D_EVENT
, "maprequest: done.\n");
8642 motionnotify(xcb_motion_notify_event_t
*e
)
8644 struct swm_region
*r
;
8647 DNPRINTF(SWM_D_FOCUS
, "motionnotify: time: %u, win (x,y): 0x%x "
8648 "(%d,%d), detail: %s(%d), root (x,y): 0x%x (%d,%d), "
8649 "child: 0x%x, same_screen_focus: %s, state: %d\n",
8650 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
8651 get_notify_detail_label(e
->detail
), e
->detail
,
8652 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
8653 YESNO(e
->same_screen
), e
->state
);
8655 last_event_time
= e
->time
;
8657 if (focus_mode
== SWM_FOCUS_MANUAL
)
8660 num_screens
= get_screen_count();
8661 for (i
= 0; i
< num_screens
; i
++)
8662 if (screens
[i
].root
== e
->root
)
8665 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
8666 if (X(r
) <= e
->root_x
&& e
->root_x
< MAX_X(r
) &&
8667 Y(r
) <= e
->root_y
&& e
->root_y
< MAX_Y(r
))
8675 get_atom_name(xcb_atom_t atom
)
8680 * This should be disabled during most debugging since
8681 * xcb_get_* causes an xcb_flush.
8684 xcb_get_atom_name_reply_t
*r
;
8686 r
= xcb_get_atom_name_reply(conn
,
8687 xcb_get_atom_name(conn
, atom
),
8690 len
= xcb_get_atom_name_name_length(r
);
8692 name
= malloc(len
+ 1);
8694 memcpy(name
, xcb_get_atom_name_name(r
), len
);
8708 propertynotify(xcb_property_notify_event_t
*e
)
8711 struct workspace
*ws
;
8715 name
= get_atom_name(e
->atom
);
8716 DNPRINTF(SWM_D_EVENT
, "propertynotify: window: 0x%x, atom: %s(%u), "
8717 "time: %#x, state: %u\n", e
->window
, name
, e
->atom
, e
->time
,
8721 win
= find_window(e
->window
);
8727 last_event_time
= e
->time
;
8729 if (e
->atom
== a_swm_iconic
) {
8730 if (e
->state
== XCB_PROPERTY_NEW_VALUE
) {
8731 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
8732 ws
->focus_pending
= get_focus_prev(win
);
8740 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
8741 if (ws
->focus_pending
) {
8742 focus_win(ws
->focus_pending
);
8743 ws
->focus_pending
= NULL
;
8745 xcb_set_input_focus(conn
,
8746 XCB_INPUT_FOCUS_PARENT
,
8754 } else if (e
->state
== XCB_PROPERTY_DELETE
) {
8755 /* The window is no longer iconic, restack ws. */
8756 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
8757 ws
->focus_pending
= get_focus_magic(win
);
8761 /* Flush EnterNotify for mapnotify, if needed. */
8764 } else if (e
->atom
== a_state
) {
8765 /* State just changed, make sure it gets focused if mapped. */
8766 if (e
->state
== XCB_PROPERTY_NEW_VALUE
) {
8767 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
8769 ws
->focus_pending
== win
) {
8770 focus_win(ws
->focus_pending
);
8771 ws
->focus_pending
= NULL
;
8775 } else if (e
->atom
== XCB_ATOM_WM_CLASS
||
8776 e
->atom
== XCB_ATOM_WM_NAME
) {
8784 unmapnotify(xcb_unmap_notify_event_t
*e
)
8787 struct workspace
*ws
;
8789 DNPRINTF(SWM_D_EVENT
, "unmapnotify: window: 0x%x\n", e
->window
);
8791 /* If we aren't managing the window, then ignore. */
8792 win
= find_window(e
->window
);
8793 if (win
== NULL
|| win
->id
!= e
->window
)
8798 if (getstate(e
->window
) != XCB_ICCCM_WM_STATE_ICONIC
)
8799 set_win_state(win
, XCB_ICCCM_WM_STATE_ICONIC
);
8802 /* window unmapped itself */
8803 /* do unmap/unfocus/restack and unmanage */
8806 /* If win was focused, make sure to focus on something else. */
8807 if (win
== ws
->focus
) {
8808 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
8809 ws
->focus_pending
= get_focus_prev(win
);
8810 DNPRINTF(SWM_D_EVENT
, "unmapnotify: "
8811 "focus_pending: 0x%x\n",
8812 WINID(ws
->focus_pending
));
8818 unmanage_window(win
);
8823 if (focus_mode
== SWM_FOCUS_FOLLOW
) {
8825 focus_win(get_pointer_win(ws
->r
->s
->root
));
8826 } else if (ws
->focus_pending
) {
8827 focus_win(ws
->focus_pending
);
8828 ws
->focus_pending
= NULL
;
8829 } else if (ws
->focus
== NULL
&& ws
->r
) {
8830 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
,
8831 ws
->r
->id
, XCB_CURRENT_TIME
);
8835 if (getstate(e
->window
) == XCB_ICCCM_WM_STATE_NORMAL
)
8836 set_win_state(win
, XCB_ICCCM_WM_STATE_ICONIC
);
8843 visibilitynotify(xcb_visibility_notify_event_t
*e
)
8845 DNPRINTF(SWM_D_EVENT
, "visibilitynotify: window: 0x%x\n",
8851 clientmessage(xcb_client_message_event_t
*e
)
8854 xcb_map_request_event_t mre
;
8858 name
= get_atom_name(e
->type
);
8859 DNPRINTF(SWM_D_EVENT
, "clientmessage: window: 0x%x, atom: %s(%u)\n",
8860 e
->window
, name
, e
->type
);
8863 win
= find_window(e
->window
);
8866 if (e
->type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
8867 /* Manage the window with maprequest. */
8868 DNPRINTF(SWM_D_EVENT
, "clientmessage: request focus on "
8869 "unmanaged window.\n");
8870 mre
.window
= e
->window
;
8876 if (e
->type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
8877 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_ACTIVE_WINDOW\n");
8880 if (e
->type
== ewmh
[_NET_CLOSE_WINDOW
].atom
) {
8881 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_CLOSE_WINDOW\n");
8882 if (win
->can_delete
)
8883 client_msg(win
, a_delete
, 0);
8885 xcb_kill_client(conn
, win
->id
);
8887 if (e
->type
== ewmh
[_NET_MOVERESIZE_WINDOW
].atom
) {
8888 DNPRINTF(SWM_D_EVENT
,
8889 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
8890 if (win
->floating
) {
8891 if (e
->data
.data32
[0] & (1<<8)) /* x */
8892 X(win
) = e
->data
.data32
[1];
8893 if (e
->data
.data32
[0] & (1<<9)) /* y */
8894 Y(win
) = e
->data
.data32
[2];
8895 if (e
->data
.data32
[0] & (1<<10)) /* width */
8896 WIDTH(win
) = e
->data
.data32
[3];
8897 if (e
->data
.data32
[0] & (1<<11)) /* height */
8898 HEIGHT(win
) = e
->data
.data32
[4];
8903 /* TODO: Change stack sizes */
8904 /* notify no change was made. */
8905 config_win(win
, NULL
);
8908 if (e
->type
== ewmh
[_NET_WM_STATE
].atom
) {
8909 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_WM_STATE\n");
8910 ewmh_update_win_state(win
, e
->data
.data32
[1], e
->data
.data32
[0]);
8911 if (e
->data
.data32
[2])
8912 ewmh_update_win_state(win
, e
->data
.data32
[2],
8924 int errcode
= xcb_connection_has_error(conn
);
8925 #ifdef XCB_CONN_ERROR
8928 case XCB_CONN_ERROR
:
8929 s
= "Socket error, pipe error or other stream error.";
8931 case XCB_CONN_CLOSED_EXT_NOTSUPPORTED
:
8932 s
= "Extension not supported.";
8934 case XCB_CONN_CLOSED_MEM_INSUFFICIENT
:
8935 s
= "Insufficient memory.";
8937 case XCB_CONN_CLOSED_REQ_LEN_EXCEED
:
8938 s
= "Request length was exceeded.";
8940 case XCB_CONN_CLOSED_PARSE_ERR
:
8941 s
= "Error parsing display string.";
8944 s
= "Unknown error.";
8947 errx(errcode
, "X CONNECTION ERROR: %s", s
);
8950 errx(errcode
, "X CONNECTION ERROR");
8958 const uint32_t val
= XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT
|
8959 XCB_EVENT_MASK_ENTER_WINDOW
;
8961 xcb_void_cookie_t wac
;
8962 xcb_generic_error_t
*error
;
8964 /* this causes an error if some other window manager is running */
8965 num_screens
= get_screen_count();
8966 for (i
= 0; i
< num_screens
; i
++) {
8967 if ((sc
= get_screen(i
)) == NULL
)
8968 errx(1, "ERROR: can't get screen %d.", i
);
8969 DNPRINTF(SWM_D_INIT
, "enable_wm: screen %d, root: 0x%x\n",
8971 wac
= xcb_change_window_attributes_checked(conn
, sc
->root
,
8972 XCB_CW_EVENT_MASK
, &val
);
8973 if ((error
= xcb_request_check(conn
, wac
))) {
8974 DNPRINTF(SWM_D_INIT
, "enable_wm: error_code: %u\n",
8980 /* click to focus on empty region */
8981 xcb_grab_button(conn
, 1, sc
->root
, BUTTONMASK
,
8982 XCB_GRAB_MODE_SYNC
, XCB_GRAB_MODE_ASYNC
, XCB_WINDOW_NONE
,
8983 XCB_CURSOR_NONE
, XCB_BUTTON_INDEX_1
, XCB_BUTTON_MASK_ANY
);
8990 new_region(struct swm_screen
*s
, int x
, int y
, int w
, int h
)
8992 struct swm_region
*r
, *n
;
8993 struct workspace
*ws
= NULL
;
8997 DNPRINTF(SWM_D_MISC
, "new region: screen[%d]:%dx%d+%d+%d\n",
8998 s
->idx
, w
, h
, x
, y
);
9000 /* remove any conflicting regions */
9001 n
= TAILQ_FIRST(&s
->rl
);
9004 n
= TAILQ_NEXT(r
, entry
);
9005 if (X(r
) < (x
+ w
) && (X(r
) + WIDTH(r
)) > x
&&
9006 Y(r
) < (y
+ h
) && (Y(r
) + HEIGHT(r
)) > y
) {
9007 if (r
->ws
->r
!= NULL
)
9008 r
->ws
->old_r
= r
->ws
->r
;
9011 xcb_destroy_window(conn
, r
->id
);
9012 TAILQ_REMOVE(&s
->rl
, r
, entry
);
9013 TAILQ_INSERT_TAIL(&s
->orl
, r
, entry
);
9017 /* search old regions for one to reuse */
9019 /* size + location match */
9020 TAILQ_FOREACH(r
, &s
->orl
, entry
)
9021 if (X(r
) == x
&& Y(r
) == y
&&
9022 HEIGHT(r
) == h
&& WIDTH(r
) == w
)
9026 TAILQ_FOREACH(r
, &s
->orl
, entry
)
9027 if (HEIGHT(r
) == h
&& WIDTH(r
) == w
)
9031 TAILQ_REMOVE(&s
->orl
, r
, entry
);
9032 /* try to use old region's workspace */
9033 if (r
->ws
->r
== NULL
)
9036 if ((r
= calloc(1, sizeof(struct swm_region
))) == NULL
)
9037 err(1, "new_region: calloc: failed to allocate memory "
9040 /* if we don't have a workspace already, find one */
9042 for (i
= 0; i
< workspace_limit
; i
++)
9043 if (s
->ws
[i
].r
== NULL
) {
9050 errx(1, "new_region: no free workspaces");
9061 TAILQ_INSERT_TAIL(&s
->rl
, r
, entry
);
9063 /* Invisible region window to detect pointer events on empty regions. */
9064 r
->id
= xcb_generate_id(conn
);
9065 wa
[0] = XCB_EVENT_MASK_POINTER_MOTION
|
9066 XCB_EVENT_MASK_POINTER_MOTION_HINT
;
9068 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, r
->id
, r
->s
->root
,
9069 X(r
), Y(r
), WIDTH(r
), HEIGHT(r
), 0, XCB_WINDOW_CLASS_INPUT_ONLY
,
9070 XCB_COPY_FROM_PARENT
, XCB_CW_EVENT_MASK
, wa
);
9072 xcb_map_window(conn
, r
->id
);
9078 #ifdef SWM_XRR_HAS_CRTC
9081 #endif /* SWM_XRR_HAS_CRTC */
9082 struct swm_region
*r
;
9085 xcb_randr_get_screen_resources_current_cookie_t src
;
9086 xcb_randr_get_screen_resources_current_reply_t
*srr
;
9087 xcb_randr_get_crtc_info_cookie_t cic
;
9088 xcb_randr_get_crtc_info_reply_t
*cir
= NULL
;
9089 xcb_randr_crtc_t
*crtc
;
9090 xcb_screen_t
*screen
;
9092 DNPRINTF(SWM_D_MISC
, "scan_xrandr: screen: %d\n", i
);
9094 if ((screen
= get_screen(i
)) == NULL
)
9095 errx(1, "ERROR: can't get screen %d.", i
);
9097 num_screens
= get_screen_count();
9098 if (i
>= num_screens
)
9099 errx(1, "scan_xrandr: invalid screen");
9101 /* remove any old regions */
9102 while ((r
= TAILQ_FIRST(&screens
[i
].rl
)) != NULL
) {
9103 r
->ws
->old_r
= r
->ws
->r
= NULL
;
9105 xcb_destroy_window(conn
, r
->id
);
9106 TAILQ_REMOVE(&screens
[i
].rl
, r
, entry
);
9107 TAILQ_INSERT_TAIL(&screens
[i
].orl
, r
, entry
);
9111 /* map virtual screens onto physical screens */
9112 #ifdef SWM_XRR_HAS_CRTC
9113 if (xrandr_support
) {
9114 src
= xcb_randr_get_screen_resources_current(conn
,
9116 srr
= xcb_randr_get_screen_resources_current_reply(conn
, src
,
9119 new_region(&screens
[i
], 0, 0,
9120 screen
->width_in_pixels
,
9121 screen
->height_in_pixels
);
9124 ncrtc
= srr
->num_crtcs
;
9126 crtc
= xcb_randr_get_screen_resources_current_crtcs(srr
);
9127 for (c
= 0; c
< ncrtc
; c
++) {
9128 cic
= xcb_randr_get_crtc_info(conn
, crtc
[c
],
9130 cir
= xcb_randr_get_crtc_info_reply(conn
, cic
, NULL
);
9133 if (cir
->num_outputs
== 0) {
9139 new_region(&screens
[i
], 0, 0,
9140 screen
->width_in_pixels
,
9141 screen
->height_in_pixels
);
9143 new_region(&screens
[i
],
9144 cir
->x
, cir
->y
, cir
->width
, cir
->height
);
9149 #endif /* SWM_XRR_HAS_CRTC */
9151 /* If detection failed, create a single region that spans the screen. */
9152 if (TAILQ_EMPTY(&screens
[i
].rl
))
9153 new_region(&screens
[i
], 0, 0, screen
->width_in_pixels
,
9154 screen
->height_in_pixels
);
9157 /* Cleanup unused previously visible workspaces. */
9158 TAILQ_FOREACH(r
, &screens
[i
].orl
, entry
) {
9159 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
)
9162 /* The screen shouldn't focus on an unused region. */
9163 if (screens
[i
].r_focus
== r
)
9164 screens
[i
].r_focus
= NULL
;
9167 DNPRINTF(SWM_D_MISC
, "scan_xrandr: done.\n");
9171 screenchange(xcb_randr_screen_change_notify_event_t
*e
)
9173 struct swm_region
*r
;
9176 DNPRINTF(SWM_D_EVENT
, "screenchange: root: 0x%x\n", e
->root
);
9178 num_screens
= get_screen_count();
9179 /* silly event doesn't include the screen index */
9180 for (i
= 0; i
< num_screens
; i
++)
9181 if (screens
[i
].root
== e
->root
)
9183 if (i
>= num_screens
)
9184 errx(1, "screenchange: screen not found");
9186 /* brute force for now, just re-enumerate the regions */
9190 print_win_geom(e
->root
);
9192 /* add bars to all regions */
9193 for (i
= 0; i
< num_screens
; i
++) {
9194 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
9200 /* Make sure a region has focus on each screen. */
9201 for (i
= 0; i
< num_screens
; i
++) {
9202 if (screens
[i
].r_focus
== NULL
) {
9203 r
= TAILQ_FIRST(&screens
[i
].rl
);
9216 struct swm_region
*r
= NULL
;
9217 xcb_window_t
*wins
= NULL
, trans
;
9219 int i
, j
, num_screens
;
9220 uint16_t state
, manage
, mapped
;
9222 xcb_query_tree_cookie_t qtc
;
9223 xcb_query_tree_reply_t
*qtr
;
9224 xcb_get_window_attributes_cookie_t gac
;
9225 xcb_get_window_attributes_reply_t
*gar
;
9226 xcb_get_property_cookie_t pc
;
9228 DNPRINTF(SWM_D_INIT
, "grab_windows: begin\n");
9229 num_screens
= get_screen_count();
9230 for (i
= 0; i
< num_screens
; i
++) {
9231 qtc
= xcb_query_tree(conn
, screens
[i
].root
);
9232 qtr
= xcb_query_tree_reply(conn
, qtc
, NULL
);
9235 wins
= xcb_query_tree_children(qtr
);
9236 no
= xcb_query_tree_children_length(qtr
);
9237 /* attach windows to a region */
9238 /* normal windows */
9239 DNPRINTF(SWM_D_INIT
, "grab_windows: grab top level windows.\n");
9240 for (j
= 0; j
< no
; j
++) {
9241 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
9242 if (r
->id
== wins
[j
]) {
9243 DNPRINTF(SWM_D_INIT
, "grab_windows: "
9244 "skip %#x; region input window.\n",
9253 gac
= xcb_get_window_attributes(conn
, wins
[j
]);
9254 gar
= xcb_get_window_attributes_reply(conn
, gac
, NULL
);
9256 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
9257 "doesn't exist.\n", wins
[j
]);
9261 if (gar
->override_redirect
) {
9262 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
9263 "override_redirect set.\n", wins
[j
]);
9268 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
9269 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
9271 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
9272 "is transient for %#x.\n", wins
[j
], trans
);
9277 state
= getstate(wins
[j
]);
9278 manage
= state
!= XCB_ICCCM_WM_STATE_WITHDRAWN
;
9279 mapped
= gar
->map_state
!= XCB_MAP_STATE_UNMAPPED
;
9280 if (mapped
|| manage
)
9281 manage_window(wins
[j
], mapped
);
9284 /* transient windows */
9285 DNPRINTF(SWM_D_INIT
, "grab_windows: grab transient windows.\n");
9286 for (j
= 0; j
< no
; j
++) {
9287 gac
= xcb_get_window_attributes(conn
, wins
[j
]);
9288 gar
= xcb_get_window_attributes_reply(conn
, gac
, NULL
);
9290 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
9291 "doesn't exist.\n", wins
[j
]);
9295 if (gar
->override_redirect
) {
9296 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
9297 "override_redirect set.\n", wins
[j
]);
9302 state
= getstate(wins
[j
]);
9303 manage
= state
!= XCB_ICCCM_WM_STATE_WITHDRAWN
;
9304 mapped
= gar
->map_state
!= XCB_MAP_STATE_UNMAPPED
;
9305 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
9306 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
9307 &trans
, NULL
) && manage
)
9308 manage_window(wins
[j
], mapped
);
9313 DNPRINTF(SWM_D_INIT
, "grab_windows: done.\n");
9319 int i
, j
, k
, num_screens
;
9320 struct workspace
*ws
;
9321 uint32_t gcv
[1], wa
[1];
9322 const xcb_query_extension_reply_t
*qep
;
9323 xcb_screen_t
*screen
;
9324 xcb_randr_query_version_cookie_t c
;
9325 xcb_randr_query_version_reply_t
*r
;
9327 num_screens
= get_screen_count();
9328 if ((screens
= calloc(num_screens
,
9329 sizeof(struct swm_screen
))) == NULL
)
9330 err(1, "setup_screens: calloc: failed to allocate memory for "
9333 /* initial Xrandr setup */
9335 qep
= xcb_get_extension_data(conn
, &xcb_randr_id
);
9337 c
= xcb_randr_query_version(conn
, 1, 1);
9338 r
= xcb_randr_query_version_reply(conn
, c
, NULL
);
9340 if (r
->major_version
>= 1) {
9342 xrandr_eventbase
= qep
->first_event
;
9348 wa
[0] = cursors
[XC_LEFT_PTR
].cid
;
9350 /* map physical screens */
9351 for (i
= 0; i
< num_screens
; i
++) {
9352 DNPRINTF(SWM_D_WS
, "setup_screens: init screen: %d\n", i
);
9354 screens
[i
].r_focus
= NULL
;
9356 TAILQ_INIT(&screens
[i
].rl
);
9357 TAILQ_INIT(&screens
[i
].orl
);
9358 if ((screen
= get_screen(i
)) == NULL
)
9359 errx(1, "ERROR: can't get screen %d.", i
);
9360 screens
[i
].root
= screen
->root
;
9362 /* set default colors */
9363 setscreencolor("red", i
+ 1, SWM_S_COLOR_FOCUS
);
9364 setscreencolor("rgb:88/88/88", i
+ 1, SWM_S_COLOR_UNFOCUS
);
9365 setscreencolor("rgb:00/80/80", i
+ 1, SWM_S_COLOR_BAR_BORDER
);
9366 setscreencolor("rgb:00/40/40", i
+ 1,
9367 SWM_S_COLOR_BAR_BORDER_UNFOCUS
);
9368 setscreencolor("black", i
+ 1, SWM_S_COLOR_BAR
);
9369 setscreencolor("rgb:a0/a0/a0", i
+ 1, SWM_S_COLOR_BAR_FONT
);
9371 /* create graphics context on screen */
9372 screens
[i
].bar_gc
= xcb_generate_id(conn
);
9374 xcb_create_gc(conn
, screens
[i
].bar_gc
, screens
[i
].root
,
9375 XCB_GC_GRAPHICS_EXPOSURES
, gcv
);
9377 /* set default cursor */
9378 xcb_change_window_attributes(conn
, screens
[i
].root
,
9381 /* init all workspaces */
9382 /* XXX these should be dynamically allocated too */
9383 for (j
= 0; j
< SWM_WS_MAX
; j
++) {
9384 ws
= &screens
[i
].ws
[j
];
9387 ws
->bar_enabled
= 1;
9389 ws
->focus_prev
= NULL
;
9390 ws
->focus_pending
= NULL
;
9393 TAILQ_INIT(&ws
->winlist
);
9394 TAILQ_INIT(&ws
->unmanagedlist
);
9396 for (k
= 0; layouts
[k
].l_stack
!= NULL
; k
++)
9397 if (layouts
[k
].l_config
!= NULL
)
9398 layouts
[k
].l_config(ws
,
9399 SWM_ARG_ID_STACKINIT
);
9400 ws
->cur_layout
= &layouts
[0];
9401 ws
->cur_layout
->l_string(ws
);
9407 xcb_randr_select_input(conn
, screens
[i
].root
,
9408 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE
);
9415 if ((bar_fonts
= strdup(SWM_BAR_FONTS
)) == NULL
)
9416 err(1, "setup_globals: strdup: failed to allocate memory.");
9418 if ((clock_format
= strdup("%a %b %d %R %Z %Y")) == NULL
)
9419 err(1, "setup_globals: strdup: failed to allocate memory.");
9421 if ((syms
= xcb_key_symbols_alloc(conn
)) == NULL
)
9422 errx(1, "unable to allocate key symbols");
9424 a_state
= get_atom_from_string("WM_STATE");
9425 a_prot
= get_atom_from_string("WM_PROTOCOLS");
9426 a_delete
= get_atom_from_string("WM_DELETE_WINDOW");
9427 a_takefocus
= get_atom_from_string("WM_TAKE_FOCUS");
9428 a_wmname
= get_atom_from_string("WM_NAME");
9429 a_netwmname
= get_atom_from_string("_NET_WM_NAME");
9430 a_utf8_string
= get_atom_from_string("UTF8_STRING");
9431 a_string
= get_atom_from_string("STRING");
9432 a_swm_iconic
= get_atom_from_string("_SWM_ICONIC");
9433 a_swm_ws
= get_atom_from_string("_SWM_WS");
9440 xcb_atom_t netwmcheck
;
9441 xcb_window_t root
, win
;
9443 /* work around sun jdk bugs, code from wmname */
9444 netwmcheck
= get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
9446 num_screens
= get_screen_count();
9447 for (i
= 0; i
< num_screens
; i
++) {
9448 root
= screens
[i
].root
;
9450 win
= xcb_generate_id(conn
);
9451 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, win
, root
,
9452 0, 0, 1, 1, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
9453 XCB_COPY_FROM_PARENT
, 0, NULL
);
9455 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, root
,
9456 netwmcheck
, XCB_ATOM_WINDOW
, 32, 1, &win
);
9457 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
9458 netwmcheck
, XCB_ATOM_WINDOW
, 32, 1, &win
);
9459 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
9460 a_netwmname
, a_utf8_string
, 8, strlen("LG3D"), "LG3D");
9465 shutdown_cleanup(void)
9469 /* disable alarm because the following code may not be interrupted */
9471 if (signal(SIGALRM
, SIG_IGN
) == SIG_ERR
)
9472 err(1, "can't disable alarm");
9481 num_screens
= get_screen_count();
9482 for (i
= 0; i
< num_screens
; ++i
) {
9483 if (screens
[i
].bar_gc
!= 0)
9484 xcb_free_gc(conn
, screens
[i
].bar_gc
);
9485 if (!bar_font_legacy
)
9486 XftColorFree(display
, DefaultVisual(display
, i
),
9487 DefaultColormap(display
, i
), &bar_font_color
);
9490 if (bar_font_legacy
)
9491 XFreeFontSet(display
, bar_fs
);
9493 XftFontClose(display
, bar_font
);
9496 xcb_key_symbols_free(syms
);
9498 xcb_disconnect(conn
);
9502 event_error(xcb_generic_error_t
*e
)
9506 DNPRINTF(SWM_D_EVENT
, "event_error: %s(%u) from %s(%u), sequence: %u, "
9507 "resource_id: %u, minor_code: %u\n",
9508 xcb_event_get_error_label(e
->error_code
), e
->error_code
,
9509 xcb_event_get_request_label(e
->major_code
), e
->major_code
,
9510 e
->sequence
, e
->resource_id
, e
->minor_code
);
9514 event_handle(xcb_generic_event_t
*evt
)
9516 uint8_t type
= XCB_EVENT_RESPONSE_TYPE(evt
);
9518 DNPRINTF(SWM_D_EVENT
, "XCB Event: %s(%d), seq %u\n",
9519 xcb_event_get_label(XCB_EVENT_RESPONSE_TYPE(evt
)),
9520 XCB_EVENT_RESPONSE_TYPE(evt
), evt
->sequence
);
9523 #define EVENT(type, callback) case type: callback((void *)evt); return
9524 EVENT(0, event_error
);
9525 EVENT(XCB_BUTTON_PRESS
, buttonpress
);
9526 /*EVENT(XCB_BUTTON_RELEASE, buttonpress);*/
9527 /*EVENT(XCB_CIRCULATE_NOTIFY, );*/
9528 /*EVENT(XCB_CIRCULATE_REQUEST, );*/
9529 EVENT(XCB_CLIENT_MESSAGE
, clientmessage
);
9530 /*EVENT(XCB_COLORMAP_NOTIFY, );*/
9531 EVENT(XCB_CONFIGURE_NOTIFY
, configurenotify
);
9532 EVENT(XCB_CONFIGURE_REQUEST
, configurerequest
);
9533 /*EVENT(XCB_CREATE_NOTIFY, );*/
9534 EVENT(XCB_DESTROY_NOTIFY
, destroynotify
);
9535 EVENT(XCB_ENTER_NOTIFY
, enternotify
);
9536 EVENT(XCB_EXPOSE
, expose
);
9538 EVENT(XCB_FOCUS_IN
, focusin
);
9539 EVENT(XCB_FOCUS_OUT
, focusout
);
9541 /*EVENT(XCB_GRAPHICS_EXPOSURE, );*/
9542 /*EVENT(XCB_GRAVITY_NOTIFY, );*/
9543 EVENT(XCB_KEY_PRESS
, keypress
);
9544 /*EVENT(XCB_KEY_RELEASE, keypress);*/
9545 /*EVENT(XCB_KEYMAP_NOTIFY, );*/
9547 EVENT(XCB_LEAVE_NOTIFY
, leavenotify
);
9549 EVENT(XCB_MAP_NOTIFY
, mapnotify
);
9550 EVENT(XCB_MAP_REQUEST
, maprequest
);
9551 EVENT(XCB_MAPPING_NOTIFY
, mappingnotify
);
9552 EVENT(XCB_MOTION_NOTIFY
, motionnotify
);
9553 /*EVENT(XCB_NO_EXPOSURE, );*/
9554 EVENT(XCB_PROPERTY_NOTIFY
, propertynotify
);
9555 /*EVENT(XCB_REPARENT_NOTIFY, );*/
9556 /*EVENT(XCB_RESIZE_REQUEST, );*/
9557 /*EVENT(XCB_SELECTION_CLEAR, );*/
9558 /*EVENT(XCB_SELECTION_NOTIFY, );*/
9559 /*EVENT(XCB_SELECTION_REQUEST, );*/
9560 EVENT(XCB_UNMAP_NOTIFY
, unmapnotify
);
9561 /*EVENT(XCB_VISIBILITY_NOTIFY, visibilitynotify);*/
9564 if (type
- xrandr_eventbase
== XCB_RANDR_SCREEN_CHANGE_NOTIFY
)
9565 screenchange((void *)evt
);
9569 main(int argc
, char *argv
[])
9571 struct swm_region
*r
;
9572 char conf
[PATH_MAX
], *cfile
= NULL
;
9574 int xfd
, i
, num_screens
, startup
= 1;
9575 struct sigaction sact
;
9576 xcb_generic_event_t
*evt
;
9580 int stdin_ready
= 0;
9583 /* suppress unused warning since var is needed */
9586 time_started
= time(NULL
);
9589 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION
, buildstr
);
9590 if (!setlocale(LC_CTYPE
, "") || !setlocale(LC_TIME
, ""))
9591 warnx("no locale support");
9593 /* handle some signals */
9594 bzero(&sact
, sizeof(sact
));
9595 sigemptyset(&sact
.sa_mask
);
9597 sact
.sa_handler
= sighdlr
;
9598 sigaction(SIGINT
, &sact
, NULL
);
9599 sigaction(SIGQUIT
, &sact
, NULL
);
9600 sigaction(SIGTERM
, &sact
, NULL
);
9601 sigaction(SIGHUP
, &sact
, NULL
);
9603 sact
.sa_handler
= sighdlr
;
9604 sact
.sa_flags
= SA_NOCLDSTOP
;
9605 sigaction(SIGCHLD
, &sact
, NULL
);
9607 if (!(display
= XOpenDisplay(0)))
9608 errx(1, "can not open display");
9610 conn
= XGetXCBConnection(display
);
9611 if (xcb_connection_has_error(conn
))
9612 errx(1, "can not get XCB connection");
9614 XSetEventQueueOwner(display
, XCBOwnsEventQueue
);
9616 xcb_prefetch_extension_data(conn
, &xcb_randr_id
);
9617 xfd
= xcb_get_file_descriptor(conn
);
9619 /* look for local and global conf file */
9620 pwd
= getpwuid(getuid());
9622 errx(1, "invalid user: %d", getuid());
9624 xcb_grab_server(conn
);
9627 /* flush all events */
9628 while ((evt
= xcb_poll_for_event(conn
))) {
9629 if (XCB_EVENT_RESPONSE_TYPE(evt
) == 0)
9634 if (enable_wm() != 0)
9635 errx(1, "another window manager is currently running");
9637 /* Load Xcursors and/or cursorfont glyph cursors. */
9649 for (i
= 0; ; i
++) {
9654 snprintf(conf
, sizeof conf
, "%s/.%s",
9655 pwd
->pw_dir
, SWM_CONF_FILE
);
9659 snprintf(conf
, sizeof conf
, "/etc/%s",
9664 snprintf(conf
, sizeof conf
, "%s/.%s",
9665 pwd
->pw_dir
, SWM_CONF_FILE_OLD
);
9669 snprintf(conf
, sizeof conf
, "/etc/%s",
9676 if (strlen(conf
) && stat(conf
, &sb
) != -1)
9677 if (S_ISREG(sb
.st_mode
)) {
9684 /* load conf (if any) */
9686 conf_load(cfile
, SWM_CONF_DEFAULT
);
9691 /* set some values to work around bad programs */
9693 /* grab existing windows (before we build the bars) */
9696 if (getenv("SWM_STARTED") == NULL
)
9697 setenv("SWM_STARTED", "YES", 1);
9699 /* setup all bars */
9700 num_screens
= get_screen_count();
9701 for (i
= 0; i
< num_screens
; i
++)
9702 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
9709 xcb_ungrab_server(conn
);
9712 rd_max
= xfd
> STDIN_FILENO
? xfd
: STDIN_FILENO
;
9715 while ((evt
= xcb_poll_for_event(conn
))) {
9722 /* If just (re)started, set default focus if needed. */
9726 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
9727 r
= TAILQ_FIRST(&screens
[0].rl
);
9739 FD_SET(STDIN_FILENO
, &rd
);
9744 num_readable
= select(rd_max
+ 1, &rd
, NULL
, NULL
, &tv
);
9745 if (num_readable
== -1 && errno
!= EINTR
) {
9746 DNPRINTF(SWM_D_MISC
, "select failed");
9747 } else if (num_readable
> 0 && FD_ISSET(STDIN_FILENO
, &rd
)) {
9752 restart(NULL
, NULL
);