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 0x7
255 #define SWM_CK_FOCUS 0x1
256 #define SWM_CK_POINTER 0x2
257 #define SWM_CK_FALLBACK 0x4
259 #define SWM_CONF_DEFAULT (0)
260 #define SWM_CONF_KEYMAPPING (1)
263 #define SWM_LIB "/usr/local/lib/libswmhack.so"
270 xcb_atom_t a_takefocus
;
272 xcb_atom_t a_netwmname
;
273 xcb_atom_t a_utf8_string
;
275 xcb_atom_t a_swm_iconic
;
277 volatile sig_atomic_t running
= 1;
278 volatile sig_atomic_t restart_wm
= 0;
279 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 unsigned char bar_ext
[SWM_BAR_MAX
];
352 char bar_vertext
[SWM_BAR_MAX
];
354 sig_atomic_t bar_alarm
= 0;
357 int bar_border_width
= 1;
358 int bar_at_bottom
= 0;
360 int bar_extra_running
= 0;
363 int bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
364 char *bar_format
= NULL
;
365 int stack_enabled
= 1;
366 int clock_enabled
= 1;
367 int urgent_enabled
= 0;
368 char *clock_format
= NULL
;
369 int title_name_enabled
= 0;
370 int title_class_enabled
= 0;
371 int window_name_enabled
= 0;
372 int focus_mode
= SWM_FOCUS_DEFAULT
;
373 int focus_close
= SWM_STACK_BELOW
;
374 int focus_close_wrap
= 1;
375 int focus_default
= SWM_STACK_TOP
;
376 int spawn_position
= SWM_STACK_TOP
;
377 int disable_border
= 0;
378 int border_width
= 1;
379 int verbose_layout
= 0;
383 XFontSetExtents
*bar_fs_extents
;
385 int bar_font_legacy
= 1;
387 XftColor bar_font_color
;
390 /* layout manager data */
391 struct swm_geometry
{
404 struct swm_geometry g
;
407 /* virtual "screens" */
409 TAILQ_ENTRY(swm_region
) entry
;
410 struct swm_geometry g
;
411 struct workspace
*ws
; /* current workspace on this region */
412 struct workspace
*ws_prior
; /* prior workspace on this region */
413 struct swm_screen
*s
; /* screen idx */
416 TAILQ_HEAD(swm_region_list
, swm_region
);
419 TAILQ_ENTRY(ws_win
) entry
;
421 xcb_window_t transient
;
422 struct ws_win
*focus_child
; /* focus on child transient */
423 struct swm_geometry g
; /* current geometry */
424 struct swm_geometry g_float
; /* region coordinates */
425 int g_floatvalid
; /* g_float geometry validity */
426 int floatmaxed
; /* whether maxed by max_stack */
432 unsigned int ewmh_flags
;
433 int font_size_boundary
[SWM_MAX_FONT_STEPS
];
439 unsigned long quirks
;
440 struct workspace
*ws
; /* always valid */
441 struct swm_screen
*s
; /* always valid, never changes */
442 xcb_get_geometry_reply_t
*wa
;
444 xcb_icccm_get_wm_class_reply_t ch
;
445 xcb_icccm_wm_hints_t hints
;
447 TAILQ_HEAD(ws_win_list
, ws_win
);
451 TAILQ_ENTRY(pid_e
) entry
;
455 TAILQ_HEAD(pid_list
, pid_e
);
456 struct pid_list pidlist
= TAILQ_HEAD_INITIALIZER(pidlist
);
458 /* layout handlers */
460 void vertical_config(struct workspace
*, int);
461 void vertical_stack(struct workspace
*, struct swm_geometry
*);
462 void horizontal_config(struct workspace
*, int);
463 void horizontal_stack(struct workspace
*, struct swm_geometry
*);
464 void max_stack(struct workspace
*, struct swm_geometry
*);
465 void plain_stacker(struct workspace
*);
466 void fancy_stacker(struct workspace
*);
469 void (*l_stack
)(struct workspace
*, struct swm_geometry
*);
470 void (*l_config
)(struct workspace
*, int);
472 #define SWM_L_FOCUSPREV (1<<0)
473 #define SWM_L_MAPONFOCUS (1<<1)
474 void (*l_string
)(struct workspace
*);
476 /* stack, configure */
477 { vertical_stack
, vertical_config
, 0, plain_stacker
},
478 { horizontal_stack
, horizontal_config
, 0, plain_stacker
},
480 SWM_L_MAPONFOCUS
| SWM_L_FOCUSPREV
, plain_stacker
},
481 { NULL
, NULL
, 0, NULL
},
484 /* position of max_stack mode in the layouts array, index into layouts! */
485 #define SWM_V_STACK (0)
486 #define SWM_H_STACK (1)
487 #define SWM_MAX_STACK (2)
489 #define SWM_H_SLICE (32)
490 #define SWM_V_SLICE (32)
492 /* define work spaces */
494 int idx
; /* workspace index */
495 char *name
; /* workspace name */
496 int always_raise
; /* raise windows on focus */
497 struct layout
*cur_layout
; /* current layout handlers */
498 struct ws_win
*focus
; /* may be NULL */
499 struct ws_win
*focus_prev
; /* may be NULL */
500 struct ws_win
*focus_pending
; /* may be NULL */
501 struct swm_region
*r
; /* may be NULL */
502 struct swm_region
*old_r
; /* may be NULL */
503 struct ws_win_list winlist
; /* list of windows in ws */
504 struct ws_win_list unmanagedlist
; /* list of dead windows in ws */
505 char stacker
[10]; /* display stacker and layout */
509 int horizontal_msize
;
511 int horizontal_stacks
;
522 SWM_S_COLOR_BAR_BORDER
,
523 SWM_S_COLOR_BAR_FONT
,
529 /* physical screen mapping */
530 #define SWM_WS_MAX (22) /* hard limit */
531 int workspace_limit
= 10; /* soft limit */
534 int idx
; /* screen index */
535 struct swm_region_list rl
; /* list of regions on this screen */
536 struct swm_region_list orl
; /* list of old regions */
538 struct workspace ws
[SWM_WS_MAX
];
544 } c
[SWM_S_COLOR_MAX
];
546 xcb_gcontext_t bar_gc
;
549 struct swm_screen
*screens
;
551 /* args to functions */
554 #define SWM_ARG_ID_FOCUSNEXT (0)
555 #define SWM_ARG_ID_FOCUSPREV (1)
556 #define SWM_ARG_ID_FOCUSMAIN (2)
557 #define SWM_ARG_ID_SWAPNEXT (10)
558 #define SWM_ARG_ID_SWAPPREV (11)
559 #define SWM_ARG_ID_SWAPMAIN (12)
560 #define SWM_ARG_ID_MOVELAST (13)
561 #define SWM_ARG_ID_MASTERSHRINK (20)
562 #define SWM_ARG_ID_MASTERGROW (21)
563 #define SWM_ARG_ID_MASTERADD (22)
564 #define SWM_ARG_ID_MASTERDEL (23)
565 #define SWM_ARG_ID_FLIPLAYOUT (24)
566 #define SWM_ARG_ID_STACKRESET (30)
567 #define SWM_ARG_ID_STACKINIT (31)
568 #define SWM_ARG_ID_CYCLEWS_UP (40)
569 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
570 #define SWM_ARG_ID_CYCLESC_UP (42)
571 #define SWM_ARG_ID_CYCLESC_DOWN (43)
572 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
573 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
574 #define SWM_ARG_ID_STACKINC (50)
575 #define SWM_ARG_ID_STACKDEC (51)
576 #define SWM_ARG_ID_SS_ALL (60)
577 #define SWM_ARG_ID_SS_WINDOW (61)
578 #define SWM_ARG_ID_DONTCENTER (70)
579 #define SWM_ARG_ID_CENTER (71)
580 #define SWM_ARG_ID_KILLWINDOW (80)
581 #define SWM_ARG_ID_DELETEWINDOW (81)
582 #define SWM_ARG_ID_WIDTHGROW (90)
583 #define SWM_ARG_ID_WIDTHSHRINK (91)
584 #define SWM_ARG_ID_HEIGHTGROW (92)
585 #define SWM_ARG_ID_HEIGHTSHRINK (93)
586 #define SWM_ARG_ID_MOVEUP (100)
587 #define SWM_ARG_ID_MOVEDOWN (101)
588 #define SWM_ARG_ID_MOVELEFT (102)
589 #define SWM_ARG_ID_MOVERIGHT (103)
595 TAILQ_ENTRY(quirk
) entry
;
599 #define SWM_Q_FLOAT (1<<0) /* float this window */
600 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
601 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
602 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
603 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
604 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
606 TAILQ_HEAD(quirk_list
, quirk
);
607 struct quirk_list quirks
= TAILQ_HEAD_INITIALIZER(quirks
);
610 * Supported EWMH hints should be added to
611 * both the enum and the ewmh array
616 _NET_MOVERESIZE_WINDOW
,
617 _NET_WM_ACTION_ABOVE
,
618 _NET_WM_ACTION_CLOSE
,
619 _NET_WM_ACTION_FULLSCREEN
,
621 _NET_WM_ACTION_RESIZE
,
622 _NET_WM_ALLOWED_ACTIONS
,
626 _NET_WM_STATE_FULLSCREEN
,
627 _NET_WM_STATE_HIDDEN
,
628 _NET_WM_STATE_MAXIMIZED_HORZ
,
629 _NET_WM_STATE_MAXIMIZED_VERT
,
630 _NET_WM_STATE_SKIP_PAGER
,
631 _NET_WM_STATE_SKIP_TASKBAR
,
633 _NET_WM_WINDOW_TYPE_DIALOG
,
634 _NET_WM_WINDOW_TYPE_DOCK
,
635 _NET_WM_WINDOW_TYPE_NORMAL
,
636 _NET_WM_WINDOW_TYPE_SPLASH
,
637 _NET_WM_WINDOW_TYPE_TOOLBAR
,
638 _NET_WM_WINDOW_TYPE_UTILITY
,
639 _SWM_WM_STATE_MANUAL
,
646 } ewmh
[SWM_EWMH_HINT_MAX
] = {
647 /* must be in same order as in the enum */
648 {"_NET_ACTIVE_WINDOW", XCB_ATOM_NONE
},
649 {"_NET_CLOSE_WINDOW", XCB_ATOM_NONE
},
650 {"_NET_MOVERESIZE_WINDOW", XCB_ATOM_NONE
},
651 {"_NET_WM_ACTION_ABOVE", XCB_ATOM_NONE
},
652 {"_NET_WM_ACTION_CLOSE", XCB_ATOM_NONE
},
653 {"_NET_WM_ACTION_FULLSCREEN", XCB_ATOM_NONE
},
654 {"_NET_WM_ACTION_MOVE", XCB_ATOM_NONE
},
655 {"_NET_WM_ACTION_RESIZE", XCB_ATOM_NONE
},
656 {"_NET_WM_ALLOWED_ACTIONS", XCB_ATOM_NONE
},
657 {"_NET_WM_NAME", XCB_ATOM_NONE
},
658 {"_NET_WM_STATE", XCB_ATOM_NONE
},
659 {"_NET_WM_STATE_ABOVE", XCB_ATOM_NONE
},
660 {"_NET_WM_STATE_FULLSCREEN", XCB_ATOM_NONE
},
661 {"_NET_WM_STATE_HIDDEN", XCB_ATOM_NONE
},
662 {"_NET_WM_STATE_MAXIMIZED_HORZ", XCB_ATOM_NONE
},
663 {"_NET_WM_STATE_MAXIMIZED_VERT", XCB_ATOM_NONE
},
664 {"_NET_WM_STATE_SKIP_PAGER", XCB_ATOM_NONE
},
665 {"_NET_WM_STATE_SKIP_TASKBAR", XCB_ATOM_NONE
},
666 {"_NET_WM_WINDOW_TYPE", XCB_ATOM_NONE
},
667 {"_NET_WM_WINDOW_TYPE_DIALOG", XCB_ATOM_NONE
},
668 {"_NET_WM_WINDOW_TYPE_DOCK", XCB_ATOM_NONE
},
669 {"_NET_WM_WINDOW_TYPE_NORMAL", XCB_ATOM_NONE
},
670 {"_NET_WM_WINDOW_TYPE_SPLASH", XCB_ATOM_NONE
},
671 {"_NET_WM_WINDOW_TYPE_TOOLBAR", XCB_ATOM_NONE
},
672 {"_NET_WM_WINDOW_TYPE_UTILITY", XCB_ATOM_NONE
},
673 {"_SWM_WM_STATE_MANUAL", XCB_ATOM_NONE
},
680 XC_BOTTOM_LEFT_CORNER
,
681 XC_BOTTOM_RIGHT_CORNER
,
689 char *name
; /* Name used by Xcursor .*/
690 uint8_t cf_char
; /* cursorfont index. */
692 } cursors
[XC_MAX
] = {
693 {"fleur", XC_fleur
, XCB_CURSOR_NONE
},
694 {"left_ptr", XC_left_ptr
, XCB_CURSOR_NONE
},
695 {"bottom_left_corner", XC_bottom_left_corner
, XCB_CURSOR_NONE
},
696 {"bottom_right_corner", XC_bottom_right_corner
, XCB_CURSOR_NONE
},
697 {"sizing", XC_sizing
, XCB_CURSOR_NONE
},
698 {"top_left_corner", XC_top_left_corner
, XCB_CURSOR_NONE
},
699 {"top_right_corner", XC_top_right_corner
, XCB_CURSOR_NONE
},
704 TAILQ_ENTRY(spawn_prog
) entry
;
709 TAILQ_HEAD(spawn_list
, spawn_prog
);
710 struct spawn_list spawns
= TAILQ_HEAD_INITIALIZER(spawns
);
712 /* function prototypes */
713 void adjust_font(struct ws_win
*);
714 void bar_class_name(char *, size_t, struct swm_region
*);
715 void bar_class_title_name(char *, size_t, struct swm_region
*);
716 void bar_cleanup(struct swm_region
*);
717 void bar_extra_stop(void);
718 void bar_fmt(const char *, char *, struct swm_region
*, size_t);
719 void bar_fmt_expand(char *, size_t);
720 void bar_fmt_print(void);
721 void bar_print(struct swm_region
*, const char *);
722 void bar_print_legacy(struct swm_region
*, const char *);
723 void bar_refresh(void);
724 void bar_replace(char *, char *, struct swm_region
*, size_t);
725 void bar_replace_pad(char *, int *, size_t);
726 char * bar_replace_seq(char *, char *, struct swm_region
*, size_t *,
728 void bar_setup(struct swm_region
*);
729 void bar_signal(int);
730 void bar_title_name(char *, size_t, struct swm_region
*);
731 void bar_toggle(struct swm_region
*, union arg
*);
732 void bar_update(void);
733 void bar_urgent(char *, size_t);
734 void bar_window_float(char *, size_t, struct swm_region
*);
735 void bar_window_name(char *, size_t, struct swm_region
*);
736 void bar_workspace_name(char *, size_t, struct swm_region
*);
737 void buttonpress(xcb_button_press_event_t
*);
738 void check_conn(void);
739 void clear_keys(void);
740 void clientmessage(xcb_client_message_event_t
*);
741 void client_msg(struct ws_win
*, xcb_atom_t
, xcb_timestamp_t
);
742 int conf_load(const char *, int);
743 void configurenotify(xcb_configure_notify_event_t
*);
744 void configurerequest(xcb_configure_request_event_t
*);
745 void config_win(struct ws_win
*, xcb_configure_request_event_t
*);
746 void constrain_window(struct ws_win
*, struct swm_region
*, int);
747 int count_win(struct workspace
*, int);
748 void cursors_cleanup(void);
749 void cursors_load(void);
750 void custom_region(char *);
751 void cyclescr(struct swm_region
*, union arg
*);
752 void cyclews(struct swm_region
*, union arg
*);
753 void cycle_layout(struct swm_region
*, union arg
*);
754 void destroynotify(xcb_destroy_notify_event_t
*);
755 void dumpwins(struct swm_region
*, union arg
*);
757 void enternotify(xcb_enter_notify_event_t
*);
758 void event_drain(uint8_t);
759 void event_error(xcb_generic_error_t
*);
760 void event_handle(xcb_generic_event_t
*);
761 void ewmh_autoquirk(struct ws_win
*);
762 void ewmh_get_win_state(struct ws_win
*);
763 int ewmh_set_win_fullscreen(struct ws_win
*, int);
764 void ewmh_update_actions(struct ws_win
*);
765 void ewmh_update_win_state(struct ws_win
*, xcb_atom_t
, long);
766 char *expand_tilde(const char *);
767 void expose(xcb_expose_event_t
*);
768 void fake_keypress(struct ws_win
*, xcb_keysym_t
, uint16_t);
769 struct pid_e
*find_pid(pid_t
);
770 struct ws_win
*find_unmanaged_window(xcb_window_t
);
771 struct ws_win
*find_window(xcb_window_t
);
772 void floating_toggle(struct swm_region
*, union arg
*);
773 int floating_toggle_win(struct ws_win
*);
774 void focus(struct swm_region
*, union arg
*);
776 void focusin(xcb_focus_in_event_t
*);
777 void focusout(xcb_focus_out_event_t
*);
779 void focus_flush(void);
780 void focus_win(struct ws_win
*);
781 void fontset_init(void);
782 void free_window(struct ws_win
*);
783 xcb_atom_t
get_atom_from_string(const char *);
785 char *get_atom_name(xcb_atom_t
);
787 struct ws_win
*get_focus_magic(struct ws_win
*);
788 struct ws_win
*get_focus_prev(struct ws_win
*);
790 char *get_notify_detail_label(uint8_t);
791 char *get_notify_mode_label(uint8_t);
793 struct ws_win
*get_pointer_win(xcb_window_t
);
794 struct ws_win
*get_region_focus(struct swm_region
*);
795 xcb_screen_t
*get_screen(int);
797 char *get_stack_mode_name(uint8_t);
799 int32_t get_swm_iconic(struct ws_win
*);
800 char *get_win_name(xcb_window_t
);
801 int get_ws_idx(xcb_window_t
);
802 uint32_t getstate(xcb_window_t
);
803 void grabbuttons(struct ws_win
*);
805 void grab_windows(void);
806 void iconify(struct swm_region
*, union arg
*);
808 void keypress(xcb_key_press_event_t
*);
809 void kill_refs(struct ws_win
*);
811 void leavenotify(xcb_leave_notify_event_t
*);
813 void load_float_geom(struct ws_win
*, struct swm_region
*);
814 struct ws_win
*manage_window(xcb_window_t
, uint16_t);
815 void map_window(struct ws_win
*);
816 void mapnotify(xcb_map_notify_event_t
*);
817 void mappingnotify(xcb_mapping_notify_event_t
*);
818 void maprequest(xcb_map_request_event_t
*);
819 void move(struct ws_win
*, union arg
*);
820 void move_step(struct swm_region
*, union arg
*);
821 uint32_t name_to_pixel(const char *);
822 void name_workspace(struct swm_region
*, union arg
*);
823 void new_region(struct swm_screen
*, int, int, int, int);
824 int parsekeys(char *, unsigned int, unsigned int *, KeySym
*);
825 int parsequirks(char *, unsigned long *);
826 int parse_rgb(const char *, uint16_t *, uint16_t *, uint16_t *);
827 void pressbutton(struct swm_region
*, union arg
*);
828 void priorws(struct swm_region
*, union arg
*);
830 void print_win_geom(xcb_window_t
);
832 void propertynotify(xcb_property_notify_event_t
*);
833 void quirk_insert(const char *, const char *, unsigned long);
834 void quirk_remove(struct quirk
*);
835 void quirk_replace(struct quirk
*, const char *, const char *,
837 void quit(struct swm_region
*, union arg
*);
838 void raise_toggle(struct swm_region
*, union arg
*);
839 void resize(struct ws_win
*, union arg
*);
840 void resize_step(struct swm_region
*, union arg
*);
841 void restart(struct swm_region
*, union arg
*);
842 struct swm_region
*root_to_region(xcb_window_t
, int);
843 void screenchange(xcb_randr_screen_change_notify_event_t
*);
844 void scan_xrandr(int);
845 void search_do_resp(void);
846 void search_resp_name_workspace(const char *, unsigned long);
847 void search_resp_search_window(const char *);
848 void search_resp_search_workspace(const char *);
849 void search_resp_uniconify(const char *, unsigned long);
850 void search_win(struct swm_region
*, union arg
*);
851 void search_win_cleanup(void);
852 void search_workspace(struct swm_region
*, union arg
*);
853 void send_to_ws(struct swm_region
*, union arg
*);
854 int setautorun(char *, char *, int);
855 int setconfbinding(char *, char *, int);
856 int setconfcolor(char *, char *, int);
857 int setconfmodkey(char *, char *, int);
858 int setconfquirk(char *, char *, int);
859 int setconfregion(char *, char *, int);
860 int setconfspawn(char *, char *, int);
861 int setconfvalue(char *, char *, int);
862 int setkeymapping(char *, char *, int);
863 int setlayout(char *, char *, int);
864 void setquirk(const char *, const char *, unsigned long);
865 void setscreencolor(char *, int, int);
866 void setspawn(const char *, const char *);
867 void setup_ewmh(void);
868 void setup_globals(void);
869 void setup_keys(void);
870 void setup_quirks(void);
871 void setup_screens(void);
872 void setup_spawn(void);
873 void set_child_transient(struct ws_win
*, xcb_window_t
*);
874 void set_swm_iconic(struct ws_win
*, int);
875 void set_win_state(struct ws_win
*, uint16_t);
876 void shutdown_cleanup(void);
878 void socket_setnonblock(int);
879 void sort_windows(struct ws_win_list
*);
880 void spawn(int, union arg
*, int);
881 void spawn_custom(struct swm_region
*, union arg
*, const char *);
882 int spawn_expand(struct swm_region
*, union arg
*, const char *, char ***);
883 void spawn_insert(const char *, const char *);
884 void spawn_remove(struct spawn_prog
*);
885 void spawn_replace(struct spawn_prog
*, const char *, const char *);
886 void spawn_select(struct swm_region
*, union arg
*, const char *, int *);
887 void stack_config(struct swm_region
*, union arg
*);
888 void stack_floater(struct ws_win
*, struct swm_region
*);
889 void stack_master(struct workspace
*, struct swm_geometry
*, int, int);
890 void store_float_geom(struct ws_win
*, struct swm_region
*);
891 char * strdupsafe(const char *);
892 void swapwin(struct swm_region
*, union arg
*);
893 void switchws(struct swm_region
*, union arg
*);
894 void teardown_ewmh(void);
895 void unfocus_win(struct ws_win
*);
896 void uniconify(struct swm_region
*, union arg
*);
897 void unmanage_window(struct ws_win
*);
898 void unmapnotify(xcb_unmap_notify_event_t
*);
899 void unmap_all(void);
900 void unmap_window(struct ws_win
*);
901 void updatenumlockmask(void);
902 void update_modkey(unsigned int);
903 void update_window(struct ws_win
*);
904 int validate_win(struct ws_win
*);
905 int validate_ws(struct workspace
*);
906 /*void visibilitynotify(xcb_visibility_notify_event_t *);*/
907 void version(struct swm_region
*, union arg
*);
908 pid_t
window_get_pid(xcb_window_t
);
909 void wkill(struct swm_region
*, union arg
*);
910 void workaround(void);
911 void xft_init(struct swm_region
*);
916 xcb_font_t cf
= XCB_NONE
;
919 for (i
= 0; i
< LENGTH(cursors
); ++i
) {
920 /* try to load Xcursor first. */
921 cursors
[i
].cid
= XcursorLibraryLoadCursor(display
,
924 /* fallback to cursorfont. */
925 if (cursors
[i
].cid
== XCB_CURSOR_NONE
) {
926 if (cf
== XCB_NONE
) {
927 cf
= xcb_generate_id(conn
);
928 xcb_open_font(conn
, cf
, strlen("cursor"),
932 cursors
[i
].cid
= xcb_generate_id(conn
);
933 xcb_create_glyph_cursor(conn
, cursors
[i
].cid
, cf
, cf
,
934 cursors
[i
].cf_char
, cursors
[i
].cf_char
+ 1, 0, 0, 0,
935 0xffff, 0xffff, 0xffff);
941 xcb_close_font(conn
, cf
);
945 cursors_cleanup(void)
948 for (i
= 0; i
< LENGTH(cursors
); ++i
)
949 xcb_free_cursor(conn
, cursors
[i
].cid
);
953 expand_tilde(const char *s
)
962 errx(1, "expand_tilde: NULL string.");
971 if ((max
= sysconf(_SC_LOGIN_NAME_MAX
)) == -1)
972 errx(1, "expand_tilde: sysconf");
974 if ((user
= calloc(1, max
+ 1)) == NULL
)
975 errx(1, "expand_tilde: calloc");
977 for (i
= 0; s
[i
] != '/' && s
[i
] != '\0'; ++i
)
982 ppwd
= strlen(user
) == 0 ? getpwuid(getuid()) : getpwnam(user
);
988 if (asprintf(&result
, "%s%s", ppwd
->pw_dir
, s
) == -1)
992 errx(1, "expand_tilde: failed to allocate memory.");
998 parse_rgb(const char *rgb
, uint16_t *rr
, uint16_t *gg
, uint16_t *bb
)
1000 unsigned int tmpr
, tmpg
, tmpb
;
1002 if (sscanf(rgb
, "rgb:%x/%x/%x", &tmpr
, &tmpg
, &tmpb
) != 3)
1005 *rr
= RGB_8_TO_16(tmpr
);
1006 *gg
= RGB_8_TO_16(tmpg
);
1007 *bb
= RGB_8_TO_16(tmpb
);
1013 get_screen(int screen
)
1015 const xcb_setup_t
*r
;
1016 xcb_screen_iterator_t iter
;
1018 if ((r
= xcb_get_setup(conn
)) == NULL
) {
1019 DNPRINTF(SWM_D_MISC
, "get_screen: xcb_get_setup\n");
1023 iter
= xcb_setup_roots_iterator(r
);
1024 for (; iter
.rem
; --screen
, xcb_screen_next(&iter
))
1034 if (focus_mode
== SWM_FOCUS_DEFAULT
)
1035 event_drain(XCB_ENTER_NOTIFY
);
1041 get_atom_from_string(const char *str
)
1043 xcb_intern_atom_cookie_t c
;
1044 xcb_intern_atom_reply_t
*r
;
1047 c
= xcb_intern_atom(conn
, 0, strlen(str
), str
);
1048 r
= xcb_intern_atom_reply(conn
, c
, NULL
);
1056 return (XCB_ATOM_NONE
);
1060 set_swm_iconic(struct ws_win
*win
, int newv
)
1067 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
1068 a_swm_iconic
, XCB_ATOM_INTEGER
, 32, 1, &v
);
1070 xcb_delete_property(conn
, win
->id
, a_swm_iconic
);
1074 get_swm_iconic(struct ws_win
*win
)
1077 xcb_get_property_reply_t
*pr
= NULL
;
1079 pr
= xcb_get_property_reply(conn
,
1080 xcb_get_property(conn
, 0, win
->id
, a_swm_iconic
,
1081 XCB_ATOM_INTEGER
, 0, 1), NULL
);
1084 if (pr
->type
!= XCB_ATOM_INTEGER
|| pr
->format
!= 32)
1086 v
= *((int32_t *)xcb_get_property_value(pr
));
1096 xcb_atom_t sup_list
;
1097 int i
, j
, num_screens
;
1099 sup_list
= get_atom_from_string("_NET_SUPPORTED");
1101 for (i
= 0; i
< LENGTH(ewmh
); i
++)
1102 ewmh
[i
].atom
= get_atom_from_string(ewmh
[i
].name
);
1104 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1105 for (i
= 0; i
< num_screens
; i
++) {
1106 /* Support check window will be created by workaround(). */
1108 /* Report supported atoms */
1109 xcb_delete_property(conn
, screens
[i
].root
, sup_list
);
1110 for (j
= 0; j
< LENGTH(ewmh
); j
++)
1111 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
,
1112 screens
[i
].root
, sup_list
, XCB_ATOM_ATOM
, 32, 1,
1121 xcb_atom_t sup_check
, sup_list
;
1123 xcb_get_property_cookie_t pc
;
1124 xcb_get_property_reply_t
*pr
;
1126 sup_check
= get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
1127 sup_list
= get_atom_from_string("_NET_SUPPORTED");
1128 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1130 for (i
= 0; i
< num_screens
; i
++) {
1131 /* Get the support check window and destroy it */
1132 pc
= xcb_get_property(conn
, 0, screens
[i
].root
, sup_check
,
1133 XCB_ATOM_WINDOW
, 0, 1);
1134 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
1137 if (pr
->format
== sup_check
) {
1138 id
= *((xcb_window_t
*)xcb_get_property_value(pr
));
1140 xcb_destroy_window(conn
, id
);
1141 xcb_delete_property(conn
, screens
[i
].root
, sup_check
);
1142 xcb_delete_property(conn
, screens
[i
].root
, sup_list
);
1149 ewmh_autoquirk(struct ws_win
*win
)
1153 xcb_get_property_cookie_t c
;
1154 xcb_get_property_reply_t
*r
;
1156 c
= xcb_get_property(conn
, 0, win
->id
,
1157 ewmh
[_NET_WM_WINDOW_TYPE
].atom
, XCB_ATOM_ATOM
, 0, UINT32_MAX
);
1158 r
= xcb_get_property_reply(conn
, c
, NULL
);
1162 n
= xcb_get_property_value_length(r
);
1163 type
= xcb_get_property_value(r
);
1165 for (i
= 0; i
< n
; i
++) {
1166 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_NORMAL
].atom
)
1168 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_DOCK
].atom
||
1169 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_TOOLBAR
].atom
||
1170 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_UTILITY
].atom
) {
1172 win
->quirks
= SWM_Q_FLOAT
| SWM_Q_ANYWHERE
;
1175 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_SPLASH
].atom
||
1176 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_DIALOG
].atom
) {
1178 win
->quirks
= SWM_Q_FLOAT
;
1185 #define SWM_EWMH_ACTION_COUNT_MAX (6)
1186 #define EWMH_F_FULLSCREEN (1<<0)
1187 #define EWMH_F_ABOVE (1<<1)
1188 #define EWMH_F_HIDDEN (1<<2)
1189 #define EWMH_F_SKIP_PAGER (1<<3)
1190 #define EWMH_F_SKIP_TASKBAR (1<<4)
1191 #define SWM_F_MANUAL (1<<5)
1194 ewmh_set_win_fullscreen(struct ws_win
*win
, int fs
)
1202 DNPRINTF(SWM_D_MISC
, "ewmh_set_win_fullscreen: window: 0x%x, "
1203 "fullscreen %s\n", win
->id
, YESNO(fs
));
1206 if (!win
->g_floatvalid
)
1207 store_float_geom(win
, win
->ws
->r
);
1209 win
->g
= win
->ws
->r
->g
;
1212 load_float_geom(win
, win
->ws
->r
);
1219 ewmh_update_actions(struct ws_win
*win
)
1221 xcb_atom_t actions
[SWM_EWMH_ACTION_COUNT_MAX
];
1227 actions
[n
++] = ewmh
[_NET_WM_ACTION_CLOSE
].atom
;
1229 if (win
->floating
) {
1230 actions
[n
++] = ewmh
[_NET_WM_ACTION_MOVE
].atom
;
1231 actions
[n
++] = ewmh
[_NET_WM_ACTION_RESIZE
].atom
;
1232 actions
[n
++] = ewmh
[_NET_WM_ACTION_ABOVE
].atom
;
1235 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
1236 ewmh
[_NET_WM_ALLOWED_ACTIONS
].atom
, XCB_ATOM_ATOM
, 32, 1, actions
);
1239 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
1240 #define _NET_WM_STATE_ADD 1 /* add/set property */
1241 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
1244 ewmh_update_win_state(struct ws_win
*win
, xcb_atom_t state
, long action
)
1246 unsigned int mask
= 0;
1247 unsigned int changed
= 0;
1248 unsigned int orig_flags
;
1253 DNPRINTF(SWM_D_PROP
, "ewmh_update_win_state: window: 0x%x, state: %ld, "
1254 "action: %ld\n", win
->id
, (unsigned long)state
, action
);
1256 if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
)
1257 mask
= EWMH_F_FULLSCREEN
;
1258 else if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
)
1259 mask
= EWMH_F_ABOVE
;
1260 else if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
)
1261 mask
= SWM_F_MANUAL
;
1262 else if (state
== ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
)
1263 mask
= EWMH_F_SKIP_PAGER
;
1264 else if (state
== ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
)
1265 mask
= EWMH_F_SKIP_TASKBAR
;
1267 orig_flags
= win
->ewmh_flags
;
1270 case _NET_WM_STATE_REMOVE
:
1271 win
->ewmh_flags
&= ~mask
;
1273 case _NET_WM_STATE_ADD
:
1274 win
->ewmh_flags
|= mask
;
1276 case _NET_WM_STATE_TOGGLE
:
1277 win
->ewmh_flags
^= mask
;
1281 changed
= (win
->ewmh_flags
& mask
) ^ (orig_flags
& mask
) ? 1 : 0;
1283 if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
) {
1284 if (changed
&& !floating_toggle_win(win
))
1285 win
->ewmh_flags
= orig_flags
; /* revert */
1286 } else if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
) {
1288 win
->manual
= (win
->ewmh_flags
& SWM_F_MANUAL
) != 0;
1289 } else if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
) {
1290 if (changed
&& !ewmh_set_win_fullscreen(win
,
1291 win
->ewmh_flags
& EWMH_F_FULLSCREEN
))
1292 win
->ewmh_flags
= orig_flags
; /* revert */
1295 xcb_delete_property(conn
, win
->id
, ewmh
[_NET_WM_STATE
].atom
);
1297 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
1298 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1299 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1300 &ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
);
1301 else if (win
->ewmh_flags
& EWMH_F_SKIP_PAGER
)
1302 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1303 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1304 &ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
);
1305 else if (win
->ewmh_flags
& EWMH_F_SKIP_TASKBAR
)
1306 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1307 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1308 &ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
);
1309 else if (win
->ewmh_flags
& EWMH_F_ABOVE
)
1310 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1311 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1312 &ewmh
[_NET_WM_STATE_ABOVE
].atom
);
1313 else if (win
->ewmh_flags
& SWM_F_MANUAL
)
1314 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1315 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1316 &ewmh
[_SWM_WM_STATE_MANUAL
].atom
);
1320 ewmh_get_win_state(struct ws_win
*win
)
1323 xcb_get_property_cookie_t c
;
1324 xcb_get_property_reply_t
*r
;
1330 win
->ewmh_flags
= 0;
1332 win
->ewmh_flags
|= EWMH_F_ABOVE
;
1334 win
->ewmh_flags
|= SWM_F_MANUAL
;
1336 c
= xcb_get_property(conn
, 0, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
1337 XCB_ATOM_ATOM
, 0, UINT32_MAX
);
1338 r
= xcb_get_property_reply(conn
, c
, NULL
);
1342 states
= xcb_get_property_value(r
);
1343 n
= xcb_get_property_value_length(r
);
1345 for (i
= 0; i
< n
; i
++)
1346 ewmh_update_win_state(win
, states
[i
], _NET_WM_STATE_ADD
);
1354 dumpwins(struct swm_region
*r
, union arg
*args
)
1358 xcb_get_window_attributes_cookie_t c
;
1359 xcb_get_window_attributes_reply_t
*wa
;
1361 /* suppress unused warning since var is needed */
1364 if (r
->ws
== NULL
) {
1365 warnx("dumpwins: invalid workspace");
1369 warnx("=== managed window list ws %02d ===", r
->ws
->idx
);
1370 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
1371 state
= getstate(win
->id
);
1372 c
= xcb_get_window_attributes(conn
, win
->id
);
1373 wa
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
1375 warnx("window: 0x%x, map_state: %d, state: %u, "
1376 "transient: 0x%x", win
->id
, wa
->map_state
,
1377 state
, win
->transient
);
1380 warnx("window: 0x%x, failed xcb_get_window_attributes",
1384 warnx("===== unmanaged window list =====");
1385 TAILQ_FOREACH(win
, &r
->ws
->unmanagedlist
, entry
) {
1386 state
= getstate(win
->id
);
1387 c
= xcb_get_window_attributes(conn
, win
->id
);
1388 wa
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
1390 warnx("window: 0x%x, map_state: %d, state: %u, "
1391 "transient: 0x%x", win
->id
, wa
->map_state
,
1392 state
, win
->transient
);
1395 warnx("window: 0x%x, failed xcb_get_window_attributes",
1399 warnx("=================================");
1403 dumpwins(struct swm_region
*r
, union arg
*s
)
1408 #endif /* SWM_DEBUG */
1413 int saved_errno
, status
;
1416 saved_errno
= errno
;
1420 while ((pid
= waitpid(WAIT_ANY
, &status
, WNOHANG
)) != 0) {
1425 if (errno
!= ECHILD
)
1426 warn("sighdlr: waitpid");
1427 #endif /* SWM_DEBUG */
1430 if (pid
== searchpid
)
1434 if (WIFEXITED(status
)) {
1435 if (WEXITSTATUS(status
) != 0)
1436 warnx("sighdlr: child exit status: %d",
1437 WEXITSTATUS(status
));
1439 warnx("sighdlr: child is terminated "
1441 #endif /* SWM_DEBUG */
1455 errno
= saved_errno
;
1461 struct pid_e
*p
= NULL
;
1463 DNPRINTF(SWM_D_MISC
, "find_pid: %d\n", pid
);
1468 TAILQ_FOREACH(p
, &pidlist
, entry
) {
1477 name_to_pixel(const char *colorname
)
1479 uint32_t result
= 0;
1480 char cname
[32] = "#";
1481 xcb_screen_t
*screen
;
1482 xcb_colormap_t cmap
;
1483 xcb_alloc_color_reply_t
*cr
;
1484 xcb_alloc_named_color_reply_t
*nr
;
1485 uint16_t rr
, gg
, bb
;
1487 screen
= xcb_setup_roots_iterator(xcb_get_setup(conn
)).data
;
1488 cmap
= screen
->default_colormap
;
1490 /* color is in format rgb://rr/gg/bb */
1491 if (strncmp(colorname
, "rgb:", 4) == 0) {
1492 if (parse_rgb(colorname
, &rr
, &gg
, &bb
) == -1)
1493 warnx("could not parse rgb %s", colorname
);
1495 cr
= xcb_alloc_color_reply(conn
,
1496 xcb_alloc_color(conn
, cmap
, rr
, gg
, bb
),
1502 warnx("color '%s' not found", colorname
);
1505 nr
= xcb_alloc_named_color_reply(conn
,
1506 xcb_alloc_named_color(conn
, cmap
, strlen(colorname
),
1509 strlcat(cname
, colorname
+ 2, sizeof cname
- 1);
1510 nr
= xcb_alloc_named_color_reply(conn
,
1511 xcb_alloc_named_color(conn
, cmap
, strlen(cname
),
1518 warnx("color '%s' not found", colorname
);
1525 setscreencolor(char *val
, int i
, int c
)
1529 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1530 if (i
> 0 && i
<= num_screens
) {
1531 screens
[i
- 1].c
[c
].pixel
= name_to_pixel(val
);
1532 free(screens
[i
- 1].c
[c
].name
);
1533 if ((screens
[i
- 1].c
[c
].name
= strdup(val
)) == NULL
)
1535 } else if (i
== -1) {
1536 for (i
= 0; i
< num_screens
; i
++) {
1537 screens
[i
].c
[c
].pixel
= name_to_pixel(val
);
1538 free(screens
[i
].c
[c
].name
);
1539 if ((screens
[i
].c
[c
].name
= strdup(val
)) == NULL
)
1543 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1548 fancy_stacker(struct workspace
*ws
)
1550 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1551 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1552 snprintf(ws
->stacker
, sizeof ws
->stacker
,
1553 ws
->l_state
.vertical_flip
? "[%d>%d]" : "[%d|%d]",
1554 ws
->l_state
.vertical_mwin
, ws
->l_state
.vertical_stacks
);
1555 if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1556 snprintf(ws
->stacker
, sizeof ws
->stacker
,
1557 ws
->l_state
.horizontal_flip
? "[%dv%d]" : "[%d-%d]",
1558 ws
->l_state
.horizontal_mwin
, ws
->l_state
.horizontal_stacks
);
1562 plain_stacker(struct workspace
*ws
)
1564 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1565 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1566 strlcpy(ws
->stacker
, ws
->l_state
.vertical_flip
? "[>]" : "[|]",
1567 sizeof ws
->stacker
);
1568 if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1569 strlcpy(ws
->stacker
, ws
->l_state
.horizontal_flip
? "[v]" : "[-]",
1570 sizeof ws
->stacker
);
1574 custom_region(char *val
)
1576 unsigned int x
, y
, w
, h
;
1577 int sidx
, num_screens
;
1578 xcb_screen_t
*screen
;
1580 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1581 if (sscanf(val
, "screen[%d]:%ux%u+%u+%u", &sidx
, &w
, &h
, &x
, &y
) != 5)
1582 errx(1, "invalid custom region, "
1583 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1584 if (sidx
< 1 || sidx
> num_screens
)
1585 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1589 if ((screen
= get_screen(sidx
)) == NULL
)
1590 errx(1, "ERROR: can't get screen %d.", sidx
);
1593 errx(1, "region %ux%u+%u+%u too small", w
, h
, x
, y
);
1595 if (x
> screen
->width_in_pixels
||
1596 y
> screen
->height_in_pixels
||
1597 w
+ x
> screen
->width_in_pixels
||
1598 h
+ y
> screen
->height_in_pixels
) {
1599 warnx("ignoring region %ux%u+%u+%u - not within screen "
1600 "boundaries (%ux%u)", w
, h
, x
, y
,
1601 screen
->width_in_pixels
, screen
->height_in_pixels
);
1605 new_region(&screens
[sidx
], x
, y
, w
, h
);
1609 socket_setnonblock(int fd
)
1613 if ((flags
= fcntl(fd
, F_GETFL
, 0)) == -1)
1614 err(1, "fcntl F_GETFL");
1615 flags
|= O_NONBLOCK
;
1616 if ((flags
= fcntl(fd
, F_SETFL
, flags
)) == -1)
1617 err(1, "fcntl F_SETFL");
1621 bar_print_legacy(struct swm_region
*r
, const char *s
)
1623 xcb_rectangle_t rect
;
1628 XRectangle ibox
, lbox
;
1632 XmbTextExtents(bar_fs
, s
, len
, &ibox
, &lbox
);
1634 switch (bar_justify
) {
1635 case SWM_BAR_JUSTIFY_LEFT
:
1638 case SWM_BAR_JUSTIFY_CENTER
:
1639 x
= (WIDTH(r
) - lbox
.width
) / 2;
1641 case SWM_BAR_JUSTIFY_RIGHT
:
1642 x
= WIDTH(r
) - lbox
.width
- SWM_BAR_OFFSET
;
1646 if (x
< SWM_BAR_OFFSET
)
1651 rect
.width
= WIDTH(r
->bar
);
1652 rect
.height
= HEIGHT(r
->bar
);
1654 /* clear back buffer */
1655 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
1656 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
1657 xcb_poly_fill_rectangle(conn
, r
->bar
->buffer
, r
->s
->bar_gc
, 1, &rect
);
1659 /* draw back buffer */
1660 gcvd
.graphics_exposures
= 0;
1661 draw
= XCreateGC(display
, r
->bar
->buffer
, GCGraphicsExposures
, &gcvd
);
1662 XSetForeground(display
, draw
, r
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
);
1663 DRAWSTRING(display
, r
->bar
->buffer
, bar_fs
, draw
,
1664 x
, (bar_fs_extents
->max_logical_extent
.height
- lbox
.height
) / 2 -
1666 XFreeGC(display
, draw
);
1669 xcb_copy_area(conn
, r
->bar
->buffer
, r
->bar
->id
, r
->s
->bar_gc
, 0, 0,
1670 0, 0, WIDTH(r
->bar
), HEIGHT(r
->bar
));
1674 bar_print(struct swm_region
*r
, const char *s
)
1677 xcb_rectangle_t rect
;
1685 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
, &info
);
1687 switch (bar_justify
) {
1688 case SWM_BAR_JUSTIFY_LEFT
:
1691 case SWM_BAR_JUSTIFY_CENTER
:
1692 x
= (WIDTH(r
) - info
.width
) / 2;
1694 case SWM_BAR_JUSTIFY_RIGHT
:
1695 x
= WIDTH(r
) - info
.width
- SWM_BAR_OFFSET
;
1699 if (x
< SWM_BAR_OFFSET
)
1704 rect
.width
= WIDTH(r
->bar
);
1705 rect
.height
= HEIGHT(r
->bar
);
1707 /* clear back buffer */
1708 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
1709 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
1710 xcb_poly_fill_rectangle(conn
, r
->bar
->buffer
, r
->s
->bar_gc
, 1, &rect
);
1712 /* draw back buffer */
1713 draw
= XftDrawCreate(display
, r
->bar
->buffer
,
1714 DefaultVisual(display
, r
->s
->idx
),
1715 DefaultColormap(display
, r
->s
->idx
));
1717 XftDrawStringUtf8(draw
, &bar_font_color
, bar_font
, x
,
1718 (HEIGHT(r
->bar
) + bar_font
->height
) / 2 - bar_font
->descent
,
1721 XftDrawDestroy(draw
);
1724 xcb_copy_area(conn
, r
->bar
->buffer
, r
->bar
->id
, r
->s
->bar_gc
, 0, 0,
1725 0, 0, WIDTH(r
->bar
), HEIGHT(r
->bar
));
1729 bar_extra_stop(void)
1733 bzero(bar_pipe
, sizeof bar_pipe
);
1736 kill(bar_pid
, SIGTERM
);
1739 strlcpy((char *)bar_ext
, "", sizeof bar_ext
);
1744 bar_class_name(char *s
, size_t sz
, struct swm_region
*r
)
1746 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1748 if (r
->ws
->focus
->ch
.class_name
!= NULL
)
1749 strlcat(s
, r
->ws
->focus
->ch
.class_name
, sz
);
1753 bar_title_name(char *s
, size_t sz
, struct swm_region
*r
)
1755 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1757 if (r
->ws
->focus
->ch
.instance_name
!= NULL
)
1758 strlcat(s
, r
->ws
->focus
->ch
.instance_name
, sz
);
1762 bar_class_title_name(char *s
, size_t sz
, struct swm_region
*r
)
1764 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1767 bar_class_name(s
, sz
, r
);
1768 strlcat(s
, ":", sz
);
1769 bar_title_name(s
, sz
, r
);
1773 bar_window_float(char *s
, size_t sz
, struct swm_region
*r
)
1775 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1777 if (r
->ws
->focus
->floating
)
1778 strlcat(s
, "(f)", sz
);
1782 bar_window_name(char *s
, size_t sz
, struct swm_region
*r
)
1786 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1788 if ((title
= get_win_name(r
->ws
->focus
->id
)) == NULL
)
1791 strlcat(s
, title
, sz
);
1795 int urgent
[SWM_WS_MAX
];
1797 bar_urgent(char *s
, size_t sz
)
1800 int i
, j
, num_screens
;
1802 xcb_get_property_cookie_t c
;
1803 xcb_icccm_wm_hints_t hints
;
1805 for (i
= 0; i
< workspace_limit
; i
++)
1808 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1809 for (i
= 0; i
< num_screens
; i
++)
1810 for (j
= 0; j
< workspace_limit
; j
++)
1811 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
) {
1812 c
= xcb_icccm_get_wm_hints(conn
, win
->id
);
1813 if (xcb_icccm_get_wm_hints_reply(conn
, c
,
1816 if (hints
.flags
& XCB_ICCCM_WM_HINT_X_URGENCY
)
1820 for (i
= 0; i
< workspace_limit
; i
++) {
1822 snprintf(b
, sizeof b
, "%d ", i
+ 1);
1824 snprintf(b
, sizeof b
, "- ");
1830 bar_workspace_name(char *s
, size_t sz
, struct swm_region
*r
)
1832 if (r
== NULL
|| r
->ws
== NULL
)
1834 if (r
->ws
->name
!= NULL
)
1835 strlcat(s
, r
->ws
->name
, sz
);
1838 /* build the default bar format according to the defined enabled options */
1840 bar_fmt(const char *fmtexp
, char *fmtnew
, struct swm_region
*r
, size_t sz
)
1842 /* if format provided, just copy the buffers */
1843 if (bar_format
!= NULL
) {
1844 strlcpy(fmtnew
, fmtexp
, sz
);
1848 /* reset the output buffer */
1851 strlcat(fmtnew
, "+N:+I ", sz
);
1853 strlcat(fmtnew
, "+S", sz
);
1854 strlcat(fmtnew
, " ", sz
);
1856 /* only show the workspace name if there's actually one */
1857 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->name
!= NULL
)
1858 strlcat(fmtnew
, "<+D>", sz
);
1859 strlcat(fmtnew
, "+3<", sz
);
1861 if (clock_enabled
) {
1862 strlcat(fmtnew
, fmtexp
, sz
);
1863 strlcat(fmtnew
, "+4<", sz
);
1866 /* bar_urgent already adds the space before the last asterisk */
1868 strlcat(fmtnew
, "* +U*+4<", sz
);
1870 if (title_class_enabled
) {
1871 strlcat(fmtnew
, "+C", sz
);
1872 if (!title_name_enabled
)
1873 strlcat(fmtnew
, "+4<", sz
);
1876 /* checks needed by the colon and floating strlcat(3) calls below */
1877 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->focus
!= NULL
) {
1878 if (title_name_enabled
) {
1879 if (title_class_enabled
)
1880 strlcat(fmtnew
, ":", sz
);
1881 strlcat(fmtnew
, "+T+4<", sz
);
1883 if (window_name_enabled
) {
1884 if (r
->ws
->focus
->floating
)
1885 strlcat(fmtnew
, "+F ", sz
);
1886 strlcat(fmtnew
, "+64W ", sz
);
1890 /* finally add the action script output and the version */
1891 strlcat(fmtnew
, "+4<+A+4<+V", sz
);
1895 bar_replace_pad(char *tmp
, int *limit
, size_t sz
)
1897 /* special case; no limit given, pad one space, instead */
1898 if (*limit
== (int)sz
- 1)
1900 snprintf(tmp
, sz
, "%*s", *limit
, " ");
1903 /* replaces the bar format character sequences (like in tmux(1)) */
1905 bar_replace_seq(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t *offrep
,
1909 char tmp
[SWM_BAR_MAX
];
1913 /* reset strlcat(3) buffer */
1916 /* get number, if any */
1919 if (sscanf(fmt
, "%d%n", &limit
, &size
) != 1)
1920 limit
= sizeof tmp
- 1;
1921 if (limit
<= 0 || limit
>= (int)sizeof tmp
)
1922 limit
= sizeof tmp
- 1;
1924 /* there is nothing to replace (ie EOL) */
1931 bar_replace_pad(tmp
, &limit
, sizeof tmp
);
1934 snprintf(tmp
, sizeof tmp
, "%s", bar_ext
);
1937 bar_class_name(tmp
, sizeof tmp
, r
);
1940 bar_workspace_name(tmp
, sizeof tmp
, r
);
1943 bar_window_float(tmp
, sizeof tmp
, r
);
1946 snprintf(tmp
, sizeof tmp
, "%d", r
->ws
->idx
+ 1);
1949 snprintf(tmp
, sizeof tmp
, "%d", r
->s
->idx
+ 1);
1952 bar_class_title_name(tmp
, sizeof tmp
, r
);
1955 snprintf(tmp
, sizeof tmp
, "%s", r
->ws
->stacker
);
1958 bar_title_name(tmp
, sizeof tmp
, r
);
1961 bar_urgent(tmp
, sizeof tmp
);
1964 snprintf(tmp
, sizeof tmp
, "%s", bar_vertext
);
1967 bar_window_name(tmp
, sizeof tmp
, r
);
1970 /* unknown character sequence; copy as-is */
1971 snprintf(tmp
, sizeof tmp
, "+%c", *fmt
);
1977 if ((int)len
< limit
)
1979 while (limit
-- > 0) {
1980 if (*offrep
>= sz
- 1)
1982 fmtrep
[(*offrep
)++] = *ptr
++;
1990 bar_replace(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t sz
)
1995 while (*fmt
!= '\0') {
1997 /* skip ordinary characters */
2000 fmtrep
[off
++] = *fmt
++;
2004 /* character sequence found; replace it */
2005 fmt
= bar_replace_seq(fmt
, fmtrep
, r
, &off
, sz
);
2014 bar_fmt_expand(char *fmtexp
, size_t sz
)
2021 /* start by grabbing the current time and date */
2023 localtime_r(&tmt
, &tm
);
2025 /* figure out what to expand */
2026 if (bar_format
!= NULL
)
2028 else if (bar_format
== NULL
&& clock_enabled
)
2030 /* if nothing to expand bail out */
2036 /* copy as-is, just in case the format shouldn't be expanded below */
2037 strlcpy(fmtexp
, fmt
, sz
);
2038 /* finally pass the string through strftime(3) */
2039 #ifndef SWM_DENY_CLOCK_FORMAT
2040 if ((len
= strftime(fmtexp
, sz
, fmt
, &tm
)) == 0)
2041 warnx("format too long");
2049 char fmtexp
[SWM_BAR_MAX
], fmtnew
[SWM_BAR_MAX
];
2050 char fmtrep
[SWM_BAR_MAX
];
2052 struct swm_region
*r
;
2054 /* expand the format by first passing it through strftime(3) */
2055 bar_fmt_expand(fmtexp
, sizeof fmtexp
);
2057 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2058 for (i
= 0; i
< num_screens
; i
++) {
2059 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
2062 bar_fmt(fmtexp
, fmtnew
, r
, sizeof fmtnew
);
2063 bar_replace(fmtnew
, fmtrep
, r
, sizeof fmtrep
);
2064 if (bar_font_legacy
)
2065 bar_print_legacy(r
, fmtrep
);
2067 bar_print(r
, fmtrep
);
2080 if (bar_extra
&& bar_extra_running
) {
2081 /* ignore short reads; it'll correct itself */
2082 while ((b
= fgetln(stdin
, &len
)) != NULL
)
2083 if (b
&& b
[len
- 1] == '\n') {
2085 strlcpy((char *)bar_ext
, b
, sizeof bar_ext
);
2087 if (b
== NULL
&& errno
!= EAGAIN
) {
2088 warn("bar_update: bar_extra failed");
2092 strlcpy((char *)bar_ext
, "", sizeof bar_ext
);
2101 /* suppress unused warning since var is needed */
2108 bar_toggle(struct swm_region
*r
, union arg
*args
)
2110 struct swm_region
*tmpr
;
2113 /* suppress unused warnings since vars are needed */
2117 DNPRINTF(SWM_D_BAR
, "bar_toggle\n");
2119 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2121 for (i
= 0; i
< num_screens
; i
++)
2122 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
2124 xcb_unmap_window(conn
, tmpr
->bar
->id
);
2126 for (i
= 0; i
< num_screens
; i
++)
2127 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
2129 xcb_map_window(conn
, tmpr
->bar
->id
);
2132 bar_enabled
= !bar_enabled
;
2136 /* must be after stack */
2145 struct swm_region
*r
;
2149 /* do this here because the conf file is in memory */
2150 if (bar_extra
&& !bar_extra_running
&& bar_argv
[0]) {
2151 /* launch external status app */
2152 bar_extra_running
= 1;
2153 if (pipe(bar_pipe
) == -1)
2154 err(1, "pipe error");
2155 socket_setnonblock(bar_pipe
[0]);
2156 socket_setnonblock(bar_pipe
[1]); /* XXX hmmm, really? */
2157 if (dup2(bar_pipe
[0], 0) == -1)
2159 if (dup2(bar_pipe
[1], 1) == -1)
2161 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
2162 err(1, "could not disable SIGPIPE");
2163 switch (bar_pid
= fork()) {
2165 err(1, "cannot fork");
2169 execvp(bar_argv
[0], bar_argv
);
2170 err(1, "%s external app failed", bar_argv
[0]);
2172 default: /* parent */
2178 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2179 for (i
= 0; i
< num_screens
; i
++)
2180 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
2183 wa
[0] = screens
[i
].c
[SWM_S_COLOR_BAR
].pixel
;
2184 wa
[1] = screens
[i
].c
[SWM_S_COLOR_BAR_BORDER
].pixel
;
2185 xcb_change_window_attributes(conn
, r
->bar
->id
,
2186 XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
, wa
);
2196 while ((s
= index(s
, '-'))) {
2201 return (count
== 14);
2207 char *default_string
;
2208 char **missing_charsets
;
2209 int num_missing_charsets
= 0;
2213 XFreeFontSet(display
, bar_fs
);
2217 DNPRINTF(SWM_D_INIT
, "fontset_init: loading bar_fonts: %s\n", bar_fonts
);
2219 bar_fs
= XCreateFontSet(display
, bar_fonts
, &missing_charsets
,
2220 &num_missing_charsets
, &default_string
);
2222 if (num_missing_charsets
> 0) {
2223 warnx("Unable to load charset(s):");
2225 for (i
= 0; i
< num_missing_charsets
; ++i
)
2226 warnx("%s", missing_charsets
[i
]);
2228 XFreeStringList(missing_charsets
);
2230 if (strcmp(default_string
, ""))
2231 warnx("Glyphs from those sets will be replaced "
2232 "by '%s'.", default_string
);
2234 warnx("Glyphs from those sets won't be drawn.");
2238 errx(1, "Error creating font set structure.");
2240 bar_fs_extents
= XExtentsOfFontSet(bar_fs
);
2242 bar_height
= bar_fs_extents
->max_logical_extent
.height
+
2243 2 * bar_border_width
;
2250 xft_init(struct swm_region
*r
)
2252 char *font
, *d
, *search
;
2255 if (bar_font
== NULL
) {
2256 if ((d
= strdup(bar_fonts
)) == NULL
)
2257 errx(1, "insufficient memory.");
2259 while ((font
= strsep(&search
, ",")) != NULL
) {
2263 DNPRINTF(SWM_D_INIT
, "xft_init: try font %s\n", font
);
2266 bar_font
= XftFontOpenXlfd(display
, r
->s
->idx
,
2269 bar_font
= XftFontOpenName(display
, r
->s
->idx
,
2274 warnx("unable to load font %s", font
);
2277 DNPRINTF(SWM_D_INIT
, "successfully opened "
2285 if (bar_font
== NULL
)
2286 errx(1, "unable to open a font");
2288 PIXEL_TO_XRENDERCOLOR(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
, color
);
2290 if (!XftColorAllocValue(display
, DefaultVisual(display
, r
->s
->idx
),
2291 DefaultColormap(display
, r
->s
->idx
), &color
, &bar_font_color
))
2292 warn("unable to allocate Xft color");
2294 bar_height
= bar_font
->height
+ 2 * bar_border_width
;
2301 bar_setup(struct swm_region
*r
)
2303 xcb_screen_t
*screen
;
2306 DNPRINTF(SWM_D_BAR
, "bar_setup: screen %d.\n",
2309 if ((screen
= get_screen(r
->s
->idx
)) == NULL
)
2310 errx(1, "ERROR: can't get screen %d.", r
->s
->idx
);
2315 if ((r
->bar
= calloc(1, sizeof(struct swm_bar
))) == NULL
)
2316 err(1, "bar_setup: calloc: failed to allocate memory.");
2318 if (bar_font_legacy
)
2324 Y(r
->bar
) = bar_at_bottom
? (Y(r
) + HEIGHT(r
) - bar_height
) : Y(r
);
2325 WIDTH(r
->bar
) = WIDTH(r
) - 2 * bar_border_width
;
2326 HEIGHT(r
->bar
) = bar_height
- 2 * bar_border_width
;
2328 r
->bar
->id
= xcb_generate_id(conn
);
2329 wa
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
2330 wa
[1] = r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].pixel
;
2331 wa
[2] = XCB_EVENT_MASK_EXPOSURE
;
2333 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, r
->bar
->id
, r
->s
->root
,
2334 X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
), HEIGHT(r
->bar
),
2335 bar_border_width
, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
2336 XCB_COPY_FROM_PARENT
, XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
2337 | XCB_CW_EVENT_MASK
, wa
);
2339 r
->bar
->buffer
= xcb_generate_id(conn
);
2340 xcb_create_pixmap(conn
, screen
->root_depth
, r
->bar
->buffer
, r
->bar
->id
,
2341 WIDTH(r
->bar
), HEIGHT(r
->bar
));
2344 xcb_randr_select_input(conn
, r
->bar
->id
,
2345 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE
);
2348 xcb_map_window(conn
, r
->bar
->id
);
2350 DNPRINTF(SWM_D_BAR
, "bar_setup: window: 0x%x, (x,y) w x h: (%d,%d) "
2351 "%d x %d\n", WINID(r
->bar
), X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
),
2354 if (signal(SIGALRM
, bar_signal
) == SIG_ERR
)
2355 err(1, "could not install bar_signal");
2360 bar_cleanup(struct swm_region
*r
)
2364 xcb_destroy_window(conn
, r
->bar
->id
);
2365 xcb_free_pixmap(conn
, r
->bar
->buffer
);
2371 set_win_state(struct ws_win
*win
, uint16_t state
)
2373 uint16_t data
[2] = { state
, XCB_ATOM_NONE
};
2375 DNPRINTF(SWM_D_EVENT
, "set_win_state: window: 0x%x, state: %u\n",
2381 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
, a_state
,
2382 a_state
, 32, 2, data
);
2386 getstate(xcb_window_t w
)
2388 uint32_t result
= 0;
2389 xcb_get_property_cookie_t c
;
2390 xcb_get_property_reply_t
*r
;
2392 c
= xcb_get_property(conn
, 0, w
, a_state
, a_state
, 0L, 2L);
2393 r
= xcb_get_property_reply(conn
, c
, NULL
);
2396 if (r
->type
== a_state
&& r
->format
== 32 && r
->length
== 2)
2397 result
= *((uint32_t *)xcb_get_property_value(r
));
2401 DNPRINTF(SWM_D_MISC
, "getstate property: win 0x%x state %u\n", w
,
2407 version(struct swm_region
*r
, union arg
*args
)
2409 /* suppress unused warnings since vars are needed */
2413 bar_version
= !bar_version
;
2415 snprintf(bar_vertext
, sizeof bar_vertext
,
2416 "Version: %s Build: %s", SPECTRWM_VERSION
, buildstr
);
2418 strlcpy(bar_vertext
, "", sizeof bar_vertext
);
2425 client_msg(struct ws_win
*win
, xcb_atom_t a
, xcb_timestamp_t t
)
2427 xcb_client_message_event_t ev
;
2435 name
= get_atom_name(a
);
2436 DNPRINTF(SWM_D_EVENT
, "client_msg: window: 0x%x, atom: %s(%u), "
2438 win
->id
, name
, a
, t
);
2442 bzero(&ev
, sizeof ev
);
2443 ev
.response_type
= XCB_CLIENT_MESSAGE
;
2444 ev
.window
= win
->id
;
2447 ev
.data
.data32
[0] = a
;
2448 ev
.data
.data32
[1] = t
;
2450 xcb_send_event(conn
, 0, win
->id
,
2451 XCB_EVENT_MASK_NO_EVENT
, (const char *)&ev
);
2454 /* synthetic response to a ConfigureRequest when not making a change */
2456 config_win(struct ws_win
*win
, xcb_configure_request_event_t
*ev
)
2458 xcb_configure_notify_event_t ce
;
2463 /* send notification of unchanged state. */
2464 bzero(&ce
, sizeof(ce
));
2465 ce
.response_type
= XCB_CONFIGURE_NOTIFY
;
2468 ce
.width
= WIDTH(win
);
2469 ce
.height
= HEIGHT(win
);
2470 ce
.override_redirect
= 0;
2475 ce
.window
= win
->id
;
2476 ce
.border_width
= BORDER(win
);
2477 ce
.above_sibling
= XCB_WINDOW_NONE
;
2480 ce
.event
= ev
->window
;
2481 ce
.window
= ev
->window
;
2483 /* make response appear more WM_SIZE_HINTS-compliant */
2484 if (win
->sh
.flags
) {
2485 DNPRINTF(SWM_D_MISC
, "config_win: hints: window: 0x%x,"
2486 " sh.flags: %u, min: %d x %d, max: %d x %d, inc: "
2487 "%d x %d\n", win
->id
, win
->sh
.flags
, SH_MIN_W(win
),
2488 SH_MIN_H(win
), SH_MAX_W(win
), SH_MAX_H(win
),
2489 SH_INC_W(win
), SH_INC_H(win
));
2494 /* the hint may be set... to 0! */
2495 if (SH_MIN_W(win
) > 0 && ce
.width
< SH_MIN_W(win
))
2496 ce
.width
= SH_MIN_W(win
);
2497 if (SH_MIN_H(win
) > 0 && ce
.height
< SH_MIN_H(win
))
2498 ce
.height
= SH_MIN_H(win
);
2503 /* may also be advertized as 0 */
2504 if (SH_MAX_W(win
) > 0 && ce
.width
> SH_MAX_W(win
))
2505 ce
.width
= SH_MAX_W(win
);
2506 if (SH_MAX_H(win
) > 0 && ce
.height
> SH_MAX_H(win
))
2507 ce
.height
= SH_MAX_H(win
);
2510 /* resize increment. */
2512 if (SH_INC_W(win
) > 1 && ce
.width
> SH_INC_W(win
))
2513 ce
.width
-= (ce
.width
- SH_MIN_W(win
)) %
2515 if (SH_INC_H(win
) > 1 && ce
.height
> SH_INC_H(win
))
2516 ce
.height
-= (ce
.height
- SH_MIN_H(win
)) %
2520 /* adjust x and y for requested border_width. */
2521 ce
.x
+= BORDER(win
) - ev
->border_width
;
2522 ce
.y
+= BORDER(win
) - ev
->border_width
;
2523 ce
.border_width
= ev
->border_width
;
2524 ce
.above_sibling
= ev
->sibling
;
2527 DNPRINTF(SWM_D_MISC
, "config_win: ewmh: %s, window: 0x%x, (x,y) w x h: "
2528 "(%d,%d) %d x %d, border: %d\n", YESNO(ev
== NULL
), win
->id
, ce
.x
,
2529 ce
.y
, ce
.width
, ce
.height
, ce
.border_width
);
2531 xcb_send_event(conn
, 0, win
->id
, XCB_EVENT_MASK_STRUCTURE_NOTIFY
,
2536 count_win(struct workspace
*ws
, int count_transient
)
2541 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
2542 if (!count_transient
&& win
->floating
)
2544 if (!count_transient
&& win
->transient
)
2550 DNPRINTF(SWM_D_MISC
, "count_win: %d\n", count
);
2556 quit(struct swm_region
*r
, union arg
*args
)
2558 /* suppress unused warnings since vars are needed */
2562 DNPRINTF(SWM_D_MISC
, "quit\n");
2567 map_window(struct ws_win
*win
)
2569 uint32_t val
= XCB_STACK_MODE_ABOVE
;
2574 DNPRINTF(SWM_D_EVENT
, "map_window: win 0x%x, mapped: %s\n", win
->id
,
2575 YESNO(win
->mapped
));
2577 xcb_configure_window(conn
, win
->id
,
2578 XCB_CONFIG_WINDOW_STACK_MODE
, &val
);
2583 xcb_map_window(conn
, win
->id
);
2584 set_win_state(win
, XCB_ICCCM_WM_STATE_NORMAL
);
2589 unmap_window(struct ws_win
*win
)
2594 DNPRINTF(SWM_D_EVENT
, "unmap_window: win 0x%x, mapped: %s\n", win
->id
,
2595 YESNO(win
->mapped
));
2600 xcb_unmap_window(conn
, win
->id
);
2601 set_win_state(win
, XCB_ICCCM_WM_STATE_ICONIC
);
2609 int i
, j
, num_screens
;
2611 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2612 for (i
= 0; i
< num_screens
; i
++)
2613 for (j
= 0; j
< workspace_limit
; j
++)
2614 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2619 fake_keypress(struct ws_win
*win
, xcb_keysym_t keysym
, uint16_t modifiers
)
2621 xcb_key_press_event_t event
;
2622 xcb_keycode_t
*keycode
;
2627 keycode
= xcb_key_symbols_get_keycode(syms
, keysym
);
2629 DNPRINTF(SWM_D_MISC
, "fake_keypress: win 0x%x keycode %u\n",
2632 bzero(&event
, sizeof(event
));
2633 event
.event
= win
->id
;
2634 event
.root
= win
->s
->root
;
2635 event
.child
= XCB_WINDOW_NONE
;
2636 event
.time
= XCB_CURRENT_TIME
;
2637 event
.event_x
= X(win
);
2638 event
.event_y
= Y(win
);
2641 event
.same_screen
= 1;
2642 event
.detail
= *keycode
;
2643 event
.state
= modifiers
;
2645 event
.response_type
= XCB_KEY_PRESS
;
2646 xcb_send_event(conn
, 1, win
->id
,
2647 XCB_EVENT_MASK_KEY_PRESS
, (const char *)&event
);
2649 event
.response_type
= XCB_KEY_RELEASE
;
2650 xcb_send_event(conn
, 1, win
->id
,
2651 XCB_EVENT_MASK_KEY_RELEASE
, (const char *)&event
);
2657 restart(struct swm_region
*r
, union arg
*args
)
2659 /* suppress unused warning since var is needed */
2663 DNPRINTF(SWM_D_MISC
, "restart: %s\n", start_argv
[0]);
2667 execvp(start_argv
[0], start_argv
);
2668 warn("execvp failed");
2673 get_pointer_win(xcb_window_t root
)
2675 struct ws_win
*win
= NULL
;
2676 xcb_query_pointer_reply_t
*r
;
2678 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: root: 0x%x.\n", root
);
2680 r
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, root
), NULL
);
2682 win
= find_window(r
->child
);
2684 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: 0x%x.\n",
2687 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: none.\n");
2695 root_to_region(xcb_window_t root
, int check
)
2698 struct swm_region
*r
= NULL
;
2700 xcb_query_pointer_reply_t
*qpr
;
2701 xcb_get_input_focus_reply_t
*gifr
;
2703 DNPRINTF(SWM_D_MISC
, "root_to_region: window: 0x%x\n", root
);
2705 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2706 for (i
= 0; i
< num_screens
; i
++)
2707 if (screens
[i
].root
== root
)
2710 if (check
& SWM_CK_FOCUS
) {
2711 /* Try to find an actively focused window */
2712 gifr
= xcb_get_input_focus_reply(conn
,
2713 xcb_get_input_focus(conn
), NULL
);
2715 cfw
= find_window(gifr
->focus
);
2716 if (cfw
&& cfw
->ws
->r
)
2723 if (r
== NULL
&& check
& SWM_CK_POINTER
) {
2724 /* No region with an active focus; try to use pointer. */
2725 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
,
2726 screens
[i
].root
), NULL
);
2729 DNPRINTF(SWM_D_MISC
, "root_to_region: pointer: "
2730 "(%d,%d)\n", qpr
->root_x
, qpr
->root_y
);
2731 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2732 if (X(r
) <= qpr
->root_x
&&
2733 qpr
->root_x
< MAX_X(r
) &&
2734 Y(r
) <= qpr
->root_y
&&
2735 qpr
->root_y
< MAX_Y(r
))
2742 if (r
== NULL
&& check
& SWM_CK_FALLBACK
)
2743 r
= TAILQ_FIRST(&screens
[i
].rl
);
2749 find_unmanaged_window(xcb_window_t id
)
2752 int i
, j
, num_screens
;
2754 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2755 for (i
= 0; i
< num_screens
; i
++)
2756 for (j
= 0; j
< workspace_limit
; j
++)
2757 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].unmanagedlist
,
2765 find_window(xcb_window_t id
)
2768 int i
, j
, num_screens
;
2769 xcb_query_tree_reply_t
*r
;
2771 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2772 for (i
= 0; i
< num_screens
; i
++)
2773 for (j
= 0; j
< workspace_limit
; j
++)
2774 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2778 r
= xcb_query_tree_reply(conn
, xcb_query_tree(conn
, id
), NULL
);
2782 /* if we were looking for the parent return that window instead */
2783 if (r
->parent
== 0 || r
->root
== r
->parent
) {
2788 /* look for parent */
2789 for (i
= 0; i
< num_screens
; i
++)
2790 for (j
= 0; j
< workspace_limit
; j
++)
2791 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2792 if (r
->parent
== win
->id
) {
2802 spawn(int ws_idx
, union arg
*args
, int close_fd
)
2807 DNPRINTF(SWM_D_MISC
, "spawn: %s\n", args
->argv
[0]);
2809 close(xcb_get_file_descriptor(conn
));
2811 setenv("LD_PRELOAD", SWM_LIB
, 1);
2813 if (asprintf(&ret
, "%d", ws_idx
) == -1) {
2814 warn("spawn: asprintf SWM_WS");
2817 setenv("_SWM_WS", ret
, 1);
2821 if (asprintf(&ret
, "%d", getpid()) == -1) {
2822 warn("spawn: asprintf _SWM_PID");
2825 setenv("_SWM_PID", ret
, 1);
2829 if (setsid() == -1) {
2830 warn("spawn: setsid");
2836 * close stdin and stdout to prevent interaction between apps
2837 * and the baraction script
2838 * leave stderr open to record errors
2840 if ((fd
= open(_PATH_DEVNULL
, O_RDWR
, 0)) == -1) {
2841 warn("spawn: open");
2844 dup2(fd
, STDIN_FILENO
);
2845 dup2(fd
, STDOUT_FILENO
);
2850 execvp(args
->argv
[0], args
->argv
);
2852 warn("spawn: execvp");
2857 kill_refs(struct ws_win
*win
)
2859 int i
, x
, num_screens
;
2860 struct swm_region
*r
;
2861 struct workspace
*ws
;
2866 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2867 for (i
= 0; i
< num_screens
; i
++)
2868 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2869 for (x
= 0; x
< workspace_limit
; x
++) {
2871 if (win
== ws
->focus
)
2873 if (win
== ws
->focus_prev
)
2874 ws
->focus_prev
= NULL
;
2879 validate_win(struct ws_win
*testwin
)
2882 struct workspace
*ws
;
2883 struct swm_region
*r
;
2884 int i
, x
, num_screens
;
2886 if (testwin
== NULL
)
2889 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2890 for (i
= 0; i
< num_screens
; i
++)
2891 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2892 for (x
= 0; x
< workspace_limit
; x
++) {
2894 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
2902 validate_ws(struct workspace
*testws
)
2904 struct swm_region
*r
;
2905 struct workspace
*ws
;
2906 int i
, x
, num_screens
;
2908 /* validate all ws */
2909 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2910 for (i
= 0; i
< num_screens
; i
++)
2911 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2912 for (x
= 0; x
< workspace_limit
; x
++) {
2921 unfocus_win(struct ws_win
*win
)
2923 xcb_window_t none
= XCB_WINDOW_NONE
;
2925 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: window: 0x%x\n", WINID(win
));
2929 if (win
->ws
== NULL
) {
2930 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: NULL ws.\n");
2934 if (validate_ws(win
->ws
)) {
2935 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: invalid ws.\n");
2939 if (win
->ws
->r
== NULL
) {
2940 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: NULL region.\n");
2944 if (validate_win(win
)) {
2945 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: invalid win.\n");
2950 if (win
->ws
->focus
== win
) {
2951 win
->ws
->focus
= NULL
;
2952 win
->ws
->focus_prev
= win
;
2955 if (validate_win(win
->ws
->focus
)) {
2956 kill_refs(win
->ws
->focus
);
2957 win
->ws
->focus
= NULL
;
2959 if (validate_win(win
->ws
->focus_prev
)) {
2960 kill_refs(win
->ws
->focus_prev
);
2961 win
->ws
->focus_prev
= NULL
;
2964 xcb_change_window_attributes(conn
, win
->id
, XCB_CW_BORDER_PIXEL
,
2965 &win
->ws
->r
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
);
2967 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
2968 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1, &none
);
2970 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: done.\n");
2974 focus_win(struct ws_win
*win
)
2976 struct ws_win
*cfw
= NULL
, *parent
= NULL
, *w
;
2977 struct workspace
*ws
;
2978 xcb_get_input_focus_reply_t
*r
;
2980 DNPRINTF(SWM_D_FOCUS
, "focus_win: window: 0x%x\n", WINID(win
));
2985 if (win
->ws
== NULL
)
2990 if (validate_ws(ws
))
2993 if (validate_win(win
)) {
2998 r
= xcb_get_input_focus_reply(conn
, xcb_get_input_focus(conn
), NULL
);
3000 cfw
= find_window(r
->focus
);
3006 if (ws
->focus
!= win
) {
3007 if (ws
->focus
&& ws
->focus
!= cfw
)
3008 unfocus_win(ws
->focus
);
3012 /* If this window directs focus to a child window, then clear. */
3013 if (win
->focus_child
)
3014 win
->focus_child
= NULL
;
3016 /* If transient, adjust parent's focus child for focus_magic. */
3017 if (win
->transient
) {
3018 parent
= find_window(win
->transient
);
3019 if (parent
&& parent
->focus_child
!= win
)
3020 parent
->focus_child
= win
;
3023 if (cfw
!= win
&& ws
->r
!= NULL
) {
3024 /* Set input focus if no input hint, or indicated by hint. */
3025 if (!(win
->hints
.flags
& XCB_ICCCM_WM_HINT_INPUT
) ||
3026 (win
->hints
.flags
& XCB_ICCCM_WM_HINT_INPUT
&&
3028 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
,
3029 win
->id
, last_event_time
);
3031 /* Tell app it can adjust focus to a specific window. */
3032 if (win
->take_focus
) {
3033 /* java is special; always tell parent */
3034 if (win
->transient
&& win
->java
)
3035 client_msg(parent
, a_takefocus
,
3038 client_msg(win
, a_takefocus
, last_event_time
);
3041 xcb_change_window_attributes(conn
, win
->id
, XCB_CW_BORDER_PIXEL
,
3042 &ws
->r
->s
->c
[SWM_S_COLOR_FOCUS
].pixel
);
3044 if (ws
->cur_layout
->flags
& SWM_L_MAPONFOCUS
||
3046 /* If a parent exists, map it first. */
3050 /* Map siblings next. */
3051 TAILQ_FOREACH(w
, &ws
->winlist
, entry
)
3052 if (w
!= win
&& !w
->iconic
&&
3053 w
->transient
== parent
->id
)
3057 /* Map focused window. */
3060 /* Finally, map children of focus window. */
3061 TAILQ_FOREACH(w
, &ws
->winlist
, entry
)
3062 if (w
->transient
== win
->id
&& !w
->iconic
)
3067 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
3068 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1,
3075 DNPRINTF(SWM_D_FOCUS
, "focus_win: done.\n");
3078 /* If a child window should have focus instead, return it. */
3080 get_focus_magic(struct ws_win
*win
)
3082 struct ws_win
*parent
= NULL
;
3083 struct ws_win
*child
= NULL
;
3085 DNPRINTF(SWM_D_FOCUS
, "get_focus_magic: window: 0x%x\n", WINID(win
));
3089 if (win
->transient
) {
3090 parent
= find_window(win
->transient
);
3092 /* If parent prefers focus elsewhere, then try to do so. */
3093 if (parent
&& (child
= parent
->focus_child
)) {
3094 if (validate_win(child
) == 0 && child
->mapped
)
3097 parent
->focus_child
= NULL
;
3101 /* If this window prefers focus elsewhere, then try to do so. */
3102 if ((child
= win
->focus_child
)) {
3103 if (validate_win(child
) == 0 && child
->mapped
)
3106 win
->focus_child
= NULL
;
3113 event_drain(uint8_t rt
)
3115 xcb_generic_event_t
*evt
;
3117 /* ensure all pending requests have been processed before filtering. */
3119 while ((evt
= xcb_poll_for_event(conn
))) {
3120 if (XCB_EVENT_RESPONSE_TYPE(evt
) != rt
)
3128 switchws(struct swm_region
*r
, union arg
*args
)
3130 int wsid
= args
->id
, unmap_old
= 0;
3131 struct swm_region
*this_r
, *other_r
;
3133 struct workspace
*new_ws
, *old_ws
;
3138 if (wsid
>= workspace_limit
)
3142 old_ws
= this_r
->ws
;
3143 new_ws
= &this_r
->s
->ws
[wsid
];
3145 DNPRINTF(SWM_D_WS
, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
3146 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), old_ws
->idx
, wsid
);
3148 if (new_ws
== NULL
|| old_ws
== NULL
)
3150 if (new_ws
== old_ws
)
3153 unfocus_win(old_ws
->focus
);
3155 other_r
= new_ws
->r
;
3156 if (other_r
== NULL
) {
3157 /* the other workspace is hidden, hide this one */
3161 /* the other ws is visible in another region, exchange them */
3162 other_r
->ws_prior
= new_ws
;
3163 other_r
->ws
= old_ws
;
3164 old_ws
->r
= other_r
;
3166 this_r
->ws_prior
= old_ws
;
3167 this_r
->ws
= new_ws
;
3170 /* Set focus_pending before stacking. */
3171 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
3172 new_ws
->focus_pending
= get_region_focus(new_ws
->r
);
3176 /* unmap old windows */
3178 TAILQ_FOREACH(win
, &old_ws
->winlist
, entry
)
3181 /* if workspaces were swapped, then don't wait to set focus */
3182 if (old_ws
->r
&& focus_mode
!= SWM_FOCUS_FOLLOW
) {
3183 if (new_ws
->focus_pending
) {
3184 focus_win(new_ws
->focus_pending
);
3186 /* Empty region, focus on root. */
3187 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
,
3188 new_ws
->r
->s
[new_ws
->r
->s
->idx
].root
,
3193 /* Clear bar if new ws is empty. */
3194 if (new_ws
->focus_pending
== NULL
)
3199 DNPRINTF(SWM_D_WS
, "switchws: done.\n");
3203 cyclews(struct swm_region
*r
, union arg
*args
)
3206 struct swm_screen
*s
= r
->s
;
3209 DNPRINTF(SWM_D_WS
, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3210 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
3215 case SWM_ARG_ID_CYCLEWS_UP_ALL
:
3218 case SWM_ARG_ID_CYCLEWS_UP
:
3219 if (a
.id
< workspace_limit
- 1)
3224 case SWM_ARG_ID_CYCLEWS_DOWN_ALL
:
3227 case SWM_ARG_ID_CYCLEWS_DOWN
:
3231 a
.id
= workspace_limit
- 1;
3238 (!cycle_empty
&& TAILQ_EMPTY(&s
->ws
[a
.id
].winlist
)))
3240 if (!cycle_visible
&& s
->ws
[a
.id
].r
!= NULL
)
3244 } while (a
.id
!= r
->ws
->idx
);
3248 priorws(struct swm_region
*r
, union arg
*args
)
3254 DNPRINTF(SWM_D_WS
, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3255 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
3257 if (r
->ws_prior
== NULL
)
3260 a
.id
= r
->ws_prior
->idx
;
3265 cyclescr(struct swm_region
*r
, union arg
*args
)
3268 struct swm_region
*rr
= NULL
;
3271 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
3272 /* do nothing if we don't have more than one screen */
3273 if (!(num_screens
> 1 || outputs
> 1))
3278 case SWM_ARG_ID_CYCLESC_UP
:
3279 rr
= TAILQ_NEXT(r
, entry
);
3281 rr
= TAILQ_FIRST(&screens
[i
].rl
);
3283 case SWM_ARG_ID_CYCLESC_DOWN
:
3284 rr
= TAILQ_PREV(r
, swm_region_list
, entry
);
3286 rr
= TAILQ_LAST(&screens
[i
].rl
, swm_region_list
);
3294 nfw
= get_region_focus(rr
);
3298 /* New region is empty; unfocus old region and warp pointer. */
3299 unfocus_win(r
->ws
->focus
);
3300 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
,
3301 rr
->s
[i
].root
, XCB_CURRENT_TIME
);
3303 /* Clear bar since empty. */
3311 sort_windows(struct ws_win_list
*wl
)
3313 struct ws_win
*win
, *parent
, *nxt
;
3318 for (win
= TAILQ_FIRST(wl
); win
!= TAILQ_END(wl
); win
= nxt
) {
3319 nxt
= TAILQ_NEXT(win
, entry
);
3320 if (win
->transient
) {
3321 parent
= find_window(win
->transient
);
3322 if (parent
== NULL
) {
3323 warnx("not possible bug");
3326 TAILQ_REMOVE(wl
, win
, entry
);
3327 TAILQ_INSERT_AFTER(wl
, parent
, win
, entry
);
3334 swapwin(struct swm_region
*r
, union arg
*args
)
3336 struct ws_win
*target
, *source
;
3337 struct ws_win
*cur_focus
;
3338 struct ws_win_list
*wl
;
3340 DNPRINTF(SWM_D_WS
, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3341 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
3343 cur_focus
= r
->ws
->focus
;
3344 if (cur_focus
== NULL
)
3348 wl
= &source
->ws
->winlist
;
3351 case SWM_ARG_ID_SWAPPREV
:
3352 if (source
->transient
)
3353 source
= find_window(source
->transient
);
3354 target
= TAILQ_PREV(source
, ws_win_list
, entry
);
3355 if (target
&& target
->transient
)
3356 target
= find_window(target
->transient
);
3357 TAILQ_REMOVE(wl
, source
, entry
);
3359 TAILQ_INSERT_TAIL(wl
, source
, entry
);
3361 TAILQ_INSERT_BEFORE(target
, source
, entry
);
3363 case SWM_ARG_ID_SWAPNEXT
:
3364 target
= TAILQ_NEXT(source
, entry
);
3365 /* move the parent and let the sort handle the move */
3366 if (source
->transient
)
3367 source
= find_window(source
->transient
);
3368 TAILQ_REMOVE(wl
, source
, entry
);
3370 TAILQ_INSERT_HEAD(wl
, source
, entry
);
3372 TAILQ_INSERT_AFTER(wl
, target
, source
, entry
);
3374 case SWM_ARG_ID_SWAPMAIN
:
3375 target
= TAILQ_FIRST(wl
);
3376 if (target
== source
) {
3377 if (source
->ws
->focus_prev
!= NULL
&&
3378 source
->ws
->focus_prev
!= target
)
3379 source
= source
->ws
->focus_prev
;
3383 if (target
== NULL
|| source
== NULL
)
3385 source
->ws
->focus_prev
= target
;
3386 TAILQ_REMOVE(wl
, target
, entry
);
3387 TAILQ_INSERT_BEFORE(source
, target
, entry
);
3388 TAILQ_REMOVE(wl
, source
, entry
);
3389 TAILQ_INSERT_HEAD(wl
, source
, entry
);
3391 case SWM_ARG_ID_MOVELAST
:
3392 TAILQ_REMOVE(wl
, source
, entry
);
3393 TAILQ_INSERT_TAIL(wl
, source
, entry
);
3396 DNPRINTF(SWM_D_MOVE
, "swapwin: invalid id: %d\n", args
->id
);
3408 get_focus_prev(struct ws_win
*win
)
3410 struct ws_win
*winfocus
= NULL
;
3411 struct ws_win
*cur_focus
= NULL
;
3412 struct ws_win_list
*wl
= NULL
;
3413 struct workspace
*ws
= NULL
;
3415 if (!(win
&& win
->ws
))
3420 cur_focus
= ws
->focus
;
3422 DNPRINTF(SWM_D_FOCUS
, "get_focus_prev: window: 0x%x, cur_focus: 0x%x\n",
3423 WINID(win
), WINID(cur_focus
));
3425 /* pickle, just focus on whatever */
3426 if (cur_focus
== NULL
) {
3427 /* use prev_focus if valid */
3428 if (ws
->focus_prev
&& ws
->focus_prev
!= cur_focus
&&
3429 find_window(WINID(ws
->focus_prev
)))
3430 winfocus
= ws
->focus_prev
;
3434 /* if transient focus on parent */
3435 if (cur_focus
->transient
) {
3436 winfocus
= find_window(cur_focus
->transient
);
3440 /* if in max_stack try harder */
3441 if ((win
->quirks
& SWM_Q_FOCUSPREV
) ||
3442 (ws
->cur_layout
->flags
& SWM_L_FOCUSPREV
)) {
3443 if (cur_focus
!= ws
->focus_prev
)
3444 winfocus
= ws
->focus_prev
;
3446 winfocus
= TAILQ_PREV(win
, ws_win_list
, entry
);
3451 DNPRINTF(SWM_D_FOCUS
, "get_focus_prev: focus_close: %d\n", focus_close
);
3453 if (winfocus
== NULL
|| winfocus
== win
) {
3454 switch (focus_close
) {
3455 case SWM_STACK_BOTTOM
:
3456 winfocus
= TAILQ_FIRST(wl
);
3459 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
3461 case SWM_STACK_ABOVE
:
3462 if ((winfocus
= TAILQ_NEXT(cur_focus
, entry
)) == NULL
) {
3463 if (focus_close_wrap
)
3464 winfocus
= TAILQ_FIRST(wl
);
3466 winfocus
= TAILQ_PREV(cur_focus
,
3467 ws_win_list
, entry
);
3470 case SWM_STACK_BELOW
:
3471 if ((winfocus
= TAILQ_PREV(cur_focus
, ws_win_list
,
3473 if (focus_close_wrap
)
3474 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
3476 winfocus
= TAILQ_NEXT(cur_focus
, entry
);
3482 if (winfocus
== NULL
) {
3483 if (focus_default
== SWM_STACK_TOP
)
3484 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
3486 winfocus
= TAILQ_FIRST(wl
);
3491 return get_focus_magic(winfocus
);
3495 get_region_focus(struct swm_region
*r
)
3497 struct ws_win
*winfocus
= NULL
;
3502 if (r
->ws
->focus
&& !r
->ws
->focus
->iconic
)
3503 winfocus
= r
->ws
->focus
;
3504 else if (r
->ws
->focus_prev
&& !r
->ws
->focus_prev
->iconic
)
3505 winfocus
= r
->ws
->focus_prev
;
3507 TAILQ_FOREACH(winfocus
, &r
->ws
->winlist
, entry
)
3508 if (!winfocus
->iconic
)
3511 return get_focus_magic(winfocus
);
3515 focus(struct swm_region
*r
, union arg
*args
)
3517 struct ws_win
*head
, *cur_focus
= NULL
, *winfocus
= NULL
;
3518 struct ws_win_list
*wl
= NULL
;
3519 struct workspace
*ws
= NULL
;
3525 DNPRINTF(SWM_D_FOCUS
, "focus: id: %d\n", args
->id
);
3527 if ((cur_focus
= r
->ws
->focus
) == NULL
)
3531 if (TAILQ_EMPTY(wl
))
3533 /* make sure there is at least one uniconified window */
3535 TAILQ_FOREACH(winfocus
, wl
, entry
)
3536 if (!winfocus
->iconic
) {
3544 case SWM_ARG_ID_FOCUSPREV
:
3545 head
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
3547 head
= TAILQ_LAST(wl
, ws_win_list
);
3549 if (WINID(winfocus
) == cur_focus
->transient
) {
3550 head
= TAILQ_PREV(winfocus
, ws_win_list
, entry
);
3552 head
= TAILQ_LAST(wl
, ws_win_list
);
3557 if (winfocus
&& winfocus
->iconic
) {
3558 while (winfocus
!= cur_focus
) {
3559 if (winfocus
== NULL
)
3560 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
3561 if (!winfocus
->iconic
)
3563 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
,
3569 case SWM_ARG_ID_FOCUSNEXT
:
3570 head
= TAILQ_NEXT(cur_focus
, entry
);
3572 head
= TAILQ_FIRST(wl
);
3576 if (winfocus
&& winfocus
->iconic
) {
3577 while (winfocus
!= cur_focus
) {
3578 if (winfocus
== NULL
)
3579 winfocus
= TAILQ_FIRST(wl
);
3580 if (!winfocus
->iconic
)
3582 winfocus
= TAILQ_NEXT(winfocus
, entry
);
3587 case SWM_ARG_ID_FOCUSMAIN
:
3588 winfocus
= TAILQ_FIRST(wl
);
3589 if (winfocus
== cur_focus
)
3590 winfocus
= cur_focus
->ws
->focus_prev
;
3597 focus_win(get_focus_magic(winfocus
));
3603 cycle_layout(struct swm_region
*r
, union arg
*args
)
3605 struct workspace
*ws
= r
->ws
;
3607 /* suppress unused warning since var is needed */
3610 DNPRINTF(SWM_D_EVENT
, "cycle_layout: workspace: %d\n", ws
->idx
);
3613 if (ws
->cur_layout
->l_stack
== NULL
)
3614 ws
->cur_layout
= &layouts
[0];
3619 focus_win(get_region_focus(r
));
3625 stack_config(struct swm_region
*r
, union arg
*args
)
3627 struct workspace
*ws
= r
->ws
;
3629 DNPRINTF(SWM_D_STACK
, "stack_config: id: %d workspace: %d\n",
3632 if (ws
->cur_layout
->l_config
!= NULL
)
3633 ws
->cur_layout
->l_config(ws
, args
->id
);
3635 if (args
->id
!= SWM_ARG_ID_STACKINIT
)
3644 struct swm_geometry g
;
3645 struct swm_region
*r
;
3651 DNPRINTF(SWM_D_STACK
, "stack: begin\n");
3653 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
3654 for (i
= 0; i
< num_screens
; i
++) {
3658 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
3659 DNPRINTF(SWM_D_STACK
, "stack: workspace: %d "
3660 "(screen: %d, region: %d)\n", r
->ws
->idx
, i
, j
++);
3662 /* start with screen geometry, adjust for bar */
3664 g
.w
-= 2 * border_width
;
3665 g
.h
-= 2 * border_width
;
3671 r
->ws
->cur_layout
->l_stack(r
->ws
, &g
);
3672 r
->ws
->cur_layout
->l_string(r
->ws
);
3673 /* save r so we can track region changes */
3680 DNPRINTF(SWM_D_STACK
, "stack: end\n");
3684 store_float_geom(struct ws_win
*win
, struct swm_region
*r
)
3686 if (win
== NULL
|| r
== NULL
)
3689 /* retain window geom and region geom */
3690 win
->g_float
= win
->g
;
3691 win
->g_float
.x
-= X(r
);
3692 win
->g_float
.y
-= Y(r
);
3693 win
->g_floatvalid
= 1;
3694 DNPRINTF(SWM_D_MISC
, "store_float_geom: window: 0x%x, g: (%d,%d)"
3695 " %d x %d, g_float: (%d,%d) %d x %d\n", win
->id
, X(win
), Y(win
),
3696 WIDTH(win
), HEIGHT(win
), win
->g_float
.x
, win
->g_float
.y
,
3697 win
->g_float
.w
, win
->g_float
.h
);
3701 load_float_geom(struct ws_win
*win
, struct swm_region
*r
)
3703 if (win
== NULL
|| r
== NULL
)
3706 if (win
->g_floatvalid
) {
3707 win
->g
= win
->g_float
;
3710 DNPRINTF(SWM_D_MISC
, "load_float_geom: window: 0x%x, g: (%d,%d)"
3711 "%d x %d\n", win
->id
, X(win
), Y(win
), WIDTH(win
),
3714 DNPRINTF(SWM_D_MISC
, "load_float_geom: window: 0x%x, g_float "
3715 "is not set.\n", win
->id
);
3720 stack_floater(struct ws_win
*win
, struct swm_region
*r
)
3725 DNPRINTF(SWM_D_MISC
, "stack_floater: window: 0x%x\n", win
->id
);
3728 * to allow windows to change their size (e.g. mplayer fs) only retrieve
3729 * geom on ws switches or return from max mode
3731 if (win
->floatmaxed
|| (r
!= r
->ws
->old_r
&&
3732 !(win
->ewmh_flags
& EWMH_F_FULLSCREEN
))) {
3733 /* update geometry for the new region */
3734 load_float_geom(win
, r
);
3737 win
->floatmaxed
= 0;
3740 * if set to fullscreen mode, configure window to maximum size.
3742 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
3743 if (!win
->g_floatvalid
)
3744 store_float_geom(win
, win
->ws
->r
);
3750 * remove border on fullscreen floater when in fullscreen mode or when
3751 * the quirk is present.
3753 if ((win
->ewmh_flags
& EWMH_F_FULLSCREEN
) ||
3754 ((win
->quirks
& SWM_Q_FULLSCREEN
) &&
3755 (WIDTH(win
) >= WIDTH(r
)) && (HEIGHT(win
) >= HEIGHT(r
)))) {
3756 if (win
->bordered
) {
3758 X(win
) += border_width
;
3759 Y(win
) += border_width
;
3761 } else if (!win
->bordered
) {
3763 X(win
) -= border_width
;
3764 Y(win
) -= border_width
;
3767 if (win
->transient
&& (win
->quirks
& SWM_Q_TRANSSZ
)) {
3768 WIDTH(win
) = (double)WIDTH(r
) * dialog_ratio
;
3769 HEIGHT(win
) = (double)HEIGHT(r
) * dialog_ratio
;
3772 if (!win
->manual
&& !(win
->ewmh_flags
& EWMH_F_FULLSCREEN
) &&
3773 !(win
->quirks
& SWM_Q_ANYWHERE
)) {
3775 * floaters and transients are auto-centred unless moved,
3776 * resized or ANYWHERE quirk is set.
3778 X(win
) = X(r
) + (WIDTH(r
) - WIDTH(win
)) / 2 - BORDER(win
);
3779 Y(win
) = Y(r
) + (HEIGHT(r
) - HEIGHT(win
)) / 2 - BORDER(win
);
3781 store_float_geom(win
, r
);
3784 /* keep window within region bounds */
3785 constrain_window(win
, r
, 0);
3791 * Send keystrokes to terminal to decrease/increase the font size as the
3792 * window size changes.
3795 adjust_font(struct ws_win
*win
)
3797 if (!(win
->quirks
& SWM_Q_XTERM_FONTADJ
) ||
3798 win
->floating
|| win
->transient
)
3801 if (win
->sh
.width_inc
&& win
->last_inc
!= win
->sh
.width_inc
&&
3802 WIDTH(win
) / win
->sh
.width_inc
< term_width
&&
3803 win
->font_steps
< SWM_MAX_FONT_STEPS
) {
3804 win
->font_size_boundary
[win
->font_steps
] =
3805 (win
->sh
.width_inc
* term_width
) + win
->sh
.base_width
;
3808 win
->last_inc
= win
->sh
.width_inc
;
3809 fake_keypress(win
, XK_KP_Subtract
, XCB_MOD_MASK_SHIFT
);
3810 } else if (win
->font_steps
&& win
->last_inc
!= win
->sh
.width_inc
&&
3811 WIDTH(win
) > win
->font_size_boundary
[win
->font_steps
- 1]) {
3814 win
->last_inc
= win
->sh
.width_inc
;
3815 fake_keypress(win
, XK_KP_Add
, XCB_MOD_MASK_SHIFT
);
3819 #define SWAPXY(g) do { \
3821 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
3822 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
3825 stack_master(struct workspace
*ws
, struct swm_geometry
*g
, int rot
, int flip
)
3827 struct swm_geometry win_g
, r_g
= *g
;
3828 struct ws_win
*win
, *fs_win
= NULL
;
3829 int i
, j
, s
, stacks
;
3830 int w_inc
= 1, h_inc
, w_base
= 1, h_base
;
3831 int hrh
, extra
= 0, h_slice
, last_h
= 0;
3832 int split
, colno
, winno
, mwin
, msize
, mscale
;
3833 int remain
, missing
, v_slice
, reconfigure
= 0;
3836 DNPRINTF(SWM_D_STACK
, "stack_master: workspace: %d, rot: %s, "
3837 "flip: %s\n", ws
->idx
, YESNO(rot
), YESNO(flip
));
3839 winno
= count_win(ws
, 0);
3840 if (winno
== 0 && count_win(ws
, 1) == 0)
3843 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
3844 if (!win
->transient
&& !win
->floating
&& !win
->iconic
)
3851 w_inc
= win
->sh
.width_inc
;
3852 w_base
= win
->sh
.base_width
;
3853 mwin
= ws
->l_state
.horizontal_mwin
;
3854 mscale
= ws
->l_state
.horizontal_msize
;
3855 stacks
= ws
->l_state
.horizontal_stacks
;
3858 w_inc
= win
->sh
.height_inc
;
3859 w_base
= win
->sh
.base_height
;
3860 mwin
= ws
->l_state
.vertical_mwin
;
3861 mscale
= ws
->l_state
.vertical_msize
;
3862 stacks
= ws
->l_state
.vertical_stacks
;
3866 if (stacks
> winno
- mwin
)
3867 stacks
= winno
- mwin
;
3871 h_slice
= r_g
.h
/ SWM_H_SLICE
;
3872 if (mwin
&& winno
> mwin
) {
3873 v_slice
= r_g
.w
/ SWM_V_SLICE
;
3877 win_g
.w
= v_slice
* mscale
;
3879 if (w_inc
> 1 && w_inc
< v_slice
) {
3880 /* adjust for window's requested size increment */
3881 remain
= (win_g
.w
- w_base
) % w_inc
;
3887 win_g
.x
+= r_g
.w
- msize
;
3890 colno
= split
= winno
/ stacks
;
3891 win_g
.w
= ((r_g
.w
- (stacks
* 2 * border_width
) +
3892 2 * border_width
) / stacks
);
3894 hrh
= r_g
.h
/ colno
;
3895 extra
= r_g
.h
- (colno
* hrh
);
3896 win_g
.h
= hrh
- 2 * border_width
;
3898 /* stack all the tiled windows */
3899 i
= j
= 0, s
= stacks
;
3900 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3901 if (win
->transient
|| win
->floating
)
3906 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
3911 if (split
&& i
== split
) {
3912 colno
= (winno
- mwin
) / stacks
;
3913 if (s
<= (winno
- mwin
) % stacks
)
3915 split
= split
+ colno
;
3916 hrh
= (r_g
.h
/ colno
);
3917 extra
= r_g
.h
- (colno
* hrh
);
3921 win_g
.x
+= win_g
.w
+ 2 * border_width
;
3922 win_g
.w
= (r_g
.w
- msize
-
3923 (stacks
* 2 * border_width
)) / stacks
;
3925 win_g
.w
+= (r_g
.w
- msize
-
3926 (stacks
* 2 * border_width
)) % stacks
;
3930 win_g
.h
= hrh
- 2 * border_width
;
3932 h_inc
= win
->sh
.width_inc
;
3933 h_base
= win
->sh
.base_width
;
3935 h_inc
= win
->sh
.height_inc
;
3936 h_base
= win
->sh
.base_height
;
3938 if (j
== colno
- 1) {
3939 win_g
.h
= hrh
+ extra
;
3940 } else if (h_inc
> 1 && h_inc
< h_slice
) {
3941 /* adjust for window's requested size increment */
3942 remain
= (win_g
.h
- h_base
) % h_inc
;
3943 missing
= h_inc
- remain
;
3945 if (missing
<= extra
|| j
== 0) {
3957 win_g
.y
+= last_h
+ 2 * border_width
;
3959 if (disable_border
&& !bar_enabled
&& winno
== 1){
3961 win_g
.w
+= 2 * border_width
;
3962 win_g
.h
+= 2 * border_width
;
3967 if (X(win
) != win_g
.y
|| Y(win
) != win_g
.x
||
3968 WIDTH(win
) != win_g
.h
|| HEIGHT(win
) != win_g
.w
) {
3972 WIDTH(win
) = win_g
.h
;
3973 HEIGHT(win
) = win_g
.w
;
3976 if (X(win
) != win_g
.x
|| Y(win
) != win_g
.y
||
3977 WIDTH(win
) != win_g
.w
|| HEIGHT(win
) != win_g
.h
) {
3981 WIDTH(win
) = win_g
.w
;
3982 HEIGHT(win
) = win_g
.h
;
3986 if (bordered
!= win
->bordered
) {
3988 win
->bordered
= bordered
;
4004 /* now, stack all the floaters and transients */
4005 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
4006 if (!win
->transient
&& !win
->floating
)
4010 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
4015 stack_floater(win
, ws
->r
);
4019 /* Make sure fs_win is stacked last so it's on top. */
4021 stack_floater(fs_win
, ws
->r
);
4027 vertical_config(struct workspace
*ws
, int id
)
4029 DNPRINTF(SWM_D_STACK
, "vertical_config: id: %d, workspace: %d\n",
4033 case SWM_ARG_ID_STACKRESET
:
4034 case SWM_ARG_ID_STACKINIT
:
4035 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ 2;
4036 ws
->l_state
.vertical_mwin
= 1;
4037 ws
->l_state
.vertical_stacks
= 1;
4039 case SWM_ARG_ID_MASTERSHRINK
:
4040 if (ws
->l_state
.vertical_msize
> 1)
4041 ws
->l_state
.vertical_msize
--;
4043 case SWM_ARG_ID_MASTERGROW
:
4044 if (ws
->l_state
.vertical_msize
< SWM_V_SLICE
- 1)
4045 ws
->l_state
.vertical_msize
++;
4047 case SWM_ARG_ID_MASTERADD
:
4048 ws
->l_state
.vertical_mwin
++;
4050 case SWM_ARG_ID_MASTERDEL
:
4051 if (ws
->l_state
.vertical_mwin
> 0)
4052 ws
->l_state
.vertical_mwin
--;
4054 case SWM_ARG_ID_STACKINC
:
4055 ws
->l_state
.vertical_stacks
++;
4057 case SWM_ARG_ID_STACKDEC
:
4058 if (ws
->l_state
.vertical_stacks
> 1)
4059 ws
->l_state
.vertical_stacks
--;
4061 case SWM_ARG_ID_FLIPLAYOUT
:
4062 ws
->l_state
.vertical_flip
= !ws
->l_state
.vertical_flip
;
4070 vertical_stack(struct workspace
*ws
, struct swm_geometry
*g
)
4072 DNPRINTF(SWM_D_STACK
, "vertical_stack: workspace: %d\n", ws
->idx
);
4074 stack_master(ws
, g
, 0, ws
->l_state
.vertical_flip
);
4078 horizontal_config(struct workspace
*ws
, int id
)
4080 DNPRINTF(SWM_D_STACK
, "horizontal_config: workspace: %d\n", ws
->idx
);
4083 case SWM_ARG_ID_STACKRESET
:
4084 case SWM_ARG_ID_STACKINIT
:
4085 ws
->l_state
.horizontal_mwin
= 1;
4086 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ 2;
4087 ws
->l_state
.horizontal_stacks
= 1;
4089 case SWM_ARG_ID_MASTERSHRINK
:
4090 if (ws
->l_state
.horizontal_msize
> 1)
4091 ws
->l_state
.horizontal_msize
--;
4093 case SWM_ARG_ID_MASTERGROW
:
4094 if (ws
->l_state
.horizontal_msize
< SWM_H_SLICE
- 1)
4095 ws
->l_state
.horizontal_msize
++;
4097 case SWM_ARG_ID_MASTERADD
:
4098 ws
->l_state
.horizontal_mwin
++;
4100 case SWM_ARG_ID_MASTERDEL
:
4101 if (ws
->l_state
.horizontal_mwin
> 0)
4102 ws
->l_state
.horizontal_mwin
--;
4104 case SWM_ARG_ID_STACKINC
:
4105 ws
->l_state
.horizontal_stacks
++;
4107 case SWM_ARG_ID_STACKDEC
:
4108 if (ws
->l_state
.horizontal_stacks
> 1)
4109 ws
->l_state
.horizontal_stacks
--;
4111 case SWM_ARG_ID_FLIPLAYOUT
:
4112 ws
->l_state
.horizontal_flip
= !ws
->l_state
.horizontal_flip
;
4120 horizontal_stack(struct workspace
*ws
, struct swm_geometry
*g
)
4122 DNPRINTF(SWM_D_STACK
, "horizontal_stack: workspace: %d\n", ws
->idx
);
4124 stack_master(ws
, g
, 1, ws
->l_state
.horizontal_flip
);
4127 /* fullscreen view */
4129 max_stack(struct workspace
*ws
, struct swm_geometry
*g
)
4131 struct swm_geometry gg
= *g
;
4132 struct ws_win
*w
, *win
= NULL
, *parent
= NULL
;
4133 int winno
, num_screens
;
4135 DNPRINTF(SWM_D_STACK
, "max_stack: workspace: %d\n", ws
->idx
);
4140 winno
= count_win(ws
, 0);
4141 if (winno
== 0 && count_win(ws
, 1) == 0)
4144 /* Figure out which top level window should be visible. */
4145 if (ws
->focus_pending
)
4146 win
= ws
->focus_pending
;
4150 win
= TAILQ_FIRST(&ws
->winlist
);
4153 parent
= find_window(win
->transient
);
4155 DNPRINTF(SWM_D_STACK
, "max_stack: win: 0x%x\n", win
->id
);
4157 /* maximize all top level windows */
4158 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
4159 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
4160 if (w
->transient
|| w
->iconic
)
4163 if (w
->floating
&& !w
->floatmaxed
) {
4165 * retain geometry for retrieval on exit from
4168 store_float_geom(w
, ws
->r
);
4172 /* only reconfigure if necessary */
4173 if (X(w
) != gg
.x
|| Y(w
) != gg
.y
|| WIDTH(w
) != gg
.w
||
4174 HEIGHT(w
) != gg
.h
) {
4180 WIDTH(w
) += 2 * border_width
;
4181 HEIGHT(w
) += 2 * border_width
;
4187 /* Unmap unwanted windows if not multi-screen. */
4188 if (!(num_screens
> 1 || outputs
> 1) && (w
!= win
||
4189 w
!= parent
|| w
->transient
!= win
->id
))
4193 /* If a parent exists, map it first. */
4197 /* Map siblings next. */
4198 TAILQ_FOREACH(w
, &ws
->winlist
, entry
)
4199 if (w
!= win
&& !w
->iconic
&&
4200 w
->transient
== parent
->id
) {
4201 stack_floater(w
, ws
->r
);
4206 /* Map focused window. */
4209 /* Finally, map children of focus window. */
4210 TAILQ_FOREACH(w
, &ws
->winlist
, entry
)
4211 if (w
->transient
== win
->id
&& !w
->iconic
) {
4212 stack_floater(w
, ws
->r
);
4218 send_to_ws(struct swm_region
*r
, union arg
*args
)
4220 int wsid
= args
->id
;
4221 struct ws_win
*win
= NULL
, *parent
;
4222 struct workspace
*ws
, *nws
, *pws
;
4223 char ws_idx_str
[SWM_PROPLEN
];
4225 if (wsid
>= workspace_limit
)
4228 if (r
&& r
->ws
&& r
->ws
->focus
)
4233 if (win
->ws
->idx
== wsid
)
4236 DNPRINTF(SWM_D_MOVE
, "send_to_ws: win 0x%x, ws %d -> %d\n", win
->id
,
4237 win
->ws
->idx
, wsid
);
4240 nws
= &win
->s
->ws
[wsid
];
4242 /* Update the window's workspace property: _SWM_WS */
4243 if (snprintf(ws_idx_str
, SWM_PROPLEN
, "%d", nws
->idx
) < SWM_PROPLEN
) {
4244 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
4245 ws
->focus_pending
= get_focus_prev(win
);
4247 /* Move the parent if this is a transient window. */
4248 if (win
->transient
) {
4249 parent
= find_window(win
->transient
);
4252 /* Set new focus in parent's ws if needed. */
4253 if (pws
->focus
== parent
) {
4254 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
4255 pws
->focus_pending
=
4256 get_focus_prev(parent
);
4258 unfocus_win(parent
);
4260 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
4261 pws
->focus
= pws
->focus_pending
;
4263 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
4264 pws
->focus_pending
= NULL
;
4267 /* Don't unmap parent if new ws is visible */
4269 unmap_window(parent
);
4272 TAILQ_REMOVE(&ws
->winlist
, parent
, entry
);
4273 TAILQ_INSERT_TAIL(&nws
->winlist
, parent
, entry
);
4276 DNPRINTF(SWM_D_PROP
, "send_to_ws: set "
4277 "property: _SWM_WS: %s\n", ws_idx_str
);
4278 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
4279 parent
->id
, a_swm_ws
, XCB_ATOM_STRING
, 8,
4280 strlen(ws_idx_str
), ws_idx_str
);
4286 /* Don't unmap if new ws is visible */
4291 TAILQ_REMOVE(&ws
->winlist
, win
, entry
);
4292 TAILQ_INSERT_TAIL(&nws
->winlist
, win
, entry
);
4295 /* Set focus on new ws. */
4296 unfocus_win(nws
->focus
);
4299 DNPRINTF(SWM_D_PROP
, "send_to_ws: set property: _SWM_WS: %s\n",
4301 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
4302 a_swm_ws
, XCB_ATOM_STRING
, 8, strlen(ws_idx_str
),
4305 /* Restack and set new focus. */
4308 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
4309 focus_win(ws
->focus_pending
);
4310 ws
->focus_pending
= NULL
;
4316 DNPRINTF(SWM_D_MOVE
, "send_to_ws: done.\n");
4320 pressbutton(struct swm_region
*r
, union arg
*args
)
4322 /* suppress unused warning since var is needed */
4325 xcb_test_fake_input(conn
, XCB_BUTTON_PRESS
, args
->id
,
4326 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
4327 xcb_test_fake_input(conn
, XCB_BUTTON_RELEASE
, args
->id
,
4328 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
4332 raise_toggle(struct swm_region
*r
, union arg
*args
)
4334 /* suppress unused warning since var is needed */
4337 if (r
== NULL
|| r
->ws
== NULL
)
4340 r
->ws
->always_raise
= !r
->ws
->always_raise
;
4342 /* bring floaters back to top */
4343 if (!r
->ws
->always_raise
)
4350 iconify(struct swm_region
*r
, union arg
*args
)
4352 /* suppress unused warning since var is needed */
4355 if (r
->ws
->focus
== NULL
)
4358 set_swm_iconic(r
->ws
->focus
, 1);
4364 get_win_name(xcb_window_t win
)
4367 xcb_get_property_cookie_t c
;
4368 xcb_get_property_reply_t
*r
;
4370 /* First try _NET_WM_NAME for UTF-8. */
4371 c
= xcb_get_property(conn
, 0, win
, a_netwmname
,
4372 XCB_GET_PROPERTY_TYPE_ANY
, 0, UINT_MAX
);
4373 r
= xcb_get_property_reply(conn
, c
, NULL
);
4375 if (!r
|| r
->type
== XCB_NONE
) {
4377 /* Use WM_NAME instead; no UTF-8. */
4378 c
= xcb_get_property(conn
, 0, win
, XCB_ATOM_WM_NAME
,
4379 XCB_GET_PROPERTY_TYPE_ANY
, 0, UINT_MAX
);
4380 r
= xcb_get_property_reply(conn
, c
, NULL
);
4382 if(!r
|| r
->type
== XCB_NONE
) {
4389 name
= strndup(xcb_get_property_value(r
),
4390 xcb_get_property_value_length(r
));
4397 uniconify(struct swm_region
*r
, union arg
*args
)
4404 DNPRINTF(SWM_D_MISC
, "uniconify\n");
4406 if (r
== NULL
|| r
->ws
== NULL
)
4409 /* make sure we have anything to uniconify */
4410 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
4411 if (win
->ws
== NULL
)
4412 continue; /* should never happen */
4421 search_resp_action
= SWM_SEARCH_UNICONIFY
;
4423 spawn_select(r
, args
, "search", &searchpid
);
4425 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
4428 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
4429 if (win
->ws
== NULL
)
4430 continue; /* should never happen */
4434 name
= get_win_name(win
->id
);
4437 fprintf(lfile
, "%s.%u\n", name
, win
->id
);
4445 name_workspace(struct swm_region
*r
, union arg
*args
)
4449 DNPRINTF(SWM_D_MISC
, "name_workspace\n");
4455 search_resp_action
= SWM_SEARCH_NAME_WORKSPACE
;
4457 spawn_select(r
, args
, "name_workspace", &searchpid
);
4459 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
4462 fprintf(lfile
, "%s", "");
4467 search_workspace(struct swm_region
*r
, union arg
*args
)
4470 struct workspace
*ws
;
4473 DNPRINTF(SWM_D_MISC
, "search_workspace\n");
4479 search_resp_action
= SWM_SEARCH_SEARCH_WORKSPACE
;
4481 spawn_select(r
, args
, "search", &searchpid
);
4483 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
4486 for (i
= 0; i
< workspace_limit
; i
++) {
4490 fprintf(lfile
, "%d%s%s\n", ws
->idx
+ 1,
4491 (ws
->name
? ":" : ""), (ws
->name
? ws
->name
: ""));
4498 search_win_cleanup(void)
4500 struct search_window
*sw
= NULL
;
4502 while ((sw
= TAILQ_FIRST(&search_wl
)) != NULL
) {
4503 xcb_destroy_window(conn
, sw
->indicator
);
4504 TAILQ_REMOVE(&search_wl
, sw
, entry
);
4510 search_win(struct swm_region
*r
, union arg
*args
)
4512 struct ws_win
*win
= NULL
;
4513 struct search_window
*sw
= NULL
;
4516 int i
, width
, height
;
4524 XRectangle l_ibox
, l_lbox
;
4526 DNPRINTF(SWM_D_MISC
, "search_win\n");
4529 search_resp_action
= SWM_SEARCH_SEARCH_WINDOW
;
4531 spawn_select(r
, args
, "search", &searchpid
);
4533 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
4536 TAILQ_INIT(&search_wl
);
4539 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
4543 sw
= calloc(1, sizeof(struct search_window
));
4545 warn("search_win: calloc");
4547 search_win_cleanup();
4553 snprintf(s
, sizeof s
, "%d", i
);
4556 w
= xcb_generate_id(conn
);
4557 wa
[0] = r
->s
->c
[SWM_S_COLOR_FOCUS
].pixel
;
4558 wa
[1] = r
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
;
4560 if (bar_font_legacy
) {
4561 XmbTextExtents(bar_fs
, s
, len
, &l_ibox
, &l_lbox
);
4562 width
= l_lbox
.width
+ 4;
4563 height
= bar_fs_extents
->max_logical_extent
.height
+ 4;
4565 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
,
4567 width
= info
.width
+ 4;
4568 height
= bar_font
->height
+ 4;
4571 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, w
, win
->id
, 0, 0,
4572 width
, height
, 1, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
4573 XCB_COPY_FROM_PARENT
, XCB_CW_BACK_PIXEL
|
4574 XCB_CW_BORDER_PIXEL
, wa
);
4576 xcb_map_window(conn
, w
);
4579 TAILQ_INSERT_TAIL(&search_wl
, sw
, entry
);
4581 if (bar_font_legacy
) {
4582 l_gcv
.graphics_exposures
= 0;
4583 l_draw
= XCreateGC(display
, w
, 0, &l_gcv
);
4585 XSetForeground(display
, l_draw
,
4586 r
->s
->c
[SWM_S_COLOR_BAR
].pixel
);
4588 DRAWSTRING(display
, w
, bar_fs
, l_draw
, 2,
4589 (bar_fs_extents
->max_logical_extent
.height
-
4590 l_lbox
.height
) / 2 - l_lbox
.y
, s
, len
);
4592 XFreeGC(display
, l_draw
);
4595 draw
= XftDrawCreate(display
, w
,
4596 DefaultVisual(display
, r
->s
->idx
),
4597 DefaultColormap(display
, r
->s
->idx
));
4599 XftDrawStringUtf8(draw
, &bar_font_color
, bar_font
, 2,
4600 (HEIGHT(r
->bar
) + bar_font
->height
) / 2 -
4601 bar_font
->descent
, (FcChar8
*)s
, len
);
4603 XftDrawDestroy(draw
);
4606 DNPRINTF(SWM_D_MISC
, "search_win: mapped window: 0x%x\n", w
);
4608 fprintf(lfile
, "%d\n", i
);
4618 search_resp_uniconify(const char *resp
, unsigned long len
)
4624 DNPRINTF(SWM_D_MISC
, "search_resp_uniconify: resp: %s\n", resp
);
4626 TAILQ_FOREACH(win
, &search_r
->ws
->winlist
, entry
) {
4629 name
= get_win_name(win
->id
);
4632 if (asprintf(&s
, "%s.%u", name
, win
->id
) == -1) {
4637 if (strncmp(s
, resp
, len
) == 0) {
4638 /* XXX this should be a callback to generalize */
4639 set_swm_iconic(win
, 0);
4648 search_resp_name_workspace(const char *resp
, unsigned long len
)
4650 struct workspace
*ws
;
4652 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: resp: %s\n", resp
);
4654 if (search_r
->ws
== NULL
)
4659 free(search_r
->ws
->name
);
4660 search_r
->ws
->name
= NULL
;
4664 ws
->name
= strdup(resp
);
4665 if (ws
->name
== NULL
) {
4666 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: "
4667 "strdup: %s", strerror(errno
));
4674 search_resp_search_workspace(const char *resp
)
4681 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: resp: %s\n", resp
);
4685 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: strdup: %s",
4692 ws_idx
= (int)strtonum(q
, 1, workspace_limit
, &errstr
);
4694 DNPRINTF(SWM_D_MISC
, "workspace idx is %s: %s",
4701 switchws(search_r
, &a
);
4705 search_resp_search_window(const char *resp
)
4710 struct search_window
*sw
;
4712 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: resp: %s\n", resp
);
4716 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: strdup: %s",
4721 idx
= (int)strtonum(s
, 1, INT_MAX
, &errstr
);
4723 DNPRINTF(SWM_D_MISC
, "window idx is %s: %s",
4730 TAILQ_FOREACH(sw
, &search_wl
, entry
)
4731 if (idx
== sw
->idx
) {
4737 #define MAX_RESP_LEN 1024
4740 search_do_resp(void)
4746 DNPRINTF(SWM_D_MISC
, "search_do_resp:\n");
4751 if ((resp
= calloc(1, MAX_RESP_LEN
+ 1)) == NULL
) {
4752 warn("search: calloc");
4756 rbytes
= read(select_resp_pipe
[0], resp
, MAX_RESP_LEN
);
4758 warn("search: read error");
4761 resp
[rbytes
] = '\0';
4764 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
4765 * newline, so work around that by sanitizing the resp now.
4767 resp
[strcspn(resp
, "\n")] = '\0';
4770 switch (search_resp_action
) {
4771 case SWM_SEARCH_UNICONIFY
:
4772 search_resp_uniconify(resp
, len
);
4774 case SWM_SEARCH_NAME_WORKSPACE
:
4775 search_resp_name_workspace(resp
, len
);
4777 case SWM_SEARCH_SEARCH_WORKSPACE
:
4778 search_resp_search_workspace(resp
);
4780 case SWM_SEARCH_SEARCH_WINDOW
:
4781 search_resp_search_window(resp
);
4786 if (search_resp_action
== SWM_SEARCH_SEARCH_WINDOW
)
4787 search_win_cleanup();
4789 search_resp_action
= SWM_SEARCH_NONE
;
4790 close(select_resp_pipe
[0]);
4797 wkill(struct swm_region
*r
, union arg
*args
)
4799 DNPRINTF(SWM_D_MISC
, "wkill: id: %d\n", args
->id
);
4801 if (r
->ws
->focus
== NULL
)
4804 if (args
->id
== SWM_ARG_ID_KILLWINDOW
)
4805 xcb_kill_client(conn
, r
->ws
->focus
->id
);
4807 if (r
->ws
->focus
->can_delete
)
4808 client_msg(r
->ws
->focus
, a_delete
, 0);
4814 floating_toggle_win(struct ws_win
*win
)
4816 struct swm_region
*r
;
4826 /* reject floating toggles in max stack mode */
4827 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
4830 if (win
->floating
) {
4831 if (!win
->floatmaxed
) {
4832 /* retain position for refloat */
4833 store_float_geom(win
, r
);
4837 load_float_geom(win
, r
);
4841 ewmh_update_actions(win
);
4847 floating_toggle(struct swm_region
*r
, union arg
*args
)
4849 struct ws_win
*win
= r
->ws
->focus
;
4851 /* suppress unused warning since var is needed */
4857 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
4860 ewmh_update_win_state(win
, ewmh
[_NET_WM_STATE_ABOVE
].atom
,
4861 _NET_WM_STATE_TOGGLE
);
4865 if (win
== win
->ws
->focus
)
4872 constrain_window(struct ws_win
*win
, struct swm_region
*r
, int resizable
)
4874 if (MAX_X(win
) + BORDER(win
) > MAX_X(r
)) {
4876 WIDTH(win
) = MAX_X(r
) - X(win
) - BORDER(win
);
4878 X(win
) = MAX_X(r
)- WIDTH(win
) - BORDER(win
);
4881 if (X(win
) + BORDER(win
) < X(r
)) {
4883 WIDTH(win
) -= X(r
) - X(win
) - BORDER(win
);
4885 X(win
) = X(r
) - BORDER(win
);
4888 if (MAX_Y(win
) + BORDER(win
) > MAX_Y(r
)) {
4890 HEIGHT(win
) = MAX_Y(r
) - Y(win
) - BORDER(win
);
4892 Y(win
) = MAX_Y(r
) - HEIGHT(win
) - BORDER(win
);
4895 if (Y(win
) + BORDER(win
) < Y(r
)) {
4897 HEIGHT(win
) -= Y(r
) - Y(win
) - BORDER(win
);
4899 Y(win
) = Y(r
) - BORDER(win
);
4905 if (HEIGHT(win
) < 1)
4911 update_window(struct ws_win
*win
)
4916 mask
= XCB_CONFIG_WINDOW_X
| XCB_CONFIG_WINDOW_Y
|
4917 XCB_CONFIG_WINDOW_WIDTH
| XCB_CONFIG_WINDOW_HEIGHT
|
4918 XCB_CONFIG_WINDOW_BORDER_WIDTH
;
4922 wc
[3] = HEIGHT(win
);
4923 wc
[4] = BORDER(win
);
4925 DNPRINTF(SWM_D_EVENT
, "update_window: window: 0x%x, (x,y) w x h: "
4926 "(%d,%d) %d x %d, bordered: %s\n", win
->id
, wc
[0], wc
[1], wc
[2],
4927 wc
[3], YESNO(win
->bordered
));
4929 xcb_configure_window(conn
, win
->id
, mask
, wc
);
4932 #define SWM_RESIZE_STEPS (50)
4935 resize(struct ws_win
*win
, union arg
*args
)
4937 xcb_timestamp_t timestamp
= 0;
4938 struct swm_region
*r
= NULL
;
4939 int resize_step
= 0;
4940 struct swm_geometry g
;
4941 int top
= 0, left
= 0, resizing
;
4943 xcb_cursor_t cursor
;
4944 xcb_query_pointer_reply_t
*xpr
;
4945 xcb_generic_event_t
*evt
;
4946 xcb_motion_notify_event_t
*mne
;
4952 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
4955 DNPRINTF(SWM_D_EVENT
, "resize: window: 0x%x, floating: %s, "
4956 "transient: 0x%x\n", win
->id
, YESNO(win
->floating
),
4959 if (!win
->transient
&& !win
->floating
)
4962 /* reject resizes in max mode for floaters (transient ok) */
4963 if (win
->floatmaxed
)
4967 ewmh_update_win_state(win
, ewmh
[_SWM_WM_STATE_MANUAL
].atom
,
4975 case SWM_ARG_ID_WIDTHSHRINK
:
4976 WIDTH(win
) -= SWM_RESIZE_STEPS
;
4979 case SWM_ARG_ID_WIDTHGROW
:
4980 WIDTH(win
) += SWM_RESIZE_STEPS
;
4983 case SWM_ARG_ID_HEIGHTSHRINK
:
4984 HEIGHT(win
) -= SWM_RESIZE_STEPS
;
4987 case SWM_ARG_ID_HEIGHTGROW
:
4988 HEIGHT(win
) += SWM_RESIZE_STEPS
;
4995 constrain_window(win
, r
, 1);
4997 store_float_geom(win
,r
);
5001 /* get cursor offset from window root */
5002 xpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
5009 if (xpr
->win_x
< WIDTH(win
) / 2)
5012 if (xpr
->win_y
< HEIGHT(win
) / 2)
5015 if (args
->id
== SWM_ARG_ID_CENTER
)
5016 cursor
= cursors
[XC_SIZING
].cid
;
5018 cursor
= cursors
[left
? XC_TOP_LEFT_CORNER
:
5019 XC_TOP_RIGHT_CORNER
].cid
;
5021 cursor
= cursors
[left
? XC_BOTTOM_LEFT_CORNER
:
5022 XC_BOTTOM_RIGHT_CORNER
].cid
;
5024 xcb_grab_pointer(conn
, 0, win
->id
, MOUSEMASK
,
5025 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
, XCB_WINDOW_NONE
, cursor
,
5030 while ((evt
= xcb_wait_for_event(conn
)) && resizing
) {
5031 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
5032 case XCB_BUTTON_RELEASE
:
5033 DNPRINTF(SWM_D_EVENT
, "resize: BUTTON_RELEASE\n");
5036 case XCB_MOTION_NOTIFY
:
5037 mne
= (xcb_motion_notify_event_t
*)evt
;
5038 /* cursor offset/delta from start of the operation */
5039 dx
= mne
->root_x
- xpr
->root_x
;
5040 dy
= mne
->root_y
- xpr
->root_y
;
5046 if (args
->id
== SWM_ARG_ID_CENTER
) {
5047 if (g
.h
/ 2 + dy
< 1)
5051 HEIGHT(win
) = g
.h
+ 2 * dy
;
5059 HEIGHT(win
) = g
.h
+ dy
;
5066 if (args
->id
== SWM_ARG_ID_CENTER
) {
5067 if (g
.w
/ 2 + dx
< 1)
5071 WIDTH(win
) = g
.w
+ 2 * dx
;
5079 WIDTH(win
) = g
.w
+ dx
;
5082 constrain_window(win
, r
, 1);
5084 /* not free, don't sync more than 120 times / second */
5085 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
5086 timestamp
= mne
->time
;
5101 store_float_geom(win
,r
);
5103 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
5105 DNPRINTF(SWM_D_EVENT
, "resize: done.\n");
5109 resize_step(struct swm_region
*r
, union arg
*args
)
5111 struct ws_win
*win
= NULL
;
5113 if (r
&& r
->ws
&& r
->ws
->focus
)
5122 #define SWM_MOVE_STEPS (50)
5125 move(struct ws_win
*win
, union arg
*args
)
5127 xcb_timestamp_t timestamp
= 0;
5128 int move_step
= 0, moving
;
5129 struct swm_region
*r
= NULL
;
5130 xcb_query_pointer_reply_t
*qpr
;
5131 xcb_generic_event_t
*evt
;
5132 xcb_motion_notify_event_t
*mne
;
5138 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
5141 DNPRINTF(SWM_D_EVENT
, "move: window: 0x%x, floating: %s, transient: "
5142 "0x%x\n", win
->id
, YESNO(win
->floating
), win
->transient
);
5144 /* in max_stack mode should only move transients */
5145 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
] && !win
->transient
)
5149 if (!win
->floating
&& !win
->transient
) {
5150 store_float_geom(win
, r
);
5151 ewmh_update_win_state(win
, ewmh
[_NET_WM_STATE_ABOVE
].atom
,
5154 ewmh_update_win_state(win
, ewmh
[_SWM_WM_STATE_MANUAL
].atom
,
5163 case SWM_ARG_ID_MOVELEFT
:
5164 X(win
) -= (SWM_MOVE_STEPS
- border_width
);
5167 case SWM_ARG_ID_MOVERIGHT
:
5168 X(win
) += (SWM_MOVE_STEPS
- border_width
);
5171 case SWM_ARG_ID_MOVEUP
:
5172 Y(win
) -= (SWM_MOVE_STEPS
- border_width
);
5175 case SWM_ARG_ID_MOVEDOWN
:
5176 Y(win
) += (SWM_MOVE_STEPS
- border_width
);
5183 constrain_window(win
, r
, 0);
5185 store_float_geom(win
, r
);
5189 xcb_grab_pointer(conn
, 0, win
->id
, MOUSEMASK
,
5190 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
,
5191 XCB_WINDOW_NONE
, cursors
[XC_FLEUR
].cid
, XCB_CURRENT_TIME
);
5193 /* get cursor offset from window root */
5194 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
5197 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
5203 while ((evt
= xcb_wait_for_event(conn
)) && moving
) {
5204 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
5205 case XCB_BUTTON_RELEASE
:
5206 DNPRINTF(SWM_D_EVENT
, "move: BUTTON_RELEASE\n");
5209 case XCB_MOTION_NOTIFY
:
5210 mne
= (xcb_motion_notify_event_t
*)evt
;
5211 X(win
) = mne
->root_x
- qpr
->win_x
- border_width
;
5212 Y(win
) = mne
->root_y
- qpr
->win_y
- border_width
;
5214 constrain_window(win
, r
, 0);
5216 /* not free, don't sync more than 120 times / second */
5217 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
5218 timestamp
= mne
->time
;
5233 store_float_geom(win
, r
);
5235 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
5236 DNPRINTF(SWM_D_EVENT
, "move: done.\n");
5240 move_step(struct swm_region
*r
, union arg
*args
)
5242 struct ws_win
*win
= NULL
;
5244 if (r
&& r
->ws
&& r
->ws
->focus
)
5249 if (!win
->transient
&& !win
->floating
)
5256 /* user/key callable function IDs */
5306 KF_SEARCH_WORKSPACE
,
5347 KF_DUMPWINS
, /* MUST BE LAST */
5351 /* key definitions */
5353 char name
[SWM_FUNCNAME_LEN
];
5354 void (*func
)(struct swm_region
*r
, union arg
*);
5356 } keyfuncs
[KF_INVALID
+ 1] = {
5357 /* name function argument */
5358 { "bar_toggle", bar_toggle
, {0} },
5359 { "button2", pressbutton
, {2} },
5360 { "cycle_layout", cycle_layout
, {0} },
5361 { "flip_layout", stack_config
, {.id
= SWM_ARG_ID_FLIPLAYOUT
} },
5362 { "float_toggle", floating_toggle
,{0} },
5363 { "focus_main", focus
, {.id
= SWM_ARG_ID_FOCUSMAIN
} },
5364 { "focus_next", focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
5365 { "focus_prev", focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
5366 { "height_grow", resize_step
, {.id
= SWM_ARG_ID_HEIGHTGROW
} },
5367 { "height_shrink", resize_step
, {.id
= SWM_ARG_ID_HEIGHTSHRINK
} },
5368 { "iconify", iconify
, {0} },
5369 { "master_shrink", stack_config
, {.id
= SWM_ARG_ID_MASTERSHRINK
} },
5370 { "master_grow", stack_config
, {.id
= SWM_ARG_ID_MASTERGROW
} },
5371 { "master_add", stack_config
, {.id
= SWM_ARG_ID_MASTERADD
} },
5372 { "master_del", stack_config
, {.id
= SWM_ARG_ID_MASTERDEL
} },
5373 { "move_down", move_step
, {.id
= SWM_ARG_ID_MOVEDOWN
} },
5374 { "move_left", move_step
, {.id
= SWM_ARG_ID_MOVELEFT
} },
5375 { "move_right", move_step
, {.id
= SWM_ARG_ID_MOVERIGHT
} },
5376 { "move_up", move_step
, {.id
= SWM_ARG_ID_MOVEUP
} },
5377 { "mvws_1", send_to_ws
, {.id
= 0} },
5378 { "mvws_2", send_to_ws
, {.id
= 1} },
5379 { "mvws_3", send_to_ws
, {.id
= 2} },
5380 { "mvws_4", send_to_ws
, {.id
= 3} },
5381 { "mvws_5", send_to_ws
, {.id
= 4} },
5382 { "mvws_6", send_to_ws
, {.id
= 5} },
5383 { "mvws_7", send_to_ws
, {.id
= 6} },
5384 { "mvws_8", send_to_ws
, {.id
= 7} },
5385 { "mvws_9", send_to_ws
, {.id
= 8} },
5386 { "mvws_10", send_to_ws
, {.id
= 9} },
5387 { "mvws_11", send_to_ws
, {.id
= 10} },
5388 { "mvws_12", send_to_ws
, {.id
= 11} },
5389 { "mvws_13", send_to_ws
, {.id
= 12} },
5390 { "mvws_14", send_to_ws
, {.id
= 13} },
5391 { "mvws_15", send_to_ws
, {.id
= 14} },
5392 { "mvws_16", send_to_ws
, {.id
= 15} },
5393 { "mvws_17", send_to_ws
, {.id
= 16} },
5394 { "mvws_18", send_to_ws
, {.id
= 17} },
5395 { "mvws_19", send_to_ws
, {.id
= 18} },
5396 { "mvws_20", send_to_ws
, {.id
= 19} },
5397 { "mvws_21", send_to_ws
, {.id
= 20} },
5398 { "mvws_22", send_to_ws
, {.id
= 21} },
5399 { "name_workspace", name_workspace
, {0} },
5400 { "quit", quit
, {0} },
5401 { "raise_toggle", raise_toggle
, {0} },
5402 { "restart", restart
, {0} },
5403 { "screen_next", cyclescr
, {.id
= SWM_ARG_ID_CYCLESC_UP
} },
5404 { "screen_prev", cyclescr
, {.id
= SWM_ARG_ID_CYCLESC_DOWN
} },
5405 { "search_win", search_win
, {0} },
5406 { "search_workspace", search_workspace
, {0} },
5407 { "spawn_custom", NULL
, {0} },
5408 { "stack_inc", stack_config
, {.id
= SWM_ARG_ID_STACKINC
} },
5409 { "stack_dec", stack_config
, {.id
= SWM_ARG_ID_STACKDEC
} },
5410 { "stack_reset", stack_config
, {.id
= SWM_ARG_ID_STACKRESET
} },
5411 { "swap_main", swapwin
, {.id
= SWM_ARG_ID_SWAPMAIN
} },
5412 { "swap_next", swapwin
, {.id
= SWM_ARG_ID_SWAPNEXT
} },
5413 { "swap_prev", swapwin
, {.id
= SWM_ARG_ID_SWAPPREV
} },
5414 { "uniconify", uniconify
, {0} },
5415 { "version", version
, {0} },
5416 { "width_grow", resize_step
, {.id
= SWM_ARG_ID_WIDTHGROW
} },
5417 { "width_shrink", resize_step
, {.id
= SWM_ARG_ID_WIDTHSHRINK
} },
5418 { "wind_del", wkill
, {.id
= SWM_ARG_ID_DELETEWINDOW
} },
5419 { "wind_kill", wkill
, {.id
= SWM_ARG_ID_KILLWINDOW
} },
5420 { "ws_1", switchws
, {.id
= 0} },
5421 { "ws_2", switchws
, {.id
= 1} },
5422 { "ws_3", switchws
, {.id
= 2} },
5423 { "ws_4", switchws
, {.id
= 3} },
5424 { "ws_5", switchws
, {.id
= 4} },
5425 { "ws_6", switchws
, {.id
= 5} },
5426 { "ws_7", switchws
, {.id
= 6} },
5427 { "ws_8", switchws
, {.id
= 7} },
5428 { "ws_9", switchws
, {.id
= 8} },
5429 { "ws_10", switchws
, {.id
= 9} },
5430 { "ws_11", switchws
, {.id
= 10} },
5431 { "ws_12", switchws
, {.id
= 11} },
5432 { "ws_13", switchws
, {.id
= 12} },
5433 { "ws_14", switchws
, {.id
= 13} },
5434 { "ws_15", switchws
, {.id
= 14} },
5435 { "ws_16", switchws
, {.id
= 15} },
5436 { "ws_17", switchws
, {.id
= 16} },
5437 { "ws_18", switchws
, {.id
= 17} },
5438 { "ws_19", switchws
, {.id
= 18} },
5439 { "ws_20", switchws
, {.id
= 19} },
5440 { "ws_21", switchws
, {.id
= 20} },
5441 { "ws_22", switchws
, {.id
= 21} },
5442 { "ws_next", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP
} },
5443 { "ws_next_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP_ALL
} },
5444 { "ws_prev", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN
} },
5445 { "ws_prev_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN_ALL
} },
5446 { "ws_prior", priorws
, {0} },
5447 { "dumpwins", dumpwins
, {0} }, /* MUST BE LAST */
5448 { "invalid key func", NULL
, {0} },
5451 RB_ENTRY(key
) entry
;
5454 enum keyfuncid funcid
;
5457 RB_HEAD(key_tree
, key
);
5460 key_cmp(struct key
*kp1
, struct key
*kp2
)
5462 if (kp1
->keysym
< kp2
->keysym
)
5464 if (kp1
->keysym
> kp2
->keysym
)
5467 if (kp1
->mod
< kp2
->mod
)
5469 if (kp1
->mod
> kp2
->mod
)
5475 RB_GENERATE(key_tree
, key
, entry
, key_cmp
);
5476 struct key_tree keys
;
5479 enum { client_click
, root_click
};
5481 unsigned int action
;
5483 unsigned int button
;
5484 void (*func
)(struct ws_win
*, union arg
*);
5487 #define MODKEY_SHIFT MODKEY | XCB_MOD_MASK_SHIFT
5488 /* action key mouse button func args */
5489 { client_click
, MODKEY
, XCB_BUTTON_INDEX_3
, resize
, {.id
= SWM_ARG_ID_DONTCENTER
} },
5490 { client_click
, MODKEY_SHIFT
, XCB_BUTTON_INDEX_3
, resize
, {.id
= SWM_ARG_ID_CENTER
} },
5491 { client_click
, MODKEY
, XCB_BUTTON_INDEX_1
, move
, {0} },
5496 update_modkey(unsigned int mod
)
5502 RB_FOREACH(kp
, key_tree
, &keys
)
5503 if (kp
->mod
& XCB_MOD_MASK_SHIFT
)
5504 kp
->mod
= mod
| XCB_MOD_MASK_SHIFT
;
5508 for (i
= 0; i
< LENGTH(buttons
); i
++)
5509 if (buttons
[i
].mask
& XCB_MOD_MASK_SHIFT
)
5510 buttons
[i
].mask
= mod
| XCB_MOD_MASK_SHIFT
;
5512 buttons
[i
].mask
= mod
;
5516 spawn_expand(struct swm_region
*r
, union arg
*args
, const char *spawn_name
,
5519 struct spawn_prog
*prog
= NULL
;
5521 char *ap
, **real_args
;
5523 /* suppress unused warning since var is needed */
5526 DNPRINTF(SWM_D_SPAWN
, "spawn_expand: %s\n", spawn_name
);
5529 TAILQ_FOREACH(prog
, &spawns
, entry
) {
5530 if (!strcasecmp(spawn_name
, prog
->name
))
5534 warnx("spawn_custom: program %s not found", spawn_name
);
5538 /* make room for expanded args */
5539 if ((real_args
= calloc(prog
->argc
+ 1, sizeof(char *))) == NULL
)
5540 err(1, "spawn_custom: calloc real_args");
5542 /* expand spawn_args into real_args */
5543 for (i
= 0; i
< prog
->argc
; i
++) {
5545 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: raw arg: %s\n", ap
);
5546 if (!strcasecmp(ap
, "$bar_border")) {
5548 strdup(r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].name
))
5550 err(1, "spawn_custom border color");
5551 } else if (!strcasecmp(ap
, "$bar_color")) {
5553 strdup(r
->s
->c
[SWM_S_COLOR_BAR
].name
))
5555 err(1, "spawn_custom bar color");
5556 } else if (!strcasecmp(ap
, "$bar_font")) {
5557 if ((real_args
[i
] = strdup(bar_fonts
))
5559 err(1, "spawn_custom bar fonts");
5560 } else if (!strcasecmp(ap
, "$bar_font_color")) {
5562 strdup(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].name
))
5564 err(1, "spawn_custom color font");
5565 } else if (!strcasecmp(ap
, "$color_focus")) {
5567 strdup(r
->s
->c
[SWM_S_COLOR_FOCUS
].name
))
5569 err(1, "spawn_custom color focus");
5570 } else if (!strcasecmp(ap
, "$color_unfocus")) {
5572 strdup(r
->s
->c
[SWM_S_COLOR_UNFOCUS
].name
))
5574 err(1, "spawn_custom color unfocus");
5576 /* no match --> copy as is */
5577 if ((real_args
[i
] = strdup(ap
)) == NULL
)
5578 err(1, "spawn_custom strdup(ap)");
5580 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: cooked arg: %s\n",
5585 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: result: ");
5586 for (i
= 0; i
< prog
->argc
; i
++)
5587 DNPRINTF(SWM_D_SPAWN
, "\"%s\" ", real_args
[i
]);
5588 DNPRINTF(SWM_D_SPAWN
, "\n");
5590 *ret_args
= real_args
;
5591 return (prog
->argc
);
5595 spawn_custom(struct swm_region
*r
, union arg
*args
, const char *spawn_name
)
5601 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
5605 spawn(r
->ws
->idx
, &a
, 1);
5607 for (i
= 0; i
< spawn_argc
; i
++)
5613 spawn_select(struct swm_region
*r
, union arg
*args
, const char *spawn_name
,
5620 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
5624 if (pipe(select_list_pipe
) == -1)
5625 err(1, "pipe error");
5626 if (pipe(select_resp_pipe
) == -1)
5627 err(1, "pipe error");
5629 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
5630 err(1, "could not disable SIGPIPE");
5631 switch (*pid
= fork()) {
5633 err(1, "cannot fork");
5636 if (dup2(select_list_pipe
[0], 0) == -1)
5638 if (dup2(select_resp_pipe
[1], 1) == -1)
5640 close(select_list_pipe
[1]);
5641 close(select_resp_pipe
[0]);
5642 spawn(r
->ws
->idx
, &a
, 0);
5644 default: /* parent */
5645 close(select_list_pipe
[0]);
5646 close(select_resp_pipe
[1]);
5650 for (i
= 0; i
< spawn_argc
; i
++)
5656 spawn_insert(const char *name
, const char *args
)
5658 char *arg
, *cp
, *ptr
;
5659 struct spawn_prog
*sp
;
5661 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: %s\n", name
);
5663 if ((sp
= calloc(1, sizeof *sp
)) == NULL
)
5664 err(1, "spawn_insert: calloc");
5665 if ((sp
->name
= strdup(name
)) == NULL
)
5666 err(1, "spawn_insert: strdup");
5668 /* convert the arguments to an argument list */
5669 if ((ptr
= cp
= strdup(args
)) == NULL
)
5670 err(1, "spawn_insert: strdup");
5671 while ((arg
= strsep(&ptr
, " \t")) != NULL
) {
5672 /* empty field; skip it */
5677 if ((sp
->argv
= realloc(sp
->argv
, sp
->argc
*
5678 sizeof *sp
->argv
)) == NULL
)
5679 err(1, "spawn_insert: realloc");
5680 if ((sp
->argv
[sp
->argc
- 1] = strdup(arg
)) == NULL
)
5681 err(1, "spawn_insert: strdup");
5685 TAILQ_INSERT_TAIL(&spawns
, sp
, entry
);
5686 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: leave\n");
5690 spawn_remove(struct spawn_prog
*sp
)
5694 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: %s\n", sp
->name
);
5696 TAILQ_REMOVE(&spawns
, sp
, entry
);
5697 for (i
= 0; i
< sp
->argc
; i
++)
5703 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: leave\n");
5707 spawn_replace(struct spawn_prog
*sp
, const char *name
, const char *args
)
5709 DNPRINTF(SWM_D_SPAWN
, "spawn_replace: %s [%s]\n", sp
->name
, name
);
5712 spawn_insert(name
, args
);
5714 DNPRINTF(SWM_D_SPAWN
, "spawn_replace: leave\n");
5718 setspawn(const char *name
, const char *args
)
5720 struct spawn_prog
*sp
;
5722 DNPRINTF(SWM_D_SPAWN
, "setspawn: %s\n", name
);
5727 TAILQ_FOREACH(sp
, &spawns
, entry
) {
5728 if (!strcmp(sp
->name
, name
)) {
5732 spawn_replace(sp
, name
, args
);
5733 DNPRINTF(SWM_D_SPAWN
, "setspawn: leave\n");
5737 if (*args
== '\0') {
5738 warnx("error: setspawn: cannot find program: %s", name
);
5742 spawn_insert(name
, args
);
5743 DNPRINTF(SWM_D_SPAWN
, "setspawn: leave\n");
5747 setconfspawn(char *selector
, char *value
, int flags
)
5751 /* suppress unused warning since var is needed */
5754 args
= expand_tilde(value
);
5756 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: [%s] [%s]\n", selector
, args
);
5758 setspawn(selector
, args
);
5761 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: done.\n");
5768 setconfspawn("term", "xterm", 0);
5769 setconfspawn("spawn_term", "xterm", 0);
5770 setconfspawn("screenshot_all", "screenshot.sh full", 0);
5771 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
5772 setconfspawn("lock", "xlock", 0);
5773 setconfspawn("initscr", "initscreen.sh", 0);
5774 setconfspawn("menu", "dmenu_run"
5777 " -nf $bar_font_color"
5779 " -sf $bar_color", 0);
5780 setconfspawn("search", "dmenu"
5784 " -nf $bar_font_color"
5786 " -sf $bar_color", 0);
5787 setconfspawn("name_workspace", "dmenu"
5791 " -nf $bar_font_color"
5793 " -sf $bar_color", 0);
5797 #define SWM_MODNAME_SIZE 32
5798 #define SWM_KEY_WS "\n+ \t"
5800 parsekeys(char *keystr
, unsigned int currmod
, unsigned int *mod
, KeySym
*ks
)
5804 DNPRINTF(SWM_D_KEY
, "parsekeys: enter [%s]\n", keystr
);
5805 if (mod
== NULL
|| ks
== NULL
) {
5806 DNPRINTF(SWM_D_KEY
, "parsekeys: no mod or key vars\n");
5809 if (keystr
== NULL
|| strlen(keystr
) == 0) {
5810 DNPRINTF(SWM_D_KEY
, "parsekeys: no keystr\n");
5814 *ks
= XCB_NO_SYMBOL
;
5816 while ((name
= strsep(&cp
, SWM_KEY_WS
)) != NULL
) {
5817 DNPRINTF(SWM_D_KEY
, "parsekeys: key [%s]\n", name
);
5819 cp
+= (long)strspn(cp
, SWM_KEY_WS
);
5820 if (strncasecmp(name
, "MOD", SWM_MODNAME_SIZE
) == 0)
5822 else if (!strncasecmp(name
, "Mod1", SWM_MODNAME_SIZE
))
5823 *mod
|= XCB_MOD_MASK_1
;
5824 else if (!strncasecmp(name
, "Mod2", SWM_MODNAME_SIZE
))
5825 *mod
+= XCB_MOD_MASK_2
;
5826 else if (!strncmp(name
, "Mod3", SWM_MODNAME_SIZE
))
5827 *mod
|= XCB_MOD_MASK_3
;
5828 else if (!strncmp(name
, "Mod4", SWM_MODNAME_SIZE
))
5829 *mod
|= XCB_MOD_MASK_4
;
5830 else if (strncasecmp(name
, "SHIFT", SWM_MODNAME_SIZE
) == 0)
5831 *mod
|= XCB_MOD_MASK_SHIFT
;
5832 else if (strncasecmp(name
, "CONTROL", SWM_MODNAME_SIZE
) == 0)
5833 *mod
|= XCB_MOD_MASK_CONTROL
;
5835 *ks
= XStringToKeysym(name
);
5836 XConvertCase(*ks
, ks
, &uks
);
5837 if (ks
== XCB_NO_SYMBOL
) {
5839 "parsekeys: invalid key %s\n",
5845 DNPRINTF(SWM_D_KEY
, "parsekeys: leave ok\n");
5850 strdupsafe(const char *str
)
5855 return (strdup(str
));
5859 key_insert(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
5860 const char *spawn_name
)
5864 DNPRINTF(SWM_D_KEY
, "key_insert: enter %s [%s]\n",
5865 keyfuncs
[kfid
].name
, spawn_name
);
5867 if ((kp
= malloc(sizeof *kp
)) == NULL
)
5868 err(1, "key_insert: malloc");
5873 kp
->spawn_name
= strdupsafe(spawn_name
);
5874 RB_INSERT(key_tree
, &keys
, kp
);
5876 DNPRINTF(SWM_D_KEY
, "key_insert: leave\n");
5880 key_lookup(unsigned int mod
, KeySym ks
)
5887 return (RB_FIND(key_tree
, &keys
, &kp
));
5891 key_remove(struct key
*kp
)
5893 DNPRINTF(SWM_D_KEY
, "key_remove: %s\n", keyfuncs
[kp
->funcid
].name
);
5895 RB_REMOVE(key_tree
, &keys
, kp
);
5896 free(kp
->spawn_name
);
5899 DNPRINTF(SWM_D_KEY
, "key_remove: leave\n");
5903 key_replace(struct key
*kp
, unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
5904 const char *spawn_name
)
5906 DNPRINTF(SWM_D_KEY
, "key_replace: %s [%s]\n", keyfuncs
[kp
->funcid
].name
,
5910 key_insert(mod
, ks
, kfid
, spawn_name
);
5912 DNPRINTF(SWM_D_KEY
, "key_replace: leave\n");
5916 setkeybinding(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
5917 const char *spawn_name
)
5921 DNPRINTF(SWM_D_KEY
, "setkeybinding: enter %s [%s]\n",
5922 keyfuncs
[kfid
].name
, spawn_name
);
5924 if ((kp
= key_lookup(mod
, ks
)) != NULL
) {
5925 if (kfid
== KF_INVALID
)
5928 key_replace(kp
, mod
, ks
, kfid
, spawn_name
);
5929 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
5932 if (kfid
== KF_INVALID
) {
5933 warnx("error: setkeybinding: cannot find mod/key combination");
5934 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
5938 key_insert(mod
, ks
, kfid
, spawn_name
);
5939 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
5943 setconfbinding(char *selector
, char *value
, int flags
)
5945 enum keyfuncid kfid
;
5948 struct spawn_prog
*sp
;
5950 /* suppress unused warning since var is needed */
5953 DNPRINTF(SWM_D_KEY
, "setconfbinding: enter\n");
5954 if (selector
== NULL
) {
5955 DNPRINTF(SWM_D_KEY
, "setconfbinding: unbind %s\n", value
);
5956 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
5958 setkeybinding(mod
, ks
, kfid
, NULL
);
5963 /* search by key function name */
5964 for (kfid
= 0; kfid
< KF_INVALID
; (kfid
)++) {
5965 if (strncasecmp(selector
, keyfuncs
[kfid
].name
,
5966 SWM_FUNCNAME_LEN
) == 0) {
5967 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match\n",
5969 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
5970 setkeybinding(mod
, ks
, kfid
, NULL
);
5976 /* search by custom spawn name */
5977 TAILQ_FOREACH(sp
, &spawns
, entry
) {
5978 if (strcasecmp(selector
, sp
->name
) == 0) {
5979 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match\n",
5981 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
5982 setkeybinding(mod
, ks
, KF_SPAWN_CUSTOM
,
5989 DNPRINTF(SWM_D_KEY
, "setconfbinding: no match\n");
5996 #define MODKEY_SHIFT MODKEY | XCB_MOD_MASK_SHIFT
5997 setkeybinding(MODKEY
, XK_space
, KF_CYCLE_LAYOUT
,NULL
);
5998 setkeybinding(MODKEY_SHIFT
, XK_backslash
, KF_FLIP_LAYOUT
, NULL
);
5999 setkeybinding(MODKEY_SHIFT
, XK_space
, KF_STACK_RESET
, NULL
);
6000 setkeybinding(MODKEY
, XK_h
, KF_MASTER_SHRINK
, NULL
);
6001 setkeybinding(MODKEY
, XK_l
, KF_MASTER_GROW
, NULL
);
6002 setkeybinding(MODKEY
, XK_comma
, KF_MASTER_ADD
, NULL
);
6003 setkeybinding(MODKEY
, XK_period
, KF_MASTER_DEL
, NULL
);
6004 setkeybinding(MODKEY_SHIFT
, XK_comma
, KF_STACK_INC
, NULL
);
6005 setkeybinding(MODKEY_SHIFT
, XK_period
, KF_STACK_DEC
, NULL
);
6006 setkeybinding(MODKEY
, XK_Return
, KF_SWAP_MAIN
, NULL
);
6007 setkeybinding(MODKEY
, XK_j
, KF_FOCUS_NEXT
, NULL
);
6008 setkeybinding(MODKEY
, XK_k
, KF_FOCUS_PREV
, NULL
);
6009 setkeybinding(MODKEY_SHIFT
, XK_j
, KF_SWAP_NEXT
, NULL
);
6010 setkeybinding(MODKEY_SHIFT
, XK_k
, KF_SWAP_PREV
, NULL
);
6011 setkeybinding(MODKEY_SHIFT
, XK_Return
, KF_SPAWN_CUSTOM
,"term");
6012 setkeybinding(MODKEY
, XK_p
, KF_SPAWN_CUSTOM
,"menu");
6013 setkeybinding(MODKEY_SHIFT
, XK_q
, KF_QUIT
, NULL
);
6014 setkeybinding(MODKEY
, XK_q
, KF_RESTART
, NULL
);
6015 setkeybinding(MODKEY
, XK_m
, KF_FOCUS_MAIN
, NULL
);
6016 setkeybinding(MODKEY
, XK_1
, KF_WS_1
, NULL
);
6017 setkeybinding(MODKEY
, XK_2
, KF_WS_2
, NULL
);
6018 setkeybinding(MODKEY
, XK_3
, KF_WS_3
, NULL
);
6019 setkeybinding(MODKEY
, XK_4
, KF_WS_4
, NULL
);
6020 setkeybinding(MODKEY
, XK_5
, KF_WS_5
, NULL
);
6021 setkeybinding(MODKEY
, XK_6
, KF_WS_6
, NULL
);
6022 setkeybinding(MODKEY
, XK_7
, KF_WS_7
, NULL
);
6023 setkeybinding(MODKEY
, XK_8
, KF_WS_8
, NULL
);
6024 setkeybinding(MODKEY
, XK_9
, KF_WS_9
, NULL
);
6025 setkeybinding(MODKEY
, XK_0
, KF_WS_10
, NULL
);
6026 setkeybinding(MODKEY
, XK_F1
, KF_WS_11
, NULL
);
6027 setkeybinding(MODKEY
, XK_F2
, KF_WS_12
, NULL
);
6028 setkeybinding(MODKEY
, XK_F3
, KF_WS_13
, NULL
);
6029 setkeybinding(MODKEY
, XK_F4
, KF_WS_14
, NULL
);
6030 setkeybinding(MODKEY
, XK_F5
, KF_WS_15
, NULL
);
6031 setkeybinding(MODKEY
, XK_F6
, KF_WS_16
, NULL
);
6032 setkeybinding(MODKEY
, XK_F7
, KF_WS_17
, NULL
);
6033 setkeybinding(MODKEY
, XK_F8
, KF_WS_18
, NULL
);
6034 setkeybinding(MODKEY
, XK_F9
, KF_WS_19
, NULL
);
6035 setkeybinding(MODKEY
, XK_F10
, KF_WS_20
, NULL
);
6036 setkeybinding(MODKEY
, XK_F11
, KF_WS_21
, NULL
);
6037 setkeybinding(MODKEY
, XK_F12
, KF_WS_22
, NULL
);
6038 setkeybinding(MODKEY
, XK_Right
, KF_WS_NEXT
, NULL
);
6039 setkeybinding(MODKEY
, XK_Left
, KF_WS_PREV
, NULL
);
6040 setkeybinding(MODKEY
, XK_Up
, KF_WS_NEXT_ALL
, NULL
);
6041 setkeybinding(MODKEY
, XK_Down
, KF_WS_PREV_ALL
, NULL
);
6042 setkeybinding(MODKEY
, XK_a
, KF_WS_PRIOR
, NULL
);
6043 setkeybinding(MODKEY_SHIFT
, XK_Right
, KF_SCREEN_NEXT
, NULL
);
6044 setkeybinding(MODKEY_SHIFT
, XK_Left
, KF_SCREEN_PREV
, NULL
);
6045 setkeybinding(MODKEY_SHIFT
, XK_1
, KF_MVWS_1
, NULL
);
6046 setkeybinding(MODKEY_SHIFT
, XK_2
, KF_MVWS_2
, NULL
);
6047 setkeybinding(MODKEY_SHIFT
, XK_3
, KF_MVWS_3
, NULL
);
6048 setkeybinding(MODKEY_SHIFT
, XK_4
, KF_MVWS_4
, NULL
);
6049 setkeybinding(MODKEY_SHIFT
, XK_5
, KF_MVWS_5
, NULL
);
6050 setkeybinding(MODKEY_SHIFT
, XK_6
, KF_MVWS_6
, NULL
);
6051 setkeybinding(MODKEY_SHIFT
, XK_7
, KF_MVWS_7
, NULL
);
6052 setkeybinding(MODKEY_SHIFT
, XK_8
, KF_MVWS_8
, NULL
);
6053 setkeybinding(MODKEY_SHIFT
, XK_9
, KF_MVWS_9
, NULL
);
6054 setkeybinding(MODKEY_SHIFT
, XK_0
, KF_MVWS_10
, NULL
);
6055 setkeybinding(MODKEY_SHIFT
, XK_F1
, KF_MVWS_11
, NULL
);
6056 setkeybinding(MODKEY_SHIFT
, XK_F2
, KF_MVWS_12
, NULL
);
6057 setkeybinding(MODKEY_SHIFT
, XK_F3
, KF_MVWS_13
, NULL
);
6058 setkeybinding(MODKEY_SHIFT
, XK_F4
, KF_MVWS_14
, NULL
);
6059 setkeybinding(MODKEY_SHIFT
, XK_F5
, KF_MVWS_15
, NULL
);
6060 setkeybinding(MODKEY_SHIFT
, XK_F6
, KF_MVWS_16
, NULL
);
6061 setkeybinding(MODKEY_SHIFT
, XK_F7
, KF_MVWS_17
, NULL
);
6062 setkeybinding(MODKEY_SHIFT
, XK_F8
, KF_MVWS_18
, NULL
);
6063 setkeybinding(MODKEY_SHIFT
, XK_F9
, KF_MVWS_19
, NULL
);
6064 setkeybinding(MODKEY_SHIFT
, XK_F10
, KF_MVWS_20
, NULL
);
6065 setkeybinding(MODKEY_SHIFT
, XK_F11
, KF_MVWS_21
, NULL
);
6066 setkeybinding(MODKEY_SHIFT
, XK_F12
, KF_MVWS_22
, NULL
);
6067 setkeybinding(MODKEY
, XK_b
, KF_BAR_TOGGLE
, NULL
);
6068 setkeybinding(MODKEY
, XK_Tab
, KF_FOCUS_NEXT
, NULL
);
6069 setkeybinding(MODKEY_SHIFT
, XK_Tab
, KF_FOCUS_PREV
, NULL
);
6070 setkeybinding(MODKEY_SHIFT
, XK_x
, KF_WIND_KILL
, NULL
);
6071 setkeybinding(MODKEY
, XK_x
, KF_WIND_DEL
, NULL
);
6072 setkeybinding(MODKEY
, XK_s
, KF_SPAWN_CUSTOM
,"screenshot_all");
6073 setkeybinding(MODKEY_SHIFT
, XK_s
, KF_SPAWN_CUSTOM
,"screenshot_wind");
6074 setkeybinding(MODKEY
, XK_t
, KF_FLOAT_TOGGLE
,NULL
);
6075 setkeybinding(MODKEY_SHIFT
, XK_v
, KF_VERSION
, NULL
);
6076 setkeybinding(MODKEY_SHIFT
, XK_Delete
, KF_SPAWN_CUSTOM
,"lock");
6077 setkeybinding(MODKEY_SHIFT
, XK_i
, KF_SPAWN_CUSTOM
,"initscr");
6078 setkeybinding(MODKEY
, XK_w
, KF_ICONIFY
, NULL
);
6079 setkeybinding(MODKEY_SHIFT
, XK_w
, KF_UNICONIFY
, NULL
);
6080 setkeybinding(MODKEY_SHIFT
, XK_r
, KF_RAISE_TOGGLE
,NULL
);
6081 setkeybinding(MODKEY
, XK_v
, KF_BUTTON2
, NULL
);
6082 setkeybinding(MODKEY
, XK_equal
, KF_WIDTH_GROW
, NULL
);
6083 setkeybinding(MODKEY
, XK_minus
, KF_WIDTH_SHRINK
,NULL
);
6084 setkeybinding(MODKEY_SHIFT
, XK_equal
, KF_HEIGHT_GROW
,NULL
);
6085 setkeybinding(MODKEY_SHIFT
, XK_minus
, KF_HEIGHT_SHRINK
,NULL
);
6086 setkeybinding(MODKEY
, XK_bracketleft
, KF_MOVE_LEFT
,NULL
);
6087 setkeybinding(MODKEY
, XK_bracketright
,KF_MOVE_RIGHT
,NULL
);
6088 setkeybinding(MODKEY_SHIFT
, XK_bracketleft
, KF_MOVE_UP
, NULL
);
6089 setkeybinding(MODKEY_SHIFT
, XK_bracketright
,KF_MOVE_DOWN
,NULL
);
6090 setkeybinding(MODKEY_SHIFT
, XK_slash
, KF_NAME_WORKSPACE
,NULL
);
6091 setkeybinding(MODKEY
, XK_slash
, KF_SEARCH_WORKSPACE
,NULL
);
6092 setkeybinding(MODKEY
, XK_f
, KF_SEARCH_WIN
, NULL
);
6094 setkeybinding(MODKEY_SHIFT
, XK_d
, KF_DUMPWINS
, NULL
);
6104 while (RB_EMPTY(&keys
) == 0) {
6105 kp
= RB_ROOT(&keys
);
6111 setkeymapping(char *selector
, char *value
, int flags
)
6113 char keymapping_file
[PATH_MAX
];
6115 /* suppress unused warnings since vars are needed */
6119 DNPRINTF(SWM_D_KEY
, "setkeymapping: enter\n");
6120 if (value
[0] == '~')
6121 snprintf(keymapping_file
, sizeof keymapping_file
, "%s/%s",
6122 pwd
->pw_dir
, &value
[1]);
6124 strlcpy(keymapping_file
, value
, sizeof keymapping_file
);
6126 /* load new key bindings; if it fails, revert to default bindings */
6127 if (conf_load(keymapping_file
, SWM_CONF_KEYMAPPING
)) {
6131 DNPRINTF(SWM_D_KEY
, "setkeymapping: leave\n");
6136 updatenumlockmask(void)
6139 xcb_get_modifier_mapping_reply_t
*modmap_r
;
6140 xcb_keycode_t
*modmap
, kc
, *keycode
;
6144 modmap_r
= xcb_get_modifier_mapping_reply(conn
,
6145 xcb_get_modifier_mapping(conn
),
6148 modmap
= xcb_get_modifier_mapping_keycodes(modmap_r
);
6149 for (i
= 0; i
< 8; i
++) {
6150 for (j
= 0; j
< modmap_r
->keycodes_per_modifier
; j
++) {
6151 kc
= modmap
[i
* modmap_r
->keycodes_per_modifier
6153 keycode
= xcb_key_symbols_get_keycode(syms
,
6156 numlockmask
= (1 << i
);
6162 DNPRINTF(SWM_D_MISC
, "updatenumlockmask: %d\n", numlockmask
);
6169 int num_screens
, k
, j
;
6170 unsigned int modifiers
[3];
6171 xcb_keycode_t
*code
;
6173 DNPRINTF(SWM_D_MISC
, "grabkeys\n");
6174 updatenumlockmask();
6177 modifiers
[1] = numlockmask
;
6178 modifiers
[2] = numlockmask
| XCB_MOD_MASK_LOCK
;
6180 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
6181 for (k
= 0; k
< num_screens
; k
++) {
6182 if (TAILQ_EMPTY(&screens
[k
].rl
))
6184 xcb_ungrab_key(conn
, XCB_GRAB_ANY
, screens
[k
].root
,
6186 RB_FOREACH(kp
, key_tree
, &keys
) {
6187 if ((code
= xcb_key_symbols_get_keycode(syms
,
6189 for (j
= 0; j
< LENGTH(modifiers
); j
++)
6190 xcb_grab_key(conn
, 1,
6192 kp
->mod
| modifiers
[j
],
6193 *code
, XCB_GRAB_MODE_SYNC
,
6194 XCB_GRAB_MODE_ASYNC
);
6202 grabbuttons(struct ws_win
*win
)
6206 DNPRINTF(SWM_D_MOUSE
, "grabbuttons: win 0x%x\n", win
->id
);
6208 for (i
= 0; i
< LENGTH(buttons
); i
++)
6209 if (buttons
[i
].action
== client_click
)
6210 xcb_grab_button(conn
, 0, win
->id
, BUTTONMASK
,
6211 XCB_GRAB_MODE_SYNC
, XCB_GRAB_MODE_ASYNC
,
6212 XCB_WINDOW_NONE
, XCB_CURSOR_NONE
,
6213 buttons
[i
].button
, buttons
[i
].mask
);
6216 const char *quirkname
[] = {
6217 "NONE", /* config string for "no value" */
6226 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
6227 #define SWM_Q_WS "\n|+ \t"
6229 parsequirks(char *qstr
, unsigned long *quirk
)
6239 while ((name
= strsep(&cp
, SWM_Q_WS
)) != NULL
) {
6241 cp
+= (long)strspn(cp
, SWM_Q_WS
);
6242 for (i
= 0; i
< LENGTH(quirkname
); i
++) {
6243 if (!strncasecmp(name
, quirkname
[i
], SWM_QUIRK_LEN
)) {
6244 DNPRINTF(SWM_D_QUIRK
,
6245 "parsequirks: %s\n", name
);
6250 *quirk
|= 1 << (i
-1);
6254 if (i
>= LENGTH(quirkname
)) {
6255 DNPRINTF(SWM_D_QUIRK
,
6256 "parsequirks: invalid quirk [%s]\n", name
);
6264 quirk_insert(const char *class, const char *name
, unsigned long quirk
)
6268 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: %s:%s [%lu]\n", class, name
,
6271 if ((qp
= malloc(sizeof *qp
)) == NULL
)
6272 err(1, "quirk_insert: malloc");
6273 if ((qp
->class = strdup(class)) == NULL
)
6274 err(1, "quirk_insert: strdup");
6275 if ((qp
->name
= strdup(name
)) == NULL
)
6276 err(1, "quirk_insert: strdup");
6279 TAILQ_INSERT_TAIL(&quirks
, qp
, entry
);
6281 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: leave\n");
6285 quirk_remove(struct quirk
*qp
)
6287 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: %s:%s [%lu]\n", qp
->class,
6288 qp
->name
, qp
->quirk
);
6290 TAILQ_REMOVE(&quirks
, qp
, entry
);
6295 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: leave\n");
6299 quirk_replace(struct quirk
*qp
, const char *class, const char *name
,
6300 unsigned long quirk
)
6302 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: %s:%s [%lu]\n", qp
->class,
6303 qp
->name
, qp
->quirk
);
6306 quirk_insert(class, name
, quirk
);
6308 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: leave\n");
6312 setquirk(const char *class, const char *name
, unsigned long quirk
)
6316 DNPRINTF(SWM_D_QUIRK
, "setquirk: enter %s:%s [%lu]\n", class, name
,
6319 /* Remove/replace existing quirk. */
6320 TAILQ_FOREACH(qp
, &quirks
, entry
) {
6321 if (!strcmp(qp
->class, class) && !strcmp(qp
->name
, name
)) {
6325 quirk_replace(qp
, class, name
, quirk
);
6326 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
6331 /* Only insert if quirk is not NONE. */
6333 quirk_insert(class, name
, quirk
);
6335 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
6339 setconfquirk(char *selector
, char *value
, int flags
)
6341 char *cp
, *class, *name
;
6345 /* suppress unused warning since var is needed */
6348 if (selector
== NULL
)
6350 if ((cp
= strchr(selector
, ':')) == NULL
)
6355 if ((retval
= parsequirks(value
, &qrks
)) == 0)
6356 setquirk(class, name
, qrks
);
6363 setquirk("MPlayer", "xv", SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
| SWM_Q_FOCUSPREV
);
6364 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT
);
6365 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ
);
6366 setquirk("Firefox", "Dialog", SWM_Q_FLOAT
);
6367 setquirk("Gimp", "gimp", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
6368 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ
);
6369 setquirk("xine", "Xine Window", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
6370 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
6371 setquirk("xine", "xine Panel", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
6372 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
6373 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN
| SWM_Q_FLOAT
);
6374 setquirk("pcb", "pcb", SWM_Q_FLOAT
);
6375 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
);
6378 /* conf file stuff */
6379 #define SWM_CONF_FILE "spectrwm.conf"
6380 #define SWM_CONF_FILE_OLD "scrotwm.conf"
6384 SWM_S_BAR_AT_BOTTOM
,
6385 SWM_S_BAR_BORDER_WIDTH
,
6392 SWM_S_CLOCK_ENABLED
,
6395 SWM_S_CYCLE_VISIBLE
,
6397 SWM_S_DISABLE_BORDER
,
6399 SWM_S_FOCUS_CLOSE_WRAP
,
6400 SWM_S_FOCUS_DEFAULT
,
6406 SWM_S_STACK_ENABLED
,
6408 SWM_S_TITLE_CLASS_ENABLED
,
6409 SWM_S_TITLE_NAME_ENABLED
,
6410 SWM_S_URGENT_ENABLED
,
6411 SWM_S_VERBOSE_LAYOUT
,
6412 SWM_S_WINDOW_NAME_ENABLED
,
6413 SWM_S_WORKSPACE_LIMIT
6417 setconfvalue(char *selector
, char *value
, int flags
)
6422 /* suppress unused warning since var is needed */
6426 case SWM_S_BAR_ACTION
:
6428 if ((bar_argv
[0] = expand_tilde(value
)) == NULL
)
6429 err(1, "setconfvalue: bar_action");
6431 case SWM_S_BAR_AT_BOTTOM
:
6432 bar_at_bottom
= atoi(value
);
6434 case SWM_S_BAR_BORDER_WIDTH
:
6435 bar_border_width
= atoi(value
);
6436 if (bar_border_width
< 0)
6437 bar_border_width
= 0;
6439 case SWM_S_BAR_DELAY
:
6440 bar_delay
= atoi(value
);
6442 case SWM_S_BAR_ENABLED
:
6443 bar_enabled
= atoi(value
);
6445 case SWM_S_BAR_FONT
:
6447 if (asprintf(&bar_fonts
, "%s,%s", value
, bar_fonts
) == -1)
6448 err(1, "setconfvalue: asprintf: failed to allocate "
6449 "memory for bar_fonts.");
6452 /* If already in xft mode, then we are done. */
6453 if (!bar_font_legacy
)
6456 /* If there are any non-XLFD entries, switch to Xft mode. */
6457 while ((b
= strsep(&value
, ",")) != NULL
) {
6461 bar_font_legacy
= 0;
6466 case SWM_S_BAR_FORMAT
:
6468 if ((bar_format
= strdup(value
)) == NULL
)
6469 err(1, "setconfvalue: bar_format");
6471 case SWM_S_BAR_JUSTIFY
:
6472 if (!strcmp(value
, "left"))
6473 bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
6474 else if (!strcmp(value
, "center"))
6475 bar_justify
= SWM_BAR_JUSTIFY_CENTER
;
6476 else if (!strcmp(value
, "right"))
6477 bar_justify
= SWM_BAR_JUSTIFY_RIGHT
;
6479 errx(1, "invalid bar_justify");
6481 case SWM_S_BORDER_WIDTH
:
6482 border_width
= atoi(value
);
6483 if (border_width
< 0)
6486 case SWM_S_CLOCK_ENABLED
:
6487 clock_enabled
= atoi(value
);
6489 case SWM_S_CLOCK_FORMAT
:
6490 #ifndef SWM_DENY_CLOCK_FORMAT
6492 if ((clock_format
= strdup(value
)) == NULL
)
6493 err(1, "setconfvalue: clock_format");
6496 case SWM_S_CYCLE_EMPTY
:
6497 cycle_empty
= atoi(value
);
6499 case SWM_S_CYCLE_VISIBLE
:
6500 cycle_visible
= atoi(value
);
6502 case SWM_S_DIALOG_RATIO
:
6503 dialog_ratio
= atof(value
);
6504 if (dialog_ratio
> 1.0 || dialog_ratio
<= .3)
6507 case SWM_S_DISABLE_BORDER
:
6508 disable_border
= atoi(value
);
6510 case SWM_S_FOCUS_CLOSE
:
6511 if (!strcmp(value
, "first"))
6512 focus_close
= SWM_STACK_BOTTOM
;
6513 else if (!strcmp(value
, "last"))
6514 focus_close
= SWM_STACK_TOP
;
6515 else if (!strcmp(value
, "next"))
6516 focus_close
= SWM_STACK_ABOVE
;
6517 else if (!strcmp(value
, "previous"))
6518 focus_close
= SWM_STACK_BELOW
;
6520 errx(1, "focus_close");
6522 case SWM_S_FOCUS_CLOSE_WRAP
:
6523 focus_close_wrap
= atoi(value
);
6525 case SWM_S_FOCUS_DEFAULT
:
6526 if (!strcmp(value
, "last"))
6527 focus_default
= SWM_STACK_TOP
;
6528 else if (!strcmp(value
, "first"))
6529 focus_default
= SWM_STACK_BOTTOM
;
6531 errx(1, "focus_default");
6533 case SWM_S_FOCUS_MODE
:
6534 if (!strcmp(value
, "default"))
6535 focus_mode
= SWM_FOCUS_DEFAULT
;
6536 else if (!strcmp(value
, "follow") ||
6537 !strcmp(value
, "follow_cursor"))
6538 focus_mode
= SWM_FOCUS_FOLLOW
;
6539 else if (!strcmp(value
, "manual"))
6540 focus_mode
= SWM_FOCUS_MANUAL
;
6542 errx(1, "focus_mode");
6544 case SWM_S_SPAWN_ORDER
:
6545 if (!strcmp(value
, "first"))
6546 spawn_position
= SWM_STACK_BOTTOM
;
6547 else if (!strcmp(value
, "last"))
6548 spawn_position
= SWM_STACK_TOP
;
6549 else if (!strcmp(value
, "next"))
6550 spawn_position
= SWM_STACK_ABOVE
;
6551 else if (!strcmp(value
, "previous"))
6552 spawn_position
= SWM_STACK_BELOW
;
6554 errx(1, "spawn_position");
6556 case SWM_S_SPAWN_TERM
:
6557 setconfspawn("term", value
, 0);
6558 setconfspawn("spawn_term", value
, 0);
6562 case SWM_S_SS_ENABLED
:
6563 ss_enabled
= atoi(value
);
6565 case SWM_S_STACK_ENABLED
:
6566 stack_enabled
= atoi(value
);
6568 case SWM_S_TERM_WIDTH
:
6569 term_width
= atoi(value
);
6573 case SWM_S_TITLE_CLASS_ENABLED
:
6574 title_class_enabled
= atoi(value
);
6576 case SWM_S_TITLE_NAME_ENABLED
:
6577 title_name_enabled
= atoi(value
);
6579 case SWM_S_URGENT_ENABLED
:
6580 urgent_enabled
= atoi(value
);
6582 case SWM_S_VERBOSE_LAYOUT
:
6583 verbose_layout
= atoi(value
);
6584 for (i
= 0; layouts
[i
].l_stack
!= NULL
; i
++) {
6586 layouts
[i
].l_string
= fancy_stacker
;
6588 layouts
[i
].l_string
= plain_stacker
;
6591 case SWM_S_WINDOW_NAME_ENABLED
:
6592 window_name_enabled
= atoi(value
);
6594 case SWM_S_WORKSPACE_LIMIT
:
6595 workspace_limit
= atoi(value
);
6596 if (workspace_limit
> SWM_WS_MAX
)
6597 workspace_limit
= SWM_WS_MAX
;
6598 else if (workspace_limit
< 1)
6599 workspace_limit
= 1;
6608 setconfmodkey(char *selector
, char *value
, int flags
)
6610 /* suppress unused warnings since vars are needed */
6614 if (!strncasecmp(value
, "Mod1", strlen("Mod1")))
6615 update_modkey(XCB_MOD_MASK_1
);
6616 else if (!strncasecmp(value
, "Mod2", strlen("Mod2")))
6617 update_modkey(XCB_MOD_MASK_2
);
6618 else if (!strncasecmp(value
, "Mod3", strlen("Mod3")))
6619 update_modkey(XCB_MOD_MASK_3
);
6620 else if (!strncasecmp(value
, "Mod4", strlen("Mod4")))
6621 update_modkey(XCB_MOD_MASK_4
);
6628 setconfcolor(char *selector
, char *value
, int flags
)
6630 setscreencolor(value
, ((selector
== NULL
)?-1:atoi(selector
)), flags
);
6635 setconfregion(char *selector
, char *value
, int flags
)
6637 /* suppress unused warnings since vars are needed */
6641 custom_region(value
);
6646 setautorun(char *selector
, char *value
, int flags
)
6656 /* suppress unused warnings since vars are needed */
6660 if (getenv("SWM_STARTED"))
6664 if (sscanf(value
, "ws[%d]:%1023c", &ws_id
, s
) != 2)
6665 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
6667 if (ws_id
< 0 || ws_id
>= workspace_limit
)
6668 errx(1, "autorun: invalid workspace %d", ws_id
+ 1);
6670 sp
= expand_tilde((char *)&s
);
6673 * This is a little intricate
6675 * If the pid already exists we simply reuse it because it means it was
6676 * used before AND not claimed by manage_window. We get away with
6677 * altering it in the parent after INSERT because this can not be a race
6680 while ((ap
= strsep(&sp
, " \t")) != NULL
) {
6683 DNPRINTF(SWM_D_SPAWN
, "setautorun: arg [%s]\n", ap
);
6685 if ((a
.argv
= realloc(a
.argv
, argc
* sizeof(char *))) == NULL
)
6686 err(1, "setautorun: realloc");
6687 a
.argv
[argc
- 1] = ap
;
6691 if ((a
.argv
= realloc(a
.argv
, (argc
+ 1) * sizeof(char *))) == NULL
)
6692 err(1, "setautorun: realloc");
6693 a
.argv
[argc
] = NULL
;
6695 if ((pid
= fork()) == 0) {
6696 spawn(ws_id
, &a
, 1);
6705 p
= calloc(1, sizeof *p
);
6708 TAILQ_INSERT_TAIL(&pidlist
, p
, entry
);
6718 setlayout(char *selector
, char *value
, int flags
)
6720 int ws_id
, i
, x
, mg
, ma
, si
, ar
, f
= 0;
6721 int st
= SWM_V_STACK
, num_screens
;
6723 struct workspace
*ws
;
6725 /* suppress unused warnings since vars are needed */
6729 if (getenv("SWM_STARTED"))
6733 if (sscanf(value
, "ws[%d]:%d:%d:%d:%d:%1023c",
6734 &ws_id
, &mg
, &ma
, &si
, &ar
, s
) != 6)
6735 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
6736 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
6739 if (ws_id
< 0 || ws_id
>= workspace_limit
)
6740 errx(1, "layout: invalid workspace %d", ws_id
+ 1);
6742 if (!strcasecmp(s
, "vertical"))
6744 else if (!strcasecmp(s
, "vertical_flip")) {
6747 } else if (!strcasecmp(s
, "horizontal"))
6749 else if (!strcasecmp(s
, "horizontal_flip")) {
6752 } else if (!strcasecmp(s
, "fullscreen"))
6755 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
6756 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
6759 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
6760 for (i
= 0; i
< num_screens
; i
++) {
6761 ws
= (struct workspace
*)&screens
[i
].ws
;
6762 ws
[ws_id
].cur_layout
= &layouts
[st
];
6764 ws
[ws_id
].always_raise
= ar
;
6765 if (st
== SWM_MAX_STACK
)
6769 for (x
= 0; x
< abs(mg
); x
++) {
6770 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
6771 mg
>= 0 ? SWM_ARG_ID_MASTERGROW
:
6772 SWM_ARG_ID_MASTERSHRINK
);
6776 for (x
= 0; x
< abs(ma
); x
++) {
6777 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
6778 ma
>= 0 ? SWM_ARG_ID_MASTERADD
:
6779 SWM_ARG_ID_MASTERDEL
);
6783 for (x
= 0; x
< abs(si
); x
++) {
6784 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
6785 si
>= 0 ? SWM_ARG_ID_STACKINC
:
6786 SWM_ARG_ID_STACKDEC
);
6791 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
6792 SWM_ARG_ID_FLIPLAYOUT
);
6802 /* config options */
6803 struct config_option
{
6805 int (*func
)(char*, char*, int);
6808 struct config_option configopt
[] = {
6809 { "bar_enabled", setconfvalue
, SWM_S_BAR_ENABLED
},
6810 { "bar_at_bottom", setconfvalue
, SWM_S_BAR_AT_BOTTOM
},
6811 { "bar_border", setconfcolor
, SWM_S_COLOR_BAR_BORDER
},
6812 { "bar_border_width", setconfvalue
, SWM_S_BAR_BORDER_WIDTH
},
6813 { "bar_color", setconfcolor
, SWM_S_COLOR_BAR
},
6814 { "bar_font_color", setconfcolor
, SWM_S_COLOR_BAR_FONT
},
6815 { "bar_font", setconfvalue
, SWM_S_BAR_FONT
},
6816 { "bar_action", setconfvalue
, SWM_S_BAR_ACTION
},
6817 { "bar_delay", setconfvalue
, SWM_S_BAR_DELAY
},
6818 { "bar_justify", setconfvalue
, SWM_S_BAR_JUSTIFY
},
6819 { "bar_format", setconfvalue
, SWM_S_BAR_FORMAT
},
6820 { "keyboard_mapping", setkeymapping
, 0 },
6821 { "bind", setconfbinding
, 0 },
6822 { "stack_enabled", setconfvalue
, SWM_S_STACK_ENABLED
},
6823 { "clock_enabled", setconfvalue
, SWM_S_CLOCK_ENABLED
},
6824 { "clock_format", setconfvalue
, SWM_S_CLOCK_FORMAT
},
6825 { "color_focus", setconfcolor
, SWM_S_COLOR_FOCUS
},
6826 { "color_unfocus", setconfcolor
, SWM_S_COLOR_UNFOCUS
},
6827 { "cycle_empty", setconfvalue
, SWM_S_CYCLE_EMPTY
},
6828 { "cycle_visible", setconfvalue
, SWM_S_CYCLE_VISIBLE
},
6829 { "workspace_limit", setconfvalue
, SWM_S_WORKSPACE_LIMIT
},
6830 { "dialog_ratio", setconfvalue
, SWM_S_DIALOG_RATIO
},
6831 { "verbose_layout", setconfvalue
, SWM_S_VERBOSE_LAYOUT
},
6832 { "modkey", setconfmodkey
, 0 },
6833 { "program", setconfspawn
, 0 },
6834 { "quirk", setconfquirk
, 0 },
6835 { "region", setconfregion
, 0 },
6836 { "spawn_term", setconfvalue
, SWM_S_SPAWN_TERM
},
6837 { "screenshot_enabled", setconfvalue
, SWM_S_SS_ENABLED
},
6838 { "screenshot_app", setconfvalue
, SWM_S_SS_APP
},
6839 { "window_name_enabled", setconfvalue
, SWM_S_WINDOW_NAME_ENABLED
},
6840 { "urgent_enabled", setconfvalue
, SWM_S_URGENT_ENABLED
},
6841 { "term_width", setconfvalue
, SWM_S_TERM_WIDTH
},
6842 { "title_class_enabled", setconfvalue
, SWM_S_TITLE_CLASS_ENABLED
},
6843 { "title_name_enabled", setconfvalue
, SWM_S_TITLE_NAME_ENABLED
},
6844 { "focus_mode", setconfvalue
, SWM_S_FOCUS_MODE
},
6845 { "focus_close", setconfvalue
, SWM_S_FOCUS_CLOSE
},
6846 { "focus_close_wrap", setconfvalue
, SWM_S_FOCUS_CLOSE_WRAP
},
6847 { "focus_default", setconfvalue
, SWM_S_FOCUS_DEFAULT
},
6848 { "spawn_position", setconfvalue
, SWM_S_SPAWN_ORDER
},
6849 { "disable_border", setconfvalue
, SWM_S_DISABLE_BORDER
},
6850 { "border_width", setconfvalue
, SWM_S_BORDER_WIDTH
},
6851 { "autorun", setautorun
, 0 },
6852 { "layout", setlayout
, 0 },
6856 conf_load(const char *filename
, int keymapping
)
6859 char *line
, *cp
, *optsub
, *optval
;
6860 size_t linelen
, lineno
= 0;
6861 int wordlen
, i
, optidx
;
6862 struct config_option
*opt
= NULL
;
6864 DNPRINTF(SWM_D_CONF
, "conf_load: begin\n");
6866 if (filename
== NULL
) {
6867 warnx("conf_load: no filename");
6870 if ((config
= fopen(filename
, "r")) == NULL
) {
6871 warn("conf_load: fopen: %s", filename
);
6875 while (!feof(config
)) {
6876 if ((line
= fparseln(config
, &linelen
, &lineno
, NULL
, 0))
6879 err(1, "%s", filename
);
6884 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
6885 if (cp
[0] == '\0') {
6890 /* get config option */
6891 wordlen
= strcspn(cp
, "=[ \t\n");
6893 warnx("%s: line %zd: no option found",
6898 for (i
= 0; i
< LENGTH(configopt
); i
++) {
6899 opt
= &configopt
[i
];
6900 if (!strncasecmp(cp
, opt
->optname
, wordlen
) &&
6901 (int)strlen(opt
->optname
) == wordlen
) {
6907 warnx("%s: line %zd: unknown option %.*s",
6908 filename
, lineno
, wordlen
, cp
);
6911 if (keymapping
&& opt
&& strcmp(opt
->optname
, "bind")) {
6912 warnx("%s: line %zd: invalid option %.*s",
6913 filename
, lineno
, wordlen
, cp
);
6917 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
6918 /* get [selector] if any */
6922 wordlen
= strcspn(cp
, "]");
6925 warnx("%s: line %zd: syntax error",
6930 if (asprintf(&optsub
, "%.*s", wordlen
, cp
) ==
6932 warnx("%s: line %zd: unable to allocate"
6933 "memory for selector", filename
,
6939 cp
+= strspn(cp
, "] \t\n"); /* eat trailing */
6941 cp
+= strspn(cp
, "= \t\n"); /* eat trailing */
6943 optval
= strdup(cp
);
6944 /* call function to deal with it all */
6945 if (configopt
[optidx
].func(optsub
, optval
,
6946 configopt
[optidx
].funcflags
) != 0)
6947 errx(1, "%s: line %zd: invalid data for %s",
6948 filename
, lineno
, configopt
[optidx
].optname
);
6955 DNPRINTF(SWM_D_CONF
, "conf_load: end\n");
6962 DNPRINTF(SWM_D_CONF
, "conf_load: end with error.\n");
6968 set_child_transient(struct ws_win
*win
, xcb_window_t
*trans
)
6970 struct ws_win
*parent
, *w
;
6971 struct swm_region
*r
;
6972 struct workspace
*ws
;
6973 xcb_icccm_wm_hints_t wmh
;
6975 parent
= find_window(win
->transient
);
6977 parent
->focus_child
= win
;
6979 DNPRINTF(SWM_D_MISC
, "set_child_transient: parent doesn't exist"
6980 " for 0x%x trans 0x%x\n", win
->id
, win
->transient
);
6982 r
= root_to_region(win
->wa
->root
, SWM_CK_ALL
);
6984 /* parent doen't exist in our window list */
6985 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
6986 if (xcb_icccm_get_wm_hints_reply(conn
,
6987 xcb_icccm_get_wm_hints(conn
, w
->id
),
6989 warnx("can't get hints for 0x%x", w
->id
);
6993 if (win
->hints
.window_group
!= wmh
.window_group
)
6996 w
->focus_child
= win
;
6997 win
->transient
= w
->id
;
6999 DNPRINTF(SWM_D_MISC
, "set_child_transient: adjusting "
7000 "transient to 0x%x\n", win
->transient
);
7007 window_get_pid(xcb_window_t win
)
7012 xcb_get_property_cookie_t pc
;
7013 xcb_get_property_reply_t
*pr
;
7015 apid
= get_atom_from_string("_NET_WM_PID");
7016 if (apid
== XCB_ATOM_NONE
)
7019 pc
= xcb_get_property(conn
, 0, win
, apid
, XCB_ATOM_CARDINAL
, 0, 1);
7020 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
7023 if (pr
->type
!= XCB_ATOM_CARDINAL
) {
7028 if (pr
->type
== apid
&& pr
->format
== 32)
7029 ret
= *((pid_t
*)xcb_get_property_value(pr
));
7035 apid
= get_atom_from_string("_SWM_PID");
7036 pc
= xcb_get_property(conn
, 0, win
, apid
, XCB_ATOM_STRING
,
7038 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
7041 if (pr
->type
!= apid
) {
7046 ret
= (pid_t
)strtonum(xcb_get_property_value(pr
), 0, INT_MAX
, &errstr
);
7053 get_ws_idx(xcb_window_t id
)
7059 xcb_get_property_reply_t
*gpr
;
7061 gpr
= xcb_get_property_reply(conn
,
7062 xcb_get_property(conn
, 0, id
, a_swm_ws
,
7063 XCB_ATOM_STRING
, 0, SWM_PROPLEN
),
7068 proplen
= xcb_get_property_value_length(gpr
);
7070 prop
= malloc(proplen
+ 1);
7073 xcb_get_property_value(gpr
),
7075 prop
[proplen
] = '\0';
7082 DNPRINTF(SWM_D_PROP
, "get_ws_idx: _SWM_WS: %s\n", prop
);
7083 ws_idx
= (int)strtonum(prop
, 0, workspace_limit
- 1, &errstr
);
7085 DNPRINTF(SWM_D_PROP
, "get_ws_idx: window: #%s: %s",
7095 manage_window(xcb_window_t id
, uint16_t mapped
)
7097 xcb_window_t trans
= XCB_WINDOW_NONE
;
7098 struct ws_win
*win
, *ww
;
7100 char ws_idx_str
[SWM_PROPLEN
];
7101 struct swm_region
*r
;
7105 xcb_icccm_get_wm_protocols_reply_t wpr
;
7107 if ((win
= find_window(id
)) != NULL
) {
7108 DNPRINTF(SWM_D_MISC
, "manage_window: win 0x%x already "
7109 "managed; skipping.)\n", id
);
7110 return (win
); /* Already managed. */
7113 /* See if window is on the unmanaged list. */
7114 if ((win
= find_unmanaged_window(id
)) != NULL
) {
7115 DNPRINTF(SWM_D_MISC
, "manage_window: win 0x%x found on "
7116 "unmanaged list.\n", id
);
7117 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
7120 set_child_transient(win
, &trans
);
7124 DNPRINTF(SWM_D_MISC
, "manage_window: win 0x%x is new.\n", id
);
7127 /* Create and initialize ws_win object. */
7128 if ((win
= calloc(1, sizeof(struct ws_win
))) == NULL
)
7129 err(1, "manage_window: calloc: failed to allocate memory for "
7134 /* Get window geometry. */
7135 win
->wa
= xcb_get_geometry_reply(conn
,
7136 xcb_get_geometry(conn
, win
->id
),
7139 /* Figure out which region the window belongs to. */
7140 r
= root_to_region(win
->wa
->root
, SWM_CK_ALL
);
7142 /* Ignore window border if there is one. */
7143 WIDTH(win
) = win
->wa
->width
;
7144 HEIGHT(win
) = win
->wa
->height
;
7145 X(win
) = win
->wa
->x
+ win
->wa
->border_width
- border_width
;
7146 Y(win
) = win
->wa
->y
+ win
->wa
->border_width
- border_width
;
7148 win
->mapped
= mapped
;
7149 win
->floatmaxed
= 0;
7150 win
->ewmh_flags
= 0;
7151 win
->s
= r
->s
; /* this never changes */
7153 store_float_geom(win
, r
);
7155 /* Get WM_SIZE_HINTS. */
7156 xcb_icccm_get_wm_normal_hints_reply(conn
,
7157 xcb_icccm_get_wm_normal_hints(conn
, win
->id
),
7161 xcb_icccm_get_wm_hints_reply(conn
,
7162 xcb_icccm_get_wm_hints(conn
, win
->id
),
7165 /* Get WM_TRANSIENT_FOR; see if window is a transient. */
7166 xcb_icccm_get_wm_transient_for_reply(conn
,
7167 xcb_icccm_get_wm_transient_for(conn
, win
->id
),
7170 win
->transient
= trans
;
7171 set_child_transient(win
, &win
->transient
);
7174 /* Get supported protocols. */
7175 if (xcb_icccm_get_wm_protocols_reply(conn
,
7176 xcb_icccm_get_wm_protocols(conn
, win
->id
, a_prot
),
7178 for (i
= 0; i
< wpr
.atoms_len
; i
++) {
7179 if (wpr
.atoms
[i
] == a_takefocus
)
7180 win
->take_focus
= 1;
7181 if (wpr
.atoms
[i
] == a_delete
)
7182 win
->can_delete
= 1;
7184 xcb_icccm_get_wm_protocols_reply_wipe(&wpr
);
7187 win
->iconic
= get_swm_iconic(win
);
7189 /* Figure out which workspace the window belongs to. */
7190 if ((p
= find_pid(window_get_pid(win
->id
))) != NULL
) {
7191 win
->ws
= &r
->s
->ws
[p
->ws
];
7192 TAILQ_REMOVE(&pidlist
, p
, entry
);
7195 } else if ((ws_idx
= get_ws_idx(win
->id
)) != -1 &&
7197 /* _SWM_WS is set; use that. */
7198 win
->ws
= &r
->s
->ws
[ws_idx
];
7199 } else if (trans
&& (ww
= find_window(trans
)) != NULL
) {
7200 /* Launch transients in the same ws as parent. */
7206 /* Set the _SWM_WS atom so we can remember this after reincarnation. */
7207 if (snprintf(ws_idx_str
, SWM_PROPLEN
, "%d", win
->ws
->idx
) <
7209 DNPRINTF(SWM_D_PROP
, "manage_window: set _SWM_WS: %s\n",
7211 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
7212 a_swm_ws
, XCB_ATOM_STRING
, 8, strlen(ws_idx_str
),
7217 ewmh_autoquirk(win
);
7219 /* Determine initial quirks. */
7220 if (xcb_icccm_get_wm_class_reply(conn
,
7221 xcb_icccm_get_wm_class(conn
, win
->id
),
7223 DNPRINTF(SWM_D_CLASS
, "manage_window: class: %s, name: %s\n",
7224 win
->ch
.class_name
, win
->ch
.instance_name
);
7226 /* java is retarded so treat it special */
7227 if (strstr(win
->ch
.instance_name
, "sun-awt")) {
7228 DNPRINTF(SWM_D_CLASS
, "manage_window: java window "
7233 TAILQ_FOREACH(qp
, &quirks
, entry
) {
7234 if (!strcmp(win
->ch
.class_name
, qp
->class) &&
7235 !strcmp(win
->ch
.instance_name
, qp
->name
)) {
7236 DNPRINTF(SWM_D_CLASS
, "manage_window: on quirks"
7237 "list; mask: 0x%lx\n", qp
->quirk
);
7238 if (qp
->quirk
& SWM_Q_FLOAT
)
7240 win
->quirks
= qp
->quirk
;
7245 /* Alter window position if quirky */
7246 if (win
->quirks
& SWM_Q_ANYWHERE
)
7249 /* Reset font sizes (the bruteforce way; no default keybinding). */
7250 if (win
->quirks
& SWM_Q_XTERM_FONTADJ
) {
7251 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
7252 fake_keypress(win
, XK_KP_Subtract
, XCB_MOD_MASK_SHIFT
);
7253 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
7254 fake_keypress(win
, XK_KP_Add
, XCB_MOD_MASK_SHIFT
);
7257 /* Make sure window is positioned inside its region, if its active. */
7259 constrain_window(win
, win
->ws
->r
, 0);
7263 /* Select which X events to monitor and set border pixel color. */
7264 wa
[0] = win
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
;
7265 wa
[1] = XCB_EVENT_MASK_ENTER_WINDOW
| XCB_EVENT_MASK_PROPERTY_CHANGE
|
7266 XCB_EVENT_MASK_STRUCTURE_NOTIFY
;
7268 wa
[1] |= XCB_EVENT_MASK_LEAVE_WINDOW
| XCB_EVENT_MASK_FOCUS_CHANGE
;
7271 xcb_change_window_attributes(conn
, win
->id
, XCB_CW_BORDER_PIXEL
|
7272 XCB_CW_EVENT_MASK
, wa
);
7275 /* Figure out where to stack the window in the workspace. */
7276 if (trans
&& (ww
= find_window(trans
)))
7277 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, ww
, win
, entry
);
7278 else if (win
->ws
->focus
&& spawn_position
== SWM_STACK_ABOVE
)
7279 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, win
->ws
->focus
, win
,
7281 else if (win
->ws
->focus
&& spawn_position
== SWM_STACK_BELOW
)
7282 TAILQ_INSERT_BEFORE(win
->ws
->focus
, win
, entry
);
7283 else switch (spawn_position
) {
7286 case SWM_STACK_ABOVE
:
7287 TAILQ_INSERT_TAIL(&win
->ws
->winlist
, win
, entry
);
7289 case SWM_STACK_BOTTOM
:
7290 case SWM_STACK_BELOW
:
7291 TAILQ_INSERT_HEAD(&win
->ws
->winlist
, win
, entry
);
7294 /* Get initial _NET_WM_STATE */
7295 ewmh_get_win_state(win
);
7296 /* Set initial _NET_WM_ALLOWED_ACTIONS */
7297 ewmh_update_actions(win
);
7301 DNPRINTF(SWM_D_MISC
, "manage_window: done. window: 0x%x, (x,y) w x h: "
7302 "(%d,%d) %d x %d, ws: %d, iconic: %s, transient: 0x%x\n", win
->id
,
7303 X(win
), Y(win
), WIDTH(win
), HEIGHT(win
), win
->ws
->idx
,
7304 YESNO(win
->iconic
), win
->transient
);
7310 free_window(struct ws_win
*win
)
7312 DNPRINTF(SWM_D_MISC
, "free_window: window: 0x%x\n", win
->id
);
7317 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
7322 xcb_icccm_get_wm_class_reply_wipe(&win
->ch
);
7327 memset(win
, 0xff, sizeof *win
); /* XXX kill later */
7330 DNPRINTF(SWM_D_MISC
, "free_window: done.\n");
7334 unmanage_window(struct ws_win
*win
)
7336 struct ws_win
*parent
;
7341 DNPRINTF(SWM_D_MISC
, "unmanage_window: window: 0x%x\n", win
->id
);
7343 if (win
->transient
) {
7344 parent
= find_window(win
->transient
);
7346 parent
->focus_child
= NULL
;
7349 TAILQ_REMOVE(&win
->ws
->winlist
, win
, entry
);
7350 TAILQ_INSERT_TAIL(&win
->ws
->unmanagedlist
, win
, entry
);
7354 expose(xcb_expose_event_t
*e
)
7357 struct swm_region
*r
;
7359 DNPRINTF(SWM_D_EVENT
, "expose: window: 0x%x\n", e
->window
);
7361 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7362 for (i
= 0; i
< num_screens
; i
++)
7363 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
7364 if (e
->window
== WINID(r
->bar
))
7372 focusin(xcb_focus_in_event_t
*e
)
7374 DNPRINTF(SWM_D_EVENT
, "focusin: window: 0x%x, mode: %s(%u), "
7375 "detail: %s(%u)\n", e
->event
, get_notify_mode_label(e
->mode
),
7376 e
->mode
, get_notify_detail_label(e
->detail
), e
->detail
);
7380 focusout(xcb_focus_out_event_t
*e
)
7382 DNPRINTF(SWM_D_EVENT
, "focusout: window: 0x%x, mode: %s(%u), "
7383 "detail: %s(%u)\n", e
->event
, get_notify_mode_label(e
->mode
),
7384 e
->mode
, get_notify_detail_label(e
->detail
), e
->detail
);
7389 keypress(xcb_key_press_event_t
*e
)
7391 xcb_keysym_t keysym
;
7394 keysym
= xcb_key_press_lookup_keysym(syms
, e
, 0);
7396 DNPRINTF(SWM_D_EVENT
, "keypress: %u %u\n", e
->detail
, keysym
);
7398 if ((kp
= key_lookup(CLEANMASK(e
->state
), keysym
)) == NULL
)
7401 last_event_time
= e
->time
;
7403 if (kp
->funcid
== KF_SPAWN_CUSTOM
)
7404 spawn_custom(root_to_region(e
->root
, SWM_CK_ALL
),
7405 &(keyfuncs
[kp
->funcid
].args
), kp
->spawn_name
);
7406 else if (keyfuncs
[kp
->funcid
].func
)
7407 keyfuncs
[kp
->funcid
].func(root_to_region(e
->root
, SWM_CK_ALL
),
7408 &(keyfuncs
[kp
->funcid
].args
));
7412 buttonpress(xcb_button_press_event_t
*e
)
7414 struct ws_win
*win
= NULL
;
7415 struct swm_region
*r
, *old_r
;
7419 DNPRINTF(SWM_D_EVENT
, "buttonpress: win (x,y): 0x%x (%d,%d), "
7420 "detail: %u, time: %u, root (x,y): 0x%x (%d,%d), child: 0x%x, "
7421 "state: %u, same_screen: %s\n", e
->event
, e
->event_x
, e
->event_y
,
7422 e
->detail
, e
->time
, e
->root
, e
->root_x
, e
->root_y
, e
->child
,
7423 e
->state
, YESNO(e
->same_screen
));
7425 if (e
->event
== e
->root
) {
7426 if (e
->child
!= 0) {
7427 win
= find_window(e
->child
);
7428 /* Pass ButtonPress to window if it isn't managed. */
7432 /* Focus on empty region */
7433 /* If no windows on region if its empty. */
7434 r
= root_to_region(e
->root
, SWM_CK_POINTER
);
7435 if (r
&& TAILQ_EMPTY(&r
->ws
->winlist
)) {
7436 old_r
= root_to_region(e
->root
, SWM_CK_FOCUS
);
7437 if (old_r
&& old_r
!= r
)
7438 unfocus_win(old_r
->ws
->focus
);
7440 xcb_set_input_focus(conn
,
7441 XCB_INPUT_FOCUS_PARENT
, e
->root
, e
->time
);
7443 /* Clear bar since empty. */
7451 win
= find_window(e
->event
);
7457 last_event_time
= e
->time
;
7459 focus_win(get_focus_magic(win
));
7461 for (i
= 0; i
< LENGTH(buttons
); i
++)
7462 if (client_click
== buttons
[i
].action
&& buttons
[i
].func
&&
7463 buttons
[i
].button
== e
->detail
&&
7464 CLEANMASK(buttons
[i
].mask
) == CLEANMASK(e
->state
)) {
7465 buttons
[i
].func(win
, &buttons
[i
].args
);
7471 DNPRINTF(SWM_D_EVENT
, "buttonpress: passing to window.\n");
7472 /* Replay event to event window */
7473 xcb_allow_events(conn
, XCB_ALLOW_REPLAY_POINTER
, e
->time
);
7475 DNPRINTF(SWM_D_EVENT
, "buttonpress: handled.\n");
7476 /* Unfreeze grab events. */
7477 xcb_allow_events(conn
, XCB_ALLOW_SYNC_POINTER
, e
->time
);
7485 print_win_geom(xcb_window_t w
)
7487 xcb_get_geometry_reply_t
*wa
;
7489 wa
= xcb_get_geometry_reply(conn
, xcb_get_geometry(conn
, w
), NULL
);
7492 DNPRINTF(SWM_D_MISC
, "print_win_geom: window not found: 0x%x\n",
7497 DNPRINTF(SWM_D_MISC
, "print_win_geom: window: 0x%x, root: 0x%x, "
7498 "depth: %u, (x,y) w x h: (%d,%d) %d x %d, border: %d\n",
7499 w
, wa
->root
, wa
->depth
, wa
->x
, wa
->y
, wa
->width
, wa
->height
,
7508 get_stack_mode_name(uint8_t mode
)
7513 case XCB_STACK_MODE_ABOVE
:
7516 case XCB_STACK_MODE_BELOW
:
7519 case XCB_STACK_MODE_TOP_IF
:
7522 case XCB_STACK_MODE_BOTTOM_IF
:
7525 case XCB_STACK_MODE_OPPOSITE
:
7537 configurerequest(xcb_configure_request_event_t
*e
)
7540 struct swm_region
*r
= NULL
;
7543 uint32_t wc
[7] = {0};
7545 if ((win
= find_window(e
->window
)) == NULL
)
7546 if ((win
= find_unmanaged_window(e
->window
)) == NULL
)
7550 if (swm_debug
& SWM_D_EVENT
) {
7551 print_win_geom(e
->window
);
7553 DNPRINTF(SWM_D_EVENT
, "configurerequest: window: 0x%x, "
7554 "parent: 0x%x, new: %s, value_mask: %u { ", e
->window
,
7555 e
->parent
, YESNO(new), e
->value_mask
);
7556 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
)
7557 DPRINTF("X: %d ", e
->x
);
7558 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
)
7559 DPRINTF("Y: %d ", e
->y
);
7560 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
)
7561 DPRINTF("W: %u ", e
->width
);
7562 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
)
7563 DPRINTF("H: %u ", e
->height
);
7564 if (e
->value_mask
& XCB_CONFIG_WINDOW_BORDER_WIDTH
)
7565 DPRINTF("Border: %u ", e
->border_width
);
7566 if (e
->value_mask
& XCB_CONFIG_WINDOW_SIBLING
)
7567 DPRINTF("Sibling: 0x%x ", e
->sibling
);
7568 if (e
->value_mask
& XCB_CONFIG_WINDOW_STACK_MODE
)
7569 DPRINTF("StackMode: %s(%u) ",
7570 get_stack_mode_name(e
->stack_mode
), e
->stack_mode
);
7576 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
) {
7577 mask
|= XCB_CONFIG_WINDOW_X
;
7580 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
) {
7581 mask
|= XCB_CONFIG_WINDOW_Y
;
7584 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
) {
7585 mask
|= XCB_CONFIG_WINDOW_WIDTH
;
7588 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
) {
7589 mask
|= XCB_CONFIG_WINDOW_HEIGHT
;
7590 wc
[i
++] = e
->height
;
7592 if (e
->value_mask
& XCB_CONFIG_WINDOW_BORDER_WIDTH
) {
7593 mask
|= XCB_CONFIG_WINDOW_BORDER_WIDTH
;
7594 wc
[i
++] = e
->border_width
;
7596 if (e
->value_mask
& XCB_CONFIG_WINDOW_SIBLING
) {
7597 mask
|= XCB_CONFIG_WINDOW_SIBLING
;
7598 wc
[i
++] = e
->sibling
;
7600 if (e
->value_mask
& XCB_CONFIG_WINDOW_STACK_MODE
) {
7601 mask
|= XCB_CONFIG_WINDOW_STACK_MODE
;
7602 wc
[i
++] = e
->stack_mode
;
7606 xcb_configure_window(conn
, e
->window
, mask
, wc
);
7609 } else if ((!win
->manual
|| win
->quirks
& SWM_Q_ANYWHERE
) &&
7610 !(win
->ewmh_flags
& EWMH_F_FULLSCREEN
)) {
7613 else if (win
->ws
->old_r
)
7616 /* windows are centered unless ANYWHERE quirk is set. */
7617 if (win
->quirks
& SWM_Q_ANYWHERE
) {
7618 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
) {
7619 win
->g_float
.x
= e
->x
;
7621 win
->g_float
.x
-= X(r
);
7624 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
) {
7625 win
->g_float
.y
= e
->y
;
7627 win
->g_float
.y
-= Y(r
);
7631 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
)
7632 win
->g_float
.w
= e
->width
;
7634 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
)
7635 win
->g_float
.h
= e
->height
;
7637 win
->g_floatvalid
= 1;
7639 if (win
->floating
&& r
&& (win
->transient
||
7640 win
->ws
->cur_layout
!= &layouts
[SWM_MAX_STACK
])) {
7641 WIDTH(win
) = win
->g_float
.w
;
7642 HEIGHT(win
) = win
->g_float
.h
;
7644 stack_floater(win
, win
->ws
->r
);
7656 DNPRINTF(SWM_D_EVENT
, "configurerequest: done.\n");
7660 configurenotify(xcb_configure_notify_event_t
*e
)
7664 DNPRINTF(SWM_D_EVENT
, "configurenotify: win 0x%x, event win: 0x%x, "
7665 "(x,y) WxH: (%d,%d) %ux%u, border: %u, above_sibling: 0x%x, "
7666 "override_redirect: %s\n", e
->window
, e
->event
, e
->x
, e
->y
,
7667 e
->width
, e
->height
, e
->border_width
, e
->above_sibling
,
7668 YESNO(e
->override_redirect
));
7670 win
= find_window(e
->window
);
7672 xcb_icccm_get_wm_normal_hints_reply(conn
,
7673 xcb_icccm_get_wm_normal_hints(conn
, win
->id
),
7676 if (font_adjusted
) {
7684 destroynotify(xcb_destroy_notify_event_t
*e
)
7688 DNPRINTF(SWM_D_EVENT
, "destroynotify: window: 0x%x\n", e
->window
);
7690 if ((win
= find_window(e
->window
)) == NULL
) {
7691 if ((win
= find_unmanaged_window(e
->window
)) == NULL
)
7697 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
7698 /* If we were focused, make sure we focus on something else. */
7699 if (win
== win
->ws
->focus
)
7700 win
->ws
->focus_pending
= get_focus_prev(win
);
7703 unmanage_window(win
);
7706 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
7707 if (win
->ws
->focus_pending
) {
7708 focus_win(win
->ws
->focus_pending
);
7709 win
->ws
->focus_pending
= NULL
;
7720 get_notify_detail_label(uint8_t detail
)
7725 case XCB_NOTIFY_DETAIL_ANCESTOR
:
7728 case XCB_NOTIFY_DETAIL_VIRTUAL
:
7731 case XCB_NOTIFY_DETAIL_INFERIOR
:
7734 case XCB_NOTIFY_DETAIL_NONLINEAR
:
7735 label
= "Nonlinear";
7737 case XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL
:
7738 label
= "NonlinearVirtual";
7740 case XCB_NOTIFY_DETAIL_POINTER
:
7743 case XCB_NOTIFY_DETAIL_POINTER_ROOT
:
7744 label
= "PointerRoot";
7746 case XCB_NOTIFY_DETAIL_NONE
:
7757 get_notify_mode_label(uint8_t mode
)
7762 case XCB_NOTIFY_MODE_NORMAL
:
7765 case XCB_NOTIFY_MODE_GRAB
:
7768 case XCB_NOTIFY_MODE_UNGRAB
:
7771 case XCB_NOTIFY_MODE_WHILE_GRABBED
:
7772 label
= "WhileGrabbed";
7783 enternotify(xcb_enter_notify_event_t
*e
)
7786 struct swm_region
*old_r
, *r
;
7788 DNPRINTF(SWM_D_FOCUS
, "enternotify: time: %u, win (x,y): 0x%x "
7789 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): 0x%x (%d,%d), "
7790 "child: 0x%x, same_screen_focus: %s, state: %d\n",
7791 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
7792 get_notify_mode_label(e
->mode
), e
->mode
,
7793 get_notify_detail_label(e
->detail
), e
->detail
,
7794 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
7795 YESNO(e
->same_screen_focus
), e
->state
);
7797 if (focus_mode
== SWM_FOCUS_MANUAL
&&
7798 e
->mode
== XCB_NOTIFY_MODE_NORMAL
) {
7799 DNPRINTF(SWM_D_EVENT
, "enternotify: manual focus; ignoring.\n");
7803 last_event_time
= e
->time
;
7805 if ((win
= find_window(e
->event
)) == NULL
) {
7806 if (e
->event
== e
->root
) {
7807 /* If no windows on pointer region, then focus root. */
7808 r
= root_to_region(e
->root
, SWM_CK_POINTER
);
7809 if (TAILQ_EMPTY(&r
->ws
->winlist
)) {
7810 old_r
= root_to_region(e
->root
, SWM_CK_FOCUS
);
7811 if (old_r
&& old_r
!= r
)
7812 unfocus_win(old_r
->ws
->focus
);
7814 xcb_set_input_focus(conn
,
7815 XCB_INPUT_FOCUS_PARENT
, e
->root
, e
->time
);
7817 /* Clear bar since empty. */
7823 DNPRINTF(SWM_D_EVENT
, "enternotify: window is NULL; "
7829 focus_win(get_focus_magic(win
));
7836 leavenotify(xcb_leave_notify_event_t
*e
)
7838 DNPRINTF(SWM_D_FOCUS
, "leavenotify: time: %u, win (x,y): 0x%x "
7839 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): 0x%x (%d,%d), "
7840 "child: 0x%x, same_screen_focus: %s, state: %d\n",
7841 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
7842 get_notify_mode_label(e
->mode
), e
->mode
,
7843 get_notify_detail_label(e
->detail
), e
->detail
,
7844 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
7845 YESNO(e
->same_screen_focus
), e
->state
);
7849 /* lets us use one switch statement for arbitrary mode/detail combinations */
7850 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
7853 mapnotify(xcb_map_notify_event_t
*e
)
7857 DNPRINTF(SWM_D_EVENT
, "mapnotify: window: 0x%x\n", e
->window
);
7859 if ((win
= find_window(e
->window
)) == NULL
)
7860 win
= manage_window(e
->window
, 1);
7863 set_win_state(win
, XCB_ICCCM_WM_STATE_NORMAL
);
7865 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
7866 if (win
->ws
->focus_pending
== win
) {
7868 win
->ws
->focus_pending
= NULL
;
7877 mappingnotify(xcb_mapping_notify_event_t
*e
)
7879 xcb_refresh_keyboard_mapping(syms
, e
);
7881 if (e
->request
== XCB_MAPPING_KEYBOARD
)
7886 maprequest(xcb_map_request_event_t
*e
)
7889 xcb_get_window_attributes_reply_t
*war
;
7891 DNPRINTF(SWM_D_EVENT
, "maprequest: win 0x%x\n",
7894 war
= xcb_get_window_attributes_reply(conn
,
7895 xcb_get_window_attributes(conn
, e
->window
),
7898 DNPRINTF(SWM_D_EVENT
, "maprequest: window lost.\n");
7902 if (war
->override_redirect
) {
7903 DNPRINTF(SWM_D_EVENT
, "maprequest: override_redirect; "
7908 win
= manage_window(e
->window
,
7909 (war
->map_state
== XCB_MAP_STATE_VIEWABLE
));
7911 /* The new window should get focus; prepare. */
7912 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
7913 win
->ws
->focus_pending
= get_focus_magic(win
);
7915 /* All windows need to be mapped if they are in the current workspace.*/
7919 /* Ignore EnterNotify to handle the mapnotify without interference. */
7920 if (focus_mode
== SWM_FOCUS_DEFAULT
)
7921 event_drain(XCB_ENTER_NOTIFY
);
7924 DNPRINTF(SWM_D_EVENT
, "maprequest: done.\n");
7929 get_atom_name(xcb_atom_t atom
)
7934 * This should be disabled during most debugging since
7935 * xcb_get_* causes an xcb_flush.
7938 xcb_get_atom_name_reply_t
*r
;
7940 r
= xcb_get_atom_name_reply(conn
,
7941 xcb_get_atom_name(conn
, atom
),
7944 len
= xcb_get_atom_name_name_length(r
);
7946 name
= malloc(len
+ 1);
7948 memcpy(name
, xcb_get_atom_name_name(r
), len
);
7962 propertynotify(xcb_property_notify_event_t
*e
)
7968 name
= get_atom_name(e
->atom
);
7969 DNPRINTF(SWM_D_EVENT
, "propertynotify: window: 0x%x, atom: %s(%u), "
7970 "time: %#x, state: %u\n", e
->window
, name
, e
->atom
, e
->time
,
7974 win
= find_window(e
->window
);
7978 last_event_time
= e
->time
;
7980 if (e
->atom
== a_swm_iconic
) {
7981 if (e
->state
== XCB_PROPERTY_NEW_VALUE
) {
7982 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
7983 win
->ws
->focus_pending
= get_focus_prev(win
);
7990 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
7991 focus_win(win
->ws
->focus_pending
);
7992 win
->ws
->focus_pending
= NULL
;
7996 } else if (e
->state
== XCB_PROPERTY_DELETE
) {
7997 /* The window is no longer iconic, restack ws. */
7998 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
7999 win
->ws
->focus_pending
= get_focus_magic(win
);
8003 /* Flush EnterNotify for mapnotify, if needed. */
8006 } else if (e
->atom
== a_state
) {
8007 /* State just changed, make sure it gets focused if mapped. */
8008 if (e
->state
== XCB_PROPERTY_NEW_VALUE
) {
8009 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
8011 win
->ws
->focus_pending
== win
) {
8012 focus_win(win
->ws
->focus_pending
);
8013 win
->ws
->focus_pending
= NULL
;
8017 } else if (e
->atom
== XCB_ATOM_WM_CLASS
||
8018 e
->atom
== XCB_ATOM_WM_NAME
) {
8026 unmapnotify(xcb_unmap_notify_event_t
*e
)
8029 struct workspace
*ws
;
8031 DNPRINTF(SWM_D_EVENT
, "unmapnotify: window: 0x%x\n", e
->window
);
8033 /* If we aren't managing the window, then ignore. */
8034 win
= find_window(e
->window
);
8040 if (getstate(e
->window
) != XCB_ICCCM_WM_STATE_ICONIC
)
8041 set_win_state(win
, XCB_ICCCM_WM_STATE_ICONIC
);
8044 /* window unmapped itself */
8045 /* do unmap/unfocus/restack and unmanage */
8048 /* If win was focused, make sure to focus on something else. */
8049 if (win
== ws
->focus
) {
8050 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
8051 ws
->focus_pending
= get_focus_prev(win
);
8052 DNPRINTF(SWM_D_EVENT
, "unmapnotify: "
8053 "focus_pending: 0x%x\n",
8054 WINID(ws
->focus_pending
));
8060 unmanage_window(win
);
8065 if (focus_mode
== SWM_FOCUS_FOLLOW
) {
8067 focus_win(get_pointer_win(ws
->r
->s
->root
));
8069 if (ws
->focus_pending
) {
8070 focus_win(ws
->focus_pending
);
8071 ws
->focus_pending
= NULL
;
8076 if (getstate(e
->window
) == XCB_ICCCM_WM_STATE_NORMAL
)
8077 set_win_state(win
, XCB_ICCCM_WM_STATE_ICONIC
);
8084 visibilitynotify(xcb_visibility_notify_event_t
*e
)
8086 DNPRINTF(SWM_D_EVENT
, "visibilitynotify: window: 0x%x\n",
8092 clientmessage(xcb_client_message_event_t
*e
)
8095 xcb_map_request_event_t mre
;
8099 name
= get_atom_name(e
->type
);
8100 DNPRINTF(SWM_D_EVENT
, "clientmessage: window: 0x%x, atom: %s(%u)\n",
8101 e
->window
, name
, e
->type
);
8104 win
= find_window(e
->window
);
8107 if (e
->type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
8108 /* Manage the window with maprequest. */
8109 DNPRINTF(SWM_D_EVENT
, "clientmessage: request focus on "
8110 "unmanaged window.\n");
8111 mre
.window
= e
->window
;
8117 if (e
->type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
8118 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_ACTIVE_WINDOW\n");
8121 if (e
->type
== ewmh
[_NET_CLOSE_WINDOW
].atom
) {
8122 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_CLOSE_WINDOW\n");
8123 if (win
->can_delete
)
8124 client_msg(win
, a_delete
, 0);
8126 xcb_kill_client(conn
, win
->id
);
8128 if (e
->type
== ewmh
[_NET_MOVERESIZE_WINDOW
].atom
) {
8129 DNPRINTF(SWM_D_EVENT
,
8130 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
8131 if (win
->floating
) {
8132 if (e
->data
.data32
[0] & (1<<8)) /* x */
8133 X(win
) = e
->data
.data32
[1];
8134 if (e
->data
.data32
[0] & (1<<9)) /* y */
8135 Y(win
) = e
->data
.data32
[2];
8136 if (e
->data
.data32
[0] & (1<<10)) /* width */
8137 WIDTH(win
) = e
->data
.data32
[3];
8138 if (e
->data
.data32
[0] & (1<<11)) /* height */
8139 HEIGHT(win
) = e
->data
.data32
[4];
8144 /* TODO: Change stack sizes */
8145 /* notify no change was made. */
8146 config_win(win
, NULL
);
8149 if (e
->type
== ewmh
[_NET_WM_STATE
].atom
) {
8150 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_WM_STATE\n");
8151 ewmh_update_win_state(win
, e
->data
.data32
[1], e
->data
.data32
[0]);
8152 if (e
->data
.data32
[2])
8153 ewmh_update_win_state(win
, e
->data
.data32
[2],
8165 int errcode
= xcb_connection_has_error(conn
);
8166 #ifdef XCB_CONN_ERROR
8169 case XCB_CONN_ERROR
:
8170 s
= "Socket error, pipe error or other stream error.";
8172 case XCB_CONN_CLOSED_EXT_NOTSUPPORTED
:
8173 s
= "Extension not supported.";
8175 case XCB_CONN_CLOSED_MEM_INSUFFICIENT
:
8176 s
= "Insufficient memory.";
8178 case XCB_CONN_CLOSED_REQ_LEN_EXCEED
:
8179 s
= "Request length was exceeded.";
8181 case XCB_CONN_CLOSED_PARSE_ERR
:
8182 s
= "Error parsing display string.";
8185 s
= "Unknown error.";
8188 errx(errcode
, "X CONNECTION ERROR: %s", s
);
8191 errx(errcode
, "X CONNECTION ERROR");
8199 const uint32_t val
= XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT
|
8200 XCB_EVENT_MASK_ENTER_WINDOW
;
8202 xcb_void_cookie_t wac
;
8203 xcb_generic_error_t
*error
;
8205 /* this causes an error if some other window manager is running */
8206 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
8207 for (i
= 0; i
< num_screens
; i
++) {
8208 if ((sc
= get_screen(i
)) == NULL
)
8209 errx(1, "ERROR: can't get screen %d.", i
);
8210 DNPRINTF(SWM_D_INIT
, "enable_wm: screen %d, root: 0x%x\n",
8212 wac
= xcb_change_window_attributes_checked(conn
, sc
->root
,
8213 XCB_CW_EVENT_MASK
, &val
);
8214 if ((error
= xcb_request_check(conn
, wac
))) {
8215 DNPRINTF(SWM_D_INIT
, "enable_wm: error_code: %u\n",
8221 /* click to focus on empty region */
8222 xcb_grab_button(conn
, 1, sc
->root
, BUTTONMASK
,
8223 XCB_GRAB_MODE_SYNC
, XCB_GRAB_MODE_ASYNC
, XCB_WINDOW_NONE
,
8224 XCB_CURSOR_NONE
, XCB_BUTTON_INDEX_1
, XCB_BUTTON_MASK_ANY
);
8231 new_region(struct swm_screen
*s
, int x
, int y
, int w
, int h
)
8233 struct swm_region
*r
, *n
;
8234 struct workspace
*ws
= NULL
;
8237 DNPRINTF(SWM_D_MISC
, "new region: screen[%d]:%dx%d+%d+%d\n",
8238 s
->idx
, w
, h
, x
, y
);
8240 /* remove any conflicting regions */
8241 n
= TAILQ_FIRST(&s
->rl
);
8244 n
= TAILQ_NEXT(r
, entry
);
8245 if (X(r
) < (x
+ w
) &&
8246 (X(r
) + WIDTH(r
)) > x
&&
8248 (Y(r
) + HEIGHT(r
)) > y
) {
8249 if (r
->ws
->r
!= NULL
)
8250 r
->ws
->old_r
= r
->ws
->r
;
8253 TAILQ_REMOVE(&s
->rl
, r
, entry
);
8254 TAILQ_INSERT_TAIL(&s
->orl
, r
, entry
);
8258 /* search old regions for one to reuse */
8260 /* size + location match */
8261 TAILQ_FOREACH(r
, &s
->orl
, entry
)
8262 if (X(r
) == x
&& Y(r
) == y
&&
8263 HEIGHT(r
) == h
&& WIDTH(r
) == w
)
8267 TAILQ_FOREACH(r
, &s
->orl
, entry
)
8268 if (HEIGHT(r
) == h
&& WIDTH(r
) == w
)
8272 TAILQ_REMOVE(&s
->orl
, r
, entry
);
8273 /* try to use old region's workspace */
8274 if (r
->ws
->r
== NULL
)
8277 if ((r
= calloc(1, sizeof(struct swm_region
))) == NULL
)
8278 err(1, "new_region: calloc: failed to allocate memory "
8281 /* if we don't have a workspace already, find one */
8283 for (i
= 0; i
< workspace_limit
; i
++)
8284 if (s
->ws
[i
].r
== NULL
) {
8291 errx(1, "new_region: no free workspaces");
8302 TAILQ_INSERT_TAIL(&s
->rl
, r
, entry
);
8308 #ifdef SWM_XRR_HAS_CRTC
8311 #endif /* SWM_XRR_HAS_CRTC */
8312 struct swm_region
*r
;
8314 xcb_randr_get_screen_resources_current_cookie_t src
;
8315 xcb_randr_get_screen_resources_current_reply_t
*srr
;
8316 xcb_randr_get_crtc_info_cookie_t cic
;
8317 xcb_randr_get_crtc_info_reply_t
*cir
= NULL
;
8318 xcb_randr_crtc_t
*crtc
;
8319 xcb_screen_t
*screen
;
8321 DNPRINTF(SWM_D_MISC
, "scan_xrandr: screen: %d\n", i
);
8323 if ((screen
= get_screen(i
)) == NULL
)
8324 errx(1, "ERROR: can't get screen %d.", i
);
8326 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
8327 if (i
>= num_screens
)
8328 errx(1, "scan_xrandr: invalid screen");
8330 /* remove any old regions */
8331 while ((r
= TAILQ_FIRST(&screens
[i
].rl
)) != NULL
) {
8332 r
->ws
->old_r
= r
->ws
->r
= NULL
;
8334 TAILQ_REMOVE(&screens
[i
].rl
, r
, entry
);
8335 TAILQ_INSERT_TAIL(&screens
[i
].orl
, r
, entry
);
8339 /* map virtual screens onto physical screens */
8340 #ifdef SWM_XRR_HAS_CRTC
8341 if (xrandr_support
) {
8342 src
= xcb_randr_get_screen_resources_current(conn
,
8344 srr
= xcb_randr_get_screen_resources_current_reply(conn
, src
,
8347 new_region(&screens
[i
], 0, 0,
8348 screen
->width_in_pixels
,
8349 screen
->height_in_pixels
);
8352 ncrtc
= srr
->num_crtcs
;
8354 crtc
= xcb_randr_get_screen_resources_current_crtcs(srr
);
8355 for (c
= 0; c
< ncrtc
; c
++) {
8356 cic
= xcb_randr_get_crtc_info(conn
, crtc
[c
],
8358 cir
= xcb_randr_get_crtc_info_reply(conn
, cic
, NULL
);
8361 if (cir
->num_outputs
== 0) {
8367 new_region(&screens
[i
], 0, 0,
8368 screen
->width_in_pixels
,
8369 screen
->height_in_pixels
);
8371 new_region(&screens
[i
],
8372 cir
->x
, cir
->y
, cir
->width
, cir
->height
);
8377 #endif /* SWM_XRR_HAS_CRTC */
8379 /* If detection failed, create a single region that spans the screen. */
8380 if (TAILQ_EMPTY(&screens
[i
].rl
))
8381 new_region(&screens
[i
], 0, 0, screen
->width_in_pixels
,
8382 screen
->height_in_pixels
);
8385 DNPRINTF(SWM_D_MISC
, "scan_xrandr: done.\n");
8389 screenchange(xcb_randr_screen_change_notify_event_t
*e
)
8391 struct swm_region
*r
;
8394 DNPRINTF(SWM_D_EVENT
, "screenchange: root: 0x%x\n", e
->root
);
8396 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
8397 /* silly event doesn't include the screen index */
8398 for (i
= 0; i
< num_screens
; i
++)
8399 if (screens
[i
].root
== e
->root
)
8401 if (i
>= num_screens
)
8402 errx(1, "screenchange: screen not found");
8404 /* brute force for now, just re-enumerate the regions */
8408 print_win_geom(e
->root
);
8410 /* add bars to all regions */
8411 for (i
= 0; i
< num_screens
; i
++)
8412 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
8420 xcb_window_t
*wins
= NULL
, trans
;
8422 int i
, j
, num_screens
;
8423 uint16_t state
, manage
, mapped
;
8425 xcb_query_tree_cookie_t qtc
;
8426 xcb_query_tree_reply_t
*qtr
;
8427 xcb_get_window_attributes_cookie_t c
;
8428 xcb_get_window_attributes_reply_t
*r
;
8429 xcb_get_property_cookie_t pc
;
8431 DNPRINTF(SWM_D_INIT
, "grab_windows: begin\n");
8432 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
8433 for (i
= 0; i
< num_screens
; i
++) {
8434 qtc
= xcb_query_tree(conn
, screens
[i
].root
);
8435 qtr
= xcb_query_tree_reply(conn
, qtc
, NULL
);
8438 wins
= xcb_query_tree_children(qtr
);
8439 no
= xcb_query_tree_children_length(qtr
);
8440 /* attach windows to a region */
8441 /* normal windows */
8442 DNPRINTF(SWM_D_INIT
, "grab_windows: grab top level windows.\n");
8443 for (j
= 0; j
< no
; j
++) {
8444 c
= xcb_get_window_attributes(conn
, wins
[j
]);
8445 r
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
8447 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
8448 "doesn't exist.\n", wins
[j
]);
8451 if (r
->override_redirect
) {
8452 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
8453 "override_redirect set.\n", wins
[j
]);
8458 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
8459 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
8461 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
8462 "is transient for %#x.\n", wins
[j
], trans
);
8467 state
= getstate(wins
[j
]);
8468 manage
= state
!= XCB_ICCCM_WM_STATE_WITHDRAWN
;
8469 mapped
= r
->map_state
!= XCB_MAP_STATE_UNMAPPED
;
8470 if (mapped
|| manage
)
8471 manage_window(wins
[j
], mapped
);
8474 /* transient windows */
8475 DNPRINTF(SWM_D_INIT
, "grab_windows: grab transient windows.\n");
8476 for (j
= 0; j
< no
; j
++) {
8477 c
= xcb_get_window_attributes(conn
, wins
[j
]);
8478 r
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
8480 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
8481 "doesn't exist.\n", wins
[j
]);
8484 if (r
->override_redirect
) {
8485 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
8486 "override_redirect set.\n", wins
[j
]);
8491 state
= getstate(wins
[j
]);
8492 manage
= state
!= XCB_ICCCM_WM_STATE_WITHDRAWN
;
8493 mapped
= r
->map_state
!= XCB_MAP_STATE_UNMAPPED
;
8494 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
8495 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
8496 &trans
, NULL
) && manage
)
8497 manage_window(wins
[j
], mapped
);
8502 DNPRINTF(SWM_D_INIT
, "grab_windows: done.\n");
8508 int i
, j
, k
, num_screens
;
8509 struct workspace
*ws
;
8510 uint32_t gcv
[1], wa
[1];
8511 const xcb_query_extension_reply_t
*qep
;
8512 xcb_screen_t
*screen
;
8513 xcb_randr_query_version_cookie_t c
;
8514 xcb_randr_query_version_reply_t
*r
;
8516 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
8517 if ((screens
= calloc(num_screens
,
8518 sizeof(struct swm_screen
))) == NULL
)
8519 err(1, "setup_screens: calloc: failed to allocate memory for "
8522 /* initial Xrandr setup */
8524 qep
= xcb_get_extension_data(conn
, &xcb_randr_id
);
8526 c
= xcb_randr_query_version(conn
, 1, 1);
8527 r
= xcb_randr_query_version_reply(conn
, c
, NULL
);
8529 if (r
->major_version
>= 1) {
8531 xrandr_eventbase
= qep
->first_event
;
8537 wa
[0] = cursors
[XC_LEFT_PTR
].cid
;
8539 /* map physical screens */
8540 for (i
= 0; i
< num_screens
; i
++) {
8541 DNPRINTF(SWM_D_WS
, "setup_screens: init screen: %d\n", i
);
8543 TAILQ_INIT(&screens
[i
].rl
);
8544 TAILQ_INIT(&screens
[i
].orl
);
8545 if ((screen
= get_screen(i
)) == NULL
)
8546 errx(1, "ERROR: can't get screen %d.", i
);
8547 screens
[i
].root
= screen
->root
;
8549 /* set default colors */
8550 setscreencolor("red", i
+ 1, SWM_S_COLOR_FOCUS
);
8551 setscreencolor("rgb:88/88/88", i
+ 1, SWM_S_COLOR_UNFOCUS
);
8552 setscreencolor("rgb:00/80/80", i
+ 1, SWM_S_COLOR_BAR_BORDER
);
8553 setscreencolor("black", i
+ 1, SWM_S_COLOR_BAR
);
8554 setscreencolor("rgb:a0/a0/a0", i
+ 1, SWM_S_COLOR_BAR_FONT
);
8556 /* create graphics context on screen */
8557 screens
[i
].bar_gc
= xcb_generate_id(conn
);
8559 xcb_create_gc(conn
, screens
[i
].bar_gc
, screens
[i
].root
,
8560 XCB_GC_GRAPHICS_EXPOSURES
, gcv
);
8562 /* set default cursor */
8563 xcb_change_window_attributes(conn
, screens
[i
].root
,
8566 /* init all workspaces */
8567 /* XXX these should be dynamically allocated too */
8568 for (j
= 0; j
< SWM_WS_MAX
; j
++) {
8569 ws
= &screens
[i
].ws
[j
];
8573 ws
->focus_prev
= NULL
;
8574 ws
->focus_pending
= NULL
;
8577 TAILQ_INIT(&ws
->winlist
);
8578 TAILQ_INIT(&ws
->unmanagedlist
);
8580 for (k
= 0; layouts
[k
].l_stack
!= NULL
; k
++)
8581 if (layouts
[k
].l_config
!= NULL
)
8582 layouts
[k
].l_config(ws
,
8583 SWM_ARG_ID_STACKINIT
);
8584 ws
->cur_layout
= &layouts
[0];
8585 ws
->cur_layout
->l_string(ws
);
8591 xcb_randr_select_input(conn
, screens
[i
].root
,
8592 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE
);
8599 if ((bar_fonts
= strdup(SWM_BAR_FONTS
)) == NULL
)
8600 err(1, "setup_globals: strdup: failed to allocate memory.");
8602 if ((clock_format
= strdup("%a %b %d %R %Z %Y")) == NULL
)
8603 err(1, "setup_globals: strdup: failed to allocate memory.");
8605 if ((syms
= xcb_key_symbols_alloc(conn
)) == NULL
)
8606 errx(1, "unable to allocate key symbols");
8608 a_state
= get_atom_from_string("WM_STATE");
8609 a_prot
= get_atom_from_string("WM_PROTOCOLS");
8610 a_delete
= get_atom_from_string("WM_DELETE_WINDOW");
8611 a_takefocus
= get_atom_from_string("WM_TAKE_FOCUS");
8612 a_wmname
= get_atom_from_string("WM_NAME");
8613 a_netwmname
= get_atom_from_string("_NET_WM_NAME");
8614 a_utf8_string
= get_atom_from_string("UTF8_STRING");
8615 a_string
= get_atom_from_string("STRING");
8616 a_swm_iconic
= get_atom_from_string("_SWM_ICONIC");
8617 a_swm_ws
= get_atom_from_string("_SWM_WS");
8624 xcb_atom_t netwmcheck
;
8625 xcb_window_t root
, win
;
8627 /* work around sun jdk bugs, code from wmname */
8628 netwmcheck
= get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
8630 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
8631 for (i
= 0; i
< num_screens
; i
++) {
8632 root
= screens
[i
].root
;
8634 win
= xcb_generate_id(conn
);
8635 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, win
, root
,
8636 0, 0, 1, 1, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
8637 XCB_COPY_FROM_PARENT
, 0, NULL
);
8639 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, root
,
8640 netwmcheck
, XCB_ATOM_WINDOW
, 32, 1, &win
);
8641 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
8642 netwmcheck
, XCB_ATOM_WINDOW
, 32, 1, &win
);
8643 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
8644 a_netwmname
, a_utf8_string
, 8, strlen("LG3D"), "LG3D");
8649 shutdown_cleanup(void)
8653 /* disable alarm because the following code may not be interrupted */
8655 if (signal(SIGALRM
, SIG_IGN
) == SIG_ERR
)
8656 err(1, "can't disable alarm");
8666 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
8667 for (i
= 0; i
< num_screens
; ++i
) {
8668 if (screens
[i
].bar_gc
!= 0)
8669 xcb_free_gc(conn
, screens
[i
].bar_gc
);
8670 if (!bar_font_legacy
)
8671 XftColorFree(display
, DefaultVisual(display
, i
),
8672 DefaultColormap(display
, i
), &bar_font_color
);
8675 if (bar_font_legacy
)
8676 XFreeFontSet(display
, bar_fs
);
8678 XftFontClose(display
, bar_font
);
8681 xcb_key_symbols_free(syms
);
8683 xcb_disconnect(conn
);
8687 event_error(xcb_generic_error_t
*e
)
8691 DNPRINTF(SWM_D_EVENT
, "event_error: %s(%u) from %s(%u), sequence: %u, "
8692 "resource_id: %u, minor_code: %u\n",
8693 xcb_event_get_error_label(e
->error_code
), e
->error_code
,
8694 xcb_event_get_request_label(e
->major_code
), e
->major_code
,
8695 e
->sequence
, e
->resource_id
, e
->minor_code
);
8699 event_handle(xcb_generic_event_t
*evt
)
8701 uint8_t type
= XCB_EVENT_RESPONSE_TYPE(evt
);
8703 DNPRINTF(SWM_D_EVENT
, "XCB Event: %s(%d), seq %u\n",
8704 xcb_event_get_label(XCB_EVENT_RESPONSE_TYPE(evt
)),
8705 XCB_EVENT_RESPONSE_TYPE(evt
), evt
->sequence
);
8708 #define EVENT(type, callback) case type: callback((void *)evt); return
8709 EVENT(0, event_error
);
8710 EVENT(XCB_BUTTON_PRESS
, buttonpress
);
8711 /*EVENT(XCB_BUTTON_RELEASE, buttonpress);*/
8712 /*EVENT(XCB_CIRCULATE_NOTIFY, );*/
8713 /*EVENT(XCB_CIRCULATE_REQUEST, );*/
8714 EVENT(XCB_CLIENT_MESSAGE
, clientmessage
);
8715 /*EVENT(XCB_COLORMAP_NOTIFY, );*/
8716 EVENT(XCB_CONFIGURE_NOTIFY
, configurenotify
);
8717 EVENT(XCB_CONFIGURE_REQUEST
, configurerequest
);
8718 /*EVENT(XCB_CREATE_NOTIFY, );*/
8719 EVENT(XCB_DESTROY_NOTIFY
, destroynotify
);
8720 EVENT(XCB_ENTER_NOTIFY
, enternotify
);
8721 EVENT(XCB_EXPOSE
, expose
);
8723 EVENT(XCB_FOCUS_IN
, focusin
);
8724 EVENT(XCB_FOCUS_OUT
, focusout
);
8726 /*EVENT(XCB_GRAPHICS_EXPOSURE, );*/
8727 /*EVENT(XCB_GRAVITY_NOTIFY, );*/
8728 EVENT(XCB_KEY_PRESS
, keypress
);
8729 /*EVENT(XCB_KEY_RELEASE, keypress);*/
8730 /*EVENT(XCB_KEYMAP_NOTIFY, );*/
8732 EVENT(XCB_LEAVE_NOTIFY
, leavenotify
);
8734 EVENT(XCB_MAP_NOTIFY
, mapnotify
);
8735 EVENT(XCB_MAP_REQUEST
, maprequest
);
8736 EVENT(XCB_MAPPING_NOTIFY
, mappingnotify
);
8737 /*EVENT(XCB_MOTION_NOTIFY, );*/
8738 /*EVENT(XCB_NO_EXPOSURE, );*/
8739 EVENT(XCB_PROPERTY_NOTIFY
, propertynotify
);
8740 /*EVENT(XCB_REPARENT_NOTIFY, );*/
8741 /*EVENT(XCB_RESIZE_REQUEST, );*/
8742 /*EVENT(XCB_SELECTION_CLEAR, );*/
8743 /*EVENT(XCB_SELECTION_NOTIFY, );*/
8744 /*EVENT(XCB_SELECTION_REQUEST, );*/
8745 EVENT(XCB_UNMAP_NOTIFY
, unmapnotify
);
8746 /*EVENT(XCB_VISIBILITY_NOTIFY, visibilitynotify);*/
8749 if (type
- xrandr_eventbase
== XCB_RANDR_SCREEN_CHANGE_NOTIFY
)
8750 screenchange((void *)evt
);
8754 main(int argc
, char *argv
[])
8756 struct swm_region
*r
, *rr
;
8757 struct ws_win
*winfocus
= NULL
;
8758 char conf
[PATH_MAX
], *cfile
= NULL
;
8760 int xfd
, i
, num_screens
;
8761 struct sigaction sact
;
8762 xcb_generic_event_t
*evt
;
8766 /* suppress unused warning since var is needed */
8769 time_started
= time(NULL
);
8772 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION
, buildstr
);
8773 if (!setlocale(LC_CTYPE
, "") || !setlocale(LC_TIME
, ""))
8774 warnx("no locale support");
8776 /* handle some signals */
8777 bzero(&sact
, sizeof(sact
));
8778 sigemptyset(&sact
.sa_mask
);
8780 sact
.sa_handler
= sighdlr
;
8781 sigaction(SIGINT
, &sact
, NULL
);
8782 sigaction(SIGQUIT
, &sact
, NULL
);
8783 sigaction(SIGTERM
, &sact
, NULL
);
8784 sigaction(SIGHUP
, &sact
, NULL
);
8786 sact
.sa_handler
= sighdlr
;
8787 sact
.sa_flags
= SA_NOCLDSTOP
;
8788 sigaction(SIGCHLD
, &sact
, NULL
);
8790 if (!(display
= XOpenDisplay(0)))
8791 errx(1, "can not open display");
8793 conn
= XGetXCBConnection(display
);
8794 if (xcb_connection_has_error(conn
))
8795 errx(1, "can not get XCB connection");
8797 XSetEventQueueOwner(display
, XCBOwnsEventQueue
);
8799 xcb_prefetch_extension_data(conn
, &xcb_randr_id
);
8800 xfd
= xcb_get_file_descriptor(conn
);
8802 /* look for local and global conf file */
8803 pwd
= getpwuid(getuid());
8805 errx(1, "invalid user: %d", getuid());
8807 xcb_grab_server(conn
);
8810 /* flush all events */
8811 while ((evt
= xcb_poll_for_event(conn
))) {
8812 if (XCB_EVENT_RESPONSE_TYPE(evt
) == 0)
8817 if (enable_wm() != 0)
8818 errx(1, "another window manager is currently running");
8820 /* Load Xcursors and/or cursorfont glyph cursors. */
8832 for (i
= 0; ; i
++) {
8837 snprintf(conf
, sizeof conf
, "%s/.%s",
8838 pwd
->pw_dir
, SWM_CONF_FILE
);
8842 snprintf(conf
, sizeof conf
, "/etc/%s",
8847 snprintf(conf
, sizeof conf
, "%s/.%s",
8848 pwd
->pw_dir
, SWM_CONF_FILE_OLD
);
8852 snprintf(conf
, sizeof conf
, "/etc/%s",
8859 if (strlen(conf
) && stat(conf
, &sb
) != -1)
8860 if (S_ISREG(sb
.st_mode
)) {
8867 /* load conf (if any) */
8869 conf_load(cfile
, SWM_CONF_DEFAULT
);
8872 /* set some values to work around bad programs */
8874 /* grab existing windows (before we build the bars) */
8877 if (getenv("SWM_STARTED") == NULL
)
8878 setenv("SWM_STARTED", "YES", 1);
8880 /* setup all bars */
8881 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
8882 for (i
= 0; i
< num_screens
; i
++)
8883 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
8884 if (winfocus
== NULL
)
8885 winfocus
= TAILQ_FIRST(&r
->ws
->winlist
);
8892 xcb_ungrab_server(conn
);
8896 while ((evt
= xcb_poll_for_event(conn
))) {
8903 /* If just (re)started, set default focus if needed. */
8904 if (winfocus
&& focus_mode
!= SWM_FOCUS_FOLLOW
) {
8905 rr
= winfocus
->ws
->r
;
8907 /* not a visible window */
8912 focus_win(get_region_focus(rr
));
8922 if (select(xfd
+ 1, &rd
, NULL
, NULL
, &tv
) == -1)
8923 if (errno
!= EINTR
) {
8924 DNPRINTF(SWM_D_MISC
, "select failed");
8927 restart(NULL
, NULL
);