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 : 0)
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 int get_screen_count(void);
952 char *get_stack_mode_name(uint8_t);
954 int32_t get_swm_iconic(struct ws_win
*);
955 char *get_win_name(xcb_window_t
);
956 int get_ws_idx(xcb_window_t
);
957 uint32_t getstate(xcb_window_t
);
958 void grabbuttons(struct ws_win
*);
960 void grab_windows(void);
961 void iconify(struct swm_region
*, union arg
*);
963 void keypress(xcb_key_press_event_t
*);
964 int key_cmp(struct key
*, struct key
*);
965 void key_insert(unsigned int, KeySym
, enum keyfuncid
, const char *);
966 struct key
*key_lookup(unsigned int, KeySym
);
967 void key_remove(struct key
*);
968 void key_replace(struct key
*, unsigned int, KeySym
, enum keyfuncid
,
970 void kill_bar_extra_atexit(void);
971 void kill_refs(struct ws_win
*);
973 void leavenotify(xcb_leave_notify_event_t
*);
975 void load_float_geom(struct ws_win
*);
976 struct ws_win
*manage_window(xcb_window_t
, uint16_t);
977 void map_window(struct ws_win
*, xcb_window_t
);
978 void mapnotify(xcb_map_notify_event_t
*);
979 void mappingnotify(xcb_mapping_notify_event_t
*);
980 void maprequest(xcb_map_request_event_t
*);
981 void motionnotify(xcb_motion_notify_event_t
*);
982 void move(struct ws_win
*, union arg
*);
983 void move_step(struct swm_region
*, union arg
*);
984 uint32_t name_to_pixel(int, const char *);
985 void name_workspace(struct swm_region
*, union arg
*);
986 void new_region(struct swm_screen
*, int, int, int, int);
987 int parsekeys(char *, unsigned int, unsigned int *, KeySym
*);
988 int parsequirks(char *, unsigned long *);
989 int parse_rgb(const char *, uint16_t *, uint16_t *, uint16_t *);
990 void pressbutton(struct swm_region
*, union arg
*);
991 void priorws(struct swm_region
*, union arg
*);
993 void print_win_geom(xcb_window_t
);
995 void propertynotify(xcb_property_notify_event_t
*);
996 void quirk_insert(const char *, const char *, unsigned long);
997 void quirk_remove(struct quirk
*);
998 void quirk_replace(struct quirk
*, const char *, const char *,
1000 void quit(struct swm_region
*, union arg
*);
1001 void raise_toggle(struct swm_region
*, union arg
*);
1002 void region_containment(struct ws_win
*, struct swm_region
*, int);
1003 struct swm_region
*region_under(struct swm_screen
*, int, int);
1004 void regionize(struct ws_win
*, int, int);
1005 void resize(struct ws_win
*, union arg
*);
1006 void resize_step(struct swm_region
*, union arg
*);
1007 void restart(struct swm_region
*, union arg
*);
1008 struct swm_region
*root_to_region(xcb_window_t
, int);
1009 void screenchange(xcb_randr_screen_change_notify_event_t
*);
1010 void scan_xrandr(int);
1011 void search_do_resp(void);
1012 void search_resp_name_workspace(const char *, size_t);
1013 void search_resp_search_window(const char *);
1014 void search_resp_search_workspace(const char *);
1015 void search_resp_uniconify(const char *, size_t);
1016 void search_win(struct swm_region
*, union arg
*);
1017 void search_win_cleanup(void);
1018 void search_workspace(struct swm_region
*, union arg
*);
1019 void send_to_rg(struct swm_region
*, union arg
*);
1020 void send_to_ws(struct swm_region
*, union arg
*);
1021 void set_region(struct swm_region
*);
1022 int setautorun(char *, char *, int);
1023 int setconfbinding(char *, char *, int);
1024 int setconfcolor(char *, char *, int);
1025 int setconfmodkey(char *, char *, int);
1026 int setconfquirk(char *, char *, int);
1027 int setconfregion(char *, char *, int);
1028 int setconfspawn(char *, char *, int);
1029 int setconfvalue(char *, char *, int);
1030 void setkeybinding(unsigned int, KeySym
, enum keyfuncid
, const char *);
1031 int setkeymapping(char *, char *, int);
1032 int setlayout(char *, char *, int);
1033 void setquirk(const char *, const char *, unsigned long);
1034 void setscreencolor(char *, int, int);
1035 void setspawn(const char *, const char *, int);
1036 void setup_ewmh(void);
1037 void setup_globals(void);
1038 void setup_keys(void);
1039 void setup_quirks(void);
1040 void setup_screens(void);
1041 void setup_spawn(void);
1042 void set_child_transient(struct ws_win
*, xcb_window_t
*);
1043 void set_swm_iconic(struct ws_win
*, int);
1044 void set_win_state(struct ws_win
*, uint16_t);
1045 void shutdown_cleanup(void);
1047 void socket_setnonblock(int);
1048 void sort_windows(struct ws_win_list
*);
1049 void spawn(int, union arg
*, int);
1050 void spawn_custom(struct swm_region
*, union arg
*, const char *);
1051 int spawn_expand(struct swm_region
*, union arg
*, const char *, char ***);
1052 void spawn_insert(const char *, const char *, int);
1053 void spawn_remove(struct spawn_prog
*);
1054 void spawn_replace(struct spawn_prog
*, const char *, const char *, int);
1055 void spawn_select(struct swm_region
*, union arg
*, const char *, int *);
1056 void stack_config(struct swm_region
*, union arg
*);
1057 void stack_floater(struct ws_win
*, struct swm_region
*);
1058 void stack_master(struct workspace
*, struct swm_geometry
*, int, int);
1059 void store_float_geom(struct ws_win
*);
1060 char *strdupsafe(const char *);
1061 void swapwin(struct swm_region
*, union arg
*);
1062 void switchws(struct swm_region
*, union arg
*);
1063 void teardown_ewmh(void);
1064 void unfocus_win(struct ws_win
*);
1065 void uniconify(struct swm_region
*, union arg
*);
1066 void unmanage_window(struct ws_win
*);
1067 void unmapnotify(xcb_unmap_notify_event_t
*);
1068 void unmap_all(void);
1069 void unmap_window(struct ws_win
*);
1070 void updatenumlockmask(void);
1071 void update_modkey(unsigned int);
1072 void update_window(struct ws_win
*);
1073 void validate_spawns(void);
1074 int validate_win(struct ws_win
*);
1075 int validate_ws(struct workspace
*);
1076 /*void visibilitynotify(xcb_visibility_notify_event_t *);*/
1077 void version(struct swm_region
*, union arg
*);
1078 void win_to_ws(struct ws_win
*, int, int);
1079 pid_t
window_get_pid(xcb_window_t
);
1080 void wkill(struct swm_region
*, union arg
*);
1081 void workaround(void);
1082 void xft_init(struct swm_region
*);
1083 void _add_startup_exception(const char *, va_list);
1084 void add_startup_exception(const char *, ...);
1086 RB_PROTOTYPE(key_tree
, key
, entry
, key_cmp
);
1087 RB_GENERATE(key_tree
, key
, entry
, key_cmp
);
1088 struct key_tree keys
;
1093 xcb_font_t cf
= XCB_NONE
;
1096 for (i
= 0; i
< LENGTH(cursors
); ++i
) {
1097 /* try to load Xcursor first. */
1098 cursors
[i
].cid
= XcursorLibraryLoadCursor(display
,
1101 /* fallback to cursorfont. */
1102 if (cursors
[i
].cid
== XCB_CURSOR_NONE
) {
1103 if (cf
== XCB_NONE
) {
1104 cf
= xcb_generate_id(conn
);
1105 xcb_open_font(conn
, cf
, strlen("cursor"),
1109 cursors
[i
].cid
= xcb_generate_id(conn
);
1110 xcb_create_glyph_cursor(conn
, cursors
[i
].cid
, cf
, cf
,
1111 cursors
[i
].cf_char
, cursors
[i
].cf_char
+ 1, 0, 0, 0,
1112 0xffff, 0xffff, 0xffff);
1118 xcb_close_font(conn
, cf
);
1122 cursors_cleanup(void)
1125 for (i
= 0; i
< LENGTH(cursors
); ++i
)
1126 xcb_free_cursor(conn
, cursors
[i
].cid
);
1130 expand_tilde(const char *s
)
1132 struct passwd
*ppwd
;
1140 errx(1, "expand_tilde: NULL string.");
1143 result
= strdup(sc
);
1149 if ((max
= sysconf(_SC_LOGIN_NAME_MAX
)) == -1)
1150 errx(1, "expand_tilde: sysconf");
1152 if ((user
= calloc(1, max
+ 1)) == NULL
)
1153 errx(1, "expand_tilde: calloc");
1155 for (i
= 0; s
[i
] != '/' && s
[i
] != '\0'; ++i
)
1160 ppwd
= strlen(user
) == 0 ? getpwuid(getuid()) : getpwnam(user
);
1164 result
= strdup(sc
);
1166 if (asprintf(&result
, "%s%s", ppwd
->pw_dir
, s
) == -1)
1170 errx(1, "expand_tilde: failed to allocate memory.");
1176 parse_rgb(const char *rgb
, uint16_t *rr
, uint16_t *gg
, uint16_t *bb
)
1178 unsigned int tmpr
, tmpg
, tmpb
;
1180 if (sscanf(rgb
, "rgb:%x/%x/%x", &tmpr
, &tmpg
, &tmpb
) != 3)
1183 *rr
= RGB_8_TO_16(tmpr
);
1184 *gg
= RGB_8_TO_16(tmpg
);
1185 *bb
= RGB_8_TO_16(tmpb
);
1191 get_screen(int screen
)
1193 const xcb_setup_t
*r
;
1194 xcb_screen_iterator_t iter
;
1196 if ((r
= xcb_get_setup(conn
)) == NULL
) {
1197 DNPRINTF(SWM_D_MISC
, "get_screen: xcb_get_setup\n");
1201 iter
= xcb_setup_roots_iterator(r
);
1202 for (; iter
.rem
; --screen
, xcb_screen_next(&iter
))
1210 get_screen_count(void)
1212 const xcb_setup_t
*r
;
1214 if ((r
= xcb_get_setup(conn
)) == NULL
) {
1215 DNPRINTF(SWM_D_MISC
, "get_screen_count: xcb_get_setup\n");
1219 return xcb_setup_roots_length(r
);
1223 get_region_index(struct swm_region
*r
)
1225 struct swm_region
*rr
;
1231 TAILQ_FOREACH(rr
, &r
->s
->rl
, entry
) {
1246 if (focus_mode
== SWM_FOCUS_DEFAULT
)
1247 event_drain(XCB_ENTER_NOTIFY
);
1253 get_atom_from_string(const char *str
)
1255 xcb_intern_atom_cookie_t c
;
1256 xcb_intern_atom_reply_t
*r
;
1259 c
= xcb_intern_atom(conn
, 0, strlen(str
), str
);
1260 r
= xcb_intern_atom_reply(conn
, c
, NULL
);
1268 return (XCB_ATOM_NONE
);
1272 set_swm_iconic(struct ws_win
*win
, int newv
)
1279 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
1280 a_swm_iconic
, XCB_ATOM_INTEGER
, 32, 1, &v
);
1282 xcb_delete_property(conn
, win
->id
, a_swm_iconic
);
1286 get_swm_iconic(struct ws_win
*win
)
1289 xcb_get_property_reply_t
*pr
= NULL
;
1291 pr
= xcb_get_property_reply(conn
,
1292 xcb_get_property(conn
, 0, win
->id
, a_swm_iconic
,
1293 XCB_ATOM_INTEGER
, 0, 1), NULL
);
1296 if (pr
->type
!= XCB_ATOM_INTEGER
|| pr
->format
!= 32)
1298 v
= *((int32_t *)xcb_get_property_value(pr
));
1308 xcb_atom_t sup_list
;
1309 int i
, j
, num_screens
;
1311 sup_list
= get_atom_from_string("_NET_SUPPORTED");
1313 for (i
= 0; i
< LENGTH(ewmh
); i
++)
1314 ewmh
[i
].atom
= get_atom_from_string(ewmh
[i
].name
);
1316 num_screens
= get_screen_count();
1317 for (i
= 0; i
< num_screens
; i
++) {
1318 /* Support check window will be created by workaround(). */
1320 /* Report supported atoms */
1321 xcb_delete_property(conn
, screens
[i
].root
, sup_list
);
1322 for (j
= 0; j
< LENGTH(ewmh
); j
++)
1323 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
,
1324 screens
[i
].root
, sup_list
, XCB_ATOM_ATOM
, 32, 1,
1333 xcb_atom_t sup_check
, sup_list
;
1335 xcb_get_property_cookie_t pc
;
1336 xcb_get_property_reply_t
*pr
;
1338 sup_check
= get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
1339 sup_list
= get_atom_from_string("_NET_SUPPORTED");
1340 num_screens
= get_screen_count();
1342 for (i
= 0; i
< num_screens
; i
++) {
1343 /* Get the support check window and destroy it */
1344 pc
= xcb_get_property(conn
, 0, screens
[i
].root
, sup_check
,
1345 XCB_ATOM_WINDOW
, 0, 1);
1346 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
1349 if (pr
->format
== sup_check
) {
1350 id
= *((xcb_window_t
*)xcb_get_property_value(pr
));
1352 xcb_destroy_window(conn
, id
);
1353 xcb_delete_property(conn
, screens
[i
].root
, sup_check
);
1354 xcb_delete_property(conn
, screens
[i
].root
, sup_list
);
1361 ewmh_autoquirk(struct ws_win
*win
)
1365 xcb_get_property_cookie_t c
;
1366 xcb_get_property_reply_t
*r
;
1368 c
= xcb_get_property(conn
, 0, win
->id
,
1369 ewmh
[_NET_WM_WINDOW_TYPE
].atom
, XCB_ATOM_ATOM
, 0, UINT32_MAX
);
1370 r
= xcb_get_property_reply(conn
, c
, NULL
);
1374 n
= xcb_get_property_value_length(r
);
1375 type
= xcb_get_property_value(r
);
1377 for (i
= 0; i
< n
; i
++) {
1378 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_NORMAL
].atom
)
1380 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_DOCK
].atom
||
1381 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_TOOLBAR
].atom
||
1382 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_UTILITY
].atom
) {
1384 win
->quirks
= SWM_Q_FLOAT
| SWM_Q_ANYWHERE
;
1387 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_SPLASH
].atom
||
1388 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_DIALOG
].atom
) {
1390 win
->quirks
= SWM_Q_FLOAT
;
1397 #define SWM_EWMH_ACTION_COUNT_MAX (6)
1398 #define EWMH_F_FULLSCREEN (1<<0)
1399 #define EWMH_F_ABOVE (1<<1)
1400 #define EWMH_F_HIDDEN (1<<2)
1401 #define EWMH_F_SKIP_PAGER (1<<3)
1402 #define EWMH_F_SKIP_TASKBAR (1<<4)
1403 #define SWM_F_MANUAL (1<<5)
1406 ewmh_set_win_fullscreen(struct ws_win
*win
, int fs
)
1414 DNPRINTF(SWM_D_MISC
, "ewmh_set_win_fullscreen: window: 0x%x, "
1415 "fullscreen %s\n", win
->id
, YESNO(fs
));
1418 if (!win
->g_floatvalid
)
1419 store_float_geom(win
);
1421 win
->g
= win
->ws
->r
->g
;
1424 load_float_geom(win
);
1431 ewmh_update_actions(struct ws_win
*win
)
1433 xcb_atom_t actions
[SWM_EWMH_ACTION_COUNT_MAX
];
1439 actions
[n
++] = ewmh
[_NET_WM_ACTION_CLOSE
].atom
;
1441 if (win
->floating
) {
1442 actions
[n
++] = ewmh
[_NET_WM_ACTION_MOVE
].atom
;
1443 actions
[n
++] = ewmh
[_NET_WM_ACTION_RESIZE
].atom
;
1444 actions
[n
++] = ewmh
[_NET_WM_ACTION_ABOVE
].atom
;
1447 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
1448 ewmh
[_NET_WM_ALLOWED_ACTIONS
].atom
, XCB_ATOM_ATOM
, 32, 1, actions
);
1451 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
1452 #define _NET_WM_STATE_ADD 1 /* add/set property */
1453 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
1456 ewmh_update_win_state(struct ws_win
*win
, xcb_atom_t state
, long action
)
1458 unsigned int mask
= 0;
1459 unsigned int changed
= 0;
1460 unsigned int orig_flags
;
1465 DNPRINTF(SWM_D_PROP
, "ewmh_update_win_state: window: 0x%x, state: %ld, "
1466 "action: %ld\n", win
->id
, (unsigned long)state
, action
);
1468 if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
)
1469 mask
= EWMH_F_FULLSCREEN
;
1470 else if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
)
1471 mask
= EWMH_F_ABOVE
;
1472 else if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
)
1473 mask
= SWM_F_MANUAL
;
1474 else if (state
== ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
)
1475 mask
= EWMH_F_SKIP_PAGER
;
1476 else if (state
== ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
)
1477 mask
= EWMH_F_SKIP_TASKBAR
;
1479 orig_flags
= win
->ewmh_flags
;
1482 case _NET_WM_STATE_REMOVE
:
1483 win
->ewmh_flags
&= ~mask
;
1485 case _NET_WM_STATE_ADD
:
1486 win
->ewmh_flags
|= mask
;
1488 case _NET_WM_STATE_TOGGLE
:
1489 win
->ewmh_flags
^= mask
;
1493 changed
= (win
->ewmh_flags
& mask
) ^ (orig_flags
& mask
) ? 1 : 0;
1495 if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
) {
1496 if (changed
&& !floating_toggle_win(win
))
1497 win
->ewmh_flags
= orig_flags
; /* revert */
1498 } else if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
) {
1500 win
->manual
= (win
->ewmh_flags
& SWM_F_MANUAL
) != 0;
1501 } else if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
) {
1502 if (changed
&& !ewmh_set_win_fullscreen(win
,
1503 win
->ewmh_flags
& EWMH_F_FULLSCREEN
))
1504 win
->ewmh_flags
= orig_flags
; /* revert */
1507 xcb_delete_property(conn
, win
->id
, ewmh
[_NET_WM_STATE
].atom
);
1509 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
1510 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1511 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1512 &ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
);
1513 else if (win
->ewmh_flags
& EWMH_F_SKIP_PAGER
)
1514 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1515 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1516 &ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
);
1517 else if (win
->ewmh_flags
& EWMH_F_SKIP_TASKBAR
)
1518 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1519 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1520 &ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
);
1521 else if (win
->ewmh_flags
& EWMH_F_ABOVE
)
1522 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1523 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1524 &ewmh
[_NET_WM_STATE_ABOVE
].atom
);
1525 else if (win
->ewmh_flags
& SWM_F_MANUAL
)
1526 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1527 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1528 &ewmh
[_SWM_WM_STATE_MANUAL
].atom
);
1532 ewmh_get_win_state(struct ws_win
*win
)
1535 xcb_get_property_cookie_t c
;
1536 xcb_get_property_reply_t
*r
;
1542 win
->ewmh_flags
= 0;
1544 win
->ewmh_flags
|= EWMH_F_ABOVE
;
1546 win
->ewmh_flags
|= SWM_F_MANUAL
;
1548 c
= xcb_get_property(conn
, 0, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
1549 XCB_ATOM_ATOM
, 0, UINT32_MAX
);
1550 r
= xcb_get_property_reply(conn
, c
, NULL
);
1554 states
= xcb_get_property_value(r
);
1555 n
= xcb_get_property_value_length(r
);
1557 for (i
= 0; i
< n
; i
++)
1558 ewmh_update_win_state(win
, states
[i
], _NET_WM_STATE_ADD
);
1566 dumpwins(struct swm_region
*r
, union arg
*args
)
1570 xcb_get_window_attributes_cookie_t c
;
1571 xcb_get_window_attributes_reply_t
*wa
;
1573 /* suppress unused warning since var is needed */
1576 if (r
->ws
== NULL
) {
1577 warnx("dumpwins: invalid workspace");
1581 warnx("=== managed window list ws %02d ===", r
->ws
->idx
);
1582 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
1583 state
= getstate(win
->id
);
1584 c
= xcb_get_window_attributes(conn
, win
->id
);
1585 wa
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
1587 warnx("window: 0x%x, map_state: %d, state: %u, "
1588 "transient: 0x%x", win
->id
, wa
->map_state
,
1589 state
, win
->transient
);
1592 warnx("window: 0x%x, failed xcb_get_window_attributes",
1596 warnx("===== unmanaged window list =====");
1597 TAILQ_FOREACH(win
, &r
->ws
->unmanagedlist
, entry
) {
1598 state
= getstate(win
->id
);
1599 c
= xcb_get_window_attributes(conn
, win
->id
);
1600 wa
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
1602 warnx("window: 0x%x, map_state: %d, state: %u, "
1603 "transient: 0x%x", win
->id
, wa
->map_state
,
1604 state
, win
->transient
);
1607 warnx("window: 0x%x, failed xcb_get_window_attributes",
1611 warnx("=================================");
1615 dumpwins(struct swm_region
*r
, union arg
*s
)
1620 #endif /* SWM_DEBUG */
1625 int saved_errno
, status
;
1628 saved_errno
= errno
;
1632 while ((pid
= waitpid(WAIT_ANY
, &status
, WNOHANG
)) != 0) {
1637 if (errno
!= ECHILD
)
1638 warn("sighdlr: waitpid");
1639 #endif /* SWM_DEBUG */
1642 if (pid
== searchpid
)
1646 if (WIFEXITED(status
)) {
1647 if (WEXITSTATUS(status
) != 0)
1648 warnx("sighdlr: child exit status: %d",
1649 WEXITSTATUS(status
));
1651 warnx("sighdlr: child is terminated "
1653 #endif /* SWM_DEBUG */
1667 errno
= saved_errno
;
1673 struct pid_e
*p
= NULL
;
1675 DNPRINTF(SWM_D_MISC
, "find_pid: %d\n", pid
);
1680 TAILQ_FOREACH(p
, &pidlist
, entry
) {
1689 name_to_pixel(int sidx
, const char *colorname
)
1691 uint32_t result
= 0;
1692 char cname
[32] = "#";
1693 xcb_screen_t
*screen
;
1694 xcb_colormap_t cmap
;
1695 xcb_alloc_color_reply_t
*cr
;
1696 xcb_alloc_named_color_reply_t
*nr
;
1697 uint16_t rr
, gg
, bb
;
1699 screen
= get_screen(sidx
);
1700 cmap
= screen
->default_colormap
;
1702 /* color is in format rgb://rr/gg/bb */
1703 if (strncmp(colorname
, "rgb:", 4) == 0) {
1704 if (parse_rgb(colorname
, &rr
, &gg
, &bb
) == -1)
1705 warnx("could not parse rgb %s", colorname
);
1707 cr
= xcb_alloc_color_reply(conn
,
1708 xcb_alloc_color(conn
, cmap
, rr
, gg
, bb
),
1714 warnx("color '%s' not found", colorname
);
1717 nr
= xcb_alloc_named_color_reply(conn
,
1718 xcb_alloc_named_color(conn
, cmap
, strlen(colorname
),
1721 strlcat(cname
, colorname
+ 2, sizeof cname
- 1);
1722 nr
= xcb_alloc_named_color_reply(conn
,
1723 xcb_alloc_named_color(conn
, cmap
, strlen(cname
),
1730 warnx("color '%s' not found", colorname
);
1737 setscreencolor(char *val
, int i
, int c
)
1741 num_screens
= get_screen_count();
1742 if (i
> 0 && i
<= num_screens
) {
1743 screens
[i
- 1].c
[c
].pixel
= name_to_pixel(i
- 1, val
);
1744 free(screens
[i
- 1].c
[c
].name
);
1745 if ((screens
[i
- 1].c
[c
].name
= strdup(val
)) == NULL
)
1747 } else if (i
== -1) {
1748 for (i
= 0; i
< num_screens
; i
++) {
1749 screens
[i
].c
[c
].pixel
= name_to_pixel(0, val
);
1750 free(screens
[i
].c
[c
].name
);
1751 if ((screens
[i
].c
[c
].name
= strdup(val
)) == NULL
)
1755 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1760 fancy_stacker(struct workspace
*ws
)
1762 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1763 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1764 snprintf(ws
->stacker
, sizeof ws
->stacker
,
1765 ws
->l_state
.vertical_flip
? "[%d>%d]" : "[%d|%d]",
1766 ws
->l_state
.vertical_mwin
, ws
->l_state
.vertical_stacks
);
1767 if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1768 snprintf(ws
->stacker
, sizeof ws
->stacker
,
1769 ws
->l_state
.horizontal_flip
? "[%dv%d]" : "[%d-%d]",
1770 ws
->l_state
.horizontal_mwin
, ws
->l_state
.horizontal_stacks
);
1774 plain_stacker(struct workspace
*ws
)
1776 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1777 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1778 strlcpy(ws
->stacker
, ws
->l_state
.vertical_flip
? "[>]" : "[|]",
1779 sizeof ws
->stacker
);
1780 if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1781 strlcpy(ws
->stacker
, ws
->l_state
.horizontal_flip
? "[v]" : "[-]",
1782 sizeof ws
->stacker
);
1786 custom_region(char *val
)
1788 unsigned int x
, y
, w
, h
;
1789 int sidx
, num_screens
;
1790 xcb_screen_t
*screen
;
1792 num_screens
= get_screen_count();
1793 if (sscanf(val
, "screen[%d]:%ux%u+%u+%u", &sidx
, &w
, &h
, &x
, &y
) != 5)
1794 errx(1, "invalid custom region, "
1795 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1796 if (sidx
< 1 || sidx
> num_screens
)
1797 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1801 if ((screen
= get_screen(sidx
)) == NULL
)
1802 errx(1, "ERROR: can't get screen %d.", sidx
);
1805 errx(1, "region %ux%u+%u+%u too small", w
, h
, x
, y
);
1807 if (x
> screen
->width_in_pixels
||
1808 y
> screen
->height_in_pixels
||
1809 w
+ x
> screen
->width_in_pixels
||
1810 h
+ y
> screen
->height_in_pixels
) {
1811 warnx("ignoring region %ux%u+%u+%u - not within screen "
1812 "boundaries (%ux%u)", w
, h
, x
, y
,
1813 screen
->width_in_pixels
, screen
->height_in_pixels
);
1817 new_region(&screens
[sidx
], x
, y
, w
, h
);
1821 socket_setnonblock(int fd
)
1825 if ((flags
= fcntl(fd
, F_GETFL
, 0)) == -1)
1826 err(1, "fcntl F_GETFL");
1827 flags
|= O_NONBLOCK
;
1828 if ((flags
= fcntl(fd
, F_SETFL
, flags
)) == -1)
1829 err(1, "fcntl F_SETFL");
1833 bar_print_legacy(struct swm_region
*r
, const char *s
)
1835 xcb_rectangle_t rect
;
1840 XRectangle ibox
, lbox
;
1844 XmbTextExtents(bar_fs
, s
, len
, &ibox
, &lbox
);
1846 switch (bar_justify
) {
1847 case SWM_BAR_JUSTIFY_LEFT
:
1850 case SWM_BAR_JUSTIFY_CENTER
:
1851 x
= (WIDTH(r
) - lbox
.width
) / 2;
1853 case SWM_BAR_JUSTIFY_RIGHT
:
1854 x
= WIDTH(r
) - lbox
.width
- SWM_BAR_OFFSET
;
1858 if (x
< SWM_BAR_OFFSET
)
1863 rect
.width
= WIDTH(r
->bar
);
1864 rect
.height
= HEIGHT(r
->bar
);
1866 /* clear back buffer */
1867 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
1868 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
1869 xcb_poly_fill_rectangle(conn
, r
->bar
->buffer
, r
->s
->bar_gc
, 1, &rect
);
1871 /* draw back buffer */
1872 gcvd
.graphics_exposures
= 0;
1873 draw
= XCreateGC(display
, r
->bar
->buffer
, GCGraphicsExposures
, &gcvd
);
1874 XSetForeground(display
, draw
, r
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
);
1875 DRAWSTRING(display
, r
->bar
->buffer
, bar_fs
, draw
,
1876 x
, (bar_fs_extents
->max_logical_extent
.height
- lbox
.height
) / 2 -
1878 XFreeGC(display
, draw
);
1881 xcb_copy_area(conn
, r
->bar
->buffer
, r
->bar
->id
, r
->s
->bar_gc
, 0, 0,
1882 0, 0, WIDTH(r
->bar
), HEIGHT(r
->bar
));
1886 bar_print(struct swm_region
*r
, const char *s
)
1889 xcb_rectangle_t rect
;
1897 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
, &info
);
1899 switch (bar_justify
) {
1900 case SWM_BAR_JUSTIFY_LEFT
:
1903 case SWM_BAR_JUSTIFY_CENTER
:
1904 x
= (WIDTH(r
) - info
.width
) / 2;
1906 case SWM_BAR_JUSTIFY_RIGHT
:
1907 x
= WIDTH(r
) - info
.width
- SWM_BAR_OFFSET
;
1911 if (x
< SWM_BAR_OFFSET
)
1916 rect
.width
= WIDTH(r
->bar
);
1917 rect
.height
= HEIGHT(r
->bar
);
1919 /* clear back buffer */
1920 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
1921 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
1922 xcb_poly_fill_rectangle(conn
, r
->bar
->buffer
, r
->s
->bar_gc
, 1, &rect
);
1924 /* draw back buffer */
1925 draw
= XftDrawCreate(display
, r
->bar
->buffer
,
1926 DefaultVisual(display
, r
->s
->idx
),
1927 DefaultColormap(display
, r
->s
->idx
));
1929 XftDrawStringUtf8(draw
, &bar_font_color
, bar_font
, x
,
1930 (HEIGHT(r
->bar
) + bar_font
->height
) / 2 - bar_font
->descent
,
1933 XftDrawDestroy(draw
);
1936 xcb_copy_area(conn
, r
->bar
->buffer
, r
->bar
->id
, r
->s
->bar_gc
, 0, 0,
1937 0, 0, WIDTH(r
->bar
), HEIGHT(r
->bar
));
1941 bar_extra_stop(void)
1945 bzero(bar_pipe
, sizeof bar_pipe
);
1948 kill(bar_pid
, SIGTERM
);
1951 strlcpy(bar_ext
, "", sizeof bar_ext
);
1956 bar_class_name(char *s
, size_t sz
, struct swm_region
*r
)
1958 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1960 if (r
->ws
->focus
->ch
.class_name
!= NULL
)
1961 strlcat(s
, r
->ws
->focus
->ch
.class_name
, sz
);
1965 bar_title_name(char *s
, size_t sz
, struct swm_region
*r
)
1967 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1969 if (r
->ws
->focus
->ch
.instance_name
!= NULL
)
1970 strlcat(s
, r
->ws
->focus
->ch
.instance_name
, sz
);
1974 bar_class_title_name(char *s
, size_t sz
, struct swm_region
*r
)
1976 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1979 bar_class_name(s
, sz
, r
);
1980 strlcat(s
, ":", sz
);
1981 bar_title_name(s
, sz
, r
);
1985 bar_window_float(char *s
, size_t sz
, struct swm_region
*r
)
1987 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1989 if (r
->ws
->focus
->floating
)
1990 strlcat(s
, "(f)", sz
);
1994 bar_window_name(char *s
, size_t sz
, struct swm_region
*r
)
1998 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2000 if ((title
= get_win_name(r
->ws
->focus
->id
)) == NULL
)
2003 strlcat(s
, title
, sz
);
2008 bar_urgent(char *s
, size_t sz
)
2011 int i
, j
, num_screens
;
2012 int urgent
[SWM_WS_MAX
];
2014 xcb_get_property_cookie_t c
;
2015 xcb_icccm_wm_hints_t hints
;
2017 for (i
= 0; i
< workspace_limit
; i
++)
2020 num_screens
= get_screen_count();
2021 for (i
= 0; i
< num_screens
; i
++)
2022 for (j
= 0; j
< workspace_limit
; j
++)
2023 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
) {
2024 c
= xcb_icccm_get_wm_hints(conn
, win
->id
);
2025 if (xcb_icccm_get_wm_hints_reply(conn
, c
,
2028 if (hints
.flags
& XCB_ICCCM_WM_HINT_X_URGENCY
)
2032 for (i
= 0; i
< workspace_limit
; i
++) {
2034 snprintf(b
, sizeof b
, "%d ", i
+ 1);
2036 snprintf(b
, sizeof b
, "- ");
2042 bar_workspace_name(char *s
, size_t sz
, struct swm_region
*r
)
2044 if (r
== NULL
|| r
->ws
== NULL
)
2046 if (r
->ws
->name
!= NULL
)
2047 strlcat(s
, r
->ws
->name
, sz
);
2050 /* build the default bar format according to the defined enabled options */
2052 bar_fmt(const char *fmtexp
, char *fmtnew
, struct swm_region
*r
, size_t sz
)
2054 /* if format provided, just copy the buffers */
2055 if (bar_format
!= NULL
) {
2056 strlcpy(fmtnew
, fmtexp
, sz
);
2060 /* reset the output buffer */
2063 strlcat(fmtnew
, "+N:+I ", sz
);
2065 strlcat(fmtnew
, "+S", sz
);
2066 strlcat(fmtnew
, " ", sz
);
2068 /* only show the workspace name if there's actually one */
2069 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->name
!= NULL
)
2070 strlcat(fmtnew
, "<+D>", sz
);
2071 strlcat(fmtnew
, "+3<", sz
);
2073 if (clock_enabled
) {
2074 strlcat(fmtnew
, fmtexp
, sz
);
2075 strlcat(fmtnew
, "+4<", sz
);
2078 /* bar_urgent already adds the space before the last asterisk */
2080 strlcat(fmtnew
, "* +U*+4<", sz
);
2082 if (title_class_enabled
) {
2083 strlcat(fmtnew
, "+C", sz
);
2084 if (!title_name_enabled
)
2085 strlcat(fmtnew
, "+4<", sz
);
2088 /* checks needed by the colon and floating strlcat(3) calls below */
2089 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->focus
!= NULL
) {
2090 if (title_name_enabled
) {
2091 if (title_class_enabled
)
2092 strlcat(fmtnew
, ":", sz
);
2093 strlcat(fmtnew
, "+T+4<", sz
);
2095 if (window_name_enabled
) {
2096 if (r
->ws
->focus
->floating
)
2097 strlcat(fmtnew
, "+F ", sz
);
2098 strlcat(fmtnew
, "+64W ", sz
);
2102 /* finally add the action script output and the version */
2103 strlcat(fmtnew
, "+4<+A+4<+V", sz
);
2107 bar_replace_pad(char *tmp
, int *limit
, size_t sz
)
2109 /* special case; no limit given, pad one space, instead */
2110 if (*limit
== (int)sz
- 1)
2112 snprintf(tmp
, sz
, "%*s", *limit
, " ");
2115 /* replaces the bar format character sequences (like in tmux(1)) */
2117 bar_replace_seq(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t *offrep
,
2121 char tmp
[SWM_BAR_MAX
];
2125 /* reset strlcat(3) buffer */
2128 /* get number, if any */
2131 if (sscanf(fmt
, "%d%n", &limit
, &size
) != 1)
2132 limit
= sizeof tmp
- 1;
2133 if (limit
<= 0 || limit
>= (int)sizeof tmp
)
2134 limit
= sizeof tmp
- 1;
2136 /* there is nothing to replace (ie EOL) */
2143 bar_replace_pad(tmp
, &limit
, sizeof tmp
);
2146 snprintf(tmp
, sizeof tmp
, "%s", bar_ext
);
2149 bar_class_name(tmp
, sizeof tmp
, r
);
2152 bar_workspace_name(tmp
, sizeof tmp
, r
);
2155 bar_window_float(tmp
, sizeof tmp
, r
);
2158 snprintf(tmp
, sizeof tmp
, "%d", r
->ws
->idx
+ 1);
2161 snprintf(tmp
, sizeof tmp
, "%d", r
->s
->idx
+ 1);
2164 bar_class_title_name(tmp
, sizeof tmp
, r
);
2167 snprintf(tmp
, sizeof tmp
, "%s", r
->ws
->stacker
);
2170 bar_title_name(tmp
, sizeof tmp
, r
);
2173 bar_urgent(tmp
, sizeof tmp
);
2176 snprintf(tmp
, sizeof tmp
, "%s", bar_vertext
);
2179 bar_window_name(tmp
, sizeof tmp
, r
);
2182 /* unknown character sequence; copy as-is */
2183 snprintf(tmp
, sizeof tmp
, "+%c", *fmt
);
2189 if ((int)len
< limit
)
2191 while (limit
-- > 0) {
2192 if (*offrep
>= sz
- 1)
2194 fmtrep
[(*offrep
)++] = *ptr
++;
2202 bar_replace(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t sz
)
2207 while (*fmt
!= '\0') {
2209 /* skip ordinary characters */
2212 fmtrep
[off
++] = *fmt
++;
2216 /* character sequence found; replace it */
2217 fmt
= bar_replace_seq(fmt
, fmtrep
, r
, &off
, sz
);
2226 bar_fmt_expand(char *fmtexp
, size_t sz
)
2233 /* start by grabbing the current time and date */
2235 localtime_r(&tmt
, &tm
);
2237 /* figure out what to expand */
2238 if (bar_format
!= NULL
)
2240 else if (bar_format
== NULL
&& clock_enabled
)
2242 /* if nothing to expand bail out */
2248 /* copy as-is, just in case the format shouldn't be expanded below */
2249 strlcpy(fmtexp
, fmt
, sz
);
2250 /* finally pass the string through strftime(3) */
2251 #ifndef SWM_DENY_CLOCK_FORMAT
2252 if ((len
= strftime(fmtexp
, sz
, fmt
, &tm
)) == 0)
2253 warnx("format too long");
2258 /* Redraws the bar; need to follow with xcb_flush() or focus_flush(). */
2262 char fmtexp
[SWM_BAR_MAX
], fmtnew
[SWM_BAR_MAX
];
2263 char fmtrep
[SWM_BAR_MAX
];
2265 struct swm_region
*r
;
2267 /* expand the format by first passing it through strftime(3) */
2268 bar_fmt_expand(fmtexp
, sizeof fmtexp
);
2270 num_screens
= get_screen_count();
2271 for (i
= 0; i
< num_screens
; i
++) {
2272 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
2276 if (bar_enabled
&& r
->ws
->bar_enabled
)
2277 xcb_map_window(conn
, r
->bar
->id
);
2279 xcb_unmap_window(conn
, r
->bar
->id
);
2283 if (startup_exception
)
2284 snprintf(fmtrep
, sizeof fmtrep
, "total "
2285 "exceptions: %d, first exception: %s",
2289 bar_fmt(fmtexp
, fmtnew
, r
, sizeof fmtnew
);
2290 bar_replace(fmtnew
, fmtrep
, r
, sizeof fmtrep
);
2292 if (bar_font_legacy
)
2293 bar_print_legacy(r
, fmtrep
);
2295 bar_print(r
, fmtrep
);
2301 * Reads external script output; call when stdin is readable.
2302 * Returns 1 if bar_ext was updated; otherwise 0.
2305 bar_extra_update(void)
2308 char b
[SWM_BAR_MAX
];
2314 while (fgets(b
, sizeof(b
), stdin
) != NULL
) {
2317 if (b
[len
- 1] == '\n') {
2318 /* Remove newline. */
2321 /* "Clear" bar_ext. */
2324 /* Flush buffered output. */
2325 strlcpy(bar_ext
, bar_ext_buf
, sizeof(bar_ext
));
2326 bar_ext_buf
[0] = '\0';
2328 /* Append new output to bar. */
2329 strlcat(bar_ext
, b
, sizeof(bar_ext
));
2333 /* Buffer output. */
2334 strlcat(bar_ext_buf
, b
, sizeof(bar_ext_buf
));
2339 if (errno
!= EAGAIN
) {
2340 warn("bar_action failed");
2349 bar_toggle(struct swm_region
*r
, union arg
*args
)
2351 struct swm_region
*tmpr
;
2354 /* suppress unused warnings since vars are needed */
2358 DNPRINTF(SWM_D_BAR
, "bar_toggle\n");
2361 case SWM_ARG_ID_BAR_TOGGLE_WS
:
2362 /* Only change if master switch is enabled. */
2364 r
->ws
->bar_enabled
= !r
->ws
->bar_enabled
;
2366 bar_enabled
= r
->ws
->bar_enabled
= 1;
2368 case SWM_ARG_ID_BAR_TOGGLE
:
2369 bar_enabled
= !bar_enabled
;
2373 /* update bars as necessary */
2374 num_screens
= get_screen_count();
2375 for (i
= 0; i
< num_screens
; i
++)
2376 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
2378 if (bar_enabled
&& tmpr
->ws
->bar_enabled
)
2379 xcb_map_window(conn
, tmpr
->bar
->id
);
2381 xcb_unmap_window(conn
, tmpr
->bar
->id
);
2386 /* must be after stack */
2393 bar_extra_setup(void)
2395 /* do this here because the conf file is in memory */
2396 if (!bar_extra
&& bar_argv
[0]) {
2397 /* launch external status app */
2399 if (pipe(bar_pipe
) == -1)
2400 err(1, "pipe error");
2401 socket_setnonblock(bar_pipe
[0]);
2402 socket_setnonblock(bar_pipe
[1]); /* XXX hmmm, really? */
2404 /* Set stdin to read from the pipe. */
2405 if (dup2(bar_pipe
[0], STDIN_FILENO
) == -1)
2408 /* Set stdout to write to the pipe. */
2409 if (dup2(bar_pipe
[1], STDOUT_FILENO
) == -1)
2412 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
2413 err(1, "could not disable SIGPIPE");
2414 switch (bar_pid
= fork()) {
2416 err(1, "cannot fork");
2420 execvp(bar_argv
[0], bar_argv
);
2421 err(1, "%s external app failed", bar_argv
[0]);
2423 default: /* parent */
2428 atexit(kill_bar_extra_atexit
);
2433 kill_bar_extra_atexit(void)
2436 kill(bar_pid
, SIGTERM
);
2444 while ((s
= index(s
, '-'))) {
2449 return (count
== 14);
2455 char *default_string
;
2456 char **missing_charsets
;
2457 int num_missing_charsets
= 0;
2461 XFreeFontSet(display
, bar_fs
);
2465 DNPRINTF(SWM_D_INIT
, "fontset_init: loading bar_fonts: %s\n", bar_fonts
);
2467 bar_fs
= XCreateFontSet(display
, bar_fonts
, &missing_charsets
,
2468 &num_missing_charsets
, &default_string
);
2470 if (num_missing_charsets
> 0) {
2471 warnx("Unable to load charset(s):");
2473 for (i
= 0; i
< num_missing_charsets
; ++i
)
2474 warnx("%s", missing_charsets
[i
]);
2476 XFreeStringList(missing_charsets
);
2478 if (strcmp(default_string
, ""))
2479 warnx("Glyphs from those sets will be replaced "
2480 "by '%s'.", default_string
);
2482 warnx("Glyphs from those sets won't be drawn.");
2486 errx(1, "Error creating font set structure.");
2488 bar_fs_extents
= XExtentsOfFontSet(bar_fs
);
2490 bar_height
= bar_fs_extents
->max_logical_extent
.height
+
2491 2 * bar_border_width
;
2498 xft_init(struct swm_region
*r
)
2500 char *font
, *d
, *search
;
2503 if (bar_font
== NULL
) {
2504 if ((d
= strdup(bar_fonts
)) == NULL
)
2505 errx(1, "insufficient memory.");
2507 while ((font
= strsep(&search
, ",")) != NULL
) {
2511 DNPRINTF(SWM_D_INIT
, "xft_init: try font %s\n", font
);
2514 bar_font
= XftFontOpenXlfd(display
, r
->s
->idx
,
2517 bar_font
= XftFontOpenName(display
, r
->s
->idx
,
2522 warnx("unable to load font %s", font
);
2525 DNPRINTF(SWM_D_INIT
, "successfully opened "
2533 if (bar_font
== NULL
)
2534 errx(1, "unable to open a font");
2536 PIXEL_TO_XRENDERCOLOR(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
, color
);
2538 if (!XftColorAllocValue(display
, DefaultVisual(display
, r
->s
->idx
),
2539 DefaultColormap(display
, r
->s
->idx
), &color
, &bar_font_color
))
2540 warn("Xft error: unable to allocate color.");
2542 bar_height
= bar_font
->height
+ 2 * bar_border_width
;
2549 bar_setup(struct swm_region
*r
)
2551 xcb_screen_t
*screen
;
2554 DNPRINTF(SWM_D_BAR
, "bar_setup: screen %d.\n",
2557 if ((screen
= get_screen(r
->s
->idx
)) == NULL
)
2558 errx(1, "ERROR: can't get screen %d.", r
->s
->idx
);
2563 if ((r
->bar
= calloc(1, sizeof(struct swm_bar
))) == NULL
)
2564 err(1, "bar_setup: calloc: failed to allocate memory.");
2566 if (bar_font_legacy
)
2572 Y(r
->bar
) = bar_at_bottom
? (Y(r
) + HEIGHT(r
) - bar_height
) : Y(r
);
2573 WIDTH(r
->bar
) = WIDTH(r
) - 2 * bar_border_width
;
2574 HEIGHT(r
->bar
) = bar_height
- 2 * bar_border_width
;
2576 /* Assume region is unfocused when we create the bar. */
2577 r
->bar
->id
= xcb_generate_id(conn
);
2578 wa
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
2579 wa
[1] = r
->s
->c
[SWM_S_COLOR_BAR_BORDER_UNFOCUS
].pixel
;
2580 wa
[2] = XCB_EVENT_MASK_EXPOSURE
| XCB_EVENT_MASK_POINTER_MOTION
|
2581 XCB_EVENT_MASK_POINTER_MOTION_HINT
;
2583 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, r
->bar
->id
, r
->s
->root
,
2584 X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
), HEIGHT(r
->bar
),
2585 bar_border_width
, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
2586 XCB_COPY_FROM_PARENT
, XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
2587 | XCB_CW_EVENT_MASK
, wa
);
2589 r
->bar
->buffer
= xcb_generate_id(conn
);
2590 xcb_create_pixmap(conn
, screen
->root_depth
, r
->bar
->buffer
, r
->bar
->id
,
2591 WIDTH(r
->bar
), HEIGHT(r
->bar
));
2594 xcb_randr_select_input(conn
, r
->bar
->id
,
2595 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE
);
2598 xcb_map_window(conn
, r
->bar
->id
);
2600 DNPRINTF(SWM_D_BAR
, "bar_setup: window: 0x%x, (x,y) w x h: (%d,%d) "
2601 "%d x %d\n", WINID(r
->bar
), X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
),
2608 bar_cleanup(struct swm_region
*r
)
2612 xcb_destroy_window(conn
, r
->bar
->id
);
2613 xcb_free_pixmap(conn
, r
->bar
->buffer
);
2619 set_win_state(struct ws_win
*win
, uint16_t state
)
2621 uint16_t data
[2] = { state
, XCB_ATOM_NONE
};
2623 DNPRINTF(SWM_D_EVENT
, "set_win_state: window: 0x%x, state: %u\n",
2629 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
, a_state
,
2630 a_state
, 32, 2, data
);
2634 getstate(xcb_window_t w
)
2636 uint32_t result
= 0;
2637 xcb_get_property_cookie_t c
;
2638 xcb_get_property_reply_t
*r
;
2640 c
= xcb_get_property(conn
, 0, w
, a_state
, a_state
, 0L, 2L);
2641 r
= xcb_get_property_reply(conn
, c
, NULL
);
2644 if (r
->type
== a_state
&& r
->format
== 32 && r
->length
== 2)
2645 result
= *((uint32_t *)xcb_get_property_value(r
));
2649 DNPRINTF(SWM_D_MISC
, "getstate property: win 0x%x state %u\n", w
,
2655 version(struct swm_region
*r
, union arg
*args
)
2657 /* suppress unused warnings since vars are needed */
2661 bar_version
= !bar_version
;
2663 snprintf(bar_vertext
, sizeof bar_vertext
,
2664 "Version: %s Build: %s", SPECTRWM_VERSION
, buildstr
);
2666 strlcpy(bar_vertext
, "", sizeof bar_vertext
);
2673 client_msg(struct ws_win
*win
, xcb_atom_t a
, xcb_timestamp_t t
)
2675 xcb_client_message_event_t ev
;
2683 name
= get_atom_name(a
);
2684 DNPRINTF(SWM_D_EVENT
, "client_msg: window: 0x%x, atom: %s(%u), "
2686 win
->id
, name
, a
, t
);
2690 bzero(&ev
, sizeof ev
);
2691 ev
.response_type
= XCB_CLIENT_MESSAGE
;
2692 ev
.window
= win
->id
;
2695 ev
.data
.data32
[0] = a
;
2696 ev
.data
.data32
[1] = t
;
2698 xcb_send_event(conn
, 0, win
->id
,
2699 XCB_EVENT_MASK_NO_EVENT
, (const char *)&ev
);
2702 /* synthetic response to a ConfigureRequest when not making a change */
2704 config_win(struct ws_win
*win
, xcb_configure_request_event_t
*ev
)
2706 xcb_configure_notify_event_t ce
;
2711 /* send notification of unchanged state. */
2712 bzero(&ce
, sizeof(ce
));
2713 ce
.response_type
= XCB_CONFIGURE_NOTIFY
;
2716 ce
.width
= WIDTH(win
);
2717 ce
.height
= HEIGHT(win
);
2718 ce
.override_redirect
= 0;
2723 ce
.window
= win
->id
;
2724 ce
.border_width
= BORDER(win
);
2725 ce
.above_sibling
= XCB_WINDOW_NONE
;
2728 ce
.event
= ev
->window
;
2729 ce
.window
= ev
->window
;
2731 /* make response appear more WM_SIZE_HINTS-compliant */
2732 if (win
->sh
.flags
) {
2733 DNPRINTF(SWM_D_MISC
, "config_win: hints: window: 0x%x,"
2734 " sh.flags: %u, min: %d x %d, max: %d x %d, inc: "
2735 "%d x %d\n", win
->id
, win
->sh
.flags
, SH_MIN_W(win
),
2736 SH_MIN_H(win
), SH_MAX_W(win
), SH_MAX_H(win
),
2737 SH_INC_W(win
), SH_INC_H(win
));
2742 /* the hint may be set... to 0! */
2743 if (SH_MIN_W(win
) > 0 && ce
.width
< SH_MIN_W(win
))
2744 ce
.width
= SH_MIN_W(win
);
2745 if (SH_MIN_H(win
) > 0 && ce
.height
< SH_MIN_H(win
))
2746 ce
.height
= SH_MIN_H(win
);
2751 /* may also be advertized as 0 */
2752 if (SH_MAX_W(win
) > 0 && ce
.width
> SH_MAX_W(win
))
2753 ce
.width
= SH_MAX_W(win
);
2754 if (SH_MAX_H(win
) > 0 && ce
.height
> SH_MAX_H(win
))
2755 ce
.height
= SH_MAX_H(win
);
2758 /* resize increment. */
2760 if (SH_INC_W(win
) > 1 && ce
.width
> SH_INC_W(win
))
2761 ce
.width
-= (ce
.width
- SH_MIN_W(win
)) %
2763 if (SH_INC_H(win
) > 1 && ce
.height
> SH_INC_H(win
))
2764 ce
.height
-= (ce
.height
- SH_MIN_H(win
)) %
2768 /* adjust x and y for requested border_width. */
2769 ce
.x
+= BORDER(win
) - ev
->border_width
;
2770 ce
.y
+= BORDER(win
) - ev
->border_width
;
2771 ce
.border_width
= ev
->border_width
;
2772 ce
.above_sibling
= ev
->sibling
;
2775 DNPRINTF(SWM_D_MISC
, "config_win: ewmh: %s, window: 0x%x, (x,y) w x h: "
2776 "(%d,%d) %d x %d, border: %d\n", YESNO(ev
== NULL
), win
->id
, ce
.x
,
2777 ce
.y
, ce
.width
, ce
.height
, ce
.border_width
);
2779 xcb_send_event(conn
, 0, win
->id
, XCB_EVENT_MASK_STRUCTURE_NOTIFY
,
2784 count_win(struct workspace
*ws
, int count_transient
)
2789 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
2790 if (!count_transient
&& win
->floating
)
2792 if (!count_transient
&& win
->transient
)
2798 DNPRINTF(SWM_D_MISC
, "count_win: %d\n", count
);
2804 quit(struct swm_region
*r
, union arg
*args
)
2806 /* suppress unused warnings since vars are needed */
2810 DNPRINTF(SWM_D_MISC
, "quit\n");
2815 map_window(struct ws_win
*win
, xcb_window_t sibling
)
2817 uint16_t mode
= XCB_CONFIG_WINDOW_STACK_MODE
;
2821 /* If sibling is specified, stack right above it. */
2822 if (sibling
!= XCB_WINDOW_NONE
) {
2823 mode
|= XCB_CONFIG_WINDOW_SIBLING
;
2827 val
[i
] = XCB_STACK_MODE_ABOVE
;
2832 DNPRINTF(SWM_D_EVENT
, "map_window: win 0x%x, mapped: %s, sibling: %x\n",
2833 win
->id
, YESNO(win
->mapped
), sibling
);
2835 xcb_configure_window(conn
, win
->id
, mode
, val
);
2840 xcb_map_window(conn
, win
->id
);
2841 set_win_state(win
, XCB_ICCCM_WM_STATE_NORMAL
);
2846 unmap_window(struct ws_win
*win
)
2851 DNPRINTF(SWM_D_EVENT
, "unmap_window: win 0x%x, mapped: %s\n", win
->id
,
2852 YESNO(win
->mapped
));
2857 xcb_unmap_window(conn
, win
->id
);
2858 set_win_state(win
, XCB_ICCCM_WM_STATE_ICONIC
);
2866 int i
, j
, num_screens
;
2868 num_screens
= get_screen_count();
2869 for (i
= 0; i
< num_screens
; i
++)
2870 for (j
= 0; j
< workspace_limit
; j
++)
2871 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2876 fake_keypress(struct ws_win
*win
, xcb_keysym_t keysym
, uint16_t modifiers
)
2878 xcb_key_press_event_t event
;
2879 xcb_keycode_t
*keycode
;
2884 keycode
= xcb_key_symbols_get_keycode(syms
, keysym
);
2886 DNPRINTF(SWM_D_MISC
, "fake_keypress: win 0x%x keycode %u\n",
2889 bzero(&event
, sizeof(event
));
2890 event
.event
= win
->id
;
2891 event
.root
= win
->s
->root
;
2892 event
.child
= XCB_WINDOW_NONE
;
2893 event
.time
= XCB_CURRENT_TIME
;
2894 event
.event_x
= X(win
);
2895 event
.event_y
= Y(win
);
2898 event
.same_screen
= 1;
2899 event
.detail
= *keycode
;
2900 event
.state
= modifiers
;
2902 event
.response_type
= XCB_KEY_PRESS
;
2903 xcb_send_event(conn
, 1, win
->id
,
2904 XCB_EVENT_MASK_KEY_PRESS
, (const char *)&event
);
2906 event
.response_type
= XCB_KEY_RELEASE
;
2907 xcb_send_event(conn
, 1, win
->id
,
2908 XCB_EVENT_MASK_KEY_RELEASE
, (const char *)&event
);
2914 restart(struct swm_region
*r
, union arg
*args
)
2916 /* suppress unused warning since var is needed */
2920 DNPRINTF(SWM_D_MISC
, "restart: %s\n", start_argv
[0]);
2924 execvp(start_argv
[0], start_argv
);
2925 warn("execvp failed");
2930 get_pointer_win(xcb_window_t root
)
2932 struct ws_win
*win
= NULL
;
2933 xcb_query_pointer_reply_t
*r
;
2935 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: root: 0x%x.\n", root
);
2937 r
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, root
), NULL
);
2939 win
= find_window(r
->child
);
2941 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: 0x%x.\n",
2944 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: none.\n");
2953 root_to_region(xcb_window_t root
, int check
)
2956 struct swm_region
*r
= NULL
;
2958 xcb_query_pointer_reply_t
*qpr
;
2959 xcb_get_input_focus_reply_t
*gifr
;
2961 DNPRINTF(SWM_D_MISC
, "root_to_region: window: 0x%x\n", root
);
2963 num_screens
= get_screen_count();
2964 for (i
= 0; i
< num_screens
; i
++)
2965 if (screens
[i
].root
== root
)
2968 if (check
& SWM_CK_REGION
)
2969 r
= screens
[i
].r_focus
;
2971 if (r
== NULL
&& check
& SWM_CK_FOCUS
) {
2972 /* Try to find an actively focused window */
2973 gifr
= xcb_get_input_focus_reply(conn
,
2974 xcb_get_input_focus(conn
), NULL
);
2976 cfw
= find_window(gifr
->focus
);
2977 if (cfw
&& cfw
->ws
->r
)
2984 if (r
== NULL
&& check
& SWM_CK_POINTER
) {
2985 /* No region with an active focus; try to use pointer. */
2986 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
,
2987 screens
[i
].root
), NULL
);
2990 DNPRINTF(SWM_D_MISC
, "root_to_region: pointer: "
2991 "(%d,%d)\n", qpr
->root_x
, qpr
->root_y
);
2992 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2993 if (X(r
) <= qpr
->root_x
&&
2994 qpr
->root_x
< MAX_X(r
) &&
2995 Y(r
) <= qpr
->root_y
&&
2996 qpr
->root_y
< MAX_Y(r
))
3003 if (r
== NULL
&& check
& SWM_CK_FALLBACK
)
3004 r
= TAILQ_FIRST(&screens
[i
].rl
);
3006 DNPRINTF(SWM_D_MISC
, "root_to_region: idx: %d\n", get_region_index(r
));
3012 find_unmanaged_window(xcb_window_t id
)
3015 int i
, j
, num_screens
;
3017 num_screens
= get_screen_count();
3018 for (i
= 0; i
< num_screens
; i
++)
3019 for (j
= 0; j
< workspace_limit
; j
++)
3020 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].unmanagedlist
,
3028 find_window(xcb_window_t id
)
3031 int i
, j
, num_screens
;
3032 xcb_query_tree_reply_t
*r
;
3034 num_screens
= get_screen_count();
3035 for (i
= 0; i
< num_screens
; i
++)
3036 for (j
= 0; j
< workspace_limit
; j
++)
3037 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
3041 r
= xcb_query_tree_reply(conn
, xcb_query_tree(conn
, id
), NULL
);
3045 /* if we were looking for the parent return that window instead */
3046 if (r
->parent
== 0 || r
->root
== r
->parent
) {
3051 /* look for parent */
3052 for (i
= 0; i
< num_screens
; i
++)
3053 for (j
= 0; j
< workspace_limit
; j
++)
3054 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
3055 if (r
->parent
== win
->id
) {
3065 spawn(int ws_idx
, union arg
*args
, int close_fd
)
3070 DNPRINTF(SWM_D_MISC
, "spawn: %s\n", args
->argv
[0]);
3072 close(xcb_get_file_descriptor(conn
));
3074 setenv("LD_PRELOAD", SWM_LIB
, 1);
3076 if (asprintf(&ret
, "%d", ws_idx
) == -1) {
3077 warn("spawn: asprintf SWM_WS");
3080 setenv("_SWM_WS", ret
, 1);
3084 if (asprintf(&ret
, "%d", getpid()) == -1) {
3085 warn("spawn: asprintf _SWM_PID");
3088 setenv("_SWM_PID", ret
, 1);
3092 if (setsid() == -1) {
3093 warn("spawn: setsid");
3099 * close stdin and stdout to prevent interaction between apps
3100 * and the baraction script
3101 * leave stderr open to record errors
3103 if ((fd
= open(_PATH_DEVNULL
, O_RDWR
, 0)) == -1) {
3104 warn("spawn: open");
3107 dup2(fd
, STDIN_FILENO
);
3108 dup2(fd
, STDOUT_FILENO
);
3113 execvp(args
->argv
[0], args
->argv
);
3115 warn("spawn: execvp");
3120 kill_refs(struct ws_win
*win
)
3122 int i
, x
, num_screens
;
3123 struct swm_region
*r
;
3124 struct workspace
*ws
;
3129 num_screens
= get_screen_count();
3130 for (i
= 0; i
< num_screens
; i
++)
3131 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3132 for (x
= 0; x
< workspace_limit
; x
++) {
3134 if (win
== ws
->focus
)
3136 if (win
== ws
->focus_prev
)
3137 ws
->focus_prev
= NULL
;
3142 validate_win(struct ws_win
*testwin
)
3145 struct workspace
*ws
;
3146 struct swm_region
*r
;
3147 int i
, x
, num_screens
;
3149 if (testwin
== NULL
)
3152 num_screens
= get_screen_count();
3153 for (i
= 0; i
< num_screens
; i
++)
3154 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3155 for (x
= 0; x
< workspace_limit
; x
++) {
3157 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
3165 validate_ws(struct workspace
*testws
)
3167 struct swm_region
*r
;
3168 struct workspace
*ws
;
3169 int i
, x
, num_screens
;
3171 /* validate all ws */
3172 num_screens
= get_screen_count();
3173 for (i
= 0; i
< num_screens
; i
++)
3174 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3175 for (x
= 0; x
< workspace_limit
; x
++) {
3184 unfocus_win(struct ws_win
*win
)
3186 xcb_window_t none
= XCB_WINDOW_NONE
;
3188 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: window: 0x%x\n", WINID(win
));
3192 if (win
->ws
== NULL
) {
3193 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: NULL ws.\n");
3197 if (validate_ws(win
->ws
)) {
3198 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: invalid ws.\n");
3202 if (win
->ws
->r
== NULL
) {
3203 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: NULL region.\n");
3207 if (validate_win(win
)) {
3208 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: invalid win.\n");
3213 if (win
->ws
->focus
== win
) {
3214 win
->ws
->focus
= NULL
;
3215 win
->ws
->focus_prev
= win
;
3218 if (validate_win(win
->ws
->focus
)) {
3219 kill_refs(win
->ws
->focus
);
3220 win
->ws
->focus
= NULL
;
3223 if (validate_win(win
->ws
->focus_prev
)) {
3224 kill_refs(win
->ws
->focus_prev
);
3225 win
->ws
->focus_prev
= NULL
;
3228 xcb_change_window_attributes(conn
, win
->id
, XCB_CW_BORDER_PIXEL
,
3229 &win
->ws
->r
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
);
3231 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
3232 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1, &none
);
3234 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: done.\n");
3238 focus_win(struct ws_win
*win
)
3240 struct ws_win
*cfw
= NULL
, *parent
= NULL
, *w
;
3241 struct workspace
*ws
;
3242 xcb_get_input_focus_reply_t
*r
;
3244 DNPRINTF(SWM_D_FOCUS
, "focus_win: window: 0x%x\n", WINID(win
));
3249 if (win
->ws
== NULL
)
3257 if (validate_ws(ws
))
3260 if (validate_win(win
)) {
3265 r
= xcb_get_input_focus_reply(conn
, xcb_get_input_focus(conn
), NULL
);
3267 cfw
= find_window(r
->focus
);
3273 if (ws
->focus
!= win
) {
3274 if (ws
->focus
&& ws
->focus
!= cfw
)
3275 unfocus_win(ws
->focus
);
3279 /* If this window directs focus to a child window, then clear. */
3280 if (win
->focus_child
)
3281 win
->focus_child
= NULL
;
3283 /* If transient, adjust parent's focus child for focus_magic. */
3284 if (win
->transient
) {
3285 parent
= find_window(win
->transient
);
3286 if (parent
&& parent
->focus_child
!= win
)
3287 parent
->focus_child
= win
;
3290 if (cfw
!= win
&& ws
->r
!= NULL
) {
3291 /* Set input focus if no input hint, or indicated by hint. */
3292 if (!(win
->hints
.flags
& XCB_ICCCM_WM_HINT_INPUT
) ||
3293 (win
->hints
.flags
& XCB_ICCCM_WM_HINT_INPUT
&&
3295 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
,
3296 win
->id
, last_event_time
);
3298 /* Tell app it can adjust focus to a specific window. */
3299 if (win
->take_focus
) {
3300 /* java is special; always tell parent */
3301 if (win
->transient
&& win
->java
)
3302 client_msg(parent
, a_takefocus
,
3305 client_msg(win
, a_takefocus
, last_event_time
);
3308 xcb_change_window_attributes(conn
, win
->id
, XCB_CW_BORDER_PIXEL
,
3309 &ws
->r
->s
->c
[SWM_S_COLOR_FOCUS
].pixel
);
3311 if (ws
->cur_layout
->flags
& SWM_L_MAPONFOCUS
||
3313 /* If a parent exists, map it first. */
3315 map_window(parent
, XCB_WINDOW_NONE
);
3317 /* Map siblings next. */
3318 TAILQ_FOREACH(w
, &ws
->winlist
, entry
)
3319 if (w
!= win
&& !w
->iconic
&&
3320 w
->transient
== parent
->id
)
3321 map_window(w
, XCB_WINDOW_NONE
);
3324 /* Map focused window. */
3325 map_window(win
, XCB_WINDOW_NONE
);
3327 /* Finally, map children of focus window. */
3328 TAILQ_FOREACH(w
, &ws
->winlist
, entry
)
3329 if (w
->transient
== win
->id
&& !w
->iconic
)
3330 map_window(w
, XCB_WINDOW_NONE
);
3335 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
3336 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1,
3343 DNPRINTF(SWM_D_FOCUS
, "focus_win: done.\n");
3346 /* If a child window should have focus instead, return it. */
3348 get_focus_magic(struct ws_win
*win
)
3350 struct ws_win
*parent
= NULL
;
3351 struct ws_win
*child
= NULL
;
3353 DNPRINTF(SWM_D_FOCUS
, "get_focus_magic: window: 0x%x\n", WINID(win
));
3357 if (win
->transient
) {
3358 parent
= find_window(win
->transient
);
3360 /* If parent prefers focus elsewhere, then try to do so. */
3361 if (parent
&& (child
= parent
->focus_child
)) {
3362 if (validate_win(child
) == 0 && child
->mapped
)
3365 parent
->focus_child
= NULL
;
3369 /* If this window prefers focus elsewhere, then try to do so. */
3370 if ((child
= win
->focus_child
)) {
3371 if (validate_win(child
) == 0 && child
->mapped
)
3374 win
->focus_child
= NULL
;
3381 event_drain(uint8_t rt
)
3383 xcb_generic_event_t
*evt
;
3385 /* ensure all pending requests have been processed before filtering. */
3387 while ((evt
= xcb_poll_for_event(conn
))) {
3388 if (XCB_EVENT_RESPONSE_TYPE(evt
) != rt
)
3396 set_region(struct swm_region
*r
)
3398 struct swm_region
*rf
;
3404 /* Unfocus old region bar. */
3409 xcb_change_window_attributes(conn
, rf
->bar
->id
,
3410 XCB_CW_BORDER_PIXEL
,
3411 &r
->s
->c
[SWM_S_COLOR_BAR_BORDER_UNFOCUS
].pixel
);
3414 /* Set region bar border to focus_color. */
3415 xcb_change_window_attributes(conn
, r
->bar
->id
,
3416 XCB_CW_BORDER_PIXEL
, &r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].pixel
);
3422 focus_region(struct swm_region
*r
)
3425 struct swm_region
*old_r
;
3430 old_r
= r
->s
->r_focus
;
3433 nfw
= get_region_focus(r
);
3437 /* New region is empty; need to manually unfocus win. */
3439 unfocus_win(old_r
->ws
->focus
);
3441 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
, r
->id
,
3444 /* Clear bar since empty. */
3450 switchws(struct swm_region
*r
, union arg
*args
)
3452 int wsid
= args
->id
, unmap_old
= 0;
3453 struct swm_region
*this_r
, *other_r
;
3455 struct workspace
*new_ws
, *old_ws
;
3460 if (wsid
>= workspace_limit
)
3464 old_ws
= this_r
->ws
;
3465 new_ws
= &this_r
->s
->ws
[wsid
];
3467 DNPRINTF(SWM_D_WS
, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
3468 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), old_ws
->idx
, wsid
);
3470 if (new_ws
== NULL
|| old_ws
== NULL
)
3472 if (new_ws
== old_ws
)
3475 unfocus_win(old_ws
->focus
);
3477 other_r
= new_ws
->r
;
3478 if (other_r
== NULL
) {
3479 /* the other workspace is hidden, hide this one */
3483 /* the other ws is visible in another region, exchange them */
3484 other_r
->ws_prior
= new_ws
;
3485 other_r
->ws
= old_ws
;
3486 old_ws
->r
= other_r
;
3488 this_r
->ws_prior
= old_ws
;
3489 this_r
->ws
= new_ws
;
3492 /* Set focus_pending before stacking, if needed. */
3493 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& (!new_ws
->focus_pending
||
3494 validate_win(new_ws
->focus_pending
)))
3495 new_ws
->focus_pending
= get_region_focus(new_ws
->r
);
3499 /* unmap old windows */
3501 TAILQ_FOREACH(win
, &old_ws
->winlist
, entry
)
3504 /* if workspaces were swapped, then don't wait to set focus */
3505 if (old_ws
->r
&& focus_mode
!= SWM_FOCUS_FOLLOW
) {
3506 if (new_ws
->focus_pending
) {
3507 focus_win(new_ws
->focus_pending
);
3508 new_ws
->focus_pending
= NULL
;
3512 /* Clear bar and set focus on region input win if new ws is empty. */
3513 if (new_ws
->focus_pending
== NULL
&& new_ws
->focus
== NULL
) {
3514 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
, r
->id
,
3521 DNPRINTF(SWM_D_WS
, "switchws: done.\n");
3525 cyclews(struct swm_region
*r
, union arg
*args
)
3528 struct swm_screen
*s
= r
->s
;
3532 DNPRINTF(SWM_D_WS
, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3533 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
3539 case SWM_ARG_ID_CYCLEWS_MOVE_UP
:
3542 case SWM_ARG_ID_CYCLEWS_UP_ALL
:
3545 case SWM_ARG_ID_CYCLEWS_UP
:
3546 a
.id
= (a
.id
< workspace_limit
- 1) ? a
.id
+ 1 : 0;
3548 case SWM_ARG_ID_CYCLEWS_MOVE_DOWN
:
3551 case SWM_ARG_ID_CYCLEWS_DOWN_ALL
:
3554 case SWM_ARG_ID_CYCLEWS_DOWN
:
3555 a
.id
= (a
.id
> 0) ? a
.id
- 1 : workspace_limit
- 1;
3562 (!cycle_empty
&& TAILQ_EMPTY(&s
->ws
[a
.id
].winlist
)))
3564 if (!cycle_visible
&& s
->ws
[a
.id
].r
!= NULL
)
3571 } while (a
.id
!= r
->ws
->idx
);
3575 priorws(struct swm_region
*r
, union arg
*args
)
3581 DNPRINTF(SWM_D_WS
, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3582 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
3584 if (r
->ws_prior
== NULL
)
3587 a
.id
= r
->ws_prior
->idx
;
3592 focusrg(struct swm_region
*r
, union arg
*args
)
3594 int ridx
= args
->id
, i
, num_screens
;
3595 struct swm_region
*rr
= NULL
;
3597 num_screens
= get_screen_count();
3598 /* do nothing if we don't have more than one screen */
3599 if (!(num_screens
> 1 || outputs
> 1))
3602 DNPRINTF(SWM_D_FOCUS
, "focusrg: id: %d\n", ridx
);
3604 rr
= TAILQ_FIRST(&r
->s
->rl
);
3605 for (i
= 0; i
< ridx
; ++i
)
3606 rr
= TAILQ_NEXT(rr
, entry
);
3616 cyclerg(struct swm_region
*r
, union arg
*args
)
3618 struct swm_region
*rr
= NULL
;
3621 num_screens
= get_screen_count();
3622 /* do nothing if we don't have more than one screen */
3623 if (!(num_screens
> 1 || outputs
> 1))
3628 case SWM_ARG_ID_CYCLERG_UP
:
3629 rr
= TAILQ_NEXT(r
, entry
);
3631 rr
= TAILQ_FIRST(&screens
[i
].rl
);
3633 case SWM_ARG_ID_CYCLERG_DOWN
:
3634 rr
= TAILQ_PREV(r
, swm_region_list
, entry
);
3636 rr
= TAILQ_LAST(&screens
[i
].rl
, swm_region_list
);
3649 sort_windows(struct ws_win_list
*wl
)
3651 struct ws_win
*win
, *parent
, *nxt
;
3656 for (win
= TAILQ_FIRST(wl
); win
!= TAILQ_END(wl
); win
= nxt
) {
3657 nxt
= TAILQ_NEXT(win
, entry
);
3658 if (win
->transient
) {
3659 parent
= find_window(win
->transient
);
3660 if (parent
== NULL
) {
3661 warnx("not possible bug");
3664 TAILQ_REMOVE(wl
, win
, entry
);
3665 TAILQ_INSERT_AFTER(wl
, parent
, win
, entry
);
3672 swapwin(struct swm_region
*r
, union arg
*args
)
3674 struct ws_win
*target
, *source
;
3675 struct ws_win
*cur_focus
;
3676 struct ws_win_list
*wl
;
3678 DNPRINTF(SWM_D_WS
, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3679 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
3681 cur_focus
= r
->ws
->focus
;
3682 if (cur_focus
== NULL
)
3686 wl
= &source
->ws
->winlist
;
3689 case SWM_ARG_ID_SWAPPREV
:
3690 if (source
->transient
)
3691 source
= find_window(source
->transient
);
3692 target
= TAILQ_PREV(source
, ws_win_list
, entry
);
3693 if (target
&& target
->transient
)
3694 target
= find_window(target
->transient
);
3695 TAILQ_REMOVE(wl
, source
, entry
);
3697 TAILQ_INSERT_TAIL(wl
, source
, entry
);
3699 TAILQ_INSERT_BEFORE(target
, source
, entry
);
3701 case SWM_ARG_ID_SWAPNEXT
:
3702 target
= TAILQ_NEXT(source
, entry
);
3703 /* move the parent and let the sort handle the move */
3704 if (source
->transient
)
3705 source
= find_window(source
->transient
);
3706 TAILQ_REMOVE(wl
, source
, entry
);
3708 TAILQ_INSERT_HEAD(wl
, source
, entry
);
3710 TAILQ_INSERT_AFTER(wl
, target
, source
, entry
);
3712 case SWM_ARG_ID_SWAPMAIN
:
3713 target
= TAILQ_FIRST(wl
);
3714 if (target
== source
) {
3715 if (source
->ws
->focus_prev
!= NULL
&&
3716 source
->ws
->focus_prev
!= target
)
3717 source
= source
->ws
->focus_prev
;
3721 if (target
== NULL
|| source
== NULL
)
3723 source
->ws
->focus_prev
= target
;
3724 TAILQ_REMOVE(wl
, target
, entry
);
3725 TAILQ_INSERT_BEFORE(source
, target
, entry
);
3726 TAILQ_REMOVE(wl
, source
, entry
);
3727 TAILQ_INSERT_HEAD(wl
, source
, entry
);
3729 case SWM_ARG_ID_MOVELAST
:
3730 TAILQ_REMOVE(wl
, source
, entry
);
3731 TAILQ_INSERT_TAIL(wl
, source
, entry
);
3734 DNPRINTF(SWM_D_MOVE
, "swapwin: invalid id: %d\n", args
->id
);
3746 get_focus_prev(struct ws_win
*win
)
3748 struct ws_win
*winfocus
= NULL
;
3749 struct ws_win
*cur_focus
= NULL
;
3750 struct ws_win_list
*wl
= NULL
;
3751 struct workspace
*ws
= NULL
;
3753 if (!(win
&& win
->ws
))
3758 cur_focus
= ws
->focus
;
3760 DNPRINTF(SWM_D_FOCUS
, "get_focus_prev: window: 0x%x, cur_focus: 0x%x\n",
3761 WINID(win
), WINID(cur_focus
));
3763 /* pickle, just focus on whatever */
3764 if (cur_focus
== NULL
) {
3765 /* use prev_focus if valid */
3766 if (ws
->focus_prev
&& ws
->focus_prev
!= cur_focus
&&
3767 find_window(WINID(ws
->focus_prev
)))
3768 winfocus
= ws
->focus_prev
;
3772 /* if transient focus on parent */
3773 if (cur_focus
->transient
) {
3774 winfocus
= find_window(cur_focus
->transient
);
3778 /* if in max_stack try harder */
3779 if ((win
->quirks
& SWM_Q_FOCUSPREV
) ||
3780 (ws
->cur_layout
->flags
& SWM_L_FOCUSPREV
)) {
3781 if (cur_focus
!= ws
->focus_prev
)
3782 winfocus
= ws
->focus_prev
;
3784 winfocus
= TAILQ_PREV(win
, ws_win_list
, entry
);
3789 DNPRINTF(SWM_D_FOCUS
, "get_focus_prev: focus_close: %d\n", focus_close
);
3791 if (winfocus
== NULL
|| winfocus
== win
) {
3792 switch (focus_close
) {
3793 case SWM_STACK_BOTTOM
:
3794 TAILQ_FOREACH(winfocus
, wl
, entry
)
3795 if (!winfocus
->iconic
&& winfocus
!= cur_focus
)
3799 TAILQ_FOREACH_REVERSE(winfocus
, wl
, ws_win_list
, entry
)
3800 if (!winfocus
->iconic
&& winfocus
!= cur_focus
)
3803 case SWM_STACK_ABOVE
:
3804 winfocus
= TAILQ_NEXT(cur_focus
, entry
);
3805 while (winfocus
&& winfocus
->iconic
)
3806 winfocus
= TAILQ_NEXT(winfocus
, entry
);
3808 if (winfocus
== NULL
) {
3809 if (focus_close_wrap
) {
3810 TAILQ_FOREACH(winfocus
, wl
, entry
)
3811 if (!winfocus
->iconic
&&
3812 winfocus
!= cur_focus
)
3815 TAILQ_FOREACH_REVERSE(winfocus
, wl
,
3817 if (!winfocus
->iconic
&&
3818 winfocus
!= cur_focus
)
3823 case SWM_STACK_BELOW
:
3824 winfocus
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
3825 while (winfocus
&& winfocus
->iconic
)
3826 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
,
3829 if (winfocus
== NULL
) {
3830 if (focus_close_wrap
) {
3831 TAILQ_FOREACH_REVERSE(winfocus
, wl
,
3833 if (!winfocus
->iconic
&&
3834 winfocus
!= cur_focus
)
3837 TAILQ_FOREACH(winfocus
, wl
, entry
)
3838 if (!winfocus
->iconic
&&
3839 winfocus
!= cur_focus
)
3847 if (winfocus
== NULL
||
3848 (winfocus
&& (winfocus
->iconic
|| winfocus
== cur_focus
))) {
3849 if (focus_default
== SWM_STACK_TOP
) {
3850 TAILQ_FOREACH_REVERSE(winfocus
, wl
, ws_win_list
, entry
)
3851 if (!winfocus
->iconic
&& winfocus
!= cur_focus
)
3854 TAILQ_FOREACH(winfocus
, wl
, entry
)
3855 if (!winfocus
->iconic
&& winfocus
!= cur_focus
)
3862 return get_focus_magic(winfocus
);
3866 get_region_focus(struct swm_region
*r
)
3868 struct ws_win
*winfocus
= NULL
;
3873 if (r
->ws
->focus
&& !r
->ws
->focus
->iconic
)
3874 winfocus
= r
->ws
->focus
;
3875 else if (r
->ws
->focus_prev
&& !r
->ws
->focus_prev
->iconic
)
3876 winfocus
= r
->ws
->focus_prev
;
3878 TAILQ_FOREACH(winfocus
, &r
->ws
->winlist
, entry
)
3879 if (!winfocus
->iconic
)
3882 return get_focus_magic(winfocus
);
3886 focus(struct swm_region
*r
, union arg
*args
)
3888 struct ws_win
*head
, *cur_focus
= NULL
, *winfocus
= NULL
;
3889 struct ws_win_list
*wl
= NULL
;
3890 struct workspace
*ws
= NULL
;
3896 DNPRINTF(SWM_D_FOCUS
, "focus: id: %d\n", args
->id
);
3898 if ((cur_focus
= r
->ws
->focus
) == NULL
)
3902 if (TAILQ_EMPTY(wl
))
3904 /* make sure there is at least one uniconified window */
3906 TAILQ_FOREACH(winfocus
, wl
, entry
)
3907 if (!winfocus
->iconic
) {
3915 case SWM_ARG_ID_FOCUSPREV
:
3916 head
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
3918 head
= TAILQ_LAST(wl
, ws_win_list
);
3920 if (WINID(winfocus
) == cur_focus
->transient
) {
3921 head
= TAILQ_PREV(winfocus
, ws_win_list
, entry
);
3923 head
= TAILQ_LAST(wl
, ws_win_list
);
3928 if (winfocus
&& winfocus
->iconic
) {
3929 while (winfocus
!= cur_focus
) {
3930 if (winfocus
== NULL
)
3931 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
3932 if (!winfocus
->iconic
)
3934 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
,
3940 case SWM_ARG_ID_FOCUSNEXT
:
3941 head
= TAILQ_NEXT(cur_focus
, entry
);
3943 head
= TAILQ_FIRST(wl
);
3947 if (winfocus
&& winfocus
->iconic
) {
3948 while (winfocus
!= cur_focus
) {
3949 if (winfocus
== NULL
)
3950 winfocus
= TAILQ_FIRST(wl
);
3951 if (!winfocus
->iconic
)
3953 winfocus
= TAILQ_NEXT(winfocus
, entry
);
3958 case SWM_ARG_ID_FOCUSMAIN
:
3959 winfocus
= TAILQ_FIRST(wl
);
3960 if (winfocus
== cur_focus
)
3961 winfocus
= cur_focus
->ws
->focus_prev
;
3968 focus_win(get_focus_magic(winfocus
));
3974 cycle_layout(struct swm_region
*r
, union arg
*args
)
3976 struct workspace
*ws
= r
->ws
;
3978 /* suppress unused warning since var is needed */
3981 DNPRINTF(SWM_D_EVENT
, "cycle_layout: workspace: %d\n", ws
->idx
);
3984 if (ws
->cur_layout
->l_stack
== NULL
)
3985 ws
->cur_layout
= &layouts
[0];
3990 focus_win(get_region_focus(r
));
3996 stack_config(struct swm_region
*r
, union arg
*args
)
3998 struct workspace
*ws
= r
->ws
;
4000 DNPRINTF(SWM_D_STACK
, "stack_config: id: %d workspace: %d\n",
4003 if (ws
->cur_layout
->l_config
!= NULL
)
4004 ws
->cur_layout
->l_config(ws
, args
->id
);
4006 if (args
->id
!= SWM_ARG_ID_STACKINIT
)
4015 struct swm_geometry g
;
4016 struct swm_region
*r
;
4022 DNPRINTF(SWM_D_STACK
, "stack: begin\n");
4024 num_screens
= get_screen_count();
4025 for (i
= 0; i
< num_screens
; i
++) {
4029 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
4030 DNPRINTF(SWM_D_STACK
, "stack: workspace: %d "
4031 "(screen: %d, region: %d)\n", r
->ws
->idx
, i
, j
++);
4033 /* Adjust stack area for region bar and padding. */
4035 g
.x
+= region_padding
;
4036 g
.y
+= region_padding
;
4037 g
.w
-= 2 * border_width
+ 2 * region_padding
;
4038 g
.h
-= 2 * border_width
+ 2 * region_padding
;
4039 if (bar_enabled
&& r
->ws
->bar_enabled
) {
4044 r
->ws
->cur_layout
->l_stack(r
->ws
, &g
);
4045 r
->ws
->cur_layout
->l_string(r
->ws
);
4046 /* save r so we can track region changes */
4053 DNPRINTF(SWM_D_STACK
, "stack: end\n");
4057 store_float_geom(struct ws_win
*win
)
4059 if (win
== NULL
|| win
->ws
->r
== NULL
)
4062 /* retain window geom and region geom */
4063 win
->g_float
= win
->g
;
4064 win
->g_float
.x
-= X(win
->ws
->r
);
4065 win
->g_float
.y
-= Y(win
->ws
->r
);
4066 win
->g_floatvalid
= 1;
4067 DNPRINTF(SWM_D_MISC
, "store_float_geom: window: 0x%x, g: (%d,%d)"
4068 " %d x %d, g_float: (%d,%d) %d x %d\n", win
->id
, X(win
), Y(win
),
4069 WIDTH(win
), HEIGHT(win
), win
->g_float
.x
, win
->g_float
.y
,
4070 win
->g_float
.w
, win
->g_float
.h
);
4074 load_float_geom(struct ws_win
*win
)
4076 if (win
== NULL
|| win
->ws
->r
== NULL
)
4079 if (win
->g_floatvalid
) {
4080 win
->g
= win
->g_float
;
4081 X(win
) += X(win
->ws
->r
);
4082 Y(win
) += Y(win
->ws
->r
);
4083 DNPRINTF(SWM_D_MISC
, "load_float_geom: window: 0x%x, g: (%d,%d)"
4084 "%d x %d\n", win
->id
, X(win
), Y(win
), WIDTH(win
),
4087 DNPRINTF(SWM_D_MISC
, "load_float_geom: window: 0x%x, g_float "
4088 "is not set.\n", win
->id
);
4093 stack_floater(struct ws_win
*win
, struct swm_region
*r
)
4098 DNPRINTF(SWM_D_MISC
, "stack_floater: window: 0x%x\n", win
->id
);
4101 * to allow windows to change their size (e.g. mplayer fs) only retrieve
4102 * geom on ws switches or return from max mode
4104 if (win
->floatmaxed
|| (r
!= r
->ws
->old_r
&&
4105 !(win
->ewmh_flags
& EWMH_F_FULLSCREEN
))) {
4106 /* update geometry for the new region */
4107 load_float_geom(win
);
4110 win
->floatmaxed
= 0;
4113 * if set to fullscreen mode, configure window to maximum size.
4115 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
4116 if (!win
->g_floatvalid
)
4117 store_float_geom(win
);
4123 * remove border on fullscreen floater when in fullscreen mode or when
4124 * the quirk is present.
4126 if ((win
->ewmh_flags
& EWMH_F_FULLSCREEN
) ||
4127 ((win
->quirks
& SWM_Q_FULLSCREEN
) &&
4128 (WIDTH(win
) >= WIDTH(r
)) && (HEIGHT(win
) >= HEIGHT(r
)))) {
4129 if (win
->bordered
) {
4131 X(win
) += border_width
;
4132 Y(win
) += border_width
;
4134 } else if (!win
->bordered
) {
4136 X(win
) -= border_width
;
4137 Y(win
) -= border_width
;
4140 if (win
->transient
&& (win
->quirks
& SWM_Q_TRANSSZ
)) {
4141 WIDTH(win
) = (double)WIDTH(r
) * dialog_ratio
;
4142 HEIGHT(win
) = (double)HEIGHT(r
) * dialog_ratio
;
4145 if (!win
->manual
&& !(win
->ewmh_flags
& EWMH_F_FULLSCREEN
) &&
4146 !(win
->quirks
& SWM_Q_ANYWHERE
)) {
4148 * floaters and transients are auto-centred unless moved,
4149 * resized or ANYWHERE quirk is set.
4151 X(win
) = X(r
) + (WIDTH(r
) - WIDTH(win
)) / 2 - BORDER(win
);
4152 Y(win
) = Y(r
) + (HEIGHT(r
) - HEIGHT(win
)) / 2 - BORDER(win
);
4154 store_float_geom(win
);
4157 /* Ensure at least 1 pixel of the window is in the region. */
4158 region_containment(win
, r
, SWM_CW_ALLSIDES
);
4164 * Send keystrokes to terminal to decrease/increase the font size as the
4165 * window size changes.
4168 adjust_font(struct ws_win
*win
)
4170 if (!(win
->quirks
& SWM_Q_XTERM_FONTADJ
) ||
4171 win
->floating
|| win
->transient
)
4174 if (win
->sh
.width_inc
&& win
->last_inc
!= win
->sh
.width_inc
&&
4175 WIDTH(win
) / win
->sh
.width_inc
< term_width
&&
4176 win
->font_steps
< SWM_MAX_FONT_STEPS
) {
4177 win
->font_size_boundary
[win
->font_steps
] =
4178 (win
->sh
.width_inc
* term_width
) + win
->sh
.base_width
;
4181 win
->last_inc
= win
->sh
.width_inc
;
4182 fake_keypress(win
, XK_KP_Subtract
, XCB_MOD_MASK_SHIFT
);
4183 } else if (win
->font_steps
&& win
->last_inc
!= win
->sh
.width_inc
&&
4184 WIDTH(win
) > win
->font_size_boundary
[win
->font_steps
- 1]) {
4187 win
->last_inc
= win
->sh
.width_inc
;
4188 fake_keypress(win
, XK_KP_Add
, XCB_MOD_MASK_SHIFT
);
4192 #define SWAPXY(g) do { \
4194 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
4195 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
4198 stack_master(struct workspace
*ws
, struct swm_geometry
*g
, int rot
, int flip
)
4200 struct swm_geometry win_g
, r_g
= *g
;
4201 struct ws_win
*win
, *fs_win
= NULL
, *wtmp
;
4202 xcb_window_t sibling
;
4203 int i
, j
, s
, stacks
;
4204 int w_inc
= 1, h_inc
, w_base
= 1, h_base
;
4205 int hrh
, extra
= 0, h_slice
, last_h
= 0;
4206 int split
, colno
, winno
, mwin
, msize
, mscale
;
4207 int remain
, missing
, v_slice
, reconfigure
= 0;
4210 DNPRINTF(SWM_D_STACK
, "stack_master: workspace: %d, rot: %s, "
4211 "flip: %s\n", ws
->idx
, YESNO(rot
), YESNO(flip
));
4213 winno
= count_win(ws
, 0);
4214 if (winno
== 0 && count_win(ws
, 1) == 0)
4217 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
4218 if (!win
->transient
&& !win
->floating
&& !win
->iconic
)
4225 w_inc
= win
->sh
.width_inc
;
4226 w_base
= win
->sh
.base_width
;
4227 mwin
= ws
->l_state
.horizontal_mwin
;
4228 mscale
= ws
->l_state
.horizontal_msize
;
4229 stacks
= ws
->l_state
.horizontal_stacks
;
4232 w_inc
= win
->sh
.height_inc
;
4233 w_base
= win
->sh
.base_height
;
4234 mwin
= ws
->l_state
.vertical_mwin
;
4235 mscale
= ws
->l_state
.vertical_msize
;
4236 stacks
= ws
->l_state
.vertical_stacks
;
4240 if (stacks
> winno
- mwin
)
4241 stacks
= winno
- mwin
;
4245 h_slice
= r_g
.h
/ SWM_H_SLICE
;
4246 if (mwin
&& winno
> mwin
) {
4247 v_slice
= r_g
.w
/ SWM_V_SLICE
;
4251 win_g
.w
= v_slice
* mscale
;
4253 if (w_inc
> 1 && w_inc
< v_slice
) {
4254 /* adjust for window's requested size increment */
4255 remain
= (win_g
.w
- w_base
) % w_inc
;
4261 win_g
.x
+= r_g
.w
- msize
;
4264 colno
= split
= winno
/ stacks
;
4265 win_g
.w
= ((r_g
.w
- (stacks
* 2 * border_width
) +
4266 2 * border_width
) / stacks
);
4268 hrh
= r_g
.h
/ colno
;
4269 extra
= r_g
.h
- (colno
* hrh
);
4270 win_g
.h
= hrh
- 2 * border_width
;
4272 /* stack all the tiled windows */
4273 i
= j
= 0, s
= stacks
;
4274 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
4275 if (win
->transient
|| win
->floating
|| win
->iconic
)
4278 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
4283 if (split
&& i
== split
) {
4284 colno
= (winno
- mwin
) / stacks
;
4285 if (s
<= (winno
- mwin
) % stacks
)
4288 hrh
= r_g
.h
/ colno
;
4289 extra
= r_g
.h
- (colno
* hrh
);
4292 win_g
.x
+= win_g
.w
+ 2 * border_width
+
4295 win_g
.w
= (r_g
.w
- msize
-
4296 (stacks
* (2 * border_width
+ tile_gap
))) / stacks
;
4298 win_g
.w
+= (r_g
.w
- msize
-
4299 (stacks
* (2 * border_width
+ tile_gap
))) %
4303 win_g
.x
-= win_g
.w
+ 2 * border_width
+
4309 win_g
.h
= hrh
- 2 * border_width
- tile_gap
;
4312 h_inc
= win
->sh
.width_inc
;
4313 h_base
= win
->sh
.base_width
;
4315 h_inc
= win
->sh
.height_inc
;
4316 h_base
= win
->sh
.base_height
;
4319 if (j
== colno
- 1) {
4320 win_g
.h
= hrh
+ extra
;
4321 } else if (h_inc
> 1 && h_inc
< h_slice
) {
4322 /* adjust for window's requested size increment */
4323 remain
= (win_g
.h
- h_base
) % h_inc
;
4324 missing
= h_inc
- remain
;
4326 if (missing
<= extra
|| j
== 0) {
4338 win_g
.y
+= last_h
+ 2 * border_width
+ tile_gap
;
4340 if (disable_border
&& !(bar_enabled
&& ws
->bar_enabled
) &&
4343 win_g
.w
+= 2 * border_width
;
4344 win_g
.h
+= 2 * border_width
;
4350 if (X(win
) != win_g
.y
|| Y(win
) != win_g
.x
||
4351 WIDTH(win
) != win_g
.h
|| HEIGHT(win
) != win_g
.w
) {
4355 WIDTH(win
) = win_g
.h
;
4356 HEIGHT(win
) = win_g
.w
;
4359 if (X(win
) != win_g
.x
|| Y(win
) != win_g
.y
||
4360 WIDTH(win
) != win_g
.w
|| HEIGHT(win
) != win_g
.h
) {
4364 WIDTH(win
) = win_g
.w
;
4365 HEIGHT(win
) = win_g
.h
;
4369 if (bordered
!= win
->bordered
) {
4371 win
->bordered
= bordered
;
4379 wtmp
= TAILQ_PREV(win
, ws_win_list
, entry
);
4383 sibling
= ws
->r
->bar
->id
;
4385 map_window(win
, sibling
);
4393 /* now, stack all the floaters and transients */
4394 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
4395 if (!win
->transient
&& !win
->floating
)
4399 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
4404 stack_floater(win
, ws
->r
);
4405 map_window(win
, XCB_WINDOW_NONE
);
4408 /* Make sure fs_win is stacked last so it's on top. */
4410 stack_floater(fs_win
, ws
->r
);
4411 map_window(fs_win
, XCB_WINDOW_NONE
);
4416 vertical_config(struct workspace
*ws
, int id
)
4418 DNPRINTF(SWM_D_STACK
, "vertical_config: id: %d, workspace: %d\n",
4422 case SWM_ARG_ID_STACKRESET
:
4423 case SWM_ARG_ID_STACKINIT
:
4424 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ 2;
4425 ws
->l_state
.vertical_mwin
= 1;
4426 ws
->l_state
.vertical_stacks
= 1;
4428 case SWM_ARG_ID_MASTERSHRINK
:
4429 if (ws
->l_state
.vertical_msize
> 1)
4430 ws
->l_state
.vertical_msize
--;
4432 case SWM_ARG_ID_MASTERGROW
:
4433 if (ws
->l_state
.vertical_msize
< SWM_V_SLICE
- 1)
4434 ws
->l_state
.vertical_msize
++;
4436 case SWM_ARG_ID_MASTERADD
:
4437 ws
->l_state
.vertical_mwin
++;
4439 case SWM_ARG_ID_MASTERDEL
:
4440 if (ws
->l_state
.vertical_mwin
> 0)
4441 ws
->l_state
.vertical_mwin
--;
4443 case SWM_ARG_ID_STACKINC
:
4444 ws
->l_state
.vertical_stacks
++;
4446 case SWM_ARG_ID_STACKDEC
:
4447 if (ws
->l_state
.vertical_stacks
> 1)
4448 ws
->l_state
.vertical_stacks
--;
4450 case SWM_ARG_ID_FLIPLAYOUT
:
4451 ws
->l_state
.vertical_flip
= !ws
->l_state
.vertical_flip
;
4459 vertical_stack(struct workspace
*ws
, struct swm_geometry
*g
)
4461 DNPRINTF(SWM_D_STACK
, "vertical_stack: workspace: %d\n", ws
->idx
);
4463 stack_master(ws
, g
, 0, ws
->l_state
.vertical_flip
);
4467 horizontal_config(struct workspace
*ws
, int id
)
4469 DNPRINTF(SWM_D_STACK
, "horizontal_config: workspace: %d\n", ws
->idx
);
4472 case SWM_ARG_ID_STACKRESET
:
4473 case SWM_ARG_ID_STACKINIT
:
4474 ws
->l_state
.horizontal_mwin
= 1;
4475 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ 2;
4476 ws
->l_state
.horizontal_stacks
= 1;
4478 case SWM_ARG_ID_MASTERSHRINK
:
4479 if (ws
->l_state
.horizontal_msize
> 1)
4480 ws
->l_state
.horizontal_msize
--;
4482 case SWM_ARG_ID_MASTERGROW
:
4483 if (ws
->l_state
.horizontal_msize
< SWM_H_SLICE
- 1)
4484 ws
->l_state
.horizontal_msize
++;
4486 case SWM_ARG_ID_MASTERADD
:
4487 ws
->l_state
.horizontal_mwin
++;
4489 case SWM_ARG_ID_MASTERDEL
:
4490 if (ws
->l_state
.horizontal_mwin
> 0)
4491 ws
->l_state
.horizontal_mwin
--;
4493 case SWM_ARG_ID_STACKINC
:
4494 ws
->l_state
.horizontal_stacks
++;
4496 case SWM_ARG_ID_STACKDEC
:
4497 if (ws
->l_state
.horizontal_stacks
> 1)
4498 ws
->l_state
.horizontal_stacks
--;
4500 case SWM_ARG_ID_FLIPLAYOUT
:
4501 ws
->l_state
.horizontal_flip
= !ws
->l_state
.horizontal_flip
;
4509 horizontal_stack(struct workspace
*ws
, struct swm_geometry
*g
)
4511 DNPRINTF(SWM_D_STACK
, "horizontal_stack: workspace: %d\n", ws
->idx
);
4513 stack_master(ws
, g
, 1, ws
->l_state
.horizontal_flip
);
4516 /* fullscreen view */
4518 max_stack(struct workspace
*ws
, struct swm_geometry
*g
)
4520 struct swm_geometry gg
= *g
;
4521 struct ws_win
*w
, *win
= NULL
, *parent
= NULL
;
4524 DNPRINTF(SWM_D_STACK
, "max_stack: workspace: %d\n", ws
->idx
);
4529 winno
= count_win(ws
, 0);
4530 if (winno
== 0 && count_win(ws
, 1) == 0)
4533 /* Figure out which top level window should be visible. */
4534 if (ws
->focus_pending
)
4535 win
= ws
->focus_pending
;
4538 else if (ws
->focus_prev
)
4539 win
= ws
->focus_prev
;
4541 win
= TAILQ_FIRST(&ws
->winlist
);
4544 parent
= find_window(win
->transient
);
4546 DNPRINTF(SWM_D_STACK
, "max_stack: win: 0x%x\n", win
->id
);
4548 /* maximize all top level windows */
4549 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
4550 if (w
->transient
|| w
->iconic
)
4553 if (!w
->mapped
&& w
!= win
)
4554 map_window(w
, XCB_WINDOW_NONE
);
4556 if (w
->floating
&& !w
->floatmaxed
) {
4558 * retain geometry for retrieval on exit from
4561 store_float_geom(w
);
4565 /* only reconfigure if necessary */
4566 if (X(w
) != gg
.x
|| Y(w
) != gg
.y
|| WIDTH(w
) != gg
.w
||
4567 HEIGHT(w
) != gg
.h
) {
4569 if (bar_enabled
&& ws
->bar_enabled
){
4573 WIDTH(w
) += 2 * border_width
;
4574 HEIGHT(w
) += 2 * border_width
;
4581 /* If a parent exists, map/raise it first. */
4583 map_window(parent
, XCB_WINDOW_NONE
);
4585 /* Map siblings next. */
4586 TAILQ_FOREACH(w
, &ws
->winlist
, entry
)
4587 if (w
!= win
&& !w
->iconic
&&
4588 w
->transient
== parent
->id
) {
4589 stack_floater(w
, ws
->r
);
4590 map_window(w
, XCB_WINDOW_NONE
);
4594 /* Map/raise focused window. */
4595 map_window(win
, XCB_WINDOW_NONE
);
4597 /* Finally, map/raise children of focus window. */
4598 TAILQ_FOREACH(w
, &ws
->winlist
, entry
)
4599 if (w
->transient
== win
->id
&& !w
->iconic
) {
4600 stack_floater(w
, ws
->r
);
4601 map_window(w
, XCB_WINDOW_NONE
);
4606 send_to_rg(struct swm_region
*r
, union arg
*args
)
4608 int ridx
= args
->id
, i
, num_screens
;
4609 struct swm_region
*rr
= NULL
;
4612 num_screens
= get_screen_count();
4613 /* do nothing if we don't have more than one screen */
4614 if (!(num_screens
> 1 || outputs
> 1))
4617 DNPRINTF(SWM_D_FOCUS
, "send_to_rg: id: %d\n", ridx
);
4619 rr
= TAILQ_FIRST(&r
->s
->rl
);
4620 for (i
= 0; i
< ridx
; ++i
)
4621 rr
= TAILQ_NEXT(rr
, entry
);
4632 region_under(struct swm_screen
*s
, int x
, int y
)
4634 struct swm_region
*r
= NULL
;
4636 TAILQ_FOREACH(r
, &s
->rl
, entry
) {
4637 DNPRINTF(SWM_D_MISC
, "region_under: ws: %d, region g: (%d,%d) "
4638 "%d x %d, coords: (%d,%d)\n", r
->ws
->idx
, X(r
), Y(r
),
4639 WIDTH(r
), HEIGHT(r
), x
, y
);
4640 if (X(r
) <= x
&& x
< MAX_X(r
))
4641 if (Y(r
) <= y
&& y
< MAX_Y(r
))
4649 send_to_ws(struct swm_region
*r
, union arg
*args
)
4651 int wsid
= args
->id
;
4652 struct ws_win
*win
= NULL
;
4654 if (r
&& r
->ws
&& r
->ws
->focus
)
4659 DNPRINTF(SWM_D_MOVE
, "send_to_ws: win 0x%x, ws %d\n", win
->id
, wsid
);
4661 win_to_ws(win
, wsid
, 1);
4663 /* Restack and set new focus. */
4666 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
4667 if (r
->ws
->focus_pending
) {
4668 focus_win(r
->ws
->focus_pending
);
4669 r
->ws
->focus_pending
= NULL
;
4671 xcb_set_input_focus(conn
,
4672 XCB_INPUT_FOCUS_PARENT
, r
->id
,
4681 win_to_ws(struct ws_win
*win
, int wsid
, int unfocus
)
4683 struct ws_win
*parent
;
4684 struct workspace
*ws
, *nws
, *pws
;
4685 char ws_idx_str
[SWM_PROPLEN
];
4687 if (wsid
>= workspace_limit
)
4690 if (win
->ws
->idx
== wsid
)
4694 nws
= &win
->s
->ws
[wsid
];
4696 DNPRINTF(SWM_D_MOVE
, "win_to_ws: win 0x%x, ws %d -> %d\n", win
->id
,
4699 /* Update the window's workspace property: _SWM_WS */
4700 if (snprintf(ws_idx_str
, SWM_PROPLEN
, "%d", nws
->idx
) < SWM_PROPLEN
) {
4701 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
4702 ws
->focus_pending
= get_focus_prev(win
);
4704 /* Move the parent if this is a transient window. */
4705 if (win
->transient
) {
4706 parent
= find_window(win
->transient
);
4709 /* Set new focus in parent's ws if needed. */
4710 if (pws
->focus
== parent
) {
4711 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
4712 pws
->focus_pending
=
4713 get_focus_prev(parent
);
4715 unfocus_win(parent
);
4717 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
4718 pws
->focus
= pws
->focus_pending
;
4719 pws
->focus_pending
= NULL
;
4723 /* Don't unmap parent if new ws is visible */
4725 unmap_window(parent
);
4728 TAILQ_REMOVE(&ws
->winlist
, parent
, entry
);
4729 TAILQ_INSERT_TAIL(&nws
->winlist
, parent
, entry
);
4732 DNPRINTF(SWM_D_PROP
, "win_to_ws: set "
4733 "property: _SWM_WS: %s\n", ws_idx_str
);
4734 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
4735 parent
->id
, a_swm_ws
, XCB_ATOM_STRING
, 8,
4736 strlen(ws_idx_str
), ws_idx_str
);
4743 /* Don't unmap if new ws is visible */
4748 TAILQ_REMOVE(&ws
->winlist
, win
, entry
);
4749 TAILQ_INSERT_TAIL(&nws
->winlist
, win
, entry
);
4752 /* Set focus on new ws. */
4753 unfocus_win(nws
->focus
);
4756 DNPRINTF(SWM_D_PROP
, "win_to_ws: set property: _SWM_WS: %s\n",
4758 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
4759 a_swm_ws
, XCB_ATOM_STRING
, 8, strlen(ws_idx_str
),
4763 DNPRINTF(SWM_D_MOVE
, "win_to_ws: done.\n");
4767 pressbutton(struct swm_region
*r
, union arg
*args
)
4769 /* suppress unused warning since var is needed */
4772 xcb_test_fake_input(conn
, XCB_BUTTON_PRESS
, args
->id
,
4773 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
4774 xcb_test_fake_input(conn
, XCB_BUTTON_RELEASE
, args
->id
,
4775 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
4779 raise_toggle(struct swm_region
*r
, union arg
*args
)
4781 /* suppress unused warning since var is needed */
4784 if (r
== NULL
|| r
->ws
== NULL
)
4787 r
->ws
->always_raise
= !r
->ws
->always_raise
;
4789 /* bring floaters back to top */
4790 if (!r
->ws
->always_raise
)
4797 iconify(struct swm_region
*r
, union arg
*args
)
4799 /* suppress unused warning since var is needed */
4802 if (r
->ws
->focus
== NULL
)
4805 set_swm_iconic(r
->ws
->focus
, 1);
4811 get_win_name(xcb_window_t win
)
4814 xcb_get_property_cookie_t c
;
4815 xcb_get_property_reply_t
*r
;
4817 /* First try _NET_WM_NAME for UTF-8. */
4818 c
= xcb_get_property(conn
, 0, win
, a_netwmname
,
4819 XCB_GET_PROPERTY_TYPE_ANY
, 0, UINT_MAX
);
4820 r
= xcb_get_property_reply(conn
, c
, NULL
);
4823 if (r
->type
== XCB_NONE
) {
4825 /* Use WM_NAME instead; no UTF-8. */
4826 c
= xcb_get_property(conn
, 0, win
, XCB_ATOM_WM_NAME
,
4827 XCB_GET_PROPERTY_TYPE_ANY
, 0, UINT_MAX
);
4828 r
= xcb_get_property_reply(conn
, c
, NULL
);
4832 if (r
->type
== XCB_NONE
) {
4838 name
= strndup(xcb_get_property_value(r
),
4839 xcb_get_property_value_length(r
));
4848 uniconify(struct swm_region
*r
, union arg
*args
)
4855 DNPRINTF(SWM_D_MISC
, "uniconify\n");
4857 if (r
== NULL
|| r
->ws
== NULL
)
4860 /* make sure we have anything to uniconify */
4861 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
4862 if (win
->ws
== NULL
)
4863 continue; /* should never happen */
4872 search_resp_action
= SWM_SEARCH_UNICONIFY
;
4874 spawn_select(r
, args
, "search", &searchpid
);
4876 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
4879 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
4880 if (win
->ws
== NULL
)
4881 continue; /* should never happen */
4885 name
= get_win_name(win
->id
);
4888 fprintf(lfile
, "%s.%u\n", name
, win
->id
);
4896 name_workspace(struct swm_region
*r
, union arg
*args
)
4900 DNPRINTF(SWM_D_MISC
, "name_workspace\n");
4906 search_resp_action
= SWM_SEARCH_NAME_WORKSPACE
;
4908 spawn_select(r
, args
, "name_workspace", &searchpid
);
4910 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
4913 fprintf(lfile
, "%s", "");
4918 search_workspace(struct swm_region
*r
, union arg
*args
)
4921 struct workspace
*ws
;
4924 DNPRINTF(SWM_D_MISC
, "search_workspace\n");
4930 search_resp_action
= SWM_SEARCH_SEARCH_WORKSPACE
;
4932 spawn_select(r
, args
, "search", &searchpid
);
4934 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
4937 for (i
= 0; i
< workspace_limit
; i
++) {
4941 fprintf(lfile
, "%d%s%s\n", ws
->idx
+ 1,
4942 (ws
->name
? ":" : ""), (ws
->name
? ws
->name
: ""));
4949 search_win_cleanup(void)
4951 struct search_window
*sw
= NULL
;
4953 while ((sw
= TAILQ_FIRST(&search_wl
)) != NULL
) {
4954 xcb_destroy_window(conn
, sw
->indicator
);
4955 TAILQ_REMOVE(&search_wl
, sw
, entry
);
4961 search_win(struct swm_region
*r
, union arg
*args
)
4963 struct ws_win
*win
= NULL
;
4964 struct search_window
*sw
= NULL
;
4967 int i
, width
, height
;
4975 XRectangle l_ibox
, l_lbox
;
4977 DNPRINTF(SWM_D_MISC
, "search_win\n");
4980 search_resp_action
= SWM_SEARCH_SEARCH_WINDOW
;
4982 spawn_select(r
, args
, "search", &searchpid
);
4984 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
4987 TAILQ_INIT(&search_wl
);
4990 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
4994 sw
= calloc(1, sizeof(struct search_window
));
4996 warn("search_win: calloc");
4998 search_win_cleanup();
5004 snprintf(s
, sizeof s
, "%d", i
);
5007 w
= xcb_generate_id(conn
);
5008 wa
[0] = r
->s
->c
[SWM_S_COLOR_FOCUS
].pixel
;
5009 wa
[1] = r
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
;
5011 if (bar_font_legacy
) {
5012 XmbTextExtents(bar_fs
, s
, len
, &l_ibox
, &l_lbox
);
5013 width
= l_lbox
.width
+ 4;
5014 height
= bar_fs_extents
->max_logical_extent
.height
+ 4;
5016 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
,
5018 width
= info
.width
+ 4;
5019 height
= bar_font
->height
+ 4;
5022 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, w
, win
->id
, 0, 0,
5023 width
, height
, 1, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
5024 XCB_COPY_FROM_PARENT
, XCB_CW_BACK_PIXEL
|
5025 XCB_CW_BORDER_PIXEL
, wa
);
5027 xcb_map_window(conn
, w
);
5030 TAILQ_INSERT_TAIL(&search_wl
, sw
, entry
);
5032 if (bar_font_legacy
) {
5033 l_gcv
.graphics_exposures
= 0;
5034 l_draw
= XCreateGC(display
, w
, 0, &l_gcv
);
5036 XSetForeground(display
, l_draw
,
5037 r
->s
->c
[SWM_S_COLOR_BAR
].pixel
);
5039 DRAWSTRING(display
, w
, bar_fs
, l_draw
, 2,
5040 (bar_fs_extents
->max_logical_extent
.height
-
5041 l_lbox
.height
) / 2 - l_lbox
.y
, s
, len
);
5043 XFreeGC(display
, l_draw
);
5046 draw
= XftDrawCreate(display
, w
,
5047 DefaultVisual(display
, r
->s
->idx
),
5048 DefaultColormap(display
, r
->s
->idx
));
5050 XftDrawStringUtf8(draw
, &bar_font_color
, bar_font
, 2,
5051 (HEIGHT(r
->bar
) + bar_font
->height
) / 2 -
5052 bar_font
->descent
, (FcChar8
*)s
, len
);
5054 XftDrawDestroy(draw
);
5057 DNPRINTF(SWM_D_MISC
, "search_win: mapped window: 0x%x\n", w
);
5059 fprintf(lfile
, "%d\n", i
);
5069 search_resp_uniconify(const char *resp
, size_t len
)
5075 DNPRINTF(SWM_D_MISC
, "search_resp_uniconify: resp: %s\n", resp
);
5077 TAILQ_FOREACH(win
, &search_r
->ws
->winlist
, entry
) {
5080 name
= get_win_name(win
->id
);
5083 if (asprintf(&s
, "%s.%u", name
, win
->id
) == -1) {
5088 if (strncmp(s
, resp
, len
) == 0) {
5089 /* XXX this should be a callback to generalize */
5090 set_swm_iconic(win
, 0);
5099 search_resp_name_workspace(const char *resp
, size_t len
)
5101 struct workspace
*ws
;
5103 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: resp: %s\n", resp
);
5105 if (search_r
->ws
== NULL
)
5110 free(search_r
->ws
->name
);
5111 search_r
->ws
->name
= NULL
;
5115 ws
->name
= strdup(resp
);
5116 if (ws
->name
== NULL
) {
5117 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: "
5118 "strdup: %s", strerror(errno
));
5125 search_resp_search_workspace(const char *resp
)
5132 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: resp: %s\n", resp
);
5136 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: strdup: %s",
5143 ws_idx
= (int)strtonum(q
, 1, workspace_limit
, &errstr
);
5145 DNPRINTF(SWM_D_MISC
, "workspace idx is %s: %s",
5152 switchws(search_r
, &a
);
5156 search_resp_search_window(const char *resp
)
5161 struct search_window
*sw
;
5163 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: resp: %s\n", resp
);
5167 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: strdup: %s",
5172 idx
= (int)strtonum(s
, 1, INT_MAX
, &errstr
);
5174 DNPRINTF(SWM_D_MISC
, "window idx is %s: %s",
5181 TAILQ_FOREACH(sw
, &search_wl
, entry
)
5182 if (idx
== sw
->idx
) {
5188 #define MAX_RESP_LEN 1024
5191 search_do_resp(void)
5197 DNPRINTF(SWM_D_MISC
, "search_do_resp:\n");
5202 if ((resp
= calloc(1, MAX_RESP_LEN
+ 1)) == NULL
) {
5203 warn("search: calloc");
5207 rbytes
= read(select_resp_pipe
[0], resp
, MAX_RESP_LEN
);
5209 warn("search: read error");
5212 resp
[rbytes
] = '\0';
5215 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
5216 * newline, so work around that by sanitizing the resp now.
5218 resp
[strcspn(resp
, "\n")] = '\0';
5221 switch (search_resp_action
) {
5222 case SWM_SEARCH_UNICONIFY
:
5223 search_resp_uniconify(resp
, len
);
5225 case SWM_SEARCH_NAME_WORKSPACE
:
5226 search_resp_name_workspace(resp
, len
);
5228 case SWM_SEARCH_SEARCH_WORKSPACE
:
5229 search_resp_search_workspace(resp
);
5231 case SWM_SEARCH_SEARCH_WINDOW
:
5232 search_resp_search_window(resp
);
5237 if (search_resp_action
== SWM_SEARCH_SEARCH_WINDOW
)
5238 search_win_cleanup();
5240 search_resp_action
= SWM_SEARCH_NONE
;
5241 close(select_resp_pipe
[0]);
5248 wkill(struct swm_region
*r
, union arg
*args
)
5250 DNPRINTF(SWM_D_MISC
, "wkill: id: %d\n", args
->id
);
5252 if (r
->ws
->focus
== NULL
)
5255 if (args
->id
== SWM_ARG_ID_KILLWINDOW
)
5256 xcb_kill_client(conn
, r
->ws
->focus
->id
);
5258 if (r
->ws
->focus
->can_delete
)
5259 client_msg(r
->ws
->focus
, a_delete
, 0);
5265 floating_toggle_win(struct ws_win
*win
)
5267 struct swm_region
*r
;
5277 /* reject floating toggles in max stack mode */
5278 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
5281 if (win
->floating
) {
5282 if (!win
->floatmaxed
) {
5283 /* retain position for refloat */
5284 store_float_geom(win
);
5288 load_float_geom(win
);
5292 ewmh_update_actions(win
);
5298 floating_toggle(struct swm_region
*r
, union arg
*args
)
5300 struct ws_win
*win
= r
->ws
->focus
;
5302 /* suppress unused warning since var is needed */
5308 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
5311 ewmh_update_win_state(win
, ewmh
[_NET_WM_STATE_ABOVE
].atom
,
5312 _NET_WM_STATE_TOGGLE
);
5316 if (win
== win
->ws
->focus
)
5323 region_containment(struct ws_win
*win
, struct swm_region
*r
, int opts
)
5325 struct swm_geometry g
= r
->g
;
5326 int rt
, lt
, tp
, bm
, bw
;
5328 bw
= (opts
& SWM_CW_SOFTBOUNDARY
) ? boundary_width
: 0;
5331 * Perpendicular distance of each side of the window to the respective
5332 * side of the region boundary. Positive values indicate the side of
5333 * the window has passed beyond the region boundary.
5335 rt
= opts
& SWM_CW_RIGHT
? MAX_X(win
) + BORDER(win
) - MAX_X(r
) : bw
;
5336 lt
= opts
& SWM_CW_LEFT
? X(r
) - X(win
) + BORDER(win
) : bw
;
5337 bm
= opts
& SWM_CW_BOTTOM
? MAX_Y(win
) + BORDER(win
) - MAX_Y(r
) : bw
;
5338 tp
= opts
& SWM_CW_TOP
? Y(r
) - Y(win
) + BORDER(win
) : bw
;
5340 DNPRINTF(SWM_D_MISC
, "region_containment: win 0x%x, rt: %d, lt: %d, "
5341 "bm: %d, tp: %d, SOFTBOUNDARY: %s, HARDBOUNDARY: %s\n", win
->id
, rt
,
5342 lt
, bm
, tp
, YESNO(opts
& SWM_CW_SOFTBOUNDARY
),
5343 YESNO(opts
& SWM_CW_HARDBOUNDARY
));
5346 * Disable containment if any of the flagged sides went beyond the
5347 * containment boundary, or if containment is disabled.
5349 if (!(opts
& SWM_CW_HARDBOUNDARY
|| opts
& SWM_CW_SOFTBOUNDARY
) ||
5350 (bw
!= 0 && ((rt
> bw
) || (lt
> bw
) || (bm
> bw
) || (tp
> bw
)))) {
5351 /* Make sure window has at least 1 pixel in the region */
5352 g
.x
+= 1 - WIDTH(win
);
5353 g
.y
+= 1 - HEIGHT(win
);
5354 g
.w
+= 2 * WIDTH(win
) - 2;
5355 g
.h
+= 2 * HEIGHT(win
) - 2;
5358 constrain_window(win
, &g
, &opts
);
5361 /* Move or resize a window so that flagged side(s) fit into the supplied box. */
5363 constrain_window(struct ws_win
*win
, struct swm_geometry
*b
, int *opts
)
5365 DNPRINTF(SWM_D_MISC
, "constrain_window: window: 0x%x, (x,y) w x h: "
5366 "(%d,%d) %d x %d, box: (x,y) w x h: (%d,%d) %d x %d, rt: %s, "
5367 "lt: %s, bt: %s, tp: %s, allow resize: %s\n", win
->id
, X(win
),
5368 Y(win
), WIDTH(win
), HEIGHT(win
), b
->x
, b
->y
, b
->w
, b
->h
,
5369 YESNO(*opts
& SWM_CW_RIGHT
), YESNO(*opts
& SWM_CW_LEFT
),
5370 YESNO(*opts
& SWM_CW_BOTTOM
), YESNO(*opts
& SWM_CW_TOP
),
5371 YESNO(*opts
& SWM_CW_RESIZABLE
));
5373 if ((*opts
& SWM_CW_RIGHT
) && MAX_X(win
) + BORDER(win
) > b
->x
+ b
->w
) {
5374 if (*opts
& SWM_CW_RESIZABLE
)
5375 WIDTH(win
) = b
->x
+ b
->w
- X(win
) - BORDER(win
);
5377 X(win
) = b
->x
+ b
->w
- WIDTH(win
) - BORDER(win
);
5380 if ((*opts
& SWM_CW_LEFT
) && X(win
) + BORDER(win
) < b
->x
) {
5381 if (*opts
& SWM_CW_RESIZABLE
)
5382 WIDTH(win
) -= b
->x
- X(win
) - BORDER(win
);
5384 X(win
) = b
->x
- BORDER(win
);
5387 if ((*opts
& SWM_CW_BOTTOM
) && MAX_Y(win
) + BORDER(win
) > b
->y
+ b
->h
) {
5388 if (*opts
& SWM_CW_RESIZABLE
)
5389 HEIGHT(win
) = b
->y
+ b
->h
- Y(win
) - BORDER(win
);
5391 Y(win
) = b
->y
+ b
->h
- HEIGHT(win
) - BORDER(win
);
5394 if ((*opts
& SWM_CW_TOP
) && Y(win
) + BORDER(win
) < b
->y
) {
5395 if (*opts
& SWM_CW_RESIZABLE
)
5396 HEIGHT(win
) -= b
->y
- Y(win
) - BORDER(win
);
5398 Y(win
) = b
->y
- BORDER(win
);
5401 if (*opts
& SWM_CW_RESIZABLE
) {
5404 if (HEIGHT(win
) < 1)
5410 update_window(struct ws_win
*win
)
5415 mask
= XCB_CONFIG_WINDOW_X
| XCB_CONFIG_WINDOW_Y
|
5416 XCB_CONFIG_WINDOW_WIDTH
| XCB_CONFIG_WINDOW_HEIGHT
|
5417 XCB_CONFIG_WINDOW_BORDER_WIDTH
;
5421 wc
[3] = HEIGHT(win
);
5422 wc
[4] = BORDER(win
);
5424 DNPRINTF(SWM_D_EVENT
, "update_window: window: 0x%x, (x,y) w x h: "
5425 "(%d,%d) %d x %d, bordered: %s\n", win
->id
, wc
[0], wc
[1], wc
[2],
5426 wc
[3], YESNO(win
->bordered
));
5428 xcb_configure_window(conn
, win
->id
, mask
, wc
);
5431 #define SWM_RESIZE_STEPS (50)
5434 resize(struct ws_win
*win
, union arg
*args
)
5436 xcb_timestamp_t timestamp
= 0;
5437 struct swm_region
*r
= NULL
;
5439 struct swm_geometry g
;
5440 int top
= 0, left
= 0, resizing
;
5442 xcb_cursor_t cursor
;
5443 xcb_query_pointer_reply_t
*xpr
= NULL
;
5444 xcb_generic_event_t
*evt
;
5445 xcb_motion_notify_event_t
*mne
;
5451 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
5454 DNPRINTF(SWM_D_EVENT
, "resize: window: 0x%x, floating: %s, "
5455 "transient: 0x%x\n", win
->id
, YESNO(win
->floating
),
5458 if (!win
->transient
&& !win
->floating
)
5461 /* reject resizes in max mode for floaters (transient ok) */
5462 if (win
->floatmaxed
)
5466 ewmh_update_win_state(win
, ewmh
[_SWM_WM_STATE_MANUAL
].atom
,
5473 /* It's possible for win to have been freed during focus_flush(). */
5474 if (validate_win(win
)) {
5475 DNPRINTF(SWM_D_EVENT
, "resize: invalid win.\n");
5480 case SWM_ARG_ID_WIDTHSHRINK
:
5481 WIDTH(win
) -= SWM_RESIZE_STEPS
;
5484 case SWM_ARG_ID_WIDTHGROW
:
5485 WIDTH(win
) += SWM_RESIZE_STEPS
;
5488 case SWM_ARG_ID_HEIGHTSHRINK
:
5489 HEIGHT(win
) -= SWM_RESIZE_STEPS
;
5492 case SWM_ARG_ID_HEIGHTGROW
:
5493 HEIGHT(win
) += SWM_RESIZE_STEPS
;
5500 region_containment(win
, r
, SWM_CW_ALLSIDES
| SWM_CW_RESIZABLE
|
5501 SWM_CW_HARDBOUNDARY
);
5503 store_float_geom(win
);
5507 region_containment(win
, r
, SWM_CW_ALLSIDES
| SWM_CW_RESIZABLE
|
5508 SWM_CW_SOFTBOUNDARY
);
5511 /* get cursor offset from window root */
5512 xpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
5519 if (xpr
->win_x
< WIDTH(win
) / 2)
5522 if (xpr
->win_y
< HEIGHT(win
) / 2)
5525 if (args
->id
== SWM_ARG_ID_CENTER
)
5526 cursor
= cursors
[XC_SIZING
].cid
;
5528 cursor
= cursors
[left
? XC_TOP_LEFT_CORNER
:
5529 XC_TOP_RIGHT_CORNER
].cid
;
5531 cursor
= cursors
[left
? XC_BOTTOM_LEFT_CORNER
:
5532 XC_BOTTOM_RIGHT_CORNER
].cid
;
5534 xcb_grab_pointer(conn
, 0, win
->id
, MOUSEMASK
,
5535 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
, XCB_WINDOW_NONE
, cursor
,
5540 while (resizing
&& (evt
= xcb_wait_for_event(conn
))) {
5541 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
5542 case XCB_BUTTON_RELEASE
:
5543 DNPRINTF(SWM_D_EVENT
, "resize: BUTTON_RELEASE\n");
5546 case XCB_MOTION_NOTIFY
:
5547 mne
= (xcb_motion_notify_event_t
*)evt
;
5548 /* cursor offset/delta from start of the operation */
5549 dx
= mne
->root_x
- xpr
->root_x
;
5550 dy
= mne
->root_y
- xpr
->root_y
;
5556 if (args
->id
== SWM_ARG_ID_CENTER
) {
5557 if (g
.h
/ 2 + dy
< 1)
5561 HEIGHT(win
) = g
.h
+ 2 * dy
;
5569 HEIGHT(win
) = g
.h
+ dy
;
5576 if (args
->id
== SWM_ARG_ID_CENTER
) {
5577 if (g
.w
/ 2 + dx
< 1)
5581 WIDTH(win
) = g
.w
+ 2 * dx
;
5589 WIDTH(win
) = g
.w
+ dx
;
5592 /* not free, don't sync more than 120 times / second */
5593 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
5594 timestamp
= mne
->time
;
5595 regionize(win
, mne
->root_x
, mne
->root_y
);
5596 region_containment(win
, r
, SWM_CW_ALLSIDES
|
5597 SWM_CW_RESIZABLE
| SWM_CW_HARDBOUNDARY
|
5598 SWM_CW_SOFTBOUNDARY
);
5606 /* It's possible for win to have been freed above. */
5607 if (validate_win(win
)) {
5608 DNPRINTF(SWM_D_EVENT
, "resize: invalid win.\n");
5616 region_containment(win
, r
, SWM_CW_ALLSIDES
| SWM_CW_RESIZABLE
|
5617 SWM_CW_HARDBOUNDARY
| SWM_CW_SOFTBOUNDARY
);
5621 store_float_geom(win
);
5623 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
5625 DNPRINTF(SWM_D_EVENT
, "resize: done.\n");
5629 resize_step(struct swm_region
*r
, union arg
*args
)
5631 struct ws_win
*win
= NULL
;
5633 if (r
&& r
->ws
&& r
->ws
->focus
)
5642 /* Try to set window region based on supplied coordinates or window center. */
5644 regionize(struct ws_win
*win
, int x
, int y
)
5646 struct swm_region
*r
= NULL
;
5648 r
= region_under(win
->s
, x
, y
);
5650 r
= region_under(win
->s
, X(win
) + WIDTH(win
) / 2,
5651 Y(win
) + HEIGHT(win
) / 2);
5653 if (r
&& r
!= win
->ws
->r
) {
5654 win_to_ws(win
, r
->ws
->idx
, 0);
5659 #define SWM_MOVE_STEPS (50)
5662 move(struct ws_win
*win
, union arg
*args
)
5664 xcb_timestamp_t timestamp
= 0;
5665 int move_stp
= 0, moving
;
5666 struct swm_region
*r
= NULL
;
5667 xcb_query_pointer_reply_t
*qpr
= NULL
;
5668 xcb_generic_event_t
*evt
;
5669 xcb_motion_notify_event_t
*mne
;
5675 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
5678 DNPRINTF(SWM_D_EVENT
, "move: window: 0x%x, floating: %s, transient: "
5679 "0x%x\n", win
->id
, YESNO(win
->floating
), win
->transient
);
5681 /* in max_stack mode should only move transients */
5682 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
] && !win
->transient
)
5687 ewmh_update_win_state(win
, ewmh
[_SWM_WM_STATE_MANUAL
].atom
,
5691 /* When a stacked win is moved, float it and restack. */
5692 if (!win
->floating
&& !win
->transient
) {
5693 store_float_geom(win
);
5694 ewmh_update_win_state(win
, ewmh
[_NET_WM_STATE_ABOVE
].atom
,
5701 /* It's possible for win to have been freed during focus_flush(). */
5702 if (validate_win(win
)) {
5703 DNPRINTF(SWM_D_EVENT
, "move: invalid win.\n");
5709 case SWM_ARG_ID_MOVELEFT
:
5710 X(win
) -= (SWM_MOVE_STEPS
- border_width
);
5713 case SWM_ARG_ID_MOVERIGHT
:
5714 X(win
) += (SWM_MOVE_STEPS
- border_width
);
5717 case SWM_ARG_ID_MOVEUP
:
5718 Y(win
) -= (SWM_MOVE_STEPS
- border_width
);
5721 case SWM_ARG_ID_MOVEDOWN
:
5722 Y(win
) += (SWM_MOVE_STEPS
- border_width
);
5729 regionize(win
, -1, -1);
5730 region_containment(win
, win
->ws
->r
, SWM_CW_ALLSIDES
);
5732 store_float_geom(win
);
5736 xcb_grab_pointer(conn
, 0, win
->id
, MOUSEMASK
,
5737 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
,
5738 XCB_WINDOW_NONE
, cursors
[XC_FLEUR
].cid
, XCB_CURRENT_TIME
);
5740 /* get cursor offset from window root */
5741 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
5744 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
5748 regionize(win
, qpr
->root_x
, qpr
->root_y
);
5749 region_containment(win
, win
->ws
->r
, SWM_CW_ALLSIDES
|
5750 SWM_CW_SOFTBOUNDARY
);
5754 while (moving
&& (evt
= xcb_wait_for_event(conn
))) {
5755 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
5756 case XCB_BUTTON_RELEASE
:
5757 DNPRINTF(SWM_D_EVENT
, "move: BUTTON_RELEASE\n");
5760 case XCB_MOTION_NOTIFY
:
5761 mne
= (xcb_motion_notify_event_t
*)evt
;
5762 DNPRINTF(SWM_D_EVENT
, "motion: root: 0x%x\n", mne
->root
);
5763 X(win
) = mne
->root_x
- qpr
->win_x
- border_width
;
5764 Y(win
) = mne
->root_y
- qpr
->win_y
- border_width
;
5766 /* not free, don't sync more than 120 times / second */
5767 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
5768 timestamp
= mne
->time
;
5769 regionize(win
, mne
->root_x
, mne
->root_y
);
5770 region_containment(win
, win
->ws
->r
,
5771 SWM_CW_ALLSIDES
| SWM_CW_SOFTBOUNDARY
);
5779 /* It's possible for win to have been freed above. */
5780 if (validate_win(win
)) {
5781 DNPRINTF(SWM_D_EVENT
, "move: invalid win.\n");
5789 region_containment(win
, win
->ws
->r
, SWM_CW_ALLSIDES
|
5790 SWM_CW_SOFTBOUNDARY
);
5794 store_float_geom(win
);
5797 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
5798 DNPRINTF(SWM_D_EVENT
, "move: done.\n");
5802 move_step(struct swm_region
*r
, union arg
*args
)
5804 struct ws_win
*win
= NULL
;
5806 if (r
&& r
->ws
&& r
->ws
->focus
)
5811 if (!win
->transient
&& !win
->floating
)
5818 /* key definitions */
5820 char name
[SWM_FUNCNAME_LEN
];
5821 void (*func
)(struct swm_region
*r
, union arg
*);
5823 } keyfuncs
[KF_INVALID
+ 1] = {
5824 /* name function argument */
5825 { "bar_toggle", bar_toggle
, {.id
= SWM_ARG_ID_BAR_TOGGLE
} },
5826 { "bar_toggle_ws", bar_toggle
, {.id
= SWM_ARG_ID_BAR_TOGGLE_WS
} },
5827 { "button2", pressbutton
, {2} },
5828 { "cycle_layout", cycle_layout
, {0} },
5829 { "flip_layout", stack_config
, {.id
= SWM_ARG_ID_FLIPLAYOUT
} },
5830 { "float_toggle", floating_toggle
,{0} },
5831 { "focus_main", focus
, {.id
= SWM_ARG_ID_FOCUSMAIN
} },
5832 { "focus_next", focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
5833 { "focus_prev", focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
5834 { "height_grow", resize_step
, {.id
= SWM_ARG_ID_HEIGHTGROW
} },
5835 { "height_shrink", resize_step
, {.id
= SWM_ARG_ID_HEIGHTSHRINK
} },
5836 { "iconify", iconify
, {0} },
5837 { "master_shrink", stack_config
, {.id
= SWM_ARG_ID_MASTERSHRINK
} },
5838 { "master_grow", stack_config
, {.id
= SWM_ARG_ID_MASTERGROW
} },
5839 { "master_add", stack_config
, {.id
= SWM_ARG_ID_MASTERADD
} },
5840 { "master_del", stack_config
, {.id
= SWM_ARG_ID_MASTERDEL
} },
5841 { "move_down", move_step
, {.id
= SWM_ARG_ID_MOVEDOWN
} },
5842 { "move_left", move_step
, {.id
= SWM_ARG_ID_MOVELEFT
} },
5843 { "move_right", move_step
, {.id
= SWM_ARG_ID_MOVERIGHT
} },
5844 { "move_up", move_step
, {.id
= SWM_ARG_ID_MOVEUP
} },
5845 { "mvrg_1", send_to_rg
, {.id
= 0} },
5846 { "mvrg_2", send_to_rg
, {.id
= 1} },
5847 { "mvrg_3", send_to_rg
, {.id
= 2} },
5848 { "mvrg_4", send_to_rg
, {.id
= 3} },
5849 { "mvrg_5", send_to_rg
, {.id
= 4} },
5850 { "mvrg_6", send_to_rg
, {.id
= 5} },
5851 { "mvrg_7", send_to_rg
, {.id
= 6} },
5852 { "mvrg_8", send_to_rg
, {.id
= 7} },
5853 { "mvrg_9", send_to_rg
, {.id
= 8} },
5854 { "mvws_1", send_to_ws
, {.id
= 0} },
5855 { "mvws_2", send_to_ws
, {.id
= 1} },
5856 { "mvws_3", send_to_ws
, {.id
= 2} },
5857 { "mvws_4", send_to_ws
, {.id
= 3} },
5858 { "mvws_5", send_to_ws
, {.id
= 4} },
5859 { "mvws_6", send_to_ws
, {.id
= 5} },
5860 { "mvws_7", send_to_ws
, {.id
= 6} },
5861 { "mvws_8", send_to_ws
, {.id
= 7} },
5862 { "mvws_9", send_to_ws
, {.id
= 8} },
5863 { "mvws_10", send_to_ws
, {.id
= 9} },
5864 { "mvws_11", send_to_ws
, {.id
= 10} },
5865 { "mvws_12", send_to_ws
, {.id
= 11} },
5866 { "mvws_13", send_to_ws
, {.id
= 12} },
5867 { "mvws_14", send_to_ws
, {.id
= 13} },
5868 { "mvws_15", send_to_ws
, {.id
= 14} },
5869 { "mvws_16", send_to_ws
, {.id
= 15} },
5870 { "mvws_17", send_to_ws
, {.id
= 16} },
5871 { "mvws_18", send_to_ws
, {.id
= 17} },
5872 { "mvws_19", send_to_ws
, {.id
= 18} },
5873 { "mvws_20", send_to_ws
, {.id
= 19} },
5874 { "mvws_21", send_to_ws
, {.id
= 20} },
5875 { "mvws_22", send_to_ws
, {.id
= 21} },
5876 { "name_workspace", name_workspace
, {0} },
5877 { "quit", quit
, {0} },
5878 { "raise_toggle", raise_toggle
, {0} },
5879 { "restart", restart
, {0} },
5880 { "rg_1", focusrg
, {.id
= 0} },
5881 { "rg_2", focusrg
, {.id
= 1} },
5882 { "rg_3", focusrg
, {.id
= 2} },
5883 { "rg_4", focusrg
, {.id
= 3} },
5884 { "rg_5", focusrg
, {.id
= 4} },
5885 { "rg_6", focusrg
, {.id
= 5} },
5886 { "rg_7", focusrg
, {.id
= 6} },
5887 { "rg_8", focusrg
, {.id
= 7} },
5888 { "rg_9", focusrg
, {.id
= 8} },
5889 { "rg_next", cyclerg
, {.id
= SWM_ARG_ID_CYCLERG_UP
} },
5890 { "rg_prev", cyclerg
, {.id
= SWM_ARG_ID_CYCLERG_DOWN
} },
5891 { "screen_next", cyclerg
, {.id
= SWM_ARG_ID_CYCLERG_UP
} },
5892 { "screen_prev", cyclerg
, {.id
= SWM_ARG_ID_CYCLERG_DOWN
} },
5893 { "search_win", search_win
, {0} },
5894 { "search_workspace", search_workspace
, {0} },
5895 { "spawn_custom", NULL
, {0} },
5896 { "stack_inc", stack_config
, {.id
= SWM_ARG_ID_STACKINC
} },
5897 { "stack_dec", stack_config
, {.id
= SWM_ARG_ID_STACKDEC
} },
5898 { "stack_reset", stack_config
, {.id
= SWM_ARG_ID_STACKRESET
} },
5899 { "swap_main", swapwin
, {.id
= SWM_ARG_ID_SWAPMAIN
} },
5900 { "swap_next", swapwin
, {.id
= SWM_ARG_ID_SWAPNEXT
} },
5901 { "swap_prev", swapwin
, {.id
= SWM_ARG_ID_SWAPPREV
} },
5902 { "uniconify", uniconify
, {0} },
5903 { "version", version
, {0} },
5904 { "width_grow", resize_step
, {.id
= SWM_ARG_ID_WIDTHGROW
} },
5905 { "width_shrink", resize_step
, {.id
= SWM_ARG_ID_WIDTHSHRINK
} },
5906 { "wind_del", wkill
, {.id
= SWM_ARG_ID_DELETEWINDOW
} },
5907 { "wind_kill", wkill
, {.id
= SWM_ARG_ID_KILLWINDOW
} },
5908 { "ws_1", switchws
, {.id
= 0} },
5909 { "ws_2", switchws
, {.id
= 1} },
5910 { "ws_3", switchws
, {.id
= 2} },
5911 { "ws_4", switchws
, {.id
= 3} },
5912 { "ws_5", switchws
, {.id
= 4} },
5913 { "ws_6", switchws
, {.id
= 5} },
5914 { "ws_7", switchws
, {.id
= 6} },
5915 { "ws_8", switchws
, {.id
= 7} },
5916 { "ws_9", switchws
, {.id
= 8} },
5917 { "ws_10", switchws
, {.id
= 9} },
5918 { "ws_11", switchws
, {.id
= 10} },
5919 { "ws_12", switchws
, {.id
= 11} },
5920 { "ws_13", switchws
, {.id
= 12} },
5921 { "ws_14", switchws
, {.id
= 13} },
5922 { "ws_15", switchws
, {.id
= 14} },
5923 { "ws_16", switchws
, {.id
= 15} },
5924 { "ws_17", switchws
, {.id
= 16} },
5925 { "ws_18", switchws
, {.id
= 17} },
5926 { "ws_19", switchws
, {.id
= 18} },
5927 { "ws_20", switchws
, {.id
= 19} },
5928 { "ws_21", switchws
, {.id
= 20} },
5929 { "ws_22", switchws
, {.id
= 21} },
5930 { "ws_next", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP
} },
5931 { "ws_next_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP_ALL
} },
5932 { "ws_next_move", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_MOVE_UP
} },
5933 { "ws_prev", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN
} },
5934 { "ws_prev_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN_ALL
} },
5935 { "ws_prev_move", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_MOVE_DOWN
} },
5936 { "ws_prior", priorws
, {0} },
5937 { "dumpwins", dumpwins
, {0} }, /* MUST BE LAST */
5938 { "invalid key func", NULL
, {0} },
5942 key_cmp(struct key
*kp1
, struct key
*kp2
)
5944 if (kp1
->keysym
< kp2
->keysym
)
5946 if (kp1
->keysym
> kp2
->keysym
)
5949 if (kp1
->mod
< kp2
->mod
)
5951 if (kp1
->mod
> kp2
->mod
)
5958 enum { client_click
, root_click
};
5960 unsigned int action
;
5962 unsigned int button
;
5963 void (*func
)(struct ws_win
*, union arg
*);
5966 #define MODKEY_SHIFT MODKEY | XCB_MOD_MASK_SHIFT
5967 /* action key mouse button func args */
5968 { client_click
, MODKEY
, XCB_BUTTON_INDEX_3
, resize
, {.id
= SWM_ARG_ID_DONTCENTER
} },
5969 { client_click
, MODKEY_SHIFT
, XCB_BUTTON_INDEX_3
, resize
, {.id
= SWM_ARG_ID_CENTER
} },
5970 { client_click
, MODKEY
, XCB_BUTTON_INDEX_1
, move
, {0} },
5975 update_modkey(unsigned int mod
)
5981 RB_FOREACH(kp
, key_tree
, &keys
)
5982 if (kp
->mod
& XCB_MOD_MASK_SHIFT
)
5983 kp
->mod
= mod
| XCB_MOD_MASK_SHIFT
;
5987 for (i
= 0; i
< LENGTH(buttons
); i
++)
5988 if (buttons
[i
].mask
& XCB_MOD_MASK_SHIFT
)
5989 buttons
[i
].mask
= mod
| XCB_MOD_MASK_SHIFT
;
5991 buttons
[i
].mask
= mod
;
5995 spawn_expand(struct swm_region
*r
, union arg
*args
, const char *spawn_name
,
5998 struct spawn_prog
*prog
= NULL
;
6000 char *ap
, **real_args
;
6002 /* suppress unused warning since var is needed */
6005 DNPRINTF(SWM_D_SPAWN
, "spawn_expand: %s\n", spawn_name
);
6008 TAILQ_FOREACH(prog
, &spawns
, entry
) {
6009 if (!strcasecmp(spawn_name
, prog
->name
))
6013 warnx("spawn_custom: program %s not found", spawn_name
);
6017 /* make room for expanded args */
6018 if ((real_args
= calloc(prog
->argc
+ 1, sizeof(char *))) == NULL
)
6019 err(1, "spawn_custom: calloc real_args");
6021 /* expand spawn_args into real_args */
6022 for (i
= 0; i
< prog
->argc
; i
++) {
6024 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: raw arg: %s\n", ap
);
6025 if (!strcasecmp(ap
, "$bar_border")) {
6027 strdup(r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].name
))
6029 err(1, "spawn_custom border color");
6030 } else if (!strcasecmp(ap
, "$bar_color")) {
6032 strdup(r
->s
->c
[SWM_S_COLOR_BAR
].name
))
6034 err(1, "spawn_custom bar color");
6035 } else if (!strcasecmp(ap
, "$bar_font")) {
6036 if ((real_args
[i
] = strdup(bar_fonts
))
6038 err(1, "spawn_custom bar fonts");
6039 } else if (!strcasecmp(ap
, "$bar_font_color")) {
6041 strdup(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].name
))
6043 err(1, "spawn_custom color font");
6044 } else if (!strcasecmp(ap
, "$color_focus")) {
6046 strdup(r
->s
->c
[SWM_S_COLOR_FOCUS
].name
))
6048 err(1, "spawn_custom color focus");
6049 } else if (!strcasecmp(ap
, "$color_unfocus")) {
6051 strdup(r
->s
->c
[SWM_S_COLOR_UNFOCUS
].name
))
6053 err(1, "spawn_custom color unfocus");
6054 } else if (!strcasecmp(ap
, "$region_index")) {
6055 if (asprintf(&real_args
[i
], "%d",
6056 get_region_index(r
) + 1) < 1)
6057 err(1, "spawn_custom region index");
6058 } else if (!strcasecmp(ap
, "$workspace_index")) {
6059 if (asprintf(&real_args
[i
], "%d", r
->ws
->idx
+ 1) < 1)
6060 err(1, "spawn_custom workspace index");
6062 /* no match --> copy as is */
6063 if ((real_args
[i
] = strdup(ap
)) == NULL
)
6064 err(1, "spawn_custom strdup(ap)");
6066 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: cooked arg: %s\n",
6071 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: result: ");
6072 for (i
= 0; i
< prog
->argc
; i
++)
6073 DNPRINTF(SWM_D_SPAWN
, "\"%s\" ", real_args
[i
]);
6074 DNPRINTF(SWM_D_SPAWN
, "\n");
6076 *ret_args
= real_args
;
6077 return (prog
->argc
);
6081 spawn_custom(struct swm_region
*r
, union arg
*args
, const char *spawn_name
)
6087 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
6091 spawn(r
->ws
->idx
, &a
, 1);
6093 for (i
= 0; i
< spawn_argc
; i
++)
6099 spawn_select(struct swm_region
*r
, union arg
*args
, const char *spawn_name
,
6106 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
6110 if (pipe(select_list_pipe
) == -1)
6111 err(1, "pipe error");
6112 if (pipe(select_resp_pipe
) == -1)
6113 err(1, "pipe error");
6115 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
6116 err(1, "could not disable SIGPIPE");
6117 switch (*pid
= fork()) {
6119 err(1, "cannot fork");
6122 if (dup2(select_list_pipe
[0], STDIN_FILENO
) == -1)
6124 if (dup2(select_resp_pipe
[1], STDOUT_FILENO
) == -1)
6126 close(select_list_pipe
[1]);
6127 close(select_resp_pipe
[0]);
6128 spawn(r
->ws
->idx
, &a
, 0);
6130 default: /* parent */
6131 close(select_list_pipe
[0]);
6132 close(select_resp_pipe
[1]);
6136 for (i
= 0; i
< spawn_argc
; i
++)
6142 spawn_insert(const char *name
, const char *args
, int flags
)
6144 char *arg
, *cp
, *ptr
;
6145 struct spawn_prog
*sp
;
6147 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: %s\n", name
);
6149 if ((sp
= calloc(1, sizeof *sp
)) == NULL
)
6150 err(1, "spawn_insert: calloc");
6151 if ((sp
->name
= strdup(name
)) == NULL
)
6152 err(1, "spawn_insert: strdup");
6154 /* convert the arguments to an argument list */
6155 if ((ptr
= cp
= strdup(args
)) == NULL
)
6156 err(1, "spawn_insert: strdup");
6157 while ((arg
= strsep(&ptr
, " \t")) != NULL
) {
6158 /* empty field; skip it */
6163 if ((sp
->argv
= realloc(sp
->argv
, sp
->argc
*
6164 sizeof *sp
->argv
)) == NULL
)
6165 err(1, "spawn_insert: realloc");
6166 if ((sp
->argv
[sp
->argc
- 1] = strdup(arg
)) == NULL
)
6167 err(1, "spawn_insert: strdup");
6173 TAILQ_INSERT_TAIL(&spawns
, sp
, entry
);
6174 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: leave\n");
6178 spawn_remove(struct spawn_prog
*sp
)
6182 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: %s\n", sp
->name
);
6184 TAILQ_REMOVE(&spawns
, sp
, entry
);
6185 for (i
= 0; i
< sp
->argc
; i
++)
6191 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: leave\n");
6195 setspawn(const char *name
, const char *args
, int flags
)
6197 struct spawn_prog
*sp
;
6199 DNPRINTF(SWM_D_SPAWN
, "setspawn: %s\n", name
);
6204 /* Remove any old spawn under the same name. */
6205 TAILQ_FOREACH(sp
, &spawns
, entry
)
6206 if (!strcmp(sp
->name
, name
)) {
6212 spawn_insert(name
, args
, flags
);
6214 warnx("error: setspawn: cannot find program: %s", name
);
6216 DNPRINTF(SWM_D_SPAWN
, "setspawn: leave\n");
6220 setconfspawn(char *selector
, char *value
, int flags
)
6224 args
= expand_tilde(value
);
6226 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: [%s] [%s]\n", selector
, args
);
6228 setspawn(selector
, args
, flags
);
6231 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: done.\n");
6236 validate_spawns(void)
6238 struct spawn_prog
*sp
;
6239 char which
[PATH_MAX
];
6244 RB_FOREACH(kp
, key_tree
, &keys
) {
6245 if (kp
->funcid
!= KF_SPAWN_CUSTOM
)
6249 TAILQ_FOREACH(sp
, &spawns
, entry
) {
6250 if (!strcasecmp(kp
->spawn_name
, sp
->name
))
6254 if (sp
== NULL
|| sp
->flags
& SWM_SPAWN_OPTIONAL
)
6257 /* verify we have the goods */
6258 snprintf(which
, sizeof which
, "which %s", sp
->argv
[0]);
6259 DNPRINTF(SWM_D_CONF
, "validate_spawns: which %s\n",
6261 for (i
= strlen("which "); i
< strlen(which
); i
++)
6262 if (which
[i
] == ' ') {
6266 if (system(which
) != 0)
6267 add_startup_exception("could not find %s",
6268 &which
[strlen("which ")]);
6275 setconfspawn("lock", "xlock", 0);
6277 setconfspawn("term", "xterm", 0);
6278 setconfspawn("spawn_term", "xterm", 0);
6280 setconfspawn("menu", "dmenu_run"
6283 " -nf $bar_font_color"
6285 " -sf $bar_color", 0);
6287 setconfspawn("search", "dmenu"
6291 " -nf $bar_font_color"
6293 " -sf $bar_color", 0);
6295 setconfspawn("name_workspace", "dmenu"
6299 " -nf $bar_font_color"
6301 " -sf $bar_color", 0);
6303 /* These are not verified for existence, even with a binding set. */
6304 setconfspawn("screenshot_all", "screenshot.sh full", SWM_SPAWN_OPTIONAL
);
6305 setconfspawn("screenshot_wind", "screenshot.sh window", SWM_SPAWN_OPTIONAL
);
6306 setconfspawn("initscr", "initscreen.sh", SWM_SPAWN_OPTIONAL
);
6310 #define SWM_MODNAME_SIZE 32
6311 #define SWM_KEY_WS "\n+ \t"
6313 parsekeys(char *keystr
, unsigned int currmod
, unsigned int *mod
, KeySym
*ks
)
6317 DNPRINTF(SWM_D_KEY
, "parsekeys: enter [%s]\n", keystr
);
6318 if (mod
== NULL
|| ks
== NULL
) {
6319 DNPRINTF(SWM_D_KEY
, "parsekeys: no mod or key vars\n");
6322 if (keystr
== NULL
|| strlen(keystr
) == 0) {
6323 DNPRINTF(SWM_D_KEY
, "parsekeys: no keystr\n");
6327 *ks
= XCB_NO_SYMBOL
;
6329 while ((name
= strsep(&cp
, SWM_KEY_WS
)) != NULL
) {
6330 DNPRINTF(SWM_D_KEY
, "parsekeys: key [%s]\n", name
);
6332 cp
+= (long)strspn(cp
, SWM_KEY_WS
);
6333 if (strncasecmp(name
, "MOD", SWM_MODNAME_SIZE
) == 0)
6335 else if (!strncasecmp(name
, "Mod1", SWM_MODNAME_SIZE
))
6336 *mod
|= XCB_MOD_MASK_1
;
6337 else if (!strncasecmp(name
, "Mod2", SWM_MODNAME_SIZE
))
6338 *mod
+= XCB_MOD_MASK_2
;
6339 else if (!strncmp(name
, "Mod3", SWM_MODNAME_SIZE
))
6340 *mod
|= XCB_MOD_MASK_3
;
6341 else if (!strncmp(name
, "Mod4", SWM_MODNAME_SIZE
))
6342 *mod
|= XCB_MOD_MASK_4
;
6343 else if (strncasecmp(name
, "SHIFT", SWM_MODNAME_SIZE
) == 0)
6344 *mod
|= XCB_MOD_MASK_SHIFT
;
6345 else if (strncasecmp(name
, "CONTROL", SWM_MODNAME_SIZE
) == 0)
6346 *mod
|= XCB_MOD_MASK_CONTROL
;
6348 *ks
= XStringToKeysym(name
);
6349 XConvertCase(*ks
, ks
, &uks
);
6350 if (ks
== XCB_NO_SYMBOL
) {
6352 "parsekeys: invalid key %s\n",
6358 DNPRINTF(SWM_D_KEY
, "parsekeys: leave ok\n");
6363 strdupsafe(const char *str
)
6368 return (strdup(str
));
6372 key_insert(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
6373 const char *spawn_name
)
6377 DNPRINTF(SWM_D_KEY
, "key_insert: enter %s [%s]\n",
6378 keyfuncs
[kfid
].name
, spawn_name
);
6380 if ((kp
= malloc(sizeof *kp
)) == NULL
)
6381 err(1, "key_insert: malloc");
6386 kp
->spawn_name
= strdupsafe(spawn_name
);
6387 RB_INSERT(key_tree
, &keys
, kp
);
6389 DNPRINTF(SWM_D_KEY
, "key_insert: leave\n");
6393 key_lookup(unsigned int mod
, KeySym ks
)
6400 return (RB_FIND(key_tree
, &keys
, &kp
));
6404 key_remove(struct key
*kp
)
6406 DNPRINTF(SWM_D_KEY
, "key_remove: %s\n", keyfuncs
[kp
->funcid
].name
);
6408 RB_REMOVE(key_tree
, &keys
, kp
);
6409 free(kp
->spawn_name
);
6412 DNPRINTF(SWM_D_KEY
, "key_remove: leave\n");
6416 key_replace(struct key
*kp
, unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
6417 const char *spawn_name
)
6419 DNPRINTF(SWM_D_KEY
, "key_replace: %s [%s]\n", keyfuncs
[kp
->funcid
].name
,
6423 key_insert(mod
, ks
, kfid
, spawn_name
);
6425 DNPRINTF(SWM_D_KEY
, "key_replace: leave\n");
6429 setkeybinding(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
6430 const char *spawn_name
)
6434 DNPRINTF(SWM_D_KEY
, "setkeybinding: enter %s [%s]\n",
6435 keyfuncs
[kfid
].name
, spawn_name
);
6437 if ((kp
= key_lookup(mod
, ks
)) != NULL
) {
6438 if (kfid
== KF_INVALID
)
6441 key_replace(kp
, mod
, ks
, kfid
, spawn_name
);
6442 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
6445 if (kfid
== KF_INVALID
) {
6446 warnx("bind: Key combination already unbound.");
6447 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
6451 key_insert(mod
, ks
, kfid
, spawn_name
);
6452 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
6456 setconfbinding(char *selector
, char *value
, int flags
)
6458 enum keyfuncid kfid
;
6461 struct spawn_prog
*sp
;
6463 /* suppress unused warning since var is needed */
6466 DNPRINTF(SWM_D_KEY
, "setconfbinding: enter\n");
6467 if (selector
== NULL
) {
6468 DNPRINTF(SWM_D_KEY
, "setconfbinding: unbind %s\n", value
);
6469 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
6471 setkeybinding(mod
, ks
, kfid
, NULL
);
6476 /* search by key function name */
6477 for (kfid
= 0; kfid
< KF_INVALID
; (kfid
)++) {
6478 if (strncasecmp(selector
, keyfuncs
[kfid
].name
,
6479 SWM_FUNCNAME_LEN
) == 0) {
6480 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match "
6481 "keyfunc\n", selector
);
6482 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
6483 setkeybinding(mod
, ks
, kfid
, NULL
);
6489 /* search by custom spawn name */
6490 TAILQ_FOREACH(sp
, &spawns
, entry
) {
6491 if (strcasecmp(selector
, sp
->name
) == 0) {
6492 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match "
6493 "spawn\n", selector
);
6494 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
6495 setkeybinding(mod
, ks
, KF_SPAWN_CUSTOM
,
6502 DNPRINTF(SWM_D_KEY
, "setconfbinding: no match\n");
6509 #define MODKEY_SHIFT MODKEY | XCB_MOD_MASK_SHIFT
6510 setkeybinding(MODKEY
, XK_b
, KF_BAR_TOGGLE
, NULL
);
6511 setkeybinding(MODKEY_SHIFT
, XK_b
, KF_BAR_TOGGLE_WS
,NULL
);
6512 setkeybinding(MODKEY
, XK_v
, KF_BUTTON2
, NULL
);
6513 setkeybinding(MODKEY
, XK_space
, KF_CYCLE_LAYOUT
,NULL
);
6514 setkeybinding(MODKEY_SHIFT
, XK_backslash
, KF_FLIP_LAYOUT
, NULL
);
6515 setkeybinding(MODKEY
, XK_t
, KF_FLOAT_TOGGLE
,NULL
);
6516 setkeybinding(MODKEY
, XK_m
, KF_FOCUS_MAIN
, NULL
);
6517 setkeybinding(MODKEY
, XK_j
, KF_FOCUS_NEXT
, NULL
);
6518 setkeybinding(MODKEY
, XK_Tab
, KF_FOCUS_NEXT
, NULL
);
6519 setkeybinding(MODKEY
, XK_k
, KF_FOCUS_PREV
, NULL
);
6520 setkeybinding(MODKEY_SHIFT
, XK_Tab
, KF_FOCUS_PREV
, NULL
);
6521 setkeybinding(MODKEY_SHIFT
, XK_equal
, KF_HEIGHT_GROW
,NULL
);
6522 setkeybinding(MODKEY_SHIFT
, XK_minus
, KF_HEIGHT_SHRINK
,NULL
);
6523 setkeybinding(MODKEY
, XK_w
, KF_ICONIFY
, NULL
);
6524 setkeybinding(MODKEY
, XK_h
, KF_MASTER_SHRINK
, NULL
);
6525 setkeybinding(MODKEY
, XK_l
, KF_MASTER_GROW
, NULL
);
6526 setkeybinding(MODKEY
, XK_comma
, KF_MASTER_ADD
, NULL
);
6527 setkeybinding(MODKEY
, XK_period
, KF_MASTER_DEL
, NULL
);
6528 setkeybinding(MODKEY_SHIFT
, XK_bracketright
,KF_MOVE_DOWN
,NULL
);
6529 setkeybinding(MODKEY
, XK_bracketleft
, KF_MOVE_LEFT
,NULL
);
6530 setkeybinding(MODKEY
, XK_bracketright
,KF_MOVE_RIGHT
,NULL
);
6531 setkeybinding(MODKEY_SHIFT
, XK_bracketleft
, KF_MOVE_UP
, NULL
);
6532 setkeybinding(MODKEY_SHIFT
, XK_KP_End
, KF_MVRG_1
, NULL
);
6533 setkeybinding(MODKEY_SHIFT
, XK_KP_Down
, KF_MVRG_2
, NULL
);
6534 setkeybinding(MODKEY_SHIFT
, XK_KP_Next
, KF_MVRG_3
, NULL
);
6535 setkeybinding(MODKEY_SHIFT
, XK_KP_Left
, KF_MVRG_4
, NULL
);
6536 setkeybinding(MODKEY_SHIFT
, XK_KP_Begin
, KF_MVRG_5
, NULL
);
6537 setkeybinding(MODKEY_SHIFT
, XK_KP_Right
, KF_MVRG_6
, NULL
);
6538 setkeybinding(MODKEY_SHIFT
, XK_KP_Home
, KF_MVRG_7
, NULL
);
6539 setkeybinding(MODKEY_SHIFT
, XK_KP_Up
, KF_MVRG_8
, NULL
);
6540 setkeybinding(MODKEY_SHIFT
, XK_KP_Prior
, KF_MVRG_9
, NULL
);
6541 setkeybinding(MODKEY_SHIFT
, XK_1
, KF_MVWS_1
, NULL
);
6542 setkeybinding(MODKEY_SHIFT
, XK_2
, KF_MVWS_2
, NULL
);
6543 setkeybinding(MODKEY_SHIFT
, XK_3
, KF_MVWS_3
, NULL
);
6544 setkeybinding(MODKEY_SHIFT
, XK_4
, KF_MVWS_4
, NULL
);
6545 setkeybinding(MODKEY_SHIFT
, XK_5
, KF_MVWS_5
, NULL
);
6546 setkeybinding(MODKEY_SHIFT
, XK_6
, KF_MVWS_6
, NULL
);
6547 setkeybinding(MODKEY_SHIFT
, XK_7
, KF_MVWS_7
, NULL
);
6548 setkeybinding(MODKEY_SHIFT
, XK_8
, KF_MVWS_8
, NULL
);
6549 setkeybinding(MODKEY_SHIFT
, XK_9
, KF_MVWS_9
, NULL
);
6550 setkeybinding(MODKEY_SHIFT
, XK_0
, KF_MVWS_10
, NULL
);
6551 setkeybinding(MODKEY_SHIFT
, XK_F1
, KF_MVWS_11
, NULL
);
6552 setkeybinding(MODKEY_SHIFT
, XK_F2
, KF_MVWS_12
, NULL
);
6553 setkeybinding(MODKEY_SHIFT
, XK_F3
, KF_MVWS_13
, NULL
);
6554 setkeybinding(MODKEY_SHIFT
, XK_F4
, KF_MVWS_14
, NULL
);
6555 setkeybinding(MODKEY_SHIFT
, XK_F5
, KF_MVWS_15
, NULL
);
6556 setkeybinding(MODKEY_SHIFT
, XK_F6
, KF_MVWS_16
, NULL
);
6557 setkeybinding(MODKEY_SHIFT
, XK_F7
, KF_MVWS_17
, NULL
);
6558 setkeybinding(MODKEY_SHIFT
, XK_F8
, KF_MVWS_18
, NULL
);
6559 setkeybinding(MODKEY_SHIFT
, XK_F9
, KF_MVWS_19
, NULL
);
6560 setkeybinding(MODKEY_SHIFT
, XK_F10
, KF_MVWS_20
, NULL
);
6561 setkeybinding(MODKEY_SHIFT
, XK_F11
, KF_MVWS_21
, NULL
);
6562 setkeybinding(MODKEY_SHIFT
, XK_F12
, KF_MVWS_22
, NULL
);
6563 setkeybinding(MODKEY_SHIFT
, XK_slash
, KF_NAME_WORKSPACE
,NULL
);
6564 setkeybinding(MODKEY_SHIFT
, XK_q
, KF_QUIT
, NULL
);
6565 setkeybinding(MODKEY_SHIFT
, XK_r
, KF_RAISE_TOGGLE
,NULL
);
6566 setkeybinding(MODKEY
, XK_q
, KF_RESTART
, NULL
);
6567 setkeybinding(MODKEY
, XK_KP_End
, KF_RG_1
, NULL
);
6568 setkeybinding(MODKEY
, XK_KP_Down
, KF_RG_2
, NULL
);
6569 setkeybinding(MODKEY
, XK_KP_Next
, KF_RG_3
, NULL
);
6570 setkeybinding(MODKEY
, XK_KP_Left
, KF_RG_4
, NULL
);
6571 setkeybinding(MODKEY
, XK_KP_Begin
, KF_RG_5
, NULL
);
6572 setkeybinding(MODKEY
, XK_KP_Right
, KF_RG_6
, NULL
);
6573 setkeybinding(MODKEY
, XK_KP_Home
, KF_RG_7
, NULL
);
6574 setkeybinding(MODKEY
, XK_KP_Up
, KF_RG_8
, NULL
);
6575 setkeybinding(MODKEY
, XK_KP_Prior
, KF_RG_9
, NULL
);
6576 setkeybinding(MODKEY_SHIFT
, XK_Right
, KF_RG_NEXT
, NULL
);
6577 setkeybinding(MODKEY_SHIFT
, XK_Left
, KF_RG_PREV
, NULL
);
6578 setkeybinding(MODKEY
, XK_f
, KF_SEARCH_WIN
, NULL
);
6579 setkeybinding(MODKEY
, XK_slash
, KF_SEARCH_WORKSPACE
,NULL
);
6580 setkeybinding(MODKEY_SHIFT
, XK_i
, KF_SPAWN_CUSTOM
,"initscr");
6581 setkeybinding(MODKEY_SHIFT
, XK_Delete
, KF_SPAWN_CUSTOM
,"lock");
6582 setkeybinding(MODKEY
, XK_p
, KF_SPAWN_CUSTOM
,"menu");
6583 setkeybinding(MODKEY
, XK_s
, KF_SPAWN_CUSTOM
,"screenshot_all");
6584 setkeybinding(MODKEY_SHIFT
, XK_s
, KF_SPAWN_CUSTOM
,"screenshot_wind");
6585 setkeybinding(MODKEY_SHIFT
, XK_Return
, KF_SPAWN_CUSTOM
,"term");
6586 setkeybinding(MODKEY_SHIFT
, XK_comma
, KF_STACK_INC
, NULL
);
6587 setkeybinding(MODKEY_SHIFT
, XK_period
, KF_STACK_DEC
, NULL
);
6588 setkeybinding(MODKEY_SHIFT
, XK_space
, KF_STACK_RESET
, NULL
);
6589 setkeybinding(MODKEY
, XK_Return
, KF_SWAP_MAIN
, NULL
);
6590 setkeybinding(MODKEY_SHIFT
, XK_j
, KF_SWAP_NEXT
, NULL
);
6591 setkeybinding(MODKEY_SHIFT
, XK_k
, KF_SWAP_PREV
, NULL
);
6592 setkeybinding(MODKEY_SHIFT
, XK_w
, KF_UNICONIFY
, NULL
);
6593 setkeybinding(MODKEY_SHIFT
, XK_v
, KF_VERSION
, NULL
);
6594 setkeybinding(MODKEY
, XK_equal
, KF_WIDTH_GROW
, NULL
);
6595 setkeybinding(MODKEY
, XK_minus
, KF_WIDTH_SHRINK
,NULL
);
6596 setkeybinding(MODKEY
, XK_x
, KF_WIND_DEL
, NULL
);
6597 setkeybinding(MODKEY_SHIFT
, XK_x
, KF_WIND_KILL
, NULL
);
6598 setkeybinding(MODKEY
, XK_1
, KF_WS_1
, NULL
);
6599 setkeybinding(MODKEY
, XK_2
, KF_WS_2
, NULL
);
6600 setkeybinding(MODKEY
, XK_3
, KF_WS_3
, NULL
);
6601 setkeybinding(MODKEY
, XK_4
, KF_WS_4
, NULL
);
6602 setkeybinding(MODKEY
, XK_5
, KF_WS_5
, NULL
);
6603 setkeybinding(MODKEY
, XK_6
, KF_WS_6
, NULL
);
6604 setkeybinding(MODKEY
, XK_7
, KF_WS_7
, NULL
);
6605 setkeybinding(MODKEY
, XK_8
, KF_WS_8
, NULL
);
6606 setkeybinding(MODKEY
, XK_9
, KF_WS_9
, NULL
);
6607 setkeybinding(MODKEY
, XK_0
, KF_WS_10
, NULL
);
6608 setkeybinding(MODKEY
, XK_F1
, KF_WS_11
, NULL
);
6609 setkeybinding(MODKEY
, XK_F2
, KF_WS_12
, NULL
);
6610 setkeybinding(MODKEY
, XK_F3
, KF_WS_13
, NULL
);
6611 setkeybinding(MODKEY
, XK_F4
, KF_WS_14
, NULL
);
6612 setkeybinding(MODKEY
, XK_F5
, KF_WS_15
, NULL
);
6613 setkeybinding(MODKEY
, XK_F6
, KF_WS_16
, NULL
);
6614 setkeybinding(MODKEY
, XK_F7
, KF_WS_17
, NULL
);
6615 setkeybinding(MODKEY
, XK_F8
, KF_WS_18
, NULL
);
6616 setkeybinding(MODKEY
, XK_F9
, KF_WS_19
, NULL
);
6617 setkeybinding(MODKEY
, XK_F10
, KF_WS_20
, NULL
);
6618 setkeybinding(MODKEY
, XK_F11
, KF_WS_21
, NULL
);
6619 setkeybinding(MODKEY
, XK_F12
, KF_WS_22
, NULL
);
6620 setkeybinding(MODKEY
, XK_Right
, KF_WS_NEXT
, NULL
);
6621 setkeybinding(MODKEY
, XK_Left
, KF_WS_PREV
, NULL
);
6622 setkeybinding(MODKEY
, XK_Up
, KF_WS_NEXT_ALL
, NULL
);
6623 setkeybinding(MODKEY
, XK_Down
, KF_WS_PREV_ALL
, NULL
);
6624 setkeybinding(MODKEY_SHIFT
, XK_Up
, KF_WS_NEXT_MOVE
,NULL
);
6625 setkeybinding(MODKEY_SHIFT
, XK_Down
, KF_WS_PREV_MOVE
,NULL
);
6626 setkeybinding(MODKEY
, XK_a
, KF_WS_PRIOR
, NULL
);
6628 setkeybinding(MODKEY_SHIFT
, XK_d
, KF_DUMPWINS
, NULL
);
6638 while (RB_EMPTY(&keys
) == 0) {
6639 kp
= RB_ROOT(&keys
);
6645 setkeymapping(char *selector
, char *value
, int flags
)
6647 char *keymapping_file
;
6649 /* suppress unused warnings since vars are needed */
6653 DNPRINTF(SWM_D_KEY
, "setkeymapping: enter\n");
6655 keymapping_file
= expand_tilde(value
);
6658 /* load new key bindings; if it fails, revert to default bindings */
6659 if (conf_load(keymapping_file
, SWM_CONF_KEYMAPPING
)) {
6664 free(keymapping_file
);
6666 DNPRINTF(SWM_D_KEY
, "setkeymapping: leave\n");
6671 updatenumlockmask(void)
6674 xcb_get_modifier_mapping_reply_t
*modmap_r
;
6675 xcb_keycode_t
*modmap
, kc
, *keycode
;
6679 modmap_r
= xcb_get_modifier_mapping_reply(conn
,
6680 xcb_get_modifier_mapping(conn
),
6683 modmap
= xcb_get_modifier_mapping_keycodes(modmap_r
);
6684 for (i
= 0; i
< 8; i
++) {
6685 for (j
= 0; j
< modmap_r
->keycodes_per_modifier
; j
++) {
6686 kc
= modmap
[i
* modmap_r
->keycodes_per_modifier
6688 keycode
= xcb_key_symbols_get_keycode(syms
,
6691 numlockmask
= (1 << i
);
6697 DNPRINTF(SWM_D_MISC
, "updatenumlockmask: %d\n", numlockmask
);
6704 int num_screens
, k
, j
;
6705 unsigned int modifiers
[4];
6706 xcb_keycode_t
*code
;
6708 DNPRINTF(SWM_D_MISC
, "grabkeys\n");
6709 updatenumlockmask();
6712 modifiers
[1] = numlockmask
;
6713 modifiers
[2] = XCB_MOD_MASK_LOCK
;
6714 modifiers
[3] = numlockmask
| XCB_MOD_MASK_LOCK
;
6716 num_screens
= get_screen_count();
6717 for (k
= 0; k
< num_screens
; k
++) {
6718 if (TAILQ_EMPTY(&screens
[k
].rl
))
6720 xcb_ungrab_key(conn
, XCB_GRAB_ANY
, screens
[k
].root
,
6722 RB_FOREACH(kp
, key_tree
, &keys
) {
6723 /* Skip unused ws binds. */
6724 if ((int)kp
->funcid
> KF_WS_1
+ workspace_limit
- 1 &&
6725 kp
->funcid
<= KF_WS_22
)
6728 /* Skip unused mvws binds. */
6729 if ((int)kp
->funcid
> KF_MVWS_1
+ workspace_limit
- 1 &&
6730 kp
->funcid
<= KF_MVWS_22
)
6733 if ((code
= xcb_key_symbols_get_keycode(syms
,
6735 for (j
= 0; j
< LENGTH(modifiers
); j
++)
6736 xcb_grab_key(conn
, 1,
6738 kp
->mod
| modifiers
[j
],
6739 *code
, XCB_GRAB_MODE_SYNC
,
6740 XCB_GRAB_MODE_SYNC
);
6748 grabbuttons(struct ws_win
*win
)
6750 unsigned int modifiers
[4];
6753 DNPRINTF(SWM_D_MOUSE
, "grabbuttons: win 0x%x\n", win
->id
);
6754 updatenumlockmask();
6757 modifiers
[1] = numlockmask
;
6758 modifiers
[2] = XCB_MOD_MASK_LOCK
;
6759 modifiers
[3] = numlockmask
| XCB_MOD_MASK_LOCK
;
6761 for (i
= 0; i
< LENGTH(buttons
); i
++)
6762 if (buttons
[i
].action
== client_click
)
6763 for (j
= 0; j
< LENGTH(modifiers
); ++j
)
6764 xcb_grab_button(conn
, 0, win
->id
, BUTTONMASK
,
6765 XCB_GRAB_MODE_SYNC
, XCB_GRAB_MODE_ASYNC
,
6766 XCB_WINDOW_NONE
, XCB_CURSOR_NONE
,
6767 buttons
[i
].button
, buttons
[i
].mask
|
6771 const char *quirkname
[] = {
6772 "NONE", /* config string for "no value" */
6780 "FOCUSONMAP_SINGLE",
6783 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
6784 #define SWM_Q_WS "\n|+ \t"
6786 parsequirks(char *qstr
, unsigned long *quirk
)
6796 while ((name
= strsep(&cp
, SWM_Q_WS
)) != NULL
) {
6798 cp
+= (long)strspn(cp
, SWM_Q_WS
);
6799 for (i
= 0; i
< LENGTH(quirkname
); i
++) {
6800 if (!strncasecmp(name
, quirkname
[i
], SWM_QUIRK_LEN
)) {
6801 DNPRINTF(SWM_D_QUIRK
,
6802 "parsequirks: %s\n", name
);
6807 *quirk
|= 1 << (i
-1);
6811 if (i
>= LENGTH(quirkname
)) {
6812 DNPRINTF(SWM_D_QUIRK
,
6813 "parsequirks: invalid quirk [%s]\n", name
);
6821 quirk_insert(const char *class, const char *name
, unsigned long quirk
)
6825 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: %s:%s [%lu]\n", class, name
,
6828 if ((qp
= malloc(sizeof *qp
)) == NULL
)
6829 err(1, "quirk_insert: malloc");
6830 if ((qp
->class = strdup(class)) == NULL
)
6831 err(1, "quirk_insert: strdup");
6832 if ((qp
->name
= strdup(name
)) == NULL
)
6833 err(1, "quirk_insert: strdup");
6836 TAILQ_INSERT_TAIL(&quirks
, qp
, entry
);
6838 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: leave\n");
6842 quirk_remove(struct quirk
*qp
)
6844 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: %s:%s [%lu]\n", qp
->class,
6845 qp
->name
, qp
->quirk
);
6847 TAILQ_REMOVE(&quirks
, qp
, entry
);
6852 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: leave\n");
6856 quirk_replace(struct quirk
*qp
, const char *class, const char *name
,
6857 unsigned long quirk
)
6859 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: %s:%s [%lu]\n", qp
->class,
6860 qp
->name
, qp
->quirk
);
6863 quirk_insert(class, name
, quirk
);
6865 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: leave\n");
6869 setquirk(const char *class, const char *name
, unsigned long quirk
)
6873 DNPRINTF(SWM_D_QUIRK
, "setquirk: enter %s:%s [%lu]\n", class, name
,
6876 /* Remove/replace existing quirk. */
6877 TAILQ_FOREACH(qp
, &quirks
, entry
) {
6878 if (!strcmp(qp
->class, class) && !strcmp(qp
->name
, name
)) {
6882 quirk_replace(qp
, class, name
, quirk
);
6883 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
6888 /* Only insert if quirk is not NONE. */
6890 quirk_insert(class, name
, quirk
);
6892 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
6896 setconfquirk(char *selector
, char *value
, int flags
)
6898 char *cp
, *class, *name
;
6902 /* suppress unused warning since var is needed */
6905 if (selector
== NULL
)
6907 if ((cp
= strchr(selector
, ':')) == NULL
)
6912 if ((retval
= parsequirks(value
, &qrks
)) == 0)
6913 setquirk(class, name
, qrks
);
6920 setquirk("MPlayer", "xv", SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
| SWM_Q_FOCUSPREV
);
6921 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT
);
6922 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ
);
6923 setquirk("Firefox", "Dialog", SWM_Q_FLOAT
);
6924 setquirk("Gimp", "gimp", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
6925 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ
);
6926 setquirk("xine", "Xine Window", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
6927 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
6928 setquirk("xine", "xine Panel", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
6929 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
6930 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN
| SWM_Q_FLOAT
);
6931 setquirk("pcb", "pcb", SWM_Q_FLOAT
);
6932 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
);
6935 /* conf file stuff */
6936 #define SWM_CONF_FILE "spectrwm.conf"
6937 #define SWM_CONF_FILE_OLD "scrotwm.conf"
6941 SWM_S_BAR_AT_BOTTOM
,
6942 SWM_S_BAR_BORDER_WIDTH
,
6945 SWM_S_BAR_ENABLED_WS
,
6950 SWM_S_BOUNDARY_WIDTH
,
6951 SWM_S_CLOCK_ENABLED
,
6954 SWM_S_CYCLE_VISIBLE
,
6956 SWM_S_DISABLE_BORDER
,
6958 SWM_S_FOCUS_CLOSE_WRAP
,
6959 SWM_S_FOCUS_DEFAULT
,
6961 SWM_S_REGION_PADDING
,
6966 SWM_S_STACK_ENABLED
,
6969 SWM_S_TITLE_CLASS_ENABLED
,
6970 SWM_S_TITLE_NAME_ENABLED
,
6971 SWM_S_URGENT_ENABLED
,
6972 SWM_S_VERBOSE_LAYOUT
,
6973 SWM_S_WINDOW_NAME_ENABLED
,
6974 SWM_S_WORKSPACE_LIMIT
6978 setconfvalue(char *selector
, char *value
, int flags
)
6980 struct workspace
*ws
;
6981 int i
, ws_id
, num_screens
;
6984 /* suppress unused warning since var is needed */
6988 case SWM_S_BAR_ACTION
:
6990 if ((bar_argv
[0] = expand_tilde(value
)) == NULL
)
6991 err(1, "setconfvalue: bar_action");
6993 case SWM_S_BAR_AT_BOTTOM
:
6994 bar_at_bottom
= atoi(value
);
6996 case SWM_S_BAR_BORDER_WIDTH
:
6997 bar_border_width
= atoi(value
);
6998 if (bar_border_width
< 0)
6999 bar_border_width
= 0;
7001 case SWM_S_BAR_DELAY
:
7002 /* No longer needed; leave to not break old conf files. */
7004 case SWM_S_BAR_ENABLED
:
7005 bar_enabled
= atoi(value
);
7007 case SWM_S_BAR_ENABLED_WS
:
7008 ws_id
= atoi(selector
) - 1;
7009 if (ws_id
< 0 || ws_id
>= workspace_limit
)
7010 errx(1, "setconfvalue: bar_enabled_ws: invalid "
7011 "workspace %d.", ws_id
+ 1);
7013 num_screens
= get_screen_count();
7014 for (i
= 0; i
< num_screens
; i
++) {
7015 ws
= (struct workspace
*)&screens
[i
].ws
;
7016 ws
[ws_id
].bar_enabled
= atoi(value
);
7019 case SWM_S_BAR_FONT
:
7021 if (asprintf(&bar_fonts
, "%s,%s", value
, bar_fonts
) == -1)
7022 err(1, "setconfvalue: asprintf: failed to allocate "
7023 "memory for bar_fonts.");
7026 /* If already in xft mode, then we are done. */
7027 if (!bar_font_legacy
)
7030 /* If there are any non-XLFD entries, switch to Xft mode. */
7031 while ((b
= strsep(&value
, ",")) != NULL
) {
7035 bar_font_legacy
= 0;
7040 case SWM_S_BAR_FORMAT
:
7042 if ((bar_format
= strdup(value
)) == NULL
)
7043 err(1, "setconfvalue: bar_format");
7045 case SWM_S_BAR_JUSTIFY
:
7046 if (!strcmp(value
, "left"))
7047 bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
7048 else if (!strcmp(value
, "center"))
7049 bar_justify
= SWM_BAR_JUSTIFY_CENTER
;
7050 else if (!strcmp(value
, "right"))
7051 bar_justify
= SWM_BAR_JUSTIFY_RIGHT
;
7053 errx(1, "invalid bar_justify");
7055 case SWM_S_BORDER_WIDTH
:
7056 border_width
= atoi(value
);
7057 if (border_width
< 0)
7060 case SWM_S_BOUNDARY_WIDTH
:
7061 boundary_width
= atoi(value
);
7062 if (boundary_width
< 0)
7065 case SWM_S_CLOCK_ENABLED
:
7066 clock_enabled
= atoi(value
);
7068 case SWM_S_CLOCK_FORMAT
:
7069 #ifndef SWM_DENY_CLOCK_FORMAT
7071 if ((clock_format
= strdup(value
)) == NULL
)
7072 err(1, "setconfvalue: clock_format");
7075 case SWM_S_CYCLE_EMPTY
:
7076 cycle_empty
= atoi(value
);
7078 case SWM_S_CYCLE_VISIBLE
:
7079 cycle_visible
= atoi(value
);
7081 case SWM_S_DIALOG_RATIO
:
7082 dialog_ratio
= atof(value
);
7083 if (dialog_ratio
> 1.0 || dialog_ratio
<= .3)
7086 case SWM_S_DISABLE_BORDER
:
7087 disable_border
= atoi(value
);
7089 case SWM_S_FOCUS_CLOSE
:
7090 if (!strcmp(value
, "first"))
7091 focus_close
= SWM_STACK_BOTTOM
;
7092 else if (!strcmp(value
, "last"))
7093 focus_close
= SWM_STACK_TOP
;
7094 else if (!strcmp(value
, "next"))
7095 focus_close
= SWM_STACK_ABOVE
;
7096 else if (!strcmp(value
, "previous"))
7097 focus_close
= SWM_STACK_BELOW
;
7099 errx(1, "focus_close");
7101 case SWM_S_FOCUS_CLOSE_WRAP
:
7102 focus_close_wrap
= atoi(value
);
7104 case SWM_S_FOCUS_DEFAULT
:
7105 if (!strcmp(value
, "last"))
7106 focus_default
= SWM_STACK_TOP
;
7107 else if (!strcmp(value
, "first"))
7108 focus_default
= SWM_STACK_BOTTOM
;
7110 errx(1, "focus_default");
7112 case SWM_S_FOCUS_MODE
:
7113 if (!strcmp(value
, "default"))
7114 focus_mode
= SWM_FOCUS_DEFAULT
;
7115 else if (!strcmp(value
, "follow") ||
7116 !strcmp(value
, "follow_cursor"))
7117 focus_mode
= SWM_FOCUS_FOLLOW
;
7118 else if (!strcmp(value
, "manual"))
7119 focus_mode
= SWM_FOCUS_MANUAL
;
7121 errx(1, "focus_mode");
7123 case SWM_S_REGION_PADDING
:
7124 region_padding
= atoi(value
);
7125 if (region_padding
< 0)
7128 case SWM_S_SPAWN_ORDER
:
7129 if (!strcmp(value
, "first"))
7130 spawn_position
= SWM_STACK_BOTTOM
;
7131 else if (!strcmp(value
, "last"))
7132 spawn_position
= SWM_STACK_TOP
;
7133 else if (!strcmp(value
, "next"))
7134 spawn_position
= SWM_STACK_ABOVE
;
7135 else if (!strcmp(value
, "previous"))
7136 spawn_position
= SWM_STACK_BELOW
;
7138 errx(1, "spawn_position");
7140 case SWM_S_SPAWN_TERM
:
7141 setconfspawn("term", value
, 0);
7142 setconfspawn("spawn_term", value
, 0);
7145 /* No longer needed; leave to not break old conf files. */
7147 case SWM_S_SS_ENABLED
:
7148 /* No longer needed; leave to not break old conf files. */
7150 case SWM_S_STACK_ENABLED
:
7151 stack_enabled
= atoi(value
);
7153 case SWM_S_TERM_WIDTH
:
7154 term_width
= atoi(value
);
7158 case SWM_S_TILE_GAP
:
7159 tile_gap
= atoi(value
);
7163 case SWM_S_TITLE_CLASS_ENABLED
:
7164 title_class_enabled
= atoi(value
);
7166 case SWM_S_TITLE_NAME_ENABLED
:
7167 title_name_enabled
= atoi(value
);
7169 case SWM_S_URGENT_ENABLED
:
7170 urgent_enabled
= atoi(value
);
7172 case SWM_S_VERBOSE_LAYOUT
:
7173 verbose_layout
= atoi(value
);
7174 for (i
= 0; layouts
[i
].l_stack
!= NULL
; i
++) {
7176 layouts
[i
].l_string
= fancy_stacker
;
7178 layouts
[i
].l_string
= plain_stacker
;
7181 case SWM_S_WINDOW_NAME_ENABLED
:
7182 window_name_enabled
= atoi(value
);
7184 case SWM_S_WORKSPACE_LIMIT
:
7185 workspace_limit
= atoi(value
);
7186 if (workspace_limit
> SWM_WS_MAX
)
7187 workspace_limit
= SWM_WS_MAX
;
7188 else if (workspace_limit
< 1)
7189 workspace_limit
= 1;
7198 setconfmodkey(char *selector
, char *value
, int flags
)
7200 /* suppress unused warnings since vars are needed */
7204 if (!strncasecmp(value
, "Mod1", strlen("Mod1")))
7205 update_modkey(XCB_MOD_MASK_1
);
7206 else if (!strncasecmp(value
, "Mod2", strlen("Mod2")))
7207 update_modkey(XCB_MOD_MASK_2
);
7208 else if (!strncasecmp(value
, "Mod3", strlen("Mod3")))
7209 update_modkey(XCB_MOD_MASK_3
);
7210 else if (!strncasecmp(value
, "Mod4", strlen("Mod4")))
7211 update_modkey(XCB_MOD_MASK_4
);
7218 setconfcolor(char *selector
, char *value
, int flags
)
7220 setscreencolor(value
, ((selector
== NULL
)?-1:atoi(selector
)), flags
);
7225 setconfregion(char *selector
, char *value
, int flags
)
7227 /* suppress unused warnings since vars are needed */
7231 custom_region(value
);
7236 setautorun(char *selector
, char *value
, int flags
)
7246 /* suppress unused warnings since vars are needed */
7250 if (getenv("SWM_STARTED"))
7254 if (sscanf(value
, "ws[%d]:%1023c", &ws_id
, s
) != 2)
7255 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
7257 if (ws_id
< 0 || ws_id
>= workspace_limit
)
7258 errx(1, "autorun: invalid workspace %d", ws_id
+ 1);
7260 sp
= expand_tilde((char *)&s
);
7263 * This is a little intricate
7265 * If the pid already exists we simply reuse it because it means it was
7266 * used before AND not claimed by manage_window. We get away with
7267 * altering it in the parent after INSERT because this can not be a race
7270 while ((ap
= strsep(&sp
, " \t")) != NULL
) {
7273 DNPRINTF(SWM_D_SPAWN
, "setautorun: arg [%s]\n", ap
);
7275 if ((a
.argv
= realloc(a
.argv
, argc
* sizeof(char *))) == NULL
)
7276 err(1, "setautorun: realloc");
7277 a
.argv
[argc
- 1] = ap
;
7281 if ((a
.argv
= realloc(a
.argv
, (argc
+ 1) * sizeof(char *))) == NULL
)
7282 err(1, "setautorun: realloc");
7283 a
.argv
[argc
] = NULL
;
7285 if ((pid
= fork()) == 0) {
7286 spawn(ws_id
, &a
, 1);
7295 p
= calloc(1, sizeof *p
);
7298 TAILQ_INSERT_TAIL(&pidlist
, p
, entry
);
7308 setlayout(char *selector
, char *value
, int flags
)
7310 int ws_id
, i
, x
, mg
, ma
, si
, ar
, f
= 0;
7311 int st
= SWM_V_STACK
, num_screens
;
7313 struct workspace
*ws
;
7315 /* suppress unused warnings since vars are needed */
7319 if (getenv("SWM_STARTED"))
7323 if (sscanf(value
, "ws[%d]:%d:%d:%d:%d:%1023c",
7324 &ws_id
, &mg
, &ma
, &si
, &ar
, s
) != 6)
7325 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
7326 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
7329 if (ws_id
< 0 || ws_id
>= workspace_limit
)
7330 errx(1, "layout: invalid workspace %d", ws_id
+ 1);
7332 if (!strcasecmp(s
, "vertical"))
7334 else if (!strcasecmp(s
, "vertical_flip")) {
7337 } else if (!strcasecmp(s
, "horizontal"))
7339 else if (!strcasecmp(s
, "horizontal_flip")) {
7342 } else if (!strcasecmp(s
, "fullscreen"))
7345 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
7346 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
7349 num_screens
= get_screen_count();
7350 for (i
= 0; i
< num_screens
; i
++) {
7351 ws
= (struct workspace
*)&screens
[i
].ws
;
7352 ws
[ws_id
].cur_layout
= &layouts
[st
];
7354 ws
[ws_id
].always_raise
= ar
;
7355 if (st
== SWM_MAX_STACK
)
7359 for (x
= 0; x
< abs(mg
); x
++) {
7360 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
7361 mg
>= 0 ? SWM_ARG_ID_MASTERGROW
:
7362 SWM_ARG_ID_MASTERSHRINK
);
7366 for (x
= 0; x
< abs(ma
); x
++) {
7367 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
7368 ma
>= 0 ? SWM_ARG_ID_MASTERADD
:
7369 SWM_ARG_ID_MASTERDEL
);
7373 for (x
= 0; x
< abs(si
); x
++) {
7374 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
7375 si
>= 0 ? SWM_ARG_ID_STACKINC
:
7376 SWM_ARG_ID_STACKDEC
);
7381 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
7382 SWM_ARG_ID_FLIPLAYOUT
);
7392 /* config options */
7393 struct config_option
{
7395 int (*func
)(char*, char*, int);
7398 struct config_option configopt
[] = {
7399 { "autorun", setautorun
, 0 },
7400 { "bar_action", setconfvalue
, SWM_S_BAR_ACTION
},
7401 { "bar_at_bottom", setconfvalue
, SWM_S_BAR_AT_BOTTOM
},
7402 { "bar_border", setconfcolor
, SWM_S_COLOR_BAR_BORDER
},
7403 { "bar_border_unfocus", setconfcolor
, SWM_S_COLOR_BAR_BORDER_UNFOCUS
},
7404 { "bar_border_width", setconfvalue
, SWM_S_BAR_BORDER_WIDTH
},
7405 { "bar_color", setconfcolor
, SWM_S_COLOR_BAR
},
7406 { "bar_delay", setconfvalue
, SWM_S_BAR_DELAY
},
7407 { "bar_enabled", setconfvalue
, SWM_S_BAR_ENABLED
},
7408 { "bar_enabled_ws", setconfvalue
, SWM_S_BAR_ENABLED_WS
},
7409 { "bar_font", setconfvalue
, SWM_S_BAR_FONT
},
7410 { "bar_font_color", setconfcolor
, SWM_S_COLOR_BAR_FONT
},
7411 { "bar_format", setconfvalue
, SWM_S_BAR_FORMAT
},
7412 { "bar_justify", setconfvalue
, SWM_S_BAR_JUSTIFY
},
7413 { "bind", setconfbinding
, 0 },
7414 { "border_width", setconfvalue
, SWM_S_BORDER_WIDTH
},
7415 { "boundary_width", setconfvalue
, SWM_S_BOUNDARY_WIDTH
},
7416 { "clock_enabled", setconfvalue
, SWM_S_CLOCK_ENABLED
},
7417 { "clock_format", setconfvalue
, SWM_S_CLOCK_FORMAT
},
7418 { "color_focus", setconfcolor
, SWM_S_COLOR_FOCUS
},
7419 { "color_unfocus", setconfcolor
, SWM_S_COLOR_UNFOCUS
},
7420 { "cycle_empty", setconfvalue
, SWM_S_CYCLE_EMPTY
},
7421 { "cycle_visible", setconfvalue
, SWM_S_CYCLE_VISIBLE
},
7422 { "dialog_ratio", setconfvalue
, SWM_S_DIALOG_RATIO
},
7423 { "disable_border", setconfvalue
, SWM_S_DISABLE_BORDER
},
7424 { "focus_close", setconfvalue
, SWM_S_FOCUS_CLOSE
},
7425 { "focus_close_wrap", setconfvalue
, SWM_S_FOCUS_CLOSE_WRAP
},
7426 { "focus_default", setconfvalue
, SWM_S_FOCUS_DEFAULT
},
7427 { "focus_mode", setconfvalue
, SWM_S_FOCUS_MODE
},
7428 { "keyboard_mapping", setkeymapping
, 0 },
7429 { "layout", setlayout
, 0 },
7430 { "modkey", setconfmodkey
, 0 },
7431 { "program", setconfspawn
, 0 },
7432 { "quirk", setconfquirk
, 0 },
7433 { "region", setconfregion
, 0 },
7434 { "region_padding", setconfvalue
, SWM_S_REGION_PADDING
},
7435 { "screenshot_app", setconfvalue
, SWM_S_SS_APP
},
7436 { "screenshot_enabled", setconfvalue
, SWM_S_SS_ENABLED
},
7437 { "spawn_position", setconfvalue
, SWM_S_SPAWN_ORDER
},
7438 { "spawn_term", setconfvalue
, SWM_S_SPAWN_TERM
},
7439 { "stack_enabled", setconfvalue
, SWM_S_STACK_ENABLED
},
7440 { "term_width", setconfvalue
, SWM_S_TERM_WIDTH
},
7441 { "tile_gap", setconfvalue
, SWM_S_TILE_GAP
},
7442 { "title_class_enabled", setconfvalue
, SWM_S_TITLE_CLASS_ENABLED
},
7443 { "title_name_enabled", setconfvalue
, SWM_S_TITLE_NAME_ENABLED
},
7444 { "urgent_enabled", setconfvalue
, SWM_S_URGENT_ENABLED
},
7445 { "verbose_layout", setconfvalue
, SWM_S_VERBOSE_LAYOUT
},
7446 { "window_name_enabled", setconfvalue
, SWM_S_WINDOW_NAME_ENABLED
},
7447 { "workspace_limit", setconfvalue
, SWM_S_WORKSPACE_LIMIT
},
7451 _add_startup_exception(const char *fmt
, va_list ap
)
7453 if (vasprintf(&startup_exception
, fmt
, ap
) == -1)
7454 warn("%s: asprintf", __func__
);
7458 add_startup_exception(const char *fmt
, ...)
7464 if (startup_exception
)
7467 /* force bar to be enabled due to exception */
7471 _add_startup_exception(fmt
, ap
);
7476 conf_load(const char *filename
, int keymapping
)
7479 char *line
= NULL
, *cp
, *optsub
, *optval
= NULL
;
7480 size_t linelen
, lineno
= 0;
7481 int wordlen
, i
, optidx
;
7482 struct config_option
*opt
= NULL
;
7484 DNPRINTF(SWM_D_CONF
, "conf_load: begin\n");
7486 if (filename
== NULL
) {
7487 warnx("conf_load: no filename");
7491 DNPRINTF(SWM_D_CONF
, "conf_load: open %s\n", filename
);
7493 if ((config
= fopen(filename
, "r")) == NULL
) {
7494 warn("conf_load: fopen: %s", filename
);
7498 while (!feof(config
)) {
7502 if ((line
= fparseln(config
, &linelen
, &lineno
, NULL
,
7503 FPARSELN_UNESCCOMM
| FPARSELN_UNESCCONT
)) == NULL
) {
7505 err(1, "%s", filename
);
7510 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
7511 if (cp
[0] == '\0') {
7515 /* get config option */
7516 wordlen
= strcspn(cp
, "=[ \t\n");
7518 add_startup_exception("%s: line %zd: no option found",
7523 for (i
= 0; i
< LENGTH(configopt
); i
++) {
7524 opt
= &configopt
[i
];
7525 if (!strncasecmp(cp
, opt
->optname
, wordlen
) &&
7526 (int)strlen(opt
->optname
) == wordlen
) {
7532 add_startup_exception("%s: line %zd: unknown option "
7533 "%.*s", filename
, lineno
, wordlen
, cp
);
7536 if (keymapping
&& opt
&& strcmp(opt
->optname
, "bind")) {
7537 add_startup_exception("%s: line %zd: invalid option "
7538 "%.*s", filename
, lineno
, wordlen
, cp
);
7542 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
7544 /* from here on out we call goto invalid to continue */
7546 /* get [selector] if any */
7550 wordlen
= strcspn(cp
, "]");
7553 add_startup_exception("%s: line %zd: "
7554 "syntax error", filename
, lineno
);
7558 if (asprintf(&optsub
, "%.*s", wordlen
, cp
) ==
7560 add_startup_exception("%s: line %zd: "
7561 "unable to allocatememory for "
7562 "selector", filename
, lineno
);
7567 cp
+= strspn(cp
, "] \t\n"); /* eat trailing */
7569 cp
+= strspn(cp
, "= \t\n"); /* eat trailing */
7571 optval
= strdup(cp
);
7572 if (strlen(optval
) == 0) {
7573 add_startup_exception("%s: line %zd: must supply value "
7574 "to %s", filename
, lineno
,
7575 configopt
[optidx
].optname
);
7578 /* call function to deal with it all */
7579 if (configopt
[optidx
].func(optsub
, optval
,
7580 configopt
[optidx
].funcflags
) != 0) {
7581 add_startup_exception("%s: line %zd: invalid data for "
7582 "%s", filename
, lineno
, configopt
[optidx
].optname
);
7599 DNPRINTF(SWM_D_CONF
, "conf_load: end\n");
7605 set_child_transient(struct ws_win
*win
, xcb_window_t
*trans
)
7607 struct ws_win
*parent
, *w
;
7608 struct swm_region
*r
;
7609 struct workspace
*ws
;
7610 xcb_icccm_wm_hints_t wmh
;
7612 parent
= find_window(win
->transient
);
7614 parent
->focus_child
= win
;
7616 DNPRINTF(SWM_D_MISC
, "set_child_transient: parent doesn't exist"
7617 " for 0x%x trans 0x%x\n", win
->id
, win
->transient
);
7619 r
= root_to_region(win
->wa
->root
, SWM_CK_ALL
);
7621 /* parent doen't exist in our window list */
7622 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
7623 if (xcb_icccm_get_wm_hints_reply(conn
,
7624 xcb_icccm_get_wm_hints(conn
, w
->id
),
7626 warnx("can't get hints for 0x%x", w
->id
);
7630 if (win
->hints
.window_group
!= wmh
.window_group
)
7633 w
->focus_child
= win
;
7634 win
->transient
= w
->id
;
7636 DNPRINTF(SWM_D_MISC
, "set_child_transient: adjusting "
7637 "transient to 0x%x\n", win
->transient
);
7644 window_get_pid(xcb_window_t win
)
7649 xcb_get_property_cookie_t pc
;
7650 xcb_get_property_reply_t
*pr
;
7652 apid
= get_atom_from_string("_NET_WM_PID");
7653 if (apid
== XCB_ATOM_NONE
)
7656 pc
= xcb_get_property(conn
, 0, win
, apid
, XCB_ATOM_CARDINAL
, 0, 1);
7657 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
7660 if (pr
->type
!= XCB_ATOM_CARDINAL
) {
7665 if (pr
->type
== apid
&& pr
->format
== 32)
7666 ret
= *((pid_t
*)xcb_get_property_value(pr
));
7672 apid
= get_atom_from_string("_SWM_PID");
7673 pc
= xcb_get_property(conn
, 0, win
, apid
, XCB_ATOM_STRING
,
7675 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
7678 if (pr
->type
!= apid
) {
7683 ret
= (pid_t
)strtonum(xcb_get_property_value(pr
), 0, INT_MAX
, &errstr
);
7690 get_ws_idx(xcb_window_t id
)
7696 xcb_get_property_reply_t
*gpr
;
7698 gpr
= xcb_get_property_reply(conn
,
7699 xcb_get_property(conn
, 0, id
, a_swm_ws
,
7700 XCB_ATOM_STRING
, 0, SWM_PROPLEN
),
7705 proplen
= xcb_get_property_value_length(gpr
);
7707 prop
= malloc(proplen
+ 1);
7710 xcb_get_property_value(gpr
),
7712 prop
[proplen
] = '\0';
7719 DNPRINTF(SWM_D_PROP
, "get_ws_idx: _SWM_WS: %s\n", prop
);
7720 ws_idx
= (int)strtonum(prop
, 0, workspace_limit
- 1, &errstr
);
7722 DNPRINTF(SWM_D_PROP
, "get_ws_idx: window: #%s: %s",
7732 manage_window(xcb_window_t id
, uint16_t mapped
)
7734 xcb_window_t trans
= XCB_WINDOW_NONE
;
7735 struct ws_win
*win
, *ww
;
7737 char ws_idx_str
[SWM_PROPLEN
];
7738 struct swm_region
*r
;
7742 xcb_icccm_get_wm_protocols_reply_t wpr
;
7744 if ((win
= find_window(id
)) != NULL
) {
7745 DNPRINTF(SWM_D_MISC
, "manage_window: win 0x%x already "
7746 "managed; skipping.)\n", id
);
7747 return (win
); /* Already managed. */
7750 /* See if window is on the unmanaged list. */
7751 if ((win
= find_unmanaged_window(id
)) != NULL
) {
7752 DNPRINTF(SWM_D_MISC
, "manage_window: win 0x%x found on "
7753 "unmanaged list.\n", id
);
7754 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
7757 set_child_transient(win
, &trans
);
7761 DNPRINTF(SWM_D_MISC
, "manage_window: win 0x%x is new.\n", id
);
7764 /* Create and initialize ws_win object. */
7765 if ((win
= calloc(1, sizeof(struct ws_win
))) == NULL
)
7766 err(1, "manage_window: calloc: failed to allocate memory for "
7771 /* Get window geometry. */
7772 win
->wa
= xcb_get_geometry_reply(conn
,
7773 xcb_get_geometry(conn
, win
->id
),
7776 /* Figure out which region the window belongs to. */
7777 r
= root_to_region(win
->wa
->root
, SWM_CK_ALL
);
7779 /* Ignore window border if there is one. */
7780 WIDTH(win
) = win
->wa
->width
;
7781 HEIGHT(win
) = win
->wa
->height
;
7782 X(win
) = win
->wa
->x
+ win
->wa
->border_width
- border_width
;
7783 Y(win
) = win
->wa
->y
+ win
->wa
->border_width
- border_width
;
7785 win
->mapped
= mapped
;
7786 win
->floatmaxed
= 0;
7787 win
->ewmh_flags
= 0;
7788 win
->s
= r
->s
; /* this never changes */
7790 /* Get WM_SIZE_HINTS. */
7791 xcb_icccm_get_wm_normal_hints_reply(conn
,
7792 xcb_icccm_get_wm_normal_hints(conn
, win
->id
),
7796 xcb_icccm_get_wm_hints_reply(conn
,
7797 xcb_icccm_get_wm_hints(conn
, win
->id
),
7800 /* Get WM_TRANSIENT_FOR; see if window is a transient. */
7801 xcb_icccm_get_wm_transient_for_reply(conn
,
7802 xcb_icccm_get_wm_transient_for(conn
, win
->id
),
7805 win
->transient
= trans
;
7806 set_child_transient(win
, &win
->transient
);
7809 /* Get supported protocols. */
7810 if (xcb_icccm_get_wm_protocols_reply(conn
,
7811 xcb_icccm_get_wm_protocols(conn
, win
->id
, a_prot
),
7813 for (i
= 0; i
< wpr
.atoms_len
; i
++) {
7814 if (wpr
.atoms
[i
] == a_takefocus
)
7815 win
->take_focus
= 1;
7816 if (wpr
.atoms
[i
] == a_delete
)
7817 win
->can_delete
= 1;
7819 xcb_icccm_get_wm_protocols_reply_wipe(&wpr
);
7822 win
->iconic
= get_swm_iconic(win
);
7824 /* Figure out which workspace the window belongs to. */
7825 if ((p
= find_pid(window_get_pid(win
->id
))) != NULL
) {
7826 win
->ws
= &r
->s
->ws
[p
->ws
];
7827 TAILQ_REMOVE(&pidlist
, p
, entry
);
7830 } else if ((ws_idx
= get_ws_idx(win
->id
)) != -1 &&
7832 /* _SWM_WS is set; use that. */
7833 win
->ws
= &r
->s
->ws
[ws_idx
];
7834 } else if (trans
&& (ww
= find_window(trans
)) != NULL
) {
7835 /* Launch transients in the same ws as parent. */
7841 /* Set the _SWM_WS atom so we can remember this after reincarnation. */
7842 if (snprintf(ws_idx_str
, SWM_PROPLEN
, "%d", win
->ws
->idx
) <
7844 DNPRINTF(SWM_D_PROP
, "manage_window: set _SWM_WS: %s\n",
7846 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
7847 a_swm_ws
, XCB_ATOM_STRING
, 8, strlen(ws_idx_str
),
7851 /* WS must already be set for this to work. */
7852 store_float_geom(win
);
7855 ewmh_autoquirk(win
);
7857 /* Determine initial quirks. */
7858 if (xcb_icccm_get_wm_class_reply(conn
,
7859 xcb_icccm_get_wm_class(conn
, win
->id
),
7861 DNPRINTF(SWM_D_CLASS
, "manage_window: class: %s, name: %s\n",
7862 win
->ch
.class_name
, win
->ch
.instance_name
);
7864 /* java is retarded so treat it special */
7865 if (strstr(win
->ch
.instance_name
, "sun-awt")) {
7866 DNPRINTF(SWM_D_CLASS
, "manage_window: java window "
7871 TAILQ_FOREACH(qp
, &quirks
, entry
) {
7872 if (!strcmp(win
->ch
.class_name
, qp
->class) &&
7873 !strcmp(win
->ch
.instance_name
, qp
->name
)) {
7874 DNPRINTF(SWM_D_CLASS
, "manage_window: on quirks"
7875 "list; mask: 0x%lx\n", qp
->quirk
);
7876 if (qp
->quirk
& SWM_Q_FLOAT
)
7878 win
->quirks
= qp
->quirk
;
7883 /* Alter window position if quirky */
7884 if (win
->quirks
& SWM_Q_ANYWHERE
)
7887 /* Reset font sizes (the bruteforce way; no default keybinding). */
7888 if (win
->quirks
& SWM_Q_XTERM_FONTADJ
) {
7889 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
7890 fake_keypress(win
, XK_KP_Subtract
, XCB_MOD_MASK_SHIFT
);
7891 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
7892 fake_keypress(win
, XK_KP_Add
, XCB_MOD_MASK_SHIFT
);
7895 /* Make sure window is positioned inside its region, if its active. */
7897 region_containment(win
, r
, SWM_CW_ALLSIDES
|
7898 SWM_CW_HARDBOUNDARY
);
7902 /* Select which X events to monitor and set border pixel color. */
7903 wa
[0] = win
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
;
7904 wa
[1] = XCB_EVENT_MASK_ENTER_WINDOW
| XCB_EVENT_MASK_PROPERTY_CHANGE
|
7905 XCB_EVENT_MASK_STRUCTURE_NOTIFY
;
7907 wa
[1] |= XCB_EVENT_MASK_LEAVE_WINDOW
| XCB_EVENT_MASK_FOCUS_CHANGE
;
7910 xcb_change_window_attributes(conn
, win
->id
, XCB_CW_BORDER_PIXEL
|
7911 XCB_CW_EVENT_MASK
, wa
);
7914 /* Figure out where to stack the window in the workspace. */
7915 if (trans
&& (ww
= find_window(trans
)))
7916 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, ww
, win
, entry
);
7917 else if (win
->ws
->focus
&& spawn_position
== SWM_STACK_ABOVE
)
7918 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, win
->ws
->focus
, win
,
7920 else if (win
->ws
->focus
&& spawn_position
== SWM_STACK_BELOW
)
7921 TAILQ_INSERT_BEFORE(win
->ws
->focus
, win
, entry
);
7922 else switch (spawn_position
) {
7925 case SWM_STACK_ABOVE
:
7926 TAILQ_INSERT_TAIL(&win
->ws
->winlist
, win
, entry
);
7928 case SWM_STACK_BOTTOM
:
7929 case SWM_STACK_BELOW
:
7930 TAILQ_INSERT_HEAD(&win
->ws
->winlist
, win
, entry
);
7933 /* Get initial _NET_WM_STATE */
7934 ewmh_get_win_state(win
);
7935 /* Set initial _NET_WM_ALLOWED_ACTIONS */
7936 ewmh_update_actions(win
);
7940 DNPRINTF(SWM_D_MISC
, "manage_window: done. window: 0x%x, (x,y) w x h: "
7941 "(%d,%d) %d x %d, ws: %d, iconic: %s, transient: 0x%x\n", win
->id
,
7942 X(win
), Y(win
), WIDTH(win
), HEIGHT(win
), win
->ws
->idx
,
7943 YESNO(win
->iconic
), win
->transient
);
7949 free_window(struct ws_win
*win
)
7951 DNPRINTF(SWM_D_MISC
, "free_window: window: 0x%x\n", win
->id
);
7956 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
7961 xcb_icccm_get_wm_class_reply_wipe(&win
->ch
);
7966 memset(win
, 0xff, sizeof *win
); /* XXX kill later */
7969 DNPRINTF(SWM_D_MISC
, "free_window: done.\n");
7973 unmanage_window(struct ws_win
*win
)
7975 struct ws_win
*parent
;
7980 DNPRINTF(SWM_D_MISC
, "unmanage_window: window: 0x%x\n", win
->id
);
7982 if (win
->transient
) {
7983 parent
= find_window(win
->transient
);
7985 parent
->focus_child
= NULL
;
7988 TAILQ_REMOVE(&win
->ws
->winlist
, win
, entry
);
7989 TAILQ_INSERT_TAIL(&win
->ws
->unmanagedlist
, win
, entry
);
7993 expose(xcb_expose_event_t
*e
)
7996 struct swm_region
*r
;
7998 DNPRINTF(SWM_D_EVENT
, "expose: window: 0x%x\n", e
->window
);
8000 num_screens
= get_screen_count();
8001 for (i
= 0; i
< num_screens
; i
++)
8002 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
8003 if (e
->window
== WINID(r
->bar
))
8011 focusin(xcb_focus_in_event_t
*e
)
8013 DNPRINTF(SWM_D_EVENT
, "focusin: window: 0x%x, mode: %s(%u), "
8014 "detail: %s(%u)\n", e
->event
, get_notify_mode_label(e
->mode
),
8015 e
->mode
, get_notify_detail_label(e
->detail
), e
->detail
);
8019 focusout(xcb_focus_out_event_t
*e
)
8021 DNPRINTF(SWM_D_EVENT
, "focusout: window: 0x%x, mode: %s(%u), "
8022 "detail: %s(%u)\n", e
->event
, get_notify_mode_label(e
->mode
),
8023 e
->mode
, get_notify_detail_label(e
->detail
), e
->detail
);
8028 keypress(xcb_key_press_event_t
*e
)
8030 xcb_keysym_t keysym
;
8033 keysym
= xcb_key_press_lookup_keysym(syms
, e
, 0);
8035 DNPRINTF(SWM_D_EVENT
, "keypress: keysym: %u, win (x,y): 0x%x (%d,%d), "
8036 "detail: %u, time: %u, root (x,y): 0x%x (%d,%d), child: 0x%x, "
8037 "state: %u, same_screen: %s\n", keysym
, e
->event
, e
->event_x
,
8038 e
->event_y
, e
->detail
, e
->time
, e
->root
, e
->root_x
, e
->root_y
,
8039 e
->child
, e
->state
, YESNO(e
->same_screen
));
8041 if ((kp
= key_lookup(CLEANMASK(e
->state
), keysym
)) == NULL
)
8044 last_event_time
= e
->time
;
8046 if (kp
->funcid
== KF_SPAWN_CUSTOM
)
8047 spawn_custom(root_to_region(e
->root
, SWM_CK_ALL
),
8048 &(keyfuncs
[kp
->funcid
].args
), kp
->spawn_name
);
8049 else if (keyfuncs
[kp
->funcid
].func
)
8050 keyfuncs
[kp
->funcid
].func(root_to_region(e
->root
, SWM_CK_ALL
),
8051 &(keyfuncs
[kp
->funcid
].args
));
8054 /* Unfreeze grab events. */
8055 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
, e
->time
);
8058 DNPRINTF(SWM_D_EVENT
, "keypress: done.\n");
8062 buttonpress(xcb_button_press_event_t
*e
)
8064 struct ws_win
*win
= NULL
;
8065 struct swm_region
*r
, *old_r
;
8069 DNPRINTF(SWM_D_EVENT
, "buttonpress: win (x,y): 0x%x (%d,%d), "
8070 "detail: %u, time: %u, root (x,y): 0x%x (%d,%d), child: 0x%x, "
8071 "state: %u, same_screen: %s\n", e
->event
, e
->event_x
, e
->event_y
,
8072 e
->detail
, e
->time
, e
->root
, e
->root_x
, e
->root_y
, e
->child
,
8073 e
->state
, YESNO(e
->same_screen
));
8075 if (e
->event
== e
->root
) {
8076 if (e
->child
!= 0) {
8077 win
= find_window(e
->child
);
8078 /* Pass ButtonPress to window if it isn't managed. */
8082 /* Focus on empty region */
8083 /* If no windows on region if its empty. */
8084 r
= root_to_region(e
->root
, SWM_CK_POINTER
);
8086 DNPRINTF(SWM_D_EVENT
, "buttonpress: "
8087 "NULL region; ignoring.\n");
8091 if (TAILQ_EMPTY(&r
->ws
->winlist
)) {
8092 old_r
= root_to_region(e
->root
, SWM_CK_FOCUS
);
8093 if (old_r
&& old_r
!= r
)
8094 unfocus_win(old_r
->ws
->focus
);
8096 xcb_set_input_focus(conn
,
8097 XCB_INPUT_FOCUS_PARENT
, e
->root
, e
->time
);
8099 /* Clear bar since empty. */
8107 win
= find_window(e
->event
);
8113 last_event_time
= e
->time
;
8115 focus_win(get_focus_magic(win
));
8117 for (i
= 0; i
< LENGTH(buttons
); i
++)
8118 if (client_click
== buttons
[i
].action
&& buttons
[i
].func
&&
8119 buttons
[i
].button
== e
->detail
&&
8120 CLEANMASK(buttons
[i
].mask
) == CLEANMASK(e
->state
)) {
8121 buttons
[i
].func(win
, &buttons
[i
].args
);
8127 DNPRINTF(SWM_D_EVENT
, "buttonpress: passing to window.\n");
8128 /* Replay event to event window */
8129 xcb_allow_events(conn
, XCB_ALLOW_REPLAY_POINTER
, e
->time
);
8131 DNPRINTF(SWM_D_EVENT
, "buttonpress: handled.\n");
8132 /* Unfreeze grab events. */
8133 xcb_allow_events(conn
, XCB_ALLOW_SYNC_POINTER
, e
->time
);
8141 print_win_geom(xcb_window_t w
)
8143 xcb_get_geometry_reply_t
*wa
;
8145 wa
= xcb_get_geometry_reply(conn
, xcb_get_geometry(conn
, w
), NULL
);
8148 DNPRINTF(SWM_D_MISC
, "print_win_geom: window not found: 0x%x\n",
8153 DNPRINTF(SWM_D_MISC
, "print_win_geom: window: 0x%x, root: 0x%x, "
8154 "depth: %u, (x,y) w x h: (%d,%d) %d x %d, border: %d\n",
8155 w
, wa
->root
, wa
->depth
, wa
->x
, wa
->y
, wa
->width
, wa
->height
,
8164 get_stack_mode_name(uint8_t mode
)
8169 case XCB_STACK_MODE_ABOVE
:
8172 case XCB_STACK_MODE_BELOW
:
8175 case XCB_STACK_MODE_TOP_IF
:
8178 case XCB_STACK_MODE_BOTTOM_IF
:
8181 case XCB_STACK_MODE_OPPOSITE
:
8193 configurerequest(xcb_configure_request_event_t
*e
)
8196 struct swm_region
*r
= NULL
;
8199 uint32_t wc
[7] = {0};
8201 if ((win
= find_window(e
->window
)) == NULL
)
8202 if ((win
= find_unmanaged_window(e
->window
)) == NULL
)
8206 if (swm_debug
& SWM_D_EVENT
) {
8207 print_win_geom(e
->window
);
8209 DNPRINTF(SWM_D_EVENT
, "configurerequest: window: 0x%x, "
8210 "parent: 0x%x, new: %s, value_mask: %u { ", e
->window
,
8211 e
->parent
, YESNO(new), e
->value_mask
);
8212 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
)
8213 DPRINTF("X: %d ", e
->x
);
8214 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
)
8215 DPRINTF("Y: %d ", e
->y
);
8216 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
)
8217 DPRINTF("W: %u ", e
->width
);
8218 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
)
8219 DPRINTF("H: %u ", e
->height
);
8220 if (e
->value_mask
& XCB_CONFIG_WINDOW_BORDER_WIDTH
)
8221 DPRINTF("Border: %u ", e
->border_width
);
8222 if (e
->value_mask
& XCB_CONFIG_WINDOW_SIBLING
)
8223 DPRINTF("Sibling: 0x%x ", e
->sibling
);
8224 if (e
->value_mask
& XCB_CONFIG_WINDOW_STACK_MODE
)
8225 DPRINTF("StackMode: %s(%u) ",
8226 get_stack_mode_name(e
->stack_mode
), e
->stack_mode
);
8232 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
) {
8233 mask
|= XCB_CONFIG_WINDOW_X
;
8236 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
) {
8237 mask
|= XCB_CONFIG_WINDOW_Y
;
8240 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
) {
8241 mask
|= XCB_CONFIG_WINDOW_WIDTH
;
8244 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
) {
8245 mask
|= XCB_CONFIG_WINDOW_HEIGHT
;
8246 wc
[i
++] = e
->height
;
8248 if (e
->value_mask
& XCB_CONFIG_WINDOW_BORDER_WIDTH
) {
8249 mask
|= XCB_CONFIG_WINDOW_BORDER_WIDTH
;
8250 wc
[i
++] = e
->border_width
;
8252 if (e
->value_mask
& XCB_CONFIG_WINDOW_SIBLING
) {
8253 mask
|= XCB_CONFIG_WINDOW_SIBLING
;
8254 wc
[i
++] = e
->sibling
;
8256 if (e
->value_mask
& XCB_CONFIG_WINDOW_STACK_MODE
) {
8257 mask
|= XCB_CONFIG_WINDOW_STACK_MODE
;
8258 wc
[i
++] = e
->stack_mode
;
8262 xcb_configure_window(conn
, e
->window
, mask
, wc
);
8265 } else if ((!win
->manual
|| win
->quirks
& SWM_Q_ANYWHERE
) &&
8266 !(win
->ewmh_flags
& EWMH_F_FULLSCREEN
)) {
8269 else if (win
->ws
->old_r
)
8272 /* windows are centered unless ANYWHERE quirk is set. */
8273 if (win
->quirks
& SWM_Q_ANYWHERE
) {
8274 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
) {
8275 win
->g_float
.x
= e
->x
;
8277 win
->g_float
.x
-= X(r
);
8280 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
) {
8281 win
->g_float
.y
= e
->y
;
8283 win
->g_float
.y
-= Y(r
);
8287 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
)
8288 win
->g_float
.w
= e
->width
;
8290 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
)
8291 win
->g_float
.h
= e
->height
;
8293 win
->g_floatvalid
= 1;
8295 if (win
->floating
&& r
&& (win
->transient
||
8296 win
->ws
->cur_layout
!= &layouts
[SWM_MAX_STACK
])) {
8297 WIDTH(win
) = win
->g_float
.w
;
8298 HEIGHT(win
) = win
->g_float
.h
;
8301 stack_floater(win
, r
);
8313 DNPRINTF(SWM_D_EVENT
, "configurerequest: done.\n");
8317 configurenotify(xcb_configure_notify_event_t
*e
)
8321 DNPRINTF(SWM_D_EVENT
, "configurenotify: win 0x%x, event win: 0x%x, "
8322 "(x,y) WxH: (%d,%d) %ux%u, border: %u, above_sibling: 0x%x, "
8323 "override_redirect: %s\n", e
->window
, e
->event
, e
->x
, e
->y
,
8324 e
->width
, e
->height
, e
->border_width
, e
->above_sibling
,
8325 YESNO(e
->override_redirect
));
8327 win
= find_window(e
->window
);
8329 xcb_icccm_get_wm_normal_hints_reply(conn
,
8330 xcb_icccm_get_wm_normal_hints(conn
, win
->id
),
8333 if (font_adjusted
) {
8341 destroynotify(xcb_destroy_notify_event_t
*e
)
8345 DNPRINTF(SWM_D_EVENT
, "destroynotify: window: 0x%x\n", e
->window
);
8347 if ((win
= find_window(e
->window
)) == NULL
) {
8348 if ((win
= find_unmanaged_window(e
->window
)) == NULL
)
8354 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
8355 /* If we were focused, make sure we focus on something else. */
8356 if (win
== win
->ws
->focus
)
8357 win
->ws
->focus_pending
= get_focus_prev(win
);
8360 unmanage_window(win
);
8363 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
8364 if (win
->ws
->focus_pending
) {
8365 focus_win(win
->ws
->focus_pending
);
8366 win
->ws
->focus_pending
= NULL
;
8367 } else if (win
== win
->ws
->focus
&& win
->ws
->r
) {
8368 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
,
8369 win
->ws
->r
->id
, XCB_CURRENT_TIME
);
8380 get_notify_detail_label(uint8_t detail
)
8385 case XCB_NOTIFY_DETAIL_ANCESTOR
:
8388 case XCB_NOTIFY_DETAIL_VIRTUAL
:
8391 case XCB_NOTIFY_DETAIL_INFERIOR
:
8394 case XCB_NOTIFY_DETAIL_NONLINEAR
:
8395 label
= "Nonlinear";
8397 case XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL
:
8398 label
= "NonlinearVirtual";
8400 case XCB_NOTIFY_DETAIL_POINTER
:
8403 case XCB_NOTIFY_DETAIL_POINTER_ROOT
:
8404 label
= "PointerRoot";
8406 case XCB_NOTIFY_DETAIL_NONE
:
8417 get_notify_mode_label(uint8_t mode
)
8422 case XCB_NOTIFY_MODE_NORMAL
:
8425 case XCB_NOTIFY_MODE_GRAB
:
8428 case XCB_NOTIFY_MODE_UNGRAB
:
8431 case XCB_NOTIFY_MODE_WHILE_GRABBED
:
8432 label
= "WhileGrabbed";
8443 enternotify(xcb_enter_notify_event_t
*e
)
8446 struct swm_region
*r
;
8448 DNPRINTF(SWM_D_FOCUS
, "enternotify: time: %u, win (x,y): 0x%x "
8449 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): 0x%x (%d,%d), "
8450 "child: 0x%x, same_screen_focus: %s, state: %d\n",
8451 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
8452 get_notify_mode_label(e
->mode
), e
->mode
,
8453 get_notify_detail_label(e
->detail
), e
->detail
,
8454 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
8455 YESNO(e
->same_screen_focus
), e
->state
);
8457 if (focus_mode
== SWM_FOCUS_MANUAL
&&
8458 e
->mode
== XCB_NOTIFY_MODE_NORMAL
) {
8459 DNPRINTF(SWM_D_EVENT
, "enternotify: manual focus; ignoring.\n");
8463 last_event_time
= e
->time
;
8465 if ((win
= find_window(e
->event
)) == NULL
) {
8466 if (e
->event
== e
->root
) {
8467 /* If no windows on pointer region, then focus root. */
8468 r
= root_to_region(e
->root
, SWM_CK_POINTER
);
8470 DNPRINTF(SWM_D_EVENT
, "enternotify: "
8471 "NULL region; ignoring.\n");
8477 DNPRINTF(SWM_D_EVENT
, "enternotify: window is NULL; "
8482 focus_win(get_focus_magic(win
));
8490 leavenotify(xcb_leave_notify_event_t
*e
)
8492 DNPRINTF(SWM_D_FOCUS
, "leavenotify: time: %u, win (x,y): 0x%x "
8493 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): 0x%x (%d,%d), "
8494 "child: 0x%x, same_screen_focus: %s, state: %d\n",
8495 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
8496 get_notify_mode_label(e
->mode
), e
->mode
,
8497 get_notify_detail_label(e
->detail
), e
->detail
,
8498 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
8499 YESNO(e
->same_screen_focus
), e
->state
);
8504 mapnotify(xcb_map_notify_event_t
*e
)
8508 DNPRINTF(SWM_D_EVENT
, "mapnotify: window: 0x%x\n", e
->window
);
8510 if ((win
= find_window(e
->window
)) == NULL
)
8511 win
= manage_window(e
->window
, 1);
8514 set_win_state(win
, XCB_ICCCM_WM_STATE_NORMAL
);
8516 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
8517 if (win
->ws
->focus_pending
== win
) {
8519 win
->ws
->focus_pending
= NULL
;
8528 mappingnotify(xcb_mapping_notify_event_t
*e
)
8530 xcb_refresh_keyboard_mapping(syms
, e
);
8532 if (e
->request
== XCB_MAPPING_KEYBOARD
)
8537 maprequest(xcb_map_request_event_t
*e
)
8539 struct ws_win
*win
, *w
= NULL
;
8540 xcb_get_window_attributes_reply_t
*war
;
8542 DNPRINTF(SWM_D_EVENT
, "maprequest: win 0x%x\n",
8545 war
= xcb_get_window_attributes_reply(conn
,
8546 xcb_get_window_attributes(conn
, e
->window
),
8549 DNPRINTF(SWM_D_EVENT
, "maprequest: window lost.\n");
8553 if (war
->override_redirect
) {
8554 DNPRINTF(SWM_D_EVENT
, "maprequest: override_redirect; "
8559 win
= manage_window(e
->window
,
8560 (war
->map_state
== XCB_MAP_STATE_VIEWABLE
));
8562 /* The new window should get focus; prepare. */
8563 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
8564 !(win
->quirks
& SWM_Q_NOFOCUSONMAP
)) {
8565 if (win
->quirks
& SWM_Q_FOCUSONMAP_SINGLE
) {
8566 /* See if other wins of same type are already mapped. */
8567 TAILQ_FOREACH(w
, &win
->ws
->winlist
, entry
) {
8568 if (w
== win
|| !w
->mapped
)
8571 if (!strcmp(w
->ch
.class_name
,
8572 win
->ch
.class_name
) &&
8573 !strcmp(w
->ch
.instance_name
,
8574 win
->ch
.instance_name
))
8580 win
->ws
->focus_pending
= get_focus_magic(win
);
8583 /* All windows need to be mapped if they are in the current workspace.*/
8587 /* Ignore EnterNotify to handle the mapnotify without interference. */
8588 if (focus_mode
== SWM_FOCUS_DEFAULT
)
8589 event_drain(XCB_ENTER_NOTIFY
);
8592 DNPRINTF(SWM_D_EVENT
, "maprequest: done.\n");
8596 motionnotify(xcb_motion_notify_event_t
*e
)
8598 struct swm_region
*r
;
8601 DNPRINTF(SWM_D_FOCUS
, "motionnotify: time: %u, win (x,y): 0x%x "
8602 "(%d,%d), detail: %s(%d), root (x,y): 0x%x (%d,%d), "
8603 "child: 0x%x, same_screen_focus: %s, state: %d\n",
8604 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
8605 get_notify_detail_label(e
->detail
), e
->detail
,
8606 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
8607 YESNO(e
->same_screen
), e
->state
);
8609 last_event_time
= e
->time
;
8611 if (focus_mode
== SWM_FOCUS_MANUAL
)
8614 num_screens
= get_screen_count();
8615 for (i
= 0; i
< num_screens
; i
++)
8616 if (screens
[i
].root
== e
->root
)
8619 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
8620 if (X(r
) <= e
->root_x
&& e
->root_x
< MAX_X(r
) &&
8621 Y(r
) <= e
->root_y
&& e
->root_y
< MAX_Y(r
))
8629 get_atom_name(xcb_atom_t atom
)
8634 * This should be disabled during most debugging since
8635 * xcb_get_* causes an xcb_flush.
8638 xcb_get_atom_name_reply_t
*r
;
8640 r
= xcb_get_atom_name_reply(conn
,
8641 xcb_get_atom_name(conn
, atom
),
8644 len
= xcb_get_atom_name_name_length(r
);
8646 name
= malloc(len
+ 1);
8648 memcpy(name
, xcb_get_atom_name_name(r
), len
);
8662 propertynotify(xcb_property_notify_event_t
*e
)
8665 struct workspace
*ws
;
8669 name
= get_atom_name(e
->atom
);
8670 DNPRINTF(SWM_D_EVENT
, "propertynotify: window: 0x%x, atom: %s(%u), "
8671 "time: %#x, state: %u\n", e
->window
, name
, e
->atom
, e
->time
,
8675 win
= find_window(e
->window
);
8681 last_event_time
= e
->time
;
8683 if (e
->atom
== a_swm_iconic
) {
8684 if (e
->state
== XCB_PROPERTY_NEW_VALUE
) {
8685 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
8686 ws
->focus_pending
= get_focus_prev(win
);
8694 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
8695 if (ws
->focus_pending
) {
8696 focus_win(ws
->focus_pending
);
8697 ws
->focus_pending
= NULL
;
8699 xcb_set_input_focus(conn
,
8700 XCB_INPUT_FOCUS_PARENT
,
8708 } else if (e
->state
== XCB_PROPERTY_DELETE
) {
8709 /* The window is no longer iconic, restack ws. */
8710 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
8711 ws
->focus_pending
= get_focus_magic(win
);
8715 /* Flush EnterNotify for mapnotify, if needed. */
8718 } else if (e
->atom
== a_state
) {
8719 /* State just changed, make sure it gets focused if mapped. */
8720 if (e
->state
== XCB_PROPERTY_NEW_VALUE
) {
8721 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
8723 ws
->focus_pending
== win
) {
8724 focus_win(ws
->focus_pending
);
8725 ws
->focus_pending
= NULL
;
8729 } else if (e
->atom
== XCB_ATOM_WM_CLASS
||
8730 e
->atom
== XCB_ATOM_WM_NAME
) {
8738 unmapnotify(xcb_unmap_notify_event_t
*e
)
8741 struct workspace
*ws
;
8743 DNPRINTF(SWM_D_EVENT
, "unmapnotify: window: 0x%x\n", e
->window
);
8745 /* If we aren't managing the window, then ignore. */
8746 win
= find_window(e
->window
);
8747 if (win
== NULL
|| win
->id
!= e
->window
)
8752 if (getstate(e
->window
) != XCB_ICCCM_WM_STATE_ICONIC
)
8753 set_win_state(win
, XCB_ICCCM_WM_STATE_ICONIC
);
8756 /* window unmapped itself */
8757 /* do unmap/unfocus/restack and unmanage */
8760 /* If win was focused, make sure to focus on something else. */
8761 if (win
== ws
->focus
) {
8762 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
8763 ws
->focus_pending
= get_focus_prev(win
);
8764 DNPRINTF(SWM_D_EVENT
, "unmapnotify: "
8765 "focus_pending: 0x%x\n",
8766 WINID(ws
->focus_pending
));
8772 unmanage_window(win
);
8777 if (focus_mode
== SWM_FOCUS_FOLLOW
) {
8779 focus_win(get_pointer_win(ws
->r
->s
->root
));
8780 } else if (ws
->focus_pending
) {
8781 focus_win(ws
->focus_pending
);
8782 ws
->focus_pending
= NULL
;
8783 } else if (ws
->focus
== NULL
&& ws
->r
) {
8784 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
,
8785 ws
->r
->id
, XCB_CURRENT_TIME
);
8789 if (getstate(e
->window
) == XCB_ICCCM_WM_STATE_NORMAL
)
8790 set_win_state(win
, XCB_ICCCM_WM_STATE_ICONIC
);
8797 visibilitynotify(xcb_visibility_notify_event_t
*e
)
8799 DNPRINTF(SWM_D_EVENT
, "visibilitynotify: window: 0x%x\n",
8805 clientmessage(xcb_client_message_event_t
*e
)
8808 xcb_map_request_event_t mre
;
8812 name
= get_atom_name(e
->type
);
8813 DNPRINTF(SWM_D_EVENT
, "clientmessage: window: 0x%x, atom: %s(%u)\n",
8814 e
->window
, name
, e
->type
);
8817 win
= find_window(e
->window
);
8820 if (e
->type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
8821 /* Manage the window with maprequest. */
8822 DNPRINTF(SWM_D_EVENT
, "clientmessage: request focus on "
8823 "unmanaged window.\n");
8824 mre
.window
= e
->window
;
8830 if (e
->type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
8831 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_ACTIVE_WINDOW\n");
8834 if (e
->type
== ewmh
[_NET_CLOSE_WINDOW
].atom
) {
8835 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_CLOSE_WINDOW\n");
8836 if (win
->can_delete
)
8837 client_msg(win
, a_delete
, 0);
8839 xcb_kill_client(conn
, win
->id
);
8841 if (e
->type
== ewmh
[_NET_MOVERESIZE_WINDOW
].atom
) {
8842 DNPRINTF(SWM_D_EVENT
,
8843 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
8844 if (win
->floating
) {
8845 if (e
->data
.data32
[0] & (1<<8)) /* x */
8846 X(win
) = e
->data
.data32
[1];
8847 if (e
->data
.data32
[0] & (1<<9)) /* y */
8848 Y(win
) = e
->data
.data32
[2];
8849 if (e
->data
.data32
[0] & (1<<10)) /* width */
8850 WIDTH(win
) = e
->data
.data32
[3];
8851 if (e
->data
.data32
[0] & (1<<11)) /* height */
8852 HEIGHT(win
) = e
->data
.data32
[4];
8857 /* TODO: Change stack sizes */
8858 /* notify no change was made. */
8859 config_win(win
, NULL
);
8862 if (e
->type
== ewmh
[_NET_WM_STATE
].atom
) {
8863 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_WM_STATE\n");
8864 ewmh_update_win_state(win
, e
->data
.data32
[1], e
->data
.data32
[0]);
8865 if (e
->data
.data32
[2])
8866 ewmh_update_win_state(win
, e
->data
.data32
[2],
8878 int errcode
= xcb_connection_has_error(conn
);
8879 #ifdef XCB_CONN_ERROR
8882 case XCB_CONN_ERROR
:
8883 s
= "Socket error, pipe error or other stream error.";
8885 case XCB_CONN_CLOSED_EXT_NOTSUPPORTED
:
8886 s
= "Extension not supported.";
8888 case XCB_CONN_CLOSED_MEM_INSUFFICIENT
:
8889 s
= "Insufficient memory.";
8891 case XCB_CONN_CLOSED_REQ_LEN_EXCEED
:
8892 s
= "Request length was exceeded.";
8894 case XCB_CONN_CLOSED_PARSE_ERR
:
8895 s
= "Error parsing display string.";
8898 s
= "Unknown error.";
8901 errx(errcode
, "X CONNECTION ERROR: %s", s
);
8904 errx(errcode
, "X CONNECTION ERROR");
8912 const uint32_t val
= XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT
|
8913 XCB_EVENT_MASK_ENTER_WINDOW
;
8915 xcb_void_cookie_t wac
;
8916 xcb_generic_error_t
*error
;
8918 /* this causes an error if some other window manager is running */
8919 num_screens
= get_screen_count();
8920 for (i
= 0; i
< num_screens
; i
++) {
8921 if ((sc
= get_screen(i
)) == NULL
)
8922 errx(1, "ERROR: can't get screen %d.", i
);
8923 DNPRINTF(SWM_D_INIT
, "enable_wm: screen %d, root: 0x%x\n",
8925 wac
= xcb_change_window_attributes_checked(conn
, sc
->root
,
8926 XCB_CW_EVENT_MASK
, &val
);
8927 if ((error
= xcb_request_check(conn
, wac
))) {
8928 DNPRINTF(SWM_D_INIT
, "enable_wm: error_code: %u\n",
8934 /* click to focus on empty region */
8935 xcb_grab_button(conn
, 1, sc
->root
, BUTTONMASK
,
8936 XCB_GRAB_MODE_SYNC
, XCB_GRAB_MODE_ASYNC
, XCB_WINDOW_NONE
,
8937 XCB_CURSOR_NONE
, XCB_BUTTON_INDEX_1
, XCB_BUTTON_MASK_ANY
);
8944 new_region(struct swm_screen
*s
, int x
, int y
, int w
, int h
)
8946 struct swm_region
*r
, *n
;
8947 struct workspace
*ws
= NULL
;
8951 DNPRINTF(SWM_D_MISC
, "new region: screen[%d]:%dx%d+%d+%d\n",
8952 s
->idx
, w
, h
, x
, y
);
8954 /* remove any conflicting regions */
8955 n
= TAILQ_FIRST(&s
->rl
);
8958 n
= TAILQ_NEXT(r
, entry
);
8959 if (X(r
) < (x
+ w
) && (X(r
) + WIDTH(r
)) > x
&&
8960 Y(r
) < (y
+ h
) && (Y(r
) + HEIGHT(r
)) > y
) {
8961 if (r
->ws
->r
!= NULL
)
8962 r
->ws
->old_r
= r
->ws
->r
;
8965 xcb_destroy_window(conn
, r
->id
);
8966 TAILQ_REMOVE(&s
->rl
, r
, entry
);
8967 TAILQ_INSERT_TAIL(&s
->orl
, r
, entry
);
8971 /* search old regions for one to reuse */
8973 /* size + location match */
8974 TAILQ_FOREACH(r
, &s
->orl
, entry
)
8975 if (X(r
) == x
&& Y(r
) == y
&&
8976 HEIGHT(r
) == h
&& WIDTH(r
) == w
)
8980 TAILQ_FOREACH(r
, &s
->orl
, entry
)
8981 if (HEIGHT(r
) == h
&& WIDTH(r
) == w
)
8985 TAILQ_REMOVE(&s
->orl
, r
, entry
);
8986 /* try to use old region's workspace */
8987 if (r
->ws
->r
== NULL
)
8990 if ((r
= calloc(1, sizeof(struct swm_region
))) == NULL
)
8991 err(1, "new_region: calloc: failed to allocate memory "
8994 /* if we don't have a workspace already, find one */
8996 for (i
= 0; i
< workspace_limit
; i
++)
8997 if (s
->ws
[i
].r
== NULL
) {
9004 errx(1, "new_region: no free workspaces");
9015 TAILQ_INSERT_TAIL(&s
->rl
, r
, entry
);
9017 /* Invisible region window to detect pointer events on empty regions. */
9018 r
->id
= xcb_generate_id(conn
);
9019 wa
[0] = XCB_EVENT_MASK_POINTER_MOTION
|
9020 XCB_EVENT_MASK_POINTER_MOTION_HINT
;
9022 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, r
->id
, r
->s
->root
,
9023 X(r
), Y(r
), WIDTH(r
), HEIGHT(r
), 0, XCB_WINDOW_CLASS_INPUT_ONLY
,
9024 XCB_COPY_FROM_PARENT
, XCB_CW_EVENT_MASK
, wa
);
9026 xcb_map_window(conn
, r
->id
);
9032 #ifdef SWM_XRR_HAS_CRTC
9035 #endif /* SWM_XRR_HAS_CRTC */
9036 struct swm_region
*r
;
9039 xcb_randr_get_screen_resources_current_cookie_t src
;
9040 xcb_randr_get_screen_resources_current_reply_t
*srr
;
9041 xcb_randr_get_crtc_info_cookie_t cic
;
9042 xcb_randr_get_crtc_info_reply_t
*cir
= NULL
;
9043 xcb_randr_crtc_t
*crtc
;
9044 xcb_screen_t
*screen
;
9046 DNPRINTF(SWM_D_MISC
, "scan_xrandr: screen: %d\n", i
);
9048 if ((screen
= get_screen(i
)) == NULL
)
9049 errx(1, "ERROR: can't get screen %d.", i
);
9051 num_screens
= get_screen_count();
9052 if (i
>= num_screens
)
9053 errx(1, "scan_xrandr: invalid screen");
9055 /* remove any old regions */
9056 while ((r
= TAILQ_FIRST(&screens
[i
].rl
)) != NULL
) {
9057 r
->ws
->old_r
= r
->ws
->r
= NULL
;
9059 xcb_destroy_window(conn
, r
->id
);
9060 TAILQ_REMOVE(&screens
[i
].rl
, r
, entry
);
9061 TAILQ_INSERT_TAIL(&screens
[i
].orl
, r
, entry
);
9065 /* map virtual screens onto physical screens */
9066 #ifdef SWM_XRR_HAS_CRTC
9067 if (xrandr_support
) {
9068 src
= xcb_randr_get_screen_resources_current(conn
,
9070 srr
= xcb_randr_get_screen_resources_current_reply(conn
, src
,
9073 new_region(&screens
[i
], 0, 0,
9074 screen
->width_in_pixels
,
9075 screen
->height_in_pixels
);
9078 ncrtc
= srr
->num_crtcs
;
9080 crtc
= xcb_randr_get_screen_resources_current_crtcs(srr
);
9081 for (c
= 0; c
< ncrtc
; c
++) {
9082 cic
= xcb_randr_get_crtc_info(conn
, crtc
[c
],
9084 cir
= xcb_randr_get_crtc_info_reply(conn
, cic
, NULL
);
9087 if (cir
->num_outputs
== 0) {
9093 new_region(&screens
[i
], 0, 0,
9094 screen
->width_in_pixels
,
9095 screen
->height_in_pixels
);
9097 new_region(&screens
[i
],
9098 cir
->x
, cir
->y
, cir
->width
, cir
->height
);
9103 #endif /* SWM_XRR_HAS_CRTC */
9105 /* If detection failed, create a single region that spans the screen. */
9106 if (TAILQ_EMPTY(&screens
[i
].rl
))
9107 new_region(&screens
[i
], 0, 0, screen
->width_in_pixels
,
9108 screen
->height_in_pixels
);
9111 /* Cleanup unused previously visible workspaces. */
9112 TAILQ_FOREACH(r
, &screens
[i
].orl
, entry
) {
9113 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
)
9116 /* The screen shouldn't focus on an unused region. */
9117 if (screens
[i
].r_focus
== r
)
9118 screens
[i
].r_focus
= NULL
;
9121 DNPRINTF(SWM_D_MISC
, "scan_xrandr: done.\n");
9125 screenchange(xcb_randr_screen_change_notify_event_t
*e
)
9127 struct swm_region
*r
;
9130 DNPRINTF(SWM_D_EVENT
, "screenchange: root: 0x%x\n", e
->root
);
9132 num_screens
= get_screen_count();
9133 /* silly event doesn't include the screen index */
9134 for (i
= 0; i
< num_screens
; i
++)
9135 if (screens
[i
].root
== e
->root
)
9137 if (i
>= num_screens
)
9138 errx(1, "screenchange: screen not found");
9140 /* brute force for now, just re-enumerate the regions */
9144 print_win_geom(e
->root
);
9146 /* add bars to all regions */
9147 for (i
= 0; i
< num_screens
; i
++) {
9148 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
9154 /* Make sure a region has focus on each screen. */
9155 for (i
= 0; i
< num_screens
; i
++) {
9156 if (screens
[i
].r_focus
== NULL
) {
9157 r
= TAILQ_FIRST(&screens
[i
].rl
);
9170 struct swm_region
*r
= NULL
;
9171 xcb_window_t
*wins
= NULL
, trans
;
9173 int i
, j
, num_screens
;
9174 uint16_t state
, manage
, mapped
;
9176 xcb_query_tree_cookie_t qtc
;
9177 xcb_query_tree_reply_t
*qtr
;
9178 xcb_get_window_attributes_cookie_t gac
;
9179 xcb_get_window_attributes_reply_t
*gar
;
9180 xcb_get_property_cookie_t pc
;
9182 DNPRINTF(SWM_D_INIT
, "grab_windows: begin\n");
9183 num_screens
= get_screen_count();
9184 for (i
= 0; i
< num_screens
; i
++) {
9185 qtc
= xcb_query_tree(conn
, screens
[i
].root
);
9186 qtr
= xcb_query_tree_reply(conn
, qtc
, NULL
);
9189 wins
= xcb_query_tree_children(qtr
);
9190 no
= xcb_query_tree_children_length(qtr
);
9191 /* attach windows to a region */
9192 /* normal windows */
9193 DNPRINTF(SWM_D_INIT
, "grab_windows: grab top level windows.\n");
9194 for (j
= 0; j
< no
; j
++) {
9195 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
9196 if (r
->id
== wins
[j
]) {
9197 DNPRINTF(SWM_D_INIT
, "grab_windows: "
9198 "skip %#x; region input window.\n",
9207 gac
= xcb_get_window_attributes(conn
, wins
[j
]);
9208 gar
= xcb_get_window_attributes_reply(conn
, gac
, NULL
);
9210 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
9211 "doesn't exist.\n", wins
[j
]);
9215 if (gar
->override_redirect
) {
9216 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
9217 "override_redirect set.\n", wins
[j
]);
9222 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
9223 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
9225 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
9226 "is transient for %#x.\n", wins
[j
], trans
);
9231 state
= getstate(wins
[j
]);
9232 manage
= state
!= XCB_ICCCM_WM_STATE_WITHDRAWN
;
9233 mapped
= gar
->map_state
!= XCB_MAP_STATE_UNMAPPED
;
9234 if (mapped
|| manage
)
9235 manage_window(wins
[j
], mapped
);
9238 /* transient windows */
9239 DNPRINTF(SWM_D_INIT
, "grab_windows: grab transient windows.\n");
9240 for (j
= 0; j
< no
; j
++) {
9241 gac
= xcb_get_window_attributes(conn
, wins
[j
]);
9242 gar
= xcb_get_window_attributes_reply(conn
, gac
, NULL
);
9244 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
9245 "doesn't exist.\n", wins
[j
]);
9249 if (gar
->override_redirect
) {
9250 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
9251 "override_redirect set.\n", wins
[j
]);
9256 state
= getstate(wins
[j
]);
9257 manage
= state
!= XCB_ICCCM_WM_STATE_WITHDRAWN
;
9258 mapped
= gar
->map_state
!= XCB_MAP_STATE_UNMAPPED
;
9259 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
9260 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
9261 &trans
, NULL
) && manage
)
9262 manage_window(wins
[j
], mapped
);
9267 DNPRINTF(SWM_D_INIT
, "grab_windows: done.\n");
9273 int i
, j
, k
, num_screens
;
9274 struct workspace
*ws
;
9275 uint32_t gcv
[1], wa
[1];
9276 const xcb_query_extension_reply_t
*qep
;
9277 xcb_screen_t
*screen
;
9278 xcb_randr_query_version_cookie_t c
;
9279 xcb_randr_query_version_reply_t
*r
;
9281 num_screens
= get_screen_count();
9282 if ((screens
= calloc(num_screens
,
9283 sizeof(struct swm_screen
))) == NULL
)
9284 err(1, "setup_screens: calloc: failed to allocate memory for "
9287 /* initial Xrandr setup */
9289 qep
= xcb_get_extension_data(conn
, &xcb_randr_id
);
9291 c
= xcb_randr_query_version(conn
, 1, 1);
9292 r
= xcb_randr_query_version_reply(conn
, c
, NULL
);
9294 if (r
->major_version
>= 1) {
9296 xrandr_eventbase
= qep
->first_event
;
9302 wa
[0] = cursors
[XC_LEFT_PTR
].cid
;
9304 /* map physical screens */
9305 for (i
= 0; i
< num_screens
; i
++) {
9306 DNPRINTF(SWM_D_WS
, "setup_screens: init screen: %d\n", i
);
9308 screens
[i
].r_focus
= NULL
;
9310 TAILQ_INIT(&screens
[i
].rl
);
9311 TAILQ_INIT(&screens
[i
].orl
);
9312 if ((screen
= get_screen(i
)) == NULL
)
9313 errx(1, "ERROR: can't get screen %d.", i
);
9314 screens
[i
].root
= screen
->root
;
9316 /* set default colors */
9317 setscreencolor("red", i
+ 1, SWM_S_COLOR_FOCUS
);
9318 setscreencolor("rgb:88/88/88", i
+ 1, SWM_S_COLOR_UNFOCUS
);
9319 setscreencolor("rgb:00/80/80", i
+ 1, SWM_S_COLOR_BAR_BORDER
);
9320 setscreencolor("rgb:00/40/40", i
+ 1,
9321 SWM_S_COLOR_BAR_BORDER_UNFOCUS
);
9322 setscreencolor("black", i
+ 1, SWM_S_COLOR_BAR
);
9323 setscreencolor("rgb:a0/a0/a0", i
+ 1, SWM_S_COLOR_BAR_FONT
);
9325 /* create graphics context on screen */
9326 screens
[i
].bar_gc
= xcb_generate_id(conn
);
9328 xcb_create_gc(conn
, screens
[i
].bar_gc
, screens
[i
].root
,
9329 XCB_GC_GRAPHICS_EXPOSURES
, gcv
);
9331 /* set default cursor */
9332 xcb_change_window_attributes(conn
, screens
[i
].root
,
9335 /* init all workspaces */
9336 /* XXX these should be dynamically allocated too */
9337 for (j
= 0; j
< SWM_WS_MAX
; j
++) {
9338 ws
= &screens
[i
].ws
[j
];
9341 ws
->bar_enabled
= 1;
9343 ws
->focus_prev
= NULL
;
9344 ws
->focus_pending
= NULL
;
9347 TAILQ_INIT(&ws
->winlist
);
9348 TAILQ_INIT(&ws
->unmanagedlist
);
9350 for (k
= 0; layouts
[k
].l_stack
!= NULL
; k
++)
9351 if (layouts
[k
].l_config
!= NULL
)
9352 layouts
[k
].l_config(ws
,
9353 SWM_ARG_ID_STACKINIT
);
9354 ws
->cur_layout
= &layouts
[0];
9355 ws
->cur_layout
->l_string(ws
);
9361 xcb_randr_select_input(conn
, screens
[i
].root
,
9362 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE
);
9369 if ((bar_fonts
= strdup(SWM_BAR_FONTS
)) == NULL
)
9370 err(1, "setup_globals: strdup: failed to allocate memory.");
9372 if ((clock_format
= strdup("%a %b %d %R %Z %Y")) == NULL
)
9373 err(1, "setup_globals: strdup: failed to allocate memory.");
9375 if ((syms
= xcb_key_symbols_alloc(conn
)) == NULL
)
9376 errx(1, "unable to allocate key symbols");
9378 a_state
= get_atom_from_string("WM_STATE");
9379 a_prot
= get_atom_from_string("WM_PROTOCOLS");
9380 a_delete
= get_atom_from_string("WM_DELETE_WINDOW");
9381 a_takefocus
= get_atom_from_string("WM_TAKE_FOCUS");
9382 a_wmname
= get_atom_from_string("WM_NAME");
9383 a_netwmname
= get_atom_from_string("_NET_WM_NAME");
9384 a_utf8_string
= get_atom_from_string("UTF8_STRING");
9385 a_string
= get_atom_from_string("STRING");
9386 a_swm_iconic
= get_atom_from_string("_SWM_ICONIC");
9387 a_swm_ws
= get_atom_from_string("_SWM_WS");
9394 xcb_atom_t netwmcheck
;
9395 xcb_window_t root
, win
;
9397 /* work around sun jdk bugs, code from wmname */
9398 netwmcheck
= get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
9400 num_screens
= get_screen_count();
9401 for (i
= 0; i
< num_screens
; i
++) {
9402 root
= screens
[i
].root
;
9404 win
= xcb_generate_id(conn
);
9405 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, win
, root
,
9406 0, 0, 1, 1, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
9407 XCB_COPY_FROM_PARENT
, 0, NULL
);
9409 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, root
,
9410 netwmcheck
, XCB_ATOM_WINDOW
, 32, 1, &win
);
9411 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
9412 netwmcheck
, XCB_ATOM_WINDOW
, 32, 1, &win
);
9413 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
9414 a_netwmname
, a_utf8_string
, 8, strlen("LG3D"), "LG3D");
9419 shutdown_cleanup(void)
9423 /* disable alarm because the following code may not be interrupted */
9425 if (signal(SIGALRM
, SIG_IGN
) == SIG_ERR
)
9426 err(1, "can't disable alarm");
9435 num_screens
= get_screen_count();
9436 for (i
= 0; i
< num_screens
; ++i
) {
9437 if (screens
[i
].bar_gc
!= 0)
9438 xcb_free_gc(conn
, screens
[i
].bar_gc
);
9439 if (!bar_font_legacy
)
9440 XftColorFree(display
, DefaultVisual(display
, i
),
9441 DefaultColormap(display
, i
), &bar_font_color
);
9444 if (bar_font_legacy
)
9445 XFreeFontSet(display
, bar_fs
);
9447 XftFontClose(display
, bar_font
);
9450 xcb_key_symbols_free(syms
);
9452 xcb_disconnect(conn
);
9456 event_error(xcb_generic_error_t
*e
)
9460 DNPRINTF(SWM_D_EVENT
, "event_error: %s(%u) from %s(%u), sequence: %u, "
9461 "resource_id: %u, minor_code: %u\n",
9462 xcb_event_get_error_label(e
->error_code
), e
->error_code
,
9463 xcb_event_get_request_label(e
->major_code
), e
->major_code
,
9464 e
->sequence
, e
->resource_id
, e
->minor_code
);
9468 event_handle(xcb_generic_event_t
*evt
)
9470 uint8_t type
= XCB_EVENT_RESPONSE_TYPE(evt
);
9472 DNPRINTF(SWM_D_EVENT
, "XCB Event: %s(%d), seq %u\n",
9473 xcb_event_get_label(XCB_EVENT_RESPONSE_TYPE(evt
)),
9474 XCB_EVENT_RESPONSE_TYPE(evt
), evt
->sequence
);
9477 #define EVENT(type, callback) case type: callback((void *)evt); return
9478 EVENT(0, event_error
);
9479 EVENT(XCB_BUTTON_PRESS
, buttonpress
);
9480 /*EVENT(XCB_BUTTON_RELEASE, buttonpress);*/
9481 /*EVENT(XCB_CIRCULATE_NOTIFY, );*/
9482 /*EVENT(XCB_CIRCULATE_REQUEST, );*/
9483 EVENT(XCB_CLIENT_MESSAGE
, clientmessage
);
9484 /*EVENT(XCB_COLORMAP_NOTIFY, );*/
9485 EVENT(XCB_CONFIGURE_NOTIFY
, configurenotify
);
9486 EVENT(XCB_CONFIGURE_REQUEST
, configurerequest
);
9487 /*EVENT(XCB_CREATE_NOTIFY, );*/
9488 EVENT(XCB_DESTROY_NOTIFY
, destroynotify
);
9489 EVENT(XCB_ENTER_NOTIFY
, enternotify
);
9490 EVENT(XCB_EXPOSE
, expose
);
9492 EVENT(XCB_FOCUS_IN
, focusin
);
9493 EVENT(XCB_FOCUS_OUT
, focusout
);
9495 /*EVENT(XCB_GRAPHICS_EXPOSURE, );*/
9496 /*EVENT(XCB_GRAVITY_NOTIFY, );*/
9497 EVENT(XCB_KEY_PRESS
, keypress
);
9498 /*EVENT(XCB_KEY_RELEASE, keypress);*/
9499 /*EVENT(XCB_KEYMAP_NOTIFY, );*/
9501 EVENT(XCB_LEAVE_NOTIFY
, leavenotify
);
9503 EVENT(XCB_MAP_NOTIFY
, mapnotify
);
9504 EVENT(XCB_MAP_REQUEST
, maprequest
);
9505 EVENT(XCB_MAPPING_NOTIFY
, mappingnotify
);
9506 EVENT(XCB_MOTION_NOTIFY
, motionnotify
);
9507 /*EVENT(XCB_NO_EXPOSURE, );*/
9508 EVENT(XCB_PROPERTY_NOTIFY
, propertynotify
);
9509 /*EVENT(XCB_REPARENT_NOTIFY, );*/
9510 /*EVENT(XCB_RESIZE_REQUEST, );*/
9511 /*EVENT(XCB_SELECTION_CLEAR, );*/
9512 /*EVENT(XCB_SELECTION_NOTIFY, );*/
9513 /*EVENT(XCB_SELECTION_REQUEST, );*/
9514 EVENT(XCB_UNMAP_NOTIFY
, unmapnotify
);
9515 /*EVENT(XCB_VISIBILITY_NOTIFY, visibilitynotify);*/
9518 if (type
- xrandr_eventbase
== XCB_RANDR_SCREEN_CHANGE_NOTIFY
)
9519 screenchange((void *)evt
);
9523 main(int argc
, char *argv
[])
9525 struct swm_region
*r
;
9526 char conf
[PATH_MAX
], *cfile
= NULL
;
9528 int xfd
, i
, num_screens
, startup
= 1;
9529 struct sigaction sact
;
9530 xcb_generic_event_t
*evt
;
9534 int stdin_ready
= 0;
9537 /* suppress unused warning since var is needed */
9540 time_started
= time(NULL
);
9543 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION
, buildstr
);
9544 if (!setlocale(LC_CTYPE
, "") || !setlocale(LC_TIME
, ""))
9545 warnx("no locale support");
9547 /* handle some signals */
9548 bzero(&sact
, sizeof(sact
));
9549 sigemptyset(&sact
.sa_mask
);
9551 sact
.sa_handler
= sighdlr
;
9552 sigaction(SIGINT
, &sact
, NULL
);
9553 sigaction(SIGQUIT
, &sact
, NULL
);
9554 sigaction(SIGTERM
, &sact
, NULL
);
9555 sigaction(SIGHUP
, &sact
, NULL
);
9557 sact
.sa_handler
= sighdlr
;
9558 sact
.sa_flags
= SA_NOCLDSTOP
;
9559 sigaction(SIGCHLD
, &sact
, NULL
);
9561 if (!(display
= XOpenDisplay(0)))
9562 errx(1, "can not open display");
9564 conn
= XGetXCBConnection(display
);
9565 if (xcb_connection_has_error(conn
))
9566 errx(1, "can not get XCB connection");
9568 XSetEventQueueOwner(display
, XCBOwnsEventQueue
);
9570 xcb_prefetch_extension_data(conn
, &xcb_randr_id
);
9571 xfd
= xcb_get_file_descriptor(conn
);
9573 /* look for local and global conf file */
9574 pwd
= getpwuid(getuid());
9576 errx(1, "invalid user: %d", getuid());
9578 xcb_grab_server(conn
);
9581 /* flush all events */
9582 while ((evt
= xcb_poll_for_event(conn
))) {
9583 if (XCB_EVENT_RESPONSE_TYPE(evt
) == 0)
9588 if (enable_wm() != 0)
9589 errx(1, "another window manager is currently running");
9591 /* Load Xcursors and/or cursorfont glyph cursors. */
9603 for (i
= 0; ; i
++) {
9608 snprintf(conf
, sizeof conf
, "%s/.%s",
9609 pwd
->pw_dir
, SWM_CONF_FILE
);
9613 snprintf(conf
, sizeof conf
, "/etc/%s",
9618 snprintf(conf
, sizeof conf
, "%s/.%s",
9619 pwd
->pw_dir
, SWM_CONF_FILE_OLD
);
9623 snprintf(conf
, sizeof conf
, "/etc/%s",
9630 if (strlen(conf
) && stat(conf
, &sb
) != -1)
9631 if (S_ISREG(sb
.st_mode
)) {
9638 /* load conf (if any) */
9640 conf_load(cfile
, SWM_CONF_DEFAULT
);
9645 /* set some values to work around bad programs */
9647 /* grab existing windows (before we build the bars) */
9650 if (getenv("SWM_STARTED") == NULL
)
9651 setenv("SWM_STARTED", "YES", 1);
9653 /* setup all bars */
9654 num_screens
= get_screen_count();
9655 for (i
= 0; i
< num_screens
; i
++)
9656 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
9663 xcb_ungrab_server(conn
);
9666 rd_max
= xfd
> STDIN_FILENO
? xfd
: STDIN_FILENO
;
9669 while ((evt
= xcb_poll_for_event(conn
))) {
9676 /* If just (re)started, set default focus if needed. */
9680 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
9681 r
= TAILQ_FIRST(&screens
[0].rl
);
9693 FD_SET(STDIN_FILENO
, &rd
);
9698 num_readable
= select(rd_max
+ 1, &rd
, NULL
, NULL
, &tv
);
9699 if (num_readable
== -1 && errno
!= EINTR
) {
9700 DNPRINTF(SWM_D_MISC
, "select failed");
9701 } else if (num_readable
> 0 && FD_ISSET(STDIN_FILENO
, &rd
)) {
9706 restart(NULL
, NULL
);