2 * Copyright (c) 2009-2012 Marco Peereboom <marco@peereboom.us>
3 * Copyright (c) 2009-2011 Ryan McBride <mcbride@countersiege.com>
4 * Copyright (c) 2009 Darrin Chandler <dwchandler@stilyagin.com>
5 * Copyright (c) 2009 Pierre-Yves Ritschard <pyr@spootnik.org>
6 * Copyright (c) 2010 Tuukka Kataja <stuge@xor.fi>
7 * Copyright (c) 2011 Jason L. Wright <jason@thought.net>
8 * Copyright (c) 2011-2012 Reginald Kennedy <rk@rejii.com>
9 * Copyright (c) 2011-2012 Lawrence Teo <lteo@lteo.net>
10 * Copyright (c) 2011-2012 Tiago Cunha <tcunha@gmx.com>
11 * Copyright (c) 2012 David Hill <dhill@mindcry.org>
13 * Permission to use, copy, modify, and distribute this software for any
14 * purpose with or without fee is hereby granted, provided that the above
15 * copyright notice and this permission notice appear in all copies.
17 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
18 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
20 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
21 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
22 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
23 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 * Much code and ideas taken from dwm under the following license:
27 * MIT/X Consortium License
29 * 2006-2008 Anselm R Garbe <garbeam at gmail dot com>
30 * 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
31 * 2006-2007 Jukka Salmi <jukka at salmi dot ch>
32 * 2007 Premysl Hruby <dfenze at gmail dot com>
33 * 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
34 * 2007 Christof Musik <christof at sendfax dot de>
35 * 2007-2008 Enno Gottox Boland <gottox at s01 dot de>
36 * 2007-2008 Peter Hartlich <sgkkr at hartlich dot com>
37 * 2008 Martin Hurton <martin dot hurton at gmail dot com>
39 * Permission is hereby granted, free of charge, to any person obtaining a
40 * copy of this software and associated documentation files (the "Software"),
41 * to deal in the Software without restriction, including without limitation
42 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
43 * and/or sell copies of the Software, and to permit persons to whom the
44 * Software is furnished to do so, subject to the following conditions:
46 * The above copyright notice and this permission notice shall be included in
47 * all copies or substantial portions of the Software.
49 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
50 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
51 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
52 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
53 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
54 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
55 * DEALINGS IN THE SOFTWARE.
59 #include <sys/types.h>
63 #include <sys/queue.h>
64 #include <sys/param.h>
65 #include <sys/select.h>
66 #if defined(__linux__)
68 #elif defined(__OpenBSD__)
70 #elif defined(__FreeBSD__)
91 #include <X11/cursorfont.h>
92 #include <X11/extensions/Xrandr.h>
93 #include <X11/Xcursor/Xcursor.h>
94 #include <X11/Xft/Xft.h>
95 #include <X11/Xlib-xcb.h>
96 #include <xcb/xcb_atom.h>
97 #include <xcb/xcb_aux.h>
98 #include <xcb/xcb_event.h>
99 #include <xcb/xcb_icccm.h>
100 #include <xcb/xcb_keysyms.h>
101 #include <xcb/xtest.h>
102 #include <xcb/randr.h>
110 #ifdef SPECTRWM_BUILDSTR
111 static const char *buildstr
= SPECTRWM_BUILDSTR
;
113 static const char *buildstr
= SPECTRWM_VERSION
;
116 #if !defined(__CYGWIN__) /* cygwin chokes on xrandr stuff */
118 # error XRandR versions less than 1.0 are not supported
121 # if RANDR_MAJOR >= 1
122 # if RANDR_MINOR >= 2
123 # define SWM_XRR_HAS_CRTC
126 #endif /* __CYGWIN__ */
128 #ifndef XCB_ICCCM_NUM_WM_HINTS_ELEMENTS
129 #define XCB_ICCCM_SIZE_HINT_P_MIN_SIZE XCB_SIZE_HINT_P_MIN_SIZE
130 #define XCB_ICCCM_SIZE_HINT_P_MAX_SIZE XCB_SIZE_HINT_P_MAX_SIZE
131 #define XCB_ICCCM_SIZE_HINT_P_RESIZE_INC XCB_SIZE_HINT_P_RESIZE_INC
132 #define XCB_ICCCM_WM_HINT_INPUT XCB_WM_HINT_INPUT
133 #define XCB_ICCCM_WM_HINT_X_URGENCY XCB_WM_HINT_X_URGENCY
134 #define XCB_ICCCM_WM_STATE_ICONIC XCB_WM_STATE_ICONIC
135 #define XCB_ICCCM_WM_STATE_WITHDRAWN XCB_WM_STATE_WITHDRAWN
136 #define XCB_ICCCM_WM_STATE_NORMAL XCB_WM_STATE_NORMAL
137 #define xcb_icccm_get_text_property_reply_t xcb_get_text_property_reply_t
138 #define xcb_icccm_get_text_property_reply_wipe xcb_get_text_property_reply_wipe
139 #define xcb_icccm_get_wm_class xcb_get_wm_class
140 #define xcb_icccm_get_wm_class_reply xcb_get_wm_class_reply
141 #define xcb_icccm_get_wm_class_reply_t xcb_get_wm_class_reply_t
142 #define xcb_icccm_get_wm_class_reply_wipe xcb_get_wm_class_reply_wipe
143 #define xcb_icccm_get_wm_hints xcb_get_wm_hints
144 #define xcb_icccm_get_wm_hints_reply xcb_get_wm_hints_reply
145 #define xcb_icccm_get_wm_name xcb_get_wm_name
146 #define xcb_icccm_get_wm_name_reply xcb_get_wm_name_reply
147 #define xcb_icccm_get_wm_normal_hints xcb_get_wm_normal_hints
148 #define xcb_icccm_get_wm_normal_hints_reply xcb_get_wm_normal_hints_reply
149 #define xcb_icccm_get_wm_protocols xcb_get_wm_protocols
150 #define xcb_icccm_get_wm_protocols_reply xcb_get_wm_protocols_reply
151 #define xcb_icccm_get_wm_protocols_reply_t xcb_get_wm_protocols_reply_t
152 #define xcb_icccm_get_wm_protocols_reply_wipe xcb_get_wm_protocols_reply_wipe
153 #define xcb_icccm_get_wm_transient_for xcb_get_wm_transient_for
154 #define xcb_icccm_get_wm_transient_for_reply xcb_get_wm_transient_for_reply
155 #define xcb_icccm_wm_hints_t xcb_wm_hints_t
158 /*#define SWM_DEBUG*/
160 #define DPRINTF(x...) do { \
162 fprintf(stderr, x); \
164 #define DNPRINTF(n,x...) do { \
165 if (swm_debug & n) { \
166 fprintf(stderr, "%ld ", (long)(time(NULL) - time_started)); \
167 fprintf(stderr, x); \
170 #define SWM_D_MISC 0x0001
171 #define SWM_D_EVENT 0x0002
172 #define SWM_D_WS 0x0004
173 #define SWM_D_FOCUS 0x0008
174 #define SWM_D_MOVE 0x0010
175 #define SWM_D_STACK 0x0020
176 #define SWM_D_MOUSE 0x0040
177 #define SWM_D_PROP 0x0080
178 #define SWM_D_CLASS 0x0100
179 #define SWM_D_KEY 0x0200
180 #define SWM_D_QUIRK 0x0400
181 #define SWM_D_SPAWN 0x0800
182 #define SWM_D_EVENTQ 0x1000
183 #define SWM_D_CONF 0x2000
184 #define SWM_D_BAR 0x4000
185 #define SWM_D_INIT 0x8000
187 u_int32_t swm_debug
= 0
206 #define DPRINTF(x...)
207 #define DNPRINTF(n,x...)
210 /* convert 8-bit to 16-bit */
211 #define RGB_8_TO_16(col) ((col) << 8) + (col)
213 #define PIXEL_TO_XRENDERCOLOR(px, xrc) \
214 xrc.red = RGB_8_TO_16((px) >> 16 & 0xff); \
215 xrc.green = RGB_8_TO_16((px) >> 8 & 0xff); \
216 xrc.blue = RGB_8_TO_16((px) & 0xff); \
219 #define LENGTH(x) (int)(sizeof (x) / sizeof (x)[0])
220 #define MODKEY XCB_MOD_MASK_1
221 #define CLEANMASK(mask) ((mask) & ~(numlockmask | XCB_MOD_MASK_LOCK))
222 #define BUTTONMASK (XCB_EVENT_MASK_BUTTON_PRESS | \
223 XCB_EVENT_MASK_BUTTON_RELEASE)
224 #define MOUSEMASK (BUTTONMASK|XCB_EVENT_MASK_POINTER_MOTION)
225 #define SWM_PROPLEN (16)
226 #define SWM_FUNCNAME_LEN (32)
227 #define SWM_KEYS_LEN (255)
228 #define SWM_QUIRK_LEN (64)
229 #define X(r) (r)->g.x
230 #define Y(r) (r)->g.y
231 #define WIDTH(r) (r)->g.w
232 #define HEIGHT(r) (r)->g.h
233 #define BORDER(w) ((w)->bordered ? border_width : 0)
234 #define MAX_X(r) ((r)->g.x + (r)->g.w)
235 #define MAX_Y(r) ((r)->g.y + (r)->g.h)
236 #define SH_MIN(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE
237 #define SH_MIN_W(w) (w)->sh.min_width
238 #define SH_MIN_H(w) (w)->sh.min_height
239 #define SH_MAX(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MAX_SIZE
240 #define SH_MAX_W(w) (w)->sh.max_width
241 #define SH_MAX_H(w) (w)->sh.max_height
242 #define SH_INC(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_RESIZE_INC
243 #define SH_INC_W(w) (w)->sh.width_inc
244 #define SH_INC_H(w) (w)->sh.height_inc
245 #define SWM_MAX_FONT_STEPS (3)
246 #define WINID(w) ((w) ? (w)->id : 0)
247 #define YESNO(x) ((x) ? "yes" : "no")
249 #define SWM_FOCUS_DEFAULT (0)
250 #define SWM_FOCUS_FOLLOW (1)
251 #define SWM_FOCUS_MANUAL (2)
253 #define SWM_CK_NONE 0
254 #define SWM_CK_ALL 0xf
255 #define SWM_CK_FOCUS 0x1
256 #define SWM_CK_POINTER 0x2
257 #define SWM_CK_FALLBACK 0x4
258 #define SWM_CK_REGION 0x8
260 #define SWM_CONF_DEFAULT (0)
261 #define SWM_CONF_KEYMAPPING (1)
264 #define SWM_LIB "/usr/local/lib/libswmhack.so"
271 xcb_atom_t a_takefocus
;
273 xcb_atom_t a_netwmname
;
274 xcb_atom_t a_utf8_string
;
276 xcb_atom_t a_swm_iconic
;
278 volatile sig_atomic_t running
= 1;
279 volatile sig_atomic_t restart_wm
= 0;
280 xcb_timestamp_t last_event_time
= 0;
284 int xrandr_eventbase
;
285 unsigned int numlockmask
= 0;
288 xcb_connection_t
*conn
;
289 xcb_key_symbols_t
*syms
;
292 int cycle_visible
= 0;
294 int font_adjusted
= 0;
295 unsigned int mod_key
= MODKEY
;
298 struct swm_region
*search_r
;
299 int select_list_pipe
[2];
300 int select_resp_pipe
[2];
302 volatile sig_atomic_t search_resp
;
303 int search_resp_action
;
305 struct search_window
{
306 TAILQ_ENTRY(search_window
) entry
;
310 xcb_window_t indicator
;
312 TAILQ_HEAD(search_winlist
, search_window
);
313 struct search_winlist search_wl
;
318 SWM_SEARCH_UNICONIFY
,
319 SWM_SEARCH_NAME_WORKSPACE
,
320 SWM_SEARCH_SEARCH_WORKSPACE
,
321 SWM_SEARCH_SEARCH_WINDOW
324 #define SWM_STACK_TOP (0)
325 #define SWM_STACK_BOTTOM (1)
326 #define SWM_STACK_ABOVE (2)
327 #define SWM_STACK_BELOW (3)
330 double dialog_ratio
= 0.6;
332 #define SWM_BAR_MAX (256)
333 #define SWM_BAR_JUSTIFY_LEFT (0)
334 #define SWM_BAR_JUSTIFY_CENTER (1)
335 #define SWM_BAR_JUSTIFY_RIGHT (2)
336 #define SWM_BAR_OFFSET (4)
337 #define SWM_BAR_FONTS "-*-terminus-medium-*-*-*-12-*-*-*-*-*-*-*," \
338 "-*-profont-*-*-*-*-12-*-*-*-*-*-*-*," \
339 "-*-times-medium-r-*-*-12-*-*-*-*-*-*-*," \
340 "-misc-fixed-medium-r-*-*-12-*-*-*-*-*-*-*," \
341 "-*-*-*-r-*-*-*-*-*-*-*-*-*-*"
343 #ifdef X_HAVE_UTF8_STRING
344 #define DRAWSTRING(x...) Xutf8DrawString(x)
346 #define DRAWSTRING(x...) XmbDrawString(x)
349 char *bar_argv
[] = { NULL
, NULL
};
351 char bar_ext
[SWM_BAR_MAX
];
352 char bar_ext_buf
[SWM_BAR_MAX
];
353 char bar_vertext
[SWM_BAR_MAX
];
356 int bar_border_width
= 1;
357 int bar_at_bottom
= 0;
361 int bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
362 char *bar_format
= NULL
;
363 int stack_enabled
= 1;
364 int clock_enabled
= 1;
365 int urgent_enabled
= 0;
366 char *clock_format
= NULL
;
367 int title_name_enabled
= 0;
368 int title_class_enabled
= 0;
369 int window_name_enabled
= 0;
370 int focus_mode
= SWM_FOCUS_DEFAULT
;
371 int focus_close
= SWM_STACK_BELOW
;
372 int focus_close_wrap
= 1;
373 int focus_default
= SWM_STACK_TOP
;
374 int spawn_position
= SWM_STACK_TOP
;
375 int disable_border
= 0;
376 int border_width
= 1;
377 int region_padding
= 0;
379 int verbose_layout
= 0;
383 XFontSetExtents
*bar_fs_extents
;
385 int bar_font_legacy
= 1;
387 XftColor bar_font_color
;
389 char *startup_exception
;
390 unsigned int nr_exceptions
= 0;
392 /* layout manager data */
393 struct swm_geometry
{
406 struct swm_geometry g
;
409 /* virtual "screens" */
411 TAILQ_ENTRY(swm_region
) entry
;
413 struct swm_geometry g
;
414 struct workspace
*ws
; /* current workspace on this region */
415 struct workspace
*ws_prior
; /* prior workspace on this region */
416 struct swm_screen
*s
; /* screen idx */
419 TAILQ_HEAD(swm_region_list
, swm_region
);
422 TAILQ_ENTRY(ws_win
) entry
;
424 xcb_window_t transient
;
425 struct ws_win
*focus_child
; /* focus on child transient */
426 struct swm_geometry g
; /* current geometry */
427 struct swm_geometry g_float
; /* region coordinates */
428 int g_floatvalid
; /* g_float geometry validity */
429 int floatmaxed
; /* whether maxed by max_stack */
435 unsigned int ewmh_flags
;
436 int font_size_boundary
[SWM_MAX_FONT_STEPS
];
442 unsigned long quirks
;
443 struct workspace
*ws
; /* always valid */
444 struct swm_screen
*s
; /* always valid, never changes */
445 xcb_get_geometry_reply_t
*wa
;
447 xcb_icccm_get_wm_class_reply_t ch
;
448 xcb_icccm_wm_hints_t hints
;
450 TAILQ_HEAD(ws_win_list
, ws_win
);
454 TAILQ_ENTRY(pid_e
) entry
;
458 TAILQ_HEAD(pid_list
, pid_e
);
459 struct pid_list pidlist
= TAILQ_HEAD_INITIALIZER(pidlist
);
461 /* layout handlers */
463 void vertical_config(struct workspace
*, int);
464 void vertical_stack(struct workspace
*, struct swm_geometry
*);
465 void horizontal_config(struct workspace
*, int);
466 void horizontal_stack(struct workspace
*, struct swm_geometry
*);
467 void max_stack(struct workspace
*, struct swm_geometry
*);
468 void plain_stacker(struct workspace
*);
469 void fancy_stacker(struct workspace
*);
472 void (*l_stack
)(struct workspace
*, struct swm_geometry
*);
473 void (*l_config
)(struct workspace
*, int);
475 #define SWM_L_FOCUSPREV (1<<0)
476 #define SWM_L_MAPONFOCUS (1<<1)
477 void (*l_string
)(struct workspace
*);
479 /* stack, configure */
480 { vertical_stack
, vertical_config
, 0, plain_stacker
},
481 { horizontal_stack
, horizontal_config
, 0, plain_stacker
},
483 SWM_L_MAPONFOCUS
| SWM_L_FOCUSPREV
, plain_stacker
},
484 { NULL
, NULL
, 0, NULL
},
487 /* position of max_stack mode in the layouts array, index into layouts! */
488 #define SWM_V_STACK (0)
489 #define SWM_H_STACK (1)
490 #define SWM_MAX_STACK (2)
492 #define SWM_H_SLICE (32)
493 #define SWM_V_SLICE (32)
495 /* define work spaces */
497 int idx
; /* workspace index */
498 char *name
; /* workspace name */
499 int always_raise
; /* raise windows on focus */
500 int bar_enabled
; /* bar visibility */
501 struct layout
*cur_layout
; /* current layout handlers */
502 struct ws_win
*focus
; /* may be NULL */
503 struct ws_win
*focus_prev
; /* may be NULL */
504 struct ws_win
*focus_pending
; /* may be NULL */
505 struct swm_region
*r
; /* may be NULL */
506 struct swm_region
*old_r
; /* may be NULL */
507 struct ws_win_list winlist
; /* list of windows in ws */
508 struct ws_win_list unmanagedlist
; /* list of dead windows in ws */
509 char stacker
[10]; /* display stacker and layout */
513 int horizontal_msize
;
515 int horizontal_stacks
;
526 SWM_S_COLOR_BAR_BORDER
,
527 SWM_S_COLOR_BAR_BORDER_UNFOCUS
,
528 SWM_S_COLOR_BAR_FONT
,
534 /* physical screen mapping */
535 #define SWM_WS_MAX (22) /* hard limit */
536 int workspace_limit
= 10; /* soft limit */
539 int idx
; /* screen index */
540 struct swm_region_list rl
; /* list of regions on this screen */
541 struct swm_region_list orl
; /* list of old regions */
543 struct workspace ws
[SWM_WS_MAX
];
544 struct swm_region
*r_focus
;
550 } c
[SWM_S_COLOR_MAX
];
552 xcb_gcontext_t bar_gc
;
555 struct swm_screen
*screens
;
557 /* args to functions */
560 #define SWM_ARG_ID_FOCUSNEXT (0)
561 #define SWM_ARG_ID_FOCUSPREV (1)
562 #define SWM_ARG_ID_FOCUSMAIN (2)
563 #define SWM_ARG_ID_SWAPNEXT (10)
564 #define SWM_ARG_ID_SWAPPREV (11)
565 #define SWM_ARG_ID_SWAPMAIN (12)
566 #define SWM_ARG_ID_MOVELAST (13)
567 #define SWM_ARG_ID_MASTERSHRINK (20)
568 #define SWM_ARG_ID_MASTERGROW (21)
569 #define SWM_ARG_ID_MASTERADD (22)
570 #define SWM_ARG_ID_MASTERDEL (23)
571 #define SWM_ARG_ID_FLIPLAYOUT (24)
572 #define SWM_ARG_ID_STACKRESET (30)
573 #define SWM_ARG_ID_STACKINIT (31)
574 #define SWM_ARG_ID_CYCLEWS_UP (40)
575 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
576 #define SWM_ARG_ID_CYCLERG_UP (42)
577 #define SWM_ARG_ID_CYCLERG_DOWN (43)
578 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
579 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
580 #define SWM_ARG_ID_STACKINC (50)
581 #define SWM_ARG_ID_STACKDEC (51)
582 #define SWM_ARG_ID_SS_ALL (60)
583 #define SWM_ARG_ID_SS_WINDOW (61)
584 #define SWM_ARG_ID_DONTCENTER (70)
585 #define SWM_ARG_ID_CENTER (71)
586 #define SWM_ARG_ID_KILLWINDOW (80)
587 #define SWM_ARG_ID_DELETEWINDOW (81)
588 #define SWM_ARG_ID_WIDTHGROW (90)
589 #define SWM_ARG_ID_WIDTHSHRINK (91)
590 #define SWM_ARG_ID_HEIGHTGROW (92)
591 #define SWM_ARG_ID_HEIGHTSHRINK (93)
592 #define SWM_ARG_ID_MOVEUP (100)
593 #define SWM_ARG_ID_MOVEDOWN (101)
594 #define SWM_ARG_ID_MOVELEFT (102)
595 #define SWM_ARG_ID_MOVERIGHT (103)
596 #define SWM_ARG_ID_BAR_TOGGLE (110)
597 #define SWM_ARG_ID_BAR_TOGGLE_WS (111)
603 TAILQ_ENTRY(quirk
) entry
;
607 #define SWM_Q_FLOAT (1<<0) /* float this window */
608 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
609 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
610 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
611 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
612 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
613 #define SWM_Q_NOFOCUSONMAP (1<<6) /* Don't focus on window when mapped. */
614 #define SWM_Q_FOCUSONMAP_SINGLE (1<<7) /* Only focus if single win of type. */
616 TAILQ_HEAD(quirk_list
, quirk
);
617 struct quirk_list quirks
= TAILQ_HEAD_INITIALIZER(quirks
);
620 * Supported EWMH hints should be added to
621 * both the enum and the ewmh array
626 _NET_MOVERESIZE_WINDOW
,
627 _NET_WM_ACTION_ABOVE
,
628 _NET_WM_ACTION_CLOSE
,
629 _NET_WM_ACTION_FULLSCREEN
,
631 _NET_WM_ACTION_RESIZE
,
632 _NET_WM_ALLOWED_ACTIONS
,
636 _NET_WM_STATE_FULLSCREEN
,
637 _NET_WM_STATE_HIDDEN
,
638 _NET_WM_STATE_MAXIMIZED_HORZ
,
639 _NET_WM_STATE_MAXIMIZED_VERT
,
640 _NET_WM_STATE_SKIP_PAGER
,
641 _NET_WM_STATE_SKIP_TASKBAR
,
643 _NET_WM_WINDOW_TYPE_DIALOG
,
644 _NET_WM_WINDOW_TYPE_DOCK
,
645 _NET_WM_WINDOW_TYPE_NORMAL
,
646 _NET_WM_WINDOW_TYPE_SPLASH
,
647 _NET_WM_WINDOW_TYPE_TOOLBAR
,
648 _NET_WM_WINDOW_TYPE_UTILITY
,
649 _SWM_WM_STATE_MANUAL
,
656 } ewmh
[SWM_EWMH_HINT_MAX
] = {
657 /* must be in same order as in the enum */
658 {"_NET_ACTIVE_WINDOW", XCB_ATOM_NONE
},
659 {"_NET_CLOSE_WINDOW", XCB_ATOM_NONE
},
660 {"_NET_MOVERESIZE_WINDOW", XCB_ATOM_NONE
},
661 {"_NET_WM_ACTION_ABOVE", XCB_ATOM_NONE
},
662 {"_NET_WM_ACTION_CLOSE", XCB_ATOM_NONE
},
663 {"_NET_WM_ACTION_FULLSCREEN", XCB_ATOM_NONE
},
664 {"_NET_WM_ACTION_MOVE", XCB_ATOM_NONE
},
665 {"_NET_WM_ACTION_RESIZE", XCB_ATOM_NONE
},
666 {"_NET_WM_ALLOWED_ACTIONS", XCB_ATOM_NONE
},
667 {"_NET_WM_NAME", XCB_ATOM_NONE
},
668 {"_NET_WM_STATE", XCB_ATOM_NONE
},
669 {"_NET_WM_STATE_ABOVE", XCB_ATOM_NONE
},
670 {"_NET_WM_STATE_FULLSCREEN", XCB_ATOM_NONE
},
671 {"_NET_WM_STATE_HIDDEN", XCB_ATOM_NONE
},
672 {"_NET_WM_STATE_MAXIMIZED_HORZ", XCB_ATOM_NONE
},
673 {"_NET_WM_STATE_MAXIMIZED_VERT", XCB_ATOM_NONE
},
674 {"_NET_WM_STATE_SKIP_PAGER", XCB_ATOM_NONE
},
675 {"_NET_WM_STATE_SKIP_TASKBAR", XCB_ATOM_NONE
},
676 {"_NET_WM_WINDOW_TYPE", XCB_ATOM_NONE
},
677 {"_NET_WM_WINDOW_TYPE_DIALOG", XCB_ATOM_NONE
},
678 {"_NET_WM_WINDOW_TYPE_DOCK", XCB_ATOM_NONE
},
679 {"_NET_WM_WINDOW_TYPE_NORMAL", XCB_ATOM_NONE
},
680 {"_NET_WM_WINDOW_TYPE_SPLASH", XCB_ATOM_NONE
},
681 {"_NET_WM_WINDOW_TYPE_TOOLBAR", XCB_ATOM_NONE
},
682 {"_NET_WM_WINDOW_TYPE_UTILITY", XCB_ATOM_NONE
},
683 {"_SWM_WM_STATE_MANUAL", XCB_ATOM_NONE
},
690 XC_BOTTOM_LEFT_CORNER
,
691 XC_BOTTOM_RIGHT_CORNER
,
699 char *name
; /* Name used by Xcursor .*/
700 uint8_t cf_char
; /* cursorfont index. */
702 } cursors
[XC_MAX
] = {
703 {"fleur", XC_fleur
, XCB_CURSOR_NONE
},
704 {"left_ptr", XC_left_ptr
, XCB_CURSOR_NONE
},
705 {"bottom_left_corner", XC_bottom_left_corner
, XCB_CURSOR_NONE
},
706 {"bottom_right_corner", XC_bottom_right_corner
, XCB_CURSOR_NONE
},
707 {"sizing", XC_sizing
, XCB_CURSOR_NONE
},
708 {"top_left_corner", XC_top_left_corner
, XCB_CURSOR_NONE
},
709 {"top_right_corner", XC_top_right_corner
, XCB_CURSOR_NONE
},
712 #define SWM_SPAWN_OPTIONAL 0x1
716 TAILQ_ENTRY(spawn_prog
) entry
;
722 TAILQ_HEAD(spawn_list
, spawn_prog
);
723 struct spawn_list spawns
= TAILQ_HEAD_INITIALIZER(spawns
);
725 /* user/key callable function IDs */
837 KF_DUMPWINS
, /* MUST BE LAST */
845 enum keyfuncid funcid
;
848 RB_HEAD(key_tree
, key
);
850 /* function prototypes */
851 void adjust_font(struct ws_win
*);
852 void bar_class_name(char *, size_t, struct swm_region
*);
853 void bar_class_title_name(char *, size_t, struct swm_region
*);
854 void bar_cleanup(struct swm_region
*);
855 void bar_extra_setup(void);
856 void bar_extra_stop(void);
857 int bar_extra_update(void);
858 void bar_fmt(const char *, char *, struct swm_region
*, size_t);
859 void bar_fmt_expand(char *, size_t);
861 void bar_print(struct swm_region
*, const char *);
862 void bar_print_legacy(struct swm_region
*, const char *);
863 void bar_replace(char *, char *, struct swm_region
*, size_t);
864 void bar_replace_pad(char *, int *, size_t);
865 char *bar_replace_seq(char *, char *, struct swm_region
*, size_t *, size_t);
866 void bar_setup(struct swm_region
*);
867 void bar_title_name(char *, size_t, struct swm_region
*);
868 void bar_toggle(struct swm_region
*, union arg
*);
869 void bar_urgent(char *, size_t);
870 void bar_window_float(char *, size_t, struct swm_region
*);
871 void bar_window_name(char *, size_t, struct swm_region
*);
872 void bar_workspace_name(char *, size_t, struct swm_region
*);
873 void buttonpress(xcb_button_press_event_t
*);
874 void check_conn(void);
875 void clear_keys(void);
876 void clientmessage(xcb_client_message_event_t
*);
877 void client_msg(struct ws_win
*, xcb_atom_t
, xcb_timestamp_t
);
878 int conf_load(const char *, int);
879 void configurenotify(xcb_configure_notify_event_t
*);
880 void configurerequest(xcb_configure_request_event_t
*);
881 void config_win(struct ws_win
*, xcb_configure_request_event_t
*);
882 void constrain_window(struct ws_win
*, struct swm_region
*, int);
883 int count_win(struct workspace
*, int);
884 void cursors_cleanup(void);
885 void cursors_load(void);
886 void custom_region(char *);
887 void cyclerg(struct swm_region
*, union arg
*);
888 void cyclews(struct swm_region
*, union arg
*);
889 void cycle_layout(struct swm_region
*, union arg
*);
890 void destroynotify(xcb_destroy_notify_event_t
*);
891 void dumpwins(struct swm_region
*, union arg
*);
893 void enternotify(xcb_enter_notify_event_t
*);
894 void event_drain(uint8_t);
895 void event_error(xcb_generic_error_t
*);
896 void event_handle(xcb_generic_event_t
*);
897 void ewmh_autoquirk(struct ws_win
*);
898 void ewmh_get_win_state(struct ws_win
*);
899 int ewmh_set_win_fullscreen(struct ws_win
*, int);
900 void ewmh_update_actions(struct ws_win
*);
901 void ewmh_update_win_state(struct ws_win
*, xcb_atom_t
, long);
902 char *expand_tilde(const char *);
903 void expose(xcb_expose_event_t
*);
904 void fake_keypress(struct ws_win
*, xcb_keysym_t
, uint16_t);
905 struct pid_e
*find_pid(pid_t
);
906 struct ws_win
*find_unmanaged_window(xcb_window_t
);
907 struct ws_win
*find_window(xcb_window_t
);
908 void floating_toggle(struct swm_region
*, union arg
*);
909 int floating_toggle_win(struct ws_win
*);
910 void focus(struct swm_region
*, union arg
*);
912 void focusin(xcb_focus_in_event_t
*);
913 void focusout(xcb_focus_out_event_t
*);
915 void focus_flush(void);
916 void focus_region(struct swm_region
*);
917 void focusrg(struct swm_region
*, union arg
*);
918 void focus_win(struct ws_win
*);
919 void fontset_init(void);
920 void free_window(struct ws_win
*);
921 xcb_atom_t
get_atom_from_string(const char *);
923 char *get_atom_name(xcb_atom_t
);
925 struct ws_win
*get_focus_magic(struct ws_win
*);
926 struct ws_win
*get_focus_prev(struct ws_win
*);
928 char *get_notify_detail_label(uint8_t);
929 char *get_notify_mode_label(uint8_t);
931 struct ws_win
*get_pointer_win(xcb_window_t
);
932 struct ws_win
*get_region_focus(struct swm_region
*);
933 int get_region_index(struct swm_region
*);
934 xcb_screen_t
*get_screen(int);
935 int get_screen_count(void);
937 char *get_stack_mode_name(uint8_t);
939 int32_t get_swm_iconic(struct ws_win
*);
940 char *get_win_name(xcb_window_t
);
941 int get_ws_idx(xcb_window_t
);
942 uint32_t getstate(xcb_window_t
);
943 void grabbuttons(struct ws_win
*);
945 void grab_windows(void);
946 void iconify(struct swm_region
*, union arg
*);
948 void keypress(xcb_key_press_event_t
*);
949 int key_cmp(struct key
*, struct key
*);
950 void key_insert(unsigned int, KeySym
, enum keyfuncid
, const char *);
951 struct key
*key_lookup(unsigned int, KeySym
);
952 void key_remove(struct key
*);
953 void key_replace(struct key
*, unsigned int, KeySym
, enum keyfuncid
,
955 void kill_bar_extra_atexit(void);
956 void kill_refs(struct ws_win
*);
958 void leavenotify(xcb_leave_notify_event_t
*);
960 void load_float_geom(struct ws_win
*, struct swm_region
*);
961 struct ws_win
*manage_window(xcb_window_t
, uint16_t);
962 void map_window(struct ws_win
*);
963 void mapnotify(xcb_map_notify_event_t
*);
964 void mappingnotify(xcb_mapping_notify_event_t
*);
965 void maprequest(xcb_map_request_event_t
*);
966 void motionnotify(xcb_motion_notify_event_t
*);
967 void move(struct ws_win
*, union arg
*);
968 void move_step(struct swm_region
*, union arg
*);
969 uint32_t name_to_pixel(int, const char *);
970 void name_workspace(struct swm_region
*, union arg
*);
971 void new_region(struct swm_screen
*, int, int, int, int);
972 int parsekeys(char *, unsigned int, unsigned int *, KeySym
*);
973 int parsequirks(char *, unsigned long *);
974 int parse_rgb(const char *, uint16_t *, uint16_t *, uint16_t *);
975 void pressbutton(struct swm_region
*, union arg
*);
976 void priorws(struct swm_region
*, union arg
*);
978 void print_win_geom(xcb_window_t
);
980 void propertynotify(xcb_property_notify_event_t
*);
981 void quirk_insert(const char *, const char *, unsigned long);
982 void quirk_remove(struct quirk
*);
983 void quirk_replace(struct quirk
*, const char *, const char *,
985 void quit(struct swm_region
*, union arg
*);
986 void raise_toggle(struct swm_region
*, union arg
*);
987 void resize(struct ws_win
*, union arg
*);
988 void resize_step(struct swm_region
*, union arg
*);
989 void restart(struct swm_region
*, union arg
*);
990 struct swm_region
*root_to_region(xcb_window_t
, int);
991 void screenchange(xcb_randr_screen_change_notify_event_t
*);
992 void scan_xrandr(int);
993 void search_do_resp(void);
994 void search_resp_name_workspace(const char *, unsigned long);
995 void search_resp_search_window(const char *);
996 void search_resp_search_workspace(const char *);
997 void search_resp_uniconify(const char *, unsigned long);
998 void search_win(struct swm_region
*, union arg
*);
999 void search_win_cleanup(void);
1000 void search_workspace(struct swm_region
*, union arg
*);
1001 void send_to_rg(struct swm_region
*, union arg
*);
1002 void send_to_ws(struct swm_region
*, union arg
*);
1003 void set_region(struct swm_region
*);
1004 int setautorun(char *, char *, int);
1005 int setconfbinding(char *, char *, int);
1006 int setconfcolor(char *, char *, int);
1007 int setconfmodkey(char *, char *, int);
1008 int setconfquirk(char *, char *, int);
1009 int setconfregion(char *, char *, int);
1010 int setconfspawn(char *, char *, int);
1011 int setconfvalue(char *, char *, int);
1012 void setkeybinding(unsigned int, KeySym
, enum keyfuncid
, const char *);
1013 int setkeymapping(char *, char *, int);
1014 int setlayout(char *, char *, int);
1015 void setquirk(const char *, const char *, unsigned long);
1016 void setscreencolor(char *, int, int);
1017 void setspawn(const char *, const char *, int);
1018 void setup_ewmh(void);
1019 void setup_globals(void);
1020 void setup_keys(void);
1021 void setup_quirks(void);
1022 void setup_screens(void);
1023 void setup_spawn(void);
1024 void set_child_transient(struct ws_win
*, xcb_window_t
*);
1025 void set_swm_iconic(struct ws_win
*, int);
1026 void set_win_state(struct ws_win
*, uint16_t);
1027 void shutdown_cleanup(void);
1029 void socket_setnonblock(int);
1030 void sort_windows(struct ws_win_list
*);
1031 void spawn(int, union arg
*, int);
1032 void spawn_custom(struct swm_region
*, union arg
*, const char *);
1033 int spawn_expand(struct swm_region
*, union arg
*, const char *, char ***);
1034 void spawn_insert(const char *, const char *, int);
1035 void spawn_remove(struct spawn_prog
*);
1036 void spawn_replace(struct spawn_prog
*, const char *, const char *, int);
1037 void spawn_select(struct swm_region
*, union arg
*, const char *, int *);
1038 void stack_config(struct swm_region
*, union arg
*);
1039 void stack_floater(struct ws_win
*, struct swm_region
*);
1040 void stack_master(struct workspace
*, struct swm_geometry
*, int, int);
1041 void store_float_geom(struct ws_win
*, struct swm_region
*);
1042 char *strdupsafe(const char *);
1043 void swapwin(struct swm_region
*, union arg
*);
1044 void switchws(struct swm_region
*, union arg
*);
1045 void teardown_ewmh(void);
1046 void unfocus_win(struct ws_win
*);
1047 void uniconify(struct swm_region
*, union arg
*);
1048 void unmanage_window(struct ws_win
*);
1049 void unmapnotify(xcb_unmap_notify_event_t
*);
1050 void unmap_all(void);
1051 void unmap_window(struct ws_win
*);
1052 void updatenumlockmask(void);
1053 void update_modkey(unsigned int);
1054 void update_window(struct ws_win
*);
1055 void validate_spawns(void);
1056 int validate_win(struct ws_win
*);
1057 int validate_ws(struct workspace
*);
1058 /*void visibilitynotify(xcb_visibility_notify_event_t *);*/
1059 void version(struct swm_region
*, union arg
*);
1060 pid_t
window_get_pid(xcb_window_t
);
1061 void wkill(struct swm_region
*, union arg
*);
1062 void workaround(void);
1063 void xft_init(struct swm_region
*);
1064 void _add_startup_exception(const char *, va_list);
1065 void add_startup_exception(const char *, ...);
1067 RB_PROTOTYPE(key_tree
, key
, entry
, key_cmp
);
1068 RB_GENERATE(key_tree
, key
, entry
, key_cmp
);
1069 struct key_tree keys
;
1074 xcb_font_t cf
= XCB_NONE
;
1077 for (i
= 0; i
< LENGTH(cursors
); ++i
) {
1078 /* try to load Xcursor first. */
1079 cursors
[i
].cid
= XcursorLibraryLoadCursor(display
,
1082 /* fallback to cursorfont. */
1083 if (cursors
[i
].cid
== XCB_CURSOR_NONE
) {
1084 if (cf
== XCB_NONE
) {
1085 cf
= xcb_generate_id(conn
);
1086 xcb_open_font(conn
, cf
, strlen("cursor"),
1090 cursors
[i
].cid
= xcb_generate_id(conn
);
1091 xcb_create_glyph_cursor(conn
, cursors
[i
].cid
, cf
, cf
,
1092 cursors
[i
].cf_char
, cursors
[i
].cf_char
+ 1, 0, 0, 0,
1093 0xffff, 0xffff, 0xffff);
1099 xcb_close_font(conn
, cf
);
1103 cursors_cleanup(void)
1106 for (i
= 0; i
< LENGTH(cursors
); ++i
)
1107 xcb_free_cursor(conn
, cursors
[i
].cid
);
1111 expand_tilde(const char *s
)
1113 struct passwd
*ppwd
;
1121 errx(1, "expand_tilde: NULL string.");
1124 result
= strdup(sc
);
1130 if ((max
= sysconf(_SC_LOGIN_NAME_MAX
)) == -1)
1131 errx(1, "expand_tilde: sysconf");
1133 if ((user
= calloc(1, max
+ 1)) == NULL
)
1134 errx(1, "expand_tilde: calloc");
1136 for (i
= 0; s
[i
] != '/' && s
[i
] != '\0'; ++i
)
1141 ppwd
= strlen(user
) == 0 ? getpwuid(getuid()) : getpwnam(user
);
1145 result
= strdup(sc
);
1147 if (asprintf(&result
, "%s%s", ppwd
->pw_dir
, s
) == -1)
1151 errx(1, "expand_tilde: failed to allocate memory.");
1157 parse_rgb(const char *rgb
, uint16_t *rr
, uint16_t *gg
, uint16_t *bb
)
1159 unsigned int tmpr
, tmpg
, tmpb
;
1161 if (sscanf(rgb
, "rgb:%x/%x/%x", &tmpr
, &tmpg
, &tmpb
) != 3)
1164 *rr
= RGB_8_TO_16(tmpr
);
1165 *gg
= RGB_8_TO_16(tmpg
);
1166 *bb
= RGB_8_TO_16(tmpb
);
1172 get_screen(int screen
)
1174 const xcb_setup_t
*r
;
1175 xcb_screen_iterator_t iter
;
1177 if ((r
= xcb_get_setup(conn
)) == NULL
) {
1178 DNPRINTF(SWM_D_MISC
, "get_screen: xcb_get_setup\n");
1182 iter
= xcb_setup_roots_iterator(r
);
1183 for (; iter
.rem
; --screen
, xcb_screen_next(&iter
))
1191 get_screen_count(void)
1193 const xcb_setup_t
*r
;
1195 if ((r
= xcb_get_setup(conn
)) == NULL
) {
1196 DNPRINTF(SWM_D_MISC
, "get_screen_count: xcb_get_setup\n");
1200 return xcb_setup_roots_length(r
);
1204 get_region_index(struct swm_region
*r
)
1206 struct swm_region
*rr
;
1212 TAILQ_FOREACH(rr
, &r
->s
->rl
, entry
) {
1227 if (focus_mode
== SWM_FOCUS_DEFAULT
)
1228 event_drain(XCB_ENTER_NOTIFY
);
1234 get_atom_from_string(const char *str
)
1236 xcb_intern_atom_cookie_t c
;
1237 xcb_intern_atom_reply_t
*r
;
1240 c
= xcb_intern_atom(conn
, 0, strlen(str
), str
);
1241 r
= xcb_intern_atom_reply(conn
, c
, NULL
);
1249 return (XCB_ATOM_NONE
);
1253 set_swm_iconic(struct ws_win
*win
, int newv
)
1260 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
1261 a_swm_iconic
, XCB_ATOM_INTEGER
, 32, 1, &v
);
1263 xcb_delete_property(conn
, win
->id
, a_swm_iconic
);
1267 get_swm_iconic(struct ws_win
*win
)
1270 xcb_get_property_reply_t
*pr
= NULL
;
1272 pr
= xcb_get_property_reply(conn
,
1273 xcb_get_property(conn
, 0, win
->id
, a_swm_iconic
,
1274 XCB_ATOM_INTEGER
, 0, 1), NULL
);
1277 if (pr
->type
!= XCB_ATOM_INTEGER
|| pr
->format
!= 32)
1279 v
= *((int32_t *)xcb_get_property_value(pr
));
1289 xcb_atom_t sup_list
;
1290 int i
, j
, num_screens
;
1292 sup_list
= get_atom_from_string("_NET_SUPPORTED");
1294 for (i
= 0; i
< LENGTH(ewmh
); i
++)
1295 ewmh
[i
].atom
= get_atom_from_string(ewmh
[i
].name
);
1297 num_screens
= get_screen_count();
1298 for (i
= 0; i
< num_screens
; i
++) {
1299 /* Support check window will be created by workaround(). */
1301 /* Report supported atoms */
1302 xcb_delete_property(conn
, screens
[i
].root
, sup_list
);
1303 for (j
= 0; j
< LENGTH(ewmh
); j
++)
1304 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
,
1305 screens
[i
].root
, sup_list
, XCB_ATOM_ATOM
, 32, 1,
1314 xcb_atom_t sup_check
, sup_list
;
1316 xcb_get_property_cookie_t pc
;
1317 xcb_get_property_reply_t
*pr
;
1319 sup_check
= get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
1320 sup_list
= get_atom_from_string("_NET_SUPPORTED");
1321 num_screens
= get_screen_count();
1323 for (i
= 0; i
< num_screens
; i
++) {
1324 /* Get the support check window and destroy it */
1325 pc
= xcb_get_property(conn
, 0, screens
[i
].root
, sup_check
,
1326 XCB_ATOM_WINDOW
, 0, 1);
1327 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
1330 if (pr
->format
== sup_check
) {
1331 id
= *((xcb_window_t
*)xcb_get_property_value(pr
));
1333 xcb_destroy_window(conn
, id
);
1334 xcb_delete_property(conn
, screens
[i
].root
, sup_check
);
1335 xcb_delete_property(conn
, screens
[i
].root
, sup_list
);
1342 ewmh_autoquirk(struct ws_win
*win
)
1346 xcb_get_property_cookie_t c
;
1347 xcb_get_property_reply_t
*r
;
1349 c
= xcb_get_property(conn
, 0, win
->id
,
1350 ewmh
[_NET_WM_WINDOW_TYPE
].atom
, XCB_ATOM_ATOM
, 0, UINT32_MAX
);
1351 r
= xcb_get_property_reply(conn
, c
, NULL
);
1355 n
= xcb_get_property_value_length(r
);
1356 type
= xcb_get_property_value(r
);
1358 for (i
= 0; i
< n
; i
++) {
1359 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_NORMAL
].atom
)
1361 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_DOCK
].atom
||
1362 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_TOOLBAR
].atom
||
1363 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_UTILITY
].atom
) {
1365 win
->quirks
= SWM_Q_FLOAT
| SWM_Q_ANYWHERE
;
1368 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_SPLASH
].atom
||
1369 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_DIALOG
].atom
) {
1371 win
->quirks
= SWM_Q_FLOAT
;
1378 #define SWM_EWMH_ACTION_COUNT_MAX (6)
1379 #define EWMH_F_FULLSCREEN (1<<0)
1380 #define EWMH_F_ABOVE (1<<1)
1381 #define EWMH_F_HIDDEN (1<<2)
1382 #define EWMH_F_SKIP_PAGER (1<<3)
1383 #define EWMH_F_SKIP_TASKBAR (1<<4)
1384 #define SWM_F_MANUAL (1<<5)
1387 ewmh_set_win_fullscreen(struct ws_win
*win
, int fs
)
1395 DNPRINTF(SWM_D_MISC
, "ewmh_set_win_fullscreen: window: 0x%x, "
1396 "fullscreen %s\n", win
->id
, YESNO(fs
));
1399 if (!win
->g_floatvalid
)
1400 store_float_geom(win
, win
->ws
->r
);
1402 win
->g
= win
->ws
->r
->g
;
1405 load_float_geom(win
, win
->ws
->r
);
1412 ewmh_update_actions(struct ws_win
*win
)
1414 xcb_atom_t actions
[SWM_EWMH_ACTION_COUNT_MAX
];
1420 actions
[n
++] = ewmh
[_NET_WM_ACTION_CLOSE
].atom
;
1422 if (win
->floating
) {
1423 actions
[n
++] = ewmh
[_NET_WM_ACTION_MOVE
].atom
;
1424 actions
[n
++] = ewmh
[_NET_WM_ACTION_RESIZE
].atom
;
1425 actions
[n
++] = ewmh
[_NET_WM_ACTION_ABOVE
].atom
;
1428 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
1429 ewmh
[_NET_WM_ALLOWED_ACTIONS
].atom
, XCB_ATOM_ATOM
, 32, 1, actions
);
1432 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
1433 #define _NET_WM_STATE_ADD 1 /* add/set property */
1434 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
1437 ewmh_update_win_state(struct ws_win
*win
, xcb_atom_t state
, long action
)
1439 unsigned int mask
= 0;
1440 unsigned int changed
= 0;
1441 unsigned int orig_flags
;
1446 DNPRINTF(SWM_D_PROP
, "ewmh_update_win_state: window: 0x%x, state: %ld, "
1447 "action: %ld\n", win
->id
, (unsigned long)state
, action
);
1449 if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
)
1450 mask
= EWMH_F_FULLSCREEN
;
1451 else if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
)
1452 mask
= EWMH_F_ABOVE
;
1453 else if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
)
1454 mask
= SWM_F_MANUAL
;
1455 else if (state
== ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
)
1456 mask
= EWMH_F_SKIP_PAGER
;
1457 else if (state
== ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
)
1458 mask
= EWMH_F_SKIP_TASKBAR
;
1460 orig_flags
= win
->ewmh_flags
;
1463 case _NET_WM_STATE_REMOVE
:
1464 win
->ewmh_flags
&= ~mask
;
1466 case _NET_WM_STATE_ADD
:
1467 win
->ewmh_flags
|= mask
;
1469 case _NET_WM_STATE_TOGGLE
:
1470 win
->ewmh_flags
^= mask
;
1474 changed
= (win
->ewmh_flags
& mask
) ^ (orig_flags
& mask
) ? 1 : 0;
1476 if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
) {
1477 if (changed
&& !floating_toggle_win(win
))
1478 win
->ewmh_flags
= orig_flags
; /* revert */
1479 } else if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
) {
1481 win
->manual
= (win
->ewmh_flags
& SWM_F_MANUAL
) != 0;
1482 } else if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
) {
1483 if (changed
&& !ewmh_set_win_fullscreen(win
,
1484 win
->ewmh_flags
& EWMH_F_FULLSCREEN
))
1485 win
->ewmh_flags
= orig_flags
; /* revert */
1488 xcb_delete_property(conn
, win
->id
, ewmh
[_NET_WM_STATE
].atom
);
1490 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
1491 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1492 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1493 &ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
);
1494 else if (win
->ewmh_flags
& EWMH_F_SKIP_PAGER
)
1495 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1496 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1497 &ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
);
1498 else if (win
->ewmh_flags
& EWMH_F_SKIP_TASKBAR
)
1499 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1500 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1501 &ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
);
1502 else if (win
->ewmh_flags
& EWMH_F_ABOVE
)
1503 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1504 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1505 &ewmh
[_NET_WM_STATE_ABOVE
].atom
);
1506 else if (win
->ewmh_flags
& SWM_F_MANUAL
)
1507 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1508 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1509 &ewmh
[_SWM_WM_STATE_MANUAL
].atom
);
1513 ewmh_get_win_state(struct ws_win
*win
)
1516 xcb_get_property_cookie_t c
;
1517 xcb_get_property_reply_t
*r
;
1523 win
->ewmh_flags
= 0;
1525 win
->ewmh_flags
|= EWMH_F_ABOVE
;
1527 win
->ewmh_flags
|= SWM_F_MANUAL
;
1529 c
= xcb_get_property(conn
, 0, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
1530 XCB_ATOM_ATOM
, 0, UINT32_MAX
);
1531 r
= xcb_get_property_reply(conn
, c
, NULL
);
1535 states
= xcb_get_property_value(r
);
1536 n
= xcb_get_property_value_length(r
);
1538 for (i
= 0; i
< n
; i
++)
1539 ewmh_update_win_state(win
, states
[i
], _NET_WM_STATE_ADD
);
1547 dumpwins(struct swm_region
*r
, union arg
*args
)
1551 xcb_get_window_attributes_cookie_t c
;
1552 xcb_get_window_attributes_reply_t
*wa
;
1554 /* suppress unused warning since var is needed */
1557 if (r
->ws
== NULL
) {
1558 warnx("dumpwins: invalid workspace");
1562 warnx("=== managed window list ws %02d ===", r
->ws
->idx
);
1563 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
1564 state
= getstate(win
->id
);
1565 c
= xcb_get_window_attributes(conn
, win
->id
);
1566 wa
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
1568 warnx("window: 0x%x, map_state: %d, state: %u, "
1569 "transient: 0x%x", win
->id
, wa
->map_state
,
1570 state
, win
->transient
);
1573 warnx("window: 0x%x, failed xcb_get_window_attributes",
1577 warnx("===== unmanaged window list =====");
1578 TAILQ_FOREACH(win
, &r
->ws
->unmanagedlist
, entry
) {
1579 state
= getstate(win
->id
);
1580 c
= xcb_get_window_attributes(conn
, win
->id
);
1581 wa
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
1583 warnx("window: 0x%x, map_state: %d, state: %u, "
1584 "transient: 0x%x", win
->id
, wa
->map_state
,
1585 state
, win
->transient
);
1588 warnx("window: 0x%x, failed xcb_get_window_attributes",
1592 warnx("=================================");
1596 dumpwins(struct swm_region
*r
, union arg
*s
)
1601 #endif /* SWM_DEBUG */
1606 int saved_errno
, status
;
1609 saved_errno
= errno
;
1613 while ((pid
= waitpid(WAIT_ANY
, &status
, WNOHANG
)) != 0) {
1618 if (errno
!= ECHILD
)
1619 warn("sighdlr: waitpid");
1620 #endif /* SWM_DEBUG */
1623 if (pid
== searchpid
)
1627 if (WIFEXITED(status
)) {
1628 if (WEXITSTATUS(status
) != 0)
1629 warnx("sighdlr: child exit status: %d",
1630 WEXITSTATUS(status
));
1632 warnx("sighdlr: child is terminated "
1634 #endif /* SWM_DEBUG */
1648 errno
= saved_errno
;
1654 struct pid_e
*p
= NULL
;
1656 DNPRINTF(SWM_D_MISC
, "find_pid: %d\n", pid
);
1661 TAILQ_FOREACH(p
, &pidlist
, entry
) {
1670 name_to_pixel(int sidx
, const char *colorname
)
1672 uint32_t result
= 0;
1673 char cname
[32] = "#";
1674 xcb_screen_t
*screen
;
1675 xcb_colormap_t cmap
;
1676 xcb_alloc_color_reply_t
*cr
;
1677 xcb_alloc_named_color_reply_t
*nr
;
1678 uint16_t rr
, gg
, bb
;
1680 screen
= get_screen(sidx
);
1681 cmap
= screen
->default_colormap
;
1683 /* color is in format rgb://rr/gg/bb */
1684 if (strncmp(colorname
, "rgb:", 4) == 0) {
1685 if (parse_rgb(colorname
, &rr
, &gg
, &bb
) == -1)
1686 warnx("could not parse rgb %s", colorname
);
1688 cr
= xcb_alloc_color_reply(conn
,
1689 xcb_alloc_color(conn
, cmap
, rr
, gg
, bb
),
1695 warnx("color '%s' not found", colorname
);
1698 nr
= xcb_alloc_named_color_reply(conn
,
1699 xcb_alloc_named_color(conn
, cmap
, strlen(colorname
),
1702 strlcat(cname
, colorname
+ 2, sizeof cname
- 1);
1703 nr
= xcb_alloc_named_color_reply(conn
,
1704 xcb_alloc_named_color(conn
, cmap
, strlen(cname
),
1711 warnx("color '%s' not found", colorname
);
1718 setscreencolor(char *val
, int i
, int c
)
1722 num_screens
= get_screen_count();
1723 if (i
> 0 && i
<= num_screens
) {
1724 screens
[i
- 1].c
[c
].pixel
= name_to_pixel(i
- 1, val
);
1725 free(screens
[i
- 1].c
[c
].name
);
1726 if ((screens
[i
- 1].c
[c
].name
= strdup(val
)) == NULL
)
1728 } else if (i
== -1) {
1729 for (i
= 0; i
< num_screens
; i
++) {
1730 screens
[i
].c
[c
].pixel
= name_to_pixel(0, val
);
1731 free(screens
[i
].c
[c
].name
);
1732 if ((screens
[i
].c
[c
].name
= strdup(val
)) == NULL
)
1736 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1741 fancy_stacker(struct workspace
*ws
)
1743 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1744 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1745 snprintf(ws
->stacker
, sizeof ws
->stacker
,
1746 ws
->l_state
.vertical_flip
? "[%d>%d]" : "[%d|%d]",
1747 ws
->l_state
.vertical_mwin
, ws
->l_state
.vertical_stacks
);
1748 if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1749 snprintf(ws
->stacker
, sizeof ws
->stacker
,
1750 ws
->l_state
.horizontal_flip
? "[%dv%d]" : "[%d-%d]",
1751 ws
->l_state
.horizontal_mwin
, ws
->l_state
.horizontal_stacks
);
1755 plain_stacker(struct workspace
*ws
)
1757 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1758 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1759 strlcpy(ws
->stacker
, ws
->l_state
.vertical_flip
? "[>]" : "[|]",
1760 sizeof ws
->stacker
);
1761 if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1762 strlcpy(ws
->stacker
, ws
->l_state
.horizontal_flip
? "[v]" : "[-]",
1763 sizeof ws
->stacker
);
1767 custom_region(char *val
)
1769 unsigned int x
, y
, w
, h
;
1770 int sidx
, num_screens
;
1771 xcb_screen_t
*screen
;
1773 num_screens
= get_screen_count();
1774 if (sscanf(val
, "screen[%d]:%ux%u+%u+%u", &sidx
, &w
, &h
, &x
, &y
) != 5)
1775 errx(1, "invalid custom region, "
1776 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1777 if (sidx
< 1 || sidx
> num_screens
)
1778 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1782 if ((screen
= get_screen(sidx
)) == NULL
)
1783 errx(1, "ERROR: can't get screen %d.", sidx
);
1786 errx(1, "region %ux%u+%u+%u too small", w
, h
, x
, y
);
1788 if (x
> screen
->width_in_pixels
||
1789 y
> screen
->height_in_pixels
||
1790 w
+ x
> screen
->width_in_pixels
||
1791 h
+ y
> screen
->height_in_pixels
) {
1792 warnx("ignoring region %ux%u+%u+%u - not within screen "
1793 "boundaries (%ux%u)", w
, h
, x
, y
,
1794 screen
->width_in_pixels
, screen
->height_in_pixels
);
1798 new_region(&screens
[sidx
], x
, y
, w
, h
);
1802 socket_setnonblock(int fd
)
1806 if ((flags
= fcntl(fd
, F_GETFL
, 0)) == -1)
1807 err(1, "fcntl F_GETFL");
1808 flags
|= O_NONBLOCK
;
1809 if ((flags
= fcntl(fd
, F_SETFL
, flags
)) == -1)
1810 err(1, "fcntl F_SETFL");
1814 bar_print_legacy(struct swm_region
*r
, const char *s
)
1816 xcb_rectangle_t rect
;
1821 XRectangle ibox
, lbox
;
1825 XmbTextExtents(bar_fs
, s
, len
, &ibox
, &lbox
);
1827 switch (bar_justify
) {
1828 case SWM_BAR_JUSTIFY_LEFT
:
1831 case SWM_BAR_JUSTIFY_CENTER
:
1832 x
= (WIDTH(r
) - lbox
.width
) / 2;
1834 case SWM_BAR_JUSTIFY_RIGHT
:
1835 x
= WIDTH(r
) - lbox
.width
- SWM_BAR_OFFSET
;
1839 if (x
< SWM_BAR_OFFSET
)
1844 rect
.width
= WIDTH(r
->bar
);
1845 rect
.height
= HEIGHT(r
->bar
);
1847 /* clear back buffer */
1848 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
1849 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
1850 xcb_poly_fill_rectangle(conn
, r
->bar
->buffer
, r
->s
->bar_gc
, 1, &rect
);
1852 /* draw back buffer */
1853 gcvd
.graphics_exposures
= 0;
1854 draw
= XCreateGC(display
, r
->bar
->buffer
, GCGraphicsExposures
, &gcvd
);
1855 XSetForeground(display
, draw
, r
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
);
1856 DRAWSTRING(display
, r
->bar
->buffer
, bar_fs
, draw
,
1857 x
, (bar_fs_extents
->max_logical_extent
.height
- lbox
.height
) / 2 -
1859 XFreeGC(display
, draw
);
1862 xcb_copy_area(conn
, r
->bar
->buffer
, r
->bar
->id
, r
->s
->bar_gc
, 0, 0,
1863 0, 0, WIDTH(r
->bar
), HEIGHT(r
->bar
));
1867 bar_print(struct swm_region
*r
, const char *s
)
1870 xcb_rectangle_t rect
;
1878 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
, &info
);
1880 switch (bar_justify
) {
1881 case SWM_BAR_JUSTIFY_LEFT
:
1884 case SWM_BAR_JUSTIFY_CENTER
:
1885 x
= (WIDTH(r
) - info
.width
) / 2;
1887 case SWM_BAR_JUSTIFY_RIGHT
:
1888 x
= WIDTH(r
) - info
.width
- SWM_BAR_OFFSET
;
1892 if (x
< SWM_BAR_OFFSET
)
1897 rect
.width
= WIDTH(r
->bar
);
1898 rect
.height
= HEIGHT(r
->bar
);
1900 /* clear back buffer */
1901 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
1902 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
1903 xcb_poly_fill_rectangle(conn
, r
->bar
->buffer
, r
->s
->bar_gc
, 1, &rect
);
1905 /* draw back buffer */
1906 draw
= XftDrawCreate(display
, r
->bar
->buffer
,
1907 DefaultVisual(display
, r
->s
->idx
),
1908 DefaultColormap(display
, r
->s
->idx
));
1910 XftDrawStringUtf8(draw
, &bar_font_color
, bar_font
, x
,
1911 (HEIGHT(r
->bar
) + bar_font
->height
) / 2 - bar_font
->descent
,
1914 XftDrawDestroy(draw
);
1917 xcb_copy_area(conn
, r
->bar
->buffer
, r
->bar
->id
, r
->s
->bar_gc
, 0, 0,
1918 0, 0, WIDTH(r
->bar
), HEIGHT(r
->bar
));
1922 bar_extra_stop(void)
1926 bzero(bar_pipe
, sizeof bar_pipe
);
1929 kill(bar_pid
, SIGTERM
);
1932 strlcpy(bar_ext
, "", sizeof bar_ext
);
1937 bar_class_name(char *s
, size_t sz
, struct swm_region
*r
)
1939 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1941 if (r
->ws
->focus
->ch
.class_name
!= NULL
)
1942 strlcat(s
, r
->ws
->focus
->ch
.class_name
, sz
);
1946 bar_title_name(char *s
, size_t sz
, struct swm_region
*r
)
1948 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1950 if (r
->ws
->focus
->ch
.instance_name
!= NULL
)
1951 strlcat(s
, r
->ws
->focus
->ch
.instance_name
, sz
);
1955 bar_class_title_name(char *s
, size_t sz
, struct swm_region
*r
)
1957 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1960 bar_class_name(s
, sz
, r
);
1961 strlcat(s
, ":", sz
);
1962 bar_title_name(s
, sz
, r
);
1966 bar_window_float(char *s
, size_t sz
, struct swm_region
*r
)
1968 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1970 if (r
->ws
->focus
->floating
)
1971 strlcat(s
, "(f)", sz
);
1975 bar_window_name(char *s
, size_t sz
, struct swm_region
*r
)
1979 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1981 if ((title
= get_win_name(r
->ws
->focus
->id
)) == NULL
)
1984 strlcat(s
, title
, sz
);
1988 int urgent
[SWM_WS_MAX
];
1990 bar_urgent(char *s
, size_t sz
)
1993 int i
, j
, num_screens
;
1995 xcb_get_property_cookie_t c
;
1996 xcb_icccm_wm_hints_t hints
;
1998 for (i
= 0; i
< workspace_limit
; i
++)
2001 num_screens
= get_screen_count();
2002 for (i
= 0; i
< num_screens
; i
++)
2003 for (j
= 0; j
< workspace_limit
; j
++)
2004 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
) {
2005 c
= xcb_icccm_get_wm_hints(conn
, win
->id
);
2006 if (xcb_icccm_get_wm_hints_reply(conn
, c
,
2009 if (hints
.flags
& XCB_ICCCM_WM_HINT_X_URGENCY
)
2013 for (i
= 0; i
< workspace_limit
; i
++) {
2015 snprintf(b
, sizeof b
, "%d ", i
+ 1);
2017 snprintf(b
, sizeof b
, "- ");
2023 bar_workspace_name(char *s
, size_t sz
, struct swm_region
*r
)
2025 if (r
== NULL
|| r
->ws
== NULL
)
2027 if (r
->ws
->name
!= NULL
)
2028 strlcat(s
, r
->ws
->name
, sz
);
2031 /* build the default bar format according to the defined enabled options */
2033 bar_fmt(const char *fmtexp
, char *fmtnew
, struct swm_region
*r
, size_t sz
)
2035 /* if format provided, just copy the buffers */
2036 if (bar_format
!= NULL
) {
2037 strlcpy(fmtnew
, fmtexp
, sz
);
2041 /* reset the output buffer */
2044 strlcat(fmtnew
, "+N:+I ", sz
);
2046 strlcat(fmtnew
, "+S", sz
);
2047 strlcat(fmtnew
, " ", sz
);
2049 /* only show the workspace name if there's actually one */
2050 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->name
!= NULL
)
2051 strlcat(fmtnew
, "<+D>", sz
);
2052 strlcat(fmtnew
, "+3<", sz
);
2054 if (clock_enabled
) {
2055 strlcat(fmtnew
, fmtexp
, sz
);
2056 strlcat(fmtnew
, "+4<", sz
);
2059 /* bar_urgent already adds the space before the last asterisk */
2061 strlcat(fmtnew
, "* +U*+4<", sz
);
2063 if (title_class_enabled
) {
2064 strlcat(fmtnew
, "+C", sz
);
2065 if (!title_name_enabled
)
2066 strlcat(fmtnew
, "+4<", sz
);
2069 /* checks needed by the colon and floating strlcat(3) calls below */
2070 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->focus
!= NULL
) {
2071 if (title_name_enabled
) {
2072 if (title_class_enabled
)
2073 strlcat(fmtnew
, ":", sz
);
2074 strlcat(fmtnew
, "+T+4<", sz
);
2076 if (window_name_enabled
) {
2077 if (r
->ws
->focus
->floating
)
2078 strlcat(fmtnew
, "+F ", sz
);
2079 strlcat(fmtnew
, "+64W ", sz
);
2083 /* finally add the action script output and the version */
2084 strlcat(fmtnew
, "+4<+A+4<+V", sz
);
2088 bar_replace_pad(char *tmp
, int *limit
, size_t sz
)
2090 /* special case; no limit given, pad one space, instead */
2091 if (*limit
== (int)sz
- 1)
2093 snprintf(tmp
, sz
, "%*s", *limit
, " ");
2096 /* replaces the bar format character sequences (like in tmux(1)) */
2098 bar_replace_seq(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t *offrep
,
2102 char tmp
[SWM_BAR_MAX
];
2106 /* reset strlcat(3) buffer */
2109 /* get number, if any */
2112 if (sscanf(fmt
, "%d%n", &limit
, &size
) != 1)
2113 limit
= sizeof tmp
- 1;
2114 if (limit
<= 0 || limit
>= (int)sizeof tmp
)
2115 limit
= sizeof tmp
- 1;
2117 /* there is nothing to replace (ie EOL) */
2124 bar_replace_pad(tmp
, &limit
, sizeof tmp
);
2127 snprintf(tmp
, sizeof tmp
, "%s", bar_ext
);
2130 bar_class_name(tmp
, sizeof tmp
, r
);
2133 bar_workspace_name(tmp
, sizeof tmp
, r
);
2136 bar_window_float(tmp
, sizeof tmp
, r
);
2139 snprintf(tmp
, sizeof tmp
, "%d", r
->ws
->idx
+ 1);
2142 snprintf(tmp
, sizeof tmp
, "%d", r
->s
->idx
+ 1);
2145 bar_class_title_name(tmp
, sizeof tmp
, r
);
2148 snprintf(tmp
, sizeof tmp
, "%s", r
->ws
->stacker
);
2151 bar_title_name(tmp
, sizeof tmp
, r
);
2154 bar_urgent(tmp
, sizeof tmp
);
2157 snprintf(tmp
, sizeof tmp
, "%s", bar_vertext
);
2160 bar_window_name(tmp
, sizeof tmp
, r
);
2163 /* unknown character sequence; copy as-is */
2164 snprintf(tmp
, sizeof tmp
, "+%c", *fmt
);
2170 if ((int)len
< limit
)
2172 while (limit
-- > 0) {
2173 if (*offrep
>= sz
- 1)
2175 fmtrep
[(*offrep
)++] = *ptr
++;
2183 bar_replace(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t sz
)
2188 while (*fmt
!= '\0') {
2190 /* skip ordinary characters */
2193 fmtrep
[off
++] = *fmt
++;
2197 /* character sequence found; replace it */
2198 fmt
= bar_replace_seq(fmt
, fmtrep
, r
, &off
, sz
);
2207 bar_fmt_expand(char *fmtexp
, size_t sz
)
2214 /* start by grabbing the current time and date */
2216 localtime_r(&tmt
, &tm
);
2218 /* figure out what to expand */
2219 if (bar_format
!= NULL
)
2221 else if (bar_format
== NULL
&& clock_enabled
)
2223 /* if nothing to expand bail out */
2229 /* copy as-is, just in case the format shouldn't be expanded below */
2230 strlcpy(fmtexp
, fmt
, sz
);
2231 /* finally pass the string through strftime(3) */
2232 #ifndef SWM_DENY_CLOCK_FORMAT
2233 if ((len
= strftime(fmtexp
, sz
, fmt
, &tm
)) == 0)
2234 warnx("format too long");
2239 /* Redraws the bar; need to follow with xcb_flush() or focus_flush(). */
2243 char fmtexp
[SWM_BAR_MAX
], fmtnew
[SWM_BAR_MAX
];
2244 char fmtrep
[SWM_BAR_MAX
];
2246 struct swm_region
*r
;
2248 /* expand the format by first passing it through strftime(3) */
2249 bar_fmt_expand(fmtexp
, sizeof fmtexp
);
2251 num_screens
= get_screen_count();
2252 for (i
= 0; i
< num_screens
; i
++) {
2253 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
2257 if (bar_enabled
&& r
->ws
->bar_enabled
)
2258 xcb_map_window(conn
, r
->bar
->id
);
2260 xcb_unmap_window(conn
, r
->bar
->id
);
2264 if (startup_exception
)
2265 snprintf(fmtrep
, sizeof fmtrep
, "total "
2266 "exceptions: %d, first exception: %s",
2270 bar_fmt(fmtexp
, fmtnew
, r
, sizeof fmtnew
);
2271 bar_replace(fmtnew
, fmtrep
, r
, sizeof fmtrep
);
2273 if (bar_font_legacy
)
2274 bar_print_legacy(r
, fmtrep
);
2276 bar_print(r
, fmtrep
);
2282 * Reads external script output; call when stdin is readable.
2283 * Returns 1 if bar_ext was updated; otherwise 0.
2286 bar_extra_update(void)
2289 char b
[SWM_BAR_MAX
];
2295 while (fgets(b
, sizeof(b
), stdin
) != NULL
) {
2298 if (b
[len
- 1] == '\n') {
2299 /* Remove newline. */
2302 /* "Clear" bar_ext. */
2305 /* Flush buffered output. */
2306 strlcpy(bar_ext
, bar_ext_buf
, sizeof(bar_ext
));
2307 bar_ext_buf
[0] = '\0';
2309 /* Append new output to bar. */
2310 strlcat(bar_ext
, b
, sizeof(bar_ext
));
2314 /* Buffer output. */
2315 strlcat(bar_ext_buf
, b
, sizeof(bar_ext_buf
));
2320 if (errno
!= EAGAIN
) {
2321 warn("bar_action failed");
2330 bar_toggle(struct swm_region
*r
, union arg
*args
)
2332 struct swm_region
*tmpr
;
2335 /* suppress unused warnings since vars are needed */
2339 DNPRINTF(SWM_D_BAR
, "bar_toggle\n");
2342 case SWM_ARG_ID_BAR_TOGGLE_WS
:
2343 /* Only change if master switch is enabled. */
2345 r
->ws
->bar_enabled
= !r
->ws
->bar_enabled
;
2347 bar_enabled
= r
->ws
->bar_enabled
= 1;
2349 case SWM_ARG_ID_BAR_TOGGLE
:
2350 bar_enabled
= !bar_enabled
;
2354 /* update bars as necessary */
2355 num_screens
= get_screen_count();
2356 for (i
= 0; i
< num_screens
; i
++)
2357 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
2359 if (bar_enabled
&& tmpr
->ws
->bar_enabled
)
2360 xcb_map_window(conn
, tmpr
->bar
->id
);
2362 xcb_unmap_window(conn
, tmpr
->bar
->id
);
2367 /* must be after stack */
2374 bar_extra_setup(void)
2376 /* do this here because the conf file is in memory */
2377 if (!bar_extra
&& bar_argv
[0]) {
2378 /* launch external status app */
2380 if (pipe(bar_pipe
) == -1)
2381 err(1, "pipe error");
2382 socket_setnonblock(bar_pipe
[0]);
2383 socket_setnonblock(bar_pipe
[1]); /* XXX hmmm, really? */
2385 /* Set stdin to read from the pipe. */
2386 if (dup2(bar_pipe
[0], STDIN_FILENO
) == -1)
2389 /* Set stdout to write to the pipe. */
2390 if (dup2(bar_pipe
[1], STDOUT_FILENO
) == -1)
2393 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
2394 err(1, "could not disable SIGPIPE");
2395 switch (bar_pid
= fork()) {
2397 err(1, "cannot fork");
2401 execvp(bar_argv
[0], bar_argv
);
2402 err(1, "%s external app failed", bar_argv
[0]);
2404 default: /* parent */
2409 atexit(kill_bar_extra_atexit
);
2414 kill_bar_extra_atexit(void)
2417 kill(bar_pid
, SIGTERM
);
2425 while ((s
= index(s
, '-'))) {
2430 return (count
== 14);
2436 char *default_string
;
2437 char **missing_charsets
;
2438 int num_missing_charsets
= 0;
2442 XFreeFontSet(display
, bar_fs
);
2446 DNPRINTF(SWM_D_INIT
, "fontset_init: loading bar_fonts: %s\n", bar_fonts
);
2448 bar_fs
= XCreateFontSet(display
, bar_fonts
, &missing_charsets
,
2449 &num_missing_charsets
, &default_string
);
2451 if (num_missing_charsets
> 0) {
2452 warnx("Unable to load charset(s):");
2454 for (i
= 0; i
< num_missing_charsets
; ++i
)
2455 warnx("%s", missing_charsets
[i
]);
2457 XFreeStringList(missing_charsets
);
2459 if (strcmp(default_string
, ""))
2460 warnx("Glyphs from those sets will be replaced "
2461 "by '%s'.", default_string
);
2463 warnx("Glyphs from those sets won't be drawn.");
2467 errx(1, "Error creating font set structure.");
2469 bar_fs_extents
= XExtentsOfFontSet(bar_fs
);
2471 bar_height
= bar_fs_extents
->max_logical_extent
.height
+
2472 2 * bar_border_width
;
2479 xft_init(struct swm_region
*r
)
2481 char *font
, *d
, *search
;
2484 if (bar_font
== NULL
) {
2485 if ((d
= strdup(bar_fonts
)) == NULL
)
2486 errx(1, "insufficient memory.");
2488 while ((font
= strsep(&search
, ",")) != NULL
) {
2492 DNPRINTF(SWM_D_INIT
, "xft_init: try font %s\n", font
);
2495 bar_font
= XftFontOpenXlfd(display
, r
->s
->idx
,
2498 bar_font
= XftFontOpenName(display
, r
->s
->idx
,
2503 warnx("unable to load font %s", font
);
2506 DNPRINTF(SWM_D_INIT
, "successfully opened "
2514 if (bar_font
== NULL
)
2515 errx(1, "unable to open a font");
2517 PIXEL_TO_XRENDERCOLOR(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
, color
);
2519 if (!XftColorAllocValue(display
, DefaultVisual(display
, r
->s
->idx
),
2520 DefaultColormap(display
, r
->s
->idx
), &color
, &bar_font_color
))
2521 warn("Xft error: unable to allocate color.");
2523 bar_height
= bar_font
->height
+ 2 * bar_border_width
;
2530 bar_setup(struct swm_region
*r
)
2532 xcb_screen_t
*screen
;
2535 DNPRINTF(SWM_D_BAR
, "bar_setup: screen %d.\n",
2538 if ((screen
= get_screen(r
->s
->idx
)) == NULL
)
2539 errx(1, "ERROR: can't get screen %d.", r
->s
->idx
);
2544 if ((r
->bar
= calloc(1, sizeof(struct swm_bar
))) == NULL
)
2545 err(1, "bar_setup: calloc: failed to allocate memory.");
2547 if (bar_font_legacy
)
2553 Y(r
->bar
) = bar_at_bottom
? (Y(r
) + HEIGHT(r
) - bar_height
) : Y(r
);
2554 WIDTH(r
->bar
) = WIDTH(r
) - 2 * bar_border_width
;
2555 HEIGHT(r
->bar
) = bar_height
- 2 * bar_border_width
;
2557 /* Assume region is unfocused when we create the bar. */
2558 r
->bar
->id
= xcb_generate_id(conn
);
2559 wa
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
2560 wa
[1] = r
->s
->c
[SWM_S_COLOR_BAR_BORDER_UNFOCUS
].pixel
;
2561 wa
[2] = XCB_EVENT_MASK_EXPOSURE
| XCB_EVENT_MASK_POINTER_MOTION
|
2562 XCB_EVENT_MASK_POINTER_MOTION_HINT
;
2564 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, r
->bar
->id
, r
->s
->root
,
2565 X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
), HEIGHT(r
->bar
),
2566 bar_border_width
, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
2567 XCB_COPY_FROM_PARENT
, XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
2568 | XCB_CW_EVENT_MASK
, wa
);
2570 r
->bar
->buffer
= xcb_generate_id(conn
);
2571 xcb_create_pixmap(conn
, screen
->root_depth
, r
->bar
->buffer
, r
->bar
->id
,
2572 WIDTH(r
->bar
), HEIGHT(r
->bar
));
2575 xcb_randr_select_input(conn
, r
->bar
->id
,
2576 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE
);
2579 xcb_map_window(conn
, r
->bar
->id
);
2581 DNPRINTF(SWM_D_BAR
, "bar_setup: window: 0x%x, (x,y) w x h: (%d,%d) "
2582 "%d x %d\n", WINID(r
->bar
), X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
),
2589 bar_cleanup(struct swm_region
*r
)
2593 xcb_destroy_window(conn
, r
->bar
->id
);
2594 xcb_free_pixmap(conn
, r
->bar
->buffer
);
2600 set_win_state(struct ws_win
*win
, uint16_t state
)
2602 uint16_t data
[2] = { state
, XCB_ATOM_NONE
};
2604 DNPRINTF(SWM_D_EVENT
, "set_win_state: window: 0x%x, state: %u\n",
2610 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
, a_state
,
2611 a_state
, 32, 2, data
);
2615 getstate(xcb_window_t w
)
2617 uint32_t result
= 0;
2618 xcb_get_property_cookie_t c
;
2619 xcb_get_property_reply_t
*r
;
2621 c
= xcb_get_property(conn
, 0, w
, a_state
, a_state
, 0L, 2L);
2622 r
= xcb_get_property_reply(conn
, c
, NULL
);
2625 if (r
->type
== a_state
&& r
->format
== 32 && r
->length
== 2)
2626 result
= *((uint32_t *)xcb_get_property_value(r
));
2630 DNPRINTF(SWM_D_MISC
, "getstate property: win 0x%x state %u\n", w
,
2636 version(struct swm_region
*r
, union arg
*args
)
2638 /* suppress unused warnings since vars are needed */
2642 bar_version
= !bar_version
;
2644 snprintf(bar_vertext
, sizeof bar_vertext
,
2645 "Version: %s Build: %s", SPECTRWM_VERSION
, buildstr
);
2647 strlcpy(bar_vertext
, "", sizeof bar_vertext
);
2654 client_msg(struct ws_win
*win
, xcb_atom_t a
, xcb_timestamp_t t
)
2656 xcb_client_message_event_t ev
;
2664 name
= get_atom_name(a
);
2665 DNPRINTF(SWM_D_EVENT
, "client_msg: window: 0x%x, atom: %s(%u), "
2667 win
->id
, name
, a
, t
);
2671 bzero(&ev
, sizeof ev
);
2672 ev
.response_type
= XCB_CLIENT_MESSAGE
;
2673 ev
.window
= win
->id
;
2676 ev
.data
.data32
[0] = a
;
2677 ev
.data
.data32
[1] = t
;
2679 xcb_send_event(conn
, 0, win
->id
,
2680 XCB_EVENT_MASK_NO_EVENT
, (const char *)&ev
);
2683 /* synthetic response to a ConfigureRequest when not making a change */
2685 config_win(struct ws_win
*win
, xcb_configure_request_event_t
*ev
)
2687 xcb_configure_notify_event_t ce
;
2692 /* send notification of unchanged state. */
2693 bzero(&ce
, sizeof(ce
));
2694 ce
.response_type
= XCB_CONFIGURE_NOTIFY
;
2697 ce
.width
= WIDTH(win
);
2698 ce
.height
= HEIGHT(win
);
2699 ce
.override_redirect
= 0;
2704 ce
.window
= win
->id
;
2705 ce
.border_width
= BORDER(win
);
2706 ce
.above_sibling
= XCB_WINDOW_NONE
;
2709 ce
.event
= ev
->window
;
2710 ce
.window
= ev
->window
;
2712 /* make response appear more WM_SIZE_HINTS-compliant */
2713 if (win
->sh
.flags
) {
2714 DNPRINTF(SWM_D_MISC
, "config_win: hints: window: 0x%x,"
2715 " sh.flags: %u, min: %d x %d, max: %d x %d, inc: "
2716 "%d x %d\n", win
->id
, win
->sh
.flags
, SH_MIN_W(win
),
2717 SH_MIN_H(win
), SH_MAX_W(win
), SH_MAX_H(win
),
2718 SH_INC_W(win
), SH_INC_H(win
));
2723 /* the hint may be set... to 0! */
2724 if (SH_MIN_W(win
) > 0 && ce
.width
< SH_MIN_W(win
))
2725 ce
.width
= SH_MIN_W(win
);
2726 if (SH_MIN_H(win
) > 0 && ce
.height
< SH_MIN_H(win
))
2727 ce
.height
= SH_MIN_H(win
);
2732 /* may also be advertized as 0 */
2733 if (SH_MAX_W(win
) > 0 && ce
.width
> SH_MAX_W(win
))
2734 ce
.width
= SH_MAX_W(win
);
2735 if (SH_MAX_H(win
) > 0 && ce
.height
> SH_MAX_H(win
))
2736 ce
.height
= SH_MAX_H(win
);
2739 /* resize increment. */
2741 if (SH_INC_W(win
) > 1 && ce
.width
> SH_INC_W(win
))
2742 ce
.width
-= (ce
.width
- SH_MIN_W(win
)) %
2744 if (SH_INC_H(win
) > 1 && ce
.height
> SH_INC_H(win
))
2745 ce
.height
-= (ce
.height
- SH_MIN_H(win
)) %
2749 /* adjust x and y for requested border_width. */
2750 ce
.x
+= BORDER(win
) - ev
->border_width
;
2751 ce
.y
+= BORDER(win
) - ev
->border_width
;
2752 ce
.border_width
= ev
->border_width
;
2753 ce
.above_sibling
= ev
->sibling
;
2756 DNPRINTF(SWM_D_MISC
, "config_win: ewmh: %s, window: 0x%x, (x,y) w x h: "
2757 "(%d,%d) %d x %d, border: %d\n", YESNO(ev
== NULL
), win
->id
, ce
.x
,
2758 ce
.y
, ce
.width
, ce
.height
, ce
.border_width
);
2760 xcb_send_event(conn
, 0, win
->id
, XCB_EVENT_MASK_STRUCTURE_NOTIFY
,
2765 count_win(struct workspace
*ws
, int count_transient
)
2770 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
2771 if (!count_transient
&& win
->floating
)
2773 if (!count_transient
&& win
->transient
)
2779 DNPRINTF(SWM_D_MISC
, "count_win: %d\n", count
);
2785 quit(struct swm_region
*r
, union arg
*args
)
2787 /* suppress unused warnings since vars are needed */
2791 DNPRINTF(SWM_D_MISC
, "quit\n");
2796 map_window(struct ws_win
*win
)
2798 uint32_t val
= XCB_STACK_MODE_ABOVE
;
2803 DNPRINTF(SWM_D_EVENT
, "map_window: win 0x%x, mapped: %s\n", win
->id
,
2804 YESNO(win
->mapped
));
2806 xcb_configure_window(conn
, win
->id
,
2807 XCB_CONFIG_WINDOW_STACK_MODE
, &val
);
2812 xcb_map_window(conn
, win
->id
);
2813 set_win_state(win
, XCB_ICCCM_WM_STATE_NORMAL
);
2818 unmap_window(struct ws_win
*win
)
2823 DNPRINTF(SWM_D_EVENT
, "unmap_window: win 0x%x, mapped: %s\n", win
->id
,
2824 YESNO(win
->mapped
));
2829 xcb_unmap_window(conn
, win
->id
);
2830 set_win_state(win
, XCB_ICCCM_WM_STATE_ICONIC
);
2838 int i
, j
, num_screens
;
2840 num_screens
= get_screen_count();
2841 for (i
= 0; i
< num_screens
; i
++)
2842 for (j
= 0; j
< workspace_limit
; j
++)
2843 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2848 fake_keypress(struct ws_win
*win
, xcb_keysym_t keysym
, uint16_t modifiers
)
2850 xcb_key_press_event_t event
;
2851 xcb_keycode_t
*keycode
;
2856 keycode
= xcb_key_symbols_get_keycode(syms
, keysym
);
2858 DNPRINTF(SWM_D_MISC
, "fake_keypress: win 0x%x keycode %u\n",
2861 bzero(&event
, sizeof(event
));
2862 event
.event
= win
->id
;
2863 event
.root
= win
->s
->root
;
2864 event
.child
= XCB_WINDOW_NONE
;
2865 event
.time
= XCB_CURRENT_TIME
;
2866 event
.event_x
= X(win
);
2867 event
.event_y
= Y(win
);
2870 event
.same_screen
= 1;
2871 event
.detail
= *keycode
;
2872 event
.state
= modifiers
;
2874 event
.response_type
= XCB_KEY_PRESS
;
2875 xcb_send_event(conn
, 1, win
->id
,
2876 XCB_EVENT_MASK_KEY_PRESS
, (const char *)&event
);
2878 event
.response_type
= XCB_KEY_RELEASE
;
2879 xcb_send_event(conn
, 1, win
->id
,
2880 XCB_EVENT_MASK_KEY_RELEASE
, (const char *)&event
);
2886 restart(struct swm_region
*r
, union arg
*args
)
2888 /* suppress unused warning since var is needed */
2892 DNPRINTF(SWM_D_MISC
, "restart: %s\n", start_argv
[0]);
2896 execvp(start_argv
[0], start_argv
);
2897 warn("execvp failed");
2902 get_pointer_win(xcb_window_t root
)
2904 struct ws_win
*win
= NULL
;
2905 xcb_query_pointer_reply_t
*r
;
2907 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: root: 0x%x.\n", root
);
2909 r
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, root
), NULL
);
2911 win
= find_window(r
->child
);
2913 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: 0x%x.\n",
2916 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: none.\n");
2925 root_to_region(xcb_window_t root
, int check
)
2928 struct swm_region
*r
= NULL
;
2930 xcb_query_pointer_reply_t
*qpr
;
2931 xcb_get_input_focus_reply_t
*gifr
;
2933 DNPRINTF(SWM_D_MISC
, "root_to_region: window: 0x%x\n", root
);
2935 num_screens
= get_screen_count();
2936 for (i
= 0; i
< num_screens
; i
++)
2937 if (screens
[i
].root
== root
)
2940 if (check
& SWM_CK_REGION
)
2941 r
= screens
[i
].r_focus
;
2943 if (r
== NULL
&& check
& SWM_CK_FOCUS
) {
2944 /* Try to find an actively focused window */
2945 gifr
= xcb_get_input_focus_reply(conn
,
2946 xcb_get_input_focus(conn
), NULL
);
2948 cfw
= find_window(gifr
->focus
);
2949 if (cfw
&& cfw
->ws
->r
)
2956 if (r
== NULL
&& check
& SWM_CK_POINTER
) {
2957 /* No region with an active focus; try to use pointer. */
2958 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
,
2959 screens
[i
].root
), NULL
);
2962 DNPRINTF(SWM_D_MISC
, "root_to_region: pointer: "
2963 "(%d,%d)\n", qpr
->root_x
, qpr
->root_y
);
2964 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2965 if (X(r
) <= qpr
->root_x
&&
2966 qpr
->root_x
< MAX_X(r
) &&
2967 Y(r
) <= qpr
->root_y
&&
2968 qpr
->root_y
< MAX_Y(r
))
2975 if (r
== NULL
&& check
& SWM_CK_FALLBACK
)
2976 r
= TAILQ_FIRST(&screens
[i
].rl
);
2982 find_unmanaged_window(xcb_window_t id
)
2985 int i
, j
, num_screens
;
2987 num_screens
= get_screen_count();
2988 for (i
= 0; i
< num_screens
; i
++)
2989 for (j
= 0; j
< workspace_limit
; j
++)
2990 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].unmanagedlist
,
2998 find_window(xcb_window_t id
)
3001 int i
, j
, num_screens
;
3002 xcb_query_tree_reply_t
*r
;
3004 num_screens
= get_screen_count();
3005 for (i
= 0; i
< num_screens
; i
++)
3006 for (j
= 0; j
< workspace_limit
; j
++)
3007 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
3011 r
= xcb_query_tree_reply(conn
, xcb_query_tree(conn
, id
), NULL
);
3015 /* if we were looking for the parent return that window instead */
3016 if (r
->parent
== 0 || r
->root
== r
->parent
) {
3021 /* look for parent */
3022 for (i
= 0; i
< num_screens
; i
++)
3023 for (j
= 0; j
< workspace_limit
; j
++)
3024 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
3025 if (r
->parent
== win
->id
) {
3035 spawn(int ws_idx
, union arg
*args
, int close_fd
)
3040 DNPRINTF(SWM_D_MISC
, "spawn: %s\n", args
->argv
[0]);
3042 close(xcb_get_file_descriptor(conn
));
3044 setenv("LD_PRELOAD", SWM_LIB
, 1);
3046 if (asprintf(&ret
, "%d", ws_idx
) == -1) {
3047 warn("spawn: asprintf SWM_WS");
3050 setenv("_SWM_WS", ret
, 1);
3054 if (asprintf(&ret
, "%d", getpid()) == -1) {
3055 warn("spawn: asprintf _SWM_PID");
3058 setenv("_SWM_PID", ret
, 1);
3062 if (setsid() == -1) {
3063 warn("spawn: setsid");
3069 * close stdin and stdout to prevent interaction between apps
3070 * and the baraction script
3071 * leave stderr open to record errors
3073 if ((fd
= open(_PATH_DEVNULL
, O_RDWR
, 0)) == -1) {
3074 warn("spawn: open");
3077 dup2(fd
, STDIN_FILENO
);
3078 dup2(fd
, STDOUT_FILENO
);
3083 execvp(args
->argv
[0], args
->argv
);
3085 warn("spawn: execvp");
3090 kill_refs(struct ws_win
*win
)
3092 int i
, x
, num_screens
;
3093 struct swm_region
*r
;
3094 struct workspace
*ws
;
3099 num_screens
= get_screen_count();
3100 for (i
= 0; i
< num_screens
; i
++)
3101 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3102 for (x
= 0; x
< workspace_limit
; x
++) {
3104 if (win
== ws
->focus
)
3106 if (win
== ws
->focus_prev
)
3107 ws
->focus_prev
= NULL
;
3112 validate_win(struct ws_win
*testwin
)
3115 struct workspace
*ws
;
3116 struct swm_region
*r
;
3117 int i
, x
, num_screens
;
3119 if (testwin
== NULL
)
3122 num_screens
= get_screen_count();
3123 for (i
= 0; i
< num_screens
; i
++)
3124 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3125 for (x
= 0; x
< workspace_limit
; x
++) {
3127 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
3135 validate_ws(struct workspace
*testws
)
3137 struct swm_region
*r
;
3138 struct workspace
*ws
;
3139 int i
, x
, num_screens
;
3141 /* validate all ws */
3142 num_screens
= get_screen_count();
3143 for (i
= 0; i
< num_screens
; i
++)
3144 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3145 for (x
= 0; x
< workspace_limit
; x
++) {
3154 unfocus_win(struct ws_win
*win
)
3156 xcb_window_t none
= XCB_WINDOW_NONE
;
3158 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: window: 0x%x\n", WINID(win
));
3162 if (win
->ws
== NULL
) {
3163 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: NULL ws.\n");
3167 if (validate_ws(win
->ws
)) {
3168 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: invalid ws.\n");
3172 if (win
->ws
->r
== NULL
) {
3173 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: NULL region.\n");
3177 if (validate_win(win
)) {
3178 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: invalid win.\n");
3183 if (win
->ws
->focus
== win
) {
3184 win
->ws
->focus
= NULL
;
3185 win
->ws
->focus_prev
= win
;
3188 if (validate_win(win
->ws
->focus
)) {
3189 kill_refs(win
->ws
->focus
);
3190 win
->ws
->focus
= NULL
;
3193 if (validate_win(win
->ws
->focus_prev
)) {
3194 kill_refs(win
->ws
->focus_prev
);
3195 win
->ws
->focus_prev
= NULL
;
3198 xcb_change_window_attributes(conn
, win
->id
, XCB_CW_BORDER_PIXEL
,
3199 &win
->ws
->r
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
);
3201 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
3202 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1, &none
);
3204 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: done.\n");
3208 focus_win(struct ws_win
*win
)
3210 struct ws_win
*cfw
= NULL
, *parent
= NULL
, *w
;
3211 struct workspace
*ws
;
3212 xcb_get_input_focus_reply_t
*r
;
3214 DNPRINTF(SWM_D_FOCUS
, "focus_win: window: 0x%x\n", WINID(win
));
3219 if (win
->ws
== NULL
)
3227 if (validate_ws(ws
))
3230 if (validate_win(win
)) {
3235 r
= xcb_get_input_focus_reply(conn
, xcb_get_input_focus(conn
), NULL
);
3237 cfw
= find_window(r
->focus
);
3243 if (ws
->focus
!= win
) {
3244 if (ws
->focus
&& ws
->focus
!= cfw
)
3245 unfocus_win(ws
->focus
);
3249 /* If this window directs focus to a child window, then clear. */
3250 if (win
->focus_child
)
3251 win
->focus_child
= NULL
;
3253 /* If transient, adjust parent's focus child for focus_magic. */
3254 if (win
->transient
) {
3255 parent
= find_window(win
->transient
);
3256 if (parent
&& parent
->focus_child
!= win
)
3257 parent
->focus_child
= win
;
3260 if (cfw
!= win
&& ws
->r
!= NULL
) {
3261 /* Set input focus if no input hint, or indicated by hint. */
3262 if (!(win
->hints
.flags
& XCB_ICCCM_WM_HINT_INPUT
) ||
3263 (win
->hints
.flags
& XCB_ICCCM_WM_HINT_INPUT
&&
3265 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
,
3266 win
->id
, last_event_time
);
3268 /* Tell app it can adjust focus to a specific window. */
3269 if (win
->take_focus
) {
3270 /* java is special; always tell parent */
3271 if (win
->transient
&& win
->java
)
3272 client_msg(parent
, a_takefocus
,
3275 client_msg(win
, a_takefocus
, last_event_time
);
3278 xcb_change_window_attributes(conn
, win
->id
, XCB_CW_BORDER_PIXEL
,
3279 &ws
->r
->s
->c
[SWM_S_COLOR_FOCUS
].pixel
);
3281 if (ws
->cur_layout
->flags
& SWM_L_MAPONFOCUS
||
3283 /* If a parent exists, map it first. */
3287 /* Map siblings next. */
3288 TAILQ_FOREACH(w
, &ws
->winlist
, entry
)
3289 if (w
!= win
&& !w
->iconic
&&
3290 w
->transient
== parent
->id
)
3294 /* Map focused window. */
3297 /* Finally, map children of focus window. */
3298 TAILQ_FOREACH(w
, &ws
->winlist
, entry
)
3299 if (w
->transient
== win
->id
&& !w
->iconic
)
3305 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
3306 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1,
3313 DNPRINTF(SWM_D_FOCUS
, "focus_win: done.\n");
3316 /* If a child window should have focus instead, return it. */
3318 get_focus_magic(struct ws_win
*win
)
3320 struct ws_win
*parent
= NULL
;
3321 struct ws_win
*child
= NULL
;
3323 DNPRINTF(SWM_D_FOCUS
, "get_focus_magic: window: 0x%x\n", WINID(win
));
3327 if (win
->transient
) {
3328 parent
= find_window(win
->transient
);
3330 /* If parent prefers focus elsewhere, then try to do so. */
3331 if (parent
&& (child
= parent
->focus_child
)) {
3332 if (validate_win(child
) == 0 && child
->mapped
)
3335 parent
->focus_child
= NULL
;
3339 /* If this window prefers focus elsewhere, then try to do so. */
3340 if ((child
= win
->focus_child
)) {
3341 if (validate_win(child
) == 0 && child
->mapped
)
3344 win
->focus_child
= NULL
;
3351 event_drain(uint8_t rt
)
3353 xcb_generic_event_t
*evt
;
3355 /* ensure all pending requests have been processed before filtering. */
3357 while ((evt
= xcb_poll_for_event(conn
))) {
3358 if (XCB_EVENT_RESPONSE_TYPE(evt
) != rt
)
3366 set_region(struct swm_region
*r
)
3368 struct swm_region
*rf
;
3374 /* Unfocus old region bar. */
3379 xcb_change_window_attributes(conn
, rf
->bar
->id
,
3380 XCB_CW_BORDER_PIXEL
,
3381 &r
->s
->c
[SWM_S_COLOR_BAR_BORDER_UNFOCUS
].pixel
);
3384 /* Set region bar border to focus_color. */
3385 xcb_change_window_attributes(conn
, r
->bar
->id
,
3386 XCB_CW_BORDER_PIXEL
, &r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].pixel
);
3392 focus_region(struct swm_region
*r
)
3395 struct swm_region
*old_r
;
3400 old_r
= r
->s
->r_focus
;
3403 nfw
= get_region_focus(r
);
3407 /* New region is empty; need to manually unfocus win. */
3409 unfocus_win(old_r
->ws
->focus
);
3411 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
, r
->s
->root
,
3414 /* Clear bar since empty. */
3420 switchws(struct swm_region
*r
, union arg
*args
)
3422 int wsid
= args
->id
, unmap_old
= 0;
3423 struct swm_region
*this_r
, *other_r
;
3425 struct workspace
*new_ws
, *old_ws
;
3430 if (wsid
>= workspace_limit
)
3434 old_ws
= this_r
->ws
;
3435 new_ws
= &this_r
->s
->ws
[wsid
];
3437 DNPRINTF(SWM_D_WS
, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
3438 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), old_ws
->idx
, wsid
);
3440 if (new_ws
== NULL
|| old_ws
== NULL
)
3442 if (new_ws
== old_ws
)
3445 unfocus_win(old_ws
->focus
);
3447 other_r
= new_ws
->r
;
3448 if (other_r
== NULL
) {
3449 /* the other workspace is hidden, hide this one */
3453 /* the other ws is visible in another region, exchange them */
3454 other_r
->ws_prior
= new_ws
;
3455 other_r
->ws
= old_ws
;
3456 old_ws
->r
= other_r
;
3458 this_r
->ws_prior
= old_ws
;
3459 this_r
->ws
= new_ws
;
3462 /* Set focus_pending before stacking. */
3463 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
3464 new_ws
->focus_pending
= get_region_focus(new_ws
->r
);
3468 /* unmap old windows */
3470 TAILQ_FOREACH(win
, &old_ws
->winlist
, entry
)
3473 /* if workspaces were swapped, then don't wait to set focus */
3474 if (old_ws
->r
&& focus_mode
!= SWM_FOCUS_FOLLOW
) {
3475 if (new_ws
->focus_pending
) {
3476 focus_win(new_ws
->focus_pending
);
3478 /* Empty region, focus on root. */
3479 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
,
3480 new_ws
->r
->s
[new_ws
->r
->s
->idx
].root
,
3485 /* Clear bar if new ws is empty. */
3486 if (new_ws
->focus_pending
== NULL
)
3491 DNPRINTF(SWM_D_WS
, "switchws: done.\n");
3495 cyclews(struct swm_region
*r
, union arg
*args
)
3498 struct swm_screen
*s
= r
->s
;
3501 DNPRINTF(SWM_D_WS
, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3502 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
3507 case SWM_ARG_ID_CYCLEWS_UP_ALL
:
3510 case SWM_ARG_ID_CYCLEWS_UP
:
3511 if (a
.id
< workspace_limit
- 1)
3516 case SWM_ARG_ID_CYCLEWS_DOWN_ALL
:
3519 case SWM_ARG_ID_CYCLEWS_DOWN
:
3523 a
.id
= workspace_limit
- 1;
3530 (!cycle_empty
&& TAILQ_EMPTY(&s
->ws
[a
.id
].winlist
)))
3532 if (!cycle_visible
&& s
->ws
[a
.id
].r
!= NULL
)
3536 } while (a
.id
!= r
->ws
->idx
);
3540 priorws(struct swm_region
*r
, union arg
*args
)
3546 DNPRINTF(SWM_D_WS
, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3547 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
3549 if (r
->ws_prior
== NULL
)
3552 a
.id
= r
->ws_prior
->idx
;
3557 focusrg(struct swm_region
*r
, union arg
*args
)
3559 int ridx
= args
->id
, i
, num_screens
;
3560 struct swm_region
*rr
= NULL
;
3562 num_screens
= get_screen_count();
3563 /* do nothing if we don't have more than one screen */
3564 if (!(num_screens
> 1 || outputs
> 1))
3567 DNPRINTF(SWM_D_FOCUS
, "focusrg: id: %d\n", ridx
);
3569 rr
= TAILQ_FIRST(&r
->s
->rl
);
3570 for (i
= 0; i
< ridx
; ++i
)
3571 rr
= TAILQ_NEXT(rr
, entry
);
3581 cyclerg(struct swm_region
*r
, union arg
*args
)
3583 struct swm_region
*rr
= NULL
;
3586 num_screens
= get_screen_count();
3587 /* do nothing if we don't have more than one screen */
3588 if (!(num_screens
> 1 || outputs
> 1))
3593 case SWM_ARG_ID_CYCLERG_UP
:
3594 rr
= TAILQ_NEXT(r
, entry
);
3596 rr
= TAILQ_FIRST(&screens
[i
].rl
);
3598 case SWM_ARG_ID_CYCLERG_DOWN
:
3599 rr
= TAILQ_PREV(r
, swm_region_list
, entry
);
3601 rr
= TAILQ_LAST(&screens
[i
].rl
, swm_region_list
);
3614 sort_windows(struct ws_win_list
*wl
)
3616 struct ws_win
*win
, *parent
, *nxt
;
3621 for (win
= TAILQ_FIRST(wl
); win
!= TAILQ_END(wl
); win
= nxt
) {
3622 nxt
= TAILQ_NEXT(win
, entry
);
3623 if (win
->transient
) {
3624 parent
= find_window(win
->transient
);
3625 if (parent
== NULL
) {
3626 warnx("not possible bug");
3629 TAILQ_REMOVE(wl
, win
, entry
);
3630 TAILQ_INSERT_AFTER(wl
, parent
, win
, entry
);
3637 swapwin(struct swm_region
*r
, union arg
*args
)
3639 struct ws_win
*target
, *source
;
3640 struct ws_win
*cur_focus
;
3641 struct ws_win_list
*wl
;
3643 DNPRINTF(SWM_D_WS
, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3644 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
3646 cur_focus
= r
->ws
->focus
;
3647 if (cur_focus
== NULL
)
3651 wl
= &source
->ws
->winlist
;
3654 case SWM_ARG_ID_SWAPPREV
:
3655 if (source
->transient
)
3656 source
= find_window(source
->transient
);
3657 target
= TAILQ_PREV(source
, ws_win_list
, entry
);
3658 if (target
&& target
->transient
)
3659 target
= find_window(target
->transient
);
3660 TAILQ_REMOVE(wl
, source
, entry
);
3662 TAILQ_INSERT_TAIL(wl
, source
, entry
);
3664 TAILQ_INSERT_BEFORE(target
, source
, entry
);
3666 case SWM_ARG_ID_SWAPNEXT
:
3667 target
= TAILQ_NEXT(source
, entry
);
3668 /* move the parent and let the sort handle the move */
3669 if (source
->transient
)
3670 source
= find_window(source
->transient
);
3671 TAILQ_REMOVE(wl
, source
, entry
);
3673 TAILQ_INSERT_HEAD(wl
, source
, entry
);
3675 TAILQ_INSERT_AFTER(wl
, target
, source
, entry
);
3677 case SWM_ARG_ID_SWAPMAIN
:
3678 target
= TAILQ_FIRST(wl
);
3679 if (target
== source
) {
3680 if (source
->ws
->focus_prev
!= NULL
&&
3681 source
->ws
->focus_prev
!= target
)
3682 source
= source
->ws
->focus_prev
;
3686 if (target
== NULL
|| source
== NULL
)
3688 source
->ws
->focus_prev
= target
;
3689 TAILQ_REMOVE(wl
, target
, entry
);
3690 TAILQ_INSERT_BEFORE(source
, target
, entry
);
3691 TAILQ_REMOVE(wl
, source
, entry
);
3692 TAILQ_INSERT_HEAD(wl
, source
, entry
);
3694 case SWM_ARG_ID_MOVELAST
:
3695 TAILQ_REMOVE(wl
, source
, entry
);
3696 TAILQ_INSERT_TAIL(wl
, source
, entry
);
3699 DNPRINTF(SWM_D_MOVE
, "swapwin: invalid id: %d\n", args
->id
);
3711 get_focus_prev(struct ws_win
*win
)
3713 struct ws_win
*winfocus
= NULL
;
3714 struct ws_win
*cur_focus
= NULL
;
3715 struct ws_win_list
*wl
= NULL
;
3716 struct workspace
*ws
= NULL
;
3718 if (!(win
&& win
->ws
))
3723 cur_focus
= ws
->focus
;
3725 DNPRINTF(SWM_D_FOCUS
, "get_focus_prev: window: 0x%x, cur_focus: 0x%x\n",
3726 WINID(win
), WINID(cur_focus
));
3728 /* pickle, just focus on whatever */
3729 if (cur_focus
== NULL
) {
3730 /* use prev_focus if valid */
3731 if (ws
->focus_prev
&& ws
->focus_prev
!= cur_focus
&&
3732 find_window(WINID(ws
->focus_prev
)))
3733 winfocus
= ws
->focus_prev
;
3737 /* if transient focus on parent */
3738 if (cur_focus
->transient
) {
3739 winfocus
= find_window(cur_focus
->transient
);
3743 /* if in max_stack try harder */
3744 if ((win
->quirks
& SWM_Q_FOCUSPREV
) ||
3745 (ws
->cur_layout
->flags
& SWM_L_FOCUSPREV
)) {
3746 if (cur_focus
!= ws
->focus_prev
)
3747 winfocus
= ws
->focus_prev
;
3749 winfocus
= TAILQ_PREV(win
, ws_win_list
, entry
);
3754 DNPRINTF(SWM_D_FOCUS
, "get_focus_prev: focus_close: %d\n", focus_close
);
3756 if (winfocus
== NULL
|| winfocus
== win
) {
3757 switch (focus_close
) {
3758 case SWM_STACK_BOTTOM
:
3759 TAILQ_FOREACH(winfocus
, wl
, entry
)
3760 if (!winfocus
->iconic
&& winfocus
!= cur_focus
)
3764 TAILQ_FOREACH_REVERSE(winfocus
, wl
, ws_win_list
, entry
)
3765 if (!winfocus
->iconic
&& winfocus
!= cur_focus
)
3768 case SWM_STACK_ABOVE
:
3769 winfocus
= TAILQ_NEXT(cur_focus
, entry
);
3770 while (winfocus
&& winfocus
->iconic
)
3771 winfocus
= TAILQ_NEXT(winfocus
, entry
);
3773 if (winfocus
== NULL
) {
3774 if (focus_close_wrap
) {
3775 TAILQ_FOREACH(winfocus
, wl
, entry
)
3776 if (!winfocus
->iconic
&&
3777 winfocus
!= cur_focus
)
3780 TAILQ_FOREACH_REVERSE(winfocus
, wl
,
3782 if (!winfocus
->iconic
&&
3783 winfocus
!= cur_focus
)
3788 case SWM_STACK_BELOW
:
3789 winfocus
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
3790 while (winfocus
&& winfocus
->iconic
)
3791 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
,
3794 if (winfocus
== NULL
) {
3795 if (focus_close_wrap
) {
3796 TAILQ_FOREACH_REVERSE(winfocus
, wl
,
3798 if (!winfocus
->iconic
&&
3799 winfocus
!= cur_focus
)
3802 TAILQ_FOREACH(winfocus
, wl
, entry
)
3803 if (!winfocus
->iconic
&&
3804 winfocus
!= cur_focus
)
3812 if (winfocus
== NULL
||
3813 (winfocus
&& (winfocus
->iconic
|| winfocus
== cur_focus
))) {
3814 if (focus_default
== SWM_STACK_TOP
) {
3815 TAILQ_FOREACH_REVERSE(winfocus
, wl
, ws_win_list
, entry
)
3816 if (!winfocus
->iconic
&& winfocus
!= cur_focus
)
3819 TAILQ_FOREACH(winfocus
, wl
, entry
)
3820 if (!winfocus
->iconic
&& winfocus
!= cur_focus
)
3827 return get_focus_magic(winfocus
);
3831 get_region_focus(struct swm_region
*r
)
3833 struct ws_win
*winfocus
= NULL
;
3838 if (r
->ws
->focus
&& !r
->ws
->focus
->iconic
)
3839 winfocus
= r
->ws
->focus
;
3840 else if (r
->ws
->focus_prev
&& !r
->ws
->focus_prev
->iconic
)
3841 winfocus
= r
->ws
->focus_prev
;
3843 TAILQ_FOREACH(winfocus
, &r
->ws
->winlist
, entry
)
3844 if (!winfocus
->iconic
)
3847 return get_focus_magic(winfocus
);
3851 focus(struct swm_region
*r
, union arg
*args
)
3853 struct ws_win
*head
, *cur_focus
= NULL
, *winfocus
= NULL
;
3854 struct ws_win_list
*wl
= NULL
;
3855 struct workspace
*ws
= NULL
;
3861 DNPRINTF(SWM_D_FOCUS
, "focus: id: %d\n", args
->id
);
3863 if ((cur_focus
= r
->ws
->focus
) == NULL
)
3867 if (TAILQ_EMPTY(wl
))
3869 /* make sure there is at least one uniconified window */
3871 TAILQ_FOREACH(winfocus
, wl
, entry
)
3872 if (!winfocus
->iconic
) {
3880 case SWM_ARG_ID_FOCUSPREV
:
3881 head
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
3883 head
= TAILQ_LAST(wl
, ws_win_list
);
3885 if (WINID(winfocus
) == cur_focus
->transient
) {
3886 head
= TAILQ_PREV(winfocus
, ws_win_list
, entry
);
3888 head
= TAILQ_LAST(wl
, ws_win_list
);
3893 if (winfocus
&& winfocus
->iconic
) {
3894 while (winfocus
!= cur_focus
) {
3895 if (winfocus
== NULL
)
3896 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
3897 if (!winfocus
->iconic
)
3899 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
,
3905 case SWM_ARG_ID_FOCUSNEXT
:
3906 head
= TAILQ_NEXT(cur_focus
, entry
);
3908 head
= TAILQ_FIRST(wl
);
3912 if (winfocus
&& winfocus
->iconic
) {
3913 while (winfocus
!= cur_focus
) {
3914 if (winfocus
== NULL
)
3915 winfocus
= TAILQ_FIRST(wl
);
3916 if (!winfocus
->iconic
)
3918 winfocus
= TAILQ_NEXT(winfocus
, entry
);
3923 case SWM_ARG_ID_FOCUSMAIN
:
3924 winfocus
= TAILQ_FIRST(wl
);
3925 if (winfocus
== cur_focus
)
3926 winfocus
= cur_focus
->ws
->focus_prev
;
3933 focus_win(get_focus_magic(winfocus
));
3939 cycle_layout(struct swm_region
*r
, union arg
*args
)
3941 struct workspace
*ws
= r
->ws
;
3943 /* suppress unused warning since var is needed */
3946 DNPRINTF(SWM_D_EVENT
, "cycle_layout: workspace: %d\n", ws
->idx
);
3949 if (ws
->cur_layout
->l_stack
== NULL
)
3950 ws
->cur_layout
= &layouts
[0];
3955 focus_win(get_region_focus(r
));
3961 stack_config(struct swm_region
*r
, union arg
*args
)
3963 struct workspace
*ws
= r
->ws
;
3965 DNPRINTF(SWM_D_STACK
, "stack_config: id: %d workspace: %d\n",
3968 if (ws
->cur_layout
->l_config
!= NULL
)
3969 ws
->cur_layout
->l_config(ws
, args
->id
);
3971 if (args
->id
!= SWM_ARG_ID_STACKINIT
)
3980 struct swm_geometry g
;
3981 struct swm_region
*r
;
3987 DNPRINTF(SWM_D_STACK
, "stack: begin\n");
3989 num_screens
= get_screen_count();
3990 for (i
= 0; i
< num_screens
; i
++) {
3994 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
3995 DNPRINTF(SWM_D_STACK
, "stack: workspace: %d "
3996 "(screen: %d, region: %d)\n", r
->ws
->idx
, i
, j
++);
3998 /* Adjust stack area for region bar and padding. */
4000 g
.x
+= region_padding
;
4001 g
.y
+= region_padding
;
4002 g
.w
-= 2 * border_width
+ 2 * region_padding
;
4003 g
.h
-= 2 * border_width
+ 2 * region_padding
;
4004 if (bar_enabled
&& r
->ws
->bar_enabled
) {
4009 r
->ws
->cur_layout
->l_stack(r
->ws
, &g
);
4010 r
->ws
->cur_layout
->l_string(r
->ws
);
4011 /* save r so we can track region changes */
4018 DNPRINTF(SWM_D_STACK
, "stack: end\n");
4022 store_float_geom(struct ws_win
*win
, struct swm_region
*r
)
4024 if (win
== NULL
|| r
== NULL
)
4027 /* retain window geom and region geom */
4028 win
->g_float
= win
->g
;
4029 win
->g_float
.x
-= X(r
);
4030 win
->g_float
.y
-= Y(r
);
4031 win
->g_floatvalid
= 1;
4032 DNPRINTF(SWM_D_MISC
, "store_float_geom: window: 0x%x, g: (%d,%d)"
4033 " %d x %d, g_float: (%d,%d) %d x %d\n", win
->id
, X(win
), Y(win
),
4034 WIDTH(win
), HEIGHT(win
), win
->g_float
.x
, win
->g_float
.y
,
4035 win
->g_float
.w
, win
->g_float
.h
);
4039 load_float_geom(struct ws_win
*win
, struct swm_region
*r
)
4041 if (win
== NULL
|| r
== NULL
)
4044 if (win
->g_floatvalid
) {
4045 win
->g
= win
->g_float
;
4048 DNPRINTF(SWM_D_MISC
, "load_float_geom: window: 0x%x, g: (%d,%d)"
4049 "%d x %d\n", win
->id
, X(win
), Y(win
), WIDTH(win
),
4052 DNPRINTF(SWM_D_MISC
, "load_float_geom: window: 0x%x, g_float "
4053 "is not set.\n", win
->id
);
4058 stack_floater(struct ws_win
*win
, struct swm_region
*r
)
4063 DNPRINTF(SWM_D_MISC
, "stack_floater: window: 0x%x\n", win
->id
);
4066 * to allow windows to change their size (e.g. mplayer fs) only retrieve
4067 * geom on ws switches or return from max mode
4069 if (win
->floatmaxed
|| (r
!= r
->ws
->old_r
&&
4070 !(win
->ewmh_flags
& EWMH_F_FULLSCREEN
))) {
4071 /* update geometry for the new region */
4072 load_float_geom(win
, r
);
4075 win
->floatmaxed
= 0;
4078 * if set to fullscreen mode, configure window to maximum size.
4080 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
4081 if (!win
->g_floatvalid
)
4082 store_float_geom(win
, win
->ws
->r
);
4088 * remove border on fullscreen floater when in fullscreen mode or when
4089 * the quirk is present.
4091 if ((win
->ewmh_flags
& EWMH_F_FULLSCREEN
) ||
4092 ((win
->quirks
& SWM_Q_FULLSCREEN
) &&
4093 (WIDTH(win
) >= WIDTH(r
)) && (HEIGHT(win
) >= HEIGHT(r
)))) {
4094 if (win
->bordered
) {
4096 X(win
) += border_width
;
4097 Y(win
) += border_width
;
4099 } else if (!win
->bordered
) {
4101 X(win
) -= border_width
;
4102 Y(win
) -= border_width
;
4105 if (win
->transient
&& (win
->quirks
& SWM_Q_TRANSSZ
)) {
4106 WIDTH(win
) = (double)WIDTH(r
) * dialog_ratio
;
4107 HEIGHT(win
) = (double)HEIGHT(r
) * dialog_ratio
;
4110 if (!win
->manual
&& !(win
->ewmh_flags
& EWMH_F_FULLSCREEN
) &&
4111 !(win
->quirks
& SWM_Q_ANYWHERE
)) {
4113 * floaters and transients are auto-centred unless moved,
4114 * resized or ANYWHERE quirk is set.
4116 X(win
) = X(r
) + (WIDTH(r
) - WIDTH(win
)) / 2 - BORDER(win
);
4117 Y(win
) = Y(r
) + (HEIGHT(r
) - HEIGHT(win
)) / 2 - BORDER(win
);
4119 store_float_geom(win
, r
);
4122 /* keep window within region bounds */
4123 constrain_window(win
, r
, 0);
4129 * Send keystrokes to terminal to decrease/increase the font size as the
4130 * window size changes.
4133 adjust_font(struct ws_win
*win
)
4135 if (!(win
->quirks
& SWM_Q_XTERM_FONTADJ
) ||
4136 win
->floating
|| win
->transient
)
4139 if (win
->sh
.width_inc
&& win
->last_inc
!= win
->sh
.width_inc
&&
4140 WIDTH(win
) / win
->sh
.width_inc
< term_width
&&
4141 win
->font_steps
< SWM_MAX_FONT_STEPS
) {
4142 win
->font_size_boundary
[win
->font_steps
] =
4143 (win
->sh
.width_inc
* term_width
) + win
->sh
.base_width
;
4146 win
->last_inc
= win
->sh
.width_inc
;
4147 fake_keypress(win
, XK_KP_Subtract
, XCB_MOD_MASK_SHIFT
);
4148 } else if (win
->font_steps
&& win
->last_inc
!= win
->sh
.width_inc
&&
4149 WIDTH(win
) > win
->font_size_boundary
[win
->font_steps
- 1]) {
4152 win
->last_inc
= win
->sh
.width_inc
;
4153 fake_keypress(win
, XK_KP_Add
, XCB_MOD_MASK_SHIFT
);
4157 #define SWAPXY(g) do { \
4159 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
4160 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
4163 stack_master(struct workspace
*ws
, struct swm_geometry
*g
, int rot
, int flip
)
4165 struct swm_geometry win_g
, r_g
= *g
;
4166 struct ws_win
*win
, *fs_win
= NULL
;
4167 int i
, j
, s
, stacks
;
4168 int w_inc
= 1, h_inc
, w_base
= 1, h_base
;
4169 int hrh
, extra
= 0, h_slice
, last_h
= 0;
4170 int split
, colno
, winno
, mwin
, msize
, mscale
;
4171 int remain
, missing
, v_slice
, reconfigure
= 0;
4174 DNPRINTF(SWM_D_STACK
, "stack_master: workspace: %d, rot: %s, "
4175 "flip: %s\n", ws
->idx
, YESNO(rot
), YESNO(flip
));
4177 winno
= count_win(ws
, 0);
4178 if (winno
== 0 && count_win(ws
, 1) == 0)
4181 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
4182 if (!win
->transient
&& !win
->floating
&& !win
->iconic
)
4189 w_inc
= win
->sh
.width_inc
;
4190 w_base
= win
->sh
.base_width
;
4191 mwin
= ws
->l_state
.horizontal_mwin
;
4192 mscale
= ws
->l_state
.horizontal_msize
;
4193 stacks
= ws
->l_state
.horizontal_stacks
;
4196 w_inc
= win
->sh
.height_inc
;
4197 w_base
= win
->sh
.base_height
;
4198 mwin
= ws
->l_state
.vertical_mwin
;
4199 mscale
= ws
->l_state
.vertical_msize
;
4200 stacks
= ws
->l_state
.vertical_stacks
;
4204 if (stacks
> winno
- mwin
)
4205 stacks
= winno
- mwin
;
4209 h_slice
= r_g
.h
/ SWM_H_SLICE
;
4210 if (mwin
&& winno
> mwin
) {
4211 v_slice
= r_g
.w
/ SWM_V_SLICE
;
4215 win_g
.w
= v_slice
* mscale
;
4217 if (w_inc
> 1 && w_inc
< v_slice
) {
4218 /* adjust for window's requested size increment */
4219 remain
= (win_g
.w
- w_base
) % w_inc
;
4225 win_g
.x
+= r_g
.w
- msize
;
4228 colno
= split
= winno
/ stacks
;
4229 win_g
.w
= ((r_g
.w
- (stacks
* 2 * border_width
) +
4230 2 * border_width
) / stacks
);
4232 hrh
= r_g
.h
/ colno
;
4233 extra
= r_g
.h
- (colno
* hrh
);
4234 win_g
.h
= hrh
- 2 * border_width
;
4236 /* stack all the tiled windows */
4237 i
= j
= 0, s
= stacks
;
4238 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
4239 if (win
->transient
|| win
->floating
|| win
->iconic
)
4242 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
4247 if (split
&& i
== split
) {
4248 colno
= (winno
- mwin
) / stacks
;
4249 if (s
<= (winno
- mwin
) % stacks
)
4251 split
= split
+ colno
;
4252 hrh
= (r_g
.h
/ colno
);
4253 extra
= r_g
.h
- (colno
* hrh
);
4257 win_g
.x
+= win_g
.w
+ 2 * border_width
+
4259 win_g
.w
= (r_g
.w
- msize
-
4260 (stacks
* (2 * border_width
+ tile_gap
))) / stacks
;
4262 win_g
.w
+= (r_g
.w
- msize
-
4263 (stacks
* (2 * border_width
+ tile_gap
))) %
4269 win_g
.h
= hrh
- 2 * border_width
- tile_gap
;
4272 h_inc
= win
->sh
.width_inc
;
4273 h_base
= win
->sh
.base_width
;
4275 h_inc
= win
->sh
.height_inc
;
4276 h_base
= win
->sh
.base_height
;
4279 if (j
== colno
- 1) {
4280 win_g
.h
= hrh
+ extra
;
4281 } else if (h_inc
> 1 && h_inc
< h_slice
) {
4282 /* adjust for window's requested size increment */
4283 remain
= (win_g
.h
- h_base
) % h_inc
;
4284 missing
= h_inc
- remain
;
4286 if (missing
<= extra
|| j
== 0) {
4298 win_g
.y
+= last_h
+ 2 * border_width
+ tile_gap
;
4300 if (disable_border
&& !(bar_enabled
&& ws
->bar_enabled
) &&
4303 win_g
.w
+= 2 * border_width
;
4304 win_g
.h
+= 2 * border_width
;
4310 if (X(win
) != win_g
.y
|| Y(win
) != win_g
.x
||
4311 WIDTH(win
) != win_g
.h
|| HEIGHT(win
) != win_g
.w
) {
4315 WIDTH(win
) = win_g
.h
;
4316 HEIGHT(win
) = win_g
.w
;
4319 if (X(win
) != win_g
.x
|| Y(win
) != win_g
.y
||
4320 WIDTH(win
) != win_g
.w
|| HEIGHT(win
) != win_g
.h
) {
4324 WIDTH(win
) = win_g
.w
;
4325 HEIGHT(win
) = win_g
.h
;
4329 if (bordered
!= win
->bordered
) {
4331 win
->bordered
= bordered
;
4347 /* now, stack all the floaters and transients */
4348 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
4349 if (!win
->transient
&& !win
->floating
)
4353 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
4358 stack_floater(win
, ws
->r
);
4362 /* Make sure fs_win is stacked last so it's on top. */
4364 stack_floater(fs_win
, ws
->r
);
4370 vertical_config(struct workspace
*ws
, int id
)
4372 DNPRINTF(SWM_D_STACK
, "vertical_config: id: %d, workspace: %d\n",
4376 case SWM_ARG_ID_STACKRESET
:
4377 case SWM_ARG_ID_STACKINIT
:
4378 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ 2;
4379 ws
->l_state
.vertical_mwin
= 1;
4380 ws
->l_state
.vertical_stacks
= 1;
4382 case SWM_ARG_ID_MASTERSHRINK
:
4383 if (ws
->l_state
.vertical_msize
> 1)
4384 ws
->l_state
.vertical_msize
--;
4386 case SWM_ARG_ID_MASTERGROW
:
4387 if (ws
->l_state
.vertical_msize
< SWM_V_SLICE
- 1)
4388 ws
->l_state
.vertical_msize
++;
4390 case SWM_ARG_ID_MASTERADD
:
4391 ws
->l_state
.vertical_mwin
++;
4393 case SWM_ARG_ID_MASTERDEL
:
4394 if (ws
->l_state
.vertical_mwin
> 0)
4395 ws
->l_state
.vertical_mwin
--;
4397 case SWM_ARG_ID_STACKINC
:
4398 ws
->l_state
.vertical_stacks
++;
4400 case SWM_ARG_ID_STACKDEC
:
4401 if (ws
->l_state
.vertical_stacks
> 1)
4402 ws
->l_state
.vertical_stacks
--;
4404 case SWM_ARG_ID_FLIPLAYOUT
:
4405 ws
->l_state
.vertical_flip
= !ws
->l_state
.vertical_flip
;
4413 vertical_stack(struct workspace
*ws
, struct swm_geometry
*g
)
4415 DNPRINTF(SWM_D_STACK
, "vertical_stack: workspace: %d\n", ws
->idx
);
4417 stack_master(ws
, g
, 0, ws
->l_state
.vertical_flip
);
4421 horizontal_config(struct workspace
*ws
, int id
)
4423 DNPRINTF(SWM_D_STACK
, "horizontal_config: workspace: %d\n", ws
->idx
);
4426 case SWM_ARG_ID_STACKRESET
:
4427 case SWM_ARG_ID_STACKINIT
:
4428 ws
->l_state
.horizontal_mwin
= 1;
4429 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ 2;
4430 ws
->l_state
.horizontal_stacks
= 1;
4432 case SWM_ARG_ID_MASTERSHRINK
:
4433 if (ws
->l_state
.horizontal_msize
> 1)
4434 ws
->l_state
.horizontal_msize
--;
4436 case SWM_ARG_ID_MASTERGROW
:
4437 if (ws
->l_state
.horizontal_msize
< SWM_H_SLICE
- 1)
4438 ws
->l_state
.horizontal_msize
++;
4440 case SWM_ARG_ID_MASTERADD
:
4441 ws
->l_state
.horizontal_mwin
++;
4443 case SWM_ARG_ID_MASTERDEL
:
4444 if (ws
->l_state
.horizontal_mwin
> 0)
4445 ws
->l_state
.horizontal_mwin
--;
4447 case SWM_ARG_ID_STACKINC
:
4448 ws
->l_state
.horizontal_stacks
++;
4450 case SWM_ARG_ID_STACKDEC
:
4451 if (ws
->l_state
.horizontal_stacks
> 1)
4452 ws
->l_state
.horizontal_stacks
--;
4454 case SWM_ARG_ID_FLIPLAYOUT
:
4455 ws
->l_state
.horizontal_flip
= !ws
->l_state
.horizontal_flip
;
4463 horizontal_stack(struct workspace
*ws
, struct swm_geometry
*g
)
4465 DNPRINTF(SWM_D_STACK
, "horizontal_stack: workspace: %d\n", ws
->idx
);
4467 stack_master(ws
, g
, 1, ws
->l_state
.horizontal_flip
);
4470 /* fullscreen view */
4472 max_stack(struct workspace
*ws
, struct swm_geometry
*g
)
4474 struct swm_geometry gg
= *g
;
4475 struct ws_win
*w
, *win
= NULL
, *parent
= NULL
;
4478 DNPRINTF(SWM_D_STACK
, "max_stack: workspace: %d\n", ws
->idx
);
4483 winno
= count_win(ws
, 0);
4484 if (winno
== 0 && count_win(ws
, 1) == 0)
4487 /* Figure out which top level window should be visible. */
4488 if (ws
->focus_pending
)
4489 win
= ws
->focus_pending
;
4493 win
= TAILQ_FIRST(&ws
->winlist
);
4496 parent
= find_window(win
->transient
);
4498 DNPRINTF(SWM_D_STACK
, "max_stack: win: 0x%x\n", win
->id
);
4500 /* maximize all top level windows */
4501 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
4502 if (w
->transient
|| w
->iconic
)
4505 if (!w
->mapped
&& w
!= win
)
4508 if (w
->floating
&& !w
->floatmaxed
) {
4510 * retain geometry for retrieval on exit from
4513 store_float_geom(w
, ws
->r
);
4517 /* only reconfigure if necessary */
4518 if (X(w
) != gg
.x
|| Y(w
) != gg
.y
|| WIDTH(w
) != gg
.w
||
4519 HEIGHT(w
) != gg
.h
) {
4521 if (bar_enabled
&& ws
->bar_enabled
){
4525 WIDTH(w
) += 2 * border_width
;
4526 HEIGHT(w
) += 2 * border_width
;
4533 /* If a parent exists, map/raise it first. */
4537 /* Map siblings next. */
4538 TAILQ_FOREACH(w
, &ws
->winlist
, entry
)
4539 if (w
!= win
&& !w
->iconic
&&
4540 w
->transient
== parent
->id
) {
4541 stack_floater(w
, ws
->r
);
4546 /* Map/raise focused window. */
4549 /* Finally, map/raise children of focus window. */
4550 TAILQ_FOREACH(w
, &ws
->winlist
, entry
)
4551 if (w
->transient
== win
->id
&& !w
->iconic
) {
4552 stack_floater(w
, ws
->r
);
4558 send_to_rg(struct swm_region
*r
, union arg
*args
)
4560 int ridx
= args
->id
, i
, num_screens
;
4561 struct swm_region
*rr
= NULL
;
4564 num_screens
= get_screen_count();
4565 /* do nothing if we don't have more than one screen */
4566 if (!(num_screens
> 1 || outputs
> 1))
4569 DNPRINTF(SWM_D_FOCUS
, "send_to_rg: id: %d\n", ridx
);
4571 rr
= TAILQ_FIRST(&r
->s
->rl
);
4572 for (i
= 0; i
< ridx
; ++i
)
4573 rr
= TAILQ_NEXT(rr
, entry
);
4584 send_to_ws(struct swm_region
*r
, union arg
*args
)
4586 int wsid
= args
->id
;
4587 struct ws_win
*win
= NULL
, *parent
;
4588 struct workspace
*ws
, *nws
, *pws
;
4589 char ws_idx_str
[SWM_PROPLEN
];
4591 if (wsid
>= workspace_limit
)
4594 if (r
&& r
->ws
&& r
->ws
->focus
)
4599 if (win
->ws
->idx
== wsid
)
4602 DNPRINTF(SWM_D_MOVE
, "send_to_ws: win 0x%x, ws %d -> %d\n", win
->id
,
4603 win
->ws
->idx
, wsid
);
4606 nws
= &win
->s
->ws
[wsid
];
4608 /* Update the window's workspace property: _SWM_WS */
4609 if (snprintf(ws_idx_str
, SWM_PROPLEN
, "%d", nws
->idx
) < SWM_PROPLEN
) {
4610 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
4611 ws
->focus_pending
= get_focus_prev(win
);
4613 /* Move the parent if this is a transient window. */
4614 if (win
->transient
) {
4615 parent
= find_window(win
->transient
);
4618 /* Set new focus in parent's ws if needed. */
4619 if (pws
->focus
== parent
) {
4620 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
4621 pws
->focus_pending
=
4622 get_focus_prev(parent
);
4624 unfocus_win(parent
);
4626 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
4627 pws
->focus
= pws
->focus_pending
;
4629 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
4630 pws
->focus_pending
= NULL
;
4633 /* Don't unmap parent if new ws is visible */
4635 unmap_window(parent
);
4638 TAILQ_REMOVE(&ws
->winlist
, parent
, entry
);
4639 TAILQ_INSERT_TAIL(&nws
->winlist
, parent
, entry
);
4642 DNPRINTF(SWM_D_PROP
, "send_to_ws: set "
4643 "property: _SWM_WS: %s\n", ws_idx_str
);
4644 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
4645 parent
->id
, a_swm_ws
, XCB_ATOM_STRING
, 8,
4646 strlen(ws_idx_str
), ws_idx_str
);
4652 /* Don't unmap if new ws is visible */
4657 TAILQ_REMOVE(&ws
->winlist
, win
, entry
);
4658 TAILQ_INSERT_TAIL(&nws
->winlist
, win
, entry
);
4661 /* Set focus on new ws. */
4662 unfocus_win(nws
->focus
);
4665 DNPRINTF(SWM_D_PROP
, "send_to_ws: set property: _SWM_WS: %s\n",
4667 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
4668 a_swm_ws
, XCB_ATOM_STRING
, 8, strlen(ws_idx_str
),
4671 /* Restack and set new focus. */
4674 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
4675 focus_win(ws
->focus_pending
);
4676 ws
->focus_pending
= NULL
;
4682 DNPRINTF(SWM_D_MOVE
, "send_to_ws: done.\n");
4686 pressbutton(struct swm_region
*r
, union arg
*args
)
4688 /* suppress unused warning since var is needed */
4691 xcb_test_fake_input(conn
, XCB_BUTTON_PRESS
, args
->id
,
4692 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
4693 xcb_test_fake_input(conn
, XCB_BUTTON_RELEASE
, args
->id
,
4694 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
4698 raise_toggle(struct swm_region
*r
, union arg
*args
)
4700 /* suppress unused warning since var is needed */
4703 if (r
== NULL
|| r
->ws
== NULL
)
4706 r
->ws
->always_raise
= !r
->ws
->always_raise
;
4708 /* bring floaters back to top */
4709 if (!r
->ws
->always_raise
)
4716 iconify(struct swm_region
*r
, union arg
*args
)
4718 /* suppress unused warning since var is needed */
4721 if (r
->ws
->focus
== NULL
)
4724 set_swm_iconic(r
->ws
->focus
, 1);
4730 get_win_name(xcb_window_t win
)
4733 xcb_get_property_cookie_t c
;
4734 xcb_get_property_reply_t
*r
;
4736 /* First try _NET_WM_NAME for UTF-8. */
4737 c
= xcb_get_property(conn
, 0, win
, a_netwmname
,
4738 XCB_GET_PROPERTY_TYPE_ANY
, 0, UINT_MAX
);
4739 r
= xcb_get_property_reply(conn
, c
, NULL
);
4742 if (r
->type
== XCB_NONE
) {
4744 /* Use WM_NAME instead; no UTF-8. */
4745 c
= xcb_get_property(conn
, 0, win
, XCB_ATOM_WM_NAME
,
4746 XCB_GET_PROPERTY_TYPE_ANY
, 0, UINT_MAX
);
4747 r
= xcb_get_property_reply(conn
, c
, NULL
);
4751 if (r
->type
== XCB_NONE
) {
4757 name
= strndup(xcb_get_property_value(r
),
4758 xcb_get_property_value_length(r
));
4767 uniconify(struct swm_region
*r
, union arg
*args
)
4774 DNPRINTF(SWM_D_MISC
, "uniconify\n");
4776 if (r
== NULL
|| r
->ws
== NULL
)
4779 /* make sure we have anything to uniconify */
4780 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
4781 if (win
->ws
== NULL
)
4782 continue; /* should never happen */
4791 search_resp_action
= SWM_SEARCH_UNICONIFY
;
4793 spawn_select(r
, args
, "search", &searchpid
);
4795 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
4798 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
4799 if (win
->ws
== NULL
)
4800 continue; /* should never happen */
4804 name
= get_win_name(win
->id
);
4807 fprintf(lfile
, "%s.%u\n", name
, win
->id
);
4815 name_workspace(struct swm_region
*r
, union arg
*args
)
4819 DNPRINTF(SWM_D_MISC
, "name_workspace\n");
4825 search_resp_action
= SWM_SEARCH_NAME_WORKSPACE
;
4827 spawn_select(r
, args
, "name_workspace", &searchpid
);
4829 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
4832 fprintf(lfile
, "%s", "");
4837 search_workspace(struct swm_region
*r
, union arg
*args
)
4840 struct workspace
*ws
;
4843 DNPRINTF(SWM_D_MISC
, "search_workspace\n");
4849 search_resp_action
= SWM_SEARCH_SEARCH_WORKSPACE
;
4851 spawn_select(r
, args
, "search", &searchpid
);
4853 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
4856 for (i
= 0; i
< workspace_limit
; i
++) {
4860 fprintf(lfile
, "%d%s%s\n", ws
->idx
+ 1,
4861 (ws
->name
? ":" : ""), (ws
->name
? ws
->name
: ""));
4868 search_win_cleanup(void)
4870 struct search_window
*sw
= NULL
;
4872 while ((sw
= TAILQ_FIRST(&search_wl
)) != NULL
) {
4873 xcb_destroy_window(conn
, sw
->indicator
);
4874 TAILQ_REMOVE(&search_wl
, sw
, entry
);
4880 search_win(struct swm_region
*r
, union arg
*args
)
4882 struct ws_win
*win
= NULL
;
4883 struct search_window
*sw
= NULL
;
4886 int i
, width
, height
;
4894 XRectangle l_ibox
, l_lbox
;
4896 DNPRINTF(SWM_D_MISC
, "search_win\n");
4899 search_resp_action
= SWM_SEARCH_SEARCH_WINDOW
;
4901 spawn_select(r
, args
, "search", &searchpid
);
4903 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
4906 TAILQ_INIT(&search_wl
);
4909 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
4913 sw
= calloc(1, sizeof(struct search_window
));
4915 warn("search_win: calloc");
4917 search_win_cleanup();
4923 snprintf(s
, sizeof s
, "%d", i
);
4926 w
= xcb_generate_id(conn
);
4927 wa
[0] = r
->s
->c
[SWM_S_COLOR_FOCUS
].pixel
;
4928 wa
[1] = r
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
;
4930 if (bar_font_legacy
) {
4931 XmbTextExtents(bar_fs
, s
, len
, &l_ibox
, &l_lbox
);
4932 width
= l_lbox
.width
+ 4;
4933 height
= bar_fs_extents
->max_logical_extent
.height
+ 4;
4935 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
,
4937 width
= info
.width
+ 4;
4938 height
= bar_font
->height
+ 4;
4941 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, w
, win
->id
, 0, 0,
4942 width
, height
, 1, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
4943 XCB_COPY_FROM_PARENT
, XCB_CW_BACK_PIXEL
|
4944 XCB_CW_BORDER_PIXEL
, wa
);
4946 xcb_map_window(conn
, w
);
4949 TAILQ_INSERT_TAIL(&search_wl
, sw
, entry
);
4951 if (bar_font_legacy
) {
4952 l_gcv
.graphics_exposures
= 0;
4953 l_draw
= XCreateGC(display
, w
, 0, &l_gcv
);
4955 XSetForeground(display
, l_draw
,
4956 r
->s
->c
[SWM_S_COLOR_BAR
].pixel
);
4958 DRAWSTRING(display
, w
, bar_fs
, l_draw
, 2,
4959 (bar_fs_extents
->max_logical_extent
.height
-
4960 l_lbox
.height
) / 2 - l_lbox
.y
, s
, len
);
4962 XFreeGC(display
, l_draw
);
4965 draw
= XftDrawCreate(display
, w
,
4966 DefaultVisual(display
, r
->s
->idx
),
4967 DefaultColormap(display
, r
->s
->idx
));
4969 XftDrawStringUtf8(draw
, &bar_font_color
, bar_font
, 2,
4970 (HEIGHT(r
->bar
) + bar_font
->height
) / 2 -
4971 bar_font
->descent
, (FcChar8
*)s
, len
);
4973 XftDrawDestroy(draw
);
4976 DNPRINTF(SWM_D_MISC
, "search_win: mapped window: 0x%x\n", w
);
4978 fprintf(lfile
, "%d\n", i
);
4988 search_resp_uniconify(const char *resp
, unsigned long len
)
4994 DNPRINTF(SWM_D_MISC
, "search_resp_uniconify: resp: %s\n", resp
);
4996 TAILQ_FOREACH(win
, &search_r
->ws
->winlist
, entry
) {
4999 name
= get_win_name(win
->id
);
5002 if (asprintf(&s
, "%s.%u", name
, win
->id
) == -1) {
5007 if (strncmp(s
, resp
, len
) == 0) {
5008 /* XXX this should be a callback to generalize */
5009 set_swm_iconic(win
, 0);
5018 search_resp_name_workspace(const char *resp
, unsigned long len
)
5020 struct workspace
*ws
;
5022 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: resp: %s\n", resp
);
5024 if (search_r
->ws
== NULL
)
5029 free(search_r
->ws
->name
);
5030 search_r
->ws
->name
= NULL
;
5034 ws
->name
= strdup(resp
);
5035 if (ws
->name
== NULL
) {
5036 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: "
5037 "strdup: %s", strerror(errno
));
5044 search_resp_search_workspace(const char *resp
)
5051 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: resp: %s\n", resp
);
5055 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: strdup: %s",
5062 ws_idx
= (int)strtonum(q
, 1, workspace_limit
, &errstr
);
5064 DNPRINTF(SWM_D_MISC
, "workspace idx is %s: %s",
5071 switchws(search_r
, &a
);
5075 search_resp_search_window(const char *resp
)
5080 struct search_window
*sw
;
5082 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: resp: %s\n", resp
);
5086 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: strdup: %s",
5091 idx
= (int)strtonum(s
, 1, INT_MAX
, &errstr
);
5093 DNPRINTF(SWM_D_MISC
, "window idx is %s: %s",
5100 TAILQ_FOREACH(sw
, &search_wl
, entry
)
5101 if (idx
== sw
->idx
) {
5107 #define MAX_RESP_LEN 1024
5110 search_do_resp(void)
5116 DNPRINTF(SWM_D_MISC
, "search_do_resp:\n");
5121 if ((resp
= calloc(1, MAX_RESP_LEN
+ 1)) == NULL
) {
5122 warn("search: calloc");
5126 rbytes
= read(select_resp_pipe
[0], resp
, MAX_RESP_LEN
);
5128 warn("search: read error");
5131 resp
[rbytes
] = '\0';
5134 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
5135 * newline, so work around that by sanitizing the resp now.
5137 resp
[strcspn(resp
, "\n")] = '\0';
5140 switch (search_resp_action
) {
5141 case SWM_SEARCH_UNICONIFY
:
5142 search_resp_uniconify(resp
, len
);
5144 case SWM_SEARCH_NAME_WORKSPACE
:
5145 search_resp_name_workspace(resp
, len
);
5147 case SWM_SEARCH_SEARCH_WORKSPACE
:
5148 search_resp_search_workspace(resp
);
5150 case SWM_SEARCH_SEARCH_WINDOW
:
5151 search_resp_search_window(resp
);
5156 if (search_resp_action
== SWM_SEARCH_SEARCH_WINDOW
)
5157 search_win_cleanup();
5159 search_resp_action
= SWM_SEARCH_NONE
;
5160 close(select_resp_pipe
[0]);
5167 wkill(struct swm_region
*r
, union arg
*args
)
5169 DNPRINTF(SWM_D_MISC
, "wkill: id: %d\n", args
->id
);
5171 if (r
->ws
->focus
== NULL
)
5174 if (args
->id
== SWM_ARG_ID_KILLWINDOW
)
5175 xcb_kill_client(conn
, r
->ws
->focus
->id
);
5177 if (r
->ws
->focus
->can_delete
)
5178 client_msg(r
->ws
->focus
, a_delete
, 0);
5184 floating_toggle_win(struct ws_win
*win
)
5186 struct swm_region
*r
;
5196 /* reject floating toggles in max stack mode */
5197 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
5200 if (win
->floating
) {
5201 if (!win
->floatmaxed
) {
5202 /* retain position for refloat */
5203 store_float_geom(win
, r
);
5207 load_float_geom(win
, r
);
5211 ewmh_update_actions(win
);
5217 floating_toggle(struct swm_region
*r
, union arg
*args
)
5219 struct ws_win
*win
= r
->ws
->focus
;
5221 /* suppress unused warning since var is needed */
5227 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
5230 ewmh_update_win_state(win
, ewmh
[_NET_WM_STATE_ABOVE
].atom
,
5231 _NET_WM_STATE_TOGGLE
);
5235 if (win
== win
->ws
->focus
)
5242 constrain_window(struct ws_win
*win
, struct swm_region
*r
, int resizable
)
5244 if (MAX_X(win
) + BORDER(win
) > MAX_X(r
)) {
5246 WIDTH(win
) = MAX_X(r
) - X(win
) - BORDER(win
);
5248 X(win
) = MAX_X(r
)- WIDTH(win
) - BORDER(win
);
5251 if (X(win
) + BORDER(win
) < X(r
)) {
5253 WIDTH(win
) -= X(r
) - X(win
) - BORDER(win
);
5255 X(win
) = X(r
) - BORDER(win
);
5258 if (MAX_Y(win
) + BORDER(win
) > MAX_Y(r
)) {
5260 HEIGHT(win
) = MAX_Y(r
) - Y(win
) - BORDER(win
);
5262 Y(win
) = MAX_Y(r
) - HEIGHT(win
) - BORDER(win
);
5265 if (Y(win
) + BORDER(win
) < Y(r
)) {
5267 HEIGHT(win
) -= Y(r
) - Y(win
) - BORDER(win
);
5269 Y(win
) = Y(r
) - BORDER(win
);
5275 if (HEIGHT(win
) < 1)
5281 update_window(struct ws_win
*win
)
5286 mask
= XCB_CONFIG_WINDOW_X
| XCB_CONFIG_WINDOW_Y
|
5287 XCB_CONFIG_WINDOW_WIDTH
| XCB_CONFIG_WINDOW_HEIGHT
|
5288 XCB_CONFIG_WINDOW_BORDER_WIDTH
;
5292 wc
[3] = HEIGHT(win
);
5293 wc
[4] = BORDER(win
);
5295 DNPRINTF(SWM_D_EVENT
, "update_window: window: 0x%x, (x,y) w x h: "
5296 "(%d,%d) %d x %d, bordered: %s\n", win
->id
, wc
[0], wc
[1], wc
[2],
5297 wc
[3], YESNO(win
->bordered
));
5299 xcb_configure_window(conn
, win
->id
, mask
, wc
);
5302 #define SWM_RESIZE_STEPS (50)
5305 resize(struct ws_win
*win
, union arg
*args
)
5307 xcb_timestamp_t timestamp
= 0;
5308 struct swm_region
*r
= NULL
;
5310 struct swm_geometry g
;
5311 int top
= 0, left
= 0, resizing
;
5313 xcb_cursor_t cursor
;
5314 xcb_query_pointer_reply_t
*xpr
= NULL
;
5315 xcb_generic_event_t
*evt
;
5316 xcb_motion_notify_event_t
*mne
;
5322 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
5325 DNPRINTF(SWM_D_EVENT
, "resize: window: 0x%x, floating: %s, "
5326 "transient: 0x%x\n", win
->id
, YESNO(win
->floating
),
5329 if (!win
->transient
&& !win
->floating
)
5332 /* reject resizes in max mode for floaters (transient ok) */
5333 if (win
->floatmaxed
)
5337 ewmh_update_win_state(win
, ewmh
[_SWM_WM_STATE_MANUAL
].atom
,
5344 /* It's possible for win to have been freed during focus_flush(). */
5345 if (validate_win(win
)) {
5346 DNPRINTF(SWM_D_EVENT
, "move: invalid win.\n");
5351 case SWM_ARG_ID_WIDTHSHRINK
:
5352 WIDTH(win
) -= SWM_RESIZE_STEPS
;
5355 case SWM_ARG_ID_WIDTHGROW
:
5356 WIDTH(win
) += SWM_RESIZE_STEPS
;
5359 case SWM_ARG_ID_HEIGHTSHRINK
:
5360 HEIGHT(win
) -= SWM_RESIZE_STEPS
;
5363 case SWM_ARG_ID_HEIGHTGROW
:
5364 HEIGHT(win
) += SWM_RESIZE_STEPS
;
5371 constrain_window(win
, r
, 1);
5373 store_float_geom(win
,r
);
5377 /* get cursor offset from window root */
5378 xpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
5385 if (xpr
->win_x
< WIDTH(win
) / 2)
5388 if (xpr
->win_y
< HEIGHT(win
) / 2)
5391 if (args
->id
== SWM_ARG_ID_CENTER
)
5392 cursor
= cursors
[XC_SIZING
].cid
;
5394 cursor
= cursors
[left
? XC_TOP_LEFT_CORNER
:
5395 XC_TOP_RIGHT_CORNER
].cid
;
5397 cursor
= cursors
[left
? XC_BOTTOM_LEFT_CORNER
:
5398 XC_BOTTOM_RIGHT_CORNER
].cid
;
5400 xcb_grab_pointer(conn
, 0, win
->id
, MOUSEMASK
,
5401 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
, XCB_WINDOW_NONE
, cursor
,
5406 while (resizing
&& (evt
= xcb_wait_for_event(conn
))) {
5407 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
5408 case XCB_BUTTON_RELEASE
:
5409 DNPRINTF(SWM_D_EVENT
, "resize: BUTTON_RELEASE\n");
5412 case XCB_MOTION_NOTIFY
:
5413 mne
= (xcb_motion_notify_event_t
*)evt
;
5414 /* cursor offset/delta from start of the operation */
5415 dx
= mne
->root_x
- xpr
->root_x
;
5416 dy
= mne
->root_y
- xpr
->root_y
;
5422 if (args
->id
== SWM_ARG_ID_CENTER
) {
5423 if (g
.h
/ 2 + dy
< 1)
5427 HEIGHT(win
) = g
.h
+ 2 * dy
;
5435 HEIGHT(win
) = g
.h
+ dy
;
5442 if (args
->id
== SWM_ARG_ID_CENTER
) {
5443 if (g
.w
/ 2 + dx
< 1)
5447 WIDTH(win
) = g
.w
+ 2 * dx
;
5455 WIDTH(win
) = g
.w
+ dx
;
5458 constrain_window(win
, r
, 1);
5460 /* not free, don't sync more than 120 times / second */
5461 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
5462 timestamp
= mne
->time
;
5470 /* It's possible for win to have been freed above. */
5471 if (validate_win(win
)) {
5472 DNPRINTF(SWM_D_EVENT
, "move: invalid win.\n");
5483 store_float_geom(win
,r
);
5485 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
5487 DNPRINTF(SWM_D_EVENT
, "resize: done.\n");
5491 resize_step(struct swm_region
*r
, union arg
*args
)
5493 struct ws_win
*win
= NULL
;
5495 if (r
&& r
->ws
&& r
->ws
->focus
)
5504 #define SWM_MOVE_STEPS (50)
5507 move(struct ws_win
*win
, union arg
*args
)
5509 xcb_timestamp_t timestamp
= 0;
5510 int move_stp
= 0, moving
;
5511 struct swm_region
*r
= NULL
;
5512 xcb_query_pointer_reply_t
*qpr
= NULL
;
5513 xcb_generic_event_t
*evt
;
5514 xcb_motion_notify_event_t
*mne
;
5520 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
5523 DNPRINTF(SWM_D_EVENT
, "move: window: 0x%x, floating: %s, transient: "
5524 "0x%x\n", win
->id
, YESNO(win
->floating
), win
->transient
);
5526 /* in max_stack mode should only move transients */
5527 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
] && !win
->transient
)
5531 if (!win
->floating
&& !win
->transient
) {
5532 store_float_geom(win
, r
);
5533 ewmh_update_win_state(win
, ewmh
[_NET_WM_STATE_ABOVE
].atom
,
5536 ewmh_update_win_state(win
, ewmh
[_SWM_WM_STATE_MANUAL
].atom
,
5543 /* It's possible for win to have been freed during focus_flush(). */
5544 if (validate_win(win
)) {
5545 DNPRINTF(SWM_D_EVENT
, "move: invalid win.\n");
5551 case SWM_ARG_ID_MOVELEFT
:
5552 X(win
) -= (SWM_MOVE_STEPS
- border_width
);
5555 case SWM_ARG_ID_MOVERIGHT
:
5556 X(win
) += (SWM_MOVE_STEPS
- border_width
);
5559 case SWM_ARG_ID_MOVEUP
:
5560 Y(win
) -= (SWM_MOVE_STEPS
- border_width
);
5563 case SWM_ARG_ID_MOVEDOWN
:
5564 Y(win
) += (SWM_MOVE_STEPS
- border_width
);
5571 constrain_window(win
, r
, 0);
5573 store_float_geom(win
, r
);
5577 xcb_grab_pointer(conn
, 0, win
->id
, MOUSEMASK
,
5578 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
,
5579 XCB_WINDOW_NONE
, cursors
[XC_FLEUR
].cid
, XCB_CURRENT_TIME
);
5581 /* get cursor offset from window root */
5582 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
5585 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
5591 while (moving
&& (evt
= xcb_wait_for_event(conn
))) {
5592 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
5593 case XCB_BUTTON_RELEASE
:
5594 DNPRINTF(SWM_D_EVENT
, "move: BUTTON_RELEASE\n");
5597 case XCB_MOTION_NOTIFY
:
5598 mne
= (xcb_motion_notify_event_t
*)evt
;
5599 X(win
) = mne
->root_x
- qpr
->win_x
- border_width
;
5600 Y(win
) = mne
->root_y
- qpr
->win_y
- border_width
;
5602 constrain_window(win
, r
, 0);
5604 /* not free, don't sync more than 120 times / second */
5605 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
5606 timestamp
= mne
->time
;
5614 /* It's possible for win to have been freed above. */
5615 if (validate_win(win
)) {
5616 DNPRINTF(SWM_D_EVENT
, "move: invalid win.\n");
5627 store_float_geom(win
, r
);
5630 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
5631 DNPRINTF(SWM_D_EVENT
, "move: done.\n");
5635 move_step(struct swm_region
*r
, union arg
*args
)
5637 struct ws_win
*win
= NULL
;
5639 if (r
&& r
->ws
&& r
->ws
->focus
)
5644 if (!win
->transient
&& !win
->floating
)
5651 /* key definitions */
5653 char name
[SWM_FUNCNAME_LEN
];
5654 void (*func
)(struct swm_region
*r
, union arg
*);
5656 } keyfuncs
[KF_INVALID
+ 1] = {
5657 /* name function argument */
5658 { "bar_toggle", bar_toggle
, {.id
= SWM_ARG_ID_BAR_TOGGLE
} },
5659 { "bar_toggle_ws", bar_toggle
, {.id
= SWM_ARG_ID_BAR_TOGGLE_WS
} },
5660 { "button2", pressbutton
, {2} },
5661 { "cycle_layout", cycle_layout
, {0} },
5662 { "flip_layout", stack_config
, {.id
= SWM_ARG_ID_FLIPLAYOUT
} },
5663 { "float_toggle", floating_toggle
,{0} },
5664 { "focus_main", focus
, {.id
= SWM_ARG_ID_FOCUSMAIN
} },
5665 { "focus_next", focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
5666 { "focus_prev", focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
5667 { "height_grow", resize_step
, {.id
= SWM_ARG_ID_HEIGHTGROW
} },
5668 { "height_shrink", resize_step
, {.id
= SWM_ARG_ID_HEIGHTSHRINK
} },
5669 { "iconify", iconify
, {0} },
5670 { "master_shrink", stack_config
, {.id
= SWM_ARG_ID_MASTERSHRINK
} },
5671 { "master_grow", stack_config
, {.id
= SWM_ARG_ID_MASTERGROW
} },
5672 { "master_add", stack_config
, {.id
= SWM_ARG_ID_MASTERADD
} },
5673 { "master_del", stack_config
, {.id
= SWM_ARG_ID_MASTERDEL
} },
5674 { "move_down", move_step
, {.id
= SWM_ARG_ID_MOVEDOWN
} },
5675 { "move_left", move_step
, {.id
= SWM_ARG_ID_MOVELEFT
} },
5676 { "move_right", move_step
, {.id
= SWM_ARG_ID_MOVERIGHT
} },
5677 { "move_up", move_step
, {.id
= SWM_ARG_ID_MOVEUP
} },
5678 { "mvrg_1", send_to_rg
, {.id
= 0} },
5679 { "mvrg_2", send_to_rg
, {.id
= 1} },
5680 { "mvrg_3", send_to_rg
, {.id
= 2} },
5681 { "mvrg_4", send_to_rg
, {.id
= 3} },
5682 { "mvrg_5", send_to_rg
, {.id
= 4} },
5683 { "mvrg_6", send_to_rg
, {.id
= 5} },
5684 { "mvrg_7", send_to_rg
, {.id
= 6} },
5685 { "mvrg_8", send_to_rg
, {.id
= 7} },
5686 { "mvrg_9", send_to_rg
, {.id
= 8} },
5687 { "mvws_1", send_to_ws
, {.id
= 0} },
5688 { "mvws_2", send_to_ws
, {.id
= 1} },
5689 { "mvws_3", send_to_ws
, {.id
= 2} },
5690 { "mvws_4", send_to_ws
, {.id
= 3} },
5691 { "mvws_5", send_to_ws
, {.id
= 4} },
5692 { "mvws_6", send_to_ws
, {.id
= 5} },
5693 { "mvws_7", send_to_ws
, {.id
= 6} },
5694 { "mvws_8", send_to_ws
, {.id
= 7} },
5695 { "mvws_9", send_to_ws
, {.id
= 8} },
5696 { "mvws_10", send_to_ws
, {.id
= 9} },
5697 { "mvws_11", send_to_ws
, {.id
= 10} },
5698 { "mvws_12", send_to_ws
, {.id
= 11} },
5699 { "mvws_13", send_to_ws
, {.id
= 12} },
5700 { "mvws_14", send_to_ws
, {.id
= 13} },
5701 { "mvws_15", send_to_ws
, {.id
= 14} },
5702 { "mvws_16", send_to_ws
, {.id
= 15} },
5703 { "mvws_17", send_to_ws
, {.id
= 16} },
5704 { "mvws_18", send_to_ws
, {.id
= 17} },
5705 { "mvws_19", send_to_ws
, {.id
= 18} },
5706 { "mvws_20", send_to_ws
, {.id
= 19} },
5707 { "mvws_21", send_to_ws
, {.id
= 20} },
5708 { "mvws_22", send_to_ws
, {.id
= 21} },
5709 { "name_workspace", name_workspace
, {0} },
5710 { "quit", quit
, {0} },
5711 { "raise_toggle", raise_toggle
, {0} },
5712 { "restart", restart
, {0} },
5713 { "rg_1", focusrg
, {.id
= 0} },
5714 { "rg_2", focusrg
, {.id
= 1} },
5715 { "rg_3", focusrg
, {.id
= 2} },
5716 { "rg_4", focusrg
, {.id
= 3} },
5717 { "rg_5", focusrg
, {.id
= 4} },
5718 { "rg_6", focusrg
, {.id
= 5} },
5719 { "rg_7", focusrg
, {.id
= 6} },
5720 { "rg_8", focusrg
, {.id
= 7} },
5721 { "rg_9", focusrg
, {.id
= 8} },
5722 { "rg_next", cyclerg
, {.id
= SWM_ARG_ID_CYCLERG_UP
} },
5723 { "rg_prev", cyclerg
, {.id
= SWM_ARG_ID_CYCLERG_DOWN
} },
5724 { "screen_next", cyclerg
, {.id
= SWM_ARG_ID_CYCLERG_UP
} },
5725 { "screen_prev", cyclerg
, {.id
= SWM_ARG_ID_CYCLERG_DOWN
} },
5726 { "search_win", search_win
, {0} },
5727 { "search_workspace", search_workspace
, {0} },
5728 { "spawn_custom", NULL
, {0} },
5729 { "stack_inc", stack_config
, {.id
= SWM_ARG_ID_STACKINC
} },
5730 { "stack_dec", stack_config
, {.id
= SWM_ARG_ID_STACKDEC
} },
5731 { "stack_reset", stack_config
, {.id
= SWM_ARG_ID_STACKRESET
} },
5732 { "swap_main", swapwin
, {.id
= SWM_ARG_ID_SWAPMAIN
} },
5733 { "swap_next", swapwin
, {.id
= SWM_ARG_ID_SWAPNEXT
} },
5734 { "swap_prev", swapwin
, {.id
= SWM_ARG_ID_SWAPPREV
} },
5735 { "uniconify", uniconify
, {0} },
5736 { "version", version
, {0} },
5737 { "width_grow", resize_step
, {.id
= SWM_ARG_ID_WIDTHGROW
} },
5738 { "width_shrink", resize_step
, {.id
= SWM_ARG_ID_WIDTHSHRINK
} },
5739 { "wind_del", wkill
, {.id
= SWM_ARG_ID_DELETEWINDOW
} },
5740 { "wind_kill", wkill
, {.id
= SWM_ARG_ID_KILLWINDOW
} },
5741 { "ws_1", switchws
, {.id
= 0} },
5742 { "ws_2", switchws
, {.id
= 1} },
5743 { "ws_3", switchws
, {.id
= 2} },
5744 { "ws_4", switchws
, {.id
= 3} },
5745 { "ws_5", switchws
, {.id
= 4} },
5746 { "ws_6", switchws
, {.id
= 5} },
5747 { "ws_7", switchws
, {.id
= 6} },
5748 { "ws_8", switchws
, {.id
= 7} },
5749 { "ws_9", switchws
, {.id
= 8} },
5750 { "ws_10", switchws
, {.id
= 9} },
5751 { "ws_11", switchws
, {.id
= 10} },
5752 { "ws_12", switchws
, {.id
= 11} },
5753 { "ws_13", switchws
, {.id
= 12} },
5754 { "ws_14", switchws
, {.id
= 13} },
5755 { "ws_15", switchws
, {.id
= 14} },
5756 { "ws_16", switchws
, {.id
= 15} },
5757 { "ws_17", switchws
, {.id
= 16} },
5758 { "ws_18", switchws
, {.id
= 17} },
5759 { "ws_19", switchws
, {.id
= 18} },
5760 { "ws_20", switchws
, {.id
= 19} },
5761 { "ws_21", switchws
, {.id
= 20} },
5762 { "ws_22", switchws
, {.id
= 21} },
5763 { "ws_next", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP
} },
5764 { "ws_next_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP_ALL
} },
5765 { "ws_prev", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN
} },
5766 { "ws_prev_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN_ALL
} },
5767 { "ws_prior", priorws
, {0} },
5768 { "dumpwins", dumpwins
, {0} }, /* MUST BE LAST */
5769 { "invalid key func", NULL
, {0} },
5773 key_cmp(struct key
*kp1
, struct key
*kp2
)
5775 if (kp1
->keysym
< kp2
->keysym
)
5777 if (kp1
->keysym
> kp2
->keysym
)
5780 if (kp1
->mod
< kp2
->mod
)
5782 if (kp1
->mod
> kp2
->mod
)
5789 enum { client_click
, root_click
};
5791 unsigned int action
;
5793 unsigned int button
;
5794 void (*func
)(struct ws_win
*, union arg
*);
5797 #define MODKEY_SHIFT MODKEY | XCB_MOD_MASK_SHIFT
5798 /* action key mouse button func args */
5799 { client_click
, MODKEY
, XCB_BUTTON_INDEX_3
, resize
, {.id
= SWM_ARG_ID_DONTCENTER
} },
5800 { client_click
, MODKEY_SHIFT
, XCB_BUTTON_INDEX_3
, resize
, {.id
= SWM_ARG_ID_CENTER
} },
5801 { client_click
, MODKEY
, XCB_BUTTON_INDEX_1
, move
, {0} },
5806 update_modkey(unsigned int mod
)
5812 RB_FOREACH(kp
, key_tree
, &keys
)
5813 if (kp
->mod
& XCB_MOD_MASK_SHIFT
)
5814 kp
->mod
= mod
| XCB_MOD_MASK_SHIFT
;
5818 for (i
= 0; i
< LENGTH(buttons
); i
++)
5819 if (buttons
[i
].mask
& XCB_MOD_MASK_SHIFT
)
5820 buttons
[i
].mask
= mod
| XCB_MOD_MASK_SHIFT
;
5822 buttons
[i
].mask
= mod
;
5826 spawn_expand(struct swm_region
*r
, union arg
*args
, const char *spawn_name
,
5829 struct spawn_prog
*prog
= NULL
;
5831 char *ap
, **real_args
;
5833 /* suppress unused warning since var is needed */
5836 DNPRINTF(SWM_D_SPAWN
, "spawn_expand: %s\n", spawn_name
);
5839 TAILQ_FOREACH(prog
, &spawns
, entry
) {
5840 if (!strcasecmp(spawn_name
, prog
->name
))
5844 warnx("spawn_custom: program %s not found", spawn_name
);
5848 /* make room for expanded args */
5849 if ((real_args
= calloc(prog
->argc
+ 1, sizeof(char *))) == NULL
)
5850 err(1, "spawn_custom: calloc real_args");
5852 /* expand spawn_args into real_args */
5853 for (i
= 0; i
< prog
->argc
; i
++) {
5855 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: raw arg: %s\n", ap
);
5856 if (!strcasecmp(ap
, "$bar_border")) {
5858 strdup(r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].name
))
5860 err(1, "spawn_custom border color");
5861 } else if (!strcasecmp(ap
, "$bar_color")) {
5863 strdup(r
->s
->c
[SWM_S_COLOR_BAR
].name
))
5865 err(1, "spawn_custom bar color");
5866 } else if (!strcasecmp(ap
, "$bar_font")) {
5867 if ((real_args
[i
] = strdup(bar_fonts
))
5869 err(1, "spawn_custom bar fonts");
5870 } else if (!strcasecmp(ap
, "$bar_font_color")) {
5872 strdup(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].name
))
5874 err(1, "spawn_custom color font");
5875 } else if (!strcasecmp(ap
, "$color_focus")) {
5877 strdup(r
->s
->c
[SWM_S_COLOR_FOCUS
].name
))
5879 err(1, "spawn_custom color focus");
5880 } else if (!strcasecmp(ap
, "$color_unfocus")) {
5882 strdup(r
->s
->c
[SWM_S_COLOR_UNFOCUS
].name
))
5884 err(1, "spawn_custom color unfocus");
5885 } else if (!strcasecmp(ap
, "$region_index")) {
5886 if (asprintf(&real_args
[i
], "%d",
5887 get_region_index(r
) + 1) < 1)
5888 err(1, "spawn_custom region index");
5889 } else if (!strcasecmp(ap
, "$workspace_index")) {
5890 if (asprintf(&real_args
[i
], "%d", r
->ws
->idx
+ 1) < 1)
5891 err(1, "spawn_custom workspace index");
5893 /* no match --> copy as is */
5894 if ((real_args
[i
] = strdup(ap
)) == NULL
)
5895 err(1, "spawn_custom strdup(ap)");
5897 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: cooked arg: %s\n",
5902 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: result: ");
5903 for (i
= 0; i
< prog
->argc
; i
++)
5904 DNPRINTF(SWM_D_SPAWN
, "\"%s\" ", real_args
[i
]);
5905 DNPRINTF(SWM_D_SPAWN
, "\n");
5907 *ret_args
= real_args
;
5908 return (prog
->argc
);
5912 spawn_custom(struct swm_region
*r
, union arg
*args
, const char *spawn_name
)
5918 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
5922 spawn(r
->ws
->idx
, &a
, 1);
5924 for (i
= 0; i
< spawn_argc
; i
++)
5930 spawn_select(struct swm_region
*r
, union arg
*args
, const char *spawn_name
,
5937 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
5941 if (pipe(select_list_pipe
) == -1)
5942 err(1, "pipe error");
5943 if (pipe(select_resp_pipe
) == -1)
5944 err(1, "pipe error");
5946 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
5947 err(1, "could not disable SIGPIPE");
5948 switch (*pid
= fork()) {
5950 err(1, "cannot fork");
5953 if (dup2(select_list_pipe
[0], STDIN_FILENO
) == -1)
5955 if (dup2(select_resp_pipe
[1], STDOUT_FILENO
) == -1)
5957 close(select_list_pipe
[1]);
5958 close(select_resp_pipe
[0]);
5959 spawn(r
->ws
->idx
, &a
, 0);
5961 default: /* parent */
5962 close(select_list_pipe
[0]);
5963 close(select_resp_pipe
[1]);
5967 for (i
= 0; i
< spawn_argc
; i
++)
5973 spawn_insert(const char *name
, const char *args
, int flags
)
5975 char *arg
, *cp
, *ptr
;
5976 struct spawn_prog
*sp
;
5978 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: %s\n", name
);
5980 if ((sp
= calloc(1, sizeof *sp
)) == NULL
)
5981 err(1, "spawn_insert: calloc");
5982 if ((sp
->name
= strdup(name
)) == NULL
)
5983 err(1, "spawn_insert: strdup");
5985 /* convert the arguments to an argument list */
5986 if ((ptr
= cp
= strdup(args
)) == NULL
)
5987 err(1, "spawn_insert: strdup");
5988 while ((arg
= strsep(&ptr
, " \t")) != NULL
) {
5989 /* empty field; skip it */
5994 if ((sp
->argv
= realloc(sp
->argv
, sp
->argc
*
5995 sizeof *sp
->argv
)) == NULL
)
5996 err(1, "spawn_insert: realloc");
5997 if ((sp
->argv
[sp
->argc
- 1] = strdup(arg
)) == NULL
)
5998 err(1, "spawn_insert: strdup");
6004 TAILQ_INSERT_TAIL(&spawns
, sp
, entry
);
6005 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: leave\n");
6009 spawn_remove(struct spawn_prog
*sp
)
6013 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: %s\n", sp
->name
);
6015 TAILQ_REMOVE(&spawns
, sp
, entry
);
6016 for (i
= 0; i
< sp
->argc
; i
++)
6022 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: leave\n");
6026 setspawn(const char *name
, const char *args
, int flags
)
6028 struct spawn_prog
*sp
;
6030 DNPRINTF(SWM_D_SPAWN
, "setspawn: %s\n", name
);
6035 /* Remove any old spawn under the same name. */
6036 TAILQ_FOREACH(sp
, &spawns
, entry
)
6037 if (!strcmp(sp
->name
, name
)) {
6043 spawn_insert(name
, args
, flags
);
6045 warnx("error: setspawn: cannot find program: %s", name
);
6047 DNPRINTF(SWM_D_SPAWN
, "setspawn: leave\n");
6051 setconfspawn(char *selector
, char *value
, int flags
)
6055 args
= expand_tilde(value
);
6057 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: [%s] [%s]\n", selector
, args
);
6059 setspawn(selector
, args
, flags
);
6062 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: done.\n");
6067 validate_spawns(void)
6069 struct spawn_prog
*sp
;
6070 char which
[PATH_MAX
];
6075 RB_FOREACH(kp
, key_tree
, &keys
) {
6076 if (kp
->funcid
!= KF_SPAWN_CUSTOM
)
6080 TAILQ_FOREACH(sp
, &spawns
, entry
) {
6081 if (!strcasecmp(kp
->spawn_name
, sp
->name
))
6085 if (sp
== NULL
|| sp
->flags
& SWM_SPAWN_OPTIONAL
)
6088 /* verify we have the goods */
6089 snprintf(which
, sizeof which
, "which %s", sp
->argv
[0]);
6090 DNPRINTF(SWM_D_CONF
, "validate_spawns: which %s\n",
6092 for (i
= strlen("which "); i
< strlen(which
); i
++)
6093 if (which
[i
] == ' ') {
6097 if (system(which
) != 0)
6098 add_startup_exception("could not find %s",
6099 &which
[strlen("which ")]);
6106 setconfspawn("lock", "xlock", 0);
6108 setconfspawn("term", "xterm", 0);
6109 setconfspawn("spawn_term", "xterm", 0);
6111 setconfspawn("menu", "dmenu_run"
6114 " -nf $bar_font_color"
6116 " -sf $bar_color", 0);
6118 setconfspawn("search", "dmenu"
6122 " -nf $bar_font_color"
6124 " -sf $bar_color", 0);
6126 setconfspawn("name_workspace", "dmenu"
6130 " -nf $bar_font_color"
6132 " -sf $bar_color", 0);
6134 /* These are not verified for existence, even with a binding set. */
6135 setconfspawn("screenshot_all", "screenshot.sh full", SWM_SPAWN_OPTIONAL
);
6136 setconfspawn("screenshot_wind", "screenshot.sh window", SWM_SPAWN_OPTIONAL
);
6137 setconfspawn("initscr", "initscreen.sh", SWM_SPAWN_OPTIONAL
);
6141 #define SWM_MODNAME_SIZE 32
6142 #define SWM_KEY_WS "\n+ \t"
6144 parsekeys(char *keystr
, unsigned int currmod
, unsigned int *mod
, KeySym
*ks
)
6148 DNPRINTF(SWM_D_KEY
, "parsekeys: enter [%s]\n", keystr
);
6149 if (mod
== NULL
|| ks
== NULL
) {
6150 DNPRINTF(SWM_D_KEY
, "parsekeys: no mod or key vars\n");
6153 if (keystr
== NULL
|| strlen(keystr
) == 0) {
6154 DNPRINTF(SWM_D_KEY
, "parsekeys: no keystr\n");
6158 *ks
= XCB_NO_SYMBOL
;
6160 while ((name
= strsep(&cp
, SWM_KEY_WS
)) != NULL
) {
6161 DNPRINTF(SWM_D_KEY
, "parsekeys: key [%s]\n", name
);
6163 cp
+= (long)strspn(cp
, SWM_KEY_WS
);
6164 if (strncasecmp(name
, "MOD", SWM_MODNAME_SIZE
) == 0)
6166 else if (!strncasecmp(name
, "Mod1", SWM_MODNAME_SIZE
))
6167 *mod
|= XCB_MOD_MASK_1
;
6168 else if (!strncasecmp(name
, "Mod2", SWM_MODNAME_SIZE
))
6169 *mod
+= XCB_MOD_MASK_2
;
6170 else if (!strncmp(name
, "Mod3", SWM_MODNAME_SIZE
))
6171 *mod
|= XCB_MOD_MASK_3
;
6172 else if (!strncmp(name
, "Mod4", SWM_MODNAME_SIZE
))
6173 *mod
|= XCB_MOD_MASK_4
;
6174 else if (strncasecmp(name
, "SHIFT", SWM_MODNAME_SIZE
) == 0)
6175 *mod
|= XCB_MOD_MASK_SHIFT
;
6176 else if (strncasecmp(name
, "CONTROL", SWM_MODNAME_SIZE
) == 0)
6177 *mod
|= XCB_MOD_MASK_CONTROL
;
6179 *ks
= XStringToKeysym(name
);
6180 XConvertCase(*ks
, ks
, &uks
);
6181 if (ks
== XCB_NO_SYMBOL
) {
6183 "parsekeys: invalid key %s\n",
6189 DNPRINTF(SWM_D_KEY
, "parsekeys: leave ok\n");
6194 strdupsafe(const char *str
)
6199 return (strdup(str
));
6203 key_insert(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
6204 const char *spawn_name
)
6208 DNPRINTF(SWM_D_KEY
, "key_insert: enter %s [%s]\n",
6209 keyfuncs
[kfid
].name
, spawn_name
);
6211 if ((kp
= malloc(sizeof *kp
)) == NULL
)
6212 err(1, "key_insert: malloc");
6217 kp
->spawn_name
= strdupsafe(spawn_name
);
6218 RB_INSERT(key_tree
, &keys
, kp
);
6220 DNPRINTF(SWM_D_KEY
, "key_insert: leave\n");
6224 key_lookup(unsigned int mod
, KeySym ks
)
6231 return (RB_FIND(key_tree
, &keys
, &kp
));
6235 key_remove(struct key
*kp
)
6237 DNPRINTF(SWM_D_KEY
, "key_remove: %s\n", keyfuncs
[kp
->funcid
].name
);
6239 RB_REMOVE(key_tree
, &keys
, kp
);
6240 free(kp
->spawn_name
);
6243 DNPRINTF(SWM_D_KEY
, "key_remove: leave\n");
6247 key_replace(struct key
*kp
, unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
6248 const char *spawn_name
)
6250 DNPRINTF(SWM_D_KEY
, "key_replace: %s [%s]\n", keyfuncs
[kp
->funcid
].name
,
6254 key_insert(mod
, ks
, kfid
, spawn_name
);
6256 DNPRINTF(SWM_D_KEY
, "key_replace: leave\n");
6260 setkeybinding(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
6261 const char *spawn_name
)
6265 DNPRINTF(SWM_D_KEY
, "setkeybinding: enter %s [%s]\n",
6266 keyfuncs
[kfid
].name
, spawn_name
);
6268 if ((kp
= key_lookup(mod
, ks
)) != NULL
) {
6269 if (kfid
== KF_INVALID
)
6272 key_replace(kp
, mod
, ks
, kfid
, spawn_name
);
6273 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
6276 if (kfid
== KF_INVALID
) {
6277 warnx("bind: Key combination already unbound.");
6278 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
6282 key_insert(mod
, ks
, kfid
, spawn_name
);
6283 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
6287 setconfbinding(char *selector
, char *value
, int flags
)
6289 enum keyfuncid kfid
;
6292 struct spawn_prog
*sp
;
6294 /* suppress unused warning since var is needed */
6297 DNPRINTF(SWM_D_KEY
, "setconfbinding: enter\n");
6298 if (selector
== NULL
) {
6299 DNPRINTF(SWM_D_KEY
, "setconfbinding: unbind %s\n", value
);
6300 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
6302 setkeybinding(mod
, ks
, kfid
, NULL
);
6307 /* search by key function name */
6308 for (kfid
= 0; kfid
< KF_INVALID
; (kfid
)++) {
6309 if (strncasecmp(selector
, keyfuncs
[kfid
].name
,
6310 SWM_FUNCNAME_LEN
) == 0) {
6311 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match "
6312 "keyfunc\n", selector
);
6313 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
6314 setkeybinding(mod
, ks
, kfid
, NULL
);
6320 /* search by custom spawn name */
6321 TAILQ_FOREACH(sp
, &spawns
, entry
) {
6322 if (strcasecmp(selector
, sp
->name
) == 0) {
6323 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match "
6324 "spawn\n", selector
);
6325 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
6326 setkeybinding(mod
, ks
, KF_SPAWN_CUSTOM
,
6333 DNPRINTF(SWM_D_KEY
, "setconfbinding: no match\n");
6340 #define MODKEY_SHIFT MODKEY | XCB_MOD_MASK_SHIFT
6341 setkeybinding(MODKEY
, XK_b
, KF_BAR_TOGGLE
, NULL
);
6342 setkeybinding(MODKEY_SHIFT
, XK_b
, KF_BAR_TOGGLE_WS
,NULL
);
6343 setkeybinding(MODKEY
, XK_v
, KF_BUTTON2
, NULL
);
6344 setkeybinding(MODKEY
, XK_space
, KF_CYCLE_LAYOUT
,NULL
);
6345 setkeybinding(MODKEY_SHIFT
, XK_backslash
, KF_FLIP_LAYOUT
, NULL
);
6346 setkeybinding(MODKEY
, XK_t
, KF_FLOAT_TOGGLE
,NULL
);
6347 setkeybinding(MODKEY
, XK_m
, KF_FOCUS_MAIN
, NULL
);
6348 setkeybinding(MODKEY
, XK_j
, KF_FOCUS_NEXT
, NULL
);
6349 setkeybinding(MODKEY
, XK_Tab
, KF_FOCUS_NEXT
, NULL
);
6350 setkeybinding(MODKEY
, XK_k
, KF_FOCUS_PREV
, NULL
);
6351 setkeybinding(MODKEY_SHIFT
, XK_Tab
, KF_FOCUS_PREV
, NULL
);
6352 setkeybinding(MODKEY_SHIFT
, XK_equal
, KF_HEIGHT_GROW
,NULL
);
6353 setkeybinding(MODKEY_SHIFT
, XK_minus
, KF_HEIGHT_SHRINK
,NULL
);
6354 setkeybinding(MODKEY
, XK_w
, KF_ICONIFY
, NULL
);
6355 setkeybinding(MODKEY
, XK_h
, KF_MASTER_SHRINK
, NULL
);
6356 setkeybinding(MODKEY
, XK_l
, KF_MASTER_GROW
, NULL
);
6357 setkeybinding(MODKEY
, XK_comma
, KF_MASTER_ADD
, NULL
);
6358 setkeybinding(MODKEY
, XK_period
, KF_MASTER_DEL
, NULL
);
6359 setkeybinding(MODKEY_SHIFT
, XK_bracketright
,KF_MOVE_DOWN
,NULL
);
6360 setkeybinding(MODKEY
, XK_bracketleft
, KF_MOVE_LEFT
,NULL
);
6361 setkeybinding(MODKEY
, XK_bracketright
,KF_MOVE_RIGHT
,NULL
);
6362 setkeybinding(MODKEY_SHIFT
, XK_bracketleft
, KF_MOVE_UP
, NULL
);
6363 setkeybinding(MODKEY_SHIFT
, XK_KP_End
, KF_MVRG_1
, NULL
);
6364 setkeybinding(MODKEY_SHIFT
, XK_KP_Down
, KF_MVRG_2
, NULL
);
6365 setkeybinding(MODKEY_SHIFT
, XK_KP_Next
, KF_MVRG_3
, NULL
);
6366 setkeybinding(MODKEY_SHIFT
, XK_KP_Left
, KF_MVRG_4
, NULL
);
6367 setkeybinding(MODKEY_SHIFT
, XK_KP_Begin
, KF_MVRG_5
, NULL
);
6368 setkeybinding(MODKEY_SHIFT
, XK_KP_Right
, KF_MVRG_6
, NULL
);
6369 setkeybinding(MODKEY_SHIFT
, XK_KP_Home
, KF_MVRG_7
, NULL
);
6370 setkeybinding(MODKEY_SHIFT
, XK_KP_Up
, KF_MVRG_8
, NULL
);
6371 setkeybinding(MODKEY_SHIFT
, XK_KP_Prior
, KF_MVRG_9
, NULL
);
6372 setkeybinding(MODKEY_SHIFT
, XK_1
, KF_MVWS_1
, NULL
);
6373 setkeybinding(MODKEY_SHIFT
, XK_2
, KF_MVWS_2
, NULL
);
6374 setkeybinding(MODKEY_SHIFT
, XK_3
, KF_MVWS_3
, NULL
);
6375 setkeybinding(MODKEY_SHIFT
, XK_4
, KF_MVWS_4
, NULL
);
6376 setkeybinding(MODKEY_SHIFT
, XK_5
, KF_MVWS_5
, NULL
);
6377 setkeybinding(MODKEY_SHIFT
, XK_6
, KF_MVWS_6
, NULL
);
6378 setkeybinding(MODKEY_SHIFT
, XK_7
, KF_MVWS_7
, NULL
);
6379 setkeybinding(MODKEY_SHIFT
, XK_8
, KF_MVWS_8
, NULL
);
6380 setkeybinding(MODKEY_SHIFT
, XK_9
, KF_MVWS_9
, NULL
);
6381 setkeybinding(MODKEY_SHIFT
, XK_0
, KF_MVWS_10
, NULL
);
6382 setkeybinding(MODKEY_SHIFT
, XK_F1
, KF_MVWS_11
, NULL
);
6383 setkeybinding(MODKEY_SHIFT
, XK_F2
, KF_MVWS_12
, NULL
);
6384 setkeybinding(MODKEY_SHIFT
, XK_F3
, KF_MVWS_13
, NULL
);
6385 setkeybinding(MODKEY_SHIFT
, XK_F4
, KF_MVWS_14
, NULL
);
6386 setkeybinding(MODKEY_SHIFT
, XK_F5
, KF_MVWS_15
, NULL
);
6387 setkeybinding(MODKEY_SHIFT
, XK_F6
, KF_MVWS_16
, NULL
);
6388 setkeybinding(MODKEY_SHIFT
, XK_F7
, KF_MVWS_17
, NULL
);
6389 setkeybinding(MODKEY_SHIFT
, XK_F8
, KF_MVWS_18
, NULL
);
6390 setkeybinding(MODKEY_SHIFT
, XK_F9
, KF_MVWS_19
, NULL
);
6391 setkeybinding(MODKEY_SHIFT
, XK_F10
, KF_MVWS_20
, NULL
);
6392 setkeybinding(MODKEY_SHIFT
, XK_F11
, KF_MVWS_21
, NULL
);
6393 setkeybinding(MODKEY_SHIFT
, XK_F12
, KF_MVWS_22
, NULL
);
6394 setkeybinding(MODKEY_SHIFT
, XK_slash
, KF_NAME_WORKSPACE
,NULL
);
6395 setkeybinding(MODKEY_SHIFT
, XK_q
, KF_QUIT
, NULL
);
6396 setkeybinding(MODKEY_SHIFT
, XK_r
, KF_RAISE_TOGGLE
,NULL
);
6397 setkeybinding(MODKEY
, XK_q
, KF_RESTART
, NULL
);
6398 setkeybinding(MODKEY
, XK_KP_End
, KF_RG_1
, NULL
);
6399 setkeybinding(MODKEY
, XK_KP_Down
, KF_RG_2
, NULL
);
6400 setkeybinding(MODKEY
, XK_KP_Next
, KF_RG_3
, NULL
);
6401 setkeybinding(MODKEY
, XK_KP_Left
, KF_RG_4
, NULL
);
6402 setkeybinding(MODKEY
, XK_KP_Begin
, KF_RG_5
, NULL
);
6403 setkeybinding(MODKEY
, XK_KP_Right
, KF_RG_6
, NULL
);
6404 setkeybinding(MODKEY
, XK_KP_Home
, KF_RG_7
, NULL
);
6405 setkeybinding(MODKEY
, XK_KP_Up
, KF_RG_8
, NULL
);
6406 setkeybinding(MODKEY
, XK_KP_Prior
, KF_RG_9
, NULL
);
6407 setkeybinding(MODKEY_SHIFT
, XK_Right
, KF_RG_NEXT
, NULL
);
6408 setkeybinding(MODKEY_SHIFT
, XK_Left
, KF_RG_PREV
, NULL
);
6409 setkeybinding(MODKEY
, XK_f
, KF_SEARCH_WIN
, NULL
);
6410 setkeybinding(MODKEY
, XK_slash
, KF_SEARCH_WORKSPACE
,NULL
);
6411 setkeybinding(MODKEY_SHIFT
, XK_i
, KF_SPAWN_CUSTOM
,"initscr");
6412 setkeybinding(MODKEY_SHIFT
, XK_Delete
, KF_SPAWN_CUSTOM
,"lock");
6413 setkeybinding(MODKEY
, XK_p
, KF_SPAWN_CUSTOM
,"menu");
6414 setkeybinding(MODKEY
, XK_s
, KF_SPAWN_CUSTOM
,"screenshot_all");
6415 setkeybinding(MODKEY_SHIFT
, XK_s
, KF_SPAWN_CUSTOM
,"screenshot_wind");
6416 setkeybinding(MODKEY_SHIFT
, XK_Return
, KF_SPAWN_CUSTOM
,"term");
6417 setkeybinding(MODKEY_SHIFT
, XK_comma
, KF_STACK_INC
, NULL
);
6418 setkeybinding(MODKEY_SHIFT
, XK_period
, KF_STACK_DEC
, NULL
);
6419 setkeybinding(MODKEY_SHIFT
, XK_space
, KF_STACK_RESET
, NULL
);
6420 setkeybinding(MODKEY
, XK_Return
, KF_SWAP_MAIN
, NULL
);
6421 setkeybinding(MODKEY_SHIFT
, XK_j
, KF_SWAP_NEXT
, NULL
);
6422 setkeybinding(MODKEY_SHIFT
, XK_k
, KF_SWAP_PREV
, NULL
);
6423 setkeybinding(MODKEY_SHIFT
, XK_w
, KF_UNICONIFY
, NULL
);
6424 setkeybinding(MODKEY_SHIFT
, XK_v
, KF_VERSION
, NULL
);
6425 setkeybinding(MODKEY
, XK_equal
, KF_WIDTH_GROW
, NULL
);
6426 setkeybinding(MODKEY
, XK_minus
, KF_WIDTH_SHRINK
,NULL
);
6427 setkeybinding(MODKEY
, XK_x
, KF_WIND_DEL
, NULL
);
6428 setkeybinding(MODKEY_SHIFT
, XK_x
, KF_WIND_KILL
, NULL
);
6429 setkeybinding(MODKEY
, XK_1
, KF_WS_1
, NULL
);
6430 setkeybinding(MODKEY
, XK_2
, KF_WS_2
, NULL
);
6431 setkeybinding(MODKEY
, XK_3
, KF_WS_3
, NULL
);
6432 setkeybinding(MODKEY
, XK_4
, KF_WS_4
, NULL
);
6433 setkeybinding(MODKEY
, XK_5
, KF_WS_5
, NULL
);
6434 setkeybinding(MODKEY
, XK_6
, KF_WS_6
, NULL
);
6435 setkeybinding(MODKEY
, XK_7
, KF_WS_7
, NULL
);
6436 setkeybinding(MODKEY
, XK_8
, KF_WS_8
, NULL
);
6437 setkeybinding(MODKEY
, XK_9
, KF_WS_9
, NULL
);
6438 setkeybinding(MODKEY
, XK_0
, KF_WS_10
, NULL
);
6439 setkeybinding(MODKEY
, XK_F1
, KF_WS_11
, NULL
);
6440 setkeybinding(MODKEY
, XK_F2
, KF_WS_12
, NULL
);
6441 setkeybinding(MODKEY
, XK_F3
, KF_WS_13
, NULL
);
6442 setkeybinding(MODKEY
, XK_F4
, KF_WS_14
, NULL
);
6443 setkeybinding(MODKEY
, XK_F5
, KF_WS_15
, NULL
);
6444 setkeybinding(MODKEY
, XK_F6
, KF_WS_16
, NULL
);
6445 setkeybinding(MODKEY
, XK_F7
, KF_WS_17
, NULL
);
6446 setkeybinding(MODKEY
, XK_F8
, KF_WS_18
, NULL
);
6447 setkeybinding(MODKEY
, XK_F9
, KF_WS_19
, NULL
);
6448 setkeybinding(MODKEY
, XK_F10
, KF_WS_20
, NULL
);
6449 setkeybinding(MODKEY
, XK_F11
, KF_WS_21
, NULL
);
6450 setkeybinding(MODKEY
, XK_F12
, KF_WS_22
, NULL
);
6451 setkeybinding(MODKEY
, XK_Right
, KF_WS_NEXT
, NULL
);
6452 setkeybinding(MODKEY
, XK_Left
, KF_WS_PREV
, NULL
);
6453 setkeybinding(MODKEY
, XK_Up
, KF_WS_NEXT_ALL
, NULL
);
6454 setkeybinding(MODKEY
, XK_Down
, KF_WS_PREV_ALL
, NULL
);
6455 setkeybinding(MODKEY
, XK_a
, KF_WS_PRIOR
, NULL
);
6457 setkeybinding(MODKEY_SHIFT
, XK_d
, KF_DUMPWINS
, NULL
);
6467 while (RB_EMPTY(&keys
) == 0) {
6468 kp
= RB_ROOT(&keys
);
6474 setkeymapping(char *selector
, char *value
, int flags
)
6476 char *keymapping_file
;
6478 /* suppress unused warnings since vars are needed */
6482 DNPRINTF(SWM_D_KEY
, "setkeymapping: enter\n");
6484 keymapping_file
= expand_tilde(value
);
6487 /* load new key bindings; if it fails, revert to default bindings */
6488 if (conf_load(keymapping_file
, SWM_CONF_KEYMAPPING
)) {
6493 free(keymapping_file
);
6495 DNPRINTF(SWM_D_KEY
, "setkeymapping: leave\n");
6500 updatenumlockmask(void)
6503 xcb_get_modifier_mapping_reply_t
*modmap_r
;
6504 xcb_keycode_t
*modmap
, kc
, *keycode
;
6508 modmap_r
= xcb_get_modifier_mapping_reply(conn
,
6509 xcb_get_modifier_mapping(conn
),
6512 modmap
= xcb_get_modifier_mapping_keycodes(modmap_r
);
6513 for (i
= 0; i
< 8; i
++) {
6514 for (j
= 0; j
< modmap_r
->keycodes_per_modifier
; j
++) {
6515 kc
= modmap
[i
* modmap_r
->keycodes_per_modifier
6517 keycode
= xcb_key_symbols_get_keycode(syms
,
6520 numlockmask
= (1 << i
);
6526 DNPRINTF(SWM_D_MISC
, "updatenumlockmask: %d\n", numlockmask
);
6533 int num_screens
, k
, j
;
6534 unsigned int modifiers
[4];
6535 xcb_keycode_t
*code
;
6537 DNPRINTF(SWM_D_MISC
, "grabkeys\n");
6538 updatenumlockmask();
6541 modifiers
[1] = numlockmask
;
6542 modifiers
[2] = XCB_MOD_MASK_LOCK
;
6543 modifiers
[3] = numlockmask
| XCB_MOD_MASK_LOCK
;
6545 num_screens
= get_screen_count();
6546 for (k
= 0; k
< num_screens
; k
++) {
6547 if (TAILQ_EMPTY(&screens
[k
].rl
))
6549 xcb_ungrab_key(conn
, XCB_GRAB_ANY
, screens
[k
].root
,
6551 RB_FOREACH(kp
, key_tree
, &keys
) {
6552 if ((code
= xcb_key_symbols_get_keycode(syms
,
6554 for (j
= 0; j
< LENGTH(modifiers
); j
++)
6555 xcb_grab_key(conn
, 1,
6557 kp
->mod
| modifiers
[j
],
6558 *code
, XCB_GRAB_MODE_SYNC
,
6559 XCB_GRAB_MODE_SYNC
);
6567 grabbuttons(struct ws_win
*win
)
6569 unsigned int modifiers
[4];
6572 DNPRINTF(SWM_D_MOUSE
, "grabbuttons: win 0x%x\n", win
->id
);
6573 updatenumlockmask();
6576 modifiers
[1] = numlockmask
;
6577 modifiers
[2] = XCB_MOD_MASK_LOCK
;
6578 modifiers
[3] = numlockmask
| XCB_MOD_MASK_LOCK
;
6580 for (i
= 0; i
< LENGTH(buttons
); i
++)
6581 if (buttons
[i
].action
== client_click
)
6582 for (j
= 0; j
< LENGTH(modifiers
); ++j
)
6583 xcb_grab_button(conn
, 0, win
->id
, BUTTONMASK
,
6584 XCB_GRAB_MODE_SYNC
, XCB_GRAB_MODE_ASYNC
,
6585 XCB_WINDOW_NONE
, XCB_CURSOR_NONE
,
6586 buttons
[i
].button
, buttons
[i
].mask
|
6590 const char *quirkname
[] = {
6591 "NONE", /* config string for "no value" */
6599 "FOCUSONMAP_SINGLE",
6602 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
6603 #define SWM_Q_WS "\n|+ \t"
6605 parsequirks(char *qstr
, unsigned long *quirk
)
6615 while ((name
= strsep(&cp
, SWM_Q_WS
)) != NULL
) {
6617 cp
+= (long)strspn(cp
, SWM_Q_WS
);
6618 for (i
= 0; i
< LENGTH(quirkname
); i
++) {
6619 if (!strncasecmp(name
, quirkname
[i
], SWM_QUIRK_LEN
)) {
6620 DNPRINTF(SWM_D_QUIRK
,
6621 "parsequirks: %s\n", name
);
6626 *quirk
|= 1 << (i
-1);
6630 if (i
>= LENGTH(quirkname
)) {
6631 DNPRINTF(SWM_D_QUIRK
,
6632 "parsequirks: invalid quirk [%s]\n", name
);
6640 quirk_insert(const char *class, const char *name
, unsigned long quirk
)
6644 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: %s:%s [%lu]\n", class, name
,
6647 if ((qp
= malloc(sizeof *qp
)) == NULL
)
6648 err(1, "quirk_insert: malloc");
6649 if ((qp
->class = strdup(class)) == NULL
)
6650 err(1, "quirk_insert: strdup");
6651 if ((qp
->name
= strdup(name
)) == NULL
)
6652 err(1, "quirk_insert: strdup");
6655 TAILQ_INSERT_TAIL(&quirks
, qp
, entry
);
6657 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: leave\n");
6661 quirk_remove(struct quirk
*qp
)
6663 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: %s:%s [%lu]\n", qp
->class,
6664 qp
->name
, qp
->quirk
);
6666 TAILQ_REMOVE(&quirks
, qp
, entry
);
6671 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: leave\n");
6675 quirk_replace(struct quirk
*qp
, const char *class, const char *name
,
6676 unsigned long quirk
)
6678 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: %s:%s [%lu]\n", qp
->class,
6679 qp
->name
, qp
->quirk
);
6682 quirk_insert(class, name
, quirk
);
6684 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: leave\n");
6688 setquirk(const char *class, const char *name
, unsigned long quirk
)
6692 DNPRINTF(SWM_D_QUIRK
, "setquirk: enter %s:%s [%lu]\n", class, name
,
6695 /* Remove/replace existing quirk. */
6696 TAILQ_FOREACH(qp
, &quirks
, entry
) {
6697 if (!strcmp(qp
->class, class) && !strcmp(qp
->name
, name
)) {
6701 quirk_replace(qp
, class, name
, quirk
);
6702 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
6707 /* Only insert if quirk is not NONE. */
6709 quirk_insert(class, name
, quirk
);
6711 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
6715 setconfquirk(char *selector
, char *value
, int flags
)
6717 char *cp
, *class, *name
;
6721 /* suppress unused warning since var is needed */
6724 if (selector
== NULL
)
6726 if ((cp
= strchr(selector
, ':')) == NULL
)
6731 if ((retval
= parsequirks(value
, &qrks
)) == 0)
6732 setquirk(class, name
, qrks
);
6739 setquirk("MPlayer", "xv", SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
| SWM_Q_FOCUSPREV
);
6740 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT
);
6741 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ
);
6742 setquirk("Firefox", "Dialog", SWM_Q_FLOAT
);
6743 setquirk("Gimp", "gimp", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
6744 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ
);
6745 setquirk("xine", "Xine Window", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
6746 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
6747 setquirk("xine", "xine Panel", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
6748 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
6749 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN
| SWM_Q_FLOAT
);
6750 setquirk("pcb", "pcb", SWM_Q_FLOAT
);
6751 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
);
6754 /* conf file stuff */
6755 #define SWM_CONF_FILE "spectrwm.conf"
6756 #define SWM_CONF_FILE_OLD "scrotwm.conf"
6760 SWM_S_BAR_AT_BOTTOM
,
6761 SWM_S_BAR_BORDER_WIDTH
,
6764 SWM_S_BAR_ENABLED_WS
,
6769 SWM_S_CLOCK_ENABLED
,
6772 SWM_S_CYCLE_VISIBLE
,
6774 SWM_S_DISABLE_BORDER
,
6776 SWM_S_FOCUS_CLOSE_WRAP
,
6777 SWM_S_FOCUS_DEFAULT
,
6779 SWM_S_REGION_PADDING
,
6784 SWM_S_STACK_ENABLED
,
6787 SWM_S_TITLE_CLASS_ENABLED
,
6788 SWM_S_TITLE_NAME_ENABLED
,
6789 SWM_S_URGENT_ENABLED
,
6790 SWM_S_VERBOSE_LAYOUT
,
6791 SWM_S_WINDOW_NAME_ENABLED
,
6792 SWM_S_WORKSPACE_LIMIT
6796 setconfvalue(char *selector
, char *value
, int flags
)
6798 struct workspace
*ws
;
6799 int i
, ws_id
, num_screens
;
6802 /* suppress unused warning since var is needed */
6806 case SWM_S_BAR_ACTION
:
6808 if ((bar_argv
[0] = expand_tilde(value
)) == NULL
)
6809 err(1, "setconfvalue: bar_action");
6811 case SWM_S_BAR_AT_BOTTOM
:
6812 bar_at_bottom
= atoi(value
);
6814 case SWM_S_BAR_BORDER_WIDTH
:
6815 bar_border_width
= atoi(value
);
6816 if (bar_border_width
< 0)
6817 bar_border_width
= 0;
6819 case SWM_S_BAR_DELAY
:
6820 /* No longer needed; leave to not break old conf files. */
6822 case SWM_S_BAR_ENABLED
:
6823 bar_enabled
= atoi(value
);
6825 case SWM_S_BAR_ENABLED_WS
:
6826 ws_id
= atoi(selector
) - 1;
6827 if (ws_id
< 0 || ws_id
>= workspace_limit
)
6828 errx(1, "setconfvalue: bar_enabled_ws: invalid "
6829 "workspace %d.", ws_id
+ 1);
6831 num_screens
= get_screen_count();
6832 for (i
= 0; i
< num_screens
; i
++) {
6833 ws
= (struct workspace
*)&screens
[i
].ws
;
6834 ws
[ws_id
].bar_enabled
= atoi(value
);
6837 case SWM_S_BAR_FONT
:
6839 if (asprintf(&bar_fonts
, "%s,%s", value
, bar_fonts
) == -1)
6840 err(1, "setconfvalue: asprintf: failed to allocate "
6841 "memory for bar_fonts.");
6844 /* If already in xft mode, then we are done. */
6845 if (!bar_font_legacy
)
6848 /* If there are any non-XLFD entries, switch to Xft mode. */
6849 while ((b
= strsep(&value
, ",")) != NULL
) {
6853 bar_font_legacy
= 0;
6858 case SWM_S_BAR_FORMAT
:
6860 if ((bar_format
= strdup(value
)) == NULL
)
6861 err(1, "setconfvalue: bar_format");
6863 case SWM_S_BAR_JUSTIFY
:
6864 if (!strcmp(value
, "left"))
6865 bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
6866 else if (!strcmp(value
, "center"))
6867 bar_justify
= SWM_BAR_JUSTIFY_CENTER
;
6868 else if (!strcmp(value
, "right"))
6869 bar_justify
= SWM_BAR_JUSTIFY_RIGHT
;
6871 errx(1, "invalid bar_justify");
6873 case SWM_S_BORDER_WIDTH
:
6874 border_width
= atoi(value
);
6875 if (border_width
< 0)
6878 case SWM_S_CLOCK_ENABLED
:
6879 clock_enabled
= atoi(value
);
6881 case SWM_S_CLOCK_FORMAT
:
6882 #ifndef SWM_DENY_CLOCK_FORMAT
6884 if ((clock_format
= strdup(value
)) == NULL
)
6885 err(1, "setconfvalue: clock_format");
6888 case SWM_S_CYCLE_EMPTY
:
6889 cycle_empty
= atoi(value
);
6891 case SWM_S_CYCLE_VISIBLE
:
6892 cycle_visible
= atoi(value
);
6894 case SWM_S_DIALOG_RATIO
:
6895 dialog_ratio
= atof(value
);
6896 if (dialog_ratio
> 1.0 || dialog_ratio
<= .3)
6899 case SWM_S_DISABLE_BORDER
:
6900 disable_border
= atoi(value
);
6902 case SWM_S_FOCUS_CLOSE
:
6903 if (!strcmp(value
, "first"))
6904 focus_close
= SWM_STACK_BOTTOM
;
6905 else if (!strcmp(value
, "last"))
6906 focus_close
= SWM_STACK_TOP
;
6907 else if (!strcmp(value
, "next"))
6908 focus_close
= SWM_STACK_ABOVE
;
6909 else if (!strcmp(value
, "previous"))
6910 focus_close
= SWM_STACK_BELOW
;
6912 errx(1, "focus_close");
6914 case SWM_S_FOCUS_CLOSE_WRAP
:
6915 focus_close_wrap
= atoi(value
);
6917 case SWM_S_FOCUS_DEFAULT
:
6918 if (!strcmp(value
, "last"))
6919 focus_default
= SWM_STACK_TOP
;
6920 else if (!strcmp(value
, "first"))
6921 focus_default
= SWM_STACK_BOTTOM
;
6923 errx(1, "focus_default");
6925 case SWM_S_FOCUS_MODE
:
6926 if (!strcmp(value
, "default"))
6927 focus_mode
= SWM_FOCUS_DEFAULT
;
6928 else if (!strcmp(value
, "follow") ||
6929 !strcmp(value
, "follow_cursor"))
6930 focus_mode
= SWM_FOCUS_FOLLOW
;
6931 else if (!strcmp(value
, "manual"))
6932 focus_mode
= SWM_FOCUS_MANUAL
;
6934 errx(1, "focus_mode");
6936 case SWM_S_REGION_PADDING
:
6937 region_padding
= atoi(value
);
6938 if (region_padding
< 0)
6941 case SWM_S_SPAWN_ORDER
:
6942 if (!strcmp(value
, "first"))
6943 spawn_position
= SWM_STACK_BOTTOM
;
6944 else if (!strcmp(value
, "last"))
6945 spawn_position
= SWM_STACK_TOP
;
6946 else if (!strcmp(value
, "next"))
6947 spawn_position
= SWM_STACK_ABOVE
;
6948 else if (!strcmp(value
, "previous"))
6949 spawn_position
= SWM_STACK_BELOW
;
6951 errx(1, "spawn_position");
6953 case SWM_S_SPAWN_TERM
:
6954 setconfspawn("term", value
, 0);
6955 setconfspawn("spawn_term", value
, 0);
6958 /* No longer needed; leave to not break old conf files. */
6960 case SWM_S_SS_ENABLED
:
6961 /* No longer needed; leave to not break old conf files. */
6963 case SWM_S_STACK_ENABLED
:
6964 stack_enabled
= atoi(value
);
6966 case SWM_S_TERM_WIDTH
:
6967 term_width
= atoi(value
);
6971 case SWM_S_TILE_GAP
:
6972 tile_gap
= atoi(value
);
6976 case SWM_S_TITLE_CLASS_ENABLED
:
6977 title_class_enabled
= atoi(value
);
6979 case SWM_S_TITLE_NAME_ENABLED
:
6980 title_name_enabled
= atoi(value
);
6982 case SWM_S_URGENT_ENABLED
:
6983 urgent_enabled
= atoi(value
);
6985 case SWM_S_VERBOSE_LAYOUT
:
6986 verbose_layout
= atoi(value
);
6987 for (i
= 0; layouts
[i
].l_stack
!= NULL
; i
++) {
6989 layouts
[i
].l_string
= fancy_stacker
;
6991 layouts
[i
].l_string
= plain_stacker
;
6994 case SWM_S_WINDOW_NAME_ENABLED
:
6995 window_name_enabled
= atoi(value
);
6997 case SWM_S_WORKSPACE_LIMIT
:
6998 workspace_limit
= atoi(value
);
6999 if (workspace_limit
> SWM_WS_MAX
)
7000 workspace_limit
= SWM_WS_MAX
;
7001 else if (workspace_limit
< 1)
7002 workspace_limit
= 1;
7011 setconfmodkey(char *selector
, char *value
, int flags
)
7013 /* suppress unused warnings since vars are needed */
7017 if (!strncasecmp(value
, "Mod1", strlen("Mod1")))
7018 update_modkey(XCB_MOD_MASK_1
);
7019 else if (!strncasecmp(value
, "Mod2", strlen("Mod2")))
7020 update_modkey(XCB_MOD_MASK_2
);
7021 else if (!strncasecmp(value
, "Mod3", strlen("Mod3")))
7022 update_modkey(XCB_MOD_MASK_3
);
7023 else if (!strncasecmp(value
, "Mod4", strlen("Mod4")))
7024 update_modkey(XCB_MOD_MASK_4
);
7031 setconfcolor(char *selector
, char *value
, int flags
)
7033 setscreencolor(value
, ((selector
== NULL
)?-1:atoi(selector
)), flags
);
7038 setconfregion(char *selector
, char *value
, int flags
)
7040 /* suppress unused warnings since vars are needed */
7044 custom_region(value
);
7049 setautorun(char *selector
, char *value
, int flags
)
7059 /* suppress unused warnings since vars are needed */
7063 if (getenv("SWM_STARTED"))
7067 if (sscanf(value
, "ws[%d]:%1023c", &ws_id
, s
) != 2)
7068 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
7070 if (ws_id
< 0 || ws_id
>= workspace_limit
)
7071 errx(1, "autorun: invalid workspace %d", ws_id
+ 1);
7073 sp
= expand_tilde((char *)&s
);
7076 * This is a little intricate
7078 * If the pid already exists we simply reuse it because it means it was
7079 * used before AND not claimed by manage_window. We get away with
7080 * altering it in the parent after INSERT because this can not be a race
7083 while ((ap
= strsep(&sp
, " \t")) != NULL
) {
7086 DNPRINTF(SWM_D_SPAWN
, "setautorun: arg [%s]\n", ap
);
7088 if ((a
.argv
= realloc(a
.argv
, argc
* sizeof(char *))) == NULL
)
7089 err(1, "setautorun: realloc");
7090 a
.argv
[argc
- 1] = ap
;
7094 if ((a
.argv
= realloc(a
.argv
, (argc
+ 1) * sizeof(char *))) == NULL
)
7095 err(1, "setautorun: realloc");
7096 a
.argv
[argc
] = NULL
;
7098 if ((pid
= fork()) == 0) {
7099 spawn(ws_id
, &a
, 1);
7108 p
= calloc(1, sizeof *p
);
7111 TAILQ_INSERT_TAIL(&pidlist
, p
, entry
);
7121 setlayout(char *selector
, char *value
, int flags
)
7123 int ws_id
, i
, x
, mg
, ma
, si
, ar
, f
= 0;
7124 int st
= SWM_V_STACK
, num_screens
;
7126 struct workspace
*ws
;
7128 /* suppress unused warnings since vars are needed */
7132 if (getenv("SWM_STARTED"))
7136 if (sscanf(value
, "ws[%d]:%d:%d:%d:%d:%1023c",
7137 &ws_id
, &mg
, &ma
, &si
, &ar
, s
) != 6)
7138 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
7139 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
7142 if (ws_id
< 0 || ws_id
>= workspace_limit
)
7143 errx(1, "layout: invalid workspace %d", ws_id
+ 1);
7145 if (!strcasecmp(s
, "vertical"))
7147 else if (!strcasecmp(s
, "vertical_flip")) {
7150 } else if (!strcasecmp(s
, "horizontal"))
7152 else if (!strcasecmp(s
, "horizontal_flip")) {
7155 } else if (!strcasecmp(s
, "fullscreen"))
7158 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
7159 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
7162 num_screens
= get_screen_count();
7163 for (i
= 0; i
< num_screens
; i
++) {
7164 ws
= (struct workspace
*)&screens
[i
].ws
;
7165 ws
[ws_id
].cur_layout
= &layouts
[st
];
7167 ws
[ws_id
].always_raise
= ar
;
7168 if (st
== SWM_MAX_STACK
)
7172 for (x
= 0; x
< abs(mg
); x
++) {
7173 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
7174 mg
>= 0 ? SWM_ARG_ID_MASTERGROW
:
7175 SWM_ARG_ID_MASTERSHRINK
);
7179 for (x
= 0; x
< abs(ma
); x
++) {
7180 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
7181 ma
>= 0 ? SWM_ARG_ID_MASTERADD
:
7182 SWM_ARG_ID_MASTERDEL
);
7186 for (x
= 0; x
< abs(si
); x
++) {
7187 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
7188 si
>= 0 ? SWM_ARG_ID_STACKINC
:
7189 SWM_ARG_ID_STACKDEC
);
7194 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
7195 SWM_ARG_ID_FLIPLAYOUT
);
7205 /* config options */
7206 struct config_option
{
7208 int (*func
)(char*, char*, int);
7211 struct config_option configopt
[] = {
7212 { "autorun", setautorun
, 0 },
7213 { "bar_action", setconfvalue
, SWM_S_BAR_ACTION
},
7214 { "bar_at_bottom", setconfvalue
, SWM_S_BAR_AT_BOTTOM
},
7215 { "bar_border", setconfcolor
, SWM_S_COLOR_BAR_BORDER
},
7216 { "bar_border_unfocus", setconfcolor
, SWM_S_COLOR_BAR_BORDER_UNFOCUS
},
7217 { "bar_border_width", setconfvalue
, SWM_S_BAR_BORDER_WIDTH
},
7218 { "bar_color", setconfcolor
, SWM_S_COLOR_BAR
},
7219 { "bar_delay", setconfvalue
, SWM_S_BAR_DELAY
},
7220 { "bar_enabled", setconfvalue
, SWM_S_BAR_ENABLED
},
7221 { "bar_enabled_ws", setconfvalue
, SWM_S_BAR_ENABLED_WS
},
7222 { "bar_font", setconfvalue
, SWM_S_BAR_FONT
},
7223 { "bar_font_color", setconfcolor
, SWM_S_COLOR_BAR_FONT
},
7224 { "bar_format", setconfvalue
, SWM_S_BAR_FORMAT
},
7225 { "bar_justify", setconfvalue
, SWM_S_BAR_JUSTIFY
},
7226 { "bind", setconfbinding
, 0 },
7227 { "border_width", setconfvalue
, SWM_S_BORDER_WIDTH
},
7228 { "clock_enabled", setconfvalue
, SWM_S_CLOCK_ENABLED
},
7229 { "clock_format", setconfvalue
, SWM_S_CLOCK_FORMAT
},
7230 { "color_focus", setconfcolor
, SWM_S_COLOR_FOCUS
},
7231 { "color_unfocus", setconfcolor
, SWM_S_COLOR_UNFOCUS
},
7232 { "cycle_empty", setconfvalue
, SWM_S_CYCLE_EMPTY
},
7233 { "cycle_visible", setconfvalue
, SWM_S_CYCLE_VISIBLE
},
7234 { "dialog_ratio", setconfvalue
, SWM_S_DIALOG_RATIO
},
7235 { "disable_border", setconfvalue
, SWM_S_DISABLE_BORDER
},
7236 { "focus_close", setconfvalue
, SWM_S_FOCUS_CLOSE
},
7237 { "focus_close_wrap", setconfvalue
, SWM_S_FOCUS_CLOSE_WRAP
},
7238 { "focus_default", setconfvalue
, SWM_S_FOCUS_DEFAULT
},
7239 { "focus_mode", setconfvalue
, SWM_S_FOCUS_MODE
},
7240 { "keyboard_mapping", setkeymapping
, 0 },
7241 { "layout", setlayout
, 0 },
7242 { "modkey", setconfmodkey
, 0 },
7243 { "program", setconfspawn
, 0 },
7244 { "quirk", setconfquirk
, 0 },
7245 { "region", setconfregion
, 0 },
7246 { "region_padding", setconfvalue
, SWM_S_REGION_PADDING
},
7247 { "screenshot_app", setconfvalue
, SWM_S_SS_APP
},
7248 { "screenshot_enabled", setconfvalue
, SWM_S_SS_ENABLED
},
7249 { "spawn_position", setconfvalue
, SWM_S_SPAWN_ORDER
},
7250 { "spawn_term", setconfvalue
, SWM_S_SPAWN_TERM
},
7251 { "stack_enabled", setconfvalue
, SWM_S_STACK_ENABLED
},
7252 { "term_width", setconfvalue
, SWM_S_TERM_WIDTH
},
7253 { "tile_gap", setconfvalue
, SWM_S_TILE_GAP
},
7254 { "title_class_enabled", setconfvalue
, SWM_S_TITLE_CLASS_ENABLED
},
7255 { "title_name_enabled", setconfvalue
, SWM_S_TITLE_NAME_ENABLED
},
7256 { "urgent_enabled", setconfvalue
, SWM_S_URGENT_ENABLED
},
7257 { "verbose_layout", setconfvalue
, SWM_S_VERBOSE_LAYOUT
},
7258 { "window_name_enabled", setconfvalue
, SWM_S_WINDOW_NAME_ENABLED
},
7259 { "workspace_limit", setconfvalue
, SWM_S_WORKSPACE_LIMIT
},
7263 _add_startup_exception(const char *fmt
, va_list ap
)
7265 if (vasprintf(&startup_exception
, fmt
, ap
) == -1)
7266 warn("%s: asprintf", __func__
);
7270 add_startup_exception(const char *fmt
, ...)
7276 if (startup_exception
)
7279 /* force bar to be enabled due to exception */
7283 _add_startup_exception(fmt
, ap
);
7288 conf_load(const char *filename
, int keymapping
)
7291 char *line
= NULL
, *cp
, *optsub
, *optval
= NULL
;
7292 size_t linelen
, lineno
= 0;
7293 int wordlen
, i
, optidx
;
7294 struct config_option
*opt
= NULL
;
7296 DNPRINTF(SWM_D_CONF
, "conf_load: begin\n");
7298 if (filename
== NULL
) {
7299 warnx("conf_load: no filename");
7303 DNPRINTF(SWM_D_CONF
, "conf_load: open %s\n", filename
);
7305 if ((config
= fopen(filename
, "r")) == NULL
) {
7306 warn("conf_load: fopen: %s", filename
);
7310 while (!feof(config
)) {
7314 if ((line
= fparseln(config
, &linelen
, &lineno
, NULL
,
7315 FPARSELN_UNESCCOMM
| FPARSELN_UNESCCONT
)) == NULL
) {
7317 err(1, "%s", filename
);
7322 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
7323 if (cp
[0] == '\0') {
7327 /* get config option */
7328 wordlen
= strcspn(cp
, "=[ \t\n");
7330 add_startup_exception("%s: line %zd: no option found",
7335 for (i
= 0; i
< LENGTH(configopt
); i
++) {
7336 opt
= &configopt
[i
];
7337 if (!strncasecmp(cp
, opt
->optname
, wordlen
) &&
7338 (int)strlen(opt
->optname
) == wordlen
) {
7344 add_startup_exception("%s: line %zd: unknown option "
7345 "%.*s", filename
, lineno
, wordlen
, cp
);
7348 if (keymapping
&& opt
&& strcmp(opt
->optname
, "bind")) {
7349 add_startup_exception("%s: line %zd: invalid option "
7350 "%.*s", filename
, lineno
, wordlen
, cp
);
7354 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
7356 /* from here on out we call goto invalid to continue */
7358 /* get [selector] if any */
7362 wordlen
= strcspn(cp
, "]");
7365 add_startup_exception("%s: line %zd: "
7366 "syntax error", filename
, lineno
);
7370 if (asprintf(&optsub
, "%.*s", wordlen
, cp
) ==
7372 add_startup_exception("%s: line %zd: "
7373 "unable to allocatememory for "
7374 "selector", filename
, lineno
);
7379 cp
+= strspn(cp
, "] \t\n"); /* eat trailing */
7381 cp
+= strspn(cp
, "= \t\n"); /* eat trailing */
7383 optval
= strdup(cp
);
7384 if (strlen(optval
) == 0) {
7385 add_startup_exception("%s: line %zd: must supply value "
7386 "to %s", filename
, lineno
,
7387 configopt
[optidx
].optname
);
7390 /* call function to deal with it all */
7391 if (configopt
[optidx
].func(optsub
, optval
,
7392 configopt
[optidx
].funcflags
) != 0) {
7393 add_startup_exception("%s: line %zd: invalid data for "
7394 "%s", filename
, lineno
, configopt
[optidx
].optname
);
7411 DNPRINTF(SWM_D_CONF
, "conf_load: end\n");
7417 set_child_transient(struct ws_win
*win
, xcb_window_t
*trans
)
7419 struct ws_win
*parent
, *w
;
7420 struct swm_region
*r
;
7421 struct workspace
*ws
;
7422 xcb_icccm_wm_hints_t wmh
;
7424 parent
= find_window(win
->transient
);
7426 parent
->focus_child
= win
;
7428 DNPRINTF(SWM_D_MISC
, "set_child_transient: parent doesn't exist"
7429 " for 0x%x trans 0x%x\n", win
->id
, win
->transient
);
7431 r
= root_to_region(win
->wa
->root
, SWM_CK_ALL
);
7433 /* parent doen't exist in our window list */
7434 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
7435 if (xcb_icccm_get_wm_hints_reply(conn
,
7436 xcb_icccm_get_wm_hints(conn
, w
->id
),
7438 warnx("can't get hints for 0x%x", w
->id
);
7442 if (win
->hints
.window_group
!= wmh
.window_group
)
7445 w
->focus_child
= win
;
7446 win
->transient
= w
->id
;
7448 DNPRINTF(SWM_D_MISC
, "set_child_transient: adjusting "
7449 "transient to 0x%x\n", win
->transient
);
7456 window_get_pid(xcb_window_t win
)
7461 xcb_get_property_cookie_t pc
;
7462 xcb_get_property_reply_t
*pr
;
7464 apid
= get_atom_from_string("_NET_WM_PID");
7465 if (apid
== XCB_ATOM_NONE
)
7468 pc
= xcb_get_property(conn
, 0, win
, apid
, XCB_ATOM_CARDINAL
, 0, 1);
7469 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
7472 if (pr
->type
!= XCB_ATOM_CARDINAL
) {
7477 if (pr
->type
== apid
&& pr
->format
== 32)
7478 ret
= *((pid_t
*)xcb_get_property_value(pr
));
7484 apid
= get_atom_from_string("_SWM_PID");
7485 pc
= xcb_get_property(conn
, 0, win
, apid
, XCB_ATOM_STRING
,
7487 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
7490 if (pr
->type
!= apid
) {
7495 ret
= (pid_t
)strtonum(xcb_get_property_value(pr
), 0, INT_MAX
, &errstr
);
7502 get_ws_idx(xcb_window_t id
)
7508 xcb_get_property_reply_t
*gpr
;
7510 gpr
= xcb_get_property_reply(conn
,
7511 xcb_get_property(conn
, 0, id
, a_swm_ws
,
7512 XCB_ATOM_STRING
, 0, SWM_PROPLEN
),
7517 proplen
= xcb_get_property_value_length(gpr
);
7519 prop
= malloc(proplen
+ 1);
7522 xcb_get_property_value(gpr
),
7524 prop
[proplen
] = '\0';
7531 DNPRINTF(SWM_D_PROP
, "get_ws_idx: _SWM_WS: %s\n", prop
);
7532 ws_idx
= (int)strtonum(prop
, 0, workspace_limit
- 1, &errstr
);
7534 DNPRINTF(SWM_D_PROP
, "get_ws_idx: window: #%s: %s",
7544 manage_window(xcb_window_t id
, uint16_t mapped
)
7546 xcb_window_t trans
= XCB_WINDOW_NONE
;
7547 struct ws_win
*win
, *ww
;
7549 char ws_idx_str
[SWM_PROPLEN
];
7550 struct swm_region
*r
;
7554 xcb_icccm_get_wm_protocols_reply_t wpr
;
7556 if ((win
= find_window(id
)) != NULL
) {
7557 DNPRINTF(SWM_D_MISC
, "manage_window: win 0x%x already "
7558 "managed; skipping.)\n", id
);
7559 return (win
); /* Already managed. */
7562 /* See if window is on the unmanaged list. */
7563 if ((win
= find_unmanaged_window(id
)) != NULL
) {
7564 DNPRINTF(SWM_D_MISC
, "manage_window: win 0x%x found on "
7565 "unmanaged list.\n", id
);
7566 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
7569 set_child_transient(win
, &trans
);
7573 DNPRINTF(SWM_D_MISC
, "manage_window: win 0x%x is new.\n", id
);
7576 /* Create and initialize ws_win object. */
7577 if ((win
= calloc(1, sizeof(struct ws_win
))) == NULL
)
7578 err(1, "manage_window: calloc: failed to allocate memory for "
7583 /* Get window geometry. */
7584 win
->wa
= xcb_get_geometry_reply(conn
,
7585 xcb_get_geometry(conn
, win
->id
),
7588 /* Figure out which region the window belongs to. */
7589 r
= root_to_region(win
->wa
->root
, SWM_CK_ALL
);
7591 /* Ignore window border if there is one. */
7592 WIDTH(win
) = win
->wa
->width
;
7593 HEIGHT(win
) = win
->wa
->height
;
7594 X(win
) = win
->wa
->x
+ win
->wa
->border_width
- border_width
;
7595 Y(win
) = win
->wa
->y
+ win
->wa
->border_width
- border_width
;
7597 win
->mapped
= mapped
;
7598 win
->floatmaxed
= 0;
7599 win
->ewmh_flags
= 0;
7600 win
->s
= r
->s
; /* this never changes */
7602 store_float_geom(win
, r
);
7604 /* Get WM_SIZE_HINTS. */
7605 xcb_icccm_get_wm_normal_hints_reply(conn
,
7606 xcb_icccm_get_wm_normal_hints(conn
, win
->id
),
7610 xcb_icccm_get_wm_hints_reply(conn
,
7611 xcb_icccm_get_wm_hints(conn
, win
->id
),
7614 /* Get WM_TRANSIENT_FOR; see if window is a transient. */
7615 xcb_icccm_get_wm_transient_for_reply(conn
,
7616 xcb_icccm_get_wm_transient_for(conn
, win
->id
),
7619 win
->transient
= trans
;
7620 set_child_transient(win
, &win
->transient
);
7623 /* Get supported protocols. */
7624 if (xcb_icccm_get_wm_protocols_reply(conn
,
7625 xcb_icccm_get_wm_protocols(conn
, win
->id
, a_prot
),
7627 for (i
= 0; i
< wpr
.atoms_len
; i
++) {
7628 if (wpr
.atoms
[i
] == a_takefocus
)
7629 win
->take_focus
= 1;
7630 if (wpr
.atoms
[i
] == a_delete
)
7631 win
->can_delete
= 1;
7633 xcb_icccm_get_wm_protocols_reply_wipe(&wpr
);
7636 win
->iconic
= get_swm_iconic(win
);
7638 /* Figure out which workspace the window belongs to. */
7639 if ((p
= find_pid(window_get_pid(win
->id
))) != NULL
) {
7640 win
->ws
= &r
->s
->ws
[p
->ws
];
7641 TAILQ_REMOVE(&pidlist
, p
, entry
);
7644 } else if ((ws_idx
= get_ws_idx(win
->id
)) != -1 &&
7646 /* _SWM_WS is set; use that. */
7647 win
->ws
= &r
->s
->ws
[ws_idx
];
7648 } else if (trans
&& (ww
= find_window(trans
)) != NULL
) {
7649 /* Launch transients in the same ws as parent. */
7655 /* Set the _SWM_WS atom so we can remember this after reincarnation. */
7656 if (snprintf(ws_idx_str
, SWM_PROPLEN
, "%d", win
->ws
->idx
) <
7658 DNPRINTF(SWM_D_PROP
, "manage_window: set _SWM_WS: %s\n",
7660 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
7661 a_swm_ws
, XCB_ATOM_STRING
, 8, strlen(ws_idx_str
),
7666 ewmh_autoquirk(win
);
7668 /* Determine initial quirks. */
7669 if (xcb_icccm_get_wm_class_reply(conn
,
7670 xcb_icccm_get_wm_class(conn
, win
->id
),
7672 DNPRINTF(SWM_D_CLASS
, "manage_window: class: %s, name: %s\n",
7673 win
->ch
.class_name
, win
->ch
.instance_name
);
7675 /* java is retarded so treat it special */
7676 if (strstr(win
->ch
.instance_name
, "sun-awt")) {
7677 DNPRINTF(SWM_D_CLASS
, "manage_window: java window "
7682 TAILQ_FOREACH(qp
, &quirks
, entry
) {
7683 if (!strcmp(win
->ch
.class_name
, qp
->class) &&
7684 !strcmp(win
->ch
.instance_name
, qp
->name
)) {
7685 DNPRINTF(SWM_D_CLASS
, "manage_window: on quirks"
7686 "list; mask: 0x%lx\n", qp
->quirk
);
7687 if (qp
->quirk
& SWM_Q_FLOAT
)
7689 win
->quirks
= qp
->quirk
;
7694 /* Alter window position if quirky */
7695 if (win
->quirks
& SWM_Q_ANYWHERE
)
7698 /* Reset font sizes (the bruteforce way; no default keybinding). */
7699 if (win
->quirks
& SWM_Q_XTERM_FONTADJ
) {
7700 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
7701 fake_keypress(win
, XK_KP_Subtract
, XCB_MOD_MASK_SHIFT
);
7702 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
7703 fake_keypress(win
, XK_KP_Add
, XCB_MOD_MASK_SHIFT
);
7706 /* Make sure window is positioned inside its region, if its active. */
7708 constrain_window(win
, win
->ws
->r
, 0);
7712 /* Select which X events to monitor and set border pixel color. */
7713 wa
[0] = win
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
;
7714 wa
[1] = XCB_EVENT_MASK_ENTER_WINDOW
| XCB_EVENT_MASK_PROPERTY_CHANGE
|
7715 XCB_EVENT_MASK_STRUCTURE_NOTIFY
;
7717 wa
[1] |= XCB_EVENT_MASK_LEAVE_WINDOW
| XCB_EVENT_MASK_FOCUS_CHANGE
;
7720 xcb_change_window_attributes(conn
, win
->id
, XCB_CW_BORDER_PIXEL
|
7721 XCB_CW_EVENT_MASK
, wa
);
7724 /* Figure out where to stack the window in the workspace. */
7725 if (trans
&& (ww
= find_window(trans
)))
7726 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, ww
, win
, entry
);
7727 else if (win
->ws
->focus
&& spawn_position
== SWM_STACK_ABOVE
)
7728 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, win
->ws
->focus
, win
,
7730 else if (win
->ws
->focus
&& spawn_position
== SWM_STACK_BELOW
)
7731 TAILQ_INSERT_BEFORE(win
->ws
->focus
, win
, entry
);
7732 else switch (spawn_position
) {
7735 case SWM_STACK_ABOVE
:
7736 TAILQ_INSERT_TAIL(&win
->ws
->winlist
, win
, entry
);
7738 case SWM_STACK_BOTTOM
:
7739 case SWM_STACK_BELOW
:
7740 TAILQ_INSERT_HEAD(&win
->ws
->winlist
, win
, entry
);
7743 /* Get initial _NET_WM_STATE */
7744 ewmh_get_win_state(win
);
7745 /* Set initial _NET_WM_ALLOWED_ACTIONS */
7746 ewmh_update_actions(win
);
7750 DNPRINTF(SWM_D_MISC
, "manage_window: done. window: 0x%x, (x,y) w x h: "
7751 "(%d,%d) %d x %d, ws: %d, iconic: %s, transient: 0x%x\n", win
->id
,
7752 X(win
), Y(win
), WIDTH(win
), HEIGHT(win
), win
->ws
->idx
,
7753 YESNO(win
->iconic
), win
->transient
);
7759 free_window(struct ws_win
*win
)
7761 DNPRINTF(SWM_D_MISC
, "free_window: window: 0x%x\n", win
->id
);
7766 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
7771 xcb_icccm_get_wm_class_reply_wipe(&win
->ch
);
7776 memset(win
, 0xff, sizeof *win
); /* XXX kill later */
7779 DNPRINTF(SWM_D_MISC
, "free_window: done.\n");
7783 unmanage_window(struct ws_win
*win
)
7785 struct ws_win
*parent
;
7790 DNPRINTF(SWM_D_MISC
, "unmanage_window: window: 0x%x\n", win
->id
);
7792 if (win
->transient
) {
7793 parent
= find_window(win
->transient
);
7795 parent
->focus_child
= NULL
;
7798 TAILQ_REMOVE(&win
->ws
->winlist
, win
, entry
);
7799 TAILQ_INSERT_TAIL(&win
->ws
->unmanagedlist
, win
, entry
);
7803 expose(xcb_expose_event_t
*e
)
7806 struct swm_region
*r
;
7808 DNPRINTF(SWM_D_EVENT
, "expose: window: 0x%x\n", e
->window
);
7810 num_screens
= get_screen_count();
7811 for (i
= 0; i
< num_screens
; i
++)
7812 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
7813 if (e
->window
== WINID(r
->bar
))
7821 focusin(xcb_focus_in_event_t
*e
)
7823 DNPRINTF(SWM_D_EVENT
, "focusin: window: 0x%x, mode: %s(%u), "
7824 "detail: %s(%u)\n", e
->event
, get_notify_mode_label(e
->mode
),
7825 e
->mode
, get_notify_detail_label(e
->detail
), e
->detail
);
7829 focusout(xcb_focus_out_event_t
*e
)
7831 DNPRINTF(SWM_D_EVENT
, "focusout: window: 0x%x, mode: %s(%u), "
7832 "detail: %s(%u)\n", e
->event
, get_notify_mode_label(e
->mode
),
7833 e
->mode
, get_notify_detail_label(e
->detail
), e
->detail
);
7838 keypress(xcb_key_press_event_t
*e
)
7840 xcb_keysym_t keysym
;
7843 keysym
= xcb_key_press_lookup_keysym(syms
, e
, 0);
7845 DNPRINTF(SWM_D_EVENT
, "keypress: keysym: %u, win (x,y): 0x%x (%d,%d), "
7846 "detail: %u, time: %u, root (x,y): 0x%x (%d,%d), child: 0x%x, "
7847 "state: %u, same_screen: %s\n", keysym
, e
->event
, e
->event_x
,
7848 e
->event_y
, e
->detail
, e
->time
, e
->root
, e
->root_x
, e
->root_y
,
7849 e
->child
, e
->state
, YESNO(e
->same_screen
));
7851 if ((kp
= key_lookup(CLEANMASK(e
->state
), keysym
)) == NULL
)
7854 last_event_time
= e
->time
;
7856 if (kp
->funcid
== KF_SPAWN_CUSTOM
)
7857 spawn_custom(root_to_region(e
->root
, SWM_CK_ALL
),
7858 &(keyfuncs
[kp
->funcid
].args
), kp
->spawn_name
);
7859 else if (keyfuncs
[kp
->funcid
].func
)
7860 keyfuncs
[kp
->funcid
].func(root_to_region(e
->root
, SWM_CK_ALL
),
7861 &(keyfuncs
[kp
->funcid
].args
));
7864 /* Unfreeze grab events. */
7865 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
, e
->time
);
7868 DNPRINTF(SWM_D_EVENT
, "keypress: done.\n");
7872 buttonpress(xcb_button_press_event_t
*e
)
7874 struct ws_win
*win
= NULL
;
7875 struct swm_region
*r
, *old_r
;
7879 DNPRINTF(SWM_D_EVENT
, "buttonpress: win (x,y): 0x%x (%d,%d), "
7880 "detail: %u, time: %u, root (x,y): 0x%x (%d,%d), child: 0x%x, "
7881 "state: %u, same_screen: %s\n", e
->event
, e
->event_x
, e
->event_y
,
7882 e
->detail
, e
->time
, e
->root
, e
->root_x
, e
->root_y
, e
->child
,
7883 e
->state
, YESNO(e
->same_screen
));
7885 if (e
->event
== e
->root
) {
7886 if (e
->child
!= 0) {
7887 win
= find_window(e
->child
);
7888 /* Pass ButtonPress to window if it isn't managed. */
7892 /* Focus on empty region */
7893 /* If no windows on region if its empty. */
7894 r
= root_to_region(e
->root
, SWM_CK_POINTER
);
7896 DNPRINTF(SWM_D_EVENT
, "buttonpress: "
7897 "NULL region; ignoring.\n");
7901 if (TAILQ_EMPTY(&r
->ws
->winlist
)) {
7902 old_r
= root_to_region(e
->root
, SWM_CK_FOCUS
);
7903 if (old_r
&& old_r
!= r
)
7904 unfocus_win(old_r
->ws
->focus
);
7906 xcb_set_input_focus(conn
,
7907 XCB_INPUT_FOCUS_PARENT
, e
->root
, e
->time
);
7909 /* Clear bar since empty. */
7917 win
= find_window(e
->event
);
7923 last_event_time
= e
->time
;
7925 focus_win(get_focus_magic(win
));
7927 for (i
= 0; i
< LENGTH(buttons
); i
++)
7928 if (client_click
== buttons
[i
].action
&& buttons
[i
].func
&&
7929 buttons
[i
].button
== e
->detail
&&
7930 CLEANMASK(buttons
[i
].mask
) == CLEANMASK(e
->state
)) {
7931 buttons
[i
].func(win
, &buttons
[i
].args
);
7937 DNPRINTF(SWM_D_EVENT
, "buttonpress: passing to window.\n");
7938 /* Replay event to event window */
7939 xcb_allow_events(conn
, XCB_ALLOW_REPLAY_POINTER
, e
->time
);
7941 DNPRINTF(SWM_D_EVENT
, "buttonpress: handled.\n");
7942 /* Unfreeze grab events. */
7943 xcb_allow_events(conn
, XCB_ALLOW_SYNC_POINTER
, e
->time
);
7951 print_win_geom(xcb_window_t w
)
7953 xcb_get_geometry_reply_t
*wa
;
7955 wa
= xcb_get_geometry_reply(conn
, xcb_get_geometry(conn
, w
), NULL
);
7958 DNPRINTF(SWM_D_MISC
, "print_win_geom: window not found: 0x%x\n",
7963 DNPRINTF(SWM_D_MISC
, "print_win_geom: window: 0x%x, root: 0x%x, "
7964 "depth: %u, (x,y) w x h: (%d,%d) %d x %d, border: %d\n",
7965 w
, wa
->root
, wa
->depth
, wa
->x
, wa
->y
, wa
->width
, wa
->height
,
7974 get_stack_mode_name(uint8_t mode
)
7979 case XCB_STACK_MODE_ABOVE
:
7982 case XCB_STACK_MODE_BELOW
:
7985 case XCB_STACK_MODE_TOP_IF
:
7988 case XCB_STACK_MODE_BOTTOM_IF
:
7991 case XCB_STACK_MODE_OPPOSITE
:
8003 configurerequest(xcb_configure_request_event_t
*e
)
8006 struct swm_region
*r
= NULL
;
8009 uint32_t wc
[7] = {0};
8011 if ((win
= find_window(e
->window
)) == NULL
)
8012 if ((win
= find_unmanaged_window(e
->window
)) == NULL
)
8016 if (swm_debug
& SWM_D_EVENT
) {
8017 print_win_geom(e
->window
);
8019 DNPRINTF(SWM_D_EVENT
, "configurerequest: window: 0x%x, "
8020 "parent: 0x%x, new: %s, value_mask: %u { ", e
->window
,
8021 e
->parent
, YESNO(new), e
->value_mask
);
8022 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
)
8023 DPRINTF("X: %d ", e
->x
);
8024 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
)
8025 DPRINTF("Y: %d ", e
->y
);
8026 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
)
8027 DPRINTF("W: %u ", e
->width
);
8028 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
)
8029 DPRINTF("H: %u ", e
->height
);
8030 if (e
->value_mask
& XCB_CONFIG_WINDOW_BORDER_WIDTH
)
8031 DPRINTF("Border: %u ", e
->border_width
);
8032 if (e
->value_mask
& XCB_CONFIG_WINDOW_SIBLING
)
8033 DPRINTF("Sibling: 0x%x ", e
->sibling
);
8034 if (e
->value_mask
& XCB_CONFIG_WINDOW_STACK_MODE
)
8035 DPRINTF("StackMode: %s(%u) ",
8036 get_stack_mode_name(e
->stack_mode
), e
->stack_mode
);
8042 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
) {
8043 mask
|= XCB_CONFIG_WINDOW_X
;
8046 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
) {
8047 mask
|= XCB_CONFIG_WINDOW_Y
;
8050 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
) {
8051 mask
|= XCB_CONFIG_WINDOW_WIDTH
;
8054 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
) {
8055 mask
|= XCB_CONFIG_WINDOW_HEIGHT
;
8056 wc
[i
++] = e
->height
;
8058 if (e
->value_mask
& XCB_CONFIG_WINDOW_BORDER_WIDTH
) {
8059 mask
|= XCB_CONFIG_WINDOW_BORDER_WIDTH
;
8060 wc
[i
++] = e
->border_width
;
8062 if (e
->value_mask
& XCB_CONFIG_WINDOW_SIBLING
) {
8063 mask
|= XCB_CONFIG_WINDOW_SIBLING
;
8064 wc
[i
++] = e
->sibling
;
8066 if (e
->value_mask
& XCB_CONFIG_WINDOW_STACK_MODE
) {
8067 mask
|= XCB_CONFIG_WINDOW_STACK_MODE
;
8068 wc
[i
++] = e
->stack_mode
;
8072 xcb_configure_window(conn
, e
->window
, mask
, wc
);
8075 } else if ((!win
->manual
|| win
->quirks
& SWM_Q_ANYWHERE
) &&
8076 !(win
->ewmh_flags
& EWMH_F_FULLSCREEN
)) {
8079 else if (win
->ws
->old_r
)
8082 /* windows are centered unless ANYWHERE quirk is set. */
8083 if (win
->quirks
& SWM_Q_ANYWHERE
) {
8084 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
) {
8085 win
->g_float
.x
= e
->x
;
8087 win
->g_float
.x
-= X(r
);
8090 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
) {
8091 win
->g_float
.y
= e
->y
;
8093 win
->g_float
.y
-= Y(r
);
8097 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
)
8098 win
->g_float
.w
= e
->width
;
8100 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
)
8101 win
->g_float
.h
= e
->height
;
8103 win
->g_floatvalid
= 1;
8105 if (win
->floating
&& r
&& (win
->transient
||
8106 win
->ws
->cur_layout
!= &layouts
[SWM_MAX_STACK
])) {
8107 WIDTH(win
) = win
->g_float
.w
;
8108 HEIGHT(win
) = win
->g_float
.h
;
8111 stack_floater(win
, r
);
8123 DNPRINTF(SWM_D_EVENT
, "configurerequest: done.\n");
8127 configurenotify(xcb_configure_notify_event_t
*e
)
8131 DNPRINTF(SWM_D_EVENT
, "configurenotify: win 0x%x, event win: 0x%x, "
8132 "(x,y) WxH: (%d,%d) %ux%u, border: %u, above_sibling: 0x%x, "
8133 "override_redirect: %s\n", e
->window
, e
->event
, e
->x
, e
->y
,
8134 e
->width
, e
->height
, e
->border_width
, e
->above_sibling
,
8135 YESNO(e
->override_redirect
));
8137 win
= find_window(e
->window
);
8139 xcb_icccm_get_wm_normal_hints_reply(conn
,
8140 xcb_icccm_get_wm_normal_hints(conn
, win
->id
),
8143 if (font_adjusted
) {
8151 destroynotify(xcb_destroy_notify_event_t
*e
)
8155 DNPRINTF(SWM_D_EVENT
, "destroynotify: window: 0x%x\n", e
->window
);
8157 if ((win
= find_window(e
->window
)) == NULL
) {
8158 if ((win
= find_unmanaged_window(e
->window
)) == NULL
)
8164 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
8165 /* If we were focused, make sure we focus on something else. */
8166 if (win
== win
->ws
->focus
)
8167 win
->ws
->focus_pending
= get_focus_prev(win
);
8170 unmanage_window(win
);
8173 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
8174 if (win
->ws
->focus_pending
) {
8175 focus_win(win
->ws
->focus_pending
);
8176 win
->ws
->focus_pending
= NULL
;
8187 get_notify_detail_label(uint8_t detail
)
8192 case XCB_NOTIFY_DETAIL_ANCESTOR
:
8195 case XCB_NOTIFY_DETAIL_VIRTUAL
:
8198 case XCB_NOTIFY_DETAIL_INFERIOR
:
8201 case XCB_NOTIFY_DETAIL_NONLINEAR
:
8202 label
= "Nonlinear";
8204 case XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL
:
8205 label
= "NonlinearVirtual";
8207 case XCB_NOTIFY_DETAIL_POINTER
:
8210 case XCB_NOTIFY_DETAIL_POINTER_ROOT
:
8211 label
= "PointerRoot";
8213 case XCB_NOTIFY_DETAIL_NONE
:
8224 get_notify_mode_label(uint8_t mode
)
8229 case XCB_NOTIFY_MODE_NORMAL
:
8232 case XCB_NOTIFY_MODE_GRAB
:
8235 case XCB_NOTIFY_MODE_UNGRAB
:
8238 case XCB_NOTIFY_MODE_WHILE_GRABBED
:
8239 label
= "WhileGrabbed";
8250 enternotify(xcb_enter_notify_event_t
*e
)
8253 struct swm_region
*r
;
8255 DNPRINTF(SWM_D_FOCUS
, "enternotify: time: %u, win (x,y): 0x%x "
8256 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): 0x%x (%d,%d), "
8257 "child: 0x%x, same_screen_focus: %s, state: %d\n",
8258 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
8259 get_notify_mode_label(e
->mode
), e
->mode
,
8260 get_notify_detail_label(e
->detail
), e
->detail
,
8261 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
8262 YESNO(e
->same_screen_focus
), e
->state
);
8264 if (focus_mode
== SWM_FOCUS_MANUAL
&&
8265 e
->mode
== XCB_NOTIFY_MODE_NORMAL
) {
8266 DNPRINTF(SWM_D_EVENT
, "enternotify: manual focus; ignoring.\n");
8270 last_event_time
= e
->time
;
8272 if ((win
= find_window(e
->event
)) == NULL
) {
8273 if (e
->event
== e
->root
) {
8274 /* If no windows on pointer region, then focus root. */
8275 r
= root_to_region(e
->root
, SWM_CK_POINTER
);
8277 DNPRINTF(SWM_D_EVENT
, "enternotify: "
8278 "NULL region; ignoring.\n");
8284 DNPRINTF(SWM_D_EVENT
, "enternotify: window is NULL; "
8289 focus_win(get_focus_magic(win
));
8297 leavenotify(xcb_leave_notify_event_t
*e
)
8299 DNPRINTF(SWM_D_FOCUS
, "leavenotify: time: %u, win (x,y): 0x%x "
8300 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): 0x%x (%d,%d), "
8301 "child: 0x%x, same_screen_focus: %s, state: %d\n",
8302 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
8303 get_notify_mode_label(e
->mode
), e
->mode
,
8304 get_notify_detail_label(e
->detail
), e
->detail
,
8305 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
8306 YESNO(e
->same_screen_focus
), e
->state
);
8311 mapnotify(xcb_map_notify_event_t
*e
)
8315 DNPRINTF(SWM_D_EVENT
, "mapnotify: window: 0x%x\n", e
->window
);
8317 if ((win
= find_window(e
->window
)) == NULL
)
8318 win
= manage_window(e
->window
, 1);
8321 set_win_state(win
, XCB_ICCCM_WM_STATE_NORMAL
);
8323 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
8324 if (win
->ws
->focus_pending
== win
) {
8326 win
->ws
->focus_pending
= NULL
;
8335 mappingnotify(xcb_mapping_notify_event_t
*e
)
8337 xcb_refresh_keyboard_mapping(syms
, e
);
8339 if (e
->request
== XCB_MAPPING_KEYBOARD
)
8344 maprequest(xcb_map_request_event_t
*e
)
8346 struct ws_win
*win
, *w
= NULL
;
8347 xcb_get_window_attributes_reply_t
*war
;
8349 DNPRINTF(SWM_D_EVENT
, "maprequest: win 0x%x\n",
8352 war
= xcb_get_window_attributes_reply(conn
,
8353 xcb_get_window_attributes(conn
, e
->window
),
8356 DNPRINTF(SWM_D_EVENT
, "maprequest: window lost.\n");
8360 if (war
->override_redirect
) {
8361 DNPRINTF(SWM_D_EVENT
, "maprequest: override_redirect; "
8366 win
= manage_window(e
->window
,
8367 (war
->map_state
== XCB_MAP_STATE_VIEWABLE
));
8369 /* The new window should get focus; prepare. */
8370 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
8371 !(win
->quirks
& SWM_Q_NOFOCUSONMAP
)) {
8372 if (win
->quirks
& SWM_Q_FOCUSONMAP_SINGLE
) {
8373 /* See if other wins of same type are already mapped. */
8374 TAILQ_FOREACH(w
, &win
->ws
->winlist
, entry
) {
8375 if (w
== win
|| !w
->mapped
)
8378 if (!strcmp(w
->ch
.class_name
,
8379 win
->ch
.class_name
) &&
8380 !strcmp(w
->ch
.instance_name
,
8381 win
->ch
.instance_name
))
8387 win
->ws
->focus_pending
= get_focus_magic(win
);
8390 /* All windows need to be mapped if they are in the current workspace.*/
8394 /* Ignore EnterNotify to handle the mapnotify without interference. */
8395 if (focus_mode
== SWM_FOCUS_DEFAULT
)
8396 event_drain(XCB_ENTER_NOTIFY
);
8399 DNPRINTF(SWM_D_EVENT
, "maprequest: done.\n");
8403 motionnotify(xcb_motion_notify_event_t
*e
)
8405 struct swm_region
*r
;
8408 DNPRINTF(SWM_D_FOCUS
, "motionnotify: time: %u, win (x,y): 0x%x "
8409 "(%d,%d), detail: %s(%d), root (x,y): 0x%x (%d,%d), "
8410 "child: 0x%x, same_screen_focus: %s, state: %d\n",
8411 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
8412 get_notify_detail_label(e
->detail
), e
->detail
,
8413 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
8414 YESNO(e
->same_screen
), e
->state
);
8416 last_event_time
= e
->time
;
8418 num_screens
= get_screen_count();
8419 for (i
= 0; i
< num_screens
; i
++)
8420 if (screens
[i
].root
== e
->root
)
8423 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
8424 if (X(r
) <= e
->root_x
&& e
->root_x
< MAX_X(r
) &&
8425 Y(r
) <= e
->root_y
&& e
->root_y
< MAX_Y(r
))
8433 get_atom_name(xcb_atom_t atom
)
8438 * This should be disabled during most debugging since
8439 * xcb_get_* causes an xcb_flush.
8442 xcb_get_atom_name_reply_t
*r
;
8444 r
= xcb_get_atom_name_reply(conn
,
8445 xcb_get_atom_name(conn
, atom
),
8448 len
= xcb_get_atom_name_name_length(r
);
8450 name
= malloc(len
+ 1);
8452 memcpy(name
, xcb_get_atom_name_name(r
), len
);
8466 propertynotify(xcb_property_notify_event_t
*e
)
8472 name
= get_atom_name(e
->atom
);
8473 DNPRINTF(SWM_D_EVENT
, "propertynotify: window: 0x%x, atom: %s(%u), "
8474 "time: %#x, state: %u\n", e
->window
, name
, e
->atom
, e
->time
,
8478 win
= find_window(e
->window
);
8482 last_event_time
= e
->time
;
8484 if (e
->atom
== a_swm_iconic
) {
8485 if (e
->state
== XCB_PROPERTY_NEW_VALUE
) {
8486 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
8487 win
->ws
->focus_pending
= get_focus_prev(win
);
8494 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
8495 focus_win(win
->ws
->focus_pending
);
8496 win
->ws
->focus_pending
= NULL
;
8500 } else if (e
->state
== XCB_PROPERTY_DELETE
) {
8501 /* The window is no longer iconic, restack ws. */
8502 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
8503 win
->ws
->focus_pending
= get_focus_magic(win
);
8507 /* Flush EnterNotify for mapnotify, if needed. */
8510 } else if (e
->atom
== a_state
) {
8511 /* State just changed, make sure it gets focused if mapped. */
8512 if (e
->state
== XCB_PROPERTY_NEW_VALUE
) {
8513 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
8515 win
->ws
->focus_pending
== win
) {
8516 focus_win(win
->ws
->focus_pending
);
8517 win
->ws
->focus_pending
= NULL
;
8521 } else if (e
->atom
== XCB_ATOM_WM_CLASS
||
8522 e
->atom
== XCB_ATOM_WM_NAME
) {
8530 unmapnotify(xcb_unmap_notify_event_t
*e
)
8533 struct workspace
*ws
;
8535 DNPRINTF(SWM_D_EVENT
, "unmapnotify: window: 0x%x\n", e
->window
);
8537 /* If we aren't managing the window, then ignore. */
8538 win
= find_window(e
->window
);
8539 if (win
== NULL
|| win
->id
!= e
->window
)
8544 if (getstate(e
->window
) != XCB_ICCCM_WM_STATE_ICONIC
)
8545 set_win_state(win
, XCB_ICCCM_WM_STATE_ICONIC
);
8548 /* window unmapped itself */
8549 /* do unmap/unfocus/restack and unmanage */
8552 /* If win was focused, make sure to focus on something else. */
8553 if (win
== ws
->focus
) {
8554 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
8555 ws
->focus_pending
= get_focus_prev(win
);
8556 DNPRINTF(SWM_D_EVENT
, "unmapnotify: "
8557 "focus_pending: 0x%x\n",
8558 WINID(ws
->focus_pending
));
8564 unmanage_window(win
);
8569 if (focus_mode
== SWM_FOCUS_FOLLOW
) {
8571 focus_win(get_pointer_win(ws
->r
->s
->root
));
8573 if (ws
->focus_pending
) {
8574 focus_win(ws
->focus_pending
);
8575 ws
->focus_pending
= NULL
;
8580 if (getstate(e
->window
) == XCB_ICCCM_WM_STATE_NORMAL
)
8581 set_win_state(win
, XCB_ICCCM_WM_STATE_ICONIC
);
8588 visibilitynotify(xcb_visibility_notify_event_t
*e
)
8590 DNPRINTF(SWM_D_EVENT
, "visibilitynotify: window: 0x%x\n",
8596 clientmessage(xcb_client_message_event_t
*e
)
8599 xcb_map_request_event_t mre
;
8603 name
= get_atom_name(e
->type
);
8604 DNPRINTF(SWM_D_EVENT
, "clientmessage: window: 0x%x, atom: %s(%u)\n",
8605 e
->window
, name
, e
->type
);
8608 win
= find_window(e
->window
);
8611 if (e
->type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
8612 /* Manage the window with maprequest. */
8613 DNPRINTF(SWM_D_EVENT
, "clientmessage: request focus on "
8614 "unmanaged window.\n");
8615 mre
.window
= e
->window
;
8621 if (e
->type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
8622 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_ACTIVE_WINDOW\n");
8625 if (e
->type
== ewmh
[_NET_CLOSE_WINDOW
].atom
) {
8626 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_CLOSE_WINDOW\n");
8627 if (win
->can_delete
)
8628 client_msg(win
, a_delete
, 0);
8630 xcb_kill_client(conn
, win
->id
);
8632 if (e
->type
== ewmh
[_NET_MOVERESIZE_WINDOW
].atom
) {
8633 DNPRINTF(SWM_D_EVENT
,
8634 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
8635 if (win
->floating
) {
8636 if (e
->data
.data32
[0] & (1<<8)) /* x */
8637 X(win
) = e
->data
.data32
[1];
8638 if (e
->data
.data32
[0] & (1<<9)) /* y */
8639 Y(win
) = e
->data
.data32
[2];
8640 if (e
->data
.data32
[0] & (1<<10)) /* width */
8641 WIDTH(win
) = e
->data
.data32
[3];
8642 if (e
->data
.data32
[0] & (1<<11)) /* height */
8643 HEIGHT(win
) = e
->data
.data32
[4];
8648 /* TODO: Change stack sizes */
8649 /* notify no change was made. */
8650 config_win(win
, NULL
);
8653 if (e
->type
== ewmh
[_NET_WM_STATE
].atom
) {
8654 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_WM_STATE\n");
8655 ewmh_update_win_state(win
, e
->data
.data32
[1], e
->data
.data32
[0]);
8656 if (e
->data
.data32
[2])
8657 ewmh_update_win_state(win
, e
->data
.data32
[2],
8669 int errcode
= xcb_connection_has_error(conn
);
8670 #ifdef XCB_CONN_ERROR
8673 case XCB_CONN_ERROR
:
8674 s
= "Socket error, pipe error or other stream error.";
8676 case XCB_CONN_CLOSED_EXT_NOTSUPPORTED
:
8677 s
= "Extension not supported.";
8679 case XCB_CONN_CLOSED_MEM_INSUFFICIENT
:
8680 s
= "Insufficient memory.";
8682 case XCB_CONN_CLOSED_REQ_LEN_EXCEED
:
8683 s
= "Request length was exceeded.";
8685 case XCB_CONN_CLOSED_PARSE_ERR
:
8686 s
= "Error parsing display string.";
8689 s
= "Unknown error.";
8692 errx(errcode
, "X CONNECTION ERROR: %s", s
);
8695 errx(errcode
, "X CONNECTION ERROR");
8703 const uint32_t val
= XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT
|
8704 XCB_EVENT_MASK_ENTER_WINDOW
;
8706 xcb_void_cookie_t wac
;
8707 xcb_generic_error_t
*error
;
8709 /* this causes an error if some other window manager is running */
8710 num_screens
= get_screen_count();
8711 for (i
= 0; i
< num_screens
; i
++) {
8712 if ((sc
= get_screen(i
)) == NULL
)
8713 errx(1, "ERROR: can't get screen %d.", i
);
8714 DNPRINTF(SWM_D_INIT
, "enable_wm: screen %d, root: 0x%x\n",
8716 wac
= xcb_change_window_attributes_checked(conn
, sc
->root
,
8717 XCB_CW_EVENT_MASK
, &val
);
8718 if ((error
= xcb_request_check(conn
, wac
))) {
8719 DNPRINTF(SWM_D_INIT
, "enable_wm: error_code: %u\n",
8725 /* click to focus on empty region */
8726 xcb_grab_button(conn
, 1, sc
->root
, BUTTONMASK
,
8727 XCB_GRAB_MODE_SYNC
, XCB_GRAB_MODE_ASYNC
, XCB_WINDOW_NONE
,
8728 XCB_CURSOR_NONE
, XCB_BUTTON_INDEX_1
, XCB_BUTTON_MASK_ANY
);
8735 new_region(struct swm_screen
*s
, int x
, int y
, int w
, int h
)
8737 struct swm_region
*r
, *n
;
8738 struct workspace
*ws
= NULL
;
8742 DNPRINTF(SWM_D_MISC
, "new region: screen[%d]:%dx%d+%d+%d\n",
8743 s
->idx
, w
, h
, x
, y
);
8745 /* remove any conflicting regions */
8746 n
= TAILQ_FIRST(&s
->rl
);
8749 n
= TAILQ_NEXT(r
, entry
);
8750 if (X(r
) < (x
+ w
) && (X(r
) + WIDTH(r
)) > x
&&
8751 Y(r
) < (y
+ h
) && (Y(r
) + HEIGHT(r
)) > y
) {
8752 if (r
->ws
->r
!= NULL
)
8753 r
->ws
->old_r
= r
->ws
->r
;
8756 xcb_destroy_window(conn
, r
->id
);
8757 TAILQ_REMOVE(&s
->rl
, r
, entry
);
8758 TAILQ_INSERT_TAIL(&s
->orl
, r
, entry
);
8762 /* search old regions for one to reuse */
8764 /* size + location match */
8765 TAILQ_FOREACH(r
, &s
->orl
, entry
)
8766 if (X(r
) == x
&& Y(r
) == y
&&
8767 HEIGHT(r
) == h
&& WIDTH(r
) == w
)
8771 TAILQ_FOREACH(r
, &s
->orl
, entry
)
8772 if (HEIGHT(r
) == h
&& WIDTH(r
) == w
)
8776 TAILQ_REMOVE(&s
->orl
, r
, entry
);
8777 /* try to use old region's workspace */
8778 if (r
->ws
->r
== NULL
)
8781 if ((r
= calloc(1, sizeof(struct swm_region
))) == NULL
)
8782 err(1, "new_region: calloc: failed to allocate memory "
8785 /* if we don't have a workspace already, find one */
8787 for (i
= 0; i
< workspace_limit
; i
++)
8788 if (s
->ws
[i
].r
== NULL
) {
8795 errx(1, "new_region: no free workspaces");
8806 TAILQ_INSERT_TAIL(&s
->rl
, r
, entry
);
8808 /* Invisible region window to detect pointer events on empty regions. */
8809 r
->id
= xcb_generate_id(conn
);
8810 wa
[0] = XCB_EVENT_MASK_POINTER_MOTION
|
8811 XCB_EVENT_MASK_POINTER_MOTION_HINT
;
8813 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, r
->id
, r
->s
->root
,
8814 X(r
), Y(r
), WIDTH(r
), HEIGHT(r
), 0, XCB_WINDOW_CLASS_INPUT_ONLY
,
8815 XCB_COPY_FROM_PARENT
, XCB_CW_EVENT_MASK
, wa
);
8817 xcb_map_window(conn
, r
->id
);
8823 #ifdef SWM_XRR_HAS_CRTC
8826 #endif /* SWM_XRR_HAS_CRTC */
8827 struct swm_region
*r
;
8830 xcb_randr_get_screen_resources_current_cookie_t src
;
8831 xcb_randr_get_screen_resources_current_reply_t
*srr
;
8832 xcb_randr_get_crtc_info_cookie_t cic
;
8833 xcb_randr_get_crtc_info_reply_t
*cir
= NULL
;
8834 xcb_randr_crtc_t
*crtc
;
8835 xcb_screen_t
*screen
;
8837 DNPRINTF(SWM_D_MISC
, "scan_xrandr: screen: %d\n", i
);
8839 if ((screen
= get_screen(i
)) == NULL
)
8840 errx(1, "ERROR: can't get screen %d.", i
);
8842 num_screens
= get_screen_count();
8843 if (i
>= num_screens
)
8844 errx(1, "scan_xrandr: invalid screen");
8846 /* remove any old regions */
8847 while ((r
= TAILQ_FIRST(&screens
[i
].rl
)) != NULL
) {
8848 r
->ws
->old_r
= r
->ws
->r
= NULL
;
8850 xcb_destroy_window(conn
, r
->id
);
8851 TAILQ_REMOVE(&screens
[i
].rl
, r
, entry
);
8852 TAILQ_INSERT_TAIL(&screens
[i
].orl
, r
, entry
);
8856 /* map virtual screens onto physical screens */
8857 #ifdef SWM_XRR_HAS_CRTC
8858 if (xrandr_support
) {
8859 src
= xcb_randr_get_screen_resources_current(conn
,
8861 srr
= xcb_randr_get_screen_resources_current_reply(conn
, src
,
8864 new_region(&screens
[i
], 0, 0,
8865 screen
->width_in_pixels
,
8866 screen
->height_in_pixels
);
8869 ncrtc
= srr
->num_crtcs
;
8871 crtc
= xcb_randr_get_screen_resources_current_crtcs(srr
);
8872 for (c
= 0; c
< ncrtc
; c
++) {
8873 cic
= xcb_randr_get_crtc_info(conn
, crtc
[c
],
8875 cir
= xcb_randr_get_crtc_info_reply(conn
, cic
, NULL
);
8878 if (cir
->num_outputs
== 0) {
8884 new_region(&screens
[i
], 0, 0,
8885 screen
->width_in_pixels
,
8886 screen
->height_in_pixels
);
8888 new_region(&screens
[i
],
8889 cir
->x
, cir
->y
, cir
->width
, cir
->height
);
8894 #endif /* SWM_XRR_HAS_CRTC */
8896 /* If detection failed, create a single region that spans the screen. */
8897 if (TAILQ_EMPTY(&screens
[i
].rl
))
8898 new_region(&screens
[i
], 0, 0, screen
->width_in_pixels
,
8899 screen
->height_in_pixels
);
8902 /* Cleanup unused previously visible workspaces. */
8903 TAILQ_FOREACH(r
, &screens
[i
].orl
, entry
) {
8904 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
)
8907 /* The screen shouldn't focus on an unused region. */
8908 if (screens
[i
].r_focus
== r
)
8909 screens
[i
].r_focus
= NULL
;
8912 DNPRINTF(SWM_D_MISC
, "scan_xrandr: done.\n");
8916 screenchange(xcb_randr_screen_change_notify_event_t
*e
)
8918 struct swm_region
*r
;
8921 DNPRINTF(SWM_D_EVENT
, "screenchange: root: 0x%x\n", e
->root
);
8923 num_screens
= get_screen_count();
8924 /* silly event doesn't include the screen index */
8925 for (i
= 0; i
< num_screens
; i
++)
8926 if (screens
[i
].root
== e
->root
)
8928 if (i
>= num_screens
)
8929 errx(1, "screenchange: screen not found");
8931 /* brute force for now, just re-enumerate the regions */
8935 print_win_geom(e
->root
);
8937 /* add bars to all regions */
8938 for (i
= 0; i
< num_screens
; i
++) {
8939 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
8945 /* Make sure a region has focus on each screen. */
8946 for (i
= 0; i
< num_screens
; i
++) {
8947 if (screens
[i
].r_focus
== NULL
) {
8948 r
= TAILQ_FIRST(&screens
[i
].rl
);
8961 struct swm_region
*r
= NULL
;
8962 xcb_window_t
*wins
= NULL
, trans
;
8964 int i
, j
, num_screens
;
8965 uint16_t state
, manage
, mapped
;
8967 xcb_query_tree_cookie_t qtc
;
8968 xcb_query_tree_reply_t
*qtr
;
8969 xcb_get_window_attributes_cookie_t gac
;
8970 xcb_get_window_attributes_reply_t
*gar
;
8971 xcb_get_property_cookie_t pc
;
8973 DNPRINTF(SWM_D_INIT
, "grab_windows: begin\n");
8974 num_screens
= get_screen_count();
8975 for (i
= 0; i
< num_screens
; i
++) {
8976 qtc
= xcb_query_tree(conn
, screens
[i
].root
);
8977 qtr
= xcb_query_tree_reply(conn
, qtc
, NULL
);
8980 wins
= xcb_query_tree_children(qtr
);
8981 no
= xcb_query_tree_children_length(qtr
);
8982 /* attach windows to a region */
8983 /* normal windows */
8984 DNPRINTF(SWM_D_INIT
, "grab_windows: grab top level windows.\n");
8985 for (j
= 0; j
< no
; j
++) {
8986 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
8987 if (r
->id
== wins
[j
]) {
8988 DNPRINTF(SWM_D_INIT
, "grab_windows: "
8989 "skip %#x; region input window.\n",
8998 gac
= xcb_get_window_attributes(conn
, wins
[j
]);
8999 gar
= xcb_get_window_attributes_reply(conn
, gac
, NULL
);
9001 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
9002 "doesn't exist.\n", wins
[j
]);
9006 if (gar
->override_redirect
) {
9007 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
9008 "override_redirect set.\n", wins
[j
]);
9013 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
9014 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
9016 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
9017 "is transient for %#x.\n", wins
[j
], trans
);
9022 state
= getstate(wins
[j
]);
9023 manage
= state
!= XCB_ICCCM_WM_STATE_WITHDRAWN
;
9024 mapped
= gar
->map_state
!= XCB_MAP_STATE_UNMAPPED
;
9025 if (mapped
|| manage
)
9026 manage_window(wins
[j
], mapped
);
9029 /* transient windows */
9030 DNPRINTF(SWM_D_INIT
, "grab_windows: grab transient windows.\n");
9031 for (j
= 0; j
< no
; j
++) {
9032 gac
= xcb_get_window_attributes(conn
, wins
[j
]);
9033 gar
= xcb_get_window_attributes_reply(conn
, gac
, NULL
);
9035 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
9036 "doesn't exist.\n", wins
[j
]);
9040 if (gar
->override_redirect
) {
9041 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
9042 "override_redirect set.\n", wins
[j
]);
9047 state
= getstate(wins
[j
]);
9048 manage
= state
!= XCB_ICCCM_WM_STATE_WITHDRAWN
;
9049 mapped
= gar
->map_state
!= XCB_MAP_STATE_UNMAPPED
;
9050 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
9051 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
9052 &trans
, NULL
) && manage
)
9053 manage_window(wins
[j
], mapped
);
9058 DNPRINTF(SWM_D_INIT
, "grab_windows: done.\n");
9064 int i
, j
, k
, num_screens
;
9065 struct workspace
*ws
;
9066 uint32_t gcv
[1], wa
[1];
9067 const xcb_query_extension_reply_t
*qep
;
9068 xcb_screen_t
*screen
;
9069 xcb_randr_query_version_cookie_t c
;
9070 xcb_randr_query_version_reply_t
*r
;
9072 num_screens
= get_screen_count();
9073 if ((screens
= calloc(num_screens
,
9074 sizeof(struct swm_screen
))) == NULL
)
9075 err(1, "setup_screens: calloc: failed to allocate memory for "
9078 /* initial Xrandr setup */
9080 qep
= xcb_get_extension_data(conn
, &xcb_randr_id
);
9082 c
= xcb_randr_query_version(conn
, 1, 1);
9083 r
= xcb_randr_query_version_reply(conn
, c
, NULL
);
9085 if (r
->major_version
>= 1) {
9087 xrandr_eventbase
= qep
->first_event
;
9093 wa
[0] = cursors
[XC_LEFT_PTR
].cid
;
9095 /* map physical screens */
9096 for (i
= 0; i
< num_screens
; i
++) {
9097 DNPRINTF(SWM_D_WS
, "setup_screens: init screen: %d\n", i
);
9099 screens
[i
].r_focus
= NULL
;
9101 TAILQ_INIT(&screens
[i
].rl
);
9102 TAILQ_INIT(&screens
[i
].orl
);
9103 if ((screen
= get_screen(i
)) == NULL
)
9104 errx(1, "ERROR: can't get screen %d.", i
);
9105 screens
[i
].root
= screen
->root
;
9107 /* set default colors */
9108 setscreencolor("red", i
+ 1, SWM_S_COLOR_FOCUS
);
9109 setscreencolor("rgb:88/88/88", i
+ 1, SWM_S_COLOR_UNFOCUS
);
9110 setscreencolor("rgb:00/80/80", i
+ 1, SWM_S_COLOR_BAR_BORDER
);
9111 setscreencolor("rgb:00/40/40", i
+ 1,
9112 SWM_S_COLOR_BAR_BORDER_UNFOCUS
);
9113 setscreencolor("black", i
+ 1, SWM_S_COLOR_BAR
);
9114 setscreencolor("rgb:a0/a0/a0", i
+ 1, SWM_S_COLOR_BAR_FONT
);
9116 /* create graphics context on screen */
9117 screens
[i
].bar_gc
= xcb_generate_id(conn
);
9119 xcb_create_gc(conn
, screens
[i
].bar_gc
, screens
[i
].root
,
9120 XCB_GC_GRAPHICS_EXPOSURES
, gcv
);
9122 /* set default cursor */
9123 xcb_change_window_attributes(conn
, screens
[i
].root
,
9126 /* init all workspaces */
9127 /* XXX these should be dynamically allocated too */
9128 for (j
= 0; j
< SWM_WS_MAX
; j
++) {
9129 ws
= &screens
[i
].ws
[j
];
9132 ws
->bar_enabled
= 1;
9134 ws
->focus_prev
= NULL
;
9135 ws
->focus_pending
= NULL
;
9138 TAILQ_INIT(&ws
->winlist
);
9139 TAILQ_INIT(&ws
->unmanagedlist
);
9141 for (k
= 0; layouts
[k
].l_stack
!= NULL
; k
++)
9142 if (layouts
[k
].l_config
!= NULL
)
9143 layouts
[k
].l_config(ws
,
9144 SWM_ARG_ID_STACKINIT
);
9145 ws
->cur_layout
= &layouts
[0];
9146 ws
->cur_layout
->l_string(ws
);
9152 xcb_randr_select_input(conn
, screens
[i
].root
,
9153 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE
);
9160 if ((bar_fonts
= strdup(SWM_BAR_FONTS
)) == NULL
)
9161 err(1, "setup_globals: strdup: failed to allocate memory.");
9163 if ((clock_format
= strdup("%a %b %d %R %Z %Y")) == NULL
)
9164 err(1, "setup_globals: strdup: failed to allocate memory.");
9166 if ((syms
= xcb_key_symbols_alloc(conn
)) == NULL
)
9167 errx(1, "unable to allocate key symbols");
9169 a_state
= get_atom_from_string("WM_STATE");
9170 a_prot
= get_atom_from_string("WM_PROTOCOLS");
9171 a_delete
= get_atom_from_string("WM_DELETE_WINDOW");
9172 a_takefocus
= get_atom_from_string("WM_TAKE_FOCUS");
9173 a_wmname
= get_atom_from_string("WM_NAME");
9174 a_netwmname
= get_atom_from_string("_NET_WM_NAME");
9175 a_utf8_string
= get_atom_from_string("UTF8_STRING");
9176 a_string
= get_atom_from_string("STRING");
9177 a_swm_iconic
= get_atom_from_string("_SWM_ICONIC");
9178 a_swm_ws
= get_atom_from_string("_SWM_WS");
9185 xcb_atom_t netwmcheck
;
9186 xcb_window_t root
, win
;
9188 /* work around sun jdk bugs, code from wmname */
9189 netwmcheck
= get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
9191 num_screens
= get_screen_count();
9192 for (i
= 0; i
< num_screens
; i
++) {
9193 root
= screens
[i
].root
;
9195 win
= xcb_generate_id(conn
);
9196 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, win
, root
,
9197 0, 0, 1, 1, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
9198 XCB_COPY_FROM_PARENT
, 0, NULL
);
9200 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, root
,
9201 netwmcheck
, XCB_ATOM_WINDOW
, 32, 1, &win
);
9202 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
9203 netwmcheck
, XCB_ATOM_WINDOW
, 32, 1, &win
);
9204 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
9205 a_netwmname
, a_utf8_string
, 8, strlen("LG3D"), "LG3D");
9210 shutdown_cleanup(void)
9214 /* disable alarm because the following code may not be interrupted */
9216 if (signal(SIGALRM
, SIG_IGN
) == SIG_ERR
)
9217 err(1, "can't disable alarm");
9226 num_screens
= get_screen_count();
9227 for (i
= 0; i
< num_screens
; ++i
) {
9228 if (screens
[i
].bar_gc
!= 0)
9229 xcb_free_gc(conn
, screens
[i
].bar_gc
);
9230 if (!bar_font_legacy
)
9231 XftColorFree(display
, DefaultVisual(display
, i
),
9232 DefaultColormap(display
, i
), &bar_font_color
);
9235 if (bar_font_legacy
)
9236 XFreeFontSet(display
, bar_fs
);
9238 XftFontClose(display
, bar_font
);
9241 xcb_key_symbols_free(syms
);
9243 xcb_disconnect(conn
);
9247 event_error(xcb_generic_error_t
*e
)
9251 DNPRINTF(SWM_D_EVENT
, "event_error: %s(%u) from %s(%u), sequence: %u, "
9252 "resource_id: %u, minor_code: %u\n",
9253 xcb_event_get_error_label(e
->error_code
), e
->error_code
,
9254 xcb_event_get_request_label(e
->major_code
), e
->major_code
,
9255 e
->sequence
, e
->resource_id
, e
->minor_code
);
9259 event_handle(xcb_generic_event_t
*evt
)
9261 uint8_t type
= XCB_EVENT_RESPONSE_TYPE(evt
);
9263 DNPRINTF(SWM_D_EVENT
, "XCB Event: %s(%d), seq %u\n",
9264 xcb_event_get_label(XCB_EVENT_RESPONSE_TYPE(evt
)),
9265 XCB_EVENT_RESPONSE_TYPE(evt
), evt
->sequence
);
9268 #define EVENT(type, callback) case type: callback((void *)evt); return
9269 EVENT(0, event_error
);
9270 EVENT(XCB_BUTTON_PRESS
, buttonpress
);
9271 /*EVENT(XCB_BUTTON_RELEASE, buttonpress);*/
9272 /*EVENT(XCB_CIRCULATE_NOTIFY, );*/
9273 /*EVENT(XCB_CIRCULATE_REQUEST, );*/
9274 EVENT(XCB_CLIENT_MESSAGE
, clientmessage
);
9275 /*EVENT(XCB_COLORMAP_NOTIFY, );*/
9276 EVENT(XCB_CONFIGURE_NOTIFY
, configurenotify
);
9277 EVENT(XCB_CONFIGURE_REQUEST
, configurerequest
);
9278 /*EVENT(XCB_CREATE_NOTIFY, );*/
9279 EVENT(XCB_DESTROY_NOTIFY
, destroynotify
);
9280 EVENT(XCB_ENTER_NOTIFY
, enternotify
);
9281 EVENT(XCB_EXPOSE
, expose
);
9283 EVENT(XCB_FOCUS_IN
, focusin
);
9284 EVENT(XCB_FOCUS_OUT
, focusout
);
9286 /*EVENT(XCB_GRAPHICS_EXPOSURE, );*/
9287 /*EVENT(XCB_GRAVITY_NOTIFY, );*/
9288 EVENT(XCB_KEY_PRESS
, keypress
);
9289 /*EVENT(XCB_KEY_RELEASE, keypress);*/
9290 /*EVENT(XCB_KEYMAP_NOTIFY, );*/
9292 EVENT(XCB_LEAVE_NOTIFY
, leavenotify
);
9294 EVENT(XCB_MAP_NOTIFY
, mapnotify
);
9295 EVENT(XCB_MAP_REQUEST
, maprequest
);
9296 EVENT(XCB_MAPPING_NOTIFY
, mappingnotify
);
9297 EVENT(XCB_MOTION_NOTIFY
, motionnotify
);
9298 /*EVENT(XCB_NO_EXPOSURE, );*/
9299 EVENT(XCB_PROPERTY_NOTIFY
, propertynotify
);
9300 /*EVENT(XCB_REPARENT_NOTIFY, );*/
9301 /*EVENT(XCB_RESIZE_REQUEST, );*/
9302 /*EVENT(XCB_SELECTION_CLEAR, );*/
9303 /*EVENT(XCB_SELECTION_NOTIFY, );*/
9304 /*EVENT(XCB_SELECTION_REQUEST, );*/
9305 EVENT(XCB_UNMAP_NOTIFY
, unmapnotify
);
9306 /*EVENT(XCB_VISIBILITY_NOTIFY, visibilitynotify);*/
9309 if (type
- xrandr_eventbase
== XCB_RANDR_SCREEN_CHANGE_NOTIFY
)
9310 screenchange((void *)evt
);
9314 main(int argc
, char *argv
[])
9316 struct swm_region
*r
;
9317 char conf
[PATH_MAX
], *cfile
= NULL
;
9319 int xfd
, i
, num_screens
, startup
= 1;
9320 struct sigaction sact
;
9321 xcb_generic_event_t
*evt
;
9325 int stdin_ready
= 0;
9328 /* suppress unused warning since var is needed */
9331 time_started
= time(NULL
);
9334 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION
, buildstr
);
9335 if (!setlocale(LC_CTYPE
, "") || !setlocale(LC_TIME
, ""))
9336 warnx("no locale support");
9338 /* handle some signals */
9339 bzero(&sact
, sizeof(sact
));
9340 sigemptyset(&sact
.sa_mask
);
9342 sact
.sa_handler
= sighdlr
;
9343 sigaction(SIGINT
, &sact
, NULL
);
9344 sigaction(SIGQUIT
, &sact
, NULL
);
9345 sigaction(SIGTERM
, &sact
, NULL
);
9346 sigaction(SIGHUP
, &sact
, NULL
);
9348 sact
.sa_handler
= sighdlr
;
9349 sact
.sa_flags
= SA_NOCLDSTOP
;
9350 sigaction(SIGCHLD
, &sact
, NULL
);
9352 if (!(display
= XOpenDisplay(0)))
9353 errx(1, "can not open display");
9355 conn
= XGetXCBConnection(display
);
9356 if (xcb_connection_has_error(conn
))
9357 errx(1, "can not get XCB connection");
9359 XSetEventQueueOwner(display
, XCBOwnsEventQueue
);
9361 xcb_prefetch_extension_data(conn
, &xcb_randr_id
);
9362 xfd
= xcb_get_file_descriptor(conn
);
9364 /* look for local and global conf file */
9365 pwd
= getpwuid(getuid());
9367 errx(1, "invalid user: %d", getuid());
9369 xcb_grab_server(conn
);
9372 /* flush all events */
9373 while ((evt
= xcb_poll_for_event(conn
))) {
9374 if (XCB_EVENT_RESPONSE_TYPE(evt
) == 0)
9379 if (enable_wm() != 0)
9380 errx(1, "another window manager is currently running");
9382 /* Load Xcursors and/or cursorfont glyph cursors. */
9394 for (i
= 0; ; i
++) {
9399 snprintf(conf
, sizeof conf
, "%s/.%s",
9400 pwd
->pw_dir
, SWM_CONF_FILE
);
9404 snprintf(conf
, sizeof conf
, "/etc/%s",
9409 snprintf(conf
, sizeof conf
, "%s/.%s",
9410 pwd
->pw_dir
, SWM_CONF_FILE_OLD
);
9414 snprintf(conf
, sizeof conf
, "/etc/%s",
9421 if (strlen(conf
) && stat(conf
, &sb
) != -1)
9422 if (S_ISREG(sb
.st_mode
)) {
9429 /* load conf (if any) */
9431 conf_load(cfile
, SWM_CONF_DEFAULT
);
9436 /* set some values to work around bad programs */
9438 /* grab existing windows (before we build the bars) */
9441 if (getenv("SWM_STARTED") == NULL
)
9442 setenv("SWM_STARTED", "YES", 1);
9444 /* setup all bars */
9445 num_screens
= get_screen_count();
9446 for (i
= 0; i
< num_screens
; i
++)
9447 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
9454 xcb_ungrab_server(conn
);
9457 rd_max
= xfd
> STDIN_FILENO
? xfd
: STDIN_FILENO
;
9460 while ((evt
= xcb_poll_for_event(conn
))) {
9467 /* If just (re)started, set default focus if needed. */
9471 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
9472 r
= TAILQ_FIRST(&screens
[0].rl
);
9484 FD_SET(STDIN_FILENO
, &rd
);
9489 num_readable
= select(rd_max
+ 1, &rd
, NULL
, NULL
, &tv
);
9490 if (num_readable
== -1 && errno
!= EINTR
) {
9491 DNPRINTF(SWM_D_MISC
, "select failed");
9492 } else if (num_readable
> 0 && FD_ISSET(STDIN_FILENO
, &rd
)) {
9497 restart(NULL
, NULL
);