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/Xft/Xft.h>
94 #include <X11/Xlib-xcb.h>
95 #include <xcb/xcb_atom.h>
96 #include <xcb/xcb_aux.h>
97 #include <xcb/xcb_event.h>
98 #include <xcb/xcb_icccm.h>
99 #include <xcb/xcb_keysyms.h>
100 #include <xcb/xtest.h>
101 #include <xcb/randr.h>
109 #ifdef SPECTRWM_BUILDSTR
110 static const char *buildstr
= SPECTRWM_BUILDSTR
;
112 static const char *buildstr
= SPECTRWM_VERSION
;
115 #if !defined(__CYGWIN__) /* cygwin chokes on xrandr stuff */
117 # error XRandR versions less than 1.0 are not supported
120 # if RANDR_MAJOR >= 1
121 # if RANDR_MINOR >= 2
122 # define SWM_XRR_HAS_CRTC
125 #endif /* __CYGWIN__ */
127 #ifndef XCB_ICCCM_NUM_WM_HINTS_ELEMENTS
128 #define XCB_ICCCM_SIZE_HINT_P_MIN_SIZE XCB_SIZE_HINT_P_MIN_SIZE
129 #define XCB_ICCCM_SIZE_HINT_P_MAX_SIZE XCB_SIZE_HINT_P_MAX_SIZE
130 #define XCB_ICCCM_SIZE_HINT_P_RESIZE_INC XCB_SIZE_HINT_P_RESIZE_INC
131 #define XCB_ICCCM_WM_HINT_X_URGENCY XCB_WM_HINT_X_URGENCY
132 #define XCB_ICCCM_WM_STATE_ICONIC XCB_WM_STATE_ICONIC
133 #define XCB_ICCCM_WM_STATE_WITHDRAWN XCB_WM_STATE_WITHDRAWN
134 #define XCB_ICCCM_WM_STATE_NORMAL XCB_WM_STATE_NORMAL
135 #define xcb_icccm_get_text_property_reply_t xcb_get_text_property_reply_t
136 #define xcb_icccm_get_text_property_reply_wipe xcb_get_text_property_reply_wipe
137 #define xcb_icccm_get_wm_class xcb_get_wm_class
138 #define xcb_icccm_get_wm_class_reply xcb_get_wm_class_reply
139 #define xcb_icccm_get_wm_class_reply_t xcb_get_wm_class_reply_t
140 #define xcb_icccm_get_wm_class_reply_wipe xcb_get_wm_class_reply_wipe
141 #define xcb_icccm_get_wm_hints xcb_get_wm_hints
142 #define xcb_icccm_get_wm_hints_reply xcb_get_wm_hints_reply
143 #define xcb_icccm_get_wm_name xcb_get_wm_name
144 #define xcb_icccm_get_wm_name_reply xcb_get_wm_name_reply
145 #define xcb_icccm_get_wm_normal_hints xcb_get_wm_normal_hints
146 #define xcb_icccm_get_wm_normal_hints_reply xcb_get_wm_normal_hints_reply
147 #define xcb_icccm_get_wm_protocols xcb_get_wm_protocols
148 #define xcb_icccm_get_wm_protocols_reply xcb_get_wm_protocols_reply
149 #define xcb_icccm_get_wm_protocols_reply_t xcb_get_wm_protocols_reply_t
150 #define xcb_icccm_get_wm_protocols_reply_wipe xcb_get_wm_protocols_reply_wipe
151 #define xcb_icccm_get_wm_transient_for xcb_get_wm_transient_for
152 #define xcb_icccm_get_wm_transient_for_reply xcb_get_wm_transient_for_reply
153 #define xcb_icccm_wm_hints_t xcb_wm_hints_t
156 /*#define SWM_DEBUG*/
158 #define DPRINTF(x...) do { \
160 fprintf(stderr, x); \
162 #define DNPRINTF(n,x...) do { \
163 if (swm_debug & n) { \
164 fprintf(stderr, "%ld ", (long)(time(NULL) - time_started)); \
165 fprintf(stderr, x); \
168 #define SWM_D_MISC 0x0001
169 #define SWM_D_EVENT 0x0002
170 #define SWM_D_WS 0x0004
171 #define SWM_D_FOCUS 0x0008
172 #define SWM_D_MOVE 0x0010
173 #define SWM_D_STACK 0x0020
174 #define SWM_D_MOUSE 0x0040
175 #define SWM_D_PROP 0x0080
176 #define SWM_D_CLASS 0x0100
177 #define SWM_D_KEY 0x0200
178 #define SWM_D_QUIRK 0x0400
179 #define SWM_D_SPAWN 0x0800
180 #define SWM_D_EVENTQ 0x1000
181 #define SWM_D_CONF 0x2000
182 #define SWM_D_BAR 0x4000
183 #define SWM_D_INIT 0x8000
185 u_int32_t swm_debug
= 0
204 #define DPRINTF(x...)
205 #define DNPRINTF(n,x...)
208 /* convert 8-bit to 16-bit */
209 #define RGB_8_TO_16(col) ((col) << 8) + (col)
211 #define PIXEL_TO_XRENDERCOLOR(px, xrc) \
212 xrc.red = RGB_8_TO_16((px) >> 16 & 0xff); \
213 xrc.green = RGB_8_TO_16((px) >> 8 & 0xff); \
214 xrc.blue = RGB_8_TO_16((px) & 0xff); \
217 #define LENGTH(x) (int)(sizeof (x) / sizeof (x)[0])
218 #define MODKEY XCB_MOD_MASK_1
219 #define CLEANMASK(mask) ((mask) & ~(numlockmask | XCB_MOD_MASK_LOCK))
220 #define BUTTONMASK (XCB_EVENT_MASK_BUTTON_PRESS | \
221 XCB_EVENT_MASK_BUTTON_RELEASE)
222 #define MOUSEMASK (BUTTONMASK|XCB_EVENT_MASK_POINTER_MOTION)
223 #define SWM_PROPLEN (16)
224 #define SWM_FUNCNAME_LEN (32)
225 #define SWM_KEYS_LEN (255)
226 #define SWM_QUIRK_LEN (64)
227 #define X(r) (r)->g.x
228 #define Y(r) (r)->g.y
229 #define WIDTH(r) (r)->g.w
230 #define HEIGHT(r) (r)->g.h
231 #define BORDER(w) ((w)->bordered ? border_width : 0)
232 #define MAX_X(r) ((r)->g.x + (r)->g.w)
233 #define MAX_Y(r) ((r)->g.y + (r)->g.h)
234 #define SH_MIN(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE
235 #define SH_MIN_W(w) (w)->sh.min_width
236 #define SH_MIN_H(w) (w)->sh.min_height
237 #define SH_MAX(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MAX_SIZE
238 #define SH_MAX_W(w) (w)->sh.max_width
239 #define SH_MAX_H(w) (w)->sh.max_height
240 #define SH_INC(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_RESIZE_INC
241 #define SH_INC_W(w) (w)->sh.width_inc
242 #define SH_INC_H(w) (w)->sh.height_inc
243 #define SWM_MAX_FONT_STEPS (3)
244 #define WINID(w) ((w) ? (w)->id : 0)
245 #define YESNO(x) ((x) ? "yes" : "no")
247 #define SWM_FOCUS_DEFAULT (0)
248 #define SWM_FOCUS_FOLLOW (1)
249 #define SWM_FOCUS_MANUAL (2)
251 #define SWM_CONF_DEFAULT (0)
252 #define SWM_CONF_KEYMAPPING (1)
255 #define SWM_LIB "/usr/local/lib/libswmhack.so"
262 xcb_atom_t a_takefocus
;
264 xcb_atom_t a_netwmname
;
265 xcb_atom_t a_utf8_string
;
267 xcb_atom_t a_swm_iconic
;
269 volatile sig_atomic_t running
= 1;
270 volatile sig_atomic_t restart_wm
= 0;
272 /*int last_focus_event = FocusOut;*/
276 int xrandr_eventbase
;
277 unsigned int numlockmask
= 0;
280 xcb_connection_t
*conn
;
281 xcb_key_symbols_t
*syms
;
284 int cycle_visible
= 0;
286 int font_adjusted
= 0;
287 unsigned int mod_key
= MODKEY
;
290 struct swm_region
*search_r
;
291 int select_list_pipe
[2];
292 int select_resp_pipe
[2];
294 volatile sig_atomic_t search_resp
;
295 int search_resp_action
;
297 struct search_window
{
298 TAILQ_ENTRY(search_window
) entry
;
302 xcb_window_t indicator
;
304 TAILQ_HEAD(search_winlist
, search_window
);
305 struct search_winlist search_wl
;
310 SWM_SEARCH_UNICONIFY
,
311 SWM_SEARCH_NAME_WORKSPACE
,
312 SWM_SEARCH_SEARCH_WORKSPACE
,
313 SWM_SEARCH_SEARCH_WINDOW
316 #define SWM_STACK_TOP (0)
317 #define SWM_STACK_BOTTOM (1)
318 #define SWM_STACK_ABOVE (2)
319 #define SWM_STACK_BELOW (3)
322 double dialog_ratio
= 0.6;
324 #define SWM_BAR_MAX (256)
325 #define SWM_BAR_JUSTIFY_LEFT (0)
326 #define SWM_BAR_JUSTIFY_CENTER (1)
327 #define SWM_BAR_JUSTIFY_RIGHT (2)
328 #define SWM_BAR_OFFSET (4)
329 #define SWM_BAR_FONTS "-*-terminus-medium-*-*-*-12-*-*-*-*-*-*-*," \
330 "-*-profont-*-*-*-*-12-*-*-*-*-*-*-*," \
331 "-*-times-medium-r-*-*-12-*-*-*-*-*-*-*," \
332 "-misc-fixed-medium-r-*-*-12-*-*-*-*-*-*-*," \
333 "-*-*-*-r-*-*-*-*-*-*-*-*-*-*"
335 #ifdef X_HAVE_UTF8_STRING
336 #define DRAWSTRING(x...) Xutf8DrawString(x)
338 #define DRAWSTRING(x...) XmbDrawString(x)
341 char *bar_argv
[] = { NULL
, NULL
};
343 unsigned char bar_ext
[SWM_BAR_MAX
];
344 char bar_vertext
[SWM_BAR_MAX
];
346 sig_atomic_t bar_alarm
= 0;
349 int bar_border_width
= 1;
350 int bar_at_bottom
= 0;
352 int bar_extra_running
= 0;
355 int bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
356 char *bar_format
= NULL
;
357 int stack_enabled
= 1;
358 int clock_enabled
= 1;
359 int urgent_enabled
= 0;
360 char *clock_format
= NULL
;
361 int title_name_enabled
= 0;
362 int title_class_enabled
= 0;
363 int window_name_enabled
= 0;
364 int focus_mode
= SWM_FOCUS_DEFAULT
;
365 int focus_close
= SWM_STACK_BELOW
;
366 int focus_close_wrap
= 1;
367 int focus_default
= SWM_STACK_TOP
;
368 int spawn_position
= SWM_STACK_TOP
;
369 int disable_border
= 0;
370 int border_width
= 1;
371 int verbose_layout
= 0;
375 XFontSetExtents
*bar_fs_extents
;
377 int bar_font_legacy
= 1;
379 XftColor bar_font_color
;
382 /* layout manager data */
383 struct swm_geometry
{
396 struct swm_geometry g
;
399 /* virtual "screens" */
401 TAILQ_ENTRY(swm_region
) entry
;
402 struct swm_geometry g
;
403 struct workspace
*ws
; /* current workspace on this region */
404 struct workspace
*ws_prior
; /* prior workspace on this region */
405 struct swm_screen
*s
; /* screen idx */
408 TAILQ_HEAD(swm_region_list
, swm_region
);
411 TAILQ_ENTRY(ws_win
) entry
;
413 xcb_window_t transient
;
414 struct ws_win
*focus_child
; /* focus on child transient */
415 struct swm_geometry g
; /* current geometry */
416 struct swm_geometry g_float
; /* region coordinates */
417 int g_floatvalid
; /* g_float geometry validity */
418 int floatmaxed
; /* whether maxed by max_stack */
423 unsigned int ewmh_flags
;
424 int font_size_boundary
[SWM_MAX_FONT_STEPS
];
430 unsigned long quirks
;
431 struct workspace
*ws
; /* always valid */
432 struct swm_screen
*s
; /* always valid, never changes */
433 xcb_get_geometry_reply_t
*wa
;
435 xcb_icccm_get_wm_class_reply_t ch
;
436 xcb_icccm_wm_hints_t hints
;
438 TAILQ_HEAD(ws_win_list
, ws_win
);
442 TAILQ_ENTRY(pid_e
) entry
;
446 TAILQ_HEAD(pid_list
, pid_e
);
447 struct pid_list pidlist
= TAILQ_HEAD_INITIALIZER(pidlist
);
449 /* layout handlers */
451 void vertical_config(struct workspace
*, int);
452 void vertical_stack(struct workspace
*, struct swm_geometry
*);
453 void horizontal_config(struct workspace
*, int);
454 void horizontal_stack(struct workspace
*, struct swm_geometry
*);
455 void max_stack(struct workspace
*, struct swm_geometry
*);
456 void plain_stacker(struct workspace
*);
457 void fancy_stacker(struct workspace
*);
460 void (*l_stack
)(struct workspace
*, struct swm_geometry
*);
461 void (*l_config
)(struct workspace
*, int);
463 #define SWM_L_FOCUSPREV (1<<0)
464 #define SWM_L_MAPONFOCUS (1<<1)
465 void (*l_string
)(struct workspace
*);
467 /* stack, configure */
468 { vertical_stack
, vertical_config
, 0, plain_stacker
},
469 { horizontal_stack
, horizontal_config
, 0, plain_stacker
},
471 SWM_L_MAPONFOCUS
| SWM_L_FOCUSPREV
, plain_stacker
},
472 { NULL
, NULL
, 0, NULL
},
475 /* position of max_stack mode in the layouts array, index into layouts! */
476 #define SWM_V_STACK (0)
477 #define SWM_H_STACK (1)
478 #define SWM_MAX_STACK (2)
480 #define SWM_H_SLICE (32)
481 #define SWM_V_SLICE (32)
483 /* define work spaces */
485 int idx
; /* workspace index */
486 char *name
; /* workspace name */
487 int always_raise
; /* raise windows on focus */
488 struct layout
*cur_layout
; /* current layout handlers */
489 struct ws_win
*focus
; /* may be NULL */
490 struct ws_win
*focus_prev
; /* may be NULL */
491 struct swm_region
*r
; /* may be NULL */
492 struct swm_region
*old_r
; /* may be NULL */
493 struct ws_win_list winlist
; /* list of windows in ws */
494 struct ws_win_list unmanagedlist
; /* list of dead windows in ws */
495 char stacker
[10]; /* display stacker and layout */
499 int horizontal_msize
;
501 int horizontal_stacks
;
512 SWM_S_COLOR_BAR_BORDER
,
513 SWM_S_COLOR_BAR_FONT
,
519 /* physical screen mapping */
520 #define SWM_WS_MAX (22) /* hard limit */
521 int workspace_limit
= 10; /* soft limit */
524 int idx
; /* screen index */
525 struct swm_region_list rl
; /* list of regions on this screen */
526 struct swm_region_list orl
; /* list of old regions */
528 struct workspace ws
[SWM_WS_MAX
];
534 } c
[SWM_S_COLOR_MAX
];
536 xcb_gcontext_t bar_gc
;
539 struct swm_screen
*screens
;
541 /* args to functions */
544 #define SWM_ARG_ID_FOCUSNEXT (0)
545 #define SWM_ARG_ID_FOCUSPREV (1)
546 #define SWM_ARG_ID_FOCUSMAIN (2)
547 #define SWM_ARG_ID_FOCUSCUR (4)
548 #define SWM_ARG_ID_SWAPNEXT (10)
549 #define SWM_ARG_ID_SWAPPREV (11)
550 #define SWM_ARG_ID_SWAPMAIN (12)
551 #define SWM_ARG_ID_MOVELAST (13)
552 #define SWM_ARG_ID_MASTERSHRINK (20)
553 #define SWM_ARG_ID_MASTERGROW (21)
554 #define SWM_ARG_ID_MASTERADD (22)
555 #define SWM_ARG_ID_MASTERDEL (23)
556 #define SWM_ARG_ID_FLIPLAYOUT (24)
557 #define SWM_ARG_ID_STACKRESET (30)
558 #define SWM_ARG_ID_STACKINIT (31)
559 #define SWM_ARG_ID_CYCLEWS_UP (40)
560 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
561 #define SWM_ARG_ID_CYCLESC_UP (42)
562 #define SWM_ARG_ID_CYCLESC_DOWN (43)
563 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
564 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
565 #define SWM_ARG_ID_STACKINC (50)
566 #define SWM_ARG_ID_STACKDEC (51)
567 #define SWM_ARG_ID_SS_ALL (60)
568 #define SWM_ARG_ID_SS_WINDOW (61)
569 #define SWM_ARG_ID_DONTCENTER (70)
570 #define SWM_ARG_ID_CENTER (71)
571 #define SWM_ARG_ID_KILLWINDOW (80)
572 #define SWM_ARG_ID_DELETEWINDOW (81)
573 #define SWM_ARG_ID_WIDTHGROW (90)
574 #define SWM_ARG_ID_WIDTHSHRINK (91)
575 #define SWM_ARG_ID_HEIGHTGROW (92)
576 #define SWM_ARG_ID_HEIGHTSHRINK (93)
577 #define SWM_ARG_ID_MOVEUP (100)
578 #define SWM_ARG_ID_MOVEDOWN (101)
579 #define SWM_ARG_ID_MOVELEFT (102)
580 #define SWM_ARG_ID_MOVERIGHT (103)
586 TAILQ_ENTRY(quirk
) entry
;
590 #define SWM_Q_FLOAT (1<<0) /* float this window */
591 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
592 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
593 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
594 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
595 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
597 TAILQ_HEAD(quirk_list
, quirk
);
598 struct quirk_list quirks
= TAILQ_HEAD_INITIALIZER(quirks
);
601 * Supported EWMH hints should be added to
602 * both the enum and the ewmh array
607 _NET_MOVERESIZE_WINDOW
,
608 _NET_WM_ACTION_ABOVE
,
609 _NET_WM_ACTION_CLOSE
,
610 _NET_WM_ACTION_FULLSCREEN
,
612 _NET_WM_ACTION_RESIZE
,
613 _NET_WM_ALLOWED_ACTIONS
,
617 _NET_WM_STATE_FULLSCREEN
,
618 _NET_WM_STATE_HIDDEN
,
619 _NET_WM_STATE_MAXIMIZED_HORZ
,
620 _NET_WM_STATE_MAXIMIZED_VERT
,
621 _NET_WM_STATE_SKIP_PAGER
,
622 _NET_WM_STATE_SKIP_TASKBAR
,
624 _NET_WM_WINDOW_TYPE_DIALOG
,
625 _NET_WM_WINDOW_TYPE_DOCK
,
626 _NET_WM_WINDOW_TYPE_NORMAL
,
627 _NET_WM_WINDOW_TYPE_SPLASH
,
628 _NET_WM_WINDOW_TYPE_TOOLBAR
,
629 _NET_WM_WINDOW_TYPE_UTILITY
,
630 _SWM_WM_STATE_MANUAL
,
637 } ewmh
[SWM_EWMH_HINT_MAX
] = {
638 /* must be in same order as in the enum */
639 {"_NET_ACTIVE_WINDOW", XCB_ATOM_NONE
},
640 {"_NET_CLOSE_WINDOW", XCB_ATOM_NONE
},
641 {"_NET_MOVERESIZE_WINDOW", XCB_ATOM_NONE
},
642 {"_NET_WM_ACTION_ABOVE", XCB_ATOM_NONE
},
643 {"_NET_WM_ACTION_CLOSE", XCB_ATOM_NONE
},
644 {"_NET_WM_ACTION_FULLSCREEN", XCB_ATOM_NONE
},
645 {"_NET_WM_ACTION_MOVE", XCB_ATOM_NONE
},
646 {"_NET_WM_ACTION_RESIZE", XCB_ATOM_NONE
},
647 {"_NET_WM_ALLOWED_ACTIONS", XCB_ATOM_NONE
},
648 {"_NET_WM_NAME", XCB_ATOM_NONE
},
649 {"_NET_WM_STATE", XCB_ATOM_NONE
},
650 {"_NET_WM_STATE_ABOVE", XCB_ATOM_NONE
},
651 {"_NET_WM_STATE_FULLSCREEN", XCB_ATOM_NONE
},
652 {"_NET_WM_STATE_HIDDEN", XCB_ATOM_NONE
},
653 {"_NET_WM_STATE_MAXIMIZED_HORZ", XCB_ATOM_NONE
},
654 {"_NET_WM_STATE_MAXIMIZED_VERT", XCB_ATOM_NONE
},
655 {"_NET_WM_STATE_SKIP_PAGER", XCB_ATOM_NONE
},
656 {"_NET_WM_STATE_SKIP_TASKBAR", XCB_ATOM_NONE
},
657 {"_NET_WM_WINDOW_TYPE", XCB_ATOM_NONE
},
658 {"_NET_WM_WINDOW_TYPE_DIALOG", XCB_ATOM_NONE
},
659 {"_NET_WM_WINDOW_TYPE_DOCK", XCB_ATOM_NONE
},
660 {"_NET_WM_WINDOW_TYPE_NORMAL", XCB_ATOM_NONE
},
661 {"_NET_WM_WINDOW_TYPE_SPLASH", XCB_ATOM_NONE
},
662 {"_NET_WM_WINDOW_TYPE_TOOLBAR", XCB_ATOM_NONE
},
663 {"_NET_WM_WINDOW_TYPE_UTILITY", XCB_ATOM_NONE
},
664 {"_SWM_WM_STATE_MANUAL", XCB_ATOM_NONE
},
667 /* function prototypes */
668 void buttonpress(xcb_button_press_event_t
*);
669 void check_conn(void);
670 void clientmessage(xcb_client_message_event_t
*);
671 int conf_load(char *, int);
672 void configurenotify(xcb_configure_notify_event_t
*);
673 void configurerequest(xcb_configure_request_event_t
*);
674 void constrain_window(struct ws_win
*, struct swm_region
*, int);
675 void destroynotify(xcb_destroy_notify_event_t
*);
676 void enternotify(xcb_enter_notify_event_t
*);
677 void event_drain(uint8_t);
678 void event_error(xcb_generic_error_t
*);
679 void event_handle(xcb_generic_event_t
*);
680 char *expand_tilde(char *);
681 void expose(xcb_expose_event_t
*);
682 struct ws_win
*find_window(xcb_window_t
);
683 int floating_toggle_win(struct ws_win
*);
684 void focus(struct swm_region
*, union arg
*);
685 void focus_flush(void);
686 struct ws_win
*get_focus_magic(struct ws_win
*);
687 struct ws_win
*get_focus_prev(struct ws_win
*);
688 void focus_win(struct ws_win
*);
690 void focusin(xcb_focus_in_event_t
*);
692 xcb_atom_t
get_atom_from_string(const char *);
694 char *get_atom_name(xcb_atom_t
);
695 char *get_notify_detail_label(uint8_t);
696 char *get_notify_mode_label(uint8_t);
698 xcb_screen_t
*get_screen(int);
699 char *get_win_name(xcb_window_t
);
700 uint32_t getstate(xcb_window_t
);
701 void grabbuttons(struct ws_win
*);
702 void keypress(xcb_key_press_event_t
*);
704 void leavenotify(xcb_leave_notify_event_t
*);
706 void load_float_geom(struct ws_win
*, struct swm_region
*);
707 void map_window_raised(xcb_window_t
);
708 void mapnotify(xcb_map_notify_event_t
*);
709 void mappingnotify(xcb_mapping_notify_event_t
*);
710 void maprequest(xcb_map_request_event_t
*);
711 void new_region(struct swm_screen
*, int, int, int, int);
712 int parse_rgb(const char *, uint16_t *, uint16_t *, uint16_t *);
713 void propertynotify(xcb_property_notify_event_t
*);
714 void spawn_select(struct swm_region
*, union arg
*, char *, int *);
715 void screenchange(xcb_randr_screen_change_notify_event_t
*);
716 void shutdown_cleanup(void);
717 void store_float_geom(struct ws_win
*, struct swm_region
*);
718 void unmanage_window(struct ws_win
*);
719 void unmapnotify(xcb_unmap_notify_event_t
*);
720 void update_window(struct ws_win
*);
721 /*void visibilitynotify(xcb_visibility_notify_event_t *);*/
724 expand_tilde(char *s
)
733 errx(1, "expand_tilde: NULL string.");
742 if ((max
= sysconf(_SC_LOGIN_NAME_MAX
)) == -1)
743 errx(1, "expand_tilde: sysconf");
745 if ((user
= calloc(1, max
+ 1)) == NULL
)
746 errx(1, "expand_tilde: calloc");
748 for (i
= 0; s
[i
] != '/' && s
[i
] != '\0'; ++i
)
753 ppwd
= strlen(user
) == 0 ? getpwuid(getuid()) : getpwnam(user
);
757 if (asprintf(&result
, "%s%s", ppwd
->pw_dir
, s
) == -1)
761 errx(1, "expand_tilde: failed to allocate memory.");
767 parse_rgb(const char *rgb
, uint16_t *rr
, uint16_t *gg
, uint16_t *bb
)
769 unsigned int tmpr
, tmpg
, tmpb
;
771 if (sscanf(rgb
, "rgb:%x/%x/%x", &tmpr
, &tmpg
, &tmpb
) != 3)
774 *rr
= RGB_8_TO_16(tmpr
);
775 *gg
= RGB_8_TO_16(tmpg
);
776 *bb
= RGB_8_TO_16(tmpb
);
782 get_screen(int screen
)
784 const xcb_setup_t
*r
;
785 xcb_screen_iterator_t iter
;
787 if ((r
= xcb_get_setup(conn
)) == NULL
) {
788 DNPRINTF(SWM_D_MISC
, "get_screen: xcb_get_setup\n");
792 iter
= xcb_setup_roots_iterator(r
);
793 for (; iter
.rem
; --screen
, xcb_screen_next(&iter
))
803 if (focus_mode
== SWM_FOCUS_DEFAULT
)
804 event_drain(XCB_ENTER_NOTIFY
);
810 map_window_raised(xcb_window_t win
)
812 uint32_t val
= XCB_STACK_MODE_ABOVE
;
814 xcb_configure_window(conn
, win
,
815 XCB_CONFIG_WINDOW_STACK_MODE
, &val
);
817 xcb_map_window(conn
, win
);
821 get_atom_from_string(const char *str
)
823 xcb_intern_atom_cookie_t c
;
824 xcb_intern_atom_reply_t
*r
;
827 c
= xcb_intern_atom(conn
, 0, strlen(str
), str
);
828 r
= xcb_intern_atom_reply(conn
, c
, NULL
);
836 return (XCB_ATOM_NONE
);
840 update_iconic(struct ws_win
*win
, int newv
)
847 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
848 a_swm_iconic
, XCB_ATOM_INTEGER
, 32, 1, &v
);
850 xcb_delete_property(conn
, win
->id
, a_swm_iconic
);
854 get_iconic(struct ws_win
*win
)
857 xcb_get_property_reply_t
*pr
= NULL
;
860 pr
= xcb_get_property_reply(conn
,
861 xcb_get_property(conn
, 0, win
->id
, a_swm_iconic
,
862 XCB_ATOM_INTEGER
, 0, 1), NULL
);
865 if (pr
->type
!= XCB_ATOM_INTEGER
|| pr
->format
!= 32)
867 v
= *((int32_t *)xcb_get_property_value(pr
));
878 int i
, j
, num_screens
;
880 sup_list
= get_atom_from_string("_NET_SUPPORTED");
882 for (i
= 0; i
< LENGTH(ewmh
); i
++)
883 ewmh
[i
].atom
= get_atom_from_string(ewmh
[i
].name
);
885 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
886 for (i
= 0; i
< num_screens
; i
++) {
887 /* Support check window will be created by workaround(). */
889 /* Report supported atoms */
890 xcb_delete_property(conn
, screens
[i
].root
, sup_list
);
891 for (j
= 0; j
< LENGTH(ewmh
); j
++)
892 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
,
893 screens
[i
].root
, sup_list
, XCB_ATOM_ATOM
, 32, 1,
902 xcb_atom_t sup_check
, sup_list
;
904 xcb_get_property_cookie_t pc
;
905 xcb_get_property_reply_t
*pr
;
907 sup_check
= get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
908 sup_list
= get_atom_from_string("_NET_SUPPORTED");
909 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
911 for (i
= 0; i
< num_screens
; i
++) {
912 /* Get the support check window and destroy it */
913 pc
= xcb_get_property(conn
, 0, screens
[i
].root
, sup_check
,
914 XCB_ATOM_WINDOW
, 0, 1);
915 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
918 if (pr
->format
== sup_check
) {
919 id
= *((xcb_window_t
*)xcb_get_property_value(pr
));
921 xcb_destroy_window(conn
, id
);
922 xcb_delete_property(conn
, screens
[i
].root
, sup_check
);
923 xcb_delete_property(conn
, screens
[i
].root
, sup_list
);
930 ewmh_autoquirk(struct ws_win
*win
)
934 xcb_get_property_cookie_t c
;
935 xcb_get_property_reply_t
*r
;
937 c
= xcb_get_property(conn
, 0, win
->id
,
938 ewmh
[_NET_WM_WINDOW_TYPE
].atom
, XCB_ATOM_ATOM
, 0, UINT32_MAX
);
939 r
= xcb_get_property_reply(conn
, c
, NULL
);
943 n
= xcb_get_property_value_length(r
);
944 type
= xcb_get_property_value(r
);
946 for (i
= 0; i
< n
; i
++) {
947 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_NORMAL
].atom
)
949 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_DOCK
].atom
||
950 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_TOOLBAR
].atom
||
951 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_UTILITY
].atom
) {
953 win
->quirks
= SWM_Q_FLOAT
| SWM_Q_ANYWHERE
;
956 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_SPLASH
].atom
||
957 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_DIALOG
].atom
) {
959 win
->quirks
= SWM_Q_FLOAT
;
966 #define SWM_EWMH_ACTION_COUNT_MAX (6)
967 #define EWMH_F_FULLSCREEN (1<<0)
968 #define EWMH_F_ABOVE (1<<1)
969 #define EWMH_F_HIDDEN (1<<2)
970 #define EWMH_F_SKIP_PAGER (1<<3)
971 #define EWMH_F_SKIP_TASKBAR (1<<4)
972 #define SWM_F_MANUAL (1<<5)
975 ewmh_set_win_fullscreen(struct ws_win
*win
, int fs
)
983 DNPRINTF(SWM_D_MISC
, "ewmh_set_win_fullscreen: window: 0x%x, "
984 "fullscreen %s\n", win
->id
, YESNO(fs
));
987 if (!win
->g_floatvalid
)
988 store_float_geom(win
, win
->ws
->r
);
990 win
->g
= win
->ws
->r
->g
;
993 load_float_geom(win
, win
->ws
->r
);
1000 ewmh_update_actions(struct ws_win
*win
)
1002 xcb_atom_t actions
[SWM_EWMH_ACTION_COUNT_MAX
];
1008 actions
[n
++] = ewmh
[_NET_WM_ACTION_CLOSE
].atom
;
1010 if (win
->floating
) {
1011 actions
[n
++] = ewmh
[_NET_WM_ACTION_MOVE
].atom
;
1012 actions
[n
++] = ewmh
[_NET_WM_ACTION_RESIZE
].atom
;
1013 actions
[n
++] = ewmh
[_NET_WM_ACTION_ABOVE
].atom
;
1016 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
1017 ewmh
[_NET_WM_ALLOWED_ACTIONS
].atom
, XCB_ATOM_ATOM
, 32, 1, actions
);
1020 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
1021 #define _NET_WM_STATE_ADD 1 /* add/set property */
1022 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
1025 ewmh_update_win_state(struct ws_win
*win
, xcb_atom_t state
, long action
)
1027 unsigned int mask
= 0;
1028 unsigned int changed
= 0;
1029 unsigned int orig_flags
;
1034 DNPRINTF(SWM_D_PROP
, "ewmh_update_win_state: window: 0x%x, state: %ld, "
1035 "action: %ld\n", win
->id
, (unsigned long)state
, action
);
1037 if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
)
1038 mask
= EWMH_F_FULLSCREEN
;
1039 else if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
)
1040 mask
= EWMH_F_ABOVE
;
1041 else if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
)
1042 mask
= SWM_F_MANUAL
;
1043 else if (state
== ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
)
1044 mask
= EWMH_F_SKIP_PAGER
;
1045 else if (state
== ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
)
1046 mask
= EWMH_F_SKIP_TASKBAR
;
1048 orig_flags
= win
->ewmh_flags
;
1051 case _NET_WM_STATE_REMOVE
:
1052 win
->ewmh_flags
&= ~mask
;
1054 case _NET_WM_STATE_ADD
:
1055 win
->ewmh_flags
|= mask
;
1057 case _NET_WM_STATE_TOGGLE
:
1058 win
->ewmh_flags
^= mask
;
1062 changed
= (win
->ewmh_flags
& mask
) ^ (orig_flags
& mask
) ? 1 : 0;
1064 if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
) {
1065 if (changed
&& !floating_toggle_win(win
))
1066 win
->ewmh_flags
= orig_flags
; /* revert */
1067 } else if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
) {
1069 win
->manual
= (win
->ewmh_flags
& SWM_F_MANUAL
) != 0;
1070 } else if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
) {
1071 if (changed
&& !ewmh_set_win_fullscreen(win
,
1072 win
->ewmh_flags
& EWMH_F_FULLSCREEN
))
1073 win
->ewmh_flags
= orig_flags
; /* revert */
1076 xcb_delete_property(conn
, win
->id
, ewmh
[_NET_WM_STATE
].atom
);
1078 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
1079 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1080 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1081 &ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
);
1082 else if (win
->ewmh_flags
& EWMH_F_SKIP_PAGER
)
1083 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1084 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1085 &ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
);
1086 else if (win
->ewmh_flags
& EWMH_F_SKIP_TASKBAR
)
1087 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1088 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1089 &ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
);
1090 else if (win
->ewmh_flags
& EWMH_F_ABOVE
)
1091 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1092 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1093 &ewmh
[_NET_WM_STATE_ABOVE
].atom
);
1094 else if (win
->ewmh_flags
& SWM_F_MANUAL
)
1095 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1096 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1097 &ewmh
[_SWM_WM_STATE_MANUAL
].atom
);
1101 ewmh_get_win_state(struct ws_win
*win
)
1104 xcb_get_property_cookie_t c
;
1105 xcb_get_property_reply_t
*r
;
1111 win
->ewmh_flags
= 0;
1113 win
->ewmh_flags
|= EWMH_F_ABOVE
;
1115 win
->ewmh_flags
|= SWM_F_MANUAL
;
1117 c
= xcb_get_property(conn
, 0, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
1118 XCB_ATOM_ATOM
, 0, UINT32_MAX
);
1119 r
= xcb_get_property_reply(conn
, c
, NULL
);
1123 states
= xcb_get_property_value(r
);
1124 n
= xcb_get_property_value_length(r
);
1126 for (i
= 0; i
< n
; i
++)
1127 ewmh_update_win_state(win
, states
[i
], _NET_WM_STATE_ADD
);
1135 dumpwins(struct swm_region
*r
, union arg
*args
)
1139 xcb_get_window_attributes_cookie_t c
;
1140 xcb_get_window_attributes_reply_t
*wa
;
1142 /* suppress unused warning since var is needed */
1145 if (r
->ws
== NULL
) {
1146 warnx("dumpwins: invalid workspace");
1150 warnx("=== managed window list ws %02d ===", r
->ws
->idx
);
1151 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
1152 state
= getstate(win
->id
);
1153 c
= xcb_get_window_attributes(conn
, win
->id
);
1154 wa
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
1156 warnx("window: 0x%x, map_state: %d, state: %u, "
1157 "transient: 0x%x", win
->id
, wa
->map_state
,
1158 state
, win
->transient
);
1161 warnx("window: 0x%x, failed xcb_get_window_attributes",
1165 warnx("===== unmanaged window list =====");
1166 TAILQ_FOREACH(win
, &r
->ws
->unmanagedlist
, entry
) {
1167 state
= getstate(win
->id
);
1168 c
= xcb_get_window_attributes(conn
, win
->id
);
1169 wa
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
1171 warnx("window: 0x%x, map_state: %d, state: %u, "
1172 "transient: 0x%x", win
->id
, wa
->map_state
,
1173 state
, win
->transient
);
1176 warnx("window: 0x%x, failed xcb_get_window_attributes",
1180 warnx("=================================");
1184 dumpwins(struct swm_region
*r
, union arg
*s
)
1189 #endif /* SWM_DEBUG */
1194 int saved_errno
, status
;
1197 saved_errno
= errno
;
1201 while ((pid
= waitpid(WAIT_ANY
, &status
, WNOHANG
)) != 0) {
1206 if (errno
!= ECHILD
)
1207 warn("sighdlr: waitpid");
1208 #endif /* SWM_DEBUG */
1211 if (pid
== searchpid
)
1215 if (WIFEXITED(status
)) {
1216 if (WEXITSTATUS(status
) != 0)
1217 warnx("sighdlr: child exit status: %d",
1218 WEXITSTATUS(status
));
1220 warnx("sighdlr: child is terminated "
1222 #endif /* SWM_DEBUG */
1236 errno
= saved_errno
;
1242 struct pid_e
*p
= NULL
;
1244 DNPRINTF(SWM_D_MISC
, "find_pid: %d\n", pid
);
1249 TAILQ_FOREACH(p
, &pidlist
, entry
) {
1258 name_to_pixel(const char *colorname
)
1260 uint32_t result
= 0;
1261 char cname
[32] = "#";
1262 xcb_screen_t
*screen
;
1263 xcb_colormap_t cmap
;
1264 xcb_alloc_color_reply_t
*cr
;
1265 xcb_alloc_named_color_reply_t
*nr
;
1266 uint16_t rr
, gg
, bb
;
1268 screen
= xcb_setup_roots_iterator(xcb_get_setup(conn
)).data
;
1269 cmap
= screen
->default_colormap
;
1271 /* color is in format rgb://rr/gg/bb */
1272 if (strncmp(colorname
, "rgb:", 4) == 0) {
1273 if (parse_rgb(colorname
, &rr
, &gg
, &bb
) == -1)
1274 warnx("could not parse rgb %s", colorname
);
1276 cr
= xcb_alloc_color_reply(conn
,
1277 xcb_alloc_color(conn
, cmap
, rr
, gg
, bb
),
1283 warnx("color '%s' not found", colorname
);
1286 nr
= xcb_alloc_named_color_reply(conn
,
1287 xcb_alloc_named_color(conn
, cmap
, strlen(colorname
),
1290 strlcat(cname
, colorname
+ 2, sizeof cname
- 1);
1291 nr
= xcb_alloc_named_color_reply(conn
,
1292 xcb_alloc_named_color(conn
, cmap
, strlen(cname
),
1299 warnx("color '%s' not found", colorname
);
1306 setscreencolor(char *val
, int i
, int c
)
1310 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1311 if (i
> 0 && i
<= num_screens
) {
1312 screens
[i
- 1].c
[c
].pixel
= name_to_pixel(val
);
1313 free(screens
[i
- 1].c
[c
].name
);
1314 if ((screens
[i
- 1].c
[c
].name
= strdup(val
)) == NULL
)
1316 } else if (i
== -1) {
1317 for (i
= 0; i
< num_screens
; i
++) {
1318 screens
[i
].c
[c
].pixel
= name_to_pixel(val
);
1319 free(screens
[i
].c
[c
].name
);
1320 if ((screens
[i
].c
[c
].name
= strdup(val
)) == NULL
)
1324 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1329 fancy_stacker(struct workspace
*ws
)
1331 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1332 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1333 snprintf(ws
->stacker
, sizeof ws
->stacker
,
1334 ws
->l_state
.vertical_flip
? "[%d>%d]" : "[%d|%d]",
1335 ws
->l_state
.vertical_mwin
, ws
->l_state
.vertical_stacks
);
1336 if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1337 snprintf(ws
->stacker
, sizeof ws
->stacker
,
1338 ws
->l_state
.horizontal_flip
? "[%dv%d]" : "[%d-%d]",
1339 ws
->l_state
.horizontal_mwin
, ws
->l_state
.horizontal_stacks
);
1343 plain_stacker(struct workspace
*ws
)
1345 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1346 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1347 strlcpy(ws
->stacker
, ws
->l_state
.vertical_flip
? "[>]" : "[|]",
1348 sizeof ws
->stacker
);
1349 if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1350 strlcpy(ws
->stacker
, ws
->l_state
.horizontal_flip
? "[v]" : "[-]",
1351 sizeof ws
->stacker
);
1355 custom_region(char *val
)
1357 unsigned int x
, y
, w
, h
;
1358 int sidx
, num_screens
;
1359 xcb_screen_t
*screen
;
1361 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1362 if (sscanf(val
, "screen[%d]:%ux%u+%u+%u", &sidx
, &w
, &h
, &x
, &y
) != 5)
1363 errx(1, "invalid custom region, "
1364 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1365 if (sidx
< 1 || sidx
> num_screens
)
1366 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1370 if ((screen
= get_screen(sidx
)) == NULL
)
1371 errx(1, "ERROR: can't get screen %d.", sidx
);
1374 errx(1, "region %ux%u+%u+%u too small", w
, h
, x
, y
);
1376 if (x
> screen
->width_in_pixels
||
1377 y
> screen
->height_in_pixels
||
1378 w
+ x
> screen
->width_in_pixels
||
1379 h
+ y
> screen
->height_in_pixels
) {
1380 warnx("ignoring region %ux%u+%u+%u - not within screen "
1381 "boundaries (%ux%u)", w
, h
, x
, y
,
1382 screen
->width_in_pixels
, screen
->height_in_pixels
);
1386 new_region(&screens
[sidx
], x
, y
, w
, h
);
1390 socket_setnonblock(int fd
)
1394 if ((flags
= fcntl(fd
, F_GETFL
, 0)) == -1)
1395 err(1, "fcntl F_GETFL");
1396 flags
|= O_NONBLOCK
;
1397 if ((flags
= fcntl(fd
, F_SETFL
, flags
)) == -1)
1398 err(1, "fcntl F_SETFL");
1402 bar_print_legacy(struct swm_region
*r
, const char *s
)
1404 xcb_rectangle_t rect
;
1409 XRectangle ibox
, lbox
;
1413 XmbTextExtents(bar_fs
, s
, len
, &ibox
, &lbox
);
1415 switch (bar_justify
) {
1416 case SWM_BAR_JUSTIFY_LEFT
:
1419 case SWM_BAR_JUSTIFY_CENTER
:
1420 x
= (WIDTH(r
) - lbox
.width
) / 2;
1422 case SWM_BAR_JUSTIFY_RIGHT
:
1423 x
= WIDTH(r
) - lbox
.width
- SWM_BAR_OFFSET
;
1427 if (x
< SWM_BAR_OFFSET
)
1432 rect
.width
= WIDTH(r
->bar
);
1433 rect
.height
= HEIGHT(r
->bar
);
1435 /* clear back buffer */
1436 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
1437 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
1438 xcb_poly_fill_rectangle(conn
, r
->bar
->buffer
, r
->s
->bar_gc
, 1, &rect
);
1440 /* draw back buffer */
1441 gcvd
.graphics_exposures
= 0;
1442 draw
= XCreateGC(display
, r
->bar
->buffer
, GCGraphicsExposures
, &gcvd
);
1443 XSetForeground(display
, draw
, r
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
);
1444 DRAWSTRING(display
, r
->bar
->buffer
, bar_fs
, draw
,
1445 x
, (bar_fs_extents
->max_logical_extent
.height
- lbox
.height
) / 2 -
1447 XFreeGC(display
, draw
);
1450 xcb_copy_area(conn
, r
->bar
->buffer
, r
->bar
->id
, r
->s
->bar_gc
, 0, 0,
1451 0, 0, WIDTH(r
->bar
), HEIGHT(r
->bar
));
1455 bar_print(struct swm_region
*r
, const char *s
)
1458 xcb_rectangle_t rect
;
1466 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
, &info
);
1468 switch (bar_justify
) {
1469 case SWM_BAR_JUSTIFY_LEFT
:
1472 case SWM_BAR_JUSTIFY_CENTER
:
1473 x
= (WIDTH(r
) - info
.width
) / 2;
1475 case SWM_BAR_JUSTIFY_RIGHT
:
1476 x
= WIDTH(r
) - info
.width
- SWM_BAR_OFFSET
;
1480 if (x
< SWM_BAR_OFFSET
)
1485 rect
.width
= WIDTH(r
->bar
);
1486 rect
.height
= HEIGHT(r
->bar
);
1488 /* clear back buffer */
1489 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
1490 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
1491 xcb_poly_fill_rectangle(conn
, r
->bar
->buffer
, r
->s
->bar_gc
, 1, &rect
);
1493 /* draw back buffer */
1494 draw
= XftDrawCreate(display
, r
->bar
->buffer
,
1495 DefaultVisual(display
, r
->s
->idx
),
1496 DefaultColormap(display
, r
->s
->idx
));
1498 XftDrawStringUtf8(draw
, &bar_font_color
, bar_font
, x
,
1499 (HEIGHT(r
->bar
) + bar_font
->height
) / 2 - bar_font
->descent
,
1502 XftDrawDestroy(draw
);
1505 xcb_copy_area(conn
, r
->bar
->buffer
, r
->bar
->id
, r
->s
->bar_gc
, 0, 0,
1506 0, 0, WIDTH(r
->bar
), HEIGHT(r
->bar
));
1510 bar_extra_stop(void)
1514 bzero(bar_pipe
, sizeof bar_pipe
);
1517 kill(bar_pid
, SIGTERM
);
1520 strlcpy((char *)bar_ext
, "", sizeof bar_ext
);
1525 bar_class_name(char *s
, size_t sz
, struct swm_region
*r
)
1527 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1529 if (r
->ws
->focus
->ch
.class_name
!= NULL
)
1530 strlcat(s
, r
->ws
->focus
->ch
.class_name
, sz
);
1534 bar_title_name(char *s
, size_t sz
, struct swm_region
*r
)
1536 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1538 if (r
->ws
->focus
->ch
.instance_name
!= NULL
)
1539 strlcat(s
, r
->ws
->focus
->ch
.instance_name
, sz
);
1543 bar_class_title_name(char *s
, size_t sz
, struct swm_region
*r
)
1545 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1548 bar_class_name(s
, sz
, r
);
1549 strlcat(s
, ":", sz
);
1550 bar_title_name(s
, sz
, r
);
1554 bar_window_float(char *s
, size_t sz
, struct swm_region
*r
)
1556 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1558 if (r
->ws
->focus
->floating
)
1559 strlcat(s
, "(f)", sz
);
1563 bar_window_name(char *s
, size_t sz
, struct swm_region
*r
)
1567 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1569 if ((title
= get_win_name(r
->ws
->focus
->id
)) == NULL
)
1572 strlcat(s
, title
, sz
);
1576 int urgent
[SWM_WS_MAX
];
1578 bar_urgent(char *s
, size_t sz
)
1581 int i
, j
, num_screens
;
1583 xcb_get_property_cookie_t c
;
1584 xcb_icccm_wm_hints_t hints
;
1586 for (i
= 0; i
< workspace_limit
; i
++)
1589 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1590 for (i
= 0; i
< num_screens
; i
++)
1591 for (j
= 0; j
< workspace_limit
; j
++)
1592 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
) {
1593 c
= xcb_icccm_get_wm_hints(conn
, win
->id
);
1594 if (xcb_icccm_get_wm_hints_reply(conn
, c
,
1597 if (hints
.flags
& XCB_ICCCM_WM_HINT_X_URGENCY
)
1601 for (i
= 0; i
< workspace_limit
; i
++) {
1603 snprintf(b
, sizeof b
, "%d ", i
+ 1);
1605 snprintf(b
, sizeof b
, "- ");
1611 bar_workspace_name(char *s
, size_t sz
, struct swm_region
*r
)
1613 if (r
== NULL
|| r
->ws
== NULL
)
1615 if (r
->ws
->name
!= NULL
)
1616 strlcat(s
, r
->ws
->name
, sz
);
1619 /* build the default bar format according to the defined enabled options */
1621 bar_fmt(const char *fmtexp
, char *fmtnew
, struct swm_region
*r
, size_t sz
)
1623 /* if format provided, just copy the buffers */
1624 if (bar_format
!= NULL
) {
1625 strlcpy(fmtnew
, fmtexp
, sz
);
1629 /* reset the output buffer */
1632 strlcat(fmtnew
, "+N:+I ", sz
);
1634 strlcat(fmtnew
, "+S", sz
);
1635 strlcat(fmtnew
, " ", sz
);
1637 /* only show the workspace name if there's actually one */
1638 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->name
!= NULL
)
1639 strlcat(fmtnew
, "<+D>", sz
);
1640 strlcat(fmtnew
, "+3<", sz
);
1642 if (clock_enabled
) {
1643 strlcat(fmtnew
, fmtexp
, sz
);
1644 strlcat(fmtnew
, "+4<", sz
);
1647 /* bar_urgent already adds the space before the last asterisk */
1649 strlcat(fmtnew
, "* +U*+4<", sz
);
1651 if (title_class_enabled
) {
1652 strlcat(fmtnew
, "+C", sz
);
1653 if (title_name_enabled
== 0)
1654 strlcat(fmtnew
, "+4<", sz
);
1657 /* checks needed by the colon and floating strlcat(3) calls below */
1658 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->focus
!= NULL
) {
1659 if (title_name_enabled
) {
1660 if (title_class_enabled
)
1661 strlcat(fmtnew
, ":", sz
);
1662 strlcat(fmtnew
, "+T+4<", sz
);
1664 if (window_name_enabled
) {
1665 if (r
->ws
->focus
->floating
)
1666 strlcat(fmtnew
, "+F ", sz
);
1667 strlcat(fmtnew
, "+64W ", sz
);
1671 /* finally add the action script output and the version */
1672 strlcat(fmtnew
, "+4<+A+4<+V", sz
);
1676 bar_replace_pad(char *tmp
, int *limit
, size_t sz
)
1678 /* special case; no limit given, pad one space, instead */
1679 if (*limit
== (int)sz
- 1)
1681 snprintf(tmp
, sz
, "%*s", *limit
, " ");
1684 /* replaces the bar format character sequences (like in tmux(1)) */
1686 bar_replace_seq(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t *offrep
,
1690 char tmp
[SWM_BAR_MAX
];
1694 /* reset strlcat(3) buffer */
1697 /* get number, if any */
1700 if (sscanf(fmt
, "%d%n", &limit
, &size
) != 1)
1701 limit
= sizeof tmp
- 1;
1702 if (limit
<= 0 || limit
>= (int)sizeof tmp
)
1703 limit
= sizeof tmp
- 1;
1705 /* there is nothing to replace (ie EOL) */
1712 bar_replace_pad(tmp
, &limit
, sizeof tmp
);
1715 snprintf(tmp
, sizeof tmp
, "%s", bar_ext
);
1718 bar_class_name(tmp
, sizeof tmp
, r
);
1721 bar_workspace_name(tmp
, sizeof tmp
, r
);
1724 bar_window_float(tmp
, sizeof tmp
, r
);
1727 snprintf(tmp
, sizeof tmp
, "%d", r
->ws
->idx
+ 1);
1730 snprintf(tmp
, sizeof tmp
, "%d", r
->s
->idx
+ 1);
1733 bar_class_title_name(tmp
, sizeof tmp
, r
);
1736 snprintf(tmp
, sizeof tmp
, "%s", r
->ws
->stacker
);
1739 bar_title_name(tmp
, sizeof tmp
, r
);
1742 bar_urgent(tmp
, sizeof tmp
);
1745 snprintf(tmp
, sizeof tmp
, "%s", bar_vertext
);
1748 bar_window_name(tmp
, sizeof tmp
, r
);
1751 /* unknown character sequence; copy as-is */
1752 snprintf(tmp
, sizeof tmp
, "+%c", *fmt
);
1758 if ((int)len
< limit
)
1760 while (limit
-- > 0) {
1761 if (*offrep
>= sz
- 1)
1763 fmtrep
[(*offrep
)++] = *ptr
++;
1771 bar_replace(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t sz
)
1776 while (*fmt
!= '\0') {
1778 /* skip ordinary characters */
1781 fmtrep
[off
++] = *fmt
++;
1785 /* character sequence found; replace it */
1786 fmt
= bar_replace_seq(fmt
, fmtrep
, r
, &off
, sz
);
1795 bar_fmt_expand(char *fmtexp
, size_t sz
)
1802 /* start by grabbing the current time and date */
1804 localtime_r(&tmt
, &tm
);
1806 /* figure out what to expand */
1807 if (bar_format
!= NULL
)
1809 else if (bar_format
== NULL
&& clock_enabled
)
1811 /* if nothing to expand bail out */
1817 /* copy as-is, just in case the format shouldn't be expanded below */
1818 strlcpy(fmtexp
, fmt
, sz
);
1819 /* finally pass the string through strftime(3) */
1820 #ifndef SWM_DENY_CLOCK_FORMAT
1821 if ((len
= strftime(fmtexp
, sz
, fmt
, &tm
)) == 0)
1822 warnx("format too long");
1830 char fmtexp
[SWM_BAR_MAX
], fmtnew
[SWM_BAR_MAX
];
1831 char fmtrep
[SWM_BAR_MAX
];
1833 struct swm_region
*r
;
1835 /* expand the format by first passing it through strftime(3) */
1836 bar_fmt_expand(fmtexp
, sizeof fmtexp
);
1838 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1839 for (i
= 0; i
< num_screens
; i
++) {
1840 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
1843 bar_fmt(fmtexp
, fmtnew
, r
, sizeof fmtnew
);
1844 bar_replace(fmtnew
, fmtrep
, r
, sizeof fmtrep
);
1845 if (bar_font_legacy
)
1846 bar_print_legacy(r
, fmtrep
);
1848 bar_print(r
, fmtrep
);
1859 if (bar_enabled
== 0)
1861 if (bar_extra
&& bar_extra_running
) {
1862 /* ignore short reads; it'll correct itself */
1863 while ((b
= fgetln(stdin
, &len
)) != NULL
)
1864 if (b
&& b
[len
- 1] == '\n') {
1866 strlcpy((char *)bar_ext
, b
, sizeof bar_ext
);
1868 if (b
== NULL
&& errno
!= EAGAIN
) {
1869 warn("bar_update: bar_extra failed");
1873 strlcpy((char *)bar_ext
, "", sizeof bar_ext
);
1882 /* suppress unused warning since var is needed */
1889 bar_toggle(struct swm_region
*r
, union arg
*args
)
1891 struct swm_region
*tmpr
;
1894 /* suppress unused warnings since vars are needed */
1898 DNPRINTF(SWM_D_BAR
, "bar_toggle\n");
1900 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1902 for (i
= 0; i
< num_screens
; i
++)
1903 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
1905 xcb_unmap_window(conn
, tmpr
->bar
->id
);
1907 for (i
= 0; i
< num_screens
; i
++)
1908 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
1910 map_window_raised(tmpr
->bar
->id
);
1913 bar_enabled
= !bar_enabled
;
1917 /* must be after stack */
1926 struct swm_region
*r
;
1930 /* do this here because the conf file is in memory */
1931 if (bar_extra
&& bar_extra_running
== 0 && bar_argv
[0]) {
1932 /* launch external status app */
1933 bar_extra_running
= 1;
1934 if (pipe(bar_pipe
) == -1)
1935 err(1, "pipe error");
1936 socket_setnonblock(bar_pipe
[0]);
1937 socket_setnonblock(bar_pipe
[1]); /* XXX hmmm, really? */
1938 if (dup2(bar_pipe
[0], 0) == -1)
1940 if (dup2(bar_pipe
[1], 1) == -1)
1942 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
1943 err(1, "could not disable SIGPIPE");
1944 switch (bar_pid
= fork()) {
1946 err(1, "cannot fork");
1950 execvp(bar_argv
[0], bar_argv
);
1951 err(1, "%s external app failed", bar_argv
[0]);
1953 default: /* parent */
1959 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1960 for (i
= 0; i
< num_screens
; i
++)
1961 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
1964 wa
[0] = screens
[i
].c
[SWM_S_COLOR_BAR
].pixel
;
1965 wa
[1] = screens
[i
].c
[SWM_S_COLOR_BAR_BORDER
].pixel
;
1966 xcb_change_window_attributes(conn
, r
->bar
->id
,
1967 XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
, wa
);
1977 while ((s
= index(s
, '-'))) {
1982 return (count
== 14);
1988 char *default_string
;
1989 char **missing_charsets
;
1990 int num_missing_charsets
= 0;
1994 XFreeFontSet(display
, bar_fs
);
1998 DNPRINTF(SWM_D_INIT
, "fontset_init: loading bar_fonts: %s\n", bar_fonts
);
2000 bar_fs
= XCreateFontSet(display
, bar_fonts
, &missing_charsets
,
2001 &num_missing_charsets
, &default_string
);
2003 if (num_missing_charsets
> 0) {
2004 warnx("Unable to load charset(s):");
2006 for (i
= 0; i
< num_missing_charsets
; ++i
)
2007 warnx("%s", missing_charsets
[i
]);
2009 XFreeStringList(missing_charsets
);
2011 if (strcmp(default_string
, ""))
2012 warnx("Glyphs from those sets will be replaced "
2013 "by '%s'.", default_string
);
2015 warnx("Glyphs from those sets won't be drawn.");
2019 errx(1, "Error creating font set structure.");
2021 bar_fs_extents
= XExtentsOfFontSet(bar_fs
);
2023 bar_height
= bar_fs_extents
->max_logical_extent
.height
+
2024 2 * bar_border_width
;
2031 xft_init(struct swm_region
*r
)
2033 char *font
, *d
, *search
;
2036 if (bar_font
== NULL
) {
2037 if ((d
= strdup(bar_fonts
)) == NULL
)
2038 errx(1, "insufficient memory.");
2040 while ((font
= strsep(&search
, ",")) != NULL
) {
2044 DNPRINTF(SWM_D_INIT
, "xft_init: try font %s\n", font
);
2047 bar_font
= XftFontOpenXlfd(display
, r
->s
->idx
,
2050 bar_font
= XftFontOpenName(display
, r
->s
->idx
,
2055 warnx("unable to load font %s", font
);
2058 DNPRINTF(SWM_D_INIT
, "successfully opened "
2066 if (bar_font
== NULL
)
2067 errx(1, "unable to open a font");
2069 PIXEL_TO_XRENDERCOLOR(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
, color
);
2071 if (!XftColorAllocValue(display
, DefaultVisual(display
, r
->s
->idx
),
2072 DefaultColormap(display
, r
->s
->idx
), &color
, &bar_font_color
))
2073 warn("unable to allocate Xft color");
2075 bar_height
= bar_font
->height
+ 2 * bar_border_width
;
2082 bar_setup(struct swm_region
*r
)
2084 xcb_screen_t
*screen
;
2087 DNPRINTF(SWM_D_BAR
, "bar_setup: screen %d.\n",
2090 if ((screen
= get_screen(r
->s
->idx
)) == NULL
)
2091 errx(1, "ERROR: can't get screen %d.", r
->s
->idx
);
2096 if ((r
->bar
= calloc(1, sizeof(struct swm_bar
))) == NULL
)
2097 err(1, "bar_setup: calloc: failed to allocate memory.");
2099 if (bar_font_legacy
)
2105 Y(r
->bar
) = bar_at_bottom
? (Y(r
) + HEIGHT(r
) - bar_height
) : Y(r
);
2106 WIDTH(r
->bar
) = WIDTH(r
) - 2 * bar_border_width
;
2107 HEIGHT(r
->bar
) = bar_height
- 2 * bar_border_width
;
2109 r
->bar
->id
= xcb_generate_id(conn
);
2110 wa
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
2111 wa
[1] = r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].pixel
;
2112 wa
[2] = XCB_EVENT_MASK_EXPOSURE
;
2114 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, r
->bar
->id
, r
->s
->root
,
2115 X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
), HEIGHT(r
->bar
),
2116 bar_border_width
, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
2117 XCB_COPY_FROM_PARENT
, XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
2118 | XCB_CW_EVENT_MASK
, wa
);
2120 r
->bar
->buffer
= xcb_generate_id(conn
);
2121 xcb_create_pixmap(conn
, screen
->root_depth
, r
->bar
->buffer
, r
->bar
->id
,
2122 WIDTH(r
->bar
), HEIGHT(r
->bar
));
2125 xcb_randr_select_input(conn
, r
->bar
->id
,
2126 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE
);
2129 map_window_raised(r
->bar
->id
);
2131 DNPRINTF(SWM_D_BAR
, "bar_setup: window: 0x%x, (x,y) w x h: (%d,%d) "
2132 "%d x %d\n", WINID(r
->bar
), X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
),
2135 if (signal(SIGALRM
, bar_signal
) == SIG_ERR
)
2136 err(1, "could not install bar_signal");
2141 bar_cleanup(struct swm_region
*r
)
2145 xcb_destroy_window(conn
, r
->bar
->id
);
2146 xcb_free_pixmap(conn
, r
->bar
->buffer
);
2152 set_win_state(struct ws_win
*win
, uint16_t state
)
2154 uint16_t data
[2] = { state
, XCB_ATOM_NONE
};
2156 DNPRINTF(SWM_D_EVENT
, "set_win_state: window: 0x%x\n", win
->id
);
2161 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
, a_state
,
2162 a_state
, 32, 2, data
);
2166 getstate(xcb_window_t w
)
2168 uint32_t result
= 0;
2169 xcb_get_property_cookie_t c
;
2170 xcb_get_property_reply_t
*r
;
2172 c
= xcb_get_property(conn
, 0, w
, a_state
, a_state
, 0L, 2L);
2173 r
= xcb_get_property_reply(conn
, c
, NULL
);
2176 if (r
->type
== a_state
&& r
->format
== 32 && r
->length
== 2)
2177 result
= *((uint32_t *)xcb_get_property_value(r
));
2181 DNPRINTF(SWM_D_MISC
, "getstate property: win 0x%x state %u\n", w
,
2187 version(struct swm_region
*r
, union arg
*args
)
2189 /* suppress unused warnings since vars are needed */
2193 bar_version
= !bar_version
;
2195 snprintf(bar_vertext
, sizeof bar_vertext
,
2196 "Version: %s Build: %s", SPECTRWM_VERSION
, buildstr
);
2198 strlcpy(bar_vertext
, "", sizeof bar_vertext
);
2205 client_msg(struct ws_win
*win
, xcb_atom_t a
)
2207 xcb_client_message_event_t ev
;
2212 bzero(&ev
, sizeof ev
);
2213 ev
.response_type
= XCB_CLIENT_MESSAGE
;
2214 ev
.window
= win
->id
;
2217 ev
.data
.data32
[0] = a
;
2218 ev
.data
.data32
[1] = XCB_CURRENT_TIME
;
2220 xcb_send_event(conn
, 0, win
->id
,
2221 XCB_EVENT_MASK_NO_EVENT
, (const char *)&ev
);
2224 /* synthetic response to a ConfigureRequest when not making a change */
2226 config_win(struct ws_win
*win
, xcb_configure_request_event_t
*ev
)
2228 xcb_configure_notify_event_t ce
;
2233 /* send notification of unchanged state. */
2234 bzero(&ce
, sizeof(ce
));
2235 ce
.response_type
= XCB_CONFIGURE_NOTIFY
;
2238 ce
.width
= WIDTH(win
);
2239 ce
.height
= HEIGHT(win
);
2240 ce
.override_redirect
= 0;
2245 ce
.window
= win
->id
;
2246 ce
.border_width
= BORDER(win
);
2247 ce
.above_sibling
= XCB_WINDOW_NONE
;
2250 ce
.event
= ev
->window
;
2251 ce
.window
= ev
->window
;
2253 /* make response appear more WM_SIZE_HINTS-compliant */
2254 if (win
->sh
.flags
) {
2255 DNPRINTF(SWM_D_MISC
, "config_win: hints: window: 0x%x,"
2256 " sh.flags: %u, min: %d x %d, max: %d x %d, inc: "
2257 "%d x %d\n", win
->id
, win
->sh
.flags
, SH_MIN_W(win
),
2258 SH_MIN_H(win
), SH_MAX_W(win
), SH_MAX_H(win
),
2259 SH_INC_W(win
), SH_INC_H(win
));
2264 /* the hint may be set... to 0! */
2265 if (SH_MIN_W(win
) > 0 && ce
.width
< SH_MIN_W(win
))
2266 ce
.width
= SH_MIN_W(win
);
2267 if (SH_MIN_H(win
) > 0 && ce
.height
< SH_MIN_H(win
))
2268 ce
.height
= SH_MIN_H(win
);
2273 /* may also be advertized as 0 */
2274 if (SH_MAX_W(win
) > 0 && ce
.width
> SH_MAX_W(win
))
2275 ce
.width
= SH_MAX_W(win
);
2276 if (SH_MAX_H(win
) > 0 && ce
.height
> SH_MAX_H(win
))
2277 ce
.height
= SH_MAX_H(win
);
2280 /* resize increment. */
2282 if (SH_INC_W(win
) > 1 && ce
.width
> SH_INC_W(win
))
2283 ce
.width
-= (ce
.width
- SH_MIN_W(win
)) %
2285 if (SH_INC_H(win
) > 1 && ce
.height
> SH_INC_H(win
))
2286 ce
.height
-= (ce
.height
- SH_MIN_H(win
)) %
2290 /* adjust x and y for requested border_width. */
2291 ce
.x
+= BORDER(win
) - ev
->border_width
;
2292 ce
.y
+= BORDER(win
) - ev
->border_width
;
2293 ce
.border_width
= ev
->border_width
;
2294 ce
.above_sibling
= ev
->sibling
;
2297 DNPRINTF(SWM_D_MISC
, "config_win: ewmh: %s, window: 0x%x, (x,y) w x h: "
2298 "(%d,%d) %d x %d, border: %d\n", YESNO(ev
== NULL
), win
->id
, ce
.x
,
2299 ce
.y
, ce
.width
, ce
.height
, ce
.border_width
);
2301 xcb_send_event(conn
, 0, win
->id
, XCB_EVENT_MASK_STRUCTURE_NOTIFY
,
2306 count_win(struct workspace
*ws
, int count_transient
)
2311 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
2312 if (count_transient
== 0 && win
->floating
)
2314 if (count_transient
== 0 && win
->transient
)
2320 DNPRINTF(SWM_D_MISC
, "count_win: %d\n", count
);
2326 quit(struct swm_region
*r
, union arg
*args
)
2328 /* suppress unused warnings since vars are needed */
2332 DNPRINTF(SWM_D_MISC
, "quit\n");
2337 unmap_window(struct ws_win
*win
)
2339 DNPRINTF(SWM_D_EVENT
, "unmap_window: window: 0x%x\n", win
->id
);
2344 /* don't unmap again */
2345 if (getstate(win
->id
) == XCB_ICCCM_WM_STATE_ICONIC
)
2348 set_win_state(win
, XCB_ICCCM_WM_STATE_ICONIC
);
2350 xcb_unmap_window(conn
, win
->id
);
2351 xcb_change_window_attributes(conn
, win
->id
,
2352 XCB_CW_BORDER_PIXEL
, &win
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
);
2359 int i
, j
, num_screens
;
2361 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2362 for (i
= 0; i
< num_screens
; i
++)
2363 for (j
= 0; j
< workspace_limit
; j
++)
2364 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2369 fake_keypress(struct ws_win
*win
, xcb_keysym_t keysym
, uint16_t modifiers
)
2371 xcb_key_press_event_t event
;
2372 xcb_keycode_t
*keycode
;
2377 keycode
= xcb_key_symbols_get_keycode(syms
, keysym
);
2379 DNPRINTF(SWM_D_MISC
, "fake_keypress: win 0x%x keycode %u\n",
2382 bzero(&event
, sizeof(event
));
2383 event
.event
= win
->id
;
2384 event
.root
= win
->s
->root
;
2385 event
.child
= XCB_WINDOW_NONE
;
2386 event
.time
= XCB_CURRENT_TIME
;
2387 event
.event_x
= X(win
);
2388 event
.event_y
= Y(win
);
2391 event
.same_screen
= 1;
2392 event
.detail
= *keycode
;
2393 event
.state
= modifiers
;
2395 event
.response_type
= XCB_KEY_PRESS
;
2396 xcb_send_event(conn
, 1, win
->id
,
2397 XCB_EVENT_MASK_KEY_PRESS
, (const char *)&event
);
2399 event
.response_type
= XCB_KEY_RELEASE
;
2400 xcb_send_event(conn
, 1, win
->id
,
2401 XCB_EVENT_MASK_KEY_RELEASE
, (const char *)&event
);
2407 restart(struct swm_region
*r
, union arg
*args
)
2409 /* suppress unused warning since var is needed */
2413 DNPRINTF(SWM_D_MISC
, "restart: %s\n", start_argv
[0]);
2417 execvp(start_argv
[0], start_argv
);
2418 warn("execvp failed");
2423 root_to_region(xcb_window_t root
)
2425 struct swm_region
*r
= NULL
;
2427 xcb_query_pointer_reply_t
*qpr
;
2429 DNPRINTF(SWM_D_MISC
, "root_to_region: window: 0x%x\n", root
);
2431 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2432 for (i
= 0; i
< num_screens
; i
++)
2433 if (screens
[i
].root
== root
)
2436 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
,
2437 screens
[i
].root
), NULL
);
2440 DNPRINTF(SWM_D_MISC
, "root_to_region: pointer: (%d,%d)\n",
2441 qpr
->root_x
, qpr
->root_y
);
2442 /* choose a region based on pointer location */
2443 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2444 if (X(r
) <= qpr
->root_x
&& qpr
->root_x
< MAX_X(r
) &&
2445 Y(r
) <= qpr
->root_y
&& qpr
->root_y
< MAX_Y(r
))
2451 r
= TAILQ_FIRST(&screens
[i
].rl
);
2457 find_unmanaged_window(xcb_window_t id
)
2460 int i
, j
, num_screens
;
2462 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2463 for (i
= 0; i
< num_screens
; i
++)
2464 for (j
= 0; j
< workspace_limit
; j
++)
2465 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].unmanagedlist
,
2473 find_window(xcb_window_t id
)
2476 int i
, j
, num_screens
;
2477 xcb_query_tree_reply_t
*r
;
2479 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2480 for (i
= 0; i
< num_screens
; i
++)
2481 for (j
= 0; j
< workspace_limit
; j
++)
2482 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2486 r
= xcb_query_tree_reply(conn
, xcb_query_tree(conn
, id
), NULL
);
2490 /* if we were looking for the parent return that window instead */
2491 if (r
->parent
== 0 || r
->root
== r
->parent
) {
2496 /* look for parent */
2497 for (i
= 0; i
< num_screens
; i
++)
2498 for (j
= 0; j
< workspace_limit
; j
++)
2499 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2500 if (r
->parent
== win
->id
) {
2510 spawn(int ws_idx
, union arg
*args
, int close_fd
)
2515 DNPRINTF(SWM_D_MISC
, "spawn: %s\n", args
->argv
[0]);
2517 close(xcb_get_file_descriptor(conn
));
2519 setenv("LD_PRELOAD", SWM_LIB
, 1);
2521 if (asprintf(&ret
, "%d", ws_idx
) == -1) {
2522 warn("spawn: asprintf SWM_WS");
2525 setenv("_SWM_WS", ret
, 1);
2529 if (asprintf(&ret
, "%d", getpid()) == -1) {
2530 warn("spawn: asprintf _SWM_PID");
2533 setenv("_SWM_PID", ret
, 1);
2537 if (setsid() == -1) {
2538 warn("spawn: setsid");
2544 * close stdin and stdout to prevent interaction between apps
2545 * and the baraction script
2546 * leave stderr open to record errors
2548 if ((fd
= open(_PATH_DEVNULL
, O_RDWR
, 0)) == -1) {
2549 warn("spawn: open");
2552 dup2(fd
, STDIN_FILENO
);
2553 dup2(fd
, STDOUT_FILENO
);
2558 execvp(args
->argv
[0], args
->argv
);
2560 warn("spawn: execvp");
2565 kill_refs(struct ws_win
*win
)
2567 int i
, x
, num_screens
;
2568 struct swm_region
*r
;
2569 struct workspace
*ws
;
2574 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2575 for (i
= 0; i
< num_screens
; i
++)
2576 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2577 for (x
= 0; x
< workspace_limit
; x
++) {
2579 if (win
== ws
->focus
)
2581 if (win
== ws
->focus_prev
)
2582 ws
->focus_prev
= NULL
;
2587 validate_win(struct ws_win
*testwin
)
2590 struct workspace
*ws
;
2591 struct swm_region
*r
;
2592 int i
, x
, num_screens
;
2594 if (testwin
== NULL
)
2597 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2598 for (i
= 0; i
< num_screens
; i
++)
2599 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2600 for (x
= 0; x
< workspace_limit
; x
++) {
2602 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
2610 validate_ws(struct workspace
*testws
)
2612 struct swm_region
*r
;
2613 struct workspace
*ws
;
2614 int i
, x
, num_screens
;
2616 /* validate all ws */
2617 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2618 for (i
= 0; i
< num_screens
; i
++)
2619 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2620 for (x
= 0; x
< workspace_limit
; x
++) {
2629 unfocus_win(struct ws_win
*win
)
2631 xcb_window_t none
= XCB_WINDOW_NONE
;
2633 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: window: 0x%x\n", WINID(win
));
2637 if (win
->ws
== NULL
) {
2638 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: NULL ws.\n");
2642 if (validate_ws(win
->ws
)) {
2643 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: invalid ws.\n");
2647 if (win
->ws
->r
== NULL
) {
2648 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: NULL region.\n");
2652 if (validate_win(win
)) {
2653 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: invalid win.\n");
2658 if (win
->ws
->focus
== win
) {
2659 win
->ws
->focus
= NULL
;
2660 win
->ws
->focus_prev
= win
;
2663 if (validate_win(win
->ws
->focus
)) {
2664 kill_refs(win
->ws
->focus
);
2665 win
->ws
->focus
= NULL
;
2667 if (validate_win(win
->ws
->focus_prev
)) {
2668 kill_refs(win
->ws
->focus_prev
);
2669 win
->ws
->focus_prev
= NULL
;
2672 xcb_change_window_attributes(conn
, win
->id
, XCB_CW_BORDER_PIXEL
,
2673 &win
->ws
->r
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
);
2674 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
2675 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1, &none
);
2677 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: done.\n");
2684 int i
, j
, num_screens
;
2686 DNPRINTF(SWM_D_FOCUS
, "unfocus_all\n");
2688 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2689 for (i
= 0; i
< num_screens
; i
++)
2690 for (j
= 0; j
< workspace_limit
; j
++)
2691 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2696 focus_win(struct ws_win
*win
)
2698 struct ws_win
*cfw
= NULL
;
2699 xcb_get_input_focus_reply_t
*r
;
2701 DNPRINTF(SWM_D_FOCUS
, "focus_win: window: 0x%x\n", WINID(win
));
2704 /* Clear the status-bar. */
2709 if (win
->ws
== NULL
)
2712 if (validate_ws(win
->ws
))
2715 if (validate_win(win
)) {
2720 r
= xcb_get_input_focus_reply(conn
, xcb_get_input_focus(conn
), NULL
);
2722 cfw
= find_window(r
->focus
);
2728 if (win
->ws
->focus
!= win
) {
2729 if (win
->ws
->focus
!= cfw
)
2730 unfocus_win(win
->ws
->focus
);
2731 win
->ws
->focus
= win
;
2734 /* Tell app it can set focus. */
2735 if (win
->take_focus
) {
2736 /* java is special; always tell parent */
2737 if (win
->transient
&& win
->java
)
2738 client_msg(find_window(win
->transient
), a_takefocus
);
2740 client_msg(win
, a_takefocus
);
2743 if (cfw
!= win
&& win
->ws
->r
!= NULL
) {
2745 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
,
2746 win
->id
, XCB_CURRENT_TIME
);
2748 xcb_change_window_attributes(conn
, win
->id
, XCB_CW_BORDER_PIXEL
,
2749 &win
->ws
->r
->s
->c
[SWM_S_COLOR_FOCUS
].pixel
);
2751 if (win
->ws
->cur_layout
->flags
& SWM_L_MAPONFOCUS
||
2752 win
->ws
->always_raise
)
2753 map_window_raised(win
->id
);
2755 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
2756 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1,
2763 /* If a child window should have focus instead, return it. */
2765 get_focus_magic(struct ws_win
*win
)
2767 struct ws_win
*parent
= NULL
;
2769 DNPRINTF(SWM_D_FOCUS
, "get_focus_magic: window: 0x%x\n", WINID(win
));
2773 if (win
->transient
) {
2774 parent
= find_window(win
->transient
);
2776 /* If parent prefers focus elsewhere, then do so. */
2777 if (parent
&& parent
->focus_child
) {
2778 if (validate_win(parent
->focus_child
) == 0)
2779 win
= parent
->focus_child
;
2781 parent
->focus_child
= NULL
;
2785 /* If this window prefers focus elsewhere, then do so. */
2786 if (win
->focus_child
) {
2787 if (validate_win(win
->focus_child
) == 0)
2788 win
= win
->focus_child
;
2790 win
->focus_child
= NULL
;
2797 event_drain(uint8_t rt
)
2799 xcb_generic_event_t
*evt
;
2801 /* ensure all pending requests have been processed before filtering. */
2803 while ((evt
= xcb_poll_for_event(conn
))) {
2804 if (XCB_EVENT_RESPONSE_TYPE(evt
) != rt
)
2812 switchws(struct swm_region
*r
, union arg
*args
)
2814 int wsid
= args
->id
, unmap_old
= 0;
2815 struct swm_region
*this_r
, *other_r
;
2817 struct workspace
*new_ws
, *old_ws
;
2823 if (wsid
>= workspace_limit
)
2827 old_ws
= this_r
->ws
;
2828 new_ws
= &this_r
->s
->ws
[wsid
];
2830 DNPRINTF(SWM_D_WS
, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
2831 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), old_ws
->idx
, wsid
);
2833 if (new_ws
== NULL
|| old_ws
== NULL
)
2835 if (new_ws
== old_ws
)
2838 other_r
= new_ws
->r
;
2839 if (other_r
== NULL
) {
2840 /* the other workspace is hidden, hide this one */
2844 /* the other ws is visible in another region, exchange them */
2845 other_r
->ws_prior
= new_ws
;
2846 other_r
->ws
= old_ws
;
2847 old_ws
->r
= other_r
;
2849 this_r
->ws_prior
= old_ws
;
2850 this_r
->ws
= new_ws
;
2853 /* this is needed so that we can click on a window after a restart */
2857 a
.id
= SWM_ARG_ID_FOCUSCUR
;
2858 focus(new_ws
->r
, &a
);
2860 /* unmap old windows */
2862 TAILQ_FOREACH(win
, &old_ws
->winlist
, entry
)
2867 DNPRINTF(SWM_D_WS
, "switchws: done\n");
2871 cyclews(struct swm_region
*r
, union arg
*args
)
2874 struct swm_screen
*s
= r
->s
;
2877 DNPRINTF(SWM_D_WS
, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2878 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
2883 case SWM_ARG_ID_CYCLEWS_UP_ALL
:
2886 case SWM_ARG_ID_CYCLEWS_UP
:
2887 if (a
.id
< workspace_limit
- 1)
2892 case SWM_ARG_ID_CYCLEWS_DOWN_ALL
:
2895 case SWM_ARG_ID_CYCLEWS_DOWN
:
2899 a
.id
= workspace_limit
- 1;
2906 (cycle_empty
== 0 && TAILQ_EMPTY(&s
->ws
[a
.id
].winlist
)))
2908 if (cycle_visible
== 0 && s
->ws
[a
.id
].r
!= NULL
)
2912 } while (a
.id
!= r
->ws
->idx
);
2916 priorws(struct swm_region
*r
, union arg
*args
)
2922 DNPRINTF(SWM_D_WS
, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2923 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
2925 if (r
->ws_prior
== NULL
)
2928 a
.id
= r
->ws_prior
->idx
;
2933 cyclescr(struct swm_region
*r
, union arg
*args
)
2935 struct swm_region
*rr
= NULL
;
2937 int i
, x
, y
, num_screens
;
2939 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2940 /* do nothing if we don't have more than one screen */
2941 if (!(num_screens
> 1 || outputs
> 1))
2946 case SWM_ARG_ID_CYCLESC_UP
:
2947 rr
= TAILQ_NEXT(r
, entry
);
2949 rr
= TAILQ_FIRST(&screens
[i
].rl
);
2951 case SWM_ARG_ID_CYCLESC_DOWN
:
2952 rr
= TAILQ_PREV(r
, swm_region_list
, entry
);
2954 rr
= TAILQ_LAST(&screens
[i
].rl
, swm_region_list
);
2962 /* move mouse to region */
2964 y
= Y(rr
) + 1 + (bar_enabled
? bar_height
: 0);
2965 xcb_warp_pointer(conn
, XCB_WINDOW_NONE
, rr
->s
[i
].root
, 0, 0, 0, 0,
2968 a
.id
= SWM_ARG_ID_FOCUSCUR
;
2971 if (rr
->ws
->focus
) {
2972 /* move to focus window */
2973 x
= X(rr
->ws
->focus
) + 1;
2974 y
= Y(rr
->ws
->focus
) + 1;
2975 xcb_warp_pointer(conn
, XCB_WINDOW_NONE
, rr
->s
[i
].root
, 0, 0, 0,
2981 sort_windows(struct ws_win_list
*wl
)
2983 struct ws_win
*win
, *parent
, *nxt
;
2988 for (win
= TAILQ_FIRST(wl
); win
!= TAILQ_END(wl
); win
= nxt
) {
2989 nxt
= TAILQ_NEXT(win
, entry
);
2990 if (win
->transient
) {
2991 parent
= find_window(win
->transient
);
2992 if (parent
== NULL
) {
2993 warnx("not possible bug");
2996 TAILQ_REMOVE(wl
, win
, entry
);
2997 TAILQ_INSERT_AFTER(wl
, parent
, win
, entry
);
3004 swapwin(struct swm_region
*r
, union arg
*args
)
3006 struct ws_win
*target
, *source
;
3007 struct ws_win
*cur_focus
;
3008 struct ws_win_list
*wl
;
3011 DNPRINTF(SWM_D_WS
, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3012 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
3014 cur_focus
= r
->ws
->focus
;
3015 if (cur_focus
== NULL
)
3019 wl
= &source
->ws
->winlist
;
3022 case SWM_ARG_ID_SWAPPREV
:
3023 if (source
->transient
)
3024 source
= find_window(source
->transient
);
3025 target
= TAILQ_PREV(source
, ws_win_list
, entry
);
3026 if (target
&& target
->transient
)
3027 target
= find_window(target
->transient
);
3028 TAILQ_REMOVE(wl
, source
, entry
);
3030 TAILQ_INSERT_TAIL(wl
, source
, entry
);
3032 TAILQ_INSERT_BEFORE(target
, source
, entry
);
3034 case SWM_ARG_ID_SWAPNEXT
:
3035 target
= TAILQ_NEXT(source
, entry
);
3036 /* move the parent and let the sort handle the move */
3037 if (source
->transient
)
3038 source
= find_window(source
->transient
);
3039 TAILQ_REMOVE(wl
, source
, entry
);
3041 TAILQ_INSERT_HEAD(wl
, source
, entry
);
3043 TAILQ_INSERT_AFTER(wl
, target
, source
, entry
);
3045 case SWM_ARG_ID_SWAPMAIN
:
3046 target
= TAILQ_FIRST(wl
);
3047 if (target
== source
) {
3048 if (source
->ws
->focus_prev
!= NULL
&&
3049 source
->ws
->focus_prev
!= target
)
3050 source
= source
->ws
->focus_prev
;
3054 if (target
== NULL
|| source
== NULL
)
3056 source
->ws
->focus_prev
= target
;
3057 TAILQ_REMOVE(wl
, target
, entry
);
3058 TAILQ_INSERT_BEFORE(source
, target
, entry
);
3059 TAILQ_REMOVE(wl
, source
, entry
);
3060 TAILQ_INSERT_HEAD(wl
, source
, entry
);
3062 case SWM_ARG_ID_MOVELAST
:
3063 TAILQ_REMOVE(wl
, source
, entry
);
3064 TAILQ_INSERT_TAIL(wl
, source
, entry
);
3067 DNPRINTF(SWM_D_MOVE
, "swapwin: invalid id: %d\n", args
->id
);
3079 get_focus_prev(struct ws_win
*win
)
3081 struct ws_win
*winfocus
= NULL
;
3082 struct ws_win
*cur_focus
= NULL
;
3083 struct ws_win_list
*wl
= NULL
;
3084 struct workspace
*ws
= NULL
;
3086 if (!(win
&& win
->ws
))
3091 cur_focus
= ws
->focus
;
3093 DNPRINTF(SWM_D_FOCUS
, "get_focus_prev: window: 0x%x, cur_focus: 0x%x\n",
3094 WINID(win
), WINID(cur_focus
));
3096 /* pickle, just focus on whatever */
3097 if (cur_focus
== NULL
) {
3098 /* use prev_focus if valid */
3099 if (ws
->focus_prev
&& ws
->focus_prev
!= cur_focus
&&
3100 find_window(WINID(ws
->focus_prev
)))
3101 winfocus
= ws
->focus_prev
;
3105 /* if transient focus on parent */
3106 if (cur_focus
->transient
) {
3107 winfocus
= find_window(cur_focus
->transient
);
3111 /* if in max_stack try harder */
3112 if ((win
->quirks
& SWM_Q_FOCUSPREV
) ||
3113 (ws
->cur_layout
->flags
& SWM_L_FOCUSPREV
)) {
3114 if (cur_focus
!= ws
->focus_prev
)
3115 winfocus
= ws
->focus_prev
;
3117 winfocus
= TAILQ_PREV(win
, ws_win_list
, entry
);
3122 DNPRINTF(SWM_D_FOCUS
, "get_focus_prev: focus_close: %d\n", focus_close
);
3124 if (winfocus
== NULL
|| winfocus
== win
) {
3125 switch (focus_close
) {
3126 case SWM_STACK_BOTTOM
:
3127 winfocus
= TAILQ_FIRST(wl
);
3130 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
3132 case SWM_STACK_ABOVE
:
3133 if ((winfocus
= TAILQ_NEXT(cur_focus
, entry
)) == NULL
) {
3134 if (focus_close_wrap
)
3135 winfocus
= TAILQ_FIRST(wl
);
3137 winfocus
= TAILQ_PREV(cur_focus
,
3138 ws_win_list
, entry
);
3141 case SWM_STACK_BELOW
:
3142 if ((winfocus
= TAILQ_PREV(cur_focus
, ws_win_list
,
3144 if (focus_close_wrap
)
3145 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
3147 winfocus
= TAILQ_NEXT(cur_focus
, entry
);
3153 if (winfocus
== NULL
) {
3154 if (focus_default
== SWM_STACK_TOP
)
3155 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
3157 winfocus
= TAILQ_FIRST(wl
);
3162 return get_focus_magic(winfocus
);
3166 focus(struct swm_region
*r
, union arg
*args
)
3168 struct ws_win
*winfocus
= NULL
, *head
;
3169 struct ws_win
*cur_focus
= NULL
;
3170 struct ws_win_list
*wl
= NULL
;
3171 struct workspace
*ws
= NULL
;
3177 DNPRINTF(SWM_D_FOCUS
, "focus: id: %d\n", args
->id
);
3179 /* treat FOCUS_CUR special */
3180 if (args
->id
== SWM_ARG_ID_FOCUSCUR
) {
3181 if (r
->ws
->focus
&& r
->ws
->focus
->iconic
== 0)
3182 winfocus
= r
->ws
->focus
;
3183 else if (r
->ws
->focus_prev
&& r
->ws
->focus_prev
->iconic
== 0)
3184 winfocus
= r
->ws
->focus_prev
;
3186 TAILQ_FOREACH(winfocus
, &r
->ws
->winlist
, entry
)
3187 if (winfocus
->iconic
== 0)
3190 focus_win(get_focus_magic(winfocus
));
3195 if ((cur_focus
= r
->ws
->focus
) == NULL
)
3199 if (TAILQ_EMPTY(wl
))
3201 /* make sure there is at least one uniconified window */
3203 TAILQ_FOREACH(winfocus
, wl
, entry
)
3204 if (winfocus
->iconic
== 0) {
3212 case SWM_ARG_ID_FOCUSPREV
:
3213 head
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
3215 head
= TAILQ_LAST(wl
, ws_win_list
);
3217 if (WINID(winfocus
) == cur_focus
->transient
) {
3218 head
= TAILQ_PREV(winfocus
, ws_win_list
, entry
);
3220 head
= TAILQ_LAST(wl
, ws_win_list
);
3225 if (winfocus
&& winfocus
->iconic
) {
3226 while (winfocus
!= cur_focus
) {
3227 if (winfocus
== NULL
)
3228 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
3229 if (winfocus
->iconic
== 0)
3231 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
,
3237 case SWM_ARG_ID_FOCUSNEXT
:
3238 head
= TAILQ_NEXT(cur_focus
, entry
);
3240 head
= TAILQ_FIRST(wl
);
3244 if (winfocus
&& winfocus
->iconic
) {
3245 while (winfocus
!= cur_focus
) {
3246 if (winfocus
== NULL
)
3247 winfocus
= TAILQ_FIRST(wl
);
3248 if (winfocus
->iconic
== 0)
3250 winfocus
= TAILQ_NEXT(winfocus
, entry
);
3255 case SWM_ARG_ID_FOCUSMAIN
:
3256 winfocus
= TAILQ_FIRST(wl
);
3257 if (winfocus
== cur_focus
)
3258 winfocus
= cur_focus
->ws
->focus_prev
;
3265 focus_win(get_focus_magic(winfocus
));
3271 cycle_layout(struct swm_region
*r
, union arg
*args
)
3273 struct workspace
*ws
= r
->ws
;
3276 /* suppress unused warning since var is needed */
3279 DNPRINTF(SWM_D_EVENT
, "cycle_layout: workspace: %d\n", ws
->idx
);
3282 if (ws
->cur_layout
->l_stack
== NULL
)
3283 ws
->cur_layout
= &layouts
[0];
3288 a
.id
= SWM_ARG_ID_FOCUSCUR
;
3295 stack_config(struct swm_region
*r
, union arg
*args
)
3297 struct workspace
*ws
= r
->ws
;
3299 DNPRINTF(SWM_D_STACK
, "stack_config: id: %d workspace: %d\n",
3302 if (ws
->cur_layout
->l_config
!= NULL
)
3303 ws
->cur_layout
->l_config(ws
, args
->id
);
3305 if (args
->id
!= SWM_ARG_ID_STACKINIT
)
3312 struct swm_geometry g
;
3313 struct swm_region
*r
;
3319 DNPRINTF(SWM_D_STACK
, "stack: begin\n");
3321 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
3322 for (i
= 0; i
< num_screens
; i
++) {
3326 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
3327 DNPRINTF(SWM_D_STACK
, "stack: workspace: %d "
3328 "(screen: %d, region: %d)\n", r
->ws
->idx
, i
, j
++);
3330 /* start with screen geometry, adjust for bar */
3332 g
.w
-= 2 * border_width
;
3333 g
.h
-= 2 * border_width
;
3339 r
->ws
->cur_layout
->l_stack(r
->ws
, &g
);
3340 r
->ws
->cur_layout
->l_string(r
->ws
);
3341 /* save r so we can track region changes */
3348 DNPRINTF(SWM_D_STACK
, "stack: end\n");
3352 store_float_geom(struct ws_win
*win
, struct swm_region
*r
)
3354 if (win
== NULL
|| r
== NULL
)
3357 /* retain window geom and region geom */
3358 win
->g_float
= win
->g
;
3359 win
->g_float
.x
-= X(r
);
3360 win
->g_float
.y
-= Y(r
);
3361 win
->g_floatvalid
= 1;
3362 DNPRINTF(SWM_D_MISC
, "store_float_geom: window: 0x%x, g: (%d,%d)"
3363 " %d x %d, g_float: (%d,%d) %d x %d\n", win
->id
, X(win
), Y(win
),
3364 WIDTH(win
), HEIGHT(win
), win
->g_float
.x
, win
->g_float
.y
,
3365 win
->g_float
.w
, win
->g_float
.h
);
3369 load_float_geom(struct ws_win
*win
, struct swm_region
*r
)
3371 if (win
== NULL
|| r
== NULL
)
3374 if (win
->g_floatvalid
) {
3375 win
->g
= win
->g_float
;
3378 DNPRINTF(SWM_D_MISC
, "load_float_geom: window: 0x%x, g: (%d,%d)"
3379 "%d x %d\n", win
->id
, X(win
), Y(win
), WIDTH(win
),
3382 DNPRINTF(SWM_D_MISC
, "load_float_geom: window: 0x%x, g_float "
3383 "is not set.\n", win
->id
);
3388 stack_floater(struct ws_win
*win
, struct swm_region
*r
)
3393 DNPRINTF(SWM_D_MISC
, "stack_floater: window: 0x%x\n", win
->id
);
3396 * to allow windows to change their size (e.g. mplayer fs) only retrieve
3397 * geom on ws switches or return from max mode
3399 if (win
->floatmaxed
|| (r
!= r
->ws
->old_r
&&
3400 !(win
->ewmh_flags
& EWMH_F_FULLSCREEN
))) {
3401 /* update geometry for the new region */
3402 load_float_geom(win
, r
);
3405 win
->floatmaxed
= 0;
3408 * if set to fullscreen mode, configure window to maximum size.
3410 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
3411 if (!win
->g_floatvalid
)
3412 store_float_geom(win
, win
->ws
->r
);
3418 * remove border on fullscreen floater when in fullscreen mode or when
3419 * the quirk is present.
3421 if ((win
->ewmh_flags
& EWMH_F_FULLSCREEN
) ||
3422 ((win
->quirks
& SWM_Q_FULLSCREEN
) &&
3423 (WIDTH(win
) >= WIDTH(r
)) && (HEIGHT(win
) >= HEIGHT(r
)))) {
3424 if (win
->bordered
) {
3426 X(win
) += border_width
;
3427 Y(win
) += border_width
;
3429 } else if (!win
->bordered
) {
3431 X(win
) -= border_width
;
3432 Y(win
) -= border_width
;
3435 if (win
->transient
&& (win
->quirks
& SWM_Q_TRANSSZ
)) {
3436 WIDTH(win
) = (double)WIDTH(r
) * dialog_ratio
;
3437 HEIGHT(win
) = (double)HEIGHT(r
) * dialog_ratio
;
3440 if (!win
->manual
&& !(win
->ewmh_flags
& EWMH_F_FULLSCREEN
) &&
3441 !(win
->quirks
& SWM_Q_ANYWHERE
)) {
3443 * floaters and transients are auto-centred unless moved,
3444 * resized or ANYWHERE quirk is set.
3446 X(win
) = X(r
) + (WIDTH(r
) - WIDTH(win
)) / 2 - BORDER(win
);
3447 Y(win
) = Y(r
) + (HEIGHT(r
) - HEIGHT(win
)) / 2 - BORDER(win
);
3449 store_float_geom(win
, r
);
3452 /* keep window within region bounds */
3453 constrain_window(win
, r
, 0);
3459 * Send keystrokes to terminal to decrease/increase the font size as the
3460 * window size changes.
3463 adjust_font(struct ws_win
*win
)
3465 if (!(win
->quirks
& SWM_Q_XTERM_FONTADJ
) ||
3466 win
->floating
|| win
->transient
)
3469 if (win
->sh
.width_inc
&& win
->last_inc
!= win
->sh
.width_inc
&&
3470 WIDTH(win
) / win
->sh
.width_inc
< term_width
&&
3471 win
->font_steps
< SWM_MAX_FONT_STEPS
) {
3472 win
->font_size_boundary
[win
->font_steps
] =
3473 (win
->sh
.width_inc
* term_width
) + win
->sh
.base_width
;
3476 win
->last_inc
= win
->sh
.width_inc
;
3477 fake_keypress(win
, XK_KP_Subtract
, XCB_MOD_MASK_SHIFT
);
3478 } else if (win
->font_steps
&& win
->last_inc
!= win
->sh
.width_inc
&&
3479 WIDTH(win
) > win
->font_size_boundary
[win
->font_steps
- 1]) {
3482 win
->last_inc
= win
->sh
.width_inc
;
3483 fake_keypress(win
, XK_KP_Add
, XCB_MOD_MASK_SHIFT
);
3487 #define SWAPXY(g) do { \
3489 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
3490 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
3493 stack_master(struct workspace
*ws
, struct swm_geometry
*g
, int rot
, int flip
)
3495 struct swm_geometry win_g
, r_g
= *g
;
3496 struct ws_win
*win
, *fs_win
= NULL
;
3497 int i
, j
, s
, stacks
;
3498 int w_inc
= 1, h_inc
, w_base
= 1, h_base
;
3499 int hrh
, extra
= 0, h_slice
, last_h
= 0;
3500 int split
, colno
, winno
, mwin
, msize
, mscale
;
3501 int remain
, missing
, v_slice
, reconfigure
;
3503 xcb_get_window_attributes_reply_t
*war
;
3505 DNPRINTF(SWM_D_STACK
, "stack_master: workspace: %d, rot: %s, "
3506 "flip: %s\n", ws
->idx
, YESNO(rot
), YESNO(flip
));
3508 winno
= count_win(ws
, 0);
3509 if (winno
== 0 && count_win(ws
, 1) == 0)
3512 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
3513 if (win
->transient
== 0 && win
->floating
== 0
3514 && win
->iconic
== 0)
3521 w_inc
= win
->sh
.width_inc
;
3522 w_base
= win
->sh
.base_width
;
3523 mwin
= ws
->l_state
.horizontal_mwin
;
3524 mscale
= ws
->l_state
.horizontal_msize
;
3525 stacks
= ws
->l_state
.horizontal_stacks
;
3528 w_inc
= win
->sh
.height_inc
;
3529 w_base
= win
->sh
.base_height
;
3530 mwin
= ws
->l_state
.vertical_mwin
;
3531 mscale
= ws
->l_state
.vertical_msize
;
3532 stacks
= ws
->l_state
.vertical_stacks
;
3536 if (stacks
> winno
- mwin
)
3537 stacks
= winno
- mwin
;
3541 h_slice
= r_g
.h
/ SWM_H_SLICE
;
3542 if (mwin
&& winno
> mwin
) {
3543 v_slice
= r_g
.w
/ SWM_V_SLICE
;
3547 win_g
.w
= v_slice
* mscale
;
3549 if (w_inc
> 1 && w_inc
< v_slice
) {
3550 /* adjust for window's requested size increment */
3551 remain
= (win_g
.w
- w_base
) % w_inc
;
3557 win_g
.x
+= r_g
.w
- msize
;
3560 colno
= split
= winno
/ stacks
;
3561 win_g
.w
= ((r_g
.w
- (stacks
* 2 * border_width
) +
3562 2 * border_width
) / stacks
);
3564 hrh
= r_g
.h
/ colno
;
3565 extra
= r_g
.h
- (colno
* hrh
);
3566 win_g
.h
= hrh
- 2 * border_width
;
3568 /* stack all the tiled windows */
3569 i
= j
= 0, s
= stacks
;
3570 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3571 if (win
->transient
!= 0 || win
->floating
!= 0)
3573 if (win
->iconic
!= 0)
3576 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
3581 if (split
&& i
== split
) {
3582 colno
= (winno
- mwin
) / stacks
;
3583 if (s
<= (winno
- mwin
) % stacks
)
3585 split
= split
+ colno
;
3586 hrh
= (r_g
.h
/ colno
);
3587 extra
= r_g
.h
- (colno
* hrh
);
3591 win_g
.x
+= win_g
.w
+ 2 * border_width
;
3592 win_g
.w
= (r_g
.w
- msize
-
3593 (stacks
* 2 * border_width
)) / stacks
;
3595 win_g
.w
+= (r_g
.w
- msize
-
3596 (stacks
* 2 * border_width
)) % stacks
;
3600 win_g
.h
= hrh
- 2 * border_width
;
3602 h_inc
= win
->sh
.width_inc
;
3603 h_base
= win
->sh
.base_width
;
3605 h_inc
= win
->sh
.height_inc
;
3606 h_base
= win
->sh
.base_height
;
3608 if (j
== colno
- 1) {
3609 win_g
.h
= hrh
+ extra
;
3610 } else if (h_inc
> 1 && h_inc
< h_slice
) {
3611 /* adjust for window's requested size increment */
3612 remain
= (win_g
.h
- h_base
) % h_inc
;
3613 missing
= h_inc
- remain
;
3615 if (missing
<= extra
|| j
== 0) {
3627 win_g
.y
+= last_h
+ 2 * border_width
;
3629 if (disable_border
&& bar_enabled
== 0 && winno
== 1){
3631 win_g
.w
+= 2 * border_width
;
3632 win_g
.h
+= 2 * border_width
;
3637 if (X(win
) != win_g
.y
|| Y(win
) != win_g
.x
||
3638 WIDTH(win
) != win_g
.h
|| HEIGHT(win
) != win_g
.w
) {
3642 WIDTH(win
) = win_g
.h
;
3643 HEIGHT(win
) = win_g
.w
;
3646 if (X(win
) != win_g
.x
|| Y(win
) != win_g
.y
||
3647 WIDTH(win
) != win_g
.w
|| HEIGHT(win
) != win_g
.h
) {
3651 WIDTH(win
) = win_g
.w
;
3652 HEIGHT(win
) = win_g
.h
;
3656 if (bordered
!= win
->bordered
) {
3658 win
->bordered
= bordered
;
3666 war
= xcb_get_window_attributes_reply(conn
,
3667 xcb_get_window_attributes(conn
, win
->id
),
3670 if (war
->map_state
== XCB_MAP_STATE_UNMAPPED
)
3671 map_window_raised(win
->id
);
3681 /* now, stack all the floaters and transients */
3682 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3683 if (win
->transient
== 0 && win
->floating
== 0)
3685 if (win
->iconic
== 1)
3687 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
3692 stack_floater(win
, ws
->r
);
3693 map_window_raised(win
->id
);
3697 stack_floater(fs_win
, ws
->r
);
3698 map_window_raised(fs_win
->id
);
3703 vertical_config(struct workspace
*ws
, int id
)
3705 DNPRINTF(SWM_D_STACK
, "vertical_config: id: %d, workspace: %d\n",
3709 case SWM_ARG_ID_STACKRESET
:
3710 case SWM_ARG_ID_STACKINIT
:
3711 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ 2;
3712 ws
->l_state
.vertical_mwin
= 1;
3713 ws
->l_state
.vertical_stacks
= 1;
3715 case SWM_ARG_ID_MASTERSHRINK
:
3716 if (ws
->l_state
.vertical_msize
> 1)
3717 ws
->l_state
.vertical_msize
--;
3719 case SWM_ARG_ID_MASTERGROW
:
3720 if (ws
->l_state
.vertical_msize
< SWM_V_SLICE
- 1)
3721 ws
->l_state
.vertical_msize
++;
3723 case SWM_ARG_ID_MASTERADD
:
3724 ws
->l_state
.vertical_mwin
++;
3726 case SWM_ARG_ID_MASTERDEL
:
3727 if (ws
->l_state
.vertical_mwin
> 0)
3728 ws
->l_state
.vertical_mwin
--;
3730 case SWM_ARG_ID_STACKINC
:
3731 ws
->l_state
.vertical_stacks
++;
3733 case SWM_ARG_ID_STACKDEC
:
3734 if (ws
->l_state
.vertical_stacks
> 1)
3735 ws
->l_state
.vertical_stacks
--;
3737 case SWM_ARG_ID_FLIPLAYOUT
:
3738 ws
->l_state
.vertical_flip
= !ws
->l_state
.vertical_flip
;
3746 vertical_stack(struct workspace
*ws
, struct swm_geometry
*g
)
3748 DNPRINTF(SWM_D_STACK
, "vertical_stack: workspace: %d\n", ws
->idx
);
3750 stack_master(ws
, g
, 0, ws
->l_state
.vertical_flip
);
3754 horizontal_config(struct workspace
*ws
, int id
)
3756 DNPRINTF(SWM_D_STACK
, "horizontal_config: workspace: %d\n", ws
->idx
);
3759 case SWM_ARG_ID_STACKRESET
:
3760 case SWM_ARG_ID_STACKINIT
:
3761 ws
->l_state
.horizontal_mwin
= 1;
3762 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ 2;
3763 ws
->l_state
.horizontal_stacks
= 1;
3765 case SWM_ARG_ID_MASTERSHRINK
:
3766 if (ws
->l_state
.horizontal_msize
> 1)
3767 ws
->l_state
.horizontal_msize
--;
3769 case SWM_ARG_ID_MASTERGROW
:
3770 if (ws
->l_state
.horizontal_msize
< SWM_H_SLICE
- 1)
3771 ws
->l_state
.horizontal_msize
++;
3773 case SWM_ARG_ID_MASTERADD
:
3774 ws
->l_state
.horizontal_mwin
++;
3776 case SWM_ARG_ID_MASTERDEL
:
3777 if (ws
->l_state
.horizontal_mwin
> 0)
3778 ws
->l_state
.horizontal_mwin
--;
3780 case SWM_ARG_ID_STACKINC
:
3781 ws
->l_state
.horizontal_stacks
++;
3783 case SWM_ARG_ID_STACKDEC
:
3784 if (ws
->l_state
.horizontal_stacks
> 1)
3785 ws
->l_state
.horizontal_stacks
--;
3787 case SWM_ARG_ID_FLIPLAYOUT
:
3788 ws
->l_state
.horizontal_flip
= !ws
->l_state
.horizontal_flip
;
3796 horizontal_stack(struct workspace
*ws
, struct swm_geometry
*g
)
3798 DNPRINTF(SWM_D_STACK
, "horizontal_stack: workspace: %d\n", ws
->idx
);
3800 stack_master(ws
, g
, 1, ws
->l_state
.horizontal_flip
);
3803 /* fullscreen view */
3805 max_stack(struct workspace
*ws
, struct swm_geometry
*g
)
3807 struct swm_geometry gg
= *g
;
3808 struct ws_win
*win
, *wintrans
= NULL
, *parent
= NULL
;
3809 int winno
, num_screens
;
3811 DNPRINTF(SWM_D_STACK
, "max_stack: workspace: %d\n", ws
->idx
);
3816 winno
= count_win(ws
, 0);
3817 if (winno
== 0 && count_win(ws
, 1) == 0)
3820 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
3821 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3822 if (win
->transient
) {
3824 parent
= find_window(win
->transient
);
3828 if (win
->floating
&& win
->floatmaxed
== 0 ) {
3830 * retain geometry for retrieval on exit from
3833 store_float_geom(win
, ws
->r
);
3834 win
->floatmaxed
= 1;
3837 /* only reconfigure if necessary */
3838 if (X(win
) != gg
.x
|| Y(win
) != gg
.y
|| WIDTH(win
) != gg
.w
||
3839 HEIGHT(win
) != gg
.h
) {
3845 WIDTH(win
) += 2 * border_width
;
3846 HEIGHT(win
) += 2 * border_width
;
3851 /* unmap only if we don't have multi screen */
3852 if (win
!= ws
->focus
)
3853 if (!(num_screens
> 1 || outputs
> 1))
3857 /* put the last transient on top */
3860 map_window_raised(parent
->id
);
3861 stack_floater(wintrans
, ws
->r
);
3862 focus_win(get_focus_magic(wintrans
));
3867 send_to_ws(struct swm_region
*r
, union arg
*args
)
3869 int wsid
= args
->id
;
3870 struct ws_win
*win
= NULL
, *parent
;
3871 struct workspace
*ws
, *nws
;
3872 char ws_idx_str
[SWM_PROPLEN
];
3875 if (wsid
>= workspace_limit
)
3878 if (r
&& r
->ws
&& r
->ws
->focus
)
3884 if (win
->ws
->idx
== wsid
)
3887 DNPRINTF(SWM_D_MOVE
, "send_to_ws: window: 0x%x\n", win
->id
);
3890 nws
= &win
->s
->ws
[wsid
];
3892 a
.id
= SWM_ARG_ID_FOCUSPREV
;
3894 if (win
->transient
) {
3895 parent
= find_window(win
->transient
);
3897 unmap_window(parent
);
3898 TAILQ_REMOVE(&ws
->winlist
, parent
, entry
);
3899 TAILQ_INSERT_TAIL(&nws
->winlist
, parent
, entry
);
3904 TAILQ_REMOVE(&ws
->winlist
, win
, entry
);
3905 TAILQ_INSERT_TAIL(&nws
->winlist
, win
, entry
);
3906 if (TAILQ_EMPTY(&ws
->winlist
))
3907 r
->ws
->focus
= NULL
;
3910 /* Try to update the window's workspace property */
3911 if (snprintf(ws_idx_str
, SWM_PROPLEN
, "%d", nws
->idx
) < SWM_PROPLEN
) {
3912 DNPRINTF(SWM_D_PROP
, "send_to_ws: set property: _SWM_WS: %s\n",
3914 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
3915 a_swm_ws
, XCB_ATOM_STRING
, 8, strlen(ws_idx_str
),
3926 pressbutton(struct swm_region
*r
, union arg
*args
)
3928 /* suppress unused warning since var is needed */
3931 xcb_test_fake_input(conn
, XCB_BUTTON_PRESS
, args
->id
,
3932 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
3933 xcb_test_fake_input(conn
, XCB_BUTTON_RELEASE
, args
->id
,
3934 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
3938 raise_toggle(struct swm_region
*r
, union arg
*args
)
3940 /* suppress unused warning since var is needed */
3943 if (r
== NULL
|| r
->ws
== NULL
)
3946 r
->ws
->always_raise
= !r
->ws
->always_raise
;
3948 /* bring floaters back to top */
3949 if (r
->ws
->always_raise
== 0)
3956 iconify(struct swm_region
*r
, union arg
*args
)
3960 /* suppress unused warning since var is needed */
3963 if (r
->ws
->focus
== NULL
)
3965 unmap_window(r
->ws
->focus
);
3966 update_iconic(r
->ws
->focus
, 1);
3969 r
->ws
->focus
= NULL
;
3970 a
.id
= SWM_ARG_ID_FOCUSCUR
;
3977 get_win_name(xcb_window_t win
)
3980 xcb_get_property_cookie_t c
;
3981 xcb_get_property_reply_t
*r
;
3983 /* First try _NET_WM_NAME for UTF-8. */
3984 c
= xcb_get_property(conn
, 0, win
, a_netwmname
,
3985 XCB_GET_PROPERTY_TYPE_ANY
, 0, UINT_MAX
);
3986 r
= xcb_get_property_reply(conn
, c
, NULL
);
3988 if (!r
|| r
->type
== XCB_NONE
) {
3990 /* Use WM_NAME instead; no UTF-8. */
3991 c
= xcb_get_property(conn
, 0, win
, XCB_ATOM_WM_NAME
,
3992 XCB_GET_PROPERTY_TYPE_ANY
, 0, UINT_MAX
);
3993 r
= xcb_get_property_reply(conn
, c
, NULL
);
3995 if(!r
|| r
->type
== XCB_NONE
) {
4002 name
= strndup(xcb_get_property_value(r
),
4003 xcb_get_property_value_length(r
));
4010 uniconify(struct swm_region
*r
, union arg
*args
)
4017 DNPRINTF(SWM_D_MISC
, "uniconify\n");
4019 if (r
== NULL
|| r
->ws
== NULL
)
4022 /* make sure we have anything to uniconify */
4023 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
4024 if (win
->ws
== NULL
)
4025 continue; /* should never happen */
4026 if (win
->iconic
== 0)
4034 search_resp_action
= SWM_SEARCH_UNICONIFY
;
4036 spawn_select(r
, args
, "search", &searchpid
);
4038 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
4041 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
4042 if (win
->ws
== NULL
)
4043 continue; /* should never happen */
4044 if (win
->iconic
== 0)
4047 name
= get_win_name(win
->id
);
4050 fprintf(lfile
, "%s.%u\n", name
, win
->id
);
4060 name_workspace(struct swm_region
*r
, union arg
*args
)
4064 DNPRINTF(SWM_D_MISC
, "name_workspace\n");
4070 search_resp_action
= SWM_SEARCH_NAME_WORKSPACE
;
4072 spawn_select(r
, args
, "name_workspace", &searchpid
);
4074 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
4077 fprintf(lfile
, "%s", "");
4082 search_workspace(struct swm_region
*r
, union arg
*args
)
4085 struct workspace
*ws
;
4088 DNPRINTF(SWM_D_MISC
, "search_workspace\n");
4094 search_resp_action
= SWM_SEARCH_SEARCH_WORKSPACE
;
4096 spawn_select(r
, args
, "search", &searchpid
);
4098 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
4101 for (i
= 0; i
< workspace_limit
; i
++) {
4105 fprintf(lfile
, "%d%s%s\n", ws
->idx
+ 1,
4106 (ws
->name
? ":" : ""), (ws
->name
? ws
->name
: ""));
4113 search_win_cleanup(void)
4115 struct search_window
*sw
= NULL
;
4117 while ((sw
= TAILQ_FIRST(&search_wl
)) != NULL
) {
4118 xcb_destroy_window(conn
, sw
->indicator
);
4119 TAILQ_REMOVE(&search_wl
, sw
, entry
);
4125 search_win(struct swm_region
*r
, union arg
*args
)
4127 struct ws_win
*win
= NULL
;
4128 struct search_window
*sw
= NULL
;
4131 int i
, width
, height
;
4139 XRectangle l_ibox
, l_lbox
;
4141 DNPRINTF(SWM_D_MISC
, "search_win\n");
4144 search_resp_action
= SWM_SEARCH_SEARCH_WINDOW
;
4146 spawn_select(r
, args
, "search", &searchpid
);
4148 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
4151 TAILQ_INIT(&search_wl
);
4154 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
4155 if (win
->iconic
== 1)
4158 sw
= calloc(1, sizeof(struct search_window
));
4160 warn("search_win: calloc");
4162 search_win_cleanup();
4168 snprintf(s
, sizeof s
, "%d", i
);
4171 w
= xcb_generate_id(conn
);
4172 wa
[0] = r
->s
->c
[SWM_S_COLOR_FOCUS
].pixel
;
4173 wa
[1] = r
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
;
4175 if (bar_font_legacy
) {
4176 XmbTextExtents(bar_fs
, s
, len
, &l_ibox
, &l_lbox
);
4177 width
= l_lbox
.width
+ 4;
4178 height
= bar_fs_extents
->max_logical_extent
.height
+ 4;
4180 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
,
4182 width
= info
.width
+ 4;
4183 height
= bar_font
->height
+ 4;
4186 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, w
, win
->id
, 0, 0,
4187 width
, height
, 1, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
4188 XCB_COPY_FROM_PARENT
, XCB_CW_BACK_PIXEL
|
4189 XCB_CW_BORDER_PIXEL
, wa
);
4191 map_window_raised(w
);
4194 TAILQ_INSERT_TAIL(&search_wl
, sw
, entry
);
4196 if (bar_font_legacy
) {
4197 l_gcv
.graphics_exposures
= 0;
4198 l_draw
= XCreateGC(display
, w
, 0, &l_gcv
);
4200 XSetForeground(display
, l_draw
,
4201 r
->s
->c
[SWM_S_COLOR_BAR
].pixel
);
4203 DRAWSTRING(display
, w
, bar_fs
, l_draw
, 2,
4204 (bar_fs_extents
->max_logical_extent
.height
-
4205 l_lbox
.height
) / 2 - l_lbox
.y
, s
, len
);
4207 XFreeGC(display
, l_draw
);
4210 draw
= XftDrawCreate(display
, w
,
4211 DefaultVisual(display
, r
->s
->idx
),
4212 DefaultColormap(display
, r
->s
->idx
));
4214 XftDrawStringUtf8(draw
, &bar_font_color
, bar_font
, 2,
4215 (HEIGHT(r
->bar
) + bar_font
->height
) / 2 -
4216 bar_font
->descent
, (FcChar8
*)s
, len
);
4218 XftDrawDestroy(draw
);
4221 DNPRINTF(SWM_D_MISC
, "search_win: mapped window: 0x%x\n", w
);
4223 fprintf(lfile
, "%d\n", i
);
4233 search_resp_uniconify(char *resp
, unsigned long len
)
4239 DNPRINTF(SWM_D_MISC
, "search_resp_uniconify: resp: %s\n", resp
);
4241 TAILQ_FOREACH(win
, &search_r
->ws
->winlist
, entry
) {
4242 if (win
->iconic
== 0)
4244 name
= get_win_name(win
->id
);
4247 if (asprintf(&s
, "%s.%u", name
, win
->id
) == -1) {
4252 if (strncmp(s
, resp
, len
) == 0) {
4253 /* XXX this should be a callback to generalize */
4254 update_iconic(win
, 0);
4263 search_resp_name_workspace(char *resp
, unsigned long len
)
4265 struct workspace
*ws
;
4267 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: resp: %s\n", resp
);
4269 if (search_r
->ws
== NULL
)
4274 free(search_r
->ws
->name
);
4275 search_r
->ws
->name
= NULL
;
4279 ws
->name
= strdup(resp
);
4280 if (ws
->name
== NULL
) {
4281 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: "
4282 "strdup: %s", strerror(errno
));
4289 search_resp_search_workspace(char *resp
)
4296 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: resp: %s\n", resp
);
4300 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: strdup: %s",
4307 ws_idx
= (int)strtonum(q
, 1, workspace_limit
, &errstr
);
4309 DNPRINTF(SWM_D_MISC
, "workspace idx is %s: %s",
4316 switchws(search_r
, &a
);
4320 search_resp_search_window(char *resp
)
4325 struct search_window
*sw
;
4327 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: resp: %s\n", resp
);
4331 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: strdup: %s",
4336 idx
= (int)strtonum(s
, 1, INT_MAX
, &errstr
);
4338 DNPRINTF(SWM_D_MISC
, "window idx is %s: %s",
4345 TAILQ_FOREACH(sw
, &search_wl
, entry
)
4346 if (idx
== sw
->idx
) {
4352 #define MAX_RESP_LEN 1024
4355 search_do_resp(void)
4361 DNPRINTF(SWM_D_MISC
, "search_do_resp:\n");
4366 if ((resp
= calloc(1, MAX_RESP_LEN
+ 1)) == NULL
) {
4367 warn("search: calloc");
4371 rbytes
= read(select_resp_pipe
[0], resp
, MAX_RESP_LEN
);
4373 warn("search: read error");
4376 resp
[rbytes
] = '\0';
4379 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
4380 * newline, so work around that by sanitizing the resp now.
4382 resp
[strcspn(resp
, "\n")] = '\0';
4385 switch (search_resp_action
) {
4386 case SWM_SEARCH_UNICONIFY
:
4387 search_resp_uniconify(resp
, len
);
4389 case SWM_SEARCH_NAME_WORKSPACE
:
4390 search_resp_name_workspace(resp
, len
);
4392 case SWM_SEARCH_SEARCH_WORKSPACE
:
4393 search_resp_search_workspace(resp
);
4395 case SWM_SEARCH_SEARCH_WINDOW
:
4396 search_resp_search_window(resp
);
4401 if (search_resp_action
== SWM_SEARCH_SEARCH_WINDOW
)
4402 search_win_cleanup();
4404 search_resp_action
= SWM_SEARCH_NONE
;
4405 close(select_resp_pipe
[0]);
4410 wkill(struct swm_region
*r
, union arg
*args
)
4412 DNPRINTF(SWM_D_MISC
, "wkill: id: %d\n", args
->id
);
4414 if (r
->ws
->focus
== NULL
)
4417 if (args
->id
== SWM_ARG_ID_KILLWINDOW
)
4418 xcb_kill_client(conn
, r
->ws
->focus
->id
);
4420 if (r
->ws
->focus
->can_delete
)
4421 client_msg(r
->ws
->focus
, a_delete
);
4428 floating_toggle_win(struct ws_win
*win
)
4430 struct swm_region
*r
;
4440 /* reject floating toggles in max stack mode */
4441 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
4444 if (win
->floating
) {
4445 if (!win
->floatmaxed
) {
4446 /* retain position for refloat */
4447 store_float_geom(win
, r
);
4451 load_float_geom(win
, r
);
4455 ewmh_update_actions(win
);
4461 floating_toggle(struct swm_region
*r
, union arg
*args
)
4463 struct ws_win
*win
= r
->ws
->focus
;
4466 /* suppress unused warning since var is needed */
4472 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
4475 ewmh_update_win_state(win
, ewmh
[_NET_WM_STATE_ABOVE
].atom
,
4476 _NET_WM_STATE_TOGGLE
);
4480 if (win
== win
->ws
->focus
) {
4481 a
.id
= SWM_ARG_ID_FOCUSCUR
;
4482 focus(win
->ws
->r
, &a
);
4489 constrain_window(struct ws_win
*win
, struct swm_region
*r
, int resizable
)
4491 if (MAX_X(win
) + BORDER(win
) > MAX_X(r
)) {
4493 WIDTH(win
) = MAX_X(r
) - X(win
) - BORDER(win
);
4495 X(win
) = MAX_X(r
)- WIDTH(win
) - BORDER(win
);
4498 if (X(win
) + BORDER(win
) < X(r
)) {
4500 WIDTH(win
) -= X(r
) - X(win
) - BORDER(win
);
4502 X(win
) = X(r
) - BORDER(win
);
4505 if (MAX_Y(win
) + BORDER(win
) > MAX_Y(r
)) {
4507 HEIGHT(win
) = MAX_Y(r
) - Y(win
) - BORDER(win
);
4509 Y(win
) = MAX_Y(r
) - HEIGHT(win
) - BORDER(win
);
4512 if (Y(win
) + BORDER(win
) < Y(r
)) {
4514 HEIGHT(win
) -= Y(r
) - Y(win
) - BORDER(win
);
4516 Y(win
) = Y(r
) - BORDER(win
);
4522 if (HEIGHT(win
) < 1)
4528 update_window(struct ws_win
*win
)
4533 mask
= XCB_CONFIG_WINDOW_X
| XCB_CONFIG_WINDOW_Y
|
4534 XCB_CONFIG_WINDOW_WIDTH
| XCB_CONFIG_WINDOW_HEIGHT
|
4535 XCB_CONFIG_WINDOW_BORDER_WIDTH
;
4539 wc
[3] = HEIGHT(win
);
4540 wc
[4] = BORDER(win
);
4542 DNPRINTF(SWM_D_EVENT
, "update_window: window: 0x%x, (x,y) w x h: "
4543 "(%d,%d) %d x %d, bordered: %s\n", win
->id
, wc
[0], wc
[1], wc
[2],
4544 wc
[3], YESNO(win
->bordered
));
4546 xcb_configure_window(conn
, win
->id
, mask
, wc
);
4549 #define SWM_RESIZE_STEPS (50)
4552 resize(struct ws_win
*win
, union arg
*args
)
4554 xcb_timestamp_t timestamp
= 0;
4555 struct swm_region
*r
= NULL
;
4556 int resize_step
= 0;
4557 struct swm_geometry g
;
4558 int top
= 0, left
= 0, resizing
;
4560 unsigned int shape
; /* cursor style */
4561 xcb_cursor_t cursor
;
4562 xcb_font_t cursor_font
;
4563 xcb_query_pointer_reply_t
*xpr
;
4564 xcb_generic_event_t
*evt
;
4565 xcb_motion_notify_event_t
*mne
;
4571 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
4574 DNPRINTF(SWM_D_EVENT
, "resize: window: 0x%x, floating: %s, "
4575 "transient: 0x%x\n", win
->id
, YESNO(win
->floating
),
4578 if (!(win
->transient
!= 0 || win
->floating
!= 0))
4581 /* reject resizes in max mode for floaters (transient ok) */
4582 if (win
->floatmaxed
)
4586 ewmh_update_win_state(win
, ewmh
[_SWM_WM_STATE_MANUAL
].atom
,
4594 case SWM_ARG_ID_WIDTHSHRINK
:
4595 WIDTH(win
) -= SWM_RESIZE_STEPS
;
4598 case SWM_ARG_ID_WIDTHGROW
:
4599 WIDTH(win
) += SWM_RESIZE_STEPS
;
4602 case SWM_ARG_ID_HEIGHTSHRINK
:
4603 HEIGHT(win
) -= SWM_RESIZE_STEPS
;
4606 case SWM_ARG_ID_HEIGHTGROW
:
4607 HEIGHT(win
) += SWM_RESIZE_STEPS
;
4614 constrain_window(win
, r
, 1);
4616 store_float_geom(win
,r
);
4620 /* get cursor offset from window root */
4621 xpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
4628 if (xpr
->win_x
< WIDTH(win
) / 2)
4631 if (xpr
->win_y
< HEIGHT(win
) / 2)
4634 if (args
->id
== SWM_ARG_ID_CENTER
)
4637 shape
= (left
) ? XC_top_left_corner
: XC_top_right_corner
;
4639 shape
= (left
) ? XC_bottom_left_corner
: XC_bottom_right_corner
;
4641 cursor_font
= xcb_generate_id(conn
);
4642 xcb_open_font(conn
, cursor_font
, strlen("cursor"), "cursor");
4644 cursor
= xcb_generate_id(conn
);
4645 xcb_create_glyph_cursor(conn
, cursor
, cursor_font
, cursor_font
,
4646 shape
, shape
+ 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
4648 xcb_grab_pointer(conn
, 0, win
->id
, MOUSEMASK
,
4649 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
, XCB_WINDOW_NONE
, cursor
,
4654 while ((evt
= xcb_wait_for_event(conn
)) && resizing
) {
4655 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
4656 case XCB_BUTTON_RELEASE
:
4657 DNPRINTF(SWM_D_EVENT
, "resize: BUTTON_RELEASE\n");
4660 case XCB_MOTION_NOTIFY
:
4661 mne
= (xcb_motion_notify_event_t
*)evt
;
4662 /* cursor offset/delta from start of the operation */
4663 dx
= mne
->root_x
- xpr
->root_x
;
4664 dy
= mne
->root_y
- xpr
->root_y
;
4670 if (args
->id
== SWM_ARG_ID_CENTER
) {
4671 if (g
.h
/ 2 + dy
< 1)
4675 HEIGHT(win
) = g
.h
+ 2 * dy
;
4683 HEIGHT(win
) = g
.h
+ dy
;
4690 if (args
->id
== SWM_ARG_ID_CENTER
) {
4691 if (g
.w
/ 2 + dx
< 1)
4695 WIDTH(win
) = g
.w
+ 2 * dx
;
4703 WIDTH(win
) = g
.w
+ dx
;
4706 constrain_window(win
, r
, 1);
4708 /* not free, don't sync more than 120 times / second */
4709 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
4710 timestamp
= mne
->time
;
4725 store_float_geom(win
,r
);
4727 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
4728 xcb_free_cursor(conn
, cursor
);
4729 xcb_close_font(conn
, cursor_font
);
4731 DNPRINTF(SWM_D_EVENT
, "resize: done\n");
4735 resize_step(struct swm_region
*r
, union arg
*args
)
4737 struct ws_win
*win
= NULL
;
4739 if (r
&& r
->ws
&& r
->ws
->focus
)
4747 #define SWM_MOVE_STEPS (50)
4750 move(struct ws_win
*win
, union arg
*args
)
4752 xcb_timestamp_t timestamp
= 0;
4753 int move_step
= 0, moving
;
4754 struct swm_region
*r
= NULL
;
4755 xcb_font_t cursor_font
;
4756 xcb_cursor_t cursor
;
4757 xcb_query_pointer_reply_t
*qpr
;
4758 xcb_generic_event_t
*evt
;
4759 xcb_motion_notify_event_t
*mne
;
4765 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
4768 DNPRINTF(SWM_D_EVENT
, "move: window: 0x%x, floating: %s, transient: "
4769 "0x%x\n", win
->id
, YESNO(win
->floating
), win
->transient
);
4771 /* in max_stack mode should only move transients */
4772 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
] && !win
->transient
)
4776 if (win
->floating
== 0 && !win
->transient
) {
4777 store_float_geom(win
, r
);
4778 ewmh_update_win_state(win
, ewmh
[_NET_WM_STATE_ABOVE
].atom
,
4781 ewmh_update_win_state(win
, ewmh
[_SWM_WM_STATE_MANUAL
].atom
,
4790 case SWM_ARG_ID_MOVELEFT
:
4791 X(win
) -= (SWM_MOVE_STEPS
- border_width
);
4794 case SWM_ARG_ID_MOVERIGHT
:
4795 X(win
) += (SWM_MOVE_STEPS
- border_width
);
4798 case SWM_ARG_ID_MOVEUP
:
4799 Y(win
) -= (SWM_MOVE_STEPS
- border_width
);
4802 case SWM_ARG_ID_MOVEDOWN
:
4803 Y(win
) += (SWM_MOVE_STEPS
- border_width
);
4810 constrain_window(win
, r
, 0);
4812 store_float_geom(win
, r
);
4816 cursor_font
= xcb_generate_id(conn
);
4817 xcb_open_font(conn
, cursor_font
, strlen("cursor"), "cursor");
4819 cursor
= xcb_generate_id(conn
);
4820 xcb_create_glyph_cursor(conn
, cursor
, cursor_font
, cursor_font
,
4821 XC_fleur
, XC_fleur
+ 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
4823 xcb_grab_pointer(conn
, 0, win
->id
, MOUSEMASK
,
4824 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
,
4825 XCB_WINDOW_NONE
, cursor
, XCB_CURRENT_TIME
);
4827 /* get cursor offset from window root */
4828 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
4831 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
4832 xcb_free_cursor(conn
, cursor
);
4833 xcb_close_font(conn
, cursor_font
);
4839 while ((evt
= xcb_wait_for_event(conn
)) && moving
) {
4840 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
4841 case XCB_BUTTON_RELEASE
:
4842 DNPRINTF(SWM_D_EVENT
, "move: BUTTON_RELEASE\n");
4845 case XCB_MOTION_NOTIFY
:
4846 mne
= (xcb_motion_notify_event_t
*)evt
;
4847 X(win
) = mne
->root_x
- qpr
->win_x
- border_width
;
4848 Y(win
) = mne
->root_y
- qpr
->win_y
- border_width
;
4850 constrain_window(win
, r
, 0);
4852 /* not free, don't sync more than 120 times / second */
4853 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
4854 timestamp
= mne
->time
;
4869 store_float_geom(win
, r
);
4871 xcb_free_cursor(conn
, cursor
);
4872 xcb_close_font(conn
, cursor_font
);
4873 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
4874 DNPRINTF(SWM_D_EVENT
, "move: done\n");
4878 move_step(struct swm_region
*r
, union arg
*args
)
4880 struct ws_win
*win
= NULL
;
4882 if (r
&& r
->ws
&& r
->ws
->focus
)
4887 if (!(win
->transient
!= 0 || win
->floating
!= 0))
4894 /* user/key callable function IDs */
4944 KF_SEARCH_WORKSPACE
,
4985 KF_DUMPWINS
, /* MUST BE LAST */
4989 /* key definitions */
4991 char name
[SWM_FUNCNAME_LEN
];
4992 void (*func
)(struct swm_region
*r
, union arg
*);
4994 } keyfuncs
[KF_INVALID
+ 1] = {
4995 /* name function argument */
4996 { "bar_toggle", bar_toggle
, {0} },
4997 { "button2", pressbutton
, {2} },
4998 { "cycle_layout", cycle_layout
, {0} },
4999 { "flip_layout", stack_config
, {.id
= SWM_ARG_ID_FLIPLAYOUT
} },
5000 { "float_toggle", floating_toggle
,{0} },
5001 { "focus_main", focus
, {.id
= SWM_ARG_ID_FOCUSMAIN
} },
5002 { "focus_next", focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
5003 { "focus_prev", focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
5004 { "height_grow", resize_step
, {.id
= SWM_ARG_ID_HEIGHTGROW
} },
5005 { "height_shrink", resize_step
, {.id
= SWM_ARG_ID_HEIGHTSHRINK
} },
5006 { "iconify", iconify
, {0} },
5007 { "master_shrink", stack_config
, {.id
= SWM_ARG_ID_MASTERSHRINK
} },
5008 { "master_grow", stack_config
, {.id
= SWM_ARG_ID_MASTERGROW
} },
5009 { "master_add", stack_config
, {.id
= SWM_ARG_ID_MASTERADD
} },
5010 { "master_del", stack_config
, {.id
= SWM_ARG_ID_MASTERDEL
} },
5011 { "move_down", move_step
, {.id
= SWM_ARG_ID_MOVEDOWN
} },
5012 { "move_left", move_step
, {.id
= SWM_ARG_ID_MOVELEFT
} },
5013 { "move_right", move_step
, {.id
= SWM_ARG_ID_MOVERIGHT
} },
5014 { "move_up", move_step
, {.id
= SWM_ARG_ID_MOVEUP
} },
5015 { "mvws_1", send_to_ws
, {.id
= 0} },
5016 { "mvws_2", send_to_ws
, {.id
= 1} },
5017 { "mvws_3", send_to_ws
, {.id
= 2} },
5018 { "mvws_4", send_to_ws
, {.id
= 3} },
5019 { "mvws_5", send_to_ws
, {.id
= 4} },
5020 { "mvws_6", send_to_ws
, {.id
= 5} },
5021 { "mvws_7", send_to_ws
, {.id
= 6} },
5022 { "mvws_8", send_to_ws
, {.id
= 7} },
5023 { "mvws_9", send_to_ws
, {.id
= 8} },
5024 { "mvws_10", send_to_ws
, {.id
= 9} },
5025 { "mvws_11", send_to_ws
, {.id
= 10} },
5026 { "mvws_12", send_to_ws
, {.id
= 11} },
5027 { "mvws_13", send_to_ws
, {.id
= 12} },
5028 { "mvws_14", send_to_ws
, {.id
= 13} },
5029 { "mvws_15", send_to_ws
, {.id
= 14} },
5030 { "mvws_16", send_to_ws
, {.id
= 15} },
5031 { "mvws_17", send_to_ws
, {.id
= 16} },
5032 { "mvws_18", send_to_ws
, {.id
= 17} },
5033 { "mvws_19", send_to_ws
, {.id
= 18} },
5034 { "mvws_20", send_to_ws
, {.id
= 19} },
5035 { "mvws_21", send_to_ws
, {.id
= 20} },
5036 { "mvws_22", send_to_ws
, {.id
= 21} },
5037 { "name_workspace", name_workspace
, {0} },
5038 { "quit", quit
, {0} },
5039 { "raise_toggle", raise_toggle
, {0} },
5040 { "restart", restart
, {0} },
5041 { "screen_next", cyclescr
, {.id
= SWM_ARG_ID_CYCLESC_UP
} },
5042 { "screen_prev", cyclescr
, {.id
= SWM_ARG_ID_CYCLESC_DOWN
} },
5043 { "search_win", search_win
, {0} },
5044 { "search_workspace", search_workspace
, {0} },
5045 { "spawn_custom", NULL
, {0} },
5046 { "stack_inc", stack_config
, {.id
= SWM_ARG_ID_STACKINC
} },
5047 { "stack_dec", stack_config
, {.id
= SWM_ARG_ID_STACKDEC
} },
5048 { "stack_reset", stack_config
, {.id
= SWM_ARG_ID_STACKRESET
} },
5049 { "swap_main", swapwin
, {.id
= SWM_ARG_ID_SWAPMAIN
} },
5050 { "swap_next", swapwin
, {.id
= SWM_ARG_ID_SWAPNEXT
} },
5051 { "swap_prev", swapwin
, {.id
= SWM_ARG_ID_SWAPPREV
} },
5052 { "uniconify", uniconify
, {0} },
5053 { "version", version
, {0} },
5054 { "width_grow", resize_step
, {.id
= SWM_ARG_ID_WIDTHGROW
} },
5055 { "width_shrink", resize_step
, {.id
= SWM_ARG_ID_WIDTHSHRINK
} },
5056 { "wind_del", wkill
, {.id
= SWM_ARG_ID_DELETEWINDOW
} },
5057 { "wind_kill", wkill
, {.id
= SWM_ARG_ID_KILLWINDOW
} },
5058 { "ws_1", switchws
, {.id
= 0} },
5059 { "ws_2", switchws
, {.id
= 1} },
5060 { "ws_3", switchws
, {.id
= 2} },
5061 { "ws_4", switchws
, {.id
= 3} },
5062 { "ws_5", switchws
, {.id
= 4} },
5063 { "ws_6", switchws
, {.id
= 5} },
5064 { "ws_7", switchws
, {.id
= 6} },
5065 { "ws_8", switchws
, {.id
= 7} },
5066 { "ws_9", switchws
, {.id
= 8} },
5067 { "ws_10", switchws
, {.id
= 9} },
5068 { "ws_11", switchws
, {.id
= 10} },
5069 { "ws_12", switchws
, {.id
= 11} },
5070 { "ws_13", switchws
, {.id
= 12} },
5071 { "ws_14", switchws
, {.id
= 13} },
5072 { "ws_15", switchws
, {.id
= 14} },
5073 { "ws_16", switchws
, {.id
= 15} },
5074 { "ws_17", switchws
, {.id
= 16} },
5075 { "ws_18", switchws
, {.id
= 17} },
5076 { "ws_19", switchws
, {.id
= 18} },
5077 { "ws_20", switchws
, {.id
= 19} },
5078 { "ws_21", switchws
, {.id
= 20} },
5079 { "ws_22", switchws
, {.id
= 21} },
5080 { "ws_next", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP
} },
5081 { "ws_next_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP_ALL
} },
5082 { "ws_prev", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN
} },
5083 { "ws_prev_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN_ALL
} },
5084 { "ws_prior", priorws
, {0} },
5085 { "dumpwins", dumpwins
, {0} }, /* MUST BE LAST */
5086 { "invalid key func", NULL
, {0} },
5089 RB_ENTRY(key
) entry
;
5092 enum keyfuncid funcid
;
5095 RB_HEAD(key_tree
, key
);
5098 key_cmp(struct key
*kp1
, struct key
*kp2
)
5100 if (kp1
->keysym
< kp2
->keysym
)
5102 if (kp1
->keysym
> kp2
->keysym
)
5105 if (kp1
->mod
< kp2
->mod
)
5107 if (kp1
->mod
> kp2
->mod
)
5113 RB_GENERATE(key_tree
, key
, entry
, key_cmp
);
5114 struct key_tree keys
;
5117 enum { client_click
, root_click
};
5119 unsigned int action
;
5121 unsigned int button
;
5122 void (*func
)(struct ws_win
*, union arg
*);
5125 #define MODKEY_SHIFT MODKEY | XCB_MOD_MASK_SHIFT
5126 /* action key mouse button func args */
5127 { client_click
, MODKEY
, XCB_BUTTON_INDEX_3
, resize
, {.id
= SWM_ARG_ID_DONTCENTER
} },
5128 { client_click
, MODKEY_SHIFT
, XCB_BUTTON_INDEX_3
, resize
, {.id
= SWM_ARG_ID_CENTER
} },
5129 { client_click
, MODKEY
, XCB_BUTTON_INDEX_1
, move
, {0} },
5134 update_modkey(unsigned int mod
)
5140 RB_FOREACH(kp
, key_tree
, &keys
)
5141 if (kp
->mod
& XCB_MOD_MASK_SHIFT
)
5142 kp
->mod
= mod
| XCB_MOD_MASK_SHIFT
;
5146 for (i
= 0; i
< LENGTH(buttons
); i
++)
5147 if (buttons
[i
].mask
& XCB_MOD_MASK_SHIFT
)
5148 buttons
[i
].mask
= mod
| XCB_MOD_MASK_SHIFT
;
5150 buttons
[i
].mask
= mod
;
5155 TAILQ_ENTRY(spawn_prog
) entry
;
5160 TAILQ_HEAD(spawn_list
, spawn_prog
);
5161 struct spawn_list spawns
= TAILQ_HEAD_INITIALIZER(spawns
);
5164 spawn_expand(struct swm_region
*r
, union arg
*args
, char *spawn_name
,
5167 struct spawn_prog
*prog
= NULL
;
5169 char *ap
, **real_args
;
5171 /* suppress unused warning since var is needed */
5174 DNPRINTF(SWM_D_SPAWN
, "spawn_expand: %s\n", spawn_name
);
5177 TAILQ_FOREACH(prog
, &spawns
, entry
) {
5178 if (!strcasecmp(spawn_name
, prog
->name
))
5182 warnx("spawn_custom: program %s not found", spawn_name
);
5186 /* make room for expanded args */
5187 if ((real_args
= calloc(prog
->argc
+ 1, sizeof(char *))) == NULL
)
5188 err(1, "spawn_custom: calloc real_args");
5190 /* expand spawn_args into real_args */
5191 for (i
= 0; i
< prog
->argc
; i
++) {
5193 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: raw arg: %s\n", ap
);
5194 if (!strcasecmp(ap
, "$bar_border")) {
5196 strdup(r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].name
))
5198 err(1, "spawn_custom border color");
5199 } else if (!strcasecmp(ap
, "$bar_color")) {
5201 strdup(r
->s
->c
[SWM_S_COLOR_BAR
].name
))
5203 err(1, "spawn_custom bar color");
5204 } else if (!strcasecmp(ap
, "$bar_font")) {
5205 if ((real_args
[i
] = strdup(bar_fonts
))
5207 err(1, "spawn_custom bar fonts");
5208 } else if (!strcasecmp(ap
, "$bar_font_color")) {
5210 strdup(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].name
))
5212 err(1, "spawn_custom color font");
5213 } else if (!strcasecmp(ap
, "$color_focus")) {
5215 strdup(r
->s
->c
[SWM_S_COLOR_FOCUS
].name
))
5217 err(1, "spawn_custom color focus");
5218 } else if (!strcasecmp(ap
, "$color_unfocus")) {
5220 strdup(r
->s
->c
[SWM_S_COLOR_UNFOCUS
].name
))
5222 err(1, "spawn_custom color unfocus");
5224 /* no match --> copy as is */
5225 if ((real_args
[i
] = strdup(ap
)) == NULL
)
5226 err(1, "spawn_custom strdup(ap)");
5228 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: cooked arg: %s\n",
5233 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: result: ");
5234 for (i
= 0; i
< prog
->argc
; i
++)
5235 DNPRINTF(SWM_D_SPAWN
, "\"%s\" ", real_args
[i
]);
5236 DNPRINTF(SWM_D_SPAWN
, "\n");
5238 *ret_args
= real_args
;
5239 return (prog
->argc
);
5243 spawn_custom(struct swm_region
*r
, union arg
*args
, char *spawn_name
)
5249 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
5253 spawn(r
->ws
->idx
, &a
, 1);
5255 for (i
= 0; i
< spawn_argc
; i
++)
5261 spawn_select(struct swm_region
*r
, union arg
*args
, char *spawn_name
, int *pid
)
5267 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
5271 if (pipe(select_list_pipe
) == -1)
5272 err(1, "pipe error");
5273 if (pipe(select_resp_pipe
) == -1)
5274 err(1, "pipe error");
5276 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
5277 err(1, "could not disable SIGPIPE");
5278 switch (*pid
= fork()) {
5280 err(1, "cannot fork");
5283 if (dup2(select_list_pipe
[0], 0) == -1)
5285 if (dup2(select_resp_pipe
[1], 1) == -1)
5287 close(select_list_pipe
[1]);
5288 close(select_resp_pipe
[0]);
5289 spawn(r
->ws
->idx
, &a
, 0);
5291 default: /* parent */
5292 close(select_list_pipe
[0]);
5293 close(select_resp_pipe
[1]);
5297 for (i
= 0; i
< spawn_argc
; i
++)
5303 spawn_insert(char *name
, char *args
)
5305 char *arg
, *cp
, *ptr
;
5306 struct spawn_prog
*sp
;
5308 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: %s\n", name
);
5310 if ((sp
= calloc(1, sizeof *sp
)) == NULL
)
5311 err(1, "spawn_insert: calloc");
5312 if ((sp
->name
= strdup(name
)) == NULL
)
5313 err(1, "spawn_insert: strdup");
5315 /* convert the arguments to an argument list */
5316 if ((ptr
= cp
= strdup(args
)) == NULL
)
5317 err(1, "spawn_insert: strdup");
5318 while ((arg
= strsep(&ptr
, " \t")) != NULL
) {
5319 /* empty field; skip it */
5324 if ((sp
->argv
= realloc(sp
->argv
, sp
->argc
*
5325 sizeof *sp
->argv
)) == NULL
)
5326 err(1, "spawn_insert: realloc");
5327 if ((sp
->argv
[sp
->argc
- 1] = strdup(arg
)) == NULL
)
5328 err(1, "spawn_insert: strdup");
5332 TAILQ_INSERT_TAIL(&spawns
, sp
, entry
);
5333 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: leave\n");
5337 spawn_remove(struct spawn_prog
*sp
)
5341 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: %s\n", sp
->name
);
5343 TAILQ_REMOVE(&spawns
, sp
, entry
);
5344 for (i
= 0; i
< sp
->argc
; i
++)
5350 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: leave\n");
5354 spawn_replace(struct spawn_prog
*sp
, char *name
, char *args
)
5356 DNPRINTF(SWM_D_SPAWN
, "spawn_replace: %s [%s]\n", sp
->name
, name
);
5359 spawn_insert(name
, args
);
5361 DNPRINTF(SWM_D_SPAWN
, "spawn_replace: leave\n");
5365 setspawn(char *name
, char *args
)
5367 struct spawn_prog
*sp
;
5369 DNPRINTF(SWM_D_SPAWN
, "setspawn: %s\n", name
);
5374 TAILQ_FOREACH(sp
, &spawns
, entry
) {
5375 if (!strcmp(sp
->name
, name
)) {
5379 spawn_replace(sp
, name
, args
);
5380 DNPRINTF(SWM_D_SPAWN
, "setspawn: leave\n");
5384 if (*args
== '\0') {
5385 warnx("error: setspawn: cannot find program: %s", name
);
5389 spawn_insert(name
, args
);
5390 DNPRINTF(SWM_D_SPAWN
, "setspawn: leave\n");
5394 setconfspawn(char *selector
, char *value
, int flags
)
5398 /* suppress unused warning since var is needed */
5401 args
= expand_tilde(value
);
5403 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: [%s] [%s]\n", selector
, args
);
5405 setspawn(selector
, args
);
5408 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: done\n");
5415 setconfspawn("term", "xterm", 0);
5416 setconfspawn("spawn_term", "xterm", 0);
5417 setconfspawn("screenshot_all", "screenshot.sh full", 0);
5418 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
5419 setconfspawn("lock", "xlock", 0);
5420 setconfspawn("initscr", "initscreen.sh", 0);
5421 setconfspawn("menu", "dmenu_run"
5424 " -nf $bar_font_color"
5426 " -sf $bar_color", 0);
5427 setconfspawn("search", "dmenu"
5431 " -nf $bar_font_color"
5433 " -sf $bar_color", 0);
5434 setconfspawn("name_workspace", "dmenu"
5438 " -nf $bar_font_color"
5440 " -sf $bar_color", 0);
5444 #define SWM_MODNAME_SIZE 32
5445 #define SWM_KEY_WS "\n+ \t"
5447 parsekeys(char *keystr
, unsigned int currmod
, unsigned int *mod
, KeySym
*ks
)
5451 DNPRINTF(SWM_D_KEY
, "parsekeys: enter [%s]\n", keystr
);
5452 if (mod
== NULL
|| ks
== NULL
) {
5453 DNPRINTF(SWM_D_KEY
, "parsekeys: no mod or key vars\n");
5456 if (keystr
== NULL
|| strlen(keystr
) == 0) {
5457 DNPRINTF(SWM_D_KEY
, "parsekeys: no keystr\n");
5461 *ks
= XCB_NO_SYMBOL
;
5463 while ((name
= strsep(&cp
, SWM_KEY_WS
)) != NULL
) {
5464 DNPRINTF(SWM_D_KEY
, "parsekeys: key [%s]\n", name
);
5466 cp
+= (long)strspn(cp
, SWM_KEY_WS
);
5467 if (strncasecmp(name
, "MOD", SWM_MODNAME_SIZE
) == 0)
5469 else if (!strncasecmp(name
, "Mod1", SWM_MODNAME_SIZE
))
5470 *mod
|= XCB_MOD_MASK_1
;
5471 else if (!strncasecmp(name
, "Mod2", SWM_MODNAME_SIZE
))
5472 *mod
+= XCB_MOD_MASK_2
;
5473 else if (!strncmp(name
, "Mod3", SWM_MODNAME_SIZE
))
5474 *mod
|= XCB_MOD_MASK_3
;
5475 else if (!strncmp(name
, "Mod4", SWM_MODNAME_SIZE
))
5476 *mod
|= XCB_MOD_MASK_4
;
5477 else if (strncasecmp(name
, "SHIFT", SWM_MODNAME_SIZE
) == 0)
5478 *mod
|= XCB_MOD_MASK_SHIFT
;
5479 else if (strncasecmp(name
, "CONTROL", SWM_MODNAME_SIZE
) == 0)
5480 *mod
|= XCB_MOD_MASK_CONTROL
;
5482 *ks
= XStringToKeysym(name
);
5483 XConvertCase(*ks
, ks
, &uks
);
5484 if (ks
== XCB_NO_SYMBOL
) {
5486 "parsekeys: invalid key %s\n",
5492 DNPRINTF(SWM_D_KEY
, "parsekeys: leave ok\n");
5497 strdupsafe(char *str
)
5502 return (strdup(str
));
5506 key_insert(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
, char *spawn_name
)
5510 DNPRINTF(SWM_D_KEY
, "key_insert: enter %s [%s]\n",
5511 keyfuncs
[kfid
].name
, spawn_name
);
5513 if ((kp
= malloc(sizeof *kp
)) == NULL
)
5514 err(1, "key_insert: malloc");
5519 kp
->spawn_name
= strdupsafe(spawn_name
);
5520 RB_INSERT(key_tree
, &keys
, kp
);
5522 DNPRINTF(SWM_D_KEY
, "key_insert: leave\n");
5526 key_lookup(unsigned int mod
, KeySym ks
)
5533 return (RB_FIND(key_tree
, &keys
, &kp
));
5537 key_remove(struct key
*kp
)
5539 DNPRINTF(SWM_D_KEY
, "key_remove: %s\n", keyfuncs
[kp
->funcid
].name
);
5541 RB_REMOVE(key_tree
, &keys
, kp
);
5542 free(kp
->spawn_name
);
5545 DNPRINTF(SWM_D_KEY
, "key_remove: leave\n");
5549 key_replace(struct key
*kp
, unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
5552 DNPRINTF(SWM_D_KEY
, "key_replace: %s [%s]\n", keyfuncs
[kp
->funcid
].name
,
5556 key_insert(mod
, ks
, kfid
, spawn_name
);
5558 DNPRINTF(SWM_D_KEY
, "key_replace: leave\n");
5562 setkeybinding(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
5567 DNPRINTF(SWM_D_KEY
, "setkeybinding: enter %s [%s]\n",
5568 keyfuncs
[kfid
].name
, spawn_name
);
5570 if ((kp
= key_lookup(mod
, ks
)) != NULL
) {
5571 if (kfid
== KF_INVALID
)
5574 key_replace(kp
, mod
, ks
, kfid
, spawn_name
);
5575 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
5578 if (kfid
== KF_INVALID
) {
5579 warnx("error: setkeybinding: cannot find mod/key combination");
5580 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
5584 key_insert(mod
, ks
, kfid
, spawn_name
);
5585 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
5589 setconfbinding(char *selector
, char *value
, int flags
)
5591 enum keyfuncid kfid
;
5594 struct spawn_prog
*sp
;
5596 /* suppress unused warning since var is needed */
5599 DNPRINTF(SWM_D_KEY
, "setconfbinding: enter\n");
5600 if (selector
== NULL
) {
5601 DNPRINTF(SWM_D_KEY
, "setconfbinding: unbind %s\n", value
);
5602 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
5604 setkeybinding(mod
, ks
, kfid
, NULL
);
5609 /* search by key function name */
5610 for (kfid
= 0; kfid
< KF_INVALID
; (kfid
)++) {
5611 if (strncasecmp(selector
, keyfuncs
[kfid
].name
,
5612 SWM_FUNCNAME_LEN
) == 0) {
5613 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match\n",
5615 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
5616 setkeybinding(mod
, ks
, kfid
, NULL
);
5622 /* search by custom spawn name */
5623 TAILQ_FOREACH(sp
, &spawns
, entry
) {
5624 if (strcasecmp(selector
, sp
->name
) == 0) {
5625 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match\n",
5627 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
5628 setkeybinding(mod
, ks
, KF_SPAWN_CUSTOM
,
5635 DNPRINTF(SWM_D_KEY
, "setconfbinding: no match\n");
5642 #define MODKEY_SHIFT MODKEY | XCB_MOD_MASK_SHIFT
5643 setkeybinding(MODKEY
, XK_space
, KF_CYCLE_LAYOUT
,NULL
);
5644 setkeybinding(MODKEY_SHIFT
, XK_backslash
, KF_FLIP_LAYOUT
, NULL
);
5645 setkeybinding(MODKEY_SHIFT
, XK_space
, KF_STACK_RESET
, NULL
);
5646 setkeybinding(MODKEY
, XK_h
, KF_MASTER_SHRINK
, NULL
);
5647 setkeybinding(MODKEY
, XK_l
, KF_MASTER_GROW
, NULL
);
5648 setkeybinding(MODKEY
, XK_comma
, KF_MASTER_ADD
, NULL
);
5649 setkeybinding(MODKEY
, XK_period
, KF_MASTER_DEL
, NULL
);
5650 setkeybinding(MODKEY_SHIFT
, XK_comma
, KF_STACK_INC
, NULL
);
5651 setkeybinding(MODKEY_SHIFT
, XK_period
, KF_STACK_DEC
, NULL
);
5652 setkeybinding(MODKEY
, XK_Return
, KF_SWAP_MAIN
, NULL
);
5653 setkeybinding(MODKEY
, XK_j
, KF_FOCUS_NEXT
, NULL
);
5654 setkeybinding(MODKEY
, XK_k
, KF_FOCUS_PREV
, NULL
);
5655 setkeybinding(MODKEY_SHIFT
, XK_j
, KF_SWAP_NEXT
, NULL
);
5656 setkeybinding(MODKEY_SHIFT
, XK_k
, KF_SWAP_PREV
, NULL
);
5657 setkeybinding(MODKEY_SHIFT
, XK_Return
, KF_SPAWN_CUSTOM
,"term");
5658 setkeybinding(MODKEY
, XK_p
, KF_SPAWN_CUSTOM
,"menu");
5659 setkeybinding(MODKEY_SHIFT
, XK_q
, KF_QUIT
, NULL
);
5660 setkeybinding(MODKEY
, XK_q
, KF_RESTART
, NULL
);
5661 setkeybinding(MODKEY
, XK_m
, KF_FOCUS_MAIN
, NULL
);
5662 setkeybinding(MODKEY
, XK_1
, KF_WS_1
, NULL
);
5663 setkeybinding(MODKEY
, XK_2
, KF_WS_2
, NULL
);
5664 setkeybinding(MODKEY
, XK_3
, KF_WS_3
, NULL
);
5665 setkeybinding(MODKEY
, XK_4
, KF_WS_4
, NULL
);
5666 setkeybinding(MODKEY
, XK_5
, KF_WS_5
, NULL
);
5667 setkeybinding(MODKEY
, XK_6
, KF_WS_6
, NULL
);
5668 setkeybinding(MODKEY
, XK_7
, KF_WS_7
, NULL
);
5669 setkeybinding(MODKEY
, XK_8
, KF_WS_8
, NULL
);
5670 setkeybinding(MODKEY
, XK_9
, KF_WS_9
, NULL
);
5671 setkeybinding(MODKEY
, XK_0
, KF_WS_10
, NULL
);
5672 setkeybinding(MODKEY
, XK_F1
, KF_WS_11
, NULL
);
5673 setkeybinding(MODKEY
, XK_F2
, KF_WS_12
, NULL
);
5674 setkeybinding(MODKEY
, XK_F3
, KF_WS_13
, NULL
);
5675 setkeybinding(MODKEY
, XK_F4
, KF_WS_14
, NULL
);
5676 setkeybinding(MODKEY
, XK_F5
, KF_WS_15
, NULL
);
5677 setkeybinding(MODKEY
, XK_F6
, KF_WS_16
, NULL
);
5678 setkeybinding(MODKEY
, XK_F7
, KF_WS_17
, NULL
);
5679 setkeybinding(MODKEY
, XK_F8
, KF_WS_18
, NULL
);
5680 setkeybinding(MODKEY
, XK_F9
, KF_WS_19
, NULL
);
5681 setkeybinding(MODKEY
, XK_F10
, KF_WS_20
, NULL
);
5682 setkeybinding(MODKEY
, XK_F11
, KF_WS_21
, NULL
);
5683 setkeybinding(MODKEY
, XK_F12
, KF_WS_22
, NULL
);
5684 setkeybinding(MODKEY
, XK_Right
, KF_WS_NEXT
, NULL
);
5685 setkeybinding(MODKEY
, XK_Left
, KF_WS_PREV
, NULL
);
5686 setkeybinding(MODKEY
, XK_Up
, KF_WS_NEXT_ALL
, NULL
);
5687 setkeybinding(MODKEY
, XK_Down
, KF_WS_PREV_ALL
, NULL
);
5688 setkeybinding(MODKEY
, XK_a
, KF_WS_PRIOR
, NULL
);
5689 setkeybinding(MODKEY_SHIFT
, XK_Right
, KF_SCREEN_NEXT
, NULL
);
5690 setkeybinding(MODKEY_SHIFT
, XK_Left
, KF_SCREEN_PREV
, NULL
);
5691 setkeybinding(MODKEY_SHIFT
, XK_1
, KF_MVWS_1
, NULL
);
5692 setkeybinding(MODKEY_SHIFT
, XK_2
, KF_MVWS_2
, NULL
);
5693 setkeybinding(MODKEY_SHIFT
, XK_3
, KF_MVWS_3
, NULL
);
5694 setkeybinding(MODKEY_SHIFT
, XK_4
, KF_MVWS_4
, NULL
);
5695 setkeybinding(MODKEY_SHIFT
, XK_5
, KF_MVWS_5
, NULL
);
5696 setkeybinding(MODKEY_SHIFT
, XK_6
, KF_MVWS_6
, NULL
);
5697 setkeybinding(MODKEY_SHIFT
, XK_7
, KF_MVWS_7
, NULL
);
5698 setkeybinding(MODKEY_SHIFT
, XK_8
, KF_MVWS_8
, NULL
);
5699 setkeybinding(MODKEY_SHIFT
, XK_9
, KF_MVWS_9
, NULL
);
5700 setkeybinding(MODKEY_SHIFT
, XK_0
, KF_MVWS_10
, NULL
);
5701 setkeybinding(MODKEY_SHIFT
, XK_F1
, KF_MVWS_11
, NULL
);
5702 setkeybinding(MODKEY_SHIFT
, XK_F2
, KF_MVWS_12
, NULL
);
5703 setkeybinding(MODKEY_SHIFT
, XK_F3
, KF_MVWS_13
, NULL
);
5704 setkeybinding(MODKEY_SHIFT
, XK_F4
, KF_MVWS_14
, NULL
);
5705 setkeybinding(MODKEY_SHIFT
, XK_F5
, KF_MVWS_15
, NULL
);
5706 setkeybinding(MODKEY_SHIFT
, XK_F6
, KF_MVWS_16
, NULL
);
5707 setkeybinding(MODKEY_SHIFT
, XK_F7
, KF_MVWS_17
, NULL
);
5708 setkeybinding(MODKEY_SHIFT
, XK_F8
, KF_MVWS_18
, NULL
);
5709 setkeybinding(MODKEY_SHIFT
, XK_F9
, KF_MVWS_19
, NULL
);
5710 setkeybinding(MODKEY_SHIFT
, XK_F10
, KF_MVWS_20
, NULL
);
5711 setkeybinding(MODKEY_SHIFT
, XK_F11
, KF_MVWS_21
, NULL
);
5712 setkeybinding(MODKEY_SHIFT
, XK_F12
, KF_MVWS_22
, NULL
);
5713 setkeybinding(MODKEY
, XK_b
, KF_BAR_TOGGLE
, NULL
);
5714 setkeybinding(MODKEY
, XK_Tab
, KF_FOCUS_NEXT
, NULL
);
5715 setkeybinding(MODKEY_SHIFT
, XK_Tab
, KF_FOCUS_PREV
, NULL
);
5716 setkeybinding(MODKEY_SHIFT
, XK_x
, KF_WIND_KILL
, NULL
);
5717 setkeybinding(MODKEY
, XK_x
, KF_WIND_DEL
, NULL
);
5718 setkeybinding(MODKEY
, XK_s
, KF_SPAWN_CUSTOM
,"screenshot_all");
5719 setkeybinding(MODKEY_SHIFT
, XK_s
, KF_SPAWN_CUSTOM
,"screenshot_wind");
5720 setkeybinding(MODKEY
, XK_t
, KF_FLOAT_TOGGLE
,NULL
);
5721 setkeybinding(MODKEY_SHIFT
, XK_v
, KF_VERSION
, NULL
);
5722 setkeybinding(MODKEY_SHIFT
, XK_Delete
, KF_SPAWN_CUSTOM
,"lock");
5723 setkeybinding(MODKEY_SHIFT
, XK_i
, KF_SPAWN_CUSTOM
,"initscr");
5724 setkeybinding(MODKEY
, XK_w
, KF_ICONIFY
, NULL
);
5725 setkeybinding(MODKEY_SHIFT
, XK_w
, KF_UNICONIFY
, NULL
);
5726 setkeybinding(MODKEY_SHIFT
, XK_r
, KF_RAISE_TOGGLE
,NULL
);
5727 setkeybinding(MODKEY
, XK_v
, KF_BUTTON2
, NULL
);
5728 setkeybinding(MODKEY
, XK_equal
, KF_WIDTH_GROW
, NULL
);
5729 setkeybinding(MODKEY
, XK_minus
, KF_WIDTH_SHRINK
,NULL
);
5730 setkeybinding(MODKEY_SHIFT
, XK_equal
, KF_HEIGHT_GROW
,NULL
);
5731 setkeybinding(MODKEY_SHIFT
, XK_minus
, KF_HEIGHT_SHRINK
,NULL
);
5732 setkeybinding(MODKEY
, XK_bracketleft
, KF_MOVE_LEFT
,NULL
);
5733 setkeybinding(MODKEY
, XK_bracketright
,KF_MOVE_RIGHT
,NULL
);
5734 setkeybinding(MODKEY_SHIFT
, XK_bracketleft
, KF_MOVE_UP
, NULL
);
5735 setkeybinding(MODKEY_SHIFT
, XK_bracketright
,KF_MOVE_DOWN
,NULL
);
5736 setkeybinding(MODKEY_SHIFT
, XK_slash
, KF_NAME_WORKSPACE
,NULL
);
5737 setkeybinding(MODKEY
, XK_slash
, KF_SEARCH_WORKSPACE
,NULL
);
5738 setkeybinding(MODKEY
, XK_f
, KF_SEARCH_WIN
, NULL
);
5740 setkeybinding(MODKEY_SHIFT
, XK_d
, KF_DUMPWINS
, NULL
);
5750 while (RB_EMPTY(&keys
) == 0) {
5751 kp
= RB_ROOT(&keys
);
5757 setkeymapping(char *selector
, char *value
, int flags
)
5759 char keymapping_file
[PATH_MAX
];
5761 /* suppress unused warnings since vars are needed */
5765 DNPRINTF(SWM_D_KEY
, "setkeymapping: enter\n");
5766 if (value
[0] == '~')
5767 snprintf(keymapping_file
, sizeof keymapping_file
, "%s/%s",
5768 pwd
->pw_dir
, &value
[1]);
5770 strlcpy(keymapping_file
, value
, sizeof keymapping_file
);
5772 /* load new key bindings; if it fails, revert to default bindings */
5773 if (conf_load(keymapping_file
, SWM_CONF_KEYMAPPING
)) {
5777 DNPRINTF(SWM_D_KEY
, "setkeymapping: leave\n");
5782 updatenumlockmask(void)
5785 xcb_get_modifier_mapping_reply_t
*modmap_r
;
5786 xcb_keycode_t
*modmap
, kc
, *keycode
;
5790 modmap_r
= xcb_get_modifier_mapping_reply(conn
,
5791 xcb_get_modifier_mapping(conn
),
5794 modmap
= xcb_get_modifier_mapping_keycodes(modmap_r
);
5795 for (i
= 0; i
< 8; i
++) {
5796 for (j
= 0; j
< modmap_r
->keycodes_per_modifier
; j
++) {
5797 kc
= modmap
[i
* modmap_r
->keycodes_per_modifier
5799 keycode
= xcb_key_symbols_get_keycode(syms
,
5802 numlockmask
= (1 << i
);
5808 DNPRINTF(SWM_D_MISC
, "updatenumlockmask: %d\n", numlockmask
);
5815 int num_screens
, k
, j
;
5816 unsigned int modifiers
[3];
5817 xcb_keycode_t
*code
;
5819 DNPRINTF(SWM_D_MISC
, "grabkeys\n");
5820 updatenumlockmask();
5823 modifiers
[1] = numlockmask
;
5824 modifiers
[2] = numlockmask
| XCB_MOD_MASK_LOCK
;
5826 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
5827 for (k
= 0; k
< num_screens
; k
++) {
5828 if (TAILQ_EMPTY(&screens
[k
].rl
))
5830 xcb_ungrab_key(conn
, XCB_GRAB_ANY
, screens
[k
].root
,
5832 RB_FOREACH(kp
, key_tree
, &keys
) {
5833 if ((code
= xcb_key_symbols_get_keycode(syms
,
5835 for (j
= 0; j
< LENGTH(modifiers
); j
++)
5836 xcb_grab_key(conn
, 1,
5838 kp
->mod
| modifiers
[j
],
5839 *code
, XCB_GRAB_MODE_SYNC
,
5840 XCB_GRAB_MODE_ASYNC
);
5847 grabbuttons(struct ws_win
*win
)
5851 DNPRINTF(SWM_D_MOUSE
, "grabbuttons: win 0x%x\n", win
->id
);
5853 for (i
= 0; i
< LENGTH(buttons
); i
++)
5854 if (buttons
[i
].action
== client_click
)
5855 xcb_grab_button(conn
, 0, win
->id
, BUTTONMASK
,
5856 XCB_GRAB_MODE_SYNC
, XCB_GRAB_MODE_ASYNC
,
5857 XCB_WINDOW_NONE
, XCB_CURSOR_NONE
,
5858 buttons
[i
].button
, buttons
[i
].mask
);
5860 /* click to focus */
5861 xcb_grab_button(conn
, 0, win
->id
, BUTTONMASK
, XCB_GRAB_MODE_SYNC
,
5862 XCB_GRAB_MODE_ASYNC
, XCB_WINDOW_NONE
, XCB_CURSOR_NONE
,
5863 XCB_BUTTON_INDEX_1
, XCB_BUTTON_MASK_ANY
);
5866 const char *quirkname
[] = {
5867 "NONE", /* config string for "no value" */
5876 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
5877 #define SWM_Q_WS "\n|+ \t"
5879 parsequirks(char *qstr
, unsigned long *quirk
)
5889 while ((name
= strsep(&cp
, SWM_Q_WS
)) != NULL
) {
5891 cp
+= (long)strspn(cp
, SWM_Q_WS
);
5892 for (i
= 0; i
< LENGTH(quirkname
); i
++) {
5893 if (!strncasecmp(name
, quirkname
[i
], SWM_QUIRK_LEN
)) {
5894 DNPRINTF(SWM_D_QUIRK
,
5895 "parsequirks: %s\n", name
);
5900 *quirk
|= 1 << (i
-1);
5904 if (i
>= LENGTH(quirkname
)) {
5905 DNPRINTF(SWM_D_QUIRK
,
5906 "parsequirks: invalid quirk [%s]\n", name
);
5914 quirk_insert(const char *class, const char *name
, unsigned long quirk
)
5918 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: %s:%s [%lu]\n", class, name
,
5921 if ((qp
= malloc(sizeof *qp
)) == NULL
)
5922 err(1, "quirk_insert: malloc");
5923 if ((qp
->class = strdup(class)) == NULL
)
5924 err(1, "quirk_insert: strdup");
5925 if ((qp
->name
= strdup(name
)) == NULL
)
5926 err(1, "quirk_insert: strdup");
5929 TAILQ_INSERT_TAIL(&quirks
, qp
, entry
);
5931 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: leave\n");
5935 quirk_remove(struct quirk
*qp
)
5937 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: %s:%s [%lu]\n", qp
->class,
5938 qp
->name
, qp
->quirk
);
5940 TAILQ_REMOVE(&quirks
, qp
, entry
);
5945 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: leave\n");
5949 quirk_replace(struct quirk
*qp
, const char *class, const char *name
,
5950 unsigned long quirk
)
5952 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: %s:%s [%lu]\n", qp
->class,
5953 qp
->name
, qp
->quirk
);
5956 quirk_insert(class, name
, quirk
);
5958 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: leave\n");
5962 setquirk(const char *class, const char *name
, unsigned long quirk
)
5966 DNPRINTF(SWM_D_QUIRK
, "setquirk: enter %s:%s [%lu]\n", class, name
,
5969 TAILQ_FOREACH(qp
, &quirks
, entry
) {
5970 if (!strcmp(qp
->class, class) && !strcmp(qp
->name
, name
)) {
5974 quirk_replace(qp
, class, name
, quirk
);
5975 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
5980 warnx("error: setquirk: cannot find class/name combination");
5984 quirk_insert(class, name
, quirk
);
5985 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
5989 setconfquirk(char *selector
, char *value
, int flags
)
5991 char *cp
, *class, *name
;
5995 /* suppress unused warning since var is needed */
5998 if (selector
== NULL
)
6000 if ((cp
= strchr(selector
, ':')) == NULL
)
6005 if ((retval
= parsequirks(value
, &qrks
)) == 0)
6006 setquirk(class, name
, qrks
);
6013 setquirk("MPlayer", "xv", SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
| SWM_Q_FOCUSPREV
);
6014 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT
);
6015 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ
);
6016 setquirk("Firefox", "Dialog", SWM_Q_FLOAT
);
6017 setquirk("Gimp", "gimp", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
6018 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ
);
6019 setquirk("xine", "Xine Window", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
6020 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
6021 setquirk("xine", "xine Panel", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
6022 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
6023 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN
| SWM_Q_FLOAT
);
6024 setquirk("pcb", "pcb", SWM_Q_FLOAT
);
6025 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
);
6028 /* conf file stuff */
6029 #define SWM_CONF_FILE "spectrwm.conf"
6030 #define SWM_CONF_FILE_OLD "scrotwm.conf"
6034 SWM_S_BAR_AT_BOTTOM
,
6035 SWM_S_BAR_BORDER_WIDTH
,
6042 SWM_S_CLOCK_ENABLED
,
6045 SWM_S_CYCLE_VISIBLE
,
6047 SWM_S_DISABLE_BORDER
,
6049 SWM_S_FOCUS_CLOSE_WRAP
,
6050 SWM_S_FOCUS_DEFAULT
,
6056 SWM_S_STACK_ENABLED
,
6058 SWM_S_TITLE_CLASS_ENABLED
,
6059 SWM_S_TITLE_NAME_ENABLED
,
6060 SWM_S_URGENT_ENABLED
,
6061 SWM_S_VERBOSE_LAYOUT
,
6062 SWM_S_WINDOW_NAME_ENABLED
,
6063 SWM_S_WORKSPACE_LIMIT
6067 setconfvalue(char *selector
, char *value
, int flags
)
6072 /* suppress unused warning since var is needed */
6076 case SWM_S_BAR_ACTION
:
6078 if ((bar_argv
[0] = expand_tilde(value
)) == NULL
)
6079 err(1, "setconfvalue: bar_action");
6081 case SWM_S_BAR_AT_BOTTOM
:
6082 bar_at_bottom
= atoi(value
);
6084 case SWM_S_BAR_BORDER_WIDTH
:
6085 bar_border_width
= atoi(value
);
6086 if (bar_border_width
< 0)
6087 bar_border_width
= 0;
6089 case SWM_S_BAR_DELAY
:
6090 bar_delay
= atoi(value
);
6092 case SWM_S_BAR_ENABLED
:
6093 bar_enabled
= atoi(value
);
6095 case SWM_S_BAR_FONT
:
6097 if (asprintf(&bar_fonts
, "%s,%s", value
, bar_fonts
) == -1)
6098 err(1, "setconfvalue: asprintf: failed to allocate "
6099 "memory for bar_fonts.");
6102 /* If already in xft mode, then we are done. */
6103 if (!bar_font_legacy
)
6106 /* If there are any non-XLFD entries, switch to Xft mode. */
6107 while ((b
= strsep(&value
, ",")) != NULL
) {
6111 bar_font_legacy
= 0;
6116 case SWM_S_BAR_FORMAT
:
6118 if ((bar_format
= strdup(value
)) == NULL
)
6119 err(1, "setconfvalue: bar_format");
6121 case SWM_S_BAR_JUSTIFY
:
6122 if (!strcmp(value
, "left"))
6123 bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
6124 else if (!strcmp(value
, "center"))
6125 bar_justify
= SWM_BAR_JUSTIFY_CENTER
;
6126 else if (!strcmp(value
, "right"))
6127 bar_justify
= SWM_BAR_JUSTIFY_RIGHT
;
6129 errx(1, "invalid bar_justify");
6131 case SWM_S_BORDER_WIDTH
:
6132 border_width
= atoi(value
);
6133 if (border_width
< 0)
6136 case SWM_S_CLOCK_ENABLED
:
6137 clock_enabled
= atoi(value
);
6139 case SWM_S_CLOCK_FORMAT
:
6140 #ifndef SWM_DENY_CLOCK_FORMAT
6142 if ((clock_format
= strdup(value
)) == NULL
)
6143 err(1, "setconfvalue: clock_format");
6146 case SWM_S_CYCLE_EMPTY
:
6147 cycle_empty
= atoi(value
);
6149 case SWM_S_CYCLE_VISIBLE
:
6150 cycle_visible
= atoi(value
);
6152 case SWM_S_DIALOG_RATIO
:
6153 dialog_ratio
= atof(value
);
6154 if (dialog_ratio
> 1.0 || dialog_ratio
<= .3)
6157 case SWM_S_DISABLE_BORDER
:
6158 disable_border
= atoi(value
);
6160 case SWM_S_FOCUS_CLOSE
:
6161 if (!strcmp(value
, "first"))
6162 focus_close
= SWM_STACK_BOTTOM
;
6163 else if (!strcmp(value
, "last"))
6164 focus_close
= SWM_STACK_TOP
;
6165 else if (!strcmp(value
, "next"))
6166 focus_close
= SWM_STACK_ABOVE
;
6167 else if (!strcmp(value
, "previous"))
6168 focus_close
= SWM_STACK_BELOW
;
6170 errx(1, "focus_close");
6172 case SWM_S_FOCUS_CLOSE_WRAP
:
6173 focus_close_wrap
= atoi(value
);
6175 case SWM_S_FOCUS_DEFAULT
:
6176 if (!strcmp(value
, "last"))
6177 focus_default
= SWM_STACK_TOP
;
6178 else if (!strcmp(value
, "first"))
6179 focus_default
= SWM_STACK_BOTTOM
;
6181 errx(1, "focus_default");
6183 case SWM_S_FOCUS_MODE
:
6184 if (!strcmp(value
, "default"))
6185 focus_mode
= SWM_FOCUS_DEFAULT
;
6186 else if (!strcmp(value
, "follow_cursor"))
6187 focus_mode
= SWM_FOCUS_FOLLOW
;
6188 else if (!strcmp(value
, "manual"))
6189 focus_mode
= SWM_FOCUS_MANUAL
;
6191 errx(1, "focus_mode");
6193 case SWM_S_SPAWN_ORDER
:
6194 if (!strcmp(value
, "first"))
6195 spawn_position
= SWM_STACK_BOTTOM
;
6196 else if (!strcmp(value
, "last"))
6197 spawn_position
= SWM_STACK_TOP
;
6198 else if (!strcmp(value
, "next"))
6199 spawn_position
= SWM_STACK_ABOVE
;
6200 else if (!strcmp(value
, "previous"))
6201 spawn_position
= SWM_STACK_BELOW
;
6203 errx(1, "spawn_position");
6205 case SWM_S_SPAWN_TERM
:
6206 setconfspawn("term", value
, 0);
6207 setconfspawn("spawn_term", value
, 0);
6211 case SWM_S_SS_ENABLED
:
6212 ss_enabled
= atoi(value
);
6214 case SWM_S_STACK_ENABLED
:
6215 stack_enabled
= atoi(value
);
6217 case SWM_S_TERM_WIDTH
:
6218 term_width
= atoi(value
);
6222 case SWM_S_TITLE_CLASS_ENABLED
:
6223 title_class_enabled
= atoi(value
);
6225 case SWM_S_TITLE_NAME_ENABLED
:
6226 title_name_enabled
= atoi(value
);
6228 case SWM_S_URGENT_ENABLED
:
6229 urgent_enabled
= atoi(value
);
6231 case SWM_S_VERBOSE_LAYOUT
:
6232 verbose_layout
= atoi(value
);
6233 for (i
= 0; layouts
[i
].l_stack
!= NULL
; i
++) {
6235 layouts
[i
].l_string
= fancy_stacker
;
6237 layouts
[i
].l_string
= plain_stacker
;
6240 case SWM_S_WINDOW_NAME_ENABLED
:
6241 window_name_enabled
= atoi(value
);
6243 case SWM_S_WORKSPACE_LIMIT
:
6244 workspace_limit
= atoi(value
);
6245 if (workspace_limit
> SWM_WS_MAX
)
6246 workspace_limit
= SWM_WS_MAX
;
6247 else if (workspace_limit
< 1)
6248 workspace_limit
= 1;
6257 setconfmodkey(char *selector
, char *value
, int flags
)
6259 /* suppress unused warnings since vars are needed */
6263 if (!strncasecmp(value
, "Mod1", strlen("Mod1")))
6264 update_modkey(XCB_MOD_MASK_1
);
6265 else if (!strncasecmp(value
, "Mod2", strlen("Mod2")))
6266 update_modkey(XCB_MOD_MASK_2
);
6267 else if (!strncasecmp(value
, "Mod3", strlen("Mod3")))
6268 update_modkey(XCB_MOD_MASK_3
);
6269 else if (!strncasecmp(value
, "Mod4", strlen("Mod4")))
6270 update_modkey(XCB_MOD_MASK_4
);
6277 setconfcolor(char *selector
, char *value
, int flags
)
6279 setscreencolor(value
, ((selector
== NULL
)?-1:atoi(selector
)), flags
);
6284 setconfregion(char *selector
, char *value
, int flags
)
6286 /* suppress unused warnings since vars are needed */
6290 custom_region(value
);
6295 setautorun(char *selector
, char *value
, int flags
)
6305 /* suppress unused warnings since vars are needed */
6309 if (getenv("SWM_STARTED"))
6313 if (sscanf(value
, "ws[%d]:%1023c", &ws_id
, s
) != 2)
6314 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
6316 if (ws_id
< 0 || ws_id
>= workspace_limit
)
6317 errx(1, "autorun: invalid workspace %d", ws_id
+ 1);
6320 * This is a little intricate
6322 * If the pid already exists we simply reuse it because it means it was
6323 * used before AND not claimed by manage_window. We get away with
6324 * altering it in the parent after INSERT because this can not be a race
6327 while ((ap
= strsep(&sp
, " \t")) != NULL
) {
6330 DNPRINTF(SWM_D_SPAWN
, "setautorun: arg [%s]\n", ap
);
6332 if ((a
.argv
= realloc(a
.argv
, argc
* sizeof(char *))) == NULL
)
6333 err(1, "setautorun: realloc");
6334 a
.argv
[argc
- 1] = ap
;
6337 if ((a
.argv
= realloc(a
.argv
, (argc
+ 1) * sizeof(char *))) == NULL
)
6338 err(1, "setautorun: realloc");
6339 a
.argv
[argc
] = NULL
;
6341 if ((pid
= fork()) == 0) {
6342 spawn(ws_id
, &a
, 1);
6351 p
= calloc(1, sizeof *p
);
6354 TAILQ_INSERT_TAIL(&pidlist
, p
, entry
);
6364 setlayout(char *selector
, char *value
, int flags
)
6366 int ws_id
, i
, x
, mg
, ma
, si
, ar
, f
= 0;
6367 int st
= SWM_V_STACK
, num_screens
;
6369 struct workspace
*ws
;
6371 /* suppress unused warnings since vars are needed */
6375 if (getenv("SWM_STARTED"))
6379 if (sscanf(value
, "ws[%d]:%d:%d:%d:%d:%1023c",
6380 &ws_id
, &mg
, &ma
, &si
, &ar
, s
) != 6)
6381 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
6382 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
6385 if (ws_id
< 0 || ws_id
>= workspace_limit
)
6386 errx(1, "layout: invalid workspace %d", ws_id
+ 1);
6388 if (!strcasecmp(s
, "vertical"))
6390 else if (!strcasecmp(s
, "vertical_flip")) {
6393 } else if (!strcasecmp(s
, "horizontal"))
6395 else if (!strcasecmp(s
, "horizontal_flip")) {
6398 } else if (!strcasecmp(s
, "fullscreen"))
6401 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
6402 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
6405 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
6406 for (i
= 0; i
< num_screens
; i
++) {
6407 ws
= (struct workspace
*)&screens
[i
].ws
;
6408 ws
[ws_id
].cur_layout
= &layouts
[st
];
6410 ws
[ws_id
].always_raise
= ar
;
6411 if (st
== SWM_MAX_STACK
)
6415 for (x
= 0; x
< abs(mg
); x
++) {
6416 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
6417 mg
>= 0 ? SWM_ARG_ID_MASTERGROW
:
6418 SWM_ARG_ID_MASTERSHRINK
);
6422 for (x
= 0; x
< abs(ma
); x
++) {
6423 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
6424 ma
>= 0 ? SWM_ARG_ID_MASTERADD
:
6425 SWM_ARG_ID_MASTERDEL
);
6429 for (x
= 0; x
< abs(si
); x
++) {
6430 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
6431 si
>= 0 ? SWM_ARG_ID_STACKINC
:
6432 SWM_ARG_ID_STACKDEC
);
6437 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
6438 SWM_ARG_ID_FLIPLAYOUT
);
6448 /* config options */
6449 struct config_option
{
6451 int (*func
)(char*, char*, int);
6454 struct config_option configopt
[] = {
6455 { "bar_enabled", setconfvalue
, SWM_S_BAR_ENABLED
},
6456 { "bar_at_bottom", setconfvalue
, SWM_S_BAR_AT_BOTTOM
},
6457 { "bar_border", setconfcolor
, SWM_S_COLOR_BAR_BORDER
},
6458 { "bar_border_width", setconfvalue
, SWM_S_BAR_BORDER_WIDTH
},
6459 { "bar_color", setconfcolor
, SWM_S_COLOR_BAR
},
6460 { "bar_font_color", setconfcolor
, SWM_S_COLOR_BAR_FONT
},
6461 { "bar_font", setconfvalue
, SWM_S_BAR_FONT
},
6462 { "bar_action", setconfvalue
, SWM_S_BAR_ACTION
},
6463 { "bar_delay", setconfvalue
, SWM_S_BAR_DELAY
},
6464 { "bar_justify", setconfvalue
, SWM_S_BAR_JUSTIFY
},
6465 { "bar_format", setconfvalue
, SWM_S_BAR_FORMAT
},
6466 { "keyboard_mapping", setkeymapping
, 0 },
6467 { "bind", setconfbinding
, 0 },
6468 { "stack_enabled", setconfvalue
, SWM_S_STACK_ENABLED
},
6469 { "clock_enabled", setconfvalue
, SWM_S_CLOCK_ENABLED
},
6470 { "clock_format", setconfvalue
, SWM_S_CLOCK_FORMAT
},
6471 { "color_focus", setconfcolor
, SWM_S_COLOR_FOCUS
},
6472 { "color_unfocus", setconfcolor
, SWM_S_COLOR_UNFOCUS
},
6473 { "cycle_empty", setconfvalue
, SWM_S_CYCLE_EMPTY
},
6474 { "cycle_visible", setconfvalue
, SWM_S_CYCLE_VISIBLE
},
6475 { "workspace_limit", setconfvalue
, SWM_S_WORKSPACE_LIMIT
},
6476 { "dialog_ratio", setconfvalue
, SWM_S_DIALOG_RATIO
},
6477 { "verbose_layout", setconfvalue
, SWM_S_VERBOSE_LAYOUT
},
6478 { "modkey", setconfmodkey
, 0 },
6479 { "program", setconfspawn
, 0 },
6480 { "quirk", setconfquirk
, 0 },
6481 { "region", setconfregion
, 0 },
6482 { "spawn_term", setconfvalue
, SWM_S_SPAWN_TERM
},
6483 { "screenshot_enabled", setconfvalue
, SWM_S_SS_ENABLED
},
6484 { "screenshot_app", setconfvalue
, SWM_S_SS_APP
},
6485 { "window_name_enabled", setconfvalue
, SWM_S_WINDOW_NAME_ENABLED
},
6486 { "urgent_enabled", setconfvalue
, SWM_S_URGENT_ENABLED
},
6487 { "term_width", setconfvalue
, SWM_S_TERM_WIDTH
},
6488 { "title_class_enabled", setconfvalue
, SWM_S_TITLE_CLASS_ENABLED
},
6489 { "title_name_enabled", setconfvalue
, SWM_S_TITLE_NAME_ENABLED
},
6490 { "focus_mode", setconfvalue
, SWM_S_FOCUS_MODE
},
6491 { "focus_close", setconfvalue
, SWM_S_FOCUS_CLOSE
},
6492 { "focus_close_wrap", setconfvalue
, SWM_S_FOCUS_CLOSE_WRAP
},
6493 { "focus_default", setconfvalue
, SWM_S_FOCUS_DEFAULT
},
6494 { "spawn_position", setconfvalue
, SWM_S_SPAWN_ORDER
},
6495 { "disable_border", setconfvalue
, SWM_S_DISABLE_BORDER
},
6496 { "border_width", setconfvalue
, SWM_S_BORDER_WIDTH
},
6497 { "autorun", setautorun
, 0 },
6498 { "layout", setlayout
, 0 },
6503 conf_load(char *filename
, int keymapping
)
6506 char *line
, *cp
, *optsub
, *optval
;
6507 size_t linelen
, lineno
= 0;
6508 int wordlen
, i
, optidx
;
6509 struct config_option
*opt
;
6511 DNPRINTF(SWM_D_CONF
, "conf_load: begin\n");
6513 if (filename
== NULL
) {
6514 warnx("conf_load: no filename");
6517 if ((config
= fopen(filename
, "r")) == NULL
) {
6518 warn("conf_load: fopen: %s", filename
);
6522 while (!feof(config
)) {
6523 if ((line
= fparseln(config
, &linelen
, &lineno
, NULL
, 0))
6526 err(1, "%s", filename
);
6531 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
6532 if (cp
[0] == '\0') {
6537 /* get config option */
6538 wordlen
= strcspn(cp
, "=[ \t\n");
6540 warnx("%s: line %zd: no option found",
6545 for (i
= 0; i
< LENGTH(configopt
); i
++) {
6546 opt
= &configopt
[i
];
6547 if (!strncasecmp(cp
, opt
->optname
, wordlen
) &&
6548 (int)strlen(opt
->optname
) == wordlen
) {
6554 warnx("%s: line %zd: unknown option %.*s",
6555 filename
, lineno
, wordlen
, cp
);
6558 if (keymapping
&& strcmp(opt
->optname
, "bind")) {
6559 warnx("%s: line %zd: invalid option %.*s",
6560 filename
, lineno
, wordlen
, cp
);
6564 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
6565 /* get [selector] if any */
6569 wordlen
= strcspn(cp
, "]");
6572 warnx("%s: line %zd: syntax error",
6577 if (asprintf(&optsub
, "%.*s", wordlen
, cp
) ==
6579 warnx("%s: line %zd: unable to allocate"
6580 "memory for selector", filename
,
6586 cp
+= strspn(cp
, "] \t\n"); /* eat trailing */
6588 cp
+= strspn(cp
, "= \t\n"); /* eat trailing */
6590 optval
= strdup(cp
);
6591 /* call function to deal with it all */
6592 if (configopt
[optidx
].func(optsub
, optval
,
6593 configopt
[optidx
].funcflags
) != 0)
6594 errx(1, "%s: line %zd: invalid data for %s",
6595 filename
, lineno
, configopt
[optidx
].optname
);
6602 DNPRINTF(SWM_D_CONF
, "conf_load: end\n");
6609 DNPRINTF(SWM_D_CONF
, "conf_load: end with error.\n");
6615 set_child_transient(struct ws_win
*win
, xcb_window_t
*trans
)
6617 struct ws_win
*parent
, *w
;
6618 struct swm_region
*r
;
6619 struct workspace
*ws
;
6620 xcb_icccm_wm_hints_t wmh
;
6622 parent
= find_window(win
->transient
);
6624 parent
->focus_child
= win
;
6626 DNPRINTF(SWM_D_MISC
, "set_child_transient: parent doesn't exist"
6627 " for 0x%x trans 0x%x\n", win
->id
, win
->transient
);
6629 r
= root_to_region(win
->wa
->root
);
6631 /* parent doen't exist in our window list */
6632 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
6633 if (xcb_icccm_get_wm_hints_reply(conn
,
6634 xcb_icccm_get_wm_hints(conn
, w
->id
),
6636 warnx("can't get hints for 0x%x", w
->id
);
6640 if (win
->hints
.window_group
!= wmh
.window_group
)
6643 w
->focus_child
= win
;
6644 win
->transient
= w
->id
;
6646 DNPRINTF(SWM_D_MISC
, "set_child_transient: adjusting "
6647 "transient to 0x%x\n", win
->transient
);
6654 window_get_pid(xcb_window_t win
)
6659 xcb_get_property_cookie_t pc
;
6660 xcb_get_property_reply_t
*pr
;
6662 apid
= get_atom_from_string("_NET_WM_PID");
6663 if (apid
== XCB_ATOM_NONE
)
6666 pc
= xcb_get_property(conn
, 0, win
, apid
, XCB_ATOM_CARDINAL
, 0, 1);
6667 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
6670 if (pr
->type
!= XCB_ATOM_CARDINAL
) {
6675 if (pr
->type
== apid
&& pr
->format
== 32)
6676 ret
= *((pid_t
*)xcb_get_property_value(pr
));
6682 apid
= get_atom_from_string("_SWM_PID");
6683 pc
= xcb_get_property(conn
, 0, win
, apid
, XCB_ATOM_STRING
,
6685 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
6688 if (pr
->type
!= apid
) {
6693 ret
= (pid_t
)strtonum(xcb_get_property_value(pr
), 0, INT_MAX
, &errstr
);
6700 get_ws_idx(xcb_window_t id
)
6706 xcb_get_property_reply_t
*gpr
;
6708 gpr
= xcb_get_property_reply(conn
,
6709 xcb_get_property(conn
, 0, id
, a_swm_ws
,
6710 XCB_ATOM_STRING
, 0, SWM_PROPLEN
),
6715 proplen
= xcb_get_property_value_length(gpr
);
6717 prop
= malloc(proplen
+ 1);
6720 xcb_get_property_value(gpr
),
6722 prop
[proplen
] = '\0';
6729 DNPRINTF(SWM_D_PROP
, "get_ws_idx: _SWM_WS: %s\n", prop
);
6730 ws_idx
= (int)strtonum(prop
, 0, workspace_limit
- 1, &errstr
);
6732 DNPRINTF(SWM_D_PROP
, "get_ws_idx: window: #%s: %s",
6742 manage_window(xcb_window_t id
)
6744 xcb_window_t trans
= XCB_WINDOW_NONE
;
6745 struct ws_win
*win
, *ww
;
6746 int ws_idx
, border_me
= 0;
6747 char ws_idx_str
[SWM_PROPLEN
];
6748 struct swm_region
*r
;
6751 uint32_t event_mask
, i
;
6752 xcb_icccm_get_wm_protocols_reply_t wpr
;
6754 if ((win
= find_window(id
)) != NULL
) {
6755 DNPRINTF(SWM_D_MISC
, "manage_window: win 0x%x already "
6756 "managed; skipping.)\n", id
);
6757 return (win
); /* Already managed. */
6760 /* See if window is on the unmanaged list. */
6761 if ((win
= find_unmanaged_window(id
)) != NULL
) {
6762 DNPRINTF(SWM_D_MISC
, "manage_window: win 0x%x found on "
6763 "unmanaged list.\n", id
);
6764 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
6767 set_child_transient(win
, &trans
);
6771 DNPRINTF(SWM_D_MISC
, "manage_window: win 0x%x is new.\n", id
);
6774 /* Create and initialize ws_win object. */
6775 if ((win
= calloc(1, sizeof(struct ws_win
))) == NULL
)
6776 err(1, "manage_window: calloc: failed to allocate memory for "
6781 /* Get window geometry. */
6782 win
->wa
= xcb_get_geometry_reply(conn
,
6783 xcb_get_geometry(conn
, win
->id
),
6786 /* Figure out which region the window belongs to. */
6787 r
= root_to_region(win
->wa
->root
);
6789 /* Ignore window border if there is one. */
6790 WIDTH(win
) = win
->wa
->width
;
6791 HEIGHT(win
) = win
->wa
->height
;
6792 X(win
) = win
->wa
->x
+ win
->wa
->border_width
;
6793 Y(win
) = win
->wa
->y
+ win
->wa
->border_width
;
6795 win
->floatmaxed
= 0;
6796 win
->ewmh_flags
= 0;
6797 win
->s
= r
->s
; /* this never changes */
6799 store_float_geom(win
, r
);
6801 /* Get WM_SIZE_HINTS. */
6802 xcb_icccm_get_wm_normal_hints_reply(conn
,
6803 xcb_icccm_get_wm_normal_hints(conn
, win
->id
),
6807 xcb_icccm_get_wm_hints_reply(conn
,
6808 xcb_icccm_get_wm_hints(conn
, win
->id
),
6811 /* Get WM_TRANSIENT_FOR; see if window is a transient. */
6812 xcb_icccm_get_wm_transient_for_reply(conn
,
6813 xcb_icccm_get_wm_transient_for(conn
, win
->id
),
6816 win
->transient
= trans
;
6817 set_child_transient(win
, &win
->transient
);
6820 /* Get supported protocols. */
6821 if (xcb_icccm_get_wm_protocols_reply(conn
,
6822 xcb_icccm_get_wm_protocols(conn
, win
->id
, a_prot
),
6824 for (i
= 0; i
< wpr
.atoms_len
; i
++) {
6825 if (wpr
.atoms
[i
] == a_takefocus
)
6826 win
->take_focus
= 1;
6827 if (wpr
.atoms
[i
] == a_delete
)
6828 win
->can_delete
= 1;
6830 xcb_icccm_get_wm_protocols_reply_wipe(&wpr
);
6833 win
->iconic
= get_iconic(win
);
6835 /* Figure out which workspace the window belongs to. */
6836 if ((p
= find_pid(window_get_pid(win
->id
))) != NULL
) {
6837 win
->ws
= &r
->s
->ws
[p
->ws
];
6838 TAILQ_REMOVE(&pidlist
, p
, entry
);
6841 } else if ((ws_idx
= get_ws_idx(win
->id
)) != -1 &&
6842 win
->transient
== 0) {
6843 /* _SWM_WS is set; use that. */
6844 win
->ws
= &r
->s
->ws
[ws_idx
];
6845 } else if (trans
&& (ww
= find_window(trans
)) != NULL
) {
6846 /* Launch transients in the same ws as parent. */
6853 /* Set the _SWM_WS atom so we can remember this after reincarnation. */
6854 if (snprintf(ws_idx_str
, SWM_PROPLEN
, "%d", win
->ws
->idx
) <
6856 DNPRINTF(SWM_D_PROP
, "manage_window: set _SWM_WS: %s\n",
6858 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
6859 a_swm_ws
, XCB_ATOM_STRING
, 8, strlen(ws_idx_str
),
6864 ewmh_autoquirk(win
);
6866 /* Determine initial quirks. */
6867 if (xcb_icccm_get_wm_class_reply(conn
,
6868 xcb_icccm_get_wm_class(conn
, win
->id
),
6870 DNPRINTF(SWM_D_CLASS
, "manage_window: class: %s, name: %s\n",
6871 win
->ch
.class_name
, win
->ch
.instance_name
);
6873 /* java is retarded so treat it special */
6874 if (strstr(win
->ch
.instance_name
, "sun-awt")) {
6875 DNPRINTF(SWM_D_CLASS
, "manage_window: java window "
6881 TAILQ_FOREACH(qp
, &quirks
, entry
) {
6882 if (!strcmp(win
->ch
.class_name
, qp
->class) &&
6883 !strcmp(win
->ch
.instance_name
, qp
->name
)) {
6884 DNPRINTF(SWM_D_CLASS
, "manage_window: on quirks"
6885 "list; mask: 0x%lx\n", qp
->quirk
);
6886 if (qp
->quirk
& SWM_Q_FLOAT
) {
6890 win
->quirks
= qp
->quirk
;
6895 /* Alter window position if quirky */
6896 if (win
->quirks
& SWM_Q_ANYWHERE
) {
6901 /* Reset font sizes (the bruteforce way; no default keybinding). */
6902 if (win
->quirks
& SWM_Q_XTERM_FONTADJ
) {
6903 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
6904 fake_keypress(win
, XK_KP_Subtract
, XCB_MOD_MASK_SHIFT
);
6905 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
6906 fake_keypress(win
, XK_KP_Add
, XCB_MOD_MASK_SHIFT
);
6911 X(win
) -= border_width
;
6912 Y(win
) -= border_width
;
6915 /* Make sure window is positioned inside its region, if its active. */
6917 constrain_window(win
, win
->ws
->r
, 0);
6919 if (win
->ws
->r
|| border_me
)
6922 /* Select which X events to monitor. */
6923 event_mask
= XCB_EVENT_MASK_ENTER_WINDOW
| XCB_EVENT_MASK_FOCUS_CHANGE
|
6924 XCB_EVENT_MASK_PROPERTY_CHANGE
| XCB_EVENT_MASK_STRUCTURE_NOTIFY
;
6926 event_mask
|= XCB_EVENT_MASK_LEAVE_WINDOW
;
6929 xcb_change_window_attributes(conn
, win
->id
, XCB_CW_EVENT_MASK
,
6933 /* Figure out where to stack the window in the workspace. */
6934 if (trans
&& (ww
= find_window(trans
)))
6935 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, ww
, win
, entry
);
6936 else if (win
->ws
->focus
&& spawn_position
== SWM_STACK_ABOVE
)
6937 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, win
->ws
->focus
, win
,
6939 else if (win
->ws
->focus
&& spawn_position
== SWM_STACK_BELOW
)
6940 TAILQ_INSERT_BEFORE(win
->ws
->focus
, win
, entry
);
6941 else switch (spawn_position
) {
6944 case SWM_STACK_ABOVE
:
6945 TAILQ_INSERT_TAIL(&win
->ws
->winlist
, win
, entry
);
6947 case SWM_STACK_BOTTOM
:
6948 case SWM_STACK_BELOW
:
6949 TAILQ_INSERT_HEAD(&win
->ws
->winlist
, win
, entry
);
6952 /* Get initial _NET_WM_STATE */
6953 ewmh_get_win_state(win
);
6954 /* Set initial _NET_WM_ALLOWED_ACTIONS */
6955 ewmh_update_actions(win
);
6959 DNPRINTF(SWM_D_MISC
, "manage_window: done. window: 0x%x, (x,y) w x h: "
6960 "(%d,%d) %d x %d, ws: %d, iconic: %s, transient: 0x%x\n", win
->id
,
6961 X(win
), Y(win
), WIDTH(win
), HEIGHT(win
), win
->ws
->idx
,
6962 YESNO(win
->iconic
), win
->transient
);
6968 free_window(struct ws_win
*win
)
6970 DNPRINTF(SWM_D_MISC
, "free_window: window: 0x%x\n", win
->id
);
6975 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
6980 xcb_icccm_get_wm_class_reply_wipe(&win
->ch
);
6985 memset(win
, 0xff, sizeof *win
); /* XXX kill later */
6988 DNPRINTF(SWM_D_MISC
, "free_window: done\n");
6992 unmanage_window(struct ws_win
*win
)
6994 struct ws_win
*parent
;
6999 DNPRINTF(SWM_D_MISC
, "unmanage_window: window: 0x%x\n", win
->id
);
7001 if (win
->transient
) {
7002 parent
= find_window(win
->transient
);
7004 parent
->focus_child
= NULL
;
7007 focus_win(get_focus_prev(win
));
7009 TAILQ_REMOVE(&win
->ws
->winlist
, win
, entry
);
7010 TAILQ_INSERT_TAIL(&win
->ws
->unmanagedlist
, win
, entry
);
7014 expose(xcb_expose_event_t
*e
)
7017 struct swm_region
*r
;
7019 DNPRINTF(SWM_D_EVENT
, "expose: window: 0x%x\n", e
->window
);
7021 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7022 for (i
= 0; i
< num_screens
; i
++)
7023 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
7024 if (e
->window
== WINID(r
->bar
))
7032 focusin(xcb_focus_in_event_t
*e
)
7034 DNPRINTF(SWM_D_EVENT
, "focusin: window: 0x%x, mode: %s(%u), "
7035 "detail: %s(%u)\n", e
->event
, get_notify_mode_label(e
->mode
),
7036 e
->mode
, get_notify_detail_label(e
->detail
), e
->detail
);
7041 keypress(xcb_key_press_event_t
*e
)
7043 xcb_keysym_t keysym
;
7046 keysym
= xcb_key_press_lookup_keysym(syms
, e
, 0);
7048 DNPRINTF(SWM_D_EVENT
, "keypress: %u %u\n", e
->detail
, keysym
);
7050 if ((kp
= key_lookup(CLEANMASK(e
->state
), keysym
)) == NULL
)
7053 if (kp
->funcid
== KF_SPAWN_CUSTOM
)
7054 spawn_custom(root_to_region(e
->root
),
7055 &(keyfuncs
[kp
->funcid
].args
), kp
->spawn_name
);
7056 else if (keyfuncs
[kp
->funcid
].func
)
7057 keyfuncs
[kp
->funcid
].func(root_to_region(e
->root
),
7058 &(keyfuncs
[kp
->funcid
].args
));
7062 buttonpress(xcb_button_press_event_t
*e
)
7068 DNPRINTF(SWM_D_EVENT
, "buttonpress: window 0x%x, detail: %u\n",
7069 e
->event
, e
->detail
);
7071 if ((win
= find_window(e
->event
)) == NULL
)
7074 focus_win(get_focus_magic(win
));
7076 for (i
= 0; i
< LENGTH(buttons
); i
++)
7077 if (client_click
== buttons
[i
].action
&& buttons
[i
].func
&&
7078 buttons
[i
].button
== e
->detail
&&
7079 CLEANMASK(buttons
[i
].mask
) == CLEANMASK(e
->state
)) {
7080 buttons
[i
].func(win
, &buttons
[i
].args
);
7085 DNPRINTF(SWM_D_EVENT
, "buttonpress: passing to window.\n");
7086 xcb_allow_events(conn
, XCB_ALLOW_REPLAY_POINTER
, e
->time
);
7088 DNPRINTF(SWM_D_EVENT
, "buttonpress: handled.\n");
7096 print_win_geom(xcb_window_t w
)
7098 xcb_get_geometry_reply_t
*wa
;
7100 wa
= xcb_get_geometry_reply(conn
, xcb_get_geometry(conn
, w
), NULL
);
7103 DNPRINTF(SWM_D_MISC
, "print_win_geom: window not found: 0x%x\n",
7108 DNPRINTF(SWM_D_MISC
, "print_win_geom: window: 0x%x, root: 0x%x, "
7109 "depth: %u, (x,y) w x h: (%d,%d) %d x %d, border: %d\n",
7110 w
, wa
->root
, wa
->depth
, wa
->x
, wa
->y
, wa
->width
, wa
->height
,
7119 get_stack_mode_name(uint8_t mode
)
7124 case XCB_STACK_MODE_ABOVE
:
7127 case XCB_STACK_MODE_BELOW
:
7130 case XCB_STACK_MODE_TOP_IF
:
7133 case XCB_STACK_MODE_BOTTOM_IF
:
7136 case XCB_STACK_MODE_OPPOSITE
:
7148 configurerequest(xcb_configure_request_event_t
*e
)
7151 struct swm_region
*r
= NULL
;
7154 uint32_t wc
[7] = {0};
7156 if ((win
= find_window(e
->window
)) == NULL
)
7157 if ((win
= find_unmanaged_window(e
->window
)) == NULL
)
7161 if (swm_debug
& SWM_D_EVENT
) {
7162 print_win_geom(e
->window
);
7164 DNPRINTF(SWM_D_EVENT
, "configurerequest: window: 0x%x, "
7165 "parent: 0x%x, new: %s, value_mask: %u { ", e
->window
,
7166 e
->parent
, YESNO(new), e
->value_mask
);
7167 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
)
7168 DPRINTF("X: %d ", e
->x
);
7169 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
)
7170 DPRINTF("Y: %d ", e
->y
);
7171 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
)
7172 DPRINTF("W: %u ", e
->width
);
7173 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
)
7174 DPRINTF("H: %u ", e
->height
);
7175 if (e
->value_mask
& XCB_CONFIG_WINDOW_BORDER_WIDTH
)
7176 DPRINTF("Border: %u ", e
->border_width
);
7177 if (e
->value_mask
& XCB_CONFIG_WINDOW_SIBLING
)
7178 DPRINTF("Sibling: 0x%x ", e
->sibling
);
7179 if (e
->value_mask
& XCB_CONFIG_WINDOW_STACK_MODE
)
7180 DPRINTF("StackMode: %s(%u) ",
7181 get_stack_mode_name(e
->stack_mode
), e
->stack_mode
);
7187 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
) {
7188 mask
|= XCB_CONFIG_WINDOW_X
;
7191 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
) {
7192 mask
|= XCB_CONFIG_WINDOW_Y
;
7195 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
) {
7196 mask
|= XCB_CONFIG_WINDOW_WIDTH
;
7199 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
) {
7200 mask
|= XCB_CONFIG_WINDOW_HEIGHT
;
7201 wc
[i
++] = e
->height
;
7203 if (e
->value_mask
& XCB_CONFIG_WINDOW_BORDER_WIDTH
) {
7204 mask
|= XCB_CONFIG_WINDOW_BORDER_WIDTH
;
7205 wc
[i
++] = e
->border_width
;
7207 if (e
->value_mask
& XCB_CONFIG_WINDOW_SIBLING
) {
7208 mask
|= XCB_CONFIG_WINDOW_SIBLING
;
7209 wc
[i
++] = e
->sibling
;
7211 if (e
->value_mask
& XCB_CONFIG_WINDOW_STACK_MODE
) {
7212 mask
|= XCB_CONFIG_WINDOW_STACK_MODE
;
7213 wc
[i
++] = e
->stack_mode
;
7217 xcb_configure_window(conn
, e
->window
, mask
, wc
);
7218 } else if ((!win
->manual
|| win
->quirks
& SWM_Q_ANYWHERE
) &&
7219 !(win
->ewmh_flags
& EWMH_F_FULLSCREEN
)) {
7222 else if (win
->ws
->old_r
)
7225 /* windows are centered unless ANYWHERE quirk is set. */
7226 if (win
->quirks
& SWM_Q_ANYWHERE
) {
7227 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
) {
7228 win
->g_float
.x
= e
->x
;
7230 win
->g_float
.x
-= X(r
);
7233 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
) {
7234 win
->g_float
.y
= e
->y
;
7236 win
->g_float
.y
-= Y(r
);
7240 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
)
7241 win
->g_float
.w
= e
->width
;
7243 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
)
7244 win
->g_float
.h
= e
->height
;
7246 win
->g_floatvalid
= 1;
7248 if (win
->floating
&& r
) {
7249 WIDTH(win
) = win
->g_float
.w
;
7250 HEIGHT(win
) = win
->g_float
.h
;
7252 stack_floater(win
, win
->ws
->r
);
7266 configurenotify(xcb_configure_notify_event_t
*e
)
7270 DNPRINTF(SWM_D_EVENT
, "configurenotify: window: 0x%x\n",
7273 win
= find_window(e
->window
);
7275 xcb_icccm_get_wm_normal_hints_reply(conn
,
7276 xcb_icccm_get_wm_normal_hints(conn
, win
->id
),
7279 if (font_adjusted
) {
7287 destroynotify(xcb_destroy_notify_event_t
*e
)
7291 DNPRINTF(SWM_D_EVENT
, "destroynotify: window: 0x%x\n", e
->window
);
7293 if ((win
= find_window(e
->window
)) == NULL
) {
7294 if ((win
= find_unmanaged_window(e
->window
)) == NULL
)
7300 /* make sure we focus on something */
7303 unmanage_window(win
);
7313 get_notify_detail_label(uint8_t detail
)
7318 case XCB_NOTIFY_DETAIL_ANCESTOR
:
7321 case XCB_NOTIFY_DETAIL_VIRTUAL
:
7324 case XCB_NOTIFY_DETAIL_INFERIOR
:
7327 case XCB_NOTIFY_DETAIL_NONLINEAR
:
7328 label
= "Nonlinear";
7330 case XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL
:
7331 label
= "NonlinearVirtual";
7333 case XCB_NOTIFY_DETAIL_POINTER
:
7336 case XCB_NOTIFY_DETAIL_POINTER_ROOT
:
7337 label
= "PointerRoot";
7339 case XCB_NOTIFY_DETAIL_NONE
:
7350 get_notify_mode_label(uint8_t mode
)
7355 case XCB_NOTIFY_MODE_NORMAL
:
7358 case XCB_NOTIFY_MODE_GRAB
:
7361 case XCB_NOTIFY_MODE_UNGRAB
:
7364 case XCB_NOTIFY_MODE_WHILE_GRABBED
:
7365 label
= "WhileGrabbed";
7376 enternotify(xcb_enter_notify_event_t
*e
)
7380 DNPRINTF(SWM_D_FOCUS
, "enternotify: window: 0x%x, mode: %s(%d), "
7381 "detail: %s(%d), root: 0x%x, subwindow: 0x%x, same_screen_focus: "
7382 "%s, state: %d\n", e
->event
, get_notify_mode_label(e
->mode
),
7383 e
->mode
, get_notify_detail_label(e
->detail
), e
->detail
, e
->root
,
7384 e
->child
, YESNO(e
->same_screen_focus
), e
->state
);
7386 if (focus_mode
== SWM_FOCUS_MANUAL
&&
7387 e
->mode
== XCB_NOTIFY_MODE_NORMAL
) {
7388 DNPRINTF(SWM_D_EVENT
, "enternotify: manual focus; ignoring.\n");
7392 if ((win
= find_window(e
->event
)) == NULL
) {
7393 DNPRINTF(SWM_D_EVENT
, "enternotify: window is NULL; ignoring\n");
7397 focus_win(get_focus_magic(win
));
7404 leavenotify(xcb_leave_notify_event_t
*e
)
7406 DNPRINTF(SWM_D_FOCUS
, "leavenotify: window: 0x%x, mode: %s(%d), "
7407 "detail: %s(%d), root: 0x%x, subwindow: 0x%x, same_screen_focus: "
7408 "%s, state: %d\n", e
->event
, get_notify_mode_label(e
->mode
),
7409 e
->mode
, get_notify_detail_label(e
->detail
), e
->detail
, e
->root
,
7410 e
->child
, YESNO(e
->same_screen_focus
), e
->state
);
7414 /* lets us use one switch statement for arbitrary mode/detail combinations */
7415 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
7418 mapnotify(xcb_map_notify_event_t
*e
)
7422 DNPRINTF(SWM_D_EVENT
, "mapnotify: window: 0x%x\n", e
->window
);
7424 if ((win
= find_window(e
->window
)) == NULL
)
7425 win
= manage_window(e
->window
);
7427 set_win_state(win
, XCB_ICCCM_WM_STATE_NORMAL
);
7429 /* Focus on window if it is selected. */
7430 if (win
->ws
->focus
== win
)
7437 mappingnotify(xcb_mapping_notify_event_t
*e
)
7439 xcb_refresh_keyboard_mapping(syms
, e
);
7441 if (e
->request
== XCB_MAPPING_KEYBOARD
)
7446 maprequest(xcb_map_request_event_t
*e
)
7449 xcb_get_window_attributes_reply_t
*war
;
7451 DNPRINTF(SWM_D_EVENT
, "maprequest: win 0x%x\n",
7454 war
= xcb_get_window_attributes_reply(conn
,
7455 xcb_get_window_attributes(conn
, e
->window
),
7458 DNPRINTF(SWM_D_EVENT
, "maprequest: window lost.\n");
7462 if (war
->override_redirect
) {
7463 DNPRINTF(SWM_D_EVENT
, "maprequest: override_redirect; "
7468 win
= manage_window(e
->window
);
7470 /* All windows need to be mapped if they are in the current workspace.*/
7474 /* The new window should get focus. */
7475 win
->ws
->focus
= get_focus_magic(win
);
7477 /* Ignore EnterNotify to handle the mapnotify without interference. */
7478 if (focus_mode
== SWM_FOCUS_DEFAULT
)
7479 event_drain(XCB_ENTER_NOTIFY
);
7482 DNPRINTF(SWM_D_EVENT
, "maprequest: done.\n");
7487 get_atom_name(xcb_atom_t atom
)
7492 * This should be disabled during most debugging since
7493 * xcb_get_* causes an xcb_flush.
7496 xcb_get_atom_name_reply_t
*r
;
7498 r
= xcb_get_atom_name_reply(conn
,
7499 xcb_get_atom_name(conn
, atom
),
7502 len
= xcb_get_atom_name_name_length(r
);
7504 name
= malloc(len
+ 1);
7506 memcpy(name
, xcb_get_atom_name_name(r
), len
);
7520 propertynotify(xcb_property_notify_event_t
*e
)
7526 name
= get_atom_name(e
->atom
);
7527 DNPRINTF(SWM_D_EVENT
, "propertynotify: window: 0x%x, atom: %s(%u)\n",
7528 e
->window
, name
, e
->atom
);
7533 win
= find_window(e
->window
);
7537 if (e
->state
== XCB_PROPERTY_DELETE
&& e
->atom
== a_swm_iconic
) {
7538 update_iconic(win
, 0);
7539 map_window_raised(win
->id
);
7547 case XCB_ATOM_WM_CLASS
:
7548 case XCB_ATOM_WM_NAME
:
7557 unmapnotify(xcb_unmap_notify_event_t
*e
)
7561 DNPRINTF(SWM_D_EVENT
, "unmapnotify: window: 0x%x\n", e
->window
);
7563 /* determine if we need to help unmanage this window */
7564 win
= find_window(e
->window
);
7568 if (getstate(e
->window
) == XCB_ICCCM_WM_STATE_NORMAL
) {
7569 unmanage_window(win
);
7577 visibilitynotify(xcb_visibility_notify_event_t
*e
)
7579 DNPRINTF(SWM_D_EVENT
, "visibilitynotify: window: 0x%x\n",
7585 clientmessage(xcb_client_message_event_t
*e
)
7588 xcb_map_request_event_t mre
;
7592 name
= get_atom_name(e
->type
);
7593 DNPRINTF(SWM_D_EVENT
, "clientmessage: window: 0x%x, atom: %s(%u)\n",
7594 e
->window
, name
, e
->type
);
7597 win
= find_window(e
->window
);
7600 if (e
->type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
7601 DNPRINTF(SWM_D_EVENT
, "clientmessage: request focus on "
7602 "unmanaged window.\n");
7603 mre
.window
= e
->window
;
7609 if (e
->type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
7610 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_ACTIVE_WINDOW\n");
7613 if (e
->type
== ewmh
[_NET_CLOSE_WINDOW
].atom
) {
7614 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_CLOSE_WINDOW\n");
7615 if (win
->can_delete
)
7616 client_msg(win
, a_delete
);
7618 xcb_kill_client(conn
, win
->id
);
7620 if (e
->type
== ewmh
[_NET_MOVERESIZE_WINDOW
].atom
) {
7621 DNPRINTF(SWM_D_EVENT
,
7622 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
7623 if (win
->floating
) {
7624 if (e
->data
.data32
[0] & (1<<8)) /* x */
7625 X(win
) = e
->data
.data32
[1];
7626 if (e
->data
.data32
[0] & (1<<9)) /* y */
7627 Y(win
) = e
->data
.data32
[2];
7628 if (e
->data
.data32
[0] & (1<<10)) /* width */
7629 WIDTH(win
) = e
->data
.data32
[3];
7630 if (e
->data
.data32
[0] & (1<<11)) /* height */
7631 HEIGHT(win
) = e
->data
.data32
[4];
7636 /* TODO: Change stack sizes */
7637 /* notify no change was made. */
7638 config_win(win
, NULL
);
7641 if (e
->type
== ewmh
[_NET_WM_STATE
].atom
) {
7642 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_WM_STATE\n");
7643 ewmh_update_win_state(win
, e
->data
.data32
[1], e
->data
.data32
[0]);
7644 if (e
->data
.data32
[2])
7645 ewmh_update_win_state(win
, e
->data
.data32
[2],
7657 int errcode
= xcb_connection_has_error(conn
);
7658 #ifdef XCB_CONN_ERROR
7661 case XCB_CONN_ERROR
:
7662 s
= "Socket error, pipe error or other stream error.";
7664 case XCB_CONN_CLOSED_EXT_NOTSUPPORTED
:
7665 s
= "Extension not supported.";
7667 case XCB_CONN_CLOSED_MEM_INSUFFICIENT
:
7668 s
= "Insufficient memory.";
7670 case XCB_CONN_CLOSED_REQ_LEN_EXCEED
:
7671 s
= "Request length was exceeded.";
7673 case XCB_CONN_CLOSED_PARSE_ERR
:
7674 s
= "Error parsing display string.";
7677 s
= "Unknown error.";
7680 errx(errcode
, "X CONNECTION ERROR: %s", s
);
7683 errx(errcode
, "X CONNECTION ERROR");
7691 const uint32_t val
= XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT
;
7693 xcb_void_cookie_t wac
;
7694 xcb_generic_error_t
*error
;
7696 /* this causes an error if some other window manager is running */
7697 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7698 for (i
= 0; i
< num_screens
; i
++) {
7699 if ((sc
= get_screen(i
)) == NULL
)
7700 errx(1, "ERROR: can't get screen %d.", i
);
7701 DNPRINTF(SWM_D_INIT
, "enable_wm: screen %d, root: 0x%x\n",
7703 wac
= xcb_change_window_attributes_checked(conn
, sc
->root
,
7704 XCB_CW_EVENT_MASK
, &val
);
7705 if ((error
= xcb_request_check(conn
, wac
))) {
7706 DNPRINTF(SWM_D_INIT
, "enable_wm: error_code: %u\n",
7717 new_region(struct swm_screen
*s
, int x
, int y
, int w
, int h
)
7719 struct swm_region
*r
, *n
;
7720 struct workspace
*ws
= NULL
;
7723 DNPRINTF(SWM_D_MISC
, "new region: screen[%d]:%dx%d+%d+%d\n",
7724 s
->idx
, w
, h
, x
, y
);
7726 /* remove any conflicting regions */
7727 n
= TAILQ_FIRST(&s
->rl
);
7730 n
= TAILQ_NEXT(r
, entry
);
7731 if (X(r
) < (x
+ w
) &&
7732 (X(r
) + WIDTH(r
)) > x
&&
7734 (Y(r
) + HEIGHT(r
)) > y
) {
7735 if (r
->ws
->r
!= NULL
)
7736 r
->ws
->old_r
= r
->ws
->r
;
7739 TAILQ_REMOVE(&s
->rl
, r
, entry
);
7740 TAILQ_INSERT_TAIL(&s
->orl
, r
, entry
);
7744 /* search old regions for one to reuse */
7746 /* size + location match */
7747 TAILQ_FOREACH(r
, &s
->orl
, entry
)
7748 if (X(r
) == x
&& Y(r
) == y
&&
7749 HEIGHT(r
) == h
&& WIDTH(r
) == w
)
7753 TAILQ_FOREACH(r
, &s
->orl
, entry
)
7754 if (HEIGHT(r
) == h
&& WIDTH(r
) == w
)
7758 TAILQ_REMOVE(&s
->orl
, r
, entry
);
7759 /* try to use old region's workspace */
7760 if (r
->ws
->r
== NULL
)
7763 if ((r
= calloc(1, sizeof(struct swm_region
))) == NULL
)
7764 err(1, "new_region: calloc: failed to allocate memory "
7767 /* if we don't have a workspace already, find one */
7769 for (i
= 0; i
< workspace_limit
; i
++)
7770 if (s
->ws
[i
].r
== NULL
) {
7777 errx(1, "new_region: no free workspaces");
7788 TAILQ_INSERT_TAIL(&s
->rl
, r
, entry
);
7794 #ifdef SWM_XRR_HAS_CRTC
7797 #endif /* SWM_XRR_HAS_CRTC */
7798 struct swm_region
*r
;
7800 xcb_randr_get_screen_resources_current_cookie_t src
;
7801 xcb_randr_get_screen_resources_current_reply_t
*srr
;
7802 xcb_randr_get_crtc_info_cookie_t cic
;
7803 xcb_randr_get_crtc_info_reply_t
*cir
= NULL
;
7804 xcb_randr_crtc_t
*crtc
;
7805 xcb_screen_t
*screen
;
7807 if ((screen
= get_screen(i
)) == NULL
)
7808 errx(1, "ERROR: can't get screen %d.", i
);
7810 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7811 if (i
>= num_screens
)
7812 errx(1, "scan_xrandr: invalid screen");
7814 /* remove any old regions */
7815 while ((r
= TAILQ_FIRST(&screens
[i
].rl
)) != NULL
) {
7816 r
->ws
->old_r
= r
->ws
->r
= NULL
;
7818 TAILQ_REMOVE(&screens
[i
].rl
, r
, entry
);
7819 TAILQ_INSERT_TAIL(&screens
[i
].orl
, r
, entry
);
7823 /* map virtual screens onto physical screens */
7824 #ifdef SWM_XRR_HAS_CRTC
7825 if (xrandr_support
) {
7826 src
= xcb_randr_get_screen_resources_current(conn
,
7828 srr
= xcb_randr_get_screen_resources_current_reply(conn
, src
,
7831 new_region(&screens
[i
], 0, 0,
7832 screen
->width_in_pixels
,
7833 screen
->height_in_pixels
);
7836 ncrtc
= srr
->num_crtcs
;
7838 crtc
= xcb_randr_get_screen_resources_current_crtcs(srr
);
7839 for (c
= 0; c
< ncrtc
; c
++) {
7840 cic
= xcb_randr_get_crtc_info(conn
, crtc
[c
],
7842 cir
= xcb_randr_get_crtc_info_reply(conn
, cic
, NULL
);
7845 if (cir
->num_outputs
== 0) {
7851 new_region(&screens
[i
], 0, 0,
7852 screen
->width_in_pixels
,
7853 screen
->height_in_pixels
);
7855 new_region(&screens
[i
],
7856 cir
->x
, cir
->y
, cir
->width
, cir
->height
);
7861 #endif /* SWM_XRR_HAS_CRTC */
7863 new_region(&screens
[i
], 0, 0, screen
->width_in_pixels
,
7864 screen
->height_in_pixels
);
7869 screenchange(xcb_randr_screen_change_notify_event_t
*e
)
7871 struct swm_region
*r
;
7874 DNPRINTF(SWM_D_EVENT
, "screenchange: root: 0x%x\n", e
->root
);
7876 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7877 /* silly event doesn't include the screen index */
7878 for (i
= 0; i
< num_screens
; i
++)
7879 if (screens
[i
].root
== e
->root
)
7881 if (i
>= num_screens
)
7882 errx(1, "screenchange: screen not found");
7884 /* brute force for now, just re-enumerate the regions */
7888 print_win_geom(e
->root
);
7890 /* add bars to all regions */
7891 for (i
= 0; i
< num_screens
; i
++)
7892 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
7900 xcb_window_t
*wins
= NULL
;
7902 int i
, j
, num_screens
;
7903 uint16_t state
, manage
;
7905 xcb_query_tree_cookie_t qtc
;
7906 xcb_query_tree_reply_t
*qtr
;
7907 xcb_get_window_attributes_cookie_t c
;
7908 xcb_get_window_attributes_reply_t
*r
;
7909 xcb_get_property_cookie_t pc
;
7911 DNPRINTF(SWM_D_INIT
, "grab_windows: begin\n");
7912 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7913 for (i
= 0; i
< num_screens
; i
++) {
7914 qtc
= xcb_query_tree(conn
, screens
[i
].root
);
7915 qtr
= xcb_query_tree_reply(conn
, qtc
, NULL
);
7918 wins
= xcb_query_tree_children(qtr
);
7919 no
= xcb_query_tree_children_length(qtr
);
7920 /* attach windows to a region */
7921 /* normal windows */
7922 for (j
= 0; j
< no
; j
++) {
7923 c
= xcb_get_window_attributes(conn
, wins
[j
]);
7924 r
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
7927 if (r
->override_redirect
) {
7932 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
7933 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
7939 state
= getstate(wins
[j
]);
7940 manage
= state
== XCB_ICCCM_WM_STATE_ICONIC
;
7941 if (r
->map_state
== XCB_MAP_STATE_VIEWABLE
|| manage
)
7942 manage_window(wins
[j
]);
7945 /* transient windows */
7946 for (j
= 0; j
< no
; j
++) {
7947 c
= xcb_get_window_attributes(conn
, wins
[j
]);
7948 r
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
7951 if (r
->override_redirect
) {
7957 state
= getstate(wins
[j
]);
7958 manage
= state
== XCB_ICCCM_WM_STATE_ICONIC
;
7959 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
7960 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
7961 &wins
[j
], NULL
) && manage
)
7962 manage_window(wins
[j
]);
7966 DNPRINTF(SWM_D_INIT
, "grab_windows: done\n");
7972 int i
, j
, k
, num_screens
;
7973 struct workspace
*ws
;
7974 uint32_t gcv
[1], wa
[1];
7975 const xcb_query_extension_reply_t
*qep
;
7976 xcb_screen_t
*screen
;
7977 xcb_cursor_t cursor
;
7978 xcb_font_t cursor_font
;
7979 xcb_randr_query_version_cookie_t c
;
7980 xcb_randr_query_version_reply_t
*r
;
7982 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7983 if ((screens
= calloc(num_screens
,
7984 sizeof(struct swm_screen
))) == NULL
)
7985 err(1, "setup_screens: calloc: failed to allocate memory for "
7988 /* initial Xrandr setup */
7990 qep
= xcb_get_extension_data(conn
, &xcb_randr_id
);
7992 c
= xcb_randr_query_version(conn
, 1, 1);
7993 r
= xcb_randr_query_version_reply(conn
, c
, NULL
);
7995 if (r
->major_version
>= 1) {
7997 xrandr_eventbase
= qep
->first_event
;
8003 cursor_font
= xcb_generate_id(conn
);
8004 xcb_open_font(conn
, cursor_font
, strlen("cursor"), "cursor");
8006 cursor
= xcb_generate_id(conn
);
8007 xcb_create_glyph_cursor(conn
, cursor
, cursor_font
, cursor_font
,
8008 XC_left_ptr
, XC_left_ptr
+ 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
8011 /* map physical screens */
8012 for (i
= 0; i
< num_screens
; i
++) {
8013 DNPRINTF(SWM_D_WS
, "setup_screens: init screen: %d\n", i
);
8015 TAILQ_INIT(&screens
[i
].rl
);
8016 TAILQ_INIT(&screens
[i
].orl
);
8017 if ((screen
= get_screen(i
)) == NULL
)
8018 errx(1, "ERROR: can't get screen %d.", i
);
8019 screens
[i
].root
= screen
->root
;
8021 /* set default colors */
8022 setscreencolor("red", i
+ 1, SWM_S_COLOR_FOCUS
);
8023 setscreencolor("rgb:88/88/88", i
+ 1, SWM_S_COLOR_UNFOCUS
);
8024 setscreencolor("rgb:00/80/80", i
+ 1, SWM_S_COLOR_BAR_BORDER
);
8025 setscreencolor("black", i
+ 1, SWM_S_COLOR_BAR
);
8026 setscreencolor("rgb:a0/a0/a0", i
+ 1, SWM_S_COLOR_BAR_FONT
);
8028 /* create graphics context on screen */
8029 screens
[i
].bar_gc
= xcb_generate_id(conn
);
8031 xcb_create_gc(conn
, screens
[i
].bar_gc
, screens
[i
].root
,
8032 XCB_GC_GRAPHICS_EXPOSURES
, gcv
);
8034 /* set default cursor */
8035 xcb_change_window_attributes(conn
, screens
[i
].root
,
8038 /* init all workspaces */
8039 /* XXX these should be dynamically allocated too */
8040 for (j
= 0; j
< SWM_WS_MAX
; j
++) {
8041 ws
= &screens
[i
].ws
[j
];
8047 TAILQ_INIT(&ws
->winlist
);
8048 TAILQ_INIT(&ws
->unmanagedlist
);
8050 for (k
= 0; layouts
[k
].l_stack
!= NULL
; k
++)
8051 if (layouts
[k
].l_config
!= NULL
)
8052 layouts
[k
].l_config(ws
,
8053 SWM_ARG_ID_STACKINIT
);
8054 ws
->cur_layout
= &layouts
[0];
8055 ws
->cur_layout
->l_string(ws
);
8061 xcb_randr_select_input(conn
, screens
[i
].root
,
8062 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE
);
8064 xcb_free_cursor(conn
, cursor
);
8065 xcb_close_font(conn
, cursor_font
);
8071 if ((bar_fonts
= strdup(SWM_BAR_FONTS
)) == NULL
)
8072 err(1, "setup_globals: strdup: failed to allocate memory.");
8074 if ((clock_format
= strdup("%a %b %d %R %Z %Y")) == NULL
)
8075 err(1, "setup_globals: strdup: failed to allocate memory.");
8077 if ((syms
= xcb_key_symbols_alloc(conn
)) == NULL
)
8078 errx(1, "unable to allocate key symbols");
8080 a_state
= get_atom_from_string("WM_STATE");
8081 a_prot
= get_atom_from_string("WM_PROTOCOLS");
8082 a_delete
= get_atom_from_string("WM_DELETE_WINDOW");
8083 a_takefocus
= get_atom_from_string("WM_TAKE_FOCUS");
8084 a_wmname
= get_atom_from_string("WM_NAME");
8085 a_netwmname
= get_atom_from_string("_NET_WM_NAME");
8086 a_utf8_string
= get_atom_from_string("UTF8_STRING");
8087 a_string
= get_atom_from_string("STRING");
8088 a_swm_iconic
= get_atom_from_string("_SWM_ICONIC");
8089 a_swm_ws
= get_atom_from_string("_SWM_WS");
8096 xcb_atom_t netwmcheck
;
8097 xcb_window_t root
, win
;
8099 /* work around sun jdk bugs, code from wmname */
8100 netwmcheck
= get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
8102 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
8103 for (i
= 0; i
< num_screens
; i
++) {
8104 root
= screens
[i
].root
;
8106 win
= xcb_generate_id(conn
);
8107 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, win
, root
,
8108 0, 0, 1, 1, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
8109 XCB_COPY_FROM_PARENT
, 0, NULL
);
8111 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, root
,
8112 netwmcheck
, XCB_ATOM_WINDOW
, 32, 1, &win
);
8113 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
8114 netwmcheck
, XCB_ATOM_WINDOW
, 32, 1, &win
);
8115 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
8116 a_netwmname
, a_utf8_string
, 8, strlen("LG3D"), "LG3D");
8121 shutdown_cleanup(void)
8125 /* disable alarm because the following code may not be interrupted */
8127 if (signal(SIGALRM
, SIG_IGN
) == SIG_ERR
)
8128 err(1, "can't disable alarm");
8136 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
8137 for (i
= 0; i
< num_screens
; ++i
) {
8138 if (screens
[i
].bar_gc
!= 0)
8139 xcb_free_gc(conn
, screens
[i
].bar_gc
);
8140 if (!bar_font_legacy
)
8141 XftColorFree(display
, DefaultVisual(display
, i
),
8142 DefaultColormap(display
, i
), &bar_font_color
);
8145 if (bar_font_legacy
)
8146 XFreeFontSet(display
, bar_fs
);
8148 XftFontClose(display
, bar_font
);
8151 xcb_key_symbols_free(syms
);
8153 xcb_disconnect(conn
);
8157 event_error(xcb_generic_error_t
*e
)
8161 DNPRINTF(SWM_D_EVENT
, "event_error: %s(%u) from %s(%u), sequence: %u, "
8162 "resource_id: %u, minor_code: %u\n",
8163 xcb_event_get_error_label(e
->error_code
), e
->error_code
,
8164 xcb_event_get_request_label(e
->major_code
), e
->major_code
,
8165 e
->sequence
, e
->resource_id
, e
->minor_code
);
8169 event_handle(xcb_generic_event_t
*evt
)
8171 uint8_t type
= XCB_EVENT_RESPONSE_TYPE(evt
);
8173 DNPRINTF(SWM_D_EVENT
, "XCB Event: %s(%d)\n",
8174 xcb_event_get_label(XCB_EVENT_RESPONSE_TYPE(evt
)),
8175 XCB_EVENT_RESPONSE_TYPE(evt
));
8178 #define EVENT(type, callback) case type: callback((void *)evt); return
8179 EVENT(0, event_error
);
8180 EVENT(XCB_BUTTON_PRESS
, buttonpress
);
8181 /*EVENT(XCB_BUTTON_RELEASE, buttonpress);*/
8182 /*EVENT(XCB_CIRCULATE_NOTIFY, );*/
8183 /*EVENT(XCB_CIRCULATE_REQUEST, );*/
8184 EVENT(XCB_CLIENT_MESSAGE
, clientmessage
);
8185 /*EVENT(XCB_COLORMAP_NOTIFY, );*/
8186 EVENT(XCB_CONFIGURE_NOTIFY
, configurenotify
);
8187 EVENT(XCB_CONFIGURE_REQUEST
, configurerequest
);
8188 /*EVENT(XCB_CREATE_NOTIFY, );*/
8189 EVENT(XCB_DESTROY_NOTIFY
, destroynotify
);
8190 EVENT(XCB_ENTER_NOTIFY
, enternotify
);
8191 EVENT(XCB_EXPOSE
, expose
);
8193 EVENT(XCB_FOCUS_IN
, focusin
);
8195 /*EVENT(XCB_FOCUS_OUT, );*/
8196 /*EVENT(XCB_GRAPHICS_EXPOSURE, );*/
8197 /*EVENT(XCB_GRAVITY_NOTIFY, );*/
8198 EVENT(XCB_KEY_PRESS
, keypress
);
8199 /*EVENT(XCB_KEY_RELEASE, keypress);*/
8200 /*EVENT(XCB_KEYMAP_NOTIFY, );*/
8202 EVENT(XCB_LEAVE_NOTIFY
, leavenotify
);
8204 EVENT(XCB_MAP_NOTIFY
, mapnotify
);
8205 EVENT(XCB_MAP_REQUEST
, maprequest
);
8206 EVENT(XCB_MAPPING_NOTIFY
, mappingnotify
);
8207 /*EVENT(XCB_MOTION_NOTIFY, );*/
8208 /*EVENT(XCB_NO_EXPOSURE, );*/
8209 EVENT(XCB_PROPERTY_NOTIFY
, propertynotify
);
8210 /*EVENT(XCB_REPARENT_NOTIFY, );*/
8211 /*EVENT(XCB_RESIZE_REQUEST, );*/
8212 /*EVENT(XCB_SELECTION_CLEAR, );*/
8213 /*EVENT(XCB_SELECTION_NOTIFY, );*/
8214 /*EVENT(XCB_SELECTION_REQUEST, );*/
8215 EVENT(XCB_UNMAP_NOTIFY
, unmapnotify
);
8216 /*EVENT(XCB_VISIBILITY_NOTIFY, visibilitynotify);*/
8219 if (type
- xrandr_eventbase
== XCB_RANDR_SCREEN_CHANGE_NOTIFY
)
8220 screenchange((void *)evt
);
8224 main(int argc
, char *argv
[])
8226 struct swm_region
*r
, *rr
;
8227 struct ws_win
*winfocus
= NULL
;
8229 char conf
[PATH_MAX
], *cfile
= NULL
;
8231 int xfd
, i
, num_screens
;
8232 struct sigaction sact
;
8233 xcb_generic_event_t
*evt
;
8237 /* suppress unused warning since var is needed */
8240 time_started
= time(NULL
);
8243 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION
, buildstr
);
8244 if (!setlocale(LC_CTYPE
, "") || !setlocale(LC_TIME
, ""))
8245 warnx("no locale support");
8247 /* handle some signals */
8248 bzero(&sact
, sizeof(sact
));
8249 sigemptyset(&sact
.sa_mask
);
8251 sact
.sa_handler
= sighdlr
;
8252 sigaction(SIGINT
, &sact
, NULL
);
8253 sigaction(SIGQUIT
, &sact
, NULL
);
8254 sigaction(SIGTERM
, &sact
, NULL
);
8255 sigaction(SIGHUP
, &sact
, NULL
);
8257 sact
.sa_handler
= sighdlr
;
8258 sact
.sa_flags
= SA_NOCLDSTOP
;
8259 sigaction(SIGCHLD
, &sact
, NULL
);
8261 if (!(display
= XOpenDisplay(0)))
8262 errx(1, "can not open display");
8264 conn
= XGetXCBConnection(display
);
8265 if (xcb_connection_has_error(conn
))
8266 errx(1, "can not get XCB connection");
8268 XSetEventQueueOwner(display
, XCBOwnsEventQueue
);
8270 xcb_prefetch_extension_data(conn
, &xcb_randr_id
);
8271 xfd
= xcb_get_file_descriptor(conn
);
8273 /* look for local and global conf file */
8274 pwd
= getpwuid(getuid());
8276 errx(1, "invalid user: %d", getuid());
8278 xcb_grab_server(conn
);
8281 /* flush all events */
8282 while ((evt
= xcb_poll_for_event(conn
))) {
8283 if (XCB_EVENT_RESPONSE_TYPE(evt
) == 0)
8288 if (enable_wm() != 0)
8289 errx(1, "another window manager is currently running");
8300 for (i
= 0; ; i
++) {
8305 snprintf(conf
, sizeof conf
, "%s/.%s",
8306 pwd
->pw_dir
, SWM_CONF_FILE
);
8310 snprintf(conf
, sizeof conf
, "/etc/%s",
8315 snprintf(conf
, sizeof conf
, "%s/.%s",
8316 pwd
->pw_dir
, SWM_CONF_FILE_OLD
);
8320 snprintf(conf
, sizeof conf
, "/etc/%s",
8327 if (strlen(conf
) && stat(conf
, &sb
) != -1)
8328 if (S_ISREG(sb
.st_mode
)) {
8335 /* load conf (if any) */
8337 conf_load(cfile
, SWM_CONF_DEFAULT
);
8340 /* set some values to work around bad programs */
8342 /* grab existing windows (before we build the bars) */
8345 if (getenv("SWM_STARTED") == NULL
)
8346 setenv("SWM_STARTED", "YES", 1);
8348 /* setup all bars */
8349 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
8350 for (i
= 0; i
< num_screens
; i
++)
8351 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
8352 if (winfocus
== NULL
)
8353 winfocus
= TAILQ_FIRST(&r
->ws
->winlist
);
8362 xcb_ungrab_server(conn
);
8366 while ((evt
= xcb_poll_for_event(conn
))) {
8373 /* if we are being restarted go focus on first window */
8375 rr
= winfocus
->ws
->r
;
8377 /* not a visible window */
8381 /* move pointer to first screen if multi screen */
8382 if (num_screens
> 1 || outputs
> 1)
8383 xcb_warp_pointer(conn
, XCB_WINDOW_NONE
,
8384 rr
->s
[0].root
, 0, 0, 0, 0, X(rr
),
8385 Y(rr
) + (bar_enabled
? bar_height
: 0));
8387 a
.id
= SWM_ARG_ID_FOCUSCUR
;
8397 if (select(xfd
+ 1, &rd
, NULL
, NULL
, &tv
) == -1)
8398 if (errno
!= EINTR
) {
8399 DNPRINTF(SWM_D_MISC
, "select failed");
8401 if (restart_wm
== 1)
8402 restart(NULL
, NULL
);
8403 if (search_resp
== 1)