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
;
282 xcb_timestamp_t last_event_time
;
285 int cycle_visible
= 0;
287 int font_adjusted
= 0;
288 unsigned int mod_key
= MODKEY
;
291 struct swm_region
*search_r
;
292 int select_list_pipe
[2];
293 int select_resp_pipe
[2];
295 volatile sig_atomic_t search_resp
;
296 int search_resp_action
;
298 struct search_window
{
299 TAILQ_ENTRY(search_window
) entry
;
303 xcb_window_t indicator
;
305 TAILQ_HEAD(search_winlist
, search_window
);
306 struct search_winlist search_wl
;
311 SWM_SEARCH_UNICONIFY
,
312 SWM_SEARCH_NAME_WORKSPACE
,
313 SWM_SEARCH_SEARCH_WORKSPACE
,
314 SWM_SEARCH_SEARCH_WINDOW
317 #define SWM_STACK_TOP (0)
318 #define SWM_STACK_BOTTOM (1)
319 #define SWM_STACK_ABOVE (2)
320 #define SWM_STACK_BELOW (3)
323 double dialog_ratio
= 0.6;
325 #define SWM_BAR_MAX (256)
326 #define SWM_BAR_JUSTIFY_LEFT (0)
327 #define SWM_BAR_JUSTIFY_CENTER (1)
328 #define SWM_BAR_JUSTIFY_RIGHT (2)
329 #define SWM_BAR_OFFSET (4)
330 #define SWM_BAR_FONTS "-*-terminus-medium-*-*-*-12-*-*-*-*-*-*-*," \
331 "-*-profont-*-*-*-*-12-*-*-*-*-*-*-*," \
332 "-*-times-medium-r-*-*-12-*-*-*-*-*-*-*," \
333 "-misc-fixed-medium-r-*-*-12-*-*-*-*-*-*-*," \
334 "-*-*-*-r-*-*-*-*-*-*-*-*-*-*"
336 #ifdef X_HAVE_UTF8_STRING
337 #define DRAWSTRING(x...) Xutf8DrawString(x)
339 #define DRAWSTRING(x...) XmbDrawString(x)
342 char *bar_argv
[] = { NULL
, NULL
};
344 unsigned char bar_ext
[SWM_BAR_MAX
];
345 char bar_vertext
[SWM_BAR_MAX
];
347 sig_atomic_t bar_alarm
= 0;
350 int bar_border_width
= 1;
351 int bar_at_bottom
= 0;
353 int bar_extra_running
= 0;
356 int bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
357 char *bar_format
= NULL
;
358 int stack_enabled
= 1;
359 int clock_enabled
= 1;
360 int urgent_enabled
= 0;
361 char *clock_format
= NULL
;
362 int title_name_enabled
= 0;
363 int title_class_enabled
= 0;
364 int window_name_enabled
= 0;
365 int focus_mode
= SWM_FOCUS_DEFAULT
;
366 int focus_close
= SWM_STACK_BELOW
;
367 int focus_close_wrap
= 1;
368 int focus_default
= SWM_STACK_TOP
;
369 int spawn_position
= SWM_STACK_TOP
;
370 int disable_border
= 0;
371 int border_width
= 1;
372 int verbose_layout
= 0;
376 XFontSetExtents
*bar_fs_extents
;
378 int bar_font_legacy
= 1;
380 XftColor bar_font_color
;
383 /* layout manager data */
384 struct swm_geometry
{
397 struct swm_geometry g
;
400 /* virtual "screens" */
402 TAILQ_ENTRY(swm_region
) entry
;
403 struct swm_geometry g
;
404 struct workspace
*ws
; /* current workspace on this region */
405 struct workspace
*ws_prior
; /* prior workspace on this region */
406 struct swm_screen
*s
; /* screen idx */
409 TAILQ_HEAD(swm_region_list
, swm_region
);
412 TAILQ_ENTRY(ws_win
) entry
;
414 xcb_window_t transient
;
415 struct ws_win
*focus_child
; /* focus on child transient */
416 struct swm_geometry g
; /* current geometry */
417 struct swm_geometry g_float
; /* region coordinates */
418 int g_floatvalid
; /* g_float geometry validity */
419 int floatmaxed
; /* whether maxed by max_stack */
425 unsigned int ewmh_flags
;
426 int font_size_boundary
[SWM_MAX_FONT_STEPS
];
432 unsigned long quirks
;
433 struct workspace
*ws
; /* always valid */
434 struct swm_screen
*s
; /* always valid, never changes */
435 xcb_get_geometry_reply_t
*wa
;
437 xcb_icccm_get_wm_class_reply_t ch
;
438 xcb_icccm_wm_hints_t hints
;
440 TAILQ_HEAD(ws_win_list
, ws_win
);
444 TAILQ_ENTRY(pid_e
) entry
;
448 TAILQ_HEAD(pid_list
, pid_e
);
449 struct pid_list pidlist
= TAILQ_HEAD_INITIALIZER(pidlist
);
451 /* layout handlers */
453 void vertical_config(struct workspace
*, int);
454 void vertical_stack(struct workspace
*, struct swm_geometry
*);
455 void horizontal_config(struct workspace
*, int);
456 void horizontal_stack(struct workspace
*, struct swm_geometry
*);
457 void max_stack(struct workspace
*, struct swm_geometry
*);
458 void plain_stacker(struct workspace
*);
459 void fancy_stacker(struct workspace
*);
462 void (*l_stack
)(struct workspace
*, struct swm_geometry
*);
463 void (*l_config
)(struct workspace
*, int);
465 #define SWM_L_FOCUSPREV (1<<0)
466 #define SWM_L_MAPONFOCUS (1<<1)
467 void (*l_string
)(struct workspace
*);
469 /* stack, configure */
470 { vertical_stack
, vertical_config
, 0, plain_stacker
},
471 { horizontal_stack
, horizontal_config
, 0, plain_stacker
},
473 SWM_L_MAPONFOCUS
| SWM_L_FOCUSPREV
, plain_stacker
},
474 { NULL
, NULL
, 0, NULL
},
477 /* position of max_stack mode in the layouts array, index into layouts! */
478 #define SWM_V_STACK (0)
479 #define SWM_H_STACK (1)
480 #define SWM_MAX_STACK (2)
482 #define SWM_H_SLICE (32)
483 #define SWM_V_SLICE (32)
485 /* define work spaces */
487 int idx
; /* workspace index */
488 char *name
; /* workspace name */
489 int always_raise
; /* raise windows on focus */
490 struct layout
*cur_layout
; /* current layout handlers */
491 struct ws_win
*focus
; /* may be NULL */
492 struct ws_win
*focus_prev
; /* may be NULL */
493 struct swm_region
*r
; /* may be NULL */
494 struct swm_region
*old_r
; /* may be NULL */
495 struct ws_win_list winlist
; /* list of windows in ws */
496 struct ws_win_list unmanagedlist
; /* list of dead windows in ws */
497 char stacker
[10]; /* display stacker and layout */
501 int horizontal_msize
;
503 int horizontal_stacks
;
514 SWM_S_COLOR_BAR_BORDER
,
515 SWM_S_COLOR_BAR_FONT
,
521 /* physical screen mapping */
522 #define SWM_WS_MAX (22) /* hard limit */
523 int workspace_limit
= 10; /* soft limit */
526 int idx
; /* screen index */
527 struct swm_region_list rl
; /* list of regions on this screen */
528 struct swm_region_list orl
; /* list of old regions */
530 struct workspace ws
[SWM_WS_MAX
];
536 } c
[SWM_S_COLOR_MAX
];
538 xcb_gcontext_t bar_gc
;
541 struct swm_screen
*screens
;
543 /* args to functions */
546 #define SWM_ARG_ID_FOCUSNEXT (0)
547 #define SWM_ARG_ID_FOCUSPREV (1)
548 #define SWM_ARG_ID_FOCUSMAIN (2)
549 #define SWM_ARG_ID_SWAPNEXT (10)
550 #define SWM_ARG_ID_SWAPPREV (11)
551 #define SWM_ARG_ID_SWAPMAIN (12)
552 #define SWM_ARG_ID_MOVELAST (13)
553 #define SWM_ARG_ID_MASTERSHRINK (20)
554 #define SWM_ARG_ID_MASTERGROW (21)
555 #define SWM_ARG_ID_MASTERADD (22)
556 #define SWM_ARG_ID_MASTERDEL (23)
557 #define SWM_ARG_ID_FLIPLAYOUT (24)
558 #define SWM_ARG_ID_STACKRESET (30)
559 #define SWM_ARG_ID_STACKINIT (31)
560 #define SWM_ARG_ID_CYCLEWS_UP (40)
561 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
562 #define SWM_ARG_ID_CYCLESC_UP (42)
563 #define SWM_ARG_ID_CYCLESC_DOWN (43)
564 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
565 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
566 #define SWM_ARG_ID_STACKINC (50)
567 #define SWM_ARG_ID_STACKDEC (51)
568 #define SWM_ARG_ID_SS_ALL (60)
569 #define SWM_ARG_ID_SS_WINDOW (61)
570 #define SWM_ARG_ID_DONTCENTER (70)
571 #define SWM_ARG_ID_CENTER (71)
572 #define SWM_ARG_ID_KILLWINDOW (80)
573 #define SWM_ARG_ID_DELETEWINDOW (81)
574 #define SWM_ARG_ID_WIDTHGROW (90)
575 #define SWM_ARG_ID_WIDTHSHRINK (91)
576 #define SWM_ARG_ID_HEIGHTGROW (92)
577 #define SWM_ARG_ID_HEIGHTSHRINK (93)
578 #define SWM_ARG_ID_MOVEUP (100)
579 #define SWM_ARG_ID_MOVEDOWN (101)
580 #define SWM_ARG_ID_MOVELEFT (102)
581 #define SWM_ARG_ID_MOVERIGHT (103)
587 TAILQ_ENTRY(quirk
) entry
;
591 #define SWM_Q_FLOAT (1<<0) /* float this window */
592 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
593 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
594 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
595 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
596 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
598 TAILQ_HEAD(quirk_list
, quirk
);
599 struct quirk_list quirks
= TAILQ_HEAD_INITIALIZER(quirks
);
602 * Supported EWMH hints should be added to
603 * both the enum and the ewmh array
608 _NET_MOVERESIZE_WINDOW
,
609 _NET_WM_ACTION_ABOVE
,
610 _NET_WM_ACTION_CLOSE
,
611 _NET_WM_ACTION_FULLSCREEN
,
613 _NET_WM_ACTION_RESIZE
,
614 _NET_WM_ALLOWED_ACTIONS
,
618 _NET_WM_STATE_FULLSCREEN
,
619 _NET_WM_STATE_HIDDEN
,
620 _NET_WM_STATE_MAXIMIZED_HORZ
,
621 _NET_WM_STATE_MAXIMIZED_VERT
,
622 _NET_WM_STATE_SKIP_PAGER
,
623 _NET_WM_STATE_SKIP_TASKBAR
,
625 _NET_WM_WINDOW_TYPE_DIALOG
,
626 _NET_WM_WINDOW_TYPE_DOCK
,
627 _NET_WM_WINDOW_TYPE_NORMAL
,
628 _NET_WM_WINDOW_TYPE_SPLASH
,
629 _NET_WM_WINDOW_TYPE_TOOLBAR
,
630 _NET_WM_WINDOW_TYPE_UTILITY
,
631 _SWM_WM_STATE_MANUAL
,
638 } ewmh
[SWM_EWMH_HINT_MAX
] = {
639 /* must be in same order as in the enum */
640 {"_NET_ACTIVE_WINDOW", XCB_ATOM_NONE
},
641 {"_NET_CLOSE_WINDOW", XCB_ATOM_NONE
},
642 {"_NET_MOVERESIZE_WINDOW", XCB_ATOM_NONE
},
643 {"_NET_WM_ACTION_ABOVE", XCB_ATOM_NONE
},
644 {"_NET_WM_ACTION_CLOSE", XCB_ATOM_NONE
},
645 {"_NET_WM_ACTION_FULLSCREEN", XCB_ATOM_NONE
},
646 {"_NET_WM_ACTION_MOVE", XCB_ATOM_NONE
},
647 {"_NET_WM_ACTION_RESIZE", XCB_ATOM_NONE
},
648 {"_NET_WM_ALLOWED_ACTIONS", XCB_ATOM_NONE
},
649 {"_NET_WM_NAME", XCB_ATOM_NONE
},
650 {"_NET_WM_STATE", XCB_ATOM_NONE
},
651 {"_NET_WM_STATE_ABOVE", XCB_ATOM_NONE
},
652 {"_NET_WM_STATE_FULLSCREEN", XCB_ATOM_NONE
},
653 {"_NET_WM_STATE_HIDDEN", XCB_ATOM_NONE
},
654 {"_NET_WM_STATE_MAXIMIZED_HORZ", XCB_ATOM_NONE
},
655 {"_NET_WM_STATE_MAXIMIZED_VERT", XCB_ATOM_NONE
},
656 {"_NET_WM_STATE_SKIP_PAGER", XCB_ATOM_NONE
},
657 {"_NET_WM_STATE_SKIP_TASKBAR", XCB_ATOM_NONE
},
658 {"_NET_WM_WINDOW_TYPE", XCB_ATOM_NONE
},
659 {"_NET_WM_WINDOW_TYPE_DIALOG", XCB_ATOM_NONE
},
660 {"_NET_WM_WINDOW_TYPE_DOCK", XCB_ATOM_NONE
},
661 {"_NET_WM_WINDOW_TYPE_NORMAL", XCB_ATOM_NONE
},
662 {"_NET_WM_WINDOW_TYPE_SPLASH", XCB_ATOM_NONE
},
663 {"_NET_WM_WINDOW_TYPE_TOOLBAR", XCB_ATOM_NONE
},
664 {"_NET_WM_WINDOW_TYPE_UTILITY", XCB_ATOM_NONE
},
665 {"_SWM_WM_STATE_MANUAL", XCB_ATOM_NONE
},
668 /* function prototypes */
669 void buttonpress(xcb_button_press_event_t
*);
670 void check_conn(void);
671 void clientmessage(xcb_client_message_event_t
*);
672 int conf_load(char *, int);
673 void configurenotify(xcb_configure_notify_event_t
*);
674 void configurerequest(xcb_configure_request_event_t
*);
675 void constrain_window(struct ws_win
*, struct swm_region
*, int);
676 void destroynotify(xcb_destroy_notify_event_t
*);
677 void enternotify(xcb_enter_notify_event_t
*);
678 void event_drain(uint8_t);
679 void event_error(xcb_generic_error_t
*);
680 void event_handle(xcb_generic_event_t
*);
681 char *expand_tilde(char *);
682 void expose(xcb_expose_event_t
*);
683 struct ws_win
*find_window(xcb_window_t
);
684 int floating_toggle_win(struct ws_win
*);
685 void focus(struct swm_region
*, union arg
*);
686 void focus_flush(void);
687 struct ws_win
*get_focus_magic(struct ws_win
*);
688 struct ws_win
*get_focus_prev(struct ws_win
*);
689 void focus_win(struct ws_win
*);
691 void focusin(xcb_focus_in_event_t
*);
693 xcb_atom_t
get_atom_from_string(const char *);
695 char *get_atom_name(xcb_atom_t
);
696 char *get_notify_detail_label(uint8_t);
697 char *get_notify_mode_label(uint8_t);
699 struct ws_win
*get_region_focus(struct swm_region
*);
700 xcb_screen_t
*get_screen(int);
701 char *get_win_name(xcb_window_t
);
702 uint32_t getstate(xcb_window_t
);
703 void grabbuttons(struct ws_win
*);
704 void keypress(xcb_key_press_event_t
*);
706 void leavenotify(xcb_leave_notify_event_t
*);
708 void load_float_geom(struct ws_win
*, struct swm_region
*);
709 struct ws_win
*manage_window(xcb_window_t
, uint16_t);
710 void map_window(struct ws_win
*);
711 void mapnotify(xcb_map_notify_event_t
*);
712 void mappingnotify(xcb_mapping_notify_event_t
*);
713 void maprequest(xcb_map_request_event_t
*);
714 void new_region(struct swm_screen
*, int, int, int, int);
715 int parse_rgb(const char *, uint16_t *, uint16_t *, uint16_t *);
716 void propertynotify(xcb_property_notify_event_t
*);
717 void spawn_select(struct swm_region
*, union arg
*, char *, int *);
718 void screenchange(xcb_randr_screen_change_notify_event_t
*);
719 void shutdown_cleanup(void);
720 void store_float_geom(struct ws_win
*, struct swm_region
*);
721 void unmanage_window(struct ws_win
*);
722 void unmapnotify(xcb_unmap_notify_event_t
*);
723 void unfocus_win(struct ws_win
*);
724 void update_window(struct ws_win
*);
725 /*void visibilitynotify(xcb_visibility_notify_event_t *);*/
728 expand_tilde(char *s
)
737 errx(1, "expand_tilde: NULL string.");
746 if ((max
= sysconf(_SC_LOGIN_NAME_MAX
)) == -1)
747 errx(1, "expand_tilde: sysconf");
749 if ((user
= calloc(1, max
+ 1)) == NULL
)
750 errx(1, "expand_tilde: calloc");
752 for (i
= 0; s
[i
] != '/' && s
[i
] != '\0'; ++i
)
757 ppwd
= strlen(user
) == 0 ? getpwuid(getuid()) : getpwnam(user
);
761 if (asprintf(&result
, "%s%s", ppwd
->pw_dir
, s
) == -1)
765 errx(1, "expand_tilde: failed to allocate memory.");
771 parse_rgb(const char *rgb
, uint16_t *rr
, uint16_t *gg
, uint16_t *bb
)
773 unsigned int tmpr
, tmpg
, tmpb
;
775 if (sscanf(rgb
, "rgb:%x/%x/%x", &tmpr
, &tmpg
, &tmpb
) != 3)
778 *rr
= RGB_8_TO_16(tmpr
);
779 *gg
= RGB_8_TO_16(tmpg
);
780 *bb
= RGB_8_TO_16(tmpb
);
786 get_screen(int screen
)
788 const xcb_setup_t
*r
;
789 xcb_screen_iterator_t iter
;
791 if ((r
= xcb_get_setup(conn
)) == NULL
) {
792 DNPRINTF(SWM_D_MISC
, "get_screen: xcb_get_setup\n");
796 iter
= xcb_setup_roots_iterator(r
);
797 for (; iter
.rem
; --screen
, xcb_screen_next(&iter
))
807 if (focus_mode
== SWM_FOCUS_DEFAULT
)
808 event_drain(XCB_ENTER_NOTIFY
);
814 get_atom_from_string(const char *str
)
816 xcb_intern_atom_cookie_t c
;
817 xcb_intern_atom_reply_t
*r
;
820 c
= xcb_intern_atom(conn
, 0, strlen(str
), str
);
821 r
= xcb_intern_atom_reply(conn
, c
, NULL
);
829 return (XCB_ATOM_NONE
);
833 update_iconic(struct ws_win
*win
, int newv
)
840 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
841 a_swm_iconic
, XCB_ATOM_INTEGER
, 32, 1, &v
);
843 xcb_delete_property(conn
, win
->id
, a_swm_iconic
);
847 get_swm_iconic(struct ws_win
*win
)
850 xcb_get_property_reply_t
*pr
= NULL
;
853 pr
= xcb_get_property_reply(conn
,
854 xcb_get_property(conn
, 0, win
->id
, a_swm_iconic
,
855 XCB_ATOM_INTEGER
, 0, 1), NULL
);
858 if (pr
->type
!= XCB_ATOM_INTEGER
|| pr
->format
!= 32)
860 v
= *((int32_t *)xcb_get_property_value(pr
));
871 int i
, j
, num_screens
;
873 sup_list
= get_atom_from_string("_NET_SUPPORTED");
875 for (i
= 0; i
< LENGTH(ewmh
); i
++)
876 ewmh
[i
].atom
= get_atom_from_string(ewmh
[i
].name
);
878 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
879 for (i
= 0; i
< num_screens
; i
++) {
880 /* Support check window will be created by workaround(). */
882 /* Report supported atoms */
883 xcb_delete_property(conn
, screens
[i
].root
, sup_list
);
884 for (j
= 0; j
< LENGTH(ewmh
); j
++)
885 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
,
886 screens
[i
].root
, sup_list
, XCB_ATOM_ATOM
, 32, 1,
895 xcb_atom_t sup_check
, sup_list
;
897 xcb_get_property_cookie_t pc
;
898 xcb_get_property_reply_t
*pr
;
900 sup_check
= get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
901 sup_list
= get_atom_from_string("_NET_SUPPORTED");
902 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
904 for (i
= 0; i
< num_screens
; i
++) {
905 /* Get the support check window and destroy it */
906 pc
= xcb_get_property(conn
, 0, screens
[i
].root
, sup_check
,
907 XCB_ATOM_WINDOW
, 0, 1);
908 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
911 if (pr
->format
== sup_check
) {
912 id
= *((xcb_window_t
*)xcb_get_property_value(pr
));
914 xcb_destroy_window(conn
, id
);
915 xcb_delete_property(conn
, screens
[i
].root
, sup_check
);
916 xcb_delete_property(conn
, screens
[i
].root
, sup_list
);
923 ewmh_autoquirk(struct ws_win
*win
)
927 xcb_get_property_cookie_t c
;
928 xcb_get_property_reply_t
*r
;
930 c
= xcb_get_property(conn
, 0, win
->id
,
931 ewmh
[_NET_WM_WINDOW_TYPE
].atom
, XCB_ATOM_ATOM
, 0, UINT32_MAX
);
932 r
= xcb_get_property_reply(conn
, c
, NULL
);
936 n
= xcb_get_property_value_length(r
);
937 type
= xcb_get_property_value(r
);
939 for (i
= 0; i
< n
; i
++) {
940 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_NORMAL
].atom
)
942 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_DOCK
].atom
||
943 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_TOOLBAR
].atom
||
944 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_UTILITY
].atom
) {
946 win
->quirks
= SWM_Q_FLOAT
| SWM_Q_ANYWHERE
;
949 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_SPLASH
].atom
||
950 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_DIALOG
].atom
) {
952 win
->quirks
= SWM_Q_FLOAT
;
959 #define SWM_EWMH_ACTION_COUNT_MAX (6)
960 #define EWMH_F_FULLSCREEN (1<<0)
961 #define EWMH_F_ABOVE (1<<1)
962 #define EWMH_F_HIDDEN (1<<2)
963 #define EWMH_F_SKIP_PAGER (1<<3)
964 #define EWMH_F_SKIP_TASKBAR (1<<4)
965 #define SWM_F_MANUAL (1<<5)
968 ewmh_set_win_fullscreen(struct ws_win
*win
, int fs
)
976 DNPRINTF(SWM_D_MISC
, "ewmh_set_win_fullscreen: window: 0x%x, "
977 "fullscreen %s\n", win
->id
, YESNO(fs
));
980 if (!win
->g_floatvalid
)
981 store_float_geom(win
, win
->ws
->r
);
983 win
->g
= win
->ws
->r
->g
;
986 load_float_geom(win
, win
->ws
->r
);
993 ewmh_update_actions(struct ws_win
*win
)
995 xcb_atom_t actions
[SWM_EWMH_ACTION_COUNT_MAX
];
1001 actions
[n
++] = ewmh
[_NET_WM_ACTION_CLOSE
].atom
;
1003 if (win
->floating
) {
1004 actions
[n
++] = ewmh
[_NET_WM_ACTION_MOVE
].atom
;
1005 actions
[n
++] = ewmh
[_NET_WM_ACTION_RESIZE
].atom
;
1006 actions
[n
++] = ewmh
[_NET_WM_ACTION_ABOVE
].atom
;
1009 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
1010 ewmh
[_NET_WM_ALLOWED_ACTIONS
].atom
, XCB_ATOM_ATOM
, 32, 1, actions
);
1013 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
1014 #define _NET_WM_STATE_ADD 1 /* add/set property */
1015 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
1018 ewmh_update_win_state(struct ws_win
*win
, xcb_atom_t state
, long action
)
1020 unsigned int mask
= 0;
1021 unsigned int changed
= 0;
1022 unsigned int orig_flags
;
1027 DNPRINTF(SWM_D_PROP
, "ewmh_update_win_state: window: 0x%x, state: %ld, "
1028 "action: %ld\n", win
->id
, (unsigned long)state
, action
);
1030 if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
)
1031 mask
= EWMH_F_FULLSCREEN
;
1032 else if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
)
1033 mask
= EWMH_F_ABOVE
;
1034 else if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
)
1035 mask
= SWM_F_MANUAL
;
1036 else if (state
== ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
)
1037 mask
= EWMH_F_SKIP_PAGER
;
1038 else if (state
== ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
)
1039 mask
= EWMH_F_SKIP_TASKBAR
;
1041 orig_flags
= win
->ewmh_flags
;
1044 case _NET_WM_STATE_REMOVE
:
1045 win
->ewmh_flags
&= ~mask
;
1047 case _NET_WM_STATE_ADD
:
1048 win
->ewmh_flags
|= mask
;
1050 case _NET_WM_STATE_TOGGLE
:
1051 win
->ewmh_flags
^= mask
;
1055 changed
= (win
->ewmh_flags
& mask
) ^ (orig_flags
& mask
) ? 1 : 0;
1057 if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
) {
1058 if (changed
&& !floating_toggle_win(win
))
1059 win
->ewmh_flags
= orig_flags
; /* revert */
1060 } else if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
) {
1062 win
->manual
= (win
->ewmh_flags
& SWM_F_MANUAL
) != 0;
1063 } else if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
) {
1064 if (changed
&& !ewmh_set_win_fullscreen(win
,
1065 win
->ewmh_flags
& EWMH_F_FULLSCREEN
))
1066 win
->ewmh_flags
= orig_flags
; /* revert */
1069 xcb_delete_property(conn
, win
->id
, ewmh
[_NET_WM_STATE
].atom
);
1071 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
1072 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1073 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1074 &ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
);
1075 else if (win
->ewmh_flags
& EWMH_F_SKIP_PAGER
)
1076 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1077 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1078 &ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
);
1079 else if (win
->ewmh_flags
& EWMH_F_SKIP_TASKBAR
)
1080 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1081 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1082 &ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
);
1083 else if (win
->ewmh_flags
& EWMH_F_ABOVE
)
1084 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1085 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1086 &ewmh
[_NET_WM_STATE_ABOVE
].atom
);
1087 else if (win
->ewmh_flags
& SWM_F_MANUAL
)
1088 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1089 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1090 &ewmh
[_SWM_WM_STATE_MANUAL
].atom
);
1094 ewmh_get_win_state(struct ws_win
*win
)
1097 xcb_get_property_cookie_t c
;
1098 xcb_get_property_reply_t
*r
;
1104 win
->ewmh_flags
= 0;
1106 win
->ewmh_flags
|= EWMH_F_ABOVE
;
1108 win
->ewmh_flags
|= SWM_F_MANUAL
;
1110 c
= xcb_get_property(conn
, 0, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
1111 XCB_ATOM_ATOM
, 0, UINT32_MAX
);
1112 r
= xcb_get_property_reply(conn
, c
, NULL
);
1116 states
= xcb_get_property_value(r
);
1117 n
= xcb_get_property_value_length(r
);
1119 for (i
= 0; i
< n
; i
++)
1120 ewmh_update_win_state(win
, states
[i
], _NET_WM_STATE_ADD
);
1128 dumpwins(struct swm_region
*r
, union arg
*args
)
1132 xcb_get_window_attributes_cookie_t c
;
1133 xcb_get_window_attributes_reply_t
*wa
;
1135 /* suppress unused warning since var is needed */
1138 if (r
->ws
== NULL
) {
1139 warnx("dumpwins: invalid workspace");
1143 warnx("=== managed window list ws %02d ===", r
->ws
->idx
);
1144 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
1145 state
= getstate(win
->id
);
1146 c
= xcb_get_window_attributes(conn
, win
->id
);
1147 wa
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
1149 warnx("window: 0x%x, map_state: %d, state: %u, "
1150 "transient: 0x%x", win
->id
, wa
->map_state
,
1151 state
, win
->transient
);
1154 warnx("window: 0x%x, failed xcb_get_window_attributes",
1158 warnx("===== unmanaged window list =====");
1159 TAILQ_FOREACH(win
, &r
->ws
->unmanagedlist
, entry
) {
1160 state
= getstate(win
->id
);
1161 c
= xcb_get_window_attributes(conn
, win
->id
);
1162 wa
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
1164 warnx("window: 0x%x, map_state: %d, state: %u, "
1165 "transient: 0x%x", win
->id
, wa
->map_state
,
1166 state
, win
->transient
);
1169 warnx("window: 0x%x, failed xcb_get_window_attributes",
1173 warnx("=================================");
1177 dumpwins(struct swm_region
*r
, union arg
*s
)
1182 #endif /* SWM_DEBUG */
1187 int saved_errno
, status
;
1190 saved_errno
= errno
;
1194 while ((pid
= waitpid(WAIT_ANY
, &status
, WNOHANG
)) != 0) {
1199 if (errno
!= ECHILD
)
1200 warn("sighdlr: waitpid");
1201 #endif /* SWM_DEBUG */
1204 if (pid
== searchpid
)
1208 if (WIFEXITED(status
)) {
1209 if (WEXITSTATUS(status
) != 0)
1210 warnx("sighdlr: child exit status: %d",
1211 WEXITSTATUS(status
));
1213 warnx("sighdlr: child is terminated "
1215 #endif /* SWM_DEBUG */
1229 errno
= saved_errno
;
1235 struct pid_e
*p
= NULL
;
1237 DNPRINTF(SWM_D_MISC
, "find_pid: %d\n", pid
);
1242 TAILQ_FOREACH(p
, &pidlist
, entry
) {
1251 name_to_pixel(const char *colorname
)
1253 uint32_t result
= 0;
1254 char cname
[32] = "#";
1255 xcb_screen_t
*screen
;
1256 xcb_colormap_t cmap
;
1257 xcb_alloc_color_reply_t
*cr
;
1258 xcb_alloc_named_color_reply_t
*nr
;
1259 uint16_t rr
, gg
, bb
;
1261 screen
= xcb_setup_roots_iterator(xcb_get_setup(conn
)).data
;
1262 cmap
= screen
->default_colormap
;
1264 /* color is in format rgb://rr/gg/bb */
1265 if (strncmp(colorname
, "rgb:", 4) == 0) {
1266 if (parse_rgb(colorname
, &rr
, &gg
, &bb
) == -1)
1267 warnx("could not parse rgb %s", colorname
);
1269 cr
= xcb_alloc_color_reply(conn
,
1270 xcb_alloc_color(conn
, cmap
, rr
, gg
, bb
),
1276 warnx("color '%s' not found", colorname
);
1279 nr
= xcb_alloc_named_color_reply(conn
,
1280 xcb_alloc_named_color(conn
, cmap
, strlen(colorname
),
1283 strlcat(cname
, colorname
+ 2, sizeof cname
- 1);
1284 nr
= xcb_alloc_named_color_reply(conn
,
1285 xcb_alloc_named_color(conn
, cmap
, strlen(cname
),
1292 warnx("color '%s' not found", colorname
);
1299 setscreencolor(char *val
, int i
, int c
)
1303 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1304 if (i
> 0 && i
<= num_screens
) {
1305 screens
[i
- 1].c
[c
].pixel
= name_to_pixel(val
);
1306 free(screens
[i
- 1].c
[c
].name
);
1307 if ((screens
[i
- 1].c
[c
].name
= strdup(val
)) == NULL
)
1309 } else if (i
== -1) {
1310 for (i
= 0; i
< num_screens
; i
++) {
1311 screens
[i
].c
[c
].pixel
= name_to_pixel(val
);
1312 free(screens
[i
].c
[c
].name
);
1313 if ((screens
[i
].c
[c
].name
= strdup(val
)) == NULL
)
1317 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1322 fancy_stacker(struct workspace
*ws
)
1324 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1325 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1326 snprintf(ws
->stacker
, sizeof ws
->stacker
,
1327 ws
->l_state
.vertical_flip
? "[%d>%d]" : "[%d|%d]",
1328 ws
->l_state
.vertical_mwin
, ws
->l_state
.vertical_stacks
);
1329 if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1330 snprintf(ws
->stacker
, sizeof ws
->stacker
,
1331 ws
->l_state
.horizontal_flip
? "[%dv%d]" : "[%d-%d]",
1332 ws
->l_state
.horizontal_mwin
, ws
->l_state
.horizontal_stacks
);
1336 plain_stacker(struct workspace
*ws
)
1338 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1339 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1340 strlcpy(ws
->stacker
, ws
->l_state
.vertical_flip
? "[>]" : "[|]",
1341 sizeof ws
->stacker
);
1342 if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1343 strlcpy(ws
->stacker
, ws
->l_state
.horizontal_flip
? "[v]" : "[-]",
1344 sizeof ws
->stacker
);
1348 custom_region(char *val
)
1350 unsigned int x
, y
, w
, h
;
1351 int sidx
, num_screens
;
1352 xcb_screen_t
*screen
;
1354 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1355 if (sscanf(val
, "screen[%d]:%ux%u+%u+%u", &sidx
, &w
, &h
, &x
, &y
) != 5)
1356 errx(1, "invalid custom region, "
1357 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1358 if (sidx
< 1 || sidx
> num_screens
)
1359 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1363 if ((screen
= get_screen(sidx
)) == NULL
)
1364 errx(1, "ERROR: can't get screen %d.", sidx
);
1367 errx(1, "region %ux%u+%u+%u too small", w
, h
, x
, y
);
1369 if (x
> screen
->width_in_pixels
||
1370 y
> screen
->height_in_pixels
||
1371 w
+ x
> screen
->width_in_pixels
||
1372 h
+ y
> screen
->height_in_pixels
) {
1373 warnx("ignoring region %ux%u+%u+%u - not within screen "
1374 "boundaries (%ux%u)", w
, h
, x
, y
,
1375 screen
->width_in_pixels
, screen
->height_in_pixels
);
1379 new_region(&screens
[sidx
], x
, y
, w
, h
);
1383 socket_setnonblock(int fd
)
1387 if ((flags
= fcntl(fd
, F_GETFL
, 0)) == -1)
1388 err(1, "fcntl F_GETFL");
1389 flags
|= O_NONBLOCK
;
1390 if ((flags
= fcntl(fd
, F_SETFL
, flags
)) == -1)
1391 err(1, "fcntl F_SETFL");
1395 bar_print_legacy(struct swm_region
*r
, const char *s
)
1397 xcb_rectangle_t rect
;
1402 XRectangle ibox
, lbox
;
1406 XmbTextExtents(bar_fs
, s
, len
, &ibox
, &lbox
);
1408 switch (bar_justify
) {
1409 case SWM_BAR_JUSTIFY_LEFT
:
1412 case SWM_BAR_JUSTIFY_CENTER
:
1413 x
= (WIDTH(r
) - lbox
.width
) / 2;
1415 case SWM_BAR_JUSTIFY_RIGHT
:
1416 x
= WIDTH(r
) - lbox
.width
- SWM_BAR_OFFSET
;
1420 if (x
< SWM_BAR_OFFSET
)
1425 rect
.width
= WIDTH(r
->bar
);
1426 rect
.height
= HEIGHT(r
->bar
);
1428 /* clear back buffer */
1429 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
1430 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
1431 xcb_poly_fill_rectangle(conn
, r
->bar
->buffer
, r
->s
->bar_gc
, 1, &rect
);
1433 /* draw back buffer */
1434 gcvd
.graphics_exposures
= 0;
1435 draw
= XCreateGC(display
, r
->bar
->buffer
, GCGraphicsExposures
, &gcvd
);
1436 XSetForeground(display
, draw
, r
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
);
1437 DRAWSTRING(display
, r
->bar
->buffer
, bar_fs
, draw
,
1438 x
, (bar_fs_extents
->max_logical_extent
.height
- lbox
.height
) / 2 -
1440 XFreeGC(display
, draw
);
1443 xcb_copy_area(conn
, r
->bar
->buffer
, r
->bar
->id
, r
->s
->bar_gc
, 0, 0,
1444 0, 0, WIDTH(r
->bar
), HEIGHT(r
->bar
));
1448 bar_print(struct swm_region
*r
, const char *s
)
1451 xcb_rectangle_t rect
;
1459 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
, &info
);
1461 switch (bar_justify
) {
1462 case SWM_BAR_JUSTIFY_LEFT
:
1465 case SWM_BAR_JUSTIFY_CENTER
:
1466 x
= (WIDTH(r
) - info
.width
) / 2;
1468 case SWM_BAR_JUSTIFY_RIGHT
:
1469 x
= WIDTH(r
) - info
.width
- SWM_BAR_OFFSET
;
1473 if (x
< SWM_BAR_OFFSET
)
1478 rect
.width
= WIDTH(r
->bar
);
1479 rect
.height
= HEIGHT(r
->bar
);
1481 /* clear back buffer */
1482 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
1483 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
1484 xcb_poly_fill_rectangle(conn
, r
->bar
->buffer
, r
->s
->bar_gc
, 1, &rect
);
1486 /* draw back buffer */
1487 draw
= XftDrawCreate(display
, r
->bar
->buffer
,
1488 DefaultVisual(display
, r
->s
->idx
),
1489 DefaultColormap(display
, r
->s
->idx
));
1491 XftDrawStringUtf8(draw
, &bar_font_color
, bar_font
, x
,
1492 (HEIGHT(r
->bar
) + bar_font
->height
) / 2 - bar_font
->descent
,
1495 XftDrawDestroy(draw
);
1498 xcb_copy_area(conn
, r
->bar
->buffer
, r
->bar
->id
, r
->s
->bar_gc
, 0, 0,
1499 0, 0, WIDTH(r
->bar
), HEIGHT(r
->bar
));
1503 bar_extra_stop(void)
1507 bzero(bar_pipe
, sizeof bar_pipe
);
1510 kill(bar_pid
, SIGTERM
);
1513 strlcpy((char *)bar_ext
, "", sizeof bar_ext
);
1518 bar_class_name(char *s
, size_t sz
, struct swm_region
*r
)
1520 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1522 if (r
->ws
->focus
->ch
.class_name
!= NULL
)
1523 strlcat(s
, r
->ws
->focus
->ch
.class_name
, sz
);
1527 bar_title_name(char *s
, size_t sz
, struct swm_region
*r
)
1529 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1531 if (r
->ws
->focus
->ch
.instance_name
!= NULL
)
1532 strlcat(s
, r
->ws
->focus
->ch
.instance_name
, sz
);
1536 bar_class_title_name(char *s
, size_t sz
, struct swm_region
*r
)
1538 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1541 bar_class_name(s
, sz
, r
);
1542 strlcat(s
, ":", sz
);
1543 bar_title_name(s
, sz
, r
);
1547 bar_window_float(char *s
, size_t sz
, struct swm_region
*r
)
1549 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1551 if (r
->ws
->focus
->floating
)
1552 strlcat(s
, "(f)", sz
);
1556 bar_window_name(char *s
, size_t sz
, struct swm_region
*r
)
1560 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1562 if ((title
= get_win_name(r
->ws
->focus
->id
)) == NULL
)
1565 strlcat(s
, title
, sz
);
1569 int urgent
[SWM_WS_MAX
];
1571 bar_urgent(char *s
, size_t sz
)
1574 int i
, j
, num_screens
;
1576 xcb_get_property_cookie_t c
;
1577 xcb_icccm_wm_hints_t hints
;
1579 for (i
= 0; i
< workspace_limit
; i
++)
1582 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1583 for (i
= 0; i
< num_screens
; i
++)
1584 for (j
= 0; j
< workspace_limit
; j
++)
1585 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
) {
1586 c
= xcb_icccm_get_wm_hints(conn
, win
->id
);
1587 if (xcb_icccm_get_wm_hints_reply(conn
, c
,
1590 if (hints
.flags
& XCB_ICCCM_WM_HINT_X_URGENCY
)
1594 for (i
= 0; i
< workspace_limit
; i
++) {
1596 snprintf(b
, sizeof b
, "%d ", i
+ 1);
1598 snprintf(b
, sizeof b
, "- ");
1604 bar_workspace_name(char *s
, size_t sz
, struct swm_region
*r
)
1606 if (r
== NULL
|| r
->ws
== NULL
)
1608 if (r
->ws
->name
!= NULL
)
1609 strlcat(s
, r
->ws
->name
, sz
);
1612 /* build the default bar format according to the defined enabled options */
1614 bar_fmt(const char *fmtexp
, char *fmtnew
, struct swm_region
*r
, size_t sz
)
1616 /* if format provided, just copy the buffers */
1617 if (bar_format
!= NULL
) {
1618 strlcpy(fmtnew
, fmtexp
, sz
);
1622 /* reset the output buffer */
1625 strlcat(fmtnew
, "+N:+I ", sz
);
1627 strlcat(fmtnew
, "+S", sz
);
1628 strlcat(fmtnew
, " ", sz
);
1630 /* only show the workspace name if there's actually one */
1631 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->name
!= NULL
)
1632 strlcat(fmtnew
, "<+D>", sz
);
1633 strlcat(fmtnew
, "+3<", sz
);
1635 if (clock_enabled
) {
1636 strlcat(fmtnew
, fmtexp
, sz
);
1637 strlcat(fmtnew
, "+4<", sz
);
1640 /* bar_urgent already adds the space before the last asterisk */
1642 strlcat(fmtnew
, "* +U*+4<", sz
);
1644 if (title_class_enabled
) {
1645 strlcat(fmtnew
, "+C", sz
);
1646 if (title_name_enabled
== 0)
1647 strlcat(fmtnew
, "+4<", sz
);
1650 /* checks needed by the colon and floating strlcat(3) calls below */
1651 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->focus
!= NULL
) {
1652 if (title_name_enabled
) {
1653 if (title_class_enabled
)
1654 strlcat(fmtnew
, ":", sz
);
1655 strlcat(fmtnew
, "+T+4<", sz
);
1657 if (window_name_enabled
) {
1658 if (r
->ws
->focus
->floating
)
1659 strlcat(fmtnew
, "+F ", sz
);
1660 strlcat(fmtnew
, "+64W ", sz
);
1664 /* finally add the action script output and the version */
1665 strlcat(fmtnew
, "+4<+A+4<+V", sz
);
1669 bar_replace_pad(char *tmp
, int *limit
, size_t sz
)
1671 /* special case; no limit given, pad one space, instead */
1672 if (*limit
== (int)sz
- 1)
1674 snprintf(tmp
, sz
, "%*s", *limit
, " ");
1677 /* replaces the bar format character sequences (like in tmux(1)) */
1679 bar_replace_seq(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t *offrep
,
1683 char tmp
[SWM_BAR_MAX
];
1687 /* reset strlcat(3) buffer */
1690 /* get number, if any */
1693 if (sscanf(fmt
, "%d%n", &limit
, &size
) != 1)
1694 limit
= sizeof tmp
- 1;
1695 if (limit
<= 0 || limit
>= (int)sizeof tmp
)
1696 limit
= sizeof tmp
- 1;
1698 /* there is nothing to replace (ie EOL) */
1705 bar_replace_pad(tmp
, &limit
, sizeof tmp
);
1708 snprintf(tmp
, sizeof tmp
, "%s", bar_ext
);
1711 bar_class_name(tmp
, sizeof tmp
, r
);
1714 bar_workspace_name(tmp
, sizeof tmp
, r
);
1717 bar_window_float(tmp
, sizeof tmp
, r
);
1720 snprintf(tmp
, sizeof tmp
, "%d", r
->ws
->idx
+ 1);
1723 snprintf(tmp
, sizeof tmp
, "%d", r
->s
->idx
+ 1);
1726 bar_class_title_name(tmp
, sizeof tmp
, r
);
1729 snprintf(tmp
, sizeof tmp
, "%s", r
->ws
->stacker
);
1732 bar_title_name(tmp
, sizeof tmp
, r
);
1735 bar_urgent(tmp
, sizeof tmp
);
1738 snprintf(tmp
, sizeof tmp
, "%s", bar_vertext
);
1741 bar_window_name(tmp
, sizeof tmp
, r
);
1744 /* unknown character sequence; copy as-is */
1745 snprintf(tmp
, sizeof tmp
, "+%c", *fmt
);
1751 if ((int)len
< limit
)
1753 while (limit
-- > 0) {
1754 if (*offrep
>= sz
- 1)
1756 fmtrep
[(*offrep
)++] = *ptr
++;
1764 bar_replace(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t sz
)
1769 while (*fmt
!= '\0') {
1771 /* skip ordinary characters */
1774 fmtrep
[off
++] = *fmt
++;
1778 /* character sequence found; replace it */
1779 fmt
= bar_replace_seq(fmt
, fmtrep
, r
, &off
, sz
);
1788 bar_fmt_expand(char *fmtexp
, size_t sz
)
1795 /* start by grabbing the current time and date */
1797 localtime_r(&tmt
, &tm
);
1799 /* figure out what to expand */
1800 if (bar_format
!= NULL
)
1802 else if (bar_format
== NULL
&& clock_enabled
)
1804 /* if nothing to expand bail out */
1810 /* copy as-is, just in case the format shouldn't be expanded below */
1811 strlcpy(fmtexp
, fmt
, sz
);
1812 /* finally pass the string through strftime(3) */
1813 #ifndef SWM_DENY_CLOCK_FORMAT
1814 if ((len
= strftime(fmtexp
, sz
, fmt
, &tm
)) == 0)
1815 warnx("format too long");
1823 char fmtexp
[SWM_BAR_MAX
], fmtnew
[SWM_BAR_MAX
];
1824 char fmtrep
[SWM_BAR_MAX
];
1826 struct swm_region
*r
;
1828 /* expand the format by first passing it through strftime(3) */
1829 bar_fmt_expand(fmtexp
, sizeof fmtexp
);
1831 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1832 for (i
= 0; i
< num_screens
; i
++) {
1833 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
1836 bar_fmt(fmtexp
, fmtnew
, r
, sizeof fmtnew
);
1837 bar_replace(fmtnew
, fmtrep
, r
, sizeof fmtrep
);
1838 if (bar_font_legacy
)
1839 bar_print_legacy(r
, fmtrep
);
1841 bar_print(r
, fmtrep
);
1852 if (bar_enabled
== 0)
1854 if (bar_extra
&& bar_extra_running
) {
1855 /* ignore short reads; it'll correct itself */
1856 while ((b
= fgetln(stdin
, &len
)) != NULL
)
1857 if (b
&& b
[len
- 1] == '\n') {
1859 strlcpy((char *)bar_ext
, b
, sizeof bar_ext
);
1861 if (b
== NULL
&& errno
!= EAGAIN
) {
1862 warn("bar_update: bar_extra failed");
1866 strlcpy((char *)bar_ext
, "", sizeof bar_ext
);
1875 /* suppress unused warning since var is needed */
1882 bar_toggle(struct swm_region
*r
, union arg
*args
)
1884 struct swm_region
*tmpr
;
1887 /* suppress unused warnings since vars are needed */
1891 DNPRINTF(SWM_D_BAR
, "bar_toggle\n");
1893 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1895 for (i
= 0; i
< num_screens
; i
++)
1896 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
1898 xcb_unmap_window(conn
, tmpr
->bar
->id
);
1900 for (i
= 0; i
< num_screens
; i
++)
1901 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
1903 xcb_map_window(conn
, tmpr
->bar
->id
);
1906 bar_enabled
= !bar_enabled
;
1910 /* must be after stack */
1919 struct swm_region
*r
;
1923 /* do this here because the conf file is in memory */
1924 if (bar_extra
&& bar_extra_running
== 0 && bar_argv
[0]) {
1925 /* launch external status app */
1926 bar_extra_running
= 1;
1927 if (pipe(bar_pipe
) == -1)
1928 err(1, "pipe error");
1929 socket_setnonblock(bar_pipe
[0]);
1930 socket_setnonblock(bar_pipe
[1]); /* XXX hmmm, really? */
1931 if (dup2(bar_pipe
[0], 0) == -1)
1933 if (dup2(bar_pipe
[1], 1) == -1)
1935 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
1936 err(1, "could not disable SIGPIPE");
1937 switch (bar_pid
= fork()) {
1939 err(1, "cannot fork");
1943 execvp(bar_argv
[0], bar_argv
);
1944 err(1, "%s external app failed", bar_argv
[0]);
1946 default: /* parent */
1952 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1953 for (i
= 0; i
< num_screens
; i
++)
1954 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
1957 wa
[0] = screens
[i
].c
[SWM_S_COLOR_BAR
].pixel
;
1958 wa
[1] = screens
[i
].c
[SWM_S_COLOR_BAR_BORDER
].pixel
;
1959 xcb_change_window_attributes(conn
, r
->bar
->id
,
1960 XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
, wa
);
1970 while ((s
= index(s
, '-'))) {
1975 return (count
== 14);
1981 char *default_string
;
1982 char **missing_charsets
;
1983 int num_missing_charsets
= 0;
1987 XFreeFontSet(display
, bar_fs
);
1991 DNPRINTF(SWM_D_INIT
, "fontset_init: loading bar_fonts: %s\n", bar_fonts
);
1993 bar_fs
= XCreateFontSet(display
, bar_fonts
, &missing_charsets
,
1994 &num_missing_charsets
, &default_string
);
1996 if (num_missing_charsets
> 0) {
1997 warnx("Unable to load charset(s):");
1999 for (i
= 0; i
< num_missing_charsets
; ++i
)
2000 warnx("%s", missing_charsets
[i
]);
2002 XFreeStringList(missing_charsets
);
2004 if (strcmp(default_string
, ""))
2005 warnx("Glyphs from those sets will be replaced "
2006 "by '%s'.", default_string
);
2008 warnx("Glyphs from those sets won't be drawn.");
2012 errx(1, "Error creating font set structure.");
2014 bar_fs_extents
= XExtentsOfFontSet(bar_fs
);
2016 bar_height
= bar_fs_extents
->max_logical_extent
.height
+
2017 2 * bar_border_width
;
2024 xft_init(struct swm_region
*r
)
2026 char *font
, *d
, *search
;
2029 if (bar_font
== NULL
) {
2030 if ((d
= strdup(bar_fonts
)) == NULL
)
2031 errx(1, "insufficient memory.");
2033 while ((font
= strsep(&search
, ",")) != NULL
) {
2037 DNPRINTF(SWM_D_INIT
, "xft_init: try font %s\n", font
);
2040 bar_font
= XftFontOpenXlfd(display
, r
->s
->idx
,
2043 bar_font
= XftFontOpenName(display
, r
->s
->idx
,
2048 warnx("unable to load font %s", font
);
2051 DNPRINTF(SWM_D_INIT
, "successfully opened "
2059 if (bar_font
== NULL
)
2060 errx(1, "unable to open a font");
2062 PIXEL_TO_XRENDERCOLOR(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
, color
);
2064 if (!XftColorAllocValue(display
, DefaultVisual(display
, r
->s
->idx
),
2065 DefaultColormap(display
, r
->s
->idx
), &color
, &bar_font_color
))
2066 warn("unable to allocate Xft color");
2068 bar_height
= bar_font
->height
+ 2 * bar_border_width
;
2075 bar_setup(struct swm_region
*r
)
2077 xcb_screen_t
*screen
;
2080 DNPRINTF(SWM_D_BAR
, "bar_setup: screen %d.\n",
2083 if ((screen
= get_screen(r
->s
->idx
)) == NULL
)
2084 errx(1, "ERROR: can't get screen %d.", r
->s
->idx
);
2089 if ((r
->bar
= calloc(1, sizeof(struct swm_bar
))) == NULL
)
2090 err(1, "bar_setup: calloc: failed to allocate memory.");
2092 if (bar_font_legacy
)
2098 Y(r
->bar
) = bar_at_bottom
? (Y(r
) + HEIGHT(r
) - bar_height
) : Y(r
);
2099 WIDTH(r
->bar
) = WIDTH(r
) - 2 * bar_border_width
;
2100 HEIGHT(r
->bar
) = bar_height
- 2 * bar_border_width
;
2102 r
->bar
->id
= xcb_generate_id(conn
);
2103 wa
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
2104 wa
[1] = r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].pixel
;
2105 wa
[2] = XCB_EVENT_MASK_EXPOSURE
;
2107 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, r
->bar
->id
, r
->s
->root
,
2108 X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
), HEIGHT(r
->bar
),
2109 bar_border_width
, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
2110 XCB_COPY_FROM_PARENT
, XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
2111 | XCB_CW_EVENT_MASK
, wa
);
2113 r
->bar
->buffer
= xcb_generate_id(conn
);
2114 xcb_create_pixmap(conn
, screen
->root_depth
, r
->bar
->buffer
, r
->bar
->id
,
2115 WIDTH(r
->bar
), HEIGHT(r
->bar
));
2118 xcb_randr_select_input(conn
, r
->bar
->id
,
2119 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE
);
2122 xcb_map_window(conn
, r
->bar
->id
);
2124 DNPRINTF(SWM_D_BAR
, "bar_setup: window: 0x%x, (x,y) w x h: (%d,%d) "
2125 "%d x %d\n", WINID(r
->bar
), X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
),
2128 if (signal(SIGALRM
, bar_signal
) == SIG_ERR
)
2129 err(1, "could not install bar_signal");
2134 bar_cleanup(struct swm_region
*r
)
2138 xcb_destroy_window(conn
, r
->bar
->id
);
2139 xcb_free_pixmap(conn
, r
->bar
->buffer
);
2145 set_win_state(struct ws_win
*win
, uint16_t state
)
2147 uint16_t data
[2] = { state
, XCB_ATOM_NONE
};
2149 DNPRINTF(SWM_D_EVENT
, "set_win_state: window: 0x%x\n", win
->id
);
2154 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
, a_state
,
2155 a_state
, 32, 2, data
);
2159 getstate(xcb_window_t w
)
2161 uint32_t result
= 0;
2162 xcb_get_property_cookie_t c
;
2163 xcb_get_property_reply_t
*r
;
2165 c
= xcb_get_property(conn
, 0, w
, a_state
, a_state
, 0L, 2L);
2166 r
= xcb_get_property_reply(conn
, c
, NULL
);
2169 if (r
->type
== a_state
&& r
->format
== 32 && r
->length
== 2)
2170 result
= *((uint32_t *)xcb_get_property_value(r
));
2174 DNPRINTF(SWM_D_MISC
, "getstate property: win 0x%x state %u\n", w
,
2180 version(struct swm_region
*r
, union arg
*args
)
2182 /* suppress unused warnings since vars are needed */
2186 bar_version
= !bar_version
;
2188 snprintf(bar_vertext
, sizeof bar_vertext
,
2189 "Version: %s Build: %s", SPECTRWM_VERSION
, buildstr
);
2191 strlcpy(bar_vertext
, "", sizeof bar_vertext
);
2198 client_msg(struct ws_win
*win
, xcb_atom_t a
, xcb_timestamp_t t
)
2200 xcb_client_message_event_t ev
;
2208 name
= get_atom_name(a
);
2209 DNPRINTF(SWM_D_EVENT
, "client_msg: window: 0x%x, atom: %s(%u), "
2211 win
->id
, name
, a
, t
);
2215 bzero(&ev
, sizeof ev
);
2216 ev
.response_type
= XCB_CLIENT_MESSAGE
;
2217 ev
.window
= win
->id
;
2220 ev
.data
.data32
[0] = a
;
2221 ev
.data
.data32
[1] = t
;
2223 xcb_send_event(conn
, 0, win
->id
,
2224 XCB_EVENT_MASK_NO_EVENT
, (const char *)&ev
);
2227 /* synthetic response to a ConfigureRequest when not making a change */
2229 config_win(struct ws_win
*win
, xcb_configure_request_event_t
*ev
)
2231 xcb_configure_notify_event_t ce
;
2236 /* send notification of unchanged state. */
2237 bzero(&ce
, sizeof(ce
));
2238 ce
.response_type
= XCB_CONFIGURE_NOTIFY
;
2241 ce
.width
= WIDTH(win
);
2242 ce
.height
= HEIGHT(win
);
2243 ce
.override_redirect
= 0;
2248 ce
.window
= win
->id
;
2249 ce
.border_width
= BORDER(win
);
2250 ce
.above_sibling
= XCB_WINDOW_NONE
;
2253 ce
.event
= ev
->window
;
2254 ce
.window
= ev
->window
;
2256 /* make response appear more WM_SIZE_HINTS-compliant */
2257 if (win
->sh
.flags
) {
2258 DNPRINTF(SWM_D_MISC
, "config_win: hints: window: 0x%x,"
2259 " sh.flags: %u, min: %d x %d, max: %d x %d, inc: "
2260 "%d x %d\n", win
->id
, win
->sh
.flags
, SH_MIN_W(win
),
2261 SH_MIN_H(win
), SH_MAX_W(win
), SH_MAX_H(win
),
2262 SH_INC_W(win
), SH_INC_H(win
));
2267 /* the hint may be set... to 0! */
2268 if (SH_MIN_W(win
) > 0 && ce
.width
< SH_MIN_W(win
))
2269 ce
.width
= SH_MIN_W(win
);
2270 if (SH_MIN_H(win
) > 0 && ce
.height
< SH_MIN_H(win
))
2271 ce
.height
= SH_MIN_H(win
);
2276 /* may also be advertized as 0 */
2277 if (SH_MAX_W(win
) > 0 && ce
.width
> SH_MAX_W(win
))
2278 ce
.width
= SH_MAX_W(win
);
2279 if (SH_MAX_H(win
) > 0 && ce
.height
> SH_MAX_H(win
))
2280 ce
.height
= SH_MAX_H(win
);
2283 /* resize increment. */
2285 if (SH_INC_W(win
) > 1 && ce
.width
> SH_INC_W(win
))
2286 ce
.width
-= (ce
.width
- SH_MIN_W(win
)) %
2288 if (SH_INC_H(win
) > 1 && ce
.height
> SH_INC_H(win
))
2289 ce
.height
-= (ce
.height
- SH_MIN_H(win
)) %
2293 /* adjust x and y for requested border_width. */
2294 ce
.x
+= BORDER(win
) - ev
->border_width
;
2295 ce
.y
+= BORDER(win
) - ev
->border_width
;
2296 ce
.border_width
= ev
->border_width
;
2297 ce
.above_sibling
= ev
->sibling
;
2300 DNPRINTF(SWM_D_MISC
, "config_win: ewmh: %s, window: 0x%x, (x,y) w x h: "
2301 "(%d,%d) %d x %d, border: %d\n", YESNO(ev
== NULL
), win
->id
, ce
.x
,
2302 ce
.y
, ce
.width
, ce
.height
, ce
.border_width
);
2304 xcb_send_event(conn
, 0, win
->id
, XCB_EVENT_MASK_STRUCTURE_NOTIFY
,
2309 count_win(struct workspace
*ws
, int count_transient
)
2314 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
2315 if (count_transient
== 0 && win
->floating
)
2317 if (count_transient
== 0 && win
->transient
)
2323 DNPRINTF(SWM_D_MISC
, "count_win: %d\n", count
);
2329 quit(struct swm_region
*r
, union arg
*args
)
2331 /* suppress unused warnings since vars are needed */
2335 DNPRINTF(SWM_D_MISC
, "quit\n");
2340 map_window(struct ws_win
*win
)
2342 uint32_t val
= XCB_STACK_MODE_ABOVE
;
2347 DNPRINTF(SWM_D_EVENT
, "map_window: win 0x%x, mapped: %s\n", win
->id
,
2348 YESNO(win
->mapped
));
2350 xcb_configure_window(conn
, win
->id
,
2351 XCB_CONFIG_WINDOW_STACK_MODE
, &val
);
2356 xcb_map_window(conn
, win
->id
);
2357 set_win_state(win
, XCB_ICCCM_WM_STATE_NORMAL
);
2362 unmap_window(struct ws_win
*win
)
2367 DNPRINTF(SWM_D_EVENT
, "unmap_window: win 0x%x, mapped: %s\n", win
->id
,
2368 YESNO(win
->mapped
));
2373 xcb_unmap_window(conn
, win
->id
);
2374 set_win_state(win
, XCB_ICCCM_WM_STATE_ICONIC
);
2382 int i
, j
, num_screens
;
2384 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2385 for (i
= 0; i
< num_screens
; i
++)
2386 for (j
= 0; j
< workspace_limit
; j
++)
2387 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2392 fake_keypress(struct ws_win
*win
, xcb_keysym_t keysym
, uint16_t modifiers
)
2394 xcb_key_press_event_t event
;
2395 xcb_keycode_t
*keycode
;
2400 keycode
= xcb_key_symbols_get_keycode(syms
, keysym
);
2402 DNPRINTF(SWM_D_MISC
, "fake_keypress: win 0x%x keycode %u\n",
2405 bzero(&event
, sizeof(event
));
2406 event
.event
= win
->id
;
2407 event
.root
= win
->s
->root
;
2408 event
.child
= XCB_WINDOW_NONE
;
2409 event
.time
= XCB_CURRENT_TIME
;
2410 event
.event_x
= X(win
);
2411 event
.event_y
= Y(win
);
2414 event
.same_screen
= 1;
2415 event
.detail
= *keycode
;
2416 event
.state
= modifiers
;
2418 event
.response_type
= XCB_KEY_PRESS
;
2419 xcb_send_event(conn
, 1, win
->id
,
2420 XCB_EVENT_MASK_KEY_PRESS
, (const char *)&event
);
2422 event
.response_type
= XCB_KEY_RELEASE
;
2423 xcb_send_event(conn
, 1, win
->id
,
2424 XCB_EVENT_MASK_KEY_RELEASE
, (const char *)&event
);
2430 restart(struct swm_region
*r
, union arg
*args
)
2432 /* suppress unused warning since var is needed */
2436 DNPRINTF(SWM_D_MISC
, "restart: %s\n", start_argv
[0]);
2440 execvp(start_argv
[0], start_argv
);
2441 warn("execvp failed");
2446 root_to_region(xcb_window_t root
)
2448 struct swm_region
*r
= NULL
;
2450 xcb_query_pointer_reply_t
*qpr
;
2452 DNPRINTF(SWM_D_MISC
, "root_to_region: window: 0x%x\n", root
);
2454 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2455 for (i
= 0; i
< num_screens
; i
++)
2456 if (screens
[i
].root
== root
)
2459 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
,
2460 screens
[i
].root
), NULL
);
2463 DNPRINTF(SWM_D_MISC
, "root_to_region: pointer: (%d,%d)\n",
2464 qpr
->root_x
, qpr
->root_y
);
2465 /* choose a region based on pointer location */
2466 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2467 if (X(r
) <= qpr
->root_x
&& qpr
->root_x
< MAX_X(r
) &&
2468 Y(r
) <= qpr
->root_y
&& qpr
->root_y
< MAX_Y(r
))
2474 r
= TAILQ_FIRST(&screens
[i
].rl
);
2480 find_unmanaged_window(xcb_window_t id
)
2483 int i
, j
, num_screens
;
2485 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2486 for (i
= 0; i
< num_screens
; i
++)
2487 for (j
= 0; j
< workspace_limit
; j
++)
2488 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].unmanagedlist
,
2496 find_window(xcb_window_t id
)
2499 int i
, j
, num_screens
;
2500 xcb_query_tree_reply_t
*r
;
2502 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2503 for (i
= 0; i
< num_screens
; i
++)
2504 for (j
= 0; j
< workspace_limit
; j
++)
2505 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2509 r
= xcb_query_tree_reply(conn
, xcb_query_tree(conn
, id
), NULL
);
2513 /* if we were looking for the parent return that window instead */
2514 if (r
->parent
== 0 || r
->root
== r
->parent
) {
2519 /* look for parent */
2520 for (i
= 0; i
< num_screens
; i
++)
2521 for (j
= 0; j
< workspace_limit
; j
++)
2522 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2523 if (r
->parent
== win
->id
) {
2533 spawn(int ws_idx
, union arg
*args
, int close_fd
)
2538 DNPRINTF(SWM_D_MISC
, "spawn: %s\n", args
->argv
[0]);
2540 close(xcb_get_file_descriptor(conn
));
2542 setenv("LD_PRELOAD", SWM_LIB
, 1);
2544 if (asprintf(&ret
, "%d", ws_idx
) == -1) {
2545 warn("spawn: asprintf SWM_WS");
2548 setenv("_SWM_WS", ret
, 1);
2552 if (asprintf(&ret
, "%d", getpid()) == -1) {
2553 warn("spawn: asprintf _SWM_PID");
2556 setenv("_SWM_PID", ret
, 1);
2560 if (setsid() == -1) {
2561 warn("spawn: setsid");
2567 * close stdin and stdout to prevent interaction between apps
2568 * and the baraction script
2569 * leave stderr open to record errors
2571 if ((fd
= open(_PATH_DEVNULL
, O_RDWR
, 0)) == -1) {
2572 warn("spawn: open");
2575 dup2(fd
, STDIN_FILENO
);
2576 dup2(fd
, STDOUT_FILENO
);
2581 execvp(args
->argv
[0], args
->argv
);
2583 warn("spawn: execvp");
2588 kill_refs(struct ws_win
*win
)
2590 int i
, x
, num_screens
;
2591 struct swm_region
*r
;
2592 struct workspace
*ws
;
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 if (win
== ws
->focus
)
2604 if (win
== ws
->focus_prev
)
2605 ws
->focus_prev
= NULL
;
2610 validate_win(struct ws_win
*testwin
)
2613 struct workspace
*ws
;
2614 struct swm_region
*r
;
2615 int i
, x
, num_screens
;
2617 if (testwin
== NULL
)
2620 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2621 for (i
= 0; i
< num_screens
; i
++)
2622 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2623 for (x
= 0; x
< workspace_limit
; x
++) {
2625 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
2633 validate_ws(struct workspace
*testws
)
2635 struct swm_region
*r
;
2636 struct workspace
*ws
;
2637 int i
, x
, num_screens
;
2639 /* validate all ws */
2640 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2641 for (i
= 0; i
< num_screens
; i
++)
2642 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2643 for (x
= 0; x
< workspace_limit
; x
++) {
2652 unfocus_win(struct ws_win
*win
)
2654 xcb_window_t none
= XCB_WINDOW_NONE
;
2656 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: window: 0x%x\n", WINID(win
));
2660 if (win
->ws
== NULL
) {
2661 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: NULL ws.\n");
2665 if (validate_ws(win
->ws
)) {
2666 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: invalid ws.\n");
2670 if (win
->ws
->r
== NULL
) {
2671 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: NULL region.\n");
2675 if (validate_win(win
)) {
2676 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: invalid win.\n");
2681 if (win
->ws
->focus
== win
) {
2682 win
->ws
->focus
= NULL
;
2683 win
->ws
->focus_prev
= win
;
2686 if (validate_win(win
->ws
->focus
)) {
2687 kill_refs(win
->ws
->focus
);
2688 win
->ws
->focus
= NULL
;
2690 if (validate_win(win
->ws
->focus_prev
)) {
2691 kill_refs(win
->ws
->focus_prev
);
2692 win
->ws
->focus_prev
= NULL
;
2695 xcb_change_window_attributes(conn
, win
->id
, XCB_CW_BORDER_PIXEL
,
2696 &win
->ws
->r
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
);
2698 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
2699 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1, &none
);
2701 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: done.\n");
2705 focus_win(struct ws_win
*win
)
2707 struct ws_win
*cfw
= NULL
;
2708 xcb_get_input_focus_reply_t
*r
;
2710 DNPRINTF(SWM_D_FOCUS
, "focus_win: window: 0x%x\n", WINID(win
));
2715 if (win
->ws
== NULL
)
2718 if (validate_ws(win
->ws
))
2721 if (validate_win(win
)) {
2726 r
= xcb_get_input_focus_reply(conn
, xcb_get_input_focus(conn
), NULL
);
2728 cfw
= find_window(r
->focus
);
2734 if (win
->ws
->focus
!= win
) {
2735 if (win
->ws
->focus
&& win
->ws
->focus
!= cfw
)
2736 unfocus_win(win
->ws
->focus
);
2737 win
->ws
->focus
= win
;
2740 if (cfw
!= win
&& win
->ws
->r
!= NULL
) {
2741 /* Set input focus if no input hint, or indicated by hint. */
2742 if (!(win
->hints
.flags
& XCB_ICCCM_WM_HINT_INPUT
) ||
2743 (win
->hints
.flags
& XCB_ICCCM_WM_HINT_INPUT
&&
2745 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
,
2746 win
->id
, last_event_time
);
2748 /* Tell app it can adjust focus to a specific window. */
2749 if (win
->take_focus
) {
2750 /* java is special; always tell parent */
2751 if (win
->transient
&& win
->java
)
2752 client_msg(find_window(win
->transient
),
2753 a_takefocus
, last_event_time
);
2755 client_msg(win
, a_takefocus
, last_event_time
);
2758 xcb_change_window_attributes(conn
, win
->id
, XCB_CW_BORDER_PIXEL
,
2759 &win
->ws
->r
->s
->c
[SWM_S_COLOR_FOCUS
].pixel
);
2761 if (win
->ws
->cur_layout
->flags
& SWM_L_MAPONFOCUS
||
2762 win
->ws
->always_raise
)
2765 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
2766 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1,
2773 DNPRINTF(SWM_D_FOCUS
, "focus_win: done.\n");
2776 /* If a child window should have focus instead, return it. */
2778 get_focus_magic(struct ws_win
*win
)
2780 struct ws_win
*parent
= NULL
;
2782 DNPRINTF(SWM_D_FOCUS
, "get_focus_magic: window: 0x%x\n", WINID(win
));
2786 if (win
->transient
) {
2787 parent
= find_window(win
->transient
);
2789 /* If parent prefers focus elsewhere, then do so. */
2790 if (parent
&& parent
->focus_child
) {
2791 if (validate_win(parent
->focus_child
) == 0)
2792 win
= parent
->focus_child
;
2794 parent
->focus_child
= NULL
;
2798 /* If this window prefers focus elsewhere, then do so. */
2799 if (win
->focus_child
) {
2800 if (validate_win(win
->focus_child
) == 0)
2801 win
= win
->focus_child
;
2803 win
->focus_child
= NULL
;
2810 event_drain(uint8_t rt
)
2812 xcb_generic_event_t
*evt
;
2814 /* ensure all pending requests have been processed before filtering. */
2816 while ((evt
= xcb_poll_for_event(conn
))) {
2817 if (XCB_EVENT_RESPONSE_TYPE(evt
) != rt
)
2825 switchws(struct swm_region
*r
, union arg
*args
)
2827 int wsid
= args
->id
, unmap_old
= 0;
2828 struct swm_region
*this_r
, *other_r
;
2830 struct workspace
*new_ws
, *old_ws
;
2835 if (wsid
>= workspace_limit
)
2839 old_ws
= this_r
->ws
;
2840 new_ws
= &this_r
->s
->ws
[wsid
];
2842 DNPRINTF(SWM_D_WS
, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
2843 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), old_ws
->idx
, wsid
);
2845 if (new_ws
== NULL
|| old_ws
== NULL
)
2847 if (new_ws
== old_ws
)
2850 other_r
= new_ws
->r
;
2851 if (other_r
== NULL
) {
2852 /* the other workspace is hidden, hide this one */
2856 /* the other ws is visible in another region, exchange them */
2857 other_r
->ws_prior
= new_ws
;
2858 other_r
->ws
= old_ws
;
2859 old_ws
->r
= other_r
;
2861 this_r
->ws_prior
= old_ws
;
2862 this_r
->ws
= new_ws
;
2867 new_ws
->focus
= get_region_focus(new_ws
->r
);
2869 /* unmap old windows */
2871 TAILQ_FOREACH(win
, &old_ws
->winlist
, entry
)
2874 /* make sure bar gets updated if ws is empty */
2880 DNPRINTF(SWM_D_WS
, "switchws: done.\n");
2884 cyclews(struct swm_region
*r
, union arg
*args
)
2887 struct swm_screen
*s
= r
->s
;
2890 DNPRINTF(SWM_D_WS
, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2891 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
2896 case SWM_ARG_ID_CYCLEWS_UP_ALL
:
2899 case SWM_ARG_ID_CYCLEWS_UP
:
2900 if (a
.id
< workspace_limit
- 1)
2905 case SWM_ARG_ID_CYCLEWS_DOWN_ALL
:
2908 case SWM_ARG_ID_CYCLEWS_DOWN
:
2912 a
.id
= workspace_limit
- 1;
2919 (cycle_empty
== 0 && TAILQ_EMPTY(&s
->ws
[a
.id
].winlist
)))
2921 if (cycle_visible
== 0 && s
->ws
[a
.id
].r
!= NULL
)
2925 } while (a
.id
!= r
->ws
->idx
);
2929 priorws(struct swm_region
*r
, union arg
*args
)
2935 DNPRINTF(SWM_D_WS
, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2936 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
2938 if (r
->ws_prior
== NULL
)
2941 a
.id
= r
->ws_prior
->idx
;
2946 cyclescr(struct swm_region
*r
, union arg
*args
)
2948 struct swm_region
*rr
= NULL
;
2949 int i
, x
, y
, num_screens
;
2951 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2952 /* do nothing if we don't have more than one screen */
2953 if (!(num_screens
> 1 || outputs
> 1))
2958 case SWM_ARG_ID_CYCLESC_UP
:
2959 rr
= TAILQ_NEXT(r
, entry
);
2961 rr
= TAILQ_FIRST(&screens
[i
].rl
);
2963 case SWM_ARG_ID_CYCLESC_DOWN
:
2964 rr
= TAILQ_PREV(r
, swm_region_list
, entry
);
2966 rr
= TAILQ_LAST(&screens
[i
].rl
, swm_region_list
);
2974 /* move mouse to region */
2976 y
= Y(rr
) + 1 + (bar_enabled
? bar_height
: 0);
2977 xcb_warp_pointer(conn
, XCB_WINDOW_NONE
, rr
->s
[i
].root
, 0, 0, 0, 0,
2980 rr
->ws
->focus
= get_region_focus(rr
);
2982 if (rr
->ws
->focus
) {
2983 /* move to focus window */
2984 x
= X(rr
->ws
->focus
) + 1;
2985 y
= Y(rr
->ws
->focus
) + 1;
2986 xcb_warp_pointer(conn
, XCB_WINDOW_NONE
, rr
->s
[i
].root
, 0, 0, 0,
2994 sort_windows(struct ws_win_list
*wl
)
2996 struct ws_win
*win
, *parent
, *nxt
;
3001 for (win
= TAILQ_FIRST(wl
); win
!= TAILQ_END(wl
); win
= nxt
) {
3002 nxt
= TAILQ_NEXT(win
, entry
);
3003 if (win
->transient
) {
3004 parent
= find_window(win
->transient
);
3005 if (parent
== NULL
) {
3006 warnx("not possible bug");
3009 TAILQ_REMOVE(wl
, win
, entry
);
3010 TAILQ_INSERT_AFTER(wl
, parent
, win
, entry
);
3017 swapwin(struct swm_region
*r
, union arg
*args
)
3019 struct ws_win
*target
, *source
;
3020 struct ws_win
*cur_focus
;
3021 struct ws_win_list
*wl
;
3024 DNPRINTF(SWM_D_WS
, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3025 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
3027 cur_focus
= r
->ws
->focus
;
3028 if (cur_focus
== NULL
)
3032 wl
= &source
->ws
->winlist
;
3035 case SWM_ARG_ID_SWAPPREV
:
3036 if (source
->transient
)
3037 source
= find_window(source
->transient
);
3038 target
= TAILQ_PREV(source
, ws_win_list
, entry
);
3039 if (target
&& target
->transient
)
3040 target
= find_window(target
->transient
);
3041 TAILQ_REMOVE(wl
, source
, entry
);
3043 TAILQ_INSERT_TAIL(wl
, source
, entry
);
3045 TAILQ_INSERT_BEFORE(target
, source
, entry
);
3047 case SWM_ARG_ID_SWAPNEXT
:
3048 target
= TAILQ_NEXT(source
, entry
);
3049 /* move the parent and let the sort handle the move */
3050 if (source
->transient
)
3051 source
= find_window(source
->transient
);
3052 TAILQ_REMOVE(wl
, source
, entry
);
3054 TAILQ_INSERT_HEAD(wl
, source
, entry
);
3056 TAILQ_INSERT_AFTER(wl
, target
, source
, entry
);
3058 case SWM_ARG_ID_SWAPMAIN
:
3059 target
= TAILQ_FIRST(wl
);
3060 if (target
== source
) {
3061 if (source
->ws
->focus_prev
!= NULL
&&
3062 source
->ws
->focus_prev
!= target
)
3063 source
= source
->ws
->focus_prev
;
3067 if (target
== NULL
|| source
== NULL
)
3069 source
->ws
->focus_prev
= target
;
3070 TAILQ_REMOVE(wl
, target
, entry
);
3071 TAILQ_INSERT_BEFORE(source
, target
, entry
);
3072 TAILQ_REMOVE(wl
, source
, entry
);
3073 TAILQ_INSERT_HEAD(wl
, source
, entry
);
3075 case SWM_ARG_ID_MOVELAST
:
3076 TAILQ_REMOVE(wl
, source
, entry
);
3077 TAILQ_INSERT_TAIL(wl
, source
, entry
);
3080 DNPRINTF(SWM_D_MOVE
, "swapwin: invalid id: %d\n", args
->id
);
3092 get_focus_prev(struct ws_win
*win
)
3094 struct ws_win
*winfocus
= NULL
;
3095 struct ws_win
*cur_focus
= NULL
;
3096 struct ws_win_list
*wl
= NULL
;
3097 struct workspace
*ws
= NULL
;
3099 if (!(win
&& win
->ws
))
3104 cur_focus
= ws
->focus
;
3106 DNPRINTF(SWM_D_FOCUS
, "get_focus_prev: window: 0x%x, cur_focus: 0x%x\n",
3107 WINID(win
), WINID(cur_focus
));
3109 /* pickle, just focus on whatever */
3110 if (cur_focus
== NULL
) {
3111 /* use prev_focus if valid */
3112 if (ws
->focus_prev
&& ws
->focus_prev
!= cur_focus
&&
3113 find_window(WINID(ws
->focus_prev
)))
3114 winfocus
= ws
->focus_prev
;
3118 /* if transient focus on parent */
3119 if (cur_focus
->transient
) {
3120 winfocus
= find_window(cur_focus
->transient
);
3124 /* if in max_stack try harder */
3125 if ((win
->quirks
& SWM_Q_FOCUSPREV
) ||
3126 (ws
->cur_layout
->flags
& SWM_L_FOCUSPREV
)) {
3127 if (cur_focus
!= ws
->focus_prev
)
3128 winfocus
= ws
->focus_prev
;
3130 winfocus
= TAILQ_PREV(win
, ws_win_list
, entry
);
3135 DNPRINTF(SWM_D_FOCUS
, "get_focus_prev: focus_close: %d\n", focus_close
);
3137 if (winfocus
== NULL
|| winfocus
== win
) {
3138 switch (focus_close
) {
3139 case SWM_STACK_BOTTOM
:
3140 winfocus
= TAILQ_FIRST(wl
);
3143 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
3145 case SWM_STACK_ABOVE
:
3146 if ((winfocus
= TAILQ_NEXT(cur_focus
, entry
)) == NULL
) {
3147 if (focus_close_wrap
)
3148 winfocus
= TAILQ_FIRST(wl
);
3150 winfocus
= TAILQ_PREV(cur_focus
,
3151 ws_win_list
, entry
);
3154 case SWM_STACK_BELOW
:
3155 if ((winfocus
= TAILQ_PREV(cur_focus
, ws_win_list
,
3157 if (focus_close_wrap
)
3158 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
3160 winfocus
= TAILQ_NEXT(cur_focus
, entry
);
3166 if (winfocus
== NULL
) {
3167 if (focus_default
== SWM_STACK_TOP
)
3168 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
3170 winfocus
= TAILQ_FIRST(wl
);
3175 return get_focus_magic(winfocus
);
3179 get_region_focus(struct swm_region
*r
)
3181 struct ws_win
*winfocus
= NULL
;
3186 if (r
->ws
->focus
&& !r
->ws
->focus
->iconic
)
3187 winfocus
= r
->ws
->focus
;
3188 else if (r
->ws
->focus_prev
&& !r
->ws
->focus_prev
->iconic
)
3189 winfocus
= r
->ws
->focus_prev
;
3191 TAILQ_FOREACH(winfocus
, &r
->ws
->winlist
, entry
)
3192 if (!winfocus
->iconic
)
3195 return get_focus_magic(winfocus
);
3199 focus(struct swm_region
*r
, union arg
*args
)
3201 struct ws_win
*head
, *cur_focus
= NULL
, *winfocus
= NULL
;
3202 struct ws_win_list
*wl
= NULL
;
3203 struct workspace
*ws
= NULL
;
3209 DNPRINTF(SWM_D_FOCUS
, "focus: id: %d\n", args
->id
);
3211 if ((cur_focus
= r
->ws
->focus
) == NULL
)
3215 if (TAILQ_EMPTY(wl
))
3217 /* make sure there is at least one uniconified window */
3219 TAILQ_FOREACH(winfocus
, wl
, entry
)
3220 if (winfocus
->iconic
== 0) {
3228 case SWM_ARG_ID_FOCUSPREV
:
3229 head
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
3231 head
= TAILQ_LAST(wl
, ws_win_list
);
3233 if (WINID(winfocus
) == cur_focus
->transient
) {
3234 head
= TAILQ_PREV(winfocus
, ws_win_list
, entry
);
3236 head
= TAILQ_LAST(wl
, ws_win_list
);
3241 if (winfocus
&& winfocus
->iconic
) {
3242 while (winfocus
!= cur_focus
) {
3243 if (winfocus
== NULL
)
3244 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
3245 if (winfocus
->iconic
== 0)
3247 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
,
3253 case SWM_ARG_ID_FOCUSNEXT
:
3254 head
= TAILQ_NEXT(cur_focus
, entry
);
3256 head
= TAILQ_FIRST(wl
);
3260 if (winfocus
&& winfocus
->iconic
) {
3261 while (winfocus
!= cur_focus
) {
3262 if (winfocus
== NULL
)
3263 winfocus
= TAILQ_FIRST(wl
);
3264 if (winfocus
->iconic
== 0)
3266 winfocus
= TAILQ_NEXT(winfocus
, entry
);
3271 case SWM_ARG_ID_FOCUSMAIN
:
3272 winfocus
= TAILQ_FIRST(wl
);
3273 if (winfocus
== cur_focus
)
3274 winfocus
= cur_focus
->ws
->focus_prev
;
3281 focus_win(get_focus_magic(winfocus
));
3287 cycle_layout(struct swm_region
*r
, union arg
*args
)
3289 struct workspace
*ws
= r
->ws
;
3291 /* suppress unused warning since var is needed */
3294 DNPRINTF(SWM_D_EVENT
, "cycle_layout: workspace: %d\n", ws
->idx
);
3297 if (ws
->cur_layout
->l_stack
== NULL
)
3298 ws
->cur_layout
= &layouts
[0];
3303 focus_win(get_region_focus(r
));
3309 stack_config(struct swm_region
*r
, union arg
*args
)
3311 struct workspace
*ws
= r
->ws
;
3313 DNPRINTF(SWM_D_STACK
, "stack_config: id: %d workspace: %d\n",
3316 if (ws
->cur_layout
->l_config
!= NULL
)
3317 ws
->cur_layout
->l_config(ws
, args
->id
);
3319 if (args
->id
!= SWM_ARG_ID_STACKINIT
)
3328 struct swm_geometry g
;
3329 struct swm_region
*r
;
3335 DNPRINTF(SWM_D_STACK
, "stack: begin\n");
3337 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
3338 for (i
= 0; i
< num_screens
; i
++) {
3342 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
3343 DNPRINTF(SWM_D_STACK
, "stack: workspace: %d "
3344 "(screen: %d, region: %d)\n", r
->ws
->idx
, i
, j
++);
3346 /* start with screen geometry, adjust for bar */
3348 g
.w
-= 2 * border_width
;
3349 g
.h
-= 2 * border_width
;
3355 r
->ws
->cur_layout
->l_stack(r
->ws
, &g
);
3356 r
->ws
->cur_layout
->l_string(r
->ws
);
3357 /* save r so we can track region changes */
3364 DNPRINTF(SWM_D_STACK
, "stack: end\n");
3368 store_float_geom(struct ws_win
*win
, struct swm_region
*r
)
3370 if (win
== NULL
|| r
== NULL
)
3373 /* retain window geom and region geom */
3374 win
->g_float
= win
->g
;
3375 win
->g_float
.x
-= X(r
);
3376 win
->g_float
.y
-= Y(r
);
3377 win
->g_floatvalid
= 1;
3378 DNPRINTF(SWM_D_MISC
, "store_float_geom: window: 0x%x, g: (%d,%d)"
3379 " %d x %d, g_float: (%d,%d) %d x %d\n", win
->id
, X(win
), Y(win
),
3380 WIDTH(win
), HEIGHT(win
), win
->g_float
.x
, win
->g_float
.y
,
3381 win
->g_float
.w
, win
->g_float
.h
);
3385 load_float_geom(struct ws_win
*win
, struct swm_region
*r
)
3387 if (win
== NULL
|| r
== NULL
)
3390 if (win
->g_floatvalid
) {
3391 win
->g
= win
->g_float
;
3394 DNPRINTF(SWM_D_MISC
, "load_float_geom: window: 0x%x, g: (%d,%d)"
3395 "%d x %d\n", win
->id
, X(win
), Y(win
), WIDTH(win
),
3398 DNPRINTF(SWM_D_MISC
, "load_float_geom: window: 0x%x, g_float "
3399 "is not set.\n", win
->id
);
3404 stack_floater(struct ws_win
*win
, struct swm_region
*r
)
3409 DNPRINTF(SWM_D_MISC
, "stack_floater: window: 0x%x\n", win
->id
);
3412 * to allow windows to change their size (e.g. mplayer fs) only retrieve
3413 * geom on ws switches or return from max mode
3415 if (win
->floatmaxed
|| (r
!= r
->ws
->old_r
&&
3416 !(win
->ewmh_flags
& EWMH_F_FULLSCREEN
))) {
3417 /* update geometry for the new region */
3418 load_float_geom(win
, r
);
3421 win
->floatmaxed
= 0;
3424 * if set to fullscreen mode, configure window to maximum size.
3426 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
3427 if (!win
->g_floatvalid
)
3428 store_float_geom(win
, win
->ws
->r
);
3434 * remove border on fullscreen floater when in fullscreen mode or when
3435 * the quirk is present.
3437 if ((win
->ewmh_flags
& EWMH_F_FULLSCREEN
) ||
3438 ((win
->quirks
& SWM_Q_FULLSCREEN
) &&
3439 (WIDTH(win
) >= WIDTH(r
)) && (HEIGHT(win
) >= HEIGHT(r
)))) {
3440 if (win
->bordered
) {
3442 X(win
) += border_width
;
3443 Y(win
) += border_width
;
3445 } else if (!win
->bordered
) {
3447 X(win
) -= border_width
;
3448 Y(win
) -= border_width
;
3451 if (win
->transient
&& (win
->quirks
& SWM_Q_TRANSSZ
)) {
3452 WIDTH(win
) = (double)WIDTH(r
) * dialog_ratio
;
3453 HEIGHT(win
) = (double)HEIGHT(r
) * dialog_ratio
;
3456 if (!win
->manual
&& !(win
->ewmh_flags
& EWMH_F_FULLSCREEN
) &&
3457 !(win
->quirks
& SWM_Q_ANYWHERE
)) {
3459 * floaters and transients are auto-centred unless moved,
3460 * resized or ANYWHERE quirk is set.
3462 X(win
) = X(r
) + (WIDTH(r
) - WIDTH(win
)) / 2 - BORDER(win
);
3463 Y(win
) = Y(r
) + (HEIGHT(r
) - HEIGHT(win
)) / 2 - BORDER(win
);
3465 store_float_geom(win
, r
);
3468 /* keep window within region bounds */
3469 constrain_window(win
, r
, 0);
3475 * Send keystrokes to terminal to decrease/increase the font size as the
3476 * window size changes.
3479 adjust_font(struct ws_win
*win
)
3481 if (!(win
->quirks
& SWM_Q_XTERM_FONTADJ
) ||
3482 win
->floating
|| win
->transient
)
3485 if (win
->sh
.width_inc
&& win
->last_inc
!= win
->sh
.width_inc
&&
3486 WIDTH(win
) / win
->sh
.width_inc
< term_width
&&
3487 win
->font_steps
< SWM_MAX_FONT_STEPS
) {
3488 win
->font_size_boundary
[win
->font_steps
] =
3489 (win
->sh
.width_inc
* term_width
) + win
->sh
.base_width
;
3492 win
->last_inc
= win
->sh
.width_inc
;
3493 fake_keypress(win
, XK_KP_Subtract
, XCB_MOD_MASK_SHIFT
);
3494 } else if (win
->font_steps
&& win
->last_inc
!= win
->sh
.width_inc
&&
3495 WIDTH(win
) > win
->font_size_boundary
[win
->font_steps
- 1]) {
3498 win
->last_inc
= win
->sh
.width_inc
;
3499 fake_keypress(win
, XK_KP_Add
, XCB_MOD_MASK_SHIFT
);
3503 #define SWAPXY(g) do { \
3505 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
3506 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
3509 stack_master(struct workspace
*ws
, struct swm_geometry
*g
, int rot
, int flip
)
3511 struct swm_geometry win_g
, r_g
= *g
;
3512 struct ws_win
*win
, *fs_win
= NULL
;
3513 int i
, j
, s
, stacks
;
3514 int w_inc
= 1, h_inc
, w_base
= 1, h_base
;
3515 int hrh
, extra
= 0, h_slice
, last_h
= 0;
3516 int split
, colno
, winno
, mwin
, msize
, mscale
;
3517 int remain
, missing
, v_slice
, reconfigure
;
3520 DNPRINTF(SWM_D_STACK
, "stack_master: workspace: %d, rot: %s, "
3521 "flip: %s\n", ws
->idx
, YESNO(rot
), YESNO(flip
));
3523 winno
= count_win(ws
, 0);
3524 if (winno
== 0 && count_win(ws
, 1) == 0)
3527 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
3528 if (win
->transient
== 0 && win
->floating
== 0
3529 && win
->iconic
== 0)
3536 w_inc
= win
->sh
.width_inc
;
3537 w_base
= win
->sh
.base_width
;
3538 mwin
= ws
->l_state
.horizontal_mwin
;
3539 mscale
= ws
->l_state
.horizontal_msize
;
3540 stacks
= ws
->l_state
.horizontal_stacks
;
3543 w_inc
= win
->sh
.height_inc
;
3544 w_base
= win
->sh
.base_height
;
3545 mwin
= ws
->l_state
.vertical_mwin
;
3546 mscale
= ws
->l_state
.vertical_msize
;
3547 stacks
= ws
->l_state
.vertical_stacks
;
3551 if (stacks
> winno
- mwin
)
3552 stacks
= winno
- mwin
;
3556 h_slice
= r_g
.h
/ SWM_H_SLICE
;
3557 if (mwin
&& winno
> mwin
) {
3558 v_slice
= r_g
.w
/ SWM_V_SLICE
;
3562 win_g
.w
= v_slice
* mscale
;
3564 if (w_inc
> 1 && w_inc
< v_slice
) {
3565 /* adjust for window's requested size increment */
3566 remain
= (win_g
.w
- w_base
) % w_inc
;
3572 win_g
.x
+= r_g
.w
- msize
;
3575 colno
= split
= winno
/ stacks
;
3576 win_g
.w
= ((r_g
.w
- (stacks
* 2 * border_width
) +
3577 2 * border_width
) / stacks
);
3579 hrh
= r_g
.h
/ colno
;
3580 extra
= r_g
.h
- (colno
* hrh
);
3581 win_g
.h
= hrh
- 2 * border_width
;
3583 /* stack all the tiled windows */
3584 i
= j
= 0, s
= stacks
;
3585 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3586 if (win
->transient
!= 0 || win
->floating
!= 0)
3588 if (win
->iconic
!= 0)
3591 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
3596 if (split
&& i
== split
) {
3597 colno
= (winno
- mwin
) / stacks
;
3598 if (s
<= (winno
- mwin
) % stacks
)
3600 split
= split
+ colno
;
3601 hrh
= (r_g
.h
/ colno
);
3602 extra
= r_g
.h
- (colno
* hrh
);
3606 win_g
.x
+= win_g
.w
+ 2 * border_width
;
3607 win_g
.w
= (r_g
.w
- msize
-
3608 (stacks
* 2 * border_width
)) / stacks
;
3610 win_g
.w
+= (r_g
.w
- msize
-
3611 (stacks
* 2 * border_width
)) % stacks
;
3615 win_g
.h
= hrh
- 2 * border_width
;
3617 h_inc
= win
->sh
.width_inc
;
3618 h_base
= win
->sh
.base_width
;
3620 h_inc
= win
->sh
.height_inc
;
3621 h_base
= win
->sh
.base_height
;
3623 if (j
== colno
- 1) {
3624 win_g
.h
= hrh
+ extra
;
3625 } else if (h_inc
> 1 && h_inc
< h_slice
) {
3626 /* adjust for window's requested size increment */
3627 remain
= (win_g
.h
- h_base
) % h_inc
;
3628 missing
= h_inc
- remain
;
3630 if (missing
<= extra
|| j
== 0) {
3642 win_g
.y
+= last_h
+ 2 * border_width
;
3644 if (disable_border
&& bar_enabled
== 0 && winno
== 1){
3646 win_g
.w
+= 2 * border_width
;
3647 win_g
.h
+= 2 * border_width
;
3652 if (X(win
) != win_g
.y
|| Y(win
) != win_g
.x
||
3653 WIDTH(win
) != win_g
.h
|| HEIGHT(win
) != win_g
.w
) {
3657 WIDTH(win
) = win_g
.h
;
3658 HEIGHT(win
) = win_g
.w
;
3661 if (X(win
) != win_g
.x
|| Y(win
) != win_g
.y
||
3662 WIDTH(win
) != win_g
.w
|| HEIGHT(win
) != win_g
.h
) {
3666 WIDTH(win
) = win_g
.w
;
3667 HEIGHT(win
) = win_g
.h
;
3671 if (bordered
!= win
->bordered
) {
3673 win
->bordered
= bordered
;
3689 /* now, stack all the floaters and transients */
3690 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3691 if (win
->transient
== 0 && win
->floating
== 0)
3693 if (win
->iconic
== 1)
3695 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
3700 stack_floater(win
, ws
->r
);
3705 stack_floater(fs_win
, ws
->r
);
3711 vertical_config(struct workspace
*ws
, int id
)
3713 DNPRINTF(SWM_D_STACK
, "vertical_config: id: %d, workspace: %d\n",
3717 case SWM_ARG_ID_STACKRESET
:
3718 case SWM_ARG_ID_STACKINIT
:
3719 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ 2;
3720 ws
->l_state
.vertical_mwin
= 1;
3721 ws
->l_state
.vertical_stacks
= 1;
3723 case SWM_ARG_ID_MASTERSHRINK
:
3724 if (ws
->l_state
.vertical_msize
> 1)
3725 ws
->l_state
.vertical_msize
--;
3727 case SWM_ARG_ID_MASTERGROW
:
3728 if (ws
->l_state
.vertical_msize
< SWM_V_SLICE
- 1)
3729 ws
->l_state
.vertical_msize
++;
3731 case SWM_ARG_ID_MASTERADD
:
3732 ws
->l_state
.vertical_mwin
++;
3734 case SWM_ARG_ID_MASTERDEL
:
3735 if (ws
->l_state
.vertical_mwin
> 0)
3736 ws
->l_state
.vertical_mwin
--;
3738 case SWM_ARG_ID_STACKINC
:
3739 ws
->l_state
.vertical_stacks
++;
3741 case SWM_ARG_ID_STACKDEC
:
3742 if (ws
->l_state
.vertical_stacks
> 1)
3743 ws
->l_state
.vertical_stacks
--;
3745 case SWM_ARG_ID_FLIPLAYOUT
:
3746 ws
->l_state
.vertical_flip
= !ws
->l_state
.vertical_flip
;
3754 vertical_stack(struct workspace
*ws
, struct swm_geometry
*g
)
3756 DNPRINTF(SWM_D_STACK
, "vertical_stack: workspace: %d\n", ws
->idx
);
3758 stack_master(ws
, g
, 0, ws
->l_state
.vertical_flip
);
3762 horizontal_config(struct workspace
*ws
, int id
)
3764 DNPRINTF(SWM_D_STACK
, "horizontal_config: workspace: %d\n", ws
->idx
);
3767 case SWM_ARG_ID_STACKRESET
:
3768 case SWM_ARG_ID_STACKINIT
:
3769 ws
->l_state
.horizontal_mwin
= 1;
3770 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ 2;
3771 ws
->l_state
.horizontal_stacks
= 1;
3773 case SWM_ARG_ID_MASTERSHRINK
:
3774 if (ws
->l_state
.horizontal_msize
> 1)
3775 ws
->l_state
.horizontal_msize
--;
3777 case SWM_ARG_ID_MASTERGROW
:
3778 if (ws
->l_state
.horizontal_msize
< SWM_H_SLICE
- 1)
3779 ws
->l_state
.horizontal_msize
++;
3781 case SWM_ARG_ID_MASTERADD
:
3782 ws
->l_state
.horizontal_mwin
++;
3784 case SWM_ARG_ID_MASTERDEL
:
3785 if (ws
->l_state
.horizontal_mwin
> 0)
3786 ws
->l_state
.horizontal_mwin
--;
3788 case SWM_ARG_ID_STACKINC
:
3789 ws
->l_state
.horizontal_stacks
++;
3791 case SWM_ARG_ID_STACKDEC
:
3792 if (ws
->l_state
.horizontal_stacks
> 1)
3793 ws
->l_state
.horizontal_stacks
--;
3795 case SWM_ARG_ID_FLIPLAYOUT
:
3796 ws
->l_state
.horizontal_flip
= !ws
->l_state
.horizontal_flip
;
3804 horizontal_stack(struct workspace
*ws
, struct swm_geometry
*g
)
3806 DNPRINTF(SWM_D_STACK
, "horizontal_stack: workspace: %d\n", ws
->idx
);
3808 stack_master(ws
, g
, 1, ws
->l_state
.horizontal_flip
);
3811 /* fullscreen view */
3813 max_stack(struct workspace
*ws
, struct swm_geometry
*g
)
3815 struct swm_geometry gg
= *g
;
3816 struct ws_win
*win
, *wintrans
= NULL
, *parent
= NULL
;
3817 int winno
, num_screens
;
3819 DNPRINTF(SWM_D_STACK
, "max_stack: workspace: %d\n", ws
->idx
);
3824 winno
= count_win(ws
, 0);
3825 if (winno
== 0 && count_win(ws
, 1) == 0)
3828 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
3829 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3830 if (win
->transient
) {
3832 parent
= find_window(win
->transient
);
3836 if (win
->floating
&& win
->floatmaxed
== 0 ) {
3838 * retain geometry for retrieval on exit from
3841 store_float_geom(win
, ws
->r
);
3842 win
->floatmaxed
= 1;
3845 /* only reconfigure if necessary */
3846 if (X(win
) != gg
.x
|| Y(win
) != gg
.y
|| WIDTH(win
) != gg
.w
||
3847 HEIGHT(win
) != gg
.h
) {
3853 WIDTH(win
) += 2 * border_width
;
3854 HEIGHT(win
) += 2 * border_width
;
3859 /* unmap only if we don't have multi screen */
3860 if (win
!= ws
->focus
)
3861 if (!(num_screens
> 1 || outputs
> 1))
3865 /* put the last transient on top */
3869 stack_floater(wintrans
, ws
->r
);
3870 ws
->focus
= get_focus_magic(wintrans
);
3875 send_to_ws(struct swm_region
*r
, union arg
*args
)
3877 int wsid
= args
->id
;
3878 struct ws_win
*win
= NULL
, *parent
;
3879 struct workspace
*ws
, *nws
;
3880 char ws_idx_str
[SWM_PROPLEN
];
3883 if (wsid
>= workspace_limit
)
3886 if (r
&& r
->ws
&& r
->ws
->focus
)
3892 if (win
->ws
->idx
== wsid
)
3895 DNPRINTF(SWM_D_MOVE
, "send_to_ws: window: 0x%x\n", win
->id
);
3898 nws
= &win
->s
->ws
[wsid
];
3900 a
.id
= SWM_ARG_ID_FOCUSPREV
;
3902 if (win
->transient
) {
3903 parent
= find_window(win
->transient
);
3905 unmap_window(parent
);
3906 TAILQ_REMOVE(&ws
->winlist
, parent
, entry
);
3907 TAILQ_INSERT_TAIL(&nws
->winlist
, parent
, entry
);
3912 TAILQ_REMOVE(&ws
->winlist
, win
, entry
);
3913 TAILQ_INSERT_TAIL(&nws
->winlist
, win
, entry
);
3914 if (TAILQ_EMPTY(&ws
->winlist
))
3915 r
->ws
->focus
= NULL
;
3918 /* Try to update the window's workspace property */
3919 if (snprintf(ws_idx_str
, SWM_PROPLEN
, "%d", nws
->idx
) < SWM_PROPLEN
) {
3920 DNPRINTF(SWM_D_PROP
, "send_to_ws: set property: _SWM_WS: %s\n",
3922 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
3923 a_swm_ws
, XCB_ATOM_STRING
, 8, strlen(ws_idx_str
),
3934 pressbutton(struct swm_region
*r
, union arg
*args
)
3936 /* suppress unused warning since var is needed */
3939 xcb_test_fake_input(conn
, XCB_BUTTON_PRESS
, args
->id
,
3940 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
3941 xcb_test_fake_input(conn
, XCB_BUTTON_RELEASE
, args
->id
,
3942 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
3946 raise_toggle(struct swm_region
*r
, union arg
*args
)
3948 /* suppress unused warning since var is needed */
3951 if (r
== NULL
|| r
->ws
== NULL
)
3954 r
->ws
->always_raise
= !r
->ws
->always_raise
;
3956 /* bring floaters back to top */
3957 if (r
->ws
->always_raise
== 0)
3964 iconify(struct swm_region
*r
, union arg
*args
)
3966 /* suppress unused warning since var is needed */
3969 if (r
->ws
->focus
== NULL
)
3972 update_iconic(r
->ws
->focus
, 1);
3978 get_win_name(xcb_window_t win
)
3981 xcb_get_property_cookie_t c
;
3982 xcb_get_property_reply_t
*r
;
3984 /* First try _NET_WM_NAME for UTF-8. */
3985 c
= xcb_get_property(conn
, 0, win
, a_netwmname
,
3986 XCB_GET_PROPERTY_TYPE_ANY
, 0, UINT_MAX
);
3987 r
= xcb_get_property_reply(conn
, c
, NULL
);
3989 if (!r
|| r
->type
== XCB_NONE
) {
3991 /* Use WM_NAME instead; no UTF-8. */
3992 c
= xcb_get_property(conn
, 0, win
, XCB_ATOM_WM_NAME
,
3993 XCB_GET_PROPERTY_TYPE_ANY
, 0, UINT_MAX
);
3994 r
= xcb_get_property_reply(conn
, c
, NULL
);
3996 if(!r
|| r
->type
== XCB_NONE
) {
4003 name
= strndup(xcb_get_property_value(r
),
4004 xcb_get_property_value_length(r
));
4011 uniconify(struct swm_region
*r
, union arg
*args
)
4018 DNPRINTF(SWM_D_MISC
, "uniconify\n");
4020 if (r
== NULL
|| r
->ws
== NULL
)
4023 /* make sure we have anything to uniconify */
4024 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
4025 if (win
->ws
== NULL
)
4026 continue; /* should never happen */
4027 if (win
->iconic
== 0)
4035 search_resp_action
= SWM_SEARCH_UNICONIFY
;
4037 spawn_select(r
, args
, "search", &searchpid
);
4039 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
4042 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
4043 if (win
->ws
== NULL
)
4044 continue; /* should never happen */
4045 if (win
->iconic
== 0)
4048 name
= get_win_name(win
->id
);
4051 fprintf(lfile
, "%s.%u\n", name
, win
->id
);
4059 name_workspace(struct swm_region
*r
, union arg
*args
)
4063 DNPRINTF(SWM_D_MISC
, "name_workspace\n");
4069 search_resp_action
= SWM_SEARCH_NAME_WORKSPACE
;
4071 spawn_select(r
, args
, "name_workspace", &searchpid
);
4073 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
4076 fprintf(lfile
, "%s", "");
4081 search_workspace(struct swm_region
*r
, union arg
*args
)
4084 struct workspace
*ws
;
4087 DNPRINTF(SWM_D_MISC
, "search_workspace\n");
4093 search_resp_action
= SWM_SEARCH_SEARCH_WORKSPACE
;
4095 spawn_select(r
, args
, "search", &searchpid
);
4097 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
4100 for (i
= 0; i
< workspace_limit
; i
++) {
4104 fprintf(lfile
, "%d%s%s\n", ws
->idx
+ 1,
4105 (ws
->name
? ":" : ""), (ws
->name
? ws
->name
: ""));
4112 search_win_cleanup(void)
4114 struct search_window
*sw
= NULL
;
4116 while ((sw
= TAILQ_FIRST(&search_wl
)) != NULL
) {
4117 xcb_destroy_window(conn
, sw
->indicator
);
4118 TAILQ_REMOVE(&search_wl
, sw
, entry
);
4124 search_win(struct swm_region
*r
, union arg
*args
)
4126 struct ws_win
*win
= NULL
;
4127 struct search_window
*sw
= NULL
;
4130 int i
, width
, height
;
4138 XRectangle l_ibox
, l_lbox
;
4140 DNPRINTF(SWM_D_MISC
, "search_win\n");
4143 search_resp_action
= SWM_SEARCH_SEARCH_WINDOW
;
4145 spawn_select(r
, args
, "search", &searchpid
);
4147 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
4150 TAILQ_INIT(&search_wl
);
4153 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
4154 if (win
->iconic
== 1)
4157 sw
= calloc(1, sizeof(struct search_window
));
4159 warn("search_win: calloc");
4161 search_win_cleanup();
4167 snprintf(s
, sizeof s
, "%d", i
);
4170 w
= xcb_generate_id(conn
);
4171 wa
[0] = r
->s
->c
[SWM_S_COLOR_FOCUS
].pixel
;
4172 wa
[1] = r
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
;
4174 if (bar_font_legacy
) {
4175 XmbTextExtents(bar_fs
, s
, len
, &l_ibox
, &l_lbox
);
4176 width
= l_lbox
.width
+ 4;
4177 height
= bar_fs_extents
->max_logical_extent
.height
+ 4;
4179 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
,
4181 width
= info
.width
+ 4;
4182 height
= bar_font
->height
+ 4;
4185 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, w
, win
->id
, 0, 0,
4186 width
, height
, 1, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
4187 XCB_COPY_FROM_PARENT
, XCB_CW_BACK_PIXEL
|
4188 XCB_CW_BORDER_PIXEL
, wa
);
4190 xcb_map_window(conn
, w
);
4193 TAILQ_INSERT_TAIL(&search_wl
, sw
, entry
);
4195 if (bar_font_legacy
) {
4196 l_gcv
.graphics_exposures
= 0;
4197 l_draw
= XCreateGC(display
, w
, 0, &l_gcv
);
4199 XSetForeground(display
, l_draw
,
4200 r
->s
->c
[SWM_S_COLOR_BAR
].pixel
);
4202 DRAWSTRING(display
, w
, bar_fs
, l_draw
, 2,
4203 (bar_fs_extents
->max_logical_extent
.height
-
4204 l_lbox
.height
) / 2 - l_lbox
.y
, s
, len
);
4206 XFreeGC(display
, l_draw
);
4209 draw
= XftDrawCreate(display
, w
,
4210 DefaultVisual(display
, r
->s
->idx
),
4211 DefaultColormap(display
, r
->s
->idx
));
4213 XftDrawStringUtf8(draw
, &bar_font_color
, bar_font
, 2,
4214 (HEIGHT(r
->bar
) + bar_font
->height
) / 2 -
4215 bar_font
->descent
, (FcChar8
*)s
, len
);
4217 XftDrawDestroy(draw
);
4220 DNPRINTF(SWM_D_MISC
, "search_win: mapped window: 0x%x\n", w
);
4222 fprintf(lfile
, "%d\n", i
);
4232 search_resp_uniconify(char *resp
, unsigned long len
)
4238 DNPRINTF(SWM_D_MISC
, "search_resp_uniconify: resp: %s\n", resp
);
4240 TAILQ_FOREACH(win
, &search_r
->ws
->winlist
, entry
) {
4241 if (win
->iconic
== 0)
4243 name
= get_win_name(win
->id
);
4246 if (asprintf(&s
, "%s.%u", name
, win
->id
) == -1) {
4251 if (strncmp(s
, resp
, len
) == 0) {
4252 /* XXX this should be a callback to generalize */
4253 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]);
4412 wkill(struct swm_region
*r
, union arg
*args
)
4414 DNPRINTF(SWM_D_MISC
, "wkill: id: %d\n", args
->id
);
4416 if (r
->ws
->focus
== NULL
)
4419 if (args
->id
== SWM_ARG_ID_KILLWINDOW
)
4420 xcb_kill_client(conn
, r
->ws
->focus
->id
);
4422 if (r
->ws
->focus
->can_delete
)
4423 client_msg(r
->ws
->focus
, a_delete
, 0);
4430 floating_toggle_win(struct ws_win
*win
)
4432 struct swm_region
*r
;
4442 /* reject floating toggles in max stack mode */
4443 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
4446 if (win
->floating
) {
4447 if (!win
->floatmaxed
) {
4448 /* retain position for refloat */
4449 store_float_geom(win
, r
);
4453 load_float_geom(win
, r
);
4457 ewmh_update_actions(win
);
4463 floating_toggle(struct swm_region
*r
, union arg
*args
)
4465 struct ws_win
*win
= r
->ws
->focus
;
4467 /* suppress unused warning since var is needed */
4473 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
4476 ewmh_update_win_state(win
, ewmh
[_NET_WM_STATE_ABOVE
].atom
,
4477 _NET_WM_STATE_TOGGLE
);
4481 if (win
== win
->ws
->focus
)
4488 constrain_window(struct ws_win
*win
, struct swm_region
*r
, int resizable
)
4490 if (MAX_X(win
) + BORDER(win
) > MAX_X(r
)) {
4492 WIDTH(win
) = MAX_X(r
) - X(win
) - BORDER(win
);
4494 X(win
) = MAX_X(r
)- WIDTH(win
) - BORDER(win
);
4497 if (X(win
) + BORDER(win
) < X(r
)) {
4499 WIDTH(win
) -= X(r
) - X(win
) - BORDER(win
);
4501 X(win
) = X(r
) - BORDER(win
);
4504 if (MAX_Y(win
) + BORDER(win
) > MAX_Y(r
)) {
4506 HEIGHT(win
) = MAX_Y(r
) - Y(win
) - BORDER(win
);
4508 Y(win
) = MAX_Y(r
) - HEIGHT(win
) - BORDER(win
);
4511 if (Y(win
) + BORDER(win
) < Y(r
)) {
4513 HEIGHT(win
) -= Y(r
) - Y(win
) - BORDER(win
);
4515 Y(win
) = Y(r
) - BORDER(win
);
4521 if (HEIGHT(win
) < 1)
4527 update_window(struct ws_win
*win
)
4532 mask
= XCB_CONFIG_WINDOW_X
| XCB_CONFIG_WINDOW_Y
|
4533 XCB_CONFIG_WINDOW_WIDTH
| XCB_CONFIG_WINDOW_HEIGHT
|
4534 XCB_CONFIG_WINDOW_BORDER_WIDTH
;
4538 wc
[3] = HEIGHT(win
);
4539 wc
[4] = BORDER(win
);
4541 DNPRINTF(SWM_D_EVENT
, "update_window: window: 0x%x, (x,y) w x h: "
4542 "(%d,%d) %d x %d, bordered: %s\n", win
->id
, wc
[0], wc
[1], wc
[2],
4543 wc
[3], YESNO(win
->bordered
));
4545 xcb_configure_window(conn
, win
->id
, mask
, wc
);
4548 #define SWM_RESIZE_STEPS (50)
4551 resize(struct ws_win
*win
, union arg
*args
)
4553 xcb_timestamp_t timestamp
= 0;
4554 struct swm_region
*r
= NULL
;
4555 int resize_step
= 0;
4556 struct swm_geometry g
;
4557 int top
= 0, left
= 0, resizing
;
4559 unsigned int shape
; /* cursor style */
4560 xcb_cursor_t cursor
;
4561 xcb_font_t cursor_font
;
4562 xcb_query_pointer_reply_t
*xpr
;
4563 xcb_generic_event_t
*evt
;
4564 xcb_motion_notify_event_t
*mne
;
4570 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
4573 DNPRINTF(SWM_D_EVENT
, "resize: window: 0x%x, floating: %s, "
4574 "transient: 0x%x\n", win
->id
, YESNO(win
->floating
),
4577 if (!(win
->transient
!= 0 || win
->floating
!= 0))
4580 /* reject resizes in max mode for floaters (transient ok) */
4581 if (win
->floatmaxed
)
4585 ewmh_update_win_state(win
, ewmh
[_SWM_WM_STATE_MANUAL
].atom
,
4593 case SWM_ARG_ID_WIDTHSHRINK
:
4594 WIDTH(win
) -= SWM_RESIZE_STEPS
;
4597 case SWM_ARG_ID_WIDTHGROW
:
4598 WIDTH(win
) += SWM_RESIZE_STEPS
;
4601 case SWM_ARG_ID_HEIGHTSHRINK
:
4602 HEIGHT(win
) -= SWM_RESIZE_STEPS
;
4605 case SWM_ARG_ID_HEIGHTGROW
:
4606 HEIGHT(win
) += SWM_RESIZE_STEPS
;
4613 constrain_window(win
, r
, 1);
4615 store_float_geom(win
,r
);
4619 /* get cursor offset from window root */
4620 xpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
4627 if (xpr
->win_x
< WIDTH(win
) / 2)
4630 if (xpr
->win_y
< HEIGHT(win
) / 2)
4633 if (args
->id
== SWM_ARG_ID_CENTER
)
4636 shape
= (left
) ? XC_top_left_corner
: XC_top_right_corner
;
4638 shape
= (left
) ? XC_bottom_left_corner
: XC_bottom_right_corner
;
4640 cursor_font
= xcb_generate_id(conn
);
4641 xcb_open_font(conn
, cursor_font
, strlen("cursor"), "cursor");
4643 cursor
= xcb_generate_id(conn
);
4644 xcb_create_glyph_cursor(conn
, cursor
, cursor_font
, cursor_font
,
4645 shape
, shape
+ 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
4647 xcb_grab_pointer(conn
, 0, win
->id
, MOUSEMASK
,
4648 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
, XCB_WINDOW_NONE
, cursor
,
4653 while ((evt
= xcb_wait_for_event(conn
)) && resizing
) {
4654 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
4655 case XCB_BUTTON_RELEASE
:
4656 DNPRINTF(SWM_D_EVENT
, "resize: BUTTON_RELEASE\n");
4659 case XCB_MOTION_NOTIFY
:
4660 mne
= (xcb_motion_notify_event_t
*)evt
;
4661 /* cursor offset/delta from start of the operation */
4662 dx
= mne
->root_x
- xpr
->root_x
;
4663 dy
= mne
->root_y
- xpr
->root_y
;
4669 if (args
->id
== SWM_ARG_ID_CENTER
) {
4670 if (g
.h
/ 2 + dy
< 1)
4674 HEIGHT(win
) = g
.h
+ 2 * dy
;
4682 HEIGHT(win
) = g
.h
+ dy
;
4689 if (args
->id
== SWM_ARG_ID_CENTER
) {
4690 if (g
.w
/ 2 + dx
< 1)
4694 WIDTH(win
) = g
.w
+ 2 * dx
;
4702 WIDTH(win
) = g
.w
+ dx
;
4705 constrain_window(win
, r
, 1);
4707 /* not free, don't sync more than 120 times / second */
4708 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
4709 timestamp
= mne
->time
;
4724 store_float_geom(win
,r
);
4726 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
4727 xcb_free_cursor(conn
, cursor
);
4728 xcb_close_font(conn
, cursor_font
);
4730 DNPRINTF(SWM_D_EVENT
, "resize: done.\n");
4734 resize_step(struct swm_region
*r
, union arg
*args
)
4736 struct ws_win
*win
= NULL
;
4738 if (r
&& r
->ws
&& r
->ws
->focus
)
4746 #define SWM_MOVE_STEPS (50)
4749 move(struct ws_win
*win
, union arg
*args
)
4751 xcb_timestamp_t timestamp
= 0;
4752 int move_step
= 0, moving
;
4753 struct swm_region
*r
= NULL
;
4754 xcb_font_t cursor_font
;
4755 xcb_cursor_t cursor
;
4756 xcb_query_pointer_reply_t
*qpr
;
4757 xcb_generic_event_t
*evt
;
4758 xcb_motion_notify_event_t
*mne
;
4764 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
4767 DNPRINTF(SWM_D_EVENT
, "move: window: 0x%x, floating: %s, transient: "
4768 "0x%x\n", win
->id
, YESNO(win
->floating
), win
->transient
);
4770 /* in max_stack mode should only move transients */
4771 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
] && !win
->transient
)
4775 if (win
->floating
== 0 && !win
->transient
) {
4776 store_float_geom(win
, r
);
4777 ewmh_update_win_state(win
, ewmh
[_NET_WM_STATE_ABOVE
].atom
,
4780 ewmh_update_win_state(win
, ewmh
[_SWM_WM_STATE_MANUAL
].atom
,
4789 case SWM_ARG_ID_MOVELEFT
:
4790 X(win
) -= (SWM_MOVE_STEPS
- border_width
);
4793 case SWM_ARG_ID_MOVERIGHT
:
4794 X(win
) += (SWM_MOVE_STEPS
- border_width
);
4797 case SWM_ARG_ID_MOVEUP
:
4798 Y(win
) -= (SWM_MOVE_STEPS
- border_width
);
4801 case SWM_ARG_ID_MOVEDOWN
:
4802 Y(win
) += (SWM_MOVE_STEPS
- border_width
);
4809 constrain_window(win
, r
, 0);
4811 store_float_geom(win
, r
);
4815 cursor_font
= xcb_generate_id(conn
);
4816 xcb_open_font(conn
, cursor_font
, strlen("cursor"), "cursor");
4818 cursor
= xcb_generate_id(conn
);
4819 xcb_create_glyph_cursor(conn
, cursor
, cursor_font
, cursor_font
,
4820 XC_fleur
, XC_fleur
+ 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
4822 xcb_grab_pointer(conn
, 0, win
->id
, MOUSEMASK
,
4823 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
,
4824 XCB_WINDOW_NONE
, cursor
, XCB_CURRENT_TIME
);
4826 /* get cursor offset from window root */
4827 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
4830 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
4831 xcb_free_cursor(conn
, cursor
);
4832 xcb_close_font(conn
, cursor_font
);
4838 while ((evt
= xcb_wait_for_event(conn
)) && moving
) {
4839 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
4840 case XCB_BUTTON_RELEASE
:
4841 DNPRINTF(SWM_D_EVENT
, "move: BUTTON_RELEASE\n");
4844 case XCB_MOTION_NOTIFY
:
4845 mne
= (xcb_motion_notify_event_t
*)evt
;
4846 X(win
) = mne
->root_x
- qpr
->win_x
- border_width
;
4847 Y(win
) = mne
->root_y
- qpr
->win_y
- border_width
;
4849 constrain_window(win
, r
, 0);
4851 /* not free, don't sync more than 120 times / second */
4852 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
4853 timestamp
= mne
->time
;
4868 store_float_geom(win
, r
);
4870 xcb_free_cursor(conn
, cursor
);
4871 xcb_close_font(conn
, cursor_font
);
4872 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
4873 DNPRINTF(SWM_D_EVENT
, "move: done.\n");
4877 move_step(struct swm_region
*r
, union arg
*args
)
4879 struct ws_win
*win
= NULL
;
4881 if (r
&& r
->ws
&& r
->ws
->focus
)
4886 if (!(win
->transient
!= 0 || win
->floating
!= 0))
4893 /* user/key callable function IDs */
4943 KF_SEARCH_WORKSPACE
,
4984 KF_DUMPWINS
, /* MUST BE LAST */
4988 /* key definitions */
4990 char name
[SWM_FUNCNAME_LEN
];
4991 void (*func
)(struct swm_region
*r
, union arg
*);
4993 } keyfuncs
[KF_INVALID
+ 1] = {
4994 /* name function argument */
4995 { "bar_toggle", bar_toggle
, {0} },
4996 { "button2", pressbutton
, {2} },
4997 { "cycle_layout", cycle_layout
, {0} },
4998 { "flip_layout", stack_config
, {.id
= SWM_ARG_ID_FLIPLAYOUT
} },
4999 { "float_toggle", floating_toggle
,{0} },
5000 { "focus_main", focus
, {.id
= SWM_ARG_ID_FOCUSMAIN
} },
5001 { "focus_next", focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
5002 { "focus_prev", focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
5003 { "height_grow", resize_step
, {.id
= SWM_ARG_ID_HEIGHTGROW
} },
5004 { "height_shrink", resize_step
, {.id
= SWM_ARG_ID_HEIGHTSHRINK
} },
5005 { "iconify", iconify
, {0} },
5006 { "master_shrink", stack_config
, {.id
= SWM_ARG_ID_MASTERSHRINK
} },
5007 { "master_grow", stack_config
, {.id
= SWM_ARG_ID_MASTERGROW
} },
5008 { "master_add", stack_config
, {.id
= SWM_ARG_ID_MASTERADD
} },
5009 { "master_del", stack_config
, {.id
= SWM_ARG_ID_MASTERDEL
} },
5010 { "move_down", move_step
, {.id
= SWM_ARG_ID_MOVEDOWN
} },
5011 { "move_left", move_step
, {.id
= SWM_ARG_ID_MOVELEFT
} },
5012 { "move_right", move_step
, {.id
= SWM_ARG_ID_MOVERIGHT
} },
5013 { "move_up", move_step
, {.id
= SWM_ARG_ID_MOVEUP
} },
5014 { "mvws_1", send_to_ws
, {.id
= 0} },
5015 { "mvws_2", send_to_ws
, {.id
= 1} },
5016 { "mvws_3", send_to_ws
, {.id
= 2} },
5017 { "mvws_4", send_to_ws
, {.id
= 3} },
5018 { "mvws_5", send_to_ws
, {.id
= 4} },
5019 { "mvws_6", send_to_ws
, {.id
= 5} },
5020 { "mvws_7", send_to_ws
, {.id
= 6} },
5021 { "mvws_8", send_to_ws
, {.id
= 7} },
5022 { "mvws_9", send_to_ws
, {.id
= 8} },
5023 { "mvws_10", send_to_ws
, {.id
= 9} },
5024 { "mvws_11", send_to_ws
, {.id
= 10} },
5025 { "mvws_12", send_to_ws
, {.id
= 11} },
5026 { "mvws_13", send_to_ws
, {.id
= 12} },
5027 { "mvws_14", send_to_ws
, {.id
= 13} },
5028 { "mvws_15", send_to_ws
, {.id
= 14} },
5029 { "mvws_16", send_to_ws
, {.id
= 15} },
5030 { "mvws_17", send_to_ws
, {.id
= 16} },
5031 { "mvws_18", send_to_ws
, {.id
= 17} },
5032 { "mvws_19", send_to_ws
, {.id
= 18} },
5033 { "mvws_20", send_to_ws
, {.id
= 19} },
5034 { "mvws_21", send_to_ws
, {.id
= 20} },
5035 { "mvws_22", send_to_ws
, {.id
= 21} },
5036 { "name_workspace", name_workspace
, {0} },
5037 { "quit", quit
, {0} },
5038 { "raise_toggle", raise_toggle
, {0} },
5039 { "restart", restart
, {0} },
5040 { "screen_next", cyclescr
, {.id
= SWM_ARG_ID_CYCLESC_UP
} },
5041 { "screen_prev", cyclescr
, {.id
= SWM_ARG_ID_CYCLESC_DOWN
} },
5042 { "search_win", search_win
, {0} },
5043 { "search_workspace", search_workspace
, {0} },
5044 { "spawn_custom", NULL
, {0} },
5045 { "stack_inc", stack_config
, {.id
= SWM_ARG_ID_STACKINC
} },
5046 { "stack_dec", stack_config
, {.id
= SWM_ARG_ID_STACKDEC
} },
5047 { "stack_reset", stack_config
, {.id
= SWM_ARG_ID_STACKRESET
} },
5048 { "swap_main", swapwin
, {.id
= SWM_ARG_ID_SWAPMAIN
} },
5049 { "swap_next", swapwin
, {.id
= SWM_ARG_ID_SWAPNEXT
} },
5050 { "swap_prev", swapwin
, {.id
= SWM_ARG_ID_SWAPPREV
} },
5051 { "uniconify", uniconify
, {0} },
5052 { "version", version
, {0} },
5053 { "width_grow", resize_step
, {.id
= SWM_ARG_ID_WIDTHGROW
} },
5054 { "width_shrink", resize_step
, {.id
= SWM_ARG_ID_WIDTHSHRINK
} },
5055 { "wind_del", wkill
, {.id
= SWM_ARG_ID_DELETEWINDOW
} },
5056 { "wind_kill", wkill
, {.id
= SWM_ARG_ID_KILLWINDOW
} },
5057 { "ws_1", switchws
, {.id
= 0} },
5058 { "ws_2", switchws
, {.id
= 1} },
5059 { "ws_3", switchws
, {.id
= 2} },
5060 { "ws_4", switchws
, {.id
= 3} },
5061 { "ws_5", switchws
, {.id
= 4} },
5062 { "ws_6", switchws
, {.id
= 5} },
5063 { "ws_7", switchws
, {.id
= 6} },
5064 { "ws_8", switchws
, {.id
= 7} },
5065 { "ws_9", switchws
, {.id
= 8} },
5066 { "ws_10", switchws
, {.id
= 9} },
5067 { "ws_11", switchws
, {.id
= 10} },
5068 { "ws_12", switchws
, {.id
= 11} },
5069 { "ws_13", switchws
, {.id
= 12} },
5070 { "ws_14", switchws
, {.id
= 13} },
5071 { "ws_15", switchws
, {.id
= 14} },
5072 { "ws_16", switchws
, {.id
= 15} },
5073 { "ws_17", switchws
, {.id
= 16} },
5074 { "ws_18", switchws
, {.id
= 17} },
5075 { "ws_19", switchws
, {.id
= 18} },
5076 { "ws_20", switchws
, {.id
= 19} },
5077 { "ws_21", switchws
, {.id
= 20} },
5078 { "ws_22", switchws
, {.id
= 21} },
5079 { "ws_next", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP
} },
5080 { "ws_next_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP_ALL
} },
5081 { "ws_prev", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN
} },
5082 { "ws_prev_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN_ALL
} },
5083 { "ws_prior", priorws
, {0} },
5084 { "dumpwins", dumpwins
, {0} }, /* MUST BE LAST */
5085 { "invalid key func", NULL
, {0} },
5088 RB_ENTRY(key
) entry
;
5091 enum keyfuncid funcid
;
5094 RB_HEAD(key_tree
, key
);
5097 key_cmp(struct key
*kp1
, struct key
*kp2
)
5099 if (kp1
->keysym
< kp2
->keysym
)
5101 if (kp1
->keysym
> kp2
->keysym
)
5104 if (kp1
->mod
< kp2
->mod
)
5106 if (kp1
->mod
> kp2
->mod
)
5112 RB_GENERATE(key_tree
, key
, entry
, key_cmp
);
5113 struct key_tree keys
;
5116 enum { client_click
, root_click
};
5118 unsigned int action
;
5120 unsigned int button
;
5121 void (*func
)(struct ws_win
*, union arg
*);
5124 #define MODKEY_SHIFT MODKEY | XCB_MOD_MASK_SHIFT
5125 /* action key mouse button func args */
5126 { client_click
, MODKEY
, XCB_BUTTON_INDEX_3
, resize
, {.id
= SWM_ARG_ID_DONTCENTER
} },
5127 { client_click
, MODKEY_SHIFT
, XCB_BUTTON_INDEX_3
, resize
, {.id
= SWM_ARG_ID_CENTER
} },
5128 { client_click
, MODKEY
, XCB_BUTTON_INDEX_1
, move
, {0} },
5133 update_modkey(unsigned int mod
)
5139 RB_FOREACH(kp
, key_tree
, &keys
)
5140 if (kp
->mod
& XCB_MOD_MASK_SHIFT
)
5141 kp
->mod
= mod
| XCB_MOD_MASK_SHIFT
;
5145 for (i
= 0; i
< LENGTH(buttons
); i
++)
5146 if (buttons
[i
].mask
& XCB_MOD_MASK_SHIFT
)
5147 buttons
[i
].mask
= mod
| XCB_MOD_MASK_SHIFT
;
5149 buttons
[i
].mask
= mod
;
5154 TAILQ_ENTRY(spawn_prog
) entry
;
5159 TAILQ_HEAD(spawn_list
, spawn_prog
);
5160 struct spawn_list spawns
= TAILQ_HEAD_INITIALIZER(spawns
);
5163 spawn_expand(struct swm_region
*r
, union arg
*args
, char *spawn_name
,
5166 struct spawn_prog
*prog
= NULL
;
5168 char *ap
, **real_args
;
5170 /* suppress unused warning since var is needed */
5173 DNPRINTF(SWM_D_SPAWN
, "spawn_expand: %s\n", spawn_name
);
5176 TAILQ_FOREACH(prog
, &spawns
, entry
) {
5177 if (!strcasecmp(spawn_name
, prog
->name
))
5181 warnx("spawn_custom: program %s not found", spawn_name
);
5185 /* make room for expanded args */
5186 if ((real_args
= calloc(prog
->argc
+ 1, sizeof(char *))) == NULL
)
5187 err(1, "spawn_custom: calloc real_args");
5189 /* expand spawn_args into real_args */
5190 for (i
= 0; i
< prog
->argc
; i
++) {
5192 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: raw arg: %s\n", ap
);
5193 if (!strcasecmp(ap
, "$bar_border")) {
5195 strdup(r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].name
))
5197 err(1, "spawn_custom border color");
5198 } else if (!strcasecmp(ap
, "$bar_color")) {
5200 strdup(r
->s
->c
[SWM_S_COLOR_BAR
].name
))
5202 err(1, "spawn_custom bar color");
5203 } else if (!strcasecmp(ap
, "$bar_font")) {
5204 if ((real_args
[i
] = strdup(bar_fonts
))
5206 err(1, "spawn_custom bar fonts");
5207 } else if (!strcasecmp(ap
, "$bar_font_color")) {
5209 strdup(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].name
))
5211 err(1, "spawn_custom color font");
5212 } else if (!strcasecmp(ap
, "$color_focus")) {
5214 strdup(r
->s
->c
[SWM_S_COLOR_FOCUS
].name
))
5216 err(1, "spawn_custom color focus");
5217 } else if (!strcasecmp(ap
, "$color_unfocus")) {
5219 strdup(r
->s
->c
[SWM_S_COLOR_UNFOCUS
].name
))
5221 err(1, "spawn_custom color unfocus");
5223 /* no match --> copy as is */
5224 if ((real_args
[i
] = strdup(ap
)) == NULL
)
5225 err(1, "spawn_custom strdup(ap)");
5227 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: cooked arg: %s\n",
5232 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: result: ");
5233 for (i
= 0; i
< prog
->argc
; i
++)
5234 DNPRINTF(SWM_D_SPAWN
, "\"%s\" ", real_args
[i
]);
5235 DNPRINTF(SWM_D_SPAWN
, "\n");
5237 *ret_args
= real_args
;
5238 return (prog
->argc
);
5242 spawn_custom(struct swm_region
*r
, union arg
*args
, char *spawn_name
)
5248 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
5252 spawn(r
->ws
->idx
, &a
, 1);
5254 for (i
= 0; i
< spawn_argc
; i
++)
5260 spawn_select(struct swm_region
*r
, union arg
*args
, char *spawn_name
, int *pid
)
5266 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
5270 if (pipe(select_list_pipe
) == -1)
5271 err(1, "pipe error");
5272 if (pipe(select_resp_pipe
) == -1)
5273 err(1, "pipe error");
5275 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
5276 err(1, "could not disable SIGPIPE");
5277 switch (*pid
= fork()) {
5279 err(1, "cannot fork");
5282 if (dup2(select_list_pipe
[0], 0) == -1)
5284 if (dup2(select_resp_pipe
[1], 1) == -1)
5286 close(select_list_pipe
[1]);
5287 close(select_resp_pipe
[0]);
5288 spawn(r
->ws
->idx
, &a
, 0);
5290 default: /* parent */
5291 close(select_list_pipe
[0]);
5292 close(select_resp_pipe
[1]);
5296 for (i
= 0; i
< spawn_argc
; i
++)
5302 spawn_insert(char *name
, char *args
)
5304 char *arg
, *cp
, *ptr
;
5305 struct spawn_prog
*sp
;
5307 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: %s\n", name
);
5309 if ((sp
= calloc(1, sizeof *sp
)) == NULL
)
5310 err(1, "spawn_insert: calloc");
5311 if ((sp
->name
= strdup(name
)) == NULL
)
5312 err(1, "spawn_insert: strdup");
5314 /* convert the arguments to an argument list */
5315 if ((ptr
= cp
= strdup(args
)) == NULL
)
5316 err(1, "spawn_insert: strdup");
5317 while ((arg
= strsep(&ptr
, " \t")) != NULL
) {
5318 /* empty field; skip it */
5323 if ((sp
->argv
= realloc(sp
->argv
, sp
->argc
*
5324 sizeof *sp
->argv
)) == NULL
)
5325 err(1, "spawn_insert: realloc");
5326 if ((sp
->argv
[sp
->argc
- 1] = strdup(arg
)) == NULL
)
5327 err(1, "spawn_insert: strdup");
5331 TAILQ_INSERT_TAIL(&spawns
, sp
, entry
);
5332 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: leave\n");
5336 spawn_remove(struct spawn_prog
*sp
)
5340 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: %s\n", sp
->name
);
5342 TAILQ_REMOVE(&spawns
, sp
, entry
);
5343 for (i
= 0; i
< sp
->argc
; i
++)
5349 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: leave\n");
5353 spawn_replace(struct spawn_prog
*sp
, char *name
, char *args
)
5355 DNPRINTF(SWM_D_SPAWN
, "spawn_replace: %s [%s]\n", sp
->name
, name
);
5358 spawn_insert(name
, args
);
5360 DNPRINTF(SWM_D_SPAWN
, "spawn_replace: leave\n");
5364 setspawn(char *name
, char *args
)
5366 struct spawn_prog
*sp
;
5368 DNPRINTF(SWM_D_SPAWN
, "setspawn: %s\n", name
);
5373 TAILQ_FOREACH(sp
, &spawns
, entry
) {
5374 if (!strcmp(sp
->name
, name
)) {
5378 spawn_replace(sp
, name
, args
);
5379 DNPRINTF(SWM_D_SPAWN
, "setspawn: leave\n");
5383 if (*args
== '\0') {
5384 warnx("error: setspawn: cannot find program: %s", name
);
5388 spawn_insert(name
, args
);
5389 DNPRINTF(SWM_D_SPAWN
, "setspawn: leave\n");
5393 setconfspawn(char *selector
, char *value
, int flags
)
5397 /* suppress unused warning since var is needed */
5400 args
= expand_tilde(value
);
5402 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: [%s] [%s]\n", selector
, args
);
5404 setspawn(selector
, args
);
5407 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: done.\n");
5414 setconfspawn("term", "xterm", 0);
5415 setconfspawn("spawn_term", "xterm", 0);
5416 setconfspawn("screenshot_all", "screenshot.sh full", 0);
5417 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
5418 setconfspawn("lock", "xlock", 0);
5419 setconfspawn("initscr", "initscreen.sh", 0);
5420 setconfspawn("menu", "dmenu_run"
5423 " -nf $bar_font_color"
5425 " -sf $bar_color", 0);
5426 setconfspawn("search", "dmenu"
5430 " -nf $bar_font_color"
5432 " -sf $bar_color", 0);
5433 setconfspawn("name_workspace", "dmenu"
5437 " -nf $bar_font_color"
5439 " -sf $bar_color", 0);
5443 #define SWM_MODNAME_SIZE 32
5444 #define SWM_KEY_WS "\n+ \t"
5446 parsekeys(char *keystr
, unsigned int currmod
, unsigned int *mod
, KeySym
*ks
)
5450 DNPRINTF(SWM_D_KEY
, "parsekeys: enter [%s]\n", keystr
);
5451 if (mod
== NULL
|| ks
== NULL
) {
5452 DNPRINTF(SWM_D_KEY
, "parsekeys: no mod or key vars\n");
5455 if (keystr
== NULL
|| strlen(keystr
) == 0) {
5456 DNPRINTF(SWM_D_KEY
, "parsekeys: no keystr\n");
5460 *ks
= XCB_NO_SYMBOL
;
5462 while ((name
= strsep(&cp
, SWM_KEY_WS
)) != NULL
) {
5463 DNPRINTF(SWM_D_KEY
, "parsekeys: key [%s]\n", name
);
5465 cp
+= (long)strspn(cp
, SWM_KEY_WS
);
5466 if (strncasecmp(name
, "MOD", SWM_MODNAME_SIZE
) == 0)
5468 else if (!strncasecmp(name
, "Mod1", SWM_MODNAME_SIZE
))
5469 *mod
|= XCB_MOD_MASK_1
;
5470 else if (!strncasecmp(name
, "Mod2", SWM_MODNAME_SIZE
))
5471 *mod
+= XCB_MOD_MASK_2
;
5472 else if (!strncmp(name
, "Mod3", SWM_MODNAME_SIZE
))
5473 *mod
|= XCB_MOD_MASK_3
;
5474 else if (!strncmp(name
, "Mod4", SWM_MODNAME_SIZE
))
5475 *mod
|= XCB_MOD_MASK_4
;
5476 else if (strncasecmp(name
, "SHIFT", SWM_MODNAME_SIZE
) == 0)
5477 *mod
|= XCB_MOD_MASK_SHIFT
;
5478 else if (strncasecmp(name
, "CONTROL", SWM_MODNAME_SIZE
) == 0)
5479 *mod
|= XCB_MOD_MASK_CONTROL
;
5481 *ks
= XStringToKeysym(name
);
5482 XConvertCase(*ks
, ks
, &uks
);
5483 if (ks
== XCB_NO_SYMBOL
) {
5485 "parsekeys: invalid key %s\n",
5491 DNPRINTF(SWM_D_KEY
, "parsekeys: leave ok\n");
5496 strdupsafe(char *str
)
5501 return (strdup(str
));
5505 key_insert(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
, char *spawn_name
)
5509 DNPRINTF(SWM_D_KEY
, "key_insert: enter %s [%s]\n",
5510 keyfuncs
[kfid
].name
, spawn_name
);
5512 if ((kp
= malloc(sizeof *kp
)) == NULL
)
5513 err(1, "key_insert: malloc");
5518 kp
->spawn_name
= strdupsafe(spawn_name
);
5519 RB_INSERT(key_tree
, &keys
, kp
);
5521 DNPRINTF(SWM_D_KEY
, "key_insert: leave\n");
5525 key_lookup(unsigned int mod
, KeySym ks
)
5532 return (RB_FIND(key_tree
, &keys
, &kp
));
5536 key_remove(struct key
*kp
)
5538 DNPRINTF(SWM_D_KEY
, "key_remove: %s\n", keyfuncs
[kp
->funcid
].name
);
5540 RB_REMOVE(key_tree
, &keys
, kp
);
5541 free(kp
->spawn_name
);
5544 DNPRINTF(SWM_D_KEY
, "key_remove: leave\n");
5548 key_replace(struct key
*kp
, unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
5551 DNPRINTF(SWM_D_KEY
, "key_replace: %s [%s]\n", keyfuncs
[kp
->funcid
].name
,
5555 key_insert(mod
, ks
, kfid
, spawn_name
);
5557 DNPRINTF(SWM_D_KEY
, "key_replace: leave\n");
5561 setkeybinding(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
5566 DNPRINTF(SWM_D_KEY
, "setkeybinding: enter %s [%s]\n",
5567 keyfuncs
[kfid
].name
, spawn_name
);
5569 if ((kp
= key_lookup(mod
, ks
)) != NULL
) {
5570 if (kfid
== KF_INVALID
)
5573 key_replace(kp
, mod
, ks
, kfid
, spawn_name
);
5574 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
5577 if (kfid
== KF_INVALID
) {
5578 warnx("error: setkeybinding: cannot find mod/key combination");
5579 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
5583 key_insert(mod
, ks
, kfid
, spawn_name
);
5584 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
5588 setconfbinding(char *selector
, char *value
, int flags
)
5590 enum keyfuncid kfid
;
5593 struct spawn_prog
*sp
;
5595 /* suppress unused warning since var is needed */
5598 DNPRINTF(SWM_D_KEY
, "setconfbinding: enter\n");
5599 if (selector
== NULL
) {
5600 DNPRINTF(SWM_D_KEY
, "setconfbinding: unbind %s\n", value
);
5601 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
5603 setkeybinding(mod
, ks
, kfid
, NULL
);
5608 /* search by key function name */
5609 for (kfid
= 0; kfid
< KF_INVALID
; (kfid
)++) {
5610 if (strncasecmp(selector
, keyfuncs
[kfid
].name
,
5611 SWM_FUNCNAME_LEN
) == 0) {
5612 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match\n",
5614 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
5615 setkeybinding(mod
, ks
, kfid
, NULL
);
5621 /* search by custom spawn name */
5622 TAILQ_FOREACH(sp
, &spawns
, entry
) {
5623 if (strcasecmp(selector
, sp
->name
) == 0) {
5624 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match\n",
5626 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
5627 setkeybinding(mod
, ks
, KF_SPAWN_CUSTOM
,
5634 DNPRINTF(SWM_D_KEY
, "setconfbinding: no match\n");
5641 #define MODKEY_SHIFT MODKEY | XCB_MOD_MASK_SHIFT
5642 setkeybinding(MODKEY
, XK_space
, KF_CYCLE_LAYOUT
,NULL
);
5643 setkeybinding(MODKEY_SHIFT
, XK_backslash
, KF_FLIP_LAYOUT
, NULL
);
5644 setkeybinding(MODKEY_SHIFT
, XK_space
, KF_STACK_RESET
, NULL
);
5645 setkeybinding(MODKEY
, XK_h
, KF_MASTER_SHRINK
, NULL
);
5646 setkeybinding(MODKEY
, XK_l
, KF_MASTER_GROW
, NULL
);
5647 setkeybinding(MODKEY
, XK_comma
, KF_MASTER_ADD
, NULL
);
5648 setkeybinding(MODKEY
, XK_period
, KF_MASTER_DEL
, NULL
);
5649 setkeybinding(MODKEY_SHIFT
, XK_comma
, KF_STACK_INC
, NULL
);
5650 setkeybinding(MODKEY_SHIFT
, XK_period
, KF_STACK_DEC
, NULL
);
5651 setkeybinding(MODKEY
, XK_Return
, KF_SWAP_MAIN
, NULL
);
5652 setkeybinding(MODKEY
, XK_j
, KF_FOCUS_NEXT
, NULL
);
5653 setkeybinding(MODKEY
, XK_k
, KF_FOCUS_PREV
, NULL
);
5654 setkeybinding(MODKEY_SHIFT
, XK_j
, KF_SWAP_NEXT
, NULL
);
5655 setkeybinding(MODKEY_SHIFT
, XK_k
, KF_SWAP_PREV
, NULL
);
5656 setkeybinding(MODKEY_SHIFT
, XK_Return
, KF_SPAWN_CUSTOM
,"term");
5657 setkeybinding(MODKEY
, XK_p
, KF_SPAWN_CUSTOM
,"menu");
5658 setkeybinding(MODKEY_SHIFT
, XK_q
, KF_QUIT
, NULL
);
5659 setkeybinding(MODKEY
, XK_q
, KF_RESTART
, NULL
);
5660 setkeybinding(MODKEY
, XK_m
, KF_FOCUS_MAIN
, NULL
);
5661 setkeybinding(MODKEY
, XK_1
, KF_WS_1
, NULL
);
5662 setkeybinding(MODKEY
, XK_2
, KF_WS_2
, NULL
);
5663 setkeybinding(MODKEY
, XK_3
, KF_WS_3
, NULL
);
5664 setkeybinding(MODKEY
, XK_4
, KF_WS_4
, NULL
);
5665 setkeybinding(MODKEY
, XK_5
, KF_WS_5
, NULL
);
5666 setkeybinding(MODKEY
, XK_6
, KF_WS_6
, NULL
);
5667 setkeybinding(MODKEY
, XK_7
, KF_WS_7
, NULL
);
5668 setkeybinding(MODKEY
, XK_8
, KF_WS_8
, NULL
);
5669 setkeybinding(MODKEY
, XK_9
, KF_WS_9
, NULL
);
5670 setkeybinding(MODKEY
, XK_0
, KF_WS_10
, NULL
);
5671 setkeybinding(MODKEY
, XK_F1
, KF_WS_11
, NULL
);
5672 setkeybinding(MODKEY
, XK_F2
, KF_WS_12
, NULL
);
5673 setkeybinding(MODKEY
, XK_F3
, KF_WS_13
, NULL
);
5674 setkeybinding(MODKEY
, XK_F4
, KF_WS_14
, NULL
);
5675 setkeybinding(MODKEY
, XK_F5
, KF_WS_15
, NULL
);
5676 setkeybinding(MODKEY
, XK_F6
, KF_WS_16
, NULL
);
5677 setkeybinding(MODKEY
, XK_F7
, KF_WS_17
, NULL
);
5678 setkeybinding(MODKEY
, XK_F8
, KF_WS_18
, NULL
);
5679 setkeybinding(MODKEY
, XK_F9
, KF_WS_19
, NULL
);
5680 setkeybinding(MODKEY
, XK_F10
, KF_WS_20
, NULL
);
5681 setkeybinding(MODKEY
, XK_F11
, KF_WS_21
, NULL
);
5682 setkeybinding(MODKEY
, XK_F12
, KF_WS_22
, NULL
);
5683 setkeybinding(MODKEY
, XK_Right
, KF_WS_NEXT
, NULL
);
5684 setkeybinding(MODKEY
, XK_Left
, KF_WS_PREV
, NULL
);
5685 setkeybinding(MODKEY
, XK_Up
, KF_WS_NEXT_ALL
, NULL
);
5686 setkeybinding(MODKEY
, XK_Down
, KF_WS_PREV_ALL
, NULL
);
5687 setkeybinding(MODKEY
, XK_a
, KF_WS_PRIOR
, NULL
);
5688 setkeybinding(MODKEY_SHIFT
, XK_Right
, KF_SCREEN_NEXT
, NULL
);
5689 setkeybinding(MODKEY_SHIFT
, XK_Left
, KF_SCREEN_PREV
, NULL
);
5690 setkeybinding(MODKEY_SHIFT
, XK_1
, KF_MVWS_1
, NULL
);
5691 setkeybinding(MODKEY_SHIFT
, XK_2
, KF_MVWS_2
, NULL
);
5692 setkeybinding(MODKEY_SHIFT
, XK_3
, KF_MVWS_3
, NULL
);
5693 setkeybinding(MODKEY_SHIFT
, XK_4
, KF_MVWS_4
, NULL
);
5694 setkeybinding(MODKEY_SHIFT
, XK_5
, KF_MVWS_5
, NULL
);
5695 setkeybinding(MODKEY_SHIFT
, XK_6
, KF_MVWS_6
, NULL
);
5696 setkeybinding(MODKEY_SHIFT
, XK_7
, KF_MVWS_7
, NULL
);
5697 setkeybinding(MODKEY_SHIFT
, XK_8
, KF_MVWS_8
, NULL
);
5698 setkeybinding(MODKEY_SHIFT
, XK_9
, KF_MVWS_9
, NULL
);
5699 setkeybinding(MODKEY_SHIFT
, XK_0
, KF_MVWS_10
, NULL
);
5700 setkeybinding(MODKEY_SHIFT
, XK_F1
, KF_MVWS_11
, NULL
);
5701 setkeybinding(MODKEY_SHIFT
, XK_F2
, KF_MVWS_12
, NULL
);
5702 setkeybinding(MODKEY_SHIFT
, XK_F3
, KF_MVWS_13
, NULL
);
5703 setkeybinding(MODKEY_SHIFT
, XK_F4
, KF_MVWS_14
, NULL
);
5704 setkeybinding(MODKEY_SHIFT
, XK_F5
, KF_MVWS_15
, NULL
);
5705 setkeybinding(MODKEY_SHIFT
, XK_F6
, KF_MVWS_16
, NULL
);
5706 setkeybinding(MODKEY_SHIFT
, XK_F7
, KF_MVWS_17
, NULL
);
5707 setkeybinding(MODKEY_SHIFT
, XK_F8
, KF_MVWS_18
, NULL
);
5708 setkeybinding(MODKEY_SHIFT
, XK_F9
, KF_MVWS_19
, NULL
);
5709 setkeybinding(MODKEY_SHIFT
, XK_F10
, KF_MVWS_20
, NULL
);
5710 setkeybinding(MODKEY_SHIFT
, XK_F11
, KF_MVWS_21
, NULL
);
5711 setkeybinding(MODKEY_SHIFT
, XK_F12
, KF_MVWS_22
, NULL
);
5712 setkeybinding(MODKEY
, XK_b
, KF_BAR_TOGGLE
, NULL
);
5713 setkeybinding(MODKEY
, XK_Tab
, KF_FOCUS_NEXT
, NULL
);
5714 setkeybinding(MODKEY_SHIFT
, XK_Tab
, KF_FOCUS_PREV
, NULL
);
5715 setkeybinding(MODKEY_SHIFT
, XK_x
, KF_WIND_KILL
, NULL
);
5716 setkeybinding(MODKEY
, XK_x
, KF_WIND_DEL
, NULL
);
5717 setkeybinding(MODKEY
, XK_s
, KF_SPAWN_CUSTOM
,"screenshot_all");
5718 setkeybinding(MODKEY_SHIFT
, XK_s
, KF_SPAWN_CUSTOM
,"screenshot_wind");
5719 setkeybinding(MODKEY
, XK_t
, KF_FLOAT_TOGGLE
,NULL
);
5720 setkeybinding(MODKEY_SHIFT
, XK_v
, KF_VERSION
, NULL
);
5721 setkeybinding(MODKEY_SHIFT
, XK_Delete
, KF_SPAWN_CUSTOM
,"lock");
5722 setkeybinding(MODKEY_SHIFT
, XK_i
, KF_SPAWN_CUSTOM
,"initscr");
5723 setkeybinding(MODKEY
, XK_w
, KF_ICONIFY
, NULL
);
5724 setkeybinding(MODKEY_SHIFT
, XK_w
, KF_UNICONIFY
, NULL
);
5725 setkeybinding(MODKEY_SHIFT
, XK_r
, KF_RAISE_TOGGLE
,NULL
);
5726 setkeybinding(MODKEY
, XK_v
, KF_BUTTON2
, NULL
);
5727 setkeybinding(MODKEY
, XK_equal
, KF_WIDTH_GROW
, NULL
);
5728 setkeybinding(MODKEY
, XK_minus
, KF_WIDTH_SHRINK
,NULL
);
5729 setkeybinding(MODKEY_SHIFT
, XK_equal
, KF_HEIGHT_GROW
,NULL
);
5730 setkeybinding(MODKEY_SHIFT
, XK_minus
, KF_HEIGHT_SHRINK
,NULL
);
5731 setkeybinding(MODKEY
, XK_bracketleft
, KF_MOVE_LEFT
,NULL
);
5732 setkeybinding(MODKEY
, XK_bracketright
,KF_MOVE_RIGHT
,NULL
);
5733 setkeybinding(MODKEY_SHIFT
, XK_bracketleft
, KF_MOVE_UP
, NULL
);
5734 setkeybinding(MODKEY_SHIFT
, XK_bracketright
,KF_MOVE_DOWN
,NULL
);
5735 setkeybinding(MODKEY_SHIFT
, XK_slash
, KF_NAME_WORKSPACE
,NULL
);
5736 setkeybinding(MODKEY
, XK_slash
, KF_SEARCH_WORKSPACE
,NULL
);
5737 setkeybinding(MODKEY
, XK_f
, KF_SEARCH_WIN
, NULL
);
5739 setkeybinding(MODKEY_SHIFT
, XK_d
, KF_DUMPWINS
, NULL
);
5749 while (RB_EMPTY(&keys
) == 0) {
5750 kp
= RB_ROOT(&keys
);
5756 setkeymapping(char *selector
, char *value
, int flags
)
5758 char keymapping_file
[PATH_MAX
];
5760 /* suppress unused warnings since vars are needed */
5764 DNPRINTF(SWM_D_KEY
, "setkeymapping: enter\n");
5765 if (value
[0] == '~')
5766 snprintf(keymapping_file
, sizeof keymapping_file
, "%s/%s",
5767 pwd
->pw_dir
, &value
[1]);
5769 strlcpy(keymapping_file
, value
, sizeof keymapping_file
);
5771 /* load new key bindings; if it fails, revert to default bindings */
5772 if (conf_load(keymapping_file
, SWM_CONF_KEYMAPPING
)) {
5776 DNPRINTF(SWM_D_KEY
, "setkeymapping: leave\n");
5781 updatenumlockmask(void)
5784 xcb_get_modifier_mapping_reply_t
*modmap_r
;
5785 xcb_keycode_t
*modmap
, kc
, *keycode
;
5789 modmap_r
= xcb_get_modifier_mapping_reply(conn
,
5790 xcb_get_modifier_mapping(conn
),
5793 modmap
= xcb_get_modifier_mapping_keycodes(modmap_r
);
5794 for (i
= 0; i
< 8; i
++) {
5795 for (j
= 0; j
< modmap_r
->keycodes_per_modifier
; j
++) {
5796 kc
= modmap
[i
* modmap_r
->keycodes_per_modifier
5798 keycode
= xcb_key_symbols_get_keycode(syms
,
5801 numlockmask
= (1 << i
);
5807 DNPRINTF(SWM_D_MISC
, "updatenumlockmask: %d\n", numlockmask
);
5814 int num_screens
, k
, j
;
5815 unsigned int modifiers
[3];
5816 xcb_keycode_t
*code
;
5818 DNPRINTF(SWM_D_MISC
, "grabkeys\n");
5819 updatenumlockmask();
5822 modifiers
[1] = numlockmask
;
5823 modifiers
[2] = numlockmask
| XCB_MOD_MASK_LOCK
;
5825 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
5826 for (k
= 0; k
< num_screens
; k
++) {
5827 if (TAILQ_EMPTY(&screens
[k
].rl
))
5829 xcb_ungrab_key(conn
, XCB_GRAB_ANY
, screens
[k
].root
,
5831 RB_FOREACH(kp
, key_tree
, &keys
) {
5832 if ((code
= xcb_key_symbols_get_keycode(syms
,
5834 for (j
= 0; j
< LENGTH(modifiers
); j
++)
5835 xcb_grab_key(conn
, 1,
5837 kp
->mod
| modifiers
[j
],
5838 *code
, XCB_GRAB_MODE_SYNC
,
5839 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
, uint16_t mapped
)
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
->mapped
= mapped
;
6796 win
->floatmaxed
= 0;
6797 win
->ewmh_flags
= 0;
6798 win
->s
= r
->s
; /* this never changes */
6800 store_float_geom(win
, r
);
6802 /* Get WM_SIZE_HINTS. */
6803 xcb_icccm_get_wm_normal_hints_reply(conn
,
6804 xcb_icccm_get_wm_normal_hints(conn
, win
->id
),
6808 xcb_icccm_get_wm_hints_reply(conn
,
6809 xcb_icccm_get_wm_hints(conn
, win
->id
),
6812 /* Get WM_TRANSIENT_FOR; see if window is a transient. */
6813 xcb_icccm_get_wm_transient_for_reply(conn
,
6814 xcb_icccm_get_wm_transient_for(conn
, win
->id
),
6817 win
->transient
= trans
;
6818 set_child_transient(win
, &win
->transient
);
6821 /* Get supported protocols. */
6822 if (xcb_icccm_get_wm_protocols_reply(conn
,
6823 xcb_icccm_get_wm_protocols(conn
, win
->id
, a_prot
),
6825 for (i
= 0; i
< wpr
.atoms_len
; i
++) {
6826 if (wpr
.atoms
[i
] == a_takefocus
)
6827 win
->take_focus
= 1;
6828 if (wpr
.atoms
[i
] == a_delete
)
6829 win
->can_delete
= 1;
6831 xcb_icccm_get_wm_protocols_reply_wipe(&wpr
);
6834 win
->iconic
= get_swm_iconic(win
);
6836 /* Figure out which workspace the window belongs to. */
6837 if ((p
= find_pid(window_get_pid(win
->id
))) != NULL
) {
6838 win
->ws
= &r
->s
->ws
[p
->ws
];
6839 TAILQ_REMOVE(&pidlist
, p
, entry
);
6842 } else if ((ws_idx
= get_ws_idx(win
->id
)) != -1 &&
6843 win
->transient
== 0) {
6844 /* _SWM_WS is set; use that. */
6845 win
->ws
= &r
->s
->ws
[ws_idx
];
6846 } else if (trans
&& (ww
= find_window(trans
)) != NULL
) {
6847 /* Launch transients in the same ws as parent. */
6854 /* Set the _SWM_WS atom so we can remember this after reincarnation. */
6855 if (snprintf(ws_idx_str
, SWM_PROPLEN
, "%d", win
->ws
->idx
) <
6857 DNPRINTF(SWM_D_PROP
, "manage_window: set _SWM_WS: %s\n",
6859 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
6860 a_swm_ws
, XCB_ATOM_STRING
, 8, strlen(ws_idx_str
),
6865 ewmh_autoquirk(win
);
6867 /* Determine initial quirks. */
6868 if (xcb_icccm_get_wm_class_reply(conn
,
6869 xcb_icccm_get_wm_class(conn
, win
->id
),
6871 DNPRINTF(SWM_D_CLASS
, "manage_window: class: %s, name: %s\n",
6872 win
->ch
.class_name
, win
->ch
.instance_name
);
6874 /* java is retarded so treat it special */
6875 if (strstr(win
->ch
.instance_name
, "sun-awt")) {
6876 DNPRINTF(SWM_D_CLASS
, "manage_window: java window "
6882 TAILQ_FOREACH(qp
, &quirks
, entry
) {
6883 if (!strcmp(win
->ch
.class_name
, qp
->class) &&
6884 !strcmp(win
->ch
.instance_name
, qp
->name
)) {
6885 DNPRINTF(SWM_D_CLASS
, "manage_window: on quirks"
6886 "list; mask: 0x%lx\n", qp
->quirk
);
6887 if (qp
->quirk
& SWM_Q_FLOAT
) {
6891 win
->quirks
= qp
->quirk
;
6896 /* Alter window position if quirky */
6897 if (win
->quirks
& SWM_Q_ANYWHERE
) {
6902 /* Reset font sizes (the bruteforce way; no default keybinding). */
6903 if (win
->quirks
& SWM_Q_XTERM_FONTADJ
) {
6904 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
6905 fake_keypress(win
, XK_KP_Subtract
, XCB_MOD_MASK_SHIFT
);
6906 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
6907 fake_keypress(win
, XK_KP_Add
, XCB_MOD_MASK_SHIFT
);
6912 X(win
) -= border_width
;
6913 Y(win
) -= border_width
;
6916 /* Make sure window is positioned inside its region, if its active. */
6918 constrain_window(win
, win
->ws
->r
, 0);
6920 if (win
->ws
->r
|| border_me
)
6923 /* Select which X events to monitor. */
6924 event_mask
= XCB_EVENT_MASK_ENTER_WINDOW
| XCB_EVENT_MASK_FOCUS_CHANGE
|
6925 XCB_EVENT_MASK_PROPERTY_CHANGE
| XCB_EVENT_MASK_STRUCTURE_NOTIFY
;
6927 event_mask
|= XCB_EVENT_MASK_LEAVE_WINDOW
;
6930 xcb_change_window_attributes(conn
, win
->id
, XCB_CW_EVENT_MASK
,
6934 /* Figure out where to stack the window in the workspace. */
6935 if (trans
&& (ww
= find_window(trans
)))
6936 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, ww
, win
, entry
);
6937 else if (win
->ws
->focus
&& spawn_position
== SWM_STACK_ABOVE
)
6938 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, win
->ws
->focus
, win
,
6940 else if (win
->ws
->focus
&& spawn_position
== SWM_STACK_BELOW
)
6941 TAILQ_INSERT_BEFORE(win
->ws
->focus
, win
, entry
);
6942 else switch (spawn_position
) {
6945 case SWM_STACK_ABOVE
:
6946 TAILQ_INSERT_TAIL(&win
->ws
->winlist
, win
, entry
);
6948 case SWM_STACK_BOTTOM
:
6949 case SWM_STACK_BELOW
:
6950 TAILQ_INSERT_HEAD(&win
->ws
->winlist
, win
, entry
);
6953 /* Get initial _NET_WM_STATE */
6954 ewmh_get_win_state(win
);
6955 /* Set initial _NET_WM_ALLOWED_ACTIONS */
6956 ewmh_update_actions(win
);
6960 DNPRINTF(SWM_D_MISC
, "manage_window: done. window: 0x%x, (x,y) w x h: "
6961 "(%d,%d) %d x %d, ws: %d, iconic: %s, transient: 0x%x\n", win
->id
,
6962 X(win
), Y(win
), WIDTH(win
), HEIGHT(win
), win
->ws
->idx
,
6963 YESNO(win
->iconic
), win
->transient
);
6969 free_window(struct ws_win
*win
)
6971 DNPRINTF(SWM_D_MISC
, "free_window: window: 0x%x\n", win
->id
);
6976 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
6981 xcb_icccm_get_wm_class_reply_wipe(&win
->ch
);
6986 memset(win
, 0xff, sizeof *win
); /* XXX kill later */
6989 DNPRINTF(SWM_D_MISC
, "free_window: done.\n");
6993 unmanage_window(struct ws_win
*win
)
6995 struct ws_win
*parent
;
7000 DNPRINTF(SWM_D_MISC
, "unmanage_window: window: 0x%x\n", win
->id
);
7002 if (win
->transient
) {
7003 parent
= find_window(win
->transient
);
7005 parent
->focus_child
= NULL
;
7008 focus_win(get_focus_prev(win
));
7010 TAILQ_REMOVE(&win
->ws
->winlist
, win
, entry
);
7011 TAILQ_INSERT_TAIL(&win
->ws
->unmanagedlist
, win
, entry
);
7015 expose(xcb_expose_event_t
*e
)
7018 struct swm_region
*r
;
7020 DNPRINTF(SWM_D_EVENT
, "expose: window: 0x%x\n", e
->window
);
7022 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7023 for (i
= 0; i
< num_screens
; i
++)
7024 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
7025 if (e
->window
== WINID(r
->bar
))
7033 focusin(xcb_focus_in_event_t
*e
)
7035 DNPRINTF(SWM_D_EVENT
, "focusin: window: 0x%x, mode: %s(%u), "
7036 "detail: %s(%u)\n", e
->event
, get_notify_mode_label(e
->mode
),
7037 e
->mode
, get_notify_detail_label(e
->detail
), e
->detail
);
7041 focusout(xcb_focus_out_event_t
*e
)
7043 DNPRINTF(SWM_D_EVENT
, "focusout: window: 0x%x, mode: %s(%u), "
7044 "detail: %s(%u)\n", e
->event
, get_notify_mode_label(e
->mode
),
7045 e
->mode
, get_notify_detail_label(e
->detail
), e
->detail
);
7050 keypress(xcb_key_press_event_t
*e
)
7052 xcb_keysym_t keysym
;
7055 keysym
= xcb_key_press_lookup_keysym(syms
, e
, 0);
7057 DNPRINTF(SWM_D_EVENT
, "keypress: %u %u\n", e
->detail
, keysym
);
7059 if ((kp
= key_lookup(CLEANMASK(e
->state
), keysym
)) == NULL
)
7062 last_event_time
= e
->time
;
7064 if (kp
->funcid
== KF_SPAWN_CUSTOM
)
7065 spawn_custom(root_to_region(e
->root
),
7066 &(keyfuncs
[kp
->funcid
].args
), kp
->spawn_name
);
7067 else if (keyfuncs
[kp
->funcid
].func
)
7068 keyfuncs
[kp
->funcid
].func(root_to_region(e
->root
),
7069 &(keyfuncs
[kp
->funcid
].args
));
7073 buttonpress(xcb_button_press_event_t
*e
)
7079 DNPRINTF(SWM_D_EVENT
, "buttonpress: window 0x%x, detail: %u\n",
7080 e
->event
, e
->detail
);
7082 if ((win
= find_window(e
->event
)) == NULL
)
7085 last_event_time
= e
->time
;
7087 focus_win(get_focus_magic(win
));
7089 for (i
= 0; i
< LENGTH(buttons
); i
++)
7090 if (client_click
== buttons
[i
].action
&& buttons
[i
].func
&&
7091 buttons
[i
].button
== e
->detail
&&
7092 CLEANMASK(buttons
[i
].mask
) == CLEANMASK(e
->state
)) {
7093 buttons
[i
].func(win
, &buttons
[i
].args
);
7098 DNPRINTF(SWM_D_EVENT
, "buttonpress: passing to window.\n");
7099 xcb_allow_events(conn
, XCB_ALLOW_REPLAY_POINTER
, e
->time
);
7101 DNPRINTF(SWM_D_EVENT
, "buttonpress: handled.\n");
7109 print_win_geom(xcb_window_t w
)
7111 xcb_get_geometry_reply_t
*wa
;
7113 wa
= xcb_get_geometry_reply(conn
, xcb_get_geometry(conn
, w
), NULL
);
7116 DNPRINTF(SWM_D_MISC
, "print_win_geom: window not found: 0x%x\n",
7121 DNPRINTF(SWM_D_MISC
, "print_win_geom: window: 0x%x, root: 0x%x, "
7122 "depth: %u, (x,y) w x h: (%d,%d) %d x %d, border: %d\n",
7123 w
, wa
->root
, wa
->depth
, wa
->x
, wa
->y
, wa
->width
, wa
->height
,
7132 get_stack_mode_name(uint8_t mode
)
7137 case XCB_STACK_MODE_ABOVE
:
7140 case XCB_STACK_MODE_BELOW
:
7143 case XCB_STACK_MODE_TOP_IF
:
7146 case XCB_STACK_MODE_BOTTOM_IF
:
7149 case XCB_STACK_MODE_OPPOSITE
:
7161 configurerequest(xcb_configure_request_event_t
*e
)
7164 struct swm_region
*r
= NULL
;
7167 uint32_t wc
[7] = {0};
7169 if ((win
= find_window(e
->window
)) == NULL
)
7170 if ((win
= find_unmanaged_window(e
->window
)) == NULL
)
7174 if (swm_debug
& SWM_D_EVENT
) {
7175 print_win_geom(e
->window
);
7177 DNPRINTF(SWM_D_EVENT
, "configurerequest: window: 0x%x, "
7178 "parent: 0x%x, new: %s, value_mask: %u { ", e
->window
,
7179 e
->parent
, YESNO(new), e
->value_mask
);
7180 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
)
7181 DPRINTF("X: %d ", e
->x
);
7182 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
)
7183 DPRINTF("Y: %d ", e
->y
);
7184 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
)
7185 DPRINTF("W: %u ", e
->width
);
7186 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
)
7187 DPRINTF("H: %u ", e
->height
);
7188 if (e
->value_mask
& XCB_CONFIG_WINDOW_BORDER_WIDTH
)
7189 DPRINTF("Border: %u ", e
->border_width
);
7190 if (e
->value_mask
& XCB_CONFIG_WINDOW_SIBLING
)
7191 DPRINTF("Sibling: 0x%x ", e
->sibling
);
7192 if (e
->value_mask
& XCB_CONFIG_WINDOW_STACK_MODE
)
7193 DPRINTF("StackMode: %s(%u) ",
7194 get_stack_mode_name(e
->stack_mode
), e
->stack_mode
);
7200 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
) {
7201 mask
|= XCB_CONFIG_WINDOW_X
;
7204 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
) {
7205 mask
|= XCB_CONFIG_WINDOW_Y
;
7208 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
) {
7209 mask
|= XCB_CONFIG_WINDOW_WIDTH
;
7212 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
) {
7213 mask
|= XCB_CONFIG_WINDOW_HEIGHT
;
7214 wc
[i
++] = e
->height
;
7216 if (e
->value_mask
& XCB_CONFIG_WINDOW_BORDER_WIDTH
) {
7217 mask
|= XCB_CONFIG_WINDOW_BORDER_WIDTH
;
7218 wc
[i
++] = e
->border_width
;
7220 if (e
->value_mask
& XCB_CONFIG_WINDOW_SIBLING
) {
7221 mask
|= XCB_CONFIG_WINDOW_SIBLING
;
7222 wc
[i
++] = e
->sibling
;
7224 if (e
->value_mask
& XCB_CONFIG_WINDOW_STACK_MODE
) {
7225 mask
|= XCB_CONFIG_WINDOW_STACK_MODE
;
7226 wc
[i
++] = e
->stack_mode
;
7230 xcb_configure_window(conn
, e
->window
, mask
, wc
);
7231 } else if ((!win
->manual
|| win
->quirks
& SWM_Q_ANYWHERE
) &&
7232 !(win
->ewmh_flags
& EWMH_F_FULLSCREEN
)) {
7235 else if (win
->ws
->old_r
)
7238 /* windows are centered unless ANYWHERE quirk is set. */
7239 if (win
->quirks
& SWM_Q_ANYWHERE
) {
7240 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
) {
7241 win
->g_float
.x
= e
->x
;
7243 win
->g_float
.x
-= X(r
);
7246 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
) {
7247 win
->g_float
.y
= e
->y
;
7249 win
->g_float
.y
-= Y(r
);
7253 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
)
7254 win
->g_float
.w
= e
->width
;
7256 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
)
7257 win
->g_float
.h
= e
->height
;
7259 win
->g_floatvalid
= 1;
7261 if (win
->floating
&& r
) {
7262 WIDTH(win
) = win
->g_float
.w
;
7263 HEIGHT(win
) = win
->g_float
.h
;
7265 stack_floater(win
, win
->ws
->r
);
7279 configurenotify(xcb_configure_notify_event_t
*e
)
7283 DNPRINTF(SWM_D_EVENT
, "configurenotify: window: 0x%x\n",
7286 win
= find_window(e
->window
);
7288 xcb_icccm_get_wm_normal_hints_reply(conn
,
7289 xcb_icccm_get_wm_normal_hints(conn
, win
->id
),
7292 if (font_adjusted
) {
7300 destroynotify(xcb_destroy_notify_event_t
*e
)
7304 DNPRINTF(SWM_D_EVENT
, "destroynotify: window: 0x%x\n", e
->window
);
7306 if ((win
= find_window(e
->window
)) == NULL
) {
7307 if ((win
= find_unmanaged_window(e
->window
)) == NULL
)
7313 /* make sure we focus on something */
7316 unmanage_window(win
);
7326 get_notify_detail_label(uint8_t detail
)
7331 case XCB_NOTIFY_DETAIL_ANCESTOR
:
7334 case XCB_NOTIFY_DETAIL_VIRTUAL
:
7337 case XCB_NOTIFY_DETAIL_INFERIOR
:
7340 case XCB_NOTIFY_DETAIL_NONLINEAR
:
7341 label
= "Nonlinear";
7343 case XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL
:
7344 label
= "NonlinearVirtual";
7346 case XCB_NOTIFY_DETAIL_POINTER
:
7349 case XCB_NOTIFY_DETAIL_POINTER_ROOT
:
7350 label
= "PointerRoot";
7352 case XCB_NOTIFY_DETAIL_NONE
:
7363 get_notify_mode_label(uint8_t mode
)
7368 case XCB_NOTIFY_MODE_NORMAL
:
7371 case XCB_NOTIFY_MODE_GRAB
:
7374 case XCB_NOTIFY_MODE_UNGRAB
:
7377 case XCB_NOTIFY_MODE_WHILE_GRABBED
:
7378 label
= "WhileGrabbed";
7389 enternotify(xcb_enter_notify_event_t
*e
)
7393 DNPRINTF(SWM_D_FOCUS
, "enternotify: time: %u, win (x,y): 0x%x "
7394 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): 0x%x (%d,%d), "
7395 "child: 0x%x, same_screen_focus: %s, state: %d\n",
7396 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
7397 get_notify_mode_label(e
->mode
), e
->mode
,
7398 get_notify_detail_label(e
->detail
), e
->detail
,
7399 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
7400 YESNO(e
->same_screen_focus
), e
->state
);
7402 if (focus_mode
== SWM_FOCUS_MANUAL
&&
7403 e
->mode
== XCB_NOTIFY_MODE_NORMAL
) {
7404 DNPRINTF(SWM_D_EVENT
, "enternotify: manual focus; ignoring.\n");
7408 if ((win
= find_window(e
->event
)) == NULL
) {
7409 DNPRINTF(SWM_D_EVENT
, "enternotify: window is NULL; ignoring\n");
7413 last_event_time
= e
->time
;
7415 focus_win(get_focus_magic(win
));
7422 leavenotify(xcb_leave_notify_event_t
*e
)
7424 DNPRINTF(SWM_D_FOCUS
, "leavenotify: time: %u, win (x,y): 0x%x "
7425 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): 0x%x (%d,%d), "
7426 "child: 0x%x, same_screen_focus: %s, state: %d\n",
7427 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
7428 get_notify_mode_label(e
->mode
), e
->mode
,
7429 get_notify_detail_label(e
->detail
), e
->detail
,
7430 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
7431 YESNO(e
->same_screen_focus
), e
->state
);
7435 /* lets us use one switch statement for arbitrary mode/detail combinations */
7436 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
7439 mapnotify(xcb_map_notify_event_t
*e
)
7443 DNPRINTF(SWM_D_EVENT
, "mapnotify: window: 0x%x\n", e
->window
);
7445 if ((win
= find_window(e
->window
)) == NULL
)
7446 win
= manage_window(e
->window
, 1);
7449 set_win_state(win
, XCB_ICCCM_WM_STATE_NORMAL
);
7451 /* Focus on window if it is selected. */
7452 if (win
->ws
->focus
== win
)
7459 mappingnotify(xcb_mapping_notify_event_t
*e
)
7461 xcb_refresh_keyboard_mapping(syms
, e
);
7463 if (e
->request
== XCB_MAPPING_KEYBOARD
)
7468 maprequest(xcb_map_request_event_t
*e
)
7471 xcb_get_window_attributes_reply_t
*war
;
7473 DNPRINTF(SWM_D_EVENT
, "maprequest: win 0x%x\n",
7476 war
= xcb_get_window_attributes_reply(conn
,
7477 xcb_get_window_attributes(conn
, e
->window
),
7480 DNPRINTF(SWM_D_EVENT
, "maprequest: window lost.\n");
7484 if (war
->override_redirect
) {
7485 DNPRINTF(SWM_D_EVENT
, "maprequest: override_redirect; "
7490 win
= manage_window(e
->window
,
7491 (war
->map_state
== XCB_MAP_STATE_VIEWABLE
));
7493 /* All windows need to be mapped if they are in the current workspace.*/
7497 /* The new window should get focus. */
7498 win
->ws
->focus
= get_focus_magic(win
);
7500 /* Ignore EnterNotify to handle the mapnotify without interference. */
7501 if (focus_mode
== SWM_FOCUS_DEFAULT
)
7502 event_drain(XCB_ENTER_NOTIFY
);
7505 DNPRINTF(SWM_D_EVENT
, "maprequest: done.\n");
7510 get_atom_name(xcb_atom_t atom
)
7515 * This should be disabled during most debugging since
7516 * xcb_get_* causes an xcb_flush.
7519 xcb_get_atom_name_reply_t
*r
;
7521 r
= xcb_get_atom_name_reply(conn
,
7522 xcb_get_atom_name(conn
, atom
),
7525 len
= xcb_get_atom_name_name_length(r
);
7527 name
= malloc(len
+ 1);
7529 memcpy(name
, xcb_get_atom_name_name(r
), len
);
7543 propertynotify(xcb_property_notify_event_t
*e
)
7549 name
= get_atom_name(e
->atom
);
7550 DNPRINTF(SWM_D_EVENT
, "propertynotify: window: 0x%x, atom: %s(%u), "
7551 "time: %#x\n", e
->window
, name
, e
->atom
, e
->time
);
7554 win
= find_window(e
->window
);
7558 last_event_time
= e
->time
;
7560 if (e
->atom
== a_swm_iconic
) {
7561 if (e
->state
== XCB_PROPERTY_DELETE
) {
7562 /* The window is no longer iconic, restack ws. */
7565 /* The window should get focus. */
7566 win
->ws
->focus
= get_focus_magic(win
);
7568 /* Flush EnterNotify for mapnotify, if needed. */
7571 } else if (e
->state
== XCB_PROPERTY_NEW_VALUE
) {
7572 win
->ws
->focus
= NULL
;
7578 focus_win(get_focus_prev(win
));
7583 } else if (e
->atom
== a_state
&& e
->state
== XCB_PROPERTY_NEW_VALUE
) {
7584 /* Focus on window if it is selected. */
7585 if (win
->ws
->focus
== win
)
7590 case XCB_ATOM_WM_CLASS
:
7591 case XCB_ATOM_WM_NAME
:
7602 unmapnotify(xcb_unmap_notify_event_t
*e
)
7606 DNPRINTF(SWM_D_EVENT
, "unmapnotify: window: 0x%x\n", e
->window
);
7608 /* determine if we need to help unmanage this window */
7609 win
= find_window(e
->window
);
7613 if (getstate(e
->window
) == XCB_ICCCM_WM_STATE_NORMAL
) {
7614 unmanage_window(win
);
7622 visibilitynotify(xcb_visibility_notify_event_t
*e
)
7624 DNPRINTF(SWM_D_EVENT
, "visibilitynotify: window: 0x%x\n",
7630 clientmessage(xcb_client_message_event_t
*e
)
7633 xcb_map_request_event_t mre
;
7637 name
= get_atom_name(e
->type
);
7638 DNPRINTF(SWM_D_EVENT
, "clientmessage: window: 0x%x, atom: %s(%u)\n",
7639 e
->window
, name
, e
->type
);
7642 win
= find_window(e
->window
);
7645 if (e
->type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
7646 DNPRINTF(SWM_D_EVENT
, "clientmessage: request focus on "
7647 "unmanaged window.\n");
7648 mre
.window
= e
->window
;
7654 if (e
->type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
7655 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_ACTIVE_WINDOW\n");
7658 if (e
->type
== ewmh
[_NET_CLOSE_WINDOW
].atom
) {
7659 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_CLOSE_WINDOW\n");
7660 if (win
->can_delete
)
7661 client_msg(win
, a_delete
, 0);
7663 xcb_kill_client(conn
, win
->id
);
7665 if (e
->type
== ewmh
[_NET_MOVERESIZE_WINDOW
].atom
) {
7666 DNPRINTF(SWM_D_EVENT
,
7667 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
7668 if (win
->floating
) {
7669 if (e
->data
.data32
[0] & (1<<8)) /* x */
7670 X(win
) = e
->data
.data32
[1];
7671 if (e
->data
.data32
[0] & (1<<9)) /* y */
7672 Y(win
) = e
->data
.data32
[2];
7673 if (e
->data
.data32
[0] & (1<<10)) /* width */
7674 WIDTH(win
) = e
->data
.data32
[3];
7675 if (e
->data
.data32
[0] & (1<<11)) /* height */
7676 HEIGHT(win
) = e
->data
.data32
[4];
7681 /* TODO: Change stack sizes */
7682 /* notify no change was made. */
7683 config_win(win
, NULL
);
7686 if (e
->type
== ewmh
[_NET_WM_STATE
].atom
) {
7687 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_WM_STATE\n");
7688 ewmh_update_win_state(win
, e
->data
.data32
[1], e
->data
.data32
[0]);
7689 if (e
->data
.data32
[2])
7690 ewmh_update_win_state(win
, e
->data
.data32
[2],
7702 int errcode
= xcb_connection_has_error(conn
);
7703 #ifdef XCB_CONN_ERROR
7706 case XCB_CONN_ERROR
:
7707 s
= "Socket error, pipe error or other stream error.";
7709 case XCB_CONN_CLOSED_EXT_NOTSUPPORTED
:
7710 s
= "Extension not supported.";
7712 case XCB_CONN_CLOSED_MEM_INSUFFICIENT
:
7713 s
= "Insufficient memory.";
7715 case XCB_CONN_CLOSED_REQ_LEN_EXCEED
:
7716 s
= "Request length was exceeded.";
7718 case XCB_CONN_CLOSED_PARSE_ERR
:
7719 s
= "Error parsing display string.";
7722 s
= "Unknown error.";
7725 errx(errcode
, "X CONNECTION ERROR: %s", s
);
7728 errx(errcode
, "X CONNECTION ERROR");
7736 const uint32_t val
= XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT
;
7738 xcb_void_cookie_t wac
;
7739 xcb_generic_error_t
*error
;
7741 /* this causes an error if some other window manager is running */
7742 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7743 for (i
= 0; i
< num_screens
; i
++) {
7744 if ((sc
= get_screen(i
)) == NULL
)
7745 errx(1, "ERROR: can't get screen %d.", i
);
7746 DNPRINTF(SWM_D_INIT
, "enable_wm: screen %d, root: 0x%x\n",
7748 wac
= xcb_change_window_attributes_checked(conn
, sc
->root
,
7749 XCB_CW_EVENT_MASK
, &val
);
7750 if ((error
= xcb_request_check(conn
, wac
))) {
7751 DNPRINTF(SWM_D_INIT
, "enable_wm: error_code: %u\n",
7762 new_region(struct swm_screen
*s
, int x
, int y
, int w
, int h
)
7764 struct swm_region
*r
, *n
;
7765 struct workspace
*ws
= NULL
;
7768 DNPRINTF(SWM_D_MISC
, "new region: screen[%d]:%dx%d+%d+%d\n",
7769 s
->idx
, w
, h
, x
, y
);
7771 /* remove any conflicting regions */
7772 n
= TAILQ_FIRST(&s
->rl
);
7775 n
= TAILQ_NEXT(r
, entry
);
7776 if (X(r
) < (x
+ w
) &&
7777 (X(r
) + WIDTH(r
)) > x
&&
7779 (Y(r
) + HEIGHT(r
)) > y
) {
7780 if (r
->ws
->r
!= NULL
)
7781 r
->ws
->old_r
= r
->ws
->r
;
7784 TAILQ_REMOVE(&s
->rl
, r
, entry
);
7785 TAILQ_INSERT_TAIL(&s
->orl
, r
, entry
);
7789 /* search old regions for one to reuse */
7791 /* size + location match */
7792 TAILQ_FOREACH(r
, &s
->orl
, entry
)
7793 if (X(r
) == x
&& Y(r
) == y
&&
7794 HEIGHT(r
) == h
&& WIDTH(r
) == w
)
7798 TAILQ_FOREACH(r
, &s
->orl
, entry
)
7799 if (HEIGHT(r
) == h
&& WIDTH(r
) == w
)
7803 TAILQ_REMOVE(&s
->orl
, r
, entry
);
7804 /* try to use old region's workspace */
7805 if (r
->ws
->r
== NULL
)
7808 if ((r
= calloc(1, sizeof(struct swm_region
))) == NULL
)
7809 err(1, "new_region: calloc: failed to allocate memory "
7812 /* if we don't have a workspace already, find one */
7814 for (i
= 0; i
< workspace_limit
; i
++)
7815 if (s
->ws
[i
].r
== NULL
) {
7822 errx(1, "new_region: no free workspaces");
7833 TAILQ_INSERT_TAIL(&s
->rl
, r
, entry
);
7839 #ifdef SWM_XRR_HAS_CRTC
7842 #endif /* SWM_XRR_HAS_CRTC */
7843 struct swm_region
*r
;
7845 xcb_randr_get_screen_resources_current_cookie_t src
;
7846 xcb_randr_get_screen_resources_current_reply_t
*srr
;
7847 xcb_randr_get_crtc_info_cookie_t cic
;
7848 xcb_randr_get_crtc_info_reply_t
*cir
= NULL
;
7849 xcb_randr_crtc_t
*crtc
;
7850 xcb_screen_t
*screen
;
7852 if ((screen
= get_screen(i
)) == NULL
)
7853 errx(1, "ERROR: can't get screen %d.", i
);
7855 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7856 if (i
>= num_screens
)
7857 errx(1, "scan_xrandr: invalid screen");
7859 /* remove any old regions */
7860 while ((r
= TAILQ_FIRST(&screens
[i
].rl
)) != NULL
) {
7861 r
->ws
->old_r
= r
->ws
->r
= NULL
;
7863 TAILQ_REMOVE(&screens
[i
].rl
, r
, entry
);
7864 TAILQ_INSERT_TAIL(&screens
[i
].orl
, r
, entry
);
7868 /* map virtual screens onto physical screens */
7869 #ifdef SWM_XRR_HAS_CRTC
7870 if (xrandr_support
) {
7871 src
= xcb_randr_get_screen_resources_current(conn
,
7873 srr
= xcb_randr_get_screen_resources_current_reply(conn
, src
,
7876 new_region(&screens
[i
], 0, 0,
7877 screen
->width_in_pixels
,
7878 screen
->height_in_pixels
);
7881 ncrtc
= srr
->num_crtcs
;
7883 crtc
= xcb_randr_get_screen_resources_current_crtcs(srr
);
7884 for (c
= 0; c
< ncrtc
; c
++) {
7885 cic
= xcb_randr_get_crtc_info(conn
, crtc
[c
],
7887 cir
= xcb_randr_get_crtc_info_reply(conn
, cic
, NULL
);
7890 if (cir
->num_outputs
== 0) {
7896 new_region(&screens
[i
], 0, 0,
7897 screen
->width_in_pixels
,
7898 screen
->height_in_pixels
);
7900 new_region(&screens
[i
],
7901 cir
->x
, cir
->y
, cir
->width
, cir
->height
);
7906 #endif /* SWM_XRR_HAS_CRTC */
7908 new_region(&screens
[i
], 0, 0, screen
->width_in_pixels
,
7909 screen
->height_in_pixels
);
7914 screenchange(xcb_randr_screen_change_notify_event_t
*e
)
7916 struct swm_region
*r
;
7919 DNPRINTF(SWM_D_EVENT
, "screenchange: root: 0x%x\n", e
->root
);
7921 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7922 /* silly event doesn't include the screen index */
7923 for (i
= 0; i
< num_screens
; i
++)
7924 if (screens
[i
].root
== e
->root
)
7926 if (i
>= num_screens
)
7927 errx(1, "screenchange: screen not found");
7929 /* brute force for now, just re-enumerate the regions */
7933 print_win_geom(e
->root
);
7935 /* add bars to all regions */
7936 for (i
= 0; i
< num_screens
; i
++)
7937 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
7945 xcb_window_t
*wins
= NULL
;
7947 int i
, j
, num_screens
;
7948 uint16_t state
, manage
, mapped
;
7950 xcb_query_tree_cookie_t qtc
;
7951 xcb_query_tree_reply_t
*qtr
;
7952 xcb_get_window_attributes_cookie_t c
;
7953 xcb_get_window_attributes_reply_t
*r
;
7954 xcb_get_property_cookie_t pc
;
7956 DNPRINTF(SWM_D_INIT
, "grab_windows: begin\n");
7957 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7958 for (i
= 0; i
< num_screens
; i
++) {
7959 qtc
= xcb_query_tree(conn
, screens
[i
].root
);
7960 qtr
= xcb_query_tree_reply(conn
, qtc
, NULL
);
7963 wins
= xcb_query_tree_children(qtr
);
7964 no
= xcb_query_tree_children_length(qtr
);
7965 /* attach windows to a region */
7966 /* normal windows */
7967 for (j
= 0; j
< no
; j
++) {
7968 c
= xcb_get_window_attributes(conn
, wins
[j
]);
7969 r
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
7972 if (r
->override_redirect
) {
7977 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
7978 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
7984 state
= getstate(wins
[j
]);
7985 manage
= state
== XCB_ICCCM_WM_STATE_ICONIC
;
7986 mapped
= r
->map_state
!= XCB_MAP_STATE_UNMAPPED
;
7987 if (mapped
|| manage
)
7988 manage_window(wins
[j
], mapped
);
7991 /* transient windows */
7992 for (j
= 0; j
< no
; j
++) {
7993 c
= xcb_get_window_attributes(conn
, wins
[j
]);
7994 r
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
7997 if (r
->override_redirect
) {
8003 state
= getstate(wins
[j
]);
8004 manage
= state
== XCB_ICCCM_WM_STATE_ICONIC
;
8005 mapped
= r
->map_state
!= XCB_MAP_STATE_UNMAPPED
;
8006 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
8007 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
8008 &wins
[j
], NULL
) && manage
)
8009 manage_window(wins
[j
], mapped
);
8013 DNPRINTF(SWM_D_INIT
, "grab_windows: done.\n");
8019 int i
, j
, k
, num_screens
;
8020 struct workspace
*ws
;
8021 uint32_t gcv
[1], wa
[1];
8022 const xcb_query_extension_reply_t
*qep
;
8023 xcb_screen_t
*screen
;
8024 xcb_cursor_t cursor
;
8025 xcb_font_t cursor_font
;
8026 xcb_randr_query_version_cookie_t c
;
8027 xcb_randr_query_version_reply_t
*r
;
8029 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
8030 if ((screens
= calloc(num_screens
,
8031 sizeof(struct swm_screen
))) == NULL
)
8032 err(1, "setup_screens: calloc: failed to allocate memory for "
8035 /* initial Xrandr setup */
8037 qep
= xcb_get_extension_data(conn
, &xcb_randr_id
);
8039 c
= xcb_randr_query_version(conn
, 1, 1);
8040 r
= xcb_randr_query_version_reply(conn
, c
, NULL
);
8042 if (r
->major_version
>= 1) {
8044 xrandr_eventbase
= qep
->first_event
;
8050 cursor_font
= xcb_generate_id(conn
);
8051 xcb_open_font(conn
, cursor_font
, strlen("cursor"), "cursor");
8053 cursor
= xcb_generate_id(conn
);
8054 xcb_create_glyph_cursor(conn
, cursor
, cursor_font
, cursor_font
,
8055 XC_left_ptr
, XC_left_ptr
+ 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
8058 /* map physical screens */
8059 for (i
= 0; i
< num_screens
; i
++) {
8060 DNPRINTF(SWM_D_WS
, "setup_screens: init screen: %d\n", i
);
8062 TAILQ_INIT(&screens
[i
].rl
);
8063 TAILQ_INIT(&screens
[i
].orl
);
8064 if ((screen
= get_screen(i
)) == NULL
)
8065 errx(1, "ERROR: can't get screen %d.", i
);
8066 screens
[i
].root
= screen
->root
;
8068 /* set default colors */
8069 setscreencolor("red", i
+ 1, SWM_S_COLOR_FOCUS
);
8070 setscreencolor("rgb:88/88/88", i
+ 1, SWM_S_COLOR_UNFOCUS
);
8071 setscreencolor("rgb:00/80/80", i
+ 1, SWM_S_COLOR_BAR_BORDER
);
8072 setscreencolor("black", i
+ 1, SWM_S_COLOR_BAR
);
8073 setscreencolor("rgb:a0/a0/a0", i
+ 1, SWM_S_COLOR_BAR_FONT
);
8075 /* create graphics context on screen */
8076 screens
[i
].bar_gc
= xcb_generate_id(conn
);
8078 xcb_create_gc(conn
, screens
[i
].bar_gc
, screens
[i
].root
,
8079 XCB_GC_GRAPHICS_EXPOSURES
, gcv
);
8081 /* set default cursor */
8082 xcb_change_window_attributes(conn
, screens
[i
].root
,
8085 /* init all workspaces */
8086 /* XXX these should be dynamically allocated too */
8087 for (j
= 0; j
< SWM_WS_MAX
; j
++) {
8088 ws
= &screens
[i
].ws
[j
];
8094 TAILQ_INIT(&ws
->winlist
);
8095 TAILQ_INIT(&ws
->unmanagedlist
);
8097 for (k
= 0; layouts
[k
].l_stack
!= NULL
; k
++)
8098 if (layouts
[k
].l_config
!= NULL
)
8099 layouts
[k
].l_config(ws
,
8100 SWM_ARG_ID_STACKINIT
);
8101 ws
->cur_layout
= &layouts
[0];
8102 ws
->cur_layout
->l_string(ws
);
8108 xcb_randr_select_input(conn
, screens
[i
].root
,
8109 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE
);
8111 xcb_free_cursor(conn
, cursor
);
8112 xcb_close_font(conn
, cursor_font
);
8118 if ((bar_fonts
= strdup(SWM_BAR_FONTS
)) == NULL
)
8119 err(1, "setup_globals: strdup: failed to allocate memory.");
8121 if ((clock_format
= strdup("%a %b %d %R %Z %Y")) == NULL
)
8122 err(1, "setup_globals: strdup: failed to allocate memory.");
8124 if ((syms
= xcb_key_symbols_alloc(conn
)) == NULL
)
8125 errx(1, "unable to allocate key symbols");
8127 a_state
= get_atom_from_string("WM_STATE");
8128 a_prot
= get_atom_from_string("WM_PROTOCOLS");
8129 a_delete
= get_atom_from_string("WM_DELETE_WINDOW");
8130 a_takefocus
= get_atom_from_string("WM_TAKE_FOCUS");
8131 a_wmname
= get_atom_from_string("WM_NAME");
8132 a_netwmname
= get_atom_from_string("_NET_WM_NAME");
8133 a_utf8_string
= get_atom_from_string("UTF8_STRING");
8134 a_string
= get_atom_from_string("STRING");
8135 a_swm_iconic
= get_atom_from_string("_SWM_ICONIC");
8136 a_swm_ws
= get_atom_from_string("_SWM_WS");
8143 xcb_atom_t netwmcheck
;
8144 xcb_window_t root
, win
;
8146 /* work around sun jdk bugs, code from wmname */
8147 netwmcheck
= get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
8149 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
8150 for (i
= 0; i
< num_screens
; i
++) {
8151 root
= screens
[i
].root
;
8153 win
= xcb_generate_id(conn
);
8154 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, win
, root
,
8155 0, 0, 1, 1, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
8156 XCB_COPY_FROM_PARENT
, 0, NULL
);
8158 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, root
,
8159 netwmcheck
, XCB_ATOM_WINDOW
, 32, 1, &win
);
8160 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
8161 netwmcheck
, XCB_ATOM_WINDOW
, 32, 1, &win
);
8162 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
8163 a_netwmname
, a_utf8_string
, 8, strlen("LG3D"), "LG3D");
8168 shutdown_cleanup(void)
8172 /* disable alarm because the following code may not be interrupted */
8174 if (signal(SIGALRM
, SIG_IGN
) == SIG_ERR
)
8175 err(1, "can't disable alarm");
8183 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
8184 for (i
= 0; i
< num_screens
; ++i
) {
8185 if (screens
[i
].bar_gc
!= 0)
8186 xcb_free_gc(conn
, screens
[i
].bar_gc
);
8187 if (!bar_font_legacy
)
8188 XftColorFree(display
, DefaultVisual(display
, i
),
8189 DefaultColormap(display
, i
), &bar_font_color
);
8192 if (bar_font_legacy
)
8193 XFreeFontSet(display
, bar_fs
);
8195 XftFontClose(display
, bar_font
);
8198 xcb_key_symbols_free(syms
);
8200 xcb_disconnect(conn
);
8204 event_error(xcb_generic_error_t
*e
)
8208 DNPRINTF(SWM_D_EVENT
, "event_error: %s(%u) from %s(%u), sequence: %u, "
8209 "resource_id: %u, minor_code: %u\n",
8210 xcb_event_get_error_label(e
->error_code
), e
->error_code
,
8211 xcb_event_get_request_label(e
->major_code
), e
->major_code
,
8212 e
->sequence
, e
->resource_id
, e
->minor_code
);
8216 event_handle(xcb_generic_event_t
*evt
)
8218 uint8_t type
= XCB_EVENT_RESPONSE_TYPE(evt
);
8220 DNPRINTF(SWM_D_EVENT
, "XCB Event: %s(%d), seq %u\n",
8221 xcb_event_get_label(XCB_EVENT_RESPONSE_TYPE(evt
)),
8222 XCB_EVENT_RESPONSE_TYPE(evt
), evt
->sequence
);
8225 #define EVENT(type, callback) case type: callback((void *)evt); return
8226 EVENT(0, event_error
);
8227 EVENT(XCB_BUTTON_PRESS
, buttonpress
);
8228 /*EVENT(XCB_BUTTON_RELEASE, buttonpress);*/
8229 /*EVENT(XCB_CIRCULATE_NOTIFY, );*/
8230 /*EVENT(XCB_CIRCULATE_REQUEST, );*/
8231 EVENT(XCB_CLIENT_MESSAGE
, clientmessage
);
8232 /*EVENT(XCB_COLORMAP_NOTIFY, );*/
8233 EVENT(XCB_CONFIGURE_NOTIFY
, configurenotify
);
8234 EVENT(XCB_CONFIGURE_REQUEST
, configurerequest
);
8235 /*EVENT(XCB_CREATE_NOTIFY, );*/
8236 EVENT(XCB_DESTROY_NOTIFY
, destroynotify
);
8237 EVENT(XCB_ENTER_NOTIFY
, enternotify
);
8238 EVENT(XCB_EXPOSE
, expose
);
8240 EVENT(XCB_FOCUS_IN
, focusin
);
8241 EVENT(XCB_FOCUS_OUT
, focusout
);
8243 /*EVENT(XCB_GRAPHICS_EXPOSURE, );*/
8244 /*EVENT(XCB_GRAVITY_NOTIFY, );*/
8245 EVENT(XCB_KEY_PRESS
, keypress
);
8246 /*EVENT(XCB_KEY_RELEASE, keypress);*/
8247 /*EVENT(XCB_KEYMAP_NOTIFY, );*/
8249 EVENT(XCB_LEAVE_NOTIFY
, leavenotify
);
8251 EVENT(XCB_MAP_NOTIFY
, mapnotify
);
8252 EVENT(XCB_MAP_REQUEST
, maprequest
);
8253 EVENT(XCB_MAPPING_NOTIFY
, mappingnotify
);
8254 /*EVENT(XCB_MOTION_NOTIFY, );*/
8255 /*EVENT(XCB_NO_EXPOSURE, );*/
8256 EVENT(XCB_PROPERTY_NOTIFY
, propertynotify
);
8257 /*EVENT(XCB_REPARENT_NOTIFY, );*/
8258 /*EVENT(XCB_RESIZE_REQUEST, );*/
8259 /*EVENT(XCB_SELECTION_CLEAR, );*/
8260 /*EVENT(XCB_SELECTION_NOTIFY, );*/
8261 /*EVENT(XCB_SELECTION_REQUEST, );*/
8262 EVENT(XCB_UNMAP_NOTIFY
, unmapnotify
);
8263 /*EVENT(XCB_VISIBILITY_NOTIFY, visibilitynotify);*/
8266 if (type
- xrandr_eventbase
== XCB_RANDR_SCREEN_CHANGE_NOTIFY
)
8267 screenchange((void *)evt
);
8271 main(int argc
, char *argv
[])
8273 struct swm_region
*r
, *rr
;
8274 struct ws_win
*winfocus
= NULL
;
8275 char conf
[PATH_MAX
], *cfile
= NULL
;
8277 int xfd
, i
, num_screens
;
8278 struct sigaction sact
;
8279 xcb_generic_event_t
*evt
;
8283 /* suppress unused warning since var is needed */
8286 time_started
= time(NULL
);
8289 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION
, buildstr
);
8290 if (!setlocale(LC_CTYPE
, "") || !setlocale(LC_TIME
, ""))
8291 warnx("no locale support");
8293 /* handle some signals */
8294 bzero(&sact
, sizeof(sact
));
8295 sigemptyset(&sact
.sa_mask
);
8297 sact
.sa_handler
= sighdlr
;
8298 sigaction(SIGINT
, &sact
, NULL
);
8299 sigaction(SIGQUIT
, &sact
, NULL
);
8300 sigaction(SIGTERM
, &sact
, NULL
);
8301 sigaction(SIGHUP
, &sact
, NULL
);
8303 sact
.sa_handler
= sighdlr
;
8304 sact
.sa_flags
= SA_NOCLDSTOP
;
8305 sigaction(SIGCHLD
, &sact
, NULL
);
8307 if (!(display
= XOpenDisplay(0)))
8308 errx(1, "can not open display");
8310 conn
= XGetXCBConnection(display
);
8311 if (xcb_connection_has_error(conn
))
8312 errx(1, "can not get XCB connection");
8314 XSetEventQueueOwner(display
, XCBOwnsEventQueue
);
8316 xcb_prefetch_extension_data(conn
, &xcb_randr_id
);
8317 xfd
= xcb_get_file_descriptor(conn
);
8319 /* look for local and global conf file */
8320 pwd
= getpwuid(getuid());
8322 errx(1, "invalid user: %d", getuid());
8324 xcb_grab_server(conn
);
8327 /* flush all events */
8328 while ((evt
= xcb_poll_for_event(conn
))) {
8329 if (XCB_EVENT_RESPONSE_TYPE(evt
) == 0)
8334 if (enable_wm() != 0)
8335 errx(1, "another window manager is currently running");
8346 for (i
= 0; ; i
++) {
8351 snprintf(conf
, sizeof conf
, "%s/.%s",
8352 pwd
->pw_dir
, SWM_CONF_FILE
);
8356 snprintf(conf
, sizeof conf
, "/etc/%s",
8361 snprintf(conf
, sizeof conf
, "%s/.%s",
8362 pwd
->pw_dir
, SWM_CONF_FILE_OLD
);
8366 snprintf(conf
, sizeof conf
, "/etc/%s",
8373 if (strlen(conf
) && stat(conf
, &sb
) != -1)
8374 if (S_ISREG(sb
.st_mode
)) {
8381 /* load conf (if any) */
8383 conf_load(cfile
, SWM_CONF_DEFAULT
);
8386 /* set some values to work around bad programs */
8388 /* grab existing windows (before we build the bars) */
8391 if (getenv("SWM_STARTED") == NULL
)
8392 setenv("SWM_STARTED", "YES", 1);
8394 /* setup all bars */
8395 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
8396 for (i
= 0; i
< num_screens
; i
++)
8397 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
8398 if (winfocus
== NULL
)
8399 winfocus
= TAILQ_FIRST(&r
->ws
->winlist
);
8406 xcb_ungrab_server(conn
);
8410 while ((evt
= xcb_poll_for_event(conn
))) {
8417 /* if we are being restarted go focus on first window */
8419 rr
= winfocus
->ws
->r
;
8421 /* not a visible window */
8425 /* move pointer to first screen if multi screen */
8426 if (num_screens
> 1 || outputs
> 1)
8427 xcb_warp_pointer(conn
, XCB_WINDOW_NONE
,
8428 rr
->s
[0].root
, 0, 0, 0, 0, X(rr
),
8429 Y(rr
) + (bar_enabled
? bar_height
: 0));
8431 focus_win(get_region_focus(rr
));
8441 if (select(xfd
+ 1, &rd
, NULL
, NULL
, &tv
) == -1)
8442 if (errno
!= EINTR
) {
8443 DNPRINTF(SWM_D_MISC
, "select failed");
8445 if (restart_wm
== 1)
8446 restart(NULL
, NULL
);
8447 if (search_resp
== 1)