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
);
835 if (asprintf(&result
, "%s%s", ppwd
->pw_dir
, s
) == -1)
839 errx(1, "expand_tilde: failed to allocate memory.");
845 parse_rgb(const char *rgb
, uint16_t *rr
, uint16_t *gg
, uint16_t *bb
)
847 unsigned int tmpr
, tmpg
, tmpb
;
849 if (sscanf(rgb
, "rgb:%x/%x/%x", &tmpr
, &tmpg
, &tmpb
) != 3)
852 *rr
= RGB_8_TO_16(tmpr
);
853 *gg
= RGB_8_TO_16(tmpg
);
854 *bb
= RGB_8_TO_16(tmpb
);
860 get_screen(int screen
)
862 const xcb_setup_t
*r
;
863 xcb_screen_iterator_t iter
;
865 if ((r
= xcb_get_setup(conn
)) == NULL
) {
866 DNPRINTF(SWM_D_MISC
, "get_screen: xcb_get_setup\n");
870 iter
= xcb_setup_roots_iterator(r
);
871 for (; iter
.rem
; --screen
, xcb_screen_next(&iter
))
881 if (focus_mode
== SWM_FOCUS_DEFAULT
)
882 event_drain(XCB_ENTER_NOTIFY
);
888 get_atom_from_string(const char *str
)
890 xcb_intern_atom_cookie_t c
;
891 xcb_intern_atom_reply_t
*r
;
894 c
= xcb_intern_atom(conn
, 0, strlen(str
), str
);
895 r
= xcb_intern_atom_reply(conn
, c
, NULL
);
903 return (XCB_ATOM_NONE
);
907 set_swm_iconic(struct ws_win
*win
, int newv
)
914 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
915 a_swm_iconic
, XCB_ATOM_INTEGER
, 32, 1, &v
);
917 xcb_delete_property(conn
, win
->id
, a_swm_iconic
);
921 get_swm_iconic(struct ws_win
*win
)
924 xcb_get_property_reply_t
*pr
= NULL
;
926 pr
= xcb_get_property_reply(conn
,
927 xcb_get_property(conn
, 0, win
->id
, a_swm_iconic
,
928 XCB_ATOM_INTEGER
, 0, 1), NULL
);
931 if (pr
->type
!= XCB_ATOM_INTEGER
|| pr
->format
!= 32)
933 v
= *((int32_t *)xcb_get_property_value(pr
));
944 int i
, j
, num_screens
;
946 sup_list
= get_atom_from_string("_NET_SUPPORTED");
948 for (i
= 0; i
< LENGTH(ewmh
); i
++)
949 ewmh
[i
].atom
= get_atom_from_string(ewmh
[i
].name
);
951 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
952 for (i
= 0; i
< num_screens
; i
++) {
953 /* Support check window will be created by workaround(). */
955 /* Report supported atoms */
956 xcb_delete_property(conn
, screens
[i
].root
, sup_list
);
957 for (j
= 0; j
< LENGTH(ewmh
); j
++)
958 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
,
959 screens
[i
].root
, sup_list
, XCB_ATOM_ATOM
, 32, 1,
968 xcb_atom_t sup_check
, sup_list
;
970 xcb_get_property_cookie_t pc
;
971 xcb_get_property_reply_t
*pr
;
973 sup_check
= get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
974 sup_list
= get_atom_from_string("_NET_SUPPORTED");
975 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
977 for (i
= 0; i
< num_screens
; i
++) {
978 /* Get the support check window and destroy it */
979 pc
= xcb_get_property(conn
, 0, screens
[i
].root
, sup_check
,
980 XCB_ATOM_WINDOW
, 0, 1);
981 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
984 if (pr
->format
== sup_check
) {
985 id
= *((xcb_window_t
*)xcb_get_property_value(pr
));
987 xcb_destroy_window(conn
, id
);
988 xcb_delete_property(conn
, screens
[i
].root
, sup_check
);
989 xcb_delete_property(conn
, screens
[i
].root
, sup_list
);
996 ewmh_autoquirk(struct ws_win
*win
)
1000 xcb_get_property_cookie_t c
;
1001 xcb_get_property_reply_t
*r
;
1003 c
= xcb_get_property(conn
, 0, win
->id
,
1004 ewmh
[_NET_WM_WINDOW_TYPE
].atom
, XCB_ATOM_ATOM
, 0, UINT32_MAX
);
1005 r
= xcb_get_property_reply(conn
, c
, NULL
);
1009 n
= xcb_get_property_value_length(r
);
1010 type
= xcb_get_property_value(r
);
1012 for (i
= 0; i
< n
; i
++) {
1013 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_NORMAL
].atom
)
1015 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_DOCK
].atom
||
1016 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_TOOLBAR
].atom
||
1017 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_UTILITY
].atom
) {
1019 win
->quirks
= SWM_Q_FLOAT
| SWM_Q_ANYWHERE
;
1022 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_SPLASH
].atom
||
1023 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_DIALOG
].atom
) {
1025 win
->quirks
= SWM_Q_FLOAT
;
1032 #define SWM_EWMH_ACTION_COUNT_MAX (6)
1033 #define EWMH_F_FULLSCREEN (1<<0)
1034 #define EWMH_F_ABOVE (1<<1)
1035 #define EWMH_F_HIDDEN (1<<2)
1036 #define EWMH_F_SKIP_PAGER (1<<3)
1037 #define EWMH_F_SKIP_TASKBAR (1<<4)
1038 #define SWM_F_MANUAL (1<<5)
1041 ewmh_set_win_fullscreen(struct ws_win
*win
, int fs
)
1049 DNPRINTF(SWM_D_MISC
, "ewmh_set_win_fullscreen: window: 0x%x, "
1050 "fullscreen %s\n", win
->id
, YESNO(fs
));
1053 if (!win
->g_floatvalid
)
1054 store_float_geom(win
, win
->ws
->r
);
1056 win
->g
= win
->ws
->r
->g
;
1059 load_float_geom(win
, win
->ws
->r
);
1066 ewmh_update_actions(struct ws_win
*win
)
1068 xcb_atom_t actions
[SWM_EWMH_ACTION_COUNT_MAX
];
1074 actions
[n
++] = ewmh
[_NET_WM_ACTION_CLOSE
].atom
;
1076 if (win
->floating
) {
1077 actions
[n
++] = ewmh
[_NET_WM_ACTION_MOVE
].atom
;
1078 actions
[n
++] = ewmh
[_NET_WM_ACTION_RESIZE
].atom
;
1079 actions
[n
++] = ewmh
[_NET_WM_ACTION_ABOVE
].atom
;
1082 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
1083 ewmh
[_NET_WM_ALLOWED_ACTIONS
].atom
, XCB_ATOM_ATOM
, 32, 1, actions
);
1086 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
1087 #define _NET_WM_STATE_ADD 1 /* add/set property */
1088 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
1091 ewmh_update_win_state(struct ws_win
*win
, xcb_atom_t state
, long action
)
1093 unsigned int mask
= 0;
1094 unsigned int changed
= 0;
1095 unsigned int orig_flags
;
1100 DNPRINTF(SWM_D_PROP
, "ewmh_update_win_state: window: 0x%x, state: %ld, "
1101 "action: %ld\n", win
->id
, (unsigned long)state
, action
);
1103 if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
)
1104 mask
= EWMH_F_FULLSCREEN
;
1105 else if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
)
1106 mask
= EWMH_F_ABOVE
;
1107 else if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
)
1108 mask
= SWM_F_MANUAL
;
1109 else if (state
== ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
)
1110 mask
= EWMH_F_SKIP_PAGER
;
1111 else if (state
== ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
)
1112 mask
= EWMH_F_SKIP_TASKBAR
;
1114 orig_flags
= win
->ewmh_flags
;
1117 case _NET_WM_STATE_REMOVE
:
1118 win
->ewmh_flags
&= ~mask
;
1120 case _NET_WM_STATE_ADD
:
1121 win
->ewmh_flags
|= mask
;
1123 case _NET_WM_STATE_TOGGLE
:
1124 win
->ewmh_flags
^= mask
;
1128 changed
= (win
->ewmh_flags
& mask
) ^ (orig_flags
& mask
) ? 1 : 0;
1130 if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
) {
1131 if (changed
&& !floating_toggle_win(win
))
1132 win
->ewmh_flags
= orig_flags
; /* revert */
1133 } else if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
) {
1135 win
->manual
= (win
->ewmh_flags
& SWM_F_MANUAL
) != 0;
1136 } else if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
) {
1137 if (changed
&& !ewmh_set_win_fullscreen(win
,
1138 win
->ewmh_flags
& EWMH_F_FULLSCREEN
))
1139 win
->ewmh_flags
= orig_flags
; /* revert */
1142 xcb_delete_property(conn
, win
->id
, ewmh
[_NET_WM_STATE
].atom
);
1144 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
1145 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1146 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1147 &ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
);
1148 else if (win
->ewmh_flags
& EWMH_F_SKIP_PAGER
)
1149 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1150 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1151 &ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
);
1152 else if (win
->ewmh_flags
& EWMH_F_SKIP_TASKBAR
)
1153 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1154 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1155 &ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
);
1156 else if (win
->ewmh_flags
& EWMH_F_ABOVE
)
1157 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1158 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1159 &ewmh
[_NET_WM_STATE_ABOVE
].atom
);
1160 else if (win
->ewmh_flags
& SWM_F_MANUAL
)
1161 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1162 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1163 &ewmh
[_SWM_WM_STATE_MANUAL
].atom
);
1167 ewmh_get_win_state(struct ws_win
*win
)
1170 xcb_get_property_cookie_t c
;
1171 xcb_get_property_reply_t
*r
;
1177 win
->ewmh_flags
= 0;
1179 win
->ewmh_flags
|= EWMH_F_ABOVE
;
1181 win
->ewmh_flags
|= SWM_F_MANUAL
;
1183 c
= xcb_get_property(conn
, 0, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
1184 XCB_ATOM_ATOM
, 0, UINT32_MAX
);
1185 r
= xcb_get_property_reply(conn
, c
, NULL
);
1189 states
= xcb_get_property_value(r
);
1190 n
= xcb_get_property_value_length(r
);
1192 for (i
= 0; i
< n
; i
++)
1193 ewmh_update_win_state(win
, states
[i
], _NET_WM_STATE_ADD
);
1201 dumpwins(struct swm_region
*r
, union arg
*args
)
1205 xcb_get_window_attributes_cookie_t c
;
1206 xcb_get_window_attributes_reply_t
*wa
;
1208 /* suppress unused warning since var is needed */
1211 if (r
->ws
== NULL
) {
1212 warnx("dumpwins: invalid workspace");
1216 warnx("=== managed window list ws %02d ===", r
->ws
->idx
);
1217 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
1218 state
= getstate(win
->id
);
1219 c
= xcb_get_window_attributes(conn
, win
->id
);
1220 wa
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
1222 warnx("window: 0x%x, map_state: %d, state: %u, "
1223 "transient: 0x%x", win
->id
, wa
->map_state
,
1224 state
, win
->transient
);
1227 warnx("window: 0x%x, failed xcb_get_window_attributes",
1231 warnx("===== unmanaged window list =====");
1232 TAILQ_FOREACH(win
, &r
->ws
->unmanagedlist
, entry
) {
1233 state
= getstate(win
->id
);
1234 c
= xcb_get_window_attributes(conn
, win
->id
);
1235 wa
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
1237 warnx("window: 0x%x, map_state: %d, state: %u, "
1238 "transient: 0x%x", win
->id
, wa
->map_state
,
1239 state
, win
->transient
);
1242 warnx("window: 0x%x, failed xcb_get_window_attributes",
1246 warnx("=================================");
1250 dumpwins(struct swm_region
*r
, union arg
*s
)
1255 #endif /* SWM_DEBUG */
1260 int saved_errno
, status
;
1263 saved_errno
= errno
;
1267 while ((pid
= waitpid(WAIT_ANY
, &status
, WNOHANG
)) != 0) {
1272 if (errno
!= ECHILD
)
1273 warn("sighdlr: waitpid");
1274 #endif /* SWM_DEBUG */
1277 if (pid
== searchpid
)
1281 if (WIFEXITED(status
)) {
1282 if (WEXITSTATUS(status
) != 0)
1283 warnx("sighdlr: child exit status: %d",
1284 WEXITSTATUS(status
));
1286 warnx("sighdlr: child is terminated "
1288 #endif /* SWM_DEBUG */
1302 errno
= saved_errno
;
1308 struct pid_e
*p
= NULL
;
1310 DNPRINTF(SWM_D_MISC
, "find_pid: %d\n", pid
);
1315 TAILQ_FOREACH(p
, &pidlist
, entry
) {
1324 name_to_pixel(const char *colorname
)
1326 uint32_t result
= 0;
1327 char cname
[32] = "#";
1328 xcb_screen_t
*screen
;
1329 xcb_colormap_t cmap
;
1330 xcb_alloc_color_reply_t
*cr
;
1331 xcb_alloc_named_color_reply_t
*nr
;
1332 uint16_t rr
, gg
, bb
;
1334 screen
= xcb_setup_roots_iterator(xcb_get_setup(conn
)).data
;
1335 cmap
= screen
->default_colormap
;
1337 /* color is in format rgb://rr/gg/bb */
1338 if (strncmp(colorname
, "rgb:", 4) == 0) {
1339 if (parse_rgb(colorname
, &rr
, &gg
, &bb
) == -1)
1340 warnx("could not parse rgb %s", colorname
);
1342 cr
= xcb_alloc_color_reply(conn
,
1343 xcb_alloc_color(conn
, cmap
, rr
, gg
, bb
),
1349 warnx("color '%s' not found", colorname
);
1352 nr
= xcb_alloc_named_color_reply(conn
,
1353 xcb_alloc_named_color(conn
, cmap
, strlen(colorname
),
1356 strlcat(cname
, colorname
+ 2, sizeof cname
- 1);
1357 nr
= xcb_alloc_named_color_reply(conn
,
1358 xcb_alloc_named_color(conn
, cmap
, strlen(cname
),
1365 warnx("color '%s' not found", colorname
);
1372 setscreencolor(char *val
, int i
, int c
)
1376 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1377 if (i
> 0 && i
<= num_screens
) {
1378 screens
[i
- 1].c
[c
].pixel
= name_to_pixel(val
);
1379 free(screens
[i
- 1].c
[c
].name
);
1380 if ((screens
[i
- 1].c
[c
].name
= strdup(val
)) == NULL
)
1382 } else if (i
== -1) {
1383 for (i
= 0; i
< num_screens
; i
++) {
1384 screens
[i
].c
[c
].pixel
= name_to_pixel(val
);
1385 free(screens
[i
].c
[c
].name
);
1386 if ((screens
[i
].c
[c
].name
= strdup(val
)) == NULL
)
1390 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1395 fancy_stacker(struct workspace
*ws
)
1397 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1398 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1399 snprintf(ws
->stacker
, sizeof ws
->stacker
,
1400 ws
->l_state
.vertical_flip
? "[%d>%d]" : "[%d|%d]",
1401 ws
->l_state
.vertical_mwin
, ws
->l_state
.vertical_stacks
);
1402 if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1403 snprintf(ws
->stacker
, sizeof ws
->stacker
,
1404 ws
->l_state
.horizontal_flip
? "[%dv%d]" : "[%d-%d]",
1405 ws
->l_state
.horizontal_mwin
, ws
->l_state
.horizontal_stacks
);
1409 plain_stacker(struct workspace
*ws
)
1411 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1412 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1413 strlcpy(ws
->stacker
, ws
->l_state
.vertical_flip
? "[>]" : "[|]",
1414 sizeof ws
->stacker
);
1415 if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1416 strlcpy(ws
->stacker
, ws
->l_state
.horizontal_flip
? "[v]" : "[-]",
1417 sizeof ws
->stacker
);
1421 custom_region(char *val
)
1423 unsigned int x
, y
, w
, h
;
1424 int sidx
, num_screens
;
1425 xcb_screen_t
*screen
;
1427 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1428 if (sscanf(val
, "screen[%d]:%ux%u+%u+%u", &sidx
, &w
, &h
, &x
, &y
) != 5)
1429 errx(1, "invalid custom region, "
1430 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1431 if (sidx
< 1 || sidx
> num_screens
)
1432 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1436 if ((screen
= get_screen(sidx
)) == NULL
)
1437 errx(1, "ERROR: can't get screen %d.", sidx
);
1440 errx(1, "region %ux%u+%u+%u too small", w
, h
, x
, y
);
1442 if (x
> screen
->width_in_pixels
||
1443 y
> screen
->height_in_pixels
||
1444 w
+ x
> screen
->width_in_pixels
||
1445 h
+ y
> screen
->height_in_pixels
) {
1446 warnx("ignoring region %ux%u+%u+%u - not within screen "
1447 "boundaries (%ux%u)", w
, h
, x
, y
,
1448 screen
->width_in_pixels
, screen
->height_in_pixels
);
1452 new_region(&screens
[sidx
], x
, y
, w
, h
);
1456 socket_setnonblock(int fd
)
1460 if ((flags
= fcntl(fd
, F_GETFL
, 0)) == -1)
1461 err(1, "fcntl F_GETFL");
1462 flags
|= O_NONBLOCK
;
1463 if ((flags
= fcntl(fd
, F_SETFL
, flags
)) == -1)
1464 err(1, "fcntl F_SETFL");
1468 bar_print_legacy(struct swm_region
*r
, const char *s
)
1470 xcb_rectangle_t rect
;
1475 XRectangle ibox
, lbox
;
1479 XmbTextExtents(bar_fs
, s
, len
, &ibox
, &lbox
);
1481 switch (bar_justify
) {
1482 case SWM_BAR_JUSTIFY_LEFT
:
1485 case SWM_BAR_JUSTIFY_CENTER
:
1486 x
= (WIDTH(r
) - lbox
.width
) / 2;
1488 case SWM_BAR_JUSTIFY_RIGHT
:
1489 x
= WIDTH(r
) - lbox
.width
- SWM_BAR_OFFSET
;
1493 if (x
< SWM_BAR_OFFSET
)
1498 rect
.width
= WIDTH(r
->bar
);
1499 rect
.height
= HEIGHT(r
->bar
);
1501 /* clear back buffer */
1502 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
1503 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
1504 xcb_poly_fill_rectangle(conn
, r
->bar
->buffer
, r
->s
->bar_gc
, 1, &rect
);
1506 /* draw back buffer */
1507 gcvd
.graphics_exposures
= 0;
1508 draw
= XCreateGC(display
, r
->bar
->buffer
, GCGraphicsExposures
, &gcvd
);
1509 XSetForeground(display
, draw
, r
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
);
1510 DRAWSTRING(display
, r
->bar
->buffer
, bar_fs
, draw
,
1511 x
, (bar_fs_extents
->max_logical_extent
.height
- lbox
.height
) / 2 -
1513 XFreeGC(display
, draw
);
1516 xcb_copy_area(conn
, r
->bar
->buffer
, r
->bar
->id
, r
->s
->bar_gc
, 0, 0,
1517 0, 0, WIDTH(r
->bar
), HEIGHT(r
->bar
));
1521 bar_print(struct swm_region
*r
, const char *s
)
1524 xcb_rectangle_t rect
;
1532 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
, &info
);
1534 switch (bar_justify
) {
1535 case SWM_BAR_JUSTIFY_LEFT
:
1538 case SWM_BAR_JUSTIFY_CENTER
:
1539 x
= (WIDTH(r
) - info
.width
) / 2;
1541 case SWM_BAR_JUSTIFY_RIGHT
:
1542 x
= WIDTH(r
) - info
.width
- SWM_BAR_OFFSET
;
1546 if (x
< SWM_BAR_OFFSET
)
1551 rect
.width
= WIDTH(r
->bar
);
1552 rect
.height
= HEIGHT(r
->bar
);
1554 /* clear back buffer */
1555 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
1556 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
1557 xcb_poly_fill_rectangle(conn
, r
->bar
->buffer
, r
->s
->bar_gc
, 1, &rect
);
1559 /* draw back buffer */
1560 draw
= XftDrawCreate(display
, r
->bar
->buffer
,
1561 DefaultVisual(display
, r
->s
->idx
),
1562 DefaultColormap(display
, r
->s
->idx
));
1564 XftDrawStringUtf8(draw
, &bar_font_color
, bar_font
, x
,
1565 (HEIGHT(r
->bar
) + bar_font
->height
) / 2 - bar_font
->descent
,
1568 XftDrawDestroy(draw
);
1571 xcb_copy_area(conn
, r
->bar
->buffer
, r
->bar
->id
, r
->s
->bar_gc
, 0, 0,
1572 0, 0, WIDTH(r
->bar
), HEIGHT(r
->bar
));
1576 bar_extra_stop(void)
1580 bzero(bar_pipe
, sizeof bar_pipe
);
1583 kill(bar_pid
, SIGTERM
);
1586 strlcpy((char *)bar_ext
, "", sizeof bar_ext
);
1591 bar_class_name(char *s
, size_t sz
, struct swm_region
*r
)
1593 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1595 if (r
->ws
->focus
->ch
.class_name
!= NULL
)
1596 strlcat(s
, r
->ws
->focus
->ch
.class_name
, sz
);
1600 bar_title_name(char *s
, size_t sz
, struct swm_region
*r
)
1602 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1604 if (r
->ws
->focus
->ch
.instance_name
!= NULL
)
1605 strlcat(s
, r
->ws
->focus
->ch
.instance_name
, sz
);
1609 bar_class_title_name(char *s
, size_t sz
, struct swm_region
*r
)
1611 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1614 bar_class_name(s
, sz
, r
);
1615 strlcat(s
, ":", sz
);
1616 bar_title_name(s
, sz
, r
);
1620 bar_window_float(char *s
, size_t sz
, struct swm_region
*r
)
1622 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1624 if (r
->ws
->focus
->floating
)
1625 strlcat(s
, "(f)", sz
);
1629 bar_window_name(char *s
, size_t sz
, struct swm_region
*r
)
1633 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1635 if ((title
= get_win_name(r
->ws
->focus
->id
)) == NULL
)
1638 strlcat(s
, title
, sz
);
1642 int urgent
[SWM_WS_MAX
];
1644 bar_urgent(char *s
, size_t sz
)
1647 int i
, j
, num_screens
;
1649 xcb_get_property_cookie_t c
;
1650 xcb_icccm_wm_hints_t hints
;
1652 for (i
= 0; i
< workspace_limit
; i
++)
1655 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1656 for (i
= 0; i
< num_screens
; i
++)
1657 for (j
= 0; j
< workspace_limit
; j
++)
1658 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
) {
1659 c
= xcb_icccm_get_wm_hints(conn
, win
->id
);
1660 if (xcb_icccm_get_wm_hints_reply(conn
, c
,
1663 if (hints
.flags
& XCB_ICCCM_WM_HINT_X_URGENCY
)
1667 for (i
= 0; i
< workspace_limit
; i
++) {
1669 snprintf(b
, sizeof b
, "%d ", i
+ 1);
1671 snprintf(b
, sizeof b
, "- ");
1677 bar_workspace_name(char *s
, size_t sz
, struct swm_region
*r
)
1679 if (r
== NULL
|| r
->ws
== NULL
)
1681 if (r
->ws
->name
!= NULL
)
1682 strlcat(s
, r
->ws
->name
, sz
);
1685 /* build the default bar format according to the defined enabled options */
1687 bar_fmt(const char *fmtexp
, char *fmtnew
, struct swm_region
*r
, size_t sz
)
1689 /* if format provided, just copy the buffers */
1690 if (bar_format
!= NULL
) {
1691 strlcpy(fmtnew
, fmtexp
, sz
);
1695 /* reset the output buffer */
1698 strlcat(fmtnew
, "+N:+I ", sz
);
1700 strlcat(fmtnew
, "+S", sz
);
1701 strlcat(fmtnew
, " ", sz
);
1703 /* only show the workspace name if there's actually one */
1704 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->name
!= NULL
)
1705 strlcat(fmtnew
, "<+D>", sz
);
1706 strlcat(fmtnew
, "+3<", sz
);
1708 if (clock_enabled
) {
1709 strlcat(fmtnew
, fmtexp
, sz
);
1710 strlcat(fmtnew
, "+4<", sz
);
1713 /* bar_urgent already adds the space before the last asterisk */
1715 strlcat(fmtnew
, "* +U*+4<", sz
);
1717 if (title_class_enabled
) {
1718 strlcat(fmtnew
, "+C", sz
);
1719 if (!title_name_enabled
)
1720 strlcat(fmtnew
, "+4<", sz
);
1723 /* checks needed by the colon and floating strlcat(3) calls below */
1724 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->focus
!= NULL
) {
1725 if (title_name_enabled
) {
1726 if (title_class_enabled
)
1727 strlcat(fmtnew
, ":", sz
);
1728 strlcat(fmtnew
, "+T+4<", sz
);
1730 if (window_name_enabled
) {
1731 if (r
->ws
->focus
->floating
)
1732 strlcat(fmtnew
, "+F ", sz
);
1733 strlcat(fmtnew
, "+64W ", sz
);
1737 /* finally add the action script output and the version */
1738 strlcat(fmtnew
, "+4<+A+4<+V", sz
);
1742 bar_replace_pad(char *tmp
, int *limit
, size_t sz
)
1744 /* special case; no limit given, pad one space, instead */
1745 if (*limit
== (int)sz
- 1)
1747 snprintf(tmp
, sz
, "%*s", *limit
, " ");
1750 /* replaces the bar format character sequences (like in tmux(1)) */
1752 bar_replace_seq(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t *offrep
,
1756 char tmp
[SWM_BAR_MAX
];
1760 /* reset strlcat(3) buffer */
1763 /* get number, if any */
1766 if (sscanf(fmt
, "%d%n", &limit
, &size
) != 1)
1767 limit
= sizeof tmp
- 1;
1768 if (limit
<= 0 || limit
>= (int)sizeof tmp
)
1769 limit
= sizeof tmp
- 1;
1771 /* there is nothing to replace (ie EOL) */
1778 bar_replace_pad(tmp
, &limit
, sizeof tmp
);
1781 snprintf(tmp
, sizeof tmp
, "%s", bar_ext
);
1784 bar_class_name(tmp
, sizeof tmp
, r
);
1787 bar_workspace_name(tmp
, sizeof tmp
, r
);
1790 bar_window_float(tmp
, sizeof tmp
, r
);
1793 snprintf(tmp
, sizeof tmp
, "%d", r
->ws
->idx
+ 1);
1796 snprintf(tmp
, sizeof tmp
, "%d", r
->s
->idx
+ 1);
1799 bar_class_title_name(tmp
, sizeof tmp
, r
);
1802 snprintf(tmp
, sizeof tmp
, "%s", r
->ws
->stacker
);
1805 bar_title_name(tmp
, sizeof tmp
, r
);
1808 bar_urgent(tmp
, sizeof tmp
);
1811 snprintf(tmp
, sizeof tmp
, "%s", bar_vertext
);
1814 bar_window_name(tmp
, sizeof tmp
, r
);
1817 /* unknown character sequence; copy as-is */
1818 snprintf(tmp
, sizeof tmp
, "+%c", *fmt
);
1824 if ((int)len
< limit
)
1826 while (limit
-- > 0) {
1827 if (*offrep
>= sz
- 1)
1829 fmtrep
[(*offrep
)++] = *ptr
++;
1837 bar_replace(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t sz
)
1842 while (*fmt
!= '\0') {
1844 /* skip ordinary characters */
1847 fmtrep
[off
++] = *fmt
++;
1851 /* character sequence found; replace it */
1852 fmt
= bar_replace_seq(fmt
, fmtrep
, r
, &off
, sz
);
1861 bar_fmt_expand(char *fmtexp
, size_t sz
)
1868 /* start by grabbing the current time and date */
1870 localtime_r(&tmt
, &tm
);
1872 /* figure out what to expand */
1873 if (bar_format
!= NULL
)
1875 else if (bar_format
== NULL
&& clock_enabled
)
1877 /* if nothing to expand bail out */
1883 /* copy as-is, just in case the format shouldn't be expanded below */
1884 strlcpy(fmtexp
, fmt
, sz
);
1885 /* finally pass the string through strftime(3) */
1886 #ifndef SWM_DENY_CLOCK_FORMAT
1887 if ((len
= strftime(fmtexp
, sz
, fmt
, &tm
)) == 0)
1888 warnx("format too long");
1896 char fmtexp
[SWM_BAR_MAX
], fmtnew
[SWM_BAR_MAX
];
1897 char fmtrep
[SWM_BAR_MAX
];
1899 struct swm_region
*r
;
1901 /* expand the format by first passing it through strftime(3) */
1902 bar_fmt_expand(fmtexp
, sizeof fmtexp
);
1904 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1905 for (i
= 0; i
< num_screens
; i
++) {
1906 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
1909 bar_fmt(fmtexp
, fmtnew
, r
, sizeof fmtnew
);
1910 bar_replace(fmtnew
, fmtrep
, r
, sizeof fmtrep
);
1911 if (bar_font_legacy
)
1912 bar_print_legacy(r
, fmtrep
);
1914 bar_print(r
, fmtrep
);
1927 if (bar_extra
&& bar_extra_running
) {
1928 /* ignore short reads; it'll correct itself */
1929 while ((b
= fgetln(stdin
, &len
)) != NULL
)
1930 if (b
&& b
[len
- 1] == '\n') {
1932 strlcpy((char *)bar_ext
, b
, sizeof bar_ext
);
1934 if (b
== NULL
&& errno
!= EAGAIN
) {
1935 warn("bar_update: bar_extra failed");
1939 strlcpy((char *)bar_ext
, "", sizeof bar_ext
);
1948 /* suppress unused warning since var is needed */
1955 bar_toggle(struct swm_region
*r
, union arg
*args
)
1957 struct swm_region
*tmpr
;
1960 /* suppress unused warnings since vars are needed */
1964 DNPRINTF(SWM_D_BAR
, "bar_toggle\n");
1966 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1968 for (i
= 0; i
< num_screens
; i
++)
1969 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
1971 xcb_unmap_window(conn
, tmpr
->bar
->id
);
1973 for (i
= 0; i
< num_screens
; i
++)
1974 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
1976 xcb_map_window(conn
, tmpr
->bar
->id
);
1979 bar_enabled
= !bar_enabled
;
1983 /* must be after stack */
1992 struct swm_region
*r
;
1996 /* do this here because the conf file is in memory */
1997 if (bar_extra
&& !bar_extra_running
&& bar_argv
[0]) {
1998 /* launch external status app */
1999 bar_extra_running
= 1;
2000 if (pipe(bar_pipe
) == -1)
2001 err(1, "pipe error");
2002 socket_setnonblock(bar_pipe
[0]);
2003 socket_setnonblock(bar_pipe
[1]); /* XXX hmmm, really? */
2004 if (dup2(bar_pipe
[0], 0) == -1)
2006 if (dup2(bar_pipe
[1], 1) == -1)
2008 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
2009 err(1, "could not disable SIGPIPE");
2010 switch (bar_pid
= fork()) {
2012 err(1, "cannot fork");
2016 execvp(bar_argv
[0], bar_argv
);
2017 err(1, "%s external app failed", bar_argv
[0]);
2019 default: /* parent */
2025 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2026 for (i
= 0; i
< num_screens
; i
++)
2027 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
2030 wa
[0] = screens
[i
].c
[SWM_S_COLOR_BAR
].pixel
;
2031 wa
[1] = screens
[i
].c
[SWM_S_COLOR_BAR_BORDER
].pixel
;
2032 xcb_change_window_attributes(conn
, r
->bar
->id
,
2033 XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
, wa
);
2043 while ((s
= index(s
, '-'))) {
2048 return (count
== 14);
2054 char *default_string
;
2055 char **missing_charsets
;
2056 int num_missing_charsets
= 0;
2060 XFreeFontSet(display
, bar_fs
);
2064 DNPRINTF(SWM_D_INIT
, "fontset_init: loading bar_fonts: %s\n", bar_fonts
);
2066 bar_fs
= XCreateFontSet(display
, bar_fonts
, &missing_charsets
,
2067 &num_missing_charsets
, &default_string
);
2069 if (num_missing_charsets
> 0) {
2070 warnx("Unable to load charset(s):");
2072 for (i
= 0; i
< num_missing_charsets
; ++i
)
2073 warnx("%s", missing_charsets
[i
]);
2075 XFreeStringList(missing_charsets
);
2077 if (strcmp(default_string
, ""))
2078 warnx("Glyphs from those sets will be replaced "
2079 "by '%s'.", default_string
);
2081 warnx("Glyphs from those sets won't be drawn.");
2085 errx(1, "Error creating font set structure.");
2087 bar_fs_extents
= XExtentsOfFontSet(bar_fs
);
2089 bar_height
= bar_fs_extents
->max_logical_extent
.height
+
2090 2 * bar_border_width
;
2097 xft_init(struct swm_region
*r
)
2099 char *font
, *d
, *search
;
2102 if (bar_font
== NULL
) {
2103 if ((d
= strdup(bar_fonts
)) == NULL
)
2104 errx(1, "insufficient memory.");
2106 while ((font
= strsep(&search
, ",")) != NULL
) {
2110 DNPRINTF(SWM_D_INIT
, "xft_init: try font %s\n", font
);
2113 bar_font
= XftFontOpenXlfd(display
, r
->s
->idx
,
2116 bar_font
= XftFontOpenName(display
, r
->s
->idx
,
2121 warnx("unable to load font %s", font
);
2124 DNPRINTF(SWM_D_INIT
, "successfully opened "
2132 if (bar_font
== NULL
)
2133 errx(1, "unable to open a font");
2135 PIXEL_TO_XRENDERCOLOR(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
, color
);
2137 if (!XftColorAllocValue(display
, DefaultVisual(display
, r
->s
->idx
),
2138 DefaultColormap(display
, r
->s
->idx
), &color
, &bar_font_color
))
2139 warn("unable to allocate Xft color");
2141 bar_height
= bar_font
->height
+ 2 * bar_border_width
;
2148 bar_setup(struct swm_region
*r
)
2150 xcb_screen_t
*screen
;
2153 DNPRINTF(SWM_D_BAR
, "bar_setup: screen %d.\n",
2156 if ((screen
= get_screen(r
->s
->idx
)) == NULL
)
2157 errx(1, "ERROR: can't get screen %d.", r
->s
->idx
);
2162 if ((r
->bar
= calloc(1, sizeof(struct swm_bar
))) == NULL
)
2163 err(1, "bar_setup: calloc: failed to allocate memory.");
2165 if (bar_font_legacy
)
2171 Y(r
->bar
) = bar_at_bottom
? (Y(r
) + HEIGHT(r
) - bar_height
) : Y(r
);
2172 WIDTH(r
->bar
) = WIDTH(r
) - 2 * bar_border_width
;
2173 HEIGHT(r
->bar
) = bar_height
- 2 * bar_border_width
;
2175 r
->bar
->id
= xcb_generate_id(conn
);
2176 wa
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
2177 wa
[1] = r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].pixel
;
2178 wa
[2] = XCB_EVENT_MASK_EXPOSURE
;
2180 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, r
->bar
->id
, r
->s
->root
,
2181 X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
), HEIGHT(r
->bar
),
2182 bar_border_width
, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
2183 XCB_COPY_FROM_PARENT
, XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
2184 | XCB_CW_EVENT_MASK
, wa
);
2186 r
->bar
->buffer
= xcb_generate_id(conn
);
2187 xcb_create_pixmap(conn
, screen
->root_depth
, r
->bar
->buffer
, r
->bar
->id
,
2188 WIDTH(r
->bar
), HEIGHT(r
->bar
));
2191 xcb_randr_select_input(conn
, r
->bar
->id
,
2192 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE
);
2195 xcb_map_window(conn
, r
->bar
->id
);
2197 DNPRINTF(SWM_D_BAR
, "bar_setup: window: 0x%x, (x,y) w x h: (%d,%d) "
2198 "%d x %d\n", WINID(r
->bar
), X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
),
2201 if (signal(SIGALRM
, bar_signal
) == SIG_ERR
)
2202 err(1, "could not install bar_signal");
2207 bar_cleanup(struct swm_region
*r
)
2211 xcb_destroy_window(conn
, r
->bar
->id
);
2212 xcb_free_pixmap(conn
, r
->bar
->buffer
);
2218 set_win_state(struct ws_win
*win
, uint16_t state
)
2220 uint16_t data
[2] = { state
, XCB_ATOM_NONE
};
2222 DNPRINTF(SWM_D_EVENT
, "set_win_state: window: 0x%x, state: %u\n",
2228 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
, a_state
,
2229 a_state
, 32, 2, data
);
2233 getstate(xcb_window_t w
)
2235 uint32_t result
= 0;
2236 xcb_get_property_cookie_t c
;
2237 xcb_get_property_reply_t
*r
;
2239 c
= xcb_get_property(conn
, 0, w
, a_state
, a_state
, 0L, 2L);
2240 r
= xcb_get_property_reply(conn
, c
, NULL
);
2243 if (r
->type
== a_state
&& r
->format
== 32 && r
->length
== 2)
2244 result
= *((uint32_t *)xcb_get_property_value(r
));
2248 DNPRINTF(SWM_D_MISC
, "getstate property: win 0x%x state %u\n", w
,
2254 version(struct swm_region
*r
, union arg
*args
)
2256 /* suppress unused warnings since vars are needed */
2260 bar_version
= !bar_version
;
2262 snprintf(bar_vertext
, sizeof bar_vertext
,
2263 "Version: %s Build: %s", SPECTRWM_VERSION
, buildstr
);
2265 strlcpy(bar_vertext
, "", sizeof bar_vertext
);
2272 client_msg(struct ws_win
*win
, xcb_atom_t a
, xcb_timestamp_t t
)
2274 xcb_client_message_event_t ev
;
2282 name
= get_atom_name(a
);
2283 DNPRINTF(SWM_D_EVENT
, "client_msg: window: 0x%x, atom: %s(%u), "
2285 win
->id
, name
, a
, t
);
2289 bzero(&ev
, sizeof ev
);
2290 ev
.response_type
= XCB_CLIENT_MESSAGE
;
2291 ev
.window
= win
->id
;
2294 ev
.data
.data32
[0] = a
;
2295 ev
.data
.data32
[1] = t
;
2297 xcb_send_event(conn
, 0, win
->id
,
2298 XCB_EVENT_MASK_NO_EVENT
, (const char *)&ev
);
2301 /* synthetic response to a ConfigureRequest when not making a change */
2303 config_win(struct ws_win
*win
, xcb_configure_request_event_t
*ev
)
2305 xcb_configure_notify_event_t ce
;
2310 /* send notification of unchanged state. */
2311 bzero(&ce
, sizeof(ce
));
2312 ce
.response_type
= XCB_CONFIGURE_NOTIFY
;
2315 ce
.width
= WIDTH(win
);
2316 ce
.height
= HEIGHT(win
);
2317 ce
.override_redirect
= 0;
2322 ce
.window
= win
->id
;
2323 ce
.border_width
= BORDER(win
);
2324 ce
.above_sibling
= XCB_WINDOW_NONE
;
2327 ce
.event
= ev
->window
;
2328 ce
.window
= ev
->window
;
2330 /* make response appear more WM_SIZE_HINTS-compliant */
2331 if (win
->sh
.flags
) {
2332 DNPRINTF(SWM_D_MISC
, "config_win: hints: window: 0x%x,"
2333 " sh.flags: %u, min: %d x %d, max: %d x %d, inc: "
2334 "%d x %d\n", win
->id
, win
->sh
.flags
, SH_MIN_W(win
),
2335 SH_MIN_H(win
), SH_MAX_W(win
), SH_MAX_H(win
),
2336 SH_INC_W(win
), SH_INC_H(win
));
2341 /* the hint may be set... to 0! */
2342 if (SH_MIN_W(win
) > 0 && ce
.width
< SH_MIN_W(win
))
2343 ce
.width
= SH_MIN_W(win
);
2344 if (SH_MIN_H(win
) > 0 && ce
.height
< SH_MIN_H(win
))
2345 ce
.height
= SH_MIN_H(win
);
2350 /* may also be advertized as 0 */
2351 if (SH_MAX_W(win
) > 0 && ce
.width
> SH_MAX_W(win
))
2352 ce
.width
= SH_MAX_W(win
);
2353 if (SH_MAX_H(win
) > 0 && ce
.height
> SH_MAX_H(win
))
2354 ce
.height
= SH_MAX_H(win
);
2357 /* resize increment. */
2359 if (SH_INC_W(win
) > 1 && ce
.width
> SH_INC_W(win
))
2360 ce
.width
-= (ce
.width
- SH_MIN_W(win
)) %
2362 if (SH_INC_H(win
) > 1 && ce
.height
> SH_INC_H(win
))
2363 ce
.height
-= (ce
.height
- SH_MIN_H(win
)) %
2367 /* adjust x and y for requested border_width. */
2368 ce
.x
+= BORDER(win
) - ev
->border_width
;
2369 ce
.y
+= BORDER(win
) - ev
->border_width
;
2370 ce
.border_width
= ev
->border_width
;
2371 ce
.above_sibling
= ev
->sibling
;
2374 DNPRINTF(SWM_D_MISC
, "config_win: ewmh: %s, window: 0x%x, (x,y) w x h: "
2375 "(%d,%d) %d x %d, border: %d\n", YESNO(ev
== NULL
), win
->id
, ce
.x
,
2376 ce
.y
, ce
.width
, ce
.height
, ce
.border_width
);
2378 xcb_send_event(conn
, 0, win
->id
, XCB_EVENT_MASK_STRUCTURE_NOTIFY
,
2383 count_win(struct workspace
*ws
, int count_transient
)
2388 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
2389 if (!count_transient
&& win
->floating
)
2391 if (!count_transient
&& win
->transient
)
2397 DNPRINTF(SWM_D_MISC
, "count_win: %d\n", count
);
2403 quit(struct swm_region
*r
, union arg
*args
)
2405 /* suppress unused warnings since vars are needed */
2409 DNPRINTF(SWM_D_MISC
, "quit\n");
2414 map_window(struct ws_win
*win
)
2416 uint32_t val
= XCB_STACK_MODE_ABOVE
;
2421 DNPRINTF(SWM_D_EVENT
, "map_window: win 0x%x, mapped: %s\n", win
->id
,
2422 YESNO(win
->mapped
));
2424 xcb_configure_window(conn
, win
->id
,
2425 XCB_CONFIG_WINDOW_STACK_MODE
, &val
);
2430 xcb_map_window(conn
, win
->id
);
2431 set_win_state(win
, XCB_ICCCM_WM_STATE_NORMAL
);
2436 unmap_window(struct ws_win
*win
)
2441 DNPRINTF(SWM_D_EVENT
, "unmap_window: win 0x%x, mapped: %s\n", win
->id
,
2442 YESNO(win
->mapped
));
2447 xcb_unmap_window(conn
, win
->id
);
2448 set_win_state(win
, XCB_ICCCM_WM_STATE_ICONIC
);
2456 int i
, j
, num_screens
;
2458 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2459 for (i
= 0; i
< num_screens
; i
++)
2460 for (j
= 0; j
< workspace_limit
; j
++)
2461 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2466 fake_keypress(struct ws_win
*win
, xcb_keysym_t keysym
, uint16_t modifiers
)
2468 xcb_key_press_event_t event
;
2469 xcb_keycode_t
*keycode
;
2474 keycode
= xcb_key_symbols_get_keycode(syms
, keysym
);
2476 DNPRINTF(SWM_D_MISC
, "fake_keypress: win 0x%x keycode %u\n",
2479 bzero(&event
, sizeof(event
));
2480 event
.event
= win
->id
;
2481 event
.root
= win
->s
->root
;
2482 event
.child
= XCB_WINDOW_NONE
;
2483 event
.time
= XCB_CURRENT_TIME
;
2484 event
.event_x
= X(win
);
2485 event
.event_y
= Y(win
);
2488 event
.same_screen
= 1;
2489 event
.detail
= *keycode
;
2490 event
.state
= modifiers
;
2492 event
.response_type
= XCB_KEY_PRESS
;
2493 xcb_send_event(conn
, 1, win
->id
,
2494 XCB_EVENT_MASK_KEY_PRESS
, (const char *)&event
);
2496 event
.response_type
= XCB_KEY_RELEASE
;
2497 xcb_send_event(conn
, 1, win
->id
,
2498 XCB_EVENT_MASK_KEY_RELEASE
, (const char *)&event
);
2504 restart(struct swm_region
*r
, union arg
*args
)
2506 /* suppress unused warning since var is needed */
2510 DNPRINTF(SWM_D_MISC
, "restart: %s\n", start_argv
[0]);
2514 execvp(start_argv
[0], start_argv
);
2515 warn("execvp failed");
2520 get_pointer_win(xcb_window_t root
)
2522 struct ws_win
*win
= NULL
;
2523 xcb_query_pointer_reply_t
*r
;
2525 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: root: 0x%x.\n", root
);
2527 r
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, root
), NULL
);
2529 win
= find_window(r
->child
);
2531 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: 0x%x.\n",
2534 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: none.\n");
2542 root_to_region(xcb_window_t root
, int check
)
2545 struct swm_region
*r
= NULL
;
2547 xcb_query_pointer_reply_t
*qpr
;
2548 xcb_get_input_focus_reply_t
*gifr
;
2550 DNPRINTF(SWM_D_MISC
, "root_to_region: window: 0x%x\n", root
);
2552 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2553 for (i
= 0; i
< num_screens
; i
++)
2554 if (screens
[i
].root
== root
)
2557 if (check
& SWM_CK_FOCUS
) {
2558 /* Try to find an actively focused window */
2559 gifr
= xcb_get_input_focus_reply(conn
,
2560 xcb_get_input_focus(conn
), NULL
);
2562 cfw
= find_window(gifr
->focus
);
2563 if (cfw
&& cfw
->ws
->r
)
2570 if (r
== NULL
&& check
& SWM_CK_POINTER
) {
2571 /* No region with an active focus; try to use pointer. */
2572 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
,
2573 screens
[i
].root
), NULL
);
2576 DNPRINTF(SWM_D_MISC
, "root_to_region: pointer: "
2577 "(%d,%d)\n", qpr
->root_x
, qpr
->root_y
);
2578 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2579 if (X(r
) <= qpr
->root_x
&&
2580 qpr
->root_x
< MAX_X(r
) &&
2581 Y(r
) <= qpr
->root_y
&&
2582 qpr
->root_y
< MAX_Y(r
))
2589 if (r
== NULL
&& check
& SWM_CK_FALLBACK
)
2590 r
= TAILQ_FIRST(&screens
[i
].rl
);
2596 find_unmanaged_window(xcb_window_t id
)
2599 int i
, j
, num_screens
;
2601 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2602 for (i
= 0; i
< num_screens
; i
++)
2603 for (j
= 0; j
< workspace_limit
; j
++)
2604 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].unmanagedlist
,
2612 find_window(xcb_window_t id
)
2615 int i
, j
, num_screens
;
2616 xcb_query_tree_reply_t
*r
;
2618 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2619 for (i
= 0; i
< num_screens
; i
++)
2620 for (j
= 0; j
< workspace_limit
; j
++)
2621 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2625 r
= xcb_query_tree_reply(conn
, xcb_query_tree(conn
, id
), NULL
);
2629 /* if we were looking for the parent return that window instead */
2630 if (r
->parent
== 0 || r
->root
== r
->parent
) {
2635 /* look for parent */
2636 for (i
= 0; i
< num_screens
; i
++)
2637 for (j
= 0; j
< workspace_limit
; j
++)
2638 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2639 if (r
->parent
== win
->id
) {
2649 spawn(int ws_idx
, union arg
*args
, int close_fd
)
2654 DNPRINTF(SWM_D_MISC
, "spawn: %s\n", args
->argv
[0]);
2656 close(xcb_get_file_descriptor(conn
));
2658 setenv("LD_PRELOAD", SWM_LIB
, 1);
2660 if (asprintf(&ret
, "%d", ws_idx
) == -1) {
2661 warn("spawn: asprintf SWM_WS");
2664 setenv("_SWM_WS", ret
, 1);
2668 if (asprintf(&ret
, "%d", getpid()) == -1) {
2669 warn("spawn: asprintf _SWM_PID");
2672 setenv("_SWM_PID", ret
, 1);
2676 if (setsid() == -1) {
2677 warn("spawn: setsid");
2683 * close stdin and stdout to prevent interaction between apps
2684 * and the baraction script
2685 * leave stderr open to record errors
2687 if ((fd
= open(_PATH_DEVNULL
, O_RDWR
, 0)) == -1) {
2688 warn("spawn: open");
2691 dup2(fd
, STDIN_FILENO
);
2692 dup2(fd
, STDOUT_FILENO
);
2697 execvp(args
->argv
[0], args
->argv
);
2699 warn("spawn: execvp");
2704 kill_refs(struct ws_win
*win
)
2706 int i
, x
, num_screens
;
2707 struct swm_region
*r
;
2708 struct workspace
*ws
;
2713 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2714 for (i
= 0; i
< num_screens
; i
++)
2715 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2716 for (x
= 0; x
< workspace_limit
; x
++) {
2718 if (win
== ws
->focus
)
2720 if (win
== ws
->focus_prev
)
2721 ws
->focus_prev
= NULL
;
2726 validate_win(struct ws_win
*testwin
)
2729 struct workspace
*ws
;
2730 struct swm_region
*r
;
2731 int i
, x
, num_screens
;
2733 if (testwin
== NULL
)
2736 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2737 for (i
= 0; i
< num_screens
; i
++)
2738 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2739 for (x
= 0; x
< workspace_limit
; x
++) {
2741 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
2749 validate_ws(struct workspace
*testws
)
2751 struct swm_region
*r
;
2752 struct workspace
*ws
;
2753 int i
, x
, num_screens
;
2755 /* validate all ws */
2756 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2757 for (i
= 0; i
< num_screens
; i
++)
2758 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2759 for (x
= 0; x
< workspace_limit
; x
++) {
2768 unfocus_win(struct ws_win
*win
)
2770 xcb_window_t none
= XCB_WINDOW_NONE
;
2772 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: window: 0x%x\n", WINID(win
));
2776 if (win
->ws
== NULL
) {
2777 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: NULL ws.\n");
2781 if (validate_ws(win
->ws
)) {
2782 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: invalid ws.\n");
2786 if (win
->ws
->r
== NULL
) {
2787 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: NULL region.\n");
2791 if (validate_win(win
)) {
2792 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: invalid win.\n");
2797 if (win
->ws
->focus
== win
) {
2798 win
->ws
->focus
= NULL
;
2799 win
->ws
->focus_prev
= win
;
2802 if (validate_win(win
->ws
->focus
)) {
2803 kill_refs(win
->ws
->focus
);
2804 win
->ws
->focus
= NULL
;
2806 if (validate_win(win
->ws
->focus_prev
)) {
2807 kill_refs(win
->ws
->focus_prev
);
2808 win
->ws
->focus_prev
= NULL
;
2811 xcb_change_window_attributes(conn
, win
->id
, XCB_CW_BORDER_PIXEL
,
2812 &win
->ws
->r
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
);
2814 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
2815 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1, &none
);
2817 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: done.\n");
2821 focus_win(struct ws_win
*win
)
2823 struct ws_win
*cfw
= NULL
, *parent
= NULL
, *w
;
2824 struct workspace
*ws
;
2825 xcb_get_input_focus_reply_t
*r
;
2827 DNPRINTF(SWM_D_FOCUS
, "focus_win: window: 0x%x\n", WINID(win
));
2832 if (win
->ws
== NULL
)
2837 if (validate_ws(ws
))
2840 if (validate_win(win
)) {
2845 r
= xcb_get_input_focus_reply(conn
, xcb_get_input_focus(conn
), NULL
);
2847 cfw
= find_window(r
->focus
);
2853 if (ws
->focus
!= win
) {
2854 if (ws
->focus
&& ws
->focus
!= cfw
)
2855 unfocus_win(ws
->focus
);
2859 /* If this window directs focus to a child window, then clear. */
2860 if (win
->focus_child
)
2861 win
->focus_child
= NULL
;
2863 /* If transient, adjust parent's focus child for focus_magic. */
2864 if (win
->transient
) {
2865 parent
= find_window(win
->transient
);
2866 if (parent
&& parent
->focus_child
!= win
)
2867 parent
->focus_child
= win
;
2870 if (cfw
!= win
&& ws
->r
!= NULL
) {
2871 /* Set input focus if no input hint, or indicated by hint. */
2872 if (!(win
->hints
.flags
& XCB_ICCCM_WM_HINT_INPUT
) ||
2873 (win
->hints
.flags
& XCB_ICCCM_WM_HINT_INPUT
&&
2875 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
,
2876 win
->id
, last_event_time
);
2878 /* Tell app it can adjust focus to a specific window. */
2879 if (win
->take_focus
) {
2880 /* java is special; always tell parent */
2881 if (win
->transient
&& win
->java
)
2882 client_msg(parent
, a_takefocus
,
2885 client_msg(win
, a_takefocus
, last_event_time
);
2888 xcb_change_window_attributes(conn
, win
->id
, XCB_CW_BORDER_PIXEL
,
2889 &ws
->r
->s
->c
[SWM_S_COLOR_FOCUS
].pixel
);
2891 if (ws
->cur_layout
->flags
& SWM_L_MAPONFOCUS
||
2893 /* If a parent exists, map it first. */
2897 /* Map siblings next. */
2898 TAILQ_FOREACH(w
, &ws
->winlist
, entry
)
2899 if (w
!= win
&& !w
->iconic
&&
2900 w
->transient
== parent
->id
)
2904 /* Map focused window. */
2907 /* Finally, map children of focus window. */
2908 TAILQ_FOREACH(w
, &ws
->winlist
, entry
)
2909 if (w
->transient
== win
->id
&& !w
->iconic
)
2914 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
2915 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1,
2922 DNPRINTF(SWM_D_FOCUS
, "focus_win: done.\n");
2925 /* If a child window should have focus instead, return it. */
2927 get_focus_magic(struct ws_win
*win
)
2929 struct ws_win
*parent
= NULL
;
2930 struct ws_win
*child
= NULL
;
2932 DNPRINTF(SWM_D_FOCUS
, "get_focus_magic: window: 0x%x\n", WINID(win
));
2936 if (win
->transient
) {
2937 parent
= find_window(win
->transient
);
2939 /* If parent prefers focus elsewhere, then try to do so. */
2940 if (parent
&& (child
= parent
->focus_child
)) {
2941 if (validate_win(child
) == 0 && child
->mapped
)
2944 parent
->focus_child
= NULL
;
2948 /* If this window prefers focus elsewhere, then try to do so. */
2949 if ((child
= win
->focus_child
)) {
2950 if (validate_win(child
) == 0 && child
->mapped
)
2953 win
->focus_child
= NULL
;
2960 event_drain(uint8_t rt
)
2962 xcb_generic_event_t
*evt
;
2964 /* ensure all pending requests have been processed before filtering. */
2966 while ((evt
= xcb_poll_for_event(conn
))) {
2967 if (XCB_EVENT_RESPONSE_TYPE(evt
) != rt
)
2975 switchws(struct swm_region
*r
, union arg
*args
)
2977 int wsid
= args
->id
, unmap_old
= 0;
2978 struct swm_region
*this_r
, *other_r
;
2980 struct workspace
*new_ws
, *old_ws
;
2985 if (wsid
>= workspace_limit
)
2989 old_ws
= this_r
->ws
;
2990 new_ws
= &this_r
->s
->ws
[wsid
];
2992 DNPRINTF(SWM_D_WS
, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
2993 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), old_ws
->idx
, wsid
);
2995 if (new_ws
== NULL
|| old_ws
== NULL
)
2997 if (new_ws
== old_ws
)
3000 unfocus_win(old_ws
->focus
);
3002 other_r
= new_ws
->r
;
3003 if (other_r
== NULL
) {
3004 /* the other workspace is hidden, hide this one */
3008 /* the other ws is visible in another region, exchange them */
3009 other_r
->ws_prior
= new_ws
;
3010 other_r
->ws
= old_ws
;
3011 old_ws
->r
= other_r
;
3013 this_r
->ws_prior
= old_ws
;
3014 this_r
->ws
= new_ws
;
3017 /* Set focus_pending before stacking. */
3018 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
3019 new_ws
->focus_pending
= get_region_focus(new_ws
->r
);
3023 /* unmap old windows */
3025 TAILQ_FOREACH(win
, &old_ws
->winlist
, entry
)
3028 /* if workspaces were swapped, then don't wait to set focus */
3029 if (old_ws
->r
&& focus_mode
!= SWM_FOCUS_FOLLOW
) {
3030 if (new_ws
->focus_pending
) {
3031 focus_win(new_ws
->focus_pending
);
3033 /* Empty region, focus on root. */
3034 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
,
3035 new_ws
->r
->s
[new_ws
->r
->s
->idx
].root
,
3040 /* Clear bar if new ws is empty. */
3041 if (new_ws
->focus_pending
== NULL
)
3046 DNPRINTF(SWM_D_WS
, "switchws: done.\n");
3050 cyclews(struct swm_region
*r
, union arg
*args
)
3053 struct swm_screen
*s
= r
->s
;
3056 DNPRINTF(SWM_D_WS
, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3057 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
3062 case SWM_ARG_ID_CYCLEWS_UP_ALL
:
3065 case SWM_ARG_ID_CYCLEWS_UP
:
3066 if (a
.id
< workspace_limit
- 1)
3071 case SWM_ARG_ID_CYCLEWS_DOWN_ALL
:
3074 case SWM_ARG_ID_CYCLEWS_DOWN
:
3078 a
.id
= workspace_limit
- 1;
3085 (!cycle_empty
&& TAILQ_EMPTY(&s
->ws
[a
.id
].winlist
)))
3087 if (!cycle_visible
&& s
->ws
[a
.id
].r
!= NULL
)
3091 } while (a
.id
!= r
->ws
->idx
);
3095 priorws(struct swm_region
*r
, union arg
*args
)
3101 DNPRINTF(SWM_D_WS
, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3102 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
3104 if (r
->ws_prior
== NULL
)
3107 a
.id
= r
->ws_prior
->idx
;
3112 cyclescr(struct swm_region
*r
, union arg
*args
)
3115 struct swm_region
*rr
= NULL
;
3118 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
3119 /* do nothing if we don't have more than one screen */
3120 if (!(num_screens
> 1 || outputs
> 1))
3125 case SWM_ARG_ID_CYCLESC_UP
:
3126 rr
= TAILQ_NEXT(r
, entry
);
3128 rr
= TAILQ_FIRST(&screens
[i
].rl
);
3130 case SWM_ARG_ID_CYCLESC_DOWN
:
3131 rr
= TAILQ_PREV(r
, swm_region_list
, entry
);
3133 rr
= TAILQ_LAST(&screens
[i
].rl
, swm_region_list
);
3141 nfw
= get_region_focus(rr
);
3145 /* New region is empty; unfocus old region and warp pointer. */
3146 unfocus_win(r
->ws
->focus
);
3147 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
,
3148 rr
->s
[i
].root
, XCB_CURRENT_TIME
);
3150 /* Clear bar since empty. */
3158 sort_windows(struct ws_win_list
*wl
)
3160 struct ws_win
*win
, *parent
, *nxt
;
3165 for (win
= TAILQ_FIRST(wl
); win
!= TAILQ_END(wl
); win
= nxt
) {
3166 nxt
= TAILQ_NEXT(win
, entry
);
3167 if (win
->transient
) {
3168 parent
= find_window(win
->transient
);
3169 if (parent
== NULL
) {
3170 warnx("not possible bug");
3173 TAILQ_REMOVE(wl
, win
, entry
);
3174 TAILQ_INSERT_AFTER(wl
, parent
, win
, entry
);
3181 swapwin(struct swm_region
*r
, union arg
*args
)
3183 struct ws_win
*target
, *source
;
3184 struct ws_win
*cur_focus
;
3185 struct ws_win_list
*wl
;
3187 DNPRINTF(SWM_D_WS
, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3188 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
3190 cur_focus
= r
->ws
->focus
;
3191 if (cur_focus
== NULL
)
3195 wl
= &source
->ws
->winlist
;
3198 case SWM_ARG_ID_SWAPPREV
:
3199 if (source
->transient
)
3200 source
= find_window(source
->transient
);
3201 target
= TAILQ_PREV(source
, ws_win_list
, entry
);
3202 if (target
&& target
->transient
)
3203 target
= find_window(target
->transient
);
3204 TAILQ_REMOVE(wl
, source
, entry
);
3206 TAILQ_INSERT_TAIL(wl
, source
, entry
);
3208 TAILQ_INSERT_BEFORE(target
, source
, entry
);
3210 case SWM_ARG_ID_SWAPNEXT
:
3211 target
= TAILQ_NEXT(source
, entry
);
3212 /* move the parent and let the sort handle the move */
3213 if (source
->transient
)
3214 source
= find_window(source
->transient
);
3215 TAILQ_REMOVE(wl
, source
, entry
);
3217 TAILQ_INSERT_HEAD(wl
, source
, entry
);
3219 TAILQ_INSERT_AFTER(wl
, target
, source
, entry
);
3221 case SWM_ARG_ID_SWAPMAIN
:
3222 target
= TAILQ_FIRST(wl
);
3223 if (target
== source
) {
3224 if (source
->ws
->focus_prev
!= NULL
&&
3225 source
->ws
->focus_prev
!= target
)
3226 source
= source
->ws
->focus_prev
;
3230 if (target
== NULL
|| source
== NULL
)
3232 source
->ws
->focus_prev
= target
;
3233 TAILQ_REMOVE(wl
, target
, entry
);
3234 TAILQ_INSERT_BEFORE(source
, target
, entry
);
3235 TAILQ_REMOVE(wl
, source
, entry
);
3236 TAILQ_INSERT_HEAD(wl
, source
, entry
);
3238 case SWM_ARG_ID_MOVELAST
:
3239 TAILQ_REMOVE(wl
, source
, entry
);
3240 TAILQ_INSERT_TAIL(wl
, source
, entry
);
3243 DNPRINTF(SWM_D_MOVE
, "swapwin: invalid id: %d\n", args
->id
);
3255 get_focus_prev(struct ws_win
*win
)
3257 struct ws_win
*winfocus
= NULL
;
3258 struct ws_win
*cur_focus
= NULL
;
3259 struct ws_win_list
*wl
= NULL
;
3260 struct workspace
*ws
= NULL
;
3262 if (!(win
&& win
->ws
))
3267 cur_focus
= ws
->focus
;
3269 DNPRINTF(SWM_D_FOCUS
, "get_focus_prev: window: 0x%x, cur_focus: 0x%x\n",
3270 WINID(win
), WINID(cur_focus
));
3272 /* pickle, just focus on whatever */
3273 if (cur_focus
== NULL
) {
3274 /* use prev_focus if valid */
3275 if (ws
->focus_prev
&& ws
->focus_prev
!= cur_focus
&&
3276 find_window(WINID(ws
->focus_prev
)))
3277 winfocus
= ws
->focus_prev
;
3281 /* if transient focus on parent */
3282 if (cur_focus
->transient
) {
3283 winfocus
= find_window(cur_focus
->transient
);
3287 /* if in max_stack try harder */
3288 if ((win
->quirks
& SWM_Q_FOCUSPREV
) ||
3289 (ws
->cur_layout
->flags
& SWM_L_FOCUSPREV
)) {
3290 if (cur_focus
!= ws
->focus_prev
)
3291 winfocus
= ws
->focus_prev
;
3293 winfocus
= TAILQ_PREV(win
, ws_win_list
, entry
);
3298 DNPRINTF(SWM_D_FOCUS
, "get_focus_prev: focus_close: %d\n", focus_close
);
3300 if (winfocus
== NULL
|| winfocus
== win
) {
3301 switch (focus_close
) {
3302 case SWM_STACK_BOTTOM
:
3303 winfocus
= TAILQ_FIRST(wl
);
3306 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
3308 case SWM_STACK_ABOVE
:
3309 if ((winfocus
= TAILQ_NEXT(cur_focus
, entry
)) == NULL
) {
3310 if (focus_close_wrap
)
3311 winfocus
= TAILQ_FIRST(wl
);
3313 winfocus
= TAILQ_PREV(cur_focus
,
3314 ws_win_list
, entry
);
3317 case SWM_STACK_BELOW
:
3318 if ((winfocus
= TAILQ_PREV(cur_focus
, ws_win_list
,
3320 if (focus_close_wrap
)
3321 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
3323 winfocus
= TAILQ_NEXT(cur_focus
, entry
);
3329 if (winfocus
== NULL
) {
3330 if (focus_default
== SWM_STACK_TOP
)
3331 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
3333 winfocus
= TAILQ_FIRST(wl
);
3338 return get_focus_magic(winfocus
);
3342 get_region_focus(struct swm_region
*r
)
3344 struct ws_win
*winfocus
= NULL
;
3349 if (r
->ws
->focus
&& !r
->ws
->focus
->iconic
)
3350 winfocus
= r
->ws
->focus
;
3351 else if (r
->ws
->focus_prev
&& !r
->ws
->focus_prev
->iconic
)
3352 winfocus
= r
->ws
->focus_prev
;
3354 TAILQ_FOREACH(winfocus
, &r
->ws
->winlist
, entry
)
3355 if (!winfocus
->iconic
)
3358 return get_focus_magic(winfocus
);
3362 focus(struct swm_region
*r
, union arg
*args
)
3364 struct ws_win
*head
, *cur_focus
= NULL
, *winfocus
= NULL
;
3365 struct ws_win_list
*wl
= NULL
;
3366 struct workspace
*ws
= NULL
;
3372 DNPRINTF(SWM_D_FOCUS
, "focus: id: %d\n", args
->id
);
3374 if ((cur_focus
= r
->ws
->focus
) == NULL
)
3378 if (TAILQ_EMPTY(wl
))
3380 /* make sure there is at least one uniconified window */
3382 TAILQ_FOREACH(winfocus
, wl
, entry
)
3383 if (!winfocus
->iconic
) {
3391 case SWM_ARG_ID_FOCUSPREV
:
3392 head
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
3394 head
= TAILQ_LAST(wl
, ws_win_list
);
3396 if (WINID(winfocus
) == cur_focus
->transient
) {
3397 head
= TAILQ_PREV(winfocus
, ws_win_list
, entry
);
3399 head
= TAILQ_LAST(wl
, ws_win_list
);
3404 if (winfocus
&& winfocus
->iconic
) {
3405 while (winfocus
!= cur_focus
) {
3406 if (winfocus
== NULL
)
3407 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
3408 if (!winfocus
->iconic
)
3410 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
,
3416 case SWM_ARG_ID_FOCUSNEXT
:
3417 head
= TAILQ_NEXT(cur_focus
, entry
);
3419 head
= TAILQ_FIRST(wl
);
3423 if (winfocus
&& winfocus
->iconic
) {
3424 while (winfocus
!= cur_focus
) {
3425 if (winfocus
== NULL
)
3426 winfocus
= TAILQ_FIRST(wl
);
3427 if (!winfocus
->iconic
)
3429 winfocus
= TAILQ_NEXT(winfocus
, entry
);
3434 case SWM_ARG_ID_FOCUSMAIN
:
3435 winfocus
= TAILQ_FIRST(wl
);
3436 if (winfocus
== cur_focus
)
3437 winfocus
= cur_focus
->ws
->focus_prev
;
3444 focus_win(get_focus_magic(winfocus
));
3450 cycle_layout(struct swm_region
*r
, union arg
*args
)
3452 struct workspace
*ws
= r
->ws
;
3454 /* suppress unused warning since var is needed */
3457 DNPRINTF(SWM_D_EVENT
, "cycle_layout: workspace: %d\n", ws
->idx
);
3460 if (ws
->cur_layout
->l_stack
== NULL
)
3461 ws
->cur_layout
= &layouts
[0];
3466 focus_win(get_region_focus(r
));
3472 stack_config(struct swm_region
*r
, union arg
*args
)
3474 struct workspace
*ws
= r
->ws
;
3476 DNPRINTF(SWM_D_STACK
, "stack_config: id: %d workspace: %d\n",
3479 if (ws
->cur_layout
->l_config
!= NULL
)
3480 ws
->cur_layout
->l_config(ws
, args
->id
);
3482 if (args
->id
!= SWM_ARG_ID_STACKINIT
)
3491 struct swm_geometry g
;
3492 struct swm_region
*r
;
3498 DNPRINTF(SWM_D_STACK
, "stack: begin\n");
3500 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
3501 for (i
= 0; i
< num_screens
; i
++) {
3505 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
3506 DNPRINTF(SWM_D_STACK
, "stack: workspace: %d "
3507 "(screen: %d, region: %d)\n", r
->ws
->idx
, i
, j
++);
3509 /* start with screen geometry, adjust for bar */
3511 g
.w
-= 2 * border_width
;
3512 g
.h
-= 2 * border_width
;
3518 r
->ws
->cur_layout
->l_stack(r
->ws
, &g
);
3519 r
->ws
->cur_layout
->l_string(r
->ws
);
3520 /* save r so we can track region changes */
3527 DNPRINTF(SWM_D_STACK
, "stack: end\n");
3531 store_float_geom(struct ws_win
*win
, struct swm_region
*r
)
3533 if (win
== NULL
|| r
== NULL
)
3536 /* retain window geom and region geom */
3537 win
->g_float
= win
->g
;
3538 win
->g_float
.x
-= X(r
);
3539 win
->g_float
.y
-= Y(r
);
3540 win
->g_floatvalid
= 1;
3541 DNPRINTF(SWM_D_MISC
, "store_float_geom: window: 0x%x, g: (%d,%d)"
3542 " %d x %d, g_float: (%d,%d) %d x %d\n", win
->id
, X(win
), Y(win
),
3543 WIDTH(win
), HEIGHT(win
), win
->g_float
.x
, win
->g_float
.y
,
3544 win
->g_float
.w
, win
->g_float
.h
);
3548 load_float_geom(struct ws_win
*win
, struct swm_region
*r
)
3550 if (win
== NULL
|| r
== NULL
)
3553 if (win
->g_floatvalid
) {
3554 win
->g
= win
->g_float
;
3557 DNPRINTF(SWM_D_MISC
, "load_float_geom: window: 0x%x, g: (%d,%d)"
3558 "%d x %d\n", win
->id
, X(win
), Y(win
), WIDTH(win
),
3561 DNPRINTF(SWM_D_MISC
, "load_float_geom: window: 0x%x, g_float "
3562 "is not set.\n", win
->id
);
3567 stack_floater(struct ws_win
*win
, struct swm_region
*r
)
3572 DNPRINTF(SWM_D_MISC
, "stack_floater: window: 0x%x\n", win
->id
);
3575 * to allow windows to change their size (e.g. mplayer fs) only retrieve
3576 * geom on ws switches or return from max mode
3578 if (win
->floatmaxed
|| (r
!= r
->ws
->old_r
&&
3579 !(win
->ewmh_flags
& EWMH_F_FULLSCREEN
))) {
3580 /* update geometry for the new region */
3581 load_float_geom(win
, r
);
3584 win
->floatmaxed
= 0;
3587 * if set to fullscreen mode, configure window to maximum size.
3589 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
3590 if (!win
->g_floatvalid
)
3591 store_float_geom(win
, win
->ws
->r
);
3597 * remove border on fullscreen floater when in fullscreen mode or when
3598 * the quirk is present.
3600 if ((win
->ewmh_flags
& EWMH_F_FULLSCREEN
) ||
3601 ((win
->quirks
& SWM_Q_FULLSCREEN
) &&
3602 (WIDTH(win
) >= WIDTH(r
)) && (HEIGHT(win
) >= HEIGHT(r
)))) {
3603 if (win
->bordered
) {
3605 X(win
) += border_width
;
3606 Y(win
) += border_width
;
3608 } else if (!win
->bordered
) {
3610 X(win
) -= border_width
;
3611 Y(win
) -= border_width
;
3614 if (win
->transient
&& (win
->quirks
& SWM_Q_TRANSSZ
)) {
3615 WIDTH(win
) = (double)WIDTH(r
) * dialog_ratio
;
3616 HEIGHT(win
) = (double)HEIGHT(r
) * dialog_ratio
;
3619 if (!win
->manual
&& !(win
->ewmh_flags
& EWMH_F_FULLSCREEN
) &&
3620 !(win
->quirks
& SWM_Q_ANYWHERE
)) {
3622 * floaters and transients are auto-centred unless moved,
3623 * resized or ANYWHERE quirk is set.
3625 X(win
) = X(r
) + (WIDTH(r
) - WIDTH(win
)) / 2 - BORDER(win
);
3626 Y(win
) = Y(r
) + (HEIGHT(r
) - HEIGHT(win
)) / 2 - BORDER(win
);
3628 store_float_geom(win
, r
);
3631 /* keep window within region bounds */
3632 constrain_window(win
, r
, 0);
3638 * Send keystrokes to terminal to decrease/increase the font size as the
3639 * window size changes.
3642 adjust_font(struct ws_win
*win
)
3644 if (!(win
->quirks
& SWM_Q_XTERM_FONTADJ
) ||
3645 win
->floating
|| win
->transient
)
3648 if (win
->sh
.width_inc
&& win
->last_inc
!= win
->sh
.width_inc
&&
3649 WIDTH(win
) / win
->sh
.width_inc
< term_width
&&
3650 win
->font_steps
< SWM_MAX_FONT_STEPS
) {
3651 win
->font_size_boundary
[win
->font_steps
] =
3652 (win
->sh
.width_inc
* term_width
) + win
->sh
.base_width
;
3655 win
->last_inc
= win
->sh
.width_inc
;
3656 fake_keypress(win
, XK_KP_Subtract
, XCB_MOD_MASK_SHIFT
);
3657 } else if (win
->font_steps
&& win
->last_inc
!= win
->sh
.width_inc
&&
3658 WIDTH(win
) > win
->font_size_boundary
[win
->font_steps
- 1]) {
3661 win
->last_inc
= win
->sh
.width_inc
;
3662 fake_keypress(win
, XK_KP_Add
, XCB_MOD_MASK_SHIFT
);
3666 #define SWAPXY(g) do { \
3668 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
3669 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
3672 stack_master(struct workspace
*ws
, struct swm_geometry
*g
, int rot
, int flip
)
3674 struct swm_geometry win_g
, r_g
= *g
;
3675 struct ws_win
*win
, *fs_win
= NULL
;
3676 int i
, j
, s
, stacks
;
3677 int w_inc
= 1, h_inc
, w_base
= 1, h_base
;
3678 int hrh
, extra
= 0, h_slice
, last_h
= 0;
3679 int split
, colno
, winno
, mwin
, msize
, mscale
;
3680 int remain
, missing
, v_slice
, reconfigure
= 0;
3683 DNPRINTF(SWM_D_STACK
, "stack_master: workspace: %d, rot: %s, "
3684 "flip: %s\n", ws
->idx
, YESNO(rot
), YESNO(flip
));
3686 winno
= count_win(ws
, 0);
3687 if (winno
== 0 && count_win(ws
, 1) == 0)
3690 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
3691 if (!win
->transient
&& !win
->floating
&& !win
->iconic
)
3698 w_inc
= win
->sh
.width_inc
;
3699 w_base
= win
->sh
.base_width
;
3700 mwin
= ws
->l_state
.horizontal_mwin
;
3701 mscale
= ws
->l_state
.horizontal_msize
;
3702 stacks
= ws
->l_state
.horizontal_stacks
;
3705 w_inc
= win
->sh
.height_inc
;
3706 w_base
= win
->sh
.base_height
;
3707 mwin
= ws
->l_state
.vertical_mwin
;
3708 mscale
= ws
->l_state
.vertical_msize
;
3709 stacks
= ws
->l_state
.vertical_stacks
;
3713 if (stacks
> winno
- mwin
)
3714 stacks
= winno
- mwin
;
3718 h_slice
= r_g
.h
/ SWM_H_SLICE
;
3719 if (mwin
&& winno
> mwin
) {
3720 v_slice
= r_g
.w
/ SWM_V_SLICE
;
3724 win_g
.w
= v_slice
* mscale
;
3726 if (w_inc
> 1 && w_inc
< v_slice
) {
3727 /* adjust for window's requested size increment */
3728 remain
= (win_g
.w
- w_base
) % w_inc
;
3734 win_g
.x
+= r_g
.w
- msize
;
3737 colno
= split
= winno
/ stacks
;
3738 win_g
.w
= ((r_g
.w
- (stacks
* 2 * border_width
) +
3739 2 * border_width
) / stacks
);
3741 hrh
= r_g
.h
/ colno
;
3742 extra
= r_g
.h
- (colno
* hrh
);
3743 win_g
.h
= hrh
- 2 * border_width
;
3745 /* stack all the tiled windows */
3746 i
= j
= 0, s
= stacks
;
3747 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3748 if (win
->transient
|| win
->floating
)
3753 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
3758 if (split
&& i
== split
) {
3759 colno
= (winno
- mwin
) / stacks
;
3760 if (s
<= (winno
- mwin
) % stacks
)
3762 split
= split
+ colno
;
3763 hrh
= (r_g
.h
/ colno
);
3764 extra
= r_g
.h
- (colno
* hrh
);
3768 win_g
.x
+= win_g
.w
+ 2 * border_width
;
3769 win_g
.w
= (r_g
.w
- msize
-
3770 (stacks
* 2 * border_width
)) / stacks
;
3772 win_g
.w
+= (r_g
.w
- msize
-
3773 (stacks
* 2 * border_width
)) % stacks
;
3777 win_g
.h
= hrh
- 2 * border_width
;
3779 h_inc
= win
->sh
.width_inc
;
3780 h_base
= win
->sh
.base_width
;
3782 h_inc
= win
->sh
.height_inc
;
3783 h_base
= win
->sh
.base_height
;
3785 if (j
== colno
- 1) {
3786 win_g
.h
= hrh
+ extra
;
3787 } else if (h_inc
> 1 && h_inc
< h_slice
) {
3788 /* adjust for window's requested size increment */
3789 remain
= (win_g
.h
- h_base
) % h_inc
;
3790 missing
= h_inc
- remain
;
3792 if (missing
<= extra
|| j
== 0) {
3804 win_g
.y
+= last_h
+ 2 * border_width
;
3806 if (disable_border
&& !bar_enabled
&& winno
== 1){
3808 win_g
.w
+= 2 * border_width
;
3809 win_g
.h
+= 2 * border_width
;
3814 if (X(win
) != win_g
.y
|| Y(win
) != win_g
.x
||
3815 WIDTH(win
) != win_g
.h
|| HEIGHT(win
) != win_g
.w
) {
3819 WIDTH(win
) = win_g
.h
;
3820 HEIGHT(win
) = win_g
.w
;
3823 if (X(win
) != win_g
.x
|| Y(win
) != win_g
.y
||
3824 WIDTH(win
) != win_g
.w
|| HEIGHT(win
) != win_g
.h
) {
3828 WIDTH(win
) = win_g
.w
;
3829 HEIGHT(win
) = win_g
.h
;
3833 if (bordered
!= win
->bordered
) {
3835 win
->bordered
= bordered
;
3851 /* now, stack all the floaters and transients */
3852 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3853 if (!win
->transient
&& !win
->floating
)
3857 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
3862 stack_floater(win
, ws
->r
);
3866 /* Make sure fs_win is stacked last so it's on top. */
3868 stack_floater(fs_win
, ws
->r
);
3874 vertical_config(struct workspace
*ws
, int id
)
3876 DNPRINTF(SWM_D_STACK
, "vertical_config: id: %d, workspace: %d\n",
3880 case SWM_ARG_ID_STACKRESET
:
3881 case SWM_ARG_ID_STACKINIT
:
3882 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ 2;
3883 ws
->l_state
.vertical_mwin
= 1;
3884 ws
->l_state
.vertical_stacks
= 1;
3886 case SWM_ARG_ID_MASTERSHRINK
:
3887 if (ws
->l_state
.vertical_msize
> 1)
3888 ws
->l_state
.vertical_msize
--;
3890 case SWM_ARG_ID_MASTERGROW
:
3891 if (ws
->l_state
.vertical_msize
< SWM_V_SLICE
- 1)
3892 ws
->l_state
.vertical_msize
++;
3894 case SWM_ARG_ID_MASTERADD
:
3895 ws
->l_state
.vertical_mwin
++;
3897 case SWM_ARG_ID_MASTERDEL
:
3898 if (ws
->l_state
.vertical_mwin
> 0)
3899 ws
->l_state
.vertical_mwin
--;
3901 case SWM_ARG_ID_STACKINC
:
3902 ws
->l_state
.vertical_stacks
++;
3904 case SWM_ARG_ID_STACKDEC
:
3905 if (ws
->l_state
.vertical_stacks
> 1)
3906 ws
->l_state
.vertical_stacks
--;
3908 case SWM_ARG_ID_FLIPLAYOUT
:
3909 ws
->l_state
.vertical_flip
= !ws
->l_state
.vertical_flip
;
3917 vertical_stack(struct workspace
*ws
, struct swm_geometry
*g
)
3919 DNPRINTF(SWM_D_STACK
, "vertical_stack: workspace: %d\n", ws
->idx
);
3921 stack_master(ws
, g
, 0, ws
->l_state
.vertical_flip
);
3925 horizontal_config(struct workspace
*ws
, int id
)
3927 DNPRINTF(SWM_D_STACK
, "horizontal_config: workspace: %d\n", ws
->idx
);
3930 case SWM_ARG_ID_STACKRESET
:
3931 case SWM_ARG_ID_STACKINIT
:
3932 ws
->l_state
.horizontal_mwin
= 1;
3933 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ 2;
3934 ws
->l_state
.horizontal_stacks
= 1;
3936 case SWM_ARG_ID_MASTERSHRINK
:
3937 if (ws
->l_state
.horizontal_msize
> 1)
3938 ws
->l_state
.horizontal_msize
--;
3940 case SWM_ARG_ID_MASTERGROW
:
3941 if (ws
->l_state
.horizontal_msize
< SWM_H_SLICE
- 1)
3942 ws
->l_state
.horizontal_msize
++;
3944 case SWM_ARG_ID_MASTERADD
:
3945 ws
->l_state
.horizontal_mwin
++;
3947 case SWM_ARG_ID_MASTERDEL
:
3948 if (ws
->l_state
.horizontal_mwin
> 0)
3949 ws
->l_state
.horizontal_mwin
--;
3951 case SWM_ARG_ID_STACKINC
:
3952 ws
->l_state
.horizontal_stacks
++;
3954 case SWM_ARG_ID_STACKDEC
:
3955 if (ws
->l_state
.horizontal_stacks
> 1)
3956 ws
->l_state
.horizontal_stacks
--;
3958 case SWM_ARG_ID_FLIPLAYOUT
:
3959 ws
->l_state
.horizontal_flip
= !ws
->l_state
.horizontal_flip
;
3967 horizontal_stack(struct workspace
*ws
, struct swm_geometry
*g
)
3969 DNPRINTF(SWM_D_STACK
, "horizontal_stack: workspace: %d\n", ws
->idx
);
3971 stack_master(ws
, g
, 1, ws
->l_state
.horizontal_flip
);
3974 /* fullscreen view */
3976 max_stack(struct workspace
*ws
, struct swm_geometry
*g
)
3978 struct swm_geometry gg
= *g
;
3979 struct ws_win
*w
, *win
= NULL
, *parent
= NULL
;
3980 int winno
, num_screens
;
3982 DNPRINTF(SWM_D_STACK
, "max_stack: workspace: %d\n", ws
->idx
);
3987 winno
= count_win(ws
, 0);
3988 if (winno
== 0 && count_win(ws
, 1) == 0)
3991 /* Figure out which top level window should be visible. */
3992 if (ws
->focus_pending
)
3993 win
= ws
->focus_pending
;
3997 win
= TAILQ_FIRST(&ws
->winlist
);
4000 parent
= find_window(win
->transient
);
4002 DNPRINTF(SWM_D_STACK
, "max_stack: win: 0x%x\n", win
->id
);
4004 /* maximize all top level windows */
4005 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
4006 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
4007 if (w
->transient
|| w
->iconic
)
4010 if (w
->floating
&& !w
->floatmaxed
) {
4012 * retain geometry for retrieval on exit from
4015 store_float_geom(w
, ws
->r
);
4019 /* only reconfigure if necessary */
4020 if (X(w
) != gg
.x
|| Y(w
) != gg
.y
|| WIDTH(w
) != gg
.w
||
4021 HEIGHT(w
) != gg
.h
) {
4027 WIDTH(w
) += 2 * border_width
;
4028 HEIGHT(w
) += 2 * border_width
;
4034 /* Unmap unwanted windows if not multi-screen. */
4035 if (!(num_screens
> 1 || outputs
> 1) && (w
!= win
||
4036 w
!= parent
|| w
->transient
!= win
->id
))
4040 /* If a parent exists, map it first. */
4044 /* Map siblings next. */
4045 TAILQ_FOREACH(w
, &ws
->winlist
, entry
)
4046 if (w
!= win
&& !w
->iconic
&&
4047 w
->transient
== parent
->id
) {
4048 stack_floater(w
, ws
->r
);
4053 /* Map focused window. */
4056 /* Finally, map children of focus window. */
4057 TAILQ_FOREACH(w
, &ws
->winlist
, entry
)
4058 if (w
->transient
== win
->id
&& !w
->iconic
) {
4059 stack_floater(w
, ws
->r
);
4065 send_to_ws(struct swm_region
*r
, union arg
*args
)
4067 int wsid
= args
->id
;
4068 struct ws_win
*win
= NULL
, *parent
;
4069 struct workspace
*ws
, *nws
, *pws
;
4070 char ws_idx_str
[SWM_PROPLEN
];
4072 if (wsid
>= workspace_limit
)
4075 if (r
&& r
->ws
&& r
->ws
->focus
)
4080 if (win
->ws
->idx
== wsid
)
4083 DNPRINTF(SWM_D_MOVE
, "send_to_ws: win 0x%x, ws %d -> %d\n", win
->id
,
4084 win
->ws
->idx
, wsid
);
4087 nws
= &win
->s
->ws
[wsid
];
4089 /* Update the window's workspace property: _SWM_WS */
4090 if (snprintf(ws_idx_str
, SWM_PROPLEN
, "%d", nws
->idx
) < SWM_PROPLEN
) {
4091 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
4092 ws
->focus_pending
= get_focus_prev(win
);
4094 /* Move the parent if this is a transient window. */
4095 if (win
->transient
) {
4096 parent
= find_window(win
->transient
);
4099 /* Set new focus in parent's ws if needed. */
4100 if (pws
->focus
== parent
) {
4101 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
4102 pws
->focus_pending
=
4103 get_focus_prev(parent
);
4105 unfocus_win(parent
);
4107 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
4108 pws
->focus
= pws
->focus_pending
;
4110 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
4111 pws
->focus_pending
= NULL
;
4114 /* Don't unmap parent if new ws is visible */
4116 unmap_window(parent
);
4119 TAILQ_REMOVE(&ws
->winlist
, parent
, entry
);
4120 TAILQ_INSERT_TAIL(&nws
->winlist
, parent
, entry
);
4123 DNPRINTF(SWM_D_PROP
, "send_to_ws: set "
4124 "property: _SWM_WS: %s\n", ws_idx_str
);
4125 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
4126 parent
->id
, a_swm_ws
, XCB_ATOM_STRING
, 8,
4127 strlen(ws_idx_str
), ws_idx_str
);
4133 /* Don't unmap if new ws is visible */
4138 TAILQ_REMOVE(&ws
->winlist
, win
, entry
);
4139 TAILQ_INSERT_TAIL(&nws
->winlist
, win
, entry
);
4142 /* Set focus on new ws. */
4143 unfocus_win(nws
->focus
);
4146 DNPRINTF(SWM_D_PROP
, "send_to_ws: set property: _SWM_WS: %s\n",
4148 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
4149 a_swm_ws
, XCB_ATOM_STRING
, 8, strlen(ws_idx_str
),
4152 /* Restack and set new focus. */
4155 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
4156 focus_win(ws
->focus_pending
);
4157 ws
->focus_pending
= NULL
;
4163 DNPRINTF(SWM_D_MOVE
, "send_to_ws: done.\n");
4167 pressbutton(struct swm_region
*r
, union arg
*args
)
4169 /* suppress unused warning since var is needed */
4172 xcb_test_fake_input(conn
, XCB_BUTTON_PRESS
, args
->id
,
4173 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
4174 xcb_test_fake_input(conn
, XCB_BUTTON_RELEASE
, args
->id
,
4175 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
4179 raise_toggle(struct swm_region
*r
, union arg
*args
)
4181 /* suppress unused warning since var is needed */
4184 if (r
== NULL
|| r
->ws
== NULL
)
4187 r
->ws
->always_raise
= !r
->ws
->always_raise
;
4189 /* bring floaters back to top */
4190 if (!r
->ws
->always_raise
)
4197 iconify(struct swm_region
*r
, union arg
*args
)
4199 /* suppress unused warning since var is needed */
4202 if (r
->ws
->focus
== NULL
)
4205 set_swm_iconic(r
->ws
->focus
, 1);
4211 get_win_name(xcb_window_t win
)
4214 xcb_get_property_cookie_t c
;
4215 xcb_get_property_reply_t
*r
;
4217 /* First try _NET_WM_NAME for UTF-8. */
4218 c
= xcb_get_property(conn
, 0, win
, a_netwmname
,
4219 XCB_GET_PROPERTY_TYPE_ANY
, 0, UINT_MAX
);
4220 r
= xcb_get_property_reply(conn
, c
, NULL
);
4222 if (!r
|| r
->type
== XCB_NONE
) {
4224 /* Use WM_NAME instead; no UTF-8. */
4225 c
= xcb_get_property(conn
, 0, win
, XCB_ATOM_WM_NAME
,
4226 XCB_GET_PROPERTY_TYPE_ANY
, 0, UINT_MAX
);
4227 r
= xcb_get_property_reply(conn
, c
, NULL
);
4229 if(!r
|| r
->type
== XCB_NONE
) {
4236 name
= strndup(xcb_get_property_value(r
),
4237 xcb_get_property_value_length(r
));
4244 uniconify(struct swm_region
*r
, union arg
*args
)
4251 DNPRINTF(SWM_D_MISC
, "uniconify\n");
4253 if (r
== NULL
|| r
->ws
== NULL
)
4256 /* make sure we have anything to uniconify */
4257 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
4258 if (win
->ws
== NULL
)
4259 continue; /* should never happen */
4268 search_resp_action
= SWM_SEARCH_UNICONIFY
;
4270 spawn_select(r
, args
, "search", &searchpid
);
4272 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
4275 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
4276 if (win
->ws
== NULL
)
4277 continue; /* should never happen */
4281 name
= get_win_name(win
->id
);
4284 fprintf(lfile
, "%s.%u\n", name
, win
->id
);
4292 name_workspace(struct swm_region
*r
, union arg
*args
)
4296 DNPRINTF(SWM_D_MISC
, "name_workspace\n");
4302 search_resp_action
= SWM_SEARCH_NAME_WORKSPACE
;
4304 spawn_select(r
, args
, "name_workspace", &searchpid
);
4306 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
4309 fprintf(lfile
, "%s", "");
4314 search_workspace(struct swm_region
*r
, union arg
*args
)
4317 struct workspace
*ws
;
4320 DNPRINTF(SWM_D_MISC
, "search_workspace\n");
4326 search_resp_action
= SWM_SEARCH_SEARCH_WORKSPACE
;
4328 spawn_select(r
, args
, "search", &searchpid
);
4330 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
4333 for (i
= 0; i
< workspace_limit
; i
++) {
4337 fprintf(lfile
, "%d%s%s\n", ws
->idx
+ 1,
4338 (ws
->name
? ":" : ""), (ws
->name
? ws
->name
: ""));
4345 search_win_cleanup(void)
4347 struct search_window
*sw
= NULL
;
4349 while ((sw
= TAILQ_FIRST(&search_wl
)) != NULL
) {
4350 xcb_destroy_window(conn
, sw
->indicator
);
4351 TAILQ_REMOVE(&search_wl
, sw
, entry
);
4357 search_win(struct swm_region
*r
, union arg
*args
)
4359 struct ws_win
*win
= NULL
;
4360 struct search_window
*sw
= NULL
;
4363 int i
, width
, height
;
4371 XRectangle l_ibox
, l_lbox
;
4373 DNPRINTF(SWM_D_MISC
, "search_win\n");
4376 search_resp_action
= SWM_SEARCH_SEARCH_WINDOW
;
4378 spawn_select(r
, args
, "search", &searchpid
);
4380 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
4383 TAILQ_INIT(&search_wl
);
4386 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
4390 sw
= calloc(1, sizeof(struct search_window
));
4392 warn("search_win: calloc");
4394 search_win_cleanup();
4400 snprintf(s
, sizeof s
, "%d", i
);
4403 w
= xcb_generate_id(conn
);
4404 wa
[0] = r
->s
->c
[SWM_S_COLOR_FOCUS
].pixel
;
4405 wa
[1] = r
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
;
4407 if (bar_font_legacy
) {
4408 XmbTextExtents(bar_fs
, s
, len
, &l_ibox
, &l_lbox
);
4409 width
= l_lbox
.width
+ 4;
4410 height
= bar_fs_extents
->max_logical_extent
.height
+ 4;
4412 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
,
4414 width
= info
.width
+ 4;
4415 height
= bar_font
->height
+ 4;
4418 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, w
, win
->id
, 0, 0,
4419 width
, height
, 1, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
4420 XCB_COPY_FROM_PARENT
, XCB_CW_BACK_PIXEL
|
4421 XCB_CW_BORDER_PIXEL
, wa
);
4423 xcb_map_window(conn
, w
);
4426 TAILQ_INSERT_TAIL(&search_wl
, sw
, entry
);
4428 if (bar_font_legacy
) {
4429 l_gcv
.graphics_exposures
= 0;
4430 l_draw
= XCreateGC(display
, w
, 0, &l_gcv
);
4432 XSetForeground(display
, l_draw
,
4433 r
->s
->c
[SWM_S_COLOR_BAR
].pixel
);
4435 DRAWSTRING(display
, w
, bar_fs
, l_draw
, 2,
4436 (bar_fs_extents
->max_logical_extent
.height
-
4437 l_lbox
.height
) / 2 - l_lbox
.y
, s
, len
);
4439 XFreeGC(display
, l_draw
);
4442 draw
= XftDrawCreate(display
, w
,
4443 DefaultVisual(display
, r
->s
->idx
),
4444 DefaultColormap(display
, r
->s
->idx
));
4446 XftDrawStringUtf8(draw
, &bar_font_color
, bar_font
, 2,
4447 (HEIGHT(r
->bar
) + bar_font
->height
) / 2 -
4448 bar_font
->descent
, (FcChar8
*)s
, len
);
4450 XftDrawDestroy(draw
);
4453 DNPRINTF(SWM_D_MISC
, "search_win: mapped window: 0x%x\n", w
);
4455 fprintf(lfile
, "%d\n", i
);
4465 search_resp_uniconify(char *resp
, unsigned long len
)
4471 DNPRINTF(SWM_D_MISC
, "search_resp_uniconify: resp: %s\n", resp
);
4473 TAILQ_FOREACH(win
, &search_r
->ws
->winlist
, entry
) {
4476 name
= get_win_name(win
->id
);
4479 if (asprintf(&s
, "%s.%u", name
, win
->id
) == -1) {
4484 if (strncmp(s
, resp
, len
) == 0) {
4485 /* XXX this should be a callback to generalize */
4486 set_swm_iconic(win
, 0);
4495 search_resp_name_workspace(char *resp
, unsigned long len
)
4497 struct workspace
*ws
;
4499 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: resp: %s\n", resp
);
4501 if (search_r
->ws
== NULL
)
4506 free(search_r
->ws
->name
);
4507 search_r
->ws
->name
= NULL
;
4511 ws
->name
= strdup(resp
);
4512 if (ws
->name
== NULL
) {
4513 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: "
4514 "strdup: %s", strerror(errno
));
4521 search_resp_search_workspace(char *resp
)
4528 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: resp: %s\n", resp
);
4532 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: strdup: %s",
4539 ws_idx
= (int)strtonum(q
, 1, workspace_limit
, &errstr
);
4541 DNPRINTF(SWM_D_MISC
, "workspace idx is %s: %s",
4548 switchws(search_r
, &a
);
4552 search_resp_search_window(char *resp
)
4557 struct search_window
*sw
;
4559 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: resp: %s\n", resp
);
4563 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: strdup: %s",
4568 idx
= (int)strtonum(s
, 1, INT_MAX
, &errstr
);
4570 DNPRINTF(SWM_D_MISC
, "window idx is %s: %s",
4577 TAILQ_FOREACH(sw
, &search_wl
, entry
)
4578 if (idx
== sw
->idx
) {
4584 #define MAX_RESP_LEN 1024
4587 search_do_resp(void)
4593 DNPRINTF(SWM_D_MISC
, "search_do_resp:\n");
4598 if ((resp
= calloc(1, MAX_RESP_LEN
+ 1)) == NULL
) {
4599 warn("search: calloc");
4603 rbytes
= read(select_resp_pipe
[0], resp
, MAX_RESP_LEN
);
4605 warn("search: read error");
4608 resp
[rbytes
] = '\0';
4611 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
4612 * newline, so work around that by sanitizing the resp now.
4614 resp
[strcspn(resp
, "\n")] = '\0';
4617 switch (search_resp_action
) {
4618 case SWM_SEARCH_UNICONIFY
:
4619 search_resp_uniconify(resp
, len
);
4621 case SWM_SEARCH_NAME_WORKSPACE
:
4622 search_resp_name_workspace(resp
, len
);
4624 case SWM_SEARCH_SEARCH_WORKSPACE
:
4625 search_resp_search_workspace(resp
);
4627 case SWM_SEARCH_SEARCH_WINDOW
:
4628 search_resp_search_window(resp
);
4633 if (search_resp_action
== SWM_SEARCH_SEARCH_WINDOW
)
4634 search_win_cleanup();
4636 search_resp_action
= SWM_SEARCH_NONE
;
4637 close(select_resp_pipe
[0]);
4644 wkill(struct swm_region
*r
, union arg
*args
)
4646 DNPRINTF(SWM_D_MISC
, "wkill: id: %d\n", args
->id
);
4648 if (r
->ws
->focus
== NULL
)
4651 if (args
->id
== SWM_ARG_ID_KILLWINDOW
)
4652 xcb_kill_client(conn
, r
->ws
->focus
->id
);
4654 if (r
->ws
->focus
->can_delete
)
4655 client_msg(r
->ws
->focus
, a_delete
, 0);
4661 floating_toggle_win(struct ws_win
*win
)
4663 struct swm_region
*r
;
4673 /* reject floating toggles in max stack mode */
4674 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
4677 if (win
->floating
) {
4678 if (!win
->floatmaxed
) {
4679 /* retain position for refloat */
4680 store_float_geom(win
, r
);
4684 load_float_geom(win
, r
);
4688 ewmh_update_actions(win
);
4694 floating_toggle(struct swm_region
*r
, union arg
*args
)
4696 struct ws_win
*win
= r
->ws
->focus
;
4698 /* suppress unused warning since var is needed */
4704 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
4707 ewmh_update_win_state(win
, ewmh
[_NET_WM_STATE_ABOVE
].atom
,
4708 _NET_WM_STATE_TOGGLE
);
4712 if (win
== win
->ws
->focus
)
4719 constrain_window(struct ws_win
*win
, struct swm_region
*r
, int resizable
)
4721 if (MAX_X(win
) + BORDER(win
) > MAX_X(r
)) {
4723 WIDTH(win
) = MAX_X(r
) - X(win
) - BORDER(win
);
4725 X(win
) = MAX_X(r
)- WIDTH(win
) - BORDER(win
);
4728 if (X(win
) + BORDER(win
) < X(r
)) {
4730 WIDTH(win
) -= X(r
) - X(win
) - BORDER(win
);
4732 X(win
) = X(r
) - BORDER(win
);
4735 if (MAX_Y(win
) + BORDER(win
) > MAX_Y(r
)) {
4737 HEIGHT(win
) = MAX_Y(r
) - Y(win
) - BORDER(win
);
4739 Y(win
) = MAX_Y(r
) - HEIGHT(win
) - BORDER(win
);
4742 if (Y(win
) + BORDER(win
) < Y(r
)) {
4744 HEIGHT(win
) -= Y(r
) - Y(win
) - BORDER(win
);
4746 Y(win
) = Y(r
) - BORDER(win
);
4752 if (HEIGHT(win
) < 1)
4758 update_window(struct ws_win
*win
)
4763 mask
= XCB_CONFIG_WINDOW_X
| XCB_CONFIG_WINDOW_Y
|
4764 XCB_CONFIG_WINDOW_WIDTH
| XCB_CONFIG_WINDOW_HEIGHT
|
4765 XCB_CONFIG_WINDOW_BORDER_WIDTH
;
4769 wc
[3] = HEIGHT(win
);
4770 wc
[4] = BORDER(win
);
4772 DNPRINTF(SWM_D_EVENT
, "update_window: window: 0x%x, (x,y) w x h: "
4773 "(%d,%d) %d x %d, bordered: %s\n", win
->id
, wc
[0], wc
[1], wc
[2],
4774 wc
[3], YESNO(win
->bordered
));
4776 xcb_configure_window(conn
, win
->id
, mask
, wc
);
4779 #define SWM_RESIZE_STEPS (50)
4782 resize(struct ws_win
*win
, union arg
*args
)
4784 xcb_timestamp_t timestamp
= 0;
4785 struct swm_region
*r
= NULL
;
4786 int resize_step
= 0;
4787 struct swm_geometry g
;
4788 int top
= 0, left
= 0, resizing
;
4790 xcb_cursor_t cursor
;
4791 xcb_query_pointer_reply_t
*xpr
;
4792 xcb_generic_event_t
*evt
;
4793 xcb_motion_notify_event_t
*mne
;
4799 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
4802 DNPRINTF(SWM_D_EVENT
, "resize: window: 0x%x, floating: %s, "
4803 "transient: 0x%x\n", win
->id
, YESNO(win
->floating
),
4806 if (!win
->transient
&& !win
->floating
)
4809 /* reject resizes in max mode for floaters (transient ok) */
4810 if (win
->floatmaxed
)
4814 ewmh_update_win_state(win
, ewmh
[_SWM_WM_STATE_MANUAL
].atom
,
4822 case SWM_ARG_ID_WIDTHSHRINK
:
4823 WIDTH(win
) -= SWM_RESIZE_STEPS
;
4826 case SWM_ARG_ID_WIDTHGROW
:
4827 WIDTH(win
) += SWM_RESIZE_STEPS
;
4830 case SWM_ARG_ID_HEIGHTSHRINK
:
4831 HEIGHT(win
) -= SWM_RESIZE_STEPS
;
4834 case SWM_ARG_ID_HEIGHTGROW
:
4835 HEIGHT(win
) += SWM_RESIZE_STEPS
;
4842 constrain_window(win
, r
, 1);
4844 store_float_geom(win
,r
);
4848 /* get cursor offset from window root */
4849 xpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
4856 if (xpr
->win_x
< WIDTH(win
) / 2)
4859 if (xpr
->win_y
< HEIGHT(win
) / 2)
4862 if (args
->id
== SWM_ARG_ID_CENTER
)
4863 cursor
= cursors
[XC_SIZING
].cid
;
4865 cursor
= cursors
[left
? XC_TOP_LEFT_CORNER
:
4866 XC_TOP_RIGHT_CORNER
].cid
;
4868 cursor
= cursors
[left
? XC_BOTTOM_LEFT_CORNER
:
4869 XC_BOTTOM_RIGHT_CORNER
].cid
;
4871 xcb_grab_pointer(conn
, 0, win
->id
, MOUSEMASK
,
4872 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
, XCB_WINDOW_NONE
, cursor
,
4877 while ((evt
= xcb_wait_for_event(conn
)) && resizing
) {
4878 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
4879 case XCB_BUTTON_RELEASE
:
4880 DNPRINTF(SWM_D_EVENT
, "resize: BUTTON_RELEASE\n");
4883 case XCB_MOTION_NOTIFY
:
4884 mne
= (xcb_motion_notify_event_t
*)evt
;
4885 /* cursor offset/delta from start of the operation */
4886 dx
= mne
->root_x
- xpr
->root_x
;
4887 dy
= mne
->root_y
- xpr
->root_y
;
4893 if (args
->id
== SWM_ARG_ID_CENTER
) {
4894 if (g
.h
/ 2 + dy
< 1)
4898 HEIGHT(win
) = g
.h
+ 2 * dy
;
4906 HEIGHT(win
) = g
.h
+ dy
;
4913 if (args
->id
== SWM_ARG_ID_CENTER
) {
4914 if (g
.w
/ 2 + dx
< 1)
4918 WIDTH(win
) = g
.w
+ 2 * dx
;
4926 WIDTH(win
) = g
.w
+ dx
;
4929 constrain_window(win
, r
, 1);
4931 /* not free, don't sync more than 120 times / second */
4932 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
4933 timestamp
= mne
->time
;
4948 store_float_geom(win
,r
);
4950 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
4952 DNPRINTF(SWM_D_EVENT
, "resize: done.\n");
4956 resize_step(struct swm_region
*r
, union arg
*args
)
4958 struct ws_win
*win
= NULL
;
4960 if (r
&& r
->ws
&& r
->ws
->focus
)
4969 #define SWM_MOVE_STEPS (50)
4972 move(struct ws_win
*win
, union arg
*args
)
4974 xcb_timestamp_t timestamp
= 0;
4975 int move_step
= 0, moving
;
4976 struct swm_region
*r
= NULL
;
4977 xcb_query_pointer_reply_t
*qpr
;
4978 xcb_generic_event_t
*evt
;
4979 xcb_motion_notify_event_t
*mne
;
4985 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
4988 DNPRINTF(SWM_D_EVENT
, "move: window: 0x%x, floating: %s, transient: "
4989 "0x%x\n", win
->id
, YESNO(win
->floating
), win
->transient
);
4991 /* in max_stack mode should only move transients */
4992 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
] && !win
->transient
)
4996 if (!win
->floating
&& !win
->transient
) {
4997 store_float_geom(win
, r
);
4998 ewmh_update_win_state(win
, ewmh
[_NET_WM_STATE_ABOVE
].atom
,
5001 ewmh_update_win_state(win
, ewmh
[_SWM_WM_STATE_MANUAL
].atom
,
5010 case SWM_ARG_ID_MOVELEFT
:
5011 X(win
) -= (SWM_MOVE_STEPS
- border_width
);
5014 case SWM_ARG_ID_MOVERIGHT
:
5015 X(win
) += (SWM_MOVE_STEPS
- border_width
);
5018 case SWM_ARG_ID_MOVEUP
:
5019 Y(win
) -= (SWM_MOVE_STEPS
- border_width
);
5022 case SWM_ARG_ID_MOVEDOWN
:
5023 Y(win
) += (SWM_MOVE_STEPS
- border_width
);
5030 constrain_window(win
, r
, 0);
5032 store_float_geom(win
, r
);
5036 xcb_grab_pointer(conn
, 0, win
->id
, MOUSEMASK
,
5037 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
,
5038 XCB_WINDOW_NONE
, cursors
[XC_FLEUR
].cid
, XCB_CURRENT_TIME
);
5040 /* get cursor offset from window root */
5041 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
5044 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
5050 while ((evt
= xcb_wait_for_event(conn
)) && moving
) {
5051 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
5052 case XCB_BUTTON_RELEASE
:
5053 DNPRINTF(SWM_D_EVENT
, "move: BUTTON_RELEASE\n");
5056 case XCB_MOTION_NOTIFY
:
5057 mne
= (xcb_motion_notify_event_t
*)evt
;
5058 X(win
) = mne
->root_x
- qpr
->win_x
- border_width
;
5059 Y(win
) = mne
->root_y
- qpr
->win_y
- border_width
;
5061 constrain_window(win
, r
, 0);
5063 /* not free, don't sync more than 120 times / second */
5064 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
5065 timestamp
= mne
->time
;
5080 store_float_geom(win
, r
);
5082 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
5083 DNPRINTF(SWM_D_EVENT
, "move: done.\n");
5087 move_step(struct swm_region
*r
, union arg
*args
)
5089 struct ws_win
*win
= NULL
;
5091 if (r
&& r
->ws
&& r
->ws
->focus
)
5096 if (!win
->transient
&& !win
->floating
)
5103 /* user/key callable function IDs */
5153 KF_SEARCH_WORKSPACE
,
5194 KF_DUMPWINS
, /* MUST BE LAST */
5198 /* key definitions */
5200 char name
[SWM_FUNCNAME_LEN
];
5201 void (*func
)(struct swm_region
*r
, union arg
*);
5203 } keyfuncs
[KF_INVALID
+ 1] = {
5204 /* name function argument */
5205 { "bar_toggle", bar_toggle
, {0} },
5206 { "button2", pressbutton
, {2} },
5207 { "cycle_layout", cycle_layout
, {0} },
5208 { "flip_layout", stack_config
, {.id
= SWM_ARG_ID_FLIPLAYOUT
} },
5209 { "float_toggle", floating_toggle
,{0} },
5210 { "focus_main", focus
, {.id
= SWM_ARG_ID_FOCUSMAIN
} },
5211 { "focus_next", focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
5212 { "focus_prev", focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
5213 { "height_grow", resize_step
, {.id
= SWM_ARG_ID_HEIGHTGROW
} },
5214 { "height_shrink", resize_step
, {.id
= SWM_ARG_ID_HEIGHTSHRINK
} },
5215 { "iconify", iconify
, {0} },
5216 { "master_shrink", stack_config
, {.id
= SWM_ARG_ID_MASTERSHRINK
} },
5217 { "master_grow", stack_config
, {.id
= SWM_ARG_ID_MASTERGROW
} },
5218 { "master_add", stack_config
, {.id
= SWM_ARG_ID_MASTERADD
} },
5219 { "master_del", stack_config
, {.id
= SWM_ARG_ID_MASTERDEL
} },
5220 { "move_down", move_step
, {.id
= SWM_ARG_ID_MOVEDOWN
} },
5221 { "move_left", move_step
, {.id
= SWM_ARG_ID_MOVELEFT
} },
5222 { "move_right", move_step
, {.id
= SWM_ARG_ID_MOVERIGHT
} },
5223 { "move_up", move_step
, {.id
= SWM_ARG_ID_MOVEUP
} },
5224 { "mvws_1", send_to_ws
, {.id
= 0} },
5225 { "mvws_2", send_to_ws
, {.id
= 1} },
5226 { "mvws_3", send_to_ws
, {.id
= 2} },
5227 { "mvws_4", send_to_ws
, {.id
= 3} },
5228 { "mvws_5", send_to_ws
, {.id
= 4} },
5229 { "mvws_6", send_to_ws
, {.id
= 5} },
5230 { "mvws_7", send_to_ws
, {.id
= 6} },
5231 { "mvws_8", send_to_ws
, {.id
= 7} },
5232 { "mvws_9", send_to_ws
, {.id
= 8} },
5233 { "mvws_10", send_to_ws
, {.id
= 9} },
5234 { "mvws_11", send_to_ws
, {.id
= 10} },
5235 { "mvws_12", send_to_ws
, {.id
= 11} },
5236 { "mvws_13", send_to_ws
, {.id
= 12} },
5237 { "mvws_14", send_to_ws
, {.id
= 13} },
5238 { "mvws_15", send_to_ws
, {.id
= 14} },
5239 { "mvws_16", send_to_ws
, {.id
= 15} },
5240 { "mvws_17", send_to_ws
, {.id
= 16} },
5241 { "mvws_18", send_to_ws
, {.id
= 17} },
5242 { "mvws_19", send_to_ws
, {.id
= 18} },
5243 { "mvws_20", send_to_ws
, {.id
= 19} },
5244 { "mvws_21", send_to_ws
, {.id
= 20} },
5245 { "mvws_22", send_to_ws
, {.id
= 21} },
5246 { "name_workspace", name_workspace
, {0} },
5247 { "quit", quit
, {0} },
5248 { "raise_toggle", raise_toggle
, {0} },
5249 { "restart", restart
, {0} },
5250 { "screen_next", cyclescr
, {.id
= SWM_ARG_ID_CYCLESC_UP
} },
5251 { "screen_prev", cyclescr
, {.id
= SWM_ARG_ID_CYCLESC_DOWN
} },
5252 { "search_win", search_win
, {0} },
5253 { "search_workspace", search_workspace
, {0} },
5254 { "spawn_custom", NULL
, {0} },
5255 { "stack_inc", stack_config
, {.id
= SWM_ARG_ID_STACKINC
} },
5256 { "stack_dec", stack_config
, {.id
= SWM_ARG_ID_STACKDEC
} },
5257 { "stack_reset", stack_config
, {.id
= SWM_ARG_ID_STACKRESET
} },
5258 { "swap_main", swapwin
, {.id
= SWM_ARG_ID_SWAPMAIN
} },
5259 { "swap_next", swapwin
, {.id
= SWM_ARG_ID_SWAPNEXT
} },
5260 { "swap_prev", swapwin
, {.id
= SWM_ARG_ID_SWAPPREV
} },
5261 { "uniconify", uniconify
, {0} },
5262 { "version", version
, {0} },
5263 { "width_grow", resize_step
, {.id
= SWM_ARG_ID_WIDTHGROW
} },
5264 { "width_shrink", resize_step
, {.id
= SWM_ARG_ID_WIDTHSHRINK
} },
5265 { "wind_del", wkill
, {.id
= SWM_ARG_ID_DELETEWINDOW
} },
5266 { "wind_kill", wkill
, {.id
= SWM_ARG_ID_KILLWINDOW
} },
5267 { "ws_1", switchws
, {.id
= 0} },
5268 { "ws_2", switchws
, {.id
= 1} },
5269 { "ws_3", switchws
, {.id
= 2} },
5270 { "ws_4", switchws
, {.id
= 3} },
5271 { "ws_5", switchws
, {.id
= 4} },
5272 { "ws_6", switchws
, {.id
= 5} },
5273 { "ws_7", switchws
, {.id
= 6} },
5274 { "ws_8", switchws
, {.id
= 7} },
5275 { "ws_9", switchws
, {.id
= 8} },
5276 { "ws_10", switchws
, {.id
= 9} },
5277 { "ws_11", switchws
, {.id
= 10} },
5278 { "ws_12", switchws
, {.id
= 11} },
5279 { "ws_13", switchws
, {.id
= 12} },
5280 { "ws_14", switchws
, {.id
= 13} },
5281 { "ws_15", switchws
, {.id
= 14} },
5282 { "ws_16", switchws
, {.id
= 15} },
5283 { "ws_17", switchws
, {.id
= 16} },
5284 { "ws_18", switchws
, {.id
= 17} },
5285 { "ws_19", switchws
, {.id
= 18} },
5286 { "ws_20", switchws
, {.id
= 19} },
5287 { "ws_21", switchws
, {.id
= 20} },
5288 { "ws_22", switchws
, {.id
= 21} },
5289 { "ws_next", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP
} },
5290 { "ws_next_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP_ALL
} },
5291 { "ws_prev", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN
} },
5292 { "ws_prev_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN_ALL
} },
5293 { "ws_prior", priorws
, {0} },
5294 { "dumpwins", dumpwins
, {0} }, /* MUST BE LAST */
5295 { "invalid key func", NULL
, {0} },
5298 RB_ENTRY(key
) entry
;
5301 enum keyfuncid funcid
;
5304 RB_HEAD(key_tree
, key
);
5307 key_cmp(struct key
*kp1
, struct key
*kp2
)
5309 if (kp1
->keysym
< kp2
->keysym
)
5311 if (kp1
->keysym
> kp2
->keysym
)
5314 if (kp1
->mod
< kp2
->mod
)
5316 if (kp1
->mod
> kp2
->mod
)
5322 RB_GENERATE(key_tree
, key
, entry
, key_cmp
);
5323 struct key_tree keys
;
5326 enum { client_click
, root_click
};
5328 unsigned int action
;
5330 unsigned int button
;
5331 void (*func
)(struct ws_win
*, union arg
*);
5334 #define MODKEY_SHIFT MODKEY | XCB_MOD_MASK_SHIFT
5335 /* action key mouse button func args */
5336 { client_click
, MODKEY
, XCB_BUTTON_INDEX_3
, resize
, {.id
= SWM_ARG_ID_DONTCENTER
} },
5337 { client_click
, MODKEY_SHIFT
, XCB_BUTTON_INDEX_3
, resize
, {.id
= SWM_ARG_ID_CENTER
} },
5338 { client_click
, MODKEY
, XCB_BUTTON_INDEX_1
, move
, {0} },
5343 update_modkey(unsigned int mod
)
5349 RB_FOREACH(kp
, key_tree
, &keys
)
5350 if (kp
->mod
& XCB_MOD_MASK_SHIFT
)
5351 kp
->mod
= mod
| XCB_MOD_MASK_SHIFT
;
5355 for (i
= 0; i
< LENGTH(buttons
); i
++)
5356 if (buttons
[i
].mask
& XCB_MOD_MASK_SHIFT
)
5357 buttons
[i
].mask
= mod
| XCB_MOD_MASK_SHIFT
;
5359 buttons
[i
].mask
= mod
;
5364 TAILQ_ENTRY(spawn_prog
) entry
;
5369 TAILQ_HEAD(spawn_list
, spawn_prog
);
5370 struct spawn_list spawns
= TAILQ_HEAD_INITIALIZER(spawns
);
5373 spawn_expand(struct swm_region
*r
, union arg
*args
, char *spawn_name
,
5376 struct spawn_prog
*prog
= NULL
;
5378 char *ap
, **real_args
;
5380 /* suppress unused warning since var is needed */
5383 DNPRINTF(SWM_D_SPAWN
, "spawn_expand: %s\n", spawn_name
);
5386 TAILQ_FOREACH(prog
, &spawns
, entry
) {
5387 if (!strcasecmp(spawn_name
, prog
->name
))
5391 warnx("spawn_custom: program %s not found", spawn_name
);
5395 /* make room for expanded args */
5396 if ((real_args
= calloc(prog
->argc
+ 1, sizeof(char *))) == NULL
)
5397 err(1, "spawn_custom: calloc real_args");
5399 /* expand spawn_args into real_args */
5400 for (i
= 0; i
< prog
->argc
; i
++) {
5402 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: raw arg: %s\n", ap
);
5403 if (!strcasecmp(ap
, "$bar_border")) {
5405 strdup(r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].name
))
5407 err(1, "spawn_custom border color");
5408 } else if (!strcasecmp(ap
, "$bar_color")) {
5410 strdup(r
->s
->c
[SWM_S_COLOR_BAR
].name
))
5412 err(1, "spawn_custom bar color");
5413 } else if (!strcasecmp(ap
, "$bar_font")) {
5414 if ((real_args
[i
] = strdup(bar_fonts
))
5416 err(1, "spawn_custom bar fonts");
5417 } else if (!strcasecmp(ap
, "$bar_font_color")) {
5419 strdup(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].name
))
5421 err(1, "spawn_custom color font");
5422 } else if (!strcasecmp(ap
, "$color_focus")) {
5424 strdup(r
->s
->c
[SWM_S_COLOR_FOCUS
].name
))
5426 err(1, "spawn_custom color focus");
5427 } else if (!strcasecmp(ap
, "$color_unfocus")) {
5429 strdup(r
->s
->c
[SWM_S_COLOR_UNFOCUS
].name
))
5431 err(1, "spawn_custom color unfocus");
5433 /* no match --> copy as is */
5434 if ((real_args
[i
] = strdup(ap
)) == NULL
)
5435 err(1, "spawn_custom strdup(ap)");
5437 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: cooked arg: %s\n",
5442 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: result: ");
5443 for (i
= 0; i
< prog
->argc
; i
++)
5444 DNPRINTF(SWM_D_SPAWN
, "\"%s\" ", real_args
[i
]);
5445 DNPRINTF(SWM_D_SPAWN
, "\n");
5447 *ret_args
= real_args
;
5448 return (prog
->argc
);
5452 spawn_custom(struct swm_region
*r
, union arg
*args
, char *spawn_name
)
5458 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
5462 spawn(r
->ws
->idx
, &a
, 1);
5464 for (i
= 0; i
< spawn_argc
; i
++)
5470 spawn_select(struct swm_region
*r
, union arg
*args
, char *spawn_name
, int *pid
)
5476 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
5480 if (pipe(select_list_pipe
) == -1)
5481 err(1, "pipe error");
5482 if (pipe(select_resp_pipe
) == -1)
5483 err(1, "pipe error");
5485 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
5486 err(1, "could not disable SIGPIPE");
5487 switch (*pid
= fork()) {
5489 err(1, "cannot fork");
5492 if (dup2(select_list_pipe
[0], 0) == -1)
5494 if (dup2(select_resp_pipe
[1], 1) == -1)
5496 close(select_list_pipe
[1]);
5497 close(select_resp_pipe
[0]);
5498 spawn(r
->ws
->idx
, &a
, 0);
5500 default: /* parent */
5501 close(select_list_pipe
[0]);
5502 close(select_resp_pipe
[1]);
5506 for (i
= 0; i
< spawn_argc
; i
++)
5512 spawn_insert(char *name
, char *args
)
5514 char *arg
, *cp
, *ptr
;
5515 struct spawn_prog
*sp
;
5517 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: %s\n", name
);
5519 if ((sp
= calloc(1, sizeof *sp
)) == NULL
)
5520 err(1, "spawn_insert: calloc");
5521 if ((sp
->name
= strdup(name
)) == NULL
)
5522 err(1, "spawn_insert: strdup");
5524 /* convert the arguments to an argument list */
5525 if ((ptr
= cp
= strdup(args
)) == NULL
)
5526 err(1, "spawn_insert: strdup");
5527 while ((arg
= strsep(&ptr
, " \t")) != NULL
) {
5528 /* empty field; skip it */
5533 if ((sp
->argv
= realloc(sp
->argv
, sp
->argc
*
5534 sizeof *sp
->argv
)) == NULL
)
5535 err(1, "spawn_insert: realloc");
5536 if ((sp
->argv
[sp
->argc
- 1] = strdup(arg
)) == NULL
)
5537 err(1, "spawn_insert: strdup");
5541 TAILQ_INSERT_TAIL(&spawns
, sp
, entry
);
5542 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: leave\n");
5546 spawn_remove(struct spawn_prog
*sp
)
5550 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: %s\n", sp
->name
);
5552 TAILQ_REMOVE(&spawns
, sp
, entry
);
5553 for (i
= 0; i
< sp
->argc
; i
++)
5559 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: leave\n");
5563 spawn_replace(struct spawn_prog
*sp
, char *name
, char *args
)
5565 DNPRINTF(SWM_D_SPAWN
, "spawn_replace: %s [%s]\n", sp
->name
, name
);
5568 spawn_insert(name
, args
);
5570 DNPRINTF(SWM_D_SPAWN
, "spawn_replace: leave\n");
5574 setspawn(char *name
, char *args
)
5576 struct spawn_prog
*sp
;
5578 DNPRINTF(SWM_D_SPAWN
, "setspawn: %s\n", name
);
5583 TAILQ_FOREACH(sp
, &spawns
, entry
) {
5584 if (!strcmp(sp
->name
, name
)) {
5588 spawn_replace(sp
, name
, args
);
5589 DNPRINTF(SWM_D_SPAWN
, "setspawn: leave\n");
5593 if (*args
== '\0') {
5594 warnx("error: setspawn: cannot find program: %s", name
);
5598 spawn_insert(name
, args
);
5599 DNPRINTF(SWM_D_SPAWN
, "setspawn: leave\n");
5603 setconfspawn(char *selector
, char *value
, int flags
)
5607 /* suppress unused warning since var is needed */
5610 args
= expand_tilde(value
);
5612 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: [%s] [%s]\n", selector
, args
);
5614 setspawn(selector
, args
);
5617 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: done.\n");
5624 setconfspawn("term", "xterm", 0);
5625 setconfspawn("spawn_term", "xterm", 0);
5626 setconfspawn("screenshot_all", "screenshot.sh full", 0);
5627 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
5628 setconfspawn("lock", "xlock", 0);
5629 setconfspawn("initscr", "initscreen.sh", 0);
5630 setconfspawn("menu", "dmenu_run"
5633 " -nf $bar_font_color"
5635 " -sf $bar_color", 0);
5636 setconfspawn("search", "dmenu"
5640 " -nf $bar_font_color"
5642 " -sf $bar_color", 0);
5643 setconfspawn("name_workspace", "dmenu"
5647 " -nf $bar_font_color"
5649 " -sf $bar_color", 0);
5653 #define SWM_MODNAME_SIZE 32
5654 #define SWM_KEY_WS "\n+ \t"
5656 parsekeys(char *keystr
, unsigned int currmod
, unsigned int *mod
, KeySym
*ks
)
5660 DNPRINTF(SWM_D_KEY
, "parsekeys: enter [%s]\n", keystr
);
5661 if (mod
== NULL
|| ks
== NULL
) {
5662 DNPRINTF(SWM_D_KEY
, "parsekeys: no mod or key vars\n");
5665 if (keystr
== NULL
|| strlen(keystr
) == 0) {
5666 DNPRINTF(SWM_D_KEY
, "parsekeys: no keystr\n");
5670 *ks
= XCB_NO_SYMBOL
;
5672 while ((name
= strsep(&cp
, SWM_KEY_WS
)) != NULL
) {
5673 DNPRINTF(SWM_D_KEY
, "parsekeys: key [%s]\n", name
);
5675 cp
+= (long)strspn(cp
, SWM_KEY_WS
);
5676 if (strncasecmp(name
, "MOD", SWM_MODNAME_SIZE
) == 0)
5678 else if (!strncasecmp(name
, "Mod1", SWM_MODNAME_SIZE
))
5679 *mod
|= XCB_MOD_MASK_1
;
5680 else if (!strncasecmp(name
, "Mod2", SWM_MODNAME_SIZE
))
5681 *mod
+= XCB_MOD_MASK_2
;
5682 else if (!strncmp(name
, "Mod3", SWM_MODNAME_SIZE
))
5683 *mod
|= XCB_MOD_MASK_3
;
5684 else if (!strncmp(name
, "Mod4", SWM_MODNAME_SIZE
))
5685 *mod
|= XCB_MOD_MASK_4
;
5686 else if (strncasecmp(name
, "SHIFT", SWM_MODNAME_SIZE
) == 0)
5687 *mod
|= XCB_MOD_MASK_SHIFT
;
5688 else if (strncasecmp(name
, "CONTROL", SWM_MODNAME_SIZE
) == 0)
5689 *mod
|= XCB_MOD_MASK_CONTROL
;
5691 *ks
= XStringToKeysym(name
);
5692 XConvertCase(*ks
, ks
, &uks
);
5693 if (ks
== XCB_NO_SYMBOL
) {
5695 "parsekeys: invalid key %s\n",
5701 DNPRINTF(SWM_D_KEY
, "parsekeys: leave ok\n");
5706 strdupsafe(char *str
)
5711 return (strdup(str
));
5715 key_insert(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
, char *spawn_name
)
5719 DNPRINTF(SWM_D_KEY
, "key_insert: enter %s [%s]\n",
5720 keyfuncs
[kfid
].name
, spawn_name
);
5722 if ((kp
= malloc(sizeof *kp
)) == NULL
)
5723 err(1, "key_insert: malloc");
5728 kp
->spawn_name
= strdupsafe(spawn_name
);
5729 RB_INSERT(key_tree
, &keys
, kp
);
5731 DNPRINTF(SWM_D_KEY
, "key_insert: leave\n");
5735 key_lookup(unsigned int mod
, KeySym ks
)
5742 return (RB_FIND(key_tree
, &keys
, &kp
));
5746 key_remove(struct key
*kp
)
5748 DNPRINTF(SWM_D_KEY
, "key_remove: %s\n", keyfuncs
[kp
->funcid
].name
);
5750 RB_REMOVE(key_tree
, &keys
, kp
);
5751 free(kp
->spawn_name
);
5754 DNPRINTF(SWM_D_KEY
, "key_remove: leave\n");
5758 key_replace(struct key
*kp
, unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
5761 DNPRINTF(SWM_D_KEY
, "key_replace: %s [%s]\n", keyfuncs
[kp
->funcid
].name
,
5765 key_insert(mod
, ks
, kfid
, spawn_name
);
5767 DNPRINTF(SWM_D_KEY
, "key_replace: leave\n");
5771 setkeybinding(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
5776 DNPRINTF(SWM_D_KEY
, "setkeybinding: enter %s [%s]\n",
5777 keyfuncs
[kfid
].name
, spawn_name
);
5779 if ((kp
= key_lookup(mod
, ks
)) != NULL
) {
5780 if (kfid
== KF_INVALID
)
5783 key_replace(kp
, mod
, ks
, kfid
, spawn_name
);
5784 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
5787 if (kfid
== KF_INVALID
) {
5788 warnx("error: setkeybinding: cannot find mod/key combination");
5789 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
5793 key_insert(mod
, ks
, kfid
, spawn_name
);
5794 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
5798 setconfbinding(char *selector
, char *value
, int flags
)
5800 enum keyfuncid kfid
;
5803 struct spawn_prog
*sp
;
5805 /* suppress unused warning since var is needed */
5808 DNPRINTF(SWM_D_KEY
, "setconfbinding: enter\n");
5809 if (selector
== NULL
) {
5810 DNPRINTF(SWM_D_KEY
, "setconfbinding: unbind %s\n", value
);
5811 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
5813 setkeybinding(mod
, ks
, kfid
, NULL
);
5818 /* search by key function name */
5819 for (kfid
= 0; kfid
< KF_INVALID
; (kfid
)++) {
5820 if (strncasecmp(selector
, keyfuncs
[kfid
].name
,
5821 SWM_FUNCNAME_LEN
) == 0) {
5822 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match\n",
5824 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
5825 setkeybinding(mod
, ks
, kfid
, NULL
);
5831 /* search by custom spawn name */
5832 TAILQ_FOREACH(sp
, &spawns
, entry
) {
5833 if (strcasecmp(selector
, sp
->name
) == 0) {
5834 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match\n",
5836 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
5837 setkeybinding(mod
, ks
, KF_SPAWN_CUSTOM
,
5844 DNPRINTF(SWM_D_KEY
, "setconfbinding: no match\n");
5851 #define MODKEY_SHIFT MODKEY | XCB_MOD_MASK_SHIFT
5852 setkeybinding(MODKEY
, XK_space
, KF_CYCLE_LAYOUT
,NULL
);
5853 setkeybinding(MODKEY_SHIFT
, XK_backslash
, KF_FLIP_LAYOUT
, NULL
);
5854 setkeybinding(MODKEY_SHIFT
, XK_space
, KF_STACK_RESET
, NULL
);
5855 setkeybinding(MODKEY
, XK_h
, KF_MASTER_SHRINK
, NULL
);
5856 setkeybinding(MODKEY
, XK_l
, KF_MASTER_GROW
, NULL
);
5857 setkeybinding(MODKEY
, XK_comma
, KF_MASTER_ADD
, NULL
);
5858 setkeybinding(MODKEY
, XK_period
, KF_MASTER_DEL
, NULL
);
5859 setkeybinding(MODKEY_SHIFT
, XK_comma
, KF_STACK_INC
, NULL
);
5860 setkeybinding(MODKEY_SHIFT
, XK_period
, KF_STACK_DEC
, NULL
);
5861 setkeybinding(MODKEY
, XK_Return
, KF_SWAP_MAIN
, NULL
);
5862 setkeybinding(MODKEY
, XK_j
, KF_FOCUS_NEXT
, NULL
);
5863 setkeybinding(MODKEY
, XK_k
, KF_FOCUS_PREV
, NULL
);
5864 setkeybinding(MODKEY_SHIFT
, XK_j
, KF_SWAP_NEXT
, NULL
);
5865 setkeybinding(MODKEY_SHIFT
, XK_k
, KF_SWAP_PREV
, NULL
);
5866 setkeybinding(MODKEY_SHIFT
, XK_Return
, KF_SPAWN_CUSTOM
,"term");
5867 setkeybinding(MODKEY
, XK_p
, KF_SPAWN_CUSTOM
,"menu");
5868 setkeybinding(MODKEY_SHIFT
, XK_q
, KF_QUIT
, NULL
);
5869 setkeybinding(MODKEY
, XK_q
, KF_RESTART
, NULL
);
5870 setkeybinding(MODKEY
, XK_m
, KF_FOCUS_MAIN
, NULL
);
5871 setkeybinding(MODKEY
, XK_1
, KF_WS_1
, NULL
);
5872 setkeybinding(MODKEY
, XK_2
, KF_WS_2
, NULL
);
5873 setkeybinding(MODKEY
, XK_3
, KF_WS_3
, NULL
);
5874 setkeybinding(MODKEY
, XK_4
, KF_WS_4
, NULL
);
5875 setkeybinding(MODKEY
, XK_5
, KF_WS_5
, NULL
);
5876 setkeybinding(MODKEY
, XK_6
, KF_WS_6
, NULL
);
5877 setkeybinding(MODKEY
, XK_7
, KF_WS_7
, NULL
);
5878 setkeybinding(MODKEY
, XK_8
, KF_WS_8
, NULL
);
5879 setkeybinding(MODKEY
, XK_9
, KF_WS_9
, NULL
);
5880 setkeybinding(MODKEY
, XK_0
, KF_WS_10
, NULL
);
5881 setkeybinding(MODKEY
, XK_F1
, KF_WS_11
, NULL
);
5882 setkeybinding(MODKEY
, XK_F2
, KF_WS_12
, NULL
);
5883 setkeybinding(MODKEY
, XK_F3
, KF_WS_13
, NULL
);
5884 setkeybinding(MODKEY
, XK_F4
, KF_WS_14
, NULL
);
5885 setkeybinding(MODKEY
, XK_F5
, KF_WS_15
, NULL
);
5886 setkeybinding(MODKEY
, XK_F6
, KF_WS_16
, NULL
);
5887 setkeybinding(MODKEY
, XK_F7
, KF_WS_17
, NULL
);
5888 setkeybinding(MODKEY
, XK_F8
, KF_WS_18
, NULL
);
5889 setkeybinding(MODKEY
, XK_F9
, KF_WS_19
, NULL
);
5890 setkeybinding(MODKEY
, XK_F10
, KF_WS_20
, NULL
);
5891 setkeybinding(MODKEY
, XK_F11
, KF_WS_21
, NULL
);
5892 setkeybinding(MODKEY
, XK_F12
, KF_WS_22
, NULL
);
5893 setkeybinding(MODKEY
, XK_Right
, KF_WS_NEXT
, NULL
);
5894 setkeybinding(MODKEY
, XK_Left
, KF_WS_PREV
, NULL
);
5895 setkeybinding(MODKEY
, XK_Up
, KF_WS_NEXT_ALL
, NULL
);
5896 setkeybinding(MODKEY
, XK_Down
, KF_WS_PREV_ALL
, NULL
);
5897 setkeybinding(MODKEY
, XK_a
, KF_WS_PRIOR
, NULL
);
5898 setkeybinding(MODKEY_SHIFT
, XK_Right
, KF_SCREEN_NEXT
, NULL
);
5899 setkeybinding(MODKEY_SHIFT
, XK_Left
, KF_SCREEN_PREV
, NULL
);
5900 setkeybinding(MODKEY_SHIFT
, XK_1
, KF_MVWS_1
, NULL
);
5901 setkeybinding(MODKEY_SHIFT
, XK_2
, KF_MVWS_2
, NULL
);
5902 setkeybinding(MODKEY_SHIFT
, XK_3
, KF_MVWS_3
, NULL
);
5903 setkeybinding(MODKEY_SHIFT
, XK_4
, KF_MVWS_4
, NULL
);
5904 setkeybinding(MODKEY_SHIFT
, XK_5
, KF_MVWS_5
, NULL
);
5905 setkeybinding(MODKEY_SHIFT
, XK_6
, KF_MVWS_6
, NULL
);
5906 setkeybinding(MODKEY_SHIFT
, XK_7
, KF_MVWS_7
, NULL
);
5907 setkeybinding(MODKEY_SHIFT
, XK_8
, KF_MVWS_8
, NULL
);
5908 setkeybinding(MODKEY_SHIFT
, XK_9
, KF_MVWS_9
, NULL
);
5909 setkeybinding(MODKEY_SHIFT
, XK_0
, KF_MVWS_10
, NULL
);
5910 setkeybinding(MODKEY_SHIFT
, XK_F1
, KF_MVWS_11
, NULL
);
5911 setkeybinding(MODKEY_SHIFT
, XK_F2
, KF_MVWS_12
, NULL
);
5912 setkeybinding(MODKEY_SHIFT
, XK_F3
, KF_MVWS_13
, NULL
);
5913 setkeybinding(MODKEY_SHIFT
, XK_F4
, KF_MVWS_14
, NULL
);
5914 setkeybinding(MODKEY_SHIFT
, XK_F5
, KF_MVWS_15
, NULL
);
5915 setkeybinding(MODKEY_SHIFT
, XK_F6
, KF_MVWS_16
, NULL
);
5916 setkeybinding(MODKEY_SHIFT
, XK_F7
, KF_MVWS_17
, NULL
);
5917 setkeybinding(MODKEY_SHIFT
, XK_F8
, KF_MVWS_18
, NULL
);
5918 setkeybinding(MODKEY_SHIFT
, XK_F9
, KF_MVWS_19
, NULL
);
5919 setkeybinding(MODKEY_SHIFT
, XK_F10
, KF_MVWS_20
, NULL
);
5920 setkeybinding(MODKEY_SHIFT
, XK_F11
, KF_MVWS_21
, NULL
);
5921 setkeybinding(MODKEY_SHIFT
, XK_F12
, KF_MVWS_22
, NULL
);
5922 setkeybinding(MODKEY
, XK_b
, KF_BAR_TOGGLE
, NULL
);
5923 setkeybinding(MODKEY
, XK_Tab
, KF_FOCUS_NEXT
, NULL
);
5924 setkeybinding(MODKEY_SHIFT
, XK_Tab
, KF_FOCUS_PREV
, NULL
);
5925 setkeybinding(MODKEY_SHIFT
, XK_x
, KF_WIND_KILL
, NULL
);
5926 setkeybinding(MODKEY
, XK_x
, KF_WIND_DEL
, NULL
);
5927 setkeybinding(MODKEY
, XK_s
, KF_SPAWN_CUSTOM
,"screenshot_all");
5928 setkeybinding(MODKEY_SHIFT
, XK_s
, KF_SPAWN_CUSTOM
,"screenshot_wind");
5929 setkeybinding(MODKEY
, XK_t
, KF_FLOAT_TOGGLE
,NULL
);
5930 setkeybinding(MODKEY_SHIFT
, XK_v
, KF_VERSION
, NULL
);
5931 setkeybinding(MODKEY_SHIFT
, XK_Delete
, KF_SPAWN_CUSTOM
,"lock");
5932 setkeybinding(MODKEY_SHIFT
, XK_i
, KF_SPAWN_CUSTOM
,"initscr");
5933 setkeybinding(MODKEY
, XK_w
, KF_ICONIFY
, NULL
);
5934 setkeybinding(MODKEY_SHIFT
, XK_w
, KF_UNICONIFY
, NULL
);
5935 setkeybinding(MODKEY_SHIFT
, XK_r
, KF_RAISE_TOGGLE
,NULL
);
5936 setkeybinding(MODKEY
, XK_v
, KF_BUTTON2
, NULL
);
5937 setkeybinding(MODKEY
, XK_equal
, KF_WIDTH_GROW
, NULL
);
5938 setkeybinding(MODKEY
, XK_minus
, KF_WIDTH_SHRINK
,NULL
);
5939 setkeybinding(MODKEY_SHIFT
, XK_equal
, KF_HEIGHT_GROW
,NULL
);
5940 setkeybinding(MODKEY_SHIFT
, XK_minus
, KF_HEIGHT_SHRINK
,NULL
);
5941 setkeybinding(MODKEY
, XK_bracketleft
, KF_MOVE_LEFT
,NULL
);
5942 setkeybinding(MODKEY
, XK_bracketright
,KF_MOVE_RIGHT
,NULL
);
5943 setkeybinding(MODKEY_SHIFT
, XK_bracketleft
, KF_MOVE_UP
, NULL
);
5944 setkeybinding(MODKEY_SHIFT
, XK_bracketright
,KF_MOVE_DOWN
,NULL
);
5945 setkeybinding(MODKEY_SHIFT
, XK_slash
, KF_NAME_WORKSPACE
,NULL
);
5946 setkeybinding(MODKEY
, XK_slash
, KF_SEARCH_WORKSPACE
,NULL
);
5947 setkeybinding(MODKEY
, XK_f
, KF_SEARCH_WIN
, NULL
);
5949 setkeybinding(MODKEY_SHIFT
, XK_d
, KF_DUMPWINS
, NULL
);
5959 while (RB_EMPTY(&keys
) == 0) {
5960 kp
= RB_ROOT(&keys
);
5966 setkeymapping(char *selector
, char *value
, int flags
)
5968 char keymapping_file
[PATH_MAX
];
5970 /* suppress unused warnings since vars are needed */
5974 DNPRINTF(SWM_D_KEY
, "setkeymapping: enter\n");
5975 if (value
[0] == '~')
5976 snprintf(keymapping_file
, sizeof keymapping_file
, "%s/%s",
5977 pwd
->pw_dir
, &value
[1]);
5979 strlcpy(keymapping_file
, value
, sizeof keymapping_file
);
5981 /* load new key bindings; if it fails, revert to default bindings */
5982 if (conf_load(keymapping_file
, SWM_CONF_KEYMAPPING
)) {
5986 DNPRINTF(SWM_D_KEY
, "setkeymapping: leave\n");
5991 updatenumlockmask(void)
5994 xcb_get_modifier_mapping_reply_t
*modmap_r
;
5995 xcb_keycode_t
*modmap
, kc
, *keycode
;
5999 modmap_r
= xcb_get_modifier_mapping_reply(conn
,
6000 xcb_get_modifier_mapping(conn
),
6003 modmap
= xcb_get_modifier_mapping_keycodes(modmap_r
);
6004 for (i
= 0; i
< 8; i
++) {
6005 for (j
= 0; j
< modmap_r
->keycodes_per_modifier
; j
++) {
6006 kc
= modmap
[i
* modmap_r
->keycodes_per_modifier
6008 keycode
= xcb_key_symbols_get_keycode(syms
,
6011 numlockmask
= (1 << i
);
6017 DNPRINTF(SWM_D_MISC
, "updatenumlockmask: %d\n", numlockmask
);
6024 int num_screens
, k
, j
;
6025 unsigned int modifiers
[3];
6026 xcb_keycode_t
*code
;
6028 DNPRINTF(SWM_D_MISC
, "grabkeys\n");
6029 updatenumlockmask();
6032 modifiers
[1] = numlockmask
;
6033 modifiers
[2] = numlockmask
| XCB_MOD_MASK_LOCK
;
6035 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
6036 for (k
= 0; k
< num_screens
; k
++) {
6037 if (TAILQ_EMPTY(&screens
[k
].rl
))
6039 xcb_ungrab_key(conn
, XCB_GRAB_ANY
, screens
[k
].root
,
6041 RB_FOREACH(kp
, key_tree
, &keys
) {
6042 if ((code
= xcb_key_symbols_get_keycode(syms
,
6044 for (j
= 0; j
< LENGTH(modifiers
); j
++)
6045 xcb_grab_key(conn
, 1,
6047 kp
->mod
| modifiers
[j
],
6048 *code
, XCB_GRAB_MODE_SYNC
,
6049 XCB_GRAB_MODE_ASYNC
);
6057 grabbuttons(struct ws_win
*win
)
6061 DNPRINTF(SWM_D_MOUSE
, "grabbuttons: win 0x%x\n", win
->id
);
6063 for (i
= 0; i
< LENGTH(buttons
); i
++)
6064 if (buttons
[i
].action
== client_click
)
6065 xcb_grab_button(conn
, 0, win
->id
, BUTTONMASK
,
6066 XCB_GRAB_MODE_SYNC
, XCB_GRAB_MODE_ASYNC
,
6067 XCB_WINDOW_NONE
, XCB_CURSOR_NONE
,
6068 buttons
[i
].button
, buttons
[i
].mask
);
6071 const char *quirkname
[] = {
6072 "NONE", /* config string for "no value" */
6081 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
6082 #define SWM_Q_WS "\n|+ \t"
6084 parsequirks(char *qstr
, unsigned long *quirk
)
6094 while ((name
= strsep(&cp
, SWM_Q_WS
)) != NULL
) {
6096 cp
+= (long)strspn(cp
, SWM_Q_WS
);
6097 for (i
= 0; i
< LENGTH(quirkname
); i
++) {
6098 if (!strncasecmp(name
, quirkname
[i
], SWM_QUIRK_LEN
)) {
6099 DNPRINTF(SWM_D_QUIRK
,
6100 "parsequirks: %s\n", name
);
6105 *quirk
|= 1 << (i
-1);
6109 if (i
>= LENGTH(quirkname
)) {
6110 DNPRINTF(SWM_D_QUIRK
,
6111 "parsequirks: invalid quirk [%s]\n", name
);
6119 quirk_insert(const char *class, const char *name
, unsigned long quirk
)
6123 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: %s:%s [%lu]\n", class, name
,
6126 if ((qp
= malloc(sizeof *qp
)) == NULL
)
6127 err(1, "quirk_insert: malloc");
6128 if ((qp
->class = strdup(class)) == NULL
)
6129 err(1, "quirk_insert: strdup");
6130 if ((qp
->name
= strdup(name
)) == NULL
)
6131 err(1, "quirk_insert: strdup");
6134 TAILQ_INSERT_TAIL(&quirks
, qp
, entry
);
6136 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: leave\n");
6140 quirk_remove(struct quirk
*qp
)
6142 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: %s:%s [%lu]\n", qp
->class,
6143 qp
->name
, qp
->quirk
);
6145 TAILQ_REMOVE(&quirks
, qp
, entry
);
6150 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: leave\n");
6154 quirk_replace(struct quirk
*qp
, const char *class, const char *name
,
6155 unsigned long quirk
)
6157 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: %s:%s [%lu]\n", qp
->class,
6158 qp
->name
, qp
->quirk
);
6161 quirk_insert(class, name
, quirk
);
6163 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: leave\n");
6167 setquirk(const char *class, const char *name
, unsigned long quirk
)
6171 DNPRINTF(SWM_D_QUIRK
, "setquirk: enter %s:%s [%lu]\n", class, name
,
6174 TAILQ_FOREACH(qp
, &quirks
, entry
) {
6175 if (!strcmp(qp
->class, class) && !strcmp(qp
->name
, name
)) {
6179 quirk_replace(qp
, class, name
, quirk
);
6180 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
6185 warnx("error: setquirk: cannot find class/name combination");
6189 quirk_insert(class, name
, quirk
);
6190 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
6194 setconfquirk(char *selector
, char *value
, int flags
)
6196 char *cp
, *class, *name
;
6200 /* suppress unused warning since var is needed */
6203 if (selector
== NULL
)
6205 if ((cp
= strchr(selector
, ':')) == NULL
)
6210 if ((retval
= parsequirks(value
, &qrks
)) == 0)
6211 setquirk(class, name
, qrks
);
6218 setquirk("MPlayer", "xv", SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
| SWM_Q_FOCUSPREV
);
6219 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT
);
6220 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ
);
6221 setquirk("Firefox", "Dialog", SWM_Q_FLOAT
);
6222 setquirk("Gimp", "gimp", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
6223 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ
);
6224 setquirk("xine", "Xine Window", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
6225 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
6226 setquirk("xine", "xine Panel", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
6227 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
6228 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN
| SWM_Q_FLOAT
);
6229 setquirk("pcb", "pcb", SWM_Q_FLOAT
);
6230 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
);
6233 /* conf file stuff */
6234 #define SWM_CONF_FILE "spectrwm.conf"
6235 #define SWM_CONF_FILE_OLD "scrotwm.conf"
6239 SWM_S_BAR_AT_BOTTOM
,
6240 SWM_S_BAR_BORDER_WIDTH
,
6247 SWM_S_CLOCK_ENABLED
,
6250 SWM_S_CYCLE_VISIBLE
,
6252 SWM_S_DISABLE_BORDER
,
6254 SWM_S_FOCUS_CLOSE_WRAP
,
6255 SWM_S_FOCUS_DEFAULT
,
6261 SWM_S_STACK_ENABLED
,
6263 SWM_S_TITLE_CLASS_ENABLED
,
6264 SWM_S_TITLE_NAME_ENABLED
,
6265 SWM_S_URGENT_ENABLED
,
6266 SWM_S_VERBOSE_LAYOUT
,
6267 SWM_S_WINDOW_NAME_ENABLED
,
6268 SWM_S_WORKSPACE_LIMIT
6272 setconfvalue(char *selector
, char *value
, int flags
)
6277 /* suppress unused warning since var is needed */
6281 case SWM_S_BAR_ACTION
:
6283 if ((bar_argv
[0] = expand_tilde(value
)) == NULL
)
6284 err(1, "setconfvalue: bar_action");
6286 case SWM_S_BAR_AT_BOTTOM
:
6287 bar_at_bottom
= atoi(value
);
6289 case SWM_S_BAR_BORDER_WIDTH
:
6290 bar_border_width
= atoi(value
);
6291 if (bar_border_width
< 0)
6292 bar_border_width
= 0;
6294 case SWM_S_BAR_DELAY
:
6295 bar_delay
= atoi(value
);
6297 case SWM_S_BAR_ENABLED
:
6298 bar_enabled
= atoi(value
);
6300 case SWM_S_BAR_FONT
:
6302 if (asprintf(&bar_fonts
, "%s,%s", value
, bar_fonts
) == -1)
6303 err(1, "setconfvalue: asprintf: failed to allocate "
6304 "memory for bar_fonts.");
6307 /* If already in xft mode, then we are done. */
6308 if (!bar_font_legacy
)
6311 /* If there are any non-XLFD entries, switch to Xft mode. */
6312 while ((b
= strsep(&value
, ",")) != NULL
) {
6316 bar_font_legacy
= 0;
6321 case SWM_S_BAR_FORMAT
:
6323 if ((bar_format
= strdup(value
)) == NULL
)
6324 err(1, "setconfvalue: bar_format");
6326 case SWM_S_BAR_JUSTIFY
:
6327 if (!strcmp(value
, "left"))
6328 bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
6329 else if (!strcmp(value
, "center"))
6330 bar_justify
= SWM_BAR_JUSTIFY_CENTER
;
6331 else if (!strcmp(value
, "right"))
6332 bar_justify
= SWM_BAR_JUSTIFY_RIGHT
;
6334 errx(1, "invalid bar_justify");
6336 case SWM_S_BORDER_WIDTH
:
6337 border_width
= atoi(value
);
6338 if (border_width
< 0)
6341 case SWM_S_CLOCK_ENABLED
:
6342 clock_enabled
= atoi(value
);
6344 case SWM_S_CLOCK_FORMAT
:
6345 #ifndef SWM_DENY_CLOCK_FORMAT
6347 if ((clock_format
= strdup(value
)) == NULL
)
6348 err(1, "setconfvalue: clock_format");
6351 case SWM_S_CYCLE_EMPTY
:
6352 cycle_empty
= atoi(value
);
6354 case SWM_S_CYCLE_VISIBLE
:
6355 cycle_visible
= atoi(value
);
6357 case SWM_S_DIALOG_RATIO
:
6358 dialog_ratio
= atof(value
);
6359 if (dialog_ratio
> 1.0 || dialog_ratio
<= .3)
6362 case SWM_S_DISABLE_BORDER
:
6363 disable_border
= atoi(value
);
6365 case SWM_S_FOCUS_CLOSE
:
6366 if (!strcmp(value
, "first"))
6367 focus_close
= SWM_STACK_BOTTOM
;
6368 else if (!strcmp(value
, "last"))
6369 focus_close
= SWM_STACK_TOP
;
6370 else if (!strcmp(value
, "next"))
6371 focus_close
= SWM_STACK_ABOVE
;
6372 else if (!strcmp(value
, "previous"))
6373 focus_close
= SWM_STACK_BELOW
;
6375 errx(1, "focus_close");
6377 case SWM_S_FOCUS_CLOSE_WRAP
:
6378 focus_close_wrap
= atoi(value
);
6380 case SWM_S_FOCUS_DEFAULT
:
6381 if (!strcmp(value
, "last"))
6382 focus_default
= SWM_STACK_TOP
;
6383 else if (!strcmp(value
, "first"))
6384 focus_default
= SWM_STACK_BOTTOM
;
6386 errx(1, "focus_default");
6388 case SWM_S_FOCUS_MODE
:
6389 if (!strcmp(value
, "default"))
6390 focus_mode
= SWM_FOCUS_DEFAULT
;
6391 else if (!strcmp(value
, "follow") ||
6392 !strcmp(value
, "follow_cursor"))
6393 focus_mode
= SWM_FOCUS_FOLLOW
;
6394 else if (!strcmp(value
, "manual"))
6395 focus_mode
= SWM_FOCUS_MANUAL
;
6397 errx(1, "focus_mode");
6399 case SWM_S_SPAWN_ORDER
:
6400 if (!strcmp(value
, "first"))
6401 spawn_position
= SWM_STACK_BOTTOM
;
6402 else if (!strcmp(value
, "last"))
6403 spawn_position
= SWM_STACK_TOP
;
6404 else if (!strcmp(value
, "next"))
6405 spawn_position
= SWM_STACK_ABOVE
;
6406 else if (!strcmp(value
, "previous"))
6407 spawn_position
= SWM_STACK_BELOW
;
6409 errx(1, "spawn_position");
6411 case SWM_S_SPAWN_TERM
:
6412 setconfspawn("term", value
, 0);
6413 setconfspawn("spawn_term", value
, 0);
6417 case SWM_S_SS_ENABLED
:
6418 ss_enabled
= atoi(value
);
6420 case SWM_S_STACK_ENABLED
:
6421 stack_enabled
= atoi(value
);
6423 case SWM_S_TERM_WIDTH
:
6424 term_width
= atoi(value
);
6428 case SWM_S_TITLE_CLASS_ENABLED
:
6429 title_class_enabled
= atoi(value
);
6431 case SWM_S_TITLE_NAME_ENABLED
:
6432 title_name_enabled
= atoi(value
);
6434 case SWM_S_URGENT_ENABLED
:
6435 urgent_enabled
= atoi(value
);
6437 case SWM_S_VERBOSE_LAYOUT
:
6438 verbose_layout
= atoi(value
);
6439 for (i
= 0; layouts
[i
].l_stack
!= NULL
; i
++) {
6441 layouts
[i
].l_string
= fancy_stacker
;
6443 layouts
[i
].l_string
= plain_stacker
;
6446 case SWM_S_WINDOW_NAME_ENABLED
:
6447 window_name_enabled
= atoi(value
);
6449 case SWM_S_WORKSPACE_LIMIT
:
6450 workspace_limit
= atoi(value
);
6451 if (workspace_limit
> SWM_WS_MAX
)
6452 workspace_limit
= SWM_WS_MAX
;
6453 else if (workspace_limit
< 1)
6454 workspace_limit
= 1;
6463 setconfmodkey(char *selector
, char *value
, int flags
)
6465 /* suppress unused warnings since vars are needed */
6469 if (!strncasecmp(value
, "Mod1", strlen("Mod1")))
6470 update_modkey(XCB_MOD_MASK_1
);
6471 else if (!strncasecmp(value
, "Mod2", strlen("Mod2")))
6472 update_modkey(XCB_MOD_MASK_2
);
6473 else if (!strncasecmp(value
, "Mod3", strlen("Mod3")))
6474 update_modkey(XCB_MOD_MASK_3
);
6475 else if (!strncasecmp(value
, "Mod4", strlen("Mod4")))
6476 update_modkey(XCB_MOD_MASK_4
);
6483 setconfcolor(char *selector
, char *value
, int flags
)
6485 setscreencolor(value
, ((selector
== NULL
)?-1:atoi(selector
)), flags
);
6490 setconfregion(char *selector
, char *value
, int flags
)
6492 /* suppress unused warnings since vars are needed */
6496 custom_region(value
);
6501 setautorun(char *selector
, char *value
, int flags
)
6511 /* suppress unused warnings since vars are needed */
6515 if (getenv("SWM_STARTED"))
6519 if (sscanf(value
, "ws[%d]:%1023c", &ws_id
, s
) != 2)
6520 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
6522 if (ws_id
< 0 || ws_id
>= workspace_limit
)
6523 errx(1, "autorun: invalid workspace %d", ws_id
+ 1);
6526 * This is a little intricate
6528 * If the pid already exists we simply reuse it because it means it was
6529 * used before AND not claimed by manage_window. We get away with
6530 * altering it in the parent after INSERT because this can not be a race
6533 while ((ap
= strsep(&sp
, " \t")) != NULL
) {
6536 DNPRINTF(SWM_D_SPAWN
, "setautorun: arg [%s]\n", ap
);
6538 if ((a
.argv
= realloc(a
.argv
, argc
* sizeof(char *))) == NULL
)
6539 err(1, "setautorun: realloc");
6540 a
.argv
[argc
- 1] = ap
;
6543 if ((a
.argv
= realloc(a
.argv
, (argc
+ 1) * sizeof(char *))) == NULL
)
6544 err(1, "setautorun: realloc");
6545 a
.argv
[argc
] = NULL
;
6547 if ((pid
= fork()) == 0) {
6548 spawn(ws_id
, &a
, 1);
6557 p
= calloc(1, sizeof *p
);
6560 TAILQ_INSERT_TAIL(&pidlist
, p
, entry
);
6570 setlayout(char *selector
, char *value
, int flags
)
6572 int ws_id
, i
, x
, mg
, ma
, si
, ar
, f
= 0;
6573 int st
= SWM_V_STACK
, num_screens
;
6575 struct workspace
*ws
;
6577 /* suppress unused warnings since vars are needed */
6581 if (getenv("SWM_STARTED"))
6585 if (sscanf(value
, "ws[%d]:%d:%d:%d:%d:%1023c",
6586 &ws_id
, &mg
, &ma
, &si
, &ar
, s
) != 6)
6587 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
6588 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
6591 if (ws_id
< 0 || ws_id
>= workspace_limit
)
6592 errx(1, "layout: invalid workspace %d", ws_id
+ 1);
6594 if (!strcasecmp(s
, "vertical"))
6596 else if (!strcasecmp(s
, "vertical_flip")) {
6599 } else if (!strcasecmp(s
, "horizontal"))
6601 else if (!strcasecmp(s
, "horizontal_flip")) {
6604 } else if (!strcasecmp(s
, "fullscreen"))
6607 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
6608 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
6611 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
6612 for (i
= 0; i
< num_screens
; i
++) {
6613 ws
= (struct workspace
*)&screens
[i
].ws
;
6614 ws
[ws_id
].cur_layout
= &layouts
[st
];
6616 ws
[ws_id
].always_raise
= ar
;
6617 if (st
== SWM_MAX_STACK
)
6621 for (x
= 0; x
< abs(mg
); x
++) {
6622 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
6623 mg
>= 0 ? SWM_ARG_ID_MASTERGROW
:
6624 SWM_ARG_ID_MASTERSHRINK
);
6628 for (x
= 0; x
< abs(ma
); x
++) {
6629 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
6630 ma
>= 0 ? SWM_ARG_ID_MASTERADD
:
6631 SWM_ARG_ID_MASTERDEL
);
6635 for (x
= 0; x
< abs(si
); x
++) {
6636 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
6637 si
>= 0 ? SWM_ARG_ID_STACKINC
:
6638 SWM_ARG_ID_STACKDEC
);
6643 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
6644 SWM_ARG_ID_FLIPLAYOUT
);
6654 /* config options */
6655 struct config_option
{
6657 int (*func
)(char*, char*, int);
6660 struct config_option configopt
[] = {
6661 { "bar_enabled", setconfvalue
, SWM_S_BAR_ENABLED
},
6662 { "bar_at_bottom", setconfvalue
, SWM_S_BAR_AT_BOTTOM
},
6663 { "bar_border", setconfcolor
, SWM_S_COLOR_BAR_BORDER
},
6664 { "bar_border_width", setconfvalue
, SWM_S_BAR_BORDER_WIDTH
},
6665 { "bar_color", setconfcolor
, SWM_S_COLOR_BAR
},
6666 { "bar_font_color", setconfcolor
, SWM_S_COLOR_BAR_FONT
},
6667 { "bar_font", setconfvalue
, SWM_S_BAR_FONT
},
6668 { "bar_action", setconfvalue
, SWM_S_BAR_ACTION
},
6669 { "bar_delay", setconfvalue
, SWM_S_BAR_DELAY
},
6670 { "bar_justify", setconfvalue
, SWM_S_BAR_JUSTIFY
},
6671 { "bar_format", setconfvalue
, SWM_S_BAR_FORMAT
},
6672 { "keyboard_mapping", setkeymapping
, 0 },
6673 { "bind", setconfbinding
, 0 },
6674 { "stack_enabled", setconfvalue
, SWM_S_STACK_ENABLED
},
6675 { "clock_enabled", setconfvalue
, SWM_S_CLOCK_ENABLED
},
6676 { "clock_format", setconfvalue
, SWM_S_CLOCK_FORMAT
},
6677 { "color_focus", setconfcolor
, SWM_S_COLOR_FOCUS
},
6678 { "color_unfocus", setconfcolor
, SWM_S_COLOR_UNFOCUS
},
6679 { "cycle_empty", setconfvalue
, SWM_S_CYCLE_EMPTY
},
6680 { "cycle_visible", setconfvalue
, SWM_S_CYCLE_VISIBLE
},
6681 { "workspace_limit", setconfvalue
, SWM_S_WORKSPACE_LIMIT
},
6682 { "dialog_ratio", setconfvalue
, SWM_S_DIALOG_RATIO
},
6683 { "verbose_layout", setconfvalue
, SWM_S_VERBOSE_LAYOUT
},
6684 { "modkey", setconfmodkey
, 0 },
6685 { "program", setconfspawn
, 0 },
6686 { "quirk", setconfquirk
, 0 },
6687 { "region", setconfregion
, 0 },
6688 { "spawn_term", setconfvalue
, SWM_S_SPAWN_TERM
},
6689 { "screenshot_enabled", setconfvalue
, SWM_S_SS_ENABLED
},
6690 { "screenshot_app", setconfvalue
, SWM_S_SS_APP
},
6691 { "window_name_enabled", setconfvalue
, SWM_S_WINDOW_NAME_ENABLED
},
6692 { "urgent_enabled", setconfvalue
, SWM_S_URGENT_ENABLED
},
6693 { "term_width", setconfvalue
, SWM_S_TERM_WIDTH
},
6694 { "title_class_enabled", setconfvalue
, SWM_S_TITLE_CLASS_ENABLED
},
6695 { "title_name_enabled", setconfvalue
, SWM_S_TITLE_NAME_ENABLED
},
6696 { "focus_mode", setconfvalue
, SWM_S_FOCUS_MODE
},
6697 { "focus_close", setconfvalue
, SWM_S_FOCUS_CLOSE
},
6698 { "focus_close_wrap", setconfvalue
, SWM_S_FOCUS_CLOSE_WRAP
},
6699 { "focus_default", setconfvalue
, SWM_S_FOCUS_DEFAULT
},
6700 { "spawn_position", setconfvalue
, SWM_S_SPAWN_ORDER
},
6701 { "disable_border", setconfvalue
, SWM_S_DISABLE_BORDER
},
6702 { "border_width", setconfvalue
, SWM_S_BORDER_WIDTH
},
6703 { "autorun", setautorun
, 0 },
6704 { "layout", setlayout
, 0 },
6708 conf_load(char *filename
, int keymapping
)
6711 char *line
, *cp
, *optsub
, *optval
;
6712 size_t linelen
, lineno
= 0;
6713 int wordlen
, i
, optidx
;
6714 struct config_option
*opt
= NULL
;
6716 DNPRINTF(SWM_D_CONF
, "conf_load: begin\n");
6718 if (filename
== NULL
) {
6719 warnx("conf_load: no filename");
6722 if ((config
= fopen(filename
, "r")) == NULL
) {
6723 warn("conf_load: fopen: %s", filename
);
6727 while (!feof(config
)) {
6728 if ((line
= fparseln(config
, &linelen
, &lineno
, NULL
, 0))
6731 err(1, "%s", filename
);
6736 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
6737 if (cp
[0] == '\0') {
6742 /* get config option */
6743 wordlen
= strcspn(cp
, "=[ \t\n");
6745 warnx("%s: line %zd: no option found",
6750 for (i
= 0; i
< LENGTH(configopt
); i
++) {
6751 opt
= &configopt
[i
];
6752 if (!strncasecmp(cp
, opt
->optname
, wordlen
) &&
6753 (int)strlen(opt
->optname
) == wordlen
) {
6759 warnx("%s: line %zd: unknown option %.*s",
6760 filename
, lineno
, wordlen
, cp
);
6763 if (keymapping
&& opt
&& strcmp(opt
->optname
, "bind")) {
6764 warnx("%s: line %zd: invalid option %.*s",
6765 filename
, lineno
, wordlen
, cp
);
6769 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
6770 /* get [selector] if any */
6774 wordlen
= strcspn(cp
, "]");
6777 warnx("%s: line %zd: syntax error",
6782 if (asprintf(&optsub
, "%.*s", wordlen
, cp
) ==
6784 warnx("%s: line %zd: unable to allocate"
6785 "memory for selector", filename
,
6791 cp
+= strspn(cp
, "] \t\n"); /* eat trailing */
6793 cp
+= strspn(cp
, "= \t\n"); /* eat trailing */
6795 optval
= strdup(cp
);
6796 /* call function to deal with it all */
6797 if (configopt
[optidx
].func(optsub
, optval
,
6798 configopt
[optidx
].funcflags
) != 0)
6799 errx(1, "%s: line %zd: invalid data for %s",
6800 filename
, lineno
, configopt
[optidx
].optname
);
6807 DNPRINTF(SWM_D_CONF
, "conf_load: end\n");
6814 DNPRINTF(SWM_D_CONF
, "conf_load: end with error.\n");
6820 set_child_transient(struct ws_win
*win
, xcb_window_t
*trans
)
6822 struct ws_win
*parent
, *w
;
6823 struct swm_region
*r
;
6824 struct workspace
*ws
;
6825 xcb_icccm_wm_hints_t wmh
;
6827 parent
= find_window(win
->transient
);
6829 parent
->focus_child
= win
;
6831 DNPRINTF(SWM_D_MISC
, "set_child_transient: parent doesn't exist"
6832 " for 0x%x trans 0x%x\n", win
->id
, win
->transient
);
6834 r
= root_to_region(win
->wa
->root
, SWM_CK_ALL
);
6836 /* parent doen't exist in our window list */
6837 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
6838 if (xcb_icccm_get_wm_hints_reply(conn
,
6839 xcb_icccm_get_wm_hints(conn
, w
->id
),
6841 warnx("can't get hints for 0x%x", w
->id
);
6845 if (win
->hints
.window_group
!= wmh
.window_group
)
6848 w
->focus_child
= win
;
6849 win
->transient
= w
->id
;
6851 DNPRINTF(SWM_D_MISC
, "set_child_transient: adjusting "
6852 "transient to 0x%x\n", win
->transient
);
6859 window_get_pid(xcb_window_t win
)
6864 xcb_get_property_cookie_t pc
;
6865 xcb_get_property_reply_t
*pr
;
6867 apid
= get_atom_from_string("_NET_WM_PID");
6868 if (apid
== XCB_ATOM_NONE
)
6871 pc
= xcb_get_property(conn
, 0, win
, apid
, XCB_ATOM_CARDINAL
, 0, 1);
6872 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
6875 if (pr
->type
!= XCB_ATOM_CARDINAL
) {
6880 if (pr
->type
== apid
&& pr
->format
== 32)
6881 ret
= *((pid_t
*)xcb_get_property_value(pr
));
6887 apid
= get_atom_from_string("_SWM_PID");
6888 pc
= xcb_get_property(conn
, 0, win
, apid
, XCB_ATOM_STRING
,
6890 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
6893 if (pr
->type
!= apid
) {
6898 ret
= (pid_t
)strtonum(xcb_get_property_value(pr
), 0, INT_MAX
, &errstr
);
6905 get_ws_idx(xcb_window_t id
)
6911 xcb_get_property_reply_t
*gpr
;
6913 gpr
= xcb_get_property_reply(conn
,
6914 xcb_get_property(conn
, 0, id
, a_swm_ws
,
6915 XCB_ATOM_STRING
, 0, SWM_PROPLEN
),
6920 proplen
= xcb_get_property_value_length(gpr
);
6922 prop
= malloc(proplen
+ 1);
6925 xcb_get_property_value(gpr
),
6927 prop
[proplen
] = '\0';
6934 DNPRINTF(SWM_D_PROP
, "get_ws_idx: _SWM_WS: %s\n", prop
);
6935 ws_idx
= (int)strtonum(prop
, 0, workspace_limit
- 1, &errstr
);
6937 DNPRINTF(SWM_D_PROP
, "get_ws_idx: window: #%s: %s",
6947 manage_window(xcb_window_t id
, uint16_t mapped
)
6949 xcb_window_t trans
= XCB_WINDOW_NONE
;
6950 struct ws_win
*win
, *ww
;
6952 char ws_idx_str
[SWM_PROPLEN
];
6953 struct swm_region
*r
;
6957 xcb_icccm_get_wm_protocols_reply_t wpr
;
6959 if ((win
= find_window(id
)) != NULL
) {
6960 DNPRINTF(SWM_D_MISC
, "manage_window: win 0x%x already "
6961 "managed; skipping.)\n", id
);
6962 return (win
); /* Already managed. */
6965 /* See if window is on the unmanaged list. */
6966 if ((win
= find_unmanaged_window(id
)) != NULL
) {
6967 DNPRINTF(SWM_D_MISC
, "manage_window: win 0x%x found on "
6968 "unmanaged list.\n", id
);
6969 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
6972 set_child_transient(win
, &trans
);
6976 DNPRINTF(SWM_D_MISC
, "manage_window: win 0x%x is new.\n", id
);
6979 /* Create and initialize ws_win object. */
6980 if ((win
= calloc(1, sizeof(struct ws_win
))) == NULL
)
6981 err(1, "manage_window: calloc: failed to allocate memory for "
6986 /* Get window geometry. */
6987 win
->wa
= xcb_get_geometry_reply(conn
,
6988 xcb_get_geometry(conn
, win
->id
),
6991 /* Figure out which region the window belongs to. */
6992 r
= root_to_region(win
->wa
->root
, SWM_CK_ALL
);
6994 /* Ignore window border if there is one. */
6995 WIDTH(win
) = win
->wa
->width
;
6996 HEIGHT(win
) = win
->wa
->height
;
6997 X(win
) = win
->wa
->x
+ win
->wa
->border_width
- border_width
;
6998 Y(win
) = win
->wa
->y
+ win
->wa
->border_width
- border_width
;
7000 win
->mapped
= mapped
;
7001 win
->floatmaxed
= 0;
7002 win
->ewmh_flags
= 0;
7003 win
->s
= r
->s
; /* this never changes */
7005 store_float_geom(win
, r
);
7007 /* Get WM_SIZE_HINTS. */
7008 xcb_icccm_get_wm_normal_hints_reply(conn
,
7009 xcb_icccm_get_wm_normal_hints(conn
, win
->id
),
7013 xcb_icccm_get_wm_hints_reply(conn
,
7014 xcb_icccm_get_wm_hints(conn
, win
->id
),
7017 /* Get WM_TRANSIENT_FOR; see if window is a transient. */
7018 xcb_icccm_get_wm_transient_for_reply(conn
,
7019 xcb_icccm_get_wm_transient_for(conn
, win
->id
),
7022 win
->transient
= trans
;
7023 set_child_transient(win
, &win
->transient
);
7026 /* Get supported protocols. */
7027 if (xcb_icccm_get_wm_protocols_reply(conn
,
7028 xcb_icccm_get_wm_protocols(conn
, win
->id
, a_prot
),
7030 for (i
= 0; i
< wpr
.atoms_len
; i
++) {
7031 if (wpr
.atoms
[i
] == a_takefocus
)
7032 win
->take_focus
= 1;
7033 if (wpr
.atoms
[i
] == a_delete
)
7034 win
->can_delete
= 1;
7036 xcb_icccm_get_wm_protocols_reply_wipe(&wpr
);
7039 win
->iconic
= get_swm_iconic(win
);
7041 /* Figure out which workspace the window belongs to. */
7042 if ((p
= find_pid(window_get_pid(win
->id
))) != NULL
) {
7043 win
->ws
= &r
->s
->ws
[p
->ws
];
7044 TAILQ_REMOVE(&pidlist
, p
, entry
);
7047 } else if ((ws_idx
= get_ws_idx(win
->id
)) != -1 &&
7049 /* _SWM_WS is set; use that. */
7050 win
->ws
= &r
->s
->ws
[ws_idx
];
7051 } else if (trans
&& (ww
= find_window(trans
)) != NULL
) {
7052 /* Launch transients in the same ws as parent. */
7058 /* Set the _SWM_WS atom so we can remember this after reincarnation. */
7059 if (snprintf(ws_idx_str
, SWM_PROPLEN
, "%d", win
->ws
->idx
) <
7061 DNPRINTF(SWM_D_PROP
, "manage_window: set _SWM_WS: %s\n",
7063 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
7064 a_swm_ws
, XCB_ATOM_STRING
, 8, strlen(ws_idx_str
),
7069 ewmh_autoquirk(win
);
7071 /* Determine initial quirks. */
7072 if (xcb_icccm_get_wm_class_reply(conn
,
7073 xcb_icccm_get_wm_class(conn
, win
->id
),
7075 DNPRINTF(SWM_D_CLASS
, "manage_window: class: %s, name: %s\n",
7076 win
->ch
.class_name
, win
->ch
.instance_name
);
7078 /* java is retarded so treat it special */
7079 if (strstr(win
->ch
.instance_name
, "sun-awt")) {
7080 DNPRINTF(SWM_D_CLASS
, "manage_window: java window "
7085 TAILQ_FOREACH(qp
, &quirks
, entry
) {
7086 if (!strcmp(win
->ch
.class_name
, qp
->class) &&
7087 !strcmp(win
->ch
.instance_name
, qp
->name
)) {
7088 DNPRINTF(SWM_D_CLASS
, "manage_window: on quirks"
7089 "list; mask: 0x%lx\n", qp
->quirk
);
7090 if (qp
->quirk
& SWM_Q_FLOAT
)
7092 win
->quirks
= qp
->quirk
;
7097 /* Alter window position if quirky */
7098 if (win
->quirks
& SWM_Q_ANYWHERE
)
7101 /* Reset font sizes (the bruteforce way; no default keybinding). */
7102 if (win
->quirks
& SWM_Q_XTERM_FONTADJ
) {
7103 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
7104 fake_keypress(win
, XK_KP_Subtract
, XCB_MOD_MASK_SHIFT
);
7105 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
7106 fake_keypress(win
, XK_KP_Add
, XCB_MOD_MASK_SHIFT
);
7109 /* Make sure window is positioned inside its region, if its active. */
7111 constrain_window(win
, win
->ws
->r
, 0);
7115 /* Select which X events to monitor and set border pixel color. */
7116 wa
[0] = win
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
;
7117 wa
[1] = XCB_EVENT_MASK_ENTER_WINDOW
| XCB_EVENT_MASK_PROPERTY_CHANGE
|
7118 XCB_EVENT_MASK_STRUCTURE_NOTIFY
;
7120 wa
[1] |= XCB_EVENT_MASK_LEAVE_WINDOW
| XCB_EVENT_MASK_FOCUS_CHANGE
;
7123 xcb_change_window_attributes(conn
, win
->id
, XCB_CW_BORDER_PIXEL
|
7124 XCB_CW_EVENT_MASK
, wa
);
7127 /* Figure out where to stack the window in the workspace. */
7128 if (trans
&& (ww
= find_window(trans
)))
7129 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, ww
, win
, entry
);
7130 else if (win
->ws
->focus
&& spawn_position
== SWM_STACK_ABOVE
)
7131 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, win
->ws
->focus
, win
,
7133 else if (win
->ws
->focus
&& spawn_position
== SWM_STACK_BELOW
)
7134 TAILQ_INSERT_BEFORE(win
->ws
->focus
, win
, entry
);
7135 else switch (spawn_position
) {
7138 case SWM_STACK_ABOVE
:
7139 TAILQ_INSERT_TAIL(&win
->ws
->winlist
, win
, entry
);
7141 case SWM_STACK_BOTTOM
:
7142 case SWM_STACK_BELOW
:
7143 TAILQ_INSERT_HEAD(&win
->ws
->winlist
, win
, entry
);
7146 /* Get initial _NET_WM_STATE */
7147 ewmh_get_win_state(win
);
7148 /* Set initial _NET_WM_ALLOWED_ACTIONS */
7149 ewmh_update_actions(win
);
7153 DNPRINTF(SWM_D_MISC
, "manage_window: done. window: 0x%x, (x,y) w x h: "
7154 "(%d,%d) %d x %d, ws: %d, iconic: %s, transient: 0x%x\n", win
->id
,
7155 X(win
), Y(win
), WIDTH(win
), HEIGHT(win
), win
->ws
->idx
,
7156 YESNO(win
->iconic
), win
->transient
);
7162 free_window(struct ws_win
*win
)
7164 DNPRINTF(SWM_D_MISC
, "free_window: window: 0x%x\n", win
->id
);
7169 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
7174 xcb_icccm_get_wm_class_reply_wipe(&win
->ch
);
7179 memset(win
, 0xff, sizeof *win
); /* XXX kill later */
7182 DNPRINTF(SWM_D_MISC
, "free_window: done.\n");
7186 unmanage_window(struct ws_win
*win
)
7188 struct ws_win
*parent
;
7193 DNPRINTF(SWM_D_MISC
, "unmanage_window: window: 0x%x\n", win
->id
);
7195 if (win
->transient
) {
7196 parent
= find_window(win
->transient
);
7198 parent
->focus_child
= NULL
;
7201 TAILQ_REMOVE(&win
->ws
->winlist
, win
, entry
);
7202 TAILQ_INSERT_TAIL(&win
->ws
->unmanagedlist
, win
, entry
);
7206 expose(xcb_expose_event_t
*e
)
7209 struct swm_region
*r
;
7211 DNPRINTF(SWM_D_EVENT
, "expose: window: 0x%x\n", e
->window
);
7213 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7214 for (i
= 0; i
< num_screens
; i
++)
7215 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
7216 if (e
->window
== WINID(r
->bar
))
7224 focusin(xcb_focus_in_event_t
*e
)
7226 DNPRINTF(SWM_D_EVENT
, "focusin: window: 0x%x, mode: %s(%u), "
7227 "detail: %s(%u)\n", e
->event
, get_notify_mode_label(e
->mode
),
7228 e
->mode
, get_notify_detail_label(e
->detail
), e
->detail
);
7232 focusout(xcb_focus_out_event_t
*e
)
7234 DNPRINTF(SWM_D_EVENT
, "focusout: window: 0x%x, mode: %s(%u), "
7235 "detail: %s(%u)\n", e
->event
, get_notify_mode_label(e
->mode
),
7236 e
->mode
, get_notify_detail_label(e
->detail
), e
->detail
);
7241 keypress(xcb_key_press_event_t
*e
)
7243 xcb_keysym_t keysym
;
7246 keysym
= xcb_key_press_lookup_keysym(syms
, e
, 0);
7248 DNPRINTF(SWM_D_EVENT
, "keypress: %u %u\n", e
->detail
, keysym
);
7250 if ((kp
= key_lookup(CLEANMASK(e
->state
), keysym
)) == NULL
)
7253 last_event_time
= e
->time
;
7255 if (kp
->funcid
== KF_SPAWN_CUSTOM
)
7256 spawn_custom(root_to_region(e
->root
, SWM_CK_ALL
),
7257 &(keyfuncs
[kp
->funcid
].args
), kp
->spawn_name
);
7258 else if (keyfuncs
[kp
->funcid
].func
)
7259 keyfuncs
[kp
->funcid
].func(root_to_region(e
->root
, SWM_CK_ALL
),
7260 &(keyfuncs
[kp
->funcid
].args
));
7264 buttonpress(xcb_button_press_event_t
*e
)
7266 struct ws_win
*win
= NULL
;
7267 struct swm_region
*r
, *old_r
;
7271 DNPRINTF(SWM_D_EVENT
, "buttonpress: win (x,y): 0x%x (%d,%d), "
7272 "detail: %u, time: %u, root (x,y): 0x%x (%d,%d), child: 0x%x, "
7273 "state: %u, same_screen: %s\n", e
->event
, e
->event_x
, e
->event_y
,
7274 e
->detail
, e
->time
, e
->root
, e
->root_x
, e
->root_y
, e
->child
,
7275 e
->state
, YESNO(e
->same_screen
));
7277 if (e
->event
== e
->root
) {
7278 if (e
->child
!= 0) {
7279 win
= find_window(e
->child
);
7280 /* Pass ButtonPress to window if it isn't managed. */
7284 /* Focus on empty region */
7285 /* If no windows on region if its empty. */
7286 r
= root_to_region(e
->root
, SWM_CK_POINTER
);
7287 if (r
&& TAILQ_EMPTY(&r
->ws
->winlist
)) {
7288 old_r
= root_to_region(e
->root
, SWM_CK_FOCUS
);
7289 if (old_r
&& old_r
!= r
)
7290 unfocus_win(old_r
->ws
->focus
);
7292 xcb_set_input_focus(conn
,
7293 XCB_INPUT_FOCUS_PARENT
, e
->root
, e
->time
);
7295 /* Clear bar since empty. */
7303 win
= find_window(e
->event
);
7309 last_event_time
= e
->time
;
7311 focus_win(get_focus_magic(win
));
7313 for (i
= 0; i
< LENGTH(buttons
); i
++)
7314 if (client_click
== buttons
[i
].action
&& buttons
[i
].func
&&
7315 buttons
[i
].button
== e
->detail
&&
7316 CLEANMASK(buttons
[i
].mask
) == CLEANMASK(e
->state
)) {
7317 buttons
[i
].func(win
, &buttons
[i
].args
);
7323 DNPRINTF(SWM_D_EVENT
, "buttonpress: passing to window.\n");
7324 /* Replay event to event window */
7325 xcb_allow_events(conn
, XCB_ALLOW_REPLAY_POINTER
, e
->time
);
7327 DNPRINTF(SWM_D_EVENT
, "buttonpress: handled.\n");
7328 /* Unfreeze grab events. */
7329 xcb_allow_events(conn
, XCB_ALLOW_SYNC_POINTER
, e
->time
);
7337 print_win_geom(xcb_window_t w
)
7339 xcb_get_geometry_reply_t
*wa
;
7341 wa
= xcb_get_geometry_reply(conn
, xcb_get_geometry(conn
, w
), NULL
);
7344 DNPRINTF(SWM_D_MISC
, "print_win_geom: window not found: 0x%x\n",
7349 DNPRINTF(SWM_D_MISC
, "print_win_geom: window: 0x%x, root: 0x%x, "
7350 "depth: %u, (x,y) w x h: (%d,%d) %d x %d, border: %d\n",
7351 w
, wa
->root
, wa
->depth
, wa
->x
, wa
->y
, wa
->width
, wa
->height
,
7360 get_stack_mode_name(uint8_t mode
)
7365 case XCB_STACK_MODE_ABOVE
:
7368 case XCB_STACK_MODE_BELOW
:
7371 case XCB_STACK_MODE_TOP_IF
:
7374 case XCB_STACK_MODE_BOTTOM_IF
:
7377 case XCB_STACK_MODE_OPPOSITE
:
7389 configurerequest(xcb_configure_request_event_t
*e
)
7392 struct swm_region
*r
= NULL
;
7395 uint32_t wc
[7] = {0};
7397 if ((win
= find_window(e
->window
)) == NULL
)
7398 if ((win
= find_unmanaged_window(e
->window
)) == NULL
)
7402 if (swm_debug
& SWM_D_EVENT
) {
7403 print_win_geom(e
->window
);
7405 DNPRINTF(SWM_D_EVENT
, "configurerequest: window: 0x%x, "
7406 "parent: 0x%x, new: %s, value_mask: %u { ", e
->window
,
7407 e
->parent
, YESNO(new), e
->value_mask
);
7408 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
)
7409 DPRINTF("X: %d ", e
->x
);
7410 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
)
7411 DPRINTF("Y: %d ", e
->y
);
7412 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
)
7413 DPRINTF("W: %u ", e
->width
);
7414 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
)
7415 DPRINTF("H: %u ", e
->height
);
7416 if (e
->value_mask
& XCB_CONFIG_WINDOW_BORDER_WIDTH
)
7417 DPRINTF("Border: %u ", e
->border_width
);
7418 if (e
->value_mask
& XCB_CONFIG_WINDOW_SIBLING
)
7419 DPRINTF("Sibling: 0x%x ", e
->sibling
);
7420 if (e
->value_mask
& XCB_CONFIG_WINDOW_STACK_MODE
)
7421 DPRINTF("StackMode: %s(%u) ",
7422 get_stack_mode_name(e
->stack_mode
), e
->stack_mode
);
7428 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
) {
7429 mask
|= XCB_CONFIG_WINDOW_X
;
7432 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
) {
7433 mask
|= XCB_CONFIG_WINDOW_Y
;
7436 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
) {
7437 mask
|= XCB_CONFIG_WINDOW_WIDTH
;
7440 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
) {
7441 mask
|= XCB_CONFIG_WINDOW_HEIGHT
;
7442 wc
[i
++] = e
->height
;
7444 if (e
->value_mask
& XCB_CONFIG_WINDOW_BORDER_WIDTH
) {
7445 mask
|= XCB_CONFIG_WINDOW_BORDER_WIDTH
;
7446 wc
[i
++] = e
->border_width
;
7448 if (e
->value_mask
& XCB_CONFIG_WINDOW_SIBLING
) {
7449 mask
|= XCB_CONFIG_WINDOW_SIBLING
;
7450 wc
[i
++] = e
->sibling
;
7452 if (e
->value_mask
& XCB_CONFIG_WINDOW_STACK_MODE
) {
7453 mask
|= XCB_CONFIG_WINDOW_STACK_MODE
;
7454 wc
[i
++] = e
->stack_mode
;
7458 xcb_configure_window(conn
, e
->window
, mask
, wc
);
7461 } else if ((!win
->manual
|| win
->quirks
& SWM_Q_ANYWHERE
) &&
7462 !(win
->ewmh_flags
& EWMH_F_FULLSCREEN
)) {
7465 else if (win
->ws
->old_r
)
7468 /* windows are centered unless ANYWHERE quirk is set. */
7469 if (win
->quirks
& SWM_Q_ANYWHERE
) {
7470 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
) {
7471 win
->g_float
.x
= e
->x
;
7473 win
->g_float
.x
-= X(r
);
7476 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
) {
7477 win
->g_float
.y
= e
->y
;
7479 win
->g_float
.y
-= Y(r
);
7483 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
)
7484 win
->g_float
.w
= e
->width
;
7486 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
)
7487 win
->g_float
.h
= e
->height
;
7489 win
->g_floatvalid
= 1;
7491 if (win
->floating
&& r
&& (win
->transient
||
7492 win
->ws
->cur_layout
!= &layouts
[SWM_MAX_STACK
])) {
7493 WIDTH(win
) = win
->g_float
.w
;
7494 HEIGHT(win
) = win
->g_float
.h
;
7496 stack_floater(win
, win
->ws
->r
);
7508 DNPRINTF(SWM_D_EVENT
, "configurerequest: done.\n");
7512 configurenotify(xcb_configure_notify_event_t
*e
)
7516 DNPRINTF(SWM_D_EVENT
, "configurenotify: win 0x%x, event win: 0x%x, "
7517 "(x,y) WxH: (%d,%d) %ux%u, border: %u, above_sibling: 0x%x, "
7518 "override_redirect: %s\n", e
->window
, e
->event
, e
->x
, e
->y
,
7519 e
->width
, e
->height
, e
->border_width
, e
->above_sibling
,
7520 YESNO(e
->override_redirect
));
7522 win
= find_window(e
->window
);
7524 xcb_icccm_get_wm_normal_hints_reply(conn
,
7525 xcb_icccm_get_wm_normal_hints(conn
, win
->id
),
7528 if (font_adjusted
) {
7536 destroynotify(xcb_destroy_notify_event_t
*e
)
7540 DNPRINTF(SWM_D_EVENT
, "destroynotify: window: 0x%x\n", e
->window
);
7542 if ((win
= find_window(e
->window
)) == NULL
) {
7543 if ((win
= find_unmanaged_window(e
->window
)) == NULL
)
7549 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
7550 /* If we were focused, make sure we focus on something else. */
7551 if (win
== win
->ws
->focus
)
7552 win
->ws
->focus_pending
= get_focus_prev(win
);
7555 unmanage_window(win
);
7558 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
7559 if (win
->ws
->focus_pending
) {
7560 focus_win(win
->ws
->focus_pending
);
7561 win
->ws
->focus_pending
= NULL
;
7572 get_notify_detail_label(uint8_t detail
)
7577 case XCB_NOTIFY_DETAIL_ANCESTOR
:
7580 case XCB_NOTIFY_DETAIL_VIRTUAL
:
7583 case XCB_NOTIFY_DETAIL_INFERIOR
:
7586 case XCB_NOTIFY_DETAIL_NONLINEAR
:
7587 label
= "Nonlinear";
7589 case XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL
:
7590 label
= "NonlinearVirtual";
7592 case XCB_NOTIFY_DETAIL_POINTER
:
7595 case XCB_NOTIFY_DETAIL_POINTER_ROOT
:
7596 label
= "PointerRoot";
7598 case XCB_NOTIFY_DETAIL_NONE
:
7609 get_notify_mode_label(uint8_t mode
)
7614 case XCB_NOTIFY_MODE_NORMAL
:
7617 case XCB_NOTIFY_MODE_GRAB
:
7620 case XCB_NOTIFY_MODE_UNGRAB
:
7623 case XCB_NOTIFY_MODE_WHILE_GRABBED
:
7624 label
= "WhileGrabbed";
7635 enternotify(xcb_enter_notify_event_t
*e
)
7638 struct swm_region
*old_r
, *r
;
7640 DNPRINTF(SWM_D_FOCUS
, "enternotify: time: %u, win (x,y): 0x%x "
7641 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): 0x%x (%d,%d), "
7642 "child: 0x%x, same_screen_focus: %s, state: %d\n",
7643 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
7644 get_notify_mode_label(e
->mode
), e
->mode
,
7645 get_notify_detail_label(e
->detail
), e
->detail
,
7646 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
7647 YESNO(e
->same_screen_focus
), e
->state
);
7649 if (focus_mode
== SWM_FOCUS_MANUAL
&&
7650 e
->mode
== XCB_NOTIFY_MODE_NORMAL
) {
7651 DNPRINTF(SWM_D_EVENT
, "enternotify: manual focus; ignoring.\n");
7655 last_event_time
= e
->time
;
7657 if ((win
= find_window(e
->event
)) == NULL
) {
7658 if (e
->event
== e
->root
) {
7659 /* If no windows on pointer region, then focus root. */
7660 r
= root_to_region(e
->root
, SWM_CK_POINTER
);
7661 if (TAILQ_EMPTY(&r
->ws
->winlist
)) {
7662 old_r
= root_to_region(e
->root
, SWM_CK_FOCUS
);
7663 if (old_r
&& old_r
!= r
)
7664 unfocus_win(old_r
->ws
->focus
);
7666 xcb_set_input_focus(conn
,
7667 XCB_INPUT_FOCUS_PARENT
, e
->root
, e
->time
);
7669 /* Clear bar since empty. */
7675 DNPRINTF(SWM_D_EVENT
, "enternotify: window is NULL; "
7681 focus_win(get_focus_magic(win
));
7688 leavenotify(xcb_leave_notify_event_t
*e
)
7690 DNPRINTF(SWM_D_FOCUS
, "leavenotify: time: %u, win (x,y): 0x%x "
7691 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): 0x%x (%d,%d), "
7692 "child: 0x%x, same_screen_focus: %s, state: %d\n",
7693 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
7694 get_notify_mode_label(e
->mode
), e
->mode
,
7695 get_notify_detail_label(e
->detail
), e
->detail
,
7696 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
7697 YESNO(e
->same_screen_focus
), e
->state
);
7701 /* lets us use one switch statement for arbitrary mode/detail combinations */
7702 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
7705 mapnotify(xcb_map_notify_event_t
*e
)
7709 DNPRINTF(SWM_D_EVENT
, "mapnotify: window: 0x%x\n", e
->window
);
7711 if ((win
= find_window(e
->window
)) == NULL
)
7712 win
= manage_window(e
->window
, 1);
7715 set_win_state(win
, XCB_ICCCM_WM_STATE_NORMAL
);
7717 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
7718 if (win
->ws
->focus_pending
== win
) {
7720 win
->ws
->focus_pending
= NULL
;
7729 mappingnotify(xcb_mapping_notify_event_t
*e
)
7731 xcb_refresh_keyboard_mapping(syms
, e
);
7733 if (e
->request
== XCB_MAPPING_KEYBOARD
)
7738 maprequest(xcb_map_request_event_t
*e
)
7741 xcb_get_window_attributes_reply_t
*war
;
7743 DNPRINTF(SWM_D_EVENT
, "maprequest: win 0x%x\n",
7746 war
= xcb_get_window_attributes_reply(conn
,
7747 xcb_get_window_attributes(conn
, e
->window
),
7750 DNPRINTF(SWM_D_EVENT
, "maprequest: window lost.\n");
7754 if (war
->override_redirect
) {
7755 DNPRINTF(SWM_D_EVENT
, "maprequest: override_redirect; "
7760 win
= manage_window(e
->window
,
7761 (war
->map_state
== XCB_MAP_STATE_VIEWABLE
));
7763 /* The new window should get focus; prepare. */
7764 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
7765 win
->ws
->focus_pending
= get_focus_magic(win
);
7767 /* All windows need to be mapped if they are in the current workspace.*/
7771 /* Ignore EnterNotify to handle the mapnotify without interference. */
7772 if (focus_mode
== SWM_FOCUS_DEFAULT
)
7773 event_drain(XCB_ENTER_NOTIFY
);
7776 DNPRINTF(SWM_D_EVENT
, "maprequest: done.\n");
7781 get_atom_name(xcb_atom_t atom
)
7786 * This should be disabled during most debugging since
7787 * xcb_get_* causes an xcb_flush.
7790 xcb_get_atom_name_reply_t
*r
;
7792 r
= xcb_get_atom_name_reply(conn
,
7793 xcb_get_atom_name(conn
, atom
),
7796 len
= xcb_get_atom_name_name_length(r
);
7798 name
= malloc(len
+ 1);
7800 memcpy(name
, xcb_get_atom_name_name(r
), len
);
7814 propertynotify(xcb_property_notify_event_t
*e
)
7820 name
= get_atom_name(e
->atom
);
7821 DNPRINTF(SWM_D_EVENT
, "propertynotify: window: 0x%x, atom: %s(%u), "
7822 "time: %#x, state: %u\n", e
->window
, name
, e
->atom
, e
->time
,
7826 win
= find_window(e
->window
);
7830 last_event_time
= e
->time
;
7832 if (e
->atom
== a_swm_iconic
) {
7833 if (e
->state
== XCB_PROPERTY_NEW_VALUE
) {
7834 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
7835 win
->ws
->focus_pending
= get_focus_prev(win
);
7842 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
7843 focus_win(win
->ws
->focus_pending
);
7844 win
->ws
->focus_pending
= NULL
;
7848 } else if (e
->state
== XCB_PROPERTY_DELETE
) {
7849 /* The window is no longer iconic, restack ws. */
7850 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
7851 win
->ws
->focus_pending
= get_focus_magic(win
);
7855 /* Flush EnterNotify for mapnotify, if needed. */
7858 } else if (e
->atom
== a_state
) {
7859 /* State just changed, make sure it gets focused if mapped. */
7860 if (e
->state
== XCB_PROPERTY_NEW_VALUE
) {
7861 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
7863 win
->ws
->focus_pending
== win
) {
7864 focus_win(win
->ws
->focus_pending
);
7865 win
->ws
->focus_pending
= NULL
;
7869 } else if (e
->atom
== XCB_ATOM_WM_CLASS
||
7870 e
->atom
== XCB_ATOM_WM_NAME
) {
7878 unmapnotify(xcb_unmap_notify_event_t
*e
)
7881 struct workspace
*ws
;
7883 DNPRINTF(SWM_D_EVENT
, "unmapnotify: window: 0x%x\n", e
->window
);
7885 /* If we aren't managing the window, then ignore. */
7886 win
= find_window(e
->window
);
7892 if (getstate(e
->window
) != XCB_ICCCM_WM_STATE_ICONIC
)
7893 set_win_state(win
, XCB_ICCCM_WM_STATE_ICONIC
);
7896 /* window unmapped itself */
7897 /* do unmap/unfocus/restack and unmanage */
7900 /* If win was focused, make sure to focus on something else. */
7901 if (win
== ws
->focus
) {
7902 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
7903 ws
->focus_pending
= get_focus_prev(win
);
7904 DNPRINTF(SWM_D_EVENT
, "unmapnotify: "
7905 "focus_pending: 0x%x\n",
7906 WINID(ws
->focus_pending
));
7912 unmanage_window(win
);
7917 if (focus_mode
== SWM_FOCUS_FOLLOW
) {
7919 focus_win(get_pointer_win(ws
->r
->s
->root
));
7921 if (ws
->focus_pending
) {
7922 focus_win(ws
->focus_pending
);
7923 ws
->focus_pending
= NULL
;
7928 if (getstate(e
->window
) == XCB_ICCCM_WM_STATE_NORMAL
)
7929 set_win_state(win
, XCB_ICCCM_WM_STATE_ICONIC
);
7936 visibilitynotify(xcb_visibility_notify_event_t
*e
)
7938 DNPRINTF(SWM_D_EVENT
, "visibilitynotify: window: 0x%x\n",
7944 clientmessage(xcb_client_message_event_t
*e
)
7947 xcb_map_request_event_t mre
;
7951 name
= get_atom_name(e
->type
);
7952 DNPRINTF(SWM_D_EVENT
, "clientmessage: window: 0x%x, atom: %s(%u)\n",
7953 e
->window
, name
, e
->type
);
7956 win
= find_window(e
->window
);
7959 if (e
->type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
7960 /* Manage the window with maprequest. */
7961 DNPRINTF(SWM_D_EVENT
, "clientmessage: request focus on "
7962 "unmanaged window.\n");
7963 mre
.window
= e
->window
;
7969 if (e
->type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
7970 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_ACTIVE_WINDOW\n");
7973 if (e
->type
== ewmh
[_NET_CLOSE_WINDOW
].atom
) {
7974 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_CLOSE_WINDOW\n");
7975 if (win
->can_delete
)
7976 client_msg(win
, a_delete
, 0);
7978 xcb_kill_client(conn
, win
->id
);
7980 if (e
->type
== ewmh
[_NET_MOVERESIZE_WINDOW
].atom
) {
7981 DNPRINTF(SWM_D_EVENT
,
7982 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
7983 if (win
->floating
) {
7984 if (e
->data
.data32
[0] & (1<<8)) /* x */
7985 X(win
) = e
->data
.data32
[1];
7986 if (e
->data
.data32
[0] & (1<<9)) /* y */
7987 Y(win
) = e
->data
.data32
[2];
7988 if (e
->data
.data32
[0] & (1<<10)) /* width */
7989 WIDTH(win
) = e
->data
.data32
[3];
7990 if (e
->data
.data32
[0] & (1<<11)) /* height */
7991 HEIGHT(win
) = e
->data
.data32
[4];
7996 /* TODO: Change stack sizes */
7997 /* notify no change was made. */
7998 config_win(win
, NULL
);
8001 if (e
->type
== ewmh
[_NET_WM_STATE
].atom
) {
8002 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_WM_STATE\n");
8003 ewmh_update_win_state(win
, e
->data
.data32
[1], e
->data
.data32
[0]);
8004 if (e
->data
.data32
[2])
8005 ewmh_update_win_state(win
, e
->data
.data32
[2],
8017 int errcode
= xcb_connection_has_error(conn
);
8018 #ifdef XCB_CONN_ERROR
8021 case XCB_CONN_ERROR
:
8022 s
= "Socket error, pipe error or other stream error.";
8024 case XCB_CONN_CLOSED_EXT_NOTSUPPORTED
:
8025 s
= "Extension not supported.";
8027 case XCB_CONN_CLOSED_MEM_INSUFFICIENT
:
8028 s
= "Insufficient memory.";
8030 case XCB_CONN_CLOSED_REQ_LEN_EXCEED
:
8031 s
= "Request length was exceeded.";
8033 case XCB_CONN_CLOSED_PARSE_ERR
:
8034 s
= "Error parsing display string.";
8037 s
= "Unknown error.";
8040 errx(errcode
, "X CONNECTION ERROR: %s", s
);
8043 errx(errcode
, "X CONNECTION ERROR");
8051 const uint32_t val
= XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT
|
8052 XCB_EVENT_MASK_ENTER_WINDOW
;
8054 xcb_void_cookie_t wac
;
8055 xcb_generic_error_t
*error
;
8057 /* this causes an error if some other window manager is running */
8058 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
8059 for (i
= 0; i
< num_screens
; i
++) {
8060 if ((sc
= get_screen(i
)) == NULL
)
8061 errx(1, "ERROR: can't get screen %d.", i
);
8062 DNPRINTF(SWM_D_INIT
, "enable_wm: screen %d, root: 0x%x\n",
8064 wac
= xcb_change_window_attributes_checked(conn
, sc
->root
,
8065 XCB_CW_EVENT_MASK
, &val
);
8066 if ((error
= xcb_request_check(conn
, wac
))) {
8067 DNPRINTF(SWM_D_INIT
, "enable_wm: error_code: %u\n",
8073 /* click to focus on empty region */
8074 xcb_grab_button(conn
, 1, sc
->root
, BUTTONMASK
,
8075 XCB_GRAB_MODE_SYNC
, XCB_GRAB_MODE_ASYNC
, XCB_WINDOW_NONE
,
8076 XCB_CURSOR_NONE
, XCB_BUTTON_INDEX_1
, XCB_BUTTON_MASK_ANY
);
8083 new_region(struct swm_screen
*s
, int x
, int y
, int w
, int h
)
8085 struct swm_region
*r
, *n
;
8086 struct workspace
*ws
= NULL
;
8089 DNPRINTF(SWM_D_MISC
, "new region: screen[%d]:%dx%d+%d+%d\n",
8090 s
->idx
, w
, h
, x
, y
);
8092 /* remove any conflicting regions */
8093 n
= TAILQ_FIRST(&s
->rl
);
8096 n
= TAILQ_NEXT(r
, entry
);
8097 if (X(r
) < (x
+ w
) &&
8098 (X(r
) + WIDTH(r
)) > x
&&
8100 (Y(r
) + HEIGHT(r
)) > y
) {
8101 if (r
->ws
->r
!= NULL
)
8102 r
->ws
->old_r
= r
->ws
->r
;
8105 TAILQ_REMOVE(&s
->rl
, r
, entry
);
8106 TAILQ_INSERT_TAIL(&s
->orl
, r
, entry
);
8110 /* search old regions for one to reuse */
8112 /* size + location match */
8113 TAILQ_FOREACH(r
, &s
->orl
, entry
)
8114 if (X(r
) == x
&& Y(r
) == y
&&
8115 HEIGHT(r
) == h
&& WIDTH(r
) == w
)
8119 TAILQ_FOREACH(r
, &s
->orl
, entry
)
8120 if (HEIGHT(r
) == h
&& WIDTH(r
) == w
)
8124 TAILQ_REMOVE(&s
->orl
, r
, entry
);
8125 /* try to use old region's workspace */
8126 if (r
->ws
->r
== NULL
)
8129 if ((r
= calloc(1, sizeof(struct swm_region
))) == NULL
)
8130 err(1, "new_region: calloc: failed to allocate memory "
8133 /* if we don't have a workspace already, find one */
8135 for (i
= 0; i
< workspace_limit
; i
++)
8136 if (s
->ws
[i
].r
== NULL
) {
8143 errx(1, "new_region: no free workspaces");
8154 TAILQ_INSERT_TAIL(&s
->rl
, r
, entry
);
8160 #ifdef SWM_XRR_HAS_CRTC
8163 #endif /* SWM_XRR_HAS_CRTC */
8164 struct swm_region
*r
;
8166 xcb_randr_get_screen_resources_current_cookie_t src
;
8167 xcb_randr_get_screen_resources_current_reply_t
*srr
;
8168 xcb_randr_get_crtc_info_cookie_t cic
;
8169 xcb_randr_get_crtc_info_reply_t
*cir
= NULL
;
8170 xcb_randr_crtc_t
*crtc
;
8171 xcb_screen_t
*screen
;
8173 if ((screen
= get_screen(i
)) == NULL
)
8174 errx(1, "ERROR: can't get screen %d.", i
);
8176 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
8177 if (i
>= num_screens
)
8178 errx(1, "scan_xrandr: invalid screen");
8180 /* remove any old regions */
8181 while ((r
= TAILQ_FIRST(&screens
[i
].rl
)) != NULL
) {
8182 r
->ws
->old_r
= r
->ws
->r
= NULL
;
8184 TAILQ_REMOVE(&screens
[i
].rl
, r
, entry
);
8185 TAILQ_INSERT_TAIL(&screens
[i
].orl
, r
, entry
);
8189 /* map virtual screens onto physical screens */
8190 #ifdef SWM_XRR_HAS_CRTC
8191 if (xrandr_support
) {
8192 src
= xcb_randr_get_screen_resources_current(conn
,
8194 srr
= xcb_randr_get_screen_resources_current_reply(conn
, src
,
8197 new_region(&screens
[i
], 0, 0,
8198 screen
->width_in_pixels
,
8199 screen
->height_in_pixels
);
8202 ncrtc
= srr
->num_crtcs
;
8204 crtc
= xcb_randr_get_screen_resources_current_crtcs(srr
);
8205 for (c
= 0; c
< ncrtc
; c
++) {
8206 cic
= xcb_randr_get_crtc_info(conn
, crtc
[c
],
8208 cir
= xcb_randr_get_crtc_info_reply(conn
, cic
, NULL
);
8211 if (cir
->num_outputs
== 0) {
8217 new_region(&screens
[i
], 0, 0,
8218 screen
->width_in_pixels
,
8219 screen
->height_in_pixels
);
8221 new_region(&screens
[i
],
8222 cir
->x
, cir
->y
, cir
->width
, cir
->height
);
8227 #endif /* SWM_XRR_HAS_CRTC */
8229 new_region(&screens
[i
], 0, 0, screen
->width_in_pixels
,
8230 screen
->height_in_pixels
);
8235 screenchange(xcb_randr_screen_change_notify_event_t
*e
)
8237 struct swm_region
*r
;
8240 DNPRINTF(SWM_D_EVENT
, "screenchange: root: 0x%x\n", e
->root
);
8242 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
8243 /* silly event doesn't include the screen index */
8244 for (i
= 0; i
< num_screens
; i
++)
8245 if (screens
[i
].root
== e
->root
)
8247 if (i
>= num_screens
)
8248 errx(1, "screenchange: screen not found");
8250 /* brute force for now, just re-enumerate the regions */
8254 print_win_geom(e
->root
);
8256 /* add bars to all regions */
8257 for (i
= 0; i
< num_screens
; i
++)
8258 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
8266 xcb_window_t
*wins
= NULL
, trans
;
8268 int i
, j
, num_screens
;
8269 uint16_t state
, manage
, mapped
;
8271 xcb_query_tree_cookie_t qtc
;
8272 xcb_query_tree_reply_t
*qtr
;
8273 xcb_get_window_attributes_cookie_t c
;
8274 xcb_get_window_attributes_reply_t
*r
;
8275 xcb_get_property_cookie_t pc
;
8277 DNPRINTF(SWM_D_INIT
, "grab_windows: begin\n");
8278 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
8279 for (i
= 0; i
< num_screens
; i
++) {
8280 qtc
= xcb_query_tree(conn
, screens
[i
].root
);
8281 qtr
= xcb_query_tree_reply(conn
, qtc
, NULL
);
8284 wins
= xcb_query_tree_children(qtr
);
8285 no
= xcb_query_tree_children_length(qtr
);
8286 /* attach windows to a region */
8287 /* normal windows */
8288 DNPRINTF(SWM_D_INIT
, "grab_windows: grab top level windows.\n");
8289 for (j
= 0; j
< no
; j
++) {
8290 c
= xcb_get_window_attributes(conn
, wins
[j
]);
8291 r
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
8293 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
8294 "doesn't exist.\n", wins
[j
]);
8297 if (r
->override_redirect
) {
8298 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
8299 "override_redirect set.\n", wins
[j
]);
8304 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
8305 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
8307 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
8308 "is transient for %#x.\n", wins
[j
], trans
);
8313 state
= getstate(wins
[j
]);
8314 manage
= state
!= XCB_ICCCM_WM_STATE_WITHDRAWN
;
8315 mapped
= r
->map_state
!= XCB_MAP_STATE_UNMAPPED
;
8316 if (mapped
|| manage
)
8317 manage_window(wins
[j
], mapped
);
8320 /* transient windows */
8321 DNPRINTF(SWM_D_INIT
, "grab_windows: grab transient windows.\n");
8322 for (j
= 0; j
< no
; j
++) {
8323 c
= xcb_get_window_attributes(conn
, wins
[j
]);
8324 r
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
8326 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
8327 "doesn't exist.\n", wins
[j
]);
8330 if (r
->override_redirect
) {
8331 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
8332 "override_redirect set.\n", wins
[j
]);
8338 state
= getstate(wins
[j
]);
8339 manage
= state
!= XCB_ICCCM_WM_STATE_WITHDRAWN
;
8340 mapped
= r
->map_state
!= XCB_MAP_STATE_UNMAPPED
;
8341 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
8342 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
8343 &trans
, NULL
) && manage
)
8344 manage_window(wins
[j
], mapped
);
8348 DNPRINTF(SWM_D_INIT
, "grab_windows: done.\n");
8354 int i
, j
, k
, num_screens
;
8355 struct workspace
*ws
;
8356 uint32_t gcv
[1], wa
[1];
8357 const xcb_query_extension_reply_t
*qep
;
8358 xcb_screen_t
*screen
;
8359 xcb_randr_query_version_cookie_t c
;
8360 xcb_randr_query_version_reply_t
*r
;
8362 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
8363 if ((screens
= calloc(num_screens
,
8364 sizeof(struct swm_screen
))) == NULL
)
8365 err(1, "setup_screens: calloc: failed to allocate memory for "
8368 /* initial Xrandr setup */
8370 qep
= xcb_get_extension_data(conn
, &xcb_randr_id
);
8372 c
= xcb_randr_query_version(conn
, 1, 1);
8373 r
= xcb_randr_query_version_reply(conn
, c
, NULL
);
8375 if (r
->major_version
>= 1) {
8377 xrandr_eventbase
= qep
->first_event
;
8383 wa
[0] = cursors
[XC_LEFT_PTR
].cid
;
8385 /* map physical screens */
8386 for (i
= 0; i
< num_screens
; i
++) {
8387 DNPRINTF(SWM_D_WS
, "setup_screens: init screen: %d\n", i
);
8389 TAILQ_INIT(&screens
[i
].rl
);
8390 TAILQ_INIT(&screens
[i
].orl
);
8391 if ((screen
= get_screen(i
)) == NULL
)
8392 errx(1, "ERROR: can't get screen %d.", i
);
8393 screens
[i
].root
= screen
->root
;
8395 /* set default colors */
8396 setscreencolor("red", i
+ 1, SWM_S_COLOR_FOCUS
);
8397 setscreencolor("rgb:88/88/88", i
+ 1, SWM_S_COLOR_UNFOCUS
);
8398 setscreencolor("rgb:00/80/80", i
+ 1, SWM_S_COLOR_BAR_BORDER
);
8399 setscreencolor("black", i
+ 1, SWM_S_COLOR_BAR
);
8400 setscreencolor("rgb:a0/a0/a0", i
+ 1, SWM_S_COLOR_BAR_FONT
);
8402 /* create graphics context on screen */
8403 screens
[i
].bar_gc
= xcb_generate_id(conn
);
8405 xcb_create_gc(conn
, screens
[i
].bar_gc
, screens
[i
].root
,
8406 XCB_GC_GRAPHICS_EXPOSURES
, gcv
);
8408 /* set default cursor */
8409 xcb_change_window_attributes(conn
, screens
[i
].root
,
8412 /* init all workspaces */
8413 /* XXX these should be dynamically allocated too */
8414 for (j
= 0; j
< SWM_WS_MAX
; j
++) {
8415 ws
= &screens
[i
].ws
[j
];
8419 ws
->focus_prev
= NULL
;
8420 ws
->focus_pending
= NULL
;
8423 TAILQ_INIT(&ws
->winlist
);
8424 TAILQ_INIT(&ws
->unmanagedlist
);
8426 for (k
= 0; layouts
[k
].l_stack
!= NULL
; k
++)
8427 if (layouts
[k
].l_config
!= NULL
)
8428 layouts
[k
].l_config(ws
,
8429 SWM_ARG_ID_STACKINIT
);
8430 ws
->cur_layout
= &layouts
[0];
8431 ws
->cur_layout
->l_string(ws
);
8437 xcb_randr_select_input(conn
, screens
[i
].root
,
8438 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE
);
8445 if ((bar_fonts
= strdup(SWM_BAR_FONTS
)) == NULL
)
8446 err(1, "setup_globals: strdup: failed to allocate memory.");
8448 if ((clock_format
= strdup("%a %b %d %R %Z %Y")) == NULL
)
8449 err(1, "setup_globals: strdup: failed to allocate memory.");
8451 if ((syms
= xcb_key_symbols_alloc(conn
)) == NULL
)
8452 errx(1, "unable to allocate key symbols");
8454 a_state
= get_atom_from_string("WM_STATE");
8455 a_prot
= get_atom_from_string("WM_PROTOCOLS");
8456 a_delete
= get_atom_from_string("WM_DELETE_WINDOW");
8457 a_takefocus
= get_atom_from_string("WM_TAKE_FOCUS");
8458 a_wmname
= get_atom_from_string("WM_NAME");
8459 a_netwmname
= get_atom_from_string("_NET_WM_NAME");
8460 a_utf8_string
= get_atom_from_string("UTF8_STRING");
8461 a_string
= get_atom_from_string("STRING");
8462 a_swm_iconic
= get_atom_from_string("_SWM_ICONIC");
8463 a_swm_ws
= get_atom_from_string("_SWM_WS");
8470 xcb_atom_t netwmcheck
;
8471 xcb_window_t root
, win
;
8473 /* work around sun jdk bugs, code from wmname */
8474 netwmcheck
= get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
8476 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
8477 for (i
= 0; i
< num_screens
; i
++) {
8478 root
= screens
[i
].root
;
8480 win
= xcb_generate_id(conn
);
8481 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, win
, root
,
8482 0, 0, 1, 1, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
8483 XCB_COPY_FROM_PARENT
, 0, NULL
);
8485 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, root
,
8486 netwmcheck
, XCB_ATOM_WINDOW
, 32, 1, &win
);
8487 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
8488 netwmcheck
, XCB_ATOM_WINDOW
, 32, 1, &win
);
8489 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
8490 a_netwmname
, a_utf8_string
, 8, strlen("LG3D"), "LG3D");
8495 shutdown_cleanup(void)
8499 /* disable alarm because the following code may not be interrupted */
8501 if (signal(SIGALRM
, SIG_IGN
) == SIG_ERR
)
8502 err(1, "can't disable alarm");
8512 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
8513 for (i
= 0; i
< num_screens
; ++i
) {
8514 if (screens
[i
].bar_gc
!= 0)
8515 xcb_free_gc(conn
, screens
[i
].bar_gc
);
8516 if (!bar_font_legacy
)
8517 XftColorFree(display
, DefaultVisual(display
, i
),
8518 DefaultColormap(display
, i
), &bar_font_color
);
8521 if (bar_font_legacy
)
8522 XFreeFontSet(display
, bar_fs
);
8524 XftFontClose(display
, bar_font
);
8527 xcb_key_symbols_free(syms
);
8529 xcb_disconnect(conn
);
8533 event_error(xcb_generic_error_t
*e
)
8537 DNPRINTF(SWM_D_EVENT
, "event_error: %s(%u) from %s(%u), sequence: %u, "
8538 "resource_id: %u, minor_code: %u\n",
8539 xcb_event_get_error_label(e
->error_code
), e
->error_code
,
8540 xcb_event_get_request_label(e
->major_code
), e
->major_code
,
8541 e
->sequence
, e
->resource_id
, e
->minor_code
);
8545 event_handle(xcb_generic_event_t
*evt
)
8547 uint8_t type
= XCB_EVENT_RESPONSE_TYPE(evt
);
8549 DNPRINTF(SWM_D_EVENT
, "XCB Event: %s(%d), seq %u\n",
8550 xcb_event_get_label(XCB_EVENT_RESPONSE_TYPE(evt
)),
8551 XCB_EVENT_RESPONSE_TYPE(evt
), evt
->sequence
);
8554 #define EVENT(type, callback) case type: callback((void *)evt); return
8555 EVENT(0, event_error
);
8556 EVENT(XCB_BUTTON_PRESS
, buttonpress
);
8557 /*EVENT(XCB_BUTTON_RELEASE, buttonpress);*/
8558 /*EVENT(XCB_CIRCULATE_NOTIFY, );*/
8559 /*EVENT(XCB_CIRCULATE_REQUEST, );*/
8560 EVENT(XCB_CLIENT_MESSAGE
, clientmessage
);
8561 /*EVENT(XCB_COLORMAP_NOTIFY, );*/
8562 EVENT(XCB_CONFIGURE_NOTIFY
, configurenotify
);
8563 EVENT(XCB_CONFIGURE_REQUEST
, configurerequest
);
8564 /*EVENT(XCB_CREATE_NOTIFY, );*/
8565 EVENT(XCB_DESTROY_NOTIFY
, destroynotify
);
8566 EVENT(XCB_ENTER_NOTIFY
, enternotify
);
8567 EVENT(XCB_EXPOSE
, expose
);
8569 EVENT(XCB_FOCUS_IN
, focusin
);
8570 EVENT(XCB_FOCUS_OUT
, focusout
);
8572 /*EVENT(XCB_GRAPHICS_EXPOSURE, );*/
8573 /*EVENT(XCB_GRAVITY_NOTIFY, );*/
8574 EVENT(XCB_KEY_PRESS
, keypress
);
8575 /*EVENT(XCB_KEY_RELEASE, keypress);*/
8576 /*EVENT(XCB_KEYMAP_NOTIFY, );*/
8578 EVENT(XCB_LEAVE_NOTIFY
, leavenotify
);
8580 EVENT(XCB_MAP_NOTIFY
, mapnotify
);
8581 EVENT(XCB_MAP_REQUEST
, maprequest
);
8582 EVENT(XCB_MAPPING_NOTIFY
, mappingnotify
);
8583 /*EVENT(XCB_MOTION_NOTIFY, );*/
8584 /*EVENT(XCB_NO_EXPOSURE, );*/
8585 EVENT(XCB_PROPERTY_NOTIFY
, propertynotify
);
8586 /*EVENT(XCB_REPARENT_NOTIFY, );*/
8587 /*EVENT(XCB_RESIZE_REQUEST, );*/
8588 /*EVENT(XCB_SELECTION_CLEAR, );*/
8589 /*EVENT(XCB_SELECTION_NOTIFY, );*/
8590 /*EVENT(XCB_SELECTION_REQUEST, );*/
8591 EVENT(XCB_UNMAP_NOTIFY
, unmapnotify
);
8592 /*EVENT(XCB_VISIBILITY_NOTIFY, visibilitynotify);*/
8595 if (type
- xrandr_eventbase
== XCB_RANDR_SCREEN_CHANGE_NOTIFY
)
8596 screenchange((void *)evt
);
8600 main(int argc
, char *argv
[])
8602 struct swm_region
*r
, *rr
;
8603 struct ws_win
*winfocus
= NULL
;
8604 char conf
[PATH_MAX
], *cfile
= NULL
;
8606 int xfd
, i
, num_screens
;
8607 struct sigaction sact
;
8608 xcb_generic_event_t
*evt
;
8612 /* suppress unused warning since var is needed */
8615 time_started
= time(NULL
);
8618 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION
, buildstr
);
8619 if (!setlocale(LC_CTYPE
, "") || !setlocale(LC_TIME
, ""))
8620 warnx("no locale support");
8622 /* handle some signals */
8623 bzero(&sact
, sizeof(sact
));
8624 sigemptyset(&sact
.sa_mask
);
8626 sact
.sa_handler
= sighdlr
;
8627 sigaction(SIGINT
, &sact
, NULL
);
8628 sigaction(SIGQUIT
, &sact
, NULL
);
8629 sigaction(SIGTERM
, &sact
, NULL
);
8630 sigaction(SIGHUP
, &sact
, NULL
);
8632 sact
.sa_handler
= sighdlr
;
8633 sact
.sa_flags
= SA_NOCLDSTOP
;
8634 sigaction(SIGCHLD
, &sact
, NULL
);
8636 if (!(display
= XOpenDisplay(0)))
8637 errx(1, "can not open display");
8639 conn
= XGetXCBConnection(display
);
8640 if (xcb_connection_has_error(conn
))
8641 errx(1, "can not get XCB connection");
8643 XSetEventQueueOwner(display
, XCBOwnsEventQueue
);
8645 xcb_prefetch_extension_data(conn
, &xcb_randr_id
);
8646 xfd
= xcb_get_file_descriptor(conn
);
8648 /* look for local and global conf file */
8649 pwd
= getpwuid(getuid());
8651 errx(1, "invalid user: %d", getuid());
8653 xcb_grab_server(conn
);
8656 /* flush all events */
8657 while ((evt
= xcb_poll_for_event(conn
))) {
8658 if (XCB_EVENT_RESPONSE_TYPE(evt
) == 0)
8663 if (enable_wm() != 0)
8664 errx(1, "another window manager is currently running");
8666 /* Load Xcursors and/or cursorfont glyph cursors. */
8678 for (i
= 0; ; i
++) {
8683 snprintf(conf
, sizeof conf
, "%s/.%s",
8684 pwd
->pw_dir
, SWM_CONF_FILE
);
8688 snprintf(conf
, sizeof conf
, "/etc/%s",
8693 snprintf(conf
, sizeof conf
, "%s/.%s",
8694 pwd
->pw_dir
, SWM_CONF_FILE_OLD
);
8698 snprintf(conf
, sizeof conf
, "/etc/%s",
8705 if (strlen(conf
) && stat(conf
, &sb
) != -1)
8706 if (S_ISREG(sb
.st_mode
)) {
8713 /* load conf (if any) */
8715 conf_load(cfile
, SWM_CONF_DEFAULT
);
8718 /* set some values to work around bad programs */
8720 /* grab existing windows (before we build the bars) */
8723 if (getenv("SWM_STARTED") == NULL
)
8724 setenv("SWM_STARTED", "YES", 1);
8726 /* setup all bars */
8727 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
8728 for (i
= 0; i
< num_screens
; i
++)
8729 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
8730 if (winfocus
== NULL
)
8731 winfocus
= TAILQ_FIRST(&r
->ws
->winlist
);
8738 xcb_ungrab_server(conn
);
8742 while ((evt
= xcb_poll_for_event(conn
))) {
8749 /* If just (re)started, set default focus if needed. */
8750 if (winfocus
&& focus_mode
!= SWM_FOCUS_FOLLOW
) {
8751 rr
= winfocus
->ws
->r
;
8753 /* not a visible window */
8758 focus_win(get_region_focus(rr
));
8768 if (select(xfd
+ 1, &rd
, NULL
, NULL
, &tv
) == -1)
8769 if (errno
!= EINTR
) {
8770 DNPRINTF(SWM_D_MISC
, "select failed");
8773 restart(NULL
, NULL
);