2 * Copyright (c) 2009-2012 Marco Peereboom <marco@peereboom.us>
3 * Copyright (c) 2009-2011 Ryan McBride <mcbride@countersiege.com>
4 * Copyright (c) 2009 Darrin Chandler <dwchandler@stilyagin.com>
5 * Copyright (c) 2009 Pierre-Yves Ritschard <pyr@spootnik.org>
6 * Copyright (c) 2010 Tuukka Kataja <stuge@xor.fi>
7 * Copyright (c) 2011 Jason L. Wright <jason@thought.net>
8 * Copyright (c) 2011-2012 Reginald Kennedy <rk@rejii.com>
9 * Copyright (c) 2011-2012 Lawrence Teo <lteo@lteo.net>
10 * Copyright (c) 2011-2012 Tiago Cunha <tcunha@gmx.com>
11 * Copyright (c) 2012 David Hill <dhill@mindcry.org>
13 * Permission to use, copy, modify, and distribute this software for any
14 * purpose with or without fee is hereby granted, provided that the above
15 * copyright notice and this permission notice appear in all copies.
17 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
18 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
20 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
21 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
22 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
23 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 * Much code and ideas taken from dwm under the following license:
27 * MIT/X Consortium License
29 * 2006-2008 Anselm R Garbe <garbeam at gmail dot com>
30 * 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
31 * 2006-2007 Jukka Salmi <jukka at salmi dot ch>
32 * 2007 Premysl Hruby <dfenze at gmail dot com>
33 * 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
34 * 2007 Christof Musik <christof at sendfax dot de>
35 * 2007-2008 Enno Gottox Boland <gottox at s01 dot de>
36 * 2007-2008 Peter Hartlich <sgkkr at hartlich dot com>
37 * 2008 Martin Hurton <martin dot hurton at gmail dot com>
39 * Permission is hereby granted, free of charge, to any person obtaining a
40 * copy of this software and associated documentation files (the "Software"),
41 * to deal in the Software without restriction, including without limitation
42 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
43 * and/or sell copies of the Software, and to permit persons to whom the
44 * Software is furnished to do so, subject to the following conditions:
46 * The above copyright notice and this permission notice shall be included in
47 * all copies or substantial portions of the Software.
49 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
50 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
51 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
52 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
53 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
54 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
55 * DEALINGS IN THE SOFTWARE.
59 #include <sys/types.h>
63 #include <sys/queue.h>
64 #include <sys/param.h>
65 #include <sys/select.h>
66 #if defined(__linux__)
68 #elif defined(__OpenBSD__)
70 #elif defined(__FreeBSD__)
91 #include <X11/cursorfont.h>
92 #include <X11/extensions/Xrandr.h>
93 #include <X11/Xcursor/Xcursor.h>
94 #include <X11/Xft/Xft.h>
95 #include <X11/Xlib-xcb.h>
96 #include <xcb/xcb_atom.h>
97 #include <xcb/xcb_aux.h>
98 #include <xcb/xcb_event.h>
99 #include <xcb/xcb_icccm.h>
100 #include <xcb/xcb_keysyms.h>
101 #include <xcb/xtest.h>
102 #include <xcb/randr.h>
110 #ifdef SPECTRWM_BUILDSTR
111 static const char *buildstr
= SPECTRWM_BUILDSTR
;
113 static const char *buildstr
= SPECTRWM_VERSION
;
116 #if !defined(__CYGWIN__) /* cygwin chokes on xrandr stuff */
118 # error XRandR versions less than 1.0 are not supported
121 # if RANDR_MAJOR >= 1
122 # if RANDR_MINOR >= 2
123 # define SWM_XRR_HAS_CRTC
126 #endif /* __CYGWIN__ */
128 #ifndef XCB_ICCCM_NUM_WM_HINTS_ELEMENTS
129 #define XCB_ICCCM_SIZE_HINT_P_MIN_SIZE XCB_SIZE_HINT_P_MIN_SIZE
130 #define XCB_ICCCM_SIZE_HINT_P_MAX_SIZE XCB_SIZE_HINT_P_MAX_SIZE
131 #define XCB_ICCCM_SIZE_HINT_P_RESIZE_INC XCB_SIZE_HINT_P_RESIZE_INC
132 #define XCB_ICCCM_WM_HINT_INPUT XCB_WM_HINT_INPUT
133 #define XCB_ICCCM_WM_HINT_X_URGENCY XCB_WM_HINT_X_URGENCY
134 #define XCB_ICCCM_WM_STATE_ICONIC XCB_WM_STATE_ICONIC
135 #define XCB_ICCCM_WM_STATE_WITHDRAWN XCB_WM_STATE_WITHDRAWN
136 #define XCB_ICCCM_WM_STATE_NORMAL XCB_WM_STATE_NORMAL
137 #define xcb_icccm_get_text_property_reply_t xcb_get_text_property_reply_t
138 #define xcb_icccm_get_text_property_reply_wipe xcb_get_text_property_reply_wipe
139 #define xcb_icccm_get_wm_class xcb_get_wm_class
140 #define xcb_icccm_get_wm_class_reply xcb_get_wm_class_reply
141 #define xcb_icccm_get_wm_class_reply_t xcb_get_wm_class_reply_t
142 #define xcb_icccm_get_wm_class_reply_wipe xcb_get_wm_class_reply_wipe
143 #define xcb_icccm_get_wm_hints xcb_get_wm_hints
144 #define xcb_icccm_get_wm_hints_reply xcb_get_wm_hints_reply
145 #define xcb_icccm_get_wm_name xcb_get_wm_name
146 #define xcb_icccm_get_wm_name_reply xcb_get_wm_name_reply
147 #define xcb_icccm_get_wm_normal_hints xcb_get_wm_normal_hints
148 #define xcb_icccm_get_wm_normal_hints_reply xcb_get_wm_normal_hints_reply
149 #define xcb_icccm_get_wm_protocols xcb_get_wm_protocols
150 #define xcb_icccm_get_wm_protocols_reply xcb_get_wm_protocols_reply
151 #define xcb_icccm_get_wm_protocols_reply_t xcb_get_wm_protocols_reply_t
152 #define xcb_icccm_get_wm_protocols_reply_wipe xcb_get_wm_protocols_reply_wipe
153 #define xcb_icccm_get_wm_transient_for xcb_get_wm_transient_for
154 #define xcb_icccm_get_wm_transient_for_reply xcb_get_wm_transient_for_reply
155 #define xcb_icccm_wm_hints_t xcb_wm_hints_t
158 /*#define SWM_DEBUG*/
160 #define DPRINTF(x...) do { \
162 fprintf(stderr, x); \
164 #define DNPRINTF(n,x...) do { \
165 if (swm_debug & n) { \
166 fprintf(stderr, "%ld ", (long)(time(NULL) - time_started)); \
167 fprintf(stderr, x); \
170 #define SWM_D_MISC 0x0001
171 #define SWM_D_EVENT 0x0002
172 #define SWM_D_WS 0x0004
173 #define SWM_D_FOCUS 0x0008
174 #define SWM_D_MOVE 0x0010
175 #define SWM_D_STACK 0x0020
176 #define SWM_D_MOUSE 0x0040
177 #define SWM_D_PROP 0x0080
178 #define SWM_D_CLASS 0x0100
179 #define SWM_D_KEY 0x0200
180 #define SWM_D_QUIRK 0x0400
181 #define SWM_D_SPAWN 0x0800
182 #define SWM_D_EVENTQ 0x1000
183 #define SWM_D_CONF 0x2000
184 #define SWM_D_BAR 0x4000
185 #define SWM_D_INIT 0x8000
187 u_int32_t swm_debug
= 0
206 #define DPRINTF(x...)
207 #define DNPRINTF(n,x...)
210 /* convert 8-bit to 16-bit */
211 #define RGB_8_TO_16(col) ((col) << 8) + (col)
213 #define PIXEL_TO_XRENDERCOLOR(px, xrc) \
214 xrc.red = RGB_8_TO_16((px) >> 16 & 0xff); \
215 xrc.green = RGB_8_TO_16((px) >> 8 & 0xff); \
216 xrc.blue = RGB_8_TO_16((px) & 0xff); \
219 #define LENGTH(x) (int)(sizeof (x) / sizeof (x)[0])
220 #define MODKEY XCB_MOD_MASK_1
221 #define CLEANMASK(mask) ((mask) & ~(numlockmask | XCB_MOD_MASK_LOCK))
222 #define BUTTONMASK (XCB_EVENT_MASK_BUTTON_PRESS | \
223 XCB_EVENT_MASK_BUTTON_RELEASE)
224 #define MOUSEMASK (BUTTONMASK|XCB_EVENT_MASK_POINTER_MOTION)
225 #define SWM_PROPLEN (16)
226 #define SWM_FUNCNAME_LEN (32)
227 #define SWM_KEYS_LEN (255)
228 #define SWM_QUIRK_LEN (64)
229 #define X(r) (r)->g.x
230 #define Y(r) (r)->g.y
231 #define WIDTH(r) (r)->g.w
232 #define HEIGHT(r) (r)->g.h
233 #define BORDER(w) ((w)->bordered ? border_width : 0)
234 #define MAX_X(r) ((r)->g.x + (r)->g.w)
235 #define MAX_Y(r) ((r)->g.y + (r)->g.h)
236 #define SH_MIN(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE
237 #define SH_MIN_W(w) (w)->sh.min_width
238 #define SH_MIN_H(w) (w)->sh.min_height
239 #define SH_MAX(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MAX_SIZE
240 #define SH_MAX_W(w) (w)->sh.max_width
241 #define SH_MAX_H(w) (w)->sh.max_height
242 #define SH_INC(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_RESIZE_INC
243 #define SH_INC_W(w) (w)->sh.width_inc
244 #define SH_INC_H(w) (w)->sh.height_inc
245 #define SWM_MAX_FONT_STEPS (3)
246 #define WINID(w) ((w) ? (w)->id : 0)
247 #define YESNO(x) ((x) ? "yes" : "no")
249 #define SWM_FOCUS_DEFAULT (0)
250 #define SWM_FOCUS_FOLLOW (1)
251 #define SWM_FOCUS_MANUAL (2)
253 #define SWM_CK_NONE 0
254 #define SWM_CK_ALL 0xf
255 #define SWM_CK_FOCUS 0x1
256 #define SWM_CK_POINTER 0x2
257 #define SWM_CK_FALLBACK 0x4
258 #define SWM_CK_REGION 0x8
260 #define SWM_CONF_DEFAULT (0)
261 #define SWM_CONF_KEYMAPPING (1)
264 #define SWM_LIB "/usr/local/lib/libswmhack.so"
271 xcb_atom_t a_takefocus
;
273 xcb_atom_t a_netwmname
;
274 xcb_atom_t a_utf8_string
;
276 xcb_atom_t a_swm_iconic
;
278 volatile sig_atomic_t running
= 1;
279 volatile sig_atomic_t restart_wm
= 0;
280 xcb_timestamp_t last_event_time
= 0;
284 int xrandr_eventbase
;
285 unsigned int numlockmask
= 0;
288 xcb_connection_t
*conn
;
289 xcb_key_symbols_t
*syms
;
292 int cycle_visible
= 0;
294 int font_adjusted
= 0;
295 unsigned int mod_key
= MODKEY
;
298 struct swm_region
*search_r
;
299 int select_list_pipe
[2];
300 int select_resp_pipe
[2];
302 volatile sig_atomic_t search_resp
;
303 int search_resp_action
;
305 struct search_window
{
306 TAILQ_ENTRY(search_window
) entry
;
310 xcb_window_t indicator
;
312 TAILQ_HEAD(search_winlist
, search_window
);
313 struct search_winlist search_wl
;
318 SWM_SEARCH_UNICONIFY
,
319 SWM_SEARCH_NAME_WORKSPACE
,
320 SWM_SEARCH_SEARCH_WORKSPACE
,
321 SWM_SEARCH_SEARCH_WINDOW
324 #define SWM_STACK_TOP (0)
325 #define SWM_STACK_BOTTOM (1)
326 #define SWM_STACK_ABOVE (2)
327 #define SWM_STACK_BELOW (3)
330 double dialog_ratio
= 0.6;
332 #define SWM_BAR_MAX (256)
333 #define SWM_BAR_JUSTIFY_LEFT (0)
334 #define SWM_BAR_JUSTIFY_CENTER (1)
335 #define SWM_BAR_JUSTIFY_RIGHT (2)
336 #define SWM_BAR_OFFSET (4)
337 #define SWM_BAR_FONTS "-*-terminus-medium-*-*-*-12-*-*-*-*-*-*-*," \
338 "-*-profont-*-*-*-*-12-*-*-*-*-*-*-*," \
339 "-*-times-medium-r-*-*-12-*-*-*-*-*-*-*," \
340 "-misc-fixed-medium-r-*-*-12-*-*-*-*-*-*-*," \
341 "-*-*-*-r-*-*-*-*-*-*-*-*-*-*"
343 #ifdef X_HAVE_UTF8_STRING
344 #define DRAWSTRING(x...) Xutf8DrawString(x)
346 #define DRAWSTRING(x...) XmbDrawString(x)
349 char *bar_argv
[] = { NULL
, NULL
};
351 char bar_ext
[SWM_BAR_MAX
];
352 char bar_ext_buf
[SWM_BAR_MAX
];
353 char bar_vertext
[SWM_BAR_MAX
];
356 int bar_border_width
= 1;
357 int bar_at_bottom
= 0;
361 int bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
362 char *bar_format
= NULL
;
363 int stack_enabled
= 1;
364 int clock_enabled
= 1;
365 int urgent_enabled
= 0;
366 char *clock_format
= NULL
;
367 int title_name_enabled
= 0;
368 int title_class_enabled
= 0;
369 int window_name_enabled
= 0;
370 int focus_mode
= SWM_FOCUS_DEFAULT
;
371 int focus_close
= SWM_STACK_BELOW
;
372 int focus_close_wrap
= 1;
373 int focus_default
= SWM_STACK_TOP
;
374 int spawn_position
= SWM_STACK_TOP
;
375 int disable_border
= 0;
376 int border_width
= 1;
377 int region_padding
= 0;
379 int verbose_layout
= 0;
383 XFontSetExtents
*bar_fs_extents
;
385 int bar_font_legacy
= 1;
387 XftColor bar_font_color
;
389 char *startup_exception
;
390 unsigned int nr_exceptions
= 0;
392 /* layout manager data */
393 struct swm_geometry
{
406 struct swm_geometry g
;
409 /* virtual "screens" */
411 TAILQ_ENTRY(swm_region
) entry
;
413 struct swm_geometry g
;
414 struct workspace
*ws
; /* current workspace on this region */
415 struct workspace
*ws_prior
; /* prior workspace on this region */
416 struct swm_screen
*s
; /* screen idx */
419 TAILQ_HEAD(swm_region_list
, swm_region
);
422 TAILQ_ENTRY(ws_win
) entry
;
424 xcb_window_t transient
;
425 struct ws_win
*focus_child
; /* focus on child transient */
426 struct swm_geometry g
; /* current geometry */
427 struct swm_geometry g_float
; /* region coordinates */
428 int g_floatvalid
; /* g_float geometry validity */
429 int floatmaxed
; /* whether maxed by max_stack */
435 unsigned int ewmh_flags
;
436 int font_size_boundary
[SWM_MAX_FONT_STEPS
];
442 unsigned long quirks
;
443 struct workspace
*ws
; /* always valid */
444 struct swm_screen
*s
; /* always valid, never changes */
445 xcb_get_geometry_reply_t
*wa
;
447 xcb_icccm_get_wm_class_reply_t ch
;
448 xcb_icccm_wm_hints_t hints
;
450 TAILQ_HEAD(ws_win_list
, ws_win
);
454 TAILQ_ENTRY(pid_e
) entry
;
458 TAILQ_HEAD(pid_list
, pid_e
);
459 struct pid_list pidlist
= TAILQ_HEAD_INITIALIZER(pidlist
);
461 /* layout handlers */
463 void vertical_config(struct workspace
*, int);
464 void vertical_stack(struct workspace
*, struct swm_geometry
*);
465 void horizontal_config(struct workspace
*, int);
466 void horizontal_stack(struct workspace
*, struct swm_geometry
*);
467 void max_stack(struct workspace
*, struct swm_geometry
*);
468 void plain_stacker(struct workspace
*);
469 void fancy_stacker(struct workspace
*);
472 void (*l_stack
)(struct workspace
*, struct swm_geometry
*);
473 void (*l_config
)(struct workspace
*, int);
475 #define SWM_L_FOCUSPREV (1<<0)
476 #define SWM_L_MAPONFOCUS (1<<1)
477 void (*l_string
)(struct workspace
*);
479 /* stack, configure */
480 { vertical_stack
, vertical_config
, 0, plain_stacker
},
481 { horizontal_stack
, horizontal_config
, 0, plain_stacker
},
483 SWM_L_MAPONFOCUS
| SWM_L_FOCUSPREV
, plain_stacker
},
484 { NULL
, NULL
, 0, NULL
},
487 /* position of max_stack mode in the layouts array, index into layouts! */
488 #define SWM_V_STACK (0)
489 #define SWM_H_STACK (1)
490 #define SWM_MAX_STACK (2)
492 #define SWM_H_SLICE (32)
493 #define SWM_V_SLICE (32)
495 /* define work spaces */
497 int idx
; /* workspace index */
498 char *name
; /* workspace name */
499 int always_raise
; /* raise windows on focus */
500 int bar_enabled
; /* bar visibility */
501 struct layout
*cur_layout
; /* current layout handlers */
502 struct ws_win
*focus
; /* may be NULL */
503 struct ws_win
*focus_prev
; /* may be NULL */
504 struct ws_win
*focus_pending
; /* may be NULL */
505 struct swm_region
*r
; /* may be NULL */
506 struct swm_region
*old_r
; /* may be NULL */
507 struct ws_win_list winlist
; /* list of windows in ws */
508 struct ws_win_list unmanagedlist
; /* list of dead windows in ws */
509 char stacker
[10]; /* display stacker and layout */
513 int horizontal_msize
;
515 int horizontal_stacks
;
526 SWM_S_COLOR_BAR_BORDER
,
527 SWM_S_COLOR_BAR_BORDER_UNFOCUS
,
528 SWM_S_COLOR_BAR_FONT
,
534 /* physical screen mapping */
535 #define SWM_WS_MAX (22) /* hard limit */
536 int workspace_limit
= 10; /* soft limit */
539 int idx
; /* screen index */
540 struct swm_region_list rl
; /* list of regions on this screen */
541 struct swm_region_list orl
; /* list of old regions */
543 struct workspace ws
[SWM_WS_MAX
];
544 struct swm_region
*r_focus
;
550 } c
[SWM_S_COLOR_MAX
];
552 xcb_gcontext_t bar_gc
;
555 struct swm_screen
*screens
;
557 /* args to functions */
560 #define SWM_ARG_ID_FOCUSNEXT (0)
561 #define SWM_ARG_ID_FOCUSPREV (1)
562 #define SWM_ARG_ID_FOCUSMAIN (2)
563 #define SWM_ARG_ID_SWAPNEXT (10)
564 #define SWM_ARG_ID_SWAPPREV (11)
565 #define SWM_ARG_ID_SWAPMAIN (12)
566 #define SWM_ARG_ID_MOVELAST (13)
567 #define SWM_ARG_ID_MASTERSHRINK (20)
568 #define SWM_ARG_ID_MASTERGROW (21)
569 #define SWM_ARG_ID_MASTERADD (22)
570 #define SWM_ARG_ID_MASTERDEL (23)
571 #define SWM_ARG_ID_FLIPLAYOUT (24)
572 #define SWM_ARG_ID_STACKRESET (30)
573 #define SWM_ARG_ID_STACKINIT (31)
574 #define SWM_ARG_ID_CYCLEWS_UP (40)
575 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
576 #define SWM_ARG_ID_CYCLERG_UP (42)
577 #define SWM_ARG_ID_CYCLERG_DOWN (43)
578 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
579 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
580 #define SWM_ARG_ID_STACKINC (50)
581 #define SWM_ARG_ID_STACKDEC (51)
582 #define SWM_ARG_ID_SS_ALL (60)
583 #define SWM_ARG_ID_SS_WINDOW (61)
584 #define SWM_ARG_ID_DONTCENTER (70)
585 #define SWM_ARG_ID_CENTER (71)
586 #define SWM_ARG_ID_KILLWINDOW (80)
587 #define SWM_ARG_ID_DELETEWINDOW (81)
588 #define SWM_ARG_ID_WIDTHGROW (90)
589 #define SWM_ARG_ID_WIDTHSHRINK (91)
590 #define SWM_ARG_ID_HEIGHTGROW (92)
591 #define SWM_ARG_ID_HEIGHTSHRINK (93)
592 #define SWM_ARG_ID_MOVEUP (100)
593 #define SWM_ARG_ID_MOVEDOWN (101)
594 #define SWM_ARG_ID_MOVELEFT (102)
595 #define SWM_ARG_ID_MOVERIGHT (103)
596 #define SWM_ARG_ID_BAR_TOGGLE (110)
597 #define SWM_ARG_ID_BAR_TOGGLE_WS (111)
603 TAILQ_ENTRY(quirk
) entry
;
607 #define SWM_Q_FLOAT (1<<0) /* float this window */
608 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
609 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
610 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
611 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
612 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
614 TAILQ_HEAD(quirk_list
, quirk
);
615 struct quirk_list quirks
= TAILQ_HEAD_INITIALIZER(quirks
);
618 * Supported EWMH hints should be added to
619 * both the enum and the ewmh array
624 _NET_MOVERESIZE_WINDOW
,
625 _NET_WM_ACTION_ABOVE
,
626 _NET_WM_ACTION_CLOSE
,
627 _NET_WM_ACTION_FULLSCREEN
,
629 _NET_WM_ACTION_RESIZE
,
630 _NET_WM_ALLOWED_ACTIONS
,
634 _NET_WM_STATE_FULLSCREEN
,
635 _NET_WM_STATE_HIDDEN
,
636 _NET_WM_STATE_MAXIMIZED_HORZ
,
637 _NET_WM_STATE_MAXIMIZED_VERT
,
638 _NET_WM_STATE_SKIP_PAGER
,
639 _NET_WM_STATE_SKIP_TASKBAR
,
641 _NET_WM_WINDOW_TYPE_DIALOG
,
642 _NET_WM_WINDOW_TYPE_DOCK
,
643 _NET_WM_WINDOW_TYPE_NORMAL
,
644 _NET_WM_WINDOW_TYPE_SPLASH
,
645 _NET_WM_WINDOW_TYPE_TOOLBAR
,
646 _NET_WM_WINDOW_TYPE_UTILITY
,
647 _SWM_WM_STATE_MANUAL
,
654 } ewmh
[SWM_EWMH_HINT_MAX
] = {
655 /* must be in same order as in the enum */
656 {"_NET_ACTIVE_WINDOW", XCB_ATOM_NONE
},
657 {"_NET_CLOSE_WINDOW", XCB_ATOM_NONE
},
658 {"_NET_MOVERESIZE_WINDOW", XCB_ATOM_NONE
},
659 {"_NET_WM_ACTION_ABOVE", XCB_ATOM_NONE
},
660 {"_NET_WM_ACTION_CLOSE", XCB_ATOM_NONE
},
661 {"_NET_WM_ACTION_FULLSCREEN", XCB_ATOM_NONE
},
662 {"_NET_WM_ACTION_MOVE", XCB_ATOM_NONE
},
663 {"_NET_WM_ACTION_RESIZE", XCB_ATOM_NONE
},
664 {"_NET_WM_ALLOWED_ACTIONS", XCB_ATOM_NONE
},
665 {"_NET_WM_NAME", XCB_ATOM_NONE
},
666 {"_NET_WM_STATE", XCB_ATOM_NONE
},
667 {"_NET_WM_STATE_ABOVE", XCB_ATOM_NONE
},
668 {"_NET_WM_STATE_FULLSCREEN", XCB_ATOM_NONE
},
669 {"_NET_WM_STATE_HIDDEN", XCB_ATOM_NONE
},
670 {"_NET_WM_STATE_MAXIMIZED_HORZ", XCB_ATOM_NONE
},
671 {"_NET_WM_STATE_MAXIMIZED_VERT", XCB_ATOM_NONE
},
672 {"_NET_WM_STATE_SKIP_PAGER", XCB_ATOM_NONE
},
673 {"_NET_WM_STATE_SKIP_TASKBAR", XCB_ATOM_NONE
},
674 {"_NET_WM_WINDOW_TYPE", XCB_ATOM_NONE
},
675 {"_NET_WM_WINDOW_TYPE_DIALOG", XCB_ATOM_NONE
},
676 {"_NET_WM_WINDOW_TYPE_DOCK", XCB_ATOM_NONE
},
677 {"_NET_WM_WINDOW_TYPE_NORMAL", XCB_ATOM_NONE
},
678 {"_NET_WM_WINDOW_TYPE_SPLASH", XCB_ATOM_NONE
},
679 {"_NET_WM_WINDOW_TYPE_TOOLBAR", XCB_ATOM_NONE
},
680 {"_NET_WM_WINDOW_TYPE_UTILITY", XCB_ATOM_NONE
},
681 {"_SWM_WM_STATE_MANUAL", XCB_ATOM_NONE
},
688 XC_BOTTOM_LEFT_CORNER
,
689 XC_BOTTOM_RIGHT_CORNER
,
697 char *name
; /* Name used by Xcursor .*/
698 uint8_t cf_char
; /* cursorfont index. */
700 } cursors
[XC_MAX
] = {
701 {"fleur", XC_fleur
, XCB_CURSOR_NONE
},
702 {"left_ptr", XC_left_ptr
, XCB_CURSOR_NONE
},
703 {"bottom_left_corner", XC_bottom_left_corner
, XCB_CURSOR_NONE
},
704 {"bottom_right_corner", XC_bottom_right_corner
, XCB_CURSOR_NONE
},
705 {"sizing", XC_sizing
, XCB_CURSOR_NONE
},
706 {"top_left_corner", XC_top_left_corner
, XCB_CURSOR_NONE
},
707 {"top_right_corner", XC_top_right_corner
, XCB_CURSOR_NONE
},
712 TAILQ_ENTRY(spawn_prog
) entry
;
717 TAILQ_HEAD(spawn_list
, spawn_prog
);
718 struct spawn_list spawns
= TAILQ_HEAD_INITIALIZER(spawns
);
720 /* user/key callable function IDs */
832 KF_DUMPWINS
, /* MUST BE LAST */
840 enum keyfuncid funcid
;
843 RB_HEAD(key_tree
, key
);
845 /* function prototypes */
846 void adjust_font(struct ws_win
*);
847 void bar_class_name(char *, size_t, struct swm_region
*);
848 void bar_class_title_name(char *, size_t, struct swm_region
*);
849 void bar_cleanup(struct swm_region
*);
850 void bar_extra_setup(void);
851 void bar_extra_stop(void);
852 int bar_extra_update(void);
853 void bar_fmt(const char *, char *, struct swm_region
*, size_t);
854 void bar_fmt_expand(char *, size_t);
856 void bar_print(struct swm_region
*, const char *);
857 void bar_print_legacy(struct swm_region
*, const char *);
858 void bar_replace(char *, char *, struct swm_region
*, size_t);
859 void bar_replace_pad(char *, int *, size_t);
860 char * bar_replace_seq(char *, char *, struct swm_region
*, size_t *,
862 void bar_setup(struct swm_region
*);
863 void bar_title_name(char *, size_t, struct swm_region
*);
864 void bar_toggle(struct swm_region
*, union arg
*);
865 void bar_urgent(char *, size_t);
866 void bar_window_float(char *, size_t, struct swm_region
*);
867 void bar_window_name(char *, size_t, struct swm_region
*);
868 void bar_workspace_name(char *, size_t, struct swm_region
*);
869 void buttonpress(xcb_button_press_event_t
*);
870 void check_conn(void);
871 void clear_keys(void);
872 void clientmessage(xcb_client_message_event_t
*);
873 void client_msg(struct ws_win
*, xcb_atom_t
, xcb_timestamp_t
);
874 int conf_load(const char *, int);
875 void configurenotify(xcb_configure_notify_event_t
*);
876 void configurerequest(xcb_configure_request_event_t
*);
877 void config_win(struct ws_win
*, xcb_configure_request_event_t
*);
878 void constrain_window(struct ws_win
*, struct swm_region
*, int);
879 int count_win(struct workspace
*, int);
880 void cursors_cleanup(void);
881 void cursors_load(void);
882 void custom_region(char *);
883 void cyclerg(struct swm_region
*, union arg
*);
884 void cyclews(struct swm_region
*, union arg
*);
885 void cycle_layout(struct swm_region
*, union arg
*);
886 void destroynotify(xcb_destroy_notify_event_t
*);
887 void dumpwins(struct swm_region
*, union arg
*);
889 void enternotify(xcb_enter_notify_event_t
*);
890 void event_drain(uint8_t);
891 void event_error(xcb_generic_error_t
*);
892 void event_handle(xcb_generic_event_t
*);
893 void ewmh_autoquirk(struct ws_win
*);
894 void ewmh_get_win_state(struct ws_win
*);
895 int ewmh_set_win_fullscreen(struct ws_win
*, int);
896 void ewmh_update_actions(struct ws_win
*);
897 void ewmh_update_win_state(struct ws_win
*, xcb_atom_t
, long);
898 char *expand_tilde(const char *);
899 void expose(xcb_expose_event_t
*);
900 void fake_keypress(struct ws_win
*, xcb_keysym_t
, uint16_t);
901 struct pid_e
*find_pid(pid_t
);
902 struct ws_win
*find_unmanaged_window(xcb_window_t
);
903 struct ws_win
*find_window(xcb_window_t
);
904 void floating_toggle(struct swm_region
*, union arg
*);
905 int floating_toggle_win(struct ws_win
*);
906 void focus(struct swm_region
*, union arg
*);
908 void focusin(xcb_focus_in_event_t
*);
909 void focusout(xcb_focus_out_event_t
*);
911 void focus_flush(void);
912 void focus_region(struct swm_region
*);
913 void focusrg(struct swm_region
*, union arg
*);
914 void focus_win(struct ws_win
*);
915 void fontset_init(void);
916 void free_window(struct ws_win
*);
917 xcb_atom_t
get_atom_from_string(const char *);
919 char *get_atom_name(xcb_atom_t
);
921 struct ws_win
*get_focus_magic(struct ws_win
*);
922 struct ws_win
*get_focus_prev(struct ws_win
*);
924 char *get_notify_detail_label(uint8_t);
925 char *get_notify_mode_label(uint8_t);
927 struct ws_win
*get_pointer_win(xcb_window_t
);
928 struct ws_win
*get_region_focus(struct swm_region
*);
929 int get_region_index(struct swm_region
*);
930 xcb_screen_t
*get_screen(int);
932 char *get_stack_mode_name(uint8_t);
934 int32_t get_swm_iconic(struct ws_win
*);
935 char *get_win_name(xcb_window_t
);
936 int get_ws_idx(xcb_window_t
);
937 uint32_t getstate(xcb_window_t
);
938 void grabbuttons(struct ws_win
*);
940 void grab_windows(void);
941 void iconify(struct swm_region
*, union arg
*);
943 void keypress(xcb_key_press_event_t
*);
944 int key_cmp(struct key
*, struct key
*);
945 void key_insert(unsigned int, KeySym
, enum keyfuncid
, const char *);
946 struct key
*key_lookup(unsigned int, KeySym
);
947 void key_remove(struct key
*);
948 void key_replace(struct key
*, unsigned int, KeySym
, enum keyfuncid
,
950 void kill_bar_extra_atexit(void);
951 void kill_refs(struct ws_win
*);
953 void leavenotify(xcb_leave_notify_event_t
*);
955 void load_float_geom(struct ws_win
*, struct swm_region
*);
956 struct ws_win
*manage_window(xcb_window_t
, uint16_t);
957 void map_window(struct ws_win
*);
958 void mapnotify(xcb_map_notify_event_t
*);
959 void mappingnotify(xcb_mapping_notify_event_t
*);
960 void maprequest(xcb_map_request_event_t
*);
961 void motionnotify(xcb_motion_notify_event_t
*);
962 void move(struct ws_win
*, union arg
*);
963 void move_step(struct swm_region
*, union arg
*);
964 uint32_t name_to_pixel(const char *);
965 void name_workspace(struct swm_region
*, union arg
*);
966 void new_region(struct swm_screen
*, int, int, int, int);
967 int parsekeys(char *, unsigned int, unsigned int *, KeySym
*);
968 int parsequirks(char *, unsigned long *);
969 int parse_rgb(const char *, uint16_t *, uint16_t *, uint16_t *);
970 void pressbutton(struct swm_region
*, union arg
*);
971 void priorws(struct swm_region
*, union arg
*);
973 void print_win_geom(xcb_window_t
);
975 void propertynotify(xcb_property_notify_event_t
*);
976 void quirk_insert(const char *, const char *, unsigned long);
977 void quirk_remove(struct quirk
*);
978 void quirk_replace(struct quirk
*, const char *, const char *,
980 void quit(struct swm_region
*, union arg
*);
981 void raise_toggle(struct swm_region
*, union arg
*);
982 void resize(struct ws_win
*, union arg
*);
983 void resize_step(struct swm_region
*, union arg
*);
984 void restart(struct swm_region
*, union arg
*);
985 struct swm_region
*root_to_region(xcb_window_t
, int);
986 void screenchange(xcb_randr_screen_change_notify_event_t
*);
987 void scan_xrandr(int);
988 void search_do_resp(void);
989 void search_resp_name_workspace(const char *, unsigned long);
990 void search_resp_search_window(const char *);
991 void search_resp_search_workspace(const char *);
992 void search_resp_uniconify(const char *, unsigned long);
993 void search_win(struct swm_region
*, union arg
*);
994 void search_win_cleanup(void);
995 void search_workspace(struct swm_region
*, union arg
*);
996 void send_to_rg(struct swm_region
*, union arg
*);
997 void send_to_ws(struct swm_region
*, union arg
*);
998 void set_region(struct swm_region
*);
999 int setautorun(char *, char *, int);
1000 int setconfbinding(char *, char *, int);
1001 int setconfcolor(char *, char *, int);
1002 int setconfmodkey(char *, char *, int);
1003 int setconfquirk(char *, char *, int);
1004 int setconfregion(char *, char *, int);
1005 int setconfspawn(char *, char *, int);
1006 int setconfvalue(char *, char *, int);
1007 void setkeybinding(unsigned int, KeySym
, enum keyfuncid
, const char *);
1008 int setkeymapping(char *, char *, int);
1009 int setlayout(char *, char *, int);
1010 void setquirk(const char *, const char *, unsigned long);
1011 void setscreencolor(char *, int, int);
1012 void setspawn(const char *, const char *);
1013 void setup_ewmh(void);
1014 void setup_globals(void);
1015 void setup_keys(void);
1016 void setup_quirks(void);
1017 void setup_screens(void);
1018 void setup_spawn(void);
1019 void set_child_transient(struct ws_win
*, xcb_window_t
*);
1020 void set_swm_iconic(struct ws_win
*, int);
1021 void set_win_state(struct ws_win
*, uint16_t);
1022 void shutdown_cleanup(void);
1024 void socket_setnonblock(int);
1025 void sort_windows(struct ws_win_list
*);
1026 void spawn(int, union arg
*, int);
1027 void spawn_custom(struct swm_region
*, union arg
*, const char *);
1028 int spawn_expand(struct swm_region
*, union arg
*, const char *, char ***);
1029 void spawn_insert(const char *, const char *);
1030 void spawn_remove(struct spawn_prog
*);
1031 void spawn_replace(struct spawn_prog
*, const char *, const char *);
1032 void spawn_select(struct swm_region
*, union arg
*, const char *, int *);
1033 void stack_config(struct swm_region
*, union arg
*);
1034 void stack_floater(struct ws_win
*, struct swm_region
*);
1035 void stack_master(struct workspace
*, struct swm_geometry
*, int, int);
1036 void store_float_geom(struct ws_win
*, struct swm_region
*);
1037 char * strdupsafe(const char *);
1038 void swapwin(struct swm_region
*, union arg
*);
1039 void switchws(struct swm_region
*, union arg
*);
1040 void teardown_ewmh(void);
1041 void unfocus_win(struct ws_win
*);
1042 void uniconify(struct swm_region
*, union arg
*);
1043 void unmanage_window(struct ws_win
*);
1044 void unmapnotify(xcb_unmap_notify_event_t
*);
1045 void unmap_all(void);
1046 void unmap_window(struct ws_win
*);
1047 void updatenumlockmask(void);
1048 void update_modkey(unsigned int);
1049 void update_window(struct ws_win
*);
1050 int validate_win(struct ws_win
*);
1051 int validate_ws(struct workspace
*);
1052 /*void visibilitynotify(xcb_visibility_notify_event_t *);*/
1053 void version(struct swm_region
*, union arg
*);
1054 pid_t
window_get_pid(xcb_window_t
);
1055 void wkill(struct swm_region
*, union arg
*);
1056 void workaround(void);
1057 void xft_init(struct swm_region
*);
1058 void _add_startup_exception(const char *, va_list);
1059 void add_startup_exception(const char *, ...);
1061 RB_PROTOTYPE(key_tree
, key
, entry
, key_cmp
);
1062 RB_GENERATE(key_tree
, key
, entry
, key_cmp
);
1063 struct key_tree keys
;
1068 xcb_font_t cf
= XCB_NONE
;
1071 for (i
= 0; i
< LENGTH(cursors
); ++i
) {
1072 /* try to load Xcursor first. */
1073 cursors
[i
].cid
= XcursorLibraryLoadCursor(display
,
1076 /* fallback to cursorfont. */
1077 if (cursors
[i
].cid
== XCB_CURSOR_NONE
) {
1078 if (cf
== XCB_NONE
) {
1079 cf
= xcb_generate_id(conn
);
1080 xcb_open_font(conn
, cf
, strlen("cursor"),
1084 cursors
[i
].cid
= xcb_generate_id(conn
);
1085 xcb_create_glyph_cursor(conn
, cursors
[i
].cid
, cf
, cf
,
1086 cursors
[i
].cf_char
, cursors
[i
].cf_char
+ 1, 0, 0, 0,
1087 0xffff, 0xffff, 0xffff);
1093 xcb_close_font(conn
, cf
);
1097 cursors_cleanup(void)
1100 for (i
= 0; i
< LENGTH(cursors
); ++i
)
1101 xcb_free_cursor(conn
, cursors
[i
].cid
);
1105 expand_tilde(const char *s
)
1107 struct passwd
*ppwd
;
1115 errx(1, "expand_tilde: NULL string.");
1118 result
= strdup(sc
);
1124 if ((max
= sysconf(_SC_LOGIN_NAME_MAX
)) == -1)
1125 errx(1, "expand_tilde: sysconf");
1127 if ((user
= calloc(1, max
+ 1)) == NULL
)
1128 errx(1, "expand_tilde: calloc");
1130 for (i
= 0; s
[i
] != '/' && s
[i
] != '\0'; ++i
)
1135 ppwd
= strlen(user
) == 0 ? getpwuid(getuid()) : getpwnam(user
);
1139 result
= strdup(sc
);
1141 if (asprintf(&result
, "%s%s", ppwd
->pw_dir
, s
) == -1)
1145 errx(1, "expand_tilde: failed to allocate memory.");
1151 parse_rgb(const char *rgb
, uint16_t *rr
, uint16_t *gg
, uint16_t *bb
)
1153 unsigned int tmpr
, tmpg
, tmpb
;
1155 if (sscanf(rgb
, "rgb:%x/%x/%x", &tmpr
, &tmpg
, &tmpb
) != 3)
1158 *rr
= RGB_8_TO_16(tmpr
);
1159 *gg
= RGB_8_TO_16(tmpg
);
1160 *bb
= RGB_8_TO_16(tmpb
);
1166 get_screen(int screen
)
1168 const xcb_setup_t
*r
;
1169 xcb_screen_iterator_t iter
;
1171 if ((r
= xcb_get_setup(conn
)) == NULL
) {
1172 DNPRINTF(SWM_D_MISC
, "get_screen: xcb_get_setup\n");
1176 iter
= xcb_setup_roots_iterator(r
);
1177 for (; iter
.rem
; --screen
, xcb_screen_next(&iter
))
1185 get_region_index(struct swm_region
*r
)
1187 struct swm_region
*rr
;
1193 TAILQ_FOREACH(rr
, &r
->s
->rl
, entry
) {
1208 if (focus_mode
== SWM_FOCUS_DEFAULT
)
1209 event_drain(XCB_ENTER_NOTIFY
);
1215 get_atom_from_string(const char *str
)
1217 xcb_intern_atom_cookie_t c
;
1218 xcb_intern_atom_reply_t
*r
;
1221 c
= xcb_intern_atom(conn
, 0, strlen(str
), str
);
1222 r
= xcb_intern_atom_reply(conn
, c
, NULL
);
1230 return (XCB_ATOM_NONE
);
1234 set_swm_iconic(struct ws_win
*win
, int newv
)
1241 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
1242 a_swm_iconic
, XCB_ATOM_INTEGER
, 32, 1, &v
);
1244 xcb_delete_property(conn
, win
->id
, a_swm_iconic
);
1248 get_swm_iconic(struct ws_win
*win
)
1251 xcb_get_property_reply_t
*pr
= NULL
;
1253 pr
= xcb_get_property_reply(conn
,
1254 xcb_get_property(conn
, 0, win
->id
, a_swm_iconic
,
1255 XCB_ATOM_INTEGER
, 0, 1), NULL
);
1258 if (pr
->type
!= XCB_ATOM_INTEGER
|| pr
->format
!= 32)
1260 v
= *((int32_t *)xcb_get_property_value(pr
));
1270 xcb_atom_t sup_list
;
1271 int i
, j
, num_screens
;
1273 sup_list
= get_atom_from_string("_NET_SUPPORTED");
1275 for (i
= 0; i
< LENGTH(ewmh
); i
++)
1276 ewmh
[i
].atom
= get_atom_from_string(ewmh
[i
].name
);
1278 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1279 for (i
= 0; i
< num_screens
; i
++) {
1280 /* Support check window will be created by workaround(). */
1282 /* Report supported atoms */
1283 xcb_delete_property(conn
, screens
[i
].root
, sup_list
);
1284 for (j
= 0; j
< LENGTH(ewmh
); j
++)
1285 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
,
1286 screens
[i
].root
, sup_list
, XCB_ATOM_ATOM
, 32, 1,
1295 xcb_atom_t sup_check
, sup_list
;
1297 xcb_get_property_cookie_t pc
;
1298 xcb_get_property_reply_t
*pr
;
1300 sup_check
= get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
1301 sup_list
= get_atom_from_string("_NET_SUPPORTED");
1302 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1304 for (i
= 0; i
< num_screens
; i
++) {
1305 /* Get the support check window and destroy it */
1306 pc
= xcb_get_property(conn
, 0, screens
[i
].root
, sup_check
,
1307 XCB_ATOM_WINDOW
, 0, 1);
1308 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
1311 if (pr
->format
== sup_check
) {
1312 id
= *((xcb_window_t
*)xcb_get_property_value(pr
));
1314 xcb_destroy_window(conn
, id
);
1315 xcb_delete_property(conn
, screens
[i
].root
, sup_check
);
1316 xcb_delete_property(conn
, screens
[i
].root
, sup_list
);
1323 ewmh_autoquirk(struct ws_win
*win
)
1327 xcb_get_property_cookie_t c
;
1328 xcb_get_property_reply_t
*r
;
1330 c
= xcb_get_property(conn
, 0, win
->id
,
1331 ewmh
[_NET_WM_WINDOW_TYPE
].atom
, XCB_ATOM_ATOM
, 0, UINT32_MAX
);
1332 r
= xcb_get_property_reply(conn
, c
, NULL
);
1336 n
= xcb_get_property_value_length(r
);
1337 type
= xcb_get_property_value(r
);
1339 for (i
= 0; i
< n
; i
++) {
1340 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_NORMAL
].atom
)
1342 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_DOCK
].atom
||
1343 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_TOOLBAR
].atom
||
1344 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_UTILITY
].atom
) {
1346 win
->quirks
= SWM_Q_FLOAT
| SWM_Q_ANYWHERE
;
1349 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_SPLASH
].atom
||
1350 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_DIALOG
].atom
) {
1352 win
->quirks
= SWM_Q_FLOAT
;
1359 #define SWM_EWMH_ACTION_COUNT_MAX (6)
1360 #define EWMH_F_FULLSCREEN (1<<0)
1361 #define EWMH_F_ABOVE (1<<1)
1362 #define EWMH_F_HIDDEN (1<<2)
1363 #define EWMH_F_SKIP_PAGER (1<<3)
1364 #define EWMH_F_SKIP_TASKBAR (1<<4)
1365 #define SWM_F_MANUAL (1<<5)
1368 ewmh_set_win_fullscreen(struct ws_win
*win
, int fs
)
1376 DNPRINTF(SWM_D_MISC
, "ewmh_set_win_fullscreen: window: 0x%x, "
1377 "fullscreen %s\n", win
->id
, YESNO(fs
));
1380 if (!win
->g_floatvalid
)
1381 store_float_geom(win
, win
->ws
->r
);
1383 win
->g
= win
->ws
->r
->g
;
1386 load_float_geom(win
, win
->ws
->r
);
1393 ewmh_update_actions(struct ws_win
*win
)
1395 xcb_atom_t actions
[SWM_EWMH_ACTION_COUNT_MAX
];
1401 actions
[n
++] = ewmh
[_NET_WM_ACTION_CLOSE
].atom
;
1403 if (win
->floating
) {
1404 actions
[n
++] = ewmh
[_NET_WM_ACTION_MOVE
].atom
;
1405 actions
[n
++] = ewmh
[_NET_WM_ACTION_RESIZE
].atom
;
1406 actions
[n
++] = ewmh
[_NET_WM_ACTION_ABOVE
].atom
;
1409 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
1410 ewmh
[_NET_WM_ALLOWED_ACTIONS
].atom
, XCB_ATOM_ATOM
, 32, 1, actions
);
1413 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
1414 #define _NET_WM_STATE_ADD 1 /* add/set property */
1415 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
1418 ewmh_update_win_state(struct ws_win
*win
, xcb_atom_t state
, long action
)
1420 unsigned int mask
= 0;
1421 unsigned int changed
= 0;
1422 unsigned int orig_flags
;
1427 DNPRINTF(SWM_D_PROP
, "ewmh_update_win_state: window: 0x%x, state: %ld, "
1428 "action: %ld\n", win
->id
, (unsigned long)state
, action
);
1430 if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
)
1431 mask
= EWMH_F_FULLSCREEN
;
1432 else if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
)
1433 mask
= EWMH_F_ABOVE
;
1434 else if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
)
1435 mask
= SWM_F_MANUAL
;
1436 else if (state
== ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
)
1437 mask
= EWMH_F_SKIP_PAGER
;
1438 else if (state
== ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
)
1439 mask
= EWMH_F_SKIP_TASKBAR
;
1441 orig_flags
= win
->ewmh_flags
;
1444 case _NET_WM_STATE_REMOVE
:
1445 win
->ewmh_flags
&= ~mask
;
1447 case _NET_WM_STATE_ADD
:
1448 win
->ewmh_flags
|= mask
;
1450 case _NET_WM_STATE_TOGGLE
:
1451 win
->ewmh_flags
^= mask
;
1455 changed
= (win
->ewmh_flags
& mask
) ^ (orig_flags
& mask
) ? 1 : 0;
1457 if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
) {
1458 if (changed
&& !floating_toggle_win(win
))
1459 win
->ewmh_flags
= orig_flags
; /* revert */
1460 } else if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
) {
1462 win
->manual
= (win
->ewmh_flags
& SWM_F_MANUAL
) != 0;
1463 } else if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
) {
1464 if (changed
&& !ewmh_set_win_fullscreen(win
,
1465 win
->ewmh_flags
& EWMH_F_FULLSCREEN
))
1466 win
->ewmh_flags
= orig_flags
; /* revert */
1469 xcb_delete_property(conn
, win
->id
, ewmh
[_NET_WM_STATE
].atom
);
1471 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
1472 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1473 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1474 &ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
);
1475 else if (win
->ewmh_flags
& EWMH_F_SKIP_PAGER
)
1476 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1477 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1478 &ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
);
1479 else if (win
->ewmh_flags
& EWMH_F_SKIP_TASKBAR
)
1480 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1481 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1482 &ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
);
1483 else if (win
->ewmh_flags
& EWMH_F_ABOVE
)
1484 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1485 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1486 &ewmh
[_NET_WM_STATE_ABOVE
].atom
);
1487 else if (win
->ewmh_flags
& SWM_F_MANUAL
)
1488 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1489 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1490 &ewmh
[_SWM_WM_STATE_MANUAL
].atom
);
1494 ewmh_get_win_state(struct ws_win
*win
)
1497 xcb_get_property_cookie_t c
;
1498 xcb_get_property_reply_t
*r
;
1504 win
->ewmh_flags
= 0;
1506 win
->ewmh_flags
|= EWMH_F_ABOVE
;
1508 win
->ewmh_flags
|= SWM_F_MANUAL
;
1510 c
= xcb_get_property(conn
, 0, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
1511 XCB_ATOM_ATOM
, 0, UINT32_MAX
);
1512 r
= xcb_get_property_reply(conn
, c
, NULL
);
1516 states
= xcb_get_property_value(r
);
1517 n
= xcb_get_property_value_length(r
);
1519 for (i
= 0; i
< n
; i
++)
1520 ewmh_update_win_state(win
, states
[i
], _NET_WM_STATE_ADD
);
1528 dumpwins(struct swm_region
*r
, union arg
*args
)
1532 xcb_get_window_attributes_cookie_t c
;
1533 xcb_get_window_attributes_reply_t
*wa
;
1535 /* suppress unused warning since var is needed */
1538 if (r
->ws
== NULL
) {
1539 warnx("dumpwins: invalid workspace");
1543 warnx("=== managed window list ws %02d ===", r
->ws
->idx
);
1544 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
1545 state
= getstate(win
->id
);
1546 c
= xcb_get_window_attributes(conn
, win
->id
);
1547 wa
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
1549 warnx("window: 0x%x, map_state: %d, state: %u, "
1550 "transient: 0x%x", win
->id
, wa
->map_state
,
1551 state
, win
->transient
);
1554 warnx("window: 0x%x, failed xcb_get_window_attributes",
1558 warnx("===== unmanaged window list =====");
1559 TAILQ_FOREACH(win
, &r
->ws
->unmanagedlist
, entry
) {
1560 state
= getstate(win
->id
);
1561 c
= xcb_get_window_attributes(conn
, win
->id
);
1562 wa
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
1564 warnx("window: 0x%x, map_state: %d, state: %u, "
1565 "transient: 0x%x", win
->id
, wa
->map_state
,
1566 state
, win
->transient
);
1569 warnx("window: 0x%x, failed xcb_get_window_attributes",
1573 warnx("=================================");
1577 dumpwins(struct swm_region
*r
, union arg
*s
)
1582 #endif /* SWM_DEBUG */
1587 int saved_errno
, status
;
1590 saved_errno
= errno
;
1594 while ((pid
= waitpid(WAIT_ANY
, &status
, WNOHANG
)) != 0) {
1599 if (errno
!= ECHILD
)
1600 warn("sighdlr: waitpid");
1601 #endif /* SWM_DEBUG */
1604 if (pid
== searchpid
)
1608 if (WIFEXITED(status
)) {
1609 if (WEXITSTATUS(status
) != 0)
1610 warnx("sighdlr: child exit status: %d",
1611 WEXITSTATUS(status
));
1613 warnx("sighdlr: child is terminated "
1615 #endif /* SWM_DEBUG */
1629 errno
= saved_errno
;
1635 struct pid_e
*p
= NULL
;
1637 DNPRINTF(SWM_D_MISC
, "find_pid: %d\n", pid
);
1642 TAILQ_FOREACH(p
, &pidlist
, entry
) {
1651 name_to_pixel(const char *colorname
)
1653 uint32_t result
= 0;
1654 char cname
[32] = "#";
1655 xcb_screen_t
*screen
;
1656 xcb_colormap_t cmap
;
1657 xcb_alloc_color_reply_t
*cr
;
1658 xcb_alloc_named_color_reply_t
*nr
;
1659 uint16_t rr
, gg
, bb
;
1661 screen
= xcb_setup_roots_iterator(xcb_get_setup(conn
)).data
;
1662 cmap
= screen
->default_colormap
;
1664 /* color is in format rgb://rr/gg/bb */
1665 if (strncmp(colorname
, "rgb:", 4) == 0) {
1666 if (parse_rgb(colorname
, &rr
, &gg
, &bb
) == -1)
1667 warnx("could not parse rgb %s", colorname
);
1669 cr
= xcb_alloc_color_reply(conn
,
1670 xcb_alloc_color(conn
, cmap
, rr
, gg
, bb
),
1676 warnx("color '%s' not found", colorname
);
1679 nr
= xcb_alloc_named_color_reply(conn
,
1680 xcb_alloc_named_color(conn
, cmap
, strlen(colorname
),
1683 strlcat(cname
, colorname
+ 2, sizeof cname
- 1);
1684 nr
= xcb_alloc_named_color_reply(conn
,
1685 xcb_alloc_named_color(conn
, cmap
, strlen(cname
),
1692 warnx("color '%s' not found", colorname
);
1699 setscreencolor(char *val
, int i
, int c
)
1703 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1704 if (i
> 0 && i
<= num_screens
) {
1705 screens
[i
- 1].c
[c
].pixel
= name_to_pixel(val
);
1706 free(screens
[i
- 1].c
[c
].name
);
1707 if ((screens
[i
- 1].c
[c
].name
= strdup(val
)) == NULL
)
1709 } else if (i
== -1) {
1710 for (i
= 0; i
< num_screens
; i
++) {
1711 screens
[i
].c
[c
].pixel
= name_to_pixel(val
);
1712 free(screens
[i
].c
[c
].name
);
1713 if ((screens
[i
].c
[c
].name
= strdup(val
)) == NULL
)
1717 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1722 fancy_stacker(struct workspace
*ws
)
1724 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1725 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1726 snprintf(ws
->stacker
, sizeof ws
->stacker
,
1727 ws
->l_state
.vertical_flip
? "[%d>%d]" : "[%d|%d]",
1728 ws
->l_state
.vertical_mwin
, ws
->l_state
.vertical_stacks
);
1729 if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1730 snprintf(ws
->stacker
, sizeof ws
->stacker
,
1731 ws
->l_state
.horizontal_flip
? "[%dv%d]" : "[%d-%d]",
1732 ws
->l_state
.horizontal_mwin
, ws
->l_state
.horizontal_stacks
);
1736 plain_stacker(struct workspace
*ws
)
1738 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1739 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1740 strlcpy(ws
->stacker
, ws
->l_state
.vertical_flip
? "[>]" : "[|]",
1741 sizeof ws
->stacker
);
1742 if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1743 strlcpy(ws
->stacker
, ws
->l_state
.horizontal_flip
? "[v]" : "[-]",
1744 sizeof ws
->stacker
);
1748 custom_region(char *val
)
1750 unsigned int x
, y
, w
, h
;
1751 int sidx
, num_screens
;
1752 xcb_screen_t
*screen
;
1754 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1755 if (sscanf(val
, "screen[%d]:%ux%u+%u+%u", &sidx
, &w
, &h
, &x
, &y
) != 5)
1756 errx(1, "invalid custom region, "
1757 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1758 if (sidx
< 1 || sidx
> num_screens
)
1759 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1763 if ((screen
= get_screen(sidx
)) == NULL
)
1764 errx(1, "ERROR: can't get screen %d.", sidx
);
1767 errx(1, "region %ux%u+%u+%u too small", w
, h
, x
, y
);
1769 if (x
> screen
->width_in_pixels
||
1770 y
> screen
->height_in_pixels
||
1771 w
+ x
> screen
->width_in_pixels
||
1772 h
+ y
> screen
->height_in_pixels
) {
1773 warnx("ignoring region %ux%u+%u+%u - not within screen "
1774 "boundaries (%ux%u)", w
, h
, x
, y
,
1775 screen
->width_in_pixels
, screen
->height_in_pixels
);
1779 new_region(&screens
[sidx
], x
, y
, w
, h
);
1783 socket_setnonblock(int fd
)
1787 if ((flags
= fcntl(fd
, F_GETFL
, 0)) == -1)
1788 err(1, "fcntl F_GETFL");
1789 flags
|= O_NONBLOCK
;
1790 if ((flags
= fcntl(fd
, F_SETFL
, flags
)) == -1)
1791 err(1, "fcntl F_SETFL");
1795 bar_print_legacy(struct swm_region
*r
, const char *s
)
1797 xcb_rectangle_t rect
;
1802 XRectangle ibox
, lbox
;
1806 XmbTextExtents(bar_fs
, s
, len
, &ibox
, &lbox
);
1808 switch (bar_justify
) {
1809 case SWM_BAR_JUSTIFY_LEFT
:
1812 case SWM_BAR_JUSTIFY_CENTER
:
1813 x
= (WIDTH(r
) - lbox
.width
) / 2;
1815 case SWM_BAR_JUSTIFY_RIGHT
:
1816 x
= WIDTH(r
) - lbox
.width
- SWM_BAR_OFFSET
;
1820 if (x
< SWM_BAR_OFFSET
)
1825 rect
.width
= WIDTH(r
->bar
);
1826 rect
.height
= HEIGHT(r
->bar
);
1828 /* clear back buffer */
1829 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
1830 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
1831 xcb_poly_fill_rectangle(conn
, r
->bar
->buffer
, r
->s
->bar_gc
, 1, &rect
);
1833 /* draw back buffer */
1834 gcvd
.graphics_exposures
= 0;
1835 draw
= XCreateGC(display
, r
->bar
->buffer
, GCGraphicsExposures
, &gcvd
);
1836 XSetForeground(display
, draw
, r
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
);
1837 DRAWSTRING(display
, r
->bar
->buffer
, bar_fs
, draw
,
1838 x
, (bar_fs_extents
->max_logical_extent
.height
- lbox
.height
) / 2 -
1840 XFreeGC(display
, draw
);
1843 xcb_copy_area(conn
, r
->bar
->buffer
, r
->bar
->id
, r
->s
->bar_gc
, 0, 0,
1844 0, 0, WIDTH(r
->bar
), HEIGHT(r
->bar
));
1848 bar_print(struct swm_region
*r
, const char *s
)
1851 xcb_rectangle_t rect
;
1859 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
, &info
);
1861 switch (bar_justify
) {
1862 case SWM_BAR_JUSTIFY_LEFT
:
1865 case SWM_BAR_JUSTIFY_CENTER
:
1866 x
= (WIDTH(r
) - info
.width
) / 2;
1868 case SWM_BAR_JUSTIFY_RIGHT
:
1869 x
= WIDTH(r
) - info
.width
- SWM_BAR_OFFSET
;
1873 if (x
< SWM_BAR_OFFSET
)
1878 rect
.width
= WIDTH(r
->bar
);
1879 rect
.height
= HEIGHT(r
->bar
);
1881 /* clear back buffer */
1882 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
1883 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
1884 xcb_poly_fill_rectangle(conn
, r
->bar
->buffer
, r
->s
->bar_gc
, 1, &rect
);
1886 /* draw back buffer */
1887 draw
= XftDrawCreate(display
, r
->bar
->buffer
,
1888 DefaultVisual(display
, r
->s
->idx
),
1889 DefaultColormap(display
, r
->s
->idx
));
1891 XftDrawStringUtf8(draw
, &bar_font_color
, bar_font
, x
,
1892 (HEIGHT(r
->bar
) + bar_font
->height
) / 2 - bar_font
->descent
,
1895 XftDrawDestroy(draw
);
1898 xcb_copy_area(conn
, r
->bar
->buffer
, r
->bar
->id
, r
->s
->bar_gc
, 0, 0,
1899 0, 0, WIDTH(r
->bar
), HEIGHT(r
->bar
));
1903 bar_extra_stop(void)
1907 bzero(bar_pipe
, sizeof bar_pipe
);
1910 kill(bar_pid
, SIGTERM
);
1913 strlcpy(bar_ext
, "", sizeof bar_ext
);
1918 bar_class_name(char *s
, size_t sz
, struct swm_region
*r
)
1920 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1922 if (r
->ws
->focus
->ch
.class_name
!= NULL
)
1923 strlcat(s
, r
->ws
->focus
->ch
.class_name
, sz
);
1927 bar_title_name(char *s
, size_t sz
, struct swm_region
*r
)
1929 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1931 if (r
->ws
->focus
->ch
.instance_name
!= NULL
)
1932 strlcat(s
, r
->ws
->focus
->ch
.instance_name
, sz
);
1936 bar_class_title_name(char *s
, size_t sz
, struct swm_region
*r
)
1938 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1941 bar_class_name(s
, sz
, r
);
1942 strlcat(s
, ":", sz
);
1943 bar_title_name(s
, sz
, r
);
1947 bar_window_float(char *s
, size_t sz
, struct swm_region
*r
)
1949 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1951 if (r
->ws
->focus
->floating
)
1952 strlcat(s
, "(f)", sz
);
1956 bar_window_name(char *s
, size_t sz
, struct swm_region
*r
)
1960 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1962 if ((title
= get_win_name(r
->ws
->focus
->id
)) == NULL
)
1965 strlcat(s
, title
, sz
);
1969 int urgent
[SWM_WS_MAX
];
1971 bar_urgent(char *s
, size_t sz
)
1974 int i
, j
, num_screens
;
1976 xcb_get_property_cookie_t c
;
1977 xcb_icccm_wm_hints_t hints
;
1979 for (i
= 0; i
< workspace_limit
; i
++)
1982 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1983 for (i
= 0; i
< num_screens
; i
++)
1984 for (j
= 0; j
< workspace_limit
; j
++)
1985 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
) {
1986 c
= xcb_icccm_get_wm_hints(conn
, win
->id
);
1987 if (xcb_icccm_get_wm_hints_reply(conn
, c
,
1990 if (hints
.flags
& XCB_ICCCM_WM_HINT_X_URGENCY
)
1994 for (i
= 0; i
< workspace_limit
; i
++) {
1996 snprintf(b
, sizeof b
, "%d ", i
+ 1);
1998 snprintf(b
, sizeof b
, "- ");
2004 bar_workspace_name(char *s
, size_t sz
, struct swm_region
*r
)
2006 if (r
== NULL
|| r
->ws
== NULL
)
2008 if (r
->ws
->name
!= NULL
)
2009 strlcat(s
, r
->ws
->name
, sz
);
2012 /* build the default bar format according to the defined enabled options */
2014 bar_fmt(const char *fmtexp
, char *fmtnew
, struct swm_region
*r
, size_t sz
)
2016 /* if format provided, just copy the buffers */
2017 if (bar_format
!= NULL
) {
2018 strlcpy(fmtnew
, fmtexp
, sz
);
2022 /* reset the output buffer */
2025 strlcat(fmtnew
, "+N:+I ", sz
);
2027 strlcat(fmtnew
, "+S", sz
);
2028 strlcat(fmtnew
, " ", sz
);
2030 /* only show the workspace name if there's actually one */
2031 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->name
!= NULL
)
2032 strlcat(fmtnew
, "<+D>", sz
);
2033 strlcat(fmtnew
, "+3<", sz
);
2035 if (clock_enabled
) {
2036 strlcat(fmtnew
, fmtexp
, sz
);
2037 strlcat(fmtnew
, "+4<", sz
);
2040 /* bar_urgent already adds the space before the last asterisk */
2042 strlcat(fmtnew
, "* +U*+4<", sz
);
2044 if (title_class_enabled
) {
2045 strlcat(fmtnew
, "+C", sz
);
2046 if (!title_name_enabled
)
2047 strlcat(fmtnew
, "+4<", sz
);
2050 /* checks needed by the colon and floating strlcat(3) calls below */
2051 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->focus
!= NULL
) {
2052 if (title_name_enabled
) {
2053 if (title_class_enabled
)
2054 strlcat(fmtnew
, ":", sz
);
2055 strlcat(fmtnew
, "+T+4<", sz
);
2057 if (window_name_enabled
) {
2058 if (r
->ws
->focus
->floating
)
2059 strlcat(fmtnew
, "+F ", sz
);
2060 strlcat(fmtnew
, "+64W ", sz
);
2064 /* finally add the action script output and the version */
2065 strlcat(fmtnew
, "+4<+A+4<+V", sz
);
2069 bar_replace_pad(char *tmp
, int *limit
, size_t sz
)
2071 /* special case; no limit given, pad one space, instead */
2072 if (*limit
== (int)sz
- 1)
2074 snprintf(tmp
, sz
, "%*s", *limit
, " ");
2077 /* replaces the bar format character sequences (like in tmux(1)) */
2079 bar_replace_seq(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t *offrep
,
2083 char tmp
[SWM_BAR_MAX
];
2087 /* reset strlcat(3) buffer */
2090 /* get number, if any */
2093 if (sscanf(fmt
, "%d%n", &limit
, &size
) != 1)
2094 limit
= sizeof tmp
- 1;
2095 if (limit
<= 0 || limit
>= (int)sizeof tmp
)
2096 limit
= sizeof tmp
- 1;
2098 /* there is nothing to replace (ie EOL) */
2105 bar_replace_pad(tmp
, &limit
, sizeof tmp
);
2108 snprintf(tmp
, sizeof tmp
, "%s", bar_ext
);
2111 bar_class_name(tmp
, sizeof tmp
, r
);
2114 bar_workspace_name(tmp
, sizeof tmp
, r
);
2117 bar_window_float(tmp
, sizeof tmp
, r
);
2120 snprintf(tmp
, sizeof tmp
, "%d", r
->ws
->idx
+ 1);
2123 snprintf(tmp
, sizeof tmp
, "%d", r
->s
->idx
+ 1);
2126 bar_class_title_name(tmp
, sizeof tmp
, r
);
2129 snprintf(tmp
, sizeof tmp
, "%s", r
->ws
->stacker
);
2132 bar_title_name(tmp
, sizeof tmp
, r
);
2135 bar_urgent(tmp
, sizeof tmp
);
2138 snprintf(tmp
, sizeof tmp
, "%s", bar_vertext
);
2141 bar_window_name(tmp
, sizeof tmp
, r
);
2144 /* unknown character sequence; copy as-is */
2145 snprintf(tmp
, sizeof tmp
, "+%c", *fmt
);
2151 if ((int)len
< limit
)
2153 while (limit
-- > 0) {
2154 if (*offrep
>= sz
- 1)
2156 fmtrep
[(*offrep
)++] = *ptr
++;
2164 bar_replace(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t sz
)
2169 while (*fmt
!= '\0') {
2171 /* skip ordinary characters */
2174 fmtrep
[off
++] = *fmt
++;
2178 /* character sequence found; replace it */
2179 fmt
= bar_replace_seq(fmt
, fmtrep
, r
, &off
, sz
);
2188 bar_fmt_expand(char *fmtexp
, size_t sz
)
2195 /* start by grabbing the current time and date */
2197 localtime_r(&tmt
, &tm
);
2199 /* figure out what to expand */
2200 if (bar_format
!= NULL
)
2202 else if (bar_format
== NULL
&& clock_enabled
)
2204 /* if nothing to expand bail out */
2210 /* copy as-is, just in case the format shouldn't be expanded below */
2211 strlcpy(fmtexp
, fmt
, sz
);
2212 /* finally pass the string through strftime(3) */
2213 #ifndef SWM_DENY_CLOCK_FORMAT
2214 if ((len
= strftime(fmtexp
, sz
, fmt
, &tm
)) == 0)
2215 warnx("format too long");
2220 /* Redraws the bar; need to follow with xcb_flush() or focus_flush(). */
2224 char fmtexp
[SWM_BAR_MAX
], fmtnew
[SWM_BAR_MAX
];
2225 char fmtrep
[SWM_BAR_MAX
];
2227 struct swm_region
*r
;
2229 /* expand the format by first passing it through strftime(3) */
2230 bar_fmt_expand(fmtexp
, sizeof fmtexp
);
2232 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2233 for (i
= 0; i
< num_screens
; i
++) {
2234 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
2238 if (bar_enabled
&& r
->ws
->bar_enabled
)
2239 xcb_map_window(conn
, r
->bar
->id
);
2241 xcb_unmap_window(conn
, r
->bar
->id
);
2245 if (startup_exception
)
2246 snprintf(fmtrep
, sizeof fmtrep
, "total "
2247 "exceptions: %d, first exception: %s",
2251 bar_fmt(fmtexp
, fmtnew
, r
, sizeof fmtnew
);
2252 bar_replace(fmtnew
, fmtrep
, r
, sizeof fmtrep
);
2254 if (bar_font_legacy
)
2255 bar_print_legacy(r
, fmtrep
);
2257 bar_print(r
, fmtrep
);
2263 * Reads external script output; call when stdin is readable.
2264 * Returns 1 if bar_ext was updated; otherwise 0.
2267 bar_extra_update(void)
2270 char b
[SWM_BAR_MAX
];
2276 while (fgets(b
, sizeof(b
), stdin
) != NULL
) {
2279 if (b
[len
- 1] == '\n') {
2280 /* Remove newline. */
2283 /* "Clear" bar_ext. */
2286 /* Flush buffered output. */
2287 strlcpy(bar_ext
, bar_ext_buf
, sizeof(bar_ext
));
2288 bar_ext_buf
[0] = '\0';
2290 /* Append new output to bar. */
2291 strlcat(bar_ext
, b
, sizeof(bar_ext
));
2295 /* Buffer output. */
2296 strlcat(bar_ext_buf
, b
, sizeof(bar_ext_buf
));
2301 if (errno
!= EAGAIN
) {
2302 warn("bar_action failed");
2311 bar_toggle(struct swm_region
*r
, union arg
*args
)
2313 struct swm_region
*tmpr
;
2316 /* suppress unused warnings since vars are needed */
2320 DNPRINTF(SWM_D_BAR
, "bar_toggle\n");
2323 case SWM_ARG_ID_BAR_TOGGLE_WS
:
2324 /* Only change if master switch is enabled. */
2326 r
->ws
->bar_enabled
= !r
->ws
->bar_enabled
;
2328 bar_enabled
= r
->ws
->bar_enabled
= 1;
2330 case SWM_ARG_ID_BAR_TOGGLE
:
2331 bar_enabled
= !bar_enabled
;
2335 /* update bars as necessary */
2336 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2337 for (i
= 0; i
< num_screens
; i
++)
2338 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
2340 if (bar_enabled
&& tmpr
->ws
->bar_enabled
)
2341 xcb_map_window(conn
, tmpr
->bar
->id
);
2343 xcb_unmap_window(conn
, tmpr
->bar
->id
);
2348 /* must be after stack */
2355 bar_extra_setup(void)
2357 /* do this here because the conf file is in memory */
2358 if (!bar_extra
&& bar_argv
[0]) {
2359 /* launch external status app */
2361 if (pipe(bar_pipe
) == -1)
2362 err(1, "pipe error");
2363 socket_setnonblock(bar_pipe
[0]);
2364 socket_setnonblock(bar_pipe
[1]); /* XXX hmmm, really? */
2366 /* Set stdin to read from the pipe. */
2367 if (dup2(bar_pipe
[0], STDIN_FILENO
) == -1)
2370 /* Set stdout to write to the pipe. */
2371 if (dup2(bar_pipe
[1], STDOUT_FILENO
) == -1)
2374 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
2375 err(1, "could not disable SIGPIPE");
2376 switch (bar_pid
= fork()) {
2378 err(1, "cannot fork");
2382 execvp(bar_argv
[0], bar_argv
);
2383 err(1, "%s external app failed", bar_argv
[0]);
2385 default: /* parent */
2390 atexit(kill_bar_extra_atexit
);
2395 kill_bar_extra_atexit(void)
2398 kill(bar_pid
, SIGTERM
);
2406 while ((s
= index(s
, '-'))) {
2411 return (count
== 14);
2417 char *default_string
;
2418 char **missing_charsets
;
2419 int num_missing_charsets
= 0;
2423 XFreeFontSet(display
, bar_fs
);
2427 DNPRINTF(SWM_D_INIT
, "fontset_init: loading bar_fonts: %s\n", bar_fonts
);
2429 bar_fs
= XCreateFontSet(display
, bar_fonts
, &missing_charsets
,
2430 &num_missing_charsets
, &default_string
);
2432 if (num_missing_charsets
> 0) {
2433 warnx("Unable to load charset(s):");
2435 for (i
= 0; i
< num_missing_charsets
; ++i
)
2436 warnx("%s", missing_charsets
[i
]);
2438 XFreeStringList(missing_charsets
);
2440 if (strcmp(default_string
, ""))
2441 warnx("Glyphs from those sets will be replaced "
2442 "by '%s'.", default_string
);
2444 warnx("Glyphs from those sets won't be drawn.");
2448 errx(1, "Error creating font set structure.");
2450 bar_fs_extents
= XExtentsOfFontSet(bar_fs
);
2452 bar_height
= bar_fs_extents
->max_logical_extent
.height
+
2453 2 * bar_border_width
;
2460 xft_init(struct swm_region
*r
)
2462 char *font
, *d
, *search
;
2465 if (bar_font
== NULL
) {
2466 if ((d
= strdup(bar_fonts
)) == NULL
)
2467 errx(1, "insufficient memory.");
2469 while ((font
= strsep(&search
, ",")) != NULL
) {
2473 DNPRINTF(SWM_D_INIT
, "xft_init: try font %s\n", font
);
2476 bar_font
= XftFontOpenXlfd(display
, r
->s
->idx
,
2479 bar_font
= XftFontOpenName(display
, r
->s
->idx
,
2484 warnx("unable to load font %s", font
);
2487 DNPRINTF(SWM_D_INIT
, "successfully opened "
2495 if (bar_font
== NULL
)
2496 errx(1, "unable to open a font");
2498 PIXEL_TO_XRENDERCOLOR(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
, color
);
2500 if (!XftColorAllocValue(display
, DefaultVisual(display
, r
->s
->idx
),
2501 DefaultColormap(display
, r
->s
->idx
), &color
, &bar_font_color
))
2502 warn("Xft error: unable to allocate color.");
2504 bar_height
= bar_font
->height
+ 2 * bar_border_width
;
2511 bar_setup(struct swm_region
*r
)
2513 xcb_screen_t
*screen
;
2516 DNPRINTF(SWM_D_BAR
, "bar_setup: screen %d.\n",
2519 if ((screen
= get_screen(r
->s
->idx
)) == NULL
)
2520 errx(1, "ERROR: can't get screen %d.", r
->s
->idx
);
2525 if ((r
->bar
= calloc(1, sizeof(struct swm_bar
))) == NULL
)
2526 err(1, "bar_setup: calloc: failed to allocate memory.");
2528 if (bar_font_legacy
)
2534 Y(r
->bar
) = bar_at_bottom
? (Y(r
) + HEIGHT(r
) - bar_height
) : Y(r
);
2535 WIDTH(r
->bar
) = WIDTH(r
) - 2 * bar_border_width
;
2536 HEIGHT(r
->bar
) = bar_height
- 2 * bar_border_width
;
2538 /* Assume region is unfocused when we create the bar. */
2539 r
->bar
->id
= xcb_generate_id(conn
);
2540 wa
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
2541 wa
[1] = r
->s
->c
[SWM_S_COLOR_BAR_BORDER_UNFOCUS
].pixel
;
2542 wa
[2] = XCB_EVENT_MASK_EXPOSURE
;
2544 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, r
->bar
->id
, r
->s
->root
,
2545 X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
), HEIGHT(r
->bar
),
2546 bar_border_width
, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
2547 XCB_COPY_FROM_PARENT
, XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
2548 | XCB_CW_EVENT_MASK
, wa
);
2550 r
->bar
->buffer
= xcb_generate_id(conn
);
2551 xcb_create_pixmap(conn
, screen
->root_depth
, r
->bar
->buffer
, r
->bar
->id
,
2552 WIDTH(r
->bar
), HEIGHT(r
->bar
));
2555 xcb_randr_select_input(conn
, r
->bar
->id
,
2556 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE
);
2559 xcb_map_window(conn
, r
->bar
->id
);
2561 DNPRINTF(SWM_D_BAR
, "bar_setup: window: 0x%x, (x,y) w x h: (%d,%d) "
2562 "%d x %d\n", WINID(r
->bar
), X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
),
2569 bar_cleanup(struct swm_region
*r
)
2573 xcb_destroy_window(conn
, r
->bar
->id
);
2574 xcb_free_pixmap(conn
, r
->bar
->buffer
);
2580 set_win_state(struct ws_win
*win
, uint16_t state
)
2582 uint16_t data
[2] = { state
, XCB_ATOM_NONE
};
2584 DNPRINTF(SWM_D_EVENT
, "set_win_state: window: 0x%x, state: %u\n",
2590 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
, a_state
,
2591 a_state
, 32, 2, data
);
2595 getstate(xcb_window_t w
)
2597 uint32_t result
= 0;
2598 xcb_get_property_cookie_t c
;
2599 xcb_get_property_reply_t
*r
;
2601 c
= xcb_get_property(conn
, 0, w
, a_state
, a_state
, 0L, 2L);
2602 r
= xcb_get_property_reply(conn
, c
, NULL
);
2605 if (r
->type
== a_state
&& r
->format
== 32 && r
->length
== 2)
2606 result
= *((uint32_t *)xcb_get_property_value(r
));
2610 DNPRINTF(SWM_D_MISC
, "getstate property: win 0x%x state %u\n", w
,
2616 version(struct swm_region
*r
, union arg
*args
)
2618 /* suppress unused warnings since vars are needed */
2622 bar_version
= !bar_version
;
2624 snprintf(bar_vertext
, sizeof bar_vertext
,
2625 "Version: %s Build: %s", SPECTRWM_VERSION
, buildstr
);
2627 strlcpy(bar_vertext
, "", sizeof bar_vertext
);
2634 client_msg(struct ws_win
*win
, xcb_atom_t a
, xcb_timestamp_t t
)
2636 xcb_client_message_event_t ev
;
2644 name
= get_atom_name(a
);
2645 DNPRINTF(SWM_D_EVENT
, "client_msg: window: 0x%x, atom: %s(%u), "
2647 win
->id
, name
, a
, t
);
2651 bzero(&ev
, sizeof ev
);
2652 ev
.response_type
= XCB_CLIENT_MESSAGE
;
2653 ev
.window
= win
->id
;
2656 ev
.data
.data32
[0] = a
;
2657 ev
.data
.data32
[1] = t
;
2659 xcb_send_event(conn
, 0, win
->id
,
2660 XCB_EVENT_MASK_NO_EVENT
, (const char *)&ev
);
2663 /* synthetic response to a ConfigureRequest when not making a change */
2665 config_win(struct ws_win
*win
, xcb_configure_request_event_t
*ev
)
2667 xcb_configure_notify_event_t ce
;
2672 /* send notification of unchanged state. */
2673 bzero(&ce
, sizeof(ce
));
2674 ce
.response_type
= XCB_CONFIGURE_NOTIFY
;
2677 ce
.width
= WIDTH(win
);
2678 ce
.height
= HEIGHT(win
);
2679 ce
.override_redirect
= 0;
2684 ce
.window
= win
->id
;
2685 ce
.border_width
= BORDER(win
);
2686 ce
.above_sibling
= XCB_WINDOW_NONE
;
2689 ce
.event
= ev
->window
;
2690 ce
.window
= ev
->window
;
2692 /* make response appear more WM_SIZE_HINTS-compliant */
2693 if (win
->sh
.flags
) {
2694 DNPRINTF(SWM_D_MISC
, "config_win: hints: window: 0x%x,"
2695 " sh.flags: %u, min: %d x %d, max: %d x %d, inc: "
2696 "%d x %d\n", win
->id
, win
->sh
.flags
, SH_MIN_W(win
),
2697 SH_MIN_H(win
), SH_MAX_W(win
), SH_MAX_H(win
),
2698 SH_INC_W(win
), SH_INC_H(win
));
2703 /* the hint may be set... to 0! */
2704 if (SH_MIN_W(win
) > 0 && ce
.width
< SH_MIN_W(win
))
2705 ce
.width
= SH_MIN_W(win
);
2706 if (SH_MIN_H(win
) > 0 && ce
.height
< SH_MIN_H(win
))
2707 ce
.height
= SH_MIN_H(win
);
2712 /* may also be advertized as 0 */
2713 if (SH_MAX_W(win
) > 0 && ce
.width
> SH_MAX_W(win
))
2714 ce
.width
= SH_MAX_W(win
);
2715 if (SH_MAX_H(win
) > 0 && ce
.height
> SH_MAX_H(win
))
2716 ce
.height
= SH_MAX_H(win
);
2719 /* resize increment. */
2721 if (SH_INC_W(win
) > 1 && ce
.width
> SH_INC_W(win
))
2722 ce
.width
-= (ce
.width
- SH_MIN_W(win
)) %
2724 if (SH_INC_H(win
) > 1 && ce
.height
> SH_INC_H(win
))
2725 ce
.height
-= (ce
.height
- SH_MIN_H(win
)) %
2729 /* adjust x and y for requested border_width. */
2730 ce
.x
+= BORDER(win
) - ev
->border_width
;
2731 ce
.y
+= BORDER(win
) - ev
->border_width
;
2732 ce
.border_width
= ev
->border_width
;
2733 ce
.above_sibling
= ev
->sibling
;
2736 DNPRINTF(SWM_D_MISC
, "config_win: ewmh: %s, window: 0x%x, (x,y) w x h: "
2737 "(%d,%d) %d x %d, border: %d\n", YESNO(ev
== NULL
), win
->id
, ce
.x
,
2738 ce
.y
, ce
.width
, ce
.height
, ce
.border_width
);
2740 xcb_send_event(conn
, 0, win
->id
, XCB_EVENT_MASK_STRUCTURE_NOTIFY
,
2745 count_win(struct workspace
*ws
, int count_transient
)
2750 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
2751 if (!count_transient
&& win
->floating
)
2753 if (!count_transient
&& win
->transient
)
2759 DNPRINTF(SWM_D_MISC
, "count_win: %d\n", count
);
2765 quit(struct swm_region
*r
, union arg
*args
)
2767 /* suppress unused warnings since vars are needed */
2771 DNPRINTF(SWM_D_MISC
, "quit\n");
2776 map_window(struct ws_win
*win
)
2778 uint32_t val
= XCB_STACK_MODE_ABOVE
;
2783 DNPRINTF(SWM_D_EVENT
, "map_window: win 0x%x, mapped: %s\n", win
->id
,
2784 YESNO(win
->mapped
));
2786 xcb_configure_window(conn
, win
->id
,
2787 XCB_CONFIG_WINDOW_STACK_MODE
, &val
);
2792 xcb_map_window(conn
, win
->id
);
2793 set_win_state(win
, XCB_ICCCM_WM_STATE_NORMAL
);
2798 unmap_window(struct ws_win
*win
)
2803 DNPRINTF(SWM_D_EVENT
, "unmap_window: win 0x%x, mapped: %s\n", win
->id
,
2804 YESNO(win
->mapped
));
2809 xcb_unmap_window(conn
, win
->id
);
2810 set_win_state(win
, XCB_ICCCM_WM_STATE_ICONIC
);
2818 int i
, j
, num_screens
;
2820 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2821 for (i
= 0; i
< num_screens
; i
++)
2822 for (j
= 0; j
< workspace_limit
; j
++)
2823 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2828 fake_keypress(struct ws_win
*win
, xcb_keysym_t keysym
, uint16_t modifiers
)
2830 xcb_key_press_event_t event
;
2831 xcb_keycode_t
*keycode
;
2836 keycode
= xcb_key_symbols_get_keycode(syms
, keysym
);
2838 DNPRINTF(SWM_D_MISC
, "fake_keypress: win 0x%x keycode %u\n",
2841 bzero(&event
, sizeof(event
));
2842 event
.event
= win
->id
;
2843 event
.root
= win
->s
->root
;
2844 event
.child
= XCB_WINDOW_NONE
;
2845 event
.time
= XCB_CURRENT_TIME
;
2846 event
.event_x
= X(win
);
2847 event
.event_y
= Y(win
);
2850 event
.same_screen
= 1;
2851 event
.detail
= *keycode
;
2852 event
.state
= modifiers
;
2854 event
.response_type
= XCB_KEY_PRESS
;
2855 xcb_send_event(conn
, 1, win
->id
,
2856 XCB_EVENT_MASK_KEY_PRESS
, (const char *)&event
);
2858 event
.response_type
= XCB_KEY_RELEASE
;
2859 xcb_send_event(conn
, 1, win
->id
,
2860 XCB_EVENT_MASK_KEY_RELEASE
, (const char *)&event
);
2866 restart(struct swm_region
*r
, union arg
*args
)
2868 /* suppress unused warning since var is needed */
2872 DNPRINTF(SWM_D_MISC
, "restart: %s\n", start_argv
[0]);
2876 execvp(start_argv
[0], start_argv
);
2877 warn("execvp failed");
2882 get_pointer_win(xcb_window_t root
)
2884 struct ws_win
*win
= NULL
;
2885 xcb_query_pointer_reply_t
*r
;
2887 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: root: 0x%x.\n", root
);
2889 r
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, root
), NULL
);
2891 win
= find_window(r
->child
);
2893 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: 0x%x.\n",
2896 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: none.\n");
2905 root_to_region(xcb_window_t root
, int check
)
2908 struct swm_region
*r
= NULL
;
2910 xcb_query_pointer_reply_t
*qpr
;
2911 xcb_get_input_focus_reply_t
*gifr
;
2913 DNPRINTF(SWM_D_MISC
, "root_to_region: window: 0x%x\n", root
);
2915 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2916 for (i
= 0; i
< num_screens
; i
++)
2917 if (screens
[i
].root
== root
)
2920 if (check
& SWM_CK_REGION
)
2921 r
= screens
[i
].r_focus
;
2923 if (r
== NULL
&& check
& SWM_CK_FOCUS
) {
2924 /* Try to find an actively focused window */
2925 gifr
= xcb_get_input_focus_reply(conn
,
2926 xcb_get_input_focus(conn
), NULL
);
2928 cfw
= find_window(gifr
->focus
);
2929 if (cfw
&& cfw
->ws
->r
)
2936 if (r
== NULL
&& check
& SWM_CK_POINTER
) {
2937 /* No region with an active focus; try to use pointer. */
2938 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
,
2939 screens
[i
].root
), NULL
);
2942 DNPRINTF(SWM_D_MISC
, "root_to_region: pointer: "
2943 "(%d,%d)\n", qpr
->root_x
, qpr
->root_y
);
2944 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2945 if (X(r
) <= qpr
->root_x
&&
2946 qpr
->root_x
< MAX_X(r
) &&
2947 Y(r
) <= qpr
->root_y
&&
2948 qpr
->root_y
< MAX_Y(r
))
2955 if (r
== NULL
&& check
& SWM_CK_FALLBACK
)
2956 r
= TAILQ_FIRST(&screens
[i
].rl
);
2962 find_unmanaged_window(xcb_window_t id
)
2965 int i
, j
, num_screens
;
2967 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2968 for (i
= 0; i
< num_screens
; i
++)
2969 for (j
= 0; j
< workspace_limit
; j
++)
2970 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].unmanagedlist
,
2978 find_window(xcb_window_t id
)
2981 int i
, j
, num_screens
;
2982 xcb_query_tree_reply_t
*r
;
2984 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2985 for (i
= 0; i
< num_screens
; i
++)
2986 for (j
= 0; j
< workspace_limit
; j
++)
2987 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2991 r
= xcb_query_tree_reply(conn
, xcb_query_tree(conn
, id
), NULL
);
2995 /* if we were looking for the parent return that window instead */
2996 if (r
->parent
== 0 || r
->root
== r
->parent
) {
3001 /* look for parent */
3002 for (i
= 0; i
< num_screens
; i
++)
3003 for (j
= 0; j
< workspace_limit
; j
++)
3004 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
3005 if (r
->parent
== win
->id
) {
3015 spawn(int ws_idx
, union arg
*args
, int close_fd
)
3020 DNPRINTF(SWM_D_MISC
, "spawn: %s\n", args
->argv
[0]);
3022 close(xcb_get_file_descriptor(conn
));
3024 setenv("LD_PRELOAD", SWM_LIB
, 1);
3026 if (asprintf(&ret
, "%d", ws_idx
) == -1) {
3027 warn("spawn: asprintf SWM_WS");
3030 setenv("_SWM_WS", ret
, 1);
3034 if (asprintf(&ret
, "%d", getpid()) == -1) {
3035 warn("spawn: asprintf _SWM_PID");
3038 setenv("_SWM_PID", ret
, 1);
3042 if (setsid() == -1) {
3043 warn("spawn: setsid");
3049 * close stdin and stdout to prevent interaction between apps
3050 * and the baraction script
3051 * leave stderr open to record errors
3053 if ((fd
= open(_PATH_DEVNULL
, O_RDWR
, 0)) == -1) {
3054 warn("spawn: open");
3057 dup2(fd
, STDIN_FILENO
);
3058 dup2(fd
, STDOUT_FILENO
);
3063 execvp(args
->argv
[0], args
->argv
);
3065 warn("spawn: execvp");
3070 kill_refs(struct ws_win
*win
)
3072 int i
, x
, num_screens
;
3073 struct swm_region
*r
;
3074 struct workspace
*ws
;
3079 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
3080 for (i
= 0; i
< num_screens
; i
++)
3081 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3082 for (x
= 0; x
< workspace_limit
; x
++) {
3084 if (win
== ws
->focus
)
3086 if (win
== ws
->focus_prev
)
3087 ws
->focus_prev
= NULL
;
3092 validate_win(struct ws_win
*testwin
)
3095 struct workspace
*ws
;
3096 struct swm_region
*r
;
3097 int i
, x
, num_screens
;
3099 if (testwin
== NULL
)
3102 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
3103 for (i
= 0; i
< num_screens
; i
++)
3104 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3105 for (x
= 0; x
< workspace_limit
; x
++) {
3107 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
3115 validate_ws(struct workspace
*testws
)
3117 struct swm_region
*r
;
3118 struct workspace
*ws
;
3119 int i
, x
, num_screens
;
3121 /* validate all ws */
3122 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
3123 for (i
= 0; i
< num_screens
; i
++)
3124 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3125 for (x
= 0; x
< workspace_limit
; x
++) {
3134 unfocus_win(struct ws_win
*win
)
3136 xcb_window_t none
= XCB_WINDOW_NONE
;
3138 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: window: 0x%x\n", WINID(win
));
3142 if (win
->ws
== NULL
) {
3143 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: NULL ws.\n");
3147 if (validate_ws(win
->ws
)) {
3148 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: invalid ws.\n");
3152 if (win
->ws
->r
== NULL
) {
3153 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: NULL region.\n");
3157 if (validate_win(win
)) {
3158 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: invalid win.\n");
3163 if (win
->ws
->focus
== win
) {
3164 win
->ws
->focus
= NULL
;
3165 win
->ws
->focus_prev
= win
;
3168 if (validate_win(win
->ws
->focus
)) {
3169 kill_refs(win
->ws
->focus
);
3170 win
->ws
->focus
= NULL
;
3173 if (validate_win(win
->ws
->focus_prev
)) {
3174 kill_refs(win
->ws
->focus_prev
);
3175 win
->ws
->focus_prev
= NULL
;
3178 xcb_change_window_attributes(conn
, win
->id
, XCB_CW_BORDER_PIXEL
,
3179 &win
->ws
->r
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
);
3181 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
3182 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1, &none
);
3184 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: done.\n");
3188 focus_win(struct ws_win
*win
)
3190 struct ws_win
*cfw
= NULL
, *parent
= NULL
, *w
;
3191 struct workspace
*ws
;
3192 xcb_get_input_focus_reply_t
*r
;
3194 DNPRINTF(SWM_D_FOCUS
, "focus_win: window: 0x%x\n", WINID(win
));
3199 if (win
->ws
== NULL
)
3207 if (validate_ws(ws
))
3210 if (validate_win(win
)) {
3215 r
= xcb_get_input_focus_reply(conn
, xcb_get_input_focus(conn
), NULL
);
3217 cfw
= find_window(r
->focus
);
3223 if (ws
->focus
!= win
) {
3224 if (ws
->focus
&& ws
->focus
!= cfw
)
3225 unfocus_win(ws
->focus
);
3229 /* If this window directs focus to a child window, then clear. */
3230 if (win
->focus_child
)
3231 win
->focus_child
= NULL
;
3233 /* If transient, adjust parent's focus child for focus_magic. */
3234 if (win
->transient
) {
3235 parent
= find_window(win
->transient
);
3236 if (parent
&& parent
->focus_child
!= win
)
3237 parent
->focus_child
= win
;
3240 if (cfw
!= win
&& ws
->r
!= NULL
) {
3241 /* Set input focus if no input hint, or indicated by hint. */
3242 if (!(win
->hints
.flags
& XCB_ICCCM_WM_HINT_INPUT
) ||
3243 (win
->hints
.flags
& XCB_ICCCM_WM_HINT_INPUT
&&
3245 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
,
3246 win
->id
, last_event_time
);
3248 /* Tell app it can adjust focus to a specific window. */
3249 if (win
->take_focus
) {
3250 /* java is special; always tell parent */
3251 if (win
->transient
&& win
->java
)
3252 client_msg(parent
, a_takefocus
,
3255 client_msg(win
, a_takefocus
, last_event_time
);
3258 xcb_change_window_attributes(conn
, win
->id
, XCB_CW_BORDER_PIXEL
,
3259 &ws
->r
->s
->c
[SWM_S_COLOR_FOCUS
].pixel
);
3261 if (ws
->cur_layout
->flags
& SWM_L_MAPONFOCUS
||
3263 /* If a parent exists, map it first. */
3267 /* Map siblings next. */
3268 TAILQ_FOREACH(w
, &ws
->winlist
, entry
)
3269 if (w
!= win
&& !w
->iconic
&&
3270 w
->transient
== parent
->id
)
3274 /* Map focused window. */
3277 /* Finally, map children of focus window. */
3278 TAILQ_FOREACH(w
, &ws
->winlist
, entry
)
3279 if (w
->transient
== win
->id
&& !w
->iconic
)
3285 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
3286 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1,
3293 DNPRINTF(SWM_D_FOCUS
, "focus_win: done.\n");
3296 /* If a child window should have focus instead, return it. */
3298 get_focus_magic(struct ws_win
*win
)
3300 struct ws_win
*parent
= NULL
;
3301 struct ws_win
*child
= NULL
;
3303 DNPRINTF(SWM_D_FOCUS
, "get_focus_magic: window: 0x%x\n", WINID(win
));
3307 if (win
->transient
) {
3308 parent
= find_window(win
->transient
);
3310 /* If parent prefers focus elsewhere, then try to do so. */
3311 if (parent
&& (child
= parent
->focus_child
)) {
3312 if (validate_win(child
) == 0 && child
->mapped
)
3315 parent
->focus_child
= NULL
;
3319 /* If this window prefers focus elsewhere, then try to do so. */
3320 if ((child
= win
->focus_child
)) {
3321 if (validate_win(child
) == 0 && child
->mapped
)
3324 win
->focus_child
= NULL
;
3331 event_drain(uint8_t rt
)
3333 xcb_generic_event_t
*evt
;
3335 /* ensure all pending requests have been processed before filtering. */
3337 while ((evt
= xcb_poll_for_event(conn
))) {
3338 if (XCB_EVENT_RESPONSE_TYPE(evt
) != rt
)
3346 set_region(struct swm_region
*r
)
3348 struct swm_region
*rf
;
3354 /* Unfocus old region bar. */
3359 xcb_change_window_attributes(conn
, rf
->bar
->id
,
3360 XCB_CW_BORDER_PIXEL
,
3361 &r
->s
->c
[SWM_S_COLOR_BAR_BORDER_UNFOCUS
].pixel
);
3364 /* Set region bar border to focus_color. */
3365 xcb_change_window_attributes(conn
, r
->bar
->id
,
3366 XCB_CW_BORDER_PIXEL
, &r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].pixel
);
3372 focus_region(struct swm_region
*r
)
3375 struct swm_region
*old_r
;
3380 old_r
= r
->s
->r_focus
;
3383 nfw
= get_region_focus(r
);
3387 /* New region is empty; need to manually unfocus win. */
3389 unfocus_win(old_r
->ws
->focus
);
3391 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
, r
->s
->root
,
3394 /* Clear bar since empty. */
3400 switchws(struct swm_region
*r
, union arg
*args
)
3402 int wsid
= args
->id
, unmap_old
= 0;
3403 struct swm_region
*this_r
, *other_r
;
3405 struct workspace
*new_ws
, *old_ws
;
3410 if (wsid
>= workspace_limit
)
3414 old_ws
= this_r
->ws
;
3415 new_ws
= &this_r
->s
->ws
[wsid
];
3417 DNPRINTF(SWM_D_WS
, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
3418 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), old_ws
->idx
, wsid
);
3420 if (new_ws
== NULL
|| old_ws
== NULL
)
3422 if (new_ws
== old_ws
)
3425 unfocus_win(old_ws
->focus
);
3427 other_r
= new_ws
->r
;
3428 if (other_r
== NULL
) {
3429 /* the other workspace is hidden, hide this one */
3433 /* the other ws is visible in another region, exchange them */
3434 other_r
->ws_prior
= new_ws
;
3435 other_r
->ws
= old_ws
;
3436 old_ws
->r
= other_r
;
3438 this_r
->ws_prior
= old_ws
;
3439 this_r
->ws
= new_ws
;
3442 /* Set focus_pending before stacking. */
3443 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
3444 new_ws
->focus_pending
= get_region_focus(new_ws
->r
);
3448 /* unmap old windows */
3450 TAILQ_FOREACH(win
, &old_ws
->winlist
, entry
)
3453 /* if workspaces were swapped, then don't wait to set focus */
3454 if (old_ws
->r
&& focus_mode
!= SWM_FOCUS_FOLLOW
) {
3455 if (new_ws
->focus_pending
) {
3456 focus_win(new_ws
->focus_pending
);
3458 /* Empty region, focus on root. */
3459 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
,
3460 new_ws
->r
->s
[new_ws
->r
->s
->idx
].root
,
3465 /* Clear bar if new ws is empty. */
3466 if (new_ws
->focus_pending
== NULL
)
3471 DNPRINTF(SWM_D_WS
, "switchws: done.\n");
3475 cyclews(struct swm_region
*r
, union arg
*args
)
3478 struct swm_screen
*s
= r
->s
;
3481 DNPRINTF(SWM_D_WS
, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3482 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
3487 case SWM_ARG_ID_CYCLEWS_UP_ALL
:
3490 case SWM_ARG_ID_CYCLEWS_UP
:
3491 if (a
.id
< workspace_limit
- 1)
3496 case SWM_ARG_ID_CYCLEWS_DOWN_ALL
:
3499 case SWM_ARG_ID_CYCLEWS_DOWN
:
3503 a
.id
= workspace_limit
- 1;
3510 (!cycle_empty
&& TAILQ_EMPTY(&s
->ws
[a
.id
].winlist
)))
3512 if (!cycle_visible
&& s
->ws
[a
.id
].r
!= NULL
)
3516 } while (a
.id
!= r
->ws
->idx
);
3520 priorws(struct swm_region
*r
, union arg
*args
)
3526 DNPRINTF(SWM_D_WS
, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3527 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
3529 if (r
->ws_prior
== NULL
)
3532 a
.id
= r
->ws_prior
->idx
;
3537 focusrg(struct swm_region
*r
, union arg
*args
)
3539 int ridx
= args
->id
, i
, num_screens
;
3540 struct swm_region
*rr
= NULL
;
3542 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
3543 /* do nothing if we don't have more than one screen */
3544 if (!(num_screens
> 1 || outputs
> 1))
3547 DNPRINTF(SWM_D_FOCUS
, "focusrg: id: %d\n", ridx
);
3549 rr
= TAILQ_FIRST(&r
->s
->rl
);
3550 for (i
= 0; i
< ridx
; ++i
)
3551 rr
= TAILQ_NEXT(rr
, entry
);
3561 cyclerg(struct swm_region
*r
, union arg
*args
)
3563 struct swm_region
*rr
= NULL
;
3566 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
3567 /* do nothing if we don't have more than one screen */
3568 if (!(num_screens
> 1 || outputs
> 1))
3573 case SWM_ARG_ID_CYCLERG_UP
:
3574 rr
= TAILQ_NEXT(r
, entry
);
3576 rr
= TAILQ_FIRST(&screens
[i
].rl
);
3578 case SWM_ARG_ID_CYCLERG_DOWN
:
3579 rr
= TAILQ_PREV(r
, swm_region_list
, entry
);
3581 rr
= TAILQ_LAST(&screens
[i
].rl
, swm_region_list
);
3594 sort_windows(struct ws_win_list
*wl
)
3596 struct ws_win
*win
, *parent
, *nxt
;
3601 for (win
= TAILQ_FIRST(wl
); win
!= TAILQ_END(wl
); win
= nxt
) {
3602 nxt
= TAILQ_NEXT(win
, entry
);
3603 if (win
->transient
) {
3604 parent
= find_window(win
->transient
);
3605 if (parent
== NULL
) {
3606 warnx("not possible bug");
3609 TAILQ_REMOVE(wl
, win
, entry
);
3610 TAILQ_INSERT_AFTER(wl
, parent
, win
, entry
);
3617 swapwin(struct swm_region
*r
, union arg
*args
)
3619 struct ws_win
*target
, *source
;
3620 struct ws_win
*cur_focus
;
3621 struct ws_win_list
*wl
;
3623 DNPRINTF(SWM_D_WS
, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3624 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
3626 cur_focus
= r
->ws
->focus
;
3627 if (cur_focus
== NULL
)
3631 wl
= &source
->ws
->winlist
;
3634 case SWM_ARG_ID_SWAPPREV
:
3635 if (source
->transient
)
3636 source
= find_window(source
->transient
);
3637 target
= TAILQ_PREV(source
, ws_win_list
, entry
);
3638 if (target
&& target
->transient
)
3639 target
= find_window(target
->transient
);
3640 TAILQ_REMOVE(wl
, source
, entry
);
3642 TAILQ_INSERT_TAIL(wl
, source
, entry
);
3644 TAILQ_INSERT_BEFORE(target
, source
, entry
);
3646 case SWM_ARG_ID_SWAPNEXT
:
3647 target
= TAILQ_NEXT(source
, entry
);
3648 /* move the parent and let the sort handle the move */
3649 if (source
->transient
)
3650 source
= find_window(source
->transient
);
3651 TAILQ_REMOVE(wl
, source
, entry
);
3653 TAILQ_INSERT_HEAD(wl
, source
, entry
);
3655 TAILQ_INSERT_AFTER(wl
, target
, source
, entry
);
3657 case SWM_ARG_ID_SWAPMAIN
:
3658 target
= TAILQ_FIRST(wl
);
3659 if (target
== source
) {
3660 if (source
->ws
->focus_prev
!= NULL
&&
3661 source
->ws
->focus_prev
!= target
)
3662 source
= source
->ws
->focus_prev
;
3666 if (target
== NULL
|| source
== NULL
)
3668 source
->ws
->focus_prev
= target
;
3669 TAILQ_REMOVE(wl
, target
, entry
);
3670 TAILQ_INSERT_BEFORE(source
, target
, entry
);
3671 TAILQ_REMOVE(wl
, source
, entry
);
3672 TAILQ_INSERT_HEAD(wl
, source
, entry
);
3674 case SWM_ARG_ID_MOVELAST
:
3675 TAILQ_REMOVE(wl
, source
, entry
);
3676 TAILQ_INSERT_TAIL(wl
, source
, entry
);
3679 DNPRINTF(SWM_D_MOVE
, "swapwin: invalid id: %d\n", args
->id
);
3691 get_focus_prev(struct ws_win
*win
)
3693 struct ws_win
*winfocus
= NULL
;
3694 struct ws_win
*cur_focus
= NULL
;
3695 struct ws_win_list
*wl
= NULL
;
3696 struct workspace
*ws
= NULL
;
3698 if (!(win
&& win
->ws
))
3703 cur_focus
= ws
->focus
;
3705 DNPRINTF(SWM_D_FOCUS
, "get_focus_prev: window: 0x%x, cur_focus: 0x%x\n",
3706 WINID(win
), WINID(cur_focus
));
3708 /* pickle, just focus on whatever */
3709 if (cur_focus
== NULL
) {
3710 /* use prev_focus if valid */
3711 if (ws
->focus_prev
&& ws
->focus_prev
!= cur_focus
&&
3712 find_window(WINID(ws
->focus_prev
)))
3713 winfocus
= ws
->focus_prev
;
3717 /* if transient focus on parent */
3718 if (cur_focus
->transient
) {
3719 winfocus
= find_window(cur_focus
->transient
);
3723 /* if in max_stack try harder */
3724 if ((win
->quirks
& SWM_Q_FOCUSPREV
) ||
3725 (ws
->cur_layout
->flags
& SWM_L_FOCUSPREV
)) {
3726 if (cur_focus
!= ws
->focus_prev
)
3727 winfocus
= ws
->focus_prev
;
3729 winfocus
= TAILQ_PREV(win
, ws_win_list
, entry
);
3734 DNPRINTF(SWM_D_FOCUS
, "get_focus_prev: focus_close: %d\n", focus_close
);
3736 if (winfocus
== NULL
|| winfocus
== win
) {
3737 switch (focus_close
) {
3738 case SWM_STACK_BOTTOM
:
3739 TAILQ_FOREACH(winfocus
, wl
, entry
)
3740 if (!winfocus
->iconic
&& winfocus
!= cur_focus
)
3744 TAILQ_FOREACH_REVERSE(winfocus
, wl
, ws_win_list
, entry
)
3745 if (!winfocus
->iconic
&& winfocus
!= cur_focus
)
3748 case SWM_STACK_ABOVE
:
3749 winfocus
= TAILQ_NEXT(cur_focus
, entry
);
3750 while (winfocus
&& winfocus
->iconic
)
3751 winfocus
= TAILQ_NEXT(winfocus
, entry
);
3753 if (winfocus
== NULL
) {
3754 if (focus_close_wrap
) {
3755 TAILQ_FOREACH(winfocus
, wl
, entry
)
3756 if (!winfocus
->iconic
&&
3757 winfocus
!= cur_focus
)
3760 TAILQ_FOREACH_REVERSE(winfocus
, wl
,
3762 if (!winfocus
->iconic
&&
3763 winfocus
!= cur_focus
)
3768 case SWM_STACK_BELOW
:
3769 winfocus
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
3770 while (winfocus
&& winfocus
->iconic
)
3771 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
,
3774 if (winfocus
== NULL
) {
3775 if (focus_close_wrap
) {
3776 TAILQ_FOREACH_REVERSE(winfocus
, wl
,
3778 if (!winfocus
->iconic
&&
3779 winfocus
!= cur_focus
)
3782 TAILQ_FOREACH(winfocus
, wl
, entry
)
3783 if (!winfocus
->iconic
&&
3784 winfocus
!= cur_focus
)
3792 if (winfocus
== NULL
||
3793 (winfocus
&& (winfocus
->iconic
|| winfocus
== cur_focus
))) {
3794 if (focus_default
== SWM_STACK_TOP
) {
3795 TAILQ_FOREACH_REVERSE(winfocus
, wl
, ws_win_list
, entry
)
3796 if (!winfocus
->iconic
&& winfocus
!= cur_focus
)
3799 TAILQ_FOREACH(winfocus
, wl
, entry
)
3800 if (!winfocus
->iconic
&& winfocus
!= cur_focus
)
3807 return get_focus_magic(winfocus
);
3811 get_region_focus(struct swm_region
*r
)
3813 struct ws_win
*winfocus
= NULL
;
3818 if (r
->ws
->focus
&& !r
->ws
->focus
->iconic
)
3819 winfocus
= r
->ws
->focus
;
3820 else if (r
->ws
->focus_prev
&& !r
->ws
->focus_prev
->iconic
)
3821 winfocus
= r
->ws
->focus_prev
;
3823 TAILQ_FOREACH(winfocus
, &r
->ws
->winlist
, entry
)
3824 if (!winfocus
->iconic
)
3827 return get_focus_magic(winfocus
);
3831 focus(struct swm_region
*r
, union arg
*args
)
3833 struct ws_win
*head
, *cur_focus
= NULL
, *winfocus
= NULL
;
3834 struct ws_win_list
*wl
= NULL
;
3835 struct workspace
*ws
= NULL
;
3841 DNPRINTF(SWM_D_FOCUS
, "focus: id: %d\n", args
->id
);
3843 if ((cur_focus
= r
->ws
->focus
) == NULL
)
3847 if (TAILQ_EMPTY(wl
))
3849 /* make sure there is at least one uniconified window */
3851 TAILQ_FOREACH(winfocus
, wl
, entry
)
3852 if (!winfocus
->iconic
) {
3860 case SWM_ARG_ID_FOCUSPREV
:
3861 head
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
3863 head
= TAILQ_LAST(wl
, ws_win_list
);
3865 if (WINID(winfocus
) == cur_focus
->transient
) {
3866 head
= TAILQ_PREV(winfocus
, ws_win_list
, entry
);
3868 head
= TAILQ_LAST(wl
, ws_win_list
);
3873 if (winfocus
&& winfocus
->iconic
) {
3874 while (winfocus
!= cur_focus
) {
3875 if (winfocus
== NULL
)
3876 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
3877 if (!winfocus
->iconic
)
3879 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
,
3885 case SWM_ARG_ID_FOCUSNEXT
:
3886 head
= TAILQ_NEXT(cur_focus
, entry
);
3888 head
= TAILQ_FIRST(wl
);
3892 if (winfocus
&& winfocus
->iconic
) {
3893 while (winfocus
!= cur_focus
) {
3894 if (winfocus
== NULL
)
3895 winfocus
= TAILQ_FIRST(wl
);
3896 if (!winfocus
->iconic
)
3898 winfocus
= TAILQ_NEXT(winfocus
, entry
);
3903 case SWM_ARG_ID_FOCUSMAIN
:
3904 winfocus
= TAILQ_FIRST(wl
);
3905 if (winfocus
== cur_focus
)
3906 winfocus
= cur_focus
->ws
->focus_prev
;
3913 focus_win(get_focus_magic(winfocus
));
3919 cycle_layout(struct swm_region
*r
, union arg
*args
)
3921 struct workspace
*ws
= r
->ws
;
3923 /* suppress unused warning since var is needed */
3926 DNPRINTF(SWM_D_EVENT
, "cycle_layout: workspace: %d\n", ws
->idx
);
3929 if (ws
->cur_layout
->l_stack
== NULL
)
3930 ws
->cur_layout
= &layouts
[0];
3935 focus_win(get_region_focus(r
));
3941 stack_config(struct swm_region
*r
, union arg
*args
)
3943 struct workspace
*ws
= r
->ws
;
3945 DNPRINTF(SWM_D_STACK
, "stack_config: id: %d workspace: %d\n",
3948 if (ws
->cur_layout
->l_config
!= NULL
)
3949 ws
->cur_layout
->l_config(ws
, args
->id
);
3951 if (args
->id
!= SWM_ARG_ID_STACKINIT
)
3960 struct swm_geometry g
;
3961 struct swm_region
*r
;
3967 DNPRINTF(SWM_D_STACK
, "stack: begin\n");
3969 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
3970 for (i
= 0; i
< num_screens
; i
++) {
3974 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
3975 DNPRINTF(SWM_D_STACK
, "stack: workspace: %d "
3976 "(screen: %d, region: %d)\n", r
->ws
->idx
, i
, j
++);
3978 /* Adjust stack area for region bar and padding. */
3980 g
.x
+= region_padding
;
3981 g
.y
+= region_padding
;
3982 g
.w
-= 2 * border_width
+ 2 * region_padding
;
3983 g
.h
-= 2 * border_width
+ 2 * region_padding
;
3984 if (bar_enabled
&& r
->ws
->bar_enabled
) {
3989 r
->ws
->cur_layout
->l_stack(r
->ws
, &g
);
3990 r
->ws
->cur_layout
->l_string(r
->ws
);
3991 /* save r so we can track region changes */
3998 DNPRINTF(SWM_D_STACK
, "stack: end\n");
4002 store_float_geom(struct ws_win
*win
, struct swm_region
*r
)
4004 if (win
== NULL
|| r
== NULL
)
4007 /* retain window geom and region geom */
4008 win
->g_float
= win
->g
;
4009 win
->g_float
.x
-= X(r
);
4010 win
->g_float
.y
-= Y(r
);
4011 win
->g_floatvalid
= 1;
4012 DNPRINTF(SWM_D_MISC
, "store_float_geom: window: 0x%x, g: (%d,%d)"
4013 " %d x %d, g_float: (%d,%d) %d x %d\n", win
->id
, X(win
), Y(win
),
4014 WIDTH(win
), HEIGHT(win
), win
->g_float
.x
, win
->g_float
.y
,
4015 win
->g_float
.w
, win
->g_float
.h
);
4019 load_float_geom(struct ws_win
*win
, struct swm_region
*r
)
4021 if (win
== NULL
|| r
== NULL
)
4024 if (win
->g_floatvalid
) {
4025 win
->g
= win
->g_float
;
4028 DNPRINTF(SWM_D_MISC
, "load_float_geom: window: 0x%x, g: (%d,%d)"
4029 "%d x %d\n", win
->id
, X(win
), Y(win
), WIDTH(win
),
4032 DNPRINTF(SWM_D_MISC
, "load_float_geom: window: 0x%x, g_float "
4033 "is not set.\n", win
->id
);
4038 stack_floater(struct ws_win
*win
, struct swm_region
*r
)
4043 DNPRINTF(SWM_D_MISC
, "stack_floater: window: 0x%x\n", win
->id
);
4046 * to allow windows to change their size (e.g. mplayer fs) only retrieve
4047 * geom on ws switches or return from max mode
4049 if (win
->floatmaxed
|| (r
!= r
->ws
->old_r
&&
4050 !(win
->ewmh_flags
& EWMH_F_FULLSCREEN
))) {
4051 /* update geometry for the new region */
4052 load_float_geom(win
, r
);
4055 win
->floatmaxed
= 0;
4058 * if set to fullscreen mode, configure window to maximum size.
4060 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
4061 if (!win
->g_floatvalid
)
4062 store_float_geom(win
, win
->ws
->r
);
4068 * remove border on fullscreen floater when in fullscreen mode or when
4069 * the quirk is present.
4071 if ((win
->ewmh_flags
& EWMH_F_FULLSCREEN
) ||
4072 ((win
->quirks
& SWM_Q_FULLSCREEN
) &&
4073 (WIDTH(win
) >= WIDTH(r
)) && (HEIGHT(win
) >= HEIGHT(r
)))) {
4074 if (win
->bordered
) {
4076 X(win
) += border_width
;
4077 Y(win
) += border_width
;
4079 } else if (!win
->bordered
) {
4081 X(win
) -= border_width
;
4082 Y(win
) -= border_width
;
4085 if (win
->transient
&& (win
->quirks
& SWM_Q_TRANSSZ
)) {
4086 WIDTH(win
) = (double)WIDTH(r
) * dialog_ratio
;
4087 HEIGHT(win
) = (double)HEIGHT(r
) * dialog_ratio
;
4090 if (!win
->manual
&& !(win
->ewmh_flags
& EWMH_F_FULLSCREEN
) &&
4091 !(win
->quirks
& SWM_Q_ANYWHERE
)) {
4093 * floaters and transients are auto-centred unless moved,
4094 * resized or ANYWHERE quirk is set.
4096 X(win
) = X(r
) + (WIDTH(r
) - WIDTH(win
)) / 2 - BORDER(win
);
4097 Y(win
) = Y(r
) + (HEIGHT(r
) - HEIGHT(win
)) / 2 - BORDER(win
);
4099 store_float_geom(win
, r
);
4102 /* keep window within region bounds */
4103 constrain_window(win
, r
, 0);
4109 * Send keystrokes to terminal to decrease/increase the font size as the
4110 * window size changes.
4113 adjust_font(struct ws_win
*win
)
4115 if (!(win
->quirks
& SWM_Q_XTERM_FONTADJ
) ||
4116 win
->floating
|| win
->transient
)
4119 if (win
->sh
.width_inc
&& win
->last_inc
!= win
->sh
.width_inc
&&
4120 WIDTH(win
) / win
->sh
.width_inc
< term_width
&&
4121 win
->font_steps
< SWM_MAX_FONT_STEPS
) {
4122 win
->font_size_boundary
[win
->font_steps
] =
4123 (win
->sh
.width_inc
* term_width
) + win
->sh
.base_width
;
4126 win
->last_inc
= win
->sh
.width_inc
;
4127 fake_keypress(win
, XK_KP_Subtract
, XCB_MOD_MASK_SHIFT
);
4128 } else if (win
->font_steps
&& win
->last_inc
!= win
->sh
.width_inc
&&
4129 WIDTH(win
) > win
->font_size_boundary
[win
->font_steps
- 1]) {
4132 win
->last_inc
= win
->sh
.width_inc
;
4133 fake_keypress(win
, XK_KP_Add
, XCB_MOD_MASK_SHIFT
);
4137 #define SWAPXY(g) do { \
4139 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
4140 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
4143 stack_master(struct workspace
*ws
, struct swm_geometry
*g
, int rot
, int flip
)
4145 struct swm_geometry win_g
, r_g
= *g
;
4146 struct ws_win
*win
, *fs_win
= NULL
;
4147 int i
, j
, s
, stacks
;
4148 int w_inc
= 1, h_inc
, w_base
= 1, h_base
;
4149 int hrh
, extra
= 0, h_slice
, last_h
= 0;
4150 int split
, colno
, winno
, mwin
, msize
, mscale
;
4151 int remain
, missing
, v_slice
, reconfigure
= 0;
4154 DNPRINTF(SWM_D_STACK
, "stack_master: workspace: %d, rot: %s, "
4155 "flip: %s\n", ws
->idx
, YESNO(rot
), YESNO(flip
));
4157 winno
= count_win(ws
, 0);
4158 if (winno
== 0 && count_win(ws
, 1) == 0)
4161 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
4162 if (!win
->transient
&& !win
->floating
&& !win
->iconic
)
4169 w_inc
= win
->sh
.width_inc
;
4170 w_base
= win
->sh
.base_width
;
4171 mwin
= ws
->l_state
.horizontal_mwin
;
4172 mscale
= ws
->l_state
.horizontal_msize
;
4173 stacks
= ws
->l_state
.horizontal_stacks
;
4176 w_inc
= win
->sh
.height_inc
;
4177 w_base
= win
->sh
.base_height
;
4178 mwin
= ws
->l_state
.vertical_mwin
;
4179 mscale
= ws
->l_state
.vertical_msize
;
4180 stacks
= ws
->l_state
.vertical_stacks
;
4184 if (stacks
> winno
- mwin
)
4185 stacks
= winno
- mwin
;
4189 h_slice
= r_g
.h
/ SWM_H_SLICE
;
4190 if (mwin
&& winno
> mwin
) {
4191 v_slice
= r_g
.w
/ SWM_V_SLICE
;
4195 win_g
.w
= v_slice
* mscale
;
4197 if (w_inc
> 1 && w_inc
< v_slice
) {
4198 /* adjust for window's requested size increment */
4199 remain
= (win_g
.w
- w_base
) % w_inc
;
4205 win_g
.x
+= r_g
.w
- msize
;
4208 colno
= split
= winno
/ stacks
;
4209 win_g
.w
= ((r_g
.w
- (stacks
* 2 * border_width
) +
4210 2 * border_width
) / stacks
);
4212 hrh
= r_g
.h
/ colno
;
4213 extra
= r_g
.h
- (colno
* hrh
);
4214 win_g
.h
= hrh
- 2 * border_width
;
4216 /* stack all the tiled windows */
4217 i
= j
= 0, s
= stacks
;
4218 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
4219 if (win
->transient
|| win
->floating
|| win
->iconic
)
4222 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
4227 if (split
&& i
== split
) {
4228 colno
= (winno
- mwin
) / stacks
;
4229 if (s
<= (winno
- mwin
) % stacks
)
4231 split
= split
+ colno
;
4232 hrh
= (r_g
.h
/ colno
);
4233 extra
= r_g
.h
- (colno
* hrh
);
4237 win_g
.x
+= win_g
.w
+ 2 * border_width
+
4239 win_g
.w
= (r_g
.w
- msize
-
4240 (stacks
* (2 * border_width
+ tile_gap
))) / stacks
;
4242 win_g
.w
+= (r_g
.w
- msize
-
4243 (stacks
* (2 * border_width
+ tile_gap
))) %
4249 win_g
.h
= hrh
- 2 * border_width
- tile_gap
;
4252 h_inc
= win
->sh
.width_inc
;
4253 h_base
= win
->sh
.base_width
;
4255 h_inc
= win
->sh
.height_inc
;
4256 h_base
= win
->sh
.base_height
;
4259 if (j
== colno
- 1) {
4260 win_g
.h
= hrh
+ extra
;
4261 } else if (h_inc
> 1 && h_inc
< h_slice
) {
4262 /* adjust for window's requested size increment */
4263 remain
= (win_g
.h
- h_base
) % h_inc
;
4264 missing
= h_inc
- remain
;
4266 if (missing
<= extra
|| j
== 0) {
4278 win_g
.y
+= last_h
+ 2 * border_width
+ tile_gap
;
4280 if (disable_border
&& !(bar_enabled
&& ws
->bar_enabled
) &&
4283 win_g
.w
+= 2 * border_width
;
4284 win_g
.h
+= 2 * border_width
;
4290 if (X(win
) != win_g
.y
|| Y(win
) != win_g
.x
||
4291 WIDTH(win
) != win_g
.h
|| HEIGHT(win
) != win_g
.w
) {
4295 WIDTH(win
) = win_g
.h
;
4296 HEIGHT(win
) = win_g
.w
;
4299 if (X(win
) != win_g
.x
|| Y(win
) != win_g
.y
||
4300 WIDTH(win
) != win_g
.w
|| HEIGHT(win
) != win_g
.h
) {
4304 WIDTH(win
) = win_g
.w
;
4305 HEIGHT(win
) = win_g
.h
;
4309 if (bordered
!= win
->bordered
) {
4311 win
->bordered
= bordered
;
4327 /* now, stack all the floaters and transients */
4328 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
4329 if (!win
->transient
&& !win
->floating
)
4333 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
4338 stack_floater(win
, ws
->r
);
4342 /* Make sure fs_win is stacked last so it's on top. */
4344 stack_floater(fs_win
, ws
->r
);
4350 vertical_config(struct workspace
*ws
, int id
)
4352 DNPRINTF(SWM_D_STACK
, "vertical_config: id: %d, workspace: %d\n",
4356 case SWM_ARG_ID_STACKRESET
:
4357 case SWM_ARG_ID_STACKINIT
:
4358 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ 2;
4359 ws
->l_state
.vertical_mwin
= 1;
4360 ws
->l_state
.vertical_stacks
= 1;
4362 case SWM_ARG_ID_MASTERSHRINK
:
4363 if (ws
->l_state
.vertical_msize
> 1)
4364 ws
->l_state
.vertical_msize
--;
4366 case SWM_ARG_ID_MASTERGROW
:
4367 if (ws
->l_state
.vertical_msize
< SWM_V_SLICE
- 1)
4368 ws
->l_state
.vertical_msize
++;
4370 case SWM_ARG_ID_MASTERADD
:
4371 ws
->l_state
.vertical_mwin
++;
4373 case SWM_ARG_ID_MASTERDEL
:
4374 if (ws
->l_state
.vertical_mwin
> 0)
4375 ws
->l_state
.vertical_mwin
--;
4377 case SWM_ARG_ID_STACKINC
:
4378 ws
->l_state
.vertical_stacks
++;
4380 case SWM_ARG_ID_STACKDEC
:
4381 if (ws
->l_state
.vertical_stacks
> 1)
4382 ws
->l_state
.vertical_stacks
--;
4384 case SWM_ARG_ID_FLIPLAYOUT
:
4385 ws
->l_state
.vertical_flip
= !ws
->l_state
.vertical_flip
;
4393 vertical_stack(struct workspace
*ws
, struct swm_geometry
*g
)
4395 DNPRINTF(SWM_D_STACK
, "vertical_stack: workspace: %d\n", ws
->idx
);
4397 stack_master(ws
, g
, 0, ws
->l_state
.vertical_flip
);
4401 horizontal_config(struct workspace
*ws
, int id
)
4403 DNPRINTF(SWM_D_STACK
, "horizontal_config: workspace: %d\n", ws
->idx
);
4406 case SWM_ARG_ID_STACKRESET
:
4407 case SWM_ARG_ID_STACKINIT
:
4408 ws
->l_state
.horizontal_mwin
= 1;
4409 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ 2;
4410 ws
->l_state
.horizontal_stacks
= 1;
4412 case SWM_ARG_ID_MASTERSHRINK
:
4413 if (ws
->l_state
.horizontal_msize
> 1)
4414 ws
->l_state
.horizontal_msize
--;
4416 case SWM_ARG_ID_MASTERGROW
:
4417 if (ws
->l_state
.horizontal_msize
< SWM_H_SLICE
- 1)
4418 ws
->l_state
.horizontal_msize
++;
4420 case SWM_ARG_ID_MASTERADD
:
4421 ws
->l_state
.horizontal_mwin
++;
4423 case SWM_ARG_ID_MASTERDEL
:
4424 if (ws
->l_state
.horizontal_mwin
> 0)
4425 ws
->l_state
.horizontal_mwin
--;
4427 case SWM_ARG_ID_STACKINC
:
4428 ws
->l_state
.horizontal_stacks
++;
4430 case SWM_ARG_ID_STACKDEC
:
4431 if (ws
->l_state
.horizontal_stacks
> 1)
4432 ws
->l_state
.horizontal_stacks
--;
4434 case SWM_ARG_ID_FLIPLAYOUT
:
4435 ws
->l_state
.horizontal_flip
= !ws
->l_state
.horizontal_flip
;
4443 horizontal_stack(struct workspace
*ws
, struct swm_geometry
*g
)
4445 DNPRINTF(SWM_D_STACK
, "horizontal_stack: workspace: %d\n", ws
->idx
);
4447 stack_master(ws
, g
, 1, ws
->l_state
.horizontal_flip
);
4450 /* fullscreen view */
4452 max_stack(struct workspace
*ws
, struct swm_geometry
*g
)
4454 struct swm_geometry gg
= *g
;
4455 struct ws_win
*w
, *win
= NULL
, *parent
= NULL
;
4458 DNPRINTF(SWM_D_STACK
, "max_stack: workspace: %d\n", ws
->idx
);
4463 winno
= count_win(ws
, 0);
4464 if (winno
== 0 && count_win(ws
, 1) == 0)
4467 /* Figure out which top level window should be visible. */
4468 if (ws
->focus_pending
)
4469 win
= ws
->focus_pending
;
4473 win
= TAILQ_FIRST(&ws
->winlist
);
4476 parent
= find_window(win
->transient
);
4478 DNPRINTF(SWM_D_STACK
, "max_stack: win: 0x%x\n", win
->id
);
4480 /* maximize all top level windows */
4481 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
4482 if (w
->transient
|| w
->iconic
)
4485 if (!w
->mapped
&& w
!= win
)
4488 if (w
->floating
&& !w
->floatmaxed
) {
4490 * retain geometry for retrieval on exit from
4493 store_float_geom(w
, ws
->r
);
4497 /* only reconfigure if necessary */
4498 if (X(w
) != gg
.x
|| Y(w
) != gg
.y
|| WIDTH(w
) != gg
.w
||
4499 HEIGHT(w
) != gg
.h
) {
4501 if (bar_enabled
&& ws
->bar_enabled
){
4505 WIDTH(w
) += 2 * border_width
;
4506 HEIGHT(w
) += 2 * border_width
;
4513 /* If a parent exists, map/raise it first. */
4517 /* Map siblings next. */
4518 TAILQ_FOREACH(w
, &ws
->winlist
, entry
)
4519 if (w
!= win
&& !w
->iconic
&&
4520 w
->transient
== parent
->id
) {
4521 stack_floater(w
, ws
->r
);
4526 /* Map/raise focused window. */
4529 /* Finally, map/raise children of focus window. */
4530 TAILQ_FOREACH(w
, &ws
->winlist
, entry
)
4531 if (w
->transient
== win
->id
&& !w
->iconic
) {
4532 stack_floater(w
, ws
->r
);
4538 send_to_rg(struct swm_region
*r
, union arg
*args
)
4540 int ridx
= args
->id
, i
, num_screens
;
4541 struct swm_region
*rr
= NULL
;
4544 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
4545 /* do nothing if we don't have more than one screen */
4546 if (!(num_screens
> 1 || outputs
> 1))
4549 DNPRINTF(SWM_D_FOCUS
, "send_to_rg: id: %d\n", ridx
);
4551 rr
= TAILQ_FIRST(&r
->s
->rl
);
4552 for (i
= 0; i
< ridx
; ++i
)
4553 rr
= TAILQ_NEXT(rr
, entry
);
4564 send_to_ws(struct swm_region
*r
, union arg
*args
)
4566 int wsid
= args
->id
;
4567 struct ws_win
*win
= NULL
, *parent
;
4568 struct workspace
*ws
, *nws
, *pws
;
4569 char ws_idx_str
[SWM_PROPLEN
];
4571 if (wsid
>= workspace_limit
)
4574 if (r
&& r
->ws
&& r
->ws
->focus
)
4579 if (win
->ws
->idx
== wsid
)
4582 DNPRINTF(SWM_D_MOVE
, "send_to_ws: win 0x%x, ws %d -> %d\n", win
->id
,
4583 win
->ws
->idx
, wsid
);
4586 nws
= &win
->s
->ws
[wsid
];
4588 /* Update the window's workspace property: _SWM_WS */
4589 if (snprintf(ws_idx_str
, SWM_PROPLEN
, "%d", nws
->idx
) < SWM_PROPLEN
) {
4590 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
4591 ws
->focus_pending
= get_focus_prev(win
);
4593 /* Move the parent if this is a transient window. */
4594 if (win
->transient
) {
4595 parent
= find_window(win
->transient
);
4598 /* Set new focus in parent's ws if needed. */
4599 if (pws
->focus
== parent
) {
4600 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
4601 pws
->focus_pending
=
4602 get_focus_prev(parent
);
4604 unfocus_win(parent
);
4606 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
4607 pws
->focus
= pws
->focus_pending
;
4609 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
4610 pws
->focus_pending
= NULL
;
4613 /* Don't unmap parent if new ws is visible */
4615 unmap_window(parent
);
4618 TAILQ_REMOVE(&ws
->winlist
, parent
, entry
);
4619 TAILQ_INSERT_TAIL(&nws
->winlist
, parent
, entry
);
4622 DNPRINTF(SWM_D_PROP
, "send_to_ws: set "
4623 "property: _SWM_WS: %s\n", ws_idx_str
);
4624 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
4625 parent
->id
, a_swm_ws
, XCB_ATOM_STRING
, 8,
4626 strlen(ws_idx_str
), ws_idx_str
);
4632 /* Don't unmap if new ws is visible */
4637 TAILQ_REMOVE(&ws
->winlist
, win
, entry
);
4638 TAILQ_INSERT_TAIL(&nws
->winlist
, win
, entry
);
4641 /* Set focus on new ws. */
4642 unfocus_win(nws
->focus
);
4645 DNPRINTF(SWM_D_PROP
, "send_to_ws: set property: _SWM_WS: %s\n",
4647 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
4648 a_swm_ws
, XCB_ATOM_STRING
, 8, strlen(ws_idx_str
),
4651 /* Restack and set new focus. */
4654 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
4655 focus_win(ws
->focus_pending
);
4656 ws
->focus_pending
= NULL
;
4662 DNPRINTF(SWM_D_MOVE
, "send_to_ws: done.\n");
4666 pressbutton(struct swm_region
*r
, union arg
*args
)
4668 /* suppress unused warning since var is needed */
4671 xcb_test_fake_input(conn
, XCB_BUTTON_PRESS
, args
->id
,
4672 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
4673 xcb_test_fake_input(conn
, XCB_BUTTON_RELEASE
, args
->id
,
4674 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
4678 raise_toggle(struct swm_region
*r
, union arg
*args
)
4680 /* suppress unused warning since var is needed */
4683 if (r
== NULL
|| r
->ws
== NULL
)
4686 r
->ws
->always_raise
= !r
->ws
->always_raise
;
4688 /* bring floaters back to top */
4689 if (!r
->ws
->always_raise
)
4696 iconify(struct swm_region
*r
, union arg
*args
)
4698 /* suppress unused warning since var is needed */
4701 if (r
->ws
->focus
== NULL
)
4704 set_swm_iconic(r
->ws
->focus
, 1);
4710 get_win_name(xcb_window_t win
)
4713 xcb_get_property_cookie_t c
;
4714 xcb_get_property_reply_t
*r
;
4716 /* First try _NET_WM_NAME for UTF-8. */
4717 c
= xcb_get_property(conn
, 0, win
, a_netwmname
,
4718 XCB_GET_PROPERTY_TYPE_ANY
, 0, UINT_MAX
);
4719 r
= xcb_get_property_reply(conn
, c
, NULL
);
4722 if (r
->type
== XCB_NONE
) {
4724 /* Use WM_NAME instead; no UTF-8. */
4725 c
= xcb_get_property(conn
, 0, win
, XCB_ATOM_WM_NAME
,
4726 XCB_GET_PROPERTY_TYPE_ANY
, 0, UINT_MAX
);
4727 r
= xcb_get_property_reply(conn
, c
, NULL
);
4731 if (r
->type
== XCB_NONE
) {
4737 name
= strndup(xcb_get_property_value(r
),
4738 xcb_get_property_value_length(r
));
4747 uniconify(struct swm_region
*r
, union arg
*args
)
4754 DNPRINTF(SWM_D_MISC
, "uniconify\n");
4756 if (r
== NULL
|| r
->ws
== NULL
)
4759 /* make sure we have anything to uniconify */
4760 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
4761 if (win
->ws
== NULL
)
4762 continue; /* should never happen */
4771 search_resp_action
= SWM_SEARCH_UNICONIFY
;
4773 spawn_select(r
, args
, "search", &searchpid
);
4775 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
4778 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
4779 if (win
->ws
== NULL
)
4780 continue; /* should never happen */
4784 name
= get_win_name(win
->id
);
4787 fprintf(lfile
, "%s.%u\n", name
, win
->id
);
4795 name_workspace(struct swm_region
*r
, union arg
*args
)
4799 DNPRINTF(SWM_D_MISC
, "name_workspace\n");
4805 search_resp_action
= SWM_SEARCH_NAME_WORKSPACE
;
4807 spawn_select(r
, args
, "name_workspace", &searchpid
);
4809 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
4812 fprintf(lfile
, "%s", "");
4817 search_workspace(struct swm_region
*r
, union arg
*args
)
4820 struct workspace
*ws
;
4823 DNPRINTF(SWM_D_MISC
, "search_workspace\n");
4829 search_resp_action
= SWM_SEARCH_SEARCH_WORKSPACE
;
4831 spawn_select(r
, args
, "search", &searchpid
);
4833 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
4836 for (i
= 0; i
< workspace_limit
; i
++) {
4840 fprintf(lfile
, "%d%s%s\n", ws
->idx
+ 1,
4841 (ws
->name
? ":" : ""), (ws
->name
? ws
->name
: ""));
4848 search_win_cleanup(void)
4850 struct search_window
*sw
= NULL
;
4852 while ((sw
= TAILQ_FIRST(&search_wl
)) != NULL
) {
4853 xcb_destroy_window(conn
, sw
->indicator
);
4854 TAILQ_REMOVE(&search_wl
, sw
, entry
);
4860 search_win(struct swm_region
*r
, union arg
*args
)
4862 struct ws_win
*win
= NULL
;
4863 struct search_window
*sw
= NULL
;
4866 int i
, width
, height
;
4874 XRectangle l_ibox
, l_lbox
;
4876 DNPRINTF(SWM_D_MISC
, "search_win\n");
4879 search_resp_action
= SWM_SEARCH_SEARCH_WINDOW
;
4881 spawn_select(r
, args
, "search", &searchpid
);
4883 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
4886 TAILQ_INIT(&search_wl
);
4889 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
4893 sw
= calloc(1, sizeof(struct search_window
));
4895 warn("search_win: calloc");
4897 search_win_cleanup();
4903 snprintf(s
, sizeof s
, "%d", i
);
4906 w
= xcb_generate_id(conn
);
4907 wa
[0] = r
->s
->c
[SWM_S_COLOR_FOCUS
].pixel
;
4908 wa
[1] = r
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
;
4910 if (bar_font_legacy
) {
4911 XmbTextExtents(bar_fs
, s
, len
, &l_ibox
, &l_lbox
);
4912 width
= l_lbox
.width
+ 4;
4913 height
= bar_fs_extents
->max_logical_extent
.height
+ 4;
4915 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
,
4917 width
= info
.width
+ 4;
4918 height
= bar_font
->height
+ 4;
4921 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, w
, win
->id
, 0, 0,
4922 width
, height
, 1, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
4923 XCB_COPY_FROM_PARENT
, XCB_CW_BACK_PIXEL
|
4924 XCB_CW_BORDER_PIXEL
, wa
);
4926 xcb_map_window(conn
, w
);
4929 TAILQ_INSERT_TAIL(&search_wl
, sw
, entry
);
4931 if (bar_font_legacy
) {
4932 l_gcv
.graphics_exposures
= 0;
4933 l_draw
= XCreateGC(display
, w
, 0, &l_gcv
);
4935 XSetForeground(display
, l_draw
,
4936 r
->s
->c
[SWM_S_COLOR_BAR
].pixel
);
4938 DRAWSTRING(display
, w
, bar_fs
, l_draw
, 2,
4939 (bar_fs_extents
->max_logical_extent
.height
-
4940 l_lbox
.height
) / 2 - l_lbox
.y
, s
, len
);
4942 XFreeGC(display
, l_draw
);
4945 draw
= XftDrawCreate(display
, w
,
4946 DefaultVisual(display
, r
->s
->idx
),
4947 DefaultColormap(display
, r
->s
->idx
));
4949 XftDrawStringUtf8(draw
, &bar_font_color
, bar_font
, 2,
4950 (HEIGHT(r
->bar
) + bar_font
->height
) / 2 -
4951 bar_font
->descent
, (FcChar8
*)s
, len
);
4953 XftDrawDestroy(draw
);
4956 DNPRINTF(SWM_D_MISC
, "search_win: mapped window: 0x%x\n", w
);
4958 fprintf(lfile
, "%d\n", i
);
4968 search_resp_uniconify(const char *resp
, unsigned long len
)
4974 DNPRINTF(SWM_D_MISC
, "search_resp_uniconify: resp: %s\n", resp
);
4976 TAILQ_FOREACH(win
, &search_r
->ws
->winlist
, entry
) {
4979 name
= get_win_name(win
->id
);
4982 if (asprintf(&s
, "%s.%u", name
, win
->id
) == -1) {
4987 if (strncmp(s
, resp
, len
) == 0) {
4988 /* XXX this should be a callback to generalize */
4989 set_swm_iconic(win
, 0);
4998 search_resp_name_workspace(const char *resp
, unsigned long len
)
5000 struct workspace
*ws
;
5002 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: resp: %s\n", resp
);
5004 if (search_r
->ws
== NULL
)
5009 free(search_r
->ws
->name
);
5010 search_r
->ws
->name
= NULL
;
5014 ws
->name
= strdup(resp
);
5015 if (ws
->name
== NULL
) {
5016 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: "
5017 "strdup: %s", strerror(errno
));
5024 search_resp_search_workspace(const char *resp
)
5031 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: resp: %s\n", resp
);
5035 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: strdup: %s",
5042 ws_idx
= (int)strtonum(q
, 1, workspace_limit
, &errstr
);
5044 DNPRINTF(SWM_D_MISC
, "workspace idx is %s: %s",
5051 switchws(search_r
, &a
);
5055 search_resp_search_window(const char *resp
)
5060 struct search_window
*sw
;
5062 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: resp: %s\n", resp
);
5066 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: strdup: %s",
5071 idx
= (int)strtonum(s
, 1, INT_MAX
, &errstr
);
5073 DNPRINTF(SWM_D_MISC
, "window idx is %s: %s",
5080 TAILQ_FOREACH(sw
, &search_wl
, entry
)
5081 if (idx
== sw
->idx
) {
5087 #define MAX_RESP_LEN 1024
5090 search_do_resp(void)
5096 DNPRINTF(SWM_D_MISC
, "search_do_resp:\n");
5101 if ((resp
= calloc(1, MAX_RESP_LEN
+ 1)) == NULL
) {
5102 warn("search: calloc");
5106 rbytes
= read(select_resp_pipe
[0], resp
, MAX_RESP_LEN
);
5108 warn("search: read error");
5111 resp
[rbytes
] = '\0';
5114 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
5115 * newline, so work around that by sanitizing the resp now.
5117 resp
[strcspn(resp
, "\n")] = '\0';
5120 switch (search_resp_action
) {
5121 case SWM_SEARCH_UNICONIFY
:
5122 search_resp_uniconify(resp
, len
);
5124 case SWM_SEARCH_NAME_WORKSPACE
:
5125 search_resp_name_workspace(resp
, len
);
5127 case SWM_SEARCH_SEARCH_WORKSPACE
:
5128 search_resp_search_workspace(resp
);
5130 case SWM_SEARCH_SEARCH_WINDOW
:
5131 search_resp_search_window(resp
);
5136 if (search_resp_action
== SWM_SEARCH_SEARCH_WINDOW
)
5137 search_win_cleanup();
5139 search_resp_action
= SWM_SEARCH_NONE
;
5140 close(select_resp_pipe
[0]);
5147 wkill(struct swm_region
*r
, union arg
*args
)
5149 DNPRINTF(SWM_D_MISC
, "wkill: id: %d\n", args
->id
);
5151 if (r
->ws
->focus
== NULL
)
5154 if (args
->id
== SWM_ARG_ID_KILLWINDOW
)
5155 xcb_kill_client(conn
, r
->ws
->focus
->id
);
5157 if (r
->ws
->focus
->can_delete
)
5158 client_msg(r
->ws
->focus
, a_delete
, 0);
5164 floating_toggle_win(struct ws_win
*win
)
5166 struct swm_region
*r
;
5176 /* reject floating toggles in max stack mode */
5177 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
5180 if (win
->floating
) {
5181 if (!win
->floatmaxed
) {
5182 /* retain position for refloat */
5183 store_float_geom(win
, r
);
5187 load_float_geom(win
, r
);
5191 ewmh_update_actions(win
);
5197 floating_toggle(struct swm_region
*r
, union arg
*args
)
5199 struct ws_win
*win
= r
->ws
->focus
;
5201 /* suppress unused warning since var is needed */
5207 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
5210 ewmh_update_win_state(win
, ewmh
[_NET_WM_STATE_ABOVE
].atom
,
5211 _NET_WM_STATE_TOGGLE
);
5215 if (win
== win
->ws
->focus
)
5222 constrain_window(struct ws_win
*win
, struct swm_region
*r
, int resizable
)
5224 if (MAX_X(win
) + BORDER(win
) > MAX_X(r
)) {
5226 WIDTH(win
) = MAX_X(r
) - X(win
) - BORDER(win
);
5228 X(win
) = MAX_X(r
)- WIDTH(win
) - BORDER(win
);
5231 if (X(win
) + BORDER(win
) < X(r
)) {
5233 WIDTH(win
) -= X(r
) - X(win
) - BORDER(win
);
5235 X(win
) = X(r
) - BORDER(win
);
5238 if (MAX_Y(win
) + BORDER(win
) > MAX_Y(r
)) {
5240 HEIGHT(win
) = MAX_Y(r
) - Y(win
) - BORDER(win
);
5242 Y(win
) = MAX_Y(r
) - HEIGHT(win
) - BORDER(win
);
5245 if (Y(win
) + BORDER(win
) < Y(r
)) {
5247 HEIGHT(win
) -= Y(r
) - Y(win
) - BORDER(win
);
5249 Y(win
) = Y(r
) - BORDER(win
);
5255 if (HEIGHT(win
) < 1)
5261 update_window(struct ws_win
*win
)
5266 mask
= XCB_CONFIG_WINDOW_X
| XCB_CONFIG_WINDOW_Y
|
5267 XCB_CONFIG_WINDOW_WIDTH
| XCB_CONFIG_WINDOW_HEIGHT
|
5268 XCB_CONFIG_WINDOW_BORDER_WIDTH
;
5272 wc
[3] = HEIGHT(win
);
5273 wc
[4] = BORDER(win
);
5275 DNPRINTF(SWM_D_EVENT
, "update_window: window: 0x%x, (x,y) w x h: "
5276 "(%d,%d) %d x %d, bordered: %s\n", win
->id
, wc
[0], wc
[1], wc
[2],
5277 wc
[3], YESNO(win
->bordered
));
5279 xcb_configure_window(conn
, win
->id
, mask
, wc
);
5282 #define SWM_RESIZE_STEPS (50)
5285 resize(struct ws_win
*win
, union arg
*args
)
5287 xcb_timestamp_t timestamp
= 0;
5288 struct swm_region
*r
= NULL
;
5290 struct swm_geometry g
;
5291 int top
= 0, left
= 0, resizing
;
5293 xcb_cursor_t cursor
;
5294 xcb_query_pointer_reply_t
*xpr
;
5295 xcb_generic_event_t
*evt
;
5296 xcb_motion_notify_event_t
*mne
;
5302 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
5305 DNPRINTF(SWM_D_EVENT
, "resize: window: 0x%x, floating: %s, "
5306 "transient: 0x%x\n", win
->id
, YESNO(win
->floating
),
5309 if (!win
->transient
&& !win
->floating
)
5312 /* reject resizes in max mode for floaters (transient ok) */
5313 if (win
->floatmaxed
)
5317 ewmh_update_win_state(win
, ewmh
[_SWM_WM_STATE_MANUAL
].atom
,
5325 case SWM_ARG_ID_WIDTHSHRINK
:
5326 WIDTH(win
) -= SWM_RESIZE_STEPS
;
5329 case SWM_ARG_ID_WIDTHGROW
:
5330 WIDTH(win
) += SWM_RESIZE_STEPS
;
5333 case SWM_ARG_ID_HEIGHTSHRINK
:
5334 HEIGHT(win
) -= SWM_RESIZE_STEPS
;
5337 case SWM_ARG_ID_HEIGHTGROW
:
5338 HEIGHT(win
) += SWM_RESIZE_STEPS
;
5345 constrain_window(win
, r
, 1);
5347 store_float_geom(win
,r
);
5351 /* get cursor offset from window root */
5352 xpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
5359 if (xpr
->win_x
< WIDTH(win
) / 2)
5362 if (xpr
->win_y
< HEIGHT(win
) / 2)
5365 if (args
->id
== SWM_ARG_ID_CENTER
)
5366 cursor
= cursors
[XC_SIZING
].cid
;
5368 cursor
= cursors
[left
? XC_TOP_LEFT_CORNER
:
5369 XC_TOP_RIGHT_CORNER
].cid
;
5371 cursor
= cursors
[left
? XC_BOTTOM_LEFT_CORNER
:
5372 XC_BOTTOM_RIGHT_CORNER
].cid
;
5374 xcb_grab_pointer(conn
, 0, win
->id
, MOUSEMASK
,
5375 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
, XCB_WINDOW_NONE
, cursor
,
5380 while ((evt
= xcb_wait_for_event(conn
)) && resizing
) {
5381 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
5382 case XCB_BUTTON_RELEASE
:
5383 DNPRINTF(SWM_D_EVENT
, "resize: BUTTON_RELEASE\n");
5386 case XCB_MOTION_NOTIFY
:
5387 mne
= (xcb_motion_notify_event_t
*)evt
;
5388 /* cursor offset/delta from start of the operation */
5389 dx
= mne
->root_x
- xpr
->root_x
;
5390 dy
= mne
->root_y
- xpr
->root_y
;
5396 if (args
->id
== SWM_ARG_ID_CENTER
) {
5397 if (g
.h
/ 2 + dy
< 1)
5401 HEIGHT(win
) = g
.h
+ 2 * dy
;
5409 HEIGHT(win
) = g
.h
+ dy
;
5416 if (args
->id
== SWM_ARG_ID_CENTER
) {
5417 if (g
.w
/ 2 + dx
< 1)
5421 WIDTH(win
) = g
.w
+ 2 * dx
;
5429 WIDTH(win
) = g
.w
+ dx
;
5432 constrain_window(win
, r
, 1);
5434 /* not free, don't sync more than 120 times / second */
5435 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
5436 timestamp
= mne
->time
;
5451 store_float_geom(win
,r
);
5453 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
5455 DNPRINTF(SWM_D_EVENT
, "resize: done.\n");
5459 resize_step(struct swm_region
*r
, union arg
*args
)
5461 struct ws_win
*win
= NULL
;
5463 if (r
&& r
->ws
&& r
->ws
->focus
)
5472 #define SWM_MOVE_STEPS (50)
5475 move(struct ws_win
*win
, union arg
*args
)
5477 xcb_timestamp_t timestamp
= 0;
5478 int move_stp
= 0, moving
;
5479 struct swm_region
*r
= NULL
;
5480 xcb_query_pointer_reply_t
*qpr
;
5481 xcb_generic_event_t
*evt
;
5482 xcb_motion_notify_event_t
*mne
;
5488 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
5491 DNPRINTF(SWM_D_EVENT
, "move: window: 0x%x, floating: %s, transient: "
5492 "0x%x\n", win
->id
, YESNO(win
->floating
), win
->transient
);
5494 /* in max_stack mode should only move transients */
5495 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
] && !win
->transient
)
5499 if (!win
->floating
&& !win
->transient
) {
5500 store_float_geom(win
, r
);
5501 ewmh_update_win_state(win
, ewmh
[_NET_WM_STATE_ABOVE
].atom
,
5504 ewmh_update_win_state(win
, ewmh
[_SWM_WM_STATE_MANUAL
].atom
,
5513 case SWM_ARG_ID_MOVELEFT
:
5514 X(win
) -= (SWM_MOVE_STEPS
- border_width
);
5517 case SWM_ARG_ID_MOVERIGHT
:
5518 X(win
) += (SWM_MOVE_STEPS
- border_width
);
5521 case SWM_ARG_ID_MOVEUP
:
5522 Y(win
) -= (SWM_MOVE_STEPS
- border_width
);
5525 case SWM_ARG_ID_MOVEDOWN
:
5526 Y(win
) += (SWM_MOVE_STEPS
- border_width
);
5533 constrain_window(win
, r
, 0);
5535 store_float_geom(win
, r
);
5539 xcb_grab_pointer(conn
, 0, win
->id
, MOUSEMASK
,
5540 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
,
5541 XCB_WINDOW_NONE
, cursors
[XC_FLEUR
].cid
, XCB_CURRENT_TIME
);
5543 /* get cursor offset from window root */
5544 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
5547 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
5553 while ((evt
= xcb_wait_for_event(conn
)) && moving
) {
5554 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
5555 case XCB_BUTTON_RELEASE
:
5556 DNPRINTF(SWM_D_EVENT
, "move: BUTTON_RELEASE\n");
5559 case XCB_MOTION_NOTIFY
:
5560 mne
= (xcb_motion_notify_event_t
*)evt
;
5561 X(win
) = mne
->root_x
- qpr
->win_x
- border_width
;
5562 Y(win
) = mne
->root_y
- qpr
->win_y
- border_width
;
5564 constrain_window(win
, r
, 0);
5566 /* not free, don't sync more than 120 times / second */
5567 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
5568 timestamp
= mne
->time
;
5583 store_float_geom(win
, r
);
5585 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
5586 DNPRINTF(SWM_D_EVENT
, "move: done.\n");
5590 move_step(struct swm_region
*r
, union arg
*args
)
5592 struct ws_win
*win
= NULL
;
5594 if (r
&& r
->ws
&& r
->ws
->focus
)
5599 if (!win
->transient
&& !win
->floating
)
5606 /* key definitions */
5608 char name
[SWM_FUNCNAME_LEN
];
5609 void (*func
)(struct swm_region
*r
, union arg
*);
5611 } keyfuncs
[KF_INVALID
+ 1] = {
5612 /* name function argument */
5613 { "bar_toggle", bar_toggle
, {.id
= SWM_ARG_ID_BAR_TOGGLE
} },
5614 { "bar_toggle_ws", bar_toggle
, {.id
= SWM_ARG_ID_BAR_TOGGLE_WS
} },
5615 { "button2", pressbutton
, {2} },
5616 { "cycle_layout", cycle_layout
, {0} },
5617 { "flip_layout", stack_config
, {.id
= SWM_ARG_ID_FLIPLAYOUT
} },
5618 { "float_toggle", floating_toggle
,{0} },
5619 { "focus_main", focus
, {.id
= SWM_ARG_ID_FOCUSMAIN
} },
5620 { "focus_next", focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
5621 { "focus_prev", focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
5622 { "height_grow", resize_step
, {.id
= SWM_ARG_ID_HEIGHTGROW
} },
5623 { "height_shrink", resize_step
, {.id
= SWM_ARG_ID_HEIGHTSHRINK
} },
5624 { "iconify", iconify
, {0} },
5625 { "master_shrink", stack_config
, {.id
= SWM_ARG_ID_MASTERSHRINK
} },
5626 { "master_grow", stack_config
, {.id
= SWM_ARG_ID_MASTERGROW
} },
5627 { "master_add", stack_config
, {.id
= SWM_ARG_ID_MASTERADD
} },
5628 { "master_del", stack_config
, {.id
= SWM_ARG_ID_MASTERDEL
} },
5629 { "move_down", move_step
, {.id
= SWM_ARG_ID_MOVEDOWN
} },
5630 { "move_left", move_step
, {.id
= SWM_ARG_ID_MOVELEFT
} },
5631 { "move_right", move_step
, {.id
= SWM_ARG_ID_MOVERIGHT
} },
5632 { "move_up", move_step
, {.id
= SWM_ARG_ID_MOVEUP
} },
5633 { "mvrg_1", send_to_rg
, {.id
= 0} },
5634 { "mvrg_2", send_to_rg
, {.id
= 1} },
5635 { "mvrg_3", send_to_rg
, {.id
= 2} },
5636 { "mvrg_4", send_to_rg
, {.id
= 3} },
5637 { "mvrg_5", send_to_rg
, {.id
= 4} },
5638 { "mvrg_6", send_to_rg
, {.id
= 5} },
5639 { "mvrg_7", send_to_rg
, {.id
= 6} },
5640 { "mvrg_8", send_to_rg
, {.id
= 7} },
5641 { "mvrg_9", send_to_rg
, {.id
= 8} },
5642 { "mvws_1", send_to_ws
, {.id
= 0} },
5643 { "mvws_2", send_to_ws
, {.id
= 1} },
5644 { "mvws_3", send_to_ws
, {.id
= 2} },
5645 { "mvws_4", send_to_ws
, {.id
= 3} },
5646 { "mvws_5", send_to_ws
, {.id
= 4} },
5647 { "mvws_6", send_to_ws
, {.id
= 5} },
5648 { "mvws_7", send_to_ws
, {.id
= 6} },
5649 { "mvws_8", send_to_ws
, {.id
= 7} },
5650 { "mvws_9", send_to_ws
, {.id
= 8} },
5651 { "mvws_10", send_to_ws
, {.id
= 9} },
5652 { "mvws_11", send_to_ws
, {.id
= 10} },
5653 { "mvws_12", send_to_ws
, {.id
= 11} },
5654 { "mvws_13", send_to_ws
, {.id
= 12} },
5655 { "mvws_14", send_to_ws
, {.id
= 13} },
5656 { "mvws_15", send_to_ws
, {.id
= 14} },
5657 { "mvws_16", send_to_ws
, {.id
= 15} },
5658 { "mvws_17", send_to_ws
, {.id
= 16} },
5659 { "mvws_18", send_to_ws
, {.id
= 17} },
5660 { "mvws_19", send_to_ws
, {.id
= 18} },
5661 { "mvws_20", send_to_ws
, {.id
= 19} },
5662 { "mvws_21", send_to_ws
, {.id
= 20} },
5663 { "mvws_22", send_to_ws
, {.id
= 21} },
5664 { "name_workspace", name_workspace
, {0} },
5665 { "quit", quit
, {0} },
5666 { "raise_toggle", raise_toggle
, {0} },
5667 { "restart", restart
, {0} },
5668 { "rg_1", focusrg
, {.id
= 0} },
5669 { "rg_2", focusrg
, {.id
= 1} },
5670 { "rg_3", focusrg
, {.id
= 2} },
5671 { "rg_4", focusrg
, {.id
= 3} },
5672 { "rg_5", focusrg
, {.id
= 4} },
5673 { "rg_6", focusrg
, {.id
= 5} },
5674 { "rg_7", focusrg
, {.id
= 6} },
5675 { "rg_8", focusrg
, {.id
= 7} },
5676 { "rg_9", focusrg
, {.id
= 8} },
5677 { "rg_next", cyclerg
, {.id
= SWM_ARG_ID_CYCLERG_UP
} },
5678 { "rg_prev", cyclerg
, {.id
= SWM_ARG_ID_CYCLERG_DOWN
} },
5679 { "screen_next", cyclerg
, {.id
= SWM_ARG_ID_CYCLERG_UP
} },
5680 { "screen_prev", cyclerg
, {.id
= SWM_ARG_ID_CYCLERG_DOWN
} },
5681 { "search_win", search_win
, {0} },
5682 { "search_workspace", search_workspace
, {0} },
5683 { "spawn_custom", NULL
, {0} },
5684 { "stack_inc", stack_config
, {.id
= SWM_ARG_ID_STACKINC
} },
5685 { "stack_dec", stack_config
, {.id
= SWM_ARG_ID_STACKDEC
} },
5686 { "stack_reset", stack_config
, {.id
= SWM_ARG_ID_STACKRESET
} },
5687 { "swap_main", swapwin
, {.id
= SWM_ARG_ID_SWAPMAIN
} },
5688 { "swap_next", swapwin
, {.id
= SWM_ARG_ID_SWAPNEXT
} },
5689 { "swap_prev", swapwin
, {.id
= SWM_ARG_ID_SWAPPREV
} },
5690 { "uniconify", uniconify
, {0} },
5691 { "version", version
, {0} },
5692 { "width_grow", resize_step
, {.id
= SWM_ARG_ID_WIDTHGROW
} },
5693 { "width_shrink", resize_step
, {.id
= SWM_ARG_ID_WIDTHSHRINK
} },
5694 { "wind_del", wkill
, {.id
= SWM_ARG_ID_DELETEWINDOW
} },
5695 { "wind_kill", wkill
, {.id
= SWM_ARG_ID_KILLWINDOW
} },
5696 { "ws_1", switchws
, {.id
= 0} },
5697 { "ws_2", switchws
, {.id
= 1} },
5698 { "ws_3", switchws
, {.id
= 2} },
5699 { "ws_4", switchws
, {.id
= 3} },
5700 { "ws_5", switchws
, {.id
= 4} },
5701 { "ws_6", switchws
, {.id
= 5} },
5702 { "ws_7", switchws
, {.id
= 6} },
5703 { "ws_8", switchws
, {.id
= 7} },
5704 { "ws_9", switchws
, {.id
= 8} },
5705 { "ws_10", switchws
, {.id
= 9} },
5706 { "ws_11", switchws
, {.id
= 10} },
5707 { "ws_12", switchws
, {.id
= 11} },
5708 { "ws_13", switchws
, {.id
= 12} },
5709 { "ws_14", switchws
, {.id
= 13} },
5710 { "ws_15", switchws
, {.id
= 14} },
5711 { "ws_16", switchws
, {.id
= 15} },
5712 { "ws_17", switchws
, {.id
= 16} },
5713 { "ws_18", switchws
, {.id
= 17} },
5714 { "ws_19", switchws
, {.id
= 18} },
5715 { "ws_20", switchws
, {.id
= 19} },
5716 { "ws_21", switchws
, {.id
= 20} },
5717 { "ws_22", switchws
, {.id
= 21} },
5718 { "ws_next", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP
} },
5719 { "ws_next_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP_ALL
} },
5720 { "ws_prev", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN
} },
5721 { "ws_prev_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN_ALL
} },
5722 { "ws_prior", priorws
, {0} },
5723 { "dumpwins", dumpwins
, {0} }, /* MUST BE LAST */
5724 { "invalid key func", NULL
, {0} },
5728 key_cmp(struct key
*kp1
, struct key
*kp2
)
5730 if (kp1
->keysym
< kp2
->keysym
)
5732 if (kp1
->keysym
> kp2
->keysym
)
5735 if (kp1
->mod
< kp2
->mod
)
5737 if (kp1
->mod
> kp2
->mod
)
5744 enum { client_click
, root_click
};
5746 unsigned int action
;
5748 unsigned int button
;
5749 void (*func
)(struct ws_win
*, union arg
*);
5752 #define MODKEY_SHIFT MODKEY | XCB_MOD_MASK_SHIFT
5753 /* action key mouse button func args */
5754 { client_click
, MODKEY
, XCB_BUTTON_INDEX_3
, resize
, {.id
= SWM_ARG_ID_DONTCENTER
} },
5755 { client_click
, MODKEY_SHIFT
, XCB_BUTTON_INDEX_3
, resize
, {.id
= SWM_ARG_ID_CENTER
} },
5756 { client_click
, MODKEY
, XCB_BUTTON_INDEX_1
, move
, {0} },
5761 update_modkey(unsigned int mod
)
5767 RB_FOREACH(kp
, key_tree
, &keys
)
5768 if (kp
->mod
& XCB_MOD_MASK_SHIFT
)
5769 kp
->mod
= mod
| XCB_MOD_MASK_SHIFT
;
5773 for (i
= 0; i
< LENGTH(buttons
); i
++)
5774 if (buttons
[i
].mask
& XCB_MOD_MASK_SHIFT
)
5775 buttons
[i
].mask
= mod
| XCB_MOD_MASK_SHIFT
;
5777 buttons
[i
].mask
= mod
;
5781 spawn_expand(struct swm_region
*r
, union arg
*args
, const char *spawn_name
,
5784 struct spawn_prog
*prog
= NULL
;
5786 char *ap
, **real_args
;
5788 /* suppress unused warning since var is needed */
5791 DNPRINTF(SWM_D_SPAWN
, "spawn_expand: %s\n", spawn_name
);
5794 TAILQ_FOREACH(prog
, &spawns
, entry
) {
5795 if (!strcasecmp(spawn_name
, prog
->name
))
5799 warnx("spawn_custom: program %s not found", spawn_name
);
5803 /* make room for expanded args */
5804 if ((real_args
= calloc(prog
->argc
+ 1, sizeof(char *))) == NULL
)
5805 err(1, "spawn_custom: calloc real_args");
5807 /* expand spawn_args into real_args */
5808 for (i
= 0; i
< prog
->argc
; i
++) {
5810 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: raw arg: %s\n", ap
);
5811 if (!strcasecmp(ap
, "$bar_border")) {
5813 strdup(r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].name
))
5815 err(1, "spawn_custom border color");
5816 } else if (!strcasecmp(ap
, "$bar_color")) {
5818 strdup(r
->s
->c
[SWM_S_COLOR_BAR
].name
))
5820 err(1, "spawn_custom bar color");
5821 } else if (!strcasecmp(ap
, "$bar_font")) {
5822 if ((real_args
[i
] = strdup(bar_fonts
))
5824 err(1, "spawn_custom bar fonts");
5825 } else if (!strcasecmp(ap
, "$bar_font_color")) {
5827 strdup(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].name
))
5829 err(1, "spawn_custom color font");
5830 } else if (!strcasecmp(ap
, "$color_focus")) {
5832 strdup(r
->s
->c
[SWM_S_COLOR_FOCUS
].name
))
5834 err(1, "spawn_custom color focus");
5835 } else if (!strcasecmp(ap
, "$color_unfocus")) {
5837 strdup(r
->s
->c
[SWM_S_COLOR_UNFOCUS
].name
))
5839 err(1, "spawn_custom color unfocus");
5840 } else if (!strcasecmp(ap
, "$region_index")) {
5841 if (asprintf(&real_args
[i
], "%d",
5842 get_region_index(r
) + 1) < 1)
5843 err(1, "spawn_custom region index");
5844 } else if (!strcasecmp(ap
, "$workspace_index")) {
5845 if (asprintf(&real_args
[i
], "%d", r
->ws
->idx
+ 1) < 1)
5846 err(1, "spawn_custom workspace index");
5848 /* no match --> copy as is */
5849 if ((real_args
[i
] = strdup(ap
)) == NULL
)
5850 err(1, "spawn_custom strdup(ap)");
5852 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: cooked arg: %s\n",
5857 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: result: ");
5858 for (i
= 0; i
< prog
->argc
; i
++)
5859 DNPRINTF(SWM_D_SPAWN
, "\"%s\" ", real_args
[i
]);
5860 DNPRINTF(SWM_D_SPAWN
, "\n");
5862 *ret_args
= real_args
;
5863 return (prog
->argc
);
5867 spawn_custom(struct swm_region
*r
, union arg
*args
, const char *spawn_name
)
5873 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
5877 spawn(r
->ws
->idx
, &a
, 1);
5879 for (i
= 0; i
< spawn_argc
; i
++)
5885 spawn_select(struct swm_region
*r
, union arg
*args
, const char *spawn_name
,
5892 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
5896 if (pipe(select_list_pipe
) == -1)
5897 err(1, "pipe error");
5898 if (pipe(select_resp_pipe
) == -1)
5899 err(1, "pipe error");
5901 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
5902 err(1, "could not disable SIGPIPE");
5903 switch (*pid
= fork()) {
5905 err(1, "cannot fork");
5908 if (dup2(select_list_pipe
[0], STDIN_FILENO
) == -1)
5910 if (dup2(select_resp_pipe
[1], STDOUT_FILENO
) == -1)
5912 close(select_list_pipe
[1]);
5913 close(select_resp_pipe
[0]);
5914 spawn(r
->ws
->idx
, &a
, 0);
5916 default: /* parent */
5917 close(select_list_pipe
[0]);
5918 close(select_resp_pipe
[1]);
5922 for (i
= 0; i
< spawn_argc
; i
++)
5928 spawn_insert(const char *name
, const char *args
)
5930 char *arg
, *cp
, *ptr
;
5931 struct spawn_prog
*sp
;
5933 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: %s\n", name
);
5935 if ((sp
= calloc(1, sizeof *sp
)) == NULL
)
5936 err(1, "spawn_insert: calloc");
5937 if ((sp
->name
= strdup(name
)) == NULL
)
5938 err(1, "spawn_insert: strdup");
5940 /* convert the arguments to an argument list */
5941 if ((ptr
= cp
= strdup(args
)) == NULL
)
5942 err(1, "spawn_insert: strdup");
5943 while ((arg
= strsep(&ptr
, " \t")) != NULL
) {
5944 /* empty field; skip it */
5949 if ((sp
->argv
= realloc(sp
->argv
, sp
->argc
*
5950 sizeof *sp
->argv
)) == NULL
)
5951 err(1, "spawn_insert: realloc");
5952 if ((sp
->argv
[sp
->argc
- 1] = strdup(arg
)) == NULL
)
5953 err(1, "spawn_insert: strdup");
5957 TAILQ_INSERT_TAIL(&spawns
, sp
, entry
);
5958 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: leave\n");
5962 spawn_remove(struct spawn_prog
*sp
)
5966 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: %s\n", sp
->name
);
5968 TAILQ_REMOVE(&spawns
, sp
, entry
);
5969 for (i
= 0; i
< sp
->argc
; i
++)
5975 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: leave\n");
5979 spawn_replace(struct spawn_prog
*sp
, const char *name
, const char *args
)
5981 DNPRINTF(SWM_D_SPAWN
, "spawn_replace: %s [%s]\n", sp
->name
, name
);
5984 spawn_insert(name
, args
);
5986 DNPRINTF(SWM_D_SPAWN
, "spawn_replace: leave\n");
5990 setspawn(const char *name
, const char *args
)
5992 struct spawn_prog
*sp
;
5994 DNPRINTF(SWM_D_SPAWN
, "setspawn: %s\n", name
);
5999 TAILQ_FOREACH(sp
, &spawns
, entry
) {
6000 if (!strcmp(sp
->name
, name
)) {
6004 spawn_replace(sp
, name
, args
);
6005 DNPRINTF(SWM_D_SPAWN
, "setspawn: leave\n");
6009 if (*args
== '\0') {
6010 warnx("error: setspawn: cannot find program: %s", name
);
6014 spawn_insert(name
, args
);
6015 DNPRINTF(SWM_D_SPAWN
, "setspawn: leave\n");
6019 setconfspawn(char *selector
, char *value
, int flags
)
6023 /* suppress unused warning since var is needed */
6026 args
= expand_tilde(value
);
6028 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: [%s] [%s]\n", selector
, args
);
6030 setspawn(selector
, args
);
6033 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: done.\n");
6040 setconfspawn("term", "xterm", 0);
6041 setconfspawn("spawn_term", "xterm", 0);
6042 setconfspawn("screenshot_all", "screenshot.sh full", 0);
6043 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
6044 setconfspawn("lock", "xlock", 0);
6045 setconfspawn("initscr", "initscreen.sh", 0);
6046 setconfspawn("menu", "dmenu_run"
6049 " -nf $bar_font_color"
6051 " -sf $bar_color", 0);
6052 setconfspawn("search", "dmenu"
6056 " -nf $bar_font_color"
6058 " -sf $bar_color", 0);
6059 setconfspawn("name_workspace", "dmenu"
6063 " -nf $bar_font_color"
6065 " -sf $bar_color", 0);
6067 /* only test dmenu for now, really should expand this */
6068 if (system("dmenu -v") != 0)
6069 add_startup_exception("you must install dmenu");
6073 #define SWM_MODNAME_SIZE 32
6074 #define SWM_KEY_WS "\n+ \t"
6076 parsekeys(char *keystr
, unsigned int currmod
, unsigned int *mod
, KeySym
*ks
)
6080 DNPRINTF(SWM_D_KEY
, "parsekeys: enter [%s]\n", keystr
);
6081 if (mod
== NULL
|| ks
== NULL
) {
6082 DNPRINTF(SWM_D_KEY
, "parsekeys: no mod or key vars\n");
6085 if (keystr
== NULL
|| strlen(keystr
) == 0) {
6086 DNPRINTF(SWM_D_KEY
, "parsekeys: no keystr\n");
6090 *ks
= XCB_NO_SYMBOL
;
6092 while ((name
= strsep(&cp
, SWM_KEY_WS
)) != NULL
) {
6093 DNPRINTF(SWM_D_KEY
, "parsekeys: key [%s]\n", name
);
6095 cp
+= (long)strspn(cp
, SWM_KEY_WS
);
6096 if (strncasecmp(name
, "MOD", SWM_MODNAME_SIZE
) == 0)
6098 else if (!strncasecmp(name
, "Mod1", SWM_MODNAME_SIZE
))
6099 *mod
|= XCB_MOD_MASK_1
;
6100 else if (!strncasecmp(name
, "Mod2", SWM_MODNAME_SIZE
))
6101 *mod
+= XCB_MOD_MASK_2
;
6102 else if (!strncmp(name
, "Mod3", SWM_MODNAME_SIZE
))
6103 *mod
|= XCB_MOD_MASK_3
;
6104 else if (!strncmp(name
, "Mod4", SWM_MODNAME_SIZE
))
6105 *mod
|= XCB_MOD_MASK_4
;
6106 else if (strncasecmp(name
, "SHIFT", SWM_MODNAME_SIZE
) == 0)
6107 *mod
|= XCB_MOD_MASK_SHIFT
;
6108 else if (strncasecmp(name
, "CONTROL", SWM_MODNAME_SIZE
) == 0)
6109 *mod
|= XCB_MOD_MASK_CONTROL
;
6111 *ks
= XStringToKeysym(name
);
6112 XConvertCase(*ks
, ks
, &uks
);
6113 if (ks
== XCB_NO_SYMBOL
) {
6115 "parsekeys: invalid key %s\n",
6121 DNPRINTF(SWM_D_KEY
, "parsekeys: leave ok\n");
6126 strdupsafe(const char *str
)
6131 return (strdup(str
));
6135 key_insert(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
6136 const char *spawn_name
)
6140 DNPRINTF(SWM_D_KEY
, "key_insert: enter %s [%s]\n",
6141 keyfuncs
[kfid
].name
, spawn_name
);
6143 if ((kp
= malloc(sizeof *kp
)) == NULL
)
6144 err(1, "key_insert: malloc");
6149 kp
->spawn_name
= strdupsafe(spawn_name
);
6150 RB_INSERT(key_tree
, &keys
, kp
);
6152 DNPRINTF(SWM_D_KEY
, "key_insert: leave\n");
6156 key_lookup(unsigned int mod
, KeySym ks
)
6163 return (RB_FIND(key_tree
, &keys
, &kp
));
6167 key_remove(struct key
*kp
)
6169 DNPRINTF(SWM_D_KEY
, "key_remove: %s\n", keyfuncs
[kp
->funcid
].name
);
6171 RB_REMOVE(key_tree
, &keys
, kp
);
6172 free(kp
->spawn_name
);
6175 DNPRINTF(SWM_D_KEY
, "key_remove: leave\n");
6179 key_replace(struct key
*kp
, unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
6180 const char *spawn_name
)
6182 DNPRINTF(SWM_D_KEY
, "key_replace: %s [%s]\n", keyfuncs
[kp
->funcid
].name
,
6186 key_insert(mod
, ks
, kfid
, spawn_name
);
6188 DNPRINTF(SWM_D_KEY
, "key_replace: leave\n");
6192 setkeybinding(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
6193 const char *spawn_name
)
6197 DNPRINTF(SWM_D_KEY
, "setkeybinding: enter %s [%s]\n",
6198 keyfuncs
[kfid
].name
, spawn_name
);
6200 if ((kp
= key_lookup(mod
, ks
)) != NULL
) {
6201 if (kfid
== KF_INVALID
)
6204 key_replace(kp
, mod
, ks
, kfid
, spawn_name
);
6205 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
6208 if (kfid
== KF_INVALID
) {
6209 warnx("bind: Key combination already unbound.");
6210 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
6214 key_insert(mod
, ks
, kfid
, spawn_name
);
6215 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
6219 setconfbinding(char *selector
, char *value
, int flags
)
6221 enum keyfuncid kfid
;
6224 struct spawn_prog
*sp
;
6226 /* suppress unused warning since var is needed */
6229 DNPRINTF(SWM_D_KEY
, "setconfbinding: enter\n");
6230 if (selector
== NULL
) {
6231 DNPRINTF(SWM_D_KEY
, "setconfbinding: unbind %s\n", value
);
6232 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
6234 setkeybinding(mod
, ks
, kfid
, NULL
);
6239 /* search by key function name */
6240 for (kfid
= 0; kfid
< KF_INVALID
; (kfid
)++) {
6241 if (strncasecmp(selector
, keyfuncs
[kfid
].name
,
6242 SWM_FUNCNAME_LEN
) == 0) {
6243 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match\n",
6245 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
6246 setkeybinding(mod
, ks
, kfid
, NULL
);
6252 /* search by custom spawn name */
6253 TAILQ_FOREACH(sp
, &spawns
, entry
) {
6254 if (strcasecmp(selector
, sp
->name
) == 0) {
6255 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match\n",
6257 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
6258 setkeybinding(mod
, ks
, KF_SPAWN_CUSTOM
,
6265 DNPRINTF(SWM_D_KEY
, "setconfbinding: no match\n");
6272 #define MODKEY_SHIFT MODKEY | XCB_MOD_MASK_SHIFT
6273 setkeybinding(MODKEY
, XK_b
, KF_BAR_TOGGLE
, NULL
);
6274 setkeybinding(MODKEY_SHIFT
, XK_b
, KF_BAR_TOGGLE_WS
,NULL
);
6275 setkeybinding(MODKEY
, XK_v
, KF_BUTTON2
, NULL
);
6276 setkeybinding(MODKEY
, XK_space
, KF_CYCLE_LAYOUT
,NULL
);
6277 setkeybinding(MODKEY_SHIFT
, XK_backslash
, KF_FLIP_LAYOUT
, NULL
);
6278 setkeybinding(MODKEY
, XK_t
, KF_FLOAT_TOGGLE
,NULL
);
6279 setkeybinding(MODKEY
, XK_m
, KF_FOCUS_MAIN
, NULL
);
6280 setkeybinding(MODKEY
, XK_j
, KF_FOCUS_NEXT
, NULL
);
6281 setkeybinding(MODKEY
, XK_Tab
, KF_FOCUS_NEXT
, NULL
);
6282 setkeybinding(MODKEY
, XK_k
, KF_FOCUS_PREV
, NULL
);
6283 setkeybinding(MODKEY_SHIFT
, XK_Tab
, KF_FOCUS_PREV
, NULL
);
6284 setkeybinding(MODKEY_SHIFT
, XK_equal
, KF_HEIGHT_GROW
,NULL
);
6285 setkeybinding(MODKEY_SHIFT
, XK_minus
, KF_HEIGHT_SHRINK
,NULL
);
6286 setkeybinding(MODKEY
, XK_w
, KF_ICONIFY
, NULL
);
6287 setkeybinding(MODKEY
, XK_h
, KF_MASTER_SHRINK
, NULL
);
6288 setkeybinding(MODKEY
, XK_l
, KF_MASTER_GROW
, NULL
);
6289 setkeybinding(MODKEY
, XK_comma
, KF_MASTER_ADD
, NULL
);
6290 setkeybinding(MODKEY
, XK_period
, KF_MASTER_DEL
, NULL
);
6291 setkeybinding(MODKEY_SHIFT
, XK_bracketright
,KF_MOVE_DOWN
,NULL
);
6292 setkeybinding(MODKEY
, XK_bracketleft
, KF_MOVE_LEFT
,NULL
);
6293 setkeybinding(MODKEY
, XK_bracketright
,KF_MOVE_RIGHT
,NULL
);
6294 setkeybinding(MODKEY_SHIFT
, XK_bracketleft
, KF_MOVE_UP
, NULL
);
6295 setkeybinding(MODKEY_SHIFT
, XK_KP_End
, KF_MVRG_1
, NULL
);
6296 setkeybinding(MODKEY_SHIFT
, XK_KP_Down
, KF_MVRG_2
, NULL
);
6297 setkeybinding(MODKEY_SHIFT
, XK_KP_Next
, KF_MVRG_3
, NULL
);
6298 setkeybinding(MODKEY_SHIFT
, XK_KP_Left
, KF_MVRG_4
, NULL
);
6299 setkeybinding(MODKEY_SHIFT
, XK_KP_Begin
, KF_MVRG_5
, NULL
);
6300 setkeybinding(MODKEY_SHIFT
, XK_KP_Right
, KF_MVRG_6
, NULL
);
6301 setkeybinding(MODKEY_SHIFT
, XK_KP_Home
, KF_MVRG_7
, NULL
);
6302 setkeybinding(MODKEY_SHIFT
, XK_KP_Up
, KF_MVRG_8
, NULL
);
6303 setkeybinding(MODKEY_SHIFT
, XK_KP_Prior
, KF_MVRG_9
, NULL
);
6304 setkeybinding(MODKEY_SHIFT
, XK_1
, KF_MVWS_1
, NULL
);
6305 setkeybinding(MODKEY_SHIFT
, XK_2
, KF_MVWS_2
, NULL
);
6306 setkeybinding(MODKEY_SHIFT
, XK_3
, KF_MVWS_3
, NULL
);
6307 setkeybinding(MODKEY_SHIFT
, XK_4
, KF_MVWS_4
, NULL
);
6308 setkeybinding(MODKEY_SHIFT
, XK_5
, KF_MVWS_5
, NULL
);
6309 setkeybinding(MODKEY_SHIFT
, XK_6
, KF_MVWS_6
, NULL
);
6310 setkeybinding(MODKEY_SHIFT
, XK_7
, KF_MVWS_7
, NULL
);
6311 setkeybinding(MODKEY_SHIFT
, XK_8
, KF_MVWS_8
, NULL
);
6312 setkeybinding(MODKEY_SHIFT
, XK_9
, KF_MVWS_9
, NULL
);
6313 setkeybinding(MODKEY_SHIFT
, XK_0
, KF_MVWS_10
, NULL
);
6314 setkeybinding(MODKEY_SHIFT
, XK_F1
, KF_MVWS_11
, NULL
);
6315 setkeybinding(MODKEY_SHIFT
, XK_F2
, KF_MVWS_12
, NULL
);
6316 setkeybinding(MODKEY_SHIFT
, XK_F3
, KF_MVWS_13
, NULL
);
6317 setkeybinding(MODKEY_SHIFT
, XK_F4
, KF_MVWS_14
, NULL
);
6318 setkeybinding(MODKEY_SHIFT
, XK_F5
, KF_MVWS_15
, NULL
);
6319 setkeybinding(MODKEY_SHIFT
, XK_F6
, KF_MVWS_16
, NULL
);
6320 setkeybinding(MODKEY_SHIFT
, XK_F7
, KF_MVWS_17
, NULL
);
6321 setkeybinding(MODKEY_SHIFT
, XK_F8
, KF_MVWS_18
, NULL
);
6322 setkeybinding(MODKEY_SHIFT
, XK_F9
, KF_MVWS_19
, NULL
);
6323 setkeybinding(MODKEY_SHIFT
, XK_F10
, KF_MVWS_20
, NULL
);
6324 setkeybinding(MODKEY_SHIFT
, XK_F11
, KF_MVWS_21
, NULL
);
6325 setkeybinding(MODKEY_SHIFT
, XK_F12
, KF_MVWS_22
, NULL
);
6326 setkeybinding(MODKEY_SHIFT
, XK_slash
, KF_NAME_WORKSPACE
,NULL
);
6327 setkeybinding(MODKEY_SHIFT
, XK_q
, KF_QUIT
, NULL
);
6328 setkeybinding(MODKEY_SHIFT
, XK_r
, KF_RAISE_TOGGLE
,NULL
);
6329 setkeybinding(MODKEY
, XK_q
, KF_RESTART
, NULL
);
6330 setkeybinding(MODKEY
, XK_KP_End
, KF_RG_1
, NULL
);
6331 setkeybinding(MODKEY
, XK_KP_Down
, KF_RG_2
, NULL
);
6332 setkeybinding(MODKEY
, XK_KP_Next
, KF_RG_3
, NULL
);
6333 setkeybinding(MODKEY
, XK_KP_Left
, KF_RG_4
, NULL
);
6334 setkeybinding(MODKEY
, XK_KP_Begin
, KF_RG_5
, NULL
);
6335 setkeybinding(MODKEY
, XK_KP_Right
, KF_RG_6
, NULL
);
6336 setkeybinding(MODKEY
, XK_KP_Home
, KF_RG_7
, NULL
);
6337 setkeybinding(MODKEY
, XK_KP_Up
, KF_RG_8
, NULL
);
6338 setkeybinding(MODKEY
, XK_KP_Prior
, KF_RG_9
, NULL
);
6339 setkeybinding(MODKEY_SHIFT
, XK_Right
, KF_RG_NEXT
, NULL
);
6340 setkeybinding(MODKEY_SHIFT
, XK_Left
, KF_RG_PREV
, NULL
);
6341 setkeybinding(MODKEY
, XK_f
, KF_SEARCH_WIN
, NULL
);
6342 setkeybinding(MODKEY
, XK_slash
, KF_SEARCH_WORKSPACE
,NULL
);
6343 setkeybinding(MODKEY_SHIFT
, XK_i
, KF_SPAWN_CUSTOM
,"initscr");
6344 setkeybinding(MODKEY_SHIFT
, XK_Delete
, KF_SPAWN_CUSTOM
,"lock");
6345 setkeybinding(MODKEY
, XK_p
, KF_SPAWN_CUSTOM
,"menu");
6346 setkeybinding(MODKEY
, XK_s
, KF_SPAWN_CUSTOM
,"screenshot_all");
6347 setkeybinding(MODKEY_SHIFT
, XK_s
, KF_SPAWN_CUSTOM
,"screenshot_wind");
6348 setkeybinding(MODKEY_SHIFT
, XK_Return
, KF_SPAWN_CUSTOM
,"term");
6349 setkeybinding(MODKEY_SHIFT
, XK_comma
, KF_STACK_INC
, NULL
);
6350 setkeybinding(MODKEY_SHIFT
, XK_period
, KF_STACK_DEC
, NULL
);
6351 setkeybinding(MODKEY_SHIFT
, XK_space
, KF_STACK_RESET
, NULL
);
6352 setkeybinding(MODKEY
, XK_Return
, KF_SWAP_MAIN
, NULL
);
6353 setkeybinding(MODKEY_SHIFT
, XK_j
, KF_SWAP_NEXT
, NULL
);
6354 setkeybinding(MODKEY_SHIFT
, XK_k
, KF_SWAP_PREV
, NULL
);
6355 setkeybinding(MODKEY_SHIFT
, XK_w
, KF_UNICONIFY
, NULL
);
6356 setkeybinding(MODKEY_SHIFT
, XK_v
, KF_VERSION
, NULL
);
6357 setkeybinding(MODKEY
, XK_equal
, KF_WIDTH_GROW
, NULL
);
6358 setkeybinding(MODKEY
, XK_minus
, KF_WIDTH_SHRINK
,NULL
);
6359 setkeybinding(MODKEY
, XK_x
, KF_WIND_DEL
, NULL
);
6360 setkeybinding(MODKEY_SHIFT
, XK_x
, KF_WIND_KILL
, NULL
);
6361 setkeybinding(MODKEY
, XK_1
, KF_WS_1
, NULL
);
6362 setkeybinding(MODKEY
, XK_2
, KF_WS_2
, NULL
);
6363 setkeybinding(MODKEY
, XK_3
, KF_WS_3
, NULL
);
6364 setkeybinding(MODKEY
, XK_4
, KF_WS_4
, NULL
);
6365 setkeybinding(MODKEY
, XK_5
, KF_WS_5
, NULL
);
6366 setkeybinding(MODKEY
, XK_6
, KF_WS_6
, NULL
);
6367 setkeybinding(MODKEY
, XK_7
, KF_WS_7
, NULL
);
6368 setkeybinding(MODKEY
, XK_8
, KF_WS_8
, NULL
);
6369 setkeybinding(MODKEY
, XK_9
, KF_WS_9
, NULL
);
6370 setkeybinding(MODKEY
, XK_0
, KF_WS_10
, NULL
);
6371 setkeybinding(MODKEY
, XK_F1
, KF_WS_11
, NULL
);
6372 setkeybinding(MODKEY
, XK_F2
, KF_WS_12
, NULL
);
6373 setkeybinding(MODKEY
, XK_F3
, KF_WS_13
, NULL
);
6374 setkeybinding(MODKEY
, XK_F4
, KF_WS_14
, NULL
);
6375 setkeybinding(MODKEY
, XK_F5
, KF_WS_15
, NULL
);
6376 setkeybinding(MODKEY
, XK_F6
, KF_WS_16
, NULL
);
6377 setkeybinding(MODKEY
, XK_F7
, KF_WS_17
, NULL
);
6378 setkeybinding(MODKEY
, XK_F8
, KF_WS_18
, NULL
);
6379 setkeybinding(MODKEY
, XK_F9
, KF_WS_19
, NULL
);
6380 setkeybinding(MODKEY
, XK_F10
, KF_WS_20
, NULL
);
6381 setkeybinding(MODKEY
, XK_F11
, KF_WS_21
, NULL
);
6382 setkeybinding(MODKEY
, XK_F12
, KF_WS_22
, NULL
);
6383 setkeybinding(MODKEY
, XK_Right
, KF_WS_NEXT
, NULL
);
6384 setkeybinding(MODKEY
, XK_Left
, KF_WS_PREV
, NULL
);
6385 setkeybinding(MODKEY
, XK_Up
, KF_WS_NEXT_ALL
, NULL
);
6386 setkeybinding(MODKEY
, XK_Down
, KF_WS_PREV_ALL
, NULL
);
6387 setkeybinding(MODKEY
, XK_a
, KF_WS_PRIOR
, NULL
);
6389 setkeybinding(MODKEY_SHIFT
, XK_d
, KF_DUMPWINS
, NULL
);
6399 while (RB_EMPTY(&keys
) == 0) {
6400 kp
= RB_ROOT(&keys
);
6406 setkeymapping(char *selector
, char *value
, int flags
)
6408 char keymapping_file
[PATH_MAX
];
6410 /* suppress unused warnings since vars are needed */
6414 DNPRINTF(SWM_D_KEY
, "setkeymapping: enter\n");
6415 if (value
[0] == '~')
6416 snprintf(keymapping_file
, sizeof keymapping_file
, "%s/%s",
6417 pwd
->pw_dir
, &value
[1]);
6419 strlcpy(keymapping_file
, value
, sizeof keymapping_file
);
6421 /* load new key bindings; if it fails, revert to default bindings */
6422 if (conf_load(keymapping_file
, SWM_CONF_KEYMAPPING
)) {
6426 DNPRINTF(SWM_D_KEY
, "setkeymapping: leave\n");
6431 updatenumlockmask(void)
6434 xcb_get_modifier_mapping_reply_t
*modmap_r
;
6435 xcb_keycode_t
*modmap
, kc
, *keycode
;
6439 modmap_r
= xcb_get_modifier_mapping_reply(conn
,
6440 xcb_get_modifier_mapping(conn
),
6443 modmap
= xcb_get_modifier_mapping_keycodes(modmap_r
);
6444 for (i
= 0; i
< 8; i
++) {
6445 for (j
= 0; j
< modmap_r
->keycodes_per_modifier
; j
++) {
6446 kc
= modmap
[i
* modmap_r
->keycodes_per_modifier
6448 keycode
= xcb_key_symbols_get_keycode(syms
,
6451 numlockmask
= (1 << i
);
6457 DNPRINTF(SWM_D_MISC
, "updatenumlockmask: %d\n", numlockmask
);
6464 int num_screens
, k
, j
;
6465 unsigned int modifiers
[4];
6466 xcb_keycode_t
*code
;
6468 DNPRINTF(SWM_D_MISC
, "grabkeys\n");
6469 updatenumlockmask();
6472 modifiers
[1] = numlockmask
;
6473 modifiers
[2] = XCB_MOD_MASK_LOCK
;
6474 modifiers
[3] = numlockmask
| XCB_MOD_MASK_LOCK
;
6476 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
6477 for (k
= 0; k
< num_screens
; k
++) {
6478 if (TAILQ_EMPTY(&screens
[k
].rl
))
6480 xcb_ungrab_key(conn
, XCB_GRAB_ANY
, screens
[k
].root
,
6482 RB_FOREACH(kp
, key_tree
, &keys
) {
6483 if ((code
= xcb_key_symbols_get_keycode(syms
,
6485 for (j
= 0; j
< LENGTH(modifiers
); j
++)
6486 xcb_grab_key(conn
, 1,
6488 kp
->mod
| modifiers
[j
],
6489 *code
, XCB_GRAB_MODE_SYNC
,
6490 XCB_GRAB_MODE_SYNC
);
6498 grabbuttons(struct ws_win
*win
)
6500 unsigned int modifiers
[4];
6503 DNPRINTF(SWM_D_MOUSE
, "grabbuttons: win 0x%x\n", win
->id
);
6504 updatenumlockmask();
6507 modifiers
[1] = numlockmask
;
6508 modifiers
[2] = XCB_MOD_MASK_LOCK
;
6509 modifiers
[3] = numlockmask
| XCB_MOD_MASK_LOCK
;
6511 for (i
= 0; i
< LENGTH(buttons
); i
++)
6512 if (buttons
[i
].action
== client_click
)
6513 for (j
= 0; j
< LENGTH(modifiers
); ++j
)
6514 xcb_grab_button(conn
, 0, win
->id
, BUTTONMASK
,
6515 XCB_GRAB_MODE_SYNC
, XCB_GRAB_MODE_ASYNC
,
6516 XCB_WINDOW_NONE
, XCB_CURSOR_NONE
,
6517 buttons
[i
].button
, buttons
[i
].mask
|
6521 const char *quirkname
[] = {
6522 "NONE", /* config string for "no value" */
6531 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
6532 #define SWM_Q_WS "\n|+ \t"
6534 parsequirks(char *qstr
, unsigned long *quirk
)
6544 while ((name
= strsep(&cp
, SWM_Q_WS
)) != NULL
) {
6546 cp
+= (long)strspn(cp
, SWM_Q_WS
);
6547 for (i
= 0; i
< LENGTH(quirkname
); i
++) {
6548 if (!strncasecmp(name
, quirkname
[i
], SWM_QUIRK_LEN
)) {
6549 DNPRINTF(SWM_D_QUIRK
,
6550 "parsequirks: %s\n", name
);
6555 *quirk
|= 1 << (i
-1);
6559 if (i
>= LENGTH(quirkname
)) {
6560 DNPRINTF(SWM_D_QUIRK
,
6561 "parsequirks: invalid quirk [%s]\n", name
);
6569 quirk_insert(const char *class, const char *name
, unsigned long quirk
)
6573 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: %s:%s [%lu]\n", class, name
,
6576 if ((qp
= malloc(sizeof *qp
)) == NULL
)
6577 err(1, "quirk_insert: malloc");
6578 if ((qp
->class = strdup(class)) == NULL
)
6579 err(1, "quirk_insert: strdup");
6580 if ((qp
->name
= strdup(name
)) == NULL
)
6581 err(1, "quirk_insert: strdup");
6584 TAILQ_INSERT_TAIL(&quirks
, qp
, entry
);
6586 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: leave\n");
6590 quirk_remove(struct quirk
*qp
)
6592 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: %s:%s [%lu]\n", qp
->class,
6593 qp
->name
, qp
->quirk
);
6595 TAILQ_REMOVE(&quirks
, qp
, entry
);
6600 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: leave\n");
6604 quirk_replace(struct quirk
*qp
, const char *class, const char *name
,
6605 unsigned long quirk
)
6607 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: %s:%s [%lu]\n", qp
->class,
6608 qp
->name
, qp
->quirk
);
6611 quirk_insert(class, name
, quirk
);
6613 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: leave\n");
6617 setquirk(const char *class, const char *name
, unsigned long quirk
)
6621 DNPRINTF(SWM_D_QUIRK
, "setquirk: enter %s:%s [%lu]\n", class, name
,
6624 /* Remove/replace existing quirk. */
6625 TAILQ_FOREACH(qp
, &quirks
, entry
) {
6626 if (!strcmp(qp
->class, class) && !strcmp(qp
->name
, name
)) {
6630 quirk_replace(qp
, class, name
, quirk
);
6631 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
6636 /* Only insert if quirk is not NONE. */
6638 quirk_insert(class, name
, quirk
);
6640 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
6644 setconfquirk(char *selector
, char *value
, int flags
)
6646 char *cp
, *class, *name
;
6650 /* suppress unused warning since var is needed */
6653 if (selector
== NULL
)
6655 if ((cp
= strchr(selector
, ':')) == NULL
)
6660 if ((retval
= parsequirks(value
, &qrks
)) == 0)
6661 setquirk(class, name
, qrks
);
6668 setquirk("MPlayer", "xv", SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
| SWM_Q_FOCUSPREV
);
6669 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT
);
6670 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ
);
6671 setquirk("Firefox", "Dialog", SWM_Q_FLOAT
);
6672 setquirk("Gimp", "gimp", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
6673 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ
);
6674 setquirk("xine", "Xine Window", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
6675 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
6676 setquirk("xine", "xine Panel", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
6677 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
6678 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN
| SWM_Q_FLOAT
);
6679 setquirk("pcb", "pcb", SWM_Q_FLOAT
);
6680 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
);
6683 /* conf file stuff */
6684 #define SWM_CONF_FILE "spectrwm.conf"
6685 #define SWM_CONF_FILE_OLD "scrotwm.conf"
6689 SWM_S_BAR_AT_BOTTOM
,
6690 SWM_S_BAR_BORDER_WIDTH
,
6693 SWM_S_BAR_ENABLED_WS
,
6698 SWM_S_CLOCK_ENABLED
,
6701 SWM_S_CYCLE_VISIBLE
,
6703 SWM_S_DISABLE_BORDER
,
6705 SWM_S_FOCUS_CLOSE_WRAP
,
6706 SWM_S_FOCUS_DEFAULT
,
6708 SWM_S_REGION_PADDING
,
6713 SWM_S_STACK_ENABLED
,
6716 SWM_S_TITLE_CLASS_ENABLED
,
6717 SWM_S_TITLE_NAME_ENABLED
,
6718 SWM_S_URGENT_ENABLED
,
6719 SWM_S_VERBOSE_LAYOUT
,
6720 SWM_S_WINDOW_NAME_ENABLED
,
6721 SWM_S_WORKSPACE_LIMIT
6725 setconfvalue(char *selector
, char *value
, int flags
)
6727 struct workspace
*ws
;
6728 int i
, ws_id
, num_screens
;
6731 /* suppress unused warning since var is needed */
6735 case SWM_S_BAR_ACTION
:
6737 if ((bar_argv
[0] = expand_tilde(value
)) == NULL
)
6738 err(1, "setconfvalue: bar_action");
6740 case SWM_S_BAR_AT_BOTTOM
:
6741 bar_at_bottom
= atoi(value
);
6743 case SWM_S_BAR_BORDER_WIDTH
:
6744 bar_border_width
= atoi(value
);
6745 if (bar_border_width
< 0)
6746 bar_border_width
= 0;
6748 case SWM_S_BAR_DELAY
:
6749 /* No longer needed; leave to not break old conf files. */
6751 case SWM_S_BAR_ENABLED
:
6752 bar_enabled
= atoi(value
);
6754 case SWM_S_BAR_ENABLED_WS
:
6755 ws_id
= atoi(selector
) - 1;
6756 if (ws_id
< 0 || ws_id
>= workspace_limit
)
6757 errx(1, "setconfvalue: bar_enabled_ws: invalid "
6758 "workspace %d.", ws_id
+ 1);
6760 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
6761 for (i
= 0; i
< num_screens
; i
++) {
6762 ws
= (struct workspace
*)&screens
[i
].ws
;
6763 ws
[ws_id
].bar_enabled
= atoi(value
);
6766 case SWM_S_BAR_FONT
:
6768 if (asprintf(&bar_fonts
, "%s,%s", value
, bar_fonts
) == -1)
6769 err(1, "setconfvalue: asprintf: failed to allocate "
6770 "memory for bar_fonts.");
6773 /* If already in xft mode, then we are done. */
6774 if (!bar_font_legacy
)
6777 /* If there are any non-XLFD entries, switch to Xft mode. */
6778 while ((b
= strsep(&value
, ",")) != NULL
) {
6782 bar_font_legacy
= 0;
6787 case SWM_S_BAR_FORMAT
:
6789 if ((bar_format
= strdup(value
)) == NULL
)
6790 err(1, "setconfvalue: bar_format");
6792 case SWM_S_BAR_JUSTIFY
:
6793 if (!strcmp(value
, "left"))
6794 bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
6795 else if (!strcmp(value
, "center"))
6796 bar_justify
= SWM_BAR_JUSTIFY_CENTER
;
6797 else if (!strcmp(value
, "right"))
6798 bar_justify
= SWM_BAR_JUSTIFY_RIGHT
;
6800 errx(1, "invalid bar_justify");
6802 case SWM_S_BORDER_WIDTH
:
6803 border_width
= atoi(value
);
6804 if (border_width
< 0)
6807 case SWM_S_CLOCK_ENABLED
:
6808 clock_enabled
= atoi(value
);
6810 case SWM_S_CLOCK_FORMAT
:
6811 #ifndef SWM_DENY_CLOCK_FORMAT
6813 if ((clock_format
= strdup(value
)) == NULL
)
6814 err(1, "setconfvalue: clock_format");
6817 case SWM_S_CYCLE_EMPTY
:
6818 cycle_empty
= atoi(value
);
6820 case SWM_S_CYCLE_VISIBLE
:
6821 cycle_visible
= atoi(value
);
6823 case SWM_S_DIALOG_RATIO
:
6824 dialog_ratio
= atof(value
);
6825 if (dialog_ratio
> 1.0 || dialog_ratio
<= .3)
6828 case SWM_S_DISABLE_BORDER
:
6829 disable_border
= atoi(value
);
6831 case SWM_S_FOCUS_CLOSE
:
6832 if (!strcmp(value
, "first"))
6833 focus_close
= SWM_STACK_BOTTOM
;
6834 else if (!strcmp(value
, "last"))
6835 focus_close
= SWM_STACK_TOP
;
6836 else if (!strcmp(value
, "next"))
6837 focus_close
= SWM_STACK_ABOVE
;
6838 else if (!strcmp(value
, "previous"))
6839 focus_close
= SWM_STACK_BELOW
;
6841 errx(1, "focus_close");
6843 case SWM_S_FOCUS_CLOSE_WRAP
:
6844 focus_close_wrap
= atoi(value
);
6846 case SWM_S_FOCUS_DEFAULT
:
6847 if (!strcmp(value
, "last"))
6848 focus_default
= SWM_STACK_TOP
;
6849 else if (!strcmp(value
, "first"))
6850 focus_default
= SWM_STACK_BOTTOM
;
6852 errx(1, "focus_default");
6854 case SWM_S_FOCUS_MODE
:
6855 if (!strcmp(value
, "default"))
6856 focus_mode
= SWM_FOCUS_DEFAULT
;
6857 else if (!strcmp(value
, "follow") ||
6858 !strcmp(value
, "follow_cursor"))
6859 focus_mode
= SWM_FOCUS_FOLLOW
;
6860 else if (!strcmp(value
, "manual"))
6861 focus_mode
= SWM_FOCUS_MANUAL
;
6863 errx(1, "focus_mode");
6865 case SWM_S_REGION_PADDING
:
6866 region_padding
= atoi(value
);
6867 if (region_padding
< 0)
6870 case SWM_S_SPAWN_ORDER
:
6871 if (!strcmp(value
, "first"))
6872 spawn_position
= SWM_STACK_BOTTOM
;
6873 else if (!strcmp(value
, "last"))
6874 spawn_position
= SWM_STACK_TOP
;
6875 else if (!strcmp(value
, "next"))
6876 spawn_position
= SWM_STACK_ABOVE
;
6877 else if (!strcmp(value
, "previous"))
6878 spawn_position
= SWM_STACK_BELOW
;
6880 errx(1, "spawn_position");
6882 case SWM_S_SPAWN_TERM
:
6883 setconfspawn("term", value
, 0);
6884 setconfspawn("spawn_term", value
, 0);
6887 /* No longer needed; leave to not break old conf files. */
6889 case SWM_S_SS_ENABLED
:
6890 /* No longer needed; leave to not break old conf files. */
6892 case SWM_S_STACK_ENABLED
:
6893 stack_enabled
= atoi(value
);
6895 case SWM_S_TERM_WIDTH
:
6896 term_width
= atoi(value
);
6900 case SWM_S_TILE_GAP
:
6901 tile_gap
= atoi(value
);
6905 case SWM_S_TITLE_CLASS_ENABLED
:
6906 title_class_enabled
= atoi(value
);
6908 case SWM_S_TITLE_NAME_ENABLED
:
6909 title_name_enabled
= atoi(value
);
6911 case SWM_S_URGENT_ENABLED
:
6912 urgent_enabled
= atoi(value
);
6914 case SWM_S_VERBOSE_LAYOUT
:
6915 verbose_layout
= atoi(value
);
6916 for (i
= 0; layouts
[i
].l_stack
!= NULL
; i
++) {
6918 layouts
[i
].l_string
= fancy_stacker
;
6920 layouts
[i
].l_string
= plain_stacker
;
6923 case SWM_S_WINDOW_NAME_ENABLED
:
6924 window_name_enabled
= atoi(value
);
6926 case SWM_S_WORKSPACE_LIMIT
:
6927 workspace_limit
= atoi(value
);
6928 if (workspace_limit
> SWM_WS_MAX
)
6929 workspace_limit
= SWM_WS_MAX
;
6930 else if (workspace_limit
< 1)
6931 workspace_limit
= 1;
6940 setconfmodkey(char *selector
, char *value
, int flags
)
6942 /* suppress unused warnings since vars are needed */
6946 if (!strncasecmp(value
, "Mod1", strlen("Mod1")))
6947 update_modkey(XCB_MOD_MASK_1
);
6948 else if (!strncasecmp(value
, "Mod2", strlen("Mod2")))
6949 update_modkey(XCB_MOD_MASK_2
);
6950 else if (!strncasecmp(value
, "Mod3", strlen("Mod3")))
6951 update_modkey(XCB_MOD_MASK_3
);
6952 else if (!strncasecmp(value
, "Mod4", strlen("Mod4")))
6953 update_modkey(XCB_MOD_MASK_4
);
6960 setconfcolor(char *selector
, char *value
, int flags
)
6962 setscreencolor(value
, ((selector
== NULL
)?-1:atoi(selector
)), flags
);
6967 setconfregion(char *selector
, char *value
, int flags
)
6969 /* suppress unused warnings since vars are needed */
6973 custom_region(value
);
6978 setautorun(char *selector
, char *value
, int flags
)
6988 /* suppress unused warnings since vars are needed */
6992 if (getenv("SWM_STARTED"))
6996 if (sscanf(value
, "ws[%d]:%1023c", &ws_id
, s
) != 2)
6997 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
6999 if (ws_id
< 0 || ws_id
>= workspace_limit
)
7000 errx(1, "autorun: invalid workspace %d", ws_id
+ 1);
7002 sp
= expand_tilde((char *)&s
);
7005 * This is a little intricate
7007 * If the pid already exists we simply reuse it because it means it was
7008 * used before AND not claimed by manage_window. We get away with
7009 * altering it in the parent after INSERT because this can not be a race
7012 while ((ap
= strsep(&sp
, " \t")) != NULL
) {
7015 DNPRINTF(SWM_D_SPAWN
, "setautorun: arg [%s]\n", ap
);
7017 if ((a
.argv
= realloc(a
.argv
, argc
* sizeof(char *))) == NULL
)
7018 err(1, "setautorun: realloc");
7019 a
.argv
[argc
- 1] = ap
;
7023 if ((a
.argv
= realloc(a
.argv
, (argc
+ 1) * sizeof(char *))) == NULL
)
7024 err(1, "setautorun: realloc");
7025 a
.argv
[argc
] = NULL
;
7027 if ((pid
= fork()) == 0) {
7028 spawn(ws_id
, &a
, 1);
7037 p
= calloc(1, sizeof *p
);
7040 TAILQ_INSERT_TAIL(&pidlist
, p
, entry
);
7050 setlayout(char *selector
, char *value
, int flags
)
7052 int ws_id
, i
, x
, mg
, ma
, si
, ar
, f
= 0;
7053 int st
= SWM_V_STACK
, num_screens
;
7055 struct workspace
*ws
;
7057 /* suppress unused warnings since vars are needed */
7061 if (getenv("SWM_STARTED"))
7065 if (sscanf(value
, "ws[%d]:%d:%d:%d:%d:%1023c",
7066 &ws_id
, &mg
, &ma
, &si
, &ar
, s
) != 6)
7067 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
7068 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
7071 if (ws_id
< 0 || ws_id
>= workspace_limit
)
7072 errx(1, "layout: invalid workspace %d", ws_id
+ 1);
7074 if (!strcasecmp(s
, "vertical"))
7076 else if (!strcasecmp(s
, "vertical_flip")) {
7079 } else if (!strcasecmp(s
, "horizontal"))
7081 else if (!strcasecmp(s
, "horizontal_flip")) {
7084 } else if (!strcasecmp(s
, "fullscreen"))
7087 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
7088 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
7091 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7092 for (i
= 0; i
< num_screens
; i
++) {
7093 ws
= (struct workspace
*)&screens
[i
].ws
;
7094 ws
[ws_id
].cur_layout
= &layouts
[st
];
7096 ws
[ws_id
].always_raise
= ar
;
7097 if (st
== SWM_MAX_STACK
)
7101 for (x
= 0; x
< abs(mg
); x
++) {
7102 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
7103 mg
>= 0 ? SWM_ARG_ID_MASTERGROW
:
7104 SWM_ARG_ID_MASTERSHRINK
);
7108 for (x
= 0; x
< abs(ma
); x
++) {
7109 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
7110 ma
>= 0 ? SWM_ARG_ID_MASTERADD
:
7111 SWM_ARG_ID_MASTERDEL
);
7115 for (x
= 0; x
< abs(si
); x
++) {
7116 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
7117 si
>= 0 ? SWM_ARG_ID_STACKINC
:
7118 SWM_ARG_ID_STACKDEC
);
7123 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
7124 SWM_ARG_ID_FLIPLAYOUT
);
7134 /* config options */
7135 struct config_option
{
7137 int (*func
)(char*, char*, int);
7140 struct config_option configopt
[] = {
7141 { "autorun", setautorun
, 0 },
7142 { "bar_action", setconfvalue
, SWM_S_BAR_ACTION
},
7143 { "bar_at_bottom", setconfvalue
, SWM_S_BAR_AT_BOTTOM
},
7144 { "bar_border", setconfcolor
, SWM_S_COLOR_BAR_BORDER
},
7145 { "bar_border_unfocus", setconfcolor
, SWM_S_COLOR_BAR_BORDER_UNFOCUS
},
7146 { "bar_border_width", setconfvalue
, SWM_S_BAR_BORDER_WIDTH
},
7147 { "bar_color", setconfcolor
, SWM_S_COLOR_BAR
},
7148 { "bar_delay", setconfvalue
, SWM_S_BAR_DELAY
},
7149 { "bar_enabled", setconfvalue
, SWM_S_BAR_ENABLED
},
7150 { "bar_enabled_ws", setconfvalue
, SWM_S_BAR_ENABLED_WS
},
7151 { "bar_font", setconfvalue
, SWM_S_BAR_FONT
},
7152 { "bar_font_color", setconfcolor
, SWM_S_COLOR_BAR_FONT
},
7153 { "bar_format", setconfvalue
, SWM_S_BAR_FORMAT
},
7154 { "bar_justify", setconfvalue
, SWM_S_BAR_JUSTIFY
},
7155 { "bind", setconfbinding
, 0 },
7156 { "border_width", setconfvalue
, SWM_S_BORDER_WIDTH
},
7157 { "clock_enabled", setconfvalue
, SWM_S_CLOCK_ENABLED
},
7158 { "clock_format", setconfvalue
, SWM_S_CLOCK_FORMAT
},
7159 { "color_focus", setconfcolor
, SWM_S_COLOR_FOCUS
},
7160 { "color_unfocus", setconfcolor
, SWM_S_COLOR_UNFOCUS
},
7161 { "cycle_empty", setconfvalue
, SWM_S_CYCLE_EMPTY
},
7162 { "cycle_visible", setconfvalue
, SWM_S_CYCLE_VISIBLE
},
7163 { "dialog_ratio", setconfvalue
, SWM_S_DIALOG_RATIO
},
7164 { "disable_border", setconfvalue
, SWM_S_DISABLE_BORDER
},
7165 { "focus_close", setconfvalue
, SWM_S_FOCUS_CLOSE
},
7166 { "focus_close_wrap", setconfvalue
, SWM_S_FOCUS_CLOSE_WRAP
},
7167 { "focus_default", setconfvalue
, SWM_S_FOCUS_DEFAULT
},
7168 { "focus_mode", setconfvalue
, SWM_S_FOCUS_MODE
},
7169 { "keyboard_mapping", setkeymapping
, 0 },
7170 { "layout", setlayout
, 0 },
7171 { "modkey", setconfmodkey
, 0 },
7172 { "program", setconfspawn
, 0 },
7173 { "quirk", setconfquirk
, 0 },
7174 { "region", setconfregion
, 0 },
7175 { "region_padding", setconfvalue
, SWM_S_REGION_PADDING
},
7176 { "screenshot_app", setconfvalue
, SWM_S_SS_APP
},
7177 { "screenshot_enabled", setconfvalue
, SWM_S_SS_ENABLED
},
7178 { "spawn_position", setconfvalue
, SWM_S_SPAWN_ORDER
},
7179 { "spawn_term", setconfvalue
, SWM_S_SPAWN_TERM
},
7180 { "stack_enabled", setconfvalue
, SWM_S_STACK_ENABLED
},
7181 { "term_width", setconfvalue
, SWM_S_TERM_WIDTH
},
7182 { "tile_gap", setconfvalue
, SWM_S_TILE_GAP
},
7183 { "title_class_enabled", setconfvalue
, SWM_S_TITLE_CLASS_ENABLED
},
7184 { "title_name_enabled", setconfvalue
, SWM_S_TITLE_NAME_ENABLED
},
7185 { "urgent_enabled", setconfvalue
, SWM_S_URGENT_ENABLED
},
7186 { "verbose_layout", setconfvalue
, SWM_S_VERBOSE_LAYOUT
},
7187 { "window_name_enabled", setconfvalue
, SWM_S_WINDOW_NAME_ENABLED
},
7188 { "workspace_limit", setconfvalue
, SWM_S_WORKSPACE_LIMIT
},
7192 _add_startup_exception(const char *fmt
, va_list ap
)
7194 if (vasprintf(&startup_exception
, fmt
, ap
) == -1)
7195 warn("%s: asprintf", __func__
);
7199 add_startup_exception(const char *fmt
, ...)
7205 if (startup_exception
)
7208 /* force bar to be enabled due to exception */
7212 _add_startup_exception(fmt
, ap
);
7217 conf_load(const char *filename
, int keymapping
)
7220 char *line
= NULL
, *cp
, *optsub
, *optval
;
7221 size_t linelen
, lineno
= 0;
7222 int wordlen
, i
, optidx
;
7223 struct config_option
*opt
= NULL
;
7225 DNPRINTF(SWM_D_CONF
, "conf_load: begin\n");
7227 if (filename
== NULL
) {
7228 warnx("conf_load: no filename");
7231 if ((config
= fopen(filename
, "r")) == NULL
) {
7232 warn("conf_load: fopen: %s", filename
);
7236 while (!feof(config
)) {
7240 if ((line
= fparseln(config
, &linelen
, &lineno
, NULL
, 0))
7243 err(1, "%s", filename
);
7248 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
7249 if (cp
[0] == '\0') {
7253 /* get config option */
7254 wordlen
= strcspn(cp
, "=[ \t\n");
7256 add_startup_exception("%s: line %zd: no option found",
7261 for (i
= 0; i
< LENGTH(configopt
); i
++) {
7262 opt
= &configopt
[i
];
7263 if (!strncasecmp(cp
, opt
->optname
, wordlen
) &&
7264 (int)strlen(opt
->optname
) == wordlen
) {
7270 add_startup_exception("%s: line %zd: unknown option "
7271 "%.*s", filename
, lineno
, wordlen
, cp
);
7274 if (keymapping
&& opt
&& strcmp(opt
->optname
, "bind")) {
7275 add_startup_exception("%s: line %zd: invalid option "
7276 "%.*s", filename
, lineno
, wordlen
, cp
);
7280 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
7282 /* from here on out we call goto invalid to continue */
7284 /* get [selector] if any */
7288 wordlen
= strcspn(cp
, "]");
7291 add_startup_exception("%s: line %zd: "
7292 "syntax error", filename
, lineno
);
7296 if (asprintf(&optsub
, "%.*s", wordlen
, cp
) ==
7298 add_startup_exception("%s: line %zd: "
7299 "unable to allocatememory for "
7300 "selector", filename
, lineno
);
7305 cp
+= strspn(cp
, "] \t\n"); /* eat trailing */
7307 cp
+= strspn(cp
, "= \t\n"); /* eat trailing */
7309 optval
= strdup(cp
);
7310 /* call function to deal with it all */
7311 if (configopt
[optidx
].func(optsub
, optval
,
7312 configopt
[optidx
].funcflags
) != 0) {
7313 add_startup_exception("%s: line %zd: invalid data for "
7314 "%s", filename
, lineno
, configopt
[optidx
].optname
);
7331 DNPRINTF(SWM_D_CONF
, "conf_load: end\n");
7337 set_child_transient(struct ws_win
*win
, xcb_window_t
*trans
)
7339 struct ws_win
*parent
, *w
;
7340 struct swm_region
*r
;
7341 struct workspace
*ws
;
7342 xcb_icccm_wm_hints_t wmh
;
7344 parent
= find_window(win
->transient
);
7346 parent
->focus_child
= win
;
7348 DNPRINTF(SWM_D_MISC
, "set_child_transient: parent doesn't exist"
7349 " for 0x%x trans 0x%x\n", win
->id
, win
->transient
);
7351 r
= root_to_region(win
->wa
->root
, SWM_CK_ALL
);
7353 /* parent doen't exist in our window list */
7354 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
7355 if (xcb_icccm_get_wm_hints_reply(conn
,
7356 xcb_icccm_get_wm_hints(conn
, w
->id
),
7358 warnx("can't get hints for 0x%x", w
->id
);
7362 if (win
->hints
.window_group
!= wmh
.window_group
)
7365 w
->focus_child
= win
;
7366 win
->transient
= w
->id
;
7368 DNPRINTF(SWM_D_MISC
, "set_child_transient: adjusting "
7369 "transient to 0x%x\n", win
->transient
);
7376 window_get_pid(xcb_window_t win
)
7381 xcb_get_property_cookie_t pc
;
7382 xcb_get_property_reply_t
*pr
;
7384 apid
= get_atom_from_string("_NET_WM_PID");
7385 if (apid
== XCB_ATOM_NONE
)
7388 pc
= xcb_get_property(conn
, 0, win
, apid
, XCB_ATOM_CARDINAL
, 0, 1);
7389 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
7392 if (pr
->type
!= XCB_ATOM_CARDINAL
) {
7397 if (pr
->type
== apid
&& pr
->format
== 32)
7398 ret
= *((pid_t
*)xcb_get_property_value(pr
));
7404 apid
= get_atom_from_string("_SWM_PID");
7405 pc
= xcb_get_property(conn
, 0, win
, apid
, XCB_ATOM_STRING
,
7407 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
7410 if (pr
->type
!= apid
) {
7415 ret
= (pid_t
)strtonum(xcb_get_property_value(pr
), 0, INT_MAX
, &errstr
);
7422 get_ws_idx(xcb_window_t id
)
7428 xcb_get_property_reply_t
*gpr
;
7430 gpr
= xcb_get_property_reply(conn
,
7431 xcb_get_property(conn
, 0, id
, a_swm_ws
,
7432 XCB_ATOM_STRING
, 0, SWM_PROPLEN
),
7437 proplen
= xcb_get_property_value_length(gpr
);
7439 prop
= malloc(proplen
+ 1);
7442 xcb_get_property_value(gpr
),
7444 prop
[proplen
] = '\0';
7451 DNPRINTF(SWM_D_PROP
, "get_ws_idx: _SWM_WS: %s\n", prop
);
7452 ws_idx
= (int)strtonum(prop
, 0, workspace_limit
- 1, &errstr
);
7454 DNPRINTF(SWM_D_PROP
, "get_ws_idx: window: #%s: %s",
7464 manage_window(xcb_window_t id
, uint16_t mapped
)
7466 xcb_window_t trans
= XCB_WINDOW_NONE
;
7467 struct ws_win
*win
, *ww
;
7469 char ws_idx_str
[SWM_PROPLEN
];
7470 struct swm_region
*r
;
7474 xcb_icccm_get_wm_protocols_reply_t wpr
;
7476 if ((win
= find_window(id
)) != NULL
) {
7477 DNPRINTF(SWM_D_MISC
, "manage_window: win 0x%x already "
7478 "managed; skipping.)\n", id
);
7479 return (win
); /* Already managed. */
7482 /* See if window is on the unmanaged list. */
7483 if ((win
= find_unmanaged_window(id
)) != NULL
) {
7484 DNPRINTF(SWM_D_MISC
, "manage_window: win 0x%x found on "
7485 "unmanaged list.\n", id
);
7486 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
7489 set_child_transient(win
, &trans
);
7493 DNPRINTF(SWM_D_MISC
, "manage_window: win 0x%x is new.\n", id
);
7496 /* Create and initialize ws_win object. */
7497 if ((win
= calloc(1, sizeof(struct ws_win
))) == NULL
)
7498 err(1, "manage_window: calloc: failed to allocate memory for "
7503 /* Get window geometry. */
7504 win
->wa
= xcb_get_geometry_reply(conn
,
7505 xcb_get_geometry(conn
, win
->id
),
7508 /* Figure out which region the window belongs to. */
7509 r
= root_to_region(win
->wa
->root
, SWM_CK_ALL
);
7511 /* Ignore window border if there is one. */
7512 WIDTH(win
) = win
->wa
->width
;
7513 HEIGHT(win
) = win
->wa
->height
;
7514 X(win
) = win
->wa
->x
+ win
->wa
->border_width
- border_width
;
7515 Y(win
) = win
->wa
->y
+ win
->wa
->border_width
- border_width
;
7517 win
->mapped
= mapped
;
7518 win
->floatmaxed
= 0;
7519 win
->ewmh_flags
= 0;
7520 win
->s
= r
->s
; /* this never changes */
7522 store_float_geom(win
, r
);
7524 /* Get WM_SIZE_HINTS. */
7525 xcb_icccm_get_wm_normal_hints_reply(conn
,
7526 xcb_icccm_get_wm_normal_hints(conn
, win
->id
),
7530 xcb_icccm_get_wm_hints_reply(conn
,
7531 xcb_icccm_get_wm_hints(conn
, win
->id
),
7534 /* Get WM_TRANSIENT_FOR; see if window is a transient. */
7535 xcb_icccm_get_wm_transient_for_reply(conn
,
7536 xcb_icccm_get_wm_transient_for(conn
, win
->id
),
7539 win
->transient
= trans
;
7540 set_child_transient(win
, &win
->transient
);
7543 /* Get supported protocols. */
7544 if (xcb_icccm_get_wm_protocols_reply(conn
,
7545 xcb_icccm_get_wm_protocols(conn
, win
->id
, a_prot
),
7547 for (i
= 0; i
< wpr
.atoms_len
; i
++) {
7548 if (wpr
.atoms
[i
] == a_takefocus
)
7549 win
->take_focus
= 1;
7550 if (wpr
.atoms
[i
] == a_delete
)
7551 win
->can_delete
= 1;
7553 xcb_icccm_get_wm_protocols_reply_wipe(&wpr
);
7556 win
->iconic
= get_swm_iconic(win
);
7558 /* Figure out which workspace the window belongs to. */
7559 if ((p
= find_pid(window_get_pid(win
->id
))) != NULL
) {
7560 win
->ws
= &r
->s
->ws
[p
->ws
];
7561 TAILQ_REMOVE(&pidlist
, p
, entry
);
7564 } else if ((ws_idx
= get_ws_idx(win
->id
)) != -1 &&
7566 /* _SWM_WS is set; use that. */
7567 win
->ws
= &r
->s
->ws
[ws_idx
];
7568 } else if (trans
&& (ww
= find_window(trans
)) != NULL
) {
7569 /* Launch transients in the same ws as parent. */
7575 /* Set the _SWM_WS atom so we can remember this after reincarnation. */
7576 if (snprintf(ws_idx_str
, SWM_PROPLEN
, "%d", win
->ws
->idx
) <
7578 DNPRINTF(SWM_D_PROP
, "manage_window: set _SWM_WS: %s\n",
7580 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
7581 a_swm_ws
, XCB_ATOM_STRING
, 8, strlen(ws_idx_str
),
7586 ewmh_autoquirk(win
);
7588 /* Determine initial quirks. */
7589 if (xcb_icccm_get_wm_class_reply(conn
,
7590 xcb_icccm_get_wm_class(conn
, win
->id
),
7592 DNPRINTF(SWM_D_CLASS
, "manage_window: class: %s, name: %s\n",
7593 win
->ch
.class_name
, win
->ch
.instance_name
);
7595 /* java is retarded so treat it special */
7596 if (strstr(win
->ch
.instance_name
, "sun-awt")) {
7597 DNPRINTF(SWM_D_CLASS
, "manage_window: java window "
7602 TAILQ_FOREACH(qp
, &quirks
, entry
) {
7603 if (!strcmp(win
->ch
.class_name
, qp
->class) &&
7604 !strcmp(win
->ch
.instance_name
, qp
->name
)) {
7605 DNPRINTF(SWM_D_CLASS
, "manage_window: on quirks"
7606 "list; mask: 0x%lx\n", qp
->quirk
);
7607 if (qp
->quirk
& SWM_Q_FLOAT
)
7609 win
->quirks
= qp
->quirk
;
7614 /* Alter window position if quirky */
7615 if (win
->quirks
& SWM_Q_ANYWHERE
)
7618 /* Reset font sizes (the bruteforce way; no default keybinding). */
7619 if (win
->quirks
& SWM_Q_XTERM_FONTADJ
) {
7620 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
7621 fake_keypress(win
, XK_KP_Subtract
, XCB_MOD_MASK_SHIFT
);
7622 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
7623 fake_keypress(win
, XK_KP_Add
, XCB_MOD_MASK_SHIFT
);
7626 /* Make sure window is positioned inside its region, if its active. */
7628 constrain_window(win
, win
->ws
->r
, 0);
7632 /* Select which X events to monitor and set border pixel color. */
7633 wa
[0] = win
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
;
7634 wa
[1] = XCB_EVENT_MASK_ENTER_WINDOW
| XCB_EVENT_MASK_PROPERTY_CHANGE
|
7635 XCB_EVENT_MASK_STRUCTURE_NOTIFY
;
7637 wa
[1] |= XCB_EVENT_MASK_LEAVE_WINDOW
| XCB_EVENT_MASK_FOCUS_CHANGE
;
7640 xcb_change_window_attributes(conn
, win
->id
, XCB_CW_BORDER_PIXEL
|
7641 XCB_CW_EVENT_MASK
, wa
);
7644 /* Figure out where to stack the window in the workspace. */
7645 if (trans
&& (ww
= find_window(trans
)))
7646 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, ww
, win
, entry
);
7647 else if (win
->ws
->focus
&& spawn_position
== SWM_STACK_ABOVE
)
7648 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, win
->ws
->focus
, win
,
7650 else if (win
->ws
->focus
&& spawn_position
== SWM_STACK_BELOW
)
7651 TAILQ_INSERT_BEFORE(win
->ws
->focus
, win
, entry
);
7652 else switch (spawn_position
) {
7655 case SWM_STACK_ABOVE
:
7656 TAILQ_INSERT_TAIL(&win
->ws
->winlist
, win
, entry
);
7658 case SWM_STACK_BOTTOM
:
7659 case SWM_STACK_BELOW
:
7660 TAILQ_INSERT_HEAD(&win
->ws
->winlist
, win
, entry
);
7663 /* Get initial _NET_WM_STATE */
7664 ewmh_get_win_state(win
);
7665 /* Set initial _NET_WM_ALLOWED_ACTIONS */
7666 ewmh_update_actions(win
);
7670 DNPRINTF(SWM_D_MISC
, "manage_window: done. window: 0x%x, (x,y) w x h: "
7671 "(%d,%d) %d x %d, ws: %d, iconic: %s, transient: 0x%x\n", win
->id
,
7672 X(win
), Y(win
), WIDTH(win
), HEIGHT(win
), win
->ws
->idx
,
7673 YESNO(win
->iconic
), win
->transient
);
7679 free_window(struct ws_win
*win
)
7681 DNPRINTF(SWM_D_MISC
, "free_window: window: 0x%x\n", win
->id
);
7686 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
7691 xcb_icccm_get_wm_class_reply_wipe(&win
->ch
);
7696 memset(win
, 0xff, sizeof *win
); /* XXX kill later */
7699 DNPRINTF(SWM_D_MISC
, "free_window: done.\n");
7703 unmanage_window(struct ws_win
*win
)
7705 struct ws_win
*parent
;
7710 DNPRINTF(SWM_D_MISC
, "unmanage_window: window: 0x%x\n", win
->id
);
7712 if (win
->transient
) {
7713 parent
= find_window(win
->transient
);
7715 parent
->focus_child
= NULL
;
7718 TAILQ_REMOVE(&win
->ws
->winlist
, win
, entry
);
7719 TAILQ_INSERT_TAIL(&win
->ws
->unmanagedlist
, win
, entry
);
7723 expose(xcb_expose_event_t
*e
)
7726 struct swm_region
*r
;
7728 DNPRINTF(SWM_D_EVENT
, "expose: window: 0x%x\n", e
->window
);
7730 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7731 for (i
= 0; i
< num_screens
; i
++)
7732 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
7733 if (e
->window
== WINID(r
->bar
))
7741 focusin(xcb_focus_in_event_t
*e
)
7743 DNPRINTF(SWM_D_EVENT
, "focusin: window: 0x%x, mode: %s(%u), "
7744 "detail: %s(%u)\n", e
->event
, get_notify_mode_label(e
->mode
),
7745 e
->mode
, get_notify_detail_label(e
->detail
), e
->detail
);
7749 focusout(xcb_focus_out_event_t
*e
)
7751 DNPRINTF(SWM_D_EVENT
, "focusout: window: 0x%x, mode: %s(%u), "
7752 "detail: %s(%u)\n", e
->event
, get_notify_mode_label(e
->mode
),
7753 e
->mode
, get_notify_detail_label(e
->detail
), e
->detail
);
7758 keypress(xcb_key_press_event_t
*e
)
7760 xcb_keysym_t keysym
;
7763 keysym
= xcb_key_press_lookup_keysym(syms
, e
, 0);
7765 DNPRINTF(SWM_D_EVENT
, "keypress: keysym: %u, win (x,y): 0x%x (%d,%d), "
7766 "detail: %u, time: %u, root (x,y): 0x%x (%d,%d), child: 0x%x, "
7767 "state: %u, same_screen: %s\n", keysym
, e
->event
, e
->event_x
,
7768 e
->event_y
, e
->detail
, e
->time
, e
->root
, e
->root_x
, e
->root_y
,
7769 e
->child
, e
->state
, YESNO(e
->same_screen
));
7771 if ((kp
= key_lookup(CLEANMASK(e
->state
), keysym
)) == NULL
)
7774 last_event_time
= e
->time
;
7776 if (kp
->funcid
== KF_SPAWN_CUSTOM
)
7777 spawn_custom(root_to_region(e
->root
, SWM_CK_ALL
),
7778 &(keyfuncs
[kp
->funcid
].args
), kp
->spawn_name
);
7779 else if (keyfuncs
[kp
->funcid
].func
)
7780 keyfuncs
[kp
->funcid
].func(root_to_region(e
->root
, SWM_CK_ALL
),
7781 &(keyfuncs
[kp
->funcid
].args
));
7784 /* Unfreeze grab events. */
7785 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
, e
->time
);
7788 DNPRINTF(SWM_D_EVENT
, "keypress: done.\n");
7792 buttonpress(xcb_button_press_event_t
*e
)
7794 struct ws_win
*win
= NULL
;
7795 struct swm_region
*r
, *old_r
;
7799 DNPRINTF(SWM_D_EVENT
, "buttonpress: win (x,y): 0x%x (%d,%d), "
7800 "detail: %u, time: %u, root (x,y): 0x%x (%d,%d), child: 0x%x, "
7801 "state: %u, same_screen: %s\n", e
->event
, e
->event_x
, e
->event_y
,
7802 e
->detail
, e
->time
, e
->root
, e
->root_x
, e
->root_y
, e
->child
,
7803 e
->state
, YESNO(e
->same_screen
));
7805 if (e
->event
== e
->root
) {
7806 if (e
->child
!= 0) {
7807 win
= find_window(e
->child
);
7808 /* Pass ButtonPress to window if it isn't managed. */
7812 /* Focus on empty region */
7813 /* If no windows on region if its empty. */
7814 r
= root_to_region(e
->root
, SWM_CK_POINTER
);
7816 DNPRINTF(SWM_D_EVENT
, "buttonpress: "
7817 "NULL region; ignoring.\n");
7821 if (TAILQ_EMPTY(&r
->ws
->winlist
)) {
7822 old_r
= root_to_region(e
->root
, SWM_CK_FOCUS
);
7823 if (old_r
&& old_r
!= r
)
7824 unfocus_win(old_r
->ws
->focus
);
7826 xcb_set_input_focus(conn
,
7827 XCB_INPUT_FOCUS_PARENT
, e
->root
, e
->time
);
7829 /* Clear bar since empty. */
7837 win
= find_window(e
->event
);
7843 last_event_time
= e
->time
;
7845 focus_win(get_focus_magic(win
));
7847 for (i
= 0; i
< LENGTH(buttons
); i
++)
7848 if (client_click
== buttons
[i
].action
&& buttons
[i
].func
&&
7849 buttons
[i
].button
== e
->detail
&&
7850 CLEANMASK(buttons
[i
].mask
) == CLEANMASK(e
->state
)) {
7851 buttons
[i
].func(win
, &buttons
[i
].args
);
7857 DNPRINTF(SWM_D_EVENT
, "buttonpress: passing to window.\n");
7858 /* Replay event to event window */
7859 xcb_allow_events(conn
, XCB_ALLOW_REPLAY_POINTER
, e
->time
);
7861 DNPRINTF(SWM_D_EVENT
, "buttonpress: handled.\n");
7862 /* Unfreeze grab events. */
7863 xcb_allow_events(conn
, XCB_ALLOW_SYNC_POINTER
, e
->time
);
7871 print_win_geom(xcb_window_t w
)
7873 xcb_get_geometry_reply_t
*wa
;
7875 wa
= xcb_get_geometry_reply(conn
, xcb_get_geometry(conn
, w
), NULL
);
7878 DNPRINTF(SWM_D_MISC
, "print_win_geom: window not found: 0x%x\n",
7883 DNPRINTF(SWM_D_MISC
, "print_win_geom: window: 0x%x, root: 0x%x, "
7884 "depth: %u, (x,y) w x h: (%d,%d) %d x %d, border: %d\n",
7885 w
, wa
->root
, wa
->depth
, wa
->x
, wa
->y
, wa
->width
, wa
->height
,
7894 get_stack_mode_name(uint8_t mode
)
7899 case XCB_STACK_MODE_ABOVE
:
7902 case XCB_STACK_MODE_BELOW
:
7905 case XCB_STACK_MODE_TOP_IF
:
7908 case XCB_STACK_MODE_BOTTOM_IF
:
7911 case XCB_STACK_MODE_OPPOSITE
:
7923 configurerequest(xcb_configure_request_event_t
*e
)
7926 struct swm_region
*r
= NULL
;
7929 uint32_t wc
[7] = {0};
7931 if ((win
= find_window(e
->window
)) == NULL
)
7932 if ((win
= find_unmanaged_window(e
->window
)) == NULL
)
7936 if (swm_debug
& SWM_D_EVENT
) {
7937 print_win_geom(e
->window
);
7939 DNPRINTF(SWM_D_EVENT
, "configurerequest: window: 0x%x, "
7940 "parent: 0x%x, new: %s, value_mask: %u { ", e
->window
,
7941 e
->parent
, YESNO(new), e
->value_mask
);
7942 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
)
7943 DPRINTF("X: %d ", e
->x
);
7944 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
)
7945 DPRINTF("Y: %d ", e
->y
);
7946 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
)
7947 DPRINTF("W: %u ", e
->width
);
7948 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
)
7949 DPRINTF("H: %u ", e
->height
);
7950 if (e
->value_mask
& XCB_CONFIG_WINDOW_BORDER_WIDTH
)
7951 DPRINTF("Border: %u ", e
->border_width
);
7952 if (e
->value_mask
& XCB_CONFIG_WINDOW_SIBLING
)
7953 DPRINTF("Sibling: 0x%x ", e
->sibling
);
7954 if (e
->value_mask
& XCB_CONFIG_WINDOW_STACK_MODE
)
7955 DPRINTF("StackMode: %s(%u) ",
7956 get_stack_mode_name(e
->stack_mode
), e
->stack_mode
);
7962 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
) {
7963 mask
|= XCB_CONFIG_WINDOW_X
;
7966 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
) {
7967 mask
|= XCB_CONFIG_WINDOW_Y
;
7970 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
) {
7971 mask
|= XCB_CONFIG_WINDOW_WIDTH
;
7974 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
) {
7975 mask
|= XCB_CONFIG_WINDOW_HEIGHT
;
7976 wc
[i
++] = e
->height
;
7978 if (e
->value_mask
& XCB_CONFIG_WINDOW_BORDER_WIDTH
) {
7979 mask
|= XCB_CONFIG_WINDOW_BORDER_WIDTH
;
7980 wc
[i
++] = e
->border_width
;
7982 if (e
->value_mask
& XCB_CONFIG_WINDOW_SIBLING
) {
7983 mask
|= XCB_CONFIG_WINDOW_SIBLING
;
7984 wc
[i
++] = e
->sibling
;
7986 if (e
->value_mask
& XCB_CONFIG_WINDOW_STACK_MODE
) {
7987 mask
|= XCB_CONFIG_WINDOW_STACK_MODE
;
7988 wc
[i
++] = e
->stack_mode
;
7992 xcb_configure_window(conn
, e
->window
, mask
, wc
);
7995 } else if ((!win
->manual
|| win
->quirks
& SWM_Q_ANYWHERE
) &&
7996 !(win
->ewmh_flags
& EWMH_F_FULLSCREEN
)) {
7999 else if (win
->ws
->old_r
)
8002 /* windows are centered unless ANYWHERE quirk is set. */
8003 if (win
->quirks
& SWM_Q_ANYWHERE
) {
8004 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
) {
8005 win
->g_float
.x
= e
->x
;
8007 win
->g_float
.x
-= X(r
);
8010 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
) {
8011 win
->g_float
.y
= e
->y
;
8013 win
->g_float
.y
-= Y(r
);
8017 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
)
8018 win
->g_float
.w
= e
->width
;
8020 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
)
8021 win
->g_float
.h
= e
->height
;
8023 win
->g_floatvalid
= 1;
8025 if (win
->floating
&& r
&& (win
->transient
||
8026 win
->ws
->cur_layout
!= &layouts
[SWM_MAX_STACK
])) {
8027 WIDTH(win
) = win
->g_float
.w
;
8028 HEIGHT(win
) = win
->g_float
.h
;
8031 stack_floater(win
, r
);
8043 DNPRINTF(SWM_D_EVENT
, "configurerequest: done.\n");
8047 configurenotify(xcb_configure_notify_event_t
*e
)
8051 DNPRINTF(SWM_D_EVENT
, "configurenotify: win 0x%x, event win: 0x%x, "
8052 "(x,y) WxH: (%d,%d) %ux%u, border: %u, above_sibling: 0x%x, "
8053 "override_redirect: %s\n", e
->window
, e
->event
, e
->x
, e
->y
,
8054 e
->width
, e
->height
, e
->border_width
, e
->above_sibling
,
8055 YESNO(e
->override_redirect
));
8057 win
= find_window(e
->window
);
8059 xcb_icccm_get_wm_normal_hints_reply(conn
,
8060 xcb_icccm_get_wm_normal_hints(conn
, win
->id
),
8063 if (font_adjusted
) {
8071 destroynotify(xcb_destroy_notify_event_t
*e
)
8075 DNPRINTF(SWM_D_EVENT
, "destroynotify: window: 0x%x\n", e
->window
);
8077 if ((win
= find_window(e
->window
)) == NULL
) {
8078 if ((win
= find_unmanaged_window(e
->window
)) == NULL
)
8084 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
8085 /* If we were focused, make sure we focus on something else. */
8086 if (win
== win
->ws
->focus
)
8087 win
->ws
->focus_pending
= get_focus_prev(win
);
8090 unmanage_window(win
);
8093 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
8094 if (win
->ws
->focus_pending
) {
8095 focus_win(win
->ws
->focus_pending
);
8096 win
->ws
->focus_pending
= NULL
;
8107 get_notify_detail_label(uint8_t detail
)
8112 case XCB_NOTIFY_DETAIL_ANCESTOR
:
8115 case XCB_NOTIFY_DETAIL_VIRTUAL
:
8118 case XCB_NOTIFY_DETAIL_INFERIOR
:
8121 case XCB_NOTIFY_DETAIL_NONLINEAR
:
8122 label
= "Nonlinear";
8124 case XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL
:
8125 label
= "NonlinearVirtual";
8127 case XCB_NOTIFY_DETAIL_POINTER
:
8130 case XCB_NOTIFY_DETAIL_POINTER_ROOT
:
8131 label
= "PointerRoot";
8133 case XCB_NOTIFY_DETAIL_NONE
:
8144 get_notify_mode_label(uint8_t mode
)
8149 case XCB_NOTIFY_MODE_NORMAL
:
8152 case XCB_NOTIFY_MODE_GRAB
:
8155 case XCB_NOTIFY_MODE_UNGRAB
:
8158 case XCB_NOTIFY_MODE_WHILE_GRABBED
:
8159 label
= "WhileGrabbed";
8170 enternotify(xcb_enter_notify_event_t
*e
)
8173 struct swm_region
*r
;
8175 DNPRINTF(SWM_D_FOCUS
, "enternotify: time: %u, win (x,y): 0x%x "
8176 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): 0x%x (%d,%d), "
8177 "child: 0x%x, same_screen_focus: %s, state: %d\n",
8178 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
8179 get_notify_mode_label(e
->mode
), e
->mode
,
8180 get_notify_detail_label(e
->detail
), e
->detail
,
8181 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
8182 YESNO(e
->same_screen_focus
), e
->state
);
8184 if (focus_mode
== SWM_FOCUS_MANUAL
&&
8185 e
->mode
== XCB_NOTIFY_MODE_NORMAL
) {
8186 DNPRINTF(SWM_D_EVENT
, "enternotify: manual focus; ignoring.\n");
8190 last_event_time
= e
->time
;
8192 if ((win
= find_window(e
->event
)) == NULL
) {
8193 if (e
->event
== e
->root
) {
8194 /* If no windows on pointer region, then focus root. */
8195 r
= root_to_region(e
->root
, SWM_CK_POINTER
);
8197 DNPRINTF(SWM_D_EVENT
, "enterntoify: "
8198 "NULL region; ignoring.\n");
8204 DNPRINTF(SWM_D_EVENT
, "enternotify: window is NULL; "
8209 focus_win(get_focus_magic(win
));
8217 leavenotify(xcb_leave_notify_event_t
*e
)
8219 DNPRINTF(SWM_D_FOCUS
, "leavenotify: time: %u, win (x,y): 0x%x "
8220 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): 0x%x (%d,%d), "
8221 "child: 0x%x, same_screen_focus: %s, state: %d\n",
8222 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
8223 get_notify_mode_label(e
->mode
), e
->mode
,
8224 get_notify_detail_label(e
->detail
), e
->detail
,
8225 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
8226 YESNO(e
->same_screen_focus
), e
->state
);
8231 mapnotify(xcb_map_notify_event_t
*e
)
8235 DNPRINTF(SWM_D_EVENT
, "mapnotify: window: 0x%x\n", e
->window
);
8237 if ((win
= find_window(e
->window
)) == NULL
)
8238 win
= manage_window(e
->window
, 1);
8241 set_win_state(win
, XCB_ICCCM_WM_STATE_NORMAL
);
8243 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
8244 if (win
->ws
->focus_pending
== win
) {
8246 win
->ws
->focus_pending
= NULL
;
8255 mappingnotify(xcb_mapping_notify_event_t
*e
)
8257 xcb_refresh_keyboard_mapping(syms
, e
);
8259 if (e
->request
== XCB_MAPPING_KEYBOARD
)
8264 maprequest(xcb_map_request_event_t
*e
)
8267 xcb_get_window_attributes_reply_t
*war
;
8269 DNPRINTF(SWM_D_EVENT
, "maprequest: win 0x%x\n",
8272 war
= xcb_get_window_attributes_reply(conn
,
8273 xcb_get_window_attributes(conn
, e
->window
),
8276 DNPRINTF(SWM_D_EVENT
, "maprequest: window lost.\n");
8280 if (war
->override_redirect
) {
8281 DNPRINTF(SWM_D_EVENT
, "maprequest: override_redirect; "
8286 win
= manage_window(e
->window
,
8287 (war
->map_state
== XCB_MAP_STATE_VIEWABLE
));
8289 /* The new window should get focus; prepare. */
8290 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
8291 win
->ws
->focus_pending
= get_focus_magic(win
);
8293 /* All windows need to be mapped if they are in the current workspace.*/
8297 /* Ignore EnterNotify to handle the mapnotify without interference. */
8298 if (focus_mode
== SWM_FOCUS_DEFAULT
)
8299 event_drain(XCB_ENTER_NOTIFY
);
8302 DNPRINTF(SWM_D_EVENT
, "maprequest: done.\n");
8306 motionnotify(xcb_motion_notify_event_t
*e
)
8308 struct swm_region
*r
;
8311 DNPRINTF(SWM_D_FOCUS
, "motionnotify: time: %u, win (x,y): 0x%x "
8312 "(%d,%d), detail: %s(%d), root (x,y): 0x%x (%d,%d), "
8313 "child: 0x%x, same_screen_focus: %s, state: %d\n",
8314 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
8315 get_notify_detail_label(e
->detail
), e
->detail
,
8316 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
8317 YESNO(e
->same_screen
), e
->state
);
8319 last_event_time
= e
->time
;
8321 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
8322 for (i
= 0; i
< num_screens
; i
++)
8323 if (screens
[i
].root
== e
->root
)
8326 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
8327 if (X(r
) <= e
->root_x
&& e
->root_x
< MAX_X(r
) &&
8328 Y(r
) <= e
->root_y
&& e
->root_y
< MAX_Y(r
))
8336 get_atom_name(xcb_atom_t atom
)
8341 * This should be disabled during most debugging since
8342 * xcb_get_* causes an xcb_flush.
8345 xcb_get_atom_name_reply_t
*r
;
8347 r
= xcb_get_atom_name_reply(conn
,
8348 xcb_get_atom_name(conn
, atom
),
8351 len
= xcb_get_atom_name_name_length(r
);
8353 name
= malloc(len
+ 1);
8355 memcpy(name
, xcb_get_atom_name_name(r
), len
);
8369 propertynotify(xcb_property_notify_event_t
*e
)
8375 name
= get_atom_name(e
->atom
);
8376 DNPRINTF(SWM_D_EVENT
, "propertynotify: window: 0x%x, atom: %s(%u), "
8377 "time: %#x, state: %u\n", e
->window
, name
, e
->atom
, e
->time
,
8381 win
= find_window(e
->window
);
8385 last_event_time
= e
->time
;
8387 if (e
->atom
== a_swm_iconic
) {
8388 if (e
->state
== XCB_PROPERTY_NEW_VALUE
) {
8389 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
8390 win
->ws
->focus_pending
= get_focus_prev(win
);
8397 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
8398 focus_win(win
->ws
->focus_pending
);
8399 win
->ws
->focus_pending
= NULL
;
8403 } else if (e
->state
== XCB_PROPERTY_DELETE
) {
8404 /* The window is no longer iconic, restack ws. */
8405 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
8406 win
->ws
->focus_pending
= get_focus_magic(win
);
8410 /* Flush EnterNotify for mapnotify, if needed. */
8413 } else if (e
->atom
== a_state
) {
8414 /* State just changed, make sure it gets focused if mapped. */
8415 if (e
->state
== XCB_PROPERTY_NEW_VALUE
) {
8416 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
8418 win
->ws
->focus_pending
== win
) {
8419 focus_win(win
->ws
->focus_pending
);
8420 win
->ws
->focus_pending
= NULL
;
8424 } else if (e
->atom
== XCB_ATOM_WM_CLASS
||
8425 e
->atom
== XCB_ATOM_WM_NAME
) {
8433 unmapnotify(xcb_unmap_notify_event_t
*e
)
8436 struct workspace
*ws
;
8438 DNPRINTF(SWM_D_EVENT
, "unmapnotify: window: 0x%x\n", e
->window
);
8440 /* If we aren't managing the window, then ignore. */
8441 win
= find_window(e
->window
);
8442 if (win
== NULL
|| win
->id
!= e
->window
)
8447 if (getstate(e
->window
) != XCB_ICCCM_WM_STATE_ICONIC
)
8448 set_win_state(win
, XCB_ICCCM_WM_STATE_ICONIC
);
8451 /* window unmapped itself */
8452 /* do unmap/unfocus/restack and unmanage */
8455 /* If win was focused, make sure to focus on something else. */
8456 if (win
== ws
->focus
) {
8457 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
8458 ws
->focus_pending
= get_focus_prev(win
);
8459 DNPRINTF(SWM_D_EVENT
, "unmapnotify: "
8460 "focus_pending: 0x%x\n",
8461 WINID(ws
->focus_pending
));
8467 unmanage_window(win
);
8472 if (focus_mode
== SWM_FOCUS_FOLLOW
) {
8474 focus_win(get_pointer_win(ws
->r
->s
->root
));
8476 if (ws
->focus_pending
) {
8477 focus_win(ws
->focus_pending
);
8478 ws
->focus_pending
= NULL
;
8483 if (getstate(e
->window
) == XCB_ICCCM_WM_STATE_NORMAL
)
8484 set_win_state(win
, XCB_ICCCM_WM_STATE_ICONIC
);
8491 visibilitynotify(xcb_visibility_notify_event_t
*e
)
8493 DNPRINTF(SWM_D_EVENT
, "visibilitynotify: window: 0x%x\n",
8499 clientmessage(xcb_client_message_event_t
*e
)
8502 xcb_map_request_event_t mre
;
8506 name
= get_atom_name(e
->type
);
8507 DNPRINTF(SWM_D_EVENT
, "clientmessage: window: 0x%x, atom: %s(%u)\n",
8508 e
->window
, name
, e
->type
);
8511 win
= find_window(e
->window
);
8514 if (e
->type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
8515 /* Manage the window with maprequest. */
8516 DNPRINTF(SWM_D_EVENT
, "clientmessage: request focus on "
8517 "unmanaged window.\n");
8518 mre
.window
= e
->window
;
8524 if (e
->type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
8525 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_ACTIVE_WINDOW\n");
8528 if (e
->type
== ewmh
[_NET_CLOSE_WINDOW
].atom
) {
8529 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_CLOSE_WINDOW\n");
8530 if (win
->can_delete
)
8531 client_msg(win
, a_delete
, 0);
8533 xcb_kill_client(conn
, win
->id
);
8535 if (e
->type
== ewmh
[_NET_MOVERESIZE_WINDOW
].atom
) {
8536 DNPRINTF(SWM_D_EVENT
,
8537 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
8538 if (win
->floating
) {
8539 if (e
->data
.data32
[0] & (1<<8)) /* x */
8540 X(win
) = e
->data
.data32
[1];
8541 if (e
->data
.data32
[0] & (1<<9)) /* y */
8542 Y(win
) = e
->data
.data32
[2];
8543 if (e
->data
.data32
[0] & (1<<10)) /* width */
8544 WIDTH(win
) = e
->data
.data32
[3];
8545 if (e
->data
.data32
[0] & (1<<11)) /* height */
8546 HEIGHT(win
) = e
->data
.data32
[4];
8551 /* TODO: Change stack sizes */
8552 /* notify no change was made. */
8553 config_win(win
, NULL
);
8556 if (e
->type
== ewmh
[_NET_WM_STATE
].atom
) {
8557 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_WM_STATE\n");
8558 ewmh_update_win_state(win
, e
->data
.data32
[1], e
->data
.data32
[0]);
8559 if (e
->data
.data32
[2])
8560 ewmh_update_win_state(win
, e
->data
.data32
[2],
8572 int errcode
= xcb_connection_has_error(conn
);
8573 #ifdef XCB_CONN_ERROR
8576 case XCB_CONN_ERROR
:
8577 s
= "Socket error, pipe error or other stream error.";
8579 case XCB_CONN_CLOSED_EXT_NOTSUPPORTED
:
8580 s
= "Extension not supported.";
8582 case XCB_CONN_CLOSED_MEM_INSUFFICIENT
:
8583 s
= "Insufficient memory.";
8585 case XCB_CONN_CLOSED_REQ_LEN_EXCEED
:
8586 s
= "Request length was exceeded.";
8588 case XCB_CONN_CLOSED_PARSE_ERR
:
8589 s
= "Error parsing display string.";
8592 s
= "Unknown error.";
8595 errx(errcode
, "X CONNECTION ERROR: %s", s
);
8598 errx(errcode
, "X CONNECTION ERROR");
8606 const uint32_t val
= XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT
|
8607 XCB_EVENT_MASK_ENTER_WINDOW
;
8609 xcb_void_cookie_t wac
;
8610 xcb_generic_error_t
*error
;
8612 /* this causes an error if some other window manager is running */
8613 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
8614 for (i
= 0; i
< num_screens
; i
++) {
8615 if ((sc
= get_screen(i
)) == NULL
)
8616 errx(1, "ERROR: can't get screen %d.", i
);
8617 DNPRINTF(SWM_D_INIT
, "enable_wm: screen %d, root: 0x%x\n",
8619 wac
= xcb_change_window_attributes_checked(conn
, sc
->root
,
8620 XCB_CW_EVENT_MASK
, &val
);
8621 if ((error
= xcb_request_check(conn
, wac
))) {
8622 DNPRINTF(SWM_D_INIT
, "enable_wm: error_code: %u\n",
8628 /* click to focus on empty region */
8629 xcb_grab_button(conn
, 1, sc
->root
, BUTTONMASK
,
8630 XCB_GRAB_MODE_SYNC
, XCB_GRAB_MODE_ASYNC
, XCB_WINDOW_NONE
,
8631 XCB_CURSOR_NONE
, XCB_BUTTON_INDEX_1
, XCB_BUTTON_MASK_ANY
);
8638 new_region(struct swm_screen
*s
, int x
, int y
, int w
, int h
)
8640 struct swm_region
*r
, *n
;
8641 struct workspace
*ws
= NULL
;
8645 DNPRINTF(SWM_D_MISC
, "new region: screen[%d]:%dx%d+%d+%d\n",
8646 s
->idx
, w
, h
, x
, y
);
8648 /* remove any conflicting regions */
8649 n
= TAILQ_FIRST(&s
->rl
);
8652 n
= TAILQ_NEXT(r
, entry
);
8653 if (X(r
) < (x
+ w
) && (X(r
) + WIDTH(r
)) > x
&&
8654 Y(r
) < (y
+ h
) && (Y(r
) + HEIGHT(r
)) > y
) {
8655 if (r
->ws
->r
!= NULL
)
8656 r
->ws
->old_r
= r
->ws
->r
;
8659 xcb_destroy_window(conn
, r
->id
);
8660 TAILQ_REMOVE(&s
->rl
, r
, entry
);
8661 TAILQ_INSERT_TAIL(&s
->orl
, r
, entry
);
8665 /* search old regions for one to reuse */
8667 /* size + location match */
8668 TAILQ_FOREACH(r
, &s
->orl
, entry
)
8669 if (X(r
) == x
&& Y(r
) == y
&&
8670 HEIGHT(r
) == h
&& WIDTH(r
) == w
)
8674 TAILQ_FOREACH(r
, &s
->orl
, entry
)
8675 if (HEIGHT(r
) == h
&& WIDTH(r
) == w
)
8679 TAILQ_REMOVE(&s
->orl
, r
, entry
);
8680 /* try to use old region's workspace */
8681 if (r
->ws
->r
== NULL
)
8684 if ((r
= calloc(1, sizeof(struct swm_region
))) == NULL
)
8685 err(1, "new_region: calloc: failed to allocate memory "
8688 /* if we don't have a workspace already, find one */
8690 for (i
= 0; i
< workspace_limit
; i
++)
8691 if (s
->ws
[i
].r
== NULL
) {
8698 errx(1, "new_region: no free workspaces");
8709 TAILQ_INSERT_TAIL(&s
->rl
, r
, entry
);
8711 /* Invisible region window to detect pointer events on empty regions. */
8712 r
->id
= xcb_generate_id(conn
);
8713 wa
[0] = XCB_EVENT_MASK_POINTER_MOTION
|
8714 XCB_EVENT_MASK_POINTER_MOTION_HINT
;
8716 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, r
->id
, r
->s
->root
,
8717 X(r
), Y(r
), WIDTH(r
), HEIGHT(r
), 0, XCB_WINDOW_CLASS_INPUT_ONLY
,
8718 XCB_COPY_FROM_PARENT
, XCB_CW_EVENT_MASK
, wa
);
8720 xcb_map_window(conn
, r
->id
);
8726 #ifdef SWM_XRR_HAS_CRTC
8729 #endif /* SWM_XRR_HAS_CRTC */
8730 struct swm_region
*r
;
8732 xcb_randr_get_screen_resources_current_cookie_t src
;
8733 xcb_randr_get_screen_resources_current_reply_t
*srr
;
8734 xcb_randr_get_crtc_info_cookie_t cic
;
8735 xcb_randr_get_crtc_info_reply_t
*cir
= NULL
;
8736 xcb_randr_crtc_t
*crtc
;
8737 xcb_screen_t
*screen
;
8739 DNPRINTF(SWM_D_MISC
, "scan_xrandr: screen: %d\n", i
);
8741 if ((screen
= get_screen(i
)) == NULL
)
8742 errx(1, "ERROR: can't get screen %d.", i
);
8744 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
8745 if (i
>= num_screens
)
8746 errx(1, "scan_xrandr: invalid screen");
8748 /* remove any old regions */
8749 while ((r
= TAILQ_FIRST(&screens
[i
].rl
)) != NULL
) {
8750 r
->ws
->old_r
= r
->ws
->r
= NULL
;
8752 xcb_destroy_window(conn
, r
->id
);
8753 TAILQ_REMOVE(&screens
[i
].rl
, r
, entry
);
8754 TAILQ_INSERT_TAIL(&screens
[i
].orl
, r
, entry
);
8756 if (r
->s
->r_focus
== r
)
8757 r
->s
->r_focus
= NULL
;
8761 /* map virtual screens onto physical screens */
8762 #ifdef SWM_XRR_HAS_CRTC
8763 if (xrandr_support
) {
8764 src
= xcb_randr_get_screen_resources_current(conn
,
8766 srr
= xcb_randr_get_screen_resources_current_reply(conn
, src
,
8769 new_region(&screens
[i
], 0, 0,
8770 screen
->width_in_pixels
,
8771 screen
->height_in_pixels
);
8774 ncrtc
= srr
->num_crtcs
;
8776 crtc
= xcb_randr_get_screen_resources_current_crtcs(srr
);
8777 for (c
= 0; c
< ncrtc
; c
++) {
8778 cic
= xcb_randr_get_crtc_info(conn
, crtc
[c
],
8780 cir
= xcb_randr_get_crtc_info_reply(conn
, cic
, NULL
);
8783 if (cir
->num_outputs
== 0) {
8789 new_region(&screens
[i
], 0, 0,
8790 screen
->width_in_pixels
,
8791 screen
->height_in_pixels
);
8793 new_region(&screens
[i
],
8794 cir
->x
, cir
->y
, cir
->width
, cir
->height
);
8799 #endif /* SWM_XRR_HAS_CRTC */
8801 /* If detection failed, create a single region that spans the screen. */
8802 if (TAILQ_EMPTY(&screens
[i
].rl
))
8803 new_region(&screens
[i
], 0, 0, screen
->width_in_pixels
,
8804 screen
->height_in_pixels
);
8807 DNPRINTF(SWM_D_MISC
, "scan_xrandr: done.\n");
8811 screenchange(xcb_randr_screen_change_notify_event_t
*e
)
8813 struct swm_region
*r
;
8816 DNPRINTF(SWM_D_EVENT
, "screenchange: root: 0x%x\n", e
->root
);
8818 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
8819 /* silly event doesn't include the screen index */
8820 for (i
= 0; i
< num_screens
; i
++)
8821 if (screens
[i
].root
== e
->root
)
8823 if (i
>= num_screens
)
8824 errx(1, "screenchange: screen not found");
8826 /* brute force for now, just re-enumerate the regions */
8830 print_win_geom(e
->root
);
8832 /* add bars to all regions */
8833 for (i
= 0; i
< num_screens
; i
++) {
8834 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
8837 if (screens
[0].r_focus
== NULL
) {
8838 /* Focus on first region. */
8839 r
= TAILQ_FIRST(&screens
[0].rl
);
8853 struct swm_region
*r
= NULL
;
8854 xcb_window_t
*wins
= NULL
, trans
;
8856 int i
, j
, num_screens
;
8857 uint16_t state
, manage
, mapped
;
8859 xcb_query_tree_cookie_t qtc
;
8860 xcb_query_tree_reply_t
*qtr
;
8861 xcb_get_window_attributes_cookie_t gac
;
8862 xcb_get_window_attributes_reply_t
*gar
;
8863 xcb_get_property_cookie_t pc
;
8865 DNPRINTF(SWM_D_INIT
, "grab_windows: begin\n");
8866 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
8867 for (i
= 0; i
< num_screens
; i
++) {
8868 qtc
= xcb_query_tree(conn
, screens
[i
].root
);
8869 qtr
= xcb_query_tree_reply(conn
, qtc
, NULL
);
8872 wins
= xcb_query_tree_children(qtr
);
8873 no
= xcb_query_tree_children_length(qtr
);
8874 /* attach windows to a region */
8875 /* normal windows */
8876 DNPRINTF(SWM_D_INIT
, "grab_windows: grab top level windows.\n");
8877 for (j
= 0; j
< no
; j
++) {
8878 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
8879 if (r
->id
== wins
[j
]) {
8880 DNPRINTF(SWM_D_INIT
, "grab_windows: "
8881 "skip %#x; region input window.\n",
8890 gac
= xcb_get_window_attributes(conn
, wins
[j
]);
8891 gar
= xcb_get_window_attributes_reply(conn
, gac
, NULL
);
8893 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
8894 "doesn't exist.\n", wins
[j
]);
8898 if (gar
->override_redirect
) {
8899 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
8900 "override_redirect set.\n", wins
[j
]);
8905 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
8906 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
8908 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
8909 "is transient for %#x.\n", wins
[j
], trans
);
8914 state
= getstate(wins
[j
]);
8915 manage
= state
!= XCB_ICCCM_WM_STATE_WITHDRAWN
;
8916 mapped
= gar
->map_state
!= XCB_MAP_STATE_UNMAPPED
;
8917 if (mapped
|| manage
)
8918 manage_window(wins
[j
], mapped
);
8921 /* transient windows */
8922 DNPRINTF(SWM_D_INIT
, "grab_windows: grab transient windows.\n");
8923 for (j
= 0; j
< no
; j
++) {
8924 gac
= xcb_get_window_attributes(conn
, wins
[j
]);
8925 gar
= xcb_get_window_attributes_reply(conn
, gac
, NULL
);
8927 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
8928 "doesn't exist.\n", wins
[j
]);
8932 if (gar
->override_redirect
) {
8933 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
8934 "override_redirect set.\n", wins
[j
]);
8939 state
= getstate(wins
[j
]);
8940 manage
= state
!= XCB_ICCCM_WM_STATE_WITHDRAWN
;
8941 mapped
= gar
->map_state
!= XCB_MAP_STATE_UNMAPPED
;
8942 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
8943 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
8944 &trans
, NULL
) && manage
)
8945 manage_window(wins
[j
], mapped
);
8950 DNPRINTF(SWM_D_INIT
, "grab_windows: done.\n");
8956 int i
, j
, k
, num_screens
;
8957 struct workspace
*ws
;
8958 uint32_t gcv
[1], wa
[1];
8959 const xcb_query_extension_reply_t
*qep
;
8960 xcb_screen_t
*screen
;
8961 xcb_randr_query_version_cookie_t c
;
8962 xcb_randr_query_version_reply_t
*r
;
8964 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
8965 if ((screens
= calloc(num_screens
,
8966 sizeof(struct swm_screen
))) == NULL
)
8967 err(1, "setup_screens: calloc: failed to allocate memory for "
8970 /* initial Xrandr setup */
8972 qep
= xcb_get_extension_data(conn
, &xcb_randr_id
);
8974 c
= xcb_randr_query_version(conn
, 1, 1);
8975 r
= xcb_randr_query_version_reply(conn
, c
, NULL
);
8977 if (r
->major_version
>= 1) {
8979 xrandr_eventbase
= qep
->first_event
;
8985 wa
[0] = cursors
[XC_LEFT_PTR
].cid
;
8987 /* map physical screens */
8988 for (i
= 0; i
< num_screens
; i
++) {
8989 DNPRINTF(SWM_D_WS
, "setup_screens: init screen: %d\n", i
);
8991 screens
[i
].r_focus
= NULL
;
8993 TAILQ_INIT(&screens
[i
].rl
);
8994 TAILQ_INIT(&screens
[i
].orl
);
8995 if ((screen
= get_screen(i
)) == NULL
)
8996 errx(1, "ERROR: can't get screen %d.", i
);
8997 screens
[i
].root
= screen
->root
;
8999 /* set default colors */
9000 setscreencolor("red", i
+ 1, SWM_S_COLOR_FOCUS
);
9001 setscreencolor("rgb:88/88/88", i
+ 1, SWM_S_COLOR_UNFOCUS
);
9002 setscreencolor("rgb:00/80/80", i
+ 1, SWM_S_COLOR_BAR_BORDER
);
9003 setscreencolor("rgb:00/40/40", i
+ 1,
9004 SWM_S_COLOR_BAR_BORDER_UNFOCUS
);
9005 setscreencolor("black", i
+ 1, SWM_S_COLOR_BAR
);
9006 setscreencolor("rgb:a0/a0/a0", i
+ 1, SWM_S_COLOR_BAR_FONT
);
9008 /* create graphics context on screen */
9009 screens
[i
].bar_gc
= xcb_generate_id(conn
);
9011 xcb_create_gc(conn
, screens
[i
].bar_gc
, screens
[i
].root
,
9012 XCB_GC_GRAPHICS_EXPOSURES
, gcv
);
9014 /* set default cursor */
9015 xcb_change_window_attributes(conn
, screens
[i
].root
,
9018 /* init all workspaces */
9019 /* XXX these should be dynamically allocated too */
9020 for (j
= 0; j
< SWM_WS_MAX
; j
++) {
9021 ws
= &screens
[i
].ws
[j
];
9024 ws
->bar_enabled
= 1;
9026 ws
->focus_prev
= NULL
;
9027 ws
->focus_pending
= NULL
;
9030 TAILQ_INIT(&ws
->winlist
);
9031 TAILQ_INIT(&ws
->unmanagedlist
);
9033 for (k
= 0; layouts
[k
].l_stack
!= NULL
; k
++)
9034 if (layouts
[k
].l_config
!= NULL
)
9035 layouts
[k
].l_config(ws
,
9036 SWM_ARG_ID_STACKINIT
);
9037 ws
->cur_layout
= &layouts
[0];
9038 ws
->cur_layout
->l_string(ws
);
9044 xcb_randr_select_input(conn
, screens
[i
].root
,
9045 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE
);
9052 if ((bar_fonts
= strdup(SWM_BAR_FONTS
)) == NULL
)
9053 err(1, "setup_globals: strdup: failed to allocate memory.");
9055 if ((clock_format
= strdup("%a %b %d %R %Z %Y")) == NULL
)
9056 err(1, "setup_globals: strdup: failed to allocate memory.");
9058 if ((syms
= xcb_key_symbols_alloc(conn
)) == NULL
)
9059 errx(1, "unable to allocate key symbols");
9061 a_state
= get_atom_from_string("WM_STATE");
9062 a_prot
= get_atom_from_string("WM_PROTOCOLS");
9063 a_delete
= get_atom_from_string("WM_DELETE_WINDOW");
9064 a_takefocus
= get_atom_from_string("WM_TAKE_FOCUS");
9065 a_wmname
= get_atom_from_string("WM_NAME");
9066 a_netwmname
= get_atom_from_string("_NET_WM_NAME");
9067 a_utf8_string
= get_atom_from_string("UTF8_STRING");
9068 a_string
= get_atom_from_string("STRING");
9069 a_swm_iconic
= get_atom_from_string("_SWM_ICONIC");
9070 a_swm_ws
= get_atom_from_string("_SWM_WS");
9077 xcb_atom_t netwmcheck
;
9078 xcb_window_t root
, win
;
9080 /* work around sun jdk bugs, code from wmname */
9081 netwmcheck
= get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
9083 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
9084 for (i
= 0; i
< num_screens
; i
++) {
9085 root
= screens
[i
].root
;
9087 win
= xcb_generate_id(conn
);
9088 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, win
, root
,
9089 0, 0, 1, 1, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
9090 XCB_COPY_FROM_PARENT
, 0, NULL
);
9092 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, root
,
9093 netwmcheck
, XCB_ATOM_WINDOW
, 32, 1, &win
);
9094 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
9095 netwmcheck
, XCB_ATOM_WINDOW
, 32, 1, &win
);
9096 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
9097 a_netwmname
, a_utf8_string
, 8, strlen("LG3D"), "LG3D");
9102 shutdown_cleanup(void)
9106 /* disable alarm because the following code may not be interrupted */
9108 if (signal(SIGALRM
, SIG_IGN
) == SIG_ERR
)
9109 err(1, "can't disable alarm");
9118 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
9119 for (i
= 0; i
< num_screens
; ++i
) {
9120 if (screens
[i
].bar_gc
!= 0)
9121 xcb_free_gc(conn
, screens
[i
].bar_gc
);
9122 if (!bar_font_legacy
)
9123 XftColorFree(display
, DefaultVisual(display
, i
),
9124 DefaultColormap(display
, i
), &bar_font_color
);
9127 if (bar_font_legacy
)
9128 XFreeFontSet(display
, bar_fs
);
9130 XftFontClose(display
, bar_font
);
9133 xcb_key_symbols_free(syms
);
9135 xcb_disconnect(conn
);
9139 event_error(xcb_generic_error_t
*e
)
9143 DNPRINTF(SWM_D_EVENT
, "event_error: %s(%u) from %s(%u), sequence: %u, "
9144 "resource_id: %u, minor_code: %u\n",
9145 xcb_event_get_error_label(e
->error_code
), e
->error_code
,
9146 xcb_event_get_request_label(e
->major_code
), e
->major_code
,
9147 e
->sequence
, e
->resource_id
, e
->minor_code
);
9151 event_handle(xcb_generic_event_t
*evt
)
9153 uint8_t type
= XCB_EVENT_RESPONSE_TYPE(evt
);
9155 DNPRINTF(SWM_D_EVENT
, "XCB Event: %s(%d), seq %u\n",
9156 xcb_event_get_label(XCB_EVENT_RESPONSE_TYPE(evt
)),
9157 XCB_EVENT_RESPONSE_TYPE(evt
), evt
->sequence
);
9160 #define EVENT(type, callback) case type: callback((void *)evt); return
9161 EVENT(0, event_error
);
9162 EVENT(XCB_BUTTON_PRESS
, buttonpress
);
9163 /*EVENT(XCB_BUTTON_RELEASE, buttonpress);*/
9164 /*EVENT(XCB_CIRCULATE_NOTIFY, );*/
9165 /*EVENT(XCB_CIRCULATE_REQUEST, );*/
9166 EVENT(XCB_CLIENT_MESSAGE
, clientmessage
);
9167 /*EVENT(XCB_COLORMAP_NOTIFY, );*/
9168 EVENT(XCB_CONFIGURE_NOTIFY
, configurenotify
);
9169 EVENT(XCB_CONFIGURE_REQUEST
, configurerequest
);
9170 /*EVENT(XCB_CREATE_NOTIFY, );*/
9171 EVENT(XCB_DESTROY_NOTIFY
, destroynotify
);
9172 EVENT(XCB_ENTER_NOTIFY
, enternotify
);
9173 EVENT(XCB_EXPOSE
, expose
);
9175 EVENT(XCB_FOCUS_IN
, focusin
);
9176 EVENT(XCB_FOCUS_OUT
, focusout
);
9178 /*EVENT(XCB_GRAPHICS_EXPOSURE, );*/
9179 /*EVENT(XCB_GRAVITY_NOTIFY, );*/
9180 EVENT(XCB_KEY_PRESS
, keypress
);
9181 /*EVENT(XCB_KEY_RELEASE, keypress);*/
9182 /*EVENT(XCB_KEYMAP_NOTIFY, );*/
9184 EVENT(XCB_LEAVE_NOTIFY
, leavenotify
);
9186 EVENT(XCB_MAP_NOTIFY
, mapnotify
);
9187 EVENT(XCB_MAP_REQUEST
, maprequest
);
9188 EVENT(XCB_MAPPING_NOTIFY
, mappingnotify
);
9189 EVENT(XCB_MOTION_NOTIFY
, motionnotify
);
9190 /*EVENT(XCB_NO_EXPOSURE, );*/
9191 EVENT(XCB_PROPERTY_NOTIFY
, propertynotify
);
9192 /*EVENT(XCB_REPARENT_NOTIFY, );*/
9193 /*EVENT(XCB_RESIZE_REQUEST, );*/
9194 /*EVENT(XCB_SELECTION_CLEAR, );*/
9195 /*EVENT(XCB_SELECTION_NOTIFY, );*/
9196 /*EVENT(XCB_SELECTION_REQUEST, );*/
9197 EVENT(XCB_UNMAP_NOTIFY
, unmapnotify
);
9198 /*EVENT(XCB_VISIBILITY_NOTIFY, visibilitynotify);*/
9201 if (type
- xrandr_eventbase
== XCB_RANDR_SCREEN_CHANGE_NOTIFY
)
9202 screenchange((void *)evt
);
9206 main(int argc
, char *argv
[])
9208 struct swm_region
*r
;
9209 char conf
[PATH_MAX
], *cfile
= NULL
;
9211 int xfd
, i
, num_screens
, startup
= 1;
9212 struct sigaction sact
;
9213 xcb_generic_event_t
*evt
;
9217 int stdin_ready
= 0;
9220 /* suppress unused warning since var is needed */
9223 time_started
= time(NULL
);
9226 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION
, buildstr
);
9227 if (!setlocale(LC_CTYPE
, "") || !setlocale(LC_TIME
, ""))
9228 warnx("no locale support");
9230 /* handle some signals */
9231 bzero(&sact
, sizeof(sact
));
9232 sigemptyset(&sact
.sa_mask
);
9234 sact
.sa_handler
= sighdlr
;
9235 sigaction(SIGINT
, &sact
, NULL
);
9236 sigaction(SIGQUIT
, &sact
, NULL
);
9237 sigaction(SIGTERM
, &sact
, NULL
);
9238 sigaction(SIGHUP
, &sact
, NULL
);
9240 sact
.sa_handler
= sighdlr
;
9241 sact
.sa_flags
= SA_NOCLDSTOP
;
9242 sigaction(SIGCHLD
, &sact
, NULL
);
9244 if (!(display
= XOpenDisplay(0)))
9245 errx(1, "can not open display");
9247 conn
= XGetXCBConnection(display
);
9248 if (xcb_connection_has_error(conn
))
9249 errx(1, "can not get XCB connection");
9251 XSetEventQueueOwner(display
, XCBOwnsEventQueue
);
9253 xcb_prefetch_extension_data(conn
, &xcb_randr_id
);
9254 xfd
= xcb_get_file_descriptor(conn
);
9256 /* look for local and global conf file */
9257 pwd
= getpwuid(getuid());
9259 errx(1, "invalid user: %d", getuid());
9261 xcb_grab_server(conn
);
9264 /* flush all events */
9265 while ((evt
= xcb_poll_for_event(conn
))) {
9266 if (XCB_EVENT_RESPONSE_TYPE(evt
) == 0)
9271 if (enable_wm() != 0)
9272 errx(1, "another window manager is currently running");
9274 /* Load Xcursors and/or cursorfont glyph cursors. */
9286 for (i
= 0; ; i
++) {
9291 snprintf(conf
, sizeof conf
, "%s/.%s",
9292 pwd
->pw_dir
, SWM_CONF_FILE
);
9296 snprintf(conf
, sizeof conf
, "/etc/%s",
9301 snprintf(conf
, sizeof conf
, "%s/.%s",
9302 pwd
->pw_dir
, SWM_CONF_FILE_OLD
);
9306 snprintf(conf
, sizeof conf
, "/etc/%s",
9313 if (strlen(conf
) && stat(conf
, &sb
) != -1)
9314 if (S_ISREG(sb
.st_mode
)) {
9321 /* load conf (if any) */
9323 conf_load(cfile
, SWM_CONF_DEFAULT
);
9326 /* set some values to work around bad programs */
9328 /* grab existing windows (before we build the bars) */
9331 if (getenv("SWM_STARTED") == NULL
)
9332 setenv("SWM_STARTED", "YES", 1);
9334 /* setup all bars */
9335 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
9336 for (i
= 0; i
< num_screens
; i
++)
9337 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
9344 xcb_ungrab_server(conn
);
9347 rd_max
= xfd
> STDIN_FILENO
? xfd
: STDIN_FILENO
;
9350 while ((evt
= xcb_poll_for_event(conn
))) {
9357 /* If just (re)started, set default focus if needed. */
9361 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
9362 r
= TAILQ_FIRST(&screens
[0].rl
);
9374 FD_SET(STDIN_FILENO
, &rd
);
9379 num_readable
= select(rd_max
+ 1, &rd
, NULL
, NULL
, &tv
);
9380 if (num_readable
== -1 && errno
!= EINTR
) {
9381 DNPRINTF(SWM_D_MISC
, "select failed");
9382 } else if (num_readable
> 0 && FD_ISSET(STDIN_FILENO
, &rd
)) {
9387 restart(NULL
, NULL
);
9397 if (bar_extra_update()) {