2 * Copyright (c) 2009-2012 Marco Peereboom <marco@peereboom.us>
3 * Copyright (c) 2009-2011 Ryan McBride <mcbride@countersiege.com>
4 * Copyright (c) 2009 Darrin Chandler <dwchandler@stilyagin.com>
5 * Copyright (c) 2009 Pierre-Yves Ritschard <pyr@spootnik.org>
6 * Copyright (c) 2010 Tuukka Kataja <stuge@xor.fi>
7 * Copyright (c) 2011 Jason L. Wright <jason@thought.net>
8 * Copyright (c) 2011-2012 Reginald Kennedy <rk@rejii.com>
9 * Copyright (c) 2011-2012 Lawrence Teo <lteo@lteo.net>
10 * Copyright (c) 2011-2012 Tiago Cunha <tcunha@gmx.com>
11 * Copyright (c) 2012 David Hill <dhill@mindcry.org>
13 * Permission to use, copy, modify, and distribute this software for any
14 * purpose with or without fee is hereby granted, provided that the above
15 * copyright notice and this permission notice appear in all copies.
17 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
18 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
20 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
21 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
22 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
23 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 * Much code and ideas taken from dwm under the following license:
27 * MIT/X Consortium License
29 * 2006-2008 Anselm R Garbe <garbeam at gmail dot com>
30 * 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
31 * 2006-2007 Jukka Salmi <jukka at salmi dot ch>
32 * 2007 Premysl Hruby <dfenze at gmail dot com>
33 * 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
34 * 2007 Christof Musik <christof at sendfax dot de>
35 * 2007-2008 Enno Gottox Boland <gottox at s01 dot de>
36 * 2007-2008 Peter Hartlich <sgkkr at hartlich dot com>
37 * 2008 Martin Hurton <martin dot hurton at gmail dot com>
39 * Permission is hereby granted, free of charge, to any person obtaining a
40 * copy of this software and associated documentation files (the "Software"),
41 * to deal in the Software without restriction, including without limitation
42 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
43 * and/or sell copies of the Software, and to permit persons to whom the
44 * Software is furnished to do so, subject to the following conditions:
46 * The above copyright notice and this permission notice shall be included in
47 * all copies or substantial portions of the Software.
49 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
50 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
51 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
52 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
53 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
54 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
55 * DEALINGS IN THE SOFTWARE.
59 #include <sys/types.h>
63 #include <sys/queue.h>
64 #include <sys/param.h>
65 #include <sys/select.h>
66 #if defined(__linux__)
68 #elif defined(__OpenBSD__)
70 #elif defined(__FreeBSD__)
91 #include <X11/cursorfont.h>
92 #include <X11/extensions/Xrandr.h>
93 #include <X11/Xcursor/Xcursor.h>
94 #include <X11/Xft/Xft.h>
95 #include <X11/Xlib-xcb.h>
96 #include <xcb/xcb_atom.h>
97 #include <xcb/xcb_aux.h>
98 #include <xcb/xcb_event.h>
99 #include <xcb/xcb_icccm.h>
100 #include <xcb/xcb_keysyms.h>
101 #include <xcb/xtest.h>
102 #include <xcb/randr.h>
110 #ifdef SPECTRWM_BUILDSTR
111 static const char *buildstr
= SPECTRWM_BUILDSTR
;
113 static const char *buildstr
= SPECTRWM_VERSION
;
116 #if !defined(__CYGWIN__) /* cygwin chokes on xrandr stuff */
118 # error XRandR versions less than 1.0 are not supported
121 # if RANDR_MAJOR >= 1
122 # if RANDR_MINOR >= 2
123 # define SWM_XRR_HAS_CRTC
126 #endif /* __CYGWIN__ */
128 #ifndef XCB_ICCCM_NUM_WM_HINTS_ELEMENTS
129 #define XCB_ICCCM_SIZE_HINT_P_MIN_SIZE XCB_SIZE_HINT_P_MIN_SIZE
130 #define XCB_ICCCM_SIZE_HINT_P_MAX_SIZE XCB_SIZE_HINT_P_MAX_SIZE
131 #define XCB_ICCCM_SIZE_HINT_P_RESIZE_INC XCB_SIZE_HINT_P_RESIZE_INC
132 #define XCB_ICCCM_WM_HINT_INPUT XCB_WM_HINT_INPUT
133 #define XCB_ICCCM_WM_HINT_X_URGENCY XCB_WM_HINT_X_URGENCY
134 #define XCB_ICCCM_WM_STATE_ICONIC XCB_WM_STATE_ICONIC
135 #define XCB_ICCCM_WM_STATE_WITHDRAWN XCB_WM_STATE_WITHDRAWN
136 #define XCB_ICCCM_WM_STATE_NORMAL XCB_WM_STATE_NORMAL
137 #define xcb_icccm_get_text_property_reply_t xcb_get_text_property_reply_t
138 #define xcb_icccm_get_text_property_reply_wipe xcb_get_text_property_reply_wipe
139 #define xcb_icccm_get_wm_class xcb_get_wm_class
140 #define xcb_icccm_get_wm_class_reply xcb_get_wm_class_reply
141 #define xcb_icccm_get_wm_class_reply_t xcb_get_wm_class_reply_t
142 #define xcb_icccm_get_wm_class_reply_wipe xcb_get_wm_class_reply_wipe
143 #define xcb_icccm_get_wm_hints xcb_get_wm_hints
144 #define xcb_icccm_get_wm_hints_reply xcb_get_wm_hints_reply
145 #define xcb_icccm_get_wm_name xcb_get_wm_name
146 #define xcb_icccm_get_wm_name_reply xcb_get_wm_name_reply
147 #define xcb_icccm_get_wm_normal_hints xcb_get_wm_normal_hints
148 #define xcb_icccm_get_wm_normal_hints_reply xcb_get_wm_normal_hints_reply
149 #define xcb_icccm_get_wm_protocols xcb_get_wm_protocols
150 #define xcb_icccm_get_wm_protocols_reply xcb_get_wm_protocols_reply
151 #define xcb_icccm_get_wm_protocols_reply_t xcb_get_wm_protocols_reply_t
152 #define xcb_icccm_get_wm_protocols_reply_wipe xcb_get_wm_protocols_reply_wipe
153 #define xcb_icccm_get_wm_transient_for xcb_get_wm_transient_for
154 #define xcb_icccm_get_wm_transient_for_reply xcb_get_wm_transient_for_reply
155 #define xcb_icccm_wm_hints_t xcb_wm_hints_t
158 /*#define SWM_DEBUG*/
160 #define DPRINTF(x...) do { \
162 fprintf(stderr, x); \
164 #define DNPRINTF(n,x...) do { \
165 if (swm_debug & n) { \
166 fprintf(stderr, "%ld ", (long)(time(NULL) - time_started)); \
167 fprintf(stderr, x); \
170 #define SWM_D_MISC 0x0001
171 #define SWM_D_EVENT 0x0002
172 #define SWM_D_WS 0x0004
173 #define SWM_D_FOCUS 0x0008
174 #define SWM_D_MOVE 0x0010
175 #define SWM_D_STACK 0x0020
176 #define SWM_D_MOUSE 0x0040
177 #define SWM_D_PROP 0x0080
178 #define SWM_D_CLASS 0x0100
179 #define SWM_D_KEY 0x0200
180 #define SWM_D_QUIRK 0x0400
181 #define SWM_D_SPAWN 0x0800
182 #define SWM_D_EVENTQ 0x1000
183 #define SWM_D_CONF 0x2000
184 #define SWM_D_BAR 0x4000
185 #define SWM_D_INIT 0x8000
187 u_int32_t swm_debug
= 0
206 #define DPRINTF(x...)
207 #define DNPRINTF(n,x...)
210 /* convert 8-bit to 16-bit */
211 #define RGB_8_TO_16(col) ((col) << 8) + (col)
213 #define PIXEL_TO_XRENDERCOLOR(px, xrc) \
214 xrc.red = RGB_8_TO_16((px) >> 16 & 0xff); \
215 xrc.green = RGB_8_TO_16((px) >> 8 & 0xff); \
216 xrc.blue = RGB_8_TO_16((px) & 0xff); \
219 #define LENGTH(x) (int)(sizeof (x) / sizeof (x)[0])
220 #define MODKEY XCB_MOD_MASK_1
221 #define CLEANMASK(mask) ((mask) & ~(numlockmask | XCB_MOD_MASK_LOCK))
222 #define BUTTONMASK (XCB_EVENT_MASK_BUTTON_PRESS | \
223 XCB_EVENT_MASK_BUTTON_RELEASE)
224 #define MOUSEMASK (BUTTONMASK|XCB_EVENT_MASK_POINTER_MOTION)
225 #define SWM_PROPLEN (16)
226 #define SWM_FUNCNAME_LEN (32)
227 #define SWM_KEYS_LEN (255)
228 #define SWM_QUIRK_LEN (64)
229 #define X(r) (r)->g.x
230 #define Y(r) (r)->g.y
231 #define WIDTH(r) (r)->g.w
232 #define HEIGHT(r) (r)->g.h
233 #define BORDER(w) ((w)->bordered ? border_width : 0)
234 #define MAX_X(r) ((r)->g.x + (r)->g.w)
235 #define MAX_Y(r) ((r)->g.y + (r)->g.h)
236 #define SH_MIN(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE
237 #define SH_MIN_W(w) (w)->sh.min_width
238 #define SH_MIN_H(w) (w)->sh.min_height
239 #define SH_MAX(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MAX_SIZE
240 #define SH_MAX_W(w) (w)->sh.max_width
241 #define SH_MAX_H(w) (w)->sh.max_height
242 #define SH_INC(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_RESIZE_INC
243 #define SH_INC_W(w) (w)->sh.width_inc
244 #define SH_INC_H(w) (w)->sh.height_inc
245 #define SWM_MAX_FONT_STEPS (3)
246 #define WINID(w) ((w) ? (w)->id : 0)
247 #define YESNO(x) ((x) ? "yes" : "no")
249 #define SWM_FOCUS_DEFAULT (0)
250 #define SWM_FOCUS_FOLLOW (1)
251 #define SWM_FOCUS_MANUAL (2)
253 #define SWM_CK_NONE 0
254 #define SWM_CK_ALL 0x7
255 #define SWM_CK_FOCUS 0x1
256 #define SWM_CK_POINTER 0x2
257 #define SWM_CK_FALLBACK 0x4
259 #define SWM_CONF_DEFAULT (0)
260 #define SWM_CONF_KEYMAPPING (1)
263 #define SWM_LIB "/usr/local/lib/libswmhack.so"
270 xcb_atom_t a_takefocus
;
272 xcb_atom_t a_netwmname
;
273 xcb_atom_t a_utf8_string
;
275 xcb_atom_t a_swm_iconic
;
277 volatile sig_atomic_t running
= 1;
278 volatile sig_atomic_t restart_wm
= 0;
279 xcb_timestamp_t last_event_time
= 0;
284 int xrandr_eventbase
;
285 unsigned int numlockmask
= 0;
288 xcb_connection_t
*conn
;
289 xcb_key_symbols_t
*syms
;
292 int cycle_visible
= 0;
294 int font_adjusted
= 0;
295 unsigned int mod_key
= MODKEY
;
298 struct swm_region
*search_r
;
299 int select_list_pipe
[2];
300 int select_resp_pipe
[2];
302 volatile sig_atomic_t search_resp
;
303 int search_resp_action
;
305 struct search_window
{
306 TAILQ_ENTRY(search_window
) entry
;
310 xcb_window_t indicator
;
312 TAILQ_HEAD(search_winlist
, search_window
);
313 struct search_winlist search_wl
;
318 SWM_SEARCH_UNICONIFY
,
319 SWM_SEARCH_NAME_WORKSPACE
,
320 SWM_SEARCH_SEARCH_WORKSPACE
,
321 SWM_SEARCH_SEARCH_WINDOW
324 #define SWM_STACK_TOP (0)
325 #define SWM_STACK_BOTTOM (1)
326 #define SWM_STACK_ABOVE (2)
327 #define SWM_STACK_BELOW (3)
330 double dialog_ratio
= 0.6;
332 #define SWM_BAR_MAX (256)
333 #define SWM_BAR_JUSTIFY_LEFT (0)
334 #define SWM_BAR_JUSTIFY_CENTER (1)
335 #define SWM_BAR_JUSTIFY_RIGHT (2)
336 #define SWM_BAR_OFFSET (4)
337 #define SWM_BAR_FONTS "-*-terminus-medium-*-*-*-12-*-*-*-*-*-*-*," \
338 "-*-profont-*-*-*-*-12-*-*-*-*-*-*-*," \
339 "-*-times-medium-r-*-*-12-*-*-*-*-*-*-*," \
340 "-misc-fixed-medium-r-*-*-12-*-*-*-*-*-*-*," \
341 "-*-*-*-r-*-*-*-*-*-*-*-*-*-*"
343 #ifdef X_HAVE_UTF8_STRING
344 #define DRAWSTRING(x...) Xutf8DrawString(x)
346 #define DRAWSTRING(x...) XmbDrawString(x)
349 char *bar_argv
[] = { NULL
, NULL
};
351 char bar_ext
[SWM_BAR_MAX
];
352 char bar_vertext
[SWM_BAR_MAX
];
354 sig_atomic_t bar_alarm
= 0;
357 int bar_border_width
= 1;
358 int bar_at_bottom
= 0;
360 int bar_extra_running
= 0;
363 int bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
364 char *bar_format
= NULL
;
365 int stack_enabled
= 1;
366 int clock_enabled
= 1;
367 int urgent_enabled
= 0;
368 char *clock_format
= NULL
;
369 int title_name_enabled
= 0;
370 int title_class_enabled
= 0;
371 int window_name_enabled
= 0;
372 int focus_mode
= SWM_FOCUS_DEFAULT
;
373 int focus_close
= SWM_STACK_BELOW
;
374 int focus_close_wrap
= 1;
375 int focus_default
= SWM_STACK_TOP
;
376 int spawn_position
= SWM_STACK_TOP
;
377 int disable_border
= 0;
378 int border_width
= 1;
379 int verbose_layout
= 0;
383 XFontSetExtents
*bar_fs_extents
;
385 int bar_font_legacy
= 1;
387 XftColor bar_font_color
;
390 /* layout manager data */
391 struct swm_geometry
{
404 struct swm_geometry g
;
407 /* virtual "screens" */
409 TAILQ_ENTRY(swm_region
) entry
;
410 struct swm_geometry g
;
411 struct workspace
*ws
; /* current workspace on this region */
412 struct workspace
*ws_prior
; /* prior workspace on this region */
413 struct swm_screen
*s
; /* screen idx */
416 TAILQ_HEAD(swm_region_list
, swm_region
);
419 TAILQ_ENTRY(ws_win
) entry
;
421 xcb_window_t transient
;
422 struct ws_win
*focus_child
; /* focus on child transient */
423 struct swm_geometry g
; /* current geometry */
424 struct swm_geometry g_float
; /* region coordinates */
425 int g_floatvalid
; /* g_float geometry validity */
426 int floatmaxed
; /* whether maxed by max_stack */
432 unsigned int ewmh_flags
;
433 int font_size_boundary
[SWM_MAX_FONT_STEPS
];
439 unsigned long quirks
;
440 struct workspace
*ws
; /* always valid */
441 struct swm_screen
*s
; /* always valid, never changes */
442 xcb_get_geometry_reply_t
*wa
;
444 xcb_icccm_get_wm_class_reply_t ch
;
445 xcb_icccm_wm_hints_t hints
;
447 TAILQ_HEAD(ws_win_list
, ws_win
);
451 TAILQ_ENTRY(pid_e
) entry
;
455 TAILQ_HEAD(pid_list
, pid_e
);
456 struct pid_list pidlist
= TAILQ_HEAD_INITIALIZER(pidlist
);
458 /* layout handlers */
460 void vertical_config(struct workspace
*, int);
461 void vertical_stack(struct workspace
*, struct swm_geometry
*);
462 void horizontal_config(struct workspace
*, int);
463 void horizontal_stack(struct workspace
*, struct swm_geometry
*);
464 void max_stack(struct workspace
*, struct swm_geometry
*);
465 void plain_stacker(struct workspace
*);
466 void fancy_stacker(struct workspace
*);
469 void (*l_stack
)(struct workspace
*, struct swm_geometry
*);
470 void (*l_config
)(struct workspace
*, int);
472 #define SWM_L_FOCUSPREV (1<<0)
473 #define SWM_L_MAPONFOCUS (1<<1)
474 void (*l_string
)(struct workspace
*);
476 /* stack, configure */
477 { vertical_stack
, vertical_config
, 0, plain_stacker
},
478 { horizontal_stack
, horizontal_config
, 0, plain_stacker
},
480 SWM_L_MAPONFOCUS
| SWM_L_FOCUSPREV
, plain_stacker
},
481 { NULL
, NULL
, 0, NULL
},
484 /* position of max_stack mode in the layouts array, index into layouts! */
485 #define SWM_V_STACK (0)
486 #define SWM_H_STACK (1)
487 #define SWM_MAX_STACK (2)
489 #define SWM_H_SLICE (32)
490 #define SWM_V_SLICE (32)
492 /* define work spaces */
494 int idx
; /* workspace index */
495 char *name
; /* workspace name */
496 int always_raise
; /* raise windows on focus */
497 struct layout
*cur_layout
; /* current layout handlers */
498 struct ws_win
*focus
; /* may be NULL */
499 struct ws_win
*focus_prev
; /* may be NULL */
500 struct ws_win
*focus_pending
; /* may be NULL */
501 struct swm_region
*r
; /* may be NULL */
502 struct swm_region
*old_r
; /* may be NULL */
503 struct ws_win_list winlist
; /* list of windows in ws */
504 struct ws_win_list unmanagedlist
; /* list of dead windows in ws */
505 char stacker
[10]; /* display stacker and layout */
509 int horizontal_msize
;
511 int horizontal_stacks
;
522 SWM_S_COLOR_BAR_BORDER
,
523 SWM_S_COLOR_BAR_FONT
,
529 /* physical screen mapping */
530 #define SWM_WS_MAX (22) /* hard limit */
531 int workspace_limit
= 10; /* soft limit */
534 int idx
; /* screen index */
535 struct swm_region_list rl
; /* list of regions on this screen */
536 struct swm_region_list orl
; /* list of old regions */
538 struct workspace ws
[SWM_WS_MAX
];
544 } c
[SWM_S_COLOR_MAX
];
546 xcb_gcontext_t bar_gc
;
549 struct swm_screen
*screens
;
551 /* args to functions */
554 #define SWM_ARG_ID_FOCUSNEXT (0)
555 #define SWM_ARG_ID_FOCUSPREV (1)
556 #define SWM_ARG_ID_FOCUSMAIN (2)
557 #define SWM_ARG_ID_SWAPNEXT (10)
558 #define SWM_ARG_ID_SWAPPREV (11)
559 #define SWM_ARG_ID_SWAPMAIN (12)
560 #define SWM_ARG_ID_MOVELAST (13)
561 #define SWM_ARG_ID_MASTERSHRINK (20)
562 #define SWM_ARG_ID_MASTERGROW (21)
563 #define SWM_ARG_ID_MASTERADD (22)
564 #define SWM_ARG_ID_MASTERDEL (23)
565 #define SWM_ARG_ID_FLIPLAYOUT (24)
566 #define SWM_ARG_ID_STACKRESET (30)
567 #define SWM_ARG_ID_STACKINIT (31)
568 #define SWM_ARG_ID_CYCLEWS_UP (40)
569 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
570 #define SWM_ARG_ID_CYCLESC_UP (42)
571 #define SWM_ARG_ID_CYCLESC_DOWN (43)
572 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
573 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
574 #define SWM_ARG_ID_STACKINC (50)
575 #define SWM_ARG_ID_STACKDEC (51)
576 #define SWM_ARG_ID_SS_ALL (60)
577 #define SWM_ARG_ID_SS_WINDOW (61)
578 #define SWM_ARG_ID_DONTCENTER (70)
579 #define SWM_ARG_ID_CENTER (71)
580 #define SWM_ARG_ID_KILLWINDOW (80)
581 #define SWM_ARG_ID_DELETEWINDOW (81)
582 #define SWM_ARG_ID_WIDTHGROW (90)
583 #define SWM_ARG_ID_WIDTHSHRINK (91)
584 #define SWM_ARG_ID_HEIGHTGROW (92)
585 #define SWM_ARG_ID_HEIGHTSHRINK (93)
586 #define SWM_ARG_ID_MOVEUP (100)
587 #define SWM_ARG_ID_MOVEDOWN (101)
588 #define SWM_ARG_ID_MOVELEFT (102)
589 #define SWM_ARG_ID_MOVERIGHT (103)
595 TAILQ_ENTRY(quirk
) entry
;
599 #define SWM_Q_FLOAT (1<<0) /* float this window */
600 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
601 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
602 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
603 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
604 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
606 TAILQ_HEAD(quirk_list
, quirk
);
607 struct quirk_list quirks
= TAILQ_HEAD_INITIALIZER(quirks
);
610 * Supported EWMH hints should be added to
611 * both the enum and the ewmh array
616 _NET_MOVERESIZE_WINDOW
,
617 _NET_WM_ACTION_ABOVE
,
618 _NET_WM_ACTION_CLOSE
,
619 _NET_WM_ACTION_FULLSCREEN
,
621 _NET_WM_ACTION_RESIZE
,
622 _NET_WM_ALLOWED_ACTIONS
,
626 _NET_WM_STATE_FULLSCREEN
,
627 _NET_WM_STATE_HIDDEN
,
628 _NET_WM_STATE_MAXIMIZED_HORZ
,
629 _NET_WM_STATE_MAXIMIZED_VERT
,
630 _NET_WM_STATE_SKIP_PAGER
,
631 _NET_WM_STATE_SKIP_TASKBAR
,
633 _NET_WM_WINDOW_TYPE_DIALOG
,
634 _NET_WM_WINDOW_TYPE_DOCK
,
635 _NET_WM_WINDOW_TYPE_NORMAL
,
636 _NET_WM_WINDOW_TYPE_SPLASH
,
637 _NET_WM_WINDOW_TYPE_TOOLBAR
,
638 _NET_WM_WINDOW_TYPE_UTILITY
,
639 _SWM_WM_STATE_MANUAL
,
646 } ewmh
[SWM_EWMH_HINT_MAX
] = {
647 /* must be in same order as in the enum */
648 {"_NET_ACTIVE_WINDOW", XCB_ATOM_NONE
},
649 {"_NET_CLOSE_WINDOW", XCB_ATOM_NONE
},
650 {"_NET_MOVERESIZE_WINDOW", XCB_ATOM_NONE
},
651 {"_NET_WM_ACTION_ABOVE", XCB_ATOM_NONE
},
652 {"_NET_WM_ACTION_CLOSE", XCB_ATOM_NONE
},
653 {"_NET_WM_ACTION_FULLSCREEN", XCB_ATOM_NONE
},
654 {"_NET_WM_ACTION_MOVE", XCB_ATOM_NONE
},
655 {"_NET_WM_ACTION_RESIZE", XCB_ATOM_NONE
},
656 {"_NET_WM_ALLOWED_ACTIONS", XCB_ATOM_NONE
},
657 {"_NET_WM_NAME", XCB_ATOM_NONE
},
658 {"_NET_WM_STATE", XCB_ATOM_NONE
},
659 {"_NET_WM_STATE_ABOVE", XCB_ATOM_NONE
},
660 {"_NET_WM_STATE_FULLSCREEN", XCB_ATOM_NONE
},
661 {"_NET_WM_STATE_HIDDEN", XCB_ATOM_NONE
},
662 {"_NET_WM_STATE_MAXIMIZED_HORZ", XCB_ATOM_NONE
},
663 {"_NET_WM_STATE_MAXIMIZED_VERT", XCB_ATOM_NONE
},
664 {"_NET_WM_STATE_SKIP_PAGER", XCB_ATOM_NONE
},
665 {"_NET_WM_STATE_SKIP_TASKBAR", XCB_ATOM_NONE
},
666 {"_NET_WM_WINDOW_TYPE", XCB_ATOM_NONE
},
667 {"_NET_WM_WINDOW_TYPE_DIALOG", XCB_ATOM_NONE
},
668 {"_NET_WM_WINDOW_TYPE_DOCK", XCB_ATOM_NONE
},
669 {"_NET_WM_WINDOW_TYPE_NORMAL", XCB_ATOM_NONE
},
670 {"_NET_WM_WINDOW_TYPE_SPLASH", XCB_ATOM_NONE
},
671 {"_NET_WM_WINDOW_TYPE_TOOLBAR", XCB_ATOM_NONE
},
672 {"_NET_WM_WINDOW_TYPE_UTILITY", XCB_ATOM_NONE
},
673 {"_SWM_WM_STATE_MANUAL", XCB_ATOM_NONE
},
680 XC_BOTTOM_LEFT_CORNER
,
681 XC_BOTTOM_RIGHT_CORNER
,
689 char *name
; /* Name used by Xcursor .*/
690 uint8_t cf_char
; /* cursorfont index. */
692 } cursors
[XC_MAX
] = {
693 {"fleur", XC_fleur
, XCB_CURSOR_NONE
},
694 {"left_ptr", XC_left_ptr
, XCB_CURSOR_NONE
},
695 {"bottom_left_corner", XC_bottom_left_corner
, XCB_CURSOR_NONE
},
696 {"bottom_right_corner", XC_bottom_right_corner
, XCB_CURSOR_NONE
},
697 {"sizing", XC_sizing
, XCB_CURSOR_NONE
},
698 {"top_left_corner", XC_top_left_corner
, XCB_CURSOR_NONE
},
699 {"top_right_corner", XC_top_right_corner
, XCB_CURSOR_NONE
},
704 TAILQ_ENTRY(spawn_prog
) entry
;
709 TAILQ_HEAD(spawn_list
, spawn_prog
);
710 struct spawn_list spawns
= TAILQ_HEAD_INITIALIZER(spawns
);
712 /* user/key callable function IDs */
803 KF_DUMPWINS
, /* MUST BE LAST */
811 enum keyfuncid funcid
;
814 RB_HEAD(key_tree
, key
);
816 /* function prototypes */
817 void adjust_font(struct ws_win
*);
818 void bar_class_name(char *, size_t, struct swm_region
*);
819 void bar_class_title_name(char *, size_t, struct swm_region
*);
820 void bar_cleanup(struct swm_region
*);
821 void bar_extra_stop(void);
822 void bar_fmt(const char *, char *, struct swm_region
*, size_t);
823 void bar_fmt_expand(char *, size_t);
824 void bar_fmt_print(void);
825 void bar_print(struct swm_region
*, const char *);
826 void bar_print_legacy(struct swm_region
*, const char *);
827 void bar_refresh(void);
828 void bar_replace(char *, char *, struct swm_region
*, size_t);
829 void bar_replace_pad(char *, int *, size_t);
830 char * bar_replace_seq(char *, char *, struct swm_region
*, size_t *,
832 void bar_setup(struct swm_region
*);
833 void bar_signal(int);
834 void bar_title_name(char *, size_t, struct swm_region
*);
835 void bar_toggle(struct swm_region
*, union arg
*);
836 void bar_update(void);
837 void bar_urgent(char *, size_t);
838 void bar_window_float(char *, size_t, struct swm_region
*);
839 void bar_window_name(char *, size_t, struct swm_region
*);
840 void bar_workspace_name(char *, size_t, struct swm_region
*);
841 void buttonpress(xcb_button_press_event_t
*);
842 void check_conn(void);
843 void clear_keys(void);
844 void clientmessage(xcb_client_message_event_t
*);
845 void client_msg(struct ws_win
*, xcb_atom_t
, xcb_timestamp_t
);
846 int conf_load(const char *, int);
847 void configurenotify(xcb_configure_notify_event_t
*);
848 void configurerequest(xcb_configure_request_event_t
*);
849 void config_win(struct ws_win
*, xcb_configure_request_event_t
*);
850 void constrain_window(struct ws_win
*, struct swm_region
*, int);
851 int count_win(struct workspace
*, int);
852 void cursors_cleanup(void);
853 void cursors_load(void);
854 void custom_region(char *);
855 void cyclescr(struct swm_region
*, union arg
*);
856 void cyclews(struct swm_region
*, union arg
*);
857 void cycle_layout(struct swm_region
*, union arg
*);
858 void destroynotify(xcb_destroy_notify_event_t
*);
859 void dumpwins(struct swm_region
*, union arg
*);
861 void enternotify(xcb_enter_notify_event_t
*);
862 void event_drain(uint8_t);
863 void event_error(xcb_generic_error_t
*);
864 void event_handle(xcb_generic_event_t
*);
865 void ewmh_autoquirk(struct ws_win
*);
866 void ewmh_get_win_state(struct ws_win
*);
867 int ewmh_set_win_fullscreen(struct ws_win
*, int);
868 void ewmh_update_actions(struct ws_win
*);
869 void ewmh_update_win_state(struct ws_win
*, xcb_atom_t
, long);
870 char *expand_tilde(const char *);
871 void expose(xcb_expose_event_t
*);
872 void fake_keypress(struct ws_win
*, xcb_keysym_t
, uint16_t);
873 struct pid_e
*find_pid(pid_t
);
874 struct ws_win
*find_unmanaged_window(xcb_window_t
);
875 struct ws_win
*find_window(xcb_window_t
);
876 void floating_toggle(struct swm_region
*, union arg
*);
877 int floating_toggle_win(struct ws_win
*);
878 void focus(struct swm_region
*, union arg
*);
880 void focusin(xcb_focus_in_event_t
*);
881 void focusout(xcb_focus_out_event_t
*);
883 void focus_flush(void);
884 void focus_win(struct ws_win
*);
885 void fontset_init(void);
886 void free_window(struct ws_win
*);
887 xcb_atom_t
get_atom_from_string(const char *);
889 char *get_atom_name(xcb_atom_t
);
891 struct ws_win
*get_focus_magic(struct ws_win
*);
892 struct ws_win
*get_focus_prev(struct ws_win
*);
894 char *get_notify_detail_label(uint8_t);
895 char *get_notify_mode_label(uint8_t);
897 struct ws_win
*get_pointer_win(xcb_window_t
);
898 struct ws_win
*get_region_focus(struct swm_region
*);
899 xcb_screen_t
*get_screen(int);
901 char *get_stack_mode_name(uint8_t);
903 int32_t get_swm_iconic(struct ws_win
*);
904 char *get_win_name(xcb_window_t
);
905 int get_ws_idx(xcb_window_t
);
906 uint32_t getstate(xcb_window_t
);
907 void grabbuttons(struct ws_win
*);
909 void grab_windows(void);
910 void iconify(struct swm_region
*, union arg
*);
912 void keypress(xcb_key_press_event_t
*);
913 int key_cmp(struct key
*, struct key
*);
914 void key_insert(unsigned int, KeySym
, enum keyfuncid
, const char *);
915 struct key
*key_lookup(unsigned int, KeySym
);
916 void key_remove(struct key
*);
917 void key_replace(struct key
*, unsigned int, KeySym
, enum keyfuncid
,
919 void kill_refs(struct ws_win
*);
921 void leavenotify(xcb_leave_notify_event_t
*);
923 void load_float_geom(struct ws_win
*, struct swm_region
*);
924 struct ws_win
*manage_window(xcb_window_t
, uint16_t);
925 void map_window(struct ws_win
*);
926 void mapnotify(xcb_map_notify_event_t
*);
927 void mappingnotify(xcb_mapping_notify_event_t
*);
928 void maprequest(xcb_map_request_event_t
*);
929 void move(struct ws_win
*, union arg
*);
930 void move_step(struct swm_region
*, union arg
*);
931 uint32_t name_to_pixel(const char *);
932 void name_workspace(struct swm_region
*, union arg
*);
933 void new_region(struct swm_screen
*, int, int, int, int);
934 int parsekeys(char *, unsigned int, unsigned int *, KeySym
*);
935 int parsequirks(char *, unsigned long *);
936 int parse_rgb(const char *, uint16_t *, uint16_t *, uint16_t *);
937 void pressbutton(struct swm_region
*, union arg
*);
938 void priorws(struct swm_region
*, union arg
*);
940 void print_win_geom(xcb_window_t
);
942 void propertynotify(xcb_property_notify_event_t
*);
943 void quirk_insert(const char *, const char *, unsigned long);
944 void quirk_remove(struct quirk
*);
945 void quirk_replace(struct quirk
*, const char *, const char *,
947 void quit(struct swm_region
*, union arg
*);
948 void raise_toggle(struct swm_region
*, union arg
*);
949 void resize(struct ws_win
*, union arg
*);
950 void resize_step(struct swm_region
*, union arg
*);
951 void restart(struct swm_region
*, union arg
*);
952 struct swm_region
*root_to_region(xcb_window_t
, int);
953 void screenchange(xcb_randr_screen_change_notify_event_t
*);
954 void scan_xrandr(int);
955 void search_do_resp(void);
956 void search_resp_name_workspace(const char *, unsigned long);
957 void search_resp_search_window(const char *);
958 void search_resp_search_workspace(const char *);
959 void search_resp_uniconify(const char *, unsigned long);
960 void search_win(struct swm_region
*, union arg
*);
961 void search_win_cleanup(void);
962 void search_workspace(struct swm_region
*, union arg
*);
963 void send_to_ws(struct swm_region
*, union arg
*);
964 int setautorun(char *, char *, int);
965 int setconfbinding(char *, char *, int);
966 int setconfcolor(char *, char *, int);
967 int setconfmodkey(char *, char *, int);
968 int setconfquirk(char *, char *, int);
969 int setconfregion(char *, char *, int);
970 int setconfspawn(char *, char *, int);
971 int setconfvalue(char *, char *, int);
972 void setkeybinding(unsigned int, KeySym
, enum keyfuncid
, const char *);
973 int setkeymapping(char *, char *, int);
974 int setlayout(char *, char *, int);
975 void setquirk(const char *, const char *, unsigned long);
976 void setscreencolor(char *, int, int);
977 void setspawn(const char *, const char *);
978 void setup_ewmh(void);
979 void setup_globals(void);
980 void setup_keys(void);
981 void setup_quirks(void);
982 void setup_screens(void);
983 void setup_spawn(void);
984 void set_child_transient(struct ws_win
*, xcb_window_t
*);
985 void set_swm_iconic(struct ws_win
*, int);
986 void set_win_state(struct ws_win
*, uint16_t);
987 void shutdown_cleanup(void);
989 void socket_setnonblock(int);
990 void sort_windows(struct ws_win_list
*);
991 void spawn(int, union arg
*, int);
992 void spawn_custom(struct swm_region
*, union arg
*, const char *);
993 int spawn_expand(struct swm_region
*, union arg
*, const char *, char ***);
994 void spawn_insert(const char *, const char *);
995 void spawn_remove(struct spawn_prog
*);
996 void spawn_replace(struct spawn_prog
*, const char *, const char *);
997 void spawn_select(struct swm_region
*, union arg
*, const char *, int *);
998 void stack_config(struct swm_region
*, union arg
*);
999 void stack_floater(struct ws_win
*, struct swm_region
*);
1000 void stack_master(struct workspace
*, struct swm_geometry
*, int, int);
1001 void store_float_geom(struct ws_win
*, struct swm_region
*);
1002 char * strdupsafe(const char *);
1003 void swapwin(struct swm_region
*, union arg
*);
1004 void switchws(struct swm_region
*, union arg
*);
1005 void teardown_ewmh(void);
1006 void unfocus_win(struct ws_win
*);
1007 void uniconify(struct swm_region
*, union arg
*);
1008 void unmanage_window(struct ws_win
*);
1009 void unmapnotify(xcb_unmap_notify_event_t
*);
1010 void unmap_all(void);
1011 void unmap_window(struct ws_win
*);
1012 void updatenumlockmask(void);
1013 void update_modkey(unsigned int);
1014 void update_window(struct ws_win
*);
1015 int validate_win(struct ws_win
*);
1016 int validate_ws(struct workspace
*);
1017 /*void visibilitynotify(xcb_visibility_notify_event_t *);*/
1018 void version(struct swm_region
*, union arg
*);
1019 pid_t
window_get_pid(xcb_window_t
);
1020 void wkill(struct swm_region
*, union arg
*);
1021 void workaround(void);
1022 void xft_init(struct swm_region
*);
1024 RB_PROTOTYPE(key_tree
, key
, entry
, key_cmp
);
1025 RB_GENERATE(key_tree
, key
, entry
, key_cmp
);
1026 struct key_tree keys
;
1031 xcb_font_t cf
= XCB_NONE
;
1034 for (i
= 0; i
< LENGTH(cursors
); ++i
) {
1035 /* try to load Xcursor first. */
1036 cursors
[i
].cid
= XcursorLibraryLoadCursor(display
,
1039 /* fallback to cursorfont. */
1040 if (cursors
[i
].cid
== XCB_CURSOR_NONE
) {
1041 if (cf
== XCB_NONE
) {
1042 cf
= xcb_generate_id(conn
);
1043 xcb_open_font(conn
, cf
, strlen("cursor"),
1047 cursors
[i
].cid
= xcb_generate_id(conn
);
1048 xcb_create_glyph_cursor(conn
, cursors
[i
].cid
, cf
, cf
,
1049 cursors
[i
].cf_char
, cursors
[i
].cf_char
+ 1, 0, 0, 0,
1050 0xffff, 0xffff, 0xffff);
1056 xcb_close_font(conn
, cf
);
1060 cursors_cleanup(void)
1063 for (i
= 0; i
< LENGTH(cursors
); ++i
)
1064 xcb_free_cursor(conn
, cursors
[i
].cid
);
1068 expand_tilde(const char *s
)
1070 struct passwd
*ppwd
;
1078 errx(1, "expand_tilde: NULL string.");
1081 result
= strdup(sc
);
1087 if ((max
= sysconf(_SC_LOGIN_NAME_MAX
)) == -1)
1088 errx(1, "expand_tilde: sysconf");
1090 if ((user
= calloc(1, max
+ 1)) == NULL
)
1091 errx(1, "expand_tilde: calloc");
1093 for (i
= 0; s
[i
] != '/' && s
[i
] != '\0'; ++i
)
1098 ppwd
= strlen(user
) == 0 ? getpwuid(getuid()) : getpwnam(user
);
1102 result
= strdup(sc
);
1104 if (asprintf(&result
, "%s%s", ppwd
->pw_dir
, s
) == -1)
1108 errx(1, "expand_tilde: failed to allocate memory.");
1114 parse_rgb(const char *rgb
, uint16_t *rr
, uint16_t *gg
, uint16_t *bb
)
1116 unsigned int tmpr
, tmpg
, tmpb
;
1118 if (sscanf(rgb
, "rgb:%x/%x/%x", &tmpr
, &tmpg
, &tmpb
) != 3)
1121 *rr
= RGB_8_TO_16(tmpr
);
1122 *gg
= RGB_8_TO_16(tmpg
);
1123 *bb
= RGB_8_TO_16(tmpb
);
1129 get_screen(int screen
)
1131 const xcb_setup_t
*r
;
1132 xcb_screen_iterator_t iter
;
1134 if ((r
= xcb_get_setup(conn
)) == NULL
) {
1135 DNPRINTF(SWM_D_MISC
, "get_screen: xcb_get_setup\n");
1139 iter
= xcb_setup_roots_iterator(r
);
1140 for (; iter
.rem
; --screen
, xcb_screen_next(&iter
))
1150 if (focus_mode
== SWM_FOCUS_DEFAULT
)
1151 event_drain(XCB_ENTER_NOTIFY
);
1157 get_atom_from_string(const char *str
)
1159 xcb_intern_atom_cookie_t c
;
1160 xcb_intern_atom_reply_t
*r
;
1163 c
= xcb_intern_atom(conn
, 0, strlen(str
), str
);
1164 r
= xcb_intern_atom_reply(conn
, c
, NULL
);
1172 return (XCB_ATOM_NONE
);
1176 set_swm_iconic(struct ws_win
*win
, int newv
)
1183 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
1184 a_swm_iconic
, XCB_ATOM_INTEGER
, 32, 1, &v
);
1186 xcb_delete_property(conn
, win
->id
, a_swm_iconic
);
1190 get_swm_iconic(struct ws_win
*win
)
1193 xcb_get_property_reply_t
*pr
= NULL
;
1195 pr
= xcb_get_property_reply(conn
,
1196 xcb_get_property(conn
, 0, win
->id
, a_swm_iconic
,
1197 XCB_ATOM_INTEGER
, 0, 1), NULL
);
1200 if (pr
->type
!= XCB_ATOM_INTEGER
|| pr
->format
!= 32)
1202 v
= *((int32_t *)xcb_get_property_value(pr
));
1212 xcb_atom_t sup_list
;
1213 int i
, j
, num_screens
;
1215 sup_list
= get_atom_from_string("_NET_SUPPORTED");
1217 for (i
= 0; i
< LENGTH(ewmh
); i
++)
1218 ewmh
[i
].atom
= get_atom_from_string(ewmh
[i
].name
);
1220 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1221 for (i
= 0; i
< num_screens
; i
++) {
1222 /* Support check window will be created by workaround(). */
1224 /* Report supported atoms */
1225 xcb_delete_property(conn
, screens
[i
].root
, sup_list
);
1226 for (j
= 0; j
< LENGTH(ewmh
); j
++)
1227 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
,
1228 screens
[i
].root
, sup_list
, XCB_ATOM_ATOM
, 32, 1,
1237 xcb_atom_t sup_check
, sup_list
;
1239 xcb_get_property_cookie_t pc
;
1240 xcb_get_property_reply_t
*pr
;
1242 sup_check
= get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
1243 sup_list
= get_atom_from_string("_NET_SUPPORTED");
1244 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1246 for (i
= 0; i
< num_screens
; i
++) {
1247 /* Get the support check window and destroy it */
1248 pc
= xcb_get_property(conn
, 0, screens
[i
].root
, sup_check
,
1249 XCB_ATOM_WINDOW
, 0, 1);
1250 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
1253 if (pr
->format
== sup_check
) {
1254 id
= *((xcb_window_t
*)xcb_get_property_value(pr
));
1256 xcb_destroy_window(conn
, id
);
1257 xcb_delete_property(conn
, screens
[i
].root
, sup_check
);
1258 xcb_delete_property(conn
, screens
[i
].root
, sup_list
);
1265 ewmh_autoquirk(struct ws_win
*win
)
1269 xcb_get_property_cookie_t c
;
1270 xcb_get_property_reply_t
*r
;
1272 c
= xcb_get_property(conn
, 0, win
->id
,
1273 ewmh
[_NET_WM_WINDOW_TYPE
].atom
, XCB_ATOM_ATOM
, 0, UINT32_MAX
);
1274 r
= xcb_get_property_reply(conn
, c
, NULL
);
1278 n
= xcb_get_property_value_length(r
);
1279 type
= xcb_get_property_value(r
);
1281 for (i
= 0; i
< n
; i
++) {
1282 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_NORMAL
].atom
)
1284 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_DOCK
].atom
||
1285 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_TOOLBAR
].atom
||
1286 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_UTILITY
].atom
) {
1288 win
->quirks
= SWM_Q_FLOAT
| SWM_Q_ANYWHERE
;
1291 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_SPLASH
].atom
||
1292 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_DIALOG
].atom
) {
1294 win
->quirks
= SWM_Q_FLOAT
;
1301 #define SWM_EWMH_ACTION_COUNT_MAX (6)
1302 #define EWMH_F_FULLSCREEN (1<<0)
1303 #define EWMH_F_ABOVE (1<<1)
1304 #define EWMH_F_HIDDEN (1<<2)
1305 #define EWMH_F_SKIP_PAGER (1<<3)
1306 #define EWMH_F_SKIP_TASKBAR (1<<4)
1307 #define SWM_F_MANUAL (1<<5)
1310 ewmh_set_win_fullscreen(struct ws_win
*win
, int fs
)
1318 DNPRINTF(SWM_D_MISC
, "ewmh_set_win_fullscreen: window: 0x%x, "
1319 "fullscreen %s\n", win
->id
, YESNO(fs
));
1322 if (!win
->g_floatvalid
)
1323 store_float_geom(win
, win
->ws
->r
);
1325 win
->g
= win
->ws
->r
->g
;
1328 load_float_geom(win
, win
->ws
->r
);
1335 ewmh_update_actions(struct ws_win
*win
)
1337 xcb_atom_t actions
[SWM_EWMH_ACTION_COUNT_MAX
];
1343 actions
[n
++] = ewmh
[_NET_WM_ACTION_CLOSE
].atom
;
1345 if (win
->floating
) {
1346 actions
[n
++] = ewmh
[_NET_WM_ACTION_MOVE
].atom
;
1347 actions
[n
++] = ewmh
[_NET_WM_ACTION_RESIZE
].atom
;
1348 actions
[n
++] = ewmh
[_NET_WM_ACTION_ABOVE
].atom
;
1351 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
1352 ewmh
[_NET_WM_ALLOWED_ACTIONS
].atom
, XCB_ATOM_ATOM
, 32, 1, actions
);
1355 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
1356 #define _NET_WM_STATE_ADD 1 /* add/set property */
1357 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
1360 ewmh_update_win_state(struct ws_win
*win
, xcb_atom_t state
, long action
)
1362 unsigned int mask
= 0;
1363 unsigned int changed
= 0;
1364 unsigned int orig_flags
;
1369 DNPRINTF(SWM_D_PROP
, "ewmh_update_win_state: window: 0x%x, state: %ld, "
1370 "action: %ld\n", win
->id
, (unsigned long)state
, action
);
1372 if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
)
1373 mask
= EWMH_F_FULLSCREEN
;
1374 else if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
)
1375 mask
= EWMH_F_ABOVE
;
1376 else if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
)
1377 mask
= SWM_F_MANUAL
;
1378 else if (state
== ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
)
1379 mask
= EWMH_F_SKIP_PAGER
;
1380 else if (state
== ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
)
1381 mask
= EWMH_F_SKIP_TASKBAR
;
1383 orig_flags
= win
->ewmh_flags
;
1386 case _NET_WM_STATE_REMOVE
:
1387 win
->ewmh_flags
&= ~mask
;
1389 case _NET_WM_STATE_ADD
:
1390 win
->ewmh_flags
|= mask
;
1392 case _NET_WM_STATE_TOGGLE
:
1393 win
->ewmh_flags
^= mask
;
1397 changed
= (win
->ewmh_flags
& mask
) ^ (orig_flags
& mask
) ? 1 : 0;
1399 if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
) {
1400 if (changed
&& !floating_toggle_win(win
))
1401 win
->ewmh_flags
= orig_flags
; /* revert */
1402 } else if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
) {
1404 win
->manual
= (win
->ewmh_flags
& SWM_F_MANUAL
) != 0;
1405 } else if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
) {
1406 if (changed
&& !ewmh_set_win_fullscreen(win
,
1407 win
->ewmh_flags
& EWMH_F_FULLSCREEN
))
1408 win
->ewmh_flags
= orig_flags
; /* revert */
1411 xcb_delete_property(conn
, win
->id
, ewmh
[_NET_WM_STATE
].atom
);
1413 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
1414 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1415 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1416 &ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
);
1417 else if (win
->ewmh_flags
& EWMH_F_SKIP_PAGER
)
1418 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1419 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1420 &ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
);
1421 else if (win
->ewmh_flags
& EWMH_F_SKIP_TASKBAR
)
1422 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1423 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1424 &ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
);
1425 else if (win
->ewmh_flags
& EWMH_F_ABOVE
)
1426 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1427 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1428 &ewmh
[_NET_WM_STATE_ABOVE
].atom
);
1429 else if (win
->ewmh_flags
& SWM_F_MANUAL
)
1430 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1431 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1432 &ewmh
[_SWM_WM_STATE_MANUAL
].atom
);
1436 ewmh_get_win_state(struct ws_win
*win
)
1439 xcb_get_property_cookie_t c
;
1440 xcb_get_property_reply_t
*r
;
1446 win
->ewmh_flags
= 0;
1448 win
->ewmh_flags
|= EWMH_F_ABOVE
;
1450 win
->ewmh_flags
|= SWM_F_MANUAL
;
1452 c
= xcb_get_property(conn
, 0, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
1453 XCB_ATOM_ATOM
, 0, UINT32_MAX
);
1454 r
= xcb_get_property_reply(conn
, c
, NULL
);
1458 states
= xcb_get_property_value(r
);
1459 n
= xcb_get_property_value_length(r
);
1461 for (i
= 0; i
< n
; i
++)
1462 ewmh_update_win_state(win
, states
[i
], _NET_WM_STATE_ADD
);
1470 dumpwins(struct swm_region
*r
, union arg
*args
)
1474 xcb_get_window_attributes_cookie_t c
;
1475 xcb_get_window_attributes_reply_t
*wa
;
1477 /* suppress unused warning since var is needed */
1480 if (r
->ws
== NULL
) {
1481 warnx("dumpwins: invalid workspace");
1485 warnx("=== managed window list ws %02d ===", r
->ws
->idx
);
1486 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
1487 state
= getstate(win
->id
);
1488 c
= xcb_get_window_attributes(conn
, win
->id
);
1489 wa
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
1491 warnx("window: 0x%x, map_state: %d, state: %u, "
1492 "transient: 0x%x", win
->id
, wa
->map_state
,
1493 state
, win
->transient
);
1496 warnx("window: 0x%x, failed xcb_get_window_attributes",
1500 warnx("===== unmanaged window list =====");
1501 TAILQ_FOREACH(win
, &r
->ws
->unmanagedlist
, entry
) {
1502 state
= getstate(win
->id
);
1503 c
= xcb_get_window_attributes(conn
, win
->id
);
1504 wa
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
1506 warnx("window: 0x%x, map_state: %d, state: %u, "
1507 "transient: 0x%x", win
->id
, wa
->map_state
,
1508 state
, win
->transient
);
1511 warnx("window: 0x%x, failed xcb_get_window_attributes",
1515 warnx("=================================");
1519 dumpwins(struct swm_region
*r
, union arg
*s
)
1524 #endif /* SWM_DEBUG */
1529 int saved_errno
, status
;
1532 saved_errno
= errno
;
1536 while ((pid
= waitpid(WAIT_ANY
, &status
, WNOHANG
)) != 0) {
1541 if (errno
!= ECHILD
)
1542 warn("sighdlr: waitpid");
1543 #endif /* SWM_DEBUG */
1546 if (pid
== searchpid
)
1550 if (WIFEXITED(status
)) {
1551 if (WEXITSTATUS(status
) != 0)
1552 warnx("sighdlr: child exit status: %d",
1553 WEXITSTATUS(status
));
1555 warnx("sighdlr: child is terminated "
1557 #endif /* SWM_DEBUG */
1571 errno
= saved_errno
;
1577 struct pid_e
*p
= NULL
;
1579 DNPRINTF(SWM_D_MISC
, "find_pid: %d\n", pid
);
1584 TAILQ_FOREACH(p
, &pidlist
, entry
) {
1593 name_to_pixel(const char *colorname
)
1595 uint32_t result
= 0;
1596 char cname
[32] = "#";
1597 xcb_screen_t
*screen
;
1598 xcb_colormap_t cmap
;
1599 xcb_alloc_color_reply_t
*cr
;
1600 xcb_alloc_named_color_reply_t
*nr
;
1601 uint16_t rr
, gg
, bb
;
1603 screen
= xcb_setup_roots_iterator(xcb_get_setup(conn
)).data
;
1604 cmap
= screen
->default_colormap
;
1606 /* color is in format rgb://rr/gg/bb */
1607 if (strncmp(colorname
, "rgb:", 4) == 0) {
1608 if (parse_rgb(colorname
, &rr
, &gg
, &bb
) == -1)
1609 warnx("could not parse rgb %s", colorname
);
1611 cr
= xcb_alloc_color_reply(conn
,
1612 xcb_alloc_color(conn
, cmap
, rr
, gg
, bb
),
1618 warnx("color '%s' not found", colorname
);
1621 nr
= xcb_alloc_named_color_reply(conn
,
1622 xcb_alloc_named_color(conn
, cmap
, strlen(colorname
),
1625 strlcat(cname
, colorname
+ 2, sizeof cname
- 1);
1626 nr
= xcb_alloc_named_color_reply(conn
,
1627 xcb_alloc_named_color(conn
, cmap
, strlen(cname
),
1634 warnx("color '%s' not found", colorname
);
1641 setscreencolor(char *val
, int i
, int c
)
1645 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1646 if (i
> 0 && i
<= num_screens
) {
1647 screens
[i
- 1].c
[c
].pixel
= name_to_pixel(val
);
1648 free(screens
[i
- 1].c
[c
].name
);
1649 if ((screens
[i
- 1].c
[c
].name
= strdup(val
)) == NULL
)
1651 } else if (i
== -1) {
1652 for (i
= 0; i
< num_screens
; i
++) {
1653 screens
[i
].c
[c
].pixel
= name_to_pixel(val
);
1654 free(screens
[i
].c
[c
].name
);
1655 if ((screens
[i
].c
[c
].name
= strdup(val
)) == NULL
)
1659 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1664 fancy_stacker(struct workspace
*ws
)
1666 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1667 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1668 snprintf(ws
->stacker
, sizeof ws
->stacker
,
1669 ws
->l_state
.vertical_flip
? "[%d>%d]" : "[%d|%d]",
1670 ws
->l_state
.vertical_mwin
, ws
->l_state
.vertical_stacks
);
1671 if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1672 snprintf(ws
->stacker
, sizeof ws
->stacker
,
1673 ws
->l_state
.horizontal_flip
? "[%dv%d]" : "[%d-%d]",
1674 ws
->l_state
.horizontal_mwin
, ws
->l_state
.horizontal_stacks
);
1678 plain_stacker(struct workspace
*ws
)
1680 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1681 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1682 strlcpy(ws
->stacker
, ws
->l_state
.vertical_flip
? "[>]" : "[|]",
1683 sizeof ws
->stacker
);
1684 if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1685 strlcpy(ws
->stacker
, ws
->l_state
.horizontal_flip
? "[v]" : "[-]",
1686 sizeof ws
->stacker
);
1690 custom_region(char *val
)
1692 unsigned int x
, y
, w
, h
;
1693 int sidx
, num_screens
;
1694 xcb_screen_t
*screen
;
1696 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1697 if (sscanf(val
, "screen[%d]:%ux%u+%u+%u", &sidx
, &w
, &h
, &x
, &y
) != 5)
1698 errx(1, "invalid custom region, "
1699 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1700 if (sidx
< 1 || sidx
> num_screens
)
1701 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1705 if ((screen
= get_screen(sidx
)) == NULL
)
1706 errx(1, "ERROR: can't get screen %d.", sidx
);
1709 errx(1, "region %ux%u+%u+%u too small", w
, h
, x
, y
);
1711 if (x
> screen
->width_in_pixels
||
1712 y
> screen
->height_in_pixels
||
1713 w
+ x
> screen
->width_in_pixels
||
1714 h
+ y
> screen
->height_in_pixels
) {
1715 warnx("ignoring region %ux%u+%u+%u - not within screen "
1716 "boundaries (%ux%u)", w
, h
, x
, y
,
1717 screen
->width_in_pixels
, screen
->height_in_pixels
);
1721 new_region(&screens
[sidx
], x
, y
, w
, h
);
1725 socket_setnonblock(int fd
)
1729 if ((flags
= fcntl(fd
, F_GETFL
, 0)) == -1)
1730 err(1, "fcntl F_GETFL");
1731 flags
|= O_NONBLOCK
;
1732 if ((flags
= fcntl(fd
, F_SETFL
, flags
)) == -1)
1733 err(1, "fcntl F_SETFL");
1737 bar_print_legacy(struct swm_region
*r
, const char *s
)
1739 xcb_rectangle_t rect
;
1744 XRectangle ibox
, lbox
;
1748 XmbTextExtents(bar_fs
, s
, len
, &ibox
, &lbox
);
1750 switch (bar_justify
) {
1751 case SWM_BAR_JUSTIFY_LEFT
:
1754 case SWM_BAR_JUSTIFY_CENTER
:
1755 x
= (WIDTH(r
) - lbox
.width
) / 2;
1757 case SWM_BAR_JUSTIFY_RIGHT
:
1758 x
= WIDTH(r
) - lbox
.width
- SWM_BAR_OFFSET
;
1762 if (x
< SWM_BAR_OFFSET
)
1767 rect
.width
= WIDTH(r
->bar
);
1768 rect
.height
= HEIGHT(r
->bar
);
1770 /* clear back buffer */
1771 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
1772 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
1773 xcb_poly_fill_rectangle(conn
, r
->bar
->buffer
, r
->s
->bar_gc
, 1, &rect
);
1775 /* draw back buffer */
1776 gcvd
.graphics_exposures
= 0;
1777 draw
= XCreateGC(display
, r
->bar
->buffer
, GCGraphicsExposures
, &gcvd
);
1778 XSetForeground(display
, draw
, r
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
);
1779 DRAWSTRING(display
, r
->bar
->buffer
, bar_fs
, draw
,
1780 x
, (bar_fs_extents
->max_logical_extent
.height
- lbox
.height
) / 2 -
1782 XFreeGC(display
, draw
);
1785 xcb_copy_area(conn
, r
->bar
->buffer
, r
->bar
->id
, r
->s
->bar_gc
, 0, 0,
1786 0, 0, WIDTH(r
->bar
), HEIGHT(r
->bar
));
1790 bar_print(struct swm_region
*r
, const char *s
)
1793 xcb_rectangle_t rect
;
1801 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
, &info
);
1803 switch (bar_justify
) {
1804 case SWM_BAR_JUSTIFY_LEFT
:
1807 case SWM_BAR_JUSTIFY_CENTER
:
1808 x
= (WIDTH(r
) - info
.width
) / 2;
1810 case SWM_BAR_JUSTIFY_RIGHT
:
1811 x
= WIDTH(r
) - info
.width
- SWM_BAR_OFFSET
;
1815 if (x
< SWM_BAR_OFFSET
)
1820 rect
.width
= WIDTH(r
->bar
);
1821 rect
.height
= HEIGHT(r
->bar
);
1823 /* clear back buffer */
1824 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
1825 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
1826 xcb_poly_fill_rectangle(conn
, r
->bar
->buffer
, r
->s
->bar_gc
, 1, &rect
);
1828 /* draw back buffer */
1829 draw
= XftDrawCreate(display
, r
->bar
->buffer
,
1830 DefaultVisual(display
, r
->s
->idx
),
1831 DefaultColormap(display
, r
->s
->idx
));
1833 XftDrawStringUtf8(draw
, &bar_font_color
, bar_font
, x
,
1834 (HEIGHT(r
->bar
) + bar_font
->height
) / 2 - bar_font
->descent
,
1837 XftDrawDestroy(draw
);
1840 xcb_copy_area(conn
, r
->bar
->buffer
, r
->bar
->id
, r
->s
->bar_gc
, 0, 0,
1841 0, 0, WIDTH(r
->bar
), HEIGHT(r
->bar
));
1845 bar_extra_stop(void)
1849 bzero(bar_pipe
, sizeof bar_pipe
);
1852 kill(bar_pid
, SIGTERM
);
1855 strlcpy(bar_ext
, "", sizeof bar_ext
);
1860 bar_class_name(char *s
, size_t sz
, struct swm_region
*r
)
1862 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1864 if (r
->ws
->focus
->ch
.class_name
!= NULL
)
1865 strlcat(s
, r
->ws
->focus
->ch
.class_name
, sz
);
1869 bar_title_name(char *s
, size_t sz
, struct swm_region
*r
)
1871 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1873 if (r
->ws
->focus
->ch
.instance_name
!= NULL
)
1874 strlcat(s
, r
->ws
->focus
->ch
.instance_name
, sz
);
1878 bar_class_title_name(char *s
, size_t sz
, struct swm_region
*r
)
1880 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1883 bar_class_name(s
, sz
, r
);
1884 strlcat(s
, ":", sz
);
1885 bar_title_name(s
, sz
, r
);
1889 bar_window_float(char *s
, size_t sz
, struct swm_region
*r
)
1891 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1893 if (r
->ws
->focus
->floating
)
1894 strlcat(s
, "(f)", sz
);
1898 bar_window_name(char *s
, size_t sz
, struct swm_region
*r
)
1902 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1904 if ((title
= get_win_name(r
->ws
->focus
->id
)) == NULL
)
1907 strlcat(s
, title
, sz
);
1911 int urgent
[SWM_WS_MAX
];
1913 bar_urgent(char *s
, size_t sz
)
1916 int i
, j
, num_screens
;
1918 xcb_get_property_cookie_t c
;
1919 xcb_icccm_wm_hints_t hints
;
1921 for (i
= 0; i
< workspace_limit
; i
++)
1924 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1925 for (i
= 0; i
< num_screens
; i
++)
1926 for (j
= 0; j
< workspace_limit
; j
++)
1927 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
) {
1928 c
= xcb_icccm_get_wm_hints(conn
, win
->id
);
1929 if (xcb_icccm_get_wm_hints_reply(conn
, c
,
1932 if (hints
.flags
& XCB_ICCCM_WM_HINT_X_URGENCY
)
1936 for (i
= 0; i
< workspace_limit
; i
++) {
1938 snprintf(b
, sizeof b
, "%d ", i
+ 1);
1940 snprintf(b
, sizeof b
, "- ");
1946 bar_workspace_name(char *s
, size_t sz
, struct swm_region
*r
)
1948 if (r
== NULL
|| r
->ws
== NULL
)
1950 if (r
->ws
->name
!= NULL
)
1951 strlcat(s
, r
->ws
->name
, sz
);
1954 /* build the default bar format according to the defined enabled options */
1956 bar_fmt(const char *fmtexp
, char *fmtnew
, struct swm_region
*r
, size_t sz
)
1958 /* if format provided, just copy the buffers */
1959 if (bar_format
!= NULL
) {
1960 strlcpy(fmtnew
, fmtexp
, sz
);
1964 /* reset the output buffer */
1967 strlcat(fmtnew
, "+N:+I ", sz
);
1969 strlcat(fmtnew
, "+S", sz
);
1970 strlcat(fmtnew
, " ", sz
);
1972 /* only show the workspace name if there's actually one */
1973 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->name
!= NULL
)
1974 strlcat(fmtnew
, "<+D>", sz
);
1975 strlcat(fmtnew
, "+3<", sz
);
1977 if (clock_enabled
) {
1978 strlcat(fmtnew
, fmtexp
, sz
);
1979 strlcat(fmtnew
, "+4<", sz
);
1982 /* bar_urgent already adds the space before the last asterisk */
1984 strlcat(fmtnew
, "* +U*+4<", sz
);
1986 if (title_class_enabled
) {
1987 strlcat(fmtnew
, "+C", sz
);
1988 if (!title_name_enabled
)
1989 strlcat(fmtnew
, "+4<", sz
);
1992 /* checks needed by the colon and floating strlcat(3) calls below */
1993 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->focus
!= NULL
) {
1994 if (title_name_enabled
) {
1995 if (title_class_enabled
)
1996 strlcat(fmtnew
, ":", sz
);
1997 strlcat(fmtnew
, "+T+4<", sz
);
1999 if (window_name_enabled
) {
2000 if (r
->ws
->focus
->floating
)
2001 strlcat(fmtnew
, "+F ", sz
);
2002 strlcat(fmtnew
, "+64W ", sz
);
2006 /* finally add the action script output and the version */
2007 strlcat(fmtnew
, "+4<+A+4<+V", sz
);
2011 bar_replace_pad(char *tmp
, int *limit
, size_t sz
)
2013 /* special case; no limit given, pad one space, instead */
2014 if (*limit
== (int)sz
- 1)
2016 snprintf(tmp
, sz
, "%*s", *limit
, " ");
2019 /* replaces the bar format character sequences (like in tmux(1)) */
2021 bar_replace_seq(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t *offrep
,
2025 char tmp
[SWM_BAR_MAX
];
2029 /* reset strlcat(3) buffer */
2032 /* get number, if any */
2035 if (sscanf(fmt
, "%d%n", &limit
, &size
) != 1)
2036 limit
= sizeof tmp
- 1;
2037 if (limit
<= 0 || limit
>= (int)sizeof tmp
)
2038 limit
= sizeof tmp
- 1;
2040 /* there is nothing to replace (ie EOL) */
2047 bar_replace_pad(tmp
, &limit
, sizeof tmp
);
2050 snprintf(tmp
, sizeof tmp
, "%s", bar_ext
);
2053 bar_class_name(tmp
, sizeof tmp
, r
);
2056 bar_workspace_name(tmp
, sizeof tmp
, r
);
2059 bar_window_float(tmp
, sizeof tmp
, r
);
2062 snprintf(tmp
, sizeof tmp
, "%d", r
->ws
->idx
+ 1);
2065 snprintf(tmp
, sizeof tmp
, "%d", r
->s
->idx
+ 1);
2068 bar_class_title_name(tmp
, sizeof tmp
, r
);
2071 snprintf(tmp
, sizeof tmp
, "%s", r
->ws
->stacker
);
2074 bar_title_name(tmp
, sizeof tmp
, r
);
2077 bar_urgent(tmp
, sizeof tmp
);
2080 snprintf(tmp
, sizeof tmp
, "%s", bar_vertext
);
2083 bar_window_name(tmp
, sizeof tmp
, r
);
2086 /* unknown character sequence; copy as-is */
2087 snprintf(tmp
, sizeof tmp
, "+%c", *fmt
);
2093 if ((int)len
< limit
)
2095 while (limit
-- > 0) {
2096 if (*offrep
>= sz
- 1)
2098 fmtrep
[(*offrep
)++] = *ptr
++;
2106 bar_replace(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t sz
)
2111 while (*fmt
!= '\0') {
2113 /* skip ordinary characters */
2116 fmtrep
[off
++] = *fmt
++;
2120 /* character sequence found; replace it */
2121 fmt
= bar_replace_seq(fmt
, fmtrep
, r
, &off
, sz
);
2130 bar_fmt_expand(char *fmtexp
, size_t sz
)
2137 /* start by grabbing the current time and date */
2139 localtime_r(&tmt
, &tm
);
2141 /* figure out what to expand */
2142 if (bar_format
!= NULL
)
2144 else if (bar_format
== NULL
&& clock_enabled
)
2146 /* if nothing to expand bail out */
2152 /* copy as-is, just in case the format shouldn't be expanded below */
2153 strlcpy(fmtexp
, fmt
, sz
);
2154 /* finally pass the string through strftime(3) */
2155 #ifndef SWM_DENY_CLOCK_FORMAT
2156 if ((len
= strftime(fmtexp
, sz
, fmt
, &tm
)) == 0)
2157 warnx("format too long");
2165 char fmtexp
[SWM_BAR_MAX
], fmtnew
[SWM_BAR_MAX
];
2166 char fmtrep
[SWM_BAR_MAX
];
2168 struct swm_region
*r
;
2170 /* expand the format by first passing it through strftime(3) */
2171 bar_fmt_expand(fmtexp
, sizeof fmtexp
);
2173 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2174 for (i
= 0; i
< num_screens
; i
++) {
2175 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
2178 bar_fmt(fmtexp
, fmtnew
, r
, sizeof fmtnew
);
2179 bar_replace(fmtnew
, fmtrep
, r
, sizeof fmtrep
);
2180 if (bar_font_legacy
)
2181 bar_print_legacy(r
, fmtrep
);
2183 bar_print(r
, fmtrep
);
2196 if (bar_extra
&& bar_extra_running
) {
2197 /* ignore short reads; it'll correct itself */
2198 while ((b
= fgetln(stdin
, &len
)) != NULL
)
2199 if (b
&& b
[len
- 1] == '\n') {
2201 strlcpy(bar_ext
, b
, sizeof bar_ext
);
2203 if (b
== NULL
&& errno
!= EAGAIN
) {
2204 warn("bar_update: bar_extra failed");
2208 strlcpy(bar_ext
, "", sizeof bar_ext
);
2217 /* suppress unused warning since var is needed */
2224 bar_toggle(struct swm_region
*r
, union arg
*args
)
2226 struct swm_region
*tmpr
;
2229 /* suppress unused warnings since vars are needed */
2233 DNPRINTF(SWM_D_BAR
, "bar_toggle\n");
2235 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2237 for (i
= 0; i
< num_screens
; i
++)
2238 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
2240 xcb_unmap_window(conn
, tmpr
->bar
->id
);
2242 for (i
= 0; i
< num_screens
; i
++)
2243 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
2245 xcb_map_window(conn
, tmpr
->bar
->id
);
2248 bar_enabled
= !bar_enabled
;
2252 /* must be after stack */
2261 struct swm_region
*r
;
2265 /* do this here because the conf file is in memory */
2266 if (bar_extra
&& !bar_extra_running
&& bar_argv
[0]) {
2267 /* launch external status app */
2268 bar_extra_running
= 1;
2269 if (pipe(bar_pipe
) == -1)
2270 err(1, "pipe error");
2271 socket_setnonblock(bar_pipe
[0]);
2272 socket_setnonblock(bar_pipe
[1]); /* XXX hmmm, really? */
2273 if (dup2(bar_pipe
[0], 0) == -1)
2275 if (dup2(bar_pipe
[1], 1) == -1)
2277 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
2278 err(1, "could not disable SIGPIPE");
2279 switch (bar_pid
= fork()) {
2281 err(1, "cannot fork");
2285 execvp(bar_argv
[0], bar_argv
);
2286 err(1, "%s external app failed", bar_argv
[0]);
2288 default: /* parent */
2294 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2295 for (i
= 0; i
< num_screens
; i
++)
2296 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
2299 wa
[0] = screens
[i
].c
[SWM_S_COLOR_BAR
].pixel
;
2300 wa
[1] = screens
[i
].c
[SWM_S_COLOR_BAR_BORDER
].pixel
;
2301 xcb_change_window_attributes(conn
, r
->bar
->id
,
2302 XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
, wa
);
2312 while ((s
= index(s
, '-'))) {
2317 return (count
== 14);
2323 char *default_string
;
2324 char **missing_charsets
;
2325 int num_missing_charsets
= 0;
2329 XFreeFontSet(display
, bar_fs
);
2333 DNPRINTF(SWM_D_INIT
, "fontset_init: loading bar_fonts: %s\n", bar_fonts
);
2335 bar_fs
= XCreateFontSet(display
, bar_fonts
, &missing_charsets
,
2336 &num_missing_charsets
, &default_string
);
2338 if (num_missing_charsets
> 0) {
2339 warnx("Unable to load charset(s):");
2341 for (i
= 0; i
< num_missing_charsets
; ++i
)
2342 warnx("%s", missing_charsets
[i
]);
2344 XFreeStringList(missing_charsets
);
2346 if (strcmp(default_string
, ""))
2347 warnx("Glyphs from those sets will be replaced "
2348 "by '%s'.", default_string
);
2350 warnx("Glyphs from those sets won't be drawn.");
2354 errx(1, "Error creating font set structure.");
2356 bar_fs_extents
= XExtentsOfFontSet(bar_fs
);
2358 bar_height
= bar_fs_extents
->max_logical_extent
.height
+
2359 2 * bar_border_width
;
2366 xft_init(struct swm_region
*r
)
2368 char *font
, *d
, *search
;
2371 if (bar_font
== NULL
) {
2372 if ((d
= strdup(bar_fonts
)) == NULL
)
2373 errx(1, "insufficient memory.");
2375 while ((font
= strsep(&search
, ",")) != NULL
) {
2379 DNPRINTF(SWM_D_INIT
, "xft_init: try font %s\n", font
);
2382 bar_font
= XftFontOpenXlfd(display
, r
->s
->idx
,
2385 bar_font
= XftFontOpenName(display
, r
->s
->idx
,
2390 warnx("unable to load font %s", font
);
2393 DNPRINTF(SWM_D_INIT
, "successfully opened "
2401 if (bar_font
== NULL
)
2402 errx(1, "unable to open a font");
2404 PIXEL_TO_XRENDERCOLOR(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
, color
);
2406 if (!XftColorAllocValue(display
, DefaultVisual(display
, r
->s
->idx
),
2407 DefaultColormap(display
, r
->s
->idx
), &color
, &bar_font_color
))
2408 warn("Xft error: unable to allocate color.");
2410 bar_height
= bar_font
->height
+ 2 * bar_border_width
;
2417 bar_setup(struct swm_region
*r
)
2419 xcb_screen_t
*screen
;
2422 DNPRINTF(SWM_D_BAR
, "bar_setup: screen %d.\n",
2425 if ((screen
= get_screen(r
->s
->idx
)) == NULL
)
2426 errx(1, "ERROR: can't get screen %d.", r
->s
->idx
);
2431 if ((r
->bar
= calloc(1, sizeof(struct swm_bar
))) == NULL
)
2432 err(1, "bar_setup: calloc: failed to allocate memory.");
2434 if (bar_font_legacy
)
2440 Y(r
->bar
) = bar_at_bottom
? (Y(r
) + HEIGHT(r
) - bar_height
) : Y(r
);
2441 WIDTH(r
->bar
) = WIDTH(r
) - 2 * bar_border_width
;
2442 HEIGHT(r
->bar
) = bar_height
- 2 * bar_border_width
;
2444 r
->bar
->id
= xcb_generate_id(conn
);
2445 wa
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
2446 wa
[1] = r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].pixel
;
2447 wa
[2] = XCB_EVENT_MASK_EXPOSURE
;
2449 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, r
->bar
->id
, r
->s
->root
,
2450 X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
), HEIGHT(r
->bar
),
2451 bar_border_width
, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
2452 XCB_COPY_FROM_PARENT
, XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
2453 | XCB_CW_EVENT_MASK
, wa
);
2455 r
->bar
->buffer
= xcb_generate_id(conn
);
2456 xcb_create_pixmap(conn
, screen
->root_depth
, r
->bar
->buffer
, r
->bar
->id
,
2457 WIDTH(r
->bar
), HEIGHT(r
->bar
));
2460 xcb_randr_select_input(conn
, r
->bar
->id
,
2461 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE
);
2464 xcb_map_window(conn
, r
->bar
->id
);
2466 DNPRINTF(SWM_D_BAR
, "bar_setup: window: 0x%x, (x,y) w x h: (%d,%d) "
2467 "%d x %d\n", WINID(r
->bar
), X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
),
2470 if (signal(SIGALRM
, bar_signal
) == SIG_ERR
)
2471 err(1, "could not install bar_signal");
2476 bar_cleanup(struct swm_region
*r
)
2480 xcb_destroy_window(conn
, r
->bar
->id
);
2481 xcb_free_pixmap(conn
, r
->bar
->buffer
);
2487 set_win_state(struct ws_win
*win
, uint16_t state
)
2489 uint16_t data
[2] = { state
, XCB_ATOM_NONE
};
2491 DNPRINTF(SWM_D_EVENT
, "set_win_state: window: 0x%x, state: %u\n",
2497 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
, a_state
,
2498 a_state
, 32, 2, data
);
2502 getstate(xcb_window_t w
)
2504 uint32_t result
= 0;
2505 xcb_get_property_cookie_t c
;
2506 xcb_get_property_reply_t
*r
;
2508 c
= xcb_get_property(conn
, 0, w
, a_state
, a_state
, 0L, 2L);
2509 r
= xcb_get_property_reply(conn
, c
, NULL
);
2512 if (r
->type
== a_state
&& r
->format
== 32 && r
->length
== 2)
2513 result
= *((uint32_t *)xcb_get_property_value(r
));
2517 DNPRINTF(SWM_D_MISC
, "getstate property: win 0x%x state %u\n", w
,
2523 version(struct swm_region
*r
, union arg
*args
)
2525 /* suppress unused warnings since vars are needed */
2529 bar_version
= !bar_version
;
2531 snprintf(bar_vertext
, sizeof bar_vertext
,
2532 "Version: %s Build: %s", SPECTRWM_VERSION
, buildstr
);
2534 strlcpy(bar_vertext
, "", sizeof bar_vertext
);
2541 client_msg(struct ws_win
*win
, xcb_atom_t a
, xcb_timestamp_t t
)
2543 xcb_client_message_event_t ev
;
2551 name
= get_atom_name(a
);
2552 DNPRINTF(SWM_D_EVENT
, "client_msg: window: 0x%x, atom: %s(%u), "
2554 win
->id
, name
, a
, t
);
2558 bzero(&ev
, sizeof ev
);
2559 ev
.response_type
= XCB_CLIENT_MESSAGE
;
2560 ev
.window
= win
->id
;
2563 ev
.data
.data32
[0] = a
;
2564 ev
.data
.data32
[1] = t
;
2566 xcb_send_event(conn
, 0, win
->id
,
2567 XCB_EVENT_MASK_NO_EVENT
, (const char *)&ev
);
2570 /* synthetic response to a ConfigureRequest when not making a change */
2572 config_win(struct ws_win
*win
, xcb_configure_request_event_t
*ev
)
2574 xcb_configure_notify_event_t ce
;
2579 /* send notification of unchanged state. */
2580 bzero(&ce
, sizeof(ce
));
2581 ce
.response_type
= XCB_CONFIGURE_NOTIFY
;
2584 ce
.width
= WIDTH(win
);
2585 ce
.height
= HEIGHT(win
);
2586 ce
.override_redirect
= 0;
2591 ce
.window
= win
->id
;
2592 ce
.border_width
= BORDER(win
);
2593 ce
.above_sibling
= XCB_WINDOW_NONE
;
2596 ce
.event
= ev
->window
;
2597 ce
.window
= ev
->window
;
2599 /* make response appear more WM_SIZE_HINTS-compliant */
2600 if (win
->sh
.flags
) {
2601 DNPRINTF(SWM_D_MISC
, "config_win: hints: window: 0x%x,"
2602 " sh.flags: %u, min: %d x %d, max: %d x %d, inc: "
2603 "%d x %d\n", win
->id
, win
->sh
.flags
, SH_MIN_W(win
),
2604 SH_MIN_H(win
), SH_MAX_W(win
), SH_MAX_H(win
),
2605 SH_INC_W(win
), SH_INC_H(win
));
2610 /* the hint may be set... to 0! */
2611 if (SH_MIN_W(win
) > 0 && ce
.width
< SH_MIN_W(win
))
2612 ce
.width
= SH_MIN_W(win
);
2613 if (SH_MIN_H(win
) > 0 && ce
.height
< SH_MIN_H(win
))
2614 ce
.height
= SH_MIN_H(win
);
2619 /* may also be advertized as 0 */
2620 if (SH_MAX_W(win
) > 0 && ce
.width
> SH_MAX_W(win
))
2621 ce
.width
= SH_MAX_W(win
);
2622 if (SH_MAX_H(win
) > 0 && ce
.height
> SH_MAX_H(win
))
2623 ce
.height
= SH_MAX_H(win
);
2626 /* resize increment. */
2628 if (SH_INC_W(win
) > 1 && ce
.width
> SH_INC_W(win
))
2629 ce
.width
-= (ce
.width
- SH_MIN_W(win
)) %
2631 if (SH_INC_H(win
) > 1 && ce
.height
> SH_INC_H(win
))
2632 ce
.height
-= (ce
.height
- SH_MIN_H(win
)) %
2636 /* adjust x and y for requested border_width. */
2637 ce
.x
+= BORDER(win
) - ev
->border_width
;
2638 ce
.y
+= BORDER(win
) - ev
->border_width
;
2639 ce
.border_width
= ev
->border_width
;
2640 ce
.above_sibling
= ev
->sibling
;
2643 DNPRINTF(SWM_D_MISC
, "config_win: ewmh: %s, window: 0x%x, (x,y) w x h: "
2644 "(%d,%d) %d x %d, border: %d\n", YESNO(ev
== NULL
), win
->id
, ce
.x
,
2645 ce
.y
, ce
.width
, ce
.height
, ce
.border_width
);
2647 xcb_send_event(conn
, 0, win
->id
, XCB_EVENT_MASK_STRUCTURE_NOTIFY
,
2652 count_win(struct workspace
*ws
, int count_transient
)
2657 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
2658 if (!count_transient
&& win
->floating
)
2660 if (!count_transient
&& win
->transient
)
2666 DNPRINTF(SWM_D_MISC
, "count_win: %d\n", count
);
2672 quit(struct swm_region
*r
, union arg
*args
)
2674 /* suppress unused warnings since vars are needed */
2678 DNPRINTF(SWM_D_MISC
, "quit\n");
2683 map_window(struct ws_win
*win
)
2685 uint32_t val
= XCB_STACK_MODE_ABOVE
;
2690 DNPRINTF(SWM_D_EVENT
, "map_window: win 0x%x, mapped: %s\n", win
->id
,
2691 YESNO(win
->mapped
));
2693 xcb_configure_window(conn
, win
->id
,
2694 XCB_CONFIG_WINDOW_STACK_MODE
, &val
);
2699 xcb_map_window(conn
, win
->id
);
2700 set_win_state(win
, XCB_ICCCM_WM_STATE_NORMAL
);
2705 unmap_window(struct ws_win
*win
)
2710 DNPRINTF(SWM_D_EVENT
, "unmap_window: win 0x%x, mapped: %s\n", win
->id
,
2711 YESNO(win
->mapped
));
2716 xcb_unmap_window(conn
, win
->id
);
2717 set_win_state(win
, XCB_ICCCM_WM_STATE_ICONIC
);
2725 int i
, j
, num_screens
;
2727 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2728 for (i
= 0; i
< num_screens
; i
++)
2729 for (j
= 0; j
< workspace_limit
; j
++)
2730 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2735 fake_keypress(struct ws_win
*win
, xcb_keysym_t keysym
, uint16_t modifiers
)
2737 xcb_key_press_event_t event
;
2738 xcb_keycode_t
*keycode
;
2743 keycode
= xcb_key_symbols_get_keycode(syms
, keysym
);
2745 DNPRINTF(SWM_D_MISC
, "fake_keypress: win 0x%x keycode %u\n",
2748 bzero(&event
, sizeof(event
));
2749 event
.event
= win
->id
;
2750 event
.root
= win
->s
->root
;
2751 event
.child
= XCB_WINDOW_NONE
;
2752 event
.time
= XCB_CURRENT_TIME
;
2753 event
.event_x
= X(win
);
2754 event
.event_y
= Y(win
);
2757 event
.same_screen
= 1;
2758 event
.detail
= *keycode
;
2759 event
.state
= modifiers
;
2761 event
.response_type
= XCB_KEY_PRESS
;
2762 xcb_send_event(conn
, 1, win
->id
,
2763 XCB_EVENT_MASK_KEY_PRESS
, (const char *)&event
);
2765 event
.response_type
= XCB_KEY_RELEASE
;
2766 xcb_send_event(conn
, 1, win
->id
,
2767 XCB_EVENT_MASK_KEY_RELEASE
, (const char *)&event
);
2773 restart(struct swm_region
*r
, union arg
*args
)
2775 /* suppress unused warning since var is needed */
2779 DNPRINTF(SWM_D_MISC
, "restart: %s\n", start_argv
[0]);
2783 execvp(start_argv
[0], start_argv
);
2784 warn("execvp failed");
2789 get_pointer_win(xcb_window_t root
)
2791 struct ws_win
*win
= NULL
;
2792 xcb_query_pointer_reply_t
*r
;
2794 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: root: 0x%x.\n", root
);
2796 r
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, root
), NULL
);
2798 win
= find_window(r
->child
);
2800 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: 0x%x.\n",
2803 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: none.\n");
2812 root_to_region(xcb_window_t root
, int check
)
2815 struct swm_region
*r
= NULL
;
2817 xcb_query_pointer_reply_t
*qpr
;
2818 xcb_get_input_focus_reply_t
*gifr
;
2820 DNPRINTF(SWM_D_MISC
, "root_to_region: window: 0x%x\n", root
);
2822 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2823 for (i
= 0; i
< num_screens
; i
++)
2824 if (screens
[i
].root
== root
)
2827 if (check
& SWM_CK_FOCUS
) {
2828 /* Try to find an actively focused window */
2829 gifr
= xcb_get_input_focus_reply(conn
,
2830 xcb_get_input_focus(conn
), NULL
);
2832 cfw
= find_window(gifr
->focus
);
2833 if (cfw
&& cfw
->ws
->r
)
2840 if (r
== NULL
&& check
& SWM_CK_POINTER
) {
2841 /* No region with an active focus; try to use pointer. */
2842 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
,
2843 screens
[i
].root
), NULL
);
2846 DNPRINTF(SWM_D_MISC
, "root_to_region: pointer: "
2847 "(%d,%d)\n", qpr
->root_x
, qpr
->root_y
);
2848 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2849 if (X(r
) <= qpr
->root_x
&&
2850 qpr
->root_x
< MAX_X(r
) &&
2851 Y(r
) <= qpr
->root_y
&&
2852 qpr
->root_y
< MAX_Y(r
))
2859 if (r
== NULL
&& check
& SWM_CK_FALLBACK
)
2860 r
= TAILQ_FIRST(&screens
[i
].rl
);
2866 find_unmanaged_window(xcb_window_t id
)
2869 int i
, j
, num_screens
;
2871 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2872 for (i
= 0; i
< num_screens
; i
++)
2873 for (j
= 0; j
< workspace_limit
; j
++)
2874 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].unmanagedlist
,
2882 find_window(xcb_window_t id
)
2885 int i
, j
, num_screens
;
2886 xcb_query_tree_reply_t
*r
;
2888 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2889 for (i
= 0; i
< num_screens
; i
++)
2890 for (j
= 0; j
< workspace_limit
; j
++)
2891 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2895 r
= xcb_query_tree_reply(conn
, xcb_query_tree(conn
, id
), NULL
);
2899 /* if we were looking for the parent return that window instead */
2900 if (r
->parent
== 0 || r
->root
== r
->parent
) {
2905 /* look for parent */
2906 for (i
= 0; i
< num_screens
; i
++)
2907 for (j
= 0; j
< workspace_limit
; j
++)
2908 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2909 if (r
->parent
== win
->id
) {
2919 spawn(int ws_idx
, union arg
*args
, int close_fd
)
2924 DNPRINTF(SWM_D_MISC
, "spawn: %s\n", args
->argv
[0]);
2926 close(xcb_get_file_descriptor(conn
));
2928 setenv("LD_PRELOAD", SWM_LIB
, 1);
2930 if (asprintf(&ret
, "%d", ws_idx
) == -1) {
2931 warn("spawn: asprintf SWM_WS");
2934 setenv("_SWM_WS", ret
, 1);
2938 if (asprintf(&ret
, "%d", getpid()) == -1) {
2939 warn("spawn: asprintf _SWM_PID");
2942 setenv("_SWM_PID", ret
, 1);
2946 if (setsid() == -1) {
2947 warn("spawn: setsid");
2953 * close stdin and stdout to prevent interaction between apps
2954 * and the baraction script
2955 * leave stderr open to record errors
2957 if ((fd
= open(_PATH_DEVNULL
, O_RDWR
, 0)) == -1) {
2958 warn("spawn: open");
2961 dup2(fd
, STDIN_FILENO
);
2962 dup2(fd
, STDOUT_FILENO
);
2967 execvp(args
->argv
[0], args
->argv
);
2969 warn("spawn: execvp");
2974 kill_refs(struct ws_win
*win
)
2976 int i
, x
, num_screens
;
2977 struct swm_region
*r
;
2978 struct workspace
*ws
;
2983 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2984 for (i
= 0; i
< num_screens
; i
++)
2985 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2986 for (x
= 0; x
< workspace_limit
; x
++) {
2988 if (win
== ws
->focus
)
2990 if (win
== ws
->focus_prev
)
2991 ws
->focus_prev
= NULL
;
2996 validate_win(struct ws_win
*testwin
)
2999 struct workspace
*ws
;
3000 struct swm_region
*r
;
3001 int i
, x
, num_screens
;
3003 if (testwin
== NULL
)
3006 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
3007 for (i
= 0; i
< num_screens
; i
++)
3008 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3009 for (x
= 0; x
< workspace_limit
; x
++) {
3011 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
3019 validate_ws(struct workspace
*testws
)
3021 struct swm_region
*r
;
3022 struct workspace
*ws
;
3023 int i
, x
, num_screens
;
3025 /* validate all ws */
3026 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
3027 for (i
= 0; i
< num_screens
; i
++)
3028 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3029 for (x
= 0; x
< workspace_limit
; x
++) {
3038 unfocus_win(struct ws_win
*win
)
3040 xcb_window_t none
= XCB_WINDOW_NONE
;
3042 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: window: 0x%x\n", WINID(win
));
3046 if (win
->ws
== NULL
) {
3047 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: NULL ws.\n");
3051 if (validate_ws(win
->ws
)) {
3052 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: invalid ws.\n");
3056 if (win
->ws
->r
== NULL
) {
3057 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: NULL region.\n");
3061 if (validate_win(win
)) {
3062 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: invalid win.\n");
3067 if (win
->ws
->focus
== win
) {
3068 win
->ws
->focus
= NULL
;
3069 win
->ws
->focus_prev
= win
;
3072 if (validate_win(win
->ws
->focus
)) {
3073 kill_refs(win
->ws
->focus
);
3074 win
->ws
->focus
= NULL
;
3076 if (validate_win(win
->ws
->focus_prev
)) {
3077 kill_refs(win
->ws
->focus_prev
);
3078 win
->ws
->focus_prev
= NULL
;
3081 xcb_change_window_attributes(conn
, win
->id
, XCB_CW_BORDER_PIXEL
,
3082 &win
->ws
->r
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
);
3084 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
3085 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1, &none
);
3087 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: done.\n");
3091 focus_win(struct ws_win
*win
)
3093 struct ws_win
*cfw
= NULL
, *parent
= NULL
, *w
;
3094 struct workspace
*ws
;
3095 xcb_get_input_focus_reply_t
*r
;
3097 DNPRINTF(SWM_D_FOCUS
, "focus_win: window: 0x%x\n", WINID(win
));
3102 if (win
->ws
== NULL
)
3107 if (validate_ws(ws
))
3110 if (validate_win(win
)) {
3115 r
= xcb_get_input_focus_reply(conn
, xcb_get_input_focus(conn
), NULL
);
3117 cfw
= find_window(r
->focus
);
3123 if (ws
->focus
!= win
) {
3124 if (ws
->focus
&& ws
->focus
!= cfw
)
3125 unfocus_win(ws
->focus
);
3129 /* If this window directs focus to a child window, then clear. */
3130 if (win
->focus_child
)
3131 win
->focus_child
= NULL
;
3133 /* If transient, adjust parent's focus child for focus_magic. */
3134 if (win
->transient
) {
3135 parent
= find_window(win
->transient
);
3136 if (parent
&& parent
->focus_child
!= win
)
3137 parent
->focus_child
= win
;
3140 if (cfw
!= win
&& ws
->r
!= NULL
) {
3141 /* Set input focus if no input hint, or indicated by hint. */
3142 if (!(win
->hints
.flags
& XCB_ICCCM_WM_HINT_INPUT
) ||
3143 (win
->hints
.flags
& XCB_ICCCM_WM_HINT_INPUT
&&
3145 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
,
3146 win
->id
, last_event_time
);
3148 /* Tell app it can adjust focus to a specific window. */
3149 if (win
->take_focus
) {
3150 /* java is special; always tell parent */
3151 if (win
->transient
&& win
->java
)
3152 client_msg(parent
, a_takefocus
,
3155 client_msg(win
, a_takefocus
, last_event_time
);
3158 xcb_change_window_attributes(conn
, win
->id
, XCB_CW_BORDER_PIXEL
,
3159 &ws
->r
->s
->c
[SWM_S_COLOR_FOCUS
].pixel
);
3161 if (ws
->cur_layout
->flags
& SWM_L_MAPONFOCUS
||
3163 /* If a parent exists, map it first. */
3167 /* Map siblings next. */
3168 TAILQ_FOREACH(w
, &ws
->winlist
, entry
)
3169 if (w
!= win
&& !w
->iconic
&&
3170 w
->transient
== parent
->id
)
3174 /* Map focused window. */
3177 /* Finally, map children of focus window. */
3178 TAILQ_FOREACH(w
, &ws
->winlist
, entry
)
3179 if (w
->transient
== win
->id
&& !w
->iconic
)
3184 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
3185 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1,
3192 DNPRINTF(SWM_D_FOCUS
, "focus_win: done.\n");
3195 /* If a child window should have focus instead, return it. */
3197 get_focus_magic(struct ws_win
*win
)
3199 struct ws_win
*parent
= NULL
;
3200 struct ws_win
*child
= NULL
;
3202 DNPRINTF(SWM_D_FOCUS
, "get_focus_magic: window: 0x%x\n", WINID(win
));
3206 if (win
->transient
) {
3207 parent
= find_window(win
->transient
);
3209 /* If parent prefers focus elsewhere, then try to do so. */
3210 if (parent
&& (child
= parent
->focus_child
)) {
3211 if (validate_win(child
) == 0 && child
->mapped
)
3214 parent
->focus_child
= NULL
;
3218 /* If this window prefers focus elsewhere, then try to do so. */
3219 if ((child
= win
->focus_child
)) {
3220 if (validate_win(child
) == 0 && child
->mapped
)
3223 win
->focus_child
= NULL
;
3230 event_drain(uint8_t rt
)
3232 xcb_generic_event_t
*evt
;
3234 /* ensure all pending requests have been processed before filtering. */
3236 while ((evt
= xcb_poll_for_event(conn
))) {
3237 if (XCB_EVENT_RESPONSE_TYPE(evt
) != rt
)
3245 switchws(struct swm_region
*r
, union arg
*args
)
3247 int wsid
= args
->id
, unmap_old
= 0;
3248 struct swm_region
*this_r
, *other_r
;
3250 struct workspace
*new_ws
, *old_ws
;
3255 if (wsid
>= workspace_limit
)
3259 old_ws
= this_r
->ws
;
3260 new_ws
= &this_r
->s
->ws
[wsid
];
3262 DNPRINTF(SWM_D_WS
, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
3263 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), old_ws
->idx
, wsid
);
3265 if (new_ws
== NULL
|| old_ws
== NULL
)
3267 if (new_ws
== old_ws
)
3270 unfocus_win(old_ws
->focus
);
3272 other_r
= new_ws
->r
;
3273 if (other_r
== NULL
) {
3274 /* the other workspace is hidden, hide this one */
3278 /* the other ws is visible in another region, exchange them */
3279 other_r
->ws_prior
= new_ws
;
3280 other_r
->ws
= old_ws
;
3281 old_ws
->r
= other_r
;
3283 this_r
->ws_prior
= old_ws
;
3284 this_r
->ws
= new_ws
;
3287 /* Set focus_pending before stacking. */
3288 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
3289 new_ws
->focus_pending
= get_region_focus(new_ws
->r
);
3293 /* unmap old windows */
3295 TAILQ_FOREACH(win
, &old_ws
->winlist
, entry
)
3298 /* if workspaces were swapped, then don't wait to set focus */
3299 if (old_ws
->r
&& focus_mode
!= SWM_FOCUS_FOLLOW
) {
3300 if (new_ws
->focus_pending
) {
3301 focus_win(new_ws
->focus_pending
);
3303 /* Empty region, focus on root. */
3304 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
,
3305 new_ws
->r
->s
[new_ws
->r
->s
->idx
].root
,
3310 /* Clear bar if new ws is empty. */
3311 if (new_ws
->focus_pending
== NULL
)
3316 DNPRINTF(SWM_D_WS
, "switchws: done.\n");
3320 cyclews(struct swm_region
*r
, union arg
*args
)
3323 struct swm_screen
*s
= r
->s
;
3326 DNPRINTF(SWM_D_WS
, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3327 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
3332 case SWM_ARG_ID_CYCLEWS_UP_ALL
:
3335 case SWM_ARG_ID_CYCLEWS_UP
:
3336 if (a
.id
< workspace_limit
- 1)
3341 case SWM_ARG_ID_CYCLEWS_DOWN_ALL
:
3344 case SWM_ARG_ID_CYCLEWS_DOWN
:
3348 a
.id
= workspace_limit
- 1;
3355 (!cycle_empty
&& TAILQ_EMPTY(&s
->ws
[a
.id
].winlist
)))
3357 if (!cycle_visible
&& s
->ws
[a
.id
].r
!= NULL
)
3361 } while (a
.id
!= r
->ws
->idx
);
3365 priorws(struct swm_region
*r
, union arg
*args
)
3371 DNPRINTF(SWM_D_WS
, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3372 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
3374 if (r
->ws_prior
== NULL
)
3377 a
.id
= r
->ws_prior
->idx
;
3382 cyclescr(struct swm_region
*r
, union arg
*args
)
3385 struct swm_region
*rr
= NULL
;
3388 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
3389 /* do nothing if we don't have more than one screen */
3390 if (!(num_screens
> 1 || outputs
> 1))
3395 case SWM_ARG_ID_CYCLESC_UP
:
3396 rr
= TAILQ_NEXT(r
, entry
);
3398 rr
= TAILQ_FIRST(&screens
[i
].rl
);
3400 case SWM_ARG_ID_CYCLESC_DOWN
:
3401 rr
= TAILQ_PREV(r
, swm_region_list
, entry
);
3403 rr
= TAILQ_LAST(&screens
[i
].rl
, swm_region_list
);
3411 nfw
= get_region_focus(rr
);
3415 /* New region is empty; unfocus old region and warp pointer. */
3416 unfocus_win(r
->ws
->focus
);
3417 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
,
3418 rr
->s
[i
].root
, XCB_CURRENT_TIME
);
3420 /* Clear bar since empty. */
3428 sort_windows(struct ws_win_list
*wl
)
3430 struct ws_win
*win
, *parent
, *nxt
;
3435 for (win
= TAILQ_FIRST(wl
); win
!= TAILQ_END(wl
); win
= nxt
) {
3436 nxt
= TAILQ_NEXT(win
, entry
);
3437 if (win
->transient
) {
3438 parent
= find_window(win
->transient
);
3439 if (parent
== NULL
) {
3440 warnx("not possible bug");
3443 TAILQ_REMOVE(wl
, win
, entry
);
3444 TAILQ_INSERT_AFTER(wl
, parent
, win
, entry
);
3451 swapwin(struct swm_region
*r
, union arg
*args
)
3453 struct ws_win
*target
, *source
;
3454 struct ws_win
*cur_focus
;
3455 struct ws_win_list
*wl
;
3457 DNPRINTF(SWM_D_WS
, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3458 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
3460 cur_focus
= r
->ws
->focus
;
3461 if (cur_focus
== NULL
)
3465 wl
= &source
->ws
->winlist
;
3468 case SWM_ARG_ID_SWAPPREV
:
3469 if (source
->transient
)
3470 source
= find_window(source
->transient
);
3471 target
= TAILQ_PREV(source
, ws_win_list
, entry
);
3472 if (target
&& target
->transient
)
3473 target
= find_window(target
->transient
);
3474 TAILQ_REMOVE(wl
, source
, entry
);
3476 TAILQ_INSERT_TAIL(wl
, source
, entry
);
3478 TAILQ_INSERT_BEFORE(target
, source
, entry
);
3480 case SWM_ARG_ID_SWAPNEXT
:
3481 target
= TAILQ_NEXT(source
, entry
);
3482 /* move the parent and let the sort handle the move */
3483 if (source
->transient
)
3484 source
= find_window(source
->transient
);
3485 TAILQ_REMOVE(wl
, source
, entry
);
3487 TAILQ_INSERT_HEAD(wl
, source
, entry
);
3489 TAILQ_INSERT_AFTER(wl
, target
, source
, entry
);
3491 case SWM_ARG_ID_SWAPMAIN
:
3492 target
= TAILQ_FIRST(wl
);
3493 if (target
== source
) {
3494 if (source
->ws
->focus_prev
!= NULL
&&
3495 source
->ws
->focus_prev
!= target
)
3496 source
= source
->ws
->focus_prev
;
3500 if (target
== NULL
|| source
== NULL
)
3502 source
->ws
->focus_prev
= target
;
3503 TAILQ_REMOVE(wl
, target
, entry
);
3504 TAILQ_INSERT_BEFORE(source
, target
, entry
);
3505 TAILQ_REMOVE(wl
, source
, entry
);
3506 TAILQ_INSERT_HEAD(wl
, source
, entry
);
3508 case SWM_ARG_ID_MOVELAST
:
3509 TAILQ_REMOVE(wl
, source
, entry
);
3510 TAILQ_INSERT_TAIL(wl
, source
, entry
);
3513 DNPRINTF(SWM_D_MOVE
, "swapwin: invalid id: %d\n", args
->id
);
3525 get_focus_prev(struct ws_win
*win
)
3527 struct ws_win
*winfocus
= NULL
;
3528 struct ws_win
*cur_focus
= NULL
;
3529 struct ws_win_list
*wl
= NULL
;
3530 struct workspace
*ws
= NULL
;
3532 if (!(win
&& win
->ws
))
3537 cur_focus
= ws
->focus
;
3539 DNPRINTF(SWM_D_FOCUS
, "get_focus_prev: window: 0x%x, cur_focus: 0x%x\n",
3540 WINID(win
), WINID(cur_focus
));
3542 /* pickle, just focus on whatever */
3543 if (cur_focus
== NULL
) {
3544 /* use prev_focus if valid */
3545 if (ws
->focus_prev
&& ws
->focus_prev
!= cur_focus
&&
3546 find_window(WINID(ws
->focus_prev
)))
3547 winfocus
= ws
->focus_prev
;
3551 /* if transient focus on parent */
3552 if (cur_focus
->transient
) {
3553 winfocus
= find_window(cur_focus
->transient
);
3557 /* if in max_stack try harder */
3558 if ((win
->quirks
& SWM_Q_FOCUSPREV
) ||
3559 (ws
->cur_layout
->flags
& SWM_L_FOCUSPREV
)) {
3560 if (cur_focus
!= ws
->focus_prev
)
3561 winfocus
= ws
->focus_prev
;
3563 winfocus
= TAILQ_PREV(win
, ws_win_list
, entry
);
3568 DNPRINTF(SWM_D_FOCUS
, "get_focus_prev: focus_close: %d\n", focus_close
);
3570 if (winfocus
== NULL
|| winfocus
== win
) {
3571 switch (focus_close
) {
3572 case SWM_STACK_BOTTOM
:
3573 winfocus
= TAILQ_FIRST(wl
);
3576 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
3578 case SWM_STACK_ABOVE
:
3579 if ((winfocus
= TAILQ_NEXT(cur_focus
, entry
)) == NULL
) {
3580 if (focus_close_wrap
)
3581 winfocus
= TAILQ_FIRST(wl
);
3583 winfocus
= TAILQ_PREV(cur_focus
,
3584 ws_win_list
, entry
);
3587 case SWM_STACK_BELOW
:
3588 if ((winfocus
= TAILQ_PREV(cur_focus
, ws_win_list
,
3590 if (focus_close_wrap
)
3591 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
3593 winfocus
= TAILQ_NEXT(cur_focus
, entry
);
3599 if (winfocus
== NULL
) {
3600 if (focus_default
== SWM_STACK_TOP
)
3601 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
3603 winfocus
= TAILQ_FIRST(wl
);
3608 return get_focus_magic(winfocus
);
3612 get_region_focus(struct swm_region
*r
)
3614 struct ws_win
*winfocus
= NULL
;
3619 if (r
->ws
->focus
&& !r
->ws
->focus
->iconic
)
3620 winfocus
= r
->ws
->focus
;
3621 else if (r
->ws
->focus_prev
&& !r
->ws
->focus_prev
->iconic
)
3622 winfocus
= r
->ws
->focus_prev
;
3624 TAILQ_FOREACH(winfocus
, &r
->ws
->winlist
, entry
)
3625 if (!winfocus
->iconic
)
3628 return get_focus_magic(winfocus
);
3632 focus(struct swm_region
*r
, union arg
*args
)
3634 struct ws_win
*head
, *cur_focus
= NULL
, *winfocus
= NULL
;
3635 struct ws_win_list
*wl
= NULL
;
3636 struct workspace
*ws
= NULL
;
3642 DNPRINTF(SWM_D_FOCUS
, "focus: id: %d\n", args
->id
);
3644 if ((cur_focus
= r
->ws
->focus
) == NULL
)
3648 if (TAILQ_EMPTY(wl
))
3650 /* make sure there is at least one uniconified window */
3652 TAILQ_FOREACH(winfocus
, wl
, entry
)
3653 if (!winfocus
->iconic
) {
3661 case SWM_ARG_ID_FOCUSPREV
:
3662 head
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
3664 head
= TAILQ_LAST(wl
, ws_win_list
);
3666 if (WINID(winfocus
) == cur_focus
->transient
) {
3667 head
= TAILQ_PREV(winfocus
, ws_win_list
, entry
);
3669 head
= TAILQ_LAST(wl
, ws_win_list
);
3674 if (winfocus
&& winfocus
->iconic
) {
3675 while (winfocus
!= cur_focus
) {
3676 if (winfocus
== NULL
)
3677 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
3678 if (!winfocus
->iconic
)
3680 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
,
3686 case SWM_ARG_ID_FOCUSNEXT
:
3687 head
= TAILQ_NEXT(cur_focus
, entry
);
3689 head
= TAILQ_FIRST(wl
);
3693 if (winfocus
&& winfocus
->iconic
) {
3694 while (winfocus
!= cur_focus
) {
3695 if (winfocus
== NULL
)
3696 winfocus
= TAILQ_FIRST(wl
);
3697 if (!winfocus
->iconic
)
3699 winfocus
= TAILQ_NEXT(winfocus
, entry
);
3704 case SWM_ARG_ID_FOCUSMAIN
:
3705 winfocus
= TAILQ_FIRST(wl
);
3706 if (winfocus
== cur_focus
)
3707 winfocus
= cur_focus
->ws
->focus_prev
;
3714 focus_win(get_focus_magic(winfocus
));
3720 cycle_layout(struct swm_region
*r
, union arg
*args
)
3722 struct workspace
*ws
= r
->ws
;
3724 /* suppress unused warning since var is needed */
3727 DNPRINTF(SWM_D_EVENT
, "cycle_layout: workspace: %d\n", ws
->idx
);
3730 if (ws
->cur_layout
->l_stack
== NULL
)
3731 ws
->cur_layout
= &layouts
[0];
3736 focus_win(get_region_focus(r
));
3742 stack_config(struct swm_region
*r
, union arg
*args
)
3744 struct workspace
*ws
= r
->ws
;
3746 DNPRINTF(SWM_D_STACK
, "stack_config: id: %d workspace: %d\n",
3749 if (ws
->cur_layout
->l_config
!= NULL
)
3750 ws
->cur_layout
->l_config(ws
, args
->id
);
3752 if (args
->id
!= SWM_ARG_ID_STACKINIT
)
3761 struct swm_geometry g
;
3762 struct swm_region
*r
;
3768 DNPRINTF(SWM_D_STACK
, "stack: begin\n");
3770 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
3771 for (i
= 0; i
< num_screens
; i
++) {
3775 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
3776 DNPRINTF(SWM_D_STACK
, "stack: workspace: %d "
3777 "(screen: %d, region: %d)\n", r
->ws
->idx
, i
, j
++);
3779 /* start with screen geometry, adjust for bar */
3781 g
.w
-= 2 * border_width
;
3782 g
.h
-= 2 * border_width
;
3788 r
->ws
->cur_layout
->l_stack(r
->ws
, &g
);
3789 r
->ws
->cur_layout
->l_string(r
->ws
);
3790 /* save r so we can track region changes */
3797 DNPRINTF(SWM_D_STACK
, "stack: end\n");
3801 store_float_geom(struct ws_win
*win
, struct swm_region
*r
)
3803 if (win
== NULL
|| r
== NULL
)
3806 /* retain window geom and region geom */
3807 win
->g_float
= win
->g
;
3808 win
->g_float
.x
-= X(r
);
3809 win
->g_float
.y
-= Y(r
);
3810 win
->g_floatvalid
= 1;
3811 DNPRINTF(SWM_D_MISC
, "store_float_geom: window: 0x%x, g: (%d,%d)"
3812 " %d x %d, g_float: (%d,%d) %d x %d\n", win
->id
, X(win
), Y(win
),
3813 WIDTH(win
), HEIGHT(win
), win
->g_float
.x
, win
->g_float
.y
,
3814 win
->g_float
.w
, win
->g_float
.h
);
3818 load_float_geom(struct ws_win
*win
, struct swm_region
*r
)
3820 if (win
== NULL
|| r
== NULL
)
3823 if (win
->g_floatvalid
) {
3824 win
->g
= win
->g_float
;
3827 DNPRINTF(SWM_D_MISC
, "load_float_geom: window: 0x%x, g: (%d,%d)"
3828 "%d x %d\n", win
->id
, X(win
), Y(win
), WIDTH(win
),
3831 DNPRINTF(SWM_D_MISC
, "load_float_geom: window: 0x%x, g_float "
3832 "is not set.\n", win
->id
);
3837 stack_floater(struct ws_win
*win
, struct swm_region
*r
)
3842 DNPRINTF(SWM_D_MISC
, "stack_floater: window: 0x%x\n", win
->id
);
3845 * to allow windows to change their size (e.g. mplayer fs) only retrieve
3846 * geom on ws switches or return from max mode
3848 if (win
->floatmaxed
|| (r
!= r
->ws
->old_r
&&
3849 !(win
->ewmh_flags
& EWMH_F_FULLSCREEN
))) {
3850 /* update geometry for the new region */
3851 load_float_geom(win
, r
);
3854 win
->floatmaxed
= 0;
3857 * if set to fullscreen mode, configure window to maximum size.
3859 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
3860 if (!win
->g_floatvalid
)
3861 store_float_geom(win
, win
->ws
->r
);
3867 * remove border on fullscreen floater when in fullscreen mode or when
3868 * the quirk is present.
3870 if ((win
->ewmh_flags
& EWMH_F_FULLSCREEN
) ||
3871 ((win
->quirks
& SWM_Q_FULLSCREEN
) &&
3872 (WIDTH(win
) >= WIDTH(r
)) && (HEIGHT(win
) >= HEIGHT(r
)))) {
3873 if (win
->bordered
) {
3875 X(win
) += border_width
;
3876 Y(win
) += border_width
;
3878 } else if (!win
->bordered
) {
3880 X(win
) -= border_width
;
3881 Y(win
) -= border_width
;
3884 if (win
->transient
&& (win
->quirks
& SWM_Q_TRANSSZ
)) {
3885 WIDTH(win
) = (double)WIDTH(r
) * dialog_ratio
;
3886 HEIGHT(win
) = (double)HEIGHT(r
) * dialog_ratio
;
3889 if (!win
->manual
&& !(win
->ewmh_flags
& EWMH_F_FULLSCREEN
) &&
3890 !(win
->quirks
& SWM_Q_ANYWHERE
)) {
3892 * floaters and transients are auto-centred unless moved,
3893 * resized or ANYWHERE quirk is set.
3895 X(win
) = X(r
) + (WIDTH(r
) - WIDTH(win
)) / 2 - BORDER(win
);
3896 Y(win
) = Y(r
) + (HEIGHT(r
) - HEIGHT(win
)) / 2 - BORDER(win
);
3898 store_float_geom(win
, r
);
3901 /* keep window within region bounds */
3902 constrain_window(win
, r
, 0);
3908 * Send keystrokes to terminal to decrease/increase the font size as the
3909 * window size changes.
3912 adjust_font(struct ws_win
*win
)
3914 if (!(win
->quirks
& SWM_Q_XTERM_FONTADJ
) ||
3915 win
->floating
|| win
->transient
)
3918 if (win
->sh
.width_inc
&& win
->last_inc
!= win
->sh
.width_inc
&&
3919 WIDTH(win
) / win
->sh
.width_inc
< term_width
&&
3920 win
->font_steps
< SWM_MAX_FONT_STEPS
) {
3921 win
->font_size_boundary
[win
->font_steps
] =
3922 (win
->sh
.width_inc
* term_width
) + win
->sh
.base_width
;
3925 win
->last_inc
= win
->sh
.width_inc
;
3926 fake_keypress(win
, XK_KP_Subtract
, XCB_MOD_MASK_SHIFT
);
3927 } else if (win
->font_steps
&& win
->last_inc
!= win
->sh
.width_inc
&&
3928 WIDTH(win
) > win
->font_size_boundary
[win
->font_steps
- 1]) {
3931 win
->last_inc
= win
->sh
.width_inc
;
3932 fake_keypress(win
, XK_KP_Add
, XCB_MOD_MASK_SHIFT
);
3936 #define SWAPXY(g) do { \
3938 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
3939 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
3942 stack_master(struct workspace
*ws
, struct swm_geometry
*g
, int rot
, int flip
)
3944 struct swm_geometry win_g
, r_g
= *g
;
3945 struct ws_win
*win
, *fs_win
= NULL
;
3946 int i
, j
, s
, stacks
;
3947 int w_inc
= 1, h_inc
, w_base
= 1, h_base
;
3948 int hrh
, extra
= 0, h_slice
, last_h
= 0;
3949 int split
, colno
, winno
, mwin
, msize
, mscale
;
3950 int remain
, missing
, v_slice
, reconfigure
= 0;
3953 DNPRINTF(SWM_D_STACK
, "stack_master: workspace: %d, rot: %s, "
3954 "flip: %s\n", ws
->idx
, YESNO(rot
), YESNO(flip
));
3956 winno
= count_win(ws
, 0);
3957 if (winno
== 0 && count_win(ws
, 1) == 0)
3960 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
3961 if (!win
->transient
&& !win
->floating
&& !win
->iconic
)
3968 w_inc
= win
->sh
.width_inc
;
3969 w_base
= win
->sh
.base_width
;
3970 mwin
= ws
->l_state
.horizontal_mwin
;
3971 mscale
= ws
->l_state
.horizontal_msize
;
3972 stacks
= ws
->l_state
.horizontal_stacks
;
3975 w_inc
= win
->sh
.height_inc
;
3976 w_base
= win
->sh
.base_height
;
3977 mwin
= ws
->l_state
.vertical_mwin
;
3978 mscale
= ws
->l_state
.vertical_msize
;
3979 stacks
= ws
->l_state
.vertical_stacks
;
3983 if (stacks
> winno
- mwin
)
3984 stacks
= winno
- mwin
;
3988 h_slice
= r_g
.h
/ SWM_H_SLICE
;
3989 if (mwin
&& winno
> mwin
) {
3990 v_slice
= r_g
.w
/ SWM_V_SLICE
;
3994 win_g
.w
= v_slice
* mscale
;
3996 if (w_inc
> 1 && w_inc
< v_slice
) {
3997 /* adjust for window's requested size increment */
3998 remain
= (win_g
.w
- w_base
) % w_inc
;
4004 win_g
.x
+= r_g
.w
- msize
;
4007 colno
= split
= winno
/ stacks
;
4008 win_g
.w
= ((r_g
.w
- (stacks
* 2 * border_width
) +
4009 2 * border_width
) / stacks
);
4011 hrh
= r_g
.h
/ colno
;
4012 extra
= r_g
.h
- (colno
* hrh
);
4013 win_g
.h
= hrh
- 2 * border_width
;
4015 /* stack all the tiled windows */
4016 i
= j
= 0, s
= stacks
;
4017 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
4018 if (win
->transient
|| win
->floating
)
4023 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
4028 if (split
&& i
== split
) {
4029 colno
= (winno
- mwin
) / stacks
;
4030 if (s
<= (winno
- mwin
) % stacks
)
4032 split
= split
+ colno
;
4033 hrh
= (r_g
.h
/ colno
);
4034 extra
= r_g
.h
- (colno
* hrh
);
4038 win_g
.x
+= win_g
.w
+ 2 * border_width
;
4039 win_g
.w
= (r_g
.w
- msize
-
4040 (stacks
* 2 * border_width
)) / stacks
;
4042 win_g
.w
+= (r_g
.w
- msize
-
4043 (stacks
* 2 * border_width
)) % stacks
;
4047 win_g
.h
= hrh
- 2 * border_width
;
4049 h_inc
= win
->sh
.width_inc
;
4050 h_base
= win
->sh
.base_width
;
4052 h_inc
= win
->sh
.height_inc
;
4053 h_base
= win
->sh
.base_height
;
4055 if (j
== colno
- 1) {
4056 win_g
.h
= hrh
+ extra
;
4057 } else if (h_inc
> 1 && h_inc
< h_slice
) {
4058 /* adjust for window's requested size increment */
4059 remain
= (win_g
.h
- h_base
) % h_inc
;
4060 missing
= h_inc
- remain
;
4062 if (missing
<= extra
|| j
== 0) {
4074 win_g
.y
+= last_h
+ 2 * border_width
;
4076 if (disable_border
&& !bar_enabled
&& winno
== 1){
4078 win_g
.w
+= 2 * border_width
;
4079 win_g
.h
+= 2 * border_width
;
4084 if (X(win
) != win_g
.y
|| Y(win
) != win_g
.x
||
4085 WIDTH(win
) != win_g
.h
|| HEIGHT(win
) != win_g
.w
) {
4089 WIDTH(win
) = win_g
.h
;
4090 HEIGHT(win
) = win_g
.w
;
4093 if (X(win
) != win_g
.x
|| Y(win
) != win_g
.y
||
4094 WIDTH(win
) != win_g
.w
|| HEIGHT(win
) != win_g
.h
) {
4098 WIDTH(win
) = win_g
.w
;
4099 HEIGHT(win
) = win_g
.h
;
4103 if (bordered
!= win
->bordered
) {
4105 win
->bordered
= bordered
;
4121 /* now, stack all the floaters and transients */
4122 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
4123 if (!win
->transient
&& !win
->floating
)
4127 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
4132 stack_floater(win
, ws
->r
);
4136 /* Make sure fs_win is stacked last so it's on top. */
4138 stack_floater(fs_win
, ws
->r
);
4144 vertical_config(struct workspace
*ws
, int id
)
4146 DNPRINTF(SWM_D_STACK
, "vertical_config: id: %d, workspace: %d\n",
4150 case SWM_ARG_ID_STACKRESET
:
4151 case SWM_ARG_ID_STACKINIT
:
4152 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ 2;
4153 ws
->l_state
.vertical_mwin
= 1;
4154 ws
->l_state
.vertical_stacks
= 1;
4156 case SWM_ARG_ID_MASTERSHRINK
:
4157 if (ws
->l_state
.vertical_msize
> 1)
4158 ws
->l_state
.vertical_msize
--;
4160 case SWM_ARG_ID_MASTERGROW
:
4161 if (ws
->l_state
.vertical_msize
< SWM_V_SLICE
- 1)
4162 ws
->l_state
.vertical_msize
++;
4164 case SWM_ARG_ID_MASTERADD
:
4165 ws
->l_state
.vertical_mwin
++;
4167 case SWM_ARG_ID_MASTERDEL
:
4168 if (ws
->l_state
.vertical_mwin
> 0)
4169 ws
->l_state
.vertical_mwin
--;
4171 case SWM_ARG_ID_STACKINC
:
4172 ws
->l_state
.vertical_stacks
++;
4174 case SWM_ARG_ID_STACKDEC
:
4175 if (ws
->l_state
.vertical_stacks
> 1)
4176 ws
->l_state
.vertical_stacks
--;
4178 case SWM_ARG_ID_FLIPLAYOUT
:
4179 ws
->l_state
.vertical_flip
= !ws
->l_state
.vertical_flip
;
4187 vertical_stack(struct workspace
*ws
, struct swm_geometry
*g
)
4189 DNPRINTF(SWM_D_STACK
, "vertical_stack: workspace: %d\n", ws
->idx
);
4191 stack_master(ws
, g
, 0, ws
->l_state
.vertical_flip
);
4195 horizontal_config(struct workspace
*ws
, int id
)
4197 DNPRINTF(SWM_D_STACK
, "horizontal_config: workspace: %d\n", ws
->idx
);
4200 case SWM_ARG_ID_STACKRESET
:
4201 case SWM_ARG_ID_STACKINIT
:
4202 ws
->l_state
.horizontal_mwin
= 1;
4203 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ 2;
4204 ws
->l_state
.horizontal_stacks
= 1;
4206 case SWM_ARG_ID_MASTERSHRINK
:
4207 if (ws
->l_state
.horizontal_msize
> 1)
4208 ws
->l_state
.horizontal_msize
--;
4210 case SWM_ARG_ID_MASTERGROW
:
4211 if (ws
->l_state
.horizontal_msize
< SWM_H_SLICE
- 1)
4212 ws
->l_state
.horizontal_msize
++;
4214 case SWM_ARG_ID_MASTERADD
:
4215 ws
->l_state
.horizontal_mwin
++;
4217 case SWM_ARG_ID_MASTERDEL
:
4218 if (ws
->l_state
.horizontal_mwin
> 0)
4219 ws
->l_state
.horizontal_mwin
--;
4221 case SWM_ARG_ID_STACKINC
:
4222 ws
->l_state
.horizontal_stacks
++;
4224 case SWM_ARG_ID_STACKDEC
:
4225 if (ws
->l_state
.horizontal_stacks
> 1)
4226 ws
->l_state
.horizontal_stacks
--;
4228 case SWM_ARG_ID_FLIPLAYOUT
:
4229 ws
->l_state
.horizontal_flip
= !ws
->l_state
.horizontal_flip
;
4237 horizontal_stack(struct workspace
*ws
, struct swm_geometry
*g
)
4239 DNPRINTF(SWM_D_STACK
, "horizontal_stack: workspace: %d\n", ws
->idx
);
4241 stack_master(ws
, g
, 1, ws
->l_state
.horizontal_flip
);
4244 /* fullscreen view */
4246 max_stack(struct workspace
*ws
, struct swm_geometry
*g
)
4248 struct swm_geometry gg
= *g
;
4249 struct ws_win
*w
, *win
= NULL
, *parent
= NULL
;
4250 int winno
, num_screens
;
4252 DNPRINTF(SWM_D_STACK
, "max_stack: workspace: %d\n", ws
->idx
);
4257 winno
= count_win(ws
, 0);
4258 if (winno
== 0 && count_win(ws
, 1) == 0)
4261 /* Figure out which top level window should be visible. */
4262 if (ws
->focus_pending
)
4263 win
= ws
->focus_pending
;
4267 win
= TAILQ_FIRST(&ws
->winlist
);
4270 parent
= find_window(win
->transient
);
4272 DNPRINTF(SWM_D_STACK
, "max_stack: win: 0x%x\n", win
->id
);
4274 /* maximize all top level windows */
4275 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
4276 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
4277 if (w
->transient
|| w
->iconic
)
4280 if (w
->floating
&& !w
->floatmaxed
) {
4282 * retain geometry for retrieval on exit from
4285 store_float_geom(w
, ws
->r
);
4289 /* only reconfigure if necessary */
4290 if (X(w
) != gg
.x
|| Y(w
) != gg
.y
|| WIDTH(w
) != gg
.w
||
4291 HEIGHT(w
) != gg
.h
) {
4297 WIDTH(w
) += 2 * border_width
;
4298 HEIGHT(w
) += 2 * border_width
;
4304 /* Unmap unwanted windows if not multi-screen. */
4305 if (!(num_screens
> 1 || outputs
> 1) && (w
!= win
||
4306 w
!= parent
|| w
->transient
!= win
->id
))
4310 /* If a parent exists, map it first. */
4314 /* Map siblings next. */
4315 TAILQ_FOREACH(w
, &ws
->winlist
, entry
)
4316 if (w
!= win
&& !w
->iconic
&&
4317 w
->transient
== parent
->id
) {
4318 stack_floater(w
, ws
->r
);
4323 /* Map focused window. */
4326 /* Finally, map children of focus window. */
4327 TAILQ_FOREACH(w
, &ws
->winlist
, entry
)
4328 if (w
->transient
== win
->id
&& !w
->iconic
) {
4329 stack_floater(w
, ws
->r
);
4335 send_to_ws(struct swm_region
*r
, union arg
*args
)
4337 int wsid
= args
->id
;
4338 struct ws_win
*win
= NULL
, *parent
;
4339 struct workspace
*ws
, *nws
, *pws
;
4340 char ws_idx_str
[SWM_PROPLEN
];
4342 if (wsid
>= workspace_limit
)
4345 if (r
&& r
->ws
&& r
->ws
->focus
)
4350 if (win
->ws
->idx
== wsid
)
4353 DNPRINTF(SWM_D_MOVE
, "send_to_ws: win 0x%x, ws %d -> %d\n", win
->id
,
4354 win
->ws
->idx
, wsid
);
4357 nws
= &win
->s
->ws
[wsid
];
4359 /* Update the window's workspace property: _SWM_WS */
4360 if (snprintf(ws_idx_str
, SWM_PROPLEN
, "%d", nws
->idx
) < SWM_PROPLEN
) {
4361 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
4362 ws
->focus_pending
= get_focus_prev(win
);
4364 /* Move the parent if this is a transient window. */
4365 if (win
->transient
) {
4366 parent
= find_window(win
->transient
);
4369 /* Set new focus in parent's ws if needed. */
4370 if (pws
->focus
== parent
) {
4371 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
4372 pws
->focus_pending
=
4373 get_focus_prev(parent
);
4375 unfocus_win(parent
);
4377 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
4378 pws
->focus
= pws
->focus_pending
;
4380 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
4381 pws
->focus_pending
= NULL
;
4384 /* Don't unmap parent if new ws is visible */
4386 unmap_window(parent
);
4389 TAILQ_REMOVE(&ws
->winlist
, parent
, entry
);
4390 TAILQ_INSERT_TAIL(&nws
->winlist
, parent
, entry
);
4393 DNPRINTF(SWM_D_PROP
, "send_to_ws: set "
4394 "property: _SWM_WS: %s\n", ws_idx_str
);
4395 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
4396 parent
->id
, a_swm_ws
, XCB_ATOM_STRING
, 8,
4397 strlen(ws_idx_str
), ws_idx_str
);
4403 /* Don't unmap if new ws is visible */
4408 TAILQ_REMOVE(&ws
->winlist
, win
, entry
);
4409 TAILQ_INSERT_TAIL(&nws
->winlist
, win
, entry
);
4412 /* Set focus on new ws. */
4413 unfocus_win(nws
->focus
);
4416 DNPRINTF(SWM_D_PROP
, "send_to_ws: set property: _SWM_WS: %s\n",
4418 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
4419 a_swm_ws
, XCB_ATOM_STRING
, 8, strlen(ws_idx_str
),
4422 /* Restack and set new focus. */
4425 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
4426 focus_win(ws
->focus_pending
);
4427 ws
->focus_pending
= NULL
;
4433 DNPRINTF(SWM_D_MOVE
, "send_to_ws: done.\n");
4437 pressbutton(struct swm_region
*r
, union arg
*args
)
4439 /* suppress unused warning since var is needed */
4442 xcb_test_fake_input(conn
, XCB_BUTTON_PRESS
, args
->id
,
4443 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
4444 xcb_test_fake_input(conn
, XCB_BUTTON_RELEASE
, args
->id
,
4445 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
4449 raise_toggle(struct swm_region
*r
, union arg
*args
)
4451 /* suppress unused warning since var is needed */
4454 if (r
== NULL
|| r
->ws
== NULL
)
4457 r
->ws
->always_raise
= !r
->ws
->always_raise
;
4459 /* bring floaters back to top */
4460 if (!r
->ws
->always_raise
)
4467 iconify(struct swm_region
*r
, union arg
*args
)
4469 /* suppress unused warning since var is needed */
4472 if (r
->ws
->focus
== NULL
)
4475 set_swm_iconic(r
->ws
->focus
, 1);
4481 get_win_name(xcb_window_t win
)
4484 xcb_get_property_cookie_t c
;
4485 xcb_get_property_reply_t
*r
;
4487 /* First try _NET_WM_NAME for UTF-8. */
4488 c
= xcb_get_property(conn
, 0, win
, a_netwmname
,
4489 XCB_GET_PROPERTY_TYPE_ANY
, 0, UINT_MAX
);
4490 r
= xcb_get_property_reply(conn
, c
, NULL
);
4493 if (r
->type
== XCB_NONE
) {
4495 /* Use WM_NAME instead; no UTF-8. */
4496 c
= xcb_get_property(conn
, 0, win
, XCB_ATOM_WM_NAME
,
4497 XCB_GET_PROPERTY_TYPE_ANY
, 0, UINT_MAX
);
4498 r
= xcb_get_property_reply(conn
, c
, NULL
);
4502 if (r
->type
== XCB_NONE
) {
4508 name
= strndup(xcb_get_property_value(r
),
4509 xcb_get_property_value_length(r
));
4518 uniconify(struct swm_region
*r
, union arg
*args
)
4525 DNPRINTF(SWM_D_MISC
, "uniconify\n");
4527 if (r
== NULL
|| r
->ws
== NULL
)
4530 /* make sure we have anything to uniconify */
4531 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
4532 if (win
->ws
== NULL
)
4533 continue; /* should never happen */
4542 search_resp_action
= SWM_SEARCH_UNICONIFY
;
4544 spawn_select(r
, args
, "search", &searchpid
);
4546 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
4549 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
4550 if (win
->ws
== NULL
)
4551 continue; /* should never happen */
4555 name
= get_win_name(win
->id
);
4558 fprintf(lfile
, "%s.%u\n", name
, win
->id
);
4566 name_workspace(struct swm_region
*r
, union arg
*args
)
4570 DNPRINTF(SWM_D_MISC
, "name_workspace\n");
4576 search_resp_action
= SWM_SEARCH_NAME_WORKSPACE
;
4578 spawn_select(r
, args
, "name_workspace", &searchpid
);
4580 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
4583 fprintf(lfile
, "%s", "");
4588 search_workspace(struct swm_region
*r
, union arg
*args
)
4591 struct workspace
*ws
;
4594 DNPRINTF(SWM_D_MISC
, "search_workspace\n");
4600 search_resp_action
= SWM_SEARCH_SEARCH_WORKSPACE
;
4602 spawn_select(r
, args
, "search", &searchpid
);
4604 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
4607 for (i
= 0; i
< workspace_limit
; i
++) {
4611 fprintf(lfile
, "%d%s%s\n", ws
->idx
+ 1,
4612 (ws
->name
? ":" : ""), (ws
->name
? ws
->name
: ""));
4619 search_win_cleanup(void)
4621 struct search_window
*sw
= NULL
;
4623 while ((sw
= TAILQ_FIRST(&search_wl
)) != NULL
) {
4624 xcb_destroy_window(conn
, sw
->indicator
);
4625 TAILQ_REMOVE(&search_wl
, sw
, entry
);
4631 search_win(struct swm_region
*r
, union arg
*args
)
4633 struct ws_win
*win
= NULL
;
4634 struct search_window
*sw
= NULL
;
4637 int i
, width
, height
;
4645 XRectangle l_ibox
, l_lbox
;
4647 DNPRINTF(SWM_D_MISC
, "search_win\n");
4650 search_resp_action
= SWM_SEARCH_SEARCH_WINDOW
;
4652 spawn_select(r
, args
, "search", &searchpid
);
4654 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
4657 TAILQ_INIT(&search_wl
);
4660 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
4664 sw
= calloc(1, sizeof(struct search_window
));
4666 warn("search_win: calloc");
4668 search_win_cleanup();
4674 snprintf(s
, sizeof s
, "%d", i
);
4677 w
= xcb_generate_id(conn
);
4678 wa
[0] = r
->s
->c
[SWM_S_COLOR_FOCUS
].pixel
;
4679 wa
[1] = r
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
;
4681 if (bar_font_legacy
) {
4682 XmbTextExtents(bar_fs
, s
, len
, &l_ibox
, &l_lbox
);
4683 width
= l_lbox
.width
+ 4;
4684 height
= bar_fs_extents
->max_logical_extent
.height
+ 4;
4686 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
,
4688 width
= info
.width
+ 4;
4689 height
= bar_font
->height
+ 4;
4692 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, w
, win
->id
, 0, 0,
4693 width
, height
, 1, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
4694 XCB_COPY_FROM_PARENT
, XCB_CW_BACK_PIXEL
|
4695 XCB_CW_BORDER_PIXEL
, wa
);
4697 xcb_map_window(conn
, w
);
4700 TAILQ_INSERT_TAIL(&search_wl
, sw
, entry
);
4702 if (bar_font_legacy
) {
4703 l_gcv
.graphics_exposures
= 0;
4704 l_draw
= XCreateGC(display
, w
, 0, &l_gcv
);
4706 XSetForeground(display
, l_draw
,
4707 r
->s
->c
[SWM_S_COLOR_BAR
].pixel
);
4709 DRAWSTRING(display
, w
, bar_fs
, l_draw
, 2,
4710 (bar_fs_extents
->max_logical_extent
.height
-
4711 l_lbox
.height
) / 2 - l_lbox
.y
, s
, len
);
4713 XFreeGC(display
, l_draw
);
4716 draw
= XftDrawCreate(display
, w
,
4717 DefaultVisual(display
, r
->s
->idx
),
4718 DefaultColormap(display
, r
->s
->idx
));
4720 XftDrawStringUtf8(draw
, &bar_font_color
, bar_font
, 2,
4721 (HEIGHT(r
->bar
) + bar_font
->height
) / 2 -
4722 bar_font
->descent
, (FcChar8
*)s
, len
);
4724 XftDrawDestroy(draw
);
4727 DNPRINTF(SWM_D_MISC
, "search_win: mapped window: 0x%x\n", w
);
4729 fprintf(lfile
, "%d\n", i
);
4739 search_resp_uniconify(const char *resp
, unsigned long len
)
4745 DNPRINTF(SWM_D_MISC
, "search_resp_uniconify: resp: %s\n", resp
);
4747 TAILQ_FOREACH(win
, &search_r
->ws
->winlist
, entry
) {
4750 name
= get_win_name(win
->id
);
4753 if (asprintf(&s
, "%s.%u", name
, win
->id
) == -1) {
4758 if (strncmp(s
, resp
, len
) == 0) {
4759 /* XXX this should be a callback to generalize */
4760 set_swm_iconic(win
, 0);
4769 search_resp_name_workspace(const char *resp
, unsigned long len
)
4771 struct workspace
*ws
;
4773 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: resp: %s\n", resp
);
4775 if (search_r
->ws
== NULL
)
4780 free(search_r
->ws
->name
);
4781 search_r
->ws
->name
= NULL
;
4785 ws
->name
= strdup(resp
);
4786 if (ws
->name
== NULL
) {
4787 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: "
4788 "strdup: %s", strerror(errno
));
4795 search_resp_search_workspace(const char *resp
)
4802 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: resp: %s\n", resp
);
4806 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: strdup: %s",
4813 ws_idx
= (int)strtonum(q
, 1, workspace_limit
, &errstr
);
4815 DNPRINTF(SWM_D_MISC
, "workspace idx is %s: %s",
4822 switchws(search_r
, &a
);
4826 search_resp_search_window(const char *resp
)
4831 struct search_window
*sw
;
4833 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: resp: %s\n", resp
);
4837 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: strdup: %s",
4842 idx
= (int)strtonum(s
, 1, INT_MAX
, &errstr
);
4844 DNPRINTF(SWM_D_MISC
, "window idx is %s: %s",
4851 TAILQ_FOREACH(sw
, &search_wl
, entry
)
4852 if (idx
== sw
->idx
) {
4858 #define MAX_RESP_LEN 1024
4861 search_do_resp(void)
4867 DNPRINTF(SWM_D_MISC
, "search_do_resp:\n");
4872 if ((resp
= calloc(1, MAX_RESP_LEN
+ 1)) == NULL
) {
4873 warn("search: calloc");
4877 rbytes
= read(select_resp_pipe
[0], resp
, MAX_RESP_LEN
);
4879 warn("search: read error");
4882 resp
[rbytes
] = '\0';
4885 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
4886 * newline, so work around that by sanitizing the resp now.
4888 resp
[strcspn(resp
, "\n")] = '\0';
4891 switch (search_resp_action
) {
4892 case SWM_SEARCH_UNICONIFY
:
4893 search_resp_uniconify(resp
, len
);
4895 case SWM_SEARCH_NAME_WORKSPACE
:
4896 search_resp_name_workspace(resp
, len
);
4898 case SWM_SEARCH_SEARCH_WORKSPACE
:
4899 search_resp_search_workspace(resp
);
4901 case SWM_SEARCH_SEARCH_WINDOW
:
4902 search_resp_search_window(resp
);
4907 if (search_resp_action
== SWM_SEARCH_SEARCH_WINDOW
)
4908 search_win_cleanup();
4910 search_resp_action
= SWM_SEARCH_NONE
;
4911 close(select_resp_pipe
[0]);
4918 wkill(struct swm_region
*r
, union arg
*args
)
4920 DNPRINTF(SWM_D_MISC
, "wkill: id: %d\n", args
->id
);
4922 if (r
->ws
->focus
== NULL
)
4925 if (args
->id
== SWM_ARG_ID_KILLWINDOW
)
4926 xcb_kill_client(conn
, r
->ws
->focus
->id
);
4928 if (r
->ws
->focus
->can_delete
)
4929 client_msg(r
->ws
->focus
, a_delete
, 0);
4935 floating_toggle_win(struct ws_win
*win
)
4937 struct swm_region
*r
;
4947 /* reject floating toggles in max stack mode */
4948 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
4951 if (win
->floating
) {
4952 if (!win
->floatmaxed
) {
4953 /* retain position for refloat */
4954 store_float_geom(win
, r
);
4958 load_float_geom(win
, r
);
4962 ewmh_update_actions(win
);
4968 floating_toggle(struct swm_region
*r
, union arg
*args
)
4970 struct ws_win
*win
= r
->ws
->focus
;
4972 /* suppress unused warning since var is needed */
4978 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
4981 ewmh_update_win_state(win
, ewmh
[_NET_WM_STATE_ABOVE
].atom
,
4982 _NET_WM_STATE_TOGGLE
);
4986 if (win
== win
->ws
->focus
)
4993 constrain_window(struct ws_win
*win
, struct swm_region
*r
, int resizable
)
4995 if (MAX_X(win
) + BORDER(win
) > MAX_X(r
)) {
4997 WIDTH(win
) = MAX_X(r
) - X(win
) - BORDER(win
);
4999 X(win
) = MAX_X(r
)- WIDTH(win
) - BORDER(win
);
5002 if (X(win
) + BORDER(win
) < X(r
)) {
5004 WIDTH(win
) -= X(r
) - X(win
) - BORDER(win
);
5006 X(win
) = X(r
) - BORDER(win
);
5009 if (MAX_Y(win
) + BORDER(win
) > MAX_Y(r
)) {
5011 HEIGHT(win
) = MAX_Y(r
) - Y(win
) - BORDER(win
);
5013 Y(win
) = MAX_Y(r
) - HEIGHT(win
) - BORDER(win
);
5016 if (Y(win
) + BORDER(win
) < Y(r
)) {
5018 HEIGHT(win
) -= Y(r
) - Y(win
) - BORDER(win
);
5020 Y(win
) = Y(r
) - BORDER(win
);
5026 if (HEIGHT(win
) < 1)
5032 update_window(struct ws_win
*win
)
5037 mask
= XCB_CONFIG_WINDOW_X
| XCB_CONFIG_WINDOW_Y
|
5038 XCB_CONFIG_WINDOW_WIDTH
| XCB_CONFIG_WINDOW_HEIGHT
|
5039 XCB_CONFIG_WINDOW_BORDER_WIDTH
;
5043 wc
[3] = HEIGHT(win
);
5044 wc
[4] = BORDER(win
);
5046 DNPRINTF(SWM_D_EVENT
, "update_window: window: 0x%x, (x,y) w x h: "
5047 "(%d,%d) %d x %d, bordered: %s\n", win
->id
, wc
[0], wc
[1], wc
[2],
5048 wc
[3], YESNO(win
->bordered
));
5050 xcb_configure_window(conn
, win
->id
, mask
, wc
);
5053 #define SWM_RESIZE_STEPS (50)
5056 resize(struct ws_win
*win
, union arg
*args
)
5058 xcb_timestamp_t timestamp
= 0;
5059 struct swm_region
*r
= NULL
;
5061 struct swm_geometry g
;
5062 int top
= 0, left
= 0, resizing
;
5064 xcb_cursor_t cursor
;
5065 xcb_query_pointer_reply_t
*xpr
;
5066 xcb_generic_event_t
*evt
;
5067 xcb_motion_notify_event_t
*mne
;
5073 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
5076 DNPRINTF(SWM_D_EVENT
, "resize: window: 0x%x, floating: %s, "
5077 "transient: 0x%x\n", win
->id
, YESNO(win
->floating
),
5080 if (!win
->transient
&& !win
->floating
)
5083 /* reject resizes in max mode for floaters (transient ok) */
5084 if (win
->floatmaxed
)
5088 ewmh_update_win_state(win
, ewmh
[_SWM_WM_STATE_MANUAL
].atom
,
5096 case SWM_ARG_ID_WIDTHSHRINK
:
5097 WIDTH(win
) -= SWM_RESIZE_STEPS
;
5100 case SWM_ARG_ID_WIDTHGROW
:
5101 WIDTH(win
) += SWM_RESIZE_STEPS
;
5104 case SWM_ARG_ID_HEIGHTSHRINK
:
5105 HEIGHT(win
) -= SWM_RESIZE_STEPS
;
5108 case SWM_ARG_ID_HEIGHTGROW
:
5109 HEIGHT(win
) += SWM_RESIZE_STEPS
;
5116 constrain_window(win
, r
, 1);
5118 store_float_geom(win
,r
);
5122 /* get cursor offset from window root */
5123 xpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
5130 if (xpr
->win_x
< WIDTH(win
) / 2)
5133 if (xpr
->win_y
< HEIGHT(win
) / 2)
5136 if (args
->id
== SWM_ARG_ID_CENTER
)
5137 cursor
= cursors
[XC_SIZING
].cid
;
5139 cursor
= cursors
[left
? XC_TOP_LEFT_CORNER
:
5140 XC_TOP_RIGHT_CORNER
].cid
;
5142 cursor
= cursors
[left
? XC_BOTTOM_LEFT_CORNER
:
5143 XC_BOTTOM_RIGHT_CORNER
].cid
;
5145 xcb_grab_pointer(conn
, 0, win
->id
, MOUSEMASK
,
5146 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
, XCB_WINDOW_NONE
, cursor
,
5151 while ((evt
= xcb_wait_for_event(conn
)) && resizing
) {
5152 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
5153 case XCB_BUTTON_RELEASE
:
5154 DNPRINTF(SWM_D_EVENT
, "resize: BUTTON_RELEASE\n");
5157 case XCB_MOTION_NOTIFY
:
5158 mne
= (xcb_motion_notify_event_t
*)evt
;
5159 /* cursor offset/delta from start of the operation */
5160 dx
= mne
->root_x
- xpr
->root_x
;
5161 dy
= mne
->root_y
- xpr
->root_y
;
5167 if (args
->id
== SWM_ARG_ID_CENTER
) {
5168 if (g
.h
/ 2 + dy
< 1)
5172 HEIGHT(win
) = g
.h
+ 2 * dy
;
5180 HEIGHT(win
) = g
.h
+ dy
;
5187 if (args
->id
== SWM_ARG_ID_CENTER
) {
5188 if (g
.w
/ 2 + dx
< 1)
5192 WIDTH(win
) = g
.w
+ 2 * dx
;
5200 WIDTH(win
) = g
.w
+ dx
;
5203 constrain_window(win
, r
, 1);
5205 /* not free, don't sync more than 120 times / second */
5206 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
5207 timestamp
= mne
->time
;
5222 store_float_geom(win
,r
);
5224 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
5226 DNPRINTF(SWM_D_EVENT
, "resize: done.\n");
5230 resize_step(struct swm_region
*r
, union arg
*args
)
5232 struct ws_win
*win
= NULL
;
5234 if (r
&& r
->ws
&& r
->ws
->focus
)
5243 #define SWM_MOVE_STEPS (50)
5246 move(struct ws_win
*win
, union arg
*args
)
5248 xcb_timestamp_t timestamp
= 0;
5249 int move_stp
= 0, moving
;
5250 struct swm_region
*r
= NULL
;
5251 xcb_query_pointer_reply_t
*qpr
;
5252 xcb_generic_event_t
*evt
;
5253 xcb_motion_notify_event_t
*mne
;
5259 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
5262 DNPRINTF(SWM_D_EVENT
, "move: window: 0x%x, floating: %s, transient: "
5263 "0x%x\n", win
->id
, YESNO(win
->floating
), win
->transient
);
5265 /* in max_stack mode should only move transients */
5266 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
] && !win
->transient
)
5270 if (!win
->floating
&& !win
->transient
) {
5271 store_float_geom(win
, r
);
5272 ewmh_update_win_state(win
, ewmh
[_NET_WM_STATE_ABOVE
].atom
,
5275 ewmh_update_win_state(win
, ewmh
[_SWM_WM_STATE_MANUAL
].atom
,
5284 case SWM_ARG_ID_MOVELEFT
:
5285 X(win
) -= (SWM_MOVE_STEPS
- border_width
);
5288 case SWM_ARG_ID_MOVERIGHT
:
5289 X(win
) += (SWM_MOVE_STEPS
- border_width
);
5292 case SWM_ARG_ID_MOVEUP
:
5293 Y(win
) -= (SWM_MOVE_STEPS
- border_width
);
5296 case SWM_ARG_ID_MOVEDOWN
:
5297 Y(win
) += (SWM_MOVE_STEPS
- border_width
);
5304 constrain_window(win
, r
, 0);
5306 store_float_geom(win
, r
);
5310 xcb_grab_pointer(conn
, 0, win
->id
, MOUSEMASK
,
5311 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
,
5312 XCB_WINDOW_NONE
, cursors
[XC_FLEUR
].cid
, XCB_CURRENT_TIME
);
5314 /* get cursor offset from window root */
5315 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
5318 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
5324 while ((evt
= xcb_wait_for_event(conn
)) && moving
) {
5325 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
5326 case XCB_BUTTON_RELEASE
:
5327 DNPRINTF(SWM_D_EVENT
, "move: BUTTON_RELEASE\n");
5330 case XCB_MOTION_NOTIFY
:
5331 mne
= (xcb_motion_notify_event_t
*)evt
;
5332 X(win
) = mne
->root_x
- qpr
->win_x
- border_width
;
5333 Y(win
) = mne
->root_y
- qpr
->win_y
- border_width
;
5335 constrain_window(win
, r
, 0);
5337 /* not free, don't sync more than 120 times / second */
5338 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
5339 timestamp
= mne
->time
;
5354 store_float_geom(win
, r
);
5356 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
5357 DNPRINTF(SWM_D_EVENT
, "move: done.\n");
5361 move_step(struct swm_region
*r
, union arg
*args
)
5363 struct ws_win
*win
= NULL
;
5365 if (r
&& r
->ws
&& r
->ws
->focus
)
5370 if (!win
->transient
&& !win
->floating
)
5377 /* key definitions */
5379 char name
[SWM_FUNCNAME_LEN
];
5380 void (*func
)(struct swm_region
*r
, union arg
*);
5382 } keyfuncs
[KF_INVALID
+ 1] = {
5383 /* name function argument */
5384 { "bar_toggle", bar_toggle
, {0} },
5385 { "button2", pressbutton
, {2} },
5386 { "cycle_layout", cycle_layout
, {0} },
5387 { "flip_layout", stack_config
, {.id
= SWM_ARG_ID_FLIPLAYOUT
} },
5388 { "float_toggle", floating_toggle
,{0} },
5389 { "focus_main", focus
, {.id
= SWM_ARG_ID_FOCUSMAIN
} },
5390 { "focus_next", focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
5391 { "focus_prev", focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
5392 { "height_grow", resize_step
, {.id
= SWM_ARG_ID_HEIGHTGROW
} },
5393 { "height_shrink", resize_step
, {.id
= SWM_ARG_ID_HEIGHTSHRINK
} },
5394 { "iconify", iconify
, {0} },
5395 { "master_shrink", stack_config
, {.id
= SWM_ARG_ID_MASTERSHRINK
} },
5396 { "master_grow", stack_config
, {.id
= SWM_ARG_ID_MASTERGROW
} },
5397 { "master_add", stack_config
, {.id
= SWM_ARG_ID_MASTERADD
} },
5398 { "master_del", stack_config
, {.id
= SWM_ARG_ID_MASTERDEL
} },
5399 { "move_down", move_step
, {.id
= SWM_ARG_ID_MOVEDOWN
} },
5400 { "move_left", move_step
, {.id
= SWM_ARG_ID_MOVELEFT
} },
5401 { "move_right", move_step
, {.id
= SWM_ARG_ID_MOVERIGHT
} },
5402 { "move_up", move_step
, {.id
= SWM_ARG_ID_MOVEUP
} },
5403 { "mvws_1", send_to_ws
, {.id
= 0} },
5404 { "mvws_2", send_to_ws
, {.id
= 1} },
5405 { "mvws_3", send_to_ws
, {.id
= 2} },
5406 { "mvws_4", send_to_ws
, {.id
= 3} },
5407 { "mvws_5", send_to_ws
, {.id
= 4} },
5408 { "mvws_6", send_to_ws
, {.id
= 5} },
5409 { "mvws_7", send_to_ws
, {.id
= 6} },
5410 { "mvws_8", send_to_ws
, {.id
= 7} },
5411 { "mvws_9", send_to_ws
, {.id
= 8} },
5412 { "mvws_10", send_to_ws
, {.id
= 9} },
5413 { "mvws_11", send_to_ws
, {.id
= 10} },
5414 { "mvws_12", send_to_ws
, {.id
= 11} },
5415 { "mvws_13", send_to_ws
, {.id
= 12} },
5416 { "mvws_14", send_to_ws
, {.id
= 13} },
5417 { "mvws_15", send_to_ws
, {.id
= 14} },
5418 { "mvws_16", send_to_ws
, {.id
= 15} },
5419 { "mvws_17", send_to_ws
, {.id
= 16} },
5420 { "mvws_18", send_to_ws
, {.id
= 17} },
5421 { "mvws_19", send_to_ws
, {.id
= 18} },
5422 { "mvws_20", send_to_ws
, {.id
= 19} },
5423 { "mvws_21", send_to_ws
, {.id
= 20} },
5424 { "mvws_22", send_to_ws
, {.id
= 21} },
5425 { "name_workspace", name_workspace
, {0} },
5426 { "quit", quit
, {0} },
5427 { "raise_toggle", raise_toggle
, {0} },
5428 { "restart", restart
, {0} },
5429 { "screen_next", cyclescr
, {.id
= SWM_ARG_ID_CYCLESC_UP
} },
5430 { "screen_prev", cyclescr
, {.id
= SWM_ARG_ID_CYCLESC_DOWN
} },
5431 { "search_win", search_win
, {0} },
5432 { "search_workspace", search_workspace
, {0} },
5433 { "spawn_custom", NULL
, {0} },
5434 { "stack_inc", stack_config
, {.id
= SWM_ARG_ID_STACKINC
} },
5435 { "stack_dec", stack_config
, {.id
= SWM_ARG_ID_STACKDEC
} },
5436 { "stack_reset", stack_config
, {.id
= SWM_ARG_ID_STACKRESET
} },
5437 { "swap_main", swapwin
, {.id
= SWM_ARG_ID_SWAPMAIN
} },
5438 { "swap_next", swapwin
, {.id
= SWM_ARG_ID_SWAPNEXT
} },
5439 { "swap_prev", swapwin
, {.id
= SWM_ARG_ID_SWAPPREV
} },
5440 { "uniconify", uniconify
, {0} },
5441 { "version", version
, {0} },
5442 { "width_grow", resize_step
, {.id
= SWM_ARG_ID_WIDTHGROW
} },
5443 { "width_shrink", resize_step
, {.id
= SWM_ARG_ID_WIDTHSHRINK
} },
5444 { "wind_del", wkill
, {.id
= SWM_ARG_ID_DELETEWINDOW
} },
5445 { "wind_kill", wkill
, {.id
= SWM_ARG_ID_KILLWINDOW
} },
5446 { "ws_1", switchws
, {.id
= 0} },
5447 { "ws_2", switchws
, {.id
= 1} },
5448 { "ws_3", switchws
, {.id
= 2} },
5449 { "ws_4", switchws
, {.id
= 3} },
5450 { "ws_5", switchws
, {.id
= 4} },
5451 { "ws_6", switchws
, {.id
= 5} },
5452 { "ws_7", switchws
, {.id
= 6} },
5453 { "ws_8", switchws
, {.id
= 7} },
5454 { "ws_9", switchws
, {.id
= 8} },
5455 { "ws_10", switchws
, {.id
= 9} },
5456 { "ws_11", switchws
, {.id
= 10} },
5457 { "ws_12", switchws
, {.id
= 11} },
5458 { "ws_13", switchws
, {.id
= 12} },
5459 { "ws_14", switchws
, {.id
= 13} },
5460 { "ws_15", switchws
, {.id
= 14} },
5461 { "ws_16", switchws
, {.id
= 15} },
5462 { "ws_17", switchws
, {.id
= 16} },
5463 { "ws_18", switchws
, {.id
= 17} },
5464 { "ws_19", switchws
, {.id
= 18} },
5465 { "ws_20", switchws
, {.id
= 19} },
5466 { "ws_21", switchws
, {.id
= 20} },
5467 { "ws_22", switchws
, {.id
= 21} },
5468 { "ws_next", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP
} },
5469 { "ws_next_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP_ALL
} },
5470 { "ws_prev", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN
} },
5471 { "ws_prev_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN_ALL
} },
5472 { "ws_prior", priorws
, {0} },
5473 { "dumpwins", dumpwins
, {0} }, /* MUST BE LAST */
5474 { "invalid key func", NULL
, {0} },
5478 key_cmp(struct key
*kp1
, struct key
*kp2
)
5480 if (kp1
->keysym
< kp2
->keysym
)
5482 if (kp1
->keysym
> kp2
->keysym
)
5485 if (kp1
->mod
< kp2
->mod
)
5487 if (kp1
->mod
> kp2
->mod
)
5494 enum { client_click
, root_click
};
5496 unsigned int action
;
5498 unsigned int button
;
5499 void (*func
)(struct ws_win
*, union arg
*);
5502 #define MODKEY_SHIFT MODKEY | XCB_MOD_MASK_SHIFT
5503 /* action key mouse button func args */
5504 { client_click
, MODKEY
, XCB_BUTTON_INDEX_3
, resize
, {.id
= SWM_ARG_ID_DONTCENTER
} },
5505 { client_click
, MODKEY_SHIFT
, XCB_BUTTON_INDEX_3
, resize
, {.id
= SWM_ARG_ID_CENTER
} },
5506 { client_click
, MODKEY
, XCB_BUTTON_INDEX_1
, move
, {0} },
5511 update_modkey(unsigned int mod
)
5517 RB_FOREACH(kp
, key_tree
, &keys
)
5518 if (kp
->mod
& XCB_MOD_MASK_SHIFT
)
5519 kp
->mod
= mod
| XCB_MOD_MASK_SHIFT
;
5523 for (i
= 0; i
< LENGTH(buttons
); i
++)
5524 if (buttons
[i
].mask
& XCB_MOD_MASK_SHIFT
)
5525 buttons
[i
].mask
= mod
| XCB_MOD_MASK_SHIFT
;
5527 buttons
[i
].mask
= mod
;
5531 spawn_expand(struct swm_region
*r
, union arg
*args
, const char *spawn_name
,
5534 struct spawn_prog
*prog
= NULL
;
5536 char *ap
, **real_args
;
5538 /* suppress unused warning since var is needed */
5541 DNPRINTF(SWM_D_SPAWN
, "spawn_expand: %s\n", spawn_name
);
5544 TAILQ_FOREACH(prog
, &spawns
, entry
) {
5545 if (!strcasecmp(spawn_name
, prog
->name
))
5549 warnx("spawn_custom: program %s not found", spawn_name
);
5553 /* make room for expanded args */
5554 if ((real_args
= calloc(prog
->argc
+ 1, sizeof(char *))) == NULL
)
5555 err(1, "spawn_custom: calloc real_args");
5557 /* expand spawn_args into real_args */
5558 for (i
= 0; i
< prog
->argc
; i
++) {
5560 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: raw arg: %s\n", ap
);
5561 if (!strcasecmp(ap
, "$bar_border")) {
5563 strdup(r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].name
))
5565 err(1, "spawn_custom border color");
5566 } else if (!strcasecmp(ap
, "$bar_color")) {
5568 strdup(r
->s
->c
[SWM_S_COLOR_BAR
].name
))
5570 err(1, "spawn_custom bar color");
5571 } else if (!strcasecmp(ap
, "$bar_font")) {
5572 if ((real_args
[i
] = strdup(bar_fonts
))
5574 err(1, "spawn_custom bar fonts");
5575 } else if (!strcasecmp(ap
, "$bar_font_color")) {
5577 strdup(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].name
))
5579 err(1, "spawn_custom color font");
5580 } else if (!strcasecmp(ap
, "$color_focus")) {
5582 strdup(r
->s
->c
[SWM_S_COLOR_FOCUS
].name
))
5584 err(1, "spawn_custom color focus");
5585 } else if (!strcasecmp(ap
, "$color_unfocus")) {
5587 strdup(r
->s
->c
[SWM_S_COLOR_UNFOCUS
].name
))
5589 err(1, "spawn_custom color unfocus");
5591 /* no match --> copy as is */
5592 if ((real_args
[i
] = strdup(ap
)) == NULL
)
5593 err(1, "spawn_custom strdup(ap)");
5595 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: cooked arg: %s\n",
5600 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: result: ");
5601 for (i
= 0; i
< prog
->argc
; i
++)
5602 DNPRINTF(SWM_D_SPAWN
, "\"%s\" ", real_args
[i
]);
5603 DNPRINTF(SWM_D_SPAWN
, "\n");
5605 *ret_args
= real_args
;
5606 return (prog
->argc
);
5610 spawn_custom(struct swm_region
*r
, union arg
*args
, const char *spawn_name
)
5616 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
5620 spawn(r
->ws
->idx
, &a
, 1);
5622 for (i
= 0; i
< spawn_argc
; i
++)
5628 spawn_select(struct swm_region
*r
, union arg
*args
, const char *spawn_name
,
5635 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
5639 if (pipe(select_list_pipe
) == -1)
5640 err(1, "pipe error");
5641 if (pipe(select_resp_pipe
) == -1)
5642 err(1, "pipe error");
5644 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
5645 err(1, "could not disable SIGPIPE");
5646 switch (*pid
= fork()) {
5648 err(1, "cannot fork");
5651 if (dup2(select_list_pipe
[0], 0) == -1)
5653 if (dup2(select_resp_pipe
[1], 1) == -1)
5655 close(select_list_pipe
[1]);
5656 close(select_resp_pipe
[0]);
5657 spawn(r
->ws
->idx
, &a
, 0);
5659 default: /* parent */
5660 close(select_list_pipe
[0]);
5661 close(select_resp_pipe
[1]);
5665 for (i
= 0; i
< spawn_argc
; i
++)
5671 spawn_insert(const char *name
, const char *args
)
5673 char *arg
, *cp
, *ptr
;
5674 struct spawn_prog
*sp
;
5676 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: %s\n", name
);
5678 if ((sp
= calloc(1, sizeof *sp
)) == NULL
)
5679 err(1, "spawn_insert: calloc");
5680 if ((sp
->name
= strdup(name
)) == NULL
)
5681 err(1, "spawn_insert: strdup");
5683 /* convert the arguments to an argument list */
5684 if ((ptr
= cp
= strdup(args
)) == NULL
)
5685 err(1, "spawn_insert: strdup");
5686 while ((arg
= strsep(&ptr
, " \t")) != NULL
) {
5687 /* empty field; skip it */
5692 if ((sp
->argv
= realloc(sp
->argv
, sp
->argc
*
5693 sizeof *sp
->argv
)) == NULL
)
5694 err(1, "spawn_insert: realloc");
5695 if ((sp
->argv
[sp
->argc
- 1] = strdup(arg
)) == NULL
)
5696 err(1, "spawn_insert: strdup");
5700 TAILQ_INSERT_TAIL(&spawns
, sp
, entry
);
5701 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: leave\n");
5705 spawn_remove(struct spawn_prog
*sp
)
5709 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: %s\n", sp
->name
);
5711 TAILQ_REMOVE(&spawns
, sp
, entry
);
5712 for (i
= 0; i
< sp
->argc
; i
++)
5718 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: leave\n");
5722 spawn_replace(struct spawn_prog
*sp
, const char *name
, const char *args
)
5724 DNPRINTF(SWM_D_SPAWN
, "spawn_replace: %s [%s]\n", sp
->name
, name
);
5727 spawn_insert(name
, args
);
5729 DNPRINTF(SWM_D_SPAWN
, "spawn_replace: leave\n");
5733 setspawn(const char *name
, const char *args
)
5735 struct spawn_prog
*sp
;
5737 DNPRINTF(SWM_D_SPAWN
, "setspawn: %s\n", name
);
5742 TAILQ_FOREACH(sp
, &spawns
, entry
) {
5743 if (!strcmp(sp
->name
, name
)) {
5747 spawn_replace(sp
, name
, args
);
5748 DNPRINTF(SWM_D_SPAWN
, "setspawn: leave\n");
5752 if (*args
== '\0') {
5753 warnx("error: setspawn: cannot find program: %s", name
);
5757 spawn_insert(name
, args
);
5758 DNPRINTF(SWM_D_SPAWN
, "setspawn: leave\n");
5762 setconfspawn(char *selector
, char *value
, int flags
)
5766 /* suppress unused warning since var is needed */
5769 args
= expand_tilde(value
);
5771 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: [%s] [%s]\n", selector
, args
);
5773 setspawn(selector
, args
);
5776 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: done.\n");
5783 setconfspawn("term", "xterm", 0);
5784 setconfspawn("spawn_term", "xterm", 0);
5785 setconfspawn("screenshot_all", "screenshot.sh full", 0);
5786 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
5787 setconfspawn("lock", "xlock", 0);
5788 setconfspawn("initscr", "initscreen.sh", 0);
5789 setconfspawn("menu", "dmenu_run"
5792 " -nf $bar_font_color"
5794 " -sf $bar_color", 0);
5795 setconfspawn("search", "dmenu"
5799 " -nf $bar_font_color"
5801 " -sf $bar_color", 0);
5802 setconfspawn("name_workspace", "dmenu"
5806 " -nf $bar_font_color"
5808 " -sf $bar_color", 0);
5812 #define SWM_MODNAME_SIZE 32
5813 #define SWM_KEY_WS "\n+ \t"
5815 parsekeys(char *keystr
, unsigned int currmod
, unsigned int *mod
, KeySym
*ks
)
5819 DNPRINTF(SWM_D_KEY
, "parsekeys: enter [%s]\n", keystr
);
5820 if (mod
== NULL
|| ks
== NULL
) {
5821 DNPRINTF(SWM_D_KEY
, "parsekeys: no mod or key vars\n");
5824 if (keystr
== NULL
|| strlen(keystr
) == 0) {
5825 DNPRINTF(SWM_D_KEY
, "parsekeys: no keystr\n");
5829 *ks
= XCB_NO_SYMBOL
;
5831 while ((name
= strsep(&cp
, SWM_KEY_WS
)) != NULL
) {
5832 DNPRINTF(SWM_D_KEY
, "parsekeys: key [%s]\n", name
);
5834 cp
+= (long)strspn(cp
, SWM_KEY_WS
);
5835 if (strncasecmp(name
, "MOD", SWM_MODNAME_SIZE
) == 0)
5837 else if (!strncasecmp(name
, "Mod1", SWM_MODNAME_SIZE
))
5838 *mod
|= XCB_MOD_MASK_1
;
5839 else if (!strncasecmp(name
, "Mod2", SWM_MODNAME_SIZE
))
5840 *mod
+= XCB_MOD_MASK_2
;
5841 else if (!strncmp(name
, "Mod3", SWM_MODNAME_SIZE
))
5842 *mod
|= XCB_MOD_MASK_3
;
5843 else if (!strncmp(name
, "Mod4", SWM_MODNAME_SIZE
))
5844 *mod
|= XCB_MOD_MASK_4
;
5845 else if (strncasecmp(name
, "SHIFT", SWM_MODNAME_SIZE
) == 0)
5846 *mod
|= XCB_MOD_MASK_SHIFT
;
5847 else if (strncasecmp(name
, "CONTROL", SWM_MODNAME_SIZE
) == 0)
5848 *mod
|= XCB_MOD_MASK_CONTROL
;
5850 *ks
= XStringToKeysym(name
);
5851 XConvertCase(*ks
, ks
, &uks
);
5852 if (ks
== XCB_NO_SYMBOL
) {
5854 "parsekeys: invalid key %s\n",
5860 DNPRINTF(SWM_D_KEY
, "parsekeys: leave ok\n");
5865 strdupsafe(const char *str
)
5870 return (strdup(str
));
5874 key_insert(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
5875 const char *spawn_name
)
5879 DNPRINTF(SWM_D_KEY
, "key_insert: enter %s [%s]\n",
5880 keyfuncs
[kfid
].name
, spawn_name
);
5882 if ((kp
= malloc(sizeof *kp
)) == NULL
)
5883 err(1, "key_insert: malloc");
5888 kp
->spawn_name
= strdupsafe(spawn_name
);
5889 RB_INSERT(key_tree
, &keys
, kp
);
5891 DNPRINTF(SWM_D_KEY
, "key_insert: leave\n");
5895 key_lookup(unsigned int mod
, KeySym ks
)
5902 return (RB_FIND(key_tree
, &keys
, &kp
));
5906 key_remove(struct key
*kp
)
5908 DNPRINTF(SWM_D_KEY
, "key_remove: %s\n", keyfuncs
[kp
->funcid
].name
);
5910 RB_REMOVE(key_tree
, &keys
, kp
);
5911 free(kp
->spawn_name
);
5914 DNPRINTF(SWM_D_KEY
, "key_remove: leave\n");
5918 key_replace(struct key
*kp
, unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
5919 const char *spawn_name
)
5921 DNPRINTF(SWM_D_KEY
, "key_replace: %s [%s]\n", keyfuncs
[kp
->funcid
].name
,
5925 key_insert(mod
, ks
, kfid
, spawn_name
);
5927 DNPRINTF(SWM_D_KEY
, "key_replace: leave\n");
5931 setkeybinding(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
5932 const char *spawn_name
)
5936 DNPRINTF(SWM_D_KEY
, "setkeybinding: enter %s [%s]\n",
5937 keyfuncs
[kfid
].name
, spawn_name
);
5939 if ((kp
= key_lookup(mod
, ks
)) != NULL
) {
5940 if (kfid
== KF_INVALID
)
5943 key_replace(kp
, mod
, ks
, kfid
, spawn_name
);
5944 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
5947 if (kfid
== KF_INVALID
) {
5948 warnx("bind: Key combination already unbound.");
5949 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
5953 key_insert(mod
, ks
, kfid
, spawn_name
);
5954 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
5958 setconfbinding(char *selector
, char *value
, int flags
)
5960 enum keyfuncid kfid
;
5963 struct spawn_prog
*sp
;
5965 /* suppress unused warning since var is needed */
5968 DNPRINTF(SWM_D_KEY
, "setconfbinding: enter\n");
5969 if (selector
== NULL
) {
5970 DNPRINTF(SWM_D_KEY
, "setconfbinding: unbind %s\n", value
);
5971 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
5973 setkeybinding(mod
, ks
, kfid
, NULL
);
5978 /* search by key function name */
5979 for (kfid
= 0; kfid
< KF_INVALID
; (kfid
)++) {
5980 if (strncasecmp(selector
, keyfuncs
[kfid
].name
,
5981 SWM_FUNCNAME_LEN
) == 0) {
5982 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match\n",
5984 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
5985 setkeybinding(mod
, ks
, kfid
, NULL
);
5991 /* search by custom spawn name */
5992 TAILQ_FOREACH(sp
, &spawns
, entry
) {
5993 if (strcasecmp(selector
, sp
->name
) == 0) {
5994 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match\n",
5996 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
5997 setkeybinding(mod
, ks
, KF_SPAWN_CUSTOM
,
6004 DNPRINTF(SWM_D_KEY
, "setconfbinding: no match\n");
6011 #define MODKEY_SHIFT MODKEY | XCB_MOD_MASK_SHIFT
6012 setkeybinding(MODKEY
, XK_space
, KF_CYCLE_LAYOUT
,NULL
);
6013 setkeybinding(MODKEY_SHIFT
, XK_backslash
, KF_FLIP_LAYOUT
, NULL
);
6014 setkeybinding(MODKEY_SHIFT
, XK_space
, KF_STACK_RESET
, NULL
);
6015 setkeybinding(MODKEY
, XK_h
, KF_MASTER_SHRINK
, NULL
);
6016 setkeybinding(MODKEY
, XK_l
, KF_MASTER_GROW
, NULL
);
6017 setkeybinding(MODKEY
, XK_comma
, KF_MASTER_ADD
, NULL
);
6018 setkeybinding(MODKEY
, XK_period
, KF_MASTER_DEL
, NULL
);
6019 setkeybinding(MODKEY_SHIFT
, XK_comma
, KF_STACK_INC
, NULL
);
6020 setkeybinding(MODKEY_SHIFT
, XK_period
, KF_STACK_DEC
, NULL
);
6021 setkeybinding(MODKEY
, XK_Return
, KF_SWAP_MAIN
, NULL
);
6022 setkeybinding(MODKEY
, XK_j
, KF_FOCUS_NEXT
, NULL
);
6023 setkeybinding(MODKEY
, XK_k
, KF_FOCUS_PREV
, NULL
);
6024 setkeybinding(MODKEY_SHIFT
, XK_j
, KF_SWAP_NEXT
, NULL
);
6025 setkeybinding(MODKEY_SHIFT
, XK_k
, KF_SWAP_PREV
, NULL
);
6026 setkeybinding(MODKEY_SHIFT
, XK_Return
, KF_SPAWN_CUSTOM
,"term");
6027 setkeybinding(MODKEY
, XK_p
, KF_SPAWN_CUSTOM
,"menu");
6028 setkeybinding(MODKEY_SHIFT
, XK_q
, KF_QUIT
, NULL
);
6029 setkeybinding(MODKEY
, XK_q
, KF_RESTART
, NULL
);
6030 setkeybinding(MODKEY
, XK_m
, KF_FOCUS_MAIN
, NULL
);
6031 setkeybinding(MODKEY
, XK_1
, KF_WS_1
, NULL
);
6032 setkeybinding(MODKEY
, XK_2
, KF_WS_2
, NULL
);
6033 setkeybinding(MODKEY
, XK_3
, KF_WS_3
, NULL
);
6034 setkeybinding(MODKEY
, XK_4
, KF_WS_4
, NULL
);
6035 setkeybinding(MODKEY
, XK_5
, KF_WS_5
, NULL
);
6036 setkeybinding(MODKEY
, XK_6
, KF_WS_6
, NULL
);
6037 setkeybinding(MODKEY
, XK_7
, KF_WS_7
, NULL
);
6038 setkeybinding(MODKEY
, XK_8
, KF_WS_8
, NULL
);
6039 setkeybinding(MODKEY
, XK_9
, KF_WS_9
, NULL
);
6040 setkeybinding(MODKEY
, XK_0
, KF_WS_10
, NULL
);
6041 setkeybinding(MODKEY
, XK_F1
, KF_WS_11
, NULL
);
6042 setkeybinding(MODKEY
, XK_F2
, KF_WS_12
, NULL
);
6043 setkeybinding(MODKEY
, XK_F3
, KF_WS_13
, NULL
);
6044 setkeybinding(MODKEY
, XK_F4
, KF_WS_14
, NULL
);
6045 setkeybinding(MODKEY
, XK_F5
, KF_WS_15
, NULL
);
6046 setkeybinding(MODKEY
, XK_F6
, KF_WS_16
, NULL
);
6047 setkeybinding(MODKEY
, XK_F7
, KF_WS_17
, NULL
);
6048 setkeybinding(MODKEY
, XK_F8
, KF_WS_18
, NULL
);
6049 setkeybinding(MODKEY
, XK_F9
, KF_WS_19
, NULL
);
6050 setkeybinding(MODKEY
, XK_F10
, KF_WS_20
, NULL
);
6051 setkeybinding(MODKEY
, XK_F11
, KF_WS_21
, NULL
);
6052 setkeybinding(MODKEY
, XK_F12
, KF_WS_22
, NULL
);
6053 setkeybinding(MODKEY
, XK_Right
, KF_WS_NEXT
, NULL
);
6054 setkeybinding(MODKEY
, XK_Left
, KF_WS_PREV
, NULL
);
6055 setkeybinding(MODKEY
, XK_Up
, KF_WS_NEXT_ALL
, NULL
);
6056 setkeybinding(MODKEY
, XK_Down
, KF_WS_PREV_ALL
, NULL
);
6057 setkeybinding(MODKEY
, XK_a
, KF_WS_PRIOR
, NULL
);
6058 setkeybinding(MODKEY_SHIFT
, XK_Right
, KF_SCREEN_NEXT
, NULL
);
6059 setkeybinding(MODKEY_SHIFT
, XK_Left
, KF_SCREEN_PREV
, NULL
);
6060 setkeybinding(MODKEY_SHIFT
, XK_1
, KF_MVWS_1
, NULL
);
6061 setkeybinding(MODKEY_SHIFT
, XK_2
, KF_MVWS_2
, NULL
);
6062 setkeybinding(MODKEY_SHIFT
, XK_3
, KF_MVWS_3
, NULL
);
6063 setkeybinding(MODKEY_SHIFT
, XK_4
, KF_MVWS_4
, NULL
);
6064 setkeybinding(MODKEY_SHIFT
, XK_5
, KF_MVWS_5
, NULL
);
6065 setkeybinding(MODKEY_SHIFT
, XK_6
, KF_MVWS_6
, NULL
);
6066 setkeybinding(MODKEY_SHIFT
, XK_7
, KF_MVWS_7
, NULL
);
6067 setkeybinding(MODKEY_SHIFT
, XK_8
, KF_MVWS_8
, NULL
);
6068 setkeybinding(MODKEY_SHIFT
, XK_9
, KF_MVWS_9
, NULL
);
6069 setkeybinding(MODKEY_SHIFT
, XK_0
, KF_MVWS_10
, NULL
);
6070 setkeybinding(MODKEY_SHIFT
, XK_F1
, KF_MVWS_11
, NULL
);
6071 setkeybinding(MODKEY_SHIFT
, XK_F2
, KF_MVWS_12
, NULL
);
6072 setkeybinding(MODKEY_SHIFT
, XK_F3
, KF_MVWS_13
, NULL
);
6073 setkeybinding(MODKEY_SHIFT
, XK_F4
, KF_MVWS_14
, NULL
);
6074 setkeybinding(MODKEY_SHIFT
, XK_F5
, KF_MVWS_15
, NULL
);
6075 setkeybinding(MODKEY_SHIFT
, XK_F6
, KF_MVWS_16
, NULL
);
6076 setkeybinding(MODKEY_SHIFT
, XK_F7
, KF_MVWS_17
, NULL
);
6077 setkeybinding(MODKEY_SHIFT
, XK_F8
, KF_MVWS_18
, NULL
);
6078 setkeybinding(MODKEY_SHIFT
, XK_F9
, KF_MVWS_19
, NULL
);
6079 setkeybinding(MODKEY_SHIFT
, XK_F10
, KF_MVWS_20
, NULL
);
6080 setkeybinding(MODKEY_SHIFT
, XK_F11
, KF_MVWS_21
, NULL
);
6081 setkeybinding(MODKEY_SHIFT
, XK_F12
, KF_MVWS_22
, NULL
);
6082 setkeybinding(MODKEY
, XK_b
, KF_BAR_TOGGLE
, NULL
);
6083 setkeybinding(MODKEY
, XK_Tab
, KF_FOCUS_NEXT
, NULL
);
6084 setkeybinding(MODKEY_SHIFT
, XK_Tab
, KF_FOCUS_PREV
, NULL
);
6085 setkeybinding(MODKEY_SHIFT
, XK_x
, KF_WIND_KILL
, NULL
);
6086 setkeybinding(MODKEY
, XK_x
, KF_WIND_DEL
, NULL
);
6087 setkeybinding(MODKEY
, XK_s
, KF_SPAWN_CUSTOM
,"screenshot_all");
6088 setkeybinding(MODKEY_SHIFT
, XK_s
, KF_SPAWN_CUSTOM
,"screenshot_wind");
6089 setkeybinding(MODKEY
, XK_t
, KF_FLOAT_TOGGLE
,NULL
);
6090 setkeybinding(MODKEY_SHIFT
, XK_v
, KF_VERSION
, NULL
);
6091 setkeybinding(MODKEY_SHIFT
, XK_Delete
, KF_SPAWN_CUSTOM
,"lock");
6092 setkeybinding(MODKEY_SHIFT
, XK_i
, KF_SPAWN_CUSTOM
,"initscr");
6093 setkeybinding(MODKEY
, XK_w
, KF_ICONIFY
, NULL
);
6094 setkeybinding(MODKEY_SHIFT
, XK_w
, KF_UNICONIFY
, NULL
);
6095 setkeybinding(MODKEY_SHIFT
, XK_r
, KF_RAISE_TOGGLE
,NULL
);
6096 setkeybinding(MODKEY
, XK_v
, KF_BUTTON2
, NULL
);
6097 setkeybinding(MODKEY
, XK_equal
, KF_WIDTH_GROW
, NULL
);
6098 setkeybinding(MODKEY
, XK_minus
, KF_WIDTH_SHRINK
,NULL
);
6099 setkeybinding(MODKEY_SHIFT
, XK_equal
, KF_HEIGHT_GROW
,NULL
);
6100 setkeybinding(MODKEY_SHIFT
, XK_minus
, KF_HEIGHT_SHRINK
,NULL
);
6101 setkeybinding(MODKEY
, XK_bracketleft
, KF_MOVE_LEFT
,NULL
);
6102 setkeybinding(MODKEY
, XK_bracketright
,KF_MOVE_RIGHT
,NULL
);
6103 setkeybinding(MODKEY_SHIFT
, XK_bracketleft
, KF_MOVE_UP
, NULL
);
6104 setkeybinding(MODKEY_SHIFT
, XK_bracketright
,KF_MOVE_DOWN
,NULL
);
6105 setkeybinding(MODKEY_SHIFT
, XK_slash
, KF_NAME_WORKSPACE
,NULL
);
6106 setkeybinding(MODKEY
, XK_slash
, KF_SEARCH_WORKSPACE
,NULL
);
6107 setkeybinding(MODKEY
, XK_f
, KF_SEARCH_WIN
, NULL
);
6109 setkeybinding(MODKEY_SHIFT
, XK_d
, KF_DUMPWINS
, NULL
);
6119 while (RB_EMPTY(&keys
) == 0) {
6120 kp
= RB_ROOT(&keys
);
6126 setkeymapping(char *selector
, char *value
, int flags
)
6128 char keymapping_file
[PATH_MAX
];
6130 /* suppress unused warnings since vars are needed */
6134 DNPRINTF(SWM_D_KEY
, "setkeymapping: enter\n");
6135 if (value
[0] == '~')
6136 snprintf(keymapping_file
, sizeof keymapping_file
, "%s/%s",
6137 pwd
->pw_dir
, &value
[1]);
6139 strlcpy(keymapping_file
, value
, sizeof keymapping_file
);
6141 /* load new key bindings; if it fails, revert to default bindings */
6142 if (conf_load(keymapping_file
, SWM_CONF_KEYMAPPING
)) {
6146 DNPRINTF(SWM_D_KEY
, "setkeymapping: leave\n");
6151 updatenumlockmask(void)
6154 xcb_get_modifier_mapping_reply_t
*modmap_r
;
6155 xcb_keycode_t
*modmap
, kc
, *keycode
;
6159 modmap_r
= xcb_get_modifier_mapping_reply(conn
,
6160 xcb_get_modifier_mapping(conn
),
6163 modmap
= xcb_get_modifier_mapping_keycodes(modmap_r
);
6164 for (i
= 0; i
< 8; i
++) {
6165 for (j
= 0; j
< modmap_r
->keycodes_per_modifier
; j
++) {
6166 kc
= modmap
[i
* modmap_r
->keycodes_per_modifier
6168 keycode
= xcb_key_symbols_get_keycode(syms
,
6171 numlockmask
= (1 << i
);
6177 DNPRINTF(SWM_D_MISC
, "updatenumlockmask: %d\n", numlockmask
);
6184 int num_screens
, k
, j
;
6185 unsigned int modifiers
[4];
6186 xcb_keycode_t
*code
;
6188 DNPRINTF(SWM_D_MISC
, "grabkeys\n");
6189 updatenumlockmask();
6192 modifiers
[1] = numlockmask
;
6193 modifiers
[2] = XCB_MOD_MASK_LOCK
;
6194 modifiers
[3] = numlockmask
| XCB_MOD_MASK_LOCK
;
6196 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
6197 for (k
= 0; k
< num_screens
; k
++) {
6198 if (TAILQ_EMPTY(&screens
[k
].rl
))
6200 xcb_ungrab_key(conn
, XCB_GRAB_ANY
, screens
[k
].root
,
6202 RB_FOREACH(kp
, key_tree
, &keys
) {
6203 if ((code
= xcb_key_symbols_get_keycode(syms
,
6205 for (j
= 0; j
< LENGTH(modifiers
); j
++)
6206 xcb_grab_key(conn
, 1,
6208 kp
->mod
| modifiers
[j
],
6209 *code
, XCB_GRAB_MODE_SYNC
,
6210 XCB_GRAB_MODE_ASYNC
);
6218 grabbuttons(struct ws_win
*win
)
6220 unsigned int modifiers
[4];
6223 DNPRINTF(SWM_D_MOUSE
, "grabbuttons: win 0x%x\n", win
->id
);
6224 updatenumlockmask();
6227 modifiers
[1] = numlockmask
;
6228 modifiers
[2] = XCB_MOD_MASK_LOCK
;
6229 modifiers
[3] = numlockmask
| XCB_MOD_MASK_LOCK
;
6231 for (i
= 0; i
< LENGTH(buttons
); i
++)
6232 if (buttons
[i
].action
== client_click
)
6233 for (j
= 0; j
< LENGTH(modifiers
); ++j
)
6234 xcb_grab_button(conn
, 0, win
->id
, BUTTONMASK
,
6235 XCB_GRAB_MODE_SYNC
, XCB_GRAB_MODE_ASYNC
,
6236 XCB_WINDOW_NONE
, XCB_CURSOR_NONE
,
6237 buttons
[i
].button
, buttons
[i
].mask
|
6241 const char *quirkname
[] = {
6242 "NONE", /* config string for "no value" */
6251 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
6252 #define SWM_Q_WS "\n|+ \t"
6254 parsequirks(char *qstr
, unsigned long *quirk
)
6264 while ((name
= strsep(&cp
, SWM_Q_WS
)) != NULL
) {
6266 cp
+= (long)strspn(cp
, SWM_Q_WS
);
6267 for (i
= 0; i
< LENGTH(quirkname
); i
++) {
6268 if (!strncasecmp(name
, quirkname
[i
], SWM_QUIRK_LEN
)) {
6269 DNPRINTF(SWM_D_QUIRK
,
6270 "parsequirks: %s\n", name
);
6275 *quirk
|= 1 << (i
-1);
6279 if (i
>= LENGTH(quirkname
)) {
6280 DNPRINTF(SWM_D_QUIRK
,
6281 "parsequirks: invalid quirk [%s]\n", name
);
6289 quirk_insert(const char *class, const char *name
, unsigned long quirk
)
6293 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: %s:%s [%lu]\n", class, name
,
6296 if ((qp
= malloc(sizeof *qp
)) == NULL
)
6297 err(1, "quirk_insert: malloc");
6298 if ((qp
->class = strdup(class)) == NULL
)
6299 err(1, "quirk_insert: strdup");
6300 if ((qp
->name
= strdup(name
)) == NULL
)
6301 err(1, "quirk_insert: strdup");
6304 TAILQ_INSERT_TAIL(&quirks
, qp
, entry
);
6306 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: leave\n");
6310 quirk_remove(struct quirk
*qp
)
6312 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: %s:%s [%lu]\n", qp
->class,
6313 qp
->name
, qp
->quirk
);
6315 TAILQ_REMOVE(&quirks
, qp
, entry
);
6320 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: leave\n");
6324 quirk_replace(struct quirk
*qp
, const char *class, const char *name
,
6325 unsigned long quirk
)
6327 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: %s:%s [%lu]\n", qp
->class,
6328 qp
->name
, qp
->quirk
);
6331 quirk_insert(class, name
, quirk
);
6333 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: leave\n");
6337 setquirk(const char *class, const char *name
, unsigned long quirk
)
6341 DNPRINTF(SWM_D_QUIRK
, "setquirk: enter %s:%s [%lu]\n", class, name
,
6344 /* Remove/replace existing quirk. */
6345 TAILQ_FOREACH(qp
, &quirks
, entry
) {
6346 if (!strcmp(qp
->class, class) && !strcmp(qp
->name
, name
)) {
6350 quirk_replace(qp
, class, name
, quirk
);
6351 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
6356 /* Only insert if quirk is not NONE. */
6358 quirk_insert(class, name
, quirk
);
6360 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
6364 setconfquirk(char *selector
, char *value
, int flags
)
6366 char *cp
, *class, *name
;
6370 /* suppress unused warning since var is needed */
6373 if (selector
== NULL
)
6375 if ((cp
= strchr(selector
, ':')) == NULL
)
6380 if ((retval
= parsequirks(value
, &qrks
)) == 0)
6381 setquirk(class, name
, qrks
);
6388 setquirk("MPlayer", "xv", SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
| SWM_Q_FOCUSPREV
);
6389 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT
);
6390 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ
);
6391 setquirk("Firefox", "Dialog", SWM_Q_FLOAT
);
6392 setquirk("Gimp", "gimp", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
6393 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ
);
6394 setquirk("xine", "Xine Window", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
6395 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
6396 setquirk("xine", "xine Panel", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
6397 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
6398 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN
| SWM_Q_FLOAT
);
6399 setquirk("pcb", "pcb", SWM_Q_FLOAT
);
6400 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
);
6403 /* conf file stuff */
6404 #define SWM_CONF_FILE "spectrwm.conf"
6405 #define SWM_CONF_FILE_OLD "scrotwm.conf"
6409 SWM_S_BAR_AT_BOTTOM
,
6410 SWM_S_BAR_BORDER_WIDTH
,
6417 SWM_S_CLOCK_ENABLED
,
6420 SWM_S_CYCLE_VISIBLE
,
6422 SWM_S_DISABLE_BORDER
,
6424 SWM_S_FOCUS_CLOSE_WRAP
,
6425 SWM_S_FOCUS_DEFAULT
,
6431 SWM_S_STACK_ENABLED
,
6433 SWM_S_TITLE_CLASS_ENABLED
,
6434 SWM_S_TITLE_NAME_ENABLED
,
6435 SWM_S_URGENT_ENABLED
,
6436 SWM_S_VERBOSE_LAYOUT
,
6437 SWM_S_WINDOW_NAME_ENABLED
,
6438 SWM_S_WORKSPACE_LIMIT
6442 setconfvalue(char *selector
, char *value
, int flags
)
6447 /* suppress unused warning since var is needed */
6451 case SWM_S_BAR_ACTION
:
6453 if ((bar_argv
[0] = expand_tilde(value
)) == NULL
)
6454 err(1, "setconfvalue: bar_action");
6456 case SWM_S_BAR_AT_BOTTOM
:
6457 bar_at_bottom
= atoi(value
);
6459 case SWM_S_BAR_BORDER_WIDTH
:
6460 bar_border_width
= atoi(value
);
6461 if (bar_border_width
< 0)
6462 bar_border_width
= 0;
6464 case SWM_S_BAR_DELAY
:
6465 bar_delay
= atoi(value
);
6467 case SWM_S_BAR_ENABLED
:
6468 bar_enabled
= atoi(value
);
6470 case SWM_S_BAR_FONT
:
6472 if (asprintf(&bar_fonts
, "%s,%s", value
, bar_fonts
) == -1)
6473 err(1, "setconfvalue: asprintf: failed to allocate "
6474 "memory for bar_fonts.");
6477 /* If already in xft mode, then we are done. */
6478 if (!bar_font_legacy
)
6481 /* If there are any non-XLFD entries, switch to Xft mode. */
6482 while ((b
= strsep(&value
, ",")) != NULL
) {
6486 bar_font_legacy
= 0;
6491 case SWM_S_BAR_FORMAT
:
6493 if ((bar_format
= strdup(value
)) == NULL
)
6494 err(1, "setconfvalue: bar_format");
6496 case SWM_S_BAR_JUSTIFY
:
6497 if (!strcmp(value
, "left"))
6498 bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
6499 else if (!strcmp(value
, "center"))
6500 bar_justify
= SWM_BAR_JUSTIFY_CENTER
;
6501 else if (!strcmp(value
, "right"))
6502 bar_justify
= SWM_BAR_JUSTIFY_RIGHT
;
6504 errx(1, "invalid bar_justify");
6506 case SWM_S_BORDER_WIDTH
:
6507 border_width
= atoi(value
);
6508 if (border_width
< 0)
6511 case SWM_S_CLOCK_ENABLED
:
6512 clock_enabled
= atoi(value
);
6514 case SWM_S_CLOCK_FORMAT
:
6515 #ifndef SWM_DENY_CLOCK_FORMAT
6517 if ((clock_format
= strdup(value
)) == NULL
)
6518 err(1, "setconfvalue: clock_format");
6521 case SWM_S_CYCLE_EMPTY
:
6522 cycle_empty
= atoi(value
);
6524 case SWM_S_CYCLE_VISIBLE
:
6525 cycle_visible
= atoi(value
);
6527 case SWM_S_DIALOG_RATIO
:
6528 dialog_ratio
= atof(value
);
6529 if (dialog_ratio
> 1.0 || dialog_ratio
<= .3)
6532 case SWM_S_DISABLE_BORDER
:
6533 disable_border
= atoi(value
);
6535 case SWM_S_FOCUS_CLOSE
:
6536 if (!strcmp(value
, "first"))
6537 focus_close
= SWM_STACK_BOTTOM
;
6538 else if (!strcmp(value
, "last"))
6539 focus_close
= SWM_STACK_TOP
;
6540 else if (!strcmp(value
, "next"))
6541 focus_close
= SWM_STACK_ABOVE
;
6542 else if (!strcmp(value
, "previous"))
6543 focus_close
= SWM_STACK_BELOW
;
6545 errx(1, "focus_close");
6547 case SWM_S_FOCUS_CLOSE_WRAP
:
6548 focus_close_wrap
= atoi(value
);
6550 case SWM_S_FOCUS_DEFAULT
:
6551 if (!strcmp(value
, "last"))
6552 focus_default
= SWM_STACK_TOP
;
6553 else if (!strcmp(value
, "first"))
6554 focus_default
= SWM_STACK_BOTTOM
;
6556 errx(1, "focus_default");
6558 case SWM_S_FOCUS_MODE
:
6559 if (!strcmp(value
, "default"))
6560 focus_mode
= SWM_FOCUS_DEFAULT
;
6561 else if (!strcmp(value
, "follow") ||
6562 !strcmp(value
, "follow_cursor"))
6563 focus_mode
= SWM_FOCUS_FOLLOW
;
6564 else if (!strcmp(value
, "manual"))
6565 focus_mode
= SWM_FOCUS_MANUAL
;
6567 errx(1, "focus_mode");
6569 case SWM_S_SPAWN_ORDER
:
6570 if (!strcmp(value
, "first"))
6571 spawn_position
= SWM_STACK_BOTTOM
;
6572 else if (!strcmp(value
, "last"))
6573 spawn_position
= SWM_STACK_TOP
;
6574 else if (!strcmp(value
, "next"))
6575 spawn_position
= SWM_STACK_ABOVE
;
6576 else if (!strcmp(value
, "previous"))
6577 spawn_position
= SWM_STACK_BELOW
;
6579 errx(1, "spawn_position");
6581 case SWM_S_SPAWN_TERM
:
6582 setconfspawn("term", value
, 0);
6583 setconfspawn("spawn_term", value
, 0);
6587 case SWM_S_SS_ENABLED
:
6588 ss_enabled
= atoi(value
);
6590 case SWM_S_STACK_ENABLED
:
6591 stack_enabled
= atoi(value
);
6593 case SWM_S_TERM_WIDTH
:
6594 term_width
= atoi(value
);
6598 case SWM_S_TITLE_CLASS_ENABLED
:
6599 title_class_enabled
= atoi(value
);
6601 case SWM_S_TITLE_NAME_ENABLED
:
6602 title_name_enabled
= atoi(value
);
6604 case SWM_S_URGENT_ENABLED
:
6605 urgent_enabled
= atoi(value
);
6607 case SWM_S_VERBOSE_LAYOUT
:
6608 verbose_layout
= atoi(value
);
6609 for (i
= 0; layouts
[i
].l_stack
!= NULL
; i
++) {
6611 layouts
[i
].l_string
= fancy_stacker
;
6613 layouts
[i
].l_string
= plain_stacker
;
6616 case SWM_S_WINDOW_NAME_ENABLED
:
6617 window_name_enabled
= atoi(value
);
6619 case SWM_S_WORKSPACE_LIMIT
:
6620 workspace_limit
= atoi(value
);
6621 if (workspace_limit
> SWM_WS_MAX
)
6622 workspace_limit
= SWM_WS_MAX
;
6623 else if (workspace_limit
< 1)
6624 workspace_limit
= 1;
6633 setconfmodkey(char *selector
, char *value
, int flags
)
6635 /* suppress unused warnings since vars are needed */
6639 if (!strncasecmp(value
, "Mod1", strlen("Mod1")))
6640 update_modkey(XCB_MOD_MASK_1
);
6641 else if (!strncasecmp(value
, "Mod2", strlen("Mod2")))
6642 update_modkey(XCB_MOD_MASK_2
);
6643 else if (!strncasecmp(value
, "Mod3", strlen("Mod3")))
6644 update_modkey(XCB_MOD_MASK_3
);
6645 else if (!strncasecmp(value
, "Mod4", strlen("Mod4")))
6646 update_modkey(XCB_MOD_MASK_4
);
6653 setconfcolor(char *selector
, char *value
, int flags
)
6655 setscreencolor(value
, ((selector
== NULL
)?-1:atoi(selector
)), flags
);
6660 setconfregion(char *selector
, char *value
, int flags
)
6662 /* suppress unused warnings since vars are needed */
6666 custom_region(value
);
6671 setautorun(char *selector
, char *value
, int flags
)
6681 /* suppress unused warnings since vars are needed */
6685 if (getenv("SWM_STARTED"))
6689 if (sscanf(value
, "ws[%d]:%1023c", &ws_id
, s
) != 2)
6690 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
6692 if (ws_id
< 0 || ws_id
>= workspace_limit
)
6693 errx(1, "autorun: invalid workspace %d", ws_id
+ 1);
6695 sp
= expand_tilde((char *)&s
);
6698 * This is a little intricate
6700 * If the pid already exists we simply reuse it because it means it was
6701 * used before AND not claimed by manage_window. We get away with
6702 * altering it in the parent after INSERT because this can not be a race
6705 while ((ap
= strsep(&sp
, " \t")) != NULL
) {
6708 DNPRINTF(SWM_D_SPAWN
, "setautorun: arg [%s]\n", ap
);
6710 if ((a
.argv
= realloc(a
.argv
, argc
* sizeof(char *))) == NULL
)
6711 err(1, "setautorun: realloc");
6712 a
.argv
[argc
- 1] = ap
;
6716 if ((a
.argv
= realloc(a
.argv
, (argc
+ 1) * sizeof(char *))) == NULL
)
6717 err(1, "setautorun: realloc");
6718 a
.argv
[argc
] = NULL
;
6720 if ((pid
= fork()) == 0) {
6721 spawn(ws_id
, &a
, 1);
6730 p
= calloc(1, sizeof *p
);
6733 TAILQ_INSERT_TAIL(&pidlist
, p
, entry
);
6743 setlayout(char *selector
, char *value
, int flags
)
6745 int ws_id
, i
, x
, mg
, ma
, si
, ar
, f
= 0;
6746 int st
= SWM_V_STACK
, num_screens
;
6748 struct workspace
*ws
;
6750 /* suppress unused warnings since vars are needed */
6754 if (getenv("SWM_STARTED"))
6758 if (sscanf(value
, "ws[%d]:%d:%d:%d:%d:%1023c",
6759 &ws_id
, &mg
, &ma
, &si
, &ar
, s
) != 6)
6760 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
6761 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
6764 if (ws_id
< 0 || ws_id
>= workspace_limit
)
6765 errx(1, "layout: invalid workspace %d", ws_id
+ 1);
6767 if (!strcasecmp(s
, "vertical"))
6769 else if (!strcasecmp(s
, "vertical_flip")) {
6772 } else if (!strcasecmp(s
, "horizontal"))
6774 else if (!strcasecmp(s
, "horizontal_flip")) {
6777 } else if (!strcasecmp(s
, "fullscreen"))
6780 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
6781 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
6784 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
6785 for (i
= 0; i
< num_screens
; i
++) {
6786 ws
= (struct workspace
*)&screens
[i
].ws
;
6787 ws
[ws_id
].cur_layout
= &layouts
[st
];
6789 ws
[ws_id
].always_raise
= ar
;
6790 if (st
== SWM_MAX_STACK
)
6794 for (x
= 0; x
< abs(mg
); x
++) {
6795 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
6796 mg
>= 0 ? SWM_ARG_ID_MASTERGROW
:
6797 SWM_ARG_ID_MASTERSHRINK
);
6801 for (x
= 0; x
< abs(ma
); x
++) {
6802 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
6803 ma
>= 0 ? SWM_ARG_ID_MASTERADD
:
6804 SWM_ARG_ID_MASTERDEL
);
6808 for (x
= 0; x
< abs(si
); x
++) {
6809 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
6810 si
>= 0 ? SWM_ARG_ID_STACKINC
:
6811 SWM_ARG_ID_STACKDEC
);
6816 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
6817 SWM_ARG_ID_FLIPLAYOUT
);
6827 /* config options */
6828 struct config_option
{
6830 int (*func
)(char*, char*, int);
6833 struct config_option configopt
[] = {
6834 { "bar_enabled", setconfvalue
, SWM_S_BAR_ENABLED
},
6835 { "bar_at_bottom", setconfvalue
, SWM_S_BAR_AT_BOTTOM
},
6836 { "bar_border", setconfcolor
, SWM_S_COLOR_BAR_BORDER
},
6837 { "bar_border_width", setconfvalue
, SWM_S_BAR_BORDER_WIDTH
},
6838 { "bar_color", setconfcolor
, SWM_S_COLOR_BAR
},
6839 { "bar_font_color", setconfcolor
, SWM_S_COLOR_BAR_FONT
},
6840 { "bar_font", setconfvalue
, SWM_S_BAR_FONT
},
6841 { "bar_action", setconfvalue
, SWM_S_BAR_ACTION
},
6842 { "bar_delay", setconfvalue
, SWM_S_BAR_DELAY
},
6843 { "bar_justify", setconfvalue
, SWM_S_BAR_JUSTIFY
},
6844 { "bar_format", setconfvalue
, SWM_S_BAR_FORMAT
},
6845 { "keyboard_mapping", setkeymapping
, 0 },
6846 { "bind", setconfbinding
, 0 },
6847 { "stack_enabled", setconfvalue
, SWM_S_STACK_ENABLED
},
6848 { "clock_enabled", setconfvalue
, SWM_S_CLOCK_ENABLED
},
6849 { "clock_format", setconfvalue
, SWM_S_CLOCK_FORMAT
},
6850 { "color_focus", setconfcolor
, SWM_S_COLOR_FOCUS
},
6851 { "color_unfocus", setconfcolor
, SWM_S_COLOR_UNFOCUS
},
6852 { "cycle_empty", setconfvalue
, SWM_S_CYCLE_EMPTY
},
6853 { "cycle_visible", setconfvalue
, SWM_S_CYCLE_VISIBLE
},
6854 { "workspace_limit", setconfvalue
, SWM_S_WORKSPACE_LIMIT
},
6855 { "dialog_ratio", setconfvalue
, SWM_S_DIALOG_RATIO
},
6856 { "verbose_layout", setconfvalue
, SWM_S_VERBOSE_LAYOUT
},
6857 { "modkey", setconfmodkey
, 0 },
6858 { "program", setconfspawn
, 0 },
6859 { "quirk", setconfquirk
, 0 },
6860 { "region", setconfregion
, 0 },
6861 { "spawn_term", setconfvalue
, SWM_S_SPAWN_TERM
},
6862 { "screenshot_enabled", setconfvalue
, SWM_S_SS_ENABLED
},
6863 { "screenshot_app", setconfvalue
, SWM_S_SS_APP
},
6864 { "window_name_enabled", setconfvalue
, SWM_S_WINDOW_NAME_ENABLED
},
6865 { "urgent_enabled", setconfvalue
, SWM_S_URGENT_ENABLED
},
6866 { "term_width", setconfvalue
, SWM_S_TERM_WIDTH
},
6867 { "title_class_enabled", setconfvalue
, SWM_S_TITLE_CLASS_ENABLED
},
6868 { "title_name_enabled", setconfvalue
, SWM_S_TITLE_NAME_ENABLED
},
6869 { "focus_mode", setconfvalue
, SWM_S_FOCUS_MODE
},
6870 { "focus_close", setconfvalue
, SWM_S_FOCUS_CLOSE
},
6871 { "focus_close_wrap", setconfvalue
, SWM_S_FOCUS_CLOSE_WRAP
},
6872 { "focus_default", setconfvalue
, SWM_S_FOCUS_DEFAULT
},
6873 { "spawn_position", setconfvalue
, SWM_S_SPAWN_ORDER
},
6874 { "disable_border", setconfvalue
, SWM_S_DISABLE_BORDER
},
6875 { "border_width", setconfvalue
, SWM_S_BORDER_WIDTH
},
6876 { "autorun", setautorun
, 0 },
6877 { "layout", setlayout
, 0 },
6881 conf_load(const char *filename
, int keymapping
)
6884 char *line
, *cp
, *optsub
, *optval
;
6885 size_t linelen
, lineno
= 0;
6886 int wordlen
, i
, optidx
;
6887 struct config_option
*opt
= NULL
;
6889 DNPRINTF(SWM_D_CONF
, "conf_load: begin\n");
6891 if (filename
== NULL
) {
6892 warnx("conf_load: no filename");
6895 if ((config
= fopen(filename
, "r")) == NULL
) {
6896 warn("conf_load: fopen: %s", filename
);
6900 while (!feof(config
)) {
6901 if ((line
= fparseln(config
, &linelen
, &lineno
, NULL
, 0))
6904 err(1, "%s", filename
);
6909 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
6910 if (cp
[0] == '\0') {
6915 /* get config option */
6916 wordlen
= strcspn(cp
, "=[ \t\n");
6918 warnx("%s: line %zd: no option found",
6923 for (i
= 0; i
< LENGTH(configopt
); i
++) {
6924 opt
= &configopt
[i
];
6925 if (!strncasecmp(cp
, opt
->optname
, wordlen
) &&
6926 (int)strlen(opt
->optname
) == wordlen
) {
6932 warnx("%s: line %zd: unknown option %.*s",
6933 filename
, lineno
, wordlen
, cp
);
6936 if (keymapping
&& opt
&& strcmp(opt
->optname
, "bind")) {
6937 warnx("%s: line %zd: invalid option %.*s",
6938 filename
, lineno
, wordlen
, cp
);
6942 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
6943 /* get [selector] if any */
6947 wordlen
= strcspn(cp
, "]");
6950 warnx("%s: line %zd: syntax error",
6955 if (asprintf(&optsub
, "%.*s", wordlen
, cp
) ==
6957 warnx("%s: line %zd: unable to allocate"
6958 "memory for selector", filename
,
6964 cp
+= strspn(cp
, "] \t\n"); /* eat trailing */
6966 cp
+= strspn(cp
, "= \t\n"); /* eat trailing */
6968 optval
= strdup(cp
);
6969 /* call function to deal with it all */
6970 if (configopt
[optidx
].func(optsub
, optval
,
6971 configopt
[optidx
].funcflags
) != 0)
6972 errx(1, "%s: line %zd: invalid data for %s",
6973 filename
, lineno
, configopt
[optidx
].optname
);
6980 DNPRINTF(SWM_D_CONF
, "conf_load: end\n");
6987 DNPRINTF(SWM_D_CONF
, "conf_load: end with error.\n");
6993 set_child_transient(struct ws_win
*win
, xcb_window_t
*trans
)
6995 struct ws_win
*parent
, *w
;
6996 struct swm_region
*r
;
6997 struct workspace
*ws
;
6998 xcb_icccm_wm_hints_t wmh
;
7000 parent
= find_window(win
->transient
);
7002 parent
->focus_child
= win
;
7004 DNPRINTF(SWM_D_MISC
, "set_child_transient: parent doesn't exist"
7005 " for 0x%x trans 0x%x\n", win
->id
, win
->transient
);
7007 r
= root_to_region(win
->wa
->root
, SWM_CK_ALL
);
7009 /* parent doen't exist in our window list */
7010 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
7011 if (xcb_icccm_get_wm_hints_reply(conn
,
7012 xcb_icccm_get_wm_hints(conn
, w
->id
),
7014 warnx("can't get hints for 0x%x", w
->id
);
7018 if (win
->hints
.window_group
!= wmh
.window_group
)
7021 w
->focus_child
= win
;
7022 win
->transient
= w
->id
;
7024 DNPRINTF(SWM_D_MISC
, "set_child_transient: adjusting "
7025 "transient to 0x%x\n", win
->transient
);
7032 window_get_pid(xcb_window_t win
)
7037 xcb_get_property_cookie_t pc
;
7038 xcb_get_property_reply_t
*pr
;
7040 apid
= get_atom_from_string("_NET_WM_PID");
7041 if (apid
== XCB_ATOM_NONE
)
7044 pc
= xcb_get_property(conn
, 0, win
, apid
, XCB_ATOM_CARDINAL
, 0, 1);
7045 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
7048 if (pr
->type
!= XCB_ATOM_CARDINAL
) {
7053 if (pr
->type
== apid
&& pr
->format
== 32)
7054 ret
= *((pid_t
*)xcb_get_property_value(pr
));
7060 apid
= get_atom_from_string("_SWM_PID");
7061 pc
= xcb_get_property(conn
, 0, win
, apid
, XCB_ATOM_STRING
,
7063 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
7066 if (pr
->type
!= apid
) {
7071 ret
= (pid_t
)strtonum(xcb_get_property_value(pr
), 0, INT_MAX
, &errstr
);
7078 get_ws_idx(xcb_window_t id
)
7084 xcb_get_property_reply_t
*gpr
;
7086 gpr
= xcb_get_property_reply(conn
,
7087 xcb_get_property(conn
, 0, id
, a_swm_ws
,
7088 XCB_ATOM_STRING
, 0, SWM_PROPLEN
),
7093 proplen
= xcb_get_property_value_length(gpr
);
7095 prop
= malloc(proplen
+ 1);
7098 xcb_get_property_value(gpr
),
7100 prop
[proplen
] = '\0';
7107 DNPRINTF(SWM_D_PROP
, "get_ws_idx: _SWM_WS: %s\n", prop
);
7108 ws_idx
= (int)strtonum(prop
, 0, workspace_limit
- 1, &errstr
);
7110 DNPRINTF(SWM_D_PROP
, "get_ws_idx: window: #%s: %s",
7120 manage_window(xcb_window_t id
, uint16_t mapped
)
7122 xcb_window_t trans
= XCB_WINDOW_NONE
;
7123 struct ws_win
*win
, *ww
;
7125 char ws_idx_str
[SWM_PROPLEN
];
7126 struct swm_region
*r
;
7130 xcb_icccm_get_wm_protocols_reply_t wpr
;
7132 if ((win
= find_window(id
)) != NULL
) {
7133 DNPRINTF(SWM_D_MISC
, "manage_window: win 0x%x already "
7134 "managed; skipping.)\n", id
);
7135 return (win
); /* Already managed. */
7138 /* See if window is on the unmanaged list. */
7139 if ((win
= find_unmanaged_window(id
)) != NULL
) {
7140 DNPRINTF(SWM_D_MISC
, "manage_window: win 0x%x found on "
7141 "unmanaged list.\n", id
);
7142 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
7145 set_child_transient(win
, &trans
);
7149 DNPRINTF(SWM_D_MISC
, "manage_window: win 0x%x is new.\n", id
);
7152 /* Create and initialize ws_win object. */
7153 if ((win
= calloc(1, sizeof(struct ws_win
))) == NULL
)
7154 err(1, "manage_window: calloc: failed to allocate memory for "
7159 /* Get window geometry. */
7160 win
->wa
= xcb_get_geometry_reply(conn
,
7161 xcb_get_geometry(conn
, win
->id
),
7164 /* Figure out which region the window belongs to. */
7165 r
= root_to_region(win
->wa
->root
, SWM_CK_ALL
);
7167 /* Ignore window border if there is one. */
7168 WIDTH(win
) = win
->wa
->width
;
7169 HEIGHT(win
) = win
->wa
->height
;
7170 X(win
) = win
->wa
->x
+ win
->wa
->border_width
- border_width
;
7171 Y(win
) = win
->wa
->y
+ win
->wa
->border_width
- border_width
;
7173 win
->mapped
= mapped
;
7174 win
->floatmaxed
= 0;
7175 win
->ewmh_flags
= 0;
7176 win
->s
= r
->s
; /* this never changes */
7178 store_float_geom(win
, r
);
7180 /* Get WM_SIZE_HINTS. */
7181 xcb_icccm_get_wm_normal_hints_reply(conn
,
7182 xcb_icccm_get_wm_normal_hints(conn
, win
->id
),
7186 xcb_icccm_get_wm_hints_reply(conn
,
7187 xcb_icccm_get_wm_hints(conn
, win
->id
),
7190 /* Get WM_TRANSIENT_FOR; see if window is a transient. */
7191 xcb_icccm_get_wm_transient_for_reply(conn
,
7192 xcb_icccm_get_wm_transient_for(conn
, win
->id
),
7195 win
->transient
= trans
;
7196 set_child_transient(win
, &win
->transient
);
7199 /* Get supported protocols. */
7200 if (xcb_icccm_get_wm_protocols_reply(conn
,
7201 xcb_icccm_get_wm_protocols(conn
, win
->id
, a_prot
),
7203 for (i
= 0; i
< wpr
.atoms_len
; i
++) {
7204 if (wpr
.atoms
[i
] == a_takefocus
)
7205 win
->take_focus
= 1;
7206 if (wpr
.atoms
[i
] == a_delete
)
7207 win
->can_delete
= 1;
7209 xcb_icccm_get_wm_protocols_reply_wipe(&wpr
);
7212 win
->iconic
= get_swm_iconic(win
);
7214 /* Figure out which workspace the window belongs to. */
7215 if ((p
= find_pid(window_get_pid(win
->id
))) != NULL
) {
7216 win
->ws
= &r
->s
->ws
[p
->ws
];
7217 TAILQ_REMOVE(&pidlist
, p
, entry
);
7220 } else if ((ws_idx
= get_ws_idx(win
->id
)) != -1 &&
7222 /* _SWM_WS is set; use that. */
7223 win
->ws
= &r
->s
->ws
[ws_idx
];
7224 } else if (trans
&& (ww
= find_window(trans
)) != NULL
) {
7225 /* Launch transients in the same ws as parent. */
7231 /* Set the _SWM_WS atom so we can remember this after reincarnation. */
7232 if (snprintf(ws_idx_str
, SWM_PROPLEN
, "%d", win
->ws
->idx
) <
7234 DNPRINTF(SWM_D_PROP
, "manage_window: set _SWM_WS: %s\n",
7236 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
7237 a_swm_ws
, XCB_ATOM_STRING
, 8, strlen(ws_idx_str
),
7242 ewmh_autoquirk(win
);
7244 /* Determine initial quirks. */
7245 if (xcb_icccm_get_wm_class_reply(conn
,
7246 xcb_icccm_get_wm_class(conn
, win
->id
),
7248 DNPRINTF(SWM_D_CLASS
, "manage_window: class: %s, name: %s\n",
7249 win
->ch
.class_name
, win
->ch
.instance_name
);
7251 /* java is retarded so treat it special */
7252 if (strstr(win
->ch
.instance_name
, "sun-awt")) {
7253 DNPRINTF(SWM_D_CLASS
, "manage_window: java window "
7258 TAILQ_FOREACH(qp
, &quirks
, entry
) {
7259 if (!strcmp(win
->ch
.class_name
, qp
->class) &&
7260 !strcmp(win
->ch
.instance_name
, qp
->name
)) {
7261 DNPRINTF(SWM_D_CLASS
, "manage_window: on quirks"
7262 "list; mask: 0x%lx\n", qp
->quirk
);
7263 if (qp
->quirk
& SWM_Q_FLOAT
)
7265 win
->quirks
= qp
->quirk
;
7270 /* Alter window position if quirky */
7271 if (win
->quirks
& SWM_Q_ANYWHERE
)
7274 /* Reset font sizes (the bruteforce way; no default keybinding). */
7275 if (win
->quirks
& SWM_Q_XTERM_FONTADJ
) {
7276 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
7277 fake_keypress(win
, XK_KP_Subtract
, XCB_MOD_MASK_SHIFT
);
7278 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
7279 fake_keypress(win
, XK_KP_Add
, XCB_MOD_MASK_SHIFT
);
7282 /* Make sure window is positioned inside its region, if its active. */
7284 constrain_window(win
, win
->ws
->r
, 0);
7288 /* Select which X events to monitor and set border pixel color. */
7289 wa
[0] = win
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
;
7290 wa
[1] = XCB_EVENT_MASK_ENTER_WINDOW
| XCB_EVENT_MASK_PROPERTY_CHANGE
|
7291 XCB_EVENT_MASK_STRUCTURE_NOTIFY
;
7293 wa
[1] |= XCB_EVENT_MASK_LEAVE_WINDOW
| XCB_EVENT_MASK_FOCUS_CHANGE
;
7296 xcb_change_window_attributes(conn
, win
->id
, XCB_CW_BORDER_PIXEL
|
7297 XCB_CW_EVENT_MASK
, wa
);
7300 /* Figure out where to stack the window in the workspace. */
7301 if (trans
&& (ww
= find_window(trans
)))
7302 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, ww
, win
, entry
);
7303 else if (win
->ws
->focus
&& spawn_position
== SWM_STACK_ABOVE
)
7304 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, win
->ws
->focus
, win
,
7306 else if (win
->ws
->focus
&& spawn_position
== SWM_STACK_BELOW
)
7307 TAILQ_INSERT_BEFORE(win
->ws
->focus
, win
, entry
);
7308 else switch (spawn_position
) {
7311 case SWM_STACK_ABOVE
:
7312 TAILQ_INSERT_TAIL(&win
->ws
->winlist
, win
, entry
);
7314 case SWM_STACK_BOTTOM
:
7315 case SWM_STACK_BELOW
:
7316 TAILQ_INSERT_HEAD(&win
->ws
->winlist
, win
, entry
);
7319 /* Get initial _NET_WM_STATE */
7320 ewmh_get_win_state(win
);
7321 /* Set initial _NET_WM_ALLOWED_ACTIONS */
7322 ewmh_update_actions(win
);
7326 DNPRINTF(SWM_D_MISC
, "manage_window: done. window: 0x%x, (x,y) w x h: "
7327 "(%d,%d) %d x %d, ws: %d, iconic: %s, transient: 0x%x\n", win
->id
,
7328 X(win
), Y(win
), WIDTH(win
), HEIGHT(win
), win
->ws
->idx
,
7329 YESNO(win
->iconic
), win
->transient
);
7335 free_window(struct ws_win
*win
)
7337 DNPRINTF(SWM_D_MISC
, "free_window: window: 0x%x\n", win
->id
);
7342 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
7347 xcb_icccm_get_wm_class_reply_wipe(&win
->ch
);
7352 memset(win
, 0xff, sizeof *win
); /* XXX kill later */
7355 DNPRINTF(SWM_D_MISC
, "free_window: done.\n");
7359 unmanage_window(struct ws_win
*win
)
7361 struct ws_win
*parent
;
7366 DNPRINTF(SWM_D_MISC
, "unmanage_window: window: 0x%x\n", win
->id
);
7368 if (win
->transient
) {
7369 parent
= find_window(win
->transient
);
7371 parent
->focus_child
= NULL
;
7374 TAILQ_REMOVE(&win
->ws
->winlist
, win
, entry
);
7375 TAILQ_INSERT_TAIL(&win
->ws
->unmanagedlist
, win
, entry
);
7379 expose(xcb_expose_event_t
*e
)
7382 struct swm_region
*r
;
7384 DNPRINTF(SWM_D_EVENT
, "expose: window: 0x%x\n", e
->window
);
7386 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7387 for (i
= 0; i
< num_screens
; i
++)
7388 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
7389 if (e
->window
== WINID(r
->bar
))
7397 focusin(xcb_focus_in_event_t
*e
)
7399 DNPRINTF(SWM_D_EVENT
, "focusin: window: 0x%x, mode: %s(%u), "
7400 "detail: %s(%u)\n", e
->event
, get_notify_mode_label(e
->mode
),
7401 e
->mode
, get_notify_detail_label(e
->detail
), e
->detail
);
7405 focusout(xcb_focus_out_event_t
*e
)
7407 DNPRINTF(SWM_D_EVENT
, "focusout: window: 0x%x, mode: %s(%u), "
7408 "detail: %s(%u)\n", e
->event
, get_notify_mode_label(e
->mode
),
7409 e
->mode
, get_notify_detail_label(e
->detail
), e
->detail
);
7414 keypress(xcb_key_press_event_t
*e
)
7416 xcb_keysym_t keysym
;
7419 keysym
= xcb_key_press_lookup_keysym(syms
, e
, 0);
7421 DNPRINTF(SWM_D_EVENT
, "keypress: %u %u\n", e
->detail
, keysym
);
7423 if ((kp
= key_lookup(CLEANMASK(e
->state
), keysym
)) == NULL
)
7426 last_event_time
= e
->time
;
7428 if (kp
->funcid
== KF_SPAWN_CUSTOM
)
7429 spawn_custom(root_to_region(e
->root
, SWM_CK_ALL
),
7430 &(keyfuncs
[kp
->funcid
].args
), kp
->spawn_name
);
7431 else if (keyfuncs
[kp
->funcid
].func
)
7432 keyfuncs
[kp
->funcid
].func(root_to_region(e
->root
, SWM_CK_ALL
),
7433 &(keyfuncs
[kp
->funcid
].args
));
7437 buttonpress(xcb_button_press_event_t
*e
)
7439 struct ws_win
*win
= NULL
;
7440 struct swm_region
*r
, *old_r
;
7444 DNPRINTF(SWM_D_EVENT
, "buttonpress: win (x,y): 0x%x (%d,%d), "
7445 "detail: %u, time: %u, root (x,y): 0x%x (%d,%d), child: 0x%x, "
7446 "state: %u, same_screen: %s\n", e
->event
, e
->event_x
, e
->event_y
,
7447 e
->detail
, e
->time
, e
->root
, e
->root_x
, e
->root_y
, e
->child
,
7448 e
->state
, YESNO(e
->same_screen
));
7450 if (e
->event
== e
->root
) {
7451 if (e
->child
!= 0) {
7452 win
= find_window(e
->child
);
7453 /* Pass ButtonPress to window if it isn't managed. */
7457 /* Focus on empty region */
7458 /* If no windows on region if its empty. */
7459 r
= root_to_region(e
->root
, SWM_CK_POINTER
);
7461 DNPRINTF(SWM_D_EVENT
, "buttonpress: "
7462 "NULL region; ignoring.\n");
7466 if (TAILQ_EMPTY(&r
->ws
->winlist
)) {
7467 old_r
= root_to_region(e
->root
, SWM_CK_FOCUS
);
7468 if (old_r
&& old_r
!= r
)
7469 unfocus_win(old_r
->ws
->focus
);
7471 xcb_set_input_focus(conn
,
7472 XCB_INPUT_FOCUS_PARENT
, e
->root
, e
->time
);
7474 /* Clear bar since empty. */
7482 win
= find_window(e
->event
);
7488 last_event_time
= e
->time
;
7490 focus_win(get_focus_magic(win
));
7492 for (i
= 0; i
< LENGTH(buttons
); i
++)
7493 if (client_click
== buttons
[i
].action
&& buttons
[i
].func
&&
7494 buttons
[i
].button
== e
->detail
&&
7495 CLEANMASK(buttons
[i
].mask
) == CLEANMASK(e
->state
)) {
7496 buttons
[i
].func(win
, &buttons
[i
].args
);
7502 DNPRINTF(SWM_D_EVENT
, "buttonpress: passing to window.\n");
7503 /* Replay event to event window */
7504 xcb_allow_events(conn
, XCB_ALLOW_REPLAY_POINTER
, e
->time
);
7506 DNPRINTF(SWM_D_EVENT
, "buttonpress: handled.\n");
7507 /* Unfreeze grab events. */
7508 xcb_allow_events(conn
, XCB_ALLOW_SYNC_POINTER
, e
->time
);
7516 print_win_geom(xcb_window_t w
)
7518 xcb_get_geometry_reply_t
*wa
;
7520 wa
= xcb_get_geometry_reply(conn
, xcb_get_geometry(conn
, w
), NULL
);
7523 DNPRINTF(SWM_D_MISC
, "print_win_geom: window not found: 0x%x\n",
7528 DNPRINTF(SWM_D_MISC
, "print_win_geom: window: 0x%x, root: 0x%x, "
7529 "depth: %u, (x,y) w x h: (%d,%d) %d x %d, border: %d\n",
7530 w
, wa
->root
, wa
->depth
, wa
->x
, wa
->y
, wa
->width
, wa
->height
,
7539 get_stack_mode_name(uint8_t mode
)
7544 case XCB_STACK_MODE_ABOVE
:
7547 case XCB_STACK_MODE_BELOW
:
7550 case XCB_STACK_MODE_TOP_IF
:
7553 case XCB_STACK_MODE_BOTTOM_IF
:
7556 case XCB_STACK_MODE_OPPOSITE
:
7568 configurerequest(xcb_configure_request_event_t
*e
)
7571 struct swm_region
*r
= NULL
;
7574 uint32_t wc
[7] = {0};
7576 if ((win
= find_window(e
->window
)) == NULL
)
7577 if ((win
= find_unmanaged_window(e
->window
)) == NULL
)
7581 if (swm_debug
& SWM_D_EVENT
) {
7582 print_win_geom(e
->window
);
7584 DNPRINTF(SWM_D_EVENT
, "configurerequest: window: 0x%x, "
7585 "parent: 0x%x, new: %s, value_mask: %u { ", e
->window
,
7586 e
->parent
, YESNO(new), e
->value_mask
);
7587 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
)
7588 DPRINTF("X: %d ", e
->x
);
7589 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
)
7590 DPRINTF("Y: %d ", e
->y
);
7591 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
)
7592 DPRINTF("W: %u ", e
->width
);
7593 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
)
7594 DPRINTF("H: %u ", e
->height
);
7595 if (e
->value_mask
& XCB_CONFIG_WINDOW_BORDER_WIDTH
)
7596 DPRINTF("Border: %u ", e
->border_width
);
7597 if (e
->value_mask
& XCB_CONFIG_WINDOW_SIBLING
)
7598 DPRINTF("Sibling: 0x%x ", e
->sibling
);
7599 if (e
->value_mask
& XCB_CONFIG_WINDOW_STACK_MODE
)
7600 DPRINTF("StackMode: %s(%u) ",
7601 get_stack_mode_name(e
->stack_mode
), e
->stack_mode
);
7607 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
) {
7608 mask
|= XCB_CONFIG_WINDOW_X
;
7611 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
) {
7612 mask
|= XCB_CONFIG_WINDOW_Y
;
7615 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
) {
7616 mask
|= XCB_CONFIG_WINDOW_WIDTH
;
7619 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
) {
7620 mask
|= XCB_CONFIG_WINDOW_HEIGHT
;
7621 wc
[i
++] = e
->height
;
7623 if (e
->value_mask
& XCB_CONFIG_WINDOW_BORDER_WIDTH
) {
7624 mask
|= XCB_CONFIG_WINDOW_BORDER_WIDTH
;
7625 wc
[i
++] = e
->border_width
;
7627 if (e
->value_mask
& XCB_CONFIG_WINDOW_SIBLING
) {
7628 mask
|= XCB_CONFIG_WINDOW_SIBLING
;
7629 wc
[i
++] = e
->sibling
;
7631 if (e
->value_mask
& XCB_CONFIG_WINDOW_STACK_MODE
) {
7632 mask
|= XCB_CONFIG_WINDOW_STACK_MODE
;
7633 wc
[i
++] = e
->stack_mode
;
7637 xcb_configure_window(conn
, e
->window
, mask
, wc
);
7640 } else if ((!win
->manual
|| win
->quirks
& SWM_Q_ANYWHERE
) &&
7641 !(win
->ewmh_flags
& EWMH_F_FULLSCREEN
)) {
7644 else if (win
->ws
->old_r
)
7647 /* windows are centered unless ANYWHERE quirk is set. */
7648 if (win
->quirks
& SWM_Q_ANYWHERE
) {
7649 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
) {
7650 win
->g_float
.x
= e
->x
;
7652 win
->g_float
.x
-= X(r
);
7655 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
) {
7656 win
->g_float
.y
= e
->y
;
7658 win
->g_float
.y
-= Y(r
);
7662 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
)
7663 win
->g_float
.w
= e
->width
;
7665 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
)
7666 win
->g_float
.h
= e
->height
;
7668 win
->g_floatvalid
= 1;
7670 if (win
->floating
&& r
&& (win
->transient
||
7671 win
->ws
->cur_layout
!= &layouts
[SWM_MAX_STACK
])) {
7672 WIDTH(win
) = win
->g_float
.w
;
7673 HEIGHT(win
) = win
->g_float
.h
;
7676 stack_floater(win
, r
);
7688 DNPRINTF(SWM_D_EVENT
, "configurerequest: done.\n");
7692 configurenotify(xcb_configure_notify_event_t
*e
)
7696 DNPRINTF(SWM_D_EVENT
, "configurenotify: win 0x%x, event win: 0x%x, "
7697 "(x,y) WxH: (%d,%d) %ux%u, border: %u, above_sibling: 0x%x, "
7698 "override_redirect: %s\n", e
->window
, e
->event
, e
->x
, e
->y
,
7699 e
->width
, e
->height
, e
->border_width
, e
->above_sibling
,
7700 YESNO(e
->override_redirect
));
7702 win
= find_window(e
->window
);
7704 xcb_icccm_get_wm_normal_hints_reply(conn
,
7705 xcb_icccm_get_wm_normal_hints(conn
, win
->id
),
7708 if (font_adjusted
) {
7716 destroynotify(xcb_destroy_notify_event_t
*e
)
7720 DNPRINTF(SWM_D_EVENT
, "destroynotify: window: 0x%x\n", e
->window
);
7722 if ((win
= find_window(e
->window
)) == NULL
) {
7723 if ((win
= find_unmanaged_window(e
->window
)) == NULL
)
7729 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
7730 /* If we were focused, make sure we focus on something else. */
7731 if (win
== win
->ws
->focus
)
7732 win
->ws
->focus_pending
= get_focus_prev(win
);
7735 unmanage_window(win
);
7738 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
7739 if (win
->ws
->focus_pending
) {
7740 focus_win(win
->ws
->focus_pending
);
7741 win
->ws
->focus_pending
= NULL
;
7752 get_notify_detail_label(uint8_t detail
)
7757 case XCB_NOTIFY_DETAIL_ANCESTOR
:
7760 case XCB_NOTIFY_DETAIL_VIRTUAL
:
7763 case XCB_NOTIFY_DETAIL_INFERIOR
:
7766 case XCB_NOTIFY_DETAIL_NONLINEAR
:
7767 label
= "Nonlinear";
7769 case XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL
:
7770 label
= "NonlinearVirtual";
7772 case XCB_NOTIFY_DETAIL_POINTER
:
7775 case XCB_NOTIFY_DETAIL_POINTER_ROOT
:
7776 label
= "PointerRoot";
7778 case XCB_NOTIFY_DETAIL_NONE
:
7789 get_notify_mode_label(uint8_t mode
)
7794 case XCB_NOTIFY_MODE_NORMAL
:
7797 case XCB_NOTIFY_MODE_GRAB
:
7800 case XCB_NOTIFY_MODE_UNGRAB
:
7803 case XCB_NOTIFY_MODE_WHILE_GRABBED
:
7804 label
= "WhileGrabbed";
7815 enternotify(xcb_enter_notify_event_t
*e
)
7818 struct swm_region
*old_r
, *r
;
7820 DNPRINTF(SWM_D_FOCUS
, "enternotify: time: %u, win (x,y): 0x%x "
7821 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): 0x%x (%d,%d), "
7822 "child: 0x%x, same_screen_focus: %s, state: %d\n",
7823 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
7824 get_notify_mode_label(e
->mode
), e
->mode
,
7825 get_notify_detail_label(e
->detail
), e
->detail
,
7826 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
7827 YESNO(e
->same_screen_focus
), e
->state
);
7829 if (focus_mode
== SWM_FOCUS_MANUAL
&&
7830 e
->mode
== XCB_NOTIFY_MODE_NORMAL
) {
7831 DNPRINTF(SWM_D_EVENT
, "enternotify: manual focus; ignoring.\n");
7835 last_event_time
= e
->time
;
7837 if ((win
= find_window(e
->event
)) == NULL
) {
7838 if (e
->event
== e
->root
) {
7839 /* If no windows on pointer region, then focus root. */
7840 r
= root_to_region(e
->root
, SWM_CK_POINTER
);
7842 DNPRINTF(SWM_D_EVENT
, "enterntoify: "
7843 "NULL region; ignoring.\n");
7847 if (TAILQ_EMPTY(&r
->ws
->winlist
)) {
7848 old_r
= root_to_region(e
->root
, SWM_CK_FOCUS
);
7849 if (old_r
&& old_r
!= r
)
7850 unfocus_win(old_r
->ws
->focus
);
7852 xcb_set_input_focus(conn
,
7853 XCB_INPUT_FOCUS_PARENT
, e
->root
, e
->time
);
7855 /* Clear bar since empty. */
7861 DNPRINTF(SWM_D_EVENT
, "enternotify: window is NULL; "
7867 focus_win(get_focus_magic(win
));
7874 leavenotify(xcb_leave_notify_event_t
*e
)
7876 DNPRINTF(SWM_D_FOCUS
, "leavenotify: time: %u, win (x,y): 0x%x "
7877 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): 0x%x (%d,%d), "
7878 "child: 0x%x, same_screen_focus: %s, state: %d\n",
7879 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
7880 get_notify_mode_label(e
->mode
), e
->mode
,
7881 get_notify_detail_label(e
->detail
), e
->detail
,
7882 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
7883 YESNO(e
->same_screen_focus
), e
->state
);
7888 mapnotify(xcb_map_notify_event_t
*e
)
7892 DNPRINTF(SWM_D_EVENT
, "mapnotify: window: 0x%x\n", e
->window
);
7894 if ((win
= find_window(e
->window
)) == NULL
)
7895 win
= manage_window(e
->window
, 1);
7898 set_win_state(win
, XCB_ICCCM_WM_STATE_NORMAL
);
7900 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
7901 if (win
->ws
->focus_pending
== win
) {
7903 win
->ws
->focus_pending
= NULL
;
7912 mappingnotify(xcb_mapping_notify_event_t
*e
)
7914 xcb_refresh_keyboard_mapping(syms
, e
);
7916 if (e
->request
== XCB_MAPPING_KEYBOARD
)
7921 maprequest(xcb_map_request_event_t
*e
)
7924 xcb_get_window_attributes_reply_t
*war
;
7926 DNPRINTF(SWM_D_EVENT
, "maprequest: win 0x%x\n",
7929 war
= xcb_get_window_attributes_reply(conn
,
7930 xcb_get_window_attributes(conn
, e
->window
),
7933 DNPRINTF(SWM_D_EVENT
, "maprequest: window lost.\n");
7937 if (war
->override_redirect
) {
7938 DNPRINTF(SWM_D_EVENT
, "maprequest: override_redirect; "
7943 win
= manage_window(e
->window
,
7944 (war
->map_state
== XCB_MAP_STATE_VIEWABLE
));
7946 /* The new window should get focus; prepare. */
7947 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
7948 win
->ws
->focus_pending
= get_focus_magic(win
);
7950 /* All windows need to be mapped if they are in the current workspace.*/
7954 /* Ignore EnterNotify to handle the mapnotify without interference. */
7955 if (focus_mode
== SWM_FOCUS_DEFAULT
)
7956 event_drain(XCB_ENTER_NOTIFY
);
7959 DNPRINTF(SWM_D_EVENT
, "maprequest: done.\n");
7964 get_atom_name(xcb_atom_t atom
)
7969 * This should be disabled during most debugging since
7970 * xcb_get_* causes an xcb_flush.
7973 xcb_get_atom_name_reply_t
*r
;
7975 r
= xcb_get_atom_name_reply(conn
,
7976 xcb_get_atom_name(conn
, atom
),
7979 len
= xcb_get_atom_name_name_length(r
);
7981 name
= malloc(len
+ 1);
7983 memcpy(name
, xcb_get_atom_name_name(r
), len
);
7997 propertynotify(xcb_property_notify_event_t
*e
)
8003 name
= get_atom_name(e
->atom
);
8004 DNPRINTF(SWM_D_EVENT
, "propertynotify: window: 0x%x, atom: %s(%u), "
8005 "time: %#x, state: %u\n", e
->window
, name
, e
->atom
, e
->time
,
8009 win
= find_window(e
->window
);
8013 last_event_time
= e
->time
;
8015 if (e
->atom
== a_swm_iconic
) {
8016 if (e
->state
== XCB_PROPERTY_NEW_VALUE
) {
8017 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
8018 win
->ws
->focus_pending
= get_focus_prev(win
);
8025 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
8026 focus_win(win
->ws
->focus_pending
);
8027 win
->ws
->focus_pending
= NULL
;
8031 } else if (e
->state
== XCB_PROPERTY_DELETE
) {
8032 /* The window is no longer iconic, restack ws. */
8033 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
8034 win
->ws
->focus_pending
= get_focus_magic(win
);
8038 /* Flush EnterNotify for mapnotify, if needed. */
8041 } else if (e
->atom
== a_state
) {
8042 /* State just changed, make sure it gets focused if mapped. */
8043 if (e
->state
== XCB_PROPERTY_NEW_VALUE
) {
8044 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
8046 win
->ws
->focus_pending
== win
) {
8047 focus_win(win
->ws
->focus_pending
);
8048 win
->ws
->focus_pending
= NULL
;
8052 } else if (e
->atom
== XCB_ATOM_WM_CLASS
||
8053 e
->atom
== XCB_ATOM_WM_NAME
) {
8061 unmapnotify(xcb_unmap_notify_event_t
*e
)
8064 struct workspace
*ws
;
8066 DNPRINTF(SWM_D_EVENT
, "unmapnotify: window: 0x%x\n", e
->window
);
8068 /* If we aren't managing the window, then ignore. */
8069 win
= find_window(e
->window
);
8075 if (getstate(e
->window
) != XCB_ICCCM_WM_STATE_ICONIC
)
8076 set_win_state(win
, XCB_ICCCM_WM_STATE_ICONIC
);
8079 /* window unmapped itself */
8080 /* do unmap/unfocus/restack and unmanage */
8083 /* If win was focused, make sure to focus on something else. */
8084 if (win
== ws
->focus
) {
8085 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
8086 ws
->focus_pending
= get_focus_prev(win
);
8087 DNPRINTF(SWM_D_EVENT
, "unmapnotify: "
8088 "focus_pending: 0x%x\n",
8089 WINID(ws
->focus_pending
));
8095 unmanage_window(win
);
8100 if (focus_mode
== SWM_FOCUS_FOLLOW
) {
8102 focus_win(get_pointer_win(ws
->r
->s
->root
));
8104 if (ws
->focus_pending
) {
8105 focus_win(ws
->focus_pending
);
8106 ws
->focus_pending
= NULL
;
8111 if (getstate(e
->window
) == XCB_ICCCM_WM_STATE_NORMAL
)
8112 set_win_state(win
, XCB_ICCCM_WM_STATE_ICONIC
);
8119 visibilitynotify(xcb_visibility_notify_event_t
*e
)
8121 DNPRINTF(SWM_D_EVENT
, "visibilitynotify: window: 0x%x\n",
8127 clientmessage(xcb_client_message_event_t
*e
)
8130 xcb_map_request_event_t mre
;
8134 name
= get_atom_name(e
->type
);
8135 DNPRINTF(SWM_D_EVENT
, "clientmessage: window: 0x%x, atom: %s(%u)\n",
8136 e
->window
, name
, e
->type
);
8139 win
= find_window(e
->window
);
8142 if (e
->type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
8143 /* Manage the window with maprequest. */
8144 DNPRINTF(SWM_D_EVENT
, "clientmessage: request focus on "
8145 "unmanaged window.\n");
8146 mre
.window
= e
->window
;
8152 if (e
->type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
8153 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_ACTIVE_WINDOW\n");
8156 if (e
->type
== ewmh
[_NET_CLOSE_WINDOW
].atom
) {
8157 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_CLOSE_WINDOW\n");
8158 if (win
->can_delete
)
8159 client_msg(win
, a_delete
, 0);
8161 xcb_kill_client(conn
, win
->id
);
8163 if (e
->type
== ewmh
[_NET_MOVERESIZE_WINDOW
].atom
) {
8164 DNPRINTF(SWM_D_EVENT
,
8165 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
8166 if (win
->floating
) {
8167 if (e
->data
.data32
[0] & (1<<8)) /* x */
8168 X(win
) = e
->data
.data32
[1];
8169 if (e
->data
.data32
[0] & (1<<9)) /* y */
8170 Y(win
) = e
->data
.data32
[2];
8171 if (e
->data
.data32
[0] & (1<<10)) /* width */
8172 WIDTH(win
) = e
->data
.data32
[3];
8173 if (e
->data
.data32
[0] & (1<<11)) /* height */
8174 HEIGHT(win
) = e
->data
.data32
[4];
8179 /* TODO: Change stack sizes */
8180 /* notify no change was made. */
8181 config_win(win
, NULL
);
8184 if (e
->type
== ewmh
[_NET_WM_STATE
].atom
) {
8185 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_WM_STATE\n");
8186 ewmh_update_win_state(win
, e
->data
.data32
[1], e
->data
.data32
[0]);
8187 if (e
->data
.data32
[2])
8188 ewmh_update_win_state(win
, e
->data
.data32
[2],
8200 int errcode
= xcb_connection_has_error(conn
);
8201 #ifdef XCB_CONN_ERROR
8204 case XCB_CONN_ERROR
:
8205 s
= "Socket error, pipe error or other stream error.";
8207 case XCB_CONN_CLOSED_EXT_NOTSUPPORTED
:
8208 s
= "Extension not supported.";
8210 case XCB_CONN_CLOSED_MEM_INSUFFICIENT
:
8211 s
= "Insufficient memory.";
8213 case XCB_CONN_CLOSED_REQ_LEN_EXCEED
:
8214 s
= "Request length was exceeded.";
8216 case XCB_CONN_CLOSED_PARSE_ERR
:
8217 s
= "Error parsing display string.";
8220 s
= "Unknown error.";
8223 errx(errcode
, "X CONNECTION ERROR: %s", s
);
8226 errx(errcode
, "X CONNECTION ERROR");
8234 const uint32_t val
= XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT
|
8235 XCB_EVENT_MASK_ENTER_WINDOW
;
8237 xcb_void_cookie_t wac
;
8238 xcb_generic_error_t
*error
;
8240 /* this causes an error if some other window manager is running */
8241 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
8242 for (i
= 0; i
< num_screens
; i
++) {
8243 if ((sc
= get_screen(i
)) == NULL
)
8244 errx(1, "ERROR: can't get screen %d.", i
);
8245 DNPRINTF(SWM_D_INIT
, "enable_wm: screen %d, root: 0x%x\n",
8247 wac
= xcb_change_window_attributes_checked(conn
, sc
->root
,
8248 XCB_CW_EVENT_MASK
, &val
);
8249 if ((error
= xcb_request_check(conn
, wac
))) {
8250 DNPRINTF(SWM_D_INIT
, "enable_wm: error_code: %u\n",
8256 /* click to focus on empty region */
8257 xcb_grab_button(conn
, 1, sc
->root
, BUTTONMASK
,
8258 XCB_GRAB_MODE_SYNC
, XCB_GRAB_MODE_ASYNC
, XCB_WINDOW_NONE
,
8259 XCB_CURSOR_NONE
, XCB_BUTTON_INDEX_1
, XCB_BUTTON_MASK_ANY
);
8266 new_region(struct swm_screen
*s
, int x
, int y
, int w
, int h
)
8268 struct swm_region
*r
, *n
;
8269 struct workspace
*ws
= NULL
;
8272 DNPRINTF(SWM_D_MISC
, "new region: screen[%d]:%dx%d+%d+%d\n",
8273 s
->idx
, w
, h
, x
, y
);
8275 /* remove any conflicting regions */
8276 n
= TAILQ_FIRST(&s
->rl
);
8279 n
= TAILQ_NEXT(r
, entry
);
8280 if (X(r
) < (x
+ w
) &&
8281 (X(r
) + WIDTH(r
)) > x
&&
8283 (Y(r
) + HEIGHT(r
)) > y
) {
8284 if (r
->ws
->r
!= NULL
)
8285 r
->ws
->old_r
= r
->ws
->r
;
8288 TAILQ_REMOVE(&s
->rl
, r
, entry
);
8289 TAILQ_INSERT_TAIL(&s
->orl
, r
, entry
);
8293 /* search old regions for one to reuse */
8295 /* size + location match */
8296 TAILQ_FOREACH(r
, &s
->orl
, entry
)
8297 if (X(r
) == x
&& Y(r
) == y
&&
8298 HEIGHT(r
) == h
&& WIDTH(r
) == w
)
8302 TAILQ_FOREACH(r
, &s
->orl
, entry
)
8303 if (HEIGHT(r
) == h
&& WIDTH(r
) == w
)
8307 TAILQ_REMOVE(&s
->orl
, r
, entry
);
8308 /* try to use old region's workspace */
8309 if (r
->ws
->r
== NULL
)
8312 if ((r
= calloc(1, sizeof(struct swm_region
))) == NULL
)
8313 err(1, "new_region: calloc: failed to allocate memory "
8316 /* if we don't have a workspace already, find one */
8318 for (i
= 0; i
< workspace_limit
; i
++)
8319 if (s
->ws
[i
].r
== NULL
) {
8326 errx(1, "new_region: no free workspaces");
8337 TAILQ_INSERT_TAIL(&s
->rl
, r
, entry
);
8343 #ifdef SWM_XRR_HAS_CRTC
8346 #endif /* SWM_XRR_HAS_CRTC */
8347 struct swm_region
*r
;
8349 xcb_randr_get_screen_resources_current_cookie_t src
;
8350 xcb_randr_get_screen_resources_current_reply_t
*srr
;
8351 xcb_randr_get_crtc_info_cookie_t cic
;
8352 xcb_randr_get_crtc_info_reply_t
*cir
= NULL
;
8353 xcb_randr_crtc_t
*crtc
;
8354 xcb_screen_t
*screen
;
8356 DNPRINTF(SWM_D_MISC
, "scan_xrandr: screen: %d\n", i
);
8358 if ((screen
= get_screen(i
)) == NULL
)
8359 errx(1, "ERROR: can't get screen %d.", i
);
8361 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
8362 if (i
>= num_screens
)
8363 errx(1, "scan_xrandr: invalid screen");
8365 /* remove any old regions */
8366 while ((r
= TAILQ_FIRST(&screens
[i
].rl
)) != NULL
) {
8367 r
->ws
->old_r
= r
->ws
->r
= NULL
;
8369 TAILQ_REMOVE(&screens
[i
].rl
, r
, entry
);
8370 TAILQ_INSERT_TAIL(&screens
[i
].orl
, r
, entry
);
8374 /* map virtual screens onto physical screens */
8375 #ifdef SWM_XRR_HAS_CRTC
8376 if (xrandr_support
) {
8377 src
= xcb_randr_get_screen_resources_current(conn
,
8379 srr
= xcb_randr_get_screen_resources_current_reply(conn
, src
,
8382 new_region(&screens
[i
], 0, 0,
8383 screen
->width_in_pixels
,
8384 screen
->height_in_pixels
);
8387 ncrtc
= srr
->num_crtcs
;
8389 crtc
= xcb_randr_get_screen_resources_current_crtcs(srr
);
8390 for (c
= 0; c
< ncrtc
; c
++) {
8391 cic
= xcb_randr_get_crtc_info(conn
, crtc
[c
],
8393 cir
= xcb_randr_get_crtc_info_reply(conn
, cic
, NULL
);
8396 if (cir
->num_outputs
== 0) {
8402 new_region(&screens
[i
], 0, 0,
8403 screen
->width_in_pixels
,
8404 screen
->height_in_pixels
);
8406 new_region(&screens
[i
],
8407 cir
->x
, cir
->y
, cir
->width
, cir
->height
);
8412 #endif /* SWM_XRR_HAS_CRTC */
8414 /* If detection failed, create a single region that spans the screen. */
8415 if (TAILQ_EMPTY(&screens
[i
].rl
))
8416 new_region(&screens
[i
], 0, 0, screen
->width_in_pixels
,
8417 screen
->height_in_pixels
);
8420 DNPRINTF(SWM_D_MISC
, "scan_xrandr: done.\n");
8424 screenchange(xcb_randr_screen_change_notify_event_t
*e
)
8426 struct swm_region
*r
;
8429 DNPRINTF(SWM_D_EVENT
, "screenchange: root: 0x%x\n", e
->root
);
8431 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
8432 /* silly event doesn't include the screen index */
8433 for (i
= 0; i
< num_screens
; i
++)
8434 if (screens
[i
].root
== e
->root
)
8436 if (i
>= num_screens
)
8437 errx(1, "screenchange: screen not found");
8439 /* brute force for now, just re-enumerate the regions */
8443 print_win_geom(e
->root
);
8445 /* add bars to all regions */
8446 for (i
= 0; i
< num_screens
; i
++)
8447 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
8455 xcb_window_t
*wins
= NULL
, trans
;
8457 int i
, j
, num_screens
;
8458 uint16_t state
, manage
, mapped
;
8460 xcb_query_tree_cookie_t qtc
;
8461 xcb_query_tree_reply_t
*qtr
;
8462 xcb_get_window_attributes_cookie_t c
;
8463 xcb_get_window_attributes_reply_t
*r
;
8464 xcb_get_property_cookie_t pc
;
8466 DNPRINTF(SWM_D_INIT
, "grab_windows: begin\n");
8467 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
8468 for (i
= 0; i
< num_screens
; i
++) {
8469 qtc
= xcb_query_tree(conn
, screens
[i
].root
);
8470 qtr
= xcb_query_tree_reply(conn
, qtc
, NULL
);
8473 wins
= xcb_query_tree_children(qtr
);
8474 no
= xcb_query_tree_children_length(qtr
);
8475 /* attach windows to a region */
8476 /* normal windows */
8477 DNPRINTF(SWM_D_INIT
, "grab_windows: grab top level windows.\n");
8478 for (j
= 0; j
< no
; j
++) {
8479 c
= xcb_get_window_attributes(conn
, wins
[j
]);
8480 r
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
8482 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
8483 "doesn't exist.\n", wins
[j
]);
8486 if (r
->override_redirect
) {
8487 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
8488 "override_redirect set.\n", wins
[j
]);
8493 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
8494 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
8496 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
8497 "is transient for %#x.\n", wins
[j
], trans
);
8502 state
= getstate(wins
[j
]);
8503 manage
= state
!= XCB_ICCCM_WM_STATE_WITHDRAWN
;
8504 mapped
= r
->map_state
!= XCB_MAP_STATE_UNMAPPED
;
8505 if (mapped
|| manage
)
8506 manage_window(wins
[j
], mapped
);
8509 /* transient windows */
8510 DNPRINTF(SWM_D_INIT
, "grab_windows: grab transient windows.\n");
8511 for (j
= 0; j
< no
; j
++) {
8512 c
= xcb_get_window_attributes(conn
, wins
[j
]);
8513 r
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
8515 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
8516 "doesn't exist.\n", wins
[j
]);
8519 if (r
->override_redirect
) {
8520 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
8521 "override_redirect set.\n", wins
[j
]);
8526 state
= getstate(wins
[j
]);
8527 manage
= state
!= XCB_ICCCM_WM_STATE_WITHDRAWN
;
8528 mapped
= r
->map_state
!= XCB_MAP_STATE_UNMAPPED
;
8529 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
8530 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
8531 &trans
, NULL
) && manage
)
8532 manage_window(wins
[j
], mapped
);
8537 DNPRINTF(SWM_D_INIT
, "grab_windows: done.\n");
8543 int i
, j
, k
, num_screens
;
8544 struct workspace
*ws
;
8545 uint32_t gcv
[1], wa
[1];
8546 const xcb_query_extension_reply_t
*qep
;
8547 xcb_screen_t
*screen
;
8548 xcb_randr_query_version_cookie_t c
;
8549 xcb_randr_query_version_reply_t
*r
;
8551 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
8552 if ((screens
= calloc(num_screens
,
8553 sizeof(struct swm_screen
))) == NULL
)
8554 err(1, "setup_screens: calloc: failed to allocate memory for "
8557 /* initial Xrandr setup */
8559 qep
= xcb_get_extension_data(conn
, &xcb_randr_id
);
8561 c
= xcb_randr_query_version(conn
, 1, 1);
8562 r
= xcb_randr_query_version_reply(conn
, c
, NULL
);
8564 if (r
->major_version
>= 1) {
8566 xrandr_eventbase
= qep
->first_event
;
8572 wa
[0] = cursors
[XC_LEFT_PTR
].cid
;
8574 /* map physical screens */
8575 for (i
= 0; i
< num_screens
; i
++) {
8576 DNPRINTF(SWM_D_WS
, "setup_screens: init screen: %d\n", i
);
8578 TAILQ_INIT(&screens
[i
].rl
);
8579 TAILQ_INIT(&screens
[i
].orl
);
8580 if ((screen
= get_screen(i
)) == NULL
)
8581 errx(1, "ERROR: can't get screen %d.", i
);
8582 screens
[i
].root
= screen
->root
;
8584 /* set default colors */
8585 setscreencolor("red", i
+ 1, SWM_S_COLOR_FOCUS
);
8586 setscreencolor("rgb:88/88/88", i
+ 1, SWM_S_COLOR_UNFOCUS
);
8587 setscreencolor("rgb:00/80/80", i
+ 1, SWM_S_COLOR_BAR_BORDER
);
8588 setscreencolor("black", i
+ 1, SWM_S_COLOR_BAR
);
8589 setscreencolor("rgb:a0/a0/a0", i
+ 1, SWM_S_COLOR_BAR_FONT
);
8591 /* create graphics context on screen */
8592 screens
[i
].bar_gc
= xcb_generate_id(conn
);
8594 xcb_create_gc(conn
, screens
[i
].bar_gc
, screens
[i
].root
,
8595 XCB_GC_GRAPHICS_EXPOSURES
, gcv
);
8597 /* set default cursor */
8598 xcb_change_window_attributes(conn
, screens
[i
].root
,
8601 /* init all workspaces */
8602 /* XXX these should be dynamically allocated too */
8603 for (j
= 0; j
< SWM_WS_MAX
; j
++) {
8604 ws
= &screens
[i
].ws
[j
];
8608 ws
->focus_prev
= NULL
;
8609 ws
->focus_pending
= NULL
;
8612 TAILQ_INIT(&ws
->winlist
);
8613 TAILQ_INIT(&ws
->unmanagedlist
);
8615 for (k
= 0; layouts
[k
].l_stack
!= NULL
; k
++)
8616 if (layouts
[k
].l_config
!= NULL
)
8617 layouts
[k
].l_config(ws
,
8618 SWM_ARG_ID_STACKINIT
);
8619 ws
->cur_layout
= &layouts
[0];
8620 ws
->cur_layout
->l_string(ws
);
8626 xcb_randr_select_input(conn
, screens
[i
].root
,
8627 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE
);
8634 if ((bar_fonts
= strdup(SWM_BAR_FONTS
)) == NULL
)
8635 err(1, "setup_globals: strdup: failed to allocate memory.");
8637 if ((clock_format
= strdup("%a %b %d %R %Z %Y")) == NULL
)
8638 err(1, "setup_globals: strdup: failed to allocate memory.");
8640 if ((syms
= xcb_key_symbols_alloc(conn
)) == NULL
)
8641 errx(1, "unable to allocate key symbols");
8643 a_state
= get_atom_from_string("WM_STATE");
8644 a_prot
= get_atom_from_string("WM_PROTOCOLS");
8645 a_delete
= get_atom_from_string("WM_DELETE_WINDOW");
8646 a_takefocus
= get_atom_from_string("WM_TAKE_FOCUS");
8647 a_wmname
= get_atom_from_string("WM_NAME");
8648 a_netwmname
= get_atom_from_string("_NET_WM_NAME");
8649 a_utf8_string
= get_atom_from_string("UTF8_STRING");
8650 a_string
= get_atom_from_string("STRING");
8651 a_swm_iconic
= get_atom_from_string("_SWM_ICONIC");
8652 a_swm_ws
= get_atom_from_string("_SWM_WS");
8659 xcb_atom_t netwmcheck
;
8660 xcb_window_t root
, win
;
8662 /* work around sun jdk bugs, code from wmname */
8663 netwmcheck
= get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
8665 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
8666 for (i
= 0; i
< num_screens
; i
++) {
8667 root
= screens
[i
].root
;
8669 win
= xcb_generate_id(conn
);
8670 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, win
, root
,
8671 0, 0, 1, 1, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
8672 XCB_COPY_FROM_PARENT
, 0, NULL
);
8674 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, root
,
8675 netwmcheck
, XCB_ATOM_WINDOW
, 32, 1, &win
);
8676 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
8677 netwmcheck
, XCB_ATOM_WINDOW
, 32, 1, &win
);
8678 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
8679 a_netwmname
, a_utf8_string
, 8, strlen("LG3D"), "LG3D");
8684 shutdown_cleanup(void)
8688 /* disable alarm because the following code may not be interrupted */
8690 if (signal(SIGALRM
, SIG_IGN
) == SIG_ERR
)
8691 err(1, "can't disable alarm");
8701 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
8702 for (i
= 0; i
< num_screens
; ++i
) {
8703 if (screens
[i
].bar_gc
!= 0)
8704 xcb_free_gc(conn
, screens
[i
].bar_gc
);
8705 if (!bar_font_legacy
)
8706 XftColorFree(display
, DefaultVisual(display
, i
),
8707 DefaultColormap(display
, i
), &bar_font_color
);
8710 if (bar_font_legacy
)
8711 XFreeFontSet(display
, bar_fs
);
8713 XftFontClose(display
, bar_font
);
8716 xcb_key_symbols_free(syms
);
8718 xcb_disconnect(conn
);
8722 event_error(xcb_generic_error_t
*e
)
8726 DNPRINTF(SWM_D_EVENT
, "event_error: %s(%u) from %s(%u), sequence: %u, "
8727 "resource_id: %u, minor_code: %u\n",
8728 xcb_event_get_error_label(e
->error_code
), e
->error_code
,
8729 xcb_event_get_request_label(e
->major_code
), e
->major_code
,
8730 e
->sequence
, e
->resource_id
, e
->minor_code
);
8734 event_handle(xcb_generic_event_t
*evt
)
8736 uint8_t type
= XCB_EVENT_RESPONSE_TYPE(evt
);
8738 DNPRINTF(SWM_D_EVENT
, "XCB Event: %s(%d), seq %u\n",
8739 xcb_event_get_label(XCB_EVENT_RESPONSE_TYPE(evt
)),
8740 XCB_EVENT_RESPONSE_TYPE(evt
), evt
->sequence
);
8743 #define EVENT(type, callback) case type: callback((void *)evt); return
8744 EVENT(0, event_error
);
8745 EVENT(XCB_BUTTON_PRESS
, buttonpress
);
8746 /*EVENT(XCB_BUTTON_RELEASE, buttonpress);*/
8747 /*EVENT(XCB_CIRCULATE_NOTIFY, );*/
8748 /*EVENT(XCB_CIRCULATE_REQUEST, );*/
8749 EVENT(XCB_CLIENT_MESSAGE
, clientmessage
);
8750 /*EVENT(XCB_COLORMAP_NOTIFY, );*/
8751 EVENT(XCB_CONFIGURE_NOTIFY
, configurenotify
);
8752 EVENT(XCB_CONFIGURE_REQUEST
, configurerequest
);
8753 /*EVENT(XCB_CREATE_NOTIFY, );*/
8754 EVENT(XCB_DESTROY_NOTIFY
, destroynotify
);
8755 EVENT(XCB_ENTER_NOTIFY
, enternotify
);
8756 EVENT(XCB_EXPOSE
, expose
);
8758 EVENT(XCB_FOCUS_IN
, focusin
);
8759 EVENT(XCB_FOCUS_OUT
, focusout
);
8761 /*EVENT(XCB_GRAPHICS_EXPOSURE, );*/
8762 /*EVENT(XCB_GRAVITY_NOTIFY, );*/
8763 EVENT(XCB_KEY_PRESS
, keypress
);
8764 /*EVENT(XCB_KEY_RELEASE, keypress);*/
8765 /*EVENT(XCB_KEYMAP_NOTIFY, );*/
8767 EVENT(XCB_LEAVE_NOTIFY
, leavenotify
);
8769 EVENT(XCB_MAP_NOTIFY
, mapnotify
);
8770 EVENT(XCB_MAP_REQUEST
, maprequest
);
8771 EVENT(XCB_MAPPING_NOTIFY
, mappingnotify
);
8772 /*EVENT(XCB_MOTION_NOTIFY, );*/
8773 /*EVENT(XCB_NO_EXPOSURE, );*/
8774 EVENT(XCB_PROPERTY_NOTIFY
, propertynotify
);
8775 /*EVENT(XCB_REPARENT_NOTIFY, );*/
8776 /*EVENT(XCB_RESIZE_REQUEST, );*/
8777 /*EVENT(XCB_SELECTION_CLEAR, );*/
8778 /*EVENT(XCB_SELECTION_NOTIFY, );*/
8779 /*EVENT(XCB_SELECTION_REQUEST, );*/
8780 EVENT(XCB_UNMAP_NOTIFY
, unmapnotify
);
8781 /*EVENT(XCB_VISIBILITY_NOTIFY, visibilitynotify);*/
8784 if (type
- xrandr_eventbase
== XCB_RANDR_SCREEN_CHANGE_NOTIFY
)
8785 screenchange((void *)evt
);
8789 main(int argc
, char *argv
[])
8791 struct swm_region
*r
, *rr
;
8792 struct ws_win
*winfocus
= NULL
;
8793 char conf
[PATH_MAX
], *cfile
= NULL
;
8795 int xfd
, i
, num_screens
;
8796 struct sigaction sact
;
8797 xcb_generic_event_t
*evt
;
8801 /* suppress unused warning since var is needed */
8804 time_started
= time(NULL
);
8807 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION
, buildstr
);
8808 if (!setlocale(LC_CTYPE
, "") || !setlocale(LC_TIME
, ""))
8809 warnx("no locale support");
8811 /* handle some signals */
8812 bzero(&sact
, sizeof(sact
));
8813 sigemptyset(&sact
.sa_mask
);
8815 sact
.sa_handler
= sighdlr
;
8816 sigaction(SIGINT
, &sact
, NULL
);
8817 sigaction(SIGQUIT
, &sact
, NULL
);
8818 sigaction(SIGTERM
, &sact
, NULL
);
8819 sigaction(SIGHUP
, &sact
, NULL
);
8821 sact
.sa_handler
= sighdlr
;
8822 sact
.sa_flags
= SA_NOCLDSTOP
;
8823 sigaction(SIGCHLD
, &sact
, NULL
);
8825 if (!(display
= XOpenDisplay(0)))
8826 errx(1, "can not open display");
8828 conn
= XGetXCBConnection(display
);
8829 if (xcb_connection_has_error(conn
))
8830 errx(1, "can not get XCB connection");
8832 XSetEventQueueOwner(display
, XCBOwnsEventQueue
);
8834 xcb_prefetch_extension_data(conn
, &xcb_randr_id
);
8835 xfd
= xcb_get_file_descriptor(conn
);
8837 /* look for local and global conf file */
8838 pwd
= getpwuid(getuid());
8840 errx(1, "invalid user: %d", getuid());
8842 xcb_grab_server(conn
);
8845 /* flush all events */
8846 while ((evt
= xcb_poll_for_event(conn
))) {
8847 if (XCB_EVENT_RESPONSE_TYPE(evt
) == 0)
8852 if (enable_wm() != 0)
8853 errx(1, "another window manager is currently running");
8855 /* Load Xcursors and/or cursorfont glyph cursors. */
8867 for (i
= 0; ; i
++) {
8872 snprintf(conf
, sizeof conf
, "%s/.%s",
8873 pwd
->pw_dir
, SWM_CONF_FILE
);
8877 snprintf(conf
, sizeof conf
, "/etc/%s",
8882 snprintf(conf
, sizeof conf
, "%s/.%s",
8883 pwd
->pw_dir
, SWM_CONF_FILE_OLD
);
8887 snprintf(conf
, sizeof conf
, "/etc/%s",
8894 if (strlen(conf
) && stat(conf
, &sb
) != -1)
8895 if (S_ISREG(sb
.st_mode
)) {
8902 /* load conf (if any) */
8904 conf_load(cfile
, SWM_CONF_DEFAULT
);
8907 /* set some values to work around bad programs */
8909 /* grab existing windows (before we build the bars) */
8912 if (getenv("SWM_STARTED") == NULL
)
8913 setenv("SWM_STARTED", "YES", 1);
8915 /* setup all bars */
8916 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
8917 for (i
= 0; i
< num_screens
; i
++)
8918 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
8919 if (winfocus
== NULL
)
8920 winfocus
= TAILQ_FIRST(&r
->ws
->winlist
);
8927 xcb_ungrab_server(conn
);
8931 while ((evt
= xcb_poll_for_event(conn
))) {
8938 /* If just (re)started, set default focus if needed. */
8939 if (winfocus
&& focus_mode
!= SWM_FOCUS_FOLLOW
) {
8940 rr
= winfocus
->ws
->r
;
8942 /* not a visible window */
8947 focus_win(get_region_focus(rr
));
8957 if (select(xfd
+ 1, &rd
, NULL
, NULL
, &tv
) == -1)
8958 if (errno
!= EINTR
) {
8959 DNPRINTF(SWM_D_MISC
, "select failed");
8962 restart(NULL
, NULL
);