2 * Copyright (c) 2009-2012 Marco Peereboom <marco@peereboom.us>
3 * Copyright (c) 2009-2011 Ryan McBride <mcbride@countersiege.com>
4 * Copyright (c) 2009 Darrin Chandler <dwchandler@stilyagin.com>
5 * Copyright (c) 2009 Pierre-Yves Ritschard <pyr@spootnik.org>
6 * Copyright (c) 2010 Tuukka Kataja <stuge@xor.fi>
7 * Copyright (c) 2011 Jason L. Wright <jason@thought.net>
8 * Copyright (c) 2011-2012 Reginald Kennedy <rk@rejii.com>
9 * Copyright (c) 2011-2012 Lawrence Teo <lteo@lteo.net>
10 * Copyright (c) 2011-2012 Tiago Cunha <tcunha@gmx.com>
11 * Copyright (c) 2012 David Hill <dhill@mindcry.org>
13 * Permission to use, copy, modify, and distribute this software for any
14 * purpose with or without fee is hereby granted, provided that the above
15 * copyright notice and this permission notice appear in all copies.
17 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
18 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
20 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
21 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
22 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
23 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 * Much code and ideas taken from dwm under the following license:
27 * MIT/X Consortium License
29 * 2006-2008 Anselm R Garbe <garbeam at gmail dot com>
30 * 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
31 * 2006-2007 Jukka Salmi <jukka at salmi dot ch>
32 * 2007 Premysl Hruby <dfenze at gmail dot com>
33 * 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
34 * 2007 Christof Musik <christof at sendfax dot de>
35 * 2007-2008 Enno Gottox Boland <gottox at s01 dot de>
36 * 2007-2008 Peter Hartlich <sgkkr at hartlich dot com>
37 * 2008 Martin Hurton <martin dot hurton at gmail dot com>
39 * Permission is hereby granted, free of charge, to any person obtaining a
40 * copy of this software and associated documentation files (the "Software"),
41 * to deal in the Software without restriction, including without limitation
42 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
43 * and/or sell copies of the Software, and to permit persons to whom the
44 * Software is furnished to do so, subject to the following conditions:
46 * The above copyright notice and this permission notice shall be included in
47 * all copies or substantial portions of the Software.
49 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
50 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
51 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
52 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
53 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
54 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
55 * DEALINGS IN THE SOFTWARE.
59 #include <sys/types.h>
63 #include <sys/queue.h>
64 #include <sys/param.h>
65 #include <sys/select.h>
66 #if defined(__linux__)
68 #elif defined(__OpenBSD__)
70 #elif defined(__FreeBSD__)
91 #include <X11/cursorfont.h>
92 #include <X11/extensions/Xrandr.h>
93 #include <X11/Xcursor/Xcursor.h>
94 #include <X11/Xft/Xft.h>
95 #include <X11/Xlib-xcb.h>
96 #include <xcb/xcb_atom.h>
97 #include <xcb/xcb_aux.h>
98 #include <xcb/xcb_event.h>
99 #include <xcb/xcb_icccm.h>
100 #include <xcb/xcb_keysyms.h>
101 #include <xcb/xtest.h>
102 #include <xcb/randr.h>
110 #ifdef SPECTRWM_BUILDSTR
111 static const char *buildstr
= SPECTRWM_BUILDSTR
;
113 static const char *buildstr
= SPECTRWM_VERSION
;
116 #if !defined(__CYGWIN__) /* cygwin chokes on xrandr stuff */
118 # error XRandR versions less than 1.0 are not supported
121 # if RANDR_MAJOR >= 1
122 # if RANDR_MINOR >= 2
123 # define SWM_XRR_HAS_CRTC
126 #endif /* __CYGWIN__ */
128 #ifndef XCB_ICCCM_NUM_WM_HINTS_ELEMENTS
129 #define XCB_ICCCM_SIZE_HINT_P_MIN_SIZE XCB_SIZE_HINT_P_MIN_SIZE
130 #define XCB_ICCCM_SIZE_HINT_P_MAX_SIZE XCB_SIZE_HINT_P_MAX_SIZE
131 #define XCB_ICCCM_SIZE_HINT_P_RESIZE_INC XCB_SIZE_HINT_P_RESIZE_INC
132 #define XCB_ICCCM_WM_HINT_INPUT XCB_WM_HINT_INPUT
133 #define XCB_ICCCM_WM_HINT_X_URGENCY XCB_WM_HINT_X_URGENCY
134 #define XCB_ICCCM_WM_STATE_ICONIC XCB_WM_STATE_ICONIC
135 #define XCB_ICCCM_WM_STATE_WITHDRAWN XCB_WM_STATE_WITHDRAWN
136 #define XCB_ICCCM_WM_STATE_NORMAL XCB_WM_STATE_NORMAL
137 #define xcb_icccm_get_text_property_reply_t xcb_get_text_property_reply_t
138 #define xcb_icccm_get_text_property_reply_wipe xcb_get_text_property_reply_wipe
139 #define xcb_icccm_get_wm_class xcb_get_wm_class
140 #define xcb_icccm_get_wm_class_reply xcb_get_wm_class_reply
141 #define xcb_icccm_get_wm_class_reply_t xcb_get_wm_class_reply_t
142 #define xcb_icccm_get_wm_class_reply_wipe xcb_get_wm_class_reply_wipe
143 #define xcb_icccm_get_wm_hints xcb_get_wm_hints
144 #define xcb_icccm_get_wm_hints_reply xcb_get_wm_hints_reply
145 #define xcb_icccm_get_wm_name xcb_get_wm_name
146 #define xcb_icccm_get_wm_name_reply xcb_get_wm_name_reply
147 #define xcb_icccm_get_wm_normal_hints xcb_get_wm_normal_hints
148 #define xcb_icccm_get_wm_normal_hints_reply xcb_get_wm_normal_hints_reply
149 #define xcb_icccm_get_wm_protocols xcb_get_wm_protocols
150 #define xcb_icccm_get_wm_protocols_reply xcb_get_wm_protocols_reply
151 #define xcb_icccm_get_wm_protocols_reply_t xcb_get_wm_protocols_reply_t
152 #define xcb_icccm_get_wm_protocols_reply_wipe xcb_get_wm_protocols_reply_wipe
153 #define xcb_icccm_get_wm_transient_for xcb_get_wm_transient_for
154 #define xcb_icccm_get_wm_transient_for_reply xcb_get_wm_transient_for_reply
155 #define xcb_icccm_wm_hints_t xcb_wm_hints_t
158 /*#define SWM_DEBUG*/
160 #define DPRINTF(x...) do { \
162 fprintf(stderr, x); \
164 #define DNPRINTF(n,x...) do { \
165 if (swm_debug & n) { \
166 fprintf(stderr, "%ld ", (long)(time(NULL) - time_started)); \
167 fprintf(stderr, x); \
170 #define SWM_D_MISC 0x0001
171 #define SWM_D_EVENT 0x0002
172 #define SWM_D_WS 0x0004
173 #define SWM_D_FOCUS 0x0008
174 #define SWM_D_MOVE 0x0010
175 #define SWM_D_STACK 0x0020
176 #define SWM_D_MOUSE 0x0040
177 #define SWM_D_PROP 0x0080
178 #define SWM_D_CLASS 0x0100
179 #define SWM_D_KEY 0x0200
180 #define SWM_D_QUIRK 0x0400
181 #define SWM_D_SPAWN 0x0800
182 #define SWM_D_EVENTQ 0x1000
183 #define SWM_D_CONF 0x2000
184 #define SWM_D_BAR 0x4000
185 #define SWM_D_INIT 0x8000
187 u_int32_t swm_debug
= 0
206 #define DPRINTF(x...)
207 #define DNPRINTF(n,x...)
210 /* convert 8-bit to 16-bit */
211 #define RGB_8_TO_16(col) ((col) << 8) + (col)
213 #define PIXEL_TO_XRENDERCOLOR(px, xrc) \
214 xrc.red = RGB_8_TO_16((px) >> 16 & 0xff); \
215 xrc.green = RGB_8_TO_16((px) >> 8 & 0xff); \
216 xrc.blue = RGB_8_TO_16((px) & 0xff); \
219 #define LENGTH(x) (int)(sizeof (x) / sizeof (x)[0])
220 #define MODKEY XCB_MOD_MASK_1
221 #define CLEANMASK(mask) ((mask) & ~(numlockmask | XCB_MOD_MASK_LOCK))
222 #define BUTTONMASK (XCB_EVENT_MASK_BUTTON_PRESS | \
223 XCB_EVENT_MASK_BUTTON_RELEASE)
224 #define MOUSEMASK (BUTTONMASK|XCB_EVENT_MASK_POINTER_MOTION)
225 #define SWM_PROPLEN (16)
226 #define SWM_FUNCNAME_LEN (32)
227 #define SWM_KEYS_LEN (255)
228 #define SWM_QUIRK_LEN (64)
229 #define X(r) (r)->g.x
230 #define Y(r) (r)->g.y
231 #define WIDTH(r) (r)->g.w
232 #define HEIGHT(r) (r)->g.h
233 #define BORDER(w) ((w)->bordered ? border_width : 0)
234 #define MAX_X(r) ((r)->g.x + (r)->g.w)
235 #define MAX_Y(r) ((r)->g.y + (r)->g.h)
236 #define SH_MIN(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE
237 #define SH_MIN_W(w) (w)->sh.min_width
238 #define SH_MIN_H(w) (w)->sh.min_height
239 #define SH_MAX(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MAX_SIZE
240 #define SH_MAX_W(w) (w)->sh.max_width
241 #define SH_MAX_H(w) (w)->sh.max_height
242 #define SH_INC(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_RESIZE_INC
243 #define SH_INC_W(w) (w)->sh.width_inc
244 #define SH_INC_H(w) (w)->sh.height_inc
245 #define SWM_MAX_FONT_STEPS (3)
246 #define WINID(w) ((w) ? (w)->id : 0)
247 #define YESNO(x) ((x) ? "yes" : "no")
249 #define SWM_FOCUS_DEFAULT (0)
250 #define SWM_FOCUS_FOLLOW (1)
251 #define SWM_FOCUS_MANUAL (2)
253 #define SWM_CK_NONE 0
254 #define SWM_CK_ALL 0x7
255 #define SWM_CK_FOCUS 0x1
256 #define SWM_CK_POINTER 0x2
257 #define SWM_CK_FALLBACK 0x4
259 #define SWM_CONF_DEFAULT (0)
260 #define SWM_CONF_KEYMAPPING (1)
263 #define SWM_LIB "/usr/local/lib/libswmhack.so"
270 xcb_atom_t a_takefocus
;
272 xcb_atom_t a_netwmname
;
273 xcb_atom_t a_utf8_string
;
275 xcb_atom_t a_swm_iconic
;
277 volatile sig_atomic_t running
= 1;
278 volatile sig_atomic_t restart_wm
= 0;
279 xcb_timestamp_t last_event_time
= 0;
284 int xrandr_eventbase
;
285 unsigned int numlockmask
= 0;
288 xcb_connection_t
*conn
;
289 xcb_key_symbols_t
*syms
;
292 int cycle_visible
= 0;
294 int font_adjusted
= 0;
295 unsigned int mod_key
= MODKEY
;
298 struct swm_region
*search_r
;
299 int select_list_pipe
[2];
300 int select_resp_pipe
[2];
302 volatile sig_atomic_t search_resp
;
303 int search_resp_action
;
305 struct search_window
{
306 TAILQ_ENTRY(search_window
) entry
;
310 xcb_window_t indicator
;
312 TAILQ_HEAD(search_winlist
, search_window
);
313 struct search_winlist search_wl
;
318 SWM_SEARCH_UNICONIFY
,
319 SWM_SEARCH_NAME_WORKSPACE
,
320 SWM_SEARCH_SEARCH_WORKSPACE
,
321 SWM_SEARCH_SEARCH_WINDOW
324 #define SWM_STACK_TOP (0)
325 #define SWM_STACK_BOTTOM (1)
326 #define SWM_STACK_ABOVE (2)
327 #define SWM_STACK_BELOW (3)
330 double dialog_ratio
= 0.6;
332 #define SWM_BAR_MAX (256)
333 #define SWM_BAR_JUSTIFY_LEFT (0)
334 #define SWM_BAR_JUSTIFY_CENTER (1)
335 #define SWM_BAR_JUSTIFY_RIGHT (2)
336 #define SWM_BAR_OFFSET (4)
337 #define SWM_BAR_FONTS "-*-terminus-medium-*-*-*-12-*-*-*-*-*-*-*," \
338 "-*-profont-*-*-*-*-12-*-*-*-*-*-*-*," \
339 "-*-times-medium-r-*-*-12-*-*-*-*-*-*-*," \
340 "-misc-fixed-medium-r-*-*-12-*-*-*-*-*-*-*," \
341 "-*-*-*-r-*-*-*-*-*-*-*-*-*-*"
343 #ifdef X_HAVE_UTF8_STRING
344 #define DRAWSTRING(x...) Xutf8DrawString(x)
346 #define DRAWSTRING(x...) XmbDrawString(x)
349 char *bar_argv
[] = { NULL
, NULL
};
351 unsigned char bar_ext
[SWM_BAR_MAX
];
352 char bar_vertext
[SWM_BAR_MAX
];
354 sig_atomic_t bar_alarm
= 0;
357 int bar_border_width
= 1;
358 int bar_at_bottom
= 0;
360 int bar_extra_running
= 0;
363 int bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
364 char *bar_format
= NULL
;
365 int stack_enabled
= 1;
366 int clock_enabled
= 1;
367 int urgent_enabled
= 0;
368 char *clock_format
= NULL
;
369 int title_name_enabled
= 0;
370 int title_class_enabled
= 0;
371 int window_name_enabled
= 0;
372 int focus_mode
= SWM_FOCUS_DEFAULT
;
373 int focus_close
= SWM_STACK_BELOW
;
374 int focus_close_wrap
= 1;
375 int focus_default
= SWM_STACK_TOP
;
376 int spawn_position
= SWM_STACK_TOP
;
377 int disable_border
= 0;
378 int border_width
= 1;
379 int verbose_layout
= 0;
383 XFontSetExtents
*bar_fs_extents
;
385 int bar_font_legacy
= 1;
387 XftColor bar_font_color
;
390 /* layout manager data */
391 struct swm_geometry
{
404 struct swm_geometry g
;
407 /* virtual "screens" */
409 TAILQ_ENTRY(swm_region
) entry
;
410 struct swm_geometry g
;
411 struct workspace
*ws
; /* current workspace on this region */
412 struct workspace
*ws_prior
; /* prior workspace on this region */
413 struct swm_screen
*s
; /* screen idx */
416 TAILQ_HEAD(swm_region_list
, swm_region
);
419 TAILQ_ENTRY(ws_win
) entry
;
421 xcb_window_t transient
;
422 struct ws_win
*focus_child
; /* focus on child transient */
423 struct swm_geometry g
; /* current geometry */
424 struct swm_geometry g_float
; /* region coordinates */
425 int g_floatvalid
; /* g_float geometry validity */
426 int floatmaxed
; /* whether maxed by max_stack */
432 unsigned int ewmh_flags
;
433 int font_size_boundary
[SWM_MAX_FONT_STEPS
];
439 unsigned long quirks
;
440 struct workspace
*ws
; /* always valid */
441 struct swm_screen
*s
; /* always valid, never changes */
442 xcb_get_geometry_reply_t
*wa
;
444 xcb_icccm_get_wm_class_reply_t ch
;
445 xcb_icccm_wm_hints_t hints
;
447 TAILQ_HEAD(ws_win_list
, ws_win
);
451 TAILQ_ENTRY(pid_e
) entry
;
455 TAILQ_HEAD(pid_list
, pid_e
);
456 struct pid_list pidlist
= TAILQ_HEAD_INITIALIZER(pidlist
);
458 /* layout handlers */
460 void vertical_config(struct workspace
*, int);
461 void vertical_stack(struct workspace
*, struct swm_geometry
*);
462 void horizontal_config(struct workspace
*, int);
463 void horizontal_stack(struct workspace
*, struct swm_geometry
*);
464 void max_stack(struct workspace
*, struct swm_geometry
*);
465 void plain_stacker(struct workspace
*);
466 void fancy_stacker(struct workspace
*);
469 void (*l_stack
)(struct workspace
*, struct swm_geometry
*);
470 void (*l_config
)(struct workspace
*, int);
472 #define SWM_L_FOCUSPREV (1<<0)
473 #define SWM_L_MAPONFOCUS (1<<1)
474 void (*l_string
)(struct workspace
*);
476 /* stack, configure */
477 { vertical_stack
, vertical_config
, 0, plain_stacker
},
478 { horizontal_stack
, horizontal_config
, 0, plain_stacker
},
480 SWM_L_MAPONFOCUS
| SWM_L_FOCUSPREV
, plain_stacker
},
481 { NULL
, NULL
, 0, NULL
},
484 /* position of max_stack mode in the layouts array, index into layouts! */
485 #define SWM_V_STACK (0)
486 #define SWM_H_STACK (1)
487 #define SWM_MAX_STACK (2)
489 #define SWM_H_SLICE (32)
490 #define SWM_V_SLICE (32)
492 /* define work spaces */
494 int idx
; /* workspace index */
495 char *name
; /* workspace name */
496 int always_raise
; /* raise windows on focus */
497 struct layout
*cur_layout
; /* current layout handlers */
498 struct ws_win
*focus
; /* may be NULL */
499 struct ws_win
*focus_prev
; /* may be NULL */
500 struct ws_win
*focus_pending
; /* may be NULL */
501 struct swm_region
*r
; /* may be NULL */
502 struct swm_region
*old_r
; /* may be NULL */
503 struct ws_win_list winlist
; /* list of windows in ws */
504 struct ws_win_list unmanagedlist
; /* list of dead windows in ws */
505 char stacker
[10]; /* display stacker and layout */
509 int horizontal_msize
;
511 int horizontal_stacks
;
522 SWM_S_COLOR_BAR_BORDER
,
523 SWM_S_COLOR_BAR_FONT
,
529 /* physical screen mapping */
530 #define SWM_WS_MAX (22) /* hard limit */
531 int workspace_limit
= 10; /* soft limit */
534 int idx
; /* screen index */
535 struct swm_region_list rl
; /* list of regions on this screen */
536 struct swm_region_list orl
; /* list of old regions */
538 struct workspace ws
[SWM_WS_MAX
];
544 } c
[SWM_S_COLOR_MAX
];
546 xcb_gcontext_t bar_gc
;
549 struct swm_screen
*screens
;
551 /* args to functions */
554 #define SWM_ARG_ID_FOCUSNEXT (0)
555 #define SWM_ARG_ID_FOCUSPREV (1)
556 #define SWM_ARG_ID_FOCUSMAIN (2)
557 #define SWM_ARG_ID_SWAPNEXT (10)
558 #define SWM_ARG_ID_SWAPPREV (11)
559 #define SWM_ARG_ID_SWAPMAIN (12)
560 #define SWM_ARG_ID_MOVELAST (13)
561 #define SWM_ARG_ID_MASTERSHRINK (20)
562 #define SWM_ARG_ID_MASTERGROW (21)
563 #define SWM_ARG_ID_MASTERADD (22)
564 #define SWM_ARG_ID_MASTERDEL (23)
565 #define SWM_ARG_ID_FLIPLAYOUT (24)
566 #define SWM_ARG_ID_STACKRESET (30)
567 #define SWM_ARG_ID_STACKINIT (31)
568 #define SWM_ARG_ID_CYCLEWS_UP (40)
569 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
570 #define SWM_ARG_ID_CYCLESC_UP (42)
571 #define SWM_ARG_ID_CYCLESC_DOWN (43)
572 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
573 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
574 #define SWM_ARG_ID_STACKINC (50)
575 #define SWM_ARG_ID_STACKDEC (51)
576 #define SWM_ARG_ID_SS_ALL (60)
577 #define SWM_ARG_ID_SS_WINDOW (61)
578 #define SWM_ARG_ID_DONTCENTER (70)
579 #define SWM_ARG_ID_CENTER (71)
580 #define SWM_ARG_ID_KILLWINDOW (80)
581 #define SWM_ARG_ID_DELETEWINDOW (81)
582 #define SWM_ARG_ID_WIDTHGROW (90)
583 #define SWM_ARG_ID_WIDTHSHRINK (91)
584 #define SWM_ARG_ID_HEIGHTGROW (92)
585 #define SWM_ARG_ID_HEIGHTSHRINK (93)
586 #define SWM_ARG_ID_MOVEUP (100)
587 #define SWM_ARG_ID_MOVEDOWN (101)
588 #define SWM_ARG_ID_MOVELEFT (102)
589 #define SWM_ARG_ID_MOVERIGHT (103)
595 TAILQ_ENTRY(quirk
) entry
;
599 #define SWM_Q_FLOAT (1<<0) /* float this window */
600 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
601 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
602 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
603 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
604 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
606 TAILQ_HEAD(quirk_list
, quirk
);
607 struct quirk_list quirks
= TAILQ_HEAD_INITIALIZER(quirks
);
610 * Supported EWMH hints should be added to
611 * both the enum and the ewmh array
616 _NET_MOVERESIZE_WINDOW
,
617 _NET_WM_ACTION_ABOVE
,
618 _NET_WM_ACTION_CLOSE
,
619 _NET_WM_ACTION_FULLSCREEN
,
621 _NET_WM_ACTION_RESIZE
,
622 _NET_WM_ALLOWED_ACTIONS
,
626 _NET_WM_STATE_FULLSCREEN
,
627 _NET_WM_STATE_HIDDEN
,
628 _NET_WM_STATE_MAXIMIZED_HORZ
,
629 _NET_WM_STATE_MAXIMIZED_VERT
,
630 _NET_WM_STATE_SKIP_PAGER
,
631 _NET_WM_STATE_SKIP_TASKBAR
,
633 _NET_WM_WINDOW_TYPE_DIALOG
,
634 _NET_WM_WINDOW_TYPE_DOCK
,
635 _NET_WM_WINDOW_TYPE_NORMAL
,
636 _NET_WM_WINDOW_TYPE_SPLASH
,
637 _NET_WM_WINDOW_TYPE_TOOLBAR
,
638 _NET_WM_WINDOW_TYPE_UTILITY
,
639 _SWM_WM_STATE_MANUAL
,
646 } ewmh
[SWM_EWMH_HINT_MAX
] = {
647 /* must be in same order as in the enum */
648 {"_NET_ACTIVE_WINDOW", XCB_ATOM_NONE
},
649 {"_NET_CLOSE_WINDOW", XCB_ATOM_NONE
},
650 {"_NET_MOVERESIZE_WINDOW", XCB_ATOM_NONE
},
651 {"_NET_WM_ACTION_ABOVE", XCB_ATOM_NONE
},
652 {"_NET_WM_ACTION_CLOSE", XCB_ATOM_NONE
},
653 {"_NET_WM_ACTION_FULLSCREEN", XCB_ATOM_NONE
},
654 {"_NET_WM_ACTION_MOVE", XCB_ATOM_NONE
},
655 {"_NET_WM_ACTION_RESIZE", XCB_ATOM_NONE
},
656 {"_NET_WM_ALLOWED_ACTIONS", XCB_ATOM_NONE
},
657 {"_NET_WM_NAME", XCB_ATOM_NONE
},
658 {"_NET_WM_STATE", XCB_ATOM_NONE
},
659 {"_NET_WM_STATE_ABOVE", XCB_ATOM_NONE
},
660 {"_NET_WM_STATE_FULLSCREEN", XCB_ATOM_NONE
},
661 {"_NET_WM_STATE_HIDDEN", XCB_ATOM_NONE
},
662 {"_NET_WM_STATE_MAXIMIZED_HORZ", XCB_ATOM_NONE
},
663 {"_NET_WM_STATE_MAXIMIZED_VERT", XCB_ATOM_NONE
},
664 {"_NET_WM_STATE_SKIP_PAGER", XCB_ATOM_NONE
},
665 {"_NET_WM_STATE_SKIP_TASKBAR", XCB_ATOM_NONE
},
666 {"_NET_WM_WINDOW_TYPE", XCB_ATOM_NONE
},
667 {"_NET_WM_WINDOW_TYPE_DIALOG", XCB_ATOM_NONE
},
668 {"_NET_WM_WINDOW_TYPE_DOCK", XCB_ATOM_NONE
},
669 {"_NET_WM_WINDOW_TYPE_NORMAL", XCB_ATOM_NONE
},
670 {"_NET_WM_WINDOW_TYPE_SPLASH", XCB_ATOM_NONE
},
671 {"_NET_WM_WINDOW_TYPE_TOOLBAR", XCB_ATOM_NONE
},
672 {"_NET_WM_WINDOW_TYPE_UTILITY", XCB_ATOM_NONE
},
673 {"_SWM_WM_STATE_MANUAL", XCB_ATOM_NONE
},
680 XC_BOTTOM_LEFT_CORNER
,
681 XC_BOTTOM_RIGHT_CORNER
,
689 char *name
; /* Name used by Xcursor .*/
690 uint8_t cf_char
; /* cursorfont index. */
692 } cursors
[XC_MAX
] = {
693 {"fleur", XC_fleur
, XCB_CURSOR_NONE
},
694 {"left_ptr", XC_left_ptr
, XCB_CURSOR_NONE
},
695 {"bottom_left_corner", XC_bottom_left_corner
, XCB_CURSOR_NONE
},
696 {"bottom_right_corner", XC_bottom_right_corner
, XCB_CURSOR_NONE
},
697 {"sizing", XC_sizing
, XCB_CURSOR_NONE
},
698 {"top_left_corner", XC_top_left_corner
, XCB_CURSOR_NONE
},
699 {"top_right_corner", XC_top_right_corner
, XCB_CURSOR_NONE
},
702 /* function prototypes */
703 void buttonpress(xcb_button_press_event_t
*);
704 void check_conn(void);
705 void clientmessage(xcb_client_message_event_t
*);
706 int conf_load(char *, int);
707 void configurenotify(xcb_configure_notify_event_t
*);
708 void configurerequest(xcb_configure_request_event_t
*);
709 void constrain_window(struct ws_win
*, struct swm_region
*, int);
710 void destroynotify(xcb_destroy_notify_event_t
*);
711 void enternotify(xcb_enter_notify_event_t
*);
712 void event_drain(uint8_t);
713 void event_error(xcb_generic_error_t
*);
714 void event_handle(xcb_generic_event_t
*);
715 char *expand_tilde(char *);
716 void expose(xcb_expose_event_t
*);
717 struct ws_win
*find_window(xcb_window_t
);
718 int floating_toggle_win(struct ws_win
*);
719 void focus(struct swm_region
*, union arg
*);
720 void focus_flush(void);
721 struct ws_win
*get_focus_magic(struct ws_win
*);
722 struct ws_win
*get_focus_prev(struct ws_win
*);
723 void focus_win(struct ws_win
*);
725 void focusin(xcb_focus_in_event_t
*);
727 xcb_atom_t
get_atom_from_string(const char *);
729 char *get_atom_name(xcb_atom_t
);
730 char *get_notify_detail_label(uint8_t);
731 char *get_notify_mode_label(uint8_t);
733 struct ws_win
*get_pointer_win(xcb_window_t
);
734 struct ws_win
*get_region_focus(struct swm_region
*);
735 xcb_screen_t
*get_screen(int);
736 char *get_win_name(xcb_window_t
);
737 uint32_t getstate(xcb_window_t
);
738 void grabbuttons(struct ws_win
*);
739 void keypress(xcb_key_press_event_t
*);
741 void leavenotify(xcb_leave_notify_event_t
*);
743 void load_float_geom(struct ws_win
*, struct swm_region
*);
744 struct ws_win
*manage_window(xcb_window_t
, uint16_t);
745 void map_window(struct ws_win
*);
746 void mapnotify(xcb_map_notify_event_t
*);
747 void mappingnotify(xcb_mapping_notify_event_t
*);
748 void maprequest(xcb_map_request_event_t
*);
749 void new_region(struct swm_screen
*, int, int, int, int);
750 int parse_rgb(const char *, uint16_t *, uint16_t *, uint16_t *);
751 void propertynotify(xcb_property_notify_event_t
*);
752 void spawn_select(struct swm_region
*, union arg
*, char *, int *);
753 void screenchange(xcb_randr_screen_change_notify_event_t
*);
754 void shutdown_cleanup(void);
755 void store_float_geom(struct ws_win
*, struct swm_region
*);
756 void unmanage_window(struct ws_win
*);
757 void unmapnotify(xcb_unmap_notify_event_t
*);
758 void unfocus_win(struct ws_win
*);
759 void update_window(struct ws_win
*);
760 /*void visibilitynotify(xcb_visibility_notify_event_t *);*/
765 xcb_font_t cf
= XCB_NONE
;
768 for (i
= 0; i
< LENGTH(cursors
); ++i
) {
769 /* try to load Xcursor first. */
770 cursors
[i
].cid
= XcursorLibraryLoadCursor(display
,
773 /* fallback to cursorfont. */
774 if (cursors
[i
].cid
== XCB_CURSOR_NONE
) {
775 if (cf
== XCB_NONE
) {
776 cf
= xcb_generate_id(conn
);
777 xcb_open_font(conn
, cf
, strlen("cursor"),
781 cursors
[i
].cid
= xcb_generate_id(conn
);
782 xcb_create_glyph_cursor(conn
, cursors
[i
].cid
, cf
, cf
,
783 cursors
[i
].cf_char
, cursors
[i
].cf_char
+ 1, 0, 0, 0,
784 0xffff, 0xffff, 0xffff);
790 xcb_close_font(conn
, cf
);
794 cursors_cleanup(void)
797 for (i
= 0; i
< LENGTH(cursors
); ++i
)
798 xcb_free_cursor(conn
, cursors
[i
].cid
);
802 expand_tilde(char *s
)
811 errx(1, "expand_tilde: NULL string.");
820 if ((max
= sysconf(_SC_LOGIN_NAME_MAX
)) == -1)
821 errx(1, "expand_tilde: sysconf");
823 if ((user
= calloc(1, max
+ 1)) == NULL
)
824 errx(1, "expand_tilde: calloc");
826 for (i
= 0; s
[i
] != '/' && s
[i
] != '\0'; ++i
)
831 ppwd
= strlen(user
) == 0 ? getpwuid(getuid()) : getpwnam(user
);
837 if (asprintf(&result
, "%s%s", ppwd
->pw_dir
, s
) == -1)
841 errx(1, "expand_tilde: failed to allocate memory.");
847 parse_rgb(const char *rgb
, uint16_t *rr
, uint16_t *gg
, uint16_t *bb
)
849 unsigned int tmpr
, tmpg
, tmpb
;
851 if (sscanf(rgb
, "rgb:%x/%x/%x", &tmpr
, &tmpg
, &tmpb
) != 3)
854 *rr
= RGB_8_TO_16(tmpr
);
855 *gg
= RGB_8_TO_16(tmpg
);
856 *bb
= RGB_8_TO_16(tmpb
);
862 get_screen(int screen
)
864 const xcb_setup_t
*r
;
865 xcb_screen_iterator_t iter
;
867 if ((r
= xcb_get_setup(conn
)) == NULL
) {
868 DNPRINTF(SWM_D_MISC
, "get_screen: xcb_get_setup\n");
872 iter
= xcb_setup_roots_iterator(r
);
873 for (; iter
.rem
; --screen
, xcb_screen_next(&iter
))
883 if (focus_mode
== SWM_FOCUS_DEFAULT
)
884 event_drain(XCB_ENTER_NOTIFY
);
890 get_atom_from_string(const char *str
)
892 xcb_intern_atom_cookie_t c
;
893 xcb_intern_atom_reply_t
*r
;
896 c
= xcb_intern_atom(conn
, 0, strlen(str
), str
);
897 r
= xcb_intern_atom_reply(conn
, c
, NULL
);
905 return (XCB_ATOM_NONE
);
909 set_swm_iconic(struct ws_win
*win
, int newv
)
916 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
917 a_swm_iconic
, XCB_ATOM_INTEGER
, 32, 1, &v
);
919 xcb_delete_property(conn
, win
->id
, a_swm_iconic
);
923 get_swm_iconic(struct ws_win
*win
)
926 xcb_get_property_reply_t
*pr
= NULL
;
928 pr
= xcb_get_property_reply(conn
,
929 xcb_get_property(conn
, 0, win
->id
, a_swm_iconic
,
930 XCB_ATOM_INTEGER
, 0, 1), NULL
);
933 if (pr
->type
!= XCB_ATOM_INTEGER
|| pr
->format
!= 32)
935 v
= *((int32_t *)xcb_get_property_value(pr
));
946 int i
, j
, num_screens
;
948 sup_list
= get_atom_from_string("_NET_SUPPORTED");
950 for (i
= 0; i
< LENGTH(ewmh
); i
++)
951 ewmh
[i
].atom
= get_atom_from_string(ewmh
[i
].name
);
953 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
954 for (i
= 0; i
< num_screens
; i
++) {
955 /* Support check window will be created by workaround(). */
957 /* Report supported atoms */
958 xcb_delete_property(conn
, screens
[i
].root
, sup_list
);
959 for (j
= 0; j
< LENGTH(ewmh
); j
++)
960 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
,
961 screens
[i
].root
, sup_list
, XCB_ATOM_ATOM
, 32, 1,
970 xcb_atom_t sup_check
, sup_list
;
972 xcb_get_property_cookie_t pc
;
973 xcb_get_property_reply_t
*pr
;
975 sup_check
= get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
976 sup_list
= get_atom_from_string("_NET_SUPPORTED");
977 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
979 for (i
= 0; i
< num_screens
; i
++) {
980 /* Get the support check window and destroy it */
981 pc
= xcb_get_property(conn
, 0, screens
[i
].root
, sup_check
,
982 XCB_ATOM_WINDOW
, 0, 1);
983 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
986 if (pr
->format
== sup_check
) {
987 id
= *((xcb_window_t
*)xcb_get_property_value(pr
));
989 xcb_destroy_window(conn
, id
);
990 xcb_delete_property(conn
, screens
[i
].root
, sup_check
);
991 xcb_delete_property(conn
, screens
[i
].root
, sup_list
);
998 ewmh_autoquirk(struct ws_win
*win
)
1002 xcb_get_property_cookie_t c
;
1003 xcb_get_property_reply_t
*r
;
1005 c
= xcb_get_property(conn
, 0, win
->id
,
1006 ewmh
[_NET_WM_WINDOW_TYPE
].atom
, XCB_ATOM_ATOM
, 0, UINT32_MAX
);
1007 r
= xcb_get_property_reply(conn
, c
, NULL
);
1011 n
= xcb_get_property_value_length(r
);
1012 type
= xcb_get_property_value(r
);
1014 for (i
= 0; i
< n
; i
++) {
1015 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_NORMAL
].atom
)
1017 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_DOCK
].atom
||
1018 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_TOOLBAR
].atom
||
1019 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_UTILITY
].atom
) {
1021 win
->quirks
= SWM_Q_FLOAT
| SWM_Q_ANYWHERE
;
1024 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_SPLASH
].atom
||
1025 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_DIALOG
].atom
) {
1027 win
->quirks
= SWM_Q_FLOAT
;
1034 #define SWM_EWMH_ACTION_COUNT_MAX (6)
1035 #define EWMH_F_FULLSCREEN (1<<0)
1036 #define EWMH_F_ABOVE (1<<1)
1037 #define EWMH_F_HIDDEN (1<<2)
1038 #define EWMH_F_SKIP_PAGER (1<<3)
1039 #define EWMH_F_SKIP_TASKBAR (1<<4)
1040 #define SWM_F_MANUAL (1<<5)
1043 ewmh_set_win_fullscreen(struct ws_win
*win
, int fs
)
1051 DNPRINTF(SWM_D_MISC
, "ewmh_set_win_fullscreen: window: 0x%x, "
1052 "fullscreen %s\n", win
->id
, YESNO(fs
));
1055 if (!win
->g_floatvalid
)
1056 store_float_geom(win
, win
->ws
->r
);
1058 win
->g
= win
->ws
->r
->g
;
1061 load_float_geom(win
, win
->ws
->r
);
1068 ewmh_update_actions(struct ws_win
*win
)
1070 xcb_atom_t actions
[SWM_EWMH_ACTION_COUNT_MAX
];
1076 actions
[n
++] = ewmh
[_NET_WM_ACTION_CLOSE
].atom
;
1078 if (win
->floating
) {
1079 actions
[n
++] = ewmh
[_NET_WM_ACTION_MOVE
].atom
;
1080 actions
[n
++] = ewmh
[_NET_WM_ACTION_RESIZE
].atom
;
1081 actions
[n
++] = ewmh
[_NET_WM_ACTION_ABOVE
].atom
;
1084 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
1085 ewmh
[_NET_WM_ALLOWED_ACTIONS
].atom
, XCB_ATOM_ATOM
, 32, 1, actions
);
1088 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
1089 #define _NET_WM_STATE_ADD 1 /* add/set property */
1090 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
1093 ewmh_update_win_state(struct ws_win
*win
, xcb_atom_t state
, long action
)
1095 unsigned int mask
= 0;
1096 unsigned int changed
= 0;
1097 unsigned int orig_flags
;
1102 DNPRINTF(SWM_D_PROP
, "ewmh_update_win_state: window: 0x%x, state: %ld, "
1103 "action: %ld\n", win
->id
, (unsigned long)state
, action
);
1105 if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
)
1106 mask
= EWMH_F_FULLSCREEN
;
1107 else if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
)
1108 mask
= EWMH_F_ABOVE
;
1109 else if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
)
1110 mask
= SWM_F_MANUAL
;
1111 else if (state
== ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
)
1112 mask
= EWMH_F_SKIP_PAGER
;
1113 else if (state
== ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
)
1114 mask
= EWMH_F_SKIP_TASKBAR
;
1116 orig_flags
= win
->ewmh_flags
;
1119 case _NET_WM_STATE_REMOVE
:
1120 win
->ewmh_flags
&= ~mask
;
1122 case _NET_WM_STATE_ADD
:
1123 win
->ewmh_flags
|= mask
;
1125 case _NET_WM_STATE_TOGGLE
:
1126 win
->ewmh_flags
^= mask
;
1130 changed
= (win
->ewmh_flags
& mask
) ^ (orig_flags
& mask
) ? 1 : 0;
1132 if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
) {
1133 if (changed
&& !floating_toggle_win(win
))
1134 win
->ewmh_flags
= orig_flags
; /* revert */
1135 } else if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
) {
1137 win
->manual
= (win
->ewmh_flags
& SWM_F_MANUAL
) != 0;
1138 } else if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
) {
1139 if (changed
&& !ewmh_set_win_fullscreen(win
,
1140 win
->ewmh_flags
& EWMH_F_FULLSCREEN
))
1141 win
->ewmh_flags
= orig_flags
; /* revert */
1144 xcb_delete_property(conn
, win
->id
, ewmh
[_NET_WM_STATE
].atom
);
1146 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
1147 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1148 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1149 &ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
);
1150 else if (win
->ewmh_flags
& EWMH_F_SKIP_PAGER
)
1151 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1152 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1153 &ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
);
1154 else if (win
->ewmh_flags
& EWMH_F_SKIP_TASKBAR
)
1155 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1156 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1157 &ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
);
1158 else if (win
->ewmh_flags
& EWMH_F_ABOVE
)
1159 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1160 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1161 &ewmh
[_NET_WM_STATE_ABOVE
].atom
);
1162 else if (win
->ewmh_flags
& SWM_F_MANUAL
)
1163 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1164 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1165 &ewmh
[_SWM_WM_STATE_MANUAL
].atom
);
1169 ewmh_get_win_state(struct ws_win
*win
)
1172 xcb_get_property_cookie_t c
;
1173 xcb_get_property_reply_t
*r
;
1179 win
->ewmh_flags
= 0;
1181 win
->ewmh_flags
|= EWMH_F_ABOVE
;
1183 win
->ewmh_flags
|= SWM_F_MANUAL
;
1185 c
= xcb_get_property(conn
, 0, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
1186 XCB_ATOM_ATOM
, 0, UINT32_MAX
);
1187 r
= xcb_get_property_reply(conn
, c
, NULL
);
1191 states
= xcb_get_property_value(r
);
1192 n
= xcb_get_property_value_length(r
);
1194 for (i
= 0; i
< n
; i
++)
1195 ewmh_update_win_state(win
, states
[i
], _NET_WM_STATE_ADD
);
1203 dumpwins(struct swm_region
*r
, union arg
*args
)
1207 xcb_get_window_attributes_cookie_t c
;
1208 xcb_get_window_attributes_reply_t
*wa
;
1210 /* suppress unused warning since var is needed */
1213 if (r
->ws
== NULL
) {
1214 warnx("dumpwins: invalid workspace");
1218 warnx("=== managed window list ws %02d ===", r
->ws
->idx
);
1219 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
1220 state
= getstate(win
->id
);
1221 c
= xcb_get_window_attributes(conn
, win
->id
);
1222 wa
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
1224 warnx("window: 0x%x, map_state: %d, state: %u, "
1225 "transient: 0x%x", win
->id
, wa
->map_state
,
1226 state
, win
->transient
);
1229 warnx("window: 0x%x, failed xcb_get_window_attributes",
1233 warnx("===== unmanaged window list =====");
1234 TAILQ_FOREACH(win
, &r
->ws
->unmanagedlist
, entry
) {
1235 state
= getstate(win
->id
);
1236 c
= xcb_get_window_attributes(conn
, win
->id
);
1237 wa
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
1239 warnx("window: 0x%x, map_state: %d, state: %u, "
1240 "transient: 0x%x", win
->id
, wa
->map_state
,
1241 state
, win
->transient
);
1244 warnx("window: 0x%x, failed xcb_get_window_attributes",
1248 warnx("=================================");
1252 dumpwins(struct swm_region
*r
, union arg
*s
)
1257 #endif /* SWM_DEBUG */
1262 int saved_errno
, status
;
1265 saved_errno
= errno
;
1269 while ((pid
= waitpid(WAIT_ANY
, &status
, WNOHANG
)) != 0) {
1274 if (errno
!= ECHILD
)
1275 warn("sighdlr: waitpid");
1276 #endif /* SWM_DEBUG */
1279 if (pid
== searchpid
)
1283 if (WIFEXITED(status
)) {
1284 if (WEXITSTATUS(status
) != 0)
1285 warnx("sighdlr: child exit status: %d",
1286 WEXITSTATUS(status
));
1288 warnx("sighdlr: child is terminated "
1290 #endif /* SWM_DEBUG */
1304 errno
= saved_errno
;
1310 struct pid_e
*p
= NULL
;
1312 DNPRINTF(SWM_D_MISC
, "find_pid: %d\n", pid
);
1317 TAILQ_FOREACH(p
, &pidlist
, entry
) {
1326 name_to_pixel(const char *colorname
)
1328 uint32_t result
= 0;
1329 char cname
[32] = "#";
1330 xcb_screen_t
*screen
;
1331 xcb_colormap_t cmap
;
1332 xcb_alloc_color_reply_t
*cr
;
1333 xcb_alloc_named_color_reply_t
*nr
;
1334 uint16_t rr
, gg
, bb
;
1336 screen
= xcb_setup_roots_iterator(xcb_get_setup(conn
)).data
;
1337 cmap
= screen
->default_colormap
;
1339 /* color is in format rgb://rr/gg/bb */
1340 if (strncmp(colorname
, "rgb:", 4) == 0) {
1341 if (parse_rgb(colorname
, &rr
, &gg
, &bb
) == -1)
1342 warnx("could not parse rgb %s", colorname
);
1344 cr
= xcb_alloc_color_reply(conn
,
1345 xcb_alloc_color(conn
, cmap
, rr
, gg
, bb
),
1351 warnx("color '%s' not found", colorname
);
1354 nr
= xcb_alloc_named_color_reply(conn
,
1355 xcb_alloc_named_color(conn
, cmap
, strlen(colorname
),
1358 strlcat(cname
, colorname
+ 2, sizeof cname
- 1);
1359 nr
= xcb_alloc_named_color_reply(conn
,
1360 xcb_alloc_named_color(conn
, cmap
, strlen(cname
),
1367 warnx("color '%s' not found", colorname
);
1374 setscreencolor(char *val
, int i
, int c
)
1378 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1379 if (i
> 0 && i
<= num_screens
) {
1380 screens
[i
- 1].c
[c
].pixel
= name_to_pixel(val
);
1381 free(screens
[i
- 1].c
[c
].name
);
1382 if ((screens
[i
- 1].c
[c
].name
= strdup(val
)) == NULL
)
1384 } else if (i
== -1) {
1385 for (i
= 0; i
< num_screens
; i
++) {
1386 screens
[i
].c
[c
].pixel
= name_to_pixel(val
);
1387 free(screens
[i
].c
[c
].name
);
1388 if ((screens
[i
].c
[c
].name
= strdup(val
)) == NULL
)
1392 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1397 fancy_stacker(struct workspace
*ws
)
1399 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1400 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1401 snprintf(ws
->stacker
, sizeof ws
->stacker
,
1402 ws
->l_state
.vertical_flip
? "[%d>%d]" : "[%d|%d]",
1403 ws
->l_state
.vertical_mwin
, ws
->l_state
.vertical_stacks
);
1404 if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1405 snprintf(ws
->stacker
, sizeof ws
->stacker
,
1406 ws
->l_state
.horizontal_flip
? "[%dv%d]" : "[%d-%d]",
1407 ws
->l_state
.horizontal_mwin
, ws
->l_state
.horizontal_stacks
);
1411 plain_stacker(struct workspace
*ws
)
1413 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1414 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1415 strlcpy(ws
->stacker
, ws
->l_state
.vertical_flip
? "[>]" : "[|]",
1416 sizeof ws
->stacker
);
1417 if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1418 strlcpy(ws
->stacker
, ws
->l_state
.horizontal_flip
? "[v]" : "[-]",
1419 sizeof ws
->stacker
);
1423 custom_region(char *val
)
1425 unsigned int x
, y
, w
, h
;
1426 int sidx
, num_screens
;
1427 xcb_screen_t
*screen
;
1429 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1430 if (sscanf(val
, "screen[%d]:%ux%u+%u+%u", &sidx
, &w
, &h
, &x
, &y
) != 5)
1431 errx(1, "invalid custom region, "
1432 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1433 if (sidx
< 1 || sidx
> num_screens
)
1434 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1438 if ((screen
= get_screen(sidx
)) == NULL
)
1439 errx(1, "ERROR: can't get screen %d.", sidx
);
1442 errx(1, "region %ux%u+%u+%u too small", w
, h
, x
, y
);
1444 if (x
> screen
->width_in_pixels
||
1445 y
> screen
->height_in_pixels
||
1446 w
+ x
> screen
->width_in_pixels
||
1447 h
+ y
> screen
->height_in_pixels
) {
1448 warnx("ignoring region %ux%u+%u+%u - not within screen "
1449 "boundaries (%ux%u)", w
, h
, x
, y
,
1450 screen
->width_in_pixels
, screen
->height_in_pixels
);
1454 new_region(&screens
[sidx
], x
, y
, w
, h
);
1458 socket_setnonblock(int fd
)
1462 if ((flags
= fcntl(fd
, F_GETFL
, 0)) == -1)
1463 err(1, "fcntl F_GETFL");
1464 flags
|= O_NONBLOCK
;
1465 if ((flags
= fcntl(fd
, F_SETFL
, flags
)) == -1)
1466 err(1, "fcntl F_SETFL");
1470 bar_print_legacy(struct swm_region
*r
, const char *s
)
1472 xcb_rectangle_t rect
;
1477 XRectangle ibox
, lbox
;
1481 XmbTextExtents(bar_fs
, s
, len
, &ibox
, &lbox
);
1483 switch (bar_justify
) {
1484 case SWM_BAR_JUSTIFY_LEFT
:
1487 case SWM_BAR_JUSTIFY_CENTER
:
1488 x
= (WIDTH(r
) - lbox
.width
) / 2;
1490 case SWM_BAR_JUSTIFY_RIGHT
:
1491 x
= WIDTH(r
) - lbox
.width
- SWM_BAR_OFFSET
;
1495 if (x
< SWM_BAR_OFFSET
)
1500 rect
.width
= WIDTH(r
->bar
);
1501 rect
.height
= HEIGHT(r
->bar
);
1503 /* clear back buffer */
1504 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
1505 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
1506 xcb_poly_fill_rectangle(conn
, r
->bar
->buffer
, r
->s
->bar_gc
, 1, &rect
);
1508 /* draw back buffer */
1509 gcvd
.graphics_exposures
= 0;
1510 draw
= XCreateGC(display
, r
->bar
->buffer
, GCGraphicsExposures
, &gcvd
);
1511 XSetForeground(display
, draw
, r
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
);
1512 DRAWSTRING(display
, r
->bar
->buffer
, bar_fs
, draw
,
1513 x
, (bar_fs_extents
->max_logical_extent
.height
- lbox
.height
) / 2 -
1515 XFreeGC(display
, draw
);
1518 xcb_copy_area(conn
, r
->bar
->buffer
, r
->bar
->id
, r
->s
->bar_gc
, 0, 0,
1519 0, 0, WIDTH(r
->bar
), HEIGHT(r
->bar
));
1523 bar_print(struct swm_region
*r
, const char *s
)
1526 xcb_rectangle_t rect
;
1534 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
, &info
);
1536 switch (bar_justify
) {
1537 case SWM_BAR_JUSTIFY_LEFT
:
1540 case SWM_BAR_JUSTIFY_CENTER
:
1541 x
= (WIDTH(r
) - info
.width
) / 2;
1543 case SWM_BAR_JUSTIFY_RIGHT
:
1544 x
= WIDTH(r
) - info
.width
- SWM_BAR_OFFSET
;
1548 if (x
< SWM_BAR_OFFSET
)
1553 rect
.width
= WIDTH(r
->bar
);
1554 rect
.height
= HEIGHT(r
->bar
);
1556 /* clear back buffer */
1557 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
1558 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
1559 xcb_poly_fill_rectangle(conn
, r
->bar
->buffer
, r
->s
->bar_gc
, 1, &rect
);
1561 /* draw back buffer */
1562 draw
= XftDrawCreate(display
, r
->bar
->buffer
,
1563 DefaultVisual(display
, r
->s
->idx
),
1564 DefaultColormap(display
, r
->s
->idx
));
1566 XftDrawStringUtf8(draw
, &bar_font_color
, bar_font
, x
,
1567 (HEIGHT(r
->bar
) + bar_font
->height
) / 2 - bar_font
->descent
,
1570 XftDrawDestroy(draw
);
1573 xcb_copy_area(conn
, r
->bar
->buffer
, r
->bar
->id
, r
->s
->bar_gc
, 0, 0,
1574 0, 0, WIDTH(r
->bar
), HEIGHT(r
->bar
));
1578 bar_extra_stop(void)
1582 bzero(bar_pipe
, sizeof bar_pipe
);
1585 kill(bar_pid
, SIGTERM
);
1588 strlcpy((char *)bar_ext
, "", sizeof bar_ext
);
1593 bar_class_name(char *s
, size_t sz
, struct swm_region
*r
)
1595 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1597 if (r
->ws
->focus
->ch
.class_name
!= NULL
)
1598 strlcat(s
, r
->ws
->focus
->ch
.class_name
, sz
);
1602 bar_title_name(char *s
, size_t sz
, struct swm_region
*r
)
1604 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1606 if (r
->ws
->focus
->ch
.instance_name
!= NULL
)
1607 strlcat(s
, r
->ws
->focus
->ch
.instance_name
, sz
);
1611 bar_class_title_name(char *s
, size_t sz
, struct swm_region
*r
)
1613 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1616 bar_class_name(s
, sz
, r
);
1617 strlcat(s
, ":", sz
);
1618 bar_title_name(s
, sz
, r
);
1622 bar_window_float(char *s
, size_t sz
, struct swm_region
*r
)
1624 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1626 if (r
->ws
->focus
->floating
)
1627 strlcat(s
, "(f)", sz
);
1631 bar_window_name(char *s
, size_t sz
, struct swm_region
*r
)
1635 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1637 if ((title
= get_win_name(r
->ws
->focus
->id
)) == NULL
)
1640 strlcat(s
, title
, sz
);
1644 int urgent
[SWM_WS_MAX
];
1646 bar_urgent(char *s
, size_t sz
)
1649 int i
, j
, num_screens
;
1651 xcb_get_property_cookie_t c
;
1652 xcb_icccm_wm_hints_t hints
;
1654 for (i
= 0; i
< workspace_limit
; i
++)
1657 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1658 for (i
= 0; i
< num_screens
; i
++)
1659 for (j
= 0; j
< workspace_limit
; j
++)
1660 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
) {
1661 c
= xcb_icccm_get_wm_hints(conn
, win
->id
);
1662 if (xcb_icccm_get_wm_hints_reply(conn
, c
,
1665 if (hints
.flags
& XCB_ICCCM_WM_HINT_X_URGENCY
)
1669 for (i
= 0; i
< workspace_limit
; i
++) {
1671 snprintf(b
, sizeof b
, "%d ", i
+ 1);
1673 snprintf(b
, sizeof b
, "- ");
1679 bar_workspace_name(char *s
, size_t sz
, struct swm_region
*r
)
1681 if (r
== NULL
|| r
->ws
== NULL
)
1683 if (r
->ws
->name
!= NULL
)
1684 strlcat(s
, r
->ws
->name
, sz
);
1687 /* build the default bar format according to the defined enabled options */
1689 bar_fmt(const char *fmtexp
, char *fmtnew
, struct swm_region
*r
, size_t sz
)
1691 /* if format provided, just copy the buffers */
1692 if (bar_format
!= NULL
) {
1693 strlcpy(fmtnew
, fmtexp
, sz
);
1697 /* reset the output buffer */
1700 strlcat(fmtnew
, "+N:+I ", sz
);
1702 strlcat(fmtnew
, "+S", sz
);
1703 strlcat(fmtnew
, " ", sz
);
1705 /* only show the workspace name if there's actually one */
1706 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->name
!= NULL
)
1707 strlcat(fmtnew
, "<+D>", sz
);
1708 strlcat(fmtnew
, "+3<", sz
);
1710 if (clock_enabled
) {
1711 strlcat(fmtnew
, fmtexp
, sz
);
1712 strlcat(fmtnew
, "+4<", sz
);
1715 /* bar_urgent already adds the space before the last asterisk */
1717 strlcat(fmtnew
, "* +U*+4<", sz
);
1719 if (title_class_enabled
) {
1720 strlcat(fmtnew
, "+C", sz
);
1721 if (!title_name_enabled
)
1722 strlcat(fmtnew
, "+4<", sz
);
1725 /* checks needed by the colon and floating strlcat(3) calls below */
1726 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->focus
!= NULL
) {
1727 if (title_name_enabled
) {
1728 if (title_class_enabled
)
1729 strlcat(fmtnew
, ":", sz
);
1730 strlcat(fmtnew
, "+T+4<", sz
);
1732 if (window_name_enabled
) {
1733 if (r
->ws
->focus
->floating
)
1734 strlcat(fmtnew
, "+F ", sz
);
1735 strlcat(fmtnew
, "+64W ", sz
);
1739 /* finally add the action script output and the version */
1740 strlcat(fmtnew
, "+4<+A+4<+V", sz
);
1744 bar_replace_pad(char *tmp
, int *limit
, size_t sz
)
1746 /* special case; no limit given, pad one space, instead */
1747 if (*limit
== (int)sz
- 1)
1749 snprintf(tmp
, sz
, "%*s", *limit
, " ");
1752 /* replaces the bar format character sequences (like in tmux(1)) */
1754 bar_replace_seq(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t *offrep
,
1758 char tmp
[SWM_BAR_MAX
];
1762 /* reset strlcat(3) buffer */
1765 /* get number, if any */
1768 if (sscanf(fmt
, "%d%n", &limit
, &size
) != 1)
1769 limit
= sizeof tmp
- 1;
1770 if (limit
<= 0 || limit
>= (int)sizeof tmp
)
1771 limit
= sizeof tmp
- 1;
1773 /* there is nothing to replace (ie EOL) */
1780 bar_replace_pad(tmp
, &limit
, sizeof tmp
);
1783 snprintf(tmp
, sizeof tmp
, "%s", bar_ext
);
1786 bar_class_name(tmp
, sizeof tmp
, r
);
1789 bar_workspace_name(tmp
, sizeof tmp
, r
);
1792 bar_window_float(tmp
, sizeof tmp
, r
);
1795 snprintf(tmp
, sizeof tmp
, "%d", r
->ws
->idx
+ 1);
1798 snprintf(tmp
, sizeof tmp
, "%d", r
->s
->idx
+ 1);
1801 bar_class_title_name(tmp
, sizeof tmp
, r
);
1804 snprintf(tmp
, sizeof tmp
, "%s", r
->ws
->stacker
);
1807 bar_title_name(tmp
, sizeof tmp
, r
);
1810 bar_urgent(tmp
, sizeof tmp
);
1813 snprintf(tmp
, sizeof tmp
, "%s", bar_vertext
);
1816 bar_window_name(tmp
, sizeof tmp
, r
);
1819 /* unknown character sequence; copy as-is */
1820 snprintf(tmp
, sizeof tmp
, "+%c", *fmt
);
1826 if ((int)len
< limit
)
1828 while (limit
-- > 0) {
1829 if (*offrep
>= sz
- 1)
1831 fmtrep
[(*offrep
)++] = *ptr
++;
1839 bar_replace(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t sz
)
1844 while (*fmt
!= '\0') {
1846 /* skip ordinary characters */
1849 fmtrep
[off
++] = *fmt
++;
1853 /* character sequence found; replace it */
1854 fmt
= bar_replace_seq(fmt
, fmtrep
, r
, &off
, sz
);
1863 bar_fmt_expand(char *fmtexp
, size_t sz
)
1870 /* start by grabbing the current time and date */
1872 localtime_r(&tmt
, &tm
);
1874 /* figure out what to expand */
1875 if (bar_format
!= NULL
)
1877 else if (bar_format
== NULL
&& clock_enabled
)
1879 /* if nothing to expand bail out */
1885 /* copy as-is, just in case the format shouldn't be expanded below */
1886 strlcpy(fmtexp
, fmt
, sz
);
1887 /* finally pass the string through strftime(3) */
1888 #ifndef SWM_DENY_CLOCK_FORMAT
1889 if ((len
= strftime(fmtexp
, sz
, fmt
, &tm
)) == 0)
1890 warnx("format too long");
1898 char fmtexp
[SWM_BAR_MAX
], fmtnew
[SWM_BAR_MAX
];
1899 char fmtrep
[SWM_BAR_MAX
];
1901 struct swm_region
*r
;
1903 /* expand the format by first passing it through strftime(3) */
1904 bar_fmt_expand(fmtexp
, sizeof fmtexp
);
1906 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1907 for (i
= 0; i
< num_screens
; i
++) {
1908 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
1911 bar_fmt(fmtexp
, fmtnew
, r
, sizeof fmtnew
);
1912 bar_replace(fmtnew
, fmtrep
, r
, sizeof fmtrep
);
1913 if (bar_font_legacy
)
1914 bar_print_legacy(r
, fmtrep
);
1916 bar_print(r
, fmtrep
);
1929 if (bar_extra
&& bar_extra_running
) {
1930 /* ignore short reads; it'll correct itself */
1931 while ((b
= fgetln(stdin
, &len
)) != NULL
)
1932 if (b
&& b
[len
- 1] == '\n') {
1934 strlcpy((char *)bar_ext
, b
, sizeof bar_ext
);
1936 if (b
== NULL
&& errno
!= EAGAIN
) {
1937 warn("bar_update: bar_extra failed");
1941 strlcpy((char *)bar_ext
, "", sizeof bar_ext
);
1950 /* suppress unused warning since var is needed */
1957 bar_toggle(struct swm_region
*r
, union arg
*args
)
1959 struct swm_region
*tmpr
;
1962 /* suppress unused warnings since vars are needed */
1966 DNPRINTF(SWM_D_BAR
, "bar_toggle\n");
1968 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1970 for (i
= 0; i
< num_screens
; i
++)
1971 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
1973 xcb_unmap_window(conn
, tmpr
->bar
->id
);
1975 for (i
= 0; i
< num_screens
; i
++)
1976 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
1978 xcb_map_window(conn
, tmpr
->bar
->id
);
1981 bar_enabled
= !bar_enabled
;
1985 /* must be after stack */
1994 struct swm_region
*r
;
1998 /* do this here because the conf file is in memory */
1999 if (bar_extra
&& !bar_extra_running
&& bar_argv
[0]) {
2000 /* launch external status app */
2001 bar_extra_running
= 1;
2002 if (pipe(bar_pipe
) == -1)
2003 err(1, "pipe error");
2004 socket_setnonblock(bar_pipe
[0]);
2005 socket_setnonblock(bar_pipe
[1]); /* XXX hmmm, really? */
2006 if (dup2(bar_pipe
[0], 0) == -1)
2008 if (dup2(bar_pipe
[1], 1) == -1)
2010 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
2011 err(1, "could not disable SIGPIPE");
2012 switch (bar_pid
= fork()) {
2014 err(1, "cannot fork");
2018 execvp(bar_argv
[0], bar_argv
);
2019 err(1, "%s external app failed", bar_argv
[0]);
2021 default: /* parent */
2027 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2028 for (i
= 0; i
< num_screens
; i
++)
2029 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
2032 wa
[0] = screens
[i
].c
[SWM_S_COLOR_BAR
].pixel
;
2033 wa
[1] = screens
[i
].c
[SWM_S_COLOR_BAR_BORDER
].pixel
;
2034 xcb_change_window_attributes(conn
, r
->bar
->id
,
2035 XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
, wa
);
2045 while ((s
= index(s
, '-'))) {
2050 return (count
== 14);
2056 char *default_string
;
2057 char **missing_charsets
;
2058 int num_missing_charsets
= 0;
2062 XFreeFontSet(display
, bar_fs
);
2066 DNPRINTF(SWM_D_INIT
, "fontset_init: loading bar_fonts: %s\n", bar_fonts
);
2068 bar_fs
= XCreateFontSet(display
, bar_fonts
, &missing_charsets
,
2069 &num_missing_charsets
, &default_string
);
2071 if (num_missing_charsets
> 0) {
2072 warnx("Unable to load charset(s):");
2074 for (i
= 0; i
< num_missing_charsets
; ++i
)
2075 warnx("%s", missing_charsets
[i
]);
2077 XFreeStringList(missing_charsets
);
2079 if (strcmp(default_string
, ""))
2080 warnx("Glyphs from those sets will be replaced "
2081 "by '%s'.", default_string
);
2083 warnx("Glyphs from those sets won't be drawn.");
2087 errx(1, "Error creating font set structure.");
2089 bar_fs_extents
= XExtentsOfFontSet(bar_fs
);
2091 bar_height
= bar_fs_extents
->max_logical_extent
.height
+
2092 2 * bar_border_width
;
2099 xft_init(struct swm_region
*r
)
2101 char *font
, *d
, *search
;
2104 if (bar_font
== NULL
) {
2105 if ((d
= strdup(bar_fonts
)) == NULL
)
2106 errx(1, "insufficient memory.");
2108 while ((font
= strsep(&search
, ",")) != NULL
) {
2112 DNPRINTF(SWM_D_INIT
, "xft_init: try font %s\n", font
);
2115 bar_font
= XftFontOpenXlfd(display
, r
->s
->idx
,
2118 bar_font
= XftFontOpenName(display
, r
->s
->idx
,
2123 warnx("unable to load font %s", font
);
2126 DNPRINTF(SWM_D_INIT
, "successfully opened "
2134 if (bar_font
== NULL
)
2135 errx(1, "unable to open a font");
2137 PIXEL_TO_XRENDERCOLOR(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
, color
);
2139 if (!XftColorAllocValue(display
, DefaultVisual(display
, r
->s
->idx
),
2140 DefaultColormap(display
, r
->s
->idx
), &color
, &bar_font_color
))
2141 warn("unable to allocate Xft color");
2143 bar_height
= bar_font
->height
+ 2 * bar_border_width
;
2150 bar_setup(struct swm_region
*r
)
2152 xcb_screen_t
*screen
;
2155 DNPRINTF(SWM_D_BAR
, "bar_setup: screen %d.\n",
2158 if ((screen
= get_screen(r
->s
->idx
)) == NULL
)
2159 errx(1, "ERROR: can't get screen %d.", r
->s
->idx
);
2164 if ((r
->bar
= calloc(1, sizeof(struct swm_bar
))) == NULL
)
2165 err(1, "bar_setup: calloc: failed to allocate memory.");
2167 if (bar_font_legacy
)
2173 Y(r
->bar
) = bar_at_bottom
? (Y(r
) + HEIGHT(r
) - bar_height
) : Y(r
);
2174 WIDTH(r
->bar
) = WIDTH(r
) - 2 * bar_border_width
;
2175 HEIGHT(r
->bar
) = bar_height
- 2 * bar_border_width
;
2177 r
->bar
->id
= xcb_generate_id(conn
);
2178 wa
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
2179 wa
[1] = r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].pixel
;
2180 wa
[2] = XCB_EVENT_MASK_EXPOSURE
;
2182 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, r
->bar
->id
, r
->s
->root
,
2183 X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
), HEIGHT(r
->bar
),
2184 bar_border_width
, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
2185 XCB_COPY_FROM_PARENT
, XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
2186 | XCB_CW_EVENT_MASK
, wa
);
2188 r
->bar
->buffer
= xcb_generate_id(conn
);
2189 xcb_create_pixmap(conn
, screen
->root_depth
, r
->bar
->buffer
, r
->bar
->id
,
2190 WIDTH(r
->bar
), HEIGHT(r
->bar
));
2193 xcb_randr_select_input(conn
, r
->bar
->id
,
2194 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE
);
2197 xcb_map_window(conn
, r
->bar
->id
);
2199 DNPRINTF(SWM_D_BAR
, "bar_setup: window: 0x%x, (x,y) w x h: (%d,%d) "
2200 "%d x %d\n", WINID(r
->bar
), X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
),
2203 if (signal(SIGALRM
, bar_signal
) == SIG_ERR
)
2204 err(1, "could not install bar_signal");
2209 bar_cleanup(struct swm_region
*r
)
2213 xcb_destroy_window(conn
, r
->bar
->id
);
2214 xcb_free_pixmap(conn
, r
->bar
->buffer
);
2220 set_win_state(struct ws_win
*win
, uint16_t state
)
2222 uint16_t data
[2] = { state
, XCB_ATOM_NONE
};
2224 DNPRINTF(SWM_D_EVENT
, "set_win_state: window: 0x%x, state: %u\n",
2230 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
, a_state
,
2231 a_state
, 32, 2, data
);
2235 getstate(xcb_window_t w
)
2237 uint32_t result
= 0;
2238 xcb_get_property_cookie_t c
;
2239 xcb_get_property_reply_t
*r
;
2241 c
= xcb_get_property(conn
, 0, w
, a_state
, a_state
, 0L, 2L);
2242 r
= xcb_get_property_reply(conn
, c
, NULL
);
2245 if (r
->type
== a_state
&& r
->format
== 32 && r
->length
== 2)
2246 result
= *((uint32_t *)xcb_get_property_value(r
));
2250 DNPRINTF(SWM_D_MISC
, "getstate property: win 0x%x state %u\n", w
,
2256 version(struct swm_region
*r
, union arg
*args
)
2258 /* suppress unused warnings since vars are needed */
2262 bar_version
= !bar_version
;
2264 snprintf(bar_vertext
, sizeof bar_vertext
,
2265 "Version: %s Build: %s", SPECTRWM_VERSION
, buildstr
);
2267 strlcpy(bar_vertext
, "", sizeof bar_vertext
);
2274 client_msg(struct ws_win
*win
, xcb_atom_t a
, xcb_timestamp_t t
)
2276 xcb_client_message_event_t ev
;
2284 name
= get_atom_name(a
);
2285 DNPRINTF(SWM_D_EVENT
, "client_msg: window: 0x%x, atom: %s(%u), "
2287 win
->id
, name
, a
, t
);
2291 bzero(&ev
, sizeof ev
);
2292 ev
.response_type
= XCB_CLIENT_MESSAGE
;
2293 ev
.window
= win
->id
;
2296 ev
.data
.data32
[0] = a
;
2297 ev
.data
.data32
[1] = t
;
2299 xcb_send_event(conn
, 0, win
->id
,
2300 XCB_EVENT_MASK_NO_EVENT
, (const char *)&ev
);
2303 /* synthetic response to a ConfigureRequest when not making a change */
2305 config_win(struct ws_win
*win
, xcb_configure_request_event_t
*ev
)
2307 xcb_configure_notify_event_t ce
;
2312 /* send notification of unchanged state. */
2313 bzero(&ce
, sizeof(ce
));
2314 ce
.response_type
= XCB_CONFIGURE_NOTIFY
;
2317 ce
.width
= WIDTH(win
);
2318 ce
.height
= HEIGHT(win
);
2319 ce
.override_redirect
= 0;
2324 ce
.window
= win
->id
;
2325 ce
.border_width
= BORDER(win
);
2326 ce
.above_sibling
= XCB_WINDOW_NONE
;
2329 ce
.event
= ev
->window
;
2330 ce
.window
= ev
->window
;
2332 /* make response appear more WM_SIZE_HINTS-compliant */
2333 if (win
->sh
.flags
) {
2334 DNPRINTF(SWM_D_MISC
, "config_win: hints: window: 0x%x,"
2335 " sh.flags: %u, min: %d x %d, max: %d x %d, inc: "
2336 "%d x %d\n", win
->id
, win
->sh
.flags
, SH_MIN_W(win
),
2337 SH_MIN_H(win
), SH_MAX_W(win
), SH_MAX_H(win
),
2338 SH_INC_W(win
), SH_INC_H(win
));
2343 /* the hint may be set... to 0! */
2344 if (SH_MIN_W(win
) > 0 && ce
.width
< SH_MIN_W(win
))
2345 ce
.width
= SH_MIN_W(win
);
2346 if (SH_MIN_H(win
) > 0 && ce
.height
< SH_MIN_H(win
))
2347 ce
.height
= SH_MIN_H(win
);
2352 /* may also be advertized as 0 */
2353 if (SH_MAX_W(win
) > 0 && ce
.width
> SH_MAX_W(win
))
2354 ce
.width
= SH_MAX_W(win
);
2355 if (SH_MAX_H(win
) > 0 && ce
.height
> SH_MAX_H(win
))
2356 ce
.height
= SH_MAX_H(win
);
2359 /* resize increment. */
2361 if (SH_INC_W(win
) > 1 && ce
.width
> SH_INC_W(win
))
2362 ce
.width
-= (ce
.width
- SH_MIN_W(win
)) %
2364 if (SH_INC_H(win
) > 1 && ce
.height
> SH_INC_H(win
))
2365 ce
.height
-= (ce
.height
- SH_MIN_H(win
)) %
2369 /* adjust x and y for requested border_width. */
2370 ce
.x
+= BORDER(win
) - ev
->border_width
;
2371 ce
.y
+= BORDER(win
) - ev
->border_width
;
2372 ce
.border_width
= ev
->border_width
;
2373 ce
.above_sibling
= ev
->sibling
;
2376 DNPRINTF(SWM_D_MISC
, "config_win: ewmh: %s, window: 0x%x, (x,y) w x h: "
2377 "(%d,%d) %d x %d, border: %d\n", YESNO(ev
== NULL
), win
->id
, ce
.x
,
2378 ce
.y
, ce
.width
, ce
.height
, ce
.border_width
);
2380 xcb_send_event(conn
, 0, win
->id
, XCB_EVENT_MASK_STRUCTURE_NOTIFY
,
2385 count_win(struct workspace
*ws
, int count_transient
)
2390 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
2391 if (!count_transient
&& win
->floating
)
2393 if (!count_transient
&& win
->transient
)
2399 DNPRINTF(SWM_D_MISC
, "count_win: %d\n", count
);
2405 quit(struct swm_region
*r
, union arg
*args
)
2407 /* suppress unused warnings since vars are needed */
2411 DNPRINTF(SWM_D_MISC
, "quit\n");
2416 map_window(struct ws_win
*win
)
2418 uint32_t val
= XCB_STACK_MODE_ABOVE
;
2423 DNPRINTF(SWM_D_EVENT
, "map_window: win 0x%x, mapped: %s\n", win
->id
,
2424 YESNO(win
->mapped
));
2426 xcb_configure_window(conn
, win
->id
,
2427 XCB_CONFIG_WINDOW_STACK_MODE
, &val
);
2432 xcb_map_window(conn
, win
->id
);
2433 set_win_state(win
, XCB_ICCCM_WM_STATE_NORMAL
);
2438 unmap_window(struct ws_win
*win
)
2443 DNPRINTF(SWM_D_EVENT
, "unmap_window: win 0x%x, mapped: %s\n", win
->id
,
2444 YESNO(win
->mapped
));
2449 xcb_unmap_window(conn
, win
->id
);
2450 set_win_state(win
, XCB_ICCCM_WM_STATE_ICONIC
);
2458 int i
, j
, num_screens
;
2460 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2461 for (i
= 0; i
< num_screens
; i
++)
2462 for (j
= 0; j
< workspace_limit
; j
++)
2463 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2468 fake_keypress(struct ws_win
*win
, xcb_keysym_t keysym
, uint16_t modifiers
)
2470 xcb_key_press_event_t event
;
2471 xcb_keycode_t
*keycode
;
2476 keycode
= xcb_key_symbols_get_keycode(syms
, keysym
);
2478 DNPRINTF(SWM_D_MISC
, "fake_keypress: win 0x%x keycode %u\n",
2481 bzero(&event
, sizeof(event
));
2482 event
.event
= win
->id
;
2483 event
.root
= win
->s
->root
;
2484 event
.child
= XCB_WINDOW_NONE
;
2485 event
.time
= XCB_CURRENT_TIME
;
2486 event
.event_x
= X(win
);
2487 event
.event_y
= Y(win
);
2490 event
.same_screen
= 1;
2491 event
.detail
= *keycode
;
2492 event
.state
= modifiers
;
2494 event
.response_type
= XCB_KEY_PRESS
;
2495 xcb_send_event(conn
, 1, win
->id
,
2496 XCB_EVENT_MASK_KEY_PRESS
, (const char *)&event
);
2498 event
.response_type
= XCB_KEY_RELEASE
;
2499 xcb_send_event(conn
, 1, win
->id
,
2500 XCB_EVENT_MASK_KEY_RELEASE
, (const char *)&event
);
2506 restart(struct swm_region
*r
, union arg
*args
)
2508 /* suppress unused warning since var is needed */
2512 DNPRINTF(SWM_D_MISC
, "restart: %s\n", start_argv
[0]);
2516 execvp(start_argv
[0], start_argv
);
2517 warn("execvp failed");
2522 get_pointer_win(xcb_window_t root
)
2524 struct ws_win
*win
= NULL
;
2525 xcb_query_pointer_reply_t
*r
;
2527 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: root: 0x%x.\n", root
);
2529 r
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, root
), NULL
);
2531 win
= find_window(r
->child
);
2533 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: 0x%x.\n",
2536 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: none.\n");
2544 root_to_region(xcb_window_t root
, int check
)
2547 struct swm_region
*r
= NULL
;
2549 xcb_query_pointer_reply_t
*qpr
;
2550 xcb_get_input_focus_reply_t
*gifr
;
2552 DNPRINTF(SWM_D_MISC
, "root_to_region: window: 0x%x\n", root
);
2554 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2555 for (i
= 0; i
< num_screens
; i
++)
2556 if (screens
[i
].root
== root
)
2559 if (check
& SWM_CK_FOCUS
) {
2560 /* Try to find an actively focused window */
2561 gifr
= xcb_get_input_focus_reply(conn
,
2562 xcb_get_input_focus(conn
), NULL
);
2564 cfw
= find_window(gifr
->focus
);
2565 if (cfw
&& cfw
->ws
->r
)
2572 if (r
== NULL
&& check
& SWM_CK_POINTER
) {
2573 /* No region with an active focus; try to use pointer. */
2574 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
,
2575 screens
[i
].root
), NULL
);
2578 DNPRINTF(SWM_D_MISC
, "root_to_region: pointer: "
2579 "(%d,%d)\n", qpr
->root_x
, qpr
->root_y
);
2580 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2581 if (X(r
) <= qpr
->root_x
&&
2582 qpr
->root_x
< MAX_X(r
) &&
2583 Y(r
) <= qpr
->root_y
&&
2584 qpr
->root_y
< MAX_Y(r
))
2591 if (r
== NULL
&& check
& SWM_CK_FALLBACK
)
2592 r
= TAILQ_FIRST(&screens
[i
].rl
);
2598 find_unmanaged_window(xcb_window_t id
)
2601 int i
, j
, num_screens
;
2603 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2604 for (i
= 0; i
< num_screens
; i
++)
2605 for (j
= 0; j
< workspace_limit
; j
++)
2606 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].unmanagedlist
,
2614 find_window(xcb_window_t id
)
2617 int i
, j
, num_screens
;
2618 xcb_query_tree_reply_t
*r
;
2620 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2621 for (i
= 0; i
< num_screens
; i
++)
2622 for (j
= 0; j
< workspace_limit
; j
++)
2623 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2627 r
= xcb_query_tree_reply(conn
, xcb_query_tree(conn
, id
), NULL
);
2631 /* if we were looking for the parent return that window instead */
2632 if (r
->parent
== 0 || r
->root
== r
->parent
) {
2637 /* look for parent */
2638 for (i
= 0; i
< num_screens
; i
++)
2639 for (j
= 0; j
< workspace_limit
; j
++)
2640 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2641 if (r
->parent
== win
->id
) {
2651 spawn(int ws_idx
, union arg
*args
, int close_fd
)
2656 DNPRINTF(SWM_D_MISC
, "spawn: %s\n", args
->argv
[0]);
2658 close(xcb_get_file_descriptor(conn
));
2660 setenv("LD_PRELOAD", SWM_LIB
, 1);
2662 if (asprintf(&ret
, "%d", ws_idx
) == -1) {
2663 warn("spawn: asprintf SWM_WS");
2666 setenv("_SWM_WS", ret
, 1);
2670 if (asprintf(&ret
, "%d", getpid()) == -1) {
2671 warn("spawn: asprintf _SWM_PID");
2674 setenv("_SWM_PID", ret
, 1);
2678 if (setsid() == -1) {
2679 warn("spawn: setsid");
2685 * close stdin and stdout to prevent interaction between apps
2686 * and the baraction script
2687 * leave stderr open to record errors
2689 if ((fd
= open(_PATH_DEVNULL
, O_RDWR
, 0)) == -1) {
2690 warn("spawn: open");
2693 dup2(fd
, STDIN_FILENO
);
2694 dup2(fd
, STDOUT_FILENO
);
2699 execvp(args
->argv
[0], args
->argv
);
2701 warn("spawn: execvp");
2706 kill_refs(struct ws_win
*win
)
2708 int i
, x
, num_screens
;
2709 struct swm_region
*r
;
2710 struct workspace
*ws
;
2715 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2716 for (i
= 0; i
< num_screens
; i
++)
2717 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2718 for (x
= 0; x
< workspace_limit
; x
++) {
2720 if (win
== ws
->focus
)
2722 if (win
== ws
->focus_prev
)
2723 ws
->focus_prev
= NULL
;
2728 validate_win(struct ws_win
*testwin
)
2731 struct workspace
*ws
;
2732 struct swm_region
*r
;
2733 int i
, x
, num_screens
;
2735 if (testwin
== NULL
)
2738 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2739 for (i
= 0; i
< num_screens
; i
++)
2740 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2741 for (x
= 0; x
< workspace_limit
; x
++) {
2743 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
2751 validate_ws(struct workspace
*testws
)
2753 struct swm_region
*r
;
2754 struct workspace
*ws
;
2755 int i
, x
, num_screens
;
2757 /* validate all ws */
2758 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2759 for (i
= 0; i
< num_screens
; i
++)
2760 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2761 for (x
= 0; x
< workspace_limit
; x
++) {
2770 unfocus_win(struct ws_win
*win
)
2772 xcb_window_t none
= XCB_WINDOW_NONE
;
2774 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: window: 0x%x\n", WINID(win
));
2778 if (win
->ws
== NULL
) {
2779 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: NULL ws.\n");
2783 if (validate_ws(win
->ws
)) {
2784 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: invalid ws.\n");
2788 if (win
->ws
->r
== NULL
) {
2789 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: NULL region.\n");
2793 if (validate_win(win
)) {
2794 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: invalid win.\n");
2799 if (win
->ws
->focus
== win
) {
2800 win
->ws
->focus
= NULL
;
2801 win
->ws
->focus_prev
= win
;
2804 if (validate_win(win
->ws
->focus
)) {
2805 kill_refs(win
->ws
->focus
);
2806 win
->ws
->focus
= NULL
;
2808 if (validate_win(win
->ws
->focus_prev
)) {
2809 kill_refs(win
->ws
->focus_prev
);
2810 win
->ws
->focus_prev
= NULL
;
2813 xcb_change_window_attributes(conn
, win
->id
, XCB_CW_BORDER_PIXEL
,
2814 &win
->ws
->r
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
);
2816 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
2817 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1, &none
);
2819 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: done.\n");
2823 focus_win(struct ws_win
*win
)
2825 struct ws_win
*cfw
= NULL
, *parent
= NULL
, *w
;
2826 struct workspace
*ws
;
2827 xcb_get_input_focus_reply_t
*r
;
2829 DNPRINTF(SWM_D_FOCUS
, "focus_win: window: 0x%x\n", WINID(win
));
2834 if (win
->ws
== NULL
)
2839 if (validate_ws(ws
))
2842 if (validate_win(win
)) {
2847 r
= xcb_get_input_focus_reply(conn
, xcb_get_input_focus(conn
), NULL
);
2849 cfw
= find_window(r
->focus
);
2855 if (ws
->focus
!= win
) {
2856 if (ws
->focus
&& ws
->focus
!= cfw
)
2857 unfocus_win(ws
->focus
);
2861 /* If this window directs focus to a child window, then clear. */
2862 if (win
->focus_child
)
2863 win
->focus_child
= NULL
;
2865 /* If transient, adjust parent's focus child for focus_magic. */
2866 if (win
->transient
) {
2867 parent
= find_window(win
->transient
);
2868 if (parent
&& parent
->focus_child
!= win
)
2869 parent
->focus_child
= win
;
2872 if (cfw
!= win
&& ws
->r
!= NULL
) {
2873 /* Set input focus if no input hint, or indicated by hint. */
2874 if (!(win
->hints
.flags
& XCB_ICCCM_WM_HINT_INPUT
) ||
2875 (win
->hints
.flags
& XCB_ICCCM_WM_HINT_INPUT
&&
2877 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
,
2878 win
->id
, last_event_time
);
2880 /* Tell app it can adjust focus to a specific window. */
2881 if (win
->take_focus
) {
2882 /* java is special; always tell parent */
2883 if (win
->transient
&& win
->java
)
2884 client_msg(parent
, a_takefocus
,
2887 client_msg(win
, a_takefocus
, last_event_time
);
2890 xcb_change_window_attributes(conn
, win
->id
, XCB_CW_BORDER_PIXEL
,
2891 &ws
->r
->s
->c
[SWM_S_COLOR_FOCUS
].pixel
);
2893 if (ws
->cur_layout
->flags
& SWM_L_MAPONFOCUS
||
2895 /* If a parent exists, map it first. */
2899 /* Map siblings next. */
2900 TAILQ_FOREACH(w
, &ws
->winlist
, entry
)
2901 if (w
!= win
&& !w
->iconic
&&
2902 w
->transient
== parent
->id
)
2906 /* Map focused window. */
2909 /* Finally, map children of focus window. */
2910 TAILQ_FOREACH(w
, &ws
->winlist
, entry
)
2911 if (w
->transient
== win
->id
&& !w
->iconic
)
2916 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
2917 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1,
2924 DNPRINTF(SWM_D_FOCUS
, "focus_win: done.\n");
2927 /* If a child window should have focus instead, return it. */
2929 get_focus_magic(struct ws_win
*win
)
2931 struct ws_win
*parent
= NULL
;
2932 struct ws_win
*child
= NULL
;
2934 DNPRINTF(SWM_D_FOCUS
, "get_focus_magic: window: 0x%x\n", WINID(win
));
2938 if (win
->transient
) {
2939 parent
= find_window(win
->transient
);
2941 /* If parent prefers focus elsewhere, then try to do so. */
2942 if (parent
&& (child
= parent
->focus_child
)) {
2943 if (validate_win(child
) == 0 && child
->mapped
)
2946 parent
->focus_child
= NULL
;
2950 /* If this window prefers focus elsewhere, then try to do so. */
2951 if ((child
= win
->focus_child
)) {
2952 if (validate_win(child
) == 0 && child
->mapped
)
2955 win
->focus_child
= NULL
;
2962 event_drain(uint8_t rt
)
2964 xcb_generic_event_t
*evt
;
2966 /* ensure all pending requests have been processed before filtering. */
2968 while ((evt
= xcb_poll_for_event(conn
))) {
2969 if (XCB_EVENT_RESPONSE_TYPE(evt
) != rt
)
2977 switchws(struct swm_region
*r
, union arg
*args
)
2979 int wsid
= args
->id
, unmap_old
= 0;
2980 struct swm_region
*this_r
, *other_r
;
2982 struct workspace
*new_ws
, *old_ws
;
2987 if (wsid
>= workspace_limit
)
2991 old_ws
= this_r
->ws
;
2992 new_ws
= &this_r
->s
->ws
[wsid
];
2994 DNPRINTF(SWM_D_WS
, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
2995 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), old_ws
->idx
, wsid
);
2997 if (new_ws
== NULL
|| old_ws
== NULL
)
2999 if (new_ws
== old_ws
)
3002 unfocus_win(old_ws
->focus
);
3004 other_r
= new_ws
->r
;
3005 if (other_r
== NULL
) {
3006 /* the other workspace is hidden, hide this one */
3010 /* the other ws is visible in another region, exchange them */
3011 other_r
->ws_prior
= new_ws
;
3012 other_r
->ws
= old_ws
;
3013 old_ws
->r
= other_r
;
3015 this_r
->ws_prior
= old_ws
;
3016 this_r
->ws
= new_ws
;
3019 /* Set focus_pending before stacking. */
3020 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
3021 new_ws
->focus_pending
= get_region_focus(new_ws
->r
);
3025 /* unmap old windows */
3027 TAILQ_FOREACH(win
, &old_ws
->winlist
, entry
)
3030 /* if workspaces were swapped, then don't wait to set focus */
3031 if (old_ws
->r
&& focus_mode
!= SWM_FOCUS_FOLLOW
) {
3032 if (new_ws
->focus_pending
) {
3033 focus_win(new_ws
->focus_pending
);
3035 /* Empty region, focus on root. */
3036 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
,
3037 new_ws
->r
->s
[new_ws
->r
->s
->idx
].root
,
3042 /* Clear bar if new ws is empty. */
3043 if (new_ws
->focus_pending
== NULL
)
3048 DNPRINTF(SWM_D_WS
, "switchws: done.\n");
3052 cyclews(struct swm_region
*r
, union arg
*args
)
3055 struct swm_screen
*s
= r
->s
;
3058 DNPRINTF(SWM_D_WS
, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3059 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
3064 case SWM_ARG_ID_CYCLEWS_UP_ALL
:
3067 case SWM_ARG_ID_CYCLEWS_UP
:
3068 if (a
.id
< workspace_limit
- 1)
3073 case SWM_ARG_ID_CYCLEWS_DOWN_ALL
:
3076 case SWM_ARG_ID_CYCLEWS_DOWN
:
3080 a
.id
= workspace_limit
- 1;
3087 (!cycle_empty
&& TAILQ_EMPTY(&s
->ws
[a
.id
].winlist
)))
3089 if (!cycle_visible
&& s
->ws
[a
.id
].r
!= NULL
)
3093 } while (a
.id
!= r
->ws
->idx
);
3097 priorws(struct swm_region
*r
, union arg
*args
)
3103 DNPRINTF(SWM_D_WS
, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3104 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
3106 if (r
->ws_prior
== NULL
)
3109 a
.id
= r
->ws_prior
->idx
;
3114 cyclescr(struct swm_region
*r
, union arg
*args
)
3117 struct swm_region
*rr
= NULL
;
3120 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
3121 /* do nothing if we don't have more than one screen */
3122 if (!(num_screens
> 1 || outputs
> 1))
3127 case SWM_ARG_ID_CYCLESC_UP
:
3128 rr
= TAILQ_NEXT(r
, entry
);
3130 rr
= TAILQ_FIRST(&screens
[i
].rl
);
3132 case SWM_ARG_ID_CYCLESC_DOWN
:
3133 rr
= TAILQ_PREV(r
, swm_region_list
, entry
);
3135 rr
= TAILQ_LAST(&screens
[i
].rl
, swm_region_list
);
3143 nfw
= get_region_focus(rr
);
3147 /* New region is empty; unfocus old region and warp pointer. */
3148 unfocus_win(r
->ws
->focus
);
3149 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
,
3150 rr
->s
[i
].root
, XCB_CURRENT_TIME
);
3152 /* Clear bar since empty. */
3160 sort_windows(struct ws_win_list
*wl
)
3162 struct ws_win
*win
, *parent
, *nxt
;
3167 for (win
= TAILQ_FIRST(wl
); win
!= TAILQ_END(wl
); win
= nxt
) {
3168 nxt
= TAILQ_NEXT(win
, entry
);
3169 if (win
->transient
) {
3170 parent
= find_window(win
->transient
);
3171 if (parent
== NULL
) {
3172 warnx("not possible bug");
3175 TAILQ_REMOVE(wl
, win
, entry
);
3176 TAILQ_INSERT_AFTER(wl
, parent
, win
, entry
);
3183 swapwin(struct swm_region
*r
, union arg
*args
)
3185 struct ws_win
*target
, *source
;
3186 struct ws_win
*cur_focus
;
3187 struct ws_win_list
*wl
;
3189 DNPRINTF(SWM_D_WS
, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3190 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
3192 cur_focus
= r
->ws
->focus
;
3193 if (cur_focus
== NULL
)
3197 wl
= &source
->ws
->winlist
;
3200 case SWM_ARG_ID_SWAPPREV
:
3201 if (source
->transient
)
3202 source
= find_window(source
->transient
);
3203 target
= TAILQ_PREV(source
, ws_win_list
, entry
);
3204 if (target
&& target
->transient
)
3205 target
= find_window(target
->transient
);
3206 TAILQ_REMOVE(wl
, source
, entry
);
3208 TAILQ_INSERT_TAIL(wl
, source
, entry
);
3210 TAILQ_INSERT_BEFORE(target
, source
, entry
);
3212 case SWM_ARG_ID_SWAPNEXT
:
3213 target
= TAILQ_NEXT(source
, entry
);
3214 /* move the parent and let the sort handle the move */
3215 if (source
->transient
)
3216 source
= find_window(source
->transient
);
3217 TAILQ_REMOVE(wl
, source
, entry
);
3219 TAILQ_INSERT_HEAD(wl
, source
, entry
);
3221 TAILQ_INSERT_AFTER(wl
, target
, source
, entry
);
3223 case SWM_ARG_ID_SWAPMAIN
:
3224 target
= TAILQ_FIRST(wl
);
3225 if (target
== source
) {
3226 if (source
->ws
->focus_prev
!= NULL
&&
3227 source
->ws
->focus_prev
!= target
)
3228 source
= source
->ws
->focus_prev
;
3232 if (target
== NULL
|| source
== NULL
)
3234 source
->ws
->focus_prev
= target
;
3235 TAILQ_REMOVE(wl
, target
, entry
);
3236 TAILQ_INSERT_BEFORE(source
, target
, entry
);
3237 TAILQ_REMOVE(wl
, source
, entry
);
3238 TAILQ_INSERT_HEAD(wl
, source
, entry
);
3240 case SWM_ARG_ID_MOVELAST
:
3241 TAILQ_REMOVE(wl
, source
, entry
);
3242 TAILQ_INSERT_TAIL(wl
, source
, entry
);
3245 DNPRINTF(SWM_D_MOVE
, "swapwin: invalid id: %d\n", args
->id
);
3257 get_focus_prev(struct ws_win
*win
)
3259 struct ws_win
*winfocus
= NULL
;
3260 struct ws_win
*cur_focus
= NULL
;
3261 struct ws_win_list
*wl
= NULL
;
3262 struct workspace
*ws
= NULL
;
3264 if (!(win
&& win
->ws
))
3269 cur_focus
= ws
->focus
;
3271 DNPRINTF(SWM_D_FOCUS
, "get_focus_prev: window: 0x%x, cur_focus: 0x%x\n",
3272 WINID(win
), WINID(cur_focus
));
3274 /* pickle, just focus on whatever */
3275 if (cur_focus
== NULL
) {
3276 /* use prev_focus if valid */
3277 if (ws
->focus_prev
&& ws
->focus_prev
!= cur_focus
&&
3278 find_window(WINID(ws
->focus_prev
)))
3279 winfocus
= ws
->focus_prev
;
3283 /* if transient focus on parent */
3284 if (cur_focus
->transient
) {
3285 winfocus
= find_window(cur_focus
->transient
);
3289 /* if in max_stack try harder */
3290 if ((win
->quirks
& SWM_Q_FOCUSPREV
) ||
3291 (ws
->cur_layout
->flags
& SWM_L_FOCUSPREV
)) {
3292 if (cur_focus
!= ws
->focus_prev
)
3293 winfocus
= ws
->focus_prev
;
3295 winfocus
= TAILQ_PREV(win
, ws_win_list
, entry
);
3300 DNPRINTF(SWM_D_FOCUS
, "get_focus_prev: focus_close: %d\n", focus_close
);
3302 if (winfocus
== NULL
|| winfocus
== win
) {
3303 switch (focus_close
) {
3304 case SWM_STACK_BOTTOM
:
3305 winfocus
= TAILQ_FIRST(wl
);
3308 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
3310 case SWM_STACK_ABOVE
:
3311 if ((winfocus
= TAILQ_NEXT(cur_focus
, entry
)) == NULL
) {
3312 if (focus_close_wrap
)
3313 winfocus
= TAILQ_FIRST(wl
);
3315 winfocus
= TAILQ_PREV(cur_focus
,
3316 ws_win_list
, entry
);
3319 case SWM_STACK_BELOW
:
3320 if ((winfocus
= TAILQ_PREV(cur_focus
, ws_win_list
,
3322 if (focus_close_wrap
)
3323 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
3325 winfocus
= TAILQ_NEXT(cur_focus
, entry
);
3331 if (winfocus
== NULL
) {
3332 if (focus_default
== SWM_STACK_TOP
)
3333 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
3335 winfocus
= TAILQ_FIRST(wl
);
3340 return get_focus_magic(winfocus
);
3344 get_region_focus(struct swm_region
*r
)
3346 struct ws_win
*winfocus
= NULL
;
3351 if (r
->ws
->focus
&& !r
->ws
->focus
->iconic
)
3352 winfocus
= r
->ws
->focus
;
3353 else if (r
->ws
->focus_prev
&& !r
->ws
->focus_prev
->iconic
)
3354 winfocus
= r
->ws
->focus_prev
;
3356 TAILQ_FOREACH(winfocus
, &r
->ws
->winlist
, entry
)
3357 if (!winfocus
->iconic
)
3360 return get_focus_magic(winfocus
);
3364 focus(struct swm_region
*r
, union arg
*args
)
3366 struct ws_win
*head
, *cur_focus
= NULL
, *winfocus
= NULL
;
3367 struct ws_win_list
*wl
= NULL
;
3368 struct workspace
*ws
= NULL
;
3374 DNPRINTF(SWM_D_FOCUS
, "focus: id: %d\n", args
->id
);
3376 if ((cur_focus
= r
->ws
->focus
) == NULL
)
3380 if (TAILQ_EMPTY(wl
))
3382 /* make sure there is at least one uniconified window */
3384 TAILQ_FOREACH(winfocus
, wl
, entry
)
3385 if (!winfocus
->iconic
) {
3393 case SWM_ARG_ID_FOCUSPREV
:
3394 head
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
3396 head
= TAILQ_LAST(wl
, ws_win_list
);
3398 if (WINID(winfocus
) == cur_focus
->transient
) {
3399 head
= TAILQ_PREV(winfocus
, ws_win_list
, entry
);
3401 head
= TAILQ_LAST(wl
, ws_win_list
);
3406 if (winfocus
&& winfocus
->iconic
) {
3407 while (winfocus
!= cur_focus
) {
3408 if (winfocus
== NULL
)
3409 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
3410 if (!winfocus
->iconic
)
3412 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
,
3418 case SWM_ARG_ID_FOCUSNEXT
:
3419 head
= TAILQ_NEXT(cur_focus
, entry
);
3421 head
= TAILQ_FIRST(wl
);
3425 if (winfocus
&& winfocus
->iconic
) {
3426 while (winfocus
!= cur_focus
) {
3427 if (winfocus
== NULL
)
3428 winfocus
= TAILQ_FIRST(wl
);
3429 if (!winfocus
->iconic
)
3431 winfocus
= TAILQ_NEXT(winfocus
, entry
);
3436 case SWM_ARG_ID_FOCUSMAIN
:
3437 winfocus
= TAILQ_FIRST(wl
);
3438 if (winfocus
== cur_focus
)
3439 winfocus
= cur_focus
->ws
->focus_prev
;
3446 focus_win(get_focus_magic(winfocus
));
3452 cycle_layout(struct swm_region
*r
, union arg
*args
)
3454 struct workspace
*ws
= r
->ws
;
3456 /* suppress unused warning since var is needed */
3459 DNPRINTF(SWM_D_EVENT
, "cycle_layout: workspace: %d\n", ws
->idx
);
3462 if (ws
->cur_layout
->l_stack
== NULL
)
3463 ws
->cur_layout
= &layouts
[0];
3468 focus_win(get_region_focus(r
));
3474 stack_config(struct swm_region
*r
, union arg
*args
)
3476 struct workspace
*ws
= r
->ws
;
3478 DNPRINTF(SWM_D_STACK
, "stack_config: id: %d workspace: %d\n",
3481 if (ws
->cur_layout
->l_config
!= NULL
)
3482 ws
->cur_layout
->l_config(ws
, args
->id
);
3484 if (args
->id
!= SWM_ARG_ID_STACKINIT
)
3493 struct swm_geometry g
;
3494 struct swm_region
*r
;
3500 DNPRINTF(SWM_D_STACK
, "stack: begin\n");
3502 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
3503 for (i
= 0; i
< num_screens
; i
++) {
3507 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
3508 DNPRINTF(SWM_D_STACK
, "stack: workspace: %d "
3509 "(screen: %d, region: %d)\n", r
->ws
->idx
, i
, j
++);
3511 /* start with screen geometry, adjust for bar */
3513 g
.w
-= 2 * border_width
;
3514 g
.h
-= 2 * border_width
;
3520 r
->ws
->cur_layout
->l_stack(r
->ws
, &g
);
3521 r
->ws
->cur_layout
->l_string(r
->ws
);
3522 /* save r so we can track region changes */
3529 DNPRINTF(SWM_D_STACK
, "stack: end\n");
3533 store_float_geom(struct ws_win
*win
, struct swm_region
*r
)
3535 if (win
== NULL
|| r
== NULL
)
3538 /* retain window geom and region geom */
3539 win
->g_float
= win
->g
;
3540 win
->g_float
.x
-= X(r
);
3541 win
->g_float
.y
-= Y(r
);
3542 win
->g_floatvalid
= 1;
3543 DNPRINTF(SWM_D_MISC
, "store_float_geom: window: 0x%x, g: (%d,%d)"
3544 " %d x %d, g_float: (%d,%d) %d x %d\n", win
->id
, X(win
), Y(win
),
3545 WIDTH(win
), HEIGHT(win
), win
->g_float
.x
, win
->g_float
.y
,
3546 win
->g_float
.w
, win
->g_float
.h
);
3550 load_float_geom(struct ws_win
*win
, struct swm_region
*r
)
3552 if (win
== NULL
|| r
== NULL
)
3555 if (win
->g_floatvalid
) {
3556 win
->g
= win
->g_float
;
3559 DNPRINTF(SWM_D_MISC
, "load_float_geom: window: 0x%x, g: (%d,%d)"
3560 "%d x %d\n", win
->id
, X(win
), Y(win
), WIDTH(win
),
3563 DNPRINTF(SWM_D_MISC
, "load_float_geom: window: 0x%x, g_float "
3564 "is not set.\n", win
->id
);
3569 stack_floater(struct ws_win
*win
, struct swm_region
*r
)
3574 DNPRINTF(SWM_D_MISC
, "stack_floater: window: 0x%x\n", win
->id
);
3577 * to allow windows to change their size (e.g. mplayer fs) only retrieve
3578 * geom on ws switches or return from max mode
3580 if (win
->floatmaxed
|| (r
!= r
->ws
->old_r
&&
3581 !(win
->ewmh_flags
& EWMH_F_FULLSCREEN
))) {
3582 /* update geometry for the new region */
3583 load_float_geom(win
, r
);
3586 win
->floatmaxed
= 0;
3589 * if set to fullscreen mode, configure window to maximum size.
3591 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
3592 if (!win
->g_floatvalid
)
3593 store_float_geom(win
, win
->ws
->r
);
3599 * remove border on fullscreen floater when in fullscreen mode or when
3600 * the quirk is present.
3602 if ((win
->ewmh_flags
& EWMH_F_FULLSCREEN
) ||
3603 ((win
->quirks
& SWM_Q_FULLSCREEN
) &&
3604 (WIDTH(win
) >= WIDTH(r
)) && (HEIGHT(win
) >= HEIGHT(r
)))) {
3605 if (win
->bordered
) {
3607 X(win
) += border_width
;
3608 Y(win
) += border_width
;
3610 } else if (!win
->bordered
) {
3612 X(win
) -= border_width
;
3613 Y(win
) -= border_width
;
3616 if (win
->transient
&& (win
->quirks
& SWM_Q_TRANSSZ
)) {
3617 WIDTH(win
) = (double)WIDTH(r
) * dialog_ratio
;
3618 HEIGHT(win
) = (double)HEIGHT(r
) * dialog_ratio
;
3621 if (!win
->manual
&& !(win
->ewmh_flags
& EWMH_F_FULLSCREEN
) &&
3622 !(win
->quirks
& SWM_Q_ANYWHERE
)) {
3624 * floaters and transients are auto-centred unless moved,
3625 * resized or ANYWHERE quirk is set.
3627 X(win
) = X(r
) + (WIDTH(r
) - WIDTH(win
)) / 2 - BORDER(win
);
3628 Y(win
) = Y(r
) + (HEIGHT(r
) - HEIGHT(win
)) / 2 - BORDER(win
);
3630 store_float_geom(win
, r
);
3633 /* keep window within region bounds */
3634 constrain_window(win
, r
, 0);
3640 * Send keystrokes to terminal to decrease/increase the font size as the
3641 * window size changes.
3644 adjust_font(struct ws_win
*win
)
3646 if (!(win
->quirks
& SWM_Q_XTERM_FONTADJ
) ||
3647 win
->floating
|| win
->transient
)
3650 if (win
->sh
.width_inc
&& win
->last_inc
!= win
->sh
.width_inc
&&
3651 WIDTH(win
) / win
->sh
.width_inc
< term_width
&&
3652 win
->font_steps
< SWM_MAX_FONT_STEPS
) {
3653 win
->font_size_boundary
[win
->font_steps
] =
3654 (win
->sh
.width_inc
* term_width
) + win
->sh
.base_width
;
3657 win
->last_inc
= win
->sh
.width_inc
;
3658 fake_keypress(win
, XK_KP_Subtract
, XCB_MOD_MASK_SHIFT
);
3659 } else if (win
->font_steps
&& win
->last_inc
!= win
->sh
.width_inc
&&
3660 WIDTH(win
) > win
->font_size_boundary
[win
->font_steps
- 1]) {
3663 win
->last_inc
= win
->sh
.width_inc
;
3664 fake_keypress(win
, XK_KP_Add
, XCB_MOD_MASK_SHIFT
);
3668 #define SWAPXY(g) do { \
3670 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
3671 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
3674 stack_master(struct workspace
*ws
, struct swm_geometry
*g
, int rot
, int flip
)
3676 struct swm_geometry win_g
, r_g
= *g
;
3677 struct ws_win
*win
, *fs_win
= NULL
;
3678 int i
, j
, s
, stacks
;
3679 int w_inc
= 1, h_inc
, w_base
= 1, h_base
;
3680 int hrh
, extra
= 0, h_slice
, last_h
= 0;
3681 int split
, colno
, winno
, mwin
, msize
, mscale
;
3682 int remain
, missing
, v_slice
, reconfigure
= 0;
3685 DNPRINTF(SWM_D_STACK
, "stack_master: workspace: %d, rot: %s, "
3686 "flip: %s\n", ws
->idx
, YESNO(rot
), YESNO(flip
));
3688 winno
= count_win(ws
, 0);
3689 if (winno
== 0 && count_win(ws
, 1) == 0)
3692 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
3693 if (!win
->transient
&& !win
->floating
&& !win
->iconic
)
3700 w_inc
= win
->sh
.width_inc
;
3701 w_base
= win
->sh
.base_width
;
3702 mwin
= ws
->l_state
.horizontal_mwin
;
3703 mscale
= ws
->l_state
.horizontal_msize
;
3704 stacks
= ws
->l_state
.horizontal_stacks
;
3707 w_inc
= win
->sh
.height_inc
;
3708 w_base
= win
->sh
.base_height
;
3709 mwin
= ws
->l_state
.vertical_mwin
;
3710 mscale
= ws
->l_state
.vertical_msize
;
3711 stacks
= ws
->l_state
.vertical_stacks
;
3715 if (stacks
> winno
- mwin
)
3716 stacks
= winno
- mwin
;
3720 h_slice
= r_g
.h
/ SWM_H_SLICE
;
3721 if (mwin
&& winno
> mwin
) {
3722 v_slice
= r_g
.w
/ SWM_V_SLICE
;
3726 win_g
.w
= v_slice
* mscale
;
3728 if (w_inc
> 1 && w_inc
< v_slice
) {
3729 /* adjust for window's requested size increment */
3730 remain
= (win_g
.w
- w_base
) % w_inc
;
3736 win_g
.x
+= r_g
.w
- msize
;
3739 colno
= split
= winno
/ stacks
;
3740 win_g
.w
= ((r_g
.w
- (stacks
* 2 * border_width
) +
3741 2 * border_width
) / stacks
);
3743 hrh
= r_g
.h
/ colno
;
3744 extra
= r_g
.h
- (colno
* hrh
);
3745 win_g
.h
= hrh
- 2 * border_width
;
3747 /* stack all the tiled windows */
3748 i
= j
= 0, s
= stacks
;
3749 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3750 if (win
->transient
|| win
->floating
)
3755 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
3760 if (split
&& i
== split
) {
3761 colno
= (winno
- mwin
) / stacks
;
3762 if (s
<= (winno
- mwin
) % stacks
)
3764 split
= split
+ colno
;
3765 hrh
= (r_g
.h
/ colno
);
3766 extra
= r_g
.h
- (colno
* hrh
);
3770 win_g
.x
+= win_g
.w
+ 2 * border_width
;
3771 win_g
.w
= (r_g
.w
- msize
-
3772 (stacks
* 2 * border_width
)) / stacks
;
3774 win_g
.w
+= (r_g
.w
- msize
-
3775 (stacks
* 2 * border_width
)) % stacks
;
3779 win_g
.h
= hrh
- 2 * border_width
;
3781 h_inc
= win
->sh
.width_inc
;
3782 h_base
= win
->sh
.base_width
;
3784 h_inc
= win
->sh
.height_inc
;
3785 h_base
= win
->sh
.base_height
;
3787 if (j
== colno
- 1) {
3788 win_g
.h
= hrh
+ extra
;
3789 } else if (h_inc
> 1 && h_inc
< h_slice
) {
3790 /* adjust for window's requested size increment */
3791 remain
= (win_g
.h
- h_base
) % h_inc
;
3792 missing
= h_inc
- remain
;
3794 if (missing
<= extra
|| j
== 0) {
3806 win_g
.y
+= last_h
+ 2 * border_width
;
3808 if (disable_border
&& !bar_enabled
&& winno
== 1){
3810 win_g
.w
+= 2 * border_width
;
3811 win_g
.h
+= 2 * border_width
;
3816 if (X(win
) != win_g
.y
|| Y(win
) != win_g
.x
||
3817 WIDTH(win
) != win_g
.h
|| HEIGHT(win
) != win_g
.w
) {
3821 WIDTH(win
) = win_g
.h
;
3822 HEIGHT(win
) = win_g
.w
;
3825 if (X(win
) != win_g
.x
|| Y(win
) != win_g
.y
||
3826 WIDTH(win
) != win_g
.w
|| HEIGHT(win
) != win_g
.h
) {
3830 WIDTH(win
) = win_g
.w
;
3831 HEIGHT(win
) = win_g
.h
;
3835 if (bordered
!= win
->bordered
) {
3837 win
->bordered
= bordered
;
3853 /* now, stack all the floaters and transients */
3854 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3855 if (!win
->transient
&& !win
->floating
)
3859 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
3864 stack_floater(win
, ws
->r
);
3868 /* Make sure fs_win is stacked last so it's on top. */
3870 stack_floater(fs_win
, ws
->r
);
3876 vertical_config(struct workspace
*ws
, int id
)
3878 DNPRINTF(SWM_D_STACK
, "vertical_config: id: %d, workspace: %d\n",
3882 case SWM_ARG_ID_STACKRESET
:
3883 case SWM_ARG_ID_STACKINIT
:
3884 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ 2;
3885 ws
->l_state
.vertical_mwin
= 1;
3886 ws
->l_state
.vertical_stacks
= 1;
3888 case SWM_ARG_ID_MASTERSHRINK
:
3889 if (ws
->l_state
.vertical_msize
> 1)
3890 ws
->l_state
.vertical_msize
--;
3892 case SWM_ARG_ID_MASTERGROW
:
3893 if (ws
->l_state
.vertical_msize
< SWM_V_SLICE
- 1)
3894 ws
->l_state
.vertical_msize
++;
3896 case SWM_ARG_ID_MASTERADD
:
3897 ws
->l_state
.vertical_mwin
++;
3899 case SWM_ARG_ID_MASTERDEL
:
3900 if (ws
->l_state
.vertical_mwin
> 0)
3901 ws
->l_state
.vertical_mwin
--;
3903 case SWM_ARG_ID_STACKINC
:
3904 ws
->l_state
.vertical_stacks
++;
3906 case SWM_ARG_ID_STACKDEC
:
3907 if (ws
->l_state
.vertical_stacks
> 1)
3908 ws
->l_state
.vertical_stacks
--;
3910 case SWM_ARG_ID_FLIPLAYOUT
:
3911 ws
->l_state
.vertical_flip
= !ws
->l_state
.vertical_flip
;
3919 vertical_stack(struct workspace
*ws
, struct swm_geometry
*g
)
3921 DNPRINTF(SWM_D_STACK
, "vertical_stack: workspace: %d\n", ws
->idx
);
3923 stack_master(ws
, g
, 0, ws
->l_state
.vertical_flip
);
3927 horizontal_config(struct workspace
*ws
, int id
)
3929 DNPRINTF(SWM_D_STACK
, "horizontal_config: workspace: %d\n", ws
->idx
);
3932 case SWM_ARG_ID_STACKRESET
:
3933 case SWM_ARG_ID_STACKINIT
:
3934 ws
->l_state
.horizontal_mwin
= 1;
3935 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ 2;
3936 ws
->l_state
.horizontal_stacks
= 1;
3938 case SWM_ARG_ID_MASTERSHRINK
:
3939 if (ws
->l_state
.horizontal_msize
> 1)
3940 ws
->l_state
.horizontal_msize
--;
3942 case SWM_ARG_ID_MASTERGROW
:
3943 if (ws
->l_state
.horizontal_msize
< SWM_H_SLICE
- 1)
3944 ws
->l_state
.horizontal_msize
++;
3946 case SWM_ARG_ID_MASTERADD
:
3947 ws
->l_state
.horizontal_mwin
++;
3949 case SWM_ARG_ID_MASTERDEL
:
3950 if (ws
->l_state
.horizontal_mwin
> 0)
3951 ws
->l_state
.horizontal_mwin
--;
3953 case SWM_ARG_ID_STACKINC
:
3954 ws
->l_state
.horizontal_stacks
++;
3956 case SWM_ARG_ID_STACKDEC
:
3957 if (ws
->l_state
.horizontal_stacks
> 1)
3958 ws
->l_state
.horizontal_stacks
--;
3960 case SWM_ARG_ID_FLIPLAYOUT
:
3961 ws
->l_state
.horizontal_flip
= !ws
->l_state
.horizontal_flip
;
3969 horizontal_stack(struct workspace
*ws
, struct swm_geometry
*g
)
3971 DNPRINTF(SWM_D_STACK
, "horizontal_stack: workspace: %d\n", ws
->idx
);
3973 stack_master(ws
, g
, 1, ws
->l_state
.horizontal_flip
);
3976 /* fullscreen view */
3978 max_stack(struct workspace
*ws
, struct swm_geometry
*g
)
3980 struct swm_geometry gg
= *g
;
3981 struct ws_win
*w
, *win
= NULL
, *parent
= NULL
;
3982 int winno
, num_screens
;
3984 DNPRINTF(SWM_D_STACK
, "max_stack: workspace: %d\n", ws
->idx
);
3989 winno
= count_win(ws
, 0);
3990 if (winno
== 0 && count_win(ws
, 1) == 0)
3993 /* Figure out which top level window should be visible. */
3994 if (ws
->focus_pending
)
3995 win
= ws
->focus_pending
;
3999 win
= TAILQ_FIRST(&ws
->winlist
);
4002 parent
= find_window(win
->transient
);
4004 DNPRINTF(SWM_D_STACK
, "max_stack: win: 0x%x\n", win
->id
);
4006 /* maximize all top level windows */
4007 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
4008 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
4009 if (w
->transient
|| w
->iconic
)
4012 if (w
->floating
&& !w
->floatmaxed
) {
4014 * retain geometry for retrieval on exit from
4017 store_float_geom(w
, ws
->r
);
4021 /* only reconfigure if necessary */
4022 if (X(w
) != gg
.x
|| Y(w
) != gg
.y
|| WIDTH(w
) != gg
.w
||
4023 HEIGHT(w
) != gg
.h
) {
4029 WIDTH(w
) += 2 * border_width
;
4030 HEIGHT(w
) += 2 * border_width
;
4036 /* Unmap unwanted windows if not multi-screen. */
4037 if (!(num_screens
> 1 || outputs
> 1) && (w
!= win
||
4038 w
!= parent
|| w
->transient
!= win
->id
))
4042 /* If a parent exists, map it first. */
4046 /* Map siblings next. */
4047 TAILQ_FOREACH(w
, &ws
->winlist
, entry
)
4048 if (w
!= win
&& !w
->iconic
&&
4049 w
->transient
== parent
->id
) {
4050 stack_floater(w
, ws
->r
);
4055 /* Map focused window. */
4058 /* Finally, map children of focus window. */
4059 TAILQ_FOREACH(w
, &ws
->winlist
, entry
)
4060 if (w
->transient
== win
->id
&& !w
->iconic
) {
4061 stack_floater(w
, ws
->r
);
4067 send_to_ws(struct swm_region
*r
, union arg
*args
)
4069 int wsid
= args
->id
;
4070 struct ws_win
*win
= NULL
, *parent
;
4071 struct workspace
*ws
, *nws
, *pws
;
4072 char ws_idx_str
[SWM_PROPLEN
];
4074 if (wsid
>= workspace_limit
)
4077 if (r
&& r
->ws
&& r
->ws
->focus
)
4082 if (win
->ws
->idx
== wsid
)
4085 DNPRINTF(SWM_D_MOVE
, "send_to_ws: win 0x%x, ws %d -> %d\n", win
->id
,
4086 win
->ws
->idx
, wsid
);
4089 nws
= &win
->s
->ws
[wsid
];
4091 /* Update the window's workspace property: _SWM_WS */
4092 if (snprintf(ws_idx_str
, SWM_PROPLEN
, "%d", nws
->idx
) < SWM_PROPLEN
) {
4093 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
4094 ws
->focus_pending
= get_focus_prev(win
);
4096 /* Move the parent if this is a transient window. */
4097 if (win
->transient
) {
4098 parent
= find_window(win
->transient
);
4101 /* Set new focus in parent's ws if needed. */
4102 if (pws
->focus
== parent
) {
4103 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
4104 pws
->focus_pending
=
4105 get_focus_prev(parent
);
4107 unfocus_win(parent
);
4109 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
4110 pws
->focus
= pws
->focus_pending
;
4112 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
4113 pws
->focus_pending
= NULL
;
4116 /* Don't unmap parent if new ws is visible */
4118 unmap_window(parent
);
4121 TAILQ_REMOVE(&ws
->winlist
, parent
, entry
);
4122 TAILQ_INSERT_TAIL(&nws
->winlist
, parent
, entry
);
4125 DNPRINTF(SWM_D_PROP
, "send_to_ws: set "
4126 "property: _SWM_WS: %s\n", ws_idx_str
);
4127 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
4128 parent
->id
, a_swm_ws
, XCB_ATOM_STRING
, 8,
4129 strlen(ws_idx_str
), ws_idx_str
);
4135 /* Don't unmap if new ws is visible */
4140 TAILQ_REMOVE(&ws
->winlist
, win
, entry
);
4141 TAILQ_INSERT_TAIL(&nws
->winlist
, win
, entry
);
4144 /* Set focus on new ws. */
4145 unfocus_win(nws
->focus
);
4148 DNPRINTF(SWM_D_PROP
, "send_to_ws: set property: _SWM_WS: %s\n",
4150 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
4151 a_swm_ws
, XCB_ATOM_STRING
, 8, strlen(ws_idx_str
),
4154 /* Restack and set new focus. */
4157 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
4158 focus_win(ws
->focus_pending
);
4159 ws
->focus_pending
= NULL
;
4165 DNPRINTF(SWM_D_MOVE
, "send_to_ws: done.\n");
4169 pressbutton(struct swm_region
*r
, union arg
*args
)
4171 /* suppress unused warning since var is needed */
4174 xcb_test_fake_input(conn
, XCB_BUTTON_PRESS
, args
->id
,
4175 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
4176 xcb_test_fake_input(conn
, XCB_BUTTON_RELEASE
, args
->id
,
4177 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
4181 raise_toggle(struct swm_region
*r
, union arg
*args
)
4183 /* suppress unused warning since var is needed */
4186 if (r
== NULL
|| r
->ws
== NULL
)
4189 r
->ws
->always_raise
= !r
->ws
->always_raise
;
4191 /* bring floaters back to top */
4192 if (!r
->ws
->always_raise
)
4199 iconify(struct swm_region
*r
, union arg
*args
)
4201 /* suppress unused warning since var is needed */
4204 if (r
->ws
->focus
== NULL
)
4207 set_swm_iconic(r
->ws
->focus
, 1);
4213 get_win_name(xcb_window_t win
)
4216 xcb_get_property_cookie_t c
;
4217 xcb_get_property_reply_t
*r
;
4219 /* First try _NET_WM_NAME for UTF-8. */
4220 c
= xcb_get_property(conn
, 0, win
, a_netwmname
,
4221 XCB_GET_PROPERTY_TYPE_ANY
, 0, UINT_MAX
);
4222 r
= xcb_get_property_reply(conn
, c
, NULL
);
4224 if (!r
|| r
->type
== XCB_NONE
) {
4226 /* Use WM_NAME instead; no UTF-8. */
4227 c
= xcb_get_property(conn
, 0, win
, XCB_ATOM_WM_NAME
,
4228 XCB_GET_PROPERTY_TYPE_ANY
, 0, UINT_MAX
);
4229 r
= xcb_get_property_reply(conn
, c
, NULL
);
4231 if(!r
|| r
->type
== XCB_NONE
) {
4238 name
= strndup(xcb_get_property_value(r
),
4239 xcb_get_property_value_length(r
));
4246 uniconify(struct swm_region
*r
, union arg
*args
)
4253 DNPRINTF(SWM_D_MISC
, "uniconify\n");
4255 if (r
== NULL
|| r
->ws
== NULL
)
4258 /* make sure we have anything to uniconify */
4259 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
4260 if (win
->ws
== NULL
)
4261 continue; /* should never happen */
4270 search_resp_action
= SWM_SEARCH_UNICONIFY
;
4272 spawn_select(r
, args
, "search", &searchpid
);
4274 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
4277 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
4278 if (win
->ws
== NULL
)
4279 continue; /* should never happen */
4283 name
= get_win_name(win
->id
);
4286 fprintf(lfile
, "%s.%u\n", name
, win
->id
);
4294 name_workspace(struct swm_region
*r
, union arg
*args
)
4298 DNPRINTF(SWM_D_MISC
, "name_workspace\n");
4304 search_resp_action
= SWM_SEARCH_NAME_WORKSPACE
;
4306 spawn_select(r
, args
, "name_workspace", &searchpid
);
4308 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
4311 fprintf(lfile
, "%s", "");
4316 search_workspace(struct swm_region
*r
, union arg
*args
)
4319 struct workspace
*ws
;
4322 DNPRINTF(SWM_D_MISC
, "search_workspace\n");
4328 search_resp_action
= SWM_SEARCH_SEARCH_WORKSPACE
;
4330 spawn_select(r
, args
, "search", &searchpid
);
4332 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
4335 for (i
= 0; i
< workspace_limit
; i
++) {
4339 fprintf(lfile
, "%d%s%s\n", ws
->idx
+ 1,
4340 (ws
->name
? ":" : ""), (ws
->name
? ws
->name
: ""));
4347 search_win_cleanup(void)
4349 struct search_window
*sw
= NULL
;
4351 while ((sw
= TAILQ_FIRST(&search_wl
)) != NULL
) {
4352 xcb_destroy_window(conn
, sw
->indicator
);
4353 TAILQ_REMOVE(&search_wl
, sw
, entry
);
4359 search_win(struct swm_region
*r
, union arg
*args
)
4361 struct ws_win
*win
= NULL
;
4362 struct search_window
*sw
= NULL
;
4365 int i
, width
, height
;
4373 XRectangle l_ibox
, l_lbox
;
4375 DNPRINTF(SWM_D_MISC
, "search_win\n");
4378 search_resp_action
= SWM_SEARCH_SEARCH_WINDOW
;
4380 spawn_select(r
, args
, "search", &searchpid
);
4382 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
4385 TAILQ_INIT(&search_wl
);
4388 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
4392 sw
= calloc(1, sizeof(struct search_window
));
4394 warn("search_win: calloc");
4396 search_win_cleanup();
4402 snprintf(s
, sizeof s
, "%d", i
);
4405 w
= xcb_generate_id(conn
);
4406 wa
[0] = r
->s
->c
[SWM_S_COLOR_FOCUS
].pixel
;
4407 wa
[1] = r
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
;
4409 if (bar_font_legacy
) {
4410 XmbTextExtents(bar_fs
, s
, len
, &l_ibox
, &l_lbox
);
4411 width
= l_lbox
.width
+ 4;
4412 height
= bar_fs_extents
->max_logical_extent
.height
+ 4;
4414 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
,
4416 width
= info
.width
+ 4;
4417 height
= bar_font
->height
+ 4;
4420 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, w
, win
->id
, 0, 0,
4421 width
, height
, 1, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
4422 XCB_COPY_FROM_PARENT
, XCB_CW_BACK_PIXEL
|
4423 XCB_CW_BORDER_PIXEL
, wa
);
4425 xcb_map_window(conn
, w
);
4428 TAILQ_INSERT_TAIL(&search_wl
, sw
, entry
);
4430 if (bar_font_legacy
) {
4431 l_gcv
.graphics_exposures
= 0;
4432 l_draw
= XCreateGC(display
, w
, 0, &l_gcv
);
4434 XSetForeground(display
, l_draw
,
4435 r
->s
->c
[SWM_S_COLOR_BAR
].pixel
);
4437 DRAWSTRING(display
, w
, bar_fs
, l_draw
, 2,
4438 (bar_fs_extents
->max_logical_extent
.height
-
4439 l_lbox
.height
) / 2 - l_lbox
.y
, s
, len
);
4441 XFreeGC(display
, l_draw
);
4444 draw
= XftDrawCreate(display
, w
,
4445 DefaultVisual(display
, r
->s
->idx
),
4446 DefaultColormap(display
, r
->s
->idx
));
4448 XftDrawStringUtf8(draw
, &bar_font_color
, bar_font
, 2,
4449 (HEIGHT(r
->bar
) + bar_font
->height
) / 2 -
4450 bar_font
->descent
, (FcChar8
*)s
, len
);
4452 XftDrawDestroy(draw
);
4455 DNPRINTF(SWM_D_MISC
, "search_win: mapped window: 0x%x\n", w
);
4457 fprintf(lfile
, "%d\n", i
);
4467 search_resp_uniconify(char *resp
, unsigned long len
)
4473 DNPRINTF(SWM_D_MISC
, "search_resp_uniconify: resp: %s\n", resp
);
4475 TAILQ_FOREACH(win
, &search_r
->ws
->winlist
, entry
) {
4478 name
= get_win_name(win
->id
);
4481 if (asprintf(&s
, "%s.%u", name
, win
->id
) == -1) {
4486 if (strncmp(s
, resp
, len
) == 0) {
4487 /* XXX this should be a callback to generalize */
4488 set_swm_iconic(win
, 0);
4497 search_resp_name_workspace(char *resp
, unsigned long len
)
4499 struct workspace
*ws
;
4501 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: resp: %s\n", resp
);
4503 if (search_r
->ws
== NULL
)
4508 free(search_r
->ws
->name
);
4509 search_r
->ws
->name
= NULL
;
4513 ws
->name
= strdup(resp
);
4514 if (ws
->name
== NULL
) {
4515 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: "
4516 "strdup: %s", strerror(errno
));
4523 search_resp_search_workspace(char *resp
)
4530 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: resp: %s\n", resp
);
4534 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: strdup: %s",
4541 ws_idx
= (int)strtonum(q
, 1, workspace_limit
, &errstr
);
4543 DNPRINTF(SWM_D_MISC
, "workspace idx is %s: %s",
4550 switchws(search_r
, &a
);
4554 search_resp_search_window(char *resp
)
4559 struct search_window
*sw
;
4561 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: resp: %s\n", resp
);
4565 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: strdup: %s",
4570 idx
= (int)strtonum(s
, 1, INT_MAX
, &errstr
);
4572 DNPRINTF(SWM_D_MISC
, "window idx is %s: %s",
4579 TAILQ_FOREACH(sw
, &search_wl
, entry
)
4580 if (idx
== sw
->idx
) {
4586 #define MAX_RESP_LEN 1024
4589 search_do_resp(void)
4595 DNPRINTF(SWM_D_MISC
, "search_do_resp:\n");
4600 if ((resp
= calloc(1, MAX_RESP_LEN
+ 1)) == NULL
) {
4601 warn("search: calloc");
4605 rbytes
= read(select_resp_pipe
[0], resp
, MAX_RESP_LEN
);
4607 warn("search: read error");
4610 resp
[rbytes
] = '\0';
4613 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
4614 * newline, so work around that by sanitizing the resp now.
4616 resp
[strcspn(resp
, "\n")] = '\0';
4619 switch (search_resp_action
) {
4620 case SWM_SEARCH_UNICONIFY
:
4621 search_resp_uniconify(resp
, len
);
4623 case SWM_SEARCH_NAME_WORKSPACE
:
4624 search_resp_name_workspace(resp
, len
);
4626 case SWM_SEARCH_SEARCH_WORKSPACE
:
4627 search_resp_search_workspace(resp
);
4629 case SWM_SEARCH_SEARCH_WINDOW
:
4630 search_resp_search_window(resp
);
4635 if (search_resp_action
== SWM_SEARCH_SEARCH_WINDOW
)
4636 search_win_cleanup();
4638 search_resp_action
= SWM_SEARCH_NONE
;
4639 close(select_resp_pipe
[0]);
4646 wkill(struct swm_region
*r
, union arg
*args
)
4648 DNPRINTF(SWM_D_MISC
, "wkill: id: %d\n", args
->id
);
4650 if (r
->ws
->focus
== NULL
)
4653 if (args
->id
== SWM_ARG_ID_KILLWINDOW
)
4654 xcb_kill_client(conn
, r
->ws
->focus
->id
);
4656 if (r
->ws
->focus
->can_delete
)
4657 client_msg(r
->ws
->focus
, a_delete
, 0);
4663 floating_toggle_win(struct ws_win
*win
)
4665 struct swm_region
*r
;
4675 /* reject floating toggles in max stack mode */
4676 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
4679 if (win
->floating
) {
4680 if (!win
->floatmaxed
) {
4681 /* retain position for refloat */
4682 store_float_geom(win
, r
);
4686 load_float_geom(win
, r
);
4690 ewmh_update_actions(win
);
4696 floating_toggle(struct swm_region
*r
, union arg
*args
)
4698 struct ws_win
*win
= r
->ws
->focus
;
4700 /* suppress unused warning since var is needed */
4706 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
4709 ewmh_update_win_state(win
, ewmh
[_NET_WM_STATE_ABOVE
].atom
,
4710 _NET_WM_STATE_TOGGLE
);
4714 if (win
== win
->ws
->focus
)
4721 constrain_window(struct ws_win
*win
, struct swm_region
*r
, int resizable
)
4723 if (MAX_X(win
) + BORDER(win
) > MAX_X(r
)) {
4725 WIDTH(win
) = MAX_X(r
) - X(win
) - BORDER(win
);
4727 X(win
) = MAX_X(r
)- WIDTH(win
) - BORDER(win
);
4730 if (X(win
) + BORDER(win
) < X(r
)) {
4732 WIDTH(win
) -= X(r
) - X(win
) - BORDER(win
);
4734 X(win
) = X(r
) - BORDER(win
);
4737 if (MAX_Y(win
) + BORDER(win
) > MAX_Y(r
)) {
4739 HEIGHT(win
) = MAX_Y(r
) - Y(win
) - BORDER(win
);
4741 Y(win
) = MAX_Y(r
) - HEIGHT(win
) - BORDER(win
);
4744 if (Y(win
) + BORDER(win
) < Y(r
)) {
4746 HEIGHT(win
) -= Y(r
) - Y(win
) - BORDER(win
);
4748 Y(win
) = Y(r
) - BORDER(win
);
4754 if (HEIGHT(win
) < 1)
4760 update_window(struct ws_win
*win
)
4765 mask
= XCB_CONFIG_WINDOW_X
| XCB_CONFIG_WINDOW_Y
|
4766 XCB_CONFIG_WINDOW_WIDTH
| XCB_CONFIG_WINDOW_HEIGHT
|
4767 XCB_CONFIG_WINDOW_BORDER_WIDTH
;
4771 wc
[3] = HEIGHT(win
);
4772 wc
[4] = BORDER(win
);
4774 DNPRINTF(SWM_D_EVENT
, "update_window: window: 0x%x, (x,y) w x h: "
4775 "(%d,%d) %d x %d, bordered: %s\n", win
->id
, wc
[0], wc
[1], wc
[2],
4776 wc
[3], YESNO(win
->bordered
));
4778 xcb_configure_window(conn
, win
->id
, mask
, wc
);
4781 #define SWM_RESIZE_STEPS (50)
4784 resize(struct ws_win
*win
, union arg
*args
)
4786 xcb_timestamp_t timestamp
= 0;
4787 struct swm_region
*r
= NULL
;
4788 int resize_step
= 0;
4789 struct swm_geometry g
;
4790 int top
= 0, left
= 0, resizing
;
4792 xcb_cursor_t cursor
;
4793 xcb_query_pointer_reply_t
*xpr
;
4794 xcb_generic_event_t
*evt
;
4795 xcb_motion_notify_event_t
*mne
;
4801 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
4804 DNPRINTF(SWM_D_EVENT
, "resize: window: 0x%x, floating: %s, "
4805 "transient: 0x%x\n", win
->id
, YESNO(win
->floating
),
4808 if (!win
->transient
&& !win
->floating
)
4811 /* reject resizes in max mode for floaters (transient ok) */
4812 if (win
->floatmaxed
)
4816 ewmh_update_win_state(win
, ewmh
[_SWM_WM_STATE_MANUAL
].atom
,
4824 case SWM_ARG_ID_WIDTHSHRINK
:
4825 WIDTH(win
) -= SWM_RESIZE_STEPS
;
4828 case SWM_ARG_ID_WIDTHGROW
:
4829 WIDTH(win
) += SWM_RESIZE_STEPS
;
4832 case SWM_ARG_ID_HEIGHTSHRINK
:
4833 HEIGHT(win
) -= SWM_RESIZE_STEPS
;
4836 case SWM_ARG_ID_HEIGHTGROW
:
4837 HEIGHT(win
) += SWM_RESIZE_STEPS
;
4844 constrain_window(win
, r
, 1);
4846 store_float_geom(win
,r
);
4850 /* get cursor offset from window root */
4851 xpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
4858 if (xpr
->win_x
< WIDTH(win
) / 2)
4861 if (xpr
->win_y
< HEIGHT(win
) / 2)
4864 if (args
->id
== SWM_ARG_ID_CENTER
)
4865 cursor
= cursors
[XC_SIZING
].cid
;
4867 cursor
= cursors
[left
? XC_TOP_LEFT_CORNER
:
4868 XC_TOP_RIGHT_CORNER
].cid
;
4870 cursor
= cursors
[left
? XC_BOTTOM_LEFT_CORNER
:
4871 XC_BOTTOM_RIGHT_CORNER
].cid
;
4873 xcb_grab_pointer(conn
, 0, win
->id
, MOUSEMASK
,
4874 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
, XCB_WINDOW_NONE
, cursor
,
4879 while ((evt
= xcb_wait_for_event(conn
)) && resizing
) {
4880 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
4881 case XCB_BUTTON_RELEASE
:
4882 DNPRINTF(SWM_D_EVENT
, "resize: BUTTON_RELEASE\n");
4885 case XCB_MOTION_NOTIFY
:
4886 mne
= (xcb_motion_notify_event_t
*)evt
;
4887 /* cursor offset/delta from start of the operation */
4888 dx
= mne
->root_x
- xpr
->root_x
;
4889 dy
= mne
->root_y
- xpr
->root_y
;
4895 if (args
->id
== SWM_ARG_ID_CENTER
) {
4896 if (g
.h
/ 2 + dy
< 1)
4900 HEIGHT(win
) = g
.h
+ 2 * dy
;
4908 HEIGHT(win
) = g
.h
+ dy
;
4915 if (args
->id
== SWM_ARG_ID_CENTER
) {
4916 if (g
.w
/ 2 + dx
< 1)
4920 WIDTH(win
) = g
.w
+ 2 * dx
;
4928 WIDTH(win
) = g
.w
+ dx
;
4931 constrain_window(win
, r
, 1);
4933 /* not free, don't sync more than 120 times / second */
4934 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
4935 timestamp
= mne
->time
;
4950 store_float_geom(win
,r
);
4952 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
4954 DNPRINTF(SWM_D_EVENT
, "resize: done.\n");
4958 resize_step(struct swm_region
*r
, union arg
*args
)
4960 struct ws_win
*win
= NULL
;
4962 if (r
&& r
->ws
&& r
->ws
->focus
)
4971 #define SWM_MOVE_STEPS (50)
4974 move(struct ws_win
*win
, union arg
*args
)
4976 xcb_timestamp_t timestamp
= 0;
4977 int move_step
= 0, moving
;
4978 struct swm_region
*r
= NULL
;
4979 xcb_query_pointer_reply_t
*qpr
;
4980 xcb_generic_event_t
*evt
;
4981 xcb_motion_notify_event_t
*mne
;
4987 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
4990 DNPRINTF(SWM_D_EVENT
, "move: window: 0x%x, floating: %s, transient: "
4991 "0x%x\n", win
->id
, YESNO(win
->floating
), win
->transient
);
4993 /* in max_stack mode should only move transients */
4994 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
] && !win
->transient
)
4998 if (!win
->floating
&& !win
->transient
) {
4999 store_float_geom(win
, r
);
5000 ewmh_update_win_state(win
, ewmh
[_NET_WM_STATE_ABOVE
].atom
,
5003 ewmh_update_win_state(win
, ewmh
[_SWM_WM_STATE_MANUAL
].atom
,
5012 case SWM_ARG_ID_MOVELEFT
:
5013 X(win
) -= (SWM_MOVE_STEPS
- border_width
);
5016 case SWM_ARG_ID_MOVERIGHT
:
5017 X(win
) += (SWM_MOVE_STEPS
- border_width
);
5020 case SWM_ARG_ID_MOVEUP
:
5021 Y(win
) -= (SWM_MOVE_STEPS
- border_width
);
5024 case SWM_ARG_ID_MOVEDOWN
:
5025 Y(win
) += (SWM_MOVE_STEPS
- border_width
);
5032 constrain_window(win
, r
, 0);
5034 store_float_geom(win
, r
);
5038 xcb_grab_pointer(conn
, 0, win
->id
, MOUSEMASK
,
5039 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
,
5040 XCB_WINDOW_NONE
, cursors
[XC_FLEUR
].cid
, XCB_CURRENT_TIME
);
5042 /* get cursor offset from window root */
5043 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
5046 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
5052 while ((evt
= xcb_wait_for_event(conn
)) && moving
) {
5053 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
5054 case XCB_BUTTON_RELEASE
:
5055 DNPRINTF(SWM_D_EVENT
, "move: BUTTON_RELEASE\n");
5058 case XCB_MOTION_NOTIFY
:
5059 mne
= (xcb_motion_notify_event_t
*)evt
;
5060 X(win
) = mne
->root_x
- qpr
->win_x
- border_width
;
5061 Y(win
) = mne
->root_y
- qpr
->win_y
- border_width
;
5063 constrain_window(win
, r
, 0);
5065 /* not free, don't sync more than 120 times / second */
5066 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
5067 timestamp
= mne
->time
;
5082 store_float_geom(win
, r
);
5084 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
5085 DNPRINTF(SWM_D_EVENT
, "move: done.\n");
5089 move_step(struct swm_region
*r
, union arg
*args
)
5091 struct ws_win
*win
= NULL
;
5093 if (r
&& r
->ws
&& r
->ws
->focus
)
5098 if (!win
->transient
&& !win
->floating
)
5105 /* user/key callable function IDs */
5155 KF_SEARCH_WORKSPACE
,
5196 KF_DUMPWINS
, /* MUST BE LAST */
5200 /* key definitions */
5202 char name
[SWM_FUNCNAME_LEN
];
5203 void (*func
)(struct swm_region
*r
, union arg
*);
5205 } keyfuncs
[KF_INVALID
+ 1] = {
5206 /* name function argument */
5207 { "bar_toggle", bar_toggle
, {0} },
5208 { "button2", pressbutton
, {2} },
5209 { "cycle_layout", cycle_layout
, {0} },
5210 { "flip_layout", stack_config
, {.id
= SWM_ARG_ID_FLIPLAYOUT
} },
5211 { "float_toggle", floating_toggle
,{0} },
5212 { "focus_main", focus
, {.id
= SWM_ARG_ID_FOCUSMAIN
} },
5213 { "focus_next", focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
5214 { "focus_prev", focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
5215 { "height_grow", resize_step
, {.id
= SWM_ARG_ID_HEIGHTGROW
} },
5216 { "height_shrink", resize_step
, {.id
= SWM_ARG_ID_HEIGHTSHRINK
} },
5217 { "iconify", iconify
, {0} },
5218 { "master_shrink", stack_config
, {.id
= SWM_ARG_ID_MASTERSHRINK
} },
5219 { "master_grow", stack_config
, {.id
= SWM_ARG_ID_MASTERGROW
} },
5220 { "master_add", stack_config
, {.id
= SWM_ARG_ID_MASTERADD
} },
5221 { "master_del", stack_config
, {.id
= SWM_ARG_ID_MASTERDEL
} },
5222 { "move_down", move_step
, {.id
= SWM_ARG_ID_MOVEDOWN
} },
5223 { "move_left", move_step
, {.id
= SWM_ARG_ID_MOVELEFT
} },
5224 { "move_right", move_step
, {.id
= SWM_ARG_ID_MOVERIGHT
} },
5225 { "move_up", move_step
, {.id
= SWM_ARG_ID_MOVEUP
} },
5226 { "mvws_1", send_to_ws
, {.id
= 0} },
5227 { "mvws_2", send_to_ws
, {.id
= 1} },
5228 { "mvws_3", send_to_ws
, {.id
= 2} },
5229 { "mvws_4", send_to_ws
, {.id
= 3} },
5230 { "mvws_5", send_to_ws
, {.id
= 4} },
5231 { "mvws_6", send_to_ws
, {.id
= 5} },
5232 { "mvws_7", send_to_ws
, {.id
= 6} },
5233 { "mvws_8", send_to_ws
, {.id
= 7} },
5234 { "mvws_9", send_to_ws
, {.id
= 8} },
5235 { "mvws_10", send_to_ws
, {.id
= 9} },
5236 { "mvws_11", send_to_ws
, {.id
= 10} },
5237 { "mvws_12", send_to_ws
, {.id
= 11} },
5238 { "mvws_13", send_to_ws
, {.id
= 12} },
5239 { "mvws_14", send_to_ws
, {.id
= 13} },
5240 { "mvws_15", send_to_ws
, {.id
= 14} },
5241 { "mvws_16", send_to_ws
, {.id
= 15} },
5242 { "mvws_17", send_to_ws
, {.id
= 16} },
5243 { "mvws_18", send_to_ws
, {.id
= 17} },
5244 { "mvws_19", send_to_ws
, {.id
= 18} },
5245 { "mvws_20", send_to_ws
, {.id
= 19} },
5246 { "mvws_21", send_to_ws
, {.id
= 20} },
5247 { "mvws_22", send_to_ws
, {.id
= 21} },
5248 { "name_workspace", name_workspace
, {0} },
5249 { "quit", quit
, {0} },
5250 { "raise_toggle", raise_toggle
, {0} },
5251 { "restart", restart
, {0} },
5252 { "screen_next", cyclescr
, {.id
= SWM_ARG_ID_CYCLESC_UP
} },
5253 { "screen_prev", cyclescr
, {.id
= SWM_ARG_ID_CYCLESC_DOWN
} },
5254 { "search_win", search_win
, {0} },
5255 { "search_workspace", search_workspace
, {0} },
5256 { "spawn_custom", NULL
, {0} },
5257 { "stack_inc", stack_config
, {.id
= SWM_ARG_ID_STACKINC
} },
5258 { "stack_dec", stack_config
, {.id
= SWM_ARG_ID_STACKDEC
} },
5259 { "stack_reset", stack_config
, {.id
= SWM_ARG_ID_STACKRESET
} },
5260 { "swap_main", swapwin
, {.id
= SWM_ARG_ID_SWAPMAIN
} },
5261 { "swap_next", swapwin
, {.id
= SWM_ARG_ID_SWAPNEXT
} },
5262 { "swap_prev", swapwin
, {.id
= SWM_ARG_ID_SWAPPREV
} },
5263 { "uniconify", uniconify
, {0} },
5264 { "version", version
, {0} },
5265 { "width_grow", resize_step
, {.id
= SWM_ARG_ID_WIDTHGROW
} },
5266 { "width_shrink", resize_step
, {.id
= SWM_ARG_ID_WIDTHSHRINK
} },
5267 { "wind_del", wkill
, {.id
= SWM_ARG_ID_DELETEWINDOW
} },
5268 { "wind_kill", wkill
, {.id
= SWM_ARG_ID_KILLWINDOW
} },
5269 { "ws_1", switchws
, {.id
= 0} },
5270 { "ws_2", switchws
, {.id
= 1} },
5271 { "ws_3", switchws
, {.id
= 2} },
5272 { "ws_4", switchws
, {.id
= 3} },
5273 { "ws_5", switchws
, {.id
= 4} },
5274 { "ws_6", switchws
, {.id
= 5} },
5275 { "ws_7", switchws
, {.id
= 6} },
5276 { "ws_8", switchws
, {.id
= 7} },
5277 { "ws_9", switchws
, {.id
= 8} },
5278 { "ws_10", switchws
, {.id
= 9} },
5279 { "ws_11", switchws
, {.id
= 10} },
5280 { "ws_12", switchws
, {.id
= 11} },
5281 { "ws_13", switchws
, {.id
= 12} },
5282 { "ws_14", switchws
, {.id
= 13} },
5283 { "ws_15", switchws
, {.id
= 14} },
5284 { "ws_16", switchws
, {.id
= 15} },
5285 { "ws_17", switchws
, {.id
= 16} },
5286 { "ws_18", switchws
, {.id
= 17} },
5287 { "ws_19", switchws
, {.id
= 18} },
5288 { "ws_20", switchws
, {.id
= 19} },
5289 { "ws_21", switchws
, {.id
= 20} },
5290 { "ws_22", switchws
, {.id
= 21} },
5291 { "ws_next", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP
} },
5292 { "ws_next_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP_ALL
} },
5293 { "ws_prev", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN
} },
5294 { "ws_prev_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN_ALL
} },
5295 { "ws_prior", priorws
, {0} },
5296 { "dumpwins", dumpwins
, {0} }, /* MUST BE LAST */
5297 { "invalid key func", NULL
, {0} },
5300 RB_ENTRY(key
) entry
;
5303 enum keyfuncid funcid
;
5306 RB_HEAD(key_tree
, key
);
5309 key_cmp(struct key
*kp1
, struct key
*kp2
)
5311 if (kp1
->keysym
< kp2
->keysym
)
5313 if (kp1
->keysym
> kp2
->keysym
)
5316 if (kp1
->mod
< kp2
->mod
)
5318 if (kp1
->mod
> kp2
->mod
)
5324 RB_GENERATE(key_tree
, key
, entry
, key_cmp
);
5325 struct key_tree keys
;
5328 enum { client_click
, root_click
};
5330 unsigned int action
;
5332 unsigned int button
;
5333 void (*func
)(struct ws_win
*, union arg
*);
5336 #define MODKEY_SHIFT MODKEY | XCB_MOD_MASK_SHIFT
5337 /* action key mouse button func args */
5338 { client_click
, MODKEY
, XCB_BUTTON_INDEX_3
, resize
, {.id
= SWM_ARG_ID_DONTCENTER
} },
5339 { client_click
, MODKEY_SHIFT
, XCB_BUTTON_INDEX_3
, resize
, {.id
= SWM_ARG_ID_CENTER
} },
5340 { client_click
, MODKEY
, XCB_BUTTON_INDEX_1
, move
, {0} },
5345 update_modkey(unsigned int mod
)
5351 RB_FOREACH(kp
, key_tree
, &keys
)
5352 if (kp
->mod
& XCB_MOD_MASK_SHIFT
)
5353 kp
->mod
= mod
| XCB_MOD_MASK_SHIFT
;
5357 for (i
= 0; i
< LENGTH(buttons
); i
++)
5358 if (buttons
[i
].mask
& XCB_MOD_MASK_SHIFT
)
5359 buttons
[i
].mask
= mod
| XCB_MOD_MASK_SHIFT
;
5361 buttons
[i
].mask
= mod
;
5366 TAILQ_ENTRY(spawn_prog
) entry
;
5371 TAILQ_HEAD(spawn_list
, spawn_prog
);
5372 struct spawn_list spawns
= TAILQ_HEAD_INITIALIZER(spawns
);
5375 spawn_expand(struct swm_region
*r
, union arg
*args
, char *spawn_name
,
5378 struct spawn_prog
*prog
= NULL
;
5380 char *ap
, **real_args
;
5382 /* suppress unused warning since var is needed */
5385 DNPRINTF(SWM_D_SPAWN
, "spawn_expand: %s\n", spawn_name
);
5388 TAILQ_FOREACH(prog
, &spawns
, entry
) {
5389 if (!strcasecmp(spawn_name
, prog
->name
))
5393 warnx("spawn_custom: program %s not found", spawn_name
);
5397 /* make room for expanded args */
5398 if ((real_args
= calloc(prog
->argc
+ 1, sizeof(char *))) == NULL
)
5399 err(1, "spawn_custom: calloc real_args");
5401 /* expand spawn_args into real_args */
5402 for (i
= 0; i
< prog
->argc
; i
++) {
5404 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: raw arg: %s\n", ap
);
5405 if (!strcasecmp(ap
, "$bar_border")) {
5407 strdup(r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].name
))
5409 err(1, "spawn_custom border color");
5410 } else if (!strcasecmp(ap
, "$bar_color")) {
5412 strdup(r
->s
->c
[SWM_S_COLOR_BAR
].name
))
5414 err(1, "spawn_custom bar color");
5415 } else if (!strcasecmp(ap
, "$bar_font")) {
5416 if ((real_args
[i
] = strdup(bar_fonts
))
5418 err(1, "spawn_custom bar fonts");
5419 } else if (!strcasecmp(ap
, "$bar_font_color")) {
5421 strdup(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].name
))
5423 err(1, "spawn_custom color font");
5424 } else if (!strcasecmp(ap
, "$color_focus")) {
5426 strdup(r
->s
->c
[SWM_S_COLOR_FOCUS
].name
))
5428 err(1, "spawn_custom color focus");
5429 } else if (!strcasecmp(ap
, "$color_unfocus")) {
5431 strdup(r
->s
->c
[SWM_S_COLOR_UNFOCUS
].name
))
5433 err(1, "spawn_custom color unfocus");
5435 /* no match --> copy as is */
5436 if ((real_args
[i
] = strdup(ap
)) == NULL
)
5437 err(1, "spawn_custom strdup(ap)");
5439 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: cooked arg: %s\n",
5444 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: result: ");
5445 for (i
= 0; i
< prog
->argc
; i
++)
5446 DNPRINTF(SWM_D_SPAWN
, "\"%s\" ", real_args
[i
]);
5447 DNPRINTF(SWM_D_SPAWN
, "\n");
5449 *ret_args
= real_args
;
5450 return (prog
->argc
);
5454 spawn_custom(struct swm_region
*r
, union arg
*args
, char *spawn_name
)
5460 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
5464 spawn(r
->ws
->idx
, &a
, 1);
5466 for (i
= 0; i
< spawn_argc
; i
++)
5472 spawn_select(struct swm_region
*r
, union arg
*args
, char *spawn_name
, int *pid
)
5478 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
5482 if (pipe(select_list_pipe
) == -1)
5483 err(1, "pipe error");
5484 if (pipe(select_resp_pipe
) == -1)
5485 err(1, "pipe error");
5487 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
5488 err(1, "could not disable SIGPIPE");
5489 switch (*pid
= fork()) {
5491 err(1, "cannot fork");
5494 if (dup2(select_list_pipe
[0], 0) == -1)
5496 if (dup2(select_resp_pipe
[1], 1) == -1)
5498 close(select_list_pipe
[1]);
5499 close(select_resp_pipe
[0]);
5500 spawn(r
->ws
->idx
, &a
, 0);
5502 default: /* parent */
5503 close(select_list_pipe
[0]);
5504 close(select_resp_pipe
[1]);
5508 for (i
= 0; i
< spawn_argc
; i
++)
5514 spawn_insert(char *name
, char *args
)
5516 char *arg
, *cp
, *ptr
;
5517 struct spawn_prog
*sp
;
5519 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: %s\n", name
);
5521 if ((sp
= calloc(1, sizeof *sp
)) == NULL
)
5522 err(1, "spawn_insert: calloc");
5523 if ((sp
->name
= strdup(name
)) == NULL
)
5524 err(1, "spawn_insert: strdup");
5526 /* convert the arguments to an argument list */
5527 if ((ptr
= cp
= strdup(args
)) == NULL
)
5528 err(1, "spawn_insert: strdup");
5529 while ((arg
= strsep(&ptr
, " \t")) != NULL
) {
5530 /* empty field; skip it */
5535 if ((sp
->argv
= realloc(sp
->argv
, sp
->argc
*
5536 sizeof *sp
->argv
)) == NULL
)
5537 err(1, "spawn_insert: realloc");
5538 if ((sp
->argv
[sp
->argc
- 1] = strdup(arg
)) == NULL
)
5539 err(1, "spawn_insert: strdup");
5543 TAILQ_INSERT_TAIL(&spawns
, sp
, entry
);
5544 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: leave\n");
5548 spawn_remove(struct spawn_prog
*sp
)
5552 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: %s\n", sp
->name
);
5554 TAILQ_REMOVE(&spawns
, sp
, entry
);
5555 for (i
= 0; i
< sp
->argc
; i
++)
5561 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: leave\n");
5565 spawn_replace(struct spawn_prog
*sp
, char *name
, char *args
)
5567 DNPRINTF(SWM_D_SPAWN
, "spawn_replace: %s [%s]\n", sp
->name
, name
);
5570 spawn_insert(name
, args
);
5572 DNPRINTF(SWM_D_SPAWN
, "spawn_replace: leave\n");
5576 setspawn(char *name
, char *args
)
5578 struct spawn_prog
*sp
;
5580 DNPRINTF(SWM_D_SPAWN
, "setspawn: %s\n", name
);
5585 TAILQ_FOREACH(sp
, &spawns
, entry
) {
5586 if (!strcmp(sp
->name
, name
)) {
5590 spawn_replace(sp
, name
, args
);
5591 DNPRINTF(SWM_D_SPAWN
, "setspawn: leave\n");
5595 if (*args
== '\0') {
5596 warnx("error: setspawn: cannot find program: %s", name
);
5600 spawn_insert(name
, args
);
5601 DNPRINTF(SWM_D_SPAWN
, "setspawn: leave\n");
5605 setconfspawn(char *selector
, char *value
, int flags
)
5609 /* suppress unused warning since var is needed */
5612 args
= expand_tilde(value
);
5614 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: [%s] [%s]\n", selector
, args
);
5616 setspawn(selector
, args
);
5619 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: done.\n");
5626 setconfspawn("term", "xterm", 0);
5627 setconfspawn("spawn_term", "xterm", 0);
5628 setconfspawn("screenshot_all", "screenshot.sh full", 0);
5629 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
5630 setconfspawn("lock", "xlock", 0);
5631 setconfspawn("initscr", "initscreen.sh", 0);
5632 setconfspawn("menu", "dmenu_run"
5635 " -nf $bar_font_color"
5637 " -sf $bar_color", 0);
5638 setconfspawn("search", "dmenu"
5642 " -nf $bar_font_color"
5644 " -sf $bar_color", 0);
5645 setconfspawn("name_workspace", "dmenu"
5649 " -nf $bar_font_color"
5651 " -sf $bar_color", 0);
5655 #define SWM_MODNAME_SIZE 32
5656 #define SWM_KEY_WS "\n+ \t"
5658 parsekeys(char *keystr
, unsigned int currmod
, unsigned int *mod
, KeySym
*ks
)
5662 DNPRINTF(SWM_D_KEY
, "parsekeys: enter [%s]\n", keystr
);
5663 if (mod
== NULL
|| ks
== NULL
) {
5664 DNPRINTF(SWM_D_KEY
, "parsekeys: no mod or key vars\n");
5667 if (keystr
== NULL
|| strlen(keystr
) == 0) {
5668 DNPRINTF(SWM_D_KEY
, "parsekeys: no keystr\n");
5672 *ks
= XCB_NO_SYMBOL
;
5674 while ((name
= strsep(&cp
, SWM_KEY_WS
)) != NULL
) {
5675 DNPRINTF(SWM_D_KEY
, "parsekeys: key [%s]\n", name
);
5677 cp
+= (long)strspn(cp
, SWM_KEY_WS
);
5678 if (strncasecmp(name
, "MOD", SWM_MODNAME_SIZE
) == 0)
5680 else if (!strncasecmp(name
, "Mod1", SWM_MODNAME_SIZE
))
5681 *mod
|= XCB_MOD_MASK_1
;
5682 else if (!strncasecmp(name
, "Mod2", SWM_MODNAME_SIZE
))
5683 *mod
+= XCB_MOD_MASK_2
;
5684 else if (!strncmp(name
, "Mod3", SWM_MODNAME_SIZE
))
5685 *mod
|= XCB_MOD_MASK_3
;
5686 else if (!strncmp(name
, "Mod4", SWM_MODNAME_SIZE
))
5687 *mod
|= XCB_MOD_MASK_4
;
5688 else if (strncasecmp(name
, "SHIFT", SWM_MODNAME_SIZE
) == 0)
5689 *mod
|= XCB_MOD_MASK_SHIFT
;
5690 else if (strncasecmp(name
, "CONTROL", SWM_MODNAME_SIZE
) == 0)
5691 *mod
|= XCB_MOD_MASK_CONTROL
;
5693 *ks
= XStringToKeysym(name
);
5694 XConvertCase(*ks
, ks
, &uks
);
5695 if (ks
== XCB_NO_SYMBOL
) {
5697 "parsekeys: invalid key %s\n",
5703 DNPRINTF(SWM_D_KEY
, "parsekeys: leave ok\n");
5708 strdupsafe(char *str
)
5713 return (strdup(str
));
5717 key_insert(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
, char *spawn_name
)
5721 DNPRINTF(SWM_D_KEY
, "key_insert: enter %s [%s]\n",
5722 keyfuncs
[kfid
].name
, spawn_name
);
5724 if ((kp
= malloc(sizeof *kp
)) == NULL
)
5725 err(1, "key_insert: malloc");
5730 kp
->spawn_name
= strdupsafe(spawn_name
);
5731 RB_INSERT(key_tree
, &keys
, kp
);
5733 DNPRINTF(SWM_D_KEY
, "key_insert: leave\n");
5737 key_lookup(unsigned int mod
, KeySym ks
)
5744 return (RB_FIND(key_tree
, &keys
, &kp
));
5748 key_remove(struct key
*kp
)
5750 DNPRINTF(SWM_D_KEY
, "key_remove: %s\n", keyfuncs
[kp
->funcid
].name
);
5752 RB_REMOVE(key_tree
, &keys
, kp
);
5753 free(kp
->spawn_name
);
5756 DNPRINTF(SWM_D_KEY
, "key_remove: leave\n");
5760 key_replace(struct key
*kp
, unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
5763 DNPRINTF(SWM_D_KEY
, "key_replace: %s [%s]\n", keyfuncs
[kp
->funcid
].name
,
5767 key_insert(mod
, ks
, kfid
, spawn_name
);
5769 DNPRINTF(SWM_D_KEY
, "key_replace: leave\n");
5773 setkeybinding(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
5778 DNPRINTF(SWM_D_KEY
, "setkeybinding: enter %s [%s]\n",
5779 keyfuncs
[kfid
].name
, spawn_name
);
5781 if ((kp
= key_lookup(mod
, ks
)) != NULL
) {
5782 if (kfid
== KF_INVALID
)
5785 key_replace(kp
, mod
, ks
, kfid
, spawn_name
);
5786 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
5789 if (kfid
== KF_INVALID
) {
5790 warnx("error: setkeybinding: cannot find mod/key combination");
5791 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
5795 key_insert(mod
, ks
, kfid
, spawn_name
);
5796 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
5800 setconfbinding(char *selector
, char *value
, int flags
)
5802 enum keyfuncid kfid
;
5805 struct spawn_prog
*sp
;
5807 /* suppress unused warning since var is needed */
5810 DNPRINTF(SWM_D_KEY
, "setconfbinding: enter\n");
5811 if (selector
== NULL
) {
5812 DNPRINTF(SWM_D_KEY
, "setconfbinding: unbind %s\n", value
);
5813 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
5815 setkeybinding(mod
, ks
, kfid
, NULL
);
5820 /* search by key function name */
5821 for (kfid
= 0; kfid
< KF_INVALID
; (kfid
)++) {
5822 if (strncasecmp(selector
, keyfuncs
[kfid
].name
,
5823 SWM_FUNCNAME_LEN
) == 0) {
5824 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match\n",
5826 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
5827 setkeybinding(mod
, ks
, kfid
, NULL
);
5833 /* search by custom spawn name */
5834 TAILQ_FOREACH(sp
, &spawns
, entry
) {
5835 if (strcasecmp(selector
, sp
->name
) == 0) {
5836 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match\n",
5838 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
5839 setkeybinding(mod
, ks
, KF_SPAWN_CUSTOM
,
5846 DNPRINTF(SWM_D_KEY
, "setconfbinding: no match\n");
5853 #define MODKEY_SHIFT MODKEY | XCB_MOD_MASK_SHIFT
5854 setkeybinding(MODKEY
, XK_space
, KF_CYCLE_LAYOUT
,NULL
);
5855 setkeybinding(MODKEY_SHIFT
, XK_backslash
, KF_FLIP_LAYOUT
, NULL
);
5856 setkeybinding(MODKEY_SHIFT
, XK_space
, KF_STACK_RESET
, NULL
);
5857 setkeybinding(MODKEY
, XK_h
, KF_MASTER_SHRINK
, NULL
);
5858 setkeybinding(MODKEY
, XK_l
, KF_MASTER_GROW
, NULL
);
5859 setkeybinding(MODKEY
, XK_comma
, KF_MASTER_ADD
, NULL
);
5860 setkeybinding(MODKEY
, XK_period
, KF_MASTER_DEL
, NULL
);
5861 setkeybinding(MODKEY_SHIFT
, XK_comma
, KF_STACK_INC
, NULL
);
5862 setkeybinding(MODKEY_SHIFT
, XK_period
, KF_STACK_DEC
, NULL
);
5863 setkeybinding(MODKEY
, XK_Return
, KF_SWAP_MAIN
, NULL
);
5864 setkeybinding(MODKEY
, XK_j
, KF_FOCUS_NEXT
, NULL
);
5865 setkeybinding(MODKEY
, XK_k
, KF_FOCUS_PREV
, NULL
);
5866 setkeybinding(MODKEY_SHIFT
, XK_j
, KF_SWAP_NEXT
, NULL
);
5867 setkeybinding(MODKEY_SHIFT
, XK_k
, KF_SWAP_PREV
, NULL
);
5868 setkeybinding(MODKEY_SHIFT
, XK_Return
, KF_SPAWN_CUSTOM
,"term");
5869 setkeybinding(MODKEY
, XK_p
, KF_SPAWN_CUSTOM
,"menu");
5870 setkeybinding(MODKEY_SHIFT
, XK_q
, KF_QUIT
, NULL
);
5871 setkeybinding(MODKEY
, XK_q
, KF_RESTART
, NULL
);
5872 setkeybinding(MODKEY
, XK_m
, KF_FOCUS_MAIN
, NULL
);
5873 setkeybinding(MODKEY
, XK_1
, KF_WS_1
, NULL
);
5874 setkeybinding(MODKEY
, XK_2
, KF_WS_2
, NULL
);
5875 setkeybinding(MODKEY
, XK_3
, KF_WS_3
, NULL
);
5876 setkeybinding(MODKEY
, XK_4
, KF_WS_4
, NULL
);
5877 setkeybinding(MODKEY
, XK_5
, KF_WS_5
, NULL
);
5878 setkeybinding(MODKEY
, XK_6
, KF_WS_6
, NULL
);
5879 setkeybinding(MODKEY
, XK_7
, KF_WS_7
, NULL
);
5880 setkeybinding(MODKEY
, XK_8
, KF_WS_8
, NULL
);
5881 setkeybinding(MODKEY
, XK_9
, KF_WS_9
, NULL
);
5882 setkeybinding(MODKEY
, XK_0
, KF_WS_10
, NULL
);
5883 setkeybinding(MODKEY
, XK_F1
, KF_WS_11
, NULL
);
5884 setkeybinding(MODKEY
, XK_F2
, KF_WS_12
, NULL
);
5885 setkeybinding(MODKEY
, XK_F3
, KF_WS_13
, NULL
);
5886 setkeybinding(MODKEY
, XK_F4
, KF_WS_14
, NULL
);
5887 setkeybinding(MODKEY
, XK_F5
, KF_WS_15
, NULL
);
5888 setkeybinding(MODKEY
, XK_F6
, KF_WS_16
, NULL
);
5889 setkeybinding(MODKEY
, XK_F7
, KF_WS_17
, NULL
);
5890 setkeybinding(MODKEY
, XK_F8
, KF_WS_18
, NULL
);
5891 setkeybinding(MODKEY
, XK_F9
, KF_WS_19
, NULL
);
5892 setkeybinding(MODKEY
, XK_F10
, KF_WS_20
, NULL
);
5893 setkeybinding(MODKEY
, XK_F11
, KF_WS_21
, NULL
);
5894 setkeybinding(MODKEY
, XK_F12
, KF_WS_22
, NULL
);
5895 setkeybinding(MODKEY
, XK_Right
, KF_WS_NEXT
, NULL
);
5896 setkeybinding(MODKEY
, XK_Left
, KF_WS_PREV
, NULL
);
5897 setkeybinding(MODKEY
, XK_Up
, KF_WS_NEXT_ALL
, NULL
);
5898 setkeybinding(MODKEY
, XK_Down
, KF_WS_PREV_ALL
, NULL
);
5899 setkeybinding(MODKEY
, XK_a
, KF_WS_PRIOR
, NULL
);
5900 setkeybinding(MODKEY_SHIFT
, XK_Right
, KF_SCREEN_NEXT
, NULL
);
5901 setkeybinding(MODKEY_SHIFT
, XK_Left
, KF_SCREEN_PREV
, NULL
);
5902 setkeybinding(MODKEY_SHIFT
, XK_1
, KF_MVWS_1
, NULL
);
5903 setkeybinding(MODKEY_SHIFT
, XK_2
, KF_MVWS_2
, NULL
);
5904 setkeybinding(MODKEY_SHIFT
, XK_3
, KF_MVWS_3
, NULL
);
5905 setkeybinding(MODKEY_SHIFT
, XK_4
, KF_MVWS_4
, NULL
);
5906 setkeybinding(MODKEY_SHIFT
, XK_5
, KF_MVWS_5
, NULL
);
5907 setkeybinding(MODKEY_SHIFT
, XK_6
, KF_MVWS_6
, NULL
);
5908 setkeybinding(MODKEY_SHIFT
, XK_7
, KF_MVWS_7
, NULL
);
5909 setkeybinding(MODKEY_SHIFT
, XK_8
, KF_MVWS_8
, NULL
);
5910 setkeybinding(MODKEY_SHIFT
, XK_9
, KF_MVWS_9
, NULL
);
5911 setkeybinding(MODKEY_SHIFT
, XK_0
, KF_MVWS_10
, NULL
);
5912 setkeybinding(MODKEY_SHIFT
, XK_F1
, KF_MVWS_11
, NULL
);
5913 setkeybinding(MODKEY_SHIFT
, XK_F2
, KF_MVWS_12
, NULL
);
5914 setkeybinding(MODKEY_SHIFT
, XK_F3
, KF_MVWS_13
, NULL
);
5915 setkeybinding(MODKEY_SHIFT
, XK_F4
, KF_MVWS_14
, NULL
);
5916 setkeybinding(MODKEY_SHIFT
, XK_F5
, KF_MVWS_15
, NULL
);
5917 setkeybinding(MODKEY_SHIFT
, XK_F6
, KF_MVWS_16
, NULL
);
5918 setkeybinding(MODKEY_SHIFT
, XK_F7
, KF_MVWS_17
, NULL
);
5919 setkeybinding(MODKEY_SHIFT
, XK_F8
, KF_MVWS_18
, NULL
);
5920 setkeybinding(MODKEY_SHIFT
, XK_F9
, KF_MVWS_19
, NULL
);
5921 setkeybinding(MODKEY_SHIFT
, XK_F10
, KF_MVWS_20
, NULL
);
5922 setkeybinding(MODKEY_SHIFT
, XK_F11
, KF_MVWS_21
, NULL
);
5923 setkeybinding(MODKEY_SHIFT
, XK_F12
, KF_MVWS_22
, NULL
);
5924 setkeybinding(MODKEY
, XK_b
, KF_BAR_TOGGLE
, NULL
);
5925 setkeybinding(MODKEY
, XK_Tab
, KF_FOCUS_NEXT
, NULL
);
5926 setkeybinding(MODKEY_SHIFT
, XK_Tab
, KF_FOCUS_PREV
, NULL
);
5927 setkeybinding(MODKEY_SHIFT
, XK_x
, KF_WIND_KILL
, NULL
);
5928 setkeybinding(MODKEY
, XK_x
, KF_WIND_DEL
, NULL
);
5929 setkeybinding(MODKEY
, XK_s
, KF_SPAWN_CUSTOM
,"screenshot_all");
5930 setkeybinding(MODKEY_SHIFT
, XK_s
, KF_SPAWN_CUSTOM
,"screenshot_wind");
5931 setkeybinding(MODKEY
, XK_t
, KF_FLOAT_TOGGLE
,NULL
);
5932 setkeybinding(MODKEY_SHIFT
, XK_v
, KF_VERSION
, NULL
);
5933 setkeybinding(MODKEY_SHIFT
, XK_Delete
, KF_SPAWN_CUSTOM
,"lock");
5934 setkeybinding(MODKEY_SHIFT
, XK_i
, KF_SPAWN_CUSTOM
,"initscr");
5935 setkeybinding(MODKEY
, XK_w
, KF_ICONIFY
, NULL
);
5936 setkeybinding(MODKEY_SHIFT
, XK_w
, KF_UNICONIFY
, NULL
);
5937 setkeybinding(MODKEY_SHIFT
, XK_r
, KF_RAISE_TOGGLE
,NULL
);
5938 setkeybinding(MODKEY
, XK_v
, KF_BUTTON2
, NULL
);
5939 setkeybinding(MODKEY
, XK_equal
, KF_WIDTH_GROW
, NULL
);
5940 setkeybinding(MODKEY
, XK_minus
, KF_WIDTH_SHRINK
,NULL
);
5941 setkeybinding(MODKEY_SHIFT
, XK_equal
, KF_HEIGHT_GROW
,NULL
);
5942 setkeybinding(MODKEY_SHIFT
, XK_minus
, KF_HEIGHT_SHRINK
,NULL
);
5943 setkeybinding(MODKEY
, XK_bracketleft
, KF_MOVE_LEFT
,NULL
);
5944 setkeybinding(MODKEY
, XK_bracketright
,KF_MOVE_RIGHT
,NULL
);
5945 setkeybinding(MODKEY_SHIFT
, XK_bracketleft
, KF_MOVE_UP
, NULL
);
5946 setkeybinding(MODKEY_SHIFT
, XK_bracketright
,KF_MOVE_DOWN
,NULL
);
5947 setkeybinding(MODKEY_SHIFT
, XK_slash
, KF_NAME_WORKSPACE
,NULL
);
5948 setkeybinding(MODKEY
, XK_slash
, KF_SEARCH_WORKSPACE
,NULL
);
5949 setkeybinding(MODKEY
, XK_f
, KF_SEARCH_WIN
, NULL
);
5951 setkeybinding(MODKEY_SHIFT
, XK_d
, KF_DUMPWINS
, NULL
);
5961 while (RB_EMPTY(&keys
) == 0) {
5962 kp
= RB_ROOT(&keys
);
5968 setkeymapping(char *selector
, char *value
, int flags
)
5970 char keymapping_file
[PATH_MAX
];
5972 /* suppress unused warnings since vars are needed */
5976 DNPRINTF(SWM_D_KEY
, "setkeymapping: enter\n");
5977 if (value
[0] == '~')
5978 snprintf(keymapping_file
, sizeof keymapping_file
, "%s/%s",
5979 pwd
->pw_dir
, &value
[1]);
5981 strlcpy(keymapping_file
, value
, sizeof keymapping_file
);
5983 /* load new key bindings; if it fails, revert to default bindings */
5984 if (conf_load(keymapping_file
, SWM_CONF_KEYMAPPING
)) {
5988 DNPRINTF(SWM_D_KEY
, "setkeymapping: leave\n");
5993 updatenumlockmask(void)
5996 xcb_get_modifier_mapping_reply_t
*modmap_r
;
5997 xcb_keycode_t
*modmap
, kc
, *keycode
;
6001 modmap_r
= xcb_get_modifier_mapping_reply(conn
,
6002 xcb_get_modifier_mapping(conn
),
6005 modmap
= xcb_get_modifier_mapping_keycodes(modmap_r
);
6006 for (i
= 0; i
< 8; i
++) {
6007 for (j
= 0; j
< modmap_r
->keycodes_per_modifier
; j
++) {
6008 kc
= modmap
[i
* modmap_r
->keycodes_per_modifier
6010 keycode
= xcb_key_symbols_get_keycode(syms
,
6013 numlockmask
= (1 << i
);
6019 DNPRINTF(SWM_D_MISC
, "updatenumlockmask: %d\n", numlockmask
);
6026 int num_screens
, k
, j
;
6027 unsigned int modifiers
[3];
6028 xcb_keycode_t
*code
;
6030 DNPRINTF(SWM_D_MISC
, "grabkeys\n");
6031 updatenumlockmask();
6034 modifiers
[1] = numlockmask
;
6035 modifiers
[2] = numlockmask
| XCB_MOD_MASK_LOCK
;
6037 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
6038 for (k
= 0; k
< num_screens
; k
++) {
6039 if (TAILQ_EMPTY(&screens
[k
].rl
))
6041 xcb_ungrab_key(conn
, XCB_GRAB_ANY
, screens
[k
].root
,
6043 RB_FOREACH(kp
, key_tree
, &keys
) {
6044 if ((code
= xcb_key_symbols_get_keycode(syms
,
6046 for (j
= 0; j
< LENGTH(modifiers
); j
++)
6047 xcb_grab_key(conn
, 1,
6049 kp
->mod
| modifiers
[j
],
6050 *code
, XCB_GRAB_MODE_SYNC
,
6051 XCB_GRAB_MODE_ASYNC
);
6059 grabbuttons(struct ws_win
*win
)
6063 DNPRINTF(SWM_D_MOUSE
, "grabbuttons: win 0x%x\n", win
->id
);
6065 for (i
= 0; i
< LENGTH(buttons
); i
++)
6066 if (buttons
[i
].action
== client_click
)
6067 xcb_grab_button(conn
, 0, win
->id
, BUTTONMASK
,
6068 XCB_GRAB_MODE_SYNC
, XCB_GRAB_MODE_ASYNC
,
6069 XCB_WINDOW_NONE
, XCB_CURSOR_NONE
,
6070 buttons
[i
].button
, buttons
[i
].mask
);
6073 const char *quirkname
[] = {
6074 "NONE", /* config string for "no value" */
6083 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
6084 #define SWM_Q_WS "\n|+ \t"
6086 parsequirks(char *qstr
, unsigned long *quirk
)
6096 while ((name
= strsep(&cp
, SWM_Q_WS
)) != NULL
) {
6098 cp
+= (long)strspn(cp
, SWM_Q_WS
);
6099 for (i
= 0; i
< LENGTH(quirkname
); i
++) {
6100 if (!strncasecmp(name
, quirkname
[i
], SWM_QUIRK_LEN
)) {
6101 DNPRINTF(SWM_D_QUIRK
,
6102 "parsequirks: %s\n", name
);
6107 *quirk
|= 1 << (i
-1);
6111 if (i
>= LENGTH(quirkname
)) {
6112 DNPRINTF(SWM_D_QUIRK
,
6113 "parsequirks: invalid quirk [%s]\n", name
);
6121 quirk_insert(const char *class, const char *name
, unsigned long quirk
)
6125 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: %s:%s [%lu]\n", class, name
,
6128 if ((qp
= malloc(sizeof *qp
)) == NULL
)
6129 err(1, "quirk_insert: malloc");
6130 if ((qp
->class = strdup(class)) == NULL
)
6131 err(1, "quirk_insert: strdup");
6132 if ((qp
->name
= strdup(name
)) == NULL
)
6133 err(1, "quirk_insert: strdup");
6136 TAILQ_INSERT_TAIL(&quirks
, qp
, entry
);
6138 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: leave\n");
6142 quirk_remove(struct quirk
*qp
)
6144 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: %s:%s [%lu]\n", qp
->class,
6145 qp
->name
, qp
->quirk
);
6147 TAILQ_REMOVE(&quirks
, qp
, entry
);
6152 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: leave\n");
6156 quirk_replace(struct quirk
*qp
, const char *class, const char *name
,
6157 unsigned long quirk
)
6159 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: %s:%s [%lu]\n", qp
->class,
6160 qp
->name
, qp
->quirk
);
6163 quirk_insert(class, name
, quirk
);
6165 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: leave\n");
6169 setquirk(const char *class, const char *name
, unsigned long quirk
)
6173 DNPRINTF(SWM_D_QUIRK
, "setquirk: enter %s:%s [%lu]\n", class, name
,
6176 /* Remove/replace existing quirk. */
6177 TAILQ_FOREACH(qp
, &quirks
, entry
) {
6178 if (!strcmp(qp
->class, class) && !strcmp(qp
->name
, name
)) {
6182 quirk_replace(qp
, class, name
, quirk
);
6183 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
6188 /* Only insert if quirk is not NONE. */
6190 quirk_insert(class, name
, quirk
);
6192 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
6196 setconfquirk(char *selector
, char *value
, int flags
)
6198 char *cp
, *class, *name
;
6202 /* suppress unused warning since var is needed */
6205 if (selector
== NULL
)
6207 if ((cp
= strchr(selector
, ':')) == NULL
)
6212 if ((retval
= parsequirks(value
, &qrks
)) == 0)
6213 setquirk(class, name
, qrks
);
6220 setquirk("MPlayer", "xv", SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
| SWM_Q_FOCUSPREV
);
6221 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT
);
6222 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ
);
6223 setquirk("Firefox", "Dialog", SWM_Q_FLOAT
);
6224 setquirk("Gimp", "gimp", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
6225 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ
);
6226 setquirk("xine", "Xine Window", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
6227 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
6228 setquirk("xine", "xine Panel", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
6229 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
6230 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN
| SWM_Q_FLOAT
);
6231 setquirk("pcb", "pcb", SWM_Q_FLOAT
);
6232 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
);
6235 /* conf file stuff */
6236 #define SWM_CONF_FILE "spectrwm.conf"
6237 #define SWM_CONF_FILE_OLD "scrotwm.conf"
6241 SWM_S_BAR_AT_BOTTOM
,
6242 SWM_S_BAR_BORDER_WIDTH
,
6249 SWM_S_CLOCK_ENABLED
,
6252 SWM_S_CYCLE_VISIBLE
,
6254 SWM_S_DISABLE_BORDER
,
6256 SWM_S_FOCUS_CLOSE_WRAP
,
6257 SWM_S_FOCUS_DEFAULT
,
6263 SWM_S_STACK_ENABLED
,
6265 SWM_S_TITLE_CLASS_ENABLED
,
6266 SWM_S_TITLE_NAME_ENABLED
,
6267 SWM_S_URGENT_ENABLED
,
6268 SWM_S_VERBOSE_LAYOUT
,
6269 SWM_S_WINDOW_NAME_ENABLED
,
6270 SWM_S_WORKSPACE_LIMIT
6274 setconfvalue(char *selector
, char *value
, int flags
)
6279 /* suppress unused warning since var is needed */
6283 case SWM_S_BAR_ACTION
:
6285 if ((bar_argv
[0] = expand_tilde(value
)) == NULL
)
6286 err(1, "setconfvalue: bar_action");
6288 case SWM_S_BAR_AT_BOTTOM
:
6289 bar_at_bottom
= atoi(value
);
6291 case SWM_S_BAR_BORDER_WIDTH
:
6292 bar_border_width
= atoi(value
);
6293 if (bar_border_width
< 0)
6294 bar_border_width
= 0;
6296 case SWM_S_BAR_DELAY
:
6297 bar_delay
= atoi(value
);
6299 case SWM_S_BAR_ENABLED
:
6300 bar_enabled
= atoi(value
);
6302 case SWM_S_BAR_FONT
:
6304 if (asprintf(&bar_fonts
, "%s,%s", value
, bar_fonts
) == -1)
6305 err(1, "setconfvalue: asprintf: failed to allocate "
6306 "memory for bar_fonts.");
6309 /* If already in xft mode, then we are done. */
6310 if (!bar_font_legacy
)
6313 /* If there are any non-XLFD entries, switch to Xft mode. */
6314 while ((b
= strsep(&value
, ",")) != NULL
) {
6318 bar_font_legacy
= 0;
6323 case SWM_S_BAR_FORMAT
:
6325 if ((bar_format
= strdup(value
)) == NULL
)
6326 err(1, "setconfvalue: bar_format");
6328 case SWM_S_BAR_JUSTIFY
:
6329 if (!strcmp(value
, "left"))
6330 bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
6331 else if (!strcmp(value
, "center"))
6332 bar_justify
= SWM_BAR_JUSTIFY_CENTER
;
6333 else if (!strcmp(value
, "right"))
6334 bar_justify
= SWM_BAR_JUSTIFY_RIGHT
;
6336 errx(1, "invalid bar_justify");
6338 case SWM_S_BORDER_WIDTH
:
6339 border_width
= atoi(value
);
6340 if (border_width
< 0)
6343 case SWM_S_CLOCK_ENABLED
:
6344 clock_enabled
= atoi(value
);
6346 case SWM_S_CLOCK_FORMAT
:
6347 #ifndef SWM_DENY_CLOCK_FORMAT
6349 if ((clock_format
= strdup(value
)) == NULL
)
6350 err(1, "setconfvalue: clock_format");
6353 case SWM_S_CYCLE_EMPTY
:
6354 cycle_empty
= atoi(value
);
6356 case SWM_S_CYCLE_VISIBLE
:
6357 cycle_visible
= atoi(value
);
6359 case SWM_S_DIALOG_RATIO
:
6360 dialog_ratio
= atof(value
);
6361 if (dialog_ratio
> 1.0 || dialog_ratio
<= .3)
6364 case SWM_S_DISABLE_BORDER
:
6365 disable_border
= atoi(value
);
6367 case SWM_S_FOCUS_CLOSE
:
6368 if (!strcmp(value
, "first"))
6369 focus_close
= SWM_STACK_BOTTOM
;
6370 else if (!strcmp(value
, "last"))
6371 focus_close
= SWM_STACK_TOP
;
6372 else if (!strcmp(value
, "next"))
6373 focus_close
= SWM_STACK_ABOVE
;
6374 else if (!strcmp(value
, "previous"))
6375 focus_close
= SWM_STACK_BELOW
;
6377 errx(1, "focus_close");
6379 case SWM_S_FOCUS_CLOSE_WRAP
:
6380 focus_close_wrap
= atoi(value
);
6382 case SWM_S_FOCUS_DEFAULT
:
6383 if (!strcmp(value
, "last"))
6384 focus_default
= SWM_STACK_TOP
;
6385 else if (!strcmp(value
, "first"))
6386 focus_default
= SWM_STACK_BOTTOM
;
6388 errx(1, "focus_default");
6390 case SWM_S_FOCUS_MODE
:
6391 if (!strcmp(value
, "default"))
6392 focus_mode
= SWM_FOCUS_DEFAULT
;
6393 else if (!strcmp(value
, "follow") ||
6394 !strcmp(value
, "follow_cursor"))
6395 focus_mode
= SWM_FOCUS_FOLLOW
;
6396 else if (!strcmp(value
, "manual"))
6397 focus_mode
= SWM_FOCUS_MANUAL
;
6399 errx(1, "focus_mode");
6401 case SWM_S_SPAWN_ORDER
:
6402 if (!strcmp(value
, "first"))
6403 spawn_position
= SWM_STACK_BOTTOM
;
6404 else if (!strcmp(value
, "last"))
6405 spawn_position
= SWM_STACK_TOP
;
6406 else if (!strcmp(value
, "next"))
6407 spawn_position
= SWM_STACK_ABOVE
;
6408 else if (!strcmp(value
, "previous"))
6409 spawn_position
= SWM_STACK_BELOW
;
6411 errx(1, "spawn_position");
6413 case SWM_S_SPAWN_TERM
:
6414 setconfspawn("term", value
, 0);
6415 setconfspawn("spawn_term", value
, 0);
6419 case SWM_S_SS_ENABLED
:
6420 ss_enabled
= atoi(value
);
6422 case SWM_S_STACK_ENABLED
:
6423 stack_enabled
= atoi(value
);
6425 case SWM_S_TERM_WIDTH
:
6426 term_width
= atoi(value
);
6430 case SWM_S_TITLE_CLASS_ENABLED
:
6431 title_class_enabled
= atoi(value
);
6433 case SWM_S_TITLE_NAME_ENABLED
:
6434 title_name_enabled
= atoi(value
);
6436 case SWM_S_URGENT_ENABLED
:
6437 urgent_enabled
= atoi(value
);
6439 case SWM_S_VERBOSE_LAYOUT
:
6440 verbose_layout
= atoi(value
);
6441 for (i
= 0; layouts
[i
].l_stack
!= NULL
; i
++) {
6443 layouts
[i
].l_string
= fancy_stacker
;
6445 layouts
[i
].l_string
= plain_stacker
;
6448 case SWM_S_WINDOW_NAME_ENABLED
:
6449 window_name_enabled
= atoi(value
);
6451 case SWM_S_WORKSPACE_LIMIT
:
6452 workspace_limit
= atoi(value
);
6453 if (workspace_limit
> SWM_WS_MAX
)
6454 workspace_limit
= SWM_WS_MAX
;
6455 else if (workspace_limit
< 1)
6456 workspace_limit
= 1;
6465 setconfmodkey(char *selector
, char *value
, int flags
)
6467 /* suppress unused warnings since vars are needed */
6471 if (!strncasecmp(value
, "Mod1", strlen("Mod1")))
6472 update_modkey(XCB_MOD_MASK_1
);
6473 else if (!strncasecmp(value
, "Mod2", strlen("Mod2")))
6474 update_modkey(XCB_MOD_MASK_2
);
6475 else if (!strncasecmp(value
, "Mod3", strlen("Mod3")))
6476 update_modkey(XCB_MOD_MASK_3
);
6477 else if (!strncasecmp(value
, "Mod4", strlen("Mod4")))
6478 update_modkey(XCB_MOD_MASK_4
);
6485 setconfcolor(char *selector
, char *value
, int flags
)
6487 setscreencolor(value
, ((selector
== NULL
)?-1:atoi(selector
)), flags
);
6492 setconfregion(char *selector
, char *value
, int flags
)
6494 /* suppress unused warnings since vars are needed */
6498 custom_region(value
);
6503 setautorun(char *selector
, char *value
, int flags
)
6513 /* suppress unused warnings since vars are needed */
6517 if (getenv("SWM_STARTED"))
6521 if (sscanf(value
, "ws[%d]:%1023c", &ws_id
, s
) != 2)
6522 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
6524 if (ws_id
< 0 || ws_id
>= workspace_limit
)
6525 errx(1, "autorun: invalid workspace %d", ws_id
+ 1);
6527 sp
= expand_tilde((char *)&s
);
6530 * This is a little intricate
6532 * If the pid already exists we simply reuse it because it means it was
6533 * used before AND not claimed by manage_window. We get away with
6534 * altering it in the parent after INSERT because this can not be a race
6537 while ((ap
= strsep(&sp
, " \t")) != NULL
) {
6540 DNPRINTF(SWM_D_SPAWN
, "setautorun: arg [%s]\n", ap
);
6542 if ((a
.argv
= realloc(a
.argv
, argc
* sizeof(char *))) == NULL
)
6543 err(1, "setautorun: realloc");
6544 a
.argv
[argc
- 1] = ap
;
6548 if ((a
.argv
= realloc(a
.argv
, (argc
+ 1) * sizeof(char *))) == NULL
)
6549 err(1, "setautorun: realloc");
6550 a
.argv
[argc
] = NULL
;
6552 if ((pid
= fork()) == 0) {
6553 spawn(ws_id
, &a
, 1);
6562 p
= calloc(1, sizeof *p
);
6565 TAILQ_INSERT_TAIL(&pidlist
, p
, entry
);
6575 setlayout(char *selector
, char *value
, int flags
)
6577 int ws_id
, i
, x
, mg
, ma
, si
, ar
, f
= 0;
6578 int st
= SWM_V_STACK
, num_screens
;
6580 struct workspace
*ws
;
6582 /* suppress unused warnings since vars are needed */
6586 if (getenv("SWM_STARTED"))
6590 if (sscanf(value
, "ws[%d]:%d:%d:%d:%d:%1023c",
6591 &ws_id
, &mg
, &ma
, &si
, &ar
, s
) != 6)
6592 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
6593 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
6596 if (ws_id
< 0 || ws_id
>= workspace_limit
)
6597 errx(1, "layout: invalid workspace %d", ws_id
+ 1);
6599 if (!strcasecmp(s
, "vertical"))
6601 else if (!strcasecmp(s
, "vertical_flip")) {
6604 } else if (!strcasecmp(s
, "horizontal"))
6606 else if (!strcasecmp(s
, "horizontal_flip")) {
6609 } else if (!strcasecmp(s
, "fullscreen"))
6612 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
6613 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
6616 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
6617 for (i
= 0; i
< num_screens
; i
++) {
6618 ws
= (struct workspace
*)&screens
[i
].ws
;
6619 ws
[ws_id
].cur_layout
= &layouts
[st
];
6621 ws
[ws_id
].always_raise
= ar
;
6622 if (st
== SWM_MAX_STACK
)
6626 for (x
= 0; x
< abs(mg
); x
++) {
6627 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
6628 mg
>= 0 ? SWM_ARG_ID_MASTERGROW
:
6629 SWM_ARG_ID_MASTERSHRINK
);
6633 for (x
= 0; x
< abs(ma
); x
++) {
6634 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
6635 ma
>= 0 ? SWM_ARG_ID_MASTERADD
:
6636 SWM_ARG_ID_MASTERDEL
);
6640 for (x
= 0; x
< abs(si
); x
++) {
6641 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
6642 si
>= 0 ? SWM_ARG_ID_STACKINC
:
6643 SWM_ARG_ID_STACKDEC
);
6648 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
6649 SWM_ARG_ID_FLIPLAYOUT
);
6659 /* config options */
6660 struct config_option
{
6662 int (*func
)(char*, char*, int);
6665 struct config_option configopt
[] = {
6666 { "bar_enabled", setconfvalue
, SWM_S_BAR_ENABLED
},
6667 { "bar_at_bottom", setconfvalue
, SWM_S_BAR_AT_BOTTOM
},
6668 { "bar_border", setconfcolor
, SWM_S_COLOR_BAR_BORDER
},
6669 { "bar_border_width", setconfvalue
, SWM_S_BAR_BORDER_WIDTH
},
6670 { "bar_color", setconfcolor
, SWM_S_COLOR_BAR
},
6671 { "bar_font_color", setconfcolor
, SWM_S_COLOR_BAR_FONT
},
6672 { "bar_font", setconfvalue
, SWM_S_BAR_FONT
},
6673 { "bar_action", setconfvalue
, SWM_S_BAR_ACTION
},
6674 { "bar_delay", setconfvalue
, SWM_S_BAR_DELAY
},
6675 { "bar_justify", setconfvalue
, SWM_S_BAR_JUSTIFY
},
6676 { "bar_format", setconfvalue
, SWM_S_BAR_FORMAT
},
6677 { "keyboard_mapping", setkeymapping
, 0 },
6678 { "bind", setconfbinding
, 0 },
6679 { "stack_enabled", setconfvalue
, SWM_S_STACK_ENABLED
},
6680 { "clock_enabled", setconfvalue
, SWM_S_CLOCK_ENABLED
},
6681 { "clock_format", setconfvalue
, SWM_S_CLOCK_FORMAT
},
6682 { "color_focus", setconfcolor
, SWM_S_COLOR_FOCUS
},
6683 { "color_unfocus", setconfcolor
, SWM_S_COLOR_UNFOCUS
},
6684 { "cycle_empty", setconfvalue
, SWM_S_CYCLE_EMPTY
},
6685 { "cycle_visible", setconfvalue
, SWM_S_CYCLE_VISIBLE
},
6686 { "workspace_limit", setconfvalue
, SWM_S_WORKSPACE_LIMIT
},
6687 { "dialog_ratio", setconfvalue
, SWM_S_DIALOG_RATIO
},
6688 { "verbose_layout", setconfvalue
, SWM_S_VERBOSE_LAYOUT
},
6689 { "modkey", setconfmodkey
, 0 },
6690 { "program", setconfspawn
, 0 },
6691 { "quirk", setconfquirk
, 0 },
6692 { "region", setconfregion
, 0 },
6693 { "spawn_term", setconfvalue
, SWM_S_SPAWN_TERM
},
6694 { "screenshot_enabled", setconfvalue
, SWM_S_SS_ENABLED
},
6695 { "screenshot_app", setconfvalue
, SWM_S_SS_APP
},
6696 { "window_name_enabled", setconfvalue
, SWM_S_WINDOW_NAME_ENABLED
},
6697 { "urgent_enabled", setconfvalue
, SWM_S_URGENT_ENABLED
},
6698 { "term_width", setconfvalue
, SWM_S_TERM_WIDTH
},
6699 { "title_class_enabled", setconfvalue
, SWM_S_TITLE_CLASS_ENABLED
},
6700 { "title_name_enabled", setconfvalue
, SWM_S_TITLE_NAME_ENABLED
},
6701 { "focus_mode", setconfvalue
, SWM_S_FOCUS_MODE
},
6702 { "focus_close", setconfvalue
, SWM_S_FOCUS_CLOSE
},
6703 { "focus_close_wrap", setconfvalue
, SWM_S_FOCUS_CLOSE_WRAP
},
6704 { "focus_default", setconfvalue
, SWM_S_FOCUS_DEFAULT
},
6705 { "spawn_position", setconfvalue
, SWM_S_SPAWN_ORDER
},
6706 { "disable_border", setconfvalue
, SWM_S_DISABLE_BORDER
},
6707 { "border_width", setconfvalue
, SWM_S_BORDER_WIDTH
},
6708 { "autorun", setautorun
, 0 },
6709 { "layout", setlayout
, 0 },
6713 conf_load(char *filename
, int keymapping
)
6716 char *line
, *cp
, *optsub
, *optval
;
6717 size_t linelen
, lineno
= 0;
6718 int wordlen
, i
, optidx
;
6719 struct config_option
*opt
= NULL
;
6721 DNPRINTF(SWM_D_CONF
, "conf_load: begin\n");
6723 if (filename
== NULL
) {
6724 warnx("conf_load: no filename");
6727 if ((config
= fopen(filename
, "r")) == NULL
) {
6728 warn("conf_load: fopen: %s", filename
);
6732 while (!feof(config
)) {
6733 if ((line
= fparseln(config
, &linelen
, &lineno
, NULL
, 0))
6736 err(1, "%s", filename
);
6741 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
6742 if (cp
[0] == '\0') {
6747 /* get config option */
6748 wordlen
= strcspn(cp
, "=[ \t\n");
6750 warnx("%s: line %zd: no option found",
6755 for (i
= 0; i
< LENGTH(configopt
); i
++) {
6756 opt
= &configopt
[i
];
6757 if (!strncasecmp(cp
, opt
->optname
, wordlen
) &&
6758 (int)strlen(opt
->optname
) == wordlen
) {
6764 warnx("%s: line %zd: unknown option %.*s",
6765 filename
, lineno
, wordlen
, cp
);
6768 if (keymapping
&& opt
&& strcmp(opt
->optname
, "bind")) {
6769 warnx("%s: line %zd: invalid option %.*s",
6770 filename
, lineno
, wordlen
, cp
);
6774 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
6775 /* get [selector] if any */
6779 wordlen
= strcspn(cp
, "]");
6782 warnx("%s: line %zd: syntax error",
6787 if (asprintf(&optsub
, "%.*s", wordlen
, cp
) ==
6789 warnx("%s: line %zd: unable to allocate"
6790 "memory for selector", filename
,
6796 cp
+= strspn(cp
, "] \t\n"); /* eat trailing */
6798 cp
+= strspn(cp
, "= \t\n"); /* eat trailing */
6800 optval
= strdup(cp
);
6801 /* call function to deal with it all */
6802 if (configopt
[optidx
].func(optsub
, optval
,
6803 configopt
[optidx
].funcflags
) != 0)
6804 errx(1, "%s: line %zd: invalid data for %s",
6805 filename
, lineno
, configopt
[optidx
].optname
);
6812 DNPRINTF(SWM_D_CONF
, "conf_load: end\n");
6819 DNPRINTF(SWM_D_CONF
, "conf_load: end with error.\n");
6825 set_child_transient(struct ws_win
*win
, xcb_window_t
*trans
)
6827 struct ws_win
*parent
, *w
;
6828 struct swm_region
*r
;
6829 struct workspace
*ws
;
6830 xcb_icccm_wm_hints_t wmh
;
6832 parent
= find_window(win
->transient
);
6834 parent
->focus_child
= win
;
6836 DNPRINTF(SWM_D_MISC
, "set_child_transient: parent doesn't exist"
6837 " for 0x%x trans 0x%x\n", win
->id
, win
->transient
);
6839 r
= root_to_region(win
->wa
->root
, SWM_CK_ALL
);
6841 /* parent doen't exist in our window list */
6842 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
6843 if (xcb_icccm_get_wm_hints_reply(conn
,
6844 xcb_icccm_get_wm_hints(conn
, w
->id
),
6846 warnx("can't get hints for 0x%x", w
->id
);
6850 if (win
->hints
.window_group
!= wmh
.window_group
)
6853 w
->focus_child
= win
;
6854 win
->transient
= w
->id
;
6856 DNPRINTF(SWM_D_MISC
, "set_child_transient: adjusting "
6857 "transient to 0x%x\n", win
->transient
);
6864 window_get_pid(xcb_window_t win
)
6869 xcb_get_property_cookie_t pc
;
6870 xcb_get_property_reply_t
*pr
;
6872 apid
= get_atom_from_string("_NET_WM_PID");
6873 if (apid
== XCB_ATOM_NONE
)
6876 pc
= xcb_get_property(conn
, 0, win
, apid
, XCB_ATOM_CARDINAL
, 0, 1);
6877 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
6880 if (pr
->type
!= XCB_ATOM_CARDINAL
) {
6885 if (pr
->type
== apid
&& pr
->format
== 32)
6886 ret
= *((pid_t
*)xcb_get_property_value(pr
));
6892 apid
= get_atom_from_string("_SWM_PID");
6893 pc
= xcb_get_property(conn
, 0, win
, apid
, XCB_ATOM_STRING
,
6895 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
6898 if (pr
->type
!= apid
) {
6903 ret
= (pid_t
)strtonum(xcb_get_property_value(pr
), 0, INT_MAX
, &errstr
);
6910 get_ws_idx(xcb_window_t id
)
6916 xcb_get_property_reply_t
*gpr
;
6918 gpr
= xcb_get_property_reply(conn
,
6919 xcb_get_property(conn
, 0, id
, a_swm_ws
,
6920 XCB_ATOM_STRING
, 0, SWM_PROPLEN
),
6925 proplen
= xcb_get_property_value_length(gpr
);
6927 prop
= malloc(proplen
+ 1);
6930 xcb_get_property_value(gpr
),
6932 prop
[proplen
] = '\0';
6939 DNPRINTF(SWM_D_PROP
, "get_ws_idx: _SWM_WS: %s\n", prop
);
6940 ws_idx
= (int)strtonum(prop
, 0, workspace_limit
- 1, &errstr
);
6942 DNPRINTF(SWM_D_PROP
, "get_ws_idx: window: #%s: %s",
6952 manage_window(xcb_window_t id
, uint16_t mapped
)
6954 xcb_window_t trans
= XCB_WINDOW_NONE
;
6955 struct ws_win
*win
, *ww
;
6957 char ws_idx_str
[SWM_PROPLEN
];
6958 struct swm_region
*r
;
6962 xcb_icccm_get_wm_protocols_reply_t wpr
;
6964 if ((win
= find_window(id
)) != NULL
) {
6965 DNPRINTF(SWM_D_MISC
, "manage_window: win 0x%x already "
6966 "managed; skipping.)\n", id
);
6967 return (win
); /* Already managed. */
6970 /* See if window is on the unmanaged list. */
6971 if ((win
= find_unmanaged_window(id
)) != NULL
) {
6972 DNPRINTF(SWM_D_MISC
, "manage_window: win 0x%x found on "
6973 "unmanaged list.\n", id
);
6974 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
6977 set_child_transient(win
, &trans
);
6981 DNPRINTF(SWM_D_MISC
, "manage_window: win 0x%x is new.\n", id
);
6984 /* Create and initialize ws_win object. */
6985 if ((win
= calloc(1, sizeof(struct ws_win
))) == NULL
)
6986 err(1, "manage_window: calloc: failed to allocate memory for "
6991 /* Get window geometry. */
6992 win
->wa
= xcb_get_geometry_reply(conn
,
6993 xcb_get_geometry(conn
, win
->id
),
6996 /* Figure out which region the window belongs to. */
6997 r
= root_to_region(win
->wa
->root
, SWM_CK_ALL
);
6999 /* Ignore window border if there is one. */
7000 WIDTH(win
) = win
->wa
->width
;
7001 HEIGHT(win
) = win
->wa
->height
;
7002 X(win
) = win
->wa
->x
+ win
->wa
->border_width
- border_width
;
7003 Y(win
) = win
->wa
->y
+ win
->wa
->border_width
- border_width
;
7005 win
->mapped
= mapped
;
7006 win
->floatmaxed
= 0;
7007 win
->ewmh_flags
= 0;
7008 win
->s
= r
->s
; /* this never changes */
7010 store_float_geom(win
, r
);
7012 /* Get WM_SIZE_HINTS. */
7013 xcb_icccm_get_wm_normal_hints_reply(conn
,
7014 xcb_icccm_get_wm_normal_hints(conn
, win
->id
),
7018 xcb_icccm_get_wm_hints_reply(conn
,
7019 xcb_icccm_get_wm_hints(conn
, win
->id
),
7022 /* Get WM_TRANSIENT_FOR; see if window is a transient. */
7023 xcb_icccm_get_wm_transient_for_reply(conn
,
7024 xcb_icccm_get_wm_transient_for(conn
, win
->id
),
7027 win
->transient
= trans
;
7028 set_child_transient(win
, &win
->transient
);
7031 /* Get supported protocols. */
7032 if (xcb_icccm_get_wm_protocols_reply(conn
,
7033 xcb_icccm_get_wm_protocols(conn
, win
->id
, a_prot
),
7035 for (i
= 0; i
< wpr
.atoms_len
; i
++) {
7036 if (wpr
.atoms
[i
] == a_takefocus
)
7037 win
->take_focus
= 1;
7038 if (wpr
.atoms
[i
] == a_delete
)
7039 win
->can_delete
= 1;
7041 xcb_icccm_get_wm_protocols_reply_wipe(&wpr
);
7044 win
->iconic
= get_swm_iconic(win
);
7046 /* Figure out which workspace the window belongs to. */
7047 if ((p
= find_pid(window_get_pid(win
->id
))) != NULL
) {
7048 win
->ws
= &r
->s
->ws
[p
->ws
];
7049 TAILQ_REMOVE(&pidlist
, p
, entry
);
7052 } else if ((ws_idx
= get_ws_idx(win
->id
)) != -1 &&
7054 /* _SWM_WS is set; use that. */
7055 win
->ws
= &r
->s
->ws
[ws_idx
];
7056 } else if (trans
&& (ww
= find_window(trans
)) != NULL
) {
7057 /* Launch transients in the same ws as parent. */
7063 /* Set the _SWM_WS atom so we can remember this after reincarnation. */
7064 if (snprintf(ws_idx_str
, SWM_PROPLEN
, "%d", win
->ws
->idx
) <
7066 DNPRINTF(SWM_D_PROP
, "manage_window: set _SWM_WS: %s\n",
7068 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
7069 a_swm_ws
, XCB_ATOM_STRING
, 8, strlen(ws_idx_str
),
7074 ewmh_autoquirk(win
);
7076 /* Determine initial quirks. */
7077 if (xcb_icccm_get_wm_class_reply(conn
,
7078 xcb_icccm_get_wm_class(conn
, win
->id
),
7080 DNPRINTF(SWM_D_CLASS
, "manage_window: class: %s, name: %s\n",
7081 win
->ch
.class_name
, win
->ch
.instance_name
);
7083 /* java is retarded so treat it special */
7084 if (strstr(win
->ch
.instance_name
, "sun-awt")) {
7085 DNPRINTF(SWM_D_CLASS
, "manage_window: java window "
7090 TAILQ_FOREACH(qp
, &quirks
, entry
) {
7091 if (!strcmp(win
->ch
.class_name
, qp
->class) &&
7092 !strcmp(win
->ch
.instance_name
, qp
->name
)) {
7093 DNPRINTF(SWM_D_CLASS
, "manage_window: on quirks"
7094 "list; mask: 0x%lx\n", qp
->quirk
);
7095 if (qp
->quirk
& SWM_Q_FLOAT
)
7097 win
->quirks
= qp
->quirk
;
7102 /* Alter window position if quirky */
7103 if (win
->quirks
& SWM_Q_ANYWHERE
)
7106 /* Reset font sizes (the bruteforce way; no default keybinding). */
7107 if (win
->quirks
& SWM_Q_XTERM_FONTADJ
) {
7108 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
7109 fake_keypress(win
, XK_KP_Subtract
, XCB_MOD_MASK_SHIFT
);
7110 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
7111 fake_keypress(win
, XK_KP_Add
, XCB_MOD_MASK_SHIFT
);
7114 /* Make sure window is positioned inside its region, if its active. */
7116 constrain_window(win
, win
->ws
->r
, 0);
7120 /* Select which X events to monitor and set border pixel color. */
7121 wa
[0] = win
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
;
7122 wa
[1] = XCB_EVENT_MASK_ENTER_WINDOW
| XCB_EVENT_MASK_PROPERTY_CHANGE
|
7123 XCB_EVENT_MASK_STRUCTURE_NOTIFY
;
7125 wa
[1] |= XCB_EVENT_MASK_LEAVE_WINDOW
| XCB_EVENT_MASK_FOCUS_CHANGE
;
7128 xcb_change_window_attributes(conn
, win
->id
, XCB_CW_BORDER_PIXEL
|
7129 XCB_CW_EVENT_MASK
, wa
);
7132 /* Figure out where to stack the window in the workspace. */
7133 if (trans
&& (ww
= find_window(trans
)))
7134 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, ww
, win
, entry
);
7135 else if (win
->ws
->focus
&& spawn_position
== SWM_STACK_ABOVE
)
7136 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, win
->ws
->focus
, win
,
7138 else if (win
->ws
->focus
&& spawn_position
== SWM_STACK_BELOW
)
7139 TAILQ_INSERT_BEFORE(win
->ws
->focus
, win
, entry
);
7140 else switch (spawn_position
) {
7143 case SWM_STACK_ABOVE
:
7144 TAILQ_INSERT_TAIL(&win
->ws
->winlist
, win
, entry
);
7146 case SWM_STACK_BOTTOM
:
7147 case SWM_STACK_BELOW
:
7148 TAILQ_INSERT_HEAD(&win
->ws
->winlist
, win
, entry
);
7151 /* Get initial _NET_WM_STATE */
7152 ewmh_get_win_state(win
);
7153 /* Set initial _NET_WM_ALLOWED_ACTIONS */
7154 ewmh_update_actions(win
);
7158 DNPRINTF(SWM_D_MISC
, "manage_window: done. window: 0x%x, (x,y) w x h: "
7159 "(%d,%d) %d x %d, ws: %d, iconic: %s, transient: 0x%x\n", win
->id
,
7160 X(win
), Y(win
), WIDTH(win
), HEIGHT(win
), win
->ws
->idx
,
7161 YESNO(win
->iconic
), win
->transient
);
7167 free_window(struct ws_win
*win
)
7169 DNPRINTF(SWM_D_MISC
, "free_window: window: 0x%x\n", win
->id
);
7174 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
7179 xcb_icccm_get_wm_class_reply_wipe(&win
->ch
);
7184 memset(win
, 0xff, sizeof *win
); /* XXX kill later */
7187 DNPRINTF(SWM_D_MISC
, "free_window: done.\n");
7191 unmanage_window(struct ws_win
*win
)
7193 struct ws_win
*parent
;
7198 DNPRINTF(SWM_D_MISC
, "unmanage_window: window: 0x%x\n", win
->id
);
7200 if (win
->transient
) {
7201 parent
= find_window(win
->transient
);
7203 parent
->focus_child
= NULL
;
7206 TAILQ_REMOVE(&win
->ws
->winlist
, win
, entry
);
7207 TAILQ_INSERT_TAIL(&win
->ws
->unmanagedlist
, win
, entry
);
7211 expose(xcb_expose_event_t
*e
)
7214 struct swm_region
*r
;
7216 DNPRINTF(SWM_D_EVENT
, "expose: window: 0x%x\n", e
->window
);
7218 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7219 for (i
= 0; i
< num_screens
; i
++)
7220 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
7221 if (e
->window
== WINID(r
->bar
))
7229 focusin(xcb_focus_in_event_t
*e
)
7231 DNPRINTF(SWM_D_EVENT
, "focusin: window: 0x%x, mode: %s(%u), "
7232 "detail: %s(%u)\n", e
->event
, get_notify_mode_label(e
->mode
),
7233 e
->mode
, get_notify_detail_label(e
->detail
), e
->detail
);
7237 focusout(xcb_focus_out_event_t
*e
)
7239 DNPRINTF(SWM_D_EVENT
, "focusout: window: 0x%x, mode: %s(%u), "
7240 "detail: %s(%u)\n", e
->event
, get_notify_mode_label(e
->mode
),
7241 e
->mode
, get_notify_detail_label(e
->detail
), e
->detail
);
7246 keypress(xcb_key_press_event_t
*e
)
7248 xcb_keysym_t keysym
;
7251 keysym
= xcb_key_press_lookup_keysym(syms
, e
, 0);
7253 DNPRINTF(SWM_D_EVENT
, "keypress: %u %u\n", e
->detail
, keysym
);
7255 if ((kp
= key_lookup(CLEANMASK(e
->state
), keysym
)) == NULL
)
7258 last_event_time
= e
->time
;
7260 if (kp
->funcid
== KF_SPAWN_CUSTOM
)
7261 spawn_custom(root_to_region(e
->root
, SWM_CK_ALL
),
7262 &(keyfuncs
[kp
->funcid
].args
), kp
->spawn_name
);
7263 else if (keyfuncs
[kp
->funcid
].func
)
7264 keyfuncs
[kp
->funcid
].func(root_to_region(e
->root
, SWM_CK_ALL
),
7265 &(keyfuncs
[kp
->funcid
].args
));
7269 buttonpress(xcb_button_press_event_t
*e
)
7271 struct ws_win
*win
= NULL
;
7272 struct swm_region
*r
, *old_r
;
7276 DNPRINTF(SWM_D_EVENT
, "buttonpress: win (x,y): 0x%x (%d,%d), "
7277 "detail: %u, time: %u, root (x,y): 0x%x (%d,%d), child: 0x%x, "
7278 "state: %u, same_screen: %s\n", e
->event
, e
->event_x
, e
->event_y
,
7279 e
->detail
, e
->time
, e
->root
, e
->root_x
, e
->root_y
, e
->child
,
7280 e
->state
, YESNO(e
->same_screen
));
7282 if (e
->event
== e
->root
) {
7283 if (e
->child
!= 0) {
7284 win
= find_window(e
->child
);
7285 /* Pass ButtonPress to window if it isn't managed. */
7289 /* Focus on empty region */
7290 /* If no windows on region if its empty. */
7291 r
= root_to_region(e
->root
, SWM_CK_POINTER
);
7292 if (r
&& TAILQ_EMPTY(&r
->ws
->winlist
)) {
7293 old_r
= root_to_region(e
->root
, SWM_CK_FOCUS
);
7294 if (old_r
&& old_r
!= r
)
7295 unfocus_win(old_r
->ws
->focus
);
7297 xcb_set_input_focus(conn
,
7298 XCB_INPUT_FOCUS_PARENT
, e
->root
, e
->time
);
7300 /* Clear bar since empty. */
7308 win
= find_window(e
->event
);
7314 last_event_time
= e
->time
;
7316 focus_win(get_focus_magic(win
));
7318 for (i
= 0; i
< LENGTH(buttons
); i
++)
7319 if (client_click
== buttons
[i
].action
&& buttons
[i
].func
&&
7320 buttons
[i
].button
== e
->detail
&&
7321 CLEANMASK(buttons
[i
].mask
) == CLEANMASK(e
->state
)) {
7322 buttons
[i
].func(win
, &buttons
[i
].args
);
7328 DNPRINTF(SWM_D_EVENT
, "buttonpress: passing to window.\n");
7329 /* Replay event to event window */
7330 xcb_allow_events(conn
, XCB_ALLOW_REPLAY_POINTER
, e
->time
);
7332 DNPRINTF(SWM_D_EVENT
, "buttonpress: handled.\n");
7333 /* Unfreeze grab events. */
7334 xcb_allow_events(conn
, XCB_ALLOW_SYNC_POINTER
, e
->time
);
7342 print_win_geom(xcb_window_t w
)
7344 xcb_get_geometry_reply_t
*wa
;
7346 wa
= xcb_get_geometry_reply(conn
, xcb_get_geometry(conn
, w
), NULL
);
7349 DNPRINTF(SWM_D_MISC
, "print_win_geom: window not found: 0x%x\n",
7354 DNPRINTF(SWM_D_MISC
, "print_win_geom: window: 0x%x, root: 0x%x, "
7355 "depth: %u, (x,y) w x h: (%d,%d) %d x %d, border: %d\n",
7356 w
, wa
->root
, wa
->depth
, wa
->x
, wa
->y
, wa
->width
, wa
->height
,
7365 get_stack_mode_name(uint8_t mode
)
7370 case XCB_STACK_MODE_ABOVE
:
7373 case XCB_STACK_MODE_BELOW
:
7376 case XCB_STACK_MODE_TOP_IF
:
7379 case XCB_STACK_MODE_BOTTOM_IF
:
7382 case XCB_STACK_MODE_OPPOSITE
:
7394 configurerequest(xcb_configure_request_event_t
*e
)
7397 struct swm_region
*r
= NULL
;
7400 uint32_t wc
[7] = {0};
7402 if ((win
= find_window(e
->window
)) == NULL
)
7403 if ((win
= find_unmanaged_window(e
->window
)) == NULL
)
7407 if (swm_debug
& SWM_D_EVENT
) {
7408 print_win_geom(e
->window
);
7410 DNPRINTF(SWM_D_EVENT
, "configurerequest: window: 0x%x, "
7411 "parent: 0x%x, new: %s, value_mask: %u { ", e
->window
,
7412 e
->parent
, YESNO(new), e
->value_mask
);
7413 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
)
7414 DPRINTF("X: %d ", e
->x
);
7415 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
)
7416 DPRINTF("Y: %d ", e
->y
);
7417 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
)
7418 DPRINTF("W: %u ", e
->width
);
7419 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
)
7420 DPRINTF("H: %u ", e
->height
);
7421 if (e
->value_mask
& XCB_CONFIG_WINDOW_BORDER_WIDTH
)
7422 DPRINTF("Border: %u ", e
->border_width
);
7423 if (e
->value_mask
& XCB_CONFIG_WINDOW_SIBLING
)
7424 DPRINTF("Sibling: 0x%x ", e
->sibling
);
7425 if (e
->value_mask
& XCB_CONFIG_WINDOW_STACK_MODE
)
7426 DPRINTF("StackMode: %s(%u) ",
7427 get_stack_mode_name(e
->stack_mode
), e
->stack_mode
);
7433 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
) {
7434 mask
|= XCB_CONFIG_WINDOW_X
;
7437 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
) {
7438 mask
|= XCB_CONFIG_WINDOW_Y
;
7441 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
) {
7442 mask
|= XCB_CONFIG_WINDOW_WIDTH
;
7445 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
) {
7446 mask
|= XCB_CONFIG_WINDOW_HEIGHT
;
7447 wc
[i
++] = e
->height
;
7449 if (e
->value_mask
& XCB_CONFIG_WINDOW_BORDER_WIDTH
) {
7450 mask
|= XCB_CONFIG_WINDOW_BORDER_WIDTH
;
7451 wc
[i
++] = e
->border_width
;
7453 if (e
->value_mask
& XCB_CONFIG_WINDOW_SIBLING
) {
7454 mask
|= XCB_CONFIG_WINDOW_SIBLING
;
7455 wc
[i
++] = e
->sibling
;
7457 if (e
->value_mask
& XCB_CONFIG_WINDOW_STACK_MODE
) {
7458 mask
|= XCB_CONFIG_WINDOW_STACK_MODE
;
7459 wc
[i
++] = e
->stack_mode
;
7463 xcb_configure_window(conn
, e
->window
, mask
, wc
);
7466 } else if ((!win
->manual
|| win
->quirks
& SWM_Q_ANYWHERE
) &&
7467 !(win
->ewmh_flags
& EWMH_F_FULLSCREEN
)) {
7470 else if (win
->ws
->old_r
)
7473 /* windows are centered unless ANYWHERE quirk is set. */
7474 if (win
->quirks
& SWM_Q_ANYWHERE
) {
7475 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
) {
7476 win
->g_float
.x
= e
->x
;
7478 win
->g_float
.x
-= X(r
);
7481 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
) {
7482 win
->g_float
.y
= e
->y
;
7484 win
->g_float
.y
-= Y(r
);
7488 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
)
7489 win
->g_float
.w
= e
->width
;
7491 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
)
7492 win
->g_float
.h
= e
->height
;
7494 win
->g_floatvalid
= 1;
7496 if (win
->floating
&& r
&& (win
->transient
||
7497 win
->ws
->cur_layout
!= &layouts
[SWM_MAX_STACK
])) {
7498 WIDTH(win
) = win
->g_float
.w
;
7499 HEIGHT(win
) = win
->g_float
.h
;
7501 stack_floater(win
, win
->ws
->r
);
7513 DNPRINTF(SWM_D_EVENT
, "configurerequest: done.\n");
7517 configurenotify(xcb_configure_notify_event_t
*e
)
7521 DNPRINTF(SWM_D_EVENT
, "configurenotify: win 0x%x, event win: 0x%x, "
7522 "(x,y) WxH: (%d,%d) %ux%u, border: %u, above_sibling: 0x%x, "
7523 "override_redirect: %s\n", e
->window
, e
->event
, e
->x
, e
->y
,
7524 e
->width
, e
->height
, e
->border_width
, e
->above_sibling
,
7525 YESNO(e
->override_redirect
));
7527 win
= find_window(e
->window
);
7529 xcb_icccm_get_wm_normal_hints_reply(conn
,
7530 xcb_icccm_get_wm_normal_hints(conn
, win
->id
),
7533 if (font_adjusted
) {
7541 destroynotify(xcb_destroy_notify_event_t
*e
)
7545 DNPRINTF(SWM_D_EVENT
, "destroynotify: window: 0x%x\n", e
->window
);
7547 if ((win
= find_window(e
->window
)) == NULL
) {
7548 if ((win
= find_unmanaged_window(e
->window
)) == NULL
)
7554 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
7555 /* If we were focused, make sure we focus on something else. */
7556 if (win
== win
->ws
->focus
)
7557 win
->ws
->focus_pending
= get_focus_prev(win
);
7560 unmanage_window(win
);
7563 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
7564 if (win
->ws
->focus_pending
) {
7565 focus_win(win
->ws
->focus_pending
);
7566 win
->ws
->focus_pending
= NULL
;
7577 get_notify_detail_label(uint8_t detail
)
7582 case XCB_NOTIFY_DETAIL_ANCESTOR
:
7585 case XCB_NOTIFY_DETAIL_VIRTUAL
:
7588 case XCB_NOTIFY_DETAIL_INFERIOR
:
7591 case XCB_NOTIFY_DETAIL_NONLINEAR
:
7592 label
= "Nonlinear";
7594 case XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL
:
7595 label
= "NonlinearVirtual";
7597 case XCB_NOTIFY_DETAIL_POINTER
:
7600 case XCB_NOTIFY_DETAIL_POINTER_ROOT
:
7601 label
= "PointerRoot";
7603 case XCB_NOTIFY_DETAIL_NONE
:
7614 get_notify_mode_label(uint8_t mode
)
7619 case XCB_NOTIFY_MODE_NORMAL
:
7622 case XCB_NOTIFY_MODE_GRAB
:
7625 case XCB_NOTIFY_MODE_UNGRAB
:
7628 case XCB_NOTIFY_MODE_WHILE_GRABBED
:
7629 label
= "WhileGrabbed";
7640 enternotify(xcb_enter_notify_event_t
*e
)
7643 struct swm_region
*old_r
, *r
;
7645 DNPRINTF(SWM_D_FOCUS
, "enternotify: time: %u, win (x,y): 0x%x "
7646 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): 0x%x (%d,%d), "
7647 "child: 0x%x, same_screen_focus: %s, state: %d\n",
7648 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
7649 get_notify_mode_label(e
->mode
), e
->mode
,
7650 get_notify_detail_label(e
->detail
), e
->detail
,
7651 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
7652 YESNO(e
->same_screen_focus
), e
->state
);
7654 if (focus_mode
== SWM_FOCUS_MANUAL
&&
7655 e
->mode
== XCB_NOTIFY_MODE_NORMAL
) {
7656 DNPRINTF(SWM_D_EVENT
, "enternotify: manual focus; ignoring.\n");
7660 last_event_time
= e
->time
;
7662 if ((win
= find_window(e
->event
)) == NULL
) {
7663 if (e
->event
== e
->root
) {
7664 /* If no windows on pointer region, then focus root. */
7665 r
= root_to_region(e
->root
, SWM_CK_POINTER
);
7666 if (TAILQ_EMPTY(&r
->ws
->winlist
)) {
7667 old_r
= root_to_region(e
->root
, SWM_CK_FOCUS
);
7668 if (old_r
&& old_r
!= r
)
7669 unfocus_win(old_r
->ws
->focus
);
7671 xcb_set_input_focus(conn
,
7672 XCB_INPUT_FOCUS_PARENT
, e
->root
, e
->time
);
7674 /* Clear bar since empty. */
7680 DNPRINTF(SWM_D_EVENT
, "enternotify: window is NULL; "
7686 focus_win(get_focus_magic(win
));
7693 leavenotify(xcb_leave_notify_event_t
*e
)
7695 DNPRINTF(SWM_D_FOCUS
, "leavenotify: time: %u, win (x,y): 0x%x "
7696 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): 0x%x (%d,%d), "
7697 "child: 0x%x, same_screen_focus: %s, state: %d\n",
7698 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
7699 get_notify_mode_label(e
->mode
), e
->mode
,
7700 get_notify_detail_label(e
->detail
), e
->detail
,
7701 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
7702 YESNO(e
->same_screen_focus
), e
->state
);
7706 /* lets us use one switch statement for arbitrary mode/detail combinations */
7707 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
7710 mapnotify(xcb_map_notify_event_t
*e
)
7714 DNPRINTF(SWM_D_EVENT
, "mapnotify: window: 0x%x\n", e
->window
);
7716 if ((win
= find_window(e
->window
)) == NULL
)
7717 win
= manage_window(e
->window
, 1);
7720 set_win_state(win
, XCB_ICCCM_WM_STATE_NORMAL
);
7722 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
7723 if (win
->ws
->focus_pending
== win
) {
7725 win
->ws
->focus_pending
= NULL
;
7734 mappingnotify(xcb_mapping_notify_event_t
*e
)
7736 xcb_refresh_keyboard_mapping(syms
, e
);
7738 if (e
->request
== XCB_MAPPING_KEYBOARD
)
7743 maprequest(xcb_map_request_event_t
*e
)
7746 xcb_get_window_attributes_reply_t
*war
;
7748 DNPRINTF(SWM_D_EVENT
, "maprequest: win 0x%x\n",
7751 war
= xcb_get_window_attributes_reply(conn
,
7752 xcb_get_window_attributes(conn
, e
->window
),
7755 DNPRINTF(SWM_D_EVENT
, "maprequest: window lost.\n");
7759 if (war
->override_redirect
) {
7760 DNPRINTF(SWM_D_EVENT
, "maprequest: override_redirect; "
7765 win
= manage_window(e
->window
,
7766 (war
->map_state
== XCB_MAP_STATE_VIEWABLE
));
7768 /* The new window should get focus; prepare. */
7769 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
7770 win
->ws
->focus_pending
= get_focus_magic(win
);
7772 /* All windows need to be mapped if they are in the current workspace.*/
7776 /* Ignore EnterNotify to handle the mapnotify without interference. */
7777 if (focus_mode
== SWM_FOCUS_DEFAULT
)
7778 event_drain(XCB_ENTER_NOTIFY
);
7781 DNPRINTF(SWM_D_EVENT
, "maprequest: done.\n");
7786 get_atom_name(xcb_atom_t atom
)
7791 * This should be disabled during most debugging since
7792 * xcb_get_* causes an xcb_flush.
7795 xcb_get_atom_name_reply_t
*r
;
7797 r
= xcb_get_atom_name_reply(conn
,
7798 xcb_get_atom_name(conn
, atom
),
7801 len
= xcb_get_atom_name_name_length(r
);
7803 name
= malloc(len
+ 1);
7805 memcpy(name
, xcb_get_atom_name_name(r
), len
);
7819 propertynotify(xcb_property_notify_event_t
*e
)
7825 name
= get_atom_name(e
->atom
);
7826 DNPRINTF(SWM_D_EVENT
, "propertynotify: window: 0x%x, atom: %s(%u), "
7827 "time: %#x, state: %u\n", e
->window
, name
, e
->atom
, e
->time
,
7831 win
= find_window(e
->window
);
7835 last_event_time
= e
->time
;
7837 if (e
->atom
== a_swm_iconic
) {
7838 if (e
->state
== XCB_PROPERTY_NEW_VALUE
) {
7839 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
7840 win
->ws
->focus_pending
= get_focus_prev(win
);
7847 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
7848 focus_win(win
->ws
->focus_pending
);
7849 win
->ws
->focus_pending
= NULL
;
7853 } else if (e
->state
== XCB_PROPERTY_DELETE
) {
7854 /* The window is no longer iconic, restack ws. */
7855 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
7856 win
->ws
->focus_pending
= get_focus_magic(win
);
7860 /* Flush EnterNotify for mapnotify, if needed. */
7863 } else if (e
->atom
== a_state
) {
7864 /* State just changed, make sure it gets focused if mapped. */
7865 if (e
->state
== XCB_PROPERTY_NEW_VALUE
) {
7866 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
7868 win
->ws
->focus_pending
== win
) {
7869 focus_win(win
->ws
->focus_pending
);
7870 win
->ws
->focus_pending
= NULL
;
7874 } else if (e
->atom
== XCB_ATOM_WM_CLASS
||
7875 e
->atom
== XCB_ATOM_WM_NAME
) {
7883 unmapnotify(xcb_unmap_notify_event_t
*e
)
7886 struct workspace
*ws
;
7888 DNPRINTF(SWM_D_EVENT
, "unmapnotify: window: 0x%x\n", e
->window
);
7890 /* If we aren't managing the window, then ignore. */
7891 win
= find_window(e
->window
);
7897 if (getstate(e
->window
) != XCB_ICCCM_WM_STATE_ICONIC
)
7898 set_win_state(win
, XCB_ICCCM_WM_STATE_ICONIC
);
7901 /* window unmapped itself */
7902 /* do unmap/unfocus/restack and unmanage */
7905 /* If win was focused, make sure to focus on something else. */
7906 if (win
== ws
->focus
) {
7907 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
7908 ws
->focus_pending
= get_focus_prev(win
);
7909 DNPRINTF(SWM_D_EVENT
, "unmapnotify: "
7910 "focus_pending: 0x%x\n",
7911 WINID(ws
->focus_pending
));
7917 unmanage_window(win
);
7922 if (focus_mode
== SWM_FOCUS_FOLLOW
) {
7924 focus_win(get_pointer_win(ws
->r
->s
->root
));
7926 if (ws
->focus_pending
) {
7927 focus_win(ws
->focus_pending
);
7928 ws
->focus_pending
= NULL
;
7933 if (getstate(e
->window
) == XCB_ICCCM_WM_STATE_NORMAL
)
7934 set_win_state(win
, XCB_ICCCM_WM_STATE_ICONIC
);
7941 visibilitynotify(xcb_visibility_notify_event_t
*e
)
7943 DNPRINTF(SWM_D_EVENT
, "visibilitynotify: window: 0x%x\n",
7949 clientmessage(xcb_client_message_event_t
*e
)
7952 xcb_map_request_event_t mre
;
7956 name
= get_atom_name(e
->type
);
7957 DNPRINTF(SWM_D_EVENT
, "clientmessage: window: 0x%x, atom: %s(%u)\n",
7958 e
->window
, name
, e
->type
);
7961 win
= find_window(e
->window
);
7964 if (e
->type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
7965 /* Manage the window with maprequest. */
7966 DNPRINTF(SWM_D_EVENT
, "clientmessage: request focus on "
7967 "unmanaged window.\n");
7968 mre
.window
= e
->window
;
7974 if (e
->type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
7975 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_ACTIVE_WINDOW\n");
7978 if (e
->type
== ewmh
[_NET_CLOSE_WINDOW
].atom
) {
7979 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_CLOSE_WINDOW\n");
7980 if (win
->can_delete
)
7981 client_msg(win
, a_delete
, 0);
7983 xcb_kill_client(conn
, win
->id
);
7985 if (e
->type
== ewmh
[_NET_MOVERESIZE_WINDOW
].atom
) {
7986 DNPRINTF(SWM_D_EVENT
,
7987 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
7988 if (win
->floating
) {
7989 if (e
->data
.data32
[0] & (1<<8)) /* x */
7990 X(win
) = e
->data
.data32
[1];
7991 if (e
->data
.data32
[0] & (1<<9)) /* y */
7992 Y(win
) = e
->data
.data32
[2];
7993 if (e
->data
.data32
[0] & (1<<10)) /* width */
7994 WIDTH(win
) = e
->data
.data32
[3];
7995 if (e
->data
.data32
[0] & (1<<11)) /* height */
7996 HEIGHT(win
) = e
->data
.data32
[4];
8001 /* TODO: Change stack sizes */
8002 /* notify no change was made. */
8003 config_win(win
, NULL
);
8006 if (e
->type
== ewmh
[_NET_WM_STATE
].atom
) {
8007 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_WM_STATE\n");
8008 ewmh_update_win_state(win
, e
->data
.data32
[1], e
->data
.data32
[0]);
8009 if (e
->data
.data32
[2])
8010 ewmh_update_win_state(win
, e
->data
.data32
[2],
8022 int errcode
= xcb_connection_has_error(conn
);
8023 #ifdef XCB_CONN_ERROR
8026 case XCB_CONN_ERROR
:
8027 s
= "Socket error, pipe error or other stream error.";
8029 case XCB_CONN_CLOSED_EXT_NOTSUPPORTED
:
8030 s
= "Extension not supported.";
8032 case XCB_CONN_CLOSED_MEM_INSUFFICIENT
:
8033 s
= "Insufficient memory.";
8035 case XCB_CONN_CLOSED_REQ_LEN_EXCEED
:
8036 s
= "Request length was exceeded.";
8038 case XCB_CONN_CLOSED_PARSE_ERR
:
8039 s
= "Error parsing display string.";
8042 s
= "Unknown error.";
8045 errx(errcode
, "X CONNECTION ERROR: %s", s
);
8048 errx(errcode
, "X CONNECTION ERROR");
8056 const uint32_t val
= XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT
|
8057 XCB_EVENT_MASK_ENTER_WINDOW
;
8059 xcb_void_cookie_t wac
;
8060 xcb_generic_error_t
*error
;
8062 /* this causes an error if some other window manager is running */
8063 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
8064 for (i
= 0; i
< num_screens
; i
++) {
8065 if ((sc
= get_screen(i
)) == NULL
)
8066 errx(1, "ERROR: can't get screen %d.", i
);
8067 DNPRINTF(SWM_D_INIT
, "enable_wm: screen %d, root: 0x%x\n",
8069 wac
= xcb_change_window_attributes_checked(conn
, sc
->root
,
8070 XCB_CW_EVENT_MASK
, &val
);
8071 if ((error
= xcb_request_check(conn
, wac
))) {
8072 DNPRINTF(SWM_D_INIT
, "enable_wm: error_code: %u\n",
8078 /* click to focus on empty region */
8079 xcb_grab_button(conn
, 1, sc
->root
, BUTTONMASK
,
8080 XCB_GRAB_MODE_SYNC
, XCB_GRAB_MODE_ASYNC
, XCB_WINDOW_NONE
,
8081 XCB_CURSOR_NONE
, XCB_BUTTON_INDEX_1
, XCB_BUTTON_MASK_ANY
);
8088 new_region(struct swm_screen
*s
, int x
, int y
, int w
, int h
)
8090 struct swm_region
*r
, *n
;
8091 struct workspace
*ws
= NULL
;
8094 DNPRINTF(SWM_D_MISC
, "new region: screen[%d]:%dx%d+%d+%d\n",
8095 s
->idx
, w
, h
, x
, y
);
8097 /* remove any conflicting regions */
8098 n
= TAILQ_FIRST(&s
->rl
);
8101 n
= TAILQ_NEXT(r
, entry
);
8102 if (X(r
) < (x
+ w
) &&
8103 (X(r
) + WIDTH(r
)) > x
&&
8105 (Y(r
) + HEIGHT(r
)) > y
) {
8106 if (r
->ws
->r
!= NULL
)
8107 r
->ws
->old_r
= r
->ws
->r
;
8110 TAILQ_REMOVE(&s
->rl
, r
, entry
);
8111 TAILQ_INSERT_TAIL(&s
->orl
, r
, entry
);
8115 /* search old regions for one to reuse */
8117 /* size + location match */
8118 TAILQ_FOREACH(r
, &s
->orl
, entry
)
8119 if (X(r
) == x
&& Y(r
) == y
&&
8120 HEIGHT(r
) == h
&& WIDTH(r
) == w
)
8124 TAILQ_FOREACH(r
, &s
->orl
, entry
)
8125 if (HEIGHT(r
) == h
&& WIDTH(r
) == w
)
8129 TAILQ_REMOVE(&s
->orl
, r
, entry
);
8130 /* try to use old region's workspace */
8131 if (r
->ws
->r
== NULL
)
8134 if ((r
= calloc(1, sizeof(struct swm_region
))) == NULL
)
8135 err(1, "new_region: calloc: failed to allocate memory "
8138 /* if we don't have a workspace already, find one */
8140 for (i
= 0; i
< workspace_limit
; i
++)
8141 if (s
->ws
[i
].r
== NULL
) {
8148 errx(1, "new_region: no free workspaces");
8159 TAILQ_INSERT_TAIL(&s
->rl
, r
, entry
);
8165 #ifdef SWM_XRR_HAS_CRTC
8168 #endif /* SWM_XRR_HAS_CRTC */
8169 struct swm_region
*r
;
8171 xcb_randr_get_screen_resources_current_cookie_t src
;
8172 xcb_randr_get_screen_resources_current_reply_t
*srr
;
8173 xcb_randr_get_crtc_info_cookie_t cic
;
8174 xcb_randr_get_crtc_info_reply_t
*cir
= NULL
;
8175 xcb_randr_crtc_t
*crtc
;
8176 xcb_screen_t
*screen
;
8178 if ((screen
= get_screen(i
)) == NULL
)
8179 errx(1, "ERROR: can't get screen %d.", i
);
8181 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
8182 if (i
>= num_screens
)
8183 errx(1, "scan_xrandr: invalid screen");
8185 /* remove any old regions */
8186 while ((r
= TAILQ_FIRST(&screens
[i
].rl
)) != NULL
) {
8187 r
->ws
->old_r
= r
->ws
->r
= NULL
;
8189 TAILQ_REMOVE(&screens
[i
].rl
, r
, entry
);
8190 TAILQ_INSERT_TAIL(&screens
[i
].orl
, r
, entry
);
8194 /* map virtual screens onto physical screens */
8195 #ifdef SWM_XRR_HAS_CRTC
8196 if (xrandr_support
) {
8197 src
= xcb_randr_get_screen_resources_current(conn
,
8199 srr
= xcb_randr_get_screen_resources_current_reply(conn
, src
,
8202 new_region(&screens
[i
], 0, 0,
8203 screen
->width_in_pixels
,
8204 screen
->height_in_pixels
);
8207 ncrtc
= srr
->num_crtcs
;
8209 crtc
= xcb_randr_get_screen_resources_current_crtcs(srr
);
8210 for (c
= 0; c
< ncrtc
; c
++) {
8211 cic
= xcb_randr_get_crtc_info(conn
, crtc
[c
],
8213 cir
= xcb_randr_get_crtc_info_reply(conn
, cic
, NULL
);
8216 if (cir
->num_outputs
== 0) {
8222 new_region(&screens
[i
], 0, 0,
8223 screen
->width_in_pixels
,
8224 screen
->height_in_pixels
);
8226 new_region(&screens
[i
],
8227 cir
->x
, cir
->y
, cir
->width
, cir
->height
);
8232 #endif /* SWM_XRR_HAS_CRTC */
8234 new_region(&screens
[i
], 0, 0, screen
->width_in_pixels
,
8235 screen
->height_in_pixels
);
8240 screenchange(xcb_randr_screen_change_notify_event_t
*e
)
8242 struct swm_region
*r
;
8245 DNPRINTF(SWM_D_EVENT
, "screenchange: root: 0x%x\n", e
->root
);
8247 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
8248 /* silly event doesn't include the screen index */
8249 for (i
= 0; i
< num_screens
; i
++)
8250 if (screens
[i
].root
== e
->root
)
8252 if (i
>= num_screens
)
8253 errx(1, "screenchange: screen not found");
8255 /* brute force for now, just re-enumerate the regions */
8259 print_win_geom(e
->root
);
8261 /* add bars to all regions */
8262 for (i
= 0; i
< num_screens
; i
++)
8263 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
8271 xcb_window_t
*wins
= NULL
, trans
;
8273 int i
, j
, num_screens
;
8274 uint16_t state
, manage
, mapped
;
8276 xcb_query_tree_cookie_t qtc
;
8277 xcb_query_tree_reply_t
*qtr
;
8278 xcb_get_window_attributes_cookie_t c
;
8279 xcb_get_window_attributes_reply_t
*r
;
8280 xcb_get_property_cookie_t pc
;
8282 DNPRINTF(SWM_D_INIT
, "grab_windows: begin\n");
8283 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
8284 for (i
= 0; i
< num_screens
; i
++) {
8285 qtc
= xcb_query_tree(conn
, screens
[i
].root
);
8286 qtr
= xcb_query_tree_reply(conn
, qtc
, NULL
);
8289 wins
= xcb_query_tree_children(qtr
);
8290 no
= xcb_query_tree_children_length(qtr
);
8291 /* attach windows to a region */
8292 /* normal windows */
8293 DNPRINTF(SWM_D_INIT
, "grab_windows: grab top level windows.\n");
8294 for (j
= 0; j
< no
; j
++) {
8295 c
= xcb_get_window_attributes(conn
, wins
[j
]);
8296 r
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
8298 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
8299 "doesn't exist.\n", wins
[j
]);
8302 if (r
->override_redirect
) {
8303 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
8304 "override_redirect set.\n", wins
[j
]);
8309 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
8310 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
8312 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
8313 "is transient for %#x.\n", wins
[j
], trans
);
8318 state
= getstate(wins
[j
]);
8319 manage
= state
!= XCB_ICCCM_WM_STATE_WITHDRAWN
;
8320 mapped
= r
->map_state
!= XCB_MAP_STATE_UNMAPPED
;
8321 if (mapped
|| manage
)
8322 manage_window(wins
[j
], mapped
);
8325 /* transient windows */
8326 DNPRINTF(SWM_D_INIT
, "grab_windows: grab transient windows.\n");
8327 for (j
= 0; j
< no
; j
++) {
8328 c
= xcb_get_window_attributes(conn
, wins
[j
]);
8329 r
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
8331 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
8332 "doesn't exist.\n", wins
[j
]);
8335 if (r
->override_redirect
) {
8336 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
8337 "override_redirect set.\n", wins
[j
]);
8342 state
= getstate(wins
[j
]);
8343 manage
= state
!= XCB_ICCCM_WM_STATE_WITHDRAWN
;
8344 mapped
= r
->map_state
!= XCB_MAP_STATE_UNMAPPED
;
8345 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
8346 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
8347 &trans
, NULL
) && manage
)
8348 manage_window(wins
[j
], mapped
);
8353 DNPRINTF(SWM_D_INIT
, "grab_windows: done.\n");
8359 int i
, j
, k
, num_screens
;
8360 struct workspace
*ws
;
8361 uint32_t gcv
[1], wa
[1];
8362 const xcb_query_extension_reply_t
*qep
;
8363 xcb_screen_t
*screen
;
8364 xcb_randr_query_version_cookie_t c
;
8365 xcb_randr_query_version_reply_t
*r
;
8367 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
8368 if ((screens
= calloc(num_screens
,
8369 sizeof(struct swm_screen
))) == NULL
)
8370 err(1, "setup_screens: calloc: failed to allocate memory for "
8373 /* initial Xrandr setup */
8375 qep
= xcb_get_extension_data(conn
, &xcb_randr_id
);
8377 c
= xcb_randr_query_version(conn
, 1, 1);
8378 r
= xcb_randr_query_version_reply(conn
, c
, NULL
);
8380 if (r
->major_version
>= 1) {
8382 xrandr_eventbase
= qep
->first_event
;
8388 wa
[0] = cursors
[XC_LEFT_PTR
].cid
;
8390 /* map physical screens */
8391 for (i
= 0; i
< num_screens
; i
++) {
8392 DNPRINTF(SWM_D_WS
, "setup_screens: init screen: %d\n", i
);
8394 TAILQ_INIT(&screens
[i
].rl
);
8395 TAILQ_INIT(&screens
[i
].orl
);
8396 if ((screen
= get_screen(i
)) == NULL
)
8397 errx(1, "ERROR: can't get screen %d.", i
);
8398 screens
[i
].root
= screen
->root
;
8400 /* set default colors */
8401 setscreencolor("red", i
+ 1, SWM_S_COLOR_FOCUS
);
8402 setscreencolor("rgb:88/88/88", i
+ 1, SWM_S_COLOR_UNFOCUS
);
8403 setscreencolor("rgb:00/80/80", i
+ 1, SWM_S_COLOR_BAR_BORDER
);
8404 setscreencolor("black", i
+ 1, SWM_S_COLOR_BAR
);
8405 setscreencolor("rgb:a0/a0/a0", i
+ 1, SWM_S_COLOR_BAR_FONT
);
8407 /* create graphics context on screen */
8408 screens
[i
].bar_gc
= xcb_generate_id(conn
);
8410 xcb_create_gc(conn
, screens
[i
].bar_gc
, screens
[i
].root
,
8411 XCB_GC_GRAPHICS_EXPOSURES
, gcv
);
8413 /* set default cursor */
8414 xcb_change_window_attributes(conn
, screens
[i
].root
,
8417 /* init all workspaces */
8418 /* XXX these should be dynamically allocated too */
8419 for (j
= 0; j
< SWM_WS_MAX
; j
++) {
8420 ws
= &screens
[i
].ws
[j
];
8424 ws
->focus_prev
= NULL
;
8425 ws
->focus_pending
= NULL
;
8428 TAILQ_INIT(&ws
->winlist
);
8429 TAILQ_INIT(&ws
->unmanagedlist
);
8431 for (k
= 0; layouts
[k
].l_stack
!= NULL
; k
++)
8432 if (layouts
[k
].l_config
!= NULL
)
8433 layouts
[k
].l_config(ws
,
8434 SWM_ARG_ID_STACKINIT
);
8435 ws
->cur_layout
= &layouts
[0];
8436 ws
->cur_layout
->l_string(ws
);
8442 xcb_randr_select_input(conn
, screens
[i
].root
,
8443 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE
);
8450 if ((bar_fonts
= strdup(SWM_BAR_FONTS
)) == NULL
)
8451 err(1, "setup_globals: strdup: failed to allocate memory.");
8453 if ((clock_format
= strdup("%a %b %d %R %Z %Y")) == NULL
)
8454 err(1, "setup_globals: strdup: failed to allocate memory.");
8456 if ((syms
= xcb_key_symbols_alloc(conn
)) == NULL
)
8457 errx(1, "unable to allocate key symbols");
8459 a_state
= get_atom_from_string("WM_STATE");
8460 a_prot
= get_atom_from_string("WM_PROTOCOLS");
8461 a_delete
= get_atom_from_string("WM_DELETE_WINDOW");
8462 a_takefocus
= get_atom_from_string("WM_TAKE_FOCUS");
8463 a_wmname
= get_atom_from_string("WM_NAME");
8464 a_netwmname
= get_atom_from_string("_NET_WM_NAME");
8465 a_utf8_string
= get_atom_from_string("UTF8_STRING");
8466 a_string
= get_atom_from_string("STRING");
8467 a_swm_iconic
= get_atom_from_string("_SWM_ICONIC");
8468 a_swm_ws
= get_atom_from_string("_SWM_WS");
8475 xcb_atom_t netwmcheck
;
8476 xcb_window_t root
, win
;
8478 /* work around sun jdk bugs, code from wmname */
8479 netwmcheck
= get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
8481 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
8482 for (i
= 0; i
< num_screens
; i
++) {
8483 root
= screens
[i
].root
;
8485 win
= xcb_generate_id(conn
);
8486 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, win
, root
,
8487 0, 0, 1, 1, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
8488 XCB_COPY_FROM_PARENT
, 0, NULL
);
8490 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, root
,
8491 netwmcheck
, XCB_ATOM_WINDOW
, 32, 1, &win
);
8492 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
8493 netwmcheck
, XCB_ATOM_WINDOW
, 32, 1, &win
);
8494 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
8495 a_netwmname
, a_utf8_string
, 8, strlen("LG3D"), "LG3D");
8500 shutdown_cleanup(void)
8504 /* disable alarm because the following code may not be interrupted */
8506 if (signal(SIGALRM
, SIG_IGN
) == SIG_ERR
)
8507 err(1, "can't disable alarm");
8517 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
8518 for (i
= 0; i
< num_screens
; ++i
) {
8519 if (screens
[i
].bar_gc
!= 0)
8520 xcb_free_gc(conn
, screens
[i
].bar_gc
);
8521 if (!bar_font_legacy
)
8522 XftColorFree(display
, DefaultVisual(display
, i
),
8523 DefaultColormap(display
, i
), &bar_font_color
);
8526 if (bar_font_legacy
)
8527 XFreeFontSet(display
, bar_fs
);
8529 XftFontClose(display
, bar_font
);
8532 xcb_key_symbols_free(syms
);
8534 xcb_disconnect(conn
);
8538 event_error(xcb_generic_error_t
*e
)
8542 DNPRINTF(SWM_D_EVENT
, "event_error: %s(%u) from %s(%u), sequence: %u, "
8543 "resource_id: %u, minor_code: %u\n",
8544 xcb_event_get_error_label(e
->error_code
), e
->error_code
,
8545 xcb_event_get_request_label(e
->major_code
), e
->major_code
,
8546 e
->sequence
, e
->resource_id
, e
->minor_code
);
8550 event_handle(xcb_generic_event_t
*evt
)
8552 uint8_t type
= XCB_EVENT_RESPONSE_TYPE(evt
);
8554 DNPRINTF(SWM_D_EVENT
, "XCB Event: %s(%d), seq %u\n",
8555 xcb_event_get_label(XCB_EVENT_RESPONSE_TYPE(evt
)),
8556 XCB_EVENT_RESPONSE_TYPE(evt
), evt
->sequence
);
8559 #define EVENT(type, callback) case type: callback((void *)evt); return
8560 EVENT(0, event_error
);
8561 EVENT(XCB_BUTTON_PRESS
, buttonpress
);
8562 /*EVENT(XCB_BUTTON_RELEASE, buttonpress);*/
8563 /*EVENT(XCB_CIRCULATE_NOTIFY, );*/
8564 /*EVENT(XCB_CIRCULATE_REQUEST, );*/
8565 EVENT(XCB_CLIENT_MESSAGE
, clientmessage
);
8566 /*EVENT(XCB_COLORMAP_NOTIFY, );*/
8567 EVENT(XCB_CONFIGURE_NOTIFY
, configurenotify
);
8568 EVENT(XCB_CONFIGURE_REQUEST
, configurerequest
);
8569 /*EVENT(XCB_CREATE_NOTIFY, );*/
8570 EVENT(XCB_DESTROY_NOTIFY
, destroynotify
);
8571 EVENT(XCB_ENTER_NOTIFY
, enternotify
);
8572 EVENT(XCB_EXPOSE
, expose
);
8574 EVENT(XCB_FOCUS_IN
, focusin
);
8575 EVENT(XCB_FOCUS_OUT
, focusout
);
8577 /*EVENT(XCB_GRAPHICS_EXPOSURE, );*/
8578 /*EVENT(XCB_GRAVITY_NOTIFY, );*/
8579 EVENT(XCB_KEY_PRESS
, keypress
);
8580 /*EVENT(XCB_KEY_RELEASE, keypress);*/
8581 /*EVENT(XCB_KEYMAP_NOTIFY, );*/
8583 EVENT(XCB_LEAVE_NOTIFY
, leavenotify
);
8585 EVENT(XCB_MAP_NOTIFY
, mapnotify
);
8586 EVENT(XCB_MAP_REQUEST
, maprequest
);
8587 EVENT(XCB_MAPPING_NOTIFY
, mappingnotify
);
8588 /*EVENT(XCB_MOTION_NOTIFY, );*/
8589 /*EVENT(XCB_NO_EXPOSURE, );*/
8590 EVENT(XCB_PROPERTY_NOTIFY
, propertynotify
);
8591 /*EVENT(XCB_REPARENT_NOTIFY, );*/
8592 /*EVENT(XCB_RESIZE_REQUEST, );*/
8593 /*EVENT(XCB_SELECTION_CLEAR, );*/
8594 /*EVENT(XCB_SELECTION_NOTIFY, );*/
8595 /*EVENT(XCB_SELECTION_REQUEST, );*/
8596 EVENT(XCB_UNMAP_NOTIFY
, unmapnotify
);
8597 /*EVENT(XCB_VISIBILITY_NOTIFY, visibilitynotify);*/
8600 if (type
- xrandr_eventbase
== XCB_RANDR_SCREEN_CHANGE_NOTIFY
)
8601 screenchange((void *)evt
);
8605 main(int argc
, char *argv
[])
8607 struct swm_region
*r
, *rr
;
8608 struct ws_win
*winfocus
= NULL
;
8609 char conf
[PATH_MAX
], *cfile
= NULL
;
8611 int xfd
, i
, num_screens
;
8612 struct sigaction sact
;
8613 xcb_generic_event_t
*evt
;
8617 /* suppress unused warning since var is needed */
8620 time_started
= time(NULL
);
8623 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION
, buildstr
);
8624 if (!setlocale(LC_CTYPE
, "") || !setlocale(LC_TIME
, ""))
8625 warnx("no locale support");
8627 /* handle some signals */
8628 bzero(&sact
, sizeof(sact
));
8629 sigemptyset(&sact
.sa_mask
);
8631 sact
.sa_handler
= sighdlr
;
8632 sigaction(SIGINT
, &sact
, NULL
);
8633 sigaction(SIGQUIT
, &sact
, NULL
);
8634 sigaction(SIGTERM
, &sact
, NULL
);
8635 sigaction(SIGHUP
, &sact
, NULL
);
8637 sact
.sa_handler
= sighdlr
;
8638 sact
.sa_flags
= SA_NOCLDSTOP
;
8639 sigaction(SIGCHLD
, &sact
, NULL
);
8641 if (!(display
= XOpenDisplay(0)))
8642 errx(1, "can not open display");
8644 conn
= XGetXCBConnection(display
);
8645 if (xcb_connection_has_error(conn
))
8646 errx(1, "can not get XCB connection");
8648 XSetEventQueueOwner(display
, XCBOwnsEventQueue
);
8650 xcb_prefetch_extension_data(conn
, &xcb_randr_id
);
8651 xfd
= xcb_get_file_descriptor(conn
);
8653 /* look for local and global conf file */
8654 pwd
= getpwuid(getuid());
8656 errx(1, "invalid user: %d", getuid());
8658 xcb_grab_server(conn
);
8661 /* flush all events */
8662 while ((evt
= xcb_poll_for_event(conn
))) {
8663 if (XCB_EVENT_RESPONSE_TYPE(evt
) == 0)
8668 if (enable_wm() != 0)
8669 errx(1, "another window manager is currently running");
8671 /* Load Xcursors and/or cursorfont glyph cursors. */
8683 for (i
= 0; ; i
++) {
8688 snprintf(conf
, sizeof conf
, "%s/.%s",
8689 pwd
->pw_dir
, SWM_CONF_FILE
);
8693 snprintf(conf
, sizeof conf
, "/etc/%s",
8698 snprintf(conf
, sizeof conf
, "%s/.%s",
8699 pwd
->pw_dir
, SWM_CONF_FILE_OLD
);
8703 snprintf(conf
, sizeof conf
, "/etc/%s",
8710 if (strlen(conf
) && stat(conf
, &sb
) != -1)
8711 if (S_ISREG(sb
.st_mode
)) {
8718 /* load conf (if any) */
8720 conf_load(cfile
, SWM_CONF_DEFAULT
);
8723 /* set some values to work around bad programs */
8725 /* grab existing windows (before we build the bars) */
8728 if (getenv("SWM_STARTED") == NULL
)
8729 setenv("SWM_STARTED", "YES", 1);
8731 /* setup all bars */
8732 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
8733 for (i
= 0; i
< num_screens
; i
++)
8734 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
8735 if (winfocus
== NULL
)
8736 winfocus
= TAILQ_FIRST(&r
->ws
->winlist
);
8743 xcb_ungrab_server(conn
);
8747 while ((evt
= xcb_poll_for_event(conn
))) {
8754 /* If just (re)started, set default focus if needed. */
8755 if (winfocus
&& focus_mode
!= SWM_FOCUS_FOLLOW
) {
8756 rr
= winfocus
->ws
->r
;
8758 /* not a visible window */
8763 focus_win(get_region_focus(rr
));
8773 if (select(xfd
+ 1, &rd
, NULL
, NULL
, &tv
) == -1)
8774 if (errno
!= EINTR
) {
8775 DNPRINTF(SWM_D_MISC
, "select failed");
8778 restart(NULL
, NULL
);